1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010	Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017	Intel Deutschland GmbH
8  * Copyright (C) 2018-2019 Intel Corporation
9  */
10 
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <net/net_namespace.h>
24 #include <net/genetlink.h>
25 #include <net/cfg80211.h>
26 #include <net/sock.h>
27 #include <net/inet_connection_sock.h>
28 #include "core.h"
29 #include "nl80211.h"
30 #include "reg.h"
31 #include "rdev-ops.h"
32 
33 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
34 				   struct genl_info *info,
35 				   struct cfg80211_crypto_settings *settings,
36 				   int cipher_limit);
37 
38 /* the netlink family */
39 static struct genl_family nl80211_fam;
40 
41 /* multicast groups */
42 enum nl80211_multicast_groups {
43 	NL80211_MCGRP_CONFIG,
44 	NL80211_MCGRP_SCAN,
45 	NL80211_MCGRP_REGULATORY,
46 	NL80211_MCGRP_MLME,
47 	NL80211_MCGRP_VENDOR,
48 	NL80211_MCGRP_NAN,
49 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
50 };
51 
52 static const struct genl_multicast_group nl80211_mcgrps[] = {
53 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
54 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
55 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
56 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
57 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
58 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
59 #ifdef CONFIG_NL80211_TESTMODE
60 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
61 #endif
62 };
63 
64 /* returns ERR_PTR values */
65 static struct wireless_dev *
__cfg80211_wdev_from_attrs(struct net * netns,struct nlattr ** attrs)66 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
67 {
68 	struct cfg80211_registered_device *rdev;
69 	struct wireless_dev *result = NULL;
70 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
71 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
72 	u64 wdev_id;
73 	int wiphy_idx = -1;
74 	int ifidx = -1;
75 
76 	ASSERT_RTNL();
77 
78 	if (!have_ifidx && !have_wdev_id)
79 		return ERR_PTR(-EINVAL);
80 
81 	if (have_ifidx)
82 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
83 	if (have_wdev_id) {
84 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
85 		wiphy_idx = wdev_id >> 32;
86 	}
87 
88 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
89 		struct wireless_dev *wdev;
90 
91 		if (wiphy_net(&rdev->wiphy) != netns)
92 			continue;
93 
94 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
95 			continue;
96 
97 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
98 			if (have_ifidx && wdev->netdev &&
99 			    wdev->netdev->ifindex == ifidx) {
100 				result = wdev;
101 				break;
102 			}
103 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
104 				result = wdev;
105 				break;
106 			}
107 		}
108 
109 		if (result)
110 			break;
111 	}
112 
113 	if (result)
114 		return result;
115 	return ERR_PTR(-ENODEV);
116 }
117 
118 static struct cfg80211_registered_device *
__cfg80211_rdev_from_attrs(struct net * netns,struct nlattr ** attrs)119 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
120 {
121 	struct cfg80211_registered_device *rdev = NULL, *tmp;
122 	struct net_device *netdev;
123 
124 	ASSERT_RTNL();
125 
126 	if (!attrs[NL80211_ATTR_WIPHY] &&
127 	    !attrs[NL80211_ATTR_IFINDEX] &&
128 	    !attrs[NL80211_ATTR_WDEV])
129 		return ERR_PTR(-EINVAL);
130 
131 	if (attrs[NL80211_ATTR_WIPHY])
132 		rdev = cfg80211_rdev_by_wiphy_idx(
133 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
134 
135 	if (attrs[NL80211_ATTR_WDEV]) {
136 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
137 		struct wireless_dev *wdev;
138 		bool found = false;
139 
140 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
141 		if (tmp) {
142 			/* make sure wdev exists */
143 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
144 				if (wdev->identifier != (u32)wdev_id)
145 					continue;
146 				found = true;
147 				break;
148 			}
149 
150 			if (!found)
151 				tmp = NULL;
152 
153 			if (rdev && tmp != rdev)
154 				return ERR_PTR(-EINVAL);
155 			rdev = tmp;
156 		}
157 	}
158 
159 	if (attrs[NL80211_ATTR_IFINDEX]) {
160 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
161 
162 		netdev = __dev_get_by_index(netns, ifindex);
163 		if (netdev) {
164 			if (netdev->ieee80211_ptr)
165 				tmp = wiphy_to_rdev(
166 					netdev->ieee80211_ptr->wiphy);
167 			else
168 				tmp = NULL;
169 
170 			/* not wireless device -- return error */
171 			if (!tmp)
172 				return ERR_PTR(-EINVAL);
173 
174 			/* mismatch -- return error */
175 			if (rdev && tmp != rdev)
176 				return ERR_PTR(-EINVAL);
177 
178 			rdev = tmp;
179 		}
180 	}
181 
182 	if (!rdev)
183 		return ERR_PTR(-ENODEV);
184 
185 	if (netns != wiphy_net(&rdev->wiphy))
186 		return ERR_PTR(-ENODEV);
187 
188 	return rdev;
189 }
190 
191 /*
192  * This function returns a pointer to the driver
193  * that the genl_info item that is passed refers to.
194  *
195  * The result of this can be a PTR_ERR and hence must
196  * be checked with IS_ERR() for errors.
197  */
198 static struct cfg80211_registered_device *
cfg80211_get_dev_from_info(struct net * netns,struct genl_info * info)199 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
200 {
201 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
202 }
203 
validate_beacon_head(const struct nlattr * attr,struct netlink_ext_ack * extack)204 static int validate_beacon_head(const struct nlattr *attr,
205 				struct netlink_ext_ack *extack)
206 {
207 	const u8 *data = nla_data(attr);
208 	unsigned int len = nla_len(attr);
209 	const struct element *elem;
210 	const struct ieee80211_mgmt *mgmt = (void *)data;
211 	unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
212 					 u.beacon.variable);
213 
214 	if (len < fixedlen)
215 		goto err;
216 
217 	if (ieee80211_hdrlen(mgmt->frame_control) !=
218 	    offsetof(struct ieee80211_mgmt, u.beacon))
219 		goto err;
220 
221 	data += fixedlen;
222 	len -= fixedlen;
223 
224 	for_each_element(elem, data, len) {
225 		/* nothing */
226 	}
227 
228 	if (for_each_element_completed(elem, data, len))
229 		return 0;
230 
231 err:
232 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
233 	return -EINVAL;
234 }
235 
validate_ie_attr(const struct nlattr * attr,struct netlink_ext_ack * extack)236 static int validate_ie_attr(const struct nlattr *attr,
237 			    struct netlink_ext_ack *extack)
238 {
239 	const u8 *data = nla_data(attr);
240 	unsigned int len = nla_len(attr);
241 	const struct element *elem;
242 
243 	for_each_element(elem, data, len) {
244 		/* nothing */
245 	}
246 
247 	if (for_each_element_completed(elem, data, len))
248 		return 0;
249 
250 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
251 	return -EINVAL;
252 }
253 
254 /* policy for the attributes */
255 static const struct nla_policy
256 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
257 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
258 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
259 					.len = U8_MAX },
260 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
261 					     .len = U8_MAX },
262 };
263 
264 static const struct nla_policy
265 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
266 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
267 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
268 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
269 		NLA_POLICY_MAX(NLA_U8, 15),
270 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
271 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
272 		NLA_POLICY_MAX(NLA_U8, 15),
273 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
274 		NLA_POLICY_MAX(NLA_U8, 31),
275 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
276 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
277 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
278 };
279 
280 static const struct nla_policy
281 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
282 	[NL80211_PMSR_TYPE_FTM] =
283 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
284 };
285 
286 static const struct nla_policy
287 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
288 	[NL80211_PMSR_REQ_ATTR_DATA] =
289 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
290 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
291 };
292 
293 static const struct nla_policy
294 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
295 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
296 	/*
297 	 * we could specify this again to be the top-level policy,
298 	 * but that would open us up to recursion problems ...
299 	 */
300 	[NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
301 	[NL80211_PMSR_PEER_ATTR_REQ] =
302 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
303 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
304 };
305 
306 static const struct nla_policy
307 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
308 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
309 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
310 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
311 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
312 	[NL80211_PMSR_ATTR_PEERS] =
313 		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
314 };
315 
316 static const struct nla_policy
317 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
318 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
319 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
320 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
321 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
322 };
323 
324 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
325 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
326 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
327 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
328 				      .len = 20-1 },
329 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
330 
331 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
332 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
333 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
334 						NL80211_EDMG_CHANNELS_MIN,
335 						NL80211_EDMG_CHANNELS_MAX),
336 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
337 						NL80211_EDMG_BW_CONFIG_MIN,
338 						NL80211_EDMG_BW_CONFIG_MAX),
339 
340 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
341 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
342 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
343 
344 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
345 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
346 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
347 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
348 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
349 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
350 
351 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
352 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
353 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
354 
355 	[NL80211_ATTR_MAC] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
356 	[NL80211_ATTR_PREV_BSSID] = {
357 		.type = NLA_EXACT_LEN_WARN,
358 		.len = ETH_ALEN
359 	},
360 
361 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
362 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
363 				    .len = WLAN_MAX_KEY_LEN },
364 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 5),
365 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
366 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
367 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
368 	[NL80211_ATTR_KEY_TYPE] =
369 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
370 
371 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
372 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
373 	[NL80211_ATTR_BEACON_HEAD] =
374 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
375 				       IEEE80211_MAX_DATA_LEN),
376 	[NL80211_ATTR_BEACON_TAIL] =
377 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
378 				       IEEE80211_MAX_DATA_LEN),
379 	[NL80211_ATTR_STA_AID] =
380 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
381 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
382 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
383 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
384 					       .len = NL80211_MAX_SUPP_RATES },
385 	[NL80211_ATTR_STA_PLINK_ACTION] =
386 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
387 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
388 		NLA_POLICY_RANGE(NLA_U8,
389 				 NL80211_TX_POWER_AUTOMATIC,
390 				 NL80211_TX_POWER_FIXED),
391 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
392 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
393 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
394 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
395 				   .len = IEEE80211_MAX_MESH_ID_LEN },
396 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
397 
398 	[NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
399 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
400 
401 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
402 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
403 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
404 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
405 					   .len = NL80211_MAX_SUPP_RATES },
406 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
407 
408 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
409 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
410 
411 	[NL80211_ATTR_HT_CAPABILITY] = {
412 		.type = NLA_EXACT_LEN_WARN,
413 		.len = NL80211_HT_CAPABILITY_LEN
414 	},
415 
416 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
417 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
418 						   validate_ie_attr,
419 						   IEEE80211_MAX_DATA_LEN),
420 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
421 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
422 
423 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
424 				.len = IEEE80211_MAX_SSID_LEN },
425 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
426 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
427 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
428 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
429 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
430 						  NL80211_MFP_NO,
431 						  NL80211_MFP_OPTIONAL),
432 	[NL80211_ATTR_STA_FLAGS2] = {
433 		.len = sizeof(struct nl80211_sta_flag_update),
434 	},
435 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
436 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
437 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
438 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
439 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
440 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
441 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
442 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
443 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
444 	[NL80211_ATTR_PMKID] = {
445 		.type = NLA_EXACT_LEN_WARN,
446 		.len = WLAN_PMKID_LEN
447 	},
448 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
449 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
450 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
451 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
452 				 .len = IEEE80211_MAX_DATA_LEN },
453 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
454 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
455 						   NL80211_PS_DISABLED,
456 						   NL80211_PS_ENABLED),
457 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
458 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
459 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
460 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
461 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
462 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
463 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
464 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
465 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
466 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
467 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
468 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
469 	[NL80211_ATTR_STA_PLINK_STATE] =
470 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
471 	[NL80211_ATTR_MESH_PEER_AID] =
472 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
473 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
474 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
475 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
476 	[NL80211_ATTR_HIDDEN_SSID] =
477 		NLA_POLICY_RANGE(NLA_U32,
478 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
479 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
480 	[NL80211_ATTR_IE_PROBE_RESP] =
481 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
482 				       IEEE80211_MAX_DATA_LEN),
483 	[NL80211_ATTR_IE_ASSOC_RESP] =
484 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
485 				       IEEE80211_MAX_DATA_LEN),
486 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
487 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
488 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
489 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
490 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
491 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
492 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
493 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
494 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
495 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
496 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
497 				      .len = IEEE80211_MAX_DATA_LEN },
498 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
499 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
500 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
501 		.len = NL80211_HT_CAPABILITY_LEN
502 	},
503 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
504 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
505 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
506 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
507 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
508 	[NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
509 	[NL80211_ATTR_VHT_CAPABILITY] = {
510 		.type = NLA_EXACT_LEN_WARN,
511 		.len = NL80211_VHT_CAPABILITY_LEN
512 	},
513 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
514 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
515 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
516 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
517 		NLA_POLICY_RANGE(NLA_U32,
518 				 NL80211_MESH_POWER_UNKNOWN + 1,
519 				 NL80211_MESH_POWER_MAX),
520 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
521 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
522 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
523 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
524 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
525 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
526 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
527 		.len = NL80211_VHT_CAPABILITY_LEN,
528 	},
529 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
530 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
531 				  .len = IEEE80211_MAX_DATA_LEN },
532 	[NL80211_ATTR_PEER_AID] =
533 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
534 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
535 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
536 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
537 	[NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
538 	[NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
539 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
540 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
541 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
542 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
543 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
544 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
545 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
546 	[NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
547 				   .len = IEEE80211_QOS_MAP_LEN_MAX },
548 	[NL80211_ATTR_MAC_HINT] = {
549 		.type = NLA_EXACT_LEN_WARN,
550 		.len = ETH_ALEN
551 	},
552 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
553 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
554 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
555 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
556 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
557 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
558 	[NL80211_ATTR_USER_PRIO] =
559 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
560 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
561 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
562 	[NL80211_ATTR_MAC_MASK] = {
563 		.type = NLA_EXACT_LEN_WARN,
564 		.len = ETH_ALEN
565 	},
566 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
567 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
568 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
569 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
570 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
572 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
573 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
574 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
575 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
576 	},
577 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
578 		.type = NLA_EXACT_LEN_WARN,
579 		.len = ETH_ALEN
580 	},
581 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
582 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
583 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
584 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
585 				    .len = FILS_MAX_KEK_LEN },
586 	[NL80211_ATTR_FILS_NONCES] = {
587 		.type = NLA_EXACT_LEN_WARN,
588 		.len = 2 * FILS_NONCE_LEN
589 	},
590 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
591 	[NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
592 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
593 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
594 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
595 	},
596 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
597 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
598 					     .len = FILS_ERP_MAX_USERNAME_LEN },
599 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
600 					  .len = FILS_ERP_MAX_REALM_LEN },
601 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
602 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
603 					.len = FILS_ERP_MAX_RRK_LEN },
604 	[NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
605 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
606 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
607 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
608 
609 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
610 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
611 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
612 	[NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
613 					 .len = NL80211_HE_MAX_CAPABILITY_LEN },
614 
615 	[NL80211_ATTR_FTM_RESPONDER] = {
616 		.type = NLA_NESTED,
617 		.validation_data = nl80211_ftm_responder_policy,
618 	},
619 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
620 	[NL80211_ATTR_PEER_MEASUREMENTS] =
621 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
622 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
623 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
624 					.len = SAE_PASSWORD_MAX_LEN },
625 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
626 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
627 };
628 
629 /* policy for the key attributes */
630 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
631 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
632 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
633 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
634 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
635 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
636 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
637 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
638 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
639 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
640 };
641 
642 /* policy for the key default flags */
643 static const struct nla_policy
644 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
645 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
646 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
647 };
648 
649 #ifdef CONFIG_PM
650 /* policy for WoWLAN attributes */
651 static const struct nla_policy
652 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
653 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
654 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
655 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
656 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
657 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
658 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
659 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
660 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
661 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
662 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
663 };
664 
665 static const struct nla_policy
666 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
667 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
668 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
669 	[NL80211_WOWLAN_TCP_DST_MAC] = {
670 		.type = NLA_EXACT_LEN_WARN,
671 		.len = ETH_ALEN
672 	},
673 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
674 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
675 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
676 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
677 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
678 	},
679 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
680 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
681 	},
682 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
683 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
684 	[NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
685 };
686 #endif /* CONFIG_PM */
687 
688 /* policy for coalesce rule attributes */
689 static const struct nla_policy
690 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
691 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
692 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
693 		NLA_POLICY_RANGE(NLA_U32,
694 				 NL80211_COALESCE_CONDITION_MATCH,
695 				 NL80211_COALESCE_CONDITION_NO_MATCH),
696 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
697 };
698 
699 /* policy for GTK rekey offload attributes */
700 static const struct nla_policy
701 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
702 	[NL80211_REKEY_DATA_KEK] = {
703 		.type = NLA_EXACT_LEN_WARN,
704 		.len = NL80211_KEK_LEN,
705 	},
706 	[NL80211_REKEY_DATA_KCK] = {
707 		.type = NLA_EXACT_LEN_WARN,
708 		.len = NL80211_KCK_LEN,
709 	},
710 	[NL80211_REKEY_DATA_REPLAY_CTR] = {
711 		.type = NLA_EXACT_LEN_WARN,
712 		.len = NL80211_REPLAY_CTR_LEN
713 	},
714 };
715 
716 static const struct nla_policy
717 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
718 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
719 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
720 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
721 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
722 };
723 
724 static const struct nla_policy
725 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
726 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
727 						 .len = IEEE80211_MAX_SSID_LEN },
728 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = {
729 		.type = NLA_EXACT_LEN_WARN,
730 		.len = ETH_ALEN
731 	},
732 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
733 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
734 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
735 };
736 
737 static const struct nla_policy
738 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
739 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
740 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
741 };
742 
743 static const struct nla_policy
744 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
745 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
746 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
747 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
748 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
749 	},
750 };
751 
752 /* policy for NAN function attributes */
753 static const struct nla_policy
754 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
755 	[NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
756 	[NL80211_NAN_FUNC_SERVICE_ID] = {
757 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
758 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
759 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
760 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
761 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
762 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
763 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = {
764 		.type = NLA_EXACT_LEN_WARN,
765 		.len = ETH_ALEN
766 	},
767 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
768 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
769 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
770 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
771 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
772 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
773 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
774 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
775 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
776 };
777 
778 /* policy for Service Response Filter attributes */
779 static const struct nla_policy
780 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
781 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
782 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
783 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
784 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
785 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
786 };
787 
788 /* policy for packet pattern attributes */
789 static const struct nla_policy
790 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
791 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
792 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
793 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
794 };
795 
nl80211_prepare_wdev_dump(struct netlink_callback * cb,struct cfg80211_registered_device ** rdev,struct wireless_dev ** wdev)796 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
797 			      struct cfg80211_registered_device **rdev,
798 			      struct wireless_dev **wdev)
799 {
800 	int err;
801 
802 	if (!cb->args[0]) {
803 		struct nlattr **attrbuf;
804 
805 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
806 				  GFP_KERNEL);
807 		if (!attrbuf)
808 			return -ENOMEM;
809 
810 		err = nlmsg_parse_deprecated(cb->nlh,
811 					     GENL_HDRLEN + nl80211_fam.hdrsize,
812 					     attrbuf, nl80211_fam.maxattr,
813 					     nl80211_policy, NULL);
814 		if (err) {
815 			kfree(attrbuf);
816 			return err;
817 		}
818 
819 		*wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
820 						   attrbuf);
821 		kfree(attrbuf);
822 		if (IS_ERR(*wdev))
823 			return PTR_ERR(*wdev);
824 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
825 		/* 0 is the first index - add 1 to parse only once */
826 		cb->args[0] = (*rdev)->wiphy_idx + 1;
827 		cb->args[1] = (*wdev)->identifier;
828 	} else {
829 		/* subtract the 1 again here */
830 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
831 		struct wireless_dev *tmp;
832 
833 		if (!wiphy)
834 			return -ENODEV;
835 		*rdev = wiphy_to_rdev(wiphy);
836 		*wdev = NULL;
837 
838 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
839 			if (tmp->identifier == cb->args[1]) {
840 				*wdev = tmp;
841 				break;
842 			}
843 		}
844 
845 		if (!*wdev)
846 			return -ENODEV;
847 	}
848 
849 	return 0;
850 }
851 
852 /* message building helper */
nl80211hdr_put(struct sk_buff * skb,u32 portid,u32 seq,int flags,u8 cmd)853 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
854 		     int flags, u8 cmd)
855 {
856 	/* since there is no private header just add the generic one */
857 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
858 }
859 
nl80211_msg_put_wmm_rules(struct sk_buff * msg,const struct ieee80211_reg_rule * rule)860 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
861 				     const struct ieee80211_reg_rule *rule)
862 {
863 	int j;
864 	struct nlattr *nl_wmm_rules =
865 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
866 
867 	if (!nl_wmm_rules)
868 		goto nla_put_failure;
869 
870 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
871 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
872 
873 		if (!nl_wmm_rule)
874 			goto nla_put_failure;
875 
876 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
877 				rule->wmm_rule.client[j].cw_min) ||
878 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
879 				rule->wmm_rule.client[j].cw_max) ||
880 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
881 			       rule->wmm_rule.client[j].aifsn) ||
882 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
883 			        rule->wmm_rule.client[j].cot))
884 			goto nla_put_failure;
885 
886 		nla_nest_end(msg, nl_wmm_rule);
887 	}
888 	nla_nest_end(msg, nl_wmm_rules);
889 
890 	return 0;
891 
892 nla_put_failure:
893 	return -ENOBUFS;
894 }
895 
nl80211_msg_put_channel(struct sk_buff * msg,struct wiphy * wiphy,struct ieee80211_channel * chan,bool large)896 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
897 				   struct ieee80211_channel *chan,
898 				   bool large)
899 {
900 	/* Some channels must be completely excluded from the
901 	 * list to protect old user-space tools from breaking
902 	 */
903 	if (!large && chan->flags &
904 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
905 		return 0;
906 
907 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
908 			chan->center_freq))
909 		goto nla_put_failure;
910 
911 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
912 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
913 		goto nla_put_failure;
914 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
915 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
916 			goto nla_put_failure;
917 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
918 			goto nla_put_failure;
919 	}
920 	if (chan->flags & IEEE80211_CHAN_RADAR) {
921 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
922 			goto nla_put_failure;
923 		if (large) {
924 			u32 time;
925 
926 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
927 
928 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
929 					chan->dfs_state))
930 				goto nla_put_failure;
931 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
932 					time))
933 				goto nla_put_failure;
934 			if (nla_put_u32(msg,
935 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
936 					chan->dfs_cac_ms))
937 				goto nla_put_failure;
938 		}
939 	}
940 
941 	if (large) {
942 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
943 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
944 			goto nla_put_failure;
945 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
946 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
947 			goto nla_put_failure;
948 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
949 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
950 			goto nla_put_failure;
951 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
952 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
953 			goto nla_put_failure;
954 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
955 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
956 			goto nla_put_failure;
957 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
958 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
959 			goto nla_put_failure;
960 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
961 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
962 			goto nla_put_failure;
963 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
964 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
965 			goto nla_put_failure;
966 	}
967 
968 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
969 			DBM_TO_MBM(chan->max_power)))
970 		goto nla_put_failure;
971 
972 	if (large) {
973 		const struct ieee80211_reg_rule *rule =
974 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
975 
976 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
977 			if (nl80211_msg_put_wmm_rules(msg, rule))
978 				goto nla_put_failure;
979 		}
980 	}
981 
982 	return 0;
983 
984  nla_put_failure:
985 	return -ENOBUFS;
986 }
987 
nl80211_put_txq_stats(struct sk_buff * msg,struct cfg80211_txq_stats * txqstats,int attrtype)988 static bool nl80211_put_txq_stats(struct sk_buff *msg,
989 				  struct cfg80211_txq_stats *txqstats,
990 				  int attrtype)
991 {
992 	struct nlattr *txqattr;
993 
994 #define PUT_TXQVAL_U32(attr, memb) do {					  \
995 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
996 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
997 		return false;						  \
998 	} while (0)
999 
1000 	txqattr = nla_nest_start_noflag(msg, attrtype);
1001 	if (!txqattr)
1002 		return false;
1003 
1004 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1005 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1006 	PUT_TXQVAL_U32(FLOWS, flows);
1007 	PUT_TXQVAL_U32(DROPS, drops);
1008 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1009 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1010 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1011 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1012 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1013 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1014 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1015 	nla_nest_end(msg, txqattr);
1016 
1017 #undef PUT_TXQVAL_U32
1018 	return true;
1019 }
1020 
1021 /* netlink command implementations */
1022 
1023 struct key_parse {
1024 	struct key_params p;
1025 	int idx;
1026 	int type;
1027 	bool def, defmgmt;
1028 	bool def_uni, def_multi;
1029 };
1030 
nl80211_parse_key_new(struct genl_info * info,struct nlattr * key,struct key_parse * k)1031 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1032 				 struct key_parse *k)
1033 {
1034 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1035 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1036 					      nl80211_key_policy,
1037 					      info->extack);
1038 	if (err)
1039 		return err;
1040 
1041 	k->def = !!tb[NL80211_KEY_DEFAULT];
1042 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1043 
1044 	if (k->def) {
1045 		k->def_uni = true;
1046 		k->def_multi = true;
1047 	}
1048 	if (k->defmgmt)
1049 		k->def_multi = true;
1050 
1051 	if (tb[NL80211_KEY_IDX])
1052 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1053 
1054 	if (tb[NL80211_KEY_DATA]) {
1055 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1056 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1057 	}
1058 
1059 	if (tb[NL80211_KEY_SEQ]) {
1060 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1061 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1062 	}
1063 
1064 	if (tb[NL80211_KEY_CIPHER])
1065 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1066 
1067 	if (tb[NL80211_KEY_TYPE])
1068 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1069 
1070 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1071 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1072 
1073 		err = nla_parse_nested_deprecated(kdt,
1074 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1075 						  tb[NL80211_KEY_DEFAULT_TYPES],
1076 						  nl80211_key_default_policy,
1077 						  info->extack);
1078 		if (err)
1079 			return err;
1080 
1081 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1082 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1083 	}
1084 
1085 	if (tb[NL80211_KEY_MODE])
1086 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1087 
1088 	return 0;
1089 }
1090 
nl80211_parse_key_old(struct genl_info * info,struct key_parse * k)1091 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1092 {
1093 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1094 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1095 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1096 	}
1097 
1098 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1099 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1100 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1101 	}
1102 
1103 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1104 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1105 
1106 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1107 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1108 
1109 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1110 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1111 
1112 	if (k->def) {
1113 		k->def_uni = true;
1114 		k->def_multi = true;
1115 	}
1116 	if (k->defmgmt)
1117 		k->def_multi = true;
1118 
1119 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1120 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1121 
1122 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1123 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1124 		int err = nla_parse_nested_deprecated(kdt,
1125 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1126 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1127 						      nl80211_key_default_policy,
1128 						      info->extack);
1129 		if (err)
1130 			return err;
1131 
1132 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1133 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1134 	}
1135 
1136 	return 0;
1137 }
1138 
nl80211_parse_key(struct genl_info * info,struct key_parse * k)1139 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1140 {
1141 	int err;
1142 
1143 	memset(k, 0, sizeof(*k));
1144 	k->idx = -1;
1145 	k->type = -1;
1146 
1147 	if (info->attrs[NL80211_ATTR_KEY])
1148 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1149 	else
1150 		err = nl80211_parse_key_old(info, k);
1151 
1152 	if (err)
1153 		return err;
1154 
1155 	if (k->def && k->defmgmt) {
1156 		GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
1157 		return -EINVAL;
1158 	}
1159 
1160 	if (k->defmgmt) {
1161 		if (k->def_uni || !k->def_multi) {
1162 			GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1163 			return -EINVAL;
1164 		}
1165 	}
1166 
1167 	if (k->idx != -1) {
1168 		if (k->defmgmt) {
1169 			if (k->idx < 4 || k->idx > 5) {
1170 				GENL_SET_ERR_MSG(info,
1171 						 "defmgmt key idx not 4 or 5");
1172 				return -EINVAL;
1173 			}
1174 		} else if (k->def) {
1175 			if (k->idx < 0 || k->idx > 3) {
1176 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1177 				return -EINVAL;
1178 			}
1179 		} else {
1180 			if (k->idx < 0 || k->idx > 5) {
1181 				GENL_SET_ERR_MSG(info, "key idx not 0-5");
1182 				return -EINVAL;
1183 			}
1184 		}
1185 	}
1186 
1187 	return 0;
1188 }
1189 
1190 static struct cfg80211_cached_keys *
nl80211_parse_connkeys(struct cfg80211_registered_device * rdev,struct genl_info * info,bool * no_ht)1191 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1192 		       struct genl_info *info, bool *no_ht)
1193 {
1194 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1195 	struct key_parse parse;
1196 	struct nlattr *key;
1197 	struct cfg80211_cached_keys *result;
1198 	int rem, err, def = 0;
1199 	bool have_key = false;
1200 
1201 	nla_for_each_nested(key, keys, rem) {
1202 		have_key = true;
1203 		break;
1204 	}
1205 
1206 	if (!have_key)
1207 		return NULL;
1208 
1209 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1210 	if (!result)
1211 		return ERR_PTR(-ENOMEM);
1212 
1213 	result->def = -1;
1214 
1215 	nla_for_each_nested(key, keys, rem) {
1216 		memset(&parse, 0, sizeof(parse));
1217 		parse.idx = -1;
1218 
1219 		err = nl80211_parse_key_new(info, key, &parse);
1220 		if (err)
1221 			goto error;
1222 		err = -EINVAL;
1223 		if (!parse.p.key)
1224 			goto error;
1225 		if (parse.idx < 0 || parse.idx > 3) {
1226 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1227 			goto error;
1228 		}
1229 		if (parse.def) {
1230 			if (def) {
1231 				GENL_SET_ERR_MSG(info,
1232 						 "only one key can be default");
1233 				goto error;
1234 			}
1235 			def = 1;
1236 			result->def = parse.idx;
1237 			if (!parse.def_uni || !parse.def_multi)
1238 				goto error;
1239 		} else if (parse.defmgmt)
1240 			goto error;
1241 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1242 						     parse.idx, false, NULL);
1243 		if (err)
1244 			goto error;
1245 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1246 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1247 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1248 			err = -EINVAL;
1249 			goto error;
1250 		}
1251 		result->params[parse.idx].cipher = parse.p.cipher;
1252 		result->params[parse.idx].key_len = parse.p.key_len;
1253 		result->params[parse.idx].key = result->data[parse.idx];
1254 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1255 
1256 		/* must be WEP key if we got here */
1257 		if (no_ht)
1258 			*no_ht = true;
1259 	}
1260 
1261 	if (result->def < 0) {
1262 		err = -EINVAL;
1263 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1264 		goto error;
1265 	}
1266 
1267 	return result;
1268  error:
1269 	kfree(result);
1270 	return ERR_PTR(err);
1271 }
1272 
nl80211_key_allowed(struct wireless_dev * wdev)1273 static int nl80211_key_allowed(struct wireless_dev *wdev)
1274 {
1275 	ASSERT_WDEV_LOCK(wdev);
1276 
1277 	switch (wdev->iftype) {
1278 	case NL80211_IFTYPE_AP:
1279 	case NL80211_IFTYPE_AP_VLAN:
1280 	case NL80211_IFTYPE_P2P_GO:
1281 	case NL80211_IFTYPE_MESH_POINT:
1282 		break;
1283 	case NL80211_IFTYPE_ADHOC:
1284 	case NL80211_IFTYPE_STATION:
1285 	case NL80211_IFTYPE_P2P_CLIENT:
1286 		if (!wdev->current_bss)
1287 			return -ENOLINK;
1288 		break;
1289 	case NL80211_IFTYPE_UNSPECIFIED:
1290 	case NL80211_IFTYPE_OCB:
1291 	case NL80211_IFTYPE_MONITOR:
1292 	case NL80211_IFTYPE_NAN:
1293 	case NL80211_IFTYPE_P2P_DEVICE:
1294 	case NL80211_IFTYPE_WDS:
1295 	case NUM_NL80211_IFTYPES:
1296 		return -EINVAL;
1297 	}
1298 
1299 	return 0;
1300 }
1301 
nl80211_get_valid_chan(struct wiphy * wiphy,struct nlattr * tb)1302 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1303 							struct nlattr *tb)
1304 {
1305 	struct ieee80211_channel *chan;
1306 
1307 	if (tb == NULL)
1308 		return NULL;
1309 	chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1310 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1311 		return NULL;
1312 	return chan;
1313 }
1314 
nl80211_put_iftypes(struct sk_buff * msg,u32 attr,u16 ifmodes)1315 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1316 {
1317 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1318 	int i;
1319 
1320 	if (!nl_modes)
1321 		goto nla_put_failure;
1322 
1323 	i = 0;
1324 	while (ifmodes) {
1325 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1326 			goto nla_put_failure;
1327 		ifmodes >>= 1;
1328 		i++;
1329 	}
1330 
1331 	nla_nest_end(msg, nl_modes);
1332 	return 0;
1333 
1334 nla_put_failure:
1335 	return -ENOBUFS;
1336 }
1337 
nl80211_put_iface_combinations(struct wiphy * wiphy,struct sk_buff * msg,bool large)1338 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1339 					  struct sk_buff *msg,
1340 					  bool large)
1341 {
1342 	struct nlattr *nl_combis;
1343 	int i, j;
1344 
1345 	nl_combis = nla_nest_start_noflag(msg,
1346 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1347 	if (!nl_combis)
1348 		goto nla_put_failure;
1349 
1350 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1351 		const struct ieee80211_iface_combination *c;
1352 		struct nlattr *nl_combi, *nl_limits;
1353 
1354 		c = &wiphy->iface_combinations[i];
1355 
1356 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1357 		if (!nl_combi)
1358 			goto nla_put_failure;
1359 
1360 		nl_limits = nla_nest_start_noflag(msg,
1361 						  NL80211_IFACE_COMB_LIMITS);
1362 		if (!nl_limits)
1363 			goto nla_put_failure;
1364 
1365 		for (j = 0; j < c->n_limits; j++) {
1366 			struct nlattr *nl_limit;
1367 
1368 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1369 			if (!nl_limit)
1370 				goto nla_put_failure;
1371 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1372 					c->limits[j].max))
1373 				goto nla_put_failure;
1374 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1375 						c->limits[j].types))
1376 				goto nla_put_failure;
1377 			nla_nest_end(msg, nl_limit);
1378 		}
1379 
1380 		nla_nest_end(msg, nl_limits);
1381 
1382 		if (c->beacon_int_infra_match &&
1383 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1384 			goto nla_put_failure;
1385 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1386 				c->num_different_channels) ||
1387 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1388 				c->max_interfaces))
1389 			goto nla_put_failure;
1390 		if (large &&
1391 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1392 				c->radar_detect_widths) ||
1393 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1394 				c->radar_detect_regions)))
1395 			goto nla_put_failure;
1396 		if (c->beacon_int_min_gcd &&
1397 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1398 				c->beacon_int_min_gcd))
1399 			goto nla_put_failure;
1400 
1401 		nla_nest_end(msg, nl_combi);
1402 	}
1403 
1404 	nla_nest_end(msg, nl_combis);
1405 
1406 	return 0;
1407 nla_put_failure:
1408 	return -ENOBUFS;
1409 }
1410 
1411 #ifdef CONFIG_PM
nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1412 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1413 					struct sk_buff *msg)
1414 {
1415 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1416 	struct nlattr *nl_tcp;
1417 
1418 	if (!tcp)
1419 		return 0;
1420 
1421 	nl_tcp = nla_nest_start_noflag(msg,
1422 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1423 	if (!nl_tcp)
1424 		return -ENOBUFS;
1425 
1426 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1427 			tcp->data_payload_max))
1428 		return -ENOBUFS;
1429 
1430 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1431 			tcp->data_payload_max))
1432 		return -ENOBUFS;
1433 
1434 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1435 		return -ENOBUFS;
1436 
1437 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1438 				sizeof(*tcp->tok), tcp->tok))
1439 		return -ENOBUFS;
1440 
1441 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1442 			tcp->data_interval_max))
1443 		return -ENOBUFS;
1444 
1445 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1446 			tcp->wake_payload_max))
1447 		return -ENOBUFS;
1448 
1449 	nla_nest_end(msg, nl_tcp);
1450 	return 0;
1451 }
1452 
nl80211_send_wowlan(struct sk_buff * msg,struct cfg80211_registered_device * rdev,bool large)1453 static int nl80211_send_wowlan(struct sk_buff *msg,
1454 			       struct cfg80211_registered_device *rdev,
1455 			       bool large)
1456 {
1457 	struct nlattr *nl_wowlan;
1458 
1459 	if (!rdev->wiphy.wowlan)
1460 		return 0;
1461 
1462 	nl_wowlan = nla_nest_start_noflag(msg,
1463 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1464 	if (!nl_wowlan)
1465 		return -ENOBUFS;
1466 
1467 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1468 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1469 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1470 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1471 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1472 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1473 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1474 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1475 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1476 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1477 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1478 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1479 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1480 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1481 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1482 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1483 		return -ENOBUFS;
1484 
1485 	if (rdev->wiphy.wowlan->n_patterns) {
1486 		struct nl80211_pattern_support pat = {
1487 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1488 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1489 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1490 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1491 		};
1492 
1493 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1494 			    sizeof(pat), &pat))
1495 			return -ENOBUFS;
1496 	}
1497 
1498 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1499 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1500 			rdev->wiphy.wowlan->max_nd_match_sets))
1501 		return -ENOBUFS;
1502 
1503 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1504 		return -ENOBUFS;
1505 
1506 	nla_nest_end(msg, nl_wowlan);
1507 
1508 	return 0;
1509 }
1510 #endif
1511 
nl80211_send_coalesce(struct sk_buff * msg,struct cfg80211_registered_device * rdev)1512 static int nl80211_send_coalesce(struct sk_buff *msg,
1513 				 struct cfg80211_registered_device *rdev)
1514 {
1515 	struct nl80211_coalesce_rule_support rule;
1516 
1517 	if (!rdev->wiphy.coalesce)
1518 		return 0;
1519 
1520 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1521 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1522 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1523 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1524 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1525 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1526 
1527 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1528 		return -ENOBUFS;
1529 
1530 	return 0;
1531 }
1532 
1533 static int
nl80211_send_iftype_data(struct sk_buff * msg,const struct ieee80211_sband_iftype_data * iftdata)1534 nl80211_send_iftype_data(struct sk_buff *msg,
1535 			 const struct ieee80211_sband_iftype_data *iftdata)
1536 {
1537 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1538 
1539 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1540 				iftdata->types_mask))
1541 		return -ENOBUFS;
1542 
1543 	if (he_cap->has_he) {
1544 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1545 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1546 			    he_cap->he_cap_elem.mac_cap_info) ||
1547 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1548 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1549 			    he_cap->he_cap_elem.phy_cap_info) ||
1550 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1551 			    sizeof(he_cap->he_mcs_nss_supp),
1552 			    &he_cap->he_mcs_nss_supp) ||
1553 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1554 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1555 			return -ENOBUFS;
1556 	}
1557 
1558 	return 0;
1559 }
1560 
nl80211_send_band_rateinfo(struct sk_buff * msg,struct ieee80211_supported_band * sband)1561 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1562 				      struct ieee80211_supported_band *sband)
1563 {
1564 	struct nlattr *nl_rates, *nl_rate;
1565 	struct ieee80211_rate *rate;
1566 	int i;
1567 
1568 	/* add HT info */
1569 	if (sband->ht_cap.ht_supported &&
1570 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1571 		     sizeof(sband->ht_cap.mcs),
1572 		     &sband->ht_cap.mcs) ||
1573 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1574 			 sband->ht_cap.cap) ||
1575 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1576 			sband->ht_cap.ampdu_factor) ||
1577 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1578 			sband->ht_cap.ampdu_density)))
1579 		return -ENOBUFS;
1580 
1581 	/* add VHT info */
1582 	if (sband->vht_cap.vht_supported &&
1583 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1584 		     sizeof(sband->vht_cap.vht_mcs),
1585 		     &sband->vht_cap.vht_mcs) ||
1586 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1587 			 sband->vht_cap.cap)))
1588 		return -ENOBUFS;
1589 
1590 	if (sband->n_iftype_data) {
1591 		struct nlattr *nl_iftype_data =
1592 			nla_nest_start_noflag(msg,
1593 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1594 		int err;
1595 
1596 		if (!nl_iftype_data)
1597 			return -ENOBUFS;
1598 
1599 		for (i = 0; i < sband->n_iftype_data; i++) {
1600 			struct nlattr *iftdata;
1601 
1602 			iftdata = nla_nest_start_noflag(msg, i + 1);
1603 			if (!iftdata)
1604 				return -ENOBUFS;
1605 
1606 			err = nl80211_send_iftype_data(msg,
1607 						       &sband->iftype_data[i]);
1608 			if (err)
1609 				return err;
1610 
1611 			nla_nest_end(msg, iftdata);
1612 		}
1613 
1614 		nla_nest_end(msg, nl_iftype_data);
1615 	}
1616 
1617 	/* add EDMG info */
1618 	if (sband->edmg_cap.channels &&
1619 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1620 		       sband->edmg_cap.channels) ||
1621 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1622 		       sband->edmg_cap.bw_config)))
1623 
1624 		return -ENOBUFS;
1625 
1626 	/* add bitrates */
1627 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1628 	if (!nl_rates)
1629 		return -ENOBUFS;
1630 
1631 	for (i = 0; i < sband->n_bitrates; i++) {
1632 		nl_rate = nla_nest_start_noflag(msg, i);
1633 		if (!nl_rate)
1634 			return -ENOBUFS;
1635 
1636 		rate = &sband->bitrates[i];
1637 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1638 				rate->bitrate))
1639 			return -ENOBUFS;
1640 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1641 		    nla_put_flag(msg,
1642 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1643 			return -ENOBUFS;
1644 
1645 		nla_nest_end(msg, nl_rate);
1646 	}
1647 
1648 	nla_nest_end(msg, nl_rates);
1649 
1650 	return 0;
1651 }
1652 
1653 static int
nl80211_send_mgmt_stypes(struct sk_buff * msg,const struct ieee80211_txrx_stypes * mgmt_stypes)1654 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1655 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1656 {
1657 	u16 stypes;
1658 	struct nlattr *nl_ftypes, *nl_ifs;
1659 	enum nl80211_iftype ift;
1660 	int i;
1661 
1662 	if (!mgmt_stypes)
1663 		return 0;
1664 
1665 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1666 	if (!nl_ifs)
1667 		return -ENOBUFS;
1668 
1669 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1670 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1671 		if (!nl_ftypes)
1672 			return -ENOBUFS;
1673 		i = 0;
1674 		stypes = mgmt_stypes[ift].tx;
1675 		while (stypes) {
1676 			if ((stypes & 1) &&
1677 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1678 					(i << 4) | IEEE80211_FTYPE_MGMT))
1679 				return -ENOBUFS;
1680 			stypes >>= 1;
1681 			i++;
1682 		}
1683 		nla_nest_end(msg, nl_ftypes);
1684 	}
1685 
1686 	nla_nest_end(msg, nl_ifs);
1687 
1688 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1689 	if (!nl_ifs)
1690 		return -ENOBUFS;
1691 
1692 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1693 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1694 		if (!nl_ftypes)
1695 			return -ENOBUFS;
1696 		i = 0;
1697 		stypes = mgmt_stypes[ift].rx;
1698 		while (stypes) {
1699 			if ((stypes & 1) &&
1700 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1701 					(i << 4) | IEEE80211_FTYPE_MGMT))
1702 				return -ENOBUFS;
1703 			stypes >>= 1;
1704 			i++;
1705 		}
1706 		nla_nest_end(msg, nl_ftypes);
1707 	}
1708 	nla_nest_end(msg, nl_ifs);
1709 
1710 	return 0;
1711 }
1712 
1713 #define CMD(op, n)							\
1714 	 do {								\
1715 		if (rdev->ops->op) {					\
1716 			i++;						\
1717 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1718 				goto nla_put_failure;			\
1719 		}							\
1720 	} while (0)
1721 
nl80211_add_commands_unsplit(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1722 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1723 					struct sk_buff *msg)
1724 {
1725 	int i = 0;
1726 
1727 	/*
1728 	 * do *NOT* add anything into this function, new things need to be
1729 	 * advertised only to new versions of userspace that can deal with
1730 	 * the split (and they can't possibly care about new features...
1731 	 */
1732 	CMD(add_virtual_intf, NEW_INTERFACE);
1733 	CMD(change_virtual_intf, SET_INTERFACE);
1734 	CMD(add_key, NEW_KEY);
1735 	CMD(start_ap, START_AP);
1736 	CMD(add_station, NEW_STATION);
1737 	CMD(add_mpath, NEW_MPATH);
1738 	CMD(update_mesh_config, SET_MESH_CONFIG);
1739 	CMD(change_bss, SET_BSS);
1740 	CMD(auth, AUTHENTICATE);
1741 	CMD(assoc, ASSOCIATE);
1742 	CMD(deauth, DEAUTHENTICATE);
1743 	CMD(disassoc, DISASSOCIATE);
1744 	CMD(join_ibss, JOIN_IBSS);
1745 	CMD(join_mesh, JOIN_MESH);
1746 	CMD(set_pmksa, SET_PMKSA);
1747 	CMD(del_pmksa, DEL_PMKSA);
1748 	CMD(flush_pmksa, FLUSH_PMKSA);
1749 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1750 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1751 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1752 	CMD(mgmt_tx, FRAME);
1753 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1754 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1755 		i++;
1756 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1757 			goto nla_put_failure;
1758 	}
1759 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1760 	    rdev->ops->join_mesh) {
1761 		i++;
1762 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1763 			goto nla_put_failure;
1764 	}
1765 	CMD(set_wds_peer, SET_WDS_PEER);
1766 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1767 		CMD(tdls_mgmt, TDLS_MGMT);
1768 		CMD(tdls_oper, TDLS_OPER);
1769 	}
1770 	if (rdev->wiphy.max_sched_scan_reqs)
1771 		CMD(sched_scan_start, START_SCHED_SCAN);
1772 	CMD(probe_client, PROBE_CLIENT);
1773 	CMD(set_noack_map, SET_NOACK_MAP);
1774 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1775 		i++;
1776 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1777 			goto nla_put_failure;
1778 	}
1779 	CMD(start_p2p_device, START_P2P_DEVICE);
1780 	CMD(set_mcast_rate, SET_MCAST_RATE);
1781 #ifdef CONFIG_NL80211_TESTMODE
1782 	CMD(testmode_cmd, TESTMODE);
1783 #endif
1784 
1785 	if (rdev->ops->connect || rdev->ops->auth) {
1786 		i++;
1787 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1788 			goto nla_put_failure;
1789 	}
1790 
1791 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1792 		i++;
1793 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1794 			goto nla_put_failure;
1795 	}
1796 
1797 	return i;
1798  nla_put_failure:
1799 	return -ENOBUFS;
1800 }
1801 
1802 static int
nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities * cap,struct sk_buff * msg)1803 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1804 			   struct sk_buff *msg)
1805 {
1806 	struct nlattr *ftm;
1807 
1808 	if (!cap->ftm.supported)
1809 		return 0;
1810 
1811 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1812 	if (!ftm)
1813 		return -ENOBUFS;
1814 
1815 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1816 		return -ENOBUFS;
1817 	if (cap->ftm.non_asap &&
1818 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1819 		return -ENOBUFS;
1820 	if (cap->ftm.request_lci &&
1821 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1822 		return -ENOBUFS;
1823 	if (cap->ftm.request_civicloc &&
1824 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1825 		return -ENOBUFS;
1826 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1827 			cap->ftm.preambles))
1828 		return -ENOBUFS;
1829 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1830 			cap->ftm.bandwidths))
1831 		return -ENOBUFS;
1832 	if (cap->ftm.max_bursts_exponent >= 0 &&
1833 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1834 			cap->ftm.max_bursts_exponent))
1835 		return -ENOBUFS;
1836 	if (cap->ftm.max_ftms_per_burst &&
1837 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1838 			cap->ftm.max_ftms_per_burst))
1839 		return -ENOBUFS;
1840 
1841 	nla_nest_end(msg, ftm);
1842 	return 0;
1843 }
1844 
nl80211_send_pmsr_capa(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1845 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1846 				  struct sk_buff *msg)
1847 {
1848 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1849 	struct nlattr *pmsr, *caps;
1850 
1851 	if (!cap)
1852 		return 0;
1853 
1854 	/*
1855 	 * we don't need to clean up anything here since the caller
1856 	 * will genlmsg_cancel() if we fail
1857 	 */
1858 
1859 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1860 	if (!pmsr)
1861 		return -ENOBUFS;
1862 
1863 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1864 		return -ENOBUFS;
1865 
1866 	if (cap->report_ap_tsf &&
1867 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1868 		return -ENOBUFS;
1869 
1870 	if (cap->randomize_mac_addr &&
1871 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1872 		return -ENOBUFS;
1873 
1874 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1875 	if (!caps)
1876 		return -ENOBUFS;
1877 
1878 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
1879 		return -ENOBUFS;
1880 
1881 	nla_nest_end(msg, caps);
1882 	nla_nest_end(msg, pmsr);
1883 
1884 	return 0;
1885 }
1886 
1887 struct nl80211_dump_wiphy_state {
1888 	s64 filter_wiphy;
1889 	long start;
1890 	long split_start, band_start, chan_start, capa_start;
1891 	bool split;
1892 };
1893 
nl80211_send_wiphy(struct cfg80211_registered_device * rdev,enum nl80211_commands cmd,struct sk_buff * msg,u32 portid,u32 seq,int flags,struct nl80211_dump_wiphy_state * state)1894 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1895 			      enum nl80211_commands cmd,
1896 			      struct sk_buff *msg, u32 portid, u32 seq,
1897 			      int flags, struct nl80211_dump_wiphy_state *state)
1898 {
1899 	void *hdr;
1900 	struct nlattr *nl_bands, *nl_band;
1901 	struct nlattr *nl_freqs, *nl_freq;
1902 	struct nlattr *nl_cmds;
1903 	enum nl80211_band band;
1904 	struct ieee80211_channel *chan;
1905 	int i;
1906 	const struct ieee80211_txrx_stypes *mgmt_stypes =
1907 				rdev->wiphy.mgmt_stypes;
1908 	u32 features;
1909 
1910 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1911 	if (!hdr)
1912 		return -ENOBUFS;
1913 
1914 	if (WARN_ON(!state))
1915 		return -EINVAL;
1916 
1917 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1918 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1919 			   wiphy_name(&rdev->wiphy)) ||
1920 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
1921 			cfg80211_rdev_list_generation))
1922 		goto nla_put_failure;
1923 
1924 	if (cmd != NL80211_CMD_NEW_WIPHY)
1925 		goto finish;
1926 
1927 	switch (state->split_start) {
1928 	case 0:
1929 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1930 			       rdev->wiphy.retry_short) ||
1931 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1932 			       rdev->wiphy.retry_long) ||
1933 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1934 				rdev->wiphy.frag_threshold) ||
1935 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1936 				rdev->wiphy.rts_threshold) ||
1937 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1938 			       rdev->wiphy.coverage_class) ||
1939 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1940 			       rdev->wiphy.max_scan_ssids) ||
1941 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1942 			       rdev->wiphy.max_sched_scan_ssids) ||
1943 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1944 				rdev->wiphy.max_scan_ie_len) ||
1945 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1946 				rdev->wiphy.max_sched_scan_ie_len) ||
1947 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1948 			       rdev->wiphy.max_match_sets) ||
1949 		    nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1950 				rdev->wiphy.max_sched_scan_plans) ||
1951 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1952 				rdev->wiphy.max_sched_scan_plan_interval) ||
1953 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1954 				rdev->wiphy.max_sched_scan_plan_iterations))
1955 			goto nla_put_failure;
1956 
1957 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1958 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1959 			goto nla_put_failure;
1960 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1961 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1962 			goto nla_put_failure;
1963 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1964 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1965 			goto nla_put_failure;
1966 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1967 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1968 			goto nla_put_failure;
1969 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1970 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1971 			goto nla_put_failure;
1972 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1973 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1974 			goto nla_put_failure;
1975 		state->split_start++;
1976 		if (state->split)
1977 			break;
1978 		/* fall through */
1979 	case 1:
1980 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1981 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
1982 			    rdev->wiphy.cipher_suites))
1983 			goto nla_put_failure;
1984 
1985 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1986 			       rdev->wiphy.max_num_pmkids))
1987 			goto nla_put_failure;
1988 
1989 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1990 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1991 			goto nla_put_failure;
1992 
1993 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1994 				rdev->wiphy.available_antennas_tx) ||
1995 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1996 				rdev->wiphy.available_antennas_rx))
1997 			goto nla_put_failure;
1998 
1999 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2000 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2001 				rdev->wiphy.probe_resp_offload))
2002 			goto nla_put_failure;
2003 
2004 		if ((rdev->wiphy.available_antennas_tx ||
2005 		     rdev->wiphy.available_antennas_rx) &&
2006 		    rdev->ops->get_antenna) {
2007 			u32 tx_ant = 0, rx_ant = 0;
2008 			int res;
2009 
2010 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2011 			if (!res) {
2012 				if (nla_put_u32(msg,
2013 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2014 						tx_ant) ||
2015 				    nla_put_u32(msg,
2016 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2017 						rx_ant))
2018 					goto nla_put_failure;
2019 			}
2020 		}
2021 
2022 		state->split_start++;
2023 		if (state->split)
2024 			break;
2025 		/* fall through */
2026 	case 2:
2027 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2028 					rdev->wiphy.interface_modes))
2029 				goto nla_put_failure;
2030 		state->split_start++;
2031 		if (state->split)
2032 			break;
2033 		/* fall through */
2034 	case 3:
2035 		nl_bands = nla_nest_start_noflag(msg,
2036 						 NL80211_ATTR_WIPHY_BANDS);
2037 		if (!nl_bands)
2038 			goto nla_put_failure;
2039 
2040 		for (band = state->band_start;
2041 		     band < NUM_NL80211_BANDS; band++) {
2042 			struct ieee80211_supported_band *sband;
2043 
2044 			sband = rdev->wiphy.bands[band];
2045 
2046 			if (!sband)
2047 				continue;
2048 
2049 			nl_band = nla_nest_start_noflag(msg, band);
2050 			if (!nl_band)
2051 				goto nla_put_failure;
2052 
2053 			switch (state->chan_start) {
2054 			case 0:
2055 				if (nl80211_send_band_rateinfo(msg, sband))
2056 					goto nla_put_failure;
2057 				state->chan_start++;
2058 				if (state->split)
2059 					break;
2060 				/* fall through */
2061 			default:
2062 				/* add frequencies */
2063 				nl_freqs = nla_nest_start_noflag(msg,
2064 								 NL80211_BAND_ATTR_FREQS);
2065 				if (!nl_freqs)
2066 					goto nla_put_failure;
2067 
2068 				for (i = state->chan_start - 1;
2069 				     i < sband->n_channels;
2070 				     i++) {
2071 					nl_freq = nla_nest_start_noflag(msg,
2072 									i);
2073 					if (!nl_freq)
2074 						goto nla_put_failure;
2075 
2076 					chan = &sband->channels[i];
2077 
2078 					if (nl80211_msg_put_channel(
2079 							msg, &rdev->wiphy, chan,
2080 							state->split))
2081 						goto nla_put_failure;
2082 
2083 					nla_nest_end(msg, nl_freq);
2084 					if (state->split)
2085 						break;
2086 				}
2087 				if (i < sband->n_channels)
2088 					state->chan_start = i + 2;
2089 				else
2090 					state->chan_start = 0;
2091 				nla_nest_end(msg, nl_freqs);
2092 			}
2093 
2094 			nla_nest_end(msg, nl_band);
2095 
2096 			if (state->split) {
2097 				/* start again here */
2098 				if (state->chan_start)
2099 					band--;
2100 				break;
2101 			}
2102 		}
2103 		nla_nest_end(msg, nl_bands);
2104 
2105 		if (band < NUM_NL80211_BANDS)
2106 			state->band_start = band + 1;
2107 		else
2108 			state->band_start = 0;
2109 
2110 		/* if bands & channels are done, continue outside */
2111 		if (state->band_start == 0 && state->chan_start == 0)
2112 			state->split_start++;
2113 		if (state->split)
2114 			break;
2115 		/* fall through */
2116 	case 4:
2117 		nl_cmds = nla_nest_start_noflag(msg,
2118 						NL80211_ATTR_SUPPORTED_COMMANDS);
2119 		if (!nl_cmds)
2120 			goto nla_put_failure;
2121 
2122 		i = nl80211_add_commands_unsplit(rdev, msg);
2123 		if (i < 0)
2124 			goto nla_put_failure;
2125 		if (state->split) {
2126 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2127 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2128 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2129 				CMD(channel_switch, CHANNEL_SWITCH);
2130 			CMD(set_qos_map, SET_QOS_MAP);
2131 			if (rdev->wiphy.features &
2132 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2133 				CMD(add_tx_ts, ADD_TX_TS);
2134 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2135 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2136 			CMD(update_ft_ies, UPDATE_FT_IES);
2137 		}
2138 #undef CMD
2139 
2140 		nla_nest_end(msg, nl_cmds);
2141 		state->split_start++;
2142 		if (state->split)
2143 			break;
2144 		/* fall through */
2145 	case 5:
2146 		if (rdev->ops->remain_on_channel &&
2147 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2148 		    nla_put_u32(msg,
2149 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2150 				rdev->wiphy.max_remain_on_channel_duration))
2151 			goto nla_put_failure;
2152 
2153 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2154 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2155 			goto nla_put_failure;
2156 
2157 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2158 			goto nla_put_failure;
2159 		state->split_start++;
2160 		if (state->split)
2161 			break;
2162 		/* fall through */
2163 	case 6:
2164 #ifdef CONFIG_PM
2165 		if (nl80211_send_wowlan(msg, rdev, state->split))
2166 			goto nla_put_failure;
2167 		state->split_start++;
2168 		if (state->split)
2169 			break;
2170 #else
2171 		state->split_start++;
2172 #endif
2173 		/* fall through */
2174 	case 7:
2175 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2176 					rdev->wiphy.software_iftypes))
2177 			goto nla_put_failure;
2178 
2179 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2180 						   state->split))
2181 			goto nla_put_failure;
2182 
2183 		state->split_start++;
2184 		if (state->split)
2185 			break;
2186 		/* fall through */
2187 	case 8:
2188 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2189 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2190 				rdev->wiphy.ap_sme_capa))
2191 			goto nla_put_failure;
2192 
2193 		features = rdev->wiphy.features;
2194 		/*
2195 		 * We can only add the per-channel limit information if the
2196 		 * dump is split, otherwise it makes it too big. Therefore
2197 		 * only advertise it in that case.
2198 		 */
2199 		if (state->split)
2200 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2201 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2202 			goto nla_put_failure;
2203 
2204 		if (rdev->wiphy.ht_capa_mod_mask &&
2205 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2206 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2207 			    rdev->wiphy.ht_capa_mod_mask))
2208 			goto nla_put_failure;
2209 
2210 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2211 		    rdev->wiphy.max_acl_mac_addrs &&
2212 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2213 				rdev->wiphy.max_acl_mac_addrs))
2214 			goto nla_put_failure;
2215 
2216 		/*
2217 		 * Any information below this point is only available to
2218 		 * applications that can deal with it being split. This
2219 		 * helps ensure that newly added capabilities don't break
2220 		 * older tools by overrunning their buffers.
2221 		 *
2222 		 * We still increment split_start so that in the split
2223 		 * case we'll continue with more data in the next round,
2224 		 * but break unconditionally so unsplit data stops here.
2225 		 */
2226 		state->split_start++;
2227 		break;
2228 	case 9:
2229 		if (rdev->wiphy.extended_capabilities &&
2230 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2231 			     rdev->wiphy.extended_capabilities_len,
2232 			     rdev->wiphy.extended_capabilities) ||
2233 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2234 			     rdev->wiphy.extended_capabilities_len,
2235 			     rdev->wiphy.extended_capabilities_mask)))
2236 			goto nla_put_failure;
2237 
2238 		if (rdev->wiphy.vht_capa_mod_mask &&
2239 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2240 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2241 			    rdev->wiphy.vht_capa_mod_mask))
2242 			goto nla_put_failure;
2243 
2244 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2245 			    rdev->wiphy.perm_addr))
2246 			goto nla_put_failure;
2247 
2248 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2249 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2250 			    rdev->wiphy.addr_mask))
2251 			goto nla_put_failure;
2252 
2253 		if (rdev->wiphy.n_addresses > 1) {
2254 			void *attr;
2255 
2256 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2257 			if (!attr)
2258 				goto nla_put_failure;
2259 
2260 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2261 				if (nla_put(msg, i + 1, ETH_ALEN,
2262 					    rdev->wiphy.addresses[i].addr))
2263 					goto nla_put_failure;
2264 
2265 			nla_nest_end(msg, attr);
2266 		}
2267 
2268 		state->split_start++;
2269 		break;
2270 	case 10:
2271 		if (nl80211_send_coalesce(msg, rdev))
2272 			goto nla_put_failure;
2273 
2274 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2275 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2276 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2277 			goto nla_put_failure;
2278 
2279 		if (rdev->wiphy.max_ap_assoc_sta &&
2280 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2281 				rdev->wiphy.max_ap_assoc_sta))
2282 			goto nla_put_failure;
2283 
2284 		state->split_start++;
2285 		break;
2286 	case 11:
2287 		if (rdev->wiphy.n_vendor_commands) {
2288 			const struct nl80211_vendor_cmd_info *info;
2289 			struct nlattr *nested;
2290 
2291 			nested = nla_nest_start_noflag(msg,
2292 						       NL80211_ATTR_VENDOR_DATA);
2293 			if (!nested)
2294 				goto nla_put_failure;
2295 
2296 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2297 				info = &rdev->wiphy.vendor_commands[i].info;
2298 				if (nla_put(msg, i + 1, sizeof(*info), info))
2299 					goto nla_put_failure;
2300 			}
2301 			nla_nest_end(msg, nested);
2302 		}
2303 
2304 		if (rdev->wiphy.n_vendor_events) {
2305 			const struct nl80211_vendor_cmd_info *info;
2306 			struct nlattr *nested;
2307 
2308 			nested = nla_nest_start_noflag(msg,
2309 						       NL80211_ATTR_VENDOR_EVENTS);
2310 			if (!nested)
2311 				goto nla_put_failure;
2312 
2313 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2314 				info = &rdev->wiphy.vendor_events[i];
2315 				if (nla_put(msg, i + 1, sizeof(*info), info))
2316 					goto nla_put_failure;
2317 			}
2318 			nla_nest_end(msg, nested);
2319 		}
2320 		state->split_start++;
2321 		break;
2322 	case 12:
2323 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2324 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2325 			       rdev->wiphy.max_num_csa_counters))
2326 			goto nla_put_failure;
2327 
2328 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2329 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2330 			goto nla_put_failure;
2331 
2332 		if (rdev->wiphy.max_sched_scan_reqs &&
2333 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2334 				rdev->wiphy.max_sched_scan_reqs))
2335 			goto nla_put_failure;
2336 
2337 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2338 			    sizeof(rdev->wiphy.ext_features),
2339 			    rdev->wiphy.ext_features))
2340 			goto nla_put_failure;
2341 
2342 		if (rdev->wiphy.bss_select_support) {
2343 			struct nlattr *nested;
2344 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2345 
2346 			nested = nla_nest_start_noflag(msg,
2347 						       NL80211_ATTR_BSS_SELECT);
2348 			if (!nested)
2349 				goto nla_put_failure;
2350 
2351 			i = 0;
2352 			while (bss_select_support) {
2353 				if ((bss_select_support & 1) &&
2354 				    nla_put_flag(msg, i))
2355 					goto nla_put_failure;
2356 				i++;
2357 				bss_select_support >>= 1;
2358 			}
2359 			nla_nest_end(msg, nested);
2360 		}
2361 
2362 		state->split_start++;
2363 		break;
2364 	case 13:
2365 		if (rdev->wiphy.num_iftype_ext_capab &&
2366 		    rdev->wiphy.iftype_ext_capab) {
2367 			struct nlattr *nested_ext_capab, *nested;
2368 
2369 			nested = nla_nest_start_noflag(msg,
2370 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2371 			if (!nested)
2372 				goto nla_put_failure;
2373 
2374 			for (i = state->capa_start;
2375 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2376 				const struct wiphy_iftype_ext_capab *capab;
2377 
2378 				capab = &rdev->wiphy.iftype_ext_capab[i];
2379 
2380 				nested_ext_capab = nla_nest_start_noflag(msg,
2381 									 i);
2382 				if (!nested_ext_capab ||
2383 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2384 						capab->iftype) ||
2385 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2386 					    capab->extended_capabilities_len,
2387 					    capab->extended_capabilities) ||
2388 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2389 					    capab->extended_capabilities_len,
2390 					    capab->extended_capabilities_mask))
2391 					goto nla_put_failure;
2392 
2393 				nla_nest_end(msg, nested_ext_capab);
2394 				if (state->split)
2395 					break;
2396 			}
2397 			nla_nest_end(msg, nested);
2398 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2399 				state->capa_start = i + 1;
2400 				break;
2401 			}
2402 		}
2403 
2404 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2405 				rdev->wiphy.nan_supported_bands))
2406 			goto nla_put_failure;
2407 
2408 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2409 					    NL80211_EXT_FEATURE_TXQS)) {
2410 			struct cfg80211_txq_stats txqstats = {};
2411 			int res;
2412 
2413 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2414 			if (!res &&
2415 			    !nl80211_put_txq_stats(msg, &txqstats,
2416 						   NL80211_ATTR_TXQ_STATS))
2417 				goto nla_put_failure;
2418 
2419 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2420 					rdev->wiphy.txq_limit))
2421 				goto nla_put_failure;
2422 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2423 					rdev->wiphy.txq_memory_limit))
2424 				goto nla_put_failure;
2425 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2426 					rdev->wiphy.txq_quantum))
2427 				goto nla_put_failure;
2428 		}
2429 
2430 		state->split_start++;
2431 		break;
2432 	case 14:
2433 		if (nl80211_send_pmsr_capa(rdev, msg))
2434 			goto nla_put_failure;
2435 
2436 		state->split_start++;
2437 		break;
2438 	case 15:
2439 		if (rdev->wiphy.akm_suites &&
2440 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2441 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2442 			    rdev->wiphy.akm_suites))
2443 			goto nla_put_failure;
2444 
2445 		/* done */
2446 		state->split_start = 0;
2447 		break;
2448 	}
2449  finish:
2450 	genlmsg_end(msg, hdr);
2451 	return 0;
2452 
2453  nla_put_failure:
2454 	genlmsg_cancel(msg, hdr);
2455 	return -EMSGSIZE;
2456 }
2457 
nl80211_dump_wiphy_parse(struct sk_buff * skb,struct netlink_callback * cb,struct nl80211_dump_wiphy_state * state)2458 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2459 				    struct netlink_callback *cb,
2460 				    struct nl80211_dump_wiphy_state *state)
2461 {
2462 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2463 	int ret;
2464 
2465 	if (!tb)
2466 		return -ENOMEM;
2467 
2468 	ret = nlmsg_parse_deprecated(cb->nlh,
2469 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2470 				     tb, nl80211_fam.maxattr,
2471 				     nl80211_policy, NULL);
2472 	/* ignore parse errors for backward compatibility */
2473 	if (ret) {
2474 		ret = 0;
2475 		goto out;
2476 	}
2477 
2478 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2479 	if (tb[NL80211_ATTR_WIPHY])
2480 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2481 	if (tb[NL80211_ATTR_WDEV])
2482 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2483 	if (tb[NL80211_ATTR_IFINDEX]) {
2484 		struct net_device *netdev;
2485 		struct cfg80211_registered_device *rdev;
2486 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2487 
2488 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2489 		if (!netdev) {
2490 			ret = -ENODEV;
2491 			goto out;
2492 		}
2493 		if (netdev->ieee80211_ptr) {
2494 			rdev = wiphy_to_rdev(
2495 				netdev->ieee80211_ptr->wiphy);
2496 			state->filter_wiphy = rdev->wiphy_idx;
2497 		}
2498 	}
2499 
2500 	ret = 0;
2501 out:
2502 	kfree(tb);
2503 	return ret;
2504 }
2505 
nl80211_dump_wiphy(struct sk_buff * skb,struct netlink_callback * cb)2506 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2507 {
2508 	int idx = 0, ret;
2509 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2510 	struct cfg80211_registered_device *rdev;
2511 
2512 	rtnl_lock();
2513 	if (!state) {
2514 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2515 		if (!state) {
2516 			rtnl_unlock();
2517 			return -ENOMEM;
2518 		}
2519 		state->filter_wiphy = -1;
2520 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2521 		if (ret) {
2522 			kfree(state);
2523 			rtnl_unlock();
2524 			return ret;
2525 		}
2526 		cb->args[0] = (long)state;
2527 	}
2528 
2529 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2530 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2531 			continue;
2532 		if (++idx <= state->start)
2533 			continue;
2534 		if (state->filter_wiphy != -1 &&
2535 		    state->filter_wiphy != rdev->wiphy_idx)
2536 			continue;
2537 		/* attempt to fit multiple wiphy data chunks into the skb */
2538 		do {
2539 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2540 						 skb,
2541 						 NETLINK_CB(cb->skb).portid,
2542 						 cb->nlh->nlmsg_seq,
2543 						 NLM_F_MULTI, state);
2544 			if (ret < 0) {
2545 				/*
2546 				 * If sending the wiphy data didn't fit (ENOBUFS
2547 				 * or EMSGSIZE returned), this SKB is still
2548 				 * empty (so it's not too big because another
2549 				 * wiphy dataset is already in the skb) and
2550 				 * we've not tried to adjust the dump allocation
2551 				 * yet ... then adjust the alloc size to be
2552 				 * bigger, and return 1 but with the empty skb.
2553 				 * This results in an empty message being RX'ed
2554 				 * in userspace, but that is ignored.
2555 				 *
2556 				 * We can then retry with the larger buffer.
2557 				 */
2558 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2559 				    !skb->len && !state->split &&
2560 				    cb->min_dump_alloc < 4096) {
2561 					cb->min_dump_alloc = 4096;
2562 					state->split_start = 0;
2563 					rtnl_unlock();
2564 					return 1;
2565 				}
2566 				idx--;
2567 				break;
2568 			}
2569 		} while (state->split_start > 0);
2570 		break;
2571 	}
2572 	rtnl_unlock();
2573 
2574 	state->start = idx;
2575 
2576 	return skb->len;
2577 }
2578 
nl80211_dump_wiphy_done(struct netlink_callback * cb)2579 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2580 {
2581 	kfree((void *)cb->args[0]);
2582 	return 0;
2583 }
2584 
nl80211_get_wiphy(struct sk_buff * skb,struct genl_info * info)2585 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2586 {
2587 	struct sk_buff *msg;
2588 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2589 	struct nl80211_dump_wiphy_state state = {};
2590 
2591 	msg = nlmsg_new(4096, GFP_KERNEL);
2592 	if (!msg)
2593 		return -ENOMEM;
2594 
2595 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2596 			       info->snd_portid, info->snd_seq, 0,
2597 			       &state) < 0) {
2598 		nlmsg_free(msg);
2599 		return -ENOBUFS;
2600 	}
2601 
2602 	return genlmsg_reply(msg, info);
2603 }
2604 
2605 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2606 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2607 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2608 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2609 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2610 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2611 };
2612 
parse_txq_params(struct nlattr * tb[],struct ieee80211_txq_params * txq_params)2613 static int parse_txq_params(struct nlattr *tb[],
2614 			    struct ieee80211_txq_params *txq_params)
2615 {
2616 	u8 ac;
2617 
2618 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2619 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2620 	    !tb[NL80211_TXQ_ATTR_AIFS])
2621 		return -EINVAL;
2622 
2623 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2624 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2625 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2626 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2627 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2628 
2629 	if (ac >= NL80211_NUM_ACS)
2630 		return -EINVAL;
2631 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2632 	return 0;
2633 }
2634 
nl80211_can_set_dev_channel(struct wireless_dev * wdev)2635 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2636 {
2637 	/*
2638 	 * You can only set the channel explicitly for WDS interfaces,
2639 	 * all others have their channel managed via their respective
2640 	 * "establish a connection" command (connect, join, ...)
2641 	 *
2642 	 * For AP/GO and mesh mode, the channel can be set with the
2643 	 * channel userspace API, but is only stored and passed to the
2644 	 * low-level driver when the AP starts or the mesh is joined.
2645 	 * This is for backward compatibility, userspace can also give
2646 	 * the channel in the start-ap or join-mesh commands instead.
2647 	 *
2648 	 * Monitors are special as they are normally slaved to
2649 	 * whatever else is going on, so they have their own special
2650 	 * operation to set the monitor channel if possible.
2651 	 */
2652 	return !wdev ||
2653 		wdev->iftype == NL80211_IFTYPE_AP ||
2654 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2655 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2656 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2657 }
2658 
nl80211_parse_chandef(struct cfg80211_registered_device * rdev,struct genl_info * info,struct cfg80211_chan_def * chandef)2659 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2660 			  struct genl_info *info,
2661 			  struct cfg80211_chan_def *chandef)
2662 {
2663 	struct netlink_ext_ack *extack = info->extack;
2664 	struct nlattr **attrs = info->attrs;
2665 	u32 control_freq;
2666 
2667 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2668 		return -EINVAL;
2669 
2670 	control_freq = nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]);
2671 
2672 	memset(chandef, 0, sizeof(*chandef));
2673 
2674 	chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2675 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2676 	chandef->center_freq1 = control_freq;
2677 	chandef->center_freq2 = 0;
2678 
2679 	/* Primary channel not allowed */
2680 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2681 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2682 				    "Channel is disabled");
2683 		return -EINVAL;
2684 	}
2685 
2686 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2687 		enum nl80211_channel_type chantype;
2688 
2689 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2690 
2691 		switch (chantype) {
2692 		case NL80211_CHAN_NO_HT:
2693 		case NL80211_CHAN_HT20:
2694 		case NL80211_CHAN_HT40PLUS:
2695 		case NL80211_CHAN_HT40MINUS:
2696 			cfg80211_chandef_create(chandef, chandef->chan,
2697 						chantype);
2698 			/* user input for center_freq is incorrect */
2699 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2700 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2701 				NL_SET_ERR_MSG_ATTR(extack,
2702 						    attrs[NL80211_ATTR_CENTER_FREQ1],
2703 						    "bad center frequency 1");
2704 				return -EINVAL;
2705 			}
2706 			/* center_freq2 must be zero */
2707 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2708 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2709 				NL_SET_ERR_MSG_ATTR(extack,
2710 						    attrs[NL80211_ATTR_CENTER_FREQ2],
2711 						    "center frequency 2 can't be used");
2712 				return -EINVAL;
2713 			}
2714 			break;
2715 		default:
2716 			NL_SET_ERR_MSG_ATTR(extack,
2717 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2718 					    "invalid channel type");
2719 			return -EINVAL;
2720 		}
2721 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2722 		chandef->width =
2723 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2724 		if (attrs[NL80211_ATTR_CENTER_FREQ1])
2725 			chandef->center_freq1 =
2726 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2727 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
2728 			chandef->center_freq2 =
2729 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2730 	}
2731 
2732 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2733 		chandef->edmg.channels =
2734 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2735 
2736 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2737 			chandef->edmg.bw_config =
2738 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2739 	} else {
2740 		chandef->edmg.bw_config = 0;
2741 		chandef->edmg.channels = 0;
2742 	}
2743 
2744 	if (!cfg80211_chandef_valid(chandef)) {
2745 		NL_SET_ERR_MSG(extack, "invalid channel definition");
2746 		return -EINVAL;
2747 	}
2748 
2749 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2750 				     IEEE80211_CHAN_DISABLED)) {
2751 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2752 		return -EINVAL;
2753 	}
2754 
2755 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2756 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
2757 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2758 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2759 		return -EINVAL;
2760 	}
2761 
2762 	return 0;
2763 }
2764 
__nl80211_set_channel(struct cfg80211_registered_device * rdev,struct net_device * dev,struct genl_info * info)2765 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2766 				 struct net_device *dev,
2767 				 struct genl_info *info)
2768 {
2769 	struct cfg80211_chan_def chandef;
2770 	int result;
2771 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2772 	struct wireless_dev *wdev = NULL;
2773 
2774 	if (dev)
2775 		wdev = dev->ieee80211_ptr;
2776 	if (!nl80211_can_set_dev_channel(wdev))
2777 		return -EOPNOTSUPP;
2778 	if (wdev)
2779 		iftype = wdev->iftype;
2780 
2781 	result = nl80211_parse_chandef(rdev, info, &chandef);
2782 	if (result)
2783 		return result;
2784 
2785 	switch (iftype) {
2786 	case NL80211_IFTYPE_AP:
2787 	case NL80211_IFTYPE_P2P_GO:
2788 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2789 						   iftype)) {
2790 			result = -EINVAL;
2791 			break;
2792 		}
2793 		if (wdev->beacon_interval) {
2794 			if (!dev || !rdev->ops->set_ap_chanwidth ||
2795 			    !(rdev->wiphy.features &
2796 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2797 				result = -EBUSY;
2798 				break;
2799 			}
2800 
2801 			/* Only allow dynamic channel width changes */
2802 			if (chandef.chan != wdev->preset_chandef.chan) {
2803 				result = -EBUSY;
2804 				break;
2805 			}
2806 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2807 			if (result)
2808 				break;
2809 		}
2810 		wdev->preset_chandef = chandef;
2811 		result = 0;
2812 		break;
2813 	case NL80211_IFTYPE_MESH_POINT:
2814 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2815 		break;
2816 	case NL80211_IFTYPE_MONITOR:
2817 		result = cfg80211_set_monitor_channel(rdev, &chandef);
2818 		break;
2819 	default:
2820 		result = -EINVAL;
2821 	}
2822 
2823 	return result;
2824 }
2825 
nl80211_set_channel(struct sk_buff * skb,struct genl_info * info)2826 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2827 {
2828 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2829 	struct net_device *netdev = info->user_ptr[1];
2830 
2831 	return __nl80211_set_channel(rdev, netdev, info);
2832 }
2833 
nl80211_set_wds_peer(struct sk_buff * skb,struct genl_info * info)2834 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2835 {
2836 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2837 	struct net_device *dev = info->user_ptr[1];
2838 	struct wireless_dev *wdev = dev->ieee80211_ptr;
2839 	const u8 *bssid;
2840 
2841 	if (!info->attrs[NL80211_ATTR_MAC])
2842 		return -EINVAL;
2843 
2844 	if (netif_running(dev))
2845 		return -EBUSY;
2846 
2847 	if (!rdev->ops->set_wds_peer)
2848 		return -EOPNOTSUPP;
2849 
2850 	if (wdev->iftype != NL80211_IFTYPE_WDS)
2851 		return -EOPNOTSUPP;
2852 
2853 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2854 	return rdev_set_wds_peer(rdev, dev, bssid);
2855 }
2856 
nl80211_set_wiphy(struct sk_buff * skb,struct genl_info * info)2857 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2858 {
2859 	struct cfg80211_registered_device *rdev;
2860 	struct net_device *netdev = NULL;
2861 	struct wireless_dev *wdev;
2862 	int result = 0, rem_txq_params = 0;
2863 	struct nlattr *nl_txq_params;
2864 	u32 changed;
2865 	u8 retry_short = 0, retry_long = 0;
2866 	u32 frag_threshold = 0, rts_threshold = 0;
2867 	u8 coverage_class = 0;
2868 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2869 
2870 	ASSERT_RTNL();
2871 
2872 	/*
2873 	 * Try to find the wiphy and netdev. Normally this
2874 	 * function shouldn't need the netdev, but this is
2875 	 * done for backward compatibility -- previously
2876 	 * setting the channel was done per wiphy, but now
2877 	 * it is per netdev. Previous userland like hostapd
2878 	 * also passed a netdev to set_wiphy, so that it is
2879 	 * possible to let that go to the right netdev!
2880 	 */
2881 
2882 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
2883 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2884 
2885 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2886 		if (netdev && netdev->ieee80211_ptr)
2887 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2888 		else
2889 			netdev = NULL;
2890 	}
2891 
2892 	if (!netdev) {
2893 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2894 						  info->attrs);
2895 		if (IS_ERR(rdev))
2896 			return PTR_ERR(rdev);
2897 		wdev = NULL;
2898 		netdev = NULL;
2899 		result = 0;
2900 	} else
2901 		wdev = netdev->ieee80211_ptr;
2902 
2903 	/*
2904 	 * end workaround code, by now the rdev is available
2905 	 * and locked, and wdev may or may not be NULL.
2906 	 */
2907 
2908 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2909 		result = cfg80211_dev_rename(
2910 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2911 
2912 	if (result)
2913 		return result;
2914 
2915 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2916 		struct ieee80211_txq_params txq_params;
2917 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2918 
2919 		if (!rdev->ops->set_txq_params)
2920 			return -EOPNOTSUPP;
2921 
2922 		if (!netdev)
2923 			return -EINVAL;
2924 
2925 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2926 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2927 			return -EINVAL;
2928 
2929 		if (!netif_running(netdev))
2930 			return -ENETDOWN;
2931 
2932 		nla_for_each_nested(nl_txq_params,
2933 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2934 				    rem_txq_params) {
2935 			result = nla_parse_nested_deprecated(tb,
2936 							     NL80211_TXQ_ATTR_MAX,
2937 							     nl_txq_params,
2938 							     txq_params_policy,
2939 							     info->extack);
2940 			if (result)
2941 				return result;
2942 			result = parse_txq_params(tb, &txq_params);
2943 			if (result)
2944 				return result;
2945 
2946 			result = rdev_set_txq_params(rdev, netdev,
2947 						     &txq_params);
2948 			if (result)
2949 				return result;
2950 		}
2951 	}
2952 
2953 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2954 		result = __nl80211_set_channel(
2955 			rdev,
2956 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2957 			info);
2958 		if (result)
2959 			return result;
2960 	}
2961 
2962 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2963 		struct wireless_dev *txp_wdev = wdev;
2964 		enum nl80211_tx_power_setting type;
2965 		int idx, mbm = 0;
2966 
2967 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2968 			txp_wdev = NULL;
2969 
2970 		if (!rdev->ops->set_tx_power)
2971 			return -EOPNOTSUPP;
2972 
2973 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2974 		type = nla_get_u32(info->attrs[idx]);
2975 
2976 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2977 		    (type != NL80211_TX_POWER_AUTOMATIC))
2978 			return -EINVAL;
2979 
2980 		if (type != NL80211_TX_POWER_AUTOMATIC) {
2981 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2982 			mbm = nla_get_u32(info->attrs[idx]);
2983 		}
2984 
2985 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2986 		if (result)
2987 			return result;
2988 	}
2989 
2990 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2991 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2992 		u32 tx_ant, rx_ant;
2993 
2994 		if ((!rdev->wiphy.available_antennas_tx &&
2995 		     !rdev->wiphy.available_antennas_rx) ||
2996 		    !rdev->ops->set_antenna)
2997 			return -EOPNOTSUPP;
2998 
2999 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3000 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3001 
3002 		/* reject antenna configurations which don't match the
3003 		 * available antenna masks, except for the "all" mask */
3004 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3005 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3006 			return -EINVAL;
3007 
3008 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3009 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3010 
3011 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3012 		if (result)
3013 			return result;
3014 	}
3015 
3016 	changed = 0;
3017 
3018 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3019 		retry_short = nla_get_u8(
3020 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3021 
3022 		changed |= WIPHY_PARAM_RETRY_SHORT;
3023 	}
3024 
3025 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3026 		retry_long = nla_get_u8(
3027 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3028 
3029 		changed |= WIPHY_PARAM_RETRY_LONG;
3030 	}
3031 
3032 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3033 		frag_threshold = nla_get_u32(
3034 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3035 		if (frag_threshold < 256)
3036 			return -EINVAL;
3037 
3038 		if (frag_threshold != (u32) -1) {
3039 			/*
3040 			 * Fragments (apart from the last one) are required to
3041 			 * have even length. Make the fragmentation code
3042 			 * simpler by stripping LSB should someone try to use
3043 			 * odd threshold value.
3044 			 */
3045 			frag_threshold &= ~0x1;
3046 		}
3047 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3048 	}
3049 
3050 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3051 		rts_threshold = nla_get_u32(
3052 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3053 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3054 	}
3055 
3056 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3057 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3058 			return -EINVAL;
3059 
3060 		coverage_class = nla_get_u8(
3061 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3062 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3063 	}
3064 
3065 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3066 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3067 			return -EOPNOTSUPP;
3068 
3069 		changed |= WIPHY_PARAM_DYN_ACK;
3070 	}
3071 
3072 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3073 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3074 					     NL80211_EXT_FEATURE_TXQS))
3075 			return -EOPNOTSUPP;
3076 		txq_limit = nla_get_u32(
3077 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3078 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3079 	}
3080 
3081 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3082 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3083 					     NL80211_EXT_FEATURE_TXQS))
3084 			return -EOPNOTSUPP;
3085 		txq_memory_limit = nla_get_u32(
3086 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3087 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3088 	}
3089 
3090 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3091 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3092 					     NL80211_EXT_FEATURE_TXQS))
3093 			return -EOPNOTSUPP;
3094 		txq_quantum = nla_get_u32(
3095 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3096 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3097 	}
3098 
3099 	if (changed) {
3100 		u8 old_retry_short, old_retry_long;
3101 		u32 old_frag_threshold, old_rts_threshold;
3102 		u8 old_coverage_class;
3103 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3104 
3105 		if (!rdev->ops->set_wiphy_params)
3106 			return -EOPNOTSUPP;
3107 
3108 		old_retry_short = rdev->wiphy.retry_short;
3109 		old_retry_long = rdev->wiphy.retry_long;
3110 		old_frag_threshold = rdev->wiphy.frag_threshold;
3111 		old_rts_threshold = rdev->wiphy.rts_threshold;
3112 		old_coverage_class = rdev->wiphy.coverage_class;
3113 		old_txq_limit = rdev->wiphy.txq_limit;
3114 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3115 		old_txq_quantum = rdev->wiphy.txq_quantum;
3116 
3117 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3118 			rdev->wiphy.retry_short = retry_short;
3119 		if (changed & WIPHY_PARAM_RETRY_LONG)
3120 			rdev->wiphy.retry_long = retry_long;
3121 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3122 			rdev->wiphy.frag_threshold = frag_threshold;
3123 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3124 			rdev->wiphy.rts_threshold = rts_threshold;
3125 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3126 			rdev->wiphy.coverage_class = coverage_class;
3127 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3128 			rdev->wiphy.txq_limit = txq_limit;
3129 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3130 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3131 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3132 			rdev->wiphy.txq_quantum = txq_quantum;
3133 
3134 		result = rdev_set_wiphy_params(rdev, changed);
3135 		if (result) {
3136 			rdev->wiphy.retry_short = old_retry_short;
3137 			rdev->wiphy.retry_long = old_retry_long;
3138 			rdev->wiphy.frag_threshold = old_frag_threshold;
3139 			rdev->wiphy.rts_threshold = old_rts_threshold;
3140 			rdev->wiphy.coverage_class = old_coverage_class;
3141 			rdev->wiphy.txq_limit = old_txq_limit;
3142 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3143 			rdev->wiphy.txq_quantum = old_txq_quantum;
3144 			return result;
3145 		}
3146 	}
3147 	return 0;
3148 }
3149 
nl80211_send_chandef(struct sk_buff * msg,const struct cfg80211_chan_def * chandef)3150 static int nl80211_send_chandef(struct sk_buff *msg,
3151 				const struct cfg80211_chan_def *chandef)
3152 {
3153 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3154 		return -EINVAL;
3155 
3156 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3157 			chandef->chan->center_freq))
3158 		return -ENOBUFS;
3159 	switch (chandef->width) {
3160 	case NL80211_CHAN_WIDTH_20_NOHT:
3161 	case NL80211_CHAN_WIDTH_20:
3162 	case NL80211_CHAN_WIDTH_40:
3163 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3164 				cfg80211_get_chandef_type(chandef)))
3165 			return -ENOBUFS;
3166 		break;
3167 	default:
3168 		break;
3169 	}
3170 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3171 		return -ENOBUFS;
3172 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3173 		return -ENOBUFS;
3174 	if (chandef->center_freq2 &&
3175 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3176 		return -ENOBUFS;
3177 	return 0;
3178 }
3179 
nl80211_send_iface(struct sk_buff * msg,u32 portid,u32 seq,int flags,struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,enum nl80211_commands cmd)3180 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3181 			      struct cfg80211_registered_device *rdev,
3182 			      struct wireless_dev *wdev,
3183 			      enum nl80211_commands cmd)
3184 {
3185 	struct net_device *dev = wdev->netdev;
3186 	void *hdr;
3187 
3188 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3189 		cmd != NL80211_CMD_DEL_INTERFACE &&
3190 		cmd != NL80211_CMD_SET_INTERFACE);
3191 
3192 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3193 	if (!hdr)
3194 		return -1;
3195 
3196 	if (dev &&
3197 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3198 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3199 		goto nla_put_failure;
3200 
3201 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3202 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3203 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3204 			      NL80211_ATTR_PAD) ||
3205 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3206 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3207 			rdev->devlist_generation ^
3208 			(cfg80211_rdev_list_generation << 2)) ||
3209 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3210 		goto nla_put_failure;
3211 
3212 	if (rdev->ops->get_channel) {
3213 		int ret;
3214 		struct cfg80211_chan_def chandef = {};
3215 
3216 		ret = rdev_get_channel(rdev, wdev, &chandef);
3217 		if (ret == 0) {
3218 			if (nl80211_send_chandef(msg, &chandef))
3219 				goto nla_put_failure;
3220 		}
3221 	}
3222 
3223 	if (rdev->ops->get_tx_power) {
3224 		int dbm, ret;
3225 
3226 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3227 		if (ret == 0 &&
3228 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3229 				DBM_TO_MBM(dbm)))
3230 			goto nla_put_failure;
3231 	}
3232 
3233 	wdev_lock(wdev);
3234 	switch (wdev->iftype) {
3235 	case NL80211_IFTYPE_AP:
3236 		if (wdev->ssid_len &&
3237 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3238 			goto nla_put_failure_locked;
3239 		break;
3240 	case NL80211_IFTYPE_STATION:
3241 	case NL80211_IFTYPE_P2P_CLIENT:
3242 	case NL80211_IFTYPE_ADHOC: {
3243 		const u8 *ssid_ie;
3244 		if (!wdev->current_bss)
3245 			break;
3246 		rcu_read_lock();
3247 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3248 					       WLAN_EID_SSID);
3249 		if (ssid_ie &&
3250 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3251 			goto nla_put_failure_rcu_locked;
3252 		rcu_read_unlock();
3253 		break;
3254 		}
3255 	default:
3256 		/* nothing */
3257 		break;
3258 	}
3259 	wdev_unlock(wdev);
3260 
3261 	if (rdev->ops->get_txq_stats) {
3262 		struct cfg80211_txq_stats txqstats = {};
3263 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3264 
3265 		if (ret == 0 &&
3266 		    !nl80211_put_txq_stats(msg, &txqstats,
3267 					   NL80211_ATTR_TXQ_STATS))
3268 			goto nla_put_failure;
3269 	}
3270 
3271 	genlmsg_end(msg, hdr);
3272 	return 0;
3273 
3274  nla_put_failure_rcu_locked:
3275 	rcu_read_unlock();
3276  nla_put_failure_locked:
3277 	wdev_unlock(wdev);
3278  nla_put_failure:
3279 	genlmsg_cancel(msg, hdr);
3280 	return -EMSGSIZE;
3281 }
3282 
nl80211_dump_interface(struct sk_buff * skb,struct netlink_callback * cb)3283 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3284 {
3285 	int wp_idx = 0;
3286 	int if_idx = 0;
3287 	int wp_start = cb->args[0];
3288 	int if_start = cb->args[1];
3289 	int filter_wiphy = -1;
3290 	struct cfg80211_registered_device *rdev;
3291 	struct wireless_dev *wdev;
3292 	int ret;
3293 
3294 	rtnl_lock();
3295 	if (!cb->args[2]) {
3296 		struct nl80211_dump_wiphy_state state = {
3297 			.filter_wiphy = -1,
3298 		};
3299 
3300 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3301 		if (ret)
3302 			goto out_unlock;
3303 
3304 		filter_wiphy = state.filter_wiphy;
3305 
3306 		/*
3307 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3308 		 * value needed to determine that parsing is necessary.
3309 		 */
3310 		if (filter_wiphy >= 0)
3311 			cb->args[2] = filter_wiphy + 1;
3312 		else
3313 			cb->args[2] = -1;
3314 	} else if (cb->args[2] > 0) {
3315 		filter_wiphy = cb->args[2] - 1;
3316 	}
3317 
3318 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3319 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3320 			continue;
3321 		if (wp_idx < wp_start) {
3322 			wp_idx++;
3323 			continue;
3324 		}
3325 
3326 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3327 			continue;
3328 
3329 		if_idx = 0;
3330 
3331 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3332 			if (if_idx < if_start) {
3333 				if_idx++;
3334 				continue;
3335 			}
3336 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3337 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3338 					       rdev, wdev,
3339 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3340 				goto out;
3341 			}
3342 			if_idx++;
3343 		}
3344 
3345 		wp_idx++;
3346 	}
3347  out:
3348 	cb->args[0] = wp_idx;
3349 	cb->args[1] = if_idx;
3350 
3351 	ret = skb->len;
3352  out_unlock:
3353 	rtnl_unlock();
3354 
3355 	return ret;
3356 }
3357 
nl80211_get_interface(struct sk_buff * skb,struct genl_info * info)3358 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3359 {
3360 	struct sk_buff *msg;
3361 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3362 	struct wireless_dev *wdev = info->user_ptr[1];
3363 
3364 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3365 	if (!msg)
3366 		return -ENOMEM;
3367 
3368 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3369 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3370 		nlmsg_free(msg);
3371 		return -ENOBUFS;
3372 	}
3373 
3374 	return genlmsg_reply(msg, info);
3375 }
3376 
3377 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3378 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3379 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3380 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3381 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3382 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3383 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3384 };
3385 
parse_monitor_flags(struct nlattr * nla,u32 * mntrflags)3386 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3387 {
3388 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3389 	int flag;
3390 
3391 	*mntrflags = 0;
3392 
3393 	if (!nla)
3394 		return -EINVAL;
3395 
3396 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3397 		return -EINVAL;
3398 
3399 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3400 		if (flags[flag])
3401 			*mntrflags |= (1<<flag);
3402 
3403 	*mntrflags |= MONITOR_FLAG_CHANGED;
3404 
3405 	return 0;
3406 }
3407 
nl80211_parse_mon_options(struct cfg80211_registered_device * rdev,enum nl80211_iftype type,struct genl_info * info,struct vif_params * params)3408 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3409 				     enum nl80211_iftype type,
3410 				     struct genl_info *info,
3411 				     struct vif_params *params)
3412 {
3413 	bool change = false;
3414 	int err;
3415 
3416 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3417 		if (type != NL80211_IFTYPE_MONITOR)
3418 			return -EINVAL;
3419 
3420 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3421 					  &params->flags);
3422 		if (err)
3423 			return err;
3424 
3425 		change = true;
3426 	}
3427 
3428 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3429 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3430 		return -EOPNOTSUPP;
3431 
3432 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3433 		const u8 *mumimo_groups;
3434 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3435 
3436 		if (type != NL80211_IFTYPE_MONITOR)
3437 			return -EINVAL;
3438 
3439 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3440 			return -EOPNOTSUPP;
3441 
3442 		mumimo_groups =
3443 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3444 
3445 		/* bits 0 and 63 are reserved and must be zero */
3446 		if ((mumimo_groups[0] & BIT(0)) ||
3447 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3448 			return -EINVAL;
3449 
3450 		params->vht_mumimo_groups = mumimo_groups;
3451 		change = true;
3452 	}
3453 
3454 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3455 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3456 
3457 		if (type != NL80211_IFTYPE_MONITOR)
3458 			return -EINVAL;
3459 
3460 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3461 			return -EOPNOTSUPP;
3462 
3463 		params->vht_mumimo_follow_addr =
3464 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3465 		change = true;
3466 	}
3467 
3468 	return change ? 1 : 0;
3469 }
3470 
nl80211_valid_4addr(struct cfg80211_registered_device * rdev,struct net_device * netdev,u8 use_4addr,enum nl80211_iftype iftype)3471 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3472 			       struct net_device *netdev, u8 use_4addr,
3473 			       enum nl80211_iftype iftype)
3474 {
3475 	if (!use_4addr) {
3476 		if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3477 			return -EBUSY;
3478 		return 0;
3479 	}
3480 
3481 	switch (iftype) {
3482 	case NL80211_IFTYPE_AP_VLAN:
3483 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3484 			return 0;
3485 		break;
3486 	case NL80211_IFTYPE_STATION:
3487 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3488 			return 0;
3489 		break;
3490 	default:
3491 		break;
3492 	}
3493 
3494 	return -EOPNOTSUPP;
3495 }
3496 
nl80211_set_interface(struct sk_buff * skb,struct genl_info * info)3497 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3498 {
3499 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3500 	struct vif_params params;
3501 	int err;
3502 	enum nl80211_iftype otype, ntype;
3503 	struct net_device *dev = info->user_ptr[1];
3504 	bool change = false;
3505 
3506 	memset(&params, 0, sizeof(params));
3507 
3508 	otype = ntype = dev->ieee80211_ptr->iftype;
3509 
3510 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3511 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3512 		if (otype != ntype)
3513 			change = true;
3514 	}
3515 
3516 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3517 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3518 
3519 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3520 			return -EINVAL;
3521 		if (netif_running(dev))
3522 			return -EBUSY;
3523 
3524 		wdev_lock(wdev);
3525 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3526 			     IEEE80211_MAX_MESH_ID_LEN);
3527 		wdev->mesh_id_up_len =
3528 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3529 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3530 		       wdev->mesh_id_up_len);
3531 		wdev_unlock(wdev);
3532 	}
3533 
3534 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3535 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3536 		change = true;
3537 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3538 		if (err)
3539 			return err;
3540 	} else {
3541 		params.use_4addr = -1;
3542 	}
3543 
3544 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3545 	if (err < 0)
3546 		return err;
3547 	if (err > 0)
3548 		change = true;
3549 
3550 	if (change)
3551 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3552 	else
3553 		err = 0;
3554 
3555 	if (!err && params.use_4addr != -1)
3556 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3557 
3558 	if (change && !err) {
3559 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3560 
3561 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3562 	}
3563 
3564 	return err;
3565 }
3566 
nl80211_new_interface(struct sk_buff * skb,struct genl_info * info)3567 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3568 {
3569 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3570 	struct vif_params params;
3571 	struct wireless_dev *wdev;
3572 	struct sk_buff *msg;
3573 	int err;
3574 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3575 
3576 	/* to avoid failing a new interface creation due to pending removal */
3577 	cfg80211_destroy_ifaces(rdev);
3578 
3579 	memset(&params, 0, sizeof(params));
3580 
3581 	if (!info->attrs[NL80211_ATTR_IFNAME])
3582 		return -EINVAL;
3583 
3584 	if (info->attrs[NL80211_ATTR_IFTYPE])
3585 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3586 
3587 	if (!rdev->ops->add_virtual_intf)
3588 		return -EOPNOTSUPP;
3589 
3590 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3591 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3592 	    info->attrs[NL80211_ATTR_MAC]) {
3593 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3594 			   ETH_ALEN);
3595 		if (!is_valid_ether_addr(params.macaddr))
3596 			return -EADDRNOTAVAIL;
3597 	}
3598 
3599 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3600 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3601 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3602 		if (err)
3603 			return err;
3604 	}
3605 
3606 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3607 		return -EOPNOTSUPP;
3608 
3609 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3610 	if (err < 0)
3611 		return err;
3612 
3613 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3614 	if (!msg)
3615 		return -ENOMEM;
3616 
3617 	wdev = rdev_add_virtual_intf(rdev,
3618 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3619 				NET_NAME_USER, type, &params);
3620 	if (WARN_ON(!wdev)) {
3621 		nlmsg_free(msg);
3622 		return -EPROTO;
3623 	} else if (IS_ERR(wdev)) {
3624 		nlmsg_free(msg);
3625 		return PTR_ERR(wdev);
3626 	}
3627 
3628 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3629 		wdev->owner_nlportid = info->snd_portid;
3630 
3631 	switch (type) {
3632 	case NL80211_IFTYPE_MESH_POINT:
3633 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3634 			break;
3635 		wdev_lock(wdev);
3636 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3637 			     IEEE80211_MAX_MESH_ID_LEN);
3638 		wdev->mesh_id_up_len =
3639 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3640 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3641 		       wdev->mesh_id_up_len);
3642 		wdev_unlock(wdev);
3643 		break;
3644 	case NL80211_IFTYPE_NAN:
3645 	case NL80211_IFTYPE_P2P_DEVICE:
3646 		/*
3647 		 * P2P Device and NAN do not have a netdev, so don't go
3648 		 * through the netdev notifier and must be added here
3649 		 */
3650 		cfg80211_init_wdev(rdev, wdev);
3651 		break;
3652 	default:
3653 		break;
3654 	}
3655 
3656 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3657 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3658 		nlmsg_free(msg);
3659 		return -ENOBUFS;
3660 	}
3661 
3662 	return genlmsg_reply(msg, info);
3663 }
3664 
nl80211_del_interface(struct sk_buff * skb,struct genl_info * info)3665 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3666 {
3667 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3668 	struct wireless_dev *wdev = info->user_ptr[1];
3669 
3670 	if (!rdev->ops->del_virtual_intf)
3671 		return -EOPNOTSUPP;
3672 
3673 	/*
3674 	 * If we remove a wireless device without a netdev then clear
3675 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3676 	 * to check if it needs to do dev_put(). Otherwise it crashes
3677 	 * since the wdev has been freed, unlike with a netdev where
3678 	 * we need the dev_put() for the netdev to really be freed.
3679 	 */
3680 	if (!wdev->netdev)
3681 		info->user_ptr[1] = NULL;
3682 
3683 	return rdev_del_virtual_intf(rdev, wdev);
3684 }
3685 
nl80211_set_noack_map(struct sk_buff * skb,struct genl_info * info)3686 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3687 {
3688 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3689 	struct net_device *dev = info->user_ptr[1];
3690 	u16 noack_map;
3691 
3692 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3693 		return -EINVAL;
3694 
3695 	if (!rdev->ops->set_noack_map)
3696 		return -EOPNOTSUPP;
3697 
3698 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3699 
3700 	return rdev_set_noack_map(rdev, dev, noack_map);
3701 }
3702 
3703 struct get_key_cookie {
3704 	struct sk_buff *msg;
3705 	int error;
3706 	int idx;
3707 };
3708 
get_key_callback(void * c,struct key_params * params)3709 static void get_key_callback(void *c, struct key_params *params)
3710 {
3711 	struct nlattr *key;
3712 	struct get_key_cookie *cookie = c;
3713 
3714 	if ((params->key &&
3715 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3716 		     params->key_len, params->key)) ||
3717 	    (params->seq &&
3718 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3719 		     params->seq_len, params->seq)) ||
3720 	    (params->cipher &&
3721 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3722 			 params->cipher)))
3723 		goto nla_put_failure;
3724 
3725 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3726 	if (!key)
3727 		goto nla_put_failure;
3728 
3729 	if ((params->key &&
3730 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3731 		     params->key_len, params->key)) ||
3732 	    (params->seq &&
3733 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3734 		     params->seq_len, params->seq)) ||
3735 	    (params->cipher &&
3736 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3737 			 params->cipher)))
3738 		goto nla_put_failure;
3739 
3740 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3741 		goto nla_put_failure;
3742 
3743 	nla_nest_end(cookie->msg, key);
3744 
3745 	return;
3746  nla_put_failure:
3747 	cookie->error = 1;
3748 }
3749 
nl80211_get_key(struct sk_buff * skb,struct genl_info * info)3750 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3751 {
3752 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3753 	int err;
3754 	struct net_device *dev = info->user_ptr[1];
3755 	u8 key_idx = 0;
3756 	const u8 *mac_addr = NULL;
3757 	bool pairwise;
3758 	struct get_key_cookie cookie = {
3759 		.error = 0,
3760 	};
3761 	void *hdr;
3762 	struct sk_buff *msg;
3763 
3764 	if (info->attrs[NL80211_ATTR_KEY_IDX])
3765 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3766 
3767 	if (info->attrs[NL80211_ATTR_MAC])
3768 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3769 
3770 	pairwise = !!mac_addr;
3771 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3772 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3773 
3774 		if (kt != NL80211_KEYTYPE_GROUP &&
3775 		    kt != NL80211_KEYTYPE_PAIRWISE)
3776 			return -EINVAL;
3777 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3778 	}
3779 
3780 	if (!rdev->ops->get_key)
3781 		return -EOPNOTSUPP;
3782 
3783 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3784 		return -ENOENT;
3785 
3786 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3787 	if (!msg)
3788 		return -ENOMEM;
3789 
3790 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3791 			     NL80211_CMD_NEW_KEY);
3792 	if (!hdr)
3793 		goto nla_put_failure;
3794 
3795 	cookie.msg = msg;
3796 	cookie.idx = key_idx;
3797 
3798 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3799 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3800 		goto nla_put_failure;
3801 	if (mac_addr &&
3802 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3803 		goto nla_put_failure;
3804 
3805 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3806 			   get_key_callback);
3807 
3808 	if (err)
3809 		goto free_msg;
3810 
3811 	if (cookie.error)
3812 		goto nla_put_failure;
3813 
3814 	genlmsg_end(msg, hdr);
3815 	return genlmsg_reply(msg, info);
3816 
3817  nla_put_failure:
3818 	err = -ENOBUFS;
3819  free_msg:
3820 	nlmsg_free(msg);
3821 	return err;
3822 }
3823 
nl80211_set_key(struct sk_buff * skb,struct genl_info * info)3824 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3825 {
3826 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3827 	struct key_parse key;
3828 	int err;
3829 	struct net_device *dev = info->user_ptr[1];
3830 
3831 	err = nl80211_parse_key(info, &key);
3832 	if (err)
3833 		return err;
3834 
3835 	if (key.idx < 0)
3836 		return -EINVAL;
3837 
3838 	/* Only support setting default key and
3839 	 * Extended Key ID action NL80211_KEY_SET_TX.
3840 	 */
3841 	if (!key.def && !key.defmgmt &&
3842 	    !(key.p.mode == NL80211_KEY_SET_TX))
3843 		return -EINVAL;
3844 
3845 	wdev_lock(dev->ieee80211_ptr);
3846 
3847 	if (key.def) {
3848 		if (!rdev->ops->set_default_key) {
3849 			err = -EOPNOTSUPP;
3850 			goto out;
3851 		}
3852 
3853 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3854 		if (err)
3855 			goto out;
3856 
3857 		err = rdev_set_default_key(rdev, dev, key.idx,
3858 						 key.def_uni, key.def_multi);
3859 
3860 		if (err)
3861 			goto out;
3862 
3863 #ifdef CONFIG_CFG80211_WEXT
3864 		dev->ieee80211_ptr->wext.default_key = key.idx;
3865 #endif
3866 	} else if (key.defmgmt) {
3867 		if (key.def_uni || !key.def_multi) {
3868 			err = -EINVAL;
3869 			goto out;
3870 		}
3871 
3872 		if (!rdev->ops->set_default_mgmt_key) {
3873 			err = -EOPNOTSUPP;
3874 			goto out;
3875 		}
3876 
3877 		err = nl80211_key_allowed(dev->ieee80211_ptr);
3878 		if (err)
3879 			goto out;
3880 
3881 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3882 		if (err)
3883 			goto out;
3884 
3885 #ifdef CONFIG_CFG80211_WEXT
3886 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3887 #endif
3888 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
3889 		   wiphy_ext_feature_isset(&rdev->wiphy,
3890 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
3891 		u8 *mac_addr = NULL;
3892 
3893 		if (info->attrs[NL80211_ATTR_MAC])
3894 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3895 
3896 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
3897 			err = -EINVAL;
3898 			goto out;
3899 		}
3900 
3901 		err = rdev_add_key(rdev, dev, key.idx,
3902 				   NL80211_KEYTYPE_PAIRWISE,
3903 				   mac_addr, &key.p);
3904 	} else {
3905 		err = -EINVAL;
3906 	}
3907  out:
3908 	wdev_unlock(dev->ieee80211_ptr);
3909 
3910 	return err;
3911 }
3912 
nl80211_new_key(struct sk_buff * skb,struct genl_info * info)3913 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3914 {
3915 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3916 	int err;
3917 	struct net_device *dev = info->user_ptr[1];
3918 	struct key_parse key;
3919 	const u8 *mac_addr = NULL;
3920 
3921 	err = nl80211_parse_key(info, &key);
3922 	if (err)
3923 		return err;
3924 
3925 	if (!key.p.key)
3926 		return -EINVAL;
3927 
3928 	if (info->attrs[NL80211_ATTR_MAC])
3929 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3930 
3931 	if (key.type == -1) {
3932 		if (mac_addr)
3933 			key.type = NL80211_KEYTYPE_PAIRWISE;
3934 		else
3935 			key.type = NL80211_KEYTYPE_GROUP;
3936 	}
3937 
3938 	/* for now */
3939 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3940 	    key.type != NL80211_KEYTYPE_GROUP)
3941 		return -EINVAL;
3942 
3943 	if (!rdev->ops->add_key)
3944 		return -EOPNOTSUPP;
3945 
3946 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3947 					   key.type == NL80211_KEYTYPE_PAIRWISE,
3948 					   mac_addr))
3949 		return -EINVAL;
3950 
3951 	wdev_lock(dev->ieee80211_ptr);
3952 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3953 	if (!err)
3954 		err = rdev_add_key(rdev, dev, key.idx,
3955 				   key.type == NL80211_KEYTYPE_PAIRWISE,
3956 				    mac_addr, &key.p);
3957 	wdev_unlock(dev->ieee80211_ptr);
3958 
3959 	return err;
3960 }
3961 
nl80211_del_key(struct sk_buff * skb,struct genl_info * info)3962 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3963 {
3964 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3965 	int err;
3966 	struct net_device *dev = info->user_ptr[1];
3967 	u8 *mac_addr = NULL;
3968 	struct key_parse key;
3969 
3970 	err = nl80211_parse_key(info, &key);
3971 	if (err)
3972 		return err;
3973 
3974 	if (info->attrs[NL80211_ATTR_MAC])
3975 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3976 
3977 	if (key.type == -1) {
3978 		if (mac_addr)
3979 			key.type = NL80211_KEYTYPE_PAIRWISE;
3980 		else
3981 			key.type = NL80211_KEYTYPE_GROUP;
3982 	}
3983 
3984 	/* for now */
3985 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3986 	    key.type != NL80211_KEYTYPE_GROUP)
3987 		return -EINVAL;
3988 
3989 	if (!rdev->ops->del_key)
3990 		return -EOPNOTSUPP;
3991 
3992 	wdev_lock(dev->ieee80211_ptr);
3993 	err = nl80211_key_allowed(dev->ieee80211_ptr);
3994 
3995 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3996 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3997 		err = -ENOENT;
3998 
3999 	if (!err)
4000 		err = rdev_del_key(rdev, dev, key.idx,
4001 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4002 				   mac_addr);
4003 
4004 #ifdef CONFIG_CFG80211_WEXT
4005 	if (!err) {
4006 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
4007 			dev->ieee80211_ptr->wext.default_key = -1;
4008 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4009 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4010 	}
4011 #endif
4012 	wdev_unlock(dev->ieee80211_ptr);
4013 
4014 	return err;
4015 }
4016 
4017 /* This function returns an error or the number of nested attributes */
validate_acl_mac_addrs(struct nlattr * nl_attr)4018 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4019 {
4020 	struct nlattr *attr;
4021 	int n_entries = 0, tmp;
4022 
4023 	nla_for_each_nested(attr, nl_attr, tmp) {
4024 		if (nla_len(attr) != ETH_ALEN)
4025 			return -EINVAL;
4026 
4027 		n_entries++;
4028 	}
4029 
4030 	return n_entries;
4031 }
4032 
4033 /*
4034  * This function parses ACL information and allocates memory for ACL data.
4035  * On successful return, the calling function is responsible to free the
4036  * ACL buffer returned by this function.
4037  */
parse_acl_data(struct wiphy * wiphy,struct genl_info * info)4038 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4039 						struct genl_info *info)
4040 {
4041 	enum nl80211_acl_policy acl_policy;
4042 	struct nlattr *attr;
4043 	struct cfg80211_acl_data *acl;
4044 	int i = 0, n_entries, tmp;
4045 
4046 	if (!wiphy->max_acl_mac_addrs)
4047 		return ERR_PTR(-EOPNOTSUPP);
4048 
4049 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4050 		return ERR_PTR(-EINVAL);
4051 
4052 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4053 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4054 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4055 		return ERR_PTR(-EINVAL);
4056 
4057 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4058 		return ERR_PTR(-EINVAL);
4059 
4060 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4061 	if (n_entries < 0)
4062 		return ERR_PTR(n_entries);
4063 
4064 	if (n_entries > wiphy->max_acl_mac_addrs)
4065 		return ERR_PTR(-ENOTSUPP);
4066 
4067 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4068 	if (!acl)
4069 		return ERR_PTR(-ENOMEM);
4070 
4071 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4072 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4073 		i++;
4074 	}
4075 
4076 	acl->n_acl_entries = n_entries;
4077 	acl->acl_policy = acl_policy;
4078 
4079 	return acl;
4080 }
4081 
nl80211_set_mac_acl(struct sk_buff * skb,struct genl_info * info)4082 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4083 {
4084 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4085 	struct net_device *dev = info->user_ptr[1];
4086 	struct cfg80211_acl_data *acl;
4087 	int err;
4088 
4089 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4090 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4091 		return -EOPNOTSUPP;
4092 
4093 	if (!dev->ieee80211_ptr->beacon_interval)
4094 		return -EINVAL;
4095 
4096 	acl = parse_acl_data(&rdev->wiphy, info);
4097 	if (IS_ERR(acl))
4098 		return PTR_ERR(acl);
4099 
4100 	err = rdev_set_mac_acl(rdev, dev, acl);
4101 
4102 	kfree(acl);
4103 
4104 	return err;
4105 }
4106 
rateset_to_mask(struct ieee80211_supported_band * sband,u8 * rates,u8 rates_len)4107 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4108 			   u8 *rates, u8 rates_len)
4109 {
4110 	u8 i;
4111 	u32 mask = 0;
4112 
4113 	for (i = 0; i < rates_len; i++) {
4114 		int rate = (rates[i] & 0x7f) * 5;
4115 		int ridx;
4116 
4117 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4118 			struct ieee80211_rate *srate =
4119 				&sband->bitrates[ridx];
4120 			if (rate == srate->bitrate) {
4121 				mask |= 1 << ridx;
4122 				break;
4123 			}
4124 		}
4125 		if (ridx == sband->n_bitrates)
4126 			return 0; /* rate not found */
4127 	}
4128 
4129 	return mask;
4130 }
4131 
ht_rateset_to_mask(struct ieee80211_supported_band * sband,u8 * rates,u8 rates_len,u8 mcs[IEEE80211_HT_MCS_MASK_LEN])4132 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4133 			       u8 *rates, u8 rates_len,
4134 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4135 {
4136 	u8 i;
4137 
4138 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4139 
4140 	for (i = 0; i < rates_len; i++) {
4141 		int ridx, rbit;
4142 
4143 		ridx = rates[i] / 8;
4144 		rbit = BIT(rates[i] % 8);
4145 
4146 		/* check validity */
4147 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4148 			return false;
4149 
4150 		/* check availability */
4151 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4152 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4153 			mcs[ridx] |= rbit;
4154 		else
4155 			return false;
4156 	}
4157 
4158 	return true;
4159 }
4160 
vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)4161 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4162 {
4163 	u16 mcs_mask = 0;
4164 
4165 	switch (vht_mcs_map) {
4166 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4167 		break;
4168 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4169 		mcs_mask = 0x00FF;
4170 		break;
4171 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4172 		mcs_mask = 0x01FF;
4173 		break;
4174 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4175 		mcs_mask = 0x03FF;
4176 		break;
4177 	default:
4178 		break;
4179 	}
4180 
4181 	return mcs_mask;
4182 }
4183 
vht_build_mcs_mask(u16 vht_mcs_map,u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])4184 static void vht_build_mcs_mask(u16 vht_mcs_map,
4185 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4186 {
4187 	u8 nss;
4188 
4189 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4190 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4191 		vht_mcs_map >>= 2;
4192 	}
4193 }
4194 
vht_set_mcs_mask(struct ieee80211_supported_band * sband,struct nl80211_txrate_vht * txrate,u16 mcs[NL80211_VHT_NSS_MAX])4195 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4196 			     struct nl80211_txrate_vht *txrate,
4197 			     u16 mcs[NL80211_VHT_NSS_MAX])
4198 {
4199 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4200 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4201 	u8 i;
4202 
4203 	if (!sband->vht_cap.vht_supported)
4204 		return false;
4205 
4206 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4207 
4208 	/* Build vht_mcs_mask from VHT capabilities */
4209 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4210 
4211 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4212 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4213 			mcs[i] = txrate->mcs[i];
4214 		else
4215 			return false;
4216 	}
4217 
4218 	return true;
4219 }
4220 
4221 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4222 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4223 				    .len = NL80211_MAX_SUPP_RATES },
4224 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
4225 				.len = NL80211_MAX_SUPP_HT_RATES },
4226 	[NL80211_TXRATE_VHT] = {
4227 		.type = NLA_EXACT_LEN_WARN,
4228 		.len = sizeof(struct nl80211_txrate_vht),
4229 	},
4230 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
4231 };
4232 
nl80211_parse_tx_bitrate_mask(struct genl_info * info,struct cfg80211_bitrate_mask * mask)4233 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4234 					 struct cfg80211_bitrate_mask *mask)
4235 {
4236 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4237 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4238 	int rem, i;
4239 	struct nlattr *tx_rates;
4240 	struct ieee80211_supported_band *sband;
4241 	u16 vht_tx_mcs_map;
4242 
4243 	memset(mask, 0, sizeof(*mask));
4244 	/* Default to all rates enabled */
4245 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4246 		sband = rdev->wiphy.bands[i];
4247 
4248 		if (!sband)
4249 			continue;
4250 
4251 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4252 		memcpy(mask->control[i].ht_mcs,
4253 		       sband->ht_cap.mcs.rx_mask,
4254 		       sizeof(mask->control[i].ht_mcs));
4255 
4256 		if (!sband->vht_cap.vht_supported)
4257 			continue;
4258 
4259 		vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4260 		vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4261 	}
4262 
4263 	/* if no rates are given set it back to the defaults */
4264 	if (!info->attrs[NL80211_ATTR_TX_RATES])
4265 		goto out;
4266 
4267 	/* The nested attribute uses enum nl80211_band as the index. This maps
4268 	 * directly to the enum nl80211_band values used in cfg80211.
4269 	 */
4270 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4271 	nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
4272 		enum nl80211_band band = nla_type(tx_rates);
4273 		int err;
4274 
4275 		if (band < 0 || band >= NUM_NL80211_BANDS)
4276 			return -EINVAL;
4277 		sband = rdev->wiphy.bands[band];
4278 		if (sband == NULL)
4279 			return -EINVAL;
4280 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4281 						  tx_rates,
4282 						  nl80211_txattr_policy,
4283 						  info->extack);
4284 		if (err)
4285 			return err;
4286 		if (tb[NL80211_TXRATE_LEGACY]) {
4287 			mask->control[band].legacy = rateset_to_mask(
4288 				sband,
4289 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4290 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4291 			if ((mask->control[band].legacy == 0) &&
4292 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4293 				return -EINVAL;
4294 		}
4295 		if (tb[NL80211_TXRATE_HT]) {
4296 			if (!ht_rateset_to_mask(
4297 					sband,
4298 					nla_data(tb[NL80211_TXRATE_HT]),
4299 					nla_len(tb[NL80211_TXRATE_HT]),
4300 					mask->control[band].ht_mcs))
4301 				return -EINVAL;
4302 		}
4303 		if (tb[NL80211_TXRATE_VHT]) {
4304 			if (!vht_set_mcs_mask(
4305 					sband,
4306 					nla_data(tb[NL80211_TXRATE_VHT]),
4307 					mask->control[band].vht_mcs))
4308 				return -EINVAL;
4309 		}
4310 		if (tb[NL80211_TXRATE_GI]) {
4311 			mask->control[band].gi =
4312 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4313 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4314 				return -EINVAL;
4315 		}
4316 
4317 		if (mask->control[band].legacy == 0) {
4318 			/* don't allow empty legacy rates if HT or VHT
4319 			 * are not even supported.
4320 			 */
4321 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4322 			      rdev->wiphy.bands[band]->vht_cap.vht_supported))
4323 				return -EINVAL;
4324 
4325 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4326 				if (mask->control[band].ht_mcs[i])
4327 					goto out;
4328 
4329 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4330 				if (mask->control[band].vht_mcs[i])
4331 					goto out;
4332 
4333 			/* legacy and mcs rates may not be both empty */
4334 			return -EINVAL;
4335 		}
4336 	}
4337 
4338 out:
4339 	return 0;
4340 }
4341 
validate_beacon_tx_rate(struct cfg80211_registered_device * rdev,enum nl80211_band band,struct cfg80211_bitrate_mask * beacon_rate)4342 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4343 				   enum nl80211_band band,
4344 				   struct cfg80211_bitrate_mask *beacon_rate)
4345 {
4346 	u32 count_ht, count_vht, i;
4347 	u32 rate = beacon_rate->control[band].legacy;
4348 
4349 	/* Allow only one rate */
4350 	if (hweight32(rate) > 1)
4351 		return -EINVAL;
4352 
4353 	count_ht = 0;
4354 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4355 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4356 			return -EINVAL;
4357 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4358 			count_ht++;
4359 			if (count_ht > 1)
4360 				return -EINVAL;
4361 		}
4362 		if (count_ht && rate)
4363 			return -EINVAL;
4364 	}
4365 
4366 	count_vht = 0;
4367 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4368 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4369 			return -EINVAL;
4370 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4371 			count_vht++;
4372 			if (count_vht > 1)
4373 				return -EINVAL;
4374 		}
4375 		if (count_vht && rate)
4376 			return -EINVAL;
4377 	}
4378 
4379 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4380 		return -EINVAL;
4381 
4382 	if (rate &&
4383 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4384 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4385 		return -EINVAL;
4386 	if (count_ht &&
4387 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4388 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4389 		return -EINVAL;
4390 	if (count_vht &&
4391 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4392 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4393 		return -EINVAL;
4394 
4395 	return 0;
4396 }
4397 
nl80211_parse_beacon(struct cfg80211_registered_device * rdev,struct nlattr * attrs[],struct cfg80211_beacon_data * bcn)4398 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4399 				struct nlattr *attrs[],
4400 				struct cfg80211_beacon_data *bcn)
4401 {
4402 	bool haveinfo = false;
4403 	int err;
4404 
4405 	memset(bcn, 0, sizeof(*bcn));
4406 
4407 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4408 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4409 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4410 		if (!bcn->head_len)
4411 			return -EINVAL;
4412 		haveinfo = true;
4413 	}
4414 
4415 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4416 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4417 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4418 		haveinfo = true;
4419 	}
4420 
4421 	if (!haveinfo)
4422 		return -EINVAL;
4423 
4424 	if (attrs[NL80211_ATTR_IE]) {
4425 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4426 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4427 	}
4428 
4429 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4430 		bcn->proberesp_ies =
4431 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4432 		bcn->proberesp_ies_len =
4433 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4434 	}
4435 
4436 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4437 		bcn->assocresp_ies =
4438 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4439 		bcn->assocresp_ies_len =
4440 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4441 	}
4442 
4443 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
4444 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4445 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4446 	}
4447 
4448 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4449 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4450 
4451 		err = nla_parse_nested_deprecated(tb,
4452 						  NL80211_FTM_RESP_ATTR_MAX,
4453 						  attrs[NL80211_ATTR_FTM_RESPONDER],
4454 						  NULL, NULL);
4455 		if (err)
4456 			return err;
4457 
4458 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4459 		    wiphy_ext_feature_isset(&rdev->wiphy,
4460 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4461 			bcn->ftm_responder = 1;
4462 		else
4463 			return -EOPNOTSUPP;
4464 
4465 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4466 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4467 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4468 		}
4469 
4470 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4471 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4472 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4473 		}
4474 	} else {
4475 		bcn->ftm_responder = -1;
4476 	}
4477 
4478 	return 0;
4479 }
4480 
nl80211_parse_he_obss_pd(struct nlattr * attrs,struct ieee80211_he_obss_pd * he_obss_pd)4481 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4482 				    struct ieee80211_he_obss_pd *he_obss_pd)
4483 {
4484 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4485 	int err;
4486 
4487 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4488 			       he_obss_pd_policy, NULL);
4489 	if (err)
4490 		return err;
4491 
4492 	if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4493 	    !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4494 		return -EINVAL;
4495 
4496 	he_obss_pd->min_offset =
4497 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4498 	he_obss_pd->max_offset =
4499 		nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4500 
4501 	if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4502 		return -EINVAL;
4503 
4504 	he_obss_pd->enable = true;
4505 
4506 	return 0;
4507 }
4508 
nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings * params,const u8 * rates)4509 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4510 					    const u8 *rates)
4511 {
4512 	int i;
4513 
4514 	if (!rates)
4515 		return;
4516 
4517 	for (i = 0; i < rates[1]; i++) {
4518 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4519 			params->ht_required = true;
4520 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4521 			params->vht_required = true;
4522 	}
4523 }
4524 
4525 /*
4526  * Since the nl80211 API didn't include, from the beginning, attributes about
4527  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4528  * benefit of drivers that rebuild IEs in the firmware.
4529  */
nl80211_calculate_ap_params(struct cfg80211_ap_settings * params)4530 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4531 {
4532 	const struct cfg80211_beacon_data *bcn = &params->beacon;
4533 	size_t ies_len = bcn->tail_len;
4534 	const u8 *ies = bcn->tail;
4535 	const u8 *rates;
4536 	const u8 *cap;
4537 
4538 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4539 	nl80211_check_ap_rate_selectors(params, rates);
4540 
4541 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4542 	nl80211_check_ap_rate_selectors(params, rates);
4543 
4544 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4545 	if (cap && cap[1] >= sizeof(*params->ht_cap))
4546 		params->ht_cap = (void *)(cap + 2);
4547 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4548 	if (cap && cap[1] >= sizeof(*params->vht_cap))
4549 		params->vht_cap = (void *)(cap + 2);
4550 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4551 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4552 		params->he_cap = (void *)(cap + 3);
4553 }
4554 
nl80211_get_ap_channel(struct cfg80211_registered_device * rdev,struct cfg80211_ap_settings * params)4555 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4556 				   struct cfg80211_ap_settings *params)
4557 {
4558 	struct wireless_dev *wdev;
4559 	bool ret = false;
4560 
4561 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4562 		if (wdev->iftype != NL80211_IFTYPE_AP &&
4563 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
4564 			continue;
4565 
4566 		if (!wdev->preset_chandef.chan)
4567 			continue;
4568 
4569 		params->chandef = wdev->preset_chandef;
4570 		ret = true;
4571 		break;
4572 	}
4573 
4574 	return ret;
4575 }
4576 
nl80211_valid_auth_type(struct cfg80211_registered_device * rdev,enum nl80211_auth_type auth_type,enum nl80211_commands cmd)4577 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4578 				    enum nl80211_auth_type auth_type,
4579 				    enum nl80211_commands cmd)
4580 {
4581 	if (auth_type > NL80211_AUTHTYPE_MAX)
4582 		return false;
4583 
4584 	switch (cmd) {
4585 	case NL80211_CMD_AUTHENTICATE:
4586 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4587 		    auth_type == NL80211_AUTHTYPE_SAE)
4588 			return false;
4589 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
4590 					     NL80211_EXT_FEATURE_FILS_STA) &&
4591 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4592 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4593 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
4594 			return false;
4595 		return true;
4596 	case NL80211_CMD_CONNECT:
4597 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4598 		    !wiphy_ext_feature_isset(&rdev->wiphy,
4599 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4600 		    auth_type == NL80211_AUTHTYPE_SAE)
4601 			return false;
4602 
4603 		/* FILS with SK PFS or PK not supported yet */
4604 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4605 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4606 			return false;
4607 		if (!wiphy_ext_feature_isset(
4608 			    &rdev->wiphy,
4609 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4610 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
4611 			return false;
4612 		return true;
4613 	case NL80211_CMD_START_AP:
4614 		/* SAE not supported yet */
4615 		if (auth_type == NL80211_AUTHTYPE_SAE)
4616 			return false;
4617 		/* FILS not supported yet */
4618 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4619 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4620 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
4621 			return false;
4622 		return true;
4623 	default:
4624 		return false;
4625 	}
4626 }
4627 
nl80211_start_ap(struct sk_buff * skb,struct genl_info * info)4628 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4629 {
4630 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4631 	struct net_device *dev = info->user_ptr[1];
4632 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4633 	struct cfg80211_ap_settings params;
4634 	int err;
4635 
4636 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4637 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4638 		return -EOPNOTSUPP;
4639 
4640 	if (!rdev->ops->start_ap)
4641 		return -EOPNOTSUPP;
4642 
4643 	if (wdev->beacon_interval)
4644 		return -EALREADY;
4645 
4646 	memset(&params, 0, sizeof(params));
4647 
4648 	/* these are required for START_AP */
4649 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4650 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4651 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
4652 		return -EINVAL;
4653 
4654 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4655 	if (err)
4656 		return err;
4657 
4658 	params.beacon_interval =
4659 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4660 	params.dtim_period =
4661 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4662 
4663 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4664 					   params.beacon_interval);
4665 	if (err)
4666 		return err;
4667 
4668 	/*
4669 	 * In theory, some of these attributes should be required here
4670 	 * but since they were not used when the command was originally
4671 	 * added, keep them optional for old user space programs to let
4672 	 * them continue to work with drivers that do not need the
4673 	 * additional information -- drivers must check!
4674 	 */
4675 	if (info->attrs[NL80211_ATTR_SSID]) {
4676 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4677 		params.ssid_len =
4678 			nla_len(info->attrs[NL80211_ATTR_SSID]);
4679 		if (params.ssid_len == 0 ||
4680 		    params.ssid_len > IEEE80211_MAX_SSID_LEN)
4681 			return -EINVAL;
4682 	}
4683 
4684 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4685 		params.hidden_ssid = nla_get_u32(
4686 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4687 
4688 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4689 
4690 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4691 		params.auth_type = nla_get_u32(
4692 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
4693 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
4694 					     NL80211_CMD_START_AP))
4695 			return -EINVAL;
4696 	} else
4697 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4698 
4699 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
4700 				      NL80211_MAX_NR_CIPHER_SUITES);
4701 	if (err)
4702 		return err;
4703 
4704 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4705 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4706 			return -EOPNOTSUPP;
4707 		params.inactivity_timeout = nla_get_u16(
4708 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4709 	}
4710 
4711 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4712 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4713 			return -EINVAL;
4714 		params.p2p_ctwindow =
4715 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4716 		if (params.p2p_ctwindow != 0 &&
4717 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4718 			return -EINVAL;
4719 	}
4720 
4721 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4722 		u8 tmp;
4723 
4724 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4725 			return -EINVAL;
4726 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4727 		params.p2p_opp_ps = tmp;
4728 		if (params.p2p_opp_ps != 0 &&
4729 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4730 			return -EINVAL;
4731 	}
4732 
4733 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4734 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
4735 		if (err)
4736 			return err;
4737 	} else if (wdev->preset_chandef.chan) {
4738 		params.chandef = wdev->preset_chandef;
4739 	} else if (!nl80211_get_ap_channel(rdev, &params))
4740 		return -EINVAL;
4741 
4742 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4743 					   wdev->iftype))
4744 		return -EINVAL;
4745 
4746 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
4747 		err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4748 		if (err)
4749 			return err;
4750 
4751 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4752 					      &params.beacon_rate);
4753 		if (err)
4754 			return err;
4755 	}
4756 
4757 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4758 		params.smps_mode =
4759 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4760 		switch (params.smps_mode) {
4761 		case NL80211_SMPS_OFF:
4762 			break;
4763 		case NL80211_SMPS_STATIC:
4764 			if (!(rdev->wiphy.features &
4765 			      NL80211_FEATURE_STATIC_SMPS))
4766 				return -EINVAL;
4767 			break;
4768 		case NL80211_SMPS_DYNAMIC:
4769 			if (!(rdev->wiphy.features &
4770 			      NL80211_FEATURE_DYNAMIC_SMPS))
4771 				return -EINVAL;
4772 			break;
4773 		default:
4774 			return -EINVAL;
4775 		}
4776 	} else {
4777 		params.smps_mode = NL80211_SMPS_OFF;
4778 	}
4779 
4780 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4781 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4782 		return -EOPNOTSUPP;
4783 
4784 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4785 		params.acl = parse_acl_data(&rdev->wiphy, info);
4786 		if (IS_ERR(params.acl))
4787 			return PTR_ERR(params.acl);
4788 	}
4789 
4790 	params.twt_responder =
4791 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
4792 
4793 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
4794 		err = nl80211_parse_he_obss_pd(
4795 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
4796 					&params.he_obss_pd);
4797 		if (err)
4798 			return err;
4799 	}
4800 
4801 	nl80211_calculate_ap_params(&params);
4802 
4803 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
4804 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
4805 
4806 	wdev_lock(wdev);
4807 	err = rdev_start_ap(rdev, dev, &params);
4808 	if (!err) {
4809 		wdev->preset_chandef = params.chandef;
4810 		wdev->beacon_interval = params.beacon_interval;
4811 		wdev->chandef = params.chandef;
4812 		wdev->ssid_len = params.ssid_len;
4813 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4814 
4815 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4816 			wdev->conn_owner_nlportid = info->snd_portid;
4817 	}
4818 	wdev_unlock(wdev);
4819 
4820 	kfree(params.acl);
4821 
4822 	return err;
4823 }
4824 
nl80211_set_beacon(struct sk_buff * skb,struct genl_info * info)4825 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4826 {
4827 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4828 	struct net_device *dev = info->user_ptr[1];
4829 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4830 	struct cfg80211_beacon_data params;
4831 	int err;
4832 
4833 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4834 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4835 		return -EOPNOTSUPP;
4836 
4837 	if (!rdev->ops->change_beacon)
4838 		return -EOPNOTSUPP;
4839 
4840 	if (!wdev->beacon_interval)
4841 		return -EINVAL;
4842 
4843 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
4844 	if (err)
4845 		return err;
4846 
4847 	wdev_lock(wdev);
4848 	err = rdev_change_beacon(rdev, dev, &params);
4849 	wdev_unlock(wdev);
4850 
4851 	return err;
4852 }
4853 
nl80211_stop_ap(struct sk_buff * skb,struct genl_info * info)4854 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4855 {
4856 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4857 	struct net_device *dev = info->user_ptr[1];
4858 
4859 	return cfg80211_stop_ap(rdev, dev, false);
4860 }
4861 
4862 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4863 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4864 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4865 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4866 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4867 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4868 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4869 };
4870 
parse_station_flags(struct genl_info * info,enum nl80211_iftype iftype,struct station_parameters * params)4871 static int parse_station_flags(struct genl_info *info,
4872 			       enum nl80211_iftype iftype,
4873 			       struct station_parameters *params)
4874 {
4875 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4876 	struct nlattr *nla;
4877 	int flag;
4878 
4879 	/*
4880 	 * Try parsing the new attribute first so userspace
4881 	 * can specify both for older kernels.
4882 	 */
4883 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4884 	if (nla) {
4885 		struct nl80211_sta_flag_update *sta_flags;
4886 
4887 		sta_flags = nla_data(nla);
4888 		params->sta_flags_mask = sta_flags->mask;
4889 		params->sta_flags_set = sta_flags->set;
4890 		params->sta_flags_set &= params->sta_flags_mask;
4891 		if ((params->sta_flags_mask |
4892 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4893 			return -EINVAL;
4894 		return 0;
4895 	}
4896 
4897 	/* if present, parse the old attribute */
4898 
4899 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4900 	if (!nla)
4901 		return 0;
4902 
4903 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
4904 		return -EINVAL;
4905 
4906 	/*
4907 	 * Only allow certain flags for interface types so that
4908 	 * other attributes are silently ignored. Remember that
4909 	 * this is backward compatibility code with old userspace
4910 	 * and shouldn't be hit in other cases anyway.
4911 	 */
4912 	switch (iftype) {
4913 	case NL80211_IFTYPE_AP:
4914 	case NL80211_IFTYPE_AP_VLAN:
4915 	case NL80211_IFTYPE_P2P_GO:
4916 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4917 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4918 					 BIT(NL80211_STA_FLAG_WME) |
4919 					 BIT(NL80211_STA_FLAG_MFP);
4920 		break;
4921 	case NL80211_IFTYPE_P2P_CLIENT:
4922 	case NL80211_IFTYPE_STATION:
4923 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4924 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
4925 		break;
4926 	case NL80211_IFTYPE_MESH_POINT:
4927 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4928 					 BIT(NL80211_STA_FLAG_MFP) |
4929 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
4930 		break;
4931 	default:
4932 		return -EINVAL;
4933 	}
4934 
4935 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4936 		if (flags[flag]) {
4937 			params->sta_flags_set |= (1<<flag);
4938 
4939 			/* no longer support new API additions in old API */
4940 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4941 				return -EINVAL;
4942 		}
4943 	}
4944 
4945 	return 0;
4946 }
4947 
nl80211_put_sta_rate(struct sk_buff * msg,struct rate_info * info,int attr)4948 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
4949 {
4950 	struct nlattr *rate;
4951 	u32 bitrate;
4952 	u16 bitrate_compat;
4953 	enum nl80211_rate_info rate_flg;
4954 
4955 	rate = nla_nest_start_noflag(msg, attr);
4956 	if (!rate)
4957 		return false;
4958 
4959 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4960 	bitrate = cfg80211_calculate_bitrate(info);
4961 	/* report 16-bit bitrate only if we can */
4962 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4963 	if (bitrate > 0 &&
4964 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4965 		return false;
4966 	if (bitrate_compat > 0 &&
4967 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4968 		return false;
4969 
4970 	switch (info->bw) {
4971 	case RATE_INFO_BW_5:
4972 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4973 		break;
4974 	case RATE_INFO_BW_10:
4975 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4976 		break;
4977 	default:
4978 		WARN_ON(1);
4979 		/* fall through */
4980 	case RATE_INFO_BW_20:
4981 		rate_flg = 0;
4982 		break;
4983 	case RATE_INFO_BW_40:
4984 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4985 		break;
4986 	case RATE_INFO_BW_80:
4987 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4988 		break;
4989 	case RATE_INFO_BW_160:
4990 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4991 		break;
4992 	case RATE_INFO_BW_HE_RU:
4993 		rate_flg = 0;
4994 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
4995 	}
4996 
4997 	if (rate_flg && nla_put_flag(msg, rate_flg))
4998 		return false;
4999 
5000 	if (info->flags & RATE_INFO_FLAGS_MCS) {
5001 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5002 			return false;
5003 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5004 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5005 			return false;
5006 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5007 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5008 			return false;
5009 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5010 			return false;
5011 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5012 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5013 			return false;
5014 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5015 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5016 			return false;
5017 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5018 			return false;
5019 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5020 			return false;
5021 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5022 			return false;
5023 		if (info->bw == RATE_INFO_BW_HE_RU &&
5024 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5025 			       info->he_ru_alloc))
5026 			return false;
5027 	}
5028 
5029 	nla_nest_end(msg, rate);
5030 	return true;
5031 }
5032 
nl80211_put_signal(struct sk_buff * msg,u8 mask,s8 * signal,int id)5033 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5034 			       int id)
5035 {
5036 	void *attr;
5037 	int i = 0;
5038 
5039 	if (!mask)
5040 		return true;
5041 
5042 	attr = nla_nest_start_noflag(msg, id);
5043 	if (!attr)
5044 		return false;
5045 
5046 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5047 		if (!(mask & BIT(i)))
5048 			continue;
5049 
5050 		if (nla_put_u8(msg, i, signal[i]))
5051 			return false;
5052 	}
5053 
5054 	nla_nest_end(msg, attr);
5055 
5056 	return true;
5057 }
5058 
nl80211_send_station(struct sk_buff * msg,u32 cmd,u32 portid,u32 seq,int flags,struct cfg80211_registered_device * rdev,struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo)5059 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5060 				u32 seq, int flags,
5061 				struct cfg80211_registered_device *rdev,
5062 				struct net_device *dev,
5063 				const u8 *mac_addr, struct station_info *sinfo)
5064 {
5065 	void *hdr;
5066 	struct nlattr *sinfoattr, *bss_param;
5067 
5068 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5069 	if (!hdr) {
5070 		cfg80211_sinfo_release_content(sinfo);
5071 		return -1;
5072 	}
5073 
5074 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5075 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5076 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5077 		goto nla_put_failure;
5078 
5079 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5080 	if (!sinfoattr)
5081 		goto nla_put_failure;
5082 
5083 #define PUT_SINFO(attr, memb, type) do {				\
5084 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5085 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5086 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5087 			     sinfo->memb))				\
5088 		goto nla_put_failure;					\
5089 	} while (0)
5090 #define PUT_SINFO_U64(attr, memb) do {					\
5091 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5092 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5093 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5094 		goto nla_put_failure;					\
5095 	} while (0)
5096 
5097 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5098 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5099 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5100 
5101 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5102 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5103 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5104 			(u32)sinfo->rx_bytes))
5105 		goto nla_put_failure;
5106 
5107 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5108 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5109 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5110 			(u32)sinfo->tx_bytes))
5111 		goto nla_put_failure;
5112 
5113 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5114 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5115 	PUT_SINFO(LLID, llid, u16);
5116 	PUT_SINFO(PLID, plid, u16);
5117 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5118 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5119 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5120 
5121 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5122 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5123 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5124 
5125 	switch (rdev->wiphy.signal_type) {
5126 	case CFG80211_SIGNAL_TYPE_MBM:
5127 		PUT_SINFO(SIGNAL, signal, u8);
5128 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5129 		break;
5130 	default:
5131 		break;
5132 	}
5133 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5134 		if (!nl80211_put_signal(msg, sinfo->chains,
5135 					sinfo->chain_signal,
5136 					NL80211_STA_INFO_CHAIN_SIGNAL))
5137 			goto nla_put_failure;
5138 	}
5139 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5140 		if (!nl80211_put_signal(msg, sinfo->chains,
5141 					sinfo->chain_signal_avg,
5142 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5143 			goto nla_put_failure;
5144 	}
5145 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5146 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5147 					  NL80211_STA_INFO_TX_BITRATE))
5148 			goto nla_put_failure;
5149 	}
5150 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5151 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5152 					  NL80211_STA_INFO_RX_BITRATE))
5153 			goto nla_put_failure;
5154 	}
5155 
5156 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5157 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5158 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5159 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5160 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5161 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5162 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5163 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5164 	PUT_SINFO(PEER_PM, peer_pm, u32);
5165 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5166 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5167 
5168 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5169 		bss_param = nla_nest_start_noflag(msg,
5170 						  NL80211_STA_INFO_BSS_PARAM);
5171 		if (!bss_param)
5172 			goto nla_put_failure;
5173 
5174 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5175 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5176 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5177 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5178 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5179 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5180 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5181 			       sinfo->bss_param.dtim_period) ||
5182 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5183 				sinfo->bss_param.beacon_interval))
5184 			goto nla_put_failure;
5185 
5186 		nla_nest_end(msg, bss_param);
5187 	}
5188 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5189 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5190 		    sizeof(struct nl80211_sta_flag_update),
5191 		    &sinfo->sta_flags))
5192 		goto nla_put_failure;
5193 
5194 	PUT_SINFO_U64(T_OFFSET, t_offset);
5195 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5196 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5197 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5198 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5199 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5200 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5201 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5202 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5203 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5204 	}
5205 
5206 #undef PUT_SINFO
5207 #undef PUT_SINFO_U64
5208 
5209 	if (sinfo->pertid) {
5210 		struct nlattr *tidsattr;
5211 		int tid;
5212 
5213 		tidsattr = nla_nest_start_noflag(msg,
5214 						 NL80211_STA_INFO_TID_STATS);
5215 		if (!tidsattr)
5216 			goto nla_put_failure;
5217 
5218 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5219 			struct cfg80211_tid_stats *tidstats;
5220 			struct nlattr *tidattr;
5221 
5222 			tidstats = &sinfo->pertid[tid];
5223 
5224 			if (!tidstats->filled)
5225 				continue;
5226 
5227 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5228 			if (!tidattr)
5229 				goto nla_put_failure;
5230 
5231 #define PUT_TIDVAL_U64(attr, memb) do {					\
5232 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5233 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5234 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5235 		goto nla_put_failure;					\
5236 	} while (0)
5237 
5238 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5239 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5240 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5241 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5242 
5243 #undef PUT_TIDVAL_U64
5244 			if ((tidstats->filled &
5245 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5246 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5247 						   NL80211_TID_STATS_TXQ_STATS))
5248 				goto nla_put_failure;
5249 
5250 			nla_nest_end(msg, tidattr);
5251 		}
5252 
5253 		nla_nest_end(msg, tidsattr);
5254 	}
5255 
5256 	nla_nest_end(msg, sinfoattr);
5257 
5258 	if (sinfo->assoc_req_ies_len &&
5259 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5260 		    sinfo->assoc_req_ies))
5261 		goto nla_put_failure;
5262 
5263 	cfg80211_sinfo_release_content(sinfo);
5264 	genlmsg_end(msg, hdr);
5265 	return 0;
5266 
5267  nla_put_failure:
5268 	cfg80211_sinfo_release_content(sinfo);
5269 	genlmsg_cancel(msg, hdr);
5270 	return -EMSGSIZE;
5271 }
5272 
nl80211_dump_station(struct sk_buff * skb,struct netlink_callback * cb)5273 static int nl80211_dump_station(struct sk_buff *skb,
5274 				struct netlink_callback *cb)
5275 {
5276 	struct station_info sinfo;
5277 	struct cfg80211_registered_device *rdev;
5278 	struct wireless_dev *wdev;
5279 	u8 mac_addr[ETH_ALEN];
5280 	int sta_idx = cb->args[2];
5281 	int err;
5282 
5283 	rtnl_lock();
5284 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5285 	if (err)
5286 		goto out_err;
5287 
5288 	if (!wdev->netdev) {
5289 		err = -EINVAL;
5290 		goto out_err;
5291 	}
5292 
5293 	if (!rdev->ops->dump_station) {
5294 		err = -EOPNOTSUPP;
5295 		goto out_err;
5296 	}
5297 
5298 	while (1) {
5299 		memset(&sinfo, 0, sizeof(sinfo));
5300 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5301 					mac_addr, &sinfo);
5302 		if (err == -ENOENT)
5303 			break;
5304 		if (err)
5305 			goto out_err;
5306 
5307 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5308 				NETLINK_CB(cb->skb).portid,
5309 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
5310 				rdev, wdev->netdev, mac_addr,
5311 				&sinfo) < 0)
5312 			goto out;
5313 
5314 		sta_idx++;
5315 	}
5316 
5317  out:
5318 	cb->args[2] = sta_idx;
5319 	err = skb->len;
5320  out_err:
5321 	rtnl_unlock();
5322 
5323 	return err;
5324 }
5325 
nl80211_get_station(struct sk_buff * skb,struct genl_info * info)5326 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5327 {
5328 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5329 	struct net_device *dev = info->user_ptr[1];
5330 	struct station_info sinfo;
5331 	struct sk_buff *msg;
5332 	u8 *mac_addr = NULL;
5333 	int err;
5334 
5335 	memset(&sinfo, 0, sizeof(sinfo));
5336 
5337 	if (!info->attrs[NL80211_ATTR_MAC])
5338 		return -EINVAL;
5339 
5340 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5341 
5342 	if (!rdev->ops->get_station)
5343 		return -EOPNOTSUPP;
5344 
5345 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5346 	if (err)
5347 		return err;
5348 
5349 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5350 	if (!msg) {
5351 		cfg80211_sinfo_release_content(&sinfo);
5352 		return -ENOMEM;
5353 	}
5354 
5355 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5356 				 info->snd_portid, info->snd_seq, 0,
5357 				 rdev, dev, mac_addr, &sinfo) < 0) {
5358 		nlmsg_free(msg);
5359 		return -ENOBUFS;
5360 	}
5361 
5362 	return genlmsg_reply(msg, info);
5363 }
5364 
cfg80211_check_station_change(struct wiphy * wiphy,struct station_parameters * params,enum cfg80211_station_type statype)5365 int cfg80211_check_station_change(struct wiphy *wiphy,
5366 				  struct station_parameters *params,
5367 				  enum cfg80211_station_type statype)
5368 {
5369 	if (params->listen_interval != -1 &&
5370 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5371 		return -EINVAL;
5372 
5373 	if (params->support_p2p_ps != -1 &&
5374 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5375 		return -EINVAL;
5376 
5377 	if (params->aid &&
5378 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5379 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5380 		return -EINVAL;
5381 
5382 	/* When you run into this, adjust the code below for the new flag */
5383 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5384 
5385 	switch (statype) {
5386 	case CFG80211_STA_MESH_PEER_KERNEL:
5387 	case CFG80211_STA_MESH_PEER_USER:
5388 		/*
5389 		 * No ignoring the TDLS flag here -- the userspace mesh
5390 		 * code doesn't have the bug of including TDLS in the
5391 		 * mask everywhere.
5392 		 */
5393 		if (params->sta_flags_mask &
5394 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5395 				  BIT(NL80211_STA_FLAG_MFP) |
5396 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5397 			return -EINVAL;
5398 		break;
5399 	case CFG80211_STA_TDLS_PEER_SETUP:
5400 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5401 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5402 			return -EINVAL;
5403 		/* ignore since it can't change */
5404 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5405 		break;
5406 	default:
5407 		/* disallow mesh-specific things */
5408 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5409 			return -EINVAL;
5410 		if (params->local_pm)
5411 			return -EINVAL;
5412 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5413 			return -EINVAL;
5414 	}
5415 
5416 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5417 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5418 		/* TDLS can't be set, ... */
5419 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5420 			return -EINVAL;
5421 		/*
5422 		 * ... but don't bother the driver with it. This works around
5423 		 * a hostapd/wpa_supplicant issue -- it always includes the
5424 		 * TLDS_PEER flag in the mask even for AP mode.
5425 		 */
5426 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5427 	}
5428 
5429 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5430 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5431 		/* reject other things that can't change */
5432 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5433 			return -EINVAL;
5434 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5435 			return -EINVAL;
5436 		if (params->supported_rates)
5437 			return -EINVAL;
5438 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
5439 		    params->he_capa)
5440 			return -EINVAL;
5441 	}
5442 
5443 	if (statype != CFG80211_STA_AP_CLIENT &&
5444 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5445 		if (params->vlan)
5446 			return -EINVAL;
5447 	}
5448 
5449 	switch (statype) {
5450 	case CFG80211_STA_AP_MLME_CLIENT:
5451 		/* Use this only for authorizing/unauthorizing a station */
5452 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5453 			return -EOPNOTSUPP;
5454 		break;
5455 	case CFG80211_STA_AP_CLIENT:
5456 	case CFG80211_STA_AP_CLIENT_UNASSOC:
5457 		/* accept only the listed bits */
5458 		if (params->sta_flags_mask &
5459 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5460 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5461 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
5462 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5463 				  BIT(NL80211_STA_FLAG_WME) |
5464 				  BIT(NL80211_STA_FLAG_MFP)))
5465 			return -EINVAL;
5466 
5467 		/* but authenticated/associated only if driver handles it */
5468 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5469 		    params->sta_flags_mask &
5470 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5471 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5472 			return -EINVAL;
5473 		break;
5474 	case CFG80211_STA_IBSS:
5475 	case CFG80211_STA_AP_STA:
5476 		/* reject any changes other than AUTHORIZED */
5477 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5478 			return -EINVAL;
5479 		break;
5480 	case CFG80211_STA_TDLS_PEER_SETUP:
5481 		/* reject any changes other than AUTHORIZED or WME */
5482 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5483 					       BIT(NL80211_STA_FLAG_WME)))
5484 			return -EINVAL;
5485 		/* force (at least) rates when authorizing */
5486 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5487 		    !params->supported_rates)
5488 			return -EINVAL;
5489 		break;
5490 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5491 		/* reject any changes */
5492 		return -EINVAL;
5493 	case CFG80211_STA_MESH_PEER_KERNEL:
5494 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5495 			return -EINVAL;
5496 		break;
5497 	case CFG80211_STA_MESH_PEER_USER:
5498 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5499 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5500 			return -EINVAL;
5501 		break;
5502 	}
5503 
5504 	/*
5505 	 * Older kernel versions ignored this attribute entirely, so don't
5506 	 * reject attempts to update it but mark it as unused instead so the
5507 	 * driver won't look at the data.
5508 	 */
5509 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5510 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
5511 		params->opmode_notif_used = false;
5512 
5513 	return 0;
5514 }
5515 EXPORT_SYMBOL(cfg80211_check_station_change);
5516 
5517 /*
5518  * Get vlan interface making sure it is running and on the right wiphy.
5519  */
get_vlan(struct genl_info * info,struct cfg80211_registered_device * rdev)5520 static struct net_device *get_vlan(struct genl_info *info,
5521 				   struct cfg80211_registered_device *rdev)
5522 {
5523 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5524 	struct net_device *v;
5525 	int ret;
5526 
5527 	if (!vlanattr)
5528 		return NULL;
5529 
5530 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5531 	if (!v)
5532 		return ERR_PTR(-ENODEV);
5533 
5534 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5535 		ret = -EINVAL;
5536 		goto error;
5537 	}
5538 
5539 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5540 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5541 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5542 		ret = -EINVAL;
5543 		goto error;
5544 	}
5545 
5546 	if (!netif_running(v)) {
5547 		ret = -ENETDOWN;
5548 		goto error;
5549 	}
5550 
5551 	return v;
5552  error:
5553 	dev_put(v);
5554 	return ERR_PTR(ret);
5555 }
5556 
5557 static const struct nla_policy
5558 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5559 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5560 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5561 };
5562 
nl80211_parse_sta_wme(struct genl_info * info,struct station_parameters * params)5563 static int nl80211_parse_sta_wme(struct genl_info *info,
5564 				 struct station_parameters *params)
5565 {
5566 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5567 	struct nlattr *nla;
5568 	int err;
5569 
5570 	/* parse WME attributes if present */
5571 	if (!info->attrs[NL80211_ATTR_STA_WME])
5572 		return 0;
5573 
5574 	nla = info->attrs[NL80211_ATTR_STA_WME];
5575 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5576 					  nl80211_sta_wme_policy,
5577 					  info->extack);
5578 	if (err)
5579 		return err;
5580 
5581 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5582 		params->uapsd_queues = nla_get_u8(
5583 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
5584 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5585 		return -EINVAL;
5586 
5587 	if (tb[NL80211_STA_WME_MAX_SP])
5588 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5589 
5590 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5591 		return -EINVAL;
5592 
5593 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5594 
5595 	return 0;
5596 }
5597 
nl80211_parse_sta_channel_info(struct genl_info * info,struct station_parameters * params)5598 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5599 				      struct station_parameters *params)
5600 {
5601 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5602 		params->supported_channels =
5603 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5604 		params->supported_channels_len =
5605 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5606 		/*
5607 		 * Need to include at least one (first channel, number of
5608 		 * channels) tuple for each subband, and must have proper
5609 		 * tuples for the rest of the data as well.
5610 		 */
5611 		if (params->supported_channels_len < 2)
5612 			return -EINVAL;
5613 		if (params->supported_channels_len % 2)
5614 			return -EINVAL;
5615 	}
5616 
5617 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5618 		params->supported_oper_classes =
5619 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5620 		params->supported_oper_classes_len =
5621 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5622 		/*
5623 		 * The value of the Length field of the Supported Operating
5624 		 * Classes element is between 2 and 253.
5625 		 */
5626 		if (params->supported_oper_classes_len < 2 ||
5627 		    params->supported_oper_classes_len > 253)
5628 			return -EINVAL;
5629 	}
5630 	return 0;
5631 }
5632 
nl80211_set_station_tdls(struct genl_info * info,struct station_parameters * params)5633 static int nl80211_set_station_tdls(struct genl_info *info,
5634 				    struct station_parameters *params)
5635 {
5636 	int err;
5637 	/* Dummy STA entry gets updated once the peer capabilities are known */
5638 	if (info->attrs[NL80211_ATTR_PEER_AID])
5639 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5640 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5641 		params->ht_capa =
5642 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5643 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5644 		params->vht_capa =
5645 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5646 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5647 		params->he_capa =
5648 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5649 		params->he_capa_len =
5650 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5651 
5652 		if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5653 			return -EINVAL;
5654 	}
5655 
5656 	err = nl80211_parse_sta_channel_info(info, params);
5657 	if (err)
5658 		return err;
5659 
5660 	return nl80211_parse_sta_wme(info, params);
5661 }
5662 
nl80211_parse_sta_txpower_setting(struct genl_info * info,struct station_parameters * params)5663 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5664 					     struct station_parameters *params)
5665 {
5666 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5667 	int idx;
5668 
5669 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5670 		if (!rdev->ops->set_tx_power ||
5671 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5672 					 NL80211_EXT_FEATURE_STA_TX_PWR))
5673 			return -EOPNOTSUPP;
5674 
5675 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5676 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
5677 
5678 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5679 			idx = NL80211_ATTR_STA_TX_POWER;
5680 
5681 			if (info->attrs[idx])
5682 				params->txpwr.power =
5683 					nla_get_s16(info->attrs[idx]);
5684 			else
5685 				return -EINVAL;
5686 		}
5687 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5688 	}
5689 
5690 	return 0;
5691 }
5692 
nl80211_set_station(struct sk_buff * skb,struct genl_info * info)5693 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5694 {
5695 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5696 	struct net_device *dev = info->user_ptr[1];
5697 	struct station_parameters params;
5698 	u8 *mac_addr;
5699 	int err;
5700 
5701 	memset(&params, 0, sizeof(params));
5702 
5703 	if (!rdev->ops->change_station)
5704 		return -EOPNOTSUPP;
5705 
5706 	/*
5707 	 * AID and listen_interval properties can be set only for unassociated
5708 	 * station. Include these parameters here and will check them in
5709 	 * cfg80211_check_station_change().
5710 	 */
5711 	if (info->attrs[NL80211_ATTR_STA_AID])
5712 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5713 
5714 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5715 		params.listen_interval =
5716 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5717 	else
5718 		params.listen_interval = -1;
5719 
5720 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5721 		params.support_p2p_ps =
5722 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5723 	else
5724 		params.support_p2p_ps = -1;
5725 
5726 	if (!info->attrs[NL80211_ATTR_MAC])
5727 		return -EINVAL;
5728 
5729 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5730 
5731 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5732 		params.supported_rates =
5733 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5734 		params.supported_rates_len =
5735 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5736 	}
5737 
5738 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5739 		params.capability =
5740 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5741 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5742 	}
5743 
5744 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5745 		params.ext_capab =
5746 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5747 		params.ext_capab_len =
5748 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5749 	}
5750 
5751 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5752 		return -EINVAL;
5753 
5754 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5755 		params.plink_action =
5756 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5757 
5758 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5759 		params.plink_state =
5760 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5761 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5762 			params.peer_aid = nla_get_u16(
5763 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5764 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5765 	}
5766 
5767 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
5768 		params.local_pm = nla_get_u32(
5769 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5770 
5771 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5772 		params.opmode_notif_used = true;
5773 		params.opmode_notif =
5774 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5775 	}
5776 
5777 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5778 		params.airtime_weight =
5779 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5780 
5781 	if (params.airtime_weight &&
5782 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5783 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5784 		return -EOPNOTSUPP;
5785 
5786 	err = nl80211_parse_sta_txpower_setting(info, &params);
5787 	if (err)
5788 		return err;
5789 
5790 	/* Include parameters for TDLS peer (will check later) */
5791 	err = nl80211_set_station_tdls(info, &params);
5792 	if (err)
5793 		return err;
5794 
5795 	params.vlan = get_vlan(info, rdev);
5796 	if (IS_ERR(params.vlan))
5797 		return PTR_ERR(params.vlan);
5798 
5799 	switch (dev->ieee80211_ptr->iftype) {
5800 	case NL80211_IFTYPE_AP:
5801 	case NL80211_IFTYPE_AP_VLAN:
5802 	case NL80211_IFTYPE_P2P_GO:
5803 	case NL80211_IFTYPE_P2P_CLIENT:
5804 	case NL80211_IFTYPE_STATION:
5805 	case NL80211_IFTYPE_ADHOC:
5806 	case NL80211_IFTYPE_MESH_POINT:
5807 		break;
5808 	default:
5809 		err = -EOPNOTSUPP;
5810 		goto out_put_vlan;
5811 	}
5812 
5813 	/* driver will call cfg80211_check_station_change() */
5814 	err = rdev_change_station(rdev, dev, mac_addr, &params);
5815 
5816  out_put_vlan:
5817 	if (params.vlan)
5818 		dev_put(params.vlan);
5819 
5820 	return err;
5821 }
5822 
nl80211_new_station(struct sk_buff * skb,struct genl_info * info)5823 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5824 {
5825 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5826 	int err;
5827 	struct net_device *dev = info->user_ptr[1];
5828 	struct station_parameters params;
5829 	u8 *mac_addr = NULL;
5830 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5831 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
5832 
5833 	memset(&params, 0, sizeof(params));
5834 
5835 	if (!rdev->ops->add_station)
5836 		return -EOPNOTSUPP;
5837 
5838 	if (!info->attrs[NL80211_ATTR_MAC])
5839 		return -EINVAL;
5840 
5841 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5842 		return -EINVAL;
5843 
5844 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5845 		return -EINVAL;
5846 
5847 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
5848 	    !info->attrs[NL80211_ATTR_PEER_AID])
5849 		return -EINVAL;
5850 
5851 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5852 	params.supported_rates =
5853 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5854 	params.supported_rates_len =
5855 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5856 	params.listen_interval =
5857 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5858 
5859 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5860 		params.support_p2p_ps =
5861 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5862 	} else {
5863 		/*
5864 		 * if not specified, assume it's supported for P2P GO interface,
5865 		 * and is NOT supported for AP interface
5866 		 */
5867 		params.support_p2p_ps =
5868 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5869 	}
5870 
5871 	if (info->attrs[NL80211_ATTR_PEER_AID])
5872 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5873 	else
5874 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5875 
5876 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5877 		params.capability =
5878 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5879 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5880 	}
5881 
5882 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5883 		params.ext_capab =
5884 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5885 		params.ext_capab_len =
5886 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5887 	}
5888 
5889 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5890 		params.ht_capa =
5891 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5892 
5893 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5894 		params.vht_capa =
5895 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5896 
5897 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5898 		params.he_capa =
5899 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5900 		params.he_capa_len =
5901 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5902 
5903 		/* max len is validated in nla policy */
5904 		if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5905 			return -EINVAL;
5906 	}
5907 
5908 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5909 		params.opmode_notif_used = true;
5910 		params.opmode_notif =
5911 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5912 	}
5913 
5914 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5915 		params.plink_action =
5916 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5917 
5918 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5919 		params.airtime_weight =
5920 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5921 
5922 	if (params.airtime_weight &&
5923 	    !wiphy_ext_feature_isset(&rdev->wiphy,
5924 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5925 		return -EOPNOTSUPP;
5926 
5927 	err = nl80211_parse_sta_txpower_setting(info, &params);
5928 	if (err)
5929 		return err;
5930 
5931 	err = nl80211_parse_sta_channel_info(info, &params);
5932 	if (err)
5933 		return err;
5934 
5935 	err = nl80211_parse_sta_wme(info, &params);
5936 	if (err)
5937 		return err;
5938 
5939 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5940 		return -EINVAL;
5941 
5942 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5943 	 * as userspace might just pass through the capabilities from the IEs
5944 	 * directly, rather than enforcing this restriction and returning an
5945 	 * error in this case.
5946 	 */
5947 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5948 		params.ht_capa = NULL;
5949 		params.vht_capa = NULL;
5950 
5951 		/* HE requires WME */
5952 		if (params.he_capa_len)
5953 			return -EINVAL;
5954 	}
5955 
5956 	/* When you run into this, adjust the code below for the new flag */
5957 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5958 
5959 	switch (dev->ieee80211_ptr->iftype) {
5960 	case NL80211_IFTYPE_AP:
5961 	case NL80211_IFTYPE_AP_VLAN:
5962 	case NL80211_IFTYPE_P2P_GO:
5963 		/* ignore WME attributes if iface/sta is not capable */
5964 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5965 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5966 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5967 
5968 		/* TDLS peers cannot be added */
5969 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5970 		    info->attrs[NL80211_ATTR_PEER_AID])
5971 			return -EINVAL;
5972 		/* but don't bother the driver with it */
5973 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5974 
5975 		/* allow authenticated/associated only if driver handles it */
5976 		if (!(rdev->wiphy.features &
5977 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5978 		    params.sta_flags_mask & auth_assoc)
5979 			return -EINVAL;
5980 
5981 		/* Older userspace, or userspace wanting to be compatible with
5982 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5983 		 * and assoc flags in the mask, but assumes the station will be
5984 		 * added as associated anyway since this was the required driver
5985 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5986 		 * introduced.
5987 		 * In order to not bother drivers with this quirk in the API
5988 		 * set the flags in both the mask and set for new stations in
5989 		 * this case.
5990 		 */
5991 		if (!(params.sta_flags_mask & auth_assoc)) {
5992 			params.sta_flags_mask |= auth_assoc;
5993 			params.sta_flags_set |= auth_assoc;
5994 		}
5995 
5996 		/* must be last in here for error handling */
5997 		params.vlan = get_vlan(info, rdev);
5998 		if (IS_ERR(params.vlan))
5999 			return PTR_ERR(params.vlan);
6000 		break;
6001 	case NL80211_IFTYPE_MESH_POINT:
6002 		/* ignore uAPSD data */
6003 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6004 
6005 		/* associated is disallowed */
6006 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6007 			return -EINVAL;
6008 		/* TDLS peers cannot be added */
6009 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6010 		    info->attrs[NL80211_ATTR_PEER_AID])
6011 			return -EINVAL;
6012 		break;
6013 	case NL80211_IFTYPE_STATION:
6014 	case NL80211_IFTYPE_P2P_CLIENT:
6015 		/* ignore uAPSD data */
6016 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6017 
6018 		/* these are disallowed */
6019 		if (params.sta_flags_mask &
6020 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
6021 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6022 			return -EINVAL;
6023 		/* Only TDLS peers can be added */
6024 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6025 			return -EINVAL;
6026 		/* Can only add if TDLS ... */
6027 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6028 			return -EOPNOTSUPP;
6029 		/* ... with external setup is supported */
6030 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6031 			return -EOPNOTSUPP;
6032 		/*
6033 		 * Older wpa_supplicant versions always mark the TDLS peer
6034 		 * as authorized, but it shouldn't yet be.
6035 		 */
6036 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6037 		break;
6038 	default:
6039 		return -EOPNOTSUPP;
6040 	}
6041 
6042 	/* be aware of params.vlan when changing code here */
6043 
6044 	err = rdev_add_station(rdev, dev, mac_addr, &params);
6045 
6046 	if (params.vlan)
6047 		dev_put(params.vlan);
6048 	return err;
6049 }
6050 
nl80211_del_station(struct sk_buff * skb,struct genl_info * info)6051 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6052 {
6053 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6054 	struct net_device *dev = info->user_ptr[1];
6055 	struct station_del_parameters params;
6056 
6057 	memset(&params, 0, sizeof(params));
6058 
6059 	if (info->attrs[NL80211_ATTR_MAC])
6060 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6061 
6062 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6063 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6064 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
6065 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6066 		return -EINVAL;
6067 
6068 	if (!rdev->ops->del_station)
6069 		return -EOPNOTSUPP;
6070 
6071 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6072 		params.subtype =
6073 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6074 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6075 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6076 			return -EINVAL;
6077 	} else {
6078 		/* Default to Deauthentication frame */
6079 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6080 	}
6081 
6082 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6083 		params.reason_code =
6084 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6085 		if (params.reason_code == 0)
6086 			return -EINVAL; /* 0 is reserved */
6087 	} else {
6088 		/* Default to reason code 2 */
6089 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6090 	}
6091 
6092 	return rdev_del_station(rdev, dev, &params);
6093 }
6094 
nl80211_send_mpath(struct sk_buff * msg,u32 portid,u32 seq,int flags,struct net_device * dev,u8 * dst,u8 * next_hop,struct mpath_info * pinfo)6095 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6096 				int flags, struct net_device *dev,
6097 				u8 *dst, u8 *next_hop,
6098 				struct mpath_info *pinfo)
6099 {
6100 	void *hdr;
6101 	struct nlattr *pinfoattr;
6102 
6103 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6104 	if (!hdr)
6105 		return -1;
6106 
6107 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6108 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6109 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6110 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6111 		goto nla_put_failure;
6112 
6113 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6114 	if (!pinfoattr)
6115 		goto nla_put_failure;
6116 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6117 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6118 			pinfo->frame_qlen))
6119 		goto nla_put_failure;
6120 	if (((pinfo->filled & MPATH_INFO_SN) &&
6121 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6122 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6123 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6124 			 pinfo->metric)) ||
6125 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6126 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6127 			 pinfo->exptime)) ||
6128 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6129 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6130 			pinfo->flags)) ||
6131 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6132 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6133 			 pinfo->discovery_timeout)) ||
6134 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6135 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6136 			pinfo->discovery_retries)) ||
6137 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6138 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6139 			pinfo->hop_count)) ||
6140 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6141 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6142 			 pinfo->path_change_count)))
6143 		goto nla_put_failure;
6144 
6145 	nla_nest_end(msg, pinfoattr);
6146 
6147 	genlmsg_end(msg, hdr);
6148 	return 0;
6149 
6150  nla_put_failure:
6151 	genlmsg_cancel(msg, hdr);
6152 	return -EMSGSIZE;
6153 }
6154 
nl80211_dump_mpath(struct sk_buff * skb,struct netlink_callback * cb)6155 static int nl80211_dump_mpath(struct sk_buff *skb,
6156 			      struct netlink_callback *cb)
6157 {
6158 	struct mpath_info pinfo;
6159 	struct cfg80211_registered_device *rdev;
6160 	struct wireless_dev *wdev;
6161 	u8 dst[ETH_ALEN];
6162 	u8 next_hop[ETH_ALEN];
6163 	int path_idx = cb->args[2];
6164 	int err;
6165 
6166 	rtnl_lock();
6167 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6168 	if (err)
6169 		goto out_err;
6170 
6171 	if (!rdev->ops->dump_mpath) {
6172 		err = -EOPNOTSUPP;
6173 		goto out_err;
6174 	}
6175 
6176 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6177 		err = -EOPNOTSUPP;
6178 		goto out_err;
6179 	}
6180 
6181 	while (1) {
6182 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6183 				      next_hop, &pinfo);
6184 		if (err == -ENOENT)
6185 			break;
6186 		if (err)
6187 			goto out_err;
6188 
6189 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6190 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6191 				       wdev->netdev, dst, next_hop,
6192 				       &pinfo) < 0)
6193 			goto out;
6194 
6195 		path_idx++;
6196 	}
6197 
6198  out:
6199 	cb->args[2] = path_idx;
6200 	err = skb->len;
6201  out_err:
6202 	rtnl_unlock();
6203 	return err;
6204 }
6205 
nl80211_get_mpath(struct sk_buff * skb,struct genl_info * info)6206 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6207 {
6208 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6209 	int err;
6210 	struct net_device *dev = info->user_ptr[1];
6211 	struct mpath_info pinfo;
6212 	struct sk_buff *msg;
6213 	u8 *dst = NULL;
6214 	u8 next_hop[ETH_ALEN];
6215 
6216 	memset(&pinfo, 0, sizeof(pinfo));
6217 
6218 	if (!info->attrs[NL80211_ATTR_MAC])
6219 		return -EINVAL;
6220 
6221 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6222 
6223 	if (!rdev->ops->get_mpath)
6224 		return -EOPNOTSUPP;
6225 
6226 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6227 		return -EOPNOTSUPP;
6228 
6229 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6230 	if (err)
6231 		return err;
6232 
6233 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6234 	if (!msg)
6235 		return -ENOMEM;
6236 
6237 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6238 				 dev, dst, next_hop, &pinfo) < 0) {
6239 		nlmsg_free(msg);
6240 		return -ENOBUFS;
6241 	}
6242 
6243 	return genlmsg_reply(msg, info);
6244 }
6245 
nl80211_set_mpath(struct sk_buff * skb,struct genl_info * info)6246 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6247 {
6248 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6249 	struct net_device *dev = info->user_ptr[1];
6250 	u8 *dst = NULL;
6251 	u8 *next_hop = NULL;
6252 
6253 	if (!info->attrs[NL80211_ATTR_MAC])
6254 		return -EINVAL;
6255 
6256 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6257 		return -EINVAL;
6258 
6259 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6260 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6261 
6262 	if (!rdev->ops->change_mpath)
6263 		return -EOPNOTSUPP;
6264 
6265 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6266 		return -EOPNOTSUPP;
6267 
6268 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6269 }
6270 
nl80211_new_mpath(struct sk_buff * skb,struct genl_info * info)6271 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6272 {
6273 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6274 	struct net_device *dev = info->user_ptr[1];
6275 	u8 *dst = NULL;
6276 	u8 *next_hop = NULL;
6277 
6278 	if (!info->attrs[NL80211_ATTR_MAC])
6279 		return -EINVAL;
6280 
6281 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6282 		return -EINVAL;
6283 
6284 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6285 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6286 
6287 	if (!rdev->ops->add_mpath)
6288 		return -EOPNOTSUPP;
6289 
6290 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6291 		return -EOPNOTSUPP;
6292 
6293 	return rdev_add_mpath(rdev, dev, dst, next_hop);
6294 }
6295 
nl80211_del_mpath(struct sk_buff * skb,struct genl_info * info)6296 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6297 {
6298 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6299 	struct net_device *dev = info->user_ptr[1];
6300 	u8 *dst = NULL;
6301 
6302 	if (info->attrs[NL80211_ATTR_MAC])
6303 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6304 
6305 	if (!rdev->ops->del_mpath)
6306 		return -EOPNOTSUPP;
6307 
6308 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6309 		return -EOPNOTSUPP;
6310 
6311 	return rdev_del_mpath(rdev, dev, dst);
6312 }
6313 
nl80211_get_mpp(struct sk_buff * skb,struct genl_info * info)6314 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6315 {
6316 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6317 	int err;
6318 	struct net_device *dev = info->user_ptr[1];
6319 	struct mpath_info pinfo;
6320 	struct sk_buff *msg;
6321 	u8 *dst = NULL;
6322 	u8 mpp[ETH_ALEN];
6323 
6324 	memset(&pinfo, 0, sizeof(pinfo));
6325 
6326 	if (!info->attrs[NL80211_ATTR_MAC])
6327 		return -EINVAL;
6328 
6329 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6330 
6331 	if (!rdev->ops->get_mpp)
6332 		return -EOPNOTSUPP;
6333 
6334 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6335 		return -EOPNOTSUPP;
6336 
6337 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6338 	if (err)
6339 		return err;
6340 
6341 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6342 	if (!msg)
6343 		return -ENOMEM;
6344 
6345 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6346 			       dev, dst, mpp, &pinfo) < 0) {
6347 		nlmsg_free(msg);
6348 		return -ENOBUFS;
6349 	}
6350 
6351 	return genlmsg_reply(msg, info);
6352 }
6353 
nl80211_dump_mpp(struct sk_buff * skb,struct netlink_callback * cb)6354 static int nl80211_dump_mpp(struct sk_buff *skb,
6355 			    struct netlink_callback *cb)
6356 {
6357 	struct mpath_info pinfo;
6358 	struct cfg80211_registered_device *rdev;
6359 	struct wireless_dev *wdev;
6360 	u8 dst[ETH_ALEN];
6361 	u8 mpp[ETH_ALEN];
6362 	int path_idx = cb->args[2];
6363 	int err;
6364 
6365 	rtnl_lock();
6366 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6367 	if (err)
6368 		goto out_err;
6369 
6370 	if (!rdev->ops->dump_mpp) {
6371 		err = -EOPNOTSUPP;
6372 		goto out_err;
6373 	}
6374 
6375 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6376 		err = -EOPNOTSUPP;
6377 		goto out_err;
6378 	}
6379 
6380 	while (1) {
6381 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6382 				    mpp, &pinfo);
6383 		if (err == -ENOENT)
6384 			break;
6385 		if (err)
6386 			goto out_err;
6387 
6388 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6389 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6390 				       wdev->netdev, dst, mpp,
6391 				       &pinfo) < 0)
6392 			goto out;
6393 
6394 		path_idx++;
6395 	}
6396 
6397  out:
6398 	cb->args[2] = path_idx;
6399 	err = skb->len;
6400  out_err:
6401 	rtnl_unlock();
6402 	return err;
6403 }
6404 
nl80211_set_bss(struct sk_buff * skb,struct genl_info * info)6405 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6406 {
6407 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6408 	struct net_device *dev = info->user_ptr[1];
6409 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6410 	struct bss_parameters params;
6411 	int err;
6412 
6413 	memset(&params, 0, sizeof(params));
6414 	/* default to not changing parameters */
6415 	params.use_cts_prot = -1;
6416 	params.use_short_preamble = -1;
6417 	params.use_short_slot_time = -1;
6418 	params.ap_isolate = -1;
6419 	params.ht_opmode = -1;
6420 	params.p2p_ctwindow = -1;
6421 	params.p2p_opp_ps = -1;
6422 
6423 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6424 		params.use_cts_prot =
6425 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6426 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6427 		params.use_short_preamble =
6428 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6429 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6430 		params.use_short_slot_time =
6431 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6432 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6433 		params.basic_rates =
6434 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6435 		params.basic_rates_len =
6436 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6437 	}
6438 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6439 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6440 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6441 		params.ht_opmode =
6442 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6443 
6444 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6445 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6446 			return -EINVAL;
6447 		params.p2p_ctwindow =
6448 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6449 		if (params.p2p_ctwindow != 0 &&
6450 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6451 			return -EINVAL;
6452 	}
6453 
6454 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6455 		u8 tmp;
6456 
6457 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6458 			return -EINVAL;
6459 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6460 		params.p2p_opp_ps = tmp;
6461 		if (params.p2p_opp_ps &&
6462 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6463 			return -EINVAL;
6464 	}
6465 
6466 	if (!rdev->ops->change_bss)
6467 		return -EOPNOTSUPP;
6468 
6469 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6470 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6471 		return -EOPNOTSUPP;
6472 
6473 	wdev_lock(wdev);
6474 	err = rdev_change_bss(rdev, dev, &params);
6475 	wdev_unlock(wdev);
6476 
6477 	return err;
6478 }
6479 
nl80211_req_set_reg(struct sk_buff * skb,struct genl_info * info)6480 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6481 {
6482 	char *data = NULL;
6483 	bool is_indoor;
6484 	enum nl80211_user_reg_hint_type user_reg_hint_type;
6485 	u32 owner_nlportid;
6486 
6487 	/*
6488 	 * You should only get this when cfg80211 hasn't yet initialized
6489 	 * completely when built-in to the kernel right between the time
6490 	 * window between nl80211_init() and regulatory_init(), if that is
6491 	 * even possible.
6492 	 */
6493 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6494 		return -EINPROGRESS;
6495 
6496 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6497 		user_reg_hint_type =
6498 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6499 	else
6500 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6501 
6502 	switch (user_reg_hint_type) {
6503 	case NL80211_USER_REG_HINT_USER:
6504 	case NL80211_USER_REG_HINT_CELL_BASE:
6505 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6506 			return -EINVAL;
6507 
6508 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6509 		return regulatory_hint_user(data, user_reg_hint_type);
6510 	case NL80211_USER_REG_HINT_INDOOR:
6511 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6512 			owner_nlportid = info->snd_portid;
6513 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6514 		} else {
6515 			owner_nlportid = 0;
6516 			is_indoor = true;
6517 		}
6518 
6519 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
6520 	default:
6521 		return -EINVAL;
6522 	}
6523 }
6524 
nl80211_reload_regdb(struct sk_buff * skb,struct genl_info * info)6525 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6526 {
6527 	return reg_reload_regdb();
6528 }
6529 
nl80211_get_mesh_config(struct sk_buff * skb,struct genl_info * info)6530 static int nl80211_get_mesh_config(struct sk_buff *skb,
6531 				   struct genl_info *info)
6532 {
6533 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6534 	struct net_device *dev = info->user_ptr[1];
6535 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6536 	struct mesh_config cur_params;
6537 	int err = 0;
6538 	void *hdr;
6539 	struct nlattr *pinfoattr;
6540 	struct sk_buff *msg;
6541 
6542 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6543 		return -EOPNOTSUPP;
6544 
6545 	if (!rdev->ops->get_mesh_config)
6546 		return -EOPNOTSUPP;
6547 
6548 	wdev_lock(wdev);
6549 	/* If not connected, get default parameters */
6550 	if (!wdev->mesh_id_len)
6551 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6552 	else
6553 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
6554 	wdev_unlock(wdev);
6555 
6556 	if (err)
6557 		return err;
6558 
6559 	/* Draw up a netlink message to send back */
6560 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6561 	if (!msg)
6562 		return -ENOMEM;
6563 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6564 			     NL80211_CMD_GET_MESH_CONFIG);
6565 	if (!hdr)
6566 		goto out;
6567 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6568 	if (!pinfoattr)
6569 		goto nla_put_failure;
6570 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6571 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6572 			cur_params.dot11MeshRetryTimeout) ||
6573 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6574 			cur_params.dot11MeshConfirmTimeout) ||
6575 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6576 			cur_params.dot11MeshHoldingTimeout) ||
6577 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6578 			cur_params.dot11MeshMaxPeerLinks) ||
6579 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6580 		       cur_params.dot11MeshMaxRetries) ||
6581 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
6582 		       cur_params.dot11MeshTTL) ||
6583 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6584 		       cur_params.element_ttl) ||
6585 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6586 		       cur_params.auto_open_plinks) ||
6587 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6588 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6589 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6590 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
6591 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6592 			cur_params.path_refresh_time) ||
6593 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6594 			cur_params.min_discovery_timeout) ||
6595 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6596 			cur_params.dot11MeshHWMPactivePathTimeout) ||
6597 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6598 			cur_params.dot11MeshHWMPpreqMinInterval) ||
6599 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6600 			cur_params.dot11MeshHWMPperrMinInterval) ||
6601 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6602 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6603 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6604 		       cur_params.dot11MeshHWMPRootMode) ||
6605 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6606 			cur_params.dot11MeshHWMPRannInterval) ||
6607 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6608 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
6609 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6610 		       cur_params.dot11MeshForwarding) ||
6611 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6612 			cur_params.rssi_threshold) ||
6613 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6614 			cur_params.ht_opmode) ||
6615 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6616 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6617 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6618 			cur_params.dot11MeshHWMProotInterval) ||
6619 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6620 			cur_params.dot11MeshHWMPconfirmationInterval) ||
6621 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6622 			cur_params.power_mode) ||
6623 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6624 			cur_params.dot11MeshAwakeWindowDuration) ||
6625 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6626 			cur_params.plink_timeout) ||
6627 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6628 		       cur_params.dot11MeshConnectedToMeshGate))
6629 		goto nla_put_failure;
6630 	nla_nest_end(msg, pinfoattr);
6631 	genlmsg_end(msg, hdr);
6632 	return genlmsg_reply(msg, info);
6633 
6634  nla_put_failure:
6635  out:
6636 	nlmsg_free(msg);
6637 	return -ENOBUFS;
6638 }
6639 
6640 static const struct nla_policy
6641 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6642 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
6643 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6644 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6645 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6646 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
6647 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
6648 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
6649 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
6650 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6651 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6652 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6653 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6654 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6655 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
6656 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6657 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6658 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6659 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6660 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6661 		NLA_POLICY_MIN(NLA_U16, 1),
6662 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6663 		NLA_POLICY_MIN(NLA_U16, 1),
6664 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6665 		NLA_POLICY_MIN(NLA_U16, 1),
6666 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6667 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6668 		NLA_POLICY_MIN(NLA_U16, 1),
6669 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6670 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6671 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
6672 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
6673 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6674 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6675 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6676 		NLA_POLICY_MIN(NLA_U16, 1),
6677 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6678 		NLA_POLICY_MIN(NLA_U16, 1),
6679 	[NL80211_MESHCONF_POWER_MODE] =
6680 		NLA_POLICY_RANGE(NLA_U32,
6681 				 NL80211_MESH_POWER_ACTIVE,
6682 				 NL80211_MESH_POWER_MAX),
6683 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6684 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6685 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6686 };
6687 
6688 static const struct nla_policy
6689 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6690 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6691 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6692 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6693 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6694 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6695 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6696 	[NL80211_MESH_SETUP_IE] =
6697 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6698 				       IEEE80211_MAX_DATA_LEN),
6699 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6700 };
6701 
nl80211_parse_mesh_config(struct genl_info * info,struct mesh_config * cfg,u32 * mask_out)6702 static int nl80211_parse_mesh_config(struct genl_info *info,
6703 				     struct mesh_config *cfg,
6704 				     u32 *mask_out)
6705 {
6706 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6707 	u32 mask = 0;
6708 	u16 ht_opmode;
6709 
6710 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
6711 do {									\
6712 	if (tb[attr]) {							\
6713 		cfg->param = fn(tb[attr]);				\
6714 		mask |= BIT((attr) - 1);				\
6715 	}								\
6716 } while (0)
6717 
6718 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6719 		return -EINVAL;
6720 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6721 		return -EINVAL;
6722 
6723 	/* This makes sure that there aren't more than 32 mesh config
6724 	 * parameters (otherwise our bitfield scheme would not work.) */
6725 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6726 
6727 	/* Fill in the params struct */
6728 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6729 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6730 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6731 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
6732 				  nla_get_u16);
6733 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6734 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
6735 				  nla_get_u16);
6736 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6737 				  NL80211_MESHCONF_MAX_PEER_LINKS,
6738 				  nla_get_u16);
6739 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6740 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
6741 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
6742 				  NL80211_MESHCONF_TTL, nla_get_u8);
6743 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
6744 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
6745 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
6746 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6747 				  nla_get_u8);
6748 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6749 				  mask,
6750 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6751 				  nla_get_u32);
6752 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
6753 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6754 				  nla_get_u8);
6755 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
6756 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
6757 				  nla_get_u32);
6758 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
6759 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
6760 		return -EINVAL;
6761 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
6762 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6763 				  nla_get_u16);
6764 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6765 				  mask,
6766 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6767 				  nla_get_u32);
6768 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
6769 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
6770 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
6771 		return -EINVAL;
6772 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
6773 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6774 				  nla_get_u16);
6775 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
6776 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6777 				  nla_get_u16);
6778 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6779 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
6780 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6781 				  nla_get_u16);
6782 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
6783 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
6784 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
6785 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6786 				  nla_get_u16);
6787 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
6788 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6789 				  nla_get_u8);
6790 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
6791 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
6792 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
6793 				  NL80211_MESHCONF_RSSI_THRESHOLD,
6794 				  nla_get_s32);
6795 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
6796 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
6797 				  nla_get_u8);
6798 	/*
6799 	 * Check HT operation mode based on
6800 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6801 	 */
6802 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6803 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6804 
6805 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6806 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6807 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6808 			return -EINVAL;
6809 
6810 		/* NON_HT_STA bit is reserved, but some programs set it */
6811 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6812 
6813 		cfg->ht_opmode = ht_opmode;
6814 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6815 	}
6816 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6817 				  dot11MeshHWMPactivePathToRootTimeout, mask,
6818 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6819 				  nla_get_u32);
6820 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
6821 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
6822 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
6823 		return -EINVAL;
6824 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
6825 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6826 				  nla_get_u16);
6827 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
6828 				  mask,
6829 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6830 				  nla_get_u16);
6831 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
6832 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
6833 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
6834 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
6835 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
6836 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
6837 	if (mask_out)
6838 		*mask_out = mask;
6839 
6840 	return 0;
6841 
6842 #undef FILL_IN_MESH_PARAM_IF_SET
6843 }
6844 
nl80211_parse_mesh_setup(struct genl_info * info,struct mesh_setup * setup)6845 static int nl80211_parse_mesh_setup(struct genl_info *info,
6846 				     struct mesh_setup *setup)
6847 {
6848 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6849 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6850 
6851 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6852 		return -EINVAL;
6853 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
6854 		return -EINVAL;
6855 
6856 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6857 		setup->sync_method =
6858 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6859 		 IEEE80211_SYNC_METHOD_VENDOR :
6860 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6861 
6862 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6863 		setup->path_sel_proto =
6864 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6865 		 IEEE80211_PATH_PROTOCOL_VENDOR :
6866 		 IEEE80211_PATH_PROTOCOL_HWMP;
6867 
6868 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6869 		setup->path_metric =
6870 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6871 		 IEEE80211_PATH_METRIC_VENDOR :
6872 		 IEEE80211_PATH_METRIC_AIRTIME;
6873 
6874 	if (tb[NL80211_MESH_SETUP_IE]) {
6875 		struct nlattr *ieattr =
6876 			tb[NL80211_MESH_SETUP_IE];
6877 		setup->ie = nla_data(ieattr);
6878 		setup->ie_len = nla_len(ieattr);
6879 	}
6880 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6881 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6882 		return -EINVAL;
6883 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6884 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6885 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6886 	if (setup->is_secure)
6887 		setup->user_mpm = true;
6888 
6889 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6890 		if (!setup->user_mpm)
6891 			return -EINVAL;
6892 		setup->auth_id =
6893 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6894 	}
6895 
6896 	return 0;
6897 }
6898 
nl80211_update_mesh_config(struct sk_buff * skb,struct genl_info * info)6899 static int nl80211_update_mesh_config(struct sk_buff *skb,
6900 				      struct genl_info *info)
6901 {
6902 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6903 	struct net_device *dev = info->user_ptr[1];
6904 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6905 	struct mesh_config cfg;
6906 	u32 mask;
6907 	int err;
6908 
6909 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6910 		return -EOPNOTSUPP;
6911 
6912 	if (!rdev->ops->update_mesh_config)
6913 		return -EOPNOTSUPP;
6914 
6915 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
6916 	if (err)
6917 		return err;
6918 
6919 	wdev_lock(wdev);
6920 	if (!wdev->mesh_id_len)
6921 		err = -ENOLINK;
6922 
6923 	if (!err)
6924 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6925 
6926 	wdev_unlock(wdev);
6927 
6928 	return err;
6929 }
6930 
nl80211_put_regdom(const struct ieee80211_regdomain * regdom,struct sk_buff * msg)6931 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6932 			      struct sk_buff *msg)
6933 {
6934 	struct nlattr *nl_reg_rules;
6935 	unsigned int i;
6936 
6937 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6938 	    (regdom->dfs_region &&
6939 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6940 		goto nla_put_failure;
6941 
6942 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
6943 	if (!nl_reg_rules)
6944 		goto nla_put_failure;
6945 
6946 	for (i = 0; i < regdom->n_reg_rules; i++) {
6947 		struct nlattr *nl_reg_rule;
6948 		const struct ieee80211_reg_rule *reg_rule;
6949 		const struct ieee80211_freq_range *freq_range;
6950 		const struct ieee80211_power_rule *power_rule;
6951 		unsigned int max_bandwidth_khz;
6952 
6953 		reg_rule = &regdom->reg_rules[i];
6954 		freq_range = &reg_rule->freq_range;
6955 		power_rule = &reg_rule->power_rule;
6956 
6957 		nl_reg_rule = nla_nest_start_noflag(msg, i);
6958 		if (!nl_reg_rule)
6959 			goto nla_put_failure;
6960 
6961 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
6962 		if (!max_bandwidth_khz)
6963 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6964 								  reg_rule);
6965 
6966 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6967 				reg_rule->flags) ||
6968 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6969 				freq_range->start_freq_khz) ||
6970 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6971 				freq_range->end_freq_khz) ||
6972 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6973 				max_bandwidth_khz) ||
6974 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6975 				power_rule->max_antenna_gain) ||
6976 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6977 				power_rule->max_eirp) ||
6978 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6979 				reg_rule->dfs_cac_ms))
6980 			goto nla_put_failure;
6981 
6982 		nla_nest_end(msg, nl_reg_rule);
6983 	}
6984 
6985 	nla_nest_end(msg, nl_reg_rules);
6986 	return 0;
6987 
6988 nla_put_failure:
6989 	return -EMSGSIZE;
6990 }
6991 
nl80211_get_reg_do(struct sk_buff * skb,struct genl_info * info)6992 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6993 {
6994 	const struct ieee80211_regdomain *regdom = NULL;
6995 	struct cfg80211_registered_device *rdev;
6996 	struct wiphy *wiphy = NULL;
6997 	struct sk_buff *msg;
6998 	void *hdr;
6999 
7000 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7001 	if (!msg)
7002 		return -ENOBUFS;
7003 
7004 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7005 			     NL80211_CMD_GET_REG);
7006 	if (!hdr)
7007 		goto put_failure;
7008 
7009 	if (info->attrs[NL80211_ATTR_WIPHY]) {
7010 		bool self_managed;
7011 
7012 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7013 		if (IS_ERR(rdev)) {
7014 			nlmsg_free(msg);
7015 			return PTR_ERR(rdev);
7016 		}
7017 
7018 		wiphy = &rdev->wiphy;
7019 		self_managed = wiphy->regulatory_flags &
7020 			       REGULATORY_WIPHY_SELF_MANAGED;
7021 		regdom = get_wiphy_regdom(wiphy);
7022 
7023 		/* a self-managed-reg device must have a private regdom */
7024 		if (WARN_ON(!regdom && self_managed)) {
7025 			nlmsg_free(msg);
7026 			return -EINVAL;
7027 		}
7028 
7029 		if (regdom &&
7030 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7031 			goto nla_put_failure;
7032 	}
7033 
7034 	if (!wiphy && reg_last_request_cell_base() &&
7035 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7036 			NL80211_USER_REG_HINT_CELL_BASE))
7037 		goto nla_put_failure;
7038 
7039 	rcu_read_lock();
7040 
7041 	if (!regdom)
7042 		regdom = rcu_dereference(cfg80211_regdomain);
7043 
7044 	if (nl80211_put_regdom(regdom, msg))
7045 		goto nla_put_failure_rcu;
7046 
7047 	rcu_read_unlock();
7048 
7049 	genlmsg_end(msg, hdr);
7050 	return genlmsg_reply(msg, info);
7051 
7052 nla_put_failure_rcu:
7053 	rcu_read_unlock();
7054 nla_put_failure:
7055 put_failure:
7056 	nlmsg_free(msg);
7057 	return -EMSGSIZE;
7058 }
7059 
nl80211_send_regdom(struct sk_buff * msg,struct netlink_callback * cb,u32 seq,int flags,struct wiphy * wiphy,const struct ieee80211_regdomain * regdom)7060 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7061 			       u32 seq, int flags, struct wiphy *wiphy,
7062 			       const struct ieee80211_regdomain *regdom)
7063 {
7064 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7065 				   NL80211_CMD_GET_REG);
7066 
7067 	if (!hdr)
7068 		return -1;
7069 
7070 	genl_dump_check_consistent(cb, hdr);
7071 
7072 	if (nl80211_put_regdom(regdom, msg))
7073 		goto nla_put_failure;
7074 
7075 	if (!wiphy && reg_last_request_cell_base() &&
7076 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7077 			NL80211_USER_REG_HINT_CELL_BASE))
7078 		goto nla_put_failure;
7079 
7080 	if (wiphy &&
7081 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7082 		goto nla_put_failure;
7083 
7084 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7085 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7086 		goto nla_put_failure;
7087 
7088 	genlmsg_end(msg, hdr);
7089 	return 0;
7090 
7091 nla_put_failure:
7092 	genlmsg_cancel(msg, hdr);
7093 	return -EMSGSIZE;
7094 }
7095 
nl80211_get_reg_dump(struct sk_buff * skb,struct netlink_callback * cb)7096 static int nl80211_get_reg_dump(struct sk_buff *skb,
7097 				struct netlink_callback *cb)
7098 {
7099 	const struct ieee80211_regdomain *regdom = NULL;
7100 	struct cfg80211_registered_device *rdev;
7101 	int err, reg_idx, start = cb->args[2];
7102 
7103 	rtnl_lock();
7104 
7105 	if (cfg80211_regdomain && start == 0) {
7106 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7107 					  NLM_F_MULTI, NULL,
7108 					  rtnl_dereference(cfg80211_regdomain));
7109 		if (err < 0)
7110 			goto out_err;
7111 	}
7112 
7113 	/* the global regdom is idx 0 */
7114 	reg_idx = 1;
7115 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7116 		regdom = get_wiphy_regdom(&rdev->wiphy);
7117 		if (!regdom)
7118 			continue;
7119 
7120 		if (++reg_idx <= start)
7121 			continue;
7122 
7123 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7124 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7125 		if (err < 0) {
7126 			reg_idx--;
7127 			break;
7128 		}
7129 	}
7130 
7131 	cb->args[2] = reg_idx;
7132 	err = skb->len;
7133 out_err:
7134 	rtnl_unlock();
7135 	return err;
7136 }
7137 
7138 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7139 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7140 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7141 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7142 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7143 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7144 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7145 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7146 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7147 };
7148 
parse_reg_rule(struct nlattr * tb[],struct ieee80211_reg_rule * reg_rule)7149 static int parse_reg_rule(struct nlattr *tb[],
7150 	struct ieee80211_reg_rule *reg_rule)
7151 {
7152 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7153 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7154 
7155 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7156 		return -EINVAL;
7157 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7158 		return -EINVAL;
7159 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7160 		return -EINVAL;
7161 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7162 		return -EINVAL;
7163 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7164 		return -EINVAL;
7165 
7166 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7167 
7168 	freq_range->start_freq_khz =
7169 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7170 	freq_range->end_freq_khz =
7171 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7172 	freq_range->max_bandwidth_khz =
7173 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7174 
7175 	power_rule->max_eirp =
7176 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7177 
7178 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7179 		power_rule->max_antenna_gain =
7180 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7181 
7182 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7183 		reg_rule->dfs_cac_ms =
7184 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7185 
7186 	return 0;
7187 }
7188 
nl80211_set_reg(struct sk_buff * skb,struct genl_info * info)7189 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7190 {
7191 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7192 	struct nlattr *nl_reg_rule;
7193 	char *alpha2;
7194 	int rem_reg_rules, r;
7195 	u32 num_rules = 0, rule_idx = 0;
7196 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7197 	struct ieee80211_regdomain *rd;
7198 
7199 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7200 		return -EINVAL;
7201 
7202 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7203 		return -EINVAL;
7204 
7205 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7206 
7207 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7208 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7209 
7210 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7211 			    rem_reg_rules) {
7212 		num_rules++;
7213 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7214 			return -EINVAL;
7215 	}
7216 
7217 	if (!reg_is_valid_request(alpha2))
7218 		return -EINVAL;
7219 
7220 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7221 	if (!rd)
7222 		return -ENOMEM;
7223 
7224 	rd->n_reg_rules = num_rules;
7225 	rd->alpha2[0] = alpha2[0];
7226 	rd->alpha2[1] = alpha2[1];
7227 
7228 	/*
7229 	 * Disable DFS master mode if the DFS region was
7230 	 * not supported or known on this kernel.
7231 	 */
7232 	if (reg_supported_dfs_region(dfs_region))
7233 		rd->dfs_region = dfs_region;
7234 
7235 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7236 			    rem_reg_rules) {
7237 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7238 						nl_reg_rule, reg_rule_policy,
7239 						info->extack);
7240 		if (r)
7241 			goto bad_reg;
7242 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7243 		if (r)
7244 			goto bad_reg;
7245 
7246 		rule_idx++;
7247 
7248 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7249 			r = -EINVAL;
7250 			goto bad_reg;
7251 		}
7252 	}
7253 
7254 	/* set_regdom takes ownership of rd */
7255 	return set_regdom(rd, REGD_SOURCE_CRDA);
7256  bad_reg:
7257 	kfree(rd);
7258 	return r;
7259 }
7260 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7261 
validate_scan_freqs(struct nlattr * freqs)7262 static int validate_scan_freqs(struct nlattr *freqs)
7263 {
7264 	struct nlattr *attr1, *attr2;
7265 	int n_channels = 0, tmp1, tmp2;
7266 
7267 	nla_for_each_nested(attr1, freqs, tmp1)
7268 		if (nla_len(attr1) != sizeof(u32))
7269 			return 0;
7270 
7271 	nla_for_each_nested(attr1, freqs, tmp1) {
7272 		n_channels++;
7273 		/*
7274 		 * Some hardware has a limited channel list for
7275 		 * scanning, and it is pretty much nonsensical
7276 		 * to scan for a channel twice, so disallow that
7277 		 * and don't require drivers to check that the
7278 		 * channel list they get isn't longer than what
7279 		 * they can scan, as long as they can scan all
7280 		 * the channels they registered at once.
7281 		 */
7282 		nla_for_each_nested(attr2, freqs, tmp2)
7283 			if (attr1 != attr2 &&
7284 			    nla_get_u32(attr1) == nla_get_u32(attr2))
7285 				return 0;
7286 	}
7287 
7288 	return n_channels;
7289 }
7290 
is_band_valid(struct wiphy * wiphy,enum nl80211_band b)7291 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7292 {
7293 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
7294 }
7295 
parse_bss_select(struct nlattr * nla,struct wiphy * wiphy,struct cfg80211_bss_selection * bss_select)7296 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7297 			    struct cfg80211_bss_selection *bss_select)
7298 {
7299 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7300 	struct nlattr *nest;
7301 	int err;
7302 	bool found = false;
7303 	int i;
7304 
7305 	/* only process one nested attribute */
7306 	nest = nla_data(nla);
7307 	if (!nla_ok(nest, nla_len(nest)))
7308 		return -EINVAL;
7309 
7310 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7311 					  nest, nl80211_bss_select_policy,
7312 					  NULL);
7313 	if (err)
7314 		return err;
7315 
7316 	/* only one attribute may be given */
7317 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7318 		if (attr[i]) {
7319 			if (found)
7320 				return -EINVAL;
7321 			found = true;
7322 		}
7323 	}
7324 
7325 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7326 
7327 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7328 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7329 
7330 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7331 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7332 		bss_select->param.band_pref =
7333 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7334 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
7335 			return -EINVAL;
7336 	}
7337 
7338 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7339 		struct nl80211_bss_select_rssi_adjust *adj_param;
7340 
7341 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7342 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7343 		bss_select->param.adjust.band = adj_param->band;
7344 		bss_select->param.adjust.delta = adj_param->delta;
7345 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7346 			return -EINVAL;
7347 	}
7348 
7349 	/* user-space did not provide behaviour attribute */
7350 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7351 		return -EINVAL;
7352 
7353 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7354 		return -EINVAL;
7355 
7356 	return 0;
7357 }
7358 
nl80211_parse_random_mac(struct nlattr ** attrs,u8 * mac_addr,u8 * mac_addr_mask)7359 int nl80211_parse_random_mac(struct nlattr **attrs,
7360 			     u8 *mac_addr, u8 *mac_addr_mask)
7361 {
7362 	int i;
7363 
7364 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7365 		eth_zero_addr(mac_addr);
7366 		eth_zero_addr(mac_addr_mask);
7367 		mac_addr[0] = 0x2;
7368 		mac_addr_mask[0] = 0x3;
7369 
7370 		return 0;
7371 	}
7372 
7373 	/* need both or none */
7374 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7375 		return -EINVAL;
7376 
7377 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7378 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7379 
7380 	/* don't allow or configure an mcast address */
7381 	if (!is_multicast_ether_addr(mac_addr_mask) ||
7382 	    is_multicast_ether_addr(mac_addr))
7383 		return -EINVAL;
7384 
7385 	/*
7386 	 * allow users to pass a MAC address that has bits set outside
7387 	 * of the mask, but don't bother drivers with having to deal
7388 	 * with such bits
7389 	 */
7390 	for (i = 0; i < ETH_ALEN; i++)
7391 		mac_addr[i] &= mac_addr_mask[i];
7392 
7393 	return 0;
7394 }
7395 
cfg80211_off_channel_oper_allowed(struct wireless_dev * wdev)7396 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7397 {
7398 	ASSERT_WDEV_LOCK(wdev);
7399 
7400 	if (!cfg80211_beaconing_iface_active(wdev))
7401 		return true;
7402 
7403 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7404 		return true;
7405 
7406 	return regulatory_pre_cac_allowed(wdev->wiphy);
7407 }
7408 
nl80211_check_scan_feat(struct wiphy * wiphy,u32 flags,u32 flag,enum nl80211_ext_feature_index feat)7409 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7410 				    enum nl80211_ext_feature_index feat)
7411 {
7412 	if (!(flags & flag))
7413 		return true;
7414 	if (wiphy_ext_feature_isset(wiphy, feat))
7415 		return true;
7416 	return false;
7417 }
7418 
7419 static int
nl80211_check_scan_flags(struct wiphy * wiphy,struct wireless_dev * wdev,void * request,struct nlattr ** attrs,bool is_sched_scan)7420 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7421 			 void *request, struct nlattr **attrs,
7422 			 bool is_sched_scan)
7423 {
7424 	u8 *mac_addr, *mac_addr_mask;
7425 	u32 *flags;
7426 	enum nl80211_feature_flags randomness_flag;
7427 
7428 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7429 		return 0;
7430 
7431 	if (is_sched_scan) {
7432 		struct cfg80211_sched_scan_request *req = request;
7433 
7434 		randomness_flag = wdev ?
7435 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7436 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7437 		flags = &req->flags;
7438 		mac_addr = req->mac_addr;
7439 		mac_addr_mask = req->mac_addr_mask;
7440 	} else {
7441 		struct cfg80211_scan_request *req = request;
7442 
7443 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7444 		flags = &req->flags;
7445 		mac_addr = req->mac_addr;
7446 		mac_addr_mask = req->mac_addr_mask;
7447 	}
7448 
7449 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7450 
7451 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7452 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7453 	    !nl80211_check_scan_feat(wiphy, *flags,
7454 				     NL80211_SCAN_FLAG_LOW_SPAN,
7455 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7456 	    !nl80211_check_scan_feat(wiphy, *flags,
7457 				     NL80211_SCAN_FLAG_LOW_POWER,
7458 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7459 	    !nl80211_check_scan_feat(wiphy, *flags,
7460 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
7461 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7462 	    !nl80211_check_scan_feat(wiphy, *flags,
7463 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7464 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7465 	    !nl80211_check_scan_feat(wiphy, *flags,
7466 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7467 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7468 	    !nl80211_check_scan_feat(wiphy, *flags,
7469 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7470 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7471 	    !nl80211_check_scan_feat(wiphy, *flags,
7472 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7473 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7474 	    !nl80211_check_scan_feat(wiphy, *flags,
7475 				     NL80211_SCAN_FLAG_RANDOM_SN,
7476 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7477 	    !nl80211_check_scan_feat(wiphy, *flags,
7478 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7479 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7480 		return -EOPNOTSUPP;
7481 
7482 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7483 		int err;
7484 
7485 		if (!(wiphy->features & randomness_flag) ||
7486 		    (wdev && wdev->current_bss))
7487 			return -EOPNOTSUPP;
7488 
7489 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7490 		if (err)
7491 			return err;
7492 	}
7493 
7494 	return 0;
7495 }
7496 
nl80211_trigger_scan(struct sk_buff * skb,struct genl_info * info)7497 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7498 {
7499 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7500 	struct wireless_dev *wdev = info->user_ptr[1];
7501 	struct cfg80211_scan_request *request;
7502 	struct nlattr *attr;
7503 	struct wiphy *wiphy;
7504 	int err, tmp, n_ssids = 0, n_channels, i;
7505 	size_t ie_len;
7506 
7507 	wiphy = &rdev->wiphy;
7508 
7509 	if (wdev->iftype == NL80211_IFTYPE_NAN)
7510 		return -EOPNOTSUPP;
7511 
7512 	if (!rdev->ops->scan)
7513 		return -EOPNOTSUPP;
7514 
7515 	if (rdev->scan_req || rdev->scan_msg) {
7516 		err = -EBUSY;
7517 		goto unlock;
7518 	}
7519 
7520 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7521 		n_channels = validate_scan_freqs(
7522 				info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7523 		if (!n_channels) {
7524 			err = -EINVAL;
7525 			goto unlock;
7526 		}
7527 	} else {
7528 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7529 	}
7530 
7531 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7532 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7533 			n_ssids++;
7534 
7535 	if (n_ssids > wiphy->max_scan_ssids) {
7536 		err = -EINVAL;
7537 		goto unlock;
7538 	}
7539 
7540 	if (info->attrs[NL80211_ATTR_IE])
7541 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7542 	else
7543 		ie_len = 0;
7544 
7545 	if (ie_len > wiphy->max_scan_ie_len) {
7546 		err = -EINVAL;
7547 		goto unlock;
7548 	}
7549 
7550 	request = kzalloc(sizeof(*request)
7551 			+ sizeof(*request->ssids) * n_ssids
7552 			+ sizeof(*request->channels) * n_channels
7553 			+ ie_len, GFP_KERNEL);
7554 	if (!request) {
7555 		err = -ENOMEM;
7556 		goto unlock;
7557 	}
7558 
7559 	if (n_ssids)
7560 		request->ssids = (void *)&request->channels[n_channels];
7561 	request->n_ssids = n_ssids;
7562 	if (ie_len) {
7563 		if (n_ssids)
7564 			request->ie = (void *)(request->ssids + n_ssids);
7565 		else
7566 			request->ie = (void *)(request->channels + n_channels);
7567 	}
7568 
7569 	i = 0;
7570 	if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7571 		/* user specified, bail out if channel not found */
7572 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7573 			struct ieee80211_channel *chan;
7574 
7575 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7576 
7577 			if (!chan) {
7578 				err = -EINVAL;
7579 				goto out_free;
7580 			}
7581 
7582 			/* ignore disabled channels */
7583 			if (chan->flags & IEEE80211_CHAN_DISABLED)
7584 				continue;
7585 
7586 			request->channels[i] = chan;
7587 			i++;
7588 		}
7589 	} else {
7590 		enum nl80211_band band;
7591 
7592 		/* all channels */
7593 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
7594 			int j;
7595 
7596 			if (!wiphy->bands[band])
7597 				continue;
7598 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7599 				struct ieee80211_channel *chan;
7600 
7601 				chan = &wiphy->bands[band]->channels[j];
7602 
7603 				if (chan->flags & IEEE80211_CHAN_DISABLED)
7604 					continue;
7605 
7606 				request->channels[i] = chan;
7607 				i++;
7608 			}
7609 		}
7610 	}
7611 
7612 	if (!i) {
7613 		err = -EINVAL;
7614 		goto out_free;
7615 	}
7616 
7617 	request->n_channels = i;
7618 
7619 	wdev_lock(wdev);
7620 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
7621 		struct ieee80211_channel *chan;
7622 
7623 		if (request->n_channels != 1) {
7624 			wdev_unlock(wdev);
7625 			err = -EBUSY;
7626 			goto out_free;
7627 		}
7628 
7629 		chan = request->channels[0];
7630 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
7631 			wdev_unlock(wdev);
7632 			err = -EBUSY;
7633 			goto out_free;
7634 		}
7635 	}
7636 	wdev_unlock(wdev);
7637 
7638 	i = 0;
7639 	if (n_ssids) {
7640 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7641 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7642 				err = -EINVAL;
7643 				goto out_free;
7644 			}
7645 			request->ssids[i].ssid_len = nla_len(attr);
7646 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7647 			i++;
7648 		}
7649 	}
7650 
7651 	if (info->attrs[NL80211_ATTR_IE]) {
7652 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7653 		memcpy((void *)request->ie,
7654 		       nla_data(info->attrs[NL80211_ATTR_IE]),
7655 		       request->ie_len);
7656 	}
7657 
7658 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7659 		if (wiphy->bands[i])
7660 			request->rates[i] =
7661 				(1 << wiphy->bands[i]->n_bitrates) - 1;
7662 
7663 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7664 		nla_for_each_nested(attr,
7665 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7666 				    tmp) {
7667 			enum nl80211_band band = nla_type(attr);
7668 
7669 			if (band < 0 || band >= NUM_NL80211_BANDS) {
7670 				err = -EINVAL;
7671 				goto out_free;
7672 			}
7673 
7674 			if (!wiphy->bands[band])
7675 				continue;
7676 
7677 			err = ieee80211_get_ratemask(wiphy->bands[band],
7678 						     nla_data(attr),
7679 						     nla_len(attr),
7680 						     &request->rates[band]);
7681 			if (err)
7682 				goto out_free;
7683 		}
7684 	}
7685 
7686 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7687 		if (!wiphy_ext_feature_isset(wiphy,
7688 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7689 			err = -EOPNOTSUPP;
7690 			goto out_free;
7691 		}
7692 
7693 		request->duration =
7694 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7695 		request->duration_mandatory =
7696 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7697 	}
7698 
7699 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7700 				       false);
7701 	if (err)
7702 		goto out_free;
7703 
7704 	request->no_cck =
7705 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7706 
7707 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
7708 	 * BSSID to scan for. This was problematic because that same attribute
7709 	 * was already used for another purpose (local random MAC address). The
7710 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7711 	 * compatibility with older userspace components, also use the
7712 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
7713 	 * the specific BSSID use case instead of the random MAC address
7714 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7715 	 */
7716 	if (info->attrs[NL80211_ATTR_BSSID])
7717 		memcpy(request->bssid,
7718 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7719 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7720 		 info->attrs[NL80211_ATTR_MAC])
7721 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7722 		       ETH_ALEN);
7723 	else
7724 		eth_broadcast_addr(request->bssid);
7725 
7726 	request->wdev = wdev;
7727 	request->wiphy = &rdev->wiphy;
7728 	request->scan_start = jiffies;
7729 
7730 	rdev->scan_req = request;
7731 	err = rdev_scan(rdev, request);
7732 
7733 	if (!err) {
7734 		nl80211_send_scan_start(rdev, wdev);
7735 		if (wdev->netdev)
7736 			dev_hold(wdev->netdev);
7737 	} else {
7738  out_free:
7739 		rdev->scan_req = NULL;
7740 		kfree(request);
7741 	}
7742 
7743  unlock:
7744 	return err;
7745 }
7746 
nl80211_abort_scan(struct sk_buff * skb,struct genl_info * info)7747 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7748 {
7749 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7750 	struct wireless_dev *wdev = info->user_ptr[1];
7751 
7752 	if (!rdev->ops->abort_scan)
7753 		return -EOPNOTSUPP;
7754 
7755 	if (rdev->scan_msg)
7756 		return 0;
7757 
7758 	if (!rdev->scan_req)
7759 		return -ENOENT;
7760 
7761 	rdev_abort_scan(rdev, wdev);
7762 	return 0;
7763 }
7764 
7765 static int
nl80211_parse_sched_scan_plans(struct wiphy * wiphy,int n_plans,struct cfg80211_sched_scan_request * request,struct nlattr ** attrs)7766 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7767 			       struct cfg80211_sched_scan_request *request,
7768 			       struct nlattr **attrs)
7769 {
7770 	int tmp, err, i = 0;
7771 	struct nlattr *attr;
7772 
7773 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7774 		u32 interval;
7775 
7776 		/*
7777 		 * If scan plans are not specified,
7778 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7779 		 * case one scan plan will be set with the specified scan
7780 		 * interval and infinite number of iterations.
7781 		 */
7782 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7783 		if (!interval)
7784 			return -EINVAL;
7785 
7786 		request->scan_plans[0].interval =
7787 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
7788 		if (!request->scan_plans[0].interval)
7789 			return -EINVAL;
7790 
7791 		if (request->scan_plans[0].interval >
7792 		    wiphy->max_sched_scan_plan_interval)
7793 			request->scan_plans[0].interval =
7794 				wiphy->max_sched_scan_plan_interval;
7795 
7796 		return 0;
7797 	}
7798 
7799 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7800 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7801 
7802 		if (WARN_ON(i >= n_plans))
7803 			return -EINVAL;
7804 
7805 		err = nla_parse_nested_deprecated(plan,
7806 						  NL80211_SCHED_SCAN_PLAN_MAX,
7807 						  attr, nl80211_plan_policy,
7808 						  NULL);
7809 		if (err)
7810 			return err;
7811 
7812 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7813 			return -EINVAL;
7814 
7815 		request->scan_plans[i].interval =
7816 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7817 		if (!request->scan_plans[i].interval ||
7818 		    request->scan_plans[i].interval >
7819 		    wiphy->max_sched_scan_plan_interval)
7820 			return -EINVAL;
7821 
7822 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7823 			request->scan_plans[i].iterations =
7824 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7825 			if (!request->scan_plans[i].iterations ||
7826 			    (request->scan_plans[i].iterations >
7827 			     wiphy->max_sched_scan_plan_iterations))
7828 				return -EINVAL;
7829 		} else if (i < n_plans - 1) {
7830 			/*
7831 			 * All scan plans but the last one must specify
7832 			 * a finite number of iterations
7833 			 */
7834 			return -EINVAL;
7835 		}
7836 
7837 		i++;
7838 	}
7839 
7840 	/*
7841 	 * The last scan plan must not specify the number of
7842 	 * iterations, it is supposed to run infinitely
7843 	 */
7844 	if (request->scan_plans[n_plans - 1].iterations)
7845 		return  -EINVAL;
7846 
7847 	return 0;
7848 }
7849 
7850 static int
nl80211_parse_sched_scan_per_band_rssi(struct wiphy * wiphy,struct cfg80211_match_set * match_sets,struct nlattr * tb_band_rssi,s32 rssi_thold)7851 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
7852 				       struct cfg80211_match_set *match_sets,
7853 				       struct nlattr *tb_band_rssi,
7854 				       s32 rssi_thold)
7855 {
7856 	struct nlattr *attr;
7857 	int i, tmp, ret = 0;
7858 
7859 	if (!wiphy_ext_feature_isset(wiphy,
7860 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
7861 		if (tb_band_rssi)
7862 			ret = -EOPNOTSUPP;
7863 		else
7864 			for (i = 0; i < NUM_NL80211_BANDS; i++)
7865 				match_sets->per_band_rssi_thold[i] =
7866 					NL80211_SCAN_RSSI_THOLD_OFF;
7867 		return ret;
7868 	}
7869 
7870 	for (i = 0; i < NUM_NL80211_BANDS; i++)
7871 		match_sets->per_band_rssi_thold[i] = rssi_thold;
7872 
7873 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
7874 		enum nl80211_band band = nla_type(attr);
7875 
7876 		if (band < 0 || band >= NUM_NL80211_BANDS)
7877 			return -EINVAL;
7878 
7879 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
7880 	}
7881 
7882 	return 0;
7883 }
7884 
7885 static struct cfg80211_sched_scan_request *
nl80211_parse_sched_scan(struct wiphy * wiphy,struct wireless_dev * wdev,struct nlattr ** attrs,int max_match_sets)7886 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7887 			 struct nlattr **attrs, int max_match_sets)
7888 {
7889 	struct cfg80211_sched_scan_request *request;
7890 	struct nlattr *attr;
7891 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7892 	enum nl80211_band band;
7893 	size_t ie_len;
7894 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7895 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7896 
7897 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7898 		n_channels = validate_scan_freqs(
7899 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7900 		if (!n_channels)
7901 			return ERR_PTR(-EINVAL);
7902 	} else {
7903 		n_channels = ieee80211_get_num_supported_channels(wiphy);
7904 	}
7905 
7906 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
7907 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7908 				    tmp)
7909 			n_ssids++;
7910 
7911 	if (n_ssids > wiphy->max_sched_scan_ssids)
7912 		return ERR_PTR(-EINVAL);
7913 
7914 	/*
7915 	 * First, count the number of 'real' matchsets. Due to an issue with
7916 	 * the old implementation, matchsets containing only the RSSI attribute
7917 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7918 	 * RSSI for all matchsets, rather than their own matchset for reporting
7919 	 * all APs with a strong RSSI. This is needed to be compatible with
7920 	 * older userspace that treated a matchset with only the RSSI as the
7921 	 * global RSSI for all other matchsets - if there are other matchsets.
7922 	 */
7923 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7924 		nla_for_each_nested(attr,
7925 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7926 				    tmp) {
7927 			struct nlattr *rssi;
7928 
7929 			err = nla_parse_nested_deprecated(tb,
7930 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7931 							  attr,
7932 							  nl80211_match_policy,
7933 							  NULL);
7934 			if (err)
7935 				return ERR_PTR(err);
7936 
7937 			/* SSID and BSSID are mutually exclusive */
7938 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7939 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7940 				return ERR_PTR(-EINVAL);
7941 
7942 			/* add other standalone attributes here */
7943 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7944 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7945 				n_match_sets++;
7946 				continue;
7947 			}
7948 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7949 			if (rssi)
7950 				default_match_rssi = nla_get_s32(rssi);
7951 		}
7952 	}
7953 
7954 	/* However, if there's no other matchset, add the RSSI one */
7955 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7956 		n_match_sets = 1;
7957 
7958 	if (n_match_sets > max_match_sets)
7959 		return ERR_PTR(-EINVAL);
7960 
7961 	if (attrs[NL80211_ATTR_IE])
7962 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7963 	else
7964 		ie_len = 0;
7965 
7966 	if (ie_len > wiphy->max_sched_scan_ie_len)
7967 		return ERR_PTR(-EINVAL);
7968 
7969 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7970 		/*
7971 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7972 		 * each scan plan already specifies its own interval
7973 		 */
7974 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7975 			return ERR_PTR(-EINVAL);
7976 
7977 		nla_for_each_nested(attr,
7978 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7979 			n_plans++;
7980 	} else {
7981 		/*
7982 		 * The scan interval attribute is kept for backward
7983 		 * compatibility. If no scan plans are specified and sched scan
7984 		 * interval is specified, one scan plan will be set with this
7985 		 * scan interval and infinite number of iterations.
7986 		 */
7987 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7988 			return ERR_PTR(-EINVAL);
7989 
7990 		n_plans = 1;
7991 	}
7992 
7993 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7994 		return ERR_PTR(-EINVAL);
7995 
7996 	if (!wiphy_ext_feature_isset(
7997 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7998 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7999 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8000 		return ERR_PTR(-EINVAL);
8001 
8002 	request = kzalloc(sizeof(*request)
8003 			+ sizeof(*request->ssids) * n_ssids
8004 			+ sizeof(*request->match_sets) * n_match_sets
8005 			+ sizeof(*request->scan_plans) * n_plans
8006 			+ sizeof(*request->channels) * n_channels
8007 			+ ie_len, GFP_KERNEL);
8008 	if (!request)
8009 		return ERR_PTR(-ENOMEM);
8010 
8011 	if (n_ssids)
8012 		request->ssids = (void *)&request->channels[n_channels];
8013 	request->n_ssids = n_ssids;
8014 	if (ie_len) {
8015 		if (n_ssids)
8016 			request->ie = (void *)(request->ssids + n_ssids);
8017 		else
8018 			request->ie = (void *)(request->channels + n_channels);
8019 	}
8020 
8021 	if (n_match_sets) {
8022 		if (request->ie)
8023 			request->match_sets = (void *)(request->ie + ie_len);
8024 		else if (n_ssids)
8025 			request->match_sets =
8026 				(void *)(request->ssids + n_ssids);
8027 		else
8028 			request->match_sets =
8029 				(void *)(request->channels + n_channels);
8030 	}
8031 	request->n_match_sets = n_match_sets;
8032 
8033 	if (n_match_sets)
8034 		request->scan_plans = (void *)(request->match_sets +
8035 					       n_match_sets);
8036 	else if (request->ie)
8037 		request->scan_plans = (void *)(request->ie + ie_len);
8038 	else if (n_ssids)
8039 		request->scan_plans = (void *)(request->ssids + n_ssids);
8040 	else
8041 		request->scan_plans = (void *)(request->channels + n_channels);
8042 
8043 	request->n_scan_plans = n_plans;
8044 
8045 	i = 0;
8046 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8047 		/* user specified, bail out if channel not found */
8048 		nla_for_each_nested(attr,
8049 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8050 				    tmp) {
8051 			struct ieee80211_channel *chan;
8052 
8053 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8054 
8055 			if (!chan) {
8056 				err = -EINVAL;
8057 				goto out_free;
8058 			}
8059 
8060 			/* ignore disabled channels */
8061 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8062 				continue;
8063 
8064 			request->channels[i] = chan;
8065 			i++;
8066 		}
8067 	} else {
8068 		/* all channels */
8069 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8070 			int j;
8071 
8072 			if (!wiphy->bands[band])
8073 				continue;
8074 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8075 				struct ieee80211_channel *chan;
8076 
8077 				chan = &wiphy->bands[band]->channels[j];
8078 
8079 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8080 					continue;
8081 
8082 				request->channels[i] = chan;
8083 				i++;
8084 			}
8085 		}
8086 	}
8087 
8088 	if (!i) {
8089 		err = -EINVAL;
8090 		goto out_free;
8091 	}
8092 
8093 	request->n_channels = i;
8094 
8095 	i = 0;
8096 	if (n_ssids) {
8097 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8098 				    tmp) {
8099 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8100 				err = -EINVAL;
8101 				goto out_free;
8102 			}
8103 			request->ssids[i].ssid_len = nla_len(attr);
8104 			memcpy(request->ssids[i].ssid, nla_data(attr),
8105 			       nla_len(attr));
8106 			i++;
8107 		}
8108 	}
8109 
8110 	i = 0;
8111 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8112 		nla_for_each_nested(attr,
8113 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8114 				    tmp) {
8115 			struct nlattr *ssid, *bssid, *rssi;
8116 
8117 			err = nla_parse_nested_deprecated(tb,
8118 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8119 							  attr,
8120 							  nl80211_match_policy,
8121 							  NULL);
8122 			if (err)
8123 				goto out_free;
8124 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8125 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8126 
8127 			if (!ssid && !bssid) {
8128 				i++;
8129 				continue;
8130 			}
8131 
8132 			if (WARN_ON(i >= n_match_sets)) {
8133 				/* this indicates a programming error,
8134 				 * the loop above should have verified
8135 				 * things properly
8136 				 */
8137 				err = -EINVAL;
8138 				goto out_free;
8139 			}
8140 
8141 			if (ssid) {
8142 				if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8143 					err = -EINVAL;
8144 					goto out_free;
8145 				}
8146 				memcpy(request->match_sets[i].ssid.ssid,
8147 				       nla_data(ssid), nla_len(ssid));
8148 				request->match_sets[i].ssid.ssid_len =
8149 					nla_len(ssid);
8150 			}
8151 			if (bssid) {
8152 				if (nla_len(bssid) != ETH_ALEN) {
8153 					err = -EINVAL;
8154 					goto out_free;
8155 				}
8156 				memcpy(request->match_sets[i].bssid,
8157 				       nla_data(bssid), ETH_ALEN);
8158 			}
8159 
8160 			/* special attribute - old implementation w/a */
8161 			request->match_sets[i].rssi_thold = default_match_rssi;
8162 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8163 			if (rssi)
8164 				request->match_sets[i].rssi_thold =
8165 					nla_get_s32(rssi);
8166 
8167 			/* Parse per band RSSI attribute */
8168 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8169 				&request->match_sets[i],
8170 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8171 				request->match_sets[i].rssi_thold);
8172 			if (err)
8173 				goto out_free;
8174 
8175 			i++;
8176 		}
8177 
8178 		/* there was no other matchset, so the RSSI one is alone */
8179 		if (i == 0 && n_match_sets)
8180 			request->match_sets[0].rssi_thold = default_match_rssi;
8181 
8182 		request->min_rssi_thold = INT_MAX;
8183 		for (i = 0; i < n_match_sets; i++)
8184 			request->min_rssi_thold =
8185 				min(request->match_sets[i].rssi_thold,
8186 				    request->min_rssi_thold);
8187 	} else {
8188 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8189 	}
8190 
8191 	if (ie_len) {
8192 		request->ie_len = ie_len;
8193 		memcpy((void *)request->ie,
8194 		       nla_data(attrs[NL80211_ATTR_IE]),
8195 		       request->ie_len);
8196 	}
8197 
8198 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8199 	if (err)
8200 		goto out_free;
8201 
8202 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8203 		request->delay =
8204 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8205 
8206 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8207 		request->relative_rssi = nla_get_s8(
8208 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8209 		request->relative_rssi_set = true;
8210 	}
8211 
8212 	if (request->relative_rssi_set &&
8213 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8214 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8215 
8216 		rssi_adjust = nla_data(
8217 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8218 		request->rssi_adjust.band = rssi_adjust->band;
8219 		request->rssi_adjust.delta = rssi_adjust->delta;
8220 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8221 			err = -EINVAL;
8222 			goto out_free;
8223 		}
8224 	}
8225 
8226 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8227 	if (err)
8228 		goto out_free;
8229 
8230 	request->scan_start = jiffies;
8231 
8232 	return request;
8233 
8234 out_free:
8235 	kfree(request);
8236 	return ERR_PTR(err);
8237 }
8238 
nl80211_start_sched_scan(struct sk_buff * skb,struct genl_info * info)8239 static int nl80211_start_sched_scan(struct sk_buff *skb,
8240 				    struct genl_info *info)
8241 {
8242 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8243 	struct net_device *dev = info->user_ptr[1];
8244 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8245 	struct cfg80211_sched_scan_request *sched_scan_req;
8246 	bool want_multi;
8247 	int err;
8248 
8249 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8250 		return -EOPNOTSUPP;
8251 
8252 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8253 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8254 	if (err)
8255 		return err;
8256 
8257 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8258 						  info->attrs,
8259 						  rdev->wiphy.max_match_sets);
8260 
8261 	err = PTR_ERR_OR_ZERO(sched_scan_req);
8262 	if (err)
8263 		goto out_err;
8264 
8265 	/* leave request id zero for legacy request
8266 	 * or if driver does not support multi-scheduled scan
8267 	 */
8268 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
8269 		while (!sched_scan_req->reqid)
8270 			sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8271 	}
8272 
8273 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8274 	if (err)
8275 		goto out_free;
8276 
8277 	sched_scan_req->dev = dev;
8278 	sched_scan_req->wiphy = &rdev->wiphy;
8279 
8280 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8281 		sched_scan_req->owner_nlportid = info->snd_portid;
8282 
8283 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8284 
8285 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8286 	return 0;
8287 
8288 out_free:
8289 	kfree(sched_scan_req);
8290 out_err:
8291 	return err;
8292 }
8293 
nl80211_stop_sched_scan(struct sk_buff * skb,struct genl_info * info)8294 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8295 				   struct genl_info *info)
8296 {
8297 	struct cfg80211_sched_scan_request *req;
8298 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8299 	u64 cookie;
8300 
8301 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8302 		return -EOPNOTSUPP;
8303 
8304 	if (info->attrs[NL80211_ATTR_COOKIE]) {
8305 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8306 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
8307 	}
8308 
8309 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8310 				     struct cfg80211_sched_scan_request,
8311 				     list);
8312 	if (!req || req->reqid ||
8313 	    (req->owner_nlportid &&
8314 	     req->owner_nlportid != info->snd_portid))
8315 		return -ENOENT;
8316 
8317 	return cfg80211_stop_sched_scan_req(rdev, req, false);
8318 }
8319 
nl80211_start_radar_detection(struct sk_buff * skb,struct genl_info * info)8320 static int nl80211_start_radar_detection(struct sk_buff *skb,
8321 					 struct genl_info *info)
8322 {
8323 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8324 	struct net_device *dev = info->user_ptr[1];
8325 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8326 	struct wiphy *wiphy = wdev->wiphy;
8327 	struct cfg80211_chan_def chandef;
8328 	enum nl80211_dfs_regions dfs_region;
8329 	unsigned int cac_time_ms;
8330 	int err;
8331 
8332 	dfs_region = reg_get_dfs_region(wiphy);
8333 	if (dfs_region == NL80211_DFS_UNSET)
8334 		return -EINVAL;
8335 
8336 	err = nl80211_parse_chandef(rdev, info, &chandef);
8337 	if (err)
8338 		return err;
8339 
8340 	if (netif_carrier_ok(dev))
8341 		return -EBUSY;
8342 
8343 	if (wdev->cac_started)
8344 		return -EBUSY;
8345 
8346 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8347 	if (err < 0)
8348 		return err;
8349 
8350 	if (err == 0)
8351 		return -EINVAL;
8352 
8353 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8354 		return -EINVAL;
8355 
8356 	/* CAC start is offloaded to HW and can't be started manually */
8357 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8358 		return -EOPNOTSUPP;
8359 
8360 	if (!rdev->ops->start_radar_detection)
8361 		return -EOPNOTSUPP;
8362 
8363 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8364 	if (WARN_ON(!cac_time_ms))
8365 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8366 
8367 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8368 	if (!err) {
8369 		wdev->chandef = chandef;
8370 		wdev->cac_started = true;
8371 		wdev->cac_start_time = jiffies;
8372 		wdev->cac_time_ms = cac_time_ms;
8373 	}
8374 	return err;
8375 }
8376 
nl80211_notify_radar_detection(struct sk_buff * skb,struct genl_info * info)8377 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8378 					  struct genl_info *info)
8379 {
8380 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8381 	struct net_device *dev = info->user_ptr[1];
8382 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8383 	struct wiphy *wiphy = wdev->wiphy;
8384 	struct cfg80211_chan_def chandef;
8385 	enum nl80211_dfs_regions dfs_region;
8386 	int err;
8387 
8388 	dfs_region = reg_get_dfs_region(wiphy);
8389 	if (dfs_region == NL80211_DFS_UNSET) {
8390 		GENL_SET_ERR_MSG(info,
8391 				 "DFS Region is not set. Unexpected Radar indication");
8392 		return -EINVAL;
8393 	}
8394 
8395 	err = nl80211_parse_chandef(rdev, info, &chandef);
8396 	if (err) {
8397 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8398 		return err;
8399 	}
8400 
8401 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8402 	if (err < 0) {
8403 		GENL_SET_ERR_MSG(info, "chandef is invalid");
8404 		return err;
8405 	}
8406 
8407 	if (err == 0) {
8408 		GENL_SET_ERR_MSG(info,
8409 				 "Unexpected Radar indication for chandef/iftype");
8410 		return -EINVAL;
8411 	}
8412 
8413 	/* Do not process this notification if radar is already detected
8414 	 * by kernel on this channel, and return success.
8415 	 */
8416 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8417 		return 0;
8418 
8419 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8420 
8421 	cfg80211_sched_dfs_chan_update(rdev);
8422 
8423 	rdev->radar_chandef = chandef;
8424 
8425 	/* Propagate this notification to other radios as well */
8426 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8427 
8428 	return 0;
8429 }
8430 
nl80211_channel_switch(struct sk_buff * skb,struct genl_info * info)8431 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8432 {
8433 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8434 	struct net_device *dev = info->user_ptr[1];
8435 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8436 	struct cfg80211_csa_settings params;
8437 	/* csa_attrs is defined static to avoid waste of stack size - this
8438 	 * function is called under RTNL lock, so this should not be a problem.
8439 	 */
8440 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8441 	int err;
8442 	bool need_new_beacon = false;
8443 	bool need_handle_dfs_flag = true;
8444 	int len, i;
8445 	u32 cs_count;
8446 
8447 	if (!rdev->ops->channel_switch ||
8448 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8449 		return -EOPNOTSUPP;
8450 
8451 	switch (dev->ieee80211_ptr->iftype) {
8452 	case NL80211_IFTYPE_AP:
8453 	case NL80211_IFTYPE_P2P_GO:
8454 		need_new_beacon = true;
8455 		/* For all modes except AP the handle_dfs flag needs to be
8456 		 * supplied to tell the kernel that userspace will handle radar
8457 		 * events when they happen. Otherwise a switch to a channel
8458 		 * requiring DFS will be rejected.
8459 		 */
8460 		need_handle_dfs_flag = false;
8461 
8462 		/* useless if AP is not running */
8463 		if (!wdev->beacon_interval)
8464 			return -ENOTCONN;
8465 		break;
8466 	case NL80211_IFTYPE_ADHOC:
8467 		if (!wdev->ssid_len)
8468 			return -ENOTCONN;
8469 		break;
8470 	case NL80211_IFTYPE_MESH_POINT:
8471 		if (!wdev->mesh_id_len)
8472 			return -ENOTCONN;
8473 		break;
8474 	default:
8475 		return -EOPNOTSUPP;
8476 	}
8477 
8478 	memset(&params, 0, sizeof(params));
8479 	params.beacon_csa.ftm_responder = -1;
8480 
8481 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8482 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8483 		return -EINVAL;
8484 
8485 	/* only important for AP, IBSS and mesh create IEs internally */
8486 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8487 		return -EINVAL;
8488 
8489 	/* Even though the attribute is u32, the specification says
8490 	 * u8, so let's make sure we don't overflow.
8491 	 */
8492 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8493 	if (cs_count > 255)
8494 		return -EINVAL;
8495 
8496 	params.count = cs_count;
8497 
8498 	if (!need_new_beacon)
8499 		goto skip_beacons;
8500 
8501 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8502 	if (err)
8503 		return err;
8504 
8505 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8506 					  info->attrs[NL80211_ATTR_CSA_IES],
8507 					  nl80211_policy, info->extack);
8508 	if (err)
8509 		return err;
8510 
8511 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8512 	if (err)
8513 		return err;
8514 
8515 	if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8516 		return -EINVAL;
8517 
8518 	len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8519 	if (!len || (len % sizeof(u16)))
8520 		return -EINVAL;
8521 
8522 	params.n_counter_offsets_beacon = len / sizeof(u16);
8523 	if (rdev->wiphy.max_num_csa_counters &&
8524 	    (params.n_counter_offsets_beacon >
8525 	     rdev->wiphy.max_num_csa_counters))
8526 		return -EINVAL;
8527 
8528 	params.counter_offsets_beacon =
8529 		nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8530 
8531 	/* sanity checks - counters should fit and be the same */
8532 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8533 		u16 offset = params.counter_offsets_beacon[i];
8534 
8535 		if (offset >= params.beacon_csa.tail_len)
8536 			return -EINVAL;
8537 
8538 		if (params.beacon_csa.tail[offset] != params.count)
8539 			return -EINVAL;
8540 	}
8541 
8542 	if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8543 		len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8544 		if (!len || (len % sizeof(u16)))
8545 			return -EINVAL;
8546 
8547 		params.n_counter_offsets_presp = len / sizeof(u16);
8548 		if (rdev->wiphy.max_num_csa_counters &&
8549 		    (params.n_counter_offsets_presp >
8550 		     rdev->wiphy.max_num_csa_counters))
8551 			return -EINVAL;
8552 
8553 		params.counter_offsets_presp =
8554 			nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8555 
8556 		/* sanity checks - counters should fit and be the same */
8557 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
8558 			u16 offset = params.counter_offsets_presp[i];
8559 
8560 			if (offset >= params.beacon_csa.probe_resp_len)
8561 				return -EINVAL;
8562 
8563 			if (params.beacon_csa.probe_resp[offset] !=
8564 			    params.count)
8565 				return -EINVAL;
8566 		}
8567 	}
8568 
8569 skip_beacons:
8570 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
8571 	if (err)
8572 		return err;
8573 
8574 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8575 					   wdev->iftype))
8576 		return -EINVAL;
8577 
8578 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
8579 					    &params.chandef,
8580 					    wdev->iftype);
8581 	if (err < 0)
8582 		return err;
8583 
8584 	if (err > 0) {
8585 		params.radar_required = true;
8586 		if (need_handle_dfs_flag &&
8587 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8588 			return -EINVAL;
8589 		}
8590 	}
8591 
8592 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8593 		params.block_tx = true;
8594 
8595 	wdev_lock(wdev);
8596 	err = rdev_channel_switch(rdev, dev, &params);
8597 	wdev_unlock(wdev);
8598 
8599 	return err;
8600 }
8601 
nl80211_send_bss(struct sk_buff * msg,struct netlink_callback * cb,u32 seq,int flags,struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,struct cfg80211_internal_bss * intbss)8602 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8603 			    u32 seq, int flags,
8604 			    struct cfg80211_registered_device *rdev,
8605 			    struct wireless_dev *wdev,
8606 			    struct cfg80211_internal_bss *intbss)
8607 {
8608 	struct cfg80211_bss *res = &intbss->pub;
8609 	const struct cfg80211_bss_ies *ies;
8610 	void *hdr;
8611 	struct nlattr *bss;
8612 
8613 	ASSERT_WDEV_LOCK(wdev);
8614 
8615 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8616 			     NL80211_CMD_NEW_SCAN_RESULTS);
8617 	if (!hdr)
8618 		return -1;
8619 
8620 	genl_dump_check_consistent(cb, hdr);
8621 
8622 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8623 		goto nla_put_failure;
8624 	if (wdev->netdev &&
8625 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8626 		goto nla_put_failure;
8627 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8628 			      NL80211_ATTR_PAD))
8629 		goto nla_put_failure;
8630 
8631 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8632 	if (!bss)
8633 		goto nla_put_failure;
8634 	if ((!is_zero_ether_addr(res->bssid) &&
8635 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8636 		goto nla_put_failure;
8637 
8638 	rcu_read_lock();
8639 	/* indicate whether we have probe response data or not */
8640 	if (rcu_access_pointer(res->proberesp_ies) &&
8641 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8642 		goto fail_unlock_rcu;
8643 
8644 	/* this pointer prefers to be pointed to probe response data
8645 	 * but is always valid
8646 	 */
8647 	ies = rcu_dereference(res->ies);
8648 	if (ies) {
8649 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8650 				      NL80211_BSS_PAD))
8651 			goto fail_unlock_rcu;
8652 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8653 					ies->len, ies->data))
8654 			goto fail_unlock_rcu;
8655 	}
8656 
8657 	/* and this pointer is always (unless driver didn't know) beacon data */
8658 	ies = rcu_dereference(res->beacon_ies);
8659 	if (ies && ies->from_beacon) {
8660 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8661 				      NL80211_BSS_PAD))
8662 			goto fail_unlock_rcu;
8663 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8664 					ies->len, ies->data))
8665 			goto fail_unlock_rcu;
8666 	}
8667 	rcu_read_unlock();
8668 
8669 	if (res->beacon_interval &&
8670 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8671 		goto nla_put_failure;
8672 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8673 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8674 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8675 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8676 			jiffies_to_msecs(jiffies - intbss->ts)))
8677 		goto nla_put_failure;
8678 
8679 	if (intbss->parent_tsf &&
8680 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8681 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
8682 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8683 		     intbss->parent_bssid)))
8684 		goto nla_put_failure;
8685 
8686 	if (intbss->ts_boottime &&
8687 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8688 			      intbss->ts_boottime, NL80211_BSS_PAD))
8689 		goto nla_put_failure;
8690 
8691 	if (!nl80211_put_signal(msg, intbss->pub.chains,
8692 				intbss->pub.chain_signal,
8693 				NL80211_BSS_CHAIN_SIGNAL))
8694 		goto nla_put_failure;
8695 
8696 	switch (rdev->wiphy.signal_type) {
8697 	case CFG80211_SIGNAL_TYPE_MBM:
8698 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8699 			goto nla_put_failure;
8700 		break;
8701 	case CFG80211_SIGNAL_TYPE_UNSPEC:
8702 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8703 			goto nla_put_failure;
8704 		break;
8705 	default:
8706 		break;
8707 	}
8708 
8709 	switch (wdev->iftype) {
8710 	case NL80211_IFTYPE_P2P_CLIENT:
8711 	case NL80211_IFTYPE_STATION:
8712 		if (intbss == wdev->current_bss &&
8713 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8714 				NL80211_BSS_STATUS_ASSOCIATED))
8715 			goto nla_put_failure;
8716 		break;
8717 	case NL80211_IFTYPE_ADHOC:
8718 		if (intbss == wdev->current_bss &&
8719 		    nla_put_u32(msg, NL80211_BSS_STATUS,
8720 				NL80211_BSS_STATUS_IBSS_JOINED))
8721 			goto nla_put_failure;
8722 		break;
8723 	default:
8724 		break;
8725 	}
8726 
8727 	nla_nest_end(msg, bss);
8728 
8729 	genlmsg_end(msg, hdr);
8730 	return 0;
8731 
8732  fail_unlock_rcu:
8733 	rcu_read_unlock();
8734  nla_put_failure:
8735 	genlmsg_cancel(msg, hdr);
8736 	return -EMSGSIZE;
8737 }
8738 
nl80211_dump_scan(struct sk_buff * skb,struct netlink_callback * cb)8739 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8740 {
8741 	struct cfg80211_registered_device *rdev;
8742 	struct cfg80211_internal_bss *scan;
8743 	struct wireless_dev *wdev;
8744 	int start = cb->args[2], idx = 0;
8745 	int err;
8746 
8747 	rtnl_lock();
8748 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8749 	if (err) {
8750 		rtnl_unlock();
8751 		return err;
8752 	}
8753 
8754 	wdev_lock(wdev);
8755 	spin_lock_bh(&rdev->bss_lock);
8756 
8757 	/*
8758 	 * dump_scan will be called multiple times to break up the scan results
8759 	 * into multiple messages.  It is unlikely that any more bss-es will be
8760 	 * expired after the first call, so only call only call this on the
8761 	 * first dump_scan invocation.
8762 	 */
8763 	if (start == 0)
8764 		cfg80211_bss_expire(rdev);
8765 
8766 	cb->seq = rdev->bss_generation;
8767 
8768 	list_for_each_entry(scan, &rdev->bss_list, list) {
8769 		if (++idx <= start)
8770 			continue;
8771 		if (nl80211_send_bss(skb, cb,
8772 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8773 				rdev, wdev, scan) < 0) {
8774 			idx--;
8775 			break;
8776 		}
8777 	}
8778 
8779 	spin_unlock_bh(&rdev->bss_lock);
8780 	wdev_unlock(wdev);
8781 
8782 	cb->args[2] = idx;
8783 	rtnl_unlock();
8784 
8785 	return skb->len;
8786 }
8787 
nl80211_send_survey(struct sk_buff * msg,u32 portid,u32 seq,int flags,struct net_device * dev,bool allow_radio_stats,struct survey_info * survey)8788 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8789 			       int flags, struct net_device *dev,
8790 			       bool allow_radio_stats,
8791 			       struct survey_info *survey)
8792 {
8793 	void *hdr;
8794 	struct nlattr *infoattr;
8795 
8796 	/* skip radio stats if userspace didn't request them */
8797 	if (!survey->channel && !allow_radio_stats)
8798 		return 0;
8799 
8800 	hdr = nl80211hdr_put(msg, portid, seq, flags,
8801 			     NL80211_CMD_NEW_SURVEY_RESULTS);
8802 	if (!hdr)
8803 		return -ENOMEM;
8804 
8805 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8806 		goto nla_put_failure;
8807 
8808 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
8809 	if (!infoattr)
8810 		goto nla_put_failure;
8811 
8812 	if (survey->channel &&
8813 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8814 			survey->channel->center_freq))
8815 		goto nla_put_failure;
8816 
8817 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8818 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8819 		goto nla_put_failure;
8820 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
8821 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8822 		goto nla_put_failure;
8823 	if ((survey->filled & SURVEY_INFO_TIME) &&
8824 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8825 			survey->time, NL80211_SURVEY_INFO_PAD))
8826 		goto nla_put_failure;
8827 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8828 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8829 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
8830 		goto nla_put_failure;
8831 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8832 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8833 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8834 		goto nla_put_failure;
8835 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8836 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8837 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
8838 		goto nla_put_failure;
8839 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8840 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8841 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
8842 		goto nla_put_failure;
8843 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8844 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8845 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
8846 		goto nla_put_failure;
8847 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
8848 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
8849 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
8850 		goto nla_put_failure;
8851 
8852 	nla_nest_end(msg, infoattr);
8853 
8854 	genlmsg_end(msg, hdr);
8855 	return 0;
8856 
8857  nla_put_failure:
8858 	genlmsg_cancel(msg, hdr);
8859 	return -EMSGSIZE;
8860 }
8861 
nl80211_dump_survey(struct sk_buff * skb,struct netlink_callback * cb)8862 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8863 {
8864 	struct nlattr **attrbuf;
8865 	struct survey_info survey;
8866 	struct cfg80211_registered_device *rdev;
8867 	struct wireless_dev *wdev;
8868 	int survey_idx = cb->args[2];
8869 	int res;
8870 	bool radio_stats;
8871 
8872 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
8873 	if (!attrbuf)
8874 		return -ENOMEM;
8875 
8876 	rtnl_lock();
8877 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8878 	if (res)
8879 		goto out_err;
8880 
8881 	/* prepare_wdev_dump parsed the attributes */
8882 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8883 
8884 	if (!wdev->netdev) {
8885 		res = -EINVAL;
8886 		goto out_err;
8887 	}
8888 
8889 	if (!rdev->ops->dump_survey) {
8890 		res = -EOPNOTSUPP;
8891 		goto out_err;
8892 	}
8893 
8894 	while (1) {
8895 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8896 		if (res == -ENOENT)
8897 			break;
8898 		if (res)
8899 			goto out_err;
8900 
8901 		/* don't send disabled channels, but do send non-channel data */
8902 		if (survey.channel &&
8903 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8904 			survey_idx++;
8905 			continue;
8906 		}
8907 
8908 		if (nl80211_send_survey(skb,
8909 				NETLINK_CB(cb->skb).portid,
8910 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
8911 				wdev->netdev, radio_stats, &survey) < 0)
8912 			goto out;
8913 		survey_idx++;
8914 	}
8915 
8916  out:
8917 	cb->args[2] = survey_idx;
8918 	res = skb->len;
8919  out_err:
8920 	kfree(attrbuf);
8921 	rtnl_unlock();
8922 	return res;
8923 }
8924 
nl80211_valid_wpa_versions(u32 wpa_versions)8925 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8926 {
8927 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8928 				  NL80211_WPA_VERSION_2 |
8929 				  NL80211_WPA_VERSION_3));
8930 }
8931 
nl80211_authenticate(struct sk_buff * skb,struct genl_info * info)8932 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8933 {
8934 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8935 	struct net_device *dev = info->user_ptr[1];
8936 	struct ieee80211_channel *chan;
8937 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8938 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
8939 	enum nl80211_auth_type auth_type;
8940 	struct key_parse key;
8941 	bool local_state_change;
8942 
8943 	if (!info->attrs[NL80211_ATTR_MAC])
8944 		return -EINVAL;
8945 
8946 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8947 		return -EINVAL;
8948 
8949 	if (!info->attrs[NL80211_ATTR_SSID])
8950 		return -EINVAL;
8951 
8952 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8953 		return -EINVAL;
8954 
8955 	err = nl80211_parse_key(info, &key);
8956 	if (err)
8957 		return err;
8958 
8959 	if (key.idx >= 0) {
8960 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8961 			return -EINVAL;
8962 		if (!key.p.key || !key.p.key_len)
8963 			return -EINVAL;
8964 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8965 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8966 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8967 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
8968 			return -EINVAL;
8969 		if (key.idx > 3)
8970 			return -EINVAL;
8971 	} else {
8972 		key.p.key_len = 0;
8973 		key.p.key = NULL;
8974 	}
8975 
8976 	if (key.idx >= 0) {
8977 		int i;
8978 		bool ok = false;
8979 
8980 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8981 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8982 				ok = true;
8983 				break;
8984 			}
8985 		}
8986 		if (!ok)
8987 			return -EINVAL;
8988 	}
8989 
8990 	if (!rdev->ops->auth)
8991 		return -EOPNOTSUPP;
8992 
8993 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8994 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8995 		return -EOPNOTSUPP;
8996 
8997 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8998 	chan = nl80211_get_valid_chan(&rdev->wiphy,
8999 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9000 	if (!chan)
9001 		return -EINVAL;
9002 
9003 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9004 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9005 
9006 	if (info->attrs[NL80211_ATTR_IE]) {
9007 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9008 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9009 	}
9010 
9011 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9012 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9013 		return -EINVAL;
9014 
9015 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
9016 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
9017 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9018 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9019 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
9020 		return -EINVAL;
9021 
9022 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9023 		if (auth_type != NL80211_AUTHTYPE_SAE &&
9024 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9025 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9026 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
9027 			return -EINVAL;
9028 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9029 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9030 		/* need to include at least Auth Transaction and Status Code */
9031 		if (auth_data_len < 4)
9032 			return -EINVAL;
9033 	}
9034 
9035 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9036 
9037 	/*
9038 	 * Since we no longer track auth state, ignore
9039 	 * requests to only change local state.
9040 	 */
9041 	if (local_state_change)
9042 		return 0;
9043 
9044 	wdev_lock(dev->ieee80211_ptr);
9045 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9046 				 ssid, ssid_len, ie, ie_len,
9047 				 key.p.key, key.p.key_len, key.idx,
9048 				 auth_data, auth_data_len);
9049 	wdev_unlock(dev->ieee80211_ptr);
9050 	return err;
9051 }
9052 
validate_pae_over_nl80211(struct cfg80211_registered_device * rdev,struct genl_info * info)9053 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9054 				     struct genl_info *info)
9055 {
9056 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9057 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9058 		return -EINVAL;
9059 	}
9060 
9061 	if (!rdev->ops->tx_control_port ||
9062 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9063 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9064 		return -EOPNOTSUPP;
9065 
9066 	return 0;
9067 }
9068 
nl80211_crypto_settings(struct cfg80211_registered_device * rdev,struct genl_info * info,struct cfg80211_crypto_settings * settings,int cipher_limit)9069 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9070 				   struct genl_info *info,
9071 				   struct cfg80211_crypto_settings *settings,
9072 				   int cipher_limit)
9073 {
9074 	memset(settings, 0, sizeof(*settings));
9075 
9076 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9077 
9078 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9079 		u16 proto;
9080 
9081 		proto = nla_get_u16(
9082 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9083 		settings->control_port_ethertype = cpu_to_be16(proto);
9084 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9085 		    proto != ETH_P_PAE)
9086 			return -EINVAL;
9087 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9088 			settings->control_port_no_encrypt = true;
9089 	} else
9090 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9091 
9092 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9093 		int r = validate_pae_over_nl80211(rdev, info);
9094 
9095 		if (r < 0)
9096 			return r;
9097 
9098 		settings->control_port_over_nl80211 = true;
9099 	}
9100 
9101 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9102 		void *data;
9103 		int len, i;
9104 
9105 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9106 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9107 		settings->n_ciphers_pairwise = len / sizeof(u32);
9108 
9109 		if (len % sizeof(u32))
9110 			return -EINVAL;
9111 
9112 		if (settings->n_ciphers_pairwise > cipher_limit)
9113 			return -EINVAL;
9114 
9115 		memcpy(settings->ciphers_pairwise, data, len);
9116 
9117 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9118 			if (!cfg80211_supported_cipher_suite(
9119 					&rdev->wiphy,
9120 					settings->ciphers_pairwise[i]))
9121 				return -EINVAL;
9122 	}
9123 
9124 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9125 		settings->cipher_group =
9126 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9127 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9128 						     settings->cipher_group))
9129 			return -EINVAL;
9130 	}
9131 
9132 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9133 		settings->wpa_versions =
9134 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9135 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9136 			return -EINVAL;
9137 	}
9138 
9139 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9140 		void *data;
9141 		int len;
9142 
9143 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9144 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9145 		settings->n_akm_suites = len / sizeof(u32);
9146 
9147 		if (len % sizeof(u32))
9148 			return -EINVAL;
9149 
9150 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9151 			return -EINVAL;
9152 
9153 		memcpy(settings->akm_suites, data, len);
9154 	}
9155 
9156 	if (info->attrs[NL80211_ATTR_PMK]) {
9157 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9158 			return -EINVAL;
9159 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9160 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9161 			return -EINVAL;
9162 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9163 	}
9164 
9165 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9166 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9167 					     NL80211_EXT_FEATURE_SAE_OFFLOAD))
9168 			return -EINVAL;
9169 		settings->sae_pwd =
9170 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9171 		settings->sae_pwd_len =
9172 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9173 	}
9174 
9175 	return 0;
9176 }
9177 
nl80211_associate(struct sk_buff * skb,struct genl_info * info)9178 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9179 {
9180 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9181 	struct net_device *dev = info->user_ptr[1];
9182 	struct ieee80211_channel *chan;
9183 	struct cfg80211_assoc_request req = {};
9184 	const u8 *bssid, *ssid;
9185 	int err, ssid_len = 0;
9186 
9187 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9188 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9189 		return -EPERM;
9190 
9191 	if (!info->attrs[NL80211_ATTR_MAC] ||
9192 	    !info->attrs[NL80211_ATTR_SSID] ||
9193 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9194 		return -EINVAL;
9195 
9196 	if (!rdev->ops->assoc)
9197 		return -EOPNOTSUPP;
9198 
9199 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9200 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9201 		return -EOPNOTSUPP;
9202 
9203 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9204 
9205 	chan = nl80211_get_valid_chan(&rdev->wiphy,
9206 				      info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9207 	if (!chan)
9208 		return -EINVAL;
9209 
9210 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9211 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9212 
9213 	if (info->attrs[NL80211_ATTR_IE]) {
9214 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9215 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9216 	}
9217 
9218 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9219 		enum nl80211_mfp mfp =
9220 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9221 		if (mfp == NL80211_MFP_REQUIRED)
9222 			req.use_mfp = true;
9223 		else if (mfp != NL80211_MFP_NO)
9224 			return -EINVAL;
9225 	}
9226 
9227 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9228 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9229 
9230 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9231 		req.flags |= ASSOC_REQ_DISABLE_HT;
9232 
9233 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9234 		memcpy(&req.ht_capa_mask,
9235 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9236 		       sizeof(req.ht_capa_mask));
9237 
9238 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9239 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9240 			return -EINVAL;
9241 		memcpy(&req.ht_capa,
9242 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9243 		       sizeof(req.ht_capa));
9244 	}
9245 
9246 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9247 		req.flags |= ASSOC_REQ_DISABLE_VHT;
9248 
9249 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9250 		memcpy(&req.vht_capa_mask,
9251 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9252 		       sizeof(req.vht_capa_mask));
9253 
9254 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9255 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9256 			return -EINVAL;
9257 		memcpy(&req.vht_capa,
9258 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9259 		       sizeof(req.vht_capa));
9260 	}
9261 
9262 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9263 		if (!((rdev->wiphy.features &
9264 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9265 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9266 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9267 					     NL80211_EXT_FEATURE_RRM))
9268 			return -EINVAL;
9269 		req.flags |= ASSOC_REQ_USE_RRM;
9270 	}
9271 
9272 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9273 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9274 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9275 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9276 			return -EINVAL;
9277 		req.fils_nonces =
9278 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9279 	}
9280 
9281 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9282 	if (!err) {
9283 		wdev_lock(dev->ieee80211_ptr);
9284 
9285 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9286 					  ssid, ssid_len, &req);
9287 
9288 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9289 			dev->ieee80211_ptr->conn_owner_nlportid =
9290 				info->snd_portid;
9291 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9292 			       bssid, ETH_ALEN);
9293 		}
9294 
9295 		wdev_unlock(dev->ieee80211_ptr);
9296 	}
9297 
9298 	return err;
9299 }
9300 
nl80211_deauthenticate(struct sk_buff * skb,struct genl_info * info)9301 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9302 {
9303 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9304 	struct net_device *dev = info->user_ptr[1];
9305 	const u8 *ie = NULL, *bssid;
9306 	int ie_len = 0, err;
9307 	u16 reason_code;
9308 	bool local_state_change;
9309 
9310 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9311 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9312 		return -EPERM;
9313 
9314 	if (!info->attrs[NL80211_ATTR_MAC])
9315 		return -EINVAL;
9316 
9317 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9318 		return -EINVAL;
9319 
9320 	if (!rdev->ops->deauth)
9321 		return -EOPNOTSUPP;
9322 
9323 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9324 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9325 		return -EOPNOTSUPP;
9326 
9327 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9328 
9329 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9330 	if (reason_code == 0) {
9331 		/* Reason Code 0 is reserved */
9332 		return -EINVAL;
9333 	}
9334 
9335 	if (info->attrs[NL80211_ATTR_IE]) {
9336 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9337 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9338 	}
9339 
9340 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9341 
9342 	wdev_lock(dev->ieee80211_ptr);
9343 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9344 				   local_state_change);
9345 	wdev_unlock(dev->ieee80211_ptr);
9346 	return err;
9347 }
9348 
nl80211_disassociate(struct sk_buff * skb,struct genl_info * info)9349 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9350 {
9351 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9352 	struct net_device *dev = info->user_ptr[1];
9353 	const u8 *ie = NULL, *bssid;
9354 	int ie_len = 0, err;
9355 	u16 reason_code;
9356 	bool local_state_change;
9357 
9358 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9359 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9360 		return -EPERM;
9361 
9362 	if (!info->attrs[NL80211_ATTR_MAC])
9363 		return -EINVAL;
9364 
9365 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9366 		return -EINVAL;
9367 
9368 	if (!rdev->ops->disassoc)
9369 		return -EOPNOTSUPP;
9370 
9371 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9372 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9373 		return -EOPNOTSUPP;
9374 
9375 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9376 
9377 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9378 	if (reason_code == 0) {
9379 		/* Reason Code 0 is reserved */
9380 		return -EINVAL;
9381 	}
9382 
9383 	if (info->attrs[NL80211_ATTR_IE]) {
9384 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9385 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9386 	}
9387 
9388 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9389 
9390 	wdev_lock(dev->ieee80211_ptr);
9391 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9392 				     local_state_change);
9393 	wdev_unlock(dev->ieee80211_ptr);
9394 	return err;
9395 }
9396 
9397 static bool
nl80211_parse_mcast_rate(struct cfg80211_registered_device * rdev,int mcast_rate[NUM_NL80211_BANDS],int rateval)9398 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9399 			 int mcast_rate[NUM_NL80211_BANDS],
9400 			 int rateval)
9401 {
9402 	struct wiphy *wiphy = &rdev->wiphy;
9403 	bool found = false;
9404 	int band, i;
9405 
9406 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
9407 		struct ieee80211_supported_band *sband;
9408 
9409 		sband = wiphy->bands[band];
9410 		if (!sband)
9411 			continue;
9412 
9413 		for (i = 0; i < sband->n_bitrates; i++) {
9414 			if (sband->bitrates[i].bitrate == rateval) {
9415 				mcast_rate[band] = i + 1;
9416 				found = true;
9417 				break;
9418 			}
9419 		}
9420 	}
9421 
9422 	return found;
9423 }
9424 
nl80211_join_ibss(struct sk_buff * skb,struct genl_info * info)9425 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
9426 {
9427 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9428 	struct net_device *dev = info->user_ptr[1];
9429 	struct cfg80211_ibss_params ibss;
9430 	struct wiphy *wiphy;
9431 	struct cfg80211_cached_keys *connkeys = NULL;
9432 	int err;
9433 
9434 	memset(&ibss, 0, sizeof(ibss));
9435 
9436 	if (!info->attrs[NL80211_ATTR_SSID] ||
9437 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9438 		return -EINVAL;
9439 
9440 	ibss.beacon_interval = 100;
9441 
9442 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9443 		ibss.beacon_interval =
9444 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9445 
9446 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9447 					   ibss.beacon_interval);
9448 	if (err)
9449 		return err;
9450 
9451 	if (!rdev->ops->join_ibss)
9452 		return -EOPNOTSUPP;
9453 
9454 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9455 		return -EOPNOTSUPP;
9456 
9457 	wiphy = &rdev->wiphy;
9458 
9459 	if (info->attrs[NL80211_ATTR_MAC]) {
9460 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9461 
9462 		if (!is_valid_ether_addr(ibss.bssid))
9463 			return -EINVAL;
9464 	}
9465 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9466 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9467 
9468 	if (info->attrs[NL80211_ATTR_IE]) {
9469 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9470 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9471 	}
9472 
9473 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9474 	if (err)
9475 		return err;
9476 
9477 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9478 				     NL80211_IFTYPE_ADHOC))
9479 		return -EINVAL;
9480 
9481 	switch (ibss.chandef.width) {
9482 	case NL80211_CHAN_WIDTH_5:
9483 	case NL80211_CHAN_WIDTH_10:
9484 	case NL80211_CHAN_WIDTH_20_NOHT:
9485 		break;
9486 	case NL80211_CHAN_WIDTH_20:
9487 	case NL80211_CHAN_WIDTH_40:
9488 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9489 			return -EINVAL;
9490 		break;
9491 	case NL80211_CHAN_WIDTH_80:
9492 	case NL80211_CHAN_WIDTH_80P80:
9493 	case NL80211_CHAN_WIDTH_160:
9494 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9495 			return -EINVAL;
9496 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9497 					     NL80211_EXT_FEATURE_VHT_IBSS))
9498 			return -EINVAL;
9499 		break;
9500 	default:
9501 		return -EINVAL;
9502 	}
9503 
9504 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9505 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9506 
9507 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9508 		u8 *rates =
9509 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9510 		int n_rates =
9511 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9512 		struct ieee80211_supported_band *sband =
9513 			wiphy->bands[ibss.chandef.chan->band];
9514 
9515 		err = ieee80211_get_ratemask(sband, rates, n_rates,
9516 					     &ibss.basic_rates);
9517 		if (err)
9518 			return err;
9519 	}
9520 
9521 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9522 		memcpy(&ibss.ht_capa_mask,
9523 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9524 		       sizeof(ibss.ht_capa_mask));
9525 
9526 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9527 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9528 			return -EINVAL;
9529 		memcpy(&ibss.ht_capa,
9530 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9531 		       sizeof(ibss.ht_capa));
9532 	}
9533 
9534 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9535 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9536 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9537 		return -EINVAL;
9538 
9539 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9540 		bool no_ht = false;
9541 
9542 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9543 		if (IS_ERR(connkeys))
9544 			return PTR_ERR(connkeys);
9545 
9546 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9547 		    no_ht) {
9548 			kzfree(connkeys);
9549 			return -EINVAL;
9550 		}
9551 	}
9552 
9553 	ibss.control_port =
9554 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9555 
9556 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9557 		int r = validate_pae_over_nl80211(rdev, info);
9558 
9559 		if (r < 0) {
9560 			kzfree(connkeys);
9561 			return r;
9562 		}
9563 
9564 		ibss.control_port_over_nl80211 = true;
9565 	}
9566 
9567 	ibss.userspace_handles_dfs =
9568 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9569 
9570 	wdev_lock(dev->ieee80211_ptr);
9571 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9572 	if (err)
9573 		kzfree(connkeys);
9574 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9575 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9576 	wdev_unlock(dev->ieee80211_ptr);
9577 
9578 	return err;
9579 }
9580 
nl80211_leave_ibss(struct sk_buff * skb,struct genl_info * info)9581 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9582 {
9583 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9584 	struct net_device *dev = info->user_ptr[1];
9585 
9586 	if (!rdev->ops->leave_ibss)
9587 		return -EOPNOTSUPP;
9588 
9589 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9590 		return -EOPNOTSUPP;
9591 
9592 	return cfg80211_leave_ibss(rdev, dev, false);
9593 }
9594 
nl80211_set_mcast_rate(struct sk_buff * skb,struct genl_info * info)9595 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9596 {
9597 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9598 	struct net_device *dev = info->user_ptr[1];
9599 	int mcast_rate[NUM_NL80211_BANDS];
9600 	u32 nla_rate;
9601 	int err;
9602 
9603 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9604 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9605 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9606 		return -EOPNOTSUPP;
9607 
9608 	if (!rdev->ops->set_mcast_rate)
9609 		return -EOPNOTSUPP;
9610 
9611 	memset(mcast_rate, 0, sizeof(mcast_rate));
9612 
9613 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9614 		return -EINVAL;
9615 
9616 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9617 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9618 		return -EINVAL;
9619 
9620 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9621 
9622 	return err;
9623 }
9624 
9625 static struct sk_buff *
__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,int approxlen,u32 portid,u32 seq,enum nl80211_commands cmd,enum nl80211_attrs attr,const struct nl80211_vendor_cmd_info * info,gfp_t gfp)9626 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9627 			    struct wireless_dev *wdev, int approxlen,
9628 			    u32 portid, u32 seq, enum nl80211_commands cmd,
9629 			    enum nl80211_attrs attr,
9630 			    const struct nl80211_vendor_cmd_info *info,
9631 			    gfp_t gfp)
9632 {
9633 	struct sk_buff *skb;
9634 	void *hdr;
9635 	struct nlattr *data;
9636 
9637 	skb = nlmsg_new(approxlen + 100, gfp);
9638 	if (!skb)
9639 		return NULL;
9640 
9641 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9642 	if (!hdr) {
9643 		kfree_skb(skb);
9644 		return NULL;
9645 	}
9646 
9647 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9648 		goto nla_put_failure;
9649 
9650 	if (info) {
9651 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9652 				info->vendor_id))
9653 			goto nla_put_failure;
9654 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9655 				info->subcmd))
9656 			goto nla_put_failure;
9657 	}
9658 
9659 	if (wdev) {
9660 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9661 				      wdev_id(wdev), NL80211_ATTR_PAD))
9662 			goto nla_put_failure;
9663 		if (wdev->netdev &&
9664 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9665 				wdev->netdev->ifindex))
9666 			goto nla_put_failure;
9667 	}
9668 
9669 	data = nla_nest_start_noflag(skb, attr);
9670 	if (!data)
9671 		goto nla_put_failure;
9672 
9673 	((void **)skb->cb)[0] = rdev;
9674 	((void **)skb->cb)[1] = hdr;
9675 	((void **)skb->cb)[2] = data;
9676 
9677 	return skb;
9678 
9679  nla_put_failure:
9680 	kfree_skb(skb);
9681 	return NULL;
9682 }
9683 
__cfg80211_alloc_event_skb(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_commands cmd,enum nl80211_attrs attr,unsigned int portid,int vendor_event_idx,int approxlen,gfp_t gfp)9684 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9685 					   struct wireless_dev *wdev,
9686 					   enum nl80211_commands cmd,
9687 					   enum nl80211_attrs attr,
9688 					   unsigned int portid,
9689 					   int vendor_event_idx,
9690 					   int approxlen, gfp_t gfp)
9691 {
9692 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9693 	const struct nl80211_vendor_cmd_info *info;
9694 
9695 	switch (cmd) {
9696 	case NL80211_CMD_TESTMODE:
9697 		if (WARN_ON(vendor_event_idx != -1))
9698 			return NULL;
9699 		info = NULL;
9700 		break;
9701 	case NL80211_CMD_VENDOR:
9702 		if (WARN_ON(vendor_event_idx < 0 ||
9703 			    vendor_event_idx >= wiphy->n_vendor_events))
9704 			return NULL;
9705 		info = &wiphy->vendor_events[vendor_event_idx];
9706 		break;
9707 	default:
9708 		WARN_ON(1);
9709 		return NULL;
9710 	}
9711 
9712 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9713 					   cmd, attr, info, gfp);
9714 }
9715 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9716 
__cfg80211_send_event_skb(struct sk_buff * skb,gfp_t gfp)9717 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9718 {
9719 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9720 	void *hdr = ((void **)skb->cb)[1];
9721 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
9722 	struct nlattr *data = ((void **)skb->cb)[2];
9723 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9724 
9725 	/* clear CB data for netlink core to own from now on */
9726 	memset(skb->cb, 0, sizeof(skb->cb));
9727 
9728 	nla_nest_end(skb, data);
9729 	genlmsg_end(skb, hdr);
9730 
9731 	if (nlhdr->nlmsg_pid) {
9732 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
9733 				nlhdr->nlmsg_pid);
9734 	} else {
9735 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9736 			mcgrp = NL80211_MCGRP_VENDOR;
9737 
9738 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
9739 					skb, 0, mcgrp, gfp);
9740 	}
9741 }
9742 EXPORT_SYMBOL(__cfg80211_send_event_skb);
9743 
9744 #ifdef CONFIG_NL80211_TESTMODE
nl80211_testmode_do(struct sk_buff * skb,struct genl_info * info)9745 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9746 {
9747 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9748 	struct wireless_dev *wdev =
9749 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9750 	int err;
9751 
9752 	if (!rdev->ops->testmode_cmd)
9753 		return -EOPNOTSUPP;
9754 
9755 	if (IS_ERR(wdev)) {
9756 		err = PTR_ERR(wdev);
9757 		if (err != -EINVAL)
9758 			return err;
9759 		wdev = NULL;
9760 	} else if (wdev->wiphy != &rdev->wiphy) {
9761 		return -EINVAL;
9762 	}
9763 
9764 	if (!info->attrs[NL80211_ATTR_TESTDATA])
9765 		return -EINVAL;
9766 
9767 	rdev->cur_cmd_info = info;
9768 	err = rdev_testmode_cmd(rdev, wdev,
9769 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9770 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9771 	rdev->cur_cmd_info = NULL;
9772 
9773 	return err;
9774 }
9775 
nl80211_testmode_dump(struct sk_buff * skb,struct netlink_callback * cb)9776 static int nl80211_testmode_dump(struct sk_buff *skb,
9777 				 struct netlink_callback *cb)
9778 {
9779 	struct cfg80211_registered_device *rdev;
9780 	struct nlattr **attrbuf = NULL;
9781 	int err;
9782 	long phy_idx;
9783 	void *data = NULL;
9784 	int data_len = 0;
9785 
9786 	rtnl_lock();
9787 
9788 	if (cb->args[0]) {
9789 		/*
9790 		 * 0 is a valid index, but not valid for args[0],
9791 		 * so we need to offset by 1.
9792 		 */
9793 		phy_idx = cb->args[0] - 1;
9794 
9795 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9796 		if (!rdev) {
9797 			err = -ENOENT;
9798 			goto out_err;
9799 		}
9800 	} else {
9801 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
9802 				  GFP_KERNEL);
9803 		if (!attrbuf) {
9804 			err = -ENOMEM;
9805 			goto out_err;
9806 		}
9807 
9808 		err = nlmsg_parse_deprecated(cb->nlh,
9809 					     GENL_HDRLEN + nl80211_fam.hdrsize,
9810 					     attrbuf, nl80211_fam.maxattr,
9811 					     nl80211_policy, NULL);
9812 		if (err)
9813 			goto out_err;
9814 
9815 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9816 		if (IS_ERR(rdev)) {
9817 			err = PTR_ERR(rdev);
9818 			goto out_err;
9819 		}
9820 		phy_idx = rdev->wiphy_idx;
9821 
9822 		if (attrbuf[NL80211_ATTR_TESTDATA])
9823 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9824 	}
9825 
9826 	if (cb->args[1]) {
9827 		data = nla_data((void *)cb->args[1]);
9828 		data_len = nla_len((void *)cb->args[1]);
9829 	}
9830 
9831 	if (!rdev->ops->testmode_dump) {
9832 		err = -EOPNOTSUPP;
9833 		goto out_err;
9834 	}
9835 
9836 	while (1) {
9837 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9838 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
9839 					   NL80211_CMD_TESTMODE);
9840 		struct nlattr *tmdata;
9841 
9842 		if (!hdr)
9843 			break;
9844 
9845 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9846 			genlmsg_cancel(skb, hdr);
9847 			break;
9848 		}
9849 
9850 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
9851 		if (!tmdata) {
9852 			genlmsg_cancel(skb, hdr);
9853 			break;
9854 		}
9855 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9856 		nla_nest_end(skb, tmdata);
9857 
9858 		if (err == -ENOBUFS || err == -ENOENT) {
9859 			genlmsg_cancel(skb, hdr);
9860 			break;
9861 		} else if (err) {
9862 			genlmsg_cancel(skb, hdr);
9863 			goto out_err;
9864 		}
9865 
9866 		genlmsg_end(skb, hdr);
9867 	}
9868 
9869 	err = skb->len;
9870 	/* see above */
9871 	cb->args[0] = phy_idx + 1;
9872  out_err:
9873 	kfree(attrbuf);
9874 	rtnl_unlock();
9875 	return err;
9876 }
9877 #endif
9878 
nl80211_connect(struct sk_buff * skb,struct genl_info * info)9879 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9880 {
9881 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9882 	struct net_device *dev = info->user_ptr[1];
9883 	struct cfg80211_connect_params connect;
9884 	struct wiphy *wiphy;
9885 	struct cfg80211_cached_keys *connkeys = NULL;
9886 	int err;
9887 
9888 	memset(&connect, 0, sizeof(connect));
9889 
9890 	if (!info->attrs[NL80211_ATTR_SSID] ||
9891 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
9892 		return -EINVAL;
9893 
9894 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9895 		connect.auth_type =
9896 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9897 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9898 					     NL80211_CMD_CONNECT))
9899 			return -EINVAL;
9900 	} else
9901 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9902 
9903 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9904 
9905 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9906 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9907 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9908 		return -EINVAL;
9909 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9910 
9911 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9912 				      NL80211_MAX_NR_CIPHER_SUITES);
9913 	if (err)
9914 		return err;
9915 
9916 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9917 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9918 		return -EOPNOTSUPP;
9919 
9920 	wiphy = &rdev->wiphy;
9921 
9922 	connect.bg_scan_period = -1;
9923 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9924 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9925 		connect.bg_scan_period =
9926 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9927 	}
9928 
9929 	if (info->attrs[NL80211_ATTR_MAC])
9930 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9931 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
9932 		connect.bssid_hint =
9933 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9934 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9935 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9936 
9937 	if (info->attrs[NL80211_ATTR_IE]) {
9938 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9939 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9940 	}
9941 
9942 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9943 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9944 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
9945 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9946 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
9947 			return -EOPNOTSUPP;
9948 	} else {
9949 		connect.mfp = NL80211_MFP_NO;
9950 	}
9951 
9952 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9953 		connect.prev_bssid =
9954 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9955 
9956 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9957 		connect.channel = nl80211_get_valid_chan(
9958 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9959 		if (!connect.channel)
9960 			return -EINVAL;
9961 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9962 		connect.channel_hint = nl80211_get_valid_chan(
9963 			wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9964 		if (!connect.channel_hint)
9965 			return -EINVAL;
9966 	}
9967 
9968 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
9969 		connect.edmg.channels =
9970 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
9971 
9972 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
9973 			connect.edmg.bw_config =
9974 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
9975 	}
9976 
9977 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9978 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9979 		if (IS_ERR(connkeys))
9980 			return PTR_ERR(connkeys);
9981 	}
9982 
9983 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9984 		connect.flags |= ASSOC_REQ_DISABLE_HT;
9985 
9986 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9987 		memcpy(&connect.ht_capa_mask,
9988 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9989 		       sizeof(connect.ht_capa_mask));
9990 
9991 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9992 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9993 			kzfree(connkeys);
9994 			return -EINVAL;
9995 		}
9996 		memcpy(&connect.ht_capa,
9997 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9998 		       sizeof(connect.ht_capa));
9999 	}
10000 
10001 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10002 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
10003 
10004 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10005 		memcpy(&connect.vht_capa_mask,
10006 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10007 		       sizeof(connect.vht_capa_mask));
10008 
10009 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10010 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10011 			kzfree(connkeys);
10012 			return -EINVAL;
10013 		}
10014 		memcpy(&connect.vht_capa,
10015 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10016 		       sizeof(connect.vht_capa));
10017 	}
10018 
10019 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10020 		if (!((rdev->wiphy.features &
10021 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10022 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10023 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10024 					     NL80211_EXT_FEATURE_RRM)) {
10025 			kzfree(connkeys);
10026 			return -EINVAL;
10027 		}
10028 		connect.flags |= ASSOC_REQ_USE_RRM;
10029 	}
10030 
10031 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10032 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10033 		kzfree(connkeys);
10034 		return -EOPNOTSUPP;
10035 	}
10036 
10037 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10038 		/* bss selection makes no sense if bssid is set */
10039 		if (connect.bssid) {
10040 			kzfree(connkeys);
10041 			return -EINVAL;
10042 		}
10043 
10044 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10045 				       wiphy, &connect.bss_select);
10046 		if (err) {
10047 			kzfree(connkeys);
10048 			return err;
10049 		}
10050 	}
10051 
10052 	if (wiphy_ext_feature_isset(&rdev->wiphy,
10053 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10054 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10055 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10056 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10057 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10058 		connect.fils_erp_username =
10059 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10060 		connect.fils_erp_username_len =
10061 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10062 		connect.fils_erp_realm =
10063 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10064 		connect.fils_erp_realm_len =
10065 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10066 		connect.fils_erp_next_seq_num =
10067 			nla_get_u16(
10068 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10069 		connect.fils_erp_rrk =
10070 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10071 		connect.fils_erp_rrk_len =
10072 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10073 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10074 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10075 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10076 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10077 		kzfree(connkeys);
10078 		return -EINVAL;
10079 	}
10080 
10081 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10082 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10083 			kzfree(connkeys);
10084 			GENL_SET_ERR_MSG(info,
10085 					 "external auth requires connection ownership");
10086 			return -EINVAL;
10087 		}
10088 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10089 	}
10090 
10091 	wdev_lock(dev->ieee80211_ptr);
10092 
10093 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10094 			       connect.prev_bssid);
10095 	if (err)
10096 		kzfree(connkeys);
10097 
10098 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10099 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10100 		if (connect.bssid)
10101 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10102 			       connect.bssid, ETH_ALEN);
10103 		else
10104 			memset(dev->ieee80211_ptr->disconnect_bssid,
10105 			       0, ETH_ALEN);
10106 	}
10107 
10108 	wdev_unlock(dev->ieee80211_ptr);
10109 
10110 	return err;
10111 }
10112 
nl80211_update_connect_params(struct sk_buff * skb,struct genl_info * info)10113 static int nl80211_update_connect_params(struct sk_buff *skb,
10114 					 struct genl_info *info)
10115 {
10116 	struct cfg80211_connect_params connect = {};
10117 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10118 	struct net_device *dev = info->user_ptr[1];
10119 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10120 	bool fils_sk_offload;
10121 	u32 auth_type;
10122 	u32 changed = 0;
10123 	int ret;
10124 
10125 	if (!rdev->ops->update_connect_params)
10126 		return -EOPNOTSUPP;
10127 
10128 	if (info->attrs[NL80211_ATTR_IE]) {
10129 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10130 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10131 		changed |= UPDATE_ASSOC_IES;
10132 	}
10133 
10134 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10135 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10136 
10137 	/*
10138 	 * when driver supports fils-sk offload all attributes must be
10139 	 * provided. So the else covers "fils-sk-not-all" and
10140 	 * "no-fils-sk-any".
10141 	 */
10142 	if (fils_sk_offload &&
10143 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10144 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10145 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10146 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10147 		connect.fils_erp_username =
10148 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10149 		connect.fils_erp_username_len =
10150 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10151 		connect.fils_erp_realm =
10152 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10153 		connect.fils_erp_realm_len =
10154 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10155 		connect.fils_erp_next_seq_num =
10156 			nla_get_u16(
10157 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10158 		connect.fils_erp_rrk =
10159 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10160 		connect.fils_erp_rrk_len =
10161 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10162 		changed |= UPDATE_FILS_ERP_INFO;
10163 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10164 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10165 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10166 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10167 		return -EINVAL;
10168 	}
10169 
10170 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10171 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10172 		if (!nl80211_valid_auth_type(rdev, auth_type,
10173 					     NL80211_CMD_CONNECT))
10174 			return -EINVAL;
10175 
10176 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10177 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10178 			return -EINVAL;
10179 
10180 		connect.auth_type = auth_type;
10181 		changed |= UPDATE_AUTH_TYPE;
10182 	}
10183 
10184 	wdev_lock(dev->ieee80211_ptr);
10185 	if (!wdev->current_bss)
10186 		ret = -ENOLINK;
10187 	else
10188 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10189 	wdev_unlock(dev->ieee80211_ptr);
10190 
10191 	return ret;
10192 }
10193 
nl80211_disconnect(struct sk_buff * skb,struct genl_info * info)10194 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10195 {
10196 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10197 	struct net_device *dev = info->user_ptr[1];
10198 	u16 reason;
10199 	int ret;
10200 
10201 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10202 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10203 		return -EPERM;
10204 
10205 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10206 		reason = WLAN_REASON_DEAUTH_LEAVING;
10207 	else
10208 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10209 
10210 	if (reason == 0)
10211 		return -EINVAL;
10212 
10213 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10214 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10215 		return -EOPNOTSUPP;
10216 
10217 	wdev_lock(dev->ieee80211_ptr);
10218 	ret = cfg80211_disconnect(rdev, dev, reason, true);
10219 	wdev_unlock(dev->ieee80211_ptr);
10220 	return ret;
10221 }
10222 
nl80211_wiphy_netns(struct sk_buff * skb,struct genl_info * info)10223 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10224 {
10225 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10226 	struct net *net;
10227 	int err;
10228 
10229 	if (info->attrs[NL80211_ATTR_PID]) {
10230 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10231 
10232 		net = get_net_ns_by_pid(pid);
10233 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10234 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10235 
10236 		net = get_net_ns_by_fd(fd);
10237 	} else {
10238 		return -EINVAL;
10239 	}
10240 
10241 	if (IS_ERR(net))
10242 		return PTR_ERR(net);
10243 
10244 	err = 0;
10245 
10246 	/* check if anything to do */
10247 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
10248 		err = cfg80211_switch_netns(rdev, net);
10249 
10250 	put_net(net);
10251 	return err;
10252 }
10253 
nl80211_setdel_pmksa(struct sk_buff * skb,struct genl_info * info)10254 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10255 {
10256 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10257 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10258 			struct cfg80211_pmksa *pmksa) = NULL;
10259 	struct net_device *dev = info->user_ptr[1];
10260 	struct cfg80211_pmksa pmksa;
10261 
10262 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10263 
10264 	if (!info->attrs[NL80211_ATTR_PMKID])
10265 		return -EINVAL;
10266 
10267 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10268 
10269 	if (info->attrs[NL80211_ATTR_MAC]) {
10270 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10271 	} else if (info->attrs[NL80211_ATTR_SSID] &&
10272 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10273 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10274 		    info->attrs[NL80211_ATTR_PMK])) {
10275 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10276 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10277 		pmksa.cache_id =
10278 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10279 	} else {
10280 		return -EINVAL;
10281 	}
10282 	if (info->attrs[NL80211_ATTR_PMK]) {
10283 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10284 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10285 	}
10286 
10287 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10288 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10289 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10290 	      wiphy_ext_feature_isset(&rdev->wiphy,
10291 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10292 		return -EOPNOTSUPP;
10293 
10294 	switch (info->genlhdr->cmd) {
10295 	case NL80211_CMD_SET_PMKSA:
10296 		rdev_ops = rdev->ops->set_pmksa;
10297 		break;
10298 	case NL80211_CMD_DEL_PMKSA:
10299 		rdev_ops = rdev->ops->del_pmksa;
10300 		break;
10301 	default:
10302 		WARN_ON(1);
10303 		break;
10304 	}
10305 
10306 	if (!rdev_ops)
10307 		return -EOPNOTSUPP;
10308 
10309 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
10310 }
10311 
nl80211_flush_pmksa(struct sk_buff * skb,struct genl_info * info)10312 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10313 {
10314 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10315 	struct net_device *dev = info->user_ptr[1];
10316 
10317 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10318 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10319 		return -EOPNOTSUPP;
10320 
10321 	if (!rdev->ops->flush_pmksa)
10322 		return -EOPNOTSUPP;
10323 
10324 	return rdev_flush_pmksa(rdev, dev);
10325 }
10326 
nl80211_tdls_mgmt(struct sk_buff * skb,struct genl_info * info)10327 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10328 {
10329 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10330 	struct net_device *dev = info->user_ptr[1];
10331 	u8 action_code, dialog_token;
10332 	u32 peer_capability = 0;
10333 	u16 status_code;
10334 	u8 *peer;
10335 	bool initiator;
10336 
10337 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10338 	    !rdev->ops->tdls_mgmt)
10339 		return -EOPNOTSUPP;
10340 
10341 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10342 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10343 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10344 	    !info->attrs[NL80211_ATTR_IE] ||
10345 	    !info->attrs[NL80211_ATTR_MAC])
10346 		return -EINVAL;
10347 
10348 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10349 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10350 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10351 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10352 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10353 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10354 		peer_capability =
10355 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10356 
10357 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10358 			      dialog_token, status_code, peer_capability,
10359 			      initiator,
10360 			      nla_data(info->attrs[NL80211_ATTR_IE]),
10361 			      nla_len(info->attrs[NL80211_ATTR_IE]));
10362 }
10363 
nl80211_tdls_oper(struct sk_buff * skb,struct genl_info * info)10364 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10365 {
10366 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10367 	struct net_device *dev = info->user_ptr[1];
10368 	enum nl80211_tdls_operation operation;
10369 	u8 *peer;
10370 
10371 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10372 	    !rdev->ops->tdls_oper)
10373 		return -EOPNOTSUPP;
10374 
10375 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10376 	    !info->attrs[NL80211_ATTR_MAC])
10377 		return -EINVAL;
10378 
10379 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10380 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10381 
10382 	return rdev_tdls_oper(rdev, dev, peer, operation);
10383 }
10384 
nl80211_remain_on_channel(struct sk_buff * skb,struct genl_info * info)10385 static int nl80211_remain_on_channel(struct sk_buff *skb,
10386 				     struct genl_info *info)
10387 {
10388 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10389 	struct wireless_dev *wdev = info->user_ptr[1];
10390 	struct cfg80211_chan_def chandef;
10391 	const struct cfg80211_chan_def *compat_chandef;
10392 	struct sk_buff *msg;
10393 	void *hdr;
10394 	u64 cookie;
10395 	u32 duration;
10396 	int err;
10397 
10398 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10399 	    !info->attrs[NL80211_ATTR_DURATION])
10400 		return -EINVAL;
10401 
10402 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10403 
10404 	if (!rdev->ops->remain_on_channel ||
10405 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10406 		return -EOPNOTSUPP;
10407 
10408 	/*
10409 	 * We should be on that channel for at least a minimum amount of
10410 	 * time (10ms) but no longer than the driver supports.
10411 	 */
10412 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10413 	    duration > rdev->wiphy.max_remain_on_channel_duration)
10414 		return -EINVAL;
10415 
10416 	err = nl80211_parse_chandef(rdev, info, &chandef);
10417 	if (err)
10418 		return err;
10419 
10420 	wdev_lock(wdev);
10421 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
10422 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10423 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10424 							     &chandef);
10425 		if (compat_chandef != &chandef) {
10426 			wdev_unlock(wdev);
10427 			return -EBUSY;
10428 		}
10429 	}
10430 	wdev_unlock(wdev);
10431 
10432 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10433 	if (!msg)
10434 		return -ENOMEM;
10435 
10436 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10437 			     NL80211_CMD_REMAIN_ON_CHANNEL);
10438 	if (!hdr) {
10439 		err = -ENOBUFS;
10440 		goto free_msg;
10441 	}
10442 
10443 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10444 				     duration, &cookie);
10445 
10446 	if (err)
10447 		goto free_msg;
10448 
10449 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10450 			      NL80211_ATTR_PAD))
10451 		goto nla_put_failure;
10452 
10453 	genlmsg_end(msg, hdr);
10454 
10455 	return genlmsg_reply(msg, info);
10456 
10457  nla_put_failure:
10458 	err = -ENOBUFS;
10459  free_msg:
10460 	nlmsg_free(msg);
10461 	return err;
10462 }
10463 
nl80211_cancel_remain_on_channel(struct sk_buff * skb,struct genl_info * info)10464 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10465 					    struct genl_info *info)
10466 {
10467 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10468 	struct wireless_dev *wdev = info->user_ptr[1];
10469 	u64 cookie;
10470 
10471 	if (!info->attrs[NL80211_ATTR_COOKIE])
10472 		return -EINVAL;
10473 
10474 	if (!rdev->ops->cancel_remain_on_channel)
10475 		return -EOPNOTSUPP;
10476 
10477 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10478 
10479 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10480 }
10481 
nl80211_set_tx_bitrate_mask(struct sk_buff * skb,struct genl_info * info)10482 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10483 				       struct genl_info *info)
10484 {
10485 	struct cfg80211_bitrate_mask mask;
10486 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10487 	struct net_device *dev = info->user_ptr[1];
10488 	int err;
10489 
10490 	if (!rdev->ops->set_bitrate_mask)
10491 		return -EOPNOTSUPP;
10492 
10493 	err = nl80211_parse_tx_bitrate_mask(info, &mask);
10494 	if (err)
10495 		return err;
10496 
10497 	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10498 }
10499 
nl80211_register_mgmt(struct sk_buff * skb,struct genl_info * info)10500 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10501 {
10502 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10503 	struct wireless_dev *wdev = info->user_ptr[1];
10504 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10505 
10506 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10507 		return -EINVAL;
10508 
10509 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10510 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10511 
10512 	switch (wdev->iftype) {
10513 	case NL80211_IFTYPE_STATION:
10514 	case NL80211_IFTYPE_ADHOC:
10515 	case NL80211_IFTYPE_P2P_CLIENT:
10516 	case NL80211_IFTYPE_AP:
10517 	case NL80211_IFTYPE_AP_VLAN:
10518 	case NL80211_IFTYPE_MESH_POINT:
10519 	case NL80211_IFTYPE_P2P_GO:
10520 	case NL80211_IFTYPE_P2P_DEVICE:
10521 		break;
10522 	case NL80211_IFTYPE_NAN:
10523 	default:
10524 		return -EOPNOTSUPP;
10525 	}
10526 
10527 	/* not much point in registering if we can't reply */
10528 	if (!rdev->ops->mgmt_tx)
10529 		return -EOPNOTSUPP;
10530 
10531 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10532 			nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10533 			nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10534 }
10535 
nl80211_tx_mgmt(struct sk_buff * skb,struct genl_info * info)10536 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10537 {
10538 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10539 	struct wireless_dev *wdev = info->user_ptr[1];
10540 	struct cfg80211_chan_def chandef;
10541 	int err;
10542 	void *hdr = NULL;
10543 	u64 cookie;
10544 	struct sk_buff *msg = NULL;
10545 	struct cfg80211_mgmt_tx_params params = {
10546 		.dont_wait_for_ack =
10547 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10548 	};
10549 
10550 	if (!info->attrs[NL80211_ATTR_FRAME])
10551 		return -EINVAL;
10552 
10553 	if (!rdev->ops->mgmt_tx)
10554 		return -EOPNOTSUPP;
10555 
10556 	switch (wdev->iftype) {
10557 	case NL80211_IFTYPE_P2P_DEVICE:
10558 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10559 			return -EINVAL;
10560 	case NL80211_IFTYPE_STATION:
10561 	case NL80211_IFTYPE_ADHOC:
10562 	case NL80211_IFTYPE_P2P_CLIENT:
10563 	case NL80211_IFTYPE_AP:
10564 	case NL80211_IFTYPE_AP_VLAN:
10565 	case NL80211_IFTYPE_MESH_POINT:
10566 	case NL80211_IFTYPE_P2P_GO:
10567 		break;
10568 	case NL80211_IFTYPE_NAN:
10569 	default:
10570 		return -EOPNOTSUPP;
10571 	}
10572 
10573 	if (info->attrs[NL80211_ATTR_DURATION]) {
10574 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10575 			return -EINVAL;
10576 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10577 
10578 		/*
10579 		 * We should wait on the channel for at least a minimum amount
10580 		 * of time (10ms) but no longer than the driver supports.
10581 		 */
10582 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10583 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
10584 			return -EINVAL;
10585 	}
10586 
10587 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10588 
10589 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10590 		return -EINVAL;
10591 
10592 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10593 
10594 	/* get the channel if any has been specified, otherwise pass NULL to
10595 	 * the driver. The latter will use the current one
10596 	 */
10597 	chandef.chan = NULL;
10598 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10599 		err = nl80211_parse_chandef(rdev, info, &chandef);
10600 		if (err)
10601 			return err;
10602 	}
10603 
10604 	if (!chandef.chan && params.offchan)
10605 		return -EINVAL;
10606 
10607 	wdev_lock(wdev);
10608 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10609 		wdev_unlock(wdev);
10610 		return -EBUSY;
10611 	}
10612 	wdev_unlock(wdev);
10613 
10614 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10615 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10616 
10617 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10618 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10619 		int i;
10620 
10621 		if (len % sizeof(u16))
10622 			return -EINVAL;
10623 
10624 		params.n_csa_offsets = len / sizeof(u16);
10625 		params.csa_offsets =
10626 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10627 
10628 		/* check that all the offsets fit the frame */
10629 		for (i = 0; i < params.n_csa_offsets; i++) {
10630 			if (params.csa_offsets[i] >= params.len)
10631 				return -EINVAL;
10632 		}
10633 	}
10634 
10635 	if (!params.dont_wait_for_ack) {
10636 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10637 		if (!msg)
10638 			return -ENOMEM;
10639 
10640 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10641 				     NL80211_CMD_FRAME);
10642 		if (!hdr) {
10643 			err = -ENOBUFS;
10644 			goto free_msg;
10645 		}
10646 	}
10647 
10648 	params.chan = chandef.chan;
10649 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10650 	if (err)
10651 		goto free_msg;
10652 
10653 	if (msg) {
10654 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10655 				      NL80211_ATTR_PAD))
10656 			goto nla_put_failure;
10657 
10658 		genlmsg_end(msg, hdr);
10659 		return genlmsg_reply(msg, info);
10660 	}
10661 
10662 	return 0;
10663 
10664  nla_put_failure:
10665 	err = -ENOBUFS;
10666  free_msg:
10667 	nlmsg_free(msg);
10668 	return err;
10669 }
10670 
nl80211_tx_mgmt_cancel_wait(struct sk_buff * skb,struct genl_info * info)10671 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10672 {
10673 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10674 	struct wireless_dev *wdev = info->user_ptr[1];
10675 	u64 cookie;
10676 
10677 	if (!info->attrs[NL80211_ATTR_COOKIE])
10678 		return -EINVAL;
10679 
10680 	if (!rdev->ops->mgmt_tx_cancel_wait)
10681 		return -EOPNOTSUPP;
10682 
10683 	switch (wdev->iftype) {
10684 	case NL80211_IFTYPE_STATION:
10685 	case NL80211_IFTYPE_ADHOC:
10686 	case NL80211_IFTYPE_P2P_CLIENT:
10687 	case NL80211_IFTYPE_AP:
10688 	case NL80211_IFTYPE_AP_VLAN:
10689 	case NL80211_IFTYPE_P2P_GO:
10690 	case NL80211_IFTYPE_P2P_DEVICE:
10691 		break;
10692 	case NL80211_IFTYPE_NAN:
10693 	default:
10694 		return -EOPNOTSUPP;
10695 	}
10696 
10697 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10698 
10699 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10700 }
10701 
nl80211_set_power_save(struct sk_buff * skb,struct genl_info * info)10702 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10703 {
10704 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10705 	struct wireless_dev *wdev;
10706 	struct net_device *dev = info->user_ptr[1];
10707 	u8 ps_state;
10708 	bool state;
10709 	int err;
10710 
10711 	if (!info->attrs[NL80211_ATTR_PS_STATE])
10712 		return -EINVAL;
10713 
10714 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10715 
10716 	wdev = dev->ieee80211_ptr;
10717 
10718 	if (!rdev->ops->set_power_mgmt)
10719 		return -EOPNOTSUPP;
10720 
10721 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10722 
10723 	if (state == wdev->ps)
10724 		return 0;
10725 
10726 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10727 	if (!err)
10728 		wdev->ps = state;
10729 	return err;
10730 }
10731 
nl80211_get_power_save(struct sk_buff * skb,struct genl_info * info)10732 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10733 {
10734 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10735 	enum nl80211_ps_state ps_state;
10736 	struct wireless_dev *wdev;
10737 	struct net_device *dev = info->user_ptr[1];
10738 	struct sk_buff *msg;
10739 	void *hdr;
10740 	int err;
10741 
10742 	wdev = dev->ieee80211_ptr;
10743 
10744 	if (!rdev->ops->set_power_mgmt)
10745 		return -EOPNOTSUPP;
10746 
10747 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10748 	if (!msg)
10749 		return -ENOMEM;
10750 
10751 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10752 			     NL80211_CMD_GET_POWER_SAVE);
10753 	if (!hdr) {
10754 		err = -ENOBUFS;
10755 		goto free_msg;
10756 	}
10757 
10758 	if (wdev->ps)
10759 		ps_state = NL80211_PS_ENABLED;
10760 	else
10761 		ps_state = NL80211_PS_DISABLED;
10762 
10763 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10764 		goto nla_put_failure;
10765 
10766 	genlmsg_end(msg, hdr);
10767 	return genlmsg_reply(msg, info);
10768 
10769  nla_put_failure:
10770 	err = -ENOBUFS;
10771  free_msg:
10772 	nlmsg_free(msg);
10773 	return err;
10774 }
10775 
10776 static const struct nla_policy
10777 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10778 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10779 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10780 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10781 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10782 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10783 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10784 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10785 };
10786 
nl80211_set_cqm_txe(struct genl_info * info,u32 rate,u32 pkts,u32 intvl)10787 static int nl80211_set_cqm_txe(struct genl_info *info,
10788 			       u32 rate, u32 pkts, u32 intvl)
10789 {
10790 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10791 	struct net_device *dev = info->user_ptr[1];
10792 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10793 
10794 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10795 		return -EINVAL;
10796 
10797 	if (!rdev->ops->set_cqm_txe_config)
10798 		return -EOPNOTSUPP;
10799 
10800 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10801 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10802 		return -EOPNOTSUPP;
10803 
10804 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10805 }
10806 
cfg80211_cqm_rssi_update(struct cfg80211_registered_device * rdev,struct net_device * dev)10807 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10808 				    struct net_device *dev)
10809 {
10810 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10811 	s32 last, low, high;
10812 	u32 hyst;
10813 	int i, n, low_index;
10814 	int err;
10815 
10816 	/* RSSI reporting disabled? */
10817 	if (!wdev->cqm_config)
10818 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10819 
10820 	/*
10821 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
10822 	 * event has been received yet, we should receive an event after a
10823 	 * connection is established and enough beacons received to calculate
10824 	 * the average.
10825 	 */
10826 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10827 	    rdev->ops->get_station) {
10828 		struct station_info sinfo = {};
10829 		u8 *mac_addr;
10830 
10831 		mac_addr = wdev->current_bss->pub.bssid;
10832 
10833 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10834 		if (err)
10835 			return err;
10836 
10837 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10838 			wdev->cqm_config->last_rssi_event_value =
10839 				(s8) sinfo.rx_beacon_signal_avg;
10840 	}
10841 
10842 	last = wdev->cqm_config->last_rssi_event_value;
10843 	hyst = wdev->cqm_config->rssi_hyst;
10844 	n = wdev->cqm_config->n_rssi_thresholds;
10845 
10846 	for (i = 0; i < n; i++) {
10847 		i = array_index_nospec(i, n);
10848 		if (last < wdev->cqm_config->rssi_thresholds[i])
10849 			break;
10850 	}
10851 
10852 	low_index = i - 1;
10853 	if (low_index >= 0) {
10854 		low_index = array_index_nospec(low_index, n);
10855 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10856 	} else {
10857 		low = S32_MIN;
10858 	}
10859 	if (i < n) {
10860 		i = array_index_nospec(i, n);
10861 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10862 	} else {
10863 		high = S32_MAX;
10864 	}
10865 
10866 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10867 }
10868 
nl80211_set_cqm_rssi(struct genl_info * info,const s32 * thresholds,int n_thresholds,u32 hysteresis)10869 static int nl80211_set_cqm_rssi(struct genl_info *info,
10870 				const s32 *thresholds, int n_thresholds,
10871 				u32 hysteresis)
10872 {
10873 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10874 	struct net_device *dev = info->user_ptr[1];
10875 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10876 	int i, err;
10877 	s32 prev = S32_MIN;
10878 
10879 	/* Check all values negative and sorted */
10880 	for (i = 0; i < n_thresholds; i++) {
10881 		if (thresholds[i] > 0 || thresholds[i] <= prev)
10882 			return -EINVAL;
10883 
10884 		prev = thresholds[i];
10885 	}
10886 
10887 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
10888 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10889 		return -EOPNOTSUPP;
10890 
10891 	wdev_lock(wdev);
10892 	cfg80211_cqm_config_free(wdev);
10893 	wdev_unlock(wdev);
10894 
10895 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10896 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10897 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10898 
10899 		return rdev_set_cqm_rssi_config(rdev, dev,
10900 						thresholds[0], hysteresis);
10901 	}
10902 
10903 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
10904 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10905 		return -EOPNOTSUPP;
10906 
10907 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10908 		n_thresholds = 0;
10909 
10910 	wdev_lock(wdev);
10911 	if (n_thresholds) {
10912 		struct cfg80211_cqm_config *cqm_config;
10913 
10914 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10915 				     n_thresholds * sizeof(s32), GFP_KERNEL);
10916 		if (!cqm_config) {
10917 			err = -ENOMEM;
10918 			goto unlock;
10919 		}
10920 
10921 		cqm_config->rssi_hyst = hysteresis;
10922 		cqm_config->n_rssi_thresholds = n_thresholds;
10923 		memcpy(cqm_config->rssi_thresholds, thresholds,
10924 		       n_thresholds * sizeof(s32));
10925 
10926 		wdev->cqm_config = cqm_config;
10927 	}
10928 
10929 	err = cfg80211_cqm_rssi_update(rdev, dev);
10930 
10931 unlock:
10932 	wdev_unlock(wdev);
10933 
10934 	return err;
10935 }
10936 
nl80211_set_cqm(struct sk_buff * skb,struct genl_info * info)10937 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10938 {
10939 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10940 	struct nlattr *cqm;
10941 	int err;
10942 
10943 	cqm = info->attrs[NL80211_ATTR_CQM];
10944 	if (!cqm)
10945 		return -EINVAL;
10946 
10947 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
10948 					  nl80211_attr_cqm_policy,
10949 					  info->extack);
10950 	if (err)
10951 		return err;
10952 
10953 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10954 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10955 		const s32 *thresholds =
10956 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10957 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10958 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10959 
10960 		if (len % 4)
10961 			return -EINVAL;
10962 
10963 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10964 					    hysteresis);
10965 	}
10966 
10967 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10968 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10969 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10970 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10971 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10972 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10973 
10974 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10975 	}
10976 
10977 	return -EINVAL;
10978 }
10979 
nl80211_join_ocb(struct sk_buff * skb,struct genl_info * info)10980 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10981 {
10982 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10983 	struct net_device *dev = info->user_ptr[1];
10984 	struct ocb_setup setup = {};
10985 	int err;
10986 
10987 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10988 	if (err)
10989 		return err;
10990 
10991 	return cfg80211_join_ocb(rdev, dev, &setup);
10992 }
10993 
nl80211_leave_ocb(struct sk_buff * skb,struct genl_info * info)10994 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
10995 {
10996 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10997 	struct net_device *dev = info->user_ptr[1];
10998 
10999 	return cfg80211_leave_ocb(rdev, dev);
11000 }
11001 
nl80211_join_mesh(struct sk_buff * skb,struct genl_info * info)11002 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11003 {
11004 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11005 	struct net_device *dev = info->user_ptr[1];
11006 	struct mesh_config cfg;
11007 	struct mesh_setup setup;
11008 	int err;
11009 
11010 	/* start with default */
11011 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11012 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
11013 
11014 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11015 		/* and parse parameters if given */
11016 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
11017 		if (err)
11018 			return err;
11019 	}
11020 
11021 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11022 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11023 		return -EINVAL;
11024 
11025 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11026 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11027 
11028 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11029 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11030 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11031 			return -EINVAL;
11032 
11033 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11034 		setup.beacon_interval =
11035 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11036 
11037 		err = cfg80211_validate_beacon_int(rdev,
11038 						   NL80211_IFTYPE_MESH_POINT,
11039 						   setup.beacon_interval);
11040 		if (err)
11041 			return err;
11042 	}
11043 
11044 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11045 		setup.dtim_period =
11046 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11047 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
11048 			return -EINVAL;
11049 	}
11050 
11051 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11052 		/* parse additional setup parameters if given */
11053 		err = nl80211_parse_mesh_setup(info, &setup);
11054 		if (err)
11055 			return err;
11056 	}
11057 
11058 	if (setup.user_mpm)
11059 		cfg.auto_open_plinks = false;
11060 
11061 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11062 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11063 		if (err)
11064 			return err;
11065 	} else {
11066 		/* __cfg80211_join_mesh() will sort it out */
11067 		setup.chandef.chan = NULL;
11068 	}
11069 
11070 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11071 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11072 		int n_rates =
11073 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11074 		struct ieee80211_supported_band *sband;
11075 
11076 		if (!setup.chandef.chan)
11077 			return -EINVAL;
11078 
11079 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
11080 
11081 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11082 					     &setup.basic_rates);
11083 		if (err)
11084 			return err;
11085 	}
11086 
11087 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11088 		err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
11089 		if (err)
11090 			return err;
11091 
11092 		if (!setup.chandef.chan)
11093 			return -EINVAL;
11094 
11095 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11096 					      &setup.beacon_rate);
11097 		if (err)
11098 			return err;
11099 	}
11100 
11101 	setup.userspace_handles_dfs =
11102 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11103 
11104 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11105 		int r = validate_pae_over_nl80211(rdev, info);
11106 
11107 		if (r < 0)
11108 			return r;
11109 
11110 		setup.control_port_over_nl80211 = true;
11111 	}
11112 
11113 	wdev_lock(dev->ieee80211_ptr);
11114 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11115 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11116 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11117 	wdev_unlock(dev->ieee80211_ptr);
11118 
11119 	return err;
11120 }
11121 
nl80211_leave_mesh(struct sk_buff * skb,struct genl_info * info)11122 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11123 {
11124 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11125 	struct net_device *dev = info->user_ptr[1];
11126 
11127 	return cfg80211_leave_mesh(rdev, dev);
11128 }
11129 
11130 #ifdef CONFIG_PM
nl80211_send_wowlan_patterns(struct sk_buff * msg,struct cfg80211_registered_device * rdev)11131 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11132 					struct cfg80211_registered_device *rdev)
11133 {
11134 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11135 	struct nlattr *nl_pats, *nl_pat;
11136 	int i, pat_len;
11137 
11138 	if (!wowlan->n_patterns)
11139 		return 0;
11140 
11141 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11142 	if (!nl_pats)
11143 		return -ENOBUFS;
11144 
11145 	for (i = 0; i < wowlan->n_patterns; i++) {
11146 		nl_pat = nla_nest_start_noflag(msg, i + 1);
11147 		if (!nl_pat)
11148 			return -ENOBUFS;
11149 		pat_len = wowlan->patterns[i].pattern_len;
11150 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11151 			    wowlan->patterns[i].mask) ||
11152 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11153 			    wowlan->patterns[i].pattern) ||
11154 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11155 				wowlan->patterns[i].pkt_offset))
11156 			return -ENOBUFS;
11157 		nla_nest_end(msg, nl_pat);
11158 	}
11159 	nla_nest_end(msg, nl_pats);
11160 
11161 	return 0;
11162 }
11163 
nl80211_send_wowlan_tcp(struct sk_buff * msg,struct cfg80211_wowlan_tcp * tcp)11164 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11165 				   struct cfg80211_wowlan_tcp *tcp)
11166 {
11167 	struct nlattr *nl_tcp;
11168 
11169 	if (!tcp)
11170 		return 0;
11171 
11172 	nl_tcp = nla_nest_start_noflag(msg,
11173 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11174 	if (!nl_tcp)
11175 		return -ENOBUFS;
11176 
11177 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11178 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11179 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11180 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11181 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11182 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11183 		    tcp->payload_len, tcp->payload) ||
11184 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11185 			tcp->data_interval) ||
11186 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11187 		    tcp->wake_len, tcp->wake_data) ||
11188 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11189 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11190 		return -ENOBUFS;
11191 
11192 	if (tcp->payload_seq.len &&
11193 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11194 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
11195 		return -ENOBUFS;
11196 
11197 	if (tcp->payload_tok.len &&
11198 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11199 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
11200 		    &tcp->payload_tok))
11201 		return -ENOBUFS;
11202 
11203 	nla_nest_end(msg, nl_tcp);
11204 
11205 	return 0;
11206 }
11207 
nl80211_send_wowlan_nd(struct sk_buff * msg,struct cfg80211_sched_scan_request * req)11208 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11209 				  struct cfg80211_sched_scan_request *req)
11210 {
11211 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11212 	int i;
11213 
11214 	if (!req)
11215 		return 0;
11216 
11217 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11218 	if (!nd)
11219 		return -ENOBUFS;
11220 
11221 	if (req->n_scan_plans == 1 &&
11222 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11223 			req->scan_plans[0].interval * 1000))
11224 		return -ENOBUFS;
11225 
11226 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11227 		return -ENOBUFS;
11228 
11229 	if (req->relative_rssi_set) {
11230 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
11231 
11232 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11233 			       req->relative_rssi))
11234 			return -ENOBUFS;
11235 
11236 		rssi_adjust.band = req->rssi_adjust.band;
11237 		rssi_adjust.delta = req->rssi_adjust.delta;
11238 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11239 			    sizeof(rssi_adjust), &rssi_adjust))
11240 			return -ENOBUFS;
11241 	}
11242 
11243 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11244 	if (!freqs)
11245 		return -ENOBUFS;
11246 
11247 	for (i = 0; i < req->n_channels; i++) {
11248 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11249 			return -ENOBUFS;
11250 	}
11251 
11252 	nla_nest_end(msg, freqs);
11253 
11254 	if (req->n_match_sets) {
11255 		matches = nla_nest_start_noflag(msg,
11256 						NL80211_ATTR_SCHED_SCAN_MATCH);
11257 		if (!matches)
11258 			return -ENOBUFS;
11259 
11260 		for (i = 0; i < req->n_match_sets; i++) {
11261 			match = nla_nest_start_noflag(msg, i);
11262 			if (!match)
11263 				return -ENOBUFS;
11264 
11265 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11266 				    req->match_sets[i].ssid.ssid_len,
11267 				    req->match_sets[i].ssid.ssid))
11268 				return -ENOBUFS;
11269 			nla_nest_end(msg, match);
11270 		}
11271 		nla_nest_end(msg, matches);
11272 	}
11273 
11274 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11275 	if (!scan_plans)
11276 		return -ENOBUFS;
11277 
11278 	for (i = 0; i < req->n_scan_plans; i++) {
11279 		scan_plan = nla_nest_start_noflag(msg, i + 1);
11280 		if (!scan_plan)
11281 			return -ENOBUFS;
11282 
11283 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11284 				req->scan_plans[i].interval) ||
11285 		    (req->scan_plans[i].iterations &&
11286 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11287 				 req->scan_plans[i].iterations)))
11288 			return -ENOBUFS;
11289 		nla_nest_end(msg, scan_plan);
11290 	}
11291 	nla_nest_end(msg, scan_plans);
11292 
11293 	nla_nest_end(msg, nd);
11294 
11295 	return 0;
11296 }
11297 
nl80211_get_wowlan(struct sk_buff * skb,struct genl_info * info)11298 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11299 {
11300 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11301 	struct sk_buff *msg;
11302 	void *hdr;
11303 	u32 size = NLMSG_DEFAULT_SIZE;
11304 
11305 	if (!rdev->wiphy.wowlan)
11306 		return -EOPNOTSUPP;
11307 
11308 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11309 		/* adjust size to have room for all the data */
11310 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11311 			rdev->wiphy.wowlan_config->tcp->payload_len +
11312 			rdev->wiphy.wowlan_config->tcp->wake_len +
11313 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11314 	}
11315 
11316 	msg = nlmsg_new(size, GFP_KERNEL);
11317 	if (!msg)
11318 		return -ENOMEM;
11319 
11320 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11321 			     NL80211_CMD_GET_WOWLAN);
11322 	if (!hdr)
11323 		goto nla_put_failure;
11324 
11325 	if (rdev->wiphy.wowlan_config) {
11326 		struct nlattr *nl_wowlan;
11327 
11328 		nl_wowlan = nla_nest_start_noflag(msg,
11329 						  NL80211_ATTR_WOWLAN_TRIGGERS);
11330 		if (!nl_wowlan)
11331 			goto nla_put_failure;
11332 
11333 		if ((rdev->wiphy.wowlan_config->any &&
11334 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11335 		    (rdev->wiphy.wowlan_config->disconnect &&
11336 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11337 		    (rdev->wiphy.wowlan_config->magic_pkt &&
11338 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11339 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11340 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11341 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
11342 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11343 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
11344 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11345 		    (rdev->wiphy.wowlan_config->rfkill_release &&
11346 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11347 			goto nla_put_failure;
11348 
11349 		if (nl80211_send_wowlan_patterns(msg, rdev))
11350 			goto nla_put_failure;
11351 
11352 		if (nl80211_send_wowlan_tcp(msg,
11353 					    rdev->wiphy.wowlan_config->tcp))
11354 			goto nla_put_failure;
11355 
11356 		if (nl80211_send_wowlan_nd(
11357 			    msg,
11358 			    rdev->wiphy.wowlan_config->nd_config))
11359 			goto nla_put_failure;
11360 
11361 		nla_nest_end(msg, nl_wowlan);
11362 	}
11363 
11364 	genlmsg_end(msg, hdr);
11365 	return genlmsg_reply(msg, info);
11366 
11367 nla_put_failure:
11368 	nlmsg_free(msg);
11369 	return -ENOBUFS;
11370 }
11371 
nl80211_parse_wowlan_tcp(struct cfg80211_registered_device * rdev,struct nlattr * attr,struct cfg80211_wowlan * trig)11372 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11373 				    struct nlattr *attr,
11374 				    struct cfg80211_wowlan *trig)
11375 {
11376 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11377 	struct cfg80211_wowlan_tcp *cfg;
11378 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
11379 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11380 	u32 size;
11381 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11382 	int err, port;
11383 
11384 	if (!rdev->wiphy.wowlan->tcp)
11385 		return -EINVAL;
11386 
11387 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11388 					  nl80211_wowlan_tcp_policy, NULL);
11389 	if (err)
11390 		return err;
11391 
11392 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11393 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11394 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11395 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11396 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11397 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11398 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11399 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11400 		return -EINVAL;
11401 
11402 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11403 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11404 		return -EINVAL;
11405 
11406 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11407 			rdev->wiphy.wowlan->tcp->data_interval_max ||
11408 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11409 		return -EINVAL;
11410 
11411 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11412 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11413 		return -EINVAL;
11414 
11415 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11416 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11417 		return -EINVAL;
11418 
11419 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11420 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11421 
11422 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11423 		tokens_size = tokln - sizeof(*tok);
11424 
11425 		if (!tok->len || tokens_size % tok->len)
11426 			return -EINVAL;
11427 		if (!rdev->wiphy.wowlan->tcp->tok)
11428 			return -EINVAL;
11429 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11430 			return -EINVAL;
11431 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11432 			return -EINVAL;
11433 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11434 			return -EINVAL;
11435 		if (tok->offset + tok->len > data_size)
11436 			return -EINVAL;
11437 	}
11438 
11439 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11440 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11441 		if (!rdev->wiphy.wowlan->tcp->seq)
11442 			return -EINVAL;
11443 		if (seq->len == 0 || seq->len > 4)
11444 			return -EINVAL;
11445 		if (seq->len + seq->offset > data_size)
11446 			return -EINVAL;
11447 	}
11448 
11449 	size = sizeof(*cfg);
11450 	size += data_size;
11451 	size += wake_size + wake_mask_size;
11452 	size += tokens_size;
11453 
11454 	cfg = kzalloc(size, GFP_KERNEL);
11455 	if (!cfg)
11456 		return -ENOMEM;
11457 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11458 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11459 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11460 	       ETH_ALEN);
11461 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11462 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11463 	else
11464 		port = 0;
11465 #ifdef CONFIG_INET
11466 	/* allocate a socket and port for it and use it */
11467 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11468 			    IPPROTO_TCP, &cfg->sock, 1);
11469 	if (err) {
11470 		kfree(cfg);
11471 		return err;
11472 	}
11473 	if (inet_csk_get_port(cfg->sock->sk, port)) {
11474 		sock_release(cfg->sock);
11475 		kfree(cfg);
11476 		return -EADDRINUSE;
11477 	}
11478 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11479 #else
11480 	if (!port) {
11481 		kfree(cfg);
11482 		return -EINVAL;
11483 	}
11484 	cfg->src_port = port;
11485 #endif
11486 
11487 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11488 	cfg->payload_len = data_size;
11489 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11490 	memcpy((void *)cfg->payload,
11491 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11492 	       data_size);
11493 	if (seq)
11494 		cfg->payload_seq = *seq;
11495 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11496 	cfg->wake_len = wake_size;
11497 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11498 	memcpy((void *)cfg->wake_data,
11499 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11500 	       wake_size);
11501 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11502 			 data_size + wake_size;
11503 	memcpy((void *)cfg->wake_mask,
11504 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11505 	       wake_mask_size);
11506 	if (tok) {
11507 		cfg->tokens_size = tokens_size;
11508 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11509 	}
11510 
11511 	trig->tcp = cfg;
11512 
11513 	return 0;
11514 }
11515 
nl80211_parse_wowlan_nd(struct cfg80211_registered_device * rdev,const struct wiphy_wowlan_support * wowlan,struct nlattr * attr,struct cfg80211_wowlan * trig)11516 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11517 				   const struct wiphy_wowlan_support *wowlan,
11518 				   struct nlattr *attr,
11519 				   struct cfg80211_wowlan *trig)
11520 {
11521 	struct nlattr **tb;
11522 	int err;
11523 
11524 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11525 	if (!tb)
11526 		return -ENOMEM;
11527 
11528 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11529 		err = -EOPNOTSUPP;
11530 		goto out;
11531 	}
11532 
11533 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11534 					  nl80211_policy, NULL);
11535 	if (err)
11536 		goto out;
11537 
11538 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11539 						   wowlan->max_nd_match_sets);
11540 	err = PTR_ERR_OR_ZERO(trig->nd_config);
11541 	if (err)
11542 		trig->nd_config = NULL;
11543 
11544 out:
11545 	kfree(tb);
11546 	return err;
11547 }
11548 
nl80211_set_wowlan(struct sk_buff * skb,struct genl_info * info)11549 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11550 {
11551 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11552 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11553 	struct cfg80211_wowlan new_triggers = {};
11554 	struct cfg80211_wowlan *ntrig;
11555 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11556 	int err, i;
11557 	bool prev_enabled = rdev->wiphy.wowlan_config;
11558 	bool regular = false;
11559 
11560 	if (!wowlan)
11561 		return -EOPNOTSUPP;
11562 
11563 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11564 		cfg80211_rdev_free_wowlan(rdev);
11565 		rdev->wiphy.wowlan_config = NULL;
11566 		goto set_wakeup;
11567 	}
11568 
11569 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11570 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11571 					  nl80211_wowlan_policy, info->extack);
11572 	if (err)
11573 		return err;
11574 
11575 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11576 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11577 			return -EINVAL;
11578 		new_triggers.any = true;
11579 	}
11580 
11581 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11582 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11583 			return -EINVAL;
11584 		new_triggers.disconnect = true;
11585 		regular = true;
11586 	}
11587 
11588 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11589 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11590 			return -EINVAL;
11591 		new_triggers.magic_pkt = true;
11592 		regular = true;
11593 	}
11594 
11595 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11596 		return -EINVAL;
11597 
11598 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11599 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11600 			return -EINVAL;
11601 		new_triggers.gtk_rekey_failure = true;
11602 		regular = true;
11603 	}
11604 
11605 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11606 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11607 			return -EINVAL;
11608 		new_triggers.eap_identity_req = true;
11609 		regular = true;
11610 	}
11611 
11612 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11613 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11614 			return -EINVAL;
11615 		new_triggers.four_way_handshake = true;
11616 		regular = true;
11617 	}
11618 
11619 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11620 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11621 			return -EINVAL;
11622 		new_triggers.rfkill_release = true;
11623 		regular = true;
11624 	}
11625 
11626 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11627 		struct nlattr *pat;
11628 		int n_patterns = 0;
11629 		int rem, pat_len, mask_len, pkt_offset;
11630 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11631 
11632 		regular = true;
11633 
11634 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11635 				    rem)
11636 			n_patterns++;
11637 		if (n_patterns > wowlan->n_patterns)
11638 			return -EINVAL;
11639 
11640 		new_triggers.patterns = kcalloc(n_patterns,
11641 						sizeof(new_triggers.patterns[0]),
11642 						GFP_KERNEL);
11643 		if (!new_triggers.patterns)
11644 			return -ENOMEM;
11645 
11646 		new_triggers.n_patterns = n_patterns;
11647 		i = 0;
11648 
11649 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11650 				    rem) {
11651 			u8 *mask_pat;
11652 
11653 			err = nla_parse_nested_deprecated(pat_tb,
11654 							  MAX_NL80211_PKTPAT,
11655 							  pat,
11656 							  nl80211_packet_pattern_policy,
11657 							  info->extack);
11658 			if (err)
11659 				goto error;
11660 
11661 			err = -EINVAL;
11662 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
11663 			    !pat_tb[NL80211_PKTPAT_PATTERN])
11664 				goto error;
11665 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11666 			mask_len = DIV_ROUND_UP(pat_len, 8);
11667 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11668 				goto error;
11669 			if (pat_len > wowlan->pattern_max_len ||
11670 			    pat_len < wowlan->pattern_min_len)
11671 				goto error;
11672 
11673 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
11674 				pkt_offset = 0;
11675 			else
11676 				pkt_offset = nla_get_u32(
11677 					pat_tb[NL80211_PKTPAT_OFFSET]);
11678 			if (pkt_offset > wowlan->max_pkt_offset)
11679 				goto error;
11680 			new_triggers.patterns[i].pkt_offset = pkt_offset;
11681 
11682 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11683 			if (!mask_pat) {
11684 				err = -ENOMEM;
11685 				goto error;
11686 			}
11687 			new_triggers.patterns[i].mask = mask_pat;
11688 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11689 			       mask_len);
11690 			mask_pat += mask_len;
11691 			new_triggers.patterns[i].pattern = mask_pat;
11692 			new_triggers.patterns[i].pattern_len = pat_len;
11693 			memcpy(mask_pat,
11694 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11695 			       pat_len);
11696 			i++;
11697 		}
11698 	}
11699 
11700 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11701 		regular = true;
11702 		err = nl80211_parse_wowlan_tcp(
11703 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11704 			&new_triggers);
11705 		if (err)
11706 			goto error;
11707 	}
11708 
11709 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11710 		regular = true;
11711 		err = nl80211_parse_wowlan_nd(
11712 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11713 			&new_triggers);
11714 		if (err)
11715 			goto error;
11716 	}
11717 
11718 	/* The 'any' trigger means the device continues operating more or less
11719 	 * as in its normal operation mode and wakes up the host on most of the
11720 	 * normal interrupts (like packet RX, ...)
11721 	 * It therefore makes little sense to combine with the more constrained
11722 	 * wakeup trigger modes.
11723 	 */
11724 	if (new_triggers.any && regular) {
11725 		err = -EINVAL;
11726 		goto error;
11727 	}
11728 
11729 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11730 	if (!ntrig) {
11731 		err = -ENOMEM;
11732 		goto error;
11733 	}
11734 	cfg80211_rdev_free_wowlan(rdev);
11735 	rdev->wiphy.wowlan_config = ntrig;
11736 
11737  set_wakeup:
11738 	if (rdev->ops->set_wakeup &&
11739 	    prev_enabled != !!rdev->wiphy.wowlan_config)
11740 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11741 
11742 	return 0;
11743  error:
11744 	for (i = 0; i < new_triggers.n_patterns; i++)
11745 		kfree(new_triggers.patterns[i].mask);
11746 	kfree(new_triggers.patterns);
11747 	if (new_triggers.tcp && new_triggers.tcp->sock)
11748 		sock_release(new_triggers.tcp->sock);
11749 	kfree(new_triggers.tcp);
11750 	kfree(new_triggers.nd_config);
11751 	return err;
11752 }
11753 #endif
11754 
nl80211_send_coalesce_rules(struct sk_buff * msg,struct cfg80211_registered_device * rdev)11755 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11756 				       struct cfg80211_registered_device *rdev)
11757 {
11758 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11759 	int i, j, pat_len;
11760 	struct cfg80211_coalesce_rules *rule;
11761 
11762 	if (!rdev->coalesce->n_rules)
11763 		return 0;
11764 
11765 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
11766 	if (!nl_rules)
11767 		return -ENOBUFS;
11768 
11769 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
11770 		nl_rule = nla_nest_start_noflag(msg, i + 1);
11771 		if (!nl_rule)
11772 			return -ENOBUFS;
11773 
11774 		rule = &rdev->coalesce->rules[i];
11775 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11776 				rule->delay))
11777 			return -ENOBUFS;
11778 
11779 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11780 				rule->condition))
11781 			return -ENOBUFS;
11782 
11783 		nl_pats = nla_nest_start_noflag(msg,
11784 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11785 		if (!nl_pats)
11786 			return -ENOBUFS;
11787 
11788 		for (j = 0; j < rule->n_patterns; j++) {
11789 			nl_pat = nla_nest_start_noflag(msg, j + 1);
11790 			if (!nl_pat)
11791 				return -ENOBUFS;
11792 			pat_len = rule->patterns[j].pattern_len;
11793 			if (nla_put(msg, NL80211_PKTPAT_MASK,
11794 				    DIV_ROUND_UP(pat_len, 8),
11795 				    rule->patterns[j].mask) ||
11796 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11797 				    rule->patterns[j].pattern) ||
11798 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11799 					rule->patterns[j].pkt_offset))
11800 				return -ENOBUFS;
11801 			nla_nest_end(msg, nl_pat);
11802 		}
11803 		nla_nest_end(msg, nl_pats);
11804 		nla_nest_end(msg, nl_rule);
11805 	}
11806 	nla_nest_end(msg, nl_rules);
11807 
11808 	return 0;
11809 }
11810 
nl80211_get_coalesce(struct sk_buff * skb,struct genl_info * info)11811 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11812 {
11813 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11814 	struct sk_buff *msg;
11815 	void *hdr;
11816 
11817 	if (!rdev->wiphy.coalesce)
11818 		return -EOPNOTSUPP;
11819 
11820 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11821 	if (!msg)
11822 		return -ENOMEM;
11823 
11824 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11825 			     NL80211_CMD_GET_COALESCE);
11826 	if (!hdr)
11827 		goto nla_put_failure;
11828 
11829 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11830 		goto nla_put_failure;
11831 
11832 	genlmsg_end(msg, hdr);
11833 	return genlmsg_reply(msg, info);
11834 
11835 nla_put_failure:
11836 	nlmsg_free(msg);
11837 	return -ENOBUFS;
11838 }
11839 
cfg80211_rdev_free_coalesce(struct cfg80211_registered_device * rdev)11840 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11841 {
11842 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
11843 	int i, j;
11844 	struct cfg80211_coalesce_rules *rule;
11845 
11846 	if (!coalesce)
11847 		return;
11848 
11849 	for (i = 0; i < coalesce->n_rules; i++) {
11850 		rule = &coalesce->rules[i];
11851 		for (j = 0; j < rule->n_patterns; j++)
11852 			kfree(rule->patterns[j].mask);
11853 		kfree(rule->patterns);
11854 	}
11855 	kfree(coalesce->rules);
11856 	kfree(coalesce);
11857 	rdev->coalesce = NULL;
11858 }
11859 
nl80211_parse_coalesce_rule(struct cfg80211_registered_device * rdev,struct nlattr * rule,struct cfg80211_coalesce_rules * new_rule)11860 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11861 				       struct nlattr *rule,
11862 				       struct cfg80211_coalesce_rules *new_rule)
11863 {
11864 	int err, i;
11865 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11866 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11867 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11868 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11869 
11870 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
11871 					  rule, nl80211_coalesce_policy, NULL);
11872 	if (err)
11873 		return err;
11874 
11875 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11876 		new_rule->delay =
11877 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11878 	if (new_rule->delay > coalesce->max_delay)
11879 		return -EINVAL;
11880 
11881 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11882 		new_rule->condition =
11883 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11884 
11885 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11886 		return -EINVAL;
11887 
11888 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11889 			    rem)
11890 		n_patterns++;
11891 	if (n_patterns > coalesce->n_patterns)
11892 		return -EINVAL;
11893 
11894 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11895 				     GFP_KERNEL);
11896 	if (!new_rule->patterns)
11897 		return -ENOMEM;
11898 
11899 	new_rule->n_patterns = n_patterns;
11900 	i = 0;
11901 
11902 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11903 			    rem) {
11904 		u8 *mask_pat;
11905 
11906 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
11907 						  pat,
11908 						  nl80211_packet_pattern_policy,
11909 						  NULL);
11910 		if (err)
11911 			return err;
11912 
11913 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
11914 		    !pat_tb[NL80211_PKTPAT_PATTERN])
11915 			return -EINVAL;
11916 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11917 		mask_len = DIV_ROUND_UP(pat_len, 8);
11918 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11919 			return -EINVAL;
11920 		if (pat_len > coalesce->pattern_max_len ||
11921 		    pat_len < coalesce->pattern_min_len)
11922 			return -EINVAL;
11923 
11924 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
11925 			pkt_offset = 0;
11926 		else
11927 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11928 		if (pkt_offset > coalesce->max_pkt_offset)
11929 			return -EINVAL;
11930 		new_rule->patterns[i].pkt_offset = pkt_offset;
11931 
11932 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11933 		if (!mask_pat)
11934 			return -ENOMEM;
11935 
11936 		new_rule->patterns[i].mask = mask_pat;
11937 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11938 		       mask_len);
11939 
11940 		mask_pat += mask_len;
11941 		new_rule->patterns[i].pattern = mask_pat;
11942 		new_rule->patterns[i].pattern_len = pat_len;
11943 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11944 		       pat_len);
11945 		i++;
11946 	}
11947 
11948 	return 0;
11949 }
11950 
nl80211_set_coalesce(struct sk_buff * skb,struct genl_info * info)11951 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11952 {
11953 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11954 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11955 	struct cfg80211_coalesce new_coalesce = {};
11956 	struct cfg80211_coalesce *n_coalesce;
11957 	int err, rem_rule, n_rules = 0, i, j;
11958 	struct nlattr *rule;
11959 	struct cfg80211_coalesce_rules *tmp_rule;
11960 
11961 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11962 		return -EOPNOTSUPP;
11963 
11964 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11965 		cfg80211_rdev_free_coalesce(rdev);
11966 		rdev_set_coalesce(rdev, NULL);
11967 		return 0;
11968 	}
11969 
11970 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11971 			    rem_rule)
11972 		n_rules++;
11973 	if (n_rules > coalesce->n_rules)
11974 		return -EINVAL;
11975 
11976 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11977 				     GFP_KERNEL);
11978 	if (!new_coalesce.rules)
11979 		return -ENOMEM;
11980 
11981 	new_coalesce.n_rules = n_rules;
11982 	i = 0;
11983 
11984 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11985 			    rem_rule) {
11986 		err = nl80211_parse_coalesce_rule(rdev, rule,
11987 						  &new_coalesce.rules[i]);
11988 		if (err)
11989 			goto error;
11990 
11991 		i++;
11992 	}
11993 
11994 	err = rdev_set_coalesce(rdev, &new_coalesce);
11995 	if (err)
11996 		goto error;
11997 
11998 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
11999 	if (!n_coalesce) {
12000 		err = -ENOMEM;
12001 		goto error;
12002 	}
12003 	cfg80211_rdev_free_coalesce(rdev);
12004 	rdev->coalesce = n_coalesce;
12005 
12006 	return 0;
12007 error:
12008 	for (i = 0; i < new_coalesce.n_rules; i++) {
12009 		tmp_rule = &new_coalesce.rules[i];
12010 		for (j = 0; j < tmp_rule->n_patterns; j++)
12011 			kfree(tmp_rule->patterns[j].mask);
12012 		kfree(tmp_rule->patterns);
12013 	}
12014 	kfree(new_coalesce.rules);
12015 
12016 	return err;
12017 }
12018 
nl80211_set_rekey_data(struct sk_buff * skb,struct genl_info * info)12019 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12020 {
12021 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12022 	struct net_device *dev = info->user_ptr[1];
12023 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12024 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12025 	struct cfg80211_gtk_rekey_data rekey_data;
12026 	int err;
12027 
12028 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12029 		return -EINVAL;
12030 
12031 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12032 					  info->attrs[NL80211_ATTR_REKEY_DATA],
12033 					  nl80211_rekey_policy, info->extack);
12034 	if (err)
12035 		return err;
12036 
12037 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12038 	    !tb[NL80211_REKEY_DATA_KCK])
12039 		return -EINVAL;
12040 	if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
12041 		return -ERANGE;
12042 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
12043 		return -ERANGE;
12044 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
12045 		return -ERANGE;
12046 
12047 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12048 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12049 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12050 
12051 	wdev_lock(wdev);
12052 	if (!wdev->current_bss) {
12053 		err = -ENOTCONN;
12054 		goto out;
12055 	}
12056 
12057 	if (!rdev->ops->set_rekey_data) {
12058 		err = -EOPNOTSUPP;
12059 		goto out;
12060 	}
12061 
12062 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12063  out:
12064 	wdev_unlock(wdev);
12065 	return err;
12066 }
12067 
nl80211_register_unexpected_frame(struct sk_buff * skb,struct genl_info * info)12068 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12069 					     struct genl_info *info)
12070 {
12071 	struct net_device *dev = info->user_ptr[1];
12072 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12073 
12074 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12075 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12076 		return -EINVAL;
12077 
12078 	if (wdev->ap_unexpected_nlportid)
12079 		return -EBUSY;
12080 
12081 	wdev->ap_unexpected_nlportid = info->snd_portid;
12082 	return 0;
12083 }
12084 
nl80211_probe_client(struct sk_buff * skb,struct genl_info * info)12085 static int nl80211_probe_client(struct sk_buff *skb,
12086 				struct genl_info *info)
12087 {
12088 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12089 	struct net_device *dev = info->user_ptr[1];
12090 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12091 	struct sk_buff *msg;
12092 	void *hdr;
12093 	const u8 *addr;
12094 	u64 cookie;
12095 	int err;
12096 
12097 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12098 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12099 		return -EOPNOTSUPP;
12100 
12101 	if (!info->attrs[NL80211_ATTR_MAC])
12102 		return -EINVAL;
12103 
12104 	if (!rdev->ops->probe_client)
12105 		return -EOPNOTSUPP;
12106 
12107 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12108 	if (!msg)
12109 		return -ENOMEM;
12110 
12111 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12112 			     NL80211_CMD_PROBE_CLIENT);
12113 	if (!hdr) {
12114 		err = -ENOBUFS;
12115 		goto free_msg;
12116 	}
12117 
12118 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12119 
12120 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12121 	if (err)
12122 		goto free_msg;
12123 
12124 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12125 			      NL80211_ATTR_PAD))
12126 		goto nla_put_failure;
12127 
12128 	genlmsg_end(msg, hdr);
12129 
12130 	return genlmsg_reply(msg, info);
12131 
12132  nla_put_failure:
12133 	err = -ENOBUFS;
12134  free_msg:
12135 	nlmsg_free(msg);
12136 	return err;
12137 }
12138 
nl80211_register_beacons(struct sk_buff * skb,struct genl_info * info)12139 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12140 {
12141 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12142 	struct cfg80211_beacon_registration *reg, *nreg;
12143 	int rv;
12144 
12145 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12146 		return -EOPNOTSUPP;
12147 
12148 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12149 	if (!nreg)
12150 		return -ENOMEM;
12151 
12152 	/* First, check if already registered. */
12153 	spin_lock_bh(&rdev->beacon_registrations_lock);
12154 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12155 		if (reg->nlportid == info->snd_portid) {
12156 			rv = -EALREADY;
12157 			goto out_err;
12158 		}
12159 	}
12160 	/* Add it to the list */
12161 	nreg->nlportid = info->snd_portid;
12162 	list_add(&nreg->list, &rdev->beacon_registrations);
12163 
12164 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12165 
12166 	return 0;
12167 out_err:
12168 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12169 	kfree(nreg);
12170 	return rv;
12171 }
12172 
nl80211_start_p2p_device(struct sk_buff * skb,struct genl_info * info)12173 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12174 {
12175 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12176 	struct wireless_dev *wdev = info->user_ptr[1];
12177 	int err;
12178 
12179 	if (!rdev->ops->start_p2p_device)
12180 		return -EOPNOTSUPP;
12181 
12182 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12183 		return -EOPNOTSUPP;
12184 
12185 	if (wdev_running(wdev))
12186 		return 0;
12187 
12188 	if (rfkill_blocked(rdev->rfkill))
12189 		return -ERFKILL;
12190 
12191 	err = rdev_start_p2p_device(rdev, wdev);
12192 	if (err)
12193 		return err;
12194 
12195 	wdev->is_running = true;
12196 	rdev->opencount++;
12197 
12198 	return 0;
12199 }
12200 
nl80211_stop_p2p_device(struct sk_buff * skb,struct genl_info * info)12201 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12202 {
12203 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12204 	struct wireless_dev *wdev = info->user_ptr[1];
12205 
12206 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12207 		return -EOPNOTSUPP;
12208 
12209 	if (!rdev->ops->stop_p2p_device)
12210 		return -EOPNOTSUPP;
12211 
12212 	cfg80211_stop_p2p_device(rdev, wdev);
12213 
12214 	return 0;
12215 }
12216 
nl80211_start_nan(struct sk_buff * skb,struct genl_info * info)12217 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12218 {
12219 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12220 	struct wireless_dev *wdev = info->user_ptr[1];
12221 	struct cfg80211_nan_conf conf = {};
12222 	int err;
12223 
12224 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12225 		return -EOPNOTSUPP;
12226 
12227 	if (wdev_running(wdev))
12228 		return -EEXIST;
12229 
12230 	if (rfkill_blocked(rdev->rfkill))
12231 		return -ERFKILL;
12232 
12233 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12234 		return -EINVAL;
12235 
12236 	conf.master_pref =
12237 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12238 
12239 	if (info->attrs[NL80211_ATTR_BANDS]) {
12240 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12241 
12242 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12243 			return -EOPNOTSUPP;
12244 
12245 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12246 			return -EINVAL;
12247 
12248 		conf.bands = bands;
12249 	}
12250 
12251 	err = rdev_start_nan(rdev, wdev, &conf);
12252 	if (err)
12253 		return err;
12254 
12255 	wdev->is_running = true;
12256 	rdev->opencount++;
12257 
12258 	return 0;
12259 }
12260 
nl80211_stop_nan(struct sk_buff * skb,struct genl_info * info)12261 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12262 {
12263 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12264 	struct wireless_dev *wdev = info->user_ptr[1];
12265 
12266 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12267 		return -EOPNOTSUPP;
12268 
12269 	cfg80211_stop_nan(rdev, wdev);
12270 
12271 	return 0;
12272 }
12273 
validate_nan_filter(struct nlattr * filter_attr)12274 static int validate_nan_filter(struct nlattr *filter_attr)
12275 {
12276 	struct nlattr *attr;
12277 	int len = 0, n_entries = 0, rem;
12278 
12279 	nla_for_each_nested(attr, filter_attr, rem) {
12280 		len += nla_len(attr);
12281 		n_entries++;
12282 	}
12283 
12284 	if (len >= U8_MAX)
12285 		return -EINVAL;
12286 
12287 	return n_entries;
12288 }
12289 
handle_nan_filter(struct nlattr * attr_filter,struct cfg80211_nan_func * func,bool tx)12290 static int handle_nan_filter(struct nlattr *attr_filter,
12291 			     struct cfg80211_nan_func *func,
12292 			     bool tx)
12293 {
12294 	struct nlattr *attr;
12295 	int n_entries, rem, i;
12296 	struct cfg80211_nan_func_filter *filter;
12297 
12298 	n_entries = validate_nan_filter(attr_filter);
12299 	if (n_entries < 0)
12300 		return n_entries;
12301 
12302 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12303 
12304 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12305 	if (!filter)
12306 		return -ENOMEM;
12307 
12308 	i = 0;
12309 	nla_for_each_nested(attr, attr_filter, rem) {
12310 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12311 		filter[i].len = nla_len(attr);
12312 		i++;
12313 	}
12314 	if (tx) {
12315 		func->num_tx_filters = n_entries;
12316 		func->tx_filters = filter;
12317 	} else {
12318 		func->num_rx_filters = n_entries;
12319 		func->rx_filters = filter;
12320 	}
12321 
12322 	return 0;
12323 }
12324 
nl80211_nan_add_func(struct sk_buff * skb,struct genl_info * info)12325 static int nl80211_nan_add_func(struct sk_buff *skb,
12326 				struct genl_info *info)
12327 {
12328 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12329 	struct wireless_dev *wdev = info->user_ptr[1];
12330 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12331 	struct cfg80211_nan_func *func;
12332 	struct sk_buff *msg = NULL;
12333 	void *hdr = NULL;
12334 	int err = 0;
12335 
12336 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12337 		return -EOPNOTSUPP;
12338 
12339 	if (!wdev_running(wdev))
12340 		return -ENOTCONN;
12341 
12342 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12343 		return -EINVAL;
12344 
12345 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12346 					  info->attrs[NL80211_ATTR_NAN_FUNC],
12347 					  nl80211_nan_func_policy,
12348 					  info->extack);
12349 	if (err)
12350 		return err;
12351 
12352 	func = kzalloc(sizeof(*func), GFP_KERNEL);
12353 	if (!func)
12354 		return -ENOMEM;
12355 
12356 	func->cookie = cfg80211_assign_cookie(rdev);
12357 
12358 	if (!tb[NL80211_NAN_FUNC_TYPE] ||
12359 	    nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12360 		err = -EINVAL;
12361 		goto out;
12362 	}
12363 
12364 
12365 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12366 
12367 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12368 		err = -EINVAL;
12369 		goto out;
12370 	}
12371 
12372 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12373 	       sizeof(func->service_id));
12374 
12375 	func->close_range =
12376 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12377 
12378 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12379 		func->serv_spec_info_len =
12380 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12381 		func->serv_spec_info =
12382 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12383 				func->serv_spec_info_len,
12384 				GFP_KERNEL);
12385 		if (!func->serv_spec_info) {
12386 			err = -ENOMEM;
12387 			goto out;
12388 		}
12389 	}
12390 
12391 	if (tb[NL80211_NAN_FUNC_TTL])
12392 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12393 
12394 	switch (func->type) {
12395 	case NL80211_NAN_FUNC_PUBLISH:
12396 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12397 			err = -EINVAL;
12398 			goto out;
12399 		}
12400 
12401 		func->publish_type =
12402 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12403 		func->publish_bcast =
12404 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12405 
12406 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12407 			func->publish_bcast) {
12408 			err = -EINVAL;
12409 			goto out;
12410 		}
12411 		break;
12412 	case NL80211_NAN_FUNC_SUBSCRIBE:
12413 		func->subscribe_active =
12414 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12415 		break;
12416 	case NL80211_NAN_FUNC_FOLLOW_UP:
12417 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12418 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12419 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12420 			err = -EINVAL;
12421 			goto out;
12422 		}
12423 
12424 		func->followup_id =
12425 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12426 		func->followup_reqid =
12427 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12428 		memcpy(func->followup_dest.addr,
12429 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12430 		       sizeof(func->followup_dest.addr));
12431 		if (func->ttl) {
12432 			err = -EINVAL;
12433 			goto out;
12434 		}
12435 		break;
12436 	default:
12437 		err = -EINVAL;
12438 		goto out;
12439 	}
12440 
12441 	if (tb[NL80211_NAN_FUNC_SRF]) {
12442 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12443 
12444 		err = nla_parse_nested_deprecated(srf_tb,
12445 						  NL80211_NAN_SRF_ATTR_MAX,
12446 						  tb[NL80211_NAN_FUNC_SRF],
12447 						  nl80211_nan_srf_policy,
12448 						  info->extack);
12449 		if (err)
12450 			goto out;
12451 
12452 		func->srf_include =
12453 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12454 
12455 		if (srf_tb[NL80211_NAN_SRF_BF]) {
12456 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12457 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12458 				err = -EINVAL;
12459 				goto out;
12460 			}
12461 
12462 			func->srf_bf_len =
12463 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12464 			func->srf_bf =
12465 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12466 					func->srf_bf_len, GFP_KERNEL);
12467 			if (!func->srf_bf) {
12468 				err = -ENOMEM;
12469 				goto out;
12470 			}
12471 
12472 			func->srf_bf_idx =
12473 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12474 		} else {
12475 			struct nlattr *attr, *mac_attr =
12476 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12477 			int n_entries, rem, i = 0;
12478 
12479 			if (!mac_attr) {
12480 				err = -EINVAL;
12481 				goto out;
12482 			}
12483 
12484 			n_entries = validate_acl_mac_addrs(mac_attr);
12485 			if (n_entries <= 0) {
12486 				err = -EINVAL;
12487 				goto out;
12488 			}
12489 
12490 			func->srf_num_macs = n_entries;
12491 			func->srf_macs =
12492 				kcalloc(n_entries, sizeof(*func->srf_macs),
12493 					GFP_KERNEL);
12494 			if (!func->srf_macs) {
12495 				err = -ENOMEM;
12496 				goto out;
12497 			}
12498 
12499 			nla_for_each_nested(attr, mac_attr, rem)
12500 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
12501 				       sizeof(*func->srf_macs));
12502 		}
12503 	}
12504 
12505 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12506 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12507 					func, true);
12508 		if (err)
12509 			goto out;
12510 	}
12511 
12512 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12513 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12514 					func, false);
12515 		if (err)
12516 			goto out;
12517 	}
12518 
12519 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12520 	if (!msg) {
12521 		err = -ENOMEM;
12522 		goto out;
12523 	}
12524 
12525 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12526 			     NL80211_CMD_ADD_NAN_FUNCTION);
12527 	/* This can't really happen - we just allocated 4KB */
12528 	if (WARN_ON(!hdr)) {
12529 		err = -ENOMEM;
12530 		goto out;
12531 	}
12532 
12533 	err = rdev_add_nan_func(rdev, wdev, func);
12534 out:
12535 	if (err < 0) {
12536 		cfg80211_free_nan_func(func);
12537 		nlmsg_free(msg);
12538 		return err;
12539 	}
12540 
12541 	/* propagate the instance id and cookie to userspace  */
12542 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12543 			      NL80211_ATTR_PAD))
12544 		goto nla_put_failure;
12545 
12546 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12547 	if (!func_attr)
12548 		goto nla_put_failure;
12549 
12550 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12551 		       func->instance_id))
12552 		goto nla_put_failure;
12553 
12554 	nla_nest_end(msg, func_attr);
12555 
12556 	genlmsg_end(msg, hdr);
12557 	return genlmsg_reply(msg, info);
12558 
12559 nla_put_failure:
12560 	nlmsg_free(msg);
12561 	return -ENOBUFS;
12562 }
12563 
nl80211_nan_del_func(struct sk_buff * skb,struct genl_info * info)12564 static int nl80211_nan_del_func(struct sk_buff *skb,
12565 			       struct genl_info *info)
12566 {
12567 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12568 	struct wireless_dev *wdev = info->user_ptr[1];
12569 	u64 cookie;
12570 
12571 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12572 		return -EOPNOTSUPP;
12573 
12574 	if (!wdev_running(wdev))
12575 		return -ENOTCONN;
12576 
12577 	if (!info->attrs[NL80211_ATTR_COOKIE])
12578 		return -EINVAL;
12579 
12580 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12581 
12582 	rdev_del_nan_func(rdev, wdev, cookie);
12583 
12584 	return 0;
12585 }
12586 
nl80211_nan_change_config(struct sk_buff * skb,struct genl_info * info)12587 static int nl80211_nan_change_config(struct sk_buff *skb,
12588 				     struct genl_info *info)
12589 {
12590 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12591 	struct wireless_dev *wdev = info->user_ptr[1];
12592 	struct cfg80211_nan_conf conf = {};
12593 	u32 changed = 0;
12594 
12595 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12596 		return -EOPNOTSUPP;
12597 
12598 	if (!wdev_running(wdev))
12599 		return -ENOTCONN;
12600 
12601 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12602 		conf.master_pref =
12603 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12604 		if (conf.master_pref <= 1 || conf.master_pref == 255)
12605 			return -EINVAL;
12606 
12607 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12608 	}
12609 
12610 	if (info->attrs[NL80211_ATTR_BANDS]) {
12611 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12612 
12613 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12614 			return -EOPNOTSUPP;
12615 
12616 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12617 			return -EINVAL;
12618 
12619 		conf.bands = bands;
12620 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12621 	}
12622 
12623 	if (!changed)
12624 		return -EINVAL;
12625 
12626 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12627 }
12628 
cfg80211_nan_match(struct wireless_dev * wdev,struct cfg80211_nan_match_params * match,gfp_t gfp)12629 void cfg80211_nan_match(struct wireless_dev *wdev,
12630 			struct cfg80211_nan_match_params *match, gfp_t gfp)
12631 {
12632 	struct wiphy *wiphy = wdev->wiphy;
12633 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12634 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12635 	struct sk_buff *msg;
12636 	void *hdr;
12637 
12638 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12639 		return;
12640 
12641 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12642 	if (!msg)
12643 		return;
12644 
12645 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12646 	if (!hdr) {
12647 		nlmsg_free(msg);
12648 		return;
12649 	}
12650 
12651 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12652 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12653 					 wdev->netdev->ifindex)) ||
12654 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12655 			      NL80211_ATTR_PAD))
12656 		goto nla_put_failure;
12657 
12658 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12659 			      NL80211_ATTR_PAD) ||
12660 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12661 		goto nla_put_failure;
12662 
12663 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12664 	if (!match_attr)
12665 		goto nla_put_failure;
12666 
12667 	local_func_attr = nla_nest_start_noflag(msg,
12668 						NL80211_NAN_MATCH_FUNC_LOCAL);
12669 	if (!local_func_attr)
12670 		goto nla_put_failure;
12671 
12672 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12673 		goto nla_put_failure;
12674 
12675 	nla_nest_end(msg, local_func_attr);
12676 
12677 	peer_func_attr = nla_nest_start_noflag(msg,
12678 					       NL80211_NAN_MATCH_FUNC_PEER);
12679 	if (!peer_func_attr)
12680 		goto nla_put_failure;
12681 
12682 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12683 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12684 		goto nla_put_failure;
12685 
12686 	if (match->info && match->info_len &&
12687 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12688 		    match->info))
12689 		goto nla_put_failure;
12690 
12691 	nla_nest_end(msg, peer_func_attr);
12692 	nla_nest_end(msg, match_attr);
12693 	genlmsg_end(msg, hdr);
12694 
12695 	if (!wdev->owner_nlportid)
12696 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12697 					msg, 0, NL80211_MCGRP_NAN, gfp);
12698 	else
12699 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12700 				wdev->owner_nlportid);
12701 
12702 	return;
12703 
12704 nla_put_failure:
12705 	nlmsg_free(msg);
12706 }
12707 EXPORT_SYMBOL(cfg80211_nan_match);
12708 
cfg80211_nan_func_terminated(struct wireless_dev * wdev,u8 inst_id,enum nl80211_nan_func_term_reason reason,u64 cookie,gfp_t gfp)12709 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12710 				  u8 inst_id,
12711 				  enum nl80211_nan_func_term_reason reason,
12712 				  u64 cookie, gfp_t gfp)
12713 {
12714 	struct wiphy *wiphy = wdev->wiphy;
12715 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12716 	struct sk_buff *msg;
12717 	struct nlattr *func_attr;
12718 	void *hdr;
12719 
12720 	if (WARN_ON(!inst_id))
12721 		return;
12722 
12723 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12724 	if (!msg)
12725 		return;
12726 
12727 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12728 	if (!hdr) {
12729 		nlmsg_free(msg);
12730 		return;
12731 	}
12732 
12733 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12734 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12735 					 wdev->netdev->ifindex)) ||
12736 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12737 			      NL80211_ATTR_PAD))
12738 		goto nla_put_failure;
12739 
12740 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12741 			      NL80211_ATTR_PAD))
12742 		goto nla_put_failure;
12743 
12744 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12745 	if (!func_attr)
12746 		goto nla_put_failure;
12747 
12748 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12749 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12750 		goto nla_put_failure;
12751 
12752 	nla_nest_end(msg, func_attr);
12753 	genlmsg_end(msg, hdr);
12754 
12755 	if (!wdev->owner_nlportid)
12756 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12757 					msg, 0, NL80211_MCGRP_NAN, gfp);
12758 	else
12759 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12760 				wdev->owner_nlportid);
12761 
12762 	return;
12763 
12764 nla_put_failure:
12765 	nlmsg_free(msg);
12766 }
12767 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12768 
nl80211_get_protocol_features(struct sk_buff * skb,struct genl_info * info)12769 static int nl80211_get_protocol_features(struct sk_buff *skb,
12770 					 struct genl_info *info)
12771 {
12772 	void *hdr;
12773 	struct sk_buff *msg;
12774 
12775 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12776 	if (!msg)
12777 		return -ENOMEM;
12778 
12779 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12780 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
12781 	if (!hdr)
12782 		goto nla_put_failure;
12783 
12784 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12785 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12786 		goto nla_put_failure;
12787 
12788 	genlmsg_end(msg, hdr);
12789 	return genlmsg_reply(msg, info);
12790 
12791  nla_put_failure:
12792 	kfree_skb(msg);
12793 	return -ENOBUFS;
12794 }
12795 
nl80211_update_ft_ies(struct sk_buff * skb,struct genl_info * info)12796 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12797 {
12798 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12799 	struct cfg80211_update_ft_ies_params ft_params;
12800 	struct net_device *dev = info->user_ptr[1];
12801 
12802 	if (!rdev->ops->update_ft_ies)
12803 		return -EOPNOTSUPP;
12804 
12805 	if (!info->attrs[NL80211_ATTR_MDID] ||
12806 	    !info->attrs[NL80211_ATTR_IE])
12807 		return -EINVAL;
12808 
12809 	memset(&ft_params, 0, sizeof(ft_params));
12810 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12811 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12812 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12813 
12814 	return rdev_update_ft_ies(rdev, dev, &ft_params);
12815 }
12816 
nl80211_crit_protocol_start(struct sk_buff * skb,struct genl_info * info)12817 static int nl80211_crit_protocol_start(struct sk_buff *skb,
12818 				       struct genl_info *info)
12819 {
12820 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12821 	struct wireless_dev *wdev = info->user_ptr[1];
12822 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12823 	u16 duration;
12824 	int ret;
12825 
12826 	if (!rdev->ops->crit_proto_start)
12827 		return -EOPNOTSUPP;
12828 
12829 	if (WARN_ON(!rdev->ops->crit_proto_stop))
12830 		return -EINVAL;
12831 
12832 	if (rdev->crit_proto_nlportid)
12833 		return -EBUSY;
12834 
12835 	/* determine protocol if provided */
12836 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12837 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12838 
12839 	if (proto >= NUM_NL80211_CRIT_PROTO)
12840 		return -EINVAL;
12841 
12842 	/* timeout must be provided */
12843 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12844 		return -EINVAL;
12845 
12846 	duration =
12847 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12848 
12849 	if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12850 		return -ERANGE;
12851 
12852 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12853 	if (!ret)
12854 		rdev->crit_proto_nlportid = info->snd_portid;
12855 
12856 	return ret;
12857 }
12858 
nl80211_crit_protocol_stop(struct sk_buff * skb,struct genl_info * info)12859 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12860 				      struct genl_info *info)
12861 {
12862 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12863 	struct wireless_dev *wdev = info->user_ptr[1];
12864 
12865 	if (!rdev->ops->crit_proto_stop)
12866 		return -EOPNOTSUPP;
12867 
12868 	if (rdev->crit_proto_nlportid) {
12869 		rdev->crit_proto_nlportid = 0;
12870 		rdev_crit_proto_stop(rdev, wdev);
12871 	}
12872 	return 0;
12873 }
12874 
nl80211_vendor_check_policy(const struct wiphy_vendor_command * vcmd,struct nlattr * attr,struct netlink_ext_ack * extack)12875 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
12876 				       struct nlattr *attr,
12877 				       struct netlink_ext_ack *extack)
12878 {
12879 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
12880 		if (attr->nla_type & NLA_F_NESTED) {
12881 			NL_SET_ERR_MSG_ATTR(extack, attr,
12882 					    "unexpected nested data");
12883 			return -EINVAL;
12884 		}
12885 
12886 		return 0;
12887 	}
12888 
12889 	if (!(attr->nla_type & NLA_F_NESTED)) {
12890 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
12891 		return -EINVAL;
12892 	}
12893 
12894 	return nl80211_validate_nested(attr, vcmd->maxattr, vcmd->policy,
12895 				       extack);
12896 }
12897 
nl80211_vendor_cmd(struct sk_buff * skb,struct genl_info * info)12898 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12899 {
12900 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12901 	struct wireless_dev *wdev =
12902 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12903 	int i, err;
12904 	u32 vid, subcmd;
12905 
12906 	if (!rdev->wiphy.vendor_commands)
12907 		return -EOPNOTSUPP;
12908 
12909 	if (IS_ERR(wdev)) {
12910 		err = PTR_ERR(wdev);
12911 		if (err != -EINVAL)
12912 			return err;
12913 		wdev = NULL;
12914 	} else if (wdev->wiphy != &rdev->wiphy) {
12915 		return -EINVAL;
12916 	}
12917 
12918 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12919 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12920 		return -EINVAL;
12921 
12922 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12923 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12924 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12925 		const struct wiphy_vendor_command *vcmd;
12926 		void *data = NULL;
12927 		int len = 0;
12928 
12929 		vcmd = &rdev->wiphy.vendor_commands[i];
12930 
12931 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12932 			continue;
12933 
12934 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12935 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12936 			if (!wdev)
12937 				return -EINVAL;
12938 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12939 			    !wdev->netdev)
12940 				return -EINVAL;
12941 
12942 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12943 				if (!wdev_running(wdev))
12944 					return -ENETDOWN;
12945 			}
12946 
12947 			if (!vcmd->doit)
12948 				return -EOPNOTSUPP;
12949 		} else {
12950 			wdev = NULL;
12951 		}
12952 
12953 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12954 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12955 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12956 
12957 			err = nl80211_vendor_check_policy(vcmd,
12958 					info->attrs[NL80211_ATTR_VENDOR_DATA],
12959 					info->extack);
12960 			if (err)
12961 				return err;
12962 		}
12963 
12964 		rdev->cur_cmd_info = info;
12965 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
12966 		rdev->cur_cmd_info = NULL;
12967 		return err;
12968 	}
12969 
12970 	return -EOPNOTSUPP;
12971 }
12972 
nl80211_prepare_vendor_dump(struct sk_buff * skb,struct netlink_callback * cb,struct cfg80211_registered_device ** rdev,struct wireless_dev ** wdev)12973 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12974 				       struct netlink_callback *cb,
12975 				       struct cfg80211_registered_device **rdev,
12976 				       struct wireless_dev **wdev)
12977 {
12978 	struct nlattr **attrbuf;
12979 	u32 vid, subcmd;
12980 	unsigned int i;
12981 	int vcmd_idx = -1;
12982 	int err;
12983 	void *data = NULL;
12984 	unsigned int data_len = 0;
12985 
12986 	if (cb->args[0]) {
12987 		/* subtract the 1 again here */
12988 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12989 		struct wireless_dev *tmp;
12990 
12991 		if (!wiphy)
12992 			return -ENODEV;
12993 		*rdev = wiphy_to_rdev(wiphy);
12994 		*wdev = NULL;
12995 
12996 		if (cb->args[1]) {
12997 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
12998 				if (tmp->identifier == cb->args[1] - 1) {
12999 					*wdev = tmp;
13000 					break;
13001 				}
13002 			}
13003 		}
13004 
13005 		/* keep rtnl locked in successful case */
13006 		return 0;
13007 	}
13008 
13009 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13010 	if (!attrbuf)
13011 		return -ENOMEM;
13012 
13013 	err = nlmsg_parse_deprecated(cb->nlh,
13014 				     GENL_HDRLEN + nl80211_fam.hdrsize,
13015 				     attrbuf, nl80211_fam.maxattr,
13016 				     nl80211_policy, NULL);
13017 	if (err)
13018 		goto out;
13019 
13020 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13021 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13022 		err = -EINVAL;
13023 		goto out;
13024 	}
13025 
13026 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13027 	if (IS_ERR(*wdev))
13028 		*wdev = NULL;
13029 
13030 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13031 	if (IS_ERR(*rdev)) {
13032 		err = PTR_ERR(*rdev);
13033 		goto out;
13034 	}
13035 
13036 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13037 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13038 
13039 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13040 		const struct wiphy_vendor_command *vcmd;
13041 
13042 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
13043 
13044 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13045 			continue;
13046 
13047 		if (!vcmd->dumpit) {
13048 			err = -EOPNOTSUPP;
13049 			goto out;
13050 		}
13051 
13052 		vcmd_idx = i;
13053 		break;
13054 	}
13055 
13056 	if (vcmd_idx < 0) {
13057 		err = -EOPNOTSUPP;
13058 		goto out;
13059 	}
13060 
13061 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13062 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13063 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13064 
13065 		err = nl80211_vendor_check_policy(
13066 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
13067 				attrbuf[NL80211_ATTR_VENDOR_DATA],
13068 				cb->extack);
13069 		if (err)
13070 			goto out;
13071 	}
13072 
13073 	/* 0 is the first index - add 1 to parse only once */
13074 	cb->args[0] = (*rdev)->wiphy_idx + 1;
13075 	/* add 1 to know if it was NULL */
13076 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13077 	cb->args[2] = vcmd_idx;
13078 	cb->args[3] = (unsigned long)data;
13079 	cb->args[4] = data_len;
13080 
13081 	/* keep rtnl locked in successful case */
13082 	err = 0;
13083 out:
13084 	kfree(attrbuf);
13085 	return err;
13086 }
13087 
nl80211_vendor_cmd_dump(struct sk_buff * skb,struct netlink_callback * cb)13088 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13089 				   struct netlink_callback *cb)
13090 {
13091 	struct cfg80211_registered_device *rdev;
13092 	struct wireless_dev *wdev;
13093 	unsigned int vcmd_idx;
13094 	const struct wiphy_vendor_command *vcmd;
13095 	void *data;
13096 	int data_len;
13097 	int err;
13098 	struct nlattr *vendor_data;
13099 
13100 	rtnl_lock();
13101 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13102 	if (err)
13103 		goto out;
13104 
13105 	vcmd_idx = cb->args[2];
13106 	data = (void *)cb->args[3];
13107 	data_len = cb->args[4];
13108 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13109 
13110 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13111 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13112 		if (!wdev) {
13113 			err = -EINVAL;
13114 			goto out;
13115 		}
13116 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13117 		    !wdev->netdev) {
13118 			err = -EINVAL;
13119 			goto out;
13120 		}
13121 
13122 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13123 			if (!wdev_running(wdev)) {
13124 				err = -ENETDOWN;
13125 				goto out;
13126 			}
13127 		}
13128 	}
13129 
13130 	while (1) {
13131 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13132 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13133 					   NL80211_CMD_VENDOR);
13134 		if (!hdr)
13135 			break;
13136 
13137 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13138 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13139 					       wdev_id(wdev),
13140 					       NL80211_ATTR_PAD))) {
13141 			genlmsg_cancel(skb, hdr);
13142 			break;
13143 		}
13144 
13145 		vendor_data = nla_nest_start_noflag(skb,
13146 						    NL80211_ATTR_VENDOR_DATA);
13147 		if (!vendor_data) {
13148 			genlmsg_cancel(skb, hdr);
13149 			break;
13150 		}
13151 
13152 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13153 				   (unsigned long *)&cb->args[5]);
13154 		nla_nest_end(skb, vendor_data);
13155 
13156 		if (err == -ENOBUFS || err == -ENOENT) {
13157 			genlmsg_cancel(skb, hdr);
13158 			break;
13159 		} else if (err) {
13160 			genlmsg_cancel(skb, hdr);
13161 			goto out;
13162 		}
13163 
13164 		genlmsg_end(skb, hdr);
13165 	}
13166 
13167 	err = skb->len;
13168  out:
13169 	rtnl_unlock();
13170 	return err;
13171 }
13172 
__cfg80211_alloc_reply_skb(struct wiphy * wiphy,enum nl80211_commands cmd,enum nl80211_attrs attr,int approxlen)13173 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13174 					   enum nl80211_commands cmd,
13175 					   enum nl80211_attrs attr,
13176 					   int approxlen)
13177 {
13178 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13179 
13180 	if (WARN_ON(!rdev->cur_cmd_info))
13181 		return NULL;
13182 
13183 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13184 					   rdev->cur_cmd_info->snd_portid,
13185 					   rdev->cur_cmd_info->snd_seq,
13186 					   cmd, attr, NULL, GFP_KERNEL);
13187 }
13188 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13189 
cfg80211_vendor_cmd_reply(struct sk_buff * skb)13190 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13191 {
13192 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13193 	void *hdr = ((void **)skb->cb)[1];
13194 	struct nlattr *data = ((void **)skb->cb)[2];
13195 
13196 	/* clear CB data for netlink core to own from now on */
13197 	memset(skb->cb, 0, sizeof(skb->cb));
13198 
13199 	if (WARN_ON(!rdev->cur_cmd_info)) {
13200 		kfree_skb(skb);
13201 		return -EINVAL;
13202 	}
13203 
13204 	nla_nest_end(skb, data);
13205 	genlmsg_end(skb, hdr);
13206 	return genlmsg_reply(skb, rdev->cur_cmd_info);
13207 }
13208 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13209 
cfg80211_vendor_cmd_get_sender(struct wiphy * wiphy)13210 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13211 {
13212 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13213 
13214 	if (WARN_ON(!rdev->cur_cmd_info))
13215 		return 0;
13216 
13217 	return rdev->cur_cmd_info->snd_portid;
13218 }
13219 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13220 
nl80211_set_qos_map(struct sk_buff * skb,struct genl_info * info)13221 static int nl80211_set_qos_map(struct sk_buff *skb,
13222 			       struct genl_info *info)
13223 {
13224 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13225 	struct cfg80211_qos_map *qos_map = NULL;
13226 	struct net_device *dev = info->user_ptr[1];
13227 	u8 *pos, len, num_des, des_len, des;
13228 	int ret;
13229 
13230 	if (!rdev->ops->set_qos_map)
13231 		return -EOPNOTSUPP;
13232 
13233 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13234 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13235 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13236 
13237 		if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13238 		    len > IEEE80211_QOS_MAP_LEN_MAX)
13239 			return -EINVAL;
13240 
13241 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13242 		if (!qos_map)
13243 			return -ENOMEM;
13244 
13245 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13246 		if (num_des) {
13247 			des_len = num_des *
13248 				sizeof(struct cfg80211_dscp_exception);
13249 			memcpy(qos_map->dscp_exception, pos, des_len);
13250 			qos_map->num_des = num_des;
13251 			for (des = 0; des < num_des; des++) {
13252 				if (qos_map->dscp_exception[des].up > 7) {
13253 					kfree(qos_map);
13254 					return -EINVAL;
13255 				}
13256 			}
13257 			pos += des_len;
13258 		}
13259 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13260 	}
13261 
13262 	wdev_lock(dev->ieee80211_ptr);
13263 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
13264 	if (!ret)
13265 		ret = rdev_set_qos_map(rdev, dev, qos_map);
13266 	wdev_unlock(dev->ieee80211_ptr);
13267 
13268 	kfree(qos_map);
13269 	return ret;
13270 }
13271 
nl80211_add_tx_ts(struct sk_buff * skb,struct genl_info * info)13272 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13273 {
13274 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13275 	struct net_device *dev = info->user_ptr[1];
13276 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13277 	const u8 *peer;
13278 	u8 tsid, up;
13279 	u16 admitted_time = 0;
13280 	int err;
13281 
13282 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13283 		return -EOPNOTSUPP;
13284 
13285 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13286 	    !info->attrs[NL80211_ATTR_USER_PRIO])
13287 		return -EINVAL;
13288 
13289 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13290 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13291 
13292 	/* WMM uses TIDs 0-7 even for TSPEC */
13293 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13294 		/* TODO: handle 802.11 TSPEC/admission control
13295 		 * need more attributes for that (e.g. BA session requirement);
13296 		 * change the WMM adminssion test above to allow both then
13297 		 */
13298 		return -EINVAL;
13299 	}
13300 
13301 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13302 
13303 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13304 		admitted_time =
13305 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13306 		if (!admitted_time)
13307 			return -EINVAL;
13308 	}
13309 
13310 	wdev_lock(wdev);
13311 	switch (wdev->iftype) {
13312 	case NL80211_IFTYPE_STATION:
13313 	case NL80211_IFTYPE_P2P_CLIENT:
13314 		if (wdev->current_bss)
13315 			break;
13316 		err = -ENOTCONN;
13317 		goto out;
13318 	default:
13319 		err = -EOPNOTSUPP;
13320 		goto out;
13321 	}
13322 
13323 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13324 
13325  out:
13326 	wdev_unlock(wdev);
13327 	return err;
13328 }
13329 
nl80211_del_tx_ts(struct sk_buff * skb,struct genl_info * info)13330 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13331 {
13332 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13333 	struct net_device *dev = info->user_ptr[1];
13334 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13335 	const u8 *peer;
13336 	u8 tsid;
13337 	int err;
13338 
13339 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13340 		return -EINVAL;
13341 
13342 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13343 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13344 
13345 	wdev_lock(wdev);
13346 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13347 	wdev_unlock(wdev);
13348 
13349 	return err;
13350 }
13351 
nl80211_tdls_channel_switch(struct sk_buff * skb,struct genl_info * info)13352 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13353 				       struct genl_info *info)
13354 {
13355 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13356 	struct net_device *dev = info->user_ptr[1];
13357 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13358 	struct cfg80211_chan_def chandef = {};
13359 	const u8 *addr;
13360 	u8 oper_class;
13361 	int err;
13362 
13363 	if (!rdev->ops->tdls_channel_switch ||
13364 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13365 		return -EOPNOTSUPP;
13366 
13367 	switch (dev->ieee80211_ptr->iftype) {
13368 	case NL80211_IFTYPE_STATION:
13369 	case NL80211_IFTYPE_P2P_CLIENT:
13370 		break;
13371 	default:
13372 		return -EOPNOTSUPP;
13373 	}
13374 
13375 	if (!info->attrs[NL80211_ATTR_MAC] ||
13376 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
13377 		return -EINVAL;
13378 
13379 	err = nl80211_parse_chandef(rdev, info, &chandef);
13380 	if (err)
13381 		return err;
13382 
13383 	/*
13384 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13385 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13386 	 * specification is not defined for them.
13387 	 */
13388 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
13389 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13390 	    chandef.width != NL80211_CHAN_WIDTH_20)
13391 		return -EINVAL;
13392 
13393 	/* we will be active on the TDLS link */
13394 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13395 					   wdev->iftype))
13396 		return -EINVAL;
13397 
13398 	/* don't allow switching to DFS channels */
13399 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13400 		return -EINVAL;
13401 
13402 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13403 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13404 
13405 	wdev_lock(wdev);
13406 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13407 	wdev_unlock(wdev);
13408 
13409 	return err;
13410 }
13411 
nl80211_tdls_cancel_channel_switch(struct sk_buff * skb,struct genl_info * info)13412 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13413 					      struct genl_info *info)
13414 {
13415 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13416 	struct net_device *dev = info->user_ptr[1];
13417 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13418 	const u8 *addr;
13419 
13420 	if (!rdev->ops->tdls_channel_switch ||
13421 	    !rdev->ops->tdls_cancel_channel_switch ||
13422 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13423 		return -EOPNOTSUPP;
13424 
13425 	switch (dev->ieee80211_ptr->iftype) {
13426 	case NL80211_IFTYPE_STATION:
13427 	case NL80211_IFTYPE_P2P_CLIENT:
13428 		break;
13429 	default:
13430 		return -EOPNOTSUPP;
13431 	}
13432 
13433 	if (!info->attrs[NL80211_ATTR_MAC])
13434 		return -EINVAL;
13435 
13436 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13437 
13438 	wdev_lock(wdev);
13439 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13440 	wdev_unlock(wdev);
13441 
13442 	return 0;
13443 }
13444 
nl80211_set_multicast_to_unicast(struct sk_buff * skb,struct genl_info * info)13445 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13446 					    struct genl_info *info)
13447 {
13448 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13449 	struct net_device *dev = info->user_ptr[1];
13450 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13451 	const struct nlattr *nla;
13452 	bool enabled;
13453 
13454 	if (!rdev->ops->set_multicast_to_unicast)
13455 		return -EOPNOTSUPP;
13456 
13457 	if (wdev->iftype != NL80211_IFTYPE_AP &&
13458 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
13459 		return -EOPNOTSUPP;
13460 
13461 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13462 	enabled = nla_get_flag(nla);
13463 
13464 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13465 }
13466 
nl80211_set_pmk(struct sk_buff * skb,struct genl_info * info)13467 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13468 {
13469 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13470 	struct net_device *dev = info->user_ptr[1];
13471 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13472 	struct cfg80211_pmk_conf pmk_conf = {};
13473 	int ret;
13474 
13475 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13476 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13477 		return -EOPNOTSUPP;
13478 
13479 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13480 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13481 		return -EOPNOTSUPP;
13482 
13483 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13484 		return -EINVAL;
13485 
13486 	wdev_lock(wdev);
13487 	if (!wdev->current_bss) {
13488 		ret = -ENOTCONN;
13489 		goto out;
13490 	}
13491 
13492 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13493 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13494 		ret = -EINVAL;
13495 		goto out;
13496 	}
13497 
13498 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13499 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13500 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13501 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13502 		ret = -EINVAL;
13503 		goto out;
13504 	}
13505 
13506 	if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13507 		int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13508 
13509 		if (r0_name_len != WLAN_PMK_NAME_LEN) {
13510 			ret = -EINVAL;
13511 			goto out;
13512 		}
13513 
13514 		pmk_conf.pmk_r0_name =
13515 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13516 	}
13517 
13518 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13519 out:
13520 	wdev_unlock(wdev);
13521 	return ret;
13522 }
13523 
nl80211_del_pmk(struct sk_buff * skb,struct genl_info * info)13524 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13525 {
13526 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13527 	struct net_device *dev = info->user_ptr[1];
13528 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13529 	const u8 *aa;
13530 	int ret;
13531 
13532 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
13533 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13534 		return -EOPNOTSUPP;
13535 
13536 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13537 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13538 		return -EOPNOTSUPP;
13539 
13540 	if (!info->attrs[NL80211_ATTR_MAC])
13541 		return -EINVAL;
13542 
13543 	wdev_lock(wdev);
13544 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13545 	ret = rdev_del_pmk(rdev, dev, aa);
13546 	wdev_unlock(wdev);
13547 
13548 	return ret;
13549 }
13550 
nl80211_external_auth(struct sk_buff * skb,struct genl_info * info)13551 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13552 {
13553 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13554 	struct net_device *dev = info->user_ptr[1];
13555 	struct cfg80211_external_auth_params params;
13556 
13557 	if (!rdev->ops->external_auth)
13558 		return -EOPNOTSUPP;
13559 
13560 	if (!info->attrs[NL80211_ATTR_SSID] &&
13561 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13562 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13563 		return -EINVAL;
13564 
13565 	if (!info->attrs[NL80211_ATTR_BSSID])
13566 		return -EINVAL;
13567 
13568 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13569 		return -EINVAL;
13570 
13571 	memset(&params, 0, sizeof(params));
13572 
13573 	if (info->attrs[NL80211_ATTR_SSID]) {
13574 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13575 		if (params.ssid.ssid_len == 0 ||
13576 		    params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13577 			return -EINVAL;
13578 		memcpy(params.ssid.ssid,
13579 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
13580 		       params.ssid.ssid_len);
13581 	}
13582 
13583 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13584 	       ETH_ALEN);
13585 
13586 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13587 
13588 	if (info->attrs[NL80211_ATTR_PMKID])
13589 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13590 
13591 	return rdev_external_auth(rdev, dev, &params);
13592 }
13593 
nl80211_tx_control_port(struct sk_buff * skb,struct genl_info * info)13594 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13595 {
13596 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13597 	struct net_device *dev = info->user_ptr[1];
13598 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13599 	const u8 *buf;
13600 	size_t len;
13601 	u8 *dest;
13602 	u16 proto;
13603 	bool noencrypt;
13604 	int err;
13605 
13606 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
13607 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13608 		return -EOPNOTSUPP;
13609 
13610 	if (!rdev->ops->tx_control_port)
13611 		return -EOPNOTSUPP;
13612 
13613 	if (!info->attrs[NL80211_ATTR_FRAME] ||
13614 	    !info->attrs[NL80211_ATTR_MAC] ||
13615 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13616 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13617 		return -EINVAL;
13618 	}
13619 
13620 	wdev_lock(wdev);
13621 
13622 	switch (wdev->iftype) {
13623 	case NL80211_IFTYPE_AP:
13624 	case NL80211_IFTYPE_P2P_GO:
13625 	case NL80211_IFTYPE_MESH_POINT:
13626 		break;
13627 	case NL80211_IFTYPE_ADHOC:
13628 	case NL80211_IFTYPE_STATION:
13629 	case NL80211_IFTYPE_P2P_CLIENT:
13630 		if (wdev->current_bss)
13631 			break;
13632 		err = -ENOTCONN;
13633 		goto out;
13634 	default:
13635 		err = -EOPNOTSUPP;
13636 		goto out;
13637 	}
13638 
13639 	wdev_unlock(wdev);
13640 
13641 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13642 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13643 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13644 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13645 	noencrypt =
13646 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13647 
13648 	return rdev_tx_control_port(rdev, dev, buf, len,
13649 				    dest, cpu_to_be16(proto), noencrypt);
13650 
13651  out:
13652 	wdev_unlock(wdev);
13653 	return err;
13654 }
13655 
nl80211_get_ftm_responder_stats(struct sk_buff * skb,struct genl_info * info)13656 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13657 					   struct genl_info *info)
13658 {
13659 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13660 	struct net_device *dev = info->user_ptr[1];
13661 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13662 	struct cfg80211_ftm_responder_stats ftm_stats = {};
13663 	struct sk_buff *msg;
13664 	void *hdr;
13665 	struct nlattr *ftm_stats_attr;
13666 	int err;
13667 
13668 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13669 		return -EOPNOTSUPP;
13670 
13671 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13672 	if (err)
13673 		return err;
13674 
13675 	if (!ftm_stats.filled)
13676 		return -ENODATA;
13677 
13678 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13679 	if (!msg)
13680 		return -ENOMEM;
13681 
13682 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13683 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
13684 	if (!hdr)
13685 		goto nla_put_failure;
13686 
13687 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
13688 		goto nla_put_failure;
13689 
13690 	ftm_stats_attr = nla_nest_start_noflag(msg,
13691 					       NL80211_ATTR_FTM_RESPONDER_STATS);
13692 	if (!ftm_stats_attr)
13693 		goto nla_put_failure;
13694 
13695 #define SET_FTM(field, name, type)					 \
13696 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13697 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
13698 			     ftm_stats.field))				 \
13699 		goto nla_put_failure; } while (0)
13700 #define SET_FTM_U64(field, name)					 \
13701 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13702 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
13703 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
13704 		goto nla_put_failure; } while (0)
13705 
13706 	SET_FTM(success_num, SUCCESS_NUM, u32);
13707 	SET_FTM(partial_num, PARTIAL_NUM, u32);
13708 	SET_FTM(failed_num, FAILED_NUM, u32);
13709 	SET_FTM(asap_num, ASAP_NUM, u32);
13710 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
13711 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
13712 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
13713 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
13714 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
13715 #undef SET_FTM
13716 
13717 	nla_nest_end(msg, ftm_stats_attr);
13718 
13719 	genlmsg_end(msg, hdr);
13720 	return genlmsg_reply(msg, info);
13721 
13722 nla_put_failure:
13723 	nlmsg_free(msg);
13724 	return -ENOBUFS;
13725 }
13726 
nl80211_update_owe_info(struct sk_buff * skb,struct genl_info * info)13727 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
13728 {
13729 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13730 	struct cfg80211_update_owe_info owe_info;
13731 	struct net_device *dev = info->user_ptr[1];
13732 
13733 	if (!rdev->ops->update_owe_info)
13734 		return -EOPNOTSUPP;
13735 
13736 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
13737 	    !info->attrs[NL80211_ATTR_MAC])
13738 		return -EINVAL;
13739 
13740 	memset(&owe_info, 0, sizeof(owe_info));
13741 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13742 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
13743 
13744 	if (info->attrs[NL80211_ATTR_IE]) {
13745 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13746 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13747 	}
13748 
13749 	return rdev_update_owe_info(rdev, dev, &owe_info);
13750 }
13751 
nl80211_probe_mesh_link(struct sk_buff * skb,struct genl_info * info)13752 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
13753 {
13754 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13755 	struct net_device *dev = info->user_ptr[1];
13756 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13757 	struct station_info sinfo = {};
13758 	const u8 *buf;
13759 	size_t len;
13760 	u8 *dest;
13761 	int err;
13762 
13763 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
13764 		return -EOPNOTSUPP;
13765 
13766 	if (!info->attrs[NL80211_ATTR_MAC] ||
13767 	    !info->attrs[NL80211_ATTR_FRAME]) {
13768 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
13769 		return -EINVAL;
13770 	}
13771 
13772 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
13773 		return -EOPNOTSUPP;
13774 
13775 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13776 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13777 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13778 
13779 	if (len < sizeof(struct ethhdr))
13780 		return -EINVAL;
13781 
13782 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
13783 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
13784 		return -EINVAL;
13785 
13786 	err = rdev_get_station(rdev, dev, dest, &sinfo);
13787 	if (err)
13788 		return err;
13789 
13790 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
13791 }
13792 
13793 #define NL80211_FLAG_NEED_WIPHY		0x01
13794 #define NL80211_FLAG_NEED_NETDEV	0x02
13795 #define NL80211_FLAG_NEED_RTNL		0x04
13796 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
13797 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
13798 					 NL80211_FLAG_CHECK_NETDEV_UP)
13799 #define NL80211_FLAG_NEED_WDEV		0x10
13800 /* If a netdev is associated, it must be UP, P2P must be started */
13801 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
13802 					 NL80211_FLAG_CHECK_NETDEV_UP)
13803 #define NL80211_FLAG_CLEAR_SKB		0x20
13804 
nl80211_pre_doit(const struct genl_ops * ops,struct sk_buff * skb,struct genl_info * info)13805 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13806 			    struct genl_info *info)
13807 {
13808 	struct cfg80211_registered_device *rdev;
13809 	struct wireless_dev *wdev;
13810 	struct net_device *dev;
13811 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13812 
13813 	if (rtnl)
13814 		rtnl_lock();
13815 
13816 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13817 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13818 		if (IS_ERR(rdev)) {
13819 			if (rtnl)
13820 				rtnl_unlock();
13821 			return PTR_ERR(rdev);
13822 		}
13823 		info->user_ptr[0] = rdev;
13824 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13825 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13826 		ASSERT_RTNL();
13827 
13828 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13829 						  info->attrs);
13830 		if (IS_ERR(wdev)) {
13831 			if (rtnl)
13832 				rtnl_unlock();
13833 			return PTR_ERR(wdev);
13834 		}
13835 
13836 		dev = wdev->netdev;
13837 		rdev = wiphy_to_rdev(wdev->wiphy);
13838 
13839 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13840 			if (!dev) {
13841 				if (rtnl)
13842 					rtnl_unlock();
13843 				return -EINVAL;
13844 			}
13845 
13846 			info->user_ptr[1] = dev;
13847 		} else {
13848 			info->user_ptr[1] = wdev;
13849 		}
13850 
13851 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13852 		    !wdev_running(wdev)) {
13853 			if (rtnl)
13854 				rtnl_unlock();
13855 			return -ENETDOWN;
13856 		}
13857 
13858 		if (dev)
13859 			dev_hold(dev);
13860 
13861 		info->user_ptr[0] = rdev;
13862 	}
13863 
13864 	return 0;
13865 }
13866 
nl80211_post_doit(const struct genl_ops * ops,struct sk_buff * skb,struct genl_info * info)13867 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13868 			      struct genl_info *info)
13869 {
13870 	if (info->user_ptr[1]) {
13871 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13872 			struct wireless_dev *wdev = info->user_ptr[1];
13873 
13874 			if (wdev->netdev)
13875 				dev_put(wdev->netdev);
13876 		} else {
13877 			dev_put(info->user_ptr[1]);
13878 		}
13879 	}
13880 
13881 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13882 		rtnl_unlock();
13883 
13884 	/* If needed, clear the netlink message payload from the SKB
13885 	 * as it might contain key data that shouldn't stick around on
13886 	 * the heap after the SKB is freed. The netlink message header
13887 	 * is still needed for further processing, so leave it intact.
13888 	 */
13889 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13890 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
13891 
13892 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13893 	}
13894 }
13895 
13896 static const struct genl_ops nl80211_ops[] = {
13897 	{
13898 		.cmd = NL80211_CMD_GET_WIPHY,
13899 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13900 		.doit = nl80211_get_wiphy,
13901 		.dumpit = nl80211_dump_wiphy,
13902 		.done = nl80211_dump_wiphy_done,
13903 		/* can be retrieved by unprivileged users */
13904 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13905 				  NL80211_FLAG_NEED_RTNL,
13906 	},
13907 	{
13908 		.cmd = NL80211_CMD_SET_WIPHY,
13909 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13910 		.doit = nl80211_set_wiphy,
13911 		.flags = GENL_UNS_ADMIN_PERM,
13912 		.internal_flags = NL80211_FLAG_NEED_RTNL,
13913 	},
13914 	{
13915 		.cmd = NL80211_CMD_GET_INTERFACE,
13916 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13917 		.doit = nl80211_get_interface,
13918 		.dumpit = nl80211_dump_interface,
13919 		/* can be retrieved by unprivileged users */
13920 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13921 				  NL80211_FLAG_NEED_RTNL,
13922 	},
13923 	{
13924 		.cmd = NL80211_CMD_SET_INTERFACE,
13925 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13926 		.doit = nl80211_set_interface,
13927 		.flags = GENL_UNS_ADMIN_PERM,
13928 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
13929 				  NL80211_FLAG_NEED_RTNL,
13930 	},
13931 	{
13932 		.cmd = NL80211_CMD_NEW_INTERFACE,
13933 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13934 		.doit = nl80211_new_interface,
13935 		.flags = GENL_UNS_ADMIN_PERM,
13936 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
13937 				  NL80211_FLAG_NEED_RTNL,
13938 	},
13939 	{
13940 		.cmd = NL80211_CMD_DEL_INTERFACE,
13941 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13942 		.doit = nl80211_del_interface,
13943 		.flags = GENL_UNS_ADMIN_PERM,
13944 		.internal_flags = NL80211_FLAG_NEED_WDEV |
13945 				  NL80211_FLAG_NEED_RTNL,
13946 	},
13947 	{
13948 		.cmd = NL80211_CMD_GET_KEY,
13949 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13950 		.doit = nl80211_get_key,
13951 		.flags = GENL_UNS_ADMIN_PERM,
13952 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13953 				  NL80211_FLAG_NEED_RTNL,
13954 	},
13955 	{
13956 		.cmd = NL80211_CMD_SET_KEY,
13957 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13958 		.doit = nl80211_set_key,
13959 		.flags = GENL_UNS_ADMIN_PERM,
13960 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13961 				  NL80211_FLAG_NEED_RTNL |
13962 				  NL80211_FLAG_CLEAR_SKB,
13963 	},
13964 	{
13965 		.cmd = NL80211_CMD_NEW_KEY,
13966 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13967 		.doit = nl80211_new_key,
13968 		.flags = GENL_UNS_ADMIN_PERM,
13969 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13970 				  NL80211_FLAG_NEED_RTNL |
13971 				  NL80211_FLAG_CLEAR_SKB,
13972 	},
13973 	{
13974 		.cmd = NL80211_CMD_DEL_KEY,
13975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13976 		.doit = nl80211_del_key,
13977 		.flags = GENL_UNS_ADMIN_PERM,
13978 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13979 				  NL80211_FLAG_NEED_RTNL,
13980 	},
13981 	{
13982 		.cmd = NL80211_CMD_SET_BEACON,
13983 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13984 		.flags = GENL_UNS_ADMIN_PERM,
13985 		.doit = nl80211_set_beacon,
13986 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13987 				  NL80211_FLAG_NEED_RTNL,
13988 	},
13989 	{
13990 		.cmd = NL80211_CMD_START_AP,
13991 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13992 		.flags = GENL_UNS_ADMIN_PERM,
13993 		.doit = nl80211_start_ap,
13994 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13995 				  NL80211_FLAG_NEED_RTNL,
13996 	},
13997 	{
13998 		.cmd = NL80211_CMD_STOP_AP,
13999 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14000 		.flags = GENL_UNS_ADMIN_PERM,
14001 		.doit = nl80211_stop_ap,
14002 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14003 				  NL80211_FLAG_NEED_RTNL,
14004 	},
14005 	{
14006 		.cmd = NL80211_CMD_GET_STATION,
14007 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14008 		.doit = nl80211_get_station,
14009 		.dumpit = nl80211_dump_station,
14010 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14011 				  NL80211_FLAG_NEED_RTNL,
14012 	},
14013 	{
14014 		.cmd = NL80211_CMD_SET_STATION,
14015 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14016 		.doit = nl80211_set_station,
14017 		.flags = GENL_UNS_ADMIN_PERM,
14018 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14019 				  NL80211_FLAG_NEED_RTNL,
14020 	},
14021 	{
14022 		.cmd = NL80211_CMD_NEW_STATION,
14023 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14024 		.doit = nl80211_new_station,
14025 		.flags = GENL_UNS_ADMIN_PERM,
14026 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14027 				  NL80211_FLAG_NEED_RTNL,
14028 	},
14029 	{
14030 		.cmd = NL80211_CMD_DEL_STATION,
14031 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14032 		.doit = nl80211_del_station,
14033 		.flags = GENL_UNS_ADMIN_PERM,
14034 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14035 				  NL80211_FLAG_NEED_RTNL,
14036 	},
14037 	{
14038 		.cmd = NL80211_CMD_GET_MPATH,
14039 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14040 		.doit = nl80211_get_mpath,
14041 		.dumpit = nl80211_dump_mpath,
14042 		.flags = GENL_UNS_ADMIN_PERM,
14043 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14044 				  NL80211_FLAG_NEED_RTNL,
14045 	},
14046 	{
14047 		.cmd = NL80211_CMD_GET_MPP,
14048 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14049 		.doit = nl80211_get_mpp,
14050 		.dumpit = nl80211_dump_mpp,
14051 		.flags = GENL_UNS_ADMIN_PERM,
14052 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14053 				  NL80211_FLAG_NEED_RTNL,
14054 	},
14055 	{
14056 		.cmd = NL80211_CMD_SET_MPATH,
14057 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14058 		.doit = nl80211_set_mpath,
14059 		.flags = GENL_UNS_ADMIN_PERM,
14060 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14061 				  NL80211_FLAG_NEED_RTNL,
14062 	},
14063 	{
14064 		.cmd = NL80211_CMD_NEW_MPATH,
14065 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14066 		.doit = nl80211_new_mpath,
14067 		.flags = GENL_UNS_ADMIN_PERM,
14068 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14069 				  NL80211_FLAG_NEED_RTNL,
14070 	},
14071 	{
14072 		.cmd = NL80211_CMD_DEL_MPATH,
14073 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14074 		.doit = nl80211_del_mpath,
14075 		.flags = GENL_UNS_ADMIN_PERM,
14076 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14077 				  NL80211_FLAG_NEED_RTNL,
14078 	},
14079 	{
14080 		.cmd = NL80211_CMD_SET_BSS,
14081 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14082 		.doit = nl80211_set_bss,
14083 		.flags = GENL_UNS_ADMIN_PERM,
14084 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14085 				  NL80211_FLAG_NEED_RTNL,
14086 	},
14087 	{
14088 		.cmd = NL80211_CMD_GET_REG,
14089 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14090 		.doit = nl80211_get_reg_do,
14091 		.dumpit = nl80211_get_reg_dump,
14092 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14093 		/* can be retrieved by unprivileged users */
14094 	},
14095 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14096 	{
14097 		.cmd = NL80211_CMD_SET_REG,
14098 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14099 		.doit = nl80211_set_reg,
14100 		.flags = GENL_ADMIN_PERM,
14101 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14102 	},
14103 #endif
14104 	{
14105 		.cmd = NL80211_CMD_REQ_SET_REG,
14106 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14107 		.doit = nl80211_req_set_reg,
14108 		.flags = GENL_ADMIN_PERM,
14109 	},
14110 	{
14111 		.cmd = NL80211_CMD_RELOAD_REGDB,
14112 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14113 		.doit = nl80211_reload_regdb,
14114 		.flags = GENL_ADMIN_PERM,
14115 	},
14116 	{
14117 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
14118 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14119 		.doit = nl80211_get_mesh_config,
14120 		/* can be retrieved by unprivileged users */
14121 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14122 				  NL80211_FLAG_NEED_RTNL,
14123 	},
14124 	{
14125 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
14126 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14127 		.doit = nl80211_update_mesh_config,
14128 		.flags = GENL_UNS_ADMIN_PERM,
14129 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14130 				  NL80211_FLAG_NEED_RTNL,
14131 	},
14132 	{
14133 		.cmd = NL80211_CMD_TRIGGER_SCAN,
14134 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14135 		.doit = nl80211_trigger_scan,
14136 		.flags = GENL_UNS_ADMIN_PERM,
14137 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14138 				  NL80211_FLAG_NEED_RTNL,
14139 	},
14140 	{
14141 		.cmd = NL80211_CMD_ABORT_SCAN,
14142 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14143 		.doit = nl80211_abort_scan,
14144 		.flags = GENL_UNS_ADMIN_PERM,
14145 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14146 				  NL80211_FLAG_NEED_RTNL,
14147 	},
14148 	{
14149 		.cmd = NL80211_CMD_GET_SCAN,
14150 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14151 		.dumpit = nl80211_dump_scan,
14152 	},
14153 	{
14154 		.cmd = NL80211_CMD_START_SCHED_SCAN,
14155 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14156 		.doit = nl80211_start_sched_scan,
14157 		.flags = GENL_UNS_ADMIN_PERM,
14158 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14159 				  NL80211_FLAG_NEED_RTNL,
14160 	},
14161 	{
14162 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
14163 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14164 		.doit = nl80211_stop_sched_scan,
14165 		.flags = GENL_UNS_ADMIN_PERM,
14166 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14167 				  NL80211_FLAG_NEED_RTNL,
14168 	},
14169 	{
14170 		.cmd = NL80211_CMD_AUTHENTICATE,
14171 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14172 		.doit = nl80211_authenticate,
14173 		.flags = GENL_UNS_ADMIN_PERM,
14174 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14175 				  NL80211_FLAG_NEED_RTNL |
14176 				  NL80211_FLAG_CLEAR_SKB,
14177 	},
14178 	{
14179 		.cmd = NL80211_CMD_ASSOCIATE,
14180 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14181 		.doit = nl80211_associate,
14182 		.flags = GENL_UNS_ADMIN_PERM,
14183 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14184 				  NL80211_FLAG_NEED_RTNL |
14185 				  NL80211_FLAG_CLEAR_SKB,
14186 	},
14187 	{
14188 		.cmd = NL80211_CMD_DEAUTHENTICATE,
14189 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14190 		.doit = nl80211_deauthenticate,
14191 		.flags = GENL_UNS_ADMIN_PERM,
14192 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14193 				  NL80211_FLAG_NEED_RTNL,
14194 	},
14195 	{
14196 		.cmd = NL80211_CMD_DISASSOCIATE,
14197 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14198 		.doit = nl80211_disassociate,
14199 		.flags = GENL_UNS_ADMIN_PERM,
14200 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14201 				  NL80211_FLAG_NEED_RTNL,
14202 	},
14203 	{
14204 		.cmd = NL80211_CMD_JOIN_IBSS,
14205 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14206 		.doit = nl80211_join_ibss,
14207 		.flags = GENL_UNS_ADMIN_PERM,
14208 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14209 				  NL80211_FLAG_NEED_RTNL,
14210 	},
14211 	{
14212 		.cmd = NL80211_CMD_LEAVE_IBSS,
14213 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14214 		.doit = nl80211_leave_ibss,
14215 		.flags = GENL_UNS_ADMIN_PERM,
14216 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14217 				  NL80211_FLAG_NEED_RTNL,
14218 	},
14219 #ifdef CONFIG_NL80211_TESTMODE
14220 	{
14221 		.cmd = NL80211_CMD_TESTMODE,
14222 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14223 		.doit = nl80211_testmode_do,
14224 		.dumpit = nl80211_testmode_dump,
14225 		.flags = GENL_UNS_ADMIN_PERM,
14226 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14227 				  NL80211_FLAG_NEED_RTNL,
14228 	},
14229 #endif
14230 	{
14231 		.cmd = NL80211_CMD_CONNECT,
14232 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14233 		.doit = nl80211_connect,
14234 		.flags = GENL_UNS_ADMIN_PERM,
14235 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14236 				  NL80211_FLAG_NEED_RTNL |
14237 				  NL80211_FLAG_CLEAR_SKB,
14238 	},
14239 	{
14240 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14241 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14242 		.doit = nl80211_update_connect_params,
14243 		.flags = GENL_ADMIN_PERM,
14244 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14245 				  NL80211_FLAG_NEED_RTNL |
14246 				  NL80211_FLAG_CLEAR_SKB,
14247 	},
14248 	{
14249 		.cmd = NL80211_CMD_DISCONNECT,
14250 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14251 		.doit = nl80211_disconnect,
14252 		.flags = GENL_UNS_ADMIN_PERM,
14253 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14254 				  NL80211_FLAG_NEED_RTNL,
14255 	},
14256 	{
14257 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
14258 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14259 		.doit = nl80211_wiphy_netns,
14260 		.flags = GENL_UNS_ADMIN_PERM,
14261 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14262 				  NL80211_FLAG_NEED_RTNL,
14263 	},
14264 	{
14265 		.cmd = NL80211_CMD_GET_SURVEY,
14266 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14267 		.dumpit = nl80211_dump_survey,
14268 	},
14269 	{
14270 		.cmd = NL80211_CMD_SET_PMKSA,
14271 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14272 		.doit = nl80211_setdel_pmksa,
14273 		.flags = GENL_UNS_ADMIN_PERM,
14274 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14275 				  NL80211_FLAG_NEED_RTNL |
14276 				  NL80211_FLAG_CLEAR_SKB,
14277 	},
14278 	{
14279 		.cmd = NL80211_CMD_DEL_PMKSA,
14280 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14281 		.doit = nl80211_setdel_pmksa,
14282 		.flags = GENL_UNS_ADMIN_PERM,
14283 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14284 				  NL80211_FLAG_NEED_RTNL,
14285 	},
14286 	{
14287 		.cmd = NL80211_CMD_FLUSH_PMKSA,
14288 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14289 		.doit = nl80211_flush_pmksa,
14290 		.flags = GENL_UNS_ADMIN_PERM,
14291 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14292 				  NL80211_FLAG_NEED_RTNL,
14293 	},
14294 	{
14295 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
14296 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14297 		.doit = nl80211_remain_on_channel,
14298 		.flags = GENL_UNS_ADMIN_PERM,
14299 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14300 				  NL80211_FLAG_NEED_RTNL,
14301 	},
14302 	{
14303 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14304 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14305 		.doit = nl80211_cancel_remain_on_channel,
14306 		.flags = GENL_UNS_ADMIN_PERM,
14307 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14308 				  NL80211_FLAG_NEED_RTNL,
14309 	},
14310 	{
14311 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14312 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14313 		.doit = nl80211_set_tx_bitrate_mask,
14314 		.flags = GENL_UNS_ADMIN_PERM,
14315 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14316 				  NL80211_FLAG_NEED_RTNL,
14317 	},
14318 	{
14319 		.cmd = NL80211_CMD_REGISTER_FRAME,
14320 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14321 		.doit = nl80211_register_mgmt,
14322 		.flags = GENL_UNS_ADMIN_PERM,
14323 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14324 				  NL80211_FLAG_NEED_RTNL,
14325 	},
14326 	{
14327 		.cmd = NL80211_CMD_FRAME,
14328 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14329 		.doit = nl80211_tx_mgmt,
14330 		.flags = GENL_UNS_ADMIN_PERM,
14331 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14332 				  NL80211_FLAG_NEED_RTNL,
14333 	},
14334 	{
14335 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14336 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14337 		.doit = nl80211_tx_mgmt_cancel_wait,
14338 		.flags = GENL_UNS_ADMIN_PERM,
14339 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14340 				  NL80211_FLAG_NEED_RTNL,
14341 	},
14342 	{
14343 		.cmd = NL80211_CMD_SET_POWER_SAVE,
14344 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14345 		.doit = nl80211_set_power_save,
14346 		.flags = GENL_UNS_ADMIN_PERM,
14347 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14348 				  NL80211_FLAG_NEED_RTNL,
14349 	},
14350 	{
14351 		.cmd = NL80211_CMD_GET_POWER_SAVE,
14352 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14353 		.doit = nl80211_get_power_save,
14354 		/* can be retrieved by unprivileged users */
14355 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14356 				  NL80211_FLAG_NEED_RTNL,
14357 	},
14358 	{
14359 		.cmd = NL80211_CMD_SET_CQM,
14360 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14361 		.doit = nl80211_set_cqm,
14362 		.flags = GENL_UNS_ADMIN_PERM,
14363 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14364 				  NL80211_FLAG_NEED_RTNL,
14365 	},
14366 	{
14367 		.cmd = NL80211_CMD_SET_CHANNEL,
14368 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14369 		.doit = nl80211_set_channel,
14370 		.flags = GENL_UNS_ADMIN_PERM,
14371 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14372 				  NL80211_FLAG_NEED_RTNL,
14373 	},
14374 	{
14375 		.cmd = NL80211_CMD_SET_WDS_PEER,
14376 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14377 		.doit = nl80211_set_wds_peer,
14378 		.flags = GENL_UNS_ADMIN_PERM,
14379 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14380 				  NL80211_FLAG_NEED_RTNL,
14381 	},
14382 	{
14383 		.cmd = NL80211_CMD_JOIN_MESH,
14384 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14385 		.doit = nl80211_join_mesh,
14386 		.flags = GENL_UNS_ADMIN_PERM,
14387 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14388 				  NL80211_FLAG_NEED_RTNL,
14389 	},
14390 	{
14391 		.cmd = NL80211_CMD_LEAVE_MESH,
14392 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14393 		.doit = nl80211_leave_mesh,
14394 		.flags = GENL_UNS_ADMIN_PERM,
14395 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14396 				  NL80211_FLAG_NEED_RTNL,
14397 	},
14398 	{
14399 		.cmd = NL80211_CMD_JOIN_OCB,
14400 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14401 		.doit = nl80211_join_ocb,
14402 		.flags = GENL_UNS_ADMIN_PERM,
14403 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14404 				  NL80211_FLAG_NEED_RTNL,
14405 	},
14406 	{
14407 		.cmd = NL80211_CMD_LEAVE_OCB,
14408 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14409 		.doit = nl80211_leave_ocb,
14410 		.flags = GENL_UNS_ADMIN_PERM,
14411 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14412 				  NL80211_FLAG_NEED_RTNL,
14413 	},
14414 #ifdef CONFIG_PM
14415 	{
14416 		.cmd = NL80211_CMD_GET_WOWLAN,
14417 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14418 		.doit = nl80211_get_wowlan,
14419 		/* can be retrieved by unprivileged users */
14420 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14421 				  NL80211_FLAG_NEED_RTNL,
14422 	},
14423 	{
14424 		.cmd = NL80211_CMD_SET_WOWLAN,
14425 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14426 		.doit = nl80211_set_wowlan,
14427 		.flags = GENL_UNS_ADMIN_PERM,
14428 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14429 				  NL80211_FLAG_NEED_RTNL,
14430 	},
14431 #endif
14432 	{
14433 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14434 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14435 		.doit = nl80211_set_rekey_data,
14436 		.flags = GENL_UNS_ADMIN_PERM,
14437 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14438 				  NL80211_FLAG_NEED_RTNL |
14439 				  NL80211_FLAG_CLEAR_SKB,
14440 	},
14441 	{
14442 		.cmd = NL80211_CMD_TDLS_MGMT,
14443 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14444 		.doit = nl80211_tdls_mgmt,
14445 		.flags = GENL_UNS_ADMIN_PERM,
14446 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14447 				  NL80211_FLAG_NEED_RTNL,
14448 	},
14449 	{
14450 		.cmd = NL80211_CMD_TDLS_OPER,
14451 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14452 		.doit = nl80211_tdls_oper,
14453 		.flags = GENL_UNS_ADMIN_PERM,
14454 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14455 				  NL80211_FLAG_NEED_RTNL,
14456 	},
14457 	{
14458 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
14459 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14460 		.doit = nl80211_register_unexpected_frame,
14461 		.flags = GENL_UNS_ADMIN_PERM,
14462 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14463 				  NL80211_FLAG_NEED_RTNL,
14464 	},
14465 	{
14466 		.cmd = NL80211_CMD_PROBE_CLIENT,
14467 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14468 		.doit = nl80211_probe_client,
14469 		.flags = GENL_UNS_ADMIN_PERM,
14470 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14471 				  NL80211_FLAG_NEED_RTNL,
14472 	},
14473 	{
14474 		.cmd = NL80211_CMD_REGISTER_BEACONS,
14475 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14476 		.doit = nl80211_register_beacons,
14477 		.flags = GENL_UNS_ADMIN_PERM,
14478 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14479 				  NL80211_FLAG_NEED_RTNL,
14480 	},
14481 	{
14482 		.cmd = NL80211_CMD_SET_NOACK_MAP,
14483 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14484 		.doit = nl80211_set_noack_map,
14485 		.flags = GENL_UNS_ADMIN_PERM,
14486 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14487 				  NL80211_FLAG_NEED_RTNL,
14488 	},
14489 	{
14490 		.cmd = NL80211_CMD_START_P2P_DEVICE,
14491 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14492 		.doit = nl80211_start_p2p_device,
14493 		.flags = GENL_UNS_ADMIN_PERM,
14494 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14495 				  NL80211_FLAG_NEED_RTNL,
14496 	},
14497 	{
14498 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
14499 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14500 		.doit = nl80211_stop_p2p_device,
14501 		.flags = GENL_UNS_ADMIN_PERM,
14502 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14503 				  NL80211_FLAG_NEED_RTNL,
14504 	},
14505 	{
14506 		.cmd = NL80211_CMD_START_NAN,
14507 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14508 		.doit = nl80211_start_nan,
14509 		.flags = GENL_ADMIN_PERM,
14510 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14511 				  NL80211_FLAG_NEED_RTNL,
14512 	},
14513 	{
14514 		.cmd = NL80211_CMD_STOP_NAN,
14515 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14516 		.doit = nl80211_stop_nan,
14517 		.flags = GENL_ADMIN_PERM,
14518 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14519 				  NL80211_FLAG_NEED_RTNL,
14520 	},
14521 	{
14522 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
14523 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14524 		.doit = nl80211_nan_add_func,
14525 		.flags = GENL_ADMIN_PERM,
14526 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14527 				  NL80211_FLAG_NEED_RTNL,
14528 	},
14529 	{
14530 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
14531 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14532 		.doit = nl80211_nan_del_func,
14533 		.flags = GENL_ADMIN_PERM,
14534 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14535 				  NL80211_FLAG_NEED_RTNL,
14536 	},
14537 	{
14538 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
14539 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14540 		.doit = nl80211_nan_change_config,
14541 		.flags = GENL_ADMIN_PERM,
14542 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14543 				  NL80211_FLAG_NEED_RTNL,
14544 	},
14545 	{
14546 		.cmd = NL80211_CMD_SET_MCAST_RATE,
14547 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14548 		.doit = nl80211_set_mcast_rate,
14549 		.flags = GENL_UNS_ADMIN_PERM,
14550 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14551 				  NL80211_FLAG_NEED_RTNL,
14552 	},
14553 	{
14554 		.cmd = NL80211_CMD_SET_MAC_ACL,
14555 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14556 		.doit = nl80211_set_mac_acl,
14557 		.flags = GENL_UNS_ADMIN_PERM,
14558 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14559 				  NL80211_FLAG_NEED_RTNL,
14560 	},
14561 	{
14562 		.cmd = NL80211_CMD_RADAR_DETECT,
14563 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14564 		.doit = nl80211_start_radar_detection,
14565 		.flags = GENL_UNS_ADMIN_PERM,
14566 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14567 				  NL80211_FLAG_NEED_RTNL,
14568 	},
14569 	{
14570 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
14571 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14572 		.doit = nl80211_get_protocol_features,
14573 	},
14574 	{
14575 		.cmd = NL80211_CMD_UPDATE_FT_IES,
14576 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14577 		.doit = nl80211_update_ft_ies,
14578 		.flags = GENL_UNS_ADMIN_PERM,
14579 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14580 				  NL80211_FLAG_NEED_RTNL,
14581 	},
14582 	{
14583 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
14584 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14585 		.doit = nl80211_crit_protocol_start,
14586 		.flags = GENL_UNS_ADMIN_PERM,
14587 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14588 				  NL80211_FLAG_NEED_RTNL,
14589 	},
14590 	{
14591 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
14592 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14593 		.doit = nl80211_crit_protocol_stop,
14594 		.flags = GENL_UNS_ADMIN_PERM,
14595 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14596 				  NL80211_FLAG_NEED_RTNL,
14597 	},
14598 	{
14599 		.cmd = NL80211_CMD_GET_COALESCE,
14600 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14601 		.doit = nl80211_get_coalesce,
14602 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14603 				  NL80211_FLAG_NEED_RTNL,
14604 	},
14605 	{
14606 		.cmd = NL80211_CMD_SET_COALESCE,
14607 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14608 		.doit = nl80211_set_coalesce,
14609 		.flags = GENL_UNS_ADMIN_PERM,
14610 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14611 				  NL80211_FLAG_NEED_RTNL,
14612 	},
14613 	{
14614 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
14615 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14616 		.doit = nl80211_channel_switch,
14617 		.flags = GENL_UNS_ADMIN_PERM,
14618 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14619 				  NL80211_FLAG_NEED_RTNL,
14620 	},
14621 	{
14622 		.cmd = NL80211_CMD_VENDOR,
14623 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14624 		.doit = nl80211_vendor_cmd,
14625 		.dumpit = nl80211_vendor_cmd_dump,
14626 		.flags = GENL_UNS_ADMIN_PERM,
14627 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14628 				  NL80211_FLAG_NEED_RTNL |
14629 				  NL80211_FLAG_CLEAR_SKB,
14630 	},
14631 	{
14632 		.cmd = NL80211_CMD_SET_QOS_MAP,
14633 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14634 		.doit = nl80211_set_qos_map,
14635 		.flags = GENL_UNS_ADMIN_PERM,
14636 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14637 				  NL80211_FLAG_NEED_RTNL,
14638 	},
14639 	{
14640 		.cmd = NL80211_CMD_ADD_TX_TS,
14641 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14642 		.doit = nl80211_add_tx_ts,
14643 		.flags = GENL_UNS_ADMIN_PERM,
14644 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14645 				  NL80211_FLAG_NEED_RTNL,
14646 	},
14647 	{
14648 		.cmd = NL80211_CMD_DEL_TX_TS,
14649 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14650 		.doit = nl80211_del_tx_ts,
14651 		.flags = GENL_UNS_ADMIN_PERM,
14652 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14653 				  NL80211_FLAG_NEED_RTNL,
14654 	},
14655 	{
14656 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
14657 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14658 		.doit = nl80211_tdls_channel_switch,
14659 		.flags = GENL_UNS_ADMIN_PERM,
14660 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14661 				  NL80211_FLAG_NEED_RTNL,
14662 	},
14663 	{
14664 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
14665 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14666 		.doit = nl80211_tdls_cancel_channel_switch,
14667 		.flags = GENL_UNS_ADMIN_PERM,
14668 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14669 				  NL80211_FLAG_NEED_RTNL,
14670 	},
14671 	{
14672 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
14673 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14674 		.doit = nl80211_set_multicast_to_unicast,
14675 		.flags = GENL_UNS_ADMIN_PERM,
14676 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14677 				  NL80211_FLAG_NEED_RTNL,
14678 	},
14679 	{
14680 		.cmd = NL80211_CMD_SET_PMK,
14681 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14682 		.doit = nl80211_set_pmk,
14683 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14684 				  NL80211_FLAG_NEED_RTNL |
14685 				  NL80211_FLAG_CLEAR_SKB,
14686 	},
14687 	{
14688 		.cmd = NL80211_CMD_DEL_PMK,
14689 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14690 		.doit = nl80211_del_pmk,
14691 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14692 				  NL80211_FLAG_NEED_RTNL,
14693 	},
14694 	{
14695 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
14696 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14697 		.doit = nl80211_external_auth,
14698 		.flags = GENL_ADMIN_PERM,
14699 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14700 				  NL80211_FLAG_NEED_RTNL,
14701 	},
14702 	{
14703 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
14704 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14705 		.doit = nl80211_tx_control_port,
14706 		.flags = GENL_UNS_ADMIN_PERM,
14707 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14708 				  NL80211_FLAG_NEED_RTNL,
14709 	},
14710 	{
14711 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
14712 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14713 		.doit = nl80211_get_ftm_responder_stats,
14714 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14715 				  NL80211_FLAG_NEED_RTNL,
14716 	},
14717 	{
14718 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
14719 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14720 		.doit = nl80211_pmsr_start,
14721 		.flags = GENL_UNS_ADMIN_PERM,
14722 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14723 				  NL80211_FLAG_NEED_RTNL,
14724 	},
14725 	{
14726 		.cmd = NL80211_CMD_NOTIFY_RADAR,
14727 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14728 		.doit = nl80211_notify_radar_detection,
14729 		.flags = GENL_UNS_ADMIN_PERM,
14730 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14731 				  NL80211_FLAG_NEED_RTNL,
14732 	},
14733 	{
14734 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
14735 		.doit = nl80211_update_owe_info,
14736 		.flags = GENL_ADMIN_PERM,
14737 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14738 				  NL80211_FLAG_NEED_RTNL,
14739 	},
14740 	{
14741 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
14742 		.doit = nl80211_probe_mesh_link,
14743 		.flags = GENL_UNS_ADMIN_PERM,
14744 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14745 				  NL80211_FLAG_NEED_RTNL,
14746 	},
14747 };
14748 
14749 static struct genl_family nl80211_fam __ro_after_init = {
14750 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
14751 	.hdrsize = 0,			/* no private header */
14752 	.version = 1,			/* no particular meaning now */
14753 	.maxattr = NL80211_ATTR_MAX,
14754 	.policy = nl80211_policy,
14755 	.netnsok = true,
14756 	.pre_doit = nl80211_pre_doit,
14757 	.post_doit = nl80211_post_doit,
14758 	.module = THIS_MODULE,
14759 	.ops = nl80211_ops,
14760 	.n_ops = ARRAY_SIZE(nl80211_ops),
14761 	.mcgrps = nl80211_mcgrps,
14762 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
14763 	.parallel_ops = true,
14764 };
14765 
14766 /* notification functions */
14767 
nl80211_notify_wiphy(struct cfg80211_registered_device * rdev,enum nl80211_commands cmd)14768 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
14769 			  enum nl80211_commands cmd)
14770 {
14771 	struct sk_buff *msg;
14772 	struct nl80211_dump_wiphy_state state = {};
14773 
14774 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14775 		cmd != NL80211_CMD_DEL_WIPHY);
14776 
14777 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14778 	if (!msg)
14779 		return;
14780 
14781 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14782 		nlmsg_free(msg);
14783 		return;
14784 	}
14785 
14786 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14787 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14788 }
14789 
nl80211_notify_iface(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,enum nl80211_commands cmd)14790 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14791 				struct wireless_dev *wdev,
14792 				enum nl80211_commands cmd)
14793 {
14794 	struct sk_buff *msg;
14795 
14796 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14797 	if (!msg)
14798 		return;
14799 
14800 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
14801 		nlmsg_free(msg);
14802 		return;
14803 	}
14804 
14805 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14806 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
14807 }
14808 
nl80211_add_scan_req(struct sk_buff * msg,struct cfg80211_registered_device * rdev)14809 static int nl80211_add_scan_req(struct sk_buff *msg,
14810 				struct cfg80211_registered_device *rdev)
14811 {
14812 	struct cfg80211_scan_request *req = rdev->scan_req;
14813 	struct nlattr *nest;
14814 	int i;
14815 
14816 	if (WARN_ON(!req))
14817 		return 0;
14818 
14819 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
14820 	if (!nest)
14821 		goto nla_put_failure;
14822 	for (i = 0; i < req->n_ssids; i++) {
14823 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14824 			goto nla_put_failure;
14825 	}
14826 	nla_nest_end(msg, nest);
14827 
14828 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14829 	if (!nest)
14830 		goto nla_put_failure;
14831 	for (i = 0; i < req->n_channels; i++) {
14832 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14833 			goto nla_put_failure;
14834 	}
14835 	nla_nest_end(msg, nest);
14836 
14837 	if (req->ie &&
14838 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14839 		goto nla_put_failure;
14840 
14841 	if (req->flags &&
14842 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14843 		goto nla_put_failure;
14844 
14845 	if (req->info.scan_start_tsf &&
14846 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14847 			       req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14848 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14849 		     req->info.tsf_bssid)))
14850 		goto nla_put_failure;
14851 
14852 	return 0;
14853  nla_put_failure:
14854 	return -ENOBUFS;
14855 }
14856 
nl80211_prep_scan_msg(struct sk_buff * msg,struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,u32 portid,u32 seq,int flags,u32 cmd)14857 static int nl80211_prep_scan_msg(struct sk_buff *msg,
14858 				 struct cfg80211_registered_device *rdev,
14859 				 struct wireless_dev *wdev,
14860 				 u32 portid, u32 seq, int flags,
14861 				 u32 cmd)
14862 {
14863 	void *hdr;
14864 
14865 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14866 	if (!hdr)
14867 		return -1;
14868 
14869 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14870 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14871 					 wdev->netdev->ifindex)) ||
14872 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14873 			      NL80211_ATTR_PAD))
14874 		goto nla_put_failure;
14875 
14876 	/* ignore errors and send incomplete event anyway */
14877 	nl80211_add_scan_req(msg, rdev);
14878 
14879 	genlmsg_end(msg, hdr);
14880 	return 0;
14881 
14882  nla_put_failure:
14883 	genlmsg_cancel(msg, hdr);
14884 	return -EMSGSIZE;
14885 }
14886 
14887 static int
nl80211_prep_sched_scan_msg(struct sk_buff * msg,struct cfg80211_sched_scan_request * req,u32 cmd)14888 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14889 			    struct cfg80211_sched_scan_request *req, u32 cmd)
14890 {
14891 	void *hdr;
14892 
14893 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14894 	if (!hdr)
14895 		return -1;
14896 
14897 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14898 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14899 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14900 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14901 			      NL80211_ATTR_PAD))
14902 		goto nla_put_failure;
14903 
14904 	genlmsg_end(msg, hdr);
14905 	return 0;
14906 
14907  nla_put_failure:
14908 	genlmsg_cancel(msg, hdr);
14909 	return -EMSGSIZE;
14910 }
14911 
nl80211_send_scan_start(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)14912 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14913 			     struct wireless_dev *wdev)
14914 {
14915 	struct sk_buff *msg;
14916 
14917 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14918 	if (!msg)
14919 		return;
14920 
14921 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14922 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
14923 		nlmsg_free(msg);
14924 		return;
14925 	}
14926 
14927 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14928 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14929 }
14930 
nl80211_build_scan_msg(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,bool aborted)14931 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14932 				       struct wireless_dev *wdev, bool aborted)
14933 {
14934 	struct sk_buff *msg;
14935 
14936 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14937 	if (!msg)
14938 		return NULL;
14939 
14940 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14941 				  aborted ? NL80211_CMD_SCAN_ABORTED :
14942 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14943 		nlmsg_free(msg);
14944 		return NULL;
14945 	}
14946 
14947 	return msg;
14948 }
14949 
14950 /* send message created by nl80211_build_scan_msg() */
nl80211_send_scan_msg(struct cfg80211_registered_device * rdev,struct sk_buff * msg)14951 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14952 			   struct sk_buff *msg)
14953 {
14954 	if (!msg)
14955 		return;
14956 
14957 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14958 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14959 }
14960 
nl80211_send_sched_scan(struct cfg80211_sched_scan_request * req,u32 cmd)14961 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14962 {
14963 	struct sk_buff *msg;
14964 
14965 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14966 	if (!msg)
14967 		return;
14968 
14969 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14970 		nlmsg_free(msg);
14971 		return;
14972 	}
14973 
14974 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14975 				NL80211_MCGRP_SCAN, GFP_KERNEL);
14976 }
14977 
nl80211_reg_change_event_fill(struct sk_buff * msg,struct regulatory_request * request)14978 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14979 					  struct regulatory_request *request)
14980 {
14981 	/* Userspace can always count this one always being set */
14982 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14983 		goto nla_put_failure;
14984 
14985 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14986 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14987 			       NL80211_REGDOM_TYPE_WORLD))
14988 			goto nla_put_failure;
14989 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
14990 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14991 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
14992 			goto nla_put_failure;
14993 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
14994 		   request->intersect) {
14995 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14996 			       NL80211_REGDOM_TYPE_INTERSECTION))
14997 			goto nla_put_failure;
14998 	} else {
14999 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15000 			       NL80211_REGDOM_TYPE_COUNTRY) ||
15001 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15002 				   request->alpha2))
15003 			goto nla_put_failure;
15004 	}
15005 
15006 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15007 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15008 
15009 		if (wiphy &&
15010 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15011 			goto nla_put_failure;
15012 
15013 		if (wiphy &&
15014 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15015 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15016 			goto nla_put_failure;
15017 	}
15018 
15019 	return true;
15020 
15021 nla_put_failure:
15022 	return false;
15023 }
15024 
15025 /*
15026  * This can happen on global regulatory changes or device specific settings
15027  * based on custom regulatory domains.
15028  */
nl80211_common_reg_change_event(enum nl80211_commands cmd_id,struct regulatory_request * request)15029 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15030 				     struct regulatory_request *request)
15031 {
15032 	struct sk_buff *msg;
15033 	void *hdr;
15034 
15035 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15036 	if (!msg)
15037 		return;
15038 
15039 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15040 	if (!hdr)
15041 		goto nla_put_failure;
15042 
15043 	if (!nl80211_reg_change_event_fill(msg, request))
15044 		goto nla_put_failure;
15045 
15046 	genlmsg_end(msg, hdr);
15047 
15048 	rcu_read_lock();
15049 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15050 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15051 	rcu_read_unlock();
15052 
15053 	return;
15054 
15055 nla_put_failure:
15056 	nlmsg_free(msg);
15057 }
15058 
nl80211_send_mlme_event(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,enum nl80211_commands cmd,gfp_t gfp,int uapsd_queues,const u8 * req_ies,size_t req_ies_len)15059 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15060 				    struct net_device *netdev,
15061 				    const u8 *buf, size_t len,
15062 				    enum nl80211_commands cmd, gfp_t gfp,
15063 				    int uapsd_queues, const u8 *req_ies,
15064 				    size_t req_ies_len)
15065 {
15066 	struct sk_buff *msg;
15067 	void *hdr;
15068 
15069 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
15070 	if (!msg)
15071 		return;
15072 
15073 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15074 	if (!hdr) {
15075 		nlmsg_free(msg);
15076 		return;
15077 	}
15078 
15079 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15080 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15081 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15082 	    (req_ies &&
15083 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15084 		goto nla_put_failure;
15085 
15086 	if (uapsd_queues >= 0) {
15087 		struct nlattr *nla_wmm =
15088 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15089 		if (!nla_wmm)
15090 			goto nla_put_failure;
15091 
15092 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15093 			       uapsd_queues))
15094 			goto nla_put_failure;
15095 
15096 		nla_nest_end(msg, nla_wmm);
15097 	}
15098 
15099 	genlmsg_end(msg, hdr);
15100 
15101 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15102 				NL80211_MCGRP_MLME, gfp);
15103 	return;
15104 
15105  nla_put_failure:
15106 	nlmsg_free(msg);
15107 }
15108 
nl80211_send_rx_auth(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15109 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15110 			  struct net_device *netdev, const u8 *buf,
15111 			  size_t len, gfp_t gfp)
15112 {
15113 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15114 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15115 }
15116 
nl80211_send_rx_assoc(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp,int uapsd_queues,const u8 * req_ies,size_t req_ies_len)15117 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15118 			   struct net_device *netdev, const u8 *buf,
15119 			   size_t len, gfp_t gfp, int uapsd_queues,
15120 			   const u8 *req_ies, size_t req_ies_len)
15121 {
15122 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15123 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15124 				req_ies, req_ies_len);
15125 }
15126 
nl80211_send_deauth(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15127 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15128 			 struct net_device *netdev, const u8 *buf,
15129 			 size_t len, gfp_t gfp)
15130 {
15131 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15132 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15133 }
15134 
nl80211_send_disassoc(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15135 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15136 			   struct net_device *netdev, const u8 *buf,
15137 			   size_t len, gfp_t gfp)
15138 {
15139 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15140 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15141 }
15142 
cfg80211_rx_unprot_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len)15143 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15144 				  size_t len)
15145 {
15146 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15147 	struct wiphy *wiphy = wdev->wiphy;
15148 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15149 	const struct ieee80211_mgmt *mgmt = (void *)buf;
15150 	u32 cmd;
15151 
15152 	if (WARN_ON(len < 2))
15153 		return;
15154 
15155 	if (ieee80211_is_deauth(mgmt->frame_control))
15156 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15157 	else
15158 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15159 
15160 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15161 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15162 				NULL, 0);
15163 }
15164 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15165 
nl80211_send_mlme_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,int cmd,const u8 * addr,gfp_t gfp)15166 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15167 				      struct net_device *netdev, int cmd,
15168 				      const u8 *addr, gfp_t gfp)
15169 {
15170 	struct sk_buff *msg;
15171 	void *hdr;
15172 
15173 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15174 	if (!msg)
15175 		return;
15176 
15177 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15178 	if (!hdr) {
15179 		nlmsg_free(msg);
15180 		return;
15181 	}
15182 
15183 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15184 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15185 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15186 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15187 		goto nla_put_failure;
15188 
15189 	genlmsg_end(msg, hdr);
15190 
15191 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15192 				NL80211_MCGRP_MLME, gfp);
15193 	return;
15194 
15195  nla_put_failure:
15196 	nlmsg_free(msg);
15197 }
15198 
nl80211_send_auth_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * addr,gfp_t gfp)15199 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15200 			       struct net_device *netdev, const u8 *addr,
15201 			       gfp_t gfp)
15202 {
15203 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15204 				  addr, gfp);
15205 }
15206 
nl80211_send_assoc_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * addr,gfp_t gfp)15207 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15208 				struct net_device *netdev, const u8 *addr,
15209 				gfp_t gfp)
15210 {
15211 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15212 				  addr, gfp);
15213 }
15214 
nl80211_send_connect_result(struct cfg80211_registered_device * rdev,struct net_device * netdev,struct cfg80211_connect_resp_params * cr,gfp_t gfp)15215 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15216 				 struct net_device *netdev,
15217 				 struct cfg80211_connect_resp_params *cr,
15218 				 gfp_t gfp)
15219 {
15220 	struct sk_buff *msg;
15221 	void *hdr;
15222 
15223 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15224 			cr->fils.kek_len + cr->fils.pmk_len +
15225 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15226 	if (!msg)
15227 		return;
15228 
15229 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15230 	if (!hdr) {
15231 		nlmsg_free(msg);
15232 		return;
15233 	}
15234 
15235 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15236 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15237 	    (cr->bssid &&
15238 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15239 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15240 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15241 			cr->status) ||
15242 	    (cr->status < 0 &&
15243 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15244 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15245 			  cr->timeout_reason))) ||
15246 	    (cr->req_ie &&
15247 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15248 	    (cr->resp_ie &&
15249 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15250 		     cr->resp_ie)) ||
15251 	    (cr->fils.update_erp_next_seq_num &&
15252 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15253 			 cr->fils.erp_next_seq_num)) ||
15254 	    (cr->status == WLAN_STATUS_SUCCESS &&
15255 	     ((cr->fils.kek &&
15256 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15257 		       cr->fils.kek)) ||
15258 	      (cr->fils.pmk &&
15259 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15260 	      (cr->fils.pmkid &&
15261 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15262 		goto nla_put_failure;
15263 
15264 	genlmsg_end(msg, hdr);
15265 
15266 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15267 				NL80211_MCGRP_MLME, gfp);
15268 	return;
15269 
15270  nla_put_failure:
15271 	nlmsg_free(msg);
15272 }
15273 
nl80211_send_roamed(struct cfg80211_registered_device * rdev,struct net_device * netdev,struct cfg80211_roam_info * info,gfp_t gfp)15274 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15275 			 struct net_device *netdev,
15276 			 struct cfg80211_roam_info *info, gfp_t gfp)
15277 {
15278 	struct sk_buff *msg;
15279 	void *hdr;
15280 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15281 
15282 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15283 			info->fils.kek_len + info->fils.pmk_len +
15284 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15285 	if (!msg)
15286 		return;
15287 
15288 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15289 	if (!hdr) {
15290 		nlmsg_free(msg);
15291 		return;
15292 	}
15293 
15294 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15295 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15296 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15297 	    (info->req_ie &&
15298 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15299 		     info->req_ie)) ||
15300 	    (info->resp_ie &&
15301 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15302 		     info->resp_ie)) ||
15303 	    (info->fils.update_erp_next_seq_num &&
15304 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15305 			 info->fils.erp_next_seq_num)) ||
15306 	    (info->fils.kek &&
15307 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15308 		     info->fils.kek)) ||
15309 	    (info->fils.pmk &&
15310 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15311 	    (info->fils.pmkid &&
15312 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15313 		goto nla_put_failure;
15314 
15315 	genlmsg_end(msg, hdr);
15316 
15317 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15318 				NL80211_MCGRP_MLME, gfp);
15319 	return;
15320 
15321  nla_put_failure:
15322 	nlmsg_free(msg);
15323 }
15324 
nl80211_send_port_authorized(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid)15325 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15326 				  struct net_device *netdev, const u8 *bssid)
15327 {
15328 	struct sk_buff *msg;
15329 	void *hdr;
15330 
15331 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15332 	if (!msg)
15333 		return;
15334 
15335 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15336 	if (!hdr) {
15337 		nlmsg_free(msg);
15338 		return;
15339 	}
15340 
15341 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15342 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15343 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15344 		goto nla_put_failure;
15345 
15346 	genlmsg_end(msg, hdr);
15347 
15348 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15349 				NL80211_MCGRP_MLME, GFP_KERNEL);
15350 	return;
15351 
15352  nla_put_failure:
15353 	nlmsg_free(msg);
15354 }
15355 
nl80211_send_disconnected(struct cfg80211_registered_device * rdev,struct net_device * netdev,u16 reason,const u8 * ie,size_t ie_len,bool from_ap)15356 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15357 			       struct net_device *netdev, u16 reason,
15358 			       const u8 *ie, size_t ie_len, bool from_ap)
15359 {
15360 	struct sk_buff *msg;
15361 	void *hdr;
15362 
15363 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15364 	if (!msg)
15365 		return;
15366 
15367 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15368 	if (!hdr) {
15369 		nlmsg_free(msg);
15370 		return;
15371 	}
15372 
15373 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15374 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15375 	    (reason &&
15376 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15377 	    (from_ap &&
15378 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15379 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15380 		goto nla_put_failure;
15381 
15382 	genlmsg_end(msg, hdr);
15383 
15384 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15385 				NL80211_MCGRP_MLME, GFP_KERNEL);
15386 	return;
15387 
15388  nla_put_failure:
15389 	nlmsg_free(msg);
15390 }
15391 
nl80211_send_ibss_bssid(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid,gfp_t gfp)15392 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15393 			     struct net_device *netdev, const u8 *bssid,
15394 			     gfp_t gfp)
15395 {
15396 	struct sk_buff *msg;
15397 	void *hdr;
15398 
15399 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15400 	if (!msg)
15401 		return;
15402 
15403 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15404 	if (!hdr) {
15405 		nlmsg_free(msg);
15406 		return;
15407 	}
15408 
15409 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15410 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15411 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15412 		goto nla_put_failure;
15413 
15414 	genlmsg_end(msg, hdr);
15415 
15416 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15417 				NL80211_MCGRP_MLME, gfp);
15418 	return;
15419 
15420  nla_put_failure:
15421 	nlmsg_free(msg);
15422 }
15423 
cfg80211_notify_new_peer_candidate(struct net_device * dev,const u8 * addr,const u8 * ie,u8 ie_len,int sig_dbm,gfp_t gfp)15424 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15425 					const u8 *ie, u8 ie_len,
15426 					int sig_dbm, gfp_t gfp)
15427 {
15428 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15429 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15430 	struct sk_buff *msg;
15431 	void *hdr;
15432 
15433 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15434 		return;
15435 
15436 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
15437 
15438 	msg = nlmsg_new(100 + ie_len, gfp);
15439 	if (!msg)
15440 		return;
15441 
15442 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15443 	if (!hdr) {
15444 		nlmsg_free(msg);
15445 		return;
15446 	}
15447 
15448 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15449 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15450 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15451 	    (ie_len && ie &&
15452 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15453 	    (sig_dbm &&
15454 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15455 		goto nla_put_failure;
15456 
15457 	genlmsg_end(msg, hdr);
15458 
15459 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15460 				NL80211_MCGRP_MLME, gfp);
15461 	return;
15462 
15463  nla_put_failure:
15464 	nlmsg_free(msg);
15465 }
15466 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15467 
nl80211_michael_mic_failure(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * addr,enum nl80211_key_type key_type,int key_id,const u8 * tsc,gfp_t gfp)15468 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15469 				 struct net_device *netdev, const u8 *addr,
15470 				 enum nl80211_key_type key_type, int key_id,
15471 				 const u8 *tsc, gfp_t gfp)
15472 {
15473 	struct sk_buff *msg;
15474 	void *hdr;
15475 
15476 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15477 	if (!msg)
15478 		return;
15479 
15480 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15481 	if (!hdr) {
15482 		nlmsg_free(msg);
15483 		return;
15484 	}
15485 
15486 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15487 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15488 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
15489 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
15490 	    (key_id != -1 &&
15491 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
15492 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
15493 		goto nla_put_failure;
15494 
15495 	genlmsg_end(msg, hdr);
15496 
15497 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15498 				NL80211_MCGRP_MLME, gfp);
15499 	return;
15500 
15501  nla_put_failure:
15502 	nlmsg_free(msg);
15503 }
15504 
nl80211_send_beacon_hint_event(struct wiphy * wiphy,struct ieee80211_channel * channel_before,struct ieee80211_channel * channel_after)15505 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
15506 				    struct ieee80211_channel *channel_before,
15507 				    struct ieee80211_channel *channel_after)
15508 {
15509 	struct sk_buff *msg;
15510 	void *hdr;
15511 	struct nlattr *nl_freq;
15512 
15513 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
15514 	if (!msg)
15515 		return;
15516 
15517 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
15518 	if (!hdr) {
15519 		nlmsg_free(msg);
15520 		return;
15521 	}
15522 
15523 	/*
15524 	 * Since we are applying the beacon hint to a wiphy we know its
15525 	 * wiphy_idx is valid
15526 	 */
15527 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
15528 		goto nla_put_failure;
15529 
15530 	/* Before */
15531 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
15532 	if (!nl_freq)
15533 		goto nla_put_failure;
15534 
15535 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
15536 		goto nla_put_failure;
15537 	nla_nest_end(msg, nl_freq);
15538 
15539 	/* After */
15540 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
15541 	if (!nl_freq)
15542 		goto nla_put_failure;
15543 
15544 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
15545 		goto nla_put_failure;
15546 	nla_nest_end(msg, nl_freq);
15547 
15548 	genlmsg_end(msg, hdr);
15549 
15550 	rcu_read_lock();
15551 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15552 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15553 	rcu_read_unlock();
15554 
15555 	return;
15556 
15557 nla_put_failure:
15558 	nlmsg_free(msg);
15559 }
15560 
nl80211_send_remain_on_chan_event(int cmd,struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,unsigned int duration,gfp_t gfp)15561 static void nl80211_send_remain_on_chan_event(
15562 	int cmd, struct cfg80211_registered_device *rdev,
15563 	struct wireless_dev *wdev, u64 cookie,
15564 	struct ieee80211_channel *chan,
15565 	unsigned int duration, gfp_t gfp)
15566 {
15567 	struct sk_buff *msg;
15568 	void *hdr;
15569 
15570 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15571 	if (!msg)
15572 		return;
15573 
15574 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15575 	if (!hdr) {
15576 		nlmsg_free(msg);
15577 		return;
15578 	}
15579 
15580 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15581 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15582 					 wdev->netdev->ifindex)) ||
15583 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15584 			      NL80211_ATTR_PAD) ||
15585 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
15586 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
15587 			NL80211_CHAN_NO_HT) ||
15588 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15589 			      NL80211_ATTR_PAD))
15590 		goto nla_put_failure;
15591 
15592 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
15593 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
15594 		goto nla_put_failure;
15595 
15596 	genlmsg_end(msg, hdr);
15597 
15598 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15599 				NL80211_MCGRP_MLME, gfp);
15600 	return;
15601 
15602  nla_put_failure:
15603 	nlmsg_free(msg);
15604 }
15605 
cfg80211_ready_on_channel(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,unsigned int duration,gfp_t gfp)15606 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
15607 			       struct ieee80211_channel *chan,
15608 			       unsigned int duration, gfp_t gfp)
15609 {
15610 	struct wiphy *wiphy = wdev->wiphy;
15611 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15612 
15613 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
15614 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
15615 					  rdev, wdev, cookie, chan,
15616 					  duration, gfp);
15617 }
15618 EXPORT_SYMBOL(cfg80211_ready_on_channel);
15619 
cfg80211_remain_on_channel_expired(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,gfp_t gfp)15620 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
15621 					struct ieee80211_channel *chan,
15622 					gfp_t gfp)
15623 {
15624 	struct wiphy *wiphy = wdev->wiphy;
15625 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15626 
15627 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
15628 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15629 					  rdev, wdev, cookie, chan, 0, gfp);
15630 }
15631 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
15632 
cfg80211_tx_mgmt_expired(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,gfp_t gfp)15633 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
15634 					struct ieee80211_channel *chan,
15635 					gfp_t gfp)
15636 {
15637 	struct wiphy *wiphy = wdev->wiphy;
15638 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15639 
15640 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
15641 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
15642 					  rdev, wdev, cookie, chan, 0, gfp);
15643 }
15644 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
15645 
cfg80211_new_sta(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo,gfp_t gfp)15646 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
15647 		      struct station_info *sinfo, gfp_t gfp)
15648 {
15649 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15650 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15651 	struct sk_buff *msg;
15652 
15653 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
15654 
15655 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15656 	if (!msg)
15657 		return;
15658 
15659 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
15660 				 rdev, dev, mac_addr, sinfo) < 0) {
15661 		nlmsg_free(msg);
15662 		return;
15663 	}
15664 
15665 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15666 				NL80211_MCGRP_MLME, gfp);
15667 }
15668 EXPORT_SYMBOL(cfg80211_new_sta);
15669 
cfg80211_del_sta_sinfo(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo,gfp_t gfp)15670 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
15671 			    struct station_info *sinfo, gfp_t gfp)
15672 {
15673 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15674 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15675 	struct sk_buff *msg;
15676 	struct station_info empty_sinfo = {};
15677 
15678 	if (!sinfo)
15679 		sinfo = &empty_sinfo;
15680 
15681 	trace_cfg80211_del_sta(dev, mac_addr);
15682 
15683 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15684 	if (!msg) {
15685 		cfg80211_sinfo_release_content(sinfo);
15686 		return;
15687 	}
15688 
15689 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
15690 				 rdev, dev, mac_addr, sinfo) < 0) {
15691 		nlmsg_free(msg);
15692 		return;
15693 	}
15694 
15695 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15696 				NL80211_MCGRP_MLME, gfp);
15697 }
15698 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
15699 
cfg80211_conn_failed(struct net_device * dev,const u8 * mac_addr,enum nl80211_connect_failed_reason reason,gfp_t gfp)15700 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
15701 			  enum nl80211_connect_failed_reason reason,
15702 			  gfp_t gfp)
15703 {
15704 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15705 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15706 	struct sk_buff *msg;
15707 	void *hdr;
15708 
15709 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
15710 	if (!msg)
15711 		return;
15712 
15713 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
15714 	if (!hdr) {
15715 		nlmsg_free(msg);
15716 		return;
15717 	}
15718 
15719 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15720 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
15721 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
15722 		goto nla_put_failure;
15723 
15724 	genlmsg_end(msg, hdr);
15725 
15726 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15727 				NL80211_MCGRP_MLME, gfp);
15728 	return;
15729 
15730  nla_put_failure:
15731 	nlmsg_free(msg);
15732 }
15733 EXPORT_SYMBOL(cfg80211_conn_failed);
15734 
__nl80211_unexpected_frame(struct net_device * dev,u8 cmd,const u8 * addr,gfp_t gfp)15735 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
15736 				       const u8 *addr, gfp_t gfp)
15737 {
15738 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15739 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15740 	struct sk_buff *msg;
15741 	void *hdr;
15742 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
15743 
15744 	if (!nlportid)
15745 		return false;
15746 
15747 	msg = nlmsg_new(100, gfp);
15748 	if (!msg)
15749 		return true;
15750 
15751 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15752 	if (!hdr) {
15753 		nlmsg_free(msg);
15754 		return true;
15755 	}
15756 
15757 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15758 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15759 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15760 		goto nla_put_failure;
15761 
15762 	genlmsg_end(msg, hdr);
15763 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15764 	return true;
15765 
15766  nla_put_failure:
15767 	nlmsg_free(msg);
15768 	return true;
15769 }
15770 
cfg80211_rx_spurious_frame(struct net_device * dev,const u8 * addr,gfp_t gfp)15771 bool cfg80211_rx_spurious_frame(struct net_device *dev,
15772 				const u8 *addr, gfp_t gfp)
15773 {
15774 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15775 	bool ret;
15776 
15777 	trace_cfg80211_rx_spurious_frame(dev, addr);
15778 
15779 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15780 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
15781 		trace_cfg80211_return_bool(false);
15782 		return false;
15783 	}
15784 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
15785 					 addr, gfp);
15786 	trace_cfg80211_return_bool(ret);
15787 	return ret;
15788 }
15789 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15790 
cfg80211_rx_unexpected_4addr_frame(struct net_device * dev,const u8 * addr,gfp_t gfp)15791 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15792 					const u8 *addr, gfp_t gfp)
15793 {
15794 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15795 	bool ret;
15796 
15797 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15798 
15799 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15800 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15801 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15802 		trace_cfg80211_return_bool(false);
15803 		return false;
15804 	}
15805 	ret = __nl80211_unexpected_frame(dev,
15806 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15807 					 addr, gfp);
15808 	trace_cfg80211_return_bool(ret);
15809 	return ret;
15810 }
15811 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15812 
nl80211_send_mgmt(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,u32 nlportid,int freq,int sig_dbm,const u8 * buf,size_t len,u32 flags,gfp_t gfp)15813 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15814 		      struct wireless_dev *wdev, u32 nlportid,
15815 		      int freq, int sig_dbm,
15816 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15817 {
15818 	struct net_device *netdev = wdev->netdev;
15819 	struct sk_buff *msg;
15820 	void *hdr;
15821 
15822 	msg = nlmsg_new(100 + len, gfp);
15823 	if (!msg)
15824 		return -ENOMEM;
15825 
15826 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15827 	if (!hdr) {
15828 		nlmsg_free(msg);
15829 		return -ENOMEM;
15830 	}
15831 
15832 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15833 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15834 					netdev->ifindex)) ||
15835 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15836 			      NL80211_ATTR_PAD) ||
15837 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15838 	    (sig_dbm &&
15839 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15840 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15841 	    (flags &&
15842 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15843 		goto nla_put_failure;
15844 
15845 	genlmsg_end(msg, hdr);
15846 
15847 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15848 
15849  nla_put_failure:
15850 	nlmsg_free(msg);
15851 	return -ENOBUFS;
15852 }
15853 
cfg80211_mgmt_tx_status(struct wireless_dev * wdev,u64 cookie,const u8 * buf,size_t len,bool ack,gfp_t gfp)15854 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15855 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
15856 {
15857 	struct wiphy *wiphy = wdev->wiphy;
15858 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15859 	struct net_device *netdev = wdev->netdev;
15860 	struct sk_buff *msg;
15861 	void *hdr;
15862 
15863 	trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15864 
15865 	msg = nlmsg_new(100 + len, gfp);
15866 	if (!msg)
15867 		return;
15868 
15869 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15870 	if (!hdr) {
15871 		nlmsg_free(msg);
15872 		return;
15873 	}
15874 
15875 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15876 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15877 				   netdev->ifindex)) ||
15878 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15879 			      NL80211_ATTR_PAD) ||
15880 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15881 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15882 			      NL80211_ATTR_PAD) ||
15883 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
15884 		goto nla_put_failure;
15885 
15886 	genlmsg_end(msg, hdr);
15887 
15888 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15889 				NL80211_MCGRP_MLME, gfp);
15890 	return;
15891 
15892  nla_put_failure:
15893 	nlmsg_free(msg);
15894 }
15895 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15896 
__nl80211_rx_control_port(struct net_device * dev,struct sk_buff * skb,bool unencrypted,gfp_t gfp)15897 static int __nl80211_rx_control_port(struct net_device *dev,
15898 				     struct sk_buff *skb,
15899 				     bool unencrypted, gfp_t gfp)
15900 {
15901 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15902 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15903 	struct ethhdr *ehdr = eth_hdr(skb);
15904 	const u8 *addr = ehdr->h_source;
15905 	u16 proto = be16_to_cpu(skb->protocol);
15906 	struct sk_buff *msg;
15907 	void *hdr;
15908 	struct nlattr *frame;
15909 
15910 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15911 
15912 	if (!nlportid)
15913 		return -ENOENT;
15914 
15915 	msg = nlmsg_new(100 + skb->len, gfp);
15916 	if (!msg)
15917 		return -ENOMEM;
15918 
15919 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15920 	if (!hdr) {
15921 		nlmsg_free(msg);
15922 		return -ENOBUFS;
15923 	}
15924 
15925 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15926 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15927 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15928 			      NL80211_ATTR_PAD) ||
15929 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15930 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15931 	    (unencrypted && nla_put_flag(msg,
15932 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15933 		goto nla_put_failure;
15934 
15935 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15936 	if (!frame)
15937 		goto nla_put_failure;
15938 
15939 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15940 	genlmsg_end(msg, hdr);
15941 
15942 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15943 
15944  nla_put_failure:
15945 	nlmsg_free(msg);
15946 	return -ENOBUFS;
15947 }
15948 
cfg80211_rx_control_port(struct net_device * dev,struct sk_buff * skb,bool unencrypted)15949 bool cfg80211_rx_control_port(struct net_device *dev,
15950 			      struct sk_buff *skb, bool unencrypted)
15951 {
15952 	int ret;
15953 
15954 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15955 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15956 	trace_cfg80211_return_bool(ret == 0);
15957 	return ret == 0;
15958 }
15959 EXPORT_SYMBOL(cfg80211_rx_control_port);
15960 
cfg80211_prepare_cqm(struct net_device * dev,const char * mac,gfp_t gfp)15961 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15962 					    const char *mac, gfp_t gfp)
15963 {
15964 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15965 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15966 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15967 	void **cb;
15968 
15969 	if (!msg)
15970 		return NULL;
15971 
15972 	cb = (void **)msg->cb;
15973 
15974 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15975 	if (!cb[0]) {
15976 		nlmsg_free(msg);
15977 		return NULL;
15978 	}
15979 
15980 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15981 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15982 		goto nla_put_failure;
15983 
15984 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15985 		goto nla_put_failure;
15986 
15987 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
15988 	if (!cb[1])
15989 		goto nla_put_failure;
15990 
15991 	cb[2] = rdev;
15992 
15993 	return msg;
15994  nla_put_failure:
15995 	nlmsg_free(msg);
15996 	return NULL;
15997 }
15998 
cfg80211_send_cqm(struct sk_buff * msg,gfp_t gfp)15999 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16000 {
16001 	void **cb = (void **)msg->cb;
16002 	struct cfg80211_registered_device *rdev = cb[2];
16003 
16004 	nla_nest_end(msg, cb[1]);
16005 	genlmsg_end(msg, cb[0]);
16006 
16007 	memset(msg->cb, 0, sizeof(msg->cb));
16008 
16009 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16010 				NL80211_MCGRP_MLME, gfp);
16011 }
16012 
cfg80211_cqm_rssi_notify(struct net_device * dev,enum nl80211_cqm_rssi_threshold_event rssi_event,s32 rssi_level,gfp_t gfp)16013 void cfg80211_cqm_rssi_notify(struct net_device *dev,
16014 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
16015 			      s32 rssi_level, gfp_t gfp)
16016 {
16017 	struct sk_buff *msg;
16018 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16019 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16020 
16021 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16022 
16023 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16024 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16025 		return;
16026 
16027 	if (wdev->cqm_config) {
16028 		wdev->cqm_config->last_rssi_event_value = rssi_level;
16029 
16030 		cfg80211_cqm_rssi_update(rdev, dev);
16031 
16032 		if (rssi_level == 0)
16033 			rssi_level = wdev->cqm_config->last_rssi_event_value;
16034 	}
16035 
16036 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16037 	if (!msg)
16038 		return;
16039 
16040 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16041 			rssi_event))
16042 		goto nla_put_failure;
16043 
16044 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16045 				      rssi_level))
16046 		goto nla_put_failure;
16047 
16048 	cfg80211_send_cqm(msg, gfp);
16049 
16050 	return;
16051 
16052  nla_put_failure:
16053 	nlmsg_free(msg);
16054 }
16055 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16056 
cfg80211_cqm_txe_notify(struct net_device * dev,const u8 * peer,u32 num_packets,u32 rate,u32 intvl,gfp_t gfp)16057 void cfg80211_cqm_txe_notify(struct net_device *dev,
16058 			     const u8 *peer, u32 num_packets,
16059 			     u32 rate, u32 intvl, gfp_t gfp)
16060 {
16061 	struct sk_buff *msg;
16062 
16063 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16064 	if (!msg)
16065 		return;
16066 
16067 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16068 		goto nla_put_failure;
16069 
16070 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16071 		goto nla_put_failure;
16072 
16073 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16074 		goto nla_put_failure;
16075 
16076 	cfg80211_send_cqm(msg, gfp);
16077 	return;
16078 
16079  nla_put_failure:
16080 	nlmsg_free(msg);
16081 }
16082 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16083 
cfg80211_cqm_pktloss_notify(struct net_device * dev,const u8 * peer,u32 num_packets,gfp_t gfp)16084 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16085 				 const u8 *peer, u32 num_packets, gfp_t gfp)
16086 {
16087 	struct sk_buff *msg;
16088 
16089 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16090 
16091 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16092 	if (!msg)
16093 		return;
16094 
16095 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16096 		goto nla_put_failure;
16097 
16098 	cfg80211_send_cqm(msg, gfp);
16099 	return;
16100 
16101  nla_put_failure:
16102 	nlmsg_free(msg);
16103 }
16104 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16105 
cfg80211_cqm_beacon_loss_notify(struct net_device * dev,gfp_t gfp)16106 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16107 {
16108 	struct sk_buff *msg;
16109 
16110 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16111 	if (!msg)
16112 		return;
16113 
16114 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16115 		goto nla_put_failure;
16116 
16117 	cfg80211_send_cqm(msg, gfp);
16118 	return;
16119 
16120  nla_put_failure:
16121 	nlmsg_free(msg);
16122 }
16123 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16124 
nl80211_gtk_rekey_notify(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)16125 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16126 				     struct net_device *netdev, const u8 *bssid,
16127 				     const u8 *replay_ctr, gfp_t gfp)
16128 {
16129 	struct sk_buff *msg;
16130 	struct nlattr *rekey_attr;
16131 	void *hdr;
16132 
16133 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16134 	if (!msg)
16135 		return;
16136 
16137 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16138 	if (!hdr) {
16139 		nlmsg_free(msg);
16140 		return;
16141 	}
16142 
16143 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16144 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16145 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16146 		goto nla_put_failure;
16147 
16148 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16149 	if (!rekey_attr)
16150 		goto nla_put_failure;
16151 
16152 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16153 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
16154 		goto nla_put_failure;
16155 
16156 	nla_nest_end(msg, rekey_attr);
16157 
16158 	genlmsg_end(msg, hdr);
16159 
16160 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16161 				NL80211_MCGRP_MLME, gfp);
16162 	return;
16163 
16164  nla_put_failure:
16165 	nlmsg_free(msg);
16166 }
16167 
cfg80211_gtk_rekey_notify(struct net_device * dev,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)16168 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16169 			       const u8 *replay_ctr, gfp_t gfp)
16170 {
16171 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16172 	struct wiphy *wiphy = wdev->wiphy;
16173 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16174 
16175 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
16176 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16177 }
16178 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16179 
16180 static void
nl80211_pmksa_candidate_notify(struct cfg80211_registered_device * rdev,struct net_device * netdev,int index,const u8 * bssid,bool preauth,gfp_t gfp)16181 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16182 			       struct net_device *netdev, int index,
16183 			       const u8 *bssid, bool preauth, gfp_t gfp)
16184 {
16185 	struct sk_buff *msg;
16186 	struct nlattr *attr;
16187 	void *hdr;
16188 
16189 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16190 	if (!msg)
16191 		return;
16192 
16193 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16194 	if (!hdr) {
16195 		nlmsg_free(msg);
16196 		return;
16197 	}
16198 
16199 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16200 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16201 		goto nla_put_failure;
16202 
16203 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16204 	if (!attr)
16205 		goto nla_put_failure;
16206 
16207 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16208 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16209 	    (preauth &&
16210 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16211 		goto nla_put_failure;
16212 
16213 	nla_nest_end(msg, attr);
16214 
16215 	genlmsg_end(msg, hdr);
16216 
16217 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16218 				NL80211_MCGRP_MLME, gfp);
16219 	return;
16220 
16221  nla_put_failure:
16222 	nlmsg_free(msg);
16223 }
16224 
cfg80211_pmksa_candidate_notify(struct net_device * dev,int index,const u8 * bssid,bool preauth,gfp_t gfp)16225 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16226 				     const u8 *bssid, bool preauth, gfp_t gfp)
16227 {
16228 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16229 	struct wiphy *wiphy = wdev->wiphy;
16230 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16231 
16232 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16233 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16234 }
16235 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16236 
nl80211_ch_switch_notify(struct cfg80211_registered_device * rdev,struct net_device * netdev,struct cfg80211_chan_def * chandef,gfp_t gfp,enum nl80211_commands notif,u8 count)16237 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16238 				     struct net_device *netdev,
16239 				     struct cfg80211_chan_def *chandef,
16240 				     gfp_t gfp,
16241 				     enum nl80211_commands notif,
16242 				     u8 count)
16243 {
16244 	struct sk_buff *msg;
16245 	void *hdr;
16246 
16247 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16248 	if (!msg)
16249 		return;
16250 
16251 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16252 	if (!hdr) {
16253 		nlmsg_free(msg);
16254 		return;
16255 	}
16256 
16257 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16258 		goto nla_put_failure;
16259 
16260 	if (nl80211_send_chandef(msg, chandef))
16261 		goto nla_put_failure;
16262 
16263 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16264 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16265 			goto nla_put_failure;
16266 
16267 	genlmsg_end(msg, hdr);
16268 
16269 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16270 				NL80211_MCGRP_MLME, gfp);
16271 	return;
16272 
16273  nla_put_failure:
16274 	nlmsg_free(msg);
16275 }
16276 
cfg80211_ch_switch_notify(struct net_device * dev,struct cfg80211_chan_def * chandef)16277 void cfg80211_ch_switch_notify(struct net_device *dev,
16278 			       struct cfg80211_chan_def *chandef)
16279 {
16280 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16281 	struct wiphy *wiphy = wdev->wiphy;
16282 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16283 
16284 	ASSERT_WDEV_LOCK(wdev);
16285 
16286 	trace_cfg80211_ch_switch_notify(dev, chandef);
16287 
16288 	wdev->chandef = *chandef;
16289 	wdev->preset_chandef = *chandef;
16290 
16291 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
16292 	    !WARN_ON(!wdev->current_bss))
16293 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16294 
16295 	cfg80211_sched_dfs_chan_update(rdev);
16296 
16297 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16298 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16299 }
16300 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16301 
cfg80211_ch_switch_started_notify(struct net_device * dev,struct cfg80211_chan_def * chandef,u8 count)16302 void cfg80211_ch_switch_started_notify(struct net_device *dev,
16303 				       struct cfg80211_chan_def *chandef,
16304 				       u8 count)
16305 {
16306 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16307 	struct wiphy *wiphy = wdev->wiphy;
16308 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16309 
16310 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
16311 
16312 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16313 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16314 }
16315 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16316 
16317 void
nl80211_radar_notify(struct cfg80211_registered_device * rdev,const struct cfg80211_chan_def * chandef,enum nl80211_radar_event event,struct net_device * netdev,gfp_t gfp)16318 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16319 		     const struct cfg80211_chan_def *chandef,
16320 		     enum nl80211_radar_event event,
16321 		     struct net_device *netdev, gfp_t gfp)
16322 {
16323 	struct sk_buff *msg;
16324 	void *hdr;
16325 
16326 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16327 	if (!msg)
16328 		return;
16329 
16330 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16331 	if (!hdr) {
16332 		nlmsg_free(msg);
16333 		return;
16334 	}
16335 
16336 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16337 		goto nla_put_failure;
16338 
16339 	/* NOP and radar events don't need a netdev parameter */
16340 	if (netdev) {
16341 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
16342 
16343 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16344 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16345 				      NL80211_ATTR_PAD))
16346 			goto nla_put_failure;
16347 	}
16348 
16349 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16350 		goto nla_put_failure;
16351 
16352 	if (nl80211_send_chandef(msg, chandef))
16353 		goto nla_put_failure;
16354 
16355 	genlmsg_end(msg, hdr);
16356 
16357 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16358 				NL80211_MCGRP_MLME, gfp);
16359 	return;
16360 
16361  nla_put_failure:
16362 	nlmsg_free(msg);
16363 }
16364 
cfg80211_sta_opmode_change_notify(struct net_device * dev,const u8 * mac,struct sta_opmode_info * sta_opmode,gfp_t gfp)16365 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16366 				       struct sta_opmode_info *sta_opmode,
16367 				       gfp_t gfp)
16368 {
16369 	struct sk_buff *msg;
16370 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16371 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16372 	void *hdr;
16373 
16374 	if (WARN_ON(!mac))
16375 		return;
16376 
16377 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16378 	if (!msg)
16379 		return;
16380 
16381 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16382 	if (!hdr) {
16383 		nlmsg_free(msg);
16384 		return;
16385 	}
16386 
16387 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16388 		goto nla_put_failure;
16389 
16390 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16391 		goto nla_put_failure;
16392 
16393 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16394 		goto nla_put_failure;
16395 
16396 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16397 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16398 		goto nla_put_failure;
16399 
16400 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16401 	    nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16402 		goto nla_put_failure;
16403 
16404 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16405 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16406 		goto nla_put_failure;
16407 
16408 	genlmsg_end(msg, hdr);
16409 
16410 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16411 				NL80211_MCGRP_MLME, gfp);
16412 
16413 	return;
16414 
16415 nla_put_failure:
16416 	nlmsg_free(msg);
16417 }
16418 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16419 
cfg80211_probe_status(struct net_device * dev,const u8 * addr,u64 cookie,bool acked,s32 ack_signal,bool is_valid_ack_signal,gfp_t gfp)16420 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16421 			   u64 cookie, bool acked, s32 ack_signal,
16422 			   bool is_valid_ack_signal, gfp_t gfp)
16423 {
16424 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16425 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16426 	struct sk_buff *msg;
16427 	void *hdr;
16428 
16429 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
16430 
16431 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16432 
16433 	if (!msg)
16434 		return;
16435 
16436 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16437 	if (!hdr) {
16438 		nlmsg_free(msg);
16439 		return;
16440 	}
16441 
16442 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16443 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16444 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16445 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16446 			      NL80211_ATTR_PAD) ||
16447 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16448 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16449 						ack_signal)))
16450 		goto nla_put_failure;
16451 
16452 	genlmsg_end(msg, hdr);
16453 
16454 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16455 				NL80211_MCGRP_MLME, gfp);
16456 	return;
16457 
16458  nla_put_failure:
16459 	nlmsg_free(msg);
16460 }
16461 EXPORT_SYMBOL(cfg80211_probe_status);
16462 
cfg80211_report_obss_beacon(struct wiphy * wiphy,const u8 * frame,size_t len,int freq,int sig_dbm)16463 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
16464 				 const u8 *frame, size_t len,
16465 				 int freq, int sig_dbm)
16466 {
16467 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16468 	struct sk_buff *msg;
16469 	void *hdr;
16470 	struct cfg80211_beacon_registration *reg;
16471 
16472 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
16473 
16474 	spin_lock_bh(&rdev->beacon_registrations_lock);
16475 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
16476 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
16477 		if (!msg) {
16478 			spin_unlock_bh(&rdev->beacon_registrations_lock);
16479 			return;
16480 		}
16481 
16482 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16483 		if (!hdr)
16484 			goto nla_put_failure;
16485 
16486 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16487 		    (freq &&
16488 		     nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
16489 		    (sig_dbm &&
16490 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16491 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
16492 			goto nla_put_failure;
16493 
16494 		genlmsg_end(msg, hdr);
16495 
16496 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
16497 	}
16498 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16499 	return;
16500 
16501  nla_put_failure:
16502 	spin_unlock_bh(&rdev->beacon_registrations_lock);
16503 	nlmsg_free(msg);
16504 }
16505 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
16506 
16507 #ifdef CONFIG_PM
cfg80211_net_detect_results(struct sk_buff * msg,struct cfg80211_wowlan_wakeup * wakeup)16508 static int cfg80211_net_detect_results(struct sk_buff *msg,
16509 				       struct cfg80211_wowlan_wakeup *wakeup)
16510 {
16511 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
16512 	struct nlattr *nl_results, *nl_match, *nl_freqs;
16513 	int i, j;
16514 
16515 	nl_results = nla_nest_start_noflag(msg,
16516 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
16517 	if (!nl_results)
16518 		return -EMSGSIZE;
16519 
16520 	for (i = 0; i < nd->n_matches; i++) {
16521 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
16522 
16523 		nl_match = nla_nest_start_noflag(msg, i);
16524 		if (!nl_match)
16525 			break;
16526 
16527 		/* The SSID attribute is optional in nl80211, but for
16528 		 * simplicity reasons it's always present in the
16529 		 * cfg80211 structure.  If a driver can't pass the
16530 		 * SSID, that needs to be changed.  A zero length SSID
16531 		 * is still a valid SSID (wildcard), so it cannot be
16532 		 * used for this purpose.
16533 		 */
16534 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
16535 			    match->ssid.ssid)) {
16536 			nla_nest_cancel(msg, nl_match);
16537 			goto out;
16538 		}
16539 
16540 		if (match->n_channels) {
16541 			nl_freqs = nla_nest_start_noflag(msg,
16542 							 NL80211_ATTR_SCAN_FREQUENCIES);
16543 			if (!nl_freqs) {
16544 				nla_nest_cancel(msg, nl_match);
16545 				goto out;
16546 			}
16547 
16548 			for (j = 0; j < match->n_channels; j++) {
16549 				if (nla_put_u32(msg, j, match->channels[j])) {
16550 					nla_nest_cancel(msg, nl_freqs);
16551 					nla_nest_cancel(msg, nl_match);
16552 					goto out;
16553 				}
16554 			}
16555 
16556 			nla_nest_end(msg, nl_freqs);
16557 		}
16558 
16559 		nla_nest_end(msg, nl_match);
16560 	}
16561 
16562 out:
16563 	nla_nest_end(msg, nl_results);
16564 	return 0;
16565 }
16566 
cfg80211_report_wowlan_wakeup(struct wireless_dev * wdev,struct cfg80211_wowlan_wakeup * wakeup,gfp_t gfp)16567 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
16568 				   struct cfg80211_wowlan_wakeup *wakeup,
16569 				   gfp_t gfp)
16570 {
16571 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16572 	struct sk_buff *msg;
16573 	void *hdr;
16574 	int size = 200;
16575 
16576 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
16577 
16578 	if (wakeup)
16579 		size += wakeup->packet_present_len;
16580 
16581 	msg = nlmsg_new(size, gfp);
16582 	if (!msg)
16583 		return;
16584 
16585 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
16586 	if (!hdr)
16587 		goto free_msg;
16588 
16589 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16590 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16591 			      NL80211_ATTR_PAD))
16592 		goto free_msg;
16593 
16594 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16595 					wdev->netdev->ifindex))
16596 		goto free_msg;
16597 
16598 	if (wakeup) {
16599 		struct nlattr *reasons;
16600 
16601 		reasons = nla_nest_start_noflag(msg,
16602 						NL80211_ATTR_WOWLAN_TRIGGERS);
16603 		if (!reasons)
16604 			goto free_msg;
16605 
16606 		if (wakeup->disconnect &&
16607 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
16608 			goto free_msg;
16609 		if (wakeup->magic_pkt &&
16610 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
16611 			goto free_msg;
16612 		if (wakeup->gtk_rekey_failure &&
16613 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
16614 			goto free_msg;
16615 		if (wakeup->eap_identity_req &&
16616 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
16617 			goto free_msg;
16618 		if (wakeup->four_way_handshake &&
16619 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
16620 			goto free_msg;
16621 		if (wakeup->rfkill_release &&
16622 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
16623 			goto free_msg;
16624 
16625 		if (wakeup->pattern_idx >= 0 &&
16626 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
16627 				wakeup->pattern_idx))
16628 			goto free_msg;
16629 
16630 		if (wakeup->tcp_match &&
16631 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
16632 			goto free_msg;
16633 
16634 		if (wakeup->tcp_connlost &&
16635 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
16636 			goto free_msg;
16637 
16638 		if (wakeup->tcp_nomoretokens &&
16639 		    nla_put_flag(msg,
16640 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
16641 			goto free_msg;
16642 
16643 		if (wakeup->packet) {
16644 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
16645 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
16646 
16647 			if (!wakeup->packet_80211) {
16648 				pkt_attr =
16649 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
16650 				len_attr =
16651 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
16652 			}
16653 
16654 			if (wakeup->packet_len &&
16655 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
16656 				goto free_msg;
16657 
16658 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
16659 				    wakeup->packet))
16660 				goto free_msg;
16661 		}
16662 
16663 		if (wakeup->net_detect &&
16664 		    cfg80211_net_detect_results(msg, wakeup))
16665 				goto free_msg;
16666 
16667 		nla_nest_end(msg, reasons);
16668 	}
16669 
16670 	genlmsg_end(msg, hdr);
16671 
16672 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16673 				NL80211_MCGRP_MLME, gfp);
16674 	return;
16675 
16676  free_msg:
16677 	nlmsg_free(msg);
16678 }
16679 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
16680 #endif
16681 
cfg80211_tdls_oper_request(struct net_device * dev,const u8 * peer,enum nl80211_tdls_operation oper,u16 reason_code,gfp_t gfp)16682 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
16683 				enum nl80211_tdls_operation oper,
16684 				u16 reason_code, gfp_t gfp)
16685 {
16686 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16687 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16688 	struct sk_buff *msg;
16689 	void *hdr;
16690 
16691 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
16692 					 reason_code);
16693 
16694 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16695 	if (!msg)
16696 		return;
16697 
16698 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
16699 	if (!hdr) {
16700 		nlmsg_free(msg);
16701 		return;
16702 	}
16703 
16704 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16705 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16706 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
16707 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
16708 	    (reason_code > 0 &&
16709 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
16710 		goto nla_put_failure;
16711 
16712 	genlmsg_end(msg, hdr);
16713 
16714 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16715 				NL80211_MCGRP_MLME, gfp);
16716 	return;
16717 
16718  nla_put_failure:
16719 	nlmsg_free(msg);
16720 }
16721 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
16722 
nl80211_netlink_notify(struct notifier_block * nb,unsigned long state,void * _notify)16723 static int nl80211_netlink_notify(struct notifier_block * nb,
16724 				  unsigned long state,
16725 				  void *_notify)
16726 {
16727 	struct netlink_notify *notify = _notify;
16728 	struct cfg80211_registered_device *rdev;
16729 	struct wireless_dev *wdev;
16730 	struct cfg80211_beacon_registration *reg, *tmp;
16731 
16732 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
16733 		return NOTIFY_DONE;
16734 
16735 	rcu_read_lock();
16736 
16737 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
16738 		struct cfg80211_sched_scan_request *sched_scan_req;
16739 
16740 		list_for_each_entry_rcu(sched_scan_req,
16741 					&rdev->sched_scan_req_list,
16742 					list) {
16743 			if (sched_scan_req->owner_nlportid == notify->portid) {
16744 				sched_scan_req->nl_owner_dead = true;
16745 				schedule_work(&rdev->sched_scan_stop_wk);
16746 			}
16747 		}
16748 
16749 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
16750 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
16751 
16752 			if (wdev->owner_nlportid == notify->portid) {
16753 				wdev->nl_owner_dead = true;
16754 				schedule_work(&rdev->destroy_work);
16755 			} else if (wdev->conn_owner_nlportid == notify->portid) {
16756 				schedule_work(&wdev->disconnect_wk);
16757 			}
16758 
16759 			cfg80211_release_pmsr(wdev, notify->portid);
16760 		}
16761 
16762 		spin_lock_bh(&rdev->beacon_registrations_lock);
16763 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
16764 					 list) {
16765 			if (reg->nlportid == notify->portid) {
16766 				list_del(&reg->list);
16767 				kfree(reg);
16768 				break;
16769 			}
16770 		}
16771 		spin_unlock_bh(&rdev->beacon_registrations_lock);
16772 	}
16773 
16774 	rcu_read_unlock();
16775 
16776 	/*
16777 	 * It is possible that the user space process that is controlling the
16778 	 * indoor setting disappeared, so notify the regulatory core.
16779 	 */
16780 	regulatory_netlink_notify(notify->portid);
16781 	return NOTIFY_OK;
16782 }
16783 
16784 static struct notifier_block nl80211_netlink_notifier = {
16785 	.notifier_call = nl80211_netlink_notify,
16786 };
16787 
cfg80211_ft_event(struct net_device * netdev,struct cfg80211_ft_event_params * ft_event)16788 void cfg80211_ft_event(struct net_device *netdev,
16789 		       struct cfg80211_ft_event_params *ft_event)
16790 {
16791 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16792 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16793 	struct sk_buff *msg;
16794 	void *hdr;
16795 
16796 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16797 
16798 	if (!ft_event->target_ap)
16799 		return;
16800 
16801 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16802 			GFP_KERNEL);
16803 	if (!msg)
16804 		return;
16805 
16806 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16807 	if (!hdr)
16808 		goto out;
16809 
16810 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16811 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16812 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16813 		goto out;
16814 
16815 	if (ft_event->ies &&
16816 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16817 		goto out;
16818 	if (ft_event->ric_ies &&
16819 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16820 		    ft_event->ric_ies))
16821 		goto out;
16822 
16823 	genlmsg_end(msg, hdr);
16824 
16825 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16826 				NL80211_MCGRP_MLME, GFP_KERNEL);
16827 	return;
16828  out:
16829 	nlmsg_free(msg);
16830 }
16831 EXPORT_SYMBOL(cfg80211_ft_event);
16832 
cfg80211_crit_proto_stopped(struct wireless_dev * wdev,gfp_t gfp)16833 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16834 {
16835 	struct cfg80211_registered_device *rdev;
16836 	struct sk_buff *msg;
16837 	void *hdr;
16838 	u32 nlportid;
16839 
16840 	rdev = wiphy_to_rdev(wdev->wiphy);
16841 	if (!rdev->crit_proto_nlportid)
16842 		return;
16843 
16844 	nlportid = rdev->crit_proto_nlportid;
16845 	rdev->crit_proto_nlportid = 0;
16846 
16847 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16848 	if (!msg)
16849 		return;
16850 
16851 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16852 	if (!hdr)
16853 		goto nla_put_failure;
16854 
16855 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16856 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16857 			      NL80211_ATTR_PAD))
16858 		goto nla_put_failure;
16859 
16860 	genlmsg_end(msg, hdr);
16861 
16862 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16863 	return;
16864 
16865  nla_put_failure:
16866 	nlmsg_free(msg);
16867 }
16868 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16869 
nl80211_send_ap_stopped(struct wireless_dev * wdev)16870 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16871 {
16872 	struct wiphy *wiphy = wdev->wiphy;
16873 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16874 	struct sk_buff *msg;
16875 	void *hdr;
16876 
16877 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16878 	if (!msg)
16879 		return;
16880 
16881 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16882 	if (!hdr)
16883 		goto out;
16884 
16885 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16886 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16887 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16888 			      NL80211_ATTR_PAD))
16889 		goto out;
16890 
16891 	genlmsg_end(msg, hdr);
16892 
16893 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16894 				NL80211_MCGRP_MLME, GFP_KERNEL);
16895 	return;
16896  out:
16897 	nlmsg_free(msg);
16898 }
16899 
cfg80211_external_auth_request(struct net_device * dev,struct cfg80211_external_auth_params * params,gfp_t gfp)16900 int cfg80211_external_auth_request(struct net_device *dev,
16901 				   struct cfg80211_external_auth_params *params,
16902 				   gfp_t gfp)
16903 {
16904 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16905 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16906 	struct sk_buff *msg;
16907 	void *hdr;
16908 
16909 	if (!wdev->conn_owner_nlportid)
16910 		return -EINVAL;
16911 
16912 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16913 	if (!msg)
16914 		return -ENOMEM;
16915 
16916 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16917 	if (!hdr)
16918 		goto nla_put_failure;
16919 
16920 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16921 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16922 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16923 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16924 			params->action) ||
16925 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16926 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16927 		    params->ssid.ssid))
16928 		goto nla_put_failure;
16929 
16930 	genlmsg_end(msg, hdr);
16931 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16932 			wdev->conn_owner_nlportid);
16933 	return 0;
16934 
16935  nla_put_failure:
16936 	nlmsg_free(msg);
16937 	return -ENOBUFS;
16938 }
16939 EXPORT_SYMBOL(cfg80211_external_auth_request);
16940 
cfg80211_update_owe_info_event(struct net_device * netdev,struct cfg80211_update_owe_info * owe_info,gfp_t gfp)16941 void cfg80211_update_owe_info_event(struct net_device *netdev,
16942 				    struct cfg80211_update_owe_info *owe_info,
16943 				    gfp_t gfp)
16944 {
16945 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16946 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16947 	struct sk_buff *msg;
16948 	void *hdr;
16949 
16950 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
16951 
16952 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16953 	if (!msg)
16954 		return;
16955 
16956 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
16957 	if (!hdr)
16958 		goto nla_put_failure;
16959 
16960 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16961 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16962 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
16963 		goto nla_put_failure;
16964 
16965 	if (!owe_info->ie_len ||
16966 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
16967 		goto nla_put_failure;
16968 
16969 	genlmsg_end(msg, hdr);
16970 
16971 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16972 				NL80211_MCGRP_MLME, gfp);
16973 	return;
16974 
16975 nla_put_failure:
16976 	genlmsg_cancel(msg, hdr);
16977 	nlmsg_free(msg);
16978 }
16979 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
16980 
16981 /* initialisation/exit functions */
16982 
nl80211_init(void)16983 int __init nl80211_init(void)
16984 {
16985 	int err;
16986 
16987 	err = genl_register_family(&nl80211_fam);
16988 	if (err)
16989 		return err;
16990 
16991 	err = netlink_register_notifier(&nl80211_netlink_notifier);
16992 	if (err)
16993 		goto err_out;
16994 
16995 	return 0;
16996  err_out:
16997 	genl_unregister_family(&nl80211_fam);
16998 	return err;
16999 }
17000 
nl80211_exit(void)17001 void nl80211_exit(void)
17002 {
17003 	netlink_unregister_notifier(&nl80211_netlink_notifier);
17004 	genl_unregister_family(&nl80211_fam);
17005 }
17006