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