xref: /openbmc/linux/net/wireless/nl80211.c (revision 1c5af5cf)
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright 2015-2017	Intel Deutschland GmbH
7  */
8 
9 #include <linux/if.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14 #include <linux/if_ether.h>
15 #include <linux/ieee80211.h>
16 #include <linux/nl80211.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/netlink.h>
19 #include <linux/nospec.h>
20 #include <linux/etherdevice.h>
21 #include <net/net_namespace.h>
22 #include <net/genetlink.h>
23 #include <net/cfg80211.h>
24 #include <net/sock.h>
25 #include <net/inet_connection_sock.h>
26 #include "core.h"
27 #include "nl80211.h"
28 #include "reg.h"
29 #include "rdev-ops.h"
30 
31 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
32 				   struct genl_info *info,
33 				   struct cfg80211_crypto_settings *settings,
34 				   int cipher_limit);
35 
36 /* the netlink family */
37 static struct genl_family nl80211_fam;
38 
39 /* multicast groups */
40 enum nl80211_multicast_groups {
41 	NL80211_MCGRP_CONFIG,
42 	NL80211_MCGRP_SCAN,
43 	NL80211_MCGRP_REGULATORY,
44 	NL80211_MCGRP_MLME,
45 	NL80211_MCGRP_VENDOR,
46 	NL80211_MCGRP_NAN,
47 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
48 };
49 
50 static const struct genl_multicast_group nl80211_mcgrps[] = {
51 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
52 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
53 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
54 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
55 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
56 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
57 #ifdef CONFIG_NL80211_TESTMODE
58 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
59 #endif
60 };
61 
62 /* returns ERR_PTR values */
63 static struct wireless_dev *
64 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
65 {
66 	struct cfg80211_registered_device *rdev;
67 	struct wireless_dev *result = NULL;
68 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
69 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
70 	u64 wdev_id;
71 	int wiphy_idx = -1;
72 	int ifidx = -1;
73 
74 	ASSERT_RTNL();
75 
76 	if (!have_ifidx && !have_wdev_id)
77 		return ERR_PTR(-EINVAL);
78 
79 	if (have_ifidx)
80 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
81 	if (have_wdev_id) {
82 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
83 		wiphy_idx = wdev_id >> 32;
84 	}
85 
86 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
87 		struct wireless_dev *wdev;
88 
89 		if (wiphy_net(&rdev->wiphy) != netns)
90 			continue;
91 
92 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
93 			continue;
94 
95 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
96 			if (have_ifidx && wdev->netdev &&
97 			    wdev->netdev->ifindex == ifidx) {
98 				result = wdev;
99 				break;
100 			}
101 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
102 				result = wdev;
103 				break;
104 			}
105 		}
106 
107 		if (result)
108 			break;
109 	}
110 
111 	if (result)
112 		return result;
113 	return ERR_PTR(-ENODEV);
114 }
115 
116 static struct cfg80211_registered_device *
117 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
118 {
119 	struct cfg80211_registered_device *rdev = NULL, *tmp;
120 	struct net_device *netdev;
121 
122 	ASSERT_RTNL();
123 
124 	if (!attrs[NL80211_ATTR_WIPHY] &&
125 	    !attrs[NL80211_ATTR_IFINDEX] &&
126 	    !attrs[NL80211_ATTR_WDEV])
127 		return ERR_PTR(-EINVAL);
128 
129 	if (attrs[NL80211_ATTR_WIPHY])
130 		rdev = cfg80211_rdev_by_wiphy_idx(
131 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
132 
133 	if (attrs[NL80211_ATTR_WDEV]) {
134 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
135 		struct wireless_dev *wdev;
136 		bool found = false;
137 
138 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
139 		if (tmp) {
140 			/* make sure wdev exists */
141 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
142 				if (wdev->identifier != (u32)wdev_id)
143 					continue;
144 				found = true;
145 				break;
146 			}
147 
148 			if (!found)
149 				tmp = NULL;
150 
151 			if (rdev && tmp != rdev)
152 				return ERR_PTR(-EINVAL);
153 			rdev = tmp;
154 		}
155 	}
156 
157 	if (attrs[NL80211_ATTR_IFINDEX]) {
158 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
159 
160 		netdev = __dev_get_by_index(netns, ifindex);
161 		if (netdev) {
162 			if (netdev->ieee80211_ptr)
163 				tmp = wiphy_to_rdev(
164 					netdev->ieee80211_ptr->wiphy);
165 			else
166 				tmp = NULL;
167 
168 			/* not wireless device -- return error */
169 			if (!tmp)
170 				return ERR_PTR(-EINVAL);
171 
172 			/* mismatch -- return error */
173 			if (rdev && tmp != rdev)
174 				return ERR_PTR(-EINVAL);
175 
176 			rdev = tmp;
177 		}
178 	}
179 
180 	if (!rdev)
181 		return ERR_PTR(-ENODEV);
182 
183 	if (netns != wiphy_net(&rdev->wiphy))
184 		return ERR_PTR(-ENODEV);
185 
186 	return rdev;
187 }
188 
189 /*
190  * This function returns a pointer to the driver
191  * that the genl_info item that is passed refers to.
192  *
193  * The result of this can be a PTR_ERR and hence must
194  * be checked with IS_ERR() for errors.
195  */
196 static struct cfg80211_registered_device *
197 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
198 {
199 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
200 }
201 
202 /* policy for the attributes */
203 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
204 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
205 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
206 				      .len = 20-1 },
207 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
208 
209 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
210 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
211 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
212 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
213 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
214 
215 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
216 	[NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
217 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
218 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
219 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
220 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
221 
222 	[NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
223 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
224 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
225 
226 	[NL80211_ATTR_MAC] = { .len = ETH_ALEN },
227 	[NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
228 
229 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
230 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
231 				    .len = WLAN_MAX_KEY_LEN },
232 	[NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
233 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
234 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
235 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
236 	[NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
237 
238 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
239 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
240 	[NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
241 				       .len = IEEE80211_MAX_DATA_LEN },
242 	[NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
243 				       .len = IEEE80211_MAX_DATA_LEN },
244 	[NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
245 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
246 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
247 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
248 					       .len = NL80211_MAX_SUPP_RATES },
249 	[NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
250 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
251 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
252 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
253 				   .len = IEEE80211_MAX_MESH_ID_LEN },
254 	[NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
255 
256 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
257 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
258 
259 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
260 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
261 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
262 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
263 					   .len = NL80211_MAX_SUPP_RATES },
264 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
265 
266 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
267 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
268 
269 	[NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
270 
271 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
272 	[NL80211_ATTR_IE] = { .type = NLA_BINARY,
273 			      .len = IEEE80211_MAX_DATA_LEN },
274 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
275 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
276 
277 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
278 				.len = IEEE80211_MAX_SSID_LEN },
279 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
280 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
281 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
282 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
283 	[NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
284 	[NL80211_ATTR_STA_FLAGS2] = {
285 		.len = sizeof(struct nl80211_sta_flag_update),
286 	},
287 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
288 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
289 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
290 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
291 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
292 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
293 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
294 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
295 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
296 	[NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
297 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
298 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
299 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
300 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
301 				 .len = IEEE80211_MAX_DATA_LEN },
302 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
303 	[NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
304 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
305 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
306 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
307 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
308 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
309 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
310 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
311 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
312 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
313 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
314 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
315 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
316 	[NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
317 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
318 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
319 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
320 	[NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
321 	[NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
322 					 .len = IEEE80211_MAX_DATA_LEN },
323 	[NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
324 					 .len = IEEE80211_MAX_DATA_LEN },
325 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
326 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
327 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
328 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
329 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
330 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
331 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
332 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
333 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
334 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
335 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
336 				      .len = IEEE80211_MAX_DATA_LEN },
337 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
338 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
339 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
340 		.len = NL80211_HT_CAPABILITY_LEN
341 	},
342 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
343 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
344 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
345 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
346 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
347 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
348 	[NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
349 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
350 	[NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
351 	[NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
352 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
353 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
354 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
355 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
356 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
357 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
358 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
359 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
360 		.len = NL80211_VHT_CAPABILITY_LEN,
361 	},
362 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
363 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
364 				  .len = IEEE80211_MAX_DATA_LEN },
365 	[NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
366 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
367 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
368 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
369 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
370 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
371 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
372 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
373 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
374 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
375 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
376 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
377 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
378 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
379 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
380 	[NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
381 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
382 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
383 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
384 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
385 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
386 	[NL80211_ATTR_TSID] = { .type = NLA_U8 },
387 	[NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
388 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
389 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
390 	[NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
391 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
392 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
393 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
394 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
395 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
396 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
397 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] = { .type = NLA_U8 },
398 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
399 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
400 	},
401 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = { .len = ETH_ALEN },
402 	[NL80211_ATTR_NAN_MASTER_PREF] = { .type = NLA_U8 },
403 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
404 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
405 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
406 				    .len = FILS_MAX_KEK_LEN },
407 	[NL80211_ATTR_FILS_NONCES] = { .len = 2 * FILS_NONCE_LEN },
408 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
409 	[NL80211_ATTR_BSSID] = { .len = ETH_ALEN },
410 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
411 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
412 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
413 	},
414 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
415 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
416 					     .len = FILS_ERP_MAX_USERNAME_LEN },
417 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
418 					  .len = FILS_ERP_MAX_REALM_LEN },
419 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
420 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
421 					.len = FILS_ERP_MAX_RRK_LEN },
422 	[NL80211_ATTR_FILS_CACHE_ID] = { .len = 2 },
423 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
424 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
425 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
426 };
427 
428 /* policy for the key attributes */
429 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
430 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
431 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
432 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
433 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
434 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
435 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
436 	[NL80211_KEY_TYPE] = { .type = NLA_U32 },
437 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
438 };
439 
440 /* policy for the key default flags */
441 static const struct nla_policy
442 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
443 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
444 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
445 };
446 
447 #ifdef CONFIG_PM
448 /* policy for WoWLAN attributes */
449 static const struct nla_policy
450 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
451 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
452 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
453 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
454 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
455 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
456 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
457 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
458 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
459 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
460 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
461 };
462 
463 static const struct nla_policy
464 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
465 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
466 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
467 	[NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
468 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
469 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
470 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
471 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
472 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
473 	},
474 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
475 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
476 	},
477 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
478 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
479 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
480 };
481 #endif /* CONFIG_PM */
482 
483 /* policy for coalesce rule attributes */
484 static const struct nla_policy
485 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
486 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
487 	[NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
488 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
489 };
490 
491 /* policy for GTK rekey offload attributes */
492 static const struct nla_policy
493 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
494 	[NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
495 	[NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
496 	[NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
497 };
498 
499 static const struct nla_policy
500 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
501 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
502 						 .len = IEEE80211_MAX_SSID_LEN },
503 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = { .len = ETH_ALEN },
504 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
505 };
506 
507 static const struct nla_policy
508 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
509 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
510 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
511 };
512 
513 static const struct nla_policy
514 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
515 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
516 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
517 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
518 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
519 	},
520 };
521 
522 /* policy for NAN function attributes */
523 static const struct nla_policy
524 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
525 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
526 	[NL80211_NAN_FUNC_SERVICE_ID] = {
527 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
528 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
529 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
530 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
531 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
532 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
533 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { .len = ETH_ALEN },
534 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
535 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
536 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
537 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
538 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
539 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
540 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
541 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
542 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
543 };
544 
545 /* policy for Service Response Filter attributes */
546 static const struct nla_policy
547 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
548 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
549 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
550 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
551 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
552 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
553 };
554 
555 /* policy for packet pattern attributes */
556 static const struct nla_policy
557 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
558 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
559 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
560 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
561 };
562 
563 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
564 				     struct netlink_callback *cb,
565 				     struct cfg80211_registered_device **rdev,
566 				     struct wireless_dev **wdev)
567 {
568 	int err;
569 
570 	if (!cb->args[0]) {
571 		err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
572 				  genl_family_attrbuf(&nl80211_fam),
573 				  nl80211_fam.maxattr, nl80211_policy, NULL);
574 		if (err)
575 			return err;
576 
577 		*wdev = __cfg80211_wdev_from_attrs(
578 					sock_net(skb->sk),
579 					genl_family_attrbuf(&nl80211_fam));
580 		if (IS_ERR(*wdev))
581 			return PTR_ERR(*wdev);
582 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
583 		/* 0 is the first index - add 1 to parse only once */
584 		cb->args[0] = (*rdev)->wiphy_idx + 1;
585 		cb->args[1] = (*wdev)->identifier;
586 	} else {
587 		/* subtract the 1 again here */
588 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
589 		struct wireless_dev *tmp;
590 
591 		if (!wiphy)
592 			return -ENODEV;
593 		*rdev = wiphy_to_rdev(wiphy);
594 		*wdev = NULL;
595 
596 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
597 			if (tmp->identifier == cb->args[1]) {
598 				*wdev = tmp;
599 				break;
600 			}
601 		}
602 
603 		if (!*wdev)
604 			return -ENODEV;
605 	}
606 
607 	return 0;
608 }
609 
610 /* IE validation */
611 static bool is_valid_ie_attr(const struct nlattr *attr)
612 {
613 	const u8 *pos;
614 	int len;
615 
616 	if (!attr)
617 		return true;
618 
619 	pos = nla_data(attr);
620 	len = nla_len(attr);
621 
622 	while (len) {
623 		u8 elemlen;
624 
625 		if (len < 2)
626 			return false;
627 		len -= 2;
628 
629 		elemlen = pos[1];
630 		if (elemlen > len)
631 			return false;
632 
633 		len -= elemlen;
634 		pos += 2 + elemlen;
635 	}
636 
637 	return true;
638 }
639 
640 /* message building helper */
641 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
642 				   int flags, u8 cmd)
643 {
644 	/* since there is no private header just add the generic one */
645 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
646 }
647 
648 static int nl80211_msg_put_channel(struct sk_buff *msg,
649 				   struct ieee80211_channel *chan,
650 				   bool large)
651 {
652 	/* Some channels must be completely excluded from the
653 	 * list to protect old user-space tools from breaking
654 	 */
655 	if (!large && chan->flags &
656 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
657 		return 0;
658 
659 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
660 			chan->center_freq))
661 		goto nla_put_failure;
662 
663 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
664 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
665 		goto nla_put_failure;
666 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
667 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
668 			goto nla_put_failure;
669 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
670 			goto nla_put_failure;
671 	}
672 	if (chan->flags & IEEE80211_CHAN_RADAR) {
673 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
674 			goto nla_put_failure;
675 		if (large) {
676 			u32 time;
677 
678 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
679 
680 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
681 					chan->dfs_state))
682 				goto nla_put_failure;
683 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
684 					time))
685 				goto nla_put_failure;
686 			if (nla_put_u32(msg,
687 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
688 					chan->dfs_cac_ms))
689 				goto nla_put_failure;
690 		}
691 	}
692 
693 	if (large) {
694 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
695 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
696 			goto nla_put_failure;
697 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
698 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
699 			goto nla_put_failure;
700 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
701 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
702 			goto nla_put_failure;
703 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
704 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
705 			goto nla_put_failure;
706 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
707 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
708 			goto nla_put_failure;
709 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
710 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
711 			goto nla_put_failure;
712 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
713 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
714 			goto nla_put_failure;
715 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
716 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
717 			goto nla_put_failure;
718 	}
719 
720 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
721 			DBM_TO_MBM(chan->max_power)))
722 		goto nla_put_failure;
723 
724 	return 0;
725 
726  nla_put_failure:
727 	return -ENOBUFS;
728 }
729 
730 /* netlink command implementations */
731 
732 struct key_parse {
733 	struct key_params p;
734 	int idx;
735 	int type;
736 	bool def, defmgmt;
737 	bool def_uni, def_multi;
738 };
739 
740 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
741 				 struct key_parse *k)
742 {
743 	struct nlattr *tb[NL80211_KEY_MAX + 1];
744 	int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
745 				   nl80211_key_policy, info->extack);
746 	if (err)
747 		return err;
748 
749 	k->def = !!tb[NL80211_KEY_DEFAULT];
750 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
751 
752 	if (k->def) {
753 		k->def_uni = true;
754 		k->def_multi = true;
755 	}
756 	if (k->defmgmt)
757 		k->def_multi = true;
758 
759 	if (tb[NL80211_KEY_IDX])
760 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
761 
762 	if (tb[NL80211_KEY_DATA]) {
763 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
764 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
765 	}
766 
767 	if (tb[NL80211_KEY_SEQ]) {
768 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
769 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
770 	}
771 
772 	if (tb[NL80211_KEY_CIPHER])
773 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
774 
775 	if (tb[NL80211_KEY_TYPE]) {
776 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
777 		if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
778 			return genl_err_attr(info, -EINVAL,
779 					     tb[NL80211_KEY_TYPE]);
780 	}
781 
782 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
783 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
784 
785 		err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
786 				       tb[NL80211_KEY_DEFAULT_TYPES],
787 				       nl80211_key_default_policy,
788 				       info->extack);
789 		if (err)
790 			return err;
791 
792 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
793 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
794 	}
795 
796 	return 0;
797 }
798 
799 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
800 {
801 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
802 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
803 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
804 	}
805 
806 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
807 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
808 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
809 	}
810 
811 	if (info->attrs[NL80211_ATTR_KEY_IDX])
812 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
813 
814 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
815 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
816 
817 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
818 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
819 
820 	if (k->def) {
821 		k->def_uni = true;
822 		k->def_multi = true;
823 	}
824 	if (k->defmgmt)
825 		k->def_multi = true;
826 
827 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
828 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
829 		if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES) {
830 			GENL_SET_ERR_MSG(info, "key type out of range");
831 			return -EINVAL;
832 		}
833 	}
834 
835 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
836 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
837 		int err = nla_parse_nested(kdt,
838 					   NUM_NL80211_KEY_DEFAULT_TYPES - 1,
839 					   info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
840 					   nl80211_key_default_policy,
841 					   info->extack);
842 		if (err)
843 			return err;
844 
845 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
846 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
847 	}
848 
849 	return 0;
850 }
851 
852 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
853 {
854 	int err;
855 
856 	memset(k, 0, sizeof(*k));
857 	k->idx = -1;
858 	k->type = -1;
859 
860 	if (info->attrs[NL80211_ATTR_KEY])
861 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
862 	else
863 		err = nl80211_parse_key_old(info, k);
864 
865 	if (err)
866 		return err;
867 
868 	if (k->def && k->defmgmt) {
869 		GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
870 		return -EINVAL;
871 	}
872 
873 	if (k->defmgmt) {
874 		if (k->def_uni || !k->def_multi) {
875 			GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
876 			return -EINVAL;
877 		}
878 	}
879 
880 	if (k->idx != -1) {
881 		if (k->defmgmt) {
882 			if (k->idx < 4 || k->idx > 5) {
883 				GENL_SET_ERR_MSG(info,
884 						 "defmgmt key idx not 4 or 5");
885 				return -EINVAL;
886 			}
887 		} else if (k->def) {
888 			if (k->idx < 0 || k->idx > 3) {
889 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
890 				return -EINVAL;
891 			}
892 		} else {
893 			if (k->idx < 0 || k->idx > 5) {
894 				GENL_SET_ERR_MSG(info, "key idx not 0-5");
895 				return -EINVAL;
896 			}
897 		}
898 	}
899 
900 	return 0;
901 }
902 
903 static struct cfg80211_cached_keys *
904 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
905 		       struct genl_info *info, bool *no_ht)
906 {
907 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
908 	struct key_parse parse;
909 	struct nlattr *key;
910 	struct cfg80211_cached_keys *result;
911 	int rem, err, def = 0;
912 	bool have_key = false;
913 
914 	nla_for_each_nested(key, keys, rem) {
915 		have_key = true;
916 		break;
917 	}
918 
919 	if (!have_key)
920 		return NULL;
921 
922 	result = kzalloc(sizeof(*result), GFP_KERNEL);
923 	if (!result)
924 		return ERR_PTR(-ENOMEM);
925 
926 	result->def = -1;
927 
928 	nla_for_each_nested(key, keys, rem) {
929 		memset(&parse, 0, sizeof(parse));
930 		parse.idx = -1;
931 
932 		err = nl80211_parse_key_new(info, key, &parse);
933 		if (err)
934 			goto error;
935 		err = -EINVAL;
936 		if (!parse.p.key)
937 			goto error;
938 		if (parse.idx < 0 || parse.idx > 3) {
939 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
940 			goto error;
941 		}
942 		if (parse.def) {
943 			if (def) {
944 				GENL_SET_ERR_MSG(info,
945 						 "only one key can be default");
946 				goto error;
947 			}
948 			def = 1;
949 			result->def = parse.idx;
950 			if (!parse.def_uni || !parse.def_multi)
951 				goto error;
952 		} else if (parse.defmgmt)
953 			goto error;
954 		err = cfg80211_validate_key_settings(rdev, &parse.p,
955 						     parse.idx, false, NULL);
956 		if (err)
957 			goto error;
958 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
959 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
960 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
961 			err = -EINVAL;
962 			goto error;
963 		}
964 		result->params[parse.idx].cipher = parse.p.cipher;
965 		result->params[parse.idx].key_len = parse.p.key_len;
966 		result->params[parse.idx].key = result->data[parse.idx];
967 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
968 
969 		/* must be WEP key if we got here */
970 		if (no_ht)
971 			*no_ht = true;
972 	}
973 
974 	if (result->def < 0) {
975 		err = -EINVAL;
976 		GENL_SET_ERR_MSG(info, "need a default/TX key");
977 		goto error;
978 	}
979 
980 	return result;
981  error:
982 	kfree(result);
983 	return ERR_PTR(err);
984 }
985 
986 static int nl80211_key_allowed(struct wireless_dev *wdev)
987 {
988 	ASSERT_WDEV_LOCK(wdev);
989 
990 	switch (wdev->iftype) {
991 	case NL80211_IFTYPE_AP:
992 	case NL80211_IFTYPE_AP_VLAN:
993 	case NL80211_IFTYPE_P2P_GO:
994 	case NL80211_IFTYPE_MESH_POINT:
995 		break;
996 	case NL80211_IFTYPE_ADHOC:
997 	case NL80211_IFTYPE_STATION:
998 	case NL80211_IFTYPE_P2P_CLIENT:
999 		if (!wdev->current_bss)
1000 			return -ENOLINK;
1001 		break;
1002 	case NL80211_IFTYPE_UNSPECIFIED:
1003 	case NL80211_IFTYPE_OCB:
1004 	case NL80211_IFTYPE_MONITOR:
1005 	case NL80211_IFTYPE_NAN:
1006 	case NL80211_IFTYPE_P2P_DEVICE:
1007 	case NL80211_IFTYPE_WDS:
1008 	case NUM_NL80211_IFTYPES:
1009 		return -EINVAL;
1010 	}
1011 
1012 	return 0;
1013 }
1014 
1015 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1016 							struct nlattr *tb)
1017 {
1018 	struct ieee80211_channel *chan;
1019 
1020 	if (tb == NULL)
1021 		return NULL;
1022 	chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1023 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1024 		return NULL;
1025 	return chan;
1026 }
1027 
1028 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1029 {
1030 	struct nlattr *nl_modes = nla_nest_start(msg, attr);
1031 	int i;
1032 
1033 	if (!nl_modes)
1034 		goto nla_put_failure;
1035 
1036 	i = 0;
1037 	while (ifmodes) {
1038 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1039 			goto nla_put_failure;
1040 		ifmodes >>= 1;
1041 		i++;
1042 	}
1043 
1044 	nla_nest_end(msg, nl_modes);
1045 	return 0;
1046 
1047 nla_put_failure:
1048 	return -ENOBUFS;
1049 }
1050 
1051 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1052 					  struct sk_buff *msg,
1053 					  bool large)
1054 {
1055 	struct nlattr *nl_combis;
1056 	int i, j;
1057 
1058 	nl_combis = nla_nest_start(msg,
1059 				NL80211_ATTR_INTERFACE_COMBINATIONS);
1060 	if (!nl_combis)
1061 		goto nla_put_failure;
1062 
1063 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1064 		const struct ieee80211_iface_combination *c;
1065 		struct nlattr *nl_combi, *nl_limits;
1066 
1067 		c = &wiphy->iface_combinations[i];
1068 
1069 		nl_combi = nla_nest_start(msg, i + 1);
1070 		if (!nl_combi)
1071 			goto nla_put_failure;
1072 
1073 		nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
1074 		if (!nl_limits)
1075 			goto nla_put_failure;
1076 
1077 		for (j = 0; j < c->n_limits; j++) {
1078 			struct nlattr *nl_limit;
1079 
1080 			nl_limit = nla_nest_start(msg, j + 1);
1081 			if (!nl_limit)
1082 				goto nla_put_failure;
1083 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1084 					c->limits[j].max))
1085 				goto nla_put_failure;
1086 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1087 						c->limits[j].types))
1088 				goto nla_put_failure;
1089 			nla_nest_end(msg, nl_limit);
1090 		}
1091 
1092 		nla_nest_end(msg, nl_limits);
1093 
1094 		if (c->beacon_int_infra_match &&
1095 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1096 			goto nla_put_failure;
1097 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1098 				c->num_different_channels) ||
1099 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1100 				c->max_interfaces))
1101 			goto nla_put_failure;
1102 		if (large &&
1103 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1104 				c->radar_detect_widths) ||
1105 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1106 				c->radar_detect_regions)))
1107 			goto nla_put_failure;
1108 		if (c->beacon_int_min_gcd &&
1109 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1110 				c->beacon_int_min_gcd))
1111 			goto nla_put_failure;
1112 
1113 		nla_nest_end(msg, nl_combi);
1114 	}
1115 
1116 	nla_nest_end(msg, nl_combis);
1117 
1118 	return 0;
1119 nla_put_failure:
1120 	return -ENOBUFS;
1121 }
1122 
1123 #ifdef CONFIG_PM
1124 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1125 					struct sk_buff *msg)
1126 {
1127 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1128 	struct nlattr *nl_tcp;
1129 
1130 	if (!tcp)
1131 		return 0;
1132 
1133 	nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1134 	if (!nl_tcp)
1135 		return -ENOBUFS;
1136 
1137 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1138 			tcp->data_payload_max))
1139 		return -ENOBUFS;
1140 
1141 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1142 			tcp->data_payload_max))
1143 		return -ENOBUFS;
1144 
1145 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1146 		return -ENOBUFS;
1147 
1148 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1149 				sizeof(*tcp->tok), tcp->tok))
1150 		return -ENOBUFS;
1151 
1152 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1153 			tcp->data_interval_max))
1154 		return -ENOBUFS;
1155 
1156 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1157 			tcp->wake_payload_max))
1158 		return -ENOBUFS;
1159 
1160 	nla_nest_end(msg, nl_tcp);
1161 	return 0;
1162 }
1163 
1164 static int nl80211_send_wowlan(struct sk_buff *msg,
1165 			       struct cfg80211_registered_device *rdev,
1166 			       bool large)
1167 {
1168 	struct nlattr *nl_wowlan;
1169 
1170 	if (!rdev->wiphy.wowlan)
1171 		return 0;
1172 
1173 	nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1174 	if (!nl_wowlan)
1175 		return -ENOBUFS;
1176 
1177 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1178 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1179 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1180 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1181 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1182 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1183 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1184 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1185 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1186 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1187 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1188 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1189 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1190 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1191 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1192 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1193 		return -ENOBUFS;
1194 
1195 	if (rdev->wiphy.wowlan->n_patterns) {
1196 		struct nl80211_pattern_support pat = {
1197 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1198 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1199 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1200 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1201 		};
1202 
1203 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1204 			    sizeof(pat), &pat))
1205 			return -ENOBUFS;
1206 	}
1207 
1208 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1209 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1210 			rdev->wiphy.wowlan->max_nd_match_sets))
1211 		return -ENOBUFS;
1212 
1213 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1214 		return -ENOBUFS;
1215 
1216 	nla_nest_end(msg, nl_wowlan);
1217 
1218 	return 0;
1219 }
1220 #endif
1221 
1222 static int nl80211_send_coalesce(struct sk_buff *msg,
1223 				 struct cfg80211_registered_device *rdev)
1224 {
1225 	struct nl80211_coalesce_rule_support rule;
1226 
1227 	if (!rdev->wiphy.coalesce)
1228 		return 0;
1229 
1230 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1231 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1232 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1233 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1234 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1235 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1236 
1237 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1238 		return -ENOBUFS;
1239 
1240 	return 0;
1241 }
1242 
1243 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1244 				      struct ieee80211_supported_band *sband)
1245 {
1246 	struct nlattr *nl_rates, *nl_rate;
1247 	struct ieee80211_rate *rate;
1248 	int i;
1249 
1250 	/* add HT info */
1251 	if (sband->ht_cap.ht_supported &&
1252 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1253 		     sizeof(sband->ht_cap.mcs),
1254 		     &sband->ht_cap.mcs) ||
1255 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1256 			 sband->ht_cap.cap) ||
1257 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1258 			sband->ht_cap.ampdu_factor) ||
1259 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1260 			sband->ht_cap.ampdu_density)))
1261 		return -ENOBUFS;
1262 
1263 	/* add VHT info */
1264 	if (sband->vht_cap.vht_supported &&
1265 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1266 		     sizeof(sband->vht_cap.vht_mcs),
1267 		     &sband->vht_cap.vht_mcs) ||
1268 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1269 			 sband->vht_cap.cap)))
1270 		return -ENOBUFS;
1271 
1272 	/* add bitrates */
1273 	nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1274 	if (!nl_rates)
1275 		return -ENOBUFS;
1276 
1277 	for (i = 0; i < sband->n_bitrates; i++) {
1278 		nl_rate = nla_nest_start(msg, i);
1279 		if (!nl_rate)
1280 			return -ENOBUFS;
1281 
1282 		rate = &sband->bitrates[i];
1283 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1284 				rate->bitrate))
1285 			return -ENOBUFS;
1286 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1287 		    nla_put_flag(msg,
1288 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1289 			return -ENOBUFS;
1290 
1291 		nla_nest_end(msg, nl_rate);
1292 	}
1293 
1294 	nla_nest_end(msg, nl_rates);
1295 
1296 	return 0;
1297 }
1298 
1299 static int
1300 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1301 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1302 {
1303 	u16 stypes;
1304 	struct nlattr *nl_ftypes, *nl_ifs;
1305 	enum nl80211_iftype ift;
1306 	int i;
1307 
1308 	if (!mgmt_stypes)
1309 		return 0;
1310 
1311 	nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1312 	if (!nl_ifs)
1313 		return -ENOBUFS;
1314 
1315 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1316 		nl_ftypes = nla_nest_start(msg, ift);
1317 		if (!nl_ftypes)
1318 			return -ENOBUFS;
1319 		i = 0;
1320 		stypes = mgmt_stypes[ift].tx;
1321 		while (stypes) {
1322 			if ((stypes & 1) &&
1323 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1324 					(i << 4) | IEEE80211_FTYPE_MGMT))
1325 				return -ENOBUFS;
1326 			stypes >>= 1;
1327 			i++;
1328 		}
1329 		nla_nest_end(msg, nl_ftypes);
1330 	}
1331 
1332 	nla_nest_end(msg, nl_ifs);
1333 
1334 	nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1335 	if (!nl_ifs)
1336 		return -ENOBUFS;
1337 
1338 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1339 		nl_ftypes = nla_nest_start(msg, ift);
1340 		if (!nl_ftypes)
1341 			return -ENOBUFS;
1342 		i = 0;
1343 		stypes = mgmt_stypes[ift].rx;
1344 		while (stypes) {
1345 			if ((stypes & 1) &&
1346 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1347 					(i << 4) | IEEE80211_FTYPE_MGMT))
1348 				return -ENOBUFS;
1349 			stypes >>= 1;
1350 			i++;
1351 		}
1352 		nla_nest_end(msg, nl_ftypes);
1353 	}
1354 	nla_nest_end(msg, nl_ifs);
1355 
1356 	return 0;
1357 }
1358 
1359 #define CMD(op, n)							\
1360 	 do {								\
1361 		if (rdev->ops->op) {					\
1362 			i++;						\
1363 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1364 				goto nla_put_failure;			\
1365 		}							\
1366 	} while (0)
1367 
1368 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1369 					struct sk_buff *msg)
1370 {
1371 	int i = 0;
1372 
1373 	/*
1374 	 * do *NOT* add anything into this function, new things need to be
1375 	 * advertised only to new versions of userspace that can deal with
1376 	 * the split (and they can't possibly care about new features...
1377 	 */
1378 	CMD(add_virtual_intf, NEW_INTERFACE);
1379 	CMD(change_virtual_intf, SET_INTERFACE);
1380 	CMD(add_key, NEW_KEY);
1381 	CMD(start_ap, START_AP);
1382 	CMD(add_station, NEW_STATION);
1383 	CMD(add_mpath, NEW_MPATH);
1384 	CMD(update_mesh_config, SET_MESH_CONFIG);
1385 	CMD(change_bss, SET_BSS);
1386 	CMD(auth, AUTHENTICATE);
1387 	CMD(assoc, ASSOCIATE);
1388 	CMD(deauth, DEAUTHENTICATE);
1389 	CMD(disassoc, DISASSOCIATE);
1390 	CMD(join_ibss, JOIN_IBSS);
1391 	CMD(join_mesh, JOIN_MESH);
1392 	CMD(set_pmksa, SET_PMKSA);
1393 	CMD(del_pmksa, DEL_PMKSA);
1394 	CMD(flush_pmksa, FLUSH_PMKSA);
1395 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1396 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1397 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1398 	CMD(mgmt_tx, FRAME);
1399 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1400 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1401 		i++;
1402 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1403 			goto nla_put_failure;
1404 	}
1405 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1406 	    rdev->ops->join_mesh) {
1407 		i++;
1408 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1409 			goto nla_put_failure;
1410 	}
1411 	CMD(set_wds_peer, SET_WDS_PEER);
1412 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1413 		CMD(tdls_mgmt, TDLS_MGMT);
1414 		CMD(tdls_oper, TDLS_OPER);
1415 	}
1416 	if (rdev->wiphy.max_sched_scan_reqs)
1417 		CMD(sched_scan_start, START_SCHED_SCAN);
1418 	CMD(probe_client, PROBE_CLIENT);
1419 	CMD(set_noack_map, SET_NOACK_MAP);
1420 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1421 		i++;
1422 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1423 			goto nla_put_failure;
1424 	}
1425 	CMD(start_p2p_device, START_P2P_DEVICE);
1426 	CMD(set_mcast_rate, SET_MCAST_RATE);
1427 #ifdef CONFIG_NL80211_TESTMODE
1428 	CMD(testmode_cmd, TESTMODE);
1429 #endif
1430 
1431 	if (rdev->ops->connect || rdev->ops->auth) {
1432 		i++;
1433 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1434 			goto nla_put_failure;
1435 	}
1436 
1437 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1438 		i++;
1439 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1440 			goto nla_put_failure;
1441 	}
1442 
1443 	return i;
1444  nla_put_failure:
1445 	return -ENOBUFS;
1446 }
1447 
1448 struct nl80211_dump_wiphy_state {
1449 	s64 filter_wiphy;
1450 	long start;
1451 	long split_start, band_start, chan_start, capa_start;
1452 	bool split;
1453 };
1454 
1455 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1456 			      enum nl80211_commands cmd,
1457 			      struct sk_buff *msg, u32 portid, u32 seq,
1458 			      int flags, struct nl80211_dump_wiphy_state *state)
1459 {
1460 	void *hdr;
1461 	struct nlattr *nl_bands, *nl_band;
1462 	struct nlattr *nl_freqs, *nl_freq;
1463 	struct nlattr *nl_cmds;
1464 	enum nl80211_band band;
1465 	struct ieee80211_channel *chan;
1466 	int i;
1467 	const struct ieee80211_txrx_stypes *mgmt_stypes =
1468 				rdev->wiphy.mgmt_stypes;
1469 	u32 features;
1470 
1471 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1472 	if (!hdr)
1473 		return -ENOBUFS;
1474 
1475 	if (WARN_ON(!state))
1476 		return -EINVAL;
1477 
1478 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1479 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1480 			   wiphy_name(&rdev->wiphy)) ||
1481 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
1482 			cfg80211_rdev_list_generation))
1483 		goto nla_put_failure;
1484 
1485 	if (cmd != NL80211_CMD_NEW_WIPHY)
1486 		goto finish;
1487 
1488 	switch (state->split_start) {
1489 	case 0:
1490 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1491 			       rdev->wiphy.retry_short) ||
1492 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1493 			       rdev->wiphy.retry_long) ||
1494 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1495 				rdev->wiphy.frag_threshold) ||
1496 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1497 				rdev->wiphy.rts_threshold) ||
1498 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1499 			       rdev->wiphy.coverage_class) ||
1500 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1501 			       rdev->wiphy.max_scan_ssids) ||
1502 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1503 			       rdev->wiphy.max_sched_scan_ssids) ||
1504 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1505 				rdev->wiphy.max_scan_ie_len) ||
1506 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1507 				rdev->wiphy.max_sched_scan_ie_len) ||
1508 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1509 			       rdev->wiphy.max_match_sets) ||
1510 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1511 				rdev->wiphy.max_sched_scan_plans) ||
1512 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1513 				rdev->wiphy.max_sched_scan_plan_interval) ||
1514 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1515 				rdev->wiphy.max_sched_scan_plan_iterations))
1516 			goto nla_put_failure;
1517 
1518 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1519 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1520 			goto nla_put_failure;
1521 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1522 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1523 			goto nla_put_failure;
1524 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1525 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1526 			goto nla_put_failure;
1527 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1528 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1529 			goto nla_put_failure;
1530 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1531 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1532 			goto nla_put_failure;
1533 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1534 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1535 			goto nla_put_failure;
1536 		state->split_start++;
1537 		if (state->split)
1538 			break;
1539 	case 1:
1540 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1541 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
1542 			    rdev->wiphy.cipher_suites))
1543 			goto nla_put_failure;
1544 
1545 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1546 			       rdev->wiphy.max_num_pmkids))
1547 			goto nla_put_failure;
1548 
1549 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1550 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1551 			goto nla_put_failure;
1552 
1553 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1554 				rdev->wiphy.available_antennas_tx) ||
1555 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1556 				rdev->wiphy.available_antennas_rx))
1557 			goto nla_put_failure;
1558 
1559 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1560 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1561 				rdev->wiphy.probe_resp_offload))
1562 			goto nla_put_failure;
1563 
1564 		if ((rdev->wiphy.available_antennas_tx ||
1565 		     rdev->wiphy.available_antennas_rx) &&
1566 		    rdev->ops->get_antenna) {
1567 			u32 tx_ant = 0, rx_ant = 0;
1568 			int res;
1569 
1570 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1571 			if (!res) {
1572 				if (nla_put_u32(msg,
1573 						NL80211_ATTR_WIPHY_ANTENNA_TX,
1574 						tx_ant) ||
1575 				    nla_put_u32(msg,
1576 						NL80211_ATTR_WIPHY_ANTENNA_RX,
1577 						rx_ant))
1578 					goto nla_put_failure;
1579 			}
1580 		}
1581 
1582 		state->split_start++;
1583 		if (state->split)
1584 			break;
1585 	case 2:
1586 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1587 					rdev->wiphy.interface_modes))
1588 				goto nla_put_failure;
1589 		state->split_start++;
1590 		if (state->split)
1591 			break;
1592 	case 3:
1593 		nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1594 		if (!nl_bands)
1595 			goto nla_put_failure;
1596 
1597 		for (band = state->band_start;
1598 		     band < NUM_NL80211_BANDS; band++) {
1599 			struct ieee80211_supported_band *sband;
1600 
1601 			sband = rdev->wiphy.bands[band];
1602 
1603 			if (!sband)
1604 				continue;
1605 
1606 			nl_band = nla_nest_start(msg, band);
1607 			if (!nl_band)
1608 				goto nla_put_failure;
1609 
1610 			switch (state->chan_start) {
1611 			case 0:
1612 				if (nl80211_send_band_rateinfo(msg, sband))
1613 					goto nla_put_failure;
1614 				state->chan_start++;
1615 				if (state->split)
1616 					break;
1617 			default:
1618 				/* add frequencies */
1619 				nl_freqs = nla_nest_start(
1620 					msg, NL80211_BAND_ATTR_FREQS);
1621 				if (!nl_freqs)
1622 					goto nla_put_failure;
1623 
1624 				for (i = state->chan_start - 1;
1625 				     i < sband->n_channels;
1626 				     i++) {
1627 					nl_freq = nla_nest_start(msg, i);
1628 					if (!nl_freq)
1629 						goto nla_put_failure;
1630 
1631 					chan = &sband->channels[i];
1632 
1633 					if (nl80211_msg_put_channel(
1634 							msg, chan,
1635 							state->split))
1636 						goto nla_put_failure;
1637 
1638 					nla_nest_end(msg, nl_freq);
1639 					if (state->split)
1640 						break;
1641 				}
1642 				if (i < sband->n_channels)
1643 					state->chan_start = i + 2;
1644 				else
1645 					state->chan_start = 0;
1646 				nla_nest_end(msg, nl_freqs);
1647 			}
1648 
1649 			nla_nest_end(msg, nl_band);
1650 
1651 			if (state->split) {
1652 				/* start again here */
1653 				if (state->chan_start)
1654 					band--;
1655 				break;
1656 			}
1657 		}
1658 		nla_nest_end(msg, nl_bands);
1659 
1660 		if (band < NUM_NL80211_BANDS)
1661 			state->band_start = band + 1;
1662 		else
1663 			state->band_start = 0;
1664 
1665 		/* if bands & channels are done, continue outside */
1666 		if (state->band_start == 0 && state->chan_start == 0)
1667 			state->split_start++;
1668 		if (state->split)
1669 			break;
1670 	case 4:
1671 		nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1672 		if (!nl_cmds)
1673 			goto nla_put_failure;
1674 
1675 		i = nl80211_add_commands_unsplit(rdev, msg);
1676 		if (i < 0)
1677 			goto nla_put_failure;
1678 		if (state->split) {
1679 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
1680 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1681 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1682 				CMD(channel_switch, CHANNEL_SWITCH);
1683 			CMD(set_qos_map, SET_QOS_MAP);
1684 			if (rdev->wiphy.features &
1685 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1686 				CMD(add_tx_ts, ADD_TX_TS);
1687 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
1688 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
1689 		}
1690 #undef CMD
1691 
1692 		nla_nest_end(msg, nl_cmds);
1693 		state->split_start++;
1694 		if (state->split)
1695 			break;
1696 	case 5:
1697 		if (rdev->ops->remain_on_channel &&
1698 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1699 		    nla_put_u32(msg,
1700 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1701 				rdev->wiphy.max_remain_on_channel_duration))
1702 			goto nla_put_failure;
1703 
1704 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1705 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1706 			goto nla_put_failure;
1707 
1708 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1709 			goto nla_put_failure;
1710 		state->split_start++;
1711 		if (state->split)
1712 			break;
1713 	case 6:
1714 #ifdef CONFIG_PM
1715 		if (nl80211_send_wowlan(msg, rdev, state->split))
1716 			goto nla_put_failure;
1717 		state->split_start++;
1718 		if (state->split)
1719 			break;
1720 #else
1721 		state->split_start++;
1722 #endif
1723 	case 7:
1724 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1725 					rdev->wiphy.software_iftypes))
1726 			goto nla_put_failure;
1727 
1728 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1729 						   state->split))
1730 			goto nla_put_failure;
1731 
1732 		state->split_start++;
1733 		if (state->split)
1734 			break;
1735 	case 8:
1736 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1737 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1738 				rdev->wiphy.ap_sme_capa))
1739 			goto nla_put_failure;
1740 
1741 		features = rdev->wiphy.features;
1742 		/*
1743 		 * We can only add the per-channel limit information if the
1744 		 * dump is split, otherwise it makes it too big. Therefore
1745 		 * only advertise it in that case.
1746 		 */
1747 		if (state->split)
1748 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1749 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1750 			goto nla_put_failure;
1751 
1752 		if (rdev->wiphy.ht_capa_mod_mask &&
1753 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1754 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
1755 			    rdev->wiphy.ht_capa_mod_mask))
1756 			goto nla_put_failure;
1757 
1758 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1759 		    rdev->wiphy.max_acl_mac_addrs &&
1760 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1761 				rdev->wiphy.max_acl_mac_addrs))
1762 			goto nla_put_failure;
1763 
1764 		/*
1765 		 * Any information below this point is only available to
1766 		 * applications that can deal with it being split. This
1767 		 * helps ensure that newly added capabilities don't break
1768 		 * older tools by overrunning their buffers.
1769 		 *
1770 		 * We still increment split_start so that in the split
1771 		 * case we'll continue with more data in the next round,
1772 		 * but break unconditionally so unsplit data stops here.
1773 		 */
1774 		state->split_start++;
1775 		break;
1776 	case 9:
1777 		if (rdev->wiphy.extended_capabilities &&
1778 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1779 			     rdev->wiphy.extended_capabilities_len,
1780 			     rdev->wiphy.extended_capabilities) ||
1781 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1782 			     rdev->wiphy.extended_capabilities_len,
1783 			     rdev->wiphy.extended_capabilities_mask)))
1784 			goto nla_put_failure;
1785 
1786 		if (rdev->wiphy.vht_capa_mod_mask &&
1787 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1788 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
1789 			    rdev->wiphy.vht_capa_mod_mask))
1790 			goto nla_put_failure;
1791 
1792 		state->split_start++;
1793 		break;
1794 	case 10:
1795 		if (nl80211_send_coalesce(msg, rdev))
1796 			goto nla_put_failure;
1797 
1798 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1799 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1800 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1801 			goto nla_put_failure;
1802 
1803 		if (rdev->wiphy.max_ap_assoc_sta &&
1804 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1805 				rdev->wiphy.max_ap_assoc_sta))
1806 			goto nla_put_failure;
1807 
1808 		state->split_start++;
1809 		break;
1810 	case 11:
1811 		if (rdev->wiphy.n_vendor_commands) {
1812 			const struct nl80211_vendor_cmd_info *info;
1813 			struct nlattr *nested;
1814 
1815 			nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1816 			if (!nested)
1817 				goto nla_put_failure;
1818 
1819 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1820 				info = &rdev->wiphy.vendor_commands[i].info;
1821 				if (nla_put(msg, i + 1, sizeof(*info), info))
1822 					goto nla_put_failure;
1823 			}
1824 			nla_nest_end(msg, nested);
1825 		}
1826 
1827 		if (rdev->wiphy.n_vendor_events) {
1828 			const struct nl80211_vendor_cmd_info *info;
1829 			struct nlattr *nested;
1830 
1831 			nested = nla_nest_start(msg,
1832 						NL80211_ATTR_VENDOR_EVENTS);
1833 			if (!nested)
1834 				goto nla_put_failure;
1835 
1836 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1837 				info = &rdev->wiphy.vendor_events[i];
1838 				if (nla_put(msg, i + 1, sizeof(*info), info))
1839 					goto nla_put_failure;
1840 			}
1841 			nla_nest_end(msg, nested);
1842 		}
1843 		state->split_start++;
1844 		break;
1845 	case 12:
1846 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1847 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1848 			       rdev->wiphy.max_num_csa_counters))
1849 			goto nla_put_failure;
1850 
1851 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1852 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1853 			goto nla_put_failure;
1854 
1855 		if (rdev->wiphy.max_sched_scan_reqs &&
1856 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
1857 				rdev->wiphy.max_sched_scan_reqs))
1858 			goto nla_put_failure;
1859 
1860 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1861 			    sizeof(rdev->wiphy.ext_features),
1862 			    rdev->wiphy.ext_features))
1863 			goto nla_put_failure;
1864 
1865 		if (rdev->wiphy.bss_select_support) {
1866 			struct nlattr *nested;
1867 			u32 bss_select_support = rdev->wiphy.bss_select_support;
1868 
1869 			nested = nla_nest_start(msg, NL80211_ATTR_BSS_SELECT);
1870 			if (!nested)
1871 				goto nla_put_failure;
1872 
1873 			i = 0;
1874 			while (bss_select_support) {
1875 				if ((bss_select_support & 1) &&
1876 				    nla_put_flag(msg, i))
1877 					goto nla_put_failure;
1878 				i++;
1879 				bss_select_support >>= 1;
1880 			}
1881 			nla_nest_end(msg, nested);
1882 		}
1883 
1884 		state->split_start++;
1885 		break;
1886 	case 13:
1887 		if (rdev->wiphy.num_iftype_ext_capab &&
1888 		    rdev->wiphy.iftype_ext_capab) {
1889 			struct nlattr *nested_ext_capab, *nested;
1890 
1891 			nested = nla_nest_start(msg,
1892 						NL80211_ATTR_IFTYPE_EXT_CAPA);
1893 			if (!nested)
1894 				goto nla_put_failure;
1895 
1896 			for (i = state->capa_start;
1897 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
1898 				const struct wiphy_iftype_ext_capab *capab;
1899 
1900 				capab = &rdev->wiphy.iftype_ext_capab[i];
1901 
1902 				nested_ext_capab = nla_nest_start(msg, i);
1903 				if (!nested_ext_capab ||
1904 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1905 						capab->iftype) ||
1906 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
1907 					    capab->extended_capabilities_len,
1908 					    capab->extended_capabilities) ||
1909 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1910 					    capab->extended_capabilities_len,
1911 					    capab->extended_capabilities_mask))
1912 					goto nla_put_failure;
1913 
1914 				nla_nest_end(msg, nested_ext_capab);
1915 				if (state->split)
1916 					break;
1917 			}
1918 			nla_nest_end(msg, nested);
1919 			if (i < rdev->wiphy.num_iftype_ext_capab) {
1920 				state->capa_start = i + 1;
1921 				break;
1922 			}
1923 		}
1924 
1925 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
1926 				rdev->wiphy.nan_supported_bands))
1927 			goto nla_put_failure;
1928 
1929 		/* done */
1930 		state->split_start = 0;
1931 		break;
1932 	}
1933  finish:
1934 	genlmsg_end(msg, hdr);
1935 	return 0;
1936 
1937  nla_put_failure:
1938 	genlmsg_cancel(msg, hdr);
1939 	return -EMSGSIZE;
1940 }
1941 
1942 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1943 				    struct netlink_callback *cb,
1944 				    struct nl80211_dump_wiphy_state *state)
1945 {
1946 	struct nlattr **tb = genl_family_attrbuf(&nl80211_fam);
1947 	int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, tb,
1948 			      nl80211_fam.maxattr, nl80211_policy, NULL);
1949 	/* ignore parse errors for backward compatibility */
1950 	if (ret)
1951 		return 0;
1952 
1953 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1954 	if (tb[NL80211_ATTR_WIPHY])
1955 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1956 	if (tb[NL80211_ATTR_WDEV])
1957 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1958 	if (tb[NL80211_ATTR_IFINDEX]) {
1959 		struct net_device *netdev;
1960 		struct cfg80211_registered_device *rdev;
1961 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1962 
1963 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1964 		if (!netdev)
1965 			return -ENODEV;
1966 		if (netdev->ieee80211_ptr) {
1967 			rdev = wiphy_to_rdev(
1968 				netdev->ieee80211_ptr->wiphy);
1969 			state->filter_wiphy = rdev->wiphy_idx;
1970 		}
1971 	}
1972 
1973 	return 0;
1974 }
1975 
1976 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1977 {
1978 	int idx = 0, ret;
1979 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1980 	struct cfg80211_registered_device *rdev;
1981 
1982 	rtnl_lock();
1983 	if (!state) {
1984 		state = kzalloc(sizeof(*state), GFP_KERNEL);
1985 		if (!state) {
1986 			rtnl_unlock();
1987 			return -ENOMEM;
1988 		}
1989 		state->filter_wiphy = -1;
1990 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
1991 		if (ret) {
1992 			kfree(state);
1993 			rtnl_unlock();
1994 			return ret;
1995 		}
1996 		cb->args[0] = (long)state;
1997 	}
1998 
1999 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2000 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2001 			continue;
2002 		if (++idx <= state->start)
2003 			continue;
2004 		if (state->filter_wiphy != -1 &&
2005 		    state->filter_wiphy != rdev->wiphy_idx)
2006 			continue;
2007 		/* attempt to fit multiple wiphy data chunks into the skb */
2008 		do {
2009 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2010 						 skb,
2011 						 NETLINK_CB(cb->skb).portid,
2012 						 cb->nlh->nlmsg_seq,
2013 						 NLM_F_MULTI, state);
2014 			if (ret < 0) {
2015 				/*
2016 				 * If sending the wiphy data didn't fit (ENOBUFS
2017 				 * or EMSGSIZE returned), this SKB is still
2018 				 * empty (so it's not too big because another
2019 				 * wiphy dataset is already in the skb) and
2020 				 * we've not tried to adjust the dump allocation
2021 				 * yet ... then adjust the alloc size to be
2022 				 * bigger, and return 1 but with the empty skb.
2023 				 * This results in an empty message being RX'ed
2024 				 * in userspace, but that is ignored.
2025 				 *
2026 				 * We can then retry with the larger buffer.
2027 				 */
2028 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2029 				    !skb->len && !state->split &&
2030 				    cb->min_dump_alloc < 4096) {
2031 					cb->min_dump_alloc = 4096;
2032 					state->split_start = 0;
2033 					rtnl_unlock();
2034 					return 1;
2035 				}
2036 				idx--;
2037 				break;
2038 			}
2039 		} while (state->split_start > 0);
2040 		break;
2041 	}
2042 	rtnl_unlock();
2043 
2044 	state->start = idx;
2045 
2046 	return skb->len;
2047 }
2048 
2049 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2050 {
2051 	kfree((void *)cb->args[0]);
2052 	return 0;
2053 }
2054 
2055 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2056 {
2057 	struct sk_buff *msg;
2058 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2059 	struct nl80211_dump_wiphy_state state = {};
2060 
2061 	msg = nlmsg_new(4096, GFP_KERNEL);
2062 	if (!msg)
2063 		return -ENOMEM;
2064 
2065 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2066 			       info->snd_portid, info->snd_seq, 0,
2067 			       &state) < 0) {
2068 		nlmsg_free(msg);
2069 		return -ENOBUFS;
2070 	}
2071 
2072 	return genlmsg_reply(msg, info);
2073 }
2074 
2075 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2076 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2077 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2078 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2079 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2080 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2081 };
2082 
2083 static int parse_txq_params(struct nlattr *tb[],
2084 			    struct ieee80211_txq_params *txq_params)
2085 {
2086 	u8 ac;
2087 
2088 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2089 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2090 	    !tb[NL80211_TXQ_ATTR_AIFS])
2091 		return -EINVAL;
2092 
2093 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2094 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2095 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2096 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2097 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2098 
2099 	if (ac >= NL80211_NUM_ACS)
2100 		return -EINVAL;
2101 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2102 	return 0;
2103 }
2104 
2105 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2106 {
2107 	/*
2108 	 * You can only set the channel explicitly for WDS interfaces,
2109 	 * all others have their channel managed via their respective
2110 	 * "establish a connection" command (connect, join, ...)
2111 	 *
2112 	 * For AP/GO and mesh mode, the channel can be set with the
2113 	 * channel userspace API, but is only stored and passed to the
2114 	 * low-level driver when the AP starts or the mesh is joined.
2115 	 * This is for backward compatibility, userspace can also give
2116 	 * the channel in the start-ap or join-mesh commands instead.
2117 	 *
2118 	 * Monitors are special as they are normally slaved to
2119 	 * whatever else is going on, so they have their own special
2120 	 * operation to set the monitor channel if possible.
2121 	 */
2122 	return !wdev ||
2123 		wdev->iftype == NL80211_IFTYPE_AP ||
2124 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2125 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2126 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2127 }
2128 
2129 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2130 				 struct genl_info *info,
2131 				 struct cfg80211_chan_def *chandef)
2132 {
2133 	u32 control_freq;
2134 
2135 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
2136 		return -EINVAL;
2137 
2138 	control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
2139 
2140 	chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2141 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2142 	chandef->center_freq1 = control_freq;
2143 	chandef->center_freq2 = 0;
2144 
2145 	/* Primary channel not allowed */
2146 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
2147 		return -EINVAL;
2148 
2149 	if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2150 		enum nl80211_channel_type chantype;
2151 
2152 		chantype = nla_get_u32(
2153 				info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2154 
2155 		switch (chantype) {
2156 		case NL80211_CHAN_NO_HT:
2157 		case NL80211_CHAN_HT20:
2158 		case NL80211_CHAN_HT40PLUS:
2159 		case NL80211_CHAN_HT40MINUS:
2160 			cfg80211_chandef_create(chandef, chandef->chan,
2161 						chantype);
2162 			/* user input for center_freq is incorrect */
2163 			if (info->attrs[NL80211_ATTR_CENTER_FREQ1] &&
2164 			    chandef->center_freq1 != nla_get_u32(
2165 					info->attrs[NL80211_ATTR_CENTER_FREQ1]))
2166 				return -EINVAL;
2167 			/* center_freq2 must be zero */
2168 			if (info->attrs[NL80211_ATTR_CENTER_FREQ2] &&
2169 			    nla_get_u32(info->attrs[NL80211_ATTR_CENTER_FREQ2]))
2170 				return -EINVAL;
2171 			break;
2172 		default:
2173 			return -EINVAL;
2174 		}
2175 	} else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2176 		chandef->width =
2177 			nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2178 		if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
2179 			chandef->center_freq1 =
2180 				nla_get_u32(
2181 					info->attrs[NL80211_ATTR_CENTER_FREQ1]);
2182 		if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
2183 			chandef->center_freq2 =
2184 				nla_get_u32(
2185 					info->attrs[NL80211_ATTR_CENTER_FREQ2]);
2186 	}
2187 
2188 	if (!cfg80211_chandef_valid(chandef))
2189 		return -EINVAL;
2190 
2191 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2192 				     IEEE80211_CHAN_DISABLED))
2193 		return -EINVAL;
2194 
2195 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2196 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2197 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
2198 		return -EINVAL;
2199 
2200 	return 0;
2201 }
2202 
2203 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2204 				 struct net_device *dev,
2205 				 struct genl_info *info)
2206 {
2207 	struct cfg80211_chan_def chandef;
2208 	int result;
2209 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2210 	struct wireless_dev *wdev = NULL;
2211 
2212 	if (dev)
2213 		wdev = dev->ieee80211_ptr;
2214 	if (!nl80211_can_set_dev_channel(wdev))
2215 		return -EOPNOTSUPP;
2216 	if (wdev)
2217 		iftype = wdev->iftype;
2218 
2219 	result = nl80211_parse_chandef(rdev, info, &chandef);
2220 	if (result)
2221 		return result;
2222 
2223 	switch (iftype) {
2224 	case NL80211_IFTYPE_AP:
2225 	case NL80211_IFTYPE_P2P_GO:
2226 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2227 						   iftype)) {
2228 			result = -EINVAL;
2229 			break;
2230 		}
2231 		if (wdev->beacon_interval) {
2232 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2233 			    !(rdev->wiphy.features &
2234 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2235 				result = -EBUSY;
2236 				break;
2237 			}
2238 
2239 			/* Only allow dynamic channel width changes */
2240 			if (chandef.chan != wdev->preset_chandef.chan) {
2241 				result = -EBUSY;
2242 				break;
2243 			}
2244 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2245 			if (result)
2246 				break;
2247 		}
2248 		wdev->preset_chandef = chandef;
2249 		result = 0;
2250 		break;
2251 	case NL80211_IFTYPE_MESH_POINT:
2252 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2253 		break;
2254 	case NL80211_IFTYPE_MONITOR:
2255 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2256 		break;
2257 	default:
2258 		result = -EINVAL;
2259 	}
2260 
2261 	return result;
2262 }
2263 
2264 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2265 {
2266 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2267 	struct net_device *netdev = info->user_ptr[1];
2268 
2269 	return __nl80211_set_channel(rdev, netdev, info);
2270 }
2271 
2272 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2273 {
2274 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2275 	struct net_device *dev = info->user_ptr[1];
2276 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2277 	const u8 *bssid;
2278 
2279 	if (!info->attrs[NL80211_ATTR_MAC])
2280 		return -EINVAL;
2281 
2282 	if (netif_running(dev))
2283 		return -EBUSY;
2284 
2285 	if (!rdev->ops->set_wds_peer)
2286 		return -EOPNOTSUPP;
2287 
2288 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2289 		return -EOPNOTSUPP;
2290 
2291 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2292 	return rdev_set_wds_peer(rdev, dev, bssid);
2293 }
2294 
2295 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2296 {
2297 	struct cfg80211_registered_device *rdev;
2298 	struct net_device *netdev = NULL;
2299 	struct wireless_dev *wdev;
2300 	int result = 0, rem_txq_params = 0;
2301 	struct nlattr *nl_txq_params;
2302 	u32 changed;
2303 	u8 retry_short = 0, retry_long = 0;
2304 	u32 frag_threshold = 0, rts_threshold = 0;
2305 	u8 coverage_class = 0;
2306 
2307 	ASSERT_RTNL();
2308 
2309 	/*
2310 	 * Try to find the wiphy and netdev. Normally this
2311 	 * function shouldn't need the netdev, but this is
2312 	 * done for backward compatibility -- previously
2313 	 * setting the channel was done per wiphy, but now
2314 	 * it is per netdev. Previous userland like hostapd
2315 	 * also passed a netdev to set_wiphy, so that it is
2316 	 * possible to let that go to the right netdev!
2317 	 */
2318 
2319 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
2320 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2321 
2322 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2323 		if (netdev && netdev->ieee80211_ptr)
2324 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2325 		else
2326 			netdev = NULL;
2327 	}
2328 
2329 	if (!netdev) {
2330 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2331 						  info->attrs);
2332 		if (IS_ERR(rdev))
2333 			return PTR_ERR(rdev);
2334 		wdev = NULL;
2335 		netdev = NULL;
2336 		result = 0;
2337 	} else
2338 		wdev = netdev->ieee80211_ptr;
2339 
2340 	/*
2341 	 * end workaround code, by now the rdev is available
2342 	 * and locked, and wdev may or may not be NULL.
2343 	 */
2344 
2345 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2346 		result = cfg80211_dev_rename(
2347 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2348 
2349 	if (result)
2350 		return result;
2351 
2352 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2353 		struct ieee80211_txq_params txq_params;
2354 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2355 
2356 		if (!rdev->ops->set_txq_params)
2357 			return -EOPNOTSUPP;
2358 
2359 		if (!netdev)
2360 			return -EINVAL;
2361 
2362 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2363 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2364 			return -EINVAL;
2365 
2366 		if (!netif_running(netdev))
2367 			return -ENETDOWN;
2368 
2369 		nla_for_each_nested(nl_txq_params,
2370 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2371 				    rem_txq_params) {
2372 			result = nla_parse_nested(tb, NL80211_TXQ_ATTR_MAX,
2373 						  nl_txq_params,
2374 						  txq_params_policy,
2375 						  info->extack);
2376 			if (result)
2377 				return result;
2378 			result = parse_txq_params(tb, &txq_params);
2379 			if (result)
2380 				return result;
2381 
2382 			result = rdev_set_txq_params(rdev, netdev,
2383 						     &txq_params);
2384 			if (result)
2385 				return result;
2386 		}
2387 	}
2388 
2389 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2390 		result = __nl80211_set_channel(
2391 			rdev,
2392 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2393 			info);
2394 		if (result)
2395 			return result;
2396 	}
2397 
2398 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2399 		struct wireless_dev *txp_wdev = wdev;
2400 		enum nl80211_tx_power_setting type;
2401 		int idx, mbm = 0;
2402 
2403 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2404 			txp_wdev = NULL;
2405 
2406 		if (!rdev->ops->set_tx_power)
2407 			return -EOPNOTSUPP;
2408 
2409 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2410 		type = nla_get_u32(info->attrs[idx]);
2411 
2412 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2413 		    (type != NL80211_TX_POWER_AUTOMATIC))
2414 			return -EINVAL;
2415 
2416 		if (type != NL80211_TX_POWER_AUTOMATIC) {
2417 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2418 			mbm = nla_get_u32(info->attrs[idx]);
2419 		}
2420 
2421 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2422 		if (result)
2423 			return result;
2424 	}
2425 
2426 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2427 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2428 		u32 tx_ant, rx_ant;
2429 
2430 		if ((!rdev->wiphy.available_antennas_tx &&
2431 		     !rdev->wiphy.available_antennas_rx) ||
2432 		    !rdev->ops->set_antenna)
2433 			return -EOPNOTSUPP;
2434 
2435 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2436 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2437 
2438 		/* reject antenna configurations which don't match the
2439 		 * available antenna masks, except for the "all" mask */
2440 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2441 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2442 			return -EINVAL;
2443 
2444 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2445 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2446 
2447 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2448 		if (result)
2449 			return result;
2450 	}
2451 
2452 	changed = 0;
2453 
2454 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2455 		retry_short = nla_get_u8(
2456 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2457 		if (retry_short == 0)
2458 			return -EINVAL;
2459 
2460 		changed |= WIPHY_PARAM_RETRY_SHORT;
2461 	}
2462 
2463 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2464 		retry_long = nla_get_u8(
2465 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2466 		if (retry_long == 0)
2467 			return -EINVAL;
2468 
2469 		changed |= WIPHY_PARAM_RETRY_LONG;
2470 	}
2471 
2472 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2473 		frag_threshold = nla_get_u32(
2474 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2475 		if (frag_threshold < 256)
2476 			return -EINVAL;
2477 
2478 		if (frag_threshold != (u32) -1) {
2479 			/*
2480 			 * Fragments (apart from the last one) are required to
2481 			 * have even length. Make the fragmentation code
2482 			 * simpler by stripping LSB should someone try to use
2483 			 * odd threshold value.
2484 			 */
2485 			frag_threshold &= ~0x1;
2486 		}
2487 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2488 	}
2489 
2490 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2491 		rts_threshold = nla_get_u32(
2492 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2493 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
2494 	}
2495 
2496 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2497 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2498 			return -EINVAL;
2499 
2500 		coverage_class = nla_get_u8(
2501 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2502 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
2503 	}
2504 
2505 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2506 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2507 			return -EOPNOTSUPP;
2508 
2509 		changed |= WIPHY_PARAM_DYN_ACK;
2510 	}
2511 
2512 	if (changed) {
2513 		u8 old_retry_short, old_retry_long;
2514 		u32 old_frag_threshold, old_rts_threshold;
2515 		u8 old_coverage_class;
2516 
2517 		if (!rdev->ops->set_wiphy_params)
2518 			return -EOPNOTSUPP;
2519 
2520 		old_retry_short = rdev->wiphy.retry_short;
2521 		old_retry_long = rdev->wiphy.retry_long;
2522 		old_frag_threshold = rdev->wiphy.frag_threshold;
2523 		old_rts_threshold = rdev->wiphy.rts_threshold;
2524 		old_coverage_class = rdev->wiphy.coverage_class;
2525 
2526 		if (changed & WIPHY_PARAM_RETRY_SHORT)
2527 			rdev->wiphy.retry_short = retry_short;
2528 		if (changed & WIPHY_PARAM_RETRY_LONG)
2529 			rdev->wiphy.retry_long = retry_long;
2530 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2531 			rdev->wiphy.frag_threshold = frag_threshold;
2532 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2533 			rdev->wiphy.rts_threshold = rts_threshold;
2534 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2535 			rdev->wiphy.coverage_class = coverage_class;
2536 
2537 		result = rdev_set_wiphy_params(rdev, changed);
2538 		if (result) {
2539 			rdev->wiphy.retry_short = old_retry_short;
2540 			rdev->wiphy.retry_long = old_retry_long;
2541 			rdev->wiphy.frag_threshold = old_frag_threshold;
2542 			rdev->wiphy.rts_threshold = old_rts_threshold;
2543 			rdev->wiphy.coverage_class = old_coverage_class;
2544 			return result;
2545 		}
2546 	}
2547 	return 0;
2548 }
2549 
2550 static inline u64 wdev_id(struct wireless_dev *wdev)
2551 {
2552 	return (u64)wdev->identifier |
2553 	       ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2554 }
2555 
2556 static int nl80211_send_chandef(struct sk_buff *msg,
2557 				const struct cfg80211_chan_def *chandef)
2558 {
2559 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2560 		return -EINVAL;
2561 
2562 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2563 			chandef->chan->center_freq))
2564 		return -ENOBUFS;
2565 	switch (chandef->width) {
2566 	case NL80211_CHAN_WIDTH_20_NOHT:
2567 	case NL80211_CHAN_WIDTH_20:
2568 	case NL80211_CHAN_WIDTH_40:
2569 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2570 				cfg80211_get_chandef_type(chandef)))
2571 			return -ENOBUFS;
2572 		break;
2573 	default:
2574 		break;
2575 	}
2576 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2577 		return -ENOBUFS;
2578 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2579 		return -ENOBUFS;
2580 	if (chandef->center_freq2 &&
2581 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2582 		return -ENOBUFS;
2583 	return 0;
2584 }
2585 
2586 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2587 			      struct cfg80211_registered_device *rdev,
2588 			      struct wireless_dev *wdev, bool removal)
2589 {
2590 	struct net_device *dev = wdev->netdev;
2591 	u8 cmd = NL80211_CMD_NEW_INTERFACE;
2592 	void *hdr;
2593 
2594 	if (removal)
2595 		cmd = NL80211_CMD_DEL_INTERFACE;
2596 
2597 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2598 	if (!hdr)
2599 		return -1;
2600 
2601 	if (dev &&
2602 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2603 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2604 		goto nla_put_failure;
2605 
2606 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2607 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2608 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
2609 			      NL80211_ATTR_PAD) ||
2610 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2611 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2612 			rdev->devlist_generation ^
2613 			(cfg80211_rdev_list_generation << 2)))
2614 		goto nla_put_failure;
2615 
2616 	if (rdev->ops->get_channel) {
2617 		int ret;
2618 		struct cfg80211_chan_def chandef;
2619 
2620 		ret = rdev_get_channel(rdev, wdev, &chandef);
2621 		if (ret == 0) {
2622 			if (nl80211_send_chandef(msg, &chandef))
2623 				goto nla_put_failure;
2624 		}
2625 	}
2626 
2627 	if (rdev->ops->get_tx_power) {
2628 		int dbm, ret;
2629 
2630 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
2631 		if (ret == 0 &&
2632 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2633 				DBM_TO_MBM(dbm)))
2634 			goto nla_put_failure;
2635 	}
2636 
2637 	wdev_lock(wdev);
2638 	switch (wdev->iftype) {
2639 	case NL80211_IFTYPE_AP:
2640 		if (wdev->ssid_len &&
2641 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2642 			goto nla_put_failure_locked;
2643 		break;
2644 	case NL80211_IFTYPE_STATION:
2645 	case NL80211_IFTYPE_P2P_CLIENT:
2646 	case NL80211_IFTYPE_ADHOC: {
2647 		const u8 *ssid_ie;
2648 		if (!wdev->current_bss)
2649 			break;
2650 		rcu_read_lock();
2651 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
2652 					       WLAN_EID_SSID);
2653 		if (ssid_ie &&
2654 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
2655 			goto nla_put_failure_rcu_locked;
2656 		rcu_read_unlock();
2657 		break;
2658 		}
2659 	default:
2660 		/* nothing */
2661 		break;
2662 	}
2663 	wdev_unlock(wdev);
2664 
2665 	genlmsg_end(msg, hdr);
2666 	return 0;
2667 
2668  nla_put_failure_rcu_locked:
2669 	rcu_read_unlock();
2670  nla_put_failure_locked:
2671 	wdev_unlock(wdev);
2672  nla_put_failure:
2673 	genlmsg_cancel(msg, hdr);
2674 	return -EMSGSIZE;
2675 }
2676 
2677 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2678 {
2679 	int wp_idx = 0;
2680 	int if_idx = 0;
2681 	int wp_start = cb->args[0];
2682 	int if_start = cb->args[1];
2683 	int filter_wiphy = -1;
2684 	struct cfg80211_registered_device *rdev;
2685 	struct wireless_dev *wdev;
2686 	int ret;
2687 
2688 	rtnl_lock();
2689 	if (!cb->args[2]) {
2690 		struct nl80211_dump_wiphy_state state = {
2691 			.filter_wiphy = -1,
2692 		};
2693 
2694 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
2695 		if (ret)
2696 			goto out_unlock;
2697 
2698 		filter_wiphy = state.filter_wiphy;
2699 
2700 		/*
2701 		 * if filtering, set cb->args[2] to +1 since 0 is the default
2702 		 * value needed to determine that parsing is necessary.
2703 		 */
2704 		if (filter_wiphy >= 0)
2705 			cb->args[2] = filter_wiphy + 1;
2706 		else
2707 			cb->args[2] = -1;
2708 	} else if (cb->args[2] > 0) {
2709 		filter_wiphy = cb->args[2] - 1;
2710 	}
2711 
2712 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2713 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2714 			continue;
2715 		if (wp_idx < wp_start) {
2716 			wp_idx++;
2717 			continue;
2718 		}
2719 
2720 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
2721 			continue;
2722 
2723 		if_idx = 0;
2724 
2725 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
2726 			if (if_idx < if_start) {
2727 				if_idx++;
2728 				continue;
2729 			}
2730 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2731 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
2732 					       rdev, wdev, false) < 0) {
2733 				goto out;
2734 			}
2735 			if_idx++;
2736 		}
2737 
2738 		wp_idx++;
2739 	}
2740  out:
2741 	cb->args[0] = wp_idx;
2742 	cb->args[1] = if_idx;
2743 
2744 	ret = skb->len;
2745  out_unlock:
2746 	rtnl_unlock();
2747 
2748 	return ret;
2749 }
2750 
2751 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2752 {
2753 	struct sk_buff *msg;
2754 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2755 	struct wireless_dev *wdev = info->user_ptr[1];
2756 
2757 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2758 	if (!msg)
2759 		return -ENOMEM;
2760 
2761 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2762 			       rdev, wdev, false) < 0) {
2763 		nlmsg_free(msg);
2764 		return -ENOBUFS;
2765 	}
2766 
2767 	return genlmsg_reply(msg, info);
2768 }
2769 
2770 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2771 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2772 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2773 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2774 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2775 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2776 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2777 };
2778 
2779 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2780 {
2781 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2782 	int flag;
2783 
2784 	*mntrflags = 0;
2785 
2786 	if (!nla)
2787 		return -EINVAL;
2788 
2789 	if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, nla,
2790 			     mntr_flags_policy, NULL))
2791 		return -EINVAL;
2792 
2793 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2794 		if (flags[flag])
2795 			*mntrflags |= (1<<flag);
2796 
2797 	*mntrflags |= MONITOR_FLAG_CHANGED;
2798 
2799 	return 0;
2800 }
2801 
2802 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
2803 				     enum nl80211_iftype type,
2804 				     struct genl_info *info,
2805 				     struct vif_params *params)
2806 {
2807 	bool change = false;
2808 	int err;
2809 
2810 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2811 		if (type != NL80211_IFTYPE_MONITOR)
2812 			return -EINVAL;
2813 
2814 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2815 					  &params->flags);
2816 		if (err)
2817 			return err;
2818 
2819 		change = true;
2820 	}
2821 
2822 	if (params->flags & MONITOR_FLAG_ACTIVE &&
2823 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2824 		return -EOPNOTSUPP;
2825 
2826 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
2827 		const u8 *mumimo_groups;
2828 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2829 
2830 		if (type != NL80211_IFTYPE_MONITOR)
2831 			return -EINVAL;
2832 
2833 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2834 			return -EOPNOTSUPP;
2835 
2836 		mumimo_groups =
2837 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
2838 
2839 		/* bits 0 and 63 are reserved and must be zero */
2840 		if ((mumimo_groups[0] & BIT(0)) ||
2841 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
2842 			return -EINVAL;
2843 
2844 		params->vht_mumimo_groups = mumimo_groups;
2845 		change = true;
2846 	}
2847 
2848 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
2849 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2850 
2851 		if (type != NL80211_IFTYPE_MONITOR)
2852 			return -EINVAL;
2853 
2854 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2855 			return -EOPNOTSUPP;
2856 
2857 		params->vht_mumimo_follow_addr =
2858 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
2859 		change = true;
2860 	}
2861 
2862 	return change ? 1 : 0;
2863 }
2864 
2865 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2866 			       struct net_device *netdev, u8 use_4addr,
2867 			       enum nl80211_iftype iftype)
2868 {
2869 	if (!use_4addr) {
2870 		if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2871 			return -EBUSY;
2872 		return 0;
2873 	}
2874 
2875 	switch (iftype) {
2876 	case NL80211_IFTYPE_AP_VLAN:
2877 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2878 			return 0;
2879 		break;
2880 	case NL80211_IFTYPE_STATION:
2881 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2882 			return 0;
2883 		break;
2884 	default:
2885 		break;
2886 	}
2887 
2888 	return -EOPNOTSUPP;
2889 }
2890 
2891 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2892 {
2893 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2894 	struct vif_params params;
2895 	int err;
2896 	enum nl80211_iftype otype, ntype;
2897 	struct net_device *dev = info->user_ptr[1];
2898 	bool change = false;
2899 
2900 	memset(&params, 0, sizeof(params));
2901 
2902 	otype = ntype = dev->ieee80211_ptr->iftype;
2903 
2904 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
2905 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2906 		if (otype != ntype)
2907 			change = true;
2908 		if (ntype > NL80211_IFTYPE_MAX)
2909 			return -EINVAL;
2910 	}
2911 
2912 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
2913 		struct wireless_dev *wdev = dev->ieee80211_ptr;
2914 
2915 		if (ntype != NL80211_IFTYPE_MESH_POINT)
2916 			return -EINVAL;
2917 		if (netif_running(dev))
2918 			return -EBUSY;
2919 
2920 		wdev_lock(wdev);
2921 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2922 			     IEEE80211_MAX_MESH_ID_LEN);
2923 		wdev->mesh_id_up_len =
2924 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2925 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2926 		       wdev->mesh_id_up_len);
2927 		wdev_unlock(wdev);
2928 	}
2929 
2930 	if (info->attrs[NL80211_ATTR_4ADDR]) {
2931 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2932 		change = true;
2933 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2934 		if (err)
2935 			return err;
2936 	} else {
2937 		params.use_4addr = -1;
2938 	}
2939 
2940 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
2941 	if (err < 0)
2942 		return err;
2943 	if (err > 0)
2944 		change = true;
2945 
2946 	if (change)
2947 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
2948 	else
2949 		err = 0;
2950 
2951 	if (!err && params.use_4addr != -1)
2952 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
2953 
2954 	return err;
2955 }
2956 
2957 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2958 {
2959 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2960 	struct vif_params params;
2961 	struct wireless_dev *wdev;
2962 	struct sk_buff *msg;
2963 	int err;
2964 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2965 
2966 	/* to avoid failing a new interface creation due to pending removal */
2967 	cfg80211_destroy_ifaces(rdev);
2968 
2969 	memset(&params, 0, sizeof(params));
2970 
2971 	if (!info->attrs[NL80211_ATTR_IFNAME])
2972 		return -EINVAL;
2973 
2974 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
2975 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2976 		if (type > NL80211_IFTYPE_MAX)
2977 			return -EINVAL;
2978 	}
2979 
2980 	if (!rdev->ops->add_virtual_intf ||
2981 	    !(rdev->wiphy.interface_modes & (1 << type)))
2982 		return -EOPNOTSUPP;
2983 
2984 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
2985 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2986 	    info->attrs[NL80211_ATTR_MAC]) {
2987 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2988 			   ETH_ALEN);
2989 		if (!is_valid_ether_addr(params.macaddr))
2990 			return -EADDRNOTAVAIL;
2991 	}
2992 
2993 	if (info->attrs[NL80211_ATTR_4ADDR]) {
2994 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2995 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2996 		if (err)
2997 			return err;
2998 	}
2999 
3000 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3001 	if (err < 0)
3002 		return err;
3003 
3004 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3005 	if (!msg)
3006 		return -ENOMEM;
3007 
3008 	wdev = rdev_add_virtual_intf(rdev,
3009 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3010 				NET_NAME_USER, type, &params);
3011 	if (WARN_ON(!wdev)) {
3012 		nlmsg_free(msg);
3013 		return -EPROTO;
3014 	} else if (IS_ERR(wdev)) {
3015 		nlmsg_free(msg);
3016 		return PTR_ERR(wdev);
3017 	}
3018 
3019 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3020 		wdev->owner_nlportid = info->snd_portid;
3021 
3022 	switch (type) {
3023 	case NL80211_IFTYPE_MESH_POINT:
3024 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3025 			break;
3026 		wdev_lock(wdev);
3027 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3028 			     IEEE80211_MAX_MESH_ID_LEN);
3029 		wdev->mesh_id_up_len =
3030 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3031 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3032 		       wdev->mesh_id_up_len);
3033 		wdev_unlock(wdev);
3034 		break;
3035 	case NL80211_IFTYPE_NAN:
3036 	case NL80211_IFTYPE_P2P_DEVICE:
3037 		/*
3038 		 * P2P Device and NAN do not have a netdev, so don't go
3039 		 * through the netdev notifier and must be added here
3040 		 */
3041 		mutex_init(&wdev->mtx);
3042 		INIT_LIST_HEAD(&wdev->event_list);
3043 		spin_lock_init(&wdev->event_lock);
3044 		INIT_LIST_HEAD(&wdev->mgmt_registrations);
3045 		spin_lock_init(&wdev->mgmt_registrations_lock);
3046 
3047 		wdev->identifier = ++rdev->wdev_id;
3048 		list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
3049 		rdev->devlist_generation++;
3050 		break;
3051 	default:
3052 		break;
3053 	}
3054 
3055 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3056 			       rdev, wdev, false) < 0) {
3057 		nlmsg_free(msg);
3058 		return -ENOBUFS;
3059 	}
3060 
3061 	/*
3062 	 * For wdevs which have no associated netdev object (e.g. of type
3063 	 * NL80211_IFTYPE_P2P_DEVICE), emit the NEW_INTERFACE event here.
3064 	 * For all other types, the event will be generated from the
3065 	 * netdev notifier
3066 	 */
3067 	if (!wdev->netdev)
3068 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
3069 
3070 	return genlmsg_reply(msg, info);
3071 }
3072 
3073 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3074 {
3075 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3076 	struct wireless_dev *wdev = info->user_ptr[1];
3077 
3078 	if (!rdev->ops->del_virtual_intf)
3079 		return -EOPNOTSUPP;
3080 
3081 	/*
3082 	 * If we remove a wireless device without a netdev then clear
3083 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3084 	 * to check if it needs to do dev_put(). Otherwise it crashes
3085 	 * since the wdev has been freed, unlike with a netdev where
3086 	 * we need the dev_put() for the netdev to really be freed.
3087 	 */
3088 	if (!wdev->netdev)
3089 		info->user_ptr[1] = NULL;
3090 
3091 	return rdev_del_virtual_intf(rdev, wdev);
3092 }
3093 
3094 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3095 {
3096 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3097 	struct net_device *dev = info->user_ptr[1];
3098 	u16 noack_map;
3099 
3100 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3101 		return -EINVAL;
3102 
3103 	if (!rdev->ops->set_noack_map)
3104 		return -EOPNOTSUPP;
3105 
3106 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3107 
3108 	return rdev_set_noack_map(rdev, dev, noack_map);
3109 }
3110 
3111 struct get_key_cookie {
3112 	struct sk_buff *msg;
3113 	int error;
3114 	int idx;
3115 };
3116 
3117 static void get_key_callback(void *c, struct key_params *params)
3118 {
3119 	struct nlattr *key;
3120 	struct get_key_cookie *cookie = c;
3121 
3122 	if ((params->key &&
3123 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3124 		     params->key_len, params->key)) ||
3125 	    (params->seq &&
3126 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3127 		     params->seq_len, params->seq)) ||
3128 	    (params->cipher &&
3129 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3130 			 params->cipher)))
3131 		goto nla_put_failure;
3132 
3133 	key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
3134 	if (!key)
3135 		goto nla_put_failure;
3136 
3137 	if ((params->key &&
3138 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3139 		     params->key_len, params->key)) ||
3140 	    (params->seq &&
3141 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3142 		     params->seq_len, params->seq)) ||
3143 	    (params->cipher &&
3144 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3145 			 params->cipher)))
3146 		goto nla_put_failure;
3147 
3148 	if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
3149 		goto nla_put_failure;
3150 
3151 	nla_nest_end(cookie->msg, key);
3152 
3153 	return;
3154  nla_put_failure:
3155 	cookie->error = 1;
3156 }
3157 
3158 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3159 {
3160 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3161 	int err;
3162 	struct net_device *dev = info->user_ptr[1];
3163 	u8 key_idx = 0;
3164 	const u8 *mac_addr = NULL;
3165 	bool pairwise;
3166 	struct get_key_cookie cookie = {
3167 		.error = 0,
3168 	};
3169 	void *hdr;
3170 	struct sk_buff *msg;
3171 
3172 	if (info->attrs[NL80211_ATTR_KEY_IDX])
3173 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3174 
3175 	if (key_idx > 5)
3176 		return -EINVAL;
3177 
3178 	if (info->attrs[NL80211_ATTR_MAC])
3179 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3180 
3181 	pairwise = !!mac_addr;
3182 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3183 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3184 
3185 		if (kt >= NUM_NL80211_KEYTYPES)
3186 			return -EINVAL;
3187 		if (kt != NL80211_KEYTYPE_GROUP &&
3188 		    kt != NL80211_KEYTYPE_PAIRWISE)
3189 			return -EINVAL;
3190 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3191 	}
3192 
3193 	if (!rdev->ops->get_key)
3194 		return -EOPNOTSUPP;
3195 
3196 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3197 		return -ENOENT;
3198 
3199 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3200 	if (!msg)
3201 		return -ENOMEM;
3202 
3203 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3204 			     NL80211_CMD_NEW_KEY);
3205 	if (!hdr)
3206 		goto nla_put_failure;
3207 
3208 	cookie.msg = msg;
3209 	cookie.idx = key_idx;
3210 
3211 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3212 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3213 		goto nla_put_failure;
3214 	if (mac_addr &&
3215 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3216 		goto nla_put_failure;
3217 
3218 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3219 			   get_key_callback);
3220 
3221 	if (err)
3222 		goto free_msg;
3223 
3224 	if (cookie.error)
3225 		goto nla_put_failure;
3226 
3227 	genlmsg_end(msg, hdr);
3228 	return genlmsg_reply(msg, info);
3229 
3230  nla_put_failure:
3231 	err = -ENOBUFS;
3232  free_msg:
3233 	nlmsg_free(msg);
3234 	return err;
3235 }
3236 
3237 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3238 {
3239 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3240 	struct key_parse key;
3241 	int err;
3242 	struct net_device *dev = info->user_ptr[1];
3243 
3244 	err = nl80211_parse_key(info, &key);
3245 	if (err)
3246 		return err;
3247 
3248 	if (key.idx < 0)
3249 		return -EINVAL;
3250 
3251 	/* only support setting default key */
3252 	if (!key.def && !key.defmgmt)
3253 		return -EINVAL;
3254 
3255 	wdev_lock(dev->ieee80211_ptr);
3256 
3257 	if (key.def) {
3258 		if (!rdev->ops->set_default_key) {
3259 			err = -EOPNOTSUPP;
3260 			goto out;
3261 		}
3262 
3263 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3264 		if (err)
3265 			goto out;
3266 
3267 		err = rdev_set_default_key(rdev, dev, key.idx,
3268 						 key.def_uni, key.def_multi);
3269 
3270 		if (err)
3271 			goto out;
3272 
3273 #ifdef CONFIG_CFG80211_WEXT
3274 		dev->ieee80211_ptr->wext.default_key = key.idx;
3275 #endif
3276 	} else {
3277 		if (key.def_uni || !key.def_multi) {
3278 			err = -EINVAL;
3279 			goto out;
3280 		}
3281 
3282 		if (!rdev->ops->set_default_mgmt_key) {
3283 			err = -EOPNOTSUPP;
3284 			goto out;
3285 		}
3286 
3287 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3288 		if (err)
3289 			goto out;
3290 
3291 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3292 		if (err)
3293 			goto out;
3294 
3295 #ifdef CONFIG_CFG80211_WEXT
3296 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3297 #endif
3298 	}
3299 
3300  out:
3301 	wdev_unlock(dev->ieee80211_ptr);
3302 
3303 	return err;
3304 }
3305 
3306 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3307 {
3308 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3309 	int err;
3310 	struct net_device *dev = info->user_ptr[1];
3311 	struct key_parse key;
3312 	const u8 *mac_addr = NULL;
3313 
3314 	err = nl80211_parse_key(info, &key);
3315 	if (err)
3316 		return err;
3317 
3318 	if (!key.p.key)
3319 		return -EINVAL;
3320 
3321 	if (info->attrs[NL80211_ATTR_MAC])
3322 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3323 
3324 	if (key.type == -1) {
3325 		if (mac_addr)
3326 			key.type = NL80211_KEYTYPE_PAIRWISE;
3327 		else
3328 			key.type = NL80211_KEYTYPE_GROUP;
3329 	}
3330 
3331 	/* for now */
3332 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3333 	    key.type != NL80211_KEYTYPE_GROUP)
3334 		return -EINVAL;
3335 
3336 	if (!rdev->ops->add_key)
3337 		return -EOPNOTSUPP;
3338 
3339 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3340 					   key.type == NL80211_KEYTYPE_PAIRWISE,
3341 					   mac_addr))
3342 		return -EINVAL;
3343 
3344 	wdev_lock(dev->ieee80211_ptr);
3345 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3346 	if (!err)
3347 		err = rdev_add_key(rdev, dev, key.idx,
3348 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3349 				    mac_addr, &key.p);
3350 	wdev_unlock(dev->ieee80211_ptr);
3351 
3352 	return err;
3353 }
3354 
3355 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3356 {
3357 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3358 	int err;
3359 	struct net_device *dev = info->user_ptr[1];
3360 	u8 *mac_addr = NULL;
3361 	struct key_parse key;
3362 
3363 	err = nl80211_parse_key(info, &key);
3364 	if (err)
3365 		return err;
3366 
3367 	if (info->attrs[NL80211_ATTR_MAC])
3368 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3369 
3370 	if (key.type == -1) {
3371 		if (mac_addr)
3372 			key.type = NL80211_KEYTYPE_PAIRWISE;
3373 		else
3374 			key.type = NL80211_KEYTYPE_GROUP;
3375 	}
3376 
3377 	/* for now */
3378 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3379 	    key.type != NL80211_KEYTYPE_GROUP)
3380 		return -EINVAL;
3381 
3382 	if (!rdev->ops->del_key)
3383 		return -EOPNOTSUPP;
3384 
3385 	wdev_lock(dev->ieee80211_ptr);
3386 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3387 
3388 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3389 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3390 		err = -ENOENT;
3391 
3392 	if (!err)
3393 		err = rdev_del_key(rdev, dev, key.idx,
3394 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3395 				   mac_addr);
3396 
3397 #ifdef CONFIG_CFG80211_WEXT
3398 	if (!err) {
3399 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
3400 			dev->ieee80211_ptr->wext.default_key = -1;
3401 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3402 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3403 	}
3404 #endif
3405 	wdev_unlock(dev->ieee80211_ptr);
3406 
3407 	return err;
3408 }
3409 
3410 /* This function returns an error or the number of nested attributes */
3411 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3412 {
3413 	struct nlattr *attr;
3414 	int n_entries = 0, tmp;
3415 
3416 	nla_for_each_nested(attr, nl_attr, tmp) {
3417 		if (nla_len(attr) != ETH_ALEN)
3418 			return -EINVAL;
3419 
3420 		n_entries++;
3421 	}
3422 
3423 	return n_entries;
3424 }
3425 
3426 /*
3427  * This function parses ACL information and allocates memory for ACL data.
3428  * On successful return, the calling function is responsible to free the
3429  * ACL buffer returned by this function.
3430  */
3431 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3432 						struct genl_info *info)
3433 {
3434 	enum nl80211_acl_policy acl_policy;
3435 	struct nlattr *attr;
3436 	struct cfg80211_acl_data *acl;
3437 	int i = 0, n_entries, tmp;
3438 
3439 	if (!wiphy->max_acl_mac_addrs)
3440 		return ERR_PTR(-EOPNOTSUPP);
3441 
3442 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3443 		return ERR_PTR(-EINVAL);
3444 
3445 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3446 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3447 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3448 		return ERR_PTR(-EINVAL);
3449 
3450 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3451 		return ERR_PTR(-EINVAL);
3452 
3453 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3454 	if (n_entries < 0)
3455 		return ERR_PTR(n_entries);
3456 
3457 	if (n_entries > wiphy->max_acl_mac_addrs)
3458 		return ERR_PTR(-ENOTSUPP);
3459 
3460 	acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3461 		      GFP_KERNEL);
3462 	if (!acl)
3463 		return ERR_PTR(-ENOMEM);
3464 
3465 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3466 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3467 		i++;
3468 	}
3469 
3470 	acl->n_acl_entries = n_entries;
3471 	acl->acl_policy = acl_policy;
3472 
3473 	return acl;
3474 }
3475 
3476 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3477 {
3478 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3479 	struct net_device *dev = info->user_ptr[1];
3480 	struct cfg80211_acl_data *acl;
3481 	int err;
3482 
3483 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3484 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3485 		return -EOPNOTSUPP;
3486 
3487 	if (!dev->ieee80211_ptr->beacon_interval)
3488 		return -EINVAL;
3489 
3490 	acl = parse_acl_data(&rdev->wiphy, info);
3491 	if (IS_ERR(acl))
3492 		return PTR_ERR(acl);
3493 
3494 	err = rdev_set_mac_acl(rdev, dev, acl);
3495 
3496 	kfree(acl);
3497 
3498 	return err;
3499 }
3500 
3501 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3502 			   u8 *rates, u8 rates_len)
3503 {
3504 	u8 i;
3505 	u32 mask = 0;
3506 
3507 	for (i = 0; i < rates_len; i++) {
3508 		int rate = (rates[i] & 0x7f) * 5;
3509 		int ridx;
3510 
3511 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
3512 			struct ieee80211_rate *srate =
3513 				&sband->bitrates[ridx];
3514 			if (rate == srate->bitrate) {
3515 				mask |= 1 << ridx;
3516 				break;
3517 			}
3518 		}
3519 		if (ridx == sband->n_bitrates)
3520 			return 0; /* rate not found */
3521 	}
3522 
3523 	return mask;
3524 }
3525 
3526 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
3527 			       u8 *rates, u8 rates_len,
3528 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
3529 {
3530 	u8 i;
3531 
3532 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
3533 
3534 	for (i = 0; i < rates_len; i++) {
3535 		int ridx, rbit;
3536 
3537 		ridx = rates[i] / 8;
3538 		rbit = BIT(rates[i] % 8);
3539 
3540 		/* check validity */
3541 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
3542 			return false;
3543 
3544 		/* check availability */
3545 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
3546 			mcs[ridx] |= rbit;
3547 		else
3548 			return false;
3549 	}
3550 
3551 	return true;
3552 }
3553 
3554 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
3555 {
3556 	u16 mcs_mask = 0;
3557 
3558 	switch (vht_mcs_map) {
3559 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
3560 		break;
3561 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
3562 		mcs_mask = 0x00FF;
3563 		break;
3564 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
3565 		mcs_mask = 0x01FF;
3566 		break;
3567 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
3568 		mcs_mask = 0x03FF;
3569 		break;
3570 	default:
3571 		break;
3572 	}
3573 
3574 	return mcs_mask;
3575 }
3576 
3577 static void vht_build_mcs_mask(u16 vht_mcs_map,
3578 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
3579 {
3580 	u8 nss;
3581 
3582 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
3583 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
3584 		vht_mcs_map >>= 2;
3585 	}
3586 }
3587 
3588 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
3589 			     struct nl80211_txrate_vht *txrate,
3590 			     u16 mcs[NL80211_VHT_NSS_MAX])
3591 {
3592 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3593 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
3594 	u8 i;
3595 
3596 	if (!sband->vht_cap.vht_supported)
3597 		return false;
3598 
3599 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
3600 
3601 	/* Build vht_mcs_mask from VHT capabilities */
3602 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
3603 
3604 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3605 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
3606 			mcs[i] = txrate->mcs[i];
3607 		else
3608 			return false;
3609 	}
3610 
3611 	return true;
3612 }
3613 
3614 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
3615 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
3616 				    .len = NL80211_MAX_SUPP_RATES },
3617 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
3618 				.len = NL80211_MAX_SUPP_HT_RATES },
3619 	[NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
3620 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
3621 };
3622 
3623 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
3624 					 struct cfg80211_bitrate_mask *mask)
3625 {
3626 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
3627 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3628 	int rem, i;
3629 	struct nlattr *tx_rates;
3630 	struct ieee80211_supported_band *sband;
3631 	u16 vht_tx_mcs_map;
3632 
3633 	memset(mask, 0, sizeof(*mask));
3634 	/* Default to all rates enabled */
3635 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
3636 		sband = rdev->wiphy.bands[i];
3637 
3638 		if (!sband)
3639 			continue;
3640 
3641 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
3642 		memcpy(mask->control[i].ht_mcs,
3643 		       sband->ht_cap.mcs.rx_mask,
3644 		       sizeof(mask->control[i].ht_mcs));
3645 
3646 		if (!sband->vht_cap.vht_supported)
3647 			continue;
3648 
3649 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3650 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
3651 	}
3652 
3653 	/* if no rates are given set it back to the defaults */
3654 	if (!info->attrs[NL80211_ATTR_TX_RATES])
3655 		goto out;
3656 
3657 	/* The nested attribute uses enum nl80211_band as the index. This maps
3658 	 * directly to the enum nl80211_band values used in cfg80211.
3659 	 */
3660 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
3661 	nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
3662 		enum nl80211_band band = nla_type(tx_rates);
3663 		int err;
3664 
3665 		if (band < 0 || band >= NUM_NL80211_BANDS)
3666 			return -EINVAL;
3667 		sband = rdev->wiphy.bands[band];
3668 		if (sband == NULL)
3669 			return -EINVAL;
3670 		err = nla_parse_nested(tb, NL80211_TXRATE_MAX, tx_rates,
3671 				       nl80211_txattr_policy, info->extack);
3672 		if (err)
3673 			return err;
3674 		if (tb[NL80211_TXRATE_LEGACY]) {
3675 			mask->control[band].legacy = rateset_to_mask(
3676 				sband,
3677 				nla_data(tb[NL80211_TXRATE_LEGACY]),
3678 				nla_len(tb[NL80211_TXRATE_LEGACY]));
3679 			if ((mask->control[band].legacy == 0) &&
3680 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
3681 				return -EINVAL;
3682 		}
3683 		if (tb[NL80211_TXRATE_HT]) {
3684 			if (!ht_rateset_to_mask(
3685 					sband,
3686 					nla_data(tb[NL80211_TXRATE_HT]),
3687 					nla_len(tb[NL80211_TXRATE_HT]),
3688 					mask->control[band].ht_mcs))
3689 				return -EINVAL;
3690 		}
3691 		if (tb[NL80211_TXRATE_VHT]) {
3692 			if (!vht_set_mcs_mask(
3693 					sband,
3694 					nla_data(tb[NL80211_TXRATE_VHT]),
3695 					mask->control[band].vht_mcs))
3696 				return -EINVAL;
3697 		}
3698 		if (tb[NL80211_TXRATE_GI]) {
3699 			mask->control[band].gi =
3700 				nla_get_u8(tb[NL80211_TXRATE_GI]);
3701 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
3702 				return -EINVAL;
3703 		}
3704 
3705 		if (mask->control[band].legacy == 0) {
3706 			/* don't allow empty legacy rates if HT or VHT
3707 			 * are not even supported.
3708 			 */
3709 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
3710 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
3711 				return -EINVAL;
3712 
3713 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3714 				if (mask->control[band].ht_mcs[i])
3715 					goto out;
3716 
3717 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3718 				if (mask->control[band].vht_mcs[i])
3719 					goto out;
3720 
3721 			/* legacy and mcs rates may not be both empty */
3722 			return -EINVAL;
3723 		}
3724 	}
3725 
3726 out:
3727 	return 0;
3728 }
3729 
3730 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
3731 				   enum nl80211_band band,
3732 				   struct cfg80211_bitrate_mask *beacon_rate)
3733 {
3734 	u32 count_ht, count_vht, i;
3735 	u32 rate = beacon_rate->control[band].legacy;
3736 
3737 	/* Allow only one rate */
3738 	if (hweight32(rate) > 1)
3739 		return -EINVAL;
3740 
3741 	count_ht = 0;
3742 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3743 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
3744 			return -EINVAL;
3745 		} else if (beacon_rate->control[band].ht_mcs[i]) {
3746 			count_ht++;
3747 			if (count_ht > 1)
3748 				return -EINVAL;
3749 		}
3750 		if (count_ht && rate)
3751 			return -EINVAL;
3752 	}
3753 
3754 	count_vht = 0;
3755 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3756 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
3757 			return -EINVAL;
3758 		} else if (beacon_rate->control[band].vht_mcs[i]) {
3759 			count_vht++;
3760 			if (count_vht > 1)
3761 				return -EINVAL;
3762 		}
3763 		if (count_vht && rate)
3764 			return -EINVAL;
3765 	}
3766 
3767 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
3768 		return -EINVAL;
3769 
3770 	if (rate &&
3771 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3772 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
3773 		return -EINVAL;
3774 	if (count_ht &&
3775 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3776 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
3777 		return -EINVAL;
3778 	if (count_vht &&
3779 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3780 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
3781 		return -EINVAL;
3782 
3783 	return 0;
3784 }
3785 
3786 static int nl80211_parse_beacon(struct nlattr *attrs[],
3787 				struct cfg80211_beacon_data *bcn)
3788 {
3789 	bool haveinfo = false;
3790 
3791 	if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3792 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3793 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3794 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3795 		return -EINVAL;
3796 
3797 	memset(bcn, 0, sizeof(*bcn));
3798 
3799 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3800 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3801 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3802 		if (!bcn->head_len)
3803 			return -EINVAL;
3804 		haveinfo = true;
3805 	}
3806 
3807 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3808 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3809 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3810 		haveinfo = true;
3811 	}
3812 
3813 	if (!haveinfo)
3814 		return -EINVAL;
3815 
3816 	if (attrs[NL80211_ATTR_IE]) {
3817 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3818 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3819 	}
3820 
3821 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3822 		bcn->proberesp_ies =
3823 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3824 		bcn->proberesp_ies_len =
3825 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3826 	}
3827 
3828 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3829 		bcn->assocresp_ies =
3830 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3831 		bcn->assocresp_ies_len =
3832 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3833 	}
3834 
3835 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
3836 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3837 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3838 	}
3839 
3840 	return 0;
3841 }
3842 
3843 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
3844 					    const u8 *rates)
3845 {
3846 	int i;
3847 
3848 	if (!rates)
3849 		return;
3850 
3851 	for (i = 0; i < rates[1]; i++) {
3852 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
3853 			params->ht_required = true;
3854 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
3855 			params->vht_required = true;
3856 	}
3857 }
3858 
3859 /*
3860  * Since the nl80211 API didn't include, from the beginning, attributes about
3861  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
3862  * benefit of drivers that rebuild IEs in the firmware.
3863  */
3864 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
3865 {
3866 	const struct cfg80211_beacon_data *bcn = &params->beacon;
3867 	size_t ies_len = bcn->tail_len;
3868 	const u8 *ies = bcn->tail;
3869 	const u8 *rates;
3870 	const u8 *cap;
3871 
3872 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
3873 	nl80211_check_ap_rate_selectors(params, rates);
3874 
3875 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
3876 	nl80211_check_ap_rate_selectors(params, rates);
3877 
3878 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
3879 	if (cap && cap[1] >= sizeof(*params->ht_cap))
3880 		params->ht_cap = (void *)(cap + 2);
3881 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
3882 	if (cap && cap[1] >= sizeof(*params->vht_cap))
3883 		params->vht_cap = (void *)(cap + 2);
3884 }
3885 
3886 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3887 				   struct cfg80211_ap_settings *params)
3888 {
3889 	struct wireless_dev *wdev;
3890 	bool ret = false;
3891 
3892 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3893 		if (wdev->iftype != NL80211_IFTYPE_AP &&
3894 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
3895 			continue;
3896 
3897 		if (!wdev->preset_chandef.chan)
3898 			continue;
3899 
3900 		params->chandef = wdev->preset_chandef;
3901 		ret = true;
3902 		break;
3903 	}
3904 
3905 	return ret;
3906 }
3907 
3908 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3909 				    enum nl80211_auth_type auth_type,
3910 				    enum nl80211_commands cmd)
3911 {
3912 	if (auth_type > NL80211_AUTHTYPE_MAX)
3913 		return false;
3914 
3915 	switch (cmd) {
3916 	case NL80211_CMD_AUTHENTICATE:
3917 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3918 		    auth_type == NL80211_AUTHTYPE_SAE)
3919 			return false;
3920 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3921 					     NL80211_EXT_FEATURE_FILS_STA) &&
3922 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3923 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3924 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
3925 			return false;
3926 		return true;
3927 	case NL80211_CMD_CONNECT:
3928 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3929 		    auth_type == NL80211_AUTHTYPE_SAE)
3930 			return false;
3931 
3932 		/* FILS with SK PFS or PK not supported yet */
3933 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3934 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
3935 			return false;
3936 		if (!wiphy_ext_feature_isset(
3937 			    &rdev->wiphy,
3938 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
3939 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
3940 			return false;
3941 		return true;
3942 	case NL80211_CMD_START_AP:
3943 		/* SAE not supported yet */
3944 		if (auth_type == NL80211_AUTHTYPE_SAE)
3945 			return false;
3946 		/* FILS not supported yet */
3947 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3948 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3949 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
3950 			return false;
3951 		return true;
3952 	default:
3953 		return false;
3954 	}
3955 }
3956 
3957 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3958 {
3959 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3960 	struct net_device *dev = info->user_ptr[1];
3961 	struct wireless_dev *wdev = dev->ieee80211_ptr;
3962 	struct cfg80211_ap_settings params;
3963 	int err;
3964 
3965 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3966 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3967 		return -EOPNOTSUPP;
3968 
3969 	if (!rdev->ops->start_ap)
3970 		return -EOPNOTSUPP;
3971 
3972 	if (wdev->beacon_interval)
3973 		return -EALREADY;
3974 
3975 	memset(&params, 0, sizeof(params));
3976 
3977 	/* these are required for START_AP */
3978 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3979 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3980 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
3981 		return -EINVAL;
3982 
3983 	err = nl80211_parse_beacon(info->attrs, &params.beacon);
3984 	if (err)
3985 		return err;
3986 
3987 	params.beacon_interval =
3988 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3989 	params.dtim_period =
3990 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3991 
3992 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
3993 					   params.beacon_interval);
3994 	if (err)
3995 		return err;
3996 
3997 	/*
3998 	 * In theory, some of these attributes should be required here
3999 	 * but since they were not used when the command was originally
4000 	 * added, keep them optional for old user space programs to let
4001 	 * them continue to work with drivers that do not need the
4002 	 * additional information -- drivers must check!
4003 	 */
4004 	if (info->attrs[NL80211_ATTR_SSID]) {
4005 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4006 		params.ssid_len =
4007 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4008 		if (params.ssid_len == 0 ||
4009 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4010 			return -EINVAL;
4011 	}
4012 
4013 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
4014 		params.hidden_ssid = nla_get_u32(
4015 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4016 		if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
4017 		    params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
4018 		    params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
4019 			return -EINVAL;
4020 	}
4021 
4022 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4023 
4024 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4025 		params.auth_type = nla_get_u32(
4026 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4027 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4028 					     NL80211_CMD_START_AP))
4029 			return -EINVAL;
4030 	} else
4031 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4032 
4033 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4034 				      NL80211_MAX_NR_CIPHER_SUITES);
4035 	if (err)
4036 		return err;
4037 
4038 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4039 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4040 			return -EOPNOTSUPP;
4041 		params.inactivity_timeout = nla_get_u16(
4042 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4043 	}
4044 
4045 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4046 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4047 			return -EINVAL;
4048 		params.p2p_ctwindow =
4049 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4050 		if (params.p2p_ctwindow > 127)
4051 			return -EINVAL;
4052 		if (params.p2p_ctwindow != 0 &&
4053 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4054 			return -EINVAL;
4055 	}
4056 
4057 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4058 		u8 tmp;
4059 
4060 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4061 			return -EINVAL;
4062 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4063 		if (tmp > 1)
4064 			return -EINVAL;
4065 		params.p2p_opp_ps = tmp;
4066 		if (params.p2p_opp_ps != 0 &&
4067 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4068 			return -EINVAL;
4069 	}
4070 
4071 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4072 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4073 		if (err)
4074 			return err;
4075 	} else if (wdev->preset_chandef.chan) {
4076 		params.chandef = wdev->preset_chandef;
4077 	} else if (!nl80211_get_ap_channel(rdev, &params))
4078 		return -EINVAL;
4079 
4080 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4081 					   wdev->iftype))
4082 		return -EINVAL;
4083 
4084 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4085 		err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4086 		if (err)
4087 			return err;
4088 
4089 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4090 					      &params.beacon_rate);
4091 		if (err)
4092 			return err;
4093 	}
4094 
4095 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4096 		params.smps_mode =
4097 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4098 		switch (params.smps_mode) {
4099 		case NL80211_SMPS_OFF:
4100 			break;
4101 		case NL80211_SMPS_STATIC:
4102 			if (!(rdev->wiphy.features &
4103 			      NL80211_FEATURE_STATIC_SMPS))
4104 				return -EINVAL;
4105 			break;
4106 		case NL80211_SMPS_DYNAMIC:
4107 			if (!(rdev->wiphy.features &
4108 			      NL80211_FEATURE_DYNAMIC_SMPS))
4109 				return -EINVAL;
4110 			break;
4111 		default:
4112 			return -EINVAL;
4113 		}
4114 	} else {
4115 		params.smps_mode = NL80211_SMPS_OFF;
4116 	}
4117 
4118 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4119 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4120 		return -EOPNOTSUPP;
4121 
4122 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4123 		params.acl = parse_acl_data(&rdev->wiphy, info);
4124 		if (IS_ERR(params.acl))
4125 			return PTR_ERR(params.acl);
4126 	}
4127 
4128 	nl80211_calculate_ap_params(&params);
4129 
4130 	wdev_lock(wdev);
4131 	err = rdev_start_ap(rdev, dev, &params);
4132 	if (!err) {
4133 		wdev->preset_chandef = params.chandef;
4134 		wdev->beacon_interval = params.beacon_interval;
4135 		wdev->chandef = params.chandef;
4136 		wdev->ssid_len = params.ssid_len;
4137 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4138 
4139 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4140 			wdev->conn_owner_nlportid = info->snd_portid;
4141 	}
4142 	wdev_unlock(wdev);
4143 
4144 	kfree(params.acl);
4145 
4146 	return err;
4147 }
4148 
4149 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4150 {
4151 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4152 	struct net_device *dev = info->user_ptr[1];
4153 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4154 	struct cfg80211_beacon_data params;
4155 	int err;
4156 
4157 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4158 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4159 		return -EOPNOTSUPP;
4160 
4161 	if (!rdev->ops->change_beacon)
4162 		return -EOPNOTSUPP;
4163 
4164 	if (!wdev->beacon_interval)
4165 		return -EINVAL;
4166 
4167 	err = nl80211_parse_beacon(info->attrs, &params);
4168 	if (err)
4169 		return err;
4170 
4171 	wdev_lock(wdev);
4172 	err = rdev_change_beacon(rdev, dev, &params);
4173 	wdev_unlock(wdev);
4174 
4175 	return err;
4176 }
4177 
4178 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4179 {
4180 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4181 	struct net_device *dev = info->user_ptr[1];
4182 
4183 	return cfg80211_stop_ap(rdev, dev, false);
4184 }
4185 
4186 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4187 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4188 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4189 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4190 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4191 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4192 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4193 };
4194 
4195 static int parse_station_flags(struct genl_info *info,
4196 			       enum nl80211_iftype iftype,
4197 			       struct station_parameters *params)
4198 {
4199 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4200 	struct nlattr *nla;
4201 	int flag;
4202 
4203 	/*
4204 	 * Try parsing the new attribute first so userspace
4205 	 * can specify both for older kernels.
4206 	 */
4207 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4208 	if (nla) {
4209 		struct nl80211_sta_flag_update *sta_flags;
4210 
4211 		sta_flags = nla_data(nla);
4212 		params->sta_flags_mask = sta_flags->mask;
4213 		params->sta_flags_set = sta_flags->set;
4214 		params->sta_flags_set &= params->sta_flags_mask;
4215 		if ((params->sta_flags_mask |
4216 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4217 			return -EINVAL;
4218 		return 0;
4219 	}
4220 
4221 	/* if present, parse the old attribute */
4222 
4223 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4224 	if (!nla)
4225 		return 0;
4226 
4227 	if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, nla,
4228 			     sta_flags_policy, info->extack))
4229 		return -EINVAL;
4230 
4231 	/*
4232 	 * Only allow certain flags for interface types so that
4233 	 * other attributes are silently ignored. Remember that
4234 	 * this is backward compatibility code with old userspace
4235 	 * and shouldn't be hit in other cases anyway.
4236 	 */
4237 	switch (iftype) {
4238 	case NL80211_IFTYPE_AP:
4239 	case NL80211_IFTYPE_AP_VLAN:
4240 	case NL80211_IFTYPE_P2P_GO:
4241 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4242 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4243 					 BIT(NL80211_STA_FLAG_WME) |
4244 					 BIT(NL80211_STA_FLAG_MFP);
4245 		break;
4246 	case NL80211_IFTYPE_P2P_CLIENT:
4247 	case NL80211_IFTYPE_STATION:
4248 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4249 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
4250 		break;
4251 	case NL80211_IFTYPE_MESH_POINT:
4252 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4253 					 BIT(NL80211_STA_FLAG_MFP) |
4254 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
4255 	default:
4256 		return -EINVAL;
4257 	}
4258 
4259 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4260 		if (flags[flag]) {
4261 			params->sta_flags_set |= (1<<flag);
4262 
4263 			/* no longer support new API additions in old API */
4264 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4265 				return -EINVAL;
4266 		}
4267 	}
4268 
4269 	return 0;
4270 }
4271 
4272 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
4273 				 int attr)
4274 {
4275 	struct nlattr *rate;
4276 	u32 bitrate;
4277 	u16 bitrate_compat;
4278 	enum nl80211_rate_info rate_flg;
4279 
4280 	rate = nla_nest_start(msg, attr);
4281 	if (!rate)
4282 		return false;
4283 
4284 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4285 	bitrate = cfg80211_calculate_bitrate(info);
4286 	/* report 16-bit bitrate only if we can */
4287 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4288 	if (bitrate > 0 &&
4289 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4290 		return false;
4291 	if (bitrate_compat > 0 &&
4292 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4293 		return false;
4294 
4295 	switch (info->bw) {
4296 	case RATE_INFO_BW_5:
4297 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4298 		break;
4299 	case RATE_INFO_BW_10:
4300 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4301 		break;
4302 	default:
4303 		WARN_ON(1);
4304 		/* fall through */
4305 	case RATE_INFO_BW_20:
4306 		rate_flg = 0;
4307 		break;
4308 	case RATE_INFO_BW_40:
4309 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4310 		break;
4311 	case RATE_INFO_BW_80:
4312 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4313 		break;
4314 	case RATE_INFO_BW_160:
4315 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4316 		break;
4317 	}
4318 
4319 	if (rate_flg && nla_put_flag(msg, rate_flg))
4320 		return false;
4321 
4322 	if (info->flags & RATE_INFO_FLAGS_MCS) {
4323 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4324 			return false;
4325 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4326 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4327 			return false;
4328 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4329 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4330 			return false;
4331 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4332 			return false;
4333 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4334 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4335 			return false;
4336 	}
4337 
4338 	nla_nest_end(msg, rate);
4339 	return true;
4340 }
4341 
4342 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4343 			       int id)
4344 {
4345 	void *attr;
4346 	int i = 0;
4347 
4348 	if (!mask)
4349 		return true;
4350 
4351 	attr = nla_nest_start(msg, id);
4352 	if (!attr)
4353 		return false;
4354 
4355 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4356 		if (!(mask & BIT(i)))
4357 			continue;
4358 
4359 		if (nla_put_u8(msg, i, signal[i]))
4360 			return false;
4361 	}
4362 
4363 	nla_nest_end(msg, attr);
4364 
4365 	return true;
4366 }
4367 
4368 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4369 				u32 seq, int flags,
4370 				struct cfg80211_registered_device *rdev,
4371 				struct net_device *dev,
4372 				const u8 *mac_addr, struct station_info *sinfo)
4373 {
4374 	void *hdr;
4375 	struct nlattr *sinfoattr, *bss_param;
4376 
4377 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
4378 	if (!hdr)
4379 		return -1;
4380 
4381 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4382 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
4383 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
4384 		goto nla_put_failure;
4385 
4386 	sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
4387 	if (!sinfoattr)
4388 		goto nla_put_failure;
4389 
4390 #define PUT_SINFO(attr, memb, type) do {				\
4391 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
4392 	if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&	\
4393 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
4394 			     sinfo->memb))				\
4395 		goto nla_put_failure;					\
4396 	} while (0)
4397 #define PUT_SINFO_U64(attr, memb) do {					\
4398 	if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&	\
4399 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
4400 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
4401 		goto nla_put_failure;					\
4402 	} while (0)
4403 
4404 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
4405 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
4406 
4407 	if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
4408 			     BIT(NL80211_STA_INFO_RX_BYTES64)) &&
4409 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
4410 			(u32)sinfo->rx_bytes))
4411 		goto nla_put_failure;
4412 
4413 	if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
4414 			     BIT(NL80211_STA_INFO_TX_BYTES64)) &&
4415 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
4416 			(u32)sinfo->tx_bytes))
4417 		goto nla_put_failure;
4418 
4419 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
4420 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
4421 	PUT_SINFO(LLID, llid, u16);
4422 	PUT_SINFO(PLID, plid, u16);
4423 	PUT_SINFO(PLINK_STATE, plink_state, u8);
4424 	PUT_SINFO_U64(RX_DURATION, rx_duration);
4425 
4426 	switch (rdev->wiphy.signal_type) {
4427 	case CFG80211_SIGNAL_TYPE_MBM:
4428 		PUT_SINFO(SIGNAL, signal, u8);
4429 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
4430 		break;
4431 	default:
4432 		break;
4433 	}
4434 	if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
4435 		if (!nl80211_put_signal(msg, sinfo->chains,
4436 					sinfo->chain_signal,
4437 					NL80211_STA_INFO_CHAIN_SIGNAL))
4438 			goto nla_put_failure;
4439 	}
4440 	if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
4441 		if (!nl80211_put_signal(msg, sinfo->chains,
4442 					sinfo->chain_signal_avg,
4443 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
4444 			goto nla_put_failure;
4445 	}
4446 	if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
4447 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
4448 					  NL80211_STA_INFO_TX_BITRATE))
4449 			goto nla_put_failure;
4450 	}
4451 	if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
4452 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
4453 					  NL80211_STA_INFO_RX_BITRATE))
4454 			goto nla_put_failure;
4455 	}
4456 
4457 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
4458 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
4459 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
4460 	PUT_SINFO(TX_FAILED, tx_failed, u32);
4461 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
4462 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
4463 	PUT_SINFO(LOCAL_PM, local_pm, u32);
4464 	PUT_SINFO(PEER_PM, peer_pm, u32);
4465 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
4466 
4467 	if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
4468 		bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
4469 		if (!bss_param)
4470 			goto nla_put_failure;
4471 
4472 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
4473 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
4474 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
4475 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
4476 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
4477 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
4478 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
4479 			       sinfo->bss_param.dtim_period) ||
4480 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
4481 				sinfo->bss_param.beacon_interval))
4482 			goto nla_put_failure;
4483 
4484 		nla_nest_end(msg, bss_param);
4485 	}
4486 	if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
4487 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
4488 		    sizeof(struct nl80211_sta_flag_update),
4489 		    &sinfo->sta_flags))
4490 		goto nla_put_failure;
4491 
4492 	PUT_SINFO_U64(T_OFFSET, t_offset);
4493 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
4494 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
4495 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
4496 	PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
4497 
4498 #undef PUT_SINFO
4499 #undef PUT_SINFO_U64
4500 
4501 	if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
4502 		struct nlattr *tidsattr;
4503 		int tid;
4504 
4505 		tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
4506 		if (!tidsattr)
4507 			goto nla_put_failure;
4508 
4509 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
4510 			struct cfg80211_tid_stats *tidstats;
4511 			struct nlattr *tidattr;
4512 
4513 			tidstats = &sinfo->pertid[tid];
4514 
4515 			if (!tidstats->filled)
4516 				continue;
4517 
4518 			tidattr = nla_nest_start(msg, tid + 1);
4519 			if (!tidattr)
4520 				goto nla_put_failure;
4521 
4522 #define PUT_TIDVAL_U64(attr, memb) do {					\
4523 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
4524 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
4525 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
4526 		goto nla_put_failure;					\
4527 	} while (0)
4528 
4529 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
4530 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
4531 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
4532 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
4533 
4534 #undef PUT_TIDVAL_U64
4535 			nla_nest_end(msg, tidattr);
4536 		}
4537 
4538 		nla_nest_end(msg, tidsattr);
4539 	}
4540 
4541 	nla_nest_end(msg, sinfoattr);
4542 
4543 	if (sinfo->assoc_req_ies_len &&
4544 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
4545 		    sinfo->assoc_req_ies))
4546 		goto nla_put_failure;
4547 
4548 	genlmsg_end(msg, hdr);
4549 	return 0;
4550 
4551  nla_put_failure:
4552 	genlmsg_cancel(msg, hdr);
4553 	return -EMSGSIZE;
4554 }
4555 
4556 static int nl80211_dump_station(struct sk_buff *skb,
4557 				struct netlink_callback *cb)
4558 {
4559 	struct station_info sinfo;
4560 	struct cfg80211_registered_device *rdev;
4561 	struct wireless_dev *wdev;
4562 	u8 mac_addr[ETH_ALEN];
4563 	int sta_idx = cb->args[2];
4564 	int err;
4565 
4566 	rtnl_lock();
4567 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4568 	if (err)
4569 		goto out_err;
4570 
4571 	if (!wdev->netdev) {
4572 		err = -EINVAL;
4573 		goto out_err;
4574 	}
4575 
4576 	if (!rdev->ops->dump_station) {
4577 		err = -EOPNOTSUPP;
4578 		goto out_err;
4579 	}
4580 
4581 	while (1) {
4582 		memset(&sinfo, 0, sizeof(sinfo));
4583 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
4584 					mac_addr, &sinfo);
4585 		if (err == -ENOENT)
4586 			break;
4587 		if (err)
4588 			goto out_err;
4589 
4590 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
4591 				NETLINK_CB(cb->skb).portid,
4592 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
4593 				rdev, wdev->netdev, mac_addr,
4594 				&sinfo) < 0)
4595 			goto out;
4596 
4597 		sta_idx++;
4598 	}
4599 
4600  out:
4601 	cb->args[2] = sta_idx;
4602 	err = skb->len;
4603  out_err:
4604 	rtnl_unlock();
4605 
4606 	return err;
4607 }
4608 
4609 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
4610 {
4611 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4612 	struct net_device *dev = info->user_ptr[1];
4613 	struct station_info sinfo;
4614 	struct sk_buff *msg;
4615 	u8 *mac_addr = NULL;
4616 	int err;
4617 
4618 	memset(&sinfo, 0, sizeof(sinfo));
4619 
4620 	if (!info->attrs[NL80211_ATTR_MAC])
4621 		return -EINVAL;
4622 
4623 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4624 
4625 	if (!rdev->ops->get_station)
4626 		return -EOPNOTSUPP;
4627 
4628 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
4629 	if (err)
4630 		return err;
4631 
4632 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4633 	if (!msg)
4634 		return -ENOMEM;
4635 
4636 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
4637 				 info->snd_portid, info->snd_seq, 0,
4638 				 rdev, dev, mac_addr, &sinfo) < 0) {
4639 		nlmsg_free(msg);
4640 		return -ENOBUFS;
4641 	}
4642 
4643 	return genlmsg_reply(msg, info);
4644 }
4645 
4646 int cfg80211_check_station_change(struct wiphy *wiphy,
4647 				  struct station_parameters *params,
4648 				  enum cfg80211_station_type statype)
4649 {
4650 	if (params->listen_interval != -1 &&
4651 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4652 		return -EINVAL;
4653 
4654 	if (params->support_p2p_ps != -1 &&
4655 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4656 		return -EINVAL;
4657 
4658 	if (params->aid &&
4659 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
4660 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4661 		return -EINVAL;
4662 
4663 	/* When you run into this, adjust the code below for the new flag */
4664 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4665 
4666 	switch (statype) {
4667 	case CFG80211_STA_MESH_PEER_KERNEL:
4668 	case CFG80211_STA_MESH_PEER_USER:
4669 		/*
4670 		 * No ignoring the TDLS flag here -- the userspace mesh
4671 		 * code doesn't have the bug of including TDLS in the
4672 		 * mask everywhere.
4673 		 */
4674 		if (params->sta_flags_mask &
4675 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4676 				  BIT(NL80211_STA_FLAG_MFP) |
4677 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
4678 			return -EINVAL;
4679 		break;
4680 	case CFG80211_STA_TDLS_PEER_SETUP:
4681 	case CFG80211_STA_TDLS_PEER_ACTIVE:
4682 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4683 			return -EINVAL;
4684 		/* ignore since it can't change */
4685 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4686 		break;
4687 	default:
4688 		/* disallow mesh-specific things */
4689 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4690 			return -EINVAL;
4691 		if (params->local_pm)
4692 			return -EINVAL;
4693 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4694 			return -EINVAL;
4695 	}
4696 
4697 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4698 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4699 		/* TDLS can't be set, ... */
4700 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4701 			return -EINVAL;
4702 		/*
4703 		 * ... but don't bother the driver with it. This works around
4704 		 * a hostapd/wpa_supplicant issue -- it always includes the
4705 		 * TLDS_PEER flag in the mask even for AP mode.
4706 		 */
4707 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4708 	}
4709 
4710 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4711 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4712 		/* reject other things that can't change */
4713 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4714 			return -EINVAL;
4715 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4716 			return -EINVAL;
4717 		if (params->supported_rates)
4718 			return -EINVAL;
4719 		if (params->ext_capab || params->ht_capa || params->vht_capa)
4720 			return -EINVAL;
4721 	}
4722 
4723 	if (statype != CFG80211_STA_AP_CLIENT &&
4724 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4725 		if (params->vlan)
4726 			return -EINVAL;
4727 	}
4728 
4729 	switch (statype) {
4730 	case CFG80211_STA_AP_MLME_CLIENT:
4731 		/* Use this only for authorizing/unauthorizing a station */
4732 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4733 			return -EOPNOTSUPP;
4734 		break;
4735 	case CFG80211_STA_AP_CLIENT:
4736 	case CFG80211_STA_AP_CLIENT_UNASSOC:
4737 		/* accept only the listed bits */
4738 		if (params->sta_flags_mask &
4739 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4740 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4741 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
4742 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4743 				  BIT(NL80211_STA_FLAG_WME) |
4744 				  BIT(NL80211_STA_FLAG_MFP)))
4745 			return -EINVAL;
4746 
4747 		/* but authenticated/associated only if driver handles it */
4748 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4749 		    params->sta_flags_mask &
4750 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4751 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4752 			return -EINVAL;
4753 		break;
4754 	case CFG80211_STA_IBSS:
4755 	case CFG80211_STA_AP_STA:
4756 		/* reject any changes other than AUTHORIZED */
4757 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4758 			return -EINVAL;
4759 		break;
4760 	case CFG80211_STA_TDLS_PEER_SETUP:
4761 		/* reject any changes other than AUTHORIZED or WME */
4762 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4763 					       BIT(NL80211_STA_FLAG_WME)))
4764 			return -EINVAL;
4765 		/* force (at least) rates when authorizing */
4766 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4767 		    !params->supported_rates)
4768 			return -EINVAL;
4769 		break;
4770 	case CFG80211_STA_TDLS_PEER_ACTIVE:
4771 		/* reject any changes */
4772 		return -EINVAL;
4773 	case CFG80211_STA_MESH_PEER_KERNEL:
4774 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4775 			return -EINVAL;
4776 		break;
4777 	case CFG80211_STA_MESH_PEER_USER:
4778 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4779 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
4780 			return -EINVAL;
4781 		break;
4782 	}
4783 
4784 	/*
4785 	 * Older kernel versions ignored this attribute entirely, so don't
4786 	 * reject attempts to update it but mark it as unused instead so the
4787 	 * driver won't look at the data.
4788 	 */
4789 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
4790 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
4791 		params->opmode_notif_used = false;
4792 
4793 	return 0;
4794 }
4795 EXPORT_SYMBOL(cfg80211_check_station_change);
4796 
4797 /*
4798  * Get vlan interface making sure it is running and on the right wiphy.
4799  */
4800 static struct net_device *get_vlan(struct genl_info *info,
4801 				   struct cfg80211_registered_device *rdev)
4802 {
4803 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
4804 	struct net_device *v;
4805 	int ret;
4806 
4807 	if (!vlanattr)
4808 		return NULL;
4809 
4810 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4811 	if (!v)
4812 		return ERR_PTR(-ENODEV);
4813 
4814 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4815 		ret = -EINVAL;
4816 		goto error;
4817 	}
4818 
4819 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4820 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4821 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4822 		ret = -EINVAL;
4823 		goto error;
4824 	}
4825 
4826 	if (!netif_running(v)) {
4827 		ret = -ENETDOWN;
4828 		goto error;
4829 	}
4830 
4831 	return v;
4832  error:
4833 	dev_put(v);
4834 	return ERR_PTR(ret);
4835 }
4836 
4837 static const struct nla_policy
4838 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
4839 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4840 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4841 };
4842 
4843 static int nl80211_parse_sta_wme(struct genl_info *info,
4844 				 struct station_parameters *params)
4845 {
4846 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4847 	struct nlattr *nla;
4848 	int err;
4849 
4850 	/* parse WME attributes if present */
4851 	if (!info->attrs[NL80211_ATTR_STA_WME])
4852 		return 0;
4853 
4854 	nla = info->attrs[NL80211_ATTR_STA_WME];
4855 	err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4856 			       nl80211_sta_wme_policy, info->extack);
4857 	if (err)
4858 		return err;
4859 
4860 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4861 		params->uapsd_queues = nla_get_u8(
4862 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
4863 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4864 		return -EINVAL;
4865 
4866 	if (tb[NL80211_STA_WME_MAX_SP])
4867 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4868 
4869 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4870 		return -EINVAL;
4871 
4872 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4873 
4874 	return 0;
4875 }
4876 
4877 static int nl80211_parse_sta_channel_info(struct genl_info *info,
4878 				      struct station_parameters *params)
4879 {
4880 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4881 		params->supported_channels =
4882 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4883 		params->supported_channels_len =
4884 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4885 		/*
4886 		 * Need to include at least one (first channel, number of
4887 		 * channels) tuple for each subband, and must have proper
4888 		 * tuples for the rest of the data as well.
4889 		 */
4890 		if (params->supported_channels_len < 2)
4891 			return -EINVAL;
4892 		if (params->supported_channels_len % 2)
4893 			return -EINVAL;
4894 	}
4895 
4896 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4897 		params->supported_oper_classes =
4898 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4899 		params->supported_oper_classes_len =
4900 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4901 		/*
4902 		 * The value of the Length field of the Supported Operating
4903 		 * Classes element is between 2 and 253.
4904 		 */
4905 		if (params->supported_oper_classes_len < 2 ||
4906 		    params->supported_oper_classes_len > 253)
4907 			return -EINVAL;
4908 	}
4909 	return 0;
4910 }
4911 
4912 static int nl80211_set_station_tdls(struct genl_info *info,
4913 				    struct station_parameters *params)
4914 {
4915 	int err;
4916 	/* Dummy STA entry gets updated once the peer capabilities are known */
4917 	if (info->attrs[NL80211_ATTR_PEER_AID])
4918 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4919 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4920 		params->ht_capa =
4921 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4922 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4923 		params->vht_capa =
4924 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4925 
4926 	err = nl80211_parse_sta_channel_info(info, params);
4927 	if (err)
4928 		return err;
4929 
4930 	return nl80211_parse_sta_wme(info, params);
4931 }
4932 
4933 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4934 {
4935 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4936 	struct net_device *dev = info->user_ptr[1];
4937 	struct station_parameters params;
4938 	u8 *mac_addr;
4939 	int err;
4940 
4941 	memset(&params, 0, sizeof(params));
4942 
4943 	if (!rdev->ops->change_station)
4944 		return -EOPNOTSUPP;
4945 
4946 	/*
4947 	 * AID and listen_interval properties can be set only for unassociated
4948 	 * station. Include these parameters here and will check them in
4949 	 * cfg80211_check_station_change().
4950 	 */
4951 	if (info->attrs[NL80211_ATTR_STA_AID])
4952 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4953 
4954 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4955 		params.listen_interval =
4956 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4957 	else
4958 		params.listen_interval = -1;
4959 
4960 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
4961 		u8 tmp;
4962 
4963 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
4964 		if (tmp >= NUM_NL80211_P2P_PS_STATUS)
4965 			return -EINVAL;
4966 
4967 		params.support_p2p_ps = tmp;
4968 	} else {
4969 		params.support_p2p_ps = -1;
4970 	}
4971 
4972 	if (!info->attrs[NL80211_ATTR_MAC])
4973 		return -EINVAL;
4974 
4975 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4976 
4977 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4978 		params.supported_rates =
4979 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4980 		params.supported_rates_len =
4981 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4982 	}
4983 
4984 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4985 		params.capability =
4986 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4987 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4988 	}
4989 
4990 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4991 		params.ext_capab =
4992 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4993 		params.ext_capab_len =
4994 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4995 	}
4996 
4997 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4998 		return -EINVAL;
4999 
5000 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5001 		params.plink_action =
5002 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5003 		if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5004 			return -EINVAL;
5005 	}
5006 
5007 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5008 		params.plink_state =
5009 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5010 		if (params.plink_state >= NUM_NL80211_PLINK_STATES)
5011 			return -EINVAL;
5012 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID]) {
5013 			params.peer_aid = nla_get_u16(
5014 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5015 			if (params.peer_aid > IEEE80211_MAX_AID)
5016 				return -EINVAL;
5017 		}
5018 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5019 	}
5020 
5021 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
5022 		enum nl80211_mesh_power_mode pm = nla_get_u32(
5023 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5024 
5025 		if (pm <= NL80211_MESH_POWER_UNKNOWN ||
5026 		    pm > NL80211_MESH_POWER_MAX)
5027 			return -EINVAL;
5028 
5029 		params.local_pm = pm;
5030 	}
5031 
5032 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5033 		params.opmode_notif_used = true;
5034 		params.opmode_notif =
5035 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5036 	}
5037 
5038 	/* Include parameters for TDLS peer (will check later) */
5039 	err = nl80211_set_station_tdls(info, &params);
5040 	if (err)
5041 		return err;
5042 
5043 	params.vlan = get_vlan(info, rdev);
5044 	if (IS_ERR(params.vlan))
5045 		return PTR_ERR(params.vlan);
5046 
5047 	switch (dev->ieee80211_ptr->iftype) {
5048 	case NL80211_IFTYPE_AP:
5049 	case NL80211_IFTYPE_AP_VLAN:
5050 	case NL80211_IFTYPE_P2P_GO:
5051 	case NL80211_IFTYPE_P2P_CLIENT:
5052 	case NL80211_IFTYPE_STATION:
5053 	case NL80211_IFTYPE_ADHOC:
5054 	case NL80211_IFTYPE_MESH_POINT:
5055 		break;
5056 	default:
5057 		err = -EOPNOTSUPP;
5058 		goto out_put_vlan;
5059 	}
5060 
5061 	/* driver will call cfg80211_check_station_change() */
5062 	err = rdev_change_station(rdev, dev, mac_addr, &params);
5063 
5064  out_put_vlan:
5065 	if (params.vlan)
5066 		dev_put(params.vlan);
5067 
5068 	return err;
5069 }
5070 
5071 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5072 {
5073 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5074 	int err;
5075 	struct net_device *dev = info->user_ptr[1];
5076 	struct station_parameters params;
5077 	u8 *mac_addr = NULL;
5078 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5079 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
5080 
5081 	memset(&params, 0, sizeof(params));
5082 
5083 	if (!rdev->ops->add_station)
5084 		return -EOPNOTSUPP;
5085 
5086 	if (!info->attrs[NL80211_ATTR_MAC])
5087 		return -EINVAL;
5088 
5089 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5090 		return -EINVAL;
5091 
5092 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5093 		return -EINVAL;
5094 
5095 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
5096 	    !info->attrs[NL80211_ATTR_PEER_AID])
5097 		return -EINVAL;
5098 
5099 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5100 	params.supported_rates =
5101 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5102 	params.supported_rates_len =
5103 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5104 	params.listen_interval =
5105 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5106 
5107 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5108 		u8 tmp;
5109 
5110 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5111 		if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5112 			return -EINVAL;
5113 
5114 		params.support_p2p_ps = tmp;
5115 	} else {
5116 		/*
5117 		 * if not specified, assume it's supported for P2P GO interface,
5118 		 * and is NOT supported for AP interface
5119 		 */
5120 		params.support_p2p_ps =
5121 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5122 	}
5123 
5124 	if (info->attrs[NL80211_ATTR_PEER_AID])
5125 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5126 	else
5127 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5128 	if (!params.aid || params.aid > IEEE80211_MAX_AID)
5129 		return -EINVAL;
5130 
5131 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5132 		params.capability =
5133 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5134 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5135 	}
5136 
5137 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5138 		params.ext_capab =
5139 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5140 		params.ext_capab_len =
5141 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5142 	}
5143 
5144 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5145 		params.ht_capa =
5146 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5147 
5148 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5149 		params.vht_capa =
5150 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5151 
5152 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5153 		params.opmode_notif_used = true;
5154 		params.opmode_notif =
5155 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5156 	}
5157 
5158 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5159 		params.plink_action =
5160 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5161 		if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5162 			return -EINVAL;
5163 	}
5164 
5165 	err = nl80211_parse_sta_channel_info(info, &params);
5166 	if (err)
5167 		return err;
5168 
5169 	err = nl80211_parse_sta_wme(info, &params);
5170 	if (err)
5171 		return err;
5172 
5173 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5174 		return -EINVAL;
5175 
5176 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5177 	 * as userspace might just pass through the capabilities from the IEs
5178 	 * directly, rather than enforcing this restriction and returning an
5179 	 * error in this case.
5180 	 */
5181 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5182 		params.ht_capa = NULL;
5183 		params.vht_capa = NULL;
5184 	}
5185 
5186 	/* When you run into this, adjust the code below for the new flag */
5187 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5188 
5189 	switch (dev->ieee80211_ptr->iftype) {
5190 	case NL80211_IFTYPE_AP:
5191 	case NL80211_IFTYPE_AP_VLAN:
5192 	case NL80211_IFTYPE_P2P_GO:
5193 		/* ignore WME attributes if iface/sta is not capable */
5194 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5195 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5196 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5197 
5198 		/* TDLS peers cannot be added */
5199 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5200 		    info->attrs[NL80211_ATTR_PEER_AID])
5201 			return -EINVAL;
5202 		/* but don't bother the driver with it */
5203 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5204 
5205 		/* allow authenticated/associated only if driver handles it */
5206 		if (!(rdev->wiphy.features &
5207 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5208 		    params.sta_flags_mask & auth_assoc)
5209 			return -EINVAL;
5210 
5211 		/* Older userspace, or userspace wanting to be compatible with
5212 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5213 		 * and assoc flags in the mask, but assumes the station will be
5214 		 * added as associated anyway since this was the required driver
5215 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5216 		 * introduced.
5217 		 * In order to not bother drivers with this quirk in the API
5218 		 * set the flags in both the mask and set for new stations in
5219 		 * this case.
5220 		 */
5221 		if (!(params.sta_flags_mask & auth_assoc)) {
5222 			params.sta_flags_mask |= auth_assoc;
5223 			params.sta_flags_set |= auth_assoc;
5224 		}
5225 
5226 		/* must be last in here for error handling */
5227 		params.vlan = get_vlan(info, rdev);
5228 		if (IS_ERR(params.vlan))
5229 			return PTR_ERR(params.vlan);
5230 		break;
5231 	case NL80211_IFTYPE_MESH_POINT:
5232 		/* ignore uAPSD data */
5233 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5234 
5235 		/* associated is disallowed */
5236 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5237 			return -EINVAL;
5238 		/* TDLS peers cannot be added */
5239 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5240 		    info->attrs[NL80211_ATTR_PEER_AID])
5241 			return -EINVAL;
5242 		break;
5243 	case NL80211_IFTYPE_STATION:
5244 	case NL80211_IFTYPE_P2P_CLIENT:
5245 		/* ignore uAPSD data */
5246 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5247 
5248 		/* these are disallowed */
5249 		if (params.sta_flags_mask &
5250 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5251 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
5252 			return -EINVAL;
5253 		/* Only TDLS peers can be added */
5254 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5255 			return -EINVAL;
5256 		/* Can only add if TDLS ... */
5257 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5258 			return -EOPNOTSUPP;
5259 		/* ... with external setup is supported */
5260 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5261 			return -EOPNOTSUPP;
5262 		/*
5263 		 * Older wpa_supplicant versions always mark the TDLS peer
5264 		 * as authorized, but it shouldn't yet be.
5265 		 */
5266 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
5267 		break;
5268 	default:
5269 		return -EOPNOTSUPP;
5270 	}
5271 
5272 	/* be aware of params.vlan when changing code here */
5273 
5274 	err = rdev_add_station(rdev, dev, mac_addr, &params);
5275 
5276 	if (params.vlan)
5277 		dev_put(params.vlan);
5278 	return err;
5279 }
5280 
5281 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5282 {
5283 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5284 	struct net_device *dev = info->user_ptr[1];
5285 	struct station_del_parameters params;
5286 
5287 	memset(&params, 0, sizeof(params));
5288 
5289 	if (info->attrs[NL80211_ATTR_MAC])
5290 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
5291 
5292 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5293 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5294 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5295 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5296 		return -EINVAL;
5297 
5298 	if (!rdev->ops->del_station)
5299 		return -EOPNOTSUPP;
5300 
5301 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
5302 		params.subtype =
5303 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
5304 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
5305 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
5306 			return -EINVAL;
5307 	} else {
5308 		/* Default to Deauthentication frame */
5309 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
5310 	}
5311 
5312 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
5313 		params.reason_code =
5314 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5315 		if (params.reason_code == 0)
5316 			return -EINVAL; /* 0 is reserved */
5317 	} else {
5318 		/* Default to reason code 2 */
5319 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
5320 	}
5321 
5322 	return rdev_del_station(rdev, dev, &params);
5323 }
5324 
5325 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
5326 				int flags, struct net_device *dev,
5327 				u8 *dst, u8 *next_hop,
5328 				struct mpath_info *pinfo)
5329 {
5330 	void *hdr;
5331 	struct nlattr *pinfoattr;
5332 
5333 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
5334 	if (!hdr)
5335 		return -1;
5336 
5337 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5338 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
5339 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
5340 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
5341 		goto nla_put_failure;
5342 
5343 	pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
5344 	if (!pinfoattr)
5345 		goto nla_put_failure;
5346 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
5347 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
5348 			pinfo->frame_qlen))
5349 		goto nla_put_failure;
5350 	if (((pinfo->filled & MPATH_INFO_SN) &&
5351 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
5352 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
5353 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
5354 			 pinfo->metric)) ||
5355 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
5356 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
5357 			 pinfo->exptime)) ||
5358 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
5359 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
5360 			pinfo->flags)) ||
5361 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
5362 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
5363 			 pinfo->discovery_timeout)) ||
5364 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
5365 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
5366 			pinfo->discovery_retries)))
5367 		goto nla_put_failure;
5368 
5369 	nla_nest_end(msg, pinfoattr);
5370 
5371 	genlmsg_end(msg, hdr);
5372 	return 0;
5373 
5374  nla_put_failure:
5375 	genlmsg_cancel(msg, hdr);
5376 	return -EMSGSIZE;
5377 }
5378 
5379 static int nl80211_dump_mpath(struct sk_buff *skb,
5380 			      struct netlink_callback *cb)
5381 {
5382 	struct mpath_info pinfo;
5383 	struct cfg80211_registered_device *rdev;
5384 	struct wireless_dev *wdev;
5385 	u8 dst[ETH_ALEN];
5386 	u8 next_hop[ETH_ALEN];
5387 	int path_idx = cb->args[2];
5388 	int err;
5389 
5390 	rtnl_lock();
5391 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5392 	if (err)
5393 		goto out_err;
5394 
5395 	if (!rdev->ops->dump_mpath) {
5396 		err = -EOPNOTSUPP;
5397 		goto out_err;
5398 	}
5399 
5400 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5401 		err = -EOPNOTSUPP;
5402 		goto out_err;
5403 	}
5404 
5405 	while (1) {
5406 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
5407 				      next_hop, &pinfo);
5408 		if (err == -ENOENT)
5409 			break;
5410 		if (err)
5411 			goto out_err;
5412 
5413 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5414 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
5415 				       wdev->netdev, dst, next_hop,
5416 				       &pinfo) < 0)
5417 			goto out;
5418 
5419 		path_idx++;
5420 	}
5421 
5422  out:
5423 	cb->args[2] = path_idx;
5424 	err = skb->len;
5425  out_err:
5426 	rtnl_unlock();
5427 	return err;
5428 }
5429 
5430 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
5431 {
5432 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5433 	int err;
5434 	struct net_device *dev = info->user_ptr[1];
5435 	struct mpath_info pinfo;
5436 	struct sk_buff *msg;
5437 	u8 *dst = NULL;
5438 	u8 next_hop[ETH_ALEN];
5439 
5440 	memset(&pinfo, 0, sizeof(pinfo));
5441 
5442 	if (!info->attrs[NL80211_ATTR_MAC])
5443 		return -EINVAL;
5444 
5445 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5446 
5447 	if (!rdev->ops->get_mpath)
5448 		return -EOPNOTSUPP;
5449 
5450 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5451 		return -EOPNOTSUPP;
5452 
5453 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
5454 	if (err)
5455 		return err;
5456 
5457 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5458 	if (!msg)
5459 		return -ENOMEM;
5460 
5461 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5462 				 dev, dst, next_hop, &pinfo) < 0) {
5463 		nlmsg_free(msg);
5464 		return -ENOBUFS;
5465 	}
5466 
5467 	return genlmsg_reply(msg, info);
5468 }
5469 
5470 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
5471 {
5472 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5473 	struct net_device *dev = info->user_ptr[1];
5474 	u8 *dst = NULL;
5475 	u8 *next_hop = NULL;
5476 
5477 	if (!info->attrs[NL80211_ATTR_MAC])
5478 		return -EINVAL;
5479 
5480 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5481 		return -EINVAL;
5482 
5483 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5484 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5485 
5486 	if (!rdev->ops->change_mpath)
5487 		return -EOPNOTSUPP;
5488 
5489 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5490 		return -EOPNOTSUPP;
5491 
5492 	return rdev_change_mpath(rdev, dev, dst, next_hop);
5493 }
5494 
5495 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
5496 {
5497 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5498 	struct net_device *dev = info->user_ptr[1];
5499 	u8 *dst = NULL;
5500 	u8 *next_hop = NULL;
5501 
5502 	if (!info->attrs[NL80211_ATTR_MAC])
5503 		return -EINVAL;
5504 
5505 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5506 		return -EINVAL;
5507 
5508 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5509 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5510 
5511 	if (!rdev->ops->add_mpath)
5512 		return -EOPNOTSUPP;
5513 
5514 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5515 		return -EOPNOTSUPP;
5516 
5517 	return rdev_add_mpath(rdev, dev, dst, next_hop);
5518 }
5519 
5520 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
5521 {
5522 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5523 	struct net_device *dev = info->user_ptr[1];
5524 	u8 *dst = NULL;
5525 
5526 	if (info->attrs[NL80211_ATTR_MAC])
5527 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5528 
5529 	if (!rdev->ops->del_mpath)
5530 		return -EOPNOTSUPP;
5531 
5532 	return rdev_del_mpath(rdev, dev, dst);
5533 }
5534 
5535 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
5536 {
5537 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5538 	int err;
5539 	struct net_device *dev = info->user_ptr[1];
5540 	struct mpath_info pinfo;
5541 	struct sk_buff *msg;
5542 	u8 *dst = NULL;
5543 	u8 mpp[ETH_ALEN];
5544 
5545 	memset(&pinfo, 0, sizeof(pinfo));
5546 
5547 	if (!info->attrs[NL80211_ATTR_MAC])
5548 		return -EINVAL;
5549 
5550 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5551 
5552 	if (!rdev->ops->get_mpp)
5553 		return -EOPNOTSUPP;
5554 
5555 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5556 		return -EOPNOTSUPP;
5557 
5558 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
5559 	if (err)
5560 		return err;
5561 
5562 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5563 	if (!msg)
5564 		return -ENOMEM;
5565 
5566 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5567 			       dev, dst, mpp, &pinfo) < 0) {
5568 		nlmsg_free(msg);
5569 		return -ENOBUFS;
5570 	}
5571 
5572 	return genlmsg_reply(msg, info);
5573 }
5574 
5575 static int nl80211_dump_mpp(struct sk_buff *skb,
5576 			    struct netlink_callback *cb)
5577 {
5578 	struct mpath_info pinfo;
5579 	struct cfg80211_registered_device *rdev;
5580 	struct wireless_dev *wdev;
5581 	u8 dst[ETH_ALEN];
5582 	u8 mpp[ETH_ALEN];
5583 	int path_idx = cb->args[2];
5584 	int err;
5585 
5586 	rtnl_lock();
5587 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5588 	if (err)
5589 		goto out_err;
5590 
5591 	if (!rdev->ops->dump_mpp) {
5592 		err = -EOPNOTSUPP;
5593 		goto out_err;
5594 	}
5595 
5596 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5597 		err = -EOPNOTSUPP;
5598 		goto out_err;
5599 	}
5600 
5601 	while (1) {
5602 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
5603 				    mpp, &pinfo);
5604 		if (err == -ENOENT)
5605 			break;
5606 		if (err)
5607 			goto out_err;
5608 
5609 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5610 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
5611 				       wdev->netdev, dst, mpp,
5612 				       &pinfo) < 0)
5613 			goto out;
5614 
5615 		path_idx++;
5616 	}
5617 
5618  out:
5619 	cb->args[2] = path_idx;
5620 	err = skb->len;
5621  out_err:
5622 	rtnl_unlock();
5623 	return err;
5624 }
5625 
5626 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
5627 {
5628 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5629 	struct net_device *dev = info->user_ptr[1];
5630 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5631 	struct bss_parameters params;
5632 	int err;
5633 
5634 	memset(&params, 0, sizeof(params));
5635 	/* default to not changing parameters */
5636 	params.use_cts_prot = -1;
5637 	params.use_short_preamble = -1;
5638 	params.use_short_slot_time = -1;
5639 	params.ap_isolate = -1;
5640 	params.ht_opmode = -1;
5641 	params.p2p_ctwindow = -1;
5642 	params.p2p_opp_ps = -1;
5643 
5644 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
5645 		params.use_cts_prot =
5646 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
5647 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
5648 		params.use_short_preamble =
5649 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
5650 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
5651 		params.use_short_slot_time =
5652 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
5653 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5654 		params.basic_rates =
5655 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5656 		params.basic_rates_len =
5657 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5658 	}
5659 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
5660 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
5661 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
5662 		params.ht_opmode =
5663 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
5664 
5665 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5666 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5667 			return -EINVAL;
5668 		params.p2p_ctwindow =
5669 			nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5670 		if (params.p2p_ctwindow < 0)
5671 			return -EINVAL;
5672 		if (params.p2p_ctwindow != 0 &&
5673 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5674 			return -EINVAL;
5675 	}
5676 
5677 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5678 		u8 tmp;
5679 
5680 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5681 			return -EINVAL;
5682 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5683 		if (tmp > 1)
5684 			return -EINVAL;
5685 		params.p2p_opp_ps = tmp;
5686 		if (params.p2p_opp_ps &&
5687 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5688 			return -EINVAL;
5689 	}
5690 
5691 	if (!rdev->ops->change_bss)
5692 		return -EOPNOTSUPP;
5693 
5694 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5695 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5696 		return -EOPNOTSUPP;
5697 
5698 	wdev_lock(wdev);
5699 	err = rdev_change_bss(rdev, dev, &params);
5700 	wdev_unlock(wdev);
5701 
5702 	return err;
5703 }
5704 
5705 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
5706 {
5707 	char *data = NULL;
5708 	bool is_indoor;
5709 	enum nl80211_user_reg_hint_type user_reg_hint_type;
5710 	u32 owner_nlportid;
5711 
5712 	/*
5713 	 * You should only get this when cfg80211 hasn't yet initialized
5714 	 * completely when built-in to the kernel right between the time
5715 	 * window between nl80211_init() and regulatory_init(), if that is
5716 	 * even possible.
5717 	 */
5718 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
5719 		return -EINPROGRESS;
5720 
5721 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
5722 		user_reg_hint_type =
5723 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
5724 	else
5725 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
5726 
5727 	switch (user_reg_hint_type) {
5728 	case NL80211_USER_REG_HINT_USER:
5729 	case NL80211_USER_REG_HINT_CELL_BASE:
5730 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5731 			return -EINVAL;
5732 
5733 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5734 		return regulatory_hint_user(data, user_reg_hint_type);
5735 	case NL80211_USER_REG_HINT_INDOOR:
5736 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5737 			owner_nlportid = info->snd_portid;
5738 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
5739 		} else {
5740 			owner_nlportid = 0;
5741 			is_indoor = true;
5742 		}
5743 
5744 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
5745 	default:
5746 		return -EINVAL;
5747 	}
5748 }
5749 
5750 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
5751 {
5752 	return reg_reload_regdb();
5753 }
5754 
5755 static int nl80211_get_mesh_config(struct sk_buff *skb,
5756 				   struct genl_info *info)
5757 {
5758 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5759 	struct net_device *dev = info->user_ptr[1];
5760 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5761 	struct mesh_config cur_params;
5762 	int err = 0;
5763 	void *hdr;
5764 	struct nlattr *pinfoattr;
5765 	struct sk_buff *msg;
5766 
5767 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5768 		return -EOPNOTSUPP;
5769 
5770 	if (!rdev->ops->get_mesh_config)
5771 		return -EOPNOTSUPP;
5772 
5773 	wdev_lock(wdev);
5774 	/* If not connected, get default parameters */
5775 	if (!wdev->mesh_id_len)
5776 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5777 	else
5778 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
5779 	wdev_unlock(wdev);
5780 
5781 	if (err)
5782 		return err;
5783 
5784 	/* Draw up a netlink message to send back */
5785 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5786 	if (!msg)
5787 		return -ENOMEM;
5788 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5789 			     NL80211_CMD_GET_MESH_CONFIG);
5790 	if (!hdr)
5791 		goto out;
5792 	pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
5793 	if (!pinfoattr)
5794 		goto nla_put_failure;
5795 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5796 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5797 			cur_params.dot11MeshRetryTimeout) ||
5798 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5799 			cur_params.dot11MeshConfirmTimeout) ||
5800 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5801 			cur_params.dot11MeshHoldingTimeout) ||
5802 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5803 			cur_params.dot11MeshMaxPeerLinks) ||
5804 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5805 		       cur_params.dot11MeshMaxRetries) ||
5806 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
5807 		       cur_params.dot11MeshTTL) ||
5808 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5809 		       cur_params.element_ttl) ||
5810 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5811 		       cur_params.auto_open_plinks) ||
5812 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5813 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
5814 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5815 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
5816 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5817 			cur_params.path_refresh_time) ||
5818 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5819 			cur_params.min_discovery_timeout) ||
5820 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5821 			cur_params.dot11MeshHWMPactivePathTimeout) ||
5822 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5823 			cur_params.dot11MeshHWMPpreqMinInterval) ||
5824 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5825 			cur_params.dot11MeshHWMPperrMinInterval) ||
5826 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5827 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5828 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5829 		       cur_params.dot11MeshHWMPRootMode) ||
5830 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5831 			cur_params.dot11MeshHWMPRannInterval) ||
5832 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5833 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
5834 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5835 		       cur_params.dot11MeshForwarding) ||
5836 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
5837 			cur_params.rssi_threshold) ||
5838 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
5839 			cur_params.ht_opmode) ||
5840 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5841 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5842 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5843 			cur_params.dot11MeshHWMProotInterval) ||
5844 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5845 			cur_params.dot11MeshHWMPconfirmationInterval) ||
5846 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5847 			cur_params.power_mode) ||
5848 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
5849 			cur_params.dot11MeshAwakeWindowDuration) ||
5850 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5851 			cur_params.plink_timeout))
5852 		goto nla_put_failure;
5853 	nla_nest_end(msg, pinfoattr);
5854 	genlmsg_end(msg, hdr);
5855 	return genlmsg_reply(msg, info);
5856 
5857  nla_put_failure:
5858  out:
5859 	nlmsg_free(msg);
5860 	return -ENOBUFS;
5861 }
5862 
5863 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
5864 	[NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5865 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5866 	[NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5867 	[NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5868 	[NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5869 	[NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
5870 	[NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
5871 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
5872 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
5873 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5874 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5875 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5876 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5877 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
5878 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
5879 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
5880 	[NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
5881 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
5882 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
5883 	[NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
5884 	[NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5885 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
5886 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5887 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
5888 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
5889 	[NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5890 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
5891 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
5892 };
5893 
5894 static const struct nla_policy
5895 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
5896 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
5897 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5898 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
5899 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
5900 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
5901 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
5902 	[NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
5903 				    .len = IEEE80211_MAX_DATA_LEN },
5904 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
5905 };
5906 
5907 static int nl80211_check_bool(const struct nlattr *nla, u8 min, u8 max, bool *out)
5908 {
5909 	u8 val = nla_get_u8(nla);
5910 	if (val < min || val > max)
5911 		return -EINVAL;
5912 	*out = val;
5913 	return 0;
5914 }
5915 
5916 static int nl80211_check_u8(const struct nlattr *nla, u8 min, u8 max, u8 *out)
5917 {
5918 	u8 val = nla_get_u8(nla);
5919 	if (val < min || val > max)
5920 		return -EINVAL;
5921 	*out = val;
5922 	return 0;
5923 }
5924 
5925 static int nl80211_check_u16(const struct nlattr *nla, u16 min, u16 max, u16 *out)
5926 {
5927 	u16 val = nla_get_u16(nla);
5928 	if (val < min || val > max)
5929 		return -EINVAL;
5930 	*out = val;
5931 	return 0;
5932 }
5933 
5934 static int nl80211_check_u32(const struct nlattr *nla, u32 min, u32 max, u32 *out)
5935 {
5936 	u32 val = nla_get_u32(nla);
5937 	if (val < min || val > max)
5938 		return -EINVAL;
5939 	*out = val;
5940 	return 0;
5941 }
5942 
5943 static int nl80211_check_s32(const struct nlattr *nla, s32 min, s32 max, s32 *out)
5944 {
5945 	s32 val = nla_get_s32(nla);
5946 	if (val < min || val > max)
5947 		return -EINVAL;
5948 	*out = val;
5949 	return 0;
5950 }
5951 
5952 static int nl80211_check_power_mode(const struct nlattr *nla,
5953 				    enum nl80211_mesh_power_mode min,
5954 				    enum nl80211_mesh_power_mode max,
5955 				    enum nl80211_mesh_power_mode *out)
5956 {
5957 	u32 val = nla_get_u32(nla);
5958 	if (val < min || val > max)
5959 		return -EINVAL;
5960 	*out = val;
5961 	return 0;
5962 }
5963 
5964 static int nl80211_parse_mesh_config(struct genl_info *info,
5965 				     struct mesh_config *cfg,
5966 				     u32 *mask_out)
5967 {
5968 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
5969 	u32 mask = 0;
5970 	u16 ht_opmode;
5971 
5972 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5973 do {									    \
5974 	if (tb[attr]) {							    \
5975 		if (fn(tb[attr], min, max, &cfg->param))		    \
5976 			return -EINVAL;					    \
5977 		mask |= (1 << (attr - 1));				    \
5978 	}								    \
5979 } while (0)
5980 
5981 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
5982 		return -EINVAL;
5983 	if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
5984 			     info->attrs[NL80211_ATTR_MESH_CONFIG],
5985 			     nl80211_meshconf_params_policy, info->extack))
5986 		return -EINVAL;
5987 
5988 	/* This makes sure that there aren't more than 32 mesh config
5989 	 * parameters (otherwise our bitfield scheme would not work.) */
5990 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5991 
5992 	/* Fill in the params struct */
5993 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
5994 				  mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5995 				  nl80211_check_u16);
5996 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
5997 				  mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5998 				  nl80211_check_u16);
5999 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
6000 				  mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
6001 				  nl80211_check_u16);
6002 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
6003 				  mask, NL80211_MESHCONF_MAX_PEER_LINKS,
6004 				  nl80211_check_u16);
6005 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
6006 				  mask, NL80211_MESHCONF_MAX_RETRIES,
6007 				  nl80211_check_u8);
6008 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
6009 				  mask, NL80211_MESHCONF_TTL, nl80211_check_u8);
6010 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
6011 				  mask, NL80211_MESHCONF_ELEMENT_TTL,
6012 				  nl80211_check_u8);
6013 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
6014 				  mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6015 				  nl80211_check_bool);
6016 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6017 				  1, 255, mask,
6018 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6019 				  nl80211_check_u32);
6020 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
6021 				  mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6022 				  nl80211_check_u8);
6023 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
6024 				  mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
6025 				  nl80211_check_u32);
6026 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
6027 				  mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6028 				  nl80211_check_u16);
6029 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6030 				  1, 65535, mask,
6031 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6032 				  nl80211_check_u32);
6033 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
6034 				  1, 65535, mask,
6035 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6036 				  nl80211_check_u16);
6037 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
6038 				  1, 65535, mask,
6039 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6040 				  nl80211_check_u16);
6041 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6042 				  dot11MeshHWMPnetDiameterTraversalTime,
6043 				  1, 65535, mask,
6044 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6045 				  nl80211_check_u16);
6046 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
6047 				  mask, NL80211_MESHCONF_HWMP_ROOTMODE,
6048 				  nl80211_check_u8);
6049 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
6050 				  mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6051 				  nl80211_check_u16);
6052 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6053 				  dot11MeshGateAnnouncementProtocol, 0, 1,
6054 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6055 				  nl80211_check_bool);
6056 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
6057 				  mask, NL80211_MESHCONF_FORWARDING,
6058 				  nl80211_check_bool);
6059 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
6060 				  mask, NL80211_MESHCONF_RSSI_THRESHOLD,
6061 				  nl80211_check_s32);
6062 	/*
6063 	 * Check HT operation mode based on
6064 	 * IEEE 802.11 2012 8.4.2.59 HT Operation element.
6065 	 */
6066 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6067 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6068 
6069 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6070 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6071 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6072 			return -EINVAL;
6073 
6074 		if ((ht_opmode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT) &&
6075 		    (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6076 			return -EINVAL;
6077 
6078 		switch (ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION) {
6079 		case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
6080 		case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
6081 			if (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT)
6082 				return -EINVAL;
6083 			break;
6084 		case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
6085 		case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
6086 			if (!(ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6087 				return -EINVAL;
6088 			break;
6089 		}
6090 		cfg->ht_opmode = ht_opmode;
6091 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6092 	}
6093 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
6094 				  1, 65535, mask,
6095 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6096 				  nl80211_check_u32);
6097 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
6098 				  mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6099 				  nl80211_check_u16);
6100 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6101 				  dot11MeshHWMPconfirmationInterval,
6102 				  1, 65535, mask,
6103 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6104 				  nl80211_check_u16);
6105 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
6106 				  NL80211_MESH_POWER_ACTIVE,
6107 				  NL80211_MESH_POWER_MAX,
6108 				  mask, NL80211_MESHCONF_POWER_MODE,
6109 				  nl80211_check_power_mode);
6110 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
6111 				  0, 65535, mask,
6112 				  NL80211_MESHCONF_AWAKE_WINDOW, nl80211_check_u16);
6113 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
6114 				  mask, NL80211_MESHCONF_PLINK_TIMEOUT,
6115 				  nl80211_check_u32);
6116 	if (mask_out)
6117 		*mask_out = mask;
6118 
6119 	return 0;
6120 
6121 #undef FILL_IN_MESH_PARAM_IF_SET
6122 }
6123 
6124 static int nl80211_parse_mesh_setup(struct genl_info *info,
6125 				     struct mesh_setup *setup)
6126 {
6127 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6128 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6129 
6130 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6131 		return -EINVAL;
6132 	if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
6133 			     info->attrs[NL80211_ATTR_MESH_SETUP],
6134 			     nl80211_mesh_setup_params_policy, info->extack))
6135 		return -EINVAL;
6136 
6137 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6138 		setup->sync_method =
6139 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6140 		 IEEE80211_SYNC_METHOD_VENDOR :
6141 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6142 
6143 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6144 		setup->path_sel_proto =
6145 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6146 		 IEEE80211_PATH_PROTOCOL_VENDOR :
6147 		 IEEE80211_PATH_PROTOCOL_HWMP;
6148 
6149 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6150 		setup->path_metric =
6151 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6152 		 IEEE80211_PATH_METRIC_VENDOR :
6153 		 IEEE80211_PATH_METRIC_AIRTIME;
6154 
6155 	if (tb[NL80211_MESH_SETUP_IE]) {
6156 		struct nlattr *ieattr =
6157 			tb[NL80211_MESH_SETUP_IE];
6158 		if (!is_valid_ie_attr(ieattr))
6159 			return -EINVAL;
6160 		setup->ie = nla_data(ieattr);
6161 		setup->ie_len = nla_len(ieattr);
6162 	}
6163 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6164 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6165 		return -EINVAL;
6166 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6167 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6168 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6169 	if (setup->is_secure)
6170 		setup->user_mpm = true;
6171 
6172 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6173 		if (!setup->user_mpm)
6174 			return -EINVAL;
6175 		setup->auth_id =
6176 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6177 	}
6178 
6179 	return 0;
6180 }
6181 
6182 static int nl80211_update_mesh_config(struct sk_buff *skb,
6183 				      struct genl_info *info)
6184 {
6185 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6186 	struct net_device *dev = info->user_ptr[1];
6187 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6188 	struct mesh_config cfg;
6189 	u32 mask;
6190 	int err;
6191 
6192 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6193 		return -EOPNOTSUPP;
6194 
6195 	if (!rdev->ops->update_mesh_config)
6196 		return -EOPNOTSUPP;
6197 
6198 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
6199 	if (err)
6200 		return err;
6201 
6202 	wdev_lock(wdev);
6203 	if (!wdev->mesh_id_len)
6204 		err = -ENOLINK;
6205 
6206 	if (!err)
6207 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6208 
6209 	wdev_unlock(wdev);
6210 
6211 	return err;
6212 }
6213 
6214 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6215 			      struct sk_buff *msg)
6216 {
6217 	struct nlattr *nl_reg_rules;
6218 	unsigned int i;
6219 
6220 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6221 	    (regdom->dfs_region &&
6222 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6223 		goto nla_put_failure;
6224 
6225 	nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
6226 	if (!nl_reg_rules)
6227 		goto nla_put_failure;
6228 
6229 	for (i = 0; i < regdom->n_reg_rules; i++) {
6230 		struct nlattr *nl_reg_rule;
6231 		const struct ieee80211_reg_rule *reg_rule;
6232 		const struct ieee80211_freq_range *freq_range;
6233 		const struct ieee80211_power_rule *power_rule;
6234 		unsigned int max_bandwidth_khz;
6235 
6236 		reg_rule = &regdom->reg_rules[i];
6237 		freq_range = &reg_rule->freq_range;
6238 		power_rule = &reg_rule->power_rule;
6239 
6240 		nl_reg_rule = nla_nest_start(msg, i);
6241 		if (!nl_reg_rule)
6242 			goto nla_put_failure;
6243 
6244 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
6245 		if (!max_bandwidth_khz)
6246 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6247 								  reg_rule);
6248 
6249 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6250 				reg_rule->flags) ||
6251 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6252 				freq_range->start_freq_khz) ||
6253 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6254 				freq_range->end_freq_khz) ||
6255 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6256 				max_bandwidth_khz) ||
6257 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6258 				power_rule->max_antenna_gain) ||
6259 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6260 				power_rule->max_eirp) ||
6261 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6262 				reg_rule->dfs_cac_ms))
6263 			goto nla_put_failure;
6264 
6265 		nla_nest_end(msg, nl_reg_rule);
6266 	}
6267 
6268 	nla_nest_end(msg, nl_reg_rules);
6269 	return 0;
6270 
6271 nla_put_failure:
6272 	return -EMSGSIZE;
6273 }
6274 
6275 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6276 {
6277 	const struct ieee80211_regdomain *regdom = NULL;
6278 	struct cfg80211_registered_device *rdev;
6279 	struct wiphy *wiphy = NULL;
6280 	struct sk_buff *msg;
6281 	void *hdr;
6282 
6283 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6284 	if (!msg)
6285 		return -ENOBUFS;
6286 
6287 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6288 			     NL80211_CMD_GET_REG);
6289 	if (!hdr)
6290 		goto put_failure;
6291 
6292 	if (info->attrs[NL80211_ATTR_WIPHY]) {
6293 		bool self_managed;
6294 
6295 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6296 		if (IS_ERR(rdev)) {
6297 			nlmsg_free(msg);
6298 			return PTR_ERR(rdev);
6299 		}
6300 
6301 		wiphy = &rdev->wiphy;
6302 		self_managed = wiphy->regulatory_flags &
6303 			       REGULATORY_WIPHY_SELF_MANAGED;
6304 		regdom = get_wiphy_regdom(wiphy);
6305 
6306 		/* a self-managed-reg device must have a private regdom */
6307 		if (WARN_ON(!regdom && self_managed)) {
6308 			nlmsg_free(msg);
6309 			return -EINVAL;
6310 		}
6311 
6312 		if (regdom &&
6313 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6314 			goto nla_put_failure;
6315 	}
6316 
6317 	if (!wiphy && reg_last_request_cell_base() &&
6318 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6319 			NL80211_USER_REG_HINT_CELL_BASE))
6320 		goto nla_put_failure;
6321 
6322 	rcu_read_lock();
6323 
6324 	if (!regdom)
6325 		regdom = rcu_dereference(cfg80211_regdomain);
6326 
6327 	if (nl80211_put_regdom(regdom, msg))
6328 		goto nla_put_failure_rcu;
6329 
6330 	rcu_read_unlock();
6331 
6332 	genlmsg_end(msg, hdr);
6333 	return genlmsg_reply(msg, info);
6334 
6335 nla_put_failure_rcu:
6336 	rcu_read_unlock();
6337 nla_put_failure:
6338 put_failure:
6339 	nlmsg_free(msg);
6340 	return -EMSGSIZE;
6341 }
6342 
6343 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6344 			       u32 seq, int flags, struct wiphy *wiphy,
6345 			       const struct ieee80211_regdomain *regdom)
6346 {
6347 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6348 				   NL80211_CMD_GET_REG);
6349 
6350 	if (!hdr)
6351 		return -1;
6352 
6353 	genl_dump_check_consistent(cb, hdr);
6354 
6355 	if (nl80211_put_regdom(regdom, msg))
6356 		goto nla_put_failure;
6357 
6358 	if (!wiphy && reg_last_request_cell_base() &&
6359 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6360 			NL80211_USER_REG_HINT_CELL_BASE))
6361 		goto nla_put_failure;
6362 
6363 	if (wiphy &&
6364 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6365 		goto nla_put_failure;
6366 
6367 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
6368 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
6369 		goto nla_put_failure;
6370 
6371 	genlmsg_end(msg, hdr);
6372 	return 0;
6373 
6374 nla_put_failure:
6375 	genlmsg_cancel(msg, hdr);
6376 	return -EMSGSIZE;
6377 }
6378 
6379 static int nl80211_get_reg_dump(struct sk_buff *skb,
6380 				struct netlink_callback *cb)
6381 {
6382 	const struct ieee80211_regdomain *regdom = NULL;
6383 	struct cfg80211_registered_device *rdev;
6384 	int err, reg_idx, start = cb->args[2];
6385 
6386 	rtnl_lock();
6387 
6388 	if (cfg80211_regdomain && start == 0) {
6389 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6390 					  NLM_F_MULTI, NULL,
6391 					  rtnl_dereference(cfg80211_regdomain));
6392 		if (err < 0)
6393 			goto out_err;
6394 	}
6395 
6396 	/* the global regdom is idx 0 */
6397 	reg_idx = 1;
6398 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
6399 		regdom = get_wiphy_regdom(&rdev->wiphy);
6400 		if (!regdom)
6401 			continue;
6402 
6403 		if (++reg_idx <= start)
6404 			continue;
6405 
6406 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6407 					  NLM_F_MULTI, &rdev->wiphy, regdom);
6408 		if (err < 0) {
6409 			reg_idx--;
6410 			break;
6411 		}
6412 	}
6413 
6414 	cb->args[2] = reg_idx;
6415 	err = skb->len;
6416 out_err:
6417 	rtnl_unlock();
6418 	return err;
6419 }
6420 
6421 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
6422 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
6423 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
6424 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
6425 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
6426 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
6427 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
6428 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
6429 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
6430 };
6431 
6432 static int parse_reg_rule(struct nlattr *tb[],
6433 	struct ieee80211_reg_rule *reg_rule)
6434 {
6435 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
6436 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
6437 
6438 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
6439 		return -EINVAL;
6440 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
6441 		return -EINVAL;
6442 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
6443 		return -EINVAL;
6444 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6445 		return -EINVAL;
6446 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6447 		return -EINVAL;
6448 
6449 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
6450 
6451 	freq_range->start_freq_khz =
6452 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
6453 	freq_range->end_freq_khz =
6454 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
6455 	freq_range->max_bandwidth_khz =
6456 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
6457 
6458 	power_rule->max_eirp =
6459 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
6460 
6461 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
6462 		power_rule->max_antenna_gain =
6463 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
6464 
6465 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
6466 		reg_rule->dfs_cac_ms =
6467 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
6468 
6469 	return 0;
6470 }
6471 
6472 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
6473 {
6474 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
6475 	struct nlattr *nl_reg_rule;
6476 	char *alpha2;
6477 	int rem_reg_rules, r;
6478 	u32 num_rules = 0, rule_idx = 0, size_of_regd;
6479 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
6480 	struct ieee80211_regdomain *rd;
6481 
6482 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6483 		return -EINVAL;
6484 
6485 	if (!info->attrs[NL80211_ATTR_REG_RULES])
6486 		return -EINVAL;
6487 
6488 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6489 
6490 	if (info->attrs[NL80211_ATTR_DFS_REGION])
6491 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
6492 
6493 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6494 			    rem_reg_rules) {
6495 		num_rules++;
6496 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
6497 			return -EINVAL;
6498 	}
6499 
6500 	if (!reg_is_valid_request(alpha2))
6501 		return -EINVAL;
6502 
6503 	size_of_regd = sizeof(struct ieee80211_regdomain) +
6504 		       num_rules * sizeof(struct ieee80211_reg_rule);
6505 
6506 	rd = kzalloc(size_of_regd, GFP_KERNEL);
6507 	if (!rd)
6508 		return -ENOMEM;
6509 
6510 	rd->n_reg_rules = num_rules;
6511 	rd->alpha2[0] = alpha2[0];
6512 	rd->alpha2[1] = alpha2[1];
6513 
6514 	/*
6515 	 * Disable DFS master mode if the DFS region was
6516 	 * not supported or known on this kernel.
6517 	 */
6518 	if (reg_supported_dfs_region(dfs_region))
6519 		rd->dfs_region = dfs_region;
6520 
6521 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6522 			    rem_reg_rules) {
6523 		r = nla_parse_nested(tb, NL80211_REG_RULE_ATTR_MAX,
6524 				     nl_reg_rule, reg_rule_policy,
6525 				     info->extack);
6526 		if (r)
6527 			goto bad_reg;
6528 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
6529 		if (r)
6530 			goto bad_reg;
6531 
6532 		rule_idx++;
6533 
6534 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
6535 			r = -EINVAL;
6536 			goto bad_reg;
6537 		}
6538 	}
6539 
6540 	/* set_regdom takes ownership of rd */
6541 	return set_regdom(rd, REGD_SOURCE_CRDA);
6542  bad_reg:
6543 	kfree(rd);
6544 	return r;
6545 }
6546 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
6547 
6548 static int validate_scan_freqs(struct nlattr *freqs)
6549 {
6550 	struct nlattr *attr1, *attr2;
6551 	int n_channels = 0, tmp1, tmp2;
6552 
6553 	nla_for_each_nested(attr1, freqs, tmp1)
6554 		if (nla_len(attr1) != sizeof(u32))
6555 			return 0;
6556 
6557 	nla_for_each_nested(attr1, freqs, tmp1) {
6558 		n_channels++;
6559 		/*
6560 		 * Some hardware has a limited channel list for
6561 		 * scanning, and it is pretty much nonsensical
6562 		 * to scan for a channel twice, so disallow that
6563 		 * and don't require drivers to check that the
6564 		 * channel list they get isn't longer than what
6565 		 * they can scan, as long as they can scan all
6566 		 * the channels they registered at once.
6567 		 */
6568 		nla_for_each_nested(attr2, freqs, tmp2)
6569 			if (attr1 != attr2 &&
6570 			    nla_get_u32(attr1) == nla_get_u32(attr2))
6571 				return 0;
6572 	}
6573 
6574 	return n_channels;
6575 }
6576 
6577 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
6578 {
6579 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
6580 }
6581 
6582 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
6583 			    struct cfg80211_bss_selection *bss_select)
6584 {
6585 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
6586 	struct nlattr *nest;
6587 	int err;
6588 	bool found = false;
6589 	int i;
6590 
6591 	/* only process one nested attribute */
6592 	nest = nla_data(nla);
6593 	if (!nla_ok(nest, nla_len(nest)))
6594 		return -EINVAL;
6595 
6596 	err = nla_parse_nested(attr, NL80211_BSS_SELECT_ATTR_MAX, nest,
6597 			       nl80211_bss_select_policy, NULL);
6598 	if (err)
6599 		return err;
6600 
6601 	/* only one attribute may be given */
6602 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
6603 		if (attr[i]) {
6604 			if (found)
6605 				return -EINVAL;
6606 			found = true;
6607 		}
6608 	}
6609 
6610 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
6611 
6612 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
6613 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
6614 
6615 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
6616 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
6617 		bss_select->param.band_pref =
6618 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
6619 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
6620 			return -EINVAL;
6621 	}
6622 
6623 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
6624 		struct nl80211_bss_select_rssi_adjust *adj_param;
6625 
6626 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
6627 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
6628 		bss_select->param.adjust.band = adj_param->band;
6629 		bss_select->param.adjust.delta = adj_param->delta;
6630 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
6631 			return -EINVAL;
6632 	}
6633 
6634 	/* user-space did not provide behaviour attribute */
6635 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
6636 		return -EINVAL;
6637 
6638 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
6639 		return -EINVAL;
6640 
6641 	return 0;
6642 }
6643 
6644 static int nl80211_parse_random_mac(struct nlattr **attrs,
6645 				    u8 *mac_addr, u8 *mac_addr_mask)
6646 {
6647 	int i;
6648 
6649 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
6650 		eth_zero_addr(mac_addr);
6651 		eth_zero_addr(mac_addr_mask);
6652 		mac_addr[0] = 0x2;
6653 		mac_addr_mask[0] = 0x3;
6654 
6655 		return 0;
6656 	}
6657 
6658 	/* need both or none */
6659 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
6660 		return -EINVAL;
6661 
6662 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
6663 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
6664 
6665 	/* don't allow or configure an mcast address */
6666 	if (!is_multicast_ether_addr(mac_addr_mask) ||
6667 	    is_multicast_ether_addr(mac_addr))
6668 		return -EINVAL;
6669 
6670 	/*
6671 	 * allow users to pass a MAC address that has bits set outside
6672 	 * of the mask, but don't bother drivers with having to deal
6673 	 * with such bits
6674 	 */
6675 	for (i = 0; i < ETH_ALEN; i++)
6676 		mac_addr[i] &= mac_addr_mask[i];
6677 
6678 	return 0;
6679 }
6680 
6681 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
6682 {
6683 	ASSERT_WDEV_LOCK(wdev);
6684 
6685 	if (!cfg80211_beaconing_iface_active(wdev))
6686 		return true;
6687 
6688 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
6689 		return true;
6690 
6691 	return regulatory_pre_cac_allowed(wdev->wiphy);
6692 }
6693 
6694 static int
6695 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
6696 			 void *request, struct nlattr **attrs,
6697 			 bool is_sched_scan)
6698 {
6699 	u8 *mac_addr, *mac_addr_mask;
6700 	u32 *flags;
6701 	enum nl80211_feature_flags randomness_flag;
6702 
6703 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
6704 		return 0;
6705 
6706 	if (is_sched_scan) {
6707 		struct cfg80211_sched_scan_request *req = request;
6708 
6709 		randomness_flag = wdev ?
6710 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
6711 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6712 		flags = &req->flags;
6713 		mac_addr = req->mac_addr;
6714 		mac_addr_mask = req->mac_addr_mask;
6715 	} else {
6716 		struct cfg80211_scan_request *req = request;
6717 
6718 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
6719 		flags = &req->flags;
6720 		mac_addr = req->mac_addr;
6721 		mac_addr_mask = req->mac_addr_mask;
6722 	}
6723 
6724 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
6725 
6726 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6727 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
6728 	    ((*flags & NL80211_SCAN_FLAG_LOW_SPAN) &&
6729 	     !wiphy_ext_feature_isset(wiphy,
6730 				      NL80211_EXT_FEATURE_LOW_SPAN_SCAN)) ||
6731 	    ((*flags & NL80211_SCAN_FLAG_LOW_POWER) &&
6732 	     !wiphy_ext_feature_isset(wiphy,
6733 				      NL80211_EXT_FEATURE_LOW_POWER_SCAN)) ||
6734 	    ((*flags & NL80211_SCAN_FLAG_HIGH_ACCURACY) &&
6735 	     !wiphy_ext_feature_isset(wiphy,
6736 				      NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN)))
6737 		return -EOPNOTSUPP;
6738 
6739 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6740 		int err;
6741 
6742 		if (!(wiphy->features & randomness_flag) ||
6743 		    (wdev && wdev->current_bss))
6744 			return -EOPNOTSUPP;
6745 
6746 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
6747 		if (err)
6748 			return err;
6749 	}
6750 
6751 	if ((*flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME) &&
6752 	    !wiphy_ext_feature_isset(wiphy,
6753 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME))
6754 		return -EOPNOTSUPP;
6755 
6756 	if ((*flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP) &&
6757 	   !wiphy_ext_feature_isset(wiphy,
6758 				    NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP))
6759 		return -EOPNOTSUPP;
6760 
6761 	if ((*flags & NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
6762 	    !wiphy_ext_feature_isset(wiphy,
6763 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION))
6764 		return -EOPNOTSUPP;
6765 
6766 	if ((*flags & NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE) &&
6767 	    !wiphy_ext_feature_isset(wiphy,
6768 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE))
6769 		return -EOPNOTSUPP;
6770 
6771 	return 0;
6772 }
6773 
6774 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
6775 {
6776 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6777 	struct wireless_dev *wdev = info->user_ptr[1];
6778 	struct cfg80211_scan_request *request;
6779 	struct nlattr *attr;
6780 	struct wiphy *wiphy;
6781 	int err, tmp, n_ssids = 0, n_channels, i;
6782 	size_t ie_len;
6783 
6784 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6785 		return -EINVAL;
6786 
6787 	wiphy = &rdev->wiphy;
6788 
6789 	if (wdev->iftype == NL80211_IFTYPE_NAN)
6790 		return -EOPNOTSUPP;
6791 
6792 	if (!rdev->ops->scan)
6793 		return -EOPNOTSUPP;
6794 
6795 	if (rdev->scan_req || rdev->scan_msg) {
6796 		err = -EBUSY;
6797 		goto unlock;
6798 	}
6799 
6800 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6801 		n_channels = validate_scan_freqs(
6802 				info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
6803 		if (!n_channels) {
6804 			err = -EINVAL;
6805 			goto unlock;
6806 		}
6807 	} else {
6808 		n_channels = ieee80211_get_num_supported_channels(wiphy);
6809 	}
6810 
6811 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
6812 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
6813 			n_ssids++;
6814 
6815 	if (n_ssids > wiphy->max_scan_ssids) {
6816 		err = -EINVAL;
6817 		goto unlock;
6818 	}
6819 
6820 	if (info->attrs[NL80211_ATTR_IE])
6821 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6822 	else
6823 		ie_len = 0;
6824 
6825 	if (ie_len > wiphy->max_scan_ie_len) {
6826 		err = -EINVAL;
6827 		goto unlock;
6828 	}
6829 
6830 	request = kzalloc(sizeof(*request)
6831 			+ sizeof(*request->ssids) * n_ssids
6832 			+ sizeof(*request->channels) * n_channels
6833 			+ ie_len, GFP_KERNEL);
6834 	if (!request) {
6835 		err = -ENOMEM;
6836 		goto unlock;
6837 	}
6838 
6839 	if (n_ssids)
6840 		request->ssids = (void *)&request->channels[n_channels];
6841 	request->n_ssids = n_ssids;
6842 	if (ie_len) {
6843 		if (n_ssids)
6844 			request->ie = (void *)(request->ssids + n_ssids);
6845 		else
6846 			request->ie = (void *)(request->channels + n_channels);
6847 	}
6848 
6849 	i = 0;
6850 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6851 		/* user specified, bail out if channel not found */
6852 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
6853 			struct ieee80211_channel *chan;
6854 
6855 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6856 
6857 			if (!chan) {
6858 				err = -EINVAL;
6859 				goto out_free;
6860 			}
6861 
6862 			/* ignore disabled channels */
6863 			if (chan->flags & IEEE80211_CHAN_DISABLED)
6864 				continue;
6865 
6866 			request->channels[i] = chan;
6867 			i++;
6868 		}
6869 	} else {
6870 		enum nl80211_band band;
6871 
6872 		/* all channels */
6873 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
6874 			int j;
6875 
6876 			if (!wiphy->bands[band])
6877 				continue;
6878 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
6879 				struct ieee80211_channel *chan;
6880 
6881 				chan = &wiphy->bands[band]->channels[j];
6882 
6883 				if (chan->flags & IEEE80211_CHAN_DISABLED)
6884 					continue;
6885 
6886 				request->channels[i] = chan;
6887 				i++;
6888 			}
6889 		}
6890 	}
6891 
6892 	if (!i) {
6893 		err = -EINVAL;
6894 		goto out_free;
6895 	}
6896 
6897 	request->n_channels = i;
6898 
6899 	wdev_lock(wdev);
6900 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
6901 		struct ieee80211_channel *chan;
6902 
6903 		if (request->n_channels != 1) {
6904 			wdev_unlock(wdev);
6905 			err = -EBUSY;
6906 			goto out_free;
6907 		}
6908 
6909 		chan = request->channels[0];
6910 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
6911 			wdev_unlock(wdev);
6912 			err = -EBUSY;
6913 			goto out_free;
6914 		}
6915 	}
6916 	wdev_unlock(wdev);
6917 
6918 	i = 0;
6919 	if (n_ssids) {
6920 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
6921 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
6922 				err = -EINVAL;
6923 				goto out_free;
6924 			}
6925 			request->ssids[i].ssid_len = nla_len(attr);
6926 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
6927 			i++;
6928 		}
6929 	}
6930 
6931 	if (info->attrs[NL80211_ATTR_IE]) {
6932 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6933 		memcpy((void *)request->ie,
6934 		       nla_data(info->attrs[NL80211_ATTR_IE]),
6935 		       request->ie_len);
6936 	}
6937 
6938 	for (i = 0; i < NUM_NL80211_BANDS; i++)
6939 		if (wiphy->bands[i])
6940 			request->rates[i] =
6941 				(1 << wiphy->bands[i]->n_bitrates) - 1;
6942 
6943 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
6944 		nla_for_each_nested(attr,
6945 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
6946 				    tmp) {
6947 			enum nl80211_band band = nla_type(attr);
6948 
6949 			if (band < 0 || band >= NUM_NL80211_BANDS) {
6950 				err = -EINVAL;
6951 				goto out_free;
6952 			}
6953 
6954 			if (!wiphy->bands[band])
6955 				continue;
6956 
6957 			err = ieee80211_get_ratemask(wiphy->bands[band],
6958 						     nla_data(attr),
6959 						     nla_len(attr),
6960 						     &request->rates[band]);
6961 			if (err)
6962 				goto out_free;
6963 		}
6964 	}
6965 
6966 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
6967 		if (!wiphy_ext_feature_isset(wiphy,
6968 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
6969 			err = -EOPNOTSUPP;
6970 			goto out_free;
6971 		}
6972 
6973 		request->duration =
6974 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
6975 		request->duration_mandatory =
6976 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
6977 	}
6978 
6979 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
6980 				       false);
6981 	if (err)
6982 		goto out_free;
6983 
6984 	request->no_cck =
6985 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6986 
6987 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
6988 	 * BSSID to scan for. This was problematic because that same attribute
6989 	 * was already used for another purpose (local random MAC address). The
6990 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
6991 	 * compatibility with older userspace components, also use the
6992 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
6993 	 * the specific BSSID use case instead of the random MAC address
6994 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
6995 	 */
6996 	if (info->attrs[NL80211_ATTR_BSSID])
6997 		memcpy(request->bssid,
6998 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
6999 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7000 		 info->attrs[NL80211_ATTR_MAC])
7001 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7002 		       ETH_ALEN);
7003 	else
7004 		eth_broadcast_addr(request->bssid);
7005 
7006 	request->wdev = wdev;
7007 	request->wiphy = &rdev->wiphy;
7008 	request->scan_start = jiffies;
7009 
7010 	rdev->scan_req = request;
7011 	err = rdev_scan(rdev, request);
7012 
7013 	if (!err) {
7014 		nl80211_send_scan_start(rdev, wdev);
7015 		if (wdev->netdev)
7016 			dev_hold(wdev->netdev);
7017 	} else {
7018  out_free:
7019 		rdev->scan_req = NULL;
7020 		kfree(request);
7021 	}
7022 
7023  unlock:
7024 	return err;
7025 }
7026 
7027 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7028 {
7029 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7030 	struct wireless_dev *wdev = info->user_ptr[1];
7031 
7032 	if (!rdev->ops->abort_scan)
7033 		return -EOPNOTSUPP;
7034 
7035 	if (rdev->scan_msg)
7036 		return 0;
7037 
7038 	if (!rdev->scan_req)
7039 		return -ENOENT;
7040 
7041 	rdev_abort_scan(rdev, wdev);
7042 	return 0;
7043 }
7044 
7045 static int
7046 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7047 			       struct cfg80211_sched_scan_request *request,
7048 			       struct nlattr **attrs)
7049 {
7050 	int tmp, err, i = 0;
7051 	struct nlattr *attr;
7052 
7053 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7054 		u32 interval;
7055 
7056 		/*
7057 		 * If scan plans are not specified,
7058 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7059 		 * case one scan plan will be set with the specified scan
7060 		 * interval and infinite number of iterations.
7061 		 */
7062 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7063 		if (!interval)
7064 			return -EINVAL;
7065 
7066 		request->scan_plans[0].interval =
7067 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
7068 		if (!request->scan_plans[0].interval)
7069 			return -EINVAL;
7070 
7071 		if (request->scan_plans[0].interval >
7072 		    wiphy->max_sched_scan_plan_interval)
7073 			request->scan_plans[0].interval =
7074 				wiphy->max_sched_scan_plan_interval;
7075 
7076 		return 0;
7077 	}
7078 
7079 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7080 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7081 
7082 		if (WARN_ON(i >= n_plans))
7083 			return -EINVAL;
7084 
7085 		err = nla_parse_nested(plan, NL80211_SCHED_SCAN_PLAN_MAX,
7086 				       attr, nl80211_plan_policy, NULL);
7087 		if (err)
7088 			return err;
7089 
7090 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7091 			return -EINVAL;
7092 
7093 		request->scan_plans[i].interval =
7094 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7095 		if (!request->scan_plans[i].interval ||
7096 		    request->scan_plans[i].interval >
7097 		    wiphy->max_sched_scan_plan_interval)
7098 			return -EINVAL;
7099 
7100 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7101 			request->scan_plans[i].iterations =
7102 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7103 			if (!request->scan_plans[i].iterations ||
7104 			    (request->scan_plans[i].iterations >
7105 			     wiphy->max_sched_scan_plan_iterations))
7106 				return -EINVAL;
7107 		} else if (i < n_plans - 1) {
7108 			/*
7109 			 * All scan plans but the last one must specify
7110 			 * a finite number of iterations
7111 			 */
7112 			return -EINVAL;
7113 		}
7114 
7115 		i++;
7116 	}
7117 
7118 	/*
7119 	 * The last scan plan must not specify the number of
7120 	 * iterations, it is supposed to run infinitely
7121 	 */
7122 	if (request->scan_plans[n_plans - 1].iterations)
7123 		return  -EINVAL;
7124 
7125 	return 0;
7126 }
7127 
7128 static struct cfg80211_sched_scan_request *
7129 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7130 			 struct nlattr **attrs, int max_match_sets)
7131 {
7132 	struct cfg80211_sched_scan_request *request;
7133 	struct nlattr *attr;
7134 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7135 	enum nl80211_band band;
7136 	size_t ie_len;
7137 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7138 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7139 
7140 	if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
7141 		return ERR_PTR(-EINVAL);
7142 
7143 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7144 		n_channels = validate_scan_freqs(
7145 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7146 		if (!n_channels)
7147 			return ERR_PTR(-EINVAL);
7148 	} else {
7149 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7150 	}
7151 
7152 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
7153 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7154 				    tmp)
7155 			n_ssids++;
7156 
7157 	if (n_ssids > wiphy->max_sched_scan_ssids)
7158 		return ERR_PTR(-EINVAL);
7159 
7160 	/*
7161 	 * First, count the number of 'real' matchsets. Due to an issue with
7162 	 * the old implementation, matchsets containing only the RSSI attribute
7163 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7164 	 * RSSI for all matchsets, rather than their own matchset for reporting
7165 	 * all APs with a strong RSSI. This is needed to be compatible with
7166 	 * older userspace that treated a matchset with only the RSSI as the
7167 	 * global RSSI for all other matchsets - if there are other matchsets.
7168 	 */
7169 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7170 		nla_for_each_nested(attr,
7171 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7172 				    tmp) {
7173 			struct nlattr *rssi;
7174 
7175 			err = nla_parse_nested(tb,
7176 					       NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7177 					       attr, nl80211_match_policy,
7178 					       NULL);
7179 			if (err)
7180 				return ERR_PTR(err);
7181 
7182 			/* SSID and BSSID are mutually exclusive */
7183 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7184 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7185 				return ERR_PTR(-EINVAL);
7186 
7187 			/* add other standalone attributes here */
7188 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7189 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7190 				n_match_sets++;
7191 				continue;
7192 			}
7193 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7194 			if (rssi)
7195 				default_match_rssi = nla_get_s32(rssi);
7196 		}
7197 	}
7198 
7199 	/* However, if there's no other matchset, add the RSSI one */
7200 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7201 		n_match_sets = 1;
7202 
7203 	if (n_match_sets > max_match_sets)
7204 		return ERR_PTR(-EINVAL);
7205 
7206 	if (attrs[NL80211_ATTR_IE])
7207 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7208 	else
7209 		ie_len = 0;
7210 
7211 	if (ie_len > wiphy->max_sched_scan_ie_len)
7212 		return ERR_PTR(-EINVAL);
7213 
7214 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7215 		/*
7216 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7217 		 * each scan plan already specifies its own interval
7218 		 */
7219 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7220 			return ERR_PTR(-EINVAL);
7221 
7222 		nla_for_each_nested(attr,
7223 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7224 			n_plans++;
7225 	} else {
7226 		/*
7227 		 * The scan interval attribute is kept for backward
7228 		 * compatibility. If no scan plans are specified and sched scan
7229 		 * interval is specified, one scan plan will be set with this
7230 		 * scan interval and infinite number of iterations.
7231 		 */
7232 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7233 			return ERR_PTR(-EINVAL);
7234 
7235 		n_plans = 1;
7236 	}
7237 
7238 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7239 		return ERR_PTR(-EINVAL);
7240 
7241 	if (!wiphy_ext_feature_isset(
7242 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7243 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7244 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7245 		return ERR_PTR(-EINVAL);
7246 
7247 	request = kzalloc(sizeof(*request)
7248 			+ sizeof(*request->ssids) * n_ssids
7249 			+ sizeof(*request->match_sets) * n_match_sets
7250 			+ sizeof(*request->scan_plans) * n_plans
7251 			+ sizeof(*request->channels) * n_channels
7252 			+ ie_len, GFP_KERNEL);
7253 	if (!request)
7254 		return ERR_PTR(-ENOMEM);
7255 
7256 	if (n_ssids)
7257 		request->ssids = (void *)&request->channels[n_channels];
7258 	request->n_ssids = n_ssids;
7259 	if (ie_len) {
7260 		if (n_ssids)
7261 			request->ie = (void *)(request->ssids + n_ssids);
7262 		else
7263 			request->ie = (void *)(request->channels + n_channels);
7264 	}
7265 
7266 	if (n_match_sets) {
7267 		if (request->ie)
7268 			request->match_sets = (void *)(request->ie + ie_len);
7269 		else if (n_ssids)
7270 			request->match_sets =
7271 				(void *)(request->ssids + n_ssids);
7272 		else
7273 			request->match_sets =
7274 				(void *)(request->channels + n_channels);
7275 	}
7276 	request->n_match_sets = n_match_sets;
7277 
7278 	if (n_match_sets)
7279 		request->scan_plans = (void *)(request->match_sets +
7280 					       n_match_sets);
7281 	else if (request->ie)
7282 		request->scan_plans = (void *)(request->ie + ie_len);
7283 	else if (n_ssids)
7284 		request->scan_plans = (void *)(request->ssids + n_ssids);
7285 	else
7286 		request->scan_plans = (void *)(request->channels + n_channels);
7287 
7288 	request->n_scan_plans = n_plans;
7289 
7290 	i = 0;
7291 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7292 		/* user specified, bail out if channel not found */
7293 		nla_for_each_nested(attr,
7294 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
7295 				    tmp) {
7296 			struct ieee80211_channel *chan;
7297 
7298 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7299 
7300 			if (!chan) {
7301 				err = -EINVAL;
7302 				goto out_free;
7303 			}
7304 
7305 			/* ignore disabled channels */
7306 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7307 				continue;
7308 
7309 			request->channels[i] = chan;
7310 			i++;
7311 		}
7312 	} else {
7313 		/* all channels */
7314 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7315 			int j;
7316 
7317 			if (!wiphy->bands[band])
7318 				continue;
7319 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7320 				struct ieee80211_channel *chan;
7321 
7322 				chan = &wiphy->bands[band]->channels[j];
7323 
7324 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7325 					continue;
7326 
7327 				request->channels[i] = chan;
7328 				i++;
7329 			}
7330 		}
7331 	}
7332 
7333 	if (!i) {
7334 		err = -EINVAL;
7335 		goto out_free;
7336 	}
7337 
7338 	request->n_channels = i;
7339 
7340 	i = 0;
7341 	if (n_ssids) {
7342 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7343 				    tmp) {
7344 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7345 				err = -EINVAL;
7346 				goto out_free;
7347 			}
7348 			request->ssids[i].ssid_len = nla_len(attr);
7349 			memcpy(request->ssids[i].ssid, nla_data(attr),
7350 			       nla_len(attr));
7351 			i++;
7352 		}
7353 	}
7354 
7355 	i = 0;
7356 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7357 		nla_for_each_nested(attr,
7358 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7359 				    tmp) {
7360 			struct nlattr *ssid, *bssid, *rssi;
7361 
7362 			err = nla_parse_nested(tb,
7363 					       NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7364 					       attr, nl80211_match_policy,
7365 					       NULL);
7366 			if (err)
7367 				goto out_free;
7368 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
7369 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
7370 			if (ssid || bssid) {
7371 				if (WARN_ON(i >= n_match_sets)) {
7372 					/* this indicates a programming error,
7373 					 * the loop above should have verified
7374 					 * things properly
7375 					 */
7376 					err = -EINVAL;
7377 					goto out_free;
7378 				}
7379 
7380 				if (ssid) {
7381 					if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
7382 						err = -EINVAL;
7383 						goto out_free;
7384 					}
7385 					memcpy(request->match_sets[i].ssid.ssid,
7386 					       nla_data(ssid), nla_len(ssid));
7387 					request->match_sets[i].ssid.ssid_len =
7388 						nla_len(ssid);
7389 				}
7390 				if (bssid) {
7391 					if (nla_len(bssid) != ETH_ALEN) {
7392 						err = -EINVAL;
7393 						goto out_free;
7394 					}
7395 					memcpy(request->match_sets[i].bssid,
7396 					       nla_data(bssid), ETH_ALEN);
7397 				}
7398 
7399 				/* special attribute - old implementation w/a */
7400 				request->match_sets[i].rssi_thold =
7401 					default_match_rssi;
7402 				rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7403 				if (rssi)
7404 					request->match_sets[i].rssi_thold =
7405 						nla_get_s32(rssi);
7406 			}
7407 			i++;
7408 		}
7409 
7410 		/* there was no other matchset, so the RSSI one is alone */
7411 		if (i == 0 && n_match_sets)
7412 			request->match_sets[0].rssi_thold = default_match_rssi;
7413 
7414 		request->min_rssi_thold = INT_MAX;
7415 		for (i = 0; i < n_match_sets; i++)
7416 			request->min_rssi_thold =
7417 				min(request->match_sets[i].rssi_thold,
7418 				    request->min_rssi_thold);
7419 	} else {
7420 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
7421 	}
7422 
7423 	if (ie_len) {
7424 		request->ie_len = ie_len;
7425 		memcpy((void *)request->ie,
7426 		       nla_data(attrs[NL80211_ATTR_IE]),
7427 		       request->ie_len);
7428 	}
7429 
7430 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
7431 	if (err)
7432 		goto out_free;
7433 
7434 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
7435 		request->delay =
7436 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
7437 
7438 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
7439 		request->relative_rssi = nla_get_s8(
7440 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
7441 		request->relative_rssi_set = true;
7442 	}
7443 
7444 	if (request->relative_rssi_set &&
7445 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
7446 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
7447 
7448 		rssi_adjust = nla_data(
7449 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
7450 		request->rssi_adjust.band = rssi_adjust->band;
7451 		request->rssi_adjust.delta = rssi_adjust->delta;
7452 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
7453 			err = -EINVAL;
7454 			goto out_free;
7455 		}
7456 	}
7457 
7458 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
7459 	if (err)
7460 		goto out_free;
7461 
7462 	request->scan_start = jiffies;
7463 
7464 	return request;
7465 
7466 out_free:
7467 	kfree(request);
7468 	return ERR_PTR(err);
7469 }
7470 
7471 static int nl80211_start_sched_scan(struct sk_buff *skb,
7472 				    struct genl_info *info)
7473 {
7474 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7475 	struct net_device *dev = info->user_ptr[1];
7476 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7477 	struct cfg80211_sched_scan_request *sched_scan_req;
7478 	bool want_multi;
7479 	int err;
7480 
7481 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
7482 		return -EOPNOTSUPP;
7483 
7484 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
7485 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
7486 	if (err)
7487 		return err;
7488 
7489 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
7490 						  info->attrs,
7491 						  rdev->wiphy.max_match_sets);
7492 
7493 	err = PTR_ERR_OR_ZERO(sched_scan_req);
7494 	if (err)
7495 		goto out_err;
7496 
7497 	/* leave request id zero for legacy request
7498 	 * or if driver does not support multi-scheduled scan
7499 	 */
7500 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
7501 		while (!sched_scan_req->reqid)
7502 			sched_scan_req->reqid = rdev->wiphy.cookie_counter++;
7503 	}
7504 
7505 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
7506 	if (err)
7507 		goto out_free;
7508 
7509 	sched_scan_req->dev = dev;
7510 	sched_scan_req->wiphy = &rdev->wiphy;
7511 
7512 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
7513 		sched_scan_req->owner_nlportid = info->snd_portid;
7514 
7515 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
7516 
7517 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
7518 	return 0;
7519 
7520 out_free:
7521 	kfree(sched_scan_req);
7522 out_err:
7523 	return err;
7524 }
7525 
7526 static int nl80211_stop_sched_scan(struct sk_buff *skb,
7527 				   struct genl_info *info)
7528 {
7529 	struct cfg80211_sched_scan_request *req;
7530 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7531 	u64 cookie;
7532 
7533 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
7534 		return -EOPNOTSUPP;
7535 
7536 	if (info->attrs[NL80211_ATTR_COOKIE]) {
7537 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7538 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
7539 	}
7540 
7541 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
7542 				     struct cfg80211_sched_scan_request,
7543 				     list);
7544 	if (!req || req->reqid ||
7545 	    (req->owner_nlportid &&
7546 	     req->owner_nlportid != info->snd_portid))
7547 		return -ENOENT;
7548 
7549 	return cfg80211_stop_sched_scan_req(rdev, req, false);
7550 }
7551 
7552 static int nl80211_start_radar_detection(struct sk_buff *skb,
7553 					 struct genl_info *info)
7554 {
7555 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7556 	struct net_device *dev = info->user_ptr[1];
7557 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7558 	struct wiphy *wiphy = wdev->wiphy;
7559 	struct cfg80211_chan_def chandef;
7560 	enum nl80211_dfs_regions dfs_region;
7561 	unsigned int cac_time_ms;
7562 	int err;
7563 
7564 	dfs_region = reg_get_dfs_region(wiphy);
7565 	if (dfs_region == NL80211_DFS_UNSET)
7566 		return -EINVAL;
7567 
7568 	err = nl80211_parse_chandef(rdev, info, &chandef);
7569 	if (err)
7570 		return err;
7571 
7572 	if (netif_carrier_ok(dev))
7573 		return -EBUSY;
7574 
7575 	if (wdev->cac_started)
7576 		return -EBUSY;
7577 
7578 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
7579 	if (err < 0)
7580 		return err;
7581 
7582 	if (err == 0)
7583 		return -EINVAL;
7584 
7585 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
7586 		return -EINVAL;
7587 
7588 	/* CAC start is offloaded to HW and can't be started manually */
7589 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
7590 		return -EOPNOTSUPP;
7591 
7592 	if (!rdev->ops->start_radar_detection)
7593 		return -EOPNOTSUPP;
7594 
7595 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
7596 	if (WARN_ON(!cac_time_ms))
7597 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
7598 
7599 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
7600 	if (!err) {
7601 		wdev->chandef = chandef;
7602 		wdev->cac_started = true;
7603 		wdev->cac_start_time = jiffies;
7604 		wdev->cac_time_ms = cac_time_ms;
7605 	}
7606 	return err;
7607 }
7608 
7609 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
7610 {
7611 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7612 	struct net_device *dev = info->user_ptr[1];
7613 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7614 	struct cfg80211_csa_settings params;
7615 	/* csa_attrs is defined static to avoid waste of stack size - this
7616 	 * function is called under RTNL lock, so this should not be a problem.
7617 	 */
7618 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
7619 	int err;
7620 	bool need_new_beacon = false;
7621 	bool need_handle_dfs_flag = true;
7622 	int len, i;
7623 	u32 cs_count;
7624 
7625 	if (!rdev->ops->channel_switch ||
7626 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
7627 		return -EOPNOTSUPP;
7628 
7629 	switch (dev->ieee80211_ptr->iftype) {
7630 	case NL80211_IFTYPE_AP:
7631 	case NL80211_IFTYPE_P2P_GO:
7632 		need_new_beacon = true;
7633 		/* For all modes except AP the handle_dfs flag needs to be
7634 		 * supplied to tell the kernel that userspace will handle radar
7635 		 * events when they happen. Otherwise a switch to a channel
7636 		 * requiring DFS will be rejected.
7637 		 */
7638 		need_handle_dfs_flag = false;
7639 
7640 		/* useless if AP is not running */
7641 		if (!wdev->beacon_interval)
7642 			return -ENOTCONN;
7643 		break;
7644 	case NL80211_IFTYPE_ADHOC:
7645 		if (!wdev->ssid_len)
7646 			return -ENOTCONN;
7647 		break;
7648 	case NL80211_IFTYPE_MESH_POINT:
7649 		if (!wdev->mesh_id_len)
7650 			return -ENOTCONN;
7651 		break;
7652 	default:
7653 		return -EOPNOTSUPP;
7654 	}
7655 
7656 	memset(&params, 0, sizeof(params));
7657 
7658 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7659 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
7660 		return -EINVAL;
7661 
7662 	/* only important for AP, IBSS and mesh create IEs internally */
7663 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
7664 		return -EINVAL;
7665 
7666 	/* Even though the attribute is u32, the specification says
7667 	 * u8, so let's make sure we don't overflow.
7668 	 */
7669 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
7670 	if (cs_count > 255)
7671 		return -EINVAL;
7672 
7673 	params.count = cs_count;
7674 
7675 	if (!need_new_beacon)
7676 		goto skip_beacons;
7677 
7678 	err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
7679 	if (err)
7680 		return err;
7681 
7682 	err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
7683 			       info->attrs[NL80211_ATTR_CSA_IES],
7684 			       nl80211_policy, info->extack);
7685 	if (err)
7686 		return err;
7687 
7688 	err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
7689 	if (err)
7690 		return err;
7691 
7692 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
7693 		return -EINVAL;
7694 
7695 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7696 	if (!len || (len % sizeof(u16)))
7697 		return -EINVAL;
7698 
7699 	params.n_counter_offsets_beacon = len / sizeof(u16);
7700 	if (rdev->wiphy.max_num_csa_counters &&
7701 	    (params.n_counter_offsets_beacon >
7702 	     rdev->wiphy.max_num_csa_counters))
7703 		return -EINVAL;
7704 
7705 	params.counter_offsets_beacon =
7706 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7707 
7708 	/* sanity checks - counters should fit and be the same */
7709 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
7710 		u16 offset = params.counter_offsets_beacon[i];
7711 
7712 		if (offset >= params.beacon_csa.tail_len)
7713 			return -EINVAL;
7714 
7715 		if (params.beacon_csa.tail[offset] != params.count)
7716 			return -EINVAL;
7717 	}
7718 
7719 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
7720 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7721 		if (!len || (len % sizeof(u16)))
7722 			return -EINVAL;
7723 
7724 		params.n_counter_offsets_presp = len / sizeof(u16);
7725 		if (rdev->wiphy.max_num_csa_counters &&
7726 		    (params.n_counter_offsets_presp >
7727 		     rdev->wiphy.max_num_csa_counters))
7728 			return -EINVAL;
7729 
7730 		params.counter_offsets_presp =
7731 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7732 
7733 		/* sanity checks - counters should fit and be the same */
7734 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
7735 			u16 offset = params.counter_offsets_presp[i];
7736 
7737 			if (offset >= params.beacon_csa.probe_resp_len)
7738 				return -EINVAL;
7739 
7740 			if (params.beacon_csa.probe_resp[offset] !=
7741 			    params.count)
7742 				return -EINVAL;
7743 		}
7744 	}
7745 
7746 skip_beacons:
7747 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
7748 	if (err)
7749 		return err;
7750 
7751 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
7752 					   wdev->iftype))
7753 		return -EINVAL;
7754 
7755 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
7756 					    &params.chandef,
7757 					    wdev->iftype);
7758 	if (err < 0)
7759 		return err;
7760 
7761 	if (err > 0) {
7762 		params.radar_required = true;
7763 		if (need_handle_dfs_flag &&
7764 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
7765 			return -EINVAL;
7766 		}
7767 	}
7768 
7769 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
7770 		params.block_tx = true;
7771 
7772 	wdev_lock(wdev);
7773 	err = rdev_channel_switch(rdev, dev, &params);
7774 	wdev_unlock(wdev);
7775 
7776 	return err;
7777 }
7778 
7779 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
7780 			    u32 seq, int flags,
7781 			    struct cfg80211_registered_device *rdev,
7782 			    struct wireless_dev *wdev,
7783 			    struct cfg80211_internal_bss *intbss)
7784 {
7785 	struct cfg80211_bss *res = &intbss->pub;
7786 	const struct cfg80211_bss_ies *ies;
7787 	void *hdr;
7788 	struct nlattr *bss;
7789 
7790 	ASSERT_WDEV_LOCK(wdev);
7791 
7792 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7793 			     NL80211_CMD_NEW_SCAN_RESULTS);
7794 	if (!hdr)
7795 		return -1;
7796 
7797 	genl_dump_check_consistent(cb, hdr);
7798 
7799 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
7800 		goto nla_put_failure;
7801 	if (wdev->netdev &&
7802 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
7803 		goto nla_put_failure;
7804 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
7805 			      NL80211_ATTR_PAD))
7806 		goto nla_put_failure;
7807 
7808 	bss = nla_nest_start(msg, NL80211_ATTR_BSS);
7809 	if (!bss)
7810 		goto nla_put_failure;
7811 	if ((!is_zero_ether_addr(res->bssid) &&
7812 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
7813 		goto nla_put_failure;
7814 
7815 	rcu_read_lock();
7816 	/* indicate whether we have probe response data or not */
7817 	if (rcu_access_pointer(res->proberesp_ies) &&
7818 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
7819 		goto fail_unlock_rcu;
7820 
7821 	/* this pointer prefers to be pointed to probe response data
7822 	 * but is always valid
7823 	 */
7824 	ies = rcu_dereference(res->ies);
7825 	if (ies) {
7826 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
7827 				      NL80211_BSS_PAD))
7828 			goto fail_unlock_rcu;
7829 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
7830 					ies->len, ies->data))
7831 			goto fail_unlock_rcu;
7832 	}
7833 
7834 	/* and this pointer is always (unless driver didn't know) beacon data */
7835 	ies = rcu_dereference(res->beacon_ies);
7836 	if (ies && ies->from_beacon) {
7837 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
7838 				      NL80211_BSS_PAD))
7839 			goto fail_unlock_rcu;
7840 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
7841 					ies->len, ies->data))
7842 			goto fail_unlock_rcu;
7843 	}
7844 	rcu_read_unlock();
7845 
7846 	if (res->beacon_interval &&
7847 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
7848 		goto nla_put_failure;
7849 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
7850 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
7851 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
7852 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
7853 			jiffies_to_msecs(jiffies - intbss->ts)))
7854 		goto nla_put_failure;
7855 
7856 	if (intbss->parent_tsf &&
7857 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
7858 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
7859 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
7860 		     intbss->parent_bssid)))
7861 		goto nla_put_failure;
7862 
7863 	if (intbss->ts_boottime &&
7864 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
7865 			      intbss->ts_boottime, NL80211_BSS_PAD))
7866 		goto nla_put_failure;
7867 
7868 	if (!nl80211_put_signal(msg, intbss->pub.chains,
7869 				intbss->pub.chain_signal,
7870 				NL80211_BSS_CHAIN_SIGNAL))
7871 		goto nla_put_failure;
7872 
7873 	switch (rdev->wiphy.signal_type) {
7874 	case CFG80211_SIGNAL_TYPE_MBM:
7875 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
7876 			goto nla_put_failure;
7877 		break;
7878 	case CFG80211_SIGNAL_TYPE_UNSPEC:
7879 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
7880 			goto nla_put_failure;
7881 		break;
7882 	default:
7883 		break;
7884 	}
7885 
7886 	switch (wdev->iftype) {
7887 	case NL80211_IFTYPE_P2P_CLIENT:
7888 	case NL80211_IFTYPE_STATION:
7889 		if (intbss == wdev->current_bss &&
7890 		    nla_put_u32(msg, NL80211_BSS_STATUS,
7891 				NL80211_BSS_STATUS_ASSOCIATED))
7892 			goto nla_put_failure;
7893 		break;
7894 	case NL80211_IFTYPE_ADHOC:
7895 		if (intbss == wdev->current_bss &&
7896 		    nla_put_u32(msg, NL80211_BSS_STATUS,
7897 				NL80211_BSS_STATUS_IBSS_JOINED))
7898 			goto nla_put_failure;
7899 		break;
7900 	default:
7901 		break;
7902 	}
7903 
7904 	nla_nest_end(msg, bss);
7905 
7906 	genlmsg_end(msg, hdr);
7907 	return 0;
7908 
7909  fail_unlock_rcu:
7910 	rcu_read_unlock();
7911  nla_put_failure:
7912 	genlmsg_cancel(msg, hdr);
7913 	return -EMSGSIZE;
7914 }
7915 
7916 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
7917 {
7918 	struct cfg80211_registered_device *rdev;
7919 	struct cfg80211_internal_bss *scan;
7920 	struct wireless_dev *wdev;
7921 	int start = cb->args[2], idx = 0;
7922 	int err;
7923 
7924 	rtnl_lock();
7925 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
7926 	if (err) {
7927 		rtnl_unlock();
7928 		return err;
7929 	}
7930 
7931 	wdev_lock(wdev);
7932 	spin_lock_bh(&rdev->bss_lock);
7933 	cfg80211_bss_expire(rdev);
7934 
7935 	cb->seq = rdev->bss_generation;
7936 
7937 	list_for_each_entry(scan, &rdev->bss_list, list) {
7938 		if (++idx <= start)
7939 			continue;
7940 		if (nl80211_send_bss(skb, cb,
7941 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
7942 				rdev, wdev, scan) < 0) {
7943 			idx--;
7944 			break;
7945 		}
7946 	}
7947 
7948 	spin_unlock_bh(&rdev->bss_lock);
7949 	wdev_unlock(wdev);
7950 
7951 	cb->args[2] = idx;
7952 	rtnl_unlock();
7953 
7954 	return skb->len;
7955 }
7956 
7957 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
7958 			       int flags, struct net_device *dev,
7959 			       bool allow_radio_stats,
7960 			       struct survey_info *survey)
7961 {
7962 	void *hdr;
7963 	struct nlattr *infoattr;
7964 
7965 	/* skip radio stats if userspace didn't request them */
7966 	if (!survey->channel && !allow_radio_stats)
7967 		return 0;
7968 
7969 	hdr = nl80211hdr_put(msg, portid, seq, flags,
7970 			     NL80211_CMD_NEW_SURVEY_RESULTS);
7971 	if (!hdr)
7972 		return -ENOMEM;
7973 
7974 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
7975 		goto nla_put_failure;
7976 
7977 	infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
7978 	if (!infoattr)
7979 		goto nla_put_failure;
7980 
7981 	if (survey->channel &&
7982 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
7983 			survey->channel->center_freq))
7984 		goto nla_put_failure;
7985 
7986 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
7987 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
7988 		goto nla_put_failure;
7989 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
7990 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
7991 		goto nla_put_failure;
7992 	if ((survey->filled & SURVEY_INFO_TIME) &&
7993 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
7994 			survey->time, NL80211_SURVEY_INFO_PAD))
7995 		goto nla_put_failure;
7996 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
7997 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
7998 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
7999 		goto nla_put_failure;
8000 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8001 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8002 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8003 		goto nla_put_failure;
8004 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8005 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8006 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
8007 		goto nla_put_failure;
8008 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8009 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8010 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
8011 		goto nla_put_failure;
8012 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8013 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8014 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
8015 		goto nla_put_failure;
8016 
8017 	nla_nest_end(msg, infoattr);
8018 
8019 	genlmsg_end(msg, hdr);
8020 	return 0;
8021 
8022  nla_put_failure:
8023 	genlmsg_cancel(msg, hdr);
8024 	return -EMSGSIZE;
8025 }
8026 
8027 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8028 {
8029 	struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8030 	struct survey_info survey;
8031 	struct cfg80211_registered_device *rdev;
8032 	struct wireless_dev *wdev;
8033 	int survey_idx = cb->args[2];
8034 	int res;
8035 	bool radio_stats;
8036 
8037 	rtnl_lock();
8038 	res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
8039 	if (res)
8040 		goto out_err;
8041 
8042 	/* prepare_wdev_dump parsed the attributes */
8043 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8044 
8045 	if (!wdev->netdev) {
8046 		res = -EINVAL;
8047 		goto out_err;
8048 	}
8049 
8050 	if (!rdev->ops->dump_survey) {
8051 		res = -EOPNOTSUPP;
8052 		goto out_err;
8053 	}
8054 
8055 	while (1) {
8056 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8057 		if (res == -ENOENT)
8058 			break;
8059 		if (res)
8060 			goto out_err;
8061 
8062 		/* don't send disabled channels, but do send non-channel data */
8063 		if (survey.channel &&
8064 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8065 			survey_idx++;
8066 			continue;
8067 		}
8068 
8069 		if (nl80211_send_survey(skb,
8070 				NETLINK_CB(cb->skb).portid,
8071 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8072 				wdev->netdev, radio_stats, &survey) < 0)
8073 			goto out;
8074 		survey_idx++;
8075 	}
8076 
8077  out:
8078 	cb->args[2] = survey_idx;
8079 	res = skb->len;
8080  out_err:
8081 	rtnl_unlock();
8082 	return res;
8083 }
8084 
8085 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8086 {
8087 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8088 				  NL80211_WPA_VERSION_2));
8089 }
8090 
8091 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8092 {
8093 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8094 	struct net_device *dev = info->user_ptr[1];
8095 	struct ieee80211_channel *chan;
8096 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8097 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
8098 	enum nl80211_auth_type auth_type;
8099 	struct key_parse key;
8100 	bool local_state_change;
8101 
8102 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8103 		return -EINVAL;
8104 
8105 	if (!info->attrs[NL80211_ATTR_MAC])
8106 		return -EINVAL;
8107 
8108 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8109 		return -EINVAL;
8110 
8111 	if (!info->attrs[NL80211_ATTR_SSID])
8112 		return -EINVAL;
8113 
8114 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8115 		return -EINVAL;
8116 
8117 	err = nl80211_parse_key(info, &key);
8118 	if (err)
8119 		return err;
8120 
8121 	if (key.idx >= 0) {
8122 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8123 			return -EINVAL;
8124 		if (!key.p.key || !key.p.key_len)
8125 			return -EINVAL;
8126 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8127 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8128 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8129 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
8130 			return -EINVAL;
8131 		if (key.idx > 3)
8132 			return -EINVAL;
8133 	} else {
8134 		key.p.key_len = 0;
8135 		key.p.key = NULL;
8136 	}
8137 
8138 	if (key.idx >= 0) {
8139 		int i;
8140 		bool ok = false;
8141 
8142 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8143 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8144 				ok = true;
8145 				break;
8146 			}
8147 		}
8148 		if (!ok)
8149 			return -EINVAL;
8150 	}
8151 
8152 	if (!rdev->ops->auth)
8153 		return -EOPNOTSUPP;
8154 
8155 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8156 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8157 		return -EOPNOTSUPP;
8158 
8159 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8160 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8161 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8162 	if (!chan)
8163 		return -EINVAL;
8164 
8165 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8166 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8167 
8168 	if (info->attrs[NL80211_ATTR_IE]) {
8169 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8170 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8171 	}
8172 
8173 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8174 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
8175 		return -EINVAL;
8176 
8177 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
8178 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
8179 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
8180 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
8181 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
8182 		return -EINVAL;
8183 
8184 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
8185 		if (auth_type != NL80211_AUTHTYPE_SAE &&
8186 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
8187 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8188 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
8189 			return -EINVAL;
8190 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8191 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
8192 		/* need to include at least Auth Transaction and Status Code */
8193 		if (auth_data_len < 4)
8194 			return -EINVAL;
8195 	}
8196 
8197 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8198 
8199 	/*
8200 	 * Since we no longer track auth state, ignore
8201 	 * requests to only change local state.
8202 	 */
8203 	if (local_state_change)
8204 		return 0;
8205 
8206 	wdev_lock(dev->ieee80211_ptr);
8207 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8208 				 ssid, ssid_len, ie, ie_len,
8209 				 key.p.key, key.p.key_len, key.idx,
8210 				 auth_data, auth_data_len);
8211 	wdev_unlock(dev->ieee80211_ptr);
8212 	return err;
8213 }
8214 
8215 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
8216 				     struct genl_info *info)
8217 {
8218 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8219 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
8220 		return -EINVAL;
8221 	}
8222 
8223 	if (!rdev->ops->tx_control_port ||
8224 	    !wiphy_ext_feature_isset(&rdev->wiphy,
8225 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
8226 		return -EOPNOTSUPP;
8227 
8228 	return 0;
8229 }
8230 
8231 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8232 				   struct genl_info *info,
8233 				   struct cfg80211_crypto_settings *settings,
8234 				   int cipher_limit)
8235 {
8236 	memset(settings, 0, sizeof(*settings));
8237 
8238 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
8239 
8240 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
8241 		u16 proto;
8242 
8243 		proto = nla_get_u16(
8244 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
8245 		settings->control_port_ethertype = cpu_to_be16(proto);
8246 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
8247 		    proto != ETH_P_PAE)
8248 			return -EINVAL;
8249 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
8250 			settings->control_port_no_encrypt = true;
8251 	} else
8252 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
8253 
8254 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
8255 		int r = validate_pae_over_nl80211(rdev, info);
8256 
8257 		if (r < 0)
8258 			return r;
8259 
8260 		settings->control_port_over_nl80211 = true;
8261 	}
8262 
8263 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
8264 		void *data;
8265 		int len, i;
8266 
8267 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8268 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8269 		settings->n_ciphers_pairwise = len / sizeof(u32);
8270 
8271 		if (len % sizeof(u32))
8272 			return -EINVAL;
8273 
8274 		if (settings->n_ciphers_pairwise > cipher_limit)
8275 			return -EINVAL;
8276 
8277 		memcpy(settings->ciphers_pairwise, data, len);
8278 
8279 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
8280 			if (!cfg80211_supported_cipher_suite(
8281 					&rdev->wiphy,
8282 					settings->ciphers_pairwise[i]))
8283 				return -EINVAL;
8284 	}
8285 
8286 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
8287 		settings->cipher_group =
8288 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
8289 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
8290 						     settings->cipher_group))
8291 			return -EINVAL;
8292 	}
8293 
8294 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
8295 		settings->wpa_versions =
8296 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
8297 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
8298 			return -EINVAL;
8299 	}
8300 
8301 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
8302 		void *data;
8303 		int len;
8304 
8305 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
8306 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
8307 		settings->n_akm_suites = len / sizeof(u32);
8308 
8309 		if (len % sizeof(u32))
8310 			return -EINVAL;
8311 
8312 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
8313 			return -EINVAL;
8314 
8315 		memcpy(settings->akm_suites, data, len);
8316 	}
8317 
8318 	if (info->attrs[NL80211_ATTR_PMK]) {
8319 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
8320 			return -EINVAL;
8321 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
8322 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
8323 			return -EINVAL;
8324 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
8325 	}
8326 
8327 	return 0;
8328 }
8329 
8330 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
8331 {
8332 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8333 	struct net_device *dev = info->user_ptr[1];
8334 	struct ieee80211_channel *chan;
8335 	struct cfg80211_assoc_request req = {};
8336 	const u8 *bssid, *ssid;
8337 	int err, ssid_len = 0;
8338 
8339 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8340 		return -EINVAL;
8341 
8342 	if (!info->attrs[NL80211_ATTR_MAC] ||
8343 	    !info->attrs[NL80211_ATTR_SSID] ||
8344 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
8345 		return -EINVAL;
8346 
8347 	if (!rdev->ops->assoc)
8348 		return -EOPNOTSUPP;
8349 
8350 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8351 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8352 		return -EOPNOTSUPP;
8353 
8354 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8355 
8356 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8357 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8358 	if (!chan)
8359 		return -EINVAL;
8360 
8361 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8362 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8363 
8364 	if (info->attrs[NL80211_ATTR_IE]) {
8365 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8366 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8367 	}
8368 
8369 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
8370 		enum nl80211_mfp mfp =
8371 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8372 		if (mfp == NL80211_MFP_REQUIRED)
8373 			req.use_mfp = true;
8374 		else if (mfp != NL80211_MFP_NO)
8375 			return -EINVAL;
8376 	}
8377 
8378 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
8379 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8380 
8381 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
8382 		req.flags |= ASSOC_REQ_DISABLE_HT;
8383 
8384 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8385 		memcpy(&req.ht_capa_mask,
8386 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8387 		       sizeof(req.ht_capa_mask));
8388 
8389 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8390 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8391 			return -EINVAL;
8392 		memcpy(&req.ht_capa,
8393 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8394 		       sizeof(req.ht_capa));
8395 	}
8396 
8397 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
8398 		req.flags |= ASSOC_REQ_DISABLE_VHT;
8399 
8400 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8401 		memcpy(&req.vht_capa_mask,
8402 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8403 		       sizeof(req.vht_capa_mask));
8404 
8405 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
8406 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8407 			return -EINVAL;
8408 		memcpy(&req.vht_capa,
8409 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8410 		       sizeof(req.vht_capa));
8411 	}
8412 
8413 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
8414 		if (!((rdev->wiphy.features &
8415 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8416 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8417 		    !wiphy_ext_feature_isset(&rdev->wiphy,
8418 					     NL80211_EXT_FEATURE_RRM))
8419 			return -EINVAL;
8420 		req.flags |= ASSOC_REQ_USE_RRM;
8421 	}
8422 
8423 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
8424 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
8425 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
8426 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
8427 			return -EINVAL;
8428 		req.fils_nonces =
8429 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
8430 	}
8431 
8432 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
8433 	if (!err) {
8434 		wdev_lock(dev->ieee80211_ptr);
8435 
8436 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
8437 					  ssid, ssid_len, &req);
8438 
8439 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8440 			dev->ieee80211_ptr->conn_owner_nlportid =
8441 				info->snd_portid;
8442 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
8443 			       bssid, ETH_ALEN);
8444 		}
8445 
8446 		wdev_unlock(dev->ieee80211_ptr);
8447 	}
8448 
8449 	return err;
8450 }
8451 
8452 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
8453 {
8454 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8455 	struct net_device *dev = info->user_ptr[1];
8456 	const u8 *ie = NULL, *bssid;
8457 	int ie_len = 0, err;
8458 	u16 reason_code;
8459 	bool local_state_change;
8460 
8461 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8462 		return -EINVAL;
8463 
8464 	if (!info->attrs[NL80211_ATTR_MAC])
8465 		return -EINVAL;
8466 
8467 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
8468 		return -EINVAL;
8469 
8470 	if (!rdev->ops->deauth)
8471 		return -EOPNOTSUPP;
8472 
8473 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8474 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8475 		return -EOPNOTSUPP;
8476 
8477 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8478 
8479 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8480 	if (reason_code == 0) {
8481 		/* Reason Code 0 is reserved */
8482 		return -EINVAL;
8483 	}
8484 
8485 	if (info->attrs[NL80211_ATTR_IE]) {
8486 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8487 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8488 	}
8489 
8490 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8491 
8492 	wdev_lock(dev->ieee80211_ptr);
8493 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
8494 				   local_state_change);
8495 	wdev_unlock(dev->ieee80211_ptr);
8496 	return err;
8497 }
8498 
8499 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
8500 {
8501 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8502 	struct net_device *dev = info->user_ptr[1];
8503 	const u8 *ie = NULL, *bssid;
8504 	int ie_len = 0, err;
8505 	u16 reason_code;
8506 	bool local_state_change;
8507 
8508 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8509 		return -EINVAL;
8510 
8511 	if (!info->attrs[NL80211_ATTR_MAC])
8512 		return -EINVAL;
8513 
8514 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
8515 		return -EINVAL;
8516 
8517 	if (!rdev->ops->disassoc)
8518 		return -EOPNOTSUPP;
8519 
8520 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8521 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8522 		return -EOPNOTSUPP;
8523 
8524 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8525 
8526 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8527 	if (reason_code == 0) {
8528 		/* Reason Code 0 is reserved */
8529 		return -EINVAL;
8530 	}
8531 
8532 	if (info->attrs[NL80211_ATTR_IE]) {
8533 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8534 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8535 	}
8536 
8537 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8538 
8539 	wdev_lock(dev->ieee80211_ptr);
8540 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
8541 				     local_state_change);
8542 	wdev_unlock(dev->ieee80211_ptr);
8543 	return err;
8544 }
8545 
8546 static bool
8547 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
8548 			 int mcast_rate[NUM_NL80211_BANDS],
8549 			 int rateval)
8550 {
8551 	struct wiphy *wiphy = &rdev->wiphy;
8552 	bool found = false;
8553 	int band, i;
8554 
8555 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
8556 		struct ieee80211_supported_band *sband;
8557 
8558 		sband = wiphy->bands[band];
8559 		if (!sband)
8560 			continue;
8561 
8562 		for (i = 0; i < sband->n_bitrates; i++) {
8563 			if (sband->bitrates[i].bitrate == rateval) {
8564 				mcast_rate[band] = i + 1;
8565 				found = true;
8566 				break;
8567 			}
8568 		}
8569 	}
8570 
8571 	return found;
8572 }
8573 
8574 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
8575 {
8576 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8577 	struct net_device *dev = info->user_ptr[1];
8578 	struct cfg80211_ibss_params ibss;
8579 	struct wiphy *wiphy;
8580 	struct cfg80211_cached_keys *connkeys = NULL;
8581 	int err;
8582 
8583 	memset(&ibss, 0, sizeof(ibss));
8584 
8585 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8586 		return -EINVAL;
8587 
8588 	if (!info->attrs[NL80211_ATTR_SSID] ||
8589 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
8590 		return -EINVAL;
8591 
8592 	ibss.beacon_interval = 100;
8593 
8594 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
8595 		ibss.beacon_interval =
8596 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8597 
8598 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
8599 					   ibss.beacon_interval);
8600 	if (err)
8601 		return err;
8602 
8603 	if (!rdev->ops->join_ibss)
8604 		return -EOPNOTSUPP;
8605 
8606 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8607 		return -EOPNOTSUPP;
8608 
8609 	wiphy = &rdev->wiphy;
8610 
8611 	if (info->attrs[NL80211_ATTR_MAC]) {
8612 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8613 
8614 		if (!is_valid_ether_addr(ibss.bssid))
8615 			return -EINVAL;
8616 	}
8617 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8618 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8619 
8620 	if (info->attrs[NL80211_ATTR_IE]) {
8621 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8622 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8623 	}
8624 
8625 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
8626 	if (err)
8627 		return err;
8628 
8629 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
8630 				     NL80211_IFTYPE_ADHOC))
8631 		return -EINVAL;
8632 
8633 	switch (ibss.chandef.width) {
8634 	case NL80211_CHAN_WIDTH_5:
8635 	case NL80211_CHAN_WIDTH_10:
8636 	case NL80211_CHAN_WIDTH_20_NOHT:
8637 		break;
8638 	case NL80211_CHAN_WIDTH_20:
8639 	case NL80211_CHAN_WIDTH_40:
8640 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8641 			return -EINVAL;
8642 		break;
8643 	case NL80211_CHAN_WIDTH_80:
8644 	case NL80211_CHAN_WIDTH_80P80:
8645 	case NL80211_CHAN_WIDTH_160:
8646 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8647 			return -EINVAL;
8648 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
8649 					     NL80211_EXT_FEATURE_VHT_IBSS))
8650 			return -EINVAL;
8651 		break;
8652 	default:
8653 		return -EINVAL;
8654 	}
8655 
8656 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
8657 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
8658 
8659 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8660 		u8 *rates =
8661 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8662 		int n_rates =
8663 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8664 		struct ieee80211_supported_band *sband =
8665 			wiphy->bands[ibss.chandef.chan->band];
8666 
8667 		err = ieee80211_get_ratemask(sband, rates, n_rates,
8668 					     &ibss.basic_rates);
8669 		if (err)
8670 			return err;
8671 	}
8672 
8673 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8674 		memcpy(&ibss.ht_capa_mask,
8675 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8676 		       sizeof(ibss.ht_capa_mask));
8677 
8678 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8679 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8680 			return -EINVAL;
8681 		memcpy(&ibss.ht_capa,
8682 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8683 		       sizeof(ibss.ht_capa));
8684 	}
8685 
8686 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8687 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
8688 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8689 		return -EINVAL;
8690 
8691 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
8692 		bool no_ht = false;
8693 
8694 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
8695 		if (IS_ERR(connkeys))
8696 			return PTR_ERR(connkeys);
8697 
8698 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
8699 		    no_ht) {
8700 			kzfree(connkeys);
8701 			return -EINVAL;
8702 		}
8703 	}
8704 
8705 	ibss.control_port =
8706 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
8707 
8708 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
8709 		int r = validate_pae_over_nl80211(rdev, info);
8710 
8711 		if (r < 0)
8712 			return r;
8713 
8714 		ibss.control_port_over_nl80211 = true;
8715 	}
8716 
8717 	ibss.userspace_handles_dfs =
8718 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
8719 
8720 	wdev_lock(dev->ieee80211_ptr);
8721 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
8722 	if (err)
8723 		kzfree(connkeys);
8724 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8725 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
8726 	wdev_unlock(dev->ieee80211_ptr);
8727 
8728 	return err;
8729 }
8730 
8731 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
8732 {
8733 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8734 	struct net_device *dev = info->user_ptr[1];
8735 
8736 	if (!rdev->ops->leave_ibss)
8737 		return -EOPNOTSUPP;
8738 
8739 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8740 		return -EOPNOTSUPP;
8741 
8742 	return cfg80211_leave_ibss(rdev, dev, false);
8743 }
8744 
8745 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
8746 {
8747 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8748 	struct net_device *dev = info->user_ptr[1];
8749 	int mcast_rate[NUM_NL80211_BANDS];
8750 	u32 nla_rate;
8751 	int err;
8752 
8753 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
8754 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
8755 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
8756 		return -EOPNOTSUPP;
8757 
8758 	if (!rdev->ops->set_mcast_rate)
8759 		return -EOPNOTSUPP;
8760 
8761 	memset(mcast_rate, 0, sizeof(mcast_rate));
8762 
8763 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
8764 		return -EINVAL;
8765 
8766 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
8767 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
8768 		return -EINVAL;
8769 
8770 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
8771 
8772 	return err;
8773 }
8774 
8775 static struct sk_buff *
8776 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
8777 			    struct wireless_dev *wdev, int approxlen,
8778 			    u32 portid, u32 seq, enum nl80211_commands cmd,
8779 			    enum nl80211_attrs attr,
8780 			    const struct nl80211_vendor_cmd_info *info,
8781 			    gfp_t gfp)
8782 {
8783 	struct sk_buff *skb;
8784 	void *hdr;
8785 	struct nlattr *data;
8786 
8787 	skb = nlmsg_new(approxlen + 100, gfp);
8788 	if (!skb)
8789 		return NULL;
8790 
8791 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
8792 	if (!hdr) {
8793 		kfree_skb(skb);
8794 		return NULL;
8795 	}
8796 
8797 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
8798 		goto nla_put_failure;
8799 
8800 	if (info) {
8801 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
8802 				info->vendor_id))
8803 			goto nla_put_failure;
8804 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
8805 				info->subcmd))
8806 			goto nla_put_failure;
8807 	}
8808 
8809 	if (wdev) {
8810 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
8811 				      wdev_id(wdev), NL80211_ATTR_PAD))
8812 			goto nla_put_failure;
8813 		if (wdev->netdev &&
8814 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
8815 				wdev->netdev->ifindex))
8816 			goto nla_put_failure;
8817 	}
8818 
8819 	data = nla_nest_start(skb, attr);
8820 	if (!data)
8821 		goto nla_put_failure;
8822 
8823 	((void **)skb->cb)[0] = rdev;
8824 	((void **)skb->cb)[1] = hdr;
8825 	((void **)skb->cb)[2] = data;
8826 
8827 	return skb;
8828 
8829  nla_put_failure:
8830 	kfree_skb(skb);
8831 	return NULL;
8832 }
8833 
8834 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
8835 					   struct wireless_dev *wdev,
8836 					   enum nl80211_commands cmd,
8837 					   enum nl80211_attrs attr,
8838 					   int vendor_event_idx,
8839 					   int approxlen, gfp_t gfp)
8840 {
8841 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
8842 	const struct nl80211_vendor_cmd_info *info;
8843 
8844 	switch (cmd) {
8845 	case NL80211_CMD_TESTMODE:
8846 		if (WARN_ON(vendor_event_idx != -1))
8847 			return NULL;
8848 		info = NULL;
8849 		break;
8850 	case NL80211_CMD_VENDOR:
8851 		if (WARN_ON(vendor_event_idx < 0 ||
8852 			    vendor_event_idx >= wiphy->n_vendor_events))
8853 			return NULL;
8854 		info = &wiphy->vendor_events[vendor_event_idx];
8855 		break;
8856 	default:
8857 		WARN_ON(1);
8858 		return NULL;
8859 	}
8860 
8861 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
8862 					   cmd, attr, info, gfp);
8863 }
8864 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
8865 
8866 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
8867 {
8868 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8869 	void *hdr = ((void **)skb->cb)[1];
8870 	struct nlattr *data = ((void **)skb->cb)[2];
8871 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
8872 
8873 	/* clear CB data for netlink core to own from now on */
8874 	memset(skb->cb, 0, sizeof(skb->cb));
8875 
8876 	nla_nest_end(skb, data);
8877 	genlmsg_end(skb, hdr);
8878 
8879 	if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
8880 		mcgrp = NL80211_MCGRP_VENDOR;
8881 
8882 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
8883 				mcgrp, gfp);
8884 }
8885 EXPORT_SYMBOL(__cfg80211_send_event_skb);
8886 
8887 #ifdef CONFIG_NL80211_TESTMODE
8888 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
8889 {
8890 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8891 	struct wireless_dev *wdev =
8892 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8893 	int err;
8894 
8895 	if (!rdev->ops->testmode_cmd)
8896 		return -EOPNOTSUPP;
8897 
8898 	if (IS_ERR(wdev)) {
8899 		err = PTR_ERR(wdev);
8900 		if (err != -EINVAL)
8901 			return err;
8902 		wdev = NULL;
8903 	} else if (wdev->wiphy != &rdev->wiphy) {
8904 		return -EINVAL;
8905 	}
8906 
8907 	if (!info->attrs[NL80211_ATTR_TESTDATA])
8908 		return -EINVAL;
8909 
8910 	rdev->cur_cmd_info = info;
8911 	err = rdev_testmode_cmd(rdev, wdev,
8912 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
8913 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
8914 	rdev->cur_cmd_info = NULL;
8915 
8916 	return err;
8917 }
8918 
8919 static int nl80211_testmode_dump(struct sk_buff *skb,
8920 				 struct netlink_callback *cb)
8921 {
8922 	struct cfg80211_registered_device *rdev;
8923 	int err;
8924 	long phy_idx;
8925 	void *data = NULL;
8926 	int data_len = 0;
8927 
8928 	rtnl_lock();
8929 
8930 	if (cb->args[0]) {
8931 		/*
8932 		 * 0 is a valid index, but not valid for args[0],
8933 		 * so we need to offset by 1.
8934 		 */
8935 		phy_idx = cb->args[0] - 1;
8936 
8937 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
8938 		if (!rdev) {
8939 			err = -ENOENT;
8940 			goto out_err;
8941 		}
8942 	} else {
8943 		struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8944 
8945 		err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
8946 				  attrbuf, nl80211_fam.maxattr,
8947 				  nl80211_policy, NULL);
8948 		if (err)
8949 			goto out_err;
8950 
8951 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
8952 		if (IS_ERR(rdev)) {
8953 			err = PTR_ERR(rdev);
8954 			goto out_err;
8955 		}
8956 		phy_idx = rdev->wiphy_idx;
8957 
8958 		if (attrbuf[NL80211_ATTR_TESTDATA])
8959 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
8960 	}
8961 
8962 	if (cb->args[1]) {
8963 		data = nla_data((void *)cb->args[1]);
8964 		data_len = nla_len((void *)cb->args[1]);
8965 	}
8966 
8967 	if (!rdev->ops->testmode_dump) {
8968 		err = -EOPNOTSUPP;
8969 		goto out_err;
8970 	}
8971 
8972 	while (1) {
8973 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
8974 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
8975 					   NL80211_CMD_TESTMODE);
8976 		struct nlattr *tmdata;
8977 
8978 		if (!hdr)
8979 			break;
8980 
8981 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
8982 			genlmsg_cancel(skb, hdr);
8983 			break;
8984 		}
8985 
8986 		tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
8987 		if (!tmdata) {
8988 			genlmsg_cancel(skb, hdr);
8989 			break;
8990 		}
8991 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
8992 		nla_nest_end(skb, tmdata);
8993 
8994 		if (err == -ENOBUFS || err == -ENOENT) {
8995 			genlmsg_cancel(skb, hdr);
8996 			break;
8997 		} else if (err) {
8998 			genlmsg_cancel(skb, hdr);
8999 			goto out_err;
9000 		}
9001 
9002 		genlmsg_end(skb, hdr);
9003 	}
9004 
9005 	err = skb->len;
9006 	/* see above */
9007 	cb->args[0] = phy_idx + 1;
9008  out_err:
9009 	rtnl_unlock();
9010 	return err;
9011 }
9012 #endif
9013 
9014 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9015 {
9016 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9017 	struct net_device *dev = info->user_ptr[1];
9018 	struct cfg80211_connect_params connect;
9019 	struct wiphy *wiphy;
9020 	struct cfg80211_cached_keys *connkeys = NULL;
9021 	int err;
9022 
9023 	memset(&connect, 0, sizeof(connect));
9024 
9025 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9026 		return -EINVAL;
9027 
9028 	if (!info->attrs[NL80211_ATTR_SSID] ||
9029 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9030 		return -EINVAL;
9031 
9032 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9033 		connect.auth_type =
9034 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9035 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9036 					     NL80211_CMD_CONNECT))
9037 			return -EINVAL;
9038 	} else
9039 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9040 
9041 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9042 
9043 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9044 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9045 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9046 		return -EINVAL;
9047 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9048 
9049 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9050 				      NL80211_MAX_NR_CIPHER_SUITES);
9051 	if (err)
9052 		return err;
9053 
9054 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9055 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9056 		return -EOPNOTSUPP;
9057 
9058 	wiphy = &rdev->wiphy;
9059 
9060 	connect.bg_scan_period = -1;
9061 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9062 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9063 		connect.bg_scan_period =
9064 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9065 	}
9066 
9067 	if (info->attrs[NL80211_ATTR_MAC])
9068 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9069 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
9070 		connect.bssid_hint =
9071 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9072 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9073 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9074 
9075 	if (info->attrs[NL80211_ATTR_IE]) {
9076 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9077 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9078 	}
9079 
9080 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9081 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9082 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
9083 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9084 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
9085 			return -EOPNOTSUPP;
9086 
9087 		if (connect.mfp != NL80211_MFP_REQUIRED &&
9088 		    connect.mfp != NL80211_MFP_NO &&
9089 		    connect.mfp != NL80211_MFP_OPTIONAL)
9090 			return -EINVAL;
9091 	} else {
9092 		connect.mfp = NL80211_MFP_NO;
9093 	}
9094 
9095 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9096 		connect.prev_bssid =
9097 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9098 
9099 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9100 		connect.channel = nl80211_get_valid_chan(
9101 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9102 		if (!connect.channel)
9103 			return -EINVAL;
9104 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9105 		connect.channel_hint = nl80211_get_valid_chan(
9106 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9107 		if (!connect.channel_hint)
9108 			return -EINVAL;
9109 	}
9110 
9111 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9112 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9113 		if (IS_ERR(connkeys))
9114 			return PTR_ERR(connkeys);
9115 	}
9116 
9117 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9118 		connect.flags |= ASSOC_REQ_DISABLE_HT;
9119 
9120 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9121 		memcpy(&connect.ht_capa_mask,
9122 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9123 		       sizeof(connect.ht_capa_mask));
9124 
9125 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9126 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9127 			kzfree(connkeys);
9128 			return -EINVAL;
9129 		}
9130 		memcpy(&connect.ht_capa,
9131 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9132 		       sizeof(connect.ht_capa));
9133 	}
9134 
9135 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9136 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
9137 
9138 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9139 		memcpy(&connect.vht_capa_mask,
9140 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9141 		       sizeof(connect.vht_capa_mask));
9142 
9143 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9144 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
9145 			kzfree(connkeys);
9146 			return -EINVAL;
9147 		}
9148 		memcpy(&connect.vht_capa,
9149 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9150 		       sizeof(connect.vht_capa));
9151 	}
9152 
9153 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9154 		if (!((rdev->wiphy.features &
9155 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9156 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9157 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9158 					     NL80211_EXT_FEATURE_RRM)) {
9159 			kzfree(connkeys);
9160 			return -EINVAL;
9161 		}
9162 		connect.flags |= ASSOC_REQ_USE_RRM;
9163 	}
9164 
9165 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
9166 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
9167 		kzfree(connkeys);
9168 		return -EOPNOTSUPP;
9169 	}
9170 
9171 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
9172 		/* bss selection makes no sense if bssid is set */
9173 		if (connect.bssid) {
9174 			kzfree(connkeys);
9175 			return -EINVAL;
9176 		}
9177 
9178 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
9179 				       wiphy, &connect.bss_select);
9180 		if (err) {
9181 			kzfree(connkeys);
9182 			return err;
9183 		}
9184 	}
9185 
9186 	if (wiphy_ext_feature_isset(&rdev->wiphy,
9187 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
9188 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9189 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9190 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9191 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9192 		connect.fils_erp_username =
9193 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9194 		connect.fils_erp_username_len =
9195 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9196 		connect.fils_erp_realm =
9197 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9198 		connect.fils_erp_realm_len =
9199 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9200 		connect.fils_erp_next_seq_num =
9201 			nla_get_u16(
9202 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9203 		connect.fils_erp_rrk =
9204 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9205 		connect.fils_erp_rrk_len =
9206 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9207 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9208 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9209 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9210 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9211 		kzfree(connkeys);
9212 		return -EINVAL;
9213 	}
9214 
9215 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
9216 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9217 			kzfree(connkeys);
9218 			GENL_SET_ERR_MSG(info,
9219 					 "external auth requires connection ownership");
9220 			return -EINVAL;
9221 		}
9222 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
9223 	}
9224 
9225 	wdev_lock(dev->ieee80211_ptr);
9226 
9227 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
9228 			       connect.prev_bssid);
9229 	if (err)
9230 		kzfree(connkeys);
9231 
9232 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9233 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9234 		if (connect.bssid)
9235 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9236 			       connect.bssid, ETH_ALEN);
9237 		else
9238 			memset(dev->ieee80211_ptr->disconnect_bssid,
9239 			       0, ETH_ALEN);
9240 	}
9241 
9242 	wdev_unlock(dev->ieee80211_ptr);
9243 
9244 	return err;
9245 }
9246 
9247 static int nl80211_update_connect_params(struct sk_buff *skb,
9248 					 struct genl_info *info)
9249 {
9250 	struct cfg80211_connect_params connect = {};
9251 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9252 	struct net_device *dev = info->user_ptr[1];
9253 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9254 	u32 changed = 0;
9255 	int ret;
9256 
9257 	if (!rdev->ops->update_connect_params)
9258 		return -EOPNOTSUPP;
9259 
9260 	if (info->attrs[NL80211_ATTR_IE]) {
9261 		if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9262 			return -EINVAL;
9263 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9264 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9265 		changed |= UPDATE_ASSOC_IES;
9266 	}
9267 
9268 	wdev_lock(dev->ieee80211_ptr);
9269 	if (!wdev->current_bss)
9270 		ret = -ENOLINK;
9271 	else
9272 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
9273 	wdev_unlock(dev->ieee80211_ptr);
9274 
9275 	return ret;
9276 }
9277 
9278 static int nl80211_disconnect(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 	u16 reason;
9283 	int ret;
9284 
9285 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9286 		reason = WLAN_REASON_DEAUTH_LEAVING;
9287 	else
9288 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9289 
9290 	if (reason == 0)
9291 		return -EINVAL;
9292 
9293 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9294 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9295 		return -EOPNOTSUPP;
9296 
9297 	wdev_lock(dev->ieee80211_ptr);
9298 	ret = cfg80211_disconnect(rdev, dev, reason, true);
9299 	wdev_unlock(dev->ieee80211_ptr);
9300 	return ret;
9301 }
9302 
9303 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
9304 {
9305 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9306 	struct net *net;
9307 	int err;
9308 
9309 	if (info->attrs[NL80211_ATTR_PID]) {
9310 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
9311 
9312 		net = get_net_ns_by_pid(pid);
9313 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
9314 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
9315 
9316 		net = get_net_ns_by_fd(fd);
9317 	} else {
9318 		return -EINVAL;
9319 	}
9320 
9321 	if (IS_ERR(net))
9322 		return PTR_ERR(net);
9323 
9324 	err = 0;
9325 
9326 	/* check if anything to do */
9327 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
9328 		err = cfg80211_switch_netns(rdev, net);
9329 
9330 	put_net(net);
9331 	return err;
9332 }
9333 
9334 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
9335 {
9336 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9337 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
9338 			struct cfg80211_pmksa *pmksa) = NULL;
9339 	struct net_device *dev = info->user_ptr[1];
9340 	struct cfg80211_pmksa pmksa;
9341 
9342 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
9343 
9344 	if (!info->attrs[NL80211_ATTR_PMKID])
9345 		return -EINVAL;
9346 
9347 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
9348 
9349 	if (info->attrs[NL80211_ATTR_MAC]) {
9350 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9351 	} else if (info->attrs[NL80211_ATTR_SSID] &&
9352 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
9353 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
9354 		    info->attrs[NL80211_ATTR_PMK])) {
9355 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9356 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9357 		pmksa.cache_id =
9358 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
9359 	} else {
9360 		return -EINVAL;
9361 	}
9362 	if (info->attrs[NL80211_ATTR_PMK]) {
9363 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9364 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
9365 	}
9366 
9367 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9368 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9369 		return -EOPNOTSUPP;
9370 
9371 	switch (info->genlhdr->cmd) {
9372 	case NL80211_CMD_SET_PMKSA:
9373 		rdev_ops = rdev->ops->set_pmksa;
9374 		break;
9375 	case NL80211_CMD_DEL_PMKSA:
9376 		rdev_ops = rdev->ops->del_pmksa;
9377 		break;
9378 	default:
9379 		WARN_ON(1);
9380 		break;
9381 	}
9382 
9383 	if (!rdev_ops)
9384 		return -EOPNOTSUPP;
9385 
9386 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
9387 }
9388 
9389 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
9390 {
9391 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9392 	struct net_device *dev = info->user_ptr[1];
9393 
9394 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9395 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9396 		return -EOPNOTSUPP;
9397 
9398 	if (!rdev->ops->flush_pmksa)
9399 		return -EOPNOTSUPP;
9400 
9401 	return rdev_flush_pmksa(rdev, dev);
9402 }
9403 
9404 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
9405 {
9406 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9407 	struct net_device *dev = info->user_ptr[1];
9408 	u8 action_code, dialog_token;
9409 	u32 peer_capability = 0;
9410 	u16 status_code;
9411 	u8 *peer;
9412 	bool initiator;
9413 
9414 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9415 	    !rdev->ops->tdls_mgmt)
9416 		return -EOPNOTSUPP;
9417 
9418 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
9419 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
9420 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
9421 	    !info->attrs[NL80211_ATTR_IE] ||
9422 	    !info->attrs[NL80211_ATTR_MAC])
9423 		return -EINVAL;
9424 
9425 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9426 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
9427 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
9428 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
9429 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
9430 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
9431 		peer_capability =
9432 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
9433 
9434 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
9435 			      dialog_token, status_code, peer_capability,
9436 			      initiator,
9437 			      nla_data(info->attrs[NL80211_ATTR_IE]),
9438 			      nla_len(info->attrs[NL80211_ATTR_IE]));
9439 }
9440 
9441 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
9442 {
9443 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9444 	struct net_device *dev = info->user_ptr[1];
9445 	enum nl80211_tdls_operation operation;
9446 	u8 *peer;
9447 
9448 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9449 	    !rdev->ops->tdls_oper)
9450 		return -EOPNOTSUPP;
9451 
9452 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
9453 	    !info->attrs[NL80211_ATTR_MAC])
9454 		return -EINVAL;
9455 
9456 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
9457 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9458 
9459 	return rdev_tdls_oper(rdev, dev, peer, operation);
9460 }
9461 
9462 static int nl80211_remain_on_channel(struct sk_buff *skb,
9463 				     struct genl_info *info)
9464 {
9465 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9466 	struct wireless_dev *wdev = info->user_ptr[1];
9467 	struct cfg80211_chan_def chandef;
9468 	const struct cfg80211_chan_def *compat_chandef;
9469 	struct sk_buff *msg;
9470 	void *hdr;
9471 	u64 cookie;
9472 	u32 duration;
9473 	int err;
9474 
9475 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9476 	    !info->attrs[NL80211_ATTR_DURATION])
9477 		return -EINVAL;
9478 
9479 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9480 
9481 	if (!rdev->ops->remain_on_channel ||
9482 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
9483 		return -EOPNOTSUPP;
9484 
9485 	/*
9486 	 * We should be on that channel for at least a minimum amount of
9487 	 * time (10ms) but no longer than the driver supports.
9488 	 */
9489 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9490 	    duration > rdev->wiphy.max_remain_on_channel_duration)
9491 		return -EINVAL;
9492 
9493 	err = nl80211_parse_chandef(rdev, info, &chandef);
9494 	if (err)
9495 		return err;
9496 
9497 	wdev_lock(wdev);
9498 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
9499 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
9500 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
9501 							     &chandef);
9502 		if (compat_chandef != &chandef) {
9503 			wdev_unlock(wdev);
9504 			return -EBUSY;
9505 		}
9506 	}
9507 	wdev_unlock(wdev);
9508 
9509 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9510 	if (!msg)
9511 		return -ENOMEM;
9512 
9513 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9514 			     NL80211_CMD_REMAIN_ON_CHANNEL);
9515 	if (!hdr) {
9516 		err = -ENOBUFS;
9517 		goto free_msg;
9518 	}
9519 
9520 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
9521 				     duration, &cookie);
9522 
9523 	if (err)
9524 		goto free_msg;
9525 
9526 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9527 			      NL80211_ATTR_PAD))
9528 		goto nla_put_failure;
9529 
9530 	genlmsg_end(msg, hdr);
9531 
9532 	return genlmsg_reply(msg, info);
9533 
9534  nla_put_failure:
9535 	err = -ENOBUFS;
9536  free_msg:
9537 	nlmsg_free(msg);
9538 	return err;
9539 }
9540 
9541 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
9542 					    struct genl_info *info)
9543 {
9544 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9545 	struct wireless_dev *wdev = info->user_ptr[1];
9546 	u64 cookie;
9547 
9548 	if (!info->attrs[NL80211_ATTR_COOKIE])
9549 		return -EINVAL;
9550 
9551 	if (!rdev->ops->cancel_remain_on_channel)
9552 		return -EOPNOTSUPP;
9553 
9554 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9555 
9556 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
9557 }
9558 
9559 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
9560 				       struct genl_info *info)
9561 {
9562 	struct cfg80211_bitrate_mask mask;
9563 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9564 	struct net_device *dev = info->user_ptr[1];
9565 	int err;
9566 
9567 	if (!rdev->ops->set_bitrate_mask)
9568 		return -EOPNOTSUPP;
9569 
9570 	err = nl80211_parse_tx_bitrate_mask(info, &mask);
9571 	if (err)
9572 		return err;
9573 
9574 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
9575 }
9576 
9577 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
9578 {
9579 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9580 	struct wireless_dev *wdev = info->user_ptr[1];
9581 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
9582 
9583 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
9584 		return -EINVAL;
9585 
9586 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
9587 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
9588 
9589 	switch (wdev->iftype) {
9590 	case NL80211_IFTYPE_STATION:
9591 	case NL80211_IFTYPE_ADHOC:
9592 	case NL80211_IFTYPE_P2P_CLIENT:
9593 	case NL80211_IFTYPE_AP:
9594 	case NL80211_IFTYPE_AP_VLAN:
9595 	case NL80211_IFTYPE_MESH_POINT:
9596 	case NL80211_IFTYPE_P2P_GO:
9597 	case NL80211_IFTYPE_P2P_DEVICE:
9598 		break;
9599 	case NL80211_IFTYPE_NAN:
9600 	default:
9601 		return -EOPNOTSUPP;
9602 	}
9603 
9604 	/* not much point in registering if we can't reply */
9605 	if (!rdev->ops->mgmt_tx)
9606 		return -EOPNOTSUPP;
9607 
9608 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
9609 			nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
9610 			nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
9611 }
9612 
9613 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
9614 {
9615 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9616 	struct wireless_dev *wdev = info->user_ptr[1];
9617 	struct cfg80211_chan_def chandef;
9618 	int err;
9619 	void *hdr = NULL;
9620 	u64 cookie;
9621 	struct sk_buff *msg = NULL;
9622 	struct cfg80211_mgmt_tx_params params = {
9623 		.dont_wait_for_ack =
9624 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
9625 	};
9626 
9627 	if (!info->attrs[NL80211_ATTR_FRAME])
9628 		return -EINVAL;
9629 
9630 	if (!rdev->ops->mgmt_tx)
9631 		return -EOPNOTSUPP;
9632 
9633 	switch (wdev->iftype) {
9634 	case NL80211_IFTYPE_P2P_DEVICE:
9635 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9636 			return -EINVAL;
9637 	case NL80211_IFTYPE_STATION:
9638 	case NL80211_IFTYPE_ADHOC:
9639 	case NL80211_IFTYPE_P2P_CLIENT:
9640 	case NL80211_IFTYPE_AP:
9641 	case NL80211_IFTYPE_AP_VLAN:
9642 	case NL80211_IFTYPE_MESH_POINT:
9643 	case NL80211_IFTYPE_P2P_GO:
9644 		break;
9645 	case NL80211_IFTYPE_NAN:
9646 	default:
9647 		return -EOPNOTSUPP;
9648 	}
9649 
9650 	if (info->attrs[NL80211_ATTR_DURATION]) {
9651 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9652 			return -EINVAL;
9653 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9654 
9655 		/*
9656 		 * We should wait on the channel for at least a minimum amount
9657 		 * of time (10ms) but no longer than the driver supports.
9658 		 */
9659 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9660 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
9661 			return -EINVAL;
9662 	}
9663 
9664 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
9665 
9666 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9667 		return -EINVAL;
9668 
9669 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9670 
9671 	/* get the channel if any has been specified, otherwise pass NULL to
9672 	 * the driver. The latter will use the current one
9673 	 */
9674 	chandef.chan = NULL;
9675 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9676 		err = nl80211_parse_chandef(rdev, info, &chandef);
9677 		if (err)
9678 			return err;
9679 	}
9680 
9681 	if (!chandef.chan && params.offchan)
9682 		return -EINVAL;
9683 
9684 	wdev_lock(wdev);
9685 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
9686 		wdev_unlock(wdev);
9687 		return -EBUSY;
9688 	}
9689 	wdev_unlock(wdev);
9690 
9691 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
9692 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
9693 
9694 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
9695 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9696 		int i;
9697 
9698 		if (len % sizeof(u16))
9699 			return -EINVAL;
9700 
9701 		params.n_csa_offsets = len / sizeof(u16);
9702 		params.csa_offsets =
9703 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9704 
9705 		/* check that all the offsets fit the frame */
9706 		for (i = 0; i < params.n_csa_offsets; i++) {
9707 			if (params.csa_offsets[i] >= params.len)
9708 				return -EINVAL;
9709 		}
9710 	}
9711 
9712 	if (!params.dont_wait_for_ack) {
9713 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9714 		if (!msg)
9715 			return -ENOMEM;
9716 
9717 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9718 				     NL80211_CMD_FRAME);
9719 		if (!hdr) {
9720 			err = -ENOBUFS;
9721 			goto free_msg;
9722 		}
9723 	}
9724 
9725 	params.chan = chandef.chan;
9726 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
9727 	if (err)
9728 		goto free_msg;
9729 
9730 	if (msg) {
9731 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9732 				      NL80211_ATTR_PAD))
9733 			goto nla_put_failure;
9734 
9735 		genlmsg_end(msg, hdr);
9736 		return genlmsg_reply(msg, info);
9737 	}
9738 
9739 	return 0;
9740 
9741  nla_put_failure:
9742 	err = -ENOBUFS;
9743  free_msg:
9744 	nlmsg_free(msg);
9745 	return err;
9746 }
9747 
9748 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
9749 {
9750 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9751 	struct wireless_dev *wdev = info->user_ptr[1];
9752 	u64 cookie;
9753 
9754 	if (!info->attrs[NL80211_ATTR_COOKIE])
9755 		return -EINVAL;
9756 
9757 	if (!rdev->ops->mgmt_tx_cancel_wait)
9758 		return -EOPNOTSUPP;
9759 
9760 	switch (wdev->iftype) {
9761 	case NL80211_IFTYPE_STATION:
9762 	case NL80211_IFTYPE_ADHOC:
9763 	case NL80211_IFTYPE_P2P_CLIENT:
9764 	case NL80211_IFTYPE_AP:
9765 	case NL80211_IFTYPE_AP_VLAN:
9766 	case NL80211_IFTYPE_P2P_GO:
9767 	case NL80211_IFTYPE_P2P_DEVICE:
9768 		break;
9769 	case NL80211_IFTYPE_NAN:
9770 	default:
9771 		return -EOPNOTSUPP;
9772 	}
9773 
9774 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9775 
9776 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
9777 }
9778 
9779 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
9780 {
9781 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9782 	struct wireless_dev *wdev;
9783 	struct net_device *dev = info->user_ptr[1];
9784 	u8 ps_state;
9785 	bool state;
9786 	int err;
9787 
9788 	if (!info->attrs[NL80211_ATTR_PS_STATE])
9789 		return -EINVAL;
9790 
9791 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
9792 
9793 	if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
9794 		return -EINVAL;
9795 
9796 	wdev = dev->ieee80211_ptr;
9797 
9798 	if (!rdev->ops->set_power_mgmt)
9799 		return -EOPNOTSUPP;
9800 
9801 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
9802 
9803 	if (state == wdev->ps)
9804 		return 0;
9805 
9806 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
9807 	if (!err)
9808 		wdev->ps = state;
9809 	return err;
9810 }
9811 
9812 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
9813 {
9814 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9815 	enum nl80211_ps_state ps_state;
9816 	struct wireless_dev *wdev;
9817 	struct net_device *dev = info->user_ptr[1];
9818 	struct sk_buff *msg;
9819 	void *hdr;
9820 	int err;
9821 
9822 	wdev = dev->ieee80211_ptr;
9823 
9824 	if (!rdev->ops->set_power_mgmt)
9825 		return -EOPNOTSUPP;
9826 
9827 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9828 	if (!msg)
9829 		return -ENOMEM;
9830 
9831 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9832 			     NL80211_CMD_GET_POWER_SAVE);
9833 	if (!hdr) {
9834 		err = -ENOBUFS;
9835 		goto free_msg;
9836 	}
9837 
9838 	if (wdev->ps)
9839 		ps_state = NL80211_PS_ENABLED;
9840 	else
9841 		ps_state = NL80211_PS_DISABLED;
9842 
9843 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
9844 		goto nla_put_failure;
9845 
9846 	genlmsg_end(msg, hdr);
9847 	return genlmsg_reply(msg, info);
9848 
9849  nla_put_failure:
9850 	err = -ENOBUFS;
9851  free_msg:
9852 	nlmsg_free(msg);
9853 	return err;
9854 }
9855 
9856 static const struct nla_policy
9857 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
9858 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
9859 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
9860 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
9861 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
9862 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
9863 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
9864 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
9865 };
9866 
9867 static int nl80211_set_cqm_txe(struct genl_info *info,
9868 			       u32 rate, u32 pkts, u32 intvl)
9869 {
9870 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9871 	struct net_device *dev = info->user_ptr[1];
9872 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9873 
9874 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
9875 		return -EINVAL;
9876 
9877 	if (!rdev->ops->set_cqm_txe_config)
9878 		return -EOPNOTSUPP;
9879 
9880 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
9881 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9882 		return -EOPNOTSUPP;
9883 
9884 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
9885 }
9886 
9887 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
9888 				    struct net_device *dev)
9889 {
9890 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9891 	s32 last, low, high;
9892 	u32 hyst;
9893 	int i, n;
9894 	int err;
9895 
9896 	/* RSSI reporting disabled? */
9897 	if (!wdev->cqm_config)
9898 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
9899 
9900 	/*
9901 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
9902 	 * event has been received yet, we should receive an event after a
9903 	 * connection is established and enough beacons received to calculate
9904 	 * the average.
9905 	 */
9906 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
9907 	    rdev->ops->get_station) {
9908 		struct station_info sinfo = {};
9909 		u8 *mac_addr;
9910 
9911 		mac_addr = wdev->current_bss->pub.bssid;
9912 
9913 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
9914 		if (err)
9915 			return err;
9916 
9917 		if (sinfo.filled & BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
9918 			wdev->cqm_config->last_rssi_event_value =
9919 				(s8) sinfo.rx_beacon_signal_avg;
9920 	}
9921 
9922 	last = wdev->cqm_config->last_rssi_event_value;
9923 	hyst = wdev->cqm_config->rssi_hyst;
9924 	n = wdev->cqm_config->n_rssi_thresholds;
9925 
9926 	for (i = 0; i < n; i++)
9927 		if (last < wdev->cqm_config->rssi_thresholds[i])
9928 			break;
9929 
9930 	low = i > 0 ?
9931 		(wdev->cqm_config->rssi_thresholds[i - 1] - hyst) : S32_MIN;
9932 	high = i < n ?
9933 		(wdev->cqm_config->rssi_thresholds[i] + hyst - 1) : S32_MAX;
9934 
9935 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
9936 }
9937 
9938 static int nl80211_set_cqm_rssi(struct genl_info *info,
9939 				const s32 *thresholds, int n_thresholds,
9940 				u32 hysteresis)
9941 {
9942 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9943 	struct net_device *dev = info->user_ptr[1];
9944 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9945 	int i, err;
9946 	s32 prev = S32_MIN;
9947 
9948 	/* Check all values negative and sorted */
9949 	for (i = 0; i < n_thresholds; i++) {
9950 		if (thresholds[i] > 0 || thresholds[i] <= prev)
9951 			return -EINVAL;
9952 
9953 		prev = thresholds[i];
9954 	}
9955 
9956 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
9957 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9958 		return -EOPNOTSUPP;
9959 
9960 	wdev_lock(wdev);
9961 	cfg80211_cqm_config_free(wdev);
9962 	wdev_unlock(wdev);
9963 
9964 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
9965 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
9966 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
9967 
9968 		return rdev_set_cqm_rssi_config(rdev, dev,
9969 						thresholds[0], hysteresis);
9970 	}
9971 
9972 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
9973 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
9974 		return -EOPNOTSUPP;
9975 
9976 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
9977 		n_thresholds = 0;
9978 
9979 	wdev_lock(wdev);
9980 	if (n_thresholds) {
9981 		struct cfg80211_cqm_config *cqm_config;
9982 
9983 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
9984 				     n_thresholds * sizeof(s32), GFP_KERNEL);
9985 		if (!cqm_config) {
9986 			err = -ENOMEM;
9987 			goto unlock;
9988 		}
9989 
9990 		cqm_config->rssi_hyst = hysteresis;
9991 		cqm_config->n_rssi_thresholds = n_thresholds;
9992 		memcpy(cqm_config->rssi_thresholds, thresholds,
9993 		       n_thresholds * sizeof(s32));
9994 
9995 		wdev->cqm_config = cqm_config;
9996 	}
9997 
9998 	err = cfg80211_cqm_rssi_update(rdev, dev);
9999 
10000 unlock:
10001 	wdev_unlock(wdev);
10002 
10003 	return err;
10004 }
10005 
10006 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10007 {
10008 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10009 	struct nlattr *cqm;
10010 	int err;
10011 
10012 	cqm = info->attrs[NL80211_ATTR_CQM];
10013 	if (!cqm)
10014 		return -EINVAL;
10015 
10016 	err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
10017 			       nl80211_attr_cqm_policy, info->extack);
10018 	if (err)
10019 		return err;
10020 
10021 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10022 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10023 		const s32 *thresholds =
10024 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10025 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10026 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10027 
10028 		if (len % 4)
10029 			return -EINVAL;
10030 
10031 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10032 					    hysteresis);
10033 	}
10034 
10035 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10036 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10037 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10038 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10039 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10040 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10041 
10042 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10043 	}
10044 
10045 	return -EINVAL;
10046 }
10047 
10048 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10049 {
10050 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10051 	struct net_device *dev = info->user_ptr[1];
10052 	struct ocb_setup setup = {};
10053 	int err;
10054 
10055 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10056 	if (err)
10057 		return err;
10058 
10059 	return cfg80211_join_ocb(rdev, dev, &setup);
10060 }
10061 
10062 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
10063 {
10064 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10065 	struct net_device *dev = info->user_ptr[1];
10066 
10067 	return cfg80211_leave_ocb(rdev, dev);
10068 }
10069 
10070 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
10071 {
10072 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10073 	struct net_device *dev = info->user_ptr[1];
10074 	struct mesh_config cfg;
10075 	struct mesh_setup setup;
10076 	int err;
10077 
10078 	/* start with default */
10079 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
10080 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
10081 
10082 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
10083 		/* and parse parameters if given */
10084 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
10085 		if (err)
10086 			return err;
10087 	}
10088 
10089 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
10090 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
10091 		return -EINVAL;
10092 
10093 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
10094 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
10095 
10096 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10097 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
10098 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10099 			return -EINVAL;
10100 
10101 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
10102 		setup.beacon_interval =
10103 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10104 
10105 		err = cfg80211_validate_beacon_int(rdev,
10106 						   NL80211_IFTYPE_MESH_POINT,
10107 						   setup.beacon_interval);
10108 		if (err)
10109 			return err;
10110 	}
10111 
10112 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
10113 		setup.dtim_period =
10114 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
10115 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
10116 			return -EINVAL;
10117 	}
10118 
10119 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
10120 		/* parse additional setup parameters if given */
10121 		err = nl80211_parse_mesh_setup(info, &setup);
10122 		if (err)
10123 			return err;
10124 	}
10125 
10126 	if (setup.user_mpm)
10127 		cfg.auto_open_plinks = false;
10128 
10129 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10130 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10131 		if (err)
10132 			return err;
10133 	} else {
10134 		/* __cfg80211_join_mesh() will sort it out */
10135 		setup.chandef.chan = NULL;
10136 	}
10137 
10138 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10139 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10140 		int n_rates =
10141 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10142 		struct ieee80211_supported_band *sband;
10143 
10144 		if (!setup.chandef.chan)
10145 			return -EINVAL;
10146 
10147 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
10148 
10149 		err = ieee80211_get_ratemask(sband, rates, n_rates,
10150 					     &setup.basic_rates);
10151 		if (err)
10152 			return err;
10153 	}
10154 
10155 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
10156 		err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
10157 		if (err)
10158 			return err;
10159 
10160 		if (!setup.chandef.chan)
10161 			return -EINVAL;
10162 
10163 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
10164 					      &setup.beacon_rate);
10165 		if (err)
10166 			return err;
10167 	}
10168 
10169 	setup.userspace_handles_dfs =
10170 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10171 
10172 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10173 		int r = validate_pae_over_nl80211(rdev, info);
10174 
10175 		if (r < 0)
10176 			return r;
10177 
10178 		setup.control_port_over_nl80211 = true;
10179 	}
10180 
10181 	wdev_lock(dev->ieee80211_ptr);
10182 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
10183 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
10184 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10185 	wdev_unlock(dev->ieee80211_ptr);
10186 
10187 	return err;
10188 }
10189 
10190 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
10191 {
10192 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10193 	struct net_device *dev = info->user_ptr[1];
10194 
10195 	return cfg80211_leave_mesh(rdev, dev);
10196 }
10197 
10198 #ifdef CONFIG_PM
10199 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
10200 					struct cfg80211_registered_device *rdev)
10201 {
10202 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
10203 	struct nlattr *nl_pats, *nl_pat;
10204 	int i, pat_len;
10205 
10206 	if (!wowlan->n_patterns)
10207 		return 0;
10208 
10209 	nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
10210 	if (!nl_pats)
10211 		return -ENOBUFS;
10212 
10213 	for (i = 0; i < wowlan->n_patterns; i++) {
10214 		nl_pat = nla_nest_start(msg, i + 1);
10215 		if (!nl_pat)
10216 			return -ENOBUFS;
10217 		pat_len = wowlan->patterns[i].pattern_len;
10218 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
10219 			    wowlan->patterns[i].mask) ||
10220 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10221 			    wowlan->patterns[i].pattern) ||
10222 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10223 				wowlan->patterns[i].pkt_offset))
10224 			return -ENOBUFS;
10225 		nla_nest_end(msg, nl_pat);
10226 	}
10227 	nla_nest_end(msg, nl_pats);
10228 
10229 	return 0;
10230 }
10231 
10232 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
10233 				   struct cfg80211_wowlan_tcp *tcp)
10234 {
10235 	struct nlattr *nl_tcp;
10236 
10237 	if (!tcp)
10238 		return 0;
10239 
10240 	nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
10241 	if (!nl_tcp)
10242 		return -ENOBUFS;
10243 
10244 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
10245 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
10246 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
10247 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
10248 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
10249 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
10250 		    tcp->payload_len, tcp->payload) ||
10251 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
10252 			tcp->data_interval) ||
10253 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
10254 		    tcp->wake_len, tcp->wake_data) ||
10255 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
10256 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
10257 		return -ENOBUFS;
10258 
10259 	if (tcp->payload_seq.len &&
10260 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
10261 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
10262 		return -ENOBUFS;
10263 
10264 	if (tcp->payload_tok.len &&
10265 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
10266 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
10267 		    &tcp->payload_tok))
10268 		return -ENOBUFS;
10269 
10270 	nla_nest_end(msg, nl_tcp);
10271 
10272 	return 0;
10273 }
10274 
10275 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
10276 				  struct cfg80211_sched_scan_request *req)
10277 {
10278 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
10279 	int i;
10280 
10281 	if (!req)
10282 		return 0;
10283 
10284 	nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
10285 	if (!nd)
10286 		return -ENOBUFS;
10287 
10288 	if (req->n_scan_plans == 1 &&
10289 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
10290 			req->scan_plans[0].interval * 1000))
10291 		return -ENOBUFS;
10292 
10293 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
10294 		return -ENOBUFS;
10295 
10296 	if (req->relative_rssi_set) {
10297 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
10298 
10299 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
10300 			       req->relative_rssi))
10301 			return -ENOBUFS;
10302 
10303 		rssi_adjust.band = req->rssi_adjust.band;
10304 		rssi_adjust.delta = req->rssi_adjust.delta;
10305 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
10306 			    sizeof(rssi_adjust), &rssi_adjust))
10307 			return -ENOBUFS;
10308 	}
10309 
10310 	freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10311 	if (!freqs)
10312 		return -ENOBUFS;
10313 
10314 	for (i = 0; i < req->n_channels; i++) {
10315 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10316 			return -ENOBUFS;
10317 	}
10318 
10319 	nla_nest_end(msg, freqs);
10320 
10321 	if (req->n_match_sets) {
10322 		matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
10323 		if (!matches)
10324 			return -ENOBUFS;
10325 
10326 		for (i = 0; i < req->n_match_sets; i++) {
10327 			match = nla_nest_start(msg, i);
10328 			if (!match)
10329 				return -ENOBUFS;
10330 
10331 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
10332 				    req->match_sets[i].ssid.ssid_len,
10333 				    req->match_sets[i].ssid.ssid))
10334 				return -ENOBUFS;
10335 			nla_nest_end(msg, match);
10336 		}
10337 		nla_nest_end(msg, matches);
10338 	}
10339 
10340 	scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
10341 	if (!scan_plans)
10342 		return -ENOBUFS;
10343 
10344 	for (i = 0; i < req->n_scan_plans; i++) {
10345 		scan_plan = nla_nest_start(msg, i + 1);
10346 		if (!scan_plan)
10347 			return -ENOBUFS;
10348 
10349 		if (!scan_plan ||
10350 		    nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
10351 				req->scan_plans[i].interval) ||
10352 		    (req->scan_plans[i].iterations &&
10353 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
10354 				 req->scan_plans[i].iterations)))
10355 			return -ENOBUFS;
10356 		nla_nest_end(msg, scan_plan);
10357 	}
10358 	nla_nest_end(msg, scan_plans);
10359 
10360 	nla_nest_end(msg, nd);
10361 
10362 	return 0;
10363 }
10364 
10365 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
10366 {
10367 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10368 	struct sk_buff *msg;
10369 	void *hdr;
10370 	u32 size = NLMSG_DEFAULT_SIZE;
10371 
10372 	if (!rdev->wiphy.wowlan)
10373 		return -EOPNOTSUPP;
10374 
10375 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
10376 		/* adjust size to have room for all the data */
10377 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
10378 			rdev->wiphy.wowlan_config->tcp->payload_len +
10379 			rdev->wiphy.wowlan_config->tcp->wake_len +
10380 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
10381 	}
10382 
10383 	msg = nlmsg_new(size, GFP_KERNEL);
10384 	if (!msg)
10385 		return -ENOMEM;
10386 
10387 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10388 			     NL80211_CMD_GET_WOWLAN);
10389 	if (!hdr)
10390 		goto nla_put_failure;
10391 
10392 	if (rdev->wiphy.wowlan_config) {
10393 		struct nlattr *nl_wowlan;
10394 
10395 		nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10396 		if (!nl_wowlan)
10397 			goto nla_put_failure;
10398 
10399 		if ((rdev->wiphy.wowlan_config->any &&
10400 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
10401 		    (rdev->wiphy.wowlan_config->disconnect &&
10402 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
10403 		    (rdev->wiphy.wowlan_config->magic_pkt &&
10404 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
10405 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
10406 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
10407 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
10408 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
10409 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
10410 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
10411 		    (rdev->wiphy.wowlan_config->rfkill_release &&
10412 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
10413 			goto nla_put_failure;
10414 
10415 		if (nl80211_send_wowlan_patterns(msg, rdev))
10416 			goto nla_put_failure;
10417 
10418 		if (nl80211_send_wowlan_tcp(msg,
10419 					    rdev->wiphy.wowlan_config->tcp))
10420 			goto nla_put_failure;
10421 
10422 		if (nl80211_send_wowlan_nd(
10423 			    msg,
10424 			    rdev->wiphy.wowlan_config->nd_config))
10425 			goto nla_put_failure;
10426 
10427 		nla_nest_end(msg, nl_wowlan);
10428 	}
10429 
10430 	genlmsg_end(msg, hdr);
10431 	return genlmsg_reply(msg, info);
10432 
10433 nla_put_failure:
10434 	nlmsg_free(msg);
10435 	return -ENOBUFS;
10436 }
10437 
10438 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
10439 				    struct nlattr *attr,
10440 				    struct cfg80211_wowlan *trig)
10441 {
10442 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
10443 	struct cfg80211_wowlan_tcp *cfg;
10444 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
10445 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
10446 	u32 size;
10447 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
10448 	int err, port;
10449 
10450 	if (!rdev->wiphy.wowlan->tcp)
10451 		return -EINVAL;
10452 
10453 	err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TCP, attr,
10454 			       nl80211_wowlan_tcp_policy, NULL);
10455 	if (err)
10456 		return err;
10457 
10458 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
10459 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
10460 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
10461 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
10462 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
10463 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
10464 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
10465 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
10466 		return -EINVAL;
10467 
10468 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
10469 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
10470 		return -EINVAL;
10471 
10472 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
10473 			rdev->wiphy.wowlan->tcp->data_interval_max ||
10474 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
10475 		return -EINVAL;
10476 
10477 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
10478 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
10479 		return -EINVAL;
10480 
10481 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
10482 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
10483 		return -EINVAL;
10484 
10485 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
10486 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10487 
10488 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10489 		tokens_size = tokln - sizeof(*tok);
10490 
10491 		if (!tok->len || tokens_size % tok->len)
10492 			return -EINVAL;
10493 		if (!rdev->wiphy.wowlan->tcp->tok)
10494 			return -EINVAL;
10495 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
10496 			return -EINVAL;
10497 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
10498 			return -EINVAL;
10499 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
10500 			return -EINVAL;
10501 		if (tok->offset + tok->len > data_size)
10502 			return -EINVAL;
10503 	}
10504 
10505 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
10506 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
10507 		if (!rdev->wiphy.wowlan->tcp->seq)
10508 			return -EINVAL;
10509 		if (seq->len == 0 || seq->len > 4)
10510 			return -EINVAL;
10511 		if (seq->len + seq->offset > data_size)
10512 			return -EINVAL;
10513 	}
10514 
10515 	size = sizeof(*cfg);
10516 	size += data_size;
10517 	size += wake_size + wake_mask_size;
10518 	size += tokens_size;
10519 
10520 	cfg = kzalloc(size, GFP_KERNEL);
10521 	if (!cfg)
10522 		return -ENOMEM;
10523 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
10524 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
10525 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
10526 	       ETH_ALEN);
10527 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
10528 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
10529 	else
10530 		port = 0;
10531 #ifdef CONFIG_INET
10532 	/* allocate a socket and port for it and use it */
10533 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
10534 			    IPPROTO_TCP, &cfg->sock, 1);
10535 	if (err) {
10536 		kfree(cfg);
10537 		return err;
10538 	}
10539 	if (inet_csk_get_port(cfg->sock->sk, port)) {
10540 		sock_release(cfg->sock);
10541 		kfree(cfg);
10542 		return -EADDRINUSE;
10543 	}
10544 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
10545 #else
10546 	if (!port) {
10547 		kfree(cfg);
10548 		return -EINVAL;
10549 	}
10550 	cfg->src_port = port;
10551 #endif
10552 
10553 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
10554 	cfg->payload_len = data_size;
10555 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
10556 	memcpy((void *)cfg->payload,
10557 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
10558 	       data_size);
10559 	if (seq)
10560 		cfg->payload_seq = *seq;
10561 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
10562 	cfg->wake_len = wake_size;
10563 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
10564 	memcpy((void *)cfg->wake_data,
10565 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
10566 	       wake_size);
10567 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
10568 			 data_size + wake_size;
10569 	memcpy((void *)cfg->wake_mask,
10570 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
10571 	       wake_mask_size);
10572 	if (tok) {
10573 		cfg->tokens_size = tokens_size;
10574 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
10575 	}
10576 
10577 	trig->tcp = cfg;
10578 
10579 	return 0;
10580 }
10581 
10582 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
10583 				   const struct wiphy_wowlan_support *wowlan,
10584 				   struct nlattr *attr,
10585 				   struct cfg80211_wowlan *trig)
10586 {
10587 	struct nlattr **tb;
10588 	int err;
10589 
10590 	tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
10591 	if (!tb)
10592 		return -ENOMEM;
10593 
10594 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
10595 		err = -EOPNOTSUPP;
10596 		goto out;
10597 	}
10598 
10599 	err = nla_parse_nested(tb, NL80211_ATTR_MAX, attr, nl80211_policy,
10600 			       NULL);
10601 	if (err)
10602 		goto out;
10603 
10604 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
10605 						   wowlan->max_nd_match_sets);
10606 	err = PTR_ERR_OR_ZERO(trig->nd_config);
10607 	if (err)
10608 		trig->nd_config = NULL;
10609 
10610 out:
10611 	kfree(tb);
10612 	return err;
10613 }
10614 
10615 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
10616 {
10617 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10618 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
10619 	struct cfg80211_wowlan new_triggers = {};
10620 	struct cfg80211_wowlan *ntrig;
10621 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
10622 	int err, i;
10623 	bool prev_enabled = rdev->wiphy.wowlan_config;
10624 	bool regular = false;
10625 
10626 	if (!wowlan)
10627 		return -EOPNOTSUPP;
10628 
10629 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
10630 		cfg80211_rdev_free_wowlan(rdev);
10631 		rdev->wiphy.wowlan_config = NULL;
10632 		goto set_wakeup;
10633 	}
10634 
10635 	err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TRIG,
10636 			       info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
10637 			       nl80211_wowlan_policy, info->extack);
10638 	if (err)
10639 		return err;
10640 
10641 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
10642 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
10643 			return -EINVAL;
10644 		new_triggers.any = true;
10645 	}
10646 
10647 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
10648 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
10649 			return -EINVAL;
10650 		new_triggers.disconnect = true;
10651 		regular = true;
10652 	}
10653 
10654 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
10655 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
10656 			return -EINVAL;
10657 		new_triggers.magic_pkt = true;
10658 		regular = true;
10659 	}
10660 
10661 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
10662 		return -EINVAL;
10663 
10664 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
10665 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
10666 			return -EINVAL;
10667 		new_triggers.gtk_rekey_failure = true;
10668 		regular = true;
10669 	}
10670 
10671 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
10672 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
10673 			return -EINVAL;
10674 		new_triggers.eap_identity_req = true;
10675 		regular = true;
10676 	}
10677 
10678 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
10679 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
10680 			return -EINVAL;
10681 		new_triggers.four_way_handshake = true;
10682 		regular = true;
10683 	}
10684 
10685 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
10686 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
10687 			return -EINVAL;
10688 		new_triggers.rfkill_release = true;
10689 		regular = true;
10690 	}
10691 
10692 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
10693 		struct nlattr *pat;
10694 		int n_patterns = 0;
10695 		int rem, pat_len, mask_len, pkt_offset;
10696 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10697 
10698 		regular = true;
10699 
10700 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10701 				    rem)
10702 			n_patterns++;
10703 		if (n_patterns > wowlan->n_patterns)
10704 			return -EINVAL;
10705 
10706 		new_triggers.patterns = kcalloc(n_patterns,
10707 						sizeof(new_triggers.patterns[0]),
10708 						GFP_KERNEL);
10709 		if (!new_triggers.patterns)
10710 			return -ENOMEM;
10711 
10712 		new_triggers.n_patterns = n_patterns;
10713 		i = 0;
10714 
10715 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10716 				    rem) {
10717 			u8 *mask_pat;
10718 
10719 			nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10720 					 nl80211_packet_pattern_policy,
10721 					 info->extack);
10722 			err = -EINVAL;
10723 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
10724 			    !pat_tb[NL80211_PKTPAT_PATTERN])
10725 				goto error;
10726 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10727 			mask_len = DIV_ROUND_UP(pat_len, 8);
10728 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10729 				goto error;
10730 			if (pat_len > wowlan->pattern_max_len ||
10731 			    pat_len < wowlan->pattern_min_len)
10732 				goto error;
10733 
10734 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
10735 				pkt_offset = 0;
10736 			else
10737 				pkt_offset = nla_get_u32(
10738 					pat_tb[NL80211_PKTPAT_OFFSET]);
10739 			if (pkt_offset > wowlan->max_pkt_offset)
10740 				goto error;
10741 			new_triggers.patterns[i].pkt_offset = pkt_offset;
10742 
10743 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10744 			if (!mask_pat) {
10745 				err = -ENOMEM;
10746 				goto error;
10747 			}
10748 			new_triggers.patterns[i].mask = mask_pat;
10749 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10750 			       mask_len);
10751 			mask_pat += mask_len;
10752 			new_triggers.patterns[i].pattern = mask_pat;
10753 			new_triggers.patterns[i].pattern_len = pat_len;
10754 			memcpy(mask_pat,
10755 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10756 			       pat_len);
10757 			i++;
10758 		}
10759 	}
10760 
10761 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
10762 		regular = true;
10763 		err = nl80211_parse_wowlan_tcp(
10764 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
10765 			&new_triggers);
10766 		if (err)
10767 			goto error;
10768 	}
10769 
10770 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
10771 		regular = true;
10772 		err = nl80211_parse_wowlan_nd(
10773 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
10774 			&new_triggers);
10775 		if (err)
10776 			goto error;
10777 	}
10778 
10779 	/* The 'any' trigger means the device continues operating more or less
10780 	 * as in its normal operation mode and wakes up the host on most of the
10781 	 * normal interrupts (like packet RX, ...)
10782 	 * It therefore makes little sense to combine with the more constrained
10783 	 * wakeup trigger modes.
10784 	 */
10785 	if (new_triggers.any && regular) {
10786 		err = -EINVAL;
10787 		goto error;
10788 	}
10789 
10790 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
10791 	if (!ntrig) {
10792 		err = -ENOMEM;
10793 		goto error;
10794 	}
10795 	cfg80211_rdev_free_wowlan(rdev);
10796 	rdev->wiphy.wowlan_config = ntrig;
10797 
10798  set_wakeup:
10799 	if (rdev->ops->set_wakeup &&
10800 	    prev_enabled != !!rdev->wiphy.wowlan_config)
10801 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
10802 
10803 	return 0;
10804  error:
10805 	for (i = 0; i < new_triggers.n_patterns; i++)
10806 		kfree(new_triggers.patterns[i].mask);
10807 	kfree(new_triggers.patterns);
10808 	if (new_triggers.tcp && new_triggers.tcp->sock)
10809 		sock_release(new_triggers.tcp->sock);
10810 	kfree(new_triggers.tcp);
10811 	kfree(new_triggers.nd_config);
10812 	return err;
10813 }
10814 #endif
10815 
10816 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
10817 				       struct cfg80211_registered_device *rdev)
10818 {
10819 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
10820 	int i, j, pat_len;
10821 	struct cfg80211_coalesce_rules *rule;
10822 
10823 	if (!rdev->coalesce->n_rules)
10824 		return 0;
10825 
10826 	nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
10827 	if (!nl_rules)
10828 		return -ENOBUFS;
10829 
10830 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
10831 		nl_rule = nla_nest_start(msg, i + 1);
10832 		if (!nl_rule)
10833 			return -ENOBUFS;
10834 
10835 		rule = &rdev->coalesce->rules[i];
10836 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
10837 				rule->delay))
10838 			return -ENOBUFS;
10839 
10840 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
10841 				rule->condition))
10842 			return -ENOBUFS;
10843 
10844 		nl_pats = nla_nest_start(msg,
10845 				NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
10846 		if (!nl_pats)
10847 			return -ENOBUFS;
10848 
10849 		for (j = 0; j < rule->n_patterns; j++) {
10850 			nl_pat = nla_nest_start(msg, j + 1);
10851 			if (!nl_pat)
10852 				return -ENOBUFS;
10853 			pat_len = rule->patterns[j].pattern_len;
10854 			if (nla_put(msg, NL80211_PKTPAT_MASK,
10855 				    DIV_ROUND_UP(pat_len, 8),
10856 				    rule->patterns[j].mask) ||
10857 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10858 				    rule->patterns[j].pattern) ||
10859 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10860 					rule->patterns[j].pkt_offset))
10861 				return -ENOBUFS;
10862 			nla_nest_end(msg, nl_pat);
10863 		}
10864 		nla_nest_end(msg, nl_pats);
10865 		nla_nest_end(msg, nl_rule);
10866 	}
10867 	nla_nest_end(msg, nl_rules);
10868 
10869 	return 0;
10870 }
10871 
10872 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
10873 {
10874 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10875 	struct sk_buff *msg;
10876 	void *hdr;
10877 
10878 	if (!rdev->wiphy.coalesce)
10879 		return -EOPNOTSUPP;
10880 
10881 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10882 	if (!msg)
10883 		return -ENOMEM;
10884 
10885 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10886 			     NL80211_CMD_GET_COALESCE);
10887 	if (!hdr)
10888 		goto nla_put_failure;
10889 
10890 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
10891 		goto nla_put_failure;
10892 
10893 	genlmsg_end(msg, hdr);
10894 	return genlmsg_reply(msg, info);
10895 
10896 nla_put_failure:
10897 	nlmsg_free(msg);
10898 	return -ENOBUFS;
10899 }
10900 
10901 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
10902 {
10903 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
10904 	int i, j;
10905 	struct cfg80211_coalesce_rules *rule;
10906 
10907 	if (!coalesce)
10908 		return;
10909 
10910 	for (i = 0; i < coalesce->n_rules; i++) {
10911 		rule = &coalesce->rules[i];
10912 		for (j = 0; j < rule->n_patterns; j++)
10913 			kfree(rule->patterns[j].mask);
10914 		kfree(rule->patterns);
10915 	}
10916 	kfree(coalesce->rules);
10917 	kfree(coalesce);
10918 	rdev->coalesce = NULL;
10919 }
10920 
10921 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
10922 				       struct nlattr *rule,
10923 				       struct cfg80211_coalesce_rules *new_rule)
10924 {
10925 	int err, i;
10926 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10927 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
10928 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
10929 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10930 
10931 	err = nla_parse_nested(tb, NL80211_ATTR_COALESCE_RULE_MAX, rule,
10932 			       nl80211_coalesce_policy, NULL);
10933 	if (err)
10934 		return err;
10935 
10936 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
10937 		new_rule->delay =
10938 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
10939 	if (new_rule->delay > coalesce->max_delay)
10940 		return -EINVAL;
10941 
10942 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
10943 		new_rule->condition =
10944 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
10945 	if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
10946 	    new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
10947 		return -EINVAL;
10948 
10949 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
10950 		return -EINVAL;
10951 
10952 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10953 			    rem)
10954 		n_patterns++;
10955 	if (n_patterns > coalesce->n_patterns)
10956 		return -EINVAL;
10957 
10958 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
10959 				     GFP_KERNEL);
10960 	if (!new_rule->patterns)
10961 		return -ENOMEM;
10962 
10963 	new_rule->n_patterns = n_patterns;
10964 	i = 0;
10965 
10966 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10967 			    rem) {
10968 		u8 *mask_pat;
10969 
10970 		nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10971 				 nl80211_packet_pattern_policy, NULL);
10972 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
10973 		    !pat_tb[NL80211_PKTPAT_PATTERN])
10974 			return -EINVAL;
10975 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10976 		mask_len = DIV_ROUND_UP(pat_len, 8);
10977 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10978 			return -EINVAL;
10979 		if (pat_len > coalesce->pattern_max_len ||
10980 		    pat_len < coalesce->pattern_min_len)
10981 			return -EINVAL;
10982 
10983 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
10984 			pkt_offset = 0;
10985 		else
10986 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
10987 		if (pkt_offset > coalesce->max_pkt_offset)
10988 			return -EINVAL;
10989 		new_rule->patterns[i].pkt_offset = pkt_offset;
10990 
10991 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10992 		if (!mask_pat)
10993 			return -ENOMEM;
10994 
10995 		new_rule->patterns[i].mask = mask_pat;
10996 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10997 		       mask_len);
10998 
10999 		mask_pat += mask_len;
11000 		new_rule->patterns[i].pattern = mask_pat;
11001 		new_rule->patterns[i].pattern_len = pat_len;
11002 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11003 		       pat_len);
11004 		i++;
11005 	}
11006 
11007 	return 0;
11008 }
11009 
11010 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11011 {
11012 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11013 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11014 	struct cfg80211_coalesce new_coalesce = {};
11015 	struct cfg80211_coalesce *n_coalesce;
11016 	int err, rem_rule, n_rules = 0, i, j;
11017 	struct nlattr *rule;
11018 	struct cfg80211_coalesce_rules *tmp_rule;
11019 
11020 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11021 		return -EOPNOTSUPP;
11022 
11023 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11024 		cfg80211_rdev_free_coalesce(rdev);
11025 		rdev_set_coalesce(rdev, NULL);
11026 		return 0;
11027 	}
11028 
11029 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11030 			    rem_rule)
11031 		n_rules++;
11032 	if (n_rules > coalesce->n_rules)
11033 		return -EINVAL;
11034 
11035 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11036 				     GFP_KERNEL);
11037 	if (!new_coalesce.rules)
11038 		return -ENOMEM;
11039 
11040 	new_coalesce.n_rules = n_rules;
11041 	i = 0;
11042 
11043 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11044 			    rem_rule) {
11045 		err = nl80211_parse_coalesce_rule(rdev, rule,
11046 						  &new_coalesce.rules[i]);
11047 		if (err)
11048 			goto error;
11049 
11050 		i++;
11051 	}
11052 
11053 	err = rdev_set_coalesce(rdev, &new_coalesce);
11054 	if (err)
11055 		goto error;
11056 
11057 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
11058 	if (!n_coalesce) {
11059 		err = -ENOMEM;
11060 		goto error;
11061 	}
11062 	cfg80211_rdev_free_coalesce(rdev);
11063 	rdev->coalesce = n_coalesce;
11064 
11065 	return 0;
11066 error:
11067 	for (i = 0; i < new_coalesce.n_rules; i++) {
11068 		tmp_rule = &new_coalesce.rules[i];
11069 		for (j = 0; j < tmp_rule->n_patterns; j++)
11070 			kfree(tmp_rule->patterns[j].mask);
11071 		kfree(tmp_rule->patterns);
11072 	}
11073 	kfree(new_coalesce.rules);
11074 
11075 	return err;
11076 }
11077 
11078 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
11079 {
11080 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11081 	struct net_device *dev = info->user_ptr[1];
11082 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11083 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
11084 	struct cfg80211_gtk_rekey_data rekey_data;
11085 	int err;
11086 
11087 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
11088 		return -EINVAL;
11089 
11090 	err = nla_parse_nested(tb, MAX_NL80211_REKEY_DATA,
11091 			       info->attrs[NL80211_ATTR_REKEY_DATA],
11092 			       nl80211_rekey_policy, info->extack);
11093 	if (err)
11094 		return err;
11095 
11096 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
11097 	    !tb[NL80211_REKEY_DATA_KCK])
11098 		return -EINVAL;
11099 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
11100 		return -ERANGE;
11101 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
11102 		return -ERANGE;
11103 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
11104 		return -ERANGE;
11105 
11106 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
11107 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
11108 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
11109 
11110 	wdev_lock(wdev);
11111 	if (!wdev->current_bss) {
11112 		err = -ENOTCONN;
11113 		goto out;
11114 	}
11115 
11116 	if (!rdev->ops->set_rekey_data) {
11117 		err = -EOPNOTSUPP;
11118 		goto out;
11119 	}
11120 
11121 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
11122  out:
11123 	wdev_unlock(wdev);
11124 	return err;
11125 }
11126 
11127 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
11128 					     struct genl_info *info)
11129 {
11130 	struct net_device *dev = info->user_ptr[1];
11131 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11132 
11133 	if (wdev->iftype != NL80211_IFTYPE_AP &&
11134 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
11135 		return -EINVAL;
11136 
11137 	if (wdev->ap_unexpected_nlportid)
11138 		return -EBUSY;
11139 
11140 	wdev->ap_unexpected_nlportid = info->snd_portid;
11141 	return 0;
11142 }
11143 
11144 static int nl80211_probe_client(struct sk_buff *skb,
11145 				struct genl_info *info)
11146 {
11147 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11148 	struct net_device *dev = info->user_ptr[1];
11149 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11150 	struct sk_buff *msg;
11151 	void *hdr;
11152 	const u8 *addr;
11153 	u64 cookie;
11154 	int err;
11155 
11156 	if (wdev->iftype != NL80211_IFTYPE_AP &&
11157 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
11158 		return -EOPNOTSUPP;
11159 
11160 	if (!info->attrs[NL80211_ATTR_MAC])
11161 		return -EINVAL;
11162 
11163 	if (!rdev->ops->probe_client)
11164 		return -EOPNOTSUPP;
11165 
11166 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11167 	if (!msg)
11168 		return -ENOMEM;
11169 
11170 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11171 			     NL80211_CMD_PROBE_CLIENT);
11172 	if (!hdr) {
11173 		err = -ENOBUFS;
11174 		goto free_msg;
11175 	}
11176 
11177 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
11178 
11179 	err = rdev_probe_client(rdev, dev, addr, &cookie);
11180 	if (err)
11181 		goto free_msg;
11182 
11183 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11184 			      NL80211_ATTR_PAD))
11185 		goto nla_put_failure;
11186 
11187 	genlmsg_end(msg, hdr);
11188 
11189 	return genlmsg_reply(msg, info);
11190 
11191  nla_put_failure:
11192 	err = -ENOBUFS;
11193  free_msg:
11194 	nlmsg_free(msg);
11195 	return err;
11196 }
11197 
11198 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
11199 {
11200 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11201 	struct cfg80211_beacon_registration *reg, *nreg;
11202 	int rv;
11203 
11204 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
11205 		return -EOPNOTSUPP;
11206 
11207 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
11208 	if (!nreg)
11209 		return -ENOMEM;
11210 
11211 	/* First, check if already registered. */
11212 	spin_lock_bh(&rdev->beacon_registrations_lock);
11213 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11214 		if (reg->nlportid == info->snd_portid) {
11215 			rv = -EALREADY;
11216 			goto out_err;
11217 		}
11218 	}
11219 	/* Add it to the list */
11220 	nreg->nlportid = info->snd_portid;
11221 	list_add(&nreg->list, &rdev->beacon_registrations);
11222 
11223 	spin_unlock_bh(&rdev->beacon_registrations_lock);
11224 
11225 	return 0;
11226 out_err:
11227 	spin_unlock_bh(&rdev->beacon_registrations_lock);
11228 	kfree(nreg);
11229 	return rv;
11230 }
11231 
11232 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
11233 {
11234 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11235 	struct wireless_dev *wdev = info->user_ptr[1];
11236 	int err;
11237 
11238 	if (!rdev->ops->start_p2p_device)
11239 		return -EOPNOTSUPP;
11240 
11241 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11242 		return -EOPNOTSUPP;
11243 
11244 	if (wdev_running(wdev))
11245 		return 0;
11246 
11247 	if (rfkill_blocked(rdev->rfkill))
11248 		return -ERFKILL;
11249 
11250 	err = rdev_start_p2p_device(rdev, wdev);
11251 	if (err)
11252 		return err;
11253 
11254 	wdev->is_running = true;
11255 	rdev->opencount++;
11256 
11257 	return 0;
11258 }
11259 
11260 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
11261 {
11262 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11263 	struct wireless_dev *wdev = info->user_ptr[1];
11264 
11265 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11266 		return -EOPNOTSUPP;
11267 
11268 	if (!rdev->ops->stop_p2p_device)
11269 		return -EOPNOTSUPP;
11270 
11271 	cfg80211_stop_p2p_device(rdev, wdev);
11272 
11273 	return 0;
11274 }
11275 
11276 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
11277 {
11278 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11279 	struct wireless_dev *wdev = info->user_ptr[1];
11280 	struct cfg80211_nan_conf conf = {};
11281 	int err;
11282 
11283 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11284 		return -EOPNOTSUPP;
11285 
11286 	if (wdev_running(wdev))
11287 		return -EEXIST;
11288 
11289 	if (rfkill_blocked(rdev->rfkill))
11290 		return -ERFKILL;
11291 
11292 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
11293 		return -EINVAL;
11294 
11295 	conf.master_pref =
11296 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11297 	if (!conf.master_pref)
11298 		return -EINVAL;
11299 
11300 	if (info->attrs[NL80211_ATTR_BANDS]) {
11301 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11302 
11303 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11304 			return -EOPNOTSUPP;
11305 
11306 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11307 			return -EINVAL;
11308 
11309 		conf.bands = bands;
11310 	}
11311 
11312 	err = rdev_start_nan(rdev, wdev, &conf);
11313 	if (err)
11314 		return err;
11315 
11316 	wdev->is_running = true;
11317 	rdev->opencount++;
11318 
11319 	return 0;
11320 }
11321 
11322 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
11323 {
11324 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11325 	struct wireless_dev *wdev = info->user_ptr[1];
11326 
11327 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11328 		return -EOPNOTSUPP;
11329 
11330 	cfg80211_stop_nan(rdev, wdev);
11331 
11332 	return 0;
11333 }
11334 
11335 static int validate_nan_filter(struct nlattr *filter_attr)
11336 {
11337 	struct nlattr *attr;
11338 	int len = 0, n_entries = 0, rem;
11339 
11340 	nla_for_each_nested(attr, filter_attr, rem) {
11341 		len += nla_len(attr);
11342 		n_entries++;
11343 	}
11344 
11345 	if (len >= U8_MAX)
11346 		return -EINVAL;
11347 
11348 	return n_entries;
11349 }
11350 
11351 static int handle_nan_filter(struct nlattr *attr_filter,
11352 			     struct cfg80211_nan_func *func,
11353 			     bool tx)
11354 {
11355 	struct nlattr *attr;
11356 	int n_entries, rem, i;
11357 	struct cfg80211_nan_func_filter *filter;
11358 
11359 	n_entries = validate_nan_filter(attr_filter);
11360 	if (n_entries < 0)
11361 		return n_entries;
11362 
11363 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
11364 
11365 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
11366 	if (!filter)
11367 		return -ENOMEM;
11368 
11369 	i = 0;
11370 	nla_for_each_nested(attr, attr_filter, rem) {
11371 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
11372 		filter[i].len = nla_len(attr);
11373 		i++;
11374 	}
11375 	if (tx) {
11376 		func->num_tx_filters = n_entries;
11377 		func->tx_filters = filter;
11378 	} else {
11379 		func->num_rx_filters = n_entries;
11380 		func->rx_filters = filter;
11381 	}
11382 
11383 	return 0;
11384 }
11385 
11386 static int nl80211_nan_add_func(struct sk_buff *skb,
11387 				struct genl_info *info)
11388 {
11389 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11390 	struct wireless_dev *wdev = info->user_ptr[1];
11391 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
11392 	struct cfg80211_nan_func *func;
11393 	struct sk_buff *msg = NULL;
11394 	void *hdr = NULL;
11395 	int err = 0;
11396 
11397 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11398 		return -EOPNOTSUPP;
11399 
11400 	if (!wdev_running(wdev))
11401 		return -ENOTCONN;
11402 
11403 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
11404 		return -EINVAL;
11405 
11406 	err = nla_parse_nested(tb, NL80211_NAN_FUNC_ATTR_MAX,
11407 			       info->attrs[NL80211_ATTR_NAN_FUNC],
11408 			       nl80211_nan_func_policy, info->extack);
11409 	if (err)
11410 		return err;
11411 
11412 	func = kzalloc(sizeof(*func), GFP_KERNEL);
11413 	if (!func)
11414 		return -ENOMEM;
11415 
11416 	func->cookie = wdev->wiphy->cookie_counter++;
11417 
11418 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
11419 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
11420 		err = -EINVAL;
11421 		goto out;
11422 	}
11423 
11424 
11425 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
11426 
11427 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
11428 		err = -EINVAL;
11429 		goto out;
11430 	}
11431 
11432 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
11433 	       sizeof(func->service_id));
11434 
11435 	func->close_range =
11436 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
11437 
11438 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
11439 		func->serv_spec_info_len =
11440 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
11441 		func->serv_spec_info =
11442 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
11443 				func->serv_spec_info_len,
11444 				GFP_KERNEL);
11445 		if (!func->serv_spec_info) {
11446 			err = -ENOMEM;
11447 			goto out;
11448 		}
11449 	}
11450 
11451 	if (tb[NL80211_NAN_FUNC_TTL])
11452 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
11453 
11454 	switch (func->type) {
11455 	case NL80211_NAN_FUNC_PUBLISH:
11456 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
11457 			err = -EINVAL;
11458 			goto out;
11459 		}
11460 
11461 		func->publish_type =
11462 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
11463 		func->publish_bcast =
11464 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
11465 
11466 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
11467 			func->publish_bcast) {
11468 			err = -EINVAL;
11469 			goto out;
11470 		}
11471 		break;
11472 	case NL80211_NAN_FUNC_SUBSCRIBE:
11473 		func->subscribe_active =
11474 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
11475 		break;
11476 	case NL80211_NAN_FUNC_FOLLOW_UP:
11477 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
11478 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
11479 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
11480 			err = -EINVAL;
11481 			goto out;
11482 		}
11483 
11484 		func->followup_id =
11485 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
11486 		func->followup_reqid =
11487 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
11488 		memcpy(func->followup_dest.addr,
11489 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
11490 		       sizeof(func->followup_dest.addr));
11491 		if (func->ttl) {
11492 			err = -EINVAL;
11493 			goto out;
11494 		}
11495 		break;
11496 	default:
11497 		err = -EINVAL;
11498 		goto out;
11499 	}
11500 
11501 	if (tb[NL80211_NAN_FUNC_SRF]) {
11502 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
11503 
11504 		err = nla_parse_nested(srf_tb, NL80211_NAN_SRF_ATTR_MAX,
11505 				       tb[NL80211_NAN_FUNC_SRF],
11506 				       nl80211_nan_srf_policy, info->extack);
11507 		if (err)
11508 			goto out;
11509 
11510 		func->srf_include =
11511 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
11512 
11513 		if (srf_tb[NL80211_NAN_SRF_BF]) {
11514 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
11515 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
11516 				err = -EINVAL;
11517 				goto out;
11518 			}
11519 
11520 			func->srf_bf_len =
11521 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
11522 			func->srf_bf =
11523 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
11524 					func->srf_bf_len, GFP_KERNEL);
11525 			if (!func->srf_bf) {
11526 				err = -ENOMEM;
11527 				goto out;
11528 			}
11529 
11530 			func->srf_bf_idx =
11531 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
11532 		} else {
11533 			struct nlattr *attr, *mac_attr =
11534 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
11535 			int n_entries, rem, i = 0;
11536 
11537 			if (!mac_attr) {
11538 				err = -EINVAL;
11539 				goto out;
11540 			}
11541 
11542 			n_entries = validate_acl_mac_addrs(mac_attr);
11543 			if (n_entries <= 0) {
11544 				err = -EINVAL;
11545 				goto out;
11546 			}
11547 
11548 			func->srf_num_macs = n_entries;
11549 			func->srf_macs =
11550 				kzalloc(sizeof(*func->srf_macs) * n_entries,
11551 					GFP_KERNEL);
11552 			if (!func->srf_macs) {
11553 				err = -ENOMEM;
11554 				goto out;
11555 			}
11556 
11557 			nla_for_each_nested(attr, mac_attr, rem)
11558 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
11559 				       sizeof(*func->srf_macs));
11560 		}
11561 	}
11562 
11563 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
11564 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
11565 					func, true);
11566 		if (err)
11567 			goto out;
11568 	}
11569 
11570 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
11571 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
11572 					func, false);
11573 		if (err)
11574 			goto out;
11575 	}
11576 
11577 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11578 	if (!msg) {
11579 		err = -ENOMEM;
11580 		goto out;
11581 	}
11582 
11583 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11584 			     NL80211_CMD_ADD_NAN_FUNCTION);
11585 	/* This can't really happen - we just allocated 4KB */
11586 	if (WARN_ON(!hdr)) {
11587 		err = -ENOMEM;
11588 		goto out;
11589 	}
11590 
11591 	err = rdev_add_nan_func(rdev, wdev, func);
11592 out:
11593 	if (err < 0) {
11594 		cfg80211_free_nan_func(func);
11595 		nlmsg_free(msg);
11596 		return err;
11597 	}
11598 
11599 	/* propagate the instance id and cookie to userspace  */
11600 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
11601 			      NL80211_ATTR_PAD))
11602 		goto nla_put_failure;
11603 
11604 	func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11605 	if (!func_attr)
11606 		goto nla_put_failure;
11607 
11608 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
11609 		       func->instance_id))
11610 		goto nla_put_failure;
11611 
11612 	nla_nest_end(msg, func_attr);
11613 
11614 	genlmsg_end(msg, hdr);
11615 	return genlmsg_reply(msg, info);
11616 
11617 nla_put_failure:
11618 	nlmsg_free(msg);
11619 	return -ENOBUFS;
11620 }
11621 
11622 static int nl80211_nan_del_func(struct sk_buff *skb,
11623 			       struct genl_info *info)
11624 {
11625 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11626 	struct wireless_dev *wdev = info->user_ptr[1];
11627 	u64 cookie;
11628 
11629 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11630 		return -EOPNOTSUPP;
11631 
11632 	if (!wdev_running(wdev))
11633 		return -ENOTCONN;
11634 
11635 	if (!info->attrs[NL80211_ATTR_COOKIE])
11636 		return -EINVAL;
11637 
11638 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11639 
11640 	rdev_del_nan_func(rdev, wdev, cookie);
11641 
11642 	return 0;
11643 }
11644 
11645 static int nl80211_nan_change_config(struct sk_buff *skb,
11646 				     struct genl_info *info)
11647 {
11648 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11649 	struct wireless_dev *wdev = info->user_ptr[1];
11650 	struct cfg80211_nan_conf conf = {};
11651 	u32 changed = 0;
11652 
11653 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11654 		return -EOPNOTSUPP;
11655 
11656 	if (!wdev_running(wdev))
11657 		return -ENOTCONN;
11658 
11659 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
11660 		conf.master_pref =
11661 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11662 		if (conf.master_pref <= 1 || conf.master_pref == 255)
11663 			return -EINVAL;
11664 
11665 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
11666 	}
11667 
11668 	if (info->attrs[NL80211_ATTR_BANDS]) {
11669 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11670 
11671 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11672 			return -EOPNOTSUPP;
11673 
11674 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11675 			return -EINVAL;
11676 
11677 		conf.bands = bands;
11678 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
11679 	}
11680 
11681 	if (!changed)
11682 		return -EINVAL;
11683 
11684 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
11685 }
11686 
11687 void cfg80211_nan_match(struct wireless_dev *wdev,
11688 			struct cfg80211_nan_match_params *match, gfp_t gfp)
11689 {
11690 	struct wiphy *wiphy = wdev->wiphy;
11691 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11692 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
11693 	struct sk_buff *msg;
11694 	void *hdr;
11695 
11696 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
11697 		return;
11698 
11699 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11700 	if (!msg)
11701 		return;
11702 
11703 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
11704 	if (!hdr) {
11705 		nlmsg_free(msg);
11706 		return;
11707 	}
11708 
11709 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11710 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11711 					 wdev->netdev->ifindex)) ||
11712 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11713 			      NL80211_ATTR_PAD))
11714 		goto nla_put_failure;
11715 
11716 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
11717 			      NL80211_ATTR_PAD) ||
11718 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
11719 		goto nla_put_failure;
11720 
11721 	match_attr = nla_nest_start(msg, NL80211_ATTR_NAN_MATCH);
11722 	if (!match_attr)
11723 		goto nla_put_failure;
11724 
11725 	local_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_LOCAL);
11726 	if (!local_func_attr)
11727 		goto nla_put_failure;
11728 
11729 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
11730 		goto nla_put_failure;
11731 
11732 	nla_nest_end(msg, local_func_attr);
11733 
11734 	peer_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_PEER);
11735 	if (!peer_func_attr)
11736 		goto nla_put_failure;
11737 
11738 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
11739 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
11740 		goto nla_put_failure;
11741 
11742 	if (match->info && match->info_len &&
11743 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
11744 		    match->info))
11745 		goto nla_put_failure;
11746 
11747 	nla_nest_end(msg, peer_func_attr);
11748 	nla_nest_end(msg, match_attr);
11749 	genlmsg_end(msg, hdr);
11750 
11751 	if (!wdev->owner_nlportid)
11752 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11753 					msg, 0, NL80211_MCGRP_NAN, gfp);
11754 	else
11755 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11756 				wdev->owner_nlportid);
11757 
11758 	return;
11759 
11760 nla_put_failure:
11761 	nlmsg_free(msg);
11762 }
11763 EXPORT_SYMBOL(cfg80211_nan_match);
11764 
11765 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
11766 				  u8 inst_id,
11767 				  enum nl80211_nan_func_term_reason reason,
11768 				  u64 cookie, gfp_t gfp)
11769 {
11770 	struct wiphy *wiphy = wdev->wiphy;
11771 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11772 	struct sk_buff *msg;
11773 	struct nlattr *func_attr;
11774 	void *hdr;
11775 
11776 	if (WARN_ON(!inst_id))
11777 		return;
11778 
11779 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11780 	if (!msg)
11781 		return;
11782 
11783 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
11784 	if (!hdr) {
11785 		nlmsg_free(msg);
11786 		return;
11787 	}
11788 
11789 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11790 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11791 					 wdev->netdev->ifindex)) ||
11792 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11793 			      NL80211_ATTR_PAD))
11794 		goto nla_put_failure;
11795 
11796 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11797 			      NL80211_ATTR_PAD))
11798 		goto nla_put_failure;
11799 
11800 	func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11801 	if (!func_attr)
11802 		goto nla_put_failure;
11803 
11804 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
11805 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
11806 		goto nla_put_failure;
11807 
11808 	nla_nest_end(msg, func_attr);
11809 	genlmsg_end(msg, hdr);
11810 
11811 	if (!wdev->owner_nlportid)
11812 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11813 					msg, 0, NL80211_MCGRP_NAN, gfp);
11814 	else
11815 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11816 				wdev->owner_nlportid);
11817 
11818 	return;
11819 
11820 nla_put_failure:
11821 	nlmsg_free(msg);
11822 }
11823 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
11824 
11825 static int nl80211_get_protocol_features(struct sk_buff *skb,
11826 					 struct genl_info *info)
11827 {
11828 	void *hdr;
11829 	struct sk_buff *msg;
11830 
11831 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11832 	if (!msg)
11833 		return -ENOMEM;
11834 
11835 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11836 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
11837 	if (!hdr)
11838 		goto nla_put_failure;
11839 
11840 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
11841 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
11842 		goto nla_put_failure;
11843 
11844 	genlmsg_end(msg, hdr);
11845 	return genlmsg_reply(msg, info);
11846 
11847  nla_put_failure:
11848 	kfree_skb(msg);
11849 	return -ENOBUFS;
11850 }
11851 
11852 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
11853 {
11854 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11855 	struct cfg80211_update_ft_ies_params ft_params;
11856 	struct net_device *dev = info->user_ptr[1];
11857 
11858 	if (!rdev->ops->update_ft_ies)
11859 		return -EOPNOTSUPP;
11860 
11861 	if (!info->attrs[NL80211_ATTR_MDID] ||
11862 	    !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
11863 		return -EINVAL;
11864 
11865 	memset(&ft_params, 0, sizeof(ft_params));
11866 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
11867 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11868 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11869 
11870 	return rdev_update_ft_ies(rdev, dev, &ft_params);
11871 }
11872 
11873 static int nl80211_crit_protocol_start(struct sk_buff *skb,
11874 				       struct genl_info *info)
11875 {
11876 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11877 	struct wireless_dev *wdev = info->user_ptr[1];
11878 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
11879 	u16 duration;
11880 	int ret;
11881 
11882 	if (!rdev->ops->crit_proto_start)
11883 		return -EOPNOTSUPP;
11884 
11885 	if (WARN_ON(!rdev->ops->crit_proto_stop))
11886 		return -EINVAL;
11887 
11888 	if (rdev->crit_proto_nlportid)
11889 		return -EBUSY;
11890 
11891 	/* determine protocol if provided */
11892 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
11893 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
11894 
11895 	if (proto >= NUM_NL80211_CRIT_PROTO)
11896 		return -EINVAL;
11897 
11898 	/* timeout must be provided */
11899 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
11900 		return -EINVAL;
11901 
11902 	duration =
11903 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
11904 
11905 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
11906 		return -ERANGE;
11907 
11908 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
11909 	if (!ret)
11910 		rdev->crit_proto_nlportid = info->snd_portid;
11911 
11912 	return ret;
11913 }
11914 
11915 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
11916 				      struct genl_info *info)
11917 {
11918 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11919 	struct wireless_dev *wdev = info->user_ptr[1];
11920 
11921 	if (!rdev->ops->crit_proto_stop)
11922 		return -EOPNOTSUPP;
11923 
11924 	if (rdev->crit_proto_nlportid) {
11925 		rdev->crit_proto_nlportid = 0;
11926 		rdev_crit_proto_stop(rdev, wdev);
11927 	}
11928 	return 0;
11929 }
11930 
11931 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
11932 {
11933 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11934 	struct wireless_dev *wdev =
11935 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
11936 	int i, err;
11937 	u32 vid, subcmd;
11938 
11939 	if (!rdev->wiphy.vendor_commands)
11940 		return -EOPNOTSUPP;
11941 
11942 	if (IS_ERR(wdev)) {
11943 		err = PTR_ERR(wdev);
11944 		if (err != -EINVAL)
11945 			return err;
11946 		wdev = NULL;
11947 	} else if (wdev->wiphy != &rdev->wiphy) {
11948 		return -EINVAL;
11949 	}
11950 
11951 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
11952 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
11953 		return -EINVAL;
11954 
11955 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
11956 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
11957 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
11958 		const struct wiphy_vendor_command *vcmd;
11959 		void *data = NULL;
11960 		int len = 0;
11961 
11962 		vcmd = &rdev->wiphy.vendor_commands[i];
11963 
11964 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11965 			continue;
11966 
11967 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
11968 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
11969 			if (!wdev)
11970 				return -EINVAL;
11971 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
11972 			    !wdev->netdev)
11973 				return -EINVAL;
11974 
11975 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
11976 				if (!wdev_running(wdev))
11977 					return -ENETDOWN;
11978 			}
11979 
11980 			if (!vcmd->doit)
11981 				return -EOPNOTSUPP;
11982 		} else {
11983 			wdev = NULL;
11984 		}
11985 
11986 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
11987 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11988 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11989 		}
11990 
11991 		rdev->cur_cmd_info = info;
11992 		err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
11993 							  data, len);
11994 		rdev->cur_cmd_info = NULL;
11995 		return err;
11996 	}
11997 
11998 	return -EOPNOTSUPP;
11999 }
12000 
12001 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12002 				       struct netlink_callback *cb,
12003 				       struct cfg80211_registered_device **rdev,
12004 				       struct wireless_dev **wdev)
12005 {
12006 	struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
12007 	u32 vid, subcmd;
12008 	unsigned int i;
12009 	int vcmd_idx = -1;
12010 	int err;
12011 	void *data = NULL;
12012 	unsigned int data_len = 0;
12013 
12014 	if (cb->args[0]) {
12015 		/* subtract the 1 again here */
12016 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12017 		struct wireless_dev *tmp;
12018 
12019 		if (!wiphy)
12020 			return -ENODEV;
12021 		*rdev = wiphy_to_rdev(wiphy);
12022 		*wdev = NULL;
12023 
12024 		if (cb->args[1]) {
12025 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
12026 				if (tmp->identifier == cb->args[1] - 1) {
12027 					*wdev = tmp;
12028 					break;
12029 				}
12030 			}
12031 		}
12032 
12033 		/* keep rtnl locked in successful case */
12034 		return 0;
12035 	}
12036 
12037 	err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, attrbuf,
12038 			  nl80211_fam.maxattr, nl80211_policy, NULL);
12039 	if (err)
12040 		return err;
12041 
12042 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
12043 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
12044 		return -EINVAL;
12045 
12046 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
12047 	if (IS_ERR(*wdev))
12048 		*wdev = NULL;
12049 
12050 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
12051 	if (IS_ERR(*rdev))
12052 		return PTR_ERR(*rdev);
12053 
12054 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
12055 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
12056 
12057 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
12058 		const struct wiphy_vendor_command *vcmd;
12059 
12060 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
12061 
12062 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12063 			continue;
12064 
12065 		if (!vcmd->dumpit)
12066 			return -EOPNOTSUPP;
12067 
12068 		vcmd_idx = i;
12069 		break;
12070 	}
12071 
12072 	if (vcmd_idx < 0)
12073 		return -EOPNOTSUPP;
12074 
12075 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
12076 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12077 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12078 	}
12079 
12080 	/* 0 is the first index - add 1 to parse only once */
12081 	cb->args[0] = (*rdev)->wiphy_idx + 1;
12082 	/* add 1 to know if it was NULL */
12083 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
12084 	cb->args[2] = vcmd_idx;
12085 	cb->args[3] = (unsigned long)data;
12086 	cb->args[4] = data_len;
12087 
12088 	/* keep rtnl locked in successful case */
12089 	return 0;
12090 }
12091 
12092 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
12093 				   struct netlink_callback *cb)
12094 {
12095 	struct cfg80211_registered_device *rdev;
12096 	struct wireless_dev *wdev;
12097 	unsigned int vcmd_idx;
12098 	const struct wiphy_vendor_command *vcmd;
12099 	void *data;
12100 	int data_len;
12101 	int err;
12102 	struct nlattr *vendor_data;
12103 
12104 	rtnl_lock();
12105 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
12106 	if (err)
12107 		goto out;
12108 
12109 	vcmd_idx = cb->args[2];
12110 	data = (void *)cb->args[3];
12111 	data_len = cb->args[4];
12112 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
12113 
12114 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12115 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12116 		if (!wdev) {
12117 			err = -EINVAL;
12118 			goto out;
12119 		}
12120 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12121 		    !wdev->netdev) {
12122 			err = -EINVAL;
12123 			goto out;
12124 		}
12125 
12126 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12127 			if (!wdev_running(wdev)) {
12128 				err = -ENETDOWN;
12129 				goto out;
12130 			}
12131 		}
12132 	}
12133 
12134 	while (1) {
12135 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
12136 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
12137 					   NL80211_CMD_VENDOR);
12138 		if (!hdr)
12139 			break;
12140 
12141 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12142 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
12143 					       wdev_id(wdev),
12144 					       NL80211_ATTR_PAD))) {
12145 			genlmsg_cancel(skb, hdr);
12146 			break;
12147 		}
12148 
12149 		vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
12150 		if (!vendor_data) {
12151 			genlmsg_cancel(skb, hdr);
12152 			break;
12153 		}
12154 
12155 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
12156 				   (unsigned long *)&cb->args[5]);
12157 		nla_nest_end(skb, vendor_data);
12158 
12159 		if (err == -ENOBUFS || err == -ENOENT) {
12160 			genlmsg_cancel(skb, hdr);
12161 			break;
12162 		} else if (err) {
12163 			genlmsg_cancel(skb, hdr);
12164 			goto out;
12165 		}
12166 
12167 		genlmsg_end(skb, hdr);
12168 	}
12169 
12170 	err = skb->len;
12171  out:
12172 	rtnl_unlock();
12173 	return err;
12174 }
12175 
12176 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
12177 					   enum nl80211_commands cmd,
12178 					   enum nl80211_attrs attr,
12179 					   int approxlen)
12180 {
12181 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12182 
12183 	if (WARN_ON(!rdev->cur_cmd_info))
12184 		return NULL;
12185 
12186 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
12187 					   rdev->cur_cmd_info->snd_portid,
12188 					   rdev->cur_cmd_info->snd_seq,
12189 					   cmd, attr, NULL, GFP_KERNEL);
12190 }
12191 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
12192 
12193 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
12194 {
12195 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
12196 	void *hdr = ((void **)skb->cb)[1];
12197 	struct nlattr *data = ((void **)skb->cb)[2];
12198 
12199 	/* clear CB data for netlink core to own from now on */
12200 	memset(skb->cb, 0, sizeof(skb->cb));
12201 
12202 	if (WARN_ON(!rdev->cur_cmd_info)) {
12203 		kfree_skb(skb);
12204 		return -EINVAL;
12205 	}
12206 
12207 	nla_nest_end(skb, data);
12208 	genlmsg_end(skb, hdr);
12209 	return genlmsg_reply(skb, rdev->cur_cmd_info);
12210 }
12211 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
12212 
12213 static int nl80211_set_qos_map(struct sk_buff *skb,
12214 			       struct genl_info *info)
12215 {
12216 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12217 	struct cfg80211_qos_map *qos_map = NULL;
12218 	struct net_device *dev = info->user_ptr[1];
12219 	u8 *pos, len, num_des, des_len, des;
12220 	int ret;
12221 
12222 	if (!rdev->ops->set_qos_map)
12223 		return -EOPNOTSUPP;
12224 
12225 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
12226 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
12227 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
12228 
12229 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
12230 		    len > IEEE80211_QOS_MAP_LEN_MAX)
12231 			return -EINVAL;
12232 
12233 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
12234 		if (!qos_map)
12235 			return -ENOMEM;
12236 
12237 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
12238 		if (num_des) {
12239 			des_len = num_des *
12240 				sizeof(struct cfg80211_dscp_exception);
12241 			memcpy(qos_map->dscp_exception, pos, des_len);
12242 			qos_map->num_des = num_des;
12243 			for (des = 0; des < num_des; des++) {
12244 				if (qos_map->dscp_exception[des].up > 7) {
12245 					kfree(qos_map);
12246 					return -EINVAL;
12247 				}
12248 			}
12249 			pos += des_len;
12250 		}
12251 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
12252 	}
12253 
12254 	wdev_lock(dev->ieee80211_ptr);
12255 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
12256 	if (!ret)
12257 		ret = rdev_set_qos_map(rdev, dev, qos_map);
12258 	wdev_unlock(dev->ieee80211_ptr);
12259 
12260 	kfree(qos_map);
12261 	return ret;
12262 }
12263 
12264 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
12265 {
12266 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12267 	struct net_device *dev = info->user_ptr[1];
12268 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12269 	const u8 *peer;
12270 	u8 tsid, up;
12271 	u16 admitted_time = 0;
12272 	int err;
12273 
12274 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
12275 		return -EOPNOTSUPP;
12276 
12277 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
12278 	    !info->attrs[NL80211_ATTR_USER_PRIO])
12279 		return -EINVAL;
12280 
12281 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12282 	if (tsid >= IEEE80211_NUM_TIDS)
12283 		return -EINVAL;
12284 
12285 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
12286 	if (up >= IEEE80211_NUM_UPS)
12287 		return -EINVAL;
12288 
12289 	/* WMM uses TIDs 0-7 even for TSPEC */
12290 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
12291 		/* TODO: handle 802.11 TSPEC/admission control
12292 		 * need more attributes for that (e.g. BA session requirement);
12293 		 * change the WMM adminssion test above to allow both then
12294 		 */
12295 		return -EINVAL;
12296 	}
12297 
12298 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12299 
12300 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
12301 		admitted_time =
12302 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
12303 		if (!admitted_time)
12304 			return -EINVAL;
12305 	}
12306 
12307 	wdev_lock(wdev);
12308 	switch (wdev->iftype) {
12309 	case NL80211_IFTYPE_STATION:
12310 	case NL80211_IFTYPE_P2P_CLIENT:
12311 		if (wdev->current_bss)
12312 			break;
12313 		err = -ENOTCONN;
12314 		goto out;
12315 	default:
12316 		err = -EOPNOTSUPP;
12317 		goto out;
12318 	}
12319 
12320 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
12321 
12322  out:
12323 	wdev_unlock(wdev);
12324 	return err;
12325 }
12326 
12327 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
12328 {
12329 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12330 	struct net_device *dev = info->user_ptr[1];
12331 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12332 	const u8 *peer;
12333 	u8 tsid;
12334 	int err;
12335 
12336 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
12337 		return -EINVAL;
12338 
12339 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12340 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12341 
12342 	wdev_lock(wdev);
12343 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
12344 	wdev_unlock(wdev);
12345 
12346 	return err;
12347 }
12348 
12349 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
12350 				       struct genl_info *info)
12351 {
12352 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12353 	struct net_device *dev = info->user_ptr[1];
12354 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12355 	struct cfg80211_chan_def chandef = {};
12356 	const u8 *addr;
12357 	u8 oper_class;
12358 	int err;
12359 
12360 	if (!rdev->ops->tdls_channel_switch ||
12361 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12362 		return -EOPNOTSUPP;
12363 
12364 	switch (dev->ieee80211_ptr->iftype) {
12365 	case NL80211_IFTYPE_STATION:
12366 	case NL80211_IFTYPE_P2P_CLIENT:
12367 		break;
12368 	default:
12369 		return -EOPNOTSUPP;
12370 	}
12371 
12372 	if (!info->attrs[NL80211_ATTR_MAC] ||
12373 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
12374 		return -EINVAL;
12375 
12376 	err = nl80211_parse_chandef(rdev, info, &chandef);
12377 	if (err)
12378 		return err;
12379 
12380 	/*
12381 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
12382 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
12383 	 * specification is not defined for them.
12384 	 */
12385 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
12386 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
12387 	    chandef.width != NL80211_CHAN_WIDTH_20)
12388 		return -EINVAL;
12389 
12390 	/* we will be active on the TDLS link */
12391 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
12392 					   wdev->iftype))
12393 		return -EINVAL;
12394 
12395 	/* don't allow switching to DFS channels */
12396 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
12397 		return -EINVAL;
12398 
12399 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12400 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
12401 
12402 	wdev_lock(wdev);
12403 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
12404 	wdev_unlock(wdev);
12405 
12406 	return err;
12407 }
12408 
12409 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
12410 					      struct genl_info *info)
12411 {
12412 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12413 	struct net_device *dev = info->user_ptr[1];
12414 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12415 	const u8 *addr;
12416 
12417 	if (!rdev->ops->tdls_channel_switch ||
12418 	    !rdev->ops->tdls_cancel_channel_switch ||
12419 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12420 		return -EOPNOTSUPP;
12421 
12422 	switch (dev->ieee80211_ptr->iftype) {
12423 	case NL80211_IFTYPE_STATION:
12424 	case NL80211_IFTYPE_P2P_CLIENT:
12425 		break;
12426 	default:
12427 		return -EOPNOTSUPP;
12428 	}
12429 
12430 	if (!info->attrs[NL80211_ATTR_MAC])
12431 		return -EINVAL;
12432 
12433 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12434 
12435 	wdev_lock(wdev);
12436 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
12437 	wdev_unlock(wdev);
12438 
12439 	return 0;
12440 }
12441 
12442 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
12443 					    struct genl_info *info)
12444 {
12445 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12446 	struct net_device *dev = info->user_ptr[1];
12447 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12448 	const struct nlattr *nla;
12449 	bool enabled;
12450 
12451 	if (!rdev->ops->set_multicast_to_unicast)
12452 		return -EOPNOTSUPP;
12453 
12454 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12455 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12456 		return -EOPNOTSUPP;
12457 
12458 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
12459 	enabled = nla_get_flag(nla);
12460 
12461 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
12462 }
12463 
12464 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
12465 {
12466 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12467 	struct net_device *dev = info->user_ptr[1];
12468 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12469 	struct cfg80211_pmk_conf pmk_conf = {};
12470 	int ret;
12471 
12472 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12473 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12474 		return -EOPNOTSUPP;
12475 
12476 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12477 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12478 		return -EOPNOTSUPP;
12479 
12480 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
12481 		return -EINVAL;
12482 
12483 	wdev_lock(wdev);
12484 	if (!wdev->current_bss) {
12485 		ret = -ENOTCONN;
12486 		goto out;
12487 	}
12488 
12489 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12490 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
12491 		ret = -EINVAL;
12492 		goto out;
12493 	}
12494 
12495 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12496 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12497 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
12498 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
12499 		ret = -EINVAL;
12500 		goto out;
12501 	}
12502 
12503 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
12504 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12505 
12506 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
12507 			ret = -EINVAL;
12508 			goto out;
12509 		}
12510 
12511 		pmk_conf.pmk_r0_name =
12512 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12513 	}
12514 
12515 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
12516 out:
12517 	wdev_unlock(wdev);
12518 	return ret;
12519 }
12520 
12521 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
12522 {
12523 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12524 	struct net_device *dev = info->user_ptr[1];
12525 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12526 	const u8 *aa;
12527 	int ret;
12528 
12529 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12530 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12531 		return -EOPNOTSUPP;
12532 
12533 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12534 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12535 		return -EOPNOTSUPP;
12536 
12537 	if (!info->attrs[NL80211_ATTR_MAC])
12538 		return -EINVAL;
12539 
12540 	wdev_lock(wdev);
12541 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12542 	ret = rdev_del_pmk(rdev, dev, aa);
12543 	wdev_unlock(wdev);
12544 
12545 	return ret;
12546 }
12547 
12548 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
12549 {
12550 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12551 	struct net_device *dev = info->user_ptr[1];
12552 	struct cfg80211_external_auth_params params;
12553 
12554 	if (!rdev->ops->external_auth)
12555 		return -EOPNOTSUPP;
12556 
12557 	if (!info->attrs[NL80211_ATTR_SSID])
12558 		return -EINVAL;
12559 
12560 	if (!info->attrs[NL80211_ATTR_BSSID])
12561 		return -EINVAL;
12562 
12563 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
12564 		return -EINVAL;
12565 
12566 	memset(&params, 0, sizeof(params));
12567 
12568 	params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12569 	if (params.ssid.ssid_len == 0 ||
12570 	    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
12571 		return -EINVAL;
12572 	memcpy(params.ssid.ssid, nla_data(info->attrs[NL80211_ATTR_SSID]),
12573 	       params.ssid.ssid_len);
12574 
12575 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
12576 	       ETH_ALEN);
12577 
12578 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12579 
12580 	return rdev_external_auth(rdev, dev, &params);
12581 }
12582 
12583 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
12584 {
12585 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12586 	struct net_device *dev = info->user_ptr[1];
12587 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12588 	const u8 *buf;
12589 	size_t len;
12590 	u8 *dest;
12591 	u16 proto;
12592 	bool noencrypt;
12593 	int err;
12594 
12595 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12596 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
12597 		return -EOPNOTSUPP;
12598 
12599 	if (!rdev->ops->tx_control_port)
12600 		return -EOPNOTSUPP;
12601 
12602 	if (!info->attrs[NL80211_ATTR_FRAME] ||
12603 	    !info->attrs[NL80211_ATTR_MAC] ||
12604 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
12605 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
12606 		return -EINVAL;
12607 	}
12608 
12609 	wdev_lock(wdev);
12610 
12611 	switch (wdev->iftype) {
12612 	case NL80211_IFTYPE_AP:
12613 	case NL80211_IFTYPE_P2P_GO:
12614 	case NL80211_IFTYPE_MESH_POINT:
12615 		break;
12616 	case NL80211_IFTYPE_ADHOC:
12617 	case NL80211_IFTYPE_STATION:
12618 	case NL80211_IFTYPE_P2P_CLIENT:
12619 		if (wdev->current_bss)
12620 			break;
12621 		err = -ENOTCONN;
12622 		goto out;
12623 	default:
12624 		err = -EOPNOTSUPP;
12625 		goto out;
12626 	}
12627 
12628 	wdev_unlock(wdev);
12629 
12630 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12631 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12632 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
12633 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
12634 	noencrypt =
12635 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
12636 
12637 	return rdev_tx_control_port(rdev, dev, buf, len,
12638 				    dest, cpu_to_be16(proto), noencrypt);
12639 
12640  out:
12641 	wdev_unlock(wdev);
12642 	return err;
12643 }
12644 
12645 #define NL80211_FLAG_NEED_WIPHY		0x01
12646 #define NL80211_FLAG_NEED_NETDEV	0x02
12647 #define NL80211_FLAG_NEED_RTNL		0x04
12648 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
12649 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
12650 					 NL80211_FLAG_CHECK_NETDEV_UP)
12651 #define NL80211_FLAG_NEED_WDEV		0x10
12652 /* If a netdev is associated, it must be UP, P2P must be started */
12653 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
12654 					 NL80211_FLAG_CHECK_NETDEV_UP)
12655 #define NL80211_FLAG_CLEAR_SKB		0x20
12656 
12657 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
12658 			    struct genl_info *info)
12659 {
12660 	struct cfg80211_registered_device *rdev;
12661 	struct wireless_dev *wdev;
12662 	struct net_device *dev;
12663 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
12664 
12665 	if (rtnl)
12666 		rtnl_lock();
12667 
12668 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
12669 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
12670 		if (IS_ERR(rdev)) {
12671 			if (rtnl)
12672 				rtnl_unlock();
12673 			return PTR_ERR(rdev);
12674 		}
12675 		info->user_ptr[0] = rdev;
12676 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
12677 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12678 		ASSERT_RTNL();
12679 
12680 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
12681 						  info->attrs);
12682 		if (IS_ERR(wdev)) {
12683 			if (rtnl)
12684 				rtnl_unlock();
12685 			return PTR_ERR(wdev);
12686 		}
12687 
12688 		dev = wdev->netdev;
12689 		rdev = wiphy_to_rdev(wdev->wiphy);
12690 
12691 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
12692 			if (!dev) {
12693 				if (rtnl)
12694 					rtnl_unlock();
12695 				return -EINVAL;
12696 			}
12697 
12698 			info->user_ptr[1] = dev;
12699 		} else {
12700 			info->user_ptr[1] = wdev;
12701 		}
12702 
12703 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
12704 		    !wdev_running(wdev)) {
12705 			if (rtnl)
12706 				rtnl_unlock();
12707 			return -ENETDOWN;
12708 		}
12709 
12710 		if (dev)
12711 			dev_hold(dev);
12712 
12713 		info->user_ptr[0] = rdev;
12714 	}
12715 
12716 	return 0;
12717 }
12718 
12719 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
12720 			      struct genl_info *info)
12721 {
12722 	if (info->user_ptr[1]) {
12723 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12724 			struct wireless_dev *wdev = info->user_ptr[1];
12725 
12726 			if (wdev->netdev)
12727 				dev_put(wdev->netdev);
12728 		} else {
12729 			dev_put(info->user_ptr[1]);
12730 		}
12731 	}
12732 
12733 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
12734 		rtnl_unlock();
12735 
12736 	/* If needed, clear the netlink message payload from the SKB
12737 	 * as it might contain key data that shouldn't stick around on
12738 	 * the heap after the SKB is freed. The netlink message header
12739 	 * is still needed for further processing, so leave it intact.
12740 	 */
12741 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
12742 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
12743 
12744 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
12745 	}
12746 }
12747 
12748 static const struct genl_ops nl80211_ops[] = {
12749 	{
12750 		.cmd = NL80211_CMD_GET_WIPHY,
12751 		.doit = nl80211_get_wiphy,
12752 		.dumpit = nl80211_dump_wiphy,
12753 		.done = nl80211_dump_wiphy_done,
12754 		.policy = nl80211_policy,
12755 		/* can be retrieved by unprivileged users */
12756 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12757 				  NL80211_FLAG_NEED_RTNL,
12758 	},
12759 	{
12760 		.cmd = NL80211_CMD_SET_WIPHY,
12761 		.doit = nl80211_set_wiphy,
12762 		.policy = nl80211_policy,
12763 		.flags = GENL_UNS_ADMIN_PERM,
12764 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12765 	},
12766 	{
12767 		.cmd = NL80211_CMD_GET_INTERFACE,
12768 		.doit = nl80211_get_interface,
12769 		.dumpit = nl80211_dump_interface,
12770 		.policy = nl80211_policy,
12771 		/* can be retrieved by unprivileged users */
12772 		.internal_flags = NL80211_FLAG_NEED_WDEV |
12773 				  NL80211_FLAG_NEED_RTNL,
12774 	},
12775 	{
12776 		.cmd = NL80211_CMD_SET_INTERFACE,
12777 		.doit = nl80211_set_interface,
12778 		.policy = nl80211_policy,
12779 		.flags = GENL_UNS_ADMIN_PERM,
12780 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
12781 				  NL80211_FLAG_NEED_RTNL,
12782 	},
12783 	{
12784 		.cmd = NL80211_CMD_NEW_INTERFACE,
12785 		.doit = nl80211_new_interface,
12786 		.policy = nl80211_policy,
12787 		.flags = GENL_UNS_ADMIN_PERM,
12788 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12789 				  NL80211_FLAG_NEED_RTNL,
12790 	},
12791 	{
12792 		.cmd = NL80211_CMD_DEL_INTERFACE,
12793 		.doit = nl80211_del_interface,
12794 		.policy = nl80211_policy,
12795 		.flags = GENL_UNS_ADMIN_PERM,
12796 		.internal_flags = NL80211_FLAG_NEED_WDEV |
12797 				  NL80211_FLAG_NEED_RTNL,
12798 	},
12799 	{
12800 		.cmd = NL80211_CMD_GET_KEY,
12801 		.doit = nl80211_get_key,
12802 		.policy = nl80211_policy,
12803 		.flags = GENL_UNS_ADMIN_PERM,
12804 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12805 				  NL80211_FLAG_NEED_RTNL,
12806 	},
12807 	{
12808 		.cmd = NL80211_CMD_SET_KEY,
12809 		.doit = nl80211_set_key,
12810 		.policy = nl80211_policy,
12811 		.flags = GENL_UNS_ADMIN_PERM,
12812 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12813 				  NL80211_FLAG_NEED_RTNL |
12814 				  NL80211_FLAG_CLEAR_SKB,
12815 	},
12816 	{
12817 		.cmd = NL80211_CMD_NEW_KEY,
12818 		.doit = nl80211_new_key,
12819 		.policy = nl80211_policy,
12820 		.flags = GENL_UNS_ADMIN_PERM,
12821 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12822 				  NL80211_FLAG_NEED_RTNL |
12823 				  NL80211_FLAG_CLEAR_SKB,
12824 	},
12825 	{
12826 		.cmd = NL80211_CMD_DEL_KEY,
12827 		.doit = nl80211_del_key,
12828 		.policy = nl80211_policy,
12829 		.flags = GENL_UNS_ADMIN_PERM,
12830 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12831 				  NL80211_FLAG_NEED_RTNL,
12832 	},
12833 	{
12834 		.cmd = NL80211_CMD_SET_BEACON,
12835 		.policy = nl80211_policy,
12836 		.flags = GENL_UNS_ADMIN_PERM,
12837 		.doit = nl80211_set_beacon,
12838 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12839 				  NL80211_FLAG_NEED_RTNL,
12840 	},
12841 	{
12842 		.cmd = NL80211_CMD_START_AP,
12843 		.policy = nl80211_policy,
12844 		.flags = GENL_UNS_ADMIN_PERM,
12845 		.doit = nl80211_start_ap,
12846 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12847 				  NL80211_FLAG_NEED_RTNL,
12848 	},
12849 	{
12850 		.cmd = NL80211_CMD_STOP_AP,
12851 		.policy = nl80211_policy,
12852 		.flags = GENL_UNS_ADMIN_PERM,
12853 		.doit = nl80211_stop_ap,
12854 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12855 				  NL80211_FLAG_NEED_RTNL,
12856 	},
12857 	{
12858 		.cmd = NL80211_CMD_GET_STATION,
12859 		.doit = nl80211_get_station,
12860 		.dumpit = nl80211_dump_station,
12861 		.policy = nl80211_policy,
12862 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
12863 				  NL80211_FLAG_NEED_RTNL,
12864 	},
12865 	{
12866 		.cmd = NL80211_CMD_SET_STATION,
12867 		.doit = nl80211_set_station,
12868 		.policy = nl80211_policy,
12869 		.flags = GENL_UNS_ADMIN_PERM,
12870 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12871 				  NL80211_FLAG_NEED_RTNL,
12872 	},
12873 	{
12874 		.cmd = NL80211_CMD_NEW_STATION,
12875 		.doit = nl80211_new_station,
12876 		.policy = nl80211_policy,
12877 		.flags = GENL_UNS_ADMIN_PERM,
12878 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12879 				  NL80211_FLAG_NEED_RTNL,
12880 	},
12881 	{
12882 		.cmd = NL80211_CMD_DEL_STATION,
12883 		.doit = nl80211_del_station,
12884 		.policy = nl80211_policy,
12885 		.flags = GENL_UNS_ADMIN_PERM,
12886 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12887 				  NL80211_FLAG_NEED_RTNL,
12888 	},
12889 	{
12890 		.cmd = NL80211_CMD_GET_MPATH,
12891 		.doit = nl80211_get_mpath,
12892 		.dumpit = nl80211_dump_mpath,
12893 		.policy = nl80211_policy,
12894 		.flags = GENL_UNS_ADMIN_PERM,
12895 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12896 				  NL80211_FLAG_NEED_RTNL,
12897 	},
12898 	{
12899 		.cmd = NL80211_CMD_GET_MPP,
12900 		.doit = nl80211_get_mpp,
12901 		.dumpit = nl80211_dump_mpp,
12902 		.policy = nl80211_policy,
12903 		.flags = GENL_UNS_ADMIN_PERM,
12904 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12905 				  NL80211_FLAG_NEED_RTNL,
12906 	},
12907 	{
12908 		.cmd = NL80211_CMD_SET_MPATH,
12909 		.doit = nl80211_set_mpath,
12910 		.policy = nl80211_policy,
12911 		.flags = GENL_UNS_ADMIN_PERM,
12912 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12913 				  NL80211_FLAG_NEED_RTNL,
12914 	},
12915 	{
12916 		.cmd = NL80211_CMD_NEW_MPATH,
12917 		.doit = nl80211_new_mpath,
12918 		.policy = nl80211_policy,
12919 		.flags = GENL_UNS_ADMIN_PERM,
12920 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12921 				  NL80211_FLAG_NEED_RTNL,
12922 	},
12923 	{
12924 		.cmd = NL80211_CMD_DEL_MPATH,
12925 		.doit = nl80211_del_mpath,
12926 		.policy = nl80211_policy,
12927 		.flags = GENL_UNS_ADMIN_PERM,
12928 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12929 				  NL80211_FLAG_NEED_RTNL,
12930 	},
12931 	{
12932 		.cmd = NL80211_CMD_SET_BSS,
12933 		.doit = nl80211_set_bss,
12934 		.policy = nl80211_policy,
12935 		.flags = GENL_UNS_ADMIN_PERM,
12936 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12937 				  NL80211_FLAG_NEED_RTNL,
12938 	},
12939 	{
12940 		.cmd = NL80211_CMD_GET_REG,
12941 		.doit = nl80211_get_reg_do,
12942 		.dumpit = nl80211_get_reg_dump,
12943 		.policy = nl80211_policy,
12944 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12945 		/* can be retrieved by unprivileged users */
12946 	},
12947 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
12948 	{
12949 		.cmd = NL80211_CMD_SET_REG,
12950 		.doit = nl80211_set_reg,
12951 		.policy = nl80211_policy,
12952 		.flags = GENL_ADMIN_PERM,
12953 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12954 	},
12955 #endif
12956 	{
12957 		.cmd = NL80211_CMD_REQ_SET_REG,
12958 		.doit = nl80211_req_set_reg,
12959 		.policy = nl80211_policy,
12960 		.flags = GENL_ADMIN_PERM,
12961 	},
12962 	{
12963 		.cmd = NL80211_CMD_RELOAD_REGDB,
12964 		.doit = nl80211_reload_regdb,
12965 		.policy = nl80211_policy,
12966 		.flags = GENL_ADMIN_PERM,
12967 	},
12968 	{
12969 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
12970 		.doit = nl80211_get_mesh_config,
12971 		.policy = nl80211_policy,
12972 		/* can be retrieved by unprivileged users */
12973 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12974 				  NL80211_FLAG_NEED_RTNL,
12975 	},
12976 	{
12977 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
12978 		.doit = nl80211_update_mesh_config,
12979 		.policy = nl80211_policy,
12980 		.flags = GENL_UNS_ADMIN_PERM,
12981 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12982 				  NL80211_FLAG_NEED_RTNL,
12983 	},
12984 	{
12985 		.cmd = NL80211_CMD_TRIGGER_SCAN,
12986 		.doit = nl80211_trigger_scan,
12987 		.policy = nl80211_policy,
12988 		.flags = GENL_UNS_ADMIN_PERM,
12989 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12990 				  NL80211_FLAG_NEED_RTNL,
12991 	},
12992 	{
12993 		.cmd = NL80211_CMD_ABORT_SCAN,
12994 		.doit = nl80211_abort_scan,
12995 		.policy = nl80211_policy,
12996 		.flags = GENL_UNS_ADMIN_PERM,
12997 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12998 				  NL80211_FLAG_NEED_RTNL,
12999 	},
13000 	{
13001 		.cmd = NL80211_CMD_GET_SCAN,
13002 		.policy = nl80211_policy,
13003 		.dumpit = nl80211_dump_scan,
13004 	},
13005 	{
13006 		.cmd = NL80211_CMD_START_SCHED_SCAN,
13007 		.doit = nl80211_start_sched_scan,
13008 		.policy = nl80211_policy,
13009 		.flags = GENL_UNS_ADMIN_PERM,
13010 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13011 				  NL80211_FLAG_NEED_RTNL,
13012 	},
13013 	{
13014 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
13015 		.doit = nl80211_stop_sched_scan,
13016 		.policy = nl80211_policy,
13017 		.flags = GENL_UNS_ADMIN_PERM,
13018 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13019 				  NL80211_FLAG_NEED_RTNL,
13020 	},
13021 	{
13022 		.cmd = NL80211_CMD_AUTHENTICATE,
13023 		.doit = nl80211_authenticate,
13024 		.policy = nl80211_policy,
13025 		.flags = GENL_UNS_ADMIN_PERM,
13026 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13027 				  NL80211_FLAG_NEED_RTNL |
13028 				  NL80211_FLAG_CLEAR_SKB,
13029 	},
13030 	{
13031 		.cmd = NL80211_CMD_ASSOCIATE,
13032 		.doit = nl80211_associate,
13033 		.policy = nl80211_policy,
13034 		.flags = GENL_UNS_ADMIN_PERM,
13035 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13036 				  NL80211_FLAG_NEED_RTNL,
13037 	},
13038 	{
13039 		.cmd = NL80211_CMD_DEAUTHENTICATE,
13040 		.doit = nl80211_deauthenticate,
13041 		.policy = nl80211_policy,
13042 		.flags = GENL_UNS_ADMIN_PERM,
13043 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13044 				  NL80211_FLAG_NEED_RTNL,
13045 	},
13046 	{
13047 		.cmd = NL80211_CMD_DISASSOCIATE,
13048 		.doit = nl80211_disassociate,
13049 		.policy = nl80211_policy,
13050 		.flags = GENL_UNS_ADMIN_PERM,
13051 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13052 				  NL80211_FLAG_NEED_RTNL,
13053 	},
13054 	{
13055 		.cmd = NL80211_CMD_JOIN_IBSS,
13056 		.doit = nl80211_join_ibss,
13057 		.policy = nl80211_policy,
13058 		.flags = GENL_UNS_ADMIN_PERM,
13059 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13060 				  NL80211_FLAG_NEED_RTNL,
13061 	},
13062 	{
13063 		.cmd = NL80211_CMD_LEAVE_IBSS,
13064 		.doit = nl80211_leave_ibss,
13065 		.policy = nl80211_policy,
13066 		.flags = GENL_UNS_ADMIN_PERM,
13067 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13068 				  NL80211_FLAG_NEED_RTNL,
13069 	},
13070 #ifdef CONFIG_NL80211_TESTMODE
13071 	{
13072 		.cmd = NL80211_CMD_TESTMODE,
13073 		.doit = nl80211_testmode_do,
13074 		.dumpit = nl80211_testmode_dump,
13075 		.policy = nl80211_policy,
13076 		.flags = GENL_UNS_ADMIN_PERM,
13077 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13078 				  NL80211_FLAG_NEED_RTNL,
13079 	},
13080 #endif
13081 	{
13082 		.cmd = NL80211_CMD_CONNECT,
13083 		.doit = nl80211_connect,
13084 		.policy = nl80211_policy,
13085 		.flags = GENL_UNS_ADMIN_PERM,
13086 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13087 				  NL80211_FLAG_NEED_RTNL,
13088 	},
13089 	{
13090 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
13091 		.doit = nl80211_update_connect_params,
13092 		.policy = nl80211_policy,
13093 		.flags = GENL_ADMIN_PERM,
13094 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13095 				  NL80211_FLAG_NEED_RTNL,
13096 	},
13097 	{
13098 		.cmd = NL80211_CMD_DISCONNECT,
13099 		.doit = nl80211_disconnect,
13100 		.policy = nl80211_policy,
13101 		.flags = GENL_UNS_ADMIN_PERM,
13102 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13103 				  NL80211_FLAG_NEED_RTNL,
13104 	},
13105 	{
13106 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
13107 		.doit = nl80211_wiphy_netns,
13108 		.policy = nl80211_policy,
13109 		.flags = GENL_UNS_ADMIN_PERM,
13110 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13111 				  NL80211_FLAG_NEED_RTNL,
13112 	},
13113 	{
13114 		.cmd = NL80211_CMD_GET_SURVEY,
13115 		.policy = nl80211_policy,
13116 		.dumpit = nl80211_dump_survey,
13117 	},
13118 	{
13119 		.cmd = NL80211_CMD_SET_PMKSA,
13120 		.doit = nl80211_setdel_pmksa,
13121 		.policy = nl80211_policy,
13122 		.flags = GENL_UNS_ADMIN_PERM,
13123 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13124 				  NL80211_FLAG_NEED_RTNL,
13125 	},
13126 	{
13127 		.cmd = NL80211_CMD_DEL_PMKSA,
13128 		.doit = nl80211_setdel_pmksa,
13129 		.policy = nl80211_policy,
13130 		.flags = GENL_UNS_ADMIN_PERM,
13131 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13132 				  NL80211_FLAG_NEED_RTNL,
13133 	},
13134 	{
13135 		.cmd = NL80211_CMD_FLUSH_PMKSA,
13136 		.doit = nl80211_flush_pmksa,
13137 		.policy = nl80211_policy,
13138 		.flags = GENL_UNS_ADMIN_PERM,
13139 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13140 				  NL80211_FLAG_NEED_RTNL,
13141 	},
13142 	{
13143 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
13144 		.doit = nl80211_remain_on_channel,
13145 		.policy = nl80211_policy,
13146 		.flags = GENL_UNS_ADMIN_PERM,
13147 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13148 				  NL80211_FLAG_NEED_RTNL,
13149 	},
13150 	{
13151 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
13152 		.doit = nl80211_cancel_remain_on_channel,
13153 		.policy = nl80211_policy,
13154 		.flags = GENL_UNS_ADMIN_PERM,
13155 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13156 				  NL80211_FLAG_NEED_RTNL,
13157 	},
13158 	{
13159 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
13160 		.doit = nl80211_set_tx_bitrate_mask,
13161 		.policy = nl80211_policy,
13162 		.flags = GENL_UNS_ADMIN_PERM,
13163 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13164 				  NL80211_FLAG_NEED_RTNL,
13165 	},
13166 	{
13167 		.cmd = NL80211_CMD_REGISTER_FRAME,
13168 		.doit = nl80211_register_mgmt,
13169 		.policy = nl80211_policy,
13170 		.flags = GENL_UNS_ADMIN_PERM,
13171 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13172 				  NL80211_FLAG_NEED_RTNL,
13173 	},
13174 	{
13175 		.cmd = NL80211_CMD_FRAME,
13176 		.doit = nl80211_tx_mgmt,
13177 		.policy = nl80211_policy,
13178 		.flags = GENL_UNS_ADMIN_PERM,
13179 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13180 				  NL80211_FLAG_NEED_RTNL,
13181 	},
13182 	{
13183 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
13184 		.doit = nl80211_tx_mgmt_cancel_wait,
13185 		.policy = nl80211_policy,
13186 		.flags = GENL_UNS_ADMIN_PERM,
13187 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13188 				  NL80211_FLAG_NEED_RTNL,
13189 	},
13190 	{
13191 		.cmd = NL80211_CMD_SET_POWER_SAVE,
13192 		.doit = nl80211_set_power_save,
13193 		.policy = nl80211_policy,
13194 		.flags = GENL_UNS_ADMIN_PERM,
13195 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13196 				  NL80211_FLAG_NEED_RTNL,
13197 	},
13198 	{
13199 		.cmd = NL80211_CMD_GET_POWER_SAVE,
13200 		.doit = nl80211_get_power_save,
13201 		.policy = nl80211_policy,
13202 		/* can be retrieved by unprivileged users */
13203 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13204 				  NL80211_FLAG_NEED_RTNL,
13205 	},
13206 	{
13207 		.cmd = NL80211_CMD_SET_CQM,
13208 		.doit = nl80211_set_cqm,
13209 		.policy = nl80211_policy,
13210 		.flags = GENL_UNS_ADMIN_PERM,
13211 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13212 				  NL80211_FLAG_NEED_RTNL,
13213 	},
13214 	{
13215 		.cmd = NL80211_CMD_SET_CHANNEL,
13216 		.doit = nl80211_set_channel,
13217 		.policy = nl80211_policy,
13218 		.flags = GENL_UNS_ADMIN_PERM,
13219 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13220 				  NL80211_FLAG_NEED_RTNL,
13221 	},
13222 	{
13223 		.cmd = NL80211_CMD_SET_WDS_PEER,
13224 		.doit = nl80211_set_wds_peer,
13225 		.policy = nl80211_policy,
13226 		.flags = GENL_UNS_ADMIN_PERM,
13227 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13228 				  NL80211_FLAG_NEED_RTNL,
13229 	},
13230 	{
13231 		.cmd = NL80211_CMD_JOIN_MESH,
13232 		.doit = nl80211_join_mesh,
13233 		.policy = nl80211_policy,
13234 		.flags = GENL_UNS_ADMIN_PERM,
13235 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13236 				  NL80211_FLAG_NEED_RTNL,
13237 	},
13238 	{
13239 		.cmd = NL80211_CMD_LEAVE_MESH,
13240 		.doit = nl80211_leave_mesh,
13241 		.policy = nl80211_policy,
13242 		.flags = GENL_UNS_ADMIN_PERM,
13243 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13244 				  NL80211_FLAG_NEED_RTNL,
13245 	},
13246 	{
13247 		.cmd = NL80211_CMD_JOIN_OCB,
13248 		.doit = nl80211_join_ocb,
13249 		.policy = nl80211_policy,
13250 		.flags = GENL_UNS_ADMIN_PERM,
13251 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13252 				  NL80211_FLAG_NEED_RTNL,
13253 	},
13254 	{
13255 		.cmd = NL80211_CMD_LEAVE_OCB,
13256 		.doit = nl80211_leave_ocb,
13257 		.policy = nl80211_policy,
13258 		.flags = GENL_UNS_ADMIN_PERM,
13259 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13260 				  NL80211_FLAG_NEED_RTNL,
13261 	},
13262 #ifdef CONFIG_PM
13263 	{
13264 		.cmd = NL80211_CMD_GET_WOWLAN,
13265 		.doit = nl80211_get_wowlan,
13266 		.policy = nl80211_policy,
13267 		/* can be retrieved by unprivileged users */
13268 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13269 				  NL80211_FLAG_NEED_RTNL,
13270 	},
13271 	{
13272 		.cmd = NL80211_CMD_SET_WOWLAN,
13273 		.doit = nl80211_set_wowlan,
13274 		.policy = nl80211_policy,
13275 		.flags = GENL_UNS_ADMIN_PERM,
13276 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13277 				  NL80211_FLAG_NEED_RTNL,
13278 	},
13279 #endif
13280 	{
13281 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
13282 		.doit = nl80211_set_rekey_data,
13283 		.policy = nl80211_policy,
13284 		.flags = GENL_UNS_ADMIN_PERM,
13285 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13286 				  NL80211_FLAG_NEED_RTNL |
13287 				  NL80211_FLAG_CLEAR_SKB,
13288 	},
13289 	{
13290 		.cmd = NL80211_CMD_TDLS_MGMT,
13291 		.doit = nl80211_tdls_mgmt,
13292 		.policy = nl80211_policy,
13293 		.flags = GENL_UNS_ADMIN_PERM,
13294 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13295 				  NL80211_FLAG_NEED_RTNL,
13296 	},
13297 	{
13298 		.cmd = NL80211_CMD_TDLS_OPER,
13299 		.doit = nl80211_tdls_oper,
13300 		.policy = nl80211_policy,
13301 		.flags = GENL_UNS_ADMIN_PERM,
13302 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13303 				  NL80211_FLAG_NEED_RTNL,
13304 	},
13305 	{
13306 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
13307 		.doit = nl80211_register_unexpected_frame,
13308 		.policy = nl80211_policy,
13309 		.flags = GENL_UNS_ADMIN_PERM,
13310 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13311 				  NL80211_FLAG_NEED_RTNL,
13312 	},
13313 	{
13314 		.cmd = NL80211_CMD_PROBE_CLIENT,
13315 		.doit = nl80211_probe_client,
13316 		.policy = nl80211_policy,
13317 		.flags = GENL_UNS_ADMIN_PERM,
13318 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13319 				  NL80211_FLAG_NEED_RTNL,
13320 	},
13321 	{
13322 		.cmd = NL80211_CMD_REGISTER_BEACONS,
13323 		.doit = nl80211_register_beacons,
13324 		.policy = nl80211_policy,
13325 		.flags = GENL_UNS_ADMIN_PERM,
13326 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13327 				  NL80211_FLAG_NEED_RTNL,
13328 	},
13329 	{
13330 		.cmd = NL80211_CMD_SET_NOACK_MAP,
13331 		.doit = nl80211_set_noack_map,
13332 		.policy = nl80211_policy,
13333 		.flags = GENL_UNS_ADMIN_PERM,
13334 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13335 				  NL80211_FLAG_NEED_RTNL,
13336 	},
13337 	{
13338 		.cmd = NL80211_CMD_START_P2P_DEVICE,
13339 		.doit = nl80211_start_p2p_device,
13340 		.policy = nl80211_policy,
13341 		.flags = GENL_UNS_ADMIN_PERM,
13342 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13343 				  NL80211_FLAG_NEED_RTNL,
13344 	},
13345 	{
13346 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
13347 		.doit = nl80211_stop_p2p_device,
13348 		.policy = nl80211_policy,
13349 		.flags = GENL_UNS_ADMIN_PERM,
13350 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13351 				  NL80211_FLAG_NEED_RTNL,
13352 	},
13353 	{
13354 		.cmd = NL80211_CMD_START_NAN,
13355 		.doit = nl80211_start_nan,
13356 		.policy = nl80211_policy,
13357 		.flags = GENL_ADMIN_PERM,
13358 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13359 				  NL80211_FLAG_NEED_RTNL,
13360 	},
13361 	{
13362 		.cmd = NL80211_CMD_STOP_NAN,
13363 		.doit = nl80211_stop_nan,
13364 		.policy = nl80211_policy,
13365 		.flags = GENL_ADMIN_PERM,
13366 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13367 				  NL80211_FLAG_NEED_RTNL,
13368 	},
13369 	{
13370 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
13371 		.doit = nl80211_nan_add_func,
13372 		.policy = nl80211_policy,
13373 		.flags = GENL_ADMIN_PERM,
13374 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13375 				  NL80211_FLAG_NEED_RTNL,
13376 	},
13377 	{
13378 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
13379 		.doit = nl80211_nan_del_func,
13380 		.policy = nl80211_policy,
13381 		.flags = GENL_ADMIN_PERM,
13382 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13383 				  NL80211_FLAG_NEED_RTNL,
13384 	},
13385 	{
13386 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
13387 		.doit = nl80211_nan_change_config,
13388 		.policy = nl80211_policy,
13389 		.flags = GENL_ADMIN_PERM,
13390 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13391 				  NL80211_FLAG_NEED_RTNL,
13392 	},
13393 	{
13394 		.cmd = NL80211_CMD_SET_MCAST_RATE,
13395 		.doit = nl80211_set_mcast_rate,
13396 		.policy = nl80211_policy,
13397 		.flags = GENL_UNS_ADMIN_PERM,
13398 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13399 				  NL80211_FLAG_NEED_RTNL,
13400 	},
13401 	{
13402 		.cmd = NL80211_CMD_SET_MAC_ACL,
13403 		.doit = nl80211_set_mac_acl,
13404 		.policy = nl80211_policy,
13405 		.flags = GENL_UNS_ADMIN_PERM,
13406 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13407 				  NL80211_FLAG_NEED_RTNL,
13408 	},
13409 	{
13410 		.cmd = NL80211_CMD_RADAR_DETECT,
13411 		.doit = nl80211_start_radar_detection,
13412 		.policy = nl80211_policy,
13413 		.flags = GENL_UNS_ADMIN_PERM,
13414 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13415 				  NL80211_FLAG_NEED_RTNL,
13416 	},
13417 	{
13418 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
13419 		.doit = nl80211_get_protocol_features,
13420 		.policy = nl80211_policy,
13421 	},
13422 	{
13423 		.cmd = NL80211_CMD_UPDATE_FT_IES,
13424 		.doit = nl80211_update_ft_ies,
13425 		.policy = nl80211_policy,
13426 		.flags = GENL_UNS_ADMIN_PERM,
13427 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13428 				  NL80211_FLAG_NEED_RTNL,
13429 	},
13430 	{
13431 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
13432 		.doit = nl80211_crit_protocol_start,
13433 		.policy = nl80211_policy,
13434 		.flags = GENL_UNS_ADMIN_PERM,
13435 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13436 				  NL80211_FLAG_NEED_RTNL,
13437 	},
13438 	{
13439 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
13440 		.doit = nl80211_crit_protocol_stop,
13441 		.policy = nl80211_policy,
13442 		.flags = GENL_UNS_ADMIN_PERM,
13443 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13444 				  NL80211_FLAG_NEED_RTNL,
13445 	},
13446 	{
13447 		.cmd = NL80211_CMD_GET_COALESCE,
13448 		.doit = nl80211_get_coalesce,
13449 		.policy = nl80211_policy,
13450 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13451 				  NL80211_FLAG_NEED_RTNL,
13452 	},
13453 	{
13454 		.cmd = NL80211_CMD_SET_COALESCE,
13455 		.doit = nl80211_set_coalesce,
13456 		.policy = nl80211_policy,
13457 		.flags = GENL_UNS_ADMIN_PERM,
13458 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13459 				  NL80211_FLAG_NEED_RTNL,
13460 	},
13461 	{
13462 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
13463 		.doit = nl80211_channel_switch,
13464 		.policy = nl80211_policy,
13465 		.flags = GENL_UNS_ADMIN_PERM,
13466 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13467 				  NL80211_FLAG_NEED_RTNL,
13468 	},
13469 	{
13470 		.cmd = NL80211_CMD_VENDOR,
13471 		.doit = nl80211_vendor_cmd,
13472 		.dumpit = nl80211_vendor_cmd_dump,
13473 		.policy = nl80211_policy,
13474 		.flags = GENL_UNS_ADMIN_PERM,
13475 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13476 				  NL80211_FLAG_NEED_RTNL,
13477 	},
13478 	{
13479 		.cmd = NL80211_CMD_SET_QOS_MAP,
13480 		.doit = nl80211_set_qos_map,
13481 		.policy = nl80211_policy,
13482 		.flags = GENL_UNS_ADMIN_PERM,
13483 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13484 				  NL80211_FLAG_NEED_RTNL,
13485 	},
13486 	{
13487 		.cmd = NL80211_CMD_ADD_TX_TS,
13488 		.doit = nl80211_add_tx_ts,
13489 		.policy = nl80211_policy,
13490 		.flags = GENL_UNS_ADMIN_PERM,
13491 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13492 				  NL80211_FLAG_NEED_RTNL,
13493 	},
13494 	{
13495 		.cmd = NL80211_CMD_DEL_TX_TS,
13496 		.doit = nl80211_del_tx_ts,
13497 		.policy = nl80211_policy,
13498 		.flags = GENL_UNS_ADMIN_PERM,
13499 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13500 				  NL80211_FLAG_NEED_RTNL,
13501 	},
13502 	{
13503 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
13504 		.doit = nl80211_tdls_channel_switch,
13505 		.policy = nl80211_policy,
13506 		.flags = GENL_UNS_ADMIN_PERM,
13507 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13508 				  NL80211_FLAG_NEED_RTNL,
13509 	},
13510 	{
13511 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
13512 		.doit = nl80211_tdls_cancel_channel_switch,
13513 		.policy = nl80211_policy,
13514 		.flags = GENL_UNS_ADMIN_PERM,
13515 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13516 				  NL80211_FLAG_NEED_RTNL,
13517 	},
13518 	{
13519 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
13520 		.doit = nl80211_set_multicast_to_unicast,
13521 		.policy = nl80211_policy,
13522 		.flags = GENL_UNS_ADMIN_PERM,
13523 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13524 				  NL80211_FLAG_NEED_RTNL,
13525 	},
13526 	{
13527 		.cmd = NL80211_CMD_SET_PMK,
13528 		.doit = nl80211_set_pmk,
13529 		.policy = nl80211_policy,
13530 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13531 				  NL80211_FLAG_NEED_RTNL,
13532 	},
13533 	{
13534 		.cmd = NL80211_CMD_DEL_PMK,
13535 		.doit = nl80211_del_pmk,
13536 		.policy = nl80211_policy,
13537 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13538 				  NL80211_FLAG_NEED_RTNL,
13539 	},
13540 	{
13541 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
13542 		.doit = nl80211_external_auth,
13543 		.policy = nl80211_policy,
13544 		.flags = GENL_ADMIN_PERM,
13545 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13546 				  NL80211_FLAG_NEED_RTNL,
13547 	},
13548 	{
13549 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
13550 		.doit = nl80211_tx_control_port,
13551 		.policy = nl80211_policy,
13552 		.flags = GENL_UNS_ADMIN_PERM,
13553 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13554 				  NL80211_FLAG_NEED_RTNL,
13555 	},
13556 };
13557 
13558 static struct genl_family nl80211_fam __ro_after_init = {
13559 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
13560 	.hdrsize = 0,			/* no private header */
13561 	.version = 1,			/* no particular meaning now */
13562 	.maxattr = NL80211_ATTR_MAX,
13563 	.netnsok = true,
13564 	.pre_doit = nl80211_pre_doit,
13565 	.post_doit = nl80211_post_doit,
13566 	.module = THIS_MODULE,
13567 	.ops = nl80211_ops,
13568 	.n_ops = ARRAY_SIZE(nl80211_ops),
13569 	.mcgrps = nl80211_mcgrps,
13570 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
13571 };
13572 
13573 /* notification functions */
13574 
13575 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
13576 			  enum nl80211_commands cmd)
13577 {
13578 	struct sk_buff *msg;
13579 	struct nl80211_dump_wiphy_state state = {};
13580 
13581 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
13582 		cmd != NL80211_CMD_DEL_WIPHY);
13583 
13584 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13585 	if (!msg)
13586 		return;
13587 
13588 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
13589 		nlmsg_free(msg);
13590 		return;
13591 	}
13592 
13593 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13594 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
13595 }
13596 
13597 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
13598 				struct wireless_dev *wdev,
13599 				enum nl80211_commands cmd)
13600 {
13601 	struct sk_buff *msg;
13602 
13603 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
13604 		cmd != NL80211_CMD_DEL_INTERFACE);
13605 
13606 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13607 	if (!msg)
13608 		return;
13609 
13610 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev,
13611 			       cmd == NL80211_CMD_DEL_INTERFACE) < 0) {
13612 		nlmsg_free(msg);
13613 		return;
13614 	}
13615 
13616 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13617 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
13618 }
13619 
13620 static int nl80211_add_scan_req(struct sk_buff *msg,
13621 				struct cfg80211_registered_device *rdev)
13622 {
13623 	struct cfg80211_scan_request *req = rdev->scan_req;
13624 	struct nlattr *nest;
13625 	int i;
13626 
13627 	if (WARN_ON(!req))
13628 		return 0;
13629 
13630 	nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
13631 	if (!nest)
13632 		goto nla_put_failure;
13633 	for (i = 0; i < req->n_ssids; i++) {
13634 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
13635 			goto nla_put_failure;
13636 	}
13637 	nla_nest_end(msg, nest);
13638 
13639 	nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13640 	if (!nest)
13641 		goto nla_put_failure;
13642 	for (i = 0; i < req->n_channels; i++) {
13643 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13644 			goto nla_put_failure;
13645 	}
13646 	nla_nest_end(msg, nest);
13647 
13648 	if (req->ie &&
13649 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
13650 		goto nla_put_failure;
13651 
13652 	if (req->flags &&
13653 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
13654 		goto nla_put_failure;
13655 
13656 	if (req->info.scan_start_tsf &&
13657 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
13658 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
13659 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
13660 		     req->info.tsf_bssid)))
13661 		goto nla_put_failure;
13662 
13663 	return 0;
13664  nla_put_failure:
13665 	return -ENOBUFS;
13666 }
13667 
13668 static int nl80211_prep_scan_msg(struct sk_buff *msg,
13669 				 struct cfg80211_registered_device *rdev,
13670 				 struct wireless_dev *wdev,
13671 				 u32 portid, u32 seq, int flags,
13672 				 u32 cmd)
13673 {
13674 	void *hdr;
13675 
13676 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
13677 	if (!hdr)
13678 		return -1;
13679 
13680 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13681 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13682 					 wdev->netdev->ifindex)) ||
13683 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13684 			      NL80211_ATTR_PAD))
13685 		goto nla_put_failure;
13686 
13687 	/* ignore errors and send incomplete event anyway */
13688 	nl80211_add_scan_req(msg, rdev);
13689 
13690 	genlmsg_end(msg, hdr);
13691 	return 0;
13692 
13693  nla_put_failure:
13694 	genlmsg_cancel(msg, hdr);
13695 	return -EMSGSIZE;
13696 }
13697 
13698 static int
13699 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
13700 			    struct cfg80211_sched_scan_request *req, u32 cmd)
13701 {
13702 	void *hdr;
13703 
13704 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13705 	if (!hdr)
13706 		return -1;
13707 
13708 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
13709 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
13710 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
13711 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
13712 			      NL80211_ATTR_PAD))
13713 		goto nla_put_failure;
13714 
13715 	genlmsg_end(msg, hdr);
13716 	return 0;
13717 
13718  nla_put_failure:
13719 	genlmsg_cancel(msg, hdr);
13720 	return -EMSGSIZE;
13721 }
13722 
13723 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
13724 			     struct wireless_dev *wdev)
13725 {
13726 	struct sk_buff *msg;
13727 
13728 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13729 	if (!msg)
13730 		return;
13731 
13732 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13733 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
13734 		nlmsg_free(msg);
13735 		return;
13736 	}
13737 
13738 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13739 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13740 }
13741 
13742 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
13743 				       struct wireless_dev *wdev, bool aborted)
13744 {
13745 	struct sk_buff *msg;
13746 
13747 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13748 	if (!msg)
13749 		return NULL;
13750 
13751 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13752 				  aborted ? NL80211_CMD_SCAN_ABORTED :
13753 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
13754 		nlmsg_free(msg);
13755 		return NULL;
13756 	}
13757 
13758 	return msg;
13759 }
13760 
13761 /* send message created by nl80211_build_scan_msg() */
13762 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
13763 			   struct sk_buff *msg)
13764 {
13765 	if (!msg)
13766 		return;
13767 
13768 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13769 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13770 }
13771 
13772 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
13773 {
13774 	struct sk_buff *msg;
13775 
13776 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13777 	if (!msg)
13778 		return;
13779 
13780 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
13781 		nlmsg_free(msg);
13782 		return;
13783 	}
13784 
13785 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
13786 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13787 }
13788 
13789 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
13790 					  struct regulatory_request *request)
13791 {
13792 	/* Userspace can always count this one always being set */
13793 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
13794 		goto nla_put_failure;
13795 
13796 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
13797 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13798 			       NL80211_REGDOM_TYPE_WORLD))
13799 			goto nla_put_failure;
13800 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
13801 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13802 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
13803 			goto nla_put_failure;
13804 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
13805 		   request->intersect) {
13806 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13807 			       NL80211_REGDOM_TYPE_INTERSECTION))
13808 			goto nla_put_failure;
13809 	} else {
13810 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13811 			       NL80211_REGDOM_TYPE_COUNTRY) ||
13812 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
13813 				   request->alpha2))
13814 			goto nla_put_failure;
13815 	}
13816 
13817 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
13818 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
13819 
13820 		if (wiphy &&
13821 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
13822 			goto nla_put_failure;
13823 
13824 		if (wiphy &&
13825 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
13826 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
13827 			goto nla_put_failure;
13828 	}
13829 
13830 	return true;
13831 
13832 nla_put_failure:
13833 	return false;
13834 }
13835 
13836 /*
13837  * This can happen on global regulatory changes or device specific settings
13838  * based on custom regulatory domains.
13839  */
13840 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
13841 				     struct regulatory_request *request)
13842 {
13843 	struct sk_buff *msg;
13844 	void *hdr;
13845 
13846 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13847 	if (!msg)
13848 		return;
13849 
13850 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
13851 	if (!hdr) {
13852 		nlmsg_free(msg);
13853 		return;
13854 	}
13855 
13856 	if (nl80211_reg_change_event_fill(msg, request) == false)
13857 		goto nla_put_failure;
13858 
13859 	genlmsg_end(msg, hdr);
13860 
13861 	rcu_read_lock();
13862 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
13863 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
13864 	rcu_read_unlock();
13865 
13866 	return;
13867 
13868 nla_put_failure:
13869 	nlmsg_free(msg);
13870 }
13871 
13872 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
13873 				    struct net_device *netdev,
13874 				    const u8 *buf, size_t len,
13875 				    enum nl80211_commands cmd, gfp_t gfp,
13876 				    int uapsd_queues)
13877 {
13878 	struct sk_buff *msg;
13879 	void *hdr;
13880 
13881 	msg = nlmsg_new(100 + len, gfp);
13882 	if (!msg)
13883 		return;
13884 
13885 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13886 	if (!hdr) {
13887 		nlmsg_free(msg);
13888 		return;
13889 	}
13890 
13891 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13892 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13893 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf))
13894 		goto nla_put_failure;
13895 
13896 	if (uapsd_queues >= 0) {
13897 		struct nlattr *nla_wmm =
13898 			nla_nest_start(msg, NL80211_ATTR_STA_WME);
13899 		if (!nla_wmm)
13900 			goto nla_put_failure;
13901 
13902 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
13903 			       uapsd_queues))
13904 			goto nla_put_failure;
13905 
13906 		nla_nest_end(msg, nla_wmm);
13907 	}
13908 
13909 	genlmsg_end(msg, hdr);
13910 
13911 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13912 				NL80211_MCGRP_MLME, gfp);
13913 	return;
13914 
13915  nla_put_failure:
13916 	nlmsg_free(msg);
13917 }
13918 
13919 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
13920 			  struct net_device *netdev, const u8 *buf,
13921 			  size_t len, gfp_t gfp)
13922 {
13923 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13924 				NL80211_CMD_AUTHENTICATE, gfp, -1);
13925 }
13926 
13927 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
13928 			   struct net_device *netdev, const u8 *buf,
13929 			   size_t len, gfp_t gfp, int uapsd_queues)
13930 {
13931 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13932 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
13933 }
13934 
13935 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
13936 			 struct net_device *netdev, const u8 *buf,
13937 			 size_t len, gfp_t gfp)
13938 {
13939 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13940 				NL80211_CMD_DEAUTHENTICATE, gfp, -1);
13941 }
13942 
13943 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
13944 			   struct net_device *netdev, const u8 *buf,
13945 			   size_t len, gfp_t gfp)
13946 {
13947 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13948 				NL80211_CMD_DISASSOCIATE, gfp, -1);
13949 }
13950 
13951 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
13952 				  size_t len)
13953 {
13954 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13955 	struct wiphy *wiphy = wdev->wiphy;
13956 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13957 	const struct ieee80211_mgmt *mgmt = (void *)buf;
13958 	u32 cmd;
13959 
13960 	if (WARN_ON(len < 2))
13961 		return;
13962 
13963 	if (ieee80211_is_deauth(mgmt->frame_control))
13964 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
13965 	else
13966 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
13967 
13968 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
13969 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
13970 }
13971 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
13972 
13973 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
13974 				      struct net_device *netdev, int cmd,
13975 				      const u8 *addr, gfp_t gfp)
13976 {
13977 	struct sk_buff *msg;
13978 	void *hdr;
13979 
13980 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13981 	if (!msg)
13982 		return;
13983 
13984 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13985 	if (!hdr) {
13986 		nlmsg_free(msg);
13987 		return;
13988 	}
13989 
13990 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13991 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13992 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13993 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
13994 		goto nla_put_failure;
13995 
13996 	genlmsg_end(msg, hdr);
13997 
13998 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13999 				NL80211_MCGRP_MLME, gfp);
14000 	return;
14001 
14002  nla_put_failure:
14003 	nlmsg_free(msg);
14004 }
14005 
14006 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
14007 			       struct net_device *netdev, const u8 *addr,
14008 			       gfp_t gfp)
14009 {
14010 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
14011 				  addr, gfp);
14012 }
14013 
14014 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
14015 				struct net_device *netdev, const u8 *addr,
14016 				gfp_t gfp)
14017 {
14018 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
14019 				  addr, gfp);
14020 }
14021 
14022 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
14023 				 struct net_device *netdev,
14024 				 struct cfg80211_connect_resp_params *cr,
14025 				 gfp_t gfp)
14026 {
14027 	struct sk_buff *msg;
14028 	void *hdr;
14029 
14030 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
14031 			cr->fils_kek_len + cr->pmk_len +
14032 			(cr->pmkid ? WLAN_PMKID_LEN : 0), gfp);
14033 	if (!msg)
14034 		return;
14035 
14036 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
14037 	if (!hdr) {
14038 		nlmsg_free(msg);
14039 		return;
14040 	}
14041 
14042 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14043 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14044 	    (cr->bssid &&
14045 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
14046 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
14047 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
14048 			cr->status) ||
14049 	    (cr->status < 0 &&
14050 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
14051 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
14052 			  cr->timeout_reason))) ||
14053 	    (cr->req_ie &&
14054 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
14055 	    (cr->resp_ie &&
14056 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
14057 		     cr->resp_ie)) ||
14058 	    (cr->update_erp_next_seq_num &&
14059 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
14060 			 cr->fils_erp_next_seq_num)) ||
14061 	    (cr->status == WLAN_STATUS_SUCCESS &&
14062 	     ((cr->fils_kek &&
14063 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils_kek_len,
14064 		       cr->fils_kek)) ||
14065 	      (cr->pmk &&
14066 	       nla_put(msg, NL80211_ATTR_PMK, cr->pmk_len, cr->pmk)) ||
14067 	      (cr->pmkid &&
14068 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->pmkid)))))
14069 		goto nla_put_failure;
14070 
14071 	genlmsg_end(msg, hdr);
14072 
14073 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14074 				NL80211_MCGRP_MLME, gfp);
14075 	return;
14076 
14077  nla_put_failure:
14078 	nlmsg_free(msg);
14079 }
14080 
14081 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
14082 			 struct net_device *netdev,
14083 			 struct cfg80211_roam_info *info, gfp_t gfp)
14084 {
14085 	struct sk_buff *msg;
14086 	void *hdr;
14087 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
14088 
14089 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len, gfp);
14090 	if (!msg)
14091 		return;
14092 
14093 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
14094 	if (!hdr) {
14095 		nlmsg_free(msg);
14096 		return;
14097 	}
14098 
14099 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14100 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14101 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
14102 	    (info->req_ie &&
14103 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
14104 		     info->req_ie)) ||
14105 	    (info->resp_ie &&
14106 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
14107 		     info->resp_ie)))
14108 		goto nla_put_failure;
14109 
14110 	genlmsg_end(msg, hdr);
14111 
14112 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14113 				NL80211_MCGRP_MLME, gfp);
14114 	return;
14115 
14116  nla_put_failure:
14117 	nlmsg_free(msg);
14118 }
14119 
14120 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
14121 				  struct net_device *netdev, const u8 *bssid)
14122 {
14123 	struct sk_buff *msg;
14124 	void *hdr;
14125 
14126 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14127 	if (!msg)
14128 		return;
14129 
14130 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
14131 	if (!hdr) {
14132 		nlmsg_free(msg);
14133 		return;
14134 	}
14135 
14136 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14137 		goto nla_put_failure;
14138 
14139 	genlmsg_end(msg, hdr);
14140 
14141 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14142 				NL80211_MCGRP_MLME, GFP_KERNEL);
14143 	return;
14144 
14145  nla_put_failure:
14146 	nlmsg_free(msg);
14147 }
14148 
14149 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
14150 			       struct net_device *netdev, u16 reason,
14151 			       const u8 *ie, size_t ie_len, bool from_ap)
14152 {
14153 	struct sk_buff *msg;
14154 	void *hdr;
14155 
14156 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
14157 	if (!msg)
14158 		return;
14159 
14160 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
14161 	if (!hdr) {
14162 		nlmsg_free(msg);
14163 		return;
14164 	}
14165 
14166 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14167 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14168 	    (reason &&
14169 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
14170 	    (from_ap &&
14171 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
14172 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
14173 		goto nla_put_failure;
14174 
14175 	genlmsg_end(msg, hdr);
14176 
14177 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14178 				NL80211_MCGRP_MLME, GFP_KERNEL);
14179 	return;
14180 
14181  nla_put_failure:
14182 	nlmsg_free(msg);
14183 }
14184 
14185 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
14186 			     struct net_device *netdev, const u8 *bssid,
14187 			     gfp_t gfp)
14188 {
14189 	struct sk_buff *msg;
14190 	void *hdr;
14191 
14192 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14193 	if (!msg)
14194 		return;
14195 
14196 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
14197 	if (!hdr) {
14198 		nlmsg_free(msg);
14199 		return;
14200 	}
14201 
14202 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14203 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14204 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14205 		goto nla_put_failure;
14206 
14207 	genlmsg_end(msg, hdr);
14208 
14209 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14210 				NL80211_MCGRP_MLME, gfp);
14211 	return;
14212 
14213  nla_put_failure:
14214 	nlmsg_free(msg);
14215 }
14216 
14217 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
14218 					const u8* ie, u8 ie_len, gfp_t gfp)
14219 {
14220 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14221 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14222 	struct sk_buff *msg;
14223 	void *hdr;
14224 
14225 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
14226 		return;
14227 
14228 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
14229 
14230 	msg = nlmsg_new(100 + ie_len, gfp);
14231 	if (!msg)
14232 		return;
14233 
14234 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
14235 	if (!hdr) {
14236 		nlmsg_free(msg);
14237 		return;
14238 	}
14239 
14240 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14241 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14242 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14243 	    (ie_len && ie &&
14244 	     nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
14245 		goto nla_put_failure;
14246 
14247 	genlmsg_end(msg, hdr);
14248 
14249 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14250 				NL80211_MCGRP_MLME, gfp);
14251 	return;
14252 
14253  nla_put_failure:
14254 	nlmsg_free(msg);
14255 }
14256 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
14257 
14258 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
14259 				 struct net_device *netdev, const u8 *addr,
14260 				 enum nl80211_key_type key_type, int key_id,
14261 				 const u8 *tsc, gfp_t gfp)
14262 {
14263 	struct sk_buff *msg;
14264 	void *hdr;
14265 
14266 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14267 	if (!msg)
14268 		return;
14269 
14270 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
14271 	if (!hdr) {
14272 		nlmsg_free(msg);
14273 		return;
14274 	}
14275 
14276 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14277 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14278 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
14279 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
14280 	    (key_id != -1 &&
14281 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
14282 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
14283 		goto nla_put_failure;
14284 
14285 	genlmsg_end(msg, hdr);
14286 
14287 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14288 				NL80211_MCGRP_MLME, gfp);
14289 	return;
14290 
14291  nla_put_failure:
14292 	nlmsg_free(msg);
14293 }
14294 
14295 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
14296 				    struct ieee80211_channel *channel_before,
14297 				    struct ieee80211_channel *channel_after)
14298 {
14299 	struct sk_buff *msg;
14300 	void *hdr;
14301 	struct nlattr *nl_freq;
14302 
14303 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
14304 	if (!msg)
14305 		return;
14306 
14307 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
14308 	if (!hdr) {
14309 		nlmsg_free(msg);
14310 		return;
14311 	}
14312 
14313 	/*
14314 	 * Since we are applying the beacon hint to a wiphy we know its
14315 	 * wiphy_idx is valid
14316 	 */
14317 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
14318 		goto nla_put_failure;
14319 
14320 	/* Before */
14321 	nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
14322 	if (!nl_freq)
14323 		goto nla_put_failure;
14324 	if (nl80211_msg_put_channel(msg, channel_before, false))
14325 		goto nla_put_failure;
14326 	nla_nest_end(msg, nl_freq);
14327 
14328 	/* After */
14329 	nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
14330 	if (!nl_freq)
14331 		goto nla_put_failure;
14332 	if (nl80211_msg_put_channel(msg, channel_after, false))
14333 		goto nla_put_failure;
14334 	nla_nest_end(msg, nl_freq);
14335 
14336 	genlmsg_end(msg, hdr);
14337 
14338 	rcu_read_lock();
14339 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14340 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14341 	rcu_read_unlock();
14342 
14343 	return;
14344 
14345 nla_put_failure:
14346 	nlmsg_free(msg);
14347 }
14348 
14349 static void nl80211_send_remain_on_chan_event(
14350 	int cmd, struct cfg80211_registered_device *rdev,
14351 	struct wireless_dev *wdev, u64 cookie,
14352 	struct ieee80211_channel *chan,
14353 	unsigned int duration, gfp_t gfp)
14354 {
14355 	struct sk_buff *msg;
14356 	void *hdr;
14357 
14358 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14359 	if (!msg)
14360 		return;
14361 
14362 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14363 	if (!hdr) {
14364 		nlmsg_free(msg);
14365 		return;
14366 	}
14367 
14368 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14369 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14370 					 wdev->netdev->ifindex)) ||
14371 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14372 			      NL80211_ATTR_PAD) ||
14373 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
14374 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
14375 			NL80211_CHAN_NO_HT) ||
14376 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14377 			      NL80211_ATTR_PAD))
14378 		goto nla_put_failure;
14379 
14380 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
14381 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
14382 		goto nla_put_failure;
14383 
14384 	genlmsg_end(msg, hdr);
14385 
14386 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14387 				NL80211_MCGRP_MLME, gfp);
14388 	return;
14389 
14390  nla_put_failure:
14391 	nlmsg_free(msg);
14392 }
14393 
14394 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
14395 			       struct ieee80211_channel *chan,
14396 			       unsigned int duration, gfp_t gfp)
14397 {
14398 	struct wiphy *wiphy = wdev->wiphy;
14399 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14400 
14401 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
14402 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
14403 					  rdev, wdev, cookie, chan,
14404 					  duration, gfp);
14405 }
14406 EXPORT_SYMBOL(cfg80211_ready_on_channel);
14407 
14408 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
14409 					struct ieee80211_channel *chan,
14410 					gfp_t gfp)
14411 {
14412 	struct wiphy *wiphy = wdev->wiphy;
14413 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14414 
14415 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
14416 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14417 					  rdev, wdev, cookie, chan, 0, gfp);
14418 }
14419 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
14420 
14421 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
14422 		      struct station_info *sinfo, gfp_t gfp)
14423 {
14424 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14425 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14426 	struct sk_buff *msg;
14427 
14428 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
14429 
14430 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14431 	if (!msg)
14432 		return;
14433 
14434 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
14435 				 rdev, dev, mac_addr, sinfo) < 0) {
14436 		nlmsg_free(msg);
14437 		return;
14438 	}
14439 
14440 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14441 				NL80211_MCGRP_MLME, gfp);
14442 }
14443 EXPORT_SYMBOL(cfg80211_new_sta);
14444 
14445 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
14446 			    struct station_info *sinfo, gfp_t gfp)
14447 {
14448 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14449 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14450 	struct sk_buff *msg;
14451 	struct station_info empty_sinfo = {};
14452 
14453 	if (!sinfo)
14454 		sinfo = &empty_sinfo;
14455 
14456 	trace_cfg80211_del_sta(dev, mac_addr);
14457 
14458 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14459 	if (!msg)
14460 		return;
14461 
14462 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
14463 				 rdev, dev, mac_addr, sinfo) < 0) {
14464 		nlmsg_free(msg);
14465 		return;
14466 	}
14467 
14468 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14469 				NL80211_MCGRP_MLME, gfp);
14470 }
14471 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
14472 
14473 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
14474 			  enum nl80211_connect_failed_reason reason,
14475 			  gfp_t gfp)
14476 {
14477 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14478 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14479 	struct sk_buff *msg;
14480 	void *hdr;
14481 
14482 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
14483 	if (!msg)
14484 		return;
14485 
14486 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
14487 	if (!hdr) {
14488 		nlmsg_free(msg);
14489 		return;
14490 	}
14491 
14492 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14493 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
14494 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
14495 		goto nla_put_failure;
14496 
14497 	genlmsg_end(msg, hdr);
14498 
14499 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14500 				NL80211_MCGRP_MLME, gfp);
14501 	return;
14502 
14503  nla_put_failure:
14504 	nlmsg_free(msg);
14505 }
14506 EXPORT_SYMBOL(cfg80211_conn_failed);
14507 
14508 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
14509 				       const u8 *addr, gfp_t gfp)
14510 {
14511 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14512 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14513 	struct sk_buff *msg;
14514 	void *hdr;
14515 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
14516 
14517 	if (!nlportid)
14518 		return false;
14519 
14520 	msg = nlmsg_new(100, gfp);
14521 	if (!msg)
14522 		return true;
14523 
14524 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14525 	if (!hdr) {
14526 		nlmsg_free(msg);
14527 		return true;
14528 	}
14529 
14530 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14531 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14532 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14533 		goto nla_put_failure;
14534 
14535 	genlmsg_end(msg, hdr);
14536 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14537 	return true;
14538 
14539  nla_put_failure:
14540 	nlmsg_free(msg);
14541 	return true;
14542 }
14543 
14544 bool cfg80211_rx_spurious_frame(struct net_device *dev,
14545 				const u8 *addr, gfp_t gfp)
14546 {
14547 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14548 	bool ret;
14549 
14550 	trace_cfg80211_rx_spurious_frame(dev, addr);
14551 
14552 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14553 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
14554 		trace_cfg80211_return_bool(false);
14555 		return false;
14556 	}
14557 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
14558 					 addr, gfp);
14559 	trace_cfg80211_return_bool(ret);
14560 	return ret;
14561 }
14562 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
14563 
14564 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
14565 					const u8 *addr, gfp_t gfp)
14566 {
14567 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14568 	bool ret;
14569 
14570 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
14571 
14572 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14573 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
14574 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
14575 		trace_cfg80211_return_bool(false);
14576 		return false;
14577 	}
14578 	ret = __nl80211_unexpected_frame(dev,
14579 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
14580 					 addr, gfp);
14581 	trace_cfg80211_return_bool(ret);
14582 	return ret;
14583 }
14584 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
14585 
14586 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
14587 		      struct wireless_dev *wdev, u32 nlportid,
14588 		      int freq, int sig_dbm,
14589 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
14590 {
14591 	struct net_device *netdev = wdev->netdev;
14592 	struct sk_buff *msg;
14593 	void *hdr;
14594 
14595 	msg = nlmsg_new(100 + len, gfp);
14596 	if (!msg)
14597 		return -ENOMEM;
14598 
14599 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14600 	if (!hdr) {
14601 		nlmsg_free(msg);
14602 		return -ENOMEM;
14603 	}
14604 
14605 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14606 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14607 					netdev->ifindex)) ||
14608 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14609 			      NL80211_ATTR_PAD) ||
14610 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
14611 	    (sig_dbm &&
14612 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14613 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14614 	    (flags &&
14615 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
14616 		goto nla_put_failure;
14617 
14618 	genlmsg_end(msg, hdr);
14619 
14620 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14621 
14622  nla_put_failure:
14623 	nlmsg_free(msg);
14624 	return -ENOBUFS;
14625 }
14626 
14627 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
14628 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
14629 {
14630 	struct wiphy *wiphy = wdev->wiphy;
14631 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14632 	struct net_device *netdev = wdev->netdev;
14633 	struct sk_buff *msg;
14634 	void *hdr;
14635 
14636 	trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
14637 
14638 	msg = nlmsg_new(100 + len, gfp);
14639 	if (!msg)
14640 		return;
14641 
14642 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
14643 	if (!hdr) {
14644 		nlmsg_free(msg);
14645 		return;
14646 	}
14647 
14648 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14649 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14650 				   netdev->ifindex)) ||
14651 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14652 			      NL80211_ATTR_PAD) ||
14653 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14654 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14655 			      NL80211_ATTR_PAD) ||
14656 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
14657 		goto nla_put_failure;
14658 
14659 	genlmsg_end(msg, hdr);
14660 
14661 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14662 				NL80211_MCGRP_MLME, gfp);
14663 	return;
14664 
14665  nla_put_failure:
14666 	nlmsg_free(msg);
14667 }
14668 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
14669 
14670 static int __nl80211_rx_control_port(struct net_device *dev,
14671 				     const u8 *buf, size_t len,
14672 				     const u8 *addr, u16 proto,
14673 				     bool unencrypted, gfp_t gfp)
14674 {
14675 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14676 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14677 	struct sk_buff *msg;
14678 	void *hdr;
14679 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
14680 
14681 	if (!nlportid)
14682 		return -ENOENT;
14683 
14684 	msg = nlmsg_new(100 + len, gfp);
14685 	if (!msg)
14686 		return -ENOMEM;
14687 
14688 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
14689 	if (!hdr) {
14690 		nlmsg_free(msg);
14691 		return -ENOBUFS;
14692 	}
14693 
14694 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14695 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14696 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14697 			      NL80211_ATTR_PAD) ||
14698 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14699 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14700 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
14701 	    (unencrypted && nla_put_flag(msg,
14702 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
14703 		goto nla_put_failure;
14704 
14705 	genlmsg_end(msg, hdr);
14706 
14707 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14708 
14709  nla_put_failure:
14710 	nlmsg_free(msg);
14711 	return -ENOBUFS;
14712 }
14713 
14714 bool cfg80211_rx_control_port(struct net_device *dev,
14715 			      const u8 *buf, size_t len,
14716 			      const u8 *addr, u16 proto, bool unencrypted)
14717 {
14718 	int ret;
14719 
14720 	trace_cfg80211_rx_control_port(dev, buf, len, addr, proto, unencrypted);
14721 	ret = __nl80211_rx_control_port(dev, buf, len, addr, proto,
14722 					unencrypted, GFP_ATOMIC);
14723 	trace_cfg80211_return_bool(ret == 0);
14724 	return ret == 0;
14725 }
14726 EXPORT_SYMBOL(cfg80211_rx_control_port);
14727 
14728 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
14729 					    const char *mac, gfp_t gfp)
14730 {
14731 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14732 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14733 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14734 	void **cb;
14735 
14736 	if (!msg)
14737 		return NULL;
14738 
14739 	cb = (void **)msg->cb;
14740 
14741 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
14742 	if (!cb[0]) {
14743 		nlmsg_free(msg);
14744 		return NULL;
14745 	}
14746 
14747 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14748 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14749 		goto nla_put_failure;
14750 
14751 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
14752 		goto nla_put_failure;
14753 
14754 	cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
14755 	if (!cb[1])
14756 		goto nla_put_failure;
14757 
14758 	cb[2] = rdev;
14759 
14760 	return msg;
14761  nla_put_failure:
14762 	nlmsg_free(msg);
14763 	return NULL;
14764 }
14765 
14766 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
14767 {
14768 	void **cb = (void **)msg->cb;
14769 	struct cfg80211_registered_device *rdev = cb[2];
14770 
14771 	nla_nest_end(msg, cb[1]);
14772 	genlmsg_end(msg, cb[0]);
14773 
14774 	memset(msg->cb, 0, sizeof(msg->cb));
14775 
14776 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14777 				NL80211_MCGRP_MLME, gfp);
14778 }
14779 
14780 void cfg80211_cqm_rssi_notify(struct net_device *dev,
14781 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
14782 			      s32 rssi_level, gfp_t gfp)
14783 {
14784 	struct sk_buff *msg;
14785 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14786 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14787 
14788 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
14789 
14790 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
14791 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
14792 		return;
14793 
14794 	if (wdev->cqm_config) {
14795 		wdev->cqm_config->last_rssi_event_value = rssi_level;
14796 
14797 		cfg80211_cqm_rssi_update(rdev, dev);
14798 
14799 		if (rssi_level == 0)
14800 			rssi_level = wdev->cqm_config->last_rssi_event_value;
14801 	}
14802 
14803 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14804 	if (!msg)
14805 		return;
14806 
14807 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
14808 			rssi_event))
14809 		goto nla_put_failure;
14810 
14811 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
14812 				      rssi_level))
14813 		goto nla_put_failure;
14814 
14815 	cfg80211_send_cqm(msg, gfp);
14816 
14817 	return;
14818 
14819  nla_put_failure:
14820 	nlmsg_free(msg);
14821 }
14822 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
14823 
14824 void cfg80211_cqm_txe_notify(struct net_device *dev,
14825 			     const u8 *peer, u32 num_packets,
14826 			     u32 rate, u32 intvl, gfp_t gfp)
14827 {
14828 	struct sk_buff *msg;
14829 
14830 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
14831 	if (!msg)
14832 		return;
14833 
14834 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
14835 		goto nla_put_failure;
14836 
14837 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
14838 		goto nla_put_failure;
14839 
14840 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
14841 		goto nla_put_failure;
14842 
14843 	cfg80211_send_cqm(msg, gfp);
14844 	return;
14845 
14846  nla_put_failure:
14847 	nlmsg_free(msg);
14848 }
14849 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
14850 
14851 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
14852 				 const u8 *peer, u32 num_packets, gfp_t gfp)
14853 {
14854 	struct sk_buff *msg;
14855 
14856 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
14857 
14858 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
14859 	if (!msg)
14860 		return;
14861 
14862 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
14863 		goto nla_put_failure;
14864 
14865 	cfg80211_send_cqm(msg, gfp);
14866 	return;
14867 
14868  nla_put_failure:
14869 	nlmsg_free(msg);
14870 }
14871 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
14872 
14873 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
14874 {
14875 	struct sk_buff *msg;
14876 
14877 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14878 	if (!msg)
14879 		return;
14880 
14881 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
14882 		goto nla_put_failure;
14883 
14884 	cfg80211_send_cqm(msg, gfp);
14885 	return;
14886 
14887  nla_put_failure:
14888 	nlmsg_free(msg);
14889 }
14890 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
14891 
14892 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
14893 				     struct net_device *netdev, const u8 *bssid,
14894 				     const u8 *replay_ctr, gfp_t gfp)
14895 {
14896 	struct sk_buff *msg;
14897 	struct nlattr *rekey_attr;
14898 	void *hdr;
14899 
14900 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14901 	if (!msg)
14902 		return;
14903 
14904 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
14905 	if (!hdr) {
14906 		nlmsg_free(msg);
14907 		return;
14908 	}
14909 
14910 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14911 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14912 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14913 		goto nla_put_failure;
14914 
14915 	rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
14916 	if (!rekey_attr)
14917 		goto nla_put_failure;
14918 
14919 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
14920 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
14921 		goto nla_put_failure;
14922 
14923 	nla_nest_end(msg, rekey_attr);
14924 
14925 	genlmsg_end(msg, hdr);
14926 
14927 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14928 				NL80211_MCGRP_MLME, gfp);
14929 	return;
14930 
14931  nla_put_failure:
14932 	nlmsg_free(msg);
14933 }
14934 
14935 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
14936 			       const u8 *replay_ctr, gfp_t gfp)
14937 {
14938 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14939 	struct wiphy *wiphy = wdev->wiphy;
14940 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14941 
14942 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
14943 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
14944 }
14945 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
14946 
14947 static void
14948 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
14949 			       struct net_device *netdev, int index,
14950 			       const u8 *bssid, bool preauth, gfp_t gfp)
14951 {
14952 	struct sk_buff *msg;
14953 	struct nlattr *attr;
14954 	void *hdr;
14955 
14956 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14957 	if (!msg)
14958 		return;
14959 
14960 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
14961 	if (!hdr) {
14962 		nlmsg_free(msg);
14963 		return;
14964 	}
14965 
14966 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14967 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14968 		goto nla_put_failure;
14969 
14970 	attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
14971 	if (!attr)
14972 		goto nla_put_failure;
14973 
14974 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
14975 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
14976 	    (preauth &&
14977 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
14978 		goto nla_put_failure;
14979 
14980 	nla_nest_end(msg, attr);
14981 
14982 	genlmsg_end(msg, hdr);
14983 
14984 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14985 				NL80211_MCGRP_MLME, gfp);
14986 	return;
14987 
14988  nla_put_failure:
14989 	nlmsg_free(msg);
14990 }
14991 
14992 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
14993 				     const u8 *bssid, bool preauth, gfp_t gfp)
14994 {
14995 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14996 	struct wiphy *wiphy = wdev->wiphy;
14997 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14998 
14999 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
15000 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
15001 }
15002 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
15003 
15004 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
15005 				     struct net_device *netdev,
15006 				     struct cfg80211_chan_def *chandef,
15007 				     gfp_t gfp,
15008 				     enum nl80211_commands notif,
15009 				     u8 count)
15010 {
15011 	struct sk_buff *msg;
15012 	void *hdr;
15013 
15014 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15015 	if (!msg)
15016 		return;
15017 
15018 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
15019 	if (!hdr) {
15020 		nlmsg_free(msg);
15021 		return;
15022 	}
15023 
15024 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
15025 		goto nla_put_failure;
15026 
15027 	if (nl80211_send_chandef(msg, chandef))
15028 		goto nla_put_failure;
15029 
15030 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
15031 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
15032 			goto nla_put_failure;
15033 
15034 	genlmsg_end(msg, hdr);
15035 
15036 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15037 				NL80211_MCGRP_MLME, gfp);
15038 	return;
15039 
15040  nla_put_failure:
15041 	nlmsg_free(msg);
15042 }
15043 
15044 void cfg80211_ch_switch_notify(struct net_device *dev,
15045 			       struct cfg80211_chan_def *chandef)
15046 {
15047 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15048 	struct wiphy *wiphy = wdev->wiphy;
15049 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15050 
15051 	ASSERT_WDEV_LOCK(wdev);
15052 
15053 	trace_cfg80211_ch_switch_notify(dev, chandef);
15054 
15055 	wdev->chandef = *chandef;
15056 	wdev->preset_chandef = *chandef;
15057 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
15058 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
15059 }
15060 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
15061 
15062 void cfg80211_ch_switch_started_notify(struct net_device *dev,
15063 				       struct cfg80211_chan_def *chandef,
15064 				       u8 count)
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 
15070 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
15071 
15072 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
15073 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
15074 }
15075 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
15076 
15077 void
15078 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
15079 		     const struct cfg80211_chan_def *chandef,
15080 		     enum nl80211_radar_event event,
15081 		     struct net_device *netdev, gfp_t gfp)
15082 {
15083 	struct sk_buff *msg;
15084 	void *hdr;
15085 
15086 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15087 	if (!msg)
15088 		return;
15089 
15090 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
15091 	if (!hdr) {
15092 		nlmsg_free(msg);
15093 		return;
15094 	}
15095 
15096 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
15097 		goto nla_put_failure;
15098 
15099 	/* NOP and radar events don't need a netdev parameter */
15100 	if (netdev) {
15101 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
15102 
15103 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15104 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15105 				      NL80211_ATTR_PAD))
15106 			goto nla_put_failure;
15107 	}
15108 
15109 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
15110 		goto nla_put_failure;
15111 
15112 	if (nl80211_send_chandef(msg, chandef))
15113 		goto nla_put_failure;
15114 
15115 	genlmsg_end(msg, hdr);
15116 
15117 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15118 				NL80211_MCGRP_MLME, gfp);
15119 	return;
15120 
15121  nla_put_failure:
15122 	nlmsg_free(msg);
15123 }
15124 
15125 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
15126 				       struct sta_opmode_info *sta_opmode,
15127 				       gfp_t gfp)
15128 {
15129 	struct sk_buff *msg;
15130 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15131 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15132 	void *hdr;
15133 
15134 	if (WARN_ON(!mac))
15135 		return;
15136 
15137 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15138 	if (!msg)
15139 		return;
15140 
15141 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
15142 	if (!hdr) {
15143 		nlmsg_free(msg);
15144 		return;
15145 	}
15146 
15147 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
15148 		goto nla_put_failure;
15149 
15150 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15151 		goto nla_put_failure;
15152 
15153 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15154 		goto nla_put_failure;
15155 
15156 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
15157 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
15158 		goto nla_put_failure;
15159 
15160 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
15161 	    nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
15162 		goto nla_put_failure;
15163 
15164 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
15165 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
15166 		goto nla_put_failure;
15167 
15168 	genlmsg_end(msg, hdr);
15169 
15170 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15171 				NL80211_MCGRP_MLME, gfp);
15172 
15173 	return;
15174 
15175 nla_put_failure:
15176 	nlmsg_free(msg);
15177 }
15178 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
15179 
15180 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
15181 			   u64 cookie, bool acked, s32 ack_signal,
15182 			   bool is_valid_ack_signal, gfp_t gfp)
15183 {
15184 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15185 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15186 	struct sk_buff *msg;
15187 	void *hdr;
15188 
15189 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
15190 
15191 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15192 
15193 	if (!msg)
15194 		return;
15195 
15196 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
15197 	if (!hdr) {
15198 		nlmsg_free(msg);
15199 		return;
15200 	}
15201 
15202 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15203 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15204 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15205 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15206 			      NL80211_ATTR_PAD) ||
15207 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
15208 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
15209 						ack_signal)))
15210 		goto nla_put_failure;
15211 
15212 	genlmsg_end(msg, hdr);
15213 
15214 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15215 				NL80211_MCGRP_MLME, gfp);
15216 	return;
15217 
15218  nla_put_failure:
15219 	nlmsg_free(msg);
15220 }
15221 EXPORT_SYMBOL(cfg80211_probe_status);
15222 
15223 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
15224 				 const u8 *frame, size_t len,
15225 				 int freq, int sig_dbm)
15226 {
15227 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15228 	struct sk_buff *msg;
15229 	void *hdr;
15230 	struct cfg80211_beacon_registration *reg;
15231 
15232 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
15233 
15234 	spin_lock_bh(&rdev->beacon_registrations_lock);
15235 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
15236 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
15237 		if (!msg) {
15238 			spin_unlock_bh(&rdev->beacon_registrations_lock);
15239 			return;
15240 		}
15241 
15242 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15243 		if (!hdr)
15244 			goto nla_put_failure;
15245 
15246 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15247 		    (freq &&
15248 		     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
15249 		    (sig_dbm &&
15250 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15251 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
15252 			goto nla_put_failure;
15253 
15254 		genlmsg_end(msg, hdr);
15255 
15256 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
15257 	}
15258 	spin_unlock_bh(&rdev->beacon_registrations_lock);
15259 	return;
15260 
15261  nla_put_failure:
15262 	spin_unlock_bh(&rdev->beacon_registrations_lock);
15263 	nlmsg_free(msg);
15264 }
15265 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
15266 
15267 #ifdef CONFIG_PM
15268 static int cfg80211_net_detect_results(struct sk_buff *msg,
15269 				       struct cfg80211_wowlan_wakeup *wakeup)
15270 {
15271 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
15272 	struct nlattr *nl_results, *nl_match, *nl_freqs;
15273 	int i, j;
15274 
15275 	nl_results = nla_nest_start(
15276 		msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
15277 	if (!nl_results)
15278 		return -EMSGSIZE;
15279 
15280 	for (i = 0; i < nd->n_matches; i++) {
15281 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
15282 
15283 		nl_match = nla_nest_start(msg, i);
15284 		if (!nl_match)
15285 			break;
15286 
15287 		/* The SSID attribute is optional in nl80211, but for
15288 		 * simplicity reasons it's always present in the
15289 		 * cfg80211 structure.  If a driver can't pass the
15290 		 * SSID, that needs to be changed.  A zero length SSID
15291 		 * is still a valid SSID (wildcard), so it cannot be
15292 		 * used for this purpose.
15293 		 */
15294 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
15295 			    match->ssid.ssid)) {
15296 			nla_nest_cancel(msg, nl_match);
15297 			goto out;
15298 		}
15299 
15300 		if (match->n_channels) {
15301 			nl_freqs = nla_nest_start(
15302 				msg, NL80211_ATTR_SCAN_FREQUENCIES);
15303 			if (!nl_freqs) {
15304 				nla_nest_cancel(msg, nl_match);
15305 				goto out;
15306 			}
15307 
15308 			for (j = 0; j < match->n_channels; j++) {
15309 				if (nla_put_u32(msg, j, match->channels[j])) {
15310 					nla_nest_cancel(msg, nl_freqs);
15311 					nla_nest_cancel(msg, nl_match);
15312 					goto out;
15313 				}
15314 			}
15315 
15316 			nla_nest_end(msg, nl_freqs);
15317 		}
15318 
15319 		nla_nest_end(msg, nl_match);
15320 	}
15321 
15322 out:
15323 	nla_nest_end(msg, nl_results);
15324 	return 0;
15325 }
15326 
15327 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
15328 				   struct cfg80211_wowlan_wakeup *wakeup,
15329 				   gfp_t gfp)
15330 {
15331 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15332 	struct sk_buff *msg;
15333 	void *hdr;
15334 	int size = 200;
15335 
15336 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
15337 
15338 	if (wakeup)
15339 		size += wakeup->packet_present_len;
15340 
15341 	msg = nlmsg_new(size, gfp);
15342 	if (!msg)
15343 		return;
15344 
15345 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
15346 	if (!hdr)
15347 		goto free_msg;
15348 
15349 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15350 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15351 			      NL80211_ATTR_PAD))
15352 		goto free_msg;
15353 
15354 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15355 					wdev->netdev->ifindex))
15356 		goto free_msg;
15357 
15358 	if (wakeup) {
15359 		struct nlattr *reasons;
15360 
15361 		reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
15362 		if (!reasons)
15363 			goto free_msg;
15364 
15365 		if (wakeup->disconnect &&
15366 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
15367 			goto free_msg;
15368 		if (wakeup->magic_pkt &&
15369 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
15370 			goto free_msg;
15371 		if (wakeup->gtk_rekey_failure &&
15372 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
15373 			goto free_msg;
15374 		if (wakeup->eap_identity_req &&
15375 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
15376 			goto free_msg;
15377 		if (wakeup->four_way_handshake &&
15378 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
15379 			goto free_msg;
15380 		if (wakeup->rfkill_release &&
15381 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
15382 			goto free_msg;
15383 
15384 		if (wakeup->pattern_idx >= 0 &&
15385 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
15386 				wakeup->pattern_idx))
15387 			goto free_msg;
15388 
15389 		if (wakeup->tcp_match &&
15390 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
15391 			goto free_msg;
15392 
15393 		if (wakeup->tcp_connlost &&
15394 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
15395 			goto free_msg;
15396 
15397 		if (wakeup->tcp_nomoretokens &&
15398 		    nla_put_flag(msg,
15399 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
15400 			goto free_msg;
15401 
15402 		if (wakeup->packet) {
15403 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
15404 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
15405 
15406 			if (!wakeup->packet_80211) {
15407 				pkt_attr =
15408 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
15409 				len_attr =
15410 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
15411 			}
15412 
15413 			if (wakeup->packet_len &&
15414 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
15415 				goto free_msg;
15416 
15417 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
15418 				    wakeup->packet))
15419 				goto free_msg;
15420 		}
15421 
15422 		if (wakeup->net_detect &&
15423 		    cfg80211_net_detect_results(msg, wakeup))
15424 				goto free_msg;
15425 
15426 		nla_nest_end(msg, reasons);
15427 	}
15428 
15429 	genlmsg_end(msg, hdr);
15430 
15431 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15432 				NL80211_MCGRP_MLME, gfp);
15433 	return;
15434 
15435  free_msg:
15436 	nlmsg_free(msg);
15437 }
15438 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
15439 #endif
15440 
15441 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
15442 				enum nl80211_tdls_operation oper,
15443 				u16 reason_code, gfp_t gfp)
15444 {
15445 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15446 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15447 	struct sk_buff *msg;
15448 	void *hdr;
15449 
15450 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
15451 					 reason_code);
15452 
15453 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15454 	if (!msg)
15455 		return;
15456 
15457 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
15458 	if (!hdr) {
15459 		nlmsg_free(msg);
15460 		return;
15461 	}
15462 
15463 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15464 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15465 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
15466 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
15467 	    (reason_code > 0 &&
15468 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
15469 		goto nla_put_failure;
15470 
15471 	genlmsg_end(msg, hdr);
15472 
15473 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15474 				NL80211_MCGRP_MLME, gfp);
15475 	return;
15476 
15477  nla_put_failure:
15478 	nlmsg_free(msg);
15479 }
15480 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
15481 
15482 static int nl80211_netlink_notify(struct notifier_block * nb,
15483 				  unsigned long state,
15484 				  void *_notify)
15485 {
15486 	struct netlink_notify *notify = _notify;
15487 	struct cfg80211_registered_device *rdev;
15488 	struct wireless_dev *wdev;
15489 	struct cfg80211_beacon_registration *reg, *tmp;
15490 
15491 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
15492 		return NOTIFY_DONE;
15493 
15494 	rcu_read_lock();
15495 
15496 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
15497 		struct cfg80211_sched_scan_request *sched_scan_req;
15498 
15499 		list_for_each_entry_rcu(sched_scan_req,
15500 					&rdev->sched_scan_req_list,
15501 					list) {
15502 			if (sched_scan_req->owner_nlportid == notify->portid) {
15503 				sched_scan_req->nl_owner_dead = true;
15504 				schedule_work(&rdev->sched_scan_stop_wk);
15505 			}
15506 		}
15507 
15508 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
15509 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
15510 
15511 			if (wdev->owner_nlportid == notify->portid) {
15512 				wdev->nl_owner_dead = true;
15513 				schedule_work(&rdev->destroy_work);
15514 			} else if (wdev->conn_owner_nlportid == notify->portid) {
15515 				schedule_work(&wdev->disconnect_wk);
15516 			}
15517 		}
15518 
15519 		spin_lock_bh(&rdev->beacon_registrations_lock);
15520 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
15521 					 list) {
15522 			if (reg->nlportid == notify->portid) {
15523 				list_del(&reg->list);
15524 				kfree(reg);
15525 				break;
15526 			}
15527 		}
15528 		spin_unlock_bh(&rdev->beacon_registrations_lock);
15529 	}
15530 
15531 	rcu_read_unlock();
15532 
15533 	/*
15534 	 * It is possible that the user space process that is controlling the
15535 	 * indoor setting disappeared, so notify the regulatory core.
15536 	 */
15537 	regulatory_netlink_notify(notify->portid);
15538 	return NOTIFY_OK;
15539 }
15540 
15541 static struct notifier_block nl80211_netlink_notifier = {
15542 	.notifier_call = nl80211_netlink_notify,
15543 };
15544 
15545 void cfg80211_ft_event(struct net_device *netdev,
15546 		       struct cfg80211_ft_event_params *ft_event)
15547 {
15548 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
15549 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15550 	struct sk_buff *msg;
15551 	void *hdr;
15552 
15553 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
15554 
15555 	if (!ft_event->target_ap)
15556 		return;
15557 
15558 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
15559 			GFP_KERNEL);
15560 	if (!msg)
15561 		return;
15562 
15563 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
15564 	if (!hdr)
15565 		goto out;
15566 
15567 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15568 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15569 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
15570 		goto out;
15571 
15572 	if (ft_event->ies &&
15573 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
15574 		goto out;
15575 	if (ft_event->ric_ies &&
15576 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
15577 		    ft_event->ric_ies))
15578 		goto out;
15579 
15580 	genlmsg_end(msg, hdr);
15581 
15582 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15583 				NL80211_MCGRP_MLME, GFP_KERNEL);
15584 	return;
15585  out:
15586 	nlmsg_free(msg);
15587 }
15588 EXPORT_SYMBOL(cfg80211_ft_event);
15589 
15590 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
15591 {
15592 	struct cfg80211_registered_device *rdev;
15593 	struct sk_buff *msg;
15594 	void *hdr;
15595 	u32 nlportid;
15596 
15597 	rdev = wiphy_to_rdev(wdev->wiphy);
15598 	if (!rdev->crit_proto_nlportid)
15599 		return;
15600 
15601 	nlportid = rdev->crit_proto_nlportid;
15602 	rdev->crit_proto_nlportid = 0;
15603 
15604 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15605 	if (!msg)
15606 		return;
15607 
15608 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
15609 	if (!hdr)
15610 		goto nla_put_failure;
15611 
15612 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15613 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15614 			      NL80211_ATTR_PAD))
15615 		goto nla_put_failure;
15616 
15617 	genlmsg_end(msg, hdr);
15618 
15619 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15620 	return;
15621 
15622  nla_put_failure:
15623 	nlmsg_free(msg);
15624 }
15625 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
15626 
15627 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
15628 {
15629 	struct wiphy *wiphy = wdev->wiphy;
15630 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15631 	struct sk_buff *msg;
15632 	void *hdr;
15633 
15634 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15635 	if (!msg)
15636 		return;
15637 
15638 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
15639 	if (!hdr)
15640 		goto out;
15641 
15642 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15643 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
15644 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15645 			      NL80211_ATTR_PAD))
15646 		goto out;
15647 
15648 	genlmsg_end(msg, hdr);
15649 
15650 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
15651 				NL80211_MCGRP_MLME, GFP_KERNEL);
15652 	return;
15653  out:
15654 	nlmsg_free(msg);
15655 }
15656 
15657 int cfg80211_external_auth_request(struct net_device *dev,
15658 				   struct cfg80211_external_auth_params *params,
15659 				   gfp_t gfp)
15660 {
15661 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15662 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15663 	struct sk_buff *msg;
15664 	void *hdr;
15665 
15666 	if (!wdev->conn_owner_nlportid)
15667 		return -EINVAL;
15668 
15669 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15670 	if (!msg)
15671 		return -ENOMEM;
15672 
15673 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
15674 	if (!hdr)
15675 		goto nla_put_failure;
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_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
15680 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
15681 			params->action) ||
15682 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
15683 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
15684 		    params->ssid.ssid))
15685 		goto nla_put_failure;
15686 
15687 	genlmsg_end(msg, hdr);
15688 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
15689 			wdev->conn_owner_nlportid);
15690 	return 0;
15691 
15692  nla_put_failure:
15693 	nlmsg_free(msg);
15694 	return -ENOBUFS;
15695 }
15696 EXPORT_SYMBOL(cfg80211_external_auth_request);
15697 
15698 /* initialisation/exit functions */
15699 
15700 int __init nl80211_init(void)
15701 {
15702 	int err;
15703 
15704 	err = genl_register_family(&nl80211_fam);
15705 	if (err)
15706 		return err;
15707 
15708 	err = netlink_register_notifier(&nl80211_netlink_notifier);
15709 	if (err)
15710 		goto err_out;
15711 
15712 	return 0;
15713  err_out:
15714 	genl_unregister_family(&nl80211_fam);
15715 	return err;
15716 }
15717 
15718 void nl80211_exit(void)
15719 {
15720 	netlink_unregister_notifier(&nl80211_netlink_notifier);
15721 	genl_unregister_family(&nl80211_fam);
15722 }
15723