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