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