xref: /openbmc/linux/net/wireless/nl80211.c (revision 82e6fdd6)
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_PRIVACY] = { .type = NLA_FLAG },
291 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
292 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
293 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
294 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
295 	[NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
296 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
297 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
298 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
299 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
300 				 .len = IEEE80211_MAX_DATA_LEN },
301 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
302 	[NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
303 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
304 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
305 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
306 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
307 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
308 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
309 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
310 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
311 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
312 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
313 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
314 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
315 	[NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
316 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
317 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
318 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
319 	[NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
320 	[NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
321 					 .len = IEEE80211_MAX_DATA_LEN },
322 	[NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
323 					 .len = IEEE80211_MAX_DATA_LEN },
324 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
325 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
326 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
327 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
328 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
329 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
330 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
331 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
332 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
333 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
334 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
335 				      .len = IEEE80211_MAX_DATA_LEN },
336 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
337 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
338 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
339 		.len = NL80211_HT_CAPABILITY_LEN
340 	},
341 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
342 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
343 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
344 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
345 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
346 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
347 	[NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
348 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
349 	[NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
350 	[NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
351 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
352 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
353 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
354 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
355 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
356 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
357 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
358 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
359 		.len = NL80211_VHT_CAPABILITY_LEN,
360 	},
361 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
362 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
363 				  .len = IEEE80211_MAX_DATA_LEN },
364 	[NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
365 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
366 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
367 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
368 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
369 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
370 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
371 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
372 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
373 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
374 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
375 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
376 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
377 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
378 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
379 	[NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
380 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
381 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
382 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
383 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
384 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
385 	[NL80211_ATTR_TSID] = { .type = NLA_U8 },
386 	[NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
387 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
388 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
389 	[NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
390 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
391 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
392 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
393 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
394 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
395 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
396 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] = { .type = NLA_U8 },
397 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
398 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
399 	},
400 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = { .len = ETH_ALEN },
401 	[NL80211_ATTR_NAN_MASTER_PREF] = { .type = NLA_U8 },
402 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
403 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
404 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
405 				    .len = FILS_MAX_KEK_LEN },
406 	[NL80211_ATTR_FILS_NONCES] = { .len = 2 * FILS_NONCE_LEN },
407 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
408 	[NL80211_ATTR_BSSID] = { .len = ETH_ALEN },
409 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
410 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
411 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
412 	},
413 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
414 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
415 					     .len = FILS_ERP_MAX_USERNAME_LEN },
416 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
417 					  .len = FILS_ERP_MAX_REALM_LEN },
418 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
419 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
420 					.len = FILS_ERP_MAX_RRK_LEN },
421 	[NL80211_ATTR_FILS_CACHE_ID] = { .len = 2 },
422 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
423 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
424 };
425 
426 /* policy for the key attributes */
427 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
428 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
429 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
430 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
431 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
432 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
433 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
434 	[NL80211_KEY_TYPE] = { .type = NLA_U32 },
435 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
436 };
437 
438 /* policy for the key default flags */
439 static const struct nla_policy
440 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
441 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
442 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
443 };
444 
445 #ifdef CONFIG_PM
446 /* policy for WoWLAN attributes */
447 static const struct nla_policy
448 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
449 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
450 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
451 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
452 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
453 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
454 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
455 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
456 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
457 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
458 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
459 };
460 
461 static const struct nla_policy
462 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
463 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
464 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
465 	[NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
466 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
467 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
468 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
469 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
470 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
471 	},
472 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
473 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
474 	},
475 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
476 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
477 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
478 };
479 #endif /* CONFIG_PM */
480 
481 /* policy for coalesce rule attributes */
482 static const struct nla_policy
483 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
484 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
485 	[NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
486 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
487 };
488 
489 /* policy for GTK rekey offload attributes */
490 static const struct nla_policy
491 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
492 	[NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
493 	[NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
494 	[NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
495 };
496 
497 static const struct nla_policy
498 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
499 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
500 						 .len = IEEE80211_MAX_SSID_LEN },
501 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = { .len = ETH_ALEN },
502 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
503 };
504 
505 static const struct nla_policy
506 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
507 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
508 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
509 };
510 
511 static const struct nla_policy
512 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
513 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
514 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
515 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
516 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
517 	},
518 };
519 
520 /* policy for NAN function attributes */
521 static const struct nla_policy
522 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
523 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
524 	[NL80211_NAN_FUNC_SERVICE_ID] = {
525 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
526 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
527 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
528 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
529 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
530 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
531 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { .len = ETH_ALEN },
532 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
533 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
534 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
535 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
536 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
537 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
538 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
539 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
540 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
541 };
542 
543 /* policy for Service Response Filter attributes */
544 static const struct nla_policy
545 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
546 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
547 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
548 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
549 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
550 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
551 };
552 
553 /* policy for packet pattern attributes */
554 static const struct nla_policy
555 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
556 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
557 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
558 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
559 };
560 
561 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
562 				     struct netlink_callback *cb,
563 				     struct cfg80211_registered_device **rdev,
564 				     struct wireless_dev **wdev)
565 {
566 	int err;
567 
568 	if (!cb->args[0]) {
569 		err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
570 				  genl_family_attrbuf(&nl80211_fam),
571 				  nl80211_fam.maxattr, nl80211_policy, NULL);
572 		if (err)
573 			return err;
574 
575 		*wdev = __cfg80211_wdev_from_attrs(
576 					sock_net(skb->sk),
577 					genl_family_attrbuf(&nl80211_fam));
578 		if (IS_ERR(*wdev))
579 			return PTR_ERR(*wdev);
580 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
581 		/* 0 is the first index - add 1 to parse only once */
582 		cb->args[0] = (*rdev)->wiphy_idx + 1;
583 		cb->args[1] = (*wdev)->identifier;
584 	} else {
585 		/* subtract the 1 again here */
586 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
587 		struct wireless_dev *tmp;
588 
589 		if (!wiphy)
590 			return -ENODEV;
591 		*rdev = wiphy_to_rdev(wiphy);
592 		*wdev = NULL;
593 
594 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
595 			if (tmp->identifier == cb->args[1]) {
596 				*wdev = tmp;
597 				break;
598 			}
599 		}
600 
601 		if (!*wdev)
602 			return -ENODEV;
603 	}
604 
605 	return 0;
606 }
607 
608 /* IE validation */
609 static bool is_valid_ie_attr(const struct nlattr *attr)
610 {
611 	const u8 *pos;
612 	int len;
613 
614 	if (!attr)
615 		return true;
616 
617 	pos = nla_data(attr);
618 	len = nla_len(attr);
619 
620 	while (len) {
621 		u8 elemlen;
622 
623 		if (len < 2)
624 			return false;
625 		len -= 2;
626 
627 		elemlen = pos[1];
628 		if (elemlen > len)
629 			return false;
630 
631 		len -= elemlen;
632 		pos += 2 + elemlen;
633 	}
634 
635 	return true;
636 }
637 
638 /* message building helper */
639 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
640 				   int flags, u8 cmd)
641 {
642 	/* since there is no private header just add the generic one */
643 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
644 }
645 
646 static int nl80211_msg_put_channel(struct sk_buff *msg,
647 				   struct ieee80211_channel *chan,
648 				   bool large)
649 {
650 	/* Some channels must be completely excluded from the
651 	 * list to protect old user-space tools from breaking
652 	 */
653 	if (!large && chan->flags &
654 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
655 		return 0;
656 
657 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
658 			chan->center_freq))
659 		goto nla_put_failure;
660 
661 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
662 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
663 		goto nla_put_failure;
664 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
665 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
666 			goto nla_put_failure;
667 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
668 			goto nla_put_failure;
669 	}
670 	if (chan->flags & IEEE80211_CHAN_RADAR) {
671 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
672 			goto nla_put_failure;
673 		if (large) {
674 			u32 time;
675 
676 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
677 
678 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
679 					chan->dfs_state))
680 				goto nla_put_failure;
681 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
682 					time))
683 				goto nla_put_failure;
684 			if (nla_put_u32(msg,
685 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
686 					chan->dfs_cac_ms))
687 				goto nla_put_failure;
688 		}
689 	}
690 
691 	if (large) {
692 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
693 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
694 			goto nla_put_failure;
695 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
696 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
697 			goto nla_put_failure;
698 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
699 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
700 			goto nla_put_failure;
701 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
702 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
703 			goto nla_put_failure;
704 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
705 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
706 			goto nla_put_failure;
707 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
708 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
709 			goto nla_put_failure;
710 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
711 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
712 			goto nla_put_failure;
713 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
714 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
715 			goto nla_put_failure;
716 	}
717 
718 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
719 			DBM_TO_MBM(chan->max_power)))
720 		goto nla_put_failure;
721 
722 	return 0;
723 
724  nla_put_failure:
725 	return -ENOBUFS;
726 }
727 
728 /* netlink command implementations */
729 
730 struct key_parse {
731 	struct key_params p;
732 	int idx;
733 	int type;
734 	bool def, defmgmt;
735 	bool def_uni, def_multi;
736 };
737 
738 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
739 				 struct key_parse *k)
740 {
741 	struct nlattr *tb[NL80211_KEY_MAX + 1];
742 	int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
743 				   nl80211_key_policy, info->extack);
744 	if (err)
745 		return err;
746 
747 	k->def = !!tb[NL80211_KEY_DEFAULT];
748 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
749 
750 	if (k->def) {
751 		k->def_uni = true;
752 		k->def_multi = true;
753 	}
754 	if (k->defmgmt)
755 		k->def_multi = true;
756 
757 	if (tb[NL80211_KEY_IDX])
758 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
759 
760 	if (tb[NL80211_KEY_DATA]) {
761 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
762 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
763 	}
764 
765 	if (tb[NL80211_KEY_SEQ]) {
766 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
767 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
768 	}
769 
770 	if (tb[NL80211_KEY_CIPHER])
771 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
772 
773 	if (tb[NL80211_KEY_TYPE]) {
774 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
775 		if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
776 			return genl_err_attr(info, -EINVAL,
777 					     tb[NL80211_KEY_TYPE]);
778 	}
779 
780 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
781 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
782 
783 		err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
784 				       tb[NL80211_KEY_DEFAULT_TYPES],
785 				       nl80211_key_default_policy,
786 				       info->extack);
787 		if (err)
788 			return err;
789 
790 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
791 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
792 	}
793 
794 	return 0;
795 }
796 
797 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
798 {
799 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
800 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
801 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
802 	}
803 
804 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
805 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
806 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
807 	}
808 
809 	if (info->attrs[NL80211_ATTR_KEY_IDX])
810 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
811 
812 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
813 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
814 
815 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
816 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
817 
818 	if (k->def) {
819 		k->def_uni = true;
820 		k->def_multi = true;
821 	}
822 	if (k->defmgmt)
823 		k->def_multi = true;
824 
825 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
826 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
827 		if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES) {
828 			GENL_SET_ERR_MSG(info, "key type out of range");
829 			return -EINVAL;
830 		}
831 	}
832 
833 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
834 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
835 		int err = nla_parse_nested(kdt,
836 					   NUM_NL80211_KEY_DEFAULT_TYPES - 1,
837 					   info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
838 					   nl80211_key_default_policy,
839 					   info->extack);
840 		if (err)
841 			return err;
842 
843 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
844 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
845 	}
846 
847 	return 0;
848 }
849 
850 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
851 {
852 	int err;
853 
854 	memset(k, 0, sizeof(*k));
855 	k->idx = -1;
856 	k->type = -1;
857 
858 	if (info->attrs[NL80211_ATTR_KEY])
859 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
860 	else
861 		err = nl80211_parse_key_old(info, k);
862 
863 	if (err)
864 		return err;
865 
866 	if (k->def && k->defmgmt) {
867 		GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
868 		return -EINVAL;
869 	}
870 
871 	if (k->defmgmt) {
872 		if (k->def_uni || !k->def_multi) {
873 			GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
874 			return -EINVAL;
875 		}
876 	}
877 
878 	if (k->idx != -1) {
879 		if (k->defmgmt) {
880 			if (k->idx < 4 || k->idx > 5) {
881 				GENL_SET_ERR_MSG(info,
882 						 "defmgmt key idx not 4 or 5");
883 				return -EINVAL;
884 			}
885 		} else if (k->def) {
886 			if (k->idx < 0 || k->idx > 3) {
887 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
888 				return -EINVAL;
889 			}
890 		} else {
891 			if (k->idx < 0 || k->idx > 5) {
892 				GENL_SET_ERR_MSG(info, "key idx not 0-5");
893 				return -EINVAL;
894 			}
895 		}
896 	}
897 
898 	return 0;
899 }
900 
901 static struct cfg80211_cached_keys *
902 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
903 		       struct genl_info *info, bool *no_ht)
904 {
905 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
906 	struct key_parse parse;
907 	struct nlattr *key;
908 	struct cfg80211_cached_keys *result;
909 	int rem, err, def = 0;
910 	bool have_key = false;
911 
912 	nla_for_each_nested(key, keys, rem) {
913 		have_key = true;
914 		break;
915 	}
916 
917 	if (!have_key)
918 		return NULL;
919 
920 	result = kzalloc(sizeof(*result), GFP_KERNEL);
921 	if (!result)
922 		return ERR_PTR(-ENOMEM);
923 
924 	result->def = -1;
925 
926 	nla_for_each_nested(key, keys, rem) {
927 		memset(&parse, 0, sizeof(parse));
928 		parse.idx = -1;
929 
930 		err = nl80211_parse_key_new(info, key, &parse);
931 		if (err)
932 			goto error;
933 		err = -EINVAL;
934 		if (!parse.p.key)
935 			goto error;
936 		if (parse.idx < 0 || parse.idx > 3) {
937 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
938 			goto error;
939 		}
940 		if (parse.def) {
941 			if (def) {
942 				GENL_SET_ERR_MSG(info,
943 						 "only one key can be default");
944 				goto error;
945 			}
946 			def = 1;
947 			result->def = parse.idx;
948 			if (!parse.def_uni || !parse.def_multi)
949 				goto error;
950 		} else if (parse.defmgmt)
951 			goto error;
952 		err = cfg80211_validate_key_settings(rdev, &parse.p,
953 						     parse.idx, false, NULL);
954 		if (err)
955 			goto error;
956 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
957 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
958 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
959 			err = -EINVAL;
960 			goto error;
961 		}
962 		result->params[parse.idx].cipher = parse.p.cipher;
963 		result->params[parse.idx].key_len = parse.p.key_len;
964 		result->params[parse.idx].key = result->data[parse.idx];
965 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
966 
967 		/* must be WEP key if we got here */
968 		if (no_ht)
969 			*no_ht = true;
970 	}
971 
972 	if (result->def < 0) {
973 		err = -EINVAL;
974 		GENL_SET_ERR_MSG(info, "need a default/TX key");
975 		goto error;
976 	}
977 
978 	return result;
979  error:
980 	kfree(result);
981 	return ERR_PTR(err);
982 }
983 
984 static int nl80211_key_allowed(struct wireless_dev *wdev)
985 {
986 	ASSERT_WDEV_LOCK(wdev);
987 
988 	switch (wdev->iftype) {
989 	case NL80211_IFTYPE_AP:
990 	case NL80211_IFTYPE_AP_VLAN:
991 	case NL80211_IFTYPE_P2P_GO:
992 	case NL80211_IFTYPE_MESH_POINT:
993 		break;
994 	case NL80211_IFTYPE_ADHOC:
995 	case NL80211_IFTYPE_STATION:
996 	case NL80211_IFTYPE_P2P_CLIENT:
997 		if (!wdev->current_bss)
998 			return -ENOLINK;
999 		break;
1000 	case NL80211_IFTYPE_UNSPECIFIED:
1001 	case NL80211_IFTYPE_OCB:
1002 	case NL80211_IFTYPE_MONITOR:
1003 	case NL80211_IFTYPE_NAN:
1004 	case NL80211_IFTYPE_P2P_DEVICE:
1005 	case NL80211_IFTYPE_WDS:
1006 	case NUM_NL80211_IFTYPES:
1007 		return -EINVAL;
1008 	}
1009 
1010 	return 0;
1011 }
1012 
1013 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1014 							struct nlattr *tb)
1015 {
1016 	struct ieee80211_channel *chan;
1017 
1018 	if (tb == NULL)
1019 		return NULL;
1020 	chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1021 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1022 		return NULL;
1023 	return chan;
1024 }
1025 
1026 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1027 {
1028 	struct nlattr *nl_modes = nla_nest_start(msg, attr);
1029 	int i;
1030 
1031 	if (!nl_modes)
1032 		goto nla_put_failure;
1033 
1034 	i = 0;
1035 	while (ifmodes) {
1036 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1037 			goto nla_put_failure;
1038 		ifmodes >>= 1;
1039 		i++;
1040 	}
1041 
1042 	nla_nest_end(msg, nl_modes);
1043 	return 0;
1044 
1045 nla_put_failure:
1046 	return -ENOBUFS;
1047 }
1048 
1049 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1050 					  struct sk_buff *msg,
1051 					  bool large)
1052 {
1053 	struct nlattr *nl_combis;
1054 	int i, j;
1055 
1056 	nl_combis = nla_nest_start(msg,
1057 				NL80211_ATTR_INTERFACE_COMBINATIONS);
1058 	if (!nl_combis)
1059 		goto nla_put_failure;
1060 
1061 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1062 		const struct ieee80211_iface_combination *c;
1063 		struct nlattr *nl_combi, *nl_limits;
1064 
1065 		c = &wiphy->iface_combinations[i];
1066 
1067 		nl_combi = nla_nest_start(msg, i + 1);
1068 		if (!nl_combi)
1069 			goto nla_put_failure;
1070 
1071 		nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
1072 		if (!nl_limits)
1073 			goto nla_put_failure;
1074 
1075 		for (j = 0; j < c->n_limits; j++) {
1076 			struct nlattr *nl_limit;
1077 
1078 			nl_limit = nla_nest_start(msg, j + 1);
1079 			if (!nl_limit)
1080 				goto nla_put_failure;
1081 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1082 					c->limits[j].max))
1083 				goto nla_put_failure;
1084 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1085 						c->limits[j].types))
1086 				goto nla_put_failure;
1087 			nla_nest_end(msg, nl_limit);
1088 		}
1089 
1090 		nla_nest_end(msg, nl_limits);
1091 
1092 		if (c->beacon_int_infra_match &&
1093 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1094 			goto nla_put_failure;
1095 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1096 				c->num_different_channels) ||
1097 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1098 				c->max_interfaces))
1099 			goto nla_put_failure;
1100 		if (large &&
1101 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1102 				c->radar_detect_widths) ||
1103 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1104 				c->radar_detect_regions)))
1105 			goto nla_put_failure;
1106 		if (c->beacon_int_min_gcd &&
1107 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1108 				c->beacon_int_min_gcd))
1109 			goto nla_put_failure;
1110 
1111 		nla_nest_end(msg, nl_combi);
1112 	}
1113 
1114 	nla_nest_end(msg, nl_combis);
1115 
1116 	return 0;
1117 nla_put_failure:
1118 	return -ENOBUFS;
1119 }
1120 
1121 #ifdef CONFIG_PM
1122 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1123 					struct sk_buff *msg)
1124 {
1125 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1126 	struct nlattr *nl_tcp;
1127 
1128 	if (!tcp)
1129 		return 0;
1130 
1131 	nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1132 	if (!nl_tcp)
1133 		return -ENOBUFS;
1134 
1135 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1136 			tcp->data_payload_max))
1137 		return -ENOBUFS;
1138 
1139 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1140 			tcp->data_payload_max))
1141 		return -ENOBUFS;
1142 
1143 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1144 		return -ENOBUFS;
1145 
1146 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1147 				sizeof(*tcp->tok), tcp->tok))
1148 		return -ENOBUFS;
1149 
1150 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1151 			tcp->data_interval_max))
1152 		return -ENOBUFS;
1153 
1154 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1155 			tcp->wake_payload_max))
1156 		return -ENOBUFS;
1157 
1158 	nla_nest_end(msg, nl_tcp);
1159 	return 0;
1160 }
1161 
1162 static int nl80211_send_wowlan(struct sk_buff *msg,
1163 			       struct cfg80211_registered_device *rdev,
1164 			       bool large)
1165 {
1166 	struct nlattr *nl_wowlan;
1167 
1168 	if (!rdev->wiphy.wowlan)
1169 		return 0;
1170 
1171 	nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1172 	if (!nl_wowlan)
1173 		return -ENOBUFS;
1174 
1175 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1176 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1177 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1178 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1179 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1180 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1181 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1182 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1183 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1184 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1185 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1186 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1187 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1188 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1189 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1190 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1191 		return -ENOBUFS;
1192 
1193 	if (rdev->wiphy.wowlan->n_patterns) {
1194 		struct nl80211_pattern_support pat = {
1195 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1196 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1197 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1198 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1199 		};
1200 
1201 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1202 			    sizeof(pat), &pat))
1203 			return -ENOBUFS;
1204 	}
1205 
1206 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1207 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1208 			rdev->wiphy.wowlan->max_nd_match_sets))
1209 		return -ENOBUFS;
1210 
1211 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1212 		return -ENOBUFS;
1213 
1214 	nla_nest_end(msg, nl_wowlan);
1215 
1216 	return 0;
1217 }
1218 #endif
1219 
1220 static int nl80211_send_coalesce(struct sk_buff *msg,
1221 				 struct cfg80211_registered_device *rdev)
1222 {
1223 	struct nl80211_coalesce_rule_support rule;
1224 
1225 	if (!rdev->wiphy.coalesce)
1226 		return 0;
1227 
1228 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1229 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1230 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1231 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1232 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1233 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1234 
1235 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1236 		return -ENOBUFS;
1237 
1238 	return 0;
1239 }
1240 
1241 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1242 				      struct ieee80211_supported_band *sband)
1243 {
1244 	struct nlattr *nl_rates, *nl_rate;
1245 	struct ieee80211_rate *rate;
1246 	int i;
1247 
1248 	/* add HT info */
1249 	if (sband->ht_cap.ht_supported &&
1250 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1251 		     sizeof(sband->ht_cap.mcs),
1252 		     &sband->ht_cap.mcs) ||
1253 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1254 			 sband->ht_cap.cap) ||
1255 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1256 			sband->ht_cap.ampdu_factor) ||
1257 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1258 			sband->ht_cap.ampdu_density)))
1259 		return -ENOBUFS;
1260 
1261 	/* add VHT info */
1262 	if (sband->vht_cap.vht_supported &&
1263 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1264 		     sizeof(sband->vht_cap.vht_mcs),
1265 		     &sband->vht_cap.vht_mcs) ||
1266 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1267 			 sband->vht_cap.cap)))
1268 		return -ENOBUFS;
1269 
1270 	/* add bitrates */
1271 	nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1272 	if (!nl_rates)
1273 		return -ENOBUFS;
1274 
1275 	for (i = 0; i < sband->n_bitrates; i++) {
1276 		nl_rate = nla_nest_start(msg, i);
1277 		if (!nl_rate)
1278 			return -ENOBUFS;
1279 
1280 		rate = &sband->bitrates[i];
1281 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1282 				rate->bitrate))
1283 			return -ENOBUFS;
1284 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1285 		    nla_put_flag(msg,
1286 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1287 			return -ENOBUFS;
1288 
1289 		nla_nest_end(msg, nl_rate);
1290 	}
1291 
1292 	nla_nest_end(msg, nl_rates);
1293 
1294 	return 0;
1295 }
1296 
1297 static int
1298 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1299 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1300 {
1301 	u16 stypes;
1302 	struct nlattr *nl_ftypes, *nl_ifs;
1303 	enum nl80211_iftype ift;
1304 	int i;
1305 
1306 	if (!mgmt_stypes)
1307 		return 0;
1308 
1309 	nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1310 	if (!nl_ifs)
1311 		return -ENOBUFS;
1312 
1313 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1314 		nl_ftypes = nla_nest_start(msg, ift);
1315 		if (!nl_ftypes)
1316 			return -ENOBUFS;
1317 		i = 0;
1318 		stypes = mgmt_stypes[ift].tx;
1319 		while (stypes) {
1320 			if ((stypes & 1) &&
1321 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1322 					(i << 4) | IEEE80211_FTYPE_MGMT))
1323 				return -ENOBUFS;
1324 			stypes >>= 1;
1325 			i++;
1326 		}
1327 		nla_nest_end(msg, nl_ftypes);
1328 	}
1329 
1330 	nla_nest_end(msg, nl_ifs);
1331 
1332 	nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1333 	if (!nl_ifs)
1334 		return -ENOBUFS;
1335 
1336 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1337 		nl_ftypes = nla_nest_start(msg, ift);
1338 		if (!nl_ftypes)
1339 			return -ENOBUFS;
1340 		i = 0;
1341 		stypes = mgmt_stypes[ift].rx;
1342 		while (stypes) {
1343 			if ((stypes & 1) &&
1344 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1345 					(i << 4) | IEEE80211_FTYPE_MGMT))
1346 				return -ENOBUFS;
1347 			stypes >>= 1;
1348 			i++;
1349 		}
1350 		nla_nest_end(msg, nl_ftypes);
1351 	}
1352 	nla_nest_end(msg, nl_ifs);
1353 
1354 	return 0;
1355 }
1356 
1357 #define CMD(op, n)							\
1358 	 do {								\
1359 		if (rdev->ops->op) {					\
1360 			i++;						\
1361 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1362 				goto nla_put_failure;			\
1363 		}							\
1364 	} while (0)
1365 
1366 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1367 					struct sk_buff *msg)
1368 {
1369 	int i = 0;
1370 
1371 	/*
1372 	 * do *NOT* add anything into this function, new things need to be
1373 	 * advertised only to new versions of userspace that can deal with
1374 	 * the split (and they can't possibly care about new features...
1375 	 */
1376 	CMD(add_virtual_intf, NEW_INTERFACE);
1377 	CMD(change_virtual_intf, SET_INTERFACE);
1378 	CMD(add_key, NEW_KEY);
1379 	CMD(start_ap, START_AP);
1380 	CMD(add_station, NEW_STATION);
1381 	CMD(add_mpath, NEW_MPATH);
1382 	CMD(update_mesh_config, SET_MESH_CONFIG);
1383 	CMD(change_bss, SET_BSS);
1384 	CMD(auth, AUTHENTICATE);
1385 	CMD(assoc, ASSOCIATE);
1386 	CMD(deauth, DEAUTHENTICATE);
1387 	CMD(disassoc, DISASSOCIATE);
1388 	CMD(join_ibss, JOIN_IBSS);
1389 	CMD(join_mesh, JOIN_MESH);
1390 	CMD(set_pmksa, SET_PMKSA);
1391 	CMD(del_pmksa, DEL_PMKSA);
1392 	CMD(flush_pmksa, FLUSH_PMKSA);
1393 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1394 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1395 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1396 	CMD(mgmt_tx, FRAME);
1397 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1398 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1399 		i++;
1400 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1401 			goto nla_put_failure;
1402 	}
1403 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1404 	    rdev->ops->join_mesh) {
1405 		i++;
1406 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1407 			goto nla_put_failure;
1408 	}
1409 	CMD(set_wds_peer, SET_WDS_PEER);
1410 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1411 		CMD(tdls_mgmt, TDLS_MGMT);
1412 		CMD(tdls_oper, TDLS_OPER);
1413 	}
1414 	if (rdev->wiphy.max_sched_scan_reqs)
1415 		CMD(sched_scan_start, START_SCHED_SCAN);
1416 	CMD(probe_client, PROBE_CLIENT);
1417 	CMD(set_noack_map, SET_NOACK_MAP);
1418 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1419 		i++;
1420 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1421 			goto nla_put_failure;
1422 	}
1423 	CMD(start_p2p_device, START_P2P_DEVICE);
1424 	CMD(set_mcast_rate, SET_MCAST_RATE);
1425 #ifdef CONFIG_NL80211_TESTMODE
1426 	CMD(testmode_cmd, TESTMODE);
1427 #endif
1428 
1429 	if (rdev->ops->connect || rdev->ops->auth) {
1430 		i++;
1431 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1432 			goto nla_put_failure;
1433 	}
1434 
1435 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1436 		i++;
1437 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1438 			goto nla_put_failure;
1439 	}
1440 
1441 	return i;
1442  nla_put_failure:
1443 	return -ENOBUFS;
1444 }
1445 
1446 struct nl80211_dump_wiphy_state {
1447 	s64 filter_wiphy;
1448 	long start;
1449 	long split_start, band_start, chan_start, capa_start;
1450 	bool split;
1451 };
1452 
1453 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1454 			      enum nl80211_commands cmd,
1455 			      struct sk_buff *msg, u32 portid, u32 seq,
1456 			      int flags, struct nl80211_dump_wiphy_state *state)
1457 {
1458 	void *hdr;
1459 	struct nlattr *nl_bands, *nl_band;
1460 	struct nlattr *nl_freqs, *nl_freq;
1461 	struct nlattr *nl_cmds;
1462 	enum nl80211_band band;
1463 	struct ieee80211_channel *chan;
1464 	int i;
1465 	const struct ieee80211_txrx_stypes *mgmt_stypes =
1466 				rdev->wiphy.mgmt_stypes;
1467 	u32 features;
1468 
1469 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1470 	if (!hdr)
1471 		return -ENOBUFS;
1472 
1473 	if (WARN_ON(!state))
1474 		return -EINVAL;
1475 
1476 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1477 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1478 			   wiphy_name(&rdev->wiphy)) ||
1479 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
1480 			cfg80211_rdev_list_generation))
1481 		goto nla_put_failure;
1482 
1483 	if (cmd != NL80211_CMD_NEW_WIPHY)
1484 		goto finish;
1485 
1486 	switch (state->split_start) {
1487 	case 0:
1488 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1489 			       rdev->wiphy.retry_short) ||
1490 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1491 			       rdev->wiphy.retry_long) ||
1492 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1493 				rdev->wiphy.frag_threshold) ||
1494 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1495 				rdev->wiphy.rts_threshold) ||
1496 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1497 			       rdev->wiphy.coverage_class) ||
1498 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1499 			       rdev->wiphy.max_scan_ssids) ||
1500 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1501 			       rdev->wiphy.max_sched_scan_ssids) ||
1502 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1503 				rdev->wiphy.max_scan_ie_len) ||
1504 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1505 				rdev->wiphy.max_sched_scan_ie_len) ||
1506 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1507 			       rdev->wiphy.max_match_sets) ||
1508 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1509 				rdev->wiphy.max_sched_scan_plans) ||
1510 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1511 				rdev->wiphy.max_sched_scan_plan_interval) ||
1512 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1513 				rdev->wiphy.max_sched_scan_plan_iterations))
1514 			goto nla_put_failure;
1515 
1516 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1517 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1518 			goto nla_put_failure;
1519 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1520 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1521 			goto nla_put_failure;
1522 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1523 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1524 			goto nla_put_failure;
1525 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1526 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1527 			goto nla_put_failure;
1528 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1529 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1530 			goto nla_put_failure;
1531 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1532 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1533 			goto nla_put_failure;
1534 		state->split_start++;
1535 		if (state->split)
1536 			break;
1537 	case 1:
1538 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1539 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
1540 			    rdev->wiphy.cipher_suites))
1541 			goto nla_put_failure;
1542 
1543 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1544 			       rdev->wiphy.max_num_pmkids))
1545 			goto nla_put_failure;
1546 
1547 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1548 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1549 			goto nla_put_failure;
1550 
1551 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1552 				rdev->wiphy.available_antennas_tx) ||
1553 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1554 				rdev->wiphy.available_antennas_rx))
1555 			goto nla_put_failure;
1556 
1557 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1558 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1559 				rdev->wiphy.probe_resp_offload))
1560 			goto nla_put_failure;
1561 
1562 		if ((rdev->wiphy.available_antennas_tx ||
1563 		     rdev->wiphy.available_antennas_rx) &&
1564 		    rdev->ops->get_antenna) {
1565 			u32 tx_ant = 0, rx_ant = 0;
1566 			int res;
1567 
1568 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1569 			if (!res) {
1570 				if (nla_put_u32(msg,
1571 						NL80211_ATTR_WIPHY_ANTENNA_TX,
1572 						tx_ant) ||
1573 				    nla_put_u32(msg,
1574 						NL80211_ATTR_WIPHY_ANTENNA_RX,
1575 						rx_ant))
1576 					goto nla_put_failure;
1577 			}
1578 		}
1579 
1580 		state->split_start++;
1581 		if (state->split)
1582 			break;
1583 	case 2:
1584 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1585 					rdev->wiphy.interface_modes))
1586 				goto nla_put_failure;
1587 		state->split_start++;
1588 		if (state->split)
1589 			break;
1590 	case 3:
1591 		nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1592 		if (!nl_bands)
1593 			goto nla_put_failure;
1594 
1595 		for (band = state->band_start;
1596 		     band < NUM_NL80211_BANDS; band++) {
1597 			struct ieee80211_supported_band *sband;
1598 
1599 			sband = rdev->wiphy.bands[band];
1600 
1601 			if (!sband)
1602 				continue;
1603 
1604 			nl_band = nla_nest_start(msg, band);
1605 			if (!nl_band)
1606 				goto nla_put_failure;
1607 
1608 			switch (state->chan_start) {
1609 			case 0:
1610 				if (nl80211_send_band_rateinfo(msg, sband))
1611 					goto nla_put_failure;
1612 				state->chan_start++;
1613 				if (state->split)
1614 					break;
1615 			default:
1616 				/* add frequencies */
1617 				nl_freqs = nla_nest_start(
1618 					msg, NL80211_BAND_ATTR_FREQS);
1619 				if (!nl_freqs)
1620 					goto nla_put_failure;
1621 
1622 				for (i = state->chan_start - 1;
1623 				     i < sband->n_channels;
1624 				     i++) {
1625 					nl_freq = nla_nest_start(msg, i);
1626 					if (!nl_freq)
1627 						goto nla_put_failure;
1628 
1629 					chan = &sband->channels[i];
1630 
1631 					if (nl80211_msg_put_channel(
1632 							msg, chan,
1633 							state->split))
1634 						goto nla_put_failure;
1635 
1636 					nla_nest_end(msg, nl_freq);
1637 					if (state->split)
1638 						break;
1639 				}
1640 				if (i < sband->n_channels)
1641 					state->chan_start = i + 2;
1642 				else
1643 					state->chan_start = 0;
1644 				nla_nest_end(msg, nl_freqs);
1645 			}
1646 
1647 			nla_nest_end(msg, nl_band);
1648 
1649 			if (state->split) {
1650 				/* start again here */
1651 				if (state->chan_start)
1652 					band--;
1653 				break;
1654 			}
1655 		}
1656 		nla_nest_end(msg, nl_bands);
1657 
1658 		if (band < NUM_NL80211_BANDS)
1659 			state->band_start = band + 1;
1660 		else
1661 			state->band_start = 0;
1662 
1663 		/* if bands & channels are done, continue outside */
1664 		if (state->band_start == 0 && state->chan_start == 0)
1665 			state->split_start++;
1666 		if (state->split)
1667 			break;
1668 	case 4:
1669 		nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1670 		if (!nl_cmds)
1671 			goto nla_put_failure;
1672 
1673 		i = nl80211_add_commands_unsplit(rdev, msg);
1674 		if (i < 0)
1675 			goto nla_put_failure;
1676 		if (state->split) {
1677 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
1678 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1679 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1680 				CMD(channel_switch, CHANNEL_SWITCH);
1681 			CMD(set_qos_map, SET_QOS_MAP);
1682 			if (rdev->wiphy.features &
1683 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1684 				CMD(add_tx_ts, ADD_TX_TS);
1685 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
1686 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
1687 		}
1688 #undef CMD
1689 
1690 		nla_nest_end(msg, nl_cmds);
1691 		state->split_start++;
1692 		if (state->split)
1693 			break;
1694 	case 5:
1695 		if (rdev->ops->remain_on_channel &&
1696 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1697 		    nla_put_u32(msg,
1698 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1699 				rdev->wiphy.max_remain_on_channel_duration))
1700 			goto nla_put_failure;
1701 
1702 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1703 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1704 			goto nla_put_failure;
1705 
1706 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1707 			goto nla_put_failure;
1708 		state->split_start++;
1709 		if (state->split)
1710 			break;
1711 	case 6:
1712 #ifdef CONFIG_PM
1713 		if (nl80211_send_wowlan(msg, rdev, state->split))
1714 			goto nla_put_failure;
1715 		state->split_start++;
1716 		if (state->split)
1717 			break;
1718 #else
1719 		state->split_start++;
1720 #endif
1721 	case 7:
1722 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1723 					rdev->wiphy.software_iftypes))
1724 			goto nla_put_failure;
1725 
1726 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1727 						   state->split))
1728 			goto nla_put_failure;
1729 
1730 		state->split_start++;
1731 		if (state->split)
1732 			break;
1733 	case 8:
1734 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1735 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1736 				rdev->wiphy.ap_sme_capa))
1737 			goto nla_put_failure;
1738 
1739 		features = rdev->wiphy.features;
1740 		/*
1741 		 * We can only add the per-channel limit information if the
1742 		 * dump is split, otherwise it makes it too big. Therefore
1743 		 * only advertise it in that case.
1744 		 */
1745 		if (state->split)
1746 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1747 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1748 			goto nla_put_failure;
1749 
1750 		if (rdev->wiphy.ht_capa_mod_mask &&
1751 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1752 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
1753 			    rdev->wiphy.ht_capa_mod_mask))
1754 			goto nla_put_failure;
1755 
1756 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1757 		    rdev->wiphy.max_acl_mac_addrs &&
1758 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1759 				rdev->wiphy.max_acl_mac_addrs))
1760 			goto nla_put_failure;
1761 
1762 		/*
1763 		 * Any information below this point is only available to
1764 		 * applications that can deal with it being split. This
1765 		 * helps ensure that newly added capabilities don't break
1766 		 * older tools by overrunning their buffers.
1767 		 *
1768 		 * We still increment split_start so that in the split
1769 		 * case we'll continue with more data in the next round,
1770 		 * but break unconditionally so unsplit data stops here.
1771 		 */
1772 		state->split_start++;
1773 		break;
1774 	case 9:
1775 		if (rdev->wiphy.extended_capabilities &&
1776 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1777 			     rdev->wiphy.extended_capabilities_len,
1778 			     rdev->wiphy.extended_capabilities) ||
1779 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1780 			     rdev->wiphy.extended_capabilities_len,
1781 			     rdev->wiphy.extended_capabilities_mask)))
1782 			goto nla_put_failure;
1783 
1784 		if (rdev->wiphy.vht_capa_mod_mask &&
1785 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1786 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
1787 			    rdev->wiphy.vht_capa_mod_mask))
1788 			goto nla_put_failure;
1789 
1790 		state->split_start++;
1791 		break;
1792 	case 10:
1793 		if (nl80211_send_coalesce(msg, rdev))
1794 			goto nla_put_failure;
1795 
1796 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1797 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1798 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1799 			goto nla_put_failure;
1800 
1801 		if (rdev->wiphy.max_ap_assoc_sta &&
1802 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1803 				rdev->wiphy.max_ap_assoc_sta))
1804 			goto nla_put_failure;
1805 
1806 		state->split_start++;
1807 		break;
1808 	case 11:
1809 		if (rdev->wiphy.n_vendor_commands) {
1810 			const struct nl80211_vendor_cmd_info *info;
1811 			struct nlattr *nested;
1812 
1813 			nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1814 			if (!nested)
1815 				goto nla_put_failure;
1816 
1817 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1818 				info = &rdev->wiphy.vendor_commands[i].info;
1819 				if (nla_put(msg, i + 1, sizeof(*info), info))
1820 					goto nla_put_failure;
1821 			}
1822 			nla_nest_end(msg, nested);
1823 		}
1824 
1825 		if (rdev->wiphy.n_vendor_events) {
1826 			const struct nl80211_vendor_cmd_info *info;
1827 			struct nlattr *nested;
1828 
1829 			nested = nla_nest_start(msg,
1830 						NL80211_ATTR_VENDOR_EVENTS);
1831 			if (!nested)
1832 				goto nla_put_failure;
1833 
1834 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1835 				info = &rdev->wiphy.vendor_events[i];
1836 				if (nla_put(msg, i + 1, sizeof(*info), info))
1837 					goto nla_put_failure;
1838 			}
1839 			nla_nest_end(msg, nested);
1840 		}
1841 		state->split_start++;
1842 		break;
1843 	case 12:
1844 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1845 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1846 			       rdev->wiphy.max_num_csa_counters))
1847 			goto nla_put_failure;
1848 
1849 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1850 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1851 			goto nla_put_failure;
1852 
1853 		if (rdev->wiphy.max_sched_scan_reqs &&
1854 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
1855 				rdev->wiphy.max_sched_scan_reqs))
1856 			goto nla_put_failure;
1857 
1858 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1859 			    sizeof(rdev->wiphy.ext_features),
1860 			    rdev->wiphy.ext_features))
1861 			goto nla_put_failure;
1862 
1863 		if (rdev->wiphy.bss_select_support) {
1864 			struct nlattr *nested;
1865 			u32 bss_select_support = rdev->wiphy.bss_select_support;
1866 
1867 			nested = nla_nest_start(msg, NL80211_ATTR_BSS_SELECT);
1868 			if (!nested)
1869 				goto nla_put_failure;
1870 
1871 			i = 0;
1872 			while (bss_select_support) {
1873 				if ((bss_select_support & 1) &&
1874 				    nla_put_flag(msg, i))
1875 					goto nla_put_failure;
1876 				i++;
1877 				bss_select_support >>= 1;
1878 			}
1879 			nla_nest_end(msg, nested);
1880 		}
1881 
1882 		state->split_start++;
1883 		break;
1884 	case 13:
1885 		if (rdev->wiphy.num_iftype_ext_capab &&
1886 		    rdev->wiphy.iftype_ext_capab) {
1887 			struct nlattr *nested_ext_capab, *nested;
1888 
1889 			nested = nla_nest_start(msg,
1890 						NL80211_ATTR_IFTYPE_EXT_CAPA);
1891 			if (!nested)
1892 				goto nla_put_failure;
1893 
1894 			for (i = state->capa_start;
1895 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
1896 				const struct wiphy_iftype_ext_capab *capab;
1897 
1898 				capab = &rdev->wiphy.iftype_ext_capab[i];
1899 
1900 				nested_ext_capab = nla_nest_start(msg, i);
1901 				if (!nested_ext_capab ||
1902 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1903 						capab->iftype) ||
1904 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
1905 					    capab->extended_capabilities_len,
1906 					    capab->extended_capabilities) ||
1907 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1908 					    capab->extended_capabilities_len,
1909 					    capab->extended_capabilities_mask))
1910 					goto nla_put_failure;
1911 
1912 				nla_nest_end(msg, nested_ext_capab);
1913 				if (state->split)
1914 					break;
1915 			}
1916 			nla_nest_end(msg, nested);
1917 			if (i < rdev->wiphy.num_iftype_ext_capab) {
1918 				state->capa_start = i + 1;
1919 				break;
1920 			}
1921 		}
1922 
1923 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
1924 				rdev->wiphy.nan_supported_bands))
1925 			goto nla_put_failure;
1926 
1927 		/* done */
1928 		state->split_start = 0;
1929 		break;
1930 	}
1931  finish:
1932 	genlmsg_end(msg, hdr);
1933 	return 0;
1934 
1935  nla_put_failure:
1936 	genlmsg_cancel(msg, hdr);
1937 	return -EMSGSIZE;
1938 }
1939 
1940 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1941 				    struct netlink_callback *cb,
1942 				    struct nl80211_dump_wiphy_state *state)
1943 {
1944 	struct nlattr **tb = genl_family_attrbuf(&nl80211_fam);
1945 	int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, tb,
1946 			      nl80211_fam.maxattr, nl80211_policy, NULL);
1947 	/* ignore parse errors for backward compatibility */
1948 	if (ret)
1949 		return 0;
1950 
1951 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1952 	if (tb[NL80211_ATTR_WIPHY])
1953 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1954 	if (tb[NL80211_ATTR_WDEV])
1955 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1956 	if (tb[NL80211_ATTR_IFINDEX]) {
1957 		struct net_device *netdev;
1958 		struct cfg80211_registered_device *rdev;
1959 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1960 
1961 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1962 		if (!netdev)
1963 			return -ENODEV;
1964 		if (netdev->ieee80211_ptr) {
1965 			rdev = wiphy_to_rdev(
1966 				netdev->ieee80211_ptr->wiphy);
1967 			state->filter_wiphy = rdev->wiphy_idx;
1968 		}
1969 	}
1970 
1971 	return 0;
1972 }
1973 
1974 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1975 {
1976 	int idx = 0, ret;
1977 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1978 	struct cfg80211_registered_device *rdev;
1979 
1980 	rtnl_lock();
1981 	if (!state) {
1982 		state = kzalloc(sizeof(*state), GFP_KERNEL);
1983 		if (!state) {
1984 			rtnl_unlock();
1985 			return -ENOMEM;
1986 		}
1987 		state->filter_wiphy = -1;
1988 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
1989 		if (ret) {
1990 			kfree(state);
1991 			rtnl_unlock();
1992 			return ret;
1993 		}
1994 		cb->args[0] = (long)state;
1995 	}
1996 
1997 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1998 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1999 			continue;
2000 		if (++idx <= state->start)
2001 			continue;
2002 		if (state->filter_wiphy != -1 &&
2003 		    state->filter_wiphy != rdev->wiphy_idx)
2004 			continue;
2005 		/* attempt to fit multiple wiphy data chunks into the skb */
2006 		do {
2007 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2008 						 skb,
2009 						 NETLINK_CB(cb->skb).portid,
2010 						 cb->nlh->nlmsg_seq,
2011 						 NLM_F_MULTI, state);
2012 			if (ret < 0) {
2013 				/*
2014 				 * If sending the wiphy data didn't fit (ENOBUFS
2015 				 * or EMSGSIZE returned), this SKB is still
2016 				 * empty (so it's not too big because another
2017 				 * wiphy dataset is already in the skb) and
2018 				 * we've not tried to adjust the dump allocation
2019 				 * yet ... then adjust the alloc size to be
2020 				 * bigger, and return 1 but with the empty skb.
2021 				 * This results in an empty message being RX'ed
2022 				 * in userspace, but that is ignored.
2023 				 *
2024 				 * We can then retry with the larger buffer.
2025 				 */
2026 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2027 				    !skb->len && !state->split &&
2028 				    cb->min_dump_alloc < 4096) {
2029 					cb->min_dump_alloc = 4096;
2030 					state->split_start = 0;
2031 					rtnl_unlock();
2032 					return 1;
2033 				}
2034 				idx--;
2035 				break;
2036 			}
2037 		} while (state->split_start > 0);
2038 		break;
2039 	}
2040 	rtnl_unlock();
2041 
2042 	state->start = idx;
2043 
2044 	return skb->len;
2045 }
2046 
2047 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2048 {
2049 	kfree((void *)cb->args[0]);
2050 	return 0;
2051 }
2052 
2053 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2054 {
2055 	struct sk_buff *msg;
2056 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2057 	struct nl80211_dump_wiphy_state state = {};
2058 
2059 	msg = nlmsg_new(4096, GFP_KERNEL);
2060 	if (!msg)
2061 		return -ENOMEM;
2062 
2063 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2064 			       info->snd_portid, info->snd_seq, 0,
2065 			       &state) < 0) {
2066 		nlmsg_free(msg);
2067 		return -ENOBUFS;
2068 	}
2069 
2070 	return genlmsg_reply(msg, info);
2071 }
2072 
2073 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2074 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2075 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2076 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2077 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2078 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2079 };
2080 
2081 static int parse_txq_params(struct nlattr *tb[],
2082 			    struct ieee80211_txq_params *txq_params)
2083 {
2084 	u8 ac;
2085 
2086 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2087 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2088 	    !tb[NL80211_TXQ_ATTR_AIFS])
2089 		return -EINVAL;
2090 
2091 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2092 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2093 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2094 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2095 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2096 
2097 	if (ac >= NL80211_NUM_ACS)
2098 		return -EINVAL;
2099 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2100 	return 0;
2101 }
2102 
2103 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2104 {
2105 	/*
2106 	 * You can only set the channel explicitly for WDS interfaces,
2107 	 * all others have their channel managed via their respective
2108 	 * "establish a connection" command (connect, join, ...)
2109 	 *
2110 	 * For AP/GO and mesh mode, the channel can be set with the
2111 	 * channel userspace API, but is only stored and passed to the
2112 	 * low-level driver when the AP starts or the mesh is joined.
2113 	 * This is for backward compatibility, userspace can also give
2114 	 * the channel in the start-ap or join-mesh commands instead.
2115 	 *
2116 	 * Monitors are special as they are normally slaved to
2117 	 * whatever else is going on, so they have their own special
2118 	 * operation to set the monitor channel if possible.
2119 	 */
2120 	return !wdev ||
2121 		wdev->iftype == NL80211_IFTYPE_AP ||
2122 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2123 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2124 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2125 }
2126 
2127 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2128 				 struct genl_info *info,
2129 				 struct cfg80211_chan_def *chandef)
2130 {
2131 	u32 control_freq;
2132 
2133 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
2134 		return -EINVAL;
2135 
2136 	control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
2137 
2138 	chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2139 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2140 	chandef->center_freq1 = control_freq;
2141 	chandef->center_freq2 = 0;
2142 
2143 	/* Primary channel not allowed */
2144 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
2145 		return -EINVAL;
2146 
2147 	if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2148 		enum nl80211_channel_type chantype;
2149 
2150 		chantype = nla_get_u32(
2151 				info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2152 
2153 		switch (chantype) {
2154 		case NL80211_CHAN_NO_HT:
2155 		case NL80211_CHAN_HT20:
2156 		case NL80211_CHAN_HT40PLUS:
2157 		case NL80211_CHAN_HT40MINUS:
2158 			cfg80211_chandef_create(chandef, chandef->chan,
2159 						chantype);
2160 			/* user input for center_freq is incorrect */
2161 			if (info->attrs[NL80211_ATTR_CENTER_FREQ1] &&
2162 			    chandef->center_freq1 != nla_get_u32(
2163 					info->attrs[NL80211_ATTR_CENTER_FREQ1]))
2164 				return -EINVAL;
2165 			/* center_freq2 must be zero */
2166 			if (info->attrs[NL80211_ATTR_CENTER_FREQ2] &&
2167 			    nla_get_u32(info->attrs[NL80211_ATTR_CENTER_FREQ2]))
2168 				return -EINVAL;
2169 			break;
2170 		default:
2171 			return -EINVAL;
2172 		}
2173 	} else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2174 		chandef->width =
2175 			nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2176 		if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
2177 			chandef->center_freq1 =
2178 				nla_get_u32(
2179 					info->attrs[NL80211_ATTR_CENTER_FREQ1]);
2180 		if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
2181 			chandef->center_freq2 =
2182 				nla_get_u32(
2183 					info->attrs[NL80211_ATTR_CENTER_FREQ2]);
2184 	}
2185 
2186 	if (!cfg80211_chandef_valid(chandef))
2187 		return -EINVAL;
2188 
2189 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2190 				     IEEE80211_CHAN_DISABLED))
2191 		return -EINVAL;
2192 
2193 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2194 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2195 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
2196 		return -EINVAL;
2197 
2198 	return 0;
2199 }
2200 
2201 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2202 				 struct net_device *dev,
2203 				 struct genl_info *info)
2204 {
2205 	struct cfg80211_chan_def chandef;
2206 	int result;
2207 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2208 	struct wireless_dev *wdev = NULL;
2209 
2210 	if (dev)
2211 		wdev = dev->ieee80211_ptr;
2212 	if (!nl80211_can_set_dev_channel(wdev))
2213 		return -EOPNOTSUPP;
2214 	if (wdev)
2215 		iftype = wdev->iftype;
2216 
2217 	result = nl80211_parse_chandef(rdev, info, &chandef);
2218 	if (result)
2219 		return result;
2220 
2221 	switch (iftype) {
2222 	case NL80211_IFTYPE_AP:
2223 	case NL80211_IFTYPE_P2P_GO:
2224 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2225 						   iftype)) {
2226 			result = -EINVAL;
2227 			break;
2228 		}
2229 		if (wdev->beacon_interval) {
2230 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2231 			    !(rdev->wiphy.features &
2232 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2233 				result = -EBUSY;
2234 				break;
2235 			}
2236 
2237 			/* Only allow dynamic channel width changes */
2238 			if (chandef.chan != wdev->preset_chandef.chan) {
2239 				result = -EBUSY;
2240 				break;
2241 			}
2242 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2243 			if (result)
2244 				break;
2245 		}
2246 		wdev->preset_chandef = chandef;
2247 		result = 0;
2248 		break;
2249 	case NL80211_IFTYPE_MESH_POINT:
2250 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2251 		break;
2252 	case NL80211_IFTYPE_MONITOR:
2253 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2254 		break;
2255 	default:
2256 		result = -EINVAL;
2257 	}
2258 
2259 	return result;
2260 }
2261 
2262 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2263 {
2264 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2265 	struct net_device *netdev = info->user_ptr[1];
2266 
2267 	return __nl80211_set_channel(rdev, netdev, info);
2268 }
2269 
2270 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2271 {
2272 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2273 	struct net_device *dev = info->user_ptr[1];
2274 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2275 	const u8 *bssid;
2276 
2277 	if (!info->attrs[NL80211_ATTR_MAC])
2278 		return -EINVAL;
2279 
2280 	if (netif_running(dev))
2281 		return -EBUSY;
2282 
2283 	if (!rdev->ops->set_wds_peer)
2284 		return -EOPNOTSUPP;
2285 
2286 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2287 		return -EOPNOTSUPP;
2288 
2289 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2290 	return rdev_set_wds_peer(rdev, dev, bssid);
2291 }
2292 
2293 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2294 {
2295 	struct cfg80211_registered_device *rdev;
2296 	struct net_device *netdev = NULL;
2297 	struct wireless_dev *wdev;
2298 	int result = 0, rem_txq_params = 0;
2299 	struct nlattr *nl_txq_params;
2300 	u32 changed;
2301 	u8 retry_short = 0, retry_long = 0;
2302 	u32 frag_threshold = 0, rts_threshold = 0;
2303 	u8 coverage_class = 0;
2304 
2305 	ASSERT_RTNL();
2306 
2307 	/*
2308 	 * Try to find the wiphy and netdev. Normally this
2309 	 * function shouldn't need the netdev, but this is
2310 	 * done for backward compatibility -- previously
2311 	 * setting the channel was done per wiphy, but now
2312 	 * it is per netdev. Previous userland like hostapd
2313 	 * also passed a netdev to set_wiphy, so that it is
2314 	 * possible to let that go to the right netdev!
2315 	 */
2316 
2317 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
2318 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2319 
2320 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2321 		if (netdev && netdev->ieee80211_ptr)
2322 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2323 		else
2324 			netdev = NULL;
2325 	}
2326 
2327 	if (!netdev) {
2328 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2329 						  info->attrs);
2330 		if (IS_ERR(rdev))
2331 			return PTR_ERR(rdev);
2332 		wdev = NULL;
2333 		netdev = NULL;
2334 		result = 0;
2335 	} else
2336 		wdev = netdev->ieee80211_ptr;
2337 
2338 	/*
2339 	 * end workaround code, by now the rdev is available
2340 	 * and locked, and wdev may or may not be NULL.
2341 	 */
2342 
2343 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2344 		result = cfg80211_dev_rename(
2345 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2346 
2347 	if (result)
2348 		return result;
2349 
2350 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2351 		struct ieee80211_txq_params txq_params;
2352 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2353 
2354 		if (!rdev->ops->set_txq_params)
2355 			return -EOPNOTSUPP;
2356 
2357 		if (!netdev)
2358 			return -EINVAL;
2359 
2360 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2361 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2362 			return -EINVAL;
2363 
2364 		if (!netif_running(netdev))
2365 			return -ENETDOWN;
2366 
2367 		nla_for_each_nested(nl_txq_params,
2368 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2369 				    rem_txq_params) {
2370 			result = nla_parse_nested(tb, NL80211_TXQ_ATTR_MAX,
2371 						  nl_txq_params,
2372 						  txq_params_policy,
2373 						  info->extack);
2374 			if (result)
2375 				return result;
2376 			result = parse_txq_params(tb, &txq_params);
2377 			if (result)
2378 				return result;
2379 
2380 			result = rdev_set_txq_params(rdev, netdev,
2381 						     &txq_params);
2382 			if (result)
2383 				return result;
2384 		}
2385 	}
2386 
2387 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2388 		result = __nl80211_set_channel(
2389 			rdev,
2390 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2391 			info);
2392 		if (result)
2393 			return result;
2394 	}
2395 
2396 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2397 		struct wireless_dev *txp_wdev = wdev;
2398 		enum nl80211_tx_power_setting type;
2399 		int idx, mbm = 0;
2400 
2401 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2402 			txp_wdev = NULL;
2403 
2404 		if (!rdev->ops->set_tx_power)
2405 			return -EOPNOTSUPP;
2406 
2407 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2408 		type = nla_get_u32(info->attrs[idx]);
2409 
2410 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2411 		    (type != NL80211_TX_POWER_AUTOMATIC))
2412 			return -EINVAL;
2413 
2414 		if (type != NL80211_TX_POWER_AUTOMATIC) {
2415 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2416 			mbm = nla_get_u32(info->attrs[idx]);
2417 		}
2418 
2419 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2420 		if (result)
2421 			return result;
2422 	}
2423 
2424 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2425 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2426 		u32 tx_ant, rx_ant;
2427 
2428 		if ((!rdev->wiphy.available_antennas_tx &&
2429 		     !rdev->wiphy.available_antennas_rx) ||
2430 		    !rdev->ops->set_antenna)
2431 			return -EOPNOTSUPP;
2432 
2433 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2434 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2435 
2436 		/* reject antenna configurations which don't match the
2437 		 * available antenna masks, except for the "all" mask */
2438 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2439 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2440 			return -EINVAL;
2441 
2442 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2443 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2444 
2445 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2446 		if (result)
2447 			return result;
2448 	}
2449 
2450 	changed = 0;
2451 
2452 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2453 		retry_short = nla_get_u8(
2454 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2455 		if (retry_short == 0)
2456 			return -EINVAL;
2457 
2458 		changed |= WIPHY_PARAM_RETRY_SHORT;
2459 	}
2460 
2461 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2462 		retry_long = nla_get_u8(
2463 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2464 		if (retry_long == 0)
2465 			return -EINVAL;
2466 
2467 		changed |= WIPHY_PARAM_RETRY_LONG;
2468 	}
2469 
2470 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2471 		frag_threshold = nla_get_u32(
2472 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2473 		if (frag_threshold < 256)
2474 			return -EINVAL;
2475 
2476 		if (frag_threshold != (u32) -1) {
2477 			/*
2478 			 * Fragments (apart from the last one) are required to
2479 			 * have even length. Make the fragmentation code
2480 			 * simpler by stripping LSB should someone try to use
2481 			 * odd threshold value.
2482 			 */
2483 			frag_threshold &= ~0x1;
2484 		}
2485 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2486 	}
2487 
2488 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2489 		rts_threshold = nla_get_u32(
2490 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2491 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
2492 	}
2493 
2494 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2495 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2496 			return -EINVAL;
2497 
2498 		coverage_class = nla_get_u8(
2499 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2500 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
2501 	}
2502 
2503 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2504 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2505 			return -EOPNOTSUPP;
2506 
2507 		changed |= WIPHY_PARAM_DYN_ACK;
2508 	}
2509 
2510 	if (changed) {
2511 		u8 old_retry_short, old_retry_long;
2512 		u32 old_frag_threshold, old_rts_threshold;
2513 		u8 old_coverage_class;
2514 
2515 		if (!rdev->ops->set_wiphy_params)
2516 			return -EOPNOTSUPP;
2517 
2518 		old_retry_short = rdev->wiphy.retry_short;
2519 		old_retry_long = rdev->wiphy.retry_long;
2520 		old_frag_threshold = rdev->wiphy.frag_threshold;
2521 		old_rts_threshold = rdev->wiphy.rts_threshold;
2522 		old_coverage_class = rdev->wiphy.coverage_class;
2523 
2524 		if (changed & WIPHY_PARAM_RETRY_SHORT)
2525 			rdev->wiphy.retry_short = retry_short;
2526 		if (changed & WIPHY_PARAM_RETRY_LONG)
2527 			rdev->wiphy.retry_long = retry_long;
2528 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2529 			rdev->wiphy.frag_threshold = frag_threshold;
2530 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2531 			rdev->wiphy.rts_threshold = rts_threshold;
2532 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2533 			rdev->wiphy.coverage_class = coverage_class;
2534 
2535 		result = rdev_set_wiphy_params(rdev, changed);
2536 		if (result) {
2537 			rdev->wiphy.retry_short = old_retry_short;
2538 			rdev->wiphy.retry_long = old_retry_long;
2539 			rdev->wiphy.frag_threshold = old_frag_threshold;
2540 			rdev->wiphy.rts_threshold = old_rts_threshold;
2541 			rdev->wiphy.coverage_class = old_coverage_class;
2542 			return result;
2543 		}
2544 	}
2545 	return 0;
2546 }
2547 
2548 static inline u64 wdev_id(struct wireless_dev *wdev)
2549 {
2550 	return (u64)wdev->identifier |
2551 	       ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2552 }
2553 
2554 static int nl80211_send_chandef(struct sk_buff *msg,
2555 				const struct cfg80211_chan_def *chandef)
2556 {
2557 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2558 		return -EINVAL;
2559 
2560 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2561 			chandef->chan->center_freq))
2562 		return -ENOBUFS;
2563 	switch (chandef->width) {
2564 	case NL80211_CHAN_WIDTH_20_NOHT:
2565 	case NL80211_CHAN_WIDTH_20:
2566 	case NL80211_CHAN_WIDTH_40:
2567 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2568 				cfg80211_get_chandef_type(chandef)))
2569 			return -ENOBUFS;
2570 		break;
2571 	default:
2572 		break;
2573 	}
2574 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2575 		return -ENOBUFS;
2576 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2577 		return -ENOBUFS;
2578 	if (chandef->center_freq2 &&
2579 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2580 		return -ENOBUFS;
2581 	return 0;
2582 }
2583 
2584 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2585 			      struct cfg80211_registered_device *rdev,
2586 			      struct wireless_dev *wdev, bool removal)
2587 {
2588 	struct net_device *dev = wdev->netdev;
2589 	u8 cmd = NL80211_CMD_NEW_INTERFACE;
2590 	void *hdr;
2591 
2592 	if (removal)
2593 		cmd = NL80211_CMD_DEL_INTERFACE;
2594 
2595 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2596 	if (!hdr)
2597 		return -1;
2598 
2599 	if (dev &&
2600 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2601 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2602 		goto nla_put_failure;
2603 
2604 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2605 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2606 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
2607 			      NL80211_ATTR_PAD) ||
2608 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2609 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2610 			rdev->devlist_generation ^
2611 			(cfg80211_rdev_list_generation << 2)))
2612 		goto nla_put_failure;
2613 
2614 	if (rdev->ops->get_channel) {
2615 		int ret;
2616 		struct cfg80211_chan_def chandef;
2617 
2618 		ret = rdev_get_channel(rdev, wdev, &chandef);
2619 		if (ret == 0) {
2620 			if (nl80211_send_chandef(msg, &chandef))
2621 				goto nla_put_failure;
2622 		}
2623 	}
2624 
2625 	if (rdev->ops->get_tx_power) {
2626 		int dbm, ret;
2627 
2628 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
2629 		if (ret == 0 &&
2630 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2631 				DBM_TO_MBM(dbm)))
2632 			goto nla_put_failure;
2633 	}
2634 
2635 	wdev_lock(wdev);
2636 	switch (wdev->iftype) {
2637 	case NL80211_IFTYPE_AP:
2638 		if (wdev->ssid_len &&
2639 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2640 			goto nla_put_failure_locked;
2641 		break;
2642 	case NL80211_IFTYPE_STATION:
2643 	case NL80211_IFTYPE_P2P_CLIENT:
2644 	case NL80211_IFTYPE_ADHOC: {
2645 		const u8 *ssid_ie;
2646 		if (!wdev->current_bss)
2647 			break;
2648 		rcu_read_lock();
2649 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
2650 					       WLAN_EID_SSID);
2651 		if (ssid_ie &&
2652 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
2653 			goto nla_put_failure_rcu_locked;
2654 		rcu_read_unlock();
2655 		break;
2656 		}
2657 	default:
2658 		/* nothing */
2659 		break;
2660 	}
2661 	wdev_unlock(wdev);
2662 
2663 	genlmsg_end(msg, hdr);
2664 	return 0;
2665 
2666  nla_put_failure_rcu_locked:
2667 	rcu_read_unlock();
2668  nla_put_failure_locked:
2669 	wdev_unlock(wdev);
2670  nla_put_failure:
2671 	genlmsg_cancel(msg, hdr);
2672 	return -EMSGSIZE;
2673 }
2674 
2675 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2676 {
2677 	int wp_idx = 0;
2678 	int if_idx = 0;
2679 	int wp_start = cb->args[0];
2680 	int if_start = cb->args[1];
2681 	int filter_wiphy = -1;
2682 	struct cfg80211_registered_device *rdev;
2683 	struct wireless_dev *wdev;
2684 	int ret;
2685 
2686 	rtnl_lock();
2687 	if (!cb->args[2]) {
2688 		struct nl80211_dump_wiphy_state state = {
2689 			.filter_wiphy = -1,
2690 		};
2691 
2692 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
2693 		if (ret)
2694 			goto out_unlock;
2695 
2696 		filter_wiphy = state.filter_wiphy;
2697 
2698 		/*
2699 		 * if filtering, set cb->args[2] to +1 since 0 is the default
2700 		 * value needed to determine that parsing is necessary.
2701 		 */
2702 		if (filter_wiphy >= 0)
2703 			cb->args[2] = filter_wiphy + 1;
2704 		else
2705 			cb->args[2] = -1;
2706 	} else if (cb->args[2] > 0) {
2707 		filter_wiphy = cb->args[2] - 1;
2708 	}
2709 
2710 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2711 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2712 			continue;
2713 		if (wp_idx < wp_start) {
2714 			wp_idx++;
2715 			continue;
2716 		}
2717 
2718 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
2719 			continue;
2720 
2721 		if_idx = 0;
2722 
2723 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
2724 			if (if_idx < if_start) {
2725 				if_idx++;
2726 				continue;
2727 			}
2728 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2729 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
2730 					       rdev, wdev, false) < 0) {
2731 				goto out;
2732 			}
2733 			if_idx++;
2734 		}
2735 
2736 		wp_idx++;
2737 	}
2738  out:
2739 	cb->args[0] = wp_idx;
2740 	cb->args[1] = if_idx;
2741 
2742 	ret = skb->len;
2743  out_unlock:
2744 	rtnl_unlock();
2745 
2746 	return ret;
2747 }
2748 
2749 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2750 {
2751 	struct sk_buff *msg;
2752 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2753 	struct wireless_dev *wdev = info->user_ptr[1];
2754 
2755 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2756 	if (!msg)
2757 		return -ENOMEM;
2758 
2759 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2760 			       rdev, wdev, false) < 0) {
2761 		nlmsg_free(msg);
2762 		return -ENOBUFS;
2763 	}
2764 
2765 	return genlmsg_reply(msg, info);
2766 }
2767 
2768 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2769 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2770 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2771 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2772 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2773 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2774 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2775 };
2776 
2777 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2778 {
2779 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2780 	int flag;
2781 
2782 	*mntrflags = 0;
2783 
2784 	if (!nla)
2785 		return -EINVAL;
2786 
2787 	if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, nla,
2788 			     mntr_flags_policy, NULL))
2789 		return -EINVAL;
2790 
2791 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2792 		if (flags[flag])
2793 			*mntrflags |= (1<<flag);
2794 
2795 	*mntrflags |= MONITOR_FLAG_CHANGED;
2796 
2797 	return 0;
2798 }
2799 
2800 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
2801 				     enum nl80211_iftype type,
2802 				     struct genl_info *info,
2803 				     struct vif_params *params)
2804 {
2805 	bool change = false;
2806 	int err;
2807 
2808 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2809 		if (type != NL80211_IFTYPE_MONITOR)
2810 			return -EINVAL;
2811 
2812 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2813 					  &params->flags);
2814 		if (err)
2815 			return err;
2816 
2817 		change = true;
2818 	}
2819 
2820 	if (params->flags & MONITOR_FLAG_ACTIVE &&
2821 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2822 		return -EOPNOTSUPP;
2823 
2824 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
2825 		const u8 *mumimo_groups;
2826 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2827 
2828 		if (type != NL80211_IFTYPE_MONITOR)
2829 			return -EINVAL;
2830 
2831 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2832 			return -EOPNOTSUPP;
2833 
2834 		mumimo_groups =
2835 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
2836 
2837 		/* bits 0 and 63 are reserved and must be zero */
2838 		if ((mumimo_groups[0] & BIT(0)) ||
2839 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
2840 			return -EINVAL;
2841 
2842 		params->vht_mumimo_groups = mumimo_groups;
2843 		change = true;
2844 	}
2845 
2846 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
2847 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2848 
2849 		if (type != NL80211_IFTYPE_MONITOR)
2850 			return -EINVAL;
2851 
2852 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2853 			return -EOPNOTSUPP;
2854 
2855 		params->vht_mumimo_follow_addr =
2856 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
2857 		change = true;
2858 	}
2859 
2860 	return change ? 1 : 0;
2861 }
2862 
2863 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2864 			       struct net_device *netdev, u8 use_4addr,
2865 			       enum nl80211_iftype iftype)
2866 {
2867 	if (!use_4addr) {
2868 		if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2869 			return -EBUSY;
2870 		return 0;
2871 	}
2872 
2873 	switch (iftype) {
2874 	case NL80211_IFTYPE_AP_VLAN:
2875 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2876 			return 0;
2877 		break;
2878 	case NL80211_IFTYPE_STATION:
2879 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2880 			return 0;
2881 		break;
2882 	default:
2883 		break;
2884 	}
2885 
2886 	return -EOPNOTSUPP;
2887 }
2888 
2889 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2890 {
2891 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2892 	struct vif_params params;
2893 	int err;
2894 	enum nl80211_iftype otype, ntype;
2895 	struct net_device *dev = info->user_ptr[1];
2896 	bool change = false;
2897 
2898 	memset(&params, 0, sizeof(params));
2899 
2900 	otype = ntype = dev->ieee80211_ptr->iftype;
2901 
2902 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
2903 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2904 		if (otype != ntype)
2905 			change = true;
2906 		if (ntype > NL80211_IFTYPE_MAX)
2907 			return -EINVAL;
2908 	}
2909 
2910 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
2911 		struct wireless_dev *wdev = dev->ieee80211_ptr;
2912 
2913 		if (ntype != NL80211_IFTYPE_MESH_POINT)
2914 			return -EINVAL;
2915 		if (netif_running(dev))
2916 			return -EBUSY;
2917 
2918 		wdev_lock(wdev);
2919 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2920 			     IEEE80211_MAX_MESH_ID_LEN);
2921 		wdev->mesh_id_up_len =
2922 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2923 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2924 		       wdev->mesh_id_up_len);
2925 		wdev_unlock(wdev);
2926 	}
2927 
2928 	if (info->attrs[NL80211_ATTR_4ADDR]) {
2929 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2930 		change = true;
2931 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2932 		if (err)
2933 			return err;
2934 	} else {
2935 		params.use_4addr = -1;
2936 	}
2937 
2938 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
2939 	if (err < 0)
2940 		return err;
2941 	if (err > 0)
2942 		change = true;
2943 
2944 	if (change)
2945 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
2946 	else
2947 		err = 0;
2948 
2949 	if (!err && params.use_4addr != -1)
2950 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
2951 
2952 	return err;
2953 }
2954 
2955 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2956 {
2957 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2958 	struct vif_params params;
2959 	struct wireless_dev *wdev;
2960 	struct sk_buff *msg;
2961 	int err;
2962 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2963 
2964 	/* to avoid failing a new interface creation due to pending removal */
2965 	cfg80211_destroy_ifaces(rdev);
2966 
2967 	memset(&params, 0, sizeof(params));
2968 
2969 	if (!info->attrs[NL80211_ATTR_IFNAME])
2970 		return -EINVAL;
2971 
2972 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
2973 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2974 		if (type > NL80211_IFTYPE_MAX)
2975 			return -EINVAL;
2976 	}
2977 
2978 	if (!rdev->ops->add_virtual_intf ||
2979 	    !(rdev->wiphy.interface_modes & (1 << type)))
2980 		return -EOPNOTSUPP;
2981 
2982 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
2983 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2984 	    info->attrs[NL80211_ATTR_MAC]) {
2985 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2986 			   ETH_ALEN);
2987 		if (!is_valid_ether_addr(params.macaddr))
2988 			return -EADDRNOTAVAIL;
2989 	}
2990 
2991 	if (info->attrs[NL80211_ATTR_4ADDR]) {
2992 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2993 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2994 		if (err)
2995 			return err;
2996 	}
2997 
2998 	err = nl80211_parse_mon_options(rdev, type, info, &params);
2999 	if (err < 0)
3000 		return err;
3001 
3002 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3003 	if (!msg)
3004 		return -ENOMEM;
3005 
3006 	wdev = rdev_add_virtual_intf(rdev,
3007 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3008 				NET_NAME_USER, type, &params);
3009 	if (WARN_ON(!wdev)) {
3010 		nlmsg_free(msg);
3011 		return -EPROTO;
3012 	} else if (IS_ERR(wdev)) {
3013 		nlmsg_free(msg);
3014 		return PTR_ERR(wdev);
3015 	}
3016 
3017 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3018 		wdev->owner_nlportid = info->snd_portid;
3019 
3020 	switch (type) {
3021 	case NL80211_IFTYPE_MESH_POINT:
3022 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3023 			break;
3024 		wdev_lock(wdev);
3025 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3026 			     IEEE80211_MAX_MESH_ID_LEN);
3027 		wdev->mesh_id_up_len =
3028 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3029 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3030 		       wdev->mesh_id_up_len);
3031 		wdev_unlock(wdev);
3032 		break;
3033 	case NL80211_IFTYPE_NAN:
3034 	case NL80211_IFTYPE_P2P_DEVICE:
3035 		/*
3036 		 * P2P Device and NAN do not have a netdev, so don't go
3037 		 * through the netdev notifier and must be added here
3038 		 */
3039 		mutex_init(&wdev->mtx);
3040 		INIT_LIST_HEAD(&wdev->event_list);
3041 		spin_lock_init(&wdev->event_lock);
3042 		INIT_LIST_HEAD(&wdev->mgmt_registrations);
3043 		spin_lock_init(&wdev->mgmt_registrations_lock);
3044 
3045 		wdev->identifier = ++rdev->wdev_id;
3046 		list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
3047 		rdev->devlist_generation++;
3048 		break;
3049 	default:
3050 		break;
3051 	}
3052 
3053 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3054 			       rdev, wdev, false) < 0) {
3055 		nlmsg_free(msg);
3056 		return -ENOBUFS;
3057 	}
3058 
3059 	/*
3060 	 * For wdevs which have no associated netdev object (e.g. of type
3061 	 * NL80211_IFTYPE_P2P_DEVICE), emit the NEW_INTERFACE event here.
3062 	 * For all other types, the event will be generated from the
3063 	 * netdev notifier
3064 	 */
3065 	if (!wdev->netdev)
3066 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
3067 
3068 	return genlmsg_reply(msg, info);
3069 }
3070 
3071 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3072 {
3073 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3074 	struct wireless_dev *wdev = info->user_ptr[1];
3075 
3076 	if (!rdev->ops->del_virtual_intf)
3077 		return -EOPNOTSUPP;
3078 
3079 	/*
3080 	 * If we remove a wireless device without a netdev then clear
3081 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3082 	 * to check if it needs to do dev_put(). Otherwise it crashes
3083 	 * since the wdev has been freed, unlike with a netdev where
3084 	 * we need the dev_put() for the netdev to really be freed.
3085 	 */
3086 	if (!wdev->netdev)
3087 		info->user_ptr[1] = NULL;
3088 
3089 	return rdev_del_virtual_intf(rdev, wdev);
3090 }
3091 
3092 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3093 {
3094 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3095 	struct net_device *dev = info->user_ptr[1];
3096 	u16 noack_map;
3097 
3098 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3099 		return -EINVAL;
3100 
3101 	if (!rdev->ops->set_noack_map)
3102 		return -EOPNOTSUPP;
3103 
3104 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3105 
3106 	return rdev_set_noack_map(rdev, dev, noack_map);
3107 }
3108 
3109 struct get_key_cookie {
3110 	struct sk_buff *msg;
3111 	int error;
3112 	int idx;
3113 };
3114 
3115 static void get_key_callback(void *c, struct key_params *params)
3116 {
3117 	struct nlattr *key;
3118 	struct get_key_cookie *cookie = c;
3119 
3120 	if ((params->key &&
3121 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3122 		     params->key_len, params->key)) ||
3123 	    (params->seq &&
3124 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3125 		     params->seq_len, params->seq)) ||
3126 	    (params->cipher &&
3127 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3128 			 params->cipher)))
3129 		goto nla_put_failure;
3130 
3131 	key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
3132 	if (!key)
3133 		goto nla_put_failure;
3134 
3135 	if ((params->key &&
3136 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3137 		     params->key_len, params->key)) ||
3138 	    (params->seq &&
3139 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3140 		     params->seq_len, params->seq)) ||
3141 	    (params->cipher &&
3142 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3143 			 params->cipher)))
3144 		goto nla_put_failure;
3145 
3146 	if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
3147 		goto nla_put_failure;
3148 
3149 	nla_nest_end(cookie->msg, key);
3150 
3151 	return;
3152  nla_put_failure:
3153 	cookie->error = 1;
3154 }
3155 
3156 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3157 {
3158 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3159 	int err;
3160 	struct net_device *dev = info->user_ptr[1];
3161 	u8 key_idx = 0;
3162 	const u8 *mac_addr = NULL;
3163 	bool pairwise;
3164 	struct get_key_cookie cookie = {
3165 		.error = 0,
3166 	};
3167 	void *hdr;
3168 	struct sk_buff *msg;
3169 
3170 	if (info->attrs[NL80211_ATTR_KEY_IDX])
3171 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3172 
3173 	if (key_idx > 5)
3174 		return -EINVAL;
3175 
3176 	if (info->attrs[NL80211_ATTR_MAC])
3177 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3178 
3179 	pairwise = !!mac_addr;
3180 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3181 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3182 
3183 		if (kt >= NUM_NL80211_KEYTYPES)
3184 			return -EINVAL;
3185 		if (kt != NL80211_KEYTYPE_GROUP &&
3186 		    kt != NL80211_KEYTYPE_PAIRWISE)
3187 			return -EINVAL;
3188 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3189 	}
3190 
3191 	if (!rdev->ops->get_key)
3192 		return -EOPNOTSUPP;
3193 
3194 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3195 		return -ENOENT;
3196 
3197 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3198 	if (!msg)
3199 		return -ENOMEM;
3200 
3201 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3202 			     NL80211_CMD_NEW_KEY);
3203 	if (!hdr)
3204 		goto nla_put_failure;
3205 
3206 	cookie.msg = msg;
3207 	cookie.idx = key_idx;
3208 
3209 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3210 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3211 		goto nla_put_failure;
3212 	if (mac_addr &&
3213 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3214 		goto nla_put_failure;
3215 
3216 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3217 			   get_key_callback);
3218 
3219 	if (err)
3220 		goto free_msg;
3221 
3222 	if (cookie.error)
3223 		goto nla_put_failure;
3224 
3225 	genlmsg_end(msg, hdr);
3226 	return genlmsg_reply(msg, info);
3227 
3228  nla_put_failure:
3229 	err = -ENOBUFS;
3230  free_msg:
3231 	nlmsg_free(msg);
3232 	return err;
3233 }
3234 
3235 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3236 {
3237 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3238 	struct key_parse key;
3239 	int err;
3240 	struct net_device *dev = info->user_ptr[1];
3241 
3242 	err = nl80211_parse_key(info, &key);
3243 	if (err)
3244 		return err;
3245 
3246 	if (key.idx < 0)
3247 		return -EINVAL;
3248 
3249 	/* only support setting default key */
3250 	if (!key.def && !key.defmgmt)
3251 		return -EINVAL;
3252 
3253 	wdev_lock(dev->ieee80211_ptr);
3254 
3255 	if (key.def) {
3256 		if (!rdev->ops->set_default_key) {
3257 			err = -EOPNOTSUPP;
3258 			goto out;
3259 		}
3260 
3261 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3262 		if (err)
3263 			goto out;
3264 
3265 		err = rdev_set_default_key(rdev, dev, key.idx,
3266 						 key.def_uni, key.def_multi);
3267 
3268 		if (err)
3269 			goto out;
3270 
3271 #ifdef CONFIG_CFG80211_WEXT
3272 		dev->ieee80211_ptr->wext.default_key = key.idx;
3273 #endif
3274 	} else {
3275 		if (key.def_uni || !key.def_multi) {
3276 			err = -EINVAL;
3277 			goto out;
3278 		}
3279 
3280 		if (!rdev->ops->set_default_mgmt_key) {
3281 			err = -EOPNOTSUPP;
3282 			goto out;
3283 		}
3284 
3285 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3286 		if (err)
3287 			goto out;
3288 
3289 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3290 		if (err)
3291 			goto out;
3292 
3293 #ifdef CONFIG_CFG80211_WEXT
3294 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3295 #endif
3296 	}
3297 
3298  out:
3299 	wdev_unlock(dev->ieee80211_ptr);
3300 
3301 	return err;
3302 }
3303 
3304 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3305 {
3306 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3307 	int err;
3308 	struct net_device *dev = info->user_ptr[1];
3309 	struct key_parse key;
3310 	const u8 *mac_addr = NULL;
3311 
3312 	err = nl80211_parse_key(info, &key);
3313 	if (err)
3314 		return err;
3315 
3316 	if (!key.p.key)
3317 		return -EINVAL;
3318 
3319 	if (info->attrs[NL80211_ATTR_MAC])
3320 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3321 
3322 	if (key.type == -1) {
3323 		if (mac_addr)
3324 			key.type = NL80211_KEYTYPE_PAIRWISE;
3325 		else
3326 			key.type = NL80211_KEYTYPE_GROUP;
3327 	}
3328 
3329 	/* for now */
3330 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3331 	    key.type != NL80211_KEYTYPE_GROUP)
3332 		return -EINVAL;
3333 
3334 	if (!rdev->ops->add_key)
3335 		return -EOPNOTSUPP;
3336 
3337 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3338 					   key.type == NL80211_KEYTYPE_PAIRWISE,
3339 					   mac_addr))
3340 		return -EINVAL;
3341 
3342 	wdev_lock(dev->ieee80211_ptr);
3343 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3344 	if (!err)
3345 		err = rdev_add_key(rdev, dev, key.idx,
3346 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3347 				    mac_addr, &key.p);
3348 	wdev_unlock(dev->ieee80211_ptr);
3349 
3350 	return err;
3351 }
3352 
3353 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3354 {
3355 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3356 	int err;
3357 	struct net_device *dev = info->user_ptr[1];
3358 	u8 *mac_addr = NULL;
3359 	struct key_parse key;
3360 
3361 	err = nl80211_parse_key(info, &key);
3362 	if (err)
3363 		return err;
3364 
3365 	if (info->attrs[NL80211_ATTR_MAC])
3366 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3367 
3368 	if (key.type == -1) {
3369 		if (mac_addr)
3370 			key.type = NL80211_KEYTYPE_PAIRWISE;
3371 		else
3372 			key.type = NL80211_KEYTYPE_GROUP;
3373 	}
3374 
3375 	/* for now */
3376 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3377 	    key.type != NL80211_KEYTYPE_GROUP)
3378 		return -EINVAL;
3379 
3380 	if (!rdev->ops->del_key)
3381 		return -EOPNOTSUPP;
3382 
3383 	wdev_lock(dev->ieee80211_ptr);
3384 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3385 
3386 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3387 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3388 		err = -ENOENT;
3389 
3390 	if (!err)
3391 		err = rdev_del_key(rdev, dev, key.idx,
3392 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3393 				   mac_addr);
3394 
3395 #ifdef CONFIG_CFG80211_WEXT
3396 	if (!err) {
3397 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
3398 			dev->ieee80211_ptr->wext.default_key = -1;
3399 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3400 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3401 	}
3402 #endif
3403 	wdev_unlock(dev->ieee80211_ptr);
3404 
3405 	return err;
3406 }
3407 
3408 /* This function returns an error or the number of nested attributes */
3409 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3410 {
3411 	struct nlattr *attr;
3412 	int n_entries = 0, tmp;
3413 
3414 	nla_for_each_nested(attr, nl_attr, tmp) {
3415 		if (nla_len(attr) != ETH_ALEN)
3416 			return -EINVAL;
3417 
3418 		n_entries++;
3419 	}
3420 
3421 	return n_entries;
3422 }
3423 
3424 /*
3425  * This function parses ACL information and allocates memory for ACL data.
3426  * On successful return, the calling function is responsible to free the
3427  * ACL buffer returned by this function.
3428  */
3429 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3430 						struct genl_info *info)
3431 {
3432 	enum nl80211_acl_policy acl_policy;
3433 	struct nlattr *attr;
3434 	struct cfg80211_acl_data *acl;
3435 	int i = 0, n_entries, tmp;
3436 
3437 	if (!wiphy->max_acl_mac_addrs)
3438 		return ERR_PTR(-EOPNOTSUPP);
3439 
3440 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3441 		return ERR_PTR(-EINVAL);
3442 
3443 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3444 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3445 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3446 		return ERR_PTR(-EINVAL);
3447 
3448 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3449 		return ERR_PTR(-EINVAL);
3450 
3451 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3452 	if (n_entries < 0)
3453 		return ERR_PTR(n_entries);
3454 
3455 	if (n_entries > wiphy->max_acl_mac_addrs)
3456 		return ERR_PTR(-ENOTSUPP);
3457 
3458 	acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3459 		      GFP_KERNEL);
3460 	if (!acl)
3461 		return ERR_PTR(-ENOMEM);
3462 
3463 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3464 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3465 		i++;
3466 	}
3467 
3468 	acl->n_acl_entries = n_entries;
3469 	acl->acl_policy = acl_policy;
3470 
3471 	return acl;
3472 }
3473 
3474 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3475 {
3476 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3477 	struct net_device *dev = info->user_ptr[1];
3478 	struct cfg80211_acl_data *acl;
3479 	int err;
3480 
3481 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3482 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3483 		return -EOPNOTSUPP;
3484 
3485 	if (!dev->ieee80211_ptr->beacon_interval)
3486 		return -EINVAL;
3487 
3488 	acl = parse_acl_data(&rdev->wiphy, info);
3489 	if (IS_ERR(acl))
3490 		return PTR_ERR(acl);
3491 
3492 	err = rdev_set_mac_acl(rdev, dev, acl);
3493 
3494 	kfree(acl);
3495 
3496 	return err;
3497 }
3498 
3499 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3500 			   u8 *rates, u8 rates_len)
3501 {
3502 	u8 i;
3503 	u32 mask = 0;
3504 
3505 	for (i = 0; i < rates_len; i++) {
3506 		int rate = (rates[i] & 0x7f) * 5;
3507 		int ridx;
3508 
3509 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
3510 			struct ieee80211_rate *srate =
3511 				&sband->bitrates[ridx];
3512 			if (rate == srate->bitrate) {
3513 				mask |= 1 << ridx;
3514 				break;
3515 			}
3516 		}
3517 		if (ridx == sband->n_bitrates)
3518 			return 0; /* rate not found */
3519 	}
3520 
3521 	return mask;
3522 }
3523 
3524 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
3525 			       u8 *rates, u8 rates_len,
3526 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
3527 {
3528 	u8 i;
3529 
3530 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
3531 
3532 	for (i = 0; i < rates_len; i++) {
3533 		int ridx, rbit;
3534 
3535 		ridx = rates[i] / 8;
3536 		rbit = BIT(rates[i] % 8);
3537 
3538 		/* check validity */
3539 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
3540 			return false;
3541 
3542 		/* check availability */
3543 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
3544 			mcs[ridx] |= rbit;
3545 		else
3546 			return false;
3547 	}
3548 
3549 	return true;
3550 }
3551 
3552 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
3553 {
3554 	u16 mcs_mask = 0;
3555 
3556 	switch (vht_mcs_map) {
3557 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
3558 		break;
3559 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
3560 		mcs_mask = 0x00FF;
3561 		break;
3562 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
3563 		mcs_mask = 0x01FF;
3564 		break;
3565 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
3566 		mcs_mask = 0x03FF;
3567 		break;
3568 	default:
3569 		break;
3570 	}
3571 
3572 	return mcs_mask;
3573 }
3574 
3575 static void vht_build_mcs_mask(u16 vht_mcs_map,
3576 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
3577 {
3578 	u8 nss;
3579 
3580 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
3581 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
3582 		vht_mcs_map >>= 2;
3583 	}
3584 }
3585 
3586 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
3587 			     struct nl80211_txrate_vht *txrate,
3588 			     u16 mcs[NL80211_VHT_NSS_MAX])
3589 {
3590 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3591 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
3592 	u8 i;
3593 
3594 	if (!sband->vht_cap.vht_supported)
3595 		return false;
3596 
3597 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
3598 
3599 	/* Build vht_mcs_mask from VHT capabilities */
3600 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
3601 
3602 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3603 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
3604 			mcs[i] = txrate->mcs[i];
3605 		else
3606 			return false;
3607 	}
3608 
3609 	return true;
3610 }
3611 
3612 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
3613 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
3614 				    .len = NL80211_MAX_SUPP_RATES },
3615 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
3616 				.len = NL80211_MAX_SUPP_HT_RATES },
3617 	[NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
3618 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
3619 };
3620 
3621 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
3622 					 struct cfg80211_bitrate_mask *mask)
3623 {
3624 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
3625 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3626 	int rem, i;
3627 	struct nlattr *tx_rates;
3628 	struct ieee80211_supported_band *sband;
3629 	u16 vht_tx_mcs_map;
3630 
3631 	memset(mask, 0, sizeof(*mask));
3632 	/* Default to all rates enabled */
3633 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
3634 		sband = rdev->wiphy.bands[i];
3635 
3636 		if (!sband)
3637 			continue;
3638 
3639 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
3640 		memcpy(mask->control[i].ht_mcs,
3641 		       sband->ht_cap.mcs.rx_mask,
3642 		       sizeof(mask->control[i].ht_mcs));
3643 
3644 		if (!sband->vht_cap.vht_supported)
3645 			continue;
3646 
3647 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3648 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
3649 	}
3650 
3651 	/* if no rates are given set it back to the defaults */
3652 	if (!info->attrs[NL80211_ATTR_TX_RATES])
3653 		goto out;
3654 
3655 	/* The nested attribute uses enum nl80211_band as the index. This maps
3656 	 * directly to the enum nl80211_band values used in cfg80211.
3657 	 */
3658 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
3659 	nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
3660 		enum nl80211_band band = nla_type(tx_rates);
3661 		int err;
3662 
3663 		if (band < 0 || band >= NUM_NL80211_BANDS)
3664 			return -EINVAL;
3665 		sband = rdev->wiphy.bands[band];
3666 		if (sband == NULL)
3667 			return -EINVAL;
3668 		err = nla_parse_nested(tb, NL80211_TXRATE_MAX, tx_rates,
3669 				       nl80211_txattr_policy, info->extack);
3670 		if (err)
3671 			return err;
3672 		if (tb[NL80211_TXRATE_LEGACY]) {
3673 			mask->control[band].legacy = rateset_to_mask(
3674 				sband,
3675 				nla_data(tb[NL80211_TXRATE_LEGACY]),
3676 				nla_len(tb[NL80211_TXRATE_LEGACY]));
3677 			if ((mask->control[band].legacy == 0) &&
3678 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
3679 				return -EINVAL;
3680 		}
3681 		if (tb[NL80211_TXRATE_HT]) {
3682 			if (!ht_rateset_to_mask(
3683 					sband,
3684 					nla_data(tb[NL80211_TXRATE_HT]),
3685 					nla_len(tb[NL80211_TXRATE_HT]),
3686 					mask->control[band].ht_mcs))
3687 				return -EINVAL;
3688 		}
3689 		if (tb[NL80211_TXRATE_VHT]) {
3690 			if (!vht_set_mcs_mask(
3691 					sband,
3692 					nla_data(tb[NL80211_TXRATE_VHT]),
3693 					mask->control[band].vht_mcs))
3694 				return -EINVAL;
3695 		}
3696 		if (tb[NL80211_TXRATE_GI]) {
3697 			mask->control[band].gi =
3698 				nla_get_u8(tb[NL80211_TXRATE_GI]);
3699 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
3700 				return -EINVAL;
3701 		}
3702 
3703 		if (mask->control[band].legacy == 0) {
3704 			/* don't allow empty legacy rates if HT or VHT
3705 			 * are not even supported.
3706 			 */
3707 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
3708 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
3709 				return -EINVAL;
3710 
3711 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3712 				if (mask->control[band].ht_mcs[i])
3713 					goto out;
3714 
3715 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3716 				if (mask->control[band].vht_mcs[i])
3717 					goto out;
3718 
3719 			/* legacy and mcs rates may not be both empty */
3720 			return -EINVAL;
3721 		}
3722 	}
3723 
3724 out:
3725 	return 0;
3726 }
3727 
3728 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
3729 				   enum nl80211_band band,
3730 				   struct cfg80211_bitrate_mask *beacon_rate)
3731 {
3732 	u32 count_ht, count_vht, i;
3733 	u32 rate = beacon_rate->control[band].legacy;
3734 
3735 	/* Allow only one rate */
3736 	if (hweight32(rate) > 1)
3737 		return -EINVAL;
3738 
3739 	count_ht = 0;
3740 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3741 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
3742 			return -EINVAL;
3743 		} else if (beacon_rate->control[band].ht_mcs[i]) {
3744 			count_ht++;
3745 			if (count_ht > 1)
3746 				return -EINVAL;
3747 		}
3748 		if (count_ht && rate)
3749 			return -EINVAL;
3750 	}
3751 
3752 	count_vht = 0;
3753 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3754 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
3755 			return -EINVAL;
3756 		} else if (beacon_rate->control[band].vht_mcs[i]) {
3757 			count_vht++;
3758 			if (count_vht > 1)
3759 				return -EINVAL;
3760 		}
3761 		if (count_vht && rate)
3762 			return -EINVAL;
3763 	}
3764 
3765 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
3766 		return -EINVAL;
3767 
3768 	if (rate &&
3769 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3770 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
3771 		return -EINVAL;
3772 	if (count_ht &&
3773 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3774 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
3775 		return -EINVAL;
3776 	if (count_vht &&
3777 	    !wiphy_ext_feature_isset(&rdev->wiphy,
3778 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
3779 		return -EINVAL;
3780 
3781 	return 0;
3782 }
3783 
3784 static int nl80211_parse_beacon(struct nlattr *attrs[],
3785 				struct cfg80211_beacon_data *bcn)
3786 {
3787 	bool haveinfo = false;
3788 
3789 	if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3790 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3791 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3792 	    !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3793 		return -EINVAL;
3794 
3795 	memset(bcn, 0, sizeof(*bcn));
3796 
3797 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3798 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3799 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3800 		if (!bcn->head_len)
3801 			return -EINVAL;
3802 		haveinfo = true;
3803 	}
3804 
3805 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3806 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3807 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3808 		haveinfo = true;
3809 	}
3810 
3811 	if (!haveinfo)
3812 		return -EINVAL;
3813 
3814 	if (attrs[NL80211_ATTR_IE]) {
3815 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3816 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3817 	}
3818 
3819 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3820 		bcn->proberesp_ies =
3821 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3822 		bcn->proberesp_ies_len =
3823 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3824 	}
3825 
3826 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3827 		bcn->assocresp_ies =
3828 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3829 		bcn->assocresp_ies_len =
3830 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3831 	}
3832 
3833 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
3834 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3835 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3836 	}
3837 
3838 	return 0;
3839 }
3840 
3841 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
3842 					    const u8 *rates)
3843 {
3844 	int i;
3845 
3846 	if (!rates)
3847 		return;
3848 
3849 	for (i = 0; i < rates[1]; i++) {
3850 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
3851 			params->ht_required = true;
3852 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
3853 			params->vht_required = true;
3854 	}
3855 }
3856 
3857 /*
3858  * Since the nl80211 API didn't include, from the beginning, attributes about
3859  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
3860  * benefit of drivers that rebuild IEs in the firmware.
3861  */
3862 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
3863 {
3864 	const struct cfg80211_beacon_data *bcn = &params->beacon;
3865 	size_t ies_len = bcn->tail_len;
3866 	const u8 *ies = bcn->tail;
3867 	const u8 *rates;
3868 	const u8 *cap;
3869 
3870 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
3871 	nl80211_check_ap_rate_selectors(params, rates);
3872 
3873 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
3874 	nl80211_check_ap_rate_selectors(params, rates);
3875 
3876 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
3877 	if (cap && cap[1] >= sizeof(*params->ht_cap))
3878 		params->ht_cap = (void *)(cap + 2);
3879 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
3880 	if (cap && cap[1] >= sizeof(*params->vht_cap))
3881 		params->vht_cap = (void *)(cap + 2);
3882 }
3883 
3884 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3885 				   struct cfg80211_ap_settings *params)
3886 {
3887 	struct wireless_dev *wdev;
3888 	bool ret = false;
3889 
3890 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3891 		if (wdev->iftype != NL80211_IFTYPE_AP &&
3892 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
3893 			continue;
3894 
3895 		if (!wdev->preset_chandef.chan)
3896 			continue;
3897 
3898 		params->chandef = wdev->preset_chandef;
3899 		ret = true;
3900 		break;
3901 	}
3902 
3903 	return ret;
3904 }
3905 
3906 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3907 				    enum nl80211_auth_type auth_type,
3908 				    enum nl80211_commands cmd)
3909 {
3910 	if (auth_type > NL80211_AUTHTYPE_MAX)
3911 		return false;
3912 
3913 	switch (cmd) {
3914 	case NL80211_CMD_AUTHENTICATE:
3915 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3916 		    auth_type == NL80211_AUTHTYPE_SAE)
3917 			return false;
3918 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3919 					     NL80211_EXT_FEATURE_FILS_STA) &&
3920 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3921 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3922 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
3923 			return false;
3924 		return true;
3925 	case NL80211_CMD_CONNECT:
3926 		/* SAE not supported yet */
3927 		if (auth_type == NL80211_AUTHTYPE_SAE)
3928 			return false;
3929 		/* FILS with SK PFS or PK not supported yet */
3930 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3931 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
3932 			return false;
3933 		if (!wiphy_ext_feature_isset(
3934 			    &rdev->wiphy,
3935 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
3936 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
3937 			return false;
3938 		return true;
3939 	case NL80211_CMD_START_AP:
3940 		/* SAE not supported yet */
3941 		if (auth_type == NL80211_AUTHTYPE_SAE)
3942 			return false;
3943 		/* FILS not supported yet */
3944 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3945 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3946 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
3947 			return false;
3948 		return true;
3949 	default:
3950 		return false;
3951 	}
3952 }
3953 
3954 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3955 {
3956 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3957 	struct net_device *dev = info->user_ptr[1];
3958 	struct wireless_dev *wdev = dev->ieee80211_ptr;
3959 	struct cfg80211_ap_settings params;
3960 	int err;
3961 
3962 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3963 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3964 		return -EOPNOTSUPP;
3965 
3966 	if (!rdev->ops->start_ap)
3967 		return -EOPNOTSUPP;
3968 
3969 	if (wdev->beacon_interval)
3970 		return -EALREADY;
3971 
3972 	memset(&params, 0, sizeof(params));
3973 
3974 	/* these are required for START_AP */
3975 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3976 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3977 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
3978 		return -EINVAL;
3979 
3980 	err = nl80211_parse_beacon(info->attrs, &params.beacon);
3981 	if (err)
3982 		return err;
3983 
3984 	params.beacon_interval =
3985 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3986 	params.dtim_period =
3987 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3988 
3989 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
3990 					   params.beacon_interval);
3991 	if (err)
3992 		return err;
3993 
3994 	/*
3995 	 * In theory, some of these attributes should be required here
3996 	 * but since they were not used when the command was originally
3997 	 * added, keep them optional for old user space programs to let
3998 	 * them continue to work with drivers that do not need the
3999 	 * additional information -- drivers must check!
4000 	 */
4001 	if (info->attrs[NL80211_ATTR_SSID]) {
4002 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4003 		params.ssid_len =
4004 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4005 		if (params.ssid_len == 0 ||
4006 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4007 			return -EINVAL;
4008 	}
4009 
4010 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
4011 		params.hidden_ssid = nla_get_u32(
4012 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4013 		if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
4014 		    params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
4015 		    params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
4016 			return -EINVAL;
4017 	}
4018 
4019 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4020 
4021 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4022 		params.auth_type = nla_get_u32(
4023 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4024 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4025 					     NL80211_CMD_START_AP))
4026 			return -EINVAL;
4027 	} else
4028 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4029 
4030 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4031 				      NL80211_MAX_NR_CIPHER_SUITES);
4032 	if (err)
4033 		return err;
4034 
4035 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4036 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4037 			return -EOPNOTSUPP;
4038 		params.inactivity_timeout = nla_get_u16(
4039 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4040 	}
4041 
4042 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4043 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4044 			return -EINVAL;
4045 		params.p2p_ctwindow =
4046 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4047 		if (params.p2p_ctwindow > 127)
4048 			return -EINVAL;
4049 		if (params.p2p_ctwindow != 0 &&
4050 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4051 			return -EINVAL;
4052 	}
4053 
4054 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4055 		u8 tmp;
4056 
4057 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4058 			return -EINVAL;
4059 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4060 		if (tmp > 1)
4061 			return -EINVAL;
4062 		params.p2p_opp_ps = tmp;
4063 		if (params.p2p_opp_ps != 0 &&
4064 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4065 			return -EINVAL;
4066 	}
4067 
4068 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4069 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4070 		if (err)
4071 			return err;
4072 	} else if (wdev->preset_chandef.chan) {
4073 		params.chandef = wdev->preset_chandef;
4074 	} else if (!nl80211_get_ap_channel(rdev, &params))
4075 		return -EINVAL;
4076 
4077 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4078 					   wdev->iftype))
4079 		return -EINVAL;
4080 
4081 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4082 		err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4083 		if (err)
4084 			return err;
4085 
4086 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4087 					      &params.beacon_rate);
4088 		if (err)
4089 			return err;
4090 	}
4091 
4092 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4093 		params.smps_mode =
4094 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4095 		switch (params.smps_mode) {
4096 		case NL80211_SMPS_OFF:
4097 			break;
4098 		case NL80211_SMPS_STATIC:
4099 			if (!(rdev->wiphy.features &
4100 			      NL80211_FEATURE_STATIC_SMPS))
4101 				return -EINVAL;
4102 			break;
4103 		case NL80211_SMPS_DYNAMIC:
4104 			if (!(rdev->wiphy.features &
4105 			      NL80211_FEATURE_DYNAMIC_SMPS))
4106 				return -EINVAL;
4107 			break;
4108 		default:
4109 			return -EINVAL;
4110 		}
4111 	} else {
4112 		params.smps_mode = NL80211_SMPS_OFF;
4113 	}
4114 
4115 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4116 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4117 		return -EOPNOTSUPP;
4118 
4119 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4120 		params.acl = parse_acl_data(&rdev->wiphy, info);
4121 		if (IS_ERR(params.acl))
4122 			return PTR_ERR(params.acl);
4123 	}
4124 
4125 	nl80211_calculate_ap_params(&params);
4126 
4127 	wdev_lock(wdev);
4128 	err = rdev_start_ap(rdev, dev, &params);
4129 	if (!err) {
4130 		wdev->preset_chandef = params.chandef;
4131 		wdev->beacon_interval = params.beacon_interval;
4132 		wdev->chandef = params.chandef;
4133 		wdev->ssid_len = params.ssid_len;
4134 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4135 	}
4136 	wdev_unlock(wdev);
4137 
4138 	kfree(params.acl);
4139 
4140 	return err;
4141 }
4142 
4143 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4144 {
4145 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4146 	struct net_device *dev = info->user_ptr[1];
4147 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4148 	struct cfg80211_beacon_data params;
4149 	int err;
4150 
4151 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4152 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4153 		return -EOPNOTSUPP;
4154 
4155 	if (!rdev->ops->change_beacon)
4156 		return -EOPNOTSUPP;
4157 
4158 	if (!wdev->beacon_interval)
4159 		return -EINVAL;
4160 
4161 	err = nl80211_parse_beacon(info->attrs, &params);
4162 	if (err)
4163 		return err;
4164 
4165 	wdev_lock(wdev);
4166 	err = rdev_change_beacon(rdev, dev, &params);
4167 	wdev_unlock(wdev);
4168 
4169 	return err;
4170 }
4171 
4172 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4173 {
4174 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4175 	struct net_device *dev = info->user_ptr[1];
4176 
4177 	return cfg80211_stop_ap(rdev, dev, false);
4178 }
4179 
4180 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4181 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4182 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4183 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4184 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4185 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4186 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4187 };
4188 
4189 static int parse_station_flags(struct genl_info *info,
4190 			       enum nl80211_iftype iftype,
4191 			       struct station_parameters *params)
4192 {
4193 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4194 	struct nlattr *nla;
4195 	int flag;
4196 
4197 	/*
4198 	 * Try parsing the new attribute first so userspace
4199 	 * can specify both for older kernels.
4200 	 */
4201 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4202 	if (nla) {
4203 		struct nl80211_sta_flag_update *sta_flags;
4204 
4205 		sta_flags = nla_data(nla);
4206 		params->sta_flags_mask = sta_flags->mask;
4207 		params->sta_flags_set = sta_flags->set;
4208 		params->sta_flags_set &= params->sta_flags_mask;
4209 		if ((params->sta_flags_mask |
4210 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4211 			return -EINVAL;
4212 		return 0;
4213 	}
4214 
4215 	/* if present, parse the old attribute */
4216 
4217 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4218 	if (!nla)
4219 		return 0;
4220 
4221 	if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, nla,
4222 			     sta_flags_policy, info->extack))
4223 		return -EINVAL;
4224 
4225 	/*
4226 	 * Only allow certain flags for interface types so that
4227 	 * other attributes are silently ignored. Remember that
4228 	 * this is backward compatibility code with old userspace
4229 	 * and shouldn't be hit in other cases anyway.
4230 	 */
4231 	switch (iftype) {
4232 	case NL80211_IFTYPE_AP:
4233 	case NL80211_IFTYPE_AP_VLAN:
4234 	case NL80211_IFTYPE_P2P_GO:
4235 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4236 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4237 					 BIT(NL80211_STA_FLAG_WME) |
4238 					 BIT(NL80211_STA_FLAG_MFP);
4239 		break;
4240 	case NL80211_IFTYPE_P2P_CLIENT:
4241 	case NL80211_IFTYPE_STATION:
4242 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4243 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
4244 		break;
4245 	case NL80211_IFTYPE_MESH_POINT:
4246 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4247 					 BIT(NL80211_STA_FLAG_MFP) |
4248 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
4249 	default:
4250 		return -EINVAL;
4251 	}
4252 
4253 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4254 		if (flags[flag]) {
4255 			params->sta_flags_set |= (1<<flag);
4256 
4257 			/* no longer support new API additions in old API */
4258 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4259 				return -EINVAL;
4260 		}
4261 	}
4262 
4263 	return 0;
4264 }
4265 
4266 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
4267 				 int attr)
4268 {
4269 	struct nlattr *rate;
4270 	u32 bitrate;
4271 	u16 bitrate_compat;
4272 	enum nl80211_rate_info rate_flg;
4273 
4274 	rate = nla_nest_start(msg, attr);
4275 	if (!rate)
4276 		return false;
4277 
4278 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4279 	bitrate = cfg80211_calculate_bitrate(info);
4280 	/* report 16-bit bitrate only if we can */
4281 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4282 	if (bitrate > 0 &&
4283 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4284 		return false;
4285 	if (bitrate_compat > 0 &&
4286 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4287 		return false;
4288 
4289 	switch (info->bw) {
4290 	case RATE_INFO_BW_5:
4291 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4292 		break;
4293 	case RATE_INFO_BW_10:
4294 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4295 		break;
4296 	default:
4297 		WARN_ON(1);
4298 		/* fall through */
4299 	case RATE_INFO_BW_20:
4300 		rate_flg = 0;
4301 		break;
4302 	case RATE_INFO_BW_40:
4303 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4304 		break;
4305 	case RATE_INFO_BW_80:
4306 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4307 		break;
4308 	case RATE_INFO_BW_160:
4309 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4310 		break;
4311 	}
4312 
4313 	if (rate_flg && nla_put_flag(msg, rate_flg))
4314 		return false;
4315 
4316 	if (info->flags & RATE_INFO_FLAGS_MCS) {
4317 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4318 			return false;
4319 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4320 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4321 			return false;
4322 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4323 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4324 			return false;
4325 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4326 			return false;
4327 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4328 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4329 			return false;
4330 	}
4331 
4332 	nla_nest_end(msg, rate);
4333 	return true;
4334 }
4335 
4336 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4337 			       int id)
4338 {
4339 	void *attr;
4340 	int i = 0;
4341 
4342 	if (!mask)
4343 		return true;
4344 
4345 	attr = nla_nest_start(msg, id);
4346 	if (!attr)
4347 		return false;
4348 
4349 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4350 		if (!(mask & BIT(i)))
4351 			continue;
4352 
4353 		if (nla_put_u8(msg, i, signal[i]))
4354 			return false;
4355 	}
4356 
4357 	nla_nest_end(msg, attr);
4358 
4359 	return true;
4360 }
4361 
4362 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4363 				u32 seq, int flags,
4364 				struct cfg80211_registered_device *rdev,
4365 				struct net_device *dev,
4366 				const u8 *mac_addr, struct station_info *sinfo)
4367 {
4368 	void *hdr;
4369 	struct nlattr *sinfoattr, *bss_param;
4370 
4371 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
4372 	if (!hdr)
4373 		return -1;
4374 
4375 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4376 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
4377 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
4378 		goto nla_put_failure;
4379 
4380 	sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
4381 	if (!sinfoattr)
4382 		goto nla_put_failure;
4383 
4384 #define PUT_SINFO(attr, memb, type) do {				\
4385 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
4386 	if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&	\
4387 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
4388 			     sinfo->memb))				\
4389 		goto nla_put_failure;					\
4390 	} while (0)
4391 #define PUT_SINFO_U64(attr, memb) do {					\
4392 	if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&	\
4393 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
4394 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
4395 		goto nla_put_failure;					\
4396 	} while (0)
4397 
4398 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
4399 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
4400 
4401 	if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
4402 			     BIT(NL80211_STA_INFO_RX_BYTES64)) &&
4403 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
4404 			(u32)sinfo->rx_bytes))
4405 		goto nla_put_failure;
4406 
4407 	if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
4408 			     BIT(NL80211_STA_INFO_TX_BYTES64)) &&
4409 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
4410 			(u32)sinfo->tx_bytes))
4411 		goto nla_put_failure;
4412 
4413 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
4414 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
4415 	PUT_SINFO(LLID, llid, u16);
4416 	PUT_SINFO(PLID, plid, u16);
4417 	PUT_SINFO(PLINK_STATE, plink_state, u8);
4418 	PUT_SINFO_U64(RX_DURATION, rx_duration);
4419 
4420 	switch (rdev->wiphy.signal_type) {
4421 	case CFG80211_SIGNAL_TYPE_MBM:
4422 		PUT_SINFO(SIGNAL, signal, u8);
4423 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
4424 		break;
4425 	default:
4426 		break;
4427 	}
4428 	if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
4429 		if (!nl80211_put_signal(msg, sinfo->chains,
4430 					sinfo->chain_signal,
4431 					NL80211_STA_INFO_CHAIN_SIGNAL))
4432 			goto nla_put_failure;
4433 	}
4434 	if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
4435 		if (!nl80211_put_signal(msg, sinfo->chains,
4436 					sinfo->chain_signal_avg,
4437 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
4438 			goto nla_put_failure;
4439 	}
4440 	if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
4441 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
4442 					  NL80211_STA_INFO_TX_BITRATE))
4443 			goto nla_put_failure;
4444 	}
4445 	if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
4446 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
4447 					  NL80211_STA_INFO_RX_BITRATE))
4448 			goto nla_put_failure;
4449 	}
4450 
4451 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
4452 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
4453 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
4454 	PUT_SINFO(TX_FAILED, tx_failed, u32);
4455 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
4456 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
4457 	PUT_SINFO(LOCAL_PM, local_pm, u32);
4458 	PUT_SINFO(PEER_PM, peer_pm, u32);
4459 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
4460 
4461 	if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
4462 		bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
4463 		if (!bss_param)
4464 			goto nla_put_failure;
4465 
4466 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
4467 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
4468 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
4469 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
4470 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
4471 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
4472 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
4473 			       sinfo->bss_param.dtim_period) ||
4474 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
4475 				sinfo->bss_param.beacon_interval))
4476 			goto nla_put_failure;
4477 
4478 		nla_nest_end(msg, bss_param);
4479 	}
4480 	if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
4481 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
4482 		    sizeof(struct nl80211_sta_flag_update),
4483 		    &sinfo->sta_flags))
4484 		goto nla_put_failure;
4485 
4486 	PUT_SINFO_U64(T_OFFSET, t_offset);
4487 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
4488 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
4489 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
4490 
4491 #undef PUT_SINFO
4492 #undef PUT_SINFO_U64
4493 
4494 	if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
4495 		struct nlattr *tidsattr;
4496 		int tid;
4497 
4498 		tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
4499 		if (!tidsattr)
4500 			goto nla_put_failure;
4501 
4502 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
4503 			struct cfg80211_tid_stats *tidstats;
4504 			struct nlattr *tidattr;
4505 
4506 			tidstats = &sinfo->pertid[tid];
4507 
4508 			if (!tidstats->filled)
4509 				continue;
4510 
4511 			tidattr = nla_nest_start(msg, tid + 1);
4512 			if (!tidattr)
4513 				goto nla_put_failure;
4514 
4515 #define PUT_TIDVAL_U64(attr, memb) do {					\
4516 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
4517 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
4518 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
4519 		goto nla_put_failure;					\
4520 	} while (0)
4521 
4522 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
4523 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
4524 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
4525 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
4526 
4527 #undef PUT_TIDVAL_U64
4528 			nla_nest_end(msg, tidattr);
4529 		}
4530 
4531 		nla_nest_end(msg, tidsattr);
4532 	}
4533 
4534 	nla_nest_end(msg, sinfoattr);
4535 
4536 	if (sinfo->assoc_req_ies_len &&
4537 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
4538 		    sinfo->assoc_req_ies))
4539 		goto nla_put_failure;
4540 
4541 	genlmsg_end(msg, hdr);
4542 	return 0;
4543 
4544  nla_put_failure:
4545 	genlmsg_cancel(msg, hdr);
4546 	return -EMSGSIZE;
4547 }
4548 
4549 static int nl80211_dump_station(struct sk_buff *skb,
4550 				struct netlink_callback *cb)
4551 {
4552 	struct station_info sinfo;
4553 	struct cfg80211_registered_device *rdev;
4554 	struct wireless_dev *wdev;
4555 	u8 mac_addr[ETH_ALEN];
4556 	int sta_idx = cb->args[2];
4557 	int err;
4558 
4559 	rtnl_lock();
4560 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4561 	if (err)
4562 		goto out_err;
4563 
4564 	if (!wdev->netdev) {
4565 		err = -EINVAL;
4566 		goto out_err;
4567 	}
4568 
4569 	if (!rdev->ops->dump_station) {
4570 		err = -EOPNOTSUPP;
4571 		goto out_err;
4572 	}
4573 
4574 	while (1) {
4575 		memset(&sinfo, 0, sizeof(sinfo));
4576 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
4577 					mac_addr, &sinfo);
4578 		if (err == -ENOENT)
4579 			break;
4580 		if (err)
4581 			goto out_err;
4582 
4583 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
4584 				NETLINK_CB(cb->skb).portid,
4585 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
4586 				rdev, wdev->netdev, mac_addr,
4587 				&sinfo) < 0)
4588 			goto out;
4589 
4590 		sta_idx++;
4591 	}
4592 
4593  out:
4594 	cb->args[2] = sta_idx;
4595 	err = skb->len;
4596  out_err:
4597 	rtnl_unlock();
4598 
4599 	return err;
4600 }
4601 
4602 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
4603 {
4604 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4605 	struct net_device *dev = info->user_ptr[1];
4606 	struct station_info sinfo;
4607 	struct sk_buff *msg;
4608 	u8 *mac_addr = NULL;
4609 	int err;
4610 
4611 	memset(&sinfo, 0, sizeof(sinfo));
4612 
4613 	if (!info->attrs[NL80211_ATTR_MAC])
4614 		return -EINVAL;
4615 
4616 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4617 
4618 	if (!rdev->ops->get_station)
4619 		return -EOPNOTSUPP;
4620 
4621 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
4622 	if (err)
4623 		return err;
4624 
4625 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4626 	if (!msg)
4627 		return -ENOMEM;
4628 
4629 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
4630 				 info->snd_portid, info->snd_seq, 0,
4631 				 rdev, dev, mac_addr, &sinfo) < 0) {
4632 		nlmsg_free(msg);
4633 		return -ENOBUFS;
4634 	}
4635 
4636 	return genlmsg_reply(msg, info);
4637 }
4638 
4639 int cfg80211_check_station_change(struct wiphy *wiphy,
4640 				  struct station_parameters *params,
4641 				  enum cfg80211_station_type statype)
4642 {
4643 	if (params->listen_interval != -1 &&
4644 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4645 		return -EINVAL;
4646 
4647 	if (params->support_p2p_ps != -1 &&
4648 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4649 		return -EINVAL;
4650 
4651 	if (params->aid &&
4652 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
4653 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4654 		return -EINVAL;
4655 
4656 	/* When you run into this, adjust the code below for the new flag */
4657 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4658 
4659 	switch (statype) {
4660 	case CFG80211_STA_MESH_PEER_KERNEL:
4661 	case CFG80211_STA_MESH_PEER_USER:
4662 		/*
4663 		 * No ignoring the TDLS flag here -- the userspace mesh
4664 		 * code doesn't have the bug of including TDLS in the
4665 		 * mask everywhere.
4666 		 */
4667 		if (params->sta_flags_mask &
4668 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4669 				  BIT(NL80211_STA_FLAG_MFP) |
4670 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
4671 			return -EINVAL;
4672 		break;
4673 	case CFG80211_STA_TDLS_PEER_SETUP:
4674 	case CFG80211_STA_TDLS_PEER_ACTIVE:
4675 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4676 			return -EINVAL;
4677 		/* ignore since it can't change */
4678 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4679 		break;
4680 	default:
4681 		/* disallow mesh-specific things */
4682 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4683 			return -EINVAL;
4684 		if (params->local_pm)
4685 			return -EINVAL;
4686 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4687 			return -EINVAL;
4688 	}
4689 
4690 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4691 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4692 		/* TDLS can't be set, ... */
4693 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4694 			return -EINVAL;
4695 		/*
4696 		 * ... but don't bother the driver with it. This works around
4697 		 * a hostapd/wpa_supplicant issue -- it always includes the
4698 		 * TLDS_PEER flag in the mask even for AP mode.
4699 		 */
4700 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4701 	}
4702 
4703 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4704 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4705 		/* reject other things that can't change */
4706 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4707 			return -EINVAL;
4708 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4709 			return -EINVAL;
4710 		if (params->supported_rates)
4711 			return -EINVAL;
4712 		if (params->ext_capab || params->ht_capa || params->vht_capa)
4713 			return -EINVAL;
4714 	}
4715 
4716 	if (statype != CFG80211_STA_AP_CLIENT &&
4717 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4718 		if (params->vlan)
4719 			return -EINVAL;
4720 	}
4721 
4722 	switch (statype) {
4723 	case CFG80211_STA_AP_MLME_CLIENT:
4724 		/* Use this only for authorizing/unauthorizing a station */
4725 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4726 			return -EOPNOTSUPP;
4727 		break;
4728 	case CFG80211_STA_AP_CLIENT:
4729 	case CFG80211_STA_AP_CLIENT_UNASSOC:
4730 		/* accept only the listed bits */
4731 		if (params->sta_flags_mask &
4732 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4733 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4734 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
4735 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4736 				  BIT(NL80211_STA_FLAG_WME) |
4737 				  BIT(NL80211_STA_FLAG_MFP)))
4738 			return -EINVAL;
4739 
4740 		/* but authenticated/associated only if driver handles it */
4741 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4742 		    params->sta_flags_mask &
4743 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4744 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4745 			return -EINVAL;
4746 		break;
4747 	case CFG80211_STA_IBSS:
4748 	case CFG80211_STA_AP_STA:
4749 		/* reject any changes other than AUTHORIZED */
4750 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4751 			return -EINVAL;
4752 		break;
4753 	case CFG80211_STA_TDLS_PEER_SETUP:
4754 		/* reject any changes other than AUTHORIZED or WME */
4755 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4756 					       BIT(NL80211_STA_FLAG_WME)))
4757 			return -EINVAL;
4758 		/* force (at least) rates when authorizing */
4759 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4760 		    !params->supported_rates)
4761 			return -EINVAL;
4762 		break;
4763 	case CFG80211_STA_TDLS_PEER_ACTIVE:
4764 		/* reject any changes */
4765 		return -EINVAL;
4766 	case CFG80211_STA_MESH_PEER_KERNEL:
4767 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4768 			return -EINVAL;
4769 		break;
4770 	case CFG80211_STA_MESH_PEER_USER:
4771 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4772 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
4773 			return -EINVAL;
4774 		break;
4775 	}
4776 
4777 	/*
4778 	 * Older kernel versions ignored this attribute entirely, so don't
4779 	 * reject attempts to update it but mark it as unused instead so the
4780 	 * driver won't look at the data.
4781 	 */
4782 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
4783 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
4784 		params->opmode_notif_used = false;
4785 
4786 	return 0;
4787 }
4788 EXPORT_SYMBOL(cfg80211_check_station_change);
4789 
4790 /*
4791  * Get vlan interface making sure it is running and on the right wiphy.
4792  */
4793 static struct net_device *get_vlan(struct genl_info *info,
4794 				   struct cfg80211_registered_device *rdev)
4795 {
4796 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
4797 	struct net_device *v;
4798 	int ret;
4799 
4800 	if (!vlanattr)
4801 		return NULL;
4802 
4803 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4804 	if (!v)
4805 		return ERR_PTR(-ENODEV);
4806 
4807 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4808 		ret = -EINVAL;
4809 		goto error;
4810 	}
4811 
4812 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4813 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4814 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4815 		ret = -EINVAL;
4816 		goto error;
4817 	}
4818 
4819 	if (!netif_running(v)) {
4820 		ret = -ENETDOWN;
4821 		goto error;
4822 	}
4823 
4824 	return v;
4825  error:
4826 	dev_put(v);
4827 	return ERR_PTR(ret);
4828 }
4829 
4830 static const struct nla_policy
4831 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
4832 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4833 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4834 };
4835 
4836 static int nl80211_parse_sta_wme(struct genl_info *info,
4837 				 struct station_parameters *params)
4838 {
4839 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4840 	struct nlattr *nla;
4841 	int err;
4842 
4843 	/* parse WME attributes if present */
4844 	if (!info->attrs[NL80211_ATTR_STA_WME])
4845 		return 0;
4846 
4847 	nla = info->attrs[NL80211_ATTR_STA_WME];
4848 	err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4849 			       nl80211_sta_wme_policy, info->extack);
4850 	if (err)
4851 		return err;
4852 
4853 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4854 		params->uapsd_queues = nla_get_u8(
4855 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
4856 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4857 		return -EINVAL;
4858 
4859 	if (tb[NL80211_STA_WME_MAX_SP])
4860 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4861 
4862 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4863 		return -EINVAL;
4864 
4865 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4866 
4867 	return 0;
4868 }
4869 
4870 static int nl80211_parse_sta_channel_info(struct genl_info *info,
4871 				      struct station_parameters *params)
4872 {
4873 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4874 		params->supported_channels =
4875 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4876 		params->supported_channels_len =
4877 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4878 		/*
4879 		 * Need to include at least one (first channel, number of
4880 		 * channels) tuple for each subband, and must have proper
4881 		 * tuples for the rest of the data as well.
4882 		 */
4883 		if (params->supported_channels_len < 2)
4884 			return -EINVAL;
4885 		if (params->supported_channels_len % 2)
4886 			return -EINVAL;
4887 	}
4888 
4889 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4890 		params->supported_oper_classes =
4891 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4892 		params->supported_oper_classes_len =
4893 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4894 		/*
4895 		 * The value of the Length field of the Supported Operating
4896 		 * Classes element is between 2 and 253.
4897 		 */
4898 		if (params->supported_oper_classes_len < 2 ||
4899 		    params->supported_oper_classes_len > 253)
4900 			return -EINVAL;
4901 	}
4902 	return 0;
4903 }
4904 
4905 static int nl80211_set_station_tdls(struct genl_info *info,
4906 				    struct station_parameters *params)
4907 {
4908 	int err;
4909 	/* Dummy STA entry gets updated once the peer capabilities are known */
4910 	if (info->attrs[NL80211_ATTR_PEER_AID])
4911 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4912 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4913 		params->ht_capa =
4914 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4915 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4916 		params->vht_capa =
4917 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4918 
4919 	err = nl80211_parse_sta_channel_info(info, params);
4920 	if (err)
4921 		return err;
4922 
4923 	return nl80211_parse_sta_wme(info, params);
4924 }
4925 
4926 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4927 {
4928 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4929 	struct net_device *dev = info->user_ptr[1];
4930 	struct station_parameters params;
4931 	u8 *mac_addr;
4932 	int err;
4933 
4934 	memset(&params, 0, sizeof(params));
4935 
4936 	if (!rdev->ops->change_station)
4937 		return -EOPNOTSUPP;
4938 
4939 	/*
4940 	 * AID and listen_interval properties can be set only for unassociated
4941 	 * station. Include these parameters here and will check them in
4942 	 * cfg80211_check_station_change().
4943 	 */
4944 	if (info->attrs[NL80211_ATTR_STA_AID])
4945 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4946 
4947 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4948 		params.listen_interval =
4949 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4950 	else
4951 		params.listen_interval = -1;
4952 
4953 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
4954 		u8 tmp;
4955 
4956 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
4957 		if (tmp >= NUM_NL80211_P2P_PS_STATUS)
4958 			return -EINVAL;
4959 
4960 		params.support_p2p_ps = tmp;
4961 	} else {
4962 		params.support_p2p_ps = -1;
4963 	}
4964 
4965 	if (!info->attrs[NL80211_ATTR_MAC])
4966 		return -EINVAL;
4967 
4968 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4969 
4970 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4971 		params.supported_rates =
4972 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4973 		params.supported_rates_len =
4974 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4975 	}
4976 
4977 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4978 		params.capability =
4979 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4980 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4981 	}
4982 
4983 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4984 		params.ext_capab =
4985 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4986 		params.ext_capab_len =
4987 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4988 	}
4989 
4990 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4991 		return -EINVAL;
4992 
4993 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4994 		params.plink_action =
4995 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4996 		if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4997 			return -EINVAL;
4998 	}
4999 
5000 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5001 		params.plink_state =
5002 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5003 		if (params.plink_state >= NUM_NL80211_PLINK_STATES)
5004 			return -EINVAL;
5005 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID]) {
5006 			params.peer_aid = nla_get_u16(
5007 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5008 			if (params.peer_aid > IEEE80211_MAX_AID)
5009 				return -EINVAL;
5010 		}
5011 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5012 	}
5013 
5014 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
5015 		enum nl80211_mesh_power_mode pm = nla_get_u32(
5016 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5017 
5018 		if (pm <= NL80211_MESH_POWER_UNKNOWN ||
5019 		    pm > NL80211_MESH_POWER_MAX)
5020 			return -EINVAL;
5021 
5022 		params.local_pm = pm;
5023 	}
5024 
5025 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5026 		params.opmode_notif_used = true;
5027 		params.opmode_notif =
5028 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5029 	}
5030 
5031 	/* Include parameters for TDLS peer (will check later) */
5032 	err = nl80211_set_station_tdls(info, &params);
5033 	if (err)
5034 		return err;
5035 
5036 	params.vlan = get_vlan(info, rdev);
5037 	if (IS_ERR(params.vlan))
5038 		return PTR_ERR(params.vlan);
5039 
5040 	switch (dev->ieee80211_ptr->iftype) {
5041 	case NL80211_IFTYPE_AP:
5042 	case NL80211_IFTYPE_AP_VLAN:
5043 	case NL80211_IFTYPE_P2P_GO:
5044 	case NL80211_IFTYPE_P2P_CLIENT:
5045 	case NL80211_IFTYPE_STATION:
5046 	case NL80211_IFTYPE_ADHOC:
5047 	case NL80211_IFTYPE_MESH_POINT:
5048 		break;
5049 	default:
5050 		err = -EOPNOTSUPP;
5051 		goto out_put_vlan;
5052 	}
5053 
5054 	/* driver will call cfg80211_check_station_change() */
5055 	err = rdev_change_station(rdev, dev, mac_addr, &params);
5056 
5057  out_put_vlan:
5058 	if (params.vlan)
5059 		dev_put(params.vlan);
5060 
5061 	return err;
5062 }
5063 
5064 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5065 {
5066 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5067 	int err;
5068 	struct net_device *dev = info->user_ptr[1];
5069 	struct station_parameters params;
5070 	u8 *mac_addr = NULL;
5071 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5072 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
5073 
5074 	memset(&params, 0, sizeof(params));
5075 
5076 	if (!rdev->ops->add_station)
5077 		return -EOPNOTSUPP;
5078 
5079 	if (!info->attrs[NL80211_ATTR_MAC])
5080 		return -EINVAL;
5081 
5082 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5083 		return -EINVAL;
5084 
5085 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5086 		return -EINVAL;
5087 
5088 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
5089 	    !info->attrs[NL80211_ATTR_PEER_AID])
5090 		return -EINVAL;
5091 
5092 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5093 	params.supported_rates =
5094 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5095 	params.supported_rates_len =
5096 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5097 	params.listen_interval =
5098 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5099 
5100 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5101 		u8 tmp;
5102 
5103 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5104 		if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5105 			return -EINVAL;
5106 
5107 		params.support_p2p_ps = tmp;
5108 	} else {
5109 		/*
5110 		 * if not specified, assume it's supported for P2P GO interface,
5111 		 * and is NOT supported for AP interface
5112 		 */
5113 		params.support_p2p_ps =
5114 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5115 	}
5116 
5117 	if (info->attrs[NL80211_ATTR_PEER_AID])
5118 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5119 	else
5120 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5121 	if (!params.aid || params.aid > IEEE80211_MAX_AID)
5122 		return -EINVAL;
5123 
5124 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5125 		params.capability =
5126 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5127 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5128 	}
5129 
5130 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5131 		params.ext_capab =
5132 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5133 		params.ext_capab_len =
5134 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5135 	}
5136 
5137 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5138 		params.ht_capa =
5139 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5140 
5141 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5142 		params.vht_capa =
5143 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5144 
5145 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5146 		params.opmode_notif_used = true;
5147 		params.opmode_notif =
5148 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5149 	}
5150 
5151 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5152 		params.plink_action =
5153 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5154 		if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5155 			return -EINVAL;
5156 	}
5157 
5158 	err = nl80211_parse_sta_channel_info(info, &params);
5159 	if (err)
5160 		return err;
5161 
5162 	err = nl80211_parse_sta_wme(info, &params);
5163 	if (err)
5164 		return err;
5165 
5166 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5167 		return -EINVAL;
5168 
5169 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5170 	 * as userspace might just pass through the capabilities from the IEs
5171 	 * directly, rather than enforcing this restriction and returning an
5172 	 * error in this case.
5173 	 */
5174 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5175 		params.ht_capa = NULL;
5176 		params.vht_capa = NULL;
5177 	}
5178 
5179 	/* When you run into this, adjust the code below for the new flag */
5180 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5181 
5182 	switch (dev->ieee80211_ptr->iftype) {
5183 	case NL80211_IFTYPE_AP:
5184 	case NL80211_IFTYPE_AP_VLAN:
5185 	case NL80211_IFTYPE_P2P_GO:
5186 		/* ignore WME attributes if iface/sta is not capable */
5187 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5188 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5189 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5190 
5191 		/* TDLS peers cannot be added */
5192 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5193 		    info->attrs[NL80211_ATTR_PEER_AID])
5194 			return -EINVAL;
5195 		/* but don't bother the driver with it */
5196 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5197 
5198 		/* allow authenticated/associated only if driver handles it */
5199 		if (!(rdev->wiphy.features &
5200 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5201 		    params.sta_flags_mask & auth_assoc)
5202 			return -EINVAL;
5203 
5204 		/* Older userspace, or userspace wanting to be compatible with
5205 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5206 		 * and assoc flags in the mask, but assumes the station will be
5207 		 * added as associated anyway since this was the required driver
5208 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5209 		 * introduced.
5210 		 * In order to not bother drivers with this quirk in the API
5211 		 * set the flags in both the mask and set for new stations in
5212 		 * this case.
5213 		 */
5214 		if (!(params.sta_flags_mask & auth_assoc)) {
5215 			params.sta_flags_mask |= auth_assoc;
5216 			params.sta_flags_set |= auth_assoc;
5217 		}
5218 
5219 		/* must be last in here for error handling */
5220 		params.vlan = get_vlan(info, rdev);
5221 		if (IS_ERR(params.vlan))
5222 			return PTR_ERR(params.vlan);
5223 		break;
5224 	case NL80211_IFTYPE_MESH_POINT:
5225 		/* ignore uAPSD data */
5226 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5227 
5228 		/* associated is disallowed */
5229 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5230 			return -EINVAL;
5231 		/* TDLS peers cannot be added */
5232 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5233 		    info->attrs[NL80211_ATTR_PEER_AID])
5234 			return -EINVAL;
5235 		break;
5236 	case NL80211_IFTYPE_STATION:
5237 	case NL80211_IFTYPE_P2P_CLIENT:
5238 		/* ignore uAPSD data */
5239 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5240 
5241 		/* these are disallowed */
5242 		if (params.sta_flags_mask &
5243 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
5244 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
5245 			return -EINVAL;
5246 		/* Only TDLS peers can be added */
5247 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5248 			return -EINVAL;
5249 		/* Can only add if TDLS ... */
5250 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5251 			return -EOPNOTSUPP;
5252 		/* ... with external setup is supported */
5253 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5254 			return -EOPNOTSUPP;
5255 		/*
5256 		 * Older wpa_supplicant versions always mark the TDLS peer
5257 		 * as authorized, but it shouldn't yet be.
5258 		 */
5259 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
5260 		break;
5261 	default:
5262 		return -EOPNOTSUPP;
5263 	}
5264 
5265 	/* be aware of params.vlan when changing code here */
5266 
5267 	err = rdev_add_station(rdev, dev, mac_addr, &params);
5268 
5269 	if (params.vlan)
5270 		dev_put(params.vlan);
5271 	return err;
5272 }
5273 
5274 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5275 {
5276 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5277 	struct net_device *dev = info->user_ptr[1];
5278 	struct station_del_parameters params;
5279 
5280 	memset(&params, 0, sizeof(params));
5281 
5282 	if (info->attrs[NL80211_ATTR_MAC])
5283 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
5284 
5285 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5286 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5287 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5288 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5289 		return -EINVAL;
5290 
5291 	if (!rdev->ops->del_station)
5292 		return -EOPNOTSUPP;
5293 
5294 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
5295 		params.subtype =
5296 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
5297 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
5298 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
5299 			return -EINVAL;
5300 	} else {
5301 		/* Default to Deauthentication frame */
5302 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
5303 	}
5304 
5305 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
5306 		params.reason_code =
5307 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5308 		if (params.reason_code == 0)
5309 			return -EINVAL; /* 0 is reserved */
5310 	} else {
5311 		/* Default to reason code 2 */
5312 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
5313 	}
5314 
5315 	return rdev_del_station(rdev, dev, &params);
5316 }
5317 
5318 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
5319 				int flags, struct net_device *dev,
5320 				u8 *dst, u8 *next_hop,
5321 				struct mpath_info *pinfo)
5322 {
5323 	void *hdr;
5324 	struct nlattr *pinfoattr;
5325 
5326 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
5327 	if (!hdr)
5328 		return -1;
5329 
5330 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5331 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
5332 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
5333 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
5334 		goto nla_put_failure;
5335 
5336 	pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
5337 	if (!pinfoattr)
5338 		goto nla_put_failure;
5339 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
5340 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
5341 			pinfo->frame_qlen))
5342 		goto nla_put_failure;
5343 	if (((pinfo->filled & MPATH_INFO_SN) &&
5344 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
5345 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
5346 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
5347 			 pinfo->metric)) ||
5348 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
5349 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
5350 			 pinfo->exptime)) ||
5351 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
5352 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
5353 			pinfo->flags)) ||
5354 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
5355 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
5356 			 pinfo->discovery_timeout)) ||
5357 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
5358 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
5359 			pinfo->discovery_retries)))
5360 		goto nla_put_failure;
5361 
5362 	nla_nest_end(msg, pinfoattr);
5363 
5364 	genlmsg_end(msg, hdr);
5365 	return 0;
5366 
5367  nla_put_failure:
5368 	genlmsg_cancel(msg, hdr);
5369 	return -EMSGSIZE;
5370 }
5371 
5372 static int nl80211_dump_mpath(struct sk_buff *skb,
5373 			      struct netlink_callback *cb)
5374 {
5375 	struct mpath_info pinfo;
5376 	struct cfg80211_registered_device *rdev;
5377 	struct wireless_dev *wdev;
5378 	u8 dst[ETH_ALEN];
5379 	u8 next_hop[ETH_ALEN];
5380 	int path_idx = cb->args[2];
5381 	int err;
5382 
5383 	rtnl_lock();
5384 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5385 	if (err)
5386 		goto out_err;
5387 
5388 	if (!rdev->ops->dump_mpath) {
5389 		err = -EOPNOTSUPP;
5390 		goto out_err;
5391 	}
5392 
5393 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5394 		err = -EOPNOTSUPP;
5395 		goto out_err;
5396 	}
5397 
5398 	while (1) {
5399 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
5400 				      next_hop, &pinfo);
5401 		if (err == -ENOENT)
5402 			break;
5403 		if (err)
5404 			goto out_err;
5405 
5406 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5407 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
5408 				       wdev->netdev, dst, next_hop,
5409 				       &pinfo) < 0)
5410 			goto out;
5411 
5412 		path_idx++;
5413 	}
5414 
5415  out:
5416 	cb->args[2] = path_idx;
5417 	err = skb->len;
5418  out_err:
5419 	rtnl_unlock();
5420 	return err;
5421 }
5422 
5423 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
5424 {
5425 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5426 	int err;
5427 	struct net_device *dev = info->user_ptr[1];
5428 	struct mpath_info pinfo;
5429 	struct sk_buff *msg;
5430 	u8 *dst = NULL;
5431 	u8 next_hop[ETH_ALEN];
5432 
5433 	memset(&pinfo, 0, sizeof(pinfo));
5434 
5435 	if (!info->attrs[NL80211_ATTR_MAC])
5436 		return -EINVAL;
5437 
5438 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5439 
5440 	if (!rdev->ops->get_mpath)
5441 		return -EOPNOTSUPP;
5442 
5443 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5444 		return -EOPNOTSUPP;
5445 
5446 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
5447 	if (err)
5448 		return err;
5449 
5450 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5451 	if (!msg)
5452 		return -ENOMEM;
5453 
5454 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5455 				 dev, dst, next_hop, &pinfo) < 0) {
5456 		nlmsg_free(msg);
5457 		return -ENOBUFS;
5458 	}
5459 
5460 	return genlmsg_reply(msg, info);
5461 }
5462 
5463 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
5464 {
5465 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5466 	struct net_device *dev = info->user_ptr[1];
5467 	u8 *dst = NULL;
5468 	u8 *next_hop = NULL;
5469 
5470 	if (!info->attrs[NL80211_ATTR_MAC])
5471 		return -EINVAL;
5472 
5473 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5474 		return -EINVAL;
5475 
5476 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5477 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5478 
5479 	if (!rdev->ops->change_mpath)
5480 		return -EOPNOTSUPP;
5481 
5482 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5483 		return -EOPNOTSUPP;
5484 
5485 	return rdev_change_mpath(rdev, dev, dst, next_hop);
5486 }
5487 
5488 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
5489 {
5490 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5491 	struct net_device *dev = info->user_ptr[1];
5492 	u8 *dst = NULL;
5493 	u8 *next_hop = NULL;
5494 
5495 	if (!info->attrs[NL80211_ATTR_MAC])
5496 		return -EINVAL;
5497 
5498 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5499 		return -EINVAL;
5500 
5501 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5502 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5503 
5504 	if (!rdev->ops->add_mpath)
5505 		return -EOPNOTSUPP;
5506 
5507 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5508 		return -EOPNOTSUPP;
5509 
5510 	return rdev_add_mpath(rdev, dev, dst, next_hop);
5511 }
5512 
5513 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
5514 {
5515 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5516 	struct net_device *dev = info->user_ptr[1];
5517 	u8 *dst = NULL;
5518 
5519 	if (info->attrs[NL80211_ATTR_MAC])
5520 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5521 
5522 	if (!rdev->ops->del_mpath)
5523 		return -EOPNOTSUPP;
5524 
5525 	return rdev_del_mpath(rdev, dev, dst);
5526 }
5527 
5528 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
5529 {
5530 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5531 	int err;
5532 	struct net_device *dev = info->user_ptr[1];
5533 	struct mpath_info pinfo;
5534 	struct sk_buff *msg;
5535 	u8 *dst = NULL;
5536 	u8 mpp[ETH_ALEN];
5537 
5538 	memset(&pinfo, 0, sizeof(pinfo));
5539 
5540 	if (!info->attrs[NL80211_ATTR_MAC])
5541 		return -EINVAL;
5542 
5543 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5544 
5545 	if (!rdev->ops->get_mpp)
5546 		return -EOPNOTSUPP;
5547 
5548 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5549 		return -EOPNOTSUPP;
5550 
5551 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
5552 	if (err)
5553 		return err;
5554 
5555 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5556 	if (!msg)
5557 		return -ENOMEM;
5558 
5559 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5560 			       dev, dst, mpp, &pinfo) < 0) {
5561 		nlmsg_free(msg);
5562 		return -ENOBUFS;
5563 	}
5564 
5565 	return genlmsg_reply(msg, info);
5566 }
5567 
5568 static int nl80211_dump_mpp(struct sk_buff *skb,
5569 			    struct netlink_callback *cb)
5570 {
5571 	struct mpath_info pinfo;
5572 	struct cfg80211_registered_device *rdev;
5573 	struct wireless_dev *wdev;
5574 	u8 dst[ETH_ALEN];
5575 	u8 mpp[ETH_ALEN];
5576 	int path_idx = cb->args[2];
5577 	int err;
5578 
5579 	rtnl_lock();
5580 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5581 	if (err)
5582 		goto out_err;
5583 
5584 	if (!rdev->ops->dump_mpp) {
5585 		err = -EOPNOTSUPP;
5586 		goto out_err;
5587 	}
5588 
5589 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5590 		err = -EOPNOTSUPP;
5591 		goto out_err;
5592 	}
5593 
5594 	while (1) {
5595 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
5596 				    mpp, &pinfo);
5597 		if (err == -ENOENT)
5598 			break;
5599 		if (err)
5600 			goto out_err;
5601 
5602 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5603 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
5604 				       wdev->netdev, dst, mpp,
5605 				       &pinfo) < 0)
5606 			goto out;
5607 
5608 		path_idx++;
5609 	}
5610 
5611  out:
5612 	cb->args[2] = path_idx;
5613 	err = skb->len;
5614  out_err:
5615 	rtnl_unlock();
5616 	return err;
5617 }
5618 
5619 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
5620 {
5621 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5622 	struct net_device *dev = info->user_ptr[1];
5623 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5624 	struct bss_parameters params;
5625 	int err;
5626 
5627 	memset(&params, 0, sizeof(params));
5628 	/* default to not changing parameters */
5629 	params.use_cts_prot = -1;
5630 	params.use_short_preamble = -1;
5631 	params.use_short_slot_time = -1;
5632 	params.ap_isolate = -1;
5633 	params.ht_opmode = -1;
5634 	params.p2p_ctwindow = -1;
5635 	params.p2p_opp_ps = -1;
5636 
5637 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
5638 		params.use_cts_prot =
5639 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
5640 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
5641 		params.use_short_preamble =
5642 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
5643 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
5644 		params.use_short_slot_time =
5645 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
5646 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5647 		params.basic_rates =
5648 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5649 		params.basic_rates_len =
5650 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5651 	}
5652 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
5653 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
5654 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
5655 		params.ht_opmode =
5656 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
5657 
5658 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5659 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5660 			return -EINVAL;
5661 		params.p2p_ctwindow =
5662 			nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5663 		if (params.p2p_ctwindow < 0)
5664 			return -EINVAL;
5665 		if (params.p2p_ctwindow != 0 &&
5666 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5667 			return -EINVAL;
5668 	}
5669 
5670 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5671 		u8 tmp;
5672 
5673 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5674 			return -EINVAL;
5675 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5676 		if (tmp > 1)
5677 			return -EINVAL;
5678 		params.p2p_opp_ps = tmp;
5679 		if (params.p2p_opp_ps &&
5680 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5681 			return -EINVAL;
5682 	}
5683 
5684 	if (!rdev->ops->change_bss)
5685 		return -EOPNOTSUPP;
5686 
5687 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5688 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5689 		return -EOPNOTSUPP;
5690 
5691 	wdev_lock(wdev);
5692 	err = rdev_change_bss(rdev, dev, &params);
5693 	wdev_unlock(wdev);
5694 
5695 	return err;
5696 }
5697 
5698 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
5699 {
5700 	char *data = NULL;
5701 	bool is_indoor;
5702 	enum nl80211_user_reg_hint_type user_reg_hint_type;
5703 	u32 owner_nlportid;
5704 
5705 	/*
5706 	 * You should only get this when cfg80211 hasn't yet initialized
5707 	 * completely when built-in to the kernel right between the time
5708 	 * window between nl80211_init() and regulatory_init(), if that is
5709 	 * even possible.
5710 	 */
5711 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
5712 		return -EINPROGRESS;
5713 
5714 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
5715 		user_reg_hint_type =
5716 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
5717 	else
5718 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
5719 
5720 	switch (user_reg_hint_type) {
5721 	case NL80211_USER_REG_HINT_USER:
5722 	case NL80211_USER_REG_HINT_CELL_BASE:
5723 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5724 			return -EINVAL;
5725 
5726 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5727 		return regulatory_hint_user(data, user_reg_hint_type);
5728 	case NL80211_USER_REG_HINT_INDOOR:
5729 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5730 			owner_nlportid = info->snd_portid;
5731 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
5732 		} else {
5733 			owner_nlportid = 0;
5734 			is_indoor = true;
5735 		}
5736 
5737 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
5738 	default:
5739 		return -EINVAL;
5740 	}
5741 }
5742 
5743 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
5744 {
5745 	return reg_reload_regdb();
5746 }
5747 
5748 static int nl80211_get_mesh_config(struct sk_buff *skb,
5749 				   struct genl_info *info)
5750 {
5751 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5752 	struct net_device *dev = info->user_ptr[1];
5753 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5754 	struct mesh_config cur_params;
5755 	int err = 0;
5756 	void *hdr;
5757 	struct nlattr *pinfoattr;
5758 	struct sk_buff *msg;
5759 
5760 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5761 		return -EOPNOTSUPP;
5762 
5763 	if (!rdev->ops->get_mesh_config)
5764 		return -EOPNOTSUPP;
5765 
5766 	wdev_lock(wdev);
5767 	/* If not connected, get default parameters */
5768 	if (!wdev->mesh_id_len)
5769 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5770 	else
5771 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
5772 	wdev_unlock(wdev);
5773 
5774 	if (err)
5775 		return err;
5776 
5777 	/* Draw up a netlink message to send back */
5778 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5779 	if (!msg)
5780 		return -ENOMEM;
5781 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5782 			     NL80211_CMD_GET_MESH_CONFIG);
5783 	if (!hdr)
5784 		goto out;
5785 	pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
5786 	if (!pinfoattr)
5787 		goto nla_put_failure;
5788 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5789 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5790 			cur_params.dot11MeshRetryTimeout) ||
5791 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5792 			cur_params.dot11MeshConfirmTimeout) ||
5793 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5794 			cur_params.dot11MeshHoldingTimeout) ||
5795 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5796 			cur_params.dot11MeshMaxPeerLinks) ||
5797 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5798 		       cur_params.dot11MeshMaxRetries) ||
5799 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
5800 		       cur_params.dot11MeshTTL) ||
5801 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5802 		       cur_params.element_ttl) ||
5803 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5804 		       cur_params.auto_open_plinks) ||
5805 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5806 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
5807 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5808 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
5809 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5810 			cur_params.path_refresh_time) ||
5811 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5812 			cur_params.min_discovery_timeout) ||
5813 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5814 			cur_params.dot11MeshHWMPactivePathTimeout) ||
5815 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5816 			cur_params.dot11MeshHWMPpreqMinInterval) ||
5817 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5818 			cur_params.dot11MeshHWMPperrMinInterval) ||
5819 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5820 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5821 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5822 		       cur_params.dot11MeshHWMPRootMode) ||
5823 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5824 			cur_params.dot11MeshHWMPRannInterval) ||
5825 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5826 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
5827 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5828 		       cur_params.dot11MeshForwarding) ||
5829 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
5830 			cur_params.rssi_threshold) ||
5831 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
5832 			cur_params.ht_opmode) ||
5833 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5834 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5835 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5836 			cur_params.dot11MeshHWMProotInterval) ||
5837 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5838 			cur_params.dot11MeshHWMPconfirmationInterval) ||
5839 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5840 			cur_params.power_mode) ||
5841 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
5842 			cur_params.dot11MeshAwakeWindowDuration) ||
5843 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5844 			cur_params.plink_timeout))
5845 		goto nla_put_failure;
5846 	nla_nest_end(msg, pinfoattr);
5847 	genlmsg_end(msg, hdr);
5848 	return genlmsg_reply(msg, info);
5849 
5850  nla_put_failure:
5851 	genlmsg_cancel(msg, hdr);
5852  out:
5853 	nlmsg_free(msg);
5854 	return -ENOBUFS;
5855 }
5856 
5857 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
5858 	[NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5859 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5860 	[NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5861 	[NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5862 	[NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5863 	[NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
5864 	[NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
5865 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
5866 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
5867 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5868 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5869 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5870 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5871 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
5872 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
5873 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
5874 	[NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
5875 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
5876 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
5877 	[NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
5878 	[NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5879 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
5880 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5881 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
5882 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
5883 	[NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5884 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
5885 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
5886 };
5887 
5888 static const struct nla_policy
5889 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
5890 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
5891 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5892 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
5893 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
5894 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
5895 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
5896 	[NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
5897 				    .len = IEEE80211_MAX_DATA_LEN },
5898 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
5899 };
5900 
5901 static int nl80211_check_bool(const struct nlattr *nla, u8 min, u8 max, bool *out)
5902 {
5903 	u8 val = nla_get_u8(nla);
5904 	if (val < min || val > max)
5905 		return -EINVAL;
5906 	*out = val;
5907 	return 0;
5908 }
5909 
5910 static int nl80211_check_u8(const struct nlattr *nla, u8 min, u8 max, u8 *out)
5911 {
5912 	u8 val = nla_get_u8(nla);
5913 	if (val < min || val > max)
5914 		return -EINVAL;
5915 	*out = val;
5916 	return 0;
5917 }
5918 
5919 static int nl80211_check_u16(const struct nlattr *nla, u16 min, u16 max, u16 *out)
5920 {
5921 	u16 val = nla_get_u16(nla);
5922 	if (val < min || val > max)
5923 		return -EINVAL;
5924 	*out = val;
5925 	return 0;
5926 }
5927 
5928 static int nl80211_check_u32(const struct nlattr *nla, u32 min, u32 max, u32 *out)
5929 {
5930 	u32 val = nla_get_u32(nla);
5931 	if (val < min || val > max)
5932 		return -EINVAL;
5933 	*out = val;
5934 	return 0;
5935 }
5936 
5937 static int nl80211_check_s32(const struct nlattr *nla, s32 min, s32 max, s32 *out)
5938 {
5939 	s32 val = nla_get_s32(nla);
5940 	if (val < min || val > max)
5941 		return -EINVAL;
5942 	*out = val;
5943 	return 0;
5944 }
5945 
5946 static int nl80211_check_power_mode(const struct nlattr *nla,
5947 				    enum nl80211_mesh_power_mode min,
5948 				    enum nl80211_mesh_power_mode max,
5949 				    enum nl80211_mesh_power_mode *out)
5950 {
5951 	u32 val = nla_get_u32(nla);
5952 	if (val < min || val > max)
5953 		return -EINVAL;
5954 	*out = val;
5955 	return 0;
5956 }
5957 
5958 static int nl80211_parse_mesh_config(struct genl_info *info,
5959 				     struct mesh_config *cfg,
5960 				     u32 *mask_out)
5961 {
5962 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
5963 	u32 mask = 0;
5964 	u16 ht_opmode;
5965 
5966 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5967 do {									    \
5968 	if (tb[attr]) {							    \
5969 		if (fn(tb[attr], min, max, &cfg->param))		    \
5970 			return -EINVAL;					    \
5971 		mask |= (1 << (attr - 1));				    \
5972 	}								    \
5973 } while (0)
5974 
5975 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
5976 		return -EINVAL;
5977 	if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
5978 			     info->attrs[NL80211_ATTR_MESH_CONFIG],
5979 			     nl80211_meshconf_params_policy, info->extack))
5980 		return -EINVAL;
5981 
5982 	/* This makes sure that there aren't more than 32 mesh config
5983 	 * parameters (otherwise our bitfield scheme would not work.) */
5984 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5985 
5986 	/* Fill in the params struct */
5987 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
5988 				  mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5989 				  nl80211_check_u16);
5990 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
5991 				  mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5992 				  nl80211_check_u16);
5993 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
5994 				  mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5995 				  nl80211_check_u16);
5996 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
5997 				  mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5998 				  nl80211_check_u16);
5999 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
6000 				  mask, NL80211_MESHCONF_MAX_RETRIES,
6001 				  nl80211_check_u8);
6002 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
6003 				  mask, NL80211_MESHCONF_TTL, nl80211_check_u8);
6004 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
6005 				  mask, NL80211_MESHCONF_ELEMENT_TTL,
6006 				  nl80211_check_u8);
6007 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
6008 				  mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6009 				  nl80211_check_bool);
6010 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6011 				  1, 255, mask,
6012 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6013 				  nl80211_check_u32);
6014 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
6015 				  mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6016 				  nl80211_check_u8);
6017 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
6018 				  mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
6019 				  nl80211_check_u32);
6020 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
6021 				  mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6022 				  nl80211_check_u16);
6023 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6024 				  1, 65535, mask,
6025 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6026 				  nl80211_check_u32);
6027 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
6028 				  1, 65535, mask,
6029 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6030 				  nl80211_check_u16);
6031 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
6032 				  1, 65535, mask,
6033 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6034 				  nl80211_check_u16);
6035 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6036 				  dot11MeshHWMPnetDiameterTraversalTime,
6037 				  1, 65535, mask,
6038 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6039 				  nl80211_check_u16);
6040 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
6041 				  mask, NL80211_MESHCONF_HWMP_ROOTMODE,
6042 				  nl80211_check_u8);
6043 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
6044 				  mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6045 				  nl80211_check_u16);
6046 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6047 				  dot11MeshGateAnnouncementProtocol, 0, 1,
6048 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6049 				  nl80211_check_bool);
6050 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
6051 				  mask, NL80211_MESHCONF_FORWARDING,
6052 				  nl80211_check_bool);
6053 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
6054 				  mask, NL80211_MESHCONF_RSSI_THRESHOLD,
6055 				  nl80211_check_s32);
6056 	/*
6057 	 * Check HT operation mode based on
6058 	 * IEEE 802.11 2012 8.4.2.59 HT Operation element.
6059 	 */
6060 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6061 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6062 
6063 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6064 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6065 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6066 			return -EINVAL;
6067 
6068 		if ((ht_opmode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT) &&
6069 		    (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6070 			return -EINVAL;
6071 
6072 		switch (ht_opmode & IEEE80211_HT_OP_MODE_PROTECTION) {
6073 		case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
6074 		case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
6075 			if (ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT)
6076 				return -EINVAL;
6077 			break;
6078 		case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
6079 		case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
6080 			if (!(ht_opmode & IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6081 				return -EINVAL;
6082 			break;
6083 		}
6084 		cfg->ht_opmode = ht_opmode;
6085 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6086 	}
6087 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
6088 				  1, 65535, mask,
6089 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6090 				  nl80211_check_u32);
6091 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
6092 				  mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6093 				  nl80211_check_u16);
6094 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6095 				  dot11MeshHWMPconfirmationInterval,
6096 				  1, 65535, mask,
6097 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6098 				  nl80211_check_u16);
6099 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
6100 				  NL80211_MESH_POWER_ACTIVE,
6101 				  NL80211_MESH_POWER_MAX,
6102 				  mask, NL80211_MESHCONF_POWER_MODE,
6103 				  nl80211_check_power_mode);
6104 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
6105 				  0, 65535, mask,
6106 				  NL80211_MESHCONF_AWAKE_WINDOW, nl80211_check_u16);
6107 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
6108 				  mask, NL80211_MESHCONF_PLINK_TIMEOUT,
6109 				  nl80211_check_u32);
6110 	if (mask_out)
6111 		*mask_out = mask;
6112 
6113 	return 0;
6114 
6115 #undef FILL_IN_MESH_PARAM_IF_SET
6116 }
6117 
6118 static int nl80211_parse_mesh_setup(struct genl_info *info,
6119 				     struct mesh_setup *setup)
6120 {
6121 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6122 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6123 
6124 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6125 		return -EINVAL;
6126 	if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
6127 			     info->attrs[NL80211_ATTR_MESH_SETUP],
6128 			     nl80211_mesh_setup_params_policy, info->extack))
6129 		return -EINVAL;
6130 
6131 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6132 		setup->sync_method =
6133 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6134 		 IEEE80211_SYNC_METHOD_VENDOR :
6135 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6136 
6137 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6138 		setup->path_sel_proto =
6139 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6140 		 IEEE80211_PATH_PROTOCOL_VENDOR :
6141 		 IEEE80211_PATH_PROTOCOL_HWMP;
6142 
6143 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6144 		setup->path_metric =
6145 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6146 		 IEEE80211_PATH_METRIC_VENDOR :
6147 		 IEEE80211_PATH_METRIC_AIRTIME;
6148 
6149 	if (tb[NL80211_MESH_SETUP_IE]) {
6150 		struct nlattr *ieattr =
6151 			tb[NL80211_MESH_SETUP_IE];
6152 		if (!is_valid_ie_attr(ieattr))
6153 			return -EINVAL;
6154 		setup->ie = nla_data(ieattr);
6155 		setup->ie_len = nla_len(ieattr);
6156 	}
6157 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6158 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6159 		return -EINVAL;
6160 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6161 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6162 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6163 	if (setup->is_secure)
6164 		setup->user_mpm = true;
6165 
6166 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6167 		if (!setup->user_mpm)
6168 			return -EINVAL;
6169 		setup->auth_id =
6170 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6171 	}
6172 
6173 	return 0;
6174 }
6175 
6176 static int nl80211_update_mesh_config(struct sk_buff *skb,
6177 				      struct genl_info *info)
6178 {
6179 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6180 	struct net_device *dev = info->user_ptr[1];
6181 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6182 	struct mesh_config cfg;
6183 	u32 mask;
6184 	int err;
6185 
6186 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6187 		return -EOPNOTSUPP;
6188 
6189 	if (!rdev->ops->update_mesh_config)
6190 		return -EOPNOTSUPP;
6191 
6192 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
6193 	if (err)
6194 		return err;
6195 
6196 	wdev_lock(wdev);
6197 	if (!wdev->mesh_id_len)
6198 		err = -ENOLINK;
6199 
6200 	if (!err)
6201 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6202 
6203 	wdev_unlock(wdev);
6204 
6205 	return err;
6206 }
6207 
6208 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6209 			      struct sk_buff *msg)
6210 {
6211 	struct nlattr *nl_reg_rules;
6212 	unsigned int i;
6213 
6214 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6215 	    (regdom->dfs_region &&
6216 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6217 		goto nla_put_failure;
6218 
6219 	nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
6220 	if (!nl_reg_rules)
6221 		goto nla_put_failure;
6222 
6223 	for (i = 0; i < regdom->n_reg_rules; i++) {
6224 		struct nlattr *nl_reg_rule;
6225 		const struct ieee80211_reg_rule *reg_rule;
6226 		const struct ieee80211_freq_range *freq_range;
6227 		const struct ieee80211_power_rule *power_rule;
6228 		unsigned int max_bandwidth_khz;
6229 
6230 		reg_rule = &regdom->reg_rules[i];
6231 		freq_range = &reg_rule->freq_range;
6232 		power_rule = &reg_rule->power_rule;
6233 
6234 		nl_reg_rule = nla_nest_start(msg, i);
6235 		if (!nl_reg_rule)
6236 			goto nla_put_failure;
6237 
6238 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
6239 		if (!max_bandwidth_khz)
6240 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6241 								  reg_rule);
6242 
6243 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6244 				reg_rule->flags) ||
6245 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6246 				freq_range->start_freq_khz) ||
6247 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6248 				freq_range->end_freq_khz) ||
6249 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6250 				max_bandwidth_khz) ||
6251 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6252 				power_rule->max_antenna_gain) ||
6253 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6254 				power_rule->max_eirp) ||
6255 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6256 				reg_rule->dfs_cac_ms))
6257 			goto nla_put_failure;
6258 
6259 		nla_nest_end(msg, nl_reg_rule);
6260 	}
6261 
6262 	nla_nest_end(msg, nl_reg_rules);
6263 	return 0;
6264 
6265 nla_put_failure:
6266 	return -EMSGSIZE;
6267 }
6268 
6269 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6270 {
6271 	const struct ieee80211_regdomain *regdom = NULL;
6272 	struct cfg80211_registered_device *rdev;
6273 	struct wiphy *wiphy = NULL;
6274 	struct sk_buff *msg;
6275 	void *hdr;
6276 
6277 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6278 	if (!msg)
6279 		return -ENOBUFS;
6280 
6281 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6282 			     NL80211_CMD_GET_REG);
6283 	if (!hdr)
6284 		goto put_failure;
6285 
6286 	if (info->attrs[NL80211_ATTR_WIPHY]) {
6287 		bool self_managed;
6288 
6289 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6290 		if (IS_ERR(rdev)) {
6291 			nlmsg_free(msg);
6292 			return PTR_ERR(rdev);
6293 		}
6294 
6295 		wiphy = &rdev->wiphy;
6296 		self_managed = wiphy->regulatory_flags &
6297 			       REGULATORY_WIPHY_SELF_MANAGED;
6298 		regdom = get_wiphy_regdom(wiphy);
6299 
6300 		/* a self-managed-reg device must have a private regdom */
6301 		if (WARN_ON(!regdom && self_managed)) {
6302 			nlmsg_free(msg);
6303 			return -EINVAL;
6304 		}
6305 
6306 		if (regdom &&
6307 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6308 			goto nla_put_failure;
6309 	}
6310 
6311 	if (!wiphy && reg_last_request_cell_base() &&
6312 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6313 			NL80211_USER_REG_HINT_CELL_BASE))
6314 		goto nla_put_failure;
6315 
6316 	rcu_read_lock();
6317 
6318 	if (!regdom)
6319 		regdom = rcu_dereference(cfg80211_regdomain);
6320 
6321 	if (nl80211_put_regdom(regdom, msg))
6322 		goto nla_put_failure_rcu;
6323 
6324 	rcu_read_unlock();
6325 
6326 	genlmsg_end(msg, hdr);
6327 	return genlmsg_reply(msg, info);
6328 
6329 nla_put_failure_rcu:
6330 	rcu_read_unlock();
6331 nla_put_failure:
6332 	genlmsg_cancel(msg, hdr);
6333 put_failure:
6334 	nlmsg_free(msg);
6335 	return -EMSGSIZE;
6336 }
6337 
6338 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6339 			       u32 seq, int flags, struct wiphy *wiphy,
6340 			       const struct ieee80211_regdomain *regdom)
6341 {
6342 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6343 				   NL80211_CMD_GET_REG);
6344 
6345 	if (!hdr)
6346 		return -1;
6347 
6348 	genl_dump_check_consistent(cb, hdr);
6349 
6350 	if (nl80211_put_regdom(regdom, msg))
6351 		goto nla_put_failure;
6352 
6353 	if (!wiphy && reg_last_request_cell_base() &&
6354 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6355 			NL80211_USER_REG_HINT_CELL_BASE))
6356 		goto nla_put_failure;
6357 
6358 	if (wiphy &&
6359 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6360 		goto nla_put_failure;
6361 
6362 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
6363 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
6364 		goto nla_put_failure;
6365 
6366 	genlmsg_end(msg, hdr);
6367 	return 0;
6368 
6369 nla_put_failure:
6370 	genlmsg_cancel(msg, hdr);
6371 	return -EMSGSIZE;
6372 }
6373 
6374 static int nl80211_get_reg_dump(struct sk_buff *skb,
6375 				struct netlink_callback *cb)
6376 {
6377 	const struct ieee80211_regdomain *regdom = NULL;
6378 	struct cfg80211_registered_device *rdev;
6379 	int err, reg_idx, start = cb->args[2];
6380 
6381 	rtnl_lock();
6382 
6383 	if (cfg80211_regdomain && start == 0) {
6384 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6385 					  NLM_F_MULTI, NULL,
6386 					  rtnl_dereference(cfg80211_regdomain));
6387 		if (err < 0)
6388 			goto out_err;
6389 	}
6390 
6391 	/* the global regdom is idx 0 */
6392 	reg_idx = 1;
6393 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
6394 		regdom = get_wiphy_regdom(&rdev->wiphy);
6395 		if (!regdom)
6396 			continue;
6397 
6398 		if (++reg_idx <= start)
6399 			continue;
6400 
6401 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6402 					  NLM_F_MULTI, &rdev->wiphy, regdom);
6403 		if (err < 0) {
6404 			reg_idx--;
6405 			break;
6406 		}
6407 	}
6408 
6409 	cb->args[2] = reg_idx;
6410 	err = skb->len;
6411 out_err:
6412 	rtnl_unlock();
6413 	return err;
6414 }
6415 
6416 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
6417 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
6418 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
6419 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
6420 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
6421 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
6422 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
6423 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
6424 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
6425 };
6426 
6427 static int parse_reg_rule(struct nlattr *tb[],
6428 	struct ieee80211_reg_rule *reg_rule)
6429 {
6430 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
6431 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
6432 
6433 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
6434 		return -EINVAL;
6435 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
6436 		return -EINVAL;
6437 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
6438 		return -EINVAL;
6439 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6440 		return -EINVAL;
6441 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6442 		return -EINVAL;
6443 
6444 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
6445 
6446 	freq_range->start_freq_khz =
6447 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
6448 	freq_range->end_freq_khz =
6449 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
6450 	freq_range->max_bandwidth_khz =
6451 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
6452 
6453 	power_rule->max_eirp =
6454 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
6455 
6456 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
6457 		power_rule->max_antenna_gain =
6458 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
6459 
6460 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
6461 		reg_rule->dfs_cac_ms =
6462 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
6463 
6464 	return 0;
6465 }
6466 
6467 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
6468 {
6469 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
6470 	struct nlattr *nl_reg_rule;
6471 	char *alpha2;
6472 	int rem_reg_rules, r;
6473 	u32 num_rules = 0, rule_idx = 0, size_of_regd;
6474 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
6475 	struct ieee80211_regdomain *rd;
6476 
6477 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6478 		return -EINVAL;
6479 
6480 	if (!info->attrs[NL80211_ATTR_REG_RULES])
6481 		return -EINVAL;
6482 
6483 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6484 
6485 	if (info->attrs[NL80211_ATTR_DFS_REGION])
6486 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
6487 
6488 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6489 			    rem_reg_rules) {
6490 		num_rules++;
6491 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
6492 			return -EINVAL;
6493 	}
6494 
6495 	if (!reg_is_valid_request(alpha2))
6496 		return -EINVAL;
6497 
6498 	size_of_regd = sizeof(struct ieee80211_regdomain) +
6499 		       num_rules * sizeof(struct ieee80211_reg_rule);
6500 
6501 	rd = kzalloc(size_of_regd, GFP_KERNEL);
6502 	if (!rd)
6503 		return -ENOMEM;
6504 
6505 	rd->n_reg_rules = num_rules;
6506 	rd->alpha2[0] = alpha2[0];
6507 	rd->alpha2[1] = alpha2[1];
6508 
6509 	/*
6510 	 * Disable DFS master mode if the DFS region was
6511 	 * not supported or known on this kernel.
6512 	 */
6513 	if (reg_supported_dfs_region(dfs_region))
6514 		rd->dfs_region = dfs_region;
6515 
6516 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6517 			    rem_reg_rules) {
6518 		r = nla_parse_nested(tb, NL80211_REG_RULE_ATTR_MAX,
6519 				     nl_reg_rule, reg_rule_policy,
6520 				     info->extack);
6521 		if (r)
6522 			goto bad_reg;
6523 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
6524 		if (r)
6525 			goto bad_reg;
6526 
6527 		rule_idx++;
6528 
6529 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
6530 			r = -EINVAL;
6531 			goto bad_reg;
6532 		}
6533 	}
6534 
6535 	/* set_regdom takes ownership of rd */
6536 	return set_regdom(rd, REGD_SOURCE_CRDA);
6537  bad_reg:
6538 	kfree(rd);
6539 	return r;
6540 }
6541 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
6542 
6543 static int validate_scan_freqs(struct nlattr *freqs)
6544 {
6545 	struct nlattr *attr1, *attr2;
6546 	int n_channels = 0, tmp1, tmp2;
6547 
6548 	nla_for_each_nested(attr1, freqs, tmp1)
6549 		if (nla_len(attr1) != sizeof(u32))
6550 			return 0;
6551 
6552 	nla_for_each_nested(attr1, freqs, tmp1) {
6553 		n_channels++;
6554 		/*
6555 		 * Some hardware has a limited channel list for
6556 		 * scanning, and it is pretty much nonsensical
6557 		 * to scan for a channel twice, so disallow that
6558 		 * and don't require drivers to check that the
6559 		 * channel list they get isn't longer than what
6560 		 * they can scan, as long as they can scan all
6561 		 * the channels they registered at once.
6562 		 */
6563 		nla_for_each_nested(attr2, freqs, tmp2)
6564 			if (attr1 != attr2 &&
6565 			    nla_get_u32(attr1) == nla_get_u32(attr2))
6566 				return 0;
6567 	}
6568 
6569 	return n_channels;
6570 }
6571 
6572 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
6573 {
6574 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
6575 }
6576 
6577 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
6578 			    struct cfg80211_bss_selection *bss_select)
6579 {
6580 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
6581 	struct nlattr *nest;
6582 	int err;
6583 	bool found = false;
6584 	int i;
6585 
6586 	/* only process one nested attribute */
6587 	nest = nla_data(nla);
6588 	if (!nla_ok(nest, nla_len(nest)))
6589 		return -EINVAL;
6590 
6591 	err = nla_parse_nested(attr, NL80211_BSS_SELECT_ATTR_MAX, nest,
6592 			       nl80211_bss_select_policy, NULL);
6593 	if (err)
6594 		return err;
6595 
6596 	/* only one attribute may be given */
6597 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
6598 		if (attr[i]) {
6599 			if (found)
6600 				return -EINVAL;
6601 			found = true;
6602 		}
6603 	}
6604 
6605 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
6606 
6607 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
6608 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
6609 
6610 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
6611 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
6612 		bss_select->param.band_pref =
6613 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
6614 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
6615 			return -EINVAL;
6616 	}
6617 
6618 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
6619 		struct nl80211_bss_select_rssi_adjust *adj_param;
6620 
6621 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
6622 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
6623 		bss_select->param.adjust.band = adj_param->band;
6624 		bss_select->param.adjust.delta = adj_param->delta;
6625 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
6626 			return -EINVAL;
6627 	}
6628 
6629 	/* user-space did not provide behaviour attribute */
6630 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
6631 		return -EINVAL;
6632 
6633 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
6634 		return -EINVAL;
6635 
6636 	return 0;
6637 }
6638 
6639 static int nl80211_parse_random_mac(struct nlattr **attrs,
6640 				    u8 *mac_addr, u8 *mac_addr_mask)
6641 {
6642 	int i;
6643 
6644 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
6645 		eth_zero_addr(mac_addr);
6646 		eth_zero_addr(mac_addr_mask);
6647 		mac_addr[0] = 0x2;
6648 		mac_addr_mask[0] = 0x3;
6649 
6650 		return 0;
6651 	}
6652 
6653 	/* need both or none */
6654 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
6655 		return -EINVAL;
6656 
6657 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
6658 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
6659 
6660 	/* don't allow or configure an mcast address */
6661 	if (!is_multicast_ether_addr(mac_addr_mask) ||
6662 	    is_multicast_ether_addr(mac_addr))
6663 		return -EINVAL;
6664 
6665 	/*
6666 	 * allow users to pass a MAC address that has bits set outside
6667 	 * of the mask, but don't bother drivers with having to deal
6668 	 * with such bits
6669 	 */
6670 	for (i = 0; i < ETH_ALEN; i++)
6671 		mac_addr[i] &= mac_addr_mask[i];
6672 
6673 	return 0;
6674 }
6675 
6676 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
6677 {
6678 	ASSERT_WDEV_LOCK(wdev);
6679 
6680 	if (!cfg80211_beaconing_iface_active(wdev))
6681 		return true;
6682 
6683 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
6684 		return true;
6685 
6686 	return regulatory_pre_cac_allowed(wdev->wiphy);
6687 }
6688 
6689 static int
6690 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
6691 			 void *request, struct nlattr **attrs,
6692 			 bool is_sched_scan)
6693 {
6694 	u8 *mac_addr, *mac_addr_mask;
6695 	u32 *flags;
6696 	enum nl80211_feature_flags randomness_flag;
6697 
6698 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
6699 		return 0;
6700 
6701 	if (is_sched_scan) {
6702 		struct cfg80211_sched_scan_request *req = request;
6703 
6704 		randomness_flag = wdev ?
6705 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
6706 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6707 		flags = &req->flags;
6708 		mac_addr = req->mac_addr;
6709 		mac_addr_mask = req->mac_addr_mask;
6710 	} else {
6711 		struct cfg80211_scan_request *req = request;
6712 
6713 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
6714 		flags = &req->flags;
6715 		mac_addr = req->mac_addr;
6716 		mac_addr_mask = req->mac_addr_mask;
6717 	}
6718 
6719 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
6720 
6721 	if ((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6722 	    !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN))
6723 		return -EOPNOTSUPP;
6724 
6725 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6726 		int err;
6727 
6728 		if (!(wiphy->features & randomness_flag) ||
6729 		    (wdev && wdev->current_bss))
6730 			return -EOPNOTSUPP;
6731 
6732 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
6733 		if (err)
6734 			return err;
6735 	}
6736 
6737 	if ((*flags & NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME) &&
6738 	    !wiphy_ext_feature_isset(wiphy,
6739 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME))
6740 		return -EOPNOTSUPP;
6741 
6742 	if ((*flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP) &&
6743 	   !wiphy_ext_feature_isset(wiphy,
6744 				    NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP))
6745 		return -EOPNOTSUPP;
6746 
6747 	if ((*flags & NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) &&
6748 	    !wiphy_ext_feature_isset(wiphy,
6749 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION))
6750 		return -EOPNOTSUPP;
6751 
6752 	if ((*flags & NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE) &&
6753 	    !wiphy_ext_feature_isset(wiphy,
6754 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE))
6755 		return -EOPNOTSUPP;
6756 
6757 	return 0;
6758 }
6759 
6760 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
6761 {
6762 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6763 	struct wireless_dev *wdev = info->user_ptr[1];
6764 	struct cfg80211_scan_request *request;
6765 	struct nlattr *attr;
6766 	struct wiphy *wiphy;
6767 	int err, tmp, n_ssids = 0, n_channels, i;
6768 	size_t ie_len;
6769 
6770 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6771 		return -EINVAL;
6772 
6773 	wiphy = &rdev->wiphy;
6774 
6775 	if (wdev->iftype == NL80211_IFTYPE_NAN)
6776 		return -EOPNOTSUPP;
6777 
6778 	if (!rdev->ops->scan)
6779 		return -EOPNOTSUPP;
6780 
6781 	if (rdev->scan_req || rdev->scan_msg) {
6782 		err = -EBUSY;
6783 		goto unlock;
6784 	}
6785 
6786 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6787 		n_channels = validate_scan_freqs(
6788 				info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
6789 		if (!n_channels) {
6790 			err = -EINVAL;
6791 			goto unlock;
6792 		}
6793 	} else {
6794 		n_channels = ieee80211_get_num_supported_channels(wiphy);
6795 	}
6796 
6797 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
6798 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
6799 			n_ssids++;
6800 
6801 	if (n_ssids > wiphy->max_scan_ssids) {
6802 		err = -EINVAL;
6803 		goto unlock;
6804 	}
6805 
6806 	if (info->attrs[NL80211_ATTR_IE])
6807 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6808 	else
6809 		ie_len = 0;
6810 
6811 	if (ie_len > wiphy->max_scan_ie_len) {
6812 		err = -EINVAL;
6813 		goto unlock;
6814 	}
6815 
6816 	request = kzalloc(sizeof(*request)
6817 			+ sizeof(*request->ssids) * n_ssids
6818 			+ sizeof(*request->channels) * n_channels
6819 			+ ie_len, GFP_KERNEL);
6820 	if (!request) {
6821 		err = -ENOMEM;
6822 		goto unlock;
6823 	}
6824 
6825 	if (n_ssids)
6826 		request->ssids = (void *)&request->channels[n_channels];
6827 	request->n_ssids = n_ssids;
6828 	if (ie_len) {
6829 		if (n_ssids)
6830 			request->ie = (void *)(request->ssids + n_ssids);
6831 		else
6832 			request->ie = (void *)(request->channels + n_channels);
6833 	}
6834 
6835 	i = 0;
6836 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6837 		/* user specified, bail out if channel not found */
6838 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
6839 			struct ieee80211_channel *chan;
6840 
6841 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6842 
6843 			if (!chan) {
6844 				err = -EINVAL;
6845 				goto out_free;
6846 			}
6847 
6848 			/* ignore disabled channels */
6849 			if (chan->flags & IEEE80211_CHAN_DISABLED)
6850 				continue;
6851 
6852 			request->channels[i] = chan;
6853 			i++;
6854 		}
6855 	} else {
6856 		enum nl80211_band band;
6857 
6858 		/* all channels */
6859 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
6860 			int j;
6861 
6862 			if (!wiphy->bands[band])
6863 				continue;
6864 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
6865 				struct ieee80211_channel *chan;
6866 
6867 				chan = &wiphy->bands[band]->channels[j];
6868 
6869 				if (chan->flags & IEEE80211_CHAN_DISABLED)
6870 					continue;
6871 
6872 				request->channels[i] = chan;
6873 				i++;
6874 			}
6875 		}
6876 	}
6877 
6878 	if (!i) {
6879 		err = -EINVAL;
6880 		goto out_free;
6881 	}
6882 
6883 	request->n_channels = i;
6884 
6885 	wdev_lock(wdev);
6886 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
6887 		struct ieee80211_channel *chan;
6888 
6889 		if (request->n_channels != 1) {
6890 			wdev_unlock(wdev);
6891 			err = -EBUSY;
6892 			goto out_free;
6893 		}
6894 
6895 		chan = request->channels[0];
6896 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
6897 			wdev_unlock(wdev);
6898 			err = -EBUSY;
6899 			goto out_free;
6900 		}
6901 	}
6902 	wdev_unlock(wdev);
6903 
6904 	i = 0;
6905 	if (n_ssids) {
6906 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
6907 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
6908 				err = -EINVAL;
6909 				goto out_free;
6910 			}
6911 			request->ssids[i].ssid_len = nla_len(attr);
6912 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
6913 			i++;
6914 		}
6915 	}
6916 
6917 	if (info->attrs[NL80211_ATTR_IE]) {
6918 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6919 		memcpy((void *)request->ie,
6920 		       nla_data(info->attrs[NL80211_ATTR_IE]),
6921 		       request->ie_len);
6922 	}
6923 
6924 	for (i = 0; i < NUM_NL80211_BANDS; i++)
6925 		if (wiphy->bands[i])
6926 			request->rates[i] =
6927 				(1 << wiphy->bands[i]->n_bitrates) - 1;
6928 
6929 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
6930 		nla_for_each_nested(attr,
6931 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
6932 				    tmp) {
6933 			enum nl80211_band band = nla_type(attr);
6934 
6935 			if (band < 0 || band >= NUM_NL80211_BANDS) {
6936 				err = -EINVAL;
6937 				goto out_free;
6938 			}
6939 
6940 			if (!wiphy->bands[band])
6941 				continue;
6942 
6943 			err = ieee80211_get_ratemask(wiphy->bands[band],
6944 						     nla_data(attr),
6945 						     nla_len(attr),
6946 						     &request->rates[band]);
6947 			if (err)
6948 				goto out_free;
6949 		}
6950 	}
6951 
6952 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
6953 		if (!wiphy_ext_feature_isset(wiphy,
6954 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
6955 			err = -EOPNOTSUPP;
6956 			goto out_free;
6957 		}
6958 
6959 		request->duration =
6960 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
6961 		request->duration_mandatory =
6962 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
6963 	}
6964 
6965 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
6966 				       false);
6967 	if (err)
6968 		goto out_free;
6969 
6970 	request->no_cck =
6971 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6972 
6973 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
6974 	 * BSSID to scan for. This was problematic because that same attribute
6975 	 * was already used for another purpose (local random MAC address). The
6976 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
6977 	 * compatibility with older userspace components, also use the
6978 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
6979 	 * the specific BSSID use case instead of the random MAC address
6980 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
6981 	 */
6982 	if (info->attrs[NL80211_ATTR_BSSID])
6983 		memcpy(request->bssid,
6984 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
6985 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
6986 		 info->attrs[NL80211_ATTR_MAC])
6987 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
6988 		       ETH_ALEN);
6989 	else
6990 		eth_broadcast_addr(request->bssid);
6991 
6992 	request->wdev = wdev;
6993 	request->wiphy = &rdev->wiphy;
6994 	request->scan_start = jiffies;
6995 
6996 	rdev->scan_req = request;
6997 	err = rdev_scan(rdev, request);
6998 
6999 	if (!err) {
7000 		nl80211_send_scan_start(rdev, wdev);
7001 		if (wdev->netdev)
7002 			dev_hold(wdev->netdev);
7003 	} else {
7004  out_free:
7005 		rdev->scan_req = NULL;
7006 		kfree(request);
7007 	}
7008 
7009  unlock:
7010 	return err;
7011 }
7012 
7013 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7014 {
7015 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7016 	struct wireless_dev *wdev = info->user_ptr[1];
7017 
7018 	if (!rdev->ops->abort_scan)
7019 		return -EOPNOTSUPP;
7020 
7021 	if (rdev->scan_msg)
7022 		return 0;
7023 
7024 	if (!rdev->scan_req)
7025 		return -ENOENT;
7026 
7027 	rdev_abort_scan(rdev, wdev);
7028 	return 0;
7029 }
7030 
7031 static int
7032 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7033 			       struct cfg80211_sched_scan_request *request,
7034 			       struct nlattr **attrs)
7035 {
7036 	int tmp, err, i = 0;
7037 	struct nlattr *attr;
7038 
7039 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7040 		u32 interval;
7041 
7042 		/*
7043 		 * If scan plans are not specified,
7044 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7045 		 * case one scan plan will be set with the specified scan
7046 		 * interval and infinite number of iterations.
7047 		 */
7048 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7049 		if (!interval)
7050 			return -EINVAL;
7051 
7052 		request->scan_plans[0].interval =
7053 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
7054 		if (!request->scan_plans[0].interval)
7055 			return -EINVAL;
7056 
7057 		if (request->scan_plans[0].interval >
7058 		    wiphy->max_sched_scan_plan_interval)
7059 			request->scan_plans[0].interval =
7060 				wiphy->max_sched_scan_plan_interval;
7061 
7062 		return 0;
7063 	}
7064 
7065 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7066 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7067 
7068 		if (WARN_ON(i >= n_plans))
7069 			return -EINVAL;
7070 
7071 		err = nla_parse_nested(plan, NL80211_SCHED_SCAN_PLAN_MAX,
7072 				       attr, nl80211_plan_policy, NULL);
7073 		if (err)
7074 			return err;
7075 
7076 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7077 			return -EINVAL;
7078 
7079 		request->scan_plans[i].interval =
7080 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7081 		if (!request->scan_plans[i].interval ||
7082 		    request->scan_plans[i].interval >
7083 		    wiphy->max_sched_scan_plan_interval)
7084 			return -EINVAL;
7085 
7086 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7087 			request->scan_plans[i].iterations =
7088 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7089 			if (!request->scan_plans[i].iterations ||
7090 			    (request->scan_plans[i].iterations >
7091 			     wiphy->max_sched_scan_plan_iterations))
7092 				return -EINVAL;
7093 		} else if (i < n_plans - 1) {
7094 			/*
7095 			 * All scan plans but the last one must specify
7096 			 * a finite number of iterations
7097 			 */
7098 			return -EINVAL;
7099 		}
7100 
7101 		i++;
7102 	}
7103 
7104 	/*
7105 	 * The last scan plan must not specify the number of
7106 	 * iterations, it is supposed to run infinitely
7107 	 */
7108 	if (request->scan_plans[n_plans - 1].iterations)
7109 		return  -EINVAL;
7110 
7111 	return 0;
7112 }
7113 
7114 static struct cfg80211_sched_scan_request *
7115 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7116 			 struct nlattr **attrs, int max_match_sets)
7117 {
7118 	struct cfg80211_sched_scan_request *request;
7119 	struct nlattr *attr;
7120 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7121 	enum nl80211_band band;
7122 	size_t ie_len;
7123 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7124 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7125 
7126 	if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
7127 		return ERR_PTR(-EINVAL);
7128 
7129 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7130 		n_channels = validate_scan_freqs(
7131 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7132 		if (!n_channels)
7133 			return ERR_PTR(-EINVAL);
7134 	} else {
7135 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7136 	}
7137 
7138 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
7139 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7140 				    tmp)
7141 			n_ssids++;
7142 
7143 	if (n_ssids > wiphy->max_sched_scan_ssids)
7144 		return ERR_PTR(-EINVAL);
7145 
7146 	/*
7147 	 * First, count the number of 'real' matchsets. Due to an issue with
7148 	 * the old implementation, matchsets containing only the RSSI attribute
7149 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7150 	 * RSSI for all matchsets, rather than their own matchset for reporting
7151 	 * all APs with a strong RSSI. This is needed to be compatible with
7152 	 * older userspace that treated a matchset with only the RSSI as the
7153 	 * global RSSI for all other matchsets - if there are other matchsets.
7154 	 */
7155 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7156 		nla_for_each_nested(attr,
7157 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7158 				    tmp) {
7159 			struct nlattr *rssi;
7160 
7161 			err = nla_parse_nested(tb,
7162 					       NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7163 					       attr, nl80211_match_policy,
7164 					       NULL);
7165 			if (err)
7166 				return ERR_PTR(err);
7167 
7168 			/* SSID and BSSID are mutually exclusive */
7169 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7170 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7171 				return ERR_PTR(-EINVAL);
7172 
7173 			/* add other standalone attributes here */
7174 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7175 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7176 				n_match_sets++;
7177 				continue;
7178 			}
7179 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7180 			if (rssi)
7181 				default_match_rssi = nla_get_s32(rssi);
7182 		}
7183 	}
7184 
7185 	/* However, if there's no other matchset, add the RSSI one */
7186 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7187 		n_match_sets = 1;
7188 
7189 	if (n_match_sets > max_match_sets)
7190 		return ERR_PTR(-EINVAL);
7191 
7192 	if (attrs[NL80211_ATTR_IE])
7193 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7194 	else
7195 		ie_len = 0;
7196 
7197 	if (ie_len > wiphy->max_sched_scan_ie_len)
7198 		return ERR_PTR(-EINVAL);
7199 
7200 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7201 		/*
7202 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7203 		 * each scan plan already specifies its own interval
7204 		 */
7205 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7206 			return ERR_PTR(-EINVAL);
7207 
7208 		nla_for_each_nested(attr,
7209 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7210 			n_plans++;
7211 	} else {
7212 		/*
7213 		 * The scan interval attribute is kept for backward
7214 		 * compatibility. If no scan plans are specified and sched scan
7215 		 * interval is specified, one scan plan will be set with this
7216 		 * scan interval and infinite number of iterations.
7217 		 */
7218 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7219 			return ERR_PTR(-EINVAL);
7220 
7221 		n_plans = 1;
7222 	}
7223 
7224 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7225 		return ERR_PTR(-EINVAL);
7226 
7227 	if (!wiphy_ext_feature_isset(
7228 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7229 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7230 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7231 		return ERR_PTR(-EINVAL);
7232 
7233 	request = kzalloc(sizeof(*request)
7234 			+ sizeof(*request->ssids) * n_ssids
7235 			+ sizeof(*request->match_sets) * n_match_sets
7236 			+ sizeof(*request->scan_plans) * n_plans
7237 			+ sizeof(*request->channels) * n_channels
7238 			+ ie_len, GFP_KERNEL);
7239 	if (!request)
7240 		return ERR_PTR(-ENOMEM);
7241 
7242 	if (n_ssids)
7243 		request->ssids = (void *)&request->channels[n_channels];
7244 	request->n_ssids = n_ssids;
7245 	if (ie_len) {
7246 		if (n_ssids)
7247 			request->ie = (void *)(request->ssids + n_ssids);
7248 		else
7249 			request->ie = (void *)(request->channels + n_channels);
7250 	}
7251 
7252 	if (n_match_sets) {
7253 		if (request->ie)
7254 			request->match_sets = (void *)(request->ie + ie_len);
7255 		else if (n_ssids)
7256 			request->match_sets =
7257 				(void *)(request->ssids + n_ssids);
7258 		else
7259 			request->match_sets =
7260 				(void *)(request->channels + n_channels);
7261 	}
7262 	request->n_match_sets = n_match_sets;
7263 
7264 	if (n_match_sets)
7265 		request->scan_plans = (void *)(request->match_sets +
7266 					       n_match_sets);
7267 	else if (request->ie)
7268 		request->scan_plans = (void *)(request->ie + ie_len);
7269 	else if (n_ssids)
7270 		request->scan_plans = (void *)(request->ssids + n_ssids);
7271 	else
7272 		request->scan_plans = (void *)(request->channels + n_channels);
7273 
7274 	request->n_scan_plans = n_plans;
7275 
7276 	i = 0;
7277 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7278 		/* user specified, bail out if channel not found */
7279 		nla_for_each_nested(attr,
7280 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
7281 				    tmp) {
7282 			struct ieee80211_channel *chan;
7283 
7284 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7285 
7286 			if (!chan) {
7287 				err = -EINVAL;
7288 				goto out_free;
7289 			}
7290 
7291 			/* ignore disabled channels */
7292 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7293 				continue;
7294 
7295 			request->channels[i] = chan;
7296 			i++;
7297 		}
7298 	} else {
7299 		/* all channels */
7300 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7301 			int j;
7302 
7303 			if (!wiphy->bands[band])
7304 				continue;
7305 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7306 				struct ieee80211_channel *chan;
7307 
7308 				chan = &wiphy->bands[band]->channels[j];
7309 
7310 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7311 					continue;
7312 
7313 				request->channels[i] = chan;
7314 				i++;
7315 			}
7316 		}
7317 	}
7318 
7319 	if (!i) {
7320 		err = -EINVAL;
7321 		goto out_free;
7322 	}
7323 
7324 	request->n_channels = i;
7325 
7326 	i = 0;
7327 	if (n_ssids) {
7328 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7329 				    tmp) {
7330 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7331 				err = -EINVAL;
7332 				goto out_free;
7333 			}
7334 			request->ssids[i].ssid_len = nla_len(attr);
7335 			memcpy(request->ssids[i].ssid, nla_data(attr),
7336 			       nla_len(attr));
7337 			i++;
7338 		}
7339 	}
7340 
7341 	i = 0;
7342 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7343 		nla_for_each_nested(attr,
7344 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7345 				    tmp) {
7346 			struct nlattr *ssid, *bssid, *rssi;
7347 
7348 			err = nla_parse_nested(tb,
7349 					       NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7350 					       attr, nl80211_match_policy,
7351 					       NULL);
7352 			if (err)
7353 				goto out_free;
7354 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
7355 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
7356 			if (ssid || bssid) {
7357 				if (WARN_ON(i >= n_match_sets)) {
7358 					/* this indicates a programming error,
7359 					 * the loop above should have verified
7360 					 * things properly
7361 					 */
7362 					err = -EINVAL;
7363 					goto out_free;
7364 				}
7365 
7366 				if (ssid) {
7367 					if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
7368 						err = -EINVAL;
7369 						goto out_free;
7370 					}
7371 					memcpy(request->match_sets[i].ssid.ssid,
7372 					       nla_data(ssid), nla_len(ssid));
7373 					request->match_sets[i].ssid.ssid_len =
7374 						nla_len(ssid);
7375 				}
7376 				if (bssid) {
7377 					if (nla_len(bssid) != ETH_ALEN) {
7378 						err = -EINVAL;
7379 						goto out_free;
7380 					}
7381 					memcpy(request->match_sets[i].bssid,
7382 					       nla_data(bssid), ETH_ALEN);
7383 				}
7384 
7385 				/* special attribute - old implementation w/a */
7386 				request->match_sets[i].rssi_thold =
7387 					default_match_rssi;
7388 				rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7389 				if (rssi)
7390 					request->match_sets[i].rssi_thold =
7391 						nla_get_s32(rssi);
7392 			}
7393 			i++;
7394 		}
7395 
7396 		/* there was no other matchset, so the RSSI one is alone */
7397 		if (i == 0 && n_match_sets)
7398 			request->match_sets[0].rssi_thold = default_match_rssi;
7399 
7400 		request->min_rssi_thold = INT_MAX;
7401 		for (i = 0; i < n_match_sets; i++)
7402 			request->min_rssi_thold =
7403 				min(request->match_sets[i].rssi_thold,
7404 				    request->min_rssi_thold);
7405 	} else {
7406 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
7407 	}
7408 
7409 	if (ie_len) {
7410 		request->ie_len = ie_len;
7411 		memcpy((void *)request->ie,
7412 		       nla_data(attrs[NL80211_ATTR_IE]),
7413 		       request->ie_len);
7414 	}
7415 
7416 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
7417 	if (err)
7418 		goto out_free;
7419 
7420 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
7421 		request->delay =
7422 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
7423 
7424 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
7425 		request->relative_rssi = nla_get_s8(
7426 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
7427 		request->relative_rssi_set = true;
7428 	}
7429 
7430 	if (request->relative_rssi_set &&
7431 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
7432 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
7433 
7434 		rssi_adjust = nla_data(
7435 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
7436 		request->rssi_adjust.band = rssi_adjust->band;
7437 		request->rssi_adjust.delta = rssi_adjust->delta;
7438 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
7439 			err = -EINVAL;
7440 			goto out_free;
7441 		}
7442 	}
7443 
7444 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
7445 	if (err)
7446 		goto out_free;
7447 
7448 	request->scan_start = jiffies;
7449 
7450 	return request;
7451 
7452 out_free:
7453 	kfree(request);
7454 	return ERR_PTR(err);
7455 }
7456 
7457 static int nl80211_start_sched_scan(struct sk_buff *skb,
7458 				    struct genl_info *info)
7459 {
7460 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7461 	struct net_device *dev = info->user_ptr[1];
7462 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7463 	struct cfg80211_sched_scan_request *sched_scan_req;
7464 	bool want_multi;
7465 	int err;
7466 
7467 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
7468 		return -EOPNOTSUPP;
7469 
7470 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
7471 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
7472 	if (err)
7473 		return err;
7474 
7475 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
7476 						  info->attrs,
7477 						  rdev->wiphy.max_match_sets);
7478 
7479 	err = PTR_ERR_OR_ZERO(sched_scan_req);
7480 	if (err)
7481 		goto out_err;
7482 
7483 	/* leave request id zero for legacy request
7484 	 * or if driver does not support multi-scheduled scan
7485 	 */
7486 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
7487 		while (!sched_scan_req->reqid)
7488 			sched_scan_req->reqid = rdev->wiphy.cookie_counter++;
7489 	}
7490 
7491 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
7492 	if (err)
7493 		goto out_free;
7494 
7495 	sched_scan_req->dev = dev;
7496 	sched_scan_req->wiphy = &rdev->wiphy;
7497 
7498 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
7499 		sched_scan_req->owner_nlportid = info->snd_portid;
7500 
7501 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
7502 
7503 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
7504 	return 0;
7505 
7506 out_free:
7507 	kfree(sched_scan_req);
7508 out_err:
7509 	return err;
7510 }
7511 
7512 static int nl80211_stop_sched_scan(struct sk_buff *skb,
7513 				   struct genl_info *info)
7514 {
7515 	struct cfg80211_sched_scan_request *req;
7516 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7517 	u64 cookie;
7518 
7519 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
7520 		return -EOPNOTSUPP;
7521 
7522 	if (info->attrs[NL80211_ATTR_COOKIE]) {
7523 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7524 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
7525 	}
7526 
7527 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
7528 				     struct cfg80211_sched_scan_request,
7529 				     list);
7530 	if (!req || req->reqid ||
7531 	    (req->owner_nlportid &&
7532 	     req->owner_nlportid != info->snd_portid))
7533 		return -ENOENT;
7534 
7535 	return cfg80211_stop_sched_scan_req(rdev, req, false);
7536 }
7537 
7538 static int nl80211_start_radar_detection(struct sk_buff *skb,
7539 					 struct genl_info *info)
7540 {
7541 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7542 	struct net_device *dev = info->user_ptr[1];
7543 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7544 	struct cfg80211_chan_def chandef;
7545 	enum nl80211_dfs_regions dfs_region;
7546 	unsigned int cac_time_ms;
7547 	int err;
7548 
7549 	dfs_region = reg_get_dfs_region(wdev->wiphy);
7550 	if (dfs_region == NL80211_DFS_UNSET)
7551 		return -EINVAL;
7552 
7553 	err = nl80211_parse_chandef(rdev, info, &chandef);
7554 	if (err)
7555 		return err;
7556 
7557 	if (netif_carrier_ok(dev))
7558 		return -EBUSY;
7559 
7560 	if (wdev->cac_started)
7561 		return -EBUSY;
7562 
7563 	err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
7564 					    wdev->iftype);
7565 	if (err < 0)
7566 		return err;
7567 
7568 	if (err == 0)
7569 		return -EINVAL;
7570 
7571 	if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
7572 		return -EINVAL;
7573 
7574 	if (!rdev->ops->start_radar_detection)
7575 		return -EOPNOTSUPP;
7576 
7577 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
7578 	if (WARN_ON(!cac_time_ms))
7579 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
7580 
7581 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
7582 	if (!err) {
7583 		wdev->chandef = chandef;
7584 		wdev->cac_started = true;
7585 		wdev->cac_start_time = jiffies;
7586 		wdev->cac_time_ms = cac_time_ms;
7587 	}
7588 	return err;
7589 }
7590 
7591 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
7592 {
7593 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7594 	struct net_device *dev = info->user_ptr[1];
7595 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7596 	struct cfg80211_csa_settings params;
7597 	/* csa_attrs is defined static to avoid waste of stack size - this
7598 	 * function is called under RTNL lock, so this should not be a problem.
7599 	 */
7600 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
7601 	int err;
7602 	bool need_new_beacon = false;
7603 	bool need_handle_dfs_flag = true;
7604 	int len, i;
7605 	u32 cs_count;
7606 
7607 	if (!rdev->ops->channel_switch ||
7608 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
7609 		return -EOPNOTSUPP;
7610 
7611 	switch (dev->ieee80211_ptr->iftype) {
7612 	case NL80211_IFTYPE_AP:
7613 	case NL80211_IFTYPE_P2P_GO:
7614 		need_new_beacon = true;
7615 		/* For all modes except AP the handle_dfs flag needs to be
7616 		 * supplied to tell the kernel that userspace will handle radar
7617 		 * events when they happen. Otherwise a switch to a channel
7618 		 * requiring DFS will be rejected.
7619 		 */
7620 		need_handle_dfs_flag = false;
7621 
7622 		/* useless if AP is not running */
7623 		if (!wdev->beacon_interval)
7624 			return -ENOTCONN;
7625 		break;
7626 	case NL80211_IFTYPE_ADHOC:
7627 		if (!wdev->ssid_len)
7628 			return -ENOTCONN;
7629 		break;
7630 	case NL80211_IFTYPE_MESH_POINT:
7631 		if (!wdev->mesh_id_len)
7632 			return -ENOTCONN;
7633 		break;
7634 	default:
7635 		return -EOPNOTSUPP;
7636 	}
7637 
7638 	memset(&params, 0, sizeof(params));
7639 
7640 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7641 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
7642 		return -EINVAL;
7643 
7644 	/* only important for AP, IBSS and mesh create IEs internally */
7645 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
7646 		return -EINVAL;
7647 
7648 	/* Even though the attribute is u32, the specification says
7649 	 * u8, so let's make sure we don't overflow.
7650 	 */
7651 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
7652 	if (cs_count > 255)
7653 		return -EINVAL;
7654 
7655 	params.count = cs_count;
7656 
7657 	if (!need_new_beacon)
7658 		goto skip_beacons;
7659 
7660 	err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
7661 	if (err)
7662 		return err;
7663 
7664 	err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
7665 			       info->attrs[NL80211_ATTR_CSA_IES],
7666 			       nl80211_policy, info->extack);
7667 	if (err)
7668 		return err;
7669 
7670 	err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
7671 	if (err)
7672 		return err;
7673 
7674 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
7675 		return -EINVAL;
7676 
7677 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7678 	if (!len || (len % sizeof(u16)))
7679 		return -EINVAL;
7680 
7681 	params.n_counter_offsets_beacon = len / sizeof(u16);
7682 	if (rdev->wiphy.max_num_csa_counters &&
7683 	    (params.n_counter_offsets_beacon >
7684 	     rdev->wiphy.max_num_csa_counters))
7685 		return -EINVAL;
7686 
7687 	params.counter_offsets_beacon =
7688 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7689 
7690 	/* sanity checks - counters should fit and be the same */
7691 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
7692 		u16 offset = params.counter_offsets_beacon[i];
7693 
7694 		if (offset >= params.beacon_csa.tail_len)
7695 			return -EINVAL;
7696 
7697 		if (params.beacon_csa.tail[offset] != params.count)
7698 			return -EINVAL;
7699 	}
7700 
7701 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
7702 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7703 		if (!len || (len % sizeof(u16)))
7704 			return -EINVAL;
7705 
7706 		params.n_counter_offsets_presp = len / sizeof(u16);
7707 		if (rdev->wiphy.max_num_csa_counters &&
7708 		    (params.n_counter_offsets_presp >
7709 		     rdev->wiphy.max_num_csa_counters))
7710 			return -EINVAL;
7711 
7712 		params.counter_offsets_presp =
7713 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7714 
7715 		/* sanity checks - counters should fit and be the same */
7716 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
7717 			u16 offset = params.counter_offsets_presp[i];
7718 
7719 			if (offset >= params.beacon_csa.probe_resp_len)
7720 				return -EINVAL;
7721 
7722 			if (params.beacon_csa.probe_resp[offset] !=
7723 			    params.count)
7724 				return -EINVAL;
7725 		}
7726 	}
7727 
7728 skip_beacons:
7729 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
7730 	if (err)
7731 		return err;
7732 
7733 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
7734 					   wdev->iftype))
7735 		return -EINVAL;
7736 
7737 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
7738 					    &params.chandef,
7739 					    wdev->iftype);
7740 	if (err < 0)
7741 		return err;
7742 
7743 	if (err > 0) {
7744 		params.radar_required = true;
7745 		if (need_handle_dfs_flag &&
7746 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
7747 			return -EINVAL;
7748 		}
7749 	}
7750 
7751 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
7752 		params.block_tx = true;
7753 
7754 	wdev_lock(wdev);
7755 	err = rdev_channel_switch(rdev, dev, &params);
7756 	wdev_unlock(wdev);
7757 
7758 	return err;
7759 }
7760 
7761 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
7762 			    u32 seq, int flags,
7763 			    struct cfg80211_registered_device *rdev,
7764 			    struct wireless_dev *wdev,
7765 			    struct cfg80211_internal_bss *intbss)
7766 {
7767 	struct cfg80211_bss *res = &intbss->pub;
7768 	const struct cfg80211_bss_ies *ies;
7769 	void *hdr;
7770 	struct nlattr *bss;
7771 
7772 	ASSERT_WDEV_LOCK(wdev);
7773 
7774 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7775 			     NL80211_CMD_NEW_SCAN_RESULTS);
7776 	if (!hdr)
7777 		return -1;
7778 
7779 	genl_dump_check_consistent(cb, hdr);
7780 
7781 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
7782 		goto nla_put_failure;
7783 	if (wdev->netdev &&
7784 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
7785 		goto nla_put_failure;
7786 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
7787 			      NL80211_ATTR_PAD))
7788 		goto nla_put_failure;
7789 
7790 	bss = nla_nest_start(msg, NL80211_ATTR_BSS);
7791 	if (!bss)
7792 		goto nla_put_failure;
7793 	if ((!is_zero_ether_addr(res->bssid) &&
7794 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
7795 		goto nla_put_failure;
7796 
7797 	rcu_read_lock();
7798 	/* indicate whether we have probe response data or not */
7799 	if (rcu_access_pointer(res->proberesp_ies) &&
7800 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
7801 		goto fail_unlock_rcu;
7802 
7803 	/* this pointer prefers to be pointed to probe response data
7804 	 * but is always valid
7805 	 */
7806 	ies = rcu_dereference(res->ies);
7807 	if (ies) {
7808 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
7809 				      NL80211_BSS_PAD))
7810 			goto fail_unlock_rcu;
7811 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
7812 					ies->len, ies->data))
7813 			goto fail_unlock_rcu;
7814 	}
7815 
7816 	/* and this pointer is always (unless driver didn't know) beacon data */
7817 	ies = rcu_dereference(res->beacon_ies);
7818 	if (ies && ies->from_beacon) {
7819 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
7820 				      NL80211_BSS_PAD))
7821 			goto fail_unlock_rcu;
7822 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
7823 					ies->len, ies->data))
7824 			goto fail_unlock_rcu;
7825 	}
7826 	rcu_read_unlock();
7827 
7828 	if (res->beacon_interval &&
7829 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
7830 		goto nla_put_failure;
7831 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
7832 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
7833 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
7834 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
7835 			jiffies_to_msecs(jiffies - intbss->ts)))
7836 		goto nla_put_failure;
7837 
7838 	if (intbss->parent_tsf &&
7839 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
7840 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
7841 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
7842 		     intbss->parent_bssid)))
7843 		goto nla_put_failure;
7844 
7845 	if (intbss->ts_boottime &&
7846 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
7847 			      intbss->ts_boottime, NL80211_BSS_PAD))
7848 		goto nla_put_failure;
7849 
7850 	if (!nl80211_put_signal(msg, intbss->pub.chains,
7851 				intbss->pub.chain_signal,
7852 				NL80211_BSS_CHAIN_SIGNAL))
7853 		goto nla_put_failure;
7854 
7855 	switch (rdev->wiphy.signal_type) {
7856 	case CFG80211_SIGNAL_TYPE_MBM:
7857 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
7858 			goto nla_put_failure;
7859 		break;
7860 	case CFG80211_SIGNAL_TYPE_UNSPEC:
7861 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
7862 			goto nla_put_failure;
7863 		break;
7864 	default:
7865 		break;
7866 	}
7867 
7868 	switch (wdev->iftype) {
7869 	case NL80211_IFTYPE_P2P_CLIENT:
7870 	case NL80211_IFTYPE_STATION:
7871 		if (intbss == wdev->current_bss &&
7872 		    nla_put_u32(msg, NL80211_BSS_STATUS,
7873 				NL80211_BSS_STATUS_ASSOCIATED))
7874 			goto nla_put_failure;
7875 		break;
7876 	case NL80211_IFTYPE_ADHOC:
7877 		if (intbss == wdev->current_bss &&
7878 		    nla_put_u32(msg, NL80211_BSS_STATUS,
7879 				NL80211_BSS_STATUS_IBSS_JOINED))
7880 			goto nla_put_failure;
7881 		break;
7882 	default:
7883 		break;
7884 	}
7885 
7886 	nla_nest_end(msg, bss);
7887 
7888 	genlmsg_end(msg, hdr);
7889 	return 0;
7890 
7891  fail_unlock_rcu:
7892 	rcu_read_unlock();
7893  nla_put_failure:
7894 	genlmsg_cancel(msg, hdr);
7895 	return -EMSGSIZE;
7896 }
7897 
7898 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
7899 {
7900 	struct cfg80211_registered_device *rdev;
7901 	struct cfg80211_internal_bss *scan;
7902 	struct wireless_dev *wdev;
7903 	int start = cb->args[2], idx = 0;
7904 	int err;
7905 
7906 	rtnl_lock();
7907 	err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
7908 	if (err) {
7909 		rtnl_unlock();
7910 		return err;
7911 	}
7912 
7913 	wdev_lock(wdev);
7914 	spin_lock_bh(&rdev->bss_lock);
7915 	cfg80211_bss_expire(rdev);
7916 
7917 	cb->seq = rdev->bss_generation;
7918 
7919 	list_for_each_entry(scan, &rdev->bss_list, list) {
7920 		if (++idx <= start)
7921 			continue;
7922 		if (nl80211_send_bss(skb, cb,
7923 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
7924 				rdev, wdev, scan) < 0) {
7925 			idx--;
7926 			break;
7927 		}
7928 	}
7929 
7930 	spin_unlock_bh(&rdev->bss_lock);
7931 	wdev_unlock(wdev);
7932 
7933 	cb->args[2] = idx;
7934 	rtnl_unlock();
7935 
7936 	return skb->len;
7937 }
7938 
7939 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
7940 			       int flags, struct net_device *dev,
7941 			       bool allow_radio_stats,
7942 			       struct survey_info *survey)
7943 {
7944 	void *hdr;
7945 	struct nlattr *infoattr;
7946 
7947 	/* skip radio stats if userspace didn't request them */
7948 	if (!survey->channel && !allow_radio_stats)
7949 		return 0;
7950 
7951 	hdr = nl80211hdr_put(msg, portid, seq, flags,
7952 			     NL80211_CMD_NEW_SURVEY_RESULTS);
7953 	if (!hdr)
7954 		return -ENOMEM;
7955 
7956 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
7957 		goto nla_put_failure;
7958 
7959 	infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
7960 	if (!infoattr)
7961 		goto nla_put_failure;
7962 
7963 	if (survey->channel &&
7964 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
7965 			survey->channel->center_freq))
7966 		goto nla_put_failure;
7967 
7968 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
7969 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
7970 		goto nla_put_failure;
7971 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
7972 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
7973 		goto nla_put_failure;
7974 	if ((survey->filled & SURVEY_INFO_TIME) &&
7975 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
7976 			survey->time, NL80211_SURVEY_INFO_PAD))
7977 		goto nla_put_failure;
7978 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
7979 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
7980 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
7981 		goto nla_put_failure;
7982 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
7983 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
7984 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
7985 		goto nla_put_failure;
7986 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
7987 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
7988 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
7989 		goto nla_put_failure;
7990 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
7991 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
7992 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
7993 		goto nla_put_failure;
7994 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
7995 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
7996 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
7997 		goto nla_put_failure;
7998 
7999 	nla_nest_end(msg, infoattr);
8000 
8001 	genlmsg_end(msg, hdr);
8002 	return 0;
8003 
8004  nla_put_failure:
8005 	genlmsg_cancel(msg, hdr);
8006 	return -EMSGSIZE;
8007 }
8008 
8009 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8010 {
8011 	struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8012 	struct survey_info survey;
8013 	struct cfg80211_registered_device *rdev;
8014 	struct wireless_dev *wdev;
8015 	int survey_idx = cb->args[2];
8016 	int res;
8017 	bool radio_stats;
8018 
8019 	rtnl_lock();
8020 	res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
8021 	if (res)
8022 		goto out_err;
8023 
8024 	/* prepare_wdev_dump parsed the attributes */
8025 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8026 
8027 	if (!wdev->netdev) {
8028 		res = -EINVAL;
8029 		goto out_err;
8030 	}
8031 
8032 	if (!rdev->ops->dump_survey) {
8033 		res = -EOPNOTSUPP;
8034 		goto out_err;
8035 	}
8036 
8037 	while (1) {
8038 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8039 		if (res == -ENOENT)
8040 			break;
8041 		if (res)
8042 			goto out_err;
8043 
8044 		/* don't send disabled channels, but do send non-channel data */
8045 		if (survey.channel &&
8046 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8047 			survey_idx++;
8048 			continue;
8049 		}
8050 
8051 		if (nl80211_send_survey(skb,
8052 				NETLINK_CB(cb->skb).portid,
8053 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8054 				wdev->netdev, radio_stats, &survey) < 0)
8055 			goto out;
8056 		survey_idx++;
8057 	}
8058 
8059  out:
8060 	cb->args[2] = survey_idx;
8061 	res = skb->len;
8062  out_err:
8063 	rtnl_unlock();
8064 	return res;
8065 }
8066 
8067 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8068 {
8069 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8070 				  NL80211_WPA_VERSION_2));
8071 }
8072 
8073 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8074 {
8075 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8076 	struct net_device *dev = info->user_ptr[1];
8077 	struct ieee80211_channel *chan;
8078 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8079 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
8080 	enum nl80211_auth_type auth_type;
8081 	struct key_parse key;
8082 	bool local_state_change;
8083 
8084 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8085 		return -EINVAL;
8086 
8087 	if (!info->attrs[NL80211_ATTR_MAC])
8088 		return -EINVAL;
8089 
8090 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8091 		return -EINVAL;
8092 
8093 	if (!info->attrs[NL80211_ATTR_SSID])
8094 		return -EINVAL;
8095 
8096 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8097 		return -EINVAL;
8098 
8099 	err = nl80211_parse_key(info, &key);
8100 	if (err)
8101 		return err;
8102 
8103 	if (key.idx >= 0) {
8104 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8105 			return -EINVAL;
8106 		if (!key.p.key || !key.p.key_len)
8107 			return -EINVAL;
8108 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8109 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8110 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8111 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
8112 			return -EINVAL;
8113 		if (key.idx > 3)
8114 			return -EINVAL;
8115 	} else {
8116 		key.p.key_len = 0;
8117 		key.p.key = NULL;
8118 	}
8119 
8120 	if (key.idx >= 0) {
8121 		int i;
8122 		bool ok = false;
8123 
8124 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8125 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8126 				ok = true;
8127 				break;
8128 			}
8129 		}
8130 		if (!ok)
8131 			return -EINVAL;
8132 	}
8133 
8134 	if (!rdev->ops->auth)
8135 		return -EOPNOTSUPP;
8136 
8137 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8138 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8139 		return -EOPNOTSUPP;
8140 
8141 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8142 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8143 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8144 	if (!chan)
8145 		return -EINVAL;
8146 
8147 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8148 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8149 
8150 	if (info->attrs[NL80211_ATTR_IE]) {
8151 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8152 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8153 	}
8154 
8155 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8156 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
8157 		return -EINVAL;
8158 
8159 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
8160 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
8161 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
8162 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
8163 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
8164 		return -EINVAL;
8165 
8166 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
8167 		if (auth_type != NL80211_AUTHTYPE_SAE &&
8168 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
8169 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8170 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
8171 			return -EINVAL;
8172 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8173 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
8174 		/* need to include at least Auth Transaction and Status Code */
8175 		if (auth_data_len < 4)
8176 			return -EINVAL;
8177 	}
8178 
8179 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8180 
8181 	/*
8182 	 * Since we no longer track auth state, ignore
8183 	 * requests to only change local state.
8184 	 */
8185 	if (local_state_change)
8186 		return 0;
8187 
8188 	wdev_lock(dev->ieee80211_ptr);
8189 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8190 				 ssid, ssid_len, ie, ie_len,
8191 				 key.p.key, key.p.key_len, key.idx,
8192 				 auth_data, auth_data_len);
8193 	wdev_unlock(dev->ieee80211_ptr);
8194 	return err;
8195 }
8196 
8197 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8198 				   struct genl_info *info,
8199 				   struct cfg80211_crypto_settings *settings,
8200 				   int cipher_limit)
8201 {
8202 	memset(settings, 0, sizeof(*settings));
8203 
8204 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
8205 
8206 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
8207 		u16 proto;
8208 
8209 		proto = nla_get_u16(
8210 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
8211 		settings->control_port_ethertype = cpu_to_be16(proto);
8212 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
8213 		    proto != ETH_P_PAE)
8214 			return -EINVAL;
8215 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
8216 			settings->control_port_no_encrypt = true;
8217 	} else
8218 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
8219 
8220 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
8221 		void *data;
8222 		int len, i;
8223 
8224 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8225 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8226 		settings->n_ciphers_pairwise = len / sizeof(u32);
8227 
8228 		if (len % sizeof(u32))
8229 			return -EINVAL;
8230 
8231 		if (settings->n_ciphers_pairwise > cipher_limit)
8232 			return -EINVAL;
8233 
8234 		memcpy(settings->ciphers_pairwise, data, len);
8235 
8236 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
8237 			if (!cfg80211_supported_cipher_suite(
8238 					&rdev->wiphy,
8239 					settings->ciphers_pairwise[i]))
8240 				return -EINVAL;
8241 	}
8242 
8243 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
8244 		settings->cipher_group =
8245 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
8246 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
8247 						     settings->cipher_group))
8248 			return -EINVAL;
8249 	}
8250 
8251 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
8252 		settings->wpa_versions =
8253 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
8254 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
8255 			return -EINVAL;
8256 	}
8257 
8258 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
8259 		void *data;
8260 		int len;
8261 
8262 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
8263 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
8264 		settings->n_akm_suites = len / sizeof(u32);
8265 
8266 		if (len % sizeof(u32))
8267 			return -EINVAL;
8268 
8269 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
8270 			return -EINVAL;
8271 
8272 		memcpy(settings->akm_suites, data, len);
8273 	}
8274 
8275 	if (info->attrs[NL80211_ATTR_PMK]) {
8276 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
8277 			return -EINVAL;
8278 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
8279 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
8280 			return -EINVAL;
8281 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
8282 	}
8283 
8284 	return 0;
8285 }
8286 
8287 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
8288 {
8289 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8290 	struct net_device *dev = info->user_ptr[1];
8291 	struct ieee80211_channel *chan;
8292 	struct cfg80211_assoc_request req = {};
8293 	const u8 *bssid, *ssid;
8294 	int err, ssid_len = 0;
8295 
8296 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8297 		return -EINVAL;
8298 
8299 	if (!info->attrs[NL80211_ATTR_MAC] ||
8300 	    !info->attrs[NL80211_ATTR_SSID] ||
8301 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
8302 		return -EINVAL;
8303 
8304 	if (!rdev->ops->assoc)
8305 		return -EOPNOTSUPP;
8306 
8307 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8308 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8309 		return -EOPNOTSUPP;
8310 
8311 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8312 
8313 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8314 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8315 	if (!chan)
8316 		return -EINVAL;
8317 
8318 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8319 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8320 
8321 	if (info->attrs[NL80211_ATTR_IE]) {
8322 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8323 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8324 	}
8325 
8326 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
8327 		enum nl80211_mfp mfp =
8328 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8329 		if (mfp == NL80211_MFP_REQUIRED)
8330 			req.use_mfp = true;
8331 		else if (mfp != NL80211_MFP_NO)
8332 			return -EINVAL;
8333 	}
8334 
8335 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
8336 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8337 
8338 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
8339 		req.flags |= ASSOC_REQ_DISABLE_HT;
8340 
8341 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8342 		memcpy(&req.ht_capa_mask,
8343 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8344 		       sizeof(req.ht_capa_mask));
8345 
8346 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8347 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8348 			return -EINVAL;
8349 		memcpy(&req.ht_capa,
8350 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8351 		       sizeof(req.ht_capa));
8352 	}
8353 
8354 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
8355 		req.flags |= ASSOC_REQ_DISABLE_VHT;
8356 
8357 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8358 		memcpy(&req.vht_capa_mask,
8359 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8360 		       sizeof(req.vht_capa_mask));
8361 
8362 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
8363 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8364 			return -EINVAL;
8365 		memcpy(&req.vht_capa,
8366 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8367 		       sizeof(req.vht_capa));
8368 	}
8369 
8370 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
8371 		if (!((rdev->wiphy.features &
8372 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8373 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8374 		    !wiphy_ext_feature_isset(&rdev->wiphy,
8375 					     NL80211_EXT_FEATURE_RRM))
8376 			return -EINVAL;
8377 		req.flags |= ASSOC_REQ_USE_RRM;
8378 	}
8379 
8380 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
8381 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
8382 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
8383 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
8384 			return -EINVAL;
8385 		req.fils_nonces =
8386 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
8387 	}
8388 
8389 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
8390 	if (!err) {
8391 		wdev_lock(dev->ieee80211_ptr);
8392 
8393 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
8394 					  ssid, ssid_len, &req);
8395 
8396 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8397 			dev->ieee80211_ptr->conn_owner_nlportid =
8398 				info->snd_portid;
8399 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
8400 			       bssid, ETH_ALEN);
8401 		}
8402 
8403 		wdev_unlock(dev->ieee80211_ptr);
8404 	}
8405 
8406 	return err;
8407 }
8408 
8409 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
8410 {
8411 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8412 	struct net_device *dev = info->user_ptr[1];
8413 	const u8 *ie = NULL, *bssid;
8414 	int ie_len = 0, err;
8415 	u16 reason_code;
8416 	bool local_state_change;
8417 
8418 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8419 		return -EINVAL;
8420 
8421 	if (!info->attrs[NL80211_ATTR_MAC])
8422 		return -EINVAL;
8423 
8424 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
8425 		return -EINVAL;
8426 
8427 	if (!rdev->ops->deauth)
8428 		return -EOPNOTSUPP;
8429 
8430 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8431 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8432 		return -EOPNOTSUPP;
8433 
8434 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8435 
8436 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8437 	if (reason_code == 0) {
8438 		/* Reason Code 0 is reserved */
8439 		return -EINVAL;
8440 	}
8441 
8442 	if (info->attrs[NL80211_ATTR_IE]) {
8443 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8444 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8445 	}
8446 
8447 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8448 
8449 	wdev_lock(dev->ieee80211_ptr);
8450 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
8451 				   local_state_change);
8452 	wdev_unlock(dev->ieee80211_ptr);
8453 	return err;
8454 }
8455 
8456 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
8457 {
8458 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8459 	struct net_device *dev = info->user_ptr[1];
8460 	const u8 *ie = NULL, *bssid;
8461 	int ie_len = 0, err;
8462 	u16 reason_code;
8463 	bool local_state_change;
8464 
8465 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8466 		return -EINVAL;
8467 
8468 	if (!info->attrs[NL80211_ATTR_MAC])
8469 		return -EINVAL;
8470 
8471 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
8472 		return -EINVAL;
8473 
8474 	if (!rdev->ops->disassoc)
8475 		return -EOPNOTSUPP;
8476 
8477 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8478 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8479 		return -EOPNOTSUPP;
8480 
8481 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8482 
8483 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8484 	if (reason_code == 0) {
8485 		/* Reason Code 0 is reserved */
8486 		return -EINVAL;
8487 	}
8488 
8489 	if (info->attrs[NL80211_ATTR_IE]) {
8490 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8491 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8492 	}
8493 
8494 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8495 
8496 	wdev_lock(dev->ieee80211_ptr);
8497 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
8498 				     local_state_change);
8499 	wdev_unlock(dev->ieee80211_ptr);
8500 	return err;
8501 }
8502 
8503 static bool
8504 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
8505 			 int mcast_rate[NUM_NL80211_BANDS],
8506 			 int rateval)
8507 {
8508 	struct wiphy *wiphy = &rdev->wiphy;
8509 	bool found = false;
8510 	int band, i;
8511 
8512 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
8513 		struct ieee80211_supported_band *sband;
8514 
8515 		sband = wiphy->bands[band];
8516 		if (!sband)
8517 			continue;
8518 
8519 		for (i = 0; i < sband->n_bitrates; i++) {
8520 			if (sband->bitrates[i].bitrate == rateval) {
8521 				mcast_rate[band] = i + 1;
8522 				found = true;
8523 				break;
8524 			}
8525 		}
8526 	}
8527 
8528 	return found;
8529 }
8530 
8531 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
8532 {
8533 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8534 	struct net_device *dev = info->user_ptr[1];
8535 	struct cfg80211_ibss_params ibss;
8536 	struct wiphy *wiphy;
8537 	struct cfg80211_cached_keys *connkeys = NULL;
8538 	int err;
8539 
8540 	memset(&ibss, 0, sizeof(ibss));
8541 
8542 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8543 		return -EINVAL;
8544 
8545 	if (!info->attrs[NL80211_ATTR_SSID] ||
8546 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
8547 		return -EINVAL;
8548 
8549 	ibss.beacon_interval = 100;
8550 
8551 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
8552 		ibss.beacon_interval =
8553 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8554 
8555 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
8556 					   ibss.beacon_interval);
8557 	if (err)
8558 		return err;
8559 
8560 	if (!rdev->ops->join_ibss)
8561 		return -EOPNOTSUPP;
8562 
8563 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8564 		return -EOPNOTSUPP;
8565 
8566 	wiphy = &rdev->wiphy;
8567 
8568 	if (info->attrs[NL80211_ATTR_MAC]) {
8569 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8570 
8571 		if (!is_valid_ether_addr(ibss.bssid))
8572 			return -EINVAL;
8573 	}
8574 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8575 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8576 
8577 	if (info->attrs[NL80211_ATTR_IE]) {
8578 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8579 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8580 	}
8581 
8582 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
8583 	if (err)
8584 		return err;
8585 
8586 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
8587 				     NL80211_IFTYPE_ADHOC))
8588 		return -EINVAL;
8589 
8590 	switch (ibss.chandef.width) {
8591 	case NL80211_CHAN_WIDTH_5:
8592 	case NL80211_CHAN_WIDTH_10:
8593 	case NL80211_CHAN_WIDTH_20_NOHT:
8594 		break;
8595 	case NL80211_CHAN_WIDTH_20:
8596 	case NL80211_CHAN_WIDTH_40:
8597 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8598 			return -EINVAL;
8599 		break;
8600 	case NL80211_CHAN_WIDTH_80:
8601 	case NL80211_CHAN_WIDTH_80P80:
8602 	case NL80211_CHAN_WIDTH_160:
8603 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8604 			return -EINVAL;
8605 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
8606 					     NL80211_EXT_FEATURE_VHT_IBSS))
8607 			return -EINVAL;
8608 		break;
8609 	default:
8610 		return -EINVAL;
8611 	}
8612 
8613 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
8614 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
8615 
8616 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8617 		u8 *rates =
8618 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8619 		int n_rates =
8620 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8621 		struct ieee80211_supported_band *sband =
8622 			wiphy->bands[ibss.chandef.chan->band];
8623 
8624 		err = ieee80211_get_ratemask(sband, rates, n_rates,
8625 					     &ibss.basic_rates);
8626 		if (err)
8627 			return err;
8628 	}
8629 
8630 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8631 		memcpy(&ibss.ht_capa_mask,
8632 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8633 		       sizeof(ibss.ht_capa_mask));
8634 
8635 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8636 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8637 			return -EINVAL;
8638 		memcpy(&ibss.ht_capa,
8639 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8640 		       sizeof(ibss.ht_capa));
8641 	}
8642 
8643 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8644 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
8645 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8646 		return -EINVAL;
8647 
8648 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
8649 		bool no_ht = false;
8650 
8651 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
8652 		if (IS_ERR(connkeys))
8653 			return PTR_ERR(connkeys);
8654 
8655 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
8656 		    no_ht) {
8657 			kzfree(connkeys);
8658 			return -EINVAL;
8659 		}
8660 	}
8661 
8662 	ibss.control_port =
8663 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
8664 
8665 	ibss.userspace_handles_dfs =
8666 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
8667 
8668 	err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
8669 	if (err)
8670 		kzfree(connkeys);
8671 	return err;
8672 }
8673 
8674 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
8675 {
8676 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8677 	struct net_device *dev = info->user_ptr[1];
8678 
8679 	if (!rdev->ops->leave_ibss)
8680 		return -EOPNOTSUPP;
8681 
8682 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8683 		return -EOPNOTSUPP;
8684 
8685 	return cfg80211_leave_ibss(rdev, dev, false);
8686 }
8687 
8688 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
8689 {
8690 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8691 	struct net_device *dev = info->user_ptr[1];
8692 	int mcast_rate[NUM_NL80211_BANDS];
8693 	u32 nla_rate;
8694 	int err;
8695 
8696 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
8697 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
8698 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
8699 		return -EOPNOTSUPP;
8700 
8701 	if (!rdev->ops->set_mcast_rate)
8702 		return -EOPNOTSUPP;
8703 
8704 	memset(mcast_rate, 0, sizeof(mcast_rate));
8705 
8706 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
8707 		return -EINVAL;
8708 
8709 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
8710 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
8711 		return -EINVAL;
8712 
8713 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
8714 
8715 	return err;
8716 }
8717 
8718 static struct sk_buff *
8719 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
8720 			    struct wireless_dev *wdev, int approxlen,
8721 			    u32 portid, u32 seq, enum nl80211_commands cmd,
8722 			    enum nl80211_attrs attr,
8723 			    const struct nl80211_vendor_cmd_info *info,
8724 			    gfp_t gfp)
8725 {
8726 	struct sk_buff *skb;
8727 	void *hdr;
8728 	struct nlattr *data;
8729 
8730 	skb = nlmsg_new(approxlen + 100, gfp);
8731 	if (!skb)
8732 		return NULL;
8733 
8734 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
8735 	if (!hdr) {
8736 		kfree_skb(skb);
8737 		return NULL;
8738 	}
8739 
8740 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
8741 		goto nla_put_failure;
8742 
8743 	if (info) {
8744 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
8745 				info->vendor_id))
8746 			goto nla_put_failure;
8747 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
8748 				info->subcmd))
8749 			goto nla_put_failure;
8750 	}
8751 
8752 	if (wdev) {
8753 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
8754 				      wdev_id(wdev), NL80211_ATTR_PAD))
8755 			goto nla_put_failure;
8756 		if (wdev->netdev &&
8757 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
8758 				wdev->netdev->ifindex))
8759 			goto nla_put_failure;
8760 	}
8761 
8762 	data = nla_nest_start(skb, attr);
8763 	if (!data)
8764 		goto nla_put_failure;
8765 
8766 	((void **)skb->cb)[0] = rdev;
8767 	((void **)skb->cb)[1] = hdr;
8768 	((void **)skb->cb)[2] = data;
8769 
8770 	return skb;
8771 
8772  nla_put_failure:
8773 	kfree_skb(skb);
8774 	return NULL;
8775 }
8776 
8777 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
8778 					   struct wireless_dev *wdev,
8779 					   enum nl80211_commands cmd,
8780 					   enum nl80211_attrs attr,
8781 					   int vendor_event_idx,
8782 					   int approxlen, gfp_t gfp)
8783 {
8784 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
8785 	const struct nl80211_vendor_cmd_info *info;
8786 
8787 	switch (cmd) {
8788 	case NL80211_CMD_TESTMODE:
8789 		if (WARN_ON(vendor_event_idx != -1))
8790 			return NULL;
8791 		info = NULL;
8792 		break;
8793 	case NL80211_CMD_VENDOR:
8794 		if (WARN_ON(vendor_event_idx < 0 ||
8795 			    vendor_event_idx >= wiphy->n_vendor_events))
8796 			return NULL;
8797 		info = &wiphy->vendor_events[vendor_event_idx];
8798 		break;
8799 	default:
8800 		WARN_ON(1);
8801 		return NULL;
8802 	}
8803 
8804 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
8805 					   cmd, attr, info, gfp);
8806 }
8807 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
8808 
8809 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
8810 {
8811 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8812 	void *hdr = ((void **)skb->cb)[1];
8813 	struct nlattr *data = ((void **)skb->cb)[2];
8814 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
8815 
8816 	/* clear CB data for netlink core to own from now on */
8817 	memset(skb->cb, 0, sizeof(skb->cb));
8818 
8819 	nla_nest_end(skb, data);
8820 	genlmsg_end(skb, hdr);
8821 
8822 	if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
8823 		mcgrp = NL80211_MCGRP_VENDOR;
8824 
8825 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
8826 				mcgrp, gfp);
8827 }
8828 EXPORT_SYMBOL(__cfg80211_send_event_skb);
8829 
8830 #ifdef CONFIG_NL80211_TESTMODE
8831 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
8832 {
8833 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8834 	struct wireless_dev *wdev =
8835 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8836 	int err;
8837 
8838 	if (!rdev->ops->testmode_cmd)
8839 		return -EOPNOTSUPP;
8840 
8841 	if (IS_ERR(wdev)) {
8842 		err = PTR_ERR(wdev);
8843 		if (err != -EINVAL)
8844 			return err;
8845 		wdev = NULL;
8846 	} else if (wdev->wiphy != &rdev->wiphy) {
8847 		return -EINVAL;
8848 	}
8849 
8850 	if (!info->attrs[NL80211_ATTR_TESTDATA])
8851 		return -EINVAL;
8852 
8853 	rdev->cur_cmd_info = info;
8854 	err = rdev_testmode_cmd(rdev, wdev,
8855 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
8856 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
8857 	rdev->cur_cmd_info = NULL;
8858 
8859 	return err;
8860 }
8861 
8862 static int nl80211_testmode_dump(struct sk_buff *skb,
8863 				 struct netlink_callback *cb)
8864 {
8865 	struct cfg80211_registered_device *rdev;
8866 	int err;
8867 	long phy_idx;
8868 	void *data = NULL;
8869 	int data_len = 0;
8870 
8871 	rtnl_lock();
8872 
8873 	if (cb->args[0]) {
8874 		/*
8875 		 * 0 is a valid index, but not valid for args[0],
8876 		 * so we need to offset by 1.
8877 		 */
8878 		phy_idx = cb->args[0] - 1;
8879 
8880 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
8881 		if (!rdev) {
8882 			err = -ENOENT;
8883 			goto out_err;
8884 		}
8885 	} else {
8886 		struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8887 
8888 		err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
8889 				  attrbuf, nl80211_fam.maxattr,
8890 				  nl80211_policy, NULL);
8891 		if (err)
8892 			goto out_err;
8893 
8894 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
8895 		if (IS_ERR(rdev)) {
8896 			err = PTR_ERR(rdev);
8897 			goto out_err;
8898 		}
8899 		phy_idx = rdev->wiphy_idx;
8900 
8901 		if (attrbuf[NL80211_ATTR_TESTDATA])
8902 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
8903 	}
8904 
8905 	if (cb->args[1]) {
8906 		data = nla_data((void *)cb->args[1]);
8907 		data_len = nla_len((void *)cb->args[1]);
8908 	}
8909 
8910 	if (!rdev->ops->testmode_dump) {
8911 		err = -EOPNOTSUPP;
8912 		goto out_err;
8913 	}
8914 
8915 	while (1) {
8916 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
8917 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
8918 					   NL80211_CMD_TESTMODE);
8919 		struct nlattr *tmdata;
8920 
8921 		if (!hdr)
8922 			break;
8923 
8924 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
8925 			genlmsg_cancel(skb, hdr);
8926 			break;
8927 		}
8928 
8929 		tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
8930 		if (!tmdata) {
8931 			genlmsg_cancel(skb, hdr);
8932 			break;
8933 		}
8934 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
8935 		nla_nest_end(skb, tmdata);
8936 
8937 		if (err == -ENOBUFS || err == -ENOENT) {
8938 			genlmsg_cancel(skb, hdr);
8939 			break;
8940 		} else if (err) {
8941 			genlmsg_cancel(skb, hdr);
8942 			goto out_err;
8943 		}
8944 
8945 		genlmsg_end(skb, hdr);
8946 	}
8947 
8948 	err = skb->len;
8949 	/* see above */
8950 	cb->args[0] = phy_idx + 1;
8951  out_err:
8952 	rtnl_unlock();
8953 	return err;
8954 }
8955 #endif
8956 
8957 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
8958 {
8959 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8960 	struct net_device *dev = info->user_ptr[1];
8961 	struct cfg80211_connect_params connect;
8962 	struct wiphy *wiphy;
8963 	struct cfg80211_cached_keys *connkeys = NULL;
8964 	int err;
8965 
8966 	memset(&connect, 0, sizeof(connect));
8967 
8968 	if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8969 		return -EINVAL;
8970 
8971 	if (!info->attrs[NL80211_ATTR_SSID] ||
8972 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
8973 		return -EINVAL;
8974 
8975 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
8976 		connect.auth_type =
8977 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8978 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
8979 					     NL80211_CMD_CONNECT))
8980 			return -EINVAL;
8981 	} else
8982 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
8983 
8984 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
8985 
8986 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
8987 	    !wiphy_ext_feature_isset(&rdev->wiphy,
8988 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
8989 		return -EINVAL;
8990 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
8991 
8992 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
8993 				      NL80211_MAX_NR_CIPHER_SUITES);
8994 	if (err)
8995 		return err;
8996 
8997 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8998 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8999 		return -EOPNOTSUPP;
9000 
9001 	wiphy = &rdev->wiphy;
9002 
9003 	connect.bg_scan_period = -1;
9004 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9005 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9006 		connect.bg_scan_period =
9007 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9008 	}
9009 
9010 	if (info->attrs[NL80211_ATTR_MAC])
9011 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9012 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
9013 		connect.bssid_hint =
9014 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9015 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9016 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9017 
9018 	if (info->attrs[NL80211_ATTR_IE]) {
9019 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9020 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9021 	}
9022 
9023 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9024 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9025 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
9026 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9027 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
9028 			return -EOPNOTSUPP;
9029 
9030 		if (connect.mfp != NL80211_MFP_REQUIRED &&
9031 		    connect.mfp != NL80211_MFP_NO &&
9032 		    connect.mfp != NL80211_MFP_OPTIONAL)
9033 			return -EINVAL;
9034 	} else {
9035 		connect.mfp = NL80211_MFP_NO;
9036 	}
9037 
9038 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9039 		connect.prev_bssid =
9040 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9041 
9042 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9043 		connect.channel = nl80211_get_valid_chan(
9044 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9045 		if (!connect.channel)
9046 			return -EINVAL;
9047 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9048 		connect.channel_hint = nl80211_get_valid_chan(
9049 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9050 		if (!connect.channel_hint)
9051 			return -EINVAL;
9052 	}
9053 
9054 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9055 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9056 		if (IS_ERR(connkeys))
9057 			return PTR_ERR(connkeys);
9058 	}
9059 
9060 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9061 		connect.flags |= ASSOC_REQ_DISABLE_HT;
9062 
9063 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9064 		memcpy(&connect.ht_capa_mask,
9065 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9066 		       sizeof(connect.ht_capa_mask));
9067 
9068 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9069 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9070 			kzfree(connkeys);
9071 			return -EINVAL;
9072 		}
9073 		memcpy(&connect.ht_capa,
9074 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9075 		       sizeof(connect.ht_capa));
9076 	}
9077 
9078 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9079 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
9080 
9081 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9082 		memcpy(&connect.vht_capa_mask,
9083 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9084 		       sizeof(connect.vht_capa_mask));
9085 
9086 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9087 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
9088 			kzfree(connkeys);
9089 			return -EINVAL;
9090 		}
9091 		memcpy(&connect.vht_capa,
9092 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9093 		       sizeof(connect.vht_capa));
9094 	}
9095 
9096 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9097 		if (!((rdev->wiphy.features &
9098 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9099 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9100 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9101 					     NL80211_EXT_FEATURE_RRM)) {
9102 			kzfree(connkeys);
9103 			return -EINVAL;
9104 		}
9105 		connect.flags |= ASSOC_REQ_USE_RRM;
9106 	}
9107 
9108 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
9109 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
9110 		kzfree(connkeys);
9111 		return -EOPNOTSUPP;
9112 	}
9113 
9114 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
9115 		/* bss selection makes no sense if bssid is set */
9116 		if (connect.bssid) {
9117 			kzfree(connkeys);
9118 			return -EINVAL;
9119 		}
9120 
9121 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
9122 				       wiphy, &connect.bss_select);
9123 		if (err) {
9124 			kzfree(connkeys);
9125 			return err;
9126 		}
9127 	}
9128 
9129 	if (wiphy_ext_feature_isset(&rdev->wiphy,
9130 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
9131 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9132 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9133 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9134 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9135 		connect.fils_erp_username =
9136 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9137 		connect.fils_erp_username_len =
9138 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9139 		connect.fils_erp_realm =
9140 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9141 		connect.fils_erp_realm_len =
9142 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9143 		connect.fils_erp_next_seq_num =
9144 			nla_get_u16(
9145 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9146 		connect.fils_erp_rrk =
9147 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9148 		connect.fils_erp_rrk_len =
9149 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9150 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9151 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9152 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9153 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9154 		kzfree(connkeys);
9155 		return -EINVAL;
9156 	}
9157 
9158 	wdev_lock(dev->ieee80211_ptr);
9159 
9160 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
9161 			       connect.prev_bssid);
9162 	if (err)
9163 		kzfree(connkeys);
9164 
9165 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9166 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9167 		if (connect.bssid)
9168 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9169 			       connect.bssid, ETH_ALEN);
9170 		else
9171 			memset(dev->ieee80211_ptr->disconnect_bssid,
9172 			       0, ETH_ALEN);
9173 	}
9174 
9175 	wdev_unlock(dev->ieee80211_ptr);
9176 
9177 	return err;
9178 }
9179 
9180 static int nl80211_update_connect_params(struct sk_buff *skb,
9181 					 struct genl_info *info)
9182 {
9183 	struct cfg80211_connect_params connect = {};
9184 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9185 	struct net_device *dev = info->user_ptr[1];
9186 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9187 	u32 changed = 0;
9188 	int ret;
9189 
9190 	if (!rdev->ops->update_connect_params)
9191 		return -EOPNOTSUPP;
9192 
9193 	if (info->attrs[NL80211_ATTR_IE]) {
9194 		if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9195 			return -EINVAL;
9196 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9197 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9198 		changed |= UPDATE_ASSOC_IES;
9199 	}
9200 
9201 	wdev_lock(dev->ieee80211_ptr);
9202 	if (!wdev->current_bss)
9203 		ret = -ENOLINK;
9204 	else
9205 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
9206 	wdev_unlock(dev->ieee80211_ptr);
9207 
9208 	return ret;
9209 }
9210 
9211 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
9212 {
9213 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9214 	struct net_device *dev = info->user_ptr[1];
9215 	u16 reason;
9216 	int ret;
9217 
9218 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9219 		reason = WLAN_REASON_DEAUTH_LEAVING;
9220 	else
9221 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9222 
9223 	if (reason == 0)
9224 		return -EINVAL;
9225 
9226 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9227 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9228 		return -EOPNOTSUPP;
9229 
9230 	wdev_lock(dev->ieee80211_ptr);
9231 	ret = cfg80211_disconnect(rdev, dev, reason, true);
9232 	wdev_unlock(dev->ieee80211_ptr);
9233 	return ret;
9234 }
9235 
9236 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
9237 {
9238 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9239 	struct net *net;
9240 	int err;
9241 
9242 	if (info->attrs[NL80211_ATTR_PID]) {
9243 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
9244 
9245 		net = get_net_ns_by_pid(pid);
9246 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
9247 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
9248 
9249 		net = get_net_ns_by_fd(fd);
9250 	} else {
9251 		return -EINVAL;
9252 	}
9253 
9254 	if (IS_ERR(net))
9255 		return PTR_ERR(net);
9256 
9257 	err = 0;
9258 
9259 	/* check if anything to do */
9260 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
9261 		err = cfg80211_switch_netns(rdev, net);
9262 
9263 	put_net(net);
9264 	return err;
9265 }
9266 
9267 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
9268 {
9269 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9270 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
9271 			struct cfg80211_pmksa *pmksa) = NULL;
9272 	struct net_device *dev = info->user_ptr[1];
9273 	struct cfg80211_pmksa pmksa;
9274 
9275 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
9276 
9277 	if (!info->attrs[NL80211_ATTR_PMKID])
9278 		return -EINVAL;
9279 
9280 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
9281 
9282 	if (info->attrs[NL80211_ATTR_MAC]) {
9283 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9284 	} else if (info->attrs[NL80211_ATTR_SSID] &&
9285 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
9286 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
9287 		    info->attrs[NL80211_ATTR_PMK])) {
9288 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9289 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9290 		pmksa.cache_id =
9291 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
9292 	} else {
9293 		return -EINVAL;
9294 	}
9295 	if (info->attrs[NL80211_ATTR_PMK]) {
9296 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9297 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
9298 	}
9299 
9300 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9301 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9302 		return -EOPNOTSUPP;
9303 
9304 	switch (info->genlhdr->cmd) {
9305 	case NL80211_CMD_SET_PMKSA:
9306 		rdev_ops = rdev->ops->set_pmksa;
9307 		break;
9308 	case NL80211_CMD_DEL_PMKSA:
9309 		rdev_ops = rdev->ops->del_pmksa;
9310 		break;
9311 	default:
9312 		WARN_ON(1);
9313 		break;
9314 	}
9315 
9316 	if (!rdev_ops)
9317 		return -EOPNOTSUPP;
9318 
9319 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
9320 }
9321 
9322 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
9323 {
9324 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9325 	struct net_device *dev = info->user_ptr[1];
9326 
9327 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9328 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9329 		return -EOPNOTSUPP;
9330 
9331 	if (!rdev->ops->flush_pmksa)
9332 		return -EOPNOTSUPP;
9333 
9334 	return rdev_flush_pmksa(rdev, dev);
9335 }
9336 
9337 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
9338 {
9339 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9340 	struct net_device *dev = info->user_ptr[1];
9341 	u8 action_code, dialog_token;
9342 	u32 peer_capability = 0;
9343 	u16 status_code;
9344 	u8 *peer;
9345 	bool initiator;
9346 
9347 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9348 	    !rdev->ops->tdls_mgmt)
9349 		return -EOPNOTSUPP;
9350 
9351 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
9352 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
9353 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
9354 	    !info->attrs[NL80211_ATTR_IE] ||
9355 	    !info->attrs[NL80211_ATTR_MAC])
9356 		return -EINVAL;
9357 
9358 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9359 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
9360 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
9361 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
9362 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
9363 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
9364 		peer_capability =
9365 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
9366 
9367 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
9368 			      dialog_token, status_code, peer_capability,
9369 			      initiator,
9370 			      nla_data(info->attrs[NL80211_ATTR_IE]),
9371 			      nla_len(info->attrs[NL80211_ATTR_IE]));
9372 }
9373 
9374 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
9375 {
9376 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9377 	struct net_device *dev = info->user_ptr[1];
9378 	enum nl80211_tdls_operation operation;
9379 	u8 *peer;
9380 
9381 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9382 	    !rdev->ops->tdls_oper)
9383 		return -EOPNOTSUPP;
9384 
9385 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
9386 	    !info->attrs[NL80211_ATTR_MAC])
9387 		return -EINVAL;
9388 
9389 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
9390 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9391 
9392 	return rdev_tdls_oper(rdev, dev, peer, operation);
9393 }
9394 
9395 static int nl80211_remain_on_channel(struct sk_buff *skb,
9396 				     struct genl_info *info)
9397 {
9398 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9399 	struct wireless_dev *wdev = info->user_ptr[1];
9400 	struct cfg80211_chan_def chandef;
9401 	const struct cfg80211_chan_def *compat_chandef;
9402 	struct sk_buff *msg;
9403 	void *hdr;
9404 	u64 cookie;
9405 	u32 duration;
9406 	int err;
9407 
9408 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9409 	    !info->attrs[NL80211_ATTR_DURATION])
9410 		return -EINVAL;
9411 
9412 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9413 
9414 	if (!rdev->ops->remain_on_channel ||
9415 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
9416 		return -EOPNOTSUPP;
9417 
9418 	/*
9419 	 * We should be on that channel for at least a minimum amount of
9420 	 * time (10ms) but no longer than the driver supports.
9421 	 */
9422 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9423 	    duration > rdev->wiphy.max_remain_on_channel_duration)
9424 		return -EINVAL;
9425 
9426 	err = nl80211_parse_chandef(rdev, info, &chandef);
9427 	if (err)
9428 		return err;
9429 
9430 	wdev_lock(wdev);
9431 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
9432 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
9433 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
9434 							     &chandef);
9435 		if (compat_chandef != &chandef) {
9436 			wdev_unlock(wdev);
9437 			return -EBUSY;
9438 		}
9439 	}
9440 	wdev_unlock(wdev);
9441 
9442 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9443 	if (!msg)
9444 		return -ENOMEM;
9445 
9446 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9447 			     NL80211_CMD_REMAIN_ON_CHANNEL);
9448 	if (!hdr) {
9449 		err = -ENOBUFS;
9450 		goto free_msg;
9451 	}
9452 
9453 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
9454 				     duration, &cookie);
9455 
9456 	if (err)
9457 		goto free_msg;
9458 
9459 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9460 			      NL80211_ATTR_PAD))
9461 		goto nla_put_failure;
9462 
9463 	genlmsg_end(msg, hdr);
9464 
9465 	return genlmsg_reply(msg, info);
9466 
9467  nla_put_failure:
9468 	err = -ENOBUFS;
9469  free_msg:
9470 	nlmsg_free(msg);
9471 	return err;
9472 }
9473 
9474 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
9475 					    struct genl_info *info)
9476 {
9477 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9478 	struct wireless_dev *wdev = info->user_ptr[1];
9479 	u64 cookie;
9480 
9481 	if (!info->attrs[NL80211_ATTR_COOKIE])
9482 		return -EINVAL;
9483 
9484 	if (!rdev->ops->cancel_remain_on_channel)
9485 		return -EOPNOTSUPP;
9486 
9487 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9488 
9489 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
9490 }
9491 
9492 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
9493 				       struct genl_info *info)
9494 {
9495 	struct cfg80211_bitrate_mask mask;
9496 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9497 	struct net_device *dev = info->user_ptr[1];
9498 	int err;
9499 
9500 	if (!rdev->ops->set_bitrate_mask)
9501 		return -EOPNOTSUPP;
9502 
9503 	err = nl80211_parse_tx_bitrate_mask(info, &mask);
9504 	if (err)
9505 		return err;
9506 
9507 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
9508 }
9509 
9510 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
9511 {
9512 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9513 	struct wireless_dev *wdev = info->user_ptr[1];
9514 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
9515 
9516 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
9517 		return -EINVAL;
9518 
9519 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
9520 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
9521 
9522 	switch (wdev->iftype) {
9523 	case NL80211_IFTYPE_STATION:
9524 	case NL80211_IFTYPE_ADHOC:
9525 	case NL80211_IFTYPE_P2P_CLIENT:
9526 	case NL80211_IFTYPE_AP:
9527 	case NL80211_IFTYPE_AP_VLAN:
9528 	case NL80211_IFTYPE_MESH_POINT:
9529 	case NL80211_IFTYPE_P2P_GO:
9530 	case NL80211_IFTYPE_P2P_DEVICE:
9531 		break;
9532 	case NL80211_IFTYPE_NAN:
9533 	default:
9534 		return -EOPNOTSUPP;
9535 	}
9536 
9537 	/* not much point in registering if we can't reply */
9538 	if (!rdev->ops->mgmt_tx)
9539 		return -EOPNOTSUPP;
9540 
9541 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
9542 			nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
9543 			nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
9544 }
9545 
9546 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
9547 {
9548 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9549 	struct wireless_dev *wdev = info->user_ptr[1];
9550 	struct cfg80211_chan_def chandef;
9551 	int err;
9552 	void *hdr = NULL;
9553 	u64 cookie;
9554 	struct sk_buff *msg = NULL;
9555 	struct cfg80211_mgmt_tx_params params = {
9556 		.dont_wait_for_ack =
9557 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
9558 	};
9559 
9560 	if (!info->attrs[NL80211_ATTR_FRAME])
9561 		return -EINVAL;
9562 
9563 	if (!rdev->ops->mgmt_tx)
9564 		return -EOPNOTSUPP;
9565 
9566 	switch (wdev->iftype) {
9567 	case NL80211_IFTYPE_P2P_DEVICE:
9568 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9569 			return -EINVAL;
9570 	case NL80211_IFTYPE_STATION:
9571 	case NL80211_IFTYPE_ADHOC:
9572 	case NL80211_IFTYPE_P2P_CLIENT:
9573 	case NL80211_IFTYPE_AP:
9574 	case NL80211_IFTYPE_AP_VLAN:
9575 	case NL80211_IFTYPE_MESH_POINT:
9576 	case NL80211_IFTYPE_P2P_GO:
9577 		break;
9578 	case NL80211_IFTYPE_NAN:
9579 	default:
9580 		return -EOPNOTSUPP;
9581 	}
9582 
9583 	if (info->attrs[NL80211_ATTR_DURATION]) {
9584 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9585 			return -EINVAL;
9586 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9587 
9588 		/*
9589 		 * We should wait on the channel for at least a minimum amount
9590 		 * of time (10ms) but no longer than the driver supports.
9591 		 */
9592 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9593 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
9594 			return -EINVAL;
9595 	}
9596 
9597 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
9598 
9599 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9600 		return -EINVAL;
9601 
9602 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9603 
9604 	/* get the channel if any has been specified, otherwise pass NULL to
9605 	 * the driver. The latter will use the current one
9606 	 */
9607 	chandef.chan = NULL;
9608 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9609 		err = nl80211_parse_chandef(rdev, info, &chandef);
9610 		if (err)
9611 			return err;
9612 	}
9613 
9614 	if (!chandef.chan && params.offchan)
9615 		return -EINVAL;
9616 
9617 	wdev_lock(wdev);
9618 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
9619 		wdev_unlock(wdev);
9620 		return -EBUSY;
9621 	}
9622 	wdev_unlock(wdev);
9623 
9624 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
9625 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
9626 
9627 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
9628 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9629 		int i;
9630 
9631 		if (len % sizeof(u16))
9632 			return -EINVAL;
9633 
9634 		params.n_csa_offsets = len / sizeof(u16);
9635 		params.csa_offsets =
9636 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9637 
9638 		/* check that all the offsets fit the frame */
9639 		for (i = 0; i < params.n_csa_offsets; i++) {
9640 			if (params.csa_offsets[i] >= params.len)
9641 				return -EINVAL;
9642 		}
9643 	}
9644 
9645 	if (!params.dont_wait_for_ack) {
9646 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9647 		if (!msg)
9648 			return -ENOMEM;
9649 
9650 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9651 				     NL80211_CMD_FRAME);
9652 		if (!hdr) {
9653 			err = -ENOBUFS;
9654 			goto free_msg;
9655 		}
9656 	}
9657 
9658 	params.chan = chandef.chan;
9659 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
9660 	if (err)
9661 		goto free_msg;
9662 
9663 	if (msg) {
9664 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9665 				      NL80211_ATTR_PAD))
9666 			goto nla_put_failure;
9667 
9668 		genlmsg_end(msg, hdr);
9669 		return genlmsg_reply(msg, info);
9670 	}
9671 
9672 	return 0;
9673 
9674  nla_put_failure:
9675 	err = -ENOBUFS;
9676  free_msg:
9677 	nlmsg_free(msg);
9678 	return err;
9679 }
9680 
9681 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
9682 {
9683 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9684 	struct wireless_dev *wdev = info->user_ptr[1];
9685 	u64 cookie;
9686 
9687 	if (!info->attrs[NL80211_ATTR_COOKIE])
9688 		return -EINVAL;
9689 
9690 	if (!rdev->ops->mgmt_tx_cancel_wait)
9691 		return -EOPNOTSUPP;
9692 
9693 	switch (wdev->iftype) {
9694 	case NL80211_IFTYPE_STATION:
9695 	case NL80211_IFTYPE_ADHOC:
9696 	case NL80211_IFTYPE_P2P_CLIENT:
9697 	case NL80211_IFTYPE_AP:
9698 	case NL80211_IFTYPE_AP_VLAN:
9699 	case NL80211_IFTYPE_P2P_GO:
9700 	case NL80211_IFTYPE_P2P_DEVICE:
9701 		break;
9702 	case NL80211_IFTYPE_NAN:
9703 	default:
9704 		return -EOPNOTSUPP;
9705 	}
9706 
9707 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9708 
9709 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
9710 }
9711 
9712 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
9713 {
9714 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9715 	struct wireless_dev *wdev;
9716 	struct net_device *dev = info->user_ptr[1];
9717 	u8 ps_state;
9718 	bool state;
9719 	int err;
9720 
9721 	if (!info->attrs[NL80211_ATTR_PS_STATE])
9722 		return -EINVAL;
9723 
9724 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
9725 
9726 	if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
9727 		return -EINVAL;
9728 
9729 	wdev = dev->ieee80211_ptr;
9730 
9731 	if (!rdev->ops->set_power_mgmt)
9732 		return -EOPNOTSUPP;
9733 
9734 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
9735 
9736 	if (state == wdev->ps)
9737 		return 0;
9738 
9739 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
9740 	if (!err)
9741 		wdev->ps = state;
9742 	return err;
9743 }
9744 
9745 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
9746 {
9747 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9748 	enum nl80211_ps_state ps_state;
9749 	struct wireless_dev *wdev;
9750 	struct net_device *dev = info->user_ptr[1];
9751 	struct sk_buff *msg;
9752 	void *hdr;
9753 	int err;
9754 
9755 	wdev = dev->ieee80211_ptr;
9756 
9757 	if (!rdev->ops->set_power_mgmt)
9758 		return -EOPNOTSUPP;
9759 
9760 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9761 	if (!msg)
9762 		return -ENOMEM;
9763 
9764 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9765 			     NL80211_CMD_GET_POWER_SAVE);
9766 	if (!hdr) {
9767 		err = -ENOBUFS;
9768 		goto free_msg;
9769 	}
9770 
9771 	if (wdev->ps)
9772 		ps_state = NL80211_PS_ENABLED;
9773 	else
9774 		ps_state = NL80211_PS_DISABLED;
9775 
9776 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
9777 		goto nla_put_failure;
9778 
9779 	genlmsg_end(msg, hdr);
9780 	return genlmsg_reply(msg, info);
9781 
9782  nla_put_failure:
9783 	err = -ENOBUFS;
9784  free_msg:
9785 	nlmsg_free(msg);
9786 	return err;
9787 }
9788 
9789 static const struct nla_policy
9790 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
9791 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
9792 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
9793 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
9794 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
9795 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
9796 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
9797 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
9798 };
9799 
9800 static int nl80211_set_cqm_txe(struct genl_info *info,
9801 			       u32 rate, u32 pkts, u32 intvl)
9802 {
9803 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9804 	struct net_device *dev = info->user_ptr[1];
9805 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9806 
9807 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
9808 		return -EINVAL;
9809 
9810 	if (!rdev->ops->set_cqm_txe_config)
9811 		return -EOPNOTSUPP;
9812 
9813 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
9814 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9815 		return -EOPNOTSUPP;
9816 
9817 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
9818 }
9819 
9820 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
9821 				    struct net_device *dev)
9822 {
9823 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9824 	s32 last, low, high;
9825 	u32 hyst;
9826 	int i, n;
9827 	int err;
9828 
9829 	/* RSSI reporting disabled? */
9830 	if (!wdev->cqm_config)
9831 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
9832 
9833 	/*
9834 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
9835 	 * event has been received yet, we should receive an event after a
9836 	 * connection is established and enough beacons received to calculate
9837 	 * the average.
9838 	 */
9839 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
9840 	    rdev->ops->get_station) {
9841 		struct station_info sinfo = {};
9842 		u8 *mac_addr;
9843 
9844 		mac_addr = wdev->current_bss->pub.bssid;
9845 
9846 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
9847 		if (err)
9848 			return err;
9849 
9850 		if (sinfo.filled & BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
9851 			wdev->cqm_config->last_rssi_event_value =
9852 				(s8) sinfo.rx_beacon_signal_avg;
9853 	}
9854 
9855 	last = wdev->cqm_config->last_rssi_event_value;
9856 	hyst = wdev->cqm_config->rssi_hyst;
9857 	n = wdev->cqm_config->n_rssi_thresholds;
9858 
9859 	for (i = 0; i < n; i++)
9860 		if (last < wdev->cqm_config->rssi_thresholds[i])
9861 			break;
9862 
9863 	low = i > 0 ?
9864 		(wdev->cqm_config->rssi_thresholds[i - 1] - hyst) : S32_MIN;
9865 	high = i < n ?
9866 		(wdev->cqm_config->rssi_thresholds[i] + hyst - 1) : S32_MAX;
9867 
9868 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
9869 }
9870 
9871 static int nl80211_set_cqm_rssi(struct genl_info *info,
9872 				const s32 *thresholds, int n_thresholds,
9873 				u32 hysteresis)
9874 {
9875 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9876 	struct net_device *dev = info->user_ptr[1];
9877 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9878 	int i, err;
9879 	s32 prev = S32_MIN;
9880 
9881 	/* Check all values negative and sorted */
9882 	for (i = 0; i < n_thresholds; i++) {
9883 		if (thresholds[i] > 0 || thresholds[i] <= prev)
9884 			return -EINVAL;
9885 
9886 		prev = thresholds[i];
9887 	}
9888 
9889 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
9890 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9891 		return -EOPNOTSUPP;
9892 
9893 	wdev_lock(wdev);
9894 	cfg80211_cqm_config_free(wdev);
9895 	wdev_unlock(wdev);
9896 
9897 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
9898 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
9899 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
9900 
9901 		return rdev_set_cqm_rssi_config(rdev, dev,
9902 						thresholds[0], hysteresis);
9903 	}
9904 
9905 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
9906 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
9907 		return -EOPNOTSUPP;
9908 
9909 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
9910 		n_thresholds = 0;
9911 
9912 	wdev_lock(wdev);
9913 	if (n_thresholds) {
9914 		struct cfg80211_cqm_config *cqm_config;
9915 
9916 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
9917 				     n_thresholds * sizeof(s32), GFP_KERNEL);
9918 		if (!cqm_config) {
9919 			err = -ENOMEM;
9920 			goto unlock;
9921 		}
9922 
9923 		cqm_config->rssi_hyst = hysteresis;
9924 		cqm_config->n_rssi_thresholds = n_thresholds;
9925 		memcpy(cqm_config->rssi_thresholds, thresholds,
9926 		       n_thresholds * sizeof(s32));
9927 
9928 		wdev->cqm_config = cqm_config;
9929 	}
9930 
9931 	err = cfg80211_cqm_rssi_update(rdev, dev);
9932 
9933 unlock:
9934 	wdev_unlock(wdev);
9935 
9936 	return err;
9937 }
9938 
9939 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
9940 {
9941 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
9942 	struct nlattr *cqm;
9943 	int err;
9944 
9945 	cqm = info->attrs[NL80211_ATTR_CQM];
9946 	if (!cqm)
9947 		return -EINVAL;
9948 
9949 	err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
9950 			       nl80211_attr_cqm_policy, info->extack);
9951 	if (err)
9952 		return err;
9953 
9954 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
9955 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
9956 		const s32 *thresholds =
9957 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
9958 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
9959 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
9960 
9961 		if (len % 4)
9962 			return -EINVAL;
9963 
9964 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
9965 					    hysteresis);
9966 	}
9967 
9968 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
9969 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
9970 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
9971 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
9972 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
9973 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
9974 
9975 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
9976 	}
9977 
9978 	return -EINVAL;
9979 }
9980 
9981 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
9982 {
9983 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9984 	struct net_device *dev = info->user_ptr[1];
9985 	struct ocb_setup setup = {};
9986 	int err;
9987 
9988 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
9989 	if (err)
9990 		return err;
9991 
9992 	return cfg80211_join_ocb(rdev, dev, &setup);
9993 }
9994 
9995 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
9996 {
9997 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9998 	struct net_device *dev = info->user_ptr[1];
9999 
10000 	return cfg80211_leave_ocb(rdev, dev);
10001 }
10002 
10003 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
10004 {
10005 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10006 	struct net_device *dev = info->user_ptr[1];
10007 	struct mesh_config cfg;
10008 	struct mesh_setup setup;
10009 	int err;
10010 
10011 	/* start with default */
10012 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
10013 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
10014 
10015 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
10016 		/* and parse parameters if given */
10017 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
10018 		if (err)
10019 			return err;
10020 	}
10021 
10022 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
10023 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
10024 		return -EINVAL;
10025 
10026 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
10027 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
10028 
10029 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10030 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
10031 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10032 			return -EINVAL;
10033 
10034 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
10035 		setup.beacon_interval =
10036 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10037 
10038 		err = cfg80211_validate_beacon_int(rdev,
10039 						   NL80211_IFTYPE_MESH_POINT,
10040 						   setup.beacon_interval);
10041 		if (err)
10042 			return err;
10043 	}
10044 
10045 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
10046 		setup.dtim_period =
10047 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
10048 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
10049 			return -EINVAL;
10050 	}
10051 
10052 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
10053 		/* parse additional setup parameters if given */
10054 		err = nl80211_parse_mesh_setup(info, &setup);
10055 		if (err)
10056 			return err;
10057 	}
10058 
10059 	if (setup.user_mpm)
10060 		cfg.auto_open_plinks = false;
10061 
10062 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10063 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10064 		if (err)
10065 			return err;
10066 	} else {
10067 		/* cfg80211_join_mesh() will sort it out */
10068 		setup.chandef.chan = NULL;
10069 	}
10070 
10071 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10072 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10073 		int n_rates =
10074 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10075 		struct ieee80211_supported_band *sband;
10076 
10077 		if (!setup.chandef.chan)
10078 			return -EINVAL;
10079 
10080 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
10081 
10082 		err = ieee80211_get_ratemask(sband, rates, n_rates,
10083 					     &setup.basic_rates);
10084 		if (err)
10085 			return err;
10086 	}
10087 
10088 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
10089 		err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
10090 		if (err)
10091 			return err;
10092 
10093 		if (!setup.chandef.chan)
10094 			return -EINVAL;
10095 
10096 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
10097 					      &setup.beacon_rate);
10098 		if (err)
10099 			return err;
10100 	}
10101 
10102 	setup.userspace_handles_dfs =
10103 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10104 
10105 	return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
10106 }
10107 
10108 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
10109 {
10110 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10111 	struct net_device *dev = info->user_ptr[1];
10112 
10113 	return cfg80211_leave_mesh(rdev, dev);
10114 }
10115 
10116 #ifdef CONFIG_PM
10117 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
10118 					struct cfg80211_registered_device *rdev)
10119 {
10120 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
10121 	struct nlattr *nl_pats, *nl_pat;
10122 	int i, pat_len;
10123 
10124 	if (!wowlan->n_patterns)
10125 		return 0;
10126 
10127 	nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
10128 	if (!nl_pats)
10129 		return -ENOBUFS;
10130 
10131 	for (i = 0; i < wowlan->n_patterns; i++) {
10132 		nl_pat = nla_nest_start(msg, i + 1);
10133 		if (!nl_pat)
10134 			return -ENOBUFS;
10135 		pat_len = wowlan->patterns[i].pattern_len;
10136 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
10137 			    wowlan->patterns[i].mask) ||
10138 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10139 			    wowlan->patterns[i].pattern) ||
10140 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10141 				wowlan->patterns[i].pkt_offset))
10142 			return -ENOBUFS;
10143 		nla_nest_end(msg, nl_pat);
10144 	}
10145 	nla_nest_end(msg, nl_pats);
10146 
10147 	return 0;
10148 }
10149 
10150 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
10151 				   struct cfg80211_wowlan_tcp *tcp)
10152 {
10153 	struct nlattr *nl_tcp;
10154 
10155 	if (!tcp)
10156 		return 0;
10157 
10158 	nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
10159 	if (!nl_tcp)
10160 		return -ENOBUFS;
10161 
10162 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
10163 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
10164 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
10165 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
10166 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
10167 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
10168 		    tcp->payload_len, tcp->payload) ||
10169 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
10170 			tcp->data_interval) ||
10171 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
10172 		    tcp->wake_len, tcp->wake_data) ||
10173 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
10174 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
10175 		return -ENOBUFS;
10176 
10177 	if (tcp->payload_seq.len &&
10178 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
10179 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
10180 		return -ENOBUFS;
10181 
10182 	if (tcp->payload_tok.len &&
10183 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
10184 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
10185 		    &tcp->payload_tok))
10186 		return -ENOBUFS;
10187 
10188 	nla_nest_end(msg, nl_tcp);
10189 
10190 	return 0;
10191 }
10192 
10193 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
10194 				  struct cfg80211_sched_scan_request *req)
10195 {
10196 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
10197 	int i;
10198 
10199 	if (!req)
10200 		return 0;
10201 
10202 	nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
10203 	if (!nd)
10204 		return -ENOBUFS;
10205 
10206 	if (req->n_scan_plans == 1 &&
10207 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
10208 			req->scan_plans[0].interval * 1000))
10209 		return -ENOBUFS;
10210 
10211 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
10212 		return -ENOBUFS;
10213 
10214 	if (req->relative_rssi_set) {
10215 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
10216 
10217 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
10218 			       req->relative_rssi))
10219 			return -ENOBUFS;
10220 
10221 		rssi_adjust.band = req->rssi_adjust.band;
10222 		rssi_adjust.delta = req->rssi_adjust.delta;
10223 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
10224 			    sizeof(rssi_adjust), &rssi_adjust))
10225 			return -ENOBUFS;
10226 	}
10227 
10228 	freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10229 	if (!freqs)
10230 		return -ENOBUFS;
10231 
10232 	for (i = 0; i < req->n_channels; i++) {
10233 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10234 			return -ENOBUFS;
10235 	}
10236 
10237 	nla_nest_end(msg, freqs);
10238 
10239 	if (req->n_match_sets) {
10240 		matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
10241 		if (!matches)
10242 			return -ENOBUFS;
10243 
10244 		for (i = 0; i < req->n_match_sets; i++) {
10245 			match = nla_nest_start(msg, i);
10246 			if (!match)
10247 				return -ENOBUFS;
10248 
10249 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
10250 				    req->match_sets[i].ssid.ssid_len,
10251 				    req->match_sets[i].ssid.ssid))
10252 				return -ENOBUFS;
10253 			nla_nest_end(msg, match);
10254 		}
10255 		nla_nest_end(msg, matches);
10256 	}
10257 
10258 	scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
10259 	if (!scan_plans)
10260 		return -ENOBUFS;
10261 
10262 	for (i = 0; i < req->n_scan_plans; i++) {
10263 		scan_plan = nla_nest_start(msg, i + 1);
10264 		if (!scan_plan)
10265 			return -ENOBUFS;
10266 
10267 		if (!scan_plan ||
10268 		    nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
10269 				req->scan_plans[i].interval) ||
10270 		    (req->scan_plans[i].iterations &&
10271 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
10272 				 req->scan_plans[i].iterations)))
10273 			return -ENOBUFS;
10274 		nla_nest_end(msg, scan_plan);
10275 	}
10276 	nla_nest_end(msg, scan_plans);
10277 
10278 	nla_nest_end(msg, nd);
10279 
10280 	return 0;
10281 }
10282 
10283 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
10284 {
10285 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10286 	struct sk_buff *msg;
10287 	void *hdr;
10288 	u32 size = NLMSG_DEFAULT_SIZE;
10289 
10290 	if (!rdev->wiphy.wowlan)
10291 		return -EOPNOTSUPP;
10292 
10293 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
10294 		/* adjust size to have room for all the data */
10295 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
10296 			rdev->wiphy.wowlan_config->tcp->payload_len +
10297 			rdev->wiphy.wowlan_config->tcp->wake_len +
10298 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
10299 	}
10300 
10301 	msg = nlmsg_new(size, GFP_KERNEL);
10302 	if (!msg)
10303 		return -ENOMEM;
10304 
10305 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10306 			     NL80211_CMD_GET_WOWLAN);
10307 	if (!hdr)
10308 		goto nla_put_failure;
10309 
10310 	if (rdev->wiphy.wowlan_config) {
10311 		struct nlattr *nl_wowlan;
10312 
10313 		nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10314 		if (!nl_wowlan)
10315 			goto nla_put_failure;
10316 
10317 		if ((rdev->wiphy.wowlan_config->any &&
10318 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
10319 		    (rdev->wiphy.wowlan_config->disconnect &&
10320 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
10321 		    (rdev->wiphy.wowlan_config->magic_pkt &&
10322 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
10323 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
10324 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
10325 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
10326 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
10327 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
10328 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
10329 		    (rdev->wiphy.wowlan_config->rfkill_release &&
10330 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
10331 			goto nla_put_failure;
10332 
10333 		if (nl80211_send_wowlan_patterns(msg, rdev))
10334 			goto nla_put_failure;
10335 
10336 		if (nl80211_send_wowlan_tcp(msg,
10337 					    rdev->wiphy.wowlan_config->tcp))
10338 			goto nla_put_failure;
10339 
10340 		if (nl80211_send_wowlan_nd(
10341 			    msg,
10342 			    rdev->wiphy.wowlan_config->nd_config))
10343 			goto nla_put_failure;
10344 
10345 		nla_nest_end(msg, nl_wowlan);
10346 	}
10347 
10348 	genlmsg_end(msg, hdr);
10349 	return genlmsg_reply(msg, info);
10350 
10351 nla_put_failure:
10352 	nlmsg_free(msg);
10353 	return -ENOBUFS;
10354 }
10355 
10356 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
10357 				    struct nlattr *attr,
10358 				    struct cfg80211_wowlan *trig)
10359 {
10360 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
10361 	struct cfg80211_wowlan_tcp *cfg;
10362 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
10363 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
10364 	u32 size;
10365 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
10366 	int err, port;
10367 
10368 	if (!rdev->wiphy.wowlan->tcp)
10369 		return -EINVAL;
10370 
10371 	err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TCP, attr,
10372 			       nl80211_wowlan_tcp_policy, NULL);
10373 	if (err)
10374 		return err;
10375 
10376 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
10377 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
10378 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
10379 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
10380 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
10381 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
10382 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
10383 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
10384 		return -EINVAL;
10385 
10386 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
10387 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
10388 		return -EINVAL;
10389 
10390 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
10391 			rdev->wiphy.wowlan->tcp->data_interval_max ||
10392 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
10393 		return -EINVAL;
10394 
10395 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
10396 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
10397 		return -EINVAL;
10398 
10399 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
10400 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
10401 		return -EINVAL;
10402 
10403 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
10404 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10405 
10406 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10407 		tokens_size = tokln - sizeof(*tok);
10408 
10409 		if (!tok->len || tokens_size % tok->len)
10410 			return -EINVAL;
10411 		if (!rdev->wiphy.wowlan->tcp->tok)
10412 			return -EINVAL;
10413 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
10414 			return -EINVAL;
10415 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
10416 			return -EINVAL;
10417 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
10418 			return -EINVAL;
10419 		if (tok->offset + tok->len > data_size)
10420 			return -EINVAL;
10421 	}
10422 
10423 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
10424 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
10425 		if (!rdev->wiphy.wowlan->tcp->seq)
10426 			return -EINVAL;
10427 		if (seq->len == 0 || seq->len > 4)
10428 			return -EINVAL;
10429 		if (seq->len + seq->offset > data_size)
10430 			return -EINVAL;
10431 	}
10432 
10433 	size = sizeof(*cfg);
10434 	size += data_size;
10435 	size += wake_size + wake_mask_size;
10436 	size += tokens_size;
10437 
10438 	cfg = kzalloc(size, GFP_KERNEL);
10439 	if (!cfg)
10440 		return -ENOMEM;
10441 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
10442 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
10443 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
10444 	       ETH_ALEN);
10445 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
10446 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
10447 	else
10448 		port = 0;
10449 #ifdef CONFIG_INET
10450 	/* allocate a socket and port for it and use it */
10451 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
10452 			    IPPROTO_TCP, &cfg->sock, 1);
10453 	if (err) {
10454 		kfree(cfg);
10455 		return err;
10456 	}
10457 	if (inet_csk_get_port(cfg->sock->sk, port)) {
10458 		sock_release(cfg->sock);
10459 		kfree(cfg);
10460 		return -EADDRINUSE;
10461 	}
10462 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
10463 #else
10464 	if (!port) {
10465 		kfree(cfg);
10466 		return -EINVAL;
10467 	}
10468 	cfg->src_port = port;
10469 #endif
10470 
10471 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
10472 	cfg->payload_len = data_size;
10473 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
10474 	memcpy((void *)cfg->payload,
10475 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
10476 	       data_size);
10477 	if (seq)
10478 		cfg->payload_seq = *seq;
10479 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
10480 	cfg->wake_len = wake_size;
10481 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
10482 	memcpy((void *)cfg->wake_data,
10483 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
10484 	       wake_size);
10485 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
10486 			 data_size + wake_size;
10487 	memcpy((void *)cfg->wake_mask,
10488 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
10489 	       wake_mask_size);
10490 	if (tok) {
10491 		cfg->tokens_size = tokens_size;
10492 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
10493 	}
10494 
10495 	trig->tcp = cfg;
10496 
10497 	return 0;
10498 }
10499 
10500 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
10501 				   const struct wiphy_wowlan_support *wowlan,
10502 				   struct nlattr *attr,
10503 				   struct cfg80211_wowlan *trig)
10504 {
10505 	struct nlattr **tb;
10506 	int err;
10507 
10508 	tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
10509 	if (!tb)
10510 		return -ENOMEM;
10511 
10512 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
10513 		err = -EOPNOTSUPP;
10514 		goto out;
10515 	}
10516 
10517 	err = nla_parse_nested(tb, NL80211_ATTR_MAX, attr, nl80211_policy,
10518 			       NULL);
10519 	if (err)
10520 		goto out;
10521 
10522 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
10523 						   wowlan->max_nd_match_sets);
10524 	err = PTR_ERR_OR_ZERO(trig->nd_config);
10525 	if (err)
10526 		trig->nd_config = NULL;
10527 
10528 out:
10529 	kfree(tb);
10530 	return err;
10531 }
10532 
10533 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
10534 {
10535 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10536 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
10537 	struct cfg80211_wowlan new_triggers = {};
10538 	struct cfg80211_wowlan *ntrig;
10539 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
10540 	int err, i;
10541 	bool prev_enabled = rdev->wiphy.wowlan_config;
10542 	bool regular = false;
10543 
10544 	if (!wowlan)
10545 		return -EOPNOTSUPP;
10546 
10547 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
10548 		cfg80211_rdev_free_wowlan(rdev);
10549 		rdev->wiphy.wowlan_config = NULL;
10550 		goto set_wakeup;
10551 	}
10552 
10553 	err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TRIG,
10554 			       info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
10555 			       nl80211_wowlan_policy, info->extack);
10556 	if (err)
10557 		return err;
10558 
10559 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
10560 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
10561 			return -EINVAL;
10562 		new_triggers.any = true;
10563 	}
10564 
10565 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
10566 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
10567 			return -EINVAL;
10568 		new_triggers.disconnect = true;
10569 		regular = true;
10570 	}
10571 
10572 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
10573 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
10574 			return -EINVAL;
10575 		new_triggers.magic_pkt = true;
10576 		regular = true;
10577 	}
10578 
10579 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
10580 		return -EINVAL;
10581 
10582 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
10583 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
10584 			return -EINVAL;
10585 		new_triggers.gtk_rekey_failure = true;
10586 		regular = true;
10587 	}
10588 
10589 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
10590 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
10591 			return -EINVAL;
10592 		new_triggers.eap_identity_req = true;
10593 		regular = true;
10594 	}
10595 
10596 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
10597 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
10598 			return -EINVAL;
10599 		new_triggers.four_way_handshake = true;
10600 		regular = true;
10601 	}
10602 
10603 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
10604 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
10605 			return -EINVAL;
10606 		new_triggers.rfkill_release = true;
10607 		regular = true;
10608 	}
10609 
10610 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
10611 		struct nlattr *pat;
10612 		int n_patterns = 0;
10613 		int rem, pat_len, mask_len, pkt_offset;
10614 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10615 
10616 		regular = true;
10617 
10618 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10619 				    rem)
10620 			n_patterns++;
10621 		if (n_patterns > wowlan->n_patterns)
10622 			return -EINVAL;
10623 
10624 		new_triggers.patterns = kcalloc(n_patterns,
10625 						sizeof(new_triggers.patterns[0]),
10626 						GFP_KERNEL);
10627 		if (!new_triggers.patterns)
10628 			return -ENOMEM;
10629 
10630 		new_triggers.n_patterns = n_patterns;
10631 		i = 0;
10632 
10633 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10634 				    rem) {
10635 			u8 *mask_pat;
10636 
10637 			nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10638 					 nl80211_packet_pattern_policy,
10639 					 info->extack);
10640 			err = -EINVAL;
10641 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
10642 			    !pat_tb[NL80211_PKTPAT_PATTERN])
10643 				goto error;
10644 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10645 			mask_len = DIV_ROUND_UP(pat_len, 8);
10646 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10647 				goto error;
10648 			if (pat_len > wowlan->pattern_max_len ||
10649 			    pat_len < wowlan->pattern_min_len)
10650 				goto error;
10651 
10652 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
10653 				pkt_offset = 0;
10654 			else
10655 				pkt_offset = nla_get_u32(
10656 					pat_tb[NL80211_PKTPAT_OFFSET]);
10657 			if (pkt_offset > wowlan->max_pkt_offset)
10658 				goto error;
10659 			new_triggers.patterns[i].pkt_offset = pkt_offset;
10660 
10661 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10662 			if (!mask_pat) {
10663 				err = -ENOMEM;
10664 				goto error;
10665 			}
10666 			new_triggers.patterns[i].mask = mask_pat;
10667 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10668 			       mask_len);
10669 			mask_pat += mask_len;
10670 			new_triggers.patterns[i].pattern = mask_pat;
10671 			new_triggers.patterns[i].pattern_len = pat_len;
10672 			memcpy(mask_pat,
10673 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10674 			       pat_len);
10675 			i++;
10676 		}
10677 	}
10678 
10679 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
10680 		regular = true;
10681 		err = nl80211_parse_wowlan_tcp(
10682 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
10683 			&new_triggers);
10684 		if (err)
10685 			goto error;
10686 	}
10687 
10688 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
10689 		regular = true;
10690 		err = nl80211_parse_wowlan_nd(
10691 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
10692 			&new_triggers);
10693 		if (err)
10694 			goto error;
10695 	}
10696 
10697 	/* The 'any' trigger means the device continues operating more or less
10698 	 * as in its normal operation mode and wakes up the host on most of the
10699 	 * normal interrupts (like packet RX, ...)
10700 	 * It therefore makes little sense to combine with the more constrained
10701 	 * wakeup trigger modes.
10702 	 */
10703 	if (new_triggers.any && regular) {
10704 		err = -EINVAL;
10705 		goto error;
10706 	}
10707 
10708 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
10709 	if (!ntrig) {
10710 		err = -ENOMEM;
10711 		goto error;
10712 	}
10713 	cfg80211_rdev_free_wowlan(rdev);
10714 	rdev->wiphy.wowlan_config = ntrig;
10715 
10716  set_wakeup:
10717 	if (rdev->ops->set_wakeup &&
10718 	    prev_enabled != !!rdev->wiphy.wowlan_config)
10719 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
10720 
10721 	return 0;
10722  error:
10723 	for (i = 0; i < new_triggers.n_patterns; i++)
10724 		kfree(new_triggers.patterns[i].mask);
10725 	kfree(new_triggers.patterns);
10726 	if (new_triggers.tcp && new_triggers.tcp->sock)
10727 		sock_release(new_triggers.tcp->sock);
10728 	kfree(new_triggers.tcp);
10729 	kfree(new_triggers.nd_config);
10730 	return err;
10731 }
10732 #endif
10733 
10734 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
10735 				       struct cfg80211_registered_device *rdev)
10736 {
10737 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
10738 	int i, j, pat_len;
10739 	struct cfg80211_coalesce_rules *rule;
10740 
10741 	if (!rdev->coalesce->n_rules)
10742 		return 0;
10743 
10744 	nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
10745 	if (!nl_rules)
10746 		return -ENOBUFS;
10747 
10748 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
10749 		nl_rule = nla_nest_start(msg, i + 1);
10750 		if (!nl_rule)
10751 			return -ENOBUFS;
10752 
10753 		rule = &rdev->coalesce->rules[i];
10754 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
10755 				rule->delay))
10756 			return -ENOBUFS;
10757 
10758 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
10759 				rule->condition))
10760 			return -ENOBUFS;
10761 
10762 		nl_pats = nla_nest_start(msg,
10763 				NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
10764 		if (!nl_pats)
10765 			return -ENOBUFS;
10766 
10767 		for (j = 0; j < rule->n_patterns; j++) {
10768 			nl_pat = nla_nest_start(msg, j + 1);
10769 			if (!nl_pat)
10770 				return -ENOBUFS;
10771 			pat_len = rule->patterns[j].pattern_len;
10772 			if (nla_put(msg, NL80211_PKTPAT_MASK,
10773 				    DIV_ROUND_UP(pat_len, 8),
10774 				    rule->patterns[j].mask) ||
10775 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10776 				    rule->patterns[j].pattern) ||
10777 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10778 					rule->patterns[j].pkt_offset))
10779 				return -ENOBUFS;
10780 			nla_nest_end(msg, nl_pat);
10781 		}
10782 		nla_nest_end(msg, nl_pats);
10783 		nla_nest_end(msg, nl_rule);
10784 	}
10785 	nla_nest_end(msg, nl_rules);
10786 
10787 	return 0;
10788 }
10789 
10790 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
10791 {
10792 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10793 	struct sk_buff *msg;
10794 	void *hdr;
10795 
10796 	if (!rdev->wiphy.coalesce)
10797 		return -EOPNOTSUPP;
10798 
10799 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10800 	if (!msg)
10801 		return -ENOMEM;
10802 
10803 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10804 			     NL80211_CMD_GET_COALESCE);
10805 	if (!hdr)
10806 		goto nla_put_failure;
10807 
10808 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
10809 		goto nla_put_failure;
10810 
10811 	genlmsg_end(msg, hdr);
10812 	return genlmsg_reply(msg, info);
10813 
10814 nla_put_failure:
10815 	nlmsg_free(msg);
10816 	return -ENOBUFS;
10817 }
10818 
10819 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
10820 {
10821 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
10822 	int i, j;
10823 	struct cfg80211_coalesce_rules *rule;
10824 
10825 	if (!coalesce)
10826 		return;
10827 
10828 	for (i = 0; i < coalesce->n_rules; i++) {
10829 		rule = &coalesce->rules[i];
10830 		for (j = 0; j < rule->n_patterns; j++)
10831 			kfree(rule->patterns[j].mask);
10832 		kfree(rule->patterns);
10833 	}
10834 	kfree(coalesce->rules);
10835 	kfree(coalesce);
10836 	rdev->coalesce = NULL;
10837 }
10838 
10839 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
10840 				       struct nlattr *rule,
10841 				       struct cfg80211_coalesce_rules *new_rule)
10842 {
10843 	int err, i;
10844 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10845 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
10846 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
10847 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10848 
10849 	err = nla_parse_nested(tb, NL80211_ATTR_COALESCE_RULE_MAX, rule,
10850 			       nl80211_coalesce_policy, NULL);
10851 	if (err)
10852 		return err;
10853 
10854 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
10855 		new_rule->delay =
10856 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
10857 	if (new_rule->delay > coalesce->max_delay)
10858 		return -EINVAL;
10859 
10860 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
10861 		new_rule->condition =
10862 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
10863 	if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
10864 	    new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
10865 		return -EINVAL;
10866 
10867 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
10868 		return -EINVAL;
10869 
10870 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10871 			    rem)
10872 		n_patterns++;
10873 	if (n_patterns > coalesce->n_patterns)
10874 		return -EINVAL;
10875 
10876 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
10877 				     GFP_KERNEL);
10878 	if (!new_rule->patterns)
10879 		return -ENOMEM;
10880 
10881 	new_rule->n_patterns = n_patterns;
10882 	i = 0;
10883 
10884 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10885 			    rem) {
10886 		u8 *mask_pat;
10887 
10888 		nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10889 				 nl80211_packet_pattern_policy, NULL);
10890 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
10891 		    !pat_tb[NL80211_PKTPAT_PATTERN])
10892 			return -EINVAL;
10893 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10894 		mask_len = DIV_ROUND_UP(pat_len, 8);
10895 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10896 			return -EINVAL;
10897 		if (pat_len > coalesce->pattern_max_len ||
10898 		    pat_len < coalesce->pattern_min_len)
10899 			return -EINVAL;
10900 
10901 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
10902 			pkt_offset = 0;
10903 		else
10904 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
10905 		if (pkt_offset > coalesce->max_pkt_offset)
10906 			return -EINVAL;
10907 		new_rule->patterns[i].pkt_offset = pkt_offset;
10908 
10909 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10910 		if (!mask_pat)
10911 			return -ENOMEM;
10912 
10913 		new_rule->patterns[i].mask = mask_pat;
10914 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10915 		       mask_len);
10916 
10917 		mask_pat += mask_len;
10918 		new_rule->patterns[i].pattern = mask_pat;
10919 		new_rule->patterns[i].pattern_len = pat_len;
10920 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10921 		       pat_len);
10922 		i++;
10923 	}
10924 
10925 	return 0;
10926 }
10927 
10928 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
10929 {
10930 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10931 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10932 	struct cfg80211_coalesce new_coalesce = {};
10933 	struct cfg80211_coalesce *n_coalesce;
10934 	int err, rem_rule, n_rules = 0, i, j;
10935 	struct nlattr *rule;
10936 	struct cfg80211_coalesce_rules *tmp_rule;
10937 
10938 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
10939 		return -EOPNOTSUPP;
10940 
10941 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
10942 		cfg80211_rdev_free_coalesce(rdev);
10943 		rdev_set_coalesce(rdev, NULL);
10944 		return 0;
10945 	}
10946 
10947 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10948 			    rem_rule)
10949 		n_rules++;
10950 	if (n_rules > coalesce->n_rules)
10951 		return -EINVAL;
10952 
10953 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
10954 				     GFP_KERNEL);
10955 	if (!new_coalesce.rules)
10956 		return -ENOMEM;
10957 
10958 	new_coalesce.n_rules = n_rules;
10959 	i = 0;
10960 
10961 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10962 			    rem_rule) {
10963 		err = nl80211_parse_coalesce_rule(rdev, rule,
10964 						  &new_coalesce.rules[i]);
10965 		if (err)
10966 			goto error;
10967 
10968 		i++;
10969 	}
10970 
10971 	err = rdev_set_coalesce(rdev, &new_coalesce);
10972 	if (err)
10973 		goto error;
10974 
10975 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
10976 	if (!n_coalesce) {
10977 		err = -ENOMEM;
10978 		goto error;
10979 	}
10980 	cfg80211_rdev_free_coalesce(rdev);
10981 	rdev->coalesce = n_coalesce;
10982 
10983 	return 0;
10984 error:
10985 	for (i = 0; i < new_coalesce.n_rules; i++) {
10986 		tmp_rule = &new_coalesce.rules[i];
10987 		for (j = 0; j < tmp_rule->n_patterns; j++)
10988 			kfree(tmp_rule->patterns[j].mask);
10989 		kfree(tmp_rule->patterns);
10990 	}
10991 	kfree(new_coalesce.rules);
10992 
10993 	return err;
10994 }
10995 
10996 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
10997 {
10998 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10999 	struct net_device *dev = info->user_ptr[1];
11000 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11001 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
11002 	struct cfg80211_gtk_rekey_data rekey_data;
11003 	int err;
11004 
11005 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
11006 		return -EINVAL;
11007 
11008 	err = nla_parse_nested(tb, MAX_NL80211_REKEY_DATA,
11009 			       info->attrs[NL80211_ATTR_REKEY_DATA],
11010 			       nl80211_rekey_policy, info->extack);
11011 	if (err)
11012 		return err;
11013 
11014 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
11015 	    !tb[NL80211_REKEY_DATA_KCK])
11016 		return -EINVAL;
11017 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
11018 		return -ERANGE;
11019 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
11020 		return -ERANGE;
11021 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
11022 		return -ERANGE;
11023 
11024 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
11025 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
11026 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
11027 
11028 	wdev_lock(wdev);
11029 	if (!wdev->current_bss) {
11030 		err = -ENOTCONN;
11031 		goto out;
11032 	}
11033 
11034 	if (!rdev->ops->set_rekey_data) {
11035 		err = -EOPNOTSUPP;
11036 		goto out;
11037 	}
11038 
11039 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
11040  out:
11041 	wdev_unlock(wdev);
11042 	return err;
11043 }
11044 
11045 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
11046 					     struct genl_info *info)
11047 {
11048 	struct net_device *dev = info->user_ptr[1];
11049 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11050 
11051 	if (wdev->iftype != NL80211_IFTYPE_AP &&
11052 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
11053 		return -EINVAL;
11054 
11055 	if (wdev->ap_unexpected_nlportid)
11056 		return -EBUSY;
11057 
11058 	wdev->ap_unexpected_nlportid = info->snd_portid;
11059 	return 0;
11060 }
11061 
11062 static int nl80211_probe_client(struct sk_buff *skb,
11063 				struct genl_info *info)
11064 {
11065 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11066 	struct net_device *dev = info->user_ptr[1];
11067 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11068 	struct sk_buff *msg;
11069 	void *hdr;
11070 	const u8 *addr;
11071 	u64 cookie;
11072 	int err;
11073 
11074 	if (wdev->iftype != NL80211_IFTYPE_AP &&
11075 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
11076 		return -EOPNOTSUPP;
11077 
11078 	if (!info->attrs[NL80211_ATTR_MAC])
11079 		return -EINVAL;
11080 
11081 	if (!rdev->ops->probe_client)
11082 		return -EOPNOTSUPP;
11083 
11084 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11085 	if (!msg)
11086 		return -ENOMEM;
11087 
11088 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11089 			     NL80211_CMD_PROBE_CLIENT);
11090 	if (!hdr) {
11091 		err = -ENOBUFS;
11092 		goto free_msg;
11093 	}
11094 
11095 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
11096 
11097 	err = rdev_probe_client(rdev, dev, addr, &cookie);
11098 	if (err)
11099 		goto free_msg;
11100 
11101 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11102 			      NL80211_ATTR_PAD))
11103 		goto nla_put_failure;
11104 
11105 	genlmsg_end(msg, hdr);
11106 
11107 	return genlmsg_reply(msg, info);
11108 
11109  nla_put_failure:
11110 	err = -ENOBUFS;
11111  free_msg:
11112 	nlmsg_free(msg);
11113 	return err;
11114 }
11115 
11116 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
11117 {
11118 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11119 	struct cfg80211_beacon_registration *reg, *nreg;
11120 	int rv;
11121 
11122 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
11123 		return -EOPNOTSUPP;
11124 
11125 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
11126 	if (!nreg)
11127 		return -ENOMEM;
11128 
11129 	/* First, check if already registered. */
11130 	spin_lock_bh(&rdev->beacon_registrations_lock);
11131 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11132 		if (reg->nlportid == info->snd_portid) {
11133 			rv = -EALREADY;
11134 			goto out_err;
11135 		}
11136 	}
11137 	/* Add it to the list */
11138 	nreg->nlportid = info->snd_portid;
11139 	list_add(&nreg->list, &rdev->beacon_registrations);
11140 
11141 	spin_unlock_bh(&rdev->beacon_registrations_lock);
11142 
11143 	return 0;
11144 out_err:
11145 	spin_unlock_bh(&rdev->beacon_registrations_lock);
11146 	kfree(nreg);
11147 	return rv;
11148 }
11149 
11150 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
11151 {
11152 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11153 	struct wireless_dev *wdev = info->user_ptr[1];
11154 	int err;
11155 
11156 	if (!rdev->ops->start_p2p_device)
11157 		return -EOPNOTSUPP;
11158 
11159 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11160 		return -EOPNOTSUPP;
11161 
11162 	if (wdev_running(wdev))
11163 		return 0;
11164 
11165 	if (rfkill_blocked(rdev->rfkill))
11166 		return -ERFKILL;
11167 
11168 	err = rdev_start_p2p_device(rdev, wdev);
11169 	if (err)
11170 		return err;
11171 
11172 	wdev->is_running = true;
11173 	rdev->opencount++;
11174 
11175 	return 0;
11176 }
11177 
11178 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
11179 {
11180 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11181 	struct wireless_dev *wdev = info->user_ptr[1];
11182 
11183 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11184 		return -EOPNOTSUPP;
11185 
11186 	if (!rdev->ops->stop_p2p_device)
11187 		return -EOPNOTSUPP;
11188 
11189 	cfg80211_stop_p2p_device(rdev, wdev);
11190 
11191 	return 0;
11192 }
11193 
11194 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
11195 {
11196 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11197 	struct wireless_dev *wdev = info->user_ptr[1];
11198 	struct cfg80211_nan_conf conf = {};
11199 	int err;
11200 
11201 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11202 		return -EOPNOTSUPP;
11203 
11204 	if (wdev_running(wdev))
11205 		return -EEXIST;
11206 
11207 	if (rfkill_blocked(rdev->rfkill))
11208 		return -ERFKILL;
11209 
11210 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
11211 		return -EINVAL;
11212 
11213 	conf.master_pref =
11214 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11215 	if (!conf.master_pref)
11216 		return -EINVAL;
11217 
11218 	if (info->attrs[NL80211_ATTR_BANDS]) {
11219 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11220 
11221 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11222 			return -EOPNOTSUPP;
11223 
11224 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11225 			return -EINVAL;
11226 
11227 		conf.bands = bands;
11228 	}
11229 
11230 	err = rdev_start_nan(rdev, wdev, &conf);
11231 	if (err)
11232 		return err;
11233 
11234 	wdev->is_running = true;
11235 	rdev->opencount++;
11236 
11237 	return 0;
11238 }
11239 
11240 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
11241 {
11242 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11243 	struct wireless_dev *wdev = info->user_ptr[1];
11244 
11245 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11246 		return -EOPNOTSUPP;
11247 
11248 	cfg80211_stop_nan(rdev, wdev);
11249 
11250 	return 0;
11251 }
11252 
11253 static int validate_nan_filter(struct nlattr *filter_attr)
11254 {
11255 	struct nlattr *attr;
11256 	int len = 0, n_entries = 0, rem;
11257 
11258 	nla_for_each_nested(attr, filter_attr, rem) {
11259 		len += nla_len(attr);
11260 		n_entries++;
11261 	}
11262 
11263 	if (len >= U8_MAX)
11264 		return -EINVAL;
11265 
11266 	return n_entries;
11267 }
11268 
11269 static int handle_nan_filter(struct nlattr *attr_filter,
11270 			     struct cfg80211_nan_func *func,
11271 			     bool tx)
11272 {
11273 	struct nlattr *attr;
11274 	int n_entries, rem, i;
11275 	struct cfg80211_nan_func_filter *filter;
11276 
11277 	n_entries = validate_nan_filter(attr_filter);
11278 	if (n_entries < 0)
11279 		return n_entries;
11280 
11281 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
11282 
11283 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
11284 	if (!filter)
11285 		return -ENOMEM;
11286 
11287 	i = 0;
11288 	nla_for_each_nested(attr, attr_filter, rem) {
11289 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
11290 		filter[i].len = nla_len(attr);
11291 		i++;
11292 	}
11293 	if (tx) {
11294 		func->num_tx_filters = n_entries;
11295 		func->tx_filters = filter;
11296 	} else {
11297 		func->num_rx_filters = n_entries;
11298 		func->rx_filters = filter;
11299 	}
11300 
11301 	return 0;
11302 }
11303 
11304 static int nl80211_nan_add_func(struct sk_buff *skb,
11305 				struct genl_info *info)
11306 {
11307 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11308 	struct wireless_dev *wdev = info->user_ptr[1];
11309 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
11310 	struct cfg80211_nan_func *func;
11311 	struct sk_buff *msg = NULL;
11312 	void *hdr = NULL;
11313 	int err = 0;
11314 
11315 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11316 		return -EOPNOTSUPP;
11317 
11318 	if (!wdev_running(wdev))
11319 		return -ENOTCONN;
11320 
11321 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
11322 		return -EINVAL;
11323 
11324 	err = nla_parse_nested(tb, NL80211_NAN_FUNC_ATTR_MAX,
11325 			       info->attrs[NL80211_ATTR_NAN_FUNC],
11326 			       nl80211_nan_func_policy, info->extack);
11327 	if (err)
11328 		return err;
11329 
11330 	func = kzalloc(sizeof(*func), GFP_KERNEL);
11331 	if (!func)
11332 		return -ENOMEM;
11333 
11334 	func->cookie = wdev->wiphy->cookie_counter++;
11335 
11336 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
11337 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
11338 		err = -EINVAL;
11339 		goto out;
11340 	}
11341 
11342 
11343 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
11344 
11345 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
11346 		err = -EINVAL;
11347 		goto out;
11348 	}
11349 
11350 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
11351 	       sizeof(func->service_id));
11352 
11353 	func->close_range =
11354 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
11355 
11356 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
11357 		func->serv_spec_info_len =
11358 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
11359 		func->serv_spec_info =
11360 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
11361 				func->serv_spec_info_len,
11362 				GFP_KERNEL);
11363 		if (!func->serv_spec_info) {
11364 			err = -ENOMEM;
11365 			goto out;
11366 		}
11367 	}
11368 
11369 	if (tb[NL80211_NAN_FUNC_TTL])
11370 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
11371 
11372 	switch (func->type) {
11373 	case NL80211_NAN_FUNC_PUBLISH:
11374 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
11375 			err = -EINVAL;
11376 			goto out;
11377 		}
11378 
11379 		func->publish_type =
11380 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
11381 		func->publish_bcast =
11382 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
11383 
11384 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
11385 			func->publish_bcast) {
11386 			err = -EINVAL;
11387 			goto out;
11388 		}
11389 		break;
11390 	case NL80211_NAN_FUNC_SUBSCRIBE:
11391 		func->subscribe_active =
11392 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
11393 		break;
11394 	case NL80211_NAN_FUNC_FOLLOW_UP:
11395 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
11396 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
11397 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
11398 			err = -EINVAL;
11399 			goto out;
11400 		}
11401 
11402 		func->followup_id =
11403 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
11404 		func->followup_reqid =
11405 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
11406 		memcpy(func->followup_dest.addr,
11407 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
11408 		       sizeof(func->followup_dest.addr));
11409 		if (func->ttl) {
11410 			err = -EINVAL;
11411 			goto out;
11412 		}
11413 		break;
11414 	default:
11415 		err = -EINVAL;
11416 		goto out;
11417 	}
11418 
11419 	if (tb[NL80211_NAN_FUNC_SRF]) {
11420 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
11421 
11422 		err = nla_parse_nested(srf_tb, NL80211_NAN_SRF_ATTR_MAX,
11423 				       tb[NL80211_NAN_FUNC_SRF],
11424 				       nl80211_nan_srf_policy, info->extack);
11425 		if (err)
11426 			goto out;
11427 
11428 		func->srf_include =
11429 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
11430 
11431 		if (srf_tb[NL80211_NAN_SRF_BF]) {
11432 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
11433 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
11434 				err = -EINVAL;
11435 				goto out;
11436 			}
11437 
11438 			func->srf_bf_len =
11439 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
11440 			func->srf_bf =
11441 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
11442 					func->srf_bf_len, GFP_KERNEL);
11443 			if (!func->srf_bf) {
11444 				err = -ENOMEM;
11445 				goto out;
11446 			}
11447 
11448 			func->srf_bf_idx =
11449 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
11450 		} else {
11451 			struct nlattr *attr, *mac_attr =
11452 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
11453 			int n_entries, rem, i = 0;
11454 
11455 			if (!mac_attr) {
11456 				err = -EINVAL;
11457 				goto out;
11458 			}
11459 
11460 			n_entries = validate_acl_mac_addrs(mac_attr);
11461 			if (n_entries <= 0) {
11462 				err = -EINVAL;
11463 				goto out;
11464 			}
11465 
11466 			func->srf_num_macs = n_entries;
11467 			func->srf_macs =
11468 				kzalloc(sizeof(*func->srf_macs) * n_entries,
11469 					GFP_KERNEL);
11470 			if (!func->srf_macs) {
11471 				err = -ENOMEM;
11472 				goto out;
11473 			}
11474 
11475 			nla_for_each_nested(attr, mac_attr, rem)
11476 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
11477 				       sizeof(*func->srf_macs));
11478 		}
11479 	}
11480 
11481 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
11482 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
11483 					func, true);
11484 		if (err)
11485 			goto out;
11486 	}
11487 
11488 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
11489 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
11490 					func, false);
11491 		if (err)
11492 			goto out;
11493 	}
11494 
11495 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11496 	if (!msg) {
11497 		err = -ENOMEM;
11498 		goto out;
11499 	}
11500 
11501 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11502 			     NL80211_CMD_ADD_NAN_FUNCTION);
11503 	/* This can't really happen - we just allocated 4KB */
11504 	if (WARN_ON(!hdr)) {
11505 		err = -ENOMEM;
11506 		goto out;
11507 	}
11508 
11509 	err = rdev_add_nan_func(rdev, wdev, func);
11510 out:
11511 	if (err < 0) {
11512 		cfg80211_free_nan_func(func);
11513 		nlmsg_free(msg);
11514 		return err;
11515 	}
11516 
11517 	/* propagate the instance id and cookie to userspace  */
11518 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
11519 			      NL80211_ATTR_PAD))
11520 		goto nla_put_failure;
11521 
11522 	func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11523 	if (!func_attr)
11524 		goto nla_put_failure;
11525 
11526 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
11527 		       func->instance_id))
11528 		goto nla_put_failure;
11529 
11530 	nla_nest_end(msg, func_attr);
11531 
11532 	genlmsg_end(msg, hdr);
11533 	return genlmsg_reply(msg, info);
11534 
11535 nla_put_failure:
11536 	nlmsg_free(msg);
11537 	return -ENOBUFS;
11538 }
11539 
11540 static int nl80211_nan_del_func(struct sk_buff *skb,
11541 			       struct genl_info *info)
11542 {
11543 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11544 	struct wireless_dev *wdev = info->user_ptr[1];
11545 	u64 cookie;
11546 
11547 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11548 		return -EOPNOTSUPP;
11549 
11550 	if (!wdev_running(wdev))
11551 		return -ENOTCONN;
11552 
11553 	if (!info->attrs[NL80211_ATTR_COOKIE])
11554 		return -EINVAL;
11555 
11556 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11557 
11558 	rdev_del_nan_func(rdev, wdev, cookie);
11559 
11560 	return 0;
11561 }
11562 
11563 static int nl80211_nan_change_config(struct sk_buff *skb,
11564 				     struct genl_info *info)
11565 {
11566 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11567 	struct wireless_dev *wdev = info->user_ptr[1];
11568 	struct cfg80211_nan_conf conf = {};
11569 	u32 changed = 0;
11570 
11571 	if (wdev->iftype != NL80211_IFTYPE_NAN)
11572 		return -EOPNOTSUPP;
11573 
11574 	if (!wdev_running(wdev))
11575 		return -ENOTCONN;
11576 
11577 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
11578 		conf.master_pref =
11579 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11580 		if (conf.master_pref <= 1 || conf.master_pref == 255)
11581 			return -EINVAL;
11582 
11583 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
11584 	}
11585 
11586 	if (info->attrs[NL80211_ATTR_BANDS]) {
11587 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11588 
11589 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11590 			return -EOPNOTSUPP;
11591 
11592 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11593 			return -EINVAL;
11594 
11595 		conf.bands = bands;
11596 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
11597 	}
11598 
11599 	if (!changed)
11600 		return -EINVAL;
11601 
11602 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
11603 }
11604 
11605 void cfg80211_nan_match(struct wireless_dev *wdev,
11606 			struct cfg80211_nan_match_params *match, gfp_t gfp)
11607 {
11608 	struct wiphy *wiphy = wdev->wiphy;
11609 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11610 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
11611 	struct sk_buff *msg;
11612 	void *hdr;
11613 
11614 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
11615 		return;
11616 
11617 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11618 	if (!msg)
11619 		return;
11620 
11621 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
11622 	if (!hdr) {
11623 		nlmsg_free(msg);
11624 		return;
11625 	}
11626 
11627 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11628 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11629 					 wdev->netdev->ifindex)) ||
11630 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11631 			      NL80211_ATTR_PAD))
11632 		goto nla_put_failure;
11633 
11634 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
11635 			      NL80211_ATTR_PAD) ||
11636 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
11637 		goto nla_put_failure;
11638 
11639 	match_attr = nla_nest_start(msg, NL80211_ATTR_NAN_MATCH);
11640 	if (!match_attr)
11641 		goto nla_put_failure;
11642 
11643 	local_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_LOCAL);
11644 	if (!local_func_attr)
11645 		goto nla_put_failure;
11646 
11647 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
11648 		goto nla_put_failure;
11649 
11650 	nla_nest_end(msg, local_func_attr);
11651 
11652 	peer_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_PEER);
11653 	if (!peer_func_attr)
11654 		goto nla_put_failure;
11655 
11656 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
11657 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
11658 		goto nla_put_failure;
11659 
11660 	if (match->info && match->info_len &&
11661 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
11662 		    match->info))
11663 		goto nla_put_failure;
11664 
11665 	nla_nest_end(msg, peer_func_attr);
11666 	nla_nest_end(msg, match_attr);
11667 	genlmsg_end(msg, hdr);
11668 
11669 	if (!wdev->owner_nlportid)
11670 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11671 					msg, 0, NL80211_MCGRP_NAN, gfp);
11672 	else
11673 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11674 				wdev->owner_nlportid);
11675 
11676 	return;
11677 
11678 nla_put_failure:
11679 	nlmsg_free(msg);
11680 }
11681 EXPORT_SYMBOL(cfg80211_nan_match);
11682 
11683 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
11684 				  u8 inst_id,
11685 				  enum nl80211_nan_func_term_reason reason,
11686 				  u64 cookie, gfp_t gfp)
11687 {
11688 	struct wiphy *wiphy = wdev->wiphy;
11689 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11690 	struct sk_buff *msg;
11691 	struct nlattr *func_attr;
11692 	void *hdr;
11693 
11694 	if (WARN_ON(!inst_id))
11695 		return;
11696 
11697 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11698 	if (!msg)
11699 		return;
11700 
11701 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
11702 	if (!hdr) {
11703 		nlmsg_free(msg);
11704 		return;
11705 	}
11706 
11707 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11708 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11709 					 wdev->netdev->ifindex)) ||
11710 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11711 			      NL80211_ATTR_PAD))
11712 		goto nla_put_failure;
11713 
11714 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11715 			      NL80211_ATTR_PAD))
11716 		goto nla_put_failure;
11717 
11718 	func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11719 	if (!func_attr)
11720 		goto nla_put_failure;
11721 
11722 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
11723 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
11724 		goto nla_put_failure;
11725 
11726 	nla_nest_end(msg, func_attr);
11727 	genlmsg_end(msg, hdr);
11728 
11729 	if (!wdev->owner_nlportid)
11730 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11731 					msg, 0, NL80211_MCGRP_NAN, gfp);
11732 	else
11733 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11734 				wdev->owner_nlportid);
11735 
11736 	return;
11737 
11738 nla_put_failure:
11739 	nlmsg_free(msg);
11740 }
11741 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
11742 
11743 static int nl80211_get_protocol_features(struct sk_buff *skb,
11744 					 struct genl_info *info)
11745 {
11746 	void *hdr;
11747 	struct sk_buff *msg;
11748 
11749 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11750 	if (!msg)
11751 		return -ENOMEM;
11752 
11753 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11754 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
11755 	if (!hdr)
11756 		goto nla_put_failure;
11757 
11758 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
11759 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
11760 		goto nla_put_failure;
11761 
11762 	genlmsg_end(msg, hdr);
11763 	return genlmsg_reply(msg, info);
11764 
11765  nla_put_failure:
11766 	kfree_skb(msg);
11767 	return -ENOBUFS;
11768 }
11769 
11770 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
11771 {
11772 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11773 	struct cfg80211_update_ft_ies_params ft_params;
11774 	struct net_device *dev = info->user_ptr[1];
11775 
11776 	if (!rdev->ops->update_ft_ies)
11777 		return -EOPNOTSUPP;
11778 
11779 	if (!info->attrs[NL80211_ATTR_MDID] ||
11780 	    !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
11781 		return -EINVAL;
11782 
11783 	memset(&ft_params, 0, sizeof(ft_params));
11784 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
11785 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11786 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11787 
11788 	return rdev_update_ft_ies(rdev, dev, &ft_params);
11789 }
11790 
11791 static int nl80211_crit_protocol_start(struct sk_buff *skb,
11792 				       struct genl_info *info)
11793 {
11794 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11795 	struct wireless_dev *wdev = info->user_ptr[1];
11796 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
11797 	u16 duration;
11798 	int ret;
11799 
11800 	if (!rdev->ops->crit_proto_start)
11801 		return -EOPNOTSUPP;
11802 
11803 	if (WARN_ON(!rdev->ops->crit_proto_stop))
11804 		return -EINVAL;
11805 
11806 	if (rdev->crit_proto_nlportid)
11807 		return -EBUSY;
11808 
11809 	/* determine protocol if provided */
11810 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
11811 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
11812 
11813 	if (proto >= NUM_NL80211_CRIT_PROTO)
11814 		return -EINVAL;
11815 
11816 	/* timeout must be provided */
11817 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
11818 		return -EINVAL;
11819 
11820 	duration =
11821 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
11822 
11823 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
11824 		return -ERANGE;
11825 
11826 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
11827 	if (!ret)
11828 		rdev->crit_proto_nlportid = info->snd_portid;
11829 
11830 	return ret;
11831 }
11832 
11833 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
11834 				      struct genl_info *info)
11835 {
11836 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11837 	struct wireless_dev *wdev = info->user_ptr[1];
11838 
11839 	if (!rdev->ops->crit_proto_stop)
11840 		return -EOPNOTSUPP;
11841 
11842 	if (rdev->crit_proto_nlportid) {
11843 		rdev->crit_proto_nlportid = 0;
11844 		rdev_crit_proto_stop(rdev, wdev);
11845 	}
11846 	return 0;
11847 }
11848 
11849 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
11850 {
11851 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11852 	struct wireless_dev *wdev =
11853 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
11854 	int i, err;
11855 	u32 vid, subcmd;
11856 
11857 	if (!rdev->wiphy.vendor_commands)
11858 		return -EOPNOTSUPP;
11859 
11860 	if (IS_ERR(wdev)) {
11861 		err = PTR_ERR(wdev);
11862 		if (err != -EINVAL)
11863 			return err;
11864 		wdev = NULL;
11865 	} else if (wdev->wiphy != &rdev->wiphy) {
11866 		return -EINVAL;
11867 	}
11868 
11869 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
11870 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
11871 		return -EINVAL;
11872 
11873 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
11874 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
11875 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
11876 		const struct wiphy_vendor_command *vcmd;
11877 		void *data = NULL;
11878 		int len = 0;
11879 
11880 		vcmd = &rdev->wiphy.vendor_commands[i];
11881 
11882 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11883 			continue;
11884 
11885 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
11886 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
11887 			if (!wdev)
11888 				return -EINVAL;
11889 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
11890 			    !wdev->netdev)
11891 				return -EINVAL;
11892 
11893 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
11894 				if (!wdev_running(wdev))
11895 					return -ENETDOWN;
11896 			}
11897 
11898 			if (!vcmd->doit)
11899 				return -EOPNOTSUPP;
11900 		} else {
11901 			wdev = NULL;
11902 		}
11903 
11904 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
11905 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11906 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11907 		}
11908 
11909 		rdev->cur_cmd_info = info;
11910 		err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
11911 							  data, len);
11912 		rdev->cur_cmd_info = NULL;
11913 		return err;
11914 	}
11915 
11916 	return -EOPNOTSUPP;
11917 }
11918 
11919 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
11920 				       struct netlink_callback *cb,
11921 				       struct cfg80211_registered_device **rdev,
11922 				       struct wireless_dev **wdev)
11923 {
11924 	struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
11925 	u32 vid, subcmd;
11926 	unsigned int i;
11927 	int vcmd_idx = -1;
11928 	int err;
11929 	void *data = NULL;
11930 	unsigned int data_len = 0;
11931 
11932 	if (cb->args[0]) {
11933 		/* subtract the 1 again here */
11934 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
11935 		struct wireless_dev *tmp;
11936 
11937 		if (!wiphy)
11938 			return -ENODEV;
11939 		*rdev = wiphy_to_rdev(wiphy);
11940 		*wdev = NULL;
11941 
11942 		if (cb->args[1]) {
11943 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
11944 				if (tmp->identifier == cb->args[1] - 1) {
11945 					*wdev = tmp;
11946 					break;
11947 				}
11948 			}
11949 		}
11950 
11951 		/* keep rtnl locked in successful case */
11952 		return 0;
11953 	}
11954 
11955 	err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, attrbuf,
11956 			  nl80211_fam.maxattr, nl80211_policy, NULL);
11957 	if (err)
11958 		return err;
11959 
11960 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
11961 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
11962 		return -EINVAL;
11963 
11964 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
11965 	if (IS_ERR(*wdev))
11966 		*wdev = NULL;
11967 
11968 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11969 	if (IS_ERR(*rdev))
11970 		return PTR_ERR(*rdev);
11971 
11972 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
11973 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
11974 
11975 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
11976 		const struct wiphy_vendor_command *vcmd;
11977 
11978 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
11979 
11980 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11981 			continue;
11982 
11983 		if (!vcmd->dumpit)
11984 			return -EOPNOTSUPP;
11985 
11986 		vcmd_idx = i;
11987 		break;
11988 	}
11989 
11990 	if (vcmd_idx < 0)
11991 		return -EOPNOTSUPP;
11992 
11993 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
11994 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
11995 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
11996 	}
11997 
11998 	/* 0 is the first index - add 1 to parse only once */
11999 	cb->args[0] = (*rdev)->wiphy_idx + 1;
12000 	/* add 1 to know if it was NULL */
12001 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
12002 	cb->args[2] = vcmd_idx;
12003 	cb->args[3] = (unsigned long)data;
12004 	cb->args[4] = data_len;
12005 
12006 	/* keep rtnl locked in successful case */
12007 	return 0;
12008 }
12009 
12010 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
12011 				   struct netlink_callback *cb)
12012 {
12013 	struct cfg80211_registered_device *rdev;
12014 	struct wireless_dev *wdev;
12015 	unsigned int vcmd_idx;
12016 	const struct wiphy_vendor_command *vcmd;
12017 	void *data;
12018 	int data_len;
12019 	int err;
12020 	struct nlattr *vendor_data;
12021 
12022 	rtnl_lock();
12023 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
12024 	if (err)
12025 		goto out;
12026 
12027 	vcmd_idx = cb->args[2];
12028 	data = (void *)cb->args[3];
12029 	data_len = cb->args[4];
12030 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
12031 
12032 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12033 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12034 		if (!wdev) {
12035 			err = -EINVAL;
12036 			goto out;
12037 		}
12038 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12039 		    !wdev->netdev) {
12040 			err = -EINVAL;
12041 			goto out;
12042 		}
12043 
12044 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12045 			if (!wdev_running(wdev)) {
12046 				err = -ENETDOWN;
12047 				goto out;
12048 			}
12049 		}
12050 	}
12051 
12052 	while (1) {
12053 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
12054 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
12055 					   NL80211_CMD_VENDOR);
12056 		if (!hdr)
12057 			break;
12058 
12059 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12060 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
12061 					       wdev_id(wdev),
12062 					       NL80211_ATTR_PAD))) {
12063 			genlmsg_cancel(skb, hdr);
12064 			break;
12065 		}
12066 
12067 		vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
12068 		if (!vendor_data) {
12069 			genlmsg_cancel(skb, hdr);
12070 			break;
12071 		}
12072 
12073 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
12074 				   (unsigned long *)&cb->args[5]);
12075 		nla_nest_end(skb, vendor_data);
12076 
12077 		if (err == -ENOBUFS || err == -ENOENT) {
12078 			genlmsg_cancel(skb, hdr);
12079 			break;
12080 		} else if (err) {
12081 			genlmsg_cancel(skb, hdr);
12082 			goto out;
12083 		}
12084 
12085 		genlmsg_end(skb, hdr);
12086 	}
12087 
12088 	err = skb->len;
12089  out:
12090 	rtnl_unlock();
12091 	return err;
12092 }
12093 
12094 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
12095 					   enum nl80211_commands cmd,
12096 					   enum nl80211_attrs attr,
12097 					   int approxlen)
12098 {
12099 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12100 
12101 	if (WARN_ON(!rdev->cur_cmd_info))
12102 		return NULL;
12103 
12104 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
12105 					   rdev->cur_cmd_info->snd_portid,
12106 					   rdev->cur_cmd_info->snd_seq,
12107 					   cmd, attr, NULL, GFP_KERNEL);
12108 }
12109 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
12110 
12111 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
12112 {
12113 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
12114 	void *hdr = ((void **)skb->cb)[1];
12115 	struct nlattr *data = ((void **)skb->cb)[2];
12116 
12117 	/* clear CB data for netlink core to own from now on */
12118 	memset(skb->cb, 0, sizeof(skb->cb));
12119 
12120 	if (WARN_ON(!rdev->cur_cmd_info)) {
12121 		kfree_skb(skb);
12122 		return -EINVAL;
12123 	}
12124 
12125 	nla_nest_end(skb, data);
12126 	genlmsg_end(skb, hdr);
12127 	return genlmsg_reply(skb, rdev->cur_cmd_info);
12128 }
12129 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
12130 
12131 static int nl80211_set_qos_map(struct sk_buff *skb,
12132 			       struct genl_info *info)
12133 {
12134 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12135 	struct cfg80211_qos_map *qos_map = NULL;
12136 	struct net_device *dev = info->user_ptr[1];
12137 	u8 *pos, len, num_des, des_len, des;
12138 	int ret;
12139 
12140 	if (!rdev->ops->set_qos_map)
12141 		return -EOPNOTSUPP;
12142 
12143 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
12144 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
12145 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
12146 
12147 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
12148 		    len > IEEE80211_QOS_MAP_LEN_MAX)
12149 			return -EINVAL;
12150 
12151 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
12152 		if (!qos_map)
12153 			return -ENOMEM;
12154 
12155 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
12156 		if (num_des) {
12157 			des_len = num_des *
12158 				sizeof(struct cfg80211_dscp_exception);
12159 			memcpy(qos_map->dscp_exception, pos, des_len);
12160 			qos_map->num_des = num_des;
12161 			for (des = 0; des < num_des; des++) {
12162 				if (qos_map->dscp_exception[des].up > 7) {
12163 					kfree(qos_map);
12164 					return -EINVAL;
12165 				}
12166 			}
12167 			pos += des_len;
12168 		}
12169 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
12170 	}
12171 
12172 	wdev_lock(dev->ieee80211_ptr);
12173 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
12174 	if (!ret)
12175 		ret = rdev_set_qos_map(rdev, dev, qos_map);
12176 	wdev_unlock(dev->ieee80211_ptr);
12177 
12178 	kfree(qos_map);
12179 	return ret;
12180 }
12181 
12182 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
12183 {
12184 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12185 	struct net_device *dev = info->user_ptr[1];
12186 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12187 	const u8 *peer;
12188 	u8 tsid, up;
12189 	u16 admitted_time = 0;
12190 	int err;
12191 
12192 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
12193 		return -EOPNOTSUPP;
12194 
12195 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
12196 	    !info->attrs[NL80211_ATTR_USER_PRIO])
12197 		return -EINVAL;
12198 
12199 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12200 	if (tsid >= IEEE80211_NUM_TIDS)
12201 		return -EINVAL;
12202 
12203 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
12204 	if (up >= IEEE80211_NUM_UPS)
12205 		return -EINVAL;
12206 
12207 	/* WMM uses TIDs 0-7 even for TSPEC */
12208 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
12209 		/* TODO: handle 802.11 TSPEC/admission control
12210 		 * need more attributes for that (e.g. BA session requirement);
12211 		 * change the WMM adminssion test above to allow both then
12212 		 */
12213 		return -EINVAL;
12214 	}
12215 
12216 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12217 
12218 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
12219 		admitted_time =
12220 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
12221 		if (!admitted_time)
12222 			return -EINVAL;
12223 	}
12224 
12225 	wdev_lock(wdev);
12226 	switch (wdev->iftype) {
12227 	case NL80211_IFTYPE_STATION:
12228 	case NL80211_IFTYPE_P2P_CLIENT:
12229 		if (wdev->current_bss)
12230 			break;
12231 		err = -ENOTCONN;
12232 		goto out;
12233 	default:
12234 		err = -EOPNOTSUPP;
12235 		goto out;
12236 	}
12237 
12238 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
12239 
12240  out:
12241 	wdev_unlock(wdev);
12242 	return err;
12243 }
12244 
12245 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
12246 {
12247 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12248 	struct net_device *dev = info->user_ptr[1];
12249 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12250 	const u8 *peer;
12251 	u8 tsid;
12252 	int err;
12253 
12254 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
12255 		return -EINVAL;
12256 
12257 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12258 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12259 
12260 	wdev_lock(wdev);
12261 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
12262 	wdev_unlock(wdev);
12263 
12264 	return err;
12265 }
12266 
12267 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
12268 				       struct genl_info *info)
12269 {
12270 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12271 	struct net_device *dev = info->user_ptr[1];
12272 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12273 	struct cfg80211_chan_def chandef = {};
12274 	const u8 *addr;
12275 	u8 oper_class;
12276 	int err;
12277 
12278 	if (!rdev->ops->tdls_channel_switch ||
12279 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12280 		return -EOPNOTSUPP;
12281 
12282 	switch (dev->ieee80211_ptr->iftype) {
12283 	case NL80211_IFTYPE_STATION:
12284 	case NL80211_IFTYPE_P2P_CLIENT:
12285 		break;
12286 	default:
12287 		return -EOPNOTSUPP;
12288 	}
12289 
12290 	if (!info->attrs[NL80211_ATTR_MAC] ||
12291 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
12292 		return -EINVAL;
12293 
12294 	err = nl80211_parse_chandef(rdev, info, &chandef);
12295 	if (err)
12296 		return err;
12297 
12298 	/*
12299 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
12300 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
12301 	 * specification is not defined for them.
12302 	 */
12303 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
12304 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
12305 	    chandef.width != NL80211_CHAN_WIDTH_20)
12306 		return -EINVAL;
12307 
12308 	/* we will be active on the TDLS link */
12309 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
12310 					   wdev->iftype))
12311 		return -EINVAL;
12312 
12313 	/* don't allow switching to DFS channels */
12314 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
12315 		return -EINVAL;
12316 
12317 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12318 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
12319 
12320 	wdev_lock(wdev);
12321 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
12322 	wdev_unlock(wdev);
12323 
12324 	return err;
12325 }
12326 
12327 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
12328 					      struct genl_info *info)
12329 {
12330 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12331 	struct net_device *dev = info->user_ptr[1];
12332 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12333 	const u8 *addr;
12334 
12335 	if (!rdev->ops->tdls_channel_switch ||
12336 	    !rdev->ops->tdls_cancel_channel_switch ||
12337 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12338 		return -EOPNOTSUPP;
12339 
12340 	switch (dev->ieee80211_ptr->iftype) {
12341 	case NL80211_IFTYPE_STATION:
12342 	case NL80211_IFTYPE_P2P_CLIENT:
12343 		break;
12344 	default:
12345 		return -EOPNOTSUPP;
12346 	}
12347 
12348 	if (!info->attrs[NL80211_ATTR_MAC])
12349 		return -EINVAL;
12350 
12351 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12352 
12353 	wdev_lock(wdev);
12354 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
12355 	wdev_unlock(wdev);
12356 
12357 	return 0;
12358 }
12359 
12360 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
12361 					    struct genl_info *info)
12362 {
12363 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12364 	struct net_device *dev = info->user_ptr[1];
12365 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12366 	const struct nlattr *nla;
12367 	bool enabled;
12368 
12369 	if (!rdev->ops->set_multicast_to_unicast)
12370 		return -EOPNOTSUPP;
12371 
12372 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12373 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12374 		return -EOPNOTSUPP;
12375 
12376 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
12377 	enabled = nla_get_flag(nla);
12378 
12379 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
12380 }
12381 
12382 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
12383 {
12384 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12385 	struct net_device *dev = info->user_ptr[1];
12386 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12387 	struct cfg80211_pmk_conf pmk_conf = {};
12388 	int ret;
12389 
12390 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12391 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12392 		return -EOPNOTSUPP;
12393 
12394 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12395 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12396 		return -EOPNOTSUPP;
12397 
12398 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
12399 		return -EINVAL;
12400 
12401 	wdev_lock(wdev);
12402 	if (!wdev->current_bss) {
12403 		ret = -ENOTCONN;
12404 		goto out;
12405 	}
12406 
12407 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12408 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
12409 		ret = -EINVAL;
12410 		goto out;
12411 	}
12412 
12413 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12414 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12415 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
12416 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
12417 		ret = -EINVAL;
12418 		goto out;
12419 	}
12420 
12421 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
12422 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12423 
12424 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
12425 			ret = -EINVAL;
12426 			goto out;
12427 		}
12428 
12429 		pmk_conf.pmk_r0_name =
12430 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12431 	}
12432 
12433 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
12434 out:
12435 	wdev_unlock(wdev);
12436 	return ret;
12437 }
12438 
12439 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
12440 {
12441 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12442 	struct net_device *dev = info->user_ptr[1];
12443 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12444 	const u8 *aa;
12445 	int ret;
12446 
12447 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
12448 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12449 		return -EOPNOTSUPP;
12450 
12451 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
12452 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12453 		return -EOPNOTSUPP;
12454 
12455 	if (!info->attrs[NL80211_ATTR_MAC])
12456 		return -EINVAL;
12457 
12458 	wdev_lock(wdev);
12459 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12460 	ret = rdev_del_pmk(rdev, dev, aa);
12461 	wdev_unlock(wdev);
12462 
12463 	return ret;
12464 }
12465 
12466 #define NL80211_FLAG_NEED_WIPHY		0x01
12467 #define NL80211_FLAG_NEED_NETDEV	0x02
12468 #define NL80211_FLAG_NEED_RTNL		0x04
12469 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
12470 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
12471 					 NL80211_FLAG_CHECK_NETDEV_UP)
12472 #define NL80211_FLAG_NEED_WDEV		0x10
12473 /* If a netdev is associated, it must be UP, P2P must be started */
12474 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
12475 					 NL80211_FLAG_CHECK_NETDEV_UP)
12476 #define NL80211_FLAG_CLEAR_SKB		0x20
12477 
12478 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
12479 			    struct genl_info *info)
12480 {
12481 	struct cfg80211_registered_device *rdev;
12482 	struct wireless_dev *wdev;
12483 	struct net_device *dev;
12484 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
12485 
12486 	if (rtnl)
12487 		rtnl_lock();
12488 
12489 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
12490 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
12491 		if (IS_ERR(rdev)) {
12492 			if (rtnl)
12493 				rtnl_unlock();
12494 			return PTR_ERR(rdev);
12495 		}
12496 		info->user_ptr[0] = rdev;
12497 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
12498 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12499 		ASSERT_RTNL();
12500 
12501 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
12502 						  info->attrs);
12503 		if (IS_ERR(wdev)) {
12504 			if (rtnl)
12505 				rtnl_unlock();
12506 			return PTR_ERR(wdev);
12507 		}
12508 
12509 		dev = wdev->netdev;
12510 		rdev = wiphy_to_rdev(wdev->wiphy);
12511 
12512 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
12513 			if (!dev) {
12514 				if (rtnl)
12515 					rtnl_unlock();
12516 				return -EINVAL;
12517 			}
12518 
12519 			info->user_ptr[1] = dev;
12520 		} else {
12521 			info->user_ptr[1] = wdev;
12522 		}
12523 
12524 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
12525 		    !wdev_running(wdev)) {
12526 			if (rtnl)
12527 				rtnl_unlock();
12528 			return -ENETDOWN;
12529 		}
12530 
12531 		if (dev)
12532 			dev_hold(dev);
12533 
12534 		info->user_ptr[0] = rdev;
12535 	}
12536 
12537 	return 0;
12538 }
12539 
12540 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
12541 			      struct genl_info *info)
12542 {
12543 	if (info->user_ptr[1]) {
12544 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12545 			struct wireless_dev *wdev = info->user_ptr[1];
12546 
12547 			if (wdev->netdev)
12548 				dev_put(wdev->netdev);
12549 		} else {
12550 			dev_put(info->user_ptr[1]);
12551 		}
12552 	}
12553 
12554 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
12555 		rtnl_unlock();
12556 
12557 	/* If needed, clear the netlink message payload from the SKB
12558 	 * as it might contain key data that shouldn't stick around on
12559 	 * the heap after the SKB is freed. The netlink message header
12560 	 * is still needed for further processing, so leave it intact.
12561 	 */
12562 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
12563 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
12564 
12565 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
12566 	}
12567 }
12568 
12569 static const struct genl_ops nl80211_ops[] = {
12570 	{
12571 		.cmd = NL80211_CMD_GET_WIPHY,
12572 		.doit = nl80211_get_wiphy,
12573 		.dumpit = nl80211_dump_wiphy,
12574 		.done = nl80211_dump_wiphy_done,
12575 		.policy = nl80211_policy,
12576 		/* can be retrieved by unprivileged users */
12577 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12578 				  NL80211_FLAG_NEED_RTNL,
12579 	},
12580 	{
12581 		.cmd = NL80211_CMD_SET_WIPHY,
12582 		.doit = nl80211_set_wiphy,
12583 		.policy = nl80211_policy,
12584 		.flags = GENL_UNS_ADMIN_PERM,
12585 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12586 	},
12587 	{
12588 		.cmd = NL80211_CMD_GET_INTERFACE,
12589 		.doit = nl80211_get_interface,
12590 		.dumpit = nl80211_dump_interface,
12591 		.policy = nl80211_policy,
12592 		/* can be retrieved by unprivileged users */
12593 		.internal_flags = NL80211_FLAG_NEED_WDEV |
12594 				  NL80211_FLAG_NEED_RTNL,
12595 	},
12596 	{
12597 		.cmd = NL80211_CMD_SET_INTERFACE,
12598 		.doit = nl80211_set_interface,
12599 		.policy = nl80211_policy,
12600 		.flags = GENL_UNS_ADMIN_PERM,
12601 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
12602 				  NL80211_FLAG_NEED_RTNL,
12603 	},
12604 	{
12605 		.cmd = NL80211_CMD_NEW_INTERFACE,
12606 		.doit = nl80211_new_interface,
12607 		.policy = nl80211_policy,
12608 		.flags = GENL_UNS_ADMIN_PERM,
12609 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12610 				  NL80211_FLAG_NEED_RTNL,
12611 	},
12612 	{
12613 		.cmd = NL80211_CMD_DEL_INTERFACE,
12614 		.doit = nl80211_del_interface,
12615 		.policy = nl80211_policy,
12616 		.flags = GENL_UNS_ADMIN_PERM,
12617 		.internal_flags = NL80211_FLAG_NEED_WDEV |
12618 				  NL80211_FLAG_NEED_RTNL,
12619 	},
12620 	{
12621 		.cmd = NL80211_CMD_GET_KEY,
12622 		.doit = nl80211_get_key,
12623 		.policy = nl80211_policy,
12624 		.flags = GENL_UNS_ADMIN_PERM,
12625 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12626 				  NL80211_FLAG_NEED_RTNL,
12627 	},
12628 	{
12629 		.cmd = NL80211_CMD_SET_KEY,
12630 		.doit = nl80211_set_key,
12631 		.policy = nl80211_policy,
12632 		.flags = GENL_UNS_ADMIN_PERM,
12633 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12634 				  NL80211_FLAG_NEED_RTNL |
12635 				  NL80211_FLAG_CLEAR_SKB,
12636 	},
12637 	{
12638 		.cmd = NL80211_CMD_NEW_KEY,
12639 		.doit = nl80211_new_key,
12640 		.policy = nl80211_policy,
12641 		.flags = GENL_UNS_ADMIN_PERM,
12642 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12643 				  NL80211_FLAG_NEED_RTNL |
12644 				  NL80211_FLAG_CLEAR_SKB,
12645 	},
12646 	{
12647 		.cmd = NL80211_CMD_DEL_KEY,
12648 		.doit = nl80211_del_key,
12649 		.policy = nl80211_policy,
12650 		.flags = GENL_UNS_ADMIN_PERM,
12651 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12652 				  NL80211_FLAG_NEED_RTNL,
12653 	},
12654 	{
12655 		.cmd = NL80211_CMD_SET_BEACON,
12656 		.policy = nl80211_policy,
12657 		.flags = GENL_UNS_ADMIN_PERM,
12658 		.doit = nl80211_set_beacon,
12659 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12660 				  NL80211_FLAG_NEED_RTNL,
12661 	},
12662 	{
12663 		.cmd = NL80211_CMD_START_AP,
12664 		.policy = nl80211_policy,
12665 		.flags = GENL_UNS_ADMIN_PERM,
12666 		.doit = nl80211_start_ap,
12667 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12668 				  NL80211_FLAG_NEED_RTNL,
12669 	},
12670 	{
12671 		.cmd = NL80211_CMD_STOP_AP,
12672 		.policy = nl80211_policy,
12673 		.flags = GENL_UNS_ADMIN_PERM,
12674 		.doit = nl80211_stop_ap,
12675 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12676 				  NL80211_FLAG_NEED_RTNL,
12677 	},
12678 	{
12679 		.cmd = NL80211_CMD_GET_STATION,
12680 		.doit = nl80211_get_station,
12681 		.dumpit = nl80211_dump_station,
12682 		.policy = nl80211_policy,
12683 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
12684 				  NL80211_FLAG_NEED_RTNL,
12685 	},
12686 	{
12687 		.cmd = NL80211_CMD_SET_STATION,
12688 		.doit = nl80211_set_station,
12689 		.policy = nl80211_policy,
12690 		.flags = GENL_UNS_ADMIN_PERM,
12691 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12692 				  NL80211_FLAG_NEED_RTNL,
12693 	},
12694 	{
12695 		.cmd = NL80211_CMD_NEW_STATION,
12696 		.doit = nl80211_new_station,
12697 		.policy = nl80211_policy,
12698 		.flags = GENL_UNS_ADMIN_PERM,
12699 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12700 				  NL80211_FLAG_NEED_RTNL,
12701 	},
12702 	{
12703 		.cmd = NL80211_CMD_DEL_STATION,
12704 		.doit = nl80211_del_station,
12705 		.policy = nl80211_policy,
12706 		.flags = GENL_UNS_ADMIN_PERM,
12707 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12708 				  NL80211_FLAG_NEED_RTNL,
12709 	},
12710 	{
12711 		.cmd = NL80211_CMD_GET_MPATH,
12712 		.doit = nl80211_get_mpath,
12713 		.dumpit = nl80211_dump_mpath,
12714 		.policy = nl80211_policy,
12715 		.flags = GENL_UNS_ADMIN_PERM,
12716 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12717 				  NL80211_FLAG_NEED_RTNL,
12718 	},
12719 	{
12720 		.cmd = NL80211_CMD_GET_MPP,
12721 		.doit = nl80211_get_mpp,
12722 		.dumpit = nl80211_dump_mpp,
12723 		.policy = nl80211_policy,
12724 		.flags = GENL_UNS_ADMIN_PERM,
12725 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12726 				  NL80211_FLAG_NEED_RTNL,
12727 	},
12728 	{
12729 		.cmd = NL80211_CMD_SET_MPATH,
12730 		.doit = nl80211_set_mpath,
12731 		.policy = nl80211_policy,
12732 		.flags = GENL_UNS_ADMIN_PERM,
12733 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12734 				  NL80211_FLAG_NEED_RTNL,
12735 	},
12736 	{
12737 		.cmd = NL80211_CMD_NEW_MPATH,
12738 		.doit = nl80211_new_mpath,
12739 		.policy = nl80211_policy,
12740 		.flags = GENL_UNS_ADMIN_PERM,
12741 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12742 				  NL80211_FLAG_NEED_RTNL,
12743 	},
12744 	{
12745 		.cmd = NL80211_CMD_DEL_MPATH,
12746 		.doit = nl80211_del_mpath,
12747 		.policy = nl80211_policy,
12748 		.flags = GENL_UNS_ADMIN_PERM,
12749 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12750 				  NL80211_FLAG_NEED_RTNL,
12751 	},
12752 	{
12753 		.cmd = NL80211_CMD_SET_BSS,
12754 		.doit = nl80211_set_bss,
12755 		.policy = nl80211_policy,
12756 		.flags = GENL_UNS_ADMIN_PERM,
12757 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12758 				  NL80211_FLAG_NEED_RTNL,
12759 	},
12760 	{
12761 		.cmd = NL80211_CMD_GET_REG,
12762 		.doit = nl80211_get_reg_do,
12763 		.dumpit = nl80211_get_reg_dump,
12764 		.policy = nl80211_policy,
12765 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12766 		/* can be retrieved by unprivileged users */
12767 	},
12768 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
12769 	{
12770 		.cmd = NL80211_CMD_SET_REG,
12771 		.doit = nl80211_set_reg,
12772 		.policy = nl80211_policy,
12773 		.flags = GENL_ADMIN_PERM,
12774 		.internal_flags = NL80211_FLAG_NEED_RTNL,
12775 	},
12776 #endif
12777 	{
12778 		.cmd = NL80211_CMD_REQ_SET_REG,
12779 		.doit = nl80211_req_set_reg,
12780 		.policy = nl80211_policy,
12781 		.flags = GENL_ADMIN_PERM,
12782 	},
12783 	{
12784 		.cmd = NL80211_CMD_RELOAD_REGDB,
12785 		.doit = nl80211_reload_regdb,
12786 		.policy = nl80211_policy,
12787 		.flags = GENL_ADMIN_PERM,
12788 	},
12789 	{
12790 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
12791 		.doit = nl80211_get_mesh_config,
12792 		.policy = nl80211_policy,
12793 		/* can be retrieved by unprivileged users */
12794 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12795 				  NL80211_FLAG_NEED_RTNL,
12796 	},
12797 	{
12798 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
12799 		.doit = nl80211_update_mesh_config,
12800 		.policy = nl80211_policy,
12801 		.flags = GENL_UNS_ADMIN_PERM,
12802 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12803 				  NL80211_FLAG_NEED_RTNL,
12804 	},
12805 	{
12806 		.cmd = NL80211_CMD_TRIGGER_SCAN,
12807 		.doit = nl80211_trigger_scan,
12808 		.policy = nl80211_policy,
12809 		.flags = GENL_UNS_ADMIN_PERM,
12810 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12811 				  NL80211_FLAG_NEED_RTNL,
12812 	},
12813 	{
12814 		.cmd = NL80211_CMD_ABORT_SCAN,
12815 		.doit = nl80211_abort_scan,
12816 		.policy = nl80211_policy,
12817 		.flags = GENL_UNS_ADMIN_PERM,
12818 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12819 				  NL80211_FLAG_NEED_RTNL,
12820 	},
12821 	{
12822 		.cmd = NL80211_CMD_GET_SCAN,
12823 		.policy = nl80211_policy,
12824 		.dumpit = nl80211_dump_scan,
12825 	},
12826 	{
12827 		.cmd = NL80211_CMD_START_SCHED_SCAN,
12828 		.doit = nl80211_start_sched_scan,
12829 		.policy = nl80211_policy,
12830 		.flags = GENL_UNS_ADMIN_PERM,
12831 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12832 				  NL80211_FLAG_NEED_RTNL,
12833 	},
12834 	{
12835 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
12836 		.doit = nl80211_stop_sched_scan,
12837 		.policy = nl80211_policy,
12838 		.flags = GENL_UNS_ADMIN_PERM,
12839 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12840 				  NL80211_FLAG_NEED_RTNL,
12841 	},
12842 	{
12843 		.cmd = NL80211_CMD_AUTHENTICATE,
12844 		.doit = nl80211_authenticate,
12845 		.policy = nl80211_policy,
12846 		.flags = GENL_UNS_ADMIN_PERM,
12847 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12848 				  NL80211_FLAG_NEED_RTNL |
12849 				  NL80211_FLAG_CLEAR_SKB,
12850 	},
12851 	{
12852 		.cmd = NL80211_CMD_ASSOCIATE,
12853 		.doit = nl80211_associate,
12854 		.policy = nl80211_policy,
12855 		.flags = GENL_UNS_ADMIN_PERM,
12856 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12857 				  NL80211_FLAG_NEED_RTNL,
12858 	},
12859 	{
12860 		.cmd = NL80211_CMD_DEAUTHENTICATE,
12861 		.doit = nl80211_deauthenticate,
12862 		.policy = nl80211_policy,
12863 		.flags = GENL_UNS_ADMIN_PERM,
12864 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12865 				  NL80211_FLAG_NEED_RTNL,
12866 	},
12867 	{
12868 		.cmd = NL80211_CMD_DISASSOCIATE,
12869 		.doit = nl80211_disassociate,
12870 		.policy = nl80211_policy,
12871 		.flags = GENL_UNS_ADMIN_PERM,
12872 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12873 				  NL80211_FLAG_NEED_RTNL,
12874 	},
12875 	{
12876 		.cmd = NL80211_CMD_JOIN_IBSS,
12877 		.doit = nl80211_join_ibss,
12878 		.policy = nl80211_policy,
12879 		.flags = GENL_UNS_ADMIN_PERM,
12880 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12881 				  NL80211_FLAG_NEED_RTNL,
12882 	},
12883 	{
12884 		.cmd = NL80211_CMD_LEAVE_IBSS,
12885 		.doit = nl80211_leave_ibss,
12886 		.policy = nl80211_policy,
12887 		.flags = GENL_UNS_ADMIN_PERM,
12888 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12889 				  NL80211_FLAG_NEED_RTNL,
12890 	},
12891 #ifdef CONFIG_NL80211_TESTMODE
12892 	{
12893 		.cmd = NL80211_CMD_TESTMODE,
12894 		.doit = nl80211_testmode_do,
12895 		.dumpit = nl80211_testmode_dump,
12896 		.policy = nl80211_policy,
12897 		.flags = GENL_UNS_ADMIN_PERM,
12898 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12899 				  NL80211_FLAG_NEED_RTNL,
12900 	},
12901 #endif
12902 	{
12903 		.cmd = NL80211_CMD_CONNECT,
12904 		.doit = nl80211_connect,
12905 		.policy = nl80211_policy,
12906 		.flags = GENL_UNS_ADMIN_PERM,
12907 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12908 				  NL80211_FLAG_NEED_RTNL,
12909 	},
12910 	{
12911 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
12912 		.doit = nl80211_update_connect_params,
12913 		.policy = nl80211_policy,
12914 		.flags = GENL_ADMIN_PERM,
12915 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12916 				  NL80211_FLAG_NEED_RTNL,
12917 	},
12918 	{
12919 		.cmd = NL80211_CMD_DISCONNECT,
12920 		.doit = nl80211_disconnect,
12921 		.policy = nl80211_policy,
12922 		.flags = GENL_UNS_ADMIN_PERM,
12923 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12924 				  NL80211_FLAG_NEED_RTNL,
12925 	},
12926 	{
12927 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
12928 		.doit = nl80211_wiphy_netns,
12929 		.policy = nl80211_policy,
12930 		.flags = GENL_UNS_ADMIN_PERM,
12931 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
12932 				  NL80211_FLAG_NEED_RTNL,
12933 	},
12934 	{
12935 		.cmd = NL80211_CMD_GET_SURVEY,
12936 		.policy = nl80211_policy,
12937 		.dumpit = nl80211_dump_survey,
12938 	},
12939 	{
12940 		.cmd = NL80211_CMD_SET_PMKSA,
12941 		.doit = nl80211_setdel_pmksa,
12942 		.policy = nl80211_policy,
12943 		.flags = GENL_UNS_ADMIN_PERM,
12944 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12945 				  NL80211_FLAG_NEED_RTNL,
12946 	},
12947 	{
12948 		.cmd = NL80211_CMD_DEL_PMKSA,
12949 		.doit = nl80211_setdel_pmksa,
12950 		.policy = nl80211_policy,
12951 		.flags = GENL_UNS_ADMIN_PERM,
12952 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12953 				  NL80211_FLAG_NEED_RTNL,
12954 	},
12955 	{
12956 		.cmd = NL80211_CMD_FLUSH_PMKSA,
12957 		.doit = nl80211_flush_pmksa,
12958 		.policy = nl80211_policy,
12959 		.flags = GENL_UNS_ADMIN_PERM,
12960 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12961 				  NL80211_FLAG_NEED_RTNL,
12962 	},
12963 	{
12964 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
12965 		.doit = nl80211_remain_on_channel,
12966 		.policy = nl80211_policy,
12967 		.flags = GENL_UNS_ADMIN_PERM,
12968 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12969 				  NL80211_FLAG_NEED_RTNL,
12970 	},
12971 	{
12972 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
12973 		.doit = nl80211_cancel_remain_on_channel,
12974 		.policy = nl80211_policy,
12975 		.flags = GENL_UNS_ADMIN_PERM,
12976 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12977 				  NL80211_FLAG_NEED_RTNL,
12978 	},
12979 	{
12980 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
12981 		.doit = nl80211_set_tx_bitrate_mask,
12982 		.policy = nl80211_policy,
12983 		.flags = GENL_UNS_ADMIN_PERM,
12984 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
12985 				  NL80211_FLAG_NEED_RTNL,
12986 	},
12987 	{
12988 		.cmd = NL80211_CMD_REGISTER_FRAME,
12989 		.doit = nl80211_register_mgmt,
12990 		.policy = nl80211_policy,
12991 		.flags = GENL_UNS_ADMIN_PERM,
12992 		.internal_flags = NL80211_FLAG_NEED_WDEV |
12993 				  NL80211_FLAG_NEED_RTNL,
12994 	},
12995 	{
12996 		.cmd = NL80211_CMD_FRAME,
12997 		.doit = nl80211_tx_mgmt,
12998 		.policy = nl80211_policy,
12999 		.flags = GENL_UNS_ADMIN_PERM,
13000 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13001 				  NL80211_FLAG_NEED_RTNL,
13002 	},
13003 	{
13004 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
13005 		.doit = nl80211_tx_mgmt_cancel_wait,
13006 		.policy = nl80211_policy,
13007 		.flags = GENL_UNS_ADMIN_PERM,
13008 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13009 				  NL80211_FLAG_NEED_RTNL,
13010 	},
13011 	{
13012 		.cmd = NL80211_CMD_SET_POWER_SAVE,
13013 		.doit = nl80211_set_power_save,
13014 		.policy = nl80211_policy,
13015 		.flags = GENL_UNS_ADMIN_PERM,
13016 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13017 				  NL80211_FLAG_NEED_RTNL,
13018 	},
13019 	{
13020 		.cmd = NL80211_CMD_GET_POWER_SAVE,
13021 		.doit = nl80211_get_power_save,
13022 		.policy = nl80211_policy,
13023 		/* can be retrieved by unprivileged users */
13024 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13025 				  NL80211_FLAG_NEED_RTNL,
13026 	},
13027 	{
13028 		.cmd = NL80211_CMD_SET_CQM,
13029 		.doit = nl80211_set_cqm,
13030 		.policy = nl80211_policy,
13031 		.flags = GENL_UNS_ADMIN_PERM,
13032 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13033 				  NL80211_FLAG_NEED_RTNL,
13034 	},
13035 	{
13036 		.cmd = NL80211_CMD_SET_CHANNEL,
13037 		.doit = nl80211_set_channel,
13038 		.policy = nl80211_policy,
13039 		.flags = GENL_UNS_ADMIN_PERM,
13040 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13041 				  NL80211_FLAG_NEED_RTNL,
13042 	},
13043 	{
13044 		.cmd = NL80211_CMD_SET_WDS_PEER,
13045 		.doit = nl80211_set_wds_peer,
13046 		.policy = nl80211_policy,
13047 		.flags = GENL_UNS_ADMIN_PERM,
13048 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13049 				  NL80211_FLAG_NEED_RTNL,
13050 	},
13051 	{
13052 		.cmd = NL80211_CMD_JOIN_MESH,
13053 		.doit = nl80211_join_mesh,
13054 		.policy = nl80211_policy,
13055 		.flags = GENL_UNS_ADMIN_PERM,
13056 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13057 				  NL80211_FLAG_NEED_RTNL,
13058 	},
13059 	{
13060 		.cmd = NL80211_CMD_LEAVE_MESH,
13061 		.doit = nl80211_leave_mesh,
13062 		.policy = nl80211_policy,
13063 		.flags = GENL_UNS_ADMIN_PERM,
13064 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13065 				  NL80211_FLAG_NEED_RTNL,
13066 	},
13067 	{
13068 		.cmd = NL80211_CMD_JOIN_OCB,
13069 		.doit = nl80211_join_ocb,
13070 		.policy = nl80211_policy,
13071 		.flags = GENL_UNS_ADMIN_PERM,
13072 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13073 				  NL80211_FLAG_NEED_RTNL,
13074 	},
13075 	{
13076 		.cmd = NL80211_CMD_LEAVE_OCB,
13077 		.doit = nl80211_leave_ocb,
13078 		.policy = nl80211_policy,
13079 		.flags = GENL_UNS_ADMIN_PERM,
13080 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13081 				  NL80211_FLAG_NEED_RTNL,
13082 	},
13083 #ifdef CONFIG_PM
13084 	{
13085 		.cmd = NL80211_CMD_GET_WOWLAN,
13086 		.doit = nl80211_get_wowlan,
13087 		.policy = nl80211_policy,
13088 		/* can be retrieved by unprivileged users */
13089 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13090 				  NL80211_FLAG_NEED_RTNL,
13091 	},
13092 	{
13093 		.cmd = NL80211_CMD_SET_WOWLAN,
13094 		.doit = nl80211_set_wowlan,
13095 		.policy = nl80211_policy,
13096 		.flags = GENL_UNS_ADMIN_PERM,
13097 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13098 				  NL80211_FLAG_NEED_RTNL,
13099 	},
13100 #endif
13101 	{
13102 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
13103 		.doit = nl80211_set_rekey_data,
13104 		.policy = nl80211_policy,
13105 		.flags = GENL_UNS_ADMIN_PERM,
13106 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13107 				  NL80211_FLAG_NEED_RTNL |
13108 				  NL80211_FLAG_CLEAR_SKB,
13109 	},
13110 	{
13111 		.cmd = NL80211_CMD_TDLS_MGMT,
13112 		.doit = nl80211_tdls_mgmt,
13113 		.policy = nl80211_policy,
13114 		.flags = GENL_UNS_ADMIN_PERM,
13115 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13116 				  NL80211_FLAG_NEED_RTNL,
13117 	},
13118 	{
13119 		.cmd = NL80211_CMD_TDLS_OPER,
13120 		.doit = nl80211_tdls_oper,
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_UNEXPECTED_FRAME,
13128 		.doit = nl80211_register_unexpected_frame,
13129 		.policy = nl80211_policy,
13130 		.flags = GENL_UNS_ADMIN_PERM,
13131 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13132 				  NL80211_FLAG_NEED_RTNL,
13133 	},
13134 	{
13135 		.cmd = NL80211_CMD_PROBE_CLIENT,
13136 		.doit = nl80211_probe_client,
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_REGISTER_BEACONS,
13144 		.doit = nl80211_register_beacons,
13145 		.policy = nl80211_policy,
13146 		.flags = GENL_UNS_ADMIN_PERM,
13147 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13148 				  NL80211_FLAG_NEED_RTNL,
13149 	},
13150 	{
13151 		.cmd = NL80211_CMD_SET_NOACK_MAP,
13152 		.doit = nl80211_set_noack_map,
13153 		.policy = nl80211_policy,
13154 		.flags = GENL_UNS_ADMIN_PERM,
13155 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13156 				  NL80211_FLAG_NEED_RTNL,
13157 	},
13158 	{
13159 		.cmd = NL80211_CMD_START_P2P_DEVICE,
13160 		.doit = nl80211_start_p2p_device,
13161 		.policy = nl80211_policy,
13162 		.flags = GENL_UNS_ADMIN_PERM,
13163 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13164 				  NL80211_FLAG_NEED_RTNL,
13165 	},
13166 	{
13167 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
13168 		.doit = nl80211_stop_p2p_device,
13169 		.policy = nl80211_policy,
13170 		.flags = GENL_UNS_ADMIN_PERM,
13171 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13172 				  NL80211_FLAG_NEED_RTNL,
13173 	},
13174 	{
13175 		.cmd = NL80211_CMD_START_NAN,
13176 		.doit = nl80211_start_nan,
13177 		.policy = nl80211_policy,
13178 		.flags = GENL_ADMIN_PERM,
13179 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13180 				  NL80211_FLAG_NEED_RTNL,
13181 	},
13182 	{
13183 		.cmd = NL80211_CMD_STOP_NAN,
13184 		.doit = nl80211_stop_nan,
13185 		.policy = nl80211_policy,
13186 		.flags = GENL_ADMIN_PERM,
13187 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13188 				  NL80211_FLAG_NEED_RTNL,
13189 	},
13190 	{
13191 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
13192 		.doit = nl80211_nan_add_func,
13193 		.policy = nl80211_policy,
13194 		.flags = GENL_ADMIN_PERM,
13195 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13196 				  NL80211_FLAG_NEED_RTNL,
13197 	},
13198 	{
13199 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
13200 		.doit = nl80211_nan_del_func,
13201 		.policy = nl80211_policy,
13202 		.flags = GENL_ADMIN_PERM,
13203 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13204 				  NL80211_FLAG_NEED_RTNL,
13205 	},
13206 	{
13207 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
13208 		.doit = nl80211_nan_change_config,
13209 		.policy = nl80211_policy,
13210 		.flags = GENL_ADMIN_PERM,
13211 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13212 				  NL80211_FLAG_NEED_RTNL,
13213 	},
13214 	{
13215 		.cmd = NL80211_CMD_SET_MCAST_RATE,
13216 		.doit = nl80211_set_mcast_rate,
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_MAC_ACL,
13224 		.doit = nl80211_set_mac_acl,
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_RADAR_DETECT,
13232 		.doit = nl80211_start_radar_detection,
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_GET_PROTOCOL_FEATURES,
13240 		.doit = nl80211_get_protocol_features,
13241 		.policy = nl80211_policy,
13242 	},
13243 	{
13244 		.cmd = NL80211_CMD_UPDATE_FT_IES,
13245 		.doit = nl80211_update_ft_ies,
13246 		.policy = nl80211_policy,
13247 		.flags = GENL_UNS_ADMIN_PERM,
13248 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13249 				  NL80211_FLAG_NEED_RTNL,
13250 	},
13251 	{
13252 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
13253 		.doit = nl80211_crit_protocol_start,
13254 		.policy = nl80211_policy,
13255 		.flags = GENL_UNS_ADMIN_PERM,
13256 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13257 				  NL80211_FLAG_NEED_RTNL,
13258 	},
13259 	{
13260 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
13261 		.doit = nl80211_crit_protocol_stop,
13262 		.policy = nl80211_policy,
13263 		.flags = GENL_UNS_ADMIN_PERM,
13264 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13265 				  NL80211_FLAG_NEED_RTNL,
13266 	},
13267 	{
13268 		.cmd = NL80211_CMD_GET_COALESCE,
13269 		.doit = nl80211_get_coalesce,
13270 		.policy = nl80211_policy,
13271 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13272 				  NL80211_FLAG_NEED_RTNL,
13273 	},
13274 	{
13275 		.cmd = NL80211_CMD_SET_COALESCE,
13276 		.doit = nl80211_set_coalesce,
13277 		.policy = nl80211_policy,
13278 		.flags = GENL_UNS_ADMIN_PERM,
13279 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13280 				  NL80211_FLAG_NEED_RTNL,
13281 	},
13282 	{
13283 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
13284 		.doit = nl80211_channel_switch,
13285 		.policy = nl80211_policy,
13286 		.flags = GENL_UNS_ADMIN_PERM,
13287 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13288 				  NL80211_FLAG_NEED_RTNL,
13289 	},
13290 	{
13291 		.cmd = NL80211_CMD_VENDOR,
13292 		.doit = nl80211_vendor_cmd,
13293 		.dumpit = nl80211_vendor_cmd_dump,
13294 		.policy = nl80211_policy,
13295 		.flags = GENL_UNS_ADMIN_PERM,
13296 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13297 				  NL80211_FLAG_NEED_RTNL,
13298 	},
13299 	{
13300 		.cmd = NL80211_CMD_SET_QOS_MAP,
13301 		.doit = nl80211_set_qos_map,
13302 		.policy = nl80211_policy,
13303 		.flags = GENL_UNS_ADMIN_PERM,
13304 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13305 				  NL80211_FLAG_NEED_RTNL,
13306 	},
13307 	{
13308 		.cmd = NL80211_CMD_ADD_TX_TS,
13309 		.doit = nl80211_add_tx_ts,
13310 		.policy = nl80211_policy,
13311 		.flags = GENL_UNS_ADMIN_PERM,
13312 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13313 				  NL80211_FLAG_NEED_RTNL,
13314 	},
13315 	{
13316 		.cmd = NL80211_CMD_DEL_TX_TS,
13317 		.doit = nl80211_del_tx_ts,
13318 		.policy = nl80211_policy,
13319 		.flags = GENL_UNS_ADMIN_PERM,
13320 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13321 				  NL80211_FLAG_NEED_RTNL,
13322 	},
13323 	{
13324 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
13325 		.doit = nl80211_tdls_channel_switch,
13326 		.policy = nl80211_policy,
13327 		.flags = GENL_UNS_ADMIN_PERM,
13328 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13329 				  NL80211_FLAG_NEED_RTNL,
13330 	},
13331 	{
13332 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
13333 		.doit = nl80211_tdls_cancel_channel_switch,
13334 		.policy = nl80211_policy,
13335 		.flags = GENL_UNS_ADMIN_PERM,
13336 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13337 				  NL80211_FLAG_NEED_RTNL,
13338 	},
13339 	{
13340 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
13341 		.doit = nl80211_set_multicast_to_unicast,
13342 		.policy = nl80211_policy,
13343 		.flags = GENL_UNS_ADMIN_PERM,
13344 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13345 				  NL80211_FLAG_NEED_RTNL,
13346 	},
13347 	{
13348 		.cmd = NL80211_CMD_SET_PMK,
13349 		.doit = nl80211_set_pmk,
13350 		.policy = nl80211_policy,
13351 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13352 				  NL80211_FLAG_NEED_RTNL,
13353 	},
13354 	{
13355 		.cmd = NL80211_CMD_DEL_PMK,
13356 		.doit = nl80211_del_pmk,
13357 		.policy = nl80211_policy,
13358 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13359 				  NL80211_FLAG_NEED_RTNL,
13360 	},
13361 
13362 };
13363 
13364 static struct genl_family nl80211_fam __ro_after_init = {
13365 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
13366 	.hdrsize = 0,			/* no private header */
13367 	.version = 1,			/* no particular meaning now */
13368 	.maxattr = NL80211_ATTR_MAX,
13369 	.netnsok = true,
13370 	.pre_doit = nl80211_pre_doit,
13371 	.post_doit = nl80211_post_doit,
13372 	.module = THIS_MODULE,
13373 	.ops = nl80211_ops,
13374 	.n_ops = ARRAY_SIZE(nl80211_ops),
13375 	.mcgrps = nl80211_mcgrps,
13376 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
13377 };
13378 
13379 /* notification functions */
13380 
13381 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
13382 			  enum nl80211_commands cmd)
13383 {
13384 	struct sk_buff *msg;
13385 	struct nl80211_dump_wiphy_state state = {};
13386 
13387 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
13388 		cmd != NL80211_CMD_DEL_WIPHY);
13389 
13390 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13391 	if (!msg)
13392 		return;
13393 
13394 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
13395 		nlmsg_free(msg);
13396 		return;
13397 	}
13398 
13399 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13400 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
13401 }
13402 
13403 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
13404 				struct wireless_dev *wdev,
13405 				enum nl80211_commands cmd)
13406 {
13407 	struct sk_buff *msg;
13408 
13409 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
13410 		cmd != NL80211_CMD_DEL_INTERFACE);
13411 
13412 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13413 	if (!msg)
13414 		return;
13415 
13416 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev,
13417 			       cmd == NL80211_CMD_DEL_INTERFACE) < 0) {
13418 		nlmsg_free(msg);
13419 		return;
13420 	}
13421 
13422 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13423 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
13424 }
13425 
13426 static int nl80211_add_scan_req(struct sk_buff *msg,
13427 				struct cfg80211_registered_device *rdev)
13428 {
13429 	struct cfg80211_scan_request *req = rdev->scan_req;
13430 	struct nlattr *nest;
13431 	int i;
13432 
13433 	if (WARN_ON(!req))
13434 		return 0;
13435 
13436 	nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
13437 	if (!nest)
13438 		goto nla_put_failure;
13439 	for (i = 0; i < req->n_ssids; i++) {
13440 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
13441 			goto nla_put_failure;
13442 	}
13443 	nla_nest_end(msg, nest);
13444 
13445 	nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13446 	if (!nest)
13447 		goto nla_put_failure;
13448 	for (i = 0; i < req->n_channels; i++) {
13449 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13450 			goto nla_put_failure;
13451 	}
13452 	nla_nest_end(msg, nest);
13453 
13454 	if (req->ie &&
13455 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
13456 		goto nla_put_failure;
13457 
13458 	if (req->flags &&
13459 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
13460 		goto nla_put_failure;
13461 
13462 	if (req->info.scan_start_tsf &&
13463 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
13464 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
13465 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
13466 		     req->info.tsf_bssid)))
13467 		goto nla_put_failure;
13468 
13469 	return 0;
13470  nla_put_failure:
13471 	return -ENOBUFS;
13472 }
13473 
13474 static int nl80211_prep_scan_msg(struct sk_buff *msg,
13475 				 struct cfg80211_registered_device *rdev,
13476 				 struct wireless_dev *wdev,
13477 				 u32 portid, u32 seq, int flags,
13478 				 u32 cmd)
13479 {
13480 	void *hdr;
13481 
13482 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
13483 	if (!hdr)
13484 		return -1;
13485 
13486 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13487 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13488 					 wdev->netdev->ifindex)) ||
13489 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13490 			      NL80211_ATTR_PAD))
13491 		goto nla_put_failure;
13492 
13493 	/* ignore errors and send incomplete event anyway */
13494 	nl80211_add_scan_req(msg, rdev);
13495 
13496 	genlmsg_end(msg, hdr);
13497 	return 0;
13498 
13499  nla_put_failure:
13500 	genlmsg_cancel(msg, hdr);
13501 	return -EMSGSIZE;
13502 }
13503 
13504 static int
13505 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
13506 			    struct cfg80211_sched_scan_request *req, u32 cmd)
13507 {
13508 	void *hdr;
13509 
13510 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13511 	if (!hdr)
13512 		return -1;
13513 
13514 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
13515 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
13516 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
13517 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
13518 			      NL80211_ATTR_PAD))
13519 		goto nla_put_failure;
13520 
13521 	genlmsg_end(msg, hdr);
13522 	return 0;
13523 
13524  nla_put_failure:
13525 	genlmsg_cancel(msg, hdr);
13526 	return -EMSGSIZE;
13527 }
13528 
13529 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
13530 			     struct wireless_dev *wdev)
13531 {
13532 	struct sk_buff *msg;
13533 
13534 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13535 	if (!msg)
13536 		return;
13537 
13538 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13539 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
13540 		nlmsg_free(msg);
13541 		return;
13542 	}
13543 
13544 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13545 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13546 }
13547 
13548 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
13549 				       struct wireless_dev *wdev, bool aborted)
13550 {
13551 	struct sk_buff *msg;
13552 
13553 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13554 	if (!msg)
13555 		return NULL;
13556 
13557 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13558 				  aborted ? NL80211_CMD_SCAN_ABORTED :
13559 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
13560 		nlmsg_free(msg);
13561 		return NULL;
13562 	}
13563 
13564 	return msg;
13565 }
13566 
13567 /* send message created by nl80211_build_scan_msg() */
13568 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
13569 			   struct sk_buff *msg)
13570 {
13571 	if (!msg)
13572 		return;
13573 
13574 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13575 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13576 }
13577 
13578 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
13579 {
13580 	struct sk_buff *msg;
13581 
13582 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13583 	if (!msg)
13584 		return;
13585 
13586 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
13587 		nlmsg_free(msg);
13588 		return;
13589 	}
13590 
13591 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
13592 				NL80211_MCGRP_SCAN, GFP_KERNEL);
13593 }
13594 
13595 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
13596 					  struct regulatory_request *request)
13597 {
13598 	/* Userspace can always count this one always being set */
13599 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
13600 		goto nla_put_failure;
13601 
13602 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
13603 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13604 			       NL80211_REGDOM_TYPE_WORLD))
13605 			goto nla_put_failure;
13606 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
13607 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13608 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
13609 			goto nla_put_failure;
13610 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
13611 		   request->intersect) {
13612 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13613 			       NL80211_REGDOM_TYPE_INTERSECTION))
13614 			goto nla_put_failure;
13615 	} else {
13616 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13617 			       NL80211_REGDOM_TYPE_COUNTRY) ||
13618 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
13619 				   request->alpha2))
13620 			goto nla_put_failure;
13621 	}
13622 
13623 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
13624 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
13625 
13626 		if (wiphy &&
13627 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
13628 			goto nla_put_failure;
13629 
13630 		if (wiphy &&
13631 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
13632 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
13633 			goto nla_put_failure;
13634 	}
13635 
13636 	return true;
13637 
13638 nla_put_failure:
13639 	return false;
13640 }
13641 
13642 /*
13643  * This can happen on global regulatory changes or device specific settings
13644  * based on custom regulatory domains.
13645  */
13646 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
13647 				     struct regulatory_request *request)
13648 {
13649 	struct sk_buff *msg;
13650 	void *hdr;
13651 
13652 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13653 	if (!msg)
13654 		return;
13655 
13656 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
13657 	if (!hdr) {
13658 		nlmsg_free(msg);
13659 		return;
13660 	}
13661 
13662 	if (nl80211_reg_change_event_fill(msg, request) == false)
13663 		goto nla_put_failure;
13664 
13665 	genlmsg_end(msg, hdr);
13666 
13667 	rcu_read_lock();
13668 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
13669 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
13670 	rcu_read_unlock();
13671 
13672 	return;
13673 
13674 nla_put_failure:
13675 	genlmsg_cancel(msg, hdr);
13676 	nlmsg_free(msg);
13677 }
13678 
13679 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
13680 				    struct net_device *netdev,
13681 				    const u8 *buf, size_t len,
13682 				    enum nl80211_commands cmd, gfp_t gfp,
13683 				    int uapsd_queues)
13684 {
13685 	struct sk_buff *msg;
13686 	void *hdr;
13687 
13688 	msg = nlmsg_new(100 + len, gfp);
13689 	if (!msg)
13690 		return;
13691 
13692 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13693 	if (!hdr) {
13694 		nlmsg_free(msg);
13695 		return;
13696 	}
13697 
13698 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13699 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13700 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf))
13701 		goto nla_put_failure;
13702 
13703 	if (uapsd_queues >= 0) {
13704 		struct nlattr *nla_wmm =
13705 			nla_nest_start(msg, NL80211_ATTR_STA_WME);
13706 		if (!nla_wmm)
13707 			goto nla_put_failure;
13708 
13709 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
13710 			       uapsd_queues))
13711 			goto nla_put_failure;
13712 
13713 		nla_nest_end(msg, nla_wmm);
13714 	}
13715 
13716 	genlmsg_end(msg, hdr);
13717 
13718 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13719 				NL80211_MCGRP_MLME, gfp);
13720 	return;
13721 
13722  nla_put_failure:
13723 	genlmsg_cancel(msg, hdr);
13724 	nlmsg_free(msg);
13725 }
13726 
13727 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
13728 			  struct net_device *netdev, const u8 *buf,
13729 			  size_t len, gfp_t gfp)
13730 {
13731 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13732 				NL80211_CMD_AUTHENTICATE, gfp, -1);
13733 }
13734 
13735 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
13736 			   struct net_device *netdev, const u8 *buf,
13737 			   size_t len, gfp_t gfp, int uapsd_queues)
13738 {
13739 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13740 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
13741 }
13742 
13743 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
13744 			 struct net_device *netdev, const u8 *buf,
13745 			 size_t len, gfp_t gfp)
13746 {
13747 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13748 				NL80211_CMD_DEAUTHENTICATE, gfp, -1);
13749 }
13750 
13751 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
13752 			   struct net_device *netdev, const u8 *buf,
13753 			   size_t len, gfp_t gfp)
13754 {
13755 	nl80211_send_mlme_event(rdev, netdev, buf, len,
13756 				NL80211_CMD_DISASSOCIATE, gfp, -1);
13757 }
13758 
13759 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
13760 				  size_t len)
13761 {
13762 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13763 	struct wiphy *wiphy = wdev->wiphy;
13764 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13765 	const struct ieee80211_mgmt *mgmt = (void *)buf;
13766 	u32 cmd;
13767 
13768 	if (WARN_ON(len < 2))
13769 		return;
13770 
13771 	if (ieee80211_is_deauth(mgmt->frame_control))
13772 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
13773 	else
13774 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
13775 
13776 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
13777 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
13778 }
13779 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
13780 
13781 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
13782 				      struct net_device *netdev, int cmd,
13783 				      const u8 *addr, gfp_t gfp)
13784 {
13785 	struct sk_buff *msg;
13786 	void *hdr;
13787 
13788 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13789 	if (!msg)
13790 		return;
13791 
13792 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13793 	if (!hdr) {
13794 		nlmsg_free(msg);
13795 		return;
13796 	}
13797 
13798 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13799 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13800 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13801 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
13802 		goto nla_put_failure;
13803 
13804 	genlmsg_end(msg, hdr);
13805 
13806 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13807 				NL80211_MCGRP_MLME, gfp);
13808 	return;
13809 
13810  nla_put_failure:
13811 	genlmsg_cancel(msg, hdr);
13812 	nlmsg_free(msg);
13813 }
13814 
13815 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
13816 			       struct net_device *netdev, const u8 *addr,
13817 			       gfp_t gfp)
13818 {
13819 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
13820 				  addr, gfp);
13821 }
13822 
13823 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
13824 				struct net_device *netdev, const u8 *addr,
13825 				gfp_t gfp)
13826 {
13827 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
13828 				  addr, gfp);
13829 }
13830 
13831 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
13832 				 struct net_device *netdev,
13833 				 struct cfg80211_connect_resp_params *cr,
13834 				 gfp_t gfp)
13835 {
13836 	struct sk_buff *msg;
13837 	void *hdr;
13838 
13839 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
13840 			cr->fils_kek_len + cr->pmk_len +
13841 			(cr->pmkid ? WLAN_PMKID_LEN : 0), gfp);
13842 	if (!msg)
13843 		return;
13844 
13845 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
13846 	if (!hdr) {
13847 		nlmsg_free(msg);
13848 		return;
13849 	}
13850 
13851 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13852 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13853 	    (cr->bssid &&
13854 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
13855 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
13856 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
13857 			cr->status) ||
13858 	    (cr->status < 0 &&
13859 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13860 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
13861 			  cr->timeout_reason))) ||
13862 	    (cr->req_ie &&
13863 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
13864 	    (cr->resp_ie &&
13865 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
13866 		     cr->resp_ie)) ||
13867 	    (cr->update_erp_next_seq_num &&
13868 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
13869 			 cr->fils_erp_next_seq_num)) ||
13870 	    (cr->status == WLAN_STATUS_SUCCESS &&
13871 	     ((cr->fils_kek &&
13872 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils_kek_len,
13873 		       cr->fils_kek)) ||
13874 	      (cr->pmk &&
13875 	       nla_put(msg, NL80211_ATTR_PMK, cr->pmk_len, cr->pmk)) ||
13876 	      (cr->pmkid &&
13877 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->pmkid)))))
13878 		goto nla_put_failure;
13879 
13880 	genlmsg_end(msg, hdr);
13881 
13882 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13883 				NL80211_MCGRP_MLME, gfp);
13884 	return;
13885 
13886  nla_put_failure:
13887 	genlmsg_cancel(msg, hdr);
13888 	nlmsg_free(msg);
13889 }
13890 
13891 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
13892 			 struct net_device *netdev,
13893 			 struct cfg80211_roam_info *info, gfp_t gfp)
13894 {
13895 	struct sk_buff *msg;
13896 	void *hdr;
13897 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
13898 
13899 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len, gfp);
13900 	if (!msg)
13901 		return;
13902 
13903 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
13904 	if (!hdr) {
13905 		nlmsg_free(msg);
13906 		return;
13907 	}
13908 
13909 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13910 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13911 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
13912 	    (info->req_ie &&
13913 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
13914 		     info->req_ie)) ||
13915 	    (info->resp_ie &&
13916 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
13917 		     info->resp_ie)))
13918 		goto nla_put_failure;
13919 
13920 	genlmsg_end(msg, hdr);
13921 
13922 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13923 				NL80211_MCGRP_MLME, gfp);
13924 	return;
13925 
13926  nla_put_failure:
13927 	genlmsg_cancel(msg, hdr);
13928 	nlmsg_free(msg);
13929 }
13930 
13931 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
13932 				  struct net_device *netdev, const u8 *bssid)
13933 {
13934 	struct sk_buff *msg;
13935 	void *hdr;
13936 
13937 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13938 	if (!msg)
13939 		return;
13940 
13941 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
13942 	if (!hdr) {
13943 		nlmsg_free(msg);
13944 		return;
13945 	}
13946 
13947 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
13948 		goto nla_put_failure;
13949 
13950 	genlmsg_end(msg, hdr);
13951 
13952 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13953 				NL80211_MCGRP_MLME, GFP_KERNEL);
13954 	return;
13955 
13956  nla_put_failure:
13957 	genlmsg_cancel(msg, hdr);
13958 	nlmsg_free(msg);
13959 }
13960 
13961 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
13962 			       struct net_device *netdev, u16 reason,
13963 			       const u8 *ie, size_t ie_len, bool from_ap)
13964 {
13965 	struct sk_buff *msg;
13966 	void *hdr;
13967 
13968 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
13969 	if (!msg)
13970 		return;
13971 
13972 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
13973 	if (!hdr) {
13974 		nlmsg_free(msg);
13975 		return;
13976 	}
13977 
13978 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13979 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13980 	    (reason &&
13981 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
13982 	    (from_ap &&
13983 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
13984 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
13985 		goto nla_put_failure;
13986 
13987 	genlmsg_end(msg, hdr);
13988 
13989 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13990 				NL80211_MCGRP_MLME, GFP_KERNEL);
13991 	return;
13992 
13993  nla_put_failure:
13994 	genlmsg_cancel(msg, hdr);
13995 	nlmsg_free(msg);
13996 }
13997 
13998 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
13999 			     struct net_device *netdev, const u8 *bssid,
14000 			     gfp_t gfp)
14001 {
14002 	struct sk_buff *msg;
14003 	void *hdr;
14004 
14005 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14006 	if (!msg)
14007 		return;
14008 
14009 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
14010 	if (!hdr) {
14011 		nlmsg_free(msg);
14012 		return;
14013 	}
14014 
14015 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14016 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14017 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14018 		goto nla_put_failure;
14019 
14020 	genlmsg_end(msg, hdr);
14021 
14022 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14023 				NL80211_MCGRP_MLME, gfp);
14024 	return;
14025 
14026  nla_put_failure:
14027 	genlmsg_cancel(msg, hdr);
14028 	nlmsg_free(msg);
14029 }
14030 
14031 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
14032 					const u8* ie, u8 ie_len, gfp_t gfp)
14033 {
14034 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14035 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14036 	struct sk_buff *msg;
14037 	void *hdr;
14038 
14039 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
14040 		return;
14041 
14042 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
14043 
14044 	msg = nlmsg_new(100 + ie_len, gfp);
14045 	if (!msg)
14046 		return;
14047 
14048 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
14049 	if (!hdr) {
14050 		nlmsg_free(msg);
14051 		return;
14052 	}
14053 
14054 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14055 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14056 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14057 	    (ie_len && ie &&
14058 	     nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
14059 		goto nla_put_failure;
14060 
14061 	genlmsg_end(msg, hdr);
14062 
14063 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14064 				NL80211_MCGRP_MLME, gfp);
14065 	return;
14066 
14067  nla_put_failure:
14068 	genlmsg_cancel(msg, hdr);
14069 	nlmsg_free(msg);
14070 }
14071 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
14072 
14073 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
14074 				 struct net_device *netdev, const u8 *addr,
14075 				 enum nl80211_key_type key_type, int key_id,
14076 				 const u8 *tsc, gfp_t gfp)
14077 {
14078 	struct sk_buff *msg;
14079 	void *hdr;
14080 
14081 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14082 	if (!msg)
14083 		return;
14084 
14085 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
14086 	if (!hdr) {
14087 		nlmsg_free(msg);
14088 		return;
14089 	}
14090 
14091 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14092 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14093 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
14094 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
14095 	    (key_id != -1 &&
14096 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
14097 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
14098 		goto nla_put_failure;
14099 
14100 	genlmsg_end(msg, hdr);
14101 
14102 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14103 				NL80211_MCGRP_MLME, gfp);
14104 	return;
14105 
14106  nla_put_failure:
14107 	genlmsg_cancel(msg, hdr);
14108 	nlmsg_free(msg);
14109 }
14110 
14111 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
14112 				    struct ieee80211_channel *channel_before,
14113 				    struct ieee80211_channel *channel_after)
14114 {
14115 	struct sk_buff *msg;
14116 	void *hdr;
14117 	struct nlattr *nl_freq;
14118 
14119 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
14120 	if (!msg)
14121 		return;
14122 
14123 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
14124 	if (!hdr) {
14125 		nlmsg_free(msg);
14126 		return;
14127 	}
14128 
14129 	/*
14130 	 * Since we are applying the beacon hint to a wiphy we know its
14131 	 * wiphy_idx is valid
14132 	 */
14133 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
14134 		goto nla_put_failure;
14135 
14136 	/* Before */
14137 	nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
14138 	if (!nl_freq)
14139 		goto nla_put_failure;
14140 	if (nl80211_msg_put_channel(msg, channel_before, false))
14141 		goto nla_put_failure;
14142 	nla_nest_end(msg, nl_freq);
14143 
14144 	/* After */
14145 	nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
14146 	if (!nl_freq)
14147 		goto nla_put_failure;
14148 	if (nl80211_msg_put_channel(msg, channel_after, false))
14149 		goto nla_put_failure;
14150 	nla_nest_end(msg, nl_freq);
14151 
14152 	genlmsg_end(msg, hdr);
14153 
14154 	rcu_read_lock();
14155 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14156 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14157 	rcu_read_unlock();
14158 
14159 	return;
14160 
14161 nla_put_failure:
14162 	genlmsg_cancel(msg, hdr);
14163 	nlmsg_free(msg);
14164 }
14165 
14166 static void nl80211_send_remain_on_chan_event(
14167 	int cmd, struct cfg80211_registered_device *rdev,
14168 	struct wireless_dev *wdev, u64 cookie,
14169 	struct ieee80211_channel *chan,
14170 	unsigned int duration, gfp_t gfp)
14171 {
14172 	struct sk_buff *msg;
14173 	void *hdr;
14174 
14175 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14176 	if (!msg)
14177 		return;
14178 
14179 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14180 	if (!hdr) {
14181 		nlmsg_free(msg);
14182 		return;
14183 	}
14184 
14185 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14186 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14187 					 wdev->netdev->ifindex)) ||
14188 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14189 			      NL80211_ATTR_PAD) ||
14190 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
14191 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
14192 			NL80211_CHAN_NO_HT) ||
14193 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14194 			      NL80211_ATTR_PAD))
14195 		goto nla_put_failure;
14196 
14197 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
14198 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
14199 		goto nla_put_failure;
14200 
14201 	genlmsg_end(msg, hdr);
14202 
14203 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14204 				NL80211_MCGRP_MLME, gfp);
14205 	return;
14206 
14207  nla_put_failure:
14208 	genlmsg_cancel(msg, hdr);
14209 	nlmsg_free(msg);
14210 }
14211 
14212 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
14213 			       struct ieee80211_channel *chan,
14214 			       unsigned int duration, gfp_t gfp)
14215 {
14216 	struct wiphy *wiphy = wdev->wiphy;
14217 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14218 
14219 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
14220 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
14221 					  rdev, wdev, cookie, chan,
14222 					  duration, gfp);
14223 }
14224 EXPORT_SYMBOL(cfg80211_ready_on_channel);
14225 
14226 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
14227 					struct ieee80211_channel *chan,
14228 					gfp_t gfp)
14229 {
14230 	struct wiphy *wiphy = wdev->wiphy;
14231 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14232 
14233 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
14234 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14235 					  rdev, wdev, cookie, chan, 0, gfp);
14236 }
14237 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
14238 
14239 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
14240 		      struct station_info *sinfo, gfp_t gfp)
14241 {
14242 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14243 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14244 	struct sk_buff *msg;
14245 
14246 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
14247 
14248 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14249 	if (!msg)
14250 		return;
14251 
14252 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
14253 				 rdev, dev, mac_addr, sinfo) < 0) {
14254 		nlmsg_free(msg);
14255 		return;
14256 	}
14257 
14258 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14259 				NL80211_MCGRP_MLME, gfp);
14260 }
14261 EXPORT_SYMBOL(cfg80211_new_sta);
14262 
14263 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
14264 			    struct station_info *sinfo, gfp_t gfp)
14265 {
14266 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14267 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14268 	struct sk_buff *msg;
14269 	struct station_info empty_sinfo = {};
14270 
14271 	if (!sinfo)
14272 		sinfo = &empty_sinfo;
14273 
14274 	trace_cfg80211_del_sta(dev, mac_addr);
14275 
14276 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14277 	if (!msg)
14278 		return;
14279 
14280 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
14281 				 rdev, dev, mac_addr, sinfo) < 0) {
14282 		nlmsg_free(msg);
14283 		return;
14284 	}
14285 
14286 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14287 				NL80211_MCGRP_MLME, gfp);
14288 }
14289 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
14290 
14291 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
14292 			  enum nl80211_connect_failed_reason reason,
14293 			  gfp_t gfp)
14294 {
14295 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14296 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14297 	struct sk_buff *msg;
14298 	void *hdr;
14299 
14300 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
14301 	if (!msg)
14302 		return;
14303 
14304 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
14305 	if (!hdr) {
14306 		nlmsg_free(msg);
14307 		return;
14308 	}
14309 
14310 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14311 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
14312 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
14313 		goto nla_put_failure;
14314 
14315 	genlmsg_end(msg, hdr);
14316 
14317 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14318 				NL80211_MCGRP_MLME, gfp);
14319 	return;
14320 
14321  nla_put_failure:
14322 	genlmsg_cancel(msg, hdr);
14323 	nlmsg_free(msg);
14324 }
14325 EXPORT_SYMBOL(cfg80211_conn_failed);
14326 
14327 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
14328 				       const u8 *addr, gfp_t gfp)
14329 {
14330 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14331 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14332 	struct sk_buff *msg;
14333 	void *hdr;
14334 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
14335 
14336 	if (!nlportid)
14337 		return false;
14338 
14339 	msg = nlmsg_new(100, gfp);
14340 	if (!msg)
14341 		return true;
14342 
14343 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14344 	if (!hdr) {
14345 		nlmsg_free(msg);
14346 		return true;
14347 	}
14348 
14349 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14350 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14351 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14352 		goto nla_put_failure;
14353 
14354 	genlmsg_end(msg, hdr);
14355 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14356 	return true;
14357 
14358  nla_put_failure:
14359 	genlmsg_cancel(msg, hdr);
14360 	nlmsg_free(msg);
14361 	return true;
14362 }
14363 
14364 bool cfg80211_rx_spurious_frame(struct net_device *dev,
14365 				const u8 *addr, gfp_t gfp)
14366 {
14367 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14368 	bool ret;
14369 
14370 	trace_cfg80211_rx_spurious_frame(dev, addr);
14371 
14372 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14373 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
14374 		trace_cfg80211_return_bool(false);
14375 		return false;
14376 	}
14377 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
14378 					 addr, gfp);
14379 	trace_cfg80211_return_bool(ret);
14380 	return ret;
14381 }
14382 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
14383 
14384 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
14385 					const u8 *addr, gfp_t gfp)
14386 {
14387 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14388 	bool ret;
14389 
14390 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
14391 
14392 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14393 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
14394 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
14395 		trace_cfg80211_return_bool(false);
14396 		return false;
14397 	}
14398 	ret = __nl80211_unexpected_frame(dev,
14399 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
14400 					 addr, gfp);
14401 	trace_cfg80211_return_bool(ret);
14402 	return ret;
14403 }
14404 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
14405 
14406 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
14407 		      struct wireless_dev *wdev, u32 nlportid,
14408 		      int freq, int sig_dbm,
14409 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
14410 {
14411 	struct net_device *netdev = wdev->netdev;
14412 	struct sk_buff *msg;
14413 	void *hdr;
14414 
14415 	msg = nlmsg_new(100 + len, gfp);
14416 	if (!msg)
14417 		return -ENOMEM;
14418 
14419 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14420 	if (!hdr) {
14421 		nlmsg_free(msg);
14422 		return -ENOMEM;
14423 	}
14424 
14425 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14426 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14427 					netdev->ifindex)) ||
14428 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14429 			      NL80211_ATTR_PAD) ||
14430 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
14431 	    (sig_dbm &&
14432 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14433 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14434 	    (flags &&
14435 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
14436 		goto nla_put_failure;
14437 
14438 	genlmsg_end(msg, hdr);
14439 
14440 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14441 
14442  nla_put_failure:
14443 	genlmsg_cancel(msg, hdr);
14444 	nlmsg_free(msg);
14445 	return -ENOBUFS;
14446 }
14447 
14448 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
14449 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
14450 {
14451 	struct wiphy *wiphy = wdev->wiphy;
14452 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14453 	struct net_device *netdev = wdev->netdev;
14454 	struct sk_buff *msg;
14455 	void *hdr;
14456 
14457 	trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
14458 
14459 	msg = nlmsg_new(100 + len, gfp);
14460 	if (!msg)
14461 		return;
14462 
14463 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
14464 	if (!hdr) {
14465 		nlmsg_free(msg);
14466 		return;
14467 	}
14468 
14469 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14470 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14471 				   netdev->ifindex)) ||
14472 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14473 			      NL80211_ATTR_PAD) ||
14474 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14475 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14476 			      NL80211_ATTR_PAD) ||
14477 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
14478 		goto nla_put_failure;
14479 
14480 	genlmsg_end(msg, hdr);
14481 
14482 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14483 				NL80211_MCGRP_MLME, gfp);
14484 	return;
14485 
14486  nla_put_failure:
14487 	genlmsg_cancel(msg, hdr);
14488 	nlmsg_free(msg);
14489 }
14490 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
14491 
14492 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
14493 					    const char *mac, gfp_t gfp)
14494 {
14495 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14496 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14497 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14498 	void **cb;
14499 
14500 	if (!msg)
14501 		return NULL;
14502 
14503 	cb = (void **)msg->cb;
14504 
14505 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
14506 	if (!cb[0]) {
14507 		nlmsg_free(msg);
14508 		return NULL;
14509 	}
14510 
14511 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14512 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14513 		goto nla_put_failure;
14514 
14515 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
14516 		goto nla_put_failure;
14517 
14518 	cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
14519 	if (!cb[1])
14520 		goto nla_put_failure;
14521 
14522 	cb[2] = rdev;
14523 
14524 	return msg;
14525  nla_put_failure:
14526 	nlmsg_free(msg);
14527 	return NULL;
14528 }
14529 
14530 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
14531 {
14532 	void **cb = (void **)msg->cb;
14533 	struct cfg80211_registered_device *rdev = cb[2];
14534 
14535 	nla_nest_end(msg, cb[1]);
14536 	genlmsg_end(msg, cb[0]);
14537 
14538 	memset(msg->cb, 0, sizeof(msg->cb));
14539 
14540 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14541 				NL80211_MCGRP_MLME, gfp);
14542 }
14543 
14544 void cfg80211_cqm_rssi_notify(struct net_device *dev,
14545 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
14546 			      s32 rssi_level, gfp_t gfp)
14547 {
14548 	struct sk_buff *msg;
14549 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14550 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14551 
14552 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
14553 
14554 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
14555 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
14556 		return;
14557 
14558 	if (wdev->cqm_config) {
14559 		wdev->cqm_config->last_rssi_event_value = rssi_level;
14560 
14561 		cfg80211_cqm_rssi_update(rdev, dev);
14562 
14563 		if (rssi_level == 0)
14564 			rssi_level = wdev->cqm_config->last_rssi_event_value;
14565 	}
14566 
14567 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14568 	if (!msg)
14569 		return;
14570 
14571 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
14572 			rssi_event))
14573 		goto nla_put_failure;
14574 
14575 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
14576 				      rssi_level))
14577 		goto nla_put_failure;
14578 
14579 	cfg80211_send_cqm(msg, gfp);
14580 
14581 	return;
14582 
14583  nla_put_failure:
14584 	nlmsg_free(msg);
14585 }
14586 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
14587 
14588 void cfg80211_cqm_txe_notify(struct net_device *dev,
14589 			     const u8 *peer, u32 num_packets,
14590 			     u32 rate, u32 intvl, gfp_t gfp)
14591 {
14592 	struct sk_buff *msg;
14593 
14594 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
14595 	if (!msg)
14596 		return;
14597 
14598 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
14599 		goto nla_put_failure;
14600 
14601 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
14602 		goto nla_put_failure;
14603 
14604 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
14605 		goto nla_put_failure;
14606 
14607 	cfg80211_send_cqm(msg, gfp);
14608 	return;
14609 
14610  nla_put_failure:
14611 	nlmsg_free(msg);
14612 }
14613 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
14614 
14615 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
14616 				 const u8 *peer, u32 num_packets, gfp_t gfp)
14617 {
14618 	struct sk_buff *msg;
14619 
14620 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
14621 
14622 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
14623 	if (!msg)
14624 		return;
14625 
14626 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
14627 		goto nla_put_failure;
14628 
14629 	cfg80211_send_cqm(msg, gfp);
14630 	return;
14631 
14632  nla_put_failure:
14633 	nlmsg_free(msg);
14634 }
14635 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
14636 
14637 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
14638 {
14639 	struct sk_buff *msg;
14640 
14641 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14642 	if (!msg)
14643 		return;
14644 
14645 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
14646 		goto nla_put_failure;
14647 
14648 	cfg80211_send_cqm(msg, gfp);
14649 	return;
14650 
14651  nla_put_failure:
14652 	nlmsg_free(msg);
14653 }
14654 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
14655 
14656 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
14657 				     struct net_device *netdev, const u8 *bssid,
14658 				     const u8 *replay_ctr, gfp_t gfp)
14659 {
14660 	struct sk_buff *msg;
14661 	struct nlattr *rekey_attr;
14662 	void *hdr;
14663 
14664 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14665 	if (!msg)
14666 		return;
14667 
14668 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
14669 	if (!hdr) {
14670 		nlmsg_free(msg);
14671 		return;
14672 	}
14673 
14674 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14675 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14676 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14677 		goto nla_put_failure;
14678 
14679 	rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
14680 	if (!rekey_attr)
14681 		goto nla_put_failure;
14682 
14683 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
14684 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
14685 		goto nla_put_failure;
14686 
14687 	nla_nest_end(msg, rekey_attr);
14688 
14689 	genlmsg_end(msg, hdr);
14690 
14691 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14692 				NL80211_MCGRP_MLME, gfp);
14693 	return;
14694 
14695  nla_put_failure:
14696 	genlmsg_cancel(msg, hdr);
14697 	nlmsg_free(msg);
14698 }
14699 
14700 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
14701 			       const u8 *replay_ctr, gfp_t gfp)
14702 {
14703 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14704 	struct wiphy *wiphy = wdev->wiphy;
14705 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14706 
14707 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
14708 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
14709 }
14710 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
14711 
14712 static void
14713 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
14714 			       struct net_device *netdev, int index,
14715 			       const u8 *bssid, bool preauth, gfp_t gfp)
14716 {
14717 	struct sk_buff *msg;
14718 	struct nlattr *attr;
14719 	void *hdr;
14720 
14721 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14722 	if (!msg)
14723 		return;
14724 
14725 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
14726 	if (!hdr) {
14727 		nlmsg_free(msg);
14728 		return;
14729 	}
14730 
14731 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14732 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14733 		goto nla_put_failure;
14734 
14735 	attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
14736 	if (!attr)
14737 		goto nla_put_failure;
14738 
14739 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
14740 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
14741 	    (preauth &&
14742 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
14743 		goto nla_put_failure;
14744 
14745 	nla_nest_end(msg, attr);
14746 
14747 	genlmsg_end(msg, hdr);
14748 
14749 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14750 				NL80211_MCGRP_MLME, gfp);
14751 	return;
14752 
14753  nla_put_failure:
14754 	genlmsg_cancel(msg, hdr);
14755 	nlmsg_free(msg);
14756 }
14757 
14758 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
14759 				     const u8 *bssid, bool preauth, gfp_t gfp)
14760 {
14761 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14762 	struct wiphy *wiphy = wdev->wiphy;
14763 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14764 
14765 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
14766 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
14767 }
14768 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
14769 
14770 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
14771 				     struct net_device *netdev,
14772 				     struct cfg80211_chan_def *chandef,
14773 				     gfp_t gfp,
14774 				     enum nl80211_commands notif,
14775 				     u8 count)
14776 {
14777 	struct sk_buff *msg;
14778 	void *hdr;
14779 
14780 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14781 	if (!msg)
14782 		return;
14783 
14784 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
14785 	if (!hdr) {
14786 		nlmsg_free(msg);
14787 		return;
14788 	}
14789 
14790 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14791 		goto nla_put_failure;
14792 
14793 	if (nl80211_send_chandef(msg, chandef))
14794 		goto nla_put_failure;
14795 
14796 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
14797 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
14798 			goto nla_put_failure;
14799 
14800 	genlmsg_end(msg, hdr);
14801 
14802 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14803 				NL80211_MCGRP_MLME, gfp);
14804 	return;
14805 
14806  nla_put_failure:
14807 	genlmsg_cancel(msg, hdr);
14808 	nlmsg_free(msg);
14809 }
14810 
14811 void cfg80211_ch_switch_notify(struct net_device *dev,
14812 			       struct cfg80211_chan_def *chandef)
14813 {
14814 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14815 	struct wiphy *wiphy = wdev->wiphy;
14816 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14817 
14818 	ASSERT_WDEV_LOCK(wdev);
14819 
14820 	trace_cfg80211_ch_switch_notify(dev, chandef);
14821 
14822 	wdev->chandef = *chandef;
14823 	wdev->preset_chandef = *chandef;
14824 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14825 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
14826 }
14827 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
14828 
14829 void cfg80211_ch_switch_started_notify(struct net_device *dev,
14830 				       struct cfg80211_chan_def *chandef,
14831 				       u8 count)
14832 {
14833 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14834 	struct wiphy *wiphy = wdev->wiphy;
14835 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14836 
14837 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
14838 
14839 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14840 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
14841 }
14842 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
14843 
14844 void
14845 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
14846 		     const struct cfg80211_chan_def *chandef,
14847 		     enum nl80211_radar_event event,
14848 		     struct net_device *netdev, gfp_t gfp)
14849 {
14850 	struct sk_buff *msg;
14851 	void *hdr;
14852 
14853 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14854 	if (!msg)
14855 		return;
14856 
14857 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
14858 	if (!hdr) {
14859 		nlmsg_free(msg);
14860 		return;
14861 	}
14862 
14863 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
14864 		goto nla_put_failure;
14865 
14866 	/* NOP and radar events don't need a netdev parameter */
14867 	if (netdev) {
14868 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
14869 
14870 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14871 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14872 				      NL80211_ATTR_PAD))
14873 			goto nla_put_failure;
14874 	}
14875 
14876 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
14877 		goto nla_put_failure;
14878 
14879 	if (nl80211_send_chandef(msg, chandef))
14880 		goto nla_put_failure;
14881 
14882 	genlmsg_end(msg, hdr);
14883 
14884 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14885 				NL80211_MCGRP_MLME, gfp);
14886 	return;
14887 
14888  nla_put_failure:
14889 	genlmsg_cancel(msg, hdr);
14890 	nlmsg_free(msg);
14891 }
14892 
14893 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
14894 			   u64 cookie, bool acked, gfp_t gfp)
14895 {
14896 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14897 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14898 	struct sk_buff *msg;
14899 	void *hdr;
14900 
14901 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
14902 
14903 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14904 
14905 	if (!msg)
14906 		return;
14907 
14908 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
14909 	if (!hdr) {
14910 		nlmsg_free(msg);
14911 		return;
14912 	}
14913 
14914 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14915 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14916 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14917 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14918 			      NL80211_ATTR_PAD) ||
14919 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
14920 		goto nla_put_failure;
14921 
14922 	genlmsg_end(msg, hdr);
14923 
14924 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14925 				NL80211_MCGRP_MLME, gfp);
14926 	return;
14927 
14928  nla_put_failure:
14929 	genlmsg_cancel(msg, hdr);
14930 	nlmsg_free(msg);
14931 }
14932 EXPORT_SYMBOL(cfg80211_probe_status);
14933 
14934 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
14935 				 const u8 *frame, size_t len,
14936 				 int freq, int sig_dbm)
14937 {
14938 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14939 	struct sk_buff *msg;
14940 	void *hdr;
14941 	struct cfg80211_beacon_registration *reg;
14942 
14943 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
14944 
14945 	spin_lock_bh(&rdev->beacon_registrations_lock);
14946 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14947 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
14948 		if (!msg) {
14949 			spin_unlock_bh(&rdev->beacon_registrations_lock);
14950 			return;
14951 		}
14952 
14953 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14954 		if (!hdr)
14955 			goto nla_put_failure;
14956 
14957 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14958 		    (freq &&
14959 		     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
14960 		    (sig_dbm &&
14961 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14962 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
14963 			goto nla_put_failure;
14964 
14965 		genlmsg_end(msg, hdr);
14966 
14967 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
14968 	}
14969 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14970 	return;
14971 
14972  nla_put_failure:
14973 	spin_unlock_bh(&rdev->beacon_registrations_lock);
14974 	if (hdr)
14975 		genlmsg_cancel(msg, hdr);
14976 	nlmsg_free(msg);
14977 }
14978 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
14979 
14980 #ifdef CONFIG_PM
14981 static int cfg80211_net_detect_results(struct sk_buff *msg,
14982 				       struct cfg80211_wowlan_wakeup *wakeup)
14983 {
14984 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
14985 	struct nlattr *nl_results, *nl_match, *nl_freqs;
14986 	int i, j;
14987 
14988 	nl_results = nla_nest_start(
14989 		msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
14990 	if (!nl_results)
14991 		return -EMSGSIZE;
14992 
14993 	for (i = 0; i < nd->n_matches; i++) {
14994 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
14995 
14996 		nl_match = nla_nest_start(msg, i);
14997 		if (!nl_match)
14998 			break;
14999 
15000 		/* The SSID attribute is optional in nl80211, but for
15001 		 * simplicity reasons it's always present in the
15002 		 * cfg80211 structure.  If a driver can't pass the
15003 		 * SSID, that needs to be changed.  A zero length SSID
15004 		 * is still a valid SSID (wildcard), so it cannot be
15005 		 * used for this purpose.
15006 		 */
15007 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
15008 			    match->ssid.ssid)) {
15009 			nla_nest_cancel(msg, nl_match);
15010 			goto out;
15011 		}
15012 
15013 		if (match->n_channels) {
15014 			nl_freqs = nla_nest_start(
15015 				msg, NL80211_ATTR_SCAN_FREQUENCIES);
15016 			if (!nl_freqs) {
15017 				nla_nest_cancel(msg, nl_match);
15018 				goto out;
15019 			}
15020 
15021 			for (j = 0; j < match->n_channels; j++) {
15022 				if (nla_put_u32(msg, j, match->channels[j])) {
15023 					nla_nest_cancel(msg, nl_freqs);
15024 					nla_nest_cancel(msg, nl_match);
15025 					goto out;
15026 				}
15027 			}
15028 
15029 			nla_nest_end(msg, nl_freqs);
15030 		}
15031 
15032 		nla_nest_end(msg, nl_match);
15033 	}
15034 
15035 out:
15036 	nla_nest_end(msg, nl_results);
15037 	return 0;
15038 }
15039 
15040 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
15041 				   struct cfg80211_wowlan_wakeup *wakeup,
15042 				   gfp_t gfp)
15043 {
15044 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15045 	struct sk_buff *msg;
15046 	void *hdr;
15047 	int size = 200;
15048 
15049 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
15050 
15051 	if (wakeup)
15052 		size += wakeup->packet_present_len;
15053 
15054 	msg = nlmsg_new(size, gfp);
15055 	if (!msg)
15056 		return;
15057 
15058 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
15059 	if (!hdr)
15060 		goto free_msg;
15061 
15062 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15063 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15064 			      NL80211_ATTR_PAD))
15065 		goto free_msg;
15066 
15067 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15068 					wdev->netdev->ifindex))
15069 		goto free_msg;
15070 
15071 	if (wakeup) {
15072 		struct nlattr *reasons;
15073 
15074 		reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
15075 		if (!reasons)
15076 			goto free_msg;
15077 
15078 		if (wakeup->disconnect &&
15079 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
15080 			goto free_msg;
15081 		if (wakeup->magic_pkt &&
15082 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
15083 			goto free_msg;
15084 		if (wakeup->gtk_rekey_failure &&
15085 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
15086 			goto free_msg;
15087 		if (wakeup->eap_identity_req &&
15088 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
15089 			goto free_msg;
15090 		if (wakeup->four_way_handshake &&
15091 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
15092 			goto free_msg;
15093 		if (wakeup->rfkill_release &&
15094 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
15095 			goto free_msg;
15096 
15097 		if (wakeup->pattern_idx >= 0 &&
15098 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
15099 				wakeup->pattern_idx))
15100 			goto free_msg;
15101 
15102 		if (wakeup->tcp_match &&
15103 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
15104 			goto free_msg;
15105 
15106 		if (wakeup->tcp_connlost &&
15107 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
15108 			goto free_msg;
15109 
15110 		if (wakeup->tcp_nomoretokens &&
15111 		    nla_put_flag(msg,
15112 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
15113 			goto free_msg;
15114 
15115 		if (wakeup->packet) {
15116 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
15117 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
15118 
15119 			if (!wakeup->packet_80211) {
15120 				pkt_attr =
15121 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
15122 				len_attr =
15123 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
15124 			}
15125 
15126 			if (wakeup->packet_len &&
15127 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
15128 				goto free_msg;
15129 
15130 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
15131 				    wakeup->packet))
15132 				goto free_msg;
15133 		}
15134 
15135 		if (wakeup->net_detect &&
15136 		    cfg80211_net_detect_results(msg, wakeup))
15137 				goto free_msg;
15138 
15139 		nla_nest_end(msg, reasons);
15140 	}
15141 
15142 	genlmsg_end(msg, hdr);
15143 
15144 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15145 				NL80211_MCGRP_MLME, gfp);
15146 	return;
15147 
15148  free_msg:
15149 	nlmsg_free(msg);
15150 }
15151 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
15152 #endif
15153 
15154 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
15155 				enum nl80211_tdls_operation oper,
15156 				u16 reason_code, gfp_t gfp)
15157 {
15158 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15159 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15160 	struct sk_buff *msg;
15161 	void *hdr;
15162 
15163 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
15164 					 reason_code);
15165 
15166 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15167 	if (!msg)
15168 		return;
15169 
15170 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
15171 	if (!hdr) {
15172 		nlmsg_free(msg);
15173 		return;
15174 	}
15175 
15176 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15177 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15178 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
15179 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
15180 	    (reason_code > 0 &&
15181 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
15182 		goto nla_put_failure;
15183 
15184 	genlmsg_end(msg, hdr);
15185 
15186 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15187 				NL80211_MCGRP_MLME, gfp);
15188 	return;
15189 
15190  nla_put_failure:
15191 	genlmsg_cancel(msg, hdr);
15192 	nlmsg_free(msg);
15193 }
15194 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
15195 
15196 static int nl80211_netlink_notify(struct notifier_block * nb,
15197 				  unsigned long state,
15198 				  void *_notify)
15199 {
15200 	struct netlink_notify *notify = _notify;
15201 	struct cfg80211_registered_device *rdev;
15202 	struct wireless_dev *wdev;
15203 	struct cfg80211_beacon_registration *reg, *tmp;
15204 
15205 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
15206 		return NOTIFY_DONE;
15207 
15208 	rcu_read_lock();
15209 
15210 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
15211 		struct cfg80211_sched_scan_request *sched_scan_req;
15212 
15213 		list_for_each_entry_rcu(sched_scan_req,
15214 					&rdev->sched_scan_req_list,
15215 					list) {
15216 			if (sched_scan_req->owner_nlportid == notify->portid) {
15217 				sched_scan_req->nl_owner_dead = true;
15218 				schedule_work(&rdev->sched_scan_stop_wk);
15219 			}
15220 		}
15221 
15222 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
15223 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
15224 
15225 			if (wdev->owner_nlportid == notify->portid) {
15226 				wdev->nl_owner_dead = true;
15227 				schedule_work(&rdev->destroy_work);
15228 			} else if (wdev->conn_owner_nlportid == notify->portid) {
15229 				schedule_work(&wdev->disconnect_wk);
15230 			}
15231 		}
15232 
15233 		spin_lock_bh(&rdev->beacon_registrations_lock);
15234 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
15235 					 list) {
15236 			if (reg->nlportid == notify->portid) {
15237 				list_del(&reg->list);
15238 				kfree(reg);
15239 				break;
15240 			}
15241 		}
15242 		spin_unlock_bh(&rdev->beacon_registrations_lock);
15243 	}
15244 
15245 	rcu_read_unlock();
15246 
15247 	/*
15248 	 * It is possible that the user space process that is controlling the
15249 	 * indoor setting disappeared, so notify the regulatory core.
15250 	 */
15251 	regulatory_netlink_notify(notify->portid);
15252 	return NOTIFY_OK;
15253 }
15254 
15255 static struct notifier_block nl80211_netlink_notifier = {
15256 	.notifier_call = nl80211_netlink_notify,
15257 };
15258 
15259 void cfg80211_ft_event(struct net_device *netdev,
15260 		       struct cfg80211_ft_event_params *ft_event)
15261 {
15262 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
15263 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15264 	struct sk_buff *msg;
15265 	void *hdr;
15266 
15267 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
15268 
15269 	if (!ft_event->target_ap)
15270 		return;
15271 
15272 	msg = nlmsg_new(100 + ft_event->ric_ies_len, GFP_KERNEL);
15273 	if (!msg)
15274 		return;
15275 
15276 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
15277 	if (!hdr)
15278 		goto out;
15279 
15280 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15281 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15282 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
15283 		goto out;
15284 
15285 	if (ft_event->ies &&
15286 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
15287 		goto out;
15288 	if (ft_event->ric_ies &&
15289 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
15290 		    ft_event->ric_ies))
15291 		goto out;
15292 
15293 	genlmsg_end(msg, hdr);
15294 
15295 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15296 				NL80211_MCGRP_MLME, GFP_KERNEL);
15297 	return;
15298  out:
15299 	nlmsg_free(msg);
15300 }
15301 EXPORT_SYMBOL(cfg80211_ft_event);
15302 
15303 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
15304 {
15305 	struct cfg80211_registered_device *rdev;
15306 	struct sk_buff *msg;
15307 	void *hdr;
15308 	u32 nlportid;
15309 
15310 	rdev = wiphy_to_rdev(wdev->wiphy);
15311 	if (!rdev->crit_proto_nlportid)
15312 		return;
15313 
15314 	nlportid = rdev->crit_proto_nlportid;
15315 	rdev->crit_proto_nlportid = 0;
15316 
15317 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15318 	if (!msg)
15319 		return;
15320 
15321 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
15322 	if (!hdr)
15323 		goto nla_put_failure;
15324 
15325 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15326 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15327 			      NL80211_ATTR_PAD))
15328 		goto nla_put_failure;
15329 
15330 	genlmsg_end(msg, hdr);
15331 
15332 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15333 	return;
15334 
15335  nla_put_failure:
15336 	if (hdr)
15337 		genlmsg_cancel(msg, hdr);
15338 	nlmsg_free(msg);
15339 }
15340 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
15341 
15342 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
15343 {
15344 	struct wiphy *wiphy = wdev->wiphy;
15345 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15346 	struct sk_buff *msg;
15347 	void *hdr;
15348 
15349 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15350 	if (!msg)
15351 		return;
15352 
15353 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
15354 	if (!hdr)
15355 		goto out;
15356 
15357 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15358 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
15359 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15360 			      NL80211_ATTR_PAD))
15361 		goto out;
15362 
15363 	genlmsg_end(msg, hdr);
15364 
15365 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
15366 				NL80211_MCGRP_MLME, GFP_KERNEL);
15367 	return;
15368  out:
15369 	nlmsg_free(msg);
15370 }
15371 
15372 /* initialisation/exit functions */
15373 
15374 int __init nl80211_init(void)
15375 {
15376 	int err;
15377 
15378 	err = genl_register_family(&nl80211_fam);
15379 	if (err)
15380 		return err;
15381 
15382 	err = netlink_register_notifier(&nl80211_netlink_notifier);
15383 	if (err)
15384 		goto err_out;
15385 
15386 	return 0;
15387  err_out:
15388 	genl_unregister_family(&nl80211_fam);
15389 	return err;
15390 }
15391 
15392 void nl80211_exit(void)
15393 {
15394 	netlink_unregister_notifier(&nl80211_netlink_notifier);
15395 	genl_unregister_family(&nl80211_fam);
15396 }
15397