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