xref: /openbmc/linux/net/wireless/nl80211.c (revision a1b2f04ea527397fcacacd09e0d690927feef429)
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_ie_attr(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 
211 	for_each_element(elem, data, len) {
212 		/* nothing */
213 	}
214 
215 	if (for_each_element_completed(elem, data, len))
216 		return 0;
217 
218 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
219 	return -EINVAL;
220 }
221 
222 /* policy for the attributes */
223 static const struct nla_policy
224 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
225 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
226 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
227 					.len = U8_MAX },
228 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
229 					     .len = U8_MAX },
230 };
231 
232 static const struct nla_policy
233 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
234 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
235 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
236 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
237 		NLA_POLICY_MAX(NLA_U8, 15),
238 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
239 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
240 		NLA_POLICY_MAX(NLA_U8, 15),
241 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
242 		NLA_POLICY_MAX(NLA_U8, 31),
243 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
244 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
245 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
246 };
247 
248 static const struct nla_policy
249 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
250 	[NL80211_PMSR_TYPE_FTM] =
251 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
252 };
253 
254 static const struct nla_policy
255 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
256 	[NL80211_PMSR_REQ_ATTR_DATA] =
257 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
258 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
259 };
260 
261 static const struct nla_policy
262 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
263 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
264 	/*
265 	 * we could specify this again to be the top-level policy,
266 	 * but that would open us up to recursion problems ...
267 	 */
268 	[NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
269 	[NL80211_PMSR_PEER_ATTR_REQ] =
270 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
271 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
272 };
273 
274 static const struct nla_policy
275 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
276 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
277 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
278 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
279 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
280 	[NL80211_PMSR_ATTR_PEERS] =
281 		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
282 };
283 
284 static const struct nla_policy
285 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
286 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
287 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
288 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
289 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
290 };
291 
292 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
293 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
294 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
295 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
296 				      .len = 20-1 },
297 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
298 
299 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
300 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
301 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
302 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
303 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
304 
305 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
306 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
307 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
308 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
309 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
310 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
311 
312 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
313 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
314 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
315 
316 	[NL80211_ATTR_MAC] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
317 	[NL80211_ATTR_PREV_BSSID] = {
318 		.type = NLA_EXACT_LEN_WARN,
319 		.len = ETH_ALEN
320 	},
321 
322 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
323 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
324 				    .len = WLAN_MAX_KEY_LEN },
325 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 5),
326 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
327 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
328 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
329 	[NL80211_ATTR_KEY_TYPE] =
330 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
331 
332 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
333 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
334 	[NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
335 				       .len = IEEE80211_MAX_DATA_LEN },
336 	[NL80211_ATTR_BEACON_TAIL] =
337 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
338 				       IEEE80211_MAX_DATA_LEN),
339 	[NL80211_ATTR_STA_AID] =
340 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
341 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
342 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
343 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
344 					       .len = NL80211_MAX_SUPP_RATES },
345 	[NL80211_ATTR_STA_PLINK_ACTION] =
346 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
347 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
348 		NLA_POLICY_RANGE(NLA_U8,
349 				 NL80211_TX_POWER_AUTOMATIC,
350 				 NL80211_TX_POWER_FIXED),
351 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
352 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
353 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
354 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
355 				   .len = IEEE80211_MAX_MESH_ID_LEN },
356 	[NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
357 
358 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
359 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
360 
361 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
362 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
363 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
364 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
365 					   .len = NL80211_MAX_SUPP_RATES },
366 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
367 
368 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
369 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
370 
371 	[NL80211_ATTR_HT_CAPABILITY] = {
372 		.type = NLA_EXACT_LEN_WARN,
373 		.len = NL80211_HT_CAPABILITY_LEN
374 	},
375 
376 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
377 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
378 						   validate_ie_attr,
379 						   IEEE80211_MAX_DATA_LEN),
380 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
381 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
382 
383 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
384 				.len = IEEE80211_MAX_SSID_LEN },
385 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
386 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
387 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
388 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
389 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
390 						  NL80211_MFP_NO,
391 						  NL80211_MFP_OPTIONAL),
392 	[NL80211_ATTR_STA_FLAGS2] = {
393 		.len = sizeof(struct nl80211_sta_flag_update),
394 	},
395 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
396 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
397 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
398 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
399 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
400 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
401 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
402 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
403 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
404 	[NL80211_ATTR_PMKID] = {
405 		.type = NLA_EXACT_LEN_WARN,
406 		.len = WLAN_PMKID_LEN
407 	},
408 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
409 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
410 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
411 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
412 				 .len = IEEE80211_MAX_DATA_LEN },
413 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
414 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
415 						   NL80211_PS_DISABLED,
416 						   NL80211_PS_ENABLED),
417 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
418 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
419 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
420 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
421 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
422 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
423 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
424 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
425 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
426 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
427 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
428 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
429 	[NL80211_ATTR_STA_PLINK_STATE] =
430 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
431 	[NL80211_ATTR_MESH_PEER_AID] =
432 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
433 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
434 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
435 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
436 	[NL80211_ATTR_HIDDEN_SSID] =
437 		NLA_POLICY_RANGE(NLA_U32,
438 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
439 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
440 	[NL80211_ATTR_IE_PROBE_RESP] =
441 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
442 				       IEEE80211_MAX_DATA_LEN),
443 	[NL80211_ATTR_IE_ASSOC_RESP] =
444 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
445 				       IEEE80211_MAX_DATA_LEN),
446 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
447 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
448 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
449 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
450 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
451 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
452 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
453 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
454 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
455 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
456 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
457 				      .len = IEEE80211_MAX_DATA_LEN },
458 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
459 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
460 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
461 		.len = NL80211_HT_CAPABILITY_LEN
462 	},
463 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
464 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
465 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
466 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
467 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
468 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
469 	[NL80211_ATTR_VHT_CAPABILITY] = {
470 		.type = NLA_EXACT_LEN_WARN,
471 		.len = NL80211_VHT_CAPABILITY_LEN
472 	},
473 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
474 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
475 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
476 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
477 		NLA_POLICY_RANGE(NLA_U32,
478 				 NL80211_MESH_POWER_UNKNOWN + 1,
479 				 NL80211_MESH_POWER_MAX),
480 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
481 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
482 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
483 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
484 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
485 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
486 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
487 		.len = NL80211_VHT_CAPABILITY_LEN,
488 	},
489 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
490 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
491 				  .len = IEEE80211_MAX_DATA_LEN },
492 	[NL80211_ATTR_PEER_AID] =
493 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
494 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
495 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
496 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
497 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
498 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
499 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
500 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
501 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
502 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
503 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
504 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
505 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
506 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
507 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
508 	[NL80211_ATTR_MAC_HINT] = {
509 		.type = NLA_EXACT_LEN_WARN,
510 		.len = ETH_ALEN
511 	},
512 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
513 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
514 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
515 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
516 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
517 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
518 	[NL80211_ATTR_USER_PRIO] =
519 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
520 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
521 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
522 	[NL80211_ATTR_MAC_MASK] = {
523 		.type = NLA_EXACT_LEN_WARN,
524 		.len = ETH_ALEN
525 	},
526 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
527 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
528 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
529 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
530 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
531 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
532 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
533 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
534 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
535 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
536 	},
537 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
538 		.type = NLA_EXACT_LEN_WARN,
539 		.len = ETH_ALEN
540 	},
541 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
542 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
543 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
544 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
545 				    .len = FILS_MAX_KEK_LEN },
546 	[NL80211_ATTR_FILS_NONCES] = {
547 		.type = NLA_EXACT_LEN_WARN,
548 		.len = 2 * FILS_NONCE_LEN
549 	},
550 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
551 	[NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
552 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
553 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
554 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
555 	},
556 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
557 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
558 					     .len = FILS_ERP_MAX_USERNAME_LEN },
559 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
560 					  .len = FILS_ERP_MAX_REALM_LEN },
561 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
562 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
563 					.len = FILS_ERP_MAX_RRK_LEN },
564 	[NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
565 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
566 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
567 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
568 
569 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
570 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
571 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
572 	[NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
573 					 .len = NL80211_HE_MAX_CAPABILITY_LEN },
574 
575 	[NL80211_ATTR_FTM_RESPONDER] = {
576 		.type = NLA_NESTED,
577 		.validation_data = nl80211_ftm_responder_policy,
578 	},
579 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
580 	[NL80211_ATTR_PEER_MEASUREMENTS] =
581 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
582 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
583 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
584 					.len = SAE_PASSWORD_MAX_LEN },
585 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
586 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
587 };
588 
589 /* policy for the key attributes */
590 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
591 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
592 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
593 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
594 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
595 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
596 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
597 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
598 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
599 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
600 };
601 
602 /* policy for the key default flags */
603 static const struct nla_policy
604 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
605 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
606 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
607 };
608 
609 #ifdef CONFIG_PM
610 /* policy for WoWLAN attributes */
611 static const struct nla_policy
612 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
613 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
614 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
615 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
616 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
617 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
618 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
619 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
620 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
621 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
622 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
623 };
624 
625 static const struct nla_policy
626 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
627 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
628 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
629 	[NL80211_WOWLAN_TCP_DST_MAC] = {
630 		.type = NLA_EXACT_LEN_WARN,
631 		.len = ETH_ALEN
632 	},
633 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
634 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
635 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
636 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
637 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
638 	},
639 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
640 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
641 	},
642 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
643 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
644 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
645 };
646 #endif /* CONFIG_PM */
647 
648 /* policy for coalesce rule attributes */
649 static const struct nla_policy
650 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
651 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
652 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
653 		NLA_POLICY_RANGE(NLA_U32,
654 				 NL80211_COALESCE_CONDITION_MATCH,
655 				 NL80211_COALESCE_CONDITION_NO_MATCH),
656 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
657 };
658 
659 /* policy for GTK rekey offload attributes */
660 static const struct nla_policy
661 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
662 	[NL80211_REKEY_DATA_KEK] = {
663 		.type = NLA_EXACT_LEN_WARN,
664 		.len = NL80211_KEK_LEN,
665 	},
666 	[NL80211_REKEY_DATA_KCK] = {
667 		.type = NLA_EXACT_LEN_WARN,
668 		.len = NL80211_KCK_LEN,
669 	},
670 	[NL80211_REKEY_DATA_REPLAY_CTR] = {
671 		.type = NLA_EXACT_LEN_WARN,
672 		.len = NL80211_REPLAY_CTR_LEN
673 	},
674 };
675 
676 static const struct nla_policy
677 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
678 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
679 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
680 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
681 };
682 
683 static const struct nla_policy
684 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
685 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
686 						 .len = IEEE80211_MAX_SSID_LEN },
687 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = {
688 		.type = NLA_EXACT_LEN_WARN,
689 		.len = ETH_ALEN
690 	},
691 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
692 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
693 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
694 };
695 
696 static const struct nla_policy
697 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
698 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
699 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
700 };
701 
702 static const struct nla_policy
703 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
704 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
705 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
706 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
707 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
708 	},
709 };
710 
711 /* policy for NAN function attributes */
712 static const struct nla_policy
713 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
714 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
715 	[NL80211_NAN_FUNC_SERVICE_ID] = {
716 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
717 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
718 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
719 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
720 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
721 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
722 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = {
723 		.type = NLA_EXACT_LEN_WARN,
724 		.len = ETH_ALEN
725 	},
726 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
727 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
728 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
729 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
730 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
731 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
732 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
733 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
734 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
735 };
736 
737 /* policy for Service Response Filter attributes */
738 static const struct nla_policy
739 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
740 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
741 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
742 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
743 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
744 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
745 };
746 
747 /* policy for packet pattern attributes */
748 static const struct nla_policy
749 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
750 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
751 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
752 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
753 };
754 
755 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
756 			      struct cfg80211_registered_device **rdev,
757 			      struct wireless_dev **wdev)
758 {
759 	int err;
760 
761 	if (!cb->args[0]) {
762 		struct nlattr **attrbuf;
763 
764 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
765 				  GFP_KERNEL);
766 		if (!attrbuf)
767 			return -ENOMEM;
768 
769 		err = nlmsg_parse_deprecated(cb->nlh,
770 					     GENL_HDRLEN + nl80211_fam.hdrsize,
771 					     attrbuf, nl80211_fam.maxattr,
772 					     nl80211_policy, NULL);
773 		if (err) {
774 			kfree(attrbuf);
775 			return err;
776 		}
777 
778 		*wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
779 						   attrbuf);
780 		kfree(attrbuf);
781 		if (IS_ERR(*wdev))
782 			return PTR_ERR(*wdev);
783 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
784 		/* 0 is the first index - add 1 to parse only once */
785 		cb->args[0] = (*rdev)->wiphy_idx + 1;
786 		cb->args[1] = (*wdev)->identifier;
787 	} else {
788 		/* subtract the 1 again here */
789 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
790 		struct wireless_dev *tmp;
791 
792 		if (!wiphy)
793 			return -ENODEV;
794 		*rdev = wiphy_to_rdev(wiphy);
795 		*wdev = NULL;
796 
797 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
798 			if (tmp->identifier == cb->args[1]) {
799 				*wdev = tmp;
800 				break;
801 			}
802 		}
803 
804 		if (!*wdev)
805 			return -ENODEV;
806 	}
807 
808 	return 0;
809 }
810 
811 /* message building helper */
812 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
813 		     int flags, u8 cmd)
814 {
815 	/* since there is no private header just add the generic one */
816 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
817 }
818 
819 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
820 				     const struct ieee80211_reg_rule *rule)
821 {
822 	int j;
823 	struct nlattr *nl_wmm_rules =
824 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
825 
826 	if (!nl_wmm_rules)
827 		goto nla_put_failure;
828 
829 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
830 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
831 
832 		if (!nl_wmm_rule)
833 			goto nla_put_failure;
834 
835 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
836 				rule->wmm_rule.client[j].cw_min) ||
837 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
838 				rule->wmm_rule.client[j].cw_max) ||
839 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
840 			       rule->wmm_rule.client[j].aifsn) ||
841 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
842 			        rule->wmm_rule.client[j].cot))
843 			goto nla_put_failure;
844 
845 		nla_nest_end(msg, nl_wmm_rule);
846 	}
847 	nla_nest_end(msg, nl_wmm_rules);
848 
849 	return 0;
850 
851 nla_put_failure:
852 	return -ENOBUFS;
853 }
854 
855 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
856 				   struct ieee80211_channel *chan,
857 				   bool large)
858 {
859 	/* Some channels must be completely excluded from the
860 	 * list to protect old user-space tools from breaking
861 	 */
862 	if (!large && chan->flags &
863 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
864 		return 0;
865 
866 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
867 			chan->center_freq))
868 		goto nla_put_failure;
869 
870 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
871 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
872 		goto nla_put_failure;
873 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
874 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
875 			goto nla_put_failure;
876 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
877 			goto nla_put_failure;
878 	}
879 	if (chan->flags & IEEE80211_CHAN_RADAR) {
880 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
881 			goto nla_put_failure;
882 		if (large) {
883 			u32 time;
884 
885 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
886 
887 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
888 					chan->dfs_state))
889 				goto nla_put_failure;
890 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
891 					time))
892 				goto nla_put_failure;
893 			if (nla_put_u32(msg,
894 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
895 					chan->dfs_cac_ms))
896 				goto nla_put_failure;
897 		}
898 	}
899 
900 	if (large) {
901 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
902 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
903 			goto nla_put_failure;
904 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
905 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
906 			goto nla_put_failure;
907 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
908 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
909 			goto nla_put_failure;
910 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
911 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
912 			goto nla_put_failure;
913 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
914 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
915 			goto nla_put_failure;
916 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
917 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
918 			goto nla_put_failure;
919 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
920 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
921 			goto nla_put_failure;
922 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
923 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
924 			goto nla_put_failure;
925 	}
926 
927 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
928 			DBM_TO_MBM(chan->max_power)))
929 		goto nla_put_failure;
930 
931 	if (large) {
932 		const struct ieee80211_reg_rule *rule =
933 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
934 
935 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
936 			if (nl80211_msg_put_wmm_rules(msg, rule))
937 				goto nla_put_failure;
938 		}
939 	}
940 
941 	return 0;
942 
943  nla_put_failure:
944 	return -ENOBUFS;
945 }
946 
947 static bool nl80211_put_txq_stats(struct sk_buff *msg,
948 				  struct cfg80211_txq_stats *txqstats,
949 				  int attrtype)
950 {
951 	struct nlattr *txqattr;
952 
953 #define PUT_TXQVAL_U32(attr, memb) do {					  \
954 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
955 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
956 		return false;						  \
957 	} while (0)
958 
959 	txqattr = nla_nest_start_noflag(msg, attrtype);
960 	if (!txqattr)
961 		return false;
962 
963 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
964 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
965 	PUT_TXQVAL_U32(FLOWS, flows);
966 	PUT_TXQVAL_U32(DROPS, drops);
967 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
968 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
969 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
970 	PUT_TXQVAL_U32(COLLISIONS, collisions);
971 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
972 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
973 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
974 	nla_nest_end(msg, txqattr);
975 
976 #undef PUT_TXQVAL_U32
977 	return true;
978 }
979 
980 /* netlink command implementations */
981 
982 struct key_parse {
983 	struct key_params p;
984 	int idx;
985 	int type;
986 	bool def, defmgmt;
987 	bool def_uni, def_multi;
988 };
989 
990 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
991 				 struct key_parse *k)
992 {
993 	struct nlattr *tb[NL80211_KEY_MAX + 1];
994 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
995 					      nl80211_key_policy,
996 					      info->extack);
997 	if (err)
998 		return err;
999 
1000 	k->def = !!tb[NL80211_KEY_DEFAULT];
1001 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1002 
1003 	if (k->def) {
1004 		k->def_uni = true;
1005 		k->def_multi = true;
1006 	}
1007 	if (k->defmgmt)
1008 		k->def_multi = true;
1009 
1010 	if (tb[NL80211_KEY_IDX])
1011 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1012 
1013 	if (tb[NL80211_KEY_DATA]) {
1014 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1015 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1016 	}
1017 
1018 	if (tb[NL80211_KEY_SEQ]) {
1019 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1020 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1021 	}
1022 
1023 	if (tb[NL80211_KEY_CIPHER])
1024 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1025 
1026 	if (tb[NL80211_KEY_TYPE])
1027 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1028 
1029 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1030 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1031 
1032 		err = nla_parse_nested_deprecated(kdt,
1033 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1034 						  tb[NL80211_KEY_DEFAULT_TYPES],
1035 						  nl80211_key_default_policy,
1036 						  info->extack);
1037 		if (err)
1038 			return err;
1039 
1040 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1041 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1042 	}
1043 
1044 	if (tb[NL80211_KEY_MODE])
1045 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1046 
1047 	return 0;
1048 }
1049 
1050 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1051 {
1052 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1053 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1054 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1055 	}
1056 
1057 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1058 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1059 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1060 	}
1061 
1062 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1063 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1064 
1065 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1066 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1067 
1068 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1069 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1070 
1071 	if (k->def) {
1072 		k->def_uni = true;
1073 		k->def_multi = true;
1074 	}
1075 	if (k->defmgmt)
1076 		k->def_multi = true;
1077 
1078 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1079 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1080 
1081 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1082 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1083 		int err = nla_parse_nested_deprecated(kdt,
1084 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1085 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1086 						      nl80211_key_default_policy,
1087 						      info->extack);
1088 		if (err)
1089 			return err;
1090 
1091 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1092 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1099 {
1100 	int err;
1101 
1102 	memset(k, 0, sizeof(*k));
1103 	k->idx = -1;
1104 	k->type = -1;
1105 
1106 	if (info->attrs[NL80211_ATTR_KEY])
1107 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1108 	else
1109 		err = nl80211_parse_key_old(info, k);
1110 
1111 	if (err)
1112 		return err;
1113 
1114 	if (k->def && k->defmgmt) {
1115 		GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
1116 		return -EINVAL;
1117 	}
1118 
1119 	if (k->defmgmt) {
1120 		if (k->def_uni || !k->def_multi) {
1121 			GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1122 			return -EINVAL;
1123 		}
1124 	}
1125 
1126 	if (k->idx != -1) {
1127 		if (k->defmgmt) {
1128 			if (k->idx < 4 || k->idx > 5) {
1129 				GENL_SET_ERR_MSG(info,
1130 						 "defmgmt key idx not 4 or 5");
1131 				return -EINVAL;
1132 			}
1133 		} else if (k->def) {
1134 			if (k->idx < 0 || k->idx > 3) {
1135 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1136 				return -EINVAL;
1137 			}
1138 		} else {
1139 			if (k->idx < 0 || k->idx > 5) {
1140 				GENL_SET_ERR_MSG(info, "key idx not 0-5");
1141 				return -EINVAL;
1142 			}
1143 		}
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 static struct cfg80211_cached_keys *
1150 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1151 		       struct genl_info *info, bool *no_ht)
1152 {
1153 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1154 	struct key_parse parse;
1155 	struct nlattr *key;
1156 	struct cfg80211_cached_keys *result;
1157 	int rem, err, def = 0;
1158 	bool have_key = false;
1159 
1160 	nla_for_each_nested(key, keys, rem) {
1161 		have_key = true;
1162 		break;
1163 	}
1164 
1165 	if (!have_key)
1166 		return NULL;
1167 
1168 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1169 	if (!result)
1170 		return ERR_PTR(-ENOMEM);
1171 
1172 	result->def = -1;
1173 
1174 	nla_for_each_nested(key, keys, rem) {
1175 		memset(&parse, 0, sizeof(parse));
1176 		parse.idx = -1;
1177 
1178 		err = nl80211_parse_key_new(info, key, &parse);
1179 		if (err)
1180 			goto error;
1181 		err = -EINVAL;
1182 		if (!parse.p.key)
1183 			goto error;
1184 		if (parse.idx < 0 || parse.idx > 3) {
1185 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1186 			goto error;
1187 		}
1188 		if (parse.def) {
1189 			if (def) {
1190 				GENL_SET_ERR_MSG(info,
1191 						 "only one key can be default");
1192 				goto error;
1193 			}
1194 			def = 1;
1195 			result->def = parse.idx;
1196 			if (!parse.def_uni || !parse.def_multi)
1197 				goto error;
1198 		} else if (parse.defmgmt)
1199 			goto error;
1200 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1201 						     parse.idx, false, NULL);
1202 		if (err)
1203 			goto error;
1204 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1205 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1206 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1207 			err = -EINVAL;
1208 			goto error;
1209 		}
1210 		result->params[parse.idx].cipher = parse.p.cipher;
1211 		result->params[parse.idx].key_len = parse.p.key_len;
1212 		result->params[parse.idx].key = result->data[parse.idx];
1213 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1214 
1215 		/* must be WEP key if we got here */
1216 		if (no_ht)
1217 			*no_ht = true;
1218 	}
1219 
1220 	if (result->def < 0) {
1221 		err = -EINVAL;
1222 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1223 		goto error;
1224 	}
1225 
1226 	return result;
1227  error:
1228 	kfree(result);
1229 	return ERR_PTR(err);
1230 }
1231 
1232 static int nl80211_key_allowed(struct wireless_dev *wdev)
1233 {
1234 	ASSERT_WDEV_LOCK(wdev);
1235 
1236 	switch (wdev->iftype) {
1237 	case NL80211_IFTYPE_AP:
1238 	case NL80211_IFTYPE_AP_VLAN:
1239 	case NL80211_IFTYPE_P2P_GO:
1240 	case NL80211_IFTYPE_MESH_POINT:
1241 		break;
1242 	case NL80211_IFTYPE_ADHOC:
1243 	case NL80211_IFTYPE_STATION:
1244 	case NL80211_IFTYPE_P2P_CLIENT:
1245 		if (!wdev->current_bss)
1246 			return -ENOLINK;
1247 		break;
1248 	case NL80211_IFTYPE_UNSPECIFIED:
1249 	case NL80211_IFTYPE_OCB:
1250 	case NL80211_IFTYPE_MONITOR:
1251 	case NL80211_IFTYPE_NAN:
1252 	case NL80211_IFTYPE_P2P_DEVICE:
1253 	case NL80211_IFTYPE_WDS:
1254 	case NUM_NL80211_IFTYPES:
1255 		return -EINVAL;
1256 	}
1257 
1258 	return 0;
1259 }
1260 
1261 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1262 							struct nlattr *tb)
1263 {
1264 	struct ieee80211_channel *chan;
1265 
1266 	if (tb == NULL)
1267 		return NULL;
1268 	chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1269 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1270 		return NULL;
1271 	return chan;
1272 }
1273 
1274 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1275 {
1276 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1277 	int i;
1278 
1279 	if (!nl_modes)
1280 		goto nla_put_failure;
1281 
1282 	i = 0;
1283 	while (ifmodes) {
1284 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1285 			goto nla_put_failure;
1286 		ifmodes >>= 1;
1287 		i++;
1288 	}
1289 
1290 	nla_nest_end(msg, nl_modes);
1291 	return 0;
1292 
1293 nla_put_failure:
1294 	return -ENOBUFS;
1295 }
1296 
1297 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1298 					  struct sk_buff *msg,
1299 					  bool large)
1300 {
1301 	struct nlattr *nl_combis;
1302 	int i, j;
1303 
1304 	nl_combis = nla_nest_start_noflag(msg,
1305 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1306 	if (!nl_combis)
1307 		goto nla_put_failure;
1308 
1309 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1310 		const struct ieee80211_iface_combination *c;
1311 		struct nlattr *nl_combi, *nl_limits;
1312 
1313 		c = &wiphy->iface_combinations[i];
1314 
1315 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1316 		if (!nl_combi)
1317 			goto nla_put_failure;
1318 
1319 		nl_limits = nla_nest_start_noflag(msg,
1320 						  NL80211_IFACE_COMB_LIMITS);
1321 		if (!nl_limits)
1322 			goto nla_put_failure;
1323 
1324 		for (j = 0; j < c->n_limits; j++) {
1325 			struct nlattr *nl_limit;
1326 
1327 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1328 			if (!nl_limit)
1329 				goto nla_put_failure;
1330 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1331 					c->limits[j].max))
1332 				goto nla_put_failure;
1333 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1334 						c->limits[j].types))
1335 				goto nla_put_failure;
1336 			nla_nest_end(msg, nl_limit);
1337 		}
1338 
1339 		nla_nest_end(msg, nl_limits);
1340 
1341 		if (c->beacon_int_infra_match &&
1342 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1343 			goto nla_put_failure;
1344 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1345 				c->num_different_channels) ||
1346 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1347 				c->max_interfaces))
1348 			goto nla_put_failure;
1349 		if (large &&
1350 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1351 				c->radar_detect_widths) ||
1352 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1353 				c->radar_detect_regions)))
1354 			goto nla_put_failure;
1355 		if (c->beacon_int_min_gcd &&
1356 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1357 				c->beacon_int_min_gcd))
1358 			goto nla_put_failure;
1359 
1360 		nla_nest_end(msg, nl_combi);
1361 	}
1362 
1363 	nla_nest_end(msg, nl_combis);
1364 
1365 	return 0;
1366 nla_put_failure:
1367 	return -ENOBUFS;
1368 }
1369 
1370 #ifdef CONFIG_PM
1371 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1372 					struct sk_buff *msg)
1373 {
1374 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1375 	struct nlattr *nl_tcp;
1376 
1377 	if (!tcp)
1378 		return 0;
1379 
1380 	nl_tcp = nla_nest_start_noflag(msg,
1381 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1382 	if (!nl_tcp)
1383 		return -ENOBUFS;
1384 
1385 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1386 			tcp->data_payload_max))
1387 		return -ENOBUFS;
1388 
1389 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1390 			tcp->data_payload_max))
1391 		return -ENOBUFS;
1392 
1393 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1394 		return -ENOBUFS;
1395 
1396 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1397 				sizeof(*tcp->tok), tcp->tok))
1398 		return -ENOBUFS;
1399 
1400 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1401 			tcp->data_interval_max))
1402 		return -ENOBUFS;
1403 
1404 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1405 			tcp->wake_payload_max))
1406 		return -ENOBUFS;
1407 
1408 	nla_nest_end(msg, nl_tcp);
1409 	return 0;
1410 }
1411 
1412 static int nl80211_send_wowlan(struct sk_buff *msg,
1413 			       struct cfg80211_registered_device *rdev,
1414 			       bool large)
1415 {
1416 	struct nlattr *nl_wowlan;
1417 
1418 	if (!rdev->wiphy.wowlan)
1419 		return 0;
1420 
1421 	nl_wowlan = nla_nest_start_noflag(msg,
1422 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1423 	if (!nl_wowlan)
1424 		return -ENOBUFS;
1425 
1426 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1427 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1428 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1429 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1430 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1431 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1432 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1433 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1434 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1435 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1436 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1437 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1438 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1439 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1440 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1441 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1442 		return -ENOBUFS;
1443 
1444 	if (rdev->wiphy.wowlan->n_patterns) {
1445 		struct nl80211_pattern_support pat = {
1446 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1447 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1448 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1449 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1450 		};
1451 
1452 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1453 			    sizeof(pat), &pat))
1454 			return -ENOBUFS;
1455 	}
1456 
1457 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1458 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1459 			rdev->wiphy.wowlan->max_nd_match_sets))
1460 		return -ENOBUFS;
1461 
1462 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1463 		return -ENOBUFS;
1464 
1465 	nla_nest_end(msg, nl_wowlan);
1466 
1467 	return 0;
1468 }
1469 #endif
1470 
1471 static int nl80211_send_coalesce(struct sk_buff *msg,
1472 				 struct cfg80211_registered_device *rdev)
1473 {
1474 	struct nl80211_coalesce_rule_support rule;
1475 
1476 	if (!rdev->wiphy.coalesce)
1477 		return 0;
1478 
1479 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1480 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1481 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1482 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1483 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1484 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1485 
1486 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1487 		return -ENOBUFS;
1488 
1489 	return 0;
1490 }
1491 
1492 static int
1493 nl80211_send_iftype_data(struct sk_buff *msg,
1494 			 const struct ieee80211_sband_iftype_data *iftdata)
1495 {
1496 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1497 
1498 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1499 				iftdata->types_mask))
1500 		return -ENOBUFS;
1501 
1502 	if (he_cap->has_he) {
1503 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1504 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1505 			    he_cap->he_cap_elem.mac_cap_info) ||
1506 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1507 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1508 			    he_cap->he_cap_elem.phy_cap_info) ||
1509 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1510 			    sizeof(he_cap->he_mcs_nss_supp),
1511 			    &he_cap->he_mcs_nss_supp) ||
1512 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1513 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1514 			return -ENOBUFS;
1515 	}
1516 
1517 	return 0;
1518 }
1519 
1520 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1521 				      struct ieee80211_supported_band *sband)
1522 {
1523 	struct nlattr *nl_rates, *nl_rate;
1524 	struct ieee80211_rate *rate;
1525 	int i;
1526 
1527 	/* add HT info */
1528 	if (sband->ht_cap.ht_supported &&
1529 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1530 		     sizeof(sband->ht_cap.mcs),
1531 		     &sband->ht_cap.mcs) ||
1532 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1533 			 sband->ht_cap.cap) ||
1534 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1535 			sband->ht_cap.ampdu_factor) ||
1536 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1537 			sband->ht_cap.ampdu_density)))
1538 		return -ENOBUFS;
1539 
1540 	/* add VHT info */
1541 	if (sband->vht_cap.vht_supported &&
1542 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1543 		     sizeof(sband->vht_cap.vht_mcs),
1544 		     &sband->vht_cap.vht_mcs) ||
1545 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1546 			 sband->vht_cap.cap)))
1547 		return -ENOBUFS;
1548 
1549 	if (sband->n_iftype_data) {
1550 		struct nlattr *nl_iftype_data =
1551 			nla_nest_start_noflag(msg,
1552 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1553 		int err;
1554 
1555 		if (!nl_iftype_data)
1556 			return -ENOBUFS;
1557 
1558 		for (i = 0; i < sband->n_iftype_data; i++) {
1559 			struct nlattr *iftdata;
1560 
1561 			iftdata = nla_nest_start_noflag(msg, i + 1);
1562 			if (!iftdata)
1563 				return -ENOBUFS;
1564 
1565 			err = nl80211_send_iftype_data(msg,
1566 						       &sband->iftype_data[i]);
1567 			if (err)
1568 				return err;
1569 
1570 			nla_nest_end(msg, iftdata);
1571 		}
1572 
1573 		nla_nest_end(msg, nl_iftype_data);
1574 	}
1575 
1576 	/* add bitrates */
1577 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1578 	if (!nl_rates)
1579 		return -ENOBUFS;
1580 
1581 	for (i = 0; i < sband->n_bitrates; i++) {
1582 		nl_rate = nla_nest_start_noflag(msg, i);
1583 		if (!nl_rate)
1584 			return -ENOBUFS;
1585 
1586 		rate = &sband->bitrates[i];
1587 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1588 				rate->bitrate))
1589 			return -ENOBUFS;
1590 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1591 		    nla_put_flag(msg,
1592 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1593 			return -ENOBUFS;
1594 
1595 		nla_nest_end(msg, nl_rate);
1596 	}
1597 
1598 	nla_nest_end(msg, nl_rates);
1599 
1600 	return 0;
1601 }
1602 
1603 static int
1604 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1605 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1606 {
1607 	u16 stypes;
1608 	struct nlattr *nl_ftypes, *nl_ifs;
1609 	enum nl80211_iftype ift;
1610 	int i;
1611 
1612 	if (!mgmt_stypes)
1613 		return 0;
1614 
1615 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1616 	if (!nl_ifs)
1617 		return -ENOBUFS;
1618 
1619 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1620 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1621 		if (!nl_ftypes)
1622 			return -ENOBUFS;
1623 		i = 0;
1624 		stypes = mgmt_stypes[ift].tx;
1625 		while (stypes) {
1626 			if ((stypes & 1) &&
1627 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1628 					(i << 4) | IEEE80211_FTYPE_MGMT))
1629 				return -ENOBUFS;
1630 			stypes >>= 1;
1631 			i++;
1632 		}
1633 		nla_nest_end(msg, nl_ftypes);
1634 	}
1635 
1636 	nla_nest_end(msg, nl_ifs);
1637 
1638 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1639 	if (!nl_ifs)
1640 		return -ENOBUFS;
1641 
1642 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1643 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1644 		if (!nl_ftypes)
1645 			return -ENOBUFS;
1646 		i = 0;
1647 		stypes = mgmt_stypes[ift].rx;
1648 		while (stypes) {
1649 			if ((stypes & 1) &&
1650 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1651 					(i << 4) | IEEE80211_FTYPE_MGMT))
1652 				return -ENOBUFS;
1653 			stypes >>= 1;
1654 			i++;
1655 		}
1656 		nla_nest_end(msg, nl_ftypes);
1657 	}
1658 	nla_nest_end(msg, nl_ifs);
1659 
1660 	return 0;
1661 }
1662 
1663 #define CMD(op, n)							\
1664 	 do {								\
1665 		if (rdev->ops->op) {					\
1666 			i++;						\
1667 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1668 				goto nla_put_failure;			\
1669 		}							\
1670 	} while (0)
1671 
1672 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1673 					struct sk_buff *msg)
1674 {
1675 	int i = 0;
1676 
1677 	/*
1678 	 * do *NOT* add anything into this function, new things need to be
1679 	 * advertised only to new versions of userspace that can deal with
1680 	 * the split (and they can't possibly care about new features...
1681 	 */
1682 	CMD(add_virtual_intf, NEW_INTERFACE);
1683 	CMD(change_virtual_intf, SET_INTERFACE);
1684 	CMD(add_key, NEW_KEY);
1685 	CMD(start_ap, START_AP);
1686 	CMD(add_station, NEW_STATION);
1687 	CMD(add_mpath, NEW_MPATH);
1688 	CMD(update_mesh_config, SET_MESH_CONFIG);
1689 	CMD(change_bss, SET_BSS);
1690 	CMD(auth, AUTHENTICATE);
1691 	CMD(assoc, ASSOCIATE);
1692 	CMD(deauth, DEAUTHENTICATE);
1693 	CMD(disassoc, DISASSOCIATE);
1694 	CMD(join_ibss, JOIN_IBSS);
1695 	CMD(join_mesh, JOIN_MESH);
1696 	CMD(set_pmksa, SET_PMKSA);
1697 	CMD(del_pmksa, DEL_PMKSA);
1698 	CMD(flush_pmksa, FLUSH_PMKSA);
1699 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1700 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1701 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1702 	CMD(mgmt_tx, FRAME);
1703 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1704 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1705 		i++;
1706 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1707 			goto nla_put_failure;
1708 	}
1709 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1710 	    rdev->ops->join_mesh) {
1711 		i++;
1712 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1713 			goto nla_put_failure;
1714 	}
1715 	CMD(set_wds_peer, SET_WDS_PEER);
1716 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1717 		CMD(tdls_mgmt, TDLS_MGMT);
1718 		CMD(tdls_oper, TDLS_OPER);
1719 	}
1720 	if (rdev->wiphy.max_sched_scan_reqs)
1721 		CMD(sched_scan_start, START_SCHED_SCAN);
1722 	CMD(probe_client, PROBE_CLIENT);
1723 	CMD(set_noack_map, SET_NOACK_MAP);
1724 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1725 		i++;
1726 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1727 			goto nla_put_failure;
1728 	}
1729 	CMD(start_p2p_device, START_P2P_DEVICE);
1730 	CMD(set_mcast_rate, SET_MCAST_RATE);
1731 #ifdef CONFIG_NL80211_TESTMODE
1732 	CMD(testmode_cmd, TESTMODE);
1733 #endif
1734 
1735 	if (rdev->ops->connect || rdev->ops->auth) {
1736 		i++;
1737 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1738 			goto nla_put_failure;
1739 	}
1740 
1741 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1742 		i++;
1743 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1744 			goto nla_put_failure;
1745 	}
1746 
1747 	return i;
1748  nla_put_failure:
1749 	return -ENOBUFS;
1750 }
1751 
1752 static int
1753 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1754 			   struct sk_buff *msg)
1755 {
1756 	struct nlattr *ftm;
1757 
1758 	if (!cap->ftm.supported)
1759 		return 0;
1760 
1761 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1762 	if (!ftm)
1763 		return -ENOBUFS;
1764 
1765 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1766 		return -ENOBUFS;
1767 	if (cap->ftm.non_asap &&
1768 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1769 		return -ENOBUFS;
1770 	if (cap->ftm.request_lci &&
1771 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1772 		return -ENOBUFS;
1773 	if (cap->ftm.request_civicloc &&
1774 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1775 		return -ENOBUFS;
1776 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1777 			cap->ftm.preambles))
1778 		return -ENOBUFS;
1779 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1780 			cap->ftm.bandwidths))
1781 		return -ENOBUFS;
1782 	if (cap->ftm.max_bursts_exponent >= 0 &&
1783 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1784 			cap->ftm.max_bursts_exponent))
1785 		return -ENOBUFS;
1786 	if (cap->ftm.max_ftms_per_burst &&
1787 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1788 			cap->ftm.max_ftms_per_burst))
1789 		return -ENOBUFS;
1790 
1791 	nla_nest_end(msg, ftm);
1792 	return 0;
1793 }
1794 
1795 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1796 				  struct sk_buff *msg)
1797 {
1798 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1799 	struct nlattr *pmsr, *caps;
1800 
1801 	if (!cap)
1802 		return 0;
1803 
1804 	/*
1805 	 * we don't need to clean up anything here since the caller
1806 	 * will genlmsg_cancel() if we fail
1807 	 */
1808 
1809 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1810 	if (!pmsr)
1811 		return -ENOBUFS;
1812 
1813 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1814 		return -ENOBUFS;
1815 
1816 	if (cap->report_ap_tsf &&
1817 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1818 		return -ENOBUFS;
1819 
1820 	if (cap->randomize_mac_addr &&
1821 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1822 		return -ENOBUFS;
1823 
1824 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1825 	if (!caps)
1826 		return -ENOBUFS;
1827 
1828 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
1829 		return -ENOBUFS;
1830 
1831 	nla_nest_end(msg, caps);
1832 	nla_nest_end(msg, pmsr);
1833 
1834 	return 0;
1835 }
1836 
1837 struct nl80211_dump_wiphy_state {
1838 	s64 filter_wiphy;
1839 	long start;
1840 	long split_start, band_start, chan_start, capa_start;
1841 	bool split;
1842 };
1843 
1844 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1845 			      enum nl80211_commands cmd,
1846 			      struct sk_buff *msg, u32 portid, u32 seq,
1847 			      int flags, struct nl80211_dump_wiphy_state *state)
1848 {
1849 	void *hdr;
1850 	struct nlattr *nl_bands, *nl_band;
1851 	struct nlattr *nl_freqs, *nl_freq;
1852 	struct nlattr *nl_cmds;
1853 	enum nl80211_band band;
1854 	struct ieee80211_channel *chan;
1855 	int i;
1856 	const struct ieee80211_txrx_stypes *mgmt_stypes =
1857 				rdev->wiphy.mgmt_stypes;
1858 	u32 features;
1859 
1860 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1861 	if (!hdr)
1862 		return -ENOBUFS;
1863 
1864 	if (WARN_ON(!state))
1865 		return -EINVAL;
1866 
1867 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1868 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1869 			   wiphy_name(&rdev->wiphy)) ||
1870 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
1871 			cfg80211_rdev_list_generation))
1872 		goto nla_put_failure;
1873 
1874 	if (cmd != NL80211_CMD_NEW_WIPHY)
1875 		goto finish;
1876 
1877 	switch (state->split_start) {
1878 	case 0:
1879 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1880 			       rdev->wiphy.retry_short) ||
1881 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1882 			       rdev->wiphy.retry_long) ||
1883 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1884 				rdev->wiphy.frag_threshold) ||
1885 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1886 				rdev->wiphy.rts_threshold) ||
1887 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1888 			       rdev->wiphy.coverage_class) ||
1889 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1890 			       rdev->wiphy.max_scan_ssids) ||
1891 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1892 			       rdev->wiphy.max_sched_scan_ssids) ||
1893 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1894 				rdev->wiphy.max_scan_ie_len) ||
1895 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1896 				rdev->wiphy.max_sched_scan_ie_len) ||
1897 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1898 			       rdev->wiphy.max_match_sets) ||
1899 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1900 				rdev->wiphy.max_sched_scan_plans) ||
1901 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1902 				rdev->wiphy.max_sched_scan_plan_interval) ||
1903 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1904 				rdev->wiphy.max_sched_scan_plan_iterations))
1905 			goto nla_put_failure;
1906 
1907 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1908 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1909 			goto nla_put_failure;
1910 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1911 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1912 			goto nla_put_failure;
1913 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1914 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1915 			goto nla_put_failure;
1916 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1917 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1918 			goto nla_put_failure;
1919 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1920 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1921 			goto nla_put_failure;
1922 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1923 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1924 			goto nla_put_failure;
1925 		state->split_start++;
1926 		if (state->split)
1927 			break;
1928 		/* fall through */
1929 	case 1:
1930 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1931 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
1932 			    rdev->wiphy.cipher_suites))
1933 			goto nla_put_failure;
1934 
1935 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1936 			       rdev->wiphy.max_num_pmkids))
1937 			goto nla_put_failure;
1938 
1939 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1940 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1941 			goto nla_put_failure;
1942 
1943 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1944 				rdev->wiphy.available_antennas_tx) ||
1945 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1946 				rdev->wiphy.available_antennas_rx))
1947 			goto nla_put_failure;
1948 
1949 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1950 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1951 				rdev->wiphy.probe_resp_offload))
1952 			goto nla_put_failure;
1953 
1954 		if ((rdev->wiphy.available_antennas_tx ||
1955 		     rdev->wiphy.available_antennas_rx) &&
1956 		    rdev->ops->get_antenna) {
1957 			u32 tx_ant = 0, rx_ant = 0;
1958 			int res;
1959 
1960 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1961 			if (!res) {
1962 				if (nla_put_u32(msg,
1963 						NL80211_ATTR_WIPHY_ANTENNA_TX,
1964 						tx_ant) ||
1965 				    nla_put_u32(msg,
1966 						NL80211_ATTR_WIPHY_ANTENNA_RX,
1967 						rx_ant))
1968 					goto nla_put_failure;
1969 			}
1970 		}
1971 
1972 		state->split_start++;
1973 		if (state->split)
1974 			break;
1975 		/* fall through */
1976 	case 2:
1977 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1978 					rdev->wiphy.interface_modes))
1979 				goto nla_put_failure;
1980 		state->split_start++;
1981 		if (state->split)
1982 			break;
1983 		/* fall through */
1984 	case 3:
1985 		nl_bands = nla_nest_start_noflag(msg,
1986 						 NL80211_ATTR_WIPHY_BANDS);
1987 		if (!nl_bands)
1988 			goto nla_put_failure;
1989 
1990 		for (band = state->band_start;
1991 		     band < NUM_NL80211_BANDS; band++) {
1992 			struct ieee80211_supported_band *sband;
1993 
1994 			sband = rdev->wiphy.bands[band];
1995 
1996 			if (!sband)
1997 				continue;
1998 
1999 			nl_band = nla_nest_start_noflag(msg, band);
2000 			if (!nl_band)
2001 				goto nla_put_failure;
2002 
2003 			switch (state->chan_start) {
2004 			case 0:
2005 				if (nl80211_send_band_rateinfo(msg, sband))
2006 					goto nla_put_failure;
2007 				state->chan_start++;
2008 				if (state->split)
2009 					break;
2010 				/* fall through */
2011 			default:
2012 				/* add frequencies */
2013 				nl_freqs = nla_nest_start_noflag(msg,
2014 								 NL80211_BAND_ATTR_FREQS);
2015 				if (!nl_freqs)
2016 					goto nla_put_failure;
2017 
2018 				for (i = state->chan_start - 1;
2019 				     i < sband->n_channels;
2020 				     i++) {
2021 					nl_freq = nla_nest_start_noflag(msg,
2022 									i);
2023 					if (!nl_freq)
2024 						goto nla_put_failure;
2025 
2026 					chan = &sband->channels[i];
2027 
2028 					if (nl80211_msg_put_channel(
2029 							msg, &rdev->wiphy, chan,
2030 							state->split))
2031 						goto nla_put_failure;
2032 
2033 					nla_nest_end(msg, nl_freq);
2034 					if (state->split)
2035 						break;
2036 				}
2037 				if (i < sband->n_channels)
2038 					state->chan_start = i + 2;
2039 				else
2040 					state->chan_start = 0;
2041 				nla_nest_end(msg, nl_freqs);
2042 			}
2043 
2044 			nla_nest_end(msg, nl_band);
2045 
2046 			if (state->split) {
2047 				/* start again here */
2048 				if (state->chan_start)
2049 					band--;
2050 				break;
2051 			}
2052 		}
2053 		nla_nest_end(msg, nl_bands);
2054 
2055 		if (band < NUM_NL80211_BANDS)
2056 			state->band_start = band + 1;
2057 		else
2058 			state->band_start = 0;
2059 
2060 		/* if bands & channels are done, continue outside */
2061 		if (state->band_start == 0 && state->chan_start == 0)
2062 			state->split_start++;
2063 		if (state->split)
2064 			break;
2065 		/* fall through */
2066 	case 4:
2067 		nl_cmds = nla_nest_start_noflag(msg,
2068 						NL80211_ATTR_SUPPORTED_COMMANDS);
2069 		if (!nl_cmds)
2070 			goto nla_put_failure;
2071 
2072 		i = nl80211_add_commands_unsplit(rdev, msg);
2073 		if (i < 0)
2074 			goto nla_put_failure;
2075 		if (state->split) {
2076 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2077 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2078 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2079 				CMD(channel_switch, CHANNEL_SWITCH);
2080 			CMD(set_qos_map, SET_QOS_MAP);
2081 			if (rdev->wiphy.features &
2082 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2083 				CMD(add_tx_ts, ADD_TX_TS);
2084 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2085 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2086 		}
2087 #undef CMD
2088 
2089 		nla_nest_end(msg, nl_cmds);
2090 		state->split_start++;
2091 		if (state->split)
2092 			break;
2093 		/* fall through */
2094 	case 5:
2095 		if (rdev->ops->remain_on_channel &&
2096 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2097 		    nla_put_u32(msg,
2098 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2099 				rdev->wiphy.max_remain_on_channel_duration))
2100 			goto nla_put_failure;
2101 
2102 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2103 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2104 			goto nla_put_failure;
2105 
2106 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2107 			goto nla_put_failure;
2108 		state->split_start++;
2109 		if (state->split)
2110 			break;
2111 		/* fall through */
2112 	case 6:
2113 #ifdef CONFIG_PM
2114 		if (nl80211_send_wowlan(msg, rdev, state->split))
2115 			goto nla_put_failure;
2116 		state->split_start++;
2117 		if (state->split)
2118 			break;
2119 #else
2120 		state->split_start++;
2121 #endif
2122 		/* fall through */
2123 	case 7:
2124 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2125 					rdev->wiphy.software_iftypes))
2126 			goto nla_put_failure;
2127 
2128 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2129 						   state->split))
2130 			goto nla_put_failure;
2131 
2132 		state->split_start++;
2133 		if (state->split)
2134 			break;
2135 		/* fall through */
2136 	case 8:
2137 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2138 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2139 				rdev->wiphy.ap_sme_capa))
2140 			goto nla_put_failure;
2141 
2142 		features = rdev->wiphy.features;
2143 		/*
2144 		 * We can only add the per-channel limit information if the
2145 		 * dump is split, otherwise it makes it too big. Therefore
2146 		 * only advertise it in that case.
2147 		 */
2148 		if (state->split)
2149 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2150 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2151 			goto nla_put_failure;
2152 
2153 		if (rdev->wiphy.ht_capa_mod_mask &&
2154 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2155 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2156 			    rdev->wiphy.ht_capa_mod_mask))
2157 			goto nla_put_failure;
2158 
2159 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2160 		    rdev->wiphy.max_acl_mac_addrs &&
2161 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2162 				rdev->wiphy.max_acl_mac_addrs))
2163 			goto nla_put_failure;
2164 
2165 		/*
2166 		 * Any information below this point is only available to
2167 		 * applications that can deal with it being split. This
2168 		 * helps ensure that newly added capabilities don't break
2169 		 * older tools by overrunning their buffers.
2170 		 *
2171 		 * We still increment split_start so that in the split
2172 		 * case we'll continue with more data in the next round,
2173 		 * but break unconditionally so unsplit data stops here.
2174 		 */
2175 		state->split_start++;
2176 		break;
2177 	case 9:
2178 		if (rdev->wiphy.extended_capabilities &&
2179 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2180 			     rdev->wiphy.extended_capabilities_len,
2181 			     rdev->wiphy.extended_capabilities) ||
2182 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2183 			     rdev->wiphy.extended_capabilities_len,
2184 			     rdev->wiphy.extended_capabilities_mask)))
2185 			goto nla_put_failure;
2186 
2187 		if (rdev->wiphy.vht_capa_mod_mask &&
2188 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2189 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2190 			    rdev->wiphy.vht_capa_mod_mask))
2191 			goto nla_put_failure;
2192 
2193 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2194 			    rdev->wiphy.perm_addr))
2195 			goto nla_put_failure;
2196 
2197 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2198 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2199 			    rdev->wiphy.addr_mask))
2200 			goto nla_put_failure;
2201 
2202 		if (rdev->wiphy.n_addresses > 1) {
2203 			void *attr;
2204 
2205 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2206 			if (!attr)
2207 				goto nla_put_failure;
2208 
2209 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2210 				if (nla_put(msg, i + 1, ETH_ALEN,
2211 					    rdev->wiphy.addresses[i].addr))
2212 					goto nla_put_failure;
2213 
2214 			nla_nest_end(msg, attr);
2215 		}
2216 
2217 		state->split_start++;
2218 		break;
2219 	case 10:
2220 		if (nl80211_send_coalesce(msg, rdev))
2221 			goto nla_put_failure;
2222 
2223 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2224 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2225 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2226 			goto nla_put_failure;
2227 
2228 		if (rdev->wiphy.max_ap_assoc_sta &&
2229 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2230 				rdev->wiphy.max_ap_assoc_sta))
2231 			goto nla_put_failure;
2232 
2233 		state->split_start++;
2234 		break;
2235 	case 11:
2236 		if (rdev->wiphy.n_vendor_commands) {
2237 			const struct nl80211_vendor_cmd_info *info;
2238 			struct nlattr *nested;
2239 
2240 			nested = nla_nest_start_noflag(msg,
2241 						       NL80211_ATTR_VENDOR_DATA);
2242 			if (!nested)
2243 				goto nla_put_failure;
2244 
2245 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2246 				info = &rdev->wiphy.vendor_commands[i].info;
2247 				if (nla_put(msg, i + 1, sizeof(*info), info))
2248 					goto nla_put_failure;
2249 			}
2250 			nla_nest_end(msg, nested);
2251 		}
2252 
2253 		if (rdev->wiphy.n_vendor_events) {
2254 			const struct nl80211_vendor_cmd_info *info;
2255 			struct nlattr *nested;
2256 
2257 			nested = nla_nest_start_noflag(msg,
2258 						       NL80211_ATTR_VENDOR_EVENTS);
2259 			if (!nested)
2260 				goto nla_put_failure;
2261 
2262 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2263 				info = &rdev->wiphy.vendor_events[i];
2264 				if (nla_put(msg, i + 1, sizeof(*info), info))
2265 					goto nla_put_failure;
2266 			}
2267 			nla_nest_end(msg, nested);
2268 		}
2269 		state->split_start++;
2270 		break;
2271 	case 12:
2272 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2273 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2274 			       rdev->wiphy.max_num_csa_counters))
2275 			goto nla_put_failure;
2276 
2277 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2278 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2279 			goto nla_put_failure;
2280 
2281 		if (rdev->wiphy.max_sched_scan_reqs &&
2282 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2283 				rdev->wiphy.max_sched_scan_reqs))
2284 			goto nla_put_failure;
2285 
2286 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2287 			    sizeof(rdev->wiphy.ext_features),
2288 			    rdev->wiphy.ext_features))
2289 			goto nla_put_failure;
2290 
2291 		if (rdev->wiphy.bss_select_support) {
2292 			struct nlattr *nested;
2293 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2294 
2295 			nested = nla_nest_start_noflag(msg,
2296 						       NL80211_ATTR_BSS_SELECT);
2297 			if (!nested)
2298 				goto nla_put_failure;
2299 
2300 			i = 0;
2301 			while (bss_select_support) {
2302 				if ((bss_select_support & 1) &&
2303 				    nla_put_flag(msg, i))
2304 					goto nla_put_failure;
2305 				i++;
2306 				bss_select_support >>= 1;
2307 			}
2308 			nla_nest_end(msg, nested);
2309 		}
2310 
2311 		state->split_start++;
2312 		break;
2313 	case 13:
2314 		if (rdev->wiphy.num_iftype_ext_capab &&
2315 		    rdev->wiphy.iftype_ext_capab) {
2316 			struct nlattr *nested_ext_capab, *nested;
2317 
2318 			nested = nla_nest_start_noflag(msg,
2319 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2320 			if (!nested)
2321 				goto nla_put_failure;
2322 
2323 			for (i = state->capa_start;
2324 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2325 				const struct wiphy_iftype_ext_capab *capab;
2326 
2327 				capab = &rdev->wiphy.iftype_ext_capab[i];
2328 
2329 				nested_ext_capab = nla_nest_start_noflag(msg,
2330 									 i);
2331 				if (!nested_ext_capab ||
2332 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2333 						capab->iftype) ||
2334 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2335 					    capab->extended_capabilities_len,
2336 					    capab->extended_capabilities) ||
2337 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2338 					    capab->extended_capabilities_len,
2339 					    capab->extended_capabilities_mask))
2340 					goto nla_put_failure;
2341 
2342 				nla_nest_end(msg, nested_ext_capab);
2343 				if (state->split)
2344 					break;
2345 			}
2346 			nla_nest_end(msg, nested);
2347 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2348 				state->capa_start = i + 1;
2349 				break;
2350 			}
2351 		}
2352 
2353 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2354 				rdev->wiphy.nan_supported_bands))
2355 			goto nla_put_failure;
2356 
2357 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2358 					    NL80211_EXT_FEATURE_TXQS)) {
2359 			struct cfg80211_txq_stats txqstats = {};
2360 			int res;
2361 
2362 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2363 			if (!res &&
2364 			    !nl80211_put_txq_stats(msg, &txqstats,
2365 						   NL80211_ATTR_TXQ_STATS))
2366 				goto nla_put_failure;
2367 
2368 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2369 					rdev->wiphy.txq_limit))
2370 				goto nla_put_failure;
2371 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2372 					rdev->wiphy.txq_memory_limit))
2373 				goto nla_put_failure;
2374 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2375 					rdev->wiphy.txq_quantum))
2376 				goto nla_put_failure;
2377 		}
2378 
2379 		state->split_start++;
2380 		break;
2381 	case 14:
2382 		if (nl80211_send_pmsr_capa(rdev, msg))
2383 			goto nla_put_failure;
2384 
2385 		state->split_start++;
2386 		break;
2387 	case 15:
2388 		if (rdev->wiphy.akm_suites &&
2389 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2390 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2391 			    rdev->wiphy.akm_suites))
2392 			goto nla_put_failure;
2393 
2394 		/* done */
2395 		state->split_start = 0;
2396 		break;
2397 	}
2398  finish:
2399 	genlmsg_end(msg, hdr);
2400 	return 0;
2401 
2402  nla_put_failure:
2403 	genlmsg_cancel(msg, hdr);
2404 	return -EMSGSIZE;
2405 }
2406 
2407 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2408 				    struct netlink_callback *cb,
2409 				    struct nl80211_dump_wiphy_state *state)
2410 {
2411 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2412 	int ret;
2413 
2414 	if (!tb)
2415 		return -ENOMEM;
2416 
2417 	ret = nlmsg_parse_deprecated(cb->nlh,
2418 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2419 				     tb, nl80211_fam.maxattr,
2420 				     nl80211_policy, NULL);
2421 	/* ignore parse errors for backward compatibility */
2422 	if (ret) {
2423 		ret = 0;
2424 		goto out;
2425 	}
2426 
2427 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2428 	if (tb[NL80211_ATTR_WIPHY])
2429 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2430 	if (tb[NL80211_ATTR_WDEV])
2431 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2432 	if (tb[NL80211_ATTR_IFINDEX]) {
2433 		struct net_device *netdev;
2434 		struct cfg80211_registered_device *rdev;
2435 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2436 
2437 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2438 		if (!netdev) {
2439 			ret = -ENODEV;
2440 			goto out;
2441 		}
2442 		if (netdev->ieee80211_ptr) {
2443 			rdev = wiphy_to_rdev(
2444 				netdev->ieee80211_ptr->wiphy);
2445 			state->filter_wiphy = rdev->wiphy_idx;
2446 		}
2447 	}
2448 
2449 	ret = 0;
2450 out:
2451 	kfree(tb);
2452 	return ret;
2453 }
2454 
2455 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2456 {
2457 	int idx = 0, ret;
2458 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2459 	struct cfg80211_registered_device *rdev;
2460 
2461 	rtnl_lock();
2462 	if (!state) {
2463 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2464 		if (!state) {
2465 			rtnl_unlock();
2466 			return -ENOMEM;
2467 		}
2468 		state->filter_wiphy = -1;
2469 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2470 		if (ret) {
2471 			kfree(state);
2472 			rtnl_unlock();
2473 			return ret;
2474 		}
2475 		cb->args[0] = (long)state;
2476 	}
2477 
2478 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2479 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2480 			continue;
2481 		if (++idx <= state->start)
2482 			continue;
2483 		if (state->filter_wiphy != -1 &&
2484 		    state->filter_wiphy != rdev->wiphy_idx)
2485 			continue;
2486 		/* attempt to fit multiple wiphy data chunks into the skb */
2487 		do {
2488 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2489 						 skb,
2490 						 NETLINK_CB(cb->skb).portid,
2491 						 cb->nlh->nlmsg_seq,
2492 						 NLM_F_MULTI, state);
2493 			if (ret < 0) {
2494 				/*
2495 				 * If sending the wiphy data didn't fit (ENOBUFS
2496 				 * or EMSGSIZE returned), this SKB is still
2497 				 * empty (so it's not too big because another
2498 				 * wiphy dataset is already in the skb) and
2499 				 * we've not tried to adjust the dump allocation
2500 				 * yet ... then adjust the alloc size to be
2501 				 * bigger, and return 1 but with the empty skb.
2502 				 * This results in an empty message being RX'ed
2503 				 * in userspace, but that is ignored.
2504 				 *
2505 				 * We can then retry with the larger buffer.
2506 				 */
2507 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2508 				    !skb->len && !state->split &&
2509 				    cb->min_dump_alloc < 4096) {
2510 					cb->min_dump_alloc = 4096;
2511 					state->split_start = 0;
2512 					rtnl_unlock();
2513 					return 1;
2514 				}
2515 				idx--;
2516 				break;
2517 			}
2518 		} while (state->split_start > 0);
2519 		break;
2520 	}
2521 	rtnl_unlock();
2522 
2523 	state->start = idx;
2524 
2525 	return skb->len;
2526 }
2527 
2528 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2529 {
2530 	kfree((void *)cb->args[0]);
2531 	return 0;
2532 }
2533 
2534 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2535 {
2536 	struct sk_buff *msg;
2537 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2538 	struct nl80211_dump_wiphy_state state = {};
2539 
2540 	msg = nlmsg_new(4096, GFP_KERNEL);
2541 	if (!msg)
2542 		return -ENOMEM;
2543 
2544 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2545 			       info->snd_portid, info->snd_seq, 0,
2546 			       &state) < 0) {
2547 		nlmsg_free(msg);
2548 		return -ENOBUFS;
2549 	}
2550 
2551 	return genlmsg_reply(msg, info);
2552 }
2553 
2554 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2555 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2556 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2557 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2558 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2559 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2560 };
2561 
2562 static int parse_txq_params(struct nlattr *tb[],
2563 			    struct ieee80211_txq_params *txq_params)
2564 {
2565 	u8 ac;
2566 
2567 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2568 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2569 	    !tb[NL80211_TXQ_ATTR_AIFS])
2570 		return -EINVAL;
2571 
2572 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2573 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2574 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2575 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2576 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2577 
2578 	if (ac >= NL80211_NUM_ACS)
2579 		return -EINVAL;
2580 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2581 	return 0;
2582 }
2583 
2584 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2585 {
2586 	/*
2587 	 * You can only set the channel explicitly for WDS interfaces,
2588 	 * all others have their channel managed via their respective
2589 	 * "establish a connection" command (connect, join, ...)
2590 	 *
2591 	 * For AP/GO and mesh mode, the channel can be set with the
2592 	 * channel userspace API, but is only stored and passed to the
2593 	 * low-level driver when the AP starts or the mesh is joined.
2594 	 * This is for backward compatibility, userspace can also give
2595 	 * the channel in the start-ap or join-mesh commands instead.
2596 	 *
2597 	 * Monitors are special as they are normally slaved to
2598 	 * whatever else is going on, so they have their own special
2599 	 * operation to set the monitor channel if possible.
2600 	 */
2601 	return !wdev ||
2602 		wdev->iftype == NL80211_IFTYPE_AP ||
2603 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2604 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2605 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2606 }
2607 
2608 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2609 			  struct genl_info *info,
2610 			  struct cfg80211_chan_def *chandef)
2611 {
2612 	struct netlink_ext_ack *extack = info->extack;
2613 	struct nlattr **attrs = info->attrs;
2614 	u32 control_freq;
2615 
2616 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2617 		return -EINVAL;
2618 
2619 	control_freq = nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]);
2620 
2621 	chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2622 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2623 	chandef->center_freq1 = control_freq;
2624 	chandef->center_freq2 = 0;
2625 
2626 	/* Primary channel not allowed */
2627 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2628 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2629 				    "Channel is disabled");
2630 		return -EINVAL;
2631 	}
2632 
2633 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2634 		enum nl80211_channel_type chantype;
2635 
2636 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2637 
2638 		switch (chantype) {
2639 		case NL80211_CHAN_NO_HT:
2640 		case NL80211_CHAN_HT20:
2641 		case NL80211_CHAN_HT40PLUS:
2642 		case NL80211_CHAN_HT40MINUS:
2643 			cfg80211_chandef_create(chandef, chandef->chan,
2644 						chantype);
2645 			/* user input for center_freq is incorrect */
2646 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2647 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2648 				NL_SET_ERR_MSG_ATTR(extack,
2649 						    attrs[NL80211_ATTR_CENTER_FREQ1],
2650 						    "bad center frequency 1");
2651 				return -EINVAL;
2652 			}
2653 			/* center_freq2 must be zero */
2654 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2655 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2656 				NL_SET_ERR_MSG_ATTR(extack,
2657 						    attrs[NL80211_ATTR_CENTER_FREQ2],
2658 						    "center frequency 2 can't be used");
2659 				return -EINVAL;
2660 			}
2661 			break;
2662 		default:
2663 			NL_SET_ERR_MSG_ATTR(extack,
2664 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2665 					    "invalid channel type");
2666 			return -EINVAL;
2667 		}
2668 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2669 		chandef->width =
2670 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2671 		if (attrs[NL80211_ATTR_CENTER_FREQ1])
2672 			chandef->center_freq1 =
2673 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2674 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
2675 			chandef->center_freq2 =
2676 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2677 	}
2678 
2679 	if (!cfg80211_chandef_valid(chandef)) {
2680 		NL_SET_ERR_MSG(extack, "invalid channel definition");
2681 		return -EINVAL;
2682 	}
2683 
2684 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2685 				     IEEE80211_CHAN_DISABLED)) {
2686 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2687 		return -EINVAL;
2688 	}
2689 
2690 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2691 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2692 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2693 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2694 		return -EINVAL;
2695 	}
2696 
2697 	return 0;
2698 }
2699 
2700 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2701 				 struct net_device *dev,
2702 				 struct genl_info *info)
2703 {
2704 	struct cfg80211_chan_def chandef;
2705 	int result;
2706 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2707 	struct wireless_dev *wdev = NULL;
2708 
2709 	if (dev)
2710 		wdev = dev->ieee80211_ptr;
2711 	if (!nl80211_can_set_dev_channel(wdev))
2712 		return -EOPNOTSUPP;
2713 	if (wdev)
2714 		iftype = wdev->iftype;
2715 
2716 	result = nl80211_parse_chandef(rdev, info, &chandef);
2717 	if (result)
2718 		return result;
2719 
2720 	switch (iftype) {
2721 	case NL80211_IFTYPE_AP:
2722 	case NL80211_IFTYPE_P2P_GO:
2723 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2724 						   iftype)) {
2725 			result = -EINVAL;
2726 			break;
2727 		}
2728 		if (wdev->beacon_interval) {
2729 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2730 			    !(rdev->wiphy.features &
2731 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2732 				result = -EBUSY;
2733 				break;
2734 			}
2735 
2736 			/* Only allow dynamic channel width changes */
2737 			if (chandef.chan != wdev->preset_chandef.chan) {
2738 				result = -EBUSY;
2739 				break;
2740 			}
2741 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2742 			if (result)
2743 				break;
2744 		}
2745 		wdev->preset_chandef = chandef;
2746 		result = 0;
2747 		break;
2748 	case NL80211_IFTYPE_MESH_POINT:
2749 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2750 		break;
2751 	case NL80211_IFTYPE_MONITOR:
2752 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2753 		break;
2754 	default:
2755 		result = -EINVAL;
2756 	}
2757 
2758 	return result;
2759 }
2760 
2761 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2762 {
2763 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2764 	struct net_device *netdev = info->user_ptr[1];
2765 
2766 	return __nl80211_set_channel(rdev, netdev, info);
2767 }
2768 
2769 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2770 {
2771 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2772 	struct net_device *dev = info->user_ptr[1];
2773 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2774 	const u8 *bssid;
2775 
2776 	if (!info->attrs[NL80211_ATTR_MAC])
2777 		return -EINVAL;
2778 
2779 	if (netif_running(dev))
2780 		return -EBUSY;
2781 
2782 	if (!rdev->ops->set_wds_peer)
2783 		return -EOPNOTSUPP;
2784 
2785 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2786 		return -EOPNOTSUPP;
2787 
2788 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2789 	return rdev_set_wds_peer(rdev, dev, bssid);
2790 }
2791 
2792 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2793 {
2794 	struct cfg80211_registered_device *rdev;
2795 	struct net_device *netdev = NULL;
2796 	struct wireless_dev *wdev;
2797 	int result = 0, rem_txq_params = 0;
2798 	struct nlattr *nl_txq_params;
2799 	u32 changed;
2800 	u8 retry_short = 0, retry_long = 0;
2801 	u32 frag_threshold = 0, rts_threshold = 0;
2802 	u8 coverage_class = 0;
2803 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2804 
2805 	ASSERT_RTNL();
2806 
2807 	/*
2808 	 * Try to find the wiphy and netdev. Normally this
2809 	 * function shouldn't need the netdev, but this is
2810 	 * done for backward compatibility -- previously
2811 	 * setting the channel was done per wiphy, but now
2812 	 * it is per netdev. Previous userland like hostapd
2813 	 * also passed a netdev to set_wiphy, so that it is
2814 	 * possible to let that go to the right netdev!
2815 	 */
2816 
2817 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
2818 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2819 
2820 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2821 		if (netdev && netdev->ieee80211_ptr)
2822 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2823 		else
2824 			netdev = NULL;
2825 	}
2826 
2827 	if (!netdev) {
2828 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2829 						  info->attrs);
2830 		if (IS_ERR(rdev))
2831 			return PTR_ERR(rdev);
2832 		wdev = NULL;
2833 		netdev = NULL;
2834 		result = 0;
2835 	} else
2836 		wdev = netdev->ieee80211_ptr;
2837 
2838 	/*
2839 	 * end workaround code, by now the rdev is available
2840 	 * and locked, and wdev may or may not be NULL.
2841 	 */
2842 
2843 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2844 		result = cfg80211_dev_rename(
2845 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2846 
2847 	if (result)
2848 		return result;
2849 
2850 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2851 		struct ieee80211_txq_params txq_params;
2852 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2853 
2854 		if (!rdev->ops->set_txq_params)
2855 			return -EOPNOTSUPP;
2856 
2857 		if (!netdev)
2858 			return -EINVAL;
2859 
2860 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2861 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2862 			return -EINVAL;
2863 
2864 		if (!netif_running(netdev))
2865 			return -ENETDOWN;
2866 
2867 		nla_for_each_nested(nl_txq_params,
2868 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2869 				    rem_txq_params) {
2870 			result = nla_parse_nested_deprecated(tb,
2871 							     NL80211_TXQ_ATTR_MAX,
2872 							     nl_txq_params,
2873 							     txq_params_policy,
2874 							     info->extack);
2875 			if (result)
2876 				return result;
2877 			result = parse_txq_params(tb, &txq_params);
2878 			if (result)
2879 				return result;
2880 
2881 			result = rdev_set_txq_params(rdev, netdev,
2882 						     &txq_params);
2883 			if (result)
2884 				return result;
2885 		}
2886 	}
2887 
2888 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2889 		result = __nl80211_set_channel(
2890 			rdev,
2891 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2892 			info);
2893 		if (result)
2894 			return result;
2895 	}
2896 
2897 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2898 		struct wireless_dev *txp_wdev = wdev;
2899 		enum nl80211_tx_power_setting type;
2900 		int idx, mbm = 0;
2901 
2902 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2903 			txp_wdev = NULL;
2904 
2905 		if (!rdev->ops->set_tx_power)
2906 			return -EOPNOTSUPP;
2907 
2908 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2909 		type = nla_get_u32(info->attrs[idx]);
2910 
2911 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2912 		    (type != NL80211_TX_POWER_AUTOMATIC))
2913 			return -EINVAL;
2914 
2915 		if (type != NL80211_TX_POWER_AUTOMATIC) {
2916 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2917 			mbm = nla_get_u32(info->attrs[idx]);
2918 		}
2919 
2920 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2921 		if (result)
2922 			return result;
2923 	}
2924 
2925 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2926 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2927 		u32 tx_ant, rx_ant;
2928 
2929 		if ((!rdev->wiphy.available_antennas_tx &&
2930 		     !rdev->wiphy.available_antennas_rx) ||
2931 		    !rdev->ops->set_antenna)
2932 			return -EOPNOTSUPP;
2933 
2934 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2935 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2936 
2937 		/* reject antenna configurations which don't match the
2938 		 * available antenna masks, except for the "all" mask */
2939 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2940 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2941 			return -EINVAL;
2942 
2943 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2944 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2945 
2946 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2947 		if (result)
2948 			return result;
2949 	}
2950 
2951 	changed = 0;
2952 
2953 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2954 		retry_short = nla_get_u8(
2955 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2956 
2957 		changed |= WIPHY_PARAM_RETRY_SHORT;
2958 	}
2959 
2960 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2961 		retry_long = nla_get_u8(
2962 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2963 
2964 		changed |= WIPHY_PARAM_RETRY_LONG;
2965 	}
2966 
2967 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2968 		frag_threshold = nla_get_u32(
2969 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2970 		if (frag_threshold < 256)
2971 			return -EINVAL;
2972 
2973 		if (frag_threshold != (u32) -1) {
2974 			/*
2975 			 * Fragments (apart from the last one) are required to
2976 			 * have even length. Make the fragmentation code
2977 			 * simpler by stripping LSB should someone try to use
2978 			 * odd threshold value.
2979 			 */
2980 			frag_threshold &= ~0x1;
2981 		}
2982 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2983 	}
2984 
2985 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2986 		rts_threshold = nla_get_u32(
2987 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2988 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
2989 	}
2990 
2991 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2992 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2993 			return -EINVAL;
2994 
2995 		coverage_class = nla_get_u8(
2996 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2997 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
2998 	}
2999 
3000 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3001 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3002 			return -EOPNOTSUPP;
3003 
3004 		changed |= WIPHY_PARAM_DYN_ACK;
3005 	}
3006 
3007 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3008 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3009 					     NL80211_EXT_FEATURE_TXQS))
3010 			return -EOPNOTSUPP;
3011 		txq_limit = nla_get_u32(
3012 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3013 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3014 	}
3015 
3016 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3017 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3018 					     NL80211_EXT_FEATURE_TXQS))
3019 			return -EOPNOTSUPP;
3020 		txq_memory_limit = nla_get_u32(
3021 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3022 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3023 	}
3024 
3025 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3026 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3027 					     NL80211_EXT_FEATURE_TXQS))
3028 			return -EOPNOTSUPP;
3029 		txq_quantum = nla_get_u32(
3030 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3031 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3032 	}
3033 
3034 	if (changed) {
3035 		u8 old_retry_short, old_retry_long;
3036 		u32 old_frag_threshold, old_rts_threshold;
3037 		u8 old_coverage_class;
3038 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3039 
3040 		if (!rdev->ops->set_wiphy_params)
3041 			return -EOPNOTSUPP;
3042 
3043 		old_retry_short = rdev->wiphy.retry_short;
3044 		old_retry_long = rdev->wiphy.retry_long;
3045 		old_frag_threshold = rdev->wiphy.frag_threshold;
3046 		old_rts_threshold = rdev->wiphy.rts_threshold;
3047 		old_coverage_class = rdev->wiphy.coverage_class;
3048 		old_txq_limit = rdev->wiphy.txq_limit;
3049 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3050 		old_txq_quantum = rdev->wiphy.txq_quantum;
3051 
3052 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3053 			rdev->wiphy.retry_short = retry_short;
3054 		if (changed & WIPHY_PARAM_RETRY_LONG)
3055 			rdev->wiphy.retry_long = retry_long;
3056 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3057 			rdev->wiphy.frag_threshold = frag_threshold;
3058 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3059 			rdev->wiphy.rts_threshold = rts_threshold;
3060 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3061 			rdev->wiphy.coverage_class = coverage_class;
3062 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3063 			rdev->wiphy.txq_limit = txq_limit;
3064 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3065 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3066 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3067 			rdev->wiphy.txq_quantum = txq_quantum;
3068 
3069 		result = rdev_set_wiphy_params(rdev, changed);
3070 		if (result) {
3071 			rdev->wiphy.retry_short = old_retry_short;
3072 			rdev->wiphy.retry_long = old_retry_long;
3073 			rdev->wiphy.frag_threshold = old_frag_threshold;
3074 			rdev->wiphy.rts_threshold = old_rts_threshold;
3075 			rdev->wiphy.coverage_class = old_coverage_class;
3076 			rdev->wiphy.txq_limit = old_txq_limit;
3077 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3078 			rdev->wiphy.txq_quantum = old_txq_quantum;
3079 			return result;
3080 		}
3081 	}
3082 	return 0;
3083 }
3084 
3085 static int nl80211_send_chandef(struct sk_buff *msg,
3086 				const struct cfg80211_chan_def *chandef)
3087 {
3088 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3089 		return -EINVAL;
3090 
3091 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3092 			chandef->chan->center_freq))
3093 		return -ENOBUFS;
3094 	switch (chandef->width) {
3095 	case NL80211_CHAN_WIDTH_20_NOHT:
3096 	case NL80211_CHAN_WIDTH_20:
3097 	case NL80211_CHAN_WIDTH_40:
3098 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3099 				cfg80211_get_chandef_type(chandef)))
3100 			return -ENOBUFS;
3101 		break;
3102 	default:
3103 		break;
3104 	}
3105 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3106 		return -ENOBUFS;
3107 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3108 		return -ENOBUFS;
3109 	if (chandef->center_freq2 &&
3110 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3111 		return -ENOBUFS;
3112 	return 0;
3113 }
3114 
3115 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3116 			      struct cfg80211_registered_device *rdev,
3117 			      struct wireless_dev *wdev,
3118 			      enum nl80211_commands cmd)
3119 {
3120 	struct net_device *dev = wdev->netdev;
3121 	void *hdr;
3122 
3123 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3124 		cmd != NL80211_CMD_DEL_INTERFACE &&
3125 		cmd != NL80211_CMD_SET_INTERFACE);
3126 
3127 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3128 	if (!hdr)
3129 		return -1;
3130 
3131 	if (dev &&
3132 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3133 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3134 		goto nla_put_failure;
3135 
3136 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3137 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3138 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3139 			      NL80211_ATTR_PAD) ||
3140 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3141 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3142 			rdev->devlist_generation ^
3143 			(cfg80211_rdev_list_generation << 2)) ||
3144 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3145 		goto nla_put_failure;
3146 
3147 	if (rdev->ops->get_channel) {
3148 		int ret;
3149 		struct cfg80211_chan_def chandef;
3150 
3151 		ret = rdev_get_channel(rdev, wdev, &chandef);
3152 		if (ret == 0) {
3153 			if (nl80211_send_chandef(msg, &chandef))
3154 				goto nla_put_failure;
3155 		}
3156 	}
3157 
3158 	if (rdev->ops->get_tx_power) {
3159 		int dbm, ret;
3160 
3161 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3162 		if (ret == 0 &&
3163 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3164 				DBM_TO_MBM(dbm)))
3165 			goto nla_put_failure;
3166 	}
3167 
3168 	wdev_lock(wdev);
3169 	switch (wdev->iftype) {
3170 	case NL80211_IFTYPE_AP:
3171 		if (wdev->ssid_len &&
3172 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3173 			goto nla_put_failure_locked;
3174 		break;
3175 	case NL80211_IFTYPE_STATION:
3176 	case NL80211_IFTYPE_P2P_CLIENT:
3177 	case NL80211_IFTYPE_ADHOC: {
3178 		const u8 *ssid_ie;
3179 		if (!wdev->current_bss)
3180 			break;
3181 		rcu_read_lock();
3182 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3183 					       WLAN_EID_SSID);
3184 		if (ssid_ie &&
3185 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3186 			goto nla_put_failure_rcu_locked;
3187 		rcu_read_unlock();
3188 		break;
3189 		}
3190 	default:
3191 		/* nothing */
3192 		break;
3193 	}
3194 	wdev_unlock(wdev);
3195 
3196 	if (rdev->ops->get_txq_stats) {
3197 		struct cfg80211_txq_stats txqstats = {};
3198 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3199 
3200 		if (ret == 0 &&
3201 		    !nl80211_put_txq_stats(msg, &txqstats,
3202 					   NL80211_ATTR_TXQ_STATS))
3203 			goto nla_put_failure;
3204 	}
3205 
3206 	genlmsg_end(msg, hdr);
3207 	return 0;
3208 
3209  nla_put_failure_rcu_locked:
3210 	rcu_read_unlock();
3211  nla_put_failure_locked:
3212 	wdev_unlock(wdev);
3213  nla_put_failure:
3214 	genlmsg_cancel(msg, hdr);
3215 	return -EMSGSIZE;
3216 }
3217 
3218 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3219 {
3220 	int wp_idx = 0;
3221 	int if_idx = 0;
3222 	int wp_start = cb->args[0];
3223 	int if_start = cb->args[1];
3224 	int filter_wiphy = -1;
3225 	struct cfg80211_registered_device *rdev;
3226 	struct wireless_dev *wdev;
3227 	int ret;
3228 
3229 	rtnl_lock();
3230 	if (!cb->args[2]) {
3231 		struct nl80211_dump_wiphy_state state = {
3232 			.filter_wiphy = -1,
3233 		};
3234 
3235 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3236 		if (ret)
3237 			goto out_unlock;
3238 
3239 		filter_wiphy = state.filter_wiphy;
3240 
3241 		/*
3242 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3243 		 * value needed to determine that parsing is necessary.
3244 		 */
3245 		if (filter_wiphy >= 0)
3246 			cb->args[2] = filter_wiphy + 1;
3247 		else
3248 			cb->args[2] = -1;
3249 	} else if (cb->args[2] > 0) {
3250 		filter_wiphy = cb->args[2] - 1;
3251 	}
3252 
3253 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3254 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3255 			continue;
3256 		if (wp_idx < wp_start) {
3257 			wp_idx++;
3258 			continue;
3259 		}
3260 
3261 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3262 			continue;
3263 
3264 		if_idx = 0;
3265 
3266 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3267 			if (if_idx < if_start) {
3268 				if_idx++;
3269 				continue;
3270 			}
3271 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3272 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3273 					       rdev, wdev,
3274 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3275 				goto out;
3276 			}
3277 			if_idx++;
3278 		}
3279 
3280 		wp_idx++;
3281 	}
3282  out:
3283 	cb->args[0] = wp_idx;
3284 	cb->args[1] = if_idx;
3285 
3286 	ret = skb->len;
3287  out_unlock:
3288 	rtnl_unlock();
3289 
3290 	return ret;
3291 }
3292 
3293 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3294 {
3295 	struct sk_buff *msg;
3296 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3297 	struct wireless_dev *wdev = info->user_ptr[1];
3298 
3299 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3300 	if (!msg)
3301 		return -ENOMEM;
3302 
3303 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3304 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3305 		nlmsg_free(msg);
3306 		return -ENOBUFS;
3307 	}
3308 
3309 	return genlmsg_reply(msg, info);
3310 }
3311 
3312 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3313 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3314 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3315 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3316 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3317 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3318 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3319 };
3320 
3321 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3322 {
3323 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3324 	int flag;
3325 
3326 	*mntrflags = 0;
3327 
3328 	if (!nla)
3329 		return -EINVAL;
3330 
3331 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3332 		return -EINVAL;
3333 
3334 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3335 		if (flags[flag])
3336 			*mntrflags |= (1<<flag);
3337 
3338 	*mntrflags |= MONITOR_FLAG_CHANGED;
3339 
3340 	return 0;
3341 }
3342 
3343 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3344 				     enum nl80211_iftype type,
3345 				     struct genl_info *info,
3346 				     struct vif_params *params)
3347 {
3348 	bool change = false;
3349 	int err;
3350 
3351 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3352 		if (type != NL80211_IFTYPE_MONITOR)
3353 			return -EINVAL;
3354 
3355 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3356 					  &params->flags);
3357 		if (err)
3358 			return err;
3359 
3360 		change = true;
3361 	}
3362 
3363 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3364 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3365 		return -EOPNOTSUPP;
3366 
3367 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3368 		const u8 *mumimo_groups;
3369 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3370 
3371 		if (type != NL80211_IFTYPE_MONITOR)
3372 			return -EINVAL;
3373 
3374 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3375 			return -EOPNOTSUPP;
3376 
3377 		mumimo_groups =
3378 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3379 
3380 		/* bits 0 and 63 are reserved and must be zero */
3381 		if ((mumimo_groups[0] & BIT(0)) ||
3382 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3383 			return -EINVAL;
3384 
3385 		params->vht_mumimo_groups = mumimo_groups;
3386 		change = true;
3387 	}
3388 
3389 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3390 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3391 
3392 		if (type != NL80211_IFTYPE_MONITOR)
3393 			return -EINVAL;
3394 
3395 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3396 			return -EOPNOTSUPP;
3397 
3398 		params->vht_mumimo_follow_addr =
3399 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3400 		change = true;
3401 	}
3402 
3403 	return change ? 1 : 0;
3404 }
3405 
3406 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3407 			       struct net_device *netdev, u8 use_4addr,
3408 			       enum nl80211_iftype iftype)
3409 {
3410 	if (!use_4addr) {
3411 		if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3412 			return -EBUSY;
3413 		return 0;
3414 	}
3415 
3416 	switch (iftype) {
3417 	case NL80211_IFTYPE_AP_VLAN:
3418 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3419 			return 0;
3420 		break;
3421 	case NL80211_IFTYPE_STATION:
3422 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3423 			return 0;
3424 		break;
3425 	default:
3426 		break;
3427 	}
3428 
3429 	return -EOPNOTSUPP;
3430 }
3431 
3432 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3433 {
3434 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3435 	struct vif_params params;
3436 	int err;
3437 	enum nl80211_iftype otype, ntype;
3438 	struct net_device *dev = info->user_ptr[1];
3439 	bool change = false;
3440 
3441 	memset(&params, 0, sizeof(params));
3442 
3443 	otype = ntype = dev->ieee80211_ptr->iftype;
3444 
3445 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3446 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3447 		if (otype != ntype)
3448 			change = true;
3449 	}
3450 
3451 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3452 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3453 
3454 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3455 			return -EINVAL;
3456 		if (netif_running(dev))
3457 			return -EBUSY;
3458 
3459 		wdev_lock(wdev);
3460 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3461 			     IEEE80211_MAX_MESH_ID_LEN);
3462 		wdev->mesh_id_up_len =
3463 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3464 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3465 		       wdev->mesh_id_up_len);
3466 		wdev_unlock(wdev);
3467 	}
3468 
3469 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3470 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3471 		change = true;
3472 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3473 		if (err)
3474 			return err;
3475 	} else {
3476 		params.use_4addr = -1;
3477 	}
3478 
3479 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3480 	if (err < 0)
3481 		return err;
3482 	if (err > 0)
3483 		change = true;
3484 
3485 	if (change)
3486 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3487 	else
3488 		err = 0;
3489 
3490 	if (!err && params.use_4addr != -1)
3491 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3492 
3493 	if (change && !err) {
3494 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3495 
3496 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3497 	}
3498 
3499 	return err;
3500 }
3501 
3502 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3503 {
3504 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3505 	struct vif_params params;
3506 	struct wireless_dev *wdev;
3507 	struct sk_buff *msg;
3508 	int err;
3509 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3510 
3511 	/* to avoid failing a new interface creation due to pending removal */
3512 	cfg80211_destroy_ifaces(rdev);
3513 
3514 	memset(&params, 0, sizeof(params));
3515 
3516 	if (!info->attrs[NL80211_ATTR_IFNAME])
3517 		return -EINVAL;
3518 
3519 	if (info->attrs[NL80211_ATTR_IFTYPE])
3520 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3521 
3522 	if (!rdev->ops->add_virtual_intf)
3523 		return -EOPNOTSUPP;
3524 
3525 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3526 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3527 	    info->attrs[NL80211_ATTR_MAC]) {
3528 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3529 			   ETH_ALEN);
3530 		if (!is_valid_ether_addr(params.macaddr))
3531 			return -EADDRNOTAVAIL;
3532 	}
3533 
3534 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3535 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3536 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3537 		if (err)
3538 			return err;
3539 	}
3540 
3541 	if (!(rdev->wiphy.interface_modes & (1 << type)) &&
3542 	    !(type == NL80211_IFTYPE_AP_VLAN && params.use_4addr &&
3543 	      rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP))
3544 		return -EOPNOTSUPP;
3545 
3546 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3547 	if (err < 0)
3548 		return err;
3549 
3550 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3551 	if (!msg)
3552 		return -ENOMEM;
3553 
3554 	wdev = rdev_add_virtual_intf(rdev,
3555 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3556 				NET_NAME_USER, type, &params);
3557 	if (WARN_ON(!wdev)) {
3558 		nlmsg_free(msg);
3559 		return -EPROTO;
3560 	} else if (IS_ERR(wdev)) {
3561 		nlmsg_free(msg);
3562 		return PTR_ERR(wdev);
3563 	}
3564 
3565 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3566 		wdev->owner_nlportid = info->snd_portid;
3567 
3568 	switch (type) {
3569 	case NL80211_IFTYPE_MESH_POINT:
3570 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3571 			break;
3572 		wdev_lock(wdev);
3573 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3574 			     IEEE80211_MAX_MESH_ID_LEN);
3575 		wdev->mesh_id_up_len =
3576 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3577 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3578 		       wdev->mesh_id_up_len);
3579 		wdev_unlock(wdev);
3580 		break;
3581 	case NL80211_IFTYPE_NAN:
3582 	case NL80211_IFTYPE_P2P_DEVICE:
3583 		/*
3584 		 * P2P Device and NAN do not have a netdev, so don't go
3585 		 * through the netdev notifier and must be added here
3586 		 */
3587 		cfg80211_init_wdev(rdev, wdev);
3588 		break;
3589 	default:
3590 		break;
3591 	}
3592 
3593 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3594 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3595 		nlmsg_free(msg);
3596 		return -ENOBUFS;
3597 	}
3598 
3599 	return genlmsg_reply(msg, info);
3600 }
3601 
3602 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3603 {
3604 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3605 	struct wireless_dev *wdev = info->user_ptr[1];
3606 
3607 	if (!rdev->ops->del_virtual_intf)
3608 		return -EOPNOTSUPP;
3609 
3610 	/*
3611 	 * If we remove a wireless device without a netdev then clear
3612 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3613 	 * to check if it needs to do dev_put(). Otherwise it crashes
3614 	 * since the wdev has been freed, unlike with a netdev where
3615 	 * we need the dev_put() for the netdev to really be freed.
3616 	 */
3617 	if (!wdev->netdev)
3618 		info->user_ptr[1] = NULL;
3619 
3620 	return rdev_del_virtual_intf(rdev, wdev);
3621 }
3622 
3623 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3624 {
3625 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3626 	struct net_device *dev = info->user_ptr[1];
3627 	u16 noack_map;
3628 
3629 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3630 		return -EINVAL;
3631 
3632 	if (!rdev->ops->set_noack_map)
3633 		return -EOPNOTSUPP;
3634 
3635 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3636 
3637 	return rdev_set_noack_map(rdev, dev, noack_map);
3638 }
3639 
3640 struct get_key_cookie {
3641 	struct sk_buff *msg;
3642 	int error;
3643 	int idx;
3644 };
3645 
3646 static void get_key_callback(void *c, struct key_params *params)
3647 {
3648 	struct nlattr *key;
3649 	struct get_key_cookie *cookie = c;
3650 
3651 	if ((params->key &&
3652 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3653 		     params->key_len, params->key)) ||
3654 	    (params->seq &&
3655 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3656 		     params->seq_len, params->seq)) ||
3657 	    (params->cipher &&
3658 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3659 			 params->cipher)))
3660 		goto nla_put_failure;
3661 
3662 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3663 	if (!key)
3664 		goto nla_put_failure;
3665 
3666 	if ((params->key &&
3667 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3668 		     params->key_len, params->key)) ||
3669 	    (params->seq &&
3670 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3671 		     params->seq_len, params->seq)) ||
3672 	    (params->cipher &&
3673 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3674 			 params->cipher)))
3675 		goto nla_put_failure;
3676 
3677 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3678 		goto nla_put_failure;
3679 
3680 	nla_nest_end(cookie->msg, key);
3681 
3682 	return;
3683  nla_put_failure:
3684 	cookie->error = 1;
3685 }
3686 
3687 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3688 {
3689 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3690 	int err;
3691 	struct net_device *dev = info->user_ptr[1];
3692 	u8 key_idx = 0;
3693 	const u8 *mac_addr = NULL;
3694 	bool pairwise;
3695 	struct get_key_cookie cookie = {
3696 		.error = 0,
3697 	};
3698 	void *hdr;
3699 	struct sk_buff *msg;
3700 
3701 	if (info->attrs[NL80211_ATTR_KEY_IDX])
3702 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3703 
3704 	if (info->attrs[NL80211_ATTR_MAC])
3705 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3706 
3707 	pairwise = !!mac_addr;
3708 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3709 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3710 
3711 		if (kt != NL80211_KEYTYPE_GROUP &&
3712 		    kt != NL80211_KEYTYPE_PAIRWISE)
3713 			return -EINVAL;
3714 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3715 	}
3716 
3717 	if (!rdev->ops->get_key)
3718 		return -EOPNOTSUPP;
3719 
3720 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3721 		return -ENOENT;
3722 
3723 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3724 	if (!msg)
3725 		return -ENOMEM;
3726 
3727 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3728 			     NL80211_CMD_NEW_KEY);
3729 	if (!hdr)
3730 		goto nla_put_failure;
3731 
3732 	cookie.msg = msg;
3733 	cookie.idx = key_idx;
3734 
3735 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3736 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3737 		goto nla_put_failure;
3738 	if (mac_addr &&
3739 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3740 		goto nla_put_failure;
3741 
3742 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3743 			   get_key_callback);
3744 
3745 	if (err)
3746 		goto free_msg;
3747 
3748 	if (cookie.error)
3749 		goto nla_put_failure;
3750 
3751 	genlmsg_end(msg, hdr);
3752 	return genlmsg_reply(msg, info);
3753 
3754  nla_put_failure:
3755 	err = -ENOBUFS;
3756  free_msg:
3757 	nlmsg_free(msg);
3758 	return err;
3759 }
3760 
3761 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3762 {
3763 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3764 	struct key_parse key;
3765 	int err;
3766 	struct net_device *dev = info->user_ptr[1];
3767 
3768 	err = nl80211_parse_key(info, &key);
3769 	if (err)
3770 		return err;
3771 
3772 	if (key.idx < 0)
3773 		return -EINVAL;
3774 
3775 	/* Only support setting default key and
3776 	 * Extended Key ID action NL80211_KEY_SET_TX.
3777 	 */
3778 	if (!key.def && !key.defmgmt &&
3779 	    !(key.p.mode == NL80211_KEY_SET_TX))
3780 		return -EINVAL;
3781 
3782 	wdev_lock(dev->ieee80211_ptr);
3783 
3784 	if (key.def) {
3785 		if (!rdev->ops->set_default_key) {
3786 			err = -EOPNOTSUPP;
3787 			goto out;
3788 		}
3789 
3790 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3791 		if (err)
3792 			goto out;
3793 
3794 		err = rdev_set_default_key(rdev, dev, key.idx,
3795 						 key.def_uni, key.def_multi);
3796 
3797 		if (err)
3798 			goto out;
3799 
3800 #ifdef CONFIG_CFG80211_WEXT
3801 		dev->ieee80211_ptr->wext.default_key = key.idx;
3802 #endif
3803 	} else if (key.defmgmt) {
3804 		if (key.def_uni || !key.def_multi) {
3805 			err = -EINVAL;
3806 			goto out;
3807 		}
3808 
3809 		if (!rdev->ops->set_default_mgmt_key) {
3810 			err = -EOPNOTSUPP;
3811 			goto out;
3812 		}
3813 
3814 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3815 		if (err)
3816 			goto out;
3817 
3818 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3819 		if (err)
3820 			goto out;
3821 
3822 #ifdef CONFIG_CFG80211_WEXT
3823 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3824 #endif
3825 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
3826 		   wiphy_ext_feature_isset(&rdev->wiphy,
3827 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
3828 		u8 *mac_addr = NULL;
3829 
3830 		if (info->attrs[NL80211_ATTR_MAC])
3831 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3832 
3833 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
3834 			err = -EINVAL;
3835 			goto out;
3836 		}
3837 
3838 		err = rdev_add_key(rdev, dev, key.idx,
3839 				   NL80211_KEYTYPE_PAIRWISE,
3840 				   mac_addr, &key.p);
3841 	} else {
3842 		err = -EINVAL;
3843 	}
3844  out:
3845 	wdev_unlock(dev->ieee80211_ptr);
3846 
3847 	return err;
3848 }
3849 
3850 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3851 {
3852 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3853 	int err;
3854 	struct net_device *dev = info->user_ptr[1];
3855 	struct key_parse key;
3856 	const u8 *mac_addr = NULL;
3857 
3858 	err = nl80211_parse_key(info, &key);
3859 	if (err)
3860 		return err;
3861 
3862 	if (!key.p.key)
3863 		return -EINVAL;
3864 
3865 	if (info->attrs[NL80211_ATTR_MAC])
3866 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3867 
3868 	if (key.type == -1) {
3869 		if (mac_addr)
3870 			key.type = NL80211_KEYTYPE_PAIRWISE;
3871 		else
3872 			key.type = NL80211_KEYTYPE_GROUP;
3873 	}
3874 
3875 	/* for now */
3876 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3877 	    key.type != NL80211_KEYTYPE_GROUP)
3878 		return -EINVAL;
3879 
3880 	if (!rdev->ops->add_key)
3881 		return -EOPNOTSUPP;
3882 
3883 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3884 					   key.type == NL80211_KEYTYPE_PAIRWISE,
3885 					   mac_addr))
3886 		return -EINVAL;
3887 
3888 	wdev_lock(dev->ieee80211_ptr);
3889 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3890 	if (!err)
3891 		err = rdev_add_key(rdev, dev, key.idx,
3892 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3893 				    mac_addr, &key.p);
3894 	wdev_unlock(dev->ieee80211_ptr);
3895 
3896 	return err;
3897 }
3898 
3899 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3900 {
3901 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3902 	int err;
3903 	struct net_device *dev = info->user_ptr[1];
3904 	u8 *mac_addr = NULL;
3905 	struct key_parse key;
3906 
3907 	err = nl80211_parse_key(info, &key);
3908 	if (err)
3909 		return err;
3910 
3911 	if (info->attrs[NL80211_ATTR_MAC])
3912 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3913 
3914 	if (key.type == -1) {
3915 		if (mac_addr)
3916 			key.type = NL80211_KEYTYPE_PAIRWISE;
3917 		else
3918 			key.type = NL80211_KEYTYPE_GROUP;
3919 	}
3920 
3921 	/* for now */
3922 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3923 	    key.type != NL80211_KEYTYPE_GROUP)
3924 		return -EINVAL;
3925 
3926 	if (!rdev->ops->del_key)
3927 		return -EOPNOTSUPP;
3928 
3929 	wdev_lock(dev->ieee80211_ptr);
3930 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3931 
3932 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3933 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3934 		err = -ENOENT;
3935 
3936 	if (!err)
3937 		err = rdev_del_key(rdev, dev, key.idx,
3938 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3939 				   mac_addr);
3940 
3941 #ifdef CONFIG_CFG80211_WEXT
3942 	if (!err) {
3943 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
3944 			dev->ieee80211_ptr->wext.default_key = -1;
3945 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3946 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3947 	}
3948 #endif
3949 	wdev_unlock(dev->ieee80211_ptr);
3950 
3951 	return err;
3952 }
3953 
3954 /* This function returns an error or the number of nested attributes */
3955 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3956 {
3957 	struct nlattr *attr;
3958 	int n_entries = 0, tmp;
3959 
3960 	nla_for_each_nested(attr, nl_attr, tmp) {
3961 		if (nla_len(attr) != ETH_ALEN)
3962 			return -EINVAL;
3963 
3964 		n_entries++;
3965 	}
3966 
3967 	return n_entries;
3968 }
3969 
3970 /*
3971  * This function parses ACL information and allocates memory for ACL data.
3972  * On successful return, the calling function is responsible to free the
3973  * ACL buffer returned by this function.
3974  */
3975 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3976 						struct genl_info *info)
3977 {
3978 	enum nl80211_acl_policy acl_policy;
3979 	struct nlattr *attr;
3980 	struct cfg80211_acl_data *acl;
3981 	int i = 0, n_entries, tmp;
3982 
3983 	if (!wiphy->max_acl_mac_addrs)
3984 		return ERR_PTR(-EOPNOTSUPP);
3985 
3986 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3987 		return ERR_PTR(-EINVAL);
3988 
3989 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3990 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3991 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3992 		return ERR_PTR(-EINVAL);
3993 
3994 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3995 		return ERR_PTR(-EINVAL);
3996 
3997 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3998 	if (n_entries < 0)
3999 		return ERR_PTR(n_entries);
4000 
4001 	if (n_entries > wiphy->max_acl_mac_addrs)
4002 		return ERR_PTR(-ENOTSUPP);
4003 
4004 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4005 	if (!acl)
4006 		return ERR_PTR(-ENOMEM);
4007 
4008 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4009 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4010 		i++;
4011 	}
4012 
4013 	acl->n_acl_entries = n_entries;
4014 	acl->acl_policy = acl_policy;
4015 
4016 	return acl;
4017 }
4018 
4019 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4020 {
4021 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4022 	struct net_device *dev = info->user_ptr[1];
4023 	struct cfg80211_acl_data *acl;
4024 	int err;
4025 
4026 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4027 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4028 		return -EOPNOTSUPP;
4029 
4030 	if (!dev->ieee80211_ptr->beacon_interval)
4031 		return -EINVAL;
4032 
4033 	acl = parse_acl_data(&rdev->wiphy, info);
4034 	if (IS_ERR(acl))
4035 		return PTR_ERR(acl);
4036 
4037 	err = rdev_set_mac_acl(rdev, dev, acl);
4038 
4039 	kfree(acl);
4040 
4041 	return err;
4042 }
4043 
4044 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4045 			   u8 *rates, u8 rates_len)
4046 {
4047 	u8 i;
4048 	u32 mask = 0;
4049 
4050 	for (i = 0; i < rates_len; i++) {
4051 		int rate = (rates[i] & 0x7f) * 5;
4052 		int ridx;
4053 
4054 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4055 			struct ieee80211_rate *srate =
4056 				&sband->bitrates[ridx];
4057 			if (rate == srate->bitrate) {
4058 				mask |= 1 << ridx;
4059 				break;
4060 			}
4061 		}
4062 		if (ridx == sband->n_bitrates)
4063 			return 0; /* rate not found */
4064 	}
4065 
4066 	return mask;
4067 }
4068 
4069 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4070 			       u8 *rates, u8 rates_len,
4071 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4072 {
4073 	u8 i;
4074 
4075 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4076 
4077 	for (i = 0; i < rates_len; i++) {
4078 		int ridx, rbit;
4079 
4080 		ridx = rates[i] / 8;
4081 		rbit = BIT(rates[i] % 8);
4082 
4083 		/* check validity */
4084 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4085 			return false;
4086 
4087 		/* check availability */
4088 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4089 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4090 			mcs[ridx] |= rbit;
4091 		else
4092 			return false;
4093 	}
4094 
4095 	return true;
4096 }
4097 
4098 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4099 {
4100 	u16 mcs_mask = 0;
4101 
4102 	switch (vht_mcs_map) {
4103 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4104 		break;
4105 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4106 		mcs_mask = 0x00FF;
4107 		break;
4108 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4109 		mcs_mask = 0x01FF;
4110 		break;
4111 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4112 		mcs_mask = 0x03FF;
4113 		break;
4114 	default:
4115 		break;
4116 	}
4117 
4118 	return mcs_mask;
4119 }
4120 
4121 static void vht_build_mcs_mask(u16 vht_mcs_map,
4122 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4123 {
4124 	u8 nss;
4125 
4126 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4127 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4128 		vht_mcs_map >>= 2;
4129 	}
4130 }
4131 
4132 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4133 			     struct nl80211_txrate_vht *txrate,
4134 			     u16 mcs[NL80211_VHT_NSS_MAX])
4135 {
4136 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4137 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4138 	u8 i;
4139 
4140 	if (!sband->vht_cap.vht_supported)
4141 		return false;
4142 
4143 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4144 
4145 	/* Build vht_mcs_mask from VHT capabilities */
4146 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4147 
4148 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4149 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4150 			mcs[i] = txrate->mcs[i];
4151 		else
4152 			return false;
4153 	}
4154 
4155 	return true;
4156 }
4157 
4158 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4159 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4160 				    .len = NL80211_MAX_SUPP_RATES },
4161 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
4162 				.len = NL80211_MAX_SUPP_HT_RATES },
4163 	[NL80211_TXRATE_VHT] = {
4164 		.type = NLA_EXACT_LEN_WARN,
4165 		.len = sizeof(struct nl80211_txrate_vht),
4166 	},
4167 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
4168 };
4169 
4170 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4171 					 struct cfg80211_bitrate_mask *mask)
4172 {
4173 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4174 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4175 	int rem, i;
4176 	struct nlattr *tx_rates;
4177 	struct ieee80211_supported_band *sband;
4178 	u16 vht_tx_mcs_map;
4179 
4180 	memset(mask, 0, sizeof(*mask));
4181 	/* Default to all rates enabled */
4182 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4183 		sband = rdev->wiphy.bands[i];
4184 
4185 		if (!sband)
4186 			continue;
4187 
4188 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4189 		memcpy(mask->control[i].ht_mcs,
4190 		       sband->ht_cap.mcs.rx_mask,
4191 		       sizeof(mask->control[i].ht_mcs));
4192 
4193 		if (!sband->vht_cap.vht_supported)
4194 			continue;
4195 
4196 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4197 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4198 	}
4199 
4200 	/* if no rates are given set it back to the defaults */
4201 	if (!info->attrs[NL80211_ATTR_TX_RATES])
4202 		goto out;
4203 
4204 	/* The nested attribute uses enum nl80211_band as the index. This maps
4205 	 * directly to the enum nl80211_band values used in cfg80211.
4206 	 */
4207 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4208 	nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
4209 		enum nl80211_band band = nla_type(tx_rates);
4210 		int err;
4211 
4212 		if (band < 0 || band >= NUM_NL80211_BANDS)
4213 			return -EINVAL;
4214 		sband = rdev->wiphy.bands[band];
4215 		if (sband == NULL)
4216 			return -EINVAL;
4217 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4218 						  tx_rates,
4219 						  nl80211_txattr_policy,
4220 						  info->extack);
4221 		if (err)
4222 			return err;
4223 		if (tb[NL80211_TXRATE_LEGACY]) {
4224 			mask->control[band].legacy = rateset_to_mask(
4225 				sband,
4226 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4227 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4228 			if ((mask->control[band].legacy == 0) &&
4229 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4230 				return -EINVAL;
4231 		}
4232 		if (tb[NL80211_TXRATE_HT]) {
4233 			if (!ht_rateset_to_mask(
4234 					sband,
4235 					nla_data(tb[NL80211_TXRATE_HT]),
4236 					nla_len(tb[NL80211_TXRATE_HT]),
4237 					mask->control[band].ht_mcs))
4238 				return -EINVAL;
4239 		}
4240 		if (tb[NL80211_TXRATE_VHT]) {
4241 			if (!vht_set_mcs_mask(
4242 					sband,
4243 					nla_data(tb[NL80211_TXRATE_VHT]),
4244 					mask->control[band].vht_mcs))
4245 				return -EINVAL;
4246 		}
4247 		if (tb[NL80211_TXRATE_GI]) {
4248 			mask->control[band].gi =
4249 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4250 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4251 				return -EINVAL;
4252 		}
4253 
4254 		if (mask->control[band].legacy == 0) {
4255 			/* don't allow empty legacy rates if HT or VHT
4256 			 * are not even supported.
4257 			 */
4258 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4259 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
4260 				return -EINVAL;
4261 
4262 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4263 				if (mask->control[band].ht_mcs[i])
4264 					goto out;
4265 
4266 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4267 				if (mask->control[band].vht_mcs[i])
4268 					goto out;
4269 
4270 			/* legacy and mcs rates may not be both empty */
4271 			return -EINVAL;
4272 		}
4273 	}
4274 
4275 out:
4276 	return 0;
4277 }
4278 
4279 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4280 				   enum nl80211_band band,
4281 				   struct cfg80211_bitrate_mask *beacon_rate)
4282 {
4283 	u32 count_ht, count_vht, i;
4284 	u32 rate = beacon_rate->control[band].legacy;
4285 
4286 	/* Allow only one rate */
4287 	if (hweight32(rate) > 1)
4288 		return -EINVAL;
4289 
4290 	count_ht = 0;
4291 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4292 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4293 			return -EINVAL;
4294 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4295 			count_ht++;
4296 			if (count_ht > 1)
4297 				return -EINVAL;
4298 		}
4299 		if (count_ht && rate)
4300 			return -EINVAL;
4301 	}
4302 
4303 	count_vht = 0;
4304 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4305 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4306 			return -EINVAL;
4307 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4308 			count_vht++;
4309 			if (count_vht > 1)
4310 				return -EINVAL;
4311 		}
4312 		if (count_vht && rate)
4313 			return -EINVAL;
4314 	}
4315 
4316 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4317 		return -EINVAL;
4318 
4319 	if (rate &&
4320 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4321 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4322 		return -EINVAL;
4323 	if (count_ht &&
4324 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4325 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4326 		return -EINVAL;
4327 	if (count_vht &&
4328 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4329 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4330 		return -EINVAL;
4331 
4332 	return 0;
4333 }
4334 
4335 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4336 				struct nlattr *attrs[],
4337 				struct cfg80211_beacon_data *bcn)
4338 {
4339 	bool haveinfo = false;
4340 	int err;
4341 
4342 	memset(bcn, 0, sizeof(*bcn));
4343 
4344 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4345 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4346 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4347 		if (!bcn->head_len)
4348 			return -EINVAL;
4349 		haveinfo = true;
4350 	}
4351 
4352 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4353 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4354 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4355 		haveinfo = true;
4356 	}
4357 
4358 	if (!haveinfo)
4359 		return -EINVAL;
4360 
4361 	if (attrs[NL80211_ATTR_IE]) {
4362 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4363 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4364 	}
4365 
4366 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4367 		bcn->proberesp_ies =
4368 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4369 		bcn->proberesp_ies_len =
4370 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4371 	}
4372 
4373 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4374 		bcn->assocresp_ies =
4375 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4376 		bcn->assocresp_ies_len =
4377 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4378 	}
4379 
4380 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
4381 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4382 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4383 	}
4384 
4385 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4386 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4387 
4388 		err = nla_parse_nested_deprecated(tb,
4389 						  NL80211_FTM_RESP_ATTR_MAX,
4390 						  attrs[NL80211_ATTR_FTM_RESPONDER],
4391 						  NULL, NULL);
4392 		if (err)
4393 			return err;
4394 
4395 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4396 		    wiphy_ext_feature_isset(&rdev->wiphy,
4397 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4398 			bcn->ftm_responder = 1;
4399 		else
4400 			return -EOPNOTSUPP;
4401 
4402 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4403 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4404 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4405 		}
4406 
4407 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4408 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4409 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4410 		}
4411 	} else {
4412 		bcn->ftm_responder = -1;
4413 	}
4414 
4415 	return 0;
4416 }
4417 
4418 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4419 				    struct ieee80211_he_obss_pd *he_obss_pd)
4420 {
4421 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4422 	int err;
4423 
4424 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4425 			       he_obss_pd_policy, NULL);
4426 	if (err)
4427 		return err;
4428 
4429 	if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4430 	    !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4431 		return -EINVAL;
4432 
4433 	he_obss_pd->min_offset =
4434 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4435 	he_obss_pd->max_offset =
4436 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4437 
4438 	if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4439 		return -EINVAL;
4440 
4441 	he_obss_pd->enable = true;
4442 
4443 	return 0;
4444 }
4445 
4446 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4447 					    const u8 *rates)
4448 {
4449 	int i;
4450 
4451 	if (!rates)
4452 		return;
4453 
4454 	for (i = 0; i < rates[1]; i++) {
4455 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4456 			params->ht_required = true;
4457 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4458 			params->vht_required = true;
4459 	}
4460 }
4461 
4462 /*
4463  * Since the nl80211 API didn't include, from the beginning, attributes about
4464  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4465  * benefit of drivers that rebuild IEs in the firmware.
4466  */
4467 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4468 {
4469 	const struct cfg80211_beacon_data *bcn = &params->beacon;
4470 	size_t ies_len = bcn->tail_len;
4471 	const u8 *ies = bcn->tail;
4472 	const u8 *rates;
4473 	const u8 *cap;
4474 
4475 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4476 	nl80211_check_ap_rate_selectors(params, rates);
4477 
4478 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4479 	nl80211_check_ap_rate_selectors(params, rates);
4480 
4481 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4482 	if (cap && cap[1] >= sizeof(*params->ht_cap))
4483 		params->ht_cap = (void *)(cap + 2);
4484 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4485 	if (cap && cap[1] >= sizeof(*params->vht_cap))
4486 		params->vht_cap = (void *)(cap + 2);
4487 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4488 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4489 		params->he_cap = (void *)(cap + 3);
4490 }
4491 
4492 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4493 				   struct cfg80211_ap_settings *params)
4494 {
4495 	struct wireless_dev *wdev;
4496 	bool ret = false;
4497 
4498 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4499 		if (wdev->iftype != NL80211_IFTYPE_AP &&
4500 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
4501 			continue;
4502 
4503 		if (!wdev->preset_chandef.chan)
4504 			continue;
4505 
4506 		params->chandef = wdev->preset_chandef;
4507 		ret = true;
4508 		break;
4509 	}
4510 
4511 	return ret;
4512 }
4513 
4514 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4515 				    enum nl80211_auth_type auth_type,
4516 				    enum nl80211_commands cmd)
4517 {
4518 	if (auth_type > NL80211_AUTHTYPE_MAX)
4519 		return false;
4520 
4521 	switch (cmd) {
4522 	case NL80211_CMD_AUTHENTICATE:
4523 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4524 		    auth_type == NL80211_AUTHTYPE_SAE)
4525 			return false;
4526 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
4527 					     NL80211_EXT_FEATURE_FILS_STA) &&
4528 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4529 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4530 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
4531 			return false;
4532 		return true;
4533 	case NL80211_CMD_CONNECT:
4534 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4535 		    !wiphy_ext_feature_isset(&rdev->wiphy,
4536 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4537 		    auth_type == NL80211_AUTHTYPE_SAE)
4538 			return false;
4539 
4540 		/* FILS with SK PFS or PK not supported yet */
4541 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4542 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4543 			return false;
4544 		if (!wiphy_ext_feature_isset(
4545 			    &rdev->wiphy,
4546 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4547 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
4548 			return false;
4549 		return true;
4550 	case NL80211_CMD_START_AP:
4551 		/* SAE not supported yet */
4552 		if (auth_type == NL80211_AUTHTYPE_SAE)
4553 			return false;
4554 		/* FILS not supported yet */
4555 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4556 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4557 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4558 			return false;
4559 		return true;
4560 	default:
4561 		return false;
4562 	}
4563 }
4564 
4565 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4566 {
4567 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4568 	struct net_device *dev = info->user_ptr[1];
4569 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4570 	struct cfg80211_ap_settings params;
4571 	int err;
4572 
4573 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4574 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4575 		return -EOPNOTSUPP;
4576 
4577 	if (!rdev->ops->start_ap)
4578 		return -EOPNOTSUPP;
4579 
4580 	if (wdev->beacon_interval)
4581 		return -EALREADY;
4582 
4583 	memset(&params, 0, sizeof(params));
4584 
4585 	/* these are required for START_AP */
4586 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4587 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4588 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
4589 		return -EINVAL;
4590 
4591 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4592 	if (err)
4593 		return err;
4594 
4595 	params.beacon_interval =
4596 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4597 	params.dtim_period =
4598 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4599 
4600 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4601 					   params.beacon_interval);
4602 	if (err)
4603 		return err;
4604 
4605 	/*
4606 	 * In theory, some of these attributes should be required here
4607 	 * but since they were not used when the command was originally
4608 	 * added, keep them optional for old user space programs to let
4609 	 * them continue to work with drivers that do not need the
4610 	 * additional information -- drivers must check!
4611 	 */
4612 	if (info->attrs[NL80211_ATTR_SSID]) {
4613 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4614 		params.ssid_len =
4615 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4616 		if (params.ssid_len == 0 ||
4617 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4618 			return -EINVAL;
4619 	}
4620 
4621 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4622 		params.hidden_ssid = nla_get_u32(
4623 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4624 
4625 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4626 
4627 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4628 		params.auth_type = nla_get_u32(
4629 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4630 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4631 					     NL80211_CMD_START_AP))
4632 			return -EINVAL;
4633 	} else
4634 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4635 
4636 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4637 				      NL80211_MAX_NR_CIPHER_SUITES);
4638 	if (err)
4639 		return err;
4640 
4641 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4642 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4643 			return -EOPNOTSUPP;
4644 		params.inactivity_timeout = nla_get_u16(
4645 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4646 	}
4647 
4648 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4649 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4650 			return -EINVAL;
4651 		params.p2p_ctwindow =
4652 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4653 		if (params.p2p_ctwindow != 0 &&
4654 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4655 			return -EINVAL;
4656 	}
4657 
4658 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4659 		u8 tmp;
4660 
4661 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4662 			return -EINVAL;
4663 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4664 		params.p2p_opp_ps = tmp;
4665 		if (params.p2p_opp_ps != 0 &&
4666 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4667 			return -EINVAL;
4668 	}
4669 
4670 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4671 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4672 		if (err)
4673 			return err;
4674 	} else if (wdev->preset_chandef.chan) {
4675 		params.chandef = wdev->preset_chandef;
4676 	} else if (!nl80211_get_ap_channel(rdev, &params))
4677 		return -EINVAL;
4678 
4679 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4680 					   wdev->iftype))
4681 		return -EINVAL;
4682 
4683 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4684 		err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4685 		if (err)
4686 			return err;
4687 
4688 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4689 					      &params.beacon_rate);
4690 		if (err)
4691 			return err;
4692 	}
4693 
4694 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4695 		params.smps_mode =
4696 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4697 		switch (params.smps_mode) {
4698 		case NL80211_SMPS_OFF:
4699 			break;
4700 		case NL80211_SMPS_STATIC:
4701 			if (!(rdev->wiphy.features &
4702 			      NL80211_FEATURE_STATIC_SMPS))
4703 				return -EINVAL;
4704 			break;
4705 		case NL80211_SMPS_DYNAMIC:
4706 			if (!(rdev->wiphy.features &
4707 			      NL80211_FEATURE_DYNAMIC_SMPS))
4708 				return -EINVAL;
4709 			break;
4710 		default:
4711 			return -EINVAL;
4712 		}
4713 	} else {
4714 		params.smps_mode = NL80211_SMPS_OFF;
4715 	}
4716 
4717 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4718 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4719 		return -EOPNOTSUPP;
4720 
4721 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4722 		params.acl = parse_acl_data(&rdev->wiphy, info);
4723 		if (IS_ERR(params.acl))
4724 			return PTR_ERR(params.acl);
4725 	}
4726 
4727 	params.twt_responder =
4728 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
4729 
4730 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
4731 		err = nl80211_parse_he_obss_pd(
4732 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
4733 					&params.he_obss_pd);
4734 		if (err)
4735 			return err;
4736 	}
4737 
4738 	nl80211_calculate_ap_params(&params);
4739 
4740 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
4741 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
4742 
4743 	wdev_lock(wdev);
4744 	err = rdev_start_ap(rdev, dev, &params);
4745 	if (!err) {
4746 		wdev->preset_chandef = params.chandef;
4747 		wdev->beacon_interval = params.beacon_interval;
4748 		wdev->chandef = params.chandef;
4749 		wdev->ssid_len = params.ssid_len;
4750 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4751 
4752 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4753 			wdev->conn_owner_nlportid = info->snd_portid;
4754 	}
4755 	wdev_unlock(wdev);
4756 
4757 	kfree(params.acl);
4758 
4759 	return err;
4760 }
4761 
4762 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4763 {
4764 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4765 	struct net_device *dev = info->user_ptr[1];
4766 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4767 	struct cfg80211_beacon_data params;
4768 	int err;
4769 
4770 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4771 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4772 		return -EOPNOTSUPP;
4773 
4774 	if (!rdev->ops->change_beacon)
4775 		return -EOPNOTSUPP;
4776 
4777 	if (!wdev->beacon_interval)
4778 		return -EINVAL;
4779 
4780 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
4781 	if (err)
4782 		return err;
4783 
4784 	wdev_lock(wdev);
4785 	err = rdev_change_beacon(rdev, dev, &params);
4786 	wdev_unlock(wdev);
4787 
4788 	return err;
4789 }
4790 
4791 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4792 {
4793 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4794 	struct net_device *dev = info->user_ptr[1];
4795 
4796 	return cfg80211_stop_ap(rdev, dev, false);
4797 }
4798 
4799 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4800 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4801 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4802 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4803 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4804 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4805 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4806 };
4807 
4808 static int parse_station_flags(struct genl_info *info,
4809 			       enum nl80211_iftype iftype,
4810 			       struct station_parameters *params)
4811 {
4812 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4813 	struct nlattr *nla;
4814 	int flag;
4815 
4816 	/*
4817 	 * Try parsing the new attribute first so userspace
4818 	 * can specify both for older kernels.
4819 	 */
4820 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4821 	if (nla) {
4822 		struct nl80211_sta_flag_update *sta_flags;
4823 
4824 		sta_flags = nla_data(nla);
4825 		params->sta_flags_mask = sta_flags->mask;
4826 		params->sta_flags_set = sta_flags->set;
4827 		params->sta_flags_set &= params->sta_flags_mask;
4828 		if ((params->sta_flags_mask |
4829 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4830 			return -EINVAL;
4831 		return 0;
4832 	}
4833 
4834 	/* if present, parse the old attribute */
4835 
4836 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4837 	if (!nla)
4838 		return 0;
4839 
4840 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
4841 		return -EINVAL;
4842 
4843 	/*
4844 	 * Only allow certain flags for interface types so that
4845 	 * other attributes are silently ignored. Remember that
4846 	 * this is backward compatibility code with old userspace
4847 	 * and shouldn't be hit in other cases anyway.
4848 	 */
4849 	switch (iftype) {
4850 	case NL80211_IFTYPE_AP:
4851 	case NL80211_IFTYPE_AP_VLAN:
4852 	case NL80211_IFTYPE_P2P_GO:
4853 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4854 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4855 					 BIT(NL80211_STA_FLAG_WME) |
4856 					 BIT(NL80211_STA_FLAG_MFP);
4857 		break;
4858 	case NL80211_IFTYPE_P2P_CLIENT:
4859 	case NL80211_IFTYPE_STATION:
4860 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4861 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
4862 		break;
4863 	case NL80211_IFTYPE_MESH_POINT:
4864 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4865 					 BIT(NL80211_STA_FLAG_MFP) |
4866 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
4867 		break;
4868 	default:
4869 		return -EINVAL;
4870 	}
4871 
4872 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4873 		if (flags[flag]) {
4874 			params->sta_flags_set |= (1<<flag);
4875 
4876 			/* no longer support new API additions in old API */
4877 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4878 				return -EINVAL;
4879 		}
4880 	}
4881 
4882 	return 0;
4883 }
4884 
4885 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
4886 {
4887 	struct nlattr *rate;
4888 	u32 bitrate;
4889 	u16 bitrate_compat;
4890 	enum nl80211_rate_info rate_flg;
4891 
4892 	rate = nla_nest_start_noflag(msg, attr);
4893 	if (!rate)
4894 		return false;
4895 
4896 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4897 	bitrate = cfg80211_calculate_bitrate(info);
4898 	/* report 16-bit bitrate only if we can */
4899 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4900 	if (bitrate > 0 &&
4901 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4902 		return false;
4903 	if (bitrate_compat > 0 &&
4904 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4905 		return false;
4906 
4907 	switch (info->bw) {
4908 	case RATE_INFO_BW_5:
4909 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4910 		break;
4911 	case RATE_INFO_BW_10:
4912 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4913 		break;
4914 	default:
4915 		WARN_ON(1);
4916 		/* fall through */
4917 	case RATE_INFO_BW_20:
4918 		rate_flg = 0;
4919 		break;
4920 	case RATE_INFO_BW_40:
4921 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4922 		break;
4923 	case RATE_INFO_BW_80:
4924 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4925 		break;
4926 	case RATE_INFO_BW_160:
4927 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4928 		break;
4929 	case RATE_INFO_BW_HE_RU:
4930 		rate_flg = 0;
4931 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
4932 	}
4933 
4934 	if (rate_flg && nla_put_flag(msg, rate_flg))
4935 		return false;
4936 
4937 	if (info->flags & RATE_INFO_FLAGS_MCS) {
4938 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4939 			return false;
4940 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4941 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4942 			return false;
4943 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4944 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4945 			return false;
4946 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4947 			return false;
4948 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4949 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4950 			return false;
4951 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
4952 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
4953 			return false;
4954 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
4955 			return false;
4956 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
4957 			return false;
4958 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
4959 			return false;
4960 		if (info->bw == RATE_INFO_BW_HE_RU &&
4961 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
4962 			       info->he_ru_alloc))
4963 			return false;
4964 	}
4965 
4966 	nla_nest_end(msg, rate);
4967 	return true;
4968 }
4969 
4970 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4971 			       int id)
4972 {
4973 	void *attr;
4974 	int i = 0;
4975 
4976 	if (!mask)
4977 		return true;
4978 
4979 	attr = nla_nest_start_noflag(msg, id);
4980 	if (!attr)
4981 		return false;
4982 
4983 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4984 		if (!(mask & BIT(i)))
4985 			continue;
4986 
4987 		if (nla_put_u8(msg, i, signal[i]))
4988 			return false;
4989 	}
4990 
4991 	nla_nest_end(msg, attr);
4992 
4993 	return true;
4994 }
4995 
4996 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4997 				u32 seq, int flags,
4998 				struct cfg80211_registered_device *rdev,
4999 				struct net_device *dev,
5000 				const u8 *mac_addr, struct station_info *sinfo)
5001 {
5002 	void *hdr;
5003 	struct nlattr *sinfoattr, *bss_param;
5004 
5005 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5006 	if (!hdr) {
5007 		cfg80211_sinfo_release_content(sinfo);
5008 		return -1;
5009 	}
5010 
5011 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5012 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5013 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5014 		goto nla_put_failure;
5015 
5016 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5017 	if (!sinfoattr)
5018 		goto nla_put_failure;
5019 
5020 #define PUT_SINFO(attr, memb, type) do {				\
5021 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5022 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5023 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5024 			     sinfo->memb))				\
5025 		goto nla_put_failure;					\
5026 	} while (0)
5027 #define PUT_SINFO_U64(attr, memb) do {					\
5028 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5029 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5030 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5031 		goto nla_put_failure;					\
5032 	} while (0)
5033 
5034 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5035 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5036 
5037 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5038 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5039 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5040 			(u32)sinfo->rx_bytes))
5041 		goto nla_put_failure;
5042 
5043 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5044 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5045 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5046 			(u32)sinfo->tx_bytes))
5047 		goto nla_put_failure;
5048 
5049 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5050 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5051 	PUT_SINFO(LLID, llid, u16);
5052 	PUT_SINFO(PLID, plid, u16);
5053 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5054 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5055 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5056 
5057 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5058 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5059 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5060 
5061 	switch (rdev->wiphy.signal_type) {
5062 	case CFG80211_SIGNAL_TYPE_MBM:
5063 		PUT_SINFO(SIGNAL, signal, u8);
5064 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5065 		break;
5066 	default:
5067 		break;
5068 	}
5069 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5070 		if (!nl80211_put_signal(msg, sinfo->chains,
5071 					sinfo->chain_signal,
5072 					NL80211_STA_INFO_CHAIN_SIGNAL))
5073 			goto nla_put_failure;
5074 	}
5075 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5076 		if (!nl80211_put_signal(msg, sinfo->chains,
5077 					sinfo->chain_signal_avg,
5078 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5079 			goto nla_put_failure;
5080 	}
5081 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5082 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5083 					  NL80211_STA_INFO_TX_BITRATE))
5084 			goto nla_put_failure;
5085 	}
5086 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5087 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5088 					  NL80211_STA_INFO_RX_BITRATE))
5089 			goto nla_put_failure;
5090 	}
5091 
5092 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5093 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5094 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5095 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5096 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5097 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5098 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5099 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5100 	PUT_SINFO(PEER_PM, peer_pm, u32);
5101 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5102 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5103 
5104 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5105 		bss_param = nla_nest_start_noflag(msg,
5106 						  NL80211_STA_INFO_BSS_PARAM);
5107 		if (!bss_param)
5108 			goto nla_put_failure;
5109 
5110 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5111 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5112 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5113 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5114 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5115 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5116 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5117 			       sinfo->bss_param.dtim_period) ||
5118 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5119 				sinfo->bss_param.beacon_interval))
5120 			goto nla_put_failure;
5121 
5122 		nla_nest_end(msg, bss_param);
5123 	}
5124 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5125 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5126 		    sizeof(struct nl80211_sta_flag_update),
5127 		    &sinfo->sta_flags))
5128 		goto nla_put_failure;
5129 
5130 	PUT_SINFO_U64(T_OFFSET, t_offset);
5131 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5132 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5133 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5134 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5135 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5136 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5137 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5138 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5139 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5140 	}
5141 
5142 #undef PUT_SINFO
5143 #undef PUT_SINFO_U64
5144 
5145 	if (sinfo->pertid) {
5146 		struct nlattr *tidsattr;
5147 		int tid;
5148 
5149 		tidsattr = nla_nest_start_noflag(msg,
5150 						 NL80211_STA_INFO_TID_STATS);
5151 		if (!tidsattr)
5152 			goto nla_put_failure;
5153 
5154 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5155 			struct cfg80211_tid_stats *tidstats;
5156 			struct nlattr *tidattr;
5157 
5158 			tidstats = &sinfo->pertid[tid];
5159 
5160 			if (!tidstats->filled)
5161 				continue;
5162 
5163 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5164 			if (!tidattr)
5165 				goto nla_put_failure;
5166 
5167 #define PUT_TIDVAL_U64(attr, memb) do {					\
5168 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5169 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5170 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5171 		goto nla_put_failure;					\
5172 	} while (0)
5173 
5174 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5175 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5176 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5177 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5178 
5179 #undef PUT_TIDVAL_U64
5180 			if ((tidstats->filled &
5181 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5182 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5183 						   NL80211_TID_STATS_TXQ_STATS))
5184 				goto nla_put_failure;
5185 
5186 			nla_nest_end(msg, tidattr);
5187 		}
5188 
5189 		nla_nest_end(msg, tidsattr);
5190 	}
5191 
5192 	nla_nest_end(msg, sinfoattr);
5193 
5194 	if (sinfo->assoc_req_ies_len &&
5195 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5196 		    sinfo->assoc_req_ies))
5197 		goto nla_put_failure;
5198 
5199 	cfg80211_sinfo_release_content(sinfo);
5200 	genlmsg_end(msg, hdr);
5201 	return 0;
5202 
5203  nla_put_failure:
5204 	cfg80211_sinfo_release_content(sinfo);
5205 	genlmsg_cancel(msg, hdr);
5206 	return -EMSGSIZE;
5207 }
5208 
5209 static int nl80211_dump_station(struct sk_buff *skb,
5210 				struct netlink_callback *cb)
5211 {
5212 	struct station_info sinfo;
5213 	struct cfg80211_registered_device *rdev;
5214 	struct wireless_dev *wdev;
5215 	u8 mac_addr[ETH_ALEN];
5216 	int sta_idx = cb->args[2];
5217 	int err;
5218 
5219 	rtnl_lock();
5220 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5221 	if (err)
5222 		goto out_err;
5223 
5224 	if (!wdev->netdev) {
5225 		err = -EINVAL;
5226 		goto out_err;
5227 	}
5228 
5229 	if (!rdev->ops->dump_station) {
5230 		err = -EOPNOTSUPP;
5231 		goto out_err;
5232 	}
5233 
5234 	while (1) {
5235 		memset(&sinfo, 0, sizeof(sinfo));
5236 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5237 					mac_addr, &sinfo);
5238 		if (err == -ENOENT)
5239 			break;
5240 		if (err)
5241 			goto out_err;
5242 
5243 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5244 				NETLINK_CB(cb->skb).portid,
5245 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
5246 				rdev, wdev->netdev, mac_addr,
5247 				&sinfo) < 0)
5248 			goto out;
5249 
5250 		sta_idx++;
5251 	}
5252 
5253  out:
5254 	cb->args[2] = sta_idx;
5255 	err = skb->len;
5256  out_err:
5257 	rtnl_unlock();
5258 
5259 	return err;
5260 }
5261 
5262 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5263 {
5264 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5265 	struct net_device *dev = info->user_ptr[1];
5266 	struct station_info sinfo;
5267 	struct sk_buff *msg;
5268 	u8 *mac_addr = NULL;
5269 	int err;
5270 
5271 	memset(&sinfo, 0, sizeof(sinfo));
5272 
5273 	if (!info->attrs[NL80211_ATTR_MAC])
5274 		return -EINVAL;
5275 
5276 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5277 
5278 	if (!rdev->ops->get_station)
5279 		return -EOPNOTSUPP;
5280 
5281 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5282 	if (err)
5283 		return err;
5284 
5285 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5286 	if (!msg) {
5287 		cfg80211_sinfo_release_content(&sinfo);
5288 		return -ENOMEM;
5289 	}
5290 
5291 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5292 				 info->snd_portid, info->snd_seq, 0,
5293 				 rdev, dev, mac_addr, &sinfo) < 0) {
5294 		nlmsg_free(msg);
5295 		return -ENOBUFS;
5296 	}
5297 
5298 	return genlmsg_reply(msg, info);
5299 }
5300 
5301 int cfg80211_check_station_change(struct wiphy *wiphy,
5302 				  struct station_parameters *params,
5303 				  enum cfg80211_station_type statype)
5304 {
5305 	if (params->listen_interval != -1 &&
5306 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5307 		return -EINVAL;
5308 
5309 	if (params->support_p2p_ps != -1 &&
5310 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5311 		return -EINVAL;
5312 
5313 	if (params->aid &&
5314 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5315 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5316 		return -EINVAL;
5317 
5318 	/* When you run into this, adjust the code below for the new flag */
5319 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5320 
5321 	switch (statype) {
5322 	case CFG80211_STA_MESH_PEER_KERNEL:
5323 	case CFG80211_STA_MESH_PEER_USER:
5324 		/*
5325 		 * No ignoring the TDLS flag here -- the userspace mesh
5326 		 * code doesn't have the bug of including TDLS in the
5327 		 * mask everywhere.
5328 		 */
5329 		if (params->sta_flags_mask &
5330 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5331 				  BIT(NL80211_STA_FLAG_MFP) |
5332 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5333 			return -EINVAL;
5334 		break;
5335 	case CFG80211_STA_TDLS_PEER_SETUP:
5336 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5337 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5338 			return -EINVAL;
5339 		/* ignore since it can't change */
5340 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5341 		break;
5342 	default:
5343 		/* disallow mesh-specific things */
5344 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5345 			return -EINVAL;
5346 		if (params->local_pm)
5347 			return -EINVAL;
5348 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5349 			return -EINVAL;
5350 	}
5351 
5352 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5353 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5354 		/* TDLS can't be set, ... */
5355 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5356 			return -EINVAL;
5357 		/*
5358 		 * ... but don't bother the driver with it. This works around
5359 		 * a hostapd/wpa_supplicant issue -- it always includes the
5360 		 * TLDS_PEER flag in the mask even for AP mode.
5361 		 */
5362 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5363 	}
5364 
5365 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5366 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5367 		/* reject other things that can't change */
5368 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5369 			return -EINVAL;
5370 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5371 			return -EINVAL;
5372 		if (params->supported_rates)
5373 			return -EINVAL;
5374 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
5375 		    params->he_capa)
5376 			return -EINVAL;
5377 	}
5378 
5379 	if (statype != CFG80211_STA_AP_CLIENT &&
5380 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5381 		if (params->vlan)
5382 			return -EINVAL;
5383 	}
5384 
5385 	switch (statype) {
5386 	case CFG80211_STA_AP_MLME_CLIENT:
5387 		/* Use this only for authorizing/unauthorizing a station */
5388 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5389 			return -EOPNOTSUPP;
5390 		break;
5391 	case CFG80211_STA_AP_CLIENT:
5392 	case CFG80211_STA_AP_CLIENT_UNASSOC:
5393 		/* accept only the listed bits */
5394 		if (params->sta_flags_mask &
5395 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5396 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5397 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
5398 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5399 				  BIT(NL80211_STA_FLAG_WME) |
5400 				  BIT(NL80211_STA_FLAG_MFP)))
5401 			return -EINVAL;
5402 
5403 		/* but authenticated/associated only if driver handles it */
5404 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5405 		    params->sta_flags_mask &
5406 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5407 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5408 			return -EINVAL;
5409 		break;
5410 	case CFG80211_STA_IBSS:
5411 	case CFG80211_STA_AP_STA:
5412 		/* reject any changes other than AUTHORIZED */
5413 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5414 			return -EINVAL;
5415 		break;
5416 	case CFG80211_STA_TDLS_PEER_SETUP:
5417 		/* reject any changes other than AUTHORIZED or WME */
5418 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5419 					       BIT(NL80211_STA_FLAG_WME)))
5420 			return -EINVAL;
5421 		/* force (at least) rates when authorizing */
5422 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5423 		    !params->supported_rates)
5424 			return -EINVAL;
5425 		break;
5426 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5427 		/* reject any changes */
5428 		return -EINVAL;
5429 	case CFG80211_STA_MESH_PEER_KERNEL:
5430 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5431 			return -EINVAL;
5432 		break;
5433 	case CFG80211_STA_MESH_PEER_USER:
5434 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5435 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5436 			return -EINVAL;
5437 		break;
5438 	}
5439 
5440 	/*
5441 	 * Older kernel versions ignored this attribute entirely, so don't
5442 	 * reject attempts to update it but mark it as unused instead so the
5443 	 * driver won't look at the data.
5444 	 */
5445 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5446 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
5447 		params->opmode_notif_used = false;
5448 
5449 	return 0;
5450 }
5451 EXPORT_SYMBOL(cfg80211_check_station_change);
5452 
5453 /*
5454  * Get vlan interface making sure it is running and on the right wiphy.
5455  */
5456 static struct net_device *get_vlan(struct genl_info *info,
5457 				   struct cfg80211_registered_device *rdev)
5458 {
5459 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5460 	struct net_device *v;
5461 	int ret;
5462 
5463 	if (!vlanattr)
5464 		return NULL;
5465 
5466 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5467 	if (!v)
5468 		return ERR_PTR(-ENODEV);
5469 
5470 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5471 		ret = -EINVAL;
5472 		goto error;
5473 	}
5474 
5475 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5476 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5477 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5478 		ret = -EINVAL;
5479 		goto error;
5480 	}
5481 
5482 	if (!netif_running(v)) {
5483 		ret = -ENETDOWN;
5484 		goto error;
5485 	}
5486 
5487 	return v;
5488  error:
5489 	dev_put(v);
5490 	return ERR_PTR(ret);
5491 }
5492 
5493 static const struct nla_policy
5494 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5495 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5496 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5497 };
5498 
5499 static int nl80211_parse_sta_wme(struct genl_info *info,
5500 				 struct station_parameters *params)
5501 {
5502 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5503 	struct nlattr *nla;
5504 	int err;
5505 
5506 	/* parse WME attributes if present */
5507 	if (!info->attrs[NL80211_ATTR_STA_WME])
5508 		return 0;
5509 
5510 	nla = info->attrs[NL80211_ATTR_STA_WME];
5511 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5512 					  nl80211_sta_wme_policy,
5513 					  info->extack);
5514 	if (err)
5515 		return err;
5516 
5517 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5518 		params->uapsd_queues = nla_get_u8(
5519 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
5520 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5521 		return -EINVAL;
5522 
5523 	if (tb[NL80211_STA_WME_MAX_SP])
5524 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5525 
5526 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5527 		return -EINVAL;
5528 
5529 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5530 
5531 	return 0;
5532 }
5533 
5534 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5535 				      struct station_parameters *params)
5536 {
5537 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5538 		params->supported_channels =
5539 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5540 		params->supported_channels_len =
5541 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5542 		/*
5543 		 * Need to include at least one (first channel, number of
5544 		 * channels) tuple for each subband, and must have proper
5545 		 * tuples for the rest of the data as well.
5546 		 */
5547 		if (params->supported_channels_len < 2)
5548 			return -EINVAL;
5549 		if (params->supported_channels_len % 2)
5550 			return -EINVAL;
5551 	}
5552 
5553 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5554 		params->supported_oper_classes =
5555 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5556 		params->supported_oper_classes_len =
5557 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5558 		/*
5559 		 * The value of the Length field of the Supported Operating
5560 		 * Classes element is between 2 and 253.
5561 		 */
5562 		if (params->supported_oper_classes_len < 2 ||
5563 		    params->supported_oper_classes_len > 253)
5564 			return -EINVAL;
5565 	}
5566 	return 0;
5567 }
5568 
5569 static int nl80211_set_station_tdls(struct genl_info *info,
5570 				    struct station_parameters *params)
5571 {
5572 	int err;
5573 	/* Dummy STA entry gets updated once the peer capabilities are known */
5574 	if (info->attrs[NL80211_ATTR_PEER_AID])
5575 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5576 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5577 		params->ht_capa =
5578 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5579 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5580 		params->vht_capa =
5581 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5582 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5583 		params->he_capa =
5584 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5585 		params->he_capa_len =
5586 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5587 
5588 		if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5589 			return -EINVAL;
5590 	}
5591 
5592 	err = nl80211_parse_sta_channel_info(info, params);
5593 	if (err)
5594 		return err;
5595 
5596 	return nl80211_parse_sta_wme(info, params);
5597 }
5598 
5599 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5600 					     struct station_parameters *params)
5601 {
5602 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5603 	int idx;
5604 
5605 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5606 		if (!rdev->ops->set_tx_power ||
5607 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5608 					 NL80211_EXT_FEATURE_STA_TX_PWR))
5609 			return -EOPNOTSUPP;
5610 
5611 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5612 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
5613 
5614 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5615 			idx = NL80211_ATTR_STA_TX_POWER;
5616 
5617 			if (info->attrs[idx])
5618 				params->txpwr.power =
5619 					nla_get_s16(info->attrs[idx]);
5620 			else
5621 				return -EINVAL;
5622 		}
5623 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5624 	}
5625 
5626 	return 0;
5627 }
5628 
5629 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5630 {
5631 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5632 	struct net_device *dev = info->user_ptr[1];
5633 	struct station_parameters params;
5634 	u8 *mac_addr;
5635 	int err;
5636 
5637 	memset(&params, 0, sizeof(params));
5638 
5639 	if (!rdev->ops->change_station)
5640 		return -EOPNOTSUPP;
5641 
5642 	/*
5643 	 * AID and listen_interval properties can be set only for unassociated
5644 	 * station. Include these parameters here and will check them in
5645 	 * cfg80211_check_station_change().
5646 	 */
5647 	if (info->attrs[NL80211_ATTR_STA_AID])
5648 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5649 
5650 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5651 		params.listen_interval =
5652 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5653 	else
5654 		params.listen_interval = -1;
5655 
5656 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5657 		params.support_p2p_ps =
5658 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5659 	else
5660 		params.support_p2p_ps = -1;
5661 
5662 	if (!info->attrs[NL80211_ATTR_MAC])
5663 		return -EINVAL;
5664 
5665 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5666 
5667 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5668 		params.supported_rates =
5669 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5670 		params.supported_rates_len =
5671 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5672 	}
5673 
5674 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5675 		params.capability =
5676 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5677 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5678 	}
5679 
5680 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5681 		params.ext_capab =
5682 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5683 		params.ext_capab_len =
5684 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5685 	}
5686 
5687 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5688 		return -EINVAL;
5689 
5690 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5691 		params.plink_action =
5692 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5693 
5694 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5695 		params.plink_state =
5696 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5697 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5698 			params.peer_aid = nla_get_u16(
5699 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5700 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5701 	}
5702 
5703 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
5704 		params.local_pm = nla_get_u32(
5705 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5706 
5707 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5708 		params.opmode_notif_used = true;
5709 		params.opmode_notif =
5710 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5711 	}
5712 
5713 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5714 		params.airtime_weight =
5715 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5716 
5717 	if (params.airtime_weight &&
5718 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5719 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5720 		return -EOPNOTSUPP;
5721 
5722 	err = nl80211_parse_sta_txpower_setting(info, &params);
5723 	if (err)
5724 		return err;
5725 
5726 	/* Include parameters for TDLS peer (will check later) */
5727 	err = nl80211_set_station_tdls(info, &params);
5728 	if (err)
5729 		return err;
5730 
5731 	params.vlan = get_vlan(info, rdev);
5732 	if (IS_ERR(params.vlan))
5733 		return PTR_ERR(params.vlan);
5734 
5735 	switch (dev->ieee80211_ptr->iftype) {
5736 	case NL80211_IFTYPE_AP:
5737 	case NL80211_IFTYPE_AP_VLAN:
5738 	case NL80211_IFTYPE_P2P_GO:
5739 	case NL80211_IFTYPE_P2P_CLIENT:
5740 	case NL80211_IFTYPE_STATION:
5741 	case NL80211_IFTYPE_ADHOC:
5742 	case NL80211_IFTYPE_MESH_POINT:
5743 		break;
5744 	default:
5745 		err = -EOPNOTSUPP;
5746 		goto out_put_vlan;
5747 	}
5748 
5749 	/* driver will call cfg80211_check_station_change() */
5750 	err = rdev_change_station(rdev, dev, mac_addr, &params);
5751 
5752  out_put_vlan:
5753 	if (params.vlan)
5754 		dev_put(params.vlan);
5755 
5756 	return err;
5757 }
5758 
5759 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5760 {
5761 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5762 	int err;
5763 	struct net_device *dev = info->user_ptr[1];
5764 	struct station_parameters params;
5765 	u8 *mac_addr = NULL;
5766 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5767 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
5768 
5769 	memset(&params, 0, sizeof(params));
5770 
5771 	if (!rdev->ops->add_station)
5772 		return -EOPNOTSUPP;
5773 
5774 	if (!info->attrs[NL80211_ATTR_MAC])
5775 		return -EINVAL;
5776 
5777 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5778 		return -EINVAL;
5779 
5780 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5781 		return -EINVAL;
5782 
5783 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
5784 	    !info->attrs[NL80211_ATTR_PEER_AID])
5785 		return -EINVAL;
5786 
5787 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5788 	params.supported_rates =
5789 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5790 	params.supported_rates_len =
5791 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5792 	params.listen_interval =
5793 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5794 
5795 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5796 		params.support_p2p_ps =
5797 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5798 	} else {
5799 		/*
5800 		 * if not specified, assume it's supported for P2P GO interface,
5801 		 * and is NOT supported for AP interface
5802 		 */
5803 		params.support_p2p_ps =
5804 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5805 	}
5806 
5807 	if (info->attrs[NL80211_ATTR_PEER_AID])
5808 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5809 	else
5810 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5811 
5812 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5813 		params.capability =
5814 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5815 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5816 	}
5817 
5818 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5819 		params.ext_capab =
5820 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5821 		params.ext_capab_len =
5822 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5823 	}
5824 
5825 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5826 		params.ht_capa =
5827 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5828 
5829 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5830 		params.vht_capa =
5831 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5832 
5833 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5834 		params.he_capa =
5835 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5836 		params.he_capa_len =
5837 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5838 
5839 		/* max len is validated in nla policy */
5840 		if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5841 			return -EINVAL;
5842 	}
5843 
5844 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5845 		params.opmode_notif_used = true;
5846 		params.opmode_notif =
5847 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5848 	}
5849 
5850 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5851 		params.plink_action =
5852 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5853 
5854 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5855 		params.airtime_weight =
5856 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5857 
5858 	if (params.airtime_weight &&
5859 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5860 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5861 		return -EOPNOTSUPP;
5862 
5863 	err = nl80211_parse_sta_txpower_setting(info, &params);
5864 	if (err)
5865 		return err;
5866 
5867 	err = nl80211_parse_sta_channel_info(info, &params);
5868 	if (err)
5869 		return err;
5870 
5871 	err = nl80211_parse_sta_wme(info, &params);
5872 	if (err)
5873 		return err;
5874 
5875 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5876 		return -EINVAL;
5877 
5878 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5879 	 * as userspace might just pass through the capabilities from the IEs
5880 	 * directly, rather than enforcing this restriction and returning an
5881 	 * error in this case.
5882 	 */
5883 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5884 		params.ht_capa = NULL;
5885 		params.vht_capa = NULL;
5886 
5887 		/* HE requires WME */
5888 		if (params.he_capa_len)
5889 			return -EINVAL;
5890 	}
5891 
5892 	/* When you run into this, adjust the code below for the new flag */
5893 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5894 
5895 	switch (dev->ieee80211_ptr->iftype) {
5896 	case NL80211_IFTYPE_AP:
5897 	case NL80211_IFTYPE_AP_VLAN:
5898 	case NL80211_IFTYPE_P2P_GO:
5899 		/* ignore WME attributes if iface/sta is not capable */
5900 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5901 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5902 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5903 
5904 		/* TDLS peers cannot be added */
5905 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5906 		    info->attrs[NL80211_ATTR_PEER_AID])
5907 			return -EINVAL;
5908 		/* but don't bother the driver with it */
5909 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5910 
5911 		/* allow authenticated/associated only if driver handles it */
5912 		if (!(rdev->wiphy.features &
5913 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5914 		    params.sta_flags_mask & auth_assoc)
5915 			return -EINVAL;
5916 
5917 		/* Older userspace, or userspace wanting to be compatible with
5918 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5919 		 * and assoc flags in the mask, but assumes the station will be
5920 		 * added as associated anyway since this was the required driver
5921 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5922 		 * introduced.
5923 		 * In order to not bother drivers with this quirk in the API
5924 		 * set the flags in both the mask and set for new stations in
5925 		 * this case.
5926 		 */
5927 		if (!(params.sta_flags_mask & auth_assoc)) {
5928 			params.sta_flags_mask |= auth_assoc;
5929 			params.sta_flags_set |= auth_assoc;
5930 		}
5931 
5932 		/* must be last in here for error handling */
5933 		params.vlan = get_vlan(info, rdev);
5934 		if (IS_ERR(params.vlan))
5935 			return PTR_ERR(params.vlan);
5936 		break;
5937 	case NL80211_IFTYPE_MESH_POINT:
5938 		/* ignore uAPSD data */
5939 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5940 
5941 		/* associated is disallowed */
5942 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5943 			return -EINVAL;
5944 		/* TDLS peers cannot be added */
5945 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5946 		    info->attrs[NL80211_ATTR_PEER_AID])
5947 			return -EINVAL;
5948 		break;
5949 	case NL80211_IFTYPE_STATION:
5950 	case NL80211_IFTYPE_P2P_CLIENT:
5951 		/* ignore uAPSD data */
5952 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5953 
5954 		/* these are disallowed */
5955 		if (params.sta_flags_mask &
5956 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5957 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
5958 			return -EINVAL;
5959 		/* Only TDLS peers can be added */
5960 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5961 			return -EINVAL;
5962 		/* Can only add if TDLS ... */
5963 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5964 			return -EOPNOTSUPP;
5965 		/* ... with external setup is supported */
5966 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5967 			return -EOPNOTSUPP;
5968 		/*
5969 		 * Older wpa_supplicant versions always mark the TDLS peer
5970 		 * as authorized, but it shouldn't yet be.
5971 		 */
5972 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
5973 		break;
5974 	default:
5975 		return -EOPNOTSUPP;
5976 	}
5977 
5978 	/* be aware of params.vlan when changing code here */
5979 
5980 	err = rdev_add_station(rdev, dev, mac_addr, &params);
5981 
5982 	if (params.vlan)
5983 		dev_put(params.vlan);
5984 	return err;
5985 }
5986 
5987 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5988 {
5989 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5990 	struct net_device *dev = info->user_ptr[1];
5991 	struct station_del_parameters params;
5992 
5993 	memset(&params, 0, sizeof(params));
5994 
5995 	if (info->attrs[NL80211_ATTR_MAC])
5996 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
5997 
5998 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5999 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6000 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
6001 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6002 		return -EINVAL;
6003 
6004 	if (!rdev->ops->del_station)
6005 		return -EOPNOTSUPP;
6006 
6007 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6008 		params.subtype =
6009 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6010 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6011 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6012 			return -EINVAL;
6013 	} else {
6014 		/* Default to Deauthentication frame */
6015 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6016 	}
6017 
6018 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6019 		params.reason_code =
6020 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6021 		if (params.reason_code == 0)
6022 			return -EINVAL; /* 0 is reserved */
6023 	} else {
6024 		/* Default to reason code 2 */
6025 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6026 	}
6027 
6028 	return rdev_del_station(rdev, dev, &params);
6029 }
6030 
6031 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6032 				int flags, struct net_device *dev,
6033 				u8 *dst, u8 *next_hop,
6034 				struct mpath_info *pinfo)
6035 {
6036 	void *hdr;
6037 	struct nlattr *pinfoattr;
6038 
6039 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6040 	if (!hdr)
6041 		return -1;
6042 
6043 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6044 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6045 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6046 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6047 		goto nla_put_failure;
6048 
6049 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6050 	if (!pinfoattr)
6051 		goto nla_put_failure;
6052 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6053 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6054 			pinfo->frame_qlen))
6055 		goto nla_put_failure;
6056 	if (((pinfo->filled & MPATH_INFO_SN) &&
6057 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6058 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6059 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6060 			 pinfo->metric)) ||
6061 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6062 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6063 			 pinfo->exptime)) ||
6064 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6065 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6066 			pinfo->flags)) ||
6067 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6068 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6069 			 pinfo->discovery_timeout)) ||
6070 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6071 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6072 			pinfo->discovery_retries)) ||
6073 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6074 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6075 			pinfo->hop_count)) ||
6076 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6077 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6078 			 pinfo->path_change_count)))
6079 		goto nla_put_failure;
6080 
6081 	nla_nest_end(msg, pinfoattr);
6082 
6083 	genlmsg_end(msg, hdr);
6084 	return 0;
6085 
6086  nla_put_failure:
6087 	genlmsg_cancel(msg, hdr);
6088 	return -EMSGSIZE;
6089 }
6090 
6091 static int nl80211_dump_mpath(struct sk_buff *skb,
6092 			      struct netlink_callback *cb)
6093 {
6094 	struct mpath_info pinfo;
6095 	struct cfg80211_registered_device *rdev;
6096 	struct wireless_dev *wdev;
6097 	u8 dst[ETH_ALEN];
6098 	u8 next_hop[ETH_ALEN];
6099 	int path_idx = cb->args[2];
6100 	int err;
6101 
6102 	rtnl_lock();
6103 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6104 	if (err)
6105 		goto out_err;
6106 
6107 	if (!rdev->ops->dump_mpath) {
6108 		err = -EOPNOTSUPP;
6109 		goto out_err;
6110 	}
6111 
6112 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6113 		err = -EOPNOTSUPP;
6114 		goto out_err;
6115 	}
6116 
6117 	while (1) {
6118 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6119 				      next_hop, &pinfo);
6120 		if (err == -ENOENT)
6121 			break;
6122 		if (err)
6123 			goto out_err;
6124 
6125 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6126 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6127 				       wdev->netdev, dst, next_hop,
6128 				       &pinfo) < 0)
6129 			goto out;
6130 
6131 		path_idx++;
6132 	}
6133 
6134  out:
6135 	cb->args[2] = path_idx;
6136 	err = skb->len;
6137  out_err:
6138 	rtnl_unlock();
6139 	return err;
6140 }
6141 
6142 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6143 {
6144 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6145 	int err;
6146 	struct net_device *dev = info->user_ptr[1];
6147 	struct mpath_info pinfo;
6148 	struct sk_buff *msg;
6149 	u8 *dst = NULL;
6150 	u8 next_hop[ETH_ALEN];
6151 
6152 	memset(&pinfo, 0, sizeof(pinfo));
6153 
6154 	if (!info->attrs[NL80211_ATTR_MAC])
6155 		return -EINVAL;
6156 
6157 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6158 
6159 	if (!rdev->ops->get_mpath)
6160 		return -EOPNOTSUPP;
6161 
6162 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6163 		return -EOPNOTSUPP;
6164 
6165 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6166 	if (err)
6167 		return err;
6168 
6169 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6170 	if (!msg)
6171 		return -ENOMEM;
6172 
6173 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6174 				 dev, dst, next_hop, &pinfo) < 0) {
6175 		nlmsg_free(msg);
6176 		return -ENOBUFS;
6177 	}
6178 
6179 	return genlmsg_reply(msg, info);
6180 }
6181 
6182 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6183 {
6184 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6185 	struct net_device *dev = info->user_ptr[1];
6186 	u8 *dst = NULL;
6187 	u8 *next_hop = NULL;
6188 
6189 	if (!info->attrs[NL80211_ATTR_MAC])
6190 		return -EINVAL;
6191 
6192 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6193 		return -EINVAL;
6194 
6195 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6196 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6197 
6198 	if (!rdev->ops->change_mpath)
6199 		return -EOPNOTSUPP;
6200 
6201 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6202 		return -EOPNOTSUPP;
6203 
6204 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6205 }
6206 
6207 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6208 {
6209 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6210 	struct net_device *dev = info->user_ptr[1];
6211 	u8 *dst = NULL;
6212 	u8 *next_hop = NULL;
6213 
6214 	if (!info->attrs[NL80211_ATTR_MAC])
6215 		return -EINVAL;
6216 
6217 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6218 		return -EINVAL;
6219 
6220 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6221 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6222 
6223 	if (!rdev->ops->add_mpath)
6224 		return -EOPNOTSUPP;
6225 
6226 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6227 		return -EOPNOTSUPP;
6228 
6229 	return rdev_add_mpath(rdev, dev, dst, next_hop);
6230 }
6231 
6232 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6233 {
6234 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6235 	struct net_device *dev = info->user_ptr[1];
6236 	u8 *dst = NULL;
6237 
6238 	if (info->attrs[NL80211_ATTR_MAC])
6239 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6240 
6241 	if (!rdev->ops->del_mpath)
6242 		return -EOPNOTSUPP;
6243 
6244 	return rdev_del_mpath(rdev, dev, dst);
6245 }
6246 
6247 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6248 {
6249 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6250 	int err;
6251 	struct net_device *dev = info->user_ptr[1];
6252 	struct mpath_info pinfo;
6253 	struct sk_buff *msg;
6254 	u8 *dst = NULL;
6255 	u8 mpp[ETH_ALEN];
6256 
6257 	memset(&pinfo, 0, sizeof(pinfo));
6258 
6259 	if (!info->attrs[NL80211_ATTR_MAC])
6260 		return -EINVAL;
6261 
6262 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6263 
6264 	if (!rdev->ops->get_mpp)
6265 		return -EOPNOTSUPP;
6266 
6267 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6268 		return -EOPNOTSUPP;
6269 
6270 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6271 	if (err)
6272 		return err;
6273 
6274 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6275 	if (!msg)
6276 		return -ENOMEM;
6277 
6278 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6279 			       dev, dst, mpp, &pinfo) < 0) {
6280 		nlmsg_free(msg);
6281 		return -ENOBUFS;
6282 	}
6283 
6284 	return genlmsg_reply(msg, info);
6285 }
6286 
6287 static int nl80211_dump_mpp(struct sk_buff *skb,
6288 			    struct netlink_callback *cb)
6289 {
6290 	struct mpath_info pinfo;
6291 	struct cfg80211_registered_device *rdev;
6292 	struct wireless_dev *wdev;
6293 	u8 dst[ETH_ALEN];
6294 	u8 mpp[ETH_ALEN];
6295 	int path_idx = cb->args[2];
6296 	int err;
6297 
6298 	rtnl_lock();
6299 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6300 	if (err)
6301 		goto out_err;
6302 
6303 	if (!rdev->ops->dump_mpp) {
6304 		err = -EOPNOTSUPP;
6305 		goto out_err;
6306 	}
6307 
6308 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6309 		err = -EOPNOTSUPP;
6310 		goto out_err;
6311 	}
6312 
6313 	while (1) {
6314 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6315 				    mpp, &pinfo);
6316 		if (err == -ENOENT)
6317 			break;
6318 		if (err)
6319 			goto out_err;
6320 
6321 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6322 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6323 				       wdev->netdev, dst, mpp,
6324 				       &pinfo) < 0)
6325 			goto out;
6326 
6327 		path_idx++;
6328 	}
6329 
6330  out:
6331 	cb->args[2] = path_idx;
6332 	err = skb->len;
6333  out_err:
6334 	rtnl_unlock();
6335 	return err;
6336 }
6337 
6338 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6339 {
6340 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6341 	struct net_device *dev = info->user_ptr[1];
6342 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6343 	struct bss_parameters params;
6344 	int err;
6345 
6346 	memset(&params, 0, sizeof(params));
6347 	/* default to not changing parameters */
6348 	params.use_cts_prot = -1;
6349 	params.use_short_preamble = -1;
6350 	params.use_short_slot_time = -1;
6351 	params.ap_isolate = -1;
6352 	params.ht_opmode = -1;
6353 	params.p2p_ctwindow = -1;
6354 	params.p2p_opp_ps = -1;
6355 
6356 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6357 		params.use_cts_prot =
6358 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6359 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6360 		params.use_short_preamble =
6361 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6362 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6363 		params.use_short_slot_time =
6364 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6365 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6366 		params.basic_rates =
6367 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6368 		params.basic_rates_len =
6369 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6370 	}
6371 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6372 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6373 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6374 		params.ht_opmode =
6375 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6376 
6377 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6378 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6379 			return -EINVAL;
6380 		params.p2p_ctwindow =
6381 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6382 		if (params.p2p_ctwindow != 0 &&
6383 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6384 			return -EINVAL;
6385 	}
6386 
6387 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6388 		u8 tmp;
6389 
6390 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6391 			return -EINVAL;
6392 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6393 		params.p2p_opp_ps = tmp;
6394 		if (params.p2p_opp_ps &&
6395 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6396 			return -EINVAL;
6397 	}
6398 
6399 	if (!rdev->ops->change_bss)
6400 		return -EOPNOTSUPP;
6401 
6402 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6403 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6404 		return -EOPNOTSUPP;
6405 
6406 	wdev_lock(wdev);
6407 	err = rdev_change_bss(rdev, dev, &params);
6408 	wdev_unlock(wdev);
6409 
6410 	return err;
6411 }
6412 
6413 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6414 {
6415 	char *data = NULL;
6416 	bool is_indoor;
6417 	enum nl80211_user_reg_hint_type user_reg_hint_type;
6418 	u32 owner_nlportid;
6419 
6420 	/*
6421 	 * You should only get this when cfg80211 hasn't yet initialized
6422 	 * completely when built-in to the kernel right between the time
6423 	 * window between nl80211_init() and regulatory_init(), if that is
6424 	 * even possible.
6425 	 */
6426 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6427 		return -EINPROGRESS;
6428 
6429 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6430 		user_reg_hint_type =
6431 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6432 	else
6433 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6434 
6435 	switch (user_reg_hint_type) {
6436 	case NL80211_USER_REG_HINT_USER:
6437 	case NL80211_USER_REG_HINT_CELL_BASE:
6438 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6439 			return -EINVAL;
6440 
6441 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6442 		return regulatory_hint_user(data, user_reg_hint_type);
6443 	case NL80211_USER_REG_HINT_INDOOR:
6444 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6445 			owner_nlportid = info->snd_portid;
6446 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6447 		} else {
6448 			owner_nlportid = 0;
6449 			is_indoor = true;
6450 		}
6451 
6452 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
6453 	default:
6454 		return -EINVAL;
6455 	}
6456 }
6457 
6458 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6459 {
6460 	return reg_reload_regdb();
6461 }
6462 
6463 static int nl80211_get_mesh_config(struct sk_buff *skb,
6464 				   struct genl_info *info)
6465 {
6466 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6467 	struct net_device *dev = info->user_ptr[1];
6468 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6469 	struct mesh_config cur_params;
6470 	int err = 0;
6471 	void *hdr;
6472 	struct nlattr *pinfoattr;
6473 	struct sk_buff *msg;
6474 
6475 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6476 		return -EOPNOTSUPP;
6477 
6478 	if (!rdev->ops->get_mesh_config)
6479 		return -EOPNOTSUPP;
6480 
6481 	wdev_lock(wdev);
6482 	/* If not connected, get default parameters */
6483 	if (!wdev->mesh_id_len)
6484 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6485 	else
6486 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
6487 	wdev_unlock(wdev);
6488 
6489 	if (err)
6490 		return err;
6491 
6492 	/* Draw up a netlink message to send back */
6493 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6494 	if (!msg)
6495 		return -ENOMEM;
6496 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6497 			     NL80211_CMD_GET_MESH_CONFIG);
6498 	if (!hdr)
6499 		goto out;
6500 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6501 	if (!pinfoattr)
6502 		goto nla_put_failure;
6503 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6504 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6505 			cur_params.dot11MeshRetryTimeout) ||
6506 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6507 			cur_params.dot11MeshConfirmTimeout) ||
6508 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6509 			cur_params.dot11MeshHoldingTimeout) ||
6510 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6511 			cur_params.dot11MeshMaxPeerLinks) ||
6512 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6513 		       cur_params.dot11MeshMaxRetries) ||
6514 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
6515 		       cur_params.dot11MeshTTL) ||
6516 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6517 		       cur_params.element_ttl) ||
6518 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6519 		       cur_params.auto_open_plinks) ||
6520 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6521 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6522 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6523 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
6524 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6525 			cur_params.path_refresh_time) ||
6526 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6527 			cur_params.min_discovery_timeout) ||
6528 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6529 			cur_params.dot11MeshHWMPactivePathTimeout) ||
6530 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6531 			cur_params.dot11MeshHWMPpreqMinInterval) ||
6532 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6533 			cur_params.dot11MeshHWMPperrMinInterval) ||
6534 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6535 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6536 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6537 		       cur_params.dot11MeshHWMPRootMode) ||
6538 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6539 			cur_params.dot11MeshHWMPRannInterval) ||
6540 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6541 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
6542 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6543 		       cur_params.dot11MeshForwarding) ||
6544 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6545 			cur_params.rssi_threshold) ||
6546 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6547 			cur_params.ht_opmode) ||
6548 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6549 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6550 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6551 			cur_params.dot11MeshHWMProotInterval) ||
6552 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6553 			cur_params.dot11MeshHWMPconfirmationInterval) ||
6554 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6555 			cur_params.power_mode) ||
6556 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6557 			cur_params.dot11MeshAwakeWindowDuration) ||
6558 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6559 			cur_params.plink_timeout) ||
6560 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6561 		       cur_params.dot11MeshConnectedToMeshGate))
6562 		goto nla_put_failure;
6563 	nla_nest_end(msg, pinfoattr);
6564 	genlmsg_end(msg, hdr);
6565 	return genlmsg_reply(msg, info);
6566 
6567  nla_put_failure:
6568  out:
6569 	nlmsg_free(msg);
6570 	return -ENOBUFS;
6571 }
6572 
6573 static const struct nla_policy
6574 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6575 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
6576 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6577 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6578 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6579 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
6580 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6581 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
6582 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
6583 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6584 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6585 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6586 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6587 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6588 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
6589 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6590 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6591 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6592 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6593 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6594 		NLA_POLICY_MIN(NLA_U16, 1),
6595 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6596 		NLA_POLICY_MIN(NLA_U16, 1),
6597 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6598 		NLA_POLICY_MIN(NLA_U16, 1),
6599 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6600 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6601 		NLA_POLICY_MIN(NLA_U16, 1),
6602 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6603 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6604 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
6605 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
6606 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6607 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6608 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6609 		NLA_POLICY_MIN(NLA_U16, 1),
6610 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6611 		NLA_POLICY_MIN(NLA_U16, 1),
6612 	[NL80211_MESHCONF_POWER_MODE] =
6613 		NLA_POLICY_RANGE(NLA_U32,
6614 				 NL80211_MESH_POWER_ACTIVE,
6615 				 NL80211_MESH_POWER_MAX),
6616 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6617 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6618 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6619 };
6620 
6621 static const struct nla_policy
6622 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6623 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6624 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6625 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6626 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6627 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6628 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6629 	[NL80211_MESH_SETUP_IE] =
6630 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6631 				       IEEE80211_MAX_DATA_LEN),
6632 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6633 };
6634 
6635 static int nl80211_parse_mesh_config(struct genl_info *info,
6636 				     struct mesh_config *cfg,
6637 				     u32 *mask_out)
6638 {
6639 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6640 	u32 mask = 0;
6641 	u16 ht_opmode;
6642 
6643 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
6644 do {									\
6645 	if (tb[attr]) {							\
6646 		cfg->param = fn(tb[attr]);				\
6647 		mask |= BIT((attr) - 1);				\
6648 	}								\
6649 } while (0)
6650 
6651 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6652 		return -EINVAL;
6653 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6654 		return -EINVAL;
6655 
6656 	/* This makes sure that there aren't more than 32 mesh config
6657 	 * parameters (otherwise our bitfield scheme would not work.) */
6658 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6659 
6660 	/* Fill in the params struct */
6661 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6662 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6663 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6664 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
6665 				  nla_get_u16);
6666 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6667 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
6668 				  nla_get_u16);
6669 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6670 				  NL80211_MESHCONF_MAX_PEER_LINKS,
6671 				  nla_get_u16);
6672 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6673 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
6674 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
6675 				  NL80211_MESHCONF_TTL, nla_get_u8);
6676 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
6677 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
6678 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
6679 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6680 				  nla_get_u8);
6681 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6682 				  mask,
6683 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6684 				  nla_get_u32);
6685 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
6686 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6687 				  nla_get_u8);
6688 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
6689 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
6690 				  nla_get_u32);
6691 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
6692 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
6693 		return -EINVAL;
6694 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
6695 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6696 				  nla_get_u16);
6697 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6698 				  mask,
6699 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6700 				  nla_get_u32);
6701 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
6702 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
6703 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
6704 		return -EINVAL;
6705 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
6706 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6707 				  nla_get_u16);
6708 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
6709 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6710 				  nla_get_u16);
6711 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6712 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
6713 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6714 				  nla_get_u16);
6715 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
6716 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
6717 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
6718 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6719 				  nla_get_u16);
6720 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
6721 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6722 				  nla_get_u8);
6723 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
6724 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
6725 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
6726 				  NL80211_MESHCONF_RSSI_THRESHOLD,
6727 				  nla_get_s32);
6728 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
6729 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
6730 				  nla_get_u8);
6731 	/*
6732 	 * Check HT operation mode based on
6733 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6734 	 */
6735 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6736 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6737 
6738 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6739 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6740 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6741 			return -EINVAL;
6742 
6743 		/* NON_HT_STA bit is reserved, but some programs set it */
6744 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6745 
6746 		cfg->ht_opmode = ht_opmode;
6747 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6748 	}
6749 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6750 				  dot11MeshHWMPactivePathToRootTimeout, mask,
6751 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6752 				  nla_get_u32);
6753 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
6754 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
6755 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
6756 		return -EINVAL;
6757 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
6758 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6759 				  nla_get_u16);
6760 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
6761 				  mask,
6762 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6763 				  nla_get_u16);
6764 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
6765 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
6766 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
6767 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
6768 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
6769 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
6770 	if (mask_out)
6771 		*mask_out = mask;
6772 
6773 	return 0;
6774 
6775 #undef FILL_IN_MESH_PARAM_IF_SET
6776 }
6777 
6778 static int nl80211_parse_mesh_setup(struct genl_info *info,
6779 				     struct mesh_setup *setup)
6780 {
6781 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6782 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6783 
6784 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6785 		return -EINVAL;
6786 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
6787 		return -EINVAL;
6788 
6789 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6790 		setup->sync_method =
6791 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6792 		 IEEE80211_SYNC_METHOD_VENDOR :
6793 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6794 
6795 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6796 		setup->path_sel_proto =
6797 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6798 		 IEEE80211_PATH_PROTOCOL_VENDOR :
6799 		 IEEE80211_PATH_PROTOCOL_HWMP;
6800 
6801 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6802 		setup->path_metric =
6803 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6804 		 IEEE80211_PATH_METRIC_VENDOR :
6805 		 IEEE80211_PATH_METRIC_AIRTIME;
6806 
6807 	if (tb[NL80211_MESH_SETUP_IE]) {
6808 		struct nlattr *ieattr =
6809 			tb[NL80211_MESH_SETUP_IE];
6810 		setup->ie = nla_data(ieattr);
6811 		setup->ie_len = nla_len(ieattr);
6812 	}
6813 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6814 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6815 		return -EINVAL;
6816 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6817 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6818 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6819 	if (setup->is_secure)
6820 		setup->user_mpm = true;
6821 
6822 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6823 		if (!setup->user_mpm)
6824 			return -EINVAL;
6825 		setup->auth_id =
6826 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6827 	}
6828 
6829 	return 0;
6830 }
6831 
6832 static int nl80211_update_mesh_config(struct sk_buff *skb,
6833 				      struct genl_info *info)
6834 {
6835 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6836 	struct net_device *dev = info->user_ptr[1];
6837 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6838 	struct mesh_config cfg;
6839 	u32 mask;
6840 	int err;
6841 
6842 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6843 		return -EOPNOTSUPP;
6844 
6845 	if (!rdev->ops->update_mesh_config)
6846 		return -EOPNOTSUPP;
6847 
6848 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
6849 	if (err)
6850 		return err;
6851 
6852 	wdev_lock(wdev);
6853 	if (!wdev->mesh_id_len)
6854 		err = -ENOLINK;
6855 
6856 	if (!err)
6857 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6858 
6859 	wdev_unlock(wdev);
6860 
6861 	return err;
6862 }
6863 
6864 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6865 			      struct sk_buff *msg)
6866 {
6867 	struct nlattr *nl_reg_rules;
6868 	unsigned int i;
6869 
6870 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6871 	    (regdom->dfs_region &&
6872 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6873 		goto nla_put_failure;
6874 
6875 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
6876 	if (!nl_reg_rules)
6877 		goto nla_put_failure;
6878 
6879 	for (i = 0; i < regdom->n_reg_rules; i++) {
6880 		struct nlattr *nl_reg_rule;
6881 		const struct ieee80211_reg_rule *reg_rule;
6882 		const struct ieee80211_freq_range *freq_range;
6883 		const struct ieee80211_power_rule *power_rule;
6884 		unsigned int max_bandwidth_khz;
6885 
6886 		reg_rule = &regdom->reg_rules[i];
6887 		freq_range = &reg_rule->freq_range;
6888 		power_rule = &reg_rule->power_rule;
6889 
6890 		nl_reg_rule = nla_nest_start_noflag(msg, i);
6891 		if (!nl_reg_rule)
6892 			goto nla_put_failure;
6893 
6894 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
6895 		if (!max_bandwidth_khz)
6896 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6897 								  reg_rule);
6898 
6899 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6900 				reg_rule->flags) ||
6901 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6902 				freq_range->start_freq_khz) ||
6903 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6904 				freq_range->end_freq_khz) ||
6905 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6906 				max_bandwidth_khz) ||
6907 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6908 				power_rule->max_antenna_gain) ||
6909 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6910 				power_rule->max_eirp) ||
6911 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6912 				reg_rule->dfs_cac_ms))
6913 			goto nla_put_failure;
6914 
6915 		nla_nest_end(msg, nl_reg_rule);
6916 	}
6917 
6918 	nla_nest_end(msg, nl_reg_rules);
6919 	return 0;
6920 
6921 nla_put_failure:
6922 	return -EMSGSIZE;
6923 }
6924 
6925 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6926 {
6927 	const struct ieee80211_regdomain *regdom = NULL;
6928 	struct cfg80211_registered_device *rdev;
6929 	struct wiphy *wiphy = NULL;
6930 	struct sk_buff *msg;
6931 	void *hdr;
6932 
6933 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6934 	if (!msg)
6935 		return -ENOBUFS;
6936 
6937 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6938 			     NL80211_CMD_GET_REG);
6939 	if (!hdr)
6940 		goto put_failure;
6941 
6942 	if (info->attrs[NL80211_ATTR_WIPHY]) {
6943 		bool self_managed;
6944 
6945 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6946 		if (IS_ERR(rdev)) {
6947 			nlmsg_free(msg);
6948 			return PTR_ERR(rdev);
6949 		}
6950 
6951 		wiphy = &rdev->wiphy;
6952 		self_managed = wiphy->regulatory_flags &
6953 			       REGULATORY_WIPHY_SELF_MANAGED;
6954 		regdom = get_wiphy_regdom(wiphy);
6955 
6956 		/* a self-managed-reg device must have a private regdom */
6957 		if (WARN_ON(!regdom && self_managed)) {
6958 			nlmsg_free(msg);
6959 			return -EINVAL;
6960 		}
6961 
6962 		if (regdom &&
6963 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6964 			goto nla_put_failure;
6965 	}
6966 
6967 	if (!wiphy && reg_last_request_cell_base() &&
6968 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6969 			NL80211_USER_REG_HINT_CELL_BASE))
6970 		goto nla_put_failure;
6971 
6972 	rcu_read_lock();
6973 
6974 	if (!regdom)
6975 		regdom = rcu_dereference(cfg80211_regdomain);
6976 
6977 	if (nl80211_put_regdom(regdom, msg))
6978 		goto nla_put_failure_rcu;
6979 
6980 	rcu_read_unlock();
6981 
6982 	genlmsg_end(msg, hdr);
6983 	return genlmsg_reply(msg, info);
6984 
6985 nla_put_failure_rcu:
6986 	rcu_read_unlock();
6987 nla_put_failure:
6988 put_failure:
6989 	nlmsg_free(msg);
6990 	return -EMSGSIZE;
6991 }
6992 
6993 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6994 			       u32 seq, int flags, struct wiphy *wiphy,
6995 			       const struct ieee80211_regdomain *regdom)
6996 {
6997 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6998 				   NL80211_CMD_GET_REG);
6999 
7000 	if (!hdr)
7001 		return -1;
7002 
7003 	genl_dump_check_consistent(cb, hdr);
7004 
7005 	if (nl80211_put_regdom(regdom, msg))
7006 		goto nla_put_failure;
7007 
7008 	if (!wiphy && reg_last_request_cell_base() &&
7009 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7010 			NL80211_USER_REG_HINT_CELL_BASE))
7011 		goto nla_put_failure;
7012 
7013 	if (wiphy &&
7014 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7015 		goto nla_put_failure;
7016 
7017 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7018 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7019 		goto nla_put_failure;
7020 
7021 	genlmsg_end(msg, hdr);
7022 	return 0;
7023 
7024 nla_put_failure:
7025 	genlmsg_cancel(msg, hdr);
7026 	return -EMSGSIZE;
7027 }
7028 
7029 static int nl80211_get_reg_dump(struct sk_buff *skb,
7030 				struct netlink_callback *cb)
7031 {
7032 	const struct ieee80211_regdomain *regdom = NULL;
7033 	struct cfg80211_registered_device *rdev;
7034 	int err, reg_idx, start = cb->args[2];
7035 
7036 	rtnl_lock();
7037 
7038 	if (cfg80211_regdomain && start == 0) {
7039 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7040 					  NLM_F_MULTI, NULL,
7041 					  rtnl_dereference(cfg80211_regdomain));
7042 		if (err < 0)
7043 			goto out_err;
7044 	}
7045 
7046 	/* the global regdom is idx 0 */
7047 	reg_idx = 1;
7048 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7049 		regdom = get_wiphy_regdom(&rdev->wiphy);
7050 		if (!regdom)
7051 			continue;
7052 
7053 		if (++reg_idx <= start)
7054 			continue;
7055 
7056 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7057 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7058 		if (err < 0) {
7059 			reg_idx--;
7060 			break;
7061 		}
7062 	}
7063 
7064 	cb->args[2] = reg_idx;
7065 	err = skb->len;
7066 out_err:
7067 	rtnl_unlock();
7068 	return err;
7069 }
7070 
7071 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7072 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7073 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7074 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7075 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7076 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7077 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7078 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7079 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7080 };
7081 
7082 static int parse_reg_rule(struct nlattr *tb[],
7083 	struct ieee80211_reg_rule *reg_rule)
7084 {
7085 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7086 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7087 
7088 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7089 		return -EINVAL;
7090 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7091 		return -EINVAL;
7092 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7093 		return -EINVAL;
7094 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7095 		return -EINVAL;
7096 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7097 		return -EINVAL;
7098 
7099 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7100 
7101 	freq_range->start_freq_khz =
7102 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7103 	freq_range->end_freq_khz =
7104 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7105 	freq_range->max_bandwidth_khz =
7106 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7107 
7108 	power_rule->max_eirp =
7109 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7110 
7111 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7112 		power_rule->max_antenna_gain =
7113 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7114 
7115 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7116 		reg_rule->dfs_cac_ms =
7117 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7118 
7119 	return 0;
7120 }
7121 
7122 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7123 {
7124 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7125 	struct nlattr *nl_reg_rule;
7126 	char *alpha2;
7127 	int rem_reg_rules, r;
7128 	u32 num_rules = 0, rule_idx = 0;
7129 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7130 	struct ieee80211_regdomain *rd;
7131 
7132 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7133 		return -EINVAL;
7134 
7135 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7136 		return -EINVAL;
7137 
7138 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7139 
7140 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7141 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7142 
7143 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7144 			    rem_reg_rules) {
7145 		num_rules++;
7146 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7147 			return -EINVAL;
7148 	}
7149 
7150 	if (!reg_is_valid_request(alpha2))
7151 		return -EINVAL;
7152 
7153 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7154 	if (!rd)
7155 		return -ENOMEM;
7156 
7157 	rd->n_reg_rules = num_rules;
7158 	rd->alpha2[0] = alpha2[0];
7159 	rd->alpha2[1] = alpha2[1];
7160 
7161 	/*
7162 	 * Disable DFS master mode if the DFS region was
7163 	 * not supported or known on this kernel.
7164 	 */
7165 	if (reg_supported_dfs_region(dfs_region))
7166 		rd->dfs_region = dfs_region;
7167 
7168 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7169 			    rem_reg_rules) {
7170 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7171 						nl_reg_rule, reg_rule_policy,
7172 						info->extack);
7173 		if (r)
7174 			goto bad_reg;
7175 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7176 		if (r)
7177 			goto bad_reg;
7178 
7179 		rule_idx++;
7180 
7181 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7182 			r = -EINVAL;
7183 			goto bad_reg;
7184 		}
7185 	}
7186 
7187 	/* set_regdom takes ownership of rd */
7188 	return set_regdom(rd, REGD_SOURCE_CRDA);
7189  bad_reg:
7190 	kfree(rd);
7191 	return r;
7192 }
7193 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7194 
7195 static int validate_scan_freqs(struct nlattr *freqs)
7196 {
7197 	struct nlattr *attr1, *attr2;
7198 	int n_channels = 0, tmp1, tmp2;
7199 
7200 	nla_for_each_nested(attr1, freqs, tmp1)
7201 		if (nla_len(attr1) != sizeof(u32))
7202 			return 0;
7203 
7204 	nla_for_each_nested(attr1, freqs, tmp1) {
7205 		n_channels++;
7206 		/*
7207 		 * Some hardware has a limited channel list for
7208 		 * scanning, and it is pretty much nonsensical
7209 		 * to scan for a channel twice, so disallow that
7210 		 * and don't require drivers to check that the
7211 		 * channel list they get isn't longer than what
7212 		 * they can scan, as long as they can scan all
7213 		 * the channels they registered at once.
7214 		 */
7215 		nla_for_each_nested(attr2, freqs, tmp2)
7216 			if (attr1 != attr2 &&
7217 			    nla_get_u32(attr1) == nla_get_u32(attr2))
7218 				return 0;
7219 	}
7220 
7221 	return n_channels;
7222 }
7223 
7224 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7225 {
7226 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
7227 }
7228 
7229 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7230 			    struct cfg80211_bss_selection *bss_select)
7231 {
7232 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7233 	struct nlattr *nest;
7234 	int err;
7235 	bool found = false;
7236 	int i;
7237 
7238 	/* only process one nested attribute */
7239 	nest = nla_data(nla);
7240 	if (!nla_ok(nest, nla_len(nest)))
7241 		return -EINVAL;
7242 
7243 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7244 					  nest, nl80211_bss_select_policy,
7245 					  NULL);
7246 	if (err)
7247 		return err;
7248 
7249 	/* only one attribute may be given */
7250 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7251 		if (attr[i]) {
7252 			if (found)
7253 				return -EINVAL;
7254 			found = true;
7255 		}
7256 	}
7257 
7258 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7259 
7260 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7261 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7262 
7263 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7264 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7265 		bss_select->param.band_pref =
7266 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7267 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
7268 			return -EINVAL;
7269 	}
7270 
7271 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7272 		struct nl80211_bss_select_rssi_adjust *adj_param;
7273 
7274 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7275 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7276 		bss_select->param.adjust.band = adj_param->band;
7277 		bss_select->param.adjust.delta = adj_param->delta;
7278 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7279 			return -EINVAL;
7280 	}
7281 
7282 	/* user-space did not provide behaviour attribute */
7283 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7284 		return -EINVAL;
7285 
7286 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7287 		return -EINVAL;
7288 
7289 	return 0;
7290 }
7291 
7292 int nl80211_parse_random_mac(struct nlattr **attrs,
7293 			     u8 *mac_addr, u8 *mac_addr_mask)
7294 {
7295 	int i;
7296 
7297 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7298 		eth_zero_addr(mac_addr);
7299 		eth_zero_addr(mac_addr_mask);
7300 		mac_addr[0] = 0x2;
7301 		mac_addr_mask[0] = 0x3;
7302 
7303 		return 0;
7304 	}
7305 
7306 	/* need both or none */
7307 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7308 		return -EINVAL;
7309 
7310 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7311 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7312 
7313 	/* don't allow or configure an mcast address */
7314 	if (!is_multicast_ether_addr(mac_addr_mask) ||
7315 	    is_multicast_ether_addr(mac_addr))
7316 		return -EINVAL;
7317 
7318 	/*
7319 	 * allow users to pass a MAC address that has bits set outside
7320 	 * of the mask, but don't bother drivers with having to deal
7321 	 * with such bits
7322 	 */
7323 	for (i = 0; i < ETH_ALEN; i++)
7324 		mac_addr[i] &= mac_addr_mask[i];
7325 
7326 	return 0;
7327 }
7328 
7329 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7330 {
7331 	ASSERT_WDEV_LOCK(wdev);
7332 
7333 	if (!cfg80211_beaconing_iface_active(wdev))
7334 		return true;
7335 
7336 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7337 		return true;
7338 
7339 	return regulatory_pre_cac_allowed(wdev->wiphy);
7340 }
7341 
7342 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7343 				    enum nl80211_ext_feature_index feat)
7344 {
7345 	if (!(flags & flag))
7346 		return true;
7347 	if (wiphy_ext_feature_isset(wiphy, feat))
7348 		return true;
7349 	return false;
7350 }
7351 
7352 static int
7353 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7354 			 void *request, struct nlattr **attrs,
7355 			 bool is_sched_scan)
7356 {
7357 	u8 *mac_addr, *mac_addr_mask;
7358 	u32 *flags;
7359 	enum nl80211_feature_flags randomness_flag;
7360 
7361 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7362 		return 0;
7363 
7364 	if (is_sched_scan) {
7365 		struct cfg80211_sched_scan_request *req = request;
7366 
7367 		randomness_flag = wdev ?
7368 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7369 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7370 		flags = &req->flags;
7371 		mac_addr = req->mac_addr;
7372 		mac_addr_mask = req->mac_addr_mask;
7373 	} else {
7374 		struct cfg80211_scan_request *req = request;
7375 
7376 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7377 		flags = &req->flags;
7378 		mac_addr = req->mac_addr;
7379 		mac_addr_mask = req->mac_addr_mask;
7380 	}
7381 
7382 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7383 
7384 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7385 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7386 	    !nl80211_check_scan_feat(wiphy, *flags,
7387 				     NL80211_SCAN_FLAG_LOW_SPAN,
7388 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7389 	    !nl80211_check_scan_feat(wiphy, *flags,
7390 				     NL80211_SCAN_FLAG_LOW_POWER,
7391 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7392 	    !nl80211_check_scan_feat(wiphy, *flags,
7393 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
7394 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7395 	    !nl80211_check_scan_feat(wiphy, *flags,
7396 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7397 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7398 	    !nl80211_check_scan_feat(wiphy, *flags,
7399 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7400 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7401 	    !nl80211_check_scan_feat(wiphy, *flags,
7402 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7403 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7404 	    !nl80211_check_scan_feat(wiphy, *flags,
7405 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7406 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7407 	    !nl80211_check_scan_feat(wiphy, *flags,
7408 				     NL80211_SCAN_FLAG_RANDOM_SN,
7409 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7410 	    !nl80211_check_scan_feat(wiphy, *flags,
7411 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7412 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7413 		return -EOPNOTSUPP;
7414 
7415 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7416 		int err;
7417 
7418 		if (!(wiphy->features & randomness_flag) ||
7419 		    (wdev && wdev->current_bss))
7420 			return -EOPNOTSUPP;
7421 
7422 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7423 		if (err)
7424 			return err;
7425 	}
7426 
7427 	return 0;
7428 }
7429 
7430 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7431 {
7432 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7433 	struct wireless_dev *wdev = info->user_ptr[1];
7434 	struct cfg80211_scan_request *request;
7435 	struct nlattr *attr;
7436 	struct wiphy *wiphy;
7437 	int err, tmp, n_ssids = 0, n_channels, i;
7438 	size_t ie_len;
7439 
7440 	wiphy = &rdev->wiphy;
7441 
7442 	if (wdev->iftype == NL80211_IFTYPE_NAN)
7443 		return -EOPNOTSUPP;
7444 
7445 	if (!rdev->ops->scan)
7446 		return -EOPNOTSUPP;
7447 
7448 	if (rdev->scan_req || rdev->scan_msg) {
7449 		err = -EBUSY;
7450 		goto unlock;
7451 	}
7452 
7453 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7454 		n_channels = validate_scan_freqs(
7455 				info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7456 		if (!n_channels) {
7457 			err = -EINVAL;
7458 			goto unlock;
7459 		}
7460 	} else {
7461 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7462 	}
7463 
7464 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7465 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7466 			n_ssids++;
7467 
7468 	if (n_ssids > wiphy->max_scan_ssids) {
7469 		err = -EINVAL;
7470 		goto unlock;
7471 	}
7472 
7473 	if (info->attrs[NL80211_ATTR_IE])
7474 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7475 	else
7476 		ie_len = 0;
7477 
7478 	if (ie_len > wiphy->max_scan_ie_len) {
7479 		err = -EINVAL;
7480 		goto unlock;
7481 	}
7482 
7483 	request = kzalloc(sizeof(*request)
7484 			+ sizeof(*request->ssids) * n_ssids
7485 			+ sizeof(*request->channels) * n_channels
7486 			+ ie_len, GFP_KERNEL);
7487 	if (!request) {
7488 		err = -ENOMEM;
7489 		goto unlock;
7490 	}
7491 
7492 	if (n_ssids)
7493 		request->ssids = (void *)&request->channels[n_channels];
7494 	request->n_ssids = n_ssids;
7495 	if (ie_len) {
7496 		if (n_ssids)
7497 			request->ie = (void *)(request->ssids + n_ssids);
7498 		else
7499 			request->ie = (void *)(request->channels + n_channels);
7500 	}
7501 
7502 	i = 0;
7503 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7504 		/* user specified, bail out if channel not found */
7505 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7506 			struct ieee80211_channel *chan;
7507 
7508 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7509 
7510 			if (!chan) {
7511 				err = -EINVAL;
7512 				goto out_free;
7513 			}
7514 
7515 			/* ignore disabled channels */
7516 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7517 				continue;
7518 
7519 			request->channels[i] = chan;
7520 			i++;
7521 		}
7522 	} else {
7523 		enum nl80211_band band;
7524 
7525 		/* all channels */
7526 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7527 			int j;
7528 
7529 			if (!wiphy->bands[band])
7530 				continue;
7531 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7532 				struct ieee80211_channel *chan;
7533 
7534 				chan = &wiphy->bands[band]->channels[j];
7535 
7536 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7537 					continue;
7538 
7539 				request->channels[i] = chan;
7540 				i++;
7541 			}
7542 		}
7543 	}
7544 
7545 	if (!i) {
7546 		err = -EINVAL;
7547 		goto out_free;
7548 	}
7549 
7550 	request->n_channels = i;
7551 
7552 	wdev_lock(wdev);
7553 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
7554 		struct ieee80211_channel *chan;
7555 
7556 		if (request->n_channels != 1) {
7557 			wdev_unlock(wdev);
7558 			err = -EBUSY;
7559 			goto out_free;
7560 		}
7561 
7562 		chan = request->channels[0];
7563 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
7564 			wdev_unlock(wdev);
7565 			err = -EBUSY;
7566 			goto out_free;
7567 		}
7568 	}
7569 	wdev_unlock(wdev);
7570 
7571 	i = 0;
7572 	if (n_ssids) {
7573 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7574 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7575 				err = -EINVAL;
7576 				goto out_free;
7577 			}
7578 			request->ssids[i].ssid_len = nla_len(attr);
7579 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7580 			i++;
7581 		}
7582 	}
7583 
7584 	if (info->attrs[NL80211_ATTR_IE]) {
7585 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7586 		memcpy((void *)request->ie,
7587 		       nla_data(info->attrs[NL80211_ATTR_IE]),
7588 		       request->ie_len);
7589 	}
7590 
7591 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7592 		if (wiphy->bands[i])
7593 			request->rates[i] =
7594 				(1 << wiphy->bands[i]->n_bitrates) - 1;
7595 
7596 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7597 		nla_for_each_nested(attr,
7598 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7599 				    tmp) {
7600 			enum nl80211_band band = nla_type(attr);
7601 
7602 			if (band < 0 || band >= NUM_NL80211_BANDS) {
7603 				err = -EINVAL;
7604 				goto out_free;
7605 			}
7606 
7607 			if (!wiphy->bands[band])
7608 				continue;
7609 
7610 			err = ieee80211_get_ratemask(wiphy->bands[band],
7611 						     nla_data(attr),
7612 						     nla_len(attr),
7613 						     &request->rates[band]);
7614 			if (err)
7615 				goto out_free;
7616 		}
7617 	}
7618 
7619 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7620 		if (!wiphy_ext_feature_isset(wiphy,
7621 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7622 			err = -EOPNOTSUPP;
7623 			goto out_free;
7624 		}
7625 
7626 		request->duration =
7627 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7628 		request->duration_mandatory =
7629 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7630 	}
7631 
7632 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7633 				       false);
7634 	if (err)
7635 		goto out_free;
7636 
7637 	request->no_cck =
7638 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7639 
7640 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
7641 	 * BSSID to scan for. This was problematic because that same attribute
7642 	 * was already used for another purpose (local random MAC address). The
7643 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7644 	 * compatibility with older userspace components, also use the
7645 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
7646 	 * the specific BSSID use case instead of the random MAC address
7647 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7648 	 */
7649 	if (info->attrs[NL80211_ATTR_BSSID])
7650 		memcpy(request->bssid,
7651 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7652 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7653 		 info->attrs[NL80211_ATTR_MAC])
7654 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7655 		       ETH_ALEN);
7656 	else
7657 		eth_broadcast_addr(request->bssid);
7658 
7659 	request->wdev = wdev;
7660 	request->wiphy = &rdev->wiphy;
7661 	request->scan_start = jiffies;
7662 
7663 	rdev->scan_req = request;
7664 	err = rdev_scan(rdev, request);
7665 
7666 	if (!err) {
7667 		nl80211_send_scan_start(rdev, wdev);
7668 		if (wdev->netdev)
7669 			dev_hold(wdev->netdev);
7670 	} else {
7671  out_free:
7672 		rdev->scan_req = NULL;
7673 		kfree(request);
7674 	}
7675 
7676  unlock:
7677 	return err;
7678 }
7679 
7680 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7681 {
7682 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7683 	struct wireless_dev *wdev = info->user_ptr[1];
7684 
7685 	if (!rdev->ops->abort_scan)
7686 		return -EOPNOTSUPP;
7687 
7688 	if (rdev->scan_msg)
7689 		return 0;
7690 
7691 	if (!rdev->scan_req)
7692 		return -ENOENT;
7693 
7694 	rdev_abort_scan(rdev, wdev);
7695 	return 0;
7696 }
7697 
7698 static int
7699 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7700 			       struct cfg80211_sched_scan_request *request,
7701 			       struct nlattr **attrs)
7702 {
7703 	int tmp, err, i = 0;
7704 	struct nlattr *attr;
7705 
7706 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7707 		u32 interval;
7708 
7709 		/*
7710 		 * If scan plans are not specified,
7711 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7712 		 * case one scan plan will be set with the specified scan
7713 		 * interval and infinite number of iterations.
7714 		 */
7715 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7716 		if (!interval)
7717 			return -EINVAL;
7718 
7719 		request->scan_plans[0].interval =
7720 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
7721 		if (!request->scan_plans[0].interval)
7722 			return -EINVAL;
7723 
7724 		if (request->scan_plans[0].interval >
7725 		    wiphy->max_sched_scan_plan_interval)
7726 			request->scan_plans[0].interval =
7727 				wiphy->max_sched_scan_plan_interval;
7728 
7729 		return 0;
7730 	}
7731 
7732 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7733 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7734 
7735 		if (WARN_ON(i >= n_plans))
7736 			return -EINVAL;
7737 
7738 		err = nla_parse_nested_deprecated(plan,
7739 						  NL80211_SCHED_SCAN_PLAN_MAX,
7740 						  attr, nl80211_plan_policy,
7741 						  NULL);
7742 		if (err)
7743 			return err;
7744 
7745 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7746 			return -EINVAL;
7747 
7748 		request->scan_plans[i].interval =
7749 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7750 		if (!request->scan_plans[i].interval ||
7751 		    request->scan_plans[i].interval >
7752 		    wiphy->max_sched_scan_plan_interval)
7753 			return -EINVAL;
7754 
7755 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7756 			request->scan_plans[i].iterations =
7757 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7758 			if (!request->scan_plans[i].iterations ||
7759 			    (request->scan_plans[i].iterations >
7760 			     wiphy->max_sched_scan_plan_iterations))
7761 				return -EINVAL;
7762 		} else if (i < n_plans - 1) {
7763 			/*
7764 			 * All scan plans but the last one must specify
7765 			 * a finite number of iterations
7766 			 */
7767 			return -EINVAL;
7768 		}
7769 
7770 		i++;
7771 	}
7772 
7773 	/*
7774 	 * The last scan plan must not specify the number of
7775 	 * iterations, it is supposed to run infinitely
7776 	 */
7777 	if (request->scan_plans[n_plans - 1].iterations)
7778 		return  -EINVAL;
7779 
7780 	return 0;
7781 }
7782 
7783 static int
7784 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
7785 				       struct cfg80211_match_set *match_sets,
7786 				       struct nlattr *tb_band_rssi,
7787 				       s32 rssi_thold)
7788 {
7789 	struct nlattr *attr;
7790 	int i, tmp, ret = 0;
7791 
7792 	if (!wiphy_ext_feature_isset(wiphy,
7793 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
7794 		if (tb_band_rssi)
7795 			ret = -EOPNOTSUPP;
7796 		else
7797 			for (i = 0; i < NUM_NL80211_BANDS; i++)
7798 				match_sets->per_band_rssi_thold[i] =
7799 					NL80211_SCAN_RSSI_THOLD_OFF;
7800 		return ret;
7801 	}
7802 
7803 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7804 		match_sets->per_band_rssi_thold[i] = rssi_thold;
7805 
7806 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
7807 		enum nl80211_band band = nla_type(attr);
7808 
7809 		if (band < 0 || band >= NUM_NL80211_BANDS)
7810 			return -EINVAL;
7811 
7812 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
7813 	}
7814 
7815 	return 0;
7816 }
7817 
7818 static struct cfg80211_sched_scan_request *
7819 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7820 			 struct nlattr **attrs, int max_match_sets)
7821 {
7822 	struct cfg80211_sched_scan_request *request;
7823 	struct nlattr *attr;
7824 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7825 	enum nl80211_band band;
7826 	size_t ie_len;
7827 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7828 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7829 
7830 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7831 		n_channels = validate_scan_freqs(
7832 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7833 		if (!n_channels)
7834 			return ERR_PTR(-EINVAL);
7835 	} else {
7836 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7837 	}
7838 
7839 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
7840 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7841 				    tmp)
7842 			n_ssids++;
7843 
7844 	if (n_ssids > wiphy->max_sched_scan_ssids)
7845 		return ERR_PTR(-EINVAL);
7846 
7847 	/*
7848 	 * First, count the number of 'real' matchsets. Due to an issue with
7849 	 * the old implementation, matchsets containing only the RSSI attribute
7850 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7851 	 * RSSI for all matchsets, rather than their own matchset for reporting
7852 	 * all APs with a strong RSSI. This is needed to be compatible with
7853 	 * older userspace that treated a matchset with only the RSSI as the
7854 	 * global RSSI for all other matchsets - if there are other matchsets.
7855 	 */
7856 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7857 		nla_for_each_nested(attr,
7858 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7859 				    tmp) {
7860 			struct nlattr *rssi;
7861 
7862 			err = nla_parse_nested_deprecated(tb,
7863 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7864 							  attr,
7865 							  nl80211_match_policy,
7866 							  NULL);
7867 			if (err)
7868 				return ERR_PTR(err);
7869 
7870 			/* SSID and BSSID are mutually exclusive */
7871 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7872 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7873 				return ERR_PTR(-EINVAL);
7874 
7875 			/* add other standalone attributes here */
7876 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7877 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7878 				n_match_sets++;
7879 				continue;
7880 			}
7881 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7882 			if (rssi)
7883 				default_match_rssi = nla_get_s32(rssi);
7884 		}
7885 	}
7886 
7887 	/* However, if there's no other matchset, add the RSSI one */
7888 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7889 		n_match_sets = 1;
7890 
7891 	if (n_match_sets > max_match_sets)
7892 		return ERR_PTR(-EINVAL);
7893 
7894 	if (attrs[NL80211_ATTR_IE])
7895 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7896 	else
7897 		ie_len = 0;
7898 
7899 	if (ie_len > wiphy->max_sched_scan_ie_len)
7900 		return ERR_PTR(-EINVAL);
7901 
7902 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7903 		/*
7904 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7905 		 * each scan plan already specifies its own interval
7906 		 */
7907 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7908 			return ERR_PTR(-EINVAL);
7909 
7910 		nla_for_each_nested(attr,
7911 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7912 			n_plans++;
7913 	} else {
7914 		/*
7915 		 * The scan interval attribute is kept for backward
7916 		 * compatibility. If no scan plans are specified and sched scan
7917 		 * interval is specified, one scan plan will be set with this
7918 		 * scan interval and infinite number of iterations.
7919 		 */
7920 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7921 			return ERR_PTR(-EINVAL);
7922 
7923 		n_plans = 1;
7924 	}
7925 
7926 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7927 		return ERR_PTR(-EINVAL);
7928 
7929 	if (!wiphy_ext_feature_isset(
7930 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7931 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7932 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7933 		return ERR_PTR(-EINVAL);
7934 
7935 	request = kzalloc(sizeof(*request)
7936 			+ sizeof(*request->ssids) * n_ssids
7937 			+ sizeof(*request->match_sets) * n_match_sets
7938 			+ sizeof(*request->scan_plans) * n_plans
7939 			+ sizeof(*request->channels) * n_channels
7940 			+ ie_len, GFP_KERNEL);
7941 	if (!request)
7942 		return ERR_PTR(-ENOMEM);
7943 
7944 	if (n_ssids)
7945 		request->ssids = (void *)&request->channels[n_channels];
7946 	request->n_ssids = n_ssids;
7947 	if (ie_len) {
7948 		if (n_ssids)
7949 			request->ie = (void *)(request->ssids + n_ssids);
7950 		else
7951 			request->ie = (void *)(request->channels + n_channels);
7952 	}
7953 
7954 	if (n_match_sets) {
7955 		if (request->ie)
7956 			request->match_sets = (void *)(request->ie + ie_len);
7957 		else if (n_ssids)
7958 			request->match_sets =
7959 				(void *)(request->ssids + n_ssids);
7960 		else
7961 			request->match_sets =
7962 				(void *)(request->channels + n_channels);
7963 	}
7964 	request->n_match_sets = n_match_sets;
7965 
7966 	if (n_match_sets)
7967 		request->scan_plans = (void *)(request->match_sets +
7968 					       n_match_sets);
7969 	else if (request->ie)
7970 		request->scan_plans = (void *)(request->ie + ie_len);
7971 	else if (n_ssids)
7972 		request->scan_plans = (void *)(request->ssids + n_ssids);
7973 	else
7974 		request->scan_plans = (void *)(request->channels + n_channels);
7975 
7976 	request->n_scan_plans = n_plans;
7977 
7978 	i = 0;
7979 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7980 		/* user specified, bail out if channel not found */
7981 		nla_for_each_nested(attr,
7982 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
7983 				    tmp) {
7984 			struct ieee80211_channel *chan;
7985 
7986 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7987 
7988 			if (!chan) {
7989 				err = -EINVAL;
7990 				goto out_free;
7991 			}
7992 
7993 			/* ignore disabled channels */
7994 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7995 				continue;
7996 
7997 			request->channels[i] = chan;
7998 			i++;
7999 		}
8000 	} else {
8001 		/* all channels */
8002 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8003 			int j;
8004 
8005 			if (!wiphy->bands[band])
8006 				continue;
8007 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8008 				struct ieee80211_channel *chan;
8009 
8010 				chan = &wiphy->bands[band]->channels[j];
8011 
8012 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8013 					continue;
8014 
8015 				request->channels[i] = chan;
8016 				i++;
8017 			}
8018 		}
8019 	}
8020 
8021 	if (!i) {
8022 		err = -EINVAL;
8023 		goto out_free;
8024 	}
8025 
8026 	request->n_channels = i;
8027 
8028 	i = 0;
8029 	if (n_ssids) {
8030 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8031 				    tmp) {
8032 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8033 				err = -EINVAL;
8034 				goto out_free;
8035 			}
8036 			request->ssids[i].ssid_len = nla_len(attr);
8037 			memcpy(request->ssids[i].ssid, nla_data(attr),
8038 			       nla_len(attr));
8039 			i++;
8040 		}
8041 	}
8042 
8043 	i = 0;
8044 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8045 		nla_for_each_nested(attr,
8046 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8047 				    tmp) {
8048 			struct nlattr *ssid, *bssid, *rssi;
8049 
8050 			err = nla_parse_nested_deprecated(tb,
8051 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8052 							  attr,
8053 							  nl80211_match_policy,
8054 							  NULL);
8055 			if (err)
8056 				goto out_free;
8057 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8058 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8059 
8060 			if (!ssid && !bssid) {
8061 				i++;
8062 				continue;
8063 			}
8064 
8065 			if (WARN_ON(i >= n_match_sets)) {
8066 				/* this indicates a programming error,
8067 				 * the loop above should have verified
8068 				 * things properly
8069 				 */
8070 				err = -EINVAL;
8071 				goto out_free;
8072 			}
8073 
8074 			if (ssid) {
8075 				if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8076 					err = -EINVAL;
8077 					goto out_free;
8078 				}
8079 				memcpy(request->match_sets[i].ssid.ssid,
8080 				       nla_data(ssid), nla_len(ssid));
8081 				request->match_sets[i].ssid.ssid_len =
8082 					nla_len(ssid);
8083 			}
8084 			if (bssid) {
8085 				if (nla_len(bssid) != ETH_ALEN) {
8086 					err = -EINVAL;
8087 					goto out_free;
8088 				}
8089 				memcpy(request->match_sets[i].bssid,
8090 				       nla_data(bssid), ETH_ALEN);
8091 			}
8092 
8093 			/* special attribute - old implementation w/a */
8094 			request->match_sets[i].rssi_thold = default_match_rssi;
8095 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8096 			if (rssi)
8097 				request->match_sets[i].rssi_thold =
8098 					nla_get_s32(rssi);
8099 
8100 			/* Parse per band RSSI attribute */
8101 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8102 				&request->match_sets[i],
8103 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8104 				request->match_sets[i].rssi_thold);
8105 			if (err)
8106 				goto out_free;
8107 
8108 			i++;
8109 		}
8110 
8111 		/* there was no other matchset, so the RSSI one is alone */
8112 		if (i == 0 && n_match_sets)
8113 			request->match_sets[0].rssi_thold = default_match_rssi;
8114 
8115 		request->min_rssi_thold = INT_MAX;
8116 		for (i = 0; i < n_match_sets; i++)
8117 			request->min_rssi_thold =
8118 				min(request->match_sets[i].rssi_thold,
8119 				    request->min_rssi_thold);
8120 	} else {
8121 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8122 	}
8123 
8124 	if (ie_len) {
8125 		request->ie_len = ie_len;
8126 		memcpy((void *)request->ie,
8127 		       nla_data(attrs[NL80211_ATTR_IE]),
8128 		       request->ie_len);
8129 	}
8130 
8131 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8132 	if (err)
8133 		goto out_free;
8134 
8135 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8136 		request->delay =
8137 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8138 
8139 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8140 		request->relative_rssi = nla_get_s8(
8141 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8142 		request->relative_rssi_set = true;
8143 	}
8144 
8145 	if (request->relative_rssi_set &&
8146 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8147 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8148 
8149 		rssi_adjust = nla_data(
8150 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8151 		request->rssi_adjust.band = rssi_adjust->band;
8152 		request->rssi_adjust.delta = rssi_adjust->delta;
8153 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8154 			err = -EINVAL;
8155 			goto out_free;
8156 		}
8157 	}
8158 
8159 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8160 	if (err)
8161 		goto out_free;
8162 
8163 	request->scan_start = jiffies;
8164 
8165 	return request;
8166 
8167 out_free:
8168 	kfree(request);
8169 	return ERR_PTR(err);
8170 }
8171 
8172 static int nl80211_start_sched_scan(struct sk_buff *skb,
8173 				    struct genl_info *info)
8174 {
8175 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8176 	struct net_device *dev = info->user_ptr[1];
8177 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8178 	struct cfg80211_sched_scan_request *sched_scan_req;
8179 	bool want_multi;
8180 	int err;
8181 
8182 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8183 		return -EOPNOTSUPP;
8184 
8185 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8186 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8187 	if (err)
8188 		return err;
8189 
8190 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8191 						  info->attrs,
8192 						  rdev->wiphy.max_match_sets);
8193 
8194 	err = PTR_ERR_OR_ZERO(sched_scan_req);
8195 	if (err)
8196 		goto out_err;
8197 
8198 	/* leave request id zero for legacy request
8199 	 * or if driver does not support multi-scheduled scan
8200 	 */
8201 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
8202 		while (!sched_scan_req->reqid)
8203 			sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8204 	}
8205 
8206 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8207 	if (err)
8208 		goto out_free;
8209 
8210 	sched_scan_req->dev = dev;
8211 	sched_scan_req->wiphy = &rdev->wiphy;
8212 
8213 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8214 		sched_scan_req->owner_nlportid = info->snd_portid;
8215 
8216 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8217 
8218 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8219 	return 0;
8220 
8221 out_free:
8222 	kfree(sched_scan_req);
8223 out_err:
8224 	return err;
8225 }
8226 
8227 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8228 				   struct genl_info *info)
8229 {
8230 	struct cfg80211_sched_scan_request *req;
8231 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8232 	u64 cookie;
8233 
8234 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8235 		return -EOPNOTSUPP;
8236 
8237 	if (info->attrs[NL80211_ATTR_COOKIE]) {
8238 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8239 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
8240 	}
8241 
8242 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8243 				     struct cfg80211_sched_scan_request,
8244 				     list);
8245 	if (!req || req->reqid ||
8246 	    (req->owner_nlportid &&
8247 	     req->owner_nlportid != info->snd_portid))
8248 		return -ENOENT;
8249 
8250 	return cfg80211_stop_sched_scan_req(rdev, req, false);
8251 }
8252 
8253 static int nl80211_start_radar_detection(struct sk_buff *skb,
8254 					 struct genl_info *info)
8255 {
8256 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8257 	struct net_device *dev = info->user_ptr[1];
8258 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8259 	struct wiphy *wiphy = wdev->wiphy;
8260 	struct cfg80211_chan_def chandef;
8261 	enum nl80211_dfs_regions dfs_region;
8262 	unsigned int cac_time_ms;
8263 	int err;
8264 
8265 	dfs_region = reg_get_dfs_region(wiphy);
8266 	if (dfs_region == NL80211_DFS_UNSET)
8267 		return -EINVAL;
8268 
8269 	err = nl80211_parse_chandef(rdev, info, &chandef);
8270 	if (err)
8271 		return err;
8272 
8273 	if (netif_carrier_ok(dev))
8274 		return -EBUSY;
8275 
8276 	if (wdev->cac_started)
8277 		return -EBUSY;
8278 
8279 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8280 	if (err < 0)
8281 		return err;
8282 
8283 	if (err == 0)
8284 		return -EINVAL;
8285 
8286 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8287 		return -EINVAL;
8288 
8289 	/* CAC start is offloaded to HW and can't be started manually */
8290 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8291 		return -EOPNOTSUPP;
8292 
8293 	if (!rdev->ops->start_radar_detection)
8294 		return -EOPNOTSUPP;
8295 
8296 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8297 	if (WARN_ON(!cac_time_ms))
8298 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8299 
8300 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8301 	if (!err) {
8302 		wdev->chandef = chandef;
8303 		wdev->cac_started = true;
8304 		wdev->cac_start_time = jiffies;
8305 		wdev->cac_time_ms = cac_time_ms;
8306 	}
8307 	return err;
8308 }
8309 
8310 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8311 					  struct genl_info *info)
8312 {
8313 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8314 	struct net_device *dev = info->user_ptr[1];
8315 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8316 	struct wiphy *wiphy = wdev->wiphy;
8317 	struct cfg80211_chan_def chandef;
8318 	enum nl80211_dfs_regions dfs_region;
8319 	int err;
8320 
8321 	dfs_region = reg_get_dfs_region(wiphy);
8322 	if (dfs_region == NL80211_DFS_UNSET) {
8323 		GENL_SET_ERR_MSG(info,
8324 				 "DFS Region is not set. Unexpected Radar indication");
8325 		return -EINVAL;
8326 	}
8327 
8328 	err = nl80211_parse_chandef(rdev, info, &chandef);
8329 	if (err) {
8330 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8331 		return err;
8332 	}
8333 
8334 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8335 	if (err < 0) {
8336 		GENL_SET_ERR_MSG(info, "chandef is invalid");
8337 		return err;
8338 	}
8339 
8340 	if (err == 0) {
8341 		GENL_SET_ERR_MSG(info,
8342 				 "Unexpected Radar indication for chandef/iftype");
8343 		return -EINVAL;
8344 	}
8345 
8346 	/* Do not process this notification if radar is already detected
8347 	 * by kernel on this channel, and return success.
8348 	 */
8349 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8350 		return 0;
8351 
8352 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8353 
8354 	cfg80211_sched_dfs_chan_update(rdev);
8355 
8356 	rdev->radar_chandef = chandef;
8357 
8358 	/* Propagate this notification to other radios as well */
8359 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8360 
8361 	return 0;
8362 }
8363 
8364 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8365 {
8366 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8367 	struct net_device *dev = info->user_ptr[1];
8368 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8369 	struct cfg80211_csa_settings params;
8370 	/* csa_attrs is defined static to avoid waste of stack size - this
8371 	 * function is called under RTNL lock, so this should not be a problem.
8372 	 */
8373 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8374 	int err;
8375 	bool need_new_beacon = false;
8376 	bool need_handle_dfs_flag = true;
8377 	int len, i;
8378 	u32 cs_count;
8379 
8380 	if (!rdev->ops->channel_switch ||
8381 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8382 		return -EOPNOTSUPP;
8383 
8384 	switch (dev->ieee80211_ptr->iftype) {
8385 	case NL80211_IFTYPE_AP:
8386 	case NL80211_IFTYPE_P2P_GO:
8387 		need_new_beacon = true;
8388 		/* For all modes except AP the handle_dfs flag needs to be
8389 		 * supplied to tell the kernel that userspace will handle radar
8390 		 * events when they happen. Otherwise a switch to a channel
8391 		 * requiring DFS will be rejected.
8392 		 */
8393 		need_handle_dfs_flag = false;
8394 
8395 		/* useless if AP is not running */
8396 		if (!wdev->beacon_interval)
8397 			return -ENOTCONN;
8398 		break;
8399 	case NL80211_IFTYPE_ADHOC:
8400 		if (!wdev->ssid_len)
8401 			return -ENOTCONN;
8402 		break;
8403 	case NL80211_IFTYPE_MESH_POINT:
8404 		if (!wdev->mesh_id_len)
8405 			return -ENOTCONN;
8406 		break;
8407 	default:
8408 		return -EOPNOTSUPP;
8409 	}
8410 
8411 	memset(&params, 0, sizeof(params));
8412 	params.beacon_csa.ftm_responder = -1;
8413 
8414 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8415 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8416 		return -EINVAL;
8417 
8418 	/* only important for AP, IBSS and mesh create IEs internally */
8419 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8420 		return -EINVAL;
8421 
8422 	/* Even though the attribute is u32, the specification says
8423 	 * u8, so let's make sure we don't overflow.
8424 	 */
8425 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8426 	if (cs_count > 255)
8427 		return -EINVAL;
8428 
8429 	params.count = cs_count;
8430 
8431 	if (!need_new_beacon)
8432 		goto skip_beacons;
8433 
8434 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8435 	if (err)
8436 		return err;
8437 
8438 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8439 					  info->attrs[NL80211_ATTR_CSA_IES],
8440 					  nl80211_policy, info->extack);
8441 	if (err)
8442 		return err;
8443 
8444 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8445 	if (err)
8446 		return err;
8447 
8448 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8449 		return -EINVAL;
8450 
8451 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8452 	if (!len || (len % sizeof(u16)))
8453 		return -EINVAL;
8454 
8455 	params.n_counter_offsets_beacon = len / sizeof(u16);
8456 	if (rdev->wiphy.max_num_csa_counters &&
8457 	    (params.n_counter_offsets_beacon >
8458 	     rdev->wiphy.max_num_csa_counters))
8459 		return -EINVAL;
8460 
8461 	params.counter_offsets_beacon =
8462 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8463 
8464 	/* sanity checks - counters should fit and be the same */
8465 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8466 		u16 offset = params.counter_offsets_beacon[i];
8467 
8468 		if (offset >= params.beacon_csa.tail_len)
8469 			return -EINVAL;
8470 
8471 		if (params.beacon_csa.tail[offset] != params.count)
8472 			return -EINVAL;
8473 	}
8474 
8475 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8476 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8477 		if (!len || (len % sizeof(u16)))
8478 			return -EINVAL;
8479 
8480 		params.n_counter_offsets_presp = len / sizeof(u16);
8481 		if (rdev->wiphy.max_num_csa_counters &&
8482 		    (params.n_counter_offsets_presp >
8483 		     rdev->wiphy.max_num_csa_counters))
8484 			return -EINVAL;
8485 
8486 		params.counter_offsets_presp =
8487 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8488 
8489 		/* sanity checks - counters should fit and be the same */
8490 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
8491 			u16 offset = params.counter_offsets_presp[i];
8492 
8493 			if (offset >= params.beacon_csa.probe_resp_len)
8494 				return -EINVAL;
8495 
8496 			if (params.beacon_csa.probe_resp[offset] !=
8497 			    params.count)
8498 				return -EINVAL;
8499 		}
8500 	}
8501 
8502 skip_beacons:
8503 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
8504 	if (err)
8505 		return err;
8506 
8507 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8508 					   wdev->iftype))
8509 		return -EINVAL;
8510 
8511 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
8512 					    &params.chandef,
8513 					    wdev->iftype);
8514 	if (err < 0)
8515 		return err;
8516 
8517 	if (err > 0) {
8518 		params.radar_required = true;
8519 		if (need_handle_dfs_flag &&
8520 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8521 			return -EINVAL;
8522 		}
8523 	}
8524 
8525 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8526 		params.block_tx = true;
8527 
8528 	wdev_lock(wdev);
8529 	err = rdev_channel_switch(rdev, dev, &params);
8530 	wdev_unlock(wdev);
8531 
8532 	return err;
8533 }
8534 
8535 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8536 			    u32 seq, int flags,
8537 			    struct cfg80211_registered_device *rdev,
8538 			    struct wireless_dev *wdev,
8539 			    struct cfg80211_internal_bss *intbss)
8540 {
8541 	struct cfg80211_bss *res = &intbss->pub;
8542 	const struct cfg80211_bss_ies *ies;
8543 	void *hdr;
8544 	struct nlattr *bss;
8545 
8546 	ASSERT_WDEV_LOCK(wdev);
8547 
8548 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8549 			     NL80211_CMD_NEW_SCAN_RESULTS);
8550 	if (!hdr)
8551 		return -1;
8552 
8553 	genl_dump_check_consistent(cb, hdr);
8554 
8555 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8556 		goto nla_put_failure;
8557 	if (wdev->netdev &&
8558 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8559 		goto nla_put_failure;
8560 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8561 			      NL80211_ATTR_PAD))
8562 		goto nla_put_failure;
8563 
8564 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8565 	if (!bss)
8566 		goto nla_put_failure;
8567 	if ((!is_zero_ether_addr(res->bssid) &&
8568 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8569 		goto nla_put_failure;
8570 
8571 	rcu_read_lock();
8572 	/* indicate whether we have probe response data or not */
8573 	if (rcu_access_pointer(res->proberesp_ies) &&
8574 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8575 		goto fail_unlock_rcu;
8576 
8577 	/* this pointer prefers to be pointed to probe response data
8578 	 * but is always valid
8579 	 */
8580 	ies = rcu_dereference(res->ies);
8581 	if (ies) {
8582 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8583 				      NL80211_BSS_PAD))
8584 			goto fail_unlock_rcu;
8585 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8586 					ies->len, ies->data))
8587 			goto fail_unlock_rcu;
8588 	}
8589 
8590 	/* and this pointer is always (unless driver didn't know) beacon data */
8591 	ies = rcu_dereference(res->beacon_ies);
8592 	if (ies && ies->from_beacon) {
8593 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8594 				      NL80211_BSS_PAD))
8595 			goto fail_unlock_rcu;
8596 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8597 					ies->len, ies->data))
8598 			goto fail_unlock_rcu;
8599 	}
8600 	rcu_read_unlock();
8601 
8602 	if (res->beacon_interval &&
8603 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8604 		goto nla_put_failure;
8605 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8606 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8607 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8608 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8609 			jiffies_to_msecs(jiffies - intbss->ts)))
8610 		goto nla_put_failure;
8611 
8612 	if (intbss->parent_tsf &&
8613 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8614 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
8615 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8616 		     intbss->parent_bssid)))
8617 		goto nla_put_failure;
8618 
8619 	if (intbss->ts_boottime &&
8620 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8621 			      intbss->ts_boottime, NL80211_BSS_PAD))
8622 		goto nla_put_failure;
8623 
8624 	if (!nl80211_put_signal(msg, intbss->pub.chains,
8625 				intbss->pub.chain_signal,
8626 				NL80211_BSS_CHAIN_SIGNAL))
8627 		goto nla_put_failure;
8628 
8629 	switch (rdev->wiphy.signal_type) {
8630 	case CFG80211_SIGNAL_TYPE_MBM:
8631 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8632 			goto nla_put_failure;
8633 		break;
8634 	case CFG80211_SIGNAL_TYPE_UNSPEC:
8635 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8636 			goto nla_put_failure;
8637 		break;
8638 	default:
8639 		break;
8640 	}
8641 
8642 	switch (wdev->iftype) {
8643 	case NL80211_IFTYPE_P2P_CLIENT:
8644 	case NL80211_IFTYPE_STATION:
8645 		if (intbss == wdev->current_bss &&
8646 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8647 				NL80211_BSS_STATUS_ASSOCIATED))
8648 			goto nla_put_failure;
8649 		break;
8650 	case NL80211_IFTYPE_ADHOC:
8651 		if (intbss == wdev->current_bss &&
8652 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8653 				NL80211_BSS_STATUS_IBSS_JOINED))
8654 			goto nla_put_failure;
8655 		break;
8656 	default:
8657 		break;
8658 	}
8659 
8660 	nla_nest_end(msg, bss);
8661 
8662 	genlmsg_end(msg, hdr);
8663 	return 0;
8664 
8665  fail_unlock_rcu:
8666 	rcu_read_unlock();
8667  nla_put_failure:
8668 	genlmsg_cancel(msg, hdr);
8669 	return -EMSGSIZE;
8670 }
8671 
8672 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8673 {
8674 	struct cfg80211_registered_device *rdev;
8675 	struct cfg80211_internal_bss *scan;
8676 	struct wireless_dev *wdev;
8677 	int start = cb->args[2], idx = 0;
8678 	int err;
8679 
8680 	rtnl_lock();
8681 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8682 	if (err) {
8683 		rtnl_unlock();
8684 		return err;
8685 	}
8686 
8687 	wdev_lock(wdev);
8688 	spin_lock_bh(&rdev->bss_lock);
8689 
8690 	/*
8691 	 * dump_scan will be called multiple times to break up the scan results
8692 	 * into multiple messages.  It is unlikely that any more bss-es will be
8693 	 * expired after the first call, so only call only call this on the
8694 	 * first dump_scan invocation.
8695 	 */
8696 	if (start == 0)
8697 		cfg80211_bss_expire(rdev);
8698 
8699 	cb->seq = rdev->bss_generation;
8700 
8701 	list_for_each_entry(scan, &rdev->bss_list, list) {
8702 		if (++idx <= start)
8703 			continue;
8704 		if (nl80211_send_bss(skb, cb,
8705 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8706 				rdev, wdev, scan) < 0) {
8707 			idx--;
8708 			break;
8709 		}
8710 	}
8711 
8712 	spin_unlock_bh(&rdev->bss_lock);
8713 	wdev_unlock(wdev);
8714 
8715 	cb->args[2] = idx;
8716 	rtnl_unlock();
8717 
8718 	return skb->len;
8719 }
8720 
8721 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8722 			       int flags, struct net_device *dev,
8723 			       bool allow_radio_stats,
8724 			       struct survey_info *survey)
8725 {
8726 	void *hdr;
8727 	struct nlattr *infoattr;
8728 
8729 	/* skip radio stats if userspace didn't request them */
8730 	if (!survey->channel && !allow_radio_stats)
8731 		return 0;
8732 
8733 	hdr = nl80211hdr_put(msg, portid, seq, flags,
8734 			     NL80211_CMD_NEW_SURVEY_RESULTS);
8735 	if (!hdr)
8736 		return -ENOMEM;
8737 
8738 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8739 		goto nla_put_failure;
8740 
8741 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
8742 	if (!infoattr)
8743 		goto nla_put_failure;
8744 
8745 	if (survey->channel &&
8746 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8747 			survey->channel->center_freq))
8748 		goto nla_put_failure;
8749 
8750 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8751 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8752 		goto nla_put_failure;
8753 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
8754 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8755 		goto nla_put_failure;
8756 	if ((survey->filled & SURVEY_INFO_TIME) &&
8757 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8758 			survey->time, NL80211_SURVEY_INFO_PAD))
8759 		goto nla_put_failure;
8760 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8761 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8762 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
8763 		goto nla_put_failure;
8764 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8765 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8766 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8767 		goto nla_put_failure;
8768 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8769 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8770 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
8771 		goto nla_put_failure;
8772 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8773 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8774 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
8775 		goto nla_put_failure;
8776 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8777 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8778 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
8779 		goto nla_put_failure;
8780 
8781 	nla_nest_end(msg, infoattr);
8782 
8783 	genlmsg_end(msg, hdr);
8784 	return 0;
8785 
8786  nla_put_failure:
8787 	genlmsg_cancel(msg, hdr);
8788 	return -EMSGSIZE;
8789 }
8790 
8791 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8792 {
8793 	struct nlattr **attrbuf;
8794 	struct survey_info survey;
8795 	struct cfg80211_registered_device *rdev;
8796 	struct wireless_dev *wdev;
8797 	int survey_idx = cb->args[2];
8798 	int res;
8799 	bool radio_stats;
8800 
8801 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
8802 	if (!attrbuf)
8803 		return -ENOMEM;
8804 
8805 	rtnl_lock();
8806 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8807 	if (res)
8808 		goto out_err;
8809 
8810 	/* prepare_wdev_dump parsed the attributes */
8811 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8812 
8813 	if (!wdev->netdev) {
8814 		res = -EINVAL;
8815 		goto out_err;
8816 	}
8817 
8818 	if (!rdev->ops->dump_survey) {
8819 		res = -EOPNOTSUPP;
8820 		goto out_err;
8821 	}
8822 
8823 	while (1) {
8824 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8825 		if (res == -ENOENT)
8826 			break;
8827 		if (res)
8828 			goto out_err;
8829 
8830 		/* don't send disabled channels, but do send non-channel data */
8831 		if (survey.channel &&
8832 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8833 			survey_idx++;
8834 			continue;
8835 		}
8836 
8837 		if (nl80211_send_survey(skb,
8838 				NETLINK_CB(cb->skb).portid,
8839 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8840 				wdev->netdev, radio_stats, &survey) < 0)
8841 			goto out;
8842 		survey_idx++;
8843 	}
8844 
8845  out:
8846 	cb->args[2] = survey_idx;
8847 	res = skb->len;
8848  out_err:
8849 	kfree(attrbuf);
8850 	rtnl_unlock();
8851 	return res;
8852 }
8853 
8854 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8855 {
8856 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8857 				  NL80211_WPA_VERSION_2 |
8858 				  NL80211_WPA_VERSION_3));
8859 }
8860 
8861 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8862 {
8863 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8864 	struct net_device *dev = info->user_ptr[1];
8865 	struct ieee80211_channel *chan;
8866 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8867 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
8868 	enum nl80211_auth_type auth_type;
8869 	struct key_parse key;
8870 	bool local_state_change;
8871 
8872 	if (!info->attrs[NL80211_ATTR_MAC])
8873 		return -EINVAL;
8874 
8875 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8876 		return -EINVAL;
8877 
8878 	if (!info->attrs[NL80211_ATTR_SSID])
8879 		return -EINVAL;
8880 
8881 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8882 		return -EINVAL;
8883 
8884 	err = nl80211_parse_key(info, &key);
8885 	if (err)
8886 		return err;
8887 
8888 	if (key.idx >= 0) {
8889 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8890 			return -EINVAL;
8891 		if (!key.p.key || !key.p.key_len)
8892 			return -EINVAL;
8893 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8894 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8895 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8896 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
8897 			return -EINVAL;
8898 		if (key.idx > 3)
8899 			return -EINVAL;
8900 	} else {
8901 		key.p.key_len = 0;
8902 		key.p.key = NULL;
8903 	}
8904 
8905 	if (key.idx >= 0) {
8906 		int i;
8907 		bool ok = false;
8908 
8909 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8910 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8911 				ok = true;
8912 				break;
8913 			}
8914 		}
8915 		if (!ok)
8916 			return -EINVAL;
8917 	}
8918 
8919 	if (!rdev->ops->auth)
8920 		return -EOPNOTSUPP;
8921 
8922 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8923 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8924 		return -EOPNOTSUPP;
8925 
8926 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8927 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8928 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8929 	if (!chan)
8930 		return -EINVAL;
8931 
8932 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8933 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8934 
8935 	if (info->attrs[NL80211_ATTR_IE]) {
8936 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8937 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8938 	}
8939 
8940 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8941 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
8942 		return -EINVAL;
8943 
8944 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
8945 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
8946 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
8947 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
8948 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
8949 		return -EINVAL;
8950 
8951 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
8952 		if (auth_type != NL80211_AUTHTYPE_SAE &&
8953 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
8954 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8955 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
8956 			return -EINVAL;
8957 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8958 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
8959 		/* need to include at least Auth Transaction and Status Code */
8960 		if (auth_data_len < 4)
8961 			return -EINVAL;
8962 	}
8963 
8964 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8965 
8966 	/*
8967 	 * Since we no longer track auth state, ignore
8968 	 * requests to only change local state.
8969 	 */
8970 	if (local_state_change)
8971 		return 0;
8972 
8973 	wdev_lock(dev->ieee80211_ptr);
8974 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8975 				 ssid, ssid_len, ie, ie_len,
8976 				 key.p.key, key.p.key_len, key.idx,
8977 				 auth_data, auth_data_len);
8978 	wdev_unlock(dev->ieee80211_ptr);
8979 	return err;
8980 }
8981 
8982 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
8983 				     struct genl_info *info)
8984 {
8985 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8986 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
8987 		return -EINVAL;
8988 	}
8989 
8990 	if (!rdev->ops->tx_control_port ||
8991 	    !wiphy_ext_feature_isset(&rdev->wiphy,
8992 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
8993 		return -EOPNOTSUPP;
8994 
8995 	return 0;
8996 }
8997 
8998 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8999 				   struct genl_info *info,
9000 				   struct cfg80211_crypto_settings *settings,
9001 				   int cipher_limit)
9002 {
9003 	memset(settings, 0, sizeof(*settings));
9004 
9005 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9006 
9007 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9008 		u16 proto;
9009 
9010 		proto = nla_get_u16(
9011 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9012 		settings->control_port_ethertype = cpu_to_be16(proto);
9013 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9014 		    proto != ETH_P_PAE)
9015 			return -EINVAL;
9016 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9017 			settings->control_port_no_encrypt = true;
9018 	} else
9019 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9020 
9021 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9022 		int r = validate_pae_over_nl80211(rdev, info);
9023 
9024 		if (r < 0)
9025 			return r;
9026 
9027 		settings->control_port_over_nl80211 = true;
9028 	}
9029 
9030 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9031 		void *data;
9032 		int len, i;
9033 
9034 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9035 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9036 		settings->n_ciphers_pairwise = len / sizeof(u32);
9037 
9038 		if (len % sizeof(u32))
9039 			return -EINVAL;
9040 
9041 		if (settings->n_ciphers_pairwise > cipher_limit)
9042 			return -EINVAL;
9043 
9044 		memcpy(settings->ciphers_pairwise, data, len);
9045 
9046 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9047 			if (!cfg80211_supported_cipher_suite(
9048 					&rdev->wiphy,
9049 					settings->ciphers_pairwise[i]))
9050 				return -EINVAL;
9051 	}
9052 
9053 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9054 		settings->cipher_group =
9055 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9056 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9057 						     settings->cipher_group))
9058 			return -EINVAL;
9059 	}
9060 
9061 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9062 		settings->wpa_versions =
9063 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9064 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9065 			return -EINVAL;
9066 	}
9067 
9068 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9069 		void *data;
9070 		int len;
9071 
9072 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9073 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9074 		settings->n_akm_suites = len / sizeof(u32);
9075 
9076 		if (len % sizeof(u32))
9077 			return -EINVAL;
9078 
9079 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9080 			return -EINVAL;
9081 
9082 		memcpy(settings->akm_suites, data, len);
9083 	}
9084 
9085 	if (info->attrs[NL80211_ATTR_PMK]) {
9086 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9087 			return -EINVAL;
9088 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9089 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9090 			return -EINVAL;
9091 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9092 	}
9093 
9094 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9095 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9096 					     NL80211_EXT_FEATURE_SAE_OFFLOAD))
9097 			return -EINVAL;
9098 		settings->sae_pwd =
9099 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9100 		settings->sae_pwd_len =
9101 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9102 	}
9103 
9104 	return 0;
9105 }
9106 
9107 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9108 {
9109 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9110 	struct net_device *dev = info->user_ptr[1];
9111 	struct ieee80211_channel *chan;
9112 	struct cfg80211_assoc_request req = {};
9113 	const u8 *bssid, *ssid;
9114 	int err, ssid_len = 0;
9115 
9116 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9117 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9118 		return -EPERM;
9119 
9120 	if (!info->attrs[NL80211_ATTR_MAC] ||
9121 	    !info->attrs[NL80211_ATTR_SSID] ||
9122 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9123 		return -EINVAL;
9124 
9125 	if (!rdev->ops->assoc)
9126 		return -EOPNOTSUPP;
9127 
9128 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9129 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9130 		return -EOPNOTSUPP;
9131 
9132 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9133 
9134 	chan = nl80211_get_valid_chan(&rdev->wiphy,
9135 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9136 	if (!chan)
9137 		return -EINVAL;
9138 
9139 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9140 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9141 
9142 	if (info->attrs[NL80211_ATTR_IE]) {
9143 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9144 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9145 	}
9146 
9147 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9148 		enum nl80211_mfp mfp =
9149 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9150 		if (mfp == NL80211_MFP_REQUIRED)
9151 			req.use_mfp = true;
9152 		else if (mfp != NL80211_MFP_NO)
9153 			return -EINVAL;
9154 	}
9155 
9156 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9157 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9158 
9159 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9160 		req.flags |= ASSOC_REQ_DISABLE_HT;
9161 
9162 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9163 		memcpy(&req.ht_capa_mask,
9164 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9165 		       sizeof(req.ht_capa_mask));
9166 
9167 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9168 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9169 			return -EINVAL;
9170 		memcpy(&req.ht_capa,
9171 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9172 		       sizeof(req.ht_capa));
9173 	}
9174 
9175 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9176 		req.flags |= ASSOC_REQ_DISABLE_VHT;
9177 
9178 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9179 		memcpy(&req.vht_capa_mask,
9180 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9181 		       sizeof(req.vht_capa_mask));
9182 
9183 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9184 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9185 			return -EINVAL;
9186 		memcpy(&req.vht_capa,
9187 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9188 		       sizeof(req.vht_capa));
9189 	}
9190 
9191 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9192 		if (!((rdev->wiphy.features &
9193 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9194 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9195 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9196 					     NL80211_EXT_FEATURE_RRM))
9197 			return -EINVAL;
9198 		req.flags |= ASSOC_REQ_USE_RRM;
9199 	}
9200 
9201 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9202 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9203 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9204 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9205 			return -EINVAL;
9206 		req.fils_nonces =
9207 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9208 	}
9209 
9210 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9211 	if (!err) {
9212 		wdev_lock(dev->ieee80211_ptr);
9213 
9214 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9215 					  ssid, ssid_len, &req);
9216 
9217 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9218 			dev->ieee80211_ptr->conn_owner_nlportid =
9219 				info->snd_portid;
9220 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9221 			       bssid, ETH_ALEN);
9222 		}
9223 
9224 		wdev_unlock(dev->ieee80211_ptr);
9225 	}
9226 
9227 	return err;
9228 }
9229 
9230 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9231 {
9232 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9233 	struct net_device *dev = info->user_ptr[1];
9234 	const u8 *ie = NULL, *bssid;
9235 	int ie_len = 0, err;
9236 	u16 reason_code;
9237 	bool local_state_change;
9238 
9239 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9240 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9241 		return -EPERM;
9242 
9243 	if (!info->attrs[NL80211_ATTR_MAC])
9244 		return -EINVAL;
9245 
9246 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9247 		return -EINVAL;
9248 
9249 	if (!rdev->ops->deauth)
9250 		return -EOPNOTSUPP;
9251 
9252 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9253 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9254 		return -EOPNOTSUPP;
9255 
9256 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9257 
9258 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9259 	if (reason_code == 0) {
9260 		/* Reason Code 0 is reserved */
9261 		return -EINVAL;
9262 	}
9263 
9264 	if (info->attrs[NL80211_ATTR_IE]) {
9265 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9266 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9267 	}
9268 
9269 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9270 
9271 	wdev_lock(dev->ieee80211_ptr);
9272 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9273 				   local_state_change);
9274 	wdev_unlock(dev->ieee80211_ptr);
9275 	return err;
9276 }
9277 
9278 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9279 {
9280 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9281 	struct net_device *dev = info->user_ptr[1];
9282 	const u8 *ie = NULL, *bssid;
9283 	int ie_len = 0, err;
9284 	u16 reason_code;
9285 	bool local_state_change;
9286 
9287 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9288 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9289 		return -EPERM;
9290 
9291 	if (!info->attrs[NL80211_ATTR_MAC])
9292 		return -EINVAL;
9293 
9294 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9295 		return -EINVAL;
9296 
9297 	if (!rdev->ops->disassoc)
9298 		return -EOPNOTSUPP;
9299 
9300 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9301 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9302 		return -EOPNOTSUPP;
9303 
9304 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9305 
9306 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9307 	if (reason_code == 0) {
9308 		/* Reason Code 0 is reserved */
9309 		return -EINVAL;
9310 	}
9311 
9312 	if (info->attrs[NL80211_ATTR_IE]) {
9313 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9314 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9315 	}
9316 
9317 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9318 
9319 	wdev_lock(dev->ieee80211_ptr);
9320 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9321 				     local_state_change);
9322 	wdev_unlock(dev->ieee80211_ptr);
9323 	return err;
9324 }
9325 
9326 static bool
9327 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9328 			 int mcast_rate[NUM_NL80211_BANDS],
9329 			 int rateval)
9330 {
9331 	struct wiphy *wiphy = &rdev->wiphy;
9332 	bool found = false;
9333 	int band, i;
9334 
9335 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9336 		struct ieee80211_supported_band *sband;
9337 
9338 		sband = wiphy->bands[band];
9339 		if (!sband)
9340 			continue;
9341 
9342 		for (i = 0; i < sband->n_bitrates; i++) {
9343 			if (sband->bitrates[i].bitrate == rateval) {
9344 				mcast_rate[band] = i + 1;
9345 				found = true;
9346 				break;
9347 			}
9348 		}
9349 	}
9350 
9351 	return found;
9352 }
9353 
9354 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
9355 {
9356 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9357 	struct net_device *dev = info->user_ptr[1];
9358 	struct cfg80211_ibss_params ibss;
9359 	struct wiphy *wiphy;
9360 	struct cfg80211_cached_keys *connkeys = NULL;
9361 	int err;
9362 
9363 	memset(&ibss, 0, sizeof(ibss));
9364 
9365 	if (!info->attrs[NL80211_ATTR_SSID] ||
9366 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9367 		return -EINVAL;
9368 
9369 	ibss.beacon_interval = 100;
9370 
9371 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9372 		ibss.beacon_interval =
9373 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9374 
9375 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9376 					   ibss.beacon_interval);
9377 	if (err)
9378 		return err;
9379 
9380 	if (!rdev->ops->join_ibss)
9381 		return -EOPNOTSUPP;
9382 
9383 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9384 		return -EOPNOTSUPP;
9385 
9386 	wiphy = &rdev->wiphy;
9387 
9388 	if (info->attrs[NL80211_ATTR_MAC]) {
9389 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9390 
9391 		if (!is_valid_ether_addr(ibss.bssid))
9392 			return -EINVAL;
9393 	}
9394 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9395 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9396 
9397 	if (info->attrs[NL80211_ATTR_IE]) {
9398 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9399 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9400 	}
9401 
9402 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9403 	if (err)
9404 		return err;
9405 
9406 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9407 				     NL80211_IFTYPE_ADHOC))
9408 		return -EINVAL;
9409 
9410 	switch (ibss.chandef.width) {
9411 	case NL80211_CHAN_WIDTH_5:
9412 	case NL80211_CHAN_WIDTH_10:
9413 	case NL80211_CHAN_WIDTH_20_NOHT:
9414 		break;
9415 	case NL80211_CHAN_WIDTH_20:
9416 	case NL80211_CHAN_WIDTH_40:
9417 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9418 			return -EINVAL;
9419 		break;
9420 	case NL80211_CHAN_WIDTH_80:
9421 	case NL80211_CHAN_WIDTH_80P80:
9422 	case NL80211_CHAN_WIDTH_160:
9423 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9424 			return -EINVAL;
9425 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9426 					     NL80211_EXT_FEATURE_VHT_IBSS))
9427 			return -EINVAL;
9428 		break;
9429 	default:
9430 		return -EINVAL;
9431 	}
9432 
9433 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9434 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9435 
9436 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9437 		u8 *rates =
9438 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9439 		int n_rates =
9440 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9441 		struct ieee80211_supported_band *sband =
9442 			wiphy->bands[ibss.chandef.chan->band];
9443 
9444 		err = ieee80211_get_ratemask(sband, rates, n_rates,
9445 					     &ibss.basic_rates);
9446 		if (err)
9447 			return err;
9448 	}
9449 
9450 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9451 		memcpy(&ibss.ht_capa_mask,
9452 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9453 		       sizeof(ibss.ht_capa_mask));
9454 
9455 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9456 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9457 			return -EINVAL;
9458 		memcpy(&ibss.ht_capa,
9459 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9460 		       sizeof(ibss.ht_capa));
9461 	}
9462 
9463 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9464 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9465 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9466 		return -EINVAL;
9467 
9468 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9469 		bool no_ht = false;
9470 
9471 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9472 		if (IS_ERR(connkeys))
9473 			return PTR_ERR(connkeys);
9474 
9475 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9476 		    no_ht) {
9477 			kzfree(connkeys);
9478 			return -EINVAL;
9479 		}
9480 	}
9481 
9482 	ibss.control_port =
9483 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9484 
9485 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9486 		int r = validate_pae_over_nl80211(rdev, info);
9487 
9488 		if (r < 0) {
9489 			kzfree(connkeys);
9490 			return r;
9491 		}
9492 
9493 		ibss.control_port_over_nl80211 = true;
9494 	}
9495 
9496 	ibss.userspace_handles_dfs =
9497 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9498 
9499 	wdev_lock(dev->ieee80211_ptr);
9500 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9501 	if (err)
9502 		kzfree(connkeys);
9503 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9504 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9505 	wdev_unlock(dev->ieee80211_ptr);
9506 
9507 	return err;
9508 }
9509 
9510 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9511 {
9512 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9513 	struct net_device *dev = info->user_ptr[1];
9514 
9515 	if (!rdev->ops->leave_ibss)
9516 		return -EOPNOTSUPP;
9517 
9518 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9519 		return -EOPNOTSUPP;
9520 
9521 	return cfg80211_leave_ibss(rdev, dev, false);
9522 }
9523 
9524 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9525 {
9526 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9527 	struct net_device *dev = info->user_ptr[1];
9528 	int mcast_rate[NUM_NL80211_BANDS];
9529 	u32 nla_rate;
9530 	int err;
9531 
9532 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9533 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9534 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9535 		return -EOPNOTSUPP;
9536 
9537 	if (!rdev->ops->set_mcast_rate)
9538 		return -EOPNOTSUPP;
9539 
9540 	memset(mcast_rate, 0, sizeof(mcast_rate));
9541 
9542 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9543 		return -EINVAL;
9544 
9545 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9546 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9547 		return -EINVAL;
9548 
9549 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9550 
9551 	return err;
9552 }
9553 
9554 static struct sk_buff *
9555 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9556 			    struct wireless_dev *wdev, int approxlen,
9557 			    u32 portid, u32 seq, enum nl80211_commands cmd,
9558 			    enum nl80211_attrs attr,
9559 			    const struct nl80211_vendor_cmd_info *info,
9560 			    gfp_t gfp)
9561 {
9562 	struct sk_buff *skb;
9563 	void *hdr;
9564 	struct nlattr *data;
9565 
9566 	skb = nlmsg_new(approxlen + 100, gfp);
9567 	if (!skb)
9568 		return NULL;
9569 
9570 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9571 	if (!hdr) {
9572 		kfree_skb(skb);
9573 		return NULL;
9574 	}
9575 
9576 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9577 		goto nla_put_failure;
9578 
9579 	if (info) {
9580 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9581 				info->vendor_id))
9582 			goto nla_put_failure;
9583 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9584 				info->subcmd))
9585 			goto nla_put_failure;
9586 	}
9587 
9588 	if (wdev) {
9589 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9590 				      wdev_id(wdev), NL80211_ATTR_PAD))
9591 			goto nla_put_failure;
9592 		if (wdev->netdev &&
9593 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9594 				wdev->netdev->ifindex))
9595 			goto nla_put_failure;
9596 	}
9597 
9598 	data = nla_nest_start_noflag(skb, attr);
9599 	if (!data)
9600 		goto nla_put_failure;
9601 
9602 	((void **)skb->cb)[0] = rdev;
9603 	((void **)skb->cb)[1] = hdr;
9604 	((void **)skb->cb)[2] = data;
9605 
9606 	return skb;
9607 
9608  nla_put_failure:
9609 	kfree_skb(skb);
9610 	return NULL;
9611 }
9612 
9613 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9614 					   struct wireless_dev *wdev,
9615 					   enum nl80211_commands cmd,
9616 					   enum nl80211_attrs attr,
9617 					   unsigned int portid,
9618 					   int vendor_event_idx,
9619 					   int approxlen, gfp_t gfp)
9620 {
9621 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9622 	const struct nl80211_vendor_cmd_info *info;
9623 
9624 	switch (cmd) {
9625 	case NL80211_CMD_TESTMODE:
9626 		if (WARN_ON(vendor_event_idx != -1))
9627 			return NULL;
9628 		info = NULL;
9629 		break;
9630 	case NL80211_CMD_VENDOR:
9631 		if (WARN_ON(vendor_event_idx < 0 ||
9632 			    vendor_event_idx >= wiphy->n_vendor_events))
9633 			return NULL;
9634 		info = &wiphy->vendor_events[vendor_event_idx];
9635 		break;
9636 	default:
9637 		WARN_ON(1);
9638 		return NULL;
9639 	}
9640 
9641 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9642 					   cmd, attr, info, gfp);
9643 }
9644 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9645 
9646 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9647 {
9648 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9649 	void *hdr = ((void **)skb->cb)[1];
9650 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
9651 	struct nlattr *data = ((void **)skb->cb)[2];
9652 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9653 
9654 	/* clear CB data for netlink core to own from now on */
9655 	memset(skb->cb, 0, sizeof(skb->cb));
9656 
9657 	nla_nest_end(skb, data);
9658 	genlmsg_end(skb, hdr);
9659 
9660 	if (nlhdr->nlmsg_pid) {
9661 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
9662 				nlhdr->nlmsg_pid);
9663 	} else {
9664 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9665 			mcgrp = NL80211_MCGRP_VENDOR;
9666 
9667 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
9668 					skb, 0, mcgrp, gfp);
9669 	}
9670 }
9671 EXPORT_SYMBOL(__cfg80211_send_event_skb);
9672 
9673 #ifdef CONFIG_NL80211_TESTMODE
9674 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9675 {
9676 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9677 	struct wireless_dev *wdev =
9678 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9679 	int err;
9680 
9681 	if (!rdev->ops->testmode_cmd)
9682 		return -EOPNOTSUPP;
9683 
9684 	if (IS_ERR(wdev)) {
9685 		err = PTR_ERR(wdev);
9686 		if (err != -EINVAL)
9687 			return err;
9688 		wdev = NULL;
9689 	} else if (wdev->wiphy != &rdev->wiphy) {
9690 		return -EINVAL;
9691 	}
9692 
9693 	if (!info->attrs[NL80211_ATTR_TESTDATA])
9694 		return -EINVAL;
9695 
9696 	rdev->cur_cmd_info = info;
9697 	err = rdev_testmode_cmd(rdev, wdev,
9698 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9699 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9700 	rdev->cur_cmd_info = NULL;
9701 
9702 	return err;
9703 }
9704 
9705 static int nl80211_testmode_dump(struct sk_buff *skb,
9706 				 struct netlink_callback *cb)
9707 {
9708 	struct cfg80211_registered_device *rdev;
9709 	struct nlattr **attrbuf = NULL;
9710 	int err;
9711 	long phy_idx;
9712 	void *data = NULL;
9713 	int data_len = 0;
9714 
9715 	rtnl_lock();
9716 
9717 	if (cb->args[0]) {
9718 		/*
9719 		 * 0 is a valid index, but not valid for args[0],
9720 		 * so we need to offset by 1.
9721 		 */
9722 		phy_idx = cb->args[0] - 1;
9723 
9724 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9725 		if (!rdev) {
9726 			err = -ENOENT;
9727 			goto out_err;
9728 		}
9729 	} else {
9730 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
9731 				  GFP_KERNEL);
9732 		if (!attrbuf) {
9733 			err = -ENOMEM;
9734 			goto out_err;
9735 		}
9736 
9737 		err = nlmsg_parse_deprecated(cb->nlh,
9738 					     GENL_HDRLEN + nl80211_fam.hdrsize,
9739 					     attrbuf, nl80211_fam.maxattr,
9740 					     nl80211_policy, NULL);
9741 		if (err)
9742 			goto out_err;
9743 
9744 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9745 		if (IS_ERR(rdev)) {
9746 			err = PTR_ERR(rdev);
9747 			goto out_err;
9748 		}
9749 		phy_idx = rdev->wiphy_idx;
9750 
9751 		if (attrbuf[NL80211_ATTR_TESTDATA])
9752 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9753 	}
9754 
9755 	if (cb->args[1]) {
9756 		data = nla_data((void *)cb->args[1]);
9757 		data_len = nla_len((void *)cb->args[1]);
9758 	}
9759 
9760 	if (!rdev->ops->testmode_dump) {
9761 		err = -EOPNOTSUPP;
9762 		goto out_err;
9763 	}
9764 
9765 	while (1) {
9766 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9767 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
9768 					   NL80211_CMD_TESTMODE);
9769 		struct nlattr *tmdata;
9770 
9771 		if (!hdr)
9772 			break;
9773 
9774 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9775 			genlmsg_cancel(skb, hdr);
9776 			break;
9777 		}
9778 
9779 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
9780 		if (!tmdata) {
9781 			genlmsg_cancel(skb, hdr);
9782 			break;
9783 		}
9784 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9785 		nla_nest_end(skb, tmdata);
9786 
9787 		if (err == -ENOBUFS || err == -ENOENT) {
9788 			genlmsg_cancel(skb, hdr);
9789 			break;
9790 		} else if (err) {
9791 			genlmsg_cancel(skb, hdr);
9792 			goto out_err;
9793 		}
9794 
9795 		genlmsg_end(skb, hdr);
9796 	}
9797 
9798 	err = skb->len;
9799 	/* see above */
9800 	cb->args[0] = phy_idx + 1;
9801  out_err:
9802 	kfree(attrbuf);
9803 	rtnl_unlock();
9804 	return err;
9805 }
9806 #endif
9807 
9808 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9809 {
9810 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9811 	struct net_device *dev = info->user_ptr[1];
9812 	struct cfg80211_connect_params connect;
9813 	struct wiphy *wiphy;
9814 	struct cfg80211_cached_keys *connkeys = NULL;
9815 	int err;
9816 
9817 	memset(&connect, 0, sizeof(connect));
9818 
9819 	if (!info->attrs[NL80211_ATTR_SSID] ||
9820 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9821 		return -EINVAL;
9822 
9823 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9824 		connect.auth_type =
9825 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9826 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9827 					     NL80211_CMD_CONNECT))
9828 			return -EINVAL;
9829 	} else
9830 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9831 
9832 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9833 
9834 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9835 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9836 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9837 		return -EINVAL;
9838 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9839 
9840 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9841 				      NL80211_MAX_NR_CIPHER_SUITES);
9842 	if (err)
9843 		return err;
9844 
9845 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9846 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9847 		return -EOPNOTSUPP;
9848 
9849 	wiphy = &rdev->wiphy;
9850 
9851 	connect.bg_scan_period = -1;
9852 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9853 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9854 		connect.bg_scan_period =
9855 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9856 	}
9857 
9858 	if (info->attrs[NL80211_ATTR_MAC])
9859 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9860 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
9861 		connect.bssid_hint =
9862 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9863 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9864 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9865 
9866 	if (info->attrs[NL80211_ATTR_IE]) {
9867 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9868 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9869 	}
9870 
9871 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9872 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9873 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
9874 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9875 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
9876 			return -EOPNOTSUPP;
9877 	} else {
9878 		connect.mfp = NL80211_MFP_NO;
9879 	}
9880 
9881 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9882 		connect.prev_bssid =
9883 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9884 
9885 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9886 		connect.channel = nl80211_get_valid_chan(
9887 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9888 		if (!connect.channel)
9889 			return -EINVAL;
9890 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9891 		connect.channel_hint = nl80211_get_valid_chan(
9892 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9893 		if (!connect.channel_hint)
9894 			return -EINVAL;
9895 	}
9896 
9897 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9898 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9899 		if (IS_ERR(connkeys))
9900 			return PTR_ERR(connkeys);
9901 	}
9902 
9903 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9904 		connect.flags |= ASSOC_REQ_DISABLE_HT;
9905 
9906 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9907 		memcpy(&connect.ht_capa_mask,
9908 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9909 		       sizeof(connect.ht_capa_mask));
9910 
9911 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9912 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9913 			kzfree(connkeys);
9914 			return -EINVAL;
9915 		}
9916 		memcpy(&connect.ht_capa,
9917 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9918 		       sizeof(connect.ht_capa));
9919 	}
9920 
9921 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9922 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
9923 
9924 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9925 		memcpy(&connect.vht_capa_mask,
9926 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9927 		       sizeof(connect.vht_capa_mask));
9928 
9929 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9930 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
9931 			kzfree(connkeys);
9932 			return -EINVAL;
9933 		}
9934 		memcpy(&connect.vht_capa,
9935 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9936 		       sizeof(connect.vht_capa));
9937 	}
9938 
9939 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9940 		if (!((rdev->wiphy.features &
9941 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9942 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9943 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9944 					     NL80211_EXT_FEATURE_RRM)) {
9945 			kzfree(connkeys);
9946 			return -EINVAL;
9947 		}
9948 		connect.flags |= ASSOC_REQ_USE_RRM;
9949 	}
9950 
9951 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
9952 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
9953 		kzfree(connkeys);
9954 		return -EOPNOTSUPP;
9955 	}
9956 
9957 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
9958 		/* bss selection makes no sense if bssid is set */
9959 		if (connect.bssid) {
9960 			kzfree(connkeys);
9961 			return -EINVAL;
9962 		}
9963 
9964 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
9965 				       wiphy, &connect.bss_select);
9966 		if (err) {
9967 			kzfree(connkeys);
9968 			return err;
9969 		}
9970 	}
9971 
9972 	if (wiphy_ext_feature_isset(&rdev->wiphy,
9973 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
9974 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9975 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9976 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9977 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9978 		connect.fils_erp_username =
9979 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9980 		connect.fils_erp_username_len =
9981 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9982 		connect.fils_erp_realm =
9983 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9984 		connect.fils_erp_realm_len =
9985 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9986 		connect.fils_erp_next_seq_num =
9987 			nla_get_u16(
9988 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9989 		connect.fils_erp_rrk =
9990 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9991 		connect.fils_erp_rrk_len =
9992 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9993 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9994 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9995 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9996 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9997 		kzfree(connkeys);
9998 		return -EINVAL;
9999 	}
10000 
10001 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10002 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10003 			kzfree(connkeys);
10004 			GENL_SET_ERR_MSG(info,
10005 					 "external auth requires connection ownership");
10006 			return -EINVAL;
10007 		}
10008 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10009 	}
10010 
10011 	wdev_lock(dev->ieee80211_ptr);
10012 
10013 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10014 			       connect.prev_bssid);
10015 	if (err)
10016 		kzfree(connkeys);
10017 
10018 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10019 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10020 		if (connect.bssid)
10021 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10022 			       connect.bssid, ETH_ALEN);
10023 		else
10024 			memset(dev->ieee80211_ptr->disconnect_bssid,
10025 			       0, ETH_ALEN);
10026 	}
10027 
10028 	wdev_unlock(dev->ieee80211_ptr);
10029 
10030 	return err;
10031 }
10032 
10033 static int nl80211_update_connect_params(struct sk_buff *skb,
10034 					 struct genl_info *info)
10035 {
10036 	struct cfg80211_connect_params connect = {};
10037 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10038 	struct net_device *dev = info->user_ptr[1];
10039 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10040 	bool fils_sk_offload;
10041 	u32 auth_type;
10042 	u32 changed = 0;
10043 	int ret;
10044 
10045 	if (!rdev->ops->update_connect_params)
10046 		return -EOPNOTSUPP;
10047 
10048 	if (info->attrs[NL80211_ATTR_IE]) {
10049 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10050 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10051 		changed |= UPDATE_ASSOC_IES;
10052 	}
10053 
10054 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10055 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10056 
10057 	/*
10058 	 * when driver supports fils-sk offload all attributes must be
10059 	 * provided. So the else covers "fils-sk-not-all" and
10060 	 * "no-fils-sk-any".
10061 	 */
10062 	if (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 		changed |= UPDATE_FILS_ERP_INFO;
10083 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10084 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10085 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10086 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10087 		return -EINVAL;
10088 	}
10089 
10090 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10091 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10092 		if (!nl80211_valid_auth_type(rdev, auth_type,
10093 					     NL80211_CMD_CONNECT))
10094 			return -EINVAL;
10095 
10096 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10097 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10098 			return -EINVAL;
10099 
10100 		connect.auth_type = auth_type;
10101 		changed |= UPDATE_AUTH_TYPE;
10102 	}
10103 
10104 	wdev_lock(dev->ieee80211_ptr);
10105 	if (!wdev->current_bss)
10106 		ret = -ENOLINK;
10107 	else
10108 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10109 	wdev_unlock(dev->ieee80211_ptr);
10110 
10111 	return ret;
10112 }
10113 
10114 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10115 {
10116 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10117 	struct net_device *dev = info->user_ptr[1];
10118 	u16 reason;
10119 	int ret;
10120 
10121 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10122 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10123 		return -EPERM;
10124 
10125 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10126 		reason = WLAN_REASON_DEAUTH_LEAVING;
10127 	else
10128 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10129 
10130 	if (reason == 0)
10131 		return -EINVAL;
10132 
10133 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10134 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10135 		return -EOPNOTSUPP;
10136 
10137 	wdev_lock(dev->ieee80211_ptr);
10138 	ret = cfg80211_disconnect(rdev, dev, reason, true);
10139 	wdev_unlock(dev->ieee80211_ptr);
10140 	return ret;
10141 }
10142 
10143 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10144 {
10145 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10146 	struct net *net;
10147 	int err;
10148 
10149 	if (info->attrs[NL80211_ATTR_PID]) {
10150 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10151 
10152 		net = get_net_ns_by_pid(pid);
10153 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10154 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10155 
10156 		net = get_net_ns_by_fd(fd);
10157 	} else {
10158 		return -EINVAL;
10159 	}
10160 
10161 	if (IS_ERR(net))
10162 		return PTR_ERR(net);
10163 
10164 	err = 0;
10165 
10166 	/* check if anything to do */
10167 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
10168 		err = cfg80211_switch_netns(rdev, net);
10169 
10170 	put_net(net);
10171 	return err;
10172 }
10173 
10174 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10175 {
10176 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10177 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10178 			struct cfg80211_pmksa *pmksa) = NULL;
10179 	struct net_device *dev = info->user_ptr[1];
10180 	struct cfg80211_pmksa pmksa;
10181 
10182 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10183 
10184 	if (!info->attrs[NL80211_ATTR_PMKID])
10185 		return -EINVAL;
10186 
10187 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10188 
10189 	if (info->attrs[NL80211_ATTR_MAC]) {
10190 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10191 	} else if (info->attrs[NL80211_ATTR_SSID] &&
10192 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10193 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10194 		    info->attrs[NL80211_ATTR_PMK])) {
10195 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10196 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10197 		pmksa.cache_id =
10198 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10199 	} else {
10200 		return -EINVAL;
10201 	}
10202 	if (info->attrs[NL80211_ATTR_PMK]) {
10203 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10204 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10205 	}
10206 
10207 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10208 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10209 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10210 	      wiphy_ext_feature_isset(&rdev->wiphy,
10211 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10212 		return -EOPNOTSUPP;
10213 
10214 	switch (info->genlhdr->cmd) {
10215 	case NL80211_CMD_SET_PMKSA:
10216 		rdev_ops = rdev->ops->set_pmksa;
10217 		break;
10218 	case NL80211_CMD_DEL_PMKSA:
10219 		rdev_ops = rdev->ops->del_pmksa;
10220 		break;
10221 	default:
10222 		WARN_ON(1);
10223 		break;
10224 	}
10225 
10226 	if (!rdev_ops)
10227 		return -EOPNOTSUPP;
10228 
10229 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
10230 }
10231 
10232 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10233 {
10234 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10235 	struct net_device *dev = info->user_ptr[1];
10236 
10237 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10238 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10239 		return -EOPNOTSUPP;
10240 
10241 	if (!rdev->ops->flush_pmksa)
10242 		return -EOPNOTSUPP;
10243 
10244 	return rdev_flush_pmksa(rdev, dev);
10245 }
10246 
10247 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10248 {
10249 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10250 	struct net_device *dev = info->user_ptr[1];
10251 	u8 action_code, dialog_token;
10252 	u32 peer_capability = 0;
10253 	u16 status_code;
10254 	u8 *peer;
10255 	bool initiator;
10256 
10257 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10258 	    !rdev->ops->tdls_mgmt)
10259 		return -EOPNOTSUPP;
10260 
10261 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10262 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10263 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10264 	    !info->attrs[NL80211_ATTR_IE] ||
10265 	    !info->attrs[NL80211_ATTR_MAC])
10266 		return -EINVAL;
10267 
10268 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10269 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10270 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10271 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10272 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10273 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10274 		peer_capability =
10275 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10276 
10277 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10278 			      dialog_token, status_code, peer_capability,
10279 			      initiator,
10280 			      nla_data(info->attrs[NL80211_ATTR_IE]),
10281 			      nla_len(info->attrs[NL80211_ATTR_IE]));
10282 }
10283 
10284 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10285 {
10286 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10287 	struct net_device *dev = info->user_ptr[1];
10288 	enum nl80211_tdls_operation operation;
10289 	u8 *peer;
10290 
10291 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10292 	    !rdev->ops->tdls_oper)
10293 		return -EOPNOTSUPP;
10294 
10295 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10296 	    !info->attrs[NL80211_ATTR_MAC])
10297 		return -EINVAL;
10298 
10299 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10300 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10301 
10302 	return rdev_tdls_oper(rdev, dev, peer, operation);
10303 }
10304 
10305 static int nl80211_remain_on_channel(struct sk_buff *skb,
10306 				     struct genl_info *info)
10307 {
10308 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10309 	struct wireless_dev *wdev = info->user_ptr[1];
10310 	struct cfg80211_chan_def chandef;
10311 	const struct cfg80211_chan_def *compat_chandef;
10312 	struct sk_buff *msg;
10313 	void *hdr;
10314 	u64 cookie;
10315 	u32 duration;
10316 	int err;
10317 
10318 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10319 	    !info->attrs[NL80211_ATTR_DURATION])
10320 		return -EINVAL;
10321 
10322 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10323 
10324 	if (!rdev->ops->remain_on_channel ||
10325 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10326 		return -EOPNOTSUPP;
10327 
10328 	/*
10329 	 * We should be on that channel for at least a minimum amount of
10330 	 * time (10ms) but no longer than the driver supports.
10331 	 */
10332 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10333 	    duration > rdev->wiphy.max_remain_on_channel_duration)
10334 		return -EINVAL;
10335 
10336 	err = nl80211_parse_chandef(rdev, info, &chandef);
10337 	if (err)
10338 		return err;
10339 
10340 	wdev_lock(wdev);
10341 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
10342 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10343 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10344 							     &chandef);
10345 		if (compat_chandef != &chandef) {
10346 			wdev_unlock(wdev);
10347 			return -EBUSY;
10348 		}
10349 	}
10350 	wdev_unlock(wdev);
10351 
10352 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10353 	if (!msg)
10354 		return -ENOMEM;
10355 
10356 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10357 			     NL80211_CMD_REMAIN_ON_CHANNEL);
10358 	if (!hdr) {
10359 		err = -ENOBUFS;
10360 		goto free_msg;
10361 	}
10362 
10363 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10364 				     duration, &cookie);
10365 
10366 	if (err)
10367 		goto free_msg;
10368 
10369 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10370 			      NL80211_ATTR_PAD))
10371 		goto nla_put_failure;
10372 
10373 	genlmsg_end(msg, hdr);
10374 
10375 	return genlmsg_reply(msg, info);
10376 
10377  nla_put_failure:
10378 	err = -ENOBUFS;
10379  free_msg:
10380 	nlmsg_free(msg);
10381 	return err;
10382 }
10383 
10384 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10385 					    struct genl_info *info)
10386 {
10387 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10388 	struct wireless_dev *wdev = info->user_ptr[1];
10389 	u64 cookie;
10390 
10391 	if (!info->attrs[NL80211_ATTR_COOKIE])
10392 		return -EINVAL;
10393 
10394 	if (!rdev->ops->cancel_remain_on_channel)
10395 		return -EOPNOTSUPP;
10396 
10397 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10398 
10399 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10400 }
10401 
10402 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10403 				       struct genl_info *info)
10404 {
10405 	struct cfg80211_bitrate_mask mask;
10406 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10407 	struct net_device *dev = info->user_ptr[1];
10408 	int err;
10409 
10410 	if (!rdev->ops->set_bitrate_mask)
10411 		return -EOPNOTSUPP;
10412 
10413 	err = nl80211_parse_tx_bitrate_mask(info, &mask);
10414 	if (err)
10415 		return err;
10416 
10417 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10418 }
10419 
10420 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10421 {
10422 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10423 	struct wireless_dev *wdev = info->user_ptr[1];
10424 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10425 
10426 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10427 		return -EINVAL;
10428 
10429 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10430 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10431 
10432 	switch (wdev->iftype) {
10433 	case NL80211_IFTYPE_STATION:
10434 	case NL80211_IFTYPE_ADHOC:
10435 	case NL80211_IFTYPE_P2P_CLIENT:
10436 	case NL80211_IFTYPE_AP:
10437 	case NL80211_IFTYPE_AP_VLAN:
10438 	case NL80211_IFTYPE_MESH_POINT:
10439 	case NL80211_IFTYPE_P2P_GO:
10440 	case NL80211_IFTYPE_P2P_DEVICE:
10441 		break;
10442 	case NL80211_IFTYPE_NAN:
10443 	default:
10444 		return -EOPNOTSUPP;
10445 	}
10446 
10447 	/* not much point in registering if we can't reply */
10448 	if (!rdev->ops->mgmt_tx)
10449 		return -EOPNOTSUPP;
10450 
10451 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10452 			nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10453 			nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10454 }
10455 
10456 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10457 {
10458 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10459 	struct wireless_dev *wdev = info->user_ptr[1];
10460 	struct cfg80211_chan_def chandef;
10461 	int err;
10462 	void *hdr = NULL;
10463 	u64 cookie;
10464 	struct sk_buff *msg = NULL;
10465 	struct cfg80211_mgmt_tx_params params = {
10466 		.dont_wait_for_ack =
10467 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10468 	};
10469 
10470 	if (!info->attrs[NL80211_ATTR_FRAME])
10471 		return -EINVAL;
10472 
10473 	if (!rdev->ops->mgmt_tx)
10474 		return -EOPNOTSUPP;
10475 
10476 	switch (wdev->iftype) {
10477 	case NL80211_IFTYPE_P2P_DEVICE:
10478 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10479 			return -EINVAL;
10480 	case NL80211_IFTYPE_STATION:
10481 	case NL80211_IFTYPE_ADHOC:
10482 	case NL80211_IFTYPE_P2P_CLIENT:
10483 	case NL80211_IFTYPE_AP:
10484 	case NL80211_IFTYPE_AP_VLAN:
10485 	case NL80211_IFTYPE_MESH_POINT:
10486 	case NL80211_IFTYPE_P2P_GO:
10487 		break;
10488 	case NL80211_IFTYPE_NAN:
10489 	default:
10490 		return -EOPNOTSUPP;
10491 	}
10492 
10493 	if (info->attrs[NL80211_ATTR_DURATION]) {
10494 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10495 			return -EINVAL;
10496 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10497 
10498 		/*
10499 		 * We should wait on the channel for at least a minimum amount
10500 		 * of time (10ms) but no longer than the driver supports.
10501 		 */
10502 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10503 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
10504 			return -EINVAL;
10505 	}
10506 
10507 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10508 
10509 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10510 		return -EINVAL;
10511 
10512 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10513 
10514 	/* get the channel if any has been specified, otherwise pass NULL to
10515 	 * the driver. The latter will use the current one
10516 	 */
10517 	chandef.chan = NULL;
10518 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10519 		err = nl80211_parse_chandef(rdev, info, &chandef);
10520 		if (err)
10521 			return err;
10522 	}
10523 
10524 	if (!chandef.chan && params.offchan)
10525 		return -EINVAL;
10526 
10527 	wdev_lock(wdev);
10528 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10529 		wdev_unlock(wdev);
10530 		return -EBUSY;
10531 	}
10532 	wdev_unlock(wdev);
10533 
10534 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10535 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10536 
10537 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10538 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10539 		int i;
10540 
10541 		if (len % sizeof(u16))
10542 			return -EINVAL;
10543 
10544 		params.n_csa_offsets = len / sizeof(u16);
10545 		params.csa_offsets =
10546 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10547 
10548 		/* check that all the offsets fit the frame */
10549 		for (i = 0; i < params.n_csa_offsets; i++) {
10550 			if (params.csa_offsets[i] >= params.len)
10551 				return -EINVAL;
10552 		}
10553 	}
10554 
10555 	if (!params.dont_wait_for_ack) {
10556 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10557 		if (!msg)
10558 			return -ENOMEM;
10559 
10560 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10561 				     NL80211_CMD_FRAME);
10562 		if (!hdr) {
10563 			err = -ENOBUFS;
10564 			goto free_msg;
10565 		}
10566 	}
10567 
10568 	params.chan = chandef.chan;
10569 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10570 	if (err)
10571 		goto free_msg;
10572 
10573 	if (msg) {
10574 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10575 				      NL80211_ATTR_PAD))
10576 			goto nla_put_failure;
10577 
10578 		genlmsg_end(msg, hdr);
10579 		return genlmsg_reply(msg, info);
10580 	}
10581 
10582 	return 0;
10583 
10584  nla_put_failure:
10585 	err = -ENOBUFS;
10586  free_msg:
10587 	nlmsg_free(msg);
10588 	return err;
10589 }
10590 
10591 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10592 {
10593 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10594 	struct wireless_dev *wdev = info->user_ptr[1];
10595 	u64 cookie;
10596 
10597 	if (!info->attrs[NL80211_ATTR_COOKIE])
10598 		return -EINVAL;
10599 
10600 	if (!rdev->ops->mgmt_tx_cancel_wait)
10601 		return -EOPNOTSUPP;
10602 
10603 	switch (wdev->iftype) {
10604 	case NL80211_IFTYPE_STATION:
10605 	case NL80211_IFTYPE_ADHOC:
10606 	case NL80211_IFTYPE_P2P_CLIENT:
10607 	case NL80211_IFTYPE_AP:
10608 	case NL80211_IFTYPE_AP_VLAN:
10609 	case NL80211_IFTYPE_P2P_GO:
10610 	case NL80211_IFTYPE_P2P_DEVICE:
10611 		break;
10612 	case NL80211_IFTYPE_NAN:
10613 	default:
10614 		return -EOPNOTSUPP;
10615 	}
10616 
10617 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10618 
10619 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10620 }
10621 
10622 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10623 {
10624 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10625 	struct wireless_dev *wdev;
10626 	struct net_device *dev = info->user_ptr[1];
10627 	u8 ps_state;
10628 	bool state;
10629 	int err;
10630 
10631 	if (!info->attrs[NL80211_ATTR_PS_STATE])
10632 		return -EINVAL;
10633 
10634 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10635 
10636 	wdev = dev->ieee80211_ptr;
10637 
10638 	if (!rdev->ops->set_power_mgmt)
10639 		return -EOPNOTSUPP;
10640 
10641 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10642 
10643 	if (state == wdev->ps)
10644 		return 0;
10645 
10646 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10647 	if (!err)
10648 		wdev->ps = state;
10649 	return err;
10650 }
10651 
10652 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10653 {
10654 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10655 	enum nl80211_ps_state ps_state;
10656 	struct wireless_dev *wdev;
10657 	struct net_device *dev = info->user_ptr[1];
10658 	struct sk_buff *msg;
10659 	void *hdr;
10660 	int err;
10661 
10662 	wdev = dev->ieee80211_ptr;
10663 
10664 	if (!rdev->ops->set_power_mgmt)
10665 		return -EOPNOTSUPP;
10666 
10667 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10668 	if (!msg)
10669 		return -ENOMEM;
10670 
10671 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10672 			     NL80211_CMD_GET_POWER_SAVE);
10673 	if (!hdr) {
10674 		err = -ENOBUFS;
10675 		goto free_msg;
10676 	}
10677 
10678 	if (wdev->ps)
10679 		ps_state = NL80211_PS_ENABLED;
10680 	else
10681 		ps_state = NL80211_PS_DISABLED;
10682 
10683 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10684 		goto nla_put_failure;
10685 
10686 	genlmsg_end(msg, hdr);
10687 	return genlmsg_reply(msg, info);
10688 
10689  nla_put_failure:
10690 	err = -ENOBUFS;
10691  free_msg:
10692 	nlmsg_free(msg);
10693 	return err;
10694 }
10695 
10696 static const struct nla_policy
10697 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10698 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10699 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10700 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10701 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10702 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10703 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10704 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10705 };
10706 
10707 static int nl80211_set_cqm_txe(struct genl_info *info,
10708 			       u32 rate, u32 pkts, u32 intvl)
10709 {
10710 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10711 	struct net_device *dev = info->user_ptr[1];
10712 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10713 
10714 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10715 		return -EINVAL;
10716 
10717 	if (!rdev->ops->set_cqm_txe_config)
10718 		return -EOPNOTSUPP;
10719 
10720 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10721 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10722 		return -EOPNOTSUPP;
10723 
10724 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10725 }
10726 
10727 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10728 				    struct net_device *dev)
10729 {
10730 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10731 	s32 last, low, high;
10732 	u32 hyst;
10733 	int i, n, low_index;
10734 	int err;
10735 
10736 	/* RSSI reporting disabled? */
10737 	if (!wdev->cqm_config)
10738 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10739 
10740 	/*
10741 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
10742 	 * event has been received yet, we should receive an event after a
10743 	 * connection is established and enough beacons received to calculate
10744 	 * the average.
10745 	 */
10746 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10747 	    rdev->ops->get_station) {
10748 		struct station_info sinfo = {};
10749 		u8 *mac_addr;
10750 
10751 		mac_addr = wdev->current_bss->pub.bssid;
10752 
10753 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10754 		if (err)
10755 			return err;
10756 
10757 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10758 			wdev->cqm_config->last_rssi_event_value =
10759 				(s8) sinfo.rx_beacon_signal_avg;
10760 	}
10761 
10762 	last = wdev->cqm_config->last_rssi_event_value;
10763 	hyst = wdev->cqm_config->rssi_hyst;
10764 	n = wdev->cqm_config->n_rssi_thresholds;
10765 
10766 	for (i = 0; i < n; i++)
10767 		if (last < wdev->cqm_config->rssi_thresholds[i])
10768 			break;
10769 
10770 	low_index = i - 1;
10771 	if (low_index >= 0) {
10772 		low_index = array_index_nospec(low_index, n);
10773 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10774 	} else {
10775 		low = S32_MIN;
10776 	}
10777 	if (i < n) {
10778 		i = array_index_nospec(i, n);
10779 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10780 	} else {
10781 		high = S32_MAX;
10782 	}
10783 
10784 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10785 }
10786 
10787 static int nl80211_set_cqm_rssi(struct genl_info *info,
10788 				const s32 *thresholds, int n_thresholds,
10789 				u32 hysteresis)
10790 {
10791 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10792 	struct net_device *dev = info->user_ptr[1];
10793 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10794 	int i, err;
10795 	s32 prev = S32_MIN;
10796 
10797 	/* Check all values negative and sorted */
10798 	for (i = 0; i < n_thresholds; i++) {
10799 		if (thresholds[i] > 0 || thresholds[i] <= prev)
10800 			return -EINVAL;
10801 
10802 		prev = thresholds[i];
10803 	}
10804 
10805 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10806 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10807 		return -EOPNOTSUPP;
10808 
10809 	wdev_lock(wdev);
10810 	cfg80211_cqm_config_free(wdev);
10811 	wdev_unlock(wdev);
10812 
10813 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10814 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10815 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10816 
10817 		return rdev_set_cqm_rssi_config(rdev, dev,
10818 						thresholds[0], hysteresis);
10819 	}
10820 
10821 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
10822 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10823 		return -EOPNOTSUPP;
10824 
10825 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10826 		n_thresholds = 0;
10827 
10828 	wdev_lock(wdev);
10829 	if (n_thresholds) {
10830 		struct cfg80211_cqm_config *cqm_config;
10831 
10832 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10833 				     n_thresholds * sizeof(s32), GFP_KERNEL);
10834 		if (!cqm_config) {
10835 			err = -ENOMEM;
10836 			goto unlock;
10837 		}
10838 
10839 		cqm_config->rssi_hyst = hysteresis;
10840 		cqm_config->n_rssi_thresholds = n_thresholds;
10841 		memcpy(cqm_config->rssi_thresholds, thresholds,
10842 		       n_thresholds * sizeof(s32));
10843 
10844 		wdev->cqm_config = cqm_config;
10845 	}
10846 
10847 	err = cfg80211_cqm_rssi_update(rdev, dev);
10848 
10849 unlock:
10850 	wdev_unlock(wdev);
10851 
10852 	return err;
10853 }
10854 
10855 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10856 {
10857 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10858 	struct nlattr *cqm;
10859 	int err;
10860 
10861 	cqm = info->attrs[NL80211_ATTR_CQM];
10862 	if (!cqm)
10863 		return -EINVAL;
10864 
10865 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
10866 					  nl80211_attr_cqm_policy,
10867 					  info->extack);
10868 	if (err)
10869 		return err;
10870 
10871 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10872 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10873 		const s32 *thresholds =
10874 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10875 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10876 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10877 
10878 		if (len % 4)
10879 			return -EINVAL;
10880 
10881 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10882 					    hysteresis);
10883 	}
10884 
10885 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10886 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10887 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10888 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10889 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10890 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10891 
10892 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10893 	}
10894 
10895 	return -EINVAL;
10896 }
10897 
10898 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10899 {
10900 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10901 	struct net_device *dev = info->user_ptr[1];
10902 	struct ocb_setup setup = {};
10903 	int err;
10904 
10905 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10906 	if (err)
10907 		return err;
10908 
10909 	return cfg80211_join_ocb(rdev, dev, &setup);
10910 }
10911 
10912 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
10913 {
10914 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10915 	struct net_device *dev = info->user_ptr[1];
10916 
10917 	return cfg80211_leave_ocb(rdev, dev);
10918 }
10919 
10920 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
10921 {
10922 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10923 	struct net_device *dev = info->user_ptr[1];
10924 	struct mesh_config cfg;
10925 	struct mesh_setup setup;
10926 	int err;
10927 
10928 	/* start with default */
10929 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
10930 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
10931 
10932 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
10933 		/* and parse parameters if given */
10934 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
10935 		if (err)
10936 			return err;
10937 	}
10938 
10939 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
10940 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
10941 		return -EINVAL;
10942 
10943 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
10944 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
10945 
10946 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10947 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
10948 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10949 			return -EINVAL;
10950 
10951 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
10952 		setup.beacon_interval =
10953 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10954 
10955 		err = cfg80211_validate_beacon_int(rdev,
10956 						   NL80211_IFTYPE_MESH_POINT,
10957 						   setup.beacon_interval);
10958 		if (err)
10959 			return err;
10960 	}
10961 
10962 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
10963 		setup.dtim_period =
10964 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
10965 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
10966 			return -EINVAL;
10967 	}
10968 
10969 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
10970 		/* parse additional setup parameters if given */
10971 		err = nl80211_parse_mesh_setup(info, &setup);
10972 		if (err)
10973 			return err;
10974 	}
10975 
10976 	if (setup.user_mpm)
10977 		cfg.auto_open_plinks = false;
10978 
10979 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10980 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10981 		if (err)
10982 			return err;
10983 	} else {
10984 		/* __cfg80211_join_mesh() will sort it out */
10985 		setup.chandef.chan = NULL;
10986 	}
10987 
10988 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10989 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10990 		int n_rates =
10991 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10992 		struct ieee80211_supported_band *sband;
10993 
10994 		if (!setup.chandef.chan)
10995 			return -EINVAL;
10996 
10997 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
10998 
10999 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11000 					     &setup.basic_rates);
11001 		if (err)
11002 			return err;
11003 	}
11004 
11005 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11006 		err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
11007 		if (err)
11008 			return err;
11009 
11010 		if (!setup.chandef.chan)
11011 			return -EINVAL;
11012 
11013 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11014 					      &setup.beacon_rate);
11015 		if (err)
11016 			return err;
11017 	}
11018 
11019 	setup.userspace_handles_dfs =
11020 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11021 
11022 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11023 		int r = validate_pae_over_nl80211(rdev, info);
11024 
11025 		if (r < 0)
11026 			return r;
11027 
11028 		setup.control_port_over_nl80211 = true;
11029 	}
11030 
11031 	wdev_lock(dev->ieee80211_ptr);
11032 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11033 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11034 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11035 	wdev_unlock(dev->ieee80211_ptr);
11036 
11037 	return err;
11038 }
11039 
11040 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11041 {
11042 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11043 	struct net_device *dev = info->user_ptr[1];
11044 
11045 	return cfg80211_leave_mesh(rdev, dev);
11046 }
11047 
11048 #ifdef CONFIG_PM
11049 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11050 					struct cfg80211_registered_device *rdev)
11051 {
11052 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11053 	struct nlattr *nl_pats, *nl_pat;
11054 	int i, pat_len;
11055 
11056 	if (!wowlan->n_patterns)
11057 		return 0;
11058 
11059 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11060 	if (!nl_pats)
11061 		return -ENOBUFS;
11062 
11063 	for (i = 0; i < wowlan->n_patterns; i++) {
11064 		nl_pat = nla_nest_start_noflag(msg, i + 1);
11065 		if (!nl_pat)
11066 			return -ENOBUFS;
11067 		pat_len = wowlan->patterns[i].pattern_len;
11068 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11069 			    wowlan->patterns[i].mask) ||
11070 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11071 			    wowlan->patterns[i].pattern) ||
11072 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11073 				wowlan->patterns[i].pkt_offset))
11074 			return -ENOBUFS;
11075 		nla_nest_end(msg, nl_pat);
11076 	}
11077 	nla_nest_end(msg, nl_pats);
11078 
11079 	return 0;
11080 }
11081 
11082 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11083 				   struct cfg80211_wowlan_tcp *tcp)
11084 {
11085 	struct nlattr *nl_tcp;
11086 
11087 	if (!tcp)
11088 		return 0;
11089 
11090 	nl_tcp = nla_nest_start_noflag(msg,
11091 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11092 	if (!nl_tcp)
11093 		return -ENOBUFS;
11094 
11095 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11096 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11097 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11098 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11099 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11100 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11101 		    tcp->payload_len, tcp->payload) ||
11102 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11103 			tcp->data_interval) ||
11104 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11105 		    tcp->wake_len, tcp->wake_data) ||
11106 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11107 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11108 		return -ENOBUFS;
11109 
11110 	if (tcp->payload_seq.len &&
11111 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11112 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
11113 		return -ENOBUFS;
11114 
11115 	if (tcp->payload_tok.len &&
11116 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11117 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
11118 		    &tcp->payload_tok))
11119 		return -ENOBUFS;
11120 
11121 	nla_nest_end(msg, nl_tcp);
11122 
11123 	return 0;
11124 }
11125 
11126 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11127 				  struct cfg80211_sched_scan_request *req)
11128 {
11129 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11130 	int i;
11131 
11132 	if (!req)
11133 		return 0;
11134 
11135 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11136 	if (!nd)
11137 		return -ENOBUFS;
11138 
11139 	if (req->n_scan_plans == 1 &&
11140 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11141 			req->scan_plans[0].interval * 1000))
11142 		return -ENOBUFS;
11143 
11144 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11145 		return -ENOBUFS;
11146 
11147 	if (req->relative_rssi_set) {
11148 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
11149 
11150 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11151 			       req->relative_rssi))
11152 			return -ENOBUFS;
11153 
11154 		rssi_adjust.band = req->rssi_adjust.band;
11155 		rssi_adjust.delta = req->rssi_adjust.delta;
11156 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11157 			    sizeof(rssi_adjust), &rssi_adjust))
11158 			return -ENOBUFS;
11159 	}
11160 
11161 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11162 	if (!freqs)
11163 		return -ENOBUFS;
11164 
11165 	for (i = 0; i < req->n_channels; i++) {
11166 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11167 			return -ENOBUFS;
11168 	}
11169 
11170 	nla_nest_end(msg, freqs);
11171 
11172 	if (req->n_match_sets) {
11173 		matches = nla_nest_start_noflag(msg,
11174 						NL80211_ATTR_SCHED_SCAN_MATCH);
11175 		if (!matches)
11176 			return -ENOBUFS;
11177 
11178 		for (i = 0; i < req->n_match_sets; i++) {
11179 			match = nla_nest_start_noflag(msg, i);
11180 			if (!match)
11181 				return -ENOBUFS;
11182 
11183 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11184 				    req->match_sets[i].ssid.ssid_len,
11185 				    req->match_sets[i].ssid.ssid))
11186 				return -ENOBUFS;
11187 			nla_nest_end(msg, match);
11188 		}
11189 		nla_nest_end(msg, matches);
11190 	}
11191 
11192 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11193 	if (!scan_plans)
11194 		return -ENOBUFS;
11195 
11196 	for (i = 0; i < req->n_scan_plans; i++) {
11197 		scan_plan = nla_nest_start_noflag(msg, i + 1);
11198 		if (!scan_plan)
11199 			return -ENOBUFS;
11200 
11201 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11202 				req->scan_plans[i].interval) ||
11203 		    (req->scan_plans[i].iterations &&
11204 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11205 				 req->scan_plans[i].iterations)))
11206 			return -ENOBUFS;
11207 		nla_nest_end(msg, scan_plan);
11208 	}
11209 	nla_nest_end(msg, scan_plans);
11210 
11211 	nla_nest_end(msg, nd);
11212 
11213 	return 0;
11214 }
11215 
11216 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11217 {
11218 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11219 	struct sk_buff *msg;
11220 	void *hdr;
11221 	u32 size = NLMSG_DEFAULT_SIZE;
11222 
11223 	if (!rdev->wiphy.wowlan)
11224 		return -EOPNOTSUPP;
11225 
11226 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11227 		/* adjust size to have room for all the data */
11228 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11229 			rdev->wiphy.wowlan_config->tcp->payload_len +
11230 			rdev->wiphy.wowlan_config->tcp->wake_len +
11231 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11232 	}
11233 
11234 	msg = nlmsg_new(size, GFP_KERNEL);
11235 	if (!msg)
11236 		return -ENOMEM;
11237 
11238 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11239 			     NL80211_CMD_GET_WOWLAN);
11240 	if (!hdr)
11241 		goto nla_put_failure;
11242 
11243 	if (rdev->wiphy.wowlan_config) {
11244 		struct nlattr *nl_wowlan;
11245 
11246 		nl_wowlan = nla_nest_start_noflag(msg,
11247 						  NL80211_ATTR_WOWLAN_TRIGGERS);
11248 		if (!nl_wowlan)
11249 			goto nla_put_failure;
11250 
11251 		if ((rdev->wiphy.wowlan_config->any &&
11252 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11253 		    (rdev->wiphy.wowlan_config->disconnect &&
11254 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11255 		    (rdev->wiphy.wowlan_config->magic_pkt &&
11256 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11257 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11258 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11259 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
11260 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11261 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
11262 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11263 		    (rdev->wiphy.wowlan_config->rfkill_release &&
11264 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11265 			goto nla_put_failure;
11266 
11267 		if (nl80211_send_wowlan_patterns(msg, rdev))
11268 			goto nla_put_failure;
11269 
11270 		if (nl80211_send_wowlan_tcp(msg,
11271 					    rdev->wiphy.wowlan_config->tcp))
11272 			goto nla_put_failure;
11273 
11274 		if (nl80211_send_wowlan_nd(
11275 			    msg,
11276 			    rdev->wiphy.wowlan_config->nd_config))
11277 			goto nla_put_failure;
11278 
11279 		nla_nest_end(msg, nl_wowlan);
11280 	}
11281 
11282 	genlmsg_end(msg, hdr);
11283 	return genlmsg_reply(msg, info);
11284 
11285 nla_put_failure:
11286 	nlmsg_free(msg);
11287 	return -ENOBUFS;
11288 }
11289 
11290 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11291 				    struct nlattr *attr,
11292 				    struct cfg80211_wowlan *trig)
11293 {
11294 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11295 	struct cfg80211_wowlan_tcp *cfg;
11296 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
11297 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11298 	u32 size;
11299 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11300 	int err, port;
11301 
11302 	if (!rdev->wiphy.wowlan->tcp)
11303 		return -EINVAL;
11304 
11305 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11306 					  nl80211_wowlan_tcp_policy, NULL);
11307 	if (err)
11308 		return err;
11309 
11310 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11311 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11312 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11313 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11314 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11315 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11316 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11317 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11318 		return -EINVAL;
11319 
11320 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11321 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11322 		return -EINVAL;
11323 
11324 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11325 			rdev->wiphy.wowlan->tcp->data_interval_max ||
11326 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11327 		return -EINVAL;
11328 
11329 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11330 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11331 		return -EINVAL;
11332 
11333 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11334 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11335 		return -EINVAL;
11336 
11337 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11338 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11339 
11340 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11341 		tokens_size = tokln - sizeof(*tok);
11342 
11343 		if (!tok->len || tokens_size % tok->len)
11344 			return -EINVAL;
11345 		if (!rdev->wiphy.wowlan->tcp->tok)
11346 			return -EINVAL;
11347 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11348 			return -EINVAL;
11349 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11350 			return -EINVAL;
11351 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11352 			return -EINVAL;
11353 		if (tok->offset + tok->len > data_size)
11354 			return -EINVAL;
11355 	}
11356 
11357 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11358 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11359 		if (!rdev->wiphy.wowlan->tcp->seq)
11360 			return -EINVAL;
11361 		if (seq->len == 0 || seq->len > 4)
11362 			return -EINVAL;
11363 		if (seq->len + seq->offset > data_size)
11364 			return -EINVAL;
11365 	}
11366 
11367 	size = sizeof(*cfg);
11368 	size += data_size;
11369 	size += wake_size + wake_mask_size;
11370 	size += tokens_size;
11371 
11372 	cfg = kzalloc(size, GFP_KERNEL);
11373 	if (!cfg)
11374 		return -ENOMEM;
11375 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11376 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11377 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11378 	       ETH_ALEN);
11379 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11380 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11381 	else
11382 		port = 0;
11383 #ifdef CONFIG_INET
11384 	/* allocate a socket and port for it and use it */
11385 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11386 			    IPPROTO_TCP, &cfg->sock, 1);
11387 	if (err) {
11388 		kfree(cfg);
11389 		return err;
11390 	}
11391 	if (inet_csk_get_port(cfg->sock->sk, port)) {
11392 		sock_release(cfg->sock);
11393 		kfree(cfg);
11394 		return -EADDRINUSE;
11395 	}
11396 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11397 #else
11398 	if (!port) {
11399 		kfree(cfg);
11400 		return -EINVAL;
11401 	}
11402 	cfg->src_port = port;
11403 #endif
11404 
11405 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11406 	cfg->payload_len = data_size;
11407 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11408 	memcpy((void *)cfg->payload,
11409 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11410 	       data_size);
11411 	if (seq)
11412 		cfg->payload_seq = *seq;
11413 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11414 	cfg->wake_len = wake_size;
11415 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11416 	memcpy((void *)cfg->wake_data,
11417 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11418 	       wake_size);
11419 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11420 			 data_size + wake_size;
11421 	memcpy((void *)cfg->wake_mask,
11422 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11423 	       wake_mask_size);
11424 	if (tok) {
11425 		cfg->tokens_size = tokens_size;
11426 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11427 	}
11428 
11429 	trig->tcp = cfg;
11430 
11431 	return 0;
11432 }
11433 
11434 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11435 				   const struct wiphy_wowlan_support *wowlan,
11436 				   struct nlattr *attr,
11437 				   struct cfg80211_wowlan *trig)
11438 {
11439 	struct nlattr **tb;
11440 	int err;
11441 
11442 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11443 	if (!tb)
11444 		return -ENOMEM;
11445 
11446 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11447 		err = -EOPNOTSUPP;
11448 		goto out;
11449 	}
11450 
11451 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11452 					  nl80211_policy, NULL);
11453 	if (err)
11454 		goto out;
11455 
11456 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11457 						   wowlan->max_nd_match_sets);
11458 	err = PTR_ERR_OR_ZERO(trig->nd_config);
11459 	if (err)
11460 		trig->nd_config = NULL;
11461 
11462 out:
11463 	kfree(tb);
11464 	return err;
11465 }
11466 
11467 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11468 {
11469 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11470 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11471 	struct cfg80211_wowlan new_triggers = {};
11472 	struct cfg80211_wowlan *ntrig;
11473 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11474 	int err, i;
11475 	bool prev_enabled = rdev->wiphy.wowlan_config;
11476 	bool regular = false;
11477 
11478 	if (!wowlan)
11479 		return -EOPNOTSUPP;
11480 
11481 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11482 		cfg80211_rdev_free_wowlan(rdev);
11483 		rdev->wiphy.wowlan_config = NULL;
11484 		goto set_wakeup;
11485 	}
11486 
11487 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11488 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11489 					  nl80211_wowlan_policy, info->extack);
11490 	if (err)
11491 		return err;
11492 
11493 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11494 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11495 			return -EINVAL;
11496 		new_triggers.any = true;
11497 	}
11498 
11499 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11500 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11501 			return -EINVAL;
11502 		new_triggers.disconnect = true;
11503 		regular = true;
11504 	}
11505 
11506 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11507 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11508 			return -EINVAL;
11509 		new_triggers.magic_pkt = true;
11510 		regular = true;
11511 	}
11512 
11513 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11514 		return -EINVAL;
11515 
11516 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11517 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11518 			return -EINVAL;
11519 		new_triggers.gtk_rekey_failure = true;
11520 		regular = true;
11521 	}
11522 
11523 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11524 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11525 			return -EINVAL;
11526 		new_triggers.eap_identity_req = true;
11527 		regular = true;
11528 	}
11529 
11530 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11531 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11532 			return -EINVAL;
11533 		new_triggers.four_way_handshake = true;
11534 		regular = true;
11535 	}
11536 
11537 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11538 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11539 			return -EINVAL;
11540 		new_triggers.rfkill_release = true;
11541 		regular = true;
11542 	}
11543 
11544 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11545 		struct nlattr *pat;
11546 		int n_patterns = 0;
11547 		int rem, pat_len, mask_len, pkt_offset;
11548 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11549 
11550 		regular = true;
11551 
11552 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11553 				    rem)
11554 			n_patterns++;
11555 		if (n_patterns > wowlan->n_patterns)
11556 			return -EINVAL;
11557 
11558 		new_triggers.patterns = kcalloc(n_patterns,
11559 						sizeof(new_triggers.patterns[0]),
11560 						GFP_KERNEL);
11561 		if (!new_triggers.patterns)
11562 			return -ENOMEM;
11563 
11564 		new_triggers.n_patterns = n_patterns;
11565 		i = 0;
11566 
11567 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11568 				    rem) {
11569 			u8 *mask_pat;
11570 
11571 			err = nla_parse_nested_deprecated(pat_tb,
11572 							  MAX_NL80211_PKTPAT,
11573 							  pat,
11574 							  nl80211_packet_pattern_policy,
11575 							  info->extack);
11576 			if (err)
11577 				goto error;
11578 
11579 			err = -EINVAL;
11580 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
11581 			    !pat_tb[NL80211_PKTPAT_PATTERN])
11582 				goto error;
11583 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11584 			mask_len = DIV_ROUND_UP(pat_len, 8);
11585 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11586 				goto error;
11587 			if (pat_len > wowlan->pattern_max_len ||
11588 			    pat_len < wowlan->pattern_min_len)
11589 				goto error;
11590 
11591 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
11592 				pkt_offset = 0;
11593 			else
11594 				pkt_offset = nla_get_u32(
11595 					pat_tb[NL80211_PKTPAT_OFFSET]);
11596 			if (pkt_offset > wowlan->max_pkt_offset)
11597 				goto error;
11598 			new_triggers.patterns[i].pkt_offset = pkt_offset;
11599 
11600 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11601 			if (!mask_pat) {
11602 				err = -ENOMEM;
11603 				goto error;
11604 			}
11605 			new_triggers.patterns[i].mask = mask_pat;
11606 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11607 			       mask_len);
11608 			mask_pat += mask_len;
11609 			new_triggers.patterns[i].pattern = mask_pat;
11610 			new_triggers.patterns[i].pattern_len = pat_len;
11611 			memcpy(mask_pat,
11612 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11613 			       pat_len);
11614 			i++;
11615 		}
11616 	}
11617 
11618 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11619 		regular = true;
11620 		err = nl80211_parse_wowlan_tcp(
11621 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11622 			&new_triggers);
11623 		if (err)
11624 			goto error;
11625 	}
11626 
11627 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11628 		regular = true;
11629 		err = nl80211_parse_wowlan_nd(
11630 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11631 			&new_triggers);
11632 		if (err)
11633 			goto error;
11634 	}
11635 
11636 	/* The 'any' trigger means the device continues operating more or less
11637 	 * as in its normal operation mode and wakes up the host on most of the
11638 	 * normal interrupts (like packet RX, ...)
11639 	 * It therefore makes little sense to combine with the more constrained
11640 	 * wakeup trigger modes.
11641 	 */
11642 	if (new_triggers.any && regular) {
11643 		err = -EINVAL;
11644 		goto error;
11645 	}
11646 
11647 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11648 	if (!ntrig) {
11649 		err = -ENOMEM;
11650 		goto error;
11651 	}
11652 	cfg80211_rdev_free_wowlan(rdev);
11653 	rdev->wiphy.wowlan_config = ntrig;
11654 
11655  set_wakeup:
11656 	if (rdev->ops->set_wakeup &&
11657 	    prev_enabled != !!rdev->wiphy.wowlan_config)
11658 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11659 
11660 	return 0;
11661  error:
11662 	for (i = 0; i < new_triggers.n_patterns; i++)
11663 		kfree(new_triggers.patterns[i].mask);
11664 	kfree(new_triggers.patterns);
11665 	if (new_triggers.tcp && new_triggers.tcp->sock)
11666 		sock_release(new_triggers.tcp->sock);
11667 	kfree(new_triggers.tcp);
11668 	kfree(new_triggers.nd_config);
11669 	return err;
11670 }
11671 #endif
11672 
11673 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11674 				       struct cfg80211_registered_device *rdev)
11675 {
11676 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11677 	int i, j, pat_len;
11678 	struct cfg80211_coalesce_rules *rule;
11679 
11680 	if (!rdev->coalesce->n_rules)
11681 		return 0;
11682 
11683 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
11684 	if (!nl_rules)
11685 		return -ENOBUFS;
11686 
11687 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
11688 		nl_rule = nla_nest_start_noflag(msg, i + 1);
11689 		if (!nl_rule)
11690 			return -ENOBUFS;
11691 
11692 		rule = &rdev->coalesce->rules[i];
11693 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11694 				rule->delay))
11695 			return -ENOBUFS;
11696 
11697 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11698 				rule->condition))
11699 			return -ENOBUFS;
11700 
11701 		nl_pats = nla_nest_start_noflag(msg,
11702 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11703 		if (!nl_pats)
11704 			return -ENOBUFS;
11705 
11706 		for (j = 0; j < rule->n_patterns; j++) {
11707 			nl_pat = nla_nest_start_noflag(msg, j + 1);
11708 			if (!nl_pat)
11709 				return -ENOBUFS;
11710 			pat_len = rule->patterns[j].pattern_len;
11711 			if (nla_put(msg, NL80211_PKTPAT_MASK,
11712 				    DIV_ROUND_UP(pat_len, 8),
11713 				    rule->patterns[j].mask) ||
11714 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11715 				    rule->patterns[j].pattern) ||
11716 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11717 					rule->patterns[j].pkt_offset))
11718 				return -ENOBUFS;
11719 			nla_nest_end(msg, nl_pat);
11720 		}
11721 		nla_nest_end(msg, nl_pats);
11722 		nla_nest_end(msg, nl_rule);
11723 	}
11724 	nla_nest_end(msg, nl_rules);
11725 
11726 	return 0;
11727 }
11728 
11729 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11730 {
11731 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11732 	struct sk_buff *msg;
11733 	void *hdr;
11734 
11735 	if (!rdev->wiphy.coalesce)
11736 		return -EOPNOTSUPP;
11737 
11738 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11739 	if (!msg)
11740 		return -ENOMEM;
11741 
11742 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11743 			     NL80211_CMD_GET_COALESCE);
11744 	if (!hdr)
11745 		goto nla_put_failure;
11746 
11747 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11748 		goto nla_put_failure;
11749 
11750 	genlmsg_end(msg, hdr);
11751 	return genlmsg_reply(msg, info);
11752 
11753 nla_put_failure:
11754 	nlmsg_free(msg);
11755 	return -ENOBUFS;
11756 }
11757 
11758 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11759 {
11760 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
11761 	int i, j;
11762 	struct cfg80211_coalesce_rules *rule;
11763 
11764 	if (!coalesce)
11765 		return;
11766 
11767 	for (i = 0; i < coalesce->n_rules; i++) {
11768 		rule = &coalesce->rules[i];
11769 		for (j = 0; j < rule->n_patterns; j++)
11770 			kfree(rule->patterns[j].mask);
11771 		kfree(rule->patterns);
11772 	}
11773 	kfree(coalesce->rules);
11774 	kfree(coalesce);
11775 	rdev->coalesce = NULL;
11776 }
11777 
11778 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11779 				       struct nlattr *rule,
11780 				       struct cfg80211_coalesce_rules *new_rule)
11781 {
11782 	int err, i;
11783 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11784 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11785 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11786 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11787 
11788 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
11789 					  rule, nl80211_coalesce_policy, NULL);
11790 	if (err)
11791 		return err;
11792 
11793 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11794 		new_rule->delay =
11795 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11796 	if (new_rule->delay > coalesce->max_delay)
11797 		return -EINVAL;
11798 
11799 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11800 		new_rule->condition =
11801 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11802 
11803 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11804 		return -EINVAL;
11805 
11806 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11807 			    rem)
11808 		n_patterns++;
11809 	if (n_patterns > coalesce->n_patterns)
11810 		return -EINVAL;
11811 
11812 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11813 				     GFP_KERNEL);
11814 	if (!new_rule->patterns)
11815 		return -ENOMEM;
11816 
11817 	new_rule->n_patterns = n_patterns;
11818 	i = 0;
11819 
11820 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11821 			    rem) {
11822 		u8 *mask_pat;
11823 
11824 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
11825 						  pat,
11826 						  nl80211_packet_pattern_policy,
11827 						  NULL);
11828 		if (err)
11829 			return err;
11830 
11831 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
11832 		    !pat_tb[NL80211_PKTPAT_PATTERN])
11833 			return -EINVAL;
11834 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11835 		mask_len = DIV_ROUND_UP(pat_len, 8);
11836 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11837 			return -EINVAL;
11838 		if (pat_len > coalesce->pattern_max_len ||
11839 		    pat_len < coalesce->pattern_min_len)
11840 			return -EINVAL;
11841 
11842 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
11843 			pkt_offset = 0;
11844 		else
11845 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11846 		if (pkt_offset > coalesce->max_pkt_offset)
11847 			return -EINVAL;
11848 		new_rule->patterns[i].pkt_offset = pkt_offset;
11849 
11850 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11851 		if (!mask_pat)
11852 			return -ENOMEM;
11853 
11854 		new_rule->patterns[i].mask = mask_pat;
11855 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11856 		       mask_len);
11857 
11858 		mask_pat += mask_len;
11859 		new_rule->patterns[i].pattern = mask_pat;
11860 		new_rule->patterns[i].pattern_len = pat_len;
11861 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11862 		       pat_len);
11863 		i++;
11864 	}
11865 
11866 	return 0;
11867 }
11868 
11869 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11870 {
11871 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11872 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11873 	struct cfg80211_coalesce new_coalesce = {};
11874 	struct cfg80211_coalesce *n_coalesce;
11875 	int err, rem_rule, n_rules = 0, i, j;
11876 	struct nlattr *rule;
11877 	struct cfg80211_coalesce_rules *tmp_rule;
11878 
11879 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11880 		return -EOPNOTSUPP;
11881 
11882 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11883 		cfg80211_rdev_free_coalesce(rdev);
11884 		rdev_set_coalesce(rdev, NULL);
11885 		return 0;
11886 	}
11887 
11888 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11889 			    rem_rule)
11890 		n_rules++;
11891 	if (n_rules > coalesce->n_rules)
11892 		return -EINVAL;
11893 
11894 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11895 				     GFP_KERNEL);
11896 	if (!new_coalesce.rules)
11897 		return -ENOMEM;
11898 
11899 	new_coalesce.n_rules = n_rules;
11900 	i = 0;
11901 
11902 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11903 			    rem_rule) {
11904 		err = nl80211_parse_coalesce_rule(rdev, rule,
11905 						  &new_coalesce.rules[i]);
11906 		if (err)
11907 			goto error;
11908 
11909 		i++;
11910 	}
11911 
11912 	err = rdev_set_coalesce(rdev, &new_coalesce);
11913 	if (err)
11914 		goto error;
11915 
11916 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
11917 	if (!n_coalesce) {
11918 		err = -ENOMEM;
11919 		goto error;
11920 	}
11921 	cfg80211_rdev_free_coalesce(rdev);
11922 	rdev->coalesce = n_coalesce;
11923 
11924 	return 0;
11925 error:
11926 	for (i = 0; i < new_coalesce.n_rules; i++) {
11927 		tmp_rule = &new_coalesce.rules[i];
11928 		for (j = 0; j < tmp_rule->n_patterns; j++)
11929 			kfree(tmp_rule->patterns[j].mask);
11930 		kfree(tmp_rule->patterns);
11931 	}
11932 	kfree(new_coalesce.rules);
11933 
11934 	return err;
11935 }
11936 
11937 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
11938 {
11939 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11940 	struct net_device *dev = info->user_ptr[1];
11941 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11942 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
11943 	struct cfg80211_gtk_rekey_data rekey_data;
11944 	int err;
11945 
11946 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
11947 		return -EINVAL;
11948 
11949 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
11950 					  info->attrs[NL80211_ATTR_REKEY_DATA],
11951 					  nl80211_rekey_policy, info->extack);
11952 	if (err)
11953 		return err;
11954 
11955 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
11956 	    !tb[NL80211_REKEY_DATA_KCK])
11957 		return -EINVAL;
11958 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
11959 		return -ERANGE;
11960 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
11961 		return -ERANGE;
11962 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
11963 		return -ERANGE;
11964 
11965 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
11966 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
11967 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
11968 
11969 	wdev_lock(wdev);
11970 	if (!wdev->current_bss) {
11971 		err = -ENOTCONN;
11972 		goto out;
11973 	}
11974 
11975 	if (!rdev->ops->set_rekey_data) {
11976 		err = -EOPNOTSUPP;
11977 		goto out;
11978 	}
11979 
11980 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
11981  out:
11982 	wdev_unlock(wdev);
11983 	return err;
11984 }
11985 
11986 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
11987 					     struct genl_info *info)
11988 {
11989 	struct net_device *dev = info->user_ptr[1];
11990 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11991 
11992 	if (wdev->iftype != NL80211_IFTYPE_AP &&
11993 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
11994 		return -EINVAL;
11995 
11996 	if (wdev->ap_unexpected_nlportid)
11997 		return -EBUSY;
11998 
11999 	wdev->ap_unexpected_nlportid = info->snd_portid;
12000 	return 0;
12001 }
12002 
12003 static int nl80211_probe_client(struct sk_buff *skb,
12004 				struct genl_info *info)
12005 {
12006 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12007 	struct net_device *dev = info->user_ptr[1];
12008 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12009 	struct sk_buff *msg;
12010 	void *hdr;
12011 	const u8 *addr;
12012 	u64 cookie;
12013 	int err;
12014 
12015 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12016 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12017 		return -EOPNOTSUPP;
12018 
12019 	if (!info->attrs[NL80211_ATTR_MAC])
12020 		return -EINVAL;
12021 
12022 	if (!rdev->ops->probe_client)
12023 		return -EOPNOTSUPP;
12024 
12025 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12026 	if (!msg)
12027 		return -ENOMEM;
12028 
12029 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12030 			     NL80211_CMD_PROBE_CLIENT);
12031 	if (!hdr) {
12032 		err = -ENOBUFS;
12033 		goto free_msg;
12034 	}
12035 
12036 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12037 
12038 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12039 	if (err)
12040 		goto free_msg;
12041 
12042 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12043 			      NL80211_ATTR_PAD))
12044 		goto nla_put_failure;
12045 
12046 	genlmsg_end(msg, hdr);
12047 
12048 	return genlmsg_reply(msg, info);
12049 
12050  nla_put_failure:
12051 	err = -ENOBUFS;
12052  free_msg:
12053 	nlmsg_free(msg);
12054 	return err;
12055 }
12056 
12057 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12058 {
12059 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12060 	struct cfg80211_beacon_registration *reg, *nreg;
12061 	int rv;
12062 
12063 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12064 		return -EOPNOTSUPP;
12065 
12066 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12067 	if (!nreg)
12068 		return -ENOMEM;
12069 
12070 	/* First, check if already registered. */
12071 	spin_lock_bh(&rdev->beacon_registrations_lock);
12072 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12073 		if (reg->nlportid == info->snd_portid) {
12074 			rv = -EALREADY;
12075 			goto out_err;
12076 		}
12077 	}
12078 	/* Add it to the list */
12079 	nreg->nlportid = info->snd_portid;
12080 	list_add(&nreg->list, &rdev->beacon_registrations);
12081 
12082 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12083 
12084 	return 0;
12085 out_err:
12086 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12087 	kfree(nreg);
12088 	return rv;
12089 }
12090 
12091 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12092 {
12093 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12094 	struct wireless_dev *wdev = info->user_ptr[1];
12095 	int err;
12096 
12097 	if (!rdev->ops->start_p2p_device)
12098 		return -EOPNOTSUPP;
12099 
12100 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12101 		return -EOPNOTSUPP;
12102 
12103 	if (wdev_running(wdev))
12104 		return 0;
12105 
12106 	if (rfkill_blocked(rdev->rfkill))
12107 		return -ERFKILL;
12108 
12109 	err = rdev_start_p2p_device(rdev, wdev);
12110 	if (err)
12111 		return err;
12112 
12113 	wdev->is_running = true;
12114 	rdev->opencount++;
12115 
12116 	return 0;
12117 }
12118 
12119 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12120 {
12121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12122 	struct wireless_dev *wdev = info->user_ptr[1];
12123 
12124 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12125 		return -EOPNOTSUPP;
12126 
12127 	if (!rdev->ops->stop_p2p_device)
12128 		return -EOPNOTSUPP;
12129 
12130 	cfg80211_stop_p2p_device(rdev, wdev);
12131 
12132 	return 0;
12133 }
12134 
12135 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12136 {
12137 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12138 	struct wireless_dev *wdev = info->user_ptr[1];
12139 	struct cfg80211_nan_conf conf = {};
12140 	int err;
12141 
12142 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12143 		return -EOPNOTSUPP;
12144 
12145 	if (wdev_running(wdev))
12146 		return -EEXIST;
12147 
12148 	if (rfkill_blocked(rdev->rfkill))
12149 		return -ERFKILL;
12150 
12151 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12152 		return -EINVAL;
12153 
12154 	conf.master_pref =
12155 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12156 
12157 	if (info->attrs[NL80211_ATTR_BANDS]) {
12158 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12159 
12160 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12161 			return -EOPNOTSUPP;
12162 
12163 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12164 			return -EINVAL;
12165 
12166 		conf.bands = bands;
12167 	}
12168 
12169 	err = rdev_start_nan(rdev, wdev, &conf);
12170 	if (err)
12171 		return err;
12172 
12173 	wdev->is_running = true;
12174 	rdev->opencount++;
12175 
12176 	return 0;
12177 }
12178 
12179 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12180 {
12181 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12182 	struct wireless_dev *wdev = info->user_ptr[1];
12183 
12184 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12185 		return -EOPNOTSUPP;
12186 
12187 	cfg80211_stop_nan(rdev, wdev);
12188 
12189 	return 0;
12190 }
12191 
12192 static int validate_nan_filter(struct nlattr *filter_attr)
12193 {
12194 	struct nlattr *attr;
12195 	int len = 0, n_entries = 0, rem;
12196 
12197 	nla_for_each_nested(attr, filter_attr, rem) {
12198 		len += nla_len(attr);
12199 		n_entries++;
12200 	}
12201 
12202 	if (len >= U8_MAX)
12203 		return -EINVAL;
12204 
12205 	return n_entries;
12206 }
12207 
12208 static int handle_nan_filter(struct nlattr *attr_filter,
12209 			     struct cfg80211_nan_func *func,
12210 			     bool tx)
12211 {
12212 	struct nlattr *attr;
12213 	int n_entries, rem, i;
12214 	struct cfg80211_nan_func_filter *filter;
12215 
12216 	n_entries = validate_nan_filter(attr_filter);
12217 	if (n_entries < 0)
12218 		return n_entries;
12219 
12220 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12221 
12222 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12223 	if (!filter)
12224 		return -ENOMEM;
12225 
12226 	i = 0;
12227 	nla_for_each_nested(attr, attr_filter, rem) {
12228 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12229 		filter[i].len = nla_len(attr);
12230 		i++;
12231 	}
12232 	if (tx) {
12233 		func->num_tx_filters = n_entries;
12234 		func->tx_filters = filter;
12235 	} else {
12236 		func->num_rx_filters = n_entries;
12237 		func->rx_filters = filter;
12238 	}
12239 
12240 	return 0;
12241 }
12242 
12243 static int nl80211_nan_add_func(struct sk_buff *skb,
12244 				struct genl_info *info)
12245 {
12246 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12247 	struct wireless_dev *wdev = info->user_ptr[1];
12248 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12249 	struct cfg80211_nan_func *func;
12250 	struct sk_buff *msg = NULL;
12251 	void *hdr = NULL;
12252 	int err = 0;
12253 
12254 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12255 		return -EOPNOTSUPP;
12256 
12257 	if (!wdev_running(wdev))
12258 		return -ENOTCONN;
12259 
12260 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12261 		return -EINVAL;
12262 
12263 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12264 					  info->attrs[NL80211_ATTR_NAN_FUNC],
12265 					  nl80211_nan_func_policy,
12266 					  info->extack);
12267 	if (err)
12268 		return err;
12269 
12270 	func = kzalloc(sizeof(*func), GFP_KERNEL);
12271 	if (!func)
12272 		return -ENOMEM;
12273 
12274 	func->cookie = cfg80211_assign_cookie(rdev);
12275 
12276 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
12277 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12278 		err = -EINVAL;
12279 		goto out;
12280 	}
12281 
12282 
12283 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12284 
12285 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12286 		err = -EINVAL;
12287 		goto out;
12288 	}
12289 
12290 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12291 	       sizeof(func->service_id));
12292 
12293 	func->close_range =
12294 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12295 
12296 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12297 		func->serv_spec_info_len =
12298 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12299 		func->serv_spec_info =
12300 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12301 				func->serv_spec_info_len,
12302 				GFP_KERNEL);
12303 		if (!func->serv_spec_info) {
12304 			err = -ENOMEM;
12305 			goto out;
12306 		}
12307 	}
12308 
12309 	if (tb[NL80211_NAN_FUNC_TTL])
12310 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12311 
12312 	switch (func->type) {
12313 	case NL80211_NAN_FUNC_PUBLISH:
12314 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12315 			err = -EINVAL;
12316 			goto out;
12317 		}
12318 
12319 		func->publish_type =
12320 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12321 		func->publish_bcast =
12322 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12323 
12324 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12325 			func->publish_bcast) {
12326 			err = -EINVAL;
12327 			goto out;
12328 		}
12329 		break;
12330 	case NL80211_NAN_FUNC_SUBSCRIBE:
12331 		func->subscribe_active =
12332 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12333 		break;
12334 	case NL80211_NAN_FUNC_FOLLOW_UP:
12335 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12336 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12337 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12338 			err = -EINVAL;
12339 			goto out;
12340 		}
12341 
12342 		func->followup_id =
12343 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12344 		func->followup_reqid =
12345 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12346 		memcpy(func->followup_dest.addr,
12347 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12348 		       sizeof(func->followup_dest.addr));
12349 		if (func->ttl) {
12350 			err = -EINVAL;
12351 			goto out;
12352 		}
12353 		break;
12354 	default:
12355 		err = -EINVAL;
12356 		goto out;
12357 	}
12358 
12359 	if (tb[NL80211_NAN_FUNC_SRF]) {
12360 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12361 
12362 		err = nla_parse_nested_deprecated(srf_tb,
12363 						  NL80211_NAN_SRF_ATTR_MAX,
12364 						  tb[NL80211_NAN_FUNC_SRF],
12365 						  nl80211_nan_srf_policy,
12366 						  info->extack);
12367 		if (err)
12368 			goto out;
12369 
12370 		func->srf_include =
12371 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12372 
12373 		if (srf_tb[NL80211_NAN_SRF_BF]) {
12374 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12375 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12376 				err = -EINVAL;
12377 				goto out;
12378 			}
12379 
12380 			func->srf_bf_len =
12381 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12382 			func->srf_bf =
12383 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12384 					func->srf_bf_len, GFP_KERNEL);
12385 			if (!func->srf_bf) {
12386 				err = -ENOMEM;
12387 				goto out;
12388 			}
12389 
12390 			func->srf_bf_idx =
12391 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12392 		} else {
12393 			struct nlattr *attr, *mac_attr =
12394 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12395 			int n_entries, rem, i = 0;
12396 
12397 			if (!mac_attr) {
12398 				err = -EINVAL;
12399 				goto out;
12400 			}
12401 
12402 			n_entries = validate_acl_mac_addrs(mac_attr);
12403 			if (n_entries <= 0) {
12404 				err = -EINVAL;
12405 				goto out;
12406 			}
12407 
12408 			func->srf_num_macs = n_entries;
12409 			func->srf_macs =
12410 				kcalloc(n_entries, sizeof(*func->srf_macs),
12411 					GFP_KERNEL);
12412 			if (!func->srf_macs) {
12413 				err = -ENOMEM;
12414 				goto out;
12415 			}
12416 
12417 			nla_for_each_nested(attr, mac_attr, rem)
12418 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
12419 				       sizeof(*func->srf_macs));
12420 		}
12421 	}
12422 
12423 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12424 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12425 					func, true);
12426 		if (err)
12427 			goto out;
12428 	}
12429 
12430 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12431 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12432 					func, false);
12433 		if (err)
12434 			goto out;
12435 	}
12436 
12437 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12438 	if (!msg) {
12439 		err = -ENOMEM;
12440 		goto out;
12441 	}
12442 
12443 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12444 			     NL80211_CMD_ADD_NAN_FUNCTION);
12445 	/* This can't really happen - we just allocated 4KB */
12446 	if (WARN_ON(!hdr)) {
12447 		err = -ENOMEM;
12448 		goto out;
12449 	}
12450 
12451 	err = rdev_add_nan_func(rdev, wdev, func);
12452 out:
12453 	if (err < 0) {
12454 		cfg80211_free_nan_func(func);
12455 		nlmsg_free(msg);
12456 		return err;
12457 	}
12458 
12459 	/* propagate the instance id and cookie to userspace  */
12460 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12461 			      NL80211_ATTR_PAD))
12462 		goto nla_put_failure;
12463 
12464 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12465 	if (!func_attr)
12466 		goto nla_put_failure;
12467 
12468 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12469 		       func->instance_id))
12470 		goto nla_put_failure;
12471 
12472 	nla_nest_end(msg, func_attr);
12473 
12474 	genlmsg_end(msg, hdr);
12475 	return genlmsg_reply(msg, info);
12476 
12477 nla_put_failure:
12478 	nlmsg_free(msg);
12479 	return -ENOBUFS;
12480 }
12481 
12482 static int nl80211_nan_del_func(struct sk_buff *skb,
12483 			       struct genl_info *info)
12484 {
12485 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12486 	struct wireless_dev *wdev = info->user_ptr[1];
12487 	u64 cookie;
12488 
12489 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12490 		return -EOPNOTSUPP;
12491 
12492 	if (!wdev_running(wdev))
12493 		return -ENOTCONN;
12494 
12495 	if (!info->attrs[NL80211_ATTR_COOKIE])
12496 		return -EINVAL;
12497 
12498 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12499 
12500 	rdev_del_nan_func(rdev, wdev, cookie);
12501 
12502 	return 0;
12503 }
12504 
12505 static int nl80211_nan_change_config(struct sk_buff *skb,
12506 				     struct genl_info *info)
12507 {
12508 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12509 	struct wireless_dev *wdev = info->user_ptr[1];
12510 	struct cfg80211_nan_conf conf = {};
12511 	u32 changed = 0;
12512 
12513 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12514 		return -EOPNOTSUPP;
12515 
12516 	if (!wdev_running(wdev))
12517 		return -ENOTCONN;
12518 
12519 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12520 		conf.master_pref =
12521 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12522 		if (conf.master_pref <= 1 || conf.master_pref == 255)
12523 			return -EINVAL;
12524 
12525 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12526 	}
12527 
12528 	if (info->attrs[NL80211_ATTR_BANDS]) {
12529 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12530 
12531 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12532 			return -EOPNOTSUPP;
12533 
12534 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12535 			return -EINVAL;
12536 
12537 		conf.bands = bands;
12538 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12539 	}
12540 
12541 	if (!changed)
12542 		return -EINVAL;
12543 
12544 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12545 }
12546 
12547 void cfg80211_nan_match(struct wireless_dev *wdev,
12548 			struct cfg80211_nan_match_params *match, gfp_t gfp)
12549 {
12550 	struct wiphy *wiphy = wdev->wiphy;
12551 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12552 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12553 	struct sk_buff *msg;
12554 	void *hdr;
12555 
12556 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12557 		return;
12558 
12559 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12560 	if (!msg)
12561 		return;
12562 
12563 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12564 	if (!hdr) {
12565 		nlmsg_free(msg);
12566 		return;
12567 	}
12568 
12569 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12570 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12571 					 wdev->netdev->ifindex)) ||
12572 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12573 			      NL80211_ATTR_PAD))
12574 		goto nla_put_failure;
12575 
12576 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12577 			      NL80211_ATTR_PAD) ||
12578 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12579 		goto nla_put_failure;
12580 
12581 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12582 	if (!match_attr)
12583 		goto nla_put_failure;
12584 
12585 	local_func_attr = nla_nest_start_noflag(msg,
12586 						NL80211_NAN_MATCH_FUNC_LOCAL);
12587 	if (!local_func_attr)
12588 		goto nla_put_failure;
12589 
12590 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12591 		goto nla_put_failure;
12592 
12593 	nla_nest_end(msg, local_func_attr);
12594 
12595 	peer_func_attr = nla_nest_start_noflag(msg,
12596 					       NL80211_NAN_MATCH_FUNC_PEER);
12597 	if (!peer_func_attr)
12598 		goto nla_put_failure;
12599 
12600 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12601 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12602 		goto nla_put_failure;
12603 
12604 	if (match->info && match->info_len &&
12605 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12606 		    match->info))
12607 		goto nla_put_failure;
12608 
12609 	nla_nest_end(msg, peer_func_attr);
12610 	nla_nest_end(msg, match_attr);
12611 	genlmsg_end(msg, hdr);
12612 
12613 	if (!wdev->owner_nlportid)
12614 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12615 					msg, 0, NL80211_MCGRP_NAN, gfp);
12616 	else
12617 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12618 				wdev->owner_nlportid);
12619 
12620 	return;
12621 
12622 nla_put_failure:
12623 	nlmsg_free(msg);
12624 }
12625 EXPORT_SYMBOL(cfg80211_nan_match);
12626 
12627 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12628 				  u8 inst_id,
12629 				  enum nl80211_nan_func_term_reason reason,
12630 				  u64 cookie, gfp_t gfp)
12631 {
12632 	struct wiphy *wiphy = wdev->wiphy;
12633 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12634 	struct sk_buff *msg;
12635 	struct nlattr *func_attr;
12636 	void *hdr;
12637 
12638 	if (WARN_ON(!inst_id))
12639 		return;
12640 
12641 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12642 	if (!msg)
12643 		return;
12644 
12645 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12646 	if (!hdr) {
12647 		nlmsg_free(msg);
12648 		return;
12649 	}
12650 
12651 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12652 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12653 					 wdev->netdev->ifindex)) ||
12654 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12655 			      NL80211_ATTR_PAD))
12656 		goto nla_put_failure;
12657 
12658 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12659 			      NL80211_ATTR_PAD))
12660 		goto nla_put_failure;
12661 
12662 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12663 	if (!func_attr)
12664 		goto nla_put_failure;
12665 
12666 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12667 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12668 		goto nla_put_failure;
12669 
12670 	nla_nest_end(msg, func_attr);
12671 	genlmsg_end(msg, hdr);
12672 
12673 	if (!wdev->owner_nlportid)
12674 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12675 					msg, 0, NL80211_MCGRP_NAN, gfp);
12676 	else
12677 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12678 				wdev->owner_nlportid);
12679 
12680 	return;
12681 
12682 nla_put_failure:
12683 	nlmsg_free(msg);
12684 }
12685 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12686 
12687 static int nl80211_get_protocol_features(struct sk_buff *skb,
12688 					 struct genl_info *info)
12689 {
12690 	void *hdr;
12691 	struct sk_buff *msg;
12692 
12693 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12694 	if (!msg)
12695 		return -ENOMEM;
12696 
12697 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12698 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
12699 	if (!hdr)
12700 		goto nla_put_failure;
12701 
12702 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12703 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12704 		goto nla_put_failure;
12705 
12706 	genlmsg_end(msg, hdr);
12707 	return genlmsg_reply(msg, info);
12708 
12709  nla_put_failure:
12710 	kfree_skb(msg);
12711 	return -ENOBUFS;
12712 }
12713 
12714 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12715 {
12716 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12717 	struct cfg80211_update_ft_ies_params ft_params;
12718 	struct net_device *dev = info->user_ptr[1];
12719 
12720 	if (!rdev->ops->update_ft_ies)
12721 		return -EOPNOTSUPP;
12722 
12723 	if (!info->attrs[NL80211_ATTR_MDID] ||
12724 	    !info->attrs[NL80211_ATTR_IE])
12725 		return -EINVAL;
12726 
12727 	memset(&ft_params, 0, sizeof(ft_params));
12728 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12729 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12730 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12731 
12732 	return rdev_update_ft_ies(rdev, dev, &ft_params);
12733 }
12734 
12735 static int nl80211_crit_protocol_start(struct sk_buff *skb,
12736 				       struct genl_info *info)
12737 {
12738 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12739 	struct wireless_dev *wdev = info->user_ptr[1];
12740 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12741 	u16 duration;
12742 	int ret;
12743 
12744 	if (!rdev->ops->crit_proto_start)
12745 		return -EOPNOTSUPP;
12746 
12747 	if (WARN_ON(!rdev->ops->crit_proto_stop))
12748 		return -EINVAL;
12749 
12750 	if (rdev->crit_proto_nlportid)
12751 		return -EBUSY;
12752 
12753 	/* determine protocol if provided */
12754 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12755 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12756 
12757 	if (proto >= NUM_NL80211_CRIT_PROTO)
12758 		return -EINVAL;
12759 
12760 	/* timeout must be provided */
12761 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12762 		return -EINVAL;
12763 
12764 	duration =
12765 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12766 
12767 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12768 		return -ERANGE;
12769 
12770 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12771 	if (!ret)
12772 		rdev->crit_proto_nlportid = info->snd_portid;
12773 
12774 	return ret;
12775 }
12776 
12777 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12778 				      struct genl_info *info)
12779 {
12780 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12781 	struct wireless_dev *wdev = info->user_ptr[1];
12782 
12783 	if (!rdev->ops->crit_proto_stop)
12784 		return -EOPNOTSUPP;
12785 
12786 	if (rdev->crit_proto_nlportid) {
12787 		rdev->crit_proto_nlportid = 0;
12788 		rdev_crit_proto_stop(rdev, wdev);
12789 	}
12790 	return 0;
12791 }
12792 
12793 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
12794 				       struct nlattr *attr,
12795 				       struct netlink_ext_ack *extack)
12796 {
12797 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
12798 		if (attr->nla_type & NLA_F_NESTED) {
12799 			NL_SET_ERR_MSG_ATTR(extack, attr,
12800 					    "unexpected nested data");
12801 			return -EINVAL;
12802 		}
12803 
12804 		return 0;
12805 	}
12806 
12807 	if (!(attr->nla_type & NLA_F_NESTED)) {
12808 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
12809 		return -EINVAL;
12810 	}
12811 
12812 	return nl80211_validate_nested(attr, vcmd->maxattr, vcmd->policy,
12813 				       extack);
12814 }
12815 
12816 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12817 {
12818 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12819 	struct wireless_dev *wdev =
12820 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12821 	int i, err;
12822 	u32 vid, subcmd;
12823 
12824 	if (!rdev->wiphy.vendor_commands)
12825 		return -EOPNOTSUPP;
12826 
12827 	if (IS_ERR(wdev)) {
12828 		err = PTR_ERR(wdev);
12829 		if (err != -EINVAL)
12830 			return err;
12831 		wdev = NULL;
12832 	} else if (wdev->wiphy != &rdev->wiphy) {
12833 		return -EINVAL;
12834 	}
12835 
12836 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12837 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12838 		return -EINVAL;
12839 
12840 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12841 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12842 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12843 		const struct wiphy_vendor_command *vcmd;
12844 		void *data = NULL;
12845 		int len = 0;
12846 
12847 		vcmd = &rdev->wiphy.vendor_commands[i];
12848 
12849 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12850 			continue;
12851 
12852 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12853 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12854 			if (!wdev)
12855 				return -EINVAL;
12856 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12857 			    !wdev->netdev)
12858 				return -EINVAL;
12859 
12860 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12861 				if (!wdev_running(wdev))
12862 					return -ENETDOWN;
12863 			}
12864 
12865 			if (!vcmd->doit)
12866 				return -EOPNOTSUPP;
12867 		} else {
12868 			wdev = NULL;
12869 		}
12870 
12871 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12872 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12873 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12874 
12875 			err = nl80211_vendor_check_policy(vcmd,
12876 					info->attrs[NL80211_ATTR_VENDOR_DATA],
12877 					info->extack);
12878 			if (err)
12879 				return err;
12880 		}
12881 
12882 		rdev->cur_cmd_info = info;
12883 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
12884 		rdev->cur_cmd_info = NULL;
12885 		return err;
12886 	}
12887 
12888 	return -EOPNOTSUPP;
12889 }
12890 
12891 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12892 				       struct netlink_callback *cb,
12893 				       struct cfg80211_registered_device **rdev,
12894 				       struct wireless_dev **wdev)
12895 {
12896 	struct nlattr **attrbuf;
12897 	u32 vid, subcmd;
12898 	unsigned int i;
12899 	int vcmd_idx = -1;
12900 	int err;
12901 	void *data = NULL;
12902 	unsigned int data_len = 0;
12903 
12904 	if (cb->args[0]) {
12905 		/* subtract the 1 again here */
12906 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12907 		struct wireless_dev *tmp;
12908 
12909 		if (!wiphy)
12910 			return -ENODEV;
12911 		*rdev = wiphy_to_rdev(wiphy);
12912 		*wdev = NULL;
12913 
12914 		if (cb->args[1]) {
12915 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
12916 				if (tmp->identifier == cb->args[1] - 1) {
12917 					*wdev = tmp;
12918 					break;
12919 				}
12920 			}
12921 		}
12922 
12923 		/* keep rtnl locked in successful case */
12924 		return 0;
12925 	}
12926 
12927 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
12928 	if (!attrbuf)
12929 		return -ENOMEM;
12930 
12931 	err = nlmsg_parse_deprecated(cb->nlh,
12932 				     GENL_HDRLEN + nl80211_fam.hdrsize,
12933 				     attrbuf, nl80211_fam.maxattr,
12934 				     nl80211_policy, NULL);
12935 	if (err)
12936 		goto out;
12937 
12938 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
12939 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
12940 		err = -EINVAL;
12941 		goto out;
12942 	}
12943 
12944 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
12945 	if (IS_ERR(*wdev))
12946 		*wdev = NULL;
12947 
12948 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
12949 	if (IS_ERR(*rdev)) {
12950 		err = PTR_ERR(*rdev);
12951 		goto out;
12952 	}
12953 
12954 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
12955 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
12956 
12957 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
12958 		const struct wiphy_vendor_command *vcmd;
12959 
12960 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
12961 
12962 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12963 			continue;
12964 
12965 		if (!vcmd->dumpit) {
12966 			err = -EOPNOTSUPP;
12967 			goto out;
12968 		}
12969 
12970 		vcmd_idx = i;
12971 		break;
12972 	}
12973 
12974 	if (vcmd_idx < 0) {
12975 		err = -EOPNOTSUPP;
12976 		goto out;
12977 	}
12978 
12979 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
12980 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12981 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12982 
12983 		err = nl80211_vendor_check_policy(
12984 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
12985 				attrbuf[NL80211_ATTR_VENDOR_DATA],
12986 				cb->extack);
12987 		if (err)
12988 			goto out;
12989 	}
12990 
12991 	/* 0 is the first index - add 1 to parse only once */
12992 	cb->args[0] = (*rdev)->wiphy_idx + 1;
12993 	/* add 1 to know if it was NULL */
12994 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
12995 	cb->args[2] = vcmd_idx;
12996 	cb->args[3] = (unsigned long)data;
12997 	cb->args[4] = data_len;
12998 
12999 	/* keep rtnl locked in successful case */
13000 	err = 0;
13001 out:
13002 	kfree(attrbuf);
13003 	return err;
13004 }
13005 
13006 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13007 				   struct netlink_callback *cb)
13008 {
13009 	struct cfg80211_registered_device *rdev;
13010 	struct wireless_dev *wdev;
13011 	unsigned int vcmd_idx;
13012 	const struct wiphy_vendor_command *vcmd;
13013 	void *data;
13014 	int data_len;
13015 	int err;
13016 	struct nlattr *vendor_data;
13017 
13018 	rtnl_lock();
13019 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13020 	if (err)
13021 		goto out;
13022 
13023 	vcmd_idx = cb->args[2];
13024 	data = (void *)cb->args[3];
13025 	data_len = cb->args[4];
13026 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13027 
13028 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13029 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13030 		if (!wdev) {
13031 			err = -EINVAL;
13032 			goto out;
13033 		}
13034 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13035 		    !wdev->netdev) {
13036 			err = -EINVAL;
13037 			goto out;
13038 		}
13039 
13040 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13041 			if (!wdev_running(wdev)) {
13042 				err = -ENETDOWN;
13043 				goto out;
13044 			}
13045 		}
13046 	}
13047 
13048 	while (1) {
13049 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13050 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13051 					   NL80211_CMD_VENDOR);
13052 		if (!hdr)
13053 			break;
13054 
13055 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13056 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13057 					       wdev_id(wdev),
13058 					       NL80211_ATTR_PAD))) {
13059 			genlmsg_cancel(skb, hdr);
13060 			break;
13061 		}
13062 
13063 		vendor_data = nla_nest_start_noflag(skb,
13064 						    NL80211_ATTR_VENDOR_DATA);
13065 		if (!vendor_data) {
13066 			genlmsg_cancel(skb, hdr);
13067 			break;
13068 		}
13069 
13070 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13071 				   (unsigned long *)&cb->args[5]);
13072 		nla_nest_end(skb, vendor_data);
13073 
13074 		if (err == -ENOBUFS || err == -ENOENT) {
13075 			genlmsg_cancel(skb, hdr);
13076 			break;
13077 		} else if (err) {
13078 			genlmsg_cancel(skb, hdr);
13079 			goto out;
13080 		}
13081 
13082 		genlmsg_end(skb, hdr);
13083 	}
13084 
13085 	err = skb->len;
13086  out:
13087 	rtnl_unlock();
13088 	return err;
13089 }
13090 
13091 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13092 					   enum nl80211_commands cmd,
13093 					   enum nl80211_attrs attr,
13094 					   int approxlen)
13095 {
13096 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13097 
13098 	if (WARN_ON(!rdev->cur_cmd_info))
13099 		return NULL;
13100 
13101 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13102 					   rdev->cur_cmd_info->snd_portid,
13103 					   rdev->cur_cmd_info->snd_seq,
13104 					   cmd, attr, NULL, GFP_KERNEL);
13105 }
13106 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13107 
13108 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13109 {
13110 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13111 	void *hdr = ((void **)skb->cb)[1];
13112 	struct nlattr *data = ((void **)skb->cb)[2];
13113 
13114 	/* clear CB data for netlink core to own from now on */
13115 	memset(skb->cb, 0, sizeof(skb->cb));
13116 
13117 	if (WARN_ON(!rdev->cur_cmd_info)) {
13118 		kfree_skb(skb);
13119 		return -EINVAL;
13120 	}
13121 
13122 	nla_nest_end(skb, data);
13123 	genlmsg_end(skb, hdr);
13124 	return genlmsg_reply(skb, rdev->cur_cmd_info);
13125 }
13126 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13127 
13128 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13129 {
13130 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13131 
13132 	if (WARN_ON(!rdev->cur_cmd_info))
13133 		return 0;
13134 
13135 	return rdev->cur_cmd_info->snd_portid;
13136 }
13137 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13138 
13139 static int nl80211_set_qos_map(struct sk_buff *skb,
13140 			       struct genl_info *info)
13141 {
13142 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13143 	struct cfg80211_qos_map *qos_map = NULL;
13144 	struct net_device *dev = info->user_ptr[1];
13145 	u8 *pos, len, num_des, des_len, des;
13146 	int ret;
13147 
13148 	if (!rdev->ops->set_qos_map)
13149 		return -EOPNOTSUPP;
13150 
13151 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13152 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13153 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13154 
13155 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13156 		    len > IEEE80211_QOS_MAP_LEN_MAX)
13157 			return -EINVAL;
13158 
13159 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13160 		if (!qos_map)
13161 			return -ENOMEM;
13162 
13163 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13164 		if (num_des) {
13165 			des_len = num_des *
13166 				sizeof(struct cfg80211_dscp_exception);
13167 			memcpy(qos_map->dscp_exception, pos, des_len);
13168 			qos_map->num_des = num_des;
13169 			for (des = 0; des < num_des; des++) {
13170 				if (qos_map->dscp_exception[des].up > 7) {
13171 					kfree(qos_map);
13172 					return -EINVAL;
13173 				}
13174 			}
13175 			pos += des_len;
13176 		}
13177 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13178 	}
13179 
13180 	wdev_lock(dev->ieee80211_ptr);
13181 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
13182 	if (!ret)
13183 		ret = rdev_set_qos_map(rdev, dev, qos_map);
13184 	wdev_unlock(dev->ieee80211_ptr);
13185 
13186 	kfree(qos_map);
13187 	return ret;
13188 }
13189 
13190 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13191 {
13192 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13193 	struct net_device *dev = info->user_ptr[1];
13194 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13195 	const u8 *peer;
13196 	u8 tsid, up;
13197 	u16 admitted_time = 0;
13198 	int err;
13199 
13200 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13201 		return -EOPNOTSUPP;
13202 
13203 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13204 	    !info->attrs[NL80211_ATTR_USER_PRIO])
13205 		return -EINVAL;
13206 
13207 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13208 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13209 
13210 	/* WMM uses TIDs 0-7 even for TSPEC */
13211 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13212 		/* TODO: handle 802.11 TSPEC/admission control
13213 		 * need more attributes for that (e.g. BA session requirement);
13214 		 * change the WMM adminssion test above to allow both then
13215 		 */
13216 		return -EINVAL;
13217 	}
13218 
13219 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13220 
13221 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13222 		admitted_time =
13223 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13224 		if (!admitted_time)
13225 			return -EINVAL;
13226 	}
13227 
13228 	wdev_lock(wdev);
13229 	switch (wdev->iftype) {
13230 	case NL80211_IFTYPE_STATION:
13231 	case NL80211_IFTYPE_P2P_CLIENT:
13232 		if (wdev->current_bss)
13233 			break;
13234 		err = -ENOTCONN;
13235 		goto out;
13236 	default:
13237 		err = -EOPNOTSUPP;
13238 		goto out;
13239 	}
13240 
13241 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13242 
13243  out:
13244 	wdev_unlock(wdev);
13245 	return err;
13246 }
13247 
13248 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13249 {
13250 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13251 	struct net_device *dev = info->user_ptr[1];
13252 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13253 	const u8 *peer;
13254 	u8 tsid;
13255 	int err;
13256 
13257 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13258 		return -EINVAL;
13259 
13260 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13261 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13262 
13263 	wdev_lock(wdev);
13264 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13265 	wdev_unlock(wdev);
13266 
13267 	return err;
13268 }
13269 
13270 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13271 				       struct genl_info *info)
13272 {
13273 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13274 	struct net_device *dev = info->user_ptr[1];
13275 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13276 	struct cfg80211_chan_def chandef = {};
13277 	const u8 *addr;
13278 	u8 oper_class;
13279 	int err;
13280 
13281 	if (!rdev->ops->tdls_channel_switch ||
13282 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13283 		return -EOPNOTSUPP;
13284 
13285 	switch (dev->ieee80211_ptr->iftype) {
13286 	case NL80211_IFTYPE_STATION:
13287 	case NL80211_IFTYPE_P2P_CLIENT:
13288 		break;
13289 	default:
13290 		return -EOPNOTSUPP;
13291 	}
13292 
13293 	if (!info->attrs[NL80211_ATTR_MAC] ||
13294 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
13295 		return -EINVAL;
13296 
13297 	err = nl80211_parse_chandef(rdev, info, &chandef);
13298 	if (err)
13299 		return err;
13300 
13301 	/*
13302 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13303 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13304 	 * specification is not defined for them.
13305 	 */
13306 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
13307 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13308 	    chandef.width != NL80211_CHAN_WIDTH_20)
13309 		return -EINVAL;
13310 
13311 	/* we will be active on the TDLS link */
13312 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13313 					   wdev->iftype))
13314 		return -EINVAL;
13315 
13316 	/* don't allow switching to DFS channels */
13317 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13318 		return -EINVAL;
13319 
13320 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13321 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13322 
13323 	wdev_lock(wdev);
13324 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13325 	wdev_unlock(wdev);
13326 
13327 	return err;
13328 }
13329 
13330 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13331 					      struct genl_info *info)
13332 {
13333 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13334 	struct net_device *dev = info->user_ptr[1];
13335 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13336 	const u8 *addr;
13337 
13338 	if (!rdev->ops->tdls_channel_switch ||
13339 	    !rdev->ops->tdls_cancel_channel_switch ||
13340 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13341 		return -EOPNOTSUPP;
13342 
13343 	switch (dev->ieee80211_ptr->iftype) {
13344 	case NL80211_IFTYPE_STATION:
13345 	case NL80211_IFTYPE_P2P_CLIENT:
13346 		break;
13347 	default:
13348 		return -EOPNOTSUPP;
13349 	}
13350 
13351 	if (!info->attrs[NL80211_ATTR_MAC])
13352 		return -EINVAL;
13353 
13354 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13355 
13356 	wdev_lock(wdev);
13357 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13358 	wdev_unlock(wdev);
13359 
13360 	return 0;
13361 }
13362 
13363 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13364 					    struct genl_info *info)
13365 {
13366 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13367 	struct net_device *dev = info->user_ptr[1];
13368 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13369 	const struct nlattr *nla;
13370 	bool enabled;
13371 
13372 	if (!rdev->ops->set_multicast_to_unicast)
13373 		return -EOPNOTSUPP;
13374 
13375 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13376 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13377 		return -EOPNOTSUPP;
13378 
13379 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13380 	enabled = nla_get_flag(nla);
13381 
13382 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13383 }
13384 
13385 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13386 {
13387 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13388 	struct net_device *dev = info->user_ptr[1];
13389 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13390 	struct cfg80211_pmk_conf pmk_conf = {};
13391 	int ret;
13392 
13393 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13394 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13395 		return -EOPNOTSUPP;
13396 
13397 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13398 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13399 		return -EOPNOTSUPP;
13400 
13401 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13402 		return -EINVAL;
13403 
13404 	wdev_lock(wdev);
13405 	if (!wdev->current_bss) {
13406 		ret = -ENOTCONN;
13407 		goto out;
13408 	}
13409 
13410 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13411 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13412 		ret = -EINVAL;
13413 		goto out;
13414 	}
13415 
13416 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13417 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13418 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13419 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13420 		ret = -EINVAL;
13421 		goto out;
13422 	}
13423 
13424 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13425 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13426 
13427 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
13428 			ret = -EINVAL;
13429 			goto out;
13430 		}
13431 
13432 		pmk_conf.pmk_r0_name =
13433 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13434 	}
13435 
13436 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13437 out:
13438 	wdev_unlock(wdev);
13439 	return ret;
13440 }
13441 
13442 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13443 {
13444 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13445 	struct net_device *dev = info->user_ptr[1];
13446 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13447 	const u8 *aa;
13448 	int ret;
13449 
13450 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13451 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13452 		return -EOPNOTSUPP;
13453 
13454 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13455 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13456 		return -EOPNOTSUPP;
13457 
13458 	if (!info->attrs[NL80211_ATTR_MAC])
13459 		return -EINVAL;
13460 
13461 	wdev_lock(wdev);
13462 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13463 	ret = rdev_del_pmk(rdev, dev, aa);
13464 	wdev_unlock(wdev);
13465 
13466 	return ret;
13467 }
13468 
13469 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13470 {
13471 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13472 	struct net_device *dev = info->user_ptr[1];
13473 	struct cfg80211_external_auth_params params;
13474 
13475 	if (!rdev->ops->external_auth)
13476 		return -EOPNOTSUPP;
13477 
13478 	if (!info->attrs[NL80211_ATTR_SSID] &&
13479 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13480 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13481 		return -EINVAL;
13482 
13483 	if (!info->attrs[NL80211_ATTR_BSSID])
13484 		return -EINVAL;
13485 
13486 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13487 		return -EINVAL;
13488 
13489 	memset(&params, 0, sizeof(params));
13490 
13491 	if (info->attrs[NL80211_ATTR_SSID]) {
13492 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13493 		if (params.ssid.ssid_len == 0 ||
13494 		    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13495 			return -EINVAL;
13496 		memcpy(params.ssid.ssid,
13497 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
13498 		       params.ssid.ssid_len);
13499 	}
13500 
13501 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13502 	       ETH_ALEN);
13503 
13504 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13505 
13506 	if (info->attrs[NL80211_ATTR_PMKID])
13507 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13508 
13509 	return rdev_external_auth(rdev, dev, &params);
13510 }
13511 
13512 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13513 {
13514 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13515 	struct net_device *dev = info->user_ptr[1];
13516 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13517 	const u8 *buf;
13518 	size_t len;
13519 	u8 *dest;
13520 	u16 proto;
13521 	bool noencrypt;
13522 	int err;
13523 
13524 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13525 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13526 		return -EOPNOTSUPP;
13527 
13528 	if (!rdev->ops->tx_control_port)
13529 		return -EOPNOTSUPP;
13530 
13531 	if (!info->attrs[NL80211_ATTR_FRAME] ||
13532 	    !info->attrs[NL80211_ATTR_MAC] ||
13533 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13534 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13535 		return -EINVAL;
13536 	}
13537 
13538 	wdev_lock(wdev);
13539 
13540 	switch (wdev->iftype) {
13541 	case NL80211_IFTYPE_AP:
13542 	case NL80211_IFTYPE_P2P_GO:
13543 	case NL80211_IFTYPE_MESH_POINT:
13544 		break;
13545 	case NL80211_IFTYPE_ADHOC:
13546 	case NL80211_IFTYPE_STATION:
13547 	case NL80211_IFTYPE_P2P_CLIENT:
13548 		if (wdev->current_bss)
13549 			break;
13550 		err = -ENOTCONN;
13551 		goto out;
13552 	default:
13553 		err = -EOPNOTSUPP;
13554 		goto out;
13555 	}
13556 
13557 	wdev_unlock(wdev);
13558 
13559 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13560 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13561 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13562 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13563 	noencrypt =
13564 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13565 
13566 	return rdev_tx_control_port(rdev, dev, buf, len,
13567 				    dest, cpu_to_be16(proto), noencrypt);
13568 
13569  out:
13570 	wdev_unlock(wdev);
13571 	return err;
13572 }
13573 
13574 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13575 					   struct genl_info *info)
13576 {
13577 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13578 	struct net_device *dev = info->user_ptr[1];
13579 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13580 	struct cfg80211_ftm_responder_stats ftm_stats = {};
13581 	struct sk_buff *msg;
13582 	void *hdr;
13583 	struct nlattr *ftm_stats_attr;
13584 	int err;
13585 
13586 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13587 		return -EOPNOTSUPP;
13588 
13589 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13590 	if (err)
13591 		return err;
13592 
13593 	if (!ftm_stats.filled)
13594 		return -ENODATA;
13595 
13596 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13597 	if (!msg)
13598 		return -ENOMEM;
13599 
13600 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13601 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
13602 	if (!hdr)
13603 		return -ENOBUFS;
13604 
13605 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
13606 		goto nla_put_failure;
13607 
13608 	ftm_stats_attr = nla_nest_start_noflag(msg,
13609 					       NL80211_ATTR_FTM_RESPONDER_STATS);
13610 	if (!ftm_stats_attr)
13611 		goto nla_put_failure;
13612 
13613 #define SET_FTM(field, name, type)					 \
13614 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13615 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
13616 			     ftm_stats.field))				 \
13617 		goto nla_put_failure; } while (0)
13618 #define SET_FTM_U64(field, name)					 \
13619 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13620 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
13621 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
13622 		goto nla_put_failure; } while (0)
13623 
13624 	SET_FTM(success_num, SUCCESS_NUM, u32);
13625 	SET_FTM(partial_num, PARTIAL_NUM, u32);
13626 	SET_FTM(failed_num, FAILED_NUM, u32);
13627 	SET_FTM(asap_num, ASAP_NUM, u32);
13628 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
13629 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
13630 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
13631 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
13632 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
13633 #undef SET_FTM
13634 
13635 	nla_nest_end(msg, ftm_stats_attr);
13636 
13637 	genlmsg_end(msg, hdr);
13638 	return genlmsg_reply(msg, info);
13639 
13640 nla_put_failure:
13641 	nlmsg_free(msg);
13642 	return -ENOBUFS;
13643 }
13644 
13645 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
13646 {
13647 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13648 	struct cfg80211_update_owe_info owe_info;
13649 	struct net_device *dev = info->user_ptr[1];
13650 
13651 	if (!rdev->ops->update_owe_info)
13652 		return -EOPNOTSUPP;
13653 
13654 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
13655 	    !info->attrs[NL80211_ATTR_MAC])
13656 		return -EINVAL;
13657 
13658 	memset(&owe_info, 0, sizeof(owe_info));
13659 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13660 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
13661 
13662 	if (info->attrs[NL80211_ATTR_IE]) {
13663 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13664 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13665 	}
13666 
13667 	return rdev_update_owe_info(rdev, dev, &owe_info);
13668 }
13669 
13670 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
13671 {
13672 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13673 	struct net_device *dev = info->user_ptr[1];
13674 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13675 	struct station_info sinfo = {};
13676 	const u8 *buf;
13677 	size_t len;
13678 	u8 *dest;
13679 	int err;
13680 
13681 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
13682 		return -EOPNOTSUPP;
13683 
13684 	if (!info->attrs[NL80211_ATTR_MAC] ||
13685 	    !info->attrs[NL80211_ATTR_FRAME]) {
13686 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
13687 		return -EINVAL;
13688 	}
13689 
13690 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
13691 		return -EOPNOTSUPP;
13692 
13693 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13694 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13695 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13696 
13697 	if (len < sizeof(struct ethhdr))
13698 		return -EINVAL;
13699 
13700 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
13701 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
13702 		return -EINVAL;
13703 
13704 	err = rdev_get_station(rdev, dev, dest, &sinfo);
13705 	if (err)
13706 		return err;
13707 
13708 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
13709 }
13710 
13711 #define NL80211_FLAG_NEED_WIPHY		0x01
13712 #define NL80211_FLAG_NEED_NETDEV	0x02
13713 #define NL80211_FLAG_NEED_RTNL		0x04
13714 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
13715 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
13716 					 NL80211_FLAG_CHECK_NETDEV_UP)
13717 #define NL80211_FLAG_NEED_WDEV		0x10
13718 /* If a netdev is associated, it must be UP, P2P must be started */
13719 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
13720 					 NL80211_FLAG_CHECK_NETDEV_UP)
13721 #define NL80211_FLAG_CLEAR_SKB		0x20
13722 
13723 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13724 			    struct genl_info *info)
13725 {
13726 	struct cfg80211_registered_device *rdev;
13727 	struct wireless_dev *wdev;
13728 	struct net_device *dev;
13729 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13730 
13731 	if (rtnl)
13732 		rtnl_lock();
13733 
13734 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13735 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13736 		if (IS_ERR(rdev)) {
13737 			if (rtnl)
13738 				rtnl_unlock();
13739 			return PTR_ERR(rdev);
13740 		}
13741 		info->user_ptr[0] = rdev;
13742 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13743 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13744 		ASSERT_RTNL();
13745 
13746 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13747 						  info->attrs);
13748 		if (IS_ERR(wdev)) {
13749 			if (rtnl)
13750 				rtnl_unlock();
13751 			return PTR_ERR(wdev);
13752 		}
13753 
13754 		dev = wdev->netdev;
13755 		rdev = wiphy_to_rdev(wdev->wiphy);
13756 
13757 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13758 			if (!dev) {
13759 				if (rtnl)
13760 					rtnl_unlock();
13761 				return -EINVAL;
13762 			}
13763 
13764 			info->user_ptr[1] = dev;
13765 		} else {
13766 			info->user_ptr[1] = wdev;
13767 		}
13768 
13769 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13770 		    !wdev_running(wdev)) {
13771 			if (rtnl)
13772 				rtnl_unlock();
13773 			return -ENETDOWN;
13774 		}
13775 
13776 		if (dev)
13777 			dev_hold(dev);
13778 
13779 		info->user_ptr[0] = rdev;
13780 	}
13781 
13782 	return 0;
13783 }
13784 
13785 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13786 			      struct genl_info *info)
13787 {
13788 	if (info->user_ptr[1]) {
13789 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13790 			struct wireless_dev *wdev = info->user_ptr[1];
13791 
13792 			if (wdev->netdev)
13793 				dev_put(wdev->netdev);
13794 		} else {
13795 			dev_put(info->user_ptr[1]);
13796 		}
13797 	}
13798 
13799 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13800 		rtnl_unlock();
13801 
13802 	/* If needed, clear the netlink message payload from the SKB
13803 	 * as it might contain key data that shouldn't stick around on
13804 	 * the heap after the SKB is freed. The netlink message header
13805 	 * is still needed for further processing, so leave it intact.
13806 	 */
13807 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13808 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
13809 
13810 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13811 	}
13812 }
13813 
13814 static const struct genl_ops nl80211_ops[] = {
13815 	{
13816 		.cmd = NL80211_CMD_GET_WIPHY,
13817 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13818 		.doit = nl80211_get_wiphy,
13819 		.dumpit = nl80211_dump_wiphy,
13820 		.done = nl80211_dump_wiphy_done,
13821 		/* can be retrieved by unprivileged users */
13822 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13823 				  NL80211_FLAG_NEED_RTNL,
13824 	},
13825 	{
13826 		.cmd = NL80211_CMD_SET_WIPHY,
13827 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13828 		.doit = nl80211_set_wiphy,
13829 		.flags = GENL_UNS_ADMIN_PERM,
13830 		.internal_flags = NL80211_FLAG_NEED_RTNL,
13831 	},
13832 	{
13833 		.cmd = NL80211_CMD_GET_INTERFACE,
13834 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13835 		.doit = nl80211_get_interface,
13836 		.dumpit = nl80211_dump_interface,
13837 		/* can be retrieved by unprivileged users */
13838 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13839 				  NL80211_FLAG_NEED_RTNL,
13840 	},
13841 	{
13842 		.cmd = NL80211_CMD_SET_INTERFACE,
13843 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13844 		.doit = nl80211_set_interface,
13845 		.flags = GENL_UNS_ADMIN_PERM,
13846 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13847 				  NL80211_FLAG_NEED_RTNL,
13848 	},
13849 	{
13850 		.cmd = NL80211_CMD_NEW_INTERFACE,
13851 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13852 		.doit = nl80211_new_interface,
13853 		.flags = GENL_UNS_ADMIN_PERM,
13854 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13855 				  NL80211_FLAG_NEED_RTNL,
13856 	},
13857 	{
13858 		.cmd = NL80211_CMD_DEL_INTERFACE,
13859 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13860 		.doit = nl80211_del_interface,
13861 		.flags = GENL_UNS_ADMIN_PERM,
13862 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13863 				  NL80211_FLAG_NEED_RTNL,
13864 	},
13865 	{
13866 		.cmd = NL80211_CMD_GET_KEY,
13867 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13868 		.doit = nl80211_get_key,
13869 		.flags = GENL_UNS_ADMIN_PERM,
13870 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13871 				  NL80211_FLAG_NEED_RTNL,
13872 	},
13873 	{
13874 		.cmd = NL80211_CMD_SET_KEY,
13875 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13876 		.doit = nl80211_set_key,
13877 		.flags = GENL_UNS_ADMIN_PERM,
13878 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13879 				  NL80211_FLAG_NEED_RTNL |
13880 				  NL80211_FLAG_CLEAR_SKB,
13881 	},
13882 	{
13883 		.cmd = NL80211_CMD_NEW_KEY,
13884 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13885 		.doit = nl80211_new_key,
13886 		.flags = GENL_UNS_ADMIN_PERM,
13887 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13888 				  NL80211_FLAG_NEED_RTNL |
13889 				  NL80211_FLAG_CLEAR_SKB,
13890 	},
13891 	{
13892 		.cmd = NL80211_CMD_DEL_KEY,
13893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13894 		.doit = nl80211_del_key,
13895 		.flags = GENL_UNS_ADMIN_PERM,
13896 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13897 				  NL80211_FLAG_NEED_RTNL,
13898 	},
13899 	{
13900 		.cmd = NL80211_CMD_SET_BEACON,
13901 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13902 		.flags = GENL_UNS_ADMIN_PERM,
13903 		.doit = nl80211_set_beacon,
13904 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13905 				  NL80211_FLAG_NEED_RTNL,
13906 	},
13907 	{
13908 		.cmd = NL80211_CMD_START_AP,
13909 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13910 		.flags = GENL_UNS_ADMIN_PERM,
13911 		.doit = nl80211_start_ap,
13912 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13913 				  NL80211_FLAG_NEED_RTNL,
13914 	},
13915 	{
13916 		.cmd = NL80211_CMD_STOP_AP,
13917 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13918 		.flags = GENL_UNS_ADMIN_PERM,
13919 		.doit = nl80211_stop_ap,
13920 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13921 				  NL80211_FLAG_NEED_RTNL,
13922 	},
13923 	{
13924 		.cmd = NL80211_CMD_GET_STATION,
13925 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13926 		.doit = nl80211_get_station,
13927 		.dumpit = nl80211_dump_station,
13928 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13929 				  NL80211_FLAG_NEED_RTNL,
13930 	},
13931 	{
13932 		.cmd = NL80211_CMD_SET_STATION,
13933 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13934 		.doit = nl80211_set_station,
13935 		.flags = GENL_UNS_ADMIN_PERM,
13936 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13937 				  NL80211_FLAG_NEED_RTNL,
13938 	},
13939 	{
13940 		.cmd = NL80211_CMD_NEW_STATION,
13941 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13942 		.doit = nl80211_new_station,
13943 		.flags = GENL_UNS_ADMIN_PERM,
13944 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13945 				  NL80211_FLAG_NEED_RTNL,
13946 	},
13947 	{
13948 		.cmd = NL80211_CMD_DEL_STATION,
13949 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13950 		.doit = nl80211_del_station,
13951 		.flags = GENL_UNS_ADMIN_PERM,
13952 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13953 				  NL80211_FLAG_NEED_RTNL,
13954 	},
13955 	{
13956 		.cmd = NL80211_CMD_GET_MPATH,
13957 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13958 		.doit = nl80211_get_mpath,
13959 		.dumpit = nl80211_dump_mpath,
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_GET_MPP,
13966 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13967 		.doit = nl80211_get_mpp,
13968 		.dumpit = nl80211_dump_mpp,
13969 		.flags = GENL_UNS_ADMIN_PERM,
13970 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13971 				  NL80211_FLAG_NEED_RTNL,
13972 	},
13973 	{
13974 		.cmd = NL80211_CMD_SET_MPATH,
13975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13976 		.doit = nl80211_set_mpath,
13977 		.flags = GENL_UNS_ADMIN_PERM,
13978 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13979 				  NL80211_FLAG_NEED_RTNL,
13980 	},
13981 	{
13982 		.cmd = NL80211_CMD_NEW_MPATH,
13983 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13984 		.doit = nl80211_new_mpath,
13985 		.flags = GENL_UNS_ADMIN_PERM,
13986 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13987 				  NL80211_FLAG_NEED_RTNL,
13988 	},
13989 	{
13990 		.cmd = NL80211_CMD_DEL_MPATH,
13991 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13992 		.doit = nl80211_del_mpath,
13993 		.flags = GENL_UNS_ADMIN_PERM,
13994 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13995 				  NL80211_FLAG_NEED_RTNL,
13996 	},
13997 	{
13998 		.cmd = NL80211_CMD_SET_BSS,
13999 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14000 		.doit = nl80211_set_bss,
14001 		.flags = GENL_UNS_ADMIN_PERM,
14002 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14003 				  NL80211_FLAG_NEED_RTNL,
14004 	},
14005 	{
14006 		.cmd = NL80211_CMD_GET_REG,
14007 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14008 		.doit = nl80211_get_reg_do,
14009 		.dumpit = nl80211_get_reg_dump,
14010 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14011 		/* can be retrieved by unprivileged users */
14012 	},
14013 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14014 	{
14015 		.cmd = NL80211_CMD_SET_REG,
14016 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14017 		.doit = nl80211_set_reg,
14018 		.flags = GENL_ADMIN_PERM,
14019 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14020 	},
14021 #endif
14022 	{
14023 		.cmd = NL80211_CMD_REQ_SET_REG,
14024 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14025 		.doit = nl80211_req_set_reg,
14026 		.flags = GENL_ADMIN_PERM,
14027 	},
14028 	{
14029 		.cmd = NL80211_CMD_RELOAD_REGDB,
14030 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14031 		.doit = nl80211_reload_regdb,
14032 		.flags = GENL_ADMIN_PERM,
14033 	},
14034 	{
14035 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
14036 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14037 		.doit = nl80211_get_mesh_config,
14038 		/* can be retrieved by unprivileged users */
14039 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14040 				  NL80211_FLAG_NEED_RTNL,
14041 	},
14042 	{
14043 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
14044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14045 		.doit = nl80211_update_mesh_config,
14046 		.flags = GENL_UNS_ADMIN_PERM,
14047 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14048 				  NL80211_FLAG_NEED_RTNL,
14049 	},
14050 	{
14051 		.cmd = NL80211_CMD_TRIGGER_SCAN,
14052 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14053 		.doit = nl80211_trigger_scan,
14054 		.flags = GENL_UNS_ADMIN_PERM,
14055 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14056 				  NL80211_FLAG_NEED_RTNL,
14057 	},
14058 	{
14059 		.cmd = NL80211_CMD_ABORT_SCAN,
14060 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14061 		.doit = nl80211_abort_scan,
14062 		.flags = GENL_UNS_ADMIN_PERM,
14063 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14064 				  NL80211_FLAG_NEED_RTNL,
14065 	},
14066 	{
14067 		.cmd = NL80211_CMD_GET_SCAN,
14068 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14069 		.dumpit = nl80211_dump_scan,
14070 	},
14071 	{
14072 		.cmd = NL80211_CMD_START_SCHED_SCAN,
14073 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14074 		.doit = nl80211_start_sched_scan,
14075 		.flags = GENL_UNS_ADMIN_PERM,
14076 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14077 				  NL80211_FLAG_NEED_RTNL,
14078 	},
14079 	{
14080 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
14081 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14082 		.doit = nl80211_stop_sched_scan,
14083 		.flags = GENL_UNS_ADMIN_PERM,
14084 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14085 				  NL80211_FLAG_NEED_RTNL,
14086 	},
14087 	{
14088 		.cmd = NL80211_CMD_AUTHENTICATE,
14089 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14090 		.doit = nl80211_authenticate,
14091 		.flags = GENL_UNS_ADMIN_PERM,
14092 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14093 				  NL80211_FLAG_NEED_RTNL |
14094 				  NL80211_FLAG_CLEAR_SKB,
14095 	},
14096 	{
14097 		.cmd = NL80211_CMD_ASSOCIATE,
14098 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14099 		.doit = nl80211_associate,
14100 		.flags = GENL_UNS_ADMIN_PERM,
14101 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14102 				  NL80211_FLAG_NEED_RTNL |
14103 				  NL80211_FLAG_CLEAR_SKB,
14104 	},
14105 	{
14106 		.cmd = NL80211_CMD_DEAUTHENTICATE,
14107 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14108 		.doit = nl80211_deauthenticate,
14109 		.flags = GENL_UNS_ADMIN_PERM,
14110 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14111 				  NL80211_FLAG_NEED_RTNL,
14112 	},
14113 	{
14114 		.cmd = NL80211_CMD_DISASSOCIATE,
14115 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14116 		.doit = nl80211_disassociate,
14117 		.flags = GENL_UNS_ADMIN_PERM,
14118 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14119 				  NL80211_FLAG_NEED_RTNL,
14120 	},
14121 	{
14122 		.cmd = NL80211_CMD_JOIN_IBSS,
14123 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14124 		.doit = nl80211_join_ibss,
14125 		.flags = GENL_UNS_ADMIN_PERM,
14126 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14127 				  NL80211_FLAG_NEED_RTNL,
14128 	},
14129 	{
14130 		.cmd = NL80211_CMD_LEAVE_IBSS,
14131 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14132 		.doit = nl80211_leave_ibss,
14133 		.flags = GENL_UNS_ADMIN_PERM,
14134 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14135 				  NL80211_FLAG_NEED_RTNL,
14136 	},
14137 #ifdef CONFIG_NL80211_TESTMODE
14138 	{
14139 		.cmd = NL80211_CMD_TESTMODE,
14140 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14141 		.doit = nl80211_testmode_do,
14142 		.dumpit = nl80211_testmode_dump,
14143 		.flags = GENL_UNS_ADMIN_PERM,
14144 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14145 				  NL80211_FLAG_NEED_RTNL,
14146 	},
14147 #endif
14148 	{
14149 		.cmd = NL80211_CMD_CONNECT,
14150 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14151 		.doit = nl80211_connect,
14152 		.flags = GENL_UNS_ADMIN_PERM,
14153 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14154 				  NL80211_FLAG_NEED_RTNL |
14155 				  NL80211_FLAG_CLEAR_SKB,
14156 	},
14157 	{
14158 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14159 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14160 		.doit = nl80211_update_connect_params,
14161 		.flags = GENL_ADMIN_PERM,
14162 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14163 				  NL80211_FLAG_NEED_RTNL |
14164 				  NL80211_FLAG_CLEAR_SKB,
14165 	},
14166 	{
14167 		.cmd = NL80211_CMD_DISCONNECT,
14168 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14169 		.doit = nl80211_disconnect,
14170 		.flags = GENL_UNS_ADMIN_PERM,
14171 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14172 				  NL80211_FLAG_NEED_RTNL,
14173 	},
14174 	{
14175 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
14176 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14177 		.doit = nl80211_wiphy_netns,
14178 		.flags = GENL_UNS_ADMIN_PERM,
14179 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14180 				  NL80211_FLAG_NEED_RTNL,
14181 	},
14182 	{
14183 		.cmd = NL80211_CMD_GET_SURVEY,
14184 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14185 		.dumpit = nl80211_dump_survey,
14186 	},
14187 	{
14188 		.cmd = NL80211_CMD_SET_PMKSA,
14189 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14190 		.doit = nl80211_setdel_pmksa,
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_DEL_PMKSA,
14198 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14199 		.doit = nl80211_setdel_pmksa,
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_FLUSH_PMKSA,
14206 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14207 		.doit = nl80211_flush_pmksa,
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_REMAIN_ON_CHANNEL,
14214 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14215 		.doit = nl80211_remain_on_channel,
14216 		.flags = GENL_UNS_ADMIN_PERM,
14217 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14218 				  NL80211_FLAG_NEED_RTNL,
14219 	},
14220 	{
14221 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14222 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14223 		.doit = nl80211_cancel_remain_on_channel,
14224 		.flags = GENL_UNS_ADMIN_PERM,
14225 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14226 				  NL80211_FLAG_NEED_RTNL,
14227 	},
14228 	{
14229 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14230 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14231 		.doit = nl80211_set_tx_bitrate_mask,
14232 		.flags = GENL_UNS_ADMIN_PERM,
14233 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14234 				  NL80211_FLAG_NEED_RTNL,
14235 	},
14236 	{
14237 		.cmd = NL80211_CMD_REGISTER_FRAME,
14238 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14239 		.doit = nl80211_register_mgmt,
14240 		.flags = GENL_UNS_ADMIN_PERM,
14241 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14242 				  NL80211_FLAG_NEED_RTNL,
14243 	},
14244 	{
14245 		.cmd = NL80211_CMD_FRAME,
14246 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14247 		.doit = nl80211_tx_mgmt,
14248 		.flags = GENL_UNS_ADMIN_PERM,
14249 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14250 				  NL80211_FLAG_NEED_RTNL,
14251 	},
14252 	{
14253 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14254 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14255 		.doit = nl80211_tx_mgmt_cancel_wait,
14256 		.flags = GENL_UNS_ADMIN_PERM,
14257 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14258 				  NL80211_FLAG_NEED_RTNL,
14259 	},
14260 	{
14261 		.cmd = NL80211_CMD_SET_POWER_SAVE,
14262 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14263 		.doit = nl80211_set_power_save,
14264 		.flags = GENL_UNS_ADMIN_PERM,
14265 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14266 				  NL80211_FLAG_NEED_RTNL,
14267 	},
14268 	{
14269 		.cmd = NL80211_CMD_GET_POWER_SAVE,
14270 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14271 		.doit = nl80211_get_power_save,
14272 		/* can be retrieved by unprivileged users */
14273 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14274 				  NL80211_FLAG_NEED_RTNL,
14275 	},
14276 	{
14277 		.cmd = NL80211_CMD_SET_CQM,
14278 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14279 		.doit = nl80211_set_cqm,
14280 		.flags = GENL_UNS_ADMIN_PERM,
14281 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14282 				  NL80211_FLAG_NEED_RTNL,
14283 	},
14284 	{
14285 		.cmd = NL80211_CMD_SET_CHANNEL,
14286 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14287 		.doit = nl80211_set_channel,
14288 		.flags = GENL_UNS_ADMIN_PERM,
14289 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14290 				  NL80211_FLAG_NEED_RTNL,
14291 	},
14292 	{
14293 		.cmd = NL80211_CMD_SET_WDS_PEER,
14294 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14295 		.doit = nl80211_set_wds_peer,
14296 		.flags = GENL_UNS_ADMIN_PERM,
14297 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14298 				  NL80211_FLAG_NEED_RTNL,
14299 	},
14300 	{
14301 		.cmd = NL80211_CMD_JOIN_MESH,
14302 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14303 		.doit = nl80211_join_mesh,
14304 		.flags = GENL_UNS_ADMIN_PERM,
14305 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14306 				  NL80211_FLAG_NEED_RTNL,
14307 	},
14308 	{
14309 		.cmd = NL80211_CMD_LEAVE_MESH,
14310 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14311 		.doit = nl80211_leave_mesh,
14312 		.flags = GENL_UNS_ADMIN_PERM,
14313 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14314 				  NL80211_FLAG_NEED_RTNL,
14315 	},
14316 	{
14317 		.cmd = NL80211_CMD_JOIN_OCB,
14318 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14319 		.doit = nl80211_join_ocb,
14320 		.flags = GENL_UNS_ADMIN_PERM,
14321 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14322 				  NL80211_FLAG_NEED_RTNL,
14323 	},
14324 	{
14325 		.cmd = NL80211_CMD_LEAVE_OCB,
14326 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14327 		.doit = nl80211_leave_ocb,
14328 		.flags = GENL_UNS_ADMIN_PERM,
14329 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14330 				  NL80211_FLAG_NEED_RTNL,
14331 	},
14332 #ifdef CONFIG_PM
14333 	{
14334 		.cmd = NL80211_CMD_GET_WOWLAN,
14335 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14336 		.doit = nl80211_get_wowlan,
14337 		/* can be retrieved by unprivileged users */
14338 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14339 				  NL80211_FLAG_NEED_RTNL,
14340 	},
14341 	{
14342 		.cmd = NL80211_CMD_SET_WOWLAN,
14343 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14344 		.doit = nl80211_set_wowlan,
14345 		.flags = GENL_UNS_ADMIN_PERM,
14346 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14347 				  NL80211_FLAG_NEED_RTNL,
14348 	},
14349 #endif
14350 	{
14351 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14352 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14353 		.doit = nl80211_set_rekey_data,
14354 		.flags = GENL_UNS_ADMIN_PERM,
14355 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14356 				  NL80211_FLAG_NEED_RTNL |
14357 				  NL80211_FLAG_CLEAR_SKB,
14358 	},
14359 	{
14360 		.cmd = NL80211_CMD_TDLS_MGMT,
14361 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14362 		.doit = nl80211_tdls_mgmt,
14363 		.flags = GENL_UNS_ADMIN_PERM,
14364 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14365 				  NL80211_FLAG_NEED_RTNL,
14366 	},
14367 	{
14368 		.cmd = NL80211_CMD_TDLS_OPER,
14369 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14370 		.doit = nl80211_tdls_oper,
14371 		.flags = GENL_UNS_ADMIN_PERM,
14372 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14373 				  NL80211_FLAG_NEED_RTNL,
14374 	},
14375 	{
14376 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
14377 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14378 		.doit = nl80211_register_unexpected_frame,
14379 		.flags = GENL_UNS_ADMIN_PERM,
14380 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14381 				  NL80211_FLAG_NEED_RTNL,
14382 	},
14383 	{
14384 		.cmd = NL80211_CMD_PROBE_CLIENT,
14385 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14386 		.doit = nl80211_probe_client,
14387 		.flags = GENL_UNS_ADMIN_PERM,
14388 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14389 				  NL80211_FLAG_NEED_RTNL,
14390 	},
14391 	{
14392 		.cmd = NL80211_CMD_REGISTER_BEACONS,
14393 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14394 		.doit = nl80211_register_beacons,
14395 		.flags = GENL_UNS_ADMIN_PERM,
14396 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14397 				  NL80211_FLAG_NEED_RTNL,
14398 	},
14399 	{
14400 		.cmd = NL80211_CMD_SET_NOACK_MAP,
14401 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14402 		.doit = nl80211_set_noack_map,
14403 		.flags = GENL_UNS_ADMIN_PERM,
14404 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14405 				  NL80211_FLAG_NEED_RTNL,
14406 	},
14407 	{
14408 		.cmd = NL80211_CMD_START_P2P_DEVICE,
14409 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14410 		.doit = nl80211_start_p2p_device,
14411 		.flags = GENL_UNS_ADMIN_PERM,
14412 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14413 				  NL80211_FLAG_NEED_RTNL,
14414 	},
14415 	{
14416 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
14417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14418 		.doit = nl80211_stop_p2p_device,
14419 		.flags = GENL_UNS_ADMIN_PERM,
14420 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14421 				  NL80211_FLAG_NEED_RTNL,
14422 	},
14423 	{
14424 		.cmd = NL80211_CMD_START_NAN,
14425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14426 		.doit = nl80211_start_nan,
14427 		.flags = GENL_ADMIN_PERM,
14428 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14429 				  NL80211_FLAG_NEED_RTNL,
14430 	},
14431 	{
14432 		.cmd = NL80211_CMD_STOP_NAN,
14433 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14434 		.doit = nl80211_stop_nan,
14435 		.flags = GENL_ADMIN_PERM,
14436 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14437 				  NL80211_FLAG_NEED_RTNL,
14438 	},
14439 	{
14440 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
14441 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14442 		.doit = nl80211_nan_add_func,
14443 		.flags = GENL_ADMIN_PERM,
14444 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14445 				  NL80211_FLAG_NEED_RTNL,
14446 	},
14447 	{
14448 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
14449 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14450 		.doit = nl80211_nan_del_func,
14451 		.flags = GENL_ADMIN_PERM,
14452 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14453 				  NL80211_FLAG_NEED_RTNL,
14454 	},
14455 	{
14456 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
14457 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14458 		.doit = nl80211_nan_change_config,
14459 		.flags = GENL_ADMIN_PERM,
14460 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14461 				  NL80211_FLAG_NEED_RTNL,
14462 	},
14463 	{
14464 		.cmd = NL80211_CMD_SET_MCAST_RATE,
14465 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14466 		.doit = nl80211_set_mcast_rate,
14467 		.flags = GENL_UNS_ADMIN_PERM,
14468 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14469 				  NL80211_FLAG_NEED_RTNL,
14470 	},
14471 	{
14472 		.cmd = NL80211_CMD_SET_MAC_ACL,
14473 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14474 		.doit = nl80211_set_mac_acl,
14475 		.flags = GENL_UNS_ADMIN_PERM,
14476 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14477 				  NL80211_FLAG_NEED_RTNL,
14478 	},
14479 	{
14480 		.cmd = NL80211_CMD_RADAR_DETECT,
14481 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14482 		.doit = nl80211_start_radar_detection,
14483 		.flags = GENL_UNS_ADMIN_PERM,
14484 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14485 				  NL80211_FLAG_NEED_RTNL,
14486 	},
14487 	{
14488 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
14489 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14490 		.doit = nl80211_get_protocol_features,
14491 	},
14492 	{
14493 		.cmd = NL80211_CMD_UPDATE_FT_IES,
14494 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14495 		.doit = nl80211_update_ft_ies,
14496 		.flags = GENL_UNS_ADMIN_PERM,
14497 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14498 				  NL80211_FLAG_NEED_RTNL,
14499 	},
14500 	{
14501 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
14502 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14503 		.doit = nl80211_crit_protocol_start,
14504 		.flags = GENL_UNS_ADMIN_PERM,
14505 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14506 				  NL80211_FLAG_NEED_RTNL,
14507 	},
14508 	{
14509 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
14510 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14511 		.doit = nl80211_crit_protocol_stop,
14512 		.flags = GENL_UNS_ADMIN_PERM,
14513 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14514 				  NL80211_FLAG_NEED_RTNL,
14515 	},
14516 	{
14517 		.cmd = NL80211_CMD_GET_COALESCE,
14518 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14519 		.doit = nl80211_get_coalesce,
14520 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14521 				  NL80211_FLAG_NEED_RTNL,
14522 	},
14523 	{
14524 		.cmd = NL80211_CMD_SET_COALESCE,
14525 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14526 		.doit = nl80211_set_coalesce,
14527 		.flags = GENL_UNS_ADMIN_PERM,
14528 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14529 				  NL80211_FLAG_NEED_RTNL,
14530 	},
14531 	{
14532 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
14533 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14534 		.doit = nl80211_channel_switch,
14535 		.flags = GENL_UNS_ADMIN_PERM,
14536 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14537 				  NL80211_FLAG_NEED_RTNL,
14538 	},
14539 	{
14540 		.cmd = NL80211_CMD_VENDOR,
14541 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14542 		.doit = nl80211_vendor_cmd,
14543 		.dumpit = nl80211_vendor_cmd_dump,
14544 		.flags = GENL_UNS_ADMIN_PERM,
14545 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14546 				  NL80211_FLAG_NEED_RTNL |
14547 				  NL80211_FLAG_CLEAR_SKB,
14548 	},
14549 	{
14550 		.cmd = NL80211_CMD_SET_QOS_MAP,
14551 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14552 		.doit = nl80211_set_qos_map,
14553 		.flags = GENL_UNS_ADMIN_PERM,
14554 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14555 				  NL80211_FLAG_NEED_RTNL,
14556 	},
14557 	{
14558 		.cmd = NL80211_CMD_ADD_TX_TS,
14559 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14560 		.doit = nl80211_add_tx_ts,
14561 		.flags = GENL_UNS_ADMIN_PERM,
14562 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14563 				  NL80211_FLAG_NEED_RTNL,
14564 	},
14565 	{
14566 		.cmd = NL80211_CMD_DEL_TX_TS,
14567 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14568 		.doit = nl80211_del_tx_ts,
14569 		.flags = GENL_UNS_ADMIN_PERM,
14570 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14571 				  NL80211_FLAG_NEED_RTNL,
14572 	},
14573 	{
14574 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
14575 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14576 		.doit = nl80211_tdls_channel_switch,
14577 		.flags = GENL_UNS_ADMIN_PERM,
14578 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14579 				  NL80211_FLAG_NEED_RTNL,
14580 	},
14581 	{
14582 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
14583 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14584 		.doit = nl80211_tdls_cancel_channel_switch,
14585 		.flags = GENL_UNS_ADMIN_PERM,
14586 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14587 				  NL80211_FLAG_NEED_RTNL,
14588 	},
14589 	{
14590 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
14591 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14592 		.doit = nl80211_set_multicast_to_unicast,
14593 		.flags = GENL_UNS_ADMIN_PERM,
14594 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14595 				  NL80211_FLAG_NEED_RTNL,
14596 	},
14597 	{
14598 		.cmd = NL80211_CMD_SET_PMK,
14599 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14600 		.doit = nl80211_set_pmk,
14601 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14602 				  NL80211_FLAG_NEED_RTNL |
14603 				  NL80211_FLAG_CLEAR_SKB,
14604 	},
14605 	{
14606 		.cmd = NL80211_CMD_DEL_PMK,
14607 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14608 		.doit = nl80211_del_pmk,
14609 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14610 				  NL80211_FLAG_NEED_RTNL,
14611 	},
14612 	{
14613 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
14614 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14615 		.doit = nl80211_external_auth,
14616 		.flags = GENL_ADMIN_PERM,
14617 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14618 				  NL80211_FLAG_NEED_RTNL,
14619 	},
14620 	{
14621 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
14622 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14623 		.doit = nl80211_tx_control_port,
14624 		.flags = GENL_UNS_ADMIN_PERM,
14625 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14626 				  NL80211_FLAG_NEED_RTNL,
14627 	},
14628 	{
14629 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
14630 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14631 		.doit = nl80211_get_ftm_responder_stats,
14632 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14633 				  NL80211_FLAG_NEED_RTNL,
14634 	},
14635 	{
14636 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
14637 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14638 		.doit = nl80211_pmsr_start,
14639 		.flags = GENL_UNS_ADMIN_PERM,
14640 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14641 				  NL80211_FLAG_NEED_RTNL,
14642 	},
14643 	{
14644 		.cmd = NL80211_CMD_NOTIFY_RADAR,
14645 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14646 		.doit = nl80211_notify_radar_detection,
14647 		.flags = GENL_UNS_ADMIN_PERM,
14648 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14649 				  NL80211_FLAG_NEED_RTNL,
14650 	},
14651 	{
14652 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
14653 		.doit = nl80211_update_owe_info,
14654 		.flags = GENL_ADMIN_PERM,
14655 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14656 				  NL80211_FLAG_NEED_RTNL,
14657 	},
14658 	{
14659 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
14660 		.doit = nl80211_probe_mesh_link,
14661 		.flags = GENL_UNS_ADMIN_PERM,
14662 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14663 				  NL80211_FLAG_NEED_RTNL,
14664 	},
14665 };
14666 
14667 static struct genl_family nl80211_fam __ro_after_init = {
14668 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
14669 	.hdrsize = 0,			/* no private header */
14670 	.version = 1,			/* no particular meaning now */
14671 	.maxattr = NL80211_ATTR_MAX,
14672 	.policy = nl80211_policy,
14673 	.netnsok = true,
14674 	.pre_doit = nl80211_pre_doit,
14675 	.post_doit = nl80211_post_doit,
14676 	.module = THIS_MODULE,
14677 	.ops = nl80211_ops,
14678 	.n_ops = ARRAY_SIZE(nl80211_ops),
14679 	.mcgrps = nl80211_mcgrps,
14680 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
14681 	.parallel_ops = true,
14682 };
14683 
14684 /* notification functions */
14685 
14686 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
14687 			  enum nl80211_commands cmd)
14688 {
14689 	struct sk_buff *msg;
14690 	struct nl80211_dump_wiphy_state state = {};
14691 
14692 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14693 		cmd != NL80211_CMD_DEL_WIPHY);
14694 
14695 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14696 	if (!msg)
14697 		return;
14698 
14699 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14700 		nlmsg_free(msg);
14701 		return;
14702 	}
14703 
14704 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14705 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14706 }
14707 
14708 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14709 				struct wireless_dev *wdev,
14710 				enum nl80211_commands cmd)
14711 {
14712 	struct sk_buff *msg;
14713 
14714 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14715 	if (!msg)
14716 		return;
14717 
14718 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
14719 		nlmsg_free(msg);
14720 		return;
14721 	}
14722 
14723 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14724 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14725 }
14726 
14727 static int nl80211_add_scan_req(struct sk_buff *msg,
14728 				struct cfg80211_registered_device *rdev)
14729 {
14730 	struct cfg80211_scan_request *req = rdev->scan_req;
14731 	struct nlattr *nest;
14732 	int i;
14733 
14734 	if (WARN_ON(!req))
14735 		return 0;
14736 
14737 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
14738 	if (!nest)
14739 		goto nla_put_failure;
14740 	for (i = 0; i < req->n_ssids; i++) {
14741 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14742 			goto nla_put_failure;
14743 	}
14744 	nla_nest_end(msg, nest);
14745 
14746 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14747 	if (!nest)
14748 		goto nla_put_failure;
14749 	for (i = 0; i < req->n_channels; i++) {
14750 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14751 			goto nla_put_failure;
14752 	}
14753 	nla_nest_end(msg, nest);
14754 
14755 	if (req->ie &&
14756 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14757 		goto nla_put_failure;
14758 
14759 	if (req->flags &&
14760 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14761 		goto nla_put_failure;
14762 
14763 	if (req->info.scan_start_tsf &&
14764 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14765 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14766 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14767 		     req->info.tsf_bssid)))
14768 		goto nla_put_failure;
14769 
14770 	return 0;
14771  nla_put_failure:
14772 	return -ENOBUFS;
14773 }
14774 
14775 static int nl80211_prep_scan_msg(struct sk_buff *msg,
14776 				 struct cfg80211_registered_device *rdev,
14777 				 struct wireless_dev *wdev,
14778 				 u32 portid, u32 seq, int flags,
14779 				 u32 cmd)
14780 {
14781 	void *hdr;
14782 
14783 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14784 	if (!hdr)
14785 		return -1;
14786 
14787 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14788 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14789 					 wdev->netdev->ifindex)) ||
14790 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14791 			      NL80211_ATTR_PAD))
14792 		goto nla_put_failure;
14793 
14794 	/* ignore errors and send incomplete event anyway */
14795 	nl80211_add_scan_req(msg, rdev);
14796 
14797 	genlmsg_end(msg, hdr);
14798 	return 0;
14799 
14800  nla_put_failure:
14801 	genlmsg_cancel(msg, hdr);
14802 	return -EMSGSIZE;
14803 }
14804 
14805 static int
14806 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14807 			    struct cfg80211_sched_scan_request *req, u32 cmd)
14808 {
14809 	void *hdr;
14810 
14811 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14812 	if (!hdr)
14813 		return -1;
14814 
14815 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14816 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14817 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14818 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14819 			      NL80211_ATTR_PAD))
14820 		goto nla_put_failure;
14821 
14822 	genlmsg_end(msg, hdr);
14823 	return 0;
14824 
14825  nla_put_failure:
14826 	genlmsg_cancel(msg, hdr);
14827 	return -EMSGSIZE;
14828 }
14829 
14830 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14831 			     struct wireless_dev *wdev)
14832 {
14833 	struct sk_buff *msg;
14834 
14835 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14836 	if (!msg)
14837 		return;
14838 
14839 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14840 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
14841 		nlmsg_free(msg);
14842 		return;
14843 	}
14844 
14845 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14846 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14847 }
14848 
14849 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14850 				       struct wireless_dev *wdev, bool aborted)
14851 {
14852 	struct sk_buff *msg;
14853 
14854 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14855 	if (!msg)
14856 		return NULL;
14857 
14858 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14859 				  aborted ? NL80211_CMD_SCAN_ABORTED :
14860 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14861 		nlmsg_free(msg);
14862 		return NULL;
14863 	}
14864 
14865 	return msg;
14866 }
14867 
14868 /* send message created by nl80211_build_scan_msg() */
14869 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14870 			   struct sk_buff *msg)
14871 {
14872 	if (!msg)
14873 		return;
14874 
14875 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14876 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14877 }
14878 
14879 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14880 {
14881 	struct sk_buff *msg;
14882 
14883 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14884 	if (!msg)
14885 		return;
14886 
14887 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14888 		nlmsg_free(msg);
14889 		return;
14890 	}
14891 
14892 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14893 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14894 }
14895 
14896 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14897 					  struct regulatory_request *request)
14898 {
14899 	/* Userspace can always count this one always being set */
14900 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14901 		goto nla_put_failure;
14902 
14903 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14904 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14905 			       NL80211_REGDOM_TYPE_WORLD))
14906 			goto nla_put_failure;
14907 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
14908 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14909 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
14910 			goto nla_put_failure;
14911 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
14912 		   request->intersect) {
14913 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14914 			       NL80211_REGDOM_TYPE_INTERSECTION))
14915 			goto nla_put_failure;
14916 	} else {
14917 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14918 			       NL80211_REGDOM_TYPE_COUNTRY) ||
14919 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
14920 				   request->alpha2))
14921 			goto nla_put_failure;
14922 	}
14923 
14924 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
14925 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
14926 
14927 		if (wiphy &&
14928 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
14929 			goto nla_put_failure;
14930 
14931 		if (wiphy &&
14932 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
14933 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
14934 			goto nla_put_failure;
14935 	}
14936 
14937 	return true;
14938 
14939 nla_put_failure:
14940 	return false;
14941 }
14942 
14943 /*
14944  * This can happen on global regulatory changes or device specific settings
14945  * based on custom regulatory domains.
14946  */
14947 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
14948 				     struct regulatory_request *request)
14949 {
14950 	struct sk_buff *msg;
14951 	void *hdr;
14952 
14953 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14954 	if (!msg)
14955 		return;
14956 
14957 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
14958 	if (!hdr) {
14959 		nlmsg_free(msg);
14960 		return;
14961 	}
14962 
14963 	if (nl80211_reg_change_event_fill(msg, request) == false)
14964 		goto nla_put_failure;
14965 
14966 	genlmsg_end(msg, hdr);
14967 
14968 	rcu_read_lock();
14969 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14970 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14971 	rcu_read_unlock();
14972 
14973 	return;
14974 
14975 nla_put_failure:
14976 	nlmsg_free(msg);
14977 }
14978 
14979 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
14980 				    struct net_device *netdev,
14981 				    const u8 *buf, size_t len,
14982 				    enum nl80211_commands cmd, gfp_t gfp,
14983 				    int uapsd_queues, const u8 *req_ies,
14984 				    size_t req_ies_len)
14985 {
14986 	struct sk_buff *msg;
14987 	void *hdr;
14988 
14989 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
14990 	if (!msg)
14991 		return;
14992 
14993 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14994 	if (!hdr) {
14995 		nlmsg_free(msg);
14996 		return;
14997 	}
14998 
14999 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15000 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15001 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15002 	    (req_ies &&
15003 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15004 		goto nla_put_failure;
15005 
15006 	if (uapsd_queues >= 0) {
15007 		struct nlattr *nla_wmm =
15008 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15009 		if (!nla_wmm)
15010 			goto nla_put_failure;
15011 
15012 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15013 			       uapsd_queues))
15014 			goto nla_put_failure;
15015 
15016 		nla_nest_end(msg, nla_wmm);
15017 	}
15018 
15019 	genlmsg_end(msg, hdr);
15020 
15021 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15022 				NL80211_MCGRP_MLME, gfp);
15023 	return;
15024 
15025  nla_put_failure:
15026 	nlmsg_free(msg);
15027 }
15028 
15029 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15030 			  struct net_device *netdev, const u8 *buf,
15031 			  size_t len, gfp_t gfp)
15032 {
15033 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15034 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15035 }
15036 
15037 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15038 			   struct net_device *netdev, const u8 *buf,
15039 			   size_t len, gfp_t gfp, int uapsd_queues,
15040 			   const u8 *req_ies, size_t req_ies_len)
15041 {
15042 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15043 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15044 				req_ies, req_ies_len);
15045 }
15046 
15047 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15048 			 struct net_device *netdev, const u8 *buf,
15049 			 size_t len, gfp_t gfp)
15050 {
15051 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15052 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15053 }
15054 
15055 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15056 			   struct net_device *netdev, const u8 *buf,
15057 			   size_t len, gfp_t gfp)
15058 {
15059 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15060 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15061 }
15062 
15063 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15064 				  size_t len)
15065 {
15066 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15067 	struct wiphy *wiphy = wdev->wiphy;
15068 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15069 	const struct ieee80211_mgmt *mgmt = (void *)buf;
15070 	u32 cmd;
15071 
15072 	if (WARN_ON(len < 2))
15073 		return;
15074 
15075 	if (ieee80211_is_deauth(mgmt->frame_control))
15076 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15077 	else
15078 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15079 
15080 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15081 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15082 				NULL, 0);
15083 }
15084 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15085 
15086 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15087 				      struct net_device *netdev, int cmd,
15088 				      const u8 *addr, gfp_t gfp)
15089 {
15090 	struct sk_buff *msg;
15091 	void *hdr;
15092 
15093 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15094 	if (!msg)
15095 		return;
15096 
15097 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15098 	if (!hdr) {
15099 		nlmsg_free(msg);
15100 		return;
15101 	}
15102 
15103 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15104 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15105 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15106 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15107 		goto nla_put_failure;
15108 
15109 	genlmsg_end(msg, hdr);
15110 
15111 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15112 				NL80211_MCGRP_MLME, gfp);
15113 	return;
15114 
15115  nla_put_failure:
15116 	nlmsg_free(msg);
15117 }
15118 
15119 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15120 			       struct net_device *netdev, const u8 *addr,
15121 			       gfp_t gfp)
15122 {
15123 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15124 				  addr, gfp);
15125 }
15126 
15127 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15128 				struct net_device *netdev, const u8 *addr,
15129 				gfp_t gfp)
15130 {
15131 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15132 				  addr, gfp);
15133 }
15134 
15135 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15136 				 struct net_device *netdev,
15137 				 struct cfg80211_connect_resp_params *cr,
15138 				 gfp_t gfp)
15139 {
15140 	struct sk_buff *msg;
15141 	void *hdr;
15142 
15143 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15144 			cr->fils.kek_len + cr->fils.pmk_len +
15145 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15146 	if (!msg)
15147 		return;
15148 
15149 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15150 	if (!hdr) {
15151 		nlmsg_free(msg);
15152 		return;
15153 	}
15154 
15155 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15156 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15157 	    (cr->bssid &&
15158 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15159 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15160 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15161 			cr->status) ||
15162 	    (cr->status < 0 &&
15163 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15164 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15165 			  cr->timeout_reason))) ||
15166 	    (cr->req_ie &&
15167 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15168 	    (cr->resp_ie &&
15169 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15170 		     cr->resp_ie)) ||
15171 	    (cr->fils.update_erp_next_seq_num &&
15172 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15173 			 cr->fils.erp_next_seq_num)) ||
15174 	    (cr->status == WLAN_STATUS_SUCCESS &&
15175 	     ((cr->fils.kek &&
15176 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15177 		       cr->fils.kek)) ||
15178 	      (cr->fils.pmk &&
15179 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15180 	      (cr->fils.pmkid &&
15181 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15182 		goto nla_put_failure;
15183 
15184 	genlmsg_end(msg, hdr);
15185 
15186 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15187 				NL80211_MCGRP_MLME, gfp);
15188 	return;
15189 
15190  nla_put_failure:
15191 	nlmsg_free(msg);
15192 }
15193 
15194 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15195 			 struct net_device *netdev,
15196 			 struct cfg80211_roam_info *info, gfp_t gfp)
15197 {
15198 	struct sk_buff *msg;
15199 	void *hdr;
15200 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15201 
15202 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15203 			info->fils.kek_len + info->fils.pmk_len +
15204 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15205 	if (!msg)
15206 		return;
15207 
15208 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15209 	if (!hdr) {
15210 		nlmsg_free(msg);
15211 		return;
15212 	}
15213 
15214 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15215 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15216 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15217 	    (info->req_ie &&
15218 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15219 		     info->req_ie)) ||
15220 	    (info->resp_ie &&
15221 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15222 		     info->resp_ie)) ||
15223 	    (info->fils.update_erp_next_seq_num &&
15224 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15225 			 info->fils.erp_next_seq_num)) ||
15226 	    (info->fils.kek &&
15227 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15228 		     info->fils.kek)) ||
15229 	    (info->fils.pmk &&
15230 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15231 	    (info->fils.pmkid &&
15232 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15233 		goto nla_put_failure;
15234 
15235 	genlmsg_end(msg, hdr);
15236 
15237 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15238 				NL80211_MCGRP_MLME, gfp);
15239 	return;
15240 
15241  nla_put_failure:
15242 	nlmsg_free(msg);
15243 }
15244 
15245 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15246 				  struct net_device *netdev, const u8 *bssid)
15247 {
15248 	struct sk_buff *msg;
15249 	void *hdr;
15250 
15251 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15252 	if (!msg)
15253 		return;
15254 
15255 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15256 	if (!hdr) {
15257 		nlmsg_free(msg);
15258 		return;
15259 	}
15260 
15261 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15262 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15263 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15264 		goto nla_put_failure;
15265 
15266 	genlmsg_end(msg, hdr);
15267 
15268 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15269 				NL80211_MCGRP_MLME, GFP_KERNEL);
15270 	return;
15271 
15272  nla_put_failure:
15273 	nlmsg_free(msg);
15274 }
15275 
15276 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15277 			       struct net_device *netdev, u16 reason,
15278 			       const u8 *ie, size_t ie_len, bool from_ap)
15279 {
15280 	struct sk_buff *msg;
15281 	void *hdr;
15282 
15283 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15284 	if (!msg)
15285 		return;
15286 
15287 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15288 	if (!hdr) {
15289 		nlmsg_free(msg);
15290 		return;
15291 	}
15292 
15293 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15294 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15295 	    (reason &&
15296 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15297 	    (from_ap &&
15298 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15299 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15300 		goto nla_put_failure;
15301 
15302 	genlmsg_end(msg, hdr);
15303 
15304 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15305 				NL80211_MCGRP_MLME, GFP_KERNEL);
15306 	return;
15307 
15308  nla_put_failure:
15309 	nlmsg_free(msg);
15310 }
15311 
15312 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15313 			     struct net_device *netdev, const u8 *bssid,
15314 			     gfp_t gfp)
15315 {
15316 	struct sk_buff *msg;
15317 	void *hdr;
15318 
15319 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15320 	if (!msg)
15321 		return;
15322 
15323 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15324 	if (!hdr) {
15325 		nlmsg_free(msg);
15326 		return;
15327 	}
15328 
15329 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15330 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15331 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15332 		goto nla_put_failure;
15333 
15334 	genlmsg_end(msg, hdr);
15335 
15336 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15337 				NL80211_MCGRP_MLME, gfp);
15338 	return;
15339 
15340  nla_put_failure:
15341 	nlmsg_free(msg);
15342 }
15343 
15344 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15345 					const u8 *ie, u8 ie_len,
15346 					int sig_dbm, gfp_t gfp)
15347 {
15348 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15349 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15350 	struct sk_buff *msg;
15351 	void *hdr;
15352 
15353 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15354 		return;
15355 
15356 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
15357 
15358 	msg = nlmsg_new(100 + ie_len, gfp);
15359 	if (!msg)
15360 		return;
15361 
15362 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15363 	if (!hdr) {
15364 		nlmsg_free(msg);
15365 		return;
15366 	}
15367 
15368 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15369 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15370 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15371 	    (ie_len && ie &&
15372 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15373 	    (sig_dbm &&
15374 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15375 		goto nla_put_failure;
15376 
15377 	genlmsg_end(msg, hdr);
15378 
15379 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15380 				NL80211_MCGRP_MLME, gfp);
15381 	return;
15382 
15383  nla_put_failure:
15384 	nlmsg_free(msg);
15385 }
15386 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15387 
15388 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15389 				 struct net_device *netdev, const u8 *addr,
15390 				 enum nl80211_key_type key_type, int key_id,
15391 				 const u8 *tsc, gfp_t gfp)
15392 {
15393 	struct sk_buff *msg;
15394 	void *hdr;
15395 
15396 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15397 	if (!msg)
15398 		return;
15399 
15400 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15401 	if (!hdr) {
15402 		nlmsg_free(msg);
15403 		return;
15404 	}
15405 
15406 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15407 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15408 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
15409 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
15410 	    (key_id != -1 &&
15411 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
15412 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
15413 		goto nla_put_failure;
15414 
15415 	genlmsg_end(msg, hdr);
15416 
15417 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15418 				NL80211_MCGRP_MLME, gfp);
15419 	return;
15420 
15421  nla_put_failure:
15422 	nlmsg_free(msg);
15423 }
15424 
15425 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
15426 				    struct ieee80211_channel *channel_before,
15427 				    struct ieee80211_channel *channel_after)
15428 {
15429 	struct sk_buff *msg;
15430 	void *hdr;
15431 	struct nlattr *nl_freq;
15432 
15433 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
15434 	if (!msg)
15435 		return;
15436 
15437 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
15438 	if (!hdr) {
15439 		nlmsg_free(msg);
15440 		return;
15441 	}
15442 
15443 	/*
15444 	 * Since we are applying the beacon hint to a wiphy we know its
15445 	 * wiphy_idx is valid
15446 	 */
15447 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
15448 		goto nla_put_failure;
15449 
15450 	/* Before */
15451 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
15452 	if (!nl_freq)
15453 		goto nla_put_failure;
15454 
15455 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
15456 		goto nla_put_failure;
15457 	nla_nest_end(msg, nl_freq);
15458 
15459 	/* After */
15460 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
15461 	if (!nl_freq)
15462 		goto nla_put_failure;
15463 
15464 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
15465 		goto nla_put_failure;
15466 	nla_nest_end(msg, nl_freq);
15467 
15468 	genlmsg_end(msg, hdr);
15469 
15470 	rcu_read_lock();
15471 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15472 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15473 	rcu_read_unlock();
15474 
15475 	return;
15476 
15477 nla_put_failure:
15478 	nlmsg_free(msg);
15479 }
15480 
15481 static void nl80211_send_remain_on_chan_event(
15482 	int cmd, struct cfg80211_registered_device *rdev,
15483 	struct wireless_dev *wdev, u64 cookie,
15484 	struct ieee80211_channel *chan,
15485 	unsigned int duration, gfp_t gfp)
15486 {
15487 	struct sk_buff *msg;
15488 	void *hdr;
15489 
15490 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15491 	if (!msg)
15492 		return;
15493 
15494 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15495 	if (!hdr) {
15496 		nlmsg_free(msg);
15497 		return;
15498 	}
15499 
15500 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15501 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15502 					 wdev->netdev->ifindex)) ||
15503 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15504 			      NL80211_ATTR_PAD) ||
15505 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
15506 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
15507 			NL80211_CHAN_NO_HT) ||
15508 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15509 			      NL80211_ATTR_PAD))
15510 		goto nla_put_failure;
15511 
15512 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
15513 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
15514 		goto nla_put_failure;
15515 
15516 	genlmsg_end(msg, hdr);
15517 
15518 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15519 				NL80211_MCGRP_MLME, gfp);
15520 	return;
15521 
15522  nla_put_failure:
15523 	nlmsg_free(msg);
15524 }
15525 
15526 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
15527 			       struct ieee80211_channel *chan,
15528 			       unsigned int duration, gfp_t gfp)
15529 {
15530 	struct wiphy *wiphy = wdev->wiphy;
15531 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15532 
15533 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
15534 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
15535 					  rdev, wdev, cookie, chan,
15536 					  duration, gfp);
15537 }
15538 EXPORT_SYMBOL(cfg80211_ready_on_channel);
15539 
15540 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
15541 					struct ieee80211_channel *chan,
15542 					gfp_t gfp)
15543 {
15544 	struct wiphy *wiphy = wdev->wiphy;
15545 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15546 
15547 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
15548 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15549 					  rdev, wdev, cookie, chan, 0, gfp);
15550 }
15551 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
15552 
15553 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
15554 					struct ieee80211_channel *chan,
15555 					gfp_t gfp)
15556 {
15557 	struct wiphy *wiphy = wdev->wiphy;
15558 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15559 
15560 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
15561 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
15562 					  rdev, wdev, cookie, chan, 0, gfp);
15563 }
15564 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
15565 
15566 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
15567 		      struct station_info *sinfo, gfp_t gfp)
15568 {
15569 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15570 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15571 	struct sk_buff *msg;
15572 
15573 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
15574 
15575 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15576 	if (!msg)
15577 		return;
15578 
15579 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
15580 				 rdev, dev, mac_addr, sinfo) < 0) {
15581 		nlmsg_free(msg);
15582 		return;
15583 	}
15584 
15585 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15586 				NL80211_MCGRP_MLME, gfp);
15587 }
15588 EXPORT_SYMBOL(cfg80211_new_sta);
15589 
15590 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
15591 			    struct station_info *sinfo, gfp_t gfp)
15592 {
15593 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15594 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15595 	struct sk_buff *msg;
15596 	struct station_info empty_sinfo = {};
15597 
15598 	if (!sinfo)
15599 		sinfo = &empty_sinfo;
15600 
15601 	trace_cfg80211_del_sta(dev, mac_addr);
15602 
15603 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15604 	if (!msg) {
15605 		cfg80211_sinfo_release_content(sinfo);
15606 		return;
15607 	}
15608 
15609 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
15610 				 rdev, dev, mac_addr, sinfo) < 0) {
15611 		nlmsg_free(msg);
15612 		return;
15613 	}
15614 
15615 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15616 				NL80211_MCGRP_MLME, gfp);
15617 }
15618 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
15619 
15620 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
15621 			  enum nl80211_connect_failed_reason reason,
15622 			  gfp_t gfp)
15623 {
15624 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15625 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15626 	struct sk_buff *msg;
15627 	void *hdr;
15628 
15629 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
15630 	if (!msg)
15631 		return;
15632 
15633 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
15634 	if (!hdr) {
15635 		nlmsg_free(msg);
15636 		return;
15637 	}
15638 
15639 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15640 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
15641 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
15642 		goto nla_put_failure;
15643 
15644 	genlmsg_end(msg, hdr);
15645 
15646 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15647 				NL80211_MCGRP_MLME, gfp);
15648 	return;
15649 
15650  nla_put_failure:
15651 	nlmsg_free(msg);
15652 }
15653 EXPORT_SYMBOL(cfg80211_conn_failed);
15654 
15655 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
15656 				       const u8 *addr, gfp_t gfp)
15657 {
15658 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15659 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15660 	struct sk_buff *msg;
15661 	void *hdr;
15662 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
15663 
15664 	if (!nlportid)
15665 		return false;
15666 
15667 	msg = nlmsg_new(100, gfp);
15668 	if (!msg)
15669 		return true;
15670 
15671 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15672 	if (!hdr) {
15673 		nlmsg_free(msg);
15674 		return true;
15675 	}
15676 
15677 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15678 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15679 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15680 		goto nla_put_failure;
15681 
15682 	genlmsg_end(msg, hdr);
15683 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15684 	return true;
15685 
15686  nla_put_failure:
15687 	nlmsg_free(msg);
15688 	return true;
15689 }
15690 
15691 bool cfg80211_rx_spurious_frame(struct net_device *dev,
15692 				const u8 *addr, gfp_t gfp)
15693 {
15694 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15695 	bool ret;
15696 
15697 	trace_cfg80211_rx_spurious_frame(dev, addr);
15698 
15699 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15700 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
15701 		trace_cfg80211_return_bool(false);
15702 		return false;
15703 	}
15704 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
15705 					 addr, gfp);
15706 	trace_cfg80211_return_bool(ret);
15707 	return ret;
15708 }
15709 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15710 
15711 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15712 					const u8 *addr, gfp_t gfp)
15713 {
15714 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15715 	bool ret;
15716 
15717 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15718 
15719 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15720 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15721 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15722 		trace_cfg80211_return_bool(false);
15723 		return false;
15724 	}
15725 	ret = __nl80211_unexpected_frame(dev,
15726 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15727 					 addr, gfp);
15728 	trace_cfg80211_return_bool(ret);
15729 	return ret;
15730 }
15731 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15732 
15733 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15734 		      struct wireless_dev *wdev, u32 nlportid,
15735 		      int freq, int sig_dbm,
15736 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15737 {
15738 	struct net_device *netdev = wdev->netdev;
15739 	struct sk_buff *msg;
15740 	void *hdr;
15741 
15742 	msg = nlmsg_new(100 + len, gfp);
15743 	if (!msg)
15744 		return -ENOMEM;
15745 
15746 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15747 	if (!hdr) {
15748 		nlmsg_free(msg);
15749 		return -ENOMEM;
15750 	}
15751 
15752 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15753 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15754 					netdev->ifindex)) ||
15755 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15756 			      NL80211_ATTR_PAD) ||
15757 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15758 	    (sig_dbm &&
15759 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15760 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15761 	    (flags &&
15762 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15763 		goto nla_put_failure;
15764 
15765 	genlmsg_end(msg, hdr);
15766 
15767 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15768 
15769  nla_put_failure:
15770 	nlmsg_free(msg);
15771 	return -ENOBUFS;
15772 }
15773 
15774 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15775 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
15776 {
15777 	struct wiphy *wiphy = wdev->wiphy;
15778 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15779 	struct net_device *netdev = wdev->netdev;
15780 	struct sk_buff *msg;
15781 	void *hdr;
15782 
15783 	trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15784 
15785 	msg = nlmsg_new(100 + len, gfp);
15786 	if (!msg)
15787 		return;
15788 
15789 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15790 	if (!hdr) {
15791 		nlmsg_free(msg);
15792 		return;
15793 	}
15794 
15795 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15796 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15797 				   netdev->ifindex)) ||
15798 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15799 			      NL80211_ATTR_PAD) ||
15800 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15801 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15802 			      NL80211_ATTR_PAD) ||
15803 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
15804 		goto nla_put_failure;
15805 
15806 	genlmsg_end(msg, hdr);
15807 
15808 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15809 				NL80211_MCGRP_MLME, gfp);
15810 	return;
15811 
15812  nla_put_failure:
15813 	nlmsg_free(msg);
15814 }
15815 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15816 
15817 static int __nl80211_rx_control_port(struct net_device *dev,
15818 				     struct sk_buff *skb,
15819 				     bool unencrypted, gfp_t gfp)
15820 {
15821 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15822 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15823 	struct ethhdr *ehdr = eth_hdr(skb);
15824 	const u8 *addr = ehdr->h_source;
15825 	u16 proto = be16_to_cpu(skb->protocol);
15826 	struct sk_buff *msg;
15827 	void *hdr;
15828 	struct nlattr *frame;
15829 
15830 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15831 
15832 	if (!nlportid)
15833 		return -ENOENT;
15834 
15835 	msg = nlmsg_new(100 + skb->len, gfp);
15836 	if (!msg)
15837 		return -ENOMEM;
15838 
15839 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15840 	if (!hdr) {
15841 		nlmsg_free(msg);
15842 		return -ENOBUFS;
15843 	}
15844 
15845 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15846 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15847 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15848 			      NL80211_ATTR_PAD) ||
15849 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15850 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15851 	    (unencrypted && nla_put_flag(msg,
15852 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15853 		goto nla_put_failure;
15854 
15855 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15856 	if (!frame)
15857 		goto nla_put_failure;
15858 
15859 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15860 	genlmsg_end(msg, hdr);
15861 
15862 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15863 
15864  nla_put_failure:
15865 	nlmsg_free(msg);
15866 	return -ENOBUFS;
15867 }
15868 
15869 bool cfg80211_rx_control_port(struct net_device *dev,
15870 			      struct sk_buff *skb, bool unencrypted)
15871 {
15872 	int ret;
15873 
15874 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15875 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15876 	trace_cfg80211_return_bool(ret == 0);
15877 	return ret == 0;
15878 }
15879 EXPORT_SYMBOL(cfg80211_rx_control_port);
15880 
15881 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15882 					    const char *mac, gfp_t gfp)
15883 {
15884 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15885 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15886 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15887 	void **cb;
15888 
15889 	if (!msg)
15890 		return NULL;
15891 
15892 	cb = (void **)msg->cb;
15893 
15894 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15895 	if (!cb[0]) {
15896 		nlmsg_free(msg);
15897 		return NULL;
15898 	}
15899 
15900 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15901 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15902 		goto nla_put_failure;
15903 
15904 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15905 		goto nla_put_failure;
15906 
15907 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
15908 	if (!cb[1])
15909 		goto nla_put_failure;
15910 
15911 	cb[2] = rdev;
15912 
15913 	return msg;
15914  nla_put_failure:
15915 	nlmsg_free(msg);
15916 	return NULL;
15917 }
15918 
15919 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
15920 {
15921 	void **cb = (void **)msg->cb;
15922 	struct cfg80211_registered_device *rdev = cb[2];
15923 
15924 	nla_nest_end(msg, cb[1]);
15925 	genlmsg_end(msg, cb[0]);
15926 
15927 	memset(msg->cb, 0, sizeof(msg->cb));
15928 
15929 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15930 				NL80211_MCGRP_MLME, gfp);
15931 }
15932 
15933 void cfg80211_cqm_rssi_notify(struct net_device *dev,
15934 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
15935 			      s32 rssi_level, gfp_t gfp)
15936 {
15937 	struct sk_buff *msg;
15938 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15939 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15940 
15941 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
15942 
15943 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
15944 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
15945 		return;
15946 
15947 	if (wdev->cqm_config) {
15948 		wdev->cqm_config->last_rssi_event_value = rssi_level;
15949 
15950 		cfg80211_cqm_rssi_update(rdev, dev);
15951 
15952 		if (rssi_level == 0)
15953 			rssi_level = wdev->cqm_config->last_rssi_event_value;
15954 	}
15955 
15956 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
15957 	if (!msg)
15958 		return;
15959 
15960 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
15961 			rssi_event))
15962 		goto nla_put_failure;
15963 
15964 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
15965 				      rssi_level))
15966 		goto nla_put_failure;
15967 
15968 	cfg80211_send_cqm(msg, gfp);
15969 
15970 	return;
15971 
15972  nla_put_failure:
15973 	nlmsg_free(msg);
15974 }
15975 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
15976 
15977 void cfg80211_cqm_txe_notify(struct net_device *dev,
15978 			     const u8 *peer, u32 num_packets,
15979 			     u32 rate, u32 intvl, gfp_t gfp)
15980 {
15981 	struct sk_buff *msg;
15982 
15983 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
15984 	if (!msg)
15985 		return;
15986 
15987 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
15988 		goto nla_put_failure;
15989 
15990 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
15991 		goto nla_put_failure;
15992 
15993 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
15994 		goto nla_put_failure;
15995 
15996 	cfg80211_send_cqm(msg, gfp);
15997 	return;
15998 
15999  nla_put_failure:
16000 	nlmsg_free(msg);
16001 }
16002 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16003 
16004 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16005 				 const u8 *peer, u32 num_packets, gfp_t gfp)
16006 {
16007 	struct sk_buff *msg;
16008 
16009 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16010 
16011 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16012 	if (!msg)
16013 		return;
16014 
16015 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16016 		goto nla_put_failure;
16017 
16018 	cfg80211_send_cqm(msg, gfp);
16019 	return;
16020 
16021  nla_put_failure:
16022 	nlmsg_free(msg);
16023 }
16024 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16025 
16026 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16027 {
16028 	struct sk_buff *msg;
16029 
16030 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16031 	if (!msg)
16032 		return;
16033 
16034 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16035 		goto nla_put_failure;
16036 
16037 	cfg80211_send_cqm(msg, gfp);
16038 	return;
16039 
16040  nla_put_failure:
16041 	nlmsg_free(msg);
16042 }
16043 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16044 
16045 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16046 				     struct net_device *netdev, const u8 *bssid,
16047 				     const u8 *replay_ctr, gfp_t gfp)
16048 {
16049 	struct sk_buff *msg;
16050 	struct nlattr *rekey_attr;
16051 	void *hdr;
16052 
16053 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16054 	if (!msg)
16055 		return;
16056 
16057 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16058 	if (!hdr) {
16059 		nlmsg_free(msg);
16060 		return;
16061 	}
16062 
16063 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16064 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16065 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16066 		goto nla_put_failure;
16067 
16068 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16069 	if (!rekey_attr)
16070 		goto nla_put_failure;
16071 
16072 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16073 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
16074 		goto nla_put_failure;
16075 
16076 	nla_nest_end(msg, rekey_attr);
16077 
16078 	genlmsg_end(msg, hdr);
16079 
16080 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16081 				NL80211_MCGRP_MLME, gfp);
16082 	return;
16083 
16084  nla_put_failure:
16085 	nlmsg_free(msg);
16086 }
16087 
16088 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16089 			       const u8 *replay_ctr, gfp_t gfp)
16090 {
16091 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16092 	struct wiphy *wiphy = wdev->wiphy;
16093 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16094 
16095 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
16096 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16097 }
16098 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16099 
16100 static void
16101 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16102 			       struct net_device *netdev, int index,
16103 			       const u8 *bssid, bool preauth, gfp_t gfp)
16104 {
16105 	struct sk_buff *msg;
16106 	struct nlattr *attr;
16107 	void *hdr;
16108 
16109 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16110 	if (!msg)
16111 		return;
16112 
16113 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16114 	if (!hdr) {
16115 		nlmsg_free(msg);
16116 		return;
16117 	}
16118 
16119 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16120 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16121 		goto nla_put_failure;
16122 
16123 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16124 	if (!attr)
16125 		goto nla_put_failure;
16126 
16127 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16128 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16129 	    (preauth &&
16130 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16131 		goto nla_put_failure;
16132 
16133 	nla_nest_end(msg, attr);
16134 
16135 	genlmsg_end(msg, hdr);
16136 
16137 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16138 				NL80211_MCGRP_MLME, gfp);
16139 	return;
16140 
16141  nla_put_failure:
16142 	nlmsg_free(msg);
16143 }
16144 
16145 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16146 				     const u8 *bssid, bool preauth, gfp_t gfp)
16147 {
16148 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16149 	struct wiphy *wiphy = wdev->wiphy;
16150 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16151 
16152 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16153 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16154 }
16155 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16156 
16157 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16158 				     struct net_device *netdev,
16159 				     struct cfg80211_chan_def *chandef,
16160 				     gfp_t gfp,
16161 				     enum nl80211_commands notif,
16162 				     u8 count)
16163 {
16164 	struct sk_buff *msg;
16165 	void *hdr;
16166 
16167 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16168 	if (!msg)
16169 		return;
16170 
16171 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16172 	if (!hdr) {
16173 		nlmsg_free(msg);
16174 		return;
16175 	}
16176 
16177 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16178 		goto nla_put_failure;
16179 
16180 	if (nl80211_send_chandef(msg, chandef))
16181 		goto nla_put_failure;
16182 
16183 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16184 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16185 			goto nla_put_failure;
16186 
16187 	genlmsg_end(msg, hdr);
16188 
16189 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16190 				NL80211_MCGRP_MLME, gfp);
16191 	return;
16192 
16193  nla_put_failure:
16194 	nlmsg_free(msg);
16195 }
16196 
16197 void cfg80211_ch_switch_notify(struct net_device *dev,
16198 			       struct cfg80211_chan_def *chandef)
16199 {
16200 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16201 	struct wiphy *wiphy = wdev->wiphy;
16202 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16203 
16204 	ASSERT_WDEV_LOCK(wdev);
16205 
16206 	trace_cfg80211_ch_switch_notify(dev, chandef);
16207 
16208 	wdev->chandef = *chandef;
16209 	wdev->preset_chandef = *chandef;
16210 
16211 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
16212 	    !WARN_ON(!wdev->current_bss))
16213 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16214 
16215 	cfg80211_sched_dfs_chan_update(rdev);
16216 
16217 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16218 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16219 }
16220 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16221 
16222 void cfg80211_ch_switch_started_notify(struct net_device *dev,
16223 				       struct cfg80211_chan_def *chandef,
16224 				       u8 count)
16225 {
16226 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16227 	struct wiphy *wiphy = wdev->wiphy;
16228 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16229 
16230 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
16231 
16232 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16233 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16234 }
16235 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16236 
16237 void
16238 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16239 		     const struct cfg80211_chan_def *chandef,
16240 		     enum nl80211_radar_event event,
16241 		     struct net_device *netdev, gfp_t gfp)
16242 {
16243 	struct sk_buff *msg;
16244 	void *hdr;
16245 
16246 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16247 	if (!msg)
16248 		return;
16249 
16250 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16251 	if (!hdr) {
16252 		nlmsg_free(msg);
16253 		return;
16254 	}
16255 
16256 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16257 		goto nla_put_failure;
16258 
16259 	/* NOP and radar events don't need a netdev parameter */
16260 	if (netdev) {
16261 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
16262 
16263 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16264 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16265 				      NL80211_ATTR_PAD))
16266 			goto nla_put_failure;
16267 	}
16268 
16269 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16270 		goto nla_put_failure;
16271 
16272 	if (nl80211_send_chandef(msg, chandef))
16273 		goto nla_put_failure;
16274 
16275 	genlmsg_end(msg, hdr);
16276 
16277 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16278 				NL80211_MCGRP_MLME, gfp);
16279 	return;
16280 
16281  nla_put_failure:
16282 	nlmsg_free(msg);
16283 }
16284 
16285 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16286 				       struct sta_opmode_info *sta_opmode,
16287 				       gfp_t gfp)
16288 {
16289 	struct sk_buff *msg;
16290 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16291 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16292 	void *hdr;
16293 
16294 	if (WARN_ON(!mac))
16295 		return;
16296 
16297 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16298 	if (!msg)
16299 		return;
16300 
16301 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16302 	if (!hdr) {
16303 		nlmsg_free(msg);
16304 		return;
16305 	}
16306 
16307 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16308 		goto nla_put_failure;
16309 
16310 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16311 		goto nla_put_failure;
16312 
16313 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16314 		goto nla_put_failure;
16315 
16316 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16317 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16318 		goto nla_put_failure;
16319 
16320 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16321 	    nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16322 		goto nla_put_failure;
16323 
16324 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16325 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16326 		goto nla_put_failure;
16327 
16328 	genlmsg_end(msg, hdr);
16329 
16330 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16331 				NL80211_MCGRP_MLME, gfp);
16332 
16333 	return;
16334 
16335 nla_put_failure:
16336 	nlmsg_free(msg);
16337 }
16338 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16339 
16340 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16341 			   u64 cookie, bool acked, s32 ack_signal,
16342 			   bool is_valid_ack_signal, gfp_t gfp)
16343 {
16344 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16345 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16346 	struct sk_buff *msg;
16347 	void *hdr;
16348 
16349 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
16350 
16351 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16352 
16353 	if (!msg)
16354 		return;
16355 
16356 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16357 	if (!hdr) {
16358 		nlmsg_free(msg);
16359 		return;
16360 	}
16361 
16362 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16363 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16364 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16365 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16366 			      NL80211_ATTR_PAD) ||
16367 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16368 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16369 						ack_signal)))
16370 		goto nla_put_failure;
16371 
16372 	genlmsg_end(msg, hdr);
16373 
16374 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16375 				NL80211_MCGRP_MLME, gfp);
16376 	return;
16377 
16378  nla_put_failure:
16379 	nlmsg_free(msg);
16380 }
16381 EXPORT_SYMBOL(cfg80211_probe_status);
16382 
16383 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
16384 				 const u8 *frame, size_t len,
16385 				 int freq, int sig_dbm)
16386 {
16387 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16388 	struct sk_buff *msg;
16389 	void *hdr;
16390 	struct cfg80211_beacon_registration *reg;
16391 
16392 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
16393 
16394 	spin_lock_bh(&rdev->beacon_registrations_lock);
16395 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
16396 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
16397 		if (!msg) {
16398 			spin_unlock_bh(&rdev->beacon_registrations_lock);
16399 			return;
16400 		}
16401 
16402 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16403 		if (!hdr)
16404 			goto nla_put_failure;
16405 
16406 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16407 		    (freq &&
16408 		     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
16409 		    (sig_dbm &&
16410 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16411 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
16412 			goto nla_put_failure;
16413 
16414 		genlmsg_end(msg, hdr);
16415 
16416 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
16417 	}
16418 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16419 	return;
16420 
16421  nla_put_failure:
16422 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16423 	nlmsg_free(msg);
16424 }
16425 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
16426 
16427 #ifdef CONFIG_PM
16428 static int cfg80211_net_detect_results(struct sk_buff *msg,
16429 				       struct cfg80211_wowlan_wakeup *wakeup)
16430 {
16431 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
16432 	struct nlattr *nl_results, *nl_match, *nl_freqs;
16433 	int i, j;
16434 
16435 	nl_results = nla_nest_start_noflag(msg,
16436 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
16437 	if (!nl_results)
16438 		return -EMSGSIZE;
16439 
16440 	for (i = 0; i < nd->n_matches; i++) {
16441 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
16442 
16443 		nl_match = nla_nest_start_noflag(msg, i);
16444 		if (!nl_match)
16445 			break;
16446 
16447 		/* The SSID attribute is optional in nl80211, but for
16448 		 * simplicity reasons it's always present in the
16449 		 * cfg80211 structure.  If a driver can't pass the
16450 		 * SSID, that needs to be changed.  A zero length SSID
16451 		 * is still a valid SSID (wildcard), so it cannot be
16452 		 * used for this purpose.
16453 		 */
16454 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
16455 			    match->ssid.ssid)) {
16456 			nla_nest_cancel(msg, nl_match);
16457 			goto out;
16458 		}
16459 
16460 		if (match->n_channels) {
16461 			nl_freqs = nla_nest_start_noflag(msg,
16462 							 NL80211_ATTR_SCAN_FREQUENCIES);
16463 			if (!nl_freqs) {
16464 				nla_nest_cancel(msg, nl_match);
16465 				goto out;
16466 			}
16467 
16468 			for (j = 0; j < match->n_channels; j++) {
16469 				if (nla_put_u32(msg, j, match->channels[j])) {
16470 					nla_nest_cancel(msg, nl_freqs);
16471 					nla_nest_cancel(msg, nl_match);
16472 					goto out;
16473 				}
16474 			}
16475 
16476 			nla_nest_end(msg, nl_freqs);
16477 		}
16478 
16479 		nla_nest_end(msg, nl_match);
16480 	}
16481 
16482 out:
16483 	nla_nest_end(msg, nl_results);
16484 	return 0;
16485 }
16486 
16487 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
16488 				   struct cfg80211_wowlan_wakeup *wakeup,
16489 				   gfp_t gfp)
16490 {
16491 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16492 	struct sk_buff *msg;
16493 	void *hdr;
16494 	int size = 200;
16495 
16496 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
16497 
16498 	if (wakeup)
16499 		size += wakeup->packet_present_len;
16500 
16501 	msg = nlmsg_new(size, gfp);
16502 	if (!msg)
16503 		return;
16504 
16505 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
16506 	if (!hdr)
16507 		goto free_msg;
16508 
16509 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16510 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16511 			      NL80211_ATTR_PAD))
16512 		goto free_msg;
16513 
16514 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16515 					wdev->netdev->ifindex))
16516 		goto free_msg;
16517 
16518 	if (wakeup) {
16519 		struct nlattr *reasons;
16520 
16521 		reasons = nla_nest_start_noflag(msg,
16522 						NL80211_ATTR_WOWLAN_TRIGGERS);
16523 		if (!reasons)
16524 			goto free_msg;
16525 
16526 		if (wakeup->disconnect &&
16527 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
16528 			goto free_msg;
16529 		if (wakeup->magic_pkt &&
16530 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
16531 			goto free_msg;
16532 		if (wakeup->gtk_rekey_failure &&
16533 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
16534 			goto free_msg;
16535 		if (wakeup->eap_identity_req &&
16536 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
16537 			goto free_msg;
16538 		if (wakeup->four_way_handshake &&
16539 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
16540 			goto free_msg;
16541 		if (wakeup->rfkill_release &&
16542 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
16543 			goto free_msg;
16544 
16545 		if (wakeup->pattern_idx >= 0 &&
16546 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
16547 				wakeup->pattern_idx))
16548 			goto free_msg;
16549 
16550 		if (wakeup->tcp_match &&
16551 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
16552 			goto free_msg;
16553 
16554 		if (wakeup->tcp_connlost &&
16555 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
16556 			goto free_msg;
16557 
16558 		if (wakeup->tcp_nomoretokens &&
16559 		    nla_put_flag(msg,
16560 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
16561 			goto free_msg;
16562 
16563 		if (wakeup->packet) {
16564 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
16565 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
16566 
16567 			if (!wakeup->packet_80211) {
16568 				pkt_attr =
16569 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
16570 				len_attr =
16571 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
16572 			}
16573 
16574 			if (wakeup->packet_len &&
16575 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
16576 				goto free_msg;
16577 
16578 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
16579 				    wakeup->packet))
16580 				goto free_msg;
16581 		}
16582 
16583 		if (wakeup->net_detect &&
16584 		    cfg80211_net_detect_results(msg, wakeup))
16585 				goto free_msg;
16586 
16587 		nla_nest_end(msg, reasons);
16588 	}
16589 
16590 	genlmsg_end(msg, hdr);
16591 
16592 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16593 				NL80211_MCGRP_MLME, gfp);
16594 	return;
16595 
16596  free_msg:
16597 	nlmsg_free(msg);
16598 }
16599 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
16600 #endif
16601 
16602 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
16603 				enum nl80211_tdls_operation oper,
16604 				u16 reason_code, gfp_t gfp)
16605 {
16606 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16607 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16608 	struct sk_buff *msg;
16609 	void *hdr;
16610 
16611 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
16612 					 reason_code);
16613 
16614 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16615 	if (!msg)
16616 		return;
16617 
16618 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
16619 	if (!hdr) {
16620 		nlmsg_free(msg);
16621 		return;
16622 	}
16623 
16624 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16625 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16626 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
16627 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
16628 	    (reason_code > 0 &&
16629 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
16630 		goto nla_put_failure;
16631 
16632 	genlmsg_end(msg, hdr);
16633 
16634 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16635 				NL80211_MCGRP_MLME, gfp);
16636 	return;
16637 
16638  nla_put_failure:
16639 	nlmsg_free(msg);
16640 }
16641 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
16642 
16643 static int nl80211_netlink_notify(struct notifier_block * nb,
16644 				  unsigned long state,
16645 				  void *_notify)
16646 {
16647 	struct netlink_notify *notify = _notify;
16648 	struct cfg80211_registered_device *rdev;
16649 	struct wireless_dev *wdev;
16650 	struct cfg80211_beacon_registration *reg, *tmp;
16651 
16652 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
16653 		return NOTIFY_DONE;
16654 
16655 	rcu_read_lock();
16656 
16657 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
16658 		struct cfg80211_sched_scan_request *sched_scan_req;
16659 
16660 		list_for_each_entry_rcu(sched_scan_req,
16661 					&rdev->sched_scan_req_list,
16662 					list) {
16663 			if (sched_scan_req->owner_nlportid == notify->portid) {
16664 				sched_scan_req->nl_owner_dead = true;
16665 				schedule_work(&rdev->sched_scan_stop_wk);
16666 			}
16667 		}
16668 
16669 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
16670 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
16671 
16672 			if (wdev->owner_nlportid == notify->portid) {
16673 				wdev->nl_owner_dead = true;
16674 				schedule_work(&rdev->destroy_work);
16675 			} else if (wdev->conn_owner_nlportid == notify->portid) {
16676 				schedule_work(&wdev->disconnect_wk);
16677 			}
16678 
16679 			cfg80211_release_pmsr(wdev, notify->portid);
16680 		}
16681 
16682 		spin_lock_bh(&rdev->beacon_registrations_lock);
16683 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
16684 					 list) {
16685 			if (reg->nlportid == notify->portid) {
16686 				list_del(&reg->list);
16687 				kfree(reg);
16688 				break;
16689 			}
16690 		}
16691 		spin_unlock_bh(&rdev->beacon_registrations_lock);
16692 	}
16693 
16694 	rcu_read_unlock();
16695 
16696 	/*
16697 	 * It is possible that the user space process that is controlling the
16698 	 * indoor setting disappeared, so notify the regulatory core.
16699 	 */
16700 	regulatory_netlink_notify(notify->portid);
16701 	return NOTIFY_OK;
16702 }
16703 
16704 static struct notifier_block nl80211_netlink_notifier = {
16705 	.notifier_call = nl80211_netlink_notify,
16706 };
16707 
16708 void cfg80211_ft_event(struct net_device *netdev,
16709 		       struct cfg80211_ft_event_params *ft_event)
16710 {
16711 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16712 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16713 	struct sk_buff *msg;
16714 	void *hdr;
16715 
16716 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16717 
16718 	if (!ft_event->target_ap)
16719 		return;
16720 
16721 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16722 			GFP_KERNEL);
16723 	if (!msg)
16724 		return;
16725 
16726 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16727 	if (!hdr)
16728 		goto out;
16729 
16730 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16731 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16732 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16733 		goto out;
16734 
16735 	if (ft_event->ies &&
16736 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16737 		goto out;
16738 	if (ft_event->ric_ies &&
16739 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16740 		    ft_event->ric_ies))
16741 		goto out;
16742 
16743 	genlmsg_end(msg, hdr);
16744 
16745 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16746 				NL80211_MCGRP_MLME, GFP_KERNEL);
16747 	return;
16748  out:
16749 	nlmsg_free(msg);
16750 }
16751 EXPORT_SYMBOL(cfg80211_ft_event);
16752 
16753 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16754 {
16755 	struct cfg80211_registered_device *rdev;
16756 	struct sk_buff *msg;
16757 	void *hdr;
16758 	u32 nlportid;
16759 
16760 	rdev = wiphy_to_rdev(wdev->wiphy);
16761 	if (!rdev->crit_proto_nlportid)
16762 		return;
16763 
16764 	nlportid = rdev->crit_proto_nlportid;
16765 	rdev->crit_proto_nlportid = 0;
16766 
16767 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16768 	if (!msg)
16769 		return;
16770 
16771 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16772 	if (!hdr)
16773 		goto nla_put_failure;
16774 
16775 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16776 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16777 			      NL80211_ATTR_PAD))
16778 		goto nla_put_failure;
16779 
16780 	genlmsg_end(msg, hdr);
16781 
16782 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16783 	return;
16784 
16785  nla_put_failure:
16786 	nlmsg_free(msg);
16787 }
16788 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16789 
16790 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16791 {
16792 	struct wiphy *wiphy = wdev->wiphy;
16793 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16794 	struct sk_buff *msg;
16795 	void *hdr;
16796 
16797 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16798 	if (!msg)
16799 		return;
16800 
16801 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16802 	if (!hdr)
16803 		goto out;
16804 
16805 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16806 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16807 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16808 			      NL80211_ATTR_PAD))
16809 		goto out;
16810 
16811 	genlmsg_end(msg, hdr);
16812 
16813 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16814 				NL80211_MCGRP_MLME, GFP_KERNEL);
16815 	return;
16816  out:
16817 	nlmsg_free(msg);
16818 }
16819 
16820 int cfg80211_external_auth_request(struct net_device *dev,
16821 				   struct cfg80211_external_auth_params *params,
16822 				   gfp_t gfp)
16823 {
16824 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16825 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16826 	struct sk_buff *msg;
16827 	void *hdr;
16828 
16829 	if (!wdev->conn_owner_nlportid)
16830 		return -EINVAL;
16831 
16832 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16833 	if (!msg)
16834 		return -ENOMEM;
16835 
16836 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16837 	if (!hdr)
16838 		goto nla_put_failure;
16839 
16840 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16841 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16842 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16843 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16844 			params->action) ||
16845 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16846 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16847 		    params->ssid.ssid))
16848 		goto nla_put_failure;
16849 
16850 	genlmsg_end(msg, hdr);
16851 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16852 			wdev->conn_owner_nlportid);
16853 	return 0;
16854 
16855  nla_put_failure:
16856 	nlmsg_free(msg);
16857 	return -ENOBUFS;
16858 }
16859 EXPORT_SYMBOL(cfg80211_external_auth_request);
16860 
16861 void cfg80211_update_owe_info_event(struct net_device *netdev,
16862 				    struct cfg80211_update_owe_info *owe_info,
16863 				    gfp_t gfp)
16864 {
16865 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16866 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16867 	struct sk_buff *msg;
16868 	void *hdr;
16869 
16870 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
16871 
16872 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16873 	if (!msg)
16874 		return;
16875 
16876 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
16877 	if (!hdr)
16878 		goto nla_put_failure;
16879 
16880 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16881 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16882 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
16883 		goto nla_put_failure;
16884 
16885 	if (!owe_info->ie_len ||
16886 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
16887 		goto nla_put_failure;
16888 
16889 	genlmsg_end(msg, hdr);
16890 
16891 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16892 				NL80211_MCGRP_MLME, gfp);
16893 	return;
16894 
16895 nla_put_failure:
16896 	genlmsg_cancel(msg, hdr);
16897 	nlmsg_free(msg);
16898 }
16899 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
16900 
16901 /* initialisation/exit functions */
16902 
16903 int __init nl80211_init(void)
16904 {
16905 	int err;
16906 
16907 	err = genl_register_family(&nl80211_fam);
16908 	if (err)
16909 		return err;
16910 
16911 	err = netlink_register_notifier(&nl80211_netlink_notifier);
16912 	if (err)
16913 		goto err_out;
16914 
16915 	return 0;
16916  err_out:
16917 	genl_unregister_family(&nl80211_fam);
16918 	return err;
16919 }
16920 
16921 void nl80211_exit(void)
16922 {
16923 	netlink_unregister_notifier(&nl80211_netlink_notifier);
16924 	genl_unregister_family(&nl80211_fam);
16925 }
16926