Linux 4.19.133
[linux/fpc-iii.git] / net / wireless / nl80211.c
blob0221849b721807108a181c449aab7095eeca5b07
1 /*
2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright 2015-2017 Intel Deutschland GmbH
7 * Copyright (C) 2018 Intel Corporation
8 */
10 #include <linux/if.h>
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <linux/if_ether.h>
16 #include <linux/ieee80211.h>
17 #include <linux/nl80211.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/netlink.h>
20 #include <linux/nospec.h>
21 #include <linux/etherdevice.h>
22 #include <net/net_namespace.h>
23 #include <net/genetlink.h>
24 #include <net/cfg80211.h>
25 #include <net/sock.h>
26 #include <net/inet_connection_sock.h>
27 #include "core.h"
28 #include "nl80211.h"
29 #include "reg.h"
30 #include "rdev-ops.h"
32 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
33 struct genl_info *info,
34 struct cfg80211_crypto_settings *settings,
35 int cipher_limit);
37 /* the netlink family */
38 static struct genl_family nl80211_fam;
40 /* multicast groups */
41 enum nl80211_multicast_groups {
42 NL80211_MCGRP_CONFIG,
43 NL80211_MCGRP_SCAN,
44 NL80211_MCGRP_REGULATORY,
45 NL80211_MCGRP_MLME,
46 NL80211_MCGRP_VENDOR,
47 NL80211_MCGRP_NAN,
48 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 static const struct genl_multicast_group nl80211_mcgrps[] = {
52 [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
53 [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
54 [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
55 [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
56 [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
57 [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
58 #ifdef CONFIG_NL80211_TESTMODE
59 [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
60 #endif
63 /* returns ERR_PTR values */
64 static struct wireless_dev *
65 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
67 struct cfg80211_registered_device *rdev;
68 struct wireless_dev *result = NULL;
69 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
70 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
71 u64 wdev_id;
72 int wiphy_idx = -1;
73 int ifidx = -1;
75 ASSERT_RTNL();
77 if (!have_ifidx && !have_wdev_id)
78 return ERR_PTR(-EINVAL);
80 if (have_ifidx)
81 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82 if (have_wdev_id) {
83 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84 wiphy_idx = wdev_id >> 32;
87 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
88 struct wireless_dev *wdev;
90 if (wiphy_net(&rdev->wiphy) != netns)
91 continue;
93 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
94 continue;
96 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
97 if (have_ifidx && wdev->netdev &&
98 wdev->netdev->ifindex == ifidx) {
99 result = wdev;
100 break;
102 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
103 result = wdev;
104 break;
108 if (result)
109 break;
112 if (result)
113 return result;
114 return ERR_PTR(-ENODEV);
117 static struct cfg80211_registered_device *
118 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
120 struct cfg80211_registered_device *rdev = NULL, *tmp;
121 struct net_device *netdev;
123 ASSERT_RTNL();
125 if (!attrs[NL80211_ATTR_WIPHY] &&
126 !attrs[NL80211_ATTR_IFINDEX] &&
127 !attrs[NL80211_ATTR_WDEV])
128 return ERR_PTR(-EINVAL);
130 if (attrs[NL80211_ATTR_WIPHY])
131 rdev = cfg80211_rdev_by_wiphy_idx(
132 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
134 if (attrs[NL80211_ATTR_WDEV]) {
135 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
136 struct wireless_dev *wdev;
137 bool found = false;
139 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
140 if (tmp) {
141 /* make sure wdev exists */
142 list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
143 if (wdev->identifier != (u32)wdev_id)
144 continue;
145 found = true;
146 break;
149 if (!found)
150 tmp = NULL;
152 if (rdev && tmp != rdev)
153 return ERR_PTR(-EINVAL);
154 rdev = tmp;
158 if (attrs[NL80211_ATTR_IFINDEX]) {
159 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
161 netdev = __dev_get_by_index(netns, ifindex);
162 if (netdev) {
163 if (netdev->ieee80211_ptr)
164 tmp = wiphy_to_rdev(
165 netdev->ieee80211_ptr->wiphy);
166 else
167 tmp = NULL;
169 /* not wireless device -- return error */
170 if (!tmp)
171 return ERR_PTR(-EINVAL);
173 /* mismatch -- return error */
174 if (rdev && tmp != rdev)
175 return ERR_PTR(-EINVAL);
177 rdev = tmp;
181 if (!rdev)
182 return ERR_PTR(-ENODEV);
184 if (netns != wiphy_net(&rdev->wiphy))
185 return ERR_PTR(-ENODEV);
187 return rdev;
191 * This function returns a pointer to the driver
192 * that the genl_info item that is passed refers to.
194 * The result of this can be a PTR_ERR and hence must
195 * be checked with IS_ERR() for errors.
197 static struct cfg80211_registered_device *
198 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
200 return __cfg80211_rdev_from_attrs(netns, info->attrs);
203 static int validate_beacon_head(const struct nlattr *attr,
204 struct netlink_ext_ack *extack)
206 const u8 *data = nla_data(attr);
207 unsigned int len = nla_len(attr);
208 const struct element *elem;
209 const struct ieee80211_mgmt *mgmt = (void *)data;
210 unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
211 u.beacon.variable);
213 if (len < fixedlen)
214 goto err;
216 if (ieee80211_hdrlen(mgmt->frame_control) !=
217 offsetof(struct ieee80211_mgmt, u.beacon))
218 goto err;
220 data += fixedlen;
221 len -= fixedlen;
223 for_each_element(elem, data, len) {
224 /* nothing */
227 if (for_each_element_completed(elem, data, len))
228 return 0;
230 err:
231 NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
232 return -EINVAL;
235 /* policy for the attributes */
236 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
237 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
238 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
239 .len = 20-1 },
240 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
242 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
243 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
244 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
245 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
246 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
248 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
249 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
250 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
251 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
252 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
253 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
255 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
256 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
257 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
259 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
260 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
262 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
263 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
264 .len = WLAN_MAX_KEY_LEN },
265 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
266 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
267 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
268 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
269 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
271 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
272 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
273 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
274 .len = IEEE80211_MAX_DATA_LEN },
275 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
276 .len = IEEE80211_MAX_DATA_LEN },
277 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
278 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
279 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
280 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
282 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
283 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
284 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
285 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
286 .len = IEEE80211_MAX_MESH_ID_LEN },
287 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_BINARY,
288 .len = ETH_ALEN },
290 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
291 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
294 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
295 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
296 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
297 .len = NL80211_MAX_SUPP_RATES },
298 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
300 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
301 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
303 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
305 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
306 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
307 .len = IEEE80211_MAX_DATA_LEN },
308 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
309 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
311 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
312 .len = IEEE80211_MAX_SSID_LEN },
313 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
314 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
315 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
316 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
317 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
318 [NL80211_ATTR_STA_FLAGS2] = {
319 .len = sizeof(struct nl80211_sta_flag_update),
321 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
322 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
323 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
324 [NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
325 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
326 [NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
327 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
328 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
329 [NL80211_ATTR_PID] = { .type = NLA_U32 },
330 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
331 [NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
332 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
333 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
334 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
335 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
337 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
338 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
339 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
340 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
341 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
342 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
343 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
344 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
345 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
346 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
347 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
348 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
349 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
350 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
351 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
352 [NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
353 [NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
354 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
355 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
356 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
357 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
358 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
359 .len = IEEE80211_MAX_DATA_LEN },
360 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
361 .len = IEEE80211_MAX_DATA_LEN },
362 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
363 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
364 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
365 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
366 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
367 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
368 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
369 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
370 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
371 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
372 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
373 .len = IEEE80211_MAX_DATA_LEN },
374 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
375 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
376 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
377 .len = NL80211_HT_CAPABILITY_LEN
379 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
380 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
381 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
382 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
383 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
384 [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
385 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
386 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
387 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
388 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
389 [NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
390 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
391 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
392 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
393 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
394 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
395 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
396 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
397 .len = NL80211_VHT_CAPABILITY_LEN,
399 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
400 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
401 .len = IEEE80211_MAX_DATA_LEN },
402 [NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
403 [NL80211_ATTR_MAX_CRIT_PROT_DURATION] = { .type = NLA_U16 },
404 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
405 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
406 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
407 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
408 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
409 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
410 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
411 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
412 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
413 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
414 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
415 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
416 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
417 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
418 .len = IEEE80211_QOS_MAP_LEN_MAX },
419 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
420 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
421 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
422 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
423 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
424 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
425 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
426 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
427 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
428 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
429 [NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
430 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
431 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
432 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
433 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
434 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
435 [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
436 [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
437 [NL80211_ATTR_STA_SUPPORT_P2P_PS] = { .type = NLA_U8 },
438 [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
439 .len = VHT_MUMIMO_GROUPS_DATA_LEN
441 [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = { .len = ETH_ALEN },
442 [NL80211_ATTR_NAN_MASTER_PREF] = { .type = NLA_U8 },
443 [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
444 [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
445 [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
446 .len = FILS_MAX_KEK_LEN },
447 [NL80211_ATTR_FILS_NONCES] = { .len = 2 * FILS_NONCE_LEN },
448 [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
449 [NL80211_ATTR_BSSID] = { .len = ETH_ALEN },
450 [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
451 [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
452 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
454 [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
455 [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
456 .len = FILS_ERP_MAX_USERNAME_LEN },
457 [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
458 .len = FILS_ERP_MAX_REALM_LEN },
459 [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
460 [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
461 .len = FILS_ERP_MAX_RRK_LEN },
462 [NL80211_ATTR_FILS_CACHE_ID] = { .len = 2 },
463 [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
464 [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
465 [NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
467 [NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
468 [NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
469 [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
470 [NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
471 .len = NL80211_HE_MAX_CAPABILITY_LEN },
474 /* policy for the key attributes */
475 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
476 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
477 [NL80211_KEY_IDX] = { .type = NLA_U8 },
478 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
479 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
480 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
481 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
482 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
483 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
486 /* policy for the key default flags */
487 static const struct nla_policy
488 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
489 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
490 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
493 #ifdef CONFIG_PM
494 /* policy for WoWLAN attributes */
495 static const struct nla_policy
496 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
497 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
498 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
499 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
500 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
501 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
502 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
503 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
504 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
505 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
506 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
509 static const struct nla_policy
510 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
511 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
512 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
513 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
514 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
515 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
516 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
517 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
518 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
520 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
521 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
523 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
524 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
525 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
527 #endif /* CONFIG_PM */
529 /* policy for coalesce rule attributes */
530 static const struct nla_policy
531 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
532 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
533 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
534 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
537 /* policy for GTK rekey offload attributes */
538 static const struct nla_policy
539 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
540 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
541 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
542 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
545 static const struct nla_policy
546 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
547 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
548 .len = IEEE80211_MAX_SSID_LEN },
549 [NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = { .len = ETH_ALEN },
550 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
553 static const struct nla_policy
554 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
555 [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
556 [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
559 static const struct nla_policy
560 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
561 [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
562 [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
563 [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
564 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
568 /* policy for NAN function attributes */
569 static const struct nla_policy
570 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
571 [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
572 [NL80211_NAN_FUNC_SERVICE_ID] = {
573 .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
574 [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
575 [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
576 [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
577 [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
578 [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
579 [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { .len = ETH_ALEN },
580 [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
581 [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
582 [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
583 .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
584 [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
585 [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
586 [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
587 [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
588 [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
591 /* policy for Service Response Filter attributes */
592 static const struct nla_policy
593 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
594 [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
595 [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
596 .len = NL80211_NAN_FUNC_SRF_MAX_LEN },
597 [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
598 [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
601 /* policy for packet pattern attributes */
602 static const struct nla_policy
603 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
604 [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
605 [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
606 [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
609 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
610 struct netlink_callback *cb,
611 struct cfg80211_registered_device **rdev,
612 struct wireless_dev **wdev)
614 int err;
616 if (!cb->args[0]) {
617 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
618 genl_family_attrbuf(&nl80211_fam),
619 nl80211_fam.maxattr, nl80211_policy, NULL);
620 if (err)
621 return err;
623 *wdev = __cfg80211_wdev_from_attrs(
624 sock_net(skb->sk),
625 genl_family_attrbuf(&nl80211_fam));
626 if (IS_ERR(*wdev))
627 return PTR_ERR(*wdev);
628 *rdev = wiphy_to_rdev((*wdev)->wiphy);
629 /* 0 is the first index - add 1 to parse only once */
630 cb->args[0] = (*rdev)->wiphy_idx + 1;
631 cb->args[1] = (*wdev)->identifier;
632 } else {
633 /* subtract the 1 again here */
634 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
635 struct wireless_dev *tmp;
637 if (!wiphy)
638 return -ENODEV;
639 *rdev = wiphy_to_rdev(wiphy);
640 *wdev = NULL;
642 list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
643 if (tmp->identifier == cb->args[1]) {
644 *wdev = tmp;
645 break;
649 if (!*wdev)
650 return -ENODEV;
653 return 0;
656 /* IE validation */
657 static bool is_valid_ie_attr(const struct nlattr *attr)
659 const u8 *pos;
660 int len;
662 if (!attr)
663 return true;
665 pos = nla_data(attr);
666 len = nla_len(attr);
668 while (len) {
669 u8 elemlen;
671 if (len < 2)
672 return false;
673 len -= 2;
675 elemlen = pos[1];
676 if (elemlen > len)
677 return false;
679 len -= elemlen;
680 pos += 2 + elemlen;
683 return true;
686 /* message building helper */
687 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
688 int flags, u8 cmd)
690 /* since there is no private header just add the generic one */
691 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
694 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
695 const struct ieee80211_reg_rule *rule)
697 int j;
698 struct nlattr *nl_wmm_rules =
699 nla_nest_start(msg, NL80211_FREQUENCY_ATTR_WMM);
701 if (!nl_wmm_rules)
702 goto nla_put_failure;
704 for (j = 0; j < IEEE80211_NUM_ACS; j++) {
705 struct nlattr *nl_wmm_rule = nla_nest_start(msg, j);
707 if (!nl_wmm_rule)
708 goto nla_put_failure;
710 if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
711 rule->wmm_rule.client[j].cw_min) ||
712 nla_put_u16(msg, NL80211_WMMR_CW_MAX,
713 rule->wmm_rule.client[j].cw_max) ||
714 nla_put_u8(msg, NL80211_WMMR_AIFSN,
715 rule->wmm_rule.client[j].aifsn) ||
716 nla_put_u16(msg, NL80211_WMMR_TXOP,
717 rule->wmm_rule.client[j].cot))
718 goto nla_put_failure;
720 nla_nest_end(msg, nl_wmm_rule);
722 nla_nest_end(msg, nl_wmm_rules);
724 return 0;
726 nla_put_failure:
727 return -ENOBUFS;
730 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
731 struct ieee80211_channel *chan,
732 bool large)
734 /* Some channels must be completely excluded from the
735 * list to protect old user-space tools from breaking
737 if (!large && chan->flags &
738 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
739 return 0;
741 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
742 chan->center_freq))
743 goto nla_put_failure;
745 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
746 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
747 goto nla_put_failure;
748 if (chan->flags & IEEE80211_CHAN_NO_IR) {
749 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
750 goto nla_put_failure;
751 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
752 goto nla_put_failure;
754 if (chan->flags & IEEE80211_CHAN_RADAR) {
755 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
756 goto nla_put_failure;
757 if (large) {
758 u32 time;
760 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
762 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
763 chan->dfs_state))
764 goto nla_put_failure;
765 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
766 time))
767 goto nla_put_failure;
768 if (nla_put_u32(msg,
769 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
770 chan->dfs_cac_ms))
771 goto nla_put_failure;
775 if (large) {
776 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
777 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
778 goto nla_put_failure;
779 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
780 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
781 goto nla_put_failure;
782 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
783 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
784 goto nla_put_failure;
785 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
786 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
787 goto nla_put_failure;
788 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
789 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
790 goto nla_put_failure;
791 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
792 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
793 goto nla_put_failure;
794 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
795 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
796 goto nla_put_failure;
797 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
798 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
799 goto nla_put_failure;
802 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
803 DBM_TO_MBM(chan->max_power)))
804 goto nla_put_failure;
806 if (large) {
807 const struct ieee80211_reg_rule *rule =
808 freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
810 if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
811 if (nl80211_msg_put_wmm_rules(msg, rule))
812 goto nla_put_failure;
816 return 0;
818 nla_put_failure:
819 return -ENOBUFS;
822 static bool nl80211_put_txq_stats(struct sk_buff *msg,
823 struct cfg80211_txq_stats *txqstats,
824 int attrtype)
826 struct nlattr *txqattr;
828 #define PUT_TXQVAL_U32(attr, memb) do { \
829 if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) && \
830 nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
831 return false; \
832 } while (0)
834 txqattr = nla_nest_start(msg, attrtype);
835 if (!txqattr)
836 return false;
838 PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
839 PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
840 PUT_TXQVAL_U32(FLOWS, flows);
841 PUT_TXQVAL_U32(DROPS, drops);
842 PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
843 PUT_TXQVAL_U32(OVERLIMIT, overlimit);
844 PUT_TXQVAL_U32(OVERMEMORY, overmemory);
845 PUT_TXQVAL_U32(COLLISIONS, collisions);
846 PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
847 PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
848 PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
849 nla_nest_end(msg, txqattr);
851 #undef PUT_TXQVAL_U32
852 return true;
855 /* netlink command implementations */
857 struct key_parse {
858 struct key_params p;
859 int idx;
860 int type;
861 bool def, defmgmt;
862 bool def_uni, def_multi;
865 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
866 struct key_parse *k)
868 struct nlattr *tb[NL80211_KEY_MAX + 1];
869 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
870 nl80211_key_policy, info->extack);
871 if (err)
872 return err;
874 k->def = !!tb[NL80211_KEY_DEFAULT];
875 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
877 if (k->def) {
878 k->def_uni = true;
879 k->def_multi = true;
881 if (k->defmgmt)
882 k->def_multi = true;
884 if (tb[NL80211_KEY_IDX])
885 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
887 if (tb[NL80211_KEY_DATA]) {
888 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
889 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
892 if (tb[NL80211_KEY_SEQ]) {
893 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
894 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
897 if (tb[NL80211_KEY_CIPHER])
898 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
900 if (tb[NL80211_KEY_TYPE]) {
901 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
902 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
903 return genl_err_attr(info, -EINVAL,
904 tb[NL80211_KEY_TYPE]);
907 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
908 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
910 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
911 tb[NL80211_KEY_DEFAULT_TYPES],
912 nl80211_key_default_policy,
913 info->extack);
914 if (err)
915 return err;
917 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
918 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
921 return 0;
924 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
926 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
927 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
928 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
931 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
932 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
933 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
936 if (info->attrs[NL80211_ATTR_KEY_IDX])
937 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
939 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
940 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
942 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
943 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
945 if (k->def) {
946 k->def_uni = true;
947 k->def_multi = true;
949 if (k->defmgmt)
950 k->def_multi = true;
952 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
953 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
954 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES) {
955 GENL_SET_ERR_MSG(info, "key type out of range");
956 return -EINVAL;
960 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
961 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
962 int err = nla_parse_nested(kdt,
963 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
964 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
965 nl80211_key_default_policy,
966 info->extack);
967 if (err)
968 return err;
970 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
971 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
974 return 0;
977 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
979 int err;
981 memset(k, 0, sizeof(*k));
982 k->idx = -1;
983 k->type = -1;
985 if (info->attrs[NL80211_ATTR_KEY])
986 err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
987 else
988 err = nl80211_parse_key_old(info, k);
990 if (err)
991 return err;
993 if (k->def && k->defmgmt) {
994 GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
995 return -EINVAL;
998 if (k->defmgmt) {
999 if (k->def_uni || !k->def_multi) {
1000 GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1001 return -EINVAL;
1005 if (k->idx != -1) {
1006 if (k->defmgmt) {
1007 if (k->idx < 4 || k->idx > 5) {
1008 GENL_SET_ERR_MSG(info,
1009 "defmgmt key idx not 4 or 5");
1010 return -EINVAL;
1012 } else if (k->def) {
1013 if (k->idx < 0 || k->idx > 3) {
1014 GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1015 return -EINVAL;
1017 } else {
1018 if (k->idx < 0 || k->idx > 5) {
1019 GENL_SET_ERR_MSG(info, "key idx not 0-5");
1020 return -EINVAL;
1025 return 0;
1028 static struct cfg80211_cached_keys *
1029 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1030 struct genl_info *info, bool *no_ht)
1032 struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1033 struct key_parse parse;
1034 struct nlattr *key;
1035 struct cfg80211_cached_keys *result;
1036 int rem, err, def = 0;
1037 bool have_key = false;
1039 nla_for_each_nested(key, keys, rem) {
1040 have_key = true;
1041 break;
1044 if (!have_key)
1045 return NULL;
1047 result = kzalloc(sizeof(*result), GFP_KERNEL);
1048 if (!result)
1049 return ERR_PTR(-ENOMEM);
1051 result->def = -1;
1053 nla_for_each_nested(key, keys, rem) {
1054 memset(&parse, 0, sizeof(parse));
1055 parse.idx = -1;
1057 err = nl80211_parse_key_new(info, key, &parse);
1058 if (err)
1059 goto error;
1060 err = -EINVAL;
1061 if (!parse.p.key)
1062 goto error;
1063 if (parse.idx < 0 || parse.idx > 3) {
1064 GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1065 goto error;
1067 if (parse.def) {
1068 if (def) {
1069 GENL_SET_ERR_MSG(info,
1070 "only one key can be default");
1071 goto error;
1073 def = 1;
1074 result->def = parse.idx;
1075 if (!parse.def_uni || !parse.def_multi)
1076 goto error;
1077 } else if (parse.defmgmt)
1078 goto error;
1079 err = cfg80211_validate_key_settings(rdev, &parse.p,
1080 parse.idx, false, NULL);
1081 if (err)
1082 goto error;
1083 if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1084 parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1085 GENL_SET_ERR_MSG(info, "connect key must be WEP");
1086 err = -EINVAL;
1087 goto error;
1089 result->params[parse.idx].cipher = parse.p.cipher;
1090 result->params[parse.idx].key_len = parse.p.key_len;
1091 result->params[parse.idx].key = result->data[parse.idx];
1092 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1094 /* must be WEP key if we got here */
1095 if (no_ht)
1096 *no_ht = true;
1099 if (result->def < 0) {
1100 err = -EINVAL;
1101 GENL_SET_ERR_MSG(info, "need a default/TX key");
1102 goto error;
1105 return result;
1106 error:
1107 kfree(result);
1108 return ERR_PTR(err);
1111 static int nl80211_key_allowed(struct wireless_dev *wdev)
1113 ASSERT_WDEV_LOCK(wdev);
1115 switch (wdev->iftype) {
1116 case NL80211_IFTYPE_AP:
1117 case NL80211_IFTYPE_AP_VLAN:
1118 case NL80211_IFTYPE_P2P_GO:
1119 case NL80211_IFTYPE_MESH_POINT:
1120 break;
1121 case NL80211_IFTYPE_ADHOC:
1122 case NL80211_IFTYPE_STATION:
1123 case NL80211_IFTYPE_P2P_CLIENT:
1124 if (!wdev->current_bss)
1125 return -ENOLINK;
1126 break;
1127 case NL80211_IFTYPE_UNSPECIFIED:
1128 case NL80211_IFTYPE_OCB:
1129 case NL80211_IFTYPE_MONITOR:
1130 case NL80211_IFTYPE_NAN:
1131 case NL80211_IFTYPE_P2P_DEVICE:
1132 case NL80211_IFTYPE_WDS:
1133 case NUM_NL80211_IFTYPES:
1134 return -EINVAL;
1137 return 0;
1140 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1141 struct nlattr *tb)
1143 struct ieee80211_channel *chan;
1145 if (tb == NULL)
1146 return NULL;
1147 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1148 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1149 return NULL;
1150 return chan;
1153 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1155 struct nlattr *nl_modes = nla_nest_start(msg, attr);
1156 int i;
1158 if (!nl_modes)
1159 goto nla_put_failure;
1161 i = 0;
1162 while (ifmodes) {
1163 if ((ifmodes & 1) && nla_put_flag(msg, i))
1164 goto nla_put_failure;
1165 ifmodes >>= 1;
1166 i++;
1169 nla_nest_end(msg, nl_modes);
1170 return 0;
1172 nla_put_failure:
1173 return -ENOBUFS;
1176 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1177 struct sk_buff *msg,
1178 bool large)
1180 struct nlattr *nl_combis;
1181 int i, j;
1183 nl_combis = nla_nest_start(msg,
1184 NL80211_ATTR_INTERFACE_COMBINATIONS);
1185 if (!nl_combis)
1186 goto nla_put_failure;
1188 for (i = 0; i < wiphy->n_iface_combinations; i++) {
1189 const struct ieee80211_iface_combination *c;
1190 struct nlattr *nl_combi, *nl_limits;
1192 c = &wiphy->iface_combinations[i];
1194 nl_combi = nla_nest_start(msg, i + 1);
1195 if (!nl_combi)
1196 goto nla_put_failure;
1198 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
1199 if (!nl_limits)
1200 goto nla_put_failure;
1202 for (j = 0; j < c->n_limits; j++) {
1203 struct nlattr *nl_limit;
1205 nl_limit = nla_nest_start(msg, j + 1);
1206 if (!nl_limit)
1207 goto nla_put_failure;
1208 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1209 c->limits[j].max))
1210 goto nla_put_failure;
1211 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1212 c->limits[j].types))
1213 goto nla_put_failure;
1214 nla_nest_end(msg, nl_limit);
1217 nla_nest_end(msg, nl_limits);
1219 if (c->beacon_int_infra_match &&
1220 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1221 goto nla_put_failure;
1222 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1223 c->num_different_channels) ||
1224 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1225 c->max_interfaces))
1226 goto nla_put_failure;
1227 if (large &&
1228 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1229 c->radar_detect_widths) ||
1230 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1231 c->radar_detect_regions)))
1232 goto nla_put_failure;
1233 if (c->beacon_int_min_gcd &&
1234 nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1235 c->beacon_int_min_gcd))
1236 goto nla_put_failure;
1238 nla_nest_end(msg, nl_combi);
1241 nla_nest_end(msg, nl_combis);
1243 return 0;
1244 nla_put_failure:
1245 return -ENOBUFS;
1248 #ifdef CONFIG_PM
1249 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1250 struct sk_buff *msg)
1252 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1253 struct nlattr *nl_tcp;
1255 if (!tcp)
1256 return 0;
1258 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1259 if (!nl_tcp)
1260 return -ENOBUFS;
1262 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1263 tcp->data_payload_max))
1264 return -ENOBUFS;
1266 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1267 tcp->data_payload_max))
1268 return -ENOBUFS;
1270 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1271 return -ENOBUFS;
1273 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1274 sizeof(*tcp->tok), tcp->tok))
1275 return -ENOBUFS;
1277 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1278 tcp->data_interval_max))
1279 return -ENOBUFS;
1281 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1282 tcp->wake_payload_max))
1283 return -ENOBUFS;
1285 nla_nest_end(msg, nl_tcp);
1286 return 0;
1289 static int nl80211_send_wowlan(struct sk_buff *msg,
1290 struct cfg80211_registered_device *rdev,
1291 bool large)
1293 struct nlattr *nl_wowlan;
1295 if (!rdev->wiphy.wowlan)
1296 return 0;
1298 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1299 if (!nl_wowlan)
1300 return -ENOBUFS;
1302 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1303 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1304 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1305 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1306 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1307 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1308 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1309 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1310 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1311 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1312 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1313 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1314 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1315 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1316 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1317 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1318 return -ENOBUFS;
1320 if (rdev->wiphy.wowlan->n_patterns) {
1321 struct nl80211_pattern_support pat = {
1322 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1323 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1324 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1325 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1328 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1329 sizeof(pat), &pat))
1330 return -ENOBUFS;
1333 if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1334 nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1335 rdev->wiphy.wowlan->max_nd_match_sets))
1336 return -ENOBUFS;
1338 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1339 return -ENOBUFS;
1341 nla_nest_end(msg, nl_wowlan);
1343 return 0;
1345 #endif
1347 static int nl80211_send_coalesce(struct sk_buff *msg,
1348 struct cfg80211_registered_device *rdev)
1350 struct nl80211_coalesce_rule_support rule;
1352 if (!rdev->wiphy.coalesce)
1353 return 0;
1355 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1356 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1357 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1358 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1359 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1360 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1362 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1363 return -ENOBUFS;
1365 return 0;
1368 static int
1369 nl80211_send_iftype_data(struct sk_buff *msg,
1370 const struct ieee80211_sband_iftype_data *iftdata)
1372 const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1374 if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1375 iftdata->types_mask))
1376 return -ENOBUFS;
1378 if (he_cap->has_he) {
1379 if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1380 sizeof(he_cap->he_cap_elem.mac_cap_info),
1381 he_cap->he_cap_elem.mac_cap_info) ||
1382 nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1383 sizeof(he_cap->he_cap_elem.phy_cap_info),
1384 he_cap->he_cap_elem.phy_cap_info) ||
1385 nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1386 sizeof(he_cap->he_mcs_nss_supp),
1387 &he_cap->he_mcs_nss_supp) ||
1388 nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1389 sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1390 return -ENOBUFS;
1393 return 0;
1396 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1397 struct ieee80211_supported_band *sband)
1399 struct nlattr *nl_rates, *nl_rate;
1400 struct ieee80211_rate *rate;
1401 int i;
1403 /* add HT info */
1404 if (sband->ht_cap.ht_supported &&
1405 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1406 sizeof(sband->ht_cap.mcs),
1407 &sband->ht_cap.mcs) ||
1408 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1409 sband->ht_cap.cap) ||
1410 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1411 sband->ht_cap.ampdu_factor) ||
1412 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1413 sband->ht_cap.ampdu_density)))
1414 return -ENOBUFS;
1416 /* add VHT info */
1417 if (sband->vht_cap.vht_supported &&
1418 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1419 sizeof(sband->vht_cap.vht_mcs),
1420 &sband->vht_cap.vht_mcs) ||
1421 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1422 sband->vht_cap.cap)))
1423 return -ENOBUFS;
1425 if (sband->n_iftype_data) {
1426 struct nlattr *nl_iftype_data =
1427 nla_nest_start(msg, NL80211_BAND_ATTR_IFTYPE_DATA);
1428 int err;
1430 if (!nl_iftype_data)
1431 return -ENOBUFS;
1433 for (i = 0; i < sband->n_iftype_data; i++) {
1434 struct nlattr *iftdata;
1436 iftdata = nla_nest_start(msg, i + 1);
1437 if (!iftdata)
1438 return -ENOBUFS;
1440 err = nl80211_send_iftype_data(msg,
1441 &sband->iftype_data[i]);
1442 if (err)
1443 return err;
1445 nla_nest_end(msg, iftdata);
1448 nla_nest_end(msg, nl_iftype_data);
1451 /* add bitrates */
1452 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1453 if (!nl_rates)
1454 return -ENOBUFS;
1456 for (i = 0; i < sband->n_bitrates; i++) {
1457 nl_rate = nla_nest_start(msg, i);
1458 if (!nl_rate)
1459 return -ENOBUFS;
1461 rate = &sband->bitrates[i];
1462 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1463 rate->bitrate))
1464 return -ENOBUFS;
1465 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1466 nla_put_flag(msg,
1467 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1468 return -ENOBUFS;
1470 nla_nest_end(msg, nl_rate);
1473 nla_nest_end(msg, nl_rates);
1475 return 0;
1478 static int
1479 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1480 const struct ieee80211_txrx_stypes *mgmt_stypes)
1482 u16 stypes;
1483 struct nlattr *nl_ftypes, *nl_ifs;
1484 enum nl80211_iftype ift;
1485 int i;
1487 if (!mgmt_stypes)
1488 return 0;
1490 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1491 if (!nl_ifs)
1492 return -ENOBUFS;
1494 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1495 nl_ftypes = nla_nest_start(msg, ift);
1496 if (!nl_ftypes)
1497 return -ENOBUFS;
1498 i = 0;
1499 stypes = mgmt_stypes[ift].tx;
1500 while (stypes) {
1501 if ((stypes & 1) &&
1502 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1503 (i << 4) | IEEE80211_FTYPE_MGMT))
1504 return -ENOBUFS;
1505 stypes >>= 1;
1506 i++;
1508 nla_nest_end(msg, nl_ftypes);
1511 nla_nest_end(msg, nl_ifs);
1513 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1514 if (!nl_ifs)
1515 return -ENOBUFS;
1517 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1518 nl_ftypes = nla_nest_start(msg, ift);
1519 if (!nl_ftypes)
1520 return -ENOBUFS;
1521 i = 0;
1522 stypes = mgmt_stypes[ift].rx;
1523 while (stypes) {
1524 if ((stypes & 1) &&
1525 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1526 (i << 4) | IEEE80211_FTYPE_MGMT))
1527 return -ENOBUFS;
1528 stypes >>= 1;
1529 i++;
1531 nla_nest_end(msg, nl_ftypes);
1533 nla_nest_end(msg, nl_ifs);
1535 return 0;
1538 #define CMD(op, n) \
1539 do { \
1540 if (rdev->ops->op) { \
1541 i++; \
1542 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1543 goto nla_put_failure; \
1545 } while (0)
1547 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1548 struct sk_buff *msg)
1550 int i = 0;
1553 * do *NOT* add anything into this function, new things need to be
1554 * advertised only to new versions of userspace that can deal with
1555 * the split (and they can't possibly care about new features...
1557 CMD(add_virtual_intf, NEW_INTERFACE);
1558 CMD(change_virtual_intf, SET_INTERFACE);
1559 CMD(add_key, NEW_KEY);
1560 CMD(start_ap, START_AP);
1561 CMD(add_station, NEW_STATION);
1562 CMD(add_mpath, NEW_MPATH);
1563 CMD(update_mesh_config, SET_MESH_CONFIG);
1564 CMD(change_bss, SET_BSS);
1565 CMD(auth, AUTHENTICATE);
1566 CMD(assoc, ASSOCIATE);
1567 CMD(deauth, DEAUTHENTICATE);
1568 CMD(disassoc, DISASSOCIATE);
1569 CMD(join_ibss, JOIN_IBSS);
1570 CMD(join_mesh, JOIN_MESH);
1571 CMD(set_pmksa, SET_PMKSA);
1572 CMD(del_pmksa, DEL_PMKSA);
1573 CMD(flush_pmksa, FLUSH_PMKSA);
1574 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1575 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1576 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1577 CMD(mgmt_tx, FRAME);
1578 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1579 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1580 i++;
1581 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1582 goto nla_put_failure;
1584 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1585 rdev->ops->join_mesh) {
1586 i++;
1587 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1588 goto nla_put_failure;
1590 CMD(set_wds_peer, SET_WDS_PEER);
1591 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1592 CMD(tdls_mgmt, TDLS_MGMT);
1593 CMD(tdls_oper, TDLS_OPER);
1595 if (rdev->wiphy.max_sched_scan_reqs)
1596 CMD(sched_scan_start, START_SCHED_SCAN);
1597 CMD(probe_client, PROBE_CLIENT);
1598 CMD(set_noack_map, SET_NOACK_MAP);
1599 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1600 i++;
1601 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1602 goto nla_put_failure;
1604 CMD(start_p2p_device, START_P2P_DEVICE);
1605 CMD(set_mcast_rate, SET_MCAST_RATE);
1606 #ifdef CONFIG_NL80211_TESTMODE
1607 CMD(testmode_cmd, TESTMODE);
1608 #endif
1610 if (rdev->ops->connect || rdev->ops->auth) {
1611 i++;
1612 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1613 goto nla_put_failure;
1616 if (rdev->ops->disconnect || rdev->ops->deauth) {
1617 i++;
1618 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1619 goto nla_put_failure;
1622 return i;
1623 nla_put_failure:
1624 return -ENOBUFS;
1627 struct nl80211_dump_wiphy_state {
1628 s64 filter_wiphy;
1629 long start;
1630 long split_start, band_start, chan_start, capa_start;
1631 bool split;
1634 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1635 enum nl80211_commands cmd,
1636 struct sk_buff *msg, u32 portid, u32 seq,
1637 int flags, struct nl80211_dump_wiphy_state *state)
1639 void *hdr;
1640 struct nlattr *nl_bands, *nl_band;
1641 struct nlattr *nl_freqs, *nl_freq;
1642 struct nlattr *nl_cmds;
1643 enum nl80211_band band;
1644 struct ieee80211_channel *chan;
1645 int i;
1646 const struct ieee80211_txrx_stypes *mgmt_stypes =
1647 rdev->wiphy.mgmt_stypes;
1648 u32 features;
1650 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1651 if (!hdr)
1652 return -ENOBUFS;
1654 if (WARN_ON(!state))
1655 return -EINVAL;
1657 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1658 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1659 wiphy_name(&rdev->wiphy)) ||
1660 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1661 cfg80211_rdev_list_generation))
1662 goto nla_put_failure;
1664 if (cmd != NL80211_CMD_NEW_WIPHY)
1665 goto finish;
1667 switch (state->split_start) {
1668 case 0:
1669 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1670 rdev->wiphy.retry_short) ||
1671 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1672 rdev->wiphy.retry_long) ||
1673 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1674 rdev->wiphy.frag_threshold) ||
1675 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1676 rdev->wiphy.rts_threshold) ||
1677 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1678 rdev->wiphy.coverage_class) ||
1679 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1680 rdev->wiphy.max_scan_ssids) ||
1681 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1682 rdev->wiphy.max_sched_scan_ssids) ||
1683 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1684 rdev->wiphy.max_scan_ie_len) ||
1685 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1686 rdev->wiphy.max_sched_scan_ie_len) ||
1687 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1688 rdev->wiphy.max_match_sets) ||
1689 nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1690 rdev->wiphy.max_sched_scan_plans) ||
1691 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1692 rdev->wiphy.max_sched_scan_plan_interval) ||
1693 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1694 rdev->wiphy.max_sched_scan_plan_iterations))
1695 goto nla_put_failure;
1697 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1698 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1699 goto nla_put_failure;
1700 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1701 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1702 goto nla_put_failure;
1703 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1704 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1705 goto nla_put_failure;
1706 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1707 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1708 goto nla_put_failure;
1709 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1710 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1711 goto nla_put_failure;
1712 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1713 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1714 goto nla_put_failure;
1715 state->split_start++;
1716 if (state->split)
1717 break;
1718 case 1:
1719 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1720 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1721 rdev->wiphy.cipher_suites))
1722 goto nla_put_failure;
1724 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1725 rdev->wiphy.max_num_pmkids))
1726 goto nla_put_failure;
1728 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1729 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1730 goto nla_put_failure;
1732 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1733 rdev->wiphy.available_antennas_tx) ||
1734 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1735 rdev->wiphy.available_antennas_rx))
1736 goto nla_put_failure;
1738 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1739 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1740 rdev->wiphy.probe_resp_offload))
1741 goto nla_put_failure;
1743 if ((rdev->wiphy.available_antennas_tx ||
1744 rdev->wiphy.available_antennas_rx) &&
1745 rdev->ops->get_antenna) {
1746 u32 tx_ant = 0, rx_ant = 0;
1747 int res;
1749 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1750 if (!res) {
1751 if (nla_put_u32(msg,
1752 NL80211_ATTR_WIPHY_ANTENNA_TX,
1753 tx_ant) ||
1754 nla_put_u32(msg,
1755 NL80211_ATTR_WIPHY_ANTENNA_RX,
1756 rx_ant))
1757 goto nla_put_failure;
1761 state->split_start++;
1762 if (state->split)
1763 break;
1764 case 2:
1765 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1766 rdev->wiphy.interface_modes))
1767 goto nla_put_failure;
1768 state->split_start++;
1769 if (state->split)
1770 break;
1771 case 3:
1772 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1773 if (!nl_bands)
1774 goto nla_put_failure;
1776 for (band = state->band_start;
1777 band < NUM_NL80211_BANDS; band++) {
1778 struct ieee80211_supported_band *sband;
1780 sband = rdev->wiphy.bands[band];
1782 if (!sband)
1783 continue;
1785 nl_band = nla_nest_start(msg, band);
1786 if (!nl_band)
1787 goto nla_put_failure;
1789 switch (state->chan_start) {
1790 case 0:
1791 if (nl80211_send_band_rateinfo(msg, sband))
1792 goto nla_put_failure;
1793 state->chan_start++;
1794 if (state->split)
1795 break;
1796 default:
1797 /* add frequencies */
1798 nl_freqs = nla_nest_start(
1799 msg, NL80211_BAND_ATTR_FREQS);
1800 if (!nl_freqs)
1801 goto nla_put_failure;
1803 for (i = state->chan_start - 1;
1804 i < sband->n_channels;
1805 i++) {
1806 nl_freq = nla_nest_start(msg, i);
1807 if (!nl_freq)
1808 goto nla_put_failure;
1810 chan = &sband->channels[i];
1812 if (nl80211_msg_put_channel(
1813 msg, &rdev->wiphy, chan,
1814 state->split))
1815 goto nla_put_failure;
1817 nla_nest_end(msg, nl_freq);
1818 if (state->split)
1819 break;
1821 if (i < sband->n_channels)
1822 state->chan_start = i + 2;
1823 else
1824 state->chan_start = 0;
1825 nla_nest_end(msg, nl_freqs);
1828 nla_nest_end(msg, nl_band);
1830 if (state->split) {
1831 /* start again here */
1832 if (state->chan_start)
1833 band--;
1834 break;
1837 nla_nest_end(msg, nl_bands);
1839 if (band < NUM_NL80211_BANDS)
1840 state->band_start = band + 1;
1841 else
1842 state->band_start = 0;
1844 /* if bands & channels are done, continue outside */
1845 if (state->band_start == 0 && state->chan_start == 0)
1846 state->split_start++;
1847 if (state->split)
1848 break;
1849 case 4:
1850 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1851 if (!nl_cmds)
1852 goto nla_put_failure;
1854 i = nl80211_add_commands_unsplit(rdev, msg);
1855 if (i < 0)
1856 goto nla_put_failure;
1857 if (state->split) {
1858 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1859 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1860 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1861 CMD(channel_switch, CHANNEL_SWITCH);
1862 CMD(set_qos_map, SET_QOS_MAP);
1863 if (rdev->wiphy.features &
1864 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1865 CMD(add_tx_ts, ADD_TX_TS);
1866 CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
1867 CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
1869 #undef CMD
1871 nla_nest_end(msg, nl_cmds);
1872 state->split_start++;
1873 if (state->split)
1874 break;
1875 case 5:
1876 if (rdev->ops->remain_on_channel &&
1877 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1878 nla_put_u32(msg,
1879 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1880 rdev->wiphy.max_remain_on_channel_duration))
1881 goto nla_put_failure;
1883 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1884 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1885 goto nla_put_failure;
1887 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1888 goto nla_put_failure;
1889 state->split_start++;
1890 if (state->split)
1891 break;
1892 case 6:
1893 #ifdef CONFIG_PM
1894 if (nl80211_send_wowlan(msg, rdev, state->split))
1895 goto nla_put_failure;
1896 state->split_start++;
1897 if (state->split)
1898 break;
1899 #else
1900 state->split_start++;
1901 #endif
1902 case 7:
1903 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1904 rdev->wiphy.software_iftypes))
1905 goto nla_put_failure;
1907 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1908 state->split))
1909 goto nla_put_failure;
1911 state->split_start++;
1912 if (state->split)
1913 break;
1914 case 8:
1915 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1916 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1917 rdev->wiphy.ap_sme_capa))
1918 goto nla_put_failure;
1920 features = rdev->wiphy.features;
1922 * We can only add the per-channel limit information if the
1923 * dump is split, otherwise it makes it too big. Therefore
1924 * only advertise it in that case.
1926 if (state->split)
1927 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1928 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1929 goto nla_put_failure;
1931 if (rdev->wiphy.ht_capa_mod_mask &&
1932 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1933 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1934 rdev->wiphy.ht_capa_mod_mask))
1935 goto nla_put_failure;
1937 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1938 rdev->wiphy.max_acl_mac_addrs &&
1939 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1940 rdev->wiphy.max_acl_mac_addrs))
1941 goto nla_put_failure;
1944 * Any information below this point is only available to
1945 * applications that can deal with it being split. This
1946 * helps ensure that newly added capabilities don't break
1947 * older tools by overrunning their buffers.
1949 * We still increment split_start so that in the split
1950 * case we'll continue with more data in the next round,
1951 * but break unconditionally so unsplit data stops here.
1953 state->split_start++;
1954 break;
1955 case 9:
1956 if (rdev->wiphy.extended_capabilities &&
1957 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1958 rdev->wiphy.extended_capabilities_len,
1959 rdev->wiphy.extended_capabilities) ||
1960 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1961 rdev->wiphy.extended_capabilities_len,
1962 rdev->wiphy.extended_capabilities_mask)))
1963 goto nla_put_failure;
1965 if (rdev->wiphy.vht_capa_mod_mask &&
1966 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1967 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1968 rdev->wiphy.vht_capa_mod_mask))
1969 goto nla_put_failure;
1971 state->split_start++;
1972 break;
1973 case 10:
1974 if (nl80211_send_coalesce(msg, rdev))
1975 goto nla_put_failure;
1977 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1978 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1979 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1980 goto nla_put_failure;
1982 if (rdev->wiphy.max_ap_assoc_sta &&
1983 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1984 rdev->wiphy.max_ap_assoc_sta))
1985 goto nla_put_failure;
1987 state->split_start++;
1988 break;
1989 case 11:
1990 if (rdev->wiphy.n_vendor_commands) {
1991 const struct nl80211_vendor_cmd_info *info;
1992 struct nlattr *nested;
1994 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1995 if (!nested)
1996 goto nla_put_failure;
1998 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1999 info = &rdev->wiphy.vendor_commands[i].info;
2000 if (nla_put(msg, i + 1, sizeof(*info), info))
2001 goto nla_put_failure;
2003 nla_nest_end(msg, nested);
2006 if (rdev->wiphy.n_vendor_events) {
2007 const struct nl80211_vendor_cmd_info *info;
2008 struct nlattr *nested;
2010 nested = nla_nest_start(msg,
2011 NL80211_ATTR_VENDOR_EVENTS);
2012 if (!nested)
2013 goto nla_put_failure;
2015 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2016 info = &rdev->wiphy.vendor_events[i];
2017 if (nla_put(msg, i + 1, sizeof(*info), info))
2018 goto nla_put_failure;
2020 nla_nest_end(msg, nested);
2022 state->split_start++;
2023 break;
2024 case 12:
2025 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2026 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2027 rdev->wiphy.max_num_csa_counters))
2028 goto nla_put_failure;
2030 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2031 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2032 goto nla_put_failure;
2034 if (rdev->wiphy.max_sched_scan_reqs &&
2035 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2036 rdev->wiphy.max_sched_scan_reqs))
2037 goto nla_put_failure;
2039 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2040 sizeof(rdev->wiphy.ext_features),
2041 rdev->wiphy.ext_features))
2042 goto nla_put_failure;
2044 if (rdev->wiphy.bss_select_support) {
2045 struct nlattr *nested;
2046 u32 bss_select_support = rdev->wiphy.bss_select_support;
2048 nested = nla_nest_start(msg, NL80211_ATTR_BSS_SELECT);
2049 if (!nested)
2050 goto nla_put_failure;
2052 i = 0;
2053 while (bss_select_support) {
2054 if ((bss_select_support & 1) &&
2055 nla_put_flag(msg, i))
2056 goto nla_put_failure;
2057 i++;
2058 bss_select_support >>= 1;
2060 nla_nest_end(msg, nested);
2063 state->split_start++;
2064 break;
2065 case 13:
2066 if (rdev->wiphy.num_iftype_ext_capab &&
2067 rdev->wiphy.iftype_ext_capab) {
2068 struct nlattr *nested_ext_capab, *nested;
2070 nested = nla_nest_start(msg,
2071 NL80211_ATTR_IFTYPE_EXT_CAPA);
2072 if (!nested)
2073 goto nla_put_failure;
2075 for (i = state->capa_start;
2076 i < rdev->wiphy.num_iftype_ext_capab; i++) {
2077 const struct wiphy_iftype_ext_capab *capab;
2079 capab = &rdev->wiphy.iftype_ext_capab[i];
2081 nested_ext_capab = nla_nest_start(msg, i);
2082 if (!nested_ext_capab ||
2083 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2084 capab->iftype) ||
2085 nla_put(msg, NL80211_ATTR_EXT_CAPA,
2086 capab->extended_capabilities_len,
2087 capab->extended_capabilities) ||
2088 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2089 capab->extended_capabilities_len,
2090 capab->extended_capabilities_mask))
2091 goto nla_put_failure;
2093 nla_nest_end(msg, nested_ext_capab);
2094 if (state->split)
2095 break;
2097 nla_nest_end(msg, nested);
2098 if (i < rdev->wiphy.num_iftype_ext_capab) {
2099 state->capa_start = i + 1;
2100 break;
2104 if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2105 rdev->wiphy.nan_supported_bands))
2106 goto nla_put_failure;
2108 if (wiphy_ext_feature_isset(&rdev->wiphy,
2109 NL80211_EXT_FEATURE_TXQS)) {
2110 struct cfg80211_txq_stats txqstats = {};
2111 int res;
2113 res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2114 if (!res &&
2115 !nl80211_put_txq_stats(msg, &txqstats,
2116 NL80211_ATTR_TXQ_STATS))
2117 goto nla_put_failure;
2119 if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2120 rdev->wiphy.txq_limit))
2121 goto nla_put_failure;
2122 if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2123 rdev->wiphy.txq_memory_limit))
2124 goto nla_put_failure;
2125 if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2126 rdev->wiphy.txq_quantum))
2127 goto nla_put_failure;
2130 /* done */
2131 state->split_start = 0;
2132 break;
2134 finish:
2135 genlmsg_end(msg, hdr);
2136 return 0;
2138 nla_put_failure:
2139 genlmsg_cancel(msg, hdr);
2140 return -EMSGSIZE;
2143 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2144 struct netlink_callback *cb,
2145 struct nl80211_dump_wiphy_state *state)
2147 struct nlattr **tb = genl_family_attrbuf(&nl80211_fam);
2148 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, tb,
2149 nl80211_fam.maxattr, nl80211_policy, NULL);
2150 /* ignore parse errors for backward compatibility */
2151 if (ret)
2152 return 0;
2154 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2155 if (tb[NL80211_ATTR_WIPHY])
2156 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2157 if (tb[NL80211_ATTR_WDEV])
2158 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2159 if (tb[NL80211_ATTR_IFINDEX]) {
2160 struct net_device *netdev;
2161 struct cfg80211_registered_device *rdev;
2162 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2164 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2165 if (!netdev)
2166 return -ENODEV;
2167 if (netdev->ieee80211_ptr) {
2168 rdev = wiphy_to_rdev(
2169 netdev->ieee80211_ptr->wiphy);
2170 state->filter_wiphy = rdev->wiphy_idx;
2174 return 0;
2177 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2179 int idx = 0, ret;
2180 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2181 struct cfg80211_registered_device *rdev;
2183 rtnl_lock();
2184 if (!state) {
2185 state = kzalloc(sizeof(*state), GFP_KERNEL);
2186 if (!state) {
2187 rtnl_unlock();
2188 return -ENOMEM;
2190 state->filter_wiphy = -1;
2191 ret = nl80211_dump_wiphy_parse(skb, cb, state);
2192 if (ret) {
2193 kfree(state);
2194 rtnl_unlock();
2195 return ret;
2197 cb->args[0] = (long)state;
2200 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2201 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2202 continue;
2203 if (++idx <= state->start)
2204 continue;
2205 if (state->filter_wiphy != -1 &&
2206 state->filter_wiphy != rdev->wiphy_idx)
2207 continue;
2208 /* attempt to fit multiple wiphy data chunks into the skb */
2209 do {
2210 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2211 skb,
2212 NETLINK_CB(cb->skb).portid,
2213 cb->nlh->nlmsg_seq,
2214 NLM_F_MULTI, state);
2215 if (ret < 0) {
2217 * If sending the wiphy data didn't fit (ENOBUFS
2218 * or EMSGSIZE returned), this SKB is still
2219 * empty (so it's not too big because another
2220 * wiphy dataset is already in the skb) and
2221 * we've not tried to adjust the dump allocation
2222 * yet ... then adjust the alloc size to be
2223 * bigger, and return 1 but with the empty skb.
2224 * This results in an empty message being RX'ed
2225 * in userspace, but that is ignored.
2227 * We can then retry with the larger buffer.
2229 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2230 !skb->len && !state->split &&
2231 cb->min_dump_alloc < 4096) {
2232 cb->min_dump_alloc = 4096;
2233 state->split_start = 0;
2234 rtnl_unlock();
2235 return 1;
2237 idx--;
2238 break;
2240 } while (state->split_start > 0);
2241 break;
2243 rtnl_unlock();
2245 state->start = idx;
2247 return skb->len;
2250 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2252 kfree((void *)cb->args[0]);
2253 return 0;
2256 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2258 struct sk_buff *msg;
2259 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2260 struct nl80211_dump_wiphy_state state = {};
2262 msg = nlmsg_new(4096, GFP_KERNEL);
2263 if (!msg)
2264 return -ENOMEM;
2266 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2267 info->snd_portid, info->snd_seq, 0,
2268 &state) < 0) {
2269 nlmsg_free(msg);
2270 return -ENOBUFS;
2273 return genlmsg_reply(msg, info);
2276 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2277 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
2278 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
2279 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
2280 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
2281 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
2284 static int parse_txq_params(struct nlattr *tb[],
2285 struct ieee80211_txq_params *txq_params)
2287 u8 ac;
2289 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2290 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2291 !tb[NL80211_TXQ_ATTR_AIFS])
2292 return -EINVAL;
2294 ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2295 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2296 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2297 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2298 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2300 if (ac >= NL80211_NUM_ACS)
2301 return -EINVAL;
2302 txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2303 return 0;
2306 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2309 * You can only set the channel explicitly for WDS interfaces,
2310 * all others have their channel managed via their respective
2311 * "establish a connection" command (connect, join, ...)
2313 * For AP/GO and mesh mode, the channel can be set with the
2314 * channel userspace API, but is only stored and passed to the
2315 * low-level driver when the AP starts or the mesh is joined.
2316 * This is for backward compatibility, userspace can also give
2317 * the channel in the start-ap or join-mesh commands instead.
2319 * Monitors are special as they are normally slaved to
2320 * whatever else is going on, so they have their own special
2321 * operation to set the monitor channel if possible.
2323 return !wdev ||
2324 wdev->iftype == NL80211_IFTYPE_AP ||
2325 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2326 wdev->iftype == NL80211_IFTYPE_MONITOR ||
2327 wdev->iftype == NL80211_IFTYPE_P2P_GO;
2330 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2331 struct genl_info *info,
2332 struct cfg80211_chan_def *chandef)
2334 u32 control_freq;
2336 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
2337 return -EINVAL;
2339 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
2341 memset(chandef, 0, sizeof(*chandef));
2343 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2344 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2345 chandef->center_freq1 = control_freq;
2346 chandef->center_freq2 = 0;
2348 /* Primary channel not allowed */
2349 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
2350 return -EINVAL;
2352 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2353 enum nl80211_channel_type chantype;
2355 chantype = nla_get_u32(
2356 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2358 switch (chantype) {
2359 case NL80211_CHAN_NO_HT:
2360 case NL80211_CHAN_HT20:
2361 case NL80211_CHAN_HT40PLUS:
2362 case NL80211_CHAN_HT40MINUS:
2363 cfg80211_chandef_create(chandef, chandef->chan,
2364 chantype);
2365 /* user input for center_freq is incorrect */
2366 if (info->attrs[NL80211_ATTR_CENTER_FREQ1] &&
2367 chandef->center_freq1 != nla_get_u32(
2368 info->attrs[NL80211_ATTR_CENTER_FREQ1]))
2369 return -EINVAL;
2370 /* center_freq2 must be zero */
2371 if (info->attrs[NL80211_ATTR_CENTER_FREQ2] &&
2372 nla_get_u32(info->attrs[NL80211_ATTR_CENTER_FREQ2]))
2373 return -EINVAL;
2374 break;
2375 default:
2376 return -EINVAL;
2378 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2379 chandef->width =
2380 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2381 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
2382 chandef->center_freq1 =
2383 nla_get_u32(
2384 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
2385 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
2386 chandef->center_freq2 =
2387 nla_get_u32(
2388 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
2391 if (!cfg80211_chandef_valid(chandef))
2392 return -EINVAL;
2394 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2395 IEEE80211_CHAN_DISABLED))
2396 return -EINVAL;
2398 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2399 chandef->width == NL80211_CHAN_WIDTH_10) &&
2400 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
2401 return -EINVAL;
2403 return 0;
2406 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2407 struct net_device *dev,
2408 struct genl_info *info)
2410 struct cfg80211_chan_def chandef;
2411 int result;
2412 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2413 struct wireless_dev *wdev = NULL;
2415 if (dev)
2416 wdev = dev->ieee80211_ptr;
2417 if (!nl80211_can_set_dev_channel(wdev))
2418 return -EOPNOTSUPP;
2419 if (wdev)
2420 iftype = wdev->iftype;
2422 result = nl80211_parse_chandef(rdev, info, &chandef);
2423 if (result)
2424 return result;
2426 switch (iftype) {
2427 case NL80211_IFTYPE_AP:
2428 case NL80211_IFTYPE_P2P_GO:
2429 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2430 iftype)) {
2431 result = -EINVAL;
2432 break;
2434 if (wdev->beacon_interval) {
2435 if (!dev || !rdev->ops->set_ap_chanwidth ||
2436 !(rdev->wiphy.features &
2437 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2438 result = -EBUSY;
2439 break;
2442 /* Only allow dynamic channel width changes */
2443 if (chandef.chan != wdev->preset_chandef.chan) {
2444 result = -EBUSY;
2445 break;
2447 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2448 if (result)
2449 break;
2451 wdev->preset_chandef = chandef;
2452 result = 0;
2453 break;
2454 case NL80211_IFTYPE_MESH_POINT:
2455 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2456 break;
2457 case NL80211_IFTYPE_MONITOR:
2458 result = cfg80211_set_monitor_channel(rdev, &chandef);
2459 break;
2460 default:
2461 result = -EINVAL;
2464 return result;
2467 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2470 struct net_device *netdev = info->user_ptr[1];
2472 return __nl80211_set_channel(rdev, netdev, info);
2475 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2477 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2478 struct net_device *dev = info->user_ptr[1];
2479 struct wireless_dev *wdev = dev->ieee80211_ptr;
2480 const u8 *bssid;
2482 if (!info->attrs[NL80211_ATTR_MAC])
2483 return -EINVAL;
2485 if (netif_running(dev))
2486 return -EBUSY;
2488 if (!rdev->ops->set_wds_peer)
2489 return -EOPNOTSUPP;
2491 if (wdev->iftype != NL80211_IFTYPE_WDS)
2492 return -EOPNOTSUPP;
2494 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2495 return rdev_set_wds_peer(rdev, dev, bssid);
2498 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2500 struct cfg80211_registered_device *rdev;
2501 struct net_device *netdev = NULL;
2502 struct wireless_dev *wdev;
2503 int result = 0, rem_txq_params = 0;
2504 struct nlattr *nl_txq_params;
2505 u32 changed;
2506 u8 retry_short = 0, retry_long = 0;
2507 u32 frag_threshold = 0, rts_threshold = 0;
2508 u8 coverage_class = 0;
2509 u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2511 ASSERT_RTNL();
2514 * Try to find the wiphy and netdev. Normally this
2515 * function shouldn't need the netdev, but this is
2516 * done for backward compatibility -- previously
2517 * setting the channel was done per wiphy, but now
2518 * it is per netdev. Previous userland like hostapd
2519 * also passed a netdev to set_wiphy, so that it is
2520 * possible to let that go to the right netdev!
2523 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2524 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2526 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2527 if (netdev && netdev->ieee80211_ptr)
2528 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2529 else
2530 netdev = NULL;
2533 if (!netdev) {
2534 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2535 info->attrs);
2536 if (IS_ERR(rdev))
2537 return PTR_ERR(rdev);
2538 wdev = NULL;
2539 netdev = NULL;
2540 result = 0;
2541 } else
2542 wdev = netdev->ieee80211_ptr;
2545 * end workaround code, by now the rdev is available
2546 * and locked, and wdev may or may not be NULL.
2549 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2550 result = cfg80211_dev_rename(
2551 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2553 if (result)
2554 return result;
2556 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2557 struct ieee80211_txq_params txq_params;
2558 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2560 if (!rdev->ops->set_txq_params)
2561 return -EOPNOTSUPP;
2563 if (!netdev)
2564 return -EINVAL;
2566 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2567 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2568 return -EINVAL;
2570 if (!netif_running(netdev))
2571 return -ENETDOWN;
2573 nla_for_each_nested(nl_txq_params,
2574 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2575 rem_txq_params) {
2576 result = nla_parse_nested(tb, NL80211_TXQ_ATTR_MAX,
2577 nl_txq_params,
2578 txq_params_policy,
2579 info->extack);
2580 if (result)
2581 return result;
2582 result = parse_txq_params(tb, &txq_params);
2583 if (result)
2584 return result;
2586 result = rdev_set_txq_params(rdev, netdev,
2587 &txq_params);
2588 if (result)
2589 return result;
2593 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2594 result = __nl80211_set_channel(
2595 rdev,
2596 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2597 info);
2598 if (result)
2599 return result;
2602 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2603 struct wireless_dev *txp_wdev = wdev;
2604 enum nl80211_tx_power_setting type;
2605 int idx, mbm = 0;
2607 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2608 txp_wdev = NULL;
2610 if (!rdev->ops->set_tx_power)
2611 return -EOPNOTSUPP;
2613 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2614 type = nla_get_u32(info->attrs[idx]);
2616 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2617 (type != NL80211_TX_POWER_AUTOMATIC))
2618 return -EINVAL;
2620 if (type != NL80211_TX_POWER_AUTOMATIC) {
2621 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2622 mbm = nla_get_u32(info->attrs[idx]);
2625 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2626 if (result)
2627 return result;
2630 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2631 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2632 u32 tx_ant, rx_ant;
2634 if ((!rdev->wiphy.available_antennas_tx &&
2635 !rdev->wiphy.available_antennas_rx) ||
2636 !rdev->ops->set_antenna)
2637 return -EOPNOTSUPP;
2639 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2640 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2642 /* reject antenna configurations which don't match the
2643 * available antenna masks, except for the "all" mask */
2644 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2645 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2646 return -EINVAL;
2648 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2649 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2651 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2652 if (result)
2653 return result;
2656 changed = 0;
2658 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2659 retry_short = nla_get_u8(
2660 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2661 if (retry_short == 0)
2662 return -EINVAL;
2664 changed |= WIPHY_PARAM_RETRY_SHORT;
2667 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2668 retry_long = nla_get_u8(
2669 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2670 if (retry_long == 0)
2671 return -EINVAL;
2673 changed |= WIPHY_PARAM_RETRY_LONG;
2676 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2677 frag_threshold = nla_get_u32(
2678 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2679 if (frag_threshold < 256)
2680 return -EINVAL;
2682 if (frag_threshold != (u32) -1) {
2684 * Fragments (apart from the last one) are required to
2685 * have even length. Make the fragmentation code
2686 * simpler by stripping LSB should someone try to use
2687 * odd threshold value.
2689 frag_threshold &= ~0x1;
2691 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2694 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2695 rts_threshold = nla_get_u32(
2696 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2697 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2700 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2701 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2702 return -EINVAL;
2704 coverage_class = nla_get_u8(
2705 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2706 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2709 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2710 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2711 return -EOPNOTSUPP;
2713 changed |= WIPHY_PARAM_DYN_ACK;
2716 if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
2717 if (!wiphy_ext_feature_isset(&rdev->wiphy,
2718 NL80211_EXT_FEATURE_TXQS))
2719 return -EOPNOTSUPP;
2720 txq_limit = nla_get_u32(
2721 info->attrs[NL80211_ATTR_TXQ_LIMIT]);
2722 changed |= WIPHY_PARAM_TXQ_LIMIT;
2725 if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
2726 if (!wiphy_ext_feature_isset(&rdev->wiphy,
2727 NL80211_EXT_FEATURE_TXQS))
2728 return -EOPNOTSUPP;
2729 txq_memory_limit = nla_get_u32(
2730 info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
2731 changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
2734 if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
2735 if (!wiphy_ext_feature_isset(&rdev->wiphy,
2736 NL80211_EXT_FEATURE_TXQS))
2737 return -EOPNOTSUPP;
2738 txq_quantum = nla_get_u32(
2739 info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
2740 changed |= WIPHY_PARAM_TXQ_QUANTUM;
2743 if (changed) {
2744 u8 old_retry_short, old_retry_long;
2745 u32 old_frag_threshold, old_rts_threshold;
2746 u8 old_coverage_class;
2747 u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
2749 if (!rdev->ops->set_wiphy_params)
2750 return -EOPNOTSUPP;
2752 old_retry_short = rdev->wiphy.retry_short;
2753 old_retry_long = rdev->wiphy.retry_long;
2754 old_frag_threshold = rdev->wiphy.frag_threshold;
2755 old_rts_threshold = rdev->wiphy.rts_threshold;
2756 old_coverage_class = rdev->wiphy.coverage_class;
2757 old_txq_limit = rdev->wiphy.txq_limit;
2758 old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
2759 old_txq_quantum = rdev->wiphy.txq_quantum;
2761 if (changed & WIPHY_PARAM_RETRY_SHORT)
2762 rdev->wiphy.retry_short = retry_short;
2763 if (changed & WIPHY_PARAM_RETRY_LONG)
2764 rdev->wiphy.retry_long = retry_long;
2765 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2766 rdev->wiphy.frag_threshold = frag_threshold;
2767 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2768 rdev->wiphy.rts_threshold = rts_threshold;
2769 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2770 rdev->wiphy.coverage_class = coverage_class;
2771 if (changed & WIPHY_PARAM_TXQ_LIMIT)
2772 rdev->wiphy.txq_limit = txq_limit;
2773 if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
2774 rdev->wiphy.txq_memory_limit = txq_memory_limit;
2775 if (changed & WIPHY_PARAM_TXQ_QUANTUM)
2776 rdev->wiphy.txq_quantum = txq_quantum;
2778 result = rdev_set_wiphy_params(rdev, changed);
2779 if (result) {
2780 rdev->wiphy.retry_short = old_retry_short;
2781 rdev->wiphy.retry_long = old_retry_long;
2782 rdev->wiphy.frag_threshold = old_frag_threshold;
2783 rdev->wiphy.rts_threshold = old_rts_threshold;
2784 rdev->wiphy.coverage_class = old_coverage_class;
2785 rdev->wiphy.txq_limit = old_txq_limit;
2786 rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
2787 rdev->wiphy.txq_quantum = old_txq_quantum;
2788 return result;
2791 return 0;
2794 static inline u64 wdev_id(struct wireless_dev *wdev)
2796 return (u64)wdev->identifier |
2797 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2800 static int nl80211_send_chandef(struct sk_buff *msg,
2801 const struct cfg80211_chan_def *chandef)
2803 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2804 return -EINVAL;
2806 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2807 chandef->chan->center_freq))
2808 return -ENOBUFS;
2809 switch (chandef->width) {
2810 case NL80211_CHAN_WIDTH_20_NOHT:
2811 case NL80211_CHAN_WIDTH_20:
2812 case NL80211_CHAN_WIDTH_40:
2813 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2814 cfg80211_get_chandef_type(chandef)))
2815 return -ENOBUFS;
2816 break;
2817 default:
2818 break;
2820 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2821 return -ENOBUFS;
2822 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2823 return -ENOBUFS;
2824 if (chandef->center_freq2 &&
2825 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2826 return -ENOBUFS;
2827 return 0;
2830 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2831 struct cfg80211_registered_device *rdev,
2832 struct wireless_dev *wdev, bool removal)
2834 struct net_device *dev = wdev->netdev;
2835 u8 cmd = NL80211_CMD_NEW_INTERFACE;
2836 void *hdr;
2838 if (removal)
2839 cmd = NL80211_CMD_DEL_INTERFACE;
2841 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2842 if (!hdr)
2843 return -1;
2845 if (dev &&
2846 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2847 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2848 goto nla_put_failure;
2850 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2851 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2852 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
2853 NL80211_ATTR_PAD) ||
2854 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2855 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2856 rdev->devlist_generation ^
2857 (cfg80211_rdev_list_generation << 2)) ||
2858 nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
2859 goto nla_put_failure;
2861 if (rdev->ops->get_channel) {
2862 int ret;
2863 struct cfg80211_chan_def chandef = {};
2865 ret = rdev_get_channel(rdev, wdev, &chandef);
2866 if (ret == 0) {
2867 if (nl80211_send_chandef(msg, &chandef))
2868 goto nla_put_failure;
2872 if (rdev->ops->get_tx_power) {
2873 int dbm, ret;
2875 ret = rdev_get_tx_power(rdev, wdev, &dbm);
2876 if (ret == 0 &&
2877 nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2878 DBM_TO_MBM(dbm)))
2879 goto nla_put_failure;
2882 wdev_lock(wdev);
2883 switch (wdev->iftype) {
2884 case NL80211_IFTYPE_AP:
2885 if (wdev->ssid_len &&
2886 nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2887 goto nla_put_failure_locked;
2888 break;
2889 case NL80211_IFTYPE_STATION:
2890 case NL80211_IFTYPE_P2P_CLIENT:
2891 case NL80211_IFTYPE_ADHOC: {
2892 const u8 *ssid_ie;
2893 if (!wdev->current_bss)
2894 break;
2895 rcu_read_lock();
2896 ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
2897 WLAN_EID_SSID);
2898 if (ssid_ie &&
2899 nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
2900 goto nla_put_failure_rcu_locked;
2901 rcu_read_unlock();
2902 break;
2904 default:
2905 /* nothing */
2906 break;
2908 wdev_unlock(wdev);
2910 if (rdev->ops->get_txq_stats) {
2911 struct cfg80211_txq_stats txqstats = {};
2912 int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
2914 if (ret == 0 &&
2915 !nl80211_put_txq_stats(msg, &txqstats,
2916 NL80211_ATTR_TXQ_STATS))
2917 goto nla_put_failure;
2920 genlmsg_end(msg, hdr);
2921 return 0;
2923 nla_put_failure_rcu_locked:
2924 rcu_read_unlock();
2925 nla_put_failure_locked:
2926 wdev_unlock(wdev);
2927 nla_put_failure:
2928 genlmsg_cancel(msg, hdr);
2929 return -EMSGSIZE;
2932 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2934 int wp_idx = 0;
2935 int if_idx = 0;
2936 int wp_start = cb->args[0];
2937 int if_start = cb->args[1];
2938 int filter_wiphy = -1;
2939 struct cfg80211_registered_device *rdev;
2940 struct wireless_dev *wdev;
2941 int ret;
2943 rtnl_lock();
2944 if (!cb->args[2]) {
2945 struct nl80211_dump_wiphy_state state = {
2946 .filter_wiphy = -1,
2949 ret = nl80211_dump_wiphy_parse(skb, cb, &state);
2950 if (ret)
2951 goto out_unlock;
2953 filter_wiphy = state.filter_wiphy;
2956 * if filtering, set cb->args[2] to +1 since 0 is the default
2957 * value needed to determine that parsing is necessary.
2959 if (filter_wiphy >= 0)
2960 cb->args[2] = filter_wiphy + 1;
2961 else
2962 cb->args[2] = -1;
2963 } else if (cb->args[2] > 0) {
2964 filter_wiphy = cb->args[2] - 1;
2967 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2968 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2969 continue;
2970 if (wp_idx < wp_start) {
2971 wp_idx++;
2972 continue;
2975 if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
2976 continue;
2978 if_idx = 0;
2980 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
2981 if (if_idx < if_start) {
2982 if_idx++;
2983 continue;
2985 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2986 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2987 rdev, wdev, false) < 0) {
2988 goto out;
2990 if_idx++;
2993 wp_idx++;
2995 out:
2996 cb->args[0] = wp_idx;
2997 cb->args[1] = if_idx;
2999 ret = skb->len;
3000 out_unlock:
3001 rtnl_unlock();
3003 return ret;
3006 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3008 struct sk_buff *msg;
3009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3010 struct wireless_dev *wdev = info->user_ptr[1];
3012 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3013 if (!msg)
3014 return -ENOMEM;
3016 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3017 rdev, wdev, false) < 0) {
3018 nlmsg_free(msg);
3019 return -ENOBUFS;
3022 return genlmsg_reply(msg, info);
3025 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3026 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3027 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3028 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3029 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3030 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3031 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3034 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3036 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3037 int flag;
3039 *mntrflags = 0;
3041 if (!nla)
3042 return -EINVAL;
3044 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, nla,
3045 mntr_flags_policy, NULL))
3046 return -EINVAL;
3048 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3049 if (flags[flag])
3050 *mntrflags |= (1<<flag);
3052 *mntrflags |= MONITOR_FLAG_CHANGED;
3054 return 0;
3057 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3058 enum nl80211_iftype type,
3059 struct genl_info *info,
3060 struct vif_params *params)
3062 bool change = false;
3063 int err;
3065 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3066 if (type != NL80211_IFTYPE_MONITOR)
3067 return -EINVAL;
3069 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3070 &params->flags);
3071 if (err)
3072 return err;
3074 change = true;
3077 if (params->flags & MONITOR_FLAG_ACTIVE &&
3078 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3079 return -EOPNOTSUPP;
3081 if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3082 const u8 *mumimo_groups;
3083 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3085 if (type != NL80211_IFTYPE_MONITOR)
3086 return -EINVAL;
3088 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3089 return -EOPNOTSUPP;
3091 mumimo_groups =
3092 nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3094 /* bits 0 and 63 are reserved and must be zero */
3095 if ((mumimo_groups[0] & BIT(0)) ||
3096 (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3097 return -EINVAL;
3099 params->vht_mumimo_groups = mumimo_groups;
3100 change = true;
3103 if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3104 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3106 if (type != NL80211_IFTYPE_MONITOR)
3107 return -EINVAL;
3109 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3110 return -EOPNOTSUPP;
3112 params->vht_mumimo_follow_addr =
3113 nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3114 change = true;
3117 return change ? 1 : 0;
3120 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3121 struct net_device *netdev, u8 use_4addr,
3122 enum nl80211_iftype iftype)
3124 if (!use_4addr) {
3125 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3126 return -EBUSY;
3127 return 0;
3130 switch (iftype) {
3131 case NL80211_IFTYPE_AP_VLAN:
3132 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3133 return 0;
3134 break;
3135 case NL80211_IFTYPE_STATION:
3136 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3137 return 0;
3138 break;
3139 default:
3140 break;
3143 return -EOPNOTSUPP;
3146 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3148 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3149 struct vif_params params;
3150 int err;
3151 enum nl80211_iftype otype, ntype;
3152 struct net_device *dev = info->user_ptr[1];
3153 bool change = false;
3155 memset(&params, 0, sizeof(params));
3157 otype = ntype = dev->ieee80211_ptr->iftype;
3159 if (info->attrs[NL80211_ATTR_IFTYPE]) {
3160 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3161 if (otype != ntype)
3162 change = true;
3163 if (ntype > NL80211_IFTYPE_MAX)
3164 return -EINVAL;
3167 if (info->attrs[NL80211_ATTR_MESH_ID]) {
3168 struct wireless_dev *wdev = dev->ieee80211_ptr;
3170 if (ntype != NL80211_IFTYPE_MESH_POINT)
3171 return -EINVAL;
3172 if (netif_running(dev))
3173 return -EBUSY;
3175 wdev_lock(wdev);
3176 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3177 IEEE80211_MAX_MESH_ID_LEN);
3178 wdev->mesh_id_up_len =
3179 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3180 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3181 wdev->mesh_id_up_len);
3182 wdev_unlock(wdev);
3185 if (info->attrs[NL80211_ATTR_4ADDR]) {
3186 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3187 change = true;
3188 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3189 if (err)
3190 return err;
3191 } else {
3192 params.use_4addr = -1;
3195 err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3196 if (err < 0)
3197 return err;
3198 if (err > 0)
3199 change = true;
3201 if (change)
3202 err = cfg80211_change_iface(rdev, dev, ntype, &params);
3203 else
3204 err = 0;
3206 if (!err && params.use_4addr != -1)
3207 dev->ieee80211_ptr->use_4addr = params.use_4addr;
3209 return err;
3212 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3215 struct vif_params params;
3216 struct wireless_dev *wdev;
3217 struct sk_buff *msg;
3218 int err;
3219 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3221 /* to avoid failing a new interface creation due to pending removal */
3222 cfg80211_destroy_ifaces(rdev);
3224 memset(&params, 0, sizeof(params));
3226 if (!info->attrs[NL80211_ATTR_IFNAME])
3227 return -EINVAL;
3229 if (info->attrs[NL80211_ATTR_IFTYPE]) {
3230 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3231 if (type > NL80211_IFTYPE_MAX)
3232 return -EINVAL;
3235 if (!rdev->ops->add_virtual_intf)
3236 return -EOPNOTSUPP;
3238 if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3239 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3240 info->attrs[NL80211_ATTR_MAC]) {
3241 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3242 ETH_ALEN);
3243 if (!is_valid_ether_addr(params.macaddr))
3244 return -EADDRNOTAVAIL;
3247 if (info->attrs[NL80211_ATTR_4ADDR]) {
3248 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3249 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3250 if (err)
3251 return err;
3254 if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3255 return -EOPNOTSUPP;
3257 err = nl80211_parse_mon_options(rdev, type, info, &params);
3258 if (err < 0)
3259 return err;
3261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3262 if (!msg)
3263 return -ENOMEM;
3265 wdev = rdev_add_virtual_intf(rdev,
3266 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3267 NET_NAME_USER, type, &params);
3268 if (WARN_ON(!wdev)) {
3269 nlmsg_free(msg);
3270 return -EPROTO;
3271 } else if (IS_ERR(wdev)) {
3272 nlmsg_free(msg);
3273 return PTR_ERR(wdev);
3276 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3277 wdev->owner_nlportid = info->snd_portid;
3279 switch (type) {
3280 case NL80211_IFTYPE_MESH_POINT:
3281 if (!info->attrs[NL80211_ATTR_MESH_ID])
3282 break;
3283 wdev_lock(wdev);
3284 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3285 IEEE80211_MAX_MESH_ID_LEN);
3286 wdev->mesh_id_up_len =
3287 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3288 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3289 wdev->mesh_id_up_len);
3290 wdev_unlock(wdev);
3291 break;
3292 case NL80211_IFTYPE_NAN:
3293 case NL80211_IFTYPE_P2P_DEVICE:
3295 * P2P Device and NAN do not have a netdev, so don't go
3296 * through the netdev notifier and must be added here
3298 mutex_init(&wdev->mtx);
3299 INIT_LIST_HEAD(&wdev->event_list);
3300 spin_lock_init(&wdev->event_lock);
3301 INIT_LIST_HEAD(&wdev->mgmt_registrations);
3302 spin_lock_init(&wdev->mgmt_registrations_lock);
3304 wdev->identifier = ++rdev->wdev_id;
3305 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
3306 rdev->devlist_generation++;
3307 break;
3308 default:
3309 break;
3312 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3313 rdev, wdev, false) < 0) {
3314 nlmsg_free(msg);
3315 return -ENOBUFS;
3319 * For wdevs which have no associated netdev object (e.g. of type
3320 * NL80211_IFTYPE_P2P_DEVICE), emit the NEW_INTERFACE event here.
3321 * For all other types, the event will be generated from the
3322 * netdev notifier
3324 if (!wdev->netdev)
3325 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
3327 return genlmsg_reply(msg, info);
3330 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3333 struct wireless_dev *wdev = info->user_ptr[1];
3335 if (!rdev->ops->del_virtual_intf)
3336 return -EOPNOTSUPP;
3339 * If we remove a wireless device without a netdev then clear
3340 * user_ptr[1] so that nl80211_post_doit won't dereference it
3341 * to check if it needs to do dev_put(). Otherwise it crashes
3342 * since the wdev has been freed, unlike with a netdev where
3343 * we need the dev_put() for the netdev to really be freed.
3345 if (!wdev->netdev)
3346 info->user_ptr[1] = NULL;
3348 return rdev_del_virtual_intf(rdev, wdev);
3351 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3353 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3354 struct net_device *dev = info->user_ptr[1];
3355 u16 noack_map;
3357 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3358 return -EINVAL;
3360 if (!rdev->ops->set_noack_map)
3361 return -EOPNOTSUPP;
3363 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3365 return rdev_set_noack_map(rdev, dev, noack_map);
3368 struct get_key_cookie {
3369 struct sk_buff *msg;
3370 int error;
3371 int idx;
3374 static void get_key_callback(void *c, struct key_params *params)
3376 struct nlattr *key;
3377 struct get_key_cookie *cookie = c;
3379 if ((params->key &&
3380 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3381 params->key_len, params->key)) ||
3382 (params->seq &&
3383 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3384 params->seq_len, params->seq)) ||
3385 (params->cipher &&
3386 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3387 params->cipher)))
3388 goto nla_put_failure;
3390 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
3391 if (!key)
3392 goto nla_put_failure;
3394 if ((params->key &&
3395 nla_put(cookie->msg, NL80211_KEY_DATA,
3396 params->key_len, params->key)) ||
3397 (params->seq &&
3398 nla_put(cookie->msg, NL80211_KEY_SEQ,
3399 params->seq_len, params->seq)) ||
3400 (params->cipher &&
3401 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3402 params->cipher)))
3403 goto nla_put_failure;
3405 if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3406 goto nla_put_failure;
3408 nla_nest_end(cookie->msg, key);
3410 return;
3411 nla_put_failure:
3412 cookie->error = 1;
3415 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3417 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3418 int err;
3419 struct net_device *dev = info->user_ptr[1];
3420 u8 key_idx = 0;
3421 const u8 *mac_addr = NULL;
3422 bool pairwise;
3423 struct get_key_cookie cookie = {
3424 .error = 0,
3426 void *hdr;
3427 struct sk_buff *msg;
3429 if (info->attrs[NL80211_ATTR_KEY_IDX])
3430 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3432 if (key_idx > 5)
3433 return -EINVAL;
3435 if (info->attrs[NL80211_ATTR_MAC])
3436 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3438 pairwise = !!mac_addr;
3439 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3440 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3442 if (kt >= NUM_NL80211_KEYTYPES)
3443 return -EINVAL;
3444 if (kt != NL80211_KEYTYPE_GROUP &&
3445 kt != NL80211_KEYTYPE_PAIRWISE)
3446 return -EINVAL;
3447 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3450 if (!rdev->ops->get_key)
3451 return -EOPNOTSUPP;
3453 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3454 return -ENOENT;
3456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3457 if (!msg)
3458 return -ENOMEM;
3460 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3461 NL80211_CMD_NEW_KEY);
3462 if (!hdr)
3463 goto nla_put_failure;
3465 cookie.msg = msg;
3466 cookie.idx = key_idx;
3468 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3469 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3470 goto nla_put_failure;
3471 if (mac_addr &&
3472 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3473 goto nla_put_failure;
3475 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3476 get_key_callback);
3478 if (err)
3479 goto free_msg;
3481 if (cookie.error)
3482 goto nla_put_failure;
3484 genlmsg_end(msg, hdr);
3485 return genlmsg_reply(msg, info);
3487 nla_put_failure:
3488 err = -ENOBUFS;
3489 free_msg:
3490 nlmsg_free(msg);
3491 return err;
3494 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3497 struct key_parse key;
3498 int err;
3499 struct net_device *dev = info->user_ptr[1];
3501 err = nl80211_parse_key(info, &key);
3502 if (err)
3503 return err;
3505 if (key.idx < 0)
3506 return -EINVAL;
3508 /* only support setting default key */
3509 if (!key.def && !key.defmgmt)
3510 return -EINVAL;
3512 wdev_lock(dev->ieee80211_ptr);
3514 if (key.def) {
3515 if (!rdev->ops->set_default_key) {
3516 err = -EOPNOTSUPP;
3517 goto out;
3520 err = nl80211_key_allowed(dev->ieee80211_ptr);
3521 if (err)
3522 goto out;
3524 err = rdev_set_default_key(rdev, dev, key.idx,
3525 key.def_uni, key.def_multi);
3527 if (err)
3528 goto out;
3530 #ifdef CONFIG_CFG80211_WEXT
3531 dev->ieee80211_ptr->wext.default_key = key.idx;
3532 #endif
3533 } else {
3534 if (key.def_uni || !key.def_multi) {
3535 err = -EINVAL;
3536 goto out;
3539 if (!rdev->ops->set_default_mgmt_key) {
3540 err = -EOPNOTSUPP;
3541 goto out;
3544 err = nl80211_key_allowed(dev->ieee80211_ptr);
3545 if (err)
3546 goto out;
3548 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3549 if (err)
3550 goto out;
3552 #ifdef CONFIG_CFG80211_WEXT
3553 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3554 #endif
3557 out:
3558 wdev_unlock(dev->ieee80211_ptr);
3560 return err;
3563 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3565 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3566 int err;
3567 struct net_device *dev = info->user_ptr[1];
3568 struct key_parse key;
3569 const u8 *mac_addr = NULL;
3571 err = nl80211_parse_key(info, &key);
3572 if (err)
3573 return err;
3575 if (!key.p.key)
3576 return -EINVAL;
3578 if (info->attrs[NL80211_ATTR_MAC])
3579 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3581 if (key.type == -1) {
3582 if (mac_addr)
3583 key.type = NL80211_KEYTYPE_PAIRWISE;
3584 else
3585 key.type = NL80211_KEYTYPE_GROUP;
3588 /* for now */
3589 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3590 key.type != NL80211_KEYTYPE_GROUP)
3591 return -EINVAL;
3593 if (!rdev->ops->add_key)
3594 return -EOPNOTSUPP;
3596 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3597 key.type == NL80211_KEYTYPE_PAIRWISE,
3598 mac_addr))
3599 return -EINVAL;
3601 wdev_lock(dev->ieee80211_ptr);
3602 err = nl80211_key_allowed(dev->ieee80211_ptr);
3603 if (!err)
3604 err = rdev_add_key(rdev, dev, key.idx,
3605 key.type == NL80211_KEYTYPE_PAIRWISE,
3606 mac_addr, &key.p);
3607 wdev_unlock(dev->ieee80211_ptr);
3609 return err;
3612 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3615 int err;
3616 struct net_device *dev = info->user_ptr[1];
3617 u8 *mac_addr = NULL;
3618 struct key_parse key;
3620 err = nl80211_parse_key(info, &key);
3621 if (err)
3622 return err;
3624 if (info->attrs[NL80211_ATTR_MAC])
3625 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3627 if (key.type == -1) {
3628 if (mac_addr)
3629 key.type = NL80211_KEYTYPE_PAIRWISE;
3630 else
3631 key.type = NL80211_KEYTYPE_GROUP;
3634 /* for now */
3635 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3636 key.type != NL80211_KEYTYPE_GROUP)
3637 return -EINVAL;
3639 if (!rdev->ops->del_key)
3640 return -EOPNOTSUPP;
3642 wdev_lock(dev->ieee80211_ptr);
3643 err = nl80211_key_allowed(dev->ieee80211_ptr);
3645 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3646 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3647 err = -ENOENT;
3649 if (!err)
3650 err = rdev_del_key(rdev, dev, key.idx,
3651 key.type == NL80211_KEYTYPE_PAIRWISE,
3652 mac_addr);
3654 #ifdef CONFIG_CFG80211_WEXT
3655 if (!err) {
3656 if (key.idx == dev->ieee80211_ptr->wext.default_key)
3657 dev->ieee80211_ptr->wext.default_key = -1;
3658 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3659 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3661 #endif
3662 wdev_unlock(dev->ieee80211_ptr);
3664 return err;
3667 /* This function returns an error or the number of nested attributes */
3668 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3670 struct nlattr *attr;
3671 int n_entries = 0, tmp;
3673 nla_for_each_nested(attr, nl_attr, tmp) {
3674 if (nla_len(attr) != ETH_ALEN)
3675 return -EINVAL;
3677 n_entries++;
3680 return n_entries;
3684 * This function parses ACL information and allocates memory for ACL data.
3685 * On successful return, the calling function is responsible to free the
3686 * ACL buffer returned by this function.
3688 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3689 struct genl_info *info)
3691 enum nl80211_acl_policy acl_policy;
3692 struct nlattr *attr;
3693 struct cfg80211_acl_data *acl;
3694 int i = 0, n_entries, tmp;
3696 if (!wiphy->max_acl_mac_addrs)
3697 return ERR_PTR(-EOPNOTSUPP);
3699 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3700 return ERR_PTR(-EINVAL);
3702 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3703 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3704 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3705 return ERR_PTR(-EINVAL);
3707 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3708 return ERR_PTR(-EINVAL);
3710 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3711 if (n_entries < 0)
3712 return ERR_PTR(n_entries);
3714 if (n_entries > wiphy->max_acl_mac_addrs)
3715 return ERR_PTR(-ENOTSUPP);
3717 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3718 GFP_KERNEL);
3719 if (!acl)
3720 return ERR_PTR(-ENOMEM);
3722 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3723 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3724 i++;
3727 acl->n_acl_entries = n_entries;
3728 acl->acl_policy = acl_policy;
3730 return acl;
3733 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3736 struct net_device *dev = info->user_ptr[1];
3737 struct cfg80211_acl_data *acl;
3738 int err;
3740 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3741 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3742 return -EOPNOTSUPP;
3744 if (!dev->ieee80211_ptr->beacon_interval)
3745 return -EINVAL;
3747 acl = parse_acl_data(&rdev->wiphy, info);
3748 if (IS_ERR(acl))
3749 return PTR_ERR(acl);
3751 err = rdev_set_mac_acl(rdev, dev, acl);
3753 kfree(acl);
3755 return err;
3758 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3759 u8 *rates, u8 rates_len)
3761 u8 i;
3762 u32 mask = 0;
3764 for (i = 0; i < rates_len; i++) {
3765 int rate = (rates[i] & 0x7f) * 5;
3766 int ridx;
3768 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
3769 struct ieee80211_rate *srate =
3770 &sband->bitrates[ridx];
3771 if (rate == srate->bitrate) {
3772 mask |= 1 << ridx;
3773 break;
3776 if (ridx == sband->n_bitrates)
3777 return 0; /* rate not found */
3780 return mask;
3783 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
3784 u8 *rates, u8 rates_len,
3785 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
3787 u8 i;
3789 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
3791 for (i = 0; i < rates_len; i++) {
3792 int ridx, rbit;
3794 ridx = rates[i] / 8;
3795 rbit = BIT(rates[i] % 8);
3797 /* check validity */
3798 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
3799 return false;
3801 /* check availability */
3802 ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
3803 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
3804 mcs[ridx] |= rbit;
3805 else
3806 return false;
3809 return true;
3812 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
3814 u16 mcs_mask = 0;
3816 switch (vht_mcs_map) {
3817 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
3818 break;
3819 case IEEE80211_VHT_MCS_SUPPORT_0_7:
3820 mcs_mask = 0x00FF;
3821 break;
3822 case IEEE80211_VHT_MCS_SUPPORT_0_8:
3823 mcs_mask = 0x01FF;
3824 break;
3825 case IEEE80211_VHT_MCS_SUPPORT_0_9:
3826 mcs_mask = 0x03FF;
3827 break;
3828 default:
3829 break;
3832 return mcs_mask;
3835 static void vht_build_mcs_mask(u16 vht_mcs_map,
3836 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
3838 u8 nss;
3840 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
3841 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
3842 vht_mcs_map >>= 2;
3846 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
3847 struct nl80211_txrate_vht *txrate,
3848 u16 mcs[NL80211_VHT_NSS_MAX])
3850 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3851 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
3852 u8 i;
3854 if (!sband->vht_cap.vht_supported)
3855 return false;
3857 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
3859 /* Build vht_mcs_mask from VHT capabilities */
3860 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
3862 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3863 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
3864 mcs[i] = txrate->mcs[i];
3865 else
3866 return false;
3869 return true;
3872 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
3873 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
3874 .len = NL80211_MAX_SUPP_RATES },
3875 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
3876 .len = NL80211_MAX_SUPP_HT_RATES },
3877 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
3878 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
3881 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
3882 struct cfg80211_bitrate_mask *mask)
3884 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
3885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3886 int rem, i;
3887 struct nlattr *tx_rates;
3888 struct ieee80211_supported_band *sband;
3889 u16 vht_tx_mcs_map;
3891 memset(mask, 0, sizeof(*mask));
3892 /* Default to all rates enabled */
3893 for (i = 0; i < NUM_NL80211_BANDS; i++) {
3894 sband = rdev->wiphy.bands[i];
3896 if (!sband)
3897 continue;
3899 mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
3900 memcpy(mask->control[i].ht_mcs,
3901 sband->ht_cap.mcs.rx_mask,
3902 sizeof(mask->control[i].ht_mcs));
3904 if (!sband->vht_cap.vht_supported)
3905 continue;
3907 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3908 vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
3911 /* if no rates are given set it back to the defaults */
3912 if (!info->attrs[NL80211_ATTR_TX_RATES])
3913 goto out;
3915 /* The nested attribute uses enum nl80211_band as the index. This maps
3916 * directly to the enum nl80211_band values used in cfg80211.
3918 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
3919 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
3920 enum nl80211_band band = nla_type(tx_rates);
3921 int err;
3923 if (band < 0 || band >= NUM_NL80211_BANDS)
3924 return -EINVAL;
3925 sband = rdev->wiphy.bands[band];
3926 if (sband == NULL)
3927 return -EINVAL;
3928 err = nla_parse_nested(tb, NL80211_TXRATE_MAX, tx_rates,
3929 nl80211_txattr_policy, info->extack);
3930 if (err)
3931 return err;
3932 if (tb[NL80211_TXRATE_LEGACY]) {
3933 mask->control[band].legacy = rateset_to_mask(
3934 sband,
3935 nla_data(tb[NL80211_TXRATE_LEGACY]),
3936 nla_len(tb[NL80211_TXRATE_LEGACY]));
3937 if ((mask->control[band].legacy == 0) &&
3938 nla_len(tb[NL80211_TXRATE_LEGACY]))
3939 return -EINVAL;
3941 if (tb[NL80211_TXRATE_HT]) {
3942 if (!ht_rateset_to_mask(
3943 sband,
3944 nla_data(tb[NL80211_TXRATE_HT]),
3945 nla_len(tb[NL80211_TXRATE_HT]),
3946 mask->control[band].ht_mcs))
3947 return -EINVAL;
3949 if (tb[NL80211_TXRATE_VHT]) {
3950 if (!vht_set_mcs_mask(
3951 sband,
3952 nla_data(tb[NL80211_TXRATE_VHT]),
3953 mask->control[band].vht_mcs))
3954 return -EINVAL;
3956 if (tb[NL80211_TXRATE_GI]) {
3957 mask->control[band].gi =
3958 nla_get_u8(tb[NL80211_TXRATE_GI]);
3959 if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
3960 return -EINVAL;
3963 if (mask->control[band].legacy == 0) {
3964 /* don't allow empty legacy rates if HT or VHT
3965 * are not even supported.
3967 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
3968 rdev->wiphy.bands[band]->vht_cap.vht_supported))
3969 return -EINVAL;
3971 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3972 if (mask->control[band].ht_mcs[i])
3973 goto out;
3975 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3976 if (mask->control[band].vht_mcs[i])
3977 goto out;
3979 /* legacy and mcs rates may not be both empty */
3980 return -EINVAL;
3984 out:
3985 return 0;
3988 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
3989 enum nl80211_band band,
3990 struct cfg80211_bitrate_mask *beacon_rate)
3992 u32 count_ht, count_vht, i;
3993 u32 rate = beacon_rate->control[band].legacy;
3995 /* Allow only one rate */
3996 if (hweight32(rate) > 1)
3997 return -EINVAL;
3999 count_ht = 0;
4000 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4001 if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4002 return -EINVAL;
4003 } else if (beacon_rate->control[band].ht_mcs[i]) {
4004 count_ht++;
4005 if (count_ht > 1)
4006 return -EINVAL;
4008 if (count_ht && rate)
4009 return -EINVAL;
4012 count_vht = 0;
4013 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4014 if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4015 return -EINVAL;
4016 } else if (beacon_rate->control[band].vht_mcs[i]) {
4017 count_vht++;
4018 if (count_vht > 1)
4019 return -EINVAL;
4021 if (count_vht && rate)
4022 return -EINVAL;
4025 if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4026 return -EINVAL;
4028 if (rate &&
4029 !wiphy_ext_feature_isset(&rdev->wiphy,
4030 NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4031 return -EINVAL;
4032 if (count_ht &&
4033 !wiphy_ext_feature_isset(&rdev->wiphy,
4034 NL80211_EXT_FEATURE_BEACON_RATE_HT))
4035 return -EINVAL;
4036 if (count_vht &&
4037 !wiphy_ext_feature_isset(&rdev->wiphy,
4038 NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4039 return -EINVAL;
4041 return 0;
4044 static int nl80211_parse_beacon(struct nlattr *attrs[],
4045 struct cfg80211_beacon_data *bcn)
4047 bool haveinfo = false;
4049 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
4050 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
4051 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
4052 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
4053 return -EINVAL;
4055 memset(bcn, 0, sizeof(*bcn));
4057 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4058 int ret = validate_beacon_head(attrs[NL80211_ATTR_BEACON_HEAD],
4059 NULL);
4061 if (ret)
4062 return ret;
4064 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4065 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4066 if (!bcn->head_len)
4067 return -EINVAL;
4068 haveinfo = true;
4071 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4072 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4073 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4074 haveinfo = true;
4077 if (!haveinfo)
4078 return -EINVAL;
4080 if (attrs[NL80211_ATTR_IE]) {
4081 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4082 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4085 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4086 bcn->proberesp_ies =
4087 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4088 bcn->proberesp_ies_len =
4089 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4092 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4093 bcn->assocresp_ies =
4094 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4095 bcn->assocresp_ies_len =
4096 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4099 if (attrs[NL80211_ATTR_PROBE_RESP]) {
4100 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4101 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4104 return 0;
4107 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4108 const u8 *rates)
4110 int i;
4112 if (!rates)
4113 return;
4115 for (i = 0; i < rates[1]; i++) {
4116 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4117 params->ht_required = true;
4118 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4119 params->vht_required = true;
4124 * Since the nl80211 API didn't include, from the beginning, attributes about
4125 * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4126 * benefit of drivers that rebuild IEs in the firmware.
4128 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4130 const struct cfg80211_beacon_data *bcn = &params->beacon;
4131 size_t ies_len = bcn->tail_len;
4132 const u8 *ies = bcn->tail;
4133 const u8 *rates;
4134 const u8 *cap;
4136 rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4137 nl80211_check_ap_rate_selectors(params, rates);
4139 rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4140 nl80211_check_ap_rate_selectors(params, rates);
4142 cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4143 if (cap && cap[1] >= sizeof(*params->ht_cap))
4144 params->ht_cap = (void *)(cap + 2);
4145 cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4146 if (cap && cap[1] >= sizeof(*params->vht_cap))
4147 params->vht_cap = (void *)(cap + 2);
4150 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4151 struct cfg80211_ap_settings *params)
4153 struct wireless_dev *wdev;
4154 bool ret = false;
4156 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4157 if (wdev->iftype != NL80211_IFTYPE_AP &&
4158 wdev->iftype != NL80211_IFTYPE_P2P_GO)
4159 continue;
4161 if (!wdev->preset_chandef.chan)
4162 continue;
4164 params->chandef = wdev->preset_chandef;
4165 ret = true;
4166 break;
4169 return ret;
4172 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4173 enum nl80211_auth_type auth_type,
4174 enum nl80211_commands cmd)
4176 if (auth_type > NL80211_AUTHTYPE_MAX)
4177 return false;
4179 switch (cmd) {
4180 case NL80211_CMD_AUTHENTICATE:
4181 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4182 auth_type == NL80211_AUTHTYPE_SAE)
4183 return false;
4184 if (!wiphy_ext_feature_isset(&rdev->wiphy,
4185 NL80211_EXT_FEATURE_FILS_STA) &&
4186 (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4187 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4188 auth_type == NL80211_AUTHTYPE_FILS_PK))
4189 return false;
4190 return true;
4191 case NL80211_CMD_CONNECT:
4192 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4193 auth_type == NL80211_AUTHTYPE_SAE)
4194 return false;
4196 /* FILS with SK PFS or PK not supported yet */
4197 if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4198 auth_type == NL80211_AUTHTYPE_FILS_PK)
4199 return false;
4200 if (!wiphy_ext_feature_isset(
4201 &rdev->wiphy,
4202 NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4203 auth_type == NL80211_AUTHTYPE_FILS_SK)
4204 return false;
4205 return true;
4206 case NL80211_CMD_START_AP:
4207 /* SAE not supported yet */
4208 if (auth_type == NL80211_AUTHTYPE_SAE)
4209 return false;
4210 /* FILS not supported yet */
4211 if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4212 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4213 auth_type == NL80211_AUTHTYPE_FILS_PK)
4214 return false;
4215 return true;
4216 default:
4217 return false;
4221 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4224 struct net_device *dev = info->user_ptr[1];
4225 struct wireless_dev *wdev = dev->ieee80211_ptr;
4226 struct cfg80211_ap_settings params;
4227 int err;
4229 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4230 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4231 return -EOPNOTSUPP;
4233 if (!rdev->ops->start_ap)
4234 return -EOPNOTSUPP;
4236 if (wdev->beacon_interval)
4237 return -EALREADY;
4239 memset(&params, 0, sizeof(params));
4241 /* these are required for START_AP */
4242 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4243 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4244 !info->attrs[NL80211_ATTR_BEACON_HEAD])
4245 return -EINVAL;
4247 err = nl80211_parse_beacon(info->attrs, &params.beacon);
4248 if (err)
4249 return err;
4251 params.beacon_interval =
4252 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4253 params.dtim_period =
4254 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4256 err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4257 params.beacon_interval);
4258 if (err)
4259 return err;
4262 * In theory, some of these attributes should be required here
4263 * but since they were not used when the command was originally
4264 * added, keep them optional for old user space programs to let
4265 * them continue to work with drivers that do not need the
4266 * additional information -- drivers must check!
4268 if (info->attrs[NL80211_ATTR_SSID]) {
4269 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4270 params.ssid_len =
4271 nla_len(info->attrs[NL80211_ATTR_SSID]);
4272 if (params.ssid_len == 0 ||
4273 params.ssid_len > IEEE80211_MAX_SSID_LEN)
4274 return -EINVAL;
4277 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
4278 params.hidden_ssid = nla_get_u32(
4279 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4280 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
4281 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
4282 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
4283 return -EINVAL;
4286 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4288 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4289 params.auth_type = nla_get_u32(
4290 info->attrs[NL80211_ATTR_AUTH_TYPE]);
4291 if (!nl80211_valid_auth_type(rdev, params.auth_type,
4292 NL80211_CMD_START_AP))
4293 return -EINVAL;
4294 } else
4295 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4297 err = nl80211_crypto_settings(rdev, info, &params.crypto,
4298 NL80211_MAX_NR_CIPHER_SUITES);
4299 if (err)
4300 return err;
4302 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4303 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4304 return -EOPNOTSUPP;
4305 params.inactivity_timeout = nla_get_u16(
4306 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4309 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4310 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4311 return -EINVAL;
4312 params.p2p_ctwindow =
4313 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4314 if (params.p2p_ctwindow > 127)
4315 return -EINVAL;
4316 if (params.p2p_ctwindow != 0 &&
4317 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4318 return -EINVAL;
4321 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4322 u8 tmp;
4324 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4325 return -EINVAL;
4326 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4327 if (tmp > 1)
4328 return -EINVAL;
4329 params.p2p_opp_ps = tmp;
4330 if (params.p2p_opp_ps != 0 &&
4331 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4332 return -EINVAL;
4335 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4336 err = nl80211_parse_chandef(rdev, info, &params.chandef);
4337 if (err)
4338 return err;
4339 } else if (wdev->preset_chandef.chan) {
4340 params.chandef = wdev->preset_chandef;
4341 } else if (!nl80211_get_ap_channel(rdev, &params))
4342 return -EINVAL;
4344 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4345 wdev->iftype))
4346 return -EINVAL;
4348 if (info->attrs[NL80211_ATTR_TX_RATES]) {
4349 err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4350 if (err)
4351 return err;
4353 err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4354 &params.beacon_rate);
4355 if (err)
4356 return err;
4359 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4360 params.smps_mode =
4361 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4362 switch (params.smps_mode) {
4363 case NL80211_SMPS_OFF:
4364 break;
4365 case NL80211_SMPS_STATIC:
4366 if (!(rdev->wiphy.features &
4367 NL80211_FEATURE_STATIC_SMPS))
4368 return -EINVAL;
4369 break;
4370 case NL80211_SMPS_DYNAMIC:
4371 if (!(rdev->wiphy.features &
4372 NL80211_FEATURE_DYNAMIC_SMPS))
4373 return -EINVAL;
4374 break;
4375 default:
4376 return -EINVAL;
4378 } else {
4379 params.smps_mode = NL80211_SMPS_OFF;
4382 params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4383 if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4384 return -EOPNOTSUPP;
4386 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4387 params.acl = parse_acl_data(&rdev->wiphy, info);
4388 if (IS_ERR(params.acl))
4389 return PTR_ERR(params.acl);
4392 nl80211_calculate_ap_params(&params);
4394 wdev_lock(wdev);
4395 err = rdev_start_ap(rdev, dev, &params);
4396 if (!err) {
4397 wdev->preset_chandef = params.chandef;
4398 wdev->beacon_interval = params.beacon_interval;
4399 wdev->chandef = params.chandef;
4400 wdev->ssid_len = params.ssid_len;
4401 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4403 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4404 wdev->conn_owner_nlportid = info->snd_portid;
4406 wdev_unlock(wdev);
4408 kfree(params.acl);
4410 return err;
4413 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4415 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4416 struct net_device *dev = info->user_ptr[1];
4417 struct wireless_dev *wdev = dev->ieee80211_ptr;
4418 struct cfg80211_beacon_data params;
4419 int err;
4421 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4422 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4423 return -EOPNOTSUPP;
4425 if (!rdev->ops->change_beacon)
4426 return -EOPNOTSUPP;
4428 if (!wdev->beacon_interval)
4429 return -EINVAL;
4431 err = nl80211_parse_beacon(info->attrs, &params);
4432 if (err)
4433 return err;
4435 wdev_lock(wdev);
4436 err = rdev_change_beacon(rdev, dev, &params);
4437 wdev_unlock(wdev);
4439 return err;
4442 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4445 struct net_device *dev = info->user_ptr[1];
4447 return cfg80211_stop_ap(rdev, dev, false);
4450 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4451 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4452 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4453 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4454 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4455 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4456 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4459 static int parse_station_flags(struct genl_info *info,
4460 enum nl80211_iftype iftype,
4461 struct station_parameters *params)
4463 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4464 struct nlattr *nla;
4465 int flag;
4468 * Try parsing the new attribute first so userspace
4469 * can specify both for older kernels.
4471 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4472 if (nla) {
4473 struct nl80211_sta_flag_update *sta_flags;
4475 sta_flags = nla_data(nla);
4476 params->sta_flags_mask = sta_flags->mask;
4477 params->sta_flags_set = sta_flags->set;
4478 params->sta_flags_set &= params->sta_flags_mask;
4479 if ((params->sta_flags_mask |
4480 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4481 return -EINVAL;
4482 return 0;
4485 /* if present, parse the old attribute */
4487 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4488 if (!nla)
4489 return 0;
4491 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, nla,
4492 sta_flags_policy, info->extack))
4493 return -EINVAL;
4496 * Only allow certain flags for interface types so that
4497 * other attributes are silently ignored. Remember that
4498 * this is backward compatibility code with old userspace
4499 * and shouldn't be hit in other cases anyway.
4501 switch (iftype) {
4502 case NL80211_IFTYPE_AP:
4503 case NL80211_IFTYPE_AP_VLAN:
4504 case NL80211_IFTYPE_P2P_GO:
4505 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4506 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4507 BIT(NL80211_STA_FLAG_WME) |
4508 BIT(NL80211_STA_FLAG_MFP);
4509 break;
4510 case NL80211_IFTYPE_P2P_CLIENT:
4511 case NL80211_IFTYPE_STATION:
4512 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4513 BIT(NL80211_STA_FLAG_TDLS_PEER);
4514 break;
4515 case NL80211_IFTYPE_MESH_POINT:
4516 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4517 BIT(NL80211_STA_FLAG_MFP) |
4518 BIT(NL80211_STA_FLAG_AUTHORIZED);
4519 break;
4520 default:
4521 return -EINVAL;
4524 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4525 if (flags[flag]) {
4526 params->sta_flags_set |= (1<<flag);
4528 /* no longer support new API additions in old API */
4529 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4530 return -EINVAL;
4534 return 0;
4537 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
4538 int attr)
4540 struct nlattr *rate;
4541 u32 bitrate;
4542 u16 bitrate_compat;
4543 enum nl80211_rate_info rate_flg;
4545 rate = nla_nest_start(msg, attr);
4546 if (!rate)
4547 return false;
4549 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4550 bitrate = cfg80211_calculate_bitrate(info);
4551 /* report 16-bit bitrate only if we can */
4552 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4553 if (bitrate > 0 &&
4554 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4555 return false;
4556 if (bitrate_compat > 0 &&
4557 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4558 return false;
4560 switch (info->bw) {
4561 case RATE_INFO_BW_5:
4562 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4563 break;
4564 case RATE_INFO_BW_10:
4565 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4566 break;
4567 default:
4568 WARN_ON(1);
4569 /* fall through */
4570 case RATE_INFO_BW_20:
4571 rate_flg = 0;
4572 break;
4573 case RATE_INFO_BW_40:
4574 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4575 break;
4576 case RATE_INFO_BW_80:
4577 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4578 break;
4579 case RATE_INFO_BW_160:
4580 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4581 break;
4582 case RATE_INFO_BW_HE_RU:
4583 rate_flg = 0;
4584 WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
4587 if (rate_flg && nla_put_flag(msg, rate_flg))
4588 return false;
4590 if (info->flags & RATE_INFO_FLAGS_MCS) {
4591 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4592 return false;
4593 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4594 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4595 return false;
4596 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4597 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4598 return false;
4599 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4600 return false;
4601 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4602 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4603 return false;
4604 } else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
4605 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
4606 return false;
4607 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
4608 return false;
4609 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
4610 return false;
4611 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
4612 return false;
4613 if (info->bw == RATE_INFO_BW_HE_RU &&
4614 nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
4615 info->he_ru_alloc))
4616 return false;
4619 nla_nest_end(msg, rate);
4620 return true;
4623 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4624 int id)
4626 void *attr;
4627 int i = 0;
4629 if (!mask)
4630 return true;
4632 attr = nla_nest_start(msg, id);
4633 if (!attr)
4634 return false;
4636 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4637 if (!(mask & BIT(i)))
4638 continue;
4640 if (nla_put_u8(msg, i, signal[i]))
4641 return false;
4644 nla_nest_end(msg, attr);
4646 return true;
4649 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4650 u32 seq, int flags,
4651 struct cfg80211_registered_device *rdev,
4652 struct net_device *dev,
4653 const u8 *mac_addr, struct station_info *sinfo)
4655 void *hdr;
4656 struct nlattr *sinfoattr, *bss_param;
4658 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
4659 if (!hdr) {
4660 cfg80211_sinfo_release_content(sinfo);
4661 return -1;
4664 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4665 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
4666 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
4667 goto nla_put_failure;
4669 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
4670 if (!sinfoattr)
4671 goto nla_put_failure;
4673 #define PUT_SINFO(attr, memb, type) do { \
4674 BUILD_BUG_ON(sizeof(type) == sizeof(u64)); \
4675 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) && \
4676 nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr, \
4677 sinfo->memb)) \
4678 goto nla_put_failure; \
4679 } while (0)
4680 #define PUT_SINFO_U64(attr, memb) do { \
4681 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) && \
4682 nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr, \
4683 sinfo->memb, NL80211_STA_INFO_PAD)) \
4684 goto nla_put_failure; \
4685 } while (0)
4687 PUT_SINFO(CONNECTED_TIME, connected_time, u32);
4688 PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
4690 if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
4691 BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
4692 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
4693 (u32)sinfo->rx_bytes))
4694 goto nla_put_failure;
4696 if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
4697 BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
4698 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
4699 (u32)sinfo->tx_bytes))
4700 goto nla_put_failure;
4702 PUT_SINFO_U64(RX_BYTES64, rx_bytes);
4703 PUT_SINFO_U64(TX_BYTES64, tx_bytes);
4704 PUT_SINFO(LLID, llid, u16);
4705 PUT_SINFO(PLID, plid, u16);
4706 PUT_SINFO(PLINK_STATE, plink_state, u8);
4707 PUT_SINFO_U64(RX_DURATION, rx_duration);
4709 switch (rdev->wiphy.signal_type) {
4710 case CFG80211_SIGNAL_TYPE_MBM:
4711 PUT_SINFO(SIGNAL, signal, u8);
4712 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
4713 break;
4714 default:
4715 break;
4717 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
4718 if (!nl80211_put_signal(msg, sinfo->chains,
4719 sinfo->chain_signal,
4720 NL80211_STA_INFO_CHAIN_SIGNAL))
4721 goto nla_put_failure;
4723 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
4724 if (!nl80211_put_signal(msg, sinfo->chains,
4725 sinfo->chain_signal_avg,
4726 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
4727 goto nla_put_failure;
4729 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
4730 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
4731 NL80211_STA_INFO_TX_BITRATE))
4732 goto nla_put_failure;
4734 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
4735 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
4736 NL80211_STA_INFO_RX_BITRATE))
4737 goto nla_put_failure;
4740 PUT_SINFO(RX_PACKETS, rx_packets, u32);
4741 PUT_SINFO(TX_PACKETS, tx_packets, u32);
4742 PUT_SINFO(TX_RETRIES, tx_retries, u32);
4743 PUT_SINFO(TX_FAILED, tx_failed, u32);
4744 PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
4745 PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
4746 PUT_SINFO(LOCAL_PM, local_pm, u32);
4747 PUT_SINFO(PEER_PM, peer_pm, u32);
4748 PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
4750 if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
4751 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
4752 if (!bss_param)
4753 goto nla_put_failure;
4755 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
4756 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
4757 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
4758 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
4759 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
4760 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
4761 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
4762 sinfo->bss_param.dtim_period) ||
4763 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
4764 sinfo->bss_param.beacon_interval))
4765 goto nla_put_failure;
4767 nla_nest_end(msg, bss_param);
4769 if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
4770 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
4771 sizeof(struct nl80211_sta_flag_update),
4772 &sinfo->sta_flags))
4773 goto nla_put_failure;
4775 PUT_SINFO_U64(T_OFFSET, t_offset);
4776 PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
4777 PUT_SINFO_U64(BEACON_RX, rx_beacon);
4778 PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
4779 PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
4780 if (wiphy_ext_feature_isset(&rdev->wiphy,
4781 NL80211_EXT_FEATURE_DATA_ACK_SIGNAL_SUPPORT))
4782 PUT_SINFO(DATA_ACK_SIGNAL_AVG, avg_ack_signal, s8);
4784 #undef PUT_SINFO
4785 #undef PUT_SINFO_U64
4787 if (sinfo->pertid) {
4788 struct nlattr *tidsattr;
4789 int tid;
4791 tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
4792 if (!tidsattr)
4793 goto nla_put_failure;
4795 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
4796 struct cfg80211_tid_stats *tidstats;
4797 struct nlattr *tidattr;
4799 tidstats = &sinfo->pertid[tid];
4801 if (!tidstats->filled)
4802 continue;
4804 tidattr = nla_nest_start(msg, tid + 1);
4805 if (!tidattr)
4806 goto nla_put_failure;
4808 #define PUT_TIDVAL_U64(attr, memb) do { \
4809 if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) && \
4810 nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr, \
4811 tidstats->memb, NL80211_TID_STATS_PAD)) \
4812 goto nla_put_failure; \
4813 } while (0)
4815 PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
4816 PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
4817 PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
4818 PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
4820 #undef PUT_TIDVAL_U64
4821 if ((tidstats->filled &
4822 BIT(NL80211_TID_STATS_TXQ_STATS)) &&
4823 !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
4824 NL80211_TID_STATS_TXQ_STATS))
4825 goto nla_put_failure;
4827 nla_nest_end(msg, tidattr);
4830 nla_nest_end(msg, tidsattr);
4833 nla_nest_end(msg, sinfoattr);
4835 if (sinfo->assoc_req_ies_len &&
4836 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
4837 sinfo->assoc_req_ies))
4838 goto nla_put_failure;
4840 cfg80211_sinfo_release_content(sinfo);
4841 genlmsg_end(msg, hdr);
4842 return 0;
4844 nla_put_failure:
4845 cfg80211_sinfo_release_content(sinfo);
4846 genlmsg_cancel(msg, hdr);
4847 return -EMSGSIZE;
4850 static int nl80211_dump_station(struct sk_buff *skb,
4851 struct netlink_callback *cb)
4853 struct station_info sinfo;
4854 struct cfg80211_registered_device *rdev;
4855 struct wireless_dev *wdev;
4856 u8 mac_addr[ETH_ALEN];
4857 int sta_idx = cb->args[2];
4858 int err;
4860 rtnl_lock();
4861 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4862 if (err)
4863 goto out_err;
4865 if (!wdev->netdev) {
4866 err = -EINVAL;
4867 goto out_err;
4870 if (!rdev->ops->dump_station) {
4871 err = -EOPNOTSUPP;
4872 goto out_err;
4875 while (1) {
4876 memset(&sinfo, 0, sizeof(sinfo));
4877 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
4878 mac_addr, &sinfo);
4879 if (err == -ENOENT)
4880 break;
4881 if (err)
4882 goto out_err;
4884 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
4885 NETLINK_CB(cb->skb).portid,
4886 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4887 rdev, wdev->netdev, mac_addr,
4888 &sinfo) < 0)
4889 goto out;
4891 sta_idx++;
4894 out:
4895 cb->args[2] = sta_idx;
4896 err = skb->len;
4897 out_err:
4898 rtnl_unlock();
4900 return err;
4903 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
4905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4906 struct net_device *dev = info->user_ptr[1];
4907 struct station_info sinfo;
4908 struct sk_buff *msg;
4909 u8 *mac_addr = NULL;
4910 int err;
4912 memset(&sinfo, 0, sizeof(sinfo));
4914 if (!info->attrs[NL80211_ATTR_MAC])
4915 return -EINVAL;
4917 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4919 if (!rdev->ops->get_station)
4920 return -EOPNOTSUPP;
4922 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
4923 if (err)
4924 return err;
4926 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4927 if (!msg) {
4928 cfg80211_sinfo_release_content(&sinfo);
4929 return -ENOMEM;
4932 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
4933 info->snd_portid, info->snd_seq, 0,
4934 rdev, dev, mac_addr, &sinfo) < 0) {
4935 nlmsg_free(msg);
4936 return -ENOBUFS;
4939 return genlmsg_reply(msg, info);
4942 int cfg80211_check_station_change(struct wiphy *wiphy,
4943 struct station_parameters *params,
4944 enum cfg80211_station_type statype)
4946 if (params->listen_interval != -1 &&
4947 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4948 return -EINVAL;
4950 if (params->support_p2p_ps != -1 &&
4951 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4952 return -EINVAL;
4954 if (params->aid &&
4955 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
4956 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4957 return -EINVAL;
4959 /* When you run into this, adjust the code below for the new flag */
4960 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4962 switch (statype) {
4963 case CFG80211_STA_MESH_PEER_KERNEL:
4964 case CFG80211_STA_MESH_PEER_USER:
4966 * No ignoring the TDLS flag here -- the userspace mesh
4967 * code doesn't have the bug of including TDLS in the
4968 * mask everywhere.
4970 if (params->sta_flags_mask &
4971 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4972 BIT(NL80211_STA_FLAG_MFP) |
4973 BIT(NL80211_STA_FLAG_AUTHORIZED)))
4974 return -EINVAL;
4975 break;
4976 case CFG80211_STA_TDLS_PEER_SETUP:
4977 case CFG80211_STA_TDLS_PEER_ACTIVE:
4978 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4979 return -EINVAL;
4980 /* ignore since it can't change */
4981 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4982 break;
4983 default:
4984 /* disallow mesh-specific things */
4985 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4986 return -EINVAL;
4987 if (params->local_pm)
4988 return -EINVAL;
4989 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4990 return -EINVAL;
4993 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4994 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4995 /* TDLS can't be set, ... */
4996 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4997 return -EINVAL;
4999 * ... but don't bother the driver with it. This works around
5000 * a hostapd/wpa_supplicant issue -- it always includes the
5001 * TLDS_PEER flag in the mask even for AP mode.
5003 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5006 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5007 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5008 /* reject other things that can't change */
5009 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5010 return -EINVAL;
5011 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5012 return -EINVAL;
5013 if (params->supported_rates)
5014 return -EINVAL;
5015 if (params->ext_capab || params->ht_capa || params->vht_capa ||
5016 params->he_capa)
5017 return -EINVAL;
5020 if (statype != CFG80211_STA_AP_CLIENT &&
5021 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5022 if (params->vlan)
5023 return -EINVAL;
5026 switch (statype) {
5027 case CFG80211_STA_AP_MLME_CLIENT:
5028 /* Use this only for authorizing/unauthorizing a station */
5029 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5030 return -EOPNOTSUPP;
5031 break;
5032 case CFG80211_STA_AP_CLIENT:
5033 case CFG80211_STA_AP_CLIENT_UNASSOC:
5034 /* accept only the listed bits */
5035 if (params->sta_flags_mask &
5036 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5037 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5038 BIT(NL80211_STA_FLAG_ASSOCIATED) |
5039 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5040 BIT(NL80211_STA_FLAG_WME) |
5041 BIT(NL80211_STA_FLAG_MFP)))
5042 return -EINVAL;
5044 /* but authenticated/associated only if driver handles it */
5045 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5046 params->sta_flags_mask &
5047 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5048 BIT(NL80211_STA_FLAG_ASSOCIATED)))
5049 return -EINVAL;
5050 break;
5051 case CFG80211_STA_IBSS:
5052 case CFG80211_STA_AP_STA:
5053 /* reject any changes other than AUTHORIZED */
5054 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5055 return -EINVAL;
5056 break;
5057 case CFG80211_STA_TDLS_PEER_SETUP:
5058 /* reject any changes other than AUTHORIZED or WME */
5059 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5060 BIT(NL80211_STA_FLAG_WME)))
5061 return -EINVAL;
5062 /* force (at least) rates when authorizing */
5063 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5064 !params->supported_rates)
5065 return -EINVAL;
5066 break;
5067 case CFG80211_STA_TDLS_PEER_ACTIVE:
5068 /* reject any changes */
5069 return -EINVAL;
5070 case CFG80211_STA_MESH_PEER_KERNEL:
5071 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5072 return -EINVAL;
5073 break;
5074 case CFG80211_STA_MESH_PEER_USER:
5075 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5076 params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5077 return -EINVAL;
5078 break;
5082 * Older kernel versions ignored this attribute entirely, so don't
5083 * reject attempts to update it but mark it as unused instead so the
5084 * driver won't look at the data.
5086 if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5087 statype != CFG80211_STA_TDLS_PEER_SETUP)
5088 params->opmode_notif_used = false;
5090 return 0;
5092 EXPORT_SYMBOL(cfg80211_check_station_change);
5095 * Get vlan interface making sure it is running and on the right wiphy.
5097 static struct net_device *get_vlan(struct genl_info *info,
5098 struct cfg80211_registered_device *rdev)
5100 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5101 struct net_device *v;
5102 int ret;
5104 if (!vlanattr)
5105 return NULL;
5107 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5108 if (!v)
5109 return ERR_PTR(-ENODEV);
5111 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5112 ret = -EINVAL;
5113 goto error;
5116 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5117 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5118 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5119 ret = -EINVAL;
5120 goto error;
5123 if (!netif_running(v)) {
5124 ret = -ENETDOWN;
5125 goto error;
5128 return v;
5129 error:
5130 dev_put(v);
5131 return ERR_PTR(ret);
5134 static const struct nla_policy
5135 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5136 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5137 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5140 static int nl80211_parse_sta_wme(struct genl_info *info,
5141 struct station_parameters *params)
5143 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5144 struct nlattr *nla;
5145 int err;
5147 /* parse WME attributes if present */
5148 if (!info->attrs[NL80211_ATTR_STA_WME])
5149 return 0;
5151 nla = info->attrs[NL80211_ATTR_STA_WME];
5152 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
5153 nl80211_sta_wme_policy, info->extack);
5154 if (err)
5155 return err;
5157 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5158 params->uapsd_queues = nla_get_u8(
5159 tb[NL80211_STA_WME_UAPSD_QUEUES]);
5160 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5161 return -EINVAL;
5163 if (tb[NL80211_STA_WME_MAX_SP])
5164 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5166 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5167 return -EINVAL;
5169 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5171 return 0;
5174 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5175 struct station_parameters *params)
5177 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5178 params->supported_channels =
5179 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5180 params->supported_channels_len =
5181 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5183 * Need to include at least one (first channel, number of
5184 * channels) tuple for each subband, and must have proper
5185 * tuples for the rest of the data as well.
5187 if (params->supported_channels_len < 2)
5188 return -EINVAL;
5189 if (params->supported_channels_len % 2)
5190 return -EINVAL;
5193 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5194 params->supported_oper_classes =
5195 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5196 params->supported_oper_classes_len =
5197 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5199 * The value of the Length field of the Supported Operating
5200 * Classes element is between 2 and 253.
5202 if (params->supported_oper_classes_len < 2 ||
5203 params->supported_oper_classes_len > 253)
5204 return -EINVAL;
5206 return 0;
5209 static int nl80211_set_station_tdls(struct genl_info *info,
5210 struct station_parameters *params)
5212 int err;
5213 /* Dummy STA entry gets updated once the peer capabilities are known */
5214 if (info->attrs[NL80211_ATTR_PEER_AID])
5215 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5216 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5217 params->ht_capa =
5218 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5219 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5220 params->vht_capa =
5221 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5222 if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5223 params->he_capa =
5224 nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5225 params->he_capa_len =
5226 nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5228 if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5229 return -EINVAL;
5232 err = nl80211_parse_sta_channel_info(info, params);
5233 if (err)
5234 return err;
5236 return nl80211_parse_sta_wme(info, params);
5239 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5241 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5242 struct net_device *dev = info->user_ptr[1];
5243 struct station_parameters params;
5244 u8 *mac_addr;
5245 int err;
5247 memset(&params, 0, sizeof(params));
5249 if (!rdev->ops->change_station)
5250 return -EOPNOTSUPP;
5253 * AID and listen_interval properties can be set only for unassociated
5254 * station. Include these parameters here and will check them in
5255 * cfg80211_check_station_change().
5257 if (info->attrs[NL80211_ATTR_STA_AID])
5258 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5260 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5261 params.listen_interval =
5262 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5263 else
5264 params.listen_interval = -1;
5266 if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5267 u8 tmp;
5269 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5270 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5271 return -EINVAL;
5273 params.support_p2p_ps = tmp;
5274 } else {
5275 params.support_p2p_ps = -1;
5278 if (!info->attrs[NL80211_ATTR_MAC])
5279 return -EINVAL;
5281 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5283 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5284 params.supported_rates =
5285 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5286 params.supported_rates_len =
5287 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5290 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5291 params.capability =
5292 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5293 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5296 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5297 params.ext_capab =
5298 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5299 params.ext_capab_len =
5300 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5303 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5304 return -EINVAL;
5306 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5307 params.plink_action =
5308 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5309 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5310 return -EINVAL;
5313 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5314 params.plink_state =
5315 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5316 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
5317 return -EINVAL;
5318 if (info->attrs[NL80211_ATTR_MESH_PEER_AID]) {
5319 params.peer_aid = nla_get_u16(
5320 info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5321 if (params.peer_aid > IEEE80211_MAX_AID)
5322 return -EINVAL;
5324 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5327 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
5328 enum nl80211_mesh_power_mode pm = nla_get_u32(
5329 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5331 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
5332 pm > NL80211_MESH_POWER_MAX)
5333 return -EINVAL;
5335 params.local_pm = pm;
5338 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5339 params.opmode_notif_used = true;
5340 params.opmode_notif =
5341 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5344 /* Include parameters for TDLS peer (will check later) */
5345 err = nl80211_set_station_tdls(info, &params);
5346 if (err)
5347 return err;
5349 params.vlan = get_vlan(info, rdev);
5350 if (IS_ERR(params.vlan))
5351 return PTR_ERR(params.vlan);
5353 switch (dev->ieee80211_ptr->iftype) {
5354 case NL80211_IFTYPE_AP:
5355 case NL80211_IFTYPE_AP_VLAN:
5356 case NL80211_IFTYPE_P2P_GO:
5357 case NL80211_IFTYPE_P2P_CLIENT:
5358 case NL80211_IFTYPE_STATION:
5359 case NL80211_IFTYPE_ADHOC:
5360 case NL80211_IFTYPE_MESH_POINT:
5361 break;
5362 default:
5363 err = -EOPNOTSUPP;
5364 goto out_put_vlan;
5367 /* driver will call cfg80211_check_station_change() */
5368 err = rdev_change_station(rdev, dev, mac_addr, &params);
5370 out_put_vlan:
5371 if (params.vlan)
5372 dev_put(params.vlan);
5374 return err;
5377 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5379 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5380 int err;
5381 struct net_device *dev = info->user_ptr[1];
5382 struct station_parameters params;
5383 u8 *mac_addr = NULL;
5384 u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5385 BIT(NL80211_STA_FLAG_ASSOCIATED);
5387 memset(&params, 0, sizeof(params));
5389 if (!rdev->ops->add_station)
5390 return -EOPNOTSUPP;
5392 if (!info->attrs[NL80211_ATTR_MAC])
5393 return -EINVAL;
5395 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5396 return -EINVAL;
5398 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5399 return -EINVAL;
5401 if (!info->attrs[NL80211_ATTR_STA_AID] &&
5402 !info->attrs[NL80211_ATTR_PEER_AID])
5403 return -EINVAL;
5405 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5406 params.supported_rates =
5407 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5408 params.supported_rates_len =
5409 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5410 params.listen_interval =
5411 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5413 if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5414 u8 tmp;
5416 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5417 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5418 return -EINVAL;
5420 params.support_p2p_ps = tmp;
5421 } else {
5423 * if not specified, assume it's supported for P2P GO interface,
5424 * and is NOT supported for AP interface
5426 params.support_p2p_ps =
5427 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5430 if (info->attrs[NL80211_ATTR_PEER_AID])
5431 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5432 else
5433 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5434 if (!params.aid || params.aid > IEEE80211_MAX_AID)
5435 return -EINVAL;
5437 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5438 params.capability =
5439 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5440 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5443 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5444 params.ext_capab =
5445 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5446 params.ext_capab_len =
5447 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5450 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5451 params.ht_capa =
5452 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5454 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5455 params.vht_capa =
5456 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5458 if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5459 params.he_capa =
5460 nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5461 params.he_capa_len =
5462 nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5464 /* max len is validated in nla policy */
5465 if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5466 return -EINVAL;
5469 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5470 params.opmode_notif_used = true;
5471 params.opmode_notif =
5472 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5475 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5476 params.plink_action =
5477 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5478 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5479 return -EINVAL;
5482 err = nl80211_parse_sta_channel_info(info, &params);
5483 if (err)
5484 return err;
5486 err = nl80211_parse_sta_wme(info, &params);
5487 if (err)
5488 return err;
5490 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5491 return -EINVAL;
5493 /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5494 * as userspace might just pass through the capabilities from the IEs
5495 * directly, rather than enforcing this restriction and returning an
5496 * error in this case.
5498 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5499 params.ht_capa = NULL;
5500 params.vht_capa = NULL;
5502 /* HE requires WME */
5503 if (params.he_capa_len)
5504 return -EINVAL;
5507 /* When you run into this, adjust the code below for the new flag */
5508 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5510 switch (dev->ieee80211_ptr->iftype) {
5511 case NL80211_IFTYPE_AP:
5512 case NL80211_IFTYPE_AP_VLAN:
5513 case NL80211_IFTYPE_P2P_GO:
5514 /* ignore WME attributes if iface/sta is not capable */
5515 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5516 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5517 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5519 /* TDLS peers cannot be added */
5520 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5521 info->attrs[NL80211_ATTR_PEER_AID])
5522 return -EINVAL;
5523 /* but don't bother the driver with it */
5524 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5526 /* allow authenticated/associated only if driver handles it */
5527 if (!(rdev->wiphy.features &
5528 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5529 params.sta_flags_mask & auth_assoc)
5530 return -EINVAL;
5532 /* Older userspace, or userspace wanting to be compatible with
5533 * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5534 * and assoc flags in the mask, but assumes the station will be
5535 * added as associated anyway since this was the required driver
5536 * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5537 * introduced.
5538 * In order to not bother drivers with this quirk in the API
5539 * set the flags in both the mask and set for new stations in
5540 * this case.
5542 if (!(params.sta_flags_mask & auth_assoc)) {
5543 params.sta_flags_mask |= auth_assoc;
5544 params.sta_flags_set |= auth_assoc;
5547 /* must be last in here for error handling */
5548 params.vlan = get_vlan(info, rdev);
5549 if (IS_ERR(params.vlan))
5550 return PTR_ERR(params.vlan);
5551 break;
5552 case NL80211_IFTYPE_MESH_POINT:
5553 /* ignore uAPSD data */
5554 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5556 /* associated is disallowed */
5557 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5558 return -EINVAL;
5559 /* TDLS peers cannot be added */
5560 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5561 info->attrs[NL80211_ATTR_PEER_AID])
5562 return -EINVAL;
5563 break;
5564 case NL80211_IFTYPE_STATION:
5565 case NL80211_IFTYPE_P2P_CLIENT:
5566 /* ignore uAPSD data */
5567 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5569 /* these are disallowed */
5570 if (params.sta_flags_mask &
5571 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
5572 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
5573 return -EINVAL;
5574 /* Only TDLS peers can be added */
5575 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5576 return -EINVAL;
5577 /* Can only add if TDLS ... */
5578 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5579 return -EOPNOTSUPP;
5580 /* ... with external setup is supported */
5581 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5582 return -EOPNOTSUPP;
5584 * Older wpa_supplicant versions always mark the TDLS peer
5585 * as authorized, but it shouldn't yet be.
5587 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
5588 break;
5589 default:
5590 return -EOPNOTSUPP;
5593 /* be aware of params.vlan when changing code here */
5595 err = rdev_add_station(rdev, dev, mac_addr, &params);
5597 if (params.vlan)
5598 dev_put(params.vlan);
5599 return err;
5602 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5604 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5605 struct net_device *dev = info->user_ptr[1];
5606 struct station_del_parameters params;
5608 memset(&params, 0, sizeof(params));
5610 if (info->attrs[NL80211_ATTR_MAC])
5611 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
5613 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5614 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5615 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5616 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5617 return -EINVAL;
5619 if (!rdev->ops->del_station)
5620 return -EOPNOTSUPP;
5622 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
5623 params.subtype =
5624 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
5625 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
5626 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
5627 return -EINVAL;
5628 } else {
5629 /* Default to Deauthentication frame */
5630 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
5633 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
5634 params.reason_code =
5635 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5636 if (params.reason_code == 0)
5637 return -EINVAL; /* 0 is reserved */
5638 } else {
5639 /* Default to reason code 2 */
5640 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
5643 return rdev_del_station(rdev, dev, &params);
5646 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
5647 int flags, struct net_device *dev,
5648 u8 *dst, u8 *next_hop,
5649 struct mpath_info *pinfo)
5651 void *hdr;
5652 struct nlattr *pinfoattr;
5654 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
5655 if (!hdr)
5656 return -1;
5658 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5659 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
5660 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
5661 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
5662 goto nla_put_failure;
5664 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
5665 if (!pinfoattr)
5666 goto nla_put_failure;
5667 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
5668 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
5669 pinfo->frame_qlen))
5670 goto nla_put_failure;
5671 if (((pinfo->filled & MPATH_INFO_SN) &&
5672 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
5673 ((pinfo->filled & MPATH_INFO_METRIC) &&
5674 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
5675 pinfo->metric)) ||
5676 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
5677 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
5678 pinfo->exptime)) ||
5679 ((pinfo->filled & MPATH_INFO_FLAGS) &&
5680 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
5681 pinfo->flags)) ||
5682 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
5683 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
5684 pinfo->discovery_timeout)) ||
5685 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
5686 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
5687 pinfo->discovery_retries)))
5688 goto nla_put_failure;
5690 nla_nest_end(msg, pinfoattr);
5692 genlmsg_end(msg, hdr);
5693 return 0;
5695 nla_put_failure:
5696 genlmsg_cancel(msg, hdr);
5697 return -EMSGSIZE;
5700 static int nl80211_dump_mpath(struct sk_buff *skb,
5701 struct netlink_callback *cb)
5703 struct mpath_info pinfo;
5704 struct cfg80211_registered_device *rdev;
5705 struct wireless_dev *wdev;
5706 u8 dst[ETH_ALEN];
5707 u8 next_hop[ETH_ALEN];
5708 int path_idx = cb->args[2];
5709 int err;
5711 rtnl_lock();
5712 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5713 if (err)
5714 goto out_err;
5716 if (!rdev->ops->dump_mpath) {
5717 err = -EOPNOTSUPP;
5718 goto out_err;
5721 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5722 err = -EOPNOTSUPP;
5723 goto out_err;
5726 while (1) {
5727 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
5728 next_hop, &pinfo);
5729 if (err == -ENOENT)
5730 break;
5731 if (err)
5732 goto out_err;
5734 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5735 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5736 wdev->netdev, dst, next_hop,
5737 &pinfo) < 0)
5738 goto out;
5740 path_idx++;
5743 out:
5744 cb->args[2] = path_idx;
5745 err = skb->len;
5746 out_err:
5747 rtnl_unlock();
5748 return err;
5751 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
5753 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5754 int err;
5755 struct net_device *dev = info->user_ptr[1];
5756 struct mpath_info pinfo;
5757 struct sk_buff *msg;
5758 u8 *dst = NULL;
5759 u8 next_hop[ETH_ALEN];
5761 memset(&pinfo, 0, sizeof(pinfo));
5763 if (!info->attrs[NL80211_ATTR_MAC])
5764 return -EINVAL;
5766 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5768 if (!rdev->ops->get_mpath)
5769 return -EOPNOTSUPP;
5771 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5772 return -EOPNOTSUPP;
5774 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
5775 if (err)
5776 return err;
5778 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5779 if (!msg)
5780 return -ENOMEM;
5782 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5783 dev, dst, next_hop, &pinfo) < 0) {
5784 nlmsg_free(msg);
5785 return -ENOBUFS;
5788 return genlmsg_reply(msg, info);
5791 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
5793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5794 struct net_device *dev = info->user_ptr[1];
5795 u8 *dst = NULL;
5796 u8 *next_hop = NULL;
5798 if (!info->attrs[NL80211_ATTR_MAC])
5799 return -EINVAL;
5801 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5802 return -EINVAL;
5804 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5805 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5807 if (!rdev->ops->change_mpath)
5808 return -EOPNOTSUPP;
5810 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5811 return -EOPNOTSUPP;
5813 return rdev_change_mpath(rdev, dev, dst, next_hop);
5816 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
5818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5819 struct net_device *dev = info->user_ptr[1];
5820 u8 *dst = NULL;
5821 u8 *next_hop = NULL;
5823 if (!info->attrs[NL80211_ATTR_MAC])
5824 return -EINVAL;
5826 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5827 return -EINVAL;
5829 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5830 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5832 if (!rdev->ops->add_mpath)
5833 return -EOPNOTSUPP;
5835 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5836 return -EOPNOTSUPP;
5838 return rdev_add_mpath(rdev, dev, dst, next_hop);
5841 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
5843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5844 struct net_device *dev = info->user_ptr[1];
5845 u8 *dst = NULL;
5847 if (info->attrs[NL80211_ATTR_MAC])
5848 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5850 if (!rdev->ops->del_mpath)
5851 return -EOPNOTSUPP;
5853 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5854 return -EOPNOTSUPP;
5856 return rdev_del_mpath(rdev, dev, dst);
5859 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
5861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5862 int err;
5863 struct net_device *dev = info->user_ptr[1];
5864 struct mpath_info pinfo;
5865 struct sk_buff *msg;
5866 u8 *dst = NULL;
5867 u8 mpp[ETH_ALEN];
5869 memset(&pinfo, 0, sizeof(pinfo));
5871 if (!info->attrs[NL80211_ATTR_MAC])
5872 return -EINVAL;
5874 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5876 if (!rdev->ops->get_mpp)
5877 return -EOPNOTSUPP;
5879 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5880 return -EOPNOTSUPP;
5882 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
5883 if (err)
5884 return err;
5886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5887 if (!msg)
5888 return -ENOMEM;
5890 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5891 dev, dst, mpp, &pinfo) < 0) {
5892 nlmsg_free(msg);
5893 return -ENOBUFS;
5896 return genlmsg_reply(msg, info);
5899 static int nl80211_dump_mpp(struct sk_buff *skb,
5900 struct netlink_callback *cb)
5902 struct mpath_info pinfo;
5903 struct cfg80211_registered_device *rdev;
5904 struct wireless_dev *wdev;
5905 u8 dst[ETH_ALEN];
5906 u8 mpp[ETH_ALEN];
5907 int path_idx = cb->args[2];
5908 int err;
5910 rtnl_lock();
5911 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5912 if (err)
5913 goto out_err;
5915 if (!rdev->ops->dump_mpp) {
5916 err = -EOPNOTSUPP;
5917 goto out_err;
5920 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5921 err = -EOPNOTSUPP;
5922 goto out_err;
5925 while (1) {
5926 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
5927 mpp, &pinfo);
5928 if (err == -ENOENT)
5929 break;
5930 if (err)
5931 goto out_err;
5933 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5934 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5935 wdev->netdev, dst, mpp,
5936 &pinfo) < 0)
5937 goto out;
5939 path_idx++;
5942 out:
5943 cb->args[2] = path_idx;
5944 err = skb->len;
5945 out_err:
5946 rtnl_unlock();
5947 return err;
5950 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
5952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5953 struct net_device *dev = info->user_ptr[1];
5954 struct wireless_dev *wdev = dev->ieee80211_ptr;
5955 struct bss_parameters params;
5956 int err;
5958 memset(&params, 0, sizeof(params));
5959 /* default to not changing parameters */
5960 params.use_cts_prot = -1;
5961 params.use_short_preamble = -1;
5962 params.use_short_slot_time = -1;
5963 params.ap_isolate = -1;
5964 params.ht_opmode = -1;
5965 params.p2p_ctwindow = -1;
5966 params.p2p_opp_ps = -1;
5968 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
5969 params.use_cts_prot =
5970 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
5971 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
5972 params.use_short_preamble =
5973 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
5974 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
5975 params.use_short_slot_time =
5976 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
5977 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5978 params.basic_rates =
5979 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5980 params.basic_rates_len =
5981 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5983 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
5984 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
5985 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
5986 params.ht_opmode =
5987 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
5989 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5990 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5991 return -EINVAL;
5992 params.p2p_ctwindow =
5993 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5994 if (params.p2p_ctwindow < 0)
5995 return -EINVAL;
5996 if (params.p2p_ctwindow != 0 &&
5997 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5998 return -EINVAL;
6001 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6002 u8 tmp;
6004 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6005 return -EINVAL;
6006 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6007 if (tmp > 1)
6008 return -EINVAL;
6009 params.p2p_opp_ps = tmp;
6010 if (params.p2p_opp_ps &&
6011 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6012 return -EINVAL;
6015 if (!rdev->ops->change_bss)
6016 return -EOPNOTSUPP;
6018 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6019 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6020 return -EOPNOTSUPP;
6022 wdev_lock(wdev);
6023 err = rdev_change_bss(rdev, dev, &params);
6024 wdev_unlock(wdev);
6026 return err;
6029 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6031 char *data = NULL;
6032 bool is_indoor;
6033 enum nl80211_user_reg_hint_type user_reg_hint_type;
6034 u32 owner_nlportid;
6037 * You should only get this when cfg80211 hasn't yet initialized
6038 * completely when built-in to the kernel right between the time
6039 * window between nl80211_init() and regulatory_init(), if that is
6040 * even possible.
6042 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6043 return -EINPROGRESS;
6045 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6046 user_reg_hint_type =
6047 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6048 else
6049 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6051 switch (user_reg_hint_type) {
6052 case NL80211_USER_REG_HINT_USER:
6053 case NL80211_USER_REG_HINT_CELL_BASE:
6054 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6055 return -EINVAL;
6057 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6058 return regulatory_hint_user(data, user_reg_hint_type);
6059 case NL80211_USER_REG_HINT_INDOOR:
6060 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6061 owner_nlportid = info->snd_portid;
6062 is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6063 } else {
6064 owner_nlportid = 0;
6065 is_indoor = true;
6068 return regulatory_hint_indoor(is_indoor, owner_nlportid);
6069 default:
6070 return -EINVAL;
6074 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6076 return reg_reload_regdb();
6079 static int nl80211_get_mesh_config(struct sk_buff *skb,
6080 struct genl_info *info)
6082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6083 struct net_device *dev = info->user_ptr[1];
6084 struct wireless_dev *wdev = dev->ieee80211_ptr;
6085 struct mesh_config cur_params;
6086 int err = 0;
6087 void *hdr;
6088 struct nlattr *pinfoattr;
6089 struct sk_buff *msg;
6091 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6092 return -EOPNOTSUPP;
6094 if (!rdev->ops->get_mesh_config)
6095 return -EOPNOTSUPP;
6097 wdev_lock(wdev);
6098 /* If not connected, get default parameters */
6099 if (!wdev->mesh_id_len)
6100 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6101 else
6102 err = rdev_get_mesh_config(rdev, dev, &cur_params);
6103 wdev_unlock(wdev);
6105 if (err)
6106 return err;
6108 /* Draw up a netlink message to send back */
6109 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6110 if (!msg)
6111 return -ENOMEM;
6112 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6113 NL80211_CMD_GET_MESH_CONFIG);
6114 if (!hdr)
6115 goto out;
6116 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
6117 if (!pinfoattr)
6118 goto nla_put_failure;
6119 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6120 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6121 cur_params.dot11MeshRetryTimeout) ||
6122 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6123 cur_params.dot11MeshConfirmTimeout) ||
6124 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6125 cur_params.dot11MeshHoldingTimeout) ||
6126 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6127 cur_params.dot11MeshMaxPeerLinks) ||
6128 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6129 cur_params.dot11MeshMaxRetries) ||
6130 nla_put_u8(msg, NL80211_MESHCONF_TTL,
6131 cur_params.dot11MeshTTL) ||
6132 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6133 cur_params.element_ttl) ||
6134 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6135 cur_params.auto_open_plinks) ||
6136 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6137 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6138 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6139 cur_params.dot11MeshHWMPmaxPREQretries) ||
6140 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6141 cur_params.path_refresh_time) ||
6142 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6143 cur_params.min_discovery_timeout) ||
6144 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6145 cur_params.dot11MeshHWMPactivePathTimeout) ||
6146 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6147 cur_params.dot11MeshHWMPpreqMinInterval) ||
6148 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6149 cur_params.dot11MeshHWMPperrMinInterval) ||
6150 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6151 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6152 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6153 cur_params.dot11MeshHWMPRootMode) ||
6154 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6155 cur_params.dot11MeshHWMPRannInterval) ||
6156 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6157 cur_params.dot11MeshGateAnnouncementProtocol) ||
6158 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6159 cur_params.dot11MeshForwarding) ||
6160 nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6161 cur_params.rssi_threshold) ||
6162 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6163 cur_params.ht_opmode) ||
6164 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6165 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6166 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6167 cur_params.dot11MeshHWMProotInterval) ||
6168 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6169 cur_params.dot11MeshHWMPconfirmationInterval) ||
6170 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6171 cur_params.power_mode) ||
6172 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6173 cur_params.dot11MeshAwakeWindowDuration) ||
6174 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6175 cur_params.plink_timeout))
6176 goto nla_put_failure;
6177 nla_nest_end(msg, pinfoattr);
6178 genlmsg_end(msg, hdr);
6179 return genlmsg_reply(msg, info);
6181 nla_put_failure:
6182 out:
6183 nlmsg_free(msg);
6184 return -ENOBUFS;
6187 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6188 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
6189 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
6190 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
6191 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
6192 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
6193 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
6194 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
6195 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
6196 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
6197 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6198 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6199 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
6200 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6201 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
6202 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
6203 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
6204 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
6205 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
6206 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
6207 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
6208 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
6209 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6210 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6211 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
6212 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
6213 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
6214 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6215 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6218 static const struct nla_policy
6219 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6220 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6221 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6222 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6223 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6224 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6225 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6226 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
6227 .len = IEEE80211_MAX_DATA_LEN },
6228 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6231 static int nl80211_check_bool(const struct nlattr *nla, u8 min, u8 max, bool *out)
6233 u8 val = nla_get_u8(nla);
6234 if (val < min || val > max)
6235 return -EINVAL;
6236 *out = val;
6237 return 0;
6240 static int nl80211_check_u8(const struct nlattr *nla, u8 min, u8 max, u8 *out)
6242 u8 val = nla_get_u8(nla);
6243 if (val < min || val > max)
6244 return -EINVAL;
6245 *out = val;
6246 return 0;
6249 static int nl80211_check_u16(const struct nlattr *nla, u16 min, u16 max, u16 *out)
6251 u16 val = nla_get_u16(nla);
6252 if (val < min || val > max)
6253 return -EINVAL;
6254 *out = val;
6255 return 0;
6258 static int nl80211_check_u32(const struct nlattr *nla, u32 min, u32 max, u32 *out)
6260 u32 val = nla_get_u32(nla);
6261 if (val < min || val > max)
6262 return -EINVAL;
6263 *out = val;
6264 return 0;
6267 static int nl80211_check_s32(const struct nlattr *nla, s32 min, s32 max, s32 *out)
6269 s32 val = nla_get_s32(nla);
6270 if (val < min || val > max)
6271 return -EINVAL;
6272 *out = val;
6273 return 0;
6276 static int nl80211_check_power_mode(const struct nlattr *nla,
6277 enum nl80211_mesh_power_mode min,
6278 enum nl80211_mesh_power_mode max,
6279 enum nl80211_mesh_power_mode *out)
6281 u32 val = nla_get_u32(nla);
6282 if (val < min || val > max)
6283 return -EINVAL;
6284 *out = val;
6285 return 0;
6288 static int nl80211_parse_mesh_config(struct genl_info *info,
6289 struct mesh_config *cfg,
6290 u32 *mask_out)
6292 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6293 u32 mask = 0;
6294 u16 ht_opmode;
6296 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
6297 do { \
6298 if (tb[attr]) { \
6299 if (fn(tb[attr], min, max, &cfg->param)) \
6300 return -EINVAL; \
6301 mask |= (1 << (attr - 1)); \
6303 } while (0)
6305 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6306 return -EINVAL;
6307 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
6308 info->attrs[NL80211_ATTR_MESH_CONFIG],
6309 nl80211_meshconf_params_policy, info->extack))
6310 return -EINVAL;
6312 /* This makes sure that there aren't more than 32 mesh config
6313 * parameters (otherwise our bitfield scheme would not work.) */
6314 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6316 /* Fill in the params struct */
6317 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
6318 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
6319 nl80211_check_u16);
6320 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
6321 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6322 nl80211_check_u16);
6323 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
6324 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
6325 nl80211_check_u16);
6326 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
6327 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
6328 nl80211_check_u16);
6329 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
6330 mask, NL80211_MESHCONF_MAX_RETRIES,
6331 nl80211_check_u8);
6332 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
6333 mask, NL80211_MESHCONF_TTL, nl80211_check_u8);
6334 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
6335 mask, NL80211_MESHCONF_ELEMENT_TTL,
6336 nl80211_check_u8);
6337 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
6338 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6339 nl80211_check_bool);
6340 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6341 1, 255, mask,
6342 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6343 nl80211_check_u32);
6344 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
6345 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6346 nl80211_check_u8);
6347 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
6348 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
6349 nl80211_check_u32);
6350 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
6351 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6352 nl80211_check_u16);
6353 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6354 1, 65535, mask,
6355 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6356 nl80211_check_u32);
6357 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
6358 1, 65535, mask,
6359 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6360 nl80211_check_u16);
6361 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
6362 1, 65535, mask,
6363 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6364 nl80211_check_u16);
6365 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6366 dot11MeshHWMPnetDiameterTraversalTime,
6367 1, 65535, mask,
6368 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6369 nl80211_check_u16);
6370 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
6371 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
6372 nl80211_check_u8);
6373 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
6374 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6375 nl80211_check_u16);
6376 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6377 dot11MeshGateAnnouncementProtocol, 0, 1,
6378 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6379 nl80211_check_bool);
6380 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
6381 mask, NL80211_MESHCONF_FORWARDING,
6382 nl80211_check_bool);
6383 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
6384 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
6385 nl80211_check_s32);
6387 * Check HT operation mode based on
6388 * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6390 if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6391 ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6393 if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6394 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6395 IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6396 return -EINVAL;
6398 /* NON_HT_STA bit is reserved, but some programs set it */
6399 ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6401 cfg->ht_opmode = ht_opmode;
6402 mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6404 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
6405 1, 65535, mask,
6406 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6407 nl80211_check_u32);
6408 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
6409 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6410 nl80211_check_u16);
6411 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6412 dot11MeshHWMPconfirmationInterval,
6413 1, 65535, mask,
6414 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6415 nl80211_check_u16);
6416 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
6417 NL80211_MESH_POWER_ACTIVE,
6418 NL80211_MESH_POWER_MAX,
6419 mask, NL80211_MESHCONF_POWER_MODE,
6420 nl80211_check_power_mode);
6421 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
6422 0, 65535, mask,
6423 NL80211_MESHCONF_AWAKE_WINDOW, nl80211_check_u16);
6424 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
6425 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
6426 nl80211_check_u32);
6427 if (mask_out)
6428 *mask_out = mask;
6430 return 0;
6432 #undef FILL_IN_MESH_PARAM_IF_SET
6435 static int nl80211_parse_mesh_setup(struct genl_info *info,
6436 struct mesh_setup *setup)
6438 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6439 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6441 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6442 return -EINVAL;
6443 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
6444 info->attrs[NL80211_ATTR_MESH_SETUP],
6445 nl80211_mesh_setup_params_policy, info->extack))
6446 return -EINVAL;
6448 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6449 setup->sync_method =
6450 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6451 IEEE80211_SYNC_METHOD_VENDOR :
6452 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6454 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6455 setup->path_sel_proto =
6456 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6457 IEEE80211_PATH_PROTOCOL_VENDOR :
6458 IEEE80211_PATH_PROTOCOL_HWMP;
6460 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6461 setup->path_metric =
6462 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6463 IEEE80211_PATH_METRIC_VENDOR :
6464 IEEE80211_PATH_METRIC_AIRTIME;
6466 if (tb[NL80211_MESH_SETUP_IE]) {
6467 struct nlattr *ieattr =
6468 tb[NL80211_MESH_SETUP_IE];
6469 if (!is_valid_ie_attr(ieattr))
6470 return -EINVAL;
6471 setup->ie = nla_data(ieattr);
6472 setup->ie_len = nla_len(ieattr);
6474 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6475 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6476 return -EINVAL;
6477 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6478 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6479 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6480 if (setup->is_secure)
6481 setup->user_mpm = true;
6483 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6484 if (!setup->user_mpm)
6485 return -EINVAL;
6486 setup->auth_id =
6487 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6490 return 0;
6493 static int nl80211_update_mesh_config(struct sk_buff *skb,
6494 struct genl_info *info)
6496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6497 struct net_device *dev = info->user_ptr[1];
6498 struct wireless_dev *wdev = dev->ieee80211_ptr;
6499 struct mesh_config cfg;
6500 u32 mask;
6501 int err;
6503 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6504 return -EOPNOTSUPP;
6506 if (!rdev->ops->update_mesh_config)
6507 return -EOPNOTSUPP;
6509 err = nl80211_parse_mesh_config(info, &cfg, &mask);
6510 if (err)
6511 return err;
6513 wdev_lock(wdev);
6514 if (!wdev->mesh_id_len)
6515 err = -ENOLINK;
6517 if (!err)
6518 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6520 wdev_unlock(wdev);
6522 return err;
6525 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6526 struct sk_buff *msg)
6528 struct nlattr *nl_reg_rules;
6529 unsigned int i;
6531 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6532 (regdom->dfs_region &&
6533 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6534 goto nla_put_failure;
6536 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
6537 if (!nl_reg_rules)
6538 goto nla_put_failure;
6540 for (i = 0; i < regdom->n_reg_rules; i++) {
6541 struct nlattr *nl_reg_rule;
6542 const struct ieee80211_reg_rule *reg_rule;
6543 const struct ieee80211_freq_range *freq_range;
6544 const struct ieee80211_power_rule *power_rule;
6545 unsigned int max_bandwidth_khz;
6547 reg_rule = &regdom->reg_rules[i];
6548 freq_range = &reg_rule->freq_range;
6549 power_rule = &reg_rule->power_rule;
6551 nl_reg_rule = nla_nest_start(msg, i);
6552 if (!nl_reg_rule)
6553 goto nla_put_failure;
6555 max_bandwidth_khz = freq_range->max_bandwidth_khz;
6556 if (!max_bandwidth_khz)
6557 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6558 reg_rule);
6560 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6561 reg_rule->flags) ||
6562 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6563 freq_range->start_freq_khz) ||
6564 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6565 freq_range->end_freq_khz) ||
6566 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6567 max_bandwidth_khz) ||
6568 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6569 power_rule->max_antenna_gain) ||
6570 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6571 power_rule->max_eirp) ||
6572 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6573 reg_rule->dfs_cac_ms))
6574 goto nla_put_failure;
6576 nla_nest_end(msg, nl_reg_rule);
6579 nla_nest_end(msg, nl_reg_rules);
6580 return 0;
6582 nla_put_failure:
6583 return -EMSGSIZE;
6586 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6588 const struct ieee80211_regdomain *regdom = NULL;
6589 struct cfg80211_registered_device *rdev;
6590 struct wiphy *wiphy = NULL;
6591 struct sk_buff *msg;
6592 void *hdr;
6594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6595 if (!msg)
6596 return -ENOBUFS;
6598 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6599 NL80211_CMD_GET_REG);
6600 if (!hdr)
6601 goto put_failure;
6603 if (info->attrs[NL80211_ATTR_WIPHY]) {
6604 bool self_managed;
6606 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6607 if (IS_ERR(rdev)) {
6608 nlmsg_free(msg);
6609 return PTR_ERR(rdev);
6612 wiphy = &rdev->wiphy;
6613 self_managed = wiphy->regulatory_flags &
6614 REGULATORY_WIPHY_SELF_MANAGED;
6615 regdom = get_wiphy_regdom(wiphy);
6617 /* a self-managed-reg device must have a private regdom */
6618 if (WARN_ON(!regdom && self_managed)) {
6619 nlmsg_free(msg);
6620 return -EINVAL;
6623 if (regdom &&
6624 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6625 goto nla_put_failure;
6628 if (!wiphy && reg_last_request_cell_base() &&
6629 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6630 NL80211_USER_REG_HINT_CELL_BASE))
6631 goto nla_put_failure;
6633 rcu_read_lock();
6635 if (!regdom)
6636 regdom = rcu_dereference(cfg80211_regdomain);
6638 if (nl80211_put_regdom(regdom, msg))
6639 goto nla_put_failure_rcu;
6641 rcu_read_unlock();
6643 genlmsg_end(msg, hdr);
6644 return genlmsg_reply(msg, info);
6646 nla_put_failure_rcu:
6647 rcu_read_unlock();
6648 nla_put_failure:
6649 put_failure:
6650 nlmsg_free(msg);
6651 return -EMSGSIZE;
6654 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6655 u32 seq, int flags, struct wiphy *wiphy,
6656 const struct ieee80211_regdomain *regdom)
6658 void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6659 NL80211_CMD_GET_REG);
6661 if (!hdr)
6662 return -1;
6664 genl_dump_check_consistent(cb, hdr);
6666 if (nl80211_put_regdom(regdom, msg))
6667 goto nla_put_failure;
6669 if (!wiphy && reg_last_request_cell_base() &&
6670 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6671 NL80211_USER_REG_HINT_CELL_BASE))
6672 goto nla_put_failure;
6674 if (wiphy &&
6675 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6676 goto nla_put_failure;
6678 if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
6679 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
6680 goto nla_put_failure;
6682 genlmsg_end(msg, hdr);
6683 return 0;
6685 nla_put_failure:
6686 genlmsg_cancel(msg, hdr);
6687 return -EMSGSIZE;
6690 static int nl80211_get_reg_dump(struct sk_buff *skb,
6691 struct netlink_callback *cb)
6693 const struct ieee80211_regdomain *regdom = NULL;
6694 struct cfg80211_registered_device *rdev;
6695 int err, reg_idx, start = cb->args[2];
6697 rtnl_lock();
6699 if (cfg80211_regdomain && start == 0) {
6700 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6701 NLM_F_MULTI, NULL,
6702 rtnl_dereference(cfg80211_regdomain));
6703 if (err < 0)
6704 goto out_err;
6707 /* the global regdom is idx 0 */
6708 reg_idx = 1;
6709 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
6710 regdom = get_wiphy_regdom(&rdev->wiphy);
6711 if (!regdom)
6712 continue;
6714 if (++reg_idx <= start)
6715 continue;
6717 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6718 NLM_F_MULTI, &rdev->wiphy, regdom);
6719 if (err < 0) {
6720 reg_idx--;
6721 break;
6725 cb->args[2] = reg_idx;
6726 err = skb->len;
6727 out_err:
6728 rtnl_unlock();
6729 return err;
6732 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
6733 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
6734 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
6735 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
6736 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
6737 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
6738 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
6739 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
6740 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
6743 static int parse_reg_rule(struct nlattr *tb[],
6744 struct ieee80211_reg_rule *reg_rule)
6746 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
6747 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
6749 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
6750 return -EINVAL;
6751 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
6752 return -EINVAL;
6753 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
6754 return -EINVAL;
6755 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6756 return -EINVAL;
6757 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6758 return -EINVAL;
6760 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
6762 freq_range->start_freq_khz =
6763 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
6764 freq_range->end_freq_khz =
6765 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
6766 freq_range->max_bandwidth_khz =
6767 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
6769 power_rule->max_eirp =
6770 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
6772 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
6773 power_rule->max_antenna_gain =
6774 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
6776 if (tb[NL80211_ATTR_DFS_CAC_TIME])
6777 reg_rule->dfs_cac_ms =
6778 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
6780 return 0;
6783 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
6785 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
6786 struct nlattr *nl_reg_rule;
6787 char *alpha2;
6788 int rem_reg_rules, r;
6789 u32 num_rules = 0, rule_idx = 0, size_of_regd;
6790 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
6791 struct ieee80211_regdomain *rd;
6793 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6794 return -EINVAL;
6796 if (!info->attrs[NL80211_ATTR_REG_RULES])
6797 return -EINVAL;
6799 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6801 if (info->attrs[NL80211_ATTR_DFS_REGION])
6802 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
6804 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6805 rem_reg_rules) {
6806 num_rules++;
6807 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
6808 return -EINVAL;
6811 if (!reg_is_valid_request(alpha2))
6812 return -EINVAL;
6814 size_of_regd = sizeof(struct ieee80211_regdomain) +
6815 num_rules * sizeof(struct ieee80211_reg_rule);
6817 rd = kzalloc(size_of_regd, GFP_KERNEL);
6818 if (!rd)
6819 return -ENOMEM;
6821 rd->n_reg_rules = num_rules;
6822 rd->alpha2[0] = alpha2[0];
6823 rd->alpha2[1] = alpha2[1];
6826 * Disable DFS master mode if the DFS region was
6827 * not supported or known on this kernel.
6829 if (reg_supported_dfs_region(dfs_region))
6830 rd->dfs_region = dfs_region;
6832 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6833 rem_reg_rules) {
6834 r = nla_parse_nested(tb, NL80211_REG_RULE_ATTR_MAX,
6835 nl_reg_rule, reg_rule_policy,
6836 info->extack);
6837 if (r)
6838 goto bad_reg;
6839 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
6840 if (r)
6841 goto bad_reg;
6843 rule_idx++;
6845 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
6846 r = -EINVAL;
6847 goto bad_reg;
6851 /* set_regdom takes ownership of rd */
6852 return set_regdom(rd, REGD_SOURCE_CRDA);
6853 bad_reg:
6854 kfree(rd);
6855 return r;
6857 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
6859 static int validate_scan_freqs(struct nlattr *freqs)
6861 struct nlattr *attr1, *attr2;
6862 int n_channels = 0, tmp1, tmp2;
6864 nla_for_each_nested(attr1, freqs, tmp1)
6865 if (nla_len(attr1) != sizeof(u32))
6866 return 0;
6868 nla_for_each_nested(attr1, freqs, tmp1) {
6869 n_channels++;
6871 * Some hardware has a limited channel list for
6872 * scanning, and it is pretty much nonsensical
6873 * to scan for a channel twice, so disallow that
6874 * and don't require drivers to check that the
6875 * channel list they get isn't longer than what
6876 * they can scan, as long as they can scan all
6877 * the channels they registered at once.
6879 nla_for_each_nested(attr2, freqs, tmp2)
6880 if (attr1 != attr2 &&
6881 nla_get_u32(attr1) == nla_get_u32(attr2))
6882 return 0;
6885 return n_channels;
6888 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
6890 return b < NUM_NL80211_BANDS && wiphy->bands[b];
6893 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
6894 struct cfg80211_bss_selection *bss_select)
6896 struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
6897 struct nlattr *nest;
6898 int err;
6899 bool found = false;
6900 int i;
6902 /* only process one nested attribute */
6903 nest = nla_data(nla);
6904 if (!nla_ok(nest, nla_len(nest)))
6905 return -EINVAL;
6907 err = nla_parse_nested(attr, NL80211_BSS_SELECT_ATTR_MAX, nest,
6908 nl80211_bss_select_policy, NULL);
6909 if (err)
6910 return err;
6912 /* only one attribute may be given */
6913 for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
6914 if (attr[i]) {
6915 if (found)
6916 return -EINVAL;
6917 found = true;
6921 bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
6923 if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
6924 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
6926 if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
6927 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
6928 bss_select->param.band_pref =
6929 nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
6930 if (!is_band_valid(wiphy, bss_select->param.band_pref))
6931 return -EINVAL;
6934 if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
6935 struct nl80211_bss_select_rssi_adjust *adj_param;
6937 adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
6938 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
6939 bss_select->param.adjust.band = adj_param->band;
6940 bss_select->param.adjust.delta = adj_param->delta;
6941 if (!is_band_valid(wiphy, bss_select->param.adjust.band))
6942 return -EINVAL;
6945 /* user-space did not provide behaviour attribute */
6946 if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
6947 return -EINVAL;
6949 if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
6950 return -EINVAL;
6952 return 0;
6955 static int nl80211_parse_random_mac(struct nlattr **attrs,
6956 u8 *mac_addr, u8 *mac_addr_mask)
6958 int i;
6960 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
6961 eth_zero_addr(mac_addr);
6962 eth_zero_addr(mac_addr_mask);
6963 mac_addr[0] = 0x2;
6964 mac_addr_mask[0] = 0x3;
6966 return 0;
6969 /* need both or none */
6970 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
6971 return -EINVAL;
6973 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
6974 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
6976 /* don't allow or configure an mcast address */
6977 if (!is_multicast_ether_addr(mac_addr_mask) ||
6978 is_multicast_ether_addr(mac_addr))
6979 return -EINVAL;
6982 * allow users to pass a MAC address that has bits set outside
6983 * of the mask, but don't bother drivers with having to deal
6984 * with such bits
6986 for (i = 0; i < ETH_ALEN; i++)
6987 mac_addr[i] &= mac_addr_mask[i];
6989 return 0;
6992 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
6994 ASSERT_WDEV_LOCK(wdev);
6996 if (!cfg80211_beaconing_iface_active(wdev))
6997 return true;
6999 if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7000 return true;
7002 return regulatory_pre_cac_allowed(wdev->wiphy);
7005 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7006 enum nl80211_ext_feature_index feat)
7008 if (!(flags & flag))
7009 return true;
7010 if (wiphy_ext_feature_isset(wiphy, feat))
7011 return true;
7012 return false;
7015 static int
7016 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7017 void *request, struct nlattr **attrs,
7018 bool is_sched_scan)
7020 u8 *mac_addr, *mac_addr_mask;
7021 u32 *flags;
7022 enum nl80211_feature_flags randomness_flag;
7024 if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7025 return 0;
7027 if (is_sched_scan) {
7028 struct cfg80211_sched_scan_request *req = request;
7030 randomness_flag = wdev ?
7031 NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7032 NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7033 flags = &req->flags;
7034 mac_addr = req->mac_addr;
7035 mac_addr_mask = req->mac_addr_mask;
7036 } else {
7037 struct cfg80211_scan_request *req = request;
7039 randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7040 flags = &req->flags;
7041 mac_addr = req->mac_addr;
7042 mac_addr_mask = req->mac_addr_mask;
7045 *flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7047 if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7048 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7049 !nl80211_check_scan_feat(wiphy, *flags,
7050 NL80211_SCAN_FLAG_LOW_SPAN,
7051 NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7052 !nl80211_check_scan_feat(wiphy, *flags,
7053 NL80211_SCAN_FLAG_LOW_POWER,
7054 NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7055 !nl80211_check_scan_feat(wiphy, *flags,
7056 NL80211_SCAN_FLAG_HIGH_ACCURACY,
7057 NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7058 !nl80211_check_scan_feat(wiphy, *flags,
7059 NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7060 NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7061 !nl80211_check_scan_feat(wiphy, *flags,
7062 NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7063 NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7064 !nl80211_check_scan_feat(wiphy, *flags,
7065 NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7066 NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7067 !nl80211_check_scan_feat(wiphy, *flags,
7068 NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7069 NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7070 !nl80211_check_scan_feat(wiphy, *flags,
7071 NL80211_SCAN_FLAG_RANDOM_SN,
7072 NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7073 !nl80211_check_scan_feat(wiphy, *flags,
7074 NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7075 NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7076 return -EOPNOTSUPP;
7078 if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7079 int err;
7081 if (!(wiphy->features & randomness_flag) ||
7082 (wdev && wdev->current_bss))
7083 return -EOPNOTSUPP;
7085 err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7086 if (err)
7087 return err;
7090 return 0;
7093 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7095 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7096 struct wireless_dev *wdev = info->user_ptr[1];
7097 struct cfg80211_scan_request *request;
7098 struct nlattr *attr;
7099 struct wiphy *wiphy;
7100 int err, tmp, n_ssids = 0, n_channels, i;
7101 size_t ie_len;
7103 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7104 return -EINVAL;
7106 wiphy = &rdev->wiphy;
7108 if (wdev->iftype == NL80211_IFTYPE_NAN)
7109 return -EOPNOTSUPP;
7111 if (!rdev->ops->scan)
7112 return -EOPNOTSUPP;
7114 if (rdev->scan_req || rdev->scan_msg) {
7115 err = -EBUSY;
7116 goto unlock;
7119 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7120 n_channels = validate_scan_freqs(
7121 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7122 if (!n_channels) {
7123 err = -EINVAL;
7124 goto unlock;
7126 } else {
7127 n_channels = ieee80211_get_num_supported_channels(wiphy);
7130 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7131 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7132 n_ssids++;
7134 if (n_ssids > wiphy->max_scan_ssids) {
7135 err = -EINVAL;
7136 goto unlock;
7139 if (info->attrs[NL80211_ATTR_IE])
7140 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7141 else
7142 ie_len = 0;
7144 if (ie_len > wiphy->max_scan_ie_len) {
7145 err = -EINVAL;
7146 goto unlock;
7149 request = kzalloc(sizeof(*request)
7150 + sizeof(*request->ssids) * n_ssids
7151 + sizeof(*request->channels) * n_channels
7152 + ie_len, GFP_KERNEL);
7153 if (!request) {
7154 err = -ENOMEM;
7155 goto unlock;
7158 if (n_ssids)
7159 request->ssids = (void *)&request->channels[n_channels];
7160 request->n_ssids = n_ssids;
7161 if (ie_len) {
7162 if (n_ssids)
7163 request->ie = (void *)(request->ssids + n_ssids);
7164 else
7165 request->ie = (void *)(request->channels + n_channels);
7168 i = 0;
7169 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7170 /* user specified, bail out if channel not found */
7171 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7172 struct ieee80211_channel *chan;
7174 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7176 if (!chan) {
7177 err = -EINVAL;
7178 goto out_free;
7181 /* ignore disabled channels */
7182 if (chan->flags & IEEE80211_CHAN_DISABLED)
7183 continue;
7185 request->channels[i] = chan;
7186 i++;
7188 } else {
7189 enum nl80211_band band;
7191 /* all channels */
7192 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7193 int j;
7195 if (!wiphy->bands[band])
7196 continue;
7197 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7198 struct ieee80211_channel *chan;
7200 chan = &wiphy->bands[band]->channels[j];
7202 if (chan->flags & IEEE80211_CHAN_DISABLED)
7203 continue;
7205 request->channels[i] = chan;
7206 i++;
7211 if (!i) {
7212 err = -EINVAL;
7213 goto out_free;
7216 request->n_channels = i;
7218 wdev_lock(wdev);
7219 if (!cfg80211_off_channel_oper_allowed(wdev)) {
7220 struct ieee80211_channel *chan;
7222 if (request->n_channels != 1) {
7223 wdev_unlock(wdev);
7224 err = -EBUSY;
7225 goto out_free;
7228 chan = request->channels[0];
7229 if (chan->center_freq != wdev->chandef.chan->center_freq) {
7230 wdev_unlock(wdev);
7231 err = -EBUSY;
7232 goto out_free;
7235 wdev_unlock(wdev);
7237 i = 0;
7238 if (n_ssids) {
7239 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7240 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7241 err = -EINVAL;
7242 goto out_free;
7244 request->ssids[i].ssid_len = nla_len(attr);
7245 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7246 i++;
7250 if (info->attrs[NL80211_ATTR_IE]) {
7251 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7252 memcpy((void *)request->ie,
7253 nla_data(info->attrs[NL80211_ATTR_IE]),
7254 request->ie_len);
7257 for (i = 0; i < NUM_NL80211_BANDS; i++)
7258 if (wiphy->bands[i])
7259 request->rates[i] =
7260 (1 << wiphy->bands[i]->n_bitrates) - 1;
7262 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7263 nla_for_each_nested(attr,
7264 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7265 tmp) {
7266 enum nl80211_band band = nla_type(attr);
7268 if (band < 0 || band >= NUM_NL80211_BANDS) {
7269 err = -EINVAL;
7270 goto out_free;
7273 if (!wiphy->bands[band])
7274 continue;
7276 err = ieee80211_get_ratemask(wiphy->bands[band],
7277 nla_data(attr),
7278 nla_len(attr),
7279 &request->rates[band]);
7280 if (err)
7281 goto out_free;
7285 if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7286 if (!wiphy_ext_feature_isset(wiphy,
7287 NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7288 err = -EOPNOTSUPP;
7289 goto out_free;
7292 request->duration =
7293 nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7294 request->duration_mandatory =
7295 nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7298 err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7299 false);
7300 if (err)
7301 goto out_free;
7303 request->no_cck =
7304 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7306 /* Initial implementation used NL80211_ATTR_MAC to set the specific
7307 * BSSID to scan for. This was problematic because that same attribute
7308 * was already used for another purpose (local random MAC address). The
7309 * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7310 * compatibility with older userspace components, also use the
7311 * NL80211_ATTR_MAC value here if it can be determined to be used for
7312 * the specific BSSID use case instead of the random MAC address
7313 * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7315 if (info->attrs[NL80211_ATTR_BSSID])
7316 memcpy(request->bssid,
7317 nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7318 else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7319 info->attrs[NL80211_ATTR_MAC])
7320 memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7321 ETH_ALEN);
7322 else
7323 eth_broadcast_addr(request->bssid);
7325 request->wdev = wdev;
7326 request->wiphy = &rdev->wiphy;
7327 request->scan_start = jiffies;
7329 rdev->scan_req = request;
7330 err = rdev_scan(rdev, request);
7332 if (!err) {
7333 nl80211_send_scan_start(rdev, wdev);
7334 if (wdev->netdev)
7335 dev_hold(wdev->netdev);
7336 } else {
7337 out_free:
7338 rdev->scan_req = NULL;
7339 kfree(request);
7342 unlock:
7343 return err;
7346 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7348 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7349 struct wireless_dev *wdev = info->user_ptr[1];
7351 if (!rdev->ops->abort_scan)
7352 return -EOPNOTSUPP;
7354 if (rdev->scan_msg)
7355 return 0;
7357 if (!rdev->scan_req)
7358 return -ENOENT;
7360 rdev_abort_scan(rdev, wdev);
7361 return 0;
7364 static int
7365 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7366 struct cfg80211_sched_scan_request *request,
7367 struct nlattr **attrs)
7369 int tmp, err, i = 0;
7370 struct nlattr *attr;
7372 if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7373 u32 interval;
7376 * If scan plans are not specified,
7377 * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7378 * case one scan plan will be set with the specified scan
7379 * interval and infinite number of iterations.
7381 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7382 if (!interval)
7383 return -EINVAL;
7385 request->scan_plans[0].interval =
7386 DIV_ROUND_UP(interval, MSEC_PER_SEC);
7387 if (!request->scan_plans[0].interval)
7388 return -EINVAL;
7390 if (request->scan_plans[0].interval >
7391 wiphy->max_sched_scan_plan_interval)
7392 request->scan_plans[0].interval =
7393 wiphy->max_sched_scan_plan_interval;
7395 return 0;
7398 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7399 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7401 if (WARN_ON(i >= n_plans))
7402 return -EINVAL;
7404 err = nla_parse_nested(plan, NL80211_SCHED_SCAN_PLAN_MAX,
7405 attr, nl80211_plan_policy, NULL);
7406 if (err)
7407 return err;
7409 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7410 return -EINVAL;
7412 request->scan_plans[i].interval =
7413 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7414 if (!request->scan_plans[i].interval ||
7415 request->scan_plans[i].interval >
7416 wiphy->max_sched_scan_plan_interval)
7417 return -EINVAL;
7419 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7420 request->scan_plans[i].iterations =
7421 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7422 if (!request->scan_plans[i].iterations ||
7423 (request->scan_plans[i].iterations >
7424 wiphy->max_sched_scan_plan_iterations))
7425 return -EINVAL;
7426 } else if (i < n_plans - 1) {
7428 * All scan plans but the last one must specify
7429 * a finite number of iterations
7431 return -EINVAL;
7434 i++;
7438 * The last scan plan must not specify the number of
7439 * iterations, it is supposed to run infinitely
7441 if (request->scan_plans[n_plans - 1].iterations)
7442 return -EINVAL;
7444 return 0;
7447 static struct cfg80211_sched_scan_request *
7448 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7449 struct nlattr **attrs, int max_match_sets)
7451 struct cfg80211_sched_scan_request *request;
7452 struct nlattr *attr;
7453 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7454 enum nl80211_band band;
7455 size_t ie_len;
7456 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7457 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7459 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
7460 return ERR_PTR(-EINVAL);
7462 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7463 n_channels = validate_scan_freqs(
7464 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7465 if (!n_channels)
7466 return ERR_PTR(-EINVAL);
7467 } else {
7468 n_channels = ieee80211_get_num_supported_channels(wiphy);
7471 if (attrs[NL80211_ATTR_SCAN_SSIDS])
7472 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7473 tmp)
7474 n_ssids++;
7476 if (n_ssids > wiphy->max_sched_scan_ssids)
7477 return ERR_PTR(-EINVAL);
7480 * First, count the number of 'real' matchsets. Due to an issue with
7481 * the old implementation, matchsets containing only the RSSI attribute
7482 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7483 * RSSI for all matchsets, rather than their own matchset for reporting
7484 * all APs with a strong RSSI. This is needed to be compatible with
7485 * older userspace that treated a matchset with only the RSSI as the
7486 * global RSSI for all other matchsets - if there are other matchsets.
7488 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7489 nla_for_each_nested(attr,
7490 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7491 tmp) {
7492 struct nlattr *rssi;
7494 err = nla_parse_nested(tb,
7495 NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7496 attr, nl80211_match_policy,
7497 NULL);
7498 if (err)
7499 return ERR_PTR(err);
7501 /* SSID and BSSID are mutually exclusive */
7502 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7503 tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7504 return ERR_PTR(-EINVAL);
7506 /* add other standalone attributes here */
7507 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7508 tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7509 n_match_sets++;
7510 continue;
7512 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7513 if (rssi)
7514 default_match_rssi = nla_get_s32(rssi);
7518 /* However, if there's no other matchset, add the RSSI one */
7519 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7520 n_match_sets = 1;
7522 if (n_match_sets > max_match_sets)
7523 return ERR_PTR(-EINVAL);
7525 if (attrs[NL80211_ATTR_IE])
7526 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7527 else
7528 ie_len = 0;
7530 if (ie_len > wiphy->max_sched_scan_ie_len)
7531 return ERR_PTR(-EINVAL);
7533 if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7535 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7536 * each scan plan already specifies its own interval
7538 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7539 return ERR_PTR(-EINVAL);
7541 nla_for_each_nested(attr,
7542 attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7543 n_plans++;
7544 } else {
7546 * The scan interval attribute is kept for backward
7547 * compatibility. If no scan plans are specified and sched scan
7548 * interval is specified, one scan plan will be set with this
7549 * scan interval and infinite number of iterations.
7551 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7552 return ERR_PTR(-EINVAL);
7554 n_plans = 1;
7557 if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7558 return ERR_PTR(-EINVAL);
7560 if (!wiphy_ext_feature_isset(
7561 wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7562 (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7563 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7564 return ERR_PTR(-EINVAL);
7566 request = kzalloc(sizeof(*request)
7567 + sizeof(*request->ssids) * n_ssids
7568 + sizeof(*request->match_sets) * n_match_sets
7569 + sizeof(*request->scan_plans) * n_plans
7570 + sizeof(*request->channels) * n_channels
7571 + ie_len, GFP_KERNEL);
7572 if (!request)
7573 return ERR_PTR(-ENOMEM);
7575 if (n_ssids)
7576 request->ssids = (void *)&request->channels[n_channels];
7577 request->n_ssids = n_ssids;
7578 if (ie_len) {
7579 if (n_ssids)
7580 request->ie = (void *)(request->ssids + n_ssids);
7581 else
7582 request->ie = (void *)(request->channels + n_channels);
7585 if (n_match_sets) {
7586 if (request->ie)
7587 request->match_sets = (void *)(request->ie + ie_len);
7588 else if (n_ssids)
7589 request->match_sets =
7590 (void *)(request->ssids + n_ssids);
7591 else
7592 request->match_sets =
7593 (void *)(request->channels + n_channels);
7595 request->n_match_sets = n_match_sets;
7597 if (n_match_sets)
7598 request->scan_plans = (void *)(request->match_sets +
7599 n_match_sets);
7600 else if (request->ie)
7601 request->scan_plans = (void *)(request->ie + ie_len);
7602 else if (n_ssids)
7603 request->scan_plans = (void *)(request->ssids + n_ssids);
7604 else
7605 request->scan_plans = (void *)(request->channels + n_channels);
7607 request->n_scan_plans = n_plans;
7609 i = 0;
7610 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7611 /* user specified, bail out if channel not found */
7612 nla_for_each_nested(attr,
7613 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
7614 tmp) {
7615 struct ieee80211_channel *chan;
7617 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7619 if (!chan) {
7620 err = -EINVAL;
7621 goto out_free;
7624 /* ignore disabled channels */
7625 if (chan->flags & IEEE80211_CHAN_DISABLED)
7626 continue;
7628 request->channels[i] = chan;
7629 i++;
7631 } else {
7632 /* all channels */
7633 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7634 int j;
7636 if (!wiphy->bands[band])
7637 continue;
7638 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7639 struct ieee80211_channel *chan;
7641 chan = &wiphy->bands[band]->channels[j];
7643 if (chan->flags & IEEE80211_CHAN_DISABLED)
7644 continue;
7646 request->channels[i] = chan;
7647 i++;
7652 if (!i) {
7653 err = -EINVAL;
7654 goto out_free;
7657 request->n_channels = i;
7659 i = 0;
7660 if (n_ssids) {
7661 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7662 tmp) {
7663 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7664 err = -EINVAL;
7665 goto out_free;
7667 request->ssids[i].ssid_len = nla_len(attr);
7668 memcpy(request->ssids[i].ssid, nla_data(attr),
7669 nla_len(attr));
7670 i++;
7674 i = 0;
7675 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7676 nla_for_each_nested(attr,
7677 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7678 tmp) {
7679 struct nlattr *ssid, *bssid, *rssi;
7681 err = nla_parse_nested(tb,
7682 NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7683 attr, nl80211_match_policy,
7684 NULL);
7685 if (err)
7686 goto out_free;
7687 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
7688 bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
7689 if (ssid || bssid) {
7690 if (WARN_ON(i >= n_match_sets)) {
7691 /* this indicates a programming error,
7692 * the loop above should have verified
7693 * things properly
7695 err = -EINVAL;
7696 goto out_free;
7699 if (ssid) {
7700 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
7701 err = -EINVAL;
7702 goto out_free;
7704 memcpy(request->match_sets[i].ssid.ssid,
7705 nla_data(ssid), nla_len(ssid));
7706 request->match_sets[i].ssid.ssid_len =
7707 nla_len(ssid);
7709 if (bssid) {
7710 if (nla_len(bssid) != ETH_ALEN) {
7711 err = -EINVAL;
7712 goto out_free;
7714 memcpy(request->match_sets[i].bssid,
7715 nla_data(bssid), ETH_ALEN);
7718 /* special attribute - old implementation w/a */
7719 request->match_sets[i].rssi_thold =
7720 default_match_rssi;
7721 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7722 if (rssi)
7723 request->match_sets[i].rssi_thold =
7724 nla_get_s32(rssi);
7726 i++;
7729 /* there was no other matchset, so the RSSI one is alone */
7730 if (i == 0 && n_match_sets)
7731 request->match_sets[0].rssi_thold = default_match_rssi;
7733 request->min_rssi_thold = INT_MAX;
7734 for (i = 0; i < n_match_sets; i++)
7735 request->min_rssi_thold =
7736 min(request->match_sets[i].rssi_thold,
7737 request->min_rssi_thold);
7738 } else {
7739 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
7742 if (ie_len) {
7743 request->ie_len = ie_len;
7744 memcpy((void *)request->ie,
7745 nla_data(attrs[NL80211_ATTR_IE]),
7746 request->ie_len);
7749 err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
7750 if (err)
7751 goto out_free;
7753 if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
7754 request->delay =
7755 nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
7757 if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
7758 request->relative_rssi = nla_get_s8(
7759 attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
7760 request->relative_rssi_set = true;
7763 if (request->relative_rssi_set &&
7764 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
7765 struct nl80211_bss_select_rssi_adjust *rssi_adjust;
7767 rssi_adjust = nla_data(
7768 attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
7769 request->rssi_adjust.band = rssi_adjust->band;
7770 request->rssi_adjust.delta = rssi_adjust->delta;
7771 if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
7772 err = -EINVAL;
7773 goto out_free;
7777 err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
7778 if (err)
7779 goto out_free;
7781 request->scan_start = jiffies;
7783 return request;
7785 out_free:
7786 kfree(request);
7787 return ERR_PTR(err);
7790 static int nl80211_start_sched_scan(struct sk_buff *skb,
7791 struct genl_info *info)
7793 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7794 struct net_device *dev = info->user_ptr[1];
7795 struct wireless_dev *wdev = dev->ieee80211_ptr;
7796 struct cfg80211_sched_scan_request *sched_scan_req;
7797 bool want_multi;
7798 int err;
7800 if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
7801 return -EOPNOTSUPP;
7803 want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
7804 err = cfg80211_sched_scan_req_possible(rdev, want_multi);
7805 if (err)
7806 return err;
7808 sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
7809 info->attrs,
7810 rdev->wiphy.max_match_sets);
7812 err = PTR_ERR_OR_ZERO(sched_scan_req);
7813 if (err)
7814 goto out_err;
7816 /* leave request id zero for legacy request
7817 * or if driver does not support multi-scheduled scan
7819 if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
7820 while (!sched_scan_req->reqid)
7821 sched_scan_req->reqid = rdev->wiphy.cookie_counter++;
7824 err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
7825 if (err)
7826 goto out_free;
7828 sched_scan_req->dev = dev;
7829 sched_scan_req->wiphy = &rdev->wiphy;
7831 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
7832 sched_scan_req->owner_nlportid = info->snd_portid;
7834 cfg80211_add_sched_scan_req(rdev, sched_scan_req);
7836 nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
7837 return 0;
7839 out_free:
7840 kfree(sched_scan_req);
7841 out_err:
7842 return err;
7845 static int nl80211_stop_sched_scan(struct sk_buff *skb,
7846 struct genl_info *info)
7848 struct cfg80211_sched_scan_request *req;
7849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7850 u64 cookie;
7852 if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
7853 return -EOPNOTSUPP;
7855 if (info->attrs[NL80211_ATTR_COOKIE]) {
7856 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7857 return __cfg80211_stop_sched_scan(rdev, cookie, false);
7860 req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
7861 struct cfg80211_sched_scan_request,
7862 list);
7863 if (!req || req->reqid ||
7864 (req->owner_nlportid &&
7865 req->owner_nlportid != info->snd_portid))
7866 return -ENOENT;
7868 return cfg80211_stop_sched_scan_req(rdev, req, false);
7871 static int nl80211_start_radar_detection(struct sk_buff *skb,
7872 struct genl_info *info)
7874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7875 struct net_device *dev = info->user_ptr[1];
7876 struct wireless_dev *wdev = dev->ieee80211_ptr;
7877 struct wiphy *wiphy = wdev->wiphy;
7878 struct cfg80211_chan_def chandef;
7879 enum nl80211_dfs_regions dfs_region;
7880 unsigned int cac_time_ms;
7881 int err;
7883 dfs_region = reg_get_dfs_region(wiphy);
7884 if (dfs_region == NL80211_DFS_UNSET)
7885 return -EINVAL;
7887 err = nl80211_parse_chandef(rdev, info, &chandef);
7888 if (err)
7889 return err;
7891 if (netif_carrier_ok(dev))
7892 return -EBUSY;
7894 if (wdev->cac_started)
7895 return -EBUSY;
7897 err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
7898 if (err < 0)
7899 return err;
7901 if (err == 0)
7902 return -EINVAL;
7904 if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
7905 return -EINVAL;
7907 /* CAC start is offloaded to HW and can't be started manually */
7908 if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
7909 return -EOPNOTSUPP;
7911 if (!rdev->ops->start_radar_detection)
7912 return -EOPNOTSUPP;
7914 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
7915 if (WARN_ON(!cac_time_ms))
7916 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
7918 err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
7919 if (!err) {
7920 wdev->chandef = chandef;
7921 wdev->cac_started = true;
7922 wdev->cac_start_time = jiffies;
7923 wdev->cac_time_ms = cac_time_ms;
7925 return err;
7928 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
7930 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7931 struct net_device *dev = info->user_ptr[1];
7932 struct wireless_dev *wdev = dev->ieee80211_ptr;
7933 struct cfg80211_csa_settings params;
7934 /* csa_attrs is defined static to avoid waste of stack size - this
7935 * function is called under RTNL lock, so this should not be a problem.
7937 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
7938 int err;
7939 bool need_new_beacon = false;
7940 bool need_handle_dfs_flag = true;
7941 int len, i;
7942 u32 cs_count;
7944 if (!rdev->ops->channel_switch ||
7945 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
7946 return -EOPNOTSUPP;
7948 switch (dev->ieee80211_ptr->iftype) {
7949 case NL80211_IFTYPE_AP:
7950 case NL80211_IFTYPE_P2P_GO:
7951 need_new_beacon = true;
7952 /* For all modes except AP the handle_dfs flag needs to be
7953 * supplied to tell the kernel that userspace will handle radar
7954 * events when they happen. Otherwise a switch to a channel
7955 * requiring DFS will be rejected.
7957 need_handle_dfs_flag = false;
7959 /* useless if AP is not running */
7960 if (!wdev->beacon_interval)
7961 return -ENOTCONN;
7962 break;
7963 case NL80211_IFTYPE_ADHOC:
7964 if (!wdev->ssid_len)
7965 return -ENOTCONN;
7966 break;
7967 case NL80211_IFTYPE_MESH_POINT:
7968 if (!wdev->mesh_id_len)
7969 return -ENOTCONN;
7970 break;
7971 default:
7972 return -EOPNOTSUPP;
7975 memset(&params, 0, sizeof(params));
7977 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7978 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
7979 return -EINVAL;
7981 /* only important for AP, IBSS and mesh create IEs internally */
7982 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
7983 return -EINVAL;
7985 /* Even though the attribute is u32, the specification says
7986 * u8, so let's make sure we don't overflow.
7988 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
7989 if (cs_count > 255)
7990 return -EINVAL;
7992 params.count = cs_count;
7994 if (!need_new_beacon)
7995 goto skip_beacons;
7997 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
7998 if (err)
7999 return err;
8001 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
8002 info->attrs[NL80211_ATTR_CSA_IES],
8003 nl80211_policy, info->extack);
8004 if (err)
8005 return err;
8007 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
8008 if (err)
8009 return err;
8011 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8012 return -EINVAL;
8014 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8015 if (!len || (len % sizeof(u16)))
8016 return -EINVAL;
8018 params.n_counter_offsets_beacon = len / sizeof(u16);
8019 if (rdev->wiphy.max_num_csa_counters &&
8020 (params.n_counter_offsets_beacon >
8021 rdev->wiphy.max_num_csa_counters))
8022 return -EINVAL;
8024 params.counter_offsets_beacon =
8025 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8027 /* sanity checks - counters should fit and be the same */
8028 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8029 u16 offset = params.counter_offsets_beacon[i];
8031 if (offset >= params.beacon_csa.tail_len)
8032 return -EINVAL;
8034 if (params.beacon_csa.tail[offset] != params.count)
8035 return -EINVAL;
8038 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8039 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8040 if (!len || (len % sizeof(u16)))
8041 return -EINVAL;
8043 params.n_counter_offsets_presp = len / sizeof(u16);
8044 if (rdev->wiphy.max_num_csa_counters &&
8045 (params.n_counter_offsets_presp >
8046 rdev->wiphy.max_num_csa_counters))
8047 return -EINVAL;
8049 params.counter_offsets_presp =
8050 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8052 /* sanity checks - counters should fit and be the same */
8053 for (i = 0; i < params.n_counter_offsets_presp; i++) {
8054 u16 offset = params.counter_offsets_presp[i];
8056 if (offset >= params.beacon_csa.probe_resp_len)
8057 return -EINVAL;
8059 if (params.beacon_csa.probe_resp[offset] !=
8060 params.count)
8061 return -EINVAL;
8065 skip_beacons:
8066 err = nl80211_parse_chandef(rdev, info, &params.chandef);
8067 if (err)
8068 return err;
8070 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8071 wdev->iftype))
8072 return -EINVAL;
8074 err = cfg80211_chandef_dfs_required(wdev->wiphy,
8075 &params.chandef,
8076 wdev->iftype);
8077 if (err < 0)
8078 return err;
8080 if (err > 0) {
8081 params.radar_required = true;
8082 if (need_handle_dfs_flag &&
8083 !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8084 return -EINVAL;
8088 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8089 params.block_tx = true;
8091 wdev_lock(wdev);
8092 err = rdev_channel_switch(rdev, dev, &params);
8093 wdev_unlock(wdev);
8095 return err;
8098 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8099 u32 seq, int flags,
8100 struct cfg80211_registered_device *rdev,
8101 struct wireless_dev *wdev,
8102 struct cfg80211_internal_bss *intbss)
8104 struct cfg80211_bss *res = &intbss->pub;
8105 const struct cfg80211_bss_ies *ies;
8106 void *hdr;
8107 struct nlattr *bss;
8109 ASSERT_WDEV_LOCK(wdev);
8111 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8112 NL80211_CMD_NEW_SCAN_RESULTS);
8113 if (!hdr)
8114 return -1;
8116 genl_dump_check_consistent(cb, hdr);
8118 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8119 goto nla_put_failure;
8120 if (wdev->netdev &&
8121 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8122 goto nla_put_failure;
8123 if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8124 NL80211_ATTR_PAD))
8125 goto nla_put_failure;
8127 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
8128 if (!bss)
8129 goto nla_put_failure;
8130 if ((!is_zero_ether_addr(res->bssid) &&
8131 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8132 goto nla_put_failure;
8134 rcu_read_lock();
8135 /* indicate whether we have probe response data or not */
8136 if (rcu_access_pointer(res->proberesp_ies) &&
8137 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8138 goto fail_unlock_rcu;
8140 /* this pointer prefers to be pointed to probe response data
8141 * but is always valid
8143 ies = rcu_dereference(res->ies);
8144 if (ies) {
8145 if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8146 NL80211_BSS_PAD))
8147 goto fail_unlock_rcu;
8148 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8149 ies->len, ies->data))
8150 goto fail_unlock_rcu;
8153 /* and this pointer is always (unless driver didn't know) beacon data */
8154 ies = rcu_dereference(res->beacon_ies);
8155 if (ies && ies->from_beacon) {
8156 if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8157 NL80211_BSS_PAD))
8158 goto fail_unlock_rcu;
8159 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8160 ies->len, ies->data))
8161 goto fail_unlock_rcu;
8163 rcu_read_unlock();
8165 if (res->beacon_interval &&
8166 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8167 goto nla_put_failure;
8168 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8169 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8170 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8171 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8172 jiffies_to_msecs(jiffies - intbss->ts)))
8173 goto nla_put_failure;
8175 if (intbss->parent_tsf &&
8176 (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8177 intbss->parent_tsf, NL80211_BSS_PAD) ||
8178 nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8179 intbss->parent_bssid)))
8180 goto nla_put_failure;
8182 if (intbss->ts_boottime &&
8183 nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8184 intbss->ts_boottime, NL80211_BSS_PAD))
8185 goto nla_put_failure;
8187 if (!nl80211_put_signal(msg, intbss->pub.chains,
8188 intbss->pub.chain_signal,
8189 NL80211_BSS_CHAIN_SIGNAL))
8190 goto nla_put_failure;
8192 switch (rdev->wiphy.signal_type) {
8193 case CFG80211_SIGNAL_TYPE_MBM:
8194 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8195 goto nla_put_failure;
8196 break;
8197 case CFG80211_SIGNAL_TYPE_UNSPEC:
8198 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8199 goto nla_put_failure;
8200 break;
8201 default:
8202 break;
8205 switch (wdev->iftype) {
8206 case NL80211_IFTYPE_P2P_CLIENT:
8207 case NL80211_IFTYPE_STATION:
8208 if (intbss == wdev->current_bss &&
8209 nla_put_u32(msg, NL80211_BSS_STATUS,
8210 NL80211_BSS_STATUS_ASSOCIATED))
8211 goto nla_put_failure;
8212 break;
8213 case NL80211_IFTYPE_ADHOC:
8214 if (intbss == wdev->current_bss &&
8215 nla_put_u32(msg, NL80211_BSS_STATUS,
8216 NL80211_BSS_STATUS_IBSS_JOINED))
8217 goto nla_put_failure;
8218 break;
8219 default:
8220 break;
8223 nla_nest_end(msg, bss);
8225 genlmsg_end(msg, hdr);
8226 return 0;
8228 fail_unlock_rcu:
8229 rcu_read_unlock();
8230 nla_put_failure:
8231 genlmsg_cancel(msg, hdr);
8232 return -EMSGSIZE;
8235 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8237 struct cfg80211_registered_device *rdev;
8238 struct cfg80211_internal_bss *scan;
8239 struct wireless_dev *wdev;
8240 int start = cb->args[2], idx = 0;
8241 int err;
8243 rtnl_lock();
8244 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
8245 if (err) {
8246 rtnl_unlock();
8247 return err;
8250 wdev_lock(wdev);
8251 spin_lock_bh(&rdev->bss_lock);
8254 * dump_scan will be called multiple times to break up the scan results
8255 * into multiple messages. It is unlikely that any more bss-es will be
8256 * expired after the first call, so only call only call this on the
8257 * first dump_scan invocation.
8259 if (start == 0)
8260 cfg80211_bss_expire(rdev);
8262 cb->seq = rdev->bss_generation;
8264 list_for_each_entry(scan, &rdev->bss_list, list) {
8265 if (++idx <= start)
8266 continue;
8267 if (nl80211_send_bss(skb, cb,
8268 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8269 rdev, wdev, scan) < 0) {
8270 idx--;
8271 break;
8275 spin_unlock_bh(&rdev->bss_lock);
8276 wdev_unlock(wdev);
8278 cb->args[2] = idx;
8279 rtnl_unlock();
8281 return skb->len;
8284 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8285 int flags, struct net_device *dev,
8286 bool allow_radio_stats,
8287 struct survey_info *survey)
8289 void *hdr;
8290 struct nlattr *infoattr;
8292 /* skip radio stats if userspace didn't request them */
8293 if (!survey->channel && !allow_radio_stats)
8294 return 0;
8296 hdr = nl80211hdr_put(msg, portid, seq, flags,
8297 NL80211_CMD_NEW_SURVEY_RESULTS);
8298 if (!hdr)
8299 return -ENOMEM;
8301 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8302 goto nla_put_failure;
8304 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
8305 if (!infoattr)
8306 goto nla_put_failure;
8308 if (survey->channel &&
8309 nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8310 survey->channel->center_freq))
8311 goto nla_put_failure;
8313 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8314 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8315 goto nla_put_failure;
8316 if ((survey->filled & SURVEY_INFO_IN_USE) &&
8317 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8318 goto nla_put_failure;
8319 if ((survey->filled & SURVEY_INFO_TIME) &&
8320 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8321 survey->time, NL80211_SURVEY_INFO_PAD))
8322 goto nla_put_failure;
8323 if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8324 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8325 survey->time_busy, NL80211_SURVEY_INFO_PAD))
8326 goto nla_put_failure;
8327 if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8328 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8329 survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8330 goto nla_put_failure;
8331 if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8332 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8333 survey->time_rx, NL80211_SURVEY_INFO_PAD))
8334 goto nla_put_failure;
8335 if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8336 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8337 survey->time_tx, NL80211_SURVEY_INFO_PAD))
8338 goto nla_put_failure;
8339 if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8340 nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8341 survey->time_scan, NL80211_SURVEY_INFO_PAD))
8342 goto nla_put_failure;
8344 nla_nest_end(msg, infoattr);
8346 genlmsg_end(msg, hdr);
8347 return 0;
8349 nla_put_failure:
8350 genlmsg_cancel(msg, hdr);
8351 return -EMSGSIZE;
8354 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8356 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8357 struct survey_info survey;
8358 struct cfg80211_registered_device *rdev;
8359 struct wireless_dev *wdev;
8360 int survey_idx = cb->args[2];
8361 int res;
8362 bool radio_stats;
8364 rtnl_lock();
8365 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
8366 if (res)
8367 goto out_err;
8369 /* prepare_wdev_dump parsed the attributes */
8370 radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8372 if (!wdev->netdev) {
8373 res = -EINVAL;
8374 goto out_err;
8377 if (!rdev->ops->dump_survey) {
8378 res = -EOPNOTSUPP;
8379 goto out_err;
8382 while (1) {
8383 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8384 if (res == -ENOENT)
8385 break;
8386 if (res)
8387 goto out_err;
8389 /* don't send disabled channels, but do send non-channel data */
8390 if (survey.channel &&
8391 survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8392 survey_idx++;
8393 continue;
8396 if (nl80211_send_survey(skb,
8397 NETLINK_CB(cb->skb).portid,
8398 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8399 wdev->netdev, radio_stats, &survey) < 0)
8400 goto out;
8401 survey_idx++;
8404 out:
8405 cb->args[2] = survey_idx;
8406 res = skb->len;
8407 out_err:
8408 rtnl_unlock();
8409 return res;
8412 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8414 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8415 NL80211_WPA_VERSION_2));
8418 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8421 struct net_device *dev = info->user_ptr[1];
8422 struct ieee80211_channel *chan;
8423 const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8424 int err, ssid_len, ie_len = 0, auth_data_len = 0;
8425 enum nl80211_auth_type auth_type;
8426 struct key_parse key;
8427 bool local_state_change;
8429 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8430 return -EINVAL;
8432 if (!info->attrs[NL80211_ATTR_MAC])
8433 return -EINVAL;
8435 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8436 return -EINVAL;
8438 if (!info->attrs[NL80211_ATTR_SSID])
8439 return -EINVAL;
8441 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8442 return -EINVAL;
8444 err = nl80211_parse_key(info, &key);
8445 if (err)
8446 return err;
8448 if (key.idx >= 0) {
8449 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8450 return -EINVAL;
8451 if (!key.p.key || !key.p.key_len)
8452 return -EINVAL;
8453 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8454 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8455 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8456 key.p.key_len != WLAN_KEY_LEN_WEP104))
8457 return -EINVAL;
8458 if (key.idx > 3)
8459 return -EINVAL;
8460 } else {
8461 key.p.key_len = 0;
8462 key.p.key = NULL;
8465 if (key.idx >= 0) {
8466 int i;
8467 bool ok = false;
8469 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8470 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8471 ok = true;
8472 break;
8475 if (!ok)
8476 return -EINVAL;
8479 if (!rdev->ops->auth)
8480 return -EOPNOTSUPP;
8482 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8483 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8484 return -EOPNOTSUPP;
8486 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8487 chan = nl80211_get_valid_chan(&rdev->wiphy,
8488 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8489 if (!chan)
8490 return -EINVAL;
8492 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8493 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8495 if (info->attrs[NL80211_ATTR_IE]) {
8496 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8497 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8500 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8501 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
8502 return -EINVAL;
8504 if ((auth_type == NL80211_AUTHTYPE_SAE ||
8505 auth_type == NL80211_AUTHTYPE_FILS_SK ||
8506 auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
8507 auth_type == NL80211_AUTHTYPE_FILS_PK) &&
8508 !info->attrs[NL80211_ATTR_AUTH_DATA])
8509 return -EINVAL;
8511 if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
8512 if (auth_type != NL80211_AUTHTYPE_SAE &&
8513 auth_type != NL80211_AUTHTYPE_FILS_SK &&
8514 auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8515 auth_type != NL80211_AUTHTYPE_FILS_PK)
8516 return -EINVAL;
8517 auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8518 auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
8519 /* need to include at least Auth Transaction and Status Code */
8520 if (auth_data_len < 4)
8521 return -EINVAL;
8524 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8527 * Since we no longer track auth state, ignore
8528 * requests to only change local state.
8530 if (local_state_change)
8531 return 0;
8533 wdev_lock(dev->ieee80211_ptr);
8534 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8535 ssid, ssid_len, ie, ie_len,
8536 key.p.key, key.p.key_len, key.idx,
8537 auth_data, auth_data_len);
8538 wdev_unlock(dev->ieee80211_ptr);
8539 return err;
8542 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
8543 struct genl_info *info)
8545 if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8546 GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
8547 return -EINVAL;
8550 if (!rdev->ops->tx_control_port ||
8551 !wiphy_ext_feature_isset(&rdev->wiphy,
8552 NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
8553 return -EOPNOTSUPP;
8555 return 0;
8558 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8559 struct genl_info *info,
8560 struct cfg80211_crypto_settings *settings,
8561 int cipher_limit)
8563 memset(settings, 0, sizeof(*settings));
8565 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
8567 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
8568 u16 proto;
8570 proto = nla_get_u16(
8571 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
8572 settings->control_port_ethertype = cpu_to_be16(proto);
8573 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
8574 proto != ETH_P_PAE)
8575 return -EINVAL;
8576 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
8577 settings->control_port_no_encrypt = true;
8578 } else
8579 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
8581 if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
8582 int r = validate_pae_over_nl80211(rdev, info);
8584 if (r < 0)
8585 return r;
8587 settings->control_port_over_nl80211 = true;
8590 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
8591 void *data;
8592 int len, i;
8594 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8595 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8596 settings->n_ciphers_pairwise = len / sizeof(u32);
8598 if (len % sizeof(u32))
8599 return -EINVAL;
8601 if (settings->n_ciphers_pairwise > cipher_limit)
8602 return -EINVAL;
8604 memcpy(settings->ciphers_pairwise, data, len);
8606 for (i = 0; i < settings->n_ciphers_pairwise; i++)
8607 if (!cfg80211_supported_cipher_suite(
8608 &rdev->wiphy,
8609 settings->ciphers_pairwise[i]))
8610 return -EINVAL;
8613 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
8614 settings->cipher_group =
8615 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
8616 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
8617 settings->cipher_group))
8618 return -EINVAL;
8621 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
8622 settings->wpa_versions =
8623 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
8624 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
8625 return -EINVAL;
8628 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
8629 void *data;
8630 int len;
8632 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
8633 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
8634 settings->n_akm_suites = len / sizeof(u32);
8636 if (len % sizeof(u32))
8637 return -EINVAL;
8639 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
8640 return -EINVAL;
8642 memcpy(settings->akm_suites, data, len);
8645 if (info->attrs[NL80211_ATTR_PMK]) {
8646 if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
8647 return -EINVAL;
8648 if (!wiphy_ext_feature_isset(&rdev->wiphy,
8649 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
8650 return -EINVAL;
8651 settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
8654 return 0;
8657 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
8659 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8660 struct net_device *dev = info->user_ptr[1];
8661 struct ieee80211_channel *chan;
8662 struct cfg80211_assoc_request req = {};
8663 const u8 *bssid, *ssid;
8664 int err, ssid_len = 0;
8666 if (dev->ieee80211_ptr->conn_owner_nlportid &&
8667 dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
8668 return -EPERM;
8670 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8671 return -EINVAL;
8673 if (!info->attrs[NL80211_ATTR_MAC] ||
8674 !info->attrs[NL80211_ATTR_SSID] ||
8675 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
8676 return -EINVAL;
8678 if (!rdev->ops->assoc)
8679 return -EOPNOTSUPP;
8681 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8682 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8683 return -EOPNOTSUPP;
8685 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8687 chan = nl80211_get_valid_chan(&rdev->wiphy,
8688 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8689 if (!chan)
8690 return -EINVAL;
8692 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8693 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8695 if (info->attrs[NL80211_ATTR_IE]) {
8696 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8697 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8700 if (info->attrs[NL80211_ATTR_USE_MFP]) {
8701 enum nl80211_mfp mfp =
8702 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8703 if (mfp == NL80211_MFP_REQUIRED)
8704 req.use_mfp = true;
8705 else if (mfp != NL80211_MFP_NO)
8706 return -EINVAL;
8709 if (info->attrs[NL80211_ATTR_PREV_BSSID])
8710 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8712 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
8713 req.flags |= ASSOC_REQ_DISABLE_HT;
8715 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8716 memcpy(&req.ht_capa_mask,
8717 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8718 sizeof(req.ht_capa_mask));
8720 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8721 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8722 return -EINVAL;
8723 memcpy(&req.ht_capa,
8724 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8725 sizeof(req.ht_capa));
8728 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
8729 req.flags |= ASSOC_REQ_DISABLE_VHT;
8731 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8732 memcpy(&req.vht_capa_mask,
8733 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8734 sizeof(req.vht_capa_mask));
8736 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
8737 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8738 return -EINVAL;
8739 memcpy(&req.vht_capa,
8740 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8741 sizeof(req.vht_capa));
8744 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
8745 if (!((rdev->wiphy.features &
8746 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8747 (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8748 !wiphy_ext_feature_isset(&rdev->wiphy,
8749 NL80211_EXT_FEATURE_RRM))
8750 return -EINVAL;
8751 req.flags |= ASSOC_REQ_USE_RRM;
8754 if (info->attrs[NL80211_ATTR_FILS_KEK]) {
8755 req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
8756 req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
8757 if (!info->attrs[NL80211_ATTR_FILS_NONCES])
8758 return -EINVAL;
8759 req.fils_nonces =
8760 nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
8763 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
8764 if (!err) {
8765 wdev_lock(dev->ieee80211_ptr);
8767 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
8768 ssid, ssid_len, &req);
8770 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8771 dev->ieee80211_ptr->conn_owner_nlportid =
8772 info->snd_portid;
8773 memcpy(dev->ieee80211_ptr->disconnect_bssid,
8774 bssid, ETH_ALEN);
8777 wdev_unlock(dev->ieee80211_ptr);
8780 return err;
8783 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
8785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8786 struct net_device *dev = info->user_ptr[1];
8787 const u8 *ie = NULL, *bssid;
8788 int ie_len = 0, err;
8789 u16 reason_code;
8790 bool local_state_change;
8792 if (dev->ieee80211_ptr->conn_owner_nlportid &&
8793 dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
8794 return -EPERM;
8796 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8797 return -EINVAL;
8799 if (!info->attrs[NL80211_ATTR_MAC])
8800 return -EINVAL;
8802 if (!info->attrs[NL80211_ATTR_REASON_CODE])
8803 return -EINVAL;
8805 if (!rdev->ops->deauth)
8806 return -EOPNOTSUPP;
8808 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8809 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8810 return -EOPNOTSUPP;
8812 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8814 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8815 if (reason_code == 0) {
8816 /* Reason Code 0 is reserved */
8817 return -EINVAL;
8820 if (info->attrs[NL80211_ATTR_IE]) {
8821 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8822 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8825 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8827 wdev_lock(dev->ieee80211_ptr);
8828 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
8829 local_state_change);
8830 wdev_unlock(dev->ieee80211_ptr);
8831 return err;
8834 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
8836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8837 struct net_device *dev = info->user_ptr[1];
8838 const u8 *ie = NULL, *bssid;
8839 int ie_len = 0, err;
8840 u16 reason_code;
8841 bool local_state_change;
8843 if (dev->ieee80211_ptr->conn_owner_nlportid &&
8844 dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
8845 return -EPERM;
8847 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8848 return -EINVAL;
8850 if (!info->attrs[NL80211_ATTR_MAC])
8851 return -EINVAL;
8853 if (!info->attrs[NL80211_ATTR_REASON_CODE])
8854 return -EINVAL;
8856 if (!rdev->ops->disassoc)
8857 return -EOPNOTSUPP;
8859 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8860 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8861 return -EOPNOTSUPP;
8863 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8865 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8866 if (reason_code == 0) {
8867 /* Reason Code 0 is reserved */
8868 return -EINVAL;
8871 if (info->attrs[NL80211_ATTR_IE]) {
8872 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8873 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8876 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8878 wdev_lock(dev->ieee80211_ptr);
8879 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
8880 local_state_change);
8881 wdev_unlock(dev->ieee80211_ptr);
8882 return err;
8885 static bool
8886 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
8887 int mcast_rate[NUM_NL80211_BANDS],
8888 int rateval)
8890 struct wiphy *wiphy = &rdev->wiphy;
8891 bool found = false;
8892 int band, i;
8894 for (band = 0; band < NUM_NL80211_BANDS; band++) {
8895 struct ieee80211_supported_band *sband;
8897 sband = wiphy->bands[band];
8898 if (!sband)
8899 continue;
8901 for (i = 0; i < sband->n_bitrates; i++) {
8902 if (sband->bitrates[i].bitrate == rateval) {
8903 mcast_rate[band] = i + 1;
8904 found = true;
8905 break;
8910 return found;
8913 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
8915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8916 struct net_device *dev = info->user_ptr[1];
8917 struct cfg80211_ibss_params ibss;
8918 struct wiphy *wiphy;
8919 struct cfg80211_cached_keys *connkeys = NULL;
8920 int err;
8922 memset(&ibss, 0, sizeof(ibss));
8924 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8925 return -EINVAL;
8927 if (!info->attrs[NL80211_ATTR_SSID] ||
8928 !nla_len(info->attrs[NL80211_ATTR_SSID]))
8929 return -EINVAL;
8931 ibss.beacon_interval = 100;
8933 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
8934 ibss.beacon_interval =
8935 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8937 err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
8938 ibss.beacon_interval);
8939 if (err)
8940 return err;
8942 if (!rdev->ops->join_ibss)
8943 return -EOPNOTSUPP;
8945 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8946 return -EOPNOTSUPP;
8948 wiphy = &rdev->wiphy;
8950 if (info->attrs[NL80211_ATTR_MAC]) {
8951 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8953 if (!is_valid_ether_addr(ibss.bssid))
8954 return -EINVAL;
8956 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8957 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8959 if (info->attrs[NL80211_ATTR_IE]) {
8960 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8961 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8964 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
8965 if (err)
8966 return err;
8968 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
8969 NL80211_IFTYPE_ADHOC))
8970 return -EINVAL;
8972 switch (ibss.chandef.width) {
8973 case NL80211_CHAN_WIDTH_5:
8974 case NL80211_CHAN_WIDTH_10:
8975 case NL80211_CHAN_WIDTH_20_NOHT:
8976 break;
8977 case NL80211_CHAN_WIDTH_20:
8978 case NL80211_CHAN_WIDTH_40:
8979 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8980 return -EINVAL;
8981 break;
8982 case NL80211_CHAN_WIDTH_80:
8983 case NL80211_CHAN_WIDTH_80P80:
8984 case NL80211_CHAN_WIDTH_160:
8985 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8986 return -EINVAL;
8987 if (!wiphy_ext_feature_isset(&rdev->wiphy,
8988 NL80211_EXT_FEATURE_VHT_IBSS))
8989 return -EINVAL;
8990 break;
8991 default:
8992 return -EINVAL;
8995 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
8996 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
8998 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8999 u8 *rates =
9000 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9001 int n_rates =
9002 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9003 struct ieee80211_supported_band *sband =
9004 wiphy->bands[ibss.chandef.chan->band];
9006 err = ieee80211_get_ratemask(sband, rates, n_rates,
9007 &ibss.basic_rates);
9008 if (err)
9009 return err;
9012 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9013 memcpy(&ibss.ht_capa_mask,
9014 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9015 sizeof(ibss.ht_capa_mask));
9017 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9018 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9019 return -EINVAL;
9020 memcpy(&ibss.ht_capa,
9021 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9022 sizeof(ibss.ht_capa));
9025 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9026 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9027 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9028 return -EINVAL;
9030 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9031 bool no_ht = false;
9033 connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9034 if (IS_ERR(connkeys))
9035 return PTR_ERR(connkeys);
9037 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9038 no_ht) {
9039 kzfree(connkeys);
9040 return -EINVAL;
9044 ibss.control_port =
9045 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9047 if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9048 int r = validate_pae_over_nl80211(rdev, info);
9050 if (r < 0) {
9051 kzfree(connkeys);
9052 return r;
9055 ibss.control_port_over_nl80211 = true;
9058 ibss.userspace_handles_dfs =
9059 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9061 wdev_lock(dev->ieee80211_ptr);
9062 err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9063 if (err)
9064 kzfree(connkeys);
9065 else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9066 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9067 wdev_unlock(dev->ieee80211_ptr);
9069 return err;
9072 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9075 struct net_device *dev = info->user_ptr[1];
9077 if (!rdev->ops->leave_ibss)
9078 return -EOPNOTSUPP;
9080 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9081 return -EOPNOTSUPP;
9083 return cfg80211_leave_ibss(rdev, dev, false);
9086 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9088 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9089 struct net_device *dev = info->user_ptr[1];
9090 int mcast_rate[NUM_NL80211_BANDS];
9091 u32 nla_rate;
9092 int err;
9094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9095 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9096 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9097 return -EOPNOTSUPP;
9099 if (!rdev->ops->set_mcast_rate)
9100 return -EOPNOTSUPP;
9102 memset(mcast_rate, 0, sizeof(mcast_rate));
9104 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9105 return -EINVAL;
9107 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9108 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9109 return -EINVAL;
9111 err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9113 return err;
9116 static struct sk_buff *
9117 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9118 struct wireless_dev *wdev, int approxlen,
9119 u32 portid, u32 seq, enum nl80211_commands cmd,
9120 enum nl80211_attrs attr,
9121 const struct nl80211_vendor_cmd_info *info,
9122 gfp_t gfp)
9124 struct sk_buff *skb;
9125 void *hdr;
9126 struct nlattr *data;
9128 skb = nlmsg_new(approxlen + 100, gfp);
9129 if (!skb)
9130 return NULL;
9132 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9133 if (!hdr) {
9134 kfree_skb(skb);
9135 return NULL;
9138 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9139 goto nla_put_failure;
9141 if (info) {
9142 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9143 info->vendor_id))
9144 goto nla_put_failure;
9145 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9146 info->subcmd))
9147 goto nla_put_failure;
9150 if (wdev) {
9151 if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9152 wdev_id(wdev), NL80211_ATTR_PAD))
9153 goto nla_put_failure;
9154 if (wdev->netdev &&
9155 nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9156 wdev->netdev->ifindex))
9157 goto nla_put_failure;
9160 data = nla_nest_start(skb, attr);
9161 if (!data)
9162 goto nla_put_failure;
9164 ((void **)skb->cb)[0] = rdev;
9165 ((void **)skb->cb)[1] = hdr;
9166 ((void **)skb->cb)[2] = data;
9168 return skb;
9170 nla_put_failure:
9171 kfree_skb(skb);
9172 return NULL;
9175 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9176 struct wireless_dev *wdev,
9177 enum nl80211_commands cmd,
9178 enum nl80211_attrs attr,
9179 int vendor_event_idx,
9180 int approxlen, gfp_t gfp)
9182 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9183 const struct nl80211_vendor_cmd_info *info;
9185 switch (cmd) {
9186 case NL80211_CMD_TESTMODE:
9187 if (WARN_ON(vendor_event_idx != -1))
9188 return NULL;
9189 info = NULL;
9190 break;
9191 case NL80211_CMD_VENDOR:
9192 if (WARN_ON(vendor_event_idx < 0 ||
9193 vendor_event_idx >= wiphy->n_vendor_events))
9194 return NULL;
9195 info = &wiphy->vendor_events[vendor_event_idx];
9196 break;
9197 default:
9198 WARN_ON(1);
9199 return NULL;
9202 return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
9203 cmd, attr, info, gfp);
9205 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9207 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9209 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9210 void *hdr = ((void **)skb->cb)[1];
9211 struct nlattr *data = ((void **)skb->cb)[2];
9212 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9214 /* clear CB data for netlink core to own from now on */
9215 memset(skb->cb, 0, sizeof(skb->cb));
9217 nla_nest_end(skb, data);
9218 genlmsg_end(skb, hdr);
9220 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9221 mcgrp = NL80211_MCGRP_VENDOR;
9223 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
9224 mcgrp, gfp);
9226 EXPORT_SYMBOL(__cfg80211_send_event_skb);
9228 #ifdef CONFIG_NL80211_TESTMODE
9229 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9231 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9232 struct wireless_dev *wdev =
9233 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9234 int err;
9236 if (!rdev->ops->testmode_cmd)
9237 return -EOPNOTSUPP;
9239 if (IS_ERR(wdev)) {
9240 err = PTR_ERR(wdev);
9241 if (err != -EINVAL)
9242 return err;
9243 wdev = NULL;
9244 } else if (wdev->wiphy != &rdev->wiphy) {
9245 return -EINVAL;
9248 if (!info->attrs[NL80211_ATTR_TESTDATA])
9249 return -EINVAL;
9251 rdev->cur_cmd_info = info;
9252 err = rdev_testmode_cmd(rdev, wdev,
9253 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9254 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9255 rdev->cur_cmd_info = NULL;
9257 return err;
9260 static int nl80211_testmode_dump(struct sk_buff *skb,
9261 struct netlink_callback *cb)
9263 struct cfg80211_registered_device *rdev;
9264 int err;
9265 long phy_idx;
9266 void *data = NULL;
9267 int data_len = 0;
9269 rtnl_lock();
9271 if (cb->args[0]) {
9273 * 0 is a valid index, but not valid for args[0],
9274 * so we need to offset by 1.
9276 phy_idx = cb->args[0] - 1;
9278 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9279 if (!rdev) {
9280 err = -ENOENT;
9281 goto out_err;
9283 } else {
9284 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
9286 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
9287 attrbuf, nl80211_fam.maxattr,
9288 nl80211_policy, NULL);
9289 if (err)
9290 goto out_err;
9292 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9293 if (IS_ERR(rdev)) {
9294 err = PTR_ERR(rdev);
9295 goto out_err;
9297 phy_idx = rdev->wiphy_idx;
9299 if (attrbuf[NL80211_ATTR_TESTDATA])
9300 cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9303 if (cb->args[1]) {
9304 data = nla_data((void *)cb->args[1]);
9305 data_len = nla_len((void *)cb->args[1]);
9308 if (!rdev->ops->testmode_dump) {
9309 err = -EOPNOTSUPP;
9310 goto out_err;
9313 while (1) {
9314 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9315 cb->nlh->nlmsg_seq, NLM_F_MULTI,
9316 NL80211_CMD_TESTMODE);
9317 struct nlattr *tmdata;
9319 if (!hdr)
9320 break;
9322 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9323 genlmsg_cancel(skb, hdr);
9324 break;
9327 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
9328 if (!tmdata) {
9329 genlmsg_cancel(skb, hdr);
9330 break;
9332 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9333 nla_nest_end(skb, tmdata);
9335 if (err == -ENOBUFS || err == -ENOENT) {
9336 genlmsg_cancel(skb, hdr);
9337 break;
9338 } else if (err) {
9339 genlmsg_cancel(skb, hdr);
9340 goto out_err;
9343 genlmsg_end(skb, hdr);
9346 err = skb->len;
9347 /* see above */
9348 cb->args[0] = phy_idx + 1;
9349 out_err:
9350 rtnl_unlock();
9351 return err;
9353 #endif
9355 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9357 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9358 struct net_device *dev = info->user_ptr[1];
9359 struct cfg80211_connect_params connect;
9360 struct wiphy *wiphy;
9361 struct cfg80211_cached_keys *connkeys = NULL;
9362 int err;
9364 memset(&connect, 0, sizeof(connect));
9366 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9367 return -EINVAL;
9369 if (!info->attrs[NL80211_ATTR_SSID] ||
9370 !nla_len(info->attrs[NL80211_ATTR_SSID]))
9371 return -EINVAL;
9373 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9374 connect.auth_type =
9375 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9376 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9377 NL80211_CMD_CONNECT))
9378 return -EINVAL;
9379 } else
9380 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9382 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9384 if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9385 !wiphy_ext_feature_isset(&rdev->wiphy,
9386 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9387 return -EINVAL;
9388 connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9390 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9391 NL80211_MAX_NR_CIPHER_SUITES);
9392 if (err)
9393 return err;
9395 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9396 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9397 return -EOPNOTSUPP;
9399 wiphy = &rdev->wiphy;
9401 connect.bg_scan_period = -1;
9402 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9403 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9404 connect.bg_scan_period =
9405 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9408 if (info->attrs[NL80211_ATTR_MAC])
9409 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9410 else if (info->attrs[NL80211_ATTR_MAC_HINT])
9411 connect.bssid_hint =
9412 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9413 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9414 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9416 if (info->attrs[NL80211_ATTR_IE]) {
9417 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9418 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9421 if (info->attrs[NL80211_ATTR_USE_MFP]) {
9422 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9423 if (connect.mfp == NL80211_MFP_OPTIONAL &&
9424 !wiphy_ext_feature_isset(&rdev->wiphy,
9425 NL80211_EXT_FEATURE_MFP_OPTIONAL))
9426 return -EOPNOTSUPP;
9428 if (connect.mfp != NL80211_MFP_REQUIRED &&
9429 connect.mfp != NL80211_MFP_NO &&
9430 connect.mfp != NL80211_MFP_OPTIONAL)
9431 return -EINVAL;
9432 } else {
9433 connect.mfp = NL80211_MFP_NO;
9436 if (info->attrs[NL80211_ATTR_PREV_BSSID])
9437 connect.prev_bssid =
9438 nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9440 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9441 connect.channel = nl80211_get_valid_chan(
9442 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9443 if (!connect.channel)
9444 return -EINVAL;
9445 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9446 connect.channel_hint = nl80211_get_valid_chan(
9447 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9448 if (!connect.channel_hint)
9449 return -EINVAL;
9452 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9453 connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9454 if (IS_ERR(connkeys))
9455 return PTR_ERR(connkeys);
9458 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9459 connect.flags |= ASSOC_REQ_DISABLE_HT;
9461 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9462 memcpy(&connect.ht_capa_mask,
9463 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9464 sizeof(connect.ht_capa_mask));
9466 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9467 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9468 kzfree(connkeys);
9469 return -EINVAL;
9471 memcpy(&connect.ht_capa,
9472 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9473 sizeof(connect.ht_capa));
9476 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9477 connect.flags |= ASSOC_REQ_DISABLE_VHT;
9479 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9480 memcpy(&connect.vht_capa_mask,
9481 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9482 sizeof(connect.vht_capa_mask));
9484 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9485 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
9486 kzfree(connkeys);
9487 return -EINVAL;
9489 memcpy(&connect.vht_capa,
9490 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9491 sizeof(connect.vht_capa));
9494 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9495 if (!((rdev->wiphy.features &
9496 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9497 (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9498 !wiphy_ext_feature_isset(&rdev->wiphy,
9499 NL80211_EXT_FEATURE_RRM)) {
9500 kzfree(connkeys);
9501 return -EINVAL;
9503 connect.flags |= ASSOC_REQ_USE_RRM;
9506 connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
9507 if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
9508 kzfree(connkeys);
9509 return -EOPNOTSUPP;
9512 if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
9513 /* bss selection makes no sense if bssid is set */
9514 if (connect.bssid) {
9515 kzfree(connkeys);
9516 return -EINVAL;
9519 err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
9520 wiphy, &connect.bss_select);
9521 if (err) {
9522 kzfree(connkeys);
9523 return err;
9527 if (wiphy_ext_feature_isset(&rdev->wiphy,
9528 NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
9529 info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9530 info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9531 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9532 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9533 connect.fils_erp_username =
9534 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9535 connect.fils_erp_username_len =
9536 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9537 connect.fils_erp_realm =
9538 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9539 connect.fils_erp_realm_len =
9540 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9541 connect.fils_erp_next_seq_num =
9542 nla_get_u16(
9543 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9544 connect.fils_erp_rrk =
9545 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9546 connect.fils_erp_rrk_len =
9547 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9548 } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9549 info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9550 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9551 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9552 kzfree(connkeys);
9553 return -EINVAL;
9556 if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
9557 if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9558 kzfree(connkeys);
9559 GENL_SET_ERR_MSG(info,
9560 "external auth requires connection ownership");
9561 return -EINVAL;
9563 connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
9566 wdev_lock(dev->ieee80211_ptr);
9568 err = cfg80211_connect(rdev, dev, &connect, connkeys,
9569 connect.prev_bssid);
9570 if (err)
9571 kzfree(connkeys);
9573 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9574 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9575 if (connect.bssid)
9576 memcpy(dev->ieee80211_ptr->disconnect_bssid,
9577 connect.bssid, ETH_ALEN);
9578 else
9579 memset(dev->ieee80211_ptr->disconnect_bssid,
9580 0, ETH_ALEN);
9583 wdev_unlock(dev->ieee80211_ptr);
9585 return err;
9588 static int nl80211_update_connect_params(struct sk_buff *skb,
9589 struct genl_info *info)
9591 struct cfg80211_connect_params connect = {};
9592 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9593 struct net_device *dev = info->user_ptr[1];
9594 struct wireless_dev *wdev = dev->ieee80211_ptr;
9595 bool fils_sk_offload;
9596 u32 auth_type;
9597 u32 changed = 0;
9598 int ret;
9600 if (!rdev->ops->update_connect_params)
9601 return -EOPNOTSUPP;
9603 if (info->attrs[NL80211_ATTR_IE]) {
9604 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9605 return -EINVAL;
9606 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9607 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9608 changed |= UPDATE_ASSOC_IES;
9611 fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
9612 NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
9615 * when driver supports fils-sk offload all attributes must be
9616 * provided. So the else covers "fils-sk-not-all" and
9617 * "no-fils-sk-any".
9619 if (fils_sk_offload &&
9620 info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9621 info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9622 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9623 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9624 connect.fils_erp_username =
9625 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9626 connect.fils_erp_username_len =
9627 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9628 connect.fils_erp_realm =
9629 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9630 connect.fils_erp_realm_len =
9631 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9632 connect.fils_erp_next_seq_num =
9633 nla_get_u16(
9634 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9635 connect.fils_erp_rrk =
9636 nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9637 connect.fils_erp_rrk_len =
9638 nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9639 changed |= UPDATE_FILS_ERP_INFO;
9640 } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9641 info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9642 info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9643 info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9644 return -EINVAL;
9647 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9648 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9649 if (!nl80211_valid_auth_type(rdev, auth_type,
9650 NL80211_CMD_CONNECT))
9651 return -EINVAL;
9653 if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
9654 fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
9655 return -EINVAL;
9657 connect.auth_type = auth_type;
9658 changed |= UPDATE_AUTH_TYPE;
9661 wdev_lock(dev->ieee80211_ptr);
9662 if (!wdev->current_bss)
9663 ret = -ENOLINK;
9664 else
9665 ret = rdev_update_connect_params(rdev, dev, &connect, changed);
9666 wdev_unlock(dev->ieee80211_ptr);
9668 return ret;
9671 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
9673 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9674 struct net_device *dev = info->user_ptr[1];
9675 u16 reason;
9676 int ret;
9678 if (dev->ieee80211_ptr->conn_owner_nlportid &&
9679 dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9680 return -EPERM;
9682 if (!info->attrs[NL80211_ATTR_REASON_CODE])
9683 reason = WLAN_REASON_DEAUTH_LEAVING;
9684 else
9685 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9687 if (reason == 0)
9688 return -EINVAL;
9690 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9691 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9692 return -EOPNOTSUPP;
9694 wdev_lock(dev->ieee80211_ptr);
9695 ret = cfg80211_disconnect(rdev, dev, reason, true);
9696 wdev_unlock(dev->ieee80211_ptr);
9697 return ret;
9700 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
9702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9703 struct net *net;
9704 int err;
9706 if (info->attrs[NL80211_ATTR_PID]) {
9707 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
9709 net = get_net_ns_by_pid(pid);
9710 } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
9711 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
9713 net = get_net_ns_by_fd(fd);
9714 } else {
9715 return -EINVAL;
9718 if (IS_ERR(net))
9719 return PTR_ERR(net);
9721 err = 0;
9723 /* check if anything to do */
9724 if (!net_eq(wiphy_net(&rdev->wiphy), net))
9725 err = cfg80211_switch_netns(rdev, net);
9727 put_net(net);
9728 return err;
9731 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
9733 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9734 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
9735 struct cfg80211_pmksa *pmksa) = NULL;
9736 struct net_device *dev = info->user_ptr[1];
9737 struct cfg80211_pmksa pmksa;
9739 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
9741 if (!info->attrs[NL80211_ATTR_PMKID])
9742 return -EINVAL;
9744 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
9746 if (info->attrs[NL80211_ATTR_MAC]) {
9747 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9748 } else if (info->attrs[NL80211_ATTR_SSID] &&
9749 info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
9750 (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
9751 info->attrs[NL80211_ATTR_PMK])) {
9752 pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9753 pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9754 pmksa.cache_id =
9755 nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
9756 } else {
9757 return -EINVAL;
9759 if (info->attrs[NL80211_ATTR_PMK]) {
9760 pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9761 pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
9764 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9765 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9766 return -EOPNOTSUPP;
9768 switch (info->genlhdr->cmd) {
9769 case NL80211_CMD_SET_PMKSA:
9770 rdev_ops = rdev->ops->set_pmksa;
9771 break;
9772 case NL80211_CMD_DEL_PMKSA:
9773 rdev_ops = rdev->ops->del_pmksa;
9774 break;
9775 default:
9776 WARN_ON(1);
9777 break;
9780 if (!rdev_ops)
9781 return -EOPNOTSUPP;
9783 return rdev_ops(&rdev->wiphy, dev, &pmksa);
9786 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
9788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9789 struct net_device *dev = info->user_ptr[1];
9791 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9792 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9793 return -EOPNOTSUPP;
9795 if (!rdev->ops->flush_pmksa)
9796 return -EOPNOTSUPP;
9798 return rdev_flush_pmksa(rdev, dev);
9801 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
9803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9804 struct net_device *dev = info->user_ptr[1];
9805 u8 action_code, dialog_token;
9806 u32 peer_capability = 0;
9807 u16 status_code;
9808 u8 *peer;
9809 bool initiator;
9811 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9812 !rdev->ops->tdls_mgmt)
9813 return -EOPNOTSUPP;
9815 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
9816 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
9817 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
9818 !info->attrs[NL80211_ATTR_IE] ||
9819 !info->attrs[NL80211_ATTR_MAC])
9820 return -EINVAL;
9822 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9823 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
9824 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
9825 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
9826 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
9827 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
9828 peer_capability =
9829 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
9831 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
9832 dialog_token, status_code, peer_capability,
9833 initiator,
9834 nla_data(info->attrs[NL80211_ATTR_IE]),
9835 nla_len(info->attrs[NL80211_ATTR_IE]));
9838 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
9840 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9841 struct net_device *dev = info->user_ptr[1];
9842 enum nl80211_tdls_operation operation;
9843 u8 *peer;
9845 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9846 !rdev->ops->tdls_oper)
9847 return -EOPNOTSUPP;
9849 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
9850 !info->attrs[NL80211_ATTR_MAC])
9851 return -EINVAL;
9853 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
9854 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9856 return rdev_tdls_oper(rdev, dev, peer, operation);
9859 static int nl80211_remain_on_channel(struct sk_buff *skb,
9860 struct genl_info *info)
9862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9863 struct wireless_dev *wdev = info->user_ptr[1];
9864 struct cfg80211_chan_def chandef;
9865 const struct cfg80211_chan_def *compat_chandef;
9866 struct sk_buff *msg;
9867 void *hdr;
9868 u64 cookie;
9869 u32 duration;
9870 int err;
9872 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9873 !info->attrs[NL80211_ATTR_DURATION])
9874 return -EINVAL;
9876 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9878 if (!rdev->ops->remain_on_channel ||
9879 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
9880 return -EOPNOTSUPP;
9883 * We should be on that channel for at least a minimum amount of
9884 * time (10ms) but no longer than the driver supports.
9886 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9887 duration > rdev->wiphy.max_remain_on_channel_duration)
9888 return -EINVAL;
9890 err = nl80211_parse_chandef(rdev, info, &chandef);
9891 if (err)
9892 return err;
9894 wdev_lock(wdev);
9895 if (!cfg80211_off_channel_oper_allowed(wdev) &&
9896 !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
9897 compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
9898 &chandef);
9899 if (compat_chandef != &chandef) {
9900 wdev_unlock(wdev);
9901 return -EBUSY;
9904 wdev_unlock(wdev);
9906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9907 if (!msg)
9908 return -ENOMEM;
9910 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9911 NL80211_CMD_REMAIN_ON_CHANNEL);
9912 if (!hdr) {
9913 err = -ENOBUFS;
9914 goto free_msg;
9917 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
9918 duration, &cookie);
9920 if (err)
9921 goto free_msg;
9923 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9924 NL80211_ATTR_PAD))
9925 goto nla_put_failure;
9927 genlmsg_end(msg, hdr);
9929 return genlmsg_reply(msg, info);
9931 nla_put_failure:
9932 err = -ENOBUFS;
9933 free_msg:
9934 nlmsg_free(msg);
9935 return err;
9938 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
9939 struct genl_info *info)
9941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9942 struct wireless_dev *wdev = info->user_ptr[1];
9943 u64 cookie;
9945 if (!info->attrs[NL80211_ATTR_COOKIE])
9946 return -EINVAL;
9948 if (!rdev->ops->cancel_remain_on_channel)
9949 return -EOPNOTSUPP;
9951 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9953 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
9956 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
9957 struct genl_info *info)
9959 struct cfg80211_bitrate_mask mask;
9960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9961 struct net_device *dev = info->user_ptr[1];
9962 int err;
9964 if (!rdev->ops->set_bitrate_mask)
9965 return -EOPNOTSUPP;
9967 err = nl80211_parse_tx_bitrate_mask(info, &mask);
9968 if (err)
9969 return err;
9971 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
9974 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
9976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9977 struct wireless_dev *wdev = info->user_ptr[1];
9978 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
9980 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
9981 return -EINVAL;
9983 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
9984 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
9986 switch (wdev->iftype) {
9987 case NL80211_IFTYPE_STATION:
9988 case NL80211_IFTYPE_ADHOC:
9989 case NL80211_IFTYPE_P2P_CLIENT:
9990 case NL80211_IFTYPE_AP:
9991 case NL80211_IFTYPE_AP_VLAN:
9992 case NL80211_IFTYPE_MESH_POINT:
9993 case NL80211_IFTYPE_P2P_GO:
9994 case NL80211_IFTYPE_P2P_DEVICE:
9995 break;
9996 case NL80211_IFTYPE_NAN:
9997 default:
9998 return -EOPNOTSUPP;
10001 /* not much point in registering if we can't reply */
10002 if (!rdev->ops->mgmt_tx)
10003 return -EOPNOTSUPP;
10005 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10006 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10007 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10010 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10012 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10013 struct wireless_dev *wdev = info->user_ptr[1];
10014 struct cfg80211_chan_def chandef;
10015 int err;
10016 void *hdr = NULL;
10017 u64 cookie;
10018 struct sk_buff *msg = NULL;
10019 struct cfg80211_mgmt_tx_params params = {
10020 .dont_wait_for_ack =
10021 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10024 if (!info->attrs[NL80211_ATTR_FRAME])
10025 return -EINVAL;
10027 if (!rdev->ops->mgmt_tx)
10028 return -EOPNOTSUPP;
10030 switch (wdev->iftype) {
10031 case NL80211_IFTYPE_P2P_DEVICE:
10032 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10033 return -EINVAL;
10034 case NL80211_IFTYPE_STATION:
10035 case NL80211_IFTYPE_ADHOC:
10036 case NL80211_IFTYPE_P2P_CLIENT:
10037 case NL80211_IFTYPE_AP:
10038 case NL80211_IFTYPE_AP_VLAN:
10039 case NL80211_IFTYPE_MESH_POINT:
10040 case NL80211_IFTYPE_P2P_GO:
10041 break;
10042 case NL80211_IFTYPE_NAN:
10043 default:
10044 return -EOPNOTSUPP;
10047 if (info->attrs[NL80211_ATTR_DURATION]) {
10048 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10049 return -EINVAL;
10050 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10053 * We should wait on the channel for at least a minimum amount
10054 * of time (10ms) but no longer than the driver supports.
10056 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10057 params.wait > rdev->wiphy.max_remain_on_channel_duration)
10058 return -EINVAL;
10061 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10063 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10064 return -EINVAL;
10066 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10068 /* get the channel if any has been specified, otherwise pass NULL to
10069 * the driver. The latter will use the current one
10071 chandef.chan = NULL;
10072 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10073 err = nl80211_parse_chandef(rdev, info, &chandef);
10074 if (err)
10075 return err;
10078 if (!chandef.chan && params.offchan)
10079 return -EINVAL;
10081 wdev_lock(wdev);
10082 if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10083 wdev_unlock(wdev);
10084 return -EBUSY;
10086 wdev_unlock(wdev);
10088 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10089 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10091 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10092 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10093 int i;
10095 if (len % sizeof(u16))
10096 return -EINVAL;
10098 params.n_csa_offsets = len / sizeof(u16);
10099 params.csa_offsets =
10100 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10102 /* check that all the offsets fit the frame */
10103 for (i = 0; i < params.n_csa_offsets; i++) {
10104 if (params.csa_offsets[i] >= params.len)
10105 return -EINVAL;
10109 if (!params.dont_wait_for_ack) {
10110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10111 if (!msg)
10112 return -ENOMEM;
10114 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10115 NL80211_CMD_FRAME);
10116 if (!hdr) {
10117 err = -ENOBUFS;
10118 goto free_msg;
10122 params.chan = chandef.chan;
10123 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10124 if (err)
10125 goto free_msg;
10127 if (msg) {
10128 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10129 NL80211_ATTR_PAD))
10130 goto nla_put_failure;
10132 genlmsg_end(msg, hdr);
10133 return genlmsg_reply(msg, info);
10136 return 0;
10138 nla_put_failure:
10139 err = -ENOBUFS;
10140 free_msg:
10141 nlmsg_free(msg);
10142 return err;
10145 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10148 struct wireless_dev *wdev = info->user_ptr[1];
10149 u64 cookie;
10151 if (!info->attrs[NL80211_ATTR_COOKIE])
10152 return -EINVAL;
10154 if (!rdev->ops->mgmt_tx_cancel_wait)
10155 return -EOPNOTSUPP;
10157 switch (wdev->iftype) {
10158 case NL80211_IFTYPE_STATION:
10159 case NL80211_IFTYPE_ADHOC:
10160 case NL80211_IFTYPE_P2P_CLIENT:
10161 case NL80211_IFTYPE_AP:
10162 case NL80211_IFTYPE_AP_VLAN:
10163 case NL80211_IFTYPE_P2P_GO:
10164 case NL80211_IFTYPE_P2P_DEVICE:
10165 break;
10166 case NL80211_IFTYPE_NAN:
10167 default:
10168 return -EOPNOTSUPP;
10171 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10173 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10176 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10178 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10179 struct wireless_dev *wdev;
10180 struct net_device *dev = info->user_ptr[1];
10181 u8 ps_state;
10182 bool state;
10183 int err;
10185 if (!info->attrs[NL80211_ATTR_PS_STATE])
10186 return -EINVAL;
10188 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10190 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
10191 return -EINVAL;
10193 wdev = dev->ieee80211_ptr;
10195 if (!rdev->ops->set_power_mgmt)
10196 return -EOPNOTSUPP;
10198 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10200 if (state == wdev->ps)
10201 return 0;
10203 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10204 if (!err)
10205 wdev->ps = state;
10206 return err;
10209 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10212 enum nl80211_ps_state ps_state;
10213 struct wireless_dev *wdev;
10214 struct net_device *dev = info->user_ptr[1];
10215 struct sk_buff *msg;
10216 void *hdr;
10217 int err;
10219 wdev = dev->ieee80211_ptr;
10221 if (!rdev->ops->set_power_mgmt)
10222 return -EOPNOTSUPP;
10224 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10225 if (!msg)
10226 return -ENOMEM;
10228 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10229 NL80211_CMD_GET_POWER_SAVE);
10230 if (!hdr) {
10231 err = -ENOBUFS;
10232 goto free_msg;
10235 if (wdev->ps)
10236 ps_state = NL80211_PS_ENABLED;
10237 else
10238 ps_state = NL80211_PS_DISABLED;
10240 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10241 goto nla_put_failure;
10243 genlmsg_end(msg, hdr);
10244 return genlmsg_reply(msg, info);
10246 nla_put_failure:
10247 err = -ENOBUFS;
10248 free_msg:
10249 nlmsg_free(msg);
10250 return err;
10253 static const struct nla_policy
10254 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10255 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10256 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10257 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10258 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10259 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10260 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10261 [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10264 static int nl80211_set_cqm_txe(struct genl_info *info,
10265 u32 rate, u32 pkts, u32 intvl)
10267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10268 struct net_device *dev = info->user_ptr[1];
10269 struct wireless_dev *wdev = dev->ieee80211_ptr;
10271 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10272 return -EINVAL;
10274 if (!rdev->ops->set_cqm_txe_config)
10275 return -EOPNOTSUPP;
10277 if (wdev->iftype != NL80211_IFTYPE_STATION &&
10278 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10279 return -EOPNOTSUPP;
10281 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10284 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10285 struct net_device *dev)
10287 struct wireless_dev *wdev = dev->ieee80211_ptr;
10288 s32 last, low, high;
10289 u32 hyst;
10290 int i, n, low_index;
10291 int err;
10293 /* RSSI reporting disabled? */
10294 if (!wdev->cqm_config)
10295 return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10298 * Obtain current RSSI value if possible, if not and no RSSI threshold
10299 * event has been received yet, we should receive an event after a
10300 * connection is established and enough beacons received to calculate
10301 * the average.
10303 if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10304 rdev->ops->get_station) {
10305 struct station_info sinfo = {};
10306 u8 *mac_addr;
10308 mac_addr = wdev->current_bss->pub.bssid;
10310 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10311 if (err)
10312 return err;
10314 cfg80211_sinfo_release_content(&sinfo);
10315 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10316 wdev->cqm_config->last_rssi_event_value =
10317 (s8) sinfo.rx_beacon_signal_avg;
10320 last = wdev->cqm_config->last_rssi_event_value;
10321 hyst = wdev->cqm_config->rssi_hyst;
10322 n = wdev->cqm_config->n_rssi_thresholds;
10324 for (i = 0; i < n; i++) {
10325 i = array_index_nospec(i, n);
10326 if (last < wdev->cqm_config->rssi_thresholds[i])
10327 break;
10330 low_index = i - 1;
10331 if (low_index >= 0) {
10332 low_index = array_index_nospec(low_index, n);
10333 low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10334 } else {
10335 low = S32_MIN;
10337 if (i < n) {
10338 i = array_index_nospec(i, n);
10339 high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10340 } else {
10341 high = S32_MAX;
10344 return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10347 static int nl80211_set_cqm_rssi(struct genl_info *info,
10348 const s32 *thresholds, int n_thresholds,
10349 u32 hysteresis)
10351 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10352 struct net_device *dev = info->user_ptr[1];
10353 struct wireless_dev *wdev = dev->ieee80211_ptr;
10354 int i, err;
10355 s32 prev = S32_MIN;
10357 /* Check all values negative and sorted */
10358 for (i = 0; i < n_thresholds; i++) {
10359 if (thresholds[i] > 0 || thresholds[i] <= prev)
10360 return -EINVAL;
10362 prev = thresholds[i];
10365 if (wdev->iftype != NL80211_IFTYPE_STATION &&
10366 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10367 return -EOPNOTSUPP;
10369 wdev_lock(wdev);
10370 cfg80211_cqm_config_free(wdev);
10371 wdev_unlock(wdev);
10373 if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10374 if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10375 return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10377 return rdev_set_cqm_rssi_config(rdev, dev,
10378 thresholds[0], hysteresis);
10381 if (!wiphy_ext_feature_isset(&rdev->wiphy,
10382 NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10383 return -EOPNOTSUPP;
10385 if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10386 n_thresholds = 0;
10388 wdev_lock(wdev);
10389 if (n_thresholds) {
10390 struct cfg80211_cqm_config *cqm_config;
10392 cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10393 n_thresholds * sizeof(s32), GFP_KERNEL);
10394 if (!cqm_config) {
10395 err = -ENOMEM;
10396 goto unlock;
10399 cqm_config->rssi_hyst = hysteresis;
10400 cqm_config->n_rssi_thresholds = n_thresholds;
10401 memcpy(cqm_config->rssi_thresholds, thresholds,
10402 n_thresholds * sizeof(s32));
10404 wdev->cqm_config = cqm_config;
10407 err = cfg80211_cqm_rssi_update(rdev, dev);
10409 unlock:
10410 wdev_unlock(wdev);
10412 return err;
10415 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10417 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10418 struct nlattr *cqm;
10419 int err;
10421 cqm = info->attrs[NL80211_ATTR_CQM];
10422 if (!cqm)
10423 return -EINVAL;
10425 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
10426 nl80211_attr_cqm_policy, info->extack);
10427 if (err)
10428 return err;
10430 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10431 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10432 const s32 *thresholds =
10433 nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10434 int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10435 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10437 if (len % 4)
10438 return -EINVAL;
10440 return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10441 hysteresis);
10444 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10445 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10446 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10447 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10448 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10449 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10451 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10454 return -EINVAL;
10457 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10460 struct net_device *dev = info->user_ptr[1];
10461 struct ocb_setup setup = {};
10462 int err;
10464 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10465 if (err)
10466 return err;
10468 return cfg80211_join_ocb(rdev, dev, &setup);
10471 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
10473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10474 struct net_device *dev = info->user_ptr[1];
10476 return cfg80211_leave_ocb(rdev, dev);
10479 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
10481 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10482 struct net_device *dev = info->user_ptr[1];
10483 struct mesh_config cfg;
10484 struct mesh_setup setup;
10485 int err;
10487 /* start with default */
10488 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
10489 memcpy(&setup, &default_mesh_setup, sizeof(setup));
10491 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
10492 /* and parse parameters if given */
10493 err = nl80211_parse_mesh_config(info, &cfg, NULL);
10494 if (err)
10495 return err;
10498 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
10499 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
10500 return -EINVAL;
10502 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
10503 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
10505 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
10506 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
10507 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
10508 return -EINVAL;
10510 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
10511 setup.beacon_interval =
10512 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10514 err = cfg80211_validate_beacon_int(rdev,
10515 NL80211_IFTYPE_MESH_POINT,
10516 setup.beacon_interval);
10517 if (err)
10518 return err;
10521 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
10522 setup.dtim_period =
10523 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
10524 if (setup.dtim_period < 1 || setup.dtim_period > 100)
10525 return -EINVAL;
10528 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
10529 /* parse additional setup parameters if given */
10530 err = nl80211_parse_mesh_setup(info, &setup);
10531 if (err)
10532 return err;
10535 if (setup.user_mpm)
10536 cfg.auto_open_plinks = false;
10538 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10539 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10540 if (err)
10541 return err;
10542 } else {
10543 /* __cfg80211_join_mesh() will sort it out */
10544 setup.chandef.chan = NULL;
10547 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10548 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10549 int n_rates =
10550 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10551 struct ieee80211_supported_band *sband;
10553 if (!setup.chandef.chan)
10554 return -EINVAL;
10556 sband = rdev->wiphy.bands[setup.chandef.chan->band];
10558 err = ieee80211_get_ratemask(sband, rates, n_rates,
10559 &setup.basic_rates);
10560 if (err)
10561 return err;
10564 if (info->attrs[NL80211_ATTR_TX_RATES]) {
10565 err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
10566 if (err)
10567 return err;
10569 if (!setup.chandef.chan)
10570 return -EINVAL;
10572 err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
10573 &setup.beacon_rate);
10574 if (err)
10575 return err;
10578 setup.userspace_handles_dfs =
10579 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10581 if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10582 int r = validate_pae_over_nl80211(rdev, info);
10584 if (r < 0)
10585 return r;
10587 setup.control_port_over_nl80211 = true;
10590 wdev_lock(dev->ieee80211_ptr);
10591 err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
10592 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
10593 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10594 wdev_unlock(dev->ieee80211_ptr);
10596 return err;
10599 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
10601 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10602 struct net_device *dev = info->user_ptr[1];
10604 return cfg80211_leave_mesh(rdev, dev);
10607 #ifdef CONFIG_PM
10608 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
10609 struct cfg80211_registered_device *rdev)
10611 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
10612 struct nlattr *nl_pats, *nl_pat;
10613 int i, pat_len;
10615 if (!wowlan->n_patterns)
10616 return 0;
10618 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
10619 if (!nl_pats)
10620 return -ENOBUFS;
10622 for (i = 0; i < wowlan->n_patterns; i++) {
10623 nl_pat = nla_nest_start(msg, i + 1);
10624 if (!nl_pat)
10625 return -ENOBUFS;
10626 pat_len = wowlan->patterns[i].pattern_len;
10627 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
10628 wowlan->patterns[i].mask) ||
10629 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10630 wowlan->patterns[i].pattern) ||
10631 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10632 wowlan->patterns[i].pkt_offset))
10633 return -ENOBUFS;
10634 nla_nest_end(msg, nl_pat);
10636 nla_nest_end(msg, nl_pats);
10638 return 0;
10641 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
10642 struct cfg80211_wowlan_tcp *tcp)
10644 struct nlattr *nl_tcp;
10646 if (!tcp)
10647 return 0;
10649 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
10650 if (!nl_tcp)
10651 return -ENOBUFS;
10653 if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
10654 nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
10655 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
10656 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
10657 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
10658 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
10659 tcp->payload_len, tcp->payload) ||
10660 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
10661 tcp->data_interval) ||
10662 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
10663 tcp->wake_len, tcp->wake_data) ||
10664 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
10665 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
10666 return -ENOBUFS;
10668 if (tcp->payload_seq.len &&
10669 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
10670 sizeof(tcp->payload_seq), &tcp->payload_seq))
10671 return -ENOBUFS;
10673 if (tcp->payload_tok.len &&
10674 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
10675 sizeof(tcp->payload_tok) + tcp->tokens_size,
10676 &tcp->payload_tok))
10677 return -ENOBUFS;
10679 nla_nest_end(msg, nl_tcp);
10681 return 0;
10684 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
10685 struct cfg80211_sched_scan_request *req)
10687 struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
10688 int i;
10690 if (!req)
10691 return 0;
10693 nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
10694 if (!nd)
10695 return -ENOBUFS;
10697 if (req->n_scan_plans == 1 &&
10698 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
10699 req->scan_plans[0].interval * 1000))
10700 return -ENOBUFS;
10702 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
10703 return -ENOBUFS;
10705 if (req->relative_rssi_set) {
10706 struct nl80211_bss_select_rssi_adjust rssi_adjust;
10708 if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
10709 req->relative_rssi))
10710 return -ENOBUFS;
10712 rssi_adjust.band = req->rssi_adjust.band;
10713 rssi_adjust.delta = req->rssi_adjust.delta;
10714 if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
10715 sizeof(rssi_adjust), &rssi_adjust))
10716 return -ENOBUFS;
10719 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10720 if (!freqs)
10721 return -ENOBUFS;
10723 for (i = 0; i < req->n_channels; i++) {
10724 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10725 return -ENOBUFS;
10728 nla_nest_end(msg, freqs);
10730 if (req->n_match_sets) {
10731 matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
10732 if (!matches)
10733 return -ENOBUFS;
10735 for (i = 0; i < req->n_match_sets; i++) {
10736 match = nla_nest_start(msg, i);
10737 if (!match)
10738 return -ENOBUFS;
10740 if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
10741 req->match_sets[i].ssid.ssid_len,
10742 req->match_sets[i].ssid.ssid))
10743 return -ENOBUFS;
10744 nla_nest_end(msg, match);
10746 nla_nest_end(msg, matches);
10749 scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
10750 if (!scan_plans)
10751 return -ENOBUFS;
10753 for (i = 0; i < req->n_scan_plans; i++) {
10754 scan_plan = nla_nest_start(msg, i + 1);
10755 if (!scan_plan)
10756 return -ENOBUFS;
10758 if (!scan_plan ||
10759 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
10760 req->scan_plans[i].interval) ||
10761 (req->scan_plans[i].iterations &&
10762 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
10763 req->scan_plans[i].iterations)))
10764 return -ENOBUFS;
10765 nla_nest_end(msg, scan_plan);
10767 nla_nest_end(msg, scan_plans);
10769 nla_nest_end(msg, nd);
10771 return 0;
10774 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
10776 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10777 struct sk_buff *msg;
10778 void *hdr;
10779 u32 size = NLMSG_DEFAULT_SIZE;
10781 if (!rdev->wiphy.wowlan)
10782 return -EOPNOTSUPP;
10784 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
10785 /* adjust size to have room for all the data */
10786 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
10787 rdev->wiphy.wowlan_config->tcp->payload_len +
10788 rdev->wiphy.wowlan_config->tcp->wake_len +
10789 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
10792 msg = nlmsg_new(size, GFP_KERNEL);
10793 if (!msg)
10794 return -ENOMEM;
10796 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10797 NL80211_CMD_GET_WOWLAN);
10798 if (!hdr)
10799 goto nla_put_failure;
10801 if (rdev->wiphy.wowlan_config) {
10802 struct nlattr *nl_wowlan;
10804 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10805 if (!nl_wowlan)
10806 goto nla_put_failure;
10808 if ((rdev->wiphy.wowlan_config->any &&
10809 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
10810 (rdev->wiphy.wowlan_config->disconnect &&
10811 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
10812 (rdev->wiphy.wowlan_config->magic_pkt &&
10813 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
10814 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
10815 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
10816 (rdev->wiphy.wowlan_config->eap_identity_req &&
10817 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
10818 (rdev->wiphy.wowlan_config->four_way_handshake &&
10819 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
10820 (rdev->wiphy.wowlan_config->rfkill_release &&
10821 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
10822 goto nla_put_failure;
10824 if (nl80211_send_wowlan_patterns(msg, rdev))
10825 goto nla_put_failure;
10827 if (nl80211_send_wowlan_tcp(msg,
10828 rdev->wiphy.wowlan_config->tcp))
10829 goto nla_put_failure;
10831 if (nl80211_send_wowlan_nd(
10832 msg,
10833 rdev->wiphy.wowlan_config->nd_config))
10834 goto nla_put_failure;
10836 nla_nest_end(msg, nl_wowlan);
10839 genlmsg_end(msg, hdr);
10840 return genlmsg_reply(msg, info);
10842 nla_put_failure:
10843 nlmsg_free(msg);
10844 return -ENOBUFS;
10847 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
10848 struct nlattr *attr,
10849 struct cfg80211_wowlan *trig)
10851 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
10852 struct cfg80211_wowlan_tcp *cfg;
10853 struct nl80211_wowlan_tcp_data_token *tok = NULL;
10854 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
10855 u32 size;
10856 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
10857 int err, port;
10859 if (!rdev->wiphy.wowlan->tcp)
10860 return -EINVAL;
10862 err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TCP, attr,
10863 nl80211_wowlan_tcp_policy, NULL);
10864 if (err)
10865 return err;
10867 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
10868 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
10869 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
10870 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
10871 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
10872 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
10873 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
10874 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
10875 return -EINVAL;
10877 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
10878 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
10879 return -EINVAL;
10881 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
10882 rdev->wiphy.wowlan->tcp->data_interval_max ||
10883 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
10884 return -EINVAL;
10886 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
10887 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
10888 return -EINVAL;
10890 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
10891 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
10892 return -EINVAL;
10894 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
10895 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10897 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10898 tokens_size = tokln - sizeof(*tok);
10900 if (!tok->len || tokens_size % tok->len)
10901 return -EINVAL;
10902 if (!rdev->wiphy.wowlan->tcp->tok)
10903 return -EINVAL;
10904 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
10905 return -EINVAL;
10906 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
10907 return -EINVAL;
10908 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
10909 return -EINVAL;
10910 if (tok->offset + tok->len > data_size)
10911 return -EINVAL;
10914 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
10915 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
10916 if (!rdev->wiphy.wowlan->tcp->seq)
10917 return -EINVAL;
10918 if (seq->len == 0 || seq->len > 4)
10919 return -EINVAL;
10920 if (seq->len + seq->offset > data_size)
10921 return -EINVAL;
10924 size = sizeof(*cfg);
10925 size += data_size;
10926 size += wake_size + wake_mask_size;
10927 size += tokens_size;
10929 cfg = kzalloc(size, GFP_KERNEL);
10930 if (!cfg)
10931 return -ENOMEM;
10932 cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
10933 cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
10934 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
10935 ETH_ALEN);
10936 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
10937 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
10938 else
10939 port = 0;
10940 #ifdef CONFIG_INET
10941 /* allocate a socket and port for it and use it */
10942 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
10943 IPPROTO_TCP, &cfg->sock, 1);
10944 if (err) {
10945 kfree(cfg);
10946 return err;
10948 if (inet_csk_get_port(cfg->sock->sk, port)) {
10949 sock_release(cfg->sock);
10950 kfree(cfg);
10951 return -EADDRINUSE;
10953 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
10954 #else
10955 if (!port) {
10956 kfree(cfg);
10957 return -EINVAL;
10959 cfg->src_port = port;
10960 #endif
10962 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
10963 cfg->payload_len = data_size;
10964 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
10965 memcpy((void *)cfg->payload,
10966 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
10967 data_size);
10968 if (seq)
10969 cfg->payload_seq = *seq;
10970 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
10971 cfg->wake_len = wake_size;
10972 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
10973 memcpy((void *)cfg->wake_data,
10974 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
10975 wake_size);
10976 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
10977 data_size + wake_size;
10978 memcpy((void *)cfg->wake_mask,
10979 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
10980 wake_mask_size);
10981 if (tok) {
10982 cfg->tokens_size = tokens_size;
10983 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
10986 trig->tcp = cfg;
10988 return 0;
10991 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
10992 const struct wiphy_wowlan_support *wowlan,
10993 struct nlattr *attr,
10994 struct cfg80211_wowlan *trig)
10996 struct nlattr **tb;
10997 int err;
10999 tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11000 if (!tb)
11001 return -ENOMEM;
11003 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11004 err = -EOPNOTSUPP;
11005 goto out;
11008 err = nla_parse_nested(tb, NL80211_ATTR_MAX, attr, nl80211_policy,
11009 NULL);
11010 if (err)
11011 goto out;
11013 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11014 wowlan->max_nd_match_sets);
11015 err = PTR_ERR_OR_ZERO(trig->nd_config);
11016 if (err)
11017 trig->nd_config = NULL;
11019 out:
11020 kfree(tb);
11021 return err;
11024 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11027 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11028 struct cfg80211_wowlan new_triggers = {};
11029 struct cfg80211_wowlan *ntrig;
11030 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11031 int err, i;
11032 bool prev_enabled = rdev->wiphy.wowlan_config;
11033 bool regular = false;
11035 if (!wowlan)
11036 return -EOPNOTSUPP;
11038 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11039 cfg80211_rdev_free_wowlan(rdev);
11040 rdev->wiphy.wowlan_config = NULL;
11041 goto set_wakeup;
11044 err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TRIG,
11045 info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11046 nl80211_wowlan_policy, info->extack);
11047 if (err)
11048 return err;
11050 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11051 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11052 return -EINVAL;
11053 new_triggers.any = true;
11056 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11057 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11058 return -EINVAL;
11059 new_triggers.disconnect = true;
11060 regular = true;
11063 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11064 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11065 return -EINVAL;
11066 new_triggers.magic_pkt = true;
11067 regular = true;
11070 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11071 return -EINVAL;
11073 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11074 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11075 return -EINVAL;
11076 new_triggers.gtk_rekey_failure = true;
11077 regular = true;
11080 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11081 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11082 return -EINVAL;
11083 new_triggers.eap_identity_req = true;
11084 regular = true;
11087 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11088 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11089 return -EINVAL;
11090 new_triggers.four_way_handshake = true;
11091 regular = true;
11094 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11095 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11096 return -EINVAL;
11097 new_triggers.rfkill_release = true;
11098 regular = true;
11101 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11102 struct nlattr *pat;
11103 int n_patterns = 0;
11104 int rem, pat_len, mask_len, pkt_offset;
11105 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11107 regular = true;
11109 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11110 rem)
11111 n_patterns++;
11112 if (n_patterns > wowlan->n_patterns)
11113 return -EINVAL;
11115 new_triggers.patterns = kcalloc(n_patterns,
11116 sizeof(new_triggers.patterns[0]),
11117 GFP_KERNEL);
11118 if (!new_triggers.patterns)
11119 return -ENOMEM;
11121 new_triggers.n_patterns = n_patterns;
11122 i = 0;
11124 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11125 rem) {
11126 u8 *mask_pat;
11128 err = nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
11129 nl80211_packet_pattern_policy,
11130 info->extack);
11131 if (err)
11132 goto error;
11134 err = -EINVAL;
11135 if (!pat_tb[NL80211_PKTPAT_MASK] ||
11136 !pat_tb[NL80211_PKTPAT_PATTERN])
11137 goto error;
11138 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11139 mask_len = DIV_ROUND_UP(pat_len, 8);
11140 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11141 goto error;
11142 if (pat_len > wowlan->pattern_max_len ||
11143 pat_len < wowlan->pattern_min_len)
11144 goto error;
11146 if (!pat_tb[NL80211_PKTPAT_OFFSET])
11147 pkt_offset = 0;
11148 else
11149 pkt_offset = nla_get_u32(
11150 pat_tb[NL80211_PKTPAT_OFFSET]);
11151 if (pkt_offset > wowlan->max_pkt_offset)
11152 goto error;
11153 new_triggers.patterns[i].pkt_offset = pkt_offset;
11155 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11156 if (!mask_pat) {
11157 err = -ENOMEM;
11158 goto error;
11160 new_triggers.patterns[i].mask = mask_pat;
11161 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11162 mask_len);
11163 mask_pat += mask_len;
11164 new_triggers.patterns[i].pattern = mask_pat;
11165 new_triggers.patterns[i].pattern_len = pat_len;
11166 memcpy(mask_pat,
11167 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11168 pat_len);
11169 i++;
11173 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11174 regular = true;
11175 err = nl80211_parse_wowlan_tcp(
11176 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11177 &new_triggers);
11178 if (err)
11179 goto error;
11182 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11183 regular = true;
11184 err = nl80211_parse_wowlan_nd(
11185 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11186 &new_triggers);
11187 if (err)
11188 goto error;
11191 /* The 'any' trigger means the device continues operating more or less
11192 * as in its normal operation mode and wakes up the host on most of the
11193 * normal interrupts (like packet RX, ...)
11194 * It therefore makes little sense to combine with the more constrained
11195 * wakeup trigger modes.
11197 if (new_triggers.any && regular) {
11198 err = -EINVAL;
11199 goto error;
11202 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11203 if (!ntrig) {
11204 err = -ENOMEM;
11205 goto error;
11207 cfg80211_rdev_free_wowlan(rdev);
11208 rdev->wiphy.wowlan_config = ntrig;
11210 set_wakeup:
11211 if (rdev->ops->set_wakeup &&
11212 prev_enabled != !!rdev->wiphy.wowlan_config)
11213 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11215 return 0;
11216 error:
11217 for (i = 0; i < new_triggers.n_patterns; i++)
11218 kfree(new_triggers.patterns[i].mask);
11219 kfree(new_triggers.patterns);
11220 if (new_triggers.tcp && new_triggers.tcp->sock)
11221 sock_release(new_triggers.tcp->sock);
11222 kfree(new_triggers.tcp);
11223 kfree(new_triggers.nd_config);
11224 return err;
11226 #endif
11228 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11229 struct cfg80211_registered_device *rdev)
11231 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11232 int i, j, pat_len;
11233 struct cfg80211_coalesce_rules *rule;
11235 if (!rdev->coalesce->n_rules)
11236 return 0;
11238 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
11239 if (!nl_rules)
11240 return -ENOBUFS;
11242 for (i = 0; i < rdev->coalesce->n_rules; i++) {
11243 nl_rule = nla_nest_start(msg, i + 1);
11244 if (!nl_rule)
11245 return -ENOBUFS;
11247 rule = &rdev->coalesce->rules[i];
11248 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11249 rule->delay))
11250 return -ENOBUFS;
11252 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11253 rule->condition))
11254 return -ENOBUFS;
11256 nl_pats = nla_nest_start(msg,
11257 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11258 if (!nl_pats)
11259 return -ENOBUFS;
11261 for (j = 0; j < rule->n_patterns; j++) {
11262 nl_pat = nla_nest_start(msg, j + 1);
11263 if (!nl_pat)
11264 return -ENOBUFS;
11265 pat_len = rule->patterns[j].pattern_len;
11266 if (nla_put(msg, NL80211_PKTPAT_MASK,
11267 DIV_ROUND_UP(pat_len, 8),
11268 rule->patterns[j].mask) ||
11269 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11270 rule->patterns[j].pattern) ||
11271 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11272 rule->patterns[j].pkt_offset))
11273 return -ENOBUFS;
11274 nla_nest_end(msg, nl_pat);
11276 nla_nest_end(msg, nl_pats);
11277 nla_nest_end(msg, nl_rule);
11279 nla_nest_end(msg, nl_rules);
11281 return 0;
11284 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11287 struct sk_buff *msg;
11288 void *hdr;
11290 if (!rdev->wiphy.coalesce)
11291 return -EOPNOTSUPP;
11293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11294 if (!msg)
11295 return -ENOMEM;
11297 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11298 NL80211_CMD_GET_COALESCE);
11299 if (!hdr)
11300 goto nla_put_failure;
11302 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11303 goto nla_put_failure;
11305 genlmsg_end(msg, hdr);
11306 return genlmsg_reply(msg, info);
11308 nla_put_failure:
11309 nlmsg_free(msg);
11310 return -ENOBUFS;
11313 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11315 struct cfg80211_coalesce *coalesce = rdev->coalesce;
11316 int i, j;
11317 struct cfg80211_coalesce_rules *rule;
11319 if (!coalesce)
11320 return;
11322 for (i = 0; i < coalesce->n_rules; i++) {
11323 rule = &coalesce->rules[i];
11324 for (j = 0; j < rule->n_patterns; j++)
11325 kfree(rule->patterns[j].mask);
11326 kfree(rule->patterns);
11328 kfree(coalesce->rules);
11329 kfree(coalesce);
11330 rdev->coalesce = NULL;
11333 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11334 struct nlattr *rule,
11335 struct cfg80211_coalesce_rules *new_rule)
11337 int err, i;
11338 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11339 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11340 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11341 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11343 err = nla_parse_nested(tb, NL80211_ATTR_COALESCE_RULE_MAX, rule,
11344 nl80211_coalesce_policy, NULL);
11345 if (err)
11346 return err;
11348 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11349 new_rule->delay =
11350 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11351 if (new_rule->delay > coalesce->max_delay)
11352 return -EINVAL;
11354 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11355 new_rule->condition =
11356 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11357 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
11358 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
11359 return -EINVAL;
11361 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11362 return -EINVAL;
11364 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11365 rem)
11366 n_patterns++;
11367 if (n_patterns > coalesce->n_patterns)
11368 return -EINVAL;
11370 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11371 GFP_KERNEL);
11372 if (!new_rule->patterns)
11373 return -ENOMEM;
11375 new_rule->n_patterns = n_patterns;
11376 i = 0;
11378 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11379 rem) {
11380 u8 *mask_pat;
11382 err = nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
11383 nl80211_packet_pattern_policy, NULL);
11384 if (err)
11385 return err;
11387 if (!pat_tb[NL80211_PKTPAT_MASK] ||
11388 !pat_tb[NL80211_PKTPAT_PATTERN])
11389 return -EINVAL;
11390 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11391 mask_len = DIV_ROUND_UP(pat_len, 8);
11392 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11393 return -EINVAL;
11394 if (pat_len > coalesce->pattern_max_len ||
11395 pat_len < coalesce->pattern_min_len)
11396 return -EINVAL;
11398 if (!pat_tb[NL80211_PKTPAT_OFFSET])
11399 pkt_offset = 0;
11400 else
11401 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11402 if (pkt_offset > coalesce->max_pkt_offset)
11403 return -EINVAL;
11404 new_rule->patterns[i].pkt_offset = pkt_offset;
11406 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11407 if (!mask_pat)
11408 return -ENOMEM;
11410 new_rule->patterns[i].mask = mask_pat;
11411 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11412 mask_len);
11414 mask_pat += mask_len;
11415 new_rule->patterns[i].pattern = mask_pat;
11416 new_rule->patterns[i].pattern_len = pat_len;
11417 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11418 pat_len);
11419 i++;
11422 return 0;
11425 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11428 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11429 struct cfg80211_coalesce new_coalesce = {};
11430 struct cfg80211_coalesce *n_coalesce;
11431 int err, rem_rule, n_rules = 0, i, j;
11432 struct nlattr *rule;
11433 struct cfg80211_coalesce_rules *tmp_rule;
11435 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11436 return -EOPNOTSUPP;
11438 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11439 cfg80211_rdev_free_coalesce(rdev);
11440 rdev_set_coalesce(rdev, NULL);
11441 return 0;
11444 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11445 rem_rule)
11446 n_rules++;
11447 if (n_rules > coalesce->n_rules)
11448 return -EINVAL;
11450 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11451 GFP_KERNEL);
11452 if (!new_coalesce.rules)
11453 return -ENOMEM;
11455 new_coalesce.n_rules = n_rules;
11456 i = 0;
11458 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11459 rem_rule) {
11460 err = nl80211_parse_coalesce_rule(rdev, rule,
11461 &new_coalesce.rules[i]);
11462 if (err)
11463 goto error;
11465 i++;
11468 err = rdev_set_coalesce(rdev, &new_coalesce);
11469 if (err)
11470 goto error;
11472 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
11473 if (!n_coalesce) {
11474 err = -ENOMEM;
11475 goto error;
11477 cfg80211_rdev_free_coalesce(rdev);
11478 rdev->coalesce = n_coalesce;
11480 return 0;
11481 error:
11482 for (i = 0; i < new_coalesce.n_rules; i++) {
11483 tmp_rule = &new_coalesce.rules[i];
11484 for (j = 0; j < tmp_rule->n_patterns; j++)
11485 kfree(tmp_rule->patterns[j].mask);
11486 kfree(tmp_rule->patterns);
11488 kfree(new_coalesce.rules);
11490 return err;
11493 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
11495 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11496 struct net_device *dev = info->user_ptr[1];
11497 struct wireless_dev *wdev = dev->ieee80211_ptr;
11498 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
11499 struct cfg80211_gtk_rekey_data rekey_data;
11500 int err;
11502 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
11503 return -EINVAL;
11505 err = nla_parse_nested(tb, MAX_NL80211_REKEY_DATA,
11506 info->attrs[NL80211_ATTR_REKEY_DATA],
11507 nl80211_rekey_policy, info->extack);
11508 if (err)
11509 return err;
11511 if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
11512 !tb[NL80211_REKEY_DATA_KCK])
11513 return -EINVAL;
11514 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
11515 return -ERANGE;
11516 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
11517 return -ERANGE;
11518 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
11519 return -ERANGE;
11521 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
11522 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
11523 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
11525 wdev_lock(wdev);
11526 if (!wdev->current_bss) {
11527 err = -ENOTCONN;
11528 goto out;
11531 if (!rdev->ops->set_rekey_data) {
11532 err = -EOPNOTSUPP;
11533 goto out;
11536 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
11537 out:
11538 wdev_unlock(wdev);
11539 return err;
11542 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
11543 struct genl_info *info)
11545 struct net_device *dev = info->user_ptr[1];
11546 struct wireless_dev *wdev = dev->ieee80211_ptr;
11548 if (wdev->iftype != NL80211_IFTYPE_AP &&
11549 wdev->iftype != NL80211_IFTYPE_P2P_GO)
11550 return -EINVAL;
11552 if (wdev->ap_unexpected_nlportid)
11553 return -EBUSY;
11555 wdev->ap_unexpected_nlportid = info->snd_portid;
11556 return 0;
11559 static int nl80211_probe_client(struct sk_buff *skb,
11560 struct genl_info *info)
11562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11563 struct net_device *dev = info->user_ptr[1];
11564 struct wireless_dev *wdev = dev->ieee80211_ptr;
11565 struct sk_buff *msg;
11566 void *hdr;
11567 const u8 *addr;
11568 u64 cookie;
11569 int err;
11571 if (wdev->iftype != NL80211_IFTYPE_AP &&
11572 wdev->iftype != NL80211_IFTYPE_P2P_GO)
11573 return -EOPNOTSUPP;
11575 if (!info->attrs[NL80211_ATTR_MAC])
11576 return -EINVAL;
11578 if (!rdev->ops->probe_client)
11579 return -EOPNOTSUPP;
11581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11582 if (!msg)
11583 return -ENOMEM;
11585 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11586 NL80211_CMD_PROBE_CLIENT);
11587 if (!hdr) {
11588 err = -ENOBUFS;
11589 goto free_msg;
11592 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
11594 err = rdev_probe_client(rdev, dev, addr, &cookie);
11595 if (err)
11596 goto free_msg;
11598 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11599 NL80211_ATTR_PAD))
11600 goto nla_put_failure;
11602 genlmsg_end(msg, hdr);
11604 return genlmsg_reply(msg, info);
11606 nla_put_failure:
11607 err = -ENOBUFS;
11608 free_msg:
11609 nlmsg_free(msg);
11610 return err;
11613 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
11615 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11616 struct cfg80211_beacon_registration *reg, *nreg;
11617 int rv;
11619 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
11620 return -EOPNOTSUPP;
11622 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
11623 if (!nreg)
11624 return -ENOMEM;
11626 /* First, check if already registered. */
11627 spin_lock_bh(&rdev->beacon_registrations_lock);
11628 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11629 if (reg->nlportid == info->snd_portid) {
11630 rv = -EALREADY;
11631 goto out_err;
11634 /* Add it to the list */
11635 nreg->nlportid = info->snd_portid;
11636 list_add(&nreg->list, &rdev->beacon_registrations);
11638 spin_unlock_bh(&rdev->beacon_registrations_lock);
11640 return 0;
11641 out_err:
11642 spin_unlock_bh(&rdev->beacon_registrations_lock);
11643 kfree(nreg);
11644 return rv;
11647 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
11649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11650 struct wireless_dev *wdev = info->user_ptr[1];
11651 int err;
11653 if (!rdev->ops->start_p2p_device)
11654 return -EOPNOTSUPP;
11656 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11657 return -EOPNOTSUPP;
11659 if (wdev_running(wdev))
11660 return 0;
11662 if (rfkill_blocked(rdev->rfkill))
11663 return -ERFKILL;
11665 err = rdev_start_p2p_device(rdev, wdev);
11666 if (err)
11667 return err;
11669 wdev->is_running = true;
11670 rdev->opencount++;
11672 return 0;
11675 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
11677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11678 struct wireless_dev *wdev = info->user_ptr[1];
11680 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11681 return -EOPNOTSUPP;
11683 if (!rdev->ops->stop_p2p_device)
11684 return -EOPNOTSUPP;
11686 cfg80211_stop_p2p_device(rdev, wdev);
11688 return 0;
11691 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
11693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11694 struct wireless_dev *wdev = info->user_ptr[1];
11695 struct cfg80211_nan_conf conf = {};
11696 int err;
11698 if (wdev->iftype != NL80211_IFTYPE_NAN)
11699 return -EOPNOTSUPP;
11701 if (wdev_running(wdev))
11702 return -EEXIST;
11704 if (rfkill_blocked(rdev->rfkill))
11705 return -ERFKILL;
11707 if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
11708 return -EINVAL;
11710 conf.master_pref =
11711 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11712 if (!conf.master_pref)
11713 return -EINVAL;
11715 if (info->attrs[NL80211_ATTR_BANDS]) {
11716 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11718 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11719 return -EOPNOTSUPP;
11721 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11722 return -EINVAL;
11724 conf.bands = bands;
11727 err = rdev_start_nan(rdev, wdev, &conf);
11728 if (err)
11729 return err;
11731 wdev->is_running = true;
11732 rdev->opencount++;
11734 return 0;
11737 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
11739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11740 struct wireless_dev *wdev = info->user_ptr[1];
11742 if (wdev->iftype != NL80211_IFTYPE_NAN)
11743 return -EOPNOTSUPP;
11745 cfg80211_stop_nan(rdev, wdev);
11747 return 0;
11750 static int validate_nan_filter(struct nlattr *filter_attr)
11752 struct nlattr *attr;
11753 int len = 0, n_entries = 0, rem;
11755 nla_for_each_nested(attr, filter_attr, rem) {
11756 len += nla_len(attr);
11757 n_entries++;
11760 if (len >= U8_MAX)
11761 return -EINVAL;
11763 return n_entries;
11766 static int handle_nan_filter(struct nlattr *attr_filter,
11767 struct cfg80211_nan_func *func,
11768 bool tx)
11770 struct nlattr *attr;
11771 int n_entries, rem, i;
11772 struct cfg80211_nan_func_filter *filter;
11774 n_entries = validate_nan_filter(attr_filter);
11775 if (n_entries < 0)
11776 return n_entries;
11778 BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
11780 filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
11781 if (!filter)
11782 return -ENOMEM;
11784 i = 0;
11785 nla_for_each_nested(attr, attr_filter, rem) {
11786 filter[i].filter = nla_memdup(attr, GFP_KERNEL);
11787 filter[i].len = nla_len(attr);
11788 i++;
11790 if (tx) {
11791 func->num_tx_filters = n_entries;
11792 func->tx_filters = filter;
11793 } else {
11794 func->num_rx_filters = n_entries;
11795 func->rx_filters = filter;
11798 return 0;
11801 static int nl80211_nan_add_func(struct sk_buff *skb,
11802 struct genl_info *info)
11804 struct cfg80211_registered_device *rdev = info->user_ptr[0];
11805 struct wireless_dev *wdev = info->user_ptr[1];
11806 struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
11807 struct cfg80211_nan_func *func;
11808 struct sk_buff *msg = NULL;
11809 void *hdr = NULL;
11810 int err = 0;
11812 if (wdev->iftype != NL80211_IFTYPE_NAN)
11813 return -EOPNOTSUPP;
11815 if (!wdev_running(wdev))
11816 return -ENOTCONN;
11818 if (!info->attrs[NL80211_ATTR_NAN_FUNC])
11819 return -EINVAL;
11821 err = nla_parse_nested(tb, NL80211_NAN_FUNC_ATTR_MAX,
11822 info->attrs[NL80211_ATTR_NAN_FUNC],
11823 nl80211_nan_func_policy, info->extack);
11824 if (err)
11825 return err;
11827 func = kzalloc(sizeof(*func), GFP_KERNEL);
11828 if (!func)
11829 return -ENOMEM;
11831 func->cookie = wdev->wiphy->cookie_counter++;
11833 if (!tb[NL80211_NAN_FUNC_TYPE] ||
11834 nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
11835 err = -EINVAL;
11836 goto out;
11840 func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
11842 if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
11843 err = -EINVAL;
11844 goto out;
11847 memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
11848 sizeof(func->service_id));
11850 func->close_range =
11851 nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
11853 if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
11854 func->serv_spec_info_len =
11855 nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
11856 func->serv_spec_info =
11857 kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
11858 func->serv_spec_info_len,
11859 GFP_KERNEL);
11860 if (!func->serv_spec_info) {
11861 err = -ENOMEM;
11862 goto out;
11866 if (tb[NL80211_NAN_FUNC_TTL])
11867 func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
11869 switch (func->type) {
11870 case NL80211_NAN_FUNC_PUBLISH:
11871 if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
11872 err = -EINVAL;
11873 goto out;
11876 func->publish_type =
11877 nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
11878 func->publish_bcast =
11879 nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
11881 if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
11882 func->publish_bcast) {
11883 err = -EINVAL;
11884 goto out;
11886 break;
11887 case NL80211_NAN_FUNC_SUBSCRIBE:
11888 func->subscribe_active =
11889 nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
11890 break;
11891 case NL80211_NAN_FUNC_FOLLOW_UP:
11892 if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
11893 !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
11894 !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
11895 err = -EINVAL;
11896 goto out;
11899 func->followup_id =
11900 nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
11901 func->followup_reqid =
11902 nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
11903 memcpy(func->followup_dest.addr,
11904 nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
11905 sizeof(func->followup_dest.addr));
11906 if (func->ttl) {
11907 err = -EINVAL;
11908 goto out;
11910 break;
11911 default:
11912 err = -EINVAL;
11913 goto out;
11916 if (tb[NL80211_NAN_FUNC_SRF]) {
11917 struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
11919 err = nla_parse_nested(srf_tb, NL80211_NAN_SRF_ATTR_MAX,
11920 tb[NL80211_NAN_FUNC_SRF],
11921 nl80211_nan_srf_policy, info->extack);
11922 if (err)
11923 goto out;
11925 func->srf_include =
11926 nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
11928 if (srf_tb[NL80211_NAN_SRF_BF]) {
11929 if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
11930 !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
11931 err = -EINVAL;
11932 goto out;
11935 func->srf_bf_len =
11936 nla_len(srf_tb[NL80211_NAN_SRF_BF]);
11937 func->srf_bf =
11938 kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
11939 func->srf_bf_len, GFP_KERNEL);
11940 if (!func->srf_bf) {
11941 err = -ENOMEM;
11942 goto out;
11945 func->srf_bf_idx =
11946 nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
11947 } else {
11948 struct nlattr *attr, *mac_attr =
11949 srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
11950 int n_entries, rem, i = 0;
11952 if (!mac_attr) {
11953 err = -EINVAL;
11954 goto out;
11957 n_entries = validate_acl_mac_addrs(mac_attr);
11958 if (n_entries <= 0) {
11959 err = -EINVAL;
11960 goto out;
11963 func->srf_num_macs = n_entries;
11964 func->srf_macs =
11965 kcalloc(n_entries, sizeof(*func->srf_macs),
11966 GFP_KERNEL);
11967 if (!func->srf_macs) {
11968 err = -ENOMEM;
11969 goto out;
11972 nla_for_each_nested(attr, mac_attr, rem)
11973 memcpy(func->srf_macs[i++].addr, nla_data(attr),
11974 sizeof(*func->srf_macs));
11978 if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
11979 err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
11980 func, true);
11981 if (err)
11982 goto out;
11985 if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
11986 err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
11987 func, false);
11988 if (err)
11989 goto out;
11992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11993 if (!msg) {
11994 err = -ENOMEM;
11995 goto out;
11998 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11999 NL80211_CMD_ADD_NAN_FUNCTION);
12000 /* This can't really happen - we just allocated 4KB */
12001 if (WARN_ON(!hdr)) {
12002 err = -ENOMEM;
12003 goto out;
12006 err = rdev_add_nan_func(rdev, wdev, func);
12007 out:
12008 if (err < 0) {
12009 cfg80211_free_nan_func(func);
12010 nlmsg_free(msg);
12011 return err;
12014 /* propagate the instance id and cookie to userspace */
12015 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12016 NL80211_ATTR_PAD))
12017 goto nla_put_failure;
12019 func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
12020 if (!func_attr)
12021 goto nla_put_failure;
12023 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12024 func->instance_id))
12025 goto nla_put_failure;
12027 nla_nest_end(msg, func_attr);
12029 genlmsg_end(msg, hdr);
12030 return genlmsg_reply(msg, info);
12032 nla_put_failure:
12033 nlmsg_free(msg);
12034 return -ENOBUFS;
12037 static int nl80211_nan_del_func(struct sk_buff *skb,
12038 struct genl_info *info)
12040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12041 struct wireless_dev *wdev = info->user_ptr[1];
12042 u64 cookie;
12044 if (wdev->iftype != NL80211_IFTYPE_NAN)
12045 return -EOPNOTSUPP;
12047 if (!wdev_running(wdev))
12048 return -ENOTCONN;
12050 if (!info->attrs[NL80211_ATTR_COOKIE])
12051 return -EINVAL;
12053 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12055 rdev_del_nan_func(rdev, wdev, cookie);
12057 return 0;
12060 static int nl80211_nan_change_config(struct sk_buff *skb,
12061 struct genl_info *info)
12063 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12064 struct wireless_dev *wdev = info->user_ptr[1];
12065 struct cfg80211_nan_conf conf = {};
12066 u32 changed = 0;
12068 if (wdev->iftype != NL80211_IFTYPE_NAN)
12069 return -EOPNOTSUPP;
12071 if (!wdev_running(wdev))
12072 return -ENOTCONN;
12074 if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12075 conf.master_pref =
12076 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12077 if (conf.master_pref <= 1 || conf.master_pref == 255)
12078 return -EINVAL;
12080 changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12083 if (info->attrs[NL80211_ATTR_BANDS]) {
12084 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12086 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12087 return -EOPNOTSUPP;
12089 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12090 return -EINVAL;
12092 conf.bands = bands;
12093 changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12096 if (!changed)
12097 return -EINVAL;
12099 return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12102 void cfg80211_nan_match(struct wireless_dev *wdev,
12103 struct cfg80211_nan_match_params *match, gfp_t gfp)
12105 struct wiphy *wiphy = wdev->wiphy;
12106 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12107 struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12108 struct sk_buff *msg;
12109 void *hdr;
12111 if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12112 return;
12114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12115 if (!msg)
12116 return;
12118 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12119 if (!hdr) {
12120 nlmsg_free(msg);
12121 return;
12124 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12125 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12126 wdev->netdev->ifindex)) ||
12127 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12128 NL80211_ATTR_PAD))
12129 goto nla_put_failure;
12131 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12132 NL80211_ATTR_PAD) ||
12133 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12134 goto nla_put_failure;
12136 match_attr = nla_nest_start(msg, NL80211_ATTR_NAN_MATCH);
12137 if (!match_attr)
12138 goto nla_put_failure;
12140 local_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_LOCAL);
12141 if (!local_func_attr)
12142 goto nla_put_failure;
12144 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12145 goto nla_put_failure;
12147 nla_nest_end(msg, local_func_attr);
12149 peer_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_PEER);
12150 if (!peer_func_attr)
12151 goto nla_put_failure;
12153 if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12154 nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12155 goto nla_put_failure;
12157 if (match->info && match->info_len &&
12158 nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12159 match->info))
12160 goto nla_put_failure;
12162 nla_nest_end(msg, peer_func_attr);
12163 nla_nest_end(msg, match_attr);
12164 genlmsg_end(msg, hdr);
12166 if (!wdev->owner_nlportid)
12167 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12168 msg, 0, NL80211_MCGRP_NAN, gfp);
12169 else
12170 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12171 wdev->owner_nlportid);
12173 return;
12175 nla_put_failure:
12176 nlmsg_free(msg);
12178 EXPORT_SYMBOL(cfg80211_nan_match);
12180 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12181 u8 inst_id,
12182 enum nl80211_nan_func_term_reason reason,
12183 u64 cookie, gfp_t gfp)
12185 struct wiphy *wiphy = wdev->wiphy;
12186 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12187 struct sk_buff *msg;
12188 struct nlattr *func_attr;
12189 void *hdr;
12191 if (WARN_ON(!inst_id))
12192 return;
12194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12195 if (!msg)
12196 return;
12198 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12199 if (!hdr) {
12200 nlmsg_free(msg);
12201 return;
12204 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12205 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12206 wdev->netdev->ifindex)) ||
12207 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12208 NL80211_ATTR_PAD))
12209 goto nla_put_failure;
12211 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12212 NL80211_ATTR_PAD))
12213 goto nla_put_failure;
12215 func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
12216 if (!func_attr)
12217 goto nla_put_failure;
12219 if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12220 nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12221 goto nla_put_failure;
12223 nla_nest_end(msg, func_attr);
12224 genlmsg_end(msg, hdr);
12226 if (!wdev->owner_nlportid)
12227 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12228 msg, 0, NL80211_MCGRP_NAN, gfp);
12229 else
12230 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12231 wdev->owner_nlportid);
12233 return;
12235 nla_put_failure:
12236 nlmsg_free(msg);
12238 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12240 static int nl80211_get_protocol_features(struct sk_buff *skb,
12241 struct genl_info *info)
12243 void *hdr;
12244 struct sk_buff *msg;
12246 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12247 if (!msg)
12248 return -ENOMEM;
12250 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12251 NL80211_CMD_GET_PROTOCOL_FEATURES);
12252 if (!hdr)
12253 goto nla_put_failure;
12255 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12256 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12257 goto nla_put_failure;
12259 genlmsg_end(msg, hdr);
12260 return genlmsg_reply(msg, info);
12262 nla_put_failure:
12263 kfree_skb(msg);
12264 return -ENOBUFS;
12267 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12269 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12270 struct cfg80211_update_ft_ies_params ft_params;
12271 struct net_device *dev = info->user_ptr[1];
12273 if (!rdev->ops->update_ft_ies)
12274 return -EOPNOTSUPP;
12276 if (!info->attrs[NL80211_ATTR_MDID] ||
12277 !info->attrs[NL80211_ATTR_IE] ||
12278 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
12279 return -EINVAL;
12281 memset(&ft_params, 0, sizeof(ft_params));
12282 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12283 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12284 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12286 return rdev_update_ft_ies(rdev, dev, &ft_params);
12289 static int nl80211_crit_protocol_start(struct sk_buff *skb,
12290 struct genl_info *info)
12292 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12293 struct wireless_dev *wdev = info->user_ptr[1];
12294 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12295 u16 duration;
12296 int ret;
12298 if (!rdev->ops->crit_proto_start)
12299 return -EOPNOTSUPP;
12301 if (WARN_ON(!rdev->ops->crit_proto_stop))
12302 return -EINVAL;
12304 if (rdev->crit_proto_nlportid)
12305 return -EBUSY;
12307 /* determine protocol if provided */
12308 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12309 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12311 if (proto >= NUM_NL80211_CRIT_PROTO)
12312 return -EINVAL;
12314 /* timeout must be provided */
12315 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12316 return -EINVAL;
12318 duration =
12319 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12321 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12322 return -ERANGE;
12324 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12325 if (!ret)
12326 rdev->crit_proto_nlportid = info->snd_portid;
12328 return ret;
12331 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12332 struct genl_info *info)
12334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12335 struct wireless_dev *wdev = info->user_ptr[1];
12337 if (!rdev->ops->crit_proto_stop)
12338 return -EOPNOTSUPP;
12340 if (rdev->crit_proto_nlportid) {
12341 rdev->crit_proto_nlportid = 0;
12342 rdev_crit_proto_stop(rdev, wdev);
12344 return 0;
12347 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12350 struct wireless_dev *wdev =
12351 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12352 int i, err;
12353 u32 vid, subcmd;
12355 if (!rdev->wiphy.vendor_commands)
12356 return -EOPNOTSUPP;
12358 if (IS_ERR(wdev)) {
12359 err = PTR_ERR(wdev);
12360 if (err != -EINVAL)
12361 return err;
12362 wdev = NULL;
12363 } else if (wdev->wiphy != &rdev->wiphy) {
12364 return -EINVAL;
12367 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12368 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12369 return -EINVAL;
12371 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12372 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12373 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12374 const struct wiphy_vendor_command *vcmd;
12375 void *data = NULL;
12376 int len = 0;
12378 vcmd = &rdev->wiphy.vendor_commands[i];
12380 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12381 continue;
12383 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12384 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12385 if (!wdev)
12386 return -EINVAL;
12387 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12388 !wdev->netdev)
12389 return -EINVAL;
12391 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12392 if (!wdev_running(wdev))
12393 return -ENETDOWN;
12396 if (!vcmd->doit)
12397 return -EOPNOTSUPP;
12398 } else {
12399 wdev = NULL;
12402 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12403 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12404 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12407 rdev->cur_cmd_info = info;
12408 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
12409 data, len);
12410 rdev->cur_cmd_info = NULL;
12411 return err;
12414 return -EOPNOTSUPP;
12417 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12418 struct netlink_callback *cb,
12419 struct cfg80211_registered_device **rdev,
12420 struct wireless_dev **wdev)
12422 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
12423 u32 vid, subcmd;
12424 unsigned int i;
12425 int vcmd_idx = -1;
12426 int err;
12427 void *data = NULL;
12428 unsigned int data_len = 0;
12430 if (cb->args[0]) {
12431 /* subtract the 1 again here */
12432 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12433 struct wireless_dev *tmp;
12435 if (!wiphy)
12436 return -ENODEV;
12437 *rdev = wiphy_to_rdev(wiphy);
12438 *wdev = NULL;
12440 if (cb->args[1]) {
12441 list_for_each_entry(tmp, &wiphy->wdev_list, list) {
12442 if (tmp->identifier == cb->args[1] - 1) {
12443 *wdev = tmp;
12444 break;
12449 /* keep rtnl locked in successful case */
12450 return 0;
12453 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, attrbuf,
12454 nl80211_fam.maxattr, nl80211_policy, NULL);
12455 if (err)
12456 return err;
12458 if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
12459 !attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
12460 return -EINVAL;
12462 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
12463 if (IS_ERR(*wdev))
12464 *wdev = NULL;
12466 *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
12467 if (IS_ERR(*rdev))
12468 return PTR_ERR(*rdev);
12470 vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
12471 subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
12473 for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
12474 const struct wiphy_vendor_command *vcmd;
12476 vcmd = &(*rdev)->wiphy.vendor_commands[i];
12478 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12479 continue;
12481 if (!vcmd->dumpit)
12482 return -EOPNOTSUPP;
12484 vcmd_idx = i;
12485 break;
12488 if (vcmd_idx < 0)
12489 return -EOPNOTSUPP;
12491 if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
12492 data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12493 data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
12496 /* 0 is the first index - add 1 to parse only once */
12497 cb->args[0] = (*rdev)->wiphy_idx + 1;
12498 /* add 1 to know if it was NULL */
12499 cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
12500 cb->args[2] = vcmd_idx;
12501 cb->args[3] = (unsigned long)data;
12502 cb->args[4] = data_len;
12504 /* keep rtnl locked in successful case */
12505 return 0;
12508 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
12509 struct netlink_callback *cb)
12511 struct cfg80211_registered_device *rdev;
12512 struct wireless_dev *wdev;
12513 unsigned int vcmd_idx;
12514 const struct wiphy_vendor_command *vcmd;
12515 void *data;
12516 int data_len;
12517 int err;
12518 struct nlattr *vendor_data;
12520 rtnl_lock();
12521 err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
12522 if (err)
12523 goto out;
12525 vcmd_idx = cb->args[2];
12526 data = (void *)cb->args[3];
12527 data_len = cb->args[4];
12528 vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
12530 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12531 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12532 if (!wdev) {
12533 err = -EINVAL;
12534 goto out;
12536 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12537 !wdev->netdev) {
12538 err = -EINVAL;
12539 goto out;
12542 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12543 if (!wdev_running(wdev)) {
12544 err = -ENETDOWN;
12545 goto out;
12550 while (1) {
12551 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
12552 cb->nlh->nlmsg_seq, NLM_F_MULTI,
12553 NL80211_CMD_VENDOR);
12554 if (!hdr)
12555 break;
12557 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12558 (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
12559 wdev_id(wdev),
12560 NL80211_ATTR_PAD))) {
12561 genlmsg_cancel(skb, hdr);
12562 break;
12565 vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
12566 if (!vendor_data) {
12567 genlmsg_cancel(skb, hdr);
12568 break;
12571 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
12572 (unsigned long *)&cb->args[5]);
12573 nla_nest_end(skb, vendor_data);
12575 if (err == -ENOBUFS || err == -ENOENT) {
12576 genlmsg_cancel(skb, hdr);
12577 break;
12578 } else if (err) {
12579 genlmsg_cancel(skb, hdr);
12580 goto out;
12583 genlmsg_end(skb, hdr);
12586 err = skb->len;
12587 out:
12588 rtnl_unlock();
12589 return err;
12592 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
12593 enum nl80211_commands cmd,
12594 enum nl80211_attrs attr,
12595 int approxlen)
12597 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12599 if (WARN_ON(!rdev->cur_cmd_info))
12600 return NULL;
12602 return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
12603 rdev->cur_cmd_info->snd_portid,
12604 rdev->cur_cmd_info->snd_seq,
12605 cmd, attr, NULL, GFP_KERNEL);
12607 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
12609 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
12611 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
12612 void *hdr = ((void **)skb->cb)[1];
12613 struct nlattr *data = ((void **)skb->cb)[2];
12615 /* clear CB data for netlink core to own from now on */
12616 memset(skb->cb, 0, sizeof(skb->cb));
12618 if (WARN_ON(!rdev->cur_cmd_info)) {
12619 kfree_skb(skb);
12620 return -EINVAL;
12623 nla_nest_end(skb, data);
12624 genlmsg_end(skb, hdr);
12625 return genlmsg_reply(skb, rdev->cur_cmd_info);
12627 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
12629 static int nl80211_set_qos_map(struct sk_buff *skb,
12630 struct genl_info *info)
12632 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12633 struct cfg80211_qos_map *qos_map = NULL;
12634 struct net_device *dev = info->user_ptr[1];
12635 u8 *pos, len, num_des, des_len, des;
12636 int ret;
12638 if (!rdev->ops->set_qos_map)
12639 return -EOPNOTSUPP;
12641 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
12642 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
12643 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
12645 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
12646 len > IEEE80211_QOS_MAP_LEN_MAX)
12647 return -EINVAL;
12649 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
12650 if (!qos_map)
12651 return -ENOMEM;
12653 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
12654 if (num_des) {
12655 des_len = num_des *
12656 sizeof(struct cfg80211_dscp_exception);
12657 memcpy(qos_map->dscp_exception, pos, des_len);
12658 qos_map->num_des = num_des;
12659 for (des = 0; des < num_des; des++) {
12660 if (qos_map->dscp_exception[des].up > 7) {
12661 kfree(qos_map);
12662 return -EINVAL;
12665 pos += des_len;
12667 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
12670 wdev_lock(dev->ieee80211_ptr);
12671 ret = nl80211_key_allowed(dev->ieee80211_ptr);
12672 if (!ret)
12673 ret = rdev_set_qos_map(rdev, dev, qos_map);
12674 wdev_unlock(dev->ieee80211_ptr);
12676 kfree(qos_map);
12677 return ret;
12680 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
12682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12683 struct net_device *dev = info->user_ptr[1];
12684 struct wireless_dev *wdev = dev->ieee80211_ptr;
12685 const u8 *peer;
12686 u8 tsid, up;
12687 u16 admitted_time = 0;
12688 int err;
12690 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
12691 return -EOPNOTSUPP;
12693 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
12694 !info->attrs[NL80211_ATTR_USER_PRIO])
12695 return -EINVAL;
12697 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12698 if (tsid >= IEEE80211_NUM_TIDS)
12699 return -EINVAL;
12701 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
12702 if (up >= IEEE80211_NUM_UPS)
12703 return -EINVAL;
12705 /* WMM uses TIDs 0-7 even for TSPEC */
12706 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
12707 /* TODO: handle 802.11 TSPEC/admission control
12708 * need more attributes for that (e.g. BA session requirement);
12709 * change the WMM adminssion test above to allow both then
12711 return -EINVAL;
12714 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12716 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
12717 admitted_time =
12718 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
12719 if (!admitted_time)
12720 return -EINVAL;
12723 wdev_lock(wdev);
12724 switch (wdev->iftype) {
12725 case NL80211_IFTYPE_STATION:
12726 case NL80211_IFTYPE_P2P_CLIENT:
12727 if (wdev->current_bss)
12728 break;
12729 err = -ENOTCONN;
12730 goto out;
12731 default:
12732 err = -EOPNOTSUPP;
12733 goto out;
12736 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
12738 out:
12739 wdev_unlock(wdev);
12740 return err;
12743 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
12745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12746 struct net_device *dev = info->user_ptr[1];
12747 struct wireless_dev *wdev = dev->ieee80211_ptr;
12748 const u8 *peer;
12749 u8 tsid;
12750 int err;
12752 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
12753 return -EINVAL;
12755 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12756 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12758 wdev_lock(wdev);
12759 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
12760 wdev_unlock(wdev);
12762 return err;
12765 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
12766 struct genl_info *info)
12768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12769 struct net_device *dev = info->user_ptr[1];
12770 struct wireless_dev *wdev = dev->ieee80211_ptr;
12771 struct cfg80211_chan_def chandef = {};
12772 const u8 *addr;
12773 u8 oper_class;
12774 int err;
12776 if (!rdev->ops->tdls_channel_switch ||
12777 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12778 return -EOPNOTSUPP;
12780 switch (dev->ieee80211_ptr->iftype) {
12781 case NL80211_IFTYPE_STATION:
12782 case NL80211_IFTYPE_P2P_CLIENT:
12783 break;
12784 default:
12785 return -EOPNOTSUPP;
12788 if (!info->attrs[NL80211_ATTR_MAC] ||
12789 !info->attrs[NL80211_ATTR_OPER_CLASS])
12790 return -EINVAL;
12792 err = nl80211_parse_chandef(rdev, info, &chandef);
12793 if (err)
12794 return err;
12797 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
12798 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
12799 * specification is not defined for them.
12801 if (chandef.chan->band == NL80211_BAND_2GHZ &&
12802 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
12803 chandef.width != NL80211_CHAN_WIDTH_20)
12804 return -EINVAL;
12806 /* we will be active on the TDLS link */
12807 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
12808 wdev->iftype))
12809 return -EINVAL;
12811 /* don't allow switching to DFS channels */
12812 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
12813 return -EINVAL;
12815 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12816 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
12818 wdev_lock(wdev);
12819 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
12820 wdev_unlock(wdev);
12822 return err;
12825 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
12826 struct genl_info *info)
12828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12829 struct net_device *dev = info->user_ptr[1];
12830 struct wireless_dev *wdev = dev->ieee80211_ptr;
12831 const u8 *addr;
12833 if (!rdev->ops->tdls_channel_switch ||
12834 !rdev->ops->tdls_cancel_channel_switch ||
12835 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12836 return -EOPNOTSUPP;
12838 switch (dev->ieee80211_ptr->iftype) {
12839 case NL80211_IFTYPE_STATION:
12840 case NL80211_IFTYPE_P2P_CLIENT:
12841 break;
12842 default:
12843 return -EOPNOTSUPP;
12846 if (!info->attrs[NL80211_ATTR_MAC])
12847 return -EINVAL;
12849 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12851 wdev_lock(wdev);
12852 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
12853 wdev_unlock(wdev);
12855 return 0;
12858 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
12859 struct genl_info *info)
12861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12862 struct net_device *dev = info->user_ptr[1];
12863 struct wireless_dev *wdev = dev->ieee80211_ptr;
12864 const struct nlattr *nla;
12865 bool enabled;
12867 if (!rdev->ops->set_multicast_to_unicast)
12868 return -EOPNOTSUPP;
12870 if (wdev->iftype != NL80211_IFTYPE_AP &&
12871 wdev->iftype != NL80211_IFTYPE_P2P_GO)
12872 return -EOPNOTSUPP;
12874 nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
12875 enabled = nla_get_flag(nla);
12877 return rdev_set_multicast_to_unicast(rdev, dev, enabled);
12880 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
12882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12883 struct net_device *dev = info->user_ptr[1];
12884 struct wireless_dev *wdev = dev->ieee80211_ptr;
12885 struct cfg80211_pmk_conf pmk_conf = {};
12886 int ret;
12888 if (wdev->iftype != NL80211_IFTYPE_STATION &&
12889 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12890 return -EOPNOTSUPP;
12892 if (!wiphy_ext_feature_isset(&rdev->wiphy,
12893 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12894 return -EOPNOTSUPP;
12896 if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
12897 return -EINVAL;
12899 wdev_lock(wdev);
12900 if (!wdev->current_bss) {
12901 ret = -ENOTCONN;
12902 goto out;
12905 pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12906 if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
12907 ret = -EINVAL;
12908 goto out;
12911 pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12912 pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12913 if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
12914 pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
12915 ret = -EINVAL;
12916 goto out;
12919 if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
12920 int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12922 if (r0_name_len != WLAN_PMK_NAME_LEN) {
12923 ret = -EINVAL;
12924 goto out;
12927 pmk_conf.pmk_r0_name =
12928 nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12931 ret = rdev_set_pmk(rdev, dev, &pmk_conf);
12932 out:
12933 wdev_unlock(wdev);
12934 return ret;
12937 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
12939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12940 struct net_device *dev = info->user_ptr[1];
12941 struct wireless_dev *wdev = dev->ieee80211_ptr;
12942 const u8 *aa;
12943 int ret;
12945 if (wdev->iftype != NL80211_IFTYPE_STATION &&
12946 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12947 return -EOPNOTSUPP;
12949 if (!wiphy_ext_feature_isset(&rdev->wiphy,
12950 NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12951 return -EOPNOTSUPP;
12953 if (!info->attrs[NL80211_ATTR_MAC])
12954 return -EINVAL;
12956 wdev_lock(wdev);
12957 aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12958 ret = rdev_del_pmk(rdev, dev, aa);
12959 wdev_unlock(wdev);
12961 return ret;
12964 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
12966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
12967 struct net_device *dev = info->user_ptr[1];
12968 struct cfg80211_external_auth_params params;
12970 if (!rdev->ops->external_auth)
12971 return -EOPNOTSUPP;
12973 if (!info->attrs[NL80211_ATTR_SSID])
12974 return -EINVAL;
12976 if (!info->attrs[NL80211_ATTR_BSSID])
12977 return -EINVAL;
12979 if (!info->attrs[NL80211_ATTR_STATUS_CODE])
12980 return -EINVAL;
12982 memset(&params, 0, sizeof(params));
12984 params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
12985 if (params.ssid.ssid_len == 0 ||
12986 params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
12987 return -EINVAL;
12988 memcpy(params.ssid.ssid, nla_data(info->attrs[NL80211_ATTR_SSID]),
12989 params.ssid.ssid_len);
12991 memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
12992 ETH_ALEN);
12994 params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12996 return rdev_external_auth(rdev, dev, &params);
12999 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
13002 struct net_device *dev = info->user_ptr[1];
13003 struct wireless_dev *wdev = dev->ieee80211_ptr;
13004 const u8 *buf;
13005 size_t len;
13006 u8 *dest;
13007 u16 proto;
13008 bool noencrypt;
13009 int err;
13011 if (!wiphy_ext_feature_isset(&rdev->wiphy,
13012 NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13013 return -EOPNOTSUPP;
13015 if (!rdev->ops->tx_control_port)
13016 return -EOPNOTSUPP;
13018 if (!info->attrs[NL80211_ATTR_FRAME] ||
13019 !info->attrs[NL80211_ATTR_MAC] ||
13020 !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13021 GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13022 return -EINVAL;
13025 wdev_lock(wdev);
13027 switch (wdev->iftype) {
13028 case NL80211_IFTYPE_AP:
13029 case NL80211_IFTYPE_P2P_GO:
13030 case NL80211_IFTYPE_MESH_POINT:
13031 break;
13032 case NL80211_IFTYPE_ADHOC:
13033 case NL80211_IFTYPE_STATION:
13034 case NL80211_IFTYPE_P2P_CLIENT:
13035 if (wdev->current_bss)
13036 break;
13037 err = -ENOTCONN;
13038 goto out;
13039 default:
13040 err = -EOPNOTSUPP;
13041 goto out;
13044 wdev_unlock(wdev);
13046 buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13047 len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13048 dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13049 proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13050 noencrypt =
13051 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13053 return rdev_tx_control_port(rdev, dev, buf, len,
13054 dest, cpu_to_be16(proto), noencrypt);
13056 out:
13057 wdev_unlock(wdev);
13058 return err;
13061 #define NL80211_FLAG_NEED_WIPHY 0x01
13062 #define NL80211_FLAG_NEED_NETDEV 0x02
13063 #define NL80211_FLAG_NEED_RTNL 0x04
13064 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
13065 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
13066 NL80211_FLAG_CHECK_NETDEV_UP)
13067 #define NL80211_FLAG_NEED_WDEV 0x10
13068 /* If a netdev is associated, it must be UP, P2P must be started */
13069 #define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
13070 NL80211_FLAG_CHECK_NETDEV_UP)
13071 #define NL80211_FLAG_CLEAR_SKB 0x20
13073 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13074 struct genl_info *info)
13076 struct cfg80211_registered_device *rdev;
13077 struct wireless_dev *wdev;
13078 struct net_device *dev;
13079 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13081 if (rtnl)
13082 rtnl_lock();
13084 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13085 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13086 if (IS_ERR(rdev)) {
13087 if (rtnl)
13088 rtnl_unlock();
13089 return PTR_ERR(rdev);
13091 info->user_ptr[0] = rdev;
13092 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13093 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13094 ASSERT_RTNL();
13096 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13097 info->attrs);
13098 if (IS_ERR(wdev)) {
13099 if (rtnl)
13100 rtnl_unlock();
13101 return PTR_ERR(wdev);
13104 dev = wdev->netdev;
13105 rdev = wiphy_to_rdev(wdev->wiphy);
13107 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13108 if (!dev) {
13109 if (rtnl)
13110 rtnl_unlock();
13111 return -EINVAL;
13114 info->user_ptr[1] = dev;
13115 } else {
13116 info->user_ptr[1] = wdev;
13119 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13120 !wdev_running(wdev)) {
13121 if (rtnl)
13122 rtnl_unlock();
13123 return -ENETDOWN;
13126 if (dev)
13127 dev_hold(dev);
13129 info->user_ptr[0] = rdev;
13132 return 0;
13135 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13136 struct genl_info *info)
13138 if (info->user_ptr[1]) {
13139 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13140 struct wireless_dev *wdev = info->user_ptr[1];
13142 if (wdev->netdev)
13143 dev_put(wdev->netdev);
13144 } else {
13145 dev_put(info->user_ptr[1]);
13149 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13150 rtnl_unlock();
13152 /* If needed, clear the netlink message payload from the SKB
13153 * as it might contain key data that shouldn't stick around on
13154 * the heap after the SKB is freed. The netlink message header
13155 * is still needed for further processing, so leave it intact.
13157 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13158 struct nlmsghdr *nlh = nlmsg_hdr(skb);
13160 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13164 static const struct genl_ops nl80211_ops[] = {
13166 .cmd = NL80211_CMD_GET_WIPHY,
13167 .doit = nl80211_get_wiphy,
13168 .dumpit = nl80211_dump_wiphy,
13169 .done = nl80211_dump_wiphy_done,
13170 .policy = nl80211_policy,
13171 /* can be retrieved by unprivileged users */
13172 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13173 NL80211_FLAG_NEED_RTNL,
13176 .cmd = NL80211_CMD_SET_WIPHY,
13177 .doit = nl80211_set_wiphy,
13178 .policy = nl80211_policy,
13179 .flags = GENL_UNS_ADMIN_PERM,
13180 .internal_flags = NL80211_FLAG_NEED_RTNL,
13183 .cmd = NL80211_CMD_GET_INTERFACE,
13184 .doit = nl80211_get_interface,
13185 .dumpit = nl80211_dump_interface,
13186 .policy = nl80211_policy,
13187 /* can be retrieved by unprivileged users */
13188 .internal_flags = NL80211_FLAG_NEED_WDEV |
13189 NL80211_FLAG_NEED_RTNL,
13192 .cmd = NL80211_CMD_SET_INTERFACE,
13193 .doit = nl80211_set_interface,
13194 .policy = nl80211_policy,
13195 .flags = GENL_UNS_ADMIN_PERM,
13196 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13197 NL80211_FLAG_NEED_RTNL,
13200 .cmd = NL80211_CMD_NEW_INTERFACE,
13201 .doit = nl80211_new_interface,
13202 .policy = nl80211_policy,
13203 .flags = GENL_UNS_ADMIN_PERM,
13204 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13205 NL80211_FLAG_NEED_RTNL,
13208 .cmd = NL80211_CMD_DEL_INTERFACE,
13209 .doit = nl80211_del_interface,
13210 .policy = nl80211_policy,
13211 .flags = GENL_UNS_ADMIN_PERM,
13212 .internal_flags = NL80211_FLAG_NEED_WDEV |
13213 NL80211_FLAG_NEED_RTNL,
13216 .cmd = NL80211_CMD_GET_KEY,
13217 .doit = nl80211_get_key,
13218 .policy = nl80211_policy,
13219 .flags = GENL_UNS_ADMIN_PERM,
13220 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13221 NL80211_FLAG_NEED_RTNL,
13224 .cmd = NL80211_CMD_SET_KEY,
13225 .doit = nl80211_set_key,
13226 .policy = nl80211_policy,
13227 .flags = GENL_UNS_ADMIN_PERM,
13228 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13229 NL80211_FLAG_NEED_RTNL |
13230 NL80211_FLAG_CLEAR_SKB,
13233 .cmd = NL80211_CMD_NEW_KEY,
13234 .doit = nl80211_new_key,
13235 .policy = nl80211_policy,
13236 .flags = GENL_UNS_ADMIN_PERM,
13237 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13238 NL80211_FLAG_NEED_RTNL |
13239 NL80211_FLAG_CLEAR_SKB,
13242 .cmd = NL80211_CMD_DEL_KEY,
13243 .doit = nl80211_del_key,
13244 .policy = nl80211_policy,
13245 .flags = GENL_UNS_ADMIN_PERM,
13246 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13247 NL80211_FLAG_NEED_RTNL,
13250 .cmd = NL80211_CMD_SET_BEACON,
13251 .policy = nl80211_policy,
13252 .flags = GENL_UNS_ADMIN_PERM,
13253 .doit = nl80211_set_beacon,
13254 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13255 NL80211_FLAG_NEED_RTNL,
13258 .cmd = NL80211_CMD_START_AP,
13259 .policy = nl80211_policy,
13260 .flags = GENL_UNS_ADMIN_PERM,
13261 .doit = nl80211_start_ap,
13262 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13263 NL80211_FLAG_NEED_RTNL,
13266 .cmd = NL80211_CMD_STOP_AP,
13267 .policy = nl80211_policy,
13268 .flags = GENL_UNS_ADMIN_PERM,
13269 .doit = nl80211_stop_ap,
13270 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13271 NL80211_FLAG_NEED_RTNL,
13274 .cmd = NL80211_CMD_GET_STATION,
13275 .doit = nl80211_get_station,
13276 .dumpit = nl80211_dump_station,
13277 .policy = nl80211_policy,
13278 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13279 NL80211_FLAG_NEED_RTNL,
13282 .cmd = NL80211_CMD_SET_STATION,
13283 .doit = nl80211_set_station,
13284 .policy = nl80211_policy,
13285 .flags = GENL_UNS_ADMIN_PERM,
13286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13287 NL80211_FLAG_NEED_RTNL,
13290 .cmd = NL80211_CMD_NEW_STATION,
13291 .doit = nl80211_new_station,
13292 .policy = nl80211_policy,
13293 .flags = GENL_UNS_ADMIN_PERM,
13294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13295 NL80211_FLAG_NEED_RTNL,
13298 .cmd = NL80211_CMD_DEL_STATION,
13299 .doit = nl80211_del_station,
13300 .policy = nl80211_policy,
13301 .flags = GENL_UNS_ADMIN_PERM,
13302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13303 NL80211_FLAG_NEED_RTNL,
13306 .cmd = NL80211_CMD_GET_MPATH,
13307 .doit = nl80211_get_mpath,
13308 .dumpit = nl80211_dump_mpath,
13309 .policy = nl80211_policy,
13310 .flags = GENL_UNS_ADMIN_PERM,
13311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13312 NL80211_FLAG_NEED_RTNL,
13315 .cmd = NL80211_CMD_GET_MPP,
13316 .doit = nl80211_get_mpp,
13317 .dumpit = nl80211_dump_mpp,
13318 .policy = nl80211_policy,
13319 .flags = GENL_UNS_ADMIN_PERM,
13320 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13321 NL80211_FLAG_NEED_RTNL,
13324 .cmd = NL80211_CMD_SET_MPATH,
13325 .doit = nl80211_set_mpath,
13326 .policy = nl80211_policy,
13327 .flags = GENL_UNS_ADMIN_PERM,
13328 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13329 NL80211_FLAG_NEED_RTNL,
13332 .cmd = NL80211_CMD_NEW_MPATH,
13333 .doit = nl80211_new_mpath,
13334 .policy = nl80211_policy,
13335 .flags = GENL_UNS_ADMIN_PERM,
13336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13337 NL80211_FLAG_NEED_RTNL,
13340 .cmd = NL80211_CMD_DEL_MPATH,
13341 .doit = nl80211_del_mpath,
13342 .policy = nl80211_policy,
13343 .flags = GENL_UNS_ADMIN_PERM,
13344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13345 NL80211_FLAG_NEED_RTNL,
13348 .cmd = NL80211_CMD_SET_BSS,
13349 .doit = nl80211_set_bss,
13350 .policy = nl80211_policy,
13351 .flags = GENL_UNS_ADMIN_PERM,
13352 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13353 NL80211_FLAG_NEED_RTNL,
13356 .cmd = NL80211_CMD_GET_REG,
13357 .doit = nl80211_get_reg_do,
13358 .dumpit = nl80211_get_reg_dump,
13359 .policy = nl80211_policy,
13360 .internal_flags = NL80211_FLAG_NEED_RTNL,
13361 /* can be retrieved by unprivileged users */
13363 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
13365 .cmd = NL80211_CMD_SET_REG,
13366 .doit = nl80211_set_reg,
13367 .policy = nl80211_policy,
13368 .flags = GENL_ADMIN_PERM,
13369 .internal_flags = NL80211_FLAG_NEED_RTNL,
13371 #endif
13373 .cmd = NL80211_CMD_REQ_SET_REG,
13374 .doit = nl80211_req_set_reg,
13375 .policy = nl80211_policy,
13376 .flags = GENL_ADMIN_PERM,
13379 .cmd = NL80211_CMD_RELOAD_REGDB,
13380 .doit = nl80211_reload_regdb,
13381 .policy = nl80211_policy,
13382 .flags = GENL_ADMIN_PERM,
13385 .cmd = NL80211_CMD_GET_MESH_CONFIG,
13386 .doit = nl80211_get_mesh_config,
13387 .policy = nl80211_policy,
13388 /* can be retrieved by unprivileged users */
13389 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13390 NL80211_FLAG_NEED_RTNL,
13393 .cmd = NL80211_CMD_SET_MESH_CONFIG,
13394 .doit = nl80211_update_mesh_config,
13395 .policy = nl80211_policy,
13396 .flags = GENL_UNS_ADMIN_PERM,
13397 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13398 NL80211_FLAG_NEED_RTNL,
13401 .cmd = NL80211_CMD_TRIGGER_SCAN,
13402 .doit = nl80211_trigger_scan,
13403 .policy = nl80211_policy,
13404 .flags = GENL_UNS_ADMIN_PERM,
13405 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13406 NL80211_FLAG_NEED_RTNL,
13409 .cmd = NL80211_CMD_ABORT_SCAN,
13410 .doit = nl80211_abort_scan,
13411 .policy = nl80211_policy,
13412 .flags = GENL_UNS_ADMIN_PERM,
13413 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13414 NL80211_FLAG_NEED_RTNL,
13417 .cmd = NL80211_CMD_GET_SCAN,
13418 .policy = nl80211_policy,
13419 .dumpit = nl80211_dump_scan,
13422 .cmd = NL80211_CMD_START_SCHED_SCAN,
13423 .doit = nl80211_start_sched_scan,
13424 .policy = nl80211_policy,
13425 .flags = GENL_UNS_ADMIN_PERM,
13426 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13427 NL80211_FLAG_NEED_RTNL,
13430 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
13431 .doit = nl80211_stop_sched_scan,
13432 .policy = nl80211_policy,
13433 .flags = GENL_UNS_ADMIN_PERM,
13434 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13435 NL80211_FLAG_NEED_RTNL,
13438 .cmd = NL80211_CMD_AUTHENTICATE,
13439 .doit = nl80211_authenticate,
13440 .policy = nl80211_policy,
13441 .flags = GENL_UNS_ADMIN_PERM,
13442 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13443 NL80211_FLAG_NEED_RTNL |
13444 NL80211_FLAG_CLEAR_SKB,
13447 .cmd = NL80211_CMD_ASSOCIATE,
13448 .doit = nl80211_associate,
13449 .policy = nl80211_policy,
13450 .flags = GENL_UNS_ADMIN_PERM,
13451 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13452 NL80211_FLAG_NEED_RTNL |
13453 NL80211_FLAG_CLEAR_SKB,
13456 .cmd = NL80211_CMD_DEAUTHENTICATE,
13457 .doit = nl80211_deauthenticate,
13458 .policy = nl80211_policy,
13459 .flags = GENL_UNS_ADMIN_PERM,
13460 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13461 NL80211_FLAG_NEED_RTNL,
13464 .cmd = NL80211_CMD_DISASSOCIATE,
13465 .doit = nl80211_disassociate,
13466 .policy = nl80211_policy,
13467 .flags = GENL_UNS_ADMIN_PERM,
13468 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13469 NL80211_FLAG_NEED_RTNL,
13472 .cmd = NL80211_CMD_JOIN_IBSS,
13473 .doit = nl80211_join_ibss,
13474 .policy = nl80211_policy,
13475 .flags = GENL_UNS_ADMIN_PERM,
13476 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13477 NL80211_FLAG_NEED_RTNL,
13480 .cmd = NL80211_CMD_LEAVE_IBSS,
13481 .doit = nl80211_leave_ibss,
13482 .policy = nl80211_policy,
13483 .flags = GENL_UNS_ADMIN_PERM,
13484 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13485 NL80211_FLAG_NEED_RTNL,
13487 #ifdef CONFIG_NL80211_TESTMODE
13489 .cmd = NL80211_CMD_TESTMODE,
13490 .doit = nl80211_testmode_do,
13491 .dumpit = nl80211_testmode_dump,
13492 .policy = nl80211_policy,
13493 .flags = GENL_UNS_ADMIN_PERM,
13494 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13495 NL80211_FLAG_NEED_RTNL,
13497 #endif
13499 .cmd = NL80211_CMD_CONNECT,
13500 .doit = nl80211_connect,
13501 .policy = nl80211_policy,
13502 .flags = GENL_UNS_ADMIN_PERM,
13503 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13504 NL80211_FLAG_NEED_RTNL |
13505 NL80211_FLAG_CLEAR_SKB,
13508 .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
13509 .doit = nl80211_update_connect_params,
13510 .policy = nl80211_policy,
13511 .flags = GENL_ADMIN_PERM,
13512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13513 NL80211_FLAG_NEED_RTNL |
13514 NL80211_FLAG_CLEAR_SKB,
13517 .cmd = NL80211_CMD_DISCONNECT,
13518 .doit = nl80211_disconnect,
13519 .policy = nl80211_policy,
13520 .flags = GENL_UNS_ADMIN_PERM,
13521 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13522 NL80211_FLAG_NEED_RTNL,
13525 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
13526 .doit = nl80211_wiphy_netns,
13527 .policy = nl80211_policy,
13528 .flags = GENL_UNS_ADMIN_PERM,
13529 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13530 NL80211_FLAG_NEED_RTNL,
13533 .cmd = NL80211_CMD_GET_SURVEY,
13534 .policy = nl80211_policy,
13535 .dumpit = nl80211_dump_survey,
13538 .cmd = NL80211_CMD_SET_PMKSA,
13539 .doit = nl80211_setdel_pmksa,
13540 .policy = nl80211_policy,
13541 .flags = GENL_UNS_ADMIN_PERM,
13542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13543 NL80211_FLAG_NEED_RTNL |
13544 NL80211_FLAG_CLEAR_SKB,
13547 .cmd = NL80211_CMD_DEL_PMKSA,
13548 .doit = nl80211_setdel_pmksa,
13549 .policy = nl80211_policy,
13550 .flags = GENL_UNS_ADMIN_PERM,
13551 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13552 NL80211_FLAG_NEED_RTNL,
13555 .cmd = NL80211_CMD_FLUSH_PMKSA,
13556 .doit = nl80211_flush_pmksa,
13557 .policy = nl80211_policy,
13558 .flags = GENL_UNS_ADMIN_PERM,
13559 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13560 NL80211_FLAG_NEED_RTNL,
13563 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
13564 .doit = nl80211_remain_on_channel,
13565 .policy = nl80211_policy,
13566 .flags = GENL_UNS_ADMIN_PERM,
13567 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13568 NL80211_FLAG_NEED_RTNL,
13571 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
13572 .doit = nl80211_cancel_remain_on_channel,
13573 .policy = nl80211_policy,
13574 .flags = GENL_UNS_ADMIN_PERM,
13575 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13576 NL80211_FLAG_NEED_RTNL,
13579 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
13580 .doit = nl80211_set_tx_bitrate_mask,
13581 .policy = nl80211_policy,
13582 .flags = GENL_UNS_ADMIN_PERM,
13583 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13584 NL80211_FLAG_NEED_RTNL,
13587 .cmd = NL80211_CMD_REGISTER_FRAME,
13588 .doit = nl80211_register_mgmt,
13589 .policy = nl80211_policy,
13590 .flags = GENL_UNS_ADMIN_PERM,
13591 .internal_flags = NL80211_FLAG_NEED_WDEV |
13592 NL80211_FLAG_NEED_RTNL,
13595 .cmd = NL80211_CMD_FRAME,
13596 .doit = nl80211_tx_mgmt,
13597 .policy = nl80211_policy,
13598 .flags = GENL_UNS_ADMIN_PERM,
13599 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13600 NL80211_FLAG_NEED_RTNL,
13603 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
13604 .doit = nl80211_tx_mgmt_cancel_wait,
13605 .policy = nl80211_policy,
13606 .flags = GENL_UNS_ADMIN_PERM,
13607 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13608 NL80211_FLAG_NEED_RTNL,
13611 .cmd = NL80211_CMD_SET_POWER_SAVE,
13612 .doit = nl80211_set_power_save,
13613 .policy = nl80211_policy,
13614 .flags = GENL_UNS_ADMIN_PERM,
13615 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13616 NL80211_FLAG_NEED_RTNL,
13619 .cmd = NL80211_CMD_GET_POWER_SAVE,
13620 .doit = nl80211_get_power_save,
13621 .policy = nl80211_policy,
13622 /* can be retrieved by unprivileged users */
13623 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13624 NL80211_FLAG_NEED_RTNL,
13627 .cmd = NL80211_CMD_SET_CQM,
13628 .doit = nl80211_set_cqm,
13629 .policy = nl80211_policy,
13630 .flags = GENL_UNS_ADMIN_PERM,
13631 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13632 NL80211_FLAG_NEED_RTNL,
13635 .cmd = NL80211_CMD_SET_CHANNEL,
13636 .doit = nl80211_set_channel,
13637 .policy = nl80211_policy,
13638 .flags = GENL_UNS_ADMIN_PERM,
13639 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13640 NL80211_FLAG_NEED_RTNL,
13643 .cmd = NL80211_CMD_SET_WDS_PEER,
13644 .doit = nl80211_set_wds_peer,
13645 .policy = nl80211_policy,
13646 .flags = GENL_UNS_ADMIN_PERM,
13647 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13648 NL80211_FLAG_NEED_RTNL,
13651 .cmd = NL80211_CMD_JOIN_MESH,
13652 .doit = nl80211_join_mesh,
13653 .policy = nl80211_policy,
13654 .flags = GENL_UNS_ADMIN_PERM,
13655 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13656 NL80211_FLAG_NEED_RTNL,
13659 .cmd = NL80211_CMD_LEAVE_MESH,
13660 .doit = nl80211_leave_mesh,
13661 .policy = nl80211_policy,
13662 .flags = GENL_UNS_ADMIN_PERM,
13663 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13664 NL80211_FLAG_NEED_RTNL,
13667 .cmd = NL80211_CMD_JOIN_OCB,
13668 .doit = nl80211_join_ocb,
13669 .policy = nl80211_policy,
13670 .flags = GENL_UNS_ADMIN_PERM,
13671 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13672 NL80211_FLAG_NEED_RTNL,
13675 .cmd = NL80211_CMD_LEAVE_OCB,
13676 .doit = nl80211_leave_ocb,
13677 .policy = nl80211_policy,
13678 .flags = GENL_UNS_ADMIN_PERM,
13679 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13680 NL80211_FLAG_NEED_RTNL,
13682 #ifdef CONFIG_PM
13684 .cmd = NL80211_CMD_GET_WOWLAN,
13685 .doit = nl80211_get_wowlan,
13686 .policy = nl80211_policy,
13687 /* can be retrieved by unprivileged users */
13688 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13689 NL80211_FLAG_NEED_RTNL,
13692 .cmd = NL80211_CMD_SET_WOWLAN,
13693 .doit = nl80211_set_wowlan,
13694 .policy = nl80211_policy,
13695 .flags = GENL_UNS_ADMIN_PERM,
13696 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13697 NL80211_FLAG_NEED_RTNL,
13699 #endif
13701 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
13702 .doit = nl80211_set_rekey_data,
13703 .policy = nl80211_policy,
13704 .flags = GENL_UNS_ADMIN_PERM,
13705 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13706 NL80211_FLAG_NEED_RTNL |
13707 NL80211_FLAG_CLEAR_SKB,
13710 .cmd = NL80211_CMD_TDLS_MGMT,
13711 .doit = nl80211_tdls_mgmt,
13712 .policy = nl80211_policy,
13713 .flags = GENL_UNS_ADMIN_PERM,
13714 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13715 NL80211_FLAG_NEED_RTNL,
13718 .cmd = NL80211_CMD_TDLS_OPER,
13719 .doit = nl80211_tdls_oper,
13720 .policy = nl80211_policy,
13721 .flags = GENL_UNS_ADMIN_PERM,
13722 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13723 NL80211_FLAG_NEED_RTNL,
13726 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
13727 .doit = nl80211_register_unexpected_frame,
13728 .policy = nl80211_policy,
13729 .flags = GENL_UNS_ADMIN_PERM,
13730 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13731 NL80211_FLAG_NEED_RTNL,
13734 .cmd = NL80211_CMD_PROBE_CLIENT,
13735 .doit = nl80211_probe_client,
13736 .policy = nl80211_policy,
13737 .flags = GENL_UNS_ADMIN_PERM,
13738 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13739 NL80211_FLAG_NEED_RTNL,
13742 .cmd = NL80211_CMD_REGISTER_BEACONS,
13743 .doit = nl80211_register_beacons,
13744 .policy = nl80211_policy,
13745 .flags = GENL_UNS_ADMIN_PERM,
13746 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13747 NL80211_FLAG_NEED_RTNL,
13750 .cmd = NL80211_CMD_SET_NOACK_MAP,
13751 .doit = nl80211_set_noack_map,
13752 .policy = nl80211_policy,
13753 .flags = GENL_UNS_ADMIN_PERM,
13754 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13755 NL80211_FLAG_NEED_RTNL,
13758 .cmd = NL80211_CMD_START_P2P_DEVICE,
13759 .doit = nl80211_start_p2p_device,
13760 .policy = nl80211_policy,
13761 .flags = GENL_UNS_ADMIN_PERM,
13762 .internal_flags = NL80211_FLAG_NEED_WDEV |
13763 NL80211_FLAG_NEED_RTNL,
13766 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
13767 .doit = nl80211_stop_p2p_device,
13768 .policy = nl80211_policy,
13769 .flags = GENL_UNS_ADMIN_PERM,
13770 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13771 NL80211_FLAG_NEED_RTNL,
13774 .cmd = NL80211_CMD_START_NAN,
13775 .doit = nl80211_start_nan,
13776 .policy = nl80211_policy,
13777 .flags = GENL_ADMIN_PERM,
13778 .internal_flags = NL80211_FLAG_NEED_WDEV |
13779 NL80211_FLAG_NEED_RTNL,
13782 .cmd = NL80211_CMD_STOP_NAN,
13783 .doit = nl80211_stop_nan,
13784 .policy = nl80211_policy,
13785 .flags = GENL_ADMIN_PERM,
13786 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13787 NL80211_FLAG_NEED_RTNL,
13790 .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
13791 .doit = nl80211_nan_add_func,
13792 .policy = nl80211_policy,
13793 .flags = GENL_ADMIN_PERM,
13794 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13795 NL80211_FLAG_NEED_RTNL,
13798 .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
13799 .doit = nl80211_nan_del_func,
13800 .policy = nl80211_policy,
13801 .flags = GENL_ADMIN_PERM,
13802 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13803 NL80211_FLAG_NEED_RTNL,
13806 .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
13807 .doit = nl80211_nan_change_config,
13808 .policy = nl80211_policy,
13809 .flags = GENL_ADMIN_PERM,
13810 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13811 NL80211_FLAG_NEED_RTNL,
13814 .cmd = NL80211_CMD_SET_MCAST_RATE,
13815 .doit = nl80211_set_mcast_rate,
13816 .policy = nl80211_policy,
13817 .flags = GENL_UNS_ADMIN_PERM,
13818 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13819 NL80211_FLAG_NEED_RTNL,
13822 .cmd = NL80211_CMD_SET_MAC_ACL,
13823 .doit = nl80211_set_mac_acl,
13824 .policy = nl80211_policy,
13825 .flags = GENL_UNS_ADMIN_PERM,
13826 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13827 NL80211_FLAG_NEED_RTNL,
13830 .cmd = NL80211_CMD_RADAR_DETECT,
13831 .doit = nl80211_start_radar_detection,
13832 .policy = nl80211_policy,
13833 .flags = GENL_UNS_ADMIN_PERM,
13834 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13835 NL80211_FLAG_NEED_RTNL,
13838 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
13839 .doit = nl80211_get_protocol_features,
13840 .policy = nl80211_policy,
13843 .cmd = NL80211_CMD_UPDATE_FT_IES,
13844 .doit = nl80211_update_ft_ies,
13845 .policy = nl80211_policy,
13846 .flags = GENL_UNS_ADMIN_PERM,
13847 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13848 NL80211_FLAG_NEED_RTNL,
13851 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
13852 .doit = nl80211_crit_protocol_start,
13853 .policy = nl80211_policy,
13854 .flags = GENL_UNS_ADMIN_PERM,
13855 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13856 NL80211_FLAG_NEED_RTNL,
13859 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
13860 .doit = nl80211_crit_protocol_stop,
13861 .policy = nl80211_policy,
13862 .flags = GENL_UNS_ADMIN_PERM,
13863 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13864 NL80211_FLAG_NEED_RTNL,
13867 .cmd = NL80211_CMD_GET_COALESCE,
13868 .doit = nl80211_get_coalesce,
13869 .policy = nl80211_policy,
13870 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13871 NL80211_FLAG_NEED_RTNL,
13874 .cmd = NL80211_CMD_SET_COALESCE,
13875 .doit = nl80211_set_coalesce,
13876 .policy = nl80211_policy,
13877 .flags = GENL_UNS_ADMIN_PERM,
13878 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13879 NL80211_FLAG_NEED_RTNL,
13882 .cmd = NL80211_CMD_CHANNEL_SWITCH,
13883 .doit = nl80211_channel_switch,
13884 .policy = nl80211_policy,
13885 .flags = GENL_UNS_ADMIN_PERM,
13886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13887 NL80211_FLAG_NEED_RTNL,
13890 .cmd = NL80211_CMD_VENDOR,
13891 .doit = nl80211_vendor_cmd,
13892 .dumpit = nl80211_vendor_cmd_dump,
13893 .policy = nl80211_policy,
13894 .flags = GENL_UNS_ADMIN_PERM,
13895 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13896 NL80211_FLAG_NEED_RTNL |
13897 NL80211_FLAG_CLEAR_SKB,
13900 .cmd = NL80211_CMD_SET_QOS_MAP,
13901 .doit = nl80211_set_qos_map,
13902 .policy = nl80211_policy,
13903 .flags = GENL_UNS_ADMIN_PERM,
13904 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13905 NL80211_FLAG_NEED_RTNL,
13908 .cmd = NL80211_CMD_ADD_TX_TS,
13909 .doit = nl80211_add_tx_ts,
13910 .policy = nl80211_policy,
13911 .flags = GENL_UNS_ADMIN_PERM,
13912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13913 NL80211_FLAG_NEED_RTNL,
13916 .cmd = NL80211_CMD_DEL_TX_TS,
13917 .doit = nl80211_del_tx_ts,
13918 .policy = nl80211_policy,
13919 .flags = GENL_UNS_ADMIN_PERM,
13920 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13921 NL80211_FLAG_NEED_RTNL,
13924 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
13925 .doit = nl80211_tdls_channel_switch,
13926 .policy = nl80211_policy,
13927 .flags = GENL_UNS_ADMIN_PERM,
13928 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13929 NL80211_FLAG_NEED_RTNL,
13932 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
13933 .doit = nl80211_tdls_cancel_channel_switch,
13934 .policy = nl80211_policy,
13935 .flags = GENL_UNS_ADMIN_PERM,
13936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13937 NL80211_FLAG_NEED_RTNL,
13940 .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
13941 .doit = nl80211_set_multicast_to_unicast,
13942 .policy = nl80211_policy,
13943 .flags = GENL_UNS_ADMIN_PERM,
13944 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13945 NL80211_FLAG_NEED_RTNL,
13948 .cmd = NL80211_CMD_SET_PMK,
13949 .doit = nl80211_set_pmk,
13950 .policy = nl80211_policy,
13951 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13952 NL80211_FLAG_NEED_RTNL |
13953 NL80211_FLAG_CLEAR_SKB,
13956 .cmd = NL80211_CMD_DEL_PMK,
13957 .doit = nl80211_del_pmk,
13958 .policy = nl80211_policy,
13959 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13960 NL80211_FLAG_NEED_RTNL,
13963 .cmd = NL80211_CMD_EXTERNAL_AUTH,
13964 .doit = nl80211_external_auth,
13965 .policy = nl80211_policy,
13966 .flags = GENL_ADMIN_PERM,
13967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13968 NL80211_FLAG_NEED_RTNL,
13971 .cmd = NL80211_CMD_CONTROL_PORT_FRAME,
13972 .doit = nl80211_tx_control_port,
13973 .policy = nl80211_policy,
13974 .flags = GENL_UNS_ADMIN_PERM,
13975 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13976 NL80211_FLAG_NEED_RTNL,
13980 static struct genl_family nl80211_fam __ro_after_init = {
13981 .name = NL80211_GENL_NAME, /* have users key off the name instead */
13982 .hdrsize = 0, /* no private header */
13983 .version = 1, /* no particular meaning now */
13984 .maxattr = NL80211_ATTR_MAX,
13985 .netnsok = true,
13986 .pre_doit = nl80211_pre_doit,
13987 .post_doit = nl80211_post_doit,
13988 .module = THIS_MODULE,
13989 .ops = nl80211_ops,
13990 .n_ops = ARRAY_SIZE(nl80211_ops),
13991 .mcgrps = nl80211_mcgrps,
13992 .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
13995 /* notification functions */
13997 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
13998 enum nl80211_commands cmd)
14000 struct sk_buff *msg;
14001 struct nl80211_dump_wiphy_state state = {};
14003 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14004 cmd != NL80211_CMD_DEL_WIPHY);
14006 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14007 if (!msg)
14008 return;
14010 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14011 nlmsg_free(msg);
14012 return;
14015 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14016 NL80211_MCGRP_CONFIG, GFP_KERNEL);
14019 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14020 struct wireless_dev *wdev,
14021 enum nl80211_commands cmd)
14023 struct sk_buff *msg;
14025 WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
14026 cmd != NL80211_CMD_DEL_INTERFACE);
14028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14029 if (!msg)
14030 return;
14032 if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev,
14033 cmd == NL80211_CMD_DEL_INTERFACE) < 0) {
14034 nlmsg_free(msg);
14035 return;
14038 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14039 NL80211_MCGRP_CONFIG, GFP_KERNEL);
14042 static int nl80211_add_scan_req(struct sk_buff *msg,
14043 struct cfg80211_registered_device *rdev)
14045 struct cfg80211_scan_request *req = rdev->scan_req;
14046 struct nlattr *nest;
14047 int i;
14049 if (WARN_ON(!req))
14050 return 0;
14052 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
14053 if (!nest)
14054 goto nla_put_failure;
14055 for (i = 0; i < req->n_ssids; i++) {
14056 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14057 goto nla_put_failure;
14059 nla_nest_end(msg, nest);
14061 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14062 if (!nest)
14063 goto nla_put_failure;
14064 for (i = 0; i < req->n_channels; i++) {
14065 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14066 goto nla_put_failure;
14068 nla_nest_end(msg, nest);
14070 if (req->ie &&
14071 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14072 goto nla_put_failure;
14074 if (req->flags &&
14075 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14076 goto nla_put_failure;
14078 if (req->info.scan_start_tsf &&
14079 (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14080 req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14081 nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14082 req->info.tsf_bssid)))
14083 goto nla_put_failure;
14085 return 0;
14086 nla_put_failure:
14087 return -ENOBUFS;
14090 static int nl80211_prep_scan_msg(struct sk_buff *msg,
14091 struct cfg80211_registered_device *rdev,
14092 struct wireless_dev *wdev,
14093 u32 portid, u32 seq, int flags,
14094 u32 cmd)
14096 void *hdr;
14098 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14099 if (!hdr)
14100 return -1;
14102 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14103 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14104 wdev->netdev->ifindex)) ||
14105 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14106 NL80211_ATTR_PAD))
14107 goto nla_put_failure;
14109 /* ignore errors and send incomplete event anyway */
14110 nl80211_add_scan_req(msg, rdev);
14112 genlmsg_end(msg, hdr);
14113 return 0;
14115 nla_put_failure:
14116 genlmsg_cancel(msg, hdr);
14117 return -EMSGSIZE;
14120 static int
14121 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14122 struct cfg80211_sched_scan_request *req, u32 cmd)
14124 void *hdr;
14126 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14127 if (!hdr)
14128 return -1;
14130 if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14131 wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14132 nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14133 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14134 NL80211_ATTR_PAD))
14135 goto nla_put_failure;
14137 genlmsg_end(msg, hdr);
14138 return 0;
14140 nla_put_failure:
14141 genlmsg_cancel(msg, hdr);
14142 return -EMSGSIZE;
14145 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14146 struct wireless_dev *wdev)
14148 struct sk_buff *msg;
14150 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14151 if (!msg)
14152 return;
14154 if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14155 NL80211_CMD_TRIGGER_SCAN) < 0) {
14156 nlmsg_free(msg);
14157 return;
14160 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14161 NL80211_MCGRP_SCAN, GFP_KERNEL);
14164 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14165 struct wireless_dev *wdev, bool aborted)
14167 struct sk_buff *msg;
14169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14170 if (!msg)
14171 return NULL;
14173 if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14174 aborted ? NL80211_CMD_SCAN_ABORTED :
14175 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14176 nlmsg_free(msg);
14177 return NULL;
14180 return msg;
14183 /* send message created by nl80211_build_scan_msg() */
14184 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14185 struct sk_buff *msg)
14187 if (!msg)
14188 return;
14190 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14191 NL80211_MCGRP_SCAN, GFP_KERNEL);
14194 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14196 struct sk_buff *msg;
14198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14199 if (!msg)
14200 return;
14202 if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14203 nlmsg_free(msg);
14204 return;
14207 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14208 NL80211_MCGRP_SCAN, GFP_KERNEL);
14211 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14212 struct regulatory_request *request)
14214 /* Userspace can always count this one always being set */
14215 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14216 goto nla_put_failure;
14218 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14219 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14220 NL80211_REGDOM_TYPE_WORLD))
14221 goto nla_put_failure;
14222 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
14223 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14224 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
14225 goto nla_put_failure;
14226 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
14227 request->intersect) {
14228 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14229 NL80211_REGDOM_TYPE_INTERSECTION))
14230 goto nla_put_failure;
14231 } else {
14232 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14233 NL80211_REGDOM_TYPE_COUNTRY) ||
14234 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
14235 request->alpha2))
14236 goto nla_put_failure;
14239 if (request->wiphy_idx != WIPHY_IDX_INVALID) {
14240 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
14242 if (wiphy &&
14243 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
14244 goto nla_put_failure;
14246 if (wiphy &&
14247 wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
14248 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
14249 goto nla_put_failure;
14252 return true;
14254 nla_put_failure:
14255 return false;
14259 * This can happen on global regulatory changes or device specific settings
14260 * based on custom regulatory domains.
14262 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
14263 struct regulatory_request *request)
14265 struct sk_buff *msg;
14266 void *hdr;
14268 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14269 if (!msg)
14270 return;
14272 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
14273 if (!hdr) {
14274 nlmsg_free(msg);
14275 return;
14278 if (nl80211_reg_change_event_fill(msg, request) == false)
14279 goto nla_put_failure;
14281 genlmsg_end(msg, hdr);
14283 rcu_read_lock();
14284 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14285 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14286 rcu_read_unlock();
14288 return;
14290 nla_put_failure:
14291 nlmsg_free(msg);
14294 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
14295 struct net_device *netdev,
14296 const u8 *buf, size_t len,
14297 enum nl80211_commands cmd, gfp_t gfp,
14298 int uapsd_queues)
14300 struct sk_buff *msg;
14301 void *hdr;
14303 msg = nlmsg_new(100 + len, gfp);
14304 if (!msg)
14305 return;
14307 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14308 if (!hdr) {
14309 nlmsg_free(msg);
14310 return;
14313 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14314 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14315 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
14316 goto nla_put_failure;
14318 if (uapsd_queues >= 0) {
14319 struct nlattr *nla_wmm =
14320 nla_nest_start(msg, NL80211_ATTR_STA_WME);
14321 if (!nla_wmm)
14322 goto nla_put_failure;
14324 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
14325 uapsd_queues))
14326 goto nla_put_failure;
14328 nla_nest_end(msg, nla_wmm);
14331 genlmsg_end(msg, hdr);
14333 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14334 NL80211_MCGRP_MLME, gfp);
14335 return;
14337 nla_put_failure:
14338 nlmsg_free(msg);
14341 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
14342 struct net_device *netdev, const u8 *buf,
14343 size_t len, gfp_t gfp)
14345 nl80211_send_mlme_event(rdev, netdev, buf, len,
14346 NL80211_CMD_AUTHENTICATE, gfp, -1);
14349 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
14350 struct net_device *netdev, const u8 *buf,
14351 size_t len, gfp_t gfp, int uapsd_queues)
14353 nl80211_send_mlme_event(rdev, netdev, buf, len,
14354 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
14357 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
14358 struct net_device *netdev, const u8 *buf,
14359 size_t len, gfp_t gfp)
14361 nl80211_send_mlme_event(rdev, netdev, buf, len,
14362 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
14365 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
14366 struct net_device *netdev, const u8 *buf,
14367 size_t len, gfp_t gfp)
14369 nl80211_send_mlme_event(rdev, netdev, buf, len,
14370 NL80211_CMD_DISASSOCIATE, gfp, -1);
14373 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
14374 size_t len)
14376 struct wireless_dev *wdev = dev->ieee80211_ptr;
14377 struct wiphy *wiphy = wdev->wiphy;
14378 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14379 const struct ieee80211_mgmt *mgmt = (void *)buf;
14380 u32 cmd;
14382 if (WARN_ON(len < 2))
14383 return;
14385 if (ieee80211_is_deauth(mgmt->frame_control))
14386 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
14387 else
14388 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
14390 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
14391 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
14393 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
14395 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
14396 struct net_device *netdev, int cmd,
14397 const u8 *addr, gfp_t gfp)
14399 struct sk_buff *msg;
14400 void *hdr;
14402 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14403 if (!msg)
14404 return;
14406 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14407 if (!hdr) {
14408 nlmsg_free(msg);
14409 return;
14412 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14413 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14414 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
14415 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14416 goto nla_put_failure;
14418 genlmsg_end(msg, hdr);
14420 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14421 NL80211_MCGRP_MLME, gfp);
14422 return;
14424 nla_put_failure:
14425 nlmsg_free(msg);
14428 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
14429 struct net_device *netdev, const u8 *addr,
14430 gfp_t gfp)
14432 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
14433 addr, gfp);
14436 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
14437 struct net_device *netdev, const u8 *addr,
14438 gfp_t gfp)
14440 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
14441 addr, gfp);
14444 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
14445 struct net_device *netdev,
14446 struct cfg80211_connect_resp_params *cr,
14447 gfp_t gfp)
14449 struct sk_buff *msg;
14450 void *hdr;
14452 msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
14453 cr->fils.kek_len + cr->fils.pmk_len +
14454 (cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
14455 if (!msg)
14456 return;
14458 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
14459 if (!hdr) {
14460 nlmsg_free(msg);
14461 return;
14464 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14465 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14466 (cr->bssid &&
14467 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
14468 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
14469 cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
14470 cr->status) ||
14471 (cr->status < 0 &&
14472 (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
14473 nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
14474 cr->timeout_reason))) ||
14475 (cr->req_ie &&
14476 nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
14477 (cr->resp_ie &&
14478 nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
14479 cr->resp_ie)) ||
14480 (cr->fils.update_erp_next_seq_num &&
14481 nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
14482 cr->fils.erp_next_seq_num)) ||
14483 (cr->status == WLAN_STATUS_SUCCESS &&
14484 ((cr->fils.kek &&
14485 nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
14486 cr->fils.kek)) ||
14487 (cr->fils.pmk &&
14488 nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
14489 (cr->fils.pmkid &&
14490 nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
14491 goto nla_put_failure;
14493 genlmsg_end(msg, hdr);
14495 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14496 NL80211_MCGRP_MLME, gfp);
14497 return;
14499 nla_put_failure:
14500 nlmsg_free(msg);
14503 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
14504 struct net_device *netdev,
14505 struct cfg80211_roam_info *info, gfp_t gfp)
14507 struct sk_buff *msg;
14508 void *hdr;
14509 const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
14511 msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
14512 info->fils.kek_len + info->fils.pmk_len +
14513 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
14514 if (!msg)
14515 return;
14517 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
14518 if (!hdr) {
14519 nlmsg_free(msg);
14520 return;
14523 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14524 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14525 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
14526 (info->req_ie &&
14527 nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
14528 info->req_ie)) ||
14529 (info->resp_ie &&
14530 nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
14531 info->resp_ie)) ||
14532 (info->fils.update_erp_next_seq_num &&
14533 nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
14534 info->fils.erp_next_seq_num)) ||
14535 (info->fils.kek &&
14536 nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
14537 info->fils.kek)) ||
14538 (info->fils.pmk &&
14539 nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
14540 (info->fils.pmkid &&
14541 nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
14542 goto nla_put_failure;
14544 genlmsg_end(msg, hdr);
14546 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14547 NL80211_MCGRP_MLME, gfp);
14548 return;
14550 nla_put_failure:
14551 nlmsg_free(msg);
14554 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
14555 struct net_device *netdev, const u8 *bssid)
14557 struct sk_buff *msg;
14558 void *hdr;
14560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14561 if (!msg)
14562 return;
14564 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
14565 if (!hdr) {
14566 nlmsg_free(msg);
14567 return;
14570 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14571 goto nla_put_failure;
14573 genlmsg_end(msg, hdr);
14575 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14576 NL80211_MCGRP_MLME, GFP_KERNEL);
14577 return;
14579 nla_put_failure:
14580 nlmsg_free(msg);
14583 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
14584 struct net_device *netdev, u16 reason,
14585 const u8 *ie, size_t ie_len, bool from_ap)
14587 struct sk_buff *msg;
14588 void *hdr;
14590 msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
14591 if (!msg)
14592 return;
14594 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
14595 if (!hdr) {
14596 nlmsg_free(msg);
14597 return;
14600 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14601 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14602 (reason &&
14603 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
14604 (from_ap &&
14605 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
14606 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
14607 goto nla_put_failure;
14609 genlmsg_end(msg, hdr);
14611 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14612 NL80211_MCGRP_MLME, GFP_KERNEL);
14613 return;
14615 nla_put_failure:
14616 nlmsg_free(msg);
14619 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
14620 struct net_device *netdev, const u8 *bssid,
14621 gfp_t gfp)
14623 struct sk_buff *msg;
14624 void *hdr;
14626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14627 if (!msg)
14628 return;
14630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
14631 if (!hdr) {
14632 nlmsg_free(msg);
14633 return;
14636 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14637 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14638 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14639 goto nla_put_failure;
14641 genlmsg_end(msg, hdr);
14643 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14644 NL80211_MCGRP_MLME, gfp);
14645 return;
14647 nla_put_failure:
14648 nlmsg_free(msg);
14651 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
14652 const u8* ie, u8 ie_len, gfp_t gfp)
14654 struct wireless_dev *wdev = dev->ieee80211_ptr;
14655 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14656 struct sk_buff *msg;
14657 void *hdr;
14659 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
14660 return;
14662 trace_cfg80211_notify_new_peer_candidate(dev, addr);
14664 msg = nlmsg_new(100 + ie_len, gfp);
14665 if (!msg)
14666 return;
14668 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
14669 if (!hdr) {
14670 nlmsg_free(msg);
14671 return;
14674 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14675 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14676 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14677 (ie_len && ie &&
14678 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
14679 goto nla_put_failure;
14681 genlmsg_end(msg, hdr);
14683 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14684 NL80211_MCGRP_MLME, gfp);
14685 return;
14687 nla_put_failure:
14688 nlmsg_free(msg);
14690 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
14692 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
14693 struct net_device *netdev, const u8 *addr,
14694 enum nl80211_key_type key_type, int key_id,
14695 const u8 *tsc, gfp_t gfp)
14697 struct sk_buff *msg;
14698 void *hdr;
14700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14701 if (!msg)
14702 return;
14704 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
14705 if (!hdr) {
14706 nlmsg_free(msg);
14707 return;
14710 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14711 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14712 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
14713 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
14714 (key_id != -1 &&
14715 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
14716 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
14717 goto nla_put_failure;
14719 genlmsg_end(msg, hdr);
14721 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14722 NL80211_MCGRP_MLME, gfp);
14723 return;
14725 nla_put_failure:
14726 nlmsg_free(msg);
14729 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
14730 struct ieee80211_channel *channel_before,
14731 struct ieee80211_channel *channel_after)
14733 struct sk_buff *msg;
14734 void *hdr;
14735 struct nlattr *nl_freq;
14737 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
14738 if (!msg)
14739 return;
14741 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
14742 if (!hdr) {
14743 nlmsg_free(msg);
14744 return;
14748 * Since we are applying the beacon hint to a wiphy we know its
14749 * wiphy_idx is valid
14751 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
14752 goto nla_put_failure;
14754 /* Before */
14755 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
14756 if (!nl_freq)
14757 goto nla_put_failure;
14759 if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
14760 goto nla_put_failure;
14761 nla_nest_end(msg, nl_freq);
14763 /* After */
14764 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
14765 if (!nl_freq)
14766 goto nla_put_failure;
14768 if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
14769 goto nla_put_failure;
14770 nla_nest_end(msg, nl_freq);
14772 genlmsg_end(msg, hdr);
14774 rcu_read_lock();
14775 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14776 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14777 rcu_read_unlock();
14779 return;
14781 nla_put_failure:
14782 nlmsg_free(msg);
14785 static void nl80211_send_remain_on_chan_event(
14786 int cmd, struct cfg80211_registered_device *rdev,
14787 struct wireless_dev *wdev, u64 cookie,
14788 struct ieee80211_channel *chan,
14789 unsigned int duration, gfp_t gfp)
14791 struct sk_buff *msg;
14792 void *hdr;
14794 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14795 if (!msg)
14796 return;
14798 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14799 if (!hdr) {
14800 nlmsg_free(msg);
14801 return;
14804 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14805 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14806 wdev->netdev->ifindex)) ||
14807 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14808 NL80211_ATTR_PAD) ||
14809 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
14810 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
14811 NL80211_CHAN_NO_HT) ||
14812 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14813 NL80211_ATTR_PAD))
14814 goto nla_put_failure;
14816 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
14817 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
14818 goto nla_put_failure;
14820 genlmsg_end(msg, hdr);
14822 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14823 NL80211_MCGRP_MLME, gfp);
14824 return;
14826 nla_put_failure:
14827 nlmsg_free(msg);
14830 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
14831 struct ieee80211_channel *chan,
14832 unsigned int duration, gfp_t gfp)
14834 struct wiphy *wiphy = wdev->wiphy;
14835 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14837 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
14838 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
14839 rdev, wdev, cookie, chan,
14840 duration, gfp);
14842 EXPORT_SYMBOL(cfg80211_ready_on_channel);
14844 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
14845 struct ieee80211_channel *chan,
14846 gfp_t gfp)
14848 struct wiphy *wiphy = wdev->wiphy;
14849 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14851 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
14852 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14853 rdev, wdev, cookie, chan, 0, gfp);
14855 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
14857 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
14858 struct station_info *sinfo, gfp_t gfp)
14860 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14861 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14862 struct sk_buff *msg;
14864 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
14866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14867 if (!msg)
14868 return;
14870 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
14871 rdev, dev, mac_addr, sinfo) < 0) {
14872 nlmsg_free(msg);
14873 return;
14876 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14877 NL80211_MCGRP_MLME, gfp);
14879 EXPORT_SYMBOL(cfg80211_new_sta);
14881 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
14882 struct station_info *sinfo, gfp_t gfp)
14884 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14885 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14886 struct sk_buff *msg;
14887 struct station_info empty_sinfo = {};
14889 if (!sinfo)
14890 sinfo = &empty_sinfo;
14892 trace_cfg80211_del_sta(dev, mac_addr);
14894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14895 if (!msg) {
14896 cfg80211_sinfo_release_content(sinfo);
14897 return;
14900 if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
14901 rdev, dev, mac_addr, sinfo) < 0) {
14902 nlmsg_free(msg);
14903 return;
14906 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14907 NL80211_MCGRP_MLME, gfp);
14909 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
14911 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
14912 enum nl80211_connect_failed_reason reason,
14913 gfp_t gfp)
14915 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14916 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14917 struct sk_buff *msg;
14918 void *hdr;
14920 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
14921 if (!msg)
14922 return;
14924 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
14925 if (!hdr) {
14926 nlmsg_free(msg);
14927 return;
14930 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14931 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
14932 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
14933 goto nla_put_failure;
14935 genlmsg_end(msg, hdr);
14937 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14938 NL80211_MCGRP_MLME, gfp);
14939 return;
14941 nla_put_failure:
14942 nlmsg_free(msg);
14944 EXPORT_SYMBOL(cfg80211_conn_failed);
14946 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
14947 const u8 *addr, gfp_t gfp)
14949 struct wireless_dev *wdev = dev->ieee80211_ptr;
14950 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14951 struct sk_buff *msg;
14952 void *hdr;
14953 u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
14955 if (!nlportid)
14956 return false;
14958 msg = nlmsg_new(100, gfp);
14959 if (!msg)
14960 return true;
14962 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14963 if (!hdr) {
14964 nlmsg_free(msg);
14965 return true;
14968 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14969 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14970 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14971 goto nla_put_failure;
14973 genlmsg_end(msg, hdr);
14974 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14975 return true;
14977 nla_put_failure:
14978 nlmsg_free(msg);
14979 return true;
14982 bool cfg80211_rx_spurious_frame(struct net_device *dev,
14983 const u8 *addr, gfp_t gfp)
14985 struct wireless_dev *wdev = dev->ieee80211_ptr;
14986 bool ret;
14988 trace_cfg80211_rx_spurious_frame(dev, addr);
14990 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14991 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
14992 trace_cfg80211_return_bool(false);
14993 return false;
14995 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
14996 addr, gfp);
14997 trace_cfg80211_return_bool(ret);
14998 return ret;
15000 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15002 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15003 const u8 *addr, gfp_t gfp)
15005 struct wireless_dev *wdev = dev->ieee80211_ptr;
15006 bool ret;
15008 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15010 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15011 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15012 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15013 trace_cfg80211_return_bool(false);
15014 return false;
15016 ret = __nl80211_unexpected_frame(dev,
15017 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15018 addr, gfp);
15019 trace_cfg80211_return_bool(ret);
15020 return ret;
15022 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15024 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15025 struct wireless_dev *wdev, u32 nlportid,
15026 int freq, int sig_dbm,
15027 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15029 struct net_device *netdev = wdev->netdev;
15030 struct sk_buff *msg;
15031 void *hdr;
15033 msg = nlmsg_new(100 + len, gfp);
15034 if (!msg)
15035 return -ENOMEM;
15037 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15038 if (!hdr) {
15039 nlmsg_free(msg);
15040 return -ENOMEM;
15043 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15044 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15045 netdev->ifindex)) ||
15046 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15047 NL80211_ATTR_PAD) ||
15048 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15049 (sig_dbm &&
15050 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15051 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15052 (flags &&
15053 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15054 goto nla_put_failure;
15056 genlmsg_end(msg, hdr);
15058 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15060 nla_put_failure:
15061 nlmsg_free(msg);
15062 return -ENOBUFS;
15065 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15066 const u8 *buf, size_t len, bool ack, gfp_t gfp)
15068 struct wiphy *wiphy = wdev->wiphy;
15069 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15070 struct net_device *netdev = wdev->netdev;
15071 struct sk_buff *msg;
15072 void *hdr;
15074 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15076 msg = nlmsg_new(100 + len, gfp);
15077 if (!msg)
15078 return;
15080 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15081 if (!hdr) {
15082 nlmsg_free(msg);
15083 return;
15086 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15087 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15088 netdev->ifindex)) ||
15089 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15090 NL80211_ATTR_PAD) ||
15091 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15092 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15093 NL80211_ATTR_PAD) ||
15094 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
15095 goto nla_put_failure;
15097 genlmsg_end(msg, hdr);
15099 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15100 NL80211_MCGRP_MLME, gfp);
15101 return;
15103 nla_put_failure:
15104 nlmsg_free(msg);
15106 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15108 static int __nl80211_rx_control_port(struct net_device *dev,
15109 struct sk_buff *skb,
15110 bool unencrypted, gfp_t gfp)
15112 struct wireless_dev *wdev = dev->ieee80211_ptr;
15113 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15114 struct ethhdr *ehdr = eth_hdr(skb);
15115 const u8 *addr = ehdr->h_source;
15116 u16 proto = be16_to_cpu(skb->protocol);
15117 struct sk_buff *msg;
15118 void *hdr;
15119 struct nlattr *frame;
15121 u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15123 if (!nlportid)
15124 return -ENOENT;
15126 msg = nlmsg_new(100 + skb->len, gfp);
15127 if (!msg)
15128 return -ENOMEM;
15130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15131 if (!hdr) {
15132 nlmsg_free(msg);
15133 return -ENOBUFS;
15136 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15137 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15138 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15139 NL80211_ATTR_PAD) ||
15140 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15141 nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15142 (unencrypted && nla_put_flag(msg,
15143 NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15144 goto nla_put_failure;
15146 frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15147 if (!frame)
15148 goto nla_put_failure;
15150 skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15151 genlmsg_end(msg, hdr);
15153 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15155 nla_put_failure:
15156 nlmsg_free(msg);
15157 return -ENOBUFS;
15160 bool cfg80211_rx_control_port(struct net_device *dev,
15161 struct sk_buff *skb, bool unencrypted)
15163 int ret;
15165 trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15166 ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15167 trace_cfg80211_return_bool(ret == 0);
15168 return ret == 0;
15170 EXPORT_SYMBOL(cfg80211_rx_control_port);
15172 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15173 const char *mac, gfp_t gfp)
15175 struct wireless_dev *wdev = dev->ieee80211_ptr;
15176 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15177 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15178 void **cb;
15180 if (!msg)
15181 return NULL;
15183 cb = (void **)msg->cb;
15185 cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15186 if (!cb[0]) {
15187 nlmsg_free(msg);
15188 return NULL;
15191 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15192 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15193 goto nla_put_failure;
15195 if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15196 goto nla_put_failure;
15198 cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
15199 if (!cb[1])
15200 goto nla_put_failure;
15202 cb[2] = rdev;
15204 return msg;
15205 nla_put_failure:
15206 nlmsg_free(msg);
15207 return NULL;
15210 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
15212 void **cb = (void **)msg->cb;
15213 struct cfg80211_registered_device *rdev = cb[2];
15215 nla_nest_end(msg, cb[1]);
15216 genlmsg_end(msg, cb[0]);
15218 memset(msg->cb, 0, sizeof(msg->cb));
15220 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15221 NL80211_MCGRP_MLME, gfp);
15224 void cfg80211_cqm_rssi_notify(struct net_device *dev,
15225 enum nl80211_cqm_rssi_threshold_event rssi_event,
15226 s32 rssi_level, gfp_t gfp)
15228 struct sk_buff *msg;
15229 struct wireless_dev *wdev = dev->ieee80211_ptr;
15230 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15232 trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
15234 if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
15235 rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
15236 return;
15238 if (wdev->cqm_config) {
15239 wdev->cqm_config->last_rssi_event_value = rssi_level;
15241 cfg80211_cqm_rssi_update(rdev, dev);
15243 if (rssi_level == 0)
15244 rssi_level = wdev->cqm_config->last_rssi_event_value;
15247 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
15248 if (!msg)
15249 return;
15251 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
15252 rssi_event))
15253 goto nla_put_failure;
15255 if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
15256 rssi_level))
15257 goto nla_put_failure;
15259 cfg80211_send_cqm(msg, gfp);
15261 return;
15263 nla_put_failure:
15264 nlmsg_free(msg);
15266 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
15268 void cfg80211_cqm_txe_notify(struct net_device *dev,
15269 const u8 *peer, u32 num_packets,
15270 u32 rate, u32 intvl, gfp_t gfp)
15272 struct sk_buff *msg;
15274 msg = cfg80211_prepare_cqm(dev, peer, gfp);
15275 if (!msg)
15276 return;
15278 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
15279 goto nla_put_failure;
15281 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
15282 goto nla_put_failure;
15284 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
15285 goto nla_put_failure;
15287 cfg80211_send_cqm(msg, gfp);
15288 return;
15290 nla_put_failure:
15291 nlmsg_free(msg);
15293 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
15295 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
15296 const u8 *peer, u32 num_packets, gfp_t gfp)
15298 struct sk_buff *msg;
15300 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
15302 msg = cfg80211_prepare_cqm(dev, peer, gfp);
15303 if (!msg)
15304 return;
15306 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
15307 goto nla_put_failure;
15309 cfg80211_send_cqm(msg, gfp);
15310 return;
15312 nla_put_failure:
15313 nlmsg_free(msg);
15315 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
15317 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
15319 struct sk_buff *msg;
15321 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
15322 if (!msg)
15323 return;
15325 if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
15326 goto nla_put_failure;
15328 cfg80211_send_cqm(msg, gfp);
15329 return;
15331 nla_put_failure:
15332 nlmsg_free(msg);
15334 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
15336 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
15337 struct net_device *netdev, const u8 *bssid,
15338 const u8 *replay_ctr, gfp_t gfp)
15340 struct sk_buff *msg;
15341 struct nlattr *rekey_attr;
15342 void *hdr;
15344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15345 if (!msg)
15346 return;
15348 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
15349 if (!hdr) {
15350 nlmsg_free(msg);
15351 return;
15354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15355 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15356 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15357 goto nla_put_failure;
15359 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
15360 if (!rekey_attr)
15361 goto nla_put_failure;
15363 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
15364 NL80211_REPLAY_CTR_LEN, replay_ctr))
15365 goto nla_put_failure;
15367 nla_nest_end(msg, rekey_attr);
15369 genlmsg_end(msg, hdr);
15371 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15372 NL80211_MCGRP_MLME, gfp);
15373 return;
15375 nla_put_failure:
15376 nlmsg_free(msg);
15379 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
15380 const u8 *replay_ctr, gfp_t gfp)
15382 struct wireless_dev *wdev = dev->ieee80211_ptr;
15383 struct wiphy *wiphy = wdev->wiphy;
15384 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15386 trace_cfg80211_gtk_rekey_notify(dev, bssid);
15387 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
15389 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
15391 static void
15392 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
15393 struct net_device *netdev, int index,
15394 const u8 *bssid, bool preauth, gfp_t gfp)
15396 struct sk_buff *msg;
15397 struct nlattr *attr;
15398 void *hdr;
15400 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15401 if (!msg)
15402 return;
15404 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
15405 if (!hdr) {
15406 nlmsg_free(msg);
15407 return;
15410 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15411 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
15412 goto nla_put_failure;
15414 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
15415 if (!attr)
15416 goto nla_put_failure;
15418 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
15419 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
15420 (preauth &&
15421 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
15422 goto nla_put_failure;
15424 nla_nest_end(msg, attr);
15426 genlmsg_end(msg, hdr);
15428 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15429 NL80211_MCGRP_MLME, gfp);
15430 return;
15432 nla_put_failure:
15433 nlmsg_free(msg);
15436 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
15437 const u8 *bssid, bool preauth, gfp_t gfp)
15439 struct wireless_dev *wdev = dev->ieee80211_ptr;
15440 struct wiphy *wiphy = wdev->wiphy;
15441 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15443 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
15444 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
15446 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
15448 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
15449 struct net_device *netdev,
15450 struct cfg80211_chan_def *chandef,
15451 gfp_t gfp,
15452 enum nl80211_commands notif,
15453 u8 count)
15455 struct sk_buff *msg;
15456 void *hdr;
15458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15459 if (!msg)
15460 return;
15462 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
15463 if (!hdr) {
15464 nlmsg_free(msg);
15465 return;
15468 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
15469 goto nla_put_failure;
15471 if (nl80211_send_chandef(msg, chandef))
15472 goto nla_put_failure;
15474 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
15475 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
15476 goto nla_put_failure;
15478 genlmsg_end(msg, hdr);
15480 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15481 NL80211_MCGRP_MLME, gfp);
15482 return;
15484 nla_put_failure:
15485 nlmsg_free(msg);
15488 void cfg80211_ch_switch_notify(struct net_device *dev,
15489 struct cfg80211_chan_def *chandef)
15491 struct wireless_dev *wdev = dev->ieee80211_ptr;
15492 struct wiphy *wiphy = wdev->wiphy;
15493 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15495 ASSERT_WDEV_LOCK(wdev);
15497 trace_cfg80211_ch_switch_notify(dev, chandef);
15499 wdev->chandef = *chandef;
15500 wdev->preset_chandef = *chandef;
15502 if (wdev->iftype == NL80211_IFTYPE_STATION &&
15503 !WARN_ON(!wdev->current_bss))
15504 wdev->current_bss->pub.channel = chandef->chan;
15506 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
15507 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
15509 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
15511 void cfg80211_ch_switch_started_notify(struct net_device *dev,
15512 struct cfg80211_chan_def *chandef,
15513 u8 count)
15515 struct wireless_dev *wdev = dev->ieee80211_ptr;
15516 struct wiphy *wiphy = wdev->wiphy;
15517 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15519 trace_cfg80211_ch_switch_started_notify(dev, chandef);
15521 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
15522 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
15524 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
15526 void
15527 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
15528 const struct cfg80211_chan_def *chandef,
15529 enum nl80211_radar_event event,
15530 struct net_device *netdev, gfp_t gfp)
15532 struct sk_buff *msg;
15533 void *hdr;
15535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15536 if (!msg)
15537 return;
15539 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
15540 if (!hdr) {
15541 nlmsg_free(msg);
15542 return;
15545 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
15546 goto nla_put_failure;
15548 /* NOP and radar events don't need a netdev parameter */
15549 if (netdev) {
15550 struct wireless_dev *wdev = netdev->ieee80211_ptr;
15552 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15553 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15554 NL80211_ATTR_PAD))
15555 goto nla_put_failure;
15558 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
15559 goto nla_put_failure;
15561 if (nl80211_send_chandef(msg, chandef))
15562 goto nla_put_failure;
15564 genlmsg_end(msg, hdr);
15566 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15567 NL80211_MCGRP_MLME, gfp);
15568 return;
15570 nla_put_failure:
15571 nlmsg_free(msg);
15574 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
15575 struct sta_opmode_info *sta_opmode,
15576 gfp_t gfp)
15578 struct sk_buff *msg;
15579 struct wireless_dev *wdev = dev->ieee80211_ptr;
15580 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15581 void *hdr;
15583 if (WARN_ON(!mac))
15584 return;
15586 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15587 if (!msg)
15588 return;
15590 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
15591 if (!hdr) {
15592 nlmsg_free(msg);
15593 return;
15596 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
15597 goto nla_put_failure;
15599 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15600 goto nla_put_failure;
15602 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15603 goto nla_put_failure;
15605 if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
15606 nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
15607 goto nla_put_failure;
15609 if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
15610 nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
15611 goto nla_put_failure;
15613 if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
15614 nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
15615 goto nla_put_failure;
15617 genlmsg_end(msg, hdr);
15619 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15620 NL80211_MCGRP_MLME, gfp);
15622 return;
15624 nla_put_failure:
15625 nlmsg_free(msg);
15627 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
15629 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
15630 u64 cookie, bool acked, s32 ack_signal,
15631 bool is_valid_ack_signal, gfp_t gfp)
15633 struct wireless_dev *wdev = dev->ieee80211_ptr;
15634 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15635 struct sk_buff *msg;
15636 void *hdr;
15638 trace_cfg80211_probe_status(dev, addr, cookie, acked);
15640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15642 if (!msg)
15643 return;
15645 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
15646 if (!hdr) {
15647 nlmsg_free(msg);
15648 return;
15651 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15652 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15653 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15654 nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15655 NL80211_ATTR_PAD) ||
15656 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
15657 (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
15658 ack_signal)))
15659 goto nla_put_failure;
15661 genlmsg_end(msg, hdr);
15663 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15664 NL80211_MCGRP_MLME, gfp);
15665 return;
15667 nla_put_failure:
15668 nlmsg_free(msg);
15670 EXPORT_SYMBOL(cfg80211_probe_status);
15672 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
15673 const u8 *frame, size_t len,
15674 int freq, int sig_dbm)
15676 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15677 struct sk_buff *msg;
15678 void *hdr;
15679 struct cfg80211_beacon_registration *reg;
15681 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
15683 spin_lock_bh(&rdev->beacon_registrations_lock);
15684 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
15685 msg = nlmsg_new(len + 100, GFP_ATOMIC);
15686 if (!msg) {
15687 spin_unlock_bh(&rdev->beacon_registrations_lock);
15688 return;
15691 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15692 if (!hdr)
15693 goto nla_put_failure;
15695 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15696 (freq &&
15697 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
15698 (sig_dbm &&
15699 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15700 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
15701 goto nla_put_failure;
15703 genlmsg_end(msg, hdr);
15705 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
15707 spin_unlock_bh(&rdev->beacon_registrations_lock);
15708 return;
15710 nla_put_failure:
15711 spin_unlock_bh(&rdev->beacon_registrations_lock);
15712 nlmsg_free(msg);
15714 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
15716 #ifdef CONFIG_PM
15717 static int cfg80211_net_detect_results(struct sk_buff *msg,
15718 struct cfg80211_wowlan_wakeup *wakeup)
15720 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
15721 struct nlattr *nl_results, *nl_match, *nl_freqs;
15722 int i, j;
15724 nl_results = nla_nest_start(
15725 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
15726 if (!nl_results)
15727 return -EMSGSIZE;
15729 for (i = 0; i < nd->n_matches; i++) {
15730 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
15732 nl_match = nla_nest_start(msg, i);
15733 if (!nl_match)
15734 break;
15736 /* The SSID attribute is optional in nl80211, but for
15737 * simplicity reasons it's always present in the
15738 * cfg80211 structure. If a driver can't pass the
15739 * SSID, that needs to be changed. A zero length SSID
15740 * is still a valid SSID (wildcard), so it cannot be
15741 * used for this purpose.
15743 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
15744 match->ssid.ssid)) {
15745 nla_nest_cancel(msg, nl_match);
15746 goto out;
15749 if (match->n_channels) {
15750 nl_freqs = nla_nest_start(
15751 msg, NL80211_ATTR_SCAN_FREQUENCIES);
15752 if (!nl_freqs) {
15753 nla_nest_cancel(msg, nl_match);
15754 goto out;
15757 for (j = 0; j < match->n_channels; j++) {
15758 if (nla_put_u32(msg, j, match->channels[j])) {
15759 nla_nest_cancel(msg, nl_freqs);
15760 nla_nest_cancel(msg, nl_match);
15761 goto out;
15765 nla_nest_end(msg, nl_freqs);
15768 nla_nest_end(msg, nl_match);
15771 out:
15772 nla_nest_end(msg, nl_results);
15773 return 0;
15776 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
15777 struct cfg80211_wowlan_wakeup *wakeup,
15778 gfp_t gfp)
15780 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15781 struct sk_buff *msg;
15782 void *hdr;
15783 int size = 200;
15785 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
15787 if (wakeup)
15788 size += wakeup->packet_present_len;
15790 msg = nlmsg_new(size, gfp);
15791 if (!msg)
15792 return;
15794 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
15795 if (!hdr)
15796 goto free_msg;
15798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15799 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15800 NL80211_ATTR_PAD))
15801 goto free_msg;
15803 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15804 wdev->netdev->ifindex))
15805 goto free_msg;
15807 if (wakeup) {
15808 struct nlattr *reasons;
15810 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
15811 if (!reasons)
15812 goto free_msg;
15814 if (wakeup->disconnect &&
15815 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
15816 goto free_msg;
15817 if (wakeup->magic_pkt &&
15818 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
15819 goto free_msg;
15820 if (wakeup->gtk_rekey_failure &&
15821 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
15822 goto free_msg;
15823 if (wakeup->eap_identity_req &&
15824 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
15825 goto free_msg;
15826 if (wakeup->four_way_handshake &&
15827 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
15828 goto free_msg;
15829 if (wakeup->rfkill_release &&
15830 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
15831 goto free_msg;
15833 if (wakeup->pattern_idx >= 0 &&
15834 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
15835 wakeup->pattern_idx))
15836 goto free_msg;
15838 if (wakeup->tcp_match &&
15839 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
15840 goto free_msg;
15842 if (wakeup->tcp_connlost &&
15843 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
15844 goto free_msg;
15846 if (wakeup->tcp_nomoretokens &&
15847 nla_put_flag(msg,
15848 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
15849 goto free_msg;
15851 if (wakeup->packet) {
15852 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
15853 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
15855 if (!wakeup->packet_80211) {
15856 pkt_attr =
15857 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
15858 len_attr =
15859 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
15862 if (wakeup->packet_len &&
15863 nla_put_u32(msg, len_attr, wakeup->packet_len))
15864 goto free_msg;
15866 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
15867 wakeup->packet))
15868 goto free_msg;
15871 if (wakeup->net_detect &&
15872 cfg80211_net_detect_results(msg, wakeup))
15873 goto free_msg;
15875 nla_nest_end(msg, reasons);
15878 genlmsg_end(msg, hdr);
15880 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15881 NL80211_MCGRP_MLME, gfp);
15882 return;
15884 free_msg:
15885 nlmsg_free(msg);
15887 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
15888 #endif
15890 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
15891 enum nl80211_tdls_operation oper,
15892 u16 reason_code, gfp_t gfp)
15894 struct wireless_dev *wdev = dev->ieee80211_ptr;
15895 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15896 struct sk_buff *msg;
15897 void *hdr;
15899 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
15900 reason_code);
15902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15903 if (!msg)
15904 return;
15906 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
15907 if (!hdr) {
15908 nlmsg_free(msg);
15909 return;
15912 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15913 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15914 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
15915 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
15916 (reason_code > 0 &&
15917 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
15918 goto nla_put_failure;
15920 genlmsg_end(msg, hdr);
15922 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15923 NL80211_MCGRP_MLME, gfp);
15924 return;
15926 nla_put_failure:
15927 nlmsg_free(msg);
15929 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
15931 static int nl80211_netlink_notify(struct notifier_block * nb,
15932 unsigned long state,
15933 void *_notify)
15935 struct netlink_notify *notify = _notify;
15936 struct cfg80211_registered_device *rdev;
15937 struct wireless_dev *wdev;
15938 struct cfg80211_beacon_registration *reg, *tmp;
15940 if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
15941 return NOTIFY_DONE;
15943 rcu_read_lock();
15945 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
15946 struct cfg80211_sched_scan_request *sched_scan_req;
15948 list_for_each_entry_rcu(sched_scan_req,
15949 &rdev->sched_scan_req_list,
15950 list) {
15951 if (sched_scan_req->owner_nlportid == notify->portid) {
15952 sched_scan_req->nl_owner_dead = true;
15953 schedule_work(&rdev->sched_scan_stop_wk);
15957 list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
15958 cfg80211_mlme_unregister_socket(wdev, notify->portid);
15960 if (wdev->owner_nlportid == notify->portid) {
15961 wdev->nl_owner_dead = true;
15962 schedule_work(&rdev->destroy_work);
15963 } else if (wdev->conn_owner_nlportid == notify->portid) {
15964 schedule_work(&wdev->disconnect_wk);
15968 spin_lock_bh(&rdev->beacon_registrations_lock);
15969 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
15970 list) {
15971 if (reg->nlportid == notify->portid) {
15972 list_del(&reg->list);
15973 kfree(reg);
15974 break;
15977 spin_unlock_bh(&rdev->beacon_registrations_lock);
15980 rcu_read_unlock();
15983 * It is possible that the user space process that is controlling the
15984 * indoor setting disappeared, so notify the regulatory core.
15986 regulatory_netlink_notify(notify->portid);
15987 return NOTIFY_OK;
15990 static struct notifier_block nl80211_netlink_notifier = {
15991 .notifier_call = nl80211_netlink_notify,
15994 void cfg80211_ft_event(struct net_device *netdev,
15995 struct cfg80211_ft_event_params *ft_event)
15997 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
15998 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15999 struct sk_buff *msg;
16000 void *hdr;
16002 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16004 if (!ft_event->target_ap)
16005 return;
16007 msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16008 GFP_KERNEL);
16009 if (!msg)
16010 return;
16012 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16013 if (!hdr)
16014 goto out;
16016 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16017 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16018 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16019 goto out;
16021 if (ft_event->ies &&
16022 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16023 goto out;
16024 if (ft_event->ric_ies &&
16025 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16026 ft_event->ric_ies))
16027 goto out;
16029 genlmsg_end(msg, hdr);
16031 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16032 NL80211_MCGRP_MLME, GFP_KERNEL);
16033 return;
16034 out:
16035 nlmsg_free(msg);
16037 EXPORT_SYMBOL(cfg80211_ft_event);
16039 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16041 struct cfg80211_registered_device *rdev;
16042 struct sk_buff *msg;
16043 void *hdr;
16044 u32 nlportid;
16046 rdev = wiphy_to_rdev(wdev->wiphy);
16047 if (!rdev->crit_proto_nlportid)
16048 return;
16050 nlportid = rdev->crit_proto_nlportid;
16051 rdev->crit_proto_nlportid = 0;
16053 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16054 if (!msg)
16055 return;
16057 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16058 if (!hdr)
16059 goto nla_put_failure;
16061 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16062 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16063 NL80211_ATTR_PAD))
16064 goto nla_put_failure;
16066 genlmsg_end(msg, hdr);
16068 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16069 return;
16071 nla_put_failure:
16072 nlmsg_free(msg);
16074 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16076 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16078 struct wiphy *wiphy = wdev->wiphy;
16079 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16080 struct sk_buff *msg;
16081 void *hdr;
16083 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16084 if (!msg)
16085 return;
16087 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16088 if (!hdr)
16089 goto out;
16091 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16092 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16093 nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16094 NL80211_ATTR_PAD))
16095 goto out;
16097 genlmsg_end(msg, hdr);
16099 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16100 NL80211_MCGRP_MLME, GFP_KERNEL);
16101 return;
16102 out:
16103 nlmsg_free(msg);
16106 int cfg80211_external_auth_request(struct net_device *dev,
16107 struct cfg80211_external_auth_params *params,
16108 gfp_t gfp)
16110 struct wireless_dev *wdev = dev->ieee80211_ptr;
16111 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16112 struct sk_buff *msg;
16113 void *hdr;
16115 if (!wdev->conn_owner_nlportid)
16116 return -EINVAL;
16118 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16119 if (!msg)
16120 return -ENOMEM;
16122 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16123 if (!hdr)
16124 goto nla_put_failure;
16126 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16127 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16128 nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16129 nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16130 params->action) ||
16131 nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16132 nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16133 params->ssid.ssid))
16134 goto nla_put_failure;
16136 genlmsg_end(msg, hdr);
16137 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16138 wdev->conn_owner_nlportid);
16139 return 0;
16141 nla_put_failure:
16142 nlmsg_free(msg);
16143 return -ENOBUFS;
16145 EXPORT_SYMBOL(cfg80211_external_auth_request);
16147 /* initialisation/exit functions */
16149 int __init nl80211_init(void)
16151 int err;
16153 err = genl_register_family(&nl80211_fam);
16154 if (err)
16155 return err;
16157 err = netlink_register_notifier(&nl80211_netlink_notifier);
16158 if (err)
16159 goto err_out;
16161 return 0;
16162 err_out:
16163 genl_unregister_family(&nl80211_fam);
16164 return err;
16167 void nl80211_exit(void)
16169 netlink_unregister_notifier(&nl80211_netlink_notifier);
16170 genl_unregister_family(&nl80211_fam);