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