• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2021 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 <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33 
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35 				   struct genl_info *info,
36 				   struct cfg80211_crypto_settings *settings,
37 				   int cipher_limit);
38 
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41 
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44 	NL80211_MCGRP_CONFIG,
45 	NL80211_MCGRP_SCAN,
46 	NL80211_MCGRP_REGULATORY,
47 	NL80211_MCGRP_MLME,
48 	NL80211_MCGRP_VENDOR,
49 	NL80211_MCGRP_NAN,
50 	NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52 
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54 	[NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55 	[NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56 	[NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57 	[NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58 	[NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59 	[NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61 	[NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64 
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
__cfg80211_wdev_from_attrs(struct net * netns,struct nlattr ** attrs)67 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
68 {
69 	struct cfg80211_registered_device *rdev;
70 	struct wireless_dev *result = NULL;
71 	bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72 	bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73 	u64 wdev_id;
74 	int wiphy_idx = -1;
75 	int ifidx = -1;
76 
77 	ASSERT_RTNL();
78 
79 	if (!have_ifidx && !have_wdev_id)
80 		return ERR_PTR(-EINVAL);
81 
82 	if (have_ifidx)
83 		ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
84 	if (have_wdev_id) {
85 		wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
86 		wiphy_idx = wdev_id >> 32;
87 	}
88 
89 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
90 		struct wireless_dev *wdev;
91 
92 		if (wiphy_net(&rdev->wiphy) != netns)
93 			continue;
94 
95 		if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
96 			continue;
97 
98 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
99 			if (have_ifidx && wdev->netdev &&
100 			    wdev->netdev->ifindex == ifidx) {
101 				result = wdev;
102 				break;
103 			}
104 			if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
105 				result = wdev;
106 				break;
107 			}
108 		}
109 
110 		if (result)
111 			break;
112 	}
113 
114 	if (result)
115 		return result;
116 	return ERR_PTR(-ENODEV);
117 }
118 
119 static struct cfg80211_registered_device *
__cfg80211_rdev_from_attrs(struct net * netns,struct nlattr ** attrs)120 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
121 {
122 	struct cfg80211_registered_device *rdev = NULL, *tmp;
123 	struct net_device *netdev;
124 
125 	ASSERT_RTNL();
126 
127 	if (!attrs[NL80211_ATTR_WIPHY] &&
128 	    !attrs[NL80211_ATTR_IFINDEX] &&
129 	    !attrs[NL80211_ATTR_WDEV])
130 		return ERR_PTR(-EINVAL);
131 
132 	if (attrs[NL80211_ATTR_WIPHY])
133 		rdev = cfg80211_rdev_by_wiphy_idx(
134 				nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
135 
136 	if (attrs[NL80211_ATTR_WDEV]) {
137 		u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
138 		struct wireless_dev *wdev;
139 		bool found = false;
140 
141 		tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
142 		if (tmp) {
143 			/* make sure wdev exists */
144 			list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
145 				if (wdev->identifier != (u32)wdev_id)
146 					continue;
147 				found = true;
148 				break;
149 			}
150 
151 			if (!found)
152 				tmp = NULL;
153 
154 			if (rdev && tmp != rdev)
155 				return ERR_PTR(-EINVAL);
156 			rdev = tmp;
157 		}
158 	}
159 
160 	if (attrs[NL80211_ATTR_IFINDEX]) {
161 		int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
162 
163 		netdev = __dev_get_by_index(netns, ifindex);
164 		if (netdev) {
165 			if (netdev->ieee80211_ptr)
166 				tmp = wiphy_to_rdev(
167 					netdev->ieee80211_ptr->wiphy);
168 			else
169 				tmp = NULL;
170 
171 			/* not wireless device -- return error */
172 			if (!tmp)
173 				return ERR_PTR(-EINVAL);
174 
175 			/* mismatch -- return error */
176 			if (rdev && tmp != rdev)
177 				return ERR_PTR(-EINVAL);
178 
179 			rdev = tmp;
180 		}
181 	}
182 
183 	if (!rdev)
184 		return ERR_PTR(-ENODEV);
185 
186 	if (netns != wiphy_net(&rdev->wiphy))
187 		return ERR_PTR(-ENODEV);
188 
189 	return rdev;
190 }
191 
192 /*
193  * This function returns a pointer to the driver
194  * that the genl_info item that is passed refers to.
195  *
196  * The result of this can be a PTR_ERR and hence must
197  * be checked with IS_ERR() for errors.
198  */
199 static struct cfg80211_registered_device *
cfg80211_get_dev_from_info(struct net * netns,struct genl_info * info)200 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
201 {
202 	return __cfg80211_rdev_from_attrs(netns, info->attrs);
203 }
204 
validate_beacon_head(const struct nlattr * attr,struct netlink_ext_ack * extack)205 static int validate_beacon_head(const struct nlattr *attr,
206 				struct netlink_ext_ack *extack)
207 {
208 	const u8 *data = nla_data(attr);
209 	unsigned int len = nla_len(attr);
210 	const struct element *elem;
211 	const struct ieee80211_mgmt *mgmt = (void *)data;
212 	unsigned int fixedlen, hdrlen;
213 	bool s1g_bcn;
214 
215 	if (len < offsetofend(typeof(*mgmt), frame_control))
216 		goto err;
217 
218 	s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
219 	if (s1g_bcn) {
220 		fixedlen = offsetof(struct ieee80211_ext,
221 				    u.s1g_beacon.variable);
222 		hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
223 	} else {
224 		fixedlen = offsetof(struct ieee80211_mgmt,
225 				    u.beacon.variable);
226 		hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
227 	}
228 
229 	if (len < fixedlen)
230 		goto err;
231 
232 	if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
233 		goto err;
234 
235 	data += fixedlen;
236 	len -= fixedlen;
237 
238 	for_each_element(elem, data, len) {
239 		/* nothing */
240 	}
241 
242 	if (for_each_element_completed(elem, data, len))
243 		return 0;
244 
245 err:
246 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
247 	return -EINVAL;
248 }
249 
validate_ie_attr(const struct nlattr * attr,struct netlink_ext_ack * extack)250 static int validate_ie_attr(const struct nlattr *attr,
251 			    struct netlink_ext_ack *extack)
252 {
253 	const u8 *data = nla_data(attr);
254 	unsigned int len = nla_len(attr);
255 	const struct element *elem;
256 
257 	for_each_element(elem, data, len) {
258 		/* nothing */
259 	}
260 
261 	if (for_each_element_completed(elem, data, len))
262 		return 0;
263 
264 	NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
265 	return -EINVAL;
266 }
267 
268 /* policy for the attributes */
269 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
270 
271 static const struct nla_policy
272 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
273 	[NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
274 	[NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
275 					.len = U8_MAX },
276 	[NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
277 					     .len = U8_MAX },
278 };
279 
280 static const struct nla_policy
281 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
282 	[NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
283 	[NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
284 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
285 		NLA_POLICY_MAX(NLA_U8, 15),
286 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
287 	[NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
288 		NLA_POLICY_MAX(NLA_U8, 15),
289 	[NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
290 		NLA_POLICY_MAX(NLA_U8, 31),
291 	[NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
292 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
293 	[NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
294 	[NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
295 	[NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
296 };
297 
298 static const struct nla_policy
299 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
300 	[NL80211_PMSR_TYPE_FTM] =
301 		NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
302 };
303 
304 static const struct nla_policy
305 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
306 	[NL80211_PMSR_REQ_ATTR_DATA] =
307 		NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
308 	[NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
309 };
310 
311 static const struct nla_policy
312 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
313 	[NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
314 	[NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
315 	[NL80211_PMSR_PEER_ATTR_REQ] =
316 		NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
317 	[NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
318 };
319 
320 static const struct nla_policy
321 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
322 	[NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
323 	[NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
324 	[NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
325 	[NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
326 	[NL80211_PMSR_ATTR_PEERS] =
327 		NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
328 };
329 
330 static const struct nla_policy
331 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
332 	[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
333 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
334 	[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
335 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
336 	[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
337 		NLA_POLICY_RANGE(NLA_U8, 1, 20),
338 	[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
339 		NLA_POLICY_EXACT_LEN(8),
340 	[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
341 		NLA_POLICY_EXACT_LEN(8),
342 	[NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
343 };
344 
345 static const struct nla_policy
346 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
347 	[NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
348 	[NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
349 	[NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
350 };
351 
352 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
353 	[NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
354 				    .len = NL80211_MAX_SUPP_RATES },
355 	[NL80211_TXRATE_HT] = { .type = NLA_BINARY,
356 				.len = NL80211_MAX_SUPP_HT_RATES },
357 	[NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
358 	[NL80211_TXRATE_GI] = { .type = NLA_U8 },
359 	[NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
360 	[NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
361 						   NL80211_RATE_INFO_HE_GI_0_8,
362 						   NL80211_RATE_INFO_HE_GI_3_2),
363 	[NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
364 						   NL80211_RATE_INFO_HE_1XLTF,
365 						   NL80211_RATE_INFO_HE_4XLTF),
366 };
367 
368 static const struct nla_policy
369 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
370 	[NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
371 	[NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
372 	[NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
373 	[NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
374 	[NL80211_TID_CONFIG_ATTR_NOACK] =
375 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
376 	[NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
377 	[NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
378 	[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
379 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
380 	[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
381 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
382 	[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
383 			NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
384 	[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
385 			NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
386 	[NL80211_TID_CONFIG_ATTR_TX_RATE] =
387 			NLA_POLICY_NESTED(nl80211_txattr_policy),
388 };
389 
390 static const struct nla_policy
391 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
392 	[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
393 	[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
394 	NLA_POLICY_RANGE(NLA_BINARY,
395 			 NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
396 			 IEEE80211_MAX_DATA_LEN),
397 };
398 
399 static const struct nla_policy
400 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
401 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
402 	[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
403 						       .len = IEEE80211_MAX_DATA_LEN }
404 };
405 
406 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
407 	[0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
408 	[NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
409 	[NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
410 				      .len = 20-1 },
411 	[NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
412 
413 	[NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
414 	[NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
415 	[NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
416 						NL80211_EDMG_CHANNELS_MIN,
417 						NL80211_EDMG_CHANNELS_MAX),
418 	[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
419 						NL80211_EDMG_BW_CONFIG_MIN,
420 						NL80211_EDMG_BW_CONFIG_MAX),
421 
422 	[NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
423 	[NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
424 	[NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
425 	[NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
426 
427 	[NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
428 	[NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
429 	[NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
430 	[NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
431 	[NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
432 	[NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
433 
434 	[NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
435 	[NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
436 	[NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
437 
438 	[NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
439 	[NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
440 
441 	[NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
442 	[NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
443 				    .len = WLAN_MAX_KEY_LEN },
444 	[NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
445 	[NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
446 	[NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
447 	[NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
448 	[NL80211_ATTR_KEY_TYPE] =
449 		NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
450 
451 	[NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
452 	[NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
453 	[NL80211_ATTR_BEACON_HEAD] =
454 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
455 				       IEEE80211_MAX_DATA_LEN),
456 	[NL80211_ATTR_BEACON_TAIL] =
457 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
458 				       IEEE80211_MAX_DATA_LEN),
459 	[NL80211_ATTR_STA_AID] =
460 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
461 	[NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
462 	[NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
463 	[NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
464 					       .len = NL80211_MAX_SUPP_RATES },
465 	[NL80211_ATTR_STA_PLINK_ACTION] =
466 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
467 	[NL80211_ATTR_STA_TX_POWER_SETTING] =
468 		NLA_POLICY_RANGE(NLA_U8,
469 				 NL80211_TX_POWER_AUTOMATIC,
470 				 NL80211_TX_POWER_FIXED),
471 	[NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
472 	[NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
473 	[NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
474 	[NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
475 				   .len = IEEE80211_MAX_MESH_ID_LEN },
476 	[NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
477 
478 	/* allow 3 for NUL-termination, we used to declare this NLA_STRING */
479 	[NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
480 	[NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
481 
482 	[NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
483 	[NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
484 	[NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
485 	[NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
486 					   .len = NL80211_MAX_SUPP_RATES },
487 	[NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
488 
489 	[NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
490 	[NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
491 
492 	[NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
493 
494 	[NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
495 	[NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
496 						   validate_ie_attr,
497 						   IEEE80211_MAX_DATA_LEN),
498 	[NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
499 	[NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
500 
501 	[NL80211_ATTR_SSID] = { .type = NLA_BINARY,
502 				.len = IEEE80211_MAX_SSID_LEN },
503 	[NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
504 	[NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
505 	[NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
506 	[NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
507 	[NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
508 						  NL80211_MFP_NO,
509 						  NL80211_MFP_OPTIONAL),
510 	[NL80211_ATTR_STA_FLAGS2] = {
511 		.len = sizeof(struct nl80211_sta_flag_update),
512 	},
513 	[NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
514 	[NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
515 	[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
516 	[NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
517 	[NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
518 	[NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
519 	[NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
520 	[NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
521 	[NL80211_ATTR_PID] = { .type = NLA_U32 },
522 	[NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
523 	[NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
524 	[NL80211_ATTR_DURATION] = { .type = NLA_U32 },
525 	[NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
526 	[NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
527 	[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
528 				 .len = IEEE80211_MAX_DATA_LEN },
529 	[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
530 	[NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
531 						   NL80211_PS_DISABLED,
532 						   NL80211_PS_ENABLED),
533 	[NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
534 	[NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
535 	[NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
536 	[NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
537 	[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
538 	[NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
539 	[NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
540 	[NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
541 	[NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
542 	[NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
543 	[NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
544 	[NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
545 	[NL80211_ATTR_STA_PLINK_STATE] =
546 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
547 	[NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
548 	[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
549 	[NL80211_ATTR_MESH_PEER_AID] =
550 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
551 	[NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
552 	[NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
553 	[NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
554 	[NL80211_ATTR_HIDDEN_SSID] =
555 		NLA_POLICY_RANGE(NLA_U32,
556 				 NL80211_HIDDEN_SSID_NOT_IN_USE,
557 				 NL80211_HIDDEN_SSID_ZERO_CONTENTS),
558 	[NL80211_ATTR_IE_PROBE_RESP] =
559 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
560 				       IEEE80211_MAX_DATA_LEN),
561 	[NL80211_ATTR_IE_ASSOC_RESP] =
562 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
563 				       IEEE80211_MAX_DATA_LEN),
564 	[NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
565 	[NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
566 	[NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
567 	[NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
568 	[NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
569 	[NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
570 	[NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
571 	[NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
572 	[NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
573 	[NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
574 	[NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
575 				      .len = IEEE80211_MAX_DATA_LEN },
576 	[NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
577 	[NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
578 	[NL80211_ATTR_HT_CAPABILITY_MASK] = {
579 		.len = NL80211_HT_CAPABILITY_LEN
580 	},
581 	[NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
582 	[NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
583 	[NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
584 	[NL80211_ATTR_WDEV] = { .type = NLA_U64 },
585 	[NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
586 
587 	/* need to include at least Auth Transaction and Status Code */
588 	[NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
589 
590 	[NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
591 	[NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
592 	[NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
593 	[NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
594 	[NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
595 		NLA_POLICY_RANGE(NLA_U32,
596 				 NL80211_MESH_POWER_UNKNOWN + 1,
597 				 NL80211_MESH_POWER_MAX),
598 	[NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
599 	[NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
600 	[NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
601 	[NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
602 	[NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
603 	[NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
604 	[NL80211_ATTR_VHT_CAPABILITY_MASK] = {
605 		.len = NL80211_VHT_CAPABILITY_LEN,
606 	},
607 	[NL80211_ATTR_MDID] = { .type = NLA_U16 },
608 	[NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
609 				  .len = IEEE80211_MAX_DATA_LEN },
610 	[NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
611 	[NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
612 		NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
613 	[NL80211_ATTR_PEER_AID] =
614 		NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
615 	[NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
616 	[NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
617 	[NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
618 	[NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
619 	[NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
620 	[NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
621 	/*
622 	 * The value of the Length field of the Supported Operating
623 	 * Classes element is between 2 and 253.
624 	 */
625 	[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
626 		NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
627 	[NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
628 	[NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
629 	[NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
630 	[NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
631 	[NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
632 	[NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
633 						  IEEE80211_QOS_MAP_LEN_MIN,
634 						  IEEE80211_QOS_MAP_LEN_MAX),
635 	[NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
636 	[NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
637 	[NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
638 	[NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
639 	[NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
640 	[NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
641 	[NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
642 	[NL80211_ATTR_USER_PRIO] =
643 		NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
644 	[NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
645 	[NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
646 	[NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
647 	[NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
648 	[NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
649 	[NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
650 	[NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
651 	[NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
652 	[NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
653 	[NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
654 	[NL80211_ATTR_STA_SUPPORT_P2P_PS] =
655 		NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
656 	[NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
657 		.len = VHT_MUMIMO_GROUPS_DATA_LEN
658 	},
659 	[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
660 	[NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
661 	[NL80211_ATTR_BANDS] = { .type = NLA_U32 },
662 	[NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
663 	[NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
664 				    .len = FILS_MAX_KEK_LEN },
665 	[NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
666 	[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
667 	[NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
668 	[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
669 	[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
670 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
671 	},
672 	[NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
673 	[NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
674 					     .len = FILS_ERP_MAX_USERNAME_LEN },
675 	[NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
676 					  .len = FILS_ERP_MAX_REALM_LEN },
677 	[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
678 	[NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
679 					.len = FILS_ERP_MAX_RRK_LEN },
680 	[NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
681 	[NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
682 	[NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
683 	[NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
684 	[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
685 
686 	[NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
687 	[NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
688 	[NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
689 	[NL80211_ATTR_HE_CAPABILITY] =
690 		NLA_POLICY_RANGE(NLA_BINARY,
691 				 NL80211_HE_MIN_CAPABILITY_LEN,
692 				 NL80211_HE_MAX_CAPABILITY_LEN),
693 	[NL80211_ATTR_FTM_RESPONDER] =
694 		NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
695 	[NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
696 	[NL80211_ATTR_PEER_MEASUREMENTS] =
697 		NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
698 	[NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
699 	[NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
700 					.len = SAE_PASSWORD_MAX_LEN },
701 	[NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
702 	[NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
703 	[NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
704 	[NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
705 	[NL80211_ATTR_TID_CONFIG] =
706 		NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
707 	[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
708 	[NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
709 	[NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
710 	[NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
711 	[NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
712 	[NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
713 	[NL80211_ATTR_HE_6GHZ_CAPABILITY] =
714 		NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
715 	[NL80211_ATTR_FILS_DISCOVERY] =
716 		NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
717 	[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
718 		NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
719 	[NL80211_ATTR_S1G_CAPABILITY] =
720 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
721 	[NL80211_ATTR_S1G_CAPABILITY_MASK] =
722 		NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
723 	[NL80211_ATTR_SAE_PWE] =
724 		NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
725 				 NL80211_SAE_PWE_BOTH),
726 };
727 
728 /* policy for the key attributes */
729 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
730 	[NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
731 	[NL80211_KEY_IDX] = { .type = NLA_U8 },
732 	[NL80211_KEY_CIPHER] = { .type = NLA_U32 },
733 	[NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
734 	[NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
735 	[NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
736 	[NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
737 	[NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
738 	[NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
739 };
740 
741 /* policy for the key default flags */
742 static const struct nla_policy
743 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
744 	[NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
745 	[NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
746 };
747 
748 #ifdef CONFIG_PM
749 /* policy for WoWLAN attributes */
750 static const struct nla_policy
751 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
752 	[NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
753 	[NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
754 	[NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
755 	[NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
756 	[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
757 	[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
758 	[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
759 	[NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
760 	[NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
761 	[NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
762 };
763 
764 static const struct nla_policy
765 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
766 	[NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
767 	[NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
768 	[NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
769 	[NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
770 	[NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
771 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
772 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
773 		.len = sizeof(struct nl80211_wowlan_tcp_data_seq)
774 	},
775 	[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
776 		.len = sizeof(struct nl80211_wowlan_tcp_data_token)
777 	},
778 	[NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
779 	[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
780 	[NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
781 };
782 #endif /* CONFIG_PM */
783 
784 /* policy for coalesce rule attributes */
785 static const struct nla_policy
786 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
787 	[NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
788 	[NL80211_ATTR_COALESCE_RULE_CONDITION] =
789 		NLA_POLICY_RANGE(NLA_U32,
790 				 NL80211_COALESCE_CONDITION_MATCH,
791 				 NL80211_COALESCE_CONDITION_NO_MATCH),
792 	[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
793 };
794 
795 /* policy for GTK rekey offload attributes */
796 static const struct nla_policy
797 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
798 	[NL80211_REKEY_DATA_KEK] = {
799 		.type = NLA_BINARY,
800 		.len = NL80211_KEK_EXT_LEN
801 	},
802 	[NL80211_REKEY_DATA_KCK] = {
803 		.type = NLA_BINARY,
804 		.len = NL80211_KCK_EXT_LEN
805 	},
806 	[NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
807 	[NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
808 };
809 
810 static const struct nla_policy
811 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
812 	[NL80211_BAND_2GHZ] = { .type = NLA_S32 },
813 	[NL80211_BAND_5GHZ] = { .type = NLA_S32 },
814 	[NL80211_BAND_6GHZ] = { .type = NLA_S32 },
815 	[NL80211_BAND_60GHZ] = { .type = NLA_S32 },
816 };
817 
818 static const struct nla_policy
819 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
820 	[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
821 						 .len = IEEE80211_MAX_SSID_LEN },
822 	[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
823 	[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
824 	[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
825 		NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
826 };
827 
828 static const struct nla_policy
829 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
830 	[NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
831 	[NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
832 };
833 
834 static const struct nla_policy
835 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
836 	[NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
837 	[NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
838 	[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
839 		.len = sizeof(struct nl80211_bss_select_rssi_adjust)
840 	},
841 };
842 
843 /* policy for NAN function attributes */
844 static const struct nla_policy
845 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
846 	[NL80211_NAN_FUNC_TYPE] =
847 		NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
848 	[NL80211_NAN_FUNC_SERVICE_ID] = {
849 				    .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
850 	[NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
851 	[NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
852 	[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
853 	[NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
854 	[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
855 	[NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
856 	[NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
857 	[NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
858 	[NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
859 			.len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
860 	[NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
861 	[NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
862 	[NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
863 	[NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
864 	[NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
865 };
866 
867 /* policy for Service Response Filter attributes */
868 static const struct nla_policy
869 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
870 	[NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
871 	[NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
872 				 .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
873 	[NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
874 	[NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
875 };
876 
877 /* policy for packet pattern attributes */
878 static const struct nla_policy
879 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
880 	[NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
881 	[NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
882 	[NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
883 };
884 
nl80211_prepare_wdev_dump(struct netlink_callback * cb,struct cfg80211_registered_device ** rdev,struct wireless_dev ** wdev)885 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
886 			      struct cfg80211_registered_device **rdev,
887 			      struct wireless_dev **wdev)
888 {
889 	int err;
890 
891 	if (!cb->args[0]) {
892 		struct nlattr **attrbuf;
893 
894 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
895 				  GFP_KERNEL);
896 		if (!attrbuf)
897 			return -ENOMEM;
898 
899 		err = nlmsg_parse_deprecated(cb->nlh,
900 					     GENL_HDRLEN + nl80211_fam.hdrsize,
901 					     attrbuf, nl80211_fam.maxattr,
902 					     nl80211_policy, NULL);
903 		if (err) {
904 			kfree(attrbuf);
905 			return err;
906 		}
907 
908 		*wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
909 						   attrbuf);
910 		kfree(attrbuf);
911 		if (IS_ERR(*wdev))
912 			return PTR_ERR(*wdev);
913 		*rdev = wiphy_to_rdev((*wdev)->wiphy);
914 		/* 0 is the first index - add 1 to parse only once */
915 		cb->args[0] = (*rdev)->wiphy_idx + 1;
916 		cb->args[1] = (*wdev)->identifier;
917 	} else {
918 		/* subtract the 1 again here */
919 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
920 		struct wireless_dev *tmp;
921 
922 		if (!wiphy)
923 			return -ENODEV;
924 		*rdev = wiphy_to_rdev(wiphy);
925 		*wdev = NULL;
926 
927 		list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
928 			if (tmp->identifier == cb->args[1]) {
929 				*wdev = tmp;
930 				break;
931 			}
932 		}
933 
934 		if (!*wdev)
935 			return -ENODEV;
936 	}
937 
938 	return 0;
939 }
940 
941 /* message building helper */
nl80211hdr_put(struct sk_buff * skb,u32 portid,u32 seq,int flags,u8 cmd)942 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
943 		     int flags, u8 cmd)
944 {
945 	/* since there is no private header just add the generic one */
946 	return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
947 }
948 
nl80211_msg_put_wmm_rules(struct sk_buff * msg,const struct ieee80211_reg_rule * rule)949 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
950 				     const struct ieee80211_reg_rule *rule)
951 {
952 	int j;
953 	struct nlattr *nl_wmm_rules =
954 		nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
955 
956 	if (!nl_wmm_rules)
957 		goto nla_put_failure;
958 
959 	for (j = 0; j < IEEE80211_NUM_ACS; j++) {
960 		struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
961 
962 		if (!nl_wmm_rule)
963 			goto nla_put_failure;
964 
965 		if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
966 				rule->wmm_rule.client[j].cw_min) ||
967 		    nla_put_u16(msg, NL80211_WMMR_CW_MAX,
968 				rule->wmm_rule.client[j].cw_max) ||
969 		    nla_put_u8(msg, NL80211_WMMR_AIFSN,
970 			       rule->wmm_rule.client[j].aifsn) ||
971 		    nla_put_u16(msg, NL80211_WMMR_TXOP,
972 			        rule->wmm_rule.client[j].cot))
973 			goto nla_put_failure;
974 
975 		nla_nest_end(msg, nl_wmm_rule);
976 	}
977 	nla_nest_end(msg, nl_wmm_rules);
978 
979 	return 0;
980 
981 nla_put_failure:
982 	return -ENOBUFS;
983 }
984 
nl80211_msg_put_channel(struct sk_buff * msg,struct wiphy * wiphy,struct ieee80211_channel * chan,bool large)985 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
986 				   struct ieee80211_channel *chan,
987 				   bool large)
988 {
989 	/* Some channels must be completely excluded from the
990 	 * list to protect old user-space tools from breaking
991 	 */
992 	if (!large && chan->flags &
993 	    (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
994 		return 0;
995 	if (!large && chan->freq_offset)
996 		return 0;
997 
998 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
999 			chan->center_freq))
1000 		goto nla_put_failure;
1001 
1002 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1003 		goto nla_put_failure;
1004 
1005 	if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1006 	    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1007 		goto nla_put_failure;
1008 	if (chan->flags & IEEE80211_CHAN_NO_IR) {
1009 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1010 			goto nla_put_failure;
1011 		if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1012 			goto nla_put_failure;
1013 	}
1014 	if (chan->flags & IEEE80211_CHAN_RADAR) {
1015 		if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1016 			goto nla_put_failure;
1017 		if (large) {
1018 			u32 time;
1019 
1020 			time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1021 
1022 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1023 					chan->dfs_state))
1024 				goto nla_put_failure;
1025 			if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1026 					time))
1027 				goto nla_put_failure;
1028 			if (nla_put_u32(msg,
1029 					NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1030 					chan->dfs_cac_ms))
1031 				goto nla_put_failure;
1032 		}
1033 	}
1034 
1035 	if (large) {
1036 		if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1037 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1038 			goto nla_put_failure;
1039 		if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1040 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1041 			goto nla_put_failure;
1042 		if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1043 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1044 			goto nla_put_failure;
1045 		if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1046 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1047 			goto nla_put_failure;
1048 		if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1049 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1050 			goto nla_put_failure;
1051 		if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1052 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1053 			goto nla_put_failure;
1054 		if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1055 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1056 			goto nla_put_failure;
1057 		if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1058 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1059 			goto nla_put_failure;
1060 		if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1061 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1062 			goto nla_put_failure;
1063 		if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1064 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1065 			goto nla_put_failure;
1066 		if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1067 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1068 			goto nla_put_failure;
1069 		if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1070 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1071 			goto nla_put_failure;
1072 		if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1073 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1074 			goto nla_put_failure;
1075 		if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1076 		    nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1077 			goto nla_put_failure;
1078 	}
1079 
1080 	if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1081 			DBM_TO_MBM(chan->max_power)))
1082 		goto nla_put_failure;
1083 
1084 	if (large) {
1085 		const struct ieee80211_reg_rule *rule =
1086 			freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1087 
1088 		if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1089 			if (nl80211_msg_put_wmm_rules(msg, rule))
1090 				goto nla_put_failure;
1091 		}
1092 	}
1093 
1094 	return 0;
1095 
1096  nla_put_failure:
1097 	return -ENOBUFS;
1098 }
1099 
nl80211_put_txq_stats(struct sk_buff * msg,struct cfg80211_txq_stats * txqstats,int attrtype)1100 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1101 				  struct cfg80211_txq_stats *txqstats,
1102 				  int attrtype)
1103 {
1104 	struct nlattr *txqattr;
1105 
1106 #define PUT_TXQVAL_U32(attr, memb) do {					  \
1107 	if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&	  \
1108 	    nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1109 		return false;						  \
1110 	} while (0)
1111 
1112 	txqattr = nla_nest_start_noflag(msg, attrtype);
1113 	if (!txqattr)
1114 		return false;
1115 
1116 	PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1117 	PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1118 	PUT_TXQVAL_U32(FLOWS, flows);
1119 	PUT_TXQVAL_U32(DROPS, drops);
1120 	PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1121 	PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1122 	PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1123 	PUT_TXQVAL_U32(COLLISIONS, collisions);
1124 	PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1125 	PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1126 	PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1127 	nla_nest_end(msg, txqattr);
1128 
1129 #undef PUT_TXQVAL_U32
1130 	return true;
1131 }
1132 
1133 /* netlink command implementations */
1134 
1135 struct key_parse {
1136 	struct key_params p;
1137 	int idx;
1138 	int type;
1139 	bool def, defmgmt, defbeacon;
1140 	bool def_uni, def_multi;
1141 };
1142 
nl80211_parse_key_new(struct genl_info * info,struct nlattr * key,struct key_parse * k)1143 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1144 				 struct key_parse *k)
1145 {
1146 	struct nlattr *tb[NL80211_KEY_MAX + 1];
1147 	int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1148 					      nl80211_key_policy,
1149 					      info->extack);
1150 	if (err)
1151 		return err;
1152 
1153 	k->def = !!tb[NL80211_KEY_DEFAULT];
1154 	k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1155 	k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1156 
1157 	if (k->def) {
1158 		k->def_uni = true;
1159 		k->def_multi = true;
1160 	}
1161 	if (k->defmgmt || k->defbeacon)
1162 		k->def_multi = true;
1163 
1164 	if (tb[NL80211_KEY_IDX])
1165 		k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1166 
1167 	if (tb[NL80211_KEY_DATA]) {
1168 		k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1169 		k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1170 	}
1171 
1172 	if (tb[NL80211_KEY_SEQ]) {
1173 		k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1174 		k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1175 	}
1176 
1177 	if (tb[NL80211_KEY_CIPHER])
1178 		k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1179 
1180 	if (tb[NL80211_KEY_TYPE])
1181 		k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1182 
1183 	if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1184 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1185 
1186 		err = nla_parse_nested_deprecated(kdt,
1187 						  NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1188 						  tb[NL80211_KEY_DEFAULT_TYPES],
1189 						  nl80211_key_default_policy,
1190 						  info->extack);
1191 		if (err)
1192 			return err;
1193 
1194 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1195 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1196 	}
1197 
1198 	if (tb[NL80211_KEY_MODE])
1199 		k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1200 
1201 	return 0;
1202 }
1203 
nl80211_parse_key_old(struct genl_info * info,struct key_parse * k)1204 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1205 {
1206 	if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1207 		k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1208 		k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1209 	}
1210 
1211 	if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1212 		k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1213 		k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1214 	}
1215 
1216 	if (info->attrs[NL80211_ATTR_KEY_IDX])
1217 		k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1218 
1219 	if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1220 		k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1221 
1222 	k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1223 	k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1224 
1225 	if (k->def) {
1226 		k->def_uni = true;
1227 		k->def_multi = true;
1228 	}
1229 	if (k->defmgmt)
1230 		k->def_multi = true;
1231 
1232 	if (info->attrs[NL80211_ATTR_KEY_TYPE])
1233 		k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1234 
1235 	if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1236 		struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1237 		int err = nla_parse_nested_deprecated(kdt,
1238 						      NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1239 						      info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1240 						      nl80211_key_default_policy,
1241 						      info->extack);
1242 		if (err)
1243 			return err;
1244 
1245 		k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1246 		k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1247 	}
1248 
1249 	return 0;
1250 }
1251 
nl80211_parse_key(struct genl_info * info,struct key_parse * k)1252 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1253 {
1254 	int err;
1255 
1256 	memset(k, 0, sizeof(*k));
1257 	k->idx = -1;
1258 	k->type = -1;
1259 
1260 	if (info->attrs[NL80211_ATTR_KEY])
1261 		err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1262 	else
1263 		err = nl80211_parse_key_old(info, k);
1264 
1265 	if (err)
1266 		return err;
1267 
1268 	if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1269 	    (k->defbeacon ? 1 : 0) > 1) {
1270 		GENL_SET_ERR_MSG(info,
1271 				 "key with multiple default flags is invalid");
1272 		return -EINVAL;
1273 	}
1274 
1275 	if (k->defmgmt || k->defbeacon) {
1276 		if (k->def_uni || !k->def_multi) {
1277 			GENL_SET_ERR_MSG(info,
1278 					 "defmgmt/defbeacon key must be mcast");
1279 			return -EINVAL;
1280 		}
1281 	}
1282 
1283 	if (k->idx != -1) {
1284 		if (k->defmgmt) {
1285 			if (k->idx < 4 || k->idx > 5) {
1286 				GENL_SET_ERR_MSG(info,
1287 						 "defmgmt key idx not 4 or 5");
1288 				return -EINVAL;
1289 			}
1290 		} else if (k->defbeacon) {
1291 			if (k->idx < 6 || k->idx > 7) {
1292 				GENL_SET_ERR_MSG(info,
1293 						 "defbeacon key idx not 6 or 7");
1294 				return -EINVAL;
1295 			}
1296 		} else if (k->def) {
1297 			if (k->idx < 0 || k->idx > 3) {
1298 				GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1299 				return -EINVAL;
1300 			}
1301 		} else {
1302 			if (k->idx < 0 || k->idx > 7) {
1303 				GENL_SET_ERR_MSG(info, "key idx not 0-7");
1304 				return -EINVAL;
1305 			}
1306 		}
1307 	}
1308 
1309 	return 0;
1310 }
1311 
1312 static struct cfg80211_cached_keys *
nl80211_parse_connkeys(struct cfg80211_registered_device * rdev,struct genl_info * info,bool * no_ht)1313 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1314 		       struct genl_info *info, bool *no_ht)
1315 {
1316 	struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1317 	struct key_parse parse;
1318 	struct nlattr *key;
1319 	struct cfg80211_cached_keys *result;
1320 	int rem, err, def = 0;
1321 	bool have_key = false;
1322 
1323 	nla_for_each_nested(key, keys, rem) {
1324 		have_key = true;
1325 		break;
1326 	}
1327 
1328 	if (!have_key)
1329 		return NULL;
1330 
1331 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1332 	if (!result)
1333 		return ERR_PTR(-ENOMEM);
1334 
1335 	result->def = -1;
1336 
1337 	nla_for_each_nested(key, keys, rem) {
1338 		memset(&parse, 0, sizeof(parse));
1339 		parse.idx = -1;
1340 
1341 		err = nl80211_parse_key_new(info, key, &parse);
1342 		if (err)
1343 			goto error;
1344 		err = -EINVAL;
1345 		if (!parse.p.key)
1346 			goto error;
1347 		if (parse.idx < 0 || parse.idx > 3) {
1348 			GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1349 			goto error;
1350 		}
1351 		if (parse.def) {
1352 			if (def) {
1353 				GENL_SET_ERR_MSG(info,
1354 						 "only one key can be default");
1355 				goto error;
1356 			}
1357 			def = 1;
1358 			result->def = parse.idx;
1359 			if (!parse.def_uni || !parse.def_multi)
1360 				goto error;
1361 		} else if (parse.defmgmt)
1362 			goto error;
1363 		err = cfg80211_validate_key_settings(rdev, &parse.p,
1364 						     parse.idx, false, NULL);
1365 		if (err)
1366 			goto error;
1367 		if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1368 		    parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1369 			GENL_SET_ERR_MSG(info, "connect key must be WEP");
1370 			err = -EINVAL;
1371 			goto error;
1372 		}
1373 		result->params[parse.idx].cipher = parse.p.cipher;
1374 		result->params[parse.idx].key_len = parse.p.key_len;
1375 		result->params[parse.idx].key = result->data[parse.idx];
1376 		memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1377 
1378 		/* must be WEP key if we got here */
1379 		if (no_ht)
1380 			*no_ht = true;
1381 	}
1382 
1383 	if (result->def < 0) {
1384 		err = -EINVAL;
1385 		GENL_SET_ERR_MSG(info, "need a default/TX key");
1386 		goto error;
1387 	}
1388 
1389 	return result;
1390  error:
1391 	kfree(result);
1392 	return ERR_PTR(err);
1393 }
1394 
nl80211_key_allowed(struct wireless_dev * wdev)1395 static int nl80211_key_allowed(struct wireless_dev *wdev)
1396 {
1397 	ASSERT_WDEV_LOCK(wdev);
1398 
1399 	switch (wdev->iftype) {
1400 	case NL80211_IFTYPE_AP:
1401 	case NL80211_IFTYPE_AP_VLAN:
1402 	case NL80211_IFTYPE_P2P_GO:
1403 	case NL80211_IFTYPE_MESH_POINT:
1404 		break;
1405 	case NL80211_IFTYPE_ADHOC:
1406 	case NL80211_IFTYPE_STATION:
1407 	case NL80211_IFTYPE_P2P_CLIENT:
1408 		if (!wdev->current_bss)
1409 			return -ENOLINK;
1410 		break;
1411 	case NL80211_IFTYPE_UNSPECIFIED:
1412 	case NL80211_IFTYPE_OCB:
1413 	case NL80211_IFTYPE_MONITOR:
1414 	case NL80211_IFTYPE_NAN:
1415 	case NL80211_IFTYPE_P2P_DEVICE:
1416 	case NL80211_IFTYPE_WDS:
1417 	case NUM_NL80211_IFTYPES:
1418 		return -EINVAL;
1419 	}
1420 
1421 	return 0;
1422 }
1423 
nl80211_get_valid_chan(struct wiphy * wiphy,u32 freq)1424 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1425 							u32 freq)
1426 {
1427 	struct ieee80211_channel *chan;
1428 
1429 	chan = ieee80211_get_channel_khz(wiphy, freq);
1430 	if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1431 		return NULL;
1432 	return chan;
1433 }
1434 
nl80211_put_iftypes(struct sk_buff * msg,u32 attr,u16 ifmodes)1435 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1436 {
1437 	struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1438 	int i;
1439 
1440 	if (!nl_modes)
1441 		goto nla_put_failure;
1442 
1443 	i = 0;
1444 	while (ifmodes) {
1445 		if ((ifmodes & 1) && nla_put_flag(msg, i))
1446 			goto nla_put_failure;
1447 		ifmodes >>= 1;
1448 		i++;
1449 	}
1450 
1451 	nla_nest_end(msg, nl_modes);
1452 	return 0;
1453 
1454 nla_put_failure:
1455 	return -ENOBUFS;
1456 }
1457 
nl80211_put_iface_combinations(struct wiphy * wiphy,struct sk_buff * msg,bool large)1458 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1459 					  struct sk_buff *msg,
1460 					  bool large)
1461 {
1462 	struct nlattr *nl_combis;
1463 	int i, j;
1464 
1465 	nl_combis = nla_nest_start_noflag(msg,
1466 					  NL80211_ATTR_INTERFACE_COMBINATIONS);
1467 	if (!nl_combis)
1468 		goto nla_put_failure;
1469 
1470 	for (i = 0; i < wiphy->n_iface_combinations; i++) {
1471 		const struct ieee80211_iface_combination *c;
1472 		struct nlattr *nl_combi, *nl_limits;
1473 
1474 		c = &wiphy->iface_combinations[i];
1475 
1476 		nl_combi = nla_nest_start_noflag(msg, i + 1);
1477 		if (!nl_combi)
1478 			goto nla_put_failure;
1479 
1480 		nl_limits = nla_nest_start_noflag(msg,
1481 						  NL80211_IFACE_COMB_LIMITS);
1482 		if (!nl_limits)
1483 			goto nla_put_failure;
1484 
1485 		for (j = 0; j < c->n_limits; j++) {
1486 			struct nlattr *nl_limit;
1487 
1488 			nl_limit = nla_nest_start_noflag(msg, j + 1);
1489 			if (!nl_limit)
1490 				goto nla_put_failure;
1491 			if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1492 					c->limits[j].max))
1493 				goto nla_put_failure;
1494 			if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1495 						c->limits[j].types))
1496 				goto nla_put_failure;
1497 			nla_nest_end(msg, nl_limit);
1498 		}
1499 
1500 		nla_nest_end(msg, nl_limits);
1501 
1502 		if (c->beacon_int_infra_match &&
1503 		    nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1504 			goto nla_put_failure;
1505 		if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1506 				c->num_different_channels) ||
1507 		    nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1508 				c->max_interfaces))
1509 			goto nla_put_failure;
1510 		if (large &&
1511 		    (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1512 				c->radar_detect_widths) ||
1513 		     nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1514 				c->radar_detect_regions)))
1515 			goto nla_put_failure;
1516 		if (c->beacon_int_min_gcd &&
1517 		    nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1518 				c->beacon_int_min_gcd))
1519 			goto nla_put_failure;
1520 
1521 		nla_nest_end(msg, nl_combi);
1522 	}
1523 
1524 	nla_nest_end(msg, nl_combis);
1525 
1526 	return 0;
1527 nla_put_failure:
1528 	return -ENOBUFS;
1529 }
1530 
1531 #ifdef CONFIG_PM
nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1532 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1533 					struct sk_buff *msg)
1534 {
1535 	const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1536 	struct nlattr *nl_tcp;
1537 
1538 	if (!tcp)
1539 		return 0;
1540 
1541 	nl_tcp = nla_nest_start_noflag(msg,
1542 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1543 	if (!nl_tcp)
1544 		return -ENOBUFS;
1545 
1546 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1547 			tcp->data_payload_max))
1548 		return -ENOBUFS;
1549 
1550 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1551 			tcp->data_payload_max))
1552 		return -ENOBUFS;
1553 
1554 	if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1555 		return -ENOBUFS;
1556 
1557 	if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1558 				sizeof(*tcp->tok), tcp->tok))
1559 		return -ENOBUFS;
1560 
1561 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1562 			tcp->data_interval_max))
1563 		return -ENOBUFS;
1564 
1565 	if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1566 			tcp->wake_payload_max))
1567 		return -ENOBUFS;
1568 
1569 	nla_nest_end(msg, nl_tcp);
1570 	return 0;
1571 }
1572 
nl80211_send_wowlan(struct sk_buff * msg,struct cfg80211_registered_device * rdev,bool large)1573 static int nl80211_send_wowlan(struct sk_buff *msg,
1574 			       struct cfg80211_registered_device *rdev,
1575 			       bool large)
1576 {
1577 	struct nlattr *nl_wowlan;
1578 
1579 	if (!rdev->wiphy.wowlan)
1580 		return 0;
1581 
1582 	nl_wowlan = nla_nest_start_noflag(msg,
1583 					  NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1584 	if (!nl_wowlan)
1585 		return -ENOBUFS;
1586 
1587 	if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1588 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1589 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1590 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1591 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1592 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1593 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1594 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1595 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1596 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1597 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1598 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1599 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1600 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1601 	    ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1602 	     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1603 		return -ENOBUFS;
1604 
1605 	if (rdev->wiphy.wowlan->n_patterns) {
1606 		struct nl80211_pattern_support pat = {
1607 			.max_patterns = rdev->wiphy.wowlan->n_patterns,
1608 			.min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1609 			.max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1610 			.max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1611 		};
1612 
1613 		if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1614 			    sizeof(pat), &pat))
1615 			return -ENOBUFS;
1616 	}
1617 
1618 	if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1619 	    nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1620 			rdev->wiphy.wowlan->max_nd_match_sets))
1621 		return -ENOBUFS;
1622 
1623 	if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1624 		return -ENOBUFS;
1625 
1626 	nla_nest_end(msg, nl_wowlan);
1627 
1628 	return 0;
1629 }
1630 #endif
1631 
nl80211_send_coalesce(struct sk_buff * msg,struct cfg80211_registered_device * rdev)1632 static int nl80211_send_coalesce(struct sk_buff *msg,
1633 				 struct cfg80211_registered_device *rdev)
1634 {
1635 	struct nl80211_coalesce_rule_support rule;
1636 
1637 	if (!rdev->wiphy.coalesce)
1638 		return 0;
1639 
1640 	rule.max_rules = rdev->wiphy.coalesce->n_rules;
1641 	rule.max_delay = rdev->wiphy.coalesce->max_delay;
1642 	rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1643 	rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1644 	rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1645 	rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1646 
1647 	if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1648 		return -ENOBUFS;
1649 
1650 	return 0;
1651 }
1652 
1653 static int
nl80211_send_iftype_data(struct sk_buff * msg,const struct ieee80211_supported_band * sband,const struct ieee80211_sband_iftype_data * iftdata)1654 nl80211_send_iftype_data(struct sk_buff *msg,
1655 			 const struct ieee80211_supported_band *sband,
1656 			 const struct ieee80211_sband_iftype_data *iftdata)
1657 {
1658 	const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1659 
1660 	if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1661 				iftdata->types_mask))
1662 		return -ENOBUFS;
1663 
1664 	if (he_cap->has_he) {
1665 		if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1666 			    sizeof(he_cap->he_cap_elem.mac_cap_info),
1667 			    he_cap->he_cap_elem.mac_cap_info) ||
1668 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1669 			    sizeof(he_cap->he_cap_elem.phy_cap_info),
1670 			    he_cap->he_cap_elem.phy_cap_info) ||
1671 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1672 			    sizeof(he_cap->he_mcs_nss_supp),
1673 			    &he_cap->he_mcs_nss_supp) ||
1674 		    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1675 			    sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1676 			return -ENOBUFS;
1677 	}
1678 
1679 	if (sband->band == NL80211_BAND_6GHZ &&
1680 	    nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1681 		    sizeof(iftdata->he_6ghz_capa),
1682 		    &iftdata->he_6ghz_capa))
1683 		return -ENOBUFS;
1684 
1685 	return 0;
1686 }
1687 
nl80211_send_band_rateinfo(struct sk_buff * msg,struct ieee80211_supported_band * sband,bool large)1688 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1689 				      struct ieee80211_supported_band *sband,
1690 				      bool large)
1691 {
1692 	struct nlattr *nl_rates, *nl_rate;
1693 	struct ieee80211_rate *rate;
1694 	int i;
1695 
1696 	/* add HT info */
1697 	if (sband->ht_cap.ht_supported &&
1698 	    (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1699 		     sizeof(sband->ht_cap.mcs),
1700 		     &sband->ht_cap.mcs) ||
1701 	     nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1702 			 sband->ht_cap.cap) ||
1703 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1704 			sband->ht_cap.ampdu_factor) ||
1705 	     nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1706 			sband->ht_cap.ampdu_density)))
1707 		return -ENOBUFS;
1708 
1709 	/* add VHT info */
1710 	if (sband->vht_cap.vht_supported &&
1711 	    (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1712 		     sizeof(sband->vht_cap.vht_mcs),
1713 		     &sband->vht_cap.vht_mcs) ||
1714 	     nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1715 			 sband->vht_cap.cap)))
1716 		return -ENOBUFS;
1717 
1718 	if (large && sband->n_iftype_data) {
1719 		struct nlattr *nl_iftype_data =
1720 			nla_nest_start_noflag(msg,
1721 					      NL80211_BAND_ATTR_IFTYPE_DATA);
1722 		int err;
1723 
1724 		if (!nl_iftype_data)
1725 			return -ENOBUFS;
1726 
1727 		for (i = 0; i < sband->n_iftype_data; i++) {
1728 			struct nlattr *iftdata;
1729 
1730 			iftdata = nla_nest_start_noflag(msg, i + 1);
1731 			if (!iftdata)
1732 				return -ENOBUFS;
1733 
1734 			err = nl80211_send_iftype_data(msg, sband,
1735 						       &sband->iftype_data[i]);
1736 			if (err)
1737 				return err;
1738 
1739 			nla_nest_end(msg, iftdata);
1740 		}
1741 
1742 		nla_nest_end(msg, nl_iftype_data);
1743 	}
1744 
1745 	/* add EDMG info */
1746 	if (large && sband->edmg_cap.channels &&
1747 	    (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1748 		       sband->edmg_cap.channels) ||
1749 	    nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1750 		       sband->edmg_cap.bw_config)))
1751 
1752 		return -ENOBUFS;
1753 
1754 	/* add bitrates */
1755 	nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1756 	if (!nl_rates)
1757 		return -ENOBUFS;
1758 
1759 	for (i = 0; i < sband->n_bitrates; i++) {
1760 		nl_rate = nla_nest_start_noflag(msg, i);
1761 		if (!nl_rate)
1762 			return -ENOBUFS;
1763 
1764 		rate = &sband->bitrates[i];
1765 		if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1766 				rate->bitrate))
1767 			return -ENOBUFS;
1768 		if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1769 		    nla_put_flag(msg,
1770 				 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1771 			return -ENOBUFS;
1772 
1773 		nla_nest_end(msg, nl_rate);
1774 	}
1775 
1776 	nla_nest_end(msg, nl_rates);
1777 
1778 	return 0;
1779 }
1780 
1781 static int
nl80211_send_mgmt_stypes(struct sk_buff * msg,const struct ieee80211_txrx_stypes * mgmt_stypes)1782 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1783 			 const struct ieee80211_txrx_stypes *mgmt_stypes)
1784 {
1785 	u16 stypes;
1786 	struct nlattr *nl_ftypes, *nl_ifs;
1787 	enum nl80211_iftype ift;
1788 	int i;
1789 
1790 	if (!mgmt_stypes)
1791 		return 0;
1792 
1793 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1794 	if (!nl_ifs)
1795 		return -ENOBUFS;
1796 
1797 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1798 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1799 		if (!nl_ftypes)
1800 			return -ENOBUFS;
1801 		i = 0;
1802 		stypes = mgmt_stypes[ift].tx;
1803 		while (stypes) {
1804 			if ((stypes & 1) &&
1805 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1806 					(i << 4) | IEEE80211_FTYPE_MGMT))
1807 				return -ENOBUFS;
1808 			stypes >>= 1;
1809 			i++;
1810 		}
1811 		nla_nest_end(msg, nl_ftypes);
1812 	}
1813 
1814 	nla_nest_end(msg, nl_ifs);
1815 
1816 	nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1817 	if (!nl_ifs)
1818 		return -ENOBUFS;
1819 
1820 	for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1821 		nl_ftypes = nla_nest_start_noflag(msg, ift);
1822 		if (!nl_ftypes)
1823 			return -ENOBUFS;
1824 		i = 0;
1825 		stypes = mgmt_stypes[ift].rx;
1826 		while (stypes) {
1827 			if ((stypes & 1) &&
1828 			    nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1829 					(i << 4) | IEEE80211_FTYPE_MGMT))
1830 				return -ENOBUFS;
1831 			stypes >>= 1;
1832 			i++;
1833 		}
1834 		nla_nest_end(msg, nl_ftypes);
1835 	}
1836 	nla_nest_end(msg, nl_ifs);
1837 
1838 	return 0;
1839 }
1840 
1841 #define CMD(op, n)							\
1842 	 do {								\
1843 		if (rdev->ops->op) {					\
1844 			i++;						\
1845 			if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) 	\
1846 				goto nla_put_failure;			\
1847 		}							\
1848 	} while (0)
1849 
nl80211_add_commands_unsplit(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1850 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1851 					struct sk_buff *msg)
1852 {
1853 	int i = 0;
1854 
1855 	/*
1856 	 * do *NOT* add anything into this function, new things need to be
1857 	 * advertised only to new versions of userspace that can deal with
1858 	 * the split (and they can't possibly care about new features...
1859 	 */
1860 	CMD(add_virtual_intf, NEW_INTERFACE);
1861 	CMD(change_virtual_intf, SET_INTERFACE);
1862 	CMD(add_key, NEW_KEY);
1863 	CMD(start_ap, START_AP);
1864 	CMD(add_station, NEW_STATION);
1865 	CMD(add_mpath, NEW_MPATH);
1866 	CMD(update_mesh_config, SET_MESH_CONFIG);
1867 	CMD(change_bss, SET_BSS);
1868 	CMD(auth, AUTHENTICATE);
1869 	CMD(assoc, ASSOCIATE);
1870 	CMD(deauth, DEAUTHENTICATE);
1871 	CMD(disassoc, DISASSOCIATE);
1872 	CMD(join_ibss, JOIN_IBSS);
1873 	CMD(join_mesh, JOIN_MESH);
1874 	CMD(set_pmksa, SET_PMKSA);
1875 	CMD(del_pmksa, DEL_PMKSA);
1876 	CMD(flush_pmksa, FLUSH_PMKSA);
1877 	if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1878 		CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1879 	CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1880 	CMD(mgmt_tx, FRAME);
1881 	CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1882 	if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1883 		i++;
1884 		if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1885 			goto nla_put_failure;
1886 	}
1887 	if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1888 	    rdev->ops->join_mesh) {
1889 		i++;
1890 		if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1891 			goto nla_put_failure;
1892 	}
1893 	CMD(set_wds_peer, SET_WDS_PEER);
1894 	if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1895 		CMD(tdls_mgmt, TDLS_MGMT);
1896 		CMD(tdls_oper, TDLS_OPER);
1897 	}
1898 	if (rdev->wiphy.max_sched_scan_reqs)
1899 		CMD(sched_scan_start, START_SCHED_SCAN);
1900 	CMD(probe_client, PROBE_CLIENT);
1901 	CMD(set_noack_map, SET_NOACK_MAP);
1902 	if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1903 		i++;
1904 		if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1905 			goto nla_put_failure;
1906 	}
1907 	CMD(start_p2p_device, START_P2P_DEVICE);
1908 	CMD(set_mcast_rate, SET_MCAST_RATE);
1909 #ifdef CONFIG_NL80211_TESTMODE
1910 	CMD(testmode_cmd, TESTMODE);
1911 #endif
1912 
1913 	if (rdev->ops->connect || rdev->ops->auth) {
1914 		i++;
1915 		if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1916 			goto nla_put_failure;
1917 	}
1918 
1919 	if (rdev->ops->disconnect || rdev->ops->deauth) {
1920 		i++;
1921 		if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1922 			goto nla_put_failure;
1923 	}
1924 
1925 	return i;
1926  nla_put_failure:
1927 	return -ENOBUFS;
1928 }
1929 
1930 static int
nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities * cap,struct sk_buff * msg)1931 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1932 			   struct sk_buff *msg)
1933 {
1934 	struct nlattr *ftm;
1935 
1936 	if (!cap->ftm.supported)
1937 		return 0;
1938 
1939 	ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1940 	if (!ftm)
1941 		return -ENOBUFS;
1942 
1943 	if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1944 		return -ENOBUFS;
1945 	if (cap->ftm.non_asap &&
1946 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1947 		return -ENOBUFS;
1948 	if (cap->ftm.request_lci &&
1949 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1950 		return -ENOBUFS;
1951 	if (cap->ftm.request_civicloc &&
1952 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1953 		return -ENOBUFS;
1954 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1955 			cap->ftm.preambles))
1956 		return -ENOBUFS;
1957 	if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1958 			cap->ftm.bandwidths))
1959 		return -ENOBUFS;
1960 	if (cap->ftm.max_bursts_exponent >= 0 &&
1961 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1962 			cap->ftm.max_bursts_exponent))
1963 		return -ENOBUFS;
1964 	if (cap->ftm.max_ftms_per_burst &&
1965 	    nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1966 			cap->ftm.max_ftms_per_burst))
1967 		return -ENOBUFS;
1968 	if (cap->ftm.trigger_based &&
1969 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
1970 		return -ENOBUFS;
1971 	if (cap->ftm.non_trigger_based &&
1972 	    nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
1973 		return -ENOBUFS;
1974 
1975 	nla_nest_end(msg, ftm);
1976 	return 0;
1977 }
1978 
nl80211_send_pmsr_capa(struct cfg80211_registered_device * rdev,struct sk_buff * msg)1979 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1980 				  struct sk_buff *msg)
1981 {
1982 	const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1983 	struct nlattr *pmsr, *caps;
1984 
1985 	if (!cap)
1986 		return 0;
1987 
1988 	/*
1989 	 * we don't need to clean up anything here since the caller
1990 	 * will genlmsg_cancel() if we fail
1991 	 */
1992 
1993 	pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1994 	if (!pmsr)
1995 		return -ENOBUFS;
1996 
1997 	if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1998 		return -ENOBUFS;
1999 
2000 	if (cap->report_ap_tsf &&
2001 	    nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2002 		return -ENOBUFS;
2003 
2004 	if (cap->randomize_mac_addr &&
2005 	    nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2006 		return -ENOBUFS;
2007 
2008 	caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2009 	if (!caps)
2010 		return -ENOBUFS;
2011 
2012 	if (nl80211_send_pmsr_ftm_capa(cap, msg))
2013 		return -ENOBUFS;
2014 
2015 	nla_nest_end(msg, caps);
2016 	nla_nest_end(msg, pmsr);
2017 
2018 	return 0;
2019 }
2020 
2021 static int
nl80211_put_iftype_akm_suites(struct cfg80211_registered_device * rdev,struct sk_buff * msg)2022 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2023 			      struct sk_buff *msg)
2024 {
2025 	int i;
2026 	struct nlattr *nested, *nested_akms;
2027 	const struct wiphy_iftype_akm_suites *iftype_akms;
2028 
2029 	if (!rdev->wiphy.num_iftype_akm_suites ||
2030 	    !rdev->wiphy.iftype_akm_suites)
2031 		return 0;
2032 
2033 	nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2034 	if (!nested)
2035 		return -ENOBUFS;
2036 
2037 	for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2038 		nested_akms = nla_nest_start(msg, i + 1);
2039 		if (!nested_akms)
2040 			return -ENOBUFS;
2041 
2042 		iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2043 
2044 		if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2045 					iftype_akms->iftypes_mask))
2046 			return -ENOBUFS;
2047 
2048 		if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2049 			    sizeof(u32) * iftype_akms->n_akm_suites,
2050 			    iftype_akms->akm_suites)) {
2051 			return -ENOBUFS;
2052 		}
2053 		nla_nest_end(msg, nested_akms);
2054 	}
2055 
2056 	nla_nest_end(msg, nested);
2057 
2058 	return 0;
2059 }
2060 
2061 static int
nl80211_put_tid_config_support(struct cfg80211_registered_device * rdev,struct sk_buff * msg)2062 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2063 			       struct sk_buff *msg)
2064 {
2065 	struct nlattr *supp;
2066 
2067 	if (!rdev->wiphy.tid_config_support.vif &&
2068 	    !rdev->wiphy.tid_config_support.peer)
2069 		return 0;
2070 
2071 	supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2072 	if (!supp)
2073 		return -ENOSPC;
2074 
2075 	if (rdev->wiphy.tid_config_support.vif &&
2076 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2077 			      rdev->wiphy.tid_config_support.vif,
2078 			      NL80211_TID_CONFIG_ATTR_PAD))
2079 		goto fail;
2080 
2081 	if (rdev->wiphy.tid_config_support.peer &&
2082 	    nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2083 			      rdev->wiphy.tid_config_support.peer,
2084 			      NL80211_TID_CONFIG_ATTR_PAD))
2085 		goto fail;
2086 
2087 	/* for now we just use the same value ... makes more sense */
2088 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2089 		       rdev->wiphy.tid_config_support.max_retry))
2090 		goto fail;
2091 	if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2092 		       rdev->wiphy.tid_config_support.max_retry))
2093 		goto fail;
2094 
2095 	nla_nest_end(msg, supp);
2096 
2097 	return 0;
2098 fail:
2099 	nla_nest_cancel(msg, supp);
2100 	return -ENOBUFS;
2101 }
2102 
2103 struct nl80211_dump_wiphy_state {
2104 	s64 filter_wiphy;
2105 	long start;
2106 	long split_start, band_start, chan_start, capa_start;
2107 	bool split;
2108 };
2109 
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)2110 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2111 			      enum nl80211_commands cmd,
2112 			      struct sk_buff *msg, u32 portid, u32 seq,
2113 			      int flags, struct nl80211_dump_wiphy_state *state)
2114 {
2115 	void *hdr;
2116 	struct nlattr *nl_bands, *nl_band;
2117 	struct nlattr *nl_freqs, *nl_freq;
2118 	struct nlattr *nl_cmds;
2119 	enum nl80211_band band;
2120 	struct ieee80211_channel *chan;
2121 	int i;
2122 	const struct ieee80211_txrx_stypes *mgmt_stypes =
2123 				rdev->wiphy.mgmt_stypes;
2124 	u32 features;
2125 
2126 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2127 	if (!hdr)
2128 		return -ENOBUFS;
2129 
2130 	if (WARN_ON(!state))
2131 		return -EINVAL;
2132 
2133 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2134 	    nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2135 			   wiphy_name(&rdev->wiphy)) ||
2136 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
2137 			cfg80211_rdev_list_generation))
2138 		goto nla_put_failure;
2139 
2140 	if (cmd != NL80211_CMD_NEW_WIPHY)
2141 		goto finish;
2142 
2143 	switch (state->split_start) {
2144 	case 0:
2145 		if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2146 			       rdev->wiphy.retry_short) ||
2147 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2148 			       rdev->wiphy.retry_long) ||
2149 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2150 				rdev->wiphy.frag_threshold) ||
2151 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2152 				rdev->wiphy.rts_threshold) ||
2153 		    nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2154 			       rdev->wiphy.coverage_class) ||
2155 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2156 			       rdev->wiphy.max_scan_ssids) ||
2157 		    nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2158 			       rdev->wiphy.max_sched_scan_ssids) ||
2159 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2160 				rdev->wiphy.max_scan_ie_len) ||
2161 		    nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2162 				rdev->wiphy.max_sched_scan_ie_len) ||
2163 		    nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2164 			       rdev->wiphy.max_match_sets))
2165 			goto nla_put_failure;
2166 
2167 		if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2168 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2169 			goto nla_put_failure;
2170 		if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2171 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2172 			goto nla_put_failure;
2173 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2174 		    nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2175 			goto nla_put_failure;
2176 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2177 		    nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2178 			goto nla_put_failure;
2179 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2180 		    nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2181 			goto nla_put_failure;
2182 		if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2183 		    nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2184 			goto nla_put_failure;
2185 		state->split_start++;
2186 		if (state->split)
2187 			break;
2188 		fallthrough;
2189 	case 1:
2190 		if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2191 			    sizeof(u32) * rdev->wiphy.n_cipher_suites,
2192 			    rdev->wiphy.cipher_suites))
2193 			goto nla_put_failure;
2194 
2195 		if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2196 			       rdev->wiphy.max_num_pmkids))
2197 			goto nla_put_failure;
2198 
2199 		if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2200 		    nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2201 			goto nla_put_failure;
2202 
2203 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2204 				rdev->wiphy.available_antennas_tx) ||
2205 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2206 				rdev->wiphy.available_antennas_rx))
2207 			goto nla_put_failure;
2208 
2209 		if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2210 		    nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2211 				rdev->wiphy.probe_resp_offload))
2212 			goto nla_put_failure;
2213 
2214 		if ((rdev->wiphy.available_antennas_tx ||
2215 		     rdev->wiphy.available_antennas_rx) &&
2216 		    rdev->ops->get_antenna) {
2217 			u32 tx_ant = 0, rx_ant = 0;
2218 			int res;
2219 
2220 			res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2221 			if (!res) {
2222 				if (nla_put_u32(msg,
2223 						NL80211_ATTR_WIPHY_ANTENNA_TX,
2224 						tx_ant) ||
2225 				    nla_put_u32(msg,
2226 						NL80211_ATTR_WIPHY_ANTENNA_RX,
2227 						rx_ant))
2228 					goto nla_put_failure;
2229 			}
2230 		}
2231 
2232 		state->split_start++;
2233 		if (state->split)
2234 			break;
2235 		fallthrough;
2236 	case 2:
2237 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2238 					rdev->wiphy.interface_modes))
2239 				goto nla_put_failure;
2240 		state->split_start++;
2241 		if (state->split)
2242 			break;
2243 		fallthrough;
2244 	case 3:
2245 		nl_bands = nla_nest_start_noflag(msg,
2246 						 NL80211_ATTR_WIPHY_BANDS);
2247 		if (!nl_bands)
2248 			goto nla_put_failure;
2249 
2250 		for (band = state->band_start;
2251 		     band < NUM_NL80211_BANDS; band++) {
2252 			struct ieee80211_supported_band *sband;
2253 
2254 			/* omit higher bands for ancient software */
2255 			if (band > NL80211_BAND_5GHZ && !state->split)
2256 				break;
2257 
2258 			sband = rdev->wiphy.bands[band];
2259 
2260 			if (!sband)
2261 				continue;
2262 
2263 			nl_band = nla_nest_start_noflag(msg, band);
2264 			if (!nl_band)
2265 				goto nla_put_failure;
2266 
2267 			switch (state->chan_start) {
2268 			case 0:
2269 				if (nl80211_send_band_rateinfo(msg, sband,
2270 							       state->split))
2271 					goto nla_put_failure;
2272 				state->chan_start++;
2273 				if (state->split)
2274 					break;
2275 				fallthrough;
2276 			default:
2277 				/* add frequencies */
2278 				nl_freqs = nla_nest_start_noflag(msg,
2279 								 NL80211_BAND_ATTR_FREQS);
2280 				if (!nl_freqs)
2281 					goto nla_put_failure;
2282 
2283 				for (i = state->chan_start - 1;
2284 				     i < sband->n_channels;
2285 				     i++) {
2286 					nl_freq = nla_nest_start_noflag(msg,
2287 									i);
2288 					if (!nl_freq)
2289 						goto nla_put_failure;
2290 
2291 					chan = &sband->channels[i];
2292 
2293 					if (nl80211_msg_put_channel(
2294 							msg, &rdev->wiphy, chan,
2295 							state->split))
2296 						goto nla_put_failure;
2297 
2298 					nla_nest_end(msg, nl_freq);
2299 					if (state->split)
2300 						break;
2301 				}
2302 				if (i < sband->n_channels)
2303 					state->chan_start = i + 2;
2304 				else
2305 					state->chan_start = 0;
2306 				nla_nest_end(msg, nl_freqs);
2307 			}
2308 
2309 			nla_nest_end(msg, nl_band);
2310 
2311 			if (state->split) {
2312 				/* start again here */
2313 				if (state->chan_start)
2314 					band--;
2315 				break;
2316 			}
2317 		}
2318 		nla_nest_end(msg, nl_bands);
2319 
2320 		if (band < NUM_NL80211_BANDS)
2321 			state->band_start = band + 1;
2322 		else
2323 			state->band_start = 0;
2324 
2325 		/* if bands & channels are done, continue outside */
2326 		if (state->band_start == 0 && state->chan_start == 0)
2327 			state->split_start++;
2328 		if (state->split)
2329 			break;
2330 		fallthrough;
2331 	case 4:
2332 		nl_cmds = nla_nest_start_noflag(msg,
2333 						NL80211_ATTR_SUPPORTED_COMMANDS);
2334 		if (!nl_cmds)
2335 			goto nla_put_failure;
2336 
2337 		i = nl80211_add_commands_unsplit(rdev, msg);
2338 		if (i < 0)
2339 			goto nla_put_failure;
2340 		if (state->split) {
2341 			CMD(crit_proto_start, CRIT_PROTOCOL_START);
2342 			CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2343 			if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2344 				CMD(channel_switch, CHANNEL_SWITCH);
2345 			CMD(set_qos_map, SET_QOS_MAP);
2346 			if (rdev->wiphy.features &
2347 					NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2348 				CMD(add_tx_ts, ADD_TX_TS);
2349 			CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2350 			CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2351 			CMD(update_ft_ies, UPDATE_FT_IES);
2352 		}
2353 #undef CMD
2354 
2355 		nla_nest_end(msg, nl_cmds);
2356 		state->split_start++;
2357 		if (state->split)
2358 			break;
2359 		fallthrough;
2360 	case 5:
2361 		if (rdev->ops->remain_on_channel &&
2362 		    (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2363 		    nla_put_u32(msg,
2364 				NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2365 				rdev->wiphy.max_remain_on_channel_duration))
2366 			goto nla_put_failure;
2367 
2368 		if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2369 		    nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2370 			goto nla_put_failure;
2371 
2372 		state->split_start++;
2373 		if (state->split)
2374 			break;
2375 		fallthrough;
2376 	case 6:
2377 #ifdef CONFIG_PM
2378 		if (nl80211_send_wowlan(msg, rdev, state->split))
2379 			goto nla_put_failure;
2380 		state->split_start++;
2381 		if (state->split)
2382 			break;
2383 #else
2384 		state->split_start++;
2385 #endif
2386 		fallthrough;
2387 	case 7:
2388 		if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2389 					rdev->wiphy.software_iftypes))
2390 			goto nla_put_failure;
2391 
2392 		if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2393 						   state->split))
2394 			goto nla_put_failure;
2395 
2396 		state->split_start++;
2397 		if (state->split)
2398 			break;
2399 		fallthrough;
2400 	case 8:
2401 		if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2402 		    nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2403 				rdev->wiphy.ap_sme_capa))
2404 			goto nla_put_failure;
2405 
2406 		features = rdev->wiphy.features;
2407 		/*
2408 		 * We can only add the per-channel limit information if the
2409 		 * dump is split, otherwise it makes it too big. Therefore
2410 		 * only advertise it in that case.
2411 		 */
2412 		if (state->split)
2413 			features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2414 		if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2415 			goto nla_put_failure;
2416 
2417 		if (rdev->wiphy.ht_capa_mod_mask &&
2418 		    nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2419 			    sizeof(*rdev->wiphy.ht_capa_mod_mask),
2420 			    rdev->wiphy.ht_capa_mod_mask))
2421 			goto nla_put_failure;
2422 
2423 		if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2424 		    rdev->wiphy.max_acl_mac_addrs &&
2425 		    nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2426 				rdev->wiphy.max_acl_mac_addrs))
2427 			goto nla_put_failure;
2428 
2429 		/*
2430 		 * Any information below this point is only available to
2431 		 * applications that can deal with it being split. This
2432 		 * helps ensure that newly added capabilities don't break
2433 		 * older tools by overrunning their buffers.
2434 		 *
2435 		 * We still increment split_start so that in the split
2436 		 * case we'll continue with more data in the next round,
2437 		 * but break unconditionally so unsplit data stops here.
2438 		 */
2439 		if (state->split)
2440 			state->split_start++;
2441 		else
2442 			state->split_start = 0;
2443 		break;
2444 	case 9:
2445 		if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2446 			goto nla_put_failure;
2447 
2448 		if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2449 				rdev->wiphy.max_sched_scan_plans) ||
2450 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2451 				rdev->wiphy.max_sched_scan_plan_interval) ||
2452 		    nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2453 				rdev->wiphy.max_sched_scan_plan_iterations))
2454 			goto nla_put_failure;
2455 
2456 		if (rdev->wiphy.extended_capabilities &&
2457 		    (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2458 			     rdev->wiphy.extended_capabilities_len,
2459 			     rdev->wiphy.extended_capabilities) ||
2460 		     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2461 			     rdev->wiphy.extended_capabilities_len,
2462 			     rdev->wiphy.extended_capabilities_mask)))
2463 			goto nla_put_failure;
2464 
2465 		if (rdev->wiphy.vht_capa_mod_mask &&
2466 		    nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2467 			    sizeof(*rdev->wiphy.vht_capa_mod_mask),
2468 			    rdev->wiphy.vht_capa_mod_mask))
2469 			goto nla_put_failure;
2470 
2471 		if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2472 			    rdev->wiphy.perm_addr))
2473 			goto nla_put_failure;
2474 
2475 		if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2476 		    nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2477 			    rdev->wiphy.addr_mask))
2478 			goto nla_put_failure;
2479 
2480 		if (rdev->wiphy.n_addresses > 1) {
2481 			void *attr;
2482 
2483 			attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2484 			if (!attr)
2485 				goto nla_put_failure;
2486 
2487 			for (i = 0; i < rdev->wiphy.n_addresses; i++)
2488 				if (nla_put(msg, i + 1, ETH_ALEN,
2489 					    rdev->wiphy.addresses[i].addr))
2490 					goto nla_put_failure;
2491 
2492 			nla_nest_end(msg, attr);
2493 		}
2494 
2495 		state->split_start++;
2496 		break;
2497 	case 10:
2498 		if (nl80211_send_coalesce(msg, rdev))
2499 			goto nla_put_failure;
2500 
2501 		if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2502 		    (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2503 		     nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2504 			goto nla_put_failure;
2505 
2506 		if (rdev->wiphy.max_ap_assoc_sta &&
2507 		    nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2508 				rdev->wiphy.max_ap_assoc_sta))
2509 			goto nla_put_failure;
2510 
2511 		state->split_start++;
2512 		break;
2513 	case 11:
2514 		if (rdev->wiphy.n_vendor_commands) {
2515 			const struct nl80211_vendor_cmd_info *info;
2516 			struct nlattr *nested;
2517 
2518 			nested = nla_nest_start_noflag(msg,
2519 						       NL80211_ATTR_VENDOR_DATA);
2520 			if (!nested)
2521 				goto nla_put_failure;
2522 
2523 			for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2524 				info = &rdev->wiphy.vendor_commands[i].info;
2525 				if (nla_put(msg, i + 1, sizeof(*info), info))
2526 					goto nla_put_failure;
2527 			}
2528 			nla_nest_end(msg, nested);
2529 		}
2530 
2531 		if (rdev->wiphy.n_vendor_events) {
2532 			const struct nl80211_vendor_cmd_info *info;
2533 			struct nlattr *nested;
2534 
2535 			nested = nla_nest_start_noflag(msg,
2536 						       NL80211_ATTR_VENDOR_EVENTS);
2537 			if (!nested)
2538 				goto nla_put_failure;
2539 
2540 			for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2541 				info = &rdev->wiphy.vendor_events[i];
2542 				if (nla_put(msg, i + 1, sizeof(*info), info))
2543 					goto nla_put_failure;
2544 			}
2545 			nla_nest_end(msg, nested);
2546 		}
2547 		state->split_start++;
2548 		break;
2549 	case 12:
2550 		if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2551 		    nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2552 			       rdev->wiphy.max_num_csa_counters))
2553 			goto nla_put_failure;
2554 
2555 		if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2556 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2557 			goto nla_put_failure;
2558 
2559 		if (rdev->wiphy.max_sched_scan_reqs &&
2560 		    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2561 				rdev->wiphy.max_sched_scan_reqs))
2562 			goto nla_put_failure;
2563 
2564 		if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2565 			    sizeof(rdev->wiphy.ext_features),
2566 			    rdev->wiphy.ext_features))
2567 			goto nla_put_failure;
2568 
2569 		if (rdev->wiphy.bss_select_support) {
2570 			struct nlattr *nested;
2571 			u32 bss_select_support = rdev->wiphy.bss_select_support;
2572 
2573 			nested = nla_nest_start_noflag(msg,
2574 						       NL80211_ATTR_BSS_SELECT);
2575 			if (!nested)
2576 				goto nla_put_failure;
2577 
2578 			i = 0;
2579 			while (bss_select_support) {
2580 				if ((bss_select_support & 1) &&
2581 				    nla_put_flag(msg, i))
2582 					goto nla_put_failure;
2583 				i++;
2584 				bss_select_support >>= 1;
2585 			}
2586 			nla_nest_end(msg, nested);
2587 		}
2588 
2589 		state->split_start++;
2590 		break;
2591 	case 13:
2592 		if (rdev->wiphy.num_iftype_ext_capab &&
2593 		    rdev->wiphy.iftype_ext_capab) {
2594 			struct nlattr *nested_ext_capab, *nested;
2595 
2596 			nested = nla_nest_start_noflag(msg,
2597 						       NL80211_ATTR_IFTYPE_EXT_CAPA);
2598 			if (!nested)
2599 				goto nla_put_failure;
2600 
2601 			for (i = state->capa_start;
2602 			     i < rdev->wiphy.num_iftype_ext_capab; i++) {
2603 				const struct wiphy_iftype_ext_capab *capab;
2604 
2605 				capab = &rdev->wiphy.iftype_ext_capab[i];
2606 
2607 				nested_ext_capab = nla_nest_start_noflag(msg,
2608 									 i);
2609 				if (!nested_ext_capab ||
2610 				    nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2611 						capab->iftype) ||
2612 				    nla_put(msg, NL80211_ATTR_EXT_CAPA,
2613 					    capab->extended_capabilities_len,
2614 					    capab->extended_capabilities) ||
2615 				    nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2616 					    capab->extended_capabilities_len,
2617 					    capab->extended_capabilities_mask))
2618 					goto nla_put_failure;
2619 
2620 				nla_nest_end(msg, nested_ext_capab);
2621 				if (state->split)
2622 					break;
2623 			}
2624 			nla_nest_end(msg, nested);
2625 			if (i < rdev->wiphy.num_iftype_ext_capab) {
2626 				state->capa_start = i + 1;
2627 				break;
2628 			}
2629 		}
2630 
2631 		if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2632 				rdev->wiphy.nan_supported_bands))
2633 			goto nla_put_failure;
2634 
2635 		if (wiphy_ext_feature_isset(&rdev->wiphy,
2636 					    NL80211_EXT_FEATURE_TXQS)) {
2637 			struct cfg80211_txq_stats txqstats = {};
2638 			int res;
2639 
2640 			res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2641 			if (!res &&
2642 			    !nl80211_put_txq_stats(msg, &txqstats,
2643 						   NL80211_ATTR_TXQ_STATS))
2644 				goto nla_put_failure;
2645 
2646 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2647 					rdev->wiphy.txq_limit))
2648 				goto nla_put_failure;
2649 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2650 					rdev->wiphy.txq_memory_limit))
2651 				goto nla_put_failure;
2652 			if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2653 					rdev->wiphy.txq_quantum))
2654 				goto nla_put_failure;
2655 		}
2656 
2657 		state->split_start++;
2658 		break;
2659 	case 14:
2660 		if (nl80211_send_pmsr_capa(rdev, msg))
2661 			goto nla_put_failure;
2662 
2663 		state->split_start++;
2664 		break;
2665 	case 15:
2666 		if (rdev->wiphy.akm_suites &&
2667 		    nla_put(msg, NL80211_ATTR_AKM_SUITES,
2668 			    sizeof(u32) * rdev->wiphy.n_akm_suites,
2669 			    rdev->wiphy.akm_suites))
2670 			goto nla_put_failure;
2671 
2672 		if (nl80211_put_iftype_akm_suites(rdev, msg))
2673 			goto nla_put_failure;
2674 
2675 		if (nl80211_put_tid_config_support(rdev, msg))
2676 			goto nla_put_failure;
2677 
2678 		/* done */
2679 		state->split_start = 0;
2680 		break;
2681 	}
2682  finish:
2683 	genlmsg_end(msg, hdr);
2684 	return 0;
2685 
2686  nla_put_failure:
2687 	genlmsg_cancel(msg, hdr);
2688 	return -EMSGSIZE;
2689 }
2690 
nl80211_dump_wiphy_parse(struct sk_buff * skb,struct netlink_callback * cb,struct nl80211_dump_wiphy_state * state)2691 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2692 				    struct netlink_callback *cb,
2693 				    struct nl80211_dump_wiphy_state *state)
2694 {
2695 	struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2696 	int ret;
2697 
2698 	if (!tb)
2699 		return -ENOMEM;
2700 
2701 	ret = nlmsg_parse_deprecated(cb->nlh,
2702 				     GENL_HDRLEN + nl80211_fam.hdrsize,
2703 				     tb, nl80211_fam.maxattr,
2704 				     nl80211_policy, NULL);
2705 	/* ignore parse errors for backward compatibility */
2706 	if (ret) {
2707 		ret = 0;
2708 		goto out;
2709 	}
2710 
2711 	state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2712 	if (tb[NL80211_ATTR_WIPHY])
2713 		state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2714 	if (tb[NL80211_ATTR_WDEV])
2715 		state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2716 	if (tb[NL80211_ATTR_IFINDEX]) {
2717 		struct net_device *netdev;
2718 		struct cfg80211_registered_device *rdev;
2719 		int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2720 
2721 		netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2722 		if (!netdev) {
2723 			ret = -ENODEV;
2724 			goto out;
2725 		}
2726 		if (netdev->ieee80211_ptr) {
2727 			rdev = wiphy_to_rdev(
2728 				netdev->ieee80211_ptr->wiphy);
2729 			state->filter_wiphy = rdev->wiphy_idx;
2730 		}
2731 	}
2732 
2733 	ret = 0;
2734 out:
2735 	kfree(tb);
2736 	return ret;
2737 }
2738 
nl80211_dump_wiphy(struct sk_buff * skb,struct netlink_callback * cb)2739 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2740 {
2741 	int idx = 0, ret;
2742 	struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2743 	struct cfg80211_registered_device *rdev;
2744 
2745 	rtnl_lock();
2746 	if (!state) {
2747 		state = kzalloc(sizeof(*state), GFP_KERNEL);
2748 		if (!state) {
2749 			rtnl_unlock();
2750 			return -ENOMEM;
2751 		}
2752 		state->filter_wiphy = -1;
2753 		ret = nl80211_dump_wiphy_parse(skb, cb, state);
2754 		if (ret) {
2755 			kfree(state);
2756 			rtnl_unlock();
2757 			return ret;
2758 		}
2759 		cb->args[0] = (long)state;
2760 	}
2761 
2762 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2763 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2764 			continue;
2765 		if (++idx <= state->start)
2766 			continue;
2767 		if (state->filter_wiphy != -1 &&
2768 		    state->filter_wiphy != rdev->wiphy_idx)
2769 			continue;
2770 		/* attempt to fit multiple wiphy data chunks into the skb */
2771 		do {
2772 			ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2773 						 skb,
2774 						 NETLINK_CB(cb->skb).portid,
2775 						 cb->nlh->nlmsg_seq,
2776 						 NLM_F_MULTI, state);
2777 			if (ret < 0) {
2778 				/*
2779 				 * If sending the wiphy data didn't fit (ENOBUFS
2780 				 * or EMSGSIZE returned), this SKB is still
2781 				 * empty (so it's not too big because another
2782 				 * wiphy dataset is already in the skb) and
2783 				 * we've not tried to adjust the dump allocation
2784 				 * yet ... then adjust the alloc size to be
2785 				 * bigger, and return 1 but with the empty skb.
2786 				 * This results in an empty message being RX'ed
2787 				 * in userspace, but that is ignored.
2788 				 *
2789 				 * We can then retry with the larger buffer.
2790 				 */
2791 				if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2792 				    !skb->len && !state->split &&
2793 				    cb->min_dump_alloc < 4096) {
2794 					cb->min_dump_alloc = 4096;
2795 					state->split_start = 0;
2796 					rtnl_unlock();
2797 					return 1;
2798 				}
2799 				idx--;
2800 				break;
2801 			}
2802 		} while (state->split_start > 0);
2803 		break;
2804 	}
2805 	rtnl_unlock();
2806 
2807 	state->start = idx;
2808 
2809 	return skb->len;
2810 }
2811 
nl80211_dump_wiphy_done(struct netlink_callback * cb)2812 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2813 {
2814 	kfree((void *)cb->args[0]);
2815 	return 0;
2816 }
2817 
nl80211_get_wiphy(struct sk_buff * skb,struct genl_info * info)2818 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2819 {
2820 	struct sk_buff *msg;
2821 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
2822 	struct nl80211_dump_wiphy_state state = {};
2823 
2824 	msg = nlmsg_new(4096, GFP_KERNEL);
2825 	if (!msg)
2826 		return -ENOMEM;
2827 
2828 	if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2829 			       info->snd_portid, info->snd_seq, 0,
2830 			       &state) < 0) {
2831 		nlmsg_free(msg);
2832 		return -ENOBUFS;
2833 	}
2834 
2835 	return genlmsg_reply(msg, info);
2836 }
2837 
2838 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2839 	[NL80211_TXQ_ATTR_QUEUE]		= { .type = NLA_U8 },
2840 	[NL80211_TXQ_ATTR_TXOP]			= { .type = NLA_U16 },
2841 	[NL80211_TXQ_ATTR_CWMIN]		= { .type = NLA_U16 },
2842 	[NL80211_TXQ_ATTR_CWMAX]		= { .type = NLA_U16 },
2843 	[NL80211_TXQ_ATTR_AIFS]			= { .type = NLA_U8 },
2844 };
2845 
parse_txq_params(struct nlattr * tb[],struct ieee80211_txq_params * txq_params)2846 static int parse_txq_params(struct nlattr *tb[],
2847 			    struct ieee80211_txq_params *txq_params)
2848 {
2849 	u8 ac;
2850 
2851 	if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2852 	    !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2853 	    !tb[NL80211_TXQ_ATTR_AIFS])
2854 		return -EINVAL;
2855 
2856 	ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2857 	txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2858 	txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2859 	txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2860 	txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2861 
2862 	if (ac >= NL80211_NUM_ACS)
2863 		return -EINVAL;
2864 	txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2865 	return 0;
2866 }
2867 
nl80211_can_set_dev_channel(struct wireless_dev * wdev)2868 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2869 {
2870 	/*
2871 	 * You can only set the channel explicitly for WDS interfaces,
2872 	 * all others have their channel managed via their respective
2873 	 * "establish a connection" command (connect, join, ...)
2874 	 *
2875 	 * For AP/GO and mesh mode, the channel can be set with the
2876 	 * channel userspace API, but is only stored and passed to the
2877 	 * low-level driver when the AP starts or the mesh is joined.
2878 	 * This is for backward compatibility, userspace can also give
2879 	 * the channel in the start-ap or join-mesh commands instead.
2880 	 *
2881 	 * Monitors are special as they are normally slaved to
2882 	 * whatever else is going on, so they have their own special
2883 	 * operation to set the monitor channel if possible.
2884 	 */
2885 	return !wdev ||
2886 		wdev->iftype == NL80211_IFTYPE_AP ||
2887 		wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2888 		wdev->iftype == NL80211_IFTYPE_MONITOR ||
2889 		wdev->iftype == NL80211_IFTYPE_P2P_GO;
2890 }
2891 
nl80211_parse_chandef(struct cfg80211_registered_device * rdev,struct genl_info * info,struct cfg80211_chan_def * chandef)2892 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2893 			  struct genl_info *info,
2894 			  struct cfg80211_chan_def *chandef)
2895 {
2896 	struct netlink_ext_ack *extack = info->extack;
2897 	struct nlattr **attrs = info->attrs;
2898 	u32 control_freq;
2899 
2900 	if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2901 		return -EINVAL;
2902 
2903 	control_freq = MHZ_TO_KHZ(
2904 			nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
2905 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
2906 		control_freq +=
2907 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
2908 
2909 	memset(chandef, 0, sizeof(*chandef));
2910 	chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
2911 	chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2912 	chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
2913 	chandef->freq1_offset = control_freq % 1000;
2914 	chandef->center_freq2 = 0;
2915 
2916 	/* Primary channel not allowed */
2917 	if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2918 		NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2919 				    "Channel is disabled");
2920 		return -EINVAL;
2921 	}
2922 
2923 	if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2924 		enum nl80211_channel_type chantype;
2925 
2926 		chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2927 
2928 		switch (chantype) {
2929 		case NL80211_CHAN_NO_HT:
2930 		case NL80211_CHAN_HT20:
2931 		case NL80211_CHAN_HT40PLUS:
2932 		case NL80211_CHAN_HT40MINUS:
2933 			cfg80211_chandef_create(chandef, chandef->chan,
2934 						chantype);
2935 			/* user input for center_freq is incorrect */
2936 			if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2937 			    chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2938 				NL_SET_ERR_MSG_ATTR(extack,
2939 						    attrs[NL80211_ATTR_CENTER_FREQ1],
2940 						    "bad center frequency 1");
2941 				return -EINVAL;
2942 			}
2943 			/* center_freq2 must be zero */
2944 			if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2945 			    nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2946 				NL_SET_ERR_MSG_ATTR(extack,
2947 						    attrs[NL80211_ATTR_CENTER_FREQ2],
2948 						    "center frequency 2 can't be used");
2949 				return -EINVAL;
2950 			}
2951 			break;
2952 		default:
2953 			NL_SET_ERR_MSG_ATTR(extack,
2954 					    attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2955 					    "invalid channel type");
2956 			return -EINVAL;
2957 		}
2958 	} else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2959 		chandef->width =
2960 			nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2961 		if (chandef->chan->band == NL80211_BAND_S1GHZ) {
2962 			/* User input error for channel width doesn't match channel  */
2963 			if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
2964 				NL_SET_ERR_MSG_ATTR(extack,
2965 						    attrs[NL80211_ATTR_CHANNEL_WIDTH],
2966 						    "bad channel width");
2967 				return -EINVAL;
2968 			}
2969 		}
2970 		if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
2971 			chandef->center_freq1 =
2972 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2973 			if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
2974 				chandef->freq1_offset = nla_get_u32(
2975 				      attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
2976 			else
2977 				chandef->freq1_offset = 0;
2978 		}
2979 		if (attrs[NL80211_ATTR_CENTER_FREQ2])
2980 			chandef->center_freq2 =
2981 				nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2982 	}
2983 
2984 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2985 		chandef->edmg.channels =
2986 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2987 
2988 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2989 			chandef->edmg.bw_config =
2990 		     nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2991 	} else {
2992 		chandef->edmg.bw_config = 0;
2993 		chandef->edmg.channels = 0;
2994 	}
2995 
2996 	if (!cfg80211_chandef_valid(chandef)) {
2997 		NL_SET_ERR_MSG(extack, "invalid channel definition");
2998 		return -EINVAL;
2999 	}
3000 
3001 	if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3002 				     IEEE80211_CHAN_DISABLED)) {
3003 		NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3004 		return -EINVAL;
3005 	}
3006 
3007 	if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3008 	     chandef->width == NL80211_CHAN_WIDTH_10) &&
3009 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3010 		NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3011 		return -EINVAL;
3012 	}
3013 
3014 	return 0;
3015 }
3016 
__nl80211_set_channel(struct cfg80211_registered_device * rdev,struct net_device * dev,struct genl_info * info)3017 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3018 				 struct net_device *dev,
3019 				 struct genl_info *info)
3020 {
3021 	struct cfg80211_chan_def chandef;
3022 	int result;
3023 	enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3024 	struct wireless_dev *wdev = NULL;
3025 
3026 	if (dev)
3027 		wdev = dev->ieee80211_ptr;
3028 	if (!nl80211_can_set_dev_channel(wdev))
3029 		return -EOPNOTSUPP;
3030 	if (wdev)
3031 		iftype = wdev->iftype;
3032 
3033 	result = nl80211_parse_chandef(rdev, info, &chandef);
3034 	if (result)
3035 		return result;
3036 
3037 	switch (iftype) {
3038 	case NL80211_IFTYPE_AP:
3039 	case NL80211_IFTYPE_P2P_GO:
3040 		if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3041 						   iftype)) {
3042 			result = -EINVAL;
3043 			break;
3044 		}
3045 		if (wdev->beacon_interval) {
3046 			if (!dev || !rdev->ops->set_ap_chanwidth ||
3047 			    !(rdev->wiphy.features &
3048 			      NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
3049 				result = -EBUSY;
3050 				break;
3051 			}
3052 
3053 			/* Only allow dynamic channel width changes */
3054 			if (chandef.chan != wdev->preset_chandef.chan) {
3055 				result = -EBUSY;
3056 				break;
3057 			}
3058 			result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
3059 			if (result)
3060 				break;
3061 		}
3062 		wdev->preset_chandef = chandef;
3063 		result = 0;
3064 		break;
3065 	case NL80211_IFTYPE_MESH_POINT:
3066 		result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3067 		break;
3068 	case NL80211_IFTYPE_MONITOR:
3069 		result = cfg80211_set_monitor_channel(rdev, &chandef);
3070 		break;
3071 	default:
3072 		result = -EINVAL;
3073 	}
3074 
3075 	return result;
3076 }
3077 
nl80211_set_channel(struct sk_buff * skb,struct genl_info * info)3078 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3079 {
3080 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3081 	struct net_device *netdev = info->user_ptr[1];
3082 
3083 	return __nl80211_set_channel(rdev, netdev, info);
3084 }
3085 
nl80211_set_wds_peer(struct sk_buff * skb,struct genl_info * info)3086 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
3087 {
3088 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3089 	struct net_device *dev = info->user_ptr[1];
3090 	struct wireless_dev *wdev = dev->ieee80211_ptr;
3091 	const u8 *bssid;
3092 
3093 	if (!info->attrs[NL80211_ATTR_MAC])
3094 		return -EINVAL;
3095 
3096 	if (netif_running(dev))
3097 		return -EBUSY;
3098 
3099 	if (!rdev->ops->set_wds_peer)
3100 		return -EOPNOTSUPP;
3101 
3102 	if (wdev->iftype != NL80211_IFTYPE_WDS)
3103 		return -EOPNOTSUPP;
3104 
3105 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3106 	return rdev_set_wds_peer(rdev, dev, bssid);
3107 }
3108 
nl80211_set_wiphy(struct sk_buff * skb,struct genl_info * info)3109 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3110 {
3111 	struct cfg80211_registered_device *rdev;
3112 	struct net_device *netdev = NULL;
3113 	struct wireless_dev *wdev;
3114 	int result = 0, rem_txq_params = 0;
3115 	struct nlattr *nl_txq_params;
3116 	u32 changed;
3117 	u8 retry_short = 0, retry_long = 0;
3118 	u32 frag_threshold = 0, rts_threshold = 0;
3119 	u8 coverage_class = 0;
3120 	u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3121 
3122 	ASSERT_RTNL();
3123 
3124 	/*
3125 	 * Try to find the wiphy and netdev. Normally this
3126 	 * function shouldn't need the netdev, but this is
3127 	 * done for backward compatibility -- previously
3128 	 * setting the channel was done per wiphy, but now
3129 	 * it is per netdev. Previous userland like hostapd
3130 	 * also passed a netdev to set_wiphy, so that it is
3131 	 * possible to let that go to the right netdev!
3132 	 */
3133 
3134 	if (info->attrs[NL80211_ATTR_IFINDEX]) {
3135 		int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3136 
3137 		netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3138 		if (netdev && netdev->ieee80211_ptr)
3139 			rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3140 		else
3141 			netdev = NULL;
3142 	}
3143 
3144 	if (!netdev) {
3145 		rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3146 						  info->attrs);
3147 		if (IS_ERR(rdev))
3148 			return PTR_ERR(rdev);
3149 		wdev = NULL;
3150 		netdev = NULL;
3151 		result = 0;
3152 	} else
3153 		wdev = netdev->ieee80211_ptr;
3154 
3155 	/*
3156 	 * end workaround code, by now the rdev is available
3157 	 * and locked, and wdev may or may not be NULL.
3158 	 */
3159 
3160 	if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3161 		result = cfg80211_dev_rename(
3162 			rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3163 
3164 	if (result)
3165 		return result;
3166 
3167 	if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3168 		struct ieee80211_txq_params txq_params;
3169 		struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3170 
3171 		if (!rdev->ops->set_txq_params)
3172 			return -EOPNOTSUPP;
3173 
3174 		if (!netdev)
3175 			return -EINVAL;
3176 
3177 		if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3178 		    netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3179 			return -EINVAL;
3180 
3181 		if (!netif_running(netdev))
3182 			return -ENETDOWN;
3183 
3184 		nla_for_each_nested(nl_txq_params,
3185 				    info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3186 				    rem_txq_params) {
3187 			result = nla_parse_nested_deprecated(tb,
3188 							     NL80211_TXQ_ATTR_MAX,
3189 							     nl_txq_params,
3190 							     txq_params_policy,
3191 							     info->extack);
3192 			if (result)
3193 				return result;
3194 			result = parse_txq_params(tb, &txq_params);
3195 			if (result)
3196 				return result;
3197 
3198 			result = rdev_set_txq_params(rdev, netdev,
3199 						     &txq_params);
3200 			if (result)
3201 				return result;
3202 		}
3203 	}
3204 
3205 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3206 		result = __nl80211_set_channel(
3207 			rdev,
3208 			nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3209 			info);
3210 		if (result)
3211 			return result;
3212 	}
3213 
3214 	if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3215 		struct wireless_dev *txp_wdev = wdev;
3216 		enum nl80211_tx_power_setting type;
3217 		int idx, mbm = 0;
3218 
3219 		if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3220 			txp_wdev = NULL;
3221 
3222 		if (!rdev->ops->set_tx_power)
3223 			return -EOPNOTSUPP;
3224 
3225 		idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3226 		type = nla_get_u32(info->attrs[idx]);
3227 
3228 		if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3229 		    (type != NL80211_TX_POWER_AUTOMATIC))
3230 			return -EINVAL;
3231 
3232 		if (type != NL80211_TX_POWER_AUTOMATIC) {
3233 			idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3234 			mbm = nla_get_u32(info->attrs[idx]);
3235 		}
3236 
3237 		result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3238 		if (result)
3239 			return result;
3240 	}
3241 
3242 	if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3243 	    info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3244 		u32 tx_ant, rx_ant;
3245 
3246 		if ((!rdev->wiphy.available_antennas_tx &&
3247 		     !rdev->wiphy.available_antennas_rx) ||
3248 		    !rdev->ops->set_antenna)
3249 			return -EOPNOTSUPP;
3250 
3251 		tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3252 		rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3253 
3254 		/* reject antenna configurations which don't match the
3255 		 * available antenna masks, except for the "all" mask */
3256 		if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3257 		    (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3258 			return -EINVAL;
3259 
3260 		tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3261 		rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3262 
3263 		result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3264 		if (result)
3265 			return result;
3266 	}
3267 
3268 	changed = 0;
3269 
3270 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3271 		retry_short = nla_get_u8(
3272 			info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3273 
3274 		changed |= WIPHY_PARAM_RETRY_SHORT;
3275 	}
3276 
3277 	if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3278 		retry_long = nla_get_u8(
3279 			info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3280 
3281 		changed |= WIPHY_PARAM_RETRY_LONG;
3282 	}
3283 
3284 	if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3285 		frag_threshold = nla_get_u32(
3286 			info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3287 		if (frag_threshold < 256)
3288 			return -EINVAL;
3289 
3290 		if (frag_threshold != (u32) -1) {
3291 			/*
3292 			 * Fragments (apart from the last one) are required to
3293 			 * have even length. Make the fragmentation code
3294 			 * simpler by stripping LSB should someone try to use
3295 			 * odd threshold value.
3296 			 */
3297 			frag_threshold &= ~0x1;
3298 		}
3299 		changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3300 	}
3301 
3302 	if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3303 		rts_threshold = nla_get_u32(
3304 			info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3305 		changed |= WIPHY_PARAM_RTS_THRESHOLD;
3306 	}
3307 
3308 	if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3309 		if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3310 			return -EINVAL;
3311 
3312 		coverage_class = nla_get_u8(
3313 			info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3314 		changed |= WIPHY_PARAM_COVERAGE_CLASS;
3315 	}
3316 
3317 	if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3318 		if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3319 			return -EOPNOTSUPP;
3320 
3321 		changed |= WIPHY_PARAM_DYN_ACK;
3322 	}
3323 
3324 	if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3325 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3326 					     NL80211_EXT_FEATURE_TXQS))
3327 			return -EOPNOTSUPP;
3328 		txq_limit = nla_get_u32(
3329 			info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3330 		changed |= WIPHY_PARAM_TXQ_LIMIT;
3331 	}
3332 
3333 	if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3334 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3335 					     NL80211_EXT_FEATURE_TXQS))
3336 			return -EOPNOTSUPP;
3337 		txq_memory_limit = nla_get_u32(
3338 			info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3339 		changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3340 	}
3341 
3342 	if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3343 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
3344 					     NL80211_EXT_FEATURE_TXQS))
3345 			return -EOPNOTSUPP;
3346 		txq_quantum = nla_get_u32(
3347 			info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3348 		changed |= WIPHY_PARAM_TXQ_QUANTUM;
3349 	}
3350 
3351 	if (changed) {
3352 		u8 old_retry_short, old_retry_long;
3353 		u32 old_frag_threshold, old_rts_threshold;
3354 		u8 old_coverage_class;
3355 		u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3356 
3357 		if (!rdev->ops->set_wiphy_params)
3358 			return -EOPNOTSUPP;
3359 
3360 		old_retry_short = rdev->wiphy.retry_short;
3361 		old_retry_long = rdev->wiphy.retry_long;
3362 		old_frag_threshold = rdev->wiphy.frag_threshold;
3363 		old_rts_threshold = rdev->wiphy.rts_threshold;
3364 		old_coverage_class = rdev->wiphy.coverage_class;
3365 		old_txq_limit = rdev->wiphy.txq_limit;
3366 		old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3367 		old_txq_quantum = rdev->wiphy.txq_quantum;
3368 
3369 		if (changed & WIPHY_PARAM_RETRY_SHORT)
3370 			rdev->wiphy.retry_short = retry_short;
3371 		if (changed & WIPHY_PARAM_RETRY_LONG)
3372 			rdev->wiphy.retry_long = retry_long;
3373 		if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3374 			rdev->wiphy.frag_threshold = frag_threshold;
3375 		if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3376 			rdev->wiphy.rts_threshold = rts_threshold;
3377 		if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3378 			rdev->wiphy.coverage_class = coverage_class;
3379 		if (changed & WIPHY_PARAM_TXQ_LIMIT)
3380 			rdev->wiphy.txq_limit = txq_limit;
3381 		if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3382 			rdev->wiphy.txq_memory_limit = txq_memory_limit;
3383 		if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3384 			rdev->wiphy.txq_quantum = txq_quantum;
3385 
3386 		result = rdev_set_wiphy_params(rdev, changed);
3387 		if (result) {
3388 			rdev->wiphy.retry_short = old_retry_short;
3389 			rdev->wiphy.retry_long = old_retry_long;
3390 			rdev->wiphy.frag_threshold = old_frag_threshold;
3391 			rdev->wiphy.rts_threshold = old_rts_threshold;
3392 			rdev->wiphy.coverage_class = old_coverage_class;
3393 			rdev->wiphy.txq_limit = old_txq_limit;
3394 			rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3395 			rdev->wiphy.txq_quantum = old_txq_quantum;
3396 			return result;
3397 		}
3398 	}
3399 	return 0;
3400 }
3401 
nl80211_send_chandef(struct sk_buff * msg,const struct cfg80211_chan_def * chandef)3402 static int nl80211_send_chandef(struct sk_buff *msg,
3403 				const struct cfg80211_chan_def *chandef)
3404 {
3405 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3406 		return -EINVAL;
3407 
3408 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3409 			chandef->chan->center_freq))
3410 		return -ENOBUFS;
3411 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3412 			chandef->chan->freq_offset))
3413 		return -ENOBUFS;
3414 	switch (chandef->width) {
3415 	case NL80211_CHAN_WIDTH_20_NOHT:
3416 	case NL80211_CHAN_WIDTH_20:
3417 	case NL80211_CHAN_WIDTH_40:
3418 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3419 				cfg80211_get_chandef_type(chandef)))
3420 			return -ENOBUFS;
3421 		break;
3422 	default:
3423 		break;
3424 	}
3425 	if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3426 		return -ENOBUFS;
3427 	if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3428 		return -ENOBUFS;
3429 	if (chandef->center_freq2 &&
3430 	    nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3431 		return -ENOBUFS;
3432 	return 0;
3433 }
3434 
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)3435 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3436 			      struct cfg80211_registered_device *rdev,
3437 			      struct wireless_dev *wdev,
3438 			      enum nl80211_commands cmd)
3439 {
3440 	struct net_device *dev = wdev->netdev;
3441 	void *hdr;
3442 
3443 	WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3444 		cmd != NL80211_CMD_DEL_INTERFACE &&
3445 		cmd != NL80211_CMD_SET_INTERFACE);
3446 
3447 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3448 	if (!hdr)
3449 		return -1;
3450 
3451 	if (dev &&
3452 	    (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3453 	     nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3454 		goto nla_put_failure;
3455 
3456 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3457 	    nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3458 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3459 			      NL80211_ATTR_PAD) ||
3460 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3461 	    nla_put_u32(msg, NL80211_ATTR_GENERATION,
3462 			rdev->devlist_generation ^
3463 			(cfg80211_rdev_list_generation << 2)) ||
3464 	    nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3465 		goto nla_put_failure;
3466 
3467 	if (rdev->ops->get_channel) {
3468 		int ret;
3469 		struct cfg80211_chan_def chandef = {};
3470 
3471 		ret = rdev_get_channel(rdev, wdev, &chandef);
3472 		if (ret == 0) {
3473 			if (nl80211_send_chandef(msg, &chandef))
3474 				goto nla_put_failure;
3475 		}
3476 	}
3477 
3478 	if (rdev->ops->get_tx_power) {
3479 		int dbm, ret;
3480 
3481 		ret = rdev_get_tx_power(rdev, wdev, &dbm);
3482 		if (ret == 0 &&
3483 		    nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3484 				DBM_TO_MBM(dbm)))
3485 			goto nla_put_failure;
3486 	}
3487 
3488 	wdev_lock(wdev);
3489 	switch (wdev->iftype) {
3490 	case NL80211_IFTYPE_AP:
3491 	case NL80211_IFTYPE_P2P_GO:
3492 		if (wdev->ssid_len &&
3493 		    nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3494 			goto nla_put_failure_locked;
3495 		break;
3496 	case NL80211_IFTYPE_STATION:
3497 	case NL80211_IFTYPE_P2P_CLIENT:
3498 	case NL80211_IFTYPE_ADHOC: {
3499 		const u8 *ssid_ie;
3500 		if (!wdev->current_bss)
3501 			break;
3502 		rcu_read_lock();
3503 		ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3504 					       WLAN_EID_SSID);
3505 		if (ssid_ie &&
3506 		    nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3507 			goto nla_put_failure_rcu_locked;
3508 		rcu_read_unlock();
3509 		break;
3510 		}
3511 	default:
3512 		/* nothing */
3513 		break;
3514 	}
3515 	wdev_unlock(wdev);
3516 
3517 	if (rdev->ops->get_txq_stats) {
3518 		struct cfg80211_txq_stats txqstats = {};
3519 		int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3520 
3521 		if (ret == 0 &&
3522 		    !nl80211_put_txq_stats(msg, &txqstats,
3523 					   NL80211_ATTR_TXQ_STATS))
3524 			goto nla_put_failure;
3525 	}
3526 
3527 	genlmsg_end(msg, hdr);
3528 	return 0;
3529 
3530  nla_put_failure_rcu_locked:
3531 	rcu_read_unlock();
3532  nla_put_failure_locked:
3533 	wdev_unlock(wdev);
3534  nla_put_failure:
3535 	genlmsg_cancel(msg, hdr);
3536 	return -EMSGSIZE;
3537 }
3538 
nl80211_dump_interface(struct sk_buff * skb,struct netlink_callback * cb)3539 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3540 {
3541 	int wp_idx = 0;
3542 	int if_idx = 0;
3543 	int wp_start = cb->args[0];
3544 	int if_start = cb->args[1];
3545 	int filter_wiphy = -1;
3546 	struct cfg80211_registered_device *rdev;
3547 	struct wireless_dev *wdev;
3548 	int ret;
3549 
3550 	rtnl_lock();
3551 	if (!cb->args[2]) {
3552 		struct nl80211_dump_wiphy_state state = {
3553 			.filter_wiphy = -1,
3554 		};
3555 
3556 		ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3557 		if (ret)
3558 			goto out_unlock;
3559 
3560 		filter_wiphy = state.filter_wiphy;
3561 
3562 		/*
3563 		 * if filtering, set cb->args[2] to +1 since 0 is the default
3564 		 * value needed to determine that parsing is necessary.
3565 		 */
3566 		if (filter_wiphy >= 0)
3567 			cb->args[2] = filter_wiphy + 1;
3568 		else
3569 			cb->args[2] = -1;
3570 	} else if (cb->args[2] > 0) {
3571 		filter_wiphy = cb->args[2] - 1;
3572 	}
3573 
3574 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3575 		if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3576 			continue;
3577 		if (wp_idx < wp_start) {
3578 			wp_idx++;
3579 			continue;
3580 		}
3581 
3582 		if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3583 			continue;
3584 
3585 		if_idx = 0;
3586 
3587 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3588 			if (if_idx < if_start) {
3589 				if_idx++;
3590 				continue;
3591 			}
3592 			if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3593 					       cb->nlh->nlmsg_seq, NLM_F_MULTI,
3594 					       rdev, wdev,
3595 					       NL80211_CMD_NEW_INTERFACE) < 0) {
3596 				goto out;
3597 			}
3598 			if_idx++;
3599 		}
3600 
3601 		if_start = 0;
3602 		wp_idx++;
3603 	}
3604  out:
3605 	cb->args[0] = wp_idx;
3606 	cb->args[1] = if_idx;
3607 
3608 	ret = skb->len;
3609  out_unlock:
3610 	rtnl_unlock();
3611 
3612 	return ret;
3613 }
3614 
nl80211_get_interface(struct sk_buff * skb,struct genl_info * info)3615 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3616 {
3617 	struct sk_buff *msg;
3618 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3619 	struct wireless_dev *wdev = info->user_ptr[1];
3620 
3621 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3622 	if (!msg)
3623 		return -ENOMEM;
3624 
3625 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3626 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3627 		nlmsg_free(msg);
3628 		return -ENOBUFS;
3629 	}
3630 
3631 	return genlmsg_reply(msg, info);
3632 }
3633 
3634 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3635 	[NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3636 	[NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3637 	[NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3638 	[NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3639 	[NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3640 	[NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3641 };
3642 
parse_monitor_flags(struct nlattr * nla,u32 * mntrflags)3643 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3644 {
3645 	struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3646 	int flag;
3647 
3648 	*mntrflags = 0;
3649 
3650 	if (!nla)
3651 		return -EINVAL;
3652 
3653 	if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3654 		return -EINVAL;
3655 
3656 	for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3657 		if (flags[flag])
3658 			*mntrflags |= (1<<flag);
3659 
3660 	*mntrflags |= MONITOR_FLAG_CHANGED;
3661 
3662 	return 0;
3663 }
3664 
nl80211_parse_mon_options(struct cfg80211_registered_device * rdev,enum nl80211_iftype type,struct genl_info * info,struct vif_params * params)3665 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3666 				     enum nl80211_iftype type,
3667 				     struct genl_info *info,
3668 				     struct vif_params *params)
3669 {
3670 	bool change = false;
3671 	int err;
3672 
3673 	if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3674 		if (type != NL80211_IFTYPE_MONITOR)
3675 			return -EINVAL;
3676 
3677 		err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3678 					  &params->flags);
3679 		if (err)
3680 			return err;
3681 
3682 		change = true;
3683 	}
3684 
3685 	if (params->flags & MONITOR_FLAG_ACTIVE &&
3686 	    !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3687 		return -EOPNOTSUPP;
3688 
3689 	if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3690 		const u8 *mumimo_groups;
3691 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3692 
3693 		if (type != NL80211_IFTYPE_MONITOR)
3694 			return -EINVAL;
3695 
3696 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3697 			return -EOPNOTSUPP;
3698 
3699 		mumimo_groups =
3700 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3701 
3702 		/* bits 0 and 63 are reserved and must be zero */
3703 		if ((mumimo_groups[0] & BIT(0)) ||
3704 		    (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3705 			return -EINVAL;
3706 
3707 		params->vht_mumimo_groups = mumimo_groups;
3708 		change = true;
3709 	}
3710 
3711 	if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3712 		u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3713 
3714 		if (type != NL80211_IFTYPE_MONITOR)
3715 			return -EINVAL;
3716 
3717 		if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3718 			return -EOPNOTSUPP;
3719 
3720 		params->vht_mumimo_follow_addr =
3721 			nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3722 		change = true;
3723 	}
3724 
3725 	return change ? 1 : 0;
3726 }
3727 
nl80211_valid_4addr(struct cfg80211_registered_device * rdev,struct net_device * netdev,u8 use_4addr,enum nl80211_iftype iftype)3728 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3729 			       struct net_device *netdev, u8 use_4addr,
3730 			       enum nl80211_iftype iftype)
3731 {
3732 	if (!use_4addr) {
3733 		if (netdev && netif_is_bridge_port(netdev))
3734 			return -EBUSY;
3735 		return 0;
3736 	}
3737 
3738 	switch (iftype) {
3739 	case NL80211_IFTYPE_AP_VLAN:
3740 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3741 			return 0;
3742 		break;
3743 	case NL80211_IFTYPE_STATION:
3744 		if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3745 			return 0;
3746 		break;
3747 	default:
3748 		break;
3749 	}
3750 
3751 	return -EOPNOTSUPP;
3752 }
3753 
nl80211_set_interface(struct sk_buff * skb,struct genl_info * info)3754 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3755 {
3756 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3757 	struct vif_params params;
3758 	int err;
3759 	enum nl80211_iftype otype, ntype;
3760 	struct net_device *dev = info->user_ptr[1];
3761 	bool change = false;
3762 
3763 	memset(&params, 0, sizeof(params));
3764 
3765 	otype = ntype = dev->ieee80211_ptr->iftype;
3766 
3767 	if (info->attrs[NL80211_ATTR_IFTYPE]) {
3768 		ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3769 		if (otype != ntype)
3770 			change = true;
3771 	}
3772 
3773 	if (info->attrs[NL80211_ATTR_MESH_ID]) {
3774 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3775 
3776 		if (ntype != NL80211_IFTYPE_MESH_POINT)
3777 			return -EINVAL;
3778 		if (otype != NL80211_IFTYPE_MESH_POINT)
3779 			return -EINVAL;
3780 		if (netif_running(dev))
3781 			return -EBUSY;
3782 
3783 		wdev_lock(wdev);
3784 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3785 			     IEEE80211_MAX_MESH_ID_LEN);
3786 		wdev->mesh_id_up_len =
3787 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3788 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3789 		       wdev->mesh_id_up_len);
3790 		wdev_unlock(wdev);
3791 	}
3792 
3793 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3794 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3795 		change = true;
3796 		err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3797 		if (err)
3798 			return err;
3799 	} else {
3800 		params.use_4addr = -1;
3801 	}
3802 
3803 	err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3804 	if (err < 0)
3805 		return err;
3806 	if (err > 0)
3807 		change = true;
3808 
3809 	if (change)
3810 		err = cfg80211_change_iface(rdev, dev, ntype, &params);
3811 	else
3812 		err = 0;
3813 
3814 	if (!err && params.use_4addr != -1)
3815 		dev->ieee80211_ptr->use_4addr = params.use_4addr;
3816 
3817 	if (change && !err) {
3818 		struct wireless_dev *wdev = dev->ieee80211_ptr;
3819 
3820 		nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3821 	}
3822 
3823 	return err;
3824 }
3825 
nl80211_new_interface(struct sk_buff * skb,struct genl_info * info)3826 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3827 {
3828 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3829 	struct vif_params params;
3830 	struct wireless_dev *wdev;
3831 	struct sk_buff *msg;
3832 	int err;
3833 	enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3834 
3835 	/* to avoid failing a new interface creation due to pending removal */
3836 	cfg80211_destroy_ifaces(rdev);
3837 
3838 	memset(&params, 0, sizeof(params));
3839 
3840 	if (!info->attrs[NL80211_ATTR_IFNAME])
3841 		return -EINVAL;
3842 
3843 	if (info->attrs[NL80211_ATTR_IFTYPE])
3844 		type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3845 
3846 	if (!rdev->ops->add_virtual_intf)
3847 		return -EOPNOTSUPP;
3848 
3849 	if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3850 	     rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3851 	    info->attrs[NL80211_ATTR_MAC]) {
3852 		nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3853 			   ETH_ALEN);
3854 		if (!is_valid_ether_addr(params.macaddr))
3855 			return -EADDRNOTAVAIL;
3856 	}
3857 
3858 	if (info->attrs[NL80211_ATTR_4ADDR]) {
3859 		params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3860 		err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3861 		if (err)
3862 			return err;
3863 	}
3864 
3865 	if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3866 		return -EOPNOTSUPP;
3867 
3868 	err = nl80211_parse_mon_options(rdev, type, info, &params);
3869 	if (err < 0)
3870 		return err;
3871 
3872 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3873 	if (!msg)
3874 		return -ENOMEM;
3875 
3876 	wdev = rdev_add_virtual_intf(rdev,
3877 				nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3878 				NET_NAME_USER, type, &params);
3879 	if (WARN_ON(!wdev)) {
3880 		nlmsg_free(msg);
3881 		return -EPROTO;
3882 	} else if (IS_ERR(wdev)) {
3883 		nlmsg_free(msg);
3884 		return PTR_ERR(wdev);
3885 	}
3886 
3887 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3888 		wdev->owner_nlportid = info->snd_portid;
3889 
3890 	switch (type) {
3891 	case NL80211_IFTYPE_MESH_POINT:
3892 		if (!info->attrs[NL80211_ATTR_MESH_ID])
3893 			break;
3894 		wdev_lock(wdev);
3895 		BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3896 			     IEEE80211_MAX_MESH_ID_LEN);
3897 		wdev->mesh_id_up_len =
3898 			nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3899 		memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3900 		       wdev->mesh_id_up_len);
3901 		wdev_unlock(wdev);
3902 		break;
3903 	case NL80211_IFTYPE_NAN:
3904 	case NL80211_IFTYPE_P2P_DEVICE:
3905 		/*
3906 		 * P2P Device and NAN do not have a netdev, so don't go
3907 		 * through the netdev notifier and must be added here
3908 		 */
3909 		cfg80211_init_wdev(wdev);
3910 		cfg80211_register_wdev(rdev, wdev);
3911 		break;
3912 	default:
3913 		break;
3914 	}
3915 
3916 	if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3917 			       rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3918 		nlmsg_free(msg);
3919 		return -ENOBUFS;
3920 	}
3921 
3922 	return genlmsg_reply(msg, info);
3923 }
3924 
nl80211_del_interface(struct sk_buff * skb,struct genl_info * info)3925 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3926 {
3927 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3928 	struct wireless_dev *wdev = info->user_ptr[1];
3929 
3930 	if (!rdev->ops->del_virtual_intf)
3931 		return -EOPNOTSUPP;
3932 
3933 	/*
3934 	 * If we remove a wireless device without a netdev then clear
3935 	 * user_ptr[1] so that nl80211_post_doit won't dereference it
3936 	 * to check if it needs to do dev_put(). Otherwise it crashes
3937 	 * since the wdev has been freed, unlike with a netdev where
3938 	 * we need the dev_put() for the netdev to really be freed.
3939 	 */
3940 	if (!wdev->netdev)
3941 		info->user_ptr[1] = NULL;
3942 
3943 	return rdev_del_virtual_intf(rdev, wdev);
3944 }
3945 
nl80211_set_noack_map(struct sk_buff * skb,struct genl_info * info)3946 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3947 {
3948 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
3949 	struct net_device *dev = info->user_ptr[1];
3950 	u16 noack_map;
3951 
3952 	if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3953 		return -EINVAL;
3954 
3955 	if (!rdev->ops->set_noack_map)
3956 		return -EOPNOTSUPP;
3957 
3958 	noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3959 
3960 	return rdev_set_noack_map(rdev, dev, noack_map);
3961 }
3962 
3963 struct get_key_cookie {
3964 	struct sk_buff *msg;
3965 	int error;
3966 	int idx;
3967 };
3968 
get_key_callback(void * c,struct key_params * params)3969 static void get_key_callback(void *c, struct key_params *params)
3970 {
3971 	struct nlattr *key;
3972 	struct get_key_cookie *cookie = c;
3973 
3974 	if ((params->key &&
3975 	     nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3976 		     params->key_len, params->key)) ||
3977 	    (params->seq &&
3978 	     nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3979 		     params->seq_len, params->seq)) ||
3980 	    (params->cipher &&
3981 	     nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3982 			 params->cipher)))
3983 		goto nla_put_failure;
3984 
3985 	key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3986 	if (!key)
3987 		goto nla_put_failure;
3988 
3989 	if ((params->key &&
3990 	     nla_put(cookie->msg, NL80211_KEY_DATA,
3991 		     params->key_len, params->key)) ||
3992 	    (params->seq &&
3993 	     nla_put(cookie->msg, NL80211_KEY_SEQ,
3994 		     params->seq_len, params->seq)) ||
3995 	    (params->cipher &&
3996 	     nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3997 			 params->cipher)))
3998 		goto nla_put_failure;
3999 
4000 	if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4001 		goto nla_put_failure;
4002 
4003 	nla_nest_end(cookie->msg, key);
4004 
4005 	return;
4006  nla_put_failure:
4007 	cookie->error = 1;
4008 }
4009 
nl80211_get_key(struct sk_buff * skb,struct genl_info * info)4010 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4011 {
4012 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4013 	int err;
4014 	struct net_device *dev = info->user_ptr[1];
4015 	u8 key_idx = 0;
4016 	const u8 *mac_addr = NULL;
4017 	bool pairwise;
4018 	struct get_key_cookie cookie = {
4019 		.error = 0,
4020 	};
4021 	void *hdr;
4022 	struct sk_buff *msg;
4023 	bool bigtk_support = false;
4024 
4025 	if (wiphy_ext_feature_isset(&rdev->wiphy,
4026 				    NL80211_EXT_FEATURE_BEACON_PROTECTION))
4027 		bigtk_support = true;
4028 
4029 	if ((dev->ieee80211_ptr->iftype == NL80211_IFTYPE_STATION ||
4030 	     dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4031 	    wiphy_ext_feature_isset(&rdev->wiphy,
4032 				    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4033 		bigtk_support = true;
4034 
4035 	if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4036 		key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4037 
4038 		if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4039 			GENL_SET_ERR_MSG(info, "BIGTK not supported");
4040 			return -EINVAL;
4041 		}
4042 	}
4043 
4044 	if (info->attrs[NL80211_ATTR_MAC])
4045 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4046 
4047 	pairwise = !!mac_addr;
4048 	if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4049 		u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4050 
4051 		if (kt != NL80211_KEYTYPE_GROUP &&
4052 		    kt != NL80211_KEYTYPE_PAIRWISE)
4053 			return -EINVAL;
4054 		pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4055 	}
4056 
4057 	if (!rdev->ops->get_key)
4058 		return -EOPNOTSUPP;
4059 
4060 	if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4061 		return -ENOENT;
4062 
4063 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4064 	if (!msg)
4065 		return -ENOMEM;
4066 
4067 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4068 			     NL80211_CMD_NEW_KEY);
4069 	if (!hdr)
4070 		goto nla_put_failure;
4071 
4072 	cookie.msg = msg;
4073 	cookie.idx = key_idx;
4074 
4075 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4076 	    nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4077 		goto nla_put_failure;
4078 	if (mac_addr &&
4079 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4080 		goto nla_put_failure;
4081 
4082 	err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
4083 			   get_key_callback);
4084 
4085 	if (err)
4086 		goto free_msg;
4087 
4088 	if (cookie.error)
4089 		goto nla_put_failure;
4090 
4091 	genlmsg_end(msg, hdr);
4092 	return genlmsg_reply(msg, info);
4093 
4094  nla_put_failure:
4095 	err = -ENOBUFS;
4096  free_msg:
4097 	nlmsg_free(msg);
4098 	return err;
4099 }
4100 
nl80211_set_key(struct sk_buff * skb,struct genl_info * info)4101 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4102 {
4103 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4104 	struct key_parse key;
4105 	int err;
4106 	struct net_device *dev = info->user_ptr[1];
4107 
4108 	err = nl80211_parse_key(info, &key);
4109 	if (err)
4110 		return err;
4111 
4112 	if (key.idx < 0)
4113 		return -EINVAL;
4114 
4115 	/* Only support setting default key and
4116 	 * Extended Key ID action NL80211_KEY_SET_TX.
4117 	 */
4118 	if (!key.def && !key.defmgmt && !key.defbeacon &&
4119 	    !(key.p.mode == NL80211_KEY_SET_TX))
4120 		return -EINVAL;
4121 
4122 	wdev_lock(dev->ieee80211_ptr);
4123 
4124 	if (key.def) {
4125 		if (!rdev->ops->set_default_key) {
4126 			err = -EOPNOTSUPP;
4127 			goto out;
4128 		}
4129 
4130 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4131 		if (err)
4132 			goto out;
4133 
4134 		err = rdev_set_default_key(rdev, dev, key.idx,
4135 						 key.def_uni, key.def_multi);
4136 
4137 		if (err)
4138 			goto out;
4139 
4140 #ifdef CONFIG_CFG80211_WEXT
4141 		dev->ieee80211_ptr->wext.default_key = key.idx;
4142 #endif
4143 	} else if (key.defmgmt) {
4144 		if (key.def_uni || !key.def_multi) {
4145 			err = -EINVAL;
4146 			goto out;
4147 		}
4148 
4149 		if (!rdev->ops->set_default_mgmt_key) {
4150 			err = -EOPNOTSUPP;
4151 			goto out;
4152 		}
4153 
4154 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4155 		if (err)
4156 			goto out;
4157 
4158 		err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
4159 		if (err)
4160 			goto out;
4161 
4162 #ifdef CONFIG_CFG80211_WEXT
4163 		dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
4164 #endif
4165 	} else if (key.defbeacon) {
4166 		if (key.def_uni || !key.def_multi) {
4167 			err = -EINVAL;
4168 			goto out;
4169 		}
4170 
4171 		if (!rdev->ops->set_default_beacon_key) {
4172 			err = -EOPNOTSUPP;
4173 			goto out;
4174 		}
4175 
4176 		err = nl80211_key_allowed(dev->ieee80211_ptr);
4177 		if (err)
4178 			goto out;
4179 
4180 		err = rdev_set_default_beacon_key(rdev, dev, key.idx);
4181 		if (err)
4182 			goto out;
4183 	} else if (key.p.mode == NL80211_KEY_SET_TX &&
4184 		   wiphy_ext_feature_isset(&rdev->wiphy,
4185 					   NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4186 		u8 *mac_addr = NULL;
4187 
4188 		if (info->attrs[NL80211_ATTR_MAC])
4189 			mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4190 
4191 		if (!mac_addr || key.idx < 0 || key.idx > 1) {
4192 			err = -EINVAL;
4193 			goto out;
4194 		}
4195 
4196 		err = rdev_add_key(rdev, dev, key.idx,
4197 				   NL80211_KEYTYPE_PAIRWISE,
4198 				   mac_addr, &key.p);
4199 	} else {
4200 		err = -EINVAL;
4201 	}
4202  out:
4203 	wdev_unlock(dev->ieee80211_ptr);
4204 
4205 	return err;
4206 }
4207 
nl80211_new_key(struct sk_buff * skb,struct genl_info * info)4208 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4209 {
4210 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4211 	int err;
4212 	struct net_device *dev = info->user_ptr[1];
4213 	struct key_parse key;
4214 	const u8 *mac_addr = NULL;
4215 
4216 	err = nl80211_parse_key(info, &key);
4217 	if (err)
4218 		return err;
4219 
4220 	if (!key.p.key) {
4221 		GENL_SET_ERR_MSG(info, "no key");
4222 		return -EINVAL;
4223 	}
4224 
4225 	if (info->attrs[NL80211_ATTR_MAC])
4226 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4227 
4228 	if (key.type == -1) {
4229 		if (mac_addr)
4230 			key.type = NL80211_KEYTYPE_PAIRWISE;
4231 		else
4232 			key.type = NL80211_KEYTYPE_GROUP;
4233 	}
4234 
4235 	/* for now */
4236 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4237 	    key.type != NL80211_KEYTYPE_GROUP) {
4238 		GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4239 		return -EINVAL;
4240 	}
4241 
4242 	if (key.type == NL80211_KEYTYPE_GROUP &&
4243 	    info->attrs[NL80211_ATTR_VLAN_ID])
4244 		key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4245 
4246 	if (!rdev->ops->add_key)
4247 		return -EOPNOTSUPP;
4248 
4249 	if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4250 					   key.type == NL80211_KEYTYPE_PAIRWISE,
4251 					   mac_addr)) {
4252 		GENL_SET_ERR_MSG(info, "key setting validation failed");
4253 		return -EINVAL;
4254 	}
4255 
4256 	wdev_lock(dev->ieee80211_ptr);
4257 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4258 	if (err)
4259 		GENL_SET_ERR_MSG(info, "key not allowed");
4260 	if (!err) {
4261 		err = rdev_add_key(rdev, dev, key.idx,
4262 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4263 				    mac_addr, &key.p);
4264 		if (err)
4265 			GENL_SET_ERR_MSG(info, "key addition failed");
4266 	}
4267 	wdev_unlock(dev->ieee80211_ptr);
4268 
4269 	return err;
4270 }
4271 
nl80211_del_key(struct sk_buff * skb,struct genl_info * info)4272 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4273 {
4274 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4275 	int err;
4276 	struct net_device *dev = info->user_ptr[1];
4277 	u8 *mac_addr = NULL;
4278 	struct key_parse key;
4279 
4280 	err = nl80211_parse_key(info, &key);
4281 	if (err)
4282 		return err;
4283 
4284 	if (info->attrs[NL80211_ATTR_MAC])
4285 		mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4286 
4287 	if (key.type == -1) {
4288 		if (mac_addr)
4289 			key.type = NL80211_KEYTYPE_PAIRWISE;
4290 		else
4291 			key.type = NL80211_KEYTYPE_GROUP;
4292 	}
4293 
4294 	/* for now */
4295 	if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4296 	    key.type != NL80211_KEYTYPE_GROUP)
4297 		return -EINVAL;
4298 
4299 	if (!cfg80211_valid_key_idx(rdev, key.idx,
4300 				    key.type == NL80211_KEYTYPE_PAIRWISE))
4301 		return -EINVAL;
4302 
4303 	if (!rdev->ops->del_key)
4304 		return -EOPNOTSUPP;
4305 
4306 	wdev_lock(dev->ieee80211_ptr);
4307 	err = nl80211_key_allowed(dev->ieee80211_ptr);
4308 
4309 	if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4310 	    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4311 		err = -ENOENT;
4312 
4313 	if (!err)
4314 		err = rdev_del_key(rdev, dev, key.idx,
4315 				   key.type == NL80211_KEYTYPE_PAIRWISE,
4316 				   mac_addr);
4317 
4318 #ifdef CONFIG_CFG80211_WEXT
4319 	if (!err) {
4320 		if (key.idx == dev->ieee80211_ptr->wext.default_key)
4321 			dev->ieee80211_ptr->wext.default_key = -1;
4322 		else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4323 			dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4324 	}
4325 #endif
4326 	wdev_unlock(dev->ieee80211_ptr);
4327 
4328 	return err;
4329 }
4330 
4331 /* This function returns an error or the number of nested attributes */
validate_acl_mac_addrs(struct nlattr * nl_attr)4332 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4333 {
4334 	struct nlattr *attr;
4335 	int n_entries = 0, tmp;
4336 
4337 	nla_for_each_nested(attr, nl_attr, tmp) {
4338 		if (nla_len(attr) != ETH_ALEN)
4339 			return -EINVAL;
4340 
4341 		n_entries++;
4342 	}
4343 
4344 	return n_entries;
4345 }
4346 
4347 /*
4348  * This function parses ACL information and allocates memory for ACL data.
4349  * On successful return, the calling function is responsible to free the
4350  * ACL buffer returned by this function.
4351  */
parse_acl_data(struct wiphy * wiphy,struct genl_info * info)4352 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4353 						struct genl_info *info)
4354 {
4355 	enum nl80211_acl_policy acl_policy;
4356 	struct nlattr *attr;
4357 	struct cfg80211_acl_data *acl;
4358 	int i = 0, n_entries, tmp;
4359 
4360 	if (!wiphy->max_acl_mac_addrs)
4361 		return ERR_PTR(-EOPNOTSUPP);
4362 
4363 	if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4364 		return ERR_PTR(-EINVAL);
4365 
4366 	acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4367 	if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4368 	    acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4369 		return ERR_PTR(-EINVAL);
4370 
4371 	if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4372 		return ERR_PTR(-EINVAL);
4373 
4374 	n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4375 	if (n_entries < 0)
4376 		return ERR_PTR(n_entries);
4377 
4378 	if (n_entries > wiphy->max_acl_mac_addrs)
4379 		return ERR_PTR(-ENOTSUPP);
4380 
4381 	acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4382 	if (!acl)
4383 		return ERR_PTR(-ENOMEM);
4384 
4385 	nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4386 		memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4387 		i++;
4388 	}
4389 
4390 	acl->n_acl_entries = n_entries;
4391 	acl->acl_policy = acl_policy;
4392 
4393 	return acl;
4394 }
4395 
nl80211_set_mac_acl(struct sk_buff * skb,struct genl_info * info)4396 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4397 {
4398 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4399 	struct net_device *dev = info->user_ptr[1];
4400 	struct cfg80211_acl_data *acl;
4401 	int err;
4402 
4403 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4404 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4405 		return -EOPNOTSUPP;
4406 
4407 	if (!dev->ieee80211_ptr->beacon_interval)
4408 		return -EINVAL;
4409 
4410 	acl = parse_acl_data(&rdev->wiphy, info);
4411 	if (IS_ERR(acl))
4412 		return PTR_ERR(acl);
4413 
4414 	err = rdev_set_mac_acl(rdev, dev, acl);
4415 
4416 	kfree(acl);
4417 
4418 	return err;
4419 }
4420 
rateset_to_mask(struct ieee80211_supported_band * sband,u8 * rates,u8 rates_len)4421 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4422 			   u8 *rates, u8 rates_len)
4423 {
4424 	u8 i;
4425 	u32 mask = 0;
4426 
4427 	for (i = 0; i < rates_len; i++) {
4428 		int rate = (rates[i] & 0x7f) * 5;
4429 		int ridx;
4430 
4431 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4432 			struct ieee80211_rate *srate =
4433 				&sband->bitrates[ridx];
4434 			if (rate == srate->bitrate) {
4435 				mask |= 1 << ridx;
4436 				break;
4437 			}
4438 		}
4439 		if (ridx == sband->n_bitrates)
4440 			return 0; /* rate not found */
4441 	}
4442 
4443 	return mask;
4444 }
4445 
ht_rateset_to_mask(struct ieee80211_supported_band * sband,u8 * rates,u8 rates_len,u8 mcs[IEEE80211_HT_MCS_MASK_LEN])4446 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4447 			       u8 *rates, u8 rates_len,
4448 			       u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4449 {
4450 	u8 i;
4451 
4452 	memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4453 
4454 	for (i = 0; i < rates_len; i++) {
4455 		int ridx, rbit;
4456 
4457 		ridx = rates[i] / 8;
4458 		rbit = BIT(rates[i] % 8);
4459 
4460 		/* check validity */
4461 		if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4462 			return false;
4463 
4464 		/* check availability */
4465 		ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4466 		if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4467 			mcs[ridx] |= rbit;
4468 		else
4469 			return false;
4470 	}
4471 
4472 	return true;
4473 }
4474 
vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)4475 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4476 {
4477 	u16 mcs_mask = 0;
4478 
4479 	switch (vht_mcs_map) {
4480 	case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4481 		break;
4482 	case IEEE80211_VHT_MCS_SUPPORT_0_7:
4483 		mcs_mask = 0x00FF;
4484 		break;
4485 	case IEEE80211_VHT_MCS_SUPPORT_0_8:
4486 		mcs_mask = 0x01FF;
4487 		break;
4488 	case IEEE80211_VHT_MCS_SUPPORT_0_9:
4489 		mcs_mask = 0x03FF;
4490 		break;
4491 	default:
4492 		break;
4493 	}
4494 
4495 	return mcs_mask;
4496 }
4497 
vht_build_mcs_mask(u16 vht_mcs_map,u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])4498 static void vht_build_mcs_mask(u16 vht_mcs_map,
4499 			       u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4500 {
4501 	u8 nss;
4502 
4503 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4504 		vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4505 		vht_mcs_map >>= 2;
4506 	}
4507 }
4508 
vht_set_mcs_mask(struct ieee80211_supported_band * sband,struct nl80211_txrate_vht * txrate,u16 mcs[NL80211_VHT_NSS_MAX])4509 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4510 			     struct nl80211_txrate_vht *txrate,
4511 			     u16 mcs[NL80211_VHT_NSS_MAX])
4512 {
4513 	u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4514 	u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4515 	u8 i;
4516 
4517 	if (!sband->vht_cap.vht_supported)
4518 		return false;
4519 
4520 	memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4521 
4522 	/* Build vht_mcs_mask from VHT capabilities */
4523 	vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4524 
4525 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4526 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4527 			mcs[i] = txrate->mcs[i];
4528 		else
4529 			return false;
4530 	}
4531 
4532 	return true;
4533 }
4534 
he_mcs_map_to_mcs_mask(u8 he_mcs_map)4535 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
4536 {
4537 	switch (he_mcs_map) {
4538 	case IEEE80211_HE_MCS_NOT_SUPPORTED:
4539 		return 0;
4540 	case IEEE80211_HE_MCS_SUPPORT_0_7:
4541 		return 0x00FF;
4542 	case IEEE80211_HE_MCS_SUPPORT_0_9:
4543 		return 0x03FF;
4544 	case IEEE80211_HE_MCS_SUPPORT_0_11:
4545 		return 0xFFF;
4546 	default:
4547 		break;
4548 	}
4549 	return 0;
4550 }
4551 
he_build_mcs_mask(u16 he_mcs_map,u16 he_mcs_mask[NL80211_HE_NSS_MAX])4552 static void he_build_mcs_mask(u16 he_mcs_map,
4553 			      u16 he_mcs_mask[NL80211_HE_NSS_MAX])
4554 {
4555 	u8 nss;
4556 
4557 	for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
4558 		he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
4559 		he_mcs_map >>= 2;
4560 	}
4561 }
4562 
he_get_txmcsmap(struct genl_info * info,const struct ieee80211_sta_he_cap * he_cap)4563 static u16 he_get_txmcsmap(struct genl_info *info,
4564 			   const struct ieee80211_sta_he_cap *he_cap)
4565 {
4566 	struct net_device *dev = info->user_ptr[1];
4567 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4568 	__le16	tx_mcs;
4569 
4570 	switch (wdev->chandef.width) {
4571 	case NL80211_CHAN_WIDTH_80P80:
4572 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
4573 		break;
4574 	case NL80211_CHAN_WIDTH_160:
4575 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
4576 		break;
4577 	default:
4578 		tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
4579 		break;
4580 	}
4581 	return le16_to_cpu(tx_mcs);
4582 }
4583 
he_set_mcs_mask(struct genl_info * info,struct wireless_dev * wdev,struct ieee80211_supported_band * sband,struct nl80211_txrate_he * txrate,u16 mcs[NL80211_HE_NSS_MAX])4584 static bool he_set_mcs_mask(struct genl_info *info,
4585 			    struct wireless_dev *wdev,
4586 			    struct ieee80211_supported_band *sband,
4587 			    struct nl80211_txrate_he *txrate,
4588 			    u16 mcs[NL80211_HE_NSS_MAX])
4589 {
4590 	const struct ieee80211_sta_he_cap *he_cap;
4591 	u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
4592 	u16 tx_mcs_map = 0;
4593 	u8 i;
4594 
4595 	he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
4596 	if (!he_cap)
4597 		return false;
4598 
4599 	memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
4600 
4601 	tx_mcs_map = he_get_txmcsmap(info, he_cap);
4602 
4603 	/* Build he_mcs_mask from HE capabilities */
4604 	he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4605 
4606 	for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
4607 		if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4608 			mcs[i] = txrate->mcs[i];
4609 		else
4610 			return false;
4611 	}
4612 
4613 	return true;
4614 }
4615 
nl80211_parse_tx_bitrate_mask(struct genl_info * info,struct nlattr * attrs[],enum nl80211_attrs attr,struct cfg80211_bitrate_mask * mask,struct net_device * dev)4616 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4617 					 struct nlattr *attrs[],
4618 					 enum nl80211_attrs attr,
4619 					 struct cfg80211_bitrate_mask *mask,
4620 					 struct net_device *dev)
4621 {
4622 	struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4623 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
4624 	struct wireless_dev *wdev = dev->ieee80211_ptr;
4625 	int rem, i;
4626 	struct nlattr *tx_rates;
4627 	struct ieee80211_supported_band *sband;
4628 	u16 vht_tx_mcs_map, he_tx_mcs_map;
4629 
4630 	memset(mask, 0, sizeof(*mask));
4631 	/* Default to all rates enabled */
4632 	for (i = 0; i < NUM_NL80211_BANDS; i++) {
4633 		const struct ieee80211_sta_he_cap *he_cap;
4634 
4635 		sband = rdev->wiphy.bands[i];
4636 
4637 		if (!sband)
4638 			continue;
4639 
4640 		mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4641 		memcpy(mask->control[i].ht_mcs,
4642 		       sband->ht_cap.mcs.rx_mask,
4643 		       sizeof(mask->control[i].ht_mcs));
4644 
4645 		if (sband->vht_cap.vht_supported) {
4646 			vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4647 			vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4648 		}
4649 
4650 		he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
4651 		if (!he_cap)
4652 			continue;
4653 
4654 		he_tx_mcs_map = he_get_txmcsmap(info, he_cap);
4655 		he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
4656 
4657 		mask->control[i].he_gi = 0xFF;
4658 		mask->control[i].he_ltf = 0xFF;
4659 	}
4660 
4661 	/* if no rates are given set it back to the defaults */
4662 	if (!attrs[attr])
4663 		goto out;
4664 
4665 	/* The nested attribute uses enum nl80211_band as the index. This maps
4666 	 * directly to the enum nl80211_band values used in cfg80211.
4667 	 */
4668 	BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4669 	nla_for_each_nested(tx_rates, attrs[attr], rem) {
4670 		enum nl80211_band band = nla_type(tx_rates);
4671 		int err;
4672 
4673 		if (band < 0 || band >= NUM_NL80211_BANDS)
4674 			return -EINVAL;
4675 		sband = rdev->wiphy.bands[band];
4676 		if (sband == NULL)
4677 			return -EINVAL;
4678 		err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4679 						  tx_rates,
4680 						  nl80211_txattr_policy,
4681 						  info->extack);
4682 		if (err)
4683 			return err;
4684 		if (tb[NL80211_TXRATE_LEGACY]) {
4685 			mask->control[band].legacy = rateset_to_mask(
4686 				sband,
4687 				nla_data(tb[NL80211_TXRATE_LEGACY]),
4688 				nla_len(tb[NL80211_TXRATE_LEGACY]));
4689 			if ((mask->control[band].legacy == 0) &&
4690 			    nla_len(tb[NL80211_TXRATE_LEGACY]))
4691 				return -EINVAL;
4692 		}
4693 		if (tb[NL80211_TXRATE_HT]) {
4694 			if (!ht_rateset_to_mask(
4695 					sband,
4696 					nla_data(tb[NL80211_TXRATE_HT]),
4697 					nla_len(tb[NL80211_TXRATE_HT]),
4698 					mask->control[band].ht_mcs))
4699 				return -EINVAL;
4700 		}
4701 		if (tb[NL80211_TXRATE_VHT]) {
4702 			if (!vht_set_mcs_mask(
4703 					sband,
4704 					nla_data(tb[NL80211_TXRATE_VHT]),
4705 					mask->control[band].vht_mcs))
4706 				return -EINVAL;
4707 		}
4708 		if (tb[NL80211_TXRATE_GI]) {
4709 			mask->control[band].gi =
4710 				nla_get_u8(tb[NL80211_TXRATE_GI]);
4711 			if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4712 				return -EINVAL;
4713 		}
4714 		if (tb[NL80211_TXRATE_HE] &&
4715 		    !he_set_mcs_mask(info, wdev, sband,
4716 				     nla_data(tb[NL80211_TXRATE_HE]),
4717 				     mask->control[band].he_mcs))
4718 			return -EINVAL;
4719 		if (tb[NL80211_TXRATE_HE_GI])
4720 			mask->control[band].he_gi =
4721 				nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
4722 		if (tb[NL80211_TXRATE_HE_LTF])
4723 			mask->control[band].he_ltf =
4724 				nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
4725 
4726 		if (mask->control[band].legacy == 0) {
4727 			/* don't allow empty legacy rates if HT, VHT or HE
4728 			 * are not even supported.
4729 			 */
4730 			if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4731 			      rdev->wiphy.bands[band]->vht_cap.vht_supported ||
4732 			      ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
4733 				return -EINVAL;
4734 
4735 			for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4736 				if (mask->control[band].ht_mcs[i])
4737 					goto out;
4738 
4739 			for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4740 				if (mask->control[band].vht_mcs[i])
4741 					goto out;
4742 
4743 			for (i = 0; i < NL80211_HE_NSS_MAX; i++)
4744 				if (mask->control[band].he_mcs[i])
4745 					goto out;
4746 
4747 			/* legacy and mcs rates may not be both empty */
4748 			return -EINVAL;
4749 		}
4750 	}
4751 
4752 out:
4753 	return 0;
4754 }
4755 
validate_beacon_tx_rate(struct cfg80211_registered_device * rdev,enum nl80211_band band,struct cfg80211_bitrate_mask * beacon_rate)4756 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4757 				   enum nl80211_band band,
4758 				   struct cfg80211_bitrate_mask *beacon_rate)
4759 {
4760 	u32 count_ht, count_vht, i;
4761 	u32 rate = beacon_rate->control[band].legacy;
4762 
4763 	/* Allow only one rate */
4764 	if (hweight32(rate) > 1)
4765 		return -EINVAL;
4766 
4767 	count_ht = 0;
4768 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4769 		if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4770 			return -EINVAL;
4771 		} else if (beacon_rate->control[band].ht_mcs[i]) {
4772 			count_ht++;
4773 			if (count_ht > 1)
4774 				return -EINVAL;
4775 		}
4776 		if (count_ht && rate)
4777 			return -EINVAL;
4778 	}
4779 
4780 	count_vht = 0;
4781 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4782 		if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4783 			return -EINVAL;
4784 		} else if (beacon_rate->control[band].vht_mcs[i]) {
4785 			count_vht++;
4786 			if (count_vht > 1)
4787 				return -EINVAL;
4788 		}
4789 		if (count_vht && rate)
4790 			return -EINVAL;
4791 	}
4792 
4793 	if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4794 		return -EINVAL;
4795 
4796 	if (rate &&
4797 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4798 				     NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4799 		return -EINVAL;
4800 	if (count_ht &&
4801 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4802 				     NL80211_EXT_FEATURE_BEACON_RATE_HT))
4803 		return -EINVAL;
4804 	if (count_vht &&
4805 	    !wiphy_ext_feature_isset(&rdev->wiphy,
4806 				     NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4807 		return -EINVAL;
4808 
4809 	return 0;
4810 }
4811 
nl80211_parse_beacon(struct cfg80211_registered_device * rdev,struct nlattr * attrs[],struct cfg80211_beacon_data * bcn)4812 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4813 				struct nlattr *attrs[],
4814 				struct cfg80211_beacon_data *bcn)
4815 {
4816 	bool haveinfo = false;
4817 	int err;
4818 
4819 	memset(bcn, 0, sizeof(*bcn));
4820 
4821 	if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4822 		bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4823 		bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4824 		if (!bcn->head_len)
4825 			return -EINVAL;
4826 		haveinfo = true;
4827 	}
4828 
4829 	if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4830 		bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4831 		bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4832 		haveinfo = true;
4833 	}
4834 
4835 	if (!haveinfo)
4836 		return -EINVAL;
4837 
4838 	if (attrs[NL80211_ATTR_IE]) {
4839 		bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4840 		bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4841 	}
4842 
4843 	if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4844 		bcn->proberesp_ies =
4845 			nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4846 		bcn->proberesp_ies_len =
4847 			nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4848 	}
4849 
4850 	if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4851 		bcn->assocresp_ies =
4852 			nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4853 		bcn->assocresp_ies_len =
4854 			nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4855 	}
4856 
4857 	if (attrs[NL80211_ATTR_PROBE_RESP]) {
4858 		bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4859 		bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4860 	}
4861 
4862 	if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4863 		struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4864 
4865 		err = nla_parse_nested_deprecated(tb,
4866 						  NL80211_FTM_RESP_ATTR_MAX,
4867 						  attrs[NL80211_ATTR_FTM_RESPONDER],
4868 						  NULL, NULL);
4869 		if (err)
4870 			return err;
4871 
4872 		if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4873 		    wiphy_ext_feature_isset(&rdev->wiphy,
4874 					    NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4875 			bcn->ftm_responder = 1;
4876 		else
4877 			return -EOPNOTSUPP;
4878 
4879 		if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4880 			bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4881 			bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4882 		}
4883 
4884 		if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4885 			bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4886 			bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4887 		}
4888 	} else {
4889 		bcn->ftm_responder = -1;
4890 	}
4891 
4892 	return 0;
4893 }
4894 
nl80211_parse_he_obss_pd(struct nlattr * attrs,struct ieee80211_he_obss_pd * he_obss_pd)4895 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4896 				    struct ieee80211_he_obss_pd *he_obss_pd)
4897 {
4898 	struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4899 	int err;
4900 
4901 	err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4902 			       he_obss_pd_policy, NULL);
4903 	if (err)
4904 		return err;
4905 
4906 	if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
4907 		return -EINVAL;
4908 
4909 	he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
4910 
4911 	if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
4912 		he_obss_pd->min_offset =
4913 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4914 	if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4915 		he_obss_pd->max_offset =
4916 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4917 	if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
4918 		he_obss_pd->non_srg_max_offset =
4919 			nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
4920 
4921 	if (he_obss_pd->min_offset > he_obss_pd->max_offset)
4922 		return -EINVAL;
4923 
4924 	if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
4925 		memcpy(he_obss_pd->bss_color_bitmap,
4926 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
4927 		       sizeof(he_obss_pd->bss_color_bitmap));
4928 
4929 	if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
4930 		memcpy(he_obss_pd->partial_bssid_bitmap,
4931 		       nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
4932 		       sizeof(he_obss_pd->partial_bssid_bitmap));
4933 
4934 	he_obss_pd->enable = true;
4935 
4936 	return 0;
4937 }
4938 
nl80211_parse_he_bss_color(struct nlattr * attrs,struct cfg80211_he_bss_color * he_bss_color)4939 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
4940 				      struct cfg80211_he_bss_color *he_bss_color)
4941 {
4942 	struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
4943 	int err;
4944 
4945 	err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
4946 			       he_bss_color_policy, NULL);
4947 	if (err)
4948 		return err;
4949 
4950 	if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
4951 		return -EINVAL;
4952 
4953 	he_bss_color->color =
4954 		nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
4955 	he_bss_color->enabled =
4956 		!nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
4957 	he_bss_color->partial =
4958 		nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
4959 
4960 	return 0;
4961 }
4962 
nl80211_parse_fils_discovery(struct cfg80211_registered_device * rdev,struct nlattr * attrs,struct cfg80211_ap_settings * params)4963 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
4964 					struct nlattr *attrs,
4965 					struct cfg80211_ap_settings *params)
4966 {
4967 	struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
4968 	int ret;
4969 	struct cfg80211_fils_discovery *fd = &params->fils_discovery;
4970 
4971 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
4972 				     NL80211_EXT_FEATURE_FILS_DISCOVERY))
4973 		return -EINVAL;
4974 
4975 	ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
4976 			       NULL, NULL);
4977 	if (ret)
4978 		return ret;
4979 
4980 	if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
4981 	    !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
4982 	    !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
4983 		return -EINVAL;
4984 
4985 	fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
4986 	fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
4987 	fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
4988 	fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
4989 
4990 	return 0;
4991 }
4992 
4993 static int
nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device * rdev,struct nlattr * attrs,struct cfg80211_ap_settings * params)4994 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
4995 				     struct nlattr *attrs,
4996 				     struct cfg80211_ap_settings *params)
4997 {
4998 	struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
4999 	int ret;
5000 	struct cfg80211_unsol_bcast_probe_resp *presp =
5001 					&params->unsol_bcast_probe_resp;
5002 
5003 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
5004 				     NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5005 		return -EINVAL;
5006 
5007 	ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5008 			       attrs, NULL, NULL);
5009 	if (ret)
5010 		return ret;
5011 
5012 	if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5013 	    !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5014 		return -EINVAL;
5015 
5016 	presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5017 	presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5018 	presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5019 	return 0;
5020 }
5021 
nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings * params,const u8 * rates)5022 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5023 					    const u8 *rates)
5024 {
5025 	int i;
5026 
5027 	if (!rates)
5028 		return;
5029 
5030 	for (i = 0; i < rates[1]; i++) {
5031 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5032 			params->ht_required = true;
5033 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5034 			params->vht_required = true;
5035 		if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5036 			params->he_required = true;
5037 	}
5038 }
5039 
5040 /*
5041  * Since the nl80211 API didn't include, from the beginning, attributes about
5042  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5043  * benefit of drivers that rebuild IEs in the firmware.
5044  */
nl80211_calculate_ap_params(struct cfg80211_ap_settings * params)5045 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5046 {
5047 	const struct cfg80211_beacon_data *bcn = &params->beacon;
5048 	size_t ies_len = bcn->tail_len;
5049 	const u8 *ies = bcn->tail;
5050 	const u8 *rates;
5051 	const u8 *cap;
5052 
5053 	rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
5054 	nl80211_check_ap_rate_selectors(params, rates);
5055 
5056 	rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5057 	nl80211_check_ap_rate_selectors(params, rates);
5058 
5059 	cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5060 	if (cap && cap[1] >= sizeof(*params->ht_cap))
5061 		params->ht_cap = (void *)(cap + 2);
5062 	cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5063 	if (cap && cap[1] >= sizeof(*params->vht_cap))
5064 		params->vht_cap = (void *)(cap + 2);
5065 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5066 	if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
5067 		params->he_cap = (void *)(cap + 3);
5068 	cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5069 	if (cap && cap[1] >= sizeof(*params->he_oper) + 1)
5070 		params->he_oper = (void *)(cap + 3);
5071 }
5072 
nl80211_get_ap_channel(struct cfg80211_registered_device * rdev,struct cfg80211_ap_settings * params)5073 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5074 				   struct cfg80211_ap_settings *params)
5075 {
5076 	struct wireless_dev *wdev;
5077 	bool ret = false;
5078 
5079 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5080 		if (wdev->iftype != NL80211_IFTYPE_AP &&
5081 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)
5082 			continue;
5083 
5084 		if (!wdev->preset_chandef.chan)
5085 			continue;
5086 
5087 		params->chandef = wdev->preset_chandef;
5088 		ret = true;
5089 		break;
5090 	}
5091 
5092 	return ret;
5093 }
5094 
nl80211_valid_auth_type(struct cfg80211_registered_device * rdev,enum nl80211_auth_type auth_type,enum nl80211_commands cmd)5095 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5096 				    enum nl80211_auth_type auth_type,
5097 				    enum nl80211_commands cmd)
5098 {
5099 	if (auth_type > NL80211_AUTHTYPE_MAX)
5100 		return false;
5101 
5102 	switch (cmd) {
5103 	case NL80211_CMD_AUTHENTICATE:
5104 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5105 		    auth_type == NL80211_AUTHTYPE_SAE)
5106 			return false;
5107 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5108 					     NL80211_EXT_FEATURE_FILS_STA) &&
5109 		    (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5110 		     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5111 		     auth_type == NL80211_AUTHTYPE_FILS_PK))
5112 			return false;
5113 		return true;
5114 	case NL80211_CMD_CONNECT:
5115 		if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5116 		    !wiphy_ext_feature_isset(&rdev->wiphy,
5117 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5118 		    auth_type == NL80211_AUTHTYPE_SAE)
5119 			return false;
5120 
5121 		/* FILS with SK PFS or PK not supported yet */
5122 		if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5123 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5124 			return false;
5125 		if (!wiphy_ext_feature_isset(
5126 			    &rdev->wiphy,
5127 			    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5128 		    auth_type == NL80211_AUTHTYPE_FILS_SK)
5129 			return false;
5130 		return true;
5131 	case NL80211_CMD_START_AP:
5132 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
5133 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5134 		    auth_type == NL80211_AUTHTYPE_SAE)
5135 			return false;
5136 		/* FILS not supported yet */
5137 		if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5138 		    auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5139 		    auth_type == NL80211_AUTHTYPE_FILS_PK)
5140 			return false;
5141 		return true;
5142 	default:
5143 		return false;
5144 	}
5145 }
5146 
nl80211_start_ap(struct sk_buff * skb,struct genl_info * info)5147 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5148 {
5149 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5150 	struct net_device *dev = info->user_ptr[1];
5151 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5152 	struct cfg80211_ap_settings params;
5153 	int err;
5154 
5155 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5156 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5157 		return -EOPNOTSUPP;
5158 
5159 	if (!rdev->ops->start_ap)
5160 		return -EOPNOTSUPP;
5161 
5162 	if (wdev->beacon_interval)
5163 		return -EALREADY;
5164 
5165 	memset(&params, 0, sizeof(params));
5166 
5167 	/* these are required for START_AP */
5168 	if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5169 	    !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5170 	    !info->attrs[NL80211_ATTR_BEACON_HEAD])
5171 		return -EINVAL;
5172 
5173 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
5174 	if (err)
5175 		return err;
5176 
5177 	params.beacon_interval =
5178 		nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5179 	params.dtim_period =
5180 		nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5181 
5182 	err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5183 					   params.beacon_interval);
5184 	if (err)
5185 		return err;
5186 
5187 	/*
5188 	 * In theory, some of these attributes should be required here
5189 	 * but since they were not used when the command was originally
5190 	 * added, keep them optional for old user space programs to let
5191 	 * them continue to work with drivers that do not need the
5192 	 * additional information -- drivers must check!
5193 	 */
5194 	if (info->attrs[NL80211_ATTR_SSID]) {
5195 		params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5196 		params.ssid_len =
5197 			nla_len(info->attrs[NL80211_ATTR_SSID]);
5198 		if (params.ssid_len == 0)
5199 			return -EINVAL;
5200 	}
5201 
5202 	if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5203 		params.hidden_ssid = nla_get_u32(
5204 			info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5205 
5206 	params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5207 
5208 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5209 		params.auth_type = nla_get_u32(
5210 			info->attrs[NL80211_ATTR_AUTH_TYPE]);
5211 		if (!nl80211_valid_auth_type(rdev, params.auth_type,
5212 					     NL80211_CMD_START_AP))
5213 			return -EINVAL;
5214 	} else
5215 		params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5216 
5217 	err = nl80211_crypto_settings(rdev, info, &params.crypto,
5218 				      NL80211_MAX_NR_CIPHER_SUITES);
5219 	if (err)
5220 		return err;
5221 
5222 	if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5223 		if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
5224 			return -EOPNOTSUPP;
5225 		params.inactivity_timeout = nla_get_u16(
5226 			info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
5227 	}
5228 
5229 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5230 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5231 			return -EINVAL;
5232 		params.p2p_ctwindow =
5233 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5234 		if (params.p2p_ctwindow != 0 &&
5235 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5236 			return -EINVAL;
5237 	}
5238 
5239 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5240 		u8 tmp;
5241 
5242 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5243 			return -EINVAL;
5244 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5245 		params.p2p_opp_ps = tmp;
5246 		if (params.p2p_opp_ps != 0 &&
5247 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5248 			return -EINVAL;
5249 	}
5250 
5251 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5252 		err = nl80211_parse_chandef(rdev, info, &params.chandef);
5253 		if (err)
5254 			return err;
5255 	} else if (wdev->preset_chandef.chan) {
5256 		params.chandef = wdev->preset_chandef;
5257 	} else if (!nl80211_get_ap_channel(rdev, &params))
5258 		return -EINVAL;
5259 
5260 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
5261 					   wdev->iftype))
5262 		return -EINVAL;
5263 
5264 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
5265 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
5266 						    NL80211_ATTR_TX_RATES,
5267 						    &params.beacon_rate,
5268 						    dev);
5269 		if (err)
5270 			return err;
5271 
5272 		err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
5273 					      &params.beacon_rate);
5274 		if (err)
5275 			return err;
5276 	}
5277 
5278 	if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
5279 		params.smps_mode =
5280 			nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
5281 		switch (params.smps_mode) {
5282 		case NL80211_SMPS_OFF:
5283 			break;
5284 		case NL80211_SMPS_STATIC:
5285 			if (!(rdev->wiphy.features &
5286 			      NL80211_FEATURE_STATIC_SMPS))
5287 				return -EINVAL;
5288 			break;
5289 		case NL80211_SMPS_DYNAMIC:
5290 			if (!(rdev->wiphy.features &
5291 			      NL80211_FEATURE_DYNAMIC_SMPS))
5292 				return -EINVAL;
5293 			break;
5294 		default:
5295 			return -EINVAL;
5296 		}
5297 	} else {
5298 		params.smps_mode = NL80211_SMPS_OFF;
5299 	}
5300 
5301 	params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5302 	if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
5303 		return -EOPNOTSUPP;
5304 
5305 	if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5306 		params.acl = parse_acl_data(&rdev->wiphy, info);
5307 		if (IS_ERR(params.acl))
5308 			return PTR_ERR(params.acl);
5309 	}
5310 
5311 	params.twt_responder =
5312 		    nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5313 
5314 	if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5315 		err = nl80211_parse_he_obss_pd(
5316 					info->attrs[NL80211_ATTR_HE_OBSS_PD],
5317 					&params.he_obss_pd);
5318 		if (err)
5319 			goto out;
5320 	}
5321 
5322 	if (info->attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5323 		err = nl80211_parse_he_bss_color(
5324 					info->attrs[NL80211_ATTR_HE_BSS_COLOR],
5325 					&params.he_bss_color);
5326 		if (err)
5327 			goto out;
5328 	}
5329 
5330 	if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
5331 		err = nl80211_parse_fils_discovery(rdev,
5332 						   info->attrs[NL80211_ATTR_FILS_DISCOVERY],
5333 						   &params);
5334 		if (err)
5335 			goto out;
5336 	}
5337 
5338 	if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
5339 		err = nl80211_parse_unsol_bcast_probe_resp(
5340 			rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
5341 			&params);
5342 		if (err)
5343 			goto out;
5344 	}
5345 
5346 	nl80211_calculate_ap_params(&params);
5347 
5348 	if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
5349 		params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
5350 
5351 	wdev_lock(wdev);
5352 	err = rdev_start_ap(rdev, dev, &params);
5353 	if (!err) {
5354 		wdev->preset_chandef = params.chandef;
5355 		wdev->beacon_interval = params.beacon_interval;
5356 		wdev->chandef = params.chandef;
5357 		wdev->ssid_len = params.ssid_len;
5358 		memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
5359 
5360 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
5361 			wdev->conn_owner_nlportid = info->snd_portid;
5362 	}
5363 	wdev_unlock(wdev);
5364 
5365 out:
5366 	kfree(params.acl);
5367 
5368 	return err;
5369 }
5370 
nl80211_set_beacon(struct sk_buff * skb,struct genl_info * info)5371 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
5372 {
5373 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5374 	struct net_device *dev = info->user_ptr[1];
5375 	struct wireless_dev *wdev = dev->ieee80211_ptr;
5376 	struct cfg80211_beacon_data params;
5377 	int err;
5378 
5379 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5380 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5381 		return -EOPNOTSUPP;
5382 
5383 	if (!rdev->ops->change_beacon)
5384 		return -EOPNOTSUPP;
5385 
5386 	if (!wdev->beacon_interval)
5387 		return -EINVAL;
5388 
5389 	err = nl80211_parse_beacon(rdev, info->attrs, &params);
5390 	if (err)
5391 		return err;
5392 
5393 	wdev_lock(wdev);
5394 	err = rdev_change_beacon(rdev, dev, &params);
5395 	wdev_unlock(wdev);
5396 
5397 	return err;
5398 }
5399 
nl80211_stop_ap(struct sk_buff * skb,struct genl_info * info)5400 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
5401 {
5402 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5403 	struct net_device *dev = info->user_ptr[1];
5404 
5405 	return cfg80211_stop_ap(rdev, dev, false);
5406 }
5407 
5408 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
5409 	[NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
5410 	[NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
5411 	[NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
5412 	[NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
5413 	[NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
5414 	[NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
5415 };
5416 
parse_station_flags(struct genl_info * info,enum nl80211_iftype iftype,struct station_parameters * params)5417 static int parse_station_flags(struct genl_info *info,
5418 			       enum nl80211_iftype iftype,
5419 			       struct station_parameters *params)
5420 {
5421 	struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
5422 	struct nlattr *nla;
5423 	int flag;
5424 
5425 	/*
5426 	 * Try parsing the new attribute first so userspace
5427 	 * can specify both for older kernels.
5428 	 */
5429 	nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
5430 	if (nla) {
5431 		struct nl80211_sta_flag_update *sta_flags;
5432 
5433 		sta_flags = nla_data(nla);
5434 		params->sta_flags_mask = sta_flags->mask;
5435 		params->sta_flags_set = sta_flags->set;
5436 		params->sta_flags_set &= params->sta_flags_mask;
5437 		if ((params->sta_flags_mask |
5438 		     params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
5439 			return -EINVAL;
5440 		return 0;
5441 	}
5442 
5443 	/* if present, parse the old attribute */
5444 
5445 	nla = info->attrs[NL80211_ATTR_STA_FLAGS];
5446 	if (!nla)
5447 		return 0;
5448 
5449 	if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
5450 		return -EINVAL;
5451 
5452 	/*
5453 	 * Only allow certain flags for interface types so that
5454 	 * other attributes are silently ignored. Remember that
5455 	 * this is backward compatibility code with old userspace
5456 	 * and shouldn't be hit in other cases anyway.
5457 	 */
5458 	switch (iftype) {
5459 	case NL80211_IFTYPE_AP:
5460 	case NL80211_IFTYPE_AP_VLAN:
5461 	case NL80211_IFTYPE_P2P_GO:
5462 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5463 					 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5464 					 BIT(NL80211_STA_FLAG_WME) |
5465 					 BIT(NL80211_STA_FLAG_MFP);
5466 		break;
5467 	case NL80211_IFTYPE_P2P_CLIENT:
5468 	case NL80211_IFTYPE_STATION:
5469 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
5470 					 BIT(NL80211_STA_FLAG_TDLS_PEER);
5471 		break;
5472 	case NL80211_IFTYPE_MESH_POINT:
5473 		params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5474 					 BIT(NL80211_STA_FLAG_MFP) |
5475 					 BIT(NL80211_STA_FLAG_AUTHORIZED);
5476 		break;
5477 	default:
5478 		return -EINVAL;
5479 	}
5480 
5481 	for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
5482 		if (flags[flag]) {
5483 			params->sta_flags_set |= (1<<flag);
5484 
5485 			/* no longer support new API additions in old API */
5486 			if (flag > NL80211_STA_FLAG_MAX_OLD_API)
5487 				return -EINVAL;
5488 		}
5489 	}
5490 
5491 	return 0;
5492 }
5493 
nl80211_put_sta_rate(struct sk_buff * msg,struct rate_info * info,int attr)5494 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
5495 {
5496 	struct nlattr *rate;
5497 	u32 bitrate;
5498 	u16 bitrate_compat;
5499 	enum nl80211_rate_info rate_flg;
5500 
5501 	rate = nla_nest_start_noflag(msg, attr);
5502 	if (!rate)
5503 		return false;
5504 
5505 	/* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
5506 	bitrate = cfg80211_calculate_bitrate(info);
5507 	/* report 16-bit bitrate only if we can */
5508 	bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
5509 	if (bitrate > 0 &&
5510 	    nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
5511 		return false;
5512 	if (bitrate_compat > 0 &&
5513 	    nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
5514 		return false;
5515 
5516 	switch (info->bw) {
5517 	case RATE_INFO_BW_5:
5518 		rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
5519 		break;
5520 	case RATE_INFO_BW_10:
5521 		rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
5522 		break;
5523 	default:
5524 		WARN_ON(1);
5525 		fallthrough;
5526 	case RATE_INFO_BW_20:
5527 		rate_flg = 0;
5528 		break;
5529 	case RATE_INFO_BW_40:
5530 		rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
5531 		break;
5532 	case RATE_INFO_BW_80:
5533 		rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
5534 		break;
5535 	case RATE_INFO_BW_160:
5536 		rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
5537 		break;
5538 	case RATE_INFO_BW_HE_RU:
5539 		rate_flg = 0;
5540 		WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
5541 	}
5542 
5543 	if (rate_flg && nla_put_flag(msg, rate_flg))
5544 		return false;
5545 
5546 	if (info->flags & RATE_INFO_FLAGS_MCS) {
5547 		if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5548 			return false;
5549 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5550 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5551 			return false;
5552 	} else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5553 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5554 			return false;
5555 		if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5556 			return false;
5557 		if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5558 		    nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5559 			return false;
5560 	} else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5561 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5562 			return false;
5563 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5564 			return false;
5565 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5566 			return false;
5567 		if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5568 			return false;
5569 		if (info->bw == RATE_INFO_BW_HE_RU &&
5570 		    nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5571 			       info->he_ru_alloc))
5572 			return false;
5573 	}
5574 
5575 	nla_nest_end(msg, rate);
5576 	return true;
5577 }
5578 
nl80211_put_signal(struct sk_buff * msg,u8 mask,s8 * signal,int id)5579 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5580 			       int id)
5581 {
5582 	void *attr;
5583 	int i = 0;
5584 
5585 	if (!mask)
5586 		return true;
5587 
5588 	attr = nla_nest_start_noflag(msg, id);
5589 	if (!attr)
5590 		return false;
5591 
5592 	for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5593 		if (!(mask & BIT(i)))
5594 			continue;
5595 
5596 		if (nla_put_u8(msg, i, signal[i]))
5597 			return false;
5598 	}
5599 
5600 	nla_nest_end(msg, attr);
5601 
5602 	return true;
5603 }
5604 
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)5605 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5606 				u32 seq, int flags,
5607 				struct cfg80211_registered_device *rdev,
5608 				struct net_device *dev,
5609 				const u8 *mac_addr, struct station_info *sinfo)
5610 {
5611 	void *hdr;
5612 	struct nlattr *sinfoattr, *bss_param;
5613 
5614 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5615 	if (!hdr) {
5616 		cfg80211_sinfo_release_content(sinfo);
5617 		return -1;
5618 	}
5619 
5620 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5621 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5622 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5623 		goto nla_put_failure;
5624 
5625 	sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5626 	if (!sinfoattr)
5627 		goto nla_put_failure;
5628 
5629 #define PUT_SINFO(attr, memb, type) do {				\
5630 	BUILD_BUG_ON(sizeof(type) == sizeof(u64));			\
5631 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5632 	    nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,		\
5633 			     sinfo->memb))				\
5634 		goto nla_put_failure;					\
5635 	} while (0)
5636 #define PUT_SINFO_U64(attr, memb) do {					\
5637 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&	\
5638 	    nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,		\
5639 			      sinfo->memb, NL80211_STA_INFO_PAD))	\
5640 		goto nla_put_failure;					\
5641 	} while (0)
5642 
5643 	PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5644 	PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5645 	PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5646 
5647 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5648 			     BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5649 	    nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5650 			(u32)sinfo->rx_bytes))
5651 		goto nla_put_failure;
5652 
5653 	if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5654 			     BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5655 	    nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5656 			(u32)sinfo->tx_bytes))
5657 		goto nla_put_failure;
5658 
5659 	PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5660 	PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5661 	PUT_SINFO(LLID, llid, u16);
5662 	PUT_SINFO(PLID, plid, u16);
5663 	PUT_SINFO(PLINK_STATE, plink_state, u8);
5664 	PUT_SINFO_U64(RX_DURATION, rx_duration);
5665 	PUT_SINFO_U64(TX_DURATION, tx_duration);
5666 
5667 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5668 				    NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5669 		PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5670 
5671 	switch (rdev->wiphy.signal_type) {
5672 	case CFG80211_SIGNAL_TYPE_MBM:
5673 		PUT_SINFO(SIGNAL, signal, u8);
5674 		PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5675 		break;
5676 	default:
5677 		break;
5678 	}
5679 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5680 		if (!nl80211_put_signal(msg, sinfo->chains,
5681 					sinfo->chain_signal,
5682 					NL80211_STA_INFO_CHAIN_SIGNAL))
5683 			goto nla_put_failure;
5684 	}
5685 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5686 		if (!nl80211_put_signal(msg, sinfo->chains,
5687 					sinfo->chain_signal_avg,
5688 					NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5689 			goto nla_put_failure;
5690 	}
5691 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5692 		if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5693 					  NL80211_STA_INFO_TX_BITRATE))
5694 			goto nla_put_failure;
5695 	}
5696 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5697 		if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5698 					  NL80211_STA_INFO_RX_BITRATE))
5699 			goto nla_put_failure;
5700 	}
5701 
5702 	PUT_SINFO(RX_PACKETS, rx_packets, u32);
5703 	PUT_SINFO(TX_PACKETS, tx_packets, u32);
5704 	PUT_SINFO(TX_RETRIES, tx_retries, u32);
5705 	PUT_SINFO(TX_FAILED, tx_failed, u32);
5706 	PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5707 	PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5708 	PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5709 	PUT_SINFO(LOCAL_PM, local_pm, u32);
5710 	PUT_SINFO(PEER_PM, peer_pm, u32);
5711 	PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5712 	PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5713 	PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
5714 
5715 	if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5716 		bss_param = nla_nest_start_noflag(msg,
5717 						  NL80211_STA_INFO_BSS_PARAM);
5718 		if (!bss_param)
5719 			goto nla_put_failure;
5720 
5721 		if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5722 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5723 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5724 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5725 		    ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5726 		     nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5727 		    nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5728 			       sinfo->bss_param.dtim_period) ||
5729 		    nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5730 				sinfo->bss_param.beacon_interval))
5731 			goto nla_put_failure;
5732 
5733 		nla_nest_end(msg, bss_param);
5734 	}
5735 	if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5736 	    nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5737 		    sizeof(struct nl80211_sta_flag_update),
5738 		    &sinfo->sta_flags))
5739 		goto nla_put_failure;
5740 
5741 	PUT_SINFO_U64(T_OFFSET, t_offset);
5742 	PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5743 	PUT_SINFO_U64(BEACON_RX, rx_beacon);
5744 	PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5745 	PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5746 	PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5747 	if (wiphy_ext_feature_isset(&rdev->wiphy,
5748 				    NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5749 		PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5750 		PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5751 	}
5752 
5753 #undef PUT_SINFO
5754 #undef PUT_SINFO_U64
5755 
5756 	if (sinfo->pertid) {
5757 		struct nlattr *tidsattr;
5758 		int tid;
5759 
5760 		tidsattr = nla_nest_start_noflag(msg,
5761 						 NL80211_STA_INFO_TID_STATS);
5762 		if (!tidsattr)
5763 			goto nla_put_failure;
5764 
5765 		for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5766 			struct cfg80211_tid_stats *tidstats;
5767 			struct nlattr *tidattr;
5768 
5769 			tidstats = &sinfo->pertid[tid];
5770 
5771 			if (!tidstats->filled)
5772 				continue;
5773 
5774 			tidattr = nla_nest_start_noflag(msg, tid + 1);
5775 			if (!tidattr)
5776 				goto nla_put_failure;
5777 
5778 #define PUT_TIDVAL_U64(attr, memb) do {					\
5779 	if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&	\
5780 	    nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,		\
5781 			      tidstats->memb, NL80211_TID_STATS_PAD))	\
5782 		goto nla_put_failure;					\
5783 	} while (0)
5784 
5785 			PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5786 			PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5787 			PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5788 			PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5789 
5790 #undef PUT_TIDVAL_U64
5791 			if ((tidstats->filled &
5792 			     BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5793 			    !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5794 						   NL80211_TID_STATS_TXQ_STATS))
5795 				goto nla_put_failure;
5796 
5797 			nla_nest_end(msg, tidattr);
5798 		}
5799 
5800 		nla_nest_end(msg, tidsattr);
5801 	}
5802 
5803 	nla_nest_end(msg, sinfoattr);
5804 
5805 	if (sinfo->assoc_req_ies_len &&
5806 	    nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5807 		    sinfo->assoc_req_ies))
5808 		goto nla_put_failure;
5809 
5810 	cfg80211_sinfo_release_content(sinfo);
5811 	genlmsg_end(msg, hdr);
5812 	return 0;
5813 
5814  nla_put_failure:
5815 	cfg80211_sinfo_release_content(sinfo);
5816 	genlmsg_cancel(msg, hdr);
5817 	return -EMSGSIZE;
5818 }
5819 
nl80211_dump_station(struct sk_buff * skb,struct netlink_callback * cb)5820 static int nl80211_dump_station(struct sk_buff *skb,
5821 				struct netlink_callback *cb)
5822 {
5823 	struct station_info sinfo;
5824 	struct cfg80211_registered_device *rdev;
5825 	struct wireless_dev *wdev;
5826 	u8 mac_addr[ETH_ALEN];
5827 	int sta_idx = cb->args[2];
5828 	int err;
5829 
5830 	rtnl_lock();
5831 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5832 	if (err)
5833 		goto out_err;
5834 
5835 	if (!wdev->netdev) {
5836 		err = -EINVAL;
5837 		goto out_err;
5838 	}
5839 
5840 	if (!rdev->ops->dump_station) {
5841 		err = -EOPNOTSUPP;
5842 		goto out_err;
5843 	}
5844 
5845 	while (1) {
5846 		memset(&sinfo, 0, sizeof(sinfo));
5847 		err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5848 					mac_addr, &sinfo);
5849 		if (err == -ENOENT)
5850 			break;
5851 		if (err)
5852 			goto out_err;
5853 
5854 		if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5855 				NETLINK_CB(cb->skb).portid,
5856 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
5857 				rdev, wdev->netdev, mac_addr,
5858 				&sinfo) < 0)
5859 			goto out;
5860 
5861 		sta_idx++;
5862 	}
5863 
5864  out:
5865 	cb->args[2] = sta_idx;
5866 	err = skb->len;
5867  out_err:
5868 	rtnl_unlock();
5869 
5870 	return err;
5871 }
5872 
nl80211_get_station(struct sk_buff * skb,struct genl_info * info)5873 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5874 {
5875 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
5876 	struct net_device *dev = info->user_ptr[1];
5877 	struct station_info sinfo;
5878 	struct sk_buff *msg;
5879 	u8 *mac_addr = NULL;
5880 	int err;
5881 
5882 	memset(&sinfo, 0, sizeof(sinfo));
5883 
5884 	if (!info->attrs[NL80211_ATTR_MAC])
5885 		return -EINVAL;
5886 
5887 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5888 
5889 	if (!rdev->ops->get_station)
5890 		return -EOPNOTSUPP;
5891 
5892 	err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5893 	if (err)
5894 		return err;
5895 
5896 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5897 	if (!msg) {
5898 		cfg80211_sinfo_release_content(&sinfo);
5899 		return -ENOMEM;
5900 	}
5901 
5902 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5903 				 info->snd_portid, info->snd_seq, 0,
5904 				 rdev, dev, mac_addr, &sinfo) < 0) {
5905 		nlmsg_free(msg);
5906 		return -ENOBUFS;
5907 	}
5908 
5909 	return genlmsg_reply(msg, info);
5910 }
5911 
cfg80211_check_station_change(struct wiphy * wiphy,struct station_parameters * params,enum cfg80211_station_type statype)5912 int cfg80211_check_station_change(struct wiphy *wiphy,
5913 				  struct station_parameters *params,
5914 				  enum cfg80211_station_type statype)
5915 {
5916 	if (params->listen_interval != -1 &&
5917 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5918 		return -EINVAL;
5919 
5920 	if (params->support_p2p_ps != -1 &&
5921 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5922 		return -EINVAL;
5923 
5924 	if (params->aid &&
5925 	    !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5926 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5927 		return -EINVAL;
5928 
5929 	/* When you run into this, adjust the code below for the new flag */
5930 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5931 
5932 	switch (statype) {
5933 	case CFG80211_STA_MESH_PEER_KERNEL:
5934 	case CFG80211_STA_MESH_PEER_USER:
5935 		/*
5936 		 * No ignoring the TDLS flag here -- the userspace mesh
5937 		 * code doesn't have the bug of including TDLS in the
5938 		 * mask everywhere.
5939 		 */
5940 		if (params->sta_flags_mask &
5941 				~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5942 				  BIT(NL80211_STA_FLAG_MFP) |
5943 				  BIT(NL80211_STA_FLAG_AUTHORIZED)))
5944 			return -EINVAL;
5945 		break;
5946 	case CFG80211_STA_TDLS_PEER_SETUP:
5947 	case CFG80211_STA_TDLS_PEER_ACTIVE:
5948 		if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5949 			return -EINVAL;
5950 		/* ignore since it can't change */
5951 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5952 		break;
5953 	default:
5954 		/* disallow mesh-specific things */
5955 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5956 			return -EINVAL;
5957 		if (params->local_pm)
5958 			return -EINVAL;
5959 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5960 			return -EINVAL;
5961 	}
5962 
5963 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5964 	    statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5965 		/* TDLS can't be set, ... */
5966 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5967 			return -EINVAL;
5968 		/*
5969 		 * ... but don't bother the driver with it. This works around
5970 		 * a hostapd/wpa_supplicant issue -- it always includes the
5971 		 * TLDS_PEER flag in the mask even for AP mode.
5972 		 */
5973 		params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5974 	}
5975 
5976 	if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5977 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5978 		/* reject other things that can't change */
5979 		if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5980 			return -EINVAL;
5981 		if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5982 			return -EINVAL;
5983 		if (params->supported_rates)
5984 			return -EINVAL;
5985 		if (params->ext_capab || params->ht_capa || params->vht_capa ||
5986 		    params->he_capa)
5987 			return -EINVAL;
5988 	}
5989 
5990 	if (statype != CFG80211_STA_AP_CLIENT &&
5991 	    statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5992 		if (params->vlan)
5993 			return -EINVAL;
5994 	}
5995 
5996 	switch (statype) {
5997 	case CFG80211_STA_AP_MLME_CLIENT:
5998 		/* Use this only for authorizing/unauthorizing a station */
5999 		if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6000 			return -EOPNOTSUPP;
6001 		break;
6002 	case CFG80211_STA_AP_CLIENT:
6003 	case CFG80211_STA_AP_CLIENT_UNASSOC:
6004 		/* accept only the listed bits */
6005 		if (params->sta_flags_mask &
6006 				~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6007 				  BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6008 				  BIT(NL80211_STA_FLAG_ASSOCIATED) |
6009 				  BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6010 				  BIT(NL80211_STA_FLAG_WME) |
6011 				  BIT(NL80211_STA_FLAG_MFP)))
6012 			return -EINVAL;
6013 
6014 		/* but authenticated/associated only if driver handles it */
6015 		if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6016 		    params->sta_flags_mask &
6017 				(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6018 				 BIT(NL80211_STA_FLAG_ASSOCIATED)))
6019 			return -EINVAL;
6020 		break;
6021 	case CFG80211_STA_IBSS:
6022 	case CFG80211_STA_AP_STA:
6023 		/* reject any changes other than AUTHORIZED */
6024 		if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6025 			return -EINVAL;
6026 		break;
6027 	case CFG80211_STA_TDLS_PEER_SETUP:
6028 		/* reject any changes other than AUTHORIZED or WME */
6029 		if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6030 					       BIT(NL80211_STA_FLAG_WME)))
6031 			return -EINVAL;
6032 		/* force (at least) rates when authorizing */
6033 		if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6034 		    !params->supported_rates)
6035 			return -EINVAL;
6036 		break;
6037 	case CFG80211_STA_TDLS_PEER_ACTIVE:
6038 		/* reject any changes */
6039 		return -EINVAL;
6040 	case CFG80211_STA_MESH_PEER_KERNEL:
6041 		if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6042 			return -EINVAL;
6043 		break;
6044 	case CFG80211_STA_MESH_PEER_USER:
6045 		if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6046 		    params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6047 			return -EINVAL;
6048 		break;
6049 	}
6050 
6051 	/*
6052 	 * Older kernel versions ignored this attribute entirely, so don't
6053 	 * reject attempts to update it but mark it as unused instead so the
6054 	 * driver won't look at the data.
6055 	 */
6056 	if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6057 	    statype != CFG80211_STA_TDLS_PEER_SETUP)
6058 		params->opmode_notif_used = false;
6059 
6060 	return 0;
6061 }
6062 EXPORT_SYMBOL(cfg80211_check_station_change);
6063 
6064 /*
6065  * Get vlan interface making sure it is running and on the right wiphy.
6066  */
get_vlan(struct genl_info * info,struct cfg80211_registered_device * rdev)6067 static struct net_device *get_vlan(struct genl_info *info,
6068 				   struct cfg80211_registered_device *rdev)
6069 {
6070 	struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6071 	struct net_device *v;
6072 	int ret;
6073 
6074 	if (!vlanattr)
6075 		return NULL;
6076 
6077 	v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6078 	if (!v)
6079 		return ERR_PTR(-ENODEV);
6080 
6081 	if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
6082 		ret = -EINVAL;
6083 		goto error;
6084 	}
6085 
6086 	if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6087 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6088 	    v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6089 		ret = -EINVAL;
6090 		goto error;
6091 	}
6092 
6093 	if (!netif_running(v)) {
6094 		ret = -ENETDOWN;
6095 		goto error;
6096 	}
6097 
6098 	return v;
6099  error:
6100 	dev_put(v);
6101 	return ERR_PTR(ret);
6102 }
6103 
6104 static const struct nla_policy
6105 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
6106 	[NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
6107 	[NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
6108 };
6109 
nl80211_parse_sta_wme(struct genl_info * info,struct station_parameters * params)6110 static int nl80211_parse_sta_wme(struct genl_info *info,
6111 				 struct station_parameters *params)
6112 {
6113 	struct nlattr *tb[NL80211_STA_WME_MAX + 1];
6114 	struct nlattr *nla;
6115 	int err;
6116 
6117 	/* parse WME attributes if present */
6118 	if (!info->attrs[NL80211_ATTR_STA_WME])
6119 		return 0;
6120 
6121 	nla = info->attrs[NL80211_ATTR_STA_WME];
6122 	err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
6123 					  nl80211_sta_wme_policy,
6124 					  info->extack);
6125 	if (err)
6126 		return err;
6127 
6128 	if (tb[NL80211_STA_WME_UAPSD_QUEUES])
6129 		params->uapsd_queues = nla_get_u8(
6130 			tb[NL80211_STA_WME_UAPSD_QUEUES]);
6131 	if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
6132 		return -EINVAL;
6133 
6134 	if (tb[NL80211_STA_WME_MAX_SP])
6135 		params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
6136 
6137 	if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
6138 		return -EINVAL;
6139 
6140 	params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
6141 
6142 	return 0;
6143 }
6144 
nl80211_parse_sta_channel_info(struct genl_info * info,struct station_parameters * params)6145 static int nl80211_parse_sta_channel_info(struct genl_info *info,
6146 				      struct station_parameters *params)
6147 {
6148 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
6149 		params->supported_channels =
6150 		     nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6151 		params->supported_channels_len =
6152 		     nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6153 		/*
6154 		 * Need to include at least one (first channel, number of
6155 		 * channels) tuple for each subband (checked in policy),
6156 		 * and must have proper tuples for the rest of the data as well.
6157 		 */
6158 		if (params->supported_channels_len % 2)
6159 			return -EINVAL;
6160 	}
6161 
6162 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
6163 		params->supported_oper_classes =
6164 		 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6165 		params->supported_oper_classes_len =
6166 		  nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6167 	}
6168 	return 0;
6169 }
6170 
nl80211_set_station_tdls(struct genl_info * info,struct station_parameters * params)6171 static int nl80211_set_station_tdls(struct genl_info *info,
6172 				    struct station_parameters *params)
6173 {
6174 	int err;
6175 	/* Dummy STA entry gets updated once the peer capabilities are known */
6176 	if (info->attrs[NL80211_ATTR_PEER_AID])
6177 		params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6178 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6179 		params->ht_capa =
6180 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6181 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6182 		params->vht_capa =
6183 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6184 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6185 		params->he_capa =
6186 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6187 		params->he_capa_len =
6188 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6189 	}
6190 
6191 	err = nl80211_parse_sta_channel_info(info, params);
6192 	if (err)
6193 		return err;
6194 
6195 	return nl80211_parse_sta_wme(info, params);
6196 }
6197 
nl80211_parse_sta_txpower_setting(struct genl_info * info,struct station_parameters * params)6198 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
6199 					     struct station_parameters *params)
6200 {
6201 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6202 	int idx;
6203 
6204 	if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
6205 		if (!rdev->ops->set_tx_power ||
6206 		    !wiphy_ext_feature_isset(&rdev->wiphy,
6207 					 NL80211_EXT_FEATURE_STA_TX_PWR))
6208 			return -EOPNOTSUPP;
6209 
6210 		idx = NL80211_ATTR_STA_TX_POWER_SETTING;
6211 		params->txpwr.type = nla_get_u8(info->attrs[idx]);
6212 
6213 		if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
6214 			idx = NL80211_ATTR_STA_TX_POWER;
6215 
6216 			if (info->attrs[idx])
6217 				params->txpwr.power =
6218 					nla_get_s16(info->attrs[idx]);
6219 			else
6220 				return -EINVAL;
6221 		}
6222 		params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
6223 	}
6224 
6225 	return 0;
6226 }
6227 
nl80211_set_station(struct sk_buff * skb,struct genl_info * info)6228 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
6229 {
6230 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6231 	struct net_device *dev = info->user_ptr[1];
6232 	struct station_parameters params;
6233 	u8 *mac_addr;
6234 	int err;
6235 
6236 	memset(&params, 0, sizeof(params));
6237 
6238 	if (!rdev->ops->change_station)
6239 		return -EOPNOTSUPP;
6240 
6241 	/*
6242 	 * AID and listen_interval properties can be set only for unassociated
6243 	 * station. Include these parameters here and will check them in
6244 	 * cfg80211_check_station_change().
6245 	 */
6246 	if (info->attrs[NL80211_ATTR_STA_AID])
6247 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6248 
6249 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6250 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6251 
6252 	if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6253 		params.listen_interval =
6254 		     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6255 	else
6256 		params.listen_interval = -1;
6257 
6258 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
6259 		params.support_p2p_ps =
6260 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6261 	else
6262 		params.support_p2p_ps = -1;
6263 
6264 	if (!info->attrs[NL80211_ATTR_MAC])
6265 		return -EINVAL;
6266 
6267 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6268 
6269 	if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
6270 		params.supported_rates =
6271 			nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6272 		params.supported_rates_len =
6273 			nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6274 	}
6275 
6276 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
6277 		params.capability =
6278 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
6279 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
6280 	}
6281 
6282 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
6283 		params.ext_capab =
6284 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6285 		params.ext_capab_len =
6286 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6287 	}
6288 
6289 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
6290 		return -EINVAL;
6291 
6292 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
6293 		params.plink_action =
6294 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
6295 
6296 	if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
6297 		params.plink_state =
6298 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
6299 		if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
6300 			params.peer_aid = nla_get_u16(
6301 				info->attrs[NL80211_ATTR_MESH_PEER_AID]);
6302 		params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
6303 	}
6304 
6305 	if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
6306 		params.local_pm = nla_get_u32(
6307 			info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
6308 
6309 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6310 		params.opmode_notif_used = true;
6311 		params.opmode_notif =
6312 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6313 	}
6314 
6315 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6316 		params.he_6ghz_capa =
6317 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
6318 
6319 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6320 		params.airtime_weight =
6321 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6322 
6323 	if (params.airtime_weight &&
6324 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6325 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6326 		return -EOPNOTSUPP;
6327 
6328 	err = nl80211_parse_sta_txpower_setting(info, &params);
6329 	if (err)
6330 		return err;
6331 
6332 	/* Include parameters for TDLS peer (will check later) */
6333 	err = nl80211_set_station_tdls(info, &params);
6334 	if (err)
6335 		return err;
6336 
6337 	params.vlan = get_vlan(info, rdev);
6338 	if (IS_ERR(params.vlan))
6339 		return PTR_ERR(params.vlan);
6340 
6341 	switch (dev->ieee80211_ptr->iftype) {
6342 	case NL80211_IFTYPE_AP:
6343 	case NL80211_IFTYPE_AP_VLAN:
6344 	case NL80211_IFTYPE_P2P_GO:
6345 	case NL80211_IFTYPE_P2P_CLIENT:
6346 	case NL80211_IFTYPE_STATION:
6347 	case NL80211_IFTYPE_ADHOC:
6348 	case NL80211_IFTYPE_MESH_POINT:
6349 		break;
6350 	default:
6351 		err = -EOPNOTSUPP;
6352 		goto out_put_vlan;
6353 	}
6354 
6355 	/* driver will call cfg80211_check_station_change() */
6356 	err = rdev_change_station(rdev, dev, mac_addr, &params);
6357 
6358  out_put_vlan:
6359 	if (params.vlan)
6360 		dev_put(params.vlan);
6361 
6362 	return err;
6363 }
6364 
nl80211_new_station(struct sk_buff * skb,struct genl_info * info)6365 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
6366 {
6367 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6368 	int err;
6369 	struct net_device *dev = info->user_ptr[1];
6370 	struct station_parameters params;
6371 	u8 *mac_addr = NULL;
6372 	u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6373 			 BIT(NL80211_STA_FLAG_ASSOCIATED);
6374 
6375 	memset(&params, 0, sizeof(params));
6376 
6377 	if (!rdev->ops->add_station)
6378 		return -EOPNOTSUPP;
6379 
6380 	if (!info->attrs[NL80211_ATTR_MAC])
6381 		return -EINVAL;
6382 
6383 	if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6384 		return -EINVAL;
6385 
6386 	if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
6387 		return -EINVAL;
6388 
6389 	if (!info->attrs[NL80211_ATTR_STA_AID] &&
6390 	    !info->attrs[NL80211_ATTR_PEER_AID])
6391 		return -EINVAL;
6392 
6393 	mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6394 	params.supported_rates =
6395 		nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6396 	params.supported_rates_len =
6397 		nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
6398 	params.listen_interval =
6399 		nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6400 
6401 	if (info->attrs[NL80211_ATTR_VLAN_ID])
6402 		params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6403 
6404 	if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
6405 		params.support_p2p_ps =
6406 			nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6407 	} else {
6408 		/*
6409 		 * if not specified, assume it's supported for P2P GO interface,
6410 		 * and is NOT supported for AP interface
6411 		 */
6412 		params.support_p2p_ps =
6413 			dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
6414 	}
6415 
6416 	if (info->attrs[NL80211_ATTR_PEER_AID])
6417 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6418 	else
6419 		params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6420 
6421 	if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
6422 		params.capability =
6423 			nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
6424 		params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
6425 	}
6426 
6427 	if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
6428 		params.ext_capab =
6429 			nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6430 		params.ext_capab_len =
6431 			nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
6432 	}
6433 
6434 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6435 		params.ht_capa =
6436 			nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6437 
6438 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6439 		params.vht_capa =
6440 			nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6441 
6442 	if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6443 		params.he_capa =
6444 			nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6445 		params.he_capa_len =
6446 			nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6447 	}
6448 
6449 	if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
6450 		params.he_6ghz_capa =
6451 			nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
6452 
6453 	if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
6454 		params.opmode_notif_used = true;
6455 		params.opmode_notif =
6456 			nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
6457 	}
6458 
6459 	if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
6460 		params.plink_action =
6461 			nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
6462 
6463 	if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
6464 		params.airtime_weight =
6465 			nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
6466 
6467 	if (params.airtime_weight &&
6468 	    !wiphy_ext_feature_isset(&rdev->wiphy,
6469 				     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6470 		return -EOPNOTSUPP;
6471 
6472 	err = nl80211_parse_sta_txpower_setting(info, &params);
6473 	if (err)
6474 		return err;
6475 
6476 	err = nl80211_parse_sta_channel_info(info, &params);
6477 	if (err)
6478 		return err;
6479 
6480 	err = nl80211_parse_sta_wme(info, &params);
6481 	if (err)
6482 		return err;
6483 
6484 	if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
6485 		return -EINVAL;
6486 
6487 	/* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
6488 	 * as userspace might just pass through the capabilities from the IEs
6489 	 * directly, rather than enforcing this restriction and returning an
6490 	 * error in this case.
6491 	 */
6492 	if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
6493 		params.ht_capa = NULL;
6494 		params.vht_capa = NULL;
6495 
6496 		/* HE requires WME */
6497 		if (params.he_capa_len || params.he_6ghz_capa)
6498 			return -EINVAL;
6499 	}
6500 
6501 	/* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
6502 	if (params.he_6ghz_capa && (params.ht_capa || params.vht_capa))
6503 		return -EINVAL;
6504 
6505 	/* When you run into this, adjust the code below for the new flag */
6506 	BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6507 
6508 	switch (dev->ieee80211_ptr->iftype) {
6509 	case NL80211_IFTYPE_AP:
6510 	case NL80211_IFTYPE_AP_VLAN:
6511 	case NL80211_IFTYPE_P2P_GO:
6512 		/* ignore WME attributes if iface/sta is not capable */
6513 		if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
6514 		    !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
6515 			params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6516 
6517 		/* TDLS peers cannot be added */
6518 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6519 		    info->attrs[NL80211_ATTR_PEER_AID])
6520 			return -EINVAL;
6521 		/* but don't bother the driver with it */
6522 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6523 
6524 		/* allow authenticated/associated only if driver handles it */
6525 		if (!(rdev->wiphy.features &
6526 				NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6527 		    params.sta_flags_mask & auth_assoc)
6528 			return -EINVAL;
6529 
6530 		/* Older userspace, or userspace wanting to be compatible with
6531 		 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
6532 		 * and assoc flags in the mask, but assumes the station will be
6533 		 * added as associated anyway since this was the required driver
6534 		 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
6535 		 * introduced.
6536 		 * In order to not bother drivers with this quirk in the API
6537 		 * set the flags in both the mask and set for new stations in
6538 		 * this case.
6539 		 */
6540 		if (!(params.sta_flags_mask & auth_assoc)) {
6541 			params.sta_flags_mask |= auth_assoc;
6542 			params.sta_flags_set |= auth_assoc;
6543 		}
6544 
6545 		/* must be last in here for error handling */
6546 		params.vlan = get_vlan(info, rdev);
6547 		if (IS_ERR(params.vlan))
6548 			return PTR_ERR(params.vlan);
6549 		break;
6550 	case NL80211_IFTYPE_MESH_POINT:
6551 		/* ignore uAPSD data */
6552 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6553 
6554 		/* associated is disallowed */
6555 		if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6556 			return -EINVAL;
6557 		/* TDLS peers cannot be added */
6558 		if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6559 		    info->attrs[NL80211_ATTR_PEER_AID])
6560 			return -EINVAL;
6561 		break;
6562 	case NL80211_IFTYPE_STATION:
6563 	case NL80211_IFTYPE_P2P_CLIENT:
6564 		/* ignore uAPSD data */
6565 		params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6566 
6567 		/* these are disallowed */
6568 		if (params.sta_flags_mask &
6569 				(BIT(NL80211_STA_FLAG_ASSOCIATED) |
6570 				 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6571 			return -EINVAL;
6572 		/* Only TDLS peers can be added */
6573 		if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6574 			return -EINVAL;
6575 		/* Can only add if TDLS ... */
6576 		if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6577 			return -EOPNOTSUPP;
6578 		/* ... with external setup is supported */
6579 		if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6580 			return -EOPNOTSUPP;
6581 		/*
6582 		 * Older wpa_supplicant versions always mark the TDLS peer
6583 		 * as authorized, but it shouldn't yet be.
6584 		 */
6585 		params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6586 		break;
6587 	default:
6588 		return -EOPNOTSUPP;
6589 	}
6590 
6591 	/* be aware of params.vlan when changing code here */
6592 
6593 	err = rdev_add_station(rdev, dev, mac_addr, &params);
6594 
6595 	if (params.vlan)
6596 		dev_put(params.vlan);
6597 	return err;
6598 }
6599 
nl80211_del_station(struct sk_buff * skb,struct genl_info * info)6600 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6601 {
6602 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6603 	struct net_device *dev = info->user_ptr[1];
6604 	struct station_del_parameters params;
6605 
6606 	memset(&params, 0, sizeof(params));
6607 
6608 	if (info->attrs[NL80211_ATTR_MAC])
6609 		params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6610 
6611 	switch (dev->ieee80211_ptr->iftype) {
6612 	case NL80211_IFTYPE_AP:
6613 	case NL80211_IFTYPE_AP_VLAN:
6614 	case NL80211_IFTYPE_MESH_POINT:
6615 	case NL80211_IFTYPE_P2P_GO:
6616 		/* always accept these */
6617 		break;
6618 	case NL80211_IFTYPE_ADHOC:
6619 		/* conditionally accept */
6620 		if (wiphy_ext_feature_isset(&rdev->wiphy,
6621 					    NL80211_EXT_FEATURE_DEL_IBSS_STA))
6622 			break;
6623 		return -EINVAL;
6624 	default:
6625 		return -EINVAL;
6626 	}
6627 
6628 	if (!rdev->ops->del_station)
6629 		return -EOPNOTSUPP;
6630 
6631 	if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6632 		params.subtype =
6633 			nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6634 		if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6635 		    params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6636 			return -EINVAL;
6637 	} else {
6638 		/* Default to Deauthentication frame */
6639 		params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6640 	}
6641 
6642 	if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6643 		params.reason_code =
6644 			nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6645 		if (params.reason_code == 0)
6646 			return -EINVAL; /* 0 is reserved */
6647 	} else {
6648 		/* Default to reason code 2 */
6649 		params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6650 	}
6651 
6652 	return rdev_del_station(rdev, dev, &params);
6653 }
6654 
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)6655 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6656 				int flags, struct net_device *dev,
6657 				u8 *dst, u8 *next_hop,
6658 				struct mpath_info *pinfo)
6659 {
6660 	void *hdr;
6661 	struct nlattr *pinfoattr;
6662 
6663 	hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6664 	if (!hdr)
6665 		return -1;
6666 
6667 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6668 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6669 	    nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6670 	    nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6671 		goto nla_put_failure;
6672 
6673 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6674 	if (!pinfoattr)
6675 		goto nla_put_failure;
6676 	if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6677 	    nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6678 			pinfo->frame_qlen))
6679 		goto nla_put_failure;
6680 	if (((pinfo->filled & MPATH_INFO_SN) &&
6681 	     nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6682 	    ((pinfo->filled & MPATH_INFO_METRIC) &&
6683 	     nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6684 			 pinfo->metric)) ||
6685 	    ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6686 	     nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6687 			 pinfo->exptime)) ||
6688 	    ((pinfo->filled & MPATH_INFO_FLAGS) &&
6689 	     nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6690 			pinfo->flags)) ||
6691 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6692 	     nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6693 			 pinfo->discovery_timeout)) ||
6694 	    ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6695 	     nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6696 			pinfo->discovery_retries)) ||
6697 	    ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6698 	     nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6699 			pinfo->hop_count)) ||
6700 	    ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6701 	     nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6702 			 pinfo->path_change_count)))
6703 		goto nla_put_failure;
6704 
6705 	nla_nest_end(msg, pinfoattr);
6706 
6707 	genlmsg_end(msg, hdr);
6708 	return 0;
6709 
6710  nla_put_failure:
6711 	genlmsg_cancel(msg, hdr);
6712 	return -EMSGSIZE;
6713 }
6714 
nl80211_dump_mpath(struct sk_buff * skb,struct netlink_callback * cb)6715 static int nl80211_dump_mpath(struct sk_buff *skb,
6716 			      struct netlink_callback *cb)
6717 {
6718 	struct mpath_info pinfo;
6719 	struct cfg80211_registered_device *rdev;
6720 	struct wireless_dev *wdev;
6721 	u8 dst[ETH_ALEN];
6722 	u8 next_hop[ETH_ALEN];
6723 	int path_idx = cb->args[2];
6724 	int err;
6725 
6726 	rtnl_lock();
6727 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6728 	if (err)
6729 		goto out_err;
6730 
6731 	if (!rdev->ops->dump_mpath) {
6732 		err = -EOPNOTSUPP;
6733 		goto out_err;
6734 	}
6735 
6736 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6737 		err = -EOPNOTSUPP;
6738 		goto out_err;
6739 	}
6740 
6741 	while (1) {
6742 		err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6743 				      next_hop, &pinfo);
6744 		if (err == -ENOENT)
6745 			break;
6746 		if (err)
6747 			goto out_err;
6748 
6749 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6750 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6751 				       wdev->netdev, dst, next_hop,
6752 				       &pinfo) < 0)
6753 			goto out;
6754 
6755 		path_idx++;
6756 	}
6757 
6758  out:
6759 	cb->args[2] = path_idx;
6760 	err = skb->len;
6761  out_err:
6762 	rtnl_unlock();
6763 	return err;
6764 }
6765 
nl80211_get_mpath(struct sk_buff * skb,struct genl_info * info)6766 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6767 {
6768 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6769 	int err;
6770 	struct net_device *dev = info->user_ptr[1];
6771 	struct mpath_info pinfo;
6772 	struct sk_buff *msg;
6773 	u8 *dst = NULL;
6774 	u8 next_hop[ETH_ALEN];
6775 
6776 	memset(&pinfo, 0, sizeof(pinfo));
6777 
6778 	if (!info->attrs[NL80211_ATTR_MAC])
6779 		return -EINVAL;
6780 
6781 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6782 
6783 	if (!rdev->ops->get_mpath)
6784 		return -EOPNOTSUPP;
6785 
6786 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6787 		return -EOPNOTSUPP;
6788 
6789 	err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6790 	if (err)
6791 		return err;
6792 
6793 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6794 	if (!msg)
6795 		return -ENOMEM;
6796 
6797 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6798 				 dev, dst, next_hop, &pinfo) < 0) {
6799 		nlmsg_free(msg);
6800 		return -ENOBUFS;
6801 	}
6802 
6803 	return genlmsg_reply(msg, info);
6804 }
6805 
nl80211_set_mpath(struct sk_buff * skb,struct genl_info * info)6806 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6807 {
6808 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6809 	struct net_device *dev = info->user_ptr[1];
6810 	u8 *dst = NULL;
6811 	u8 *next_hop = NULL;
6812 
6813 	if (!info->attrs[NL80211_ATTR_MAC])
6814 		return -EINVAL;
6815 
6816 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6817 		return -EINVAL;
6818 
6819 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6820 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6821 
6822 	if (!rdev->ops->change_mpath)
6823 		return -EOPNOTSUPP;
6824 
6825 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6826 		return -EOPNOTSUPP;
6827 
6828 	return rdev_change_mpath(rdev, dev, dst, next_hop);
6829 }
6830 
nl80211_new_mpath(struct sk_buff * skb,struct genl_info * info)6831 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6832 {
6833 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6834 	struct net_device *dev = info->user_ptr[1];
6835 	u8 *dst = NULL;
6836 	u8 *next_hop = NULL;
6837 
6838 	if (!info->attrs[NL80211_ATTR_MAC])
6839 		return -EINVAL;
6840 
6841 	if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6842 		return -EINVAL;
6843 
6844 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6845 	next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6846 
6847 	if (!rdev->ops->add_mpath)
6848 		return -EOPNOTSUPP;
6849 
6850 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6851 		return -EOPNOTSUPP;
6852 
6853 	return rdev_add_mpath(rdev, dev, dst, next_hop);
6854 }
6855 
nl80211_del_mpath(struct sk_buff * skb,struct genl_info * info)6856 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6857 {
6858 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6859 	struct net_device *dev = info->user_ptr[1];
6860 	u8 *dst = NULL;
6861 
6862 	if (info->attrs[NL80211_ATTR_MAC])
6863 		dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6864 
6865 	if (!rdev->ops->del_mpath)
6866 		return -EOPNOTSUPP;
6867 
6868 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6869 		return -EOPNOTSUPP;
6870 
6871 	return rdev_del_mpath(rdev, dev, dst);
6872 }
6873 
nl80211_get_mpp(struct sk_buff * skb,struct genl_info * info)6874 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6875 {
6876 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6877 	int err;
6878 	struct net_device *dev = info->user_ptr[1];
6879 	struct mpath_info pinfo;
6880 	struct sk_buff *msg;
6881 	u8 *dst = NULL;
6882 	u8 mpp[ETH_ALEN];
6883 
6884 	memset(&pinfo, 0, sizeof(pinfo));
6885 
6886 	if (!info->attrs[NL80211_ATTR_MAC])
6887 		return -EINVAL;
6888 
6889 	dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6890 
6891 	if (!rdev->ops->get_mpp)
6892 		return -EOPNOTSUPP;
6893 
6894 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6895 		return -EOPNOTSUPP;
6896 
6897 	err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6898 	if (err)
6899 		return err;
6900 
6901 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6902 	if (!msg)
6903 		return -ENOMEM;
6904 
6905 	if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6906 			       dev, dst, mpp, &pinfo) < 0) {
6907 		nlmsg_free(msg);
6908 		return -ENOBUFS;
6909 	}
6910 
6911 	return genlmsg_reply(msg, info);
6912 }
6913 
nl80211_dump_mpp(struct sk_buff * skb,struct netlink_callback * cb)6914 static int nl80211_dump_mpp(struct sk_buff *skb,
6915 			    struct netlink_callback *cb)
6916 {
6917 	struct mpath_info pinfo;
6918 	struct cfg80211_registered_device *rdev;
6919 	struct wireless_dev *wdev;
6920 	u8 dst[ETH_ALEN];
6921 	u8 mpp[ETH_ALEN];
6922 	int path_idx = cb->args[2];
6923 	int err;
6924 
6925 	rtnl_lock();
6926 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6927 	if (err)
6928 		goto out_err;
6929 
6930 	if (!rdev->ops->dump_mpp) {
6931 		err = -EOPNOTSUPP;
6932 		goto out_err;
6933 	}
6934 
6935 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6936 		err = -EOPNOTSUPP;
6937 		goto out_err;
6938 	}
6939 
6940 	while (1) {
6941 		err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6942 				    mpp, &pinfo);
6943 		if (err == -ENOENT)
6944 			break;
6945 		if (err)
6946 			goto out_err;
6947 
6948 		if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6949 				       cb->nlh->nlmsg_seq, NLM_F_MULTI,
6950 				       wdev->netdev, dst, mpp,
6951 				       &pinfo) < 0)
6952 			goto out;
6953 
6954 		path_idx++;
6955 	}
6956 
6957  out:
6958 	cb->args[2] = path_idx;
6959 	err = skb->len;
6960  out_err:
6961 	rtnl_unlock();
6962 	return err;
6963 }
6964 
nl80211_set_bss(struct sk_buff * skb,struct genl_info * info)6965 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6966 {
6967 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
6968 	struct net_device *dev = info->user_ptr[1];
6969 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6970 	struct bss_parameters params;
6971 	int err;
6972 
6973 	memset(&params, 0, sizeof(params));
6974 	/* default to not changing parameters */
6975 	params.use_cts_prot = -1;
6976 	params.use_short_preamble = -1;
6977 	params.use_short_slot_time = -1;
6978 	params.ap_isolate = -1;
6979 	params.ht_opmode = -1;
6980 	params.p2p_ctwindow = -1;
6981 	params.p2p_opp_ps = -1;
6982 
6983 	if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6984 		params.use_cts_prot =
6985 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6986 	if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6987 		params.use_short_preamble =
6988 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6989 	if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6990 		params.use_short_slot_time =
6991 		    nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6992 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6993 		params.basic_rates =
6994 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6995 		params.basic_rates_len =
6996 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6997 	}
6998 	if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6999 		params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
7000 	if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
7001 		params.ht_opmode =
7002 			nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
7003 
7004 	if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
7005 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7006 			return -EINVAL;
7007 		params.p2p_ctwindow =
7008 			nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
7009 		if (params.p2p_ctwindow != 0 &&
7010 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
7011 			return -EINVAL;
7012 	}
7013 
7014 	if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
7015 		u8 tmp;
7016 
7017 		if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7018 			return -EINVAL;
7019 		tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
7020 		params.p2p_opp_ps = tmp;
7021 		if (params.p2p_opp_ps &&
7022 		    !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
7023 			return -EINVAL;
7024 	}
7025 
7026 	if (!rdev->ops->change_bss)
7027 		return -EOPNOTSUPP;
7028 
7029 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7030 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7031 		return -EOPNOTSUPP;
7032 
7033 	wdev_lock(wdev);
7034 	err = rdev_change_bss(rdev, dev, &params);
7035 	wdev_unlock(wdev);
7036 
7037 	return err;
7038 }
7039 
nl80211_req_set_reg(struct sk_buff * skb,struct genl_info * info)7040 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
7041 {
7042 	char *data = NULL;
7043 	bool is_indoor;
7044 	enum nl80211_user_reg_hint_type user_reg_hint_type;
7045 	u32 owner_nlportid;
7046 
7047 	/*
7048 	 * You should only get this when cfg80211 hasn't yet initialized
7049 	 * completely when built-in to the kernel right between the time
7050 	 * window between nl80211_init() and regulatory_init(), if that is
7051 	 * even possible.
7052 	 */
7053 	if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
7054 		return -EINPROGRESS;
7055 
7056 	if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
7057 		user_reg_hint_type =
7058 		  nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
7059 	else
7060 		user_reg_hint_type = NL80211_USER_REG_HINT_USER;
7061 
7062 	switch (user_reg_hint_type) {
7063 	case NL80211_USER_REG_HINT_USER:
7064 	case NL80211_USER_REG_HINT_CELL_BASE:
7065 		if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7066 			return -EINVAL;
7067 
7068 		data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7069 		return regulatory_hint_user(data, user_reg_hint_type);
7070 	case NL80211_USER_REG_HINT_INDOOR:
7071 		if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
7072 			owner_nlportid = info->snd_portid;
7073 			is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
7074 		} else {
7075 			owner_nlportid = 0;
7076 			is_indoor = true;
7077 		}
7078 
7079 		return regulatory_hint_indoor(is_indoor, owner_nlportid);
7080 	default:
7081 		return -EINVAL;
7082 	}
7083 }
7084 
nl80211_reload_regdb(struct sk_buff * skb,struct genl_info * info)7085 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
7086 {
7087 	return reg_reload_regdb();
7088 }
7089 
nl80211_get_mesh_config(struct sk_buff * skb,struct genl_info * info)7090 static int nl80211_get_mesh_config(struct sk_buff *skb,
7091 				   struct genl_info *info)
7092 {
7093 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7094 	struct net_device *dev = info->user_ptr[1];
7095 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7096 	struct mesh_config cur_params;
7097 	int err = 0;
7098 	void *hdr;
7099 	struct nlattr *pinfoattr;
7100 	struct sk_buff *msg;
7101 
7102 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7103 		return -EOPNOTSUPP;
7104 
7105 	if (!rdev->ops->get_mesh_config)
7106 		return -EOPNOTSUPP;
7107 
7108 	wdev_lock(wdev);
7109 	/* If not connected, get default parameters */
7110 	if (!wdev->mesh_id_len)
7111 		memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
7112 	else
7113 		err = rdev_get_mesh_config(rdev, dev, &cur_params);
7114 	wdev_unlock(wdev);
7115 
7116 	if (err)
7117 		return err;
7118 
7119 	/* Draw up a netlink message to send back */
7120 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7121 	if (!msg)
7122 		return -ENOMEM;
7123 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7124 			     NL80211_CMD_GET_MESH_CONFIG);
7125 	if (!hdr)
7126 		goto out;
7127 	pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
7128 	if (!pinfoattr)
7129 		goto nla_put_failure;
7130 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7131 	    nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
7132 			cur_params.dot11MeshRetryTimeout) ||
7133 	    nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
7134 			cur_params.dot11MeshConfirmTimeout) ||
7135 	    nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
7136 			cur_params.dot11MeshHoldingTimeout) ||
7137 	    nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7138 			cur_params.dot11MeshMaxPeerLinks) ||
7139 	    nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
7140 		       cur_params.dot11MeshMaxRetries) ||
7141 	    nla_put_u8(msg, NL80211_MESHCONF_TTL,
7142 		       cur_params.dot11MeshTTL) ||
7143 	    nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
7144 		       cur_params.element_ttl) ||
7145 	    nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7146 		       cur_params.auto_open_plinks) ||
7147 	    nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7148 			cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
7149 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7150 		       cur_params.dot11MeshHWMPmaxPREQretries) ||
7151 	    nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
7152 			cur_params.path_refresh_time) ||
7153 	    nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7154 			cur_params.min_discovery_timeout) ||
7155 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7156 			cur_params.dot11MeshHWMPactivePathTimeout) ||
7157 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7158 			cur_params.dot11MeshHWMPpreqMinInterval) ||
7159 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7160 			cur_params.dot11MeshHWMPperrMinInterval) ||
7161 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7162 			cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
7163 	    nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
7164 		       cur_params.dot11MeshHWMPRootMode) ||
7165 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7166 			cur_params.dot11MeshHWMPRannInterval) ||
7167 	    nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7168 		       cur_params.dot11MeshGateAnnouncementProtocol) ||
7169 	    nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
7170 		       cur_params.dot11MeshForwarding) ||
7171 	    nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
7172 			cur_params.rssi_threshold) ||
7173 	    nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
7174 			cur_params.ht_opmode) ||
7175 	    nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7176 			cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
7177 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7178 			cur_params.dot11MeshHWMProotInterval) ||
7179 	    nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7180 			cur_params.dot11MeshHWMPconfirmationInterval) ||
7181 	    nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
7182 			cur_params.power_mode) ||
7183 	    nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
7184 			cur_params.dot11MeshAwakeWindowDuration) ||
7185 	    nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
7186 			cur_params.plink_timeout) ||
7187 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
7188 		       cur_params.dot11MeshConnectedToMeshGate) ||
7189 	    nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
7190 		       cur_params.dot11MeshNolearn) ||
7191 	    nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
7192 		       cur_params.dot11MeshConnectedToAuthServer))
7193 		goto nla_put_failure;
7194 	nla_nest_end(msg, pinfoattr);
7195 	genlmsg_end(msg, hdr);
7196 	return genlmsg_reply(msg, info);
7197 
7198  nla_put_failure:
7199  out:
7200 	nlmsg_free(msg);
7201 	return -ENOBUFS;
7202 }
7203 
7204 static const struct nla_policy
7205 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
7206 	[NL80211_MESHCONF_RETRY_TIMEOUT] =
7207 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7208 	[NL80211_MESHCONF_CONFIRM_TIMEOUT] =
7209 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7210 	[NL80211_MESHCONF_HOLDING_TIMEOUT] =
7211 		NLA_POLICY_RANGE(NLA_U16, 1, 255),
7212 	[NL80211_MESHCONF_MAX_PEER_LINKS] =
7213 		NLA_POLICY_RANGE(NLA_U16, 0, 255),
7214 	[NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
7215 	[NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
7216 	[NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
7217 	[NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
7218 	[NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
7219 		NLA_POLICY_RANGE(NLA_U32, 1, 255),
7220 	[NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
7221 	[NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
7222 	[NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
7223 	[NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
7224 	[NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
7225 		NLA_POLICY_MIN(NLA_U16, 1),
7226 	[NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
7227 		NLA_POLICY_MIN(NLA_U16, 1),
7228 	[NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
7229 		NLA_POLICY_MIN(NLA_U16, 1),
7230 	[NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
7231 	[NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
7232 		NLA_POLICY_MIN(NLA_U16, 1),
7233 	[NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
7234 	[NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
7235 	[NL80211_MESHCONF_RSSI_THRESHOLD] =
7236 		NLA_POLICY_RANGE(NLA_S32, -255, 0),
7237 	[NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
7238 	[NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
7239 	[NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
7240 		NLA_POLICY_MIN(NLA_U16, 1),
7241 	[NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
7242 		NLA_POLICY_MIN(NLA_U16, 1),
7243 	[NL80211_MESHCONF_POWER_MODE] =
7244 		NLA_POLICY_RANGE(NLA_U32,
7245 				 NL80211_MESH_POWER_ACTIVE,
7246 				 NL80211_MESH_POWER_MAX),
7247 	[NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
7248 	[NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
7249 	[NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7250 	[NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7251 	[NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
7252 };
7253 
7254 static const struct nla_policy
7255 	nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
7256 	[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
7257 	[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
7258 	[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
7259 	[NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
7260 	[NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
7261 	[NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
7262 	[NL80211_MESH_SETUP_IE] =
7263 		NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
7264 				       IEEE80211_MAX_DATA_LEN),
7265 	[NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
7266 };
7267 
nl80211_parse_mesh_config(struct genl_info * info,struct mesh_config * cfg,u32 * mask_out)7268 static int nl80211_parse_mesh_config(struct genl_info *info,
7269 				     struct mesh_config *cfg,
7270 				     u32 *mask_out)
7271 {
7272 	struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
7273 	u32 mask = 0;
7274 	u16 ht_opmode;
7275 
7276 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)	\
7277 do {									\
7278 	if (tb[attr]) {							\
7279 		cfg->param = fn(tb[attr]);				\
7280 		mask |= BIT((attr) - 1);				\
7281 	}								\
7282 } while (0)
7283 
7284 	if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
7285 		return -EINVAL;
7286 	if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
7287 		return -EINVAL;
7288 
7289 	/* This makes sure that there aren't more than 32 mesh config
7290 	 * parameters (otherwise our bitfield scheme would not work.) */
7291 	BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
7292 
7293 	/* Fill in the params struct */
7294 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
7295 				  NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
7296 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
7297 				  NL80211_MESHCONF_CONFIRM_TIMEOUT,
7298 				  nla_get_u16);
7299 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
7300 				  NL80211_MESHCONF_HOLDING_TIMEOUT,
7301 				  nla_get_u16);
7302 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
7303 				  NL80211_MESHCONF_MAX_PEER_LINKS,
7304 				  nla_get_u16);
7305 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
7306 				  NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
7307 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
7308 				  NL80211_MESHCONF_TTL, nla_get_u8);
7309 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
7310 				  NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
7311 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
7312 				  NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7313 				  nla_get_u8);
7314 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
7315 				  mask,
7316 				  NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7317 				  nla_get_u32);
7318 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
7319 				  NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7320 				  nla_get_u8);
7321 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
7322 				  NL80211_MESHCONF_PATH_REFRESH_TIME,
7323 				  nla_get_u32);
7324 	if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
7325 	    (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
7326 		return -EINVAL;
7327 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
7328 				  NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7329 				  nla_get_u16);
7330 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
7331 				  mask,
7332 				  NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7333 				  nla_get_u32);
7334 	if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
7335 	    (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
7336 	     cfg->dot11MeshHWMPactivePathTimeout > 65535))
7337 		return -EINVAL;
7338 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
7339 				  NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7340 				  nla_get_u16);
7341 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
7342 				  NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7343 				  nla_get_u16);
7344 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7345 				  dot11MeshHWMPnetDiameterTraversalTime, mask,
7346 				  NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7347 				  nla_get_u16);
7348 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
7349 				  NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
7350 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
7351 				  NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7352 				  nla_get_u16);
7353 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
7354 				  mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7355 				  nla_get_u8);
7356 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
7357 				  NL80211_MESHCONF_FORWARDING, nla_get_u8);
7358 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
7359 				  NL80211_MESHCONF_RSSI_THRESHOLD,
7360 				  nla_get_s32);
7361 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
7362 				  NL80211_MESHCONF_CONNECTED_TO_GATE,
7363 				  nla_get_u8);
7364 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
7365 				  NL80211_MESHCONF_CONNECTED_TO_AS,
7366 				  nla_get_u8);
7367 	/*
7368 	 * Check HT operation mode based on
7369 	 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
7370 	 */
7371 	if (tb[NL80211_MESHCONF_HT_OPMODE]) {
7372 		ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
7373 
7374 		if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
7375 				  IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
7376 				  IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
7377 			return -EINVAL;
7378 
7379 		/* NON_HT_STA bit is reserved, but some programs set it */
7380 		ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
7381 
7382 		cfg->ht_opmode = ht_opmode;
7383 		mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
7384 	}
7385 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
7386 				  dot11MeshHWMPactivePathToRootTimeout, mask,
7387 				  NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7388 				  nla_get_u32);
7389 	if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
7390 	    (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
7391 	     cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
7392 		return -EINVAL;
7393 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
7394 				  NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7395 				  nla_get_u16);
7396 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
7397 				  mask,
7398 				  NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7399 				  nla_get_u16);
7400 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
7401 				  NL80211_MESHCONF_POWER_MODE, nla_get_u32);
7402 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
7403 				  NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
7404 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
7405 				  NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
7406 	FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
7407 				  NL80211_MESHCONF_NOLEARN, nla_get_u8);
7408 	if (mask_out)
7409 		*mask_out = mask;
7410 
7411 	return 0;
7412 
7413 #undef FILL_IN_MESH_PARAM_IF_SET
7414 }
7415 
nl80211_parse_mesh_setup(struct genl_info * info,struct mesh_setup * setup)7416 static int nl80211_parse_mesh_setup(struct genl_info *info,
7417 				     struct mesh_setup *setup)
7418 {
7419 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7420 	struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
7421 
7422 	if (!info->attrs[NL80211_ATTR_MESH_SETUP])
7423 		return -EINVAL;
7424 	if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
7425 		return -EINVAL;
7426 
7427 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
7428 		setup->sync_method =
7429 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
7430 		 IEEE80211_SYNC_METHOD_VENDOR :
7431 		 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
7432 
7433 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
7434 		setup->path_sel_proto =
7435 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
7436 		 IEEE80211_PATH_PROTOCOL_VENDOR :
7437 		 IEEE80211_PATH_PROTOCOL_HWMP;
7438 
7439 	if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
7440 		setup->path_metric =
7441 		(nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
7442 		 IEEE80211_PATH_METRIC_VENDOR :
7443 		 IEEE80211_PATH_METRIC_AIRTIME;
7444 
7445 	if (tb[NL80211_MESH_SETUP_IE]) {
7446 		struct nlattr *ieattr =
7447 			tb[NL80211_MESH_SETUP_IE];
7448 		setup->ie = nla_data(ieattr);
7449 		setup->ie_len = nla_len(ieattr);
7450 	}
7451 	if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
7452 	    !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
7453 		return -EINVAL;
7454 	setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
7455 	setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
7456 	setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
7457 	if (setup->is_secure)
7458 		setup->user_mpm = true;
7459 
7460 	if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
7461 		if (!setup->user_mpm)
7462 			return -EINVAL;
7463 		setup->auth_id =
7464 			nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
7465 	}
7466 
7467 	return 0;
7468 }
7469 
nl80211_update_mesh_config(struct sk_buff * skb,struct genl_info * info)7470 static int nl80211_update_mesh_config(struct sk_buff *skb,
7471 				      struct genl_info *info)
7472 {
7473 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
7474 	struct net_device *dev = info->user_ptr[1];
7475 	struct wireless_dev *wdev = dev->ieee80211_ptr;
7476 	struct mesh_config cfg = {};
7477 	u32 mask;
7478 	int err;
7479 
7480 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7481 		return -EOPNOTSUPP;
7482 
7483 	if (!rdev->ops->update_mesh_config)
7484 		return -EOPNOTSUPP;
7485 
7486 	err = nl80211_parse_mesh_config(info, &cfg, &mask);
7487 	if (err)
7488 		return err;
7489 
7490 	wdev_lock(wdev);
7491 	if (!wdev->mesh_id_len)
7492 		err = -ENOLINK;
7493 
7494 	if (!err)
7495 		err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
7496 
7497 	wdev_unlock(wdev);
7498 
7499 	return err;
7500 }
7501 
nl80211_put_regdom(const struct ieee80211_regdomain * regdom,struct sk_buff * msg)7502 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
7503 			      struct sk_buff *msg)
7504 {
7505 	struct nlattr *nl_reg_rules;
7506 	unsigned int i;
7507 
7508 	if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
7509 	    (regdom->dfs_region &&
7510 	     nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
7511 		goto nla_put_failure;
7512 
7513 	nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
7514 	if (!nl_reg_rules)
7515 		goto nla_put_failure;
7516 
7517 	for (i = 0; i < regdom->n_reg_rules; i++) {
7518 		struct nlattr *nl_reg_rule;
7519 		const struct ieee80211_reg_rule *reg_rule;
7520 		const struct ieee80211_freq_range *freq_range;
7521 		const struct ieee80211_power_rule *power_rule;
7522 		unsigned int max_bandwidth_khz;
7523 
7524 		reg_rule = &regdom->reg_rules[i];
7525 		freq_range = &reg_rule->freq_range;
7526 		power_rule = &reg_rule->power_rule;
7527 
7528 		nl_reg_rule = nla_nest_start_noflag(msg, i);
7529 		if (!nl_reg_rule)
7530 			goto nla_put_failure;
7531 
7532 		max_bandwidth_khz = freq_range->max_bandwidth_khz;
7533 		if (!max_bandwidth_khz)
7534 			max_bandwidth_khz = reg_get_max_bandwidth(regdom,
7535 								  reg_rule);
7536 
7537 		if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
7538 				reg_rule->flags) ||
7539 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
7540 				freq_range->start_freq_khz) ||
7541 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
7542 				freq_range->end_freq_khz) ||
7543 		    nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
7544 				max_bandwidth_khz) ||
7545 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
7546 				power_rule->max_antenna_gain) ||
7547 		    nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
7548 				power_rule->max_eirp) ||
7549 		    nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
7550 				reg_rule->dfs_cac_ms))
7551 			goto nla_put_failure;
7552 
7553 		nla_nest_end(msg, nl_reg_rule);
7554 	}
7555 
7556 	nla_nest_end(msg, nl_reg_rules);
7557 	return 0;
7558 
7559 nla_put_failure:
7560 	return -EMSGSIZE;
7561 }
7562 
nl80211_get_reg_do(struct sk_buff * skb,struct genl_info * info)7563 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
7564 {
7565 	const struct ieee80211_regdomain *regdom = NULL;
7566 	struct cfg80211_registered_device *rdev;
7567 	struct wiphy *wiphy = NULL;
7568 	struct sk_buff *msg;
7569 	void *hdr;
7570 
7571 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7572 	if (!msg)
7573 		return -ENOBUFS;
7574 
7575 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7576 			     NL80211_CMD_GET_REG);
7577 	if (!hdr)
7578 		goto put_failure;
7579 
7580 	if (info->attrs[NL80211_ATTR_WIPHY]) {
7581 		bool self_managed;
7582 
7583 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7584 		if (IS_ERR(rdev)) {
7585 			nlmsg_free(msg);
7586 			return PTR_ERR(rdev);
7587 		}
7588 
7589 		wiphy = &rdev->wiphy;
7590 		self_managed = wiphy->regulatory_flags &
7591 			       REGULATORY_WIPHY_SELF_MANAGED;
7592 		regdom = get_wiphy_regdom(wiphy);
7593 
7594 		/* a self-managed-reg device must have a private regdom */
7595 		if (WARN_ON(!regdom && self_managed)) {
7596 			nlmsg_free(msg);
7597 			return -EINVAL;
7598 		}
7599 
7600 		if (regdom &&
7601 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7602 			goto nla_put_failure;
7603 	}
7604 
7605 	if (!wiphy && reg_last_request_cell_base() &&
7606 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7607 			NL80211_USER_REG_HINT_CELL_BASE))
7608 		goto nla_put_failure;
7609 
7610 	rcu_read_lock();
7611 
7612 	if (!regdom)
7613 		regdom = rcu_dereference(cfg80211_regdomain);
7614 
7615 	if (nl80211_put_regdom(regdom, msg))
7616 		goto nla_put_failure_rcu;
7617 
7618 	rcu_read_unlock();
7619 
7620 	genlmsg_end(msg, hdr);
7621 	return genlmsg_reply(msg, info);
7622 
7623 nla_put_failure_rcu:
7624 	rcu_read_unlock();
7625 nla_put_failure:
7626 put_failure:
7627 	nlmsg_free(msg);
7628 	return -EMSGSIZE;
7629 }
7630 
nl80211_send_regdom(struct sk_buff * msg,struct netlink_callback * cb,u32 seq,int flags,struct wiphy * wiphy,const struct ieee80211_regdomain * regdom)7631 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7632 			       u32 seq, int flags, struct wiphy *wiphy,
7633 			       const struct ieee80211_regdomain *regdom)
7634 {
7635 	void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7636 				   NL80211_CMD_GET_REG);
7637 
7638 	if (!hdr)
7639 		return -1;
7640 
7641 	genl_dump_check_consistent(cb, hdr);
7642 
7643 	if (nl80211_put_regdom(regdom, msg))
7644 		goto nla_put_failure;
7645 
7646 	if (!wiphy && reg_last_request_cell_base() &&
7647 	    nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7648 			NL80211_USER_REG_HINT_CELL_BASE))
7649 		goto nla_put_failure;
7650 
7651 	if (wiphy &&
7652 	    nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7653 		goto nla_put_failure;
7654 
7655 	if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7656 	    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7657 		goto nla_put_failure;
7658 
7659 	genlmsg_end(msg, hdr);
7660 	return 0;
7661 
7662 nla_put_failure:
7663 	genlmsg_cancel(msg, hdr);
7664 	return -EMSGSIZE;
7665 }
7666 
nl80211_get_reg_dump(struct sk_buff * skb,struct netlink_callback * cb)7667 static int nl80211_get_reg_dump(struct sk_buff *skb,
7668 				struct netlink_callback *cb)
7669 {
7670 	const struct ieee80211_regdomain *regdom = NULL;
7671 	struct cfg80211_registered_device *rdev;
7672 	int err, reg_idx, start = cb->args[2];
7673 
7674 	rtnl_lock();
7675 
7676 	if (cfg80211_regdomain && start == 0) {
7677 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7678 					  NLM_F_MULTI, NULL,
7679 					  rtnl_dereference(cfg80211_regdomain));
7680 		if (err < 0)
7681 			goto out_err;
7682 	}
7683 
7684 	/* the global regdom is idx 0 */
7685 	reg_idx = 1;
7686 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7687 		regdom = get_wiphy_regdom(&rdev->wiphy);
7688 		if (!regdom)
7689 			continue;
7690 
7691 		if (++reg_idx <= start)
7692 			continue;
7693 
7694 		err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7695 					  NLM_F_MULTI, &rdev->wiphy, regdom);
7696 		if (err < 0) {
7697 			reg_idx--;
7698 			break;
7699 		}
7700 	}
7701 
7702 	cb->args[2] = reg_idx;
7703 	err = skb->len;
7704 out_err:
7705 	rtnl_unlock();
7706 	return err;
7707 }
7708 
7709 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7710 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7711 	[NL80211_ATTR_REG_RULE_FLAGS]		= { .type = NLA_U32 },
7712 	[NL80211_ATTR_FREQ_RANGE_START]		= { .type = NLA_U32 },
7713 	[NL80211_ATTR_FREQ_RANGE_END]		= { .type = NLA_U32 },
7714 	[NL80211_ATTR_FREQ_RANGE_MAX_BW]	= { .type = NLA_U32 },
7715 	[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]	= { .type = NLA_U32 },
7716 	[NL80211_ATTR_POWER_RULE_MAX_EIRP]	= { .type = NLA_U32 },
7717 	[NL80211_ATTR_DFS_CAC_TIME]		= { .type = NLA_U32 },
7718 };
7719 
parse_reg_rule(struct nlattr * tb[],struct ieee80211_reg_rule * reg_rule)7720 static int parse_reg_rule(struct nlattr *tb[],
7721 	struct ieee80211_reg_rule *reg_rule)
7722 {
7723 	struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7724 	struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7725 
7726 	if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7727 		return -EINVAL;
7728 	if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7729 		return -EINVAL;
7730 	if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7731 		return -EINVAL;
7732 	if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7733 		return -EINVAL;
7734 	if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7735 		return -EINVAL;
7736 
7737 	reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7738 
7739 	freq_range->start_freq_khz =
7740 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7741 	freq_range->end_freq_khz =
7742 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7743 	freq_range->max_bandwidth_khz =
7744 		nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7745 
7746 	power_rule->max_eirp =
7747 		nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7748 
7749 	if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7750 		power_rule->max_antenna_gain =
7751 			nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7752 
7753 	if (tb[NL80211_ATTR_DFS_CAC_TIME])
7754 		reg_rule->dfs_cac_ms =
7755 			nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7756 
7757 	return 0;
7758 }
7759 
nl80211_set_reg(struct sk_buff * skb,struct genl_info * info)7760 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7761 {
7762 	struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7763 	struct nlattr *nl_reg_rule;
7764 	char *alpha2;
7765 	int rem_reg_rules, r;
7766 	u32 num_rules = 0, rule_idx = 0;
7767 	enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7768 	struct ieee80211_regdomain *rd;
7769 
7770 	if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7771 		return -EINVAL;
7772 
7773 	if (!info->attrs[NL80211_ATTR_REG_RULES])
7774 		return -EINVAL;
7775 
7776 	alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7777 
7778 	if (info->attrs[NL80211_ATTR_DFS_REGION])
7779 		dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7780 
7781 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7782 			    rem_reg_rules) {
7783 		num_rules++;
7784 		if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7785 			return -EINVAL;
7786 	}
7787 
7788 	if (!reg_is_valid_request(alpha2))
7789 		return -EINVAL;
7790 
7791 	rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7792 	if (!rd)
7793 		return -ENOMEM;
7794 
7795 	rd->n_reg_rules = num_rules;
7796 	rd->alpha2[0] = alpha2[0];
7797 	rd->alpha2[1] = alpha2[1];
7798 
7799 	/*
7800 	 * Disable DFS master mode if the DFS region was
7801 	 * not supported or known on this kernel.
7802 	 */
7803 	if (reg_supported_dfs_region(dfs_region))
7804 		rd->dfs_region = dfs_region;
7805 
7806 	nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7807 			    rem_reg_rules) {
7808 		r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7809 						nl_reg_rule, reg_rule_policy,
7810 						info->extack);
7811 		if (r)
7812 			goto bad_reg;
7813 		r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7814 		if (r)
7815 			goto bad_reg;
7816 
7817 		rule_idx++;
7818 
7819 		if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7820 			r = -EINVAL;
7821 			goto bad_reg;
7822 		}
7823 	}
7824 
7825 	/* set_regdom takes ownership of rd */
7826 	return set_regdom(rd, REGD_SOURCE_CRDA);
7827  bad_reg:
7828 	kfree(rd);
7829 	return r;
7830 }
7831 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7832 
validate_scan_freqs(struct nlattr * freqs)7833 static int validate_scan_freqs(struct nlattr *freqs)
7834 {
7835 	struct nlattr *attr1, *attr2;
7836 	int n_channels = 0, tmp1, tmp2;
7837 
7838 	nla_for_each_nested(attr1, freqs, tmp1)
7839 		if (nla_len(attr1) != sizeof(u32))
7840 			return 0;
7841 
7842 	nla_for_each_nested(attr1, freqs, tmp1) {
7843 		n_channels++;
7844 		/*
7845 		 * Some hardware has a limited channel list for
7846 		 * scanning, and it is pretty much nonsensical
7847 		 * to scan for a channel twice, so disallow that
7848 		 * and don't require drivers to check that the
7849 		 * channel list they get isn't longer than what
7850 		 * they can scan, as long as they can scan all
7851 		 * the channels they registered at once.
7852 		 */
7853 		nla_for_each_nested(attr2, freqs, tmp2)
7854 			if (attr1 != attr2 &&
7855 			    nla_get_u32(attr1) == nla_get_u32(attr2))
7856 				return 0;
7857 	}
7858 
7859 	return n_channels;
7860 }
7861 
is_band_valid(struct wiphy * wiphy,enum nl80211_band b)7862 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7863 {
7864 	return b < NUM_NL80211_BANDS && wiphy->bands[b];
7865 }
7866 
parse_bss_select(struct nlattr * nla,struct wiphy * wiphy,struct cfg80211_bss_selection * bss_select)7867 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7868 			    struct cfg80211_bss_selection *bss_select)
7869 {
7870 	struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7871 	struct nlattr *nest;
7872 	int err;
7873 	bool found = false;
7874 	int i;
7875 
7876 	/* only process one nested attribute */
7877 	nest = nla_data(nla);
7878 	if (!nla_ok(nest, nla_len(nest)))
7879 		return -EINVAL;
7880 
7881 	err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7882 					  nest, nl80211_bss_select_policy,
7883 					  NULL);
7884 	if (err)
7885 		return err;
7886 
7887 	/* only one attribute may be given */
7888 	for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7889 		if (attr[i]) {
7890 			if (found)
7891 				return -EINVAL;
7892 			found = true;
7893 		}
7894 	}
7895 
7896 	bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7897 
7898 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7899 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7900 
7901 	if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7902 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7903 		bss_select->param.band_pref =
7904 			nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7905 		if (!is_band_valid(wiphy, bss_select->param.band_pref))
7906 			return -EINVAL;
7907 	}
7908 
7909 	if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7910 		struct nl80211_bss_select_rssi_adjust *adj_param;
7911 
7912 		adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7913 		bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7914 		bss_select->param.adjust.band = adj_param->band;
7915 		bss_select->param.adjust.delta = adj_param->delta;
7916 		if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7917 			return -EINVAL;
7918 	}
7919 
7920 	/* user-space did not provide behaviour attribute */
7921 	if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7922 		return -EINVAL;
7923 
7924 	if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7925 		return -EINVAL;
7926 
7927 	return 0;
7928 }
7929 
nl80211_parse_random_mac(struct nlattr ** attrs,u8 * mac_addr,u8 * mac_addr_mask)7930 int nl80211_parse_random_mac(struct nlattr **attrs,
7931 			     u8 *mac_addr, u8 *mac_addr_mask)
7932 {
7933 	int i;
7934 
7935 	if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7936 		eth_zero_addr(mac_addr);
7937 		eth_zero_addr(mac_addr_mask);
7938 		mac_addr[0] = 0x2;
7939 		mac_addr_mask[0] = 0x3;
7940 
7941 		return 0;
7942 	}
7943 
7944 	/* need both or none */
7945 	if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7946 		return -EINVAL;
7947 
7948 	memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7949 	memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7950 
7951 	/* don't allow or configure an mcast address */
7952 	if (!is_multicast_ether_addr(mac_addr_mask) ||
7953 	    is_multicast_ether_addr(mac_addr))
7954 		return -EINVAL;
7955 
7956 	/*
7957 	 * allow users to pass a MAC address that has bits set outside
7958 	 * of the mask, but don't bother drivers with having to deal
7959 	 * with such bits
7960 	 */
7961 	for (i = 0; i < ETH_ALEN; i++)
7962 		mac_addr[i] &= mac_addr_mask[i];
7963 
7964 	return 0;
7965 }
7966 
cfg80211_off_channel_oper_allowed(struct wireless_dev * wdev)7967 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7968 {
7969 	ASSERT_WDEV_LOCK(wdev);
7970 
7971 	if (!cfg80211_beaconing_iface_active(wdev))
7972 		return true;
7973 
7974 	if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7975 		return true;
7976 
7977 	return regulatory_pre_cac_allowed(wdev->wiphy);
7978 }
7979 
nl80211_check_scan_feat(struct wiphy * wiphy,u32 flags,u32 flag,enum nl80211_ext_feature_index feat)7980 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7981 				    enum nl80211_ext_feature_index feat)
7982 {
7983 	if (!(flags & flag))
7984 		return true;
7985 	if (wiphy_ext_feature_isset(wiphy, feat))
7986 		return true;
7987 	return false;
7988 }
7989 
7990 static int
nl80211_check_scan_flags(struct wiphy * wiphy,struct wireless_dev * wdev,void * request,struct nlattr ** attrs,bool is_sched_scan)7991 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7992 			 void *request, struct nlattr **attrs,
7993 			 bool is_sched_scan)
7994 {
7995 	u8 *mac_addr, *mac_addr_mask;
7996 	u32 *flags;
7997 	enum nl80211_feature_flags randomness_flag;
7998 
7999 	if (!attrs[NL80211_ATTR_SCAN_FLAGS])
8000 		return 0;
8001 
8002 	if (is_sched_scan) {
8003 		struct cfg80211_sched_scan_request *req = request;
8004 
8005 		randomness_flag = wdev ?
8006 				  NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
8007 				  NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
8008 		flags = &req->flags;
8009 		mac_addr = req->mac_addr;
8010 		mac_addr_mask = req->mac_addr_mask;
8011 	} else {
8012 		struct cfg80211_scan_request *req = request;
8013 
8014 		randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8015 		flags = &req->flags;
8016 		mac_addr = req->mac_addr;
8017 		mac_addr_mask = req->mac_addr_mask;
8018 	}
8019 
8020 	*flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
8021 
8022 	if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
8023 	     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
8024 	    !nl80211_check_scan_feat(wiphy, *flags,
8025 				     NL80211_SCAN_FLAG_LOW_SPAN,
8026 				     NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
8027 	    !nl80211_check_scan_feat(wiphy, *flags,
8028 				     NL80211_SCAN_FLAG_LOW_POWER,
8029 				     NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
8030 	    !nl80211_check_scan_feat(wiphy, *flags,
8031 				     NL80211_SCAN_FLAG_HIGH_ACCURACY,
8032 				     NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
8033 	    !nl80211_check_scan_feat(wiphy, *flags,
8034 				     NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
8035 				     NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
8036 	    !nl80211_check_scan_feat(wiphy, *flags,
8037 				     NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
8038 				     NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
8039 	    !nl80211_check_scan_feat(wiphy, *flags,
8040 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
8041 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
8042 	    !nl80211_check_scan_feat(wiphy, *flags,
8043 				     NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
8044 				     NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
8045 	    !nl80211_check_scan_feat(wiphy, *flags,
8046 				     NL80211_SCAN_FLAG_RANDOM_SN,
8047 				     NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
8048 	    !nl80211_check_scan_feat(wiphy, *flags,
8049 				     NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
8050 				     NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
8051 		return -EOPNOTSUPP;
8052 
8053 	if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
8054 		int err;
8055 
8056 		if (!(wiphy->features & randomness_flag) ||
8057 		    (wdev && wdev->current_bss))
8058 			return -EOPNOTSUPP;
8059 
8060 		err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
8061 		if (err)
8062 			return err;
8063 	}
8064 
8065 	return 0;
8066 }
8067 
nl80211_trigger_scan(struct sk_buff * skb,struct genl_info * info)8068 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
8069 {
8070 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8071 	struct wireless_dev *wdev = info->user_ptr[1];
8072 	struct cfg80211_scan_request *request;
8073 	struct nlattr *scan_freqs = NULL;
8074 	bool scan_freqs_khz = false;
8075 	struct nlattr *attr;
8076 	struct wiphy *wiphy;
8077 	int err, tmp, n_ssids = 0, n_channels, i;
8078 	size_t ie_len;
8079 
8080 	wiphy = &rdev->wiphy;
8081 
8082 	if (wdev->iftype == NL80211_IFTYPE_NAN)
8083 		return -EOPNOTSUPP;
8084 
8085 	if (!rdev->ops->scan)
8086 		return -EOPNOTSUPP;
8087 
8088 	if (rdev->scan_req || rdev->scan_msg)
8089 		return -EBUSY;
8090 
8091 	if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
8092 		if (!wiphy_ext_feature_isset(wiphy,
8093 					     NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
8094 			return -EOPNOTSUPP;
8095 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
8096 		scan_freqs_khz = true;
8097 	} else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
8098 		scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
8099 
8100 	if (scan_freqs) {
8101 		n_channels = validate_scan_freqs(scan_freqs);
8102 		if (!n_channels)
8103 			return -EINVAL;
8104 	} else {
8105 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8106 	}
8107 
8108 	if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
8109 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
8110 			n_ssids++;
8111 
8112 	if (n_ssids > wiphy->max_scan_ssids)
8113 		return -EINVAL;
8114 
8115 	if (info->attrs[NL80211_ATTR_IE])
8116 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8117 	else
8118 		ie_len = 0;
8119 
8120 	if (ie_len > wiphy->max_scan_ie_len)
8121 		return -EINVAL;
8122 
8123 	request = kzalloc(sizeof(*request)
8124 			+ sizeof(*request->ssids) * n_ssids
8125 			+ sizeof(*request->channels) * n_channels
8126 			+ ie_len, GFP_KERNEL);
8127 	if (!request)
8128 		return -ENOMEM;
8129 
8130 	if (n_ssids)
8131 		request->ssids = (void *)&request->channels[n_channels];
8132 	request->n_ssids = n_ssids;
8133 	if (ie_len) {
8134 		if (n_ssids)
8135 			request->ie = (void *)(request->ssids + n_ssids);
8136 		else
8137 			request->ie = (void *)(request->channels + n_channels);
8138 	}
8139 
8140 	i = 0;
8141 	if (scan_freqs) {
8142 		/* user specified, bail out if channel not found */
8143 		nla_for_each_nested(attr, scan_freqs, tmp) {
8144 			struct ieee80211_channel *chan;
8145 			int freq = nla_get_u32(attr);
8146 
8147 			if (!scan_freqs_khz)
8148 				freq = MHZ_TO_KHZ(freq);
8149 
8150 			chan = ieee80211_get_channel_khz(wiphy, freq);
8151 			if (!chan) {
8152 				err = -EINVAL;
8153 				goto out_free;
8154 			}
8155 
8156 			/* ignore disabled channels */
8157 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8158 				continue;
8159 
8160 			request->channels[i] = chan;
8161 			i++;
8162 		}
8163 	} else {
8164 		enum nl80211_band band;
8165 
8166 		/* all channels */
8167 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8168 			int j;
8169 
8170 			if (!wiphy->bands[band])
8171 				continue;
8172 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8173 				struct ieee80211_channel *chan;
8174 
8175 				chan = &wiphy->bands[band]->channels[j];
8176 
8177 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8178 					continue;
8179 
8180 				request->channels[i] = chan;
8181 				i++;
8182 			}
8183 		}
8184 	}
8185 
8186 	if (!i) {
8187 		err = -EINVAL;
8188 		goto out_free;
8189 	}
8190 
8191 	request->n_channels = i;
8192 
8193 	wdev_lock(wdev);
8194 	if (!cfg80211_off_channel_oper_allowed(wdev)) {
8195 		struct ieee80211_channel *chan;
8196 
8197 		if (request->n_channels != 1) {
8198 			wdev_unlock(wdev);
8199 			err = -EBUSY;
8200 			goto out_free;
8201 		}
8202 
8203 		chan = request->channels[0];
8204 		if (chan->center_freq != wdev->chandef.chan->center_freq) {
8205 			wdev_unlock(wdev);
8206 			err = -EBUSY;
8207 			goto out_free;
8208 		}
8209 	}
8210 	wdev_unlock(wdev);
8211 
8212 	i = 0;
8213 	if (n_ssids) {
8214 		nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
8215 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8216 				err = -EINVAL;
8217 				goto out_free;
8218 			}
8219 			request->ssids[i].ssid_len = nla_len(attr);
8220 			memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
8221 			i++;
8222 		}
8223 	}
8224 
8225 	if (info->attrs[NL80211_ATTR_IE]) {
8226 		request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8227 		memcpy((void *)request->ie,
8228 		       nla_data(info->attrs[NL80211_ATTR_IE]),
8229 		       request->ie_len);
8230 	}
8231 
8232 	for (i = 0; i < NUM_NL80211_BANDS; i++)
8233 		if (wiphy->bands[i])
8234 			request->rates[i] =
8235 				(1 << wiphy->bands[i]->n_bitrates) - 1;
8236 
8237 	if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
8238 		nla_for_each_nested(attr,
8239 				    info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
8240 				    tmp) {
8241 			enum nl80211_band band = nla_type(attr);
8242 
8243 			if (band < 0 || band >= NUM_NL80211_BANDS) {
8244 				err = -EINVAL;
8245 				goto out_free;
8246 			}
8247 
8248 			if (!wiphy->bands[band])
8249 				continue;
8250 
8251 			err = ieee80211_get_ratemask(wiphy->bands[band],
8252 						     nla_data(attr),
8253 						     nla_len(attr),
8254 						     &request->rates[band]);
8255 			if (err)
8256 				goto out_free;
8257 		}
8258 	}
8259 
8260 	if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
8261 		if (!wiphy_ext_feature_isset(wiphy,
8262 					NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
8263 			err = -EOPNOTSUPP;
8264 			goto out_free;
8265 		}
8266 
8267 		request->duration =
8268 			nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
8269 		request->duration_mandatory =
8270 			nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
8271 	}
8272 
8273 	err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
8274 				       false);
8275 	if (err)
8276 		goto out_free;
8277 
8278 	request->no_cck =
8279 		nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
8280 
8281 	/* Initial implementation used NL80211_ATTR_MAC to set the specific
8282 	 * BSSID to scan for. This was problematic because that same attribute
8283 	 * was already used for another purpose (local random MAC address). The
8284 	 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
8285 	 * compatibility with older userspace components, also use the
8286 	 * NL80211_ATTR_MAC value here if it can be determined to be used for
8287 	 * the specific BSSID use case instead of the random MAC address
8288 	 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
8289 	 */
8290 	if (info->attrs[NL80211_ATTR_BSSID])
8291 		memcpy(request->bssid,
8292 		       nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
8293 	else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
8294 		 info->attrs[NL80211_ATTR_MAC])
8295 		memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
8296 		       ETH_ALEN);
8297 	else
8298 		eth_broadcast_addr(request->bssid);
8299 
8300 	request->wdev = wdev;
8301 	request->wiphy = &rdev->wiphy;
8302 	request->scan_start = jiffies;
8303 
8304 	rdev->scan_req = request;
8305 	err = cfg80211_scan(rdev);
8306 
8307 	if (err)
8308 		goto out_free;
8309 
8310 	nl80211_send_scan_start(rdev, wdev);
8311 	if (wdev->netdev)
8312 		dev_hold(wdev->netdev);
8313 
8314 	return 0;
8315 
8316  out_free:
8317 	rdev->scan_req = NULL;
8318 	kfree(request);
8319 
8320 	return err;
8321 }
8322 
nl80211_abort_scan(struct sk_buff * skb,struct genl_info * info)8323 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
8324 {
8325 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8326 	struct wireless_dev *wdev = info->user_ptr[1];
8327 
8328 	if (!rdev->ops->abort_scan)
8329 		return -EOPNOTSUPP;
8330 
8331 	if (rdev->scan_msg)
8332 		return 0;
8333 
8334 	if (!rdev->scan_req)
8335 		return -ENOENT;
8336 
8337 	rdev_abort_scan(rdev, wdev);
8338 	return 0;
8339 }
8340 
8341 static int
nl80211_parse_sched_scan_plans(struct wiphy * wiphy,int n_plans,struct cfg80211_sched_scan_request * request,struct nlattr ** attrs)8342 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
8343 			       struct cfg80211_sched_scan_request *request,
8344 			       struct nlattr **attrs)
8345 {
8346 	int tmp, err, i = 0;
8347 	struct nlattr *attr;
8348 
8349 	if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8350 		u32 interval;
8351 
8352 		/*
8353 		 * If scan plans are not specified,
8354 		 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
8355 		 * case one scan plan will be set with the specified scan
8356 		 * interval and infinite number of iterations.
8357 		 */
8358 		interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
8359 		if (!interval)
8360 			return -EINVAL;
8361 
8362 		request->scan_plans[0].interval =
8363 			DIV_ROUND_UP(interval, MSEC_PER_SEC);
8364 		if (!request->scan_plans[0].interval)
8365 			return -EINVAL;
8366 
8367 		if (request->scan_plans[0].interval >
8368 		    wiphy->max_sched_scan_plan_interval)
8369 			request->scan_plans[0].interval =
8370 				wiphy->max_sched_scan_plan_interval;
8371 
8372 		return 0;
8373 	}
8374 
8375 	nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
8376 		struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
8377 
8378 		if (WARN_ON(i >= n_plans))
8379 			return -EINVAL;
8380 
8381 		err = nla_parse_nested_deprecated(plan,
8382 						  NL80211_SCHED_SCAN_PLAN_MAX,
8383 						  attr, nl80211_plan_policy,
8384 						  NULL);
8385 		if (err)
8386 			return err;
8387 
8388 		if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
8389 			return -EINVAL;
8390 
8391 		request->scan_plans[i].interval =
8392 			nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
8393 		if (!request->scan_plans[i].interval ||
8394 		    request->scan_plans[i].interval >
8395 		    wiphy->max_sched_scan_plan_interval)
8396 			return -EINVAL;
8397 
8398 		if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
8399 			request->scan_plans[i].iterations =
8400 				nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
8401 			if (!request->scan_plans[i].iterations ||
8402 			    (request->scan_plans[i].iterations >
8403 			     wiphy->max_sched_scan_plan_iterations))
8404 				return -EINVAL;
8405 		} else if (i < n_plans - 1) {
8406 			/*
8407 			 * All scan plans but the last one must specify
8408 			 * a finite number of iterations
8409 			 */
8410 			return -EINVAL;
8411 		}
8412 
8413 		i++;
8414 	}
8415 
8416 	/*
8417 	 * The last scan plan must not specify the number of
8418 	 * iterations, it is supposed to run infinitely
8419 	 */
8420 	if (request->scan_plans[n_plans - 1].iterations)
8421 		return  -EINVAL;
8422 
8423 	return 0;
8424 }
8425 
8426 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)8427 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
8428 				       struct cfg80211_match_set *match_sets,
8429 				       struct nlattr *tb_band_rssi,
8430 				       s32 rssi_thold)
8431 {
8432 	struct nlattr *attr;
8433 	int i, tmp, ret = 0;
8434 
8435 	if (!wiphy_ext_feature_isset(wiphy,
8436 		    NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
8437 		if (tb_band_rssi)
8438 			ret = -EOPNOTSUPP;
8439 		else
8440 			for (i = 0; i < NUM_NL80211_BANDS; i++)
8441 				match_sets->per_band_rssi_thold[i] =
8442 					NL80211_SCAN_RSSI_THOLD_OFF;
8443 		return ret;
8444 	}
8445 
8446 	for (i = 0; i < NUM_NL80211_BANDS; i++)
8447 		match_sets->per_band_rssi_thold[i] = rssi_thold;
8448 
8449 	nla_for_each_nested(attr, tb_band_rssi, tmp) {
8450 		enum nl80211_band band = nla_type(attr);
8451 
8452 		if (band < 0 || band >= NUM_NL80211_BANDS)
8453 			return -EINVAL;
8454 
8455 		match_sets->per_band_rssi_thold[band] =	nla_get_s32(attr);
8456 	}
8457 
8458 	return 0;
8459 }
8460 
8461 static struct cfg80211_sched_scan_request *
nl80211_parse_sched_scan(struct wiphy * wiphy,struct wireless_dev * wdev,struct nlattr ** attrs,int max_match_sets)8462 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
8463 			 struct nlattr **attrs, int max_match_sets)
8464 {
8465 	struct cfg80211_sched_scan_request *request;
8466 	struct nlattr *attr;
8467 	int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
8468 	enum nl80211_band band;
8469 	size_t ie_len;
8470 	struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
8471 	s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
8472 
8473 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8474 		n_channels = validate_scan_freqs(
8475 				attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
8476 		if (!n_channels)
8477 			return ERR_PTR(-EINVAL);
8478 	} else {
8479 		n_channels = ieee80211_get_num_supported_channels(wiphy);
8480 	}
8481 
8482 	if (attrs[NL80211_ATTR_SCAN_SSIDS])
8483 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8484 				    tmp)
8485 			n_ssids++;
8486 
8487 	if (n_ssids > wiphy->max_sched_scan_ssids)
8488 		return ERR_PTR(-EINVAL);
8489 
8490 	/*
8491 	 * First, count the number of 'real' matchsets. Due to an issue with
8492 	 * the old implementation, matchsets containing only the RSSI attribute
8493 	 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
8494 	 * RSSI for all matchsets, rather than their own matchset for reporting
8495 	 * all APs with a strong RSSI. This is needed to be compatible with
8496 	 * older userspace that treated a matchset with only the RSSI as the
8497 	 * global RSSI for all other matchsets - if there are other matchsets.
8498 	 */
8499 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8500 		nla_for_each_nested(attr,
8501 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8502 				    tmp) {
8503 			struct nlattr *rssi;
8504 
8505 			err = nla_parse_nested_deprecated(tb,
8506 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8507 							  attr,
8508 							  nl80211_match_policy,
8509 							  NULL);
8510 			if (err)
8511 				return ERR_PTR(err);
8512 
8513 			/* SSID and BSSID are mutually exclusive */
8514 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
8515 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
8516 				return ERR_PTR(-EINVAL);
8517 
8518 			/* add other standalone attributes here */
8519 			if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
8520 			    tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
8521 				n_match_sets++;
8522 				continue;
8523 			}
8524 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8525 			if (rssi)
8526 				default_match_rssi = nla_get_s32(rssi);
8527 		}
8528 	}
8529 
8530 	/* However, if there's no other matchset, add the RSSI one */
8531 	if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
8532 		n_match_sets = 1;
8533 
8534 	if (n_match_sets > max_match_sets)
8535 		return ERR_PTR(-EINVAL);
8536 
8537 	if (attrs[NL80211_ATTR_IE])
8538 		ie_len = nla_len(attrs[NL80211_ATTR_IE]);
8539 	else
8540 		ie_len = 0;
8541 
8542 	if (ie_len > wiphy->max_sched_scan_ie_len)
8543 		return ERR_PTR(-EINVAL);
8544 
8545 	if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
8546 		/*
8547 		 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
8548 		 * each scan plan already specifies its own interval
8549 		 */
8550 		if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8551 			return ERR_PTR(-EINVAL);
8552 
8553 		nla_for_each_nested(attr,
8554 				    attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
8555 			n_plans++;
8556 	} else {
8557 		/*
8558 		 * The scan interval attribute is kept for backward
8559 		 * compatibility. If no scan plans are specified and sched scan
8560 		 * interval is specified, one scan plan will be set with this
8561 		 * scan interval and infinite number of iterations.
8562 		 */
8563 		if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
8564 			return ERR_PTR(-EINVAL);
8565 
8566 		n_plans = 1;
8567 	}
8568 
8569 	if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
8570 		return ERR_PTR(-EINVAL);
8571 
8572 	if (!wiphy_ext_feature_isset(
8573 		    wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
8574 	    (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
8575 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8576 		return ERR_PTR(-EINVAL);
8577 
8578 	request = kzalloc(sizeof(*request)
8579 			+ sizeof(*request->ssids) * n_ssids
8580 			+ sizeof(*request->match_sets) * n_match_sets
8581 			+ sizeof(*request->scan_plans) * n_plans
8582 			+ sizeof(*request->channels) * n_channels
8583 			+ ie_len, GFP_KERNEL);
8584 	if (!request)
8585 		return ERR_PTR(-ENOMEM);
8586 
8587 	if (n_ssids)
8588 		request->ssids = (void *)&request->channels[n_channels];
8589 	request->n_ssids = n_ssids;
8590 	if (ie_len) {
8591 		if (n_ssids)
8592 			request->ie = (void *)(request->ssids + n_ssids);
8593 		else
8594 			request->ie = (void *)(request->channels + n_channels);
8595 	}
8596 
8597 	if (n_match_sets) {
8598 		if (request->ie)
8599 			request->match_sets = (void *)(request->ie + ie_len);
8600 		else if (n_ssids)
8601 			request->match_sets =
8602 				(void *)(request->ssids + n_ssids);
8603 		else
8604 			request->match_sets =
8605 				(void *)(request->channels + n_channels);
8606 	}
8607 	request->n_match_sets = n_match_sets;
8608 
8609 	if (n_match_sets)
8610 		request->scan_plans = (void *)(request->match_sets +
8611 					       n_match_sets);
8612 	else if (request->ie)
8613 		request->scan_plans = (void *)(request->ie + ie_len);
8614 	else if (n_ssids)
8615 		request->scan_plans = (void *)(request->ssids + n_ssids);
8616 	else
8617 		request->scan_plans = (void *)(request->channels + n_channels);
8618 
8619 	request->n_scan_plans = n_plans;
8620 
8621 	i = 0;
8622 	if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8623 		/* user specified, bail out if channel not found */
8624 		nla_for_each_nested(attr,
8625 				    attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8626 				    tmp) {
8627 			struct ieee80211_channel *chan;
8628 
8629 			chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8630 
8631 			if (!chan) {
8632 				err = -EINVAL;
8633 				goto out_free;
8634 			}
8635 
8636 			/* ignore disabled channels */
8637 			if (chan->flags & IEEE80211_CHAN_DISABLED)
8638 				continue;
8639 
8640 			request->channels[i] = chan;
8641 			i++;
8642 		}
8643 	} else {
8644 		/* all channels */
8645 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
8646 			int j;
8647 
8648 			if (!wiphy->bands[band])
8649 				continue;
8650 			for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8651 				struct ieee80211_channel *chan;
8652 
8653 				chan = &wiphy->bands[band]->channels[j];
8654 
8655 				if (chan->flags & IEEE80211_CHAN_DISABLED)
8656 					continue;
8657 
8658 				request->channels[i] = chan;
8659 				i++;
8660 			}
8661 		}
8662 	}
8663 
8664 	if (!i) {
8665 		err = -EINVAL;
8666 		goto out_free;
8667 	}
8668 
8669 	request->n_channels = i;
8670 
8671 	i = 0;
8672 	if (n_ssids) {
8673 		nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8674 				    tmp) {
8675 			if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8676 				err = -EINVAL;
8677 				goto out_free;
8678 			}
8679 			request->ssids[i].ssid_len = nla_len(attr);
8680 			memcpy(request->ssids[i].ssid, nla_data(attr),
8681 			       nla_len(attr));
8682 			i++;
8683 		}
8684 	}
8685 
8686 	i = 0;
8687 	if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8688 		nla_for_each_nested(attr,
8689 				    attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8690 				    tmp) {
8691 			struct nlattr *ssid, *bssid, *rssi;
8692 
8693 			err = nla_parse_nested_deprecated(tb,
8694 							  NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8695 							  attr,
8696 							  nl80211_match_policy,
8697 							  NULL);
8698 			if (err)
8699 				goto out_free;
8700 			ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8701 			bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8702 
8703 			if (!ssid && !bssid) {
8704 				i++;
8705 				continue;
8706 			}
8707 
8708 			if (WARN_ON(i >= n_match_sets)) {
8709 				/* this indicates a programming error,
8710 				 * the loop above should have verified
8711 				 * things properly
8712 				 */
8713 				err = -EINVAL;
8714 				goto out_free;
8715 			}
8716 
8717 			if (ssid) {
8718 				memcpy(request->match_sets[i].ssid.ssid,
8719 				       nla_data(ssid), nla_len(ssid));
8720 				request->match_sets[i].ssid.ssid_len =
8721 					nla_len(ssid);
8722 			}
8723 			if (bssid)
8724 				memcpy(request->match_sets[i].bssid,
8725 				       nla_data(bssid), ETH_ALEN);
8726 
8727 			/* special attribute - old implementation w/a */
8728 			request->match_sets[i].rssi_thold = default_match_rssi;
8729 			rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8730 			if (rssi)
8731 				request->match_sets[i].rssi_thold =
8732 					nla_get_s32(rssi);
8733 
8734 			/* Parse per band RSSI attribute */
8735 			err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8736 				&request->match_sets[i],
8737 				tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8738 				request->match_sets[i].rssi_thold);
8739 			if (err)
8740 				goto out_free;
8741 
8742 			i++;
8743 		}
8744 
8745 		/* there was no other matchset, so the RSSI one is alone */
8746 		if (i == 0 && n_match_sets)
8747 			request->match_sets[0].rssi_thold = default_match_rssi;
8748 
8749 		request->min_rssi_thold = INT_MAX;
8750 		for (i = 0; i < n_match_sets; i++)
8751 			request->min_rssi_thold =
8752 				min(request->match_sets[i].rssi_thold,
8753 				    request->min_rssi_thold);
8754 	} else {
8755 		request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8756 	}
8757 
8758 	if (ie_len) {
8759 		request->ie_len = ie_len;
8760 		memcpy((void *)request->ie,
8761 		       nla_data(attrs[NL80211_ATTR_IE]),
8762 		       request->ie_len);
8763 	}
8764 
8765 	err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8766 	if (err)
8767 		goto out_free;
8768 
8769 	if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8770 		request->delay =
8771 			nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8772 
8773 	if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8774 		request->relative_rssi = nla_get_s8(
8775 			attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8776 		request->relative_rssi_set = true;
8777 	}
8778 
8779 	if (request->relative_rssi_set &&
8780 	    attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8781 		struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8782 
8783 		rssi_adjust = nla_data(
8784 			attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8785 		request->rssi_adjust.band = rssi_adjust->band;
8786 		request->rssi_adjust.delta = rssi_adjust->delta;
8787 		if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8788 			err = -EINVAL;
8789 			goto out_free;
8790 		}
8791 	}
8792 
8793 	err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8794 	if (err)
8795 		goto out_free;
8796 
8797 	request->scan_start = jiffies;
8798 
8799 	return request;
8800 
8801 out_free:
8802 	kfree(request);
8803 	return ERR_PTR(err);
8804 }
8805 
nl80211_start_sched_scan(struct sk_buff * skb,struct genl_info * info)8806 static int nl80211_start_sched_scan(struct sk_buff *skb,
8807 				    struct genl_info *info)
8808 {
8809 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8810 	struct net_device *dev = info->user_ptr[1];
8811 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8812 	struct cfg80211_sched_scan_request *sched_scan_req;
8813 	bool want_multi;
8814 	int err;
8815 
8816 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8817 		return -EOPNOTSUPP;
8818 
8819 	want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8820 	err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8821 	if (err)
8822 		return err;
8823 
8824 	sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8825 						  info->attrs,
8826 						  rdev->wiphy.max_match_sets);
8827 
8828 	err = PTR_ERR_OR_ZERO(sched_scan_req);
8829 	if (err)
8830 		goto out_err;
8831 
8832 	/* leave request id zero for legacy request
8833 	 * or if driver does not support multi-scheduled scan
8834 	 */
8835 	if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
8836 		sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8837 
8838 	err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8839 	if (err)
8840 		goto out_free;
8841 
8842 	sched_scan_req->dev = dev;
8843 	sched_scan_req->wiphy = &rdev->wiphy;
8844 
8845 	if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8846 		sched_scan_req->owner_nlportid = info->snd_portid;
8847 
8848 	cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8849 
8850 	nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8851 	return 0;
8852 
8853 out_free:
8854 	kfree(sched_scan_req);
8855 out_err:
8856 	return err;
8857 }
8858 
nl80211_stop_sched_scan(struct sk_buff * skb,struct genl_info * info)8859 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8860 				   struct genl_info *info)
8861 {
8862 	struct cfg80211_sched_scan_request *req;
8863 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8864 	u64 cookie;
8865 
8866 	if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8867 		return -EOPNOTSUPP;
8868 
8869 	if (info->attrs[NL80211_ATTR_COOKIE]) {
8870 		cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8871 		return __cfg80211_stop_sched_scan(rdev, cookie, false);
8872 	}
8873 
8874 	req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8875 				     struct cfg80211_sched_scan_request,
8876 				     list);
8877 	if (!req || req->reqid ||
8878 	    (req->owner_nlportid &&
8879 	     req->owner_nlportid != info->snd_portid))
8880 		return -ENOENT;
8881 
8882 	return cfg80211_stop_sched_scan_req(rdev, req, false);
8883 }
8884 
nl80211_start_radar_detection(struct sk_buff * skb,struct genl_info * info)8885 static int nl80211_start_radar_detection(struct sk_buff *skb,
8886 					 struct genl_info *info)
8887 {
8888 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8889 	struct net_device *dev = info->user_ptr[1];
8890 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8891 	struct wiphy *wiphy = wdev->wiphy;
8892 	struct cfg80211_chan_def chandef;
8893 	enum nl80211_dfs_regions dfs_region;
8894 	unsigned int cac_time_ms;
8895 	int err;
8896 
8897 	dfs_region = reg_get_dfs_region(wiphy);
8898 	if (dfs_region == NL80211_DFS_UNSET)
8899 		return -EINVAL;
8900 
8901 	err = nl80211_parse_chandef(rdev, info, &chandef);
8902 	if (err)
8903 		return err;
8904 
8905 	if (netif_carrier_ok(dev))
8906 		return -EBUSY;
8907 
8908 	if (wdev->cac_started)
8909 		return -EBUSY;
8910 
8911 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8912 	if (err < 0)
8913 		return err;
8914 
8915 	if (err == 0)
8916 		return -EINVAL;
8917 
8918 	if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8919 		return -EINVAL;
8920 
8921 	/* CAC start is offloaded to HW and can't be started manually */
8922 	if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8923 		return -EOPNOTSUPP;
8924 
8925 	if (!rdev->ops->start_radar_detection)
8926 		return -EOPNOTSUPP;
8927 
8928 	cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8929 	if (WARN_ON(!cac_time_ms))
8930 		cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8931 
8932 	err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8933 	if (!err) {
8934 		wdev->chandef = chandef;
8935 		wdev->cac_started = true;
8936 		wdev->cac_start_time = jiffies;
8937 		wdev->cac_time_ms = cac_time_ms;
8938 	}
8939 	return err;
8940 }
8941 
nl80211_notify_radar_detection(struct sk_buff * skb,struct genl_info * info)8942 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8943 					  struct genl_info *info)
8944 {
8945 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8946 	struct net_device *dev = info->user_ptr[1];
8947 	struct wireless_dev *wdev = dev->ieee80211_ptr;
8948 	struct wiphy *wiphy = wdev->wiphy;
8949 	struct cfg80211_chan_def chandef;
8950 	enum nl80211_dfs_regions dfs_region;
8951 	int err;
8952 
8953 	dfs_region = reg_get_dfs_region(wiphy);
8954 	if (dfs_region == NL80211_DFS_UNSET) {
8955 		GENL_SET_ERR_MSG(info,
8956 				 "DFS Region is not set. Unexpected Radar indication");
8957 		return -EINVAL;
8958 	}
8959 
8960 	err = nl80211_parse_chandef(rdev, info, &chandef);
8961 	if (err) {
8962 		GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8963 		return err;
8964 	}
8965 
8966 	err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8967 	if (err < 0) {
8968 		GENL_SET_ERR_MSG(info, "chandef is invalid");
8969 		return err;
8970 	}
8971 
8972 	if (err == 0) {
8973 		GENL_SET_ERR_MSG(info,
8974 				 "Unexpected Radar indication for chandef/iftype");
8975 		return -EINVAL;
8976 	}
8977 
8978 	/* Do not process this notification if radar is already detected
8979 	 * by kernel on this channel, and return success.
8980 	 */
8981 	if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8982 		return 0;
8983 
8984 	cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8985 
8986 	cfg80211_sched_dfs_chan_update(rdev);
8987 
8988 	rdev->radar_chandef = chandef;
8989 
8990 	/* Propagate this notification to other radios as well */
8991 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8992 
8993 	return 0;
8994 }
8995 
nl80211_channel_switch(struct sk_buff * skb,struct genl_info * info)8996 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8997 {
8998 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
8999 	struct net_device *dev = info->user_ptr[1];
9000 	struct wireless_dev *wdev = dev->ieee80211_ptr;
9001 	struct cfg80211_csa_settings params;
9002 	/* csa_attrs is defined static to avoid waste of stack size - this
9003 	 * function is called under RTNL lock, so this should not be a problem.
9004 	 */
9005 	static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
9006 	int err;
9007 	bool need_new_beacon = false;
9008 	bool need_handle_dfs_flag = true;
9009 	int len, i;
9010 	u32 cs_count;
9011 
9012 	if (!rdev->ops->channel_switch ||
9013 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
9014 		return -EOPNOTSUPP;
9015 
9016 	switch (dev->ieee80211_ptr->iftype) {
9017 	case NL80211_IFTYPE_AP:
9018 	case NL80211_IFTYPE_P2P_GO:
9019 		need_new_beacon = true;
9020 		/* For all modes except AP the handle_dfs flag needs to be
9021 		 * supplied to tell the kernel that userspace will handle radar
9022 		 * events when they happen. Otherwise a switch to a channel
9023 		 * requiring DFS will be rejected.
9024 		 */
9025 		need_handle_dfs_flag = false;
9026 
9027 		/* useless if AP is not running */
9028 		if (!wdev->beacon_interval)
9029 			return -ENOTCONN;
9030 		break;
9031 	case NL80211_IFTYPE_ADHOC:
9032 		if (!wdev->ssid_len)
9033 			return -ENOTCONN;
9034 		break;
9035 	case NL80211_IFTYPE_MESH_POINT:
9036 		if (!wdev->mesh_id_len)
9037 			return -ENOTCONN;
9038 		break;
9039 	default:
9040 		return -EOPNOTSUPP;
9041 	}
9042 
9043 	memset(&params, 0, sizeof(params));
9044 	params.beacon_csa.ftm_responder = -1;
9045 
9046 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9047 	    !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
9048 		return -EINVAL;
9049 
9050 	/* only important for AP, IBSS and mesh create IEs internally */
9051 	if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
9052 		return -EINVAL;
9053 
9054 	/* Even though the attribute is u32, the specification says
9055 	 * u8, so let's make sure we don't overflow.
9056 	 */
9057 	cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
9058 	if (cs_count > 255)
9059 		return -EINVAL;
9060 
9061 	params.count = cs_count;
9062 
9063 	if (!need_new_beacon)
9064 		goto skip_beacons;
9065 
9066 	err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
9067 	if (err)
9068 		return err;
9069 
9070 	err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
9071 					  info->attrs[NL80211_ATTR_CSA_IES],
9072 					  nl80211_policy, info->extack);
9073 	if (err)
9074 		return err;
9075 
9076 	err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
9077 	if (err)
9078 		return err;
9079 
9080 	if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON])
9081 		return -EINVAL;
9082 
9083 	len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9084 	if (!len || (len % sizeof(u16)))
9085 		return -EINVAL;
9086 
9087 	params.n_counter_offsets_beacon = len / sizeof(u16);
9088 	if (rdev->wiphy.max_num_csa_counters &&
9089 	    (params.n_counter_offsets_beacon >
9090 	     rdev->wiphy.max_num_csa_counters))
9091 		return -EINVAL;
9092 
9093 	params.counter_offsets_beacon =
9094 		nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9095 
9096 	/* sanity checks - counters should fit and be the same */
9097 	for (i = 0; i < params.n_counter_offsets_beacon; i++) {
9098 		u16 offset = params.counter_offsets_beacon[i];
9099 
9100 		if (offset >= params.beacon_csa.tail_len)
9101 			return -EINVAL;
9102 
9103 		if (params.beacon_csa.tail[offset] != params.count)
9104 			return -EINVAL;
9105 	}
9106 
9107 	if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
9108 		len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9109 		if (!len || (len % sizeof(u16)))
9110 			return -EINVAL;
9111 
9112 		params.n_counter_offsets_presp = len / sizeof(u16);
9113 		if (rdev->wiphy.max_num_csa_counters &&
9114 		    (params.n_counter_offsets_presp >
9115 		     rdev->wiphy.max_num_csa_counters))
9116 			return -EINVAL;
9117 
9118 		params.counter_offsets_presp =
9119 			nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9120 
9121 		/* sanity checks - counters should fit and be the same */
9122 		for (i = 0; i < params.n_counter_offsets_presp; i++) {
9123 			u16 offset = params.counter_offsets_presp[i];
9124 
9125 			if (offset >= params.beacon_csa.probe_resp_len)
9126 				return -EINVAL;
9127 
9128 			if (params.beacon_csa.probe_resp[offset] !=
9129 			    params.count)
9130 				return -EINVAL;
9131 		}
9132 	}
9133 
9134 skip_beacons:
9135 	err = nl80211_parse_chandef(rdev, info, &params.chandef);
9136 	if (err)
9137 		return err;
9138 
9139 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
9140 					   wdev->iftype))
9141 		return -EINVAL;
9142 
9143 	err = cfg80211_chandef_dfs_required(wdev->wiphy,
9144 					    &params.chandef,
9145 					    wdev->iftype);
9146 	if (err < 0)
9147 		return err;
9148 
9149 	if (err > 0) {
9150 		params.radar_required = true;
9151 		if (need_handle_dfs_flag &&
9152 		    !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
9153 			return -EINVAL;
9154 		}
9155 	}
9156 
9157 	if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
9158 		params.block_tx = true;
9159 
9160 	wdev_lock(wdev);
9161 	err = rdev_channel_switch(rdev, dev, &params);
9162 	wdev_unlock(wdev);
9163 
9164 	return err;
9165 }
9166 
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)9167 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
9168 			    u32 seq, int flags,
9169 			    struct cfg80211_registered_device *rdev,
9170 			    struct wireless_dev *wdev,
9171 			    struct cfg80211_internal_bss *intbss)
9172 {
9173 	struct cfg80211_bss *res = &intbss->pub;
9174 	const struct cfg80211_bss_ies *ies;
9175 	void *hdr;
9176 	struct nlattr *bss;
9177 
9178 	ASSERT_WDEV_LOCK(wdev);
9179 
9180 	hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
9181 			     NL80211_CMD_NEW_SCAN_RESULTS);
9182 	if (!hdr)
9183 		return -1;
9184 
9185 	genl_dump_check_consistent(cb, hdr);
9186 
9187 	if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
9188 		goto nla_put_failure;
9189 	if (wdev->netdev &&
9190 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
9191 		goto nla_put_failure;
9192 	if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
9193 			      NL80211_ATTR_PAD))
9194 		goto nla_put_failure;
9195 
9196 	bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
9197 	if (!bss)
9198 		goto nla_put_failure;
9199 	if ((!is_zero_ether_addr(res->bssid) &&
9200 	     nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
9201 		goto nla_put_failure;
9202 
9203 	rcu_read_lock();
9204 	/* indicate whether we have probe response data or not */
9205 	if (rcu_access_pointer(res->proberesp_ies) &&
9206 	    nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
9207 		goto fail_unlock_rcu;
9208 
9209 	/* this pointer prefers to be pointed to probe response data
9210 	 * but is always valid
9211 	 */
9212 	ies = rcu_dereference(res->ies);
9213 	if (ies) {
9214 		if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
9215 				      NL80211_BSS_PAD))
9216 			goto fail_unlock_rcu;
9217 		if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
9218 					ies->len, ies->data))
9219 			goto fail_unlock_rcu;
9220 	}
9221 
9222 	/* and this pointer is always (unless driver didn't know) beacon data */
9223 	ies = rcu_dereference(res->beacon_ies);
9224 	if (ies && ies->from_beacon) {
9225 		if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
9226 				      NL80211_BSS_PAD))
9227 			goto fail_unlock_rcu;
9228 		if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
9229 					ies->len, ies->data))
9230 			goto fail_unlock_rcu;
9231 	}
9232 	rcu_read_unlock();
9233 
9234 	if (res->beacon_interval &&
9235 	    nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
9236 		goto nla_put_failure;
9237 	if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
9238 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
9239 	    nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
9240 			res->channel->freq_offset) ||
9241 	    nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
9242 	    nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
9243 			jiffies_to_msecs(jiffies - intbss->ts)))
9244 		goto nla_put_failure;
9245 
9246 	if (intbss->parent_tsf &&
9247 	    (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
9248 			       intbss->parent_tsf, NL80211_BSS_PAD) ||
9249 	     nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
9250 		     intbss->parent_bssid)))
9251 		goto nla_put_failure;
9252 
9253 	if (intbss->ts_boottime &&
9254 	    nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
9255 			      intbss->ts_boottime, NL80211_BSS_PAD))
9256 		goto nla_put_failure;
9257 
9258 	if (!nl80211_put_signal(msg, intbss->pub.chains,
9259 				intbss->pub.chain_signal,
9260 				NL80211_BSS_CHAIN_SIGNAL))
9261 		goto nla_put_failure;
9262 
9263 	switch (rdev->wiphy.signal_type) {
9264 	case CFG80211_SIGNAL_TYPE_MBM:
9265 		if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
9266 			goto nla_put_failure;
9267 		break;
9268 	case CFG80211_SIGNAL_TYPE_UNSPEC:
9269 		if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
9270 			goto nla_put_failure;
9271 		break;
9272 	default:
9273 		break;
9274 	}
9275 
9276 	switch (wdev->iftype) {
9277 	case NL80211_IFTYPE_P2P_CLIENT:
9278 	case NL80211_IFTYPE_STATION:
9279 		if (intbss == wdev->current_bss &&
9280 		    nla_put_u32(msg, NL80211_BSS_STATUS,
9281 				NL80211_BSS_STATUS_ASSOCIATED))
9282 			goto nla_put_failure;
9283 		break;
9284 	case NL80211_IFTYPE_ADHOC:
9285 		if (intbss == wdev->current_bss &&
9286 		    nla_put_u32(msg, NL80211_BSS_STATUS,
9287 				NL80211_BSS_STATUS_IBSS_JOINED))
9288 			goto nla_put_failure;
9289 		break;
9290 	default:
9291 		break;
9292 	}
9293 
9294 	nla_nest_end(msg, bss);
9295 
9296 	genlmsg_end(msg, hdr);
9297 	return 0;
9298 
9299  fail_unlock_rcu:
9300 	rcu_read_unlock();
9301  nla_put_failure:
9302 	genlmsg_cancel(msg, hdr);
9303 	return -EMSGSIZE;
9304 }
9305 
nl80211_dump_scan(struct sk_buff * skb,struct netlink_callback * cb)9306 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
9307 {
9308 	struct cfg80211_registered_device *rdev;
9309 	struct cfg80211_internal_bss *scan;
9310 	struct wireless_dev *wdev;
9311 	int start = cb->args[2], idx = 0;
9312 	int err;
9313 
9314 	rtnl_lock();
9315 	err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9316 	if (err) {
9317 		rtnl_unlock();
9318 		return err;
9319 	}
9320 
9321 	wdev_lock(wdev);
9322 	spin_lock_bh(&rdev->bss_lock);
9323 
9324 	/*
9325 	 * dump_scan will be called multiple times to break up the scan results
9326 	 * into multiple messages.  It is unlikely that any more bss-es will be
9327 	 * expired after the first call, so only call only call this on the
9328 	 * first dump_scan invocation.
9329 	 */
9330 	if (start == 0)
9331 		cfg80211_bss_expire(rdev);
9332 
9333 	cb->seq = rdev->bss_generation;
9334 
9335 	list_for_each_entry(scan, &rdev->bss_list, list) {
9336 		if (++idx <= start)
9337 			continue;
9338 		if (nl80211_send_bss(skb, cb,
9339 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9340 				rdev, wdev, scan) < 0) {
9341 			idx--;
9342 			break;
9343 		}
9344 	}
9345 
9346 	spin_unlock_bh(&rdev->bss_lock);
9347 	wdev_unlock(wdev);
9348 
9349 	cb->args[2] = idx;
9350 	rtnl_unlock();
9351 
9352 	return skb->len;
9353 }
9354 
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)9355 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
9356 			       int flags, struct net_device *dev,
9357 			       bool allow_radio_stats,
9358 			       struct survey_info *survey)
9359 {
9360 	void *hdr;
9361 	struct nlattr *infoattr;
9362 
9363 	/* skip radio stats if userspace didn't request them */
9364 	if (!survey->channel && !allow_radio_stats)
9365 		return 0;
9366 
9367 	hdr = nl80211hdr_put(msg, portid, seq, flags,
9368 			     NL80211_CMD_NEW_SURVEY_RESULTS);
9369 	if (!hdr)
9370 		return -ENOMEM;
9371 
9372 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
9373 		goto nla_put_failure;
9374 
9375 	infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
9376 	if (!infoattr)
9377 		goto nla_put_failure;
9378 
9379 	if (survey->channel &&
9380 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
9381 			survey->channel->center_freq))
9382 		goto nla_put_failure;
9383 
9384 	if (survey->channel && survey->channel->freq_offset &&
9385 	    nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
9386 			survey->channel->freq_offset))
9387 		goto nla_put_failure;
9388 
9389 	if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
9390 	    nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
9391 		goto nla_put_failure;
9392 	if ((survey->filled & SURVEY_INFO_IN_USE) &&
9393 	    nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
9394 		goto nla_put_failure;
9395 	if ((survey->filled & SURVEY_INFO_TIME) &&
9396 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
9397 			survey->time, NL80211_SURVEY_INFO_PAD))
9398 		goto nla_put_failure;
9399 	if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
9400 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
9401 			      survey->time_busy, NL80211_SURVEY_INFO_PAD))
9402 		goto nla_put_failure;
9403 	if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
9404 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
9405 			      survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
9406 		goto nla_put_failure;
9407 	if ((survey->filled & SURVEY_INFO_TIME_RX) &&
9408 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
9409 			      survey->time_rx, NL80211_SURVEY_INFO_PAD))
9410 		goto nla_put_failure;
9411 	if ((survey->filled & SURVEY_INFO_TIME_TX) &&
9412 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
9413 			      survey->time_tx, NL80211_SURVEY_INFO_PAD))
9414 		goto nla_put_failure;
9415 	if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
9416 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
9417 			      survey->time_scan, NL80211_SURVEY_INFO_PAD))
9418 		goto nla_put_failure;
9419 	if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
9420 	    nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
9421 			      survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
9422 		goto nla_put_failure;
9423 
9424 	nla_nest_end(msg, infoattr);
9425 
9426 	genlmsg_end(msg, hdr);
9427 	return 0;
9428 
9429  nla_put_failure:
9430 	genlmsg_cancel(msg, hdr);
9431 	return -EMSGSIZE;
9432 }
9433 
nl80211_dump_survey(struct sk_buff * skb,struct netlink_callback * cb)9434 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
9435 {
9436 	struct nlattr **attrbuf;
9437 	struct survey_info survey;
9438 	struct cfg80211_registered_device *rdev;
9439 	struct wireless_dev *wdev;
9440 	int survey_idx = cb->args[2];
9441 	int res;
9442 	bool radio_stats;
9443 
9444 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
9445 	if (!attrbuf)
9446 		return -ENOMEM;
9447 
9448 	rtnl_lock();
9449 	res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
9450 	if (res)
9451 		goto out_err;
9452 
9453 	/* prepare_wdev_dump parsed the attributes */
9454 	radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
9455 
9456 	if (!wdev->netdev) {
9457 		res = -EINVAL;
9458 		goto out_err;
9459 	}
9460 
9461 	if (!rdev->ops->dump_survey) {
9462 		res = -EOPNOTSUPP;
9463 		goto out_err;
9464 	}
9465 
9466 	while (1) {
9467 		res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
9468 		if (res == -ENOENT)
9469 			break;
9470 		if (res)
9471 			goto out_err;
9472 
9473 		/* don't send disabled channels, but do send non-channel data */
9474 		if (survey.channel &&
9475 		    survey.channel->flags & IEEE80211_CHAN_DISABLED) {
9476 			survey_idx++;
9477 			continue;
9478 		}
9479 
9480 		if (nl80211_send_survey(skb,
9481 				NETLINK_CB(cb->skb).portid,
9482 				cb->nlh->nlmsg_seq, NLM_F_MULTI,
9483 				wdev->netdev, radio_stats, &survey) < 0)
9484 			goto out;
9485 		survey_idx++;
9486 	}
9487 
9488  out:
9489 	cb->args[2] = survey_idx;
9490 	res = skb->len;
9491  out_err:
9492 	kfree(attrbuf);
9493 	rtnl_unlock();
9494 	return res;
9495 }
9496 
nl80211_valid_wpa_versions(u32 wpa_versions)9497 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
9498 {
9499 	return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
9500 				  NL80211_WPA_VERSION_2 |
9501 				  NL80211_WPA_VERSION_3));
9502 }
9503 
nl80211_authenticate(struct sk_buff * skb,struct genl_info * info)9504 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
9505 {
9506 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9507 	struct net_device *dev = info->user_ptr[1];
9508 	struct ieee80211_channel *chan;
9509 	const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
9510 	int err, ssid_len, ie_len = 0, auth_data_len = 0;
9511 	enum nl80211_auth_type auth_type;
9512 	struct key_parse key;
9513 	bool local_state_change;
9514 	u32 freq;
9515 
9516 	if (!info->attrs[NL80211_ATTR_MAC])
9517 		return -EINVAL;
9518 
9519 	if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
9520 		return -EINVAL;
9521 
9522 	if (!info->attrs[NL80211_ATTR_SSID])
9523 		return -EINVAL;
9524 
9525 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9526 		return -EINVAL;
9527 
9528 	err = nl80211_parse_key(info, &key);
9529 	if (err)
9530 		return err;
9531 
9532 	if (key.idx >= 0) {
9533 		if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
9534 			return -EINVAL;
9535 		if (!key.p.key || !key.p.key_len)
9536 			return -EINVAL;
9537 		if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
9538 		     key.p.key_len != WLAN_KEY_LEN_WEP40) &&
9539 		    (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
9540 		     key.p.key_len != WLAN_KEY_LEN_WEP104))
9541 			return -EINVAL;
9542 		if (key.idx > 3)
9543 			return -EINVAL;
9544 	} else {
9545 		key.p.key_len = 0;
9546 		key.p.key = NULL;
9547 	}
9548 
9549 	if (key.idx >= 0) {
9550 		int i;
9551 		bool ok = false;
9552 
9553 		for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
9554 			if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
9555 				ok = true;
9556 				break;
9557 			}
9558 		}
9559 		if (!ok)
9560 			return -EINVAL;
9561 	}
9562 
9563 	if (!rdev->ops->auth)
9564 		return -EOPNOTSUPP;
9565 
9566 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9567 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9568 		return -EOPNOTSUPP;
9569 
9570 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9571 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9572 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
9573 		freq +=
9574 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
9575 
9576 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
9577 	if (!chan)
9578 		return -EINVAL;
9579 
9580 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9581 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9582 
9583 	if (info->attrs[NL80211_ATTR_IE]) {
9584 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9585 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9586 	}
9587 
9588 	auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9589 	if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9590 		return -EINVAL;
9591 
9592 	if ((auth_type == NL80211_AUTHTYPE_SAE ||
9593 	     auth_type == NL80211_AUTHTYPE_FILS_SK ||
9594 	     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9595 	     auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9596 	    !info->attrs[NL80211_ATTR_AUTH_DATA])
9597 		return -EINVAL;
9598 
9599 	if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9600 		if (auth_type != NL80211_AUTHTYPE_SAE &&
9601 		    auth_type != NL80211_AUTHTYPE_FILS_SK &&
9602 		    auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9603 		    auth_type != NL80211_AUTHTYPE_FILS_PK)
9604 			return -EINVAL;
9605 		auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9606 		auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9607 	}
9608 
9609 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9610 
9611 	/*
9612 	 * Since we no longer track auth state, ignore
9613 	 * requests to only change local state.
9614 	 */
9615 	if (local_state_change)
9616 		return 0;
9617 
9618 	wdev_lock(dev->ieee80211_ptr);
9619 	err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9620 				 ssid, ssid_len, ie, ie_len,
9621 				 key.p.key, key.p.key_len, key.idx,
9622 				 auth_data, auth_data_len);
9623 	wdev_unlock(dev->ieee80211_ptr);
9624 	return err;
9625 }
9626 
validate_pae_over_nl80211(struct cfg80211_registered_device * rdev,struct genl_info * info)9627 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9628 				     struct genl_info *info)
9629 {
9630 	if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9631 		GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9632 		return -EINVAL;
9633 	}
9634 
9635 	if (!rdev->ops->tx_control_port ||
9636 	    !wiphy_ext_feature_isset(&rdev->wiphy,
9637 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9638 		return -EOPNOTSUPP;
9639 
9640 	return 0;
9641 }
9642 
nl80211_crypto_settings(struct cfg80211_registered_device * rdev,struct genl_info * info,struct cfg80211_crypto_settings * settings,int cipher_limit)9643 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9644 				   struct genl_info *info,
9645 				   struct cfg80211_crypto_settings *settings,
9646 				   int cipher_limit)
9647 {
9648 	memset(settings, 0, sizeof(*settings));
9649 
9650 	settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9651 
9652 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9653 		u16 proto;
9654 
9655 		proto = nla_get_u16(
9656 			info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9657 		settings->control_port_ethertype = cpu_to_be16(proto);
9658 		if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9659 		    proto != ETH_P_PAE)
9660 			return -EINVAL;
9661 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9662 			settings->control_port_no_encrypt = true;
9663 	} else
9664 		settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9665 
9666 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9667 		int r = validate_pae_over_nl80211(rdev, info);
9668 
9669 		if (r < 0)
9670 			return r;
9671 
9672 		settings->control_port_over_nl80211 = true;
9673 
9674 		if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
9675 			settings->control_port_no_preauth = true;
9676 	}
9677 
9678 	if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9679 		void *data;
9680 		int len, i;
9681 
9682 		data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9683 		len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9684 		settings->n_ciphers_pairwise = len / sizeof(u32);
9685 
9686 		if (len % sizeof(u32))
9687 			return -EINVAL;
9688 
9689 		if (settings->n_ciphers_pairwise > cipher_limit)
9690 			return -EINVAL;
9691 
9692 		memcpy(settings->ciphers_pairwise, data, len);
9693 
9694 		for (i = 0; i < settings->n_ciphers_pairwise; i++)
9695 			if (!cfg80211_supported_cipher_suite(
9696 					&rdev->wiphy,
9697 					settings->ciphers_pairwise[i]))
9698 				return -EINVAL;
9699 	}
9700 
9701 	if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9702 		settings->cipher_group =
9703 			nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9704 		if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9705 						     settings->cipher_group))
9706 			return -EINVAL;
9707 	}
9708 
9709 	if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9710 		settings->wpa_versions =
9711 			nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9712 		if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9713 			return -EINVAL;
9714 	}
9715 
9716 	if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9717 		void *data;
9718 		int len;
9719 
9720 		data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9721 		len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9722 		settings->n_akm_suites = len / sizeof(u32);
9723 
9724 		if (len % sizeof(u32))
9725 			return -EINVAL;
9726 
9727 		if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9728 			return -EINVAL;
9729 
9730 		memcpy(settings->akm_suites, data, len);
9731 	}
9732 
9733 	if (info->attrs[NL80211_ATTR_PMK]) {
9734 		if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9735 			return -EINVAL;
9736 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9737 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
9738 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9739 					     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
9740 			return -EINVAL;
9741 		settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9742 	}
9743 
9744 	if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9745 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
9746 					     NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
9747 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9748 					     NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
9749 			return -EINVAL;
9750 		settings->sae_pwd =
9751 			nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9752 		settings->sae_pwd_len =
9753 			nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9754 	}
9755 
9756 	if (info->attrs[NL80211_ATTR_SAE_PWE])
9757 		settings->sae_pwe =
9758 			nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
9759 	else
9760 		settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
9761 
9762 	return 0;
9763 }
9764 
nl80211_associate(struct sk_buff * skb,struct genl_info * info)9765 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9766 {
9767 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9768 	struct net_device *dev = info->user_ptr[1];
9769 	struct ieee80211_channel *chan;
9770 	struct cfg80211_assoc_request req = {};
9771 	const u8 *bssid, *ssid;
9772 	int err, ssid_len = 0;
9773 	u32 freq;
9774 
9775 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9776 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9777 		return -EPERM;
9778 
9779 	if (!info->attrs[NL80211_ATTR_MAC] ||
9780 	    !info->attrs[NL80211_ATTR_SSID] ||
9781 	    !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9782 		return -EINVAL;
9783 
9784 	if (!rdev->ops->assoc)
9785 		return -EOPNOTSUPP;
9786 
9787 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9788 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9789 		return -EOPNOTSUPP;
9790 
9791 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9792 
9793 	freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
9794 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
9795 		freq +=
9796 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
9797 	chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
9798 	if (!chan)
9799 		return -EINVAL;
9800 
9801 	ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9802 	ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9803 
9804 	if (info->attrs[NL80211_ATTR_IE]) {
9805 		req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9806 		req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9807 	}
9808 
9809 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
9810 		enum nl80211_mfp mfp =
9811 			nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9812 		if (mfp == NL80211_MFP_REQUIRED)
9813 			req.use_mfp = true;
9814 		else if (mfp != NL80211_MFP_NO)
9815 			return -EINVAL;
9816 	}
9817 
9818 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
9819 		req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9820 
9821 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9822 		req.flags |= ASSOC_REQ_DISABLE_HT;
9823 
9824 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9825 		memcpy(&req.ht_capa_mask,
9826 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9827 		       sizeof(req.ht_capa_mask));
9828 
9829 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9830 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9831 			return -EINVAL;
9832 		memcpy(&req.ht_capa,
9833 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9834 		       sizeof(req.ht_capa));
9835 	}
9836 
9837 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9838 		req.flags |= ASSOC_REQ_DISABLE_VHT;
9839 
9840 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9841 		memcpy(&req.vht_capa_mask,
9842 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9843 		       sizeof(req.vht_capa_mask));
9844 
9845 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9846 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9847 			return -EINVAL;
9848 		memcpy(&req.vht_capa,
9849 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9850 		       sizeof(req.vht_capa));
9851 	}
9852 
9853 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9854 		if (!((rdev->wiphy.features &
9855 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9856 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9857 		    !wiphy_ext_feature_isset(&rdev->wiphy,
9858 					     NL80211_EXT_FEATURE_RRM))
9859 			return -EINVAL;
9860 		req.flags |= ASSOC_REQ_USE_RRM;
9861 	}
9862 
9863 	if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9864 		req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9865 		req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9866 		if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9867 			return -EINVAL;
9868 		req.fils_nonces =
9869 			nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9870 	}
9871 
9872 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
9873 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
9874 			return -EINVAL;
9875 		memcpy(&req.s1g_capa_mask,
9876 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
9877 		       sizeof(req.s1g_capa_mask));
9878 	}
9879 
9880 	if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
9881 		if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
9882 			return -EINVAL;
9883 		memcpy(&req.s1g_capa,
9884 		       nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
9885 		       sizeof(req.s1g_capa));
9886 	}
9887 
9888 	err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9889 	if (!err) {
9890 		wdev_lock(dev->ieee80211_ptr);
9891 
9892 		err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9893 					  ssid, ssid_len, &req);
9894 
9895 		if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9896 			dev->ieee80211_ptr->conn_owner_nlportid =
9897 				info->snd_portid;
9898 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
9899 			       bssid, ETH_ALEN);
9900 		}
9901 
9902 		wdev_unlock(dev->ieee80211_ptr);
9903 	}
9904 
9905 	return err;
9906 }
9907 
nl80211_deauthenticate(struct sk_buff * skb,struct genl_info * info)9908 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9909 {
9910 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9911 	struct net_device *dev = info->user_ptr[1];
9912 	const u8 *ie = NULL, *bssid;
9913 	int ie_len = 0, err;
9914 	u16 reason_code;
9915 	bool local_state_change;
9916 
9917 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9918 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9919 		return -EPERM;
9920 
9921 	if (!info->attrs[NL80211_ATTR_MAC])
9922 		return -EINVAL;
9923 
9924 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9925 		return -EINVAL;
9926 
9927 	if (!rdev->ops->deauth)
9928 		return -EOPNOTSUPP;
9929 
9930 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9931 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9932 		return -EOPNOTSUPP;
9933 
9934 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9935 
9936 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9937 	if (reason_code == 0) {
9938 		/* Reason Code 0 is reserved */
9939 		return -EINVAL;
9940 	}
9941 
9942 	if (info->attrs[NL80211_ATTR_IE]) {
9943 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9944 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9945 	}
9946 
9947 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9948 
9949 	wdev_lock(dev->ieee80211_ptr);
9950 	err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9951 				   local_state_change);
9952 	wdev_unlock(dev->ieee80211_ptr);
9953 	return err;
9954 }
9955 
nl80211_disassociate(struct sk_buff * skb,struct genl_info * info)9956 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9957 {
9958 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
9959 	struct net_device *dev = info->user_ptr[1];
9960 	const u8 *ie = NULL, *bssid;
9961 	int ie_len = 0, err;
9962 	u16 reason_code;
9963 	bool local_state_change;
9964 
9965 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
9966 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9967 		return -EPERM;
9968 
9969 	if (!info->attrs[NL80211_ATTR_MAC])
9970 		return -EINVAL;
9971 
9972 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
9973 		return -EINVAL;
9974 
9975 	if (!rdev->ops->disassoc)
9976 		return -EOPNOTSUPP;
9977 
9978 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9979 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9980 		return -EOPNOTSUPP;
9981 
9982 	bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9983 
9984 	reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9985 	if (reason_code == 0) {
9986 		/* Reason Code 0 is reserved */
9987 		return -EINVAL;
9988 	}
9989 
9990 	if (info->attrs[NL80211_ATTR_IE]) {
9991 		ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9992 		ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9993 	}
9994 
9995 	local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9996 
9997 	wdev_lock(dev->ieee80211_ptr);
9998 	err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9999 				     local_state_change);
10000 	wdev_unlock(dev->ieee80211_ptr);
10001 	return err;
10002 }
10003 
10004 static bool
nl80211_parse_mcast_rate(struct cfg80211_registered_device * rdev,int mcast_rate[NUM_NL80211_BANDS],int rateval)10005 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
10006 			 int mcast_rate[NUM_NL80211_BANDS],
10007 			 int rateval)
10008 {
10009 	struct wiphy *wiphy = &rdev->wiphy;
10010 	bool found = false;
10011 	int band, i;
10012 
10013 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
10014 		struct ieee80211_supported_band *sband;
10015 
10016 		sband = wiphy->bands[band];
10017 		if (!sband)
10018 			continue;
10019 
10020 		for (i = 0; i < sband->n_bitrates; i++) {
10021 			if (sband->bitrates[i].bitrate == rateval) {
10022 				mcast_rate[band] = i + 1;
10023 				found = true;
10024 				break;
10025 			}
10026 		}
10027 	}
10028 
10029 	return found;
10030 }
10031 
nl80211_join_ibss(struct sk_buff * skb,struct genl_info * info)10032 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
10033 {
10034 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10035 	struct net_device *dev = info->user_ptr[1];
10036 	struct cfg80211_ibss_params ibss;
10037 	struct wiphy *wiphy;
10038 	struct cfg80211_cached_keys *connkeys = NULL;
10039 	int err;
10040 
10041 	memset(&ibss, 0, sizeof(ibss));
10042 
10043 	if (!info->attrs[NL80211_ATTR_SSID] ||
10044 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
10045 		return -EINVAL;
10046 
10047 	ibss.beacon_interval = 100;
10048 
10049 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
10050 		ibss.beacon_interval =
10051 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10052 
10053 	err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
10054 					   ibss.beacon_interval);
10055 	if (err)
10056 		return err;
10057 
10058 	if (!rdev->ops->join_ibss)
10059 		return -EOPNOTSUPP;
10060 
10061 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
10062 		return -EOPNOTSUPP;
10063 
10064 	wiphy = &rdev->wiphy;
10065 
10066 	if (info->attrs[NL80211_ATTR_MAC]) {
10067 		ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10068 
10069 		if (!is_valid_ether_addr(ibss.bssid))
10070 			return -EINVAL;
10071 	}
10072 	ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10073 	ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10074 
10075 	if (info->attrs[NL80211_ATTR_IE]) {
10076 		ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10077 		ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10078 	}
10079 
10080 	err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
10081 	if (err)
10082 		return err;
10083 
10084 	if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
10085 				     NL80211_IFTYPE_ADHOC))
10086 		return -EINVAL;
10087 
10088 	switch (ibss.chandef.width) {
10089 	case NL80211_CHAN_WIDTH_5:
10090 	case NL80211_CHAN_WIDTH_10:
10091 	case NL80211_CHAN_WIDTH_20_NOHT:
10092 		break;
10093 	case NL80211_CHAN_WIDTH_20:
10094 	case NL80211_CHAN_WIDTH_40:
10095 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
10096 			return -EINVAL;
10097 		break;
10098 	case NL80211_CHAN_WIDTH_80:
10099 	case NL80211_CHAN_WIDTH_80P80:
10100 	case NL80211_CHAN_WIDTH_160:
10101 		if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
10102 			return -EINVAL;
10103 		if (!wiphy_ext_feature_isset(&rdev->wiphy,
10104 					     NL80211_EXT_FEATURE_VHT_IBSS))
10105 			return -EINVAL;
10106 		break;
10107 	default:
10108 		return -EINVAL;
10109 	}
10110 
10111 	ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
10112 	ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
10113 
10114 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10115 		u8 *rates =
10116 			nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10117 		int n_rates =
10118 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10119 		struct ieee80211_supported_band *sband =
10120 			wiphy->bands[ibss.chandef.chan->band];
10121 
10122 		err = ieee80211_get_ratemask(sband, rates, n_rates,
10123 					     &ibss.basic_rates);
10124 		if (err)
10125 			return err;
10126 	}
10127 
10128 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10129 		memcpy(&ibss.ht_capa_mask,
10130 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10131 		       sizeof(ibss.ht_capa_mask));
10132 
10133 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10134 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10135 			return -EINVAL;
10136 		memcpy(&ibss.ht_capa,
10137 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10138 		       sizeof(ibss.ht_capa));
10139 	}
10140 
10141 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10142 	    !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
10143 			nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10144 		return -EINVAL;
10145 
10146 	if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
10147 		bool no_ht = false;
10148 
10149 		connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
10150 		if (IS_ERR(connkeys))
10151 			return PTR_ERR(connkeys);
10152 
10153 		if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
10154 		    no_ht) {
10155 			kfree_sensitive(connkeys);
10156 			return -EINVAL;
10157 		}
10158 	}
10159 
10160 	ibss.control_port =
10161 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
10162 
10163 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10164 		int r = validate_pae_over_nl80211(rdev, info);
10165 
10166 		if (r < 0) {
10167 			kfree_sensitive(connkeys);
10168 			return r;
10169 		}
10170 
10171 		ibss.control_port_over_nl80211 = true;
10172 	}
10173 
10174 	ibss.userspace_handles_dfs =
10175 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10176 
10177 	wdev_lock(dev->ieee80211_ptr);
10178 	err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
10179 	if (err)
10180 		kfree_sensitive(connkeys);
10181 	else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
10182 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10183 	wdev_unlock(dev->ieee80211_ptr);
10184 
10185 	return err;
10186 }
10187 
nl80211_leave_ibss(struct sk_buff * skb,struct genl_info * info)10188 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
10189 {
10190 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10191 	struct net_device *dev = info->user_ptr[1];
10192 
10193 	if (!rdev->ops->leave_ibss)
10194 		return -EOPNOTSUPP;
10195 
10196 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
10197 		return -EOPNOTSUPP;
10198 
10199 	return cfg80211_leave_ibss(rdev, dev, false);
10200 }
10201 
nl80211_set_mcast_rate(struct sk_buff * skb,struct genl_info * info)10202 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
10203 {
10204 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10205 	struct net_device *dev = info->user_ptr[1];
10206 	int mcast_rate[NUM_NL80211_BANDS];
10207 	u32 nla_rate;
10208 	int err;
10209 
10210 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
10211 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
10212 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
10213 		return -EOPNOTSUPP;
10214 
10215 	if (!rdev->ops->set_mcast_rate)
10216 		return -EOPNOTSUPP;
10217 
10218 	memset(mcast_rate, 0, sizeof(mcast_rate));
10219 
10220 	if (!info->attrs[NL80211_ATTR_MCAST_RATE])
10221 		return -EINVAL;
10222 
10223 	nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
10224 	if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
10225 		return -EINVAL;
10226 
10227 	err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
10228 
10229 	return err;
10230 }
10231 
10232 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)10233 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
10234 			    struct wireless_dev *wdev, int approxlen,
10235 			    u32 portid, u32 seq, enum nl80211_commands cmd,
10236 			    enum nl80211_attrs attr,
10237 			    const struct nl80211_vendor_cmd_info *info,
10238 			    gfp_t gfp)
10239 {
10240 	struct sk_buff *skb;
10241 	void *hdr;
10242 	struct nlattr *data;
10243 
10244 	skb = nlmsg_new(approxlen + 100, gfp);
10245 	if (!skb)
10246 		return NULL;
10247 
10248 	hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
10249 	if (!hdr) {
10250 		kfree_skb(skb);
10251 		return NULL;
10252 	}
10253 
10254 	if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10255 		goto nla_put_failure;
10256 
10257 	if (info) {
10258 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
10259 				info->vendor_id))
10260 			goto nla_put_failure;
10261 		if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
10262 				info->subcmd))
10263 			goto nla_put_failure;
10264 	}
10265 
10266 	if (wdev) {
10267 		if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
10268 				      wdev_id(wdev), NL80211_ATTR_PAD))
10269 			goto nla_put_failure;
10270 		if (wdev->netdev &&
10271 		    nla_put_u32(skb, NL80211_ATTR_IFINDEX,
10272 				wdev->netdev->ifindex))
10273 			goto nla_put_failure;
10274 	}
10275 
10276 	data = nla_nest_start_noflag(skb, attr);
10277 	if (!data)
10278 		goto nla_put_failure;
10279 
10280 	((void **)skb->cb)[0] = rdev;
10281 	((void **)skb->cb)[1] = hdr;
10282 	((void **)skb->cb)[2] = data;
10283 
10284 	return skb;
10285 
10286  nla_put_failure:
10287 	kfree_skb(skb);
10288 	return NULL;
10289 }
10290 
__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)10291 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
10292 					   struct wireless_dev *wdev,
10293 					   enum nl80211_commands cmd,
10294 					   enum nl80211_attrs attr,
10295 					   unsigned int portid,
10296 					   int vendor_event_idx,
10297 					   int approxlen, gfp_t gfp)
10298 {
10299 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10300 	const struct nl80211_vendor_cmd_info *info;
10301 
10302 	switch (cmd) {
10303 	case NL80211_CMD_TESTMODE:
10304 		if (WARN_ON(vendor_event_idx != -1))
10305 			return NULL;
10306 		info = NULL;
10307 		break;
10308 	case NL80211_CMD_VENDOR:
10309 		if (WARN_ON(vendor_event_idx < 0 ||
10310 			    vendor_event_idx >= wiphy->n_vendor_events))
10311 			return NULL;
10312 		info = &wiphy->vendor_events[vendor_event_idx];
10313 		break;
10314 	default:
10315 		WARN_ON(1);
10316 		return NULL;
10317 	}
10318 
10319 	return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
10320 					   cmd, attr, info, gfp);
10321 }
10322 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
10323 
__cfg80211_send_event_skb(struct sk_buff * skb,gfp_t gfp)10324 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
10325 {
10326 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
10327 	void *hdr = ((void **)skb->cb)[1];
10328 	struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
10329 	struct nlattr *data = ((void **)skb->cb)[2];
10330 	enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
10331 
10332 	/* clear CB data for netlink core to own from now on */
10333 	memset(skb->cb, 0, sizeof(skb->cb));
10334 
10335 	nla_nest_end(skb, data);
10336 	genlmsg_end(skb, hdr);
10337 
10338 	if (nlhdr->nlmsg_pid) {
10339 		genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
10340 				nlhdr->nlmsg_pid);
10341 	} else {
10342 		if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
10343 			mcgrp = NL80211_MCGRP_VENDOR;
10344 
10345 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
10346 					skb, 0, mcgrp, gfp);
10347 	}
10348 }
10349 EXPORT_SYMBOL(__cfg80211_send_event_skb);
10350 
10351 #ifdef CONFIG_NL80211_TESTMODE
nl80211_testmode_do(struct sk_buff * skb,struct genl_info * info)10352 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
10353 {
10354 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10355 	struct wireless_dev *wdev =
10356 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
10357 	int err;
10358 
10359 	if (!rdev->ops->testmode_cmd)
10360 		return -EOPNOTSUPP;
10361 
10362 	if (IS_ERR(wdev)) {
10363 		err = PTR_ERR(wdev);
10364 		if (err != -EINVAL)
10365 			return err;
10366 		wdev = NULL;
10367 	} else if (wdev->wiphy != &rdev->wiphy) {
10368 		return -EINVAL;
10369 	}
10370 
10371 	if (!info->attrs[NL80211_ATTR_TESTDATA])
10372 		return -EINVAL;
10373 
10374 	rdev->cur_cmd_info = info;
10375 	err = rdev_testmode_cmd(rdev, wdev,
10376 				nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
10377 				nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
10378 	rdev->cur_cmd_info = NULL;
10379 
10380 	return err;
10381 }
10382 
nl80211_testmode_dump(struct sk_buff * skb,struct netlink_callback * cb)10383 static int nl80211_testmode_dump(struct sk_buff *skb,
10384 				 struct netlink_callback *cb)
10385 {
10386 	struct cfg80211_registered_device *rdev;
10387 	struct nlattr **attrbuf = NULL;
10388 	int err;
10389 	long phy_idx;
10390 	void *data = NULL;
10391 	int data_len = 0;
10392 
10393 	rtnl_lock();
10394 
10395 	if (cb->args[0]) {
10396 		/*
10397 		 * 0 is a valid index, but not valid for args[0],
10398 		 * so we need to offset by 1.
10399 		 */
10400 		phy_idx = cb->args[0] - 1;
10401 
10402 		rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
10403 		if (!rdev) {
10404 			err = -ENOENT;
10405 			goto out_err;
10406 		}
10407 	} else {
10408 		attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
10409 				  GFP_KERNEL);
10410 		if (!attrbuf) {
10411 			err = -ENOMEM;
10412 			goto out_err;
10413 		}
10414 
10415 		err = nlmsg_parse_deprecated(cb->nlh,
10416 					     GENL_HDRLEN + nl80211_fam.hdrsize,
10417 					     attrbuf, nl80211_fam.maxattr,
10418 					     nl80211_policy, NULL);
10419 		if (err)
10420 			goto out_err;
10421 
10422 		rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
10423 		if (IS_ERR(rdev)) {
10424 			err = PTR_ERR(rdev);
10425 			goto out_err;
10426 		}
10427 		phy_idx = rdev->wiphy_idx;
10428 
10429 		if (attrbuf[NL80211_ATTR_TESTDATA])
10430 			cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
10431 	}
10432 
10433 	if (cb->args[1]) {
10434 		data = nla_data((void *)cb->args[1]);
10435 		data_len = nla_len((void *)cb->args[1]);
10436 	}
10437 
10438 	if (!rdev->ops->testmode_dump) {
10439 		err = -EOPNOTSUPP;
10440 		goto out_err;
10441 	}
10442 
10443 	while (1) {
10444 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
10445 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
10446 					   NL80211_CMD_TESTMODE);
10447 		struct nlattr *tmdata;
10448 
10449 		if (!hdr)
10450 			break;
10451 
10452 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
10453 			genlmsg_cancel(skb, hdr);
10454 			break;
10455 		}
10456 
10457 		tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
10458 		if (!tmdata) {
10459 			genlmsg_cancel(skb, hdr);
10460 			break;
10461 		}
10462 		err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
10463 		nla_nest_end(skb, tmdata);
10464 
10465 		if (err == -ENOBUFS || err == -ENOENT) {
10466 			genlmsg_cancel(skb, hdr);
10467 			break;
10468 		} else if (err) {
10469 			genlmsg_cancel(skb, hdr);
10470 			goto out_err;
10471 		}
10472 
10473 		genlmsg_end(skb, hdr);
10474 	}
10475 
10476 	err = skb->len;
10477 	/* see above */
10478 	cb->args[0] = phy_idx + 1;
10479  out_err:
10480 	kfree(attrbuf);
10481 	rtnl_unlock();
10482 	return err;
10483 }
10484 #endif
10485 
nl80211_connect(struct sk_buff * skb,struct genl_info * info)10486 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
10487 {
10488 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10489 	struct net_device *dev = info->user_ptr[1];
10490 	struct cfg80211_connect_params connect;
10491 	struct wiphy *wiphy;
10492 	struct cfg80211_cached_keys *connkeys = NULL;
10493 	u32 freq = 0;
10494 	int err;
10495 
10496 	memset(&connect, 0, sizeof(connect));
10497 
10498 	if (!info->attrs[NL80211_ATTR_SSID] ||
10499 	    !nla_len(info->attrs[NL80211_ATTR_SSID]))
10500 		return -EINVAL;
10501 
10502 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10503 		connect.auth_type =
10504 			nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10505 		if (!nl80211_valid_auth_type(rdev, connect.auth_type,
10506 					     NL80211_CMD_CONNECT))
10507 			return -EINVAL;
10508 	} else
10509 		connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
10510 
10511 	connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
10512 
10513 	if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
10514 	    !wiphy_ext_feature_isset(&rdev->wiphy,
10515 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
10516 		return -EINVAL;
10517 	connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
10518 
10519 	err = nl80211_crypto_settings(rdev, info, &connect.crypto,
10520 				      NL80211_MAX_NR_CIPHER_SUITES);
10521 	if (err)
10522 		return err;
10523 
10524 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10525 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10526 		return -EOPNOTSUPP;
10527 
10528 	wiphy = &rdev->wiphy;
10529 
10530 	connect.bg_scan_period = -1;
10531 	if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
10532 		(wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
10533 		connect.bg_scan_period =
10534 			nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
10535 	}
10536 
10537 	if (info->attrs[NL80211_ATTR_MAC])
10538 		connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10539 	else if (info->attrs[NL80211_ATTR_MAC_HINT])
10540 		connect.bssid_hint =
10541 			nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
10542 	connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10543 	connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10544 
10545 	if (info->attrs[NL80211_ATTR_IE]) {
10546 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10547 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10548 	}
10549 
10550 	if (info->attrs[NL80211_ATTR_USE_MFP]) {
10551 		connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10552 		if (connect.mfp == NL80211_MFP_OPTIONAL &&
10553 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10554 					     NL80211_EXT_FEATURE_MFP_OPTIONAL))
10555 			return -EOPNOTSUPP;
10556 	} else {
10557 		connect.mfp = NL80211_MFP_NO;
10558 	}
10559 
10560 	if (info->attrs[NL80211_ATTR_PREV_BSSID])
10561 		connect.prev_bssid =
10562 			nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10563 
10564 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
10565 		freq = MHZ_TO_KHZ(nla_get_u32(
10566 					info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10567 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10568 		freq +=
10569 		    nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10570 
10571 	if (freq) {
10572 		connect.channel = nl80211_get_valid_chan(wiphy, freq);
10573 		if (!connect.channel)
10574 			return -EINVAL;
10575 	} else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
10576 		freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
10577 		freq = MHZ_TO_KHZ(freq);
10578 		connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
10579 		if (!connect.channel_hint)
10580 			return -EINVAL;
10581 	}
10582 
10583 	if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
10584 		connect.edmg.channels =
10585 		      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
10586 
10587 		if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
10588 			connect.edmg.bw_config =
10589 				nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
10590 	}
10591 
10592 	if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
10593 		connkeys = nl80211_parse_connkeys(rdev, info, NULL);
10594 		if (IS_ERR(connkeys))
10595 			return PTR_ERR(connkeys);
10596 	}
10597 
10598 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10599 		connect.flags |= ASSOC_REQ_DISABLE_HT;
10600 
10601 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10602 		memcpy(&connect.ht_capa_mask,
10603 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10604 		       sizeof(connect.ht_capa_mask));
10605 
10606 	if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10607 		if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
10608 			kfree_sensitive(connkeys);
10609 			return -EINVAL;
10610 		}
10611 		memcpy(&connect.ht_capa,
10612 		       nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10613 		       sizeof(connect.ht_capa));
10614 	}
10615 
10616 	if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10617 		connect.flags |= ASSOC_REQ_DISABLE_VHT;
10618 
10619 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10620 		memcpy(&connect.vht_capa_mask,
10621 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10622 		       sizeof(connect.vht_capa_mask));
10623 
10624 	if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10625 		if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10626 			kfree_sensitive(connkeys);
10627 			return -EINVAL;
10628 		}
10629 		memcpy(&connect.vht_capa,
10630 		       nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10631 		       sizeof(connect.vht_capa));
10632 	}
10633 
10634 	if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10635 		if (!((rdev->wiphy.features &
10636 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10637 		       (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10638 		    !wiphy_ext_feature_isset(&rdev->wiphy,
10639 					     NL80211_EXT_FEATURE_RRM)) {
10640 			kfree_sensitive(connkeys);
10641 			return -EINVAL;
10642 		}
10643 		connect.flags |= ASSOC_REQ_USE_RRM;
10644 	}
10645 
10646 	connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10647 	if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10648 		kfree_sensitive(connkeys);
10649 		return -EOPNOTSUPP;
10650 	}
10651 
10652 	if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10653 		/* bss selection makes no sense if bssid is set */
10654 		if (connect.bssid) {
10655 			kfree_sensitive(connkeys);
10656 			return -EINVAL;
10657 		}
10658 
10659 		err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10660 				       wiphy, &connect.bss_select);
10661 		if (err) {
10662 			kfree_sensitive(connkeys);
10663 			return err;
10664 		}
10665 	}
10666 
10667 	if (wiphy_ext_feature_isset(&rdev->wiphy,
10668 				    NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10669 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10670 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10671 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10672 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10673 		connect.fils_erp_username =
10674 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10675 		connect.fils_erp_username_len =
10676 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10677 		connect.fils_erp_realm =
10678 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10679 		connect.fils_erp_realm_len =
10680 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10681 		connect.fils_erp_next_seq_num =
10682 			nla_get_u16(
10683 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10684 		connect.fils_erp_rrk =
10685 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10686 		connect.fils_erp_rrk_len =
10687 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10688 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10689 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10690 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10691 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10692 		kfree_sensitive(connkeys);
10693 		return -EINVAL;
10694 	}
10695 
10696 	if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10697 		if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10698 			kfree_sensitive(connkeys);
10699 			GENL_SET_ERR_MSG(info,
10700 					 "external auth requires connection ownership");
10701 			return -EINVAL;
10702 		}
10703 		connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10704 	}
10705 
10706 	wdev_lock(dev->ieee80211_ptr);
10707 
10708 	err = cfg80211_connect(rdev, dev, &connect, connkeys,
10709 			       connect.prev_bssid);
10710 	if (err)
10711 		kfree_sensitive(connkeys);
10712 
10713 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10714 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10715 		if (connect.bssid)
10716 			memcpy(dev->ieee80211_ptr->disconnect_bssid,
10717 			       connect.bssid, ETH_ALEN);
10718 		else
10719 			eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
10720 	}
10721 
10722 	wdev_unlock(dev->ieee80211_ptr);
10723 
10724 	return err;
10725 }
10726 
nl80211_update_connect_params(struct sk_buff * skb,struct genl_info * info)10727 static int nl80211_update_connect_params(struct sk_buff *skb,
10728 					 struct genl_info *info)
10729 {
10730 	struct cfg80211_connect_params connect = {};
10731 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10732 	struct net_device *dev = info->user_ptr[1];
10733 	struct wireless_dev *wdev = dev->ieee80211_ptr;
10734 	bool fils_sk_offload;
10735 	u32 auth_type;
10736 	u32 changed = 0;
10737 	int ret;
10738 
10739 	if (!rdev->ops->update_connect_params)
10740 		return -EOPNOTSUPP;
10741 
10742 	if (info->attrs[NL80211_ATTR_IE]) {
10743 		connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10744 		connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10745 		changed |= UPDATE_ASSOC_IES;
10746 	}
10747 
10748 	fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10749 						  NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10750 
10751 	/*
10752 	 * when driver supports fils-sk offload all attributes must be
10753 	 * provided. So the else covers "fils-sk-not-all" and
10754 	 * "no-fils-sk-any".
10755 	 */
10756 	if (fils_sk_offload &&
10757 	    info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10758 	    info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10759 	    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10760 	    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10761 		connect.fils_erp_username =
10762 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10763 		connect.fils_erp_username_len =
10764 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10765 		connect.fils_erp_realm =
10766 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10767 		connect.fils_erp_realm_len =
10768 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10769 		connect.fils_erp_next_seq_num =
10770 			nla_get_u16(
10771 			   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10772 		connect.fils_erp_rrk =
10773 			nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10774 		connect.fils_erp_rrk_len =
10775 			nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10776 		changed |= UPDATE_FILS_ERP_INFO;
10777 	} else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10778 		   info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10779 		   info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10780 		   info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10781 		return -EINVAL;
10782 	}
10783 
10784 	if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10785 		auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10786 		if (!nl80211_valid_auth_type(rdev, auth_type,
10787 					     NL80211_CMD_CONNECT))
10788 			return -EINVAL;
10789 
10790 		if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10791 		    fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10792 			return -EINVAL;
10793 
10794 		connect.auth_type = auth_type;
10795 		changed |= UPDATE_AUTH_TYPE;
10796 	}
10797 
10798 	wdev_lock(dev->ieee80211_ptr);
10799 	if (!wdev->current_bss)
10800 		ret = -ENOLINK;
10801 	else
10802 		ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10803 	wdev_unlock(dev->ieee80211_ptr);
10804 
10805 	return ret;
10806 }
10807 
nl80211_disconnect(struct sk_buff * skb,struct genl_info * info)10808 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10809 {
10810 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10811 	struct net_device *dev = info->user_ptr[1];
10812 	u16 reason;
10813 	int ret;
10814 
10815 	if (dev->ieee80211_ptr->conn_owner_nlportid &&
10816 	    dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10817 		return -EPERM;
10818 
10819 	if (!info->attrs[NL80211_ATTR_REASON_CODE])
10820 		reason = WLAN_REASON_DEAUTH_LEAVING;
10821 	else
10822 		reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10823 
10824 	if (reason == 0)
10825 		return -EINVAL;
10826 
10827 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10828 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10829 		return -EOPNOTSUPP;
10830 
10831 	wdev_lock(dev->ieee80211_ptr);
10832 	ret = cfg80211_disconnect(rdev, dev, reason, true);
10833 	wdev_unlock(dev->ieee80211_ptr);
10834 	return ret;
10835 }
10836 
nl80211_wiphy_netns(struct sk_buff * skb,struct genl_info * info)10837 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10838 {
10839 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10840 	struct net *net;
10841 	int err;
10842 
10843 	if (info->attrs[NL80211_ATTR_PID]) {
10844 		u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10845 
10846 		net = get_net_ns_by_pid(pid);
10847 	} else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10848 		u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10849 
10850 		net = get_net_ns_by_fd(fd);
10851 	} else {
10852 		return -EINVAL;
10853 	}
10854 
10855 	if (IS_ERR(net))
10856 		return PTR_ERR(net);
10857 
10858 	err = 0;
10859 
10860 	/* check if anything to do */
10861 	if (!net_eq(wiphy_net(&rdev->wiphy), net))
10862 		err = cfg80211_switch_netns(rdev, net);
10863 
10864 	put_net(net);
10865 	return err;
10866 }
10867 
nl80211_setdel_pmksa(struct sk_buff * skb,struct genl_info * info)10868 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10869 {
10870 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10871 	int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10872 			struct cfg80211_pmksa *pmksa) = NULL;
10873 	struct net_device *dev = info->user_ptr[1];
10874 	struct cfg80211_pmksa pmksa;
10875 
10876 	memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10877 
10878 	if (!info->attrs[NL80211_ATTR_PMKID])
10879 		return -EINVAL;
10880 
10881 	pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10882 
10883 	if (info->attrs[NL80211_ATTR_MAC]) {
10884 		pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10885 	} else if (info->attrs[NL80211_ATTR_SSID] &&
10886 		   info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10887 		   (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10888 		    info->attrs[NL80211_ATTR_PMK])) {
10889 		pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10890 		pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10891 		pmksa.cache_id =
10892 			nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10893 	} else {
10894 		return -EINVAL;
10895 	}
10896 	if (info->attrs[NL80211_ATTR_PMK]) {
10897 		pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10898 		pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10899 	}
10900 
10901 	if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
10902 		pmksa.pmk_lifetime =
10903 			nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
10904 
10905 	if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
10906 		pmksa.pmk_reauth_threshold =
10907 			nla_get_u8(
10908 				info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
10909 
10910 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10911 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10912 	    !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10913 	      wiphy_ext_feature_isset(&rdev->wiphy,
10914 				      NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10915 		return -EOPNOTSUPP;
10916 
10917 	switch (info->genlhdr->cmd) {
10918 	case NL80211_CMD_SET_PMKSA:
10919 		rdev_ops = rdev->ops->set_pmksa;
10920 		break;
10921 	case NL80211_CMD_DEL_PMKSA:
10922 		rdev_ops = rdev->ops->del_pmksa;
10923 		break;
10924 	default:
10925 		WARN_ON(1);
10926 		break;
10927 	}
10928 
10929 	if (!rdev_ops)
10930 		return -EOPNOTSUPP;
10931 
10932 	return rdev_ops(&rdev->wiphy, dev, &pmksa);
10933 }
10934 
nl80211_flush_pmksa(struct sk_buff * skb,struct genl_info * info)10935 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10936 {
10937 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10938 	struct net_device *dev = info->user_ptr[1];
10939 
10940 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10941 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10942 		return -EOPNOTSUPP;
10943 
10944 	if (!rdev->ops->flush_pmksa)
10945 		return -EOPNOTSUPP;
10946 
10947 	return rdev_flush_pmksa(rdev, dev);
10948 }
10949 
nl80211_tdls_mgmt(struct sk_buff * skb,struct genl_info * info)10950 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10951 {
10952 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10953 	struct net_device *dev = info->user_ptr[1];
10954 	u8 action_code, dialog_token;
10955 	u32 peer_capability = 0;
10956 	u16 status_code;
10957 	u8 *peer;
10958 	bool initiator;
10959 
10960 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10961 	    !rdev->ops->tdls_mgmt)
10962 		return -EOPNOTSUPP;
10963 
10964 	if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10965 	    !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10966 	    !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10967 	    !info->attrs[NL80211_ATTR_IE] ||
10968 	    !info->attrs[NL80211_ATTR_MAC])
10969 		return -EINVAL;
10970 
10971 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10972 	action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10973 	status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10974 	dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10975 	initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10976 	if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10977 		peer_capability =
10978 			nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10979 
10980 	return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10981 			      dialog_token, status_code, peer_capability,
10982 			      initiator,
10983 			      nla_data(info->attrs[NL80211_ATTR_IE]),
10984 			      nla_len(info->attrs[NL80211_ATTR_IE]));
10985 }
10986 
nl80211_tdls_oper(struct sk_buff * skb,struct genl_info * info)10987 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10988 {
10989 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
10990 	struct net_device *dev = info->user_ptr[1];
10991 	enum nl80211_tdls_operation operation;
10992 	u8 *peer;
10993 
10994 	if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10995 	    !rdev->ops->tdls_oper)
10996 		return -EOPNOTSUPP;
10997 
10998 	if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10999 	    !info->attrs[NL80211_ATTR_MAC])
11000 		return -EINVAL;
11001 
11002 	operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
11003 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
11004 
11005 	return rdev_tdls_oper(rdev, dev, peer, operation);
11006 }
11007 
nl80211_remain_on_channel(struct sk_buff * skb,struct genl_info * info)11008 static int nl80211_remain_on_channel(struct sk_buff *skb,
11009 				     struct genl_info *info)
11010 {
11011 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11012 	struct wireless_dev *wdev = info->user_ptr[1];
11013 	struct cfg80211_chan_def chandef;
11014 	const struct cfg80211_chan_def *compat_chandef;
11015 	struct sk_buff *msg;
11016 	void *hdr;
11017 	u64 cookie;
11018 	u32 duration;
11019 	int err;
11020 
11021 	if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
11022 	    !info->attrs[NL80211_ATTR_DURATION])
11023 		return -EINVAL;
11024 
11025 	duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
11026 
11027 	if (!rdev->ops->remain_on_channel ||
11028 	    !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
11029 		return -EOPNOTSUPP;
11030 
11031 	/*
11032 	 * We should be on that channel for at least a minimum amount of
11033 	 * time (10ms) but no longer than the driver supports.
11034 	 */
11035 	if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
11036 	    duration > rdev->wiphy.max_remain_on_channel_duration)
11037 		return -EINVAL;
11038 
11039 	err = nl80211_parse_chandef(rdev, info, &chandef);
11040 	if (err)
11041 		return err;
11042 
11043 	wdev_lock(wdev);
11044 	if (!cfg80211_off_channel_oper_allowed(wdev) &&
11045 	    !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
11046 		compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
11047 							     &chandef);
11048 		if (compat_chandef != &chandef) {
11049 			wdev_unlock(wdev);
11050 			return -EBUSY;
11051 		}
11052 	}
11053 	wdev_unlock(wdev);
11054 
11055 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11056 	if (!msg)
11057 		return -ENOMEM;
11058 
11059 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11060 			     NL80211_CMD_REMAIN_ON_CHANNEL);
11061 	if (!hdr) {
11062 		err = -ENOBUFS;
11063 		goto free_msg;
11064 	}
11065 
11066 	err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
11067 				     duration, &cookie);
11068 
11069 	if (err)
11070 		goto free_msg;
11071 
11072 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11073 			      NL80211_ATTR_PAD))
11074 		goto nla_put_failure;
11075 
11076 	genlmsg_end(msg, hdr);
11077 
11078 	return genlmsg_reply(msg, info);
11079 
11080  nla_put_failure:
11081 	err = -ENOBUFS;
11082  free_msg:
11083 	nlmsg_free(msg);
11084 	return err;
11085 }
11086 
nl80211_cancel_remain_on_channel(struct sk_buff * skb,struct genl_info * info)11087 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
11088 					    struct genl_info *info)
11089 {
11090 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11091 	struct wireless_dev *wdev = info->user_ptr[1];
11092 	u64 cookie;
11093 
11094 	if (!info->attrs[NL80211_ATTR_COOKIE])
11095 		return -EINVAL;
11096 
11097 	if (!rdev->ops->cancel_remain_on_channel)
11098 		return -EOPNOTSUPP;
11099 
11100 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11101 
11102 	return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
11103 }
11104 
nl80211_set_tx_bitrate_mask(struct sk_buff * skb,struct genl_info * info)11105 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
11106 				       struct genl_info *info)
11107 {
11108 	struct cfg80211_bitrate_mask mask;
11109 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11110 	struct net_device *dev = info->user_ptr[1];
11111 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11112 	int err;
11113 
11114 	if (!rdev->ops->set_bitrate_mask)
11115 		return -EOPNOTSUPP;
11116 
11117 	wdev_lock(wdev);
11118 	err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
11119 					    NL80211_ATTR_TX_RATES, &mask,
11120 					    dev);
11121 	if (err)
11122 		goto out;
11123 
11124 	err = rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
11125 out:
11126 	wdev_unlock(wdev);
11127 	return err;
11128 }
11129 
nl80211_register_mgmt(struct sk_buff * skb,struct genl_info * info)11130 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
11131 {
11132 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11133 	struct wireless_dev *wdev = info->user_ptr[1];
11134 	u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
11135 
11136 	if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
11137 		return -EINVAL;
11138 
11139 	if (info->attrs[NL80211_ATTR_FRAME_TYPE])
11140 		frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
11141 
11142 	switch (wdev->iftype) {
11143 	case NL80211_IFTYPE_STATION:
11144 	case NL80211_IFTYPE_ADHOC:
11145 	case NL80211_IFTYPE_P2P_CLIENT:
11146 	case NL80211_IFTYPE_AP:
11147 	case NL80211_IFTYPE_AP_VLAN:
11148 	case NL80211_IFTYPE_MESH_POINT:
11149 	case NL80211_IFTYPE_P2P_GO:
11150 	case NL80211_IFTYPE_P2P_DEVICE:
11151 		break;
11152 	case NL80211_IFTYPE_NAN:
11153 	default:
11154 		return -EOPNOTSUPP;
11155 	}
11156 
11157 	/* not much point in registering if we can't reply */
11158 	if (!rdev->ops->mgmt_tx)
11159 		return -EOPNOTSUPP;
11160 
11161 	if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
11162 	    !wiphy_ext_feature_isset(&rdev->wiphy,
11163 				     NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
11164 		GENL_SET_ERR_MSG(info,
11165 				 "multicast RX registrations are not supported");
11166 		return -EOPNOTSUPP;
11167 	}
11168 
11169 	return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
11170 					   nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
11171 					   nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
11172 					   info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
11173 					   info->extack);
11174 }
11175 
nl80211_tx_mgmt(struct sk_buff * skb,struct genl_info * info)11176 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
11177 {
11178 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11179 	struct wireless_dev *wdev = info->user_ptr[1];
11180 	struct cfg80211_chan_def chandef;
11181 	int err;
11182 	void *hdr = NULL;
11183 	u64 cookie;
11184 	struct sk_buff *msg = NULL;
11185 	struct cfg80211_mgmt_tx_params params = {
11186 		.dont_wait_for_ack =
11187 			info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
11188 	};
11189 
11190 	if (!info->attrs[NL80211_ATTR_FRAME])
11191 		return -EINVAL;
11192 
11193 	if (!rdev->ops->mgmt_tx)
11194 		return -EOPNOTSUPP;
11195 
11196 	switch (wdev->iftype) {
11197 	case NL80211_IFTYPE_P2P_DEVICE:
11198 		if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
11199 			return -EINVAL;
11200 	case NL80211_IFTYPE_STATION:
11201 	case NL80211_IFTYPE_ADHOC:
11202 	case NL80211_IFTYPE_P2P_CLIENT:
11203 	case NL80211_IFTYPE_AP:
11204 	case NL80211_IFTYPE_AP_VLAN:
11205 	case NL80211_IFTYPE_MESH_POINT:
11206 	case NL80211_IFTYPE_P2P_GO:
11207 		break;
11208 	case NL80211_IFTYPE_NAN:
11209 	default:
11210 		return -EOPNOTSUPP;
11211 	}
11212 
11213 	if (info->attrs[NL80211_ATTR_DURATION]) {
11214 		if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
11215 			return -EINVAL;
11216 		params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
11217 
11218 		/*
11219 		 * We should wait on the channel for at least a minimum amount
11220 		 * of time (10ms) but no longer than the driver supports.
11221 		 */
11222 		if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
11223 		    params.wait > rdev->wiphy.max_remain_on_channel_duration)
11224 			return -EINVAL;
11225 	}
11226 
11227 	params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
11228 
11229 	if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
11230 		return -EINVAL;
11231 
11232 	params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
11233 
11234 	/* get the channel if any has been specified, otherwise pass NULL to
11235 	 * the driver. The latter will use the current one
11236 	 */
11237 	chandef.chan = NULL;
11238 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11239 		err = nl80211_parse_chandef(rdev, info, &chandef);
11240 		if (err)
11241 			return err;
11242 	}
11243 
11244 	if (!chandef.chan && params.offchan)
11245 		return -EINVAL;
11246 
11247 	wdev_lock(wdev);
11248 	if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
11249 		wdev_unlock(wdev);
11250 		return -EBUSY;
11251 	}
11252 	wdev_unlock(wdev);
11253 
11254 	params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
11255 	params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
11256 
11257 	if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
11258 		int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
11259 		int i;
11260 
11261 		if (len % sizeof(u16))
11262 			return -EINVAL;
11263 
11264 		params.n_csa_offsets = len / sizeof(u16);
11265 		params.csa_offsets =
11266 			nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
11267 
11268 		/* check that all the offsets fit the frame */
11269 		for (i = 0; i < params.n_csa_offsets; i++) {
11270 			if (params.csa_offsets[i] >= params.len)
11271 				return -EINVAL;
11272 		}
11273 	}
11274 
11275 	if (!params.dont_wait_for_ack) {
11276 		msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11277 		if (!msg)
11278 			return -ENOMEM;
11279 
11280 		hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11281 				     NL80211_CMD_FRAME);
11282 		if (!hdr) {
11283 			err = -ENOBUFS;
11284 			goto free_msg;
11285 		}
11286 	}
11287 
11288 	params.chan = chandef.chan;
11289 	err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
11290 	if (err)
11291 		goto free_msg;
11292 
11293 	if (msg) {
11294 		if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11295 				      NL80211_ATTR_PAD))
11296 			goto nla_put_failure;
11297 
11298 		genlmsg_end(msg, hdr);
11299 		return genlmsg_reply(msg, info);
11300 	}
11301 
11302 	return 0;
11303 
11304  nla_put_failure:
11305 	err = -ENOBUFS;
11306  free_msg:
11307 	nlmsg_free(msg);
11308 	return err;
11309 }
11310 
nl80211_tx_mgmt_cancel_wait(struct sk_buff * skb,struct genl_info * info)11311 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
11312 {
11313 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11314 	struct wireless_dev *wdev = info->user_ptr[1];
11315 	u64 cookie;
11316 
11317 	if (!info->attrs[NL80211_ATTR_COOKIE])
11318 		return -EINVAL;
11319 
11320 	if (!rdev->ops->mgmt_tx_cancel_wait)
11321 		return -EOPNOTSUPP;
11322 
11323 	switch (wdev->iftype) {
11324 	case NL80211_IFTYPE_STATION:
11325 	case NL80211_IFTYPE_ADHOC:
11326 	case NL80211_IFTYPE_P2P_CLIENT:
11327 	case NL80211_IFTYPE_AP:
11328 	case NL80211_IFTYPE_AP_VLAN:
11329 	case NL80211_IFTYPE_P2P_GO:
11330 	case NL80211_IFTYPE_P2P_DEVICE:
11331 		break;
11332 	case NL80211_IFTYPE_NAN:
11333 	default:
11334 		return -EOPNOTSUPP;
11335 	}
11336 
11337 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11338 
11339 	return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
11340 }
11341 
nl80211_set_power_save(struct sk_buff * skb,struct genl_info * info)11342 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
11343 {
11344 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11345 	struct wireless_dev *wdev;
11346 	struct net_device *dev = info->user_ptr[1];
11347 	u8 ps_state;
11348 	bool state;
11349 	int err;
11350 
11351 	if (!info->attrs[NL80211_ATTR_PS_STATE])
11352 		return -EINVAL;
11353 
11354 	ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
11355 
11356 	wdev = dev->ieee80211_ptr;
11357 
11358 	if (!rdev->ops->set_power_mgmt)
11359 		return -EOPNOTSUPP;
11360 
11361 	state = (ps_state == NL80211_PS_ENABLED) ? true : false;
11362 
11363 	if (state == wdev->ps)
11364 		return 0;
11365 
11366 	err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
11367 	if (!err)
11368 		wdev->ps = state;
11369 	return err;
11370 }
11371 
nl80211_get_power_save(struct sk_buff * skb,struct genl_info * info)11372 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
11373 {
11374 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11375 	enum nl80211_ps_state ps_state;
11376 	struct wireless_dev *wdev;
11377 	struct net_device *dev = info->user_ptr[1];
11378 	struct sk_buff *msg;
11379 	void *hdr;
11380 	int err;
11381 
11382 	wdev = dev->ieee80211_ptr;
11383 
11384 	if (!rdev->ops->set_power_mgmt)
11385 		return -EOPNOTSUPP;
11386 
11387 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11388 	if (!msg)
11389 		return -ENOMEM;
11390 
11391 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11392 			     NL80211_CMD_GET_POWER_SAVE);
11393 	if (!hdr) {
11394 		err = -ENOBUFS;
11395 		goto free_msg;
11396 	}
11397 
11398 	if (wdev->ps)
11399 		ps_state = NL80211_PS_ENABLED;
11400 	else
11401 		ps_state = NL80211_PS_DISABLED;
11402 
11403 	if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
11404 		goto nla_put_failure;
11405 
11406 	genlmsg_end(msg, hdr);
11407 	return genlmsg_reply(msg, info);
11408 
11409  nla_put_failure:
11410 	err = -ENOBUFS;
11411  free_msg:
11412 	nlmsg_free(msg);
11413 	return err;
11414 }
11415 
11416 static const struct nla_policy
11417 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
11418 	[NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
11419 	[NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
11420 	[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
11421 	[NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
11422 	[NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
11423 	[NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
11424 	[NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
11425 };
11426 
nl80211_set_cqm_txe(struct genl_info * info,u32 rate,u32 pkts,u32 intvl)11427 static int nl80211_set_cqm_txe(struct genl_info *info,
11428 			       u32 rate, u32 pkts, u32 intvl)
11429 {
11430 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11431 	struct net_device *dev = info->user_ptr[1];
11432 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11433 
11434 	if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
11435 		return -EINVAL;
11436 
11437 	if (!rdev->ops->set_cqm_txe_config)
11438 		return -EOPNOTSUPP;
11439 
11440 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11441 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11442 		return -EOPNOTSUPP;
11443 
11444 	return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
11445 }
11446 
cfg80211_cqm_rssi_update(struct cfg80211_registered_device * rdev,struct net_device * dev)11447 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
11448 				    struct net_device *dev)
11449 {
11450 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11451 	s32 last, low, high;
11452 	u32 hyst;
11453 	int i, n, low_index;
11454 	int err;
11455 
11456 	/* RSSI reporting disabled? */
11457 	if (!wdev->cqm_config)
11458 		return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
11459 
11460 	/*
11461 	 * Obtain current RSSI value if possible, if not and no RSSI threshold
11462 	 * event has been received yet, we should receive an event after a
11463 	 * connection is established and enough beacons received to calculate
11464 	 * the average.
11465 	 */
11466 	if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
11467 	    rdev->ops->get_station) {
11468 		struct station_info sinfo = {};
11469 		u8 *mac_addr;
11470 
11471 		mac_addr = wdev->current_bss->pub.bssid;
11472 
11473 		err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
11474 		if (err)
11475 			return err;
11476 
11477 		cfg80211_sinfo_release_content(&sinfo);
11478 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
11479 			wdev->cqm_config->last_rssi_event_value =
11480 				(s8) sinfo.rx_beacon_signal_avg;
11481 	}
11482 
11483 	last = wdev->cqm_config->last_rssi_event_value;
11484 	hyst = wdev->cqm_config->rssi_hyst;
11485 	n = wdev->cqm_config->n_rssi_thresholds;
11486 
11487 	for (i = 0; i < n; i++) {
11488 		i = array_index_nospec(i, n);
11489 		if (last < wdev->cqm_config->rssi_thresholds[i])
11490 			break;
11491 	}
11492 
11493 	low_index = i - 1;
11494 	if (low_index >= 0) {
11495 		low_index = array_index_nospec(low_index, n);
11496 		low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
11497 	} else {
11498 		low = S32_MIN;
11499 	}
11500 	if (i < n) {
11501 		i = array_index_nospec(i, n);
11502 		high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
11503 	} else {
11504 		high = S32_MAX;
11505 	}
11506 
11507 	return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
11508 }
11509 
nl80211_set_cqm_rssi(struct genl_info * info,const s32 * thresholds,int n_thresholds,u32 hysteresis)11510 static int nl80211_set_cqm_rssi(struct genl_info *info,
11511 				const s32 *thresholds, int n_thresholds,
11512 				u32 hysteresis)
11513 {
11514 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11515 	struct net_device *dev = info->user_ptr[1];
11516 	struct wireless_dev *wdev = dev->ieee80211_ptr;
11517 	int i, err;
11518 	s32 prev = S32_MIN;
11519 
11520 	/* Check all values negative and sorted */
11521 	for (i = 0; i < n_thresholds; i++) {
11522 		if (thresholds[i] > 0 || thresholds[i] <= prev)
11523 			return -EINVAL;
11524 
11525 		prev = thresholds[i];
11526 	}
11527 
11528 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
11529 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
11530 		return -EOPNOTSUPP;
11531 
11532 	wdev_lock(wdev);
11533 	cfg80211_cqm_config_free(wdev);
11534 	wdev_unlock(wdev);
11535 
11536 	if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
11537 		if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
11538 			return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
11539 
11540 		return rdev_set_cqm_rssi_config(rdev, dev,
11541 						thresholds[0], hysteresis);
11542 	}
11543 
11544 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
11545 				     NL80211_EXT_FEATURE_CQM_RSSI_LIST))
11546 		return -EOPNOTSUPP;
11547 
11548 	if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
11549 		n_thresholds = 0;
11550 
11551 	wdev_lock(wdev);
11552 	if (n_thresholds) {
11553 		struct cfg80211_cqm_config *cqm_config;
11554 
11555 		cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
11556 				     n_thresholds * sizeof(s32), GFP_KERNEL);
11557 		if (!cqm_config) {
11558 			err = -ENOMEM;
11559 			goto unlock;
11560 		}
11561 
11562 		cqm_config->rssi_hyst = hysteresis;
11563 		cqm_config->n_rssi_thresholds = n_thresholds;
11564 		memcpy(cqm_config->rssi_thresholds, thresholds,
11565 		       n_thresholds * sizeof(s32));
11566 
11567 		wdev->cqm_config = cqm_config;
11568 	}
11569 
11570 	err = cfg80211_cqm_rssi_update(rdev, dev);
11571 
11572 unlock:
11573 	wdev_unlock(wdev);
11574 
11575 	return err;
11576 }
11577 
nl80211_set_cqm(struct sk_buff * skb,struct genl_info * info)11578 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
11579 {
11580 	struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
11581 	struct nlattr *cqm;
11582 	int err;
11583 
11584 	cqm = info->attrs[NL80211_ATTR_CQM];
11585 	if (!cqm)
11586 		return -EINVAL;
11587 
11588 	err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
11589 					  nl80211_attr_cqm_policy,
11590 					  info->extack);
11591 	if (err)
11592 		return err;
11593 
11594 	if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
11595 	    attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
11596 		const s32 *thresholds =
11597 			nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11598 		int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
11599 		u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
11600 
11601 		if (len % 4)
11602 			return -EINVAL;
11603 
11604 		return nl80211_set_cqm_rssi(info, thresholds, len / 4,
11605 					    hysteresis);
11606 	}
11607 
11608 	if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
11609 	    attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
11610 	    attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
11611 		u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
11612 		u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
11613 		u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
11614 
11615 		return nl80211_set_cqm_txe(info, rate, pkts, intvl);
11616 	}
11617 
11618 	return -EINVAL;
11619 }
11620 
nl80211_join_ocb(struct sk_buff * skb,struct genl_info * info)11621 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
11622 {
11623 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11624 	struct net_device *dev = info->user_ptr[1];
11625 	struct ocb_setup setup = {};
11626 	int err;
11627 
11628 	err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11629 	if (err)
11630 		return err;
11631 
11632 	return cfg80211_join_ocb(rdev, dev, &setup);
11633 }
11634 
nl80211_leave_ocb(struct sk_buff * skb,struct genl_info * info)11635 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
11636 {
11637 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11638 	struct net_device *dev = info->user_ptr[1];
11639 
11640 	return cfg80211_leave_ocb(rdev, dev);
11641 }
11642 
nl80211_join_mesh(struct sk_buff * skb,struct genl_info * info)11643 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11644 {
11645 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11646 	struct net_device *dev = info->user_ptr[1];
11647 	struct mesh_config cfg;
11648 	struct mesh_setup setup;
11649 	int err;
11650 
11651 	/* start with default */
11652 	memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11653 	memcpy(&setup, &default_mesh_setup, sizeof(setup));
11654 
11655 	if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11656 		/* and parse parameters if given */
11657 		err = nl80211_parse_mesh_config(info, &cfg, NULL);
11658 		if (err)
11659 			return err;
11660 	}
11661 
11662 	if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11663 	    !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11664 		return -EINVAL;
11665 
11666 	setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11667 	setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11668 
11669 	if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11670 	    !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11671 			    nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11672 			return -EINVAL;
11673 
11674 	if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11675 		setup.beacon_interval =
11676 			nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11677 
11678 		err = cfg80211_validate_beacon_int(rdev,
11679 						   NL80211_IFTYPE_MESH_POINT,
11680 						   setup.beacon_interval);
11681 		if (err)
11682 			return err;
11683 	}
11684 
11685 	if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11686 		setup.dtim_period =
11687 			nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11688 		if (setup.dtim_period < 1 || setup.dtim_period > 100)
11689 			return -EINVAL;
11690 	}
11691 
11692 	if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11693 		/* parse additional setup parameters if given */
11694 		err = nl80211_parse_mesh_setup(info, &setup);
11695 		if (err)
11696 			return err;
11697 	}
11698 
11699 	if (setup.user_mpm)
11700 		cfg.auto_open_plinks = false;
11701 
11702 	if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11703 		err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11704 		if (err)
11705 			return err;
11706 	} else {
11707 		/* __cfg80211_join_mesh() will sort it out */
11708 		setup.chandef.chan = NULL;
11709 	}
11710 
11711 	if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11712 		u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11713 		int n_rates =
11714 			nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11715 		struct ieee80211_supported_band *sband;
11716 
11717 		if (!setup.chandef.chan)
11718 			return -EINVAL;
11719 
11720 		sband = rdev->wiphy.bands[setup.chandef.chan->band];
11721 
11722 		err = ieee80211_get_ratemask(sband, rates, n_rates,
11723 					     &setup.basic_rates);
11724 		if (err)
11725 			return err;
11726 	}
11727 
11728 	if (info->attrs[NL80211_ATTR_TX_RATES]) {
11729 		err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
11730 						    NL80211_ATTR_TX_RATES,
11731 						    &setup.beacon_rate,
11732 						    dev);
11733 		if (err)
11734 			return err;
11735 
11736 		if (!setup.chandef.chan)
11737 			return -EINVAL;
11738 
11739 		err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11740 					      &setup.beacon_rate);
11741 		if (err)
11742 			return err;
11743 	}
11744 
11745 	setup.userspace_handles_dfs =
11746 		nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11747 
11748 	if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11749 		int r = validate_pae_over_nl80211(rdev, info);
11750 
11751 		if (r < 0)
11752 			return r;
11753 
11754 		setup.control_port_over_nl80211 = true;
11755 	}
11756 
11757 	wdev_lock(dev->ieee80211_ptr);
11758 	err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11759 	if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11760 		dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11761 	wdev_unlock(dev->ieee80211_ptr);
11762 
11763 	return err;
11764 }
11765 
nl80211_leave_mesh(struct sk_buff * skb,struct genl_info * info)11766 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11767 {
11768 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11769 	struct net_device *dev = info->user_ptr[1];
11770 
11771 	return cfg80211_leave_mesh(rdev, dev);
11772 }
11773 
11774 #ifdef CONFIG_PM
nl80211_send_wowlan_patterns(struct sk_buff * msg,struct cfg80211_registered_device * rdev)11775 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11776 					struct cfg80211_registered_device *rdev)
11777 {
11778 	struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11779 	struct nlattr *nl_pats, *nl_pat;
11780 	int i, pat_len;
11781 
11782 	if (!wowlan->n_patterns)
11783 		return 0;
11784 
11785 	nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11786 	if (!nl_pats)
11787 		return -ENOBUFS;
11788 
11789 	for (i = 0; i < wowlan->n_patterns; i++) {
11790 		nl_pat = nla_nest_start_noflag(msg, i + 1);
11791 		if (!nl_pat)
11792 			return -ENOBUFS;
11793 		pat_len = wowlan->patterns[i].pattern_len;
11794 		if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11795 			    wowlan->patterns[i].mask) ||
11796 		    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11797 			    wowlan->patterns[i].pattern) ||
11798 		    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11799 				wowlan->patterns[i].pkt_offset))
11800 			return -ENOBUFS;
11801 		nla_nest_end(msg, nl_pat);
11802 	}
11803 	nla_nest_end(msg, nl_pats);
11804 
11805 	return 0;
11806 }
11807 
nl80211_send_wowlan_tcp(struct sk_buff * msg,struct cfg80211_wowlan_tcp * tcp)11808 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11809 				   struct cfg80211_wowlan_tcp *tcp)
11810 {
11811 	struct nlattr *nl_tcp;
11812 
11813 	if (!tcp)
11814 		return 0;
11815 
11816 	nl_tcp = nla_nest_start_noflag(msg,
11817 				       NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11818 	if (!nl_tcp)
11819 		return -ENOBUFS;
11820 
11821 	if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11822 	    nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11823 	    nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11824 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11825 	    nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11826 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11827 		    tcp->payload_len, tcp->payload) ||
11828 	    nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11829 			tcp->data_interval) ||
11830 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11831 		    tcp->wake_len, tcp->wake_data) ||
11832 	    nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11833 		    DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11834 		return -ENOBUFS;
11835 
11836 	if (tcp->payload_seq.len &&
11837 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11838 		    sizeof(tcp->payload_seq), &tcp->payload_seq))
11839 		return -ENOBUFS;
11840 
11841 	if (tcp->payload_tok.len &&
11842 	    nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11843 		    sizeof(tcp->payload_tok) + tcp->tokens_size,
11844 		    &tcp->payload_tok))
11845 		return -ENOBUFS;
11846 
11847 	nla_nest_end(msg, nl_tcp);
11848 
11849 	return 0;
11850 }
11851 
nl80211_send_wowlan_nd(struct sk_buff * msg,struct cfg80211_sched_scan_request * req)11852 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11853 				  struct cfg80211_sched_scan_request *req)
11854 {
11855 	struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11856 	int i;
11857 
11858 	if (!req)
11859 		return 0;
11860 
11861 	nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11862 	if (!nd)
11863 		return -ENOBUFS;
11864 
11865 	if (req->n_scan_plans == 1 &&
11866 	    nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11867 			req->scan_plans[0].interval * 1000))
11868 		return -ENOBUFS;
11869 
11870 	if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11871 		return -ENOBUFS;
11872 
11873 	if (req->relative_rssi_set) {
11874 		struct nl80211_bss_select_rssi_adjust rssi_adjust;
11875 
11876 		if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11877 			       req->relative_rssi))
11878 			return -ENOBUFS;
11879 
11880 		rssi_adjust.band = req->rssi_adjust.band;
11881 		rssi_adjust.delta = req->rssi_adjust.delta;
11882 		if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11883 			    sizeof(rssi_adjust), &rssi_adjust))
11884 			return -ENOBUFS;
11885 	}
11886 
11887 	freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11888 	if (!freqs)
11889 		return -ENOBUFS;
11890 
11891 	for (i = 0; i < req->n_channels; i++) {
11892 		if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11893 			return -ENOBUFS;
11894 	}
11895 
11896 	nla_nest_end(msg, freqs);
11897 
11898 	if (req->n_match_sets) {
11899 		matches = nla_nest_start_noflag(msg,
11900 						NL80211_ATTR_SCHED_SCAN_MATCH);
11901 		if (!matches)
11902 			return -ENOBUFS;
11903 
11904 		for (i = 0; i < req->n_match_sets; i++) {
11905 			match = nla_nest_start_noflag(msg, i);
11906 			if (!match)
11907 				return -ENOBUFS;
11908 
11909 			if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11910 				    req->match_sets[i].ssid.ssid_len,
11911 				    req->match_sets[i].ssid.ssid))
11912 				return -ENOBUFS;
11913 			nla_nest_end(msg, match);
11914 		}
11915 		nla_nest_end(msg, matches);
11916 	}
11917 
11918 	scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11919 	if (!scan_plans)
11920 		return -ENOBUFS;
11921 
11922 	for (i = 0; i < req->n_scan_plans; i++) {
11923 		scan_plan = nla_nest_start_noflag(msg, i + 1);
11924 		if (!scan_plan)
11925 			return -ENOBUFS;
11926 
11927 		if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11928 				req->scan_plans[i].interval) ||
11929 		    (req->scan_plans[i].iterations &&
11930 		     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11931 				 req->scan_plans[i].iterations)))
11932 			return -ENOBUFS;
11933 		nla_nest_end(msg, scan_plan);
11934 	}
11935 	nla_nest_end(msg, scan_plans);
11936 
11937 	nla_nest_end(msg, nd);
11938 
11939 	return 0;
11940 }
11941 
nl80211_get_wowlan(struct sk_buff * skb,struct genl_info * info)11942 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11943 {
11944 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
11945 	struct sk_buff *msg;
11946 	void *hdr;
11947 	u32 size = NLMSG_DEFAULT_SIZE;
11948 
11949 	if (!rdev->wiphy.wowlan)
11950 		return -EOPNOTSUPP;
11951 
11952 	if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11953 		/* adjust size to have room for all the data */
11954 		size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11955 			rdev->wiphy.wowlan_config->tcp->payload_len +
11956 			rdev->wiphy.wowlan_config->tcp->wake_len +
11957 			rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11958 	}
11959 
11960 	msg = nlmsg_new(size, GFP_KERNEL);
11961 	if (!msg)
11962 		return -ENOMEM;
11963 
11964 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11965 			     NL80211_CMD_GET_WOWLAN);
11966 	if (!hdr)
11967 		goto nla_put_failure;
11968 
11969 	if (rdev->wiphy.wowlan_config) {
11970 		struct nlattr *nl_wowlan;
11971 
11972 		nl_wowlan = nla_nest_start_noflag(msg,
11973 						  NL80211_ATTR_WOWLAN_TRIGGERS);
11974 		if (!nl_wowlan)
11975 			goto nla_put_failure;
11976 
11977 		if ((rdev->wiphy.wowlan_config->any &&
11978 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11979 		    (rdev->wiphy.wowlan_config->disconnect &&
11980 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11981 		    (rdev->wiphy.wowlan_config->magic_pkt &&
11982 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11983 		    (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11984 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11985 		    (rdev->wiphy.wowlan_config->eap_identity_req &&
11986 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11987 		    (rdev->wiphy.wowlan_config->four_way_handshake &&
11988 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11989 		    (rdev->wiphy.wowlan_config->rfkill_release &&
11990 		     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11991 			goto nla_put_failure;
11992 
11993 		if (nl80211_send_wowlan_patterns(msg, rdev))
11994 			goto nla_put_failure;
11995 
11996 		if (nl80211_send_wowlan_tcp(msg,
11997 					    rdev->wiphy.wowlan_config->tcp))
11998 			goto nla_put_failure;
11999 
12000 		if (nl80211_send_wowlan_nd(
12001 			    msg,
12002 			    rdev->wiphy.wowlan_config->nd_config))
12003 			goto nla_put_failure;
12004 
12005 		nla_nest_end(msg, nl_wowlan);
12006 	}
12007 
12008 	genlmsg_end(msg, hdr);
12009 	return genlmsg_reply(msg, info);
12010 
12011 nla_put_failure:
12012 	nlmsg_free(msg);
12013 	return -ENOBUFS;
12014 }
12015 
nl80211_parse_wowlan_tcp(struct cfg80211_registered_device * rdev,struct nlattr * attr,struct cfg80211_wowlan * trig)12016 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
12017 				    struct nlattr *attr,
12018 				    struct cfg80211_wowlan *trig)
12019 {
12020 	struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
12021 	struct cfg80211_wowlan_tcp *cfg;
12022 	struct nl80211_wowlan_tcp_data_token *tok = NULL;
12023 	struct nl80211_wowlan_tcp_data_seq *seq = NULL;
12024 	u32 size;
12025 	u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
12026 	int err, port;
12027 
12028 	if (!rdev->wiphy.wowlan->tcp)
12029 		return -EINVAL;
12030 
12031 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
12032 					  nl80211_wowlan_tcp_policy, NULL);
12033 	if (err)
12034 		return err;
12035 
12036 	if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
12037 	    !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
12038 	    !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
12039 	    !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
12040 	    !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
12041 	    !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
12042 	    !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
12043 	    !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
12044 		return -EINVAL;
12045 
12046 	data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
12047 	if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
12048 		return -EINVAL;
12049 
12050 	if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
12051 			rdev->wiphy.wowlan->tcp->data_interval_max ||
12052 	    nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
12053 		return -EINVAL;
12054 
12055 	wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
12056 	if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
12057 		return -EINVAL;
12058 
12059 	wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
12060 	if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
12061 		return -EINVAL;
12062 
12063 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
12064 		u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
12065 
12066 		tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
12067 		tokens_size = tokln - sizeof(*tok);
12068 
12069 		if (!tok->len || tokens_size % tok->len)
12070 			return -EINVAL;
12071 		if (!rdev->wiphy.wowlan->tcp->tok)
12072 			return -EINVAL;
12073 		if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
12074 			return -EINVAL;
12075 		if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
12076 			return -EINVAL;
12077 		if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
12078 			return -EINVAL;
12079 		if (tok->offset + tok->len > data_size)
12080 			return -EINVAL;
12081 	}
12082 
12083 	if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
12084 		seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
12085 		if (!rdev->wiphy.wowlan->tcp->seq)
12086 			return -EINVAL;
12087 		if (seq->len == 0 || seq->len > 4)
12088 			return -EINVAL;
12089 		if (seq->len + seq->offset > data_size)
12090 			return -EINVAL;
12091 	}
12092 
12093 	size = sizeof(*cfg);
12094 	size += data_size;
12095 	size += wake_size + wake_mask_size;
12096 	size += tokens_size;
12097 
12098 	cfg = kzalloc(size, GFP_KERNEL);
12099 	if (!cfg)
12100 		return -ENOMEM;
12101 	cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
12102 	cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
12103 	memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
12104 	       ETH_ALEN);
12105 	if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
12106 		port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
12107 	else
12108 		port = 0;
12109 #ifdef CONFIG_INET
12110 	/* allocate a socket and port for it and use it */
12111 	err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
12112 			    IPPROTO_TCP, &cfg->sock, 1);
12113 	if (err) {
12114 		kfree(cfg);
12115 		return err;
12116 	}
12117 	if (inet_csk_get_port(cfg->sock->sk, port)) {
12118 		sock_release(cfg->sock);
12119 		kfree(cfg);
12120 		return -EADDRINUSE;
12121 	}
12122 	cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
12123 #else
12124 	if (!port) {
12125 		kfree(cfg);
12126 		return -EINVAL;
12127 	}
12128 	cfg->src_port = port;
12129 #endif
12130 
12131 	cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
12132 	cfg->payload_len = data_size;
12133 	cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
12134 	memcpy((void *)cfg->payload,
12135 	       nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
12136 	       data_size);
12137 	if (seq)
12138 		cfg->payload_seq = *seq;
12139 	cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
12140 	cfg->wake_len = wake_size;
12141 	cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
12142 	memcpy((void *)cfg->wake_data,
12143 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
12144 	       wake_size);
12145 	cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
12146 			 data_size + wake_size;
12147 	memcpy((void *)cfg->wake_mask,
12148 	       nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
12149 	       wake_mask_size);
12150 	if (tok) {
12151 		cfg->tokens_size = tokens_size;
12152 		memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
12153 	}
12154 
12155 	trig->tcp = cfg;
12156 
12157 	return 0;
12158 }
12159 
nl80211_parse_wowlan_nd(struct cfg80211_registered_device * rdev,const struct wiphy_wowlan_support * wowlan,struct nlattr * attr,struct cfg80211_wowlan * trig)12160 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
12161 				   const struct wiphy_wowlan_support *wowlan,
12162 				   struct nlattr *attr,
12163 				   struct cfg80211_wowlan *trig)
12164 {
12165 	struct nlattr **tb;
12166 	int err;
12167 
12168 	tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
12169 	if (!tb)
12170 		return -ENOMEM;
12171 
12172 	if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
12173 		err = -EOPNOTSUPP;
12174 		goto out;
12175 	}
12176 
12177 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
12178 					  nl80211_policy, NULL);
12179 	if (err)
12180 		goto out;
12181 
12182 	trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
12183 						   wowlan->max_nd_match_sets);
12184 	err = PTR_ERR_OR_ZERO(trig->nd_config);
12185 	if (err)
12186 		trig->nd_config = NULL;
12187 
12188 out:
12189 	kfree(tb);
12190 	return err;
12191 }
12192 
nl80211_set_wowlan(struct sk_buff * skb,struct genl_info * info)12193 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
12194 {
12195 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12196 	struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
12197 	struct cfg80211_wowlan new_triggers = {};
12198 	struct cfg80211_wowlan *ntrig;
12199 	const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
12200 	int err, i;
12201 	bool prev_enabled = rdev->wiphy.wowlan_config;
12202 	bool regular = false;
12203 
12204 	if (!wowlan)
12205 		return -EOPNOTSUPP;
12206 
12207 	if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
12208 		cfg80211_rdev_free_wowlan(rdev);
12209 		rdev->wiphy.wowlan_config = NULL;
12210 		goto set_wakeup;
12211 	}
12212 
12213 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
12214 					  info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
12215 					  nl80211_wowlan_policy, info->extack);
12216 	if (err)
12217 		return err;
12218 
12219 	if (tb[NL80211_WOWLAN_TRIG_ANY]) {
12220 		if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
12221 			return -EINVAL;
12222 		new_triggers.any = true;
12223 	}
12224 
12225 	if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
12226 		if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
12227 			return -EINVAL;
12228 		new_triggers.disconnect = true;
12229 		regular = true;
12230 	}
12231 
12232 	if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
12233 		if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
12234 			return -EINVAL;
12235 		new_triggers.magic_pkt = true;
12236 		regular = true;
12237 	}
12238 
12239 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
12240 		return -EINVAL;
12241 
12242 	if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
12243 		if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
12244 			return -EINVAL;
12245 		new_triggers.gtk_rekey_failure = true;
12246 		regular = true;
12247 	}
12248 
12249 	if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
12250 		if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
12251 			return -EINVAL;
12252 		new_triggers.eap_identity_req = true;
12253 		regular = true;
12254 	}
12255 
12256 	if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
12257 		if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
12258 			return -EINVAL;
12259 		new_triggers.four_way_handshake = true;
12260 		regular = true;
12261 	}
12262 
12263 	if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
12264 		if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
12265 			return -EINVAL;
12266 		new_triggers.rfkill_release = true;
12267 		regular = true;
12268 	}
12269 
12270 	if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
12271 		struct nlattr *pat;
12272 		int n_patterns = 0;
12273 		int rem, pat_len, mask_len, pkt_offset;
12274 		struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
12275 
12276 		regular = true;
12277 
12278 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
12279 				    rem)
12280 			n_patterns++;
12281 		if (n_patterns > wowlan->n_patterns)
12282 			return -EINVAL;
12283 
12284 		new_triggers.patterns = kcalloc(n_patterns,
12285 						sizeof(new_triggers.patterns[0]),
12286 						GFP_KERNEL);
12287 		if (!new_triggers.patterns)
12288 			return -ENOMEM;
12289 
12290 		new_triggers.n_patterns = n_patterns;
12291 		i = 0;
12292 
12293 		nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
12294 				    rem) {
12295 			u8 *mask_pat;
12296 
12297 			err = nla_parse_nested_deprecated(pat_tb,
12298 							  MAX_NL80211_PKTPAT,
12299 							  pat,
12300 							  nl80211_packet_pattern_policy,
12301 							  info->extack);
12302 			if (err)
12303 				goto error;
12304 
12305 			err = -EINVAL;
12306 			if (!pat_tb[NL80211_PKTPAT_MASK] ||
12307 			    !pat_tb[NL80211_PKTPAT_PATTERN])
12308 				goto error;
12309 			pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
12310 			mask_len = DIV_ROUND_UP(pat_len, 8);
12311 			if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
12312 				goto error;
12313 			if (pat_len > wowlan->pattern_max_len ||
12314 			    pat_len < wowlan->pattern_min_len)
12315 				goto error;
12316 
12317 			if (!pat_tb[NL80211_PKTPAT_OFFSET])
12318 				pkt_offset = 0;
12319 			else
12320 				pkt_offset = nla_get_u32(
12321 					pat_tb[NL80211_PKTPAT_OFFSET]);
12322 			if (pkt_offset > wowlan->max_pkt_offset)
12323 				goto error;
12324 			new_triggers.patterns[i].pkt_offset = pkt_offset;
12325 
12326 			mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
12327 			if (!mask_pat) {
12328 				err = -ENOMEM;
12329 				goto error;
12330 			}
12331 			new_triggers.patterns[i].mask = mask_pat;
12332 			memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12333 			       mask_len);
12334 			mask_pat += mask_len;
12335 			new_triggers.patterns[i].pattern = mask_pat;
12336 			new_triggers.patterns[i].pattern_len = pat_len;
12337 			memcpy(mask_pat,
12338 			       nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12339 			       pat_len);
12340 			i++;
12341 		}
12342 	}
12343 
12344 	if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
12345 		regular = true;
12346 		err = nl80211_parse_wowlan_tcp(
12347 			rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
12348 			&new_triggers);
12349 		if (err)
12350 			goto error;
12351 	}
12352 
12353 	if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
12354 		regular = true;
12355 		err = nl80211_parse_wowlan_nd(
12356 			rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
12357 			&new_triggers);
12358 		if (err)
12359 			goto error;
12360 	}
12361 
12362 	/* The 'any' trigger means the device continues operating more or less
12363 	 * as in its normal operation mode and wakes up the host on most of the
12364 	 * normal interrupts (like packet RX, ...)
12365 	 * It therefore makes little sense to combine with the more constrained
12366 	 * wakeup trigger modes.
12367 	 */
12368 	if (new_triggers.any && regular) {
12369 		err = -EINVAL;
12370 		goto error;
12371 	}
12372 
12373 	ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
12374 	if (!ntrig) {
12375 		err = -ENOMEM;
12376 		goto error;
12377 	}
12378 	cfg80211_rdev_free_wowlan(rdev);
12379 	rdev->wiphy.wowlan_config = ntrig;
12380 
12381  set_wakeup:
12382 	if (rdev->ops->set_wakeup &&
12383 	    prev_enabled != !!rdev->wiphy.wowlan_config)
12384 		rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
12385 
12386 	return 0;
12387  error:
12388 	for (i = 0; i < new_triggers.n_patterns; i++)
12389 		kfree(new_triggers.patterns[i].mask);
12390 	kfree(new_triggers.patterns);
12391 	if (new_triggers.tcp && new_triggers.tcp->sock)
12392 		sock_release(new_triggers.tcp->sock);
12393 	kfree(new_triggers.tcp);
12394 	kfree(new_triggers.nd_config);
12395 	return err;
12396 }
12397 #endif
12398 
nl80211_send_coalesce_rules(struct sk_buff * msg,struct cfg80211_registered_device * rdev)12399 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
12400 				       struct cfg80211_registered_device *rdev)
12401 {
12402 	struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
12403 	int i, j, pat_len;
12404 	struct cfg80211_coalesce_rules *rule;
12405 
12406 	if (!rdev->coalesce->n_rules)
12407 		return 0;
12408 
12409 	nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
12410 	if (!nl_rules)
12411 		return -ENOBUFS;
12412 
12413 	for (i = 0; i < rdev->coalesce->n_rules; i++) {
12414 		nl_rule = nla_nest_start_noflag(msg, i + 1);
12415 		if (!nl_rule)
12416 			return -ENOBUFS;
12417 
12418 		rule = &rdev->coalesce->rules[i];
12419 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
12420 				rule->delay))
12421 			return -ENOBUFS;
12422 
12423 		if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
12424 				rule->condition))
12425 			return -ENOBUFS;
12426 
12427 		nl_pats = nla_nest_start_noflag(msg,
12428 						NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
12429 		if (!nl_pats)
12430 			return -ENOBUFS;
12431 
12432 		for (j = 0; j < rule->n_patterns; j++) {
12433 			nl_pat = nla_nest_start_noflag(msg, j + 1);
12434 			if (!nl_pat)
12435 				return -ENOBUFS;
12436 			pat_len = rule->patterns[j].pattern_len;
12437 			if (nla_put(msg, NL80211_PKTPAT_MASK,
12438 				    DIV_ROUND_UP(pat_len, 8),
12439 				    rule->patterns[j].mask) ||
12440 			    nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12441 				    rule->patterns[j].pattern) ||
12442 			    nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12443 					rule->patterns[j].pkt_offset))
12444 				return -ENOBUFS;
12445 			nla_nest_end(msg, nl_pat);
12446 		}
12447 		nla_nest_end(msg, nl_pats);
12448 		nla_nest_end(msg, nl_rule);
12449 	}
12450 	nla_nest_end(msg, nl_rules);
12451 
12452 	return 0;
12453 }
12454 
nl80211_get_coalesce(struct sk_buff * skb,struct genl_info * info)12455 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
12456 {
12457 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12458 	struct sk_buff *msg;
12459 	void *hdr;
12460 
12461 	if (!rdev->wiphy.coalesce)
12462 		return -EOPNOTSUPP;
12463 
12464 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12465 	if (!msg)
12466 		return -ENOMEM;
12467 
12468 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12469 			     NL80211_CMD_GET_COALESCE);
12470 	if (!hdr)
12471 		goto nla_put_failure;
12472 
12473 	if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
12474 		goto nla_put_failure;
12475 
12476 	genlmsg_end(msg, hdr);
12477 	return genlmsg_reply(msg, info);
12478 
12479 nla_put_failure:
12480 	nlmsg_free(msg);
12481 	return -ENOBUFS;
12482 }
12483 
cfg80211_rdev_free_coalesce(struct cfg80211_registered_device * rdev)12484 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
12485 {
12486 	struct cfg80211_coalesce *coalesce = rdev->coalesce;
12487 	int i, j;
12488 	struct cfg80211_coalesce_rules *rule;
12489 
12490 	if (!coalesce)
12491 		return;
12492 
12493 	for (i = 0; i < coalesce->n_rules; i++) {
12494 		rule = &coalesce->rules[i];
12495 		for (j = 0; j < rule->n_patterns; j++)
12496 			kfree(rule->patterns[j].mask);
12497 		kfree(rule->patterns);
12498 	}
12499 	kfree(coalesce->rules);
12500 	kfree(coalesce);
12501 	rdev->coalesce = NULL;
12502 }
12503 
nl80211_parse_coalesce_rule(struct cfg80211_registered_device * rdev,struct nlattr * rule,struct cfg80211_coalesce_rules * new_rule)12504 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
12505 				       struct nlattr *rule,
12506 				       struct cfg80211_coalesce_rules *new_rule)
12507 {
12508 	int err, i;
12509 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12510 	struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
12511 	int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
12512 	struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
12513 
12514 	err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
12515 					  rule, nl80211_coalesce_policy, NULL);
12516 	if (err)
12517 		return err;
12518 
12519 	if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
12520 		new_rule->delay =
12521 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
12522 	if (new_rule->delay > coalesce->max_delay)
12523 		return -EINVAL;
12524 
12525 	if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
12526 		new_rule->condition =
12527 			nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
12528 
12529 	if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
12530 		return -EINVAL;
12531 
12532 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12533 			    rem)
12534 		n_patterns++;
12535 	if (n_patterns > coalesce->n_patterns)
12536 		return -EINVAL;
12537 
12538 	new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
12539 				     GFP_KERNEL);
12540 	if (!new_rule->patterns)
12541 		return -ENOMEM;
12542 
12543 	new_rule->n_patterns = n_patterns;
12544 	i = 0;
12545 
12546 	nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
12547 			    rem) {
12548 		u8 *mask_pat;
12549 
12550 		err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
12551 						  pat,
12552 						  nl80211_packet_pattern_policy,
12553 						  NULL);
12554 		if (err)
12555 			return err;
12556 
12557 		if (!pat_tb[NL80211_PKTPAT_MASK] ||
12558 		    !pat_tb[NL80211_PKTPAT_PATTERN])
12559 			return -EINVAL;
12560 		pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
12561 		mask_len = DIV_ROUND_UP(pat_len, 8);
12562 		if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
12563 			return -EINVAL;
12564 		if (pat_len > coalesce->pattern_max_len ||
12565 		    pat_len < coalesce->pattern_min_len)
12566 			return -EINVAL;
12567 
12568 		if (!pat_tb[NL80211_PKTPAT_OFFSET])
12569 			pkt_offset = 0;
12570 		else
12571 			pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
12572 		if (pkt_offset > coalesce->max_pkt_offset)
12573 			return -EINVAL;
12574 		new_rule->patterns[i].pkt_offset = pkt_offset;
12575 
12576 		mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
12577 		if (!mask_pat)
12578 			return -ENOMEM;
12579 
12580 		new_rule->patterns[i].mask = mask_pat;
12581 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
12582 		       mask_len);
12583 
12584 		mask_pat += mask_len;
12585 		new_rule->patterns[i].pattern = mask_pat;
12586 		new_rule->patterns[i].pattern_len = pat_len;
12587 		memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
12588 		       pat_len);
12589 		i++;
12590 	}
12591 
12592 	return 0;
12593 }
12594 
nl80211_set_coalesce(struct sk_buff * skb,struct genl_info * info)12595 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
12596 {
12597 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12598 	const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
12599 	struct cfg80211_coalesce new_coalesce = {};
12600 	struct cfg80211_coalesce *n_coalesce;
12601 	int err, rem_rule, n_rules = 0, i, j;
12602 	struct nlattr *rule;
12603 	struct cfg80211_coalesce_rules *tmp_rule;
12604 
12605 	if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
12606 		return -EOPNOTSUPP;
12607 
12608 	if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
12609 		cfg80211_rdev_free_coalesce(rdev);
12610 		rdev_set_coalesce(rdev, NULL);
12611 		return 0;
12612 	}
12613 
12614 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12615 			    rem_rule)
12616 		n_rules++;
12617 	if (n_rules > coalesce->n_rules)
12618 		return -EINVAL;
12619 
12620 	new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
12621 				     GFP_KERNEL);
12622 	if (!new_coalesce.rules)
12623 		return -ENOMEM;
12624 
12625 	new_coalesce.n_rules = n_rules;
12626 	i = 0;
12627 
12628 	nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
12629 			    rem_rule) {
12630 		err = nl80211_parse_coalesce_rule(rdev, rule,
12631 						  &new_coalesce.rules[i]);
12632 		if (err)
12633 			goto error;
12634 
12635 		i++;
12636 	}
12637 
12638 	err = rdev_set_coalesce(rdev, &new_coalesce);
12639 	if (err)
12640 		goto error;
12641 
12642 	n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
12643 	if (!n_coalesce) {
12644 		err = -ENOMEM;
12645 		goto error;
12646 	}
12647 	cfg80211_rdev_free_coalesce(rdev);
12648 	rdev->coalesce = n_coalesce;
12649 
12650 	return 0;
12651 error:
12652 	for (i = 0; i < new_coalesce.n_rules; i++) {
12653 		tmp_rule = &new_coalesce.rules[i];
12654 		for (j = 0; j < tmp_rule->n_patterns; j++)
12655 			kfree(tmp_rule->patterns[j].mask);
12656 		kfree(tmp_rule->patterns);
12657 	}
12658 	kfree(new_coalesce.rules);
12659 
12660 	return err;
12661 }
12662 
nl80211_set_rekey_data(struct sk_buff * skb,struct genl_info * info)12663 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12664 {
12665 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12666 	struct net_device *dev = info->user_ptr[1];
12667 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12668 	struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12669 	struct cfg80211_gtk_rekey_data rekey_data = {};
12670 	int err;
12671 
12672 	if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12673 		return -EINVAL;
12674 
12675 	err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12676 					  info->attrs[NL80211_ATTR_REKEY_DATA],
12677 					  nl80211_rekey_policy, info->extack);
12678 	if (err)
12679 		return err;
12680 
12681 	if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12682 	    !tb[NL80211_REKEY_DATA_KCK])
12683 		return -EINVAL;
12684 	if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
12685 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12686 	      nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
12687 		return -ERANGE;
12688 	if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
12689 	    !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
12690 	      nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN))
12691 		return -ERANGE;
12692 
12693 	rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12694 	rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12695 	rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12696 	rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
12697 	rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
12698 	if (tb[NL80211_REKEY_DATA_AKM])
12699 		rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
12700 
12701 	wdev_lock(wdev);
12702 	if (!wdev->current_bss) {
12703 		err = -ENOTCONN;
12704 		goto out;
12705 	}
12706 
12707 	if (!rdev->ops->set_rekey_data) {
12708 		err = -EOPNOTSUPP;
12709 		goto out;
12710 	}
12711 
12712 	err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12713  out:
12714 	wdev_unlock(wdev);
12715 	return err;
12716 }
12717 
nl80211_register_unexpected_frame(struct sk_buff * skb,struct genl_info * info)12718 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12719 					     struct genl_info *info)
12720 {
12721 	struct net_device *dev = info->user_ptr[1];
12722 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12723 
12724 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12725 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12726 		return -EINVAL;
12727 
12728 	if (wdev->ap_unexpected_nlportid)
12729 		return -EBUSY;
12730 
12731 	wdev->ap_unexpected_nlportid = info->snd_portid;
12732 	return 0;
12733 }
12734 
nl80211_probe_client(struct sk_buff * skb,struct genl_info * info)12735 static int nl80211_probe_client(struct sk_buff *skb,
12736 				struct genl_info *info)
12737 {
12738 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12739 	struct net_device *dev = info->user_ptr[1];
12740 	struct wireless_dev *wdev = dev->ieee80211_ptr;
12741 	struct sk_buff *msg;
12742 	void *hdr;
12743 	const u8 *addr;
12744 	u64 cookie;
12745 	int err;
12746 
12747 	if (wdev->iftype != NL80211_IFTYPE_AP &&
12748 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
12749 		return -EOPNOTSUPP;
12750 
12751 	if (!info->attrs[NL80211_ATTR_MAC])
12752 		return -EINVAL;
12753 
12754 	if (!rdev->ops->probe_client)
12755 		return -EOPNOTSUPP;
12756 
12757 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12758 	if (!msg)
12759 		return -ENOMEM;
12760 
12761 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12762 			     NL80211_CMD_PROBE_CLIENT);
12763 	if (!hdr) {
12764 		err = -ENOBUFS;
12765 		goto free_msg;
12766 	}
12767 
12768 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12769 
12770 	err = rdev_probe_client(rdev, dev, addr, &cookie);
12771 	if (err)
12772 		goto free_msg;
12773 
12774 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12775 			      NL80211_ATTR_PAD))
12776 		goto nla_put_failure;
12777 
12778 	genlmsg_end(msg, hdr);
12779 
12780 	return genlmsg_reply(msg, info);
12781 
12782  nla_put_failure:
12783 	err = -ENOBUFS;
12784  free_msg:
12785 	nlmsg_free(msg);
12786 	return err;
12787 }
12788 
nl80211_register_beacons(struct sk_buff * skb,struct genl_info * info)12789 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12790 {
12791 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12792 	struct cfg80211_beacon_registration *reg, *nreg;
12793 	int rv;
12794 
12795 	if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12796 		return -EOPNOTSUPP;
12797 
12798 	nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12799 	if (!nreg)
12800 		return -ENOMEM;
12801 
12802 	/* First, check if already registered. */
12803 	spin_lock_bh(&rdev->beacon_registrations_lock);
12804 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12805 		if (reg->nlportid == info->snd_portid) {
12806 			rv = -EALREADY;
12807 			goto out_err;
12808 		}
12809 	}
12810 	/* Add it to the list */
12811 	nreg->nlportid = info->snd_portid;
12812 	list_add(&nreg->list, &rdev->beacon_registrations);
12813 
12814 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12815 
12816 	return 0;
12817 out_err:
12818 	spin_unlock_bh(&rdev->beacon_registrations_lock);
12819 	kfree(nreg);
12820 	return rv;
12821 }
12822 
nl80211_start_p2p_device(struct sk_buff * skb,struct genl_info * info)12823 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12824 {
12825 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12826 	struct wireless_dev *wdev = info->user_ptr[1];
12827 	int err;
12828 
12829 	if (!rdev->ops->start_p2p_device)
12830 		return -EOPNOTSUPP;
12831 
12832 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12833 		return -EOPNOTSUPP;
12834 
12835 	if (wdev_running(wdev))
12836 		return 0;
12837 
12838 	if (rfkill_blocked(rdev->rfkill))
12839 		return -ERFKILL;
12840 
12841 	err = rdev_start_p2p_device(rdev, wdev);
12842 	if (err)
12843 		return err;
12844 
12845 	wdev->is_running = true;
12846 	rdev->opencount++;
12847 
12848 	return 0;
12849 }
12850 
nl80211_stop_p2p_device(struct sk_buff * skb,struct genl_info * info)12851 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12852 {
12853 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12854 	struct wireless_dev *wdev = info->user_ptr[1];
12855 
12856 	if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12857 		return -EOPNOTSUPP;
12858 
12859 	if (!rdev->ops->stop_p2p_device)
12860 		return -EOPNOTSUPP;
12861 
12862 	cfg80211_stop_p2p_device(rdev, wdev);
12863 
12864 	return 0;
12865 }
12866 
nl80211_start_nan(struct sk_buff * skb,struct genl_info * info)12867 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12868 {
12869 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12870 	struct wireless_dev *wdev = info->user_ptr[1];
12871 	struct cfg80211_nan_conf conf = {};
12872 	int err;
12873 
12874 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12875 		return -EOPNOTSUPP;
12876 
12877 	if (wdev_running(wdev))
12878 		return -EEXIST;
12879 
12880 	if (rfkill_blocked(rdev->rfkill))
12881 		return -ERFKILL;
12882 
12883 	if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12884 		return -EINVAL;
12885 
12886 	conf.master_pref =
12887 		nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12888 
12889 	if (info->attrs[NL80211_ATTR_BANDS]) {
12890 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12891 
12892 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12893 			return -EOPNOTSUPP;
12894 
12895 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12896 			return -EINVAL;
12897 
12898 		conf.bands = bands;
12899 	}
12900 
12901 	err = rdev_start_nan(rdev, wdev, &conf);
12902 	if (err)
12903 		return err;
12904 
12905 	wdev->is_running = true;
12906 	rdev->opencount++;
12907 
12908 	return 0;
12909 }
12910 
nl80211_stop_nan(struct sk_buff * skb,struct genl_info * info)12911 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12912 {
12913 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12914 	struct wireless_dev *wdev = info->user_ptr[1];
12915 
12916 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12917 		return -EOPNOTSUPP;
12918 
12919 	cfg80211_stop_nan(rdev, wdev);
12920 
12921 	return 0;
12922 }
12923 
validate_nan_filter(struct nlattr * filter_attr)12924 static int validate_nan_filter(struct nlattr *filter_attr)
12925 {
12926 	struct nlattr *attr;
12927 	int len = 0, n_entries = 0, rem;
12928 
12929 	nla_for_each_nested(attr, filter_attr, rem) {
12930 		len += nla_len(attr);
12931 		n_entries++;
12932 	}
12933 
12934 	if (len >= U8_MAX)
12935 		return -EINVAL;
12936 
12937 	return n_entries;
12938 }
12939 
handle_nan_filter(struct nlattr * attr_filter,struct cfg80211_nan_func * func,bool tx)12940 static int handle_nan_filter(struct nlattr *attr_filter,
12941 			     struct cfg80211_nan_func *func,
12942 			     bool tx)
12943 {
12944 	struct nlattr *attr;
12945 	int n_entries, rem, i;
12946 	struct cfg80211_nan_func_filter *filter;
12947 
12948 	n_entries = validate_nan_filter(attr_filter);
12949 	if (n_entries < 0)
12950 		return n_entries;
12951 
12952 	BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12953 
12954 	filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12955 	if (!filter)
12956 		return -ENOMEM;
12957 
12958 	i = 0;
12959 	nla_for_each_nested(attr, attr_filter, rem) {
12960 		filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12961 		if (!filter[i].filter)
12962 			goto err;
12963 
12964 		filter[i].len = nla_len(attr);
12965 		i++;
12966 	}
12967 	if (tx) {
12968 		func->num_tx_filters = n_entries;
12969 		func->tx_filters = filter;
12970 	} else {
12971 		func->num_rx_filters = n_entries;
12972 		func->rx_filters = filter;
12973 	}
12974 
12975 	return 0;
12976 
12977 err:
12978 	i = 0;
12979 	nla_for_each_nested(attr, attr_filter, rem) {
12980 		kfree(filter[i].filter);
12981 		i++;
12982 	}
12983 	kfree(filter);
12984 	return -ENOMEM;
12985 }
12986 
nl80211_nan_add_func(struct sk_buff * skb,struct genl_info * info)12987 static int nl80211_nan_add_func(struct sk_buff *skb,
12988 				struct genl_info *info)
12989 {
12990 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
12991 	struct wireless_dev *wdev = info->user_ptr[1];
12992 	struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12993 	struct cfg80211_nan_func *func;
12994 	struct sk_buff *msg = NULL;
12995 	void *hdr = NULL;
12996 	int err = 0;
12997 
12998 	if (wdev->iftype != NL80211_IFTYPE_NAN)
12999 		return -EOPNOTSUPP;
13000 
13001 	if (!wdev_running(wdev))
13002 		return -ENOTCONN;
13003 
13004 	if (!info->attrs[NL80211_ATTR_NAN_FUNC])
13005 		return -EINVAL;
13006 
13007 	err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
13008 					  info->attrs[NL80211_ATTR_NAN_FUNC],
13009 					  nl80211_nan_func_policy,
13010 					  info->extack);
13011 	if (err)
13012 		return err;
13013 
13014 	func = kzalloc(sizeof(*func), GFP_KERNEL);
13015 	if (!func)
13016 		return -ENOMEM;
13017 
13018 	func->cookie = cfg80211_assign_cookie(rdev);
13019 
13020 	if (!tb[NL80211_NAN_FUNC_TYPE]) {
13021 		err = -EINVAL;
13022 		goto out;
13023 	}
13024 
13025 
13026 	func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
13027 
13028 	if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
13029 		err = -EINVAL;
13030 		goto out;
13031 	}
13032 
13033 	memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
13034 	       sizeof(func->service_id));
13035 
13036 	func->close_range =
13037 		nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
13038 
13039 	if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
13040 		func->serv_spec_info_len =
13041 			nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
13042 		func->serv_spec_info =
13043 			kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
13044 				func->serv_spec_info_len,
13045 				GFP_KERNEL);
13046 		if (!func->serv_spec_info) {
13047 			err = -ENOMEM;
13048 			goto out;
13049 		}
13050 	}
13051 
13052 	if (tb[NL80211_NAN_FUNC_TTL])
13053 		func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
13054 
13055 	switch (func->type) {
13056 	case NL80211_NAN_FUNC_PUBLISH:
13057 		if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
13058 			err = -EINVAL;
13059 			goto out;
13060 		}
13061 
13062 		func->publish_type =
13063 			nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
13064 		func->publish_bcast =
13065 			nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
13066 
13067 		if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
13068 			func->publish_bcast) {
13069 			err = -EINVAL;
13070 			goto out;
13071 		}
13072 		break;
13073 	case NL80211_NAN_FUNC_SUBSCRIBE:
13074 		func->subscribe_active =
13075 			nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
13076 		break;
13077 	case NL80211_NAN_FUNC_FOLLOW_UP:
13078 		if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
13079 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
13080 		    !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
13081 			err = -EINVAL;
13082 			goto out;
13083 		}
13084 
13085 		func->followup_id =
13086 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
13087 		func->followup_reqid =
13088 			nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
13089 		memcpy(func->followup_dest.addr,
13090 		       nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
13091 		       sizeof(func->followup_dest.addr));
13092 		if (func->ttl) {
13093 			err = -EINVAL;
13094 			goto out;
13095 		}
13096 		break;
13097 	default:
13098 		err = -EINVAL;
13099 		goto out;
13100 	}
13101 
13102 	if (tb[NL80211_NAN_FUNC_SRF]) {
13103 		struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
13104 
13105 		err = nla_parse_nested_deprecated(srf_tb,
13106 						  NL80211_NAN_SRF_ATTR_MAX,
13107 						  tb[NL80211_NAN_FUNC_SRF],
13108 						  nl80211_nan_srf_policy,
13109 						  info->extack);
13110 		if (err)
13111 			goto out;
13112 
13113 		func->srf_include =
13114 			nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
13115 
13116 		if (srf_tb[NL80211_NAN_SRF_BF]) {
13117 			if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
13118 			    !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
13119 				err = -EINVAL;
13120 				goto out;
13121 			}
13122 
13123 			func->srf_bf_len =
13124 				nla_len(srf_tb[NL80211_NAN_SRF_BF]);
13125 			func->srf_bf =
13126 				kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
13127 					func->srf_bf_len, GFP_KERNEL);
13128 			if (!func->srf_bf) {
13129 				err = -ENOMEM;
13130 				goto out;
13131 			}
13132 
13133 			func->srf_bf_idx =
13134 				nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
13135 		} else {
13136 			struct nlattr *attr, *mac_attr =
13137 				srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
13138 			int n_entries, rem, i = 0;
13139 
13140 			if (!mac_attr) {
13141 				err = -EINVAL;
13142 				goto out;
13143 			}
13144 
13145 			n_entries = validate_acl_mac_addrs(mac_attr);
13146 			if (n_entries <= 0) {
13147 				err = -EINVAL;
13148 				goto out;
13149 			}
13150 
13151 			func->srf_num_macs = n_entries;
13152 			func->srf_macs =
13153 				kcalloc(n_entries, sizeof(*func->srf_macs),
13154 					GFP_KERNEL);
13155 			if (!func->srf_macs) {
13156 				err = -ENOMEM;
13157 				goto out;
13158 			}
13159 
13160 			nla_for_each_nested(attr, mac_attr, rem)
13161 				memcpy(func->srf_macs[i++].addr, nla_data(attr),
13162 				       sizeof(*func->srf_macs));
13163 		}
13164 	}
13165 
13166 	if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
13167 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
13168 					func, true);
13169 		if (err)
13170 			goto out;
13171 	}
13172 
13173 	if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
13174 		err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
13175 					func, false);
13176 		if (err)
13177 			goto out;
13178 	}
13179 
13180 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13181 	if (!msg) {
13182 		err = -ENOMEM;
13183 		goto out;
13184 	}
13185 
13186 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13187 			     NL80211_CMD_ADD_NAN_FUNCTION);
13188 	/* This can't really happen - we just allocated 4KB */
13189 	if (WARN_ON(!hdr)) {
13190 		err = -ENOMEM;
13191 		goto out;
13192 	}
13193 
13194 	err = rdev_add_nan_func(rdev, wdev, func);
13195 out:
13196 	if (err < 0) {
13197 		cfg80211_free_nan_func(func);
13198 		nlmsg_free(msg);
13199 		return err;
13200 	}
13201 
13202 	/* propagate the instance id and cookie to userspace  */
13203 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
13204 			      NL80211_ATTR_PAD))
13205 		goto nla_put_failure;
13206 
13207 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
13208 	if (!func_attr)
13209 		goto nla_put_failure;
13210 
13211 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
13212 		       func->instance_id))
13213 		goto nla_put_failure;
13214 
13215 	nla_nest_end(msg, func_attr);
13216 
13217 	genlmsg_end(msg, hdr);
13218 	return genlmsg_reply(msg, info);
13219 
13220 nla_put_failure:
13221 	nlmsg_free(msg);
13222 	return -ENOBUFS;
13223 }
13224 
nl80211_nan_del_func(struct sk_buff * skb,struct genl_info * info)13225 static int nl80211_nan_del_func(struct sk_buff *skb,
13226 			       struct genl_info *info)
13227 {
13228 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13229 	struct wireless_dev *wdev = info->user_ptr[1];
13230 	u64 cookie;
13231 
13232 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13233 		return -EOPNOTSUPP;
13234 
13235 	if (!wdev_running(wdev))
13236 		return -ENOTCONN;
13237 
13238 	if (!info->attrs[NL80211_ATTR_COOKIE])
13239 		return -EINVAL;
13240 
13241 	cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
13242 
13243 	rdev_del_nan_func(rdev, wdev, cookie);
13244 
13245 	return 0;
13246 }
13247 
nl80211_nan_change_config(struct sk_buff * skb,struct genl_info * info)13248 static int nl80211_nan_change_config(struct sk_buff *skb,
13249 				     struct genl_info *info)
13250 {
13251 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13252 	struct wireless_dev *wdev = info->user_ptr[1];
13253 	struct cfg80211_nan_conf conf = {};
13254 	u32 changed = 0;
13255 
13256 	if (wdev->iftype != NL80211_IFTYPE_NAN)
13257 		return -EOPNOTSUPP;
13258 
13259 	if (!wdev_running(wdev))
13260 		return -ENOTCONN;
13261 
13262 	if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
13263 		conf.master_pref =
13264 			nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
13265 		if (conf.master_pref <= 1 || conf.master_pref == 255)
13266 			return -EINVAL;
13267 
13268 		changed |= CFG80211_NAN_CONF_CHANGED_PREF;
13269 	}
13270 
13271 	if (info->attrs[NL80211_ATTR_BANDS]) {
13272 		u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
13273 
13274 		if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
13275 			return -EOPNOTSUPP;
13276 
13277 		if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
13278 			return -EINVAL;
13279 
13280 		conf.bands = bands;
13281 		changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
13282 	}
13283 
13284 	if (!changed)
13285 		return -EINVAL;
13286 
13287 	return rdev_nan_change_conf(rdev, wdev, &conf, changed);
13288 }
13289 
cfg80211_nan_match(struct wireless_dev * wdev,struct cfg80211_nan_match_params * match,gfp_t gfp)13290 void cfg80211_nan_match(struct wireless_dev *wdev,
13291 			struct cfg80211_nan_match_params *match, gfp_t gfp)
13292 {
13293 	struct wiphy *wiphy = wdev->wiphy;
13294 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13295 	struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
13296 	struct sk_buff *msg;
13297 	void *hdr;
13298 
13299 	if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
13300 		return;
13301 
13302 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13303 	if (!msg)
13304 		return;
13305 
13306 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
13307 	if (!hdr) {
13308 		nlmsg_free(msg);
13309 		return;
13310 	}
13311 
13312 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13313 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13314 					 wdev->netdev->ifindex)) ||
13315 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13316 			      NL80211_ATTR_PAD))
13317 		goto nla_put_failure;
13318 
13319 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
13320 			      NL80211_ATTR_PAD) ||
13321 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
13322 		goto nla_put_failure;
13323 
13324 	match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
13325 	if (!match_attr)
13326 		goto nla_put_failure;
13327 
13328 	local_func_attr = nla_nest_start_noflag(msg,
13329 						NL80211_NAN_MATCH_FUNC_LOCAL);
13330 	if (!local_func_attr)
13331 		goto nla_put_failure;
13332 
13333 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
13334 		goto nla_put_failure;
13335 
13336 	nla_nest_end(msg, local_func_attr);
13337 
13338 	peer_func_attr = nla_nest_start_noflag(msg,
13339 					       NL80211_NAN_MATCH_FUNC_PEER);
13340 	if (!peer_func_attr)
13341 		goto nla_put_failure;
13342 
13343 	if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
13344 	    nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
13345 		goto nla_put_failure;
13346 
13347 	if (match->info && match->info_len &&
13348 	    nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
13349 		    match->info))
13350 		goto nla_put_failure;
13351 
13352 	nla_nest_end(msg, peer_func_attr);
13353 	nla_nest_end(msg, match_attr);
13354 	genlmsg_end(msg, hdr);
13355 
13356 	if (!wdev->owner_nlportid)
13357 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13358 					msg, 0, NL80211_MCGRP_NAN, gfp);
13359 	else
13360 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13361 				wdev->owner_nlportid);
13362 
13363 	return;
13364 
13365 nla_put_failure:
13366 	nlmsg_free(msg);
13367 }
13368 EXPORT_SYMBOL(cfg80211_nan_match);
13369 
cfg80211_nan_func_terminated(struct wireless_dev * wdev,u8 inst_id,enum nl80211_nan_func_term_reason reason,u64 cookie,gfp_t gfp)13370 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
13371 				  u8 inst_id,
13372 				  enum nl80211_nan_func_term_reason reason,
13373 				  u64 cookie, gfp_t gfp)
13374 {
13375 	struct wiphy *wiphy = wdev->wiphy;
13376 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13377 	struct sk_buff *msg;
13378 	struct nlattr *func_attr;
13379 	void *hdr;
13380 
13381 	if (WARN_ON(!inst_id))
13382 		return;
13383 
13384 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13385 	if (!msg)
13386 		return;
13387 
13388 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
13389 	if (!hdr) {
13390 		nlmsg_free(msg);
13391 		return;
13392 	}
13393 
13394 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13395 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13396 					 wdev->netdev->ifindex)) ||
13397 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13398 			      NL80211_ATTR_PAD))
13399 		goto nla_put_failure;
13400 
13401 	if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13402 			      NL80211_ATTR_PAD))
13403 		goto nla_put_failure;
13404 
13405 	func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
13406 	if (!func_attr)
13407 		goto nla_put_failure;
13408 
13409 	if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
13410 	    nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
13411 		goto nla_put_failure;
13412 
13413 	nla_nest_end(msg, func_attr);
13414 	genlmsg_end(msg, hdr);
13415 
13416 	if (!wdev->owner_nlportid)
13417 		genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
13418 					msg, 0, NL80211_MCGRP_NAN, gfp);
13419 	else
13420 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
13421 				wdev->owner_nlportid);
13422 
13423 	return;
13424 
13425 nla_put_failure:
13426 	nlmsg_free(msg);
13427 }
13428 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
13429 
nl80211_get_protocol_features(struct sk_buff * skb,struct genl_info * info)13430 static int nl80211_get_protocol_features(struct sk_buff *skb,
13431 					 struct genl_info *info)
13432 {
13433 	void *hdr;
13434 	struct sk_buff *msg;
13435 
13436 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13437 	if (!msg)
13438 		return -ENOMEM;
13439 
13440 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13441 			     NL80211_CMD_GET_PROTOCOL_FEATURES);
13442 	if (!hdr)
13443 		goto nla_put_failure;
13444 
13445 	if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
13446 			NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
13447 		goto nla_put_failure;
13448 
13449 	genlmsg_end(msg, hdr);
13450 	return genlmsg_reply(msg, info);
13451 
13452  nla_put_failure:
13453 	kfree_skb(msg);
13454 	return -ENOBUFS;
13455 }
13456 
nl80211_update_ft_ies(struct sk_buff * skb,struct genl_info * info)13457 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
13458 {
13459 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13460 	struct cfg80211_update_ft_ies_params ft_params;
13461 	struct net_device *dev = info->user_ptr[1];
13462 
13463 	if (!rdev->ops->update_ft_ies)
13464 		return -EOPNOTSUPP;
13465 
13466 	if (!info->attrs[NL80211_ATTR_MDID] ||
13467 	    !info->attrs[NL80211_ATTR_IE])
13468 		return -EINVAL;
13469 
13470 	memset(&ft_params, 0, sizeof(ft_params));
13471 	ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
13472 	ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13473 	ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13474 
13475 	return rdev_update_ft_ies(rdev, dev, &ft_params);
13476 }
13477 
nl80211_crit_protocol_start(struct sk_buff * skb,struct genl_info * info)13478 static int nl80211_crit_protocol_start(struct sk_buff *skb,
13479 				       struct genl_info *info)
13480 {
13481 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13482 	struct wireless_dev *wdev = info->user_ptr[1];
13483 	enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
13484 	u16 duration;
13485 	int ret;
13486 
13487 	if (!rdev->ops->crit_proto_start)
13488 		return -EOPNOTSUPP;
13489 
13490 	if (WARN_ON(!rdev->ops->crit_proto_stop))
13491 		return -EINVAL;
13492 
13493 	if (rdev->crit_proto_nlportid)
13494 		return -EBUSY;
13495 
13496 	/* determine protocol if provided */
13497 	if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
13498 		proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
13499 
13500 	if (proto >= NUM_NL80211_CRIT_PROTO)
13501 		return -EINVAL;
13502 
13503 	/* timeout must be provided */
13504 	if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
13505 		return -EINVAL;
13506 
13507 	duration =
13508 		nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
13509 
13510 	ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
13511 	if (!ret)
13512 		rdev->crit_proto_nlportid = info->snd_portid;
13513 
13514 	return ret;
13515 }
13516 
nl80211_crit_protocol_stop(struct sk_buff * skb,struct genl_info * info)13517 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
13518 				      struct genl_info *info)
13519 {
13520 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13521 	struct wireless_dev *wdev = info->user_ptr[1];
13522 
13523 	if (!rdev->ops->crit_proto_stop)
13524 		return -EOPNOTSUPP;
13525 
13526 	if (rdev->crit_proto_nlportid) {
13527 		rdev->crit_proto_nlportid = 0;
13528 		rdev_crit_proto_stop(rdev, wdev);
13529 	}
13530 	return 0;
13531 }
13532 
nl80211_vendor_check_policy(const struct wiphy_vendor_command * vcmd,struct nlattr * attr,struct netlink_ext_ack * extack)13533 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
13534 				       struct nlattr *attr,
13535 				       struct netlink_ext_ack *extack)
13536 {
13537 	if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
13538 		if (attr->nla_type & NLA_F_NESTED) {
13539 			NL_SET_ERR_MSG_ATTR(extack, attr,
13540 					    "unexpected nested data");
13541 			return -EINVAL;
13542 		}
13543 
13544 		return 0;
13545 	}
13546 
13547 	if (!(attr->nla_type & NLA_F_NESTED)) {
13548 		NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
13549 		return -EINVAL;
13550 	}
13551 
13552 	return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
13553 }
13554 
nl80211_vendor_cmd(struct sk_buff * skb,struct genl_info * info)13555 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
13556 {
13557 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13558 	struct wireless_dev *wdev =
13559 		__cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
13560 	int i, err;
13561 	u32 vid, subcmd;
13562 
13563 	if (!rdev->wiphy.vendor_commands)
13564 		return -EOPNOTSUPP;
13565 
13566 	if (IS_ERR(wdev)) {
13567 		err = PTR_ERR(wdev);
13568 		if (err != -EINVAL)
13569 			return err;
13570 		wdev = NULL;
13571 	} else if (wdev->wiphy != &rdev->wiphy) {
13572 		return -EINVAL;
13573 	}
13574 
13575 	if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
13576 	    !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
13577 		return -EINVAL;
13578 
13579 	vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
13580 	subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
13581 	for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
13582 		const struct wiphy_vendor_command *vcmd;
13583 		void *data = NULL;
13584 		int len = 0;
13585 
13586 		vcmd = &rdev->wiphy.vendor_commands[i];
13587 
13588 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13589 			continue;
13590 
13591 		if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13592 				   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13593 			if (!wdev)
13594 				return -EINVAL;
13595 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13596 			    !wdev->netdev)
13597 				return -EINVAL;
13598 
13599 			if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13600 				if (!wdev_running(wdev))
13601 					return -ENETDOWN;
13602 			}
13603 		} else {
13604 			wdev = NULL;
13605 		}
13606 
13607 		if (!vcmd->doit)
13608 			return -EOPNOTSUPP;
13609 
13610 		if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
13611 			data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13612 			len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
13613 
13614 			err = nl80211_vendor_check_policy(vcmd,
13615 					info->attrs[NL80211_ATTR_VENDOR_DATA],
13616 					info->extack);
13617 			if (err)
13618 				return err;
13619 		}
13620 
13621 		rdev->cur_cmd_info = info;
13622 		err = vcmd->doit(&rdev->wiphy, wdev, data, len);
13623 		rdev->cur_cmd_info = NULL;
13624 		return err;
13625 	}
13626 
13627 	return -EOPNOTSUPP;
13628 }
13629 
nl80211_prepare_vendor_dump(struct sk_buff * skb,struct netlink_callback * cb,struct cfg80211_registered_device ** rdev,struct wireless_dev ** wdev)13630 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
13631 				       struct netlink_callback *cb,
13632 				       struct cfg80211_registered_device **rdev,
13633 				       struct wireless_dev **wdev)
13634 {
13635 	struct nlattr **attrbuf;
13636 	u32 vid, subcmd;
13637 	unsigned int i;
13638 	int vcmd_idx = -1;
13639 	int err;
13640 	void *data = NULL;
13641 	unsigned int data_len = 0;
13642 
13643 	if (cb->args[0]) {
13644 		/* subtract the 1 again here */
13645 		struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
13646 		struct wireless_dev *tmp;
13647 
13648 		if (!wiphy)
13649 			return -ENODEV;
13650 		*rdev = wiphy_to_rdev(wiphy);
13651 		*wdev = NULL;
13652 
13653 		if (cb->args[1]) {
13654 			list_for_each_entry(tmp, &wiphy->wdev_list, list) {
13655 				if (tmp->identifier == cb->args[1] - 1) {
13656 					*wdev = tmp;
13657 					break;
13658 				}
13659 			}
13660 		}
13661 
13662 		/* keep rtnl locked in successful case */
13663 		return 0;
13664 	}
13665 
13666 	attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13667 	if (!attrbuf)
13668 		return -ENOMEM;
13669 
13670 	err = nlmsg_parse_deprecated(cb->nlh,
13671 				     GENL_HDRLEN + nl80211_fam.hdrsize,
13672 				     attrbuf, nl80211_fam.maxattr,
13673 				     nl80211_policy, NULL);
13674 	if (err)
13675 		goto out;
13676 
13677 	if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13678 	    !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13679 		err = -EINVAL;
13680 		goto out;
13681 	}
13682 
13683 	*wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13684 	if (IS_ERR(*wdev))
13685 		*wdev = NULL;
13686 
13687 	*rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13688 	if (IS_ERR(*rdev)) {
13689 		err = PTR_ERR(*rdev);
13690 		goto out;
13691 	}
13692 
13693 	vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13694 	subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13695 
13696 	for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13697 		const struct wiphy_vendor_command *vcmd;
13698 
13699 		vcmd = &(*rdev)->wiphy.vendor_commands[i];
13700 
13701 		if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13702 			continue;
13703 
13704 		if (!vcmd->dumpit) {
13705 			err = -EOPNOTSUPP;
13706 			goto out;
13707 		}
13708 
13709 		vcmd_idx = i;
13710 		break;
13711 	}
13712 
13713 	if (vcmd_idx < 0) {
13714 		err = -EOPNOTSUPP;
13715 		goto out;
13716 	}
13717 
13718 	if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13719 		data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13720 		data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13721 
13722 		err = nl80211_vendor_check_policy(
13723 				&(*rdev)->wiphy.vendor_commands[vcmd_idx],
13724 				attrbuf[NL80211_ATTR_VENDOR_DATA],
13725 				cb->extack);
13726 		if (err)
13727 			goto out;
13728 	}
13729 
13730 	/* 0 is the first index - add 1 to parse only once */
13731 	cb->args[0] = (*rdev)->wiphy_idx + 1;
13732 	/* add 1 to know if it was NULL */
13733 	cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13734 	cb->args[2] = vcmd_idx;
13735 	cb->args[3] = (unsigned long)data;
13736 	cb->args[4] = data_len;
13737 
13738 	/* keep rtnl locked in successful case */
13739 	err = 0;
13740 out:
13741 	kfree(attrbuf);
13742 	return err;
13743 }
13744 
nl80211_vendor_cmd_dump(struct sk_buff * skb,struct netlink_callback * cb)13745 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13746 				   struct netlink_callback *cb)
13747 {
13748 	struct cfg80211_registered_device *rdev;
13749 	struct wireless_dev *wdev;
13750 	unsigned int vcmd_idx;
13751 	const struct wiphy_vendor_command *vcmd;
13752 	void *data;
13753 	int data_len;
13754 	int err;
13755 	struct nlattr *vendor_data;
13756 
13757 	rtnl_lock();
13758 	err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13759 	if (err)
13760 		goto out;
13761 
13762 	vcmd_idx = cb->args[2];
13763 	data = (void *)cb->args[3];
13764 	data_len = cb->args[4];
13765 	vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13766 
13767 	if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13768 			   WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13769 		if (!wdev) {
13770 			err = -EINVAL;
13771 			goto out;
13772 		}
13773 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13774 		    !wdev->netdev) {
13775 			err = -EINVAL;
13776 			goto out;
13777 		}
13778 
13779 		if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13780 			if (!wdev_running(wdev)) {
13781 				err = -ENETDOWN;
13782 				goto out;
13783 			}
13784 		}
13785 	}
13786 
13787 	while (1) {
13788 		void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13789 					   cb->nlh->nlmsg_seq, NLM_F_MULTI,
13790 					   NL80211_CMD_VENDOR);
13791 		if (!hdr)
13792 			break;
13793 
13794 		if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13795 		    (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13796 					       wdev_id(wdev),
13797 					       NL80211_ATTR_PAD))) {
13798 			genlmsg_cancel(skb, hdr);
13799 			break;
13800 		}
13801 
13802 		vendor_data = nla_nest_start_noflag(skb,
13803 						    NL80211_ATTR_VENDOR_DATA);
13804 		if (!vendor_data) {
13805 			genlmsg_cancel(skb, hdr);
13806 			break;
13807 		}
13808 
13809 		err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13810 				   (unsigned long *)&cb->args[5]);
13811 		nla_nest_end(skb, vendor_data);
13812 
13813 		if (err == -ENOBUFS || err == -ENOENT) {
13814 			genlmsg_cancel(skb, hdr);
13815 			break;
13816 		} else if (err <= 0) {
13817 			genlmsg_cancel(skb, hdr);
13818 			goto out;
13819 		}
13820 
13821 		genlmsg_end(skb, hdr);
13822 	}
13823 
13824 	err = skb->len;
13825  out:
13826 	rtnl_unlock();
13827 	return err;
13828 }
13829 
__cfg80211_alloc_reply_skb(struct wiphy * wiphy,enum nl80211_commands cmd,enum nl80211_attrs attr,int approxlen)13830 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13831 					   enum nl80211_commands cmd,
13832 					   enum nl80211_attrs attr,
13833 					   int approxlen)
13834 {
13835 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13836 
13837 	if (WARN_ON(!rdev->cur_cmd_info))
13838 		return NULL;
13839 
13840 	return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13841 					   rdev->cur_cmd_info->snd_portid,
13842 					   rdev->cur_cmd_info->snd_seq,
13843 					   cmd, attr, NULL, GFP_KERNEL);
13844 }
13845 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13846 
cfg80211_vendor_cmd_reply(struct sk_buff * skb)13847 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13848 {
13849 	struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13850 	void *hdr = ((void **)skb->cb)[1];
13851 	struct nlattr *data = ((void **)skb->cb)[2];
13852 
13853 	/* clear CB data for netlink core to own from now on */
13854 	memset(skb->cb, 0, sizeof(skb->cb));
13855 
13856 	if (WARN_ON(!rdev->cur_cmd_info)) {
13857 		kfree_skb(skb);
13858 		return -EINVAL;
13859 	}
13860 
13861 	nla_nest_end(skb, data);
13862 	genlmsg_end(skb, hdr);
13863 	return genlmsg_reply(skb, rdev->cur_cmd_info);
13864 }
13865 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13866 
cfg80211_vendor_cmd_get_sender(struct wiphy * wiphy)13867 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13868 {
13869 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13870 
13871 	if (WARN_ON(!rdev->cur_cmd_info))
13872 		return 0;
13873 
13874 	return rdev->cur_cmd_info->snd_portid;
13875 }
13876 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13877 
nl80211_set_qos_map(struct sk_buff * skb,struct genl_info * info)13878 static int nl80211_set_qos_map(struct sk_buff *skb,
13879 			       struct genl_info *info)
13880 {
13881 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13882 	struct cfg80211_qos_map *qos_map = NULL;
13883 	struct net_device *dev = info->user_ptr[1];
13884 	u8 *pos, len, num_des, des_len, des;
13885 	int ret;
13886 
13887 	if (!rdev->ops->set_qos_map)
13888 		return -EOPNOTSUPP;
13889 
13890 	if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13891 		pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13892 		len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13893 
13894 		if (len % 2)
13895 			return -EINVAL;
13896 
13897 		qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13898 		if (!qos_map)
13899 			return -ENOMEM;
13900 
13901 		num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13902 		if (num_des) {
13903 			des_len = num_des *
13904 				sizeof(struct cfg80211_dscp_exception);
13905 			memcpy(qos_map->dscp_exception, pos, des_len);
13906 			qos_map->num_des = num_des;
13907 			for (des = 0; des < num_des; des++) {
13908 				if (qos_map->dscp_exception[des].up > 7) {
13909 					kfree(qos_map);
13910 					return -EINVAL;
13911 				}
13912 			}
13913 			pos += des_len;
13914 		}
13915 		memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13916 	}
13917 
13918 	wdev_lock(dev->ieee80211_ptr);
13919 	ret = nl80211_key_allowed(dev->ieee80211_ptr);
13920 	if (!ret)
13921 		ret = rdev_set_qos_map(rdev, dev, qos_map);
13922 	wdev_unlock(dev->ieee80211_ptr);
13923 
13924 	kfree(qos_map);
13925 	return ret;
13926 }
13927 
nl80211_add_tx_ts(struct sk_buff * skb,struct genl_info * info)13928 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13929 {
13930 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13931 	struct net_device *dev = info->user_ptr[1];
13932 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13933 	const u8 *peer;
13934 	u8 tsid, up;
13935 	u16 admitted_time = 0;
13936 	int err;
13937 
13938 	if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13939 		return -EOPNOTSUPP;
13940 
13941 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13942 	    !info->attrs[NL80211_ATTR_USER_PRIO])
13943 		return -EINVAL;
13944 
13945 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13946 	up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13947 
13948 	/* WMM uses TIDs 0-7 even for TSPEC */
13949 	if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13950 		/* TODO: handle 802.11 TSPEC/admission control
13951 		 * need more attributes for that (e.g. BA session requirement);
13952 		 * change the WMM adminssion test above to allow both then
13953 		 */
13954 		return -EINVAL;
13955 	}
13956 
13957 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13958 
13959 	if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13960 		admitted_time =
13961 			nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13962 		if (!admitted_time)
13963 			return -EINVAL;
13964 	}
13965 
13966 	wdev_lock(wdev);
13967 	switch (wdev->iftype) {
13968 	case NL80211_IFTYPE_STATION:
13969 	case NL80211_IFTYPE_P2P_CLIENT:
13970 		if (wdev->current_bss)
13971 			break;
13972 		err = -ENOTCONN;
13973 		goto out;
13974 	default:
13975 		err = -EOPNOTSUPP;
13976 		goto out;
13977 	}
13978 
13979 	err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13980 
13981  out:
13982 	wdev_unlock(wdev);
13983 	return err;
13984 }
13985 
nl80211_del_tx_ts(struct sk_buff * skb,struct genl_info * info)13986 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13987 {
13988 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
13989 	struct net_device *dev = info->user_ptr[1];
13990 	struct wireless_dev *wdev = dev->ieee80211_ptr;
13991 	const u8 *peer;
13992 	u8 tsid;
13993 	int err;
13994 
13995 	if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13996 		return -EINVAL;
13997 
13998 	tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13999 	peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14000 
14001 	wdev_lock(wdev);
14002 	err = rdev_del_tx_ts(rdev, dev, tsid, peer);
14003 	wdev_unlock(wdev);
14004 
14005 	return err;
14006 }
14007 
nl80211_tdls_channel_switch(struct sk_buff * skb,struct genl_info * info)14008 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
14009 				       struct genl_info *info)
14010 {
14011 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14012 	struct net_device *dev = info->user_ptr[1];
14013 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14014 	struct cfg80211_chan_def chandef = {};
14015 	const u8 *addr;
14016 	u8 oper_class;
14017 	int err;
14018 
14019 	if (!rdev->ops->tdls_channel_switch ||
14020 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
14021 		return -EOPNOTSUPP;
14022 
14023 	switch (dev->ieee80211_ptr->iftype) {
14024 	case NL80211_IFTYPE_STATION:
14025 	case NL80211_IFTYPE_P2P_CLIENT:
14026 		break;
14027 	default:
14028 		return -EOPNOTSUPP;
14029 	}
14030 
14031 	if (!info->attrs[NL80211_ATTR_MAC] ||
14032 	    !info->attrs[NL80211_ATTR_OPER_CLASS])
14033 		return -EINVAL;
14034 
14035 	err = nl80211_parse_chandef(rdev, info, &chandef);
14036 	if (err)
14037 		return err;
14038 
14039 	/*
14040 	 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
14041 	 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
14042 	 * specification is not defined for them.
14043 	 */
14044 	if (chandef.chan->band == NL80211_BAND_2GHZ &&
14045 	    chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
14046 	    chandef.width != NL80211_CHAN_WIDTH_20)
14047 		return -EINVAL;
14048 
14049 	/* we will be active on the TDLS link */
14050 	if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
14051 					   wdev->iftype))
14052 		return -EINVAL;
14053 
14054 	/* don't allow switching to DFS channels */
14055 	if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
14056 		return -EINVAL;
14057 
14058 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14059 	oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
14060 
14061 	wdev_lock(wdev);
14062 	err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
14063 	wdev_unlock(wdev);
14064 
14065 	return err;
14066 }
14067 
nl80211_tdls_cancel_channel_switch(struct sk_buff * skb,struct genl_info * info)14068 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
14069 					      struct genl_info *info)
14070 {
14071 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14072 	struct net_device *dev = info->user_ptr[1];
14073 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14074 	const u8 *addr;
14075 
14076 	if (!rdev->ops->tdls_channel_switch ||
14077 	    !rdev->ops->tdls_cancel_channel_switch ||
14078 	    !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
14079 		return -EOPNOTSUPP;
14080 
14081 	switch (dev->ieee80211_ptr->iftype) {
14082 	case NL80211_IFTYPE_STATION:
14083 	case NL80211_IFTYPE_P2P_CLIENT:
14084 		break;
14085 	default:
14086 		return -EOPNOTSUPP;
14087 	}
14088 
14089 	if (!info->attrs[NL80211_ATTR_MAC])
14090 		return -EINVAL;
14091 
14092 	addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
14093 
14094 	wdev_lock(wdev);
14095 	rdev_tdls_cancel_channel_switch(rdev, dev, addr);
14096 	wdev_unlock(wdev);
14097 
14098 	return 0;
14099 }
14100 
nl80211_set_multicast_to_unicast(struct sk_buff * skb,struct genl_info * info)14101 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
14102 					    struct genl_info *info)
14103 {
14104 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14105 	struct net_device *dev = info->user_ptr[1];
14106 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14107 	const struct nlattr *nla;
14108 	bool enabled;
14109 
14110 	if (!rdev->ops->set_multicast_to_unicast)
14111 		return -EOPNOTSUPP;
14112 
14113 	if (wdev->iftype != NL80211_IFTYPE_AP &&
14114 	    wdev->iftype != NL80211_IFTYPE_P2P_GO)
14115 		return -EOPNOTSUPP;
14116 
14117 	nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
14118 	enabled = nla_get_flag(nla);
14119 
14120 	return rdev_set_multicast_to_unicast(rdev, dev, enabled);
14121 }
14122 
nl80211_set_pmk(struct sk_buff * skb,struct genl_info * info)14123 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
14124 {
14125 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14126 	struct net_device *dev = info->user_ptr[1];
14127 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14128 	struct cfg80211_pmk_conf pmk_conf = {};
14129 	int ret;
14130 
14131 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
14132 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
14133 		return -EOPNOTSUPP;
14134 
14135 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14136 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
14137 		return -EOPNOTSUPP;
14138 
14139 	if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
14140 		return -EINVAL;
14141 
14142 	wdev_lock(wdev);
14143 	if (!wdev->current_bss) {
14144 		ret = -ENOTCONN;
14145 		goto out;
14146 	}
14147 
14148 	pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
14149 	if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
14150 		ret = -EINVAL;
14151 		goto out;
14152 	}
14153 
14154 	pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
14155 	pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
14156 	if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
14157 	    pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
14158 		ret = -EINVAL;
14159 		goto out;
14160 	}
14161 
14162 	if (info->attrs[NL80211_ATTR_PMKR0_NAME])
14163 		pmk_conf.pmk_r0_name =
14164 			nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
14165 
14166 	ret = rdev_set_pmk(rdev, dev, &pmk_conf);
14167 out:
14168 	wdev_unlock(wdev);
14169 	return ret;
14170 }
14171 
nl80211_del_pmk(struct sk_buff * skb,struct genl_info * info)14172 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
14173 {
14174 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14175 	struct net_device *dev = info->user_ptr[1];
14176 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14177 	const u8 *aa;
14178 	int ret;
14179 
14180 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
14181 	    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
14182 		return -EOPNOTSUPP;
14183 
14184 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14185 				     NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
14186 		return -EOPNOTSUPP;
14187 
14188 	if (!info->attrs[NL80211_ATTR_MAC])
14189 		return -EINVAL;
14190 
14191 	wdev_lock(wdev);
14192 	aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
14193 	ret = rdev_del_pmk(rdev, dev, aa);
14194 	wdev_unlock(wdev);
14195 
14196 	return ret;
14197 }
14198 
nl80211_external_auth(struct sk_buff * skb,struct genl_info * info)14199 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
14200 {
14201 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14202 	struct net_device *dev = info->user_ptr[1];
14203 	struct cfg80211_external_auth_params params;
14204 
14205 	if (!rdev->ops->external_auth)
14206 		return -EOPNOTSUPP;
14207 
14208 	if (!info->attrs[NL80211_ATTR_SSID] &&
14209 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
14210 	    dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
14211 		return -EINVAL;
14212 
14213 	if (!info->attrs[NL80211_ATTR_BSSID])
14214 		return -EINVAL;
14215 
14216 	if (!info->attrs[NL80211_ATTR_STATUS_CODE])
14217 		return -EINVAL;
14218 
14219 	memset(&params, 0, sizeof(params));
14220 
14221 	if (info->attrs[NL80211_ATTR_SSID]) {
14222 		params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
14223 		if (params.ssid.ssid_len == 0)
14224 			return -EINVAL;
14225 		memcpy(params.ssid.ssid,
14226 		       nla_data(info->attrs[NL80211_ATTR_SSID]),
14227 		       params.ssid.ssid_len);
14228 	}
14229 
14230 	memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
14231 	       ETH_ALEN);
14232 
14233 	params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
14234 
14235 	if (info->attrs[NL80211_ATTR_PMKID])
14236 		params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
14237 
14238 	return rdev_external_auth(rdev, dev, &params);
14239 }
14240 
nl80211_tx_control_port(struct sk_buff * skb,struct genl_info * info)14241 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
14242 {
14243 	bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
14244 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14245 	struct net_device *dev = info->user_ptr[1];
14246 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14247 	const u8 *buf;
14248 	size_t len;
14249 	u8 *dest;
14250 	u16 proto;
14251 	bool noencrypt;
14252 	u64 cookie = 0;
14253 	int err;
14254 
14255 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
14256 				     NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
14257 		return -EOPNOTSUPP;
14258 
14259 	if (!rdev->ops->tx_control_port)
14260 		return -EOPNOTSUPP;
14261 
14262 	if (!info->attrs[NL80211_ATTR_FRAME] ||
14263 	    !info->attrs[NL80211_ATTR_MAC] ||
14264 	    !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
14265 		GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
14266 		return -EINVAL;
14267 	}
14268 
14269 	wdev_lock(wdev);
14270 
14271 	switch (wdev->iftype) {
14272 	case NL80211_IFTYPE_AP:
14273 	case NL80211_IFTYPE_P2P_GO:
14274 	case NL80211_IFTYPE_MESH_POINT:
14275 		break;
14276 	case NL80211_IFTYPE_ADHOC:
14277 	case NL80211_IFTYPE_STATION:
14278 	case NL80211_IFTYPE_P2P_CLIENT:
14279 		if (wdev->current_bss)
14280 			break;
14281 		err = -ENOTCONN;
14282 		goto out;
14283 	default:
14284 		err = -EOPNOTSUPP;
14285 		goto out;
14286 	}
14287 
14288 	wdev_unlock(wdev);
14289 
14290 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
14291 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
14292 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
14293 	proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
14294 	noencrypt =
14295 		nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
14296 
14297 	err = rdev_tx_control_port(rdev, dev, buf, len,
14298 				   dest, cpu_to_be16(proto), noencrypt,
14299 				   dont_wait_for_ack ? NULL : &cookie);
14300 	if (!err && !dont_wait_for_ack)
14301 		nl_set_extack_cookie_u64(info->extack, cookie);
14302 	return err;
14303  out:
14304 	wdev_unlock(wdev);
14305 	return err;
14306 }
14307 
nl80211_get_ftm_responder_stats(struct sk_buff * skb,struct genl_info * info)14308 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
14309 					   struct genl_info *info)
14310 {
14311 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14312 	struct net_device *dev = info->user_ptr[1];
14313 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14314 	struct cfg80211_ftm_responder_stats ftm_stats = {};
14315 	struct sk_buff *msg;
14316 	void *hdr;
14317 	struct nlattr *ftm_stats_attr;
14318 	int err;
14319 
14320 	if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
14321 		return -EOPNOTSUPP;
14322 
14323 	err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
14324 	if (err)
14325 		return err;
14326 
14327 	if (!ftm_stats.filled)
14328 		return -ENODATA;
14329 
14330 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14331 	if (!msg)
14332 		return -ENOMEM;
14333 
14334 	hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14335 			     NL80211_CMD_GET_FTM_RESPONDER_STATS);
14336 	if (!hdr)
14337 		goto nla_put_failure;
14338 
14339 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14340 		goto nla_put_failure;
14341 
14342 	ftm_stats_attr = nla_nest_start_noflag(msg,
14343 					       NL80211_ATTR_FTM_RESPONDER_STATS);
14344 	if (!ftm_stats_attr)
14345 		goto nla_put_failure;
14346 
14347 #define SET_FTM(field, name, type)					 \
14348 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14349 	    nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,		 \
14350 			     ftm_stats.field))				 \
14351 		goto nla_put_failure; } while (0)
14352 #define SET_FTM_U64(field, name)					 \
14353 	do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
14354 	    nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,		 \
14355 			      ftm_stats.field, NL80211_FTM_STATS_PAD))	 \
14356 		goto nla_put_failure; } while (0)
14357 
14358 	SET_FTM(success_num, SUCCESS_NUM, u32);
14359 	SET_FTM(partial_num, PARTIAL_NUM, u32);
14360 	SET_FTM(failed_num, FAILED_NUM, u32);
14361 	SET_FTM(asap_num, ASAP_NUM, u32);
14362 	SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
14363 	SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
14364 	SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
14365 	SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
14366 	SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
14367 #undef SET_FTM
14368 
14369 	nla_nest_end(msg, ftm_stats_attr);
14370 
14371 	genlmsg_end(msg, hdr);
14372 	return genlmsg_reply(msg, info);
14373 
14374 nla_put_failure:
14375 	nlmsg_free(msg);
14376 	return -ENOBUFS;
14377 }
14378 
nl80211_update_owe_info(struct sk_buff * skb,struct genl_info * info)14379 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
14380 {
14381 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14382 	struct cfg80211_update_owe_info owe_info;
14383 	struct net_device *dev = info->user_ptr[1];
14384 
14385 	if (!rdev->ops->update_owe_info)
14386 		return -EOPNOTSUPP;
14387 
14388 	if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
14389 	    !info->attrs[NL80211_ATTR_MAC])
14390 		return -EINVAL;
14391 
14392 	memset(&owe_info, 0, sizeof(owe_info));
14393 	owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
14394 	nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
14395 
14396 	if (info->attrs[NL80211_ATTR_IE]) {
14397 		owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14398 		owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14399 	}
14400 
14401 	return rdev_update_owe_info(rdev, dev, &owe_info);
14402 }
14403 
nl80211_probe_mesh_link(struct sk_buff * skb,struct genl_info * info)14404 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
14405 {
14406 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14407 	struct net_device *dev = info->user_ptr[1];
14408 	struct wireless_dev *wdev = dev->ieee80211_ptr;
14409 	struct station_info sinfo = {};
14410 	const u8 *buf;
14411 	size_t len;
14412 	u8 *dest;
14413 	int err;
14414 
14415 	if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
14416 		return -EOPNOTSUPP;
14417 
14418 	if (!info->attrs[NL80211_ATTR_MAC] ||
14419 	    !info->attrs[NL80211_ATTR_FRAME]) {
14420 		GENL_SET_ERR_MSG(info, "Frame or MAC missing");
14421 		return -EINVAL;
14422 	}
14423 
14424 	if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
14425 		return -EOPNOTSUPP;
14426 
14427 	dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
14428 	buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
14429 	len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
14430 
14431 	if (len < sizeof(struct ethhdr))
14432 		return -EINVAL;
14433 
14434 	if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
14435 	    !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
14436 		return -EINVAL;
14437 
14438 	err = rdev_get_station(rdev, dev, dest, &sinfo);
14439 	if (err)
14440 		return err;
14441 
14442 	cfg80211_sinfo_release_content(&sinfo);
14443 
14444 	return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
14445 }
14446 
parse_tid_conf(struct cfg80211_registered_device * rdev,struct nlattr * attrs[],struct net_device * dev,struct cfg80211_tid_cfg * tid_conf,struct genl_info * info,const u8 * peer)14447 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
14448 			  struct nlattr *attrs[], struct net_device *dev,
14449 			  struct cfg80211_tid_cfg *tid_conf,
14450 			  struct genl_info *info, const u8 *peer)
14451 {
14452 	struct netlink_ext_ack *extack = info->extack;
14453 	u64 mask;
14454 	int err;
14455 
14456 	if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
14457 		return -EINVAL;
14458 
14459 	tid_conf->config_override =
14460 			nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
14461 	tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
14462 
14463 	if (tid_conf->config_override) {
14464 		if (rdev->ops->reset_tid_config) {
14465 			err = rdev_reset_tid_config(rdev, dev, peer,
14466 						    tid_conf->tids);
14467 			if (err)
14468 				return err;
14469 		} else {
14470 			return -EINVAL;
14471 		}
14472 	}
14473 
14474 	if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
14475 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
14476 		tid_conf->noack =
14477 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
14478 	}
14479 
14480 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
14481 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
14482 		tid_conf->retry_short =
14483 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
14484 
14485 		if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
14486 			return -EINVAL;
14487 	}
14488 
14489 	if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
14490 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
14491 		tid_conf->retry_long =
14492 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
14493 
14494 		if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
14495 			return -EINVAL;
14496 	}
14497 
14498 	if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
14499 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
14500 		tid_conf->ampdu =
14501 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
14502 	}
14503 
14504 	if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
14505 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
14506 		tid_conf->rtscts =
14507 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
14508 	}
14509 
14510 	if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
14511 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
14512 		tid_conf->amsdu =
14513 			nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
14514 	}
14515 
14516 	if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
14517 		u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
14518 
14519 		tid_conf->txrate_type = nla_get_u8(attrs[idx]);
14520 
14521 		if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
14522 			attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
14523 			err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
14524 						    &tid_conf->txrate_mask, dev);
14525 			if (err)
14526 				return err;
14527 
14528 			tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
14529 		}
14530 		tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
14531 	}
14532 
14533 	if (peer)
14534 		mask = rdev->wiphy.tid_config_support.peer;
14535 	else
14536 		mask = rdev->wiphy.tid_config_support.vif;
14537 
14538 	if (tid_conf->mask & ~mask) {
14539 		NL_SET_ERR_MSG(extack, "unsupported TID configuration");
14540 		return -ENOTSUPP;
14541 	}
14542 
14543 	return 0;
14544 }
14545 
nl80211_set_tid_config(struct sk_buff * skb,struct genl_info * info)14546 static int nl80211_set_tid_config(struct sk_buff *skb,
14547 				  struct genl_info *info)
14548 {
14549 	struct cfg80211_registered_device *rdev = info->user_ptr[0];
14550 	struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
14551 	struct net_device *dev = info->user_ptr[1];
14552 	struct cfg80211_tid_config *tid_config;
14553 	struct nlattr *tid;
14554 	int conf_idx = 0, rem_conf;
14555 	int ret = -EINVAL;
14556 	u32 num_conf = 0;
14557 
14558 	if (!info->attrs[NL80211_ATTR_TID_CONFIG])
14559 		return -EINVAL;
14560 
14561 	if (!rdev->ops->set_tid_config)
14562 		return -EOPNOTSUPP;
14563 
14564 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14565 			    rem_conf)
14566 		num_conf++;
14567 
14568 	tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
14569 			     GFP_KERNEL);
14570 	if (!tid_config)
14571 		return -ENOMEM;
14572 
14573 	tid_config->n_tid_conf = num_conf;
14574 
14575 	if (info->attrs[NL80211_ATTR_MAC])
14576 		tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
14577 
14578 	nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
14579 			    rem_conf) {
14580 		ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
14581 				       tid, NULL, NULL);
14582 
14583 		if (ret)
14584 			goto bad_tid_conf;
14585 
14586 		ret = parse_tid_conf(rdev, attrs, dev,
14587 				     &tid_config->tid_conf[conf_idx],
14588 				     info, tid_config->peer);
14589 		if (ret)
14590 			goto bad_tid_conf;
14591 
14592 		conf_idx++;
14593 	}
14594 
14595 	ret = rdev_set_tid_config(rdev, dev, tid_config);
14596 
14597 bad_tid_conf:
14598 	kfree(tid_config);
14599 	return ret;
14600 }
14601 
14602 #define NL80211_FLAG_NEED_WIPHY		0x01
14603 #define NL80211_FLAG_NEED_NETDEV	0x02
14604 #define NL80211_FLAG_NEED_RTNL		0x04
14605 #define NL80211_FLAG_CHECK_NETDEV_UP	0x08
14606 #define NL80211_FLAG_NEED_NETDEV_UP	(NL80211_FLAG_NEED_NETDEV |\
14607 					 NL80211_FLAG_CHECK_NETDEV_UP)
14608 #define NL80211_FLAG_NEED_WDEV		0x10
14609 /* If a netdev is associated, it must be UP, P2P must be started */
14610 #define NL80211_FLAG_NEED_WDEV_UP	(NL80211_FLAG_NEED_WDEV |\
14611 					 NL80211_FLAG_CHECK_NETDEV_UP)
14612 #define NL80211_FLAG_CLEAR_SKB		0x20
14613 
nl80211_pre_doit(const struct genl_ops * ops,struct sk_buff * skb,struct genl_info * info)14614 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
14615 			    struct genl_info *info)
14616 {
14617 	struct cfg80211_registered_device *rdev;
14618 	struct wireless_dev *wdev;
14619 	struct net_device *dev;
14620 	bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
14621 
14622 	if (rtnl)
14623 		rtnl_lock();
14624 
14625 	if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
14626 		rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
14627 		if (IS_ERR(rdev)) {
14628 			if (rtnl)
14629 				rtnl_unlock();
14630 			return PTR_ERR(rdev);
14631 		}
14632 		info->user_ptr[0] = rdev;
14633 	} else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
14634 		   ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14635 		ASSERT_RTNL();
14636 
14637 		wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
14638 						  info->attrs);
14639 		if (IS_ERR(wdev)) {
14640 			if (rtnl)
14641 				rtnl_unlock();
14642 			return PTR_ERR(wdev);
14643 		}
14644 
14645 		dev = wdev->netdev;
14646 		rdev = wiphy_to_rdev(wdev->wiphy);
14647 
14648 		if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
14649 			if (!dev) {
14650 				if (rtnl)
14651 					rtnl_unlock();
14652 				return -EINVAL;
14653 			}
14654 
14655 			info->user_ptr[1] = dev;
14656 		} else {
14657 			info->user_ptr[1] = wdev;
14658 		}
14659 
14660 		if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
14661 		    !wdev_running(wdev)) {
14662 			if (rtnl)
14663 				rtnl_unlock();
14664 			return -ENETDOWN;
14665 		}
14666 
14667 		if (dev)
14668 			dev_hold(dev);
14669 
14670 		info->user_ptr[0] = rdev;
14671 	}
14672 
14673 	return 0;
14674 }
14675 
nl80211_post_doit(const struct genl_ops * ops,struct sk_buff * skb,struct genl_info * info)14676 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
14677 			      struct genl_info *info)
14678 {
14679 	if (info->user_ptr[1]) {
14680 		if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
14681 			struct wireless_dev *wdev = info->user_ptr[1];
14682 
14683 			if (wdev->netdev)
14684 				dev_put(wdev->netdev);
14685 		} else {
14686 			dev_put(info->user_ptr[1]);
14687 		}
14688 	}
14689 
14690 	if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
14691 		rtnl_unlock();
14692 
14693 	/* If needed, clear the netlink message payload from the SKB
14694 	 * as it might contain key data that shouldn't stick around on
14695 	 * the heap after the SKB is freed. The netlink message header
14696 	 * is still needed for further processing, so leave it intact.
14697 	 */
14698 	if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
14699 		struct nlmsghdr *nlh = nlmsg_hdr(skb);
14700 
14701 		memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
14702 	}
14703 }
14704 
14705 static const struct genl_ops nl80211_ops[] = {
14706 	{
14707 		.cmd = NL80211_CMD_GET_WIPHY,
14708 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14709 		.doit = nl80211_get_wiphy,
14710 		.dumpit = nl80211_dump_wiphy,
14711 		.done = nl80211_dump_wiphy_done,
14712 		/* can be retrieved by unprivileged users */
14713 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14714 				  NL80211_FLAG_NEED_RTNL,
14715 	},
14716 };
14717 
14718 static const struct genl_small_ops nl80211_small_ops[] = {
14719 	{
14720 		.cmd = NL80211_CMD_SET_WIPHY,
14721 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14722 		.doit = nl80211_set_wiphy,
14723 		.flags = GENL_UNS_ADMIN_PERM,
14724 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14725 	},
14726 	{
14727 		.cmd = NL80211_CMD_GET_INTERFACE,
14728 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14729 		.doit = nl80211_get_interface,
14730 		.dumpit = nl80211_dump_interface,
14731 		/* can be retrieved by unprivileged users */
14732 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14733 				  NL80211_FLAG_NEED_RTNL,
14734 	},
14735 	{
14736 		.cmd = NL80211_CMD_SET_INTERFACE,
14737 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14738 		.doit = nl80211_set_interface,
14739 		.flags = GENL_UNS_ADMIN_PERM,
14740 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14741 				  NL80211_FLAG_NEED_RTNL,
14742 	},
14743 	{
14744 		.cmd = NL80211_CMD_NEW_INTERFACE,
14745 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14746 		.doit = nl80211_new_interface,
14747 		.flags = GENL_UNS_ADMIN_PERM,
14748 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
14749 				  NL80211_FLAG_NEED_RTNL,
14750 	},
14751 	{
14752 		.cmd = NL80211_CMD_DEL_INTERFACE,
14753 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14754 		.doit = nl80211_del_interface,
14755 		.flags = GENL_UNS_ADMIN_PERM,
14756 		.internal_flags = NL80211_FLAG_NEED_WDEV |
14757 				  NL80211_FLAG_NEED_RTNL,
14758 	},
14759 	{
14760 		.cmd = NL80211_CMD_GET_KEY,
14761 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14762 		.doit = nl80211_get_key,
14763 		.flags = GENL_UNS_ADMIN_PERM,
14764 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14765 				  NL80211_FLAG_NEED_RTNL,
14766 	},
14767 	{
14768 		.cmd = NL80211_CMD_SET_KEY,
14769 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14770 		.doit = nl80211_set_key,
14771 		.flags = GENL_UNS_ADMIN_PERM,
14772 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14773 				  NL80211_FLAG_NEED_RTNL |
14774 				  NL80211_FLAG_CLEAR_SKB,
14775 	},
14776 	{
14777 		.cmd = NL80211_CMD_NEW_KEY,
14778 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14779 		.doit = nl80211_new_key,
14780 		.flags = GENL_UNS_ADMIN_PERM,
14781 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14782 				  NL80211_FLAG_NEED_RTNL |
14783 				  NL80211_FLAG_CLEAR_SKB,
14784 	},
14785 	{
14786 		.cmd = NL80211_CMD_DEL_KEY,
14787 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14788 		.doit = nl80211_del_key,
14789 		.flags = GENL_UNS_ADMIN_PERM,
14790 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14791 				  NL80211_FLAG_NEED_RTNL,
14792 	},
14793 	{
14794 		.cmd = NL80211_CMD_SET_BEACON,
14795 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14796 		.flags = GENL_UNS_ADMIN_PERM,
14797 		.doit = nl80211_set_beacon,
14798 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14799 				  NL80211_FLAG_NEED_RTNL,
14800 	},
14801 	{
14802 		.cmd = NL80211_CMD_START_AP,
14803 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14804 		.flags = GENL_UNS_ADMIN_PERM,
14805 		.doit = nl80211_start_ap,
14806 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14807 				  NL80211_FLAG_NEED_RTNL,
14808 	},
14809 	{
14810 		.cmd = NL80211_CMD_STOP_AP,
14811 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14812 		.flags = GENL_UNS_ADMIN_PERM,
14813 		.doit = nl80211_stop_ap,
14814 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14815 				  NL80211_FLAG_NEED_RTNL,
14816 	},
14817 	{
14818 		.cmd = NL80211_CMD_GET_STATION,
14819 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14820 		.doit = nl80211_get_station,
14821 		.dumpit = nl80211_dump_station,
14822 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
14823 				  NL80211_FLAG_NEED_RTNL,
14824 	},
14825 	{
14826 		.cmd = NL80211_CMD_SET_STATION,
14827 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14828 		.doit = nl80211_set_station,
14829 		.flags = GENL_UNS_ADMIN_PERM,
14830 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14831 				  NL80211_FLAG_NEED_RTNL,
14832 	},
14833 	{
14834 		.cmd = NL80211_CMD_NEW_STATION,
14835 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14836 		.doit = nl80211_new_station,
14837 		.flags = GENL_UNS_ADMIN_PERM,
14838 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14839 				  NL80211_FLAG_NEED_RTNL,
14840 	},
14841 	{
14842 		.cmd = NL80211_CMD_DEL_STATION,
14843 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14844 		.doit = nl80211_del_station,
14845 		.flags = GENL_UNS_ADMIN_PERM,
14846 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14847 				  NL80211_FLAG_NEED_RTNL,
14848 	},
14849 	{
14850 		.cmd = NL80211_CMD_GET_MPATH,
14851 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14852 		.doit = nl80211_get_mpath,
14853 		.dumpit = nl80211_dump_mpath,
14854 		.flags = GENL_UNS_ADMIN_PERM,
14855 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14856 				  NL80211_FLAG_NEED_RTNL,
14857 	},
14858 	{
14859 		.cmd = NL80211_CMD_GET_MPP,
14860 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14861 		.doit = nl80211_get_mpp,
14862 		.dumpit = nl80211_dump_mpp,
14863 		.flags = GENL_UNS_ADMIN_PERM,
14864 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14865 				  NL80211_FLAG_NEED_RTNL,
14866 	},
14867 	{
14868 		.cmd = NL80211_CMD_SET_MPATH,
14869 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14870 		.doit = nl80211_set_mpath,
14871 		.flags = GENL_UNS_ADMIN_PERM,
14872 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14873 				  NL80211_FLAG_NEED_RTNL,
14874 	},
14875 	{
14876 		.cmd = NL80211_CMD_NEW_MPATH,
14877 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14878 		.doit = nl80211_new_mpath,
14879 		.flags = GENL_UNS_ADMIN_PERM,
14880 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14881 				  NL80211_FLAG_NEED_RTNL,
14882 	},
14883 	{
14884 		.cmd = NL80211_CMD_DEL_MPATH,
14885 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14886 		.doit = nl80211_del_mpath,
14887 		.flags = GENL_UNS_ADMIN_PERM,
14888 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14889 				  NL80211_FLAG_NEED_RTNL,
14890 	},
14891 	{
14892 		.cmd = NL80211_CMD_SET_BSS,
14893 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14894 		.doit = nl80211_set_bss,
14895 		.flags = GENL_UNS_ADMIN_PERM,
14896 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14897 				  NL80211_FLAG_NEED_RTNL,
14898 	},
14899 	{
14900 		.cmd = NL80211_CMD_GET_REG,
14901 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14902 		.doit = nl80211_get_reg_do,
14903 		.dumpit = nl80211_get_reg_dump,
14904 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14905 		/* can be retrieved by unprivileged users */
14906 	},
14907 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14908 	{
14909 		.cmd = NL80211_CMD_SET_REG,
14910 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14911 		.doit = nl80211_set_reg,
14912 		.flags = GENL_ADMIN_PERM,
14913 		.internal_flags = NL80211_FLAG_NEED_RTNL,
14914 	},
14915 #endif
14916 	{
14917 		.cmd = NL80211_CMD_REQ_SET_REG,
14918 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14919 		.doit = nl80211_req_set_reg,
14920 		.flags = GENL_ADMIN_PERM,
14921 	},
14922 	{
14923 		.cmd = NL80211_CMD_RELOAD_REGDB,
14924 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14925 		.doit = nl80211_reload_regdb,
14926 		.flags = GENL_ADMIN_PERM,
14927 	},
14928 	{
14929 		.cmd = NL80211_CMD_GET_MESH_CONFIG,
14930 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14931 		.doit = nl80211_get_mesh_config,
14932 		/* can be retrieved by unprivileged users */
14933 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14934 				  NL80211_FLAG_NEED_RTNL,
14935 	},
14936 	{
14937 		.cmd = NL80211_CMD_SET_MESH_CONFIG,
14938 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14939 		.doit = nl80211_update_mesh_config,
14940 		.flags = GENL_UNS_ADMIN_PERM,
14941 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14942 				  NL80211_FLAG_NEED_RTNL,
14943 	},
14944 	{
14945 		.cmd = NL80211_CMD_TRIGGER_SCAN,
14946 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14947 		.doit = nl80211_trigger_scan,
14948 		.flags = GENL_UNS_ADMIN_PERM,
14949 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14950 				  NL80211_FLAG_NEED_RTNL,
14951 	},
14952 	{
14953 		.cmd = NL80211_CMD_ABORT_SCAN,
14954 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14955 		.doit = nl80211_abort_scan,
14956 		.flags = GENL_UNS_ADMIN_PERM,
14957 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14958 				  NL80211_FLAG_NEED_RTNL,
14959 	},
14960 	{
14961 		.cmd = NL80211_CMD_GET_SCAN,
14962 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14963 		.dumpit = nl80211_dump_scan,
14964 	},
14965 	{
14966 		.cmd = NL80211_CMD_START_SCHED_SCAN,
14967 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14968 		.doit = nl80211_start_sched_scan,
14969 		.flags = GENL_UNS_ADMIN_PERM,
14970 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14971 				  NL80211_FLAG_NEED_RTNL,
14972 	},
14973 	{
14974 		.cmd = NL80211_CMD_STOP_SCHED_SCAN,
14975 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14976 		.doit = nl80211_stop_sched_scan,
14977 		.flags = GENL_UNS_ADMIN_PERM,
14978 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14979 				  NL80211_FLAG_NEED_RTNL,
14980 	},
14981 	{
14982 		.cmd = NL80211_CMD_AUTHENTICATE,
14983 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14984 		.doit = nl80211_authenticate,
14985 		.flags = GENL_UNS_ADMIN_PERM,
14986 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14987 				  NL80211_FLAG_NEED_RTNL |
14988 				  NL80211_FLAG_CLEAR_SKB,
14989 	},
14990 	{
14991 		.cmd = NL80211_CMD_ASSOCIATE,
14992 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14993 		.doit = nl80211_associate,
14994 		.flags = GENL_UNS_ADMIN_PERM,
14995 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14996 				  NL80211_FLAG_NEED_RTNL |
14997 				  NL80211_FLAG_CLEAR_SKB,
14998 	},
14999 	{
15000 		.cmd = NL80211_CMD_DEAUTHENTICATE,
15001 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15002 		.doit = nl80211_deauthenticate,
15003 		.flags = GENL_UNS_ADMIN_PERM,
15004 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15005 				  NL80211_FLAG_NEED_RTNL,
15006 	},
15007 	{
15008 		.cmd = NL80211_CMD_DISASSOCIATE,
15009 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15010 		.doit = nl80211_disassociate,
15011 		.flags = GENL_UNS_ADMIN_PERM,
15012 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15013 				  NL80211_FLAG_NEED_RTNL,
15014 	},
15015 	{
15016 		.cmd = NL80211_CMD_JOIN_IBSS,
15017 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15018 		.doit = nl80211_join_ibss,
15019 		.flags = GENL_UNS_ADMIN_PERM,
15020 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15021 				  NL80211_FLAG_NEED_RTNL,
15022 	},
15023 	{
15024 		.cmd = NL80211_CMD_LEAVE_IBSS,
15025 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15026 		.doit = nl80211_leave_ibss,
15027 		.flags = GENL_UNS_ADMIN_PERM,
15028 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15029 				  NL80211_FLAG_NEED_RTNL,
15030 	},
15031 #ifdef CONFIG_NL80211_TESTMODE
15032 	{
15033 		.cmd = NL80211_CMD_TESTMODE,
15034 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15035 		.doit = nl80211_testmode_do,
15036 		.dumpit = nl80211_testmode_dump,
15037 		.flags = GENL_UNS_ADMIN_PERM,
15038 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15039 				  NL80211_FLAG_NEED_RTNL,
15040 	},
15041 #endif
15042 	{
15043 		.cmd = NL80211_CMD_CONNECT,
15044 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15045 		.doit = nl80211_connect,
15046 		.flags = GENL_UNS_ADMIN_PERM,
15047 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15048 				  NL80211_FLAG_NEED_RTNL |
15049 				  NL80211_FLAG_CLEAR_SKB,
15050 	},
15051 	{
15052 		.cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
15053 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15054 		.doit = nl80211_update_connect_params,
15055 		.flags = GENL_ADMIN_PERM,
15056 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15057 				  NL80211_FLAG_NEED_RTNL |
15058 				  NL80211_FLAG_CLEAR_SKB,
15059 	},
15060 	{
15061 		.cmd = NL80211_CMD_DISCONNECT,
15062 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15063 		.doit = nl80211_disconnect,
15064 		.flags = GENL_UNS_ADMIN_PERM,
15065 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15066 				  NL80211_FLAG_NEED_RTNL,
15067 	},
15068 	{
15069 		.cmd = NL80211_CMD_SET_WIPHY_NETNS,
15070 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15071 		.doit = nl80211_wiphy_netns,
15072 		.flags = GENL_UNS_ADMIN_PERM,
15073 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15074 				  NL80211_FLAG_NEED_RTNL,
15075 	},
15076 	{
15077 		.cmd = NL80211_CMD_GET_SURVEY,
15078 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15079 		.dumpit = nl80211_dump_survey,
15080 	},
15081 	{
15082 		.cmd = NL80211_CMD_SET_PMKSA,
15083 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15084 		.doit = nl80211_setdel_pmksa,
15085 		.flags = GENL_UNS_ADMIN_PERM,
15086 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15087 				  NL80211_FLAG_NEED_RTNL |
15088 				  NL80211_FLAG_CLEAR_SKB,
15089 	},
15090 	{
15091 		.cmd = NL80211_CMD_DEL_PMKSA,
15092 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15093 		.doit = nl80211_setdel_pmksa,
15094 		.flags = GENL_UNS_ADMIN_PERM,
15095 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15096 				  NL80211_FLAG_NEED_RTNL,
15097 	},
15098 	{
15099 		.cmd = NL80211_CMD_FLUSH_PMKSA,
15100 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15101 		.doit = nl80211_flush_pmksa,
15102 		.flags = GENL_UNS_ADMIN_PERM,
15103 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15104 				  NL80211_FLAG_NEED_RTNL,
15105 	},
15106 	{
15107 		.cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
15108 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15109 		.doit = nl80211_remain_on_channel,
15110 		.flags = GENL_UNS_ADMIN_PERM,
15111 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15112 				  NL80211_FLAG_NEED_RTNL,
15113 	},
15114 	{
15115 		.cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15116 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15117 		.doit = nl80211_cancel_remain_on_channel,
15118 		.flags = GENL_UNS_ADMIN_PERM,
15119 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15120 				  NL80211_FLAG_NEED_RTNL,
15121 	},
15122 	{
15123 		.cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
15124 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15125 		.doit = nl80211_set_tx_bitrate_mask,
15126 		.flags = GENL_UNS_ADMIN_PERM,
15127 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15128 				  NL80211_FLAG_NEED_RTNL,
15129 	},
15130 	{
15131 		.cmd = NL80211_CMD_REGISTER_FRAME,
15132 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15133 		.doit = nl80211_register_mgmt,
15134 		.flags = GENL_UNS_ADMIN_PERM,
15135 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15136 				  NL80211_FLAG_NEED_RTNL,
15137 	},
15138 	{
15139 		.cmd = NL80211_CMD_FRAME,
15140 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15141 		.doit = nl80211_tx_mgmt,
15142 		.flags = GENL_UNS_ADMIN_PERM,
15143 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15144 				  NL80211_FLAG_NEED_RTNL,
15145 	},
15146 	{
15147 		.cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
15148 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15149 		.doit = nl80211_tx_mgmt_cancel_wait,
15150 		.flags = GENL_UNS_ADMIN_PERM,
15151 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15152 				  NL80211_FLAG_NEED_RTNL,
15153 	},
15154 	{
15155 		.cmd = NL80211_CMD_SET_POWER_SAVE,
15156 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15157 		.doit = nl80211_set_power_save,
15158 		.flags = GENL_UNS_ADMIN_PERM,
15159 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15160 				  NL80211_FLAG_NEED_RTNL,
15161 	},
15162 	{
15163 		.cmd = NL80211_CMD_GET_POWER_SAVE,
15164 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15165 		.doit = nl80211_get_power_save,
15166 		/* can be retrieved by unprivileged users */
15167 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15168 				  NL80211_FLAG_NEED_RTNL,
15169 	},
15170 	{
15171 		.cmd = NL80211_CMD_SET_CQM,
15172 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15173 		.doit = nl80211_set_cqm,
15174 		.flags = GENL_UNS_ADMIN_PERM,
15175 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15176 				  NL80211_FLAG_NEED_RTNL,
15177 	},
15178 	{
15179 		.cmd = NL80211_CMD_SET_CHANNEL,
15180 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15181 		.doit = nl80211_set_channel,
15182 		.flags = GENL_UNS_ADMIN_PERM,
15183 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15184 				  NL80211_FLAG_NEED_RTNL,
15185 	},
15186 	{
15187 		.cmd = NL80211_CMD_SET_WDS_PEER,
15188 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15189 		.doit = nl80211_set_wds_peer,
15190 		.flags = GENL_UNS_ADMIN_PERM,
15191 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15192 				  NL80211_FLAG_NEED_RTNL,
15193 	},
15194 	{
15195 		.cmd = NL80211_CMD_JOIN_MESH,
15196 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15197 		.doit = nl80211_join_mesh,
15198 		.flags = GENL_UNS_ADMIN_PERM,
15199 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15200 				  NL80211_FLAG_NEED_RTNL,
15201 	},
15202 	{
15203 		.cmd = NL80211_CMD_LEAVE_MESH,
15204 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15205 		.doit = nl80211_leave_mesh,
15206 		.flags = GENL_UNS_ADMIN_PERM,
15207 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15208 				  NL80211_FLAG_NEED_RTNL,
15209 	},
15210 	{
15211 		.cmd = NL80211_CMD_JOIN_OCB,
15212 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15213 		.doit = nl80211_join_ocb,
15214 		.flags = GENL_UNS_ADMIN_PERM,
15215 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15216 				  NL80211_FLAG_NEED_RTNL,
15217 	},
15218 	{
15219 		.cmd = NL80211_CMD_LEAVE_OCB,
15220 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15221 		.doit = nl80211_leave_ocb,
15222 		.flags = GENL_UNS_ADMIN_PERM,
15223 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15224 				  NL80211_FLAG_NEED_RTNL,
15225 	},
15226 #ifdef CONFIG_PM
15227 	{
15228 		.cmd = NL80211_CMD_GET_WOWLAN,
15229 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15230 		.doit = nl80211_get_wowlan,
15231 		/* can be retrieved by unprivileged users */
15232 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15233 				  NL80211_FLAG_NEED_RTNL,
15234 	},
15235 	{
15236 		.cmd = NL80211_CMD_SET_WOWLAN,
15237 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15238 		.doit = nl80211_set_wowlan,
15239 		.flags = GENL_UNS_ADMIN_PERM,
15240 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15241 				  NL80211_FLAG_NEED_RTNL,
15242 	},
15243 #endif
15244 	{
15245 		.cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
15246 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15247 		.doit = nl80211_set_rekey_data,
15248 		.flags = GENL_UNS_ADMIN_PERM,
15249 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15250 				  NL80211_FLAG_NEED_RTNL |
15251 				  NL80211_FLAG_CLEAR_SKB,
15252 	},
15253 	{
15254 		.cmd = NL80211_CMD_TDLS_MGMT,
15255 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15256 		.doit = nl80211_tdls_mgmt,
15257 		.flags = GENL_UNS_ADMIN_PERM,
15258 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15259 				  NL80211_FLAG_NEED_RTNL,
15260 	},
15261 	{
15262 		.cmd = NL80211_CMD_TDLS_OPER,
15263 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15264 		.doit = nl80211_tdls_oper,
15265 		.flags = GENL_UNS_ADMIN_PERM,
15266 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15267 				  NL80211_FLAG_NEED_RTNL,
15268 	},
15269 	{
15270 		.cmd = NL80211_CMD_UNEXPECTED_FRAME,
15271 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15272 		.doit = nl80211_register_unexpected_frame,
15273 		.flags = GENL_UNS_ADMIN_PERM,
15274 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15275 				  NL80211_FLAG_NEED_RTNL,
15276 	},
15277 	{
15278 		.cmd = NL80211_CMD_PROBE_CLIENT,
15279 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15280 		.doit = nl80211_probe_client,
15281 		.flags = GENL_UNS_ADMIN_PERM,
15282 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15283 				  NL80211_FLAG_NEED_RTNL,
15284 	},
15285 	{
15286 		.cmd = NL80211_CMD_REGISTER_BEACONS,
15287 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15288 		.doit = nl80211_register_beacons,
15289 		.flags = GENL_UNS_ADMIN_PERM,
15290 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15291 				  NL80211_FLAG_NEED_RTNL,
15292 	},
15293 	{
15294 		.cmd = NL80211_CMD_SET_NOACK_MAP,
15295 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15296 		.doit = nl80211_set_noack_map,
15297 		.flags = GENL_UNS_ADMIN_PERM,
15298 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15299 				  NL80211_FLAG_NEED_RTNL,
15300 	},
15301 	{
15302 		.cmd = NL80211_CMD_START_P2P_DEVICE,
15303 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15304 		.doit = nl80211_start_p2p_device,
15305 		.flags = GENL_UNS_ADMIN_PERM,
15306 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15307 				  NL80211_FLAG_NEED_RTNL,
15308 	},
15309 	{
15310 		.cmd = NL80211_CMD_STOP_P2P_DEVICE,
15311 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15312 		.doit = nl80211_stop_p2p_device,
15313 		.flags = GENL_UNS_ADMIN_PERM,
15314 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15315 				  NL80211_FLAG_NEED_RTNL,
15316 	},
15317 	{
15318 		.cmd = NL80211_CMD_START_NAN,
15319 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15320 		.doit = nl80211_start_nan,
15321 		.flags = GENL_ADMIN_PERM,
15322 		.internal_flags = NL80211_FLAG_NEED_WDEV |
15323 				  NL80211_FLAG_NEED_RTNL,
15324 	},
15325 	{
15326 		.cmd = NL80211_CMD_STOP_NAN,
15327 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15328 		.doit = nl80211_stop_nan,
15329 		.flags = GENL_ADMIN_PERM,
15330 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15331 				  NL80211_FLAG_NEED_RTNL,
15332 	},
15333 	{
15334 		.cmd = NL80211_CMD_ADD_NAN_FUNCTION,
15335 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15336 		.doit = nl80211_nan_add_func,
15337 		.flags = GENL_ADMIN_PERM,
15338 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15339 				  NL80211_FLAG_NEED_RTNL,
15340 	},
15341 	{
15342 		.cmd = NL80211_CMD_DEL_NAN_FUNCTION,
15343 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15344 		.doit = nl80211_nan_del_func,
15345 		.flags = GENL_ADMIN_PERM,
15346 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15347 				  NL80211_FLAG_NEED_RTNL,
15348 	},
15349 	{
15350 		.cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
15351 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15352 		.doit = nl80211_nan_change_config,
15353 		.flags = GENL_ADMIN_PERM,
15354 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15355 				  NL80211_FLAG_NEED_RTNL,
15356 	},
15357 	{
15358 		.cmd = NL80211_CMD_SET_MCAST_RATE,
15359 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15360 		.doit = nl80211_set_mcast_rate,
15361 		.flags = GENL_UNS_ADMIN_PERM,
15362 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15363 				  NL80211_FLAG_NEED_RTNL,
15364 	},
15365 	{
15366 		.cmd = NL80211_CMD_SET_MAC_ACL,
15367 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15368 		.doit = nl80211_set_mac_acl,
15369 		.flags = GENL_UNS_ADMIN_PERM,
15370 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15371 				  NL80211_FLAG_NEED_RTNL,
15372 	},
15373 	{
15374 		.cmd = NL80211_CMD_RADAR_DETECT,
15375 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15376 		.doit = nl80211_start_radar_detection,
15377 		.flags = GENL_UNS_ADMIN_PERM,
15378 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15379 				  NL80211_FLAG_NEED_RTNL,
15380 	},
15381 	{
15382 		.cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
15383 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15384 		.doit = nl80211_get_protocol_features,
15385 	},
15386 	{
15387 		.cmd = NL80211_CMD_UPDATE_FT_IES,
15388 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15389 		.doit = nl80211_update_ft_ies,
15390 		.flags = GENL_UNS_ADMIN_PERM,
15391 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15392 				  NL80211_FLAG_NEED_RTNL,
15393 	},
15394 	{
15395 		.cmd = NL80211_CMD_CRIT_PROTOCOL_START,
15396 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15397 		.doit = nl80211_crit_protocol_start,
15398 		.flags = GENL_UNS_ADMIN_PERM,
15399 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15400 				  NL80211_FLAG_NEED_RTNL,
15401 	},
15402 	{
15403 		.cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
15404 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15405 		.doit = nl80211_crit_protocol_stop,
15406 		.flags = GENL_UNS_ADMIN_PERM,
15407 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15408 				  NL80211_FLAG_NEED_RTNL,
15409 	},
15410 	{
15411 		.cmd = NL80211_CMD_GET_COALESCE,
15412 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15413 		.doit = nl80211_get_coalesce,
15414 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15415 				  NL80211_FLAG_NEED_RTNL,
15416 	},
15417 	{
15418 		.cmd = NL80211_CMD_SET_COALESCE,
15419 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15420 		.doit = nl80211_set_coalesce,
15421 		.flags = GENL_UNS_ADMIN_PERM,
15422 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15423 				  NL80211_FLAG_NEED_RTNL,
15424 	},
15425 	{
15426 		.cmd = NL80211_CMD_CHANNEL_SWITCH,
15427 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15428 		.doit = nl80211_channel_switch,
15429 		.flags = GENL_UNS_ADMIN_PERM,
15430 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15431 				  NL80211_FLAG_NEED_RTNL,
15432 	},
15433 	{
15434 		.cmd = NL80211_CMD_VENDOR,
15435 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15436 		.doit = nl80211_vendor_cmd,
15437 		.dumpit = nl80211_vendor_cmd_dump,
15438 		.flags = GENL_UNS_ADMIN_PERM,
15439 		.internal_flags = NL80211_FLAG_NEED_WIPHY |
15440 				  NL80211_FLAG_NEED_RTNL |
15441 				  NL80211_FLAG_CLEAR_SKB,
15442 	},
15443 	{
15444 		.cmd = NL80211_CMD_SET_QOS_MAP,
15445 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15446 		.doit = nl80211_set_qos_map,
15447 		.flags = GENL_UNS_ADMIN_PERM,
15448 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15449 				  NL80211_FLAG_NEED_RTNL,
15450 	},
15451 	{
15452 		.cmd = NL80211_CMD_ADD_TX_TS,
15453 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15454 		.doit = nl80211_add_tx_ts,
15455 		.flags = GENL_UNS_ADMIN_PERM,
15456 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15457 				  NL80211_FLAG_NEED_RTNL,
15458 	},
15459 	{
15460 		.cmd = NL80211_CMD_DEL_TX_TS,
15461 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15462 		.doit = nl80211_del_tx_ts,
15463 		.flags = GENL_UNS_ADMIN_PERM,
15464 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15465 				  NL80211_FLAG_NEED_RTNL,
15466 	},
15467 	{
15468 		.cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
15469 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15470 		.doit = nl80211_tdls_channel_switch,
15471 		.flags = GENL_UNS_ADMIN_PERM,
15472 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15473 				  NL80211_FLAG_NEED_RTNL,
15474 	},
15475 	{
15476 		.cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
15477 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15478 		.doit = nl80211_tdls_cancel_channel_switch,
15479 		.flags = GENL_UNS_ADMIN_PERM,
15480 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15481 				  NL80211_FLAG_NEED_RTNL,
15482 	},
15483 	{
15484 		.cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
15485 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15486 		.doit = nl80211_set_multicast_to_unicast,
15487 		.flags = GENL_UNS_ADMIN_PERM,
15488 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15489 				  NL80211_FLAG_NEED_RTNL,
15490 	},
15491 	{
15492 		.cmd = NL80211_CMD_SET_PMK,
15493 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15494 		.doit = nl80211_set_pmk,
15495 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15496 				  NL80211_FLAG_NEED_RTNL |
15497 				  NL80211_FLAG_CLEAR_SKB,
15498 	},
15499 	{
15500 		.cmd = NL80211_CMD_DEL_PMK,
15501 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15502 		.doit = nl80211_del_pmk,
15503 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15504 				  NL80211_FLAG_NEED_RTNL,
15505 	},
15506 	{
15507 		.cmd = NL80211_CMD_EXTERNAL_AUTH,
15508 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15509 		.doit = nl80211_external_auth,
15510 		.flags = GENL_ADMIN_PERM,
15511 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15512 				  NL80211_FLAG_NEED_RTNL,
15513 	},
15514 	{
15515 		.cmd = NL80211_CMD_CONTROL_PORT_FRAME,
15516 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15517 		.doit = nl80211_tx_control_port,
15518 		.flags = GENL_UNS_ADMIN_PERM,
15519 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15520 				  NL80211_FLAG_NEED_RTNL,
15521 	},
15522 	{
15523 		.cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
15524 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15525 		.doit = nl80211_get_ftm_responder_stats,
15526 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15527 				  NL80211_FLAG_NEED_RTNL,
15528 	},
15529 	{
15530 		.cmd = NL80211_CMD_PEER_MEASUREMENT_START,
15531 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15532 		.doit = nl80211_pmsr_start,
15533 		.flags = GENL_UNS_ADMIN_PERM,
15534 		.internal_flags = NL80211_FLAG_NEED_WDEV_UP |
15535 				  NL80211_FLAG_NEED_RTNL,
15536 	},
15537 	{
15538 		.cmd = NL80211_CMD_NOTIFY_RADAR,
15539 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
15540 		.doit = nl80211_notify_radar_detection,
15541 		.flags = GENL_UNS_ADMIN_PERM,
15542 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15543 				  NL80211_FLAG_NEED_RTNL,
15544 	},
15545 	{
15546 		.cmd = NL80211_CMD_UPDATE_OWE_INFO,
15547 		.doit = nl80211_update_owe_info,
15548 		.flags = GENL_ADMIN_PERM,
15549 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15550 				  NL80211_FLAG_NEED_RTNL,
15551 	},
15552 	{
15553 		.cmd = NL80211_CMD_PROBE_MESH_LINK,
15554 		.doit = nl80211_probe_mesh_link,
15555 		.flags = GENL_UNS_ADMIN_PERM,
15556 		.internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
15557 				  NL80211_FLAG_NEED_RTNL,
15558 	},
15559 	{
15560 		.cmd = NL80211_CMD_SET_TID_CONFIG,
15561 		.doit = nl80211_set_tid_config,
15562 		.flags = GENL_UNS_ADMIN_PERM,
15563 		.internal_flags = NL80211_FLAG_NEED_NETDEV |
15564 				  NL80211_FLAG_NEED_RTNL,
15565 	},
15566 };
15567 
15568 static struct genl_family nl80211_fam __ro_after_init = {
15569 	.name = NL80211_GENL_NAME,	/* have users key off the name instead */
15570 	.hdrsize = 0,			/* no private header */
15571 	.version = 1,			/* no particular meaning now */
15572 	.maxattr = NL80211_ATTR_MAX,
15573 	.policy = nl80211_policy,
15574 	.netnsok = true,
15575 	.pre_doit = nl80211_pre_doit,
15576 	.post_doit = nl80211_post_doit,
15577 	.module = THIS_MODULE,
15578 	.ops = nl80211_ops,
15579 	.n_ops = ARRAY_SIZE(nl80211_ops),
15580 	.small_ops = nl80211_small_ops,
15581 	.n_small_ops = ARRAY_SIZE(nl80211_small_ops),
15582 	.mcgrps = nl80211_mcgrps,
15583 	.n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
15584 	.parallel_ops = true,
15585 };
15586 
15587 /* notification functions */
15588 
nl80211_notify_wiphy(struct cfg80211_registered_device * rdev,enum nl80211_commands cmd)15589 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
15590 			  enum nl80211_commands cmd)
15591 {
15592 	struct sk_buff *msg;
15593 	struct nl80211_dump_wiphy_state state = {};
15594 
15595 	WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
15596 		cmd != NL80211_CMD_DEL_WIPHY);
15597 
15598 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15599 	if (!msg)
15600 		return;
15601 
15602 	if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
15603 		nlmsg_free(msg);
15604 		return;
15605 	}
15606 
15607 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15608 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15609 }
15610 
nl80211_notify_iface(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,enum nl80211_commands cmd)15611 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
15612 				struct wireless_dev *wdev,
15613 				enum nl80211_commands cmd)
15614 {
15615 	struct sk_buff *msg;
15616 
15617 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15618 	if (!msg)
15619 		return;
15620 
15621 	if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
15622 		nlmsg_free(msg);
15623 		return;
15624 	}
15625 
15626 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15627 				NL80211_MCGRP_CONFIG, GFP_KERNEL);
15628 }
15629 
nl80211_add_scan_req(struct sk_buff * msg,struct cfg80211_registered_device * rdev)15630 static int nl80211_add_scan_req(struct sk_buff *msg,
15631 				struct cfg80211_registered_device *rdev)
15632 {
15633 	struct cfg80211_scan_request *req = rdev->scan_req;
15634 	struct nlattr *nest;
15635 	int i;
15636 	struct cfg80211_scan_info *info;
15637 
15638 	if (WARN_ON(!req))
15639 		return 0;
15640 
15641 	nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
15642 	if (!nest)
15643 		goto nla_put_failure;
15644 	for (i = 0; i < req->n_ssids; i++) {
15645 		if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
15646 			goto nla_put_failure;
15647 	}
15648 	nla_nest_end(msg, nest);
15649 
15650 	if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
15651 		nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
15652 		if (!nest)
15653 			goto nla_put_failure;
15654 		for (i = 0; i < req->n_channels; i++) {
15655 			if (nla_put_u32(msg, i,
15656 				   ieee80211_channel_to_khz(req->channels[i])))
15657 				goto nla_put_failure;
15658 		}
15659 		nla_nest_end(msg, nest);
15660 	} else {
15661 		nest = nla_nest_start_noflag(msg,
15662 					     NL80211_ATTR_SCAN_FREQUENCIES);
15663 		if (!nest)
15664 			goto nla_put_failure;
15665 		for (i = 0; i < req->n_channels; i++) {
15666 			if (nla_put_u32(msg, i, req->channels[i]->center_freq))
15667 				goto nla_put_failure;
15668 		}
15669 		nla_nest_end(msg, nest);
15670 	}
15671 
15672 	if (req->ie &&
15673 	    nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
15674 		goto nla_put_failure;
15675 
15676 	if (req->flags &&
15677 	    nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
15678 		goto nla_put_failure;
15679 
15680 	info = rdev->int_scan_req ? &rdev->int_scan_req->info :
15681 		&rdev->scan_req->info;
15682 	if (info->scan_start_tsf &&
15683 	    (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
15684 			       info->scan_start_tsf, NL80211_BSS_PAD) ||
15685 	     nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
15686 		     info->tsf_bssid)))
15687 		goto nla_put_failure;
15688 
15689 	return 0;
15690  nla_put_failure:
15691 	return -ENOBUFS;
15692 }
15693 
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)15694 static int nl80211_prep_scan_msg(struct sk_buff *msg,
15695 				 struct cfg80211_registered_device *rdev,
15696 				 struct wireless_dev *wdev,
15697 				 u32 portid, u32 seq, int flags,
15698 				 u32 cmd)
15699 {
15700 	void *hdr;
15701 
15702 	hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
15703 	if (!hdr)
15704 		return -1;
15705 
15706 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15707 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15708 					 wdev->netdev->ifindex)) ||
15709 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15710 			      NL80211_ATTR_PAD))
15711 		goto nla_put_failure;
15712 
15713 	/* ignore errors and send incomplete event anyway */
15714 	nl80211_add_scan_req(msg, rdev);
15715 
15716 	genlmsg_end(msg, hdr);
15717 	return 0;
15718 
15719  nla_put_failure:
15720 	genlmsg_cancel(msg, hdr);
15721 	return -EMSGSIZE;
15722 }
15723 
15724 static int
nl80211_prep_sched_scan_msg(struct sk_buff * msg,struct cfg80211_sched_scan_request * req,u32 cmd)15725 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
15726 			    struct cfg80211_sched_scan_request *req, u32 cmd)
15727 {
15728 	void *hdr;
15729 
15730 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15731 	if (!hdr)
15732 		return -1;
15733 
15734 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
15735 			wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
15736 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
15737 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
15738 			      NL80211_ATTR_PAD))
15739 		goto nla_put_failure;
15740 
15741 	genlmsg_end(msg, hdr);
15742 	return 0;
15743 
15744  nla_put_failure:
15745 	genlmsg_cancel(msg, hdr);
15746 	return -EMSGSIZE;
15747 }
15748 
nl80211_send_scan_start(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev)15749 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
15750 			     struct wireless_dev *wdev)
15751 {
15752 	struct sk_buff *msg;
15753 
15754 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15755 	if (!msg)
15756 		return;
15757 
15758 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
15759 				  NL80211_CMD_TRIGGER_SCAN) < 0) {
15760 		nlmsg_free(msg);
15761 		return;
15762 	}
15763 
15764 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15765 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15766 }
15767 
nl80211_build_scan_msg(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,bool aborted)15768 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
15769 				       struct wireless_dev *wdev, bool aborted)
15770 {
15771 	struct sk_buff *msg;
15772 
15773 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15774 	if (!msg)
15775 		return NULL;
15776 
15777 	if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
15778 				  aborted ? NL80211_CMD_SCAN_ABORTED :
15779 					    NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
15780 		nlmsg_free(msg);
15781 		return NULL;
15782 	}
15783 
15784 	return msg;
15785 }
15786 
15787 /* send message created by nl80211_build_scan_msg() */
nl80211_send_scan_msg(struct cfg80211_registered_device * rdev,struct sk_buff * msg)15788 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
15789 			   struct sk_buff *msg)
15790 {
15791 	if (!msg)
15792 		return;
15793 
15794 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15795 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15796 }
15797 
nl80211_send_sched_scan(struct cfg80211_sched_scan_request * req,u32 cmd)15798 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
15799 {
15800 	struct sk_buff *msg;
15801 
15802 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15803 	if (!msg)
15804 		return;
15805 
15806 	if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
15807 		nlmsg_free(msg);
15808 		return;
15809 	}
15810 
15811 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
15812 				NL80211_MCGRP_SCAN, GFP_KERNEL);
15813 }
15814 
nl80211_reg_change_event_fill(struct sk_buff * msg,struct regulatory_request * request)15815 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
15816 					  struct regulatory_request *request)
15817 {
15818 	/* Userspace can always count this one always being set */
15819 	if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
15820 		goto nla_put_failure;
15821 
15822 	if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
15823 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15824 			       NL80211_REGDOM_TYPE_WORLD))
15825 			goto nla_put_failure;
15826 	} else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
15827 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15828 			       NL80211_REGDOM_TYPE_CUSTOM_WORLD))
15829 			goto nla_put_failure;
15830 	} else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
15831 		   request->intersect) {
15832 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15833 			       NL80211_REGDOM_TYPE_INTERSECTION))
15834 			goto nla_put_failure;
15835 	} else {
15836 		if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15837 			       NL80211_REGDOM_TYPE_COUNTRY) ||
15838 		    nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15839 				   request->alpha2))
15840 			goto nla_put_failure;
15841 	}
15842 
15843 	if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15844 		struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15845 
15846 		if (wiphy &&
15847 		    nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15848 			goto nla_put_failure;
15849 
15850 		if (wiphy &&
15851 		    wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15852 		    nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15853 			goto nla_put_failure;
15854 	}
15855 
15856 	return true;
15857 
15858 nla_put_failure:
15859 	return false;
15860 }
15861 
15862 /*
15863  * This can happen on global regulatory changes or device specific settings
15864  * based on custom regulatory domains.
15865  */
nl80211_common_reg_change_event(enum nl80211_commands cmd_id,struct regulatory_request * request)15866 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15867 				     struct regulatory_request *request)
15868 {
15869 	struct sk_buff *msg;
15870 	void *hdr;
15871 
15872 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15873 	if (!msg)
15874 		return;
15875 
15876 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15877 	if (!hdr)
15878 		goto nla_put_failure;
15879 
15880 	if (!nl80211_reg_change_event_fill(msg, request))
15881 		goto nla_put_failure;
15882 
15883 	genlmsg_end(msg, hdr);
15884 
15885 	rcu_read_lock();
15886 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15887 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15888 	rcu_read_unlock();
15889 
15890 	return;
15891 
15892 nla_put_failure:
15893 	nlmsg_free(msg);
15894 }
15895 
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)15896 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15897 				    struct net_device *netdev,
15898 				    const u8 *buf, size_t len,
15899 				    enum nl80211_commands cmd, gfp_t gfp,
15900 				    int uapsd_queues, const u8 *req_ies,
15901 				    size_t req_ies_len)
15902 {
15903 	struct sk_buff *msg;
15904 	void *hdr;
15905 
15906 	msg = nlmsg_new(100 + len + req_ies_len, gfp);
15907 	if (!msg)
15908 		return;
15909 
15910 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15911 	if (!hdr) {
15912 		nlmsg_free(msg);
15913 		return;
15914 	}
15915 
15916 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15917 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15918 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15919 	    (req_ies &&
15920 	     nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15921 		goto nla_put_failure;
15922 
15923 	if (uapsd_queues >= 0) {
15924 		struct nlattr *nla_wmm =
15925 			nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15926 		if (!nla_wmm)
15927 			goto nla_put_failure;
15928 
15929 		if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15930 			       uapsd_queues))
15931 			goto nla_put_failure;
15932 
15933 		nla_nest_end(msg, nla_wmm);
15934 	}
15935 
15936 	genlmsg_end(msg, hdr);
15937 
15938 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15939 				NL80211_MCGRP_MLME, gfp);
15940 	return;
15941 
15942  nla_put_failure:
15943 	nlmsg_free(msg);
15944 }
15945 
nl80211_send_rx_auth(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15946 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15947 			  struct net_device *netdev, const u8 *buf,
15948 			  size_t len, gfp_t gfp)
15949 {
15950 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15951 				NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15952 }
15953 
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)15954 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15955 			   struct net_device *netdev, const u8 *buf,
15956 			   size_t len, gfp_t gfp, int uapsd_queues,
15957 			   const u8 *req_ies, size_t req_ies_len)
15958 {
15959 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15960 				NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15961 				req_ies, req_ies_len);
15962 }
15963 
nl80211_send_deauth(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15964 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15965 			 struct net_device *netdev, const u8 *buf,
15966 			 size_t len, gfp_t gfp)
15967 {
15968 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15969 				NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15970 }
15971 
nl80211_send_disassoc(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * buf,size_t len,gfp_t gfp)15972 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15973 			   struct net_device *netdev, const u8 *buf,
15974 			   size_t len, gfp_t gfp)
15975 {
15976 	nl80211_send_mlme_event(rdev, netdev, buf, len,
15977 				NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15978 }
15979 
cfg80211_rx_unprot_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len)15980 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15981 				  size_t len)
15982 {
15983 	struct wireless_dev *wdev = dev->ieee80211_ptr;
15984 	struct wiphy *wiphy = wdev->wiphy;
15985 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15986 	const struct ieee80211_mgmt *mgmt = (void *)buf;
15987 	u32 cmd;
15988 
15989 	if (WARN_ON(len < 2))
15990 		return;
15991 
15992 	if (ieee80211_is_deauth(mgmt->frame_control)) {
15993 		cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15994 	} else if (ieee80211_is_disassoc(mgmt->frame_control)) {
15995 		cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15996 	} else if (ieee80211_is_beacon(mgmt->frame_control)) {
15997 		if (wdev->unprot_beacon_reported &&
15998 		    elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
15999 			return;
16000 		cmd = NL80211_CMD_UNPROT_BEACON;
16001 		wdev->unprot_beacon_reported = jiffies;
16002 	} else {
16003 		return;
16004 	}
16005 
16006 	trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
16007 	nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
16008 				NULL, 0);
16009 }
16010 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
16011 
nl80211_send_mlme_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,int cmd,const u8 * addr,gfp_t gfp)16012 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
16013 				      struct net_device *netdev, int cmd,
16014 				      const u8 *addr, gfp_t gfp)
16015 {
16016 	struct sk_buff *msg;
16017 	void *hdr;
16018 
16019 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16020 	if (!msg)
16021 		return;
16022 
16023 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16024 	if (!hdr) {
16025 		nlmsg_free(msg);
16026 		return;
16027 	}
16028 
16029 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16030 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16031 	    nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
16032 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
16033 		goto nla_put_failure;
16034 
16035 	genlmsg_end(msg, hdr);
16036 
16037 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16038 				NL80211_MCGRP_MLME, gfp);
16039 	return;
16040 
16041  nla_put_failure:
16042 	nlmsg_free(msg);
16043 }
16044 
nl80211_send_auth_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * addr,gfp_t gfp)16045 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
16046 			       struct net_device *netdev, const u8 *addr,
16047 			       gfp_t gfp)
16048 {
16049 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
16050 				  addr, gfp);
16051 }
16052 
nl80211_send_assoc_timeout(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * addr,gfp_t gfp)16053 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
16054 				struct net_device *netdev, const u8 *addr,
16055 				gfp_t gfp)
16056 {
16057 	nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
16058 				  addr, gfp);
16059 }
16060 
nl80211_send_connect_result(struct cfg80211_registered_device * rdev,struct net_device * netdev,struct cfg80211_connect_resp_params * cr,gfp_t gfp)16061 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
16062 				 struct net_device *netdev,
16063 				 struct cfg80211_connect_resp_params *cr,
16064 				 gfp_t gfp)
16065 {
16066 	struct sk_buff *msg;
16067 	void *hdr;
16068 
16069 	msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
16070 			cr->fils.kek_len + cr->fils.pmk_len +
16071 			(cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
16072 	if (!msg)
16073 		return;
16074 
16075 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
16076 	if (!hdr) {
16077 		nlmsg_free(msg);
16078 		return;
16079 	}
16080 
16081 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16082 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16083 	    (cr->bssid &&
16084 	     nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
16085 	    nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
16086 			cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
16087 			cr->status) ||
16088 	    (cr->status < 0 &&
16089 	     (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
16090 	      nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
16091 			  cr->timeout_reason))) ||
16092 	    (cr->req_ie &&
16093 	     nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
16094 	    (cr->resp_ie &&
16095 	     nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
16096 		     cr->resp_ie)) ||
16097 	    (cr->fils.update_erp_next_seq_num &&
16098 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
16099 			 cr->fils.erp_next_seq_num)) ||
16100 	    (cr->status == WLAN_STATUS_SUCCESS &&
16101 	     ((cr->fils.kek &&
16102 	       nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
16103 		       cr->fils.kek)) ||
16104 	      (cr->fils.pmk &&
16105 	       nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
16106 	      (cr->fils.pmkid &&
16107 	       nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
16108 		goto nla_put_failure;
16109 
16110 	genlmsg_end(msg, hdr);
16111 
16112 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16113 				NL80211_MCGRP_MLME, gfp);
16114 	return;
16115 
16116  nla_put_failure:
16117 	nlmsg_free(msg);
16118 }
16119 
nl80211_send_roamed(struct cfg80211_registered_device * rdev,struct net_device * netdev,struct cfg80211_roam_info * info,gfp_t gfp)16120 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
16121 			 struct net_device *netdev,
16122 			 struct cfg80211_roam_info *info, gfp_t gfp)
16123 {
16124 	struct sk_buff *msg;
16125 	void *hdr;
16126 	const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
16127 
16128 	msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
16129 			info->fils.kek_len + info->fils.pmk_len +
16130 			(info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
16131 	if (!msg)
16132 		return;
16133 
16134 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
16135 	if (!hdr) {
16136 		nlmsg_free(msg);
16137 		return;
16138 	}
16139 
16140 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16141 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16142 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
16143 	    (info->req_ie &&
16144 	     nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
16145 		     info->req_ie)) ||
16146 	    (info->resp_ie &&
16147 	     nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
16148 		     info->resp_ie)) ||
16149 	    (info->fils.update_erp_next_seq_num &&
16150 	     nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
16151 			 info->fils.erp_next_seq_num)) ||
16152 	    (info->fils.kek &&
16153 	     nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
16154 		     info->fils.kek)) ||
16155 	    (info->fils.pmk &&
16156 	     nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
16157 	    (info->fils.pmkid &&
16158 	     nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
16159 		goto nla_put_failure;
16160 
16161 	genlmsg_end(msg, hdr);
16162 
16163 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16164 				NL80211_MCGRP_MLME, gfp);
16165 	return;
16166 
16167  nla_put_failure:
16168 	nlmsg_free(msg);
16169 }
16170 
nl80211_send_port_authorized(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid)16171 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
16172 				  struct net_device *netdev, const u8 *bssid)
16173 {
16174 	struct sk_buff *msg;
16175 	void *hdr;
16176 
16177 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16178 	if (!msg)
16179 		return;
16180 
16181 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
16182 	if (!hdr) {
16183 		nlmsg_free(msg);
16184 		return;
16185 	}
16186 
16187 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16188 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16189 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16190 		goto nla_put_failure;
16191 
16192 	genlmsg_end(msg, hdr);
16193 
16194 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16195 				NL80211_MCGRP_MLME, GFP_KERNEL);
16196 	return;
16197 
16198  nla_put_failure:
16199 	nlmsg_free(msg);
16200 }
16201 
nl80211_send_disconnected(struct cfg80211_registered_device * rdev,struct net_device * netdev,u16 reason,const u8 * ie,size_t ie_len,bool from_ap)16202 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
16203 			       struct net_device *netdev, u16 reason,
16204 			       const u8 *ie, size_t ie_len, bool from_ap)
16205 {
16206 	struct sk_buff *msg;
16207 	void *hdr;
16208 
16209 	msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
16210 	if (!msg)
16211 		return;
16212 
16213 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
16214 	if (!hdr) {
16215 		nlmsg_free(msg);
16216 		return;
16217 	}
16218 
16219 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16220 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16221 	    (reason &&
16222 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
16223 	    (from_ap &&
16224 	     nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
16225 	    (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
16226 		goto nla_put_failure;
16227 
16228 	genlmsg_end(msg, hdr);
16229 
16230 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16231 				NL80211_MCGRP_MLME, GFP_KERNEL);
16232 	return;
16233 
16234  nla_put_failure:
16235 	nlmsg_free(msg);
16236 }
16237 
nl80211_send_ibss_bssid(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid,gfp_t gfp)16238 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
16239 			     struct net_device *netdev, const u8 *bssid,
16240 			     gfp_t gfp)
16241 {
16242 	struct sk_buff *msg;
16243 	void *hdr;
16244 
16245 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16246 	if (!msg)
16247 		return;
16248 
16249 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
16250 	if (!hdr) {
16251 		nlmsg_free(msg);
16252 		return;
16253 	}
16254 
16255 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16256 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16257 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16258 		goto nla_put_failure;
16259 
16260 	genlmsg_end(msg, hdr);
16261 
16262 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16263 				NL80211_MCGRP_MLME, gfp);
16264 	return;
16265 
16266  nla_put_failure:
16267 	nlmsg_free(msg);
16268 }
16269 
cfg80211_notify_new_peer_candidate(struct net_device * dev,const u8 * addr,const u8 * ie,u8 ie_len,int sig_dbm,gfp_t gfp)16270 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
16271 					const u8 *ie, u8 ie_len,
16272 					int sig_dbm, gfp_t gfp)
16273 {
16274 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16275 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16276 	struct sk_buff *msg;
16277 	void *hdr;
16278 
16279 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
16280 		return;
16281 
16282 	trace_cfg80211_notify_new_peer_candidate(dev, addr);
16283 
16284 	msg = nlmsg_new(100 + ie_len, gfp);
16285 	if (!msg)
16286 		return;
16287 
16288 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
16289 	if (!hdr) {
16290 		nlmsg_free(msg);
16291 		return;
16292 	}
16293 
16294 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16295 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16296 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16297 	    (ie_len && ie &&
16298 	     nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
16299 	    (sig_dbm &&
16300 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
16301 		goto nla_put_failure;
16302 
16303 	genlmsg_end(msg, hdr);
16304 
16305 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16306 				NL80211_MCGRP_MLME, gfp);
16307 	return;
16308 
16309  nla_put_failure:
16310 	nlmsg_free(msg);
16311 }
16312 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
16313 
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)16314 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
16315 				 struct net_device *netdev, const u8 *addr,
16316 				 enum nl80211_key_type key_type, int key_id,
16317 				 const u8 *tsc, gfp_t gfp)
16318 {
16319 	struct sk_buff *msg;
16320 	void *hdr;
16321 
16322 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16323 	if (!msg)
16324 		return;
16325 
16326 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
16327 	if (!hdr) {
16328 		nlmsg_free(msg);
16329 		return;
16330 	}
16331 
16332 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16333 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16334 	    (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
16335 	    nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
16336 	    (key_id != -1 &&
16337 	     nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
16338 	    (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
16339 		goto nla_put_failure;
16340 
16341 	genlmsg_end(msg, hdr);
16342 
16343 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16344 				NL80211_MCGRP_MLME, gfp);
16345 	return;
16346 
16347  nla_put_failure:
16348 	nlmsg_free(msg);
16349 }
16350 
nl80211_send_beacon_hint_event(struct wiphy * wiphy,struct ieee80211_channel * channel_before,struct ieee80211_channel * channel_after)16351 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
16352 				    struct ieee80211_channel *channel_before,
16353 				    struct ieee80211_channel *channel_after)
16354 {
16355 	struct sk_buff *msg;
16356 	void *hdr;
16357 	struct nlattr *nl_freq;
16358 
16359 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
16360 	if (!msg)
16361 		return;
16362 
16363 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
16364 	if (!hdr) {
16365 		nlmsg_free(msg);
16366 		return;
16367 	}
16368 
16369 	/*
16370 	 * Since we are applying the beacon hint to a wiphy we know its
16371 	 * wiphy_idx is valid
16372 	 */
16373 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
16374 		goto nla_put_failure;
16375 
16376 	/* Before */
16377 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
16378 	if (!nl_freq)
16379 		goto nla_put_failure;
16380 
16381 	if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
16382 		goto nla_put_failure;
16383 	nla_nest_end(msg, nl_freq);
16384 
16385 	/* After */
16386 	nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
16387 	if (!nl_freq)
16388 		goto nla_put_failure;
16389 
16390 	if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
16391 		goto nla_put_failure;
16392 	nla_nest_end(msg, nl_freq);
16393 
16394 	genlmsg_end(msg, hdr);
16395 
16396 	rcu_read_lock();
16397 	genlmsg_multicast_allns(&nl80211_fam, msg, 0,
16398 				NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
16399 	rcu_read_unlock();
16400 
16401 	return;
16402 
16403 nla_put_failure:
16404 	nlmsg_free(msg);
16405 }
16406 
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)16407 static void nl80211_send_remain_on_chan_event(
16408 	int cmd, struct cfg80211_registered_device *rdev,
16409 	struct wireless_dev *wdev, u64 cookie,
16410 	struct ieee80211_channel *chan,
16411 	unsigned int duration, gfp_t gfp)
16412 {
16413 	struct sk_buff *msg;
16414 	void *hdr;
16415 
16416 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16417 	if (!msg)
16418 		return;
16419 
16420 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16421 	if (!hdr) {
16422 		nlmsg_free(msg);
16423 		return;
16424 	}
16425 
16426 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16427 	    (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16428 					 wdev->netdev->ifindex)) ||
16429 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16430 			      NL80211_ATTR_PAD) ||
16431 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
16432 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
16433 			NL80211_CHAN_NO_HT) ||
16434 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16435 			      NL80211_ATTR_PAD))
16436 		goto nla_put_failure;
16437 
16438 	if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
16439 	    nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
16440 		goto nla_put_failure;
16441 
16442 	genlmsg_end(msg, hdr);
16443 
16444 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16445 				NL80211_MCGRP_MLME, gfp);
16446 	return;
16447 
16448  nla_put_failure:
16449 	nlmsg_free(msg);
16450 }
16451 
cfg80211_ready_on_channel(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,unsigned int duration,gfp_t gfp)16452 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
16453 			       struct ieee80211_channel *chan,
16454 			       unsigned int duration, gfp_t gfp)
16455 {
16456 	struct wiphy *wiphy = wdev->wiphy;
16457 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16458 
16459 	trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
16460 	nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
16461 					  rdev, wdev, cookie, chan,
16462 					  duration, gfp);
16463 }
16464 EXPORT_SYMBOL(cfg80211_ready_on_channel);
16465 
cfg80211_remain_on_channel_expired(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,gfp_t gfp)16466 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
16467 					struct ieee80211_channel *chan,
16468 					gfp_t gfp)
16469 {
16470 	struct wiphy *wiphy = wdev->wiphy;
16471 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16472 
16473 	trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
16474 	nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16475 					  rdev, wdev, cookie, chan, 0, gfp);
16476 }
16477 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
16478 
cfg80211_tx_mgmt_expired(struct wireless_dev * wdev,u64 cookie,struct ieee80211_channel * chan,gfp_t gfp)16479 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
16480 					struct ieee80211_channel *chan,
16481 					gfp_t gfp)
16482 {
16483 	struct wiphy *wiphy = wdev->wiphy;
16484 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16485 
16486 	trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
16487 	nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
16488 					  rdev, wdev, cookie, chan, 0, gfp);
16489 }
16490 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
16491 
cfg80211_new_sta(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo,gfp_t gfp)16492 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
16493 		      struct station_info *sinfo, gfp_t gfp)
16494 {
16495 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16496 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16497 	struct sk_buff *msg;
16498 
16499 	trace_cfg80211_new_sta(dev, mac_addr, sinfo);
16500 
16501 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16502 	if (!msg)
16503 		return;
16504 
16505 	if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
16506 				 rdev, dev, mac_addr, sinfo) < 0) {
16507 		nlmsg_free(msg);
16508 		return;
16509 	}
16510 
16511 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16512 				NL80211_MCGRP_MLME, gfp);
16513 }
16514 EXPORT_SYMBOL(cfg80211_new_sta);
16515 
cfg80211_del_sta_sinfo(struct net_device * dev,const u8 * mac_addr,struct station_info * sinfo,gfp_t gfp)16516 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
16517 			    struct station_info *sinfo, gfp_t gfp)
16518 {
16519 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16520 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16521 	struct sk_buff *msg;
16522 	struct station_info empty_sinfo = {};
16523 
16524 	if (!sinfo)
16525 		sinfo = &empty_sinfo;
16526 
16527 	trace_cfg80211_del_sta(dev, mac_addr);
16528 
16529 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16530 	if (!msg) {
16531 		cfg80211_sinfo_release_content(sinfo);
16532 		return;
16533 	}
16534 
16535 	if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
16536 				 rdev, dev, mac_addr, sinfo) < 0) {
16537 		nlmsg_free(msg);
16538 		return;
16539 	}
16540 
16541 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16542 				NL80211_MCGRP_MLME, gfp);
16543 }
16544 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
16545 
cfg80211_conn_failed(struct net_device * dev,const u8 * mac_addr,enum nl80211_connect_failed_reason reason,gfp_t gfp)16546 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
16547 			  enum nl80211_connect_failed_reason reason,
16548 			  gfp_t gfp)
16549 {
16550 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
16551 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16552 	struct sk_buff *msg;
16553 	void *hdr;
16554 
16555 	msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
16556 	if (!msg)
16557 		return;
16558 
16559 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
16560 	if (!hdr) {
16561 		nlmsg_free(msg);
16562 		return;
16563 	}
16564 
16565 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16566 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
16567 	    nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
16568 		goto nla_put_failure;
16569 
16570 	genlmsg_end(msg, hdr);
16571 
16572 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16573 				NL80211_MCGRP_MLME, gfp);
16574 	return;
16575 
16576  nla_put_failure:
16577 	nlmsg_free(msg);
16578 }
16579 EXPORT_SYMBOL(cfg80211_conn_failed);
16580 
__nl80211_unexpected_frame(struct net_device * dev,u8 cmd,const u8 * addr,gfp_t gfp)16581 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
16582 				       const u8 *addr, gfp_t gfp)
16583 {
16584 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16585 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16586 	struct sk_buff *msg;
16587 	void *hdr;
16588 	u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
16589 
16590 	if (!nlportid)
16591 		return false;
16592 
16593 	msg = nlmsg_new(100, gfp);
16594 	if (!msg)
16595 		return true;
16596 
16597 	hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
16598 	if (!hdr) {
16599 		nlmsg_free(msg);
16600 		return true;
16601 	}
16602 
16603 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16604 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16605 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
16606 		goto nla_put_failure;
16607 
16608 	genlmsg_end(msg, hdr);
16609 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16610 	return true;
16611 
16612  nla_put_failure:
16613 	nlmsg_free(msg);
16614 	return true;
16615 }
16616 
cfg80211_rx_spurious_frame(struct net_device * dev,const u8 * addr,gfp_t gfp)16617 bool cfg80211_rx_spurious_frame(struct net_device *dev,
16618 				const u8 *addr, gfp_t gfp)
16619 {
16620 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16621 	bool ret;
16622 
16623 	trace_cfg80211_rx_spurious_frame(dev, addr);
16624 
16625 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16626 		    wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
16627 		trace_cfg80211_return_bool(false);
16628 		return false;
16629 	}
16630 	ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
16631 					 addr, gfp);
16632 	trace_cfg80211_return_bool(ret);
16633 	return ret;
16634 }
16635 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
16636 
cfg80211_rx_unexpected_4addr_frame(struct net_device * dev,const u8 * addr,gfp_t gfp)16637 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
16638 					const u8 *addr, gfp_t gfp)
16639 {
16640 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16641 	bool ret;
16642 
16643 	trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
16644 
16645 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
16646 		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
16647 		    wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
16648 		trace_cfg80211_return_bool(false);
16649 		return false;
16650 	}
16651 	ret = __nl80211_unexpected_frame(dev,
16652 					 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
16653 					 addr, gfp);
16654 	trace_cfg80211_return_bool(ret);
16655 	return ret;
16656 }
16657 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
16658 
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)16659 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
16660 		      struct wireless_dev *wdev, u32 nlportid,
16661 		      int freq, int sig_dbm,
16662 		      const u8 *buf, size_t len, u32 flags, gfp_t gfp)
16663 {
16664 	struct net_device *netdev = wdev->netdev;
16665 	struct sk_buff *msg;
16666 	void *hdr;
16667 
16668 	msg = nlmsg_new(100 + len, gfp);
16669 	if (!msg)
16670 		return -ENOMEM;
16671 
16672 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16673 	if (!hdr) {
16674 		nlmsg_free(msg);
16675 		return -ENOMEM;
16676 	}
16677 
16678 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16679 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16680 					netdev->ifindex)) ||
16681 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16682 			      NL80211_ATTR_PAD) ||
16683 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(freq)) ||
16684 	    nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, freq % 1000) ||
16685 	    (sig_dbm &&
16686 	     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16687 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16688 	    (flags &&
16689 	     nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
16690 		goto nla_put_failure;
16691 
16692 	genlmsg_end(msg, hdr);
16693 
16694 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16695 
16696  nla_put_failure:
16697 	nlmsg_free(msg);
16698 	return -ENOBUFS;
16699 }
16700 
nl80211_frame_tx_status(struct wireless_dev * wdev,u64 cookie,const u8 * buf,size_t len,bool ack,gfp_t gfp,enum nl80211_commands command)16701 static void nl80211_frame_tx_status(struct wireless_dev *wdev, u64 cookie,
16702 				    const u8 *buf, size_t len, bool ack,
16703 				    gfp_t gfp, enum nl80211_commands command)
16704 {
16705 	struct wiphy *wiphy = wdev->wiphy;
16706 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16707 	struct net_device *netdev = wdev->netdev;
16708 	struct sk_buff *msg;
16709 	void *hdr;
16710 
16711 	if (command == NL80211_CMD_FRAME_TX_STATUS)
16712 		trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
16713 	else
16714 		trace_cfg80211_control_port_tx_status(wdev, cookie, ack);
16715 
16716 	msg = nlmsg_new(100 + len, gfp);
16717 	if (!msg)
16718 		return;
16719 
16720 	hdr = nl80211hdr_put(msg, 0, 0, 0, command);
16721 	if (!hdr) {
16722 		nlmsg_free(msg);
16723 		return;
16724 	}
16725 
16726 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16727 	    (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16728 				   netdev->ifindex)) ||
16729 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16730 			      NL80211_ATTR_PAD) ||
16731 	    nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
16732 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16733 			      NL80211_ATTR_PAD) ||
16734 	    (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
16735 		goto nla_put_failure;
16736 
16737 	genlmsg_end(msg, hdr);
16738 
16739 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16740 				NL80211_MCGRP_MLME, gfp);
16741 	return;
16742 
16743 nla_put_failure:
16744 	nlmsg_free(msg);
16745 }
16746 
cfg80211_control_port_tx_status(struct wireless_dev * wdev,u64 cookie,const u8 * buf,size_t len,bool ack,gfp_t gfp)16747 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
16748 				     const u8 *buf, size_t len, bool ack,
16749 				     gfp_t gfp)
16750 {
16751 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
16752 				NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
16753 }
16754 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
16755 
cfg80211_mgmt_tx_status(struct wireless_dev * wdev,u64 cookie,const u8 * buf,size_t len,bool ack,gfp_t gfp)16756 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
16757 			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
16758 {
16759 	nl80211_frame_tx_status(wdev, cookie, buf, len, ack, gfp,
16760 				NL80211_CMD_FRAME_TX_STATUS);
16761 }
16762 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
16763 
__nl80211_rx_control_port(struct net_device * dev,struct sk_buff * skb,bool unencrypted,gfp_t gfp)16764 static int __nl80211_rx_control_port(struct net_device *dev,
16765 				     struct sk_buff *skb,
16766 				     bool unencrypted, gfp_t gfp)
16767 {
16768 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16769 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16770 	struct ethhdr *ehdr = eth_hdr(skb);
16771 	const u8 *addr = ehdr->h_source;
16772 	u16 proto = be16_to_cpu(skb->protocol);
16773 	struct sk_buff *msg;
16774 	void *hdr;
16775 	struct nlattr *frame;
16776 
16777 	u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
16778 
16779 	if (!nlportid)
16780 		return -ENOENT;
16781 
16782 	msg = nlmsg_new(100 + skb->len, gfp);
16783 	if (!msg)
16784 		return -ENOMEM;
16785 
16786 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
16787 	if (!hdr) {
16788 		nlmsg_free(msg);
16789 		return -ENOBUFS;
16790 	}
16791 
16792 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16793 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16794 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16795 			      NL80211_ATTR_PAD) ||
16796 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16797 	    nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
16798 	    (unencrypted && nla_put_flag(msg,
16799 					 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
16800 		goto nla_put_failure;
16801 
16802 	frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
16803 	if (!frame)
16804 		goto nla_put_failure;
16805 
16806 	skb_copy_bits(skb, 0, nla_data(frame), skb->len);
16807 	genlmsg_end(msg, hdr);
16808 
16809 	return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16810 
16811  nla_put_failure:
16812 	nlmsg_free(msg);
16813 	return -ENOBUFS;
16814 }
16815 
cfg80211_rx_control_port(struct net_device * dev,struct sk_buff * skb,bool unencrypted)16816 bool cfg80211_rx_control_port(struct net_device *dev,
16817 			      struct sk_buff *skb, bool unencrypted)
16818 {
16819 	int ret;
16820 
16821 	trace_cfg80211_rx_control_port(dev, skb, unencrypted);
16822 	ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
16823 	trace_cfg80211_return_bool(ret == 0);
16824 	return ret == 0;
16825 }
16826 EXPORT_SYMBOL(cfg80211_rx_control_port);
16827 
cfg80211_prepare_cqm(struct net_device * dev,const char * mac,gfp_t gfp)16828 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
16829 					    const char *mac, gfp_t gfp)
16830 {
16831 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16832 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16833 	struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16834 	void **cb;
16835 
16836 	if (!msg)
16837 		return NULL;
16838 
16839 	cb = (void **)msg->cb;
16840 
16841 	cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
16842 	if (!cb[0]) {
16843 		nlmsg_free(msg);
16844 		return NULL;
16845 	}
16846 
16847 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16848 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16849 		goto nla_put_failure;
16850 
16851 	if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16852 		goto nla_put_failure;
16853 
16854 	cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
16855 	if (!cb[1])
16856 		goto nla_put_failure;
16857 
16858 	cb[2] = rdev;
16859 
16860 	return msg;
16861  nla_put_failure:
16862 	nlmsg_free(msg);
16863 	return NULL;
16864 }
16865 
cfg80211_send_cqm(struct sk_buff * msg,gfp_t gfp)16866 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16867 {
16868 	void **cb = (void **)msg->cb;
16869 	struct cfg80211_registered_device *rdev = cb[2];
16870 
16871 	nla_nest_end(msg, cb[1]);
16872 	genlmsg_end(msg, cb[0]);
16873 
16874 	memset(msg->cb, 0, sizeof(msg->cb));
16875 
16876 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16877 				NL80211_MCGRP_MLME, gfp);
16878 }
16879 
cfg80211_cqm_rssi_notify(struct net_device * dev,enum nl80211_cqm_rssi_threshold_event rssi_event,s32 rssi_level,gfp_t gfp)16880 void cfg80211_cqm_rssi_notify(struct net_device *dev,
16881 			      enum nl80211_cqm_rssi_threshold_event rssi_event,
16882 			      s32 rssi_level, gfp_t gfp)
16883 {
16884 	struct sk_buff *msg;
16885 	struct wireless_dev *wdev = dev->ieee80211_ptr;
16886 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16887 
16888 	trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16889 
16890 	if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16891 		    rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16892 		return;
16893 
16894 	if (wdev->cqm_config) {
16895 		wdev->cqm_config->last_rssi_event_value = rssi_level;
16896 
16897 		cfg80211_cqm_rssi_update(rdev, dev);
16898 
16899 		if (rssi_level == 0)
16900 			rssi_level = wdev->cqm_config->last_rssi_event_value;
16901 	}
16902 
16903 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16904 	if (!msg)
16905 		return;
16906 
16907 	if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16908 			rssi_event))
16909 		goto nla_put_failure;
16910 
16911 	if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16912 				      rssi_level))
16913 		goto nla_put_failure;
16914 
16915 	cfg80211_send_cqm(msg, gfp);
16916 
16917 	return;
16918 
16919  nla_put_failure:
16920 	nlmsg_free(msg);
16921 }
16922 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16923 
cfg80211_cqm_txe_notify(struct net_device * dev,const u8 * peer,u32 num_packets,u32 rate,u32 intvl,gfp_t gfp)16924 void cfg80211_cqm_txe_notify(struct net_device *dev,
16925 			     const u8 *peer, u32 num_packets,
16926 			     u32 rate, u32 intvl, gfp_t gfp)
16927 {
16928 	struct sk_buff *msg;
16929 
16930 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16931 	if (!msg)
16932 		return;
16933 
16934 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16935 		goto nla_put_failure;
16936 
16937 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16938 		goto nla_put_failure;
16939 
16940 	if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16941 		goto nla_put_failure;
16942 
16943 	cfg80211_send_cqm(msg, gfp);
16944 	return;
16945 
16946  nla_put_failure:
16947 	nlmsg_free(msg);
16948 }
16949 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16950 
cfg80211_cqm_pktloss_notify(struct net_device * dev,const u8 * peer,u32 num_packets,gfp_t gfp)16951 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16952 				 const u8 *peer, u32 num_packets, gfp_t gfp)
16953 {
16954 	struct sk_buff *msg;
16955 
16956 	trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16957 
16958 	msg = cfg80211_prepare_cqm(dev, peer, gfp);
16959 	if (!msg)
16960 		return;
16961 
16962 	if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16963 		goto nla_put_failure;
16964 
16965 	cfg80211_send_cqm(msg, gfp);
16966 	return;
16967 
16968  nla_put_failure:
16969 	nlmsg_free(msg);
16970 }
16971 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16972 
cfg80211_cqm_beacon_loss_notify(struct net_device * dev,gfp_t gfp)16973 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16974 {
16975 	struct sk_buff *msg;
16976 
16977 	msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16978 	if (!msg)
16979 		return;
16980 
16981 	if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16982 		goto nla_put_failure;
16983 
16984 	cfg80211_send_cqm(msg, gfp);
16985 	return;
16986 
16987  nla_put_failure:
16988 	nlmsg_free(msg);
16989 }
16990 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16991 
nl80211_gtk_rekey_notify(struct cfg80211_registered_device * rdev,struct net_device * netdev,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)16992 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16993 				     struct net_device *netdev, const u8 *bssid,
16994 				     const u8 *replay_ctr, gfp_t gfp)
16995 {
16996 	struct sk_buff *msg;
16997 	struct nlattr *rekey_attr;
16998 	void *hdr;
16999 
17000 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17001 	if (!msg)
17002 		return;
17003 
17004 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
17005 	if (!hdr) {
17006 		nlmsg_free(msg);
17007 		return;
17008 	}
17009 
17010 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17011 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17012 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
17013 		goto nla_put_failure;
17014 
17015 	rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
17016 	if (!rekey_attr)
17017 		goto nla_put_failure;
17018 
17019 	if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
17020 		    NL80211_REPLAY_CTR_LEN, replay_ctr))
17021 		goto nla_put_failure;
17022 
17023 	nla_nest_end(msg, rekey_attr);
17024 
17025 	genlmsg_end(msg, hdr);
17026 
17027 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17028 				NL80211_MCGRP_MLME, gfp);
17029 	return;
17030 
17031  nla_put_failure:
17032 	nlmsg_free(msg);
17033 }
17034 
cfg80211_gtk_rekey_notify(struct net_device * dev,const u8 * bssid,const u8 * replay_ctr,gfp_t gfp)17035 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
17036 			       const u8 *replay_ctr, gfp_t gfp)
17037 {
17038 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17039 	struct wiphy *wiphy = wdev->wiphy;
17040 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17041 
17042 	trace_cfg80211_gtk_rekey_notify(dev, bssid);
17043 	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
17044 }
17045 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
17046 
17047 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)17048 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
17049 			       struct net_device *netdev, int index,
17050 			       const u8 *bssid, bool preauth, gfp_t gfp)
17051 {
17052 	struct sk_buff *msg;
17053 	struct nlattr *attr;
17054 	void *hdr;
17055 
17056 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17057 	if (!msg)
17058 		return;
17059 
17060 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
17061 	if (!hdr) {
17062 		nlmsg_free(msg);
17063 		return;
17064 	}
17065 
17066 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17067 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
17068 		goto nla_put_failure;
17069 
17070 	attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
17071 	if (!attr)
17072 		goto nla_put_failure;
17073 
17074 	if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
17075 	    nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
17076 	    (preauth &&
17077 	     nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
17078 		goto nla_put_failure;
17079 
17080 	nla_nest_end(msg, attr);
17081 
17082 	genlmsg_end(msg, hdr);
17083 
17084 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17085 				NL80211_MCGRP_MLME, gfp);
17086 	return;
17087 
17088  nla_put_failure:
17089 	nlmsg_free(msg);
17090 }
17091 
cfg80211_pmksa_candidate_notify(struct net_device * dev,int index,const u8 * bssid,bool preauth,gfp_t gfp)17092 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
17093 				     const u8 *bssid, bool preauth, gfp_t gfp)
17094 {
17095 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17096 	struct wiphy *wiphy = wdev->wiphy;
17097 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17098 
17099 	trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
17100 	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
17101 }
17102 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
17103 
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)17104 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
17105 				     struct net_device *netdev,
17106 				     struct cfg80211_chan_def *chandef,
17107 				     gfp_t gfp,
17108 				     enum nl80211_commands notif,
17109 				     u8 count)
17110 {
17111 	struct sk_buff *msg;
17112 	void *hdr;
17113 
17114 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17115 	if (!msg)
17116 		return;
17117 
17118 	hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
17119 	if (!hdr) {
17120 		nlmsg_free(msg);
17121 		return;
17122 	}
17123 
17124 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
17125 		goto nla_put_failure;
17126 
17127 	if (nl80211_send_chandef(msg, chandef))
17128 		goto nla_put_failure;
17129 
17130 	if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
17131 	    (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
17132 			goto nla_put_failure;
17133 
17134 	genlmsg_end(msg, hdr);
17135 
17136 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17137 				NL80211_MCGRP_MLME, gfp);
17138 	return;
17139 
17140  nla_put_failure:
17141 	nlmsg_free(msg);
17142 }
17143 
cfg80211_ch_switch_notify(struct net_device * dev,struct cfg80211_chan_def * chandef)17144 void cfg80211_ch_switch_notify(struct net_device *dev,
17145 			       struct cfg80211_chan_def *chandef)
17146 {
17147 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17148 	struct wiphy *wiphy = wdev->wiphy;
17149 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17150 
17151 	ASSERT_WDEV_LOCK(wdev);
17152 
17153 	trace_cfg80211_ch_switch_notify(dev, chandef);
17154 
17155 	wdev->chandef = *chandef;
17156 	wdev->preset_chandef = *chandef;
17157 
17158 	if ((wdev->iftype == NL80211_IFTYPE_STATION ||
17159 	     wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
17160 	    !WARN_ON(!wdev->current_bss))
17161 		cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
17162 
17163 	cfg80211_sched_dfs_chan_update(rdev);
17164 
17165 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
17166 				 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
17167 }
17168 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
17169 
cfg80211_ch_switch_started_notify(struct net_device * dev,struct cfg80211_chan_def * chandef,u8 count)17170 void cfg80211_ch_switch_started_notify(struct net_device *dev,
17171 				       struct cfg80211_chan_def *chandef,
17172 				       u8 count)
17173 {
17174 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17175 	struct wiphy *wiphy = wdev->wiphy;
17176 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17177 
17178 	trace_cfg80211_ch_switch_started_notify(dev, chandef);
17179 
17180 	nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
17181 				 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
17182 }
17183 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
17184 
17185 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)17186 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
17187 		     const struct cfg80211_chan_def *chandef,
17188 		     enum nl80211_radar_event event,
17189 		     struct net_device *netdev, gfp_t gfp)
17190 {
17191 	struct sk_buff *msg;
17192 	void *hdr;
17193 
17194 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17195 	if (!msg)
17196 		return;
17197 
17198 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
17199 	if (!hdr) {
17200 		nlmsg_free(msg);
17201 		return;
17202 	}
17203 
17204 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
17205 		goto nla_put_failure;
17206 
17207 	/* NOP and radar events don't need a netdev parameter */
17208 	if (netdev) {
17209 		struct wireless_dev *wdev = netdev->ieee80211_ptr;
17210 
17211 		if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17212 		    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17213 				      NL80211_ATTR_PAD))
17214 			goto nla_put_failure;
17215 	}
17216 
17217 	if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
17218 		goto nla_put_failure;
17219 
17220 	if (nl80211_send_chandef(msg, chandef))
17221 		goto nla_put_failure;
17222 
17223 	genlmsg_end(msg, hdr);
17224 
17225 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17226 				NL80211_MCGRP_MLME, gfp);
17227 	return;
17228 
17229  nla_put_failure:
17230 	nlmsg_free(msg);
17231 }
17232 
cfg80211_sta_opmode_change_notify(struct net_device * dev,const u8 * mac,struct sta_opmode_info * sta_opmode,gfp_t gfp)17233 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
17234 				       struct sta_opmode_info *sta_opmode,
17235 				       gfp_t gfp)
17236 {
17237 	struct sk_buff *msg;
17238 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17239 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17240 	void *hdr;
17241 
17242 	if (WARN_ON(!mac))
17243 		return;
17244 
17245 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17246 	if (!msg)
17247 		return;
17248 
17249 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
17250 	if (!hdr) {
17251 		nlmsg_free(msg);
17252 		return;
17253 	}
17254 
17255 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
17256 		goto nla_put_failure;
17257 
17258 	if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
17259 		goto nla_put_failure;
17260 
17261 	if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
17262 		goto nla_put_failure;
17263 
17264 	if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
17265 	    nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
17266 		goto nla_put_failure;
17267 
17268 	if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
17269 	    nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
17270 		goto nla_put_failure;
17271 
17272 	if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
17273 	    nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
17274 		goto nla_put_failure;
17275 
17276 	genlmsg_end(msg, hdr);
17277 
17278 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17279 				NL80211_MCGRP_MLME, gfp);
17280 
17281 	return;
17282 
17283 nla_put_failure:
17284 	nlmsg_free(msg);
17285 }
17286 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
17287 
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)17288 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
17289 			   u64 cookie, bool acked, s32 ack_signal,
17290 			   bool is_valid_ack_signal, gfp_t gfp)
17291 {
17292 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17293 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17294 	struct sk_buff *msg;
17295 	void *hdr;
17296 
17297 	trace_cfg80211_probe_status(dev, addr, cookie, acked);
17298 
17299 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17300 
17301 	if (!msg)
17302 		return;
17303 
17304 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
17305 	if (!hdr) {
17306 		nlmsg_free(msg);
17307 		return;
17308 	}
17309 
17310 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17311 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17312 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
17313 	    nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
17314 			      NL80211_ATTR_PAD) ||
17315 	    (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
17316 	    (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
17317 						ack_signal)))
17318 		goto nla_put_failure;
17319 
17320 	genlmsg_end(msg, hdr);
17321 
17322 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17323 				NL80211_MCGRP_MLME, gfp);
17324 	return;
17325 
17326  nla_put_failure:
17327 	nlmsg_free(msg);
17328 }
17329 EXPORT_SYMBOL(cfg80211_probe_status);
17330 
cfg80211_report_obss_beacon_khz(struct wiphy * wiphy,const u8 * frame,size_t len,int freq,int sig_dbm)17331 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
17332 				     size_t len, int freq, int sig_dbm)
17333 {
17334 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17335 	struct sk_buff *msg;
17336 	void *hdr;
17337 	struct cfg80211_beacon_registration *reg;
17338 
17339 	trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
17340 
17341 	spin_lock_bh(&rdev->beacon_registrations_lock);
17342 	list_for_each_entry(reg, &rdev->beacon_registrations, list) {
17343 		msg = nlmsg_new(len + 100, GFP_ATOMIC);
17344 		if (!msg) {
17345 			spin_unlock_bh(&rdev->beacon_registrations_lock);
17346 			return;
17347 		}
17348 
17349 		hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
17350 		if (!hdr)
17351 			goto nla_put_failure;
17352 
17353 		if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17354 		    (freq &&
17355 		     (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
17356 				  KHZ_TO_MHZ(freq)) ||
17357 		      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
17358 				  freq % 1000))) ||
17359 		    (sig_dbm &&
17360 		     nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
17361 		    nla_put(msg, NL80211_ATTR_FRAME, len, frame))
17362 			goto nla_put_failure;
17363 
17364 		genlmsg_end(msg, hdr);
17365 
17366 		genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
17367 	}
17368 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17369 	return;
17370 
17371  nla_put_failure:
17372 	spin_unlock_bh(&rdev->beacon_registrations_lock);
17373 	nlmsg_free(msg);
17374 }
17375 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
17376 
17377 #ifdef CONFIG_PM
cfg80211_net_detect_results(struct sk_buff * msg,struct cfg80211_wowlan_wakeup * wakeup)17378 static int cfg80211_net_detect_results(struct sk_buff *msg,
17379 				       struct cfg80211_wowlan_wakeup *wakeup)
17380 {
17381 	struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
17382 	struct nlattr *nl_results, *nl_match, *nl_freqs;
17383 	int i, j;
17384 
17385 	nl_results = nla_nest_start_noflag(msg,
17386 					   NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
17387 	if (!nl_results)
17388 		return -EMSGSIZE;
17389 
17390 	for (i = 0; i < nd->n_matches; i++) {
17391 		struct cfg80211_wowlan_nd_match *match = nd->matches[i];
17392 
17393 		nl_match = nla_nest_start_noflag(msg, i);
17394 		if (!nl_match)
17395 			break;
17396 
17397 		/* The SSID attribute is optional in nl80211, but for
17398 		 * simplicity reasons it's always present in the
17399 		 * cfg80211 structure.  If a driver can't pass the
17400 		 * SSID, that needs to be changed.  A zero length SSID
17401 		 * is still a valid SSID (wildcard), so it cannot be
17402 		 * used for this purpose.
17403 		 */
17404 		if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
17405 			    match->ssid.ssid)) {
17406 			nla_nest_cancel(msg, nl_match);
17407 			goto out;
17408 		}
17409 
17410 		if (match->n_channels) {
17411 			nl_freqs = nla_nest_start_noflag(msg,
17412 							 NL80211_ATTR_SCAN_FREQUENCIES);
17413 			if (!nl_freqs) {
17414 				nla_nest_cancel(msg, nl_match);
17415 				goto out;
17416 			}
17417 
17418 			for (j = 0; j < match->n_channels; j++) {
17419 				if (nla_put_u32(msg, j, match->channels[j])) {
17420 					nla_nest_cancel(msg, nl_freqs);
17421 					nla_nest_cancel(msg, nl_match);
17422 					goto out;
17423 				}
17424 			}
17425 
17426 			nla_nest_end(msg, nl_freqs);
17427 		}
17428 
17429 		nla_nest_end(msg, nl_match);
17430 	}
17431 
17432 out:
17433 	nla_nest_end(msg, nl_results);
17434 	return 0;
17435 }
17436 
cfg80211_report_wowlan_wakeup(struct wireless_dev * wdev,struct cfg80211_wowlan_wakeup * wakeup,gfp_t gfp)17437 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
17438 				   struct cfg80211_wowlan_wakeup *wakeup,
17439 				   gfp_t gfp)
17440 {
17441 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17442 	struct sk_buff *msg;
17443 	void *hdr;
17444 	int size = 200;
17445 
17446 	trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
17447 
17448 	if (wakeup)
17449 		size += wakeup->packet_present_len;
17450 
17451 	msg = nlmsg_new(size, gfp);
17452 	if (!msg)
17453 		return;
17454 
17455 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
17456 	if (!hdr)
17457 		goto free_msg;
17458 
17459 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17460 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17461 			      NL80211_ATTR_PAD))
17462 		goto free_msg;
17463 
17464 	if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17465 					wdev->netdev->ifindex))
17466 		goto free_msg;
17467 
17468 	if (wakeup) {
17469 		struct nlattr *reasons;
17470 
17471 		reasons = nla_nest_start_noflag(msg,
17472 						NL80211_ATTR_WOWLAN_TRIGGERS);
17473 		if (!reasons)
17474 			goto free_msg;
17475 
17476 		if (wakeup->disconnect &&
17477 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
17478 			goto free_msg;
17479 		if (wakeup->magic_pkt &&
17480 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
17481 			goto free_msg;
17482 		if (wakeup->gtk_rekey_failure &&
17483 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
17484 			goto free_msg;
17485 		if (wakeup->eap_identity_req &&
17486 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
17487 			goto free_msg;
17488 		if (wakeup->four_way_handshake &&
17489 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
17490 			goto free_msg;
17491 		if (wakeup->rfkill_release &&
17492 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
17493 			goto free_msg;
17494 
17495 		if (wakeup->pattern_idx >= 0 &&
17496 		    nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
17497 				wakeup->pattern_idx))
17498 			goto free_msg;
17499 
17500 		if (wakeup->tcp_match &&
17501 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
17502 			goto free_msg;
17503 
17504 		if (wakeup->tcp_connlost &&
17505 		    nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
17506 			goto free_msg;
17507 
17508 		if (wakeup->tcp_nomoretokens &&
17509 		    nla_put_flag(msg,
17510 				 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
17511 			goto free_msg;
17512 
17513 		if (wakeup->packet) {
17514 			u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
17515 			u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
17516 
17517 			if (!wakeup->packet_80211) {
17518 				pkt_attr =
17519 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
17520 				len_attr =
17521 					NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
17522 			}
17523 
17524 			if (wakeup->packet_len &&
17525 			    nla_put_u32(msg, len_attr, wakeup->packet_len))
17526 				goto free_msg;
17527 
17528 			if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
17529 				    wakeup->packet))
17530 				goto free_msg;
17531 		}
17532 
17533 		if (wakeup->net_detect &&
17534 		    cfg80211_net_detect_results(msg, wakeup))
17535 				goto free_msg;
17536 
17537 		nla_nest_end(msg, reasons);
17538 	}
17539 
17540 	genlmsg_end(msg, hdr);
17541 
17542 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17543 				NL80211_MCGRP_MLME, gfp);
17544 	return;
17545 
17546  free_msg:
17547 	nlmsg_free(msg);
17548 }
17549 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
17550 #endif
17551 
cfg80211_tdls_oper_request(struct net_device * dev,const u8 * peer,enum nl80211_tdls_operation oper,u16 reason_code,gfp_t gfp)17552 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
17553 				enum nl80211_tdls_operation oper,
17554 				u16 reason_code, gfp_t gfp)
17555 {
17556 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17557 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17558 	struct sk_buff *msg;
17559 	void *hdr;
17560 
17561 	trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
17562 					 reason_code);
17563 
17564 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17565 	if (!msg)
17566 		return;
17567 
17568 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
17569 	if (!hdr) {
17570 		nlmsg_free(msg);
17571 		return;
17572 	}
17573 
17574 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17575 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17576 	    nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
17577 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
17578 	    (reason_code > 0 &&
17579 	     nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
17580 		goto nla_put_failure;
17581 
17582 	genlmsg_end(msg, hdr);
17583 
17584 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17585 				NL80211_MCGRP_MLME, gfp);
17586 	return;
17587 
17588  nla_put_failure:
17589 	nlmsg_free(msg);
17590 }
17591 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
17592 
nl80211_netlink_notify(struct notifier_block * nb,unsigned long state,void * _notify)17593 static int nl80211_netlink_notify(struct notifier_block * nb,
17594 				  unsigned long state,
17595 				  void *_notify)
17596 {
17597 	struct netlink_notify *notify = _notify;
17598 	struct cfg80211_registered_device *rdev;
17599 	struct wireless_dev *wdev;
17600 	struct cfg80211_beacon_registration *reg, *tmp;
17601 
17602 	if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
17603 		return NOTIFY_DONE;
17604 
17605 	rcu_read_lock();
17606 
17607 	list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
17608 		struct cfg80211_sched_scan_request *sched_scan_req;
17609 
17610 		list_for_each_entry_rcu(sched_scan_req,
17611 					&rdev->sched_scan_req_list,
17612 					list) {
17613 			if (sched_scan_req->owner_nlportid == notify->portid) {
17614 				sched_scan_req->nl_owner_dead = true;
17615 				schedule_work(&rdev->sched_scan_stop_wk);
17616 			}
17617 		}
17618 
17619 		list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
17620 			cfg80211_mlme_unregister_socket(wdev, notify->portid);
17621 
17622 			if (wdev->owner_nlportid == notify->portid) {
17623 				wdev->nl_owner_dead = true;
17624 				schedule_work(&rdev->destroy_work);
17625 			} else if (wdev->conn_owner_nlportid == notify->portid) {
17626 				schedule_work(&wdev->disconnect_wk);
17627 			}
17628 
17629 			cfg80211_release_pmsr(wdev, notify->portid);
17630 		}
17631 
17632 		spin_lock_bh(&rdev->beacon_registrations_lock);
17633 		list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
17634 					 list) {
17635 			if (reg->nlportid == notify->portid) {
17636 				list_del(&reg->list);
17637 				kfree(reg);
17638 				break;
17639 			}
17640 		}
17641 		spin_unlock_bh(&rdev->beacon_registrations_lock);
17642 	}
17643 
17644 	rcu_read_unlock();
17645 
17646 	/*
17647 	 * It is possible that the user space process that is controlling the
17648 	 * indoor setting disappeared, so notify the regulatory core.
17649 	 */
17650 	regulatory_netlink_notify(notify->portid);
17651 	return NOTIFY_OK;
17652 }
17653 
17654 static struct notifier_block nl80211_netlink_notifier = {
17655 	.notifier_call = nl80211_netlink_notify,
17656 };
17657 
cfg80211_ft_event(struct net_device * netdev,struct cfg80211_ft_event_params * ft_event)17658 void cfg80211_ft_event(struct net_device *netdev,
17659 		       struct cfg80211_ft_event_params *ft_event)
17660 {
17661 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
17662 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17663 	struct sk_buff *msg;
17664 	void *hdr;
17665 
17666 	trace_cfg80211_ft_event(wiphy, netdev, ft_event);
17667 
17668 	if (!ft_event->target_ap)
17669 		return;
17670 
17671 	msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
17672 			GFP_KERNEL);
17673 	if (!msg)
17674 		return;
17675 
17676 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
17677 	if (!hdr)
17678 		goto out;
17679 
17680 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17681 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17682 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
17683 		goto out;
17684 
17685 	if (ft_event->ies &&
17686 	    nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
17687 		goto out;
17688 	if (ft_event->ric_ies &&
17689 	    nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
17690 		    ft_event->ric_ies))
17691 		goto out;
17692 
17693 	genlmsg_end(msg, hdr);
17694 
17695 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17696 				NL80211_MCGRP_MLME, GFP_KERNEL);
17697 	return;
17698  out:
17699 	nlmsg_free(msg);
17700 }
17701 EXPORT_SYMBOL(cfg80211_ft_event);
17702 
cfg80211_crit_proto_stopped(struct wireless_dev * wdev,gfp_t gfp)17703 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
17704 {
17705 	struct cfg80211_registered_device *rdev;
17706 	struct sk_buff *msg;
17707 	void *hdr;
17708 	u32 nlportid;
17709 
17710 	rdev = wiphy_to_rdev(wdev->wiphy);
17711 	if (!rdev->crit_proto_nlportid)
17712 		return;
17713 
17714 	nlportid = rdev->crit_proto_nlportid;
17715 	rdev->crit_proto_nlportid = 0;
17716 
17717 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17718 	if (!msg)
17719 		return;
17720 
17721 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
17722 	if (!hdr)
17723 		goto nla_put_failure;
17724 
17725 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17726 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17727 			      NL80211_ATTR_PAD))
17728 		goto nla_put_failure;
17729 
17730 	genlmsg_end(msg, hdr);
17731 
17732 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
17733 	return;
17734 
17735  nla_put_failure:
17736 	nlmsg_free(msg);
17737 }
17738 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
17739 
nl80211_send_ap_stopped(struct wireless_dev * wdev)17740 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
17741 {
17742 	struct wiphy *wiphy = wdev->wiphy;
17743 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17744 	struct sk_buff *msg;
17745 	void *hdr;
17746 
17747 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17748 	if (!msg)
17749 		return;
17750 
17751 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
17752 	if (!hdr)
17753 		goto out;
17754 
17755 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17756 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
17757 	    nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17758 			      NL80211_ATTR_PAD))
17759 		goto out;
17760 
17761 	genlmsg_end(msg, hdr);
17762 
17763 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
17764 				NL80211_MCGRP_MLME, GFP_KERNEL);
17765 	return;
17766  out:
17767 	nlmsg_free(msg);
17768 }
17769 
cfg80211_external_auth_request(struct net_device * dev,struct cfg80211_external_auth_params * params,gfp_t gfp)17770 int cfg80211_external_auth_request(struct net_device *dev,
17771 				   struct cfg80211_external_auth_params *params,
17772 				   gfp_t gfp)
17773 {
17774 	struct wireless_dev *wdev = dev->ieee80211_ptr;
17775 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
17776 	struct sk_buff *msg;
17777 	void *hdr;
17778 
17779 	if (!wdev->conn_owner_nlportid)
17780 		return -EINVAL;
17781 
17782 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17783 	if (!msg)
17784 		return -ENOMEM;
17785 
17786 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
17787 	if (!hdr)
17788 		goto nla_put_failure;
17789 
17790 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17791 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
17792 	    nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
17793 	    nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
17794 			params->action) ||
17795 	    nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
17796 	    nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
17797 		    params->ssid.ssid))
17798 		goto nla_put_failure;
17799 
17800 	genlmsg_end(msg, hdr);
17801 	genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
17802 			wdev->conn_owner_nlportid);
17803 	return 0;
17804 
17805  nla_put_failure:
17806 	nlmsg_free(msg);
17807 	return -ENOBUFS;
17808 }
17809 EXPORT_SYMBOL(cfg80211_external_auth_request);
17810 
cfg80211_update_owe_info_event(struct net_device * netdev,struct cfg80211_update_owe_info * owe_info,gfp_t gfp)17811 void cfg80211_update_owe_info_event(struct net_device *netdev,
17812 				    struct cfg80211_update_owe_info *owe_info,
17813 				    gfp_t gfp)
17814 {
17815 	struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
17816 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17817 	struct sk_buff *msg;
17818 	void *hdr;
17819 
17820 	trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
17821 
17822 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17823 	if (!msg)
17824 		return;
17825 
17826 	hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
17827 	if (!hdr)
17828 		goto nla_put_failure;
17829 
17830 	if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17831 	    nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17832 	    nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
17833 		goto nla_put_failure;
17834 
17835 	if (!owe_info->ie_len ||
17836 	    nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
17837 		goto nla_put_failure;
17838 
17839 	genlmsg_end(msg, hdr);
17840 
17841 	genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17842 				NL80211_MCGRP_MLME, gfp);
17843 	return;
17844 
17845 nla_put_failure:
17846 	genlmsg_cancel(msg, hdr);
17847 	nlmsg_free(msg);
17848 }
17849 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
17850 
17851 /* initialisation/exit functions */
17852 
nl80211_init(void)17853 int __init nl80211_init(void)
17854 {
17855 	int err;
17856 
17857 	err = genl_register_family(&nl80211_fam);
17858 	if (err)
17859 		return err;
17860 
17861 	err = netlink_register_notifier(&nl80211_netlink_notifier);
17862 	if (err)
17863 		goto err_out;
17864 
17865 	return 0;
17866  err_out:
17867 	genl_unregister_family(&nl80211_fam);
17868 	return err;
17869 }
17870 
nl80211_exit(void)17871 void nl80211_exit(void)
17872 {
17873 	netlink_unregister_notifier(&nl80211_netlink_notifier);
17874 	genl_unregister_family(&nl80211_fam);
17875 }
17876