dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / net / wireless / nl80211.c
blob642a78079ae1dff0a4f4e6d0dd06b7803ce78853
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 Intel Deutschland GmbH
7 */
9 #include <linux/if.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14 #include <linux/if_ether.h>
15 #include <linux/ieee80211.h>
16 #include <linux/nl80211.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/netlink.h>
19 #include <linux/nospec.h>
20 #include <linux/etherdevice.h>
21 #include <net/net_namespace.h>
22 #include <net/genetlink.h>
23 #include <net/cfg80211.h>
24 #include <net/sock.h>
25 #include <net/inet_connection_sock.h>
26 #include "core.h"
27 #include "nl80211.h"
28 #include "reg.h"
29 #include "rdev-ops.h"
31 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
32 struct genl_info *info,
33 struct cfg80211_crypto_settings *settings,
34 int cipher_limit);
36 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
37 struct genl_info *info);
38 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
39 struct genl_info *info);
41 /* the netlink family */
42 static struct genl_family nl80211_fam = {
43 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
44 .name = NL80211_GENL_NAME, /* have users key off the name instead */
45 .hdrsize = 0, /* no private header */
46 .version = 1, /* no particular meaning now */
47 .maxattr = NL80211_ATTR_MAX,
48 .netnsok = true,
49 .pre_doit = nl80211_pre_doit,
50 .post_doit = nl80211_post_doit,
53 /* multicast groups */
54 enum nl80211_multicast_groups {
55 NL80211_MCGRP_CONFIG,
56 NL80211_MCGRP_SCAN,
57 NL80211_MCGRP_REGULATORY,
58 NL80211_MCGRP_MLME,
59 NL80211_MCGRP_VENDOR,
60 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
63 static const struct genl_multicast_group nl80211_mcgrps[] = {
64 [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
65 [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
66 [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
67 [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
68 [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
69 #ifdef CONFIG_NL80211_TESTMODE
70 [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
71 #endif
74 /* returns ERR_PTR values */
75 static struct wireless_dev *
76 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
78 struct cfg80211_registered_device *rdev;
79 struct wireless_dev *result = NULL;
80 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
81 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
82 u64 wdev_id;
83 int wiphy_idx = -1;
84 int ifidx = -1;
86 ASSERT_RTNL();
88 if (!have_ifidx && !have_wdev_id)
89 return ERR_PTR(-EINVAL);
91 if (have_ifidx)
92 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
93 if (have_wdev_id) {
94 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
95 wiphy_idx = wdev_id >> 32;
98 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
99 struct wireless_dev *wdev;
101 if (wiphy_net(&rdev->wiphy) != netns)
102 continue;
104 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
105 continue;
107 list_for_each_entry(wdev, &rdev->wdev_list, list) {
108 if (have_ifidx && wdev->netdev &&
109 wdev->netdev->ifindex == ifidx) {
110 result = wdev;
111 break;
113 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
114 result = wdev;
115 break;
119 if (result)
120 break;
123 if (result)
124 return result;
125 return ERR_PTR(-ENODEV);
128 static struct cfg80211_registered_device *
129 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
131 struct cfg80211_registered_device *rdev = NULL, *tmp;
132 struct net_device *netdev;
134 ASSERT_RTNL();
136 if (!attrs[NL80211_ATTR_WIPHY] &&
137 !attrs[NL80211_ATTR_IFINDEX] &&
138 !attrs[NL80211_ATTR_WDEV])
139 return ERR_PTR(-EINVAL);
141 if (attrs[NL80211_ATTR_WIPHY])
142 rdev = cfg80211_rdev_by_wiphy_idx(
143 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
145 if (attrs[NL80211_ATTR_WDEV]) {
146 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
147 struct wireless_dev *wdev;
148 bool found = false;
150 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
151 if (tmp) {
152 /* make sure wdev exists */
153 list_for_each_entry(wdev, &tmp->wdev_list, list) {
154 if (wdev->identifier != (u32)wdev_id)
155 continue;
156 found = true;
157 break;
160 if (!found)
161 tmp = NULL;
163 if (rdev && tmp != rdev)
164 return ERR_PTR(-EINVAL);
165 rdev = tmp;
169 if (attrs[NL80211_ATTR_IFINDEX]) {
170 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
171 netdev = __dev_get_by_index(netns, ifindex);
172 if (netdev) {
173 if (netdev->ieee80211_ptr)
174 tmp = wiphy_to_rdev(
175 netdev->ieee80211_ptr->wiphy);
176 else
177 tmp = NULL;
179 /* not wireless device -- return error */
180 if (!tmp)
181 return ERR_PTR(-EINVAL);
183 /* mismatch -- return error */
184 if (rdev && tmp != rdev)
185 return ERR_PTR(-EINVAL);
187 rdev = tmp;
191 if (!rdev)
192 return ERR_PTR(-ENODEV);
194 if (netns != wiphy_net(&rdev->wiphy))
195 return ERR_PTR(-ENODEV);
197 return rdev;
201 * This function returns a pointer to the driver
202 * that the genl_info item that is passed refers to.
204 * The result of this can be a PTR_ERR and hence must
205 * be checked with IS_ERR() for errors.
207 static struct cfg80211_registered_device *
208 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
210 return __cfg80211_rdev_from_attrs(netns, info->attrs);
213 /* policy for the attributes */
214 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
215 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
216 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
217 .len = 20-1 },
218 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
220 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
221 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
222 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
223 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
224 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
227 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
228 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
229 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
230 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
231 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
233 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
234 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
237 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
238 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
240 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
241 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
242 .len = WLAN_MAX_KEY_LEN },
243 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
244 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
245 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
246 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
247 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
249 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
250 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
251 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_DATA_LEN },
253 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_DATA_LEN },
255 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
256 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
257 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
258 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
259 .len = NL80211_MAX_SUPP_RATES },
260 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
261 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
262 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
263 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
264 .len = IEEE80211_MAX_MESH_ID_LEN },
265 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
267 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
268 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
271 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
274 .len = NL80211_MAX_SUPP_RATES },
275 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
277 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
278 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
280 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
282 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
283 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
284 .len = IEEE80211_MAX_DATA_LEN },
285 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
286 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
288 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
289 .len = IEEE80211_MAX_SSID_LEN },
290 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
291 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
292 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
293 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
294 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
295 [NL80211_ATTR_STA_FLAGS2] = {
296 .len = sizeof(struct nl80211_sta_flag_update),
298 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
299 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
300 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
301 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
302 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
303 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
304 [NL80211_ATTR_PID] = { .type = NLA_U32 },
305 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
306 [NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
307 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
308 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
309 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
310 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
311 .len = IEEE80211_MAX_DATA_LEN },
312 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
313 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
314 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
315 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
316 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
317 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
319 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
320 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
321 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
322 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
323 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
324 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
325 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
326 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
327 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
328 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
329 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
330 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
331 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
332 .len = IEEE80211_MAX_DATA_LEN },
333 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
336 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
337 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
338 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
339 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
340 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
342 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
343 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
344 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
345 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
346 .len = IEEE80211_MAX_DATA_LEN },
347 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
348 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
350 .len = NL80211_HT_CAPABILITY_LEN
352 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
353 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
354 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
355 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
356 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
357 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
358 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
359 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
360 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
361 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
362 [NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
363 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
364 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
365 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
366 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
367 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
368 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
369 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
370 .len = NL80211_VHT_CAPABILITY_LEN,
372 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
373 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
374 .len = IEEE80211_MAX_DATA_LEN },
375 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
376 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
377 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
378 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
379 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
380 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
381 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
382 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
383 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
384 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
385 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
386 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
387 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
388 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
389 .len = IEEE80211_QOS_MAP_LEN_MAX },
390 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
391 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
392 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
393 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
394 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
395 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
396 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
397 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
398 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
399 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
400 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
401 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
402 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
403 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
404 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
407 /* policy for the key attributes */
408 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
409 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
410 [NL80211_KEY_IDX] = { .type = NLA_U8 },
411 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
412 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
413 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
414 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
415 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
416 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
419 /* policy for the key default flags */
420 static const struct nla_policy
421 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
422 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
423 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
426 /* policy for WoWLAN attributes */
427 static const struct nla_policy
428 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
429 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
430 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
431 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
432 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
433 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
434 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
435 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
436 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
437 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
438 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
441 static const struct nla_policy
442 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
443 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
444 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
445 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
446 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
447 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
448 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
449 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
450 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
452 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
453 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
455 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
456 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
457 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
460 /* policy for coalesce rule attributes */
461 static const struct nla_policy
462 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
463 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
464 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
465 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
468 /* policy for GTK rekey offload attributes */
469 static const struct nla_policy
470 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
471 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
472 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
473 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
476 static const struct nla_policy
477 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
478 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
479 .len = IEEE80211_MAX_SSID_LEN },
480 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
483 static const struct nla_policy
484 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
485 [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
486 [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
489 /* policy for packet pattern attributes */
490 static const struct nla_policy
491 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
492 [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
493 [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
494 [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
497 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
498 struct netlink_callback *cb,
499 struct cfg80211_registered_device **rdev,
500 struct wireless_dev **wdev)
502 int err;
504 if (!cb->args[0]) {
505 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
506 nl80211_fam.attrbuf, nl80211_fam.maxattr,
507 nl80211_policy);
508 if (err)
509 return err;
511 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
512 nl80211_fam.attrbuf);
513 if (IS_ERR(*wdev))
514 return PTR_ERR(*wdev);
515 *rdev = wiphy_to_rdev((*wdev)->wiphy);
516 /* 0 is the first index - add 1 to parse only once */
517 cb->args[0] = (*rdev)->wiphy_idx + 1;
518 cb->args[1] = (*wdev)->identifier;
519 } else {
520 /* subtract the 1 again here */
521 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
522 struct wireless_dev *tmp;
524 if (!wiphy)
525 return -ENODEV;
526 *rdev = wiphy_to_rdev(wiphy);
527 *wdev = NULL;
529 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
530 if (tmp->identifier == cb->args[1]) {
531 *wdev = tmp;
532 break;
536 if (!*wdev)
537 return -ENODEV;
540 return 0;
543 /* IE validation */
544 static bool is_valid_ie_attr(const struct nlattr *attr)
546 const u8 *pos;
547 int len;
549 if (!attr)
550 return true;
552 pos = nla_data(attr);
553 len = nla_len(attr);
555 while (len) {
556 u8 elemlen;
558 if (len < 2)
559 return false;
560 len -= 2;
562 elemlen = pos[1];
563 if (elemlen > len)
564 return false;
566 len -= elemlen;
567 pos += 2 + elemlen;
570 return true;
573 /* message building helper */
574 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
575 int flags, u8 cmd)
577 /* since there is no private header just add the generic one */
578 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
581 static int nl80211_msg_put_channel(struct sk_buff *msg,
582 struct ieee80211_channel *chan,
583 bool large)
585 /* Some channels must be completely excluded from the
586 * list to protect old user-space tools from breaking
588 if (!large && chan->flags &
589 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
590 return 0;
592 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
593 chan->center_freq))
594 goto nla_put_failure;
596 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
597 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
598 goto nla_put_failure;
599 if (chan->flags & IEEE80211_CHAN_NO_IR) {
600 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
601 goto nla_put_failure;
602 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
603 goto nla_put_failure;
605 if (chan->flags & IEEE80211_CHAN_RADAR) {
606 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
607 goto nla_put_failure;
608 if (large) {
609 u32 time;
611 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
613 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
614 chan->dfs_state))
615 goto nla_put_failure;
616 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
617 time))
618 goto nla_put_failure;
619 if (nla_put_u32(msg,
620 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
621 chan->dfs_cac_ms))
622 goto nla_put_failure;
626 if (large) {
627 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
628 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
629 goto nla_put_failure;
630 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
631 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
632 goto nla_put_failure;
633 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
634 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
635 goto nla_put_failure;
636 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
637 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
638 goto nla_put_failure;
639 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
640 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
641 goto nla_put_failure;
642 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
643 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
644 goto nla_put_failure;
645 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
646 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
647 goto nla_put_failure;
648 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
649 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
650 goto nla_put_failure;
653 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
654 DBM_TO_MBM(chan->max_power)))
655 goto nla_put_failure;
657 return 0;
659 nla_put_failure:
660 return -ENOBUFS;
663 /* netlink command implementations */
665 struct key_parse {
666 struct key_params p;
667 int idx;
668 int type;
669 bool def, defmgmt;
670 bool def_uni, def_multi;
673 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
675 struct nlattr *tb[NL80211_KEY_MAX + 1];
676 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
677 nl80211_key_policy);
678 if (err)
679 return err;
681 k->def = !!tb[NL80211_KEY_DEFAULT];
682 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
684 if (k->def) {
685 k->def_uni = true;
686 k->def_multi = true;
688 if (k->defmgmt)
689 k->def_multi = true;
691 if (tb[NL80211_KEY_IDX])
692 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
694 if (tb[NL80211_KEY_DATA]) {
695 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
696 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
699 if (tb[NL80211_KEY_SEQ]) {
700 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
701 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
704 if (tb[NL80211_KEY_CIPHER])
705 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
707 if (tb[NL80211_KEY_TYPE]) {
708 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
709 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
710 return -EINVAL;
713 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
714 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
715 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
716 tb[NL80211_KEY_DEFAULT_TYPES],
717 nl80211_key_default_policy);
718 if (err)
719 return err;
721 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
722 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
725 return 0;
728 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
730 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
731 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
732 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
735 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
736 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
737 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
740 if (info->attrs[NL80211_ATTR_KEY_IDX])
741 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
743 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
744 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
746 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
747 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
749 if (k->def) {
750 k->def_uni = true;
751 k->def_multi = true;
753 if (k->defmgmt)
754 k->def_multi = true;
756 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
757 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
758 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
759 return -EINVAL;
762 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
763 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
764 int err = nla_parse_nested(
765 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
766 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
767 nl80211_key_default_policy);
768 if (err)
769 return err;
771 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
772 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
775 return 0;
778 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
780 int err;
782 memset(k, 0, sizeof(*k));
783 k->idx = -1;
784 k->type = -1;
786 if (info->attrs[NL80211_ATTR_KEY])
787 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
788 else
789 err = nl80211_parse_key_old(info, k);
791 if (err)
792 return err;
794 if (k->def && k->defmgmt)
795 return -EINVAL;
797 if (k->defmgmt) {
798 if (k->def_uni || !k->def_multi)
799 return -EINVAL;
802 if (k->idx != -1) {
803 if (k->defmgmt) {
804 if (k->idx < 4 || k->idx > 5)
805 return -EINVAL;
806 } else if (k->def) {
807 if (k->idx < 0 || k->idx > 3)
808 return -EINVAL;
809 } else {
810 if (k->idx < 0 || k->idx > 5)
811 return -EINVAL;
815 return 0;
818 static struct cfg80211_cached_keys *
819 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
820 struct nlattr *keys, bool *no_ht)
822 struct key_parse parse;
823 struct nlattr *key;
824 struct cfg80211_cached_keys *result;
825 int rem, err, def = 0;
827 result = kzalloc(sizeof(*result), GFP_KERNEL);
828 if (!result)
829 return ERR_PTR(-ENOMEM);
831 result->def = -1;
832 result->defmgmt = -1;
834 nla_for_each_nested(key, keys, rem) {
835 memset(&parse, 0, sizeof(parse));
836 parse.idx = -1;
838 err = nl80211_parse_key_new(key, &parse);
839 if (err)
840 goto error;
841 err = -EINVAL;
842 if (!parse.p.key)
843 goto error;
844 if (parse.idx < 0 || parse.idx > 4)
845 goto error;
846 if (parse.def) {
847 if (def)
848 goto error;
849 def = 1;
850 result->def = parse.idx;
851 if (!parse.def_uni || !parse.def_multi)
852 goto error;
853 } else if (parse.defmgmt)
854 goto error;
855 err = cfg80211_validate_key_settings(rdev, &parse.p,
856 parse.idx, false, NULL);
857 if (err)
858 goto error;
859 result->params[parse.idx].cipher = parse.p.cipher;
860 result->params[parse.idx].key_len = parse.p.key_len;
861 result->params[parse.idx].key = result->data[parse.idx];
862 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
864 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
865 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
866 if (no_ht)
867 *no_ht = true;
871 return result;
872 error:
873 kfree(result);
874 return ERR_PTR(err);
877 static int nl80211_key_allowed(struct wireless_dev *wdev)
879 ASSERT_WDEV_LOCK(wdev);
881 switch (wdev->iftype) {
882 case NL80211_IFTYPE_AP:
883 case NL80211_IFTYPE_AP_VLAN:
884 case NL80211_IFTYPE_P2P_GO:
885 case NL80211_IFTYPE_MESH_POINT:
886 break;
887 case NL80211_IFTYPE_ADHOC:
888 case NL80211_IFTYPE_STATION:
889 case NL80211_IFTYPE_P2P_CLIENT:
890 if (!wdev->current_bss)
891 return -ENOLINK;
892 break;
893 case NL80211_IFTYPE_UNSPECIFIED:
894 case NL80211_IFTYPE_OCB:
895 case NL80211_IFTYPE_MONITOR:
896 case NL80211_IFTYPE_P2P_DEVICE:
897 case NL80211_IFTYPE_WDS:
898 case NUM_NL80211_IFTYPES:
899 return -EINVAL;
902 return 0;
905 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
906 struct nlattr *tb)
908 struct ieee80211_channel *chan;
910 if (tb == NULL)
911 return NULL;
912 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
913 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
914 return NULL;
915 return chan;
918 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
920 struct nlattr *nl_modes = nla_nest_start(msg, attr);
921 int i;
923 if (!nl_modes)
924 goto nla_put_failure;
926 i = 0;
927 while (ifmodes) {
928 if ((ifmodes & 1) && nla_put_flag(msg, i))
929 goto nla_put_failure;
930 ifmodes >>= 1;
931 i++;
934 nla_nest_end(msg, nl_modes);
935 return 0;
937 nla_put_failure:
938 return -ENOBUFS;
941 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
942 struct sk_buff *msg,
943 bool large)
945 struct nlattr *nl_combis;
946 int i, j;
948 nl_combis = nla_nest_start(msg,
949 NL80211_ATTR_INTERFACE_COMBINATIONS);
950 if (!nl_combis)
951 goto nla_put_failure;
953 for (i = 0; i < wiphy->n_iface_combinations; i++) {
954 const struct ieee80211_iface_combination *c;
955 struct nlattr *nl_combi, *nl_limits;
957 c = &wiphy->iface_combinations[i];
959 nl_combi = nla_nest_start(msg, i + 1);
960 if (!nl_combi)
961 goto nla_put_failure;
963 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
964 if (!nl_limits)
965 goto nla_put_failure;
967 for (j = 0; j < c->n_limits; j++) {
968 struct nlattr *nl_limit;
970 nl_limit = nla_nest_start(msg, j + 1);
971 if (!nl_limit)
972 goto nla_put_failure;
973 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
974 c->limits[j].max))
975 goto nla_put_failure;
976 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
977 c->limits[j].types))
978 goto nla_put_failure;
979 nla_nest_end(msg, nl_limit);
982 nla_nest_end(msg, nl_limits);
984 if (c->beacon_int_infra_match &&
985 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
986 goto nla_put_failure;
987 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
988 c->num_different_channels) ||
989 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
990 c->max_interfaces))
991 goto nla_put_failure;
992 if (large &&
993 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
994 c->radar_detect_widths) ||
995 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
996 c->radar_detect_regions)))
997 goto nla_put_failure;
999 nla_nest_end(msg, nl_combi);
1002 nla_nest_end(msg, nl_combis);
1004 return 0;
1005 nla_put_failure:
1006 return -ENOBUFS;
1009 #ifdef CONFIG_PM
1010 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1011 struct sk_buff *msg)
1013 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1014 struct nlattr *nl_tcp;
1016 if (!tcp)
1017 return 0;
1019 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1020 if (!nl_tcp)
1021 return -ENOBUFS;
1023 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1024 tcp->data_payload_max))
1025 return -ENOBUFS;
1027 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1028 tcp->data_payload_max))
1029 return -ENOBUFS;
1031 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1032 return -ENOBUFS;
1034 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1035 sizeof(*tcp->tok), tcp->tok))
1036 return -ENOBUFS;
1038 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1039 tcp->data_interval_max))
1040 return -ENOBUFS;
1042 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1043 tcp->wake_payload_max))
1044 return -ENOBUFS;
1046 nla_nest_end(msg, nl_tcp);
1047 return 0;
1050 static int nl80211_send_wowlan(struct sk_buff *msg,
1051 struct cfg80211_registered_device *rdev,
1052 bool large)
1054 struct nlattr *nl_wowlan;
1056 if (!rdev->wiphy.wowlan)
1057 return 0;
1059 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1060 if (!nl_wowlan)
1061 return -ENOBUFS;
1063 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1064 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1065 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1066 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1067 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1068 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1069 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1070 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1071 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1072 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1073 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1074 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1075 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1076 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1077 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1078 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1079 return -ENOBUFS;
1081 if (rdev->wiphy.wowlan->n_patterns) {
1082 struct nl80211_pattern_support pat = {
1083 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1084 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1085 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1086 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1089 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1090 sizeof(pat), &pat))
1091 return -ENOBUFS;
1094 if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1095 nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1096 rdev->wiphy.wowlan->max_nd_match_sets))
1097 return -ENOBUFS;
1099 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1100 return -ENOBUFS;
1102 nla_nest_end(msg, nl_wowlan);
1104 return 0;
1106 #endif
1108 static int nl80211_send_coalesce(struct sk_buff *msg,
1109 struct cfg80211_registered_device *rdev)
1111 struct nl80211_coalesce_rule_support rule;
1113 if (!rdev->wiphy.coalesce)
1114 return 0;
1116 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1117 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1118 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1119 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1120 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1121 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1123 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1124 return -ENOBUFS;
1126 return 0;
1129 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1130 struct ieee80211_supported_band *sband)
1132 struct nlattr *nl_rates, *nl_rate;
1133 struct ieee80211_rate *rate;
1134 int i;
1136 /* add HT info */
1137 if (sband->ht_cap.ht_supported &&
1138 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1139 sizeof(sband->ht_cap.mcs),
1140 &sband->ht_cap.mcs) ||
1141 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1142 sband->ht_cap.cap) ||
1143 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1144 sband->ht_cap.ampdu_factor) ||
1145 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1146 sband->ht_cap.ampdu_density)))
1147 return -ENOBUFS;
1149 /* add VHT info */
1150 if (sband->vht_cap.vht_supported &&
1151 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1152 sizeof(sband->vht_cap.vht_mcs),
1153 &sband->vht_cap.vht_mcs) ||
1154 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1155 sband->vht_cap.cap)))
1156 return -ENOBUFS;
1158 /* add bitrates */
1159 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1160 if (!nl_rates)
1161 return -ENOBUFS;
1163 for (i = 0; i < sband->n_bitrates; i++) {
1164 nl_rate = nla_nest_start(msg, i);
1165 if (!nl_rate)
1166 return -ENOBUFS;
1168 rate = &sband->bitrates[i];
1169 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1170 rate->bitrate))
1171 return -ENOBUFS;
1172 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1173 nla_put_flag(msg,
1174 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1175 return -ENOBUFS;
1177 nla_nest_end(msg, nl_rate);
1180 nla_nest_end(msg, nl_rates);
1182 return 0;
1185 static int
1186 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1187 const struct ieee80211_txrx_stypes *mgmt_stypes)
1189 u16 stypes;
1190 struct nlattr *nl_ftypes, *nl_ifs;
1191 enum nl80211_iftype ift;
1192 int i;
1194 if (!mgmt_stypes)
1195 return 0;
1197 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1198 if (!nl_ifs)
1199 return -ENOBUFS;
1201 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1202 nl_ftypes = nla_nest_start(msg, ift);
1203 if (!nl_ftypes)
1204 return -ENOBUFS;
1205 i = 0;
1206 stypes = mgmt_stypes[ift].tx;
1207 while (stypes) {
1208 if ((stypes & 1) &&
1209 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1210 (i << 4) | IEEE80211_FTYPE_MGMT))
1211 return -ENOBUFS;
1212 stypes >>= 1;
1213 i++;
1215 nla_nest_end(msg, nl_ftypes);
1218 nla_nest_end(msg, nl_ifs);
1220 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1221 if (!nl_ifs)
1222 return -ENOBUFS;
1224 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1225 nl_ftypes = nla_nest_start(msg, ift);
1226 if (!nl_ftypes)
1227 return -ENOBUFS;
1228 i = 0;
1229 stypes = mgmt_stypes[ift].rx;
1230 while (stypes) {
1231 if ((stypes & 1) &&
1232 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1233 (i << 4) | IEEE80211_FTYPE_MGMT))
1234 return -ENOBUFS;
1235 stypes >>= 1;
1236 i++;
1238 nla_nest_end(msg, nl_ftypes);
1240 nla_nest_end(msg, nl_ifs);
1242 return 0;
1245 struct nl80211_dump_wiphy_state {
1246 s64 filter_wiphy;
1247 long start;
1248 long split_start, band_start, chan_start;
1249 bool split;
1252 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1253 enum nl80211_commands cmd,
1254 struct sk_buff *msg, u32 portid, u32 seq,
1255 int flags, struct nl80211_dump_wiphy_state *state)
1257 void *hdr;
1258 struct nlattr *nl_bands, *nl_band;
1259 struct nlattr *nl_freqs, *nl_freq;
1260 struct nlattr *nl_cmds;
1261 enum ieee80211_band band;
1262 struct ieee80211_channel *chan;
1263 int i;
1264 const struct ieee80211_txrx_stypes *mgmt_stypes =
1265 rdev->wiphy.mgmt_stypes;
1266 u32 features;
1268 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1269 if (!hdr)
1270 return -ENOBUFS;
1272 if (WARN_ON(!state))
1273 return -EINVAL;
1275 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1276 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1277 wiphy_name(&rdev->wiphy)) ||
1278 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1279 cfg80211_rdev_list_generation))
1280 goto nla_put_failure;
1282 if (cmd != NL80211_CMD_NEW_WIPHY)
1283 goto finish;
1285 switch (state->split_start) {
1286 case 0:
1287 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1288 rdev->wiphy.retry_short) ||
1289 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1290 rdev->wiphy.retry_long) ||
1291 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1292 rdev->wiphy.frag_threshold) ||
1293 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1294 rdev->wiphy.rts_threshold) ||
1295 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1296 rdev->wiphy.coverage_class) ||
1297 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1298 rdev->wiphy.max_scan_ssids) ||
1299 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1300 rdev->wiphy.max_sched_scan_ssids) ||
1301 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1302 rdev->wiphy.max_scan_ie_len) ||
1303 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1304 rdev->wiphy.max_sched_scan_ie_len) ||
1305 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1306 rdev->wiphy.max_match_sets) ||
1307 nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1308 rdev->wiphy.max_sched_scan_plans) ||
1309 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1310 rdev->wiphy.max_sched_scan_plan_interval) ||
1311 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1312 rdev->wiphy.max_sched_scan_plan_iterations))
1313 goto nla_put_failure;
1315 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1316 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1317 goto nla_put_failure;
1318 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1319 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1320 goto nla_put_failure;
1321 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1322 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1323 goto nla_put_failure;
1324 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1325 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1326 goto nla_put_failure;
1327 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1328 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1329 goto nla_put_failure;
1330 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1331 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1332 goto nla_put_failure;
1333 state->split_start++;
1334 if (state->split)
1335 break;
1336 case 1:
1337 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1338 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1339 rdev->wiphy.cipher_suites))
1340 goto nla_put_failure;
1342 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1343 rdev->wiphy.max_num_pmkids))
1344 goto nla_put_failure;
1346 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1347 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1348 goto nla_put_failure;
1350 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1351 rdev->wiphy.available_antennas_tx) ||
1352 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1353 rdev->wiphy.available_antennas_rx))
1354 goto nla_put_failure;
1356 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1357 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1358 rdev->wiphy.probe_resp_offload))
1359 goto nla_put_failure;
1361 if ((rdev->wiphy.available_antennas_tx ||
1362 rdev->wiphy.available_antennas_rx) &&
1363 rdev->ops->get_antenna) {
1364 u32 tx_ant = 0, rx_ant = 0;
1365 int res;
1366 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1367 if (!res) {
1368 if (nla_put_u32(msg,
1369 NL80211_ATTR_WIPHY_ANTENNA_TX,
1370 tx_ant) ||
1371 nla_put_u32(msg,
1372 NL80211_ATTR_WIPHY_ANTENNA_RX,
1373 rx_ant))
1374 goto nla_put_failure;
1378 state->split_start++;
1379 if (state->split)
1380 break;
1381 case 2:
1382 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1383 rdev->wiphy.interface_modes))
1384 goto nla_put_failure;
1385 state->split_start++;
1386 if (state->split)
1387 break;
1388 case 3:
1389 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1390 if (!nl_bands)
1391 goto nla_put_failure;
1393 for (band = state->band_start;
1394 band < IEEE80211_NUM_BANDS; band++) {
1395 struct ieee80211_supported_band *sband;
1397 sband = rdev->wiphy.bands[band];
1399 if (!sband)
1400 continue;
1402 nl_band = nla_nest_start(msg, band);
1403 if (!nl_band)
1404 goto nla_put_failure;
1406 switch (state->chan_start) {
1407 case 0:
1408 if (nl80211_send_band_rateinfo(msg, sband))
1409 goto nla_put_failure;
1410 state->chan_start++;
1411 if (state->split)
1412 break;
1413 default:
1414 /* add frequencies */
1415 nl_freqs = nla_nest_start(
1416 msg, NL80211_BAND_ATTR_FREQS);
1417 if (!nl_freqs)
1418 goto nla_put_failure;
1420 for (i = state->chan_start - 1;
1421 i < sband->n_channels;
1422 i++) {
1423 nl_freq = nla_nest_start(msg, i);
1424 if (!nl_freq)
1425 goto nla_put_failure;
1427 chan = &sband->channels[i];
1429 if (nl80211_msg_put_channel(
1430 msg, chan,
1431 state->split))
1432 goto nla_put_failure;
1434 nla_nest_end(msg, nl_freq);
1435 if (state->split)
1436 break;
1438 if (i < sband->n_channels)
1439 state->chan_start = i + 2;
1440 else
1441 state->chan_start = 0;
1442 nla_nest_end(msg, nl_freqs);
1445 nla_nest_end(msg, nl_band);
1447 if (state->split) {
1448 /* start again here */
1449 if (state->chan_start)
1450 band--;
1451 break;
1454 nla_nest_end(msg, nl_bands);
1456 if (band < IEEE80211_NUM_BANDS)
1457 state->band_start = band + 1;
1458 else
1459 state->band_start = 0;
1461 /* if bands & channels are done, continue outside */
1462 if (state->band_start == 0 && state->chan_start == 0)
1463 state->split_start++;
1464 if (state->split)
1465 break;
1466 case 4:
1467 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1468 if (!nl_cmds)
1469 goto nla_put_failure;
1471 i = 0;
1472 #define CMD(op, n) \
1473 do { \
1474 if (rdev->ops->op) { \
1475 i++; \
1476 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1477 goto nla_put_failure; \
1479 } while (0)
1481 CMD(add_virtual_intf, NEW_INTERFACE);
1482 CMD(change_virtual_intf, SET_INTERFACE);
1483 CMD(add_key, NEW_KEY);
1484 CMD(start_ap, START_AP);
1485 CMD(add_station, NEW_STATION);
1486 CMD(add_mpath, NEW_MPATH);
1487 CMD(update_mesh_config, SET_MESH_CONFIG);
1488 CMD(change_bss, SET_BSS);
1489 CMD(auth, AUTHENTICATE);
1490 CMD(assoc, ASSOCIATE);
1491 CMD(deauth, DEAUTHENTICATE);
1492 CMD(disassoc, DISASSOCIATE);
1493 CMD(join_ibss, JOIN_IBSS);
1494 CMD(join_mesh, JOIN_MESH);
1495 CMD(set_pmksa, SET_PMKSA);
1496 CMD(del_pmksa, DEL_PMKSA);
1497 CMD(flush_pmksa, FLUSH_PMKSA);
1498 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1499 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1500 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1501 CMD(mgmt_tx, FRAME);
1502 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1503 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1504 i++;
1505 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1506 goto nla_put_failure;
1508 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1509 rdev->ops->join_mesh) {
1510 i++;
1511 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1512 goto nla_put_failure;
1514 CMD(set_wds_peer, SET_WDS_PEER);
1515 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1516 CMD(tdls_mgmt, TDLS_MGMT);
1517 CMD(tdls_oper, TDLS_OPER);
1519 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1520 CMD(sched_scan_start, START_SCHED_SCAN);
1521 CMD(probe_client, PROBE_CLIENT);
1522 CMD(set_noack_map, SET_NOACK_MAP);
1523 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1524 i++;
1525 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1526 goto nla_put_failure;
1528 CMD(start_p2p_device, START_P2P_DEVICE);
1529 CMD(set_mcast_rate, SET_MCAST_RATE);
1530 #ifdef CONFIG_NL80211_TESTMODE
1531 CMD(testmode_cmd, TESTMODE);
1532 #endif
1533 if (state->split) {
1534 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1535 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1536 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1537 CMD(channel_switch, CHANNEL_SWITCH);
1538 CMD(set_qos_map, SET_QOS_MAP);
1539 if (rdev->wiphy.features &
1540 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1541 CMD(add_tx_ts, ADD_TX_TS);
1543 /* add into the if now */
1544 #undef CMD
1546 if (rdev->ops->connect || rdev->ops->auth) {
1547 i++;
1548 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1549 goto nla_put_failure;
1552 if (rdev->ops->disconnect || rdev->ops->deauth) {
1553 i++;
1554 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1555 goto nla_put_failure;
1558 nla_nest_end(msg, nl_cmds);
1559 state->split_start++;
1560 if (state->split)
1561 break;
1562 case 5:
1563 if (rdev->ops->remain_on_channel &&
1564 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1565 nla_put_u32(msg,
1566 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1567 rdev->wiphy.max_remain_on_channel_duration))
1568 goto nla_put_failure;
1570 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1571 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1572 goto nla_put_failure;
1574 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1575 goto nla_put_failure;
1576 state->split_start++;
1577 if (state->split)
1578 break;
1579 case 6:
1580 #ifdef CONFIG_PM
1581 if (nl80211_send_wowlan(msg, rdev, state->split))
1582 goto nla_put_failure;
1583 state->split_start++;
1584 if (state->split)
1585 break;
1586 #else
1587 state->split_start++;
1588 #endif
1589 case 7:
1590 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1591 rdev->wiphy.software_iftypes))
1592 goto nla_put_failure;
1594 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1595 state->split))
1596 goto nla_put_failure;
1598 state->split_start++;
1599 if (state->split)
1600 break;
1601 case 8:
1602 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1603 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1604 rdev->wiphy.ap_sme_capa))
1605 goto nla_put_failure;
1607 features = rdev->wiphy.features;
1609 * We can only add the per-channel limit information if the
1610 * dump is split, otherwise it makes it too big. Therefore
1611 * only advertise it in that case.
1613 if (state->split)
1614 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1615 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1616 goto nla_put_failure;
1618 if (rdev->wiphy.ht_capa_mod_mask &&
1619 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1620 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1621 rdev->wiphy.ht_capa_mod_mask))
1622 goto nla_put_failure;
1624 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1625 rdev->wiphy.max_acl_mac_addrs &&
1626 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1627 rdev->wiphy.max_acl_mac_addrs))
1628 goto nla_put_failure;
1631 * Any information below this point is only available to
1632 * applications that can deal with it being split. This
1633 * helps ensure that newly added capabilities don't break
1634 * older tools by overrunning their buffers.
1636 * We still increment split_start so that in the split
1637 * case we'll continue with more data in the next round,
1638 * but break unconditionally so unsplit data stops here.
1640 state->split_start++;
1641 break;
1642 case 9:
1643 if (rdev->wiphy.extended_capabilities &&
1644 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1645 rdev->wiphy.extended_capabilities_len,
1646 rdev->wiphy.extended_capabilities) ||
1647 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1648 rdev->wiphy.extended_capabilities_len,
1649 rdev->wiphy.extended_capabilities_mask)))
1650 goto nla_put_failure;
1652 if (rdev->wiphy.vht_capa_mod_mask &&
1653 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1654 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1655 rdev->wiphy.vht_capa_mod_mask))
1656 goto nla_put_failure;
1658 state->split_start++;
1659 break;
1660 case 10:
1661 if (nl80211_send_coalesce(msg, rdev))
1662 goto nla_put_failure;
1664 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1665 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1666 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1667 goto nla_put_failure;
1669 if (rdev->wiphy.max_ap_assoc_sta &&
1670 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1671 rdev->wiphy.max_ap_assoc_sta))
1672 goto nla_put_failure;
1674 state->split_start++;
1675 break;
1676 case 11:
1677 if (rdev->wiphy.n_vendor_commands) {
1678 const struct nl80211_vendor_cmd_info *info;
1679 struct nlattr *nested;
1681 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1682 if (!nested)
1683 goto nla_put_failure;
1685 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1686 info = &rdev->wiphy.vendor_commands[i].info;
1687 if (nla_put(msg, i + 1, sizeof(*info), info))
1688 goto nla_put_failure;
1690 nla_nest_end(msg, nested);
1693 if (rdev->wiphy.n_vendor_events) {
1694 const struct nl80211_vendor_cmd_info *info;
1695 struct nlattr *nested;
1697 nested = nla_nest_start(msg,
1698 NL80211_ATTR_VENDOR_EVENTS);
1699 if (!nested)
1700 goto nla_put_failure;
1702 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1703 info = &rdev->wiphy.vendor_events[i];
1704 if (nla_put(msg, i + 1, sizeof(*info), info))
1705 goto nla_put_failure;
1707 nla_nest_end(msg, nested);
1709 state->split_start++;
1710 break;
1711 case 12:
1712 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1713 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1714 rdev->wiphy.max_num_csa_counters))
1715 goto nla_put_failure;
1717 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1718 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1719 goto nla_put_failure;
1721 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1722 sizeof(rdev->wiphy.ext_features),
1723 rdev->wiphy.ext_features))
1724 goto nla_put_failure;
1726 /* done */
1727 state->split_start = 0;
1728 break;
1730 finish:
1731 genlmsg_end(msg, hdr);
1732 return 0;
1734 nla_put_failure:
1735 genlmsg_cancel(msg, hdr);
1736 return -EMSGSIZE;
1739 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1740 struct netlink_callback *cb,
1741 struct nl80211_dump_wiphy_state *state)
1743 struct nlattr **tb = nl80211_fam.attrbuf;
1744 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1745 tb, nl80211_fam.maxattr, nl80211_policy);
1746 /* ignore parse errors for backward compatibility */
1747 if (ret)
1748 return 0;
1750 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1751 if (tb[NL80211_ATTR_WIPHY])
1752 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1753 if (tb[NL80211_ATTR_WDEV])
1754 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1755 if (tb[NL80211_ATTR_IFINDEX]) {
1756 struct net_device *netdev;
1757 struct cfg80211_registered_device *rdev;
1758 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1760 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1761 if (!netdev)
1762 return -ENODEV;
1763 if (netdev->ieee80211_ptr) {
1764 rdev = wiphy_to_rdev(
1765 netdev->ieee80211_ptr->wiphy);
1766 state->filter_wiphy = rdev->wiphy_idx;
1770 return 0;
1773 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1775 int idx = 0, ret;
1776 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1777 struct cfg80211_registered_device *rdev;
1779 rtnl_lock();
1780 if (!state) {
1781 state = kzalloc(sizeof(*state), GFP_KERNEL);
1782 if (!state) {
1783 rtnl_unlock();
1784 return -ENOMEM;
1786 state->filter_wiphy = -1;
1787 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1788 if (ret) {
1789 kfree(state);
1790 rtnl_unlock();
1791 return ret;
1793 cb->args[0] = (long)state;
1796 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1797 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1798 continue;
1799 if (++idx <= state->start)
1800 continue;
1801 if (state->filter_wiphy != -1 &&
1802 state->filter_wiphy != rdev->wiphy_idx)
1803 continue;
1804 /* attempt to fit multiple wiphy data chunks into the skb */
1805 do {
1806 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1807 skb,
1808 NETLINK_CB(cb->skb).portid,
1809 cb->nlh->nlmsg_seq,
1810 NLM_F_MULTI, state);
1811 if (ret < 0) {
1813 * If sending the wiphy data didn't fit (ENOBUFS
1814 * or EMSGSIZE returned), this SKB is still
1815 * empty (so it's not too big because another
1816 * wiphy dataset is already in the skb) and
1817 * we've not tried to adjust the dump allocation
1818 * yet ... then adjust the alloc size to be
1819 * bigger, and return 1 but with the empty skb.
1820 * This results in an empty message being RX'ed
1821 * in userspace, but that is ignored.
1823 * We can then retry with the larger buffer.
1825 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1826 !skb->len && !state->split &&
1827 cb->min_dump_alloc < 4096) {
1828 cb->min_dump_alloc = 4096;
1829 state->split_start = 0;
1830 rtnl_unlock();
1831 return 1;
1833 idx--;
1834 break;
1836 } while (state->split_start > 0);
1837 break;
1839 rtnl_unlock();
1841 state->start = idx;
1843 return skb->len;
1846 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1848 kfree((void *)cb->args[0]);
1849 return 0;
1852 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1854 struct sk_buff *msg;
1855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1856 struct nl80211_dump_wiphy_state state = {};
1858 msg = nlmsg_new(4096, GFP_KERNEL);
1859 if (!msg)
1860 return -ENOMEM;
1862 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
1863 info->snd_portid, info->snd_seq, 0,
1864 &state) < 0) {
1865 nlmsg_free(msg);
1866 return -ENOBUFS;
1869 return genlmsg_reply(msg, info);
1872 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1873 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1874 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1875 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1876 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1877 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1880 static int parse_txq_params(struct nlattr *tb[],
1881 struct ieee80211_txq_params *txq_params)
1883 u8 ac;
1885 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1886 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1887 !tb[NL80211_TXQ_ATTR_AIFS])
1888 return -EINVAL;
1890 ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
1891 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1892 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1893 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1894 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1896 if (ac >= NL80211_NUM_ACS)
1897 return -EINVAL;
1898 txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
1899 return 0;
1902 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1905 * You can only set the channel explicitly for WDS interfaces,
1906 * all others have their channel managed via their respective
1907 * "establish a connection" command (connect, join, ...)
1909 * For AP/GO and mesh mode, the channel can be set with the
1910 * channel userspace API, but is only stored and passed to the
1911 * low-level driver when the AP starts or the mesh is joined.
1912 * This is for backward compatibility, userspace can also give
1913 * the channel in the start-ap or join-mesh commands instead.
1915 * Monitors are special as they are normally slaved to
1916 * whatever else is going on, so they have their own special
1917 * operation to set the monitor channel if possible.
1919 return !wdev ||
1920 wdev->iftype == NL80211_IFTYPE_AP ||
1921 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1922 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1923 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1926 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1927 struct genl_info *info,
1928 struct cfg80211_chan_def *chandef)
1930 u32 control_freq;
1932 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1933 return -EINVAL;
1935 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1937 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
1938 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1939 chandef->center_freq1 = control_freq;
1940 chandef->center_freq2 = 0;
1942 /* Primary channel not allowed */
1943 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1944 return -EINVAL;
1946 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1947 enum nl80211_channel_type chantype;
1949 chantype = nla_get_u32(
1950 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1952 switch (chantype) {
1953 case NL80211_CHAN_NO_HT:
1954 case NL80211_CHAN_HT20:
1955 case NL80211_CHAN_HT40PLUS:
1956 case NL80211_CHAN_HT40MINUS:
1957 cfg80211_chandef_create(chandef, chandef->chan,
1958 chantype);
1959 break;
1960 default:
1961 return -EINVAL;
1963 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1964 chandef->width =
1965 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1966 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1967 chandef->center_freq1 =
1968 nla_get_u32(
1969 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1970 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1971 chandef->center_freq2 =
1972 nla_get_u32(
1973 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1976 if (!cfg80211_chandef_valid(chandef))
1977 return -EINVAL;
1979 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1980 IEEE80211_CHAN_DISABLED))
1981 return -EINVAL;
1983 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1984 chandef->width == NL80211_CHAN_WIDTH_10) &&
1985 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1986 return -EINVAL;
1988 return 0;
1991 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1992 struct net_device *dev,
1993 struct genl_info *info)
1995 struct cfg80211_chan_def chandef;
1996 int result;
1997 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1998 struct wireless_dev *wdev = NULL;
2000 if (dev)
2001 wdev = dev->ieee80211_ptr;
2002 if (!nl80211_can_set_dev_channel(wdev))
2003 return -EOPNOTSUPP;
2004 if (wdev)
2005 iftype = wdev->iftype;
2007 result = nl80211_parse_chandef(rdev, info, &chandef);
2008 if (result)
2009 return result;
2011 switch (iftype) {
2012 case NL80211_IFTYPE_AP:
2013 case NL80211_IFTYPE_P2P_GO:
2014 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2015 iftype)) {
2016 result = -EINVAL;
2017 break;
2019 if (wdev->beacon_interval) {
2020 if (!dev || !rdev->ops->set_ap_chanwidth ||
2021 !(rdev->wiphy.features &
2022 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2023 result = -EBUSY;
2024 break;
2027 /* Only allow dynamic channel width changes */
2028 if (chandef.chan != wdev->preset_chandef.chan) {
2029 result = -EBUSY;
2030 break;
2032 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2033 if (result)
2034 break;
2036 wdev->preset_chandef = chandef;
2037 result = 0;
2038 break;
2039 case NL80211_IFTYPE_MESH_POINT:
2040 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2041 break;
2042 case NL80211_IFTYPE_MONITOR:
2043 result = cfg80211_set_monitor_channel(rdev, &chandef);
2044 break;
2045 default:
2046 result = -EINVAL;
2049 return result;
2052 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2054 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2055 struct net_device *netdev = info->user_ptr[1];
2057 return __nl80211_set_channel(rdev, netdev, info);
2060 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2062 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2063 struct net_device *dev = info->user_ptr[1];
2064 struct wireless_dev *wdev = dev->ieee80211_ptr;
2065 const u8 *bssid;
2067 if (!info->attrs[NL80211_ATTR_MAC])
2068 return -EINVAL;
2070 if (netif_running(dev))
2071 return -EBUSY;
2073 if (!rdev->ops->set_wds_peer)
2074 return -EOPNOTSUPP;
2076 if (wdev->iftype != NL80211_IFTYPE_WDS)
2077 return -EOPNOTSUPP;
2079 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2080 return rdev_set_wds_peer(rdev, dev, bssid);
2084 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2086 struct cfg80211_registered_device *rdev;
2087 struct net_device *netdev = NULL;
2088 struct wireless_dev *wdev;
2089 int result = 0, rem_txq_params = 0;
2090 struct nlattr *nl_txq_params;
2091 u32 changed;
2092 u8 retry_short = 0, retry_long = 0;
2093 u32 frag_threshold = 0, rts_threshold = 0;
2094 u8 coverage_class = 0;
2096 ASSERT_RTNL();
2099 * Try to find the wiphy and netdev. Normally this
2100 * function shouldn't need the netdev, but this is
2101 * done for backward compatibility -- previously
2102 * setting the channel was done per wiphy, but now
2103 * it is per netdev. Previous userland like hostapd
2104 * also passed a netdev to set_wiphy, so that it is
2105 * possible to let that go to the right netdev!
2108 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2109 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2111 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2112 if (netdev && netdev->ieee80211_ptr)
2113 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2114 else
2115 netdev = NULL;
2118 if (!netdev) {
2119 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2120 info->attrs);
2121 if (IS_ERR(rdev))
2122 return PTR_ERR(rdev);
2123 wdev = NULL;
2124 netdev = NULL;
2125 result = 0;
2126 } else
2127 wdev = netdev->ieee80211_ptr;
2130 * end workaround code, by now the rdev is available
2131 * and locked, and wdev may or may not be NULL.
2134 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2135 result = cfg80211_dev_rename(
2136 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2138 if (result)
2139 return result;
2141 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2142 struct ieee80211_txq_params txq_params;
2143 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2145 if (!rdev->ops->set_txq_params)
2146 return -EOPNOTSUPP;
2148 if (!netdev)
2149 return -EINVAL;
2151 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2152 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2153 return -EINVAL;
2155 if (!netif_running(netdev))
2156 return -ENETDOWN;
2158 nla_for_each_nested(nl_txq_params,
2159 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2160 rem_txq_params) {
2161 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2162 nla_data(nl_txq_params),
2163 nla_len(nl_txq_params),
2164 txq_params_policy);
2165 if (result)
2166 return result;
2167 result = parse_txq_params(tb, &txq_params);
2168 if (result)
2169 return result;
2171 result = rdev_set_txq_params(rdev, netdev,
2172 &txq_params);
2173 if (result)
2174 return result;
2178 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2179 result = __nl80211_set_channel(
2180 rdev,
2181 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2182 info);
2183 if (result)
2184 return result;
2187 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2188 struct wireless_dev *txp_wdev = wdev;
2189 enum nl80211_tx_power_setting type;
2190 int idx, mbm = 0;
2192 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2193 txp_wdev = NULL;
2195 if (!rdev->ops->set_tx_power)
2196 return -EOPNOTSUPP;
2198 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2199 type = nla_get_u32(info->attrs[idx]);
2201 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2202 (type != NL80211_TX_POWER_AUTOMATIC))
2203 return -EINVAL;
2205 if (type != NL80211_TX_POWER_AUTOMATIC) {
2206 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2207 mbm = nla_get_u32(info->attrs[idx]);
2210 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2211 if (result)
2212 return result;
2215 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2216 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2217 u32 tx_ant, rx_ant;
2218 if ((!rdev->wiphy.available_antennas_tx &&
2219 !rdev->wiphy.available_antennas_rx) ||
2220 !rdev->ops->set_antenna)
2221 return -EOPNOTSUPP;
2223 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2224 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2226 /* reject antenna configurations which don't match the
2227 * available antenna masks, except for the "all" mask */
2228 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2229 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2230 return -EINVAL;
2232 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2233 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2235 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2236 if (result)
2237 return result;
2240 changed = 0;
2242 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2243 retry_short = nla_get_u8(
2244 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2245 if (retry_short == 0)
2246 return -EINVAL;
2248 changed |= WIPHY_PARAM_RETRY_SHORT;
2251 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2252 retry_long = nla_get_u8(
2253 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2254 if (retry_long == 0)
2255 return -EINVAL;
2257 changed |= WIPHY_PARAM_RETRY_LONG;
2260 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2261 frag_threshold = nla_get_u32(
2262 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2263 if (frag_threshold < 256)
2264 return -EINVAL;
2266 if (frag_threshold != (u32) -1) {
2268 * Fragments (apart from the last one) are required to
2269 * have even length. Make the fragmentation code
2270 * simpler by stripping LSB should someone try to use
2271 * odd threshold value.
2273 frag_threshold &= ~0x1;
2275 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2278 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2279 rts_threshold = nla_get_u32(
2280 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2281 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2284 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2285 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2286 return -EINVAL;
2288 coverage_class = nla_get_u8(
2289 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2290 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2293 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2294 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2295 return -EOPNOTSUPP;
2297 changed |= WIPHY_PARAM_DYN_ACK;
2300 if (changed) {
2301 u8 old_retry_short, old_retry_long;
2302 u32 old_frag_threshold, old_rts_threshold;
2303 u8 old_coverage_class;
2305 if (!rdev->ops->set_wiphy_params)
2306 return -EOPNOTSUPP;
2308 old_retry_short = rdev->wiphy.retry_short;
2309 old_retry_long = rdev->wiphy.retry_long;
2310 old_frag_threshold = rdev->wiphy.frag_threshold;
2311 old_rts_threshold = rdev->wiphy.rts_threshold;
2312 old_coverage_class = rdev->wiphy.coverage_class;
2314 if (changed & WIPHY_PARAM_RETRY_SHORT)
2315 rdev->wiphy.retry_short = retry_short;
2316 if (changed & WIPHY_PARAM_RETRY_LONG)
2317 rdev->wiphy.retry_long = retry_long;
2318 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2319 rdev->wiphy.frag_threshold = frag_threshold;
2320 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2321 rdev->wiphy.rts_threshold = rts_threshold;
2322 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2323 rdev->wiphy.coverage_class = coverage_class;
2325 result = rdev_set_wiphy_params(rdev, changed);
2326 if (result) {
2327 rdev->wiphy.retry_short = old_retry_short;
2328 rdev->wiphy.retry_long = old_retry_long;
2329 rdev->wiphy.frag_threshold = old_frag_threshold;
2330 rdev->wiphy.rts_threshold = old_rts_threshold;
2331 rdev->wiphy.coverage_class = old_coverage_class;
2332 return result;
2335 return 0;
2338 static inline u64 wdev_id(struct wireless_dev *wdev)
2340 return (u64)wdev->identifier |
2341 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2344 static int nl80211_send_chandef(struct sk_buff *msg,
2345 const struct cfg80211_chan_def *chandef)
2347 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2348 return -EINVAL;
2350 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2351 chandef->chan->center_freq))
2352 return -ENOBUFS;
2353 switch (chandef->width) {
2354 case NL80211_CHAN_WIDTH_20_NOHT:
2355 case NL80211_CHAN_WIDTH_20:
2356 case NL80211_CHAN_WIDTH_40:
2357 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2358 cfg80211_get_chandef_type(chandef)))
2359 return -ENOBUFS;
2360 break;
2361 default:
2362 break;
2364 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2365 return -ENOBUFS;
2366 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2367 return -ENOBUFS;
2368 if (chandef->center_freq2 &&
2369 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2370 return -ENOBUFS;
2371 return 0;
2374 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2375 struct cfg80211_registered_device *rdev,
2376 struct wireless_dev *wdev, bool removal)
2378 struct net_device *dev = wdev->netdev;
2379 u8 cmd = NL80211_CMD_NEW_INTERFACE;
2380 void *hdr;
2382 if (removal)
2383 cmd = NL80211_CMD_DEL_INTERFACE;
2385 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2386 if (!hdr)
2387 return -1;
2389 if (dev &&
2390 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2391 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2392 goto nla_put_failure;
2394 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2395 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2396 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
2397 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2398 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2399 rdev->devlist_generation ^
2400 (cfg80211_rdev_list_generation << 2)))
2401 goto nla_put_failure;
2403 if (rdev->ops->get_channel) {
2404 int ret;
2405 struct cfg80211_chan_def chandef;
2407 ret = rdev_get_channel(rdev, wdev, &chandef);
2408 if (ret == 0) {
2409 if (nl80211_send_chandef(msg, &chandef))
2410 goto nla_put_failure;
2414 if (rdev->ops->get_tx_power) {
2415 int dbm, ret;
2417 ret = rdev_get_tx_power(rdev, wdev, &dbm);
2418 if (ret == 0 &&
2419 nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2420 DBM_TO_MBM(dbm)))
2421 goto nla_put_failure;
2424 if (wdev->ssid_len) {
2425 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2426 goto nla_put_failure;
2429 genlmsg_end(msg, hdr);
2430 return 0;
2432 nla_put_failure:
2433 genlmsg_cancel(msg, hdr);
2434 return -EMSGSIZE;
2437 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2439 int wp_idx = 0;
2440 int if_idx = 0;
2441 int wp_start = cb->args[0];
2442 int if_start = cb->args[1];
2443 struct cfg80211_registered_device *rdev;
2444 struct wireless_dev *wdev;
2446 rtnl_lock();
2447 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2448 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2449 continue;
2450 if (wp_idx < wp_start) {
2451 wp_idx++;
2452 continue;
2454 if_idx = 0;
2456 list_for_each_entry(wdev, &rdev->wdev_list, list) {
2457 if (if_idx < if_start) {
2458 if_idx++;
2459 continue;
2461 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2462 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2463 rdev, wdev, false) < 0) {
2464 goto out;
2466 if_idx++;
2469 wp_idx++;
2471 out:
2472 rtnl_unlock();
2474 cb->args[0] = wp_idx;
2475 cb->args[1] = if_idx;
2477 return skb->len;
2480 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2482 struct sk_buff *msg;
2483 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2484 struct wireless_dev *wdev = info->user_ptr[1];
2486 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2487 if (!msg)
2488 return -ENOMEM;
2490 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2491 rdev, wdev, false) < 0) {
2492 nlmsg_free(msg);
2493 return -ENOBUFS;
2496 return genlmsg_reply(msg, info);
2499 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2500 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2501 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2502 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2503 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2504 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2505 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2508 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2510 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2511 int flag;
2513 *mntrflags = 0;
2515 if (!nla)
2516 return -EINVAL;
2518 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2519 nla, mntr_flags_policy))
2520 return -EINVAL;
2522 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2523 if (flags[flag])
2524 *mntrflags |= (1<<flag);
2526 return 0;
2529 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2530 struct net_device *netdev, u8 use_4addr,
2531 enum nl80211_iftype iftype)
2533 if (!use_4addr) {
2534 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2535 return -EBUSY;
2536 return 0;
2539 switch (iftype) {
2540 case NL80211_IFTYPE_AP_VLAN:
2541 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2542 return 0;
2543 break;
2544 case NL80211_IFTYPE_STATION:
2545 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2546 return 0;
2547 break;
2548 default:
2549 break;
2552 return -EOPNOTSUPP;
2555 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2558 struct vif_params params;
2559 int err;
2560 enum nl80211_iftype otype, ntype;
2561 struct net_device *dev = info->user_ptr[1];
2562 u32 _flags, *flags = NULL;
2563 bool change = false;
2565 memset(&params, 0, sizeof(params));
2567 otype = ntype = dev->ieee80211_ptr->iftype;
2569 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2570 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2571 if (otype != ntype)
2572 change = true;
2573 if (ntype > NL80211_IFTYPE_MAX)
2574 return -EINVAL;
2577 if (info->attrs[NL80211_ATTR_MESH_ID]) {
2578 struct wireless_dev *wdev = dev->ieee80211_ptr;
2580 if (ntype != NL80211_IFTYPE_MESH_POINT)
2581 return -EINVAL;
2582 if (netif_running(dev))
2583 return -EBUSY;
2585 wdev_lock(wdev);
2586 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2587 IEEE80211_MAX_MESH_ID_LEN);
2588 wdev->mesh_id_up_len =
2589 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2590 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2591 wdev->mesh_id_up_len);
2592 wdev_unlock(wdev);
2595 if (info->attrs[NL80211_ATTR_4ADDR]) {
2596 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2597 change = true;
2598 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2599 if (err)
2600 return err;
2601 } else {
2602 params.use_4addr = -1;
2605 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2606 if (ntype != NL80211_IFTYPE_MONITOR)
2607 return -EINVAL;
2608 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2609 &_flags);
2610 if (err)
2611 return err;
2613 flags = &_flags;
2614 change = true;
2617 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
2618 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2619 return -EOPNOTSUPP;
2621 if (change)
2622 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
2623 else
2624 err = 0;
2626 if (!err && params.use_4addr != -1)
2627 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2629 return err;
2632 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2635 struct vif_params params;
2636 struct wireless_dev *wdev;
2637 struct sk_buff *msg, *event;
2638 int err;
2639 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2640 u32 flags;
2642 /* to avoid failing a new interface creation due to pending removal */
2643 cfg80211_destroy_ifaces(rdev);
2645 memset(&params, 0, sizeof(params));
2647 if (!info->attrs[NL80211_ATTR_IFNAME])
2648 return -EINVAL;
2650 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2651 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2652 if (type > NL80211_IFTYPE_MAX)
2653 return -EINVAL;
2656 if (!rdev->ops->add_virtual_intf ||
2657 !(rdev->wiphy.interface_modes & (1 << type)))
2658 return -EOPNOTSUPP;
2660 if ((type == NL80211_IFTYPE_P2P_DEVICE ||
2661 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2662 info->attrs[NL80211_ATTR_MAC]) {
2663 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2664 ETH_ALEN);
2665 if (!is_valid_ether_addr(params.macaddr))
2666 return -EADDRNOTAVAIL;
2669 if (info->attrs[NL80211_ATTR_4ADDR]) {
2670 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2671 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2672 if (err)
2673 return err;
2676 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2677 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2678 &flags);
2680 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
2681 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2682 return -EOPNOTSUPP;
2684 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2685 if (!msg)
2686 return -ENOMEM;
2688 wdev = rdev_add_virtual_intf(rdev,
2689 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2690 NET_NAME_USER, type, err ? NULL : &flags,
2691 &params);
2692 if (WARN_ON(!wdev)) {
2693 nlmsg_free(msg);
2694 return -EPROTO;
2695 } else if (IS_ERR(wdev)) {
2696 nlmsg_free(msg);
2697 return PTR_ERR(wdev);
2700 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
2701 wdev->owner_nlportid = info->snd_portid;
2703 switch (type) {
2704 case NL80211_IFTYPE_MESH_POINT:
2705 if (!info->attrs[NL80211_ATTR_MESH_ID])
2706 break;
2707 wdev_lock(wdev);
2708 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2709 IEEE80211_MAX_MESH_ID_LEN);
2710 wdev->mesh_id_up_len =
2711 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2712 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2713 wdev->mesh_id_up_len);
2714 wdev_unlock(wdev);
2715 break;
2716 case NL80211_IFTYPE_P2P_DEVICE:
2718 * P2P Device doesn't have a netdev, so doesn't go
2719 * through the netdev notifier and must be added here
2721 mutex_init(&wdev->mtx);
2722 INIT_LIST_HEAD(&wdev->event_list);
2723 spin_lock_init(&wdev->event_lock);
2724 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2725 spin_lock_init(&wdev->mgmt_registrations_lock);
2727 wdev->identifier = ++rdev->wdev_id;
2728 list_add_rcu(&wdev->list, &rdev->wdev_list);
2729 rdev->devlist_generation++;
2730 break;
2731 default:
2732 break;
2735 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2736 rdev, wdev, false) < 0) {
2737 nlmsg_free(msg);
2738 return -ENOBUFS;
2741 event = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2742 if (event) {
2743 if (nl80211_send_iface(event, 0, 0, 0,
2744 rdev, wdev, false) < 0) {
2745 nlmsg_free(event);
2746 goto out;
2749 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2750 event, 0, NL80211_MCGRP_CONFIG,
2751 GFP_KERNEL);
2754 out:
2755 return genlmsg_reply(msg, info);
2758 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2761 struct wireless_dev *wdev = info->user_ptr[1];
2762 struct sk_buff *msg;
2763 int status;
2765 if (!rdev->ops->del_virtual_intf)
2766 return -EOPNOTSUPP;
2768 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2769 if (msg && nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, true) < 0) {
2770 nlmsg_free(msg);
2771 msg = NULL;
2775 * If we remove a wireless device without a netdev then clear
2776 * user_ptr[1] so that nl80211_post_doit won't dereference it
2777 * to check if it needs to do dev_put(). Otherwise it crashes
2778 * since the wdev has been freed, unlike with a netdev where
2779 * we need the dev_put() for the netdev to really be freed.
2781 if (!wdev->netdev)
2782 info->user_ptr[1] = NULL;
2784 status = rdev_del_virtual_intf(rdev, wdev);
2785 if (status >= 0 && msg)
2786 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2787 msg, 0, NL80211_MCGRP_CONFIG,
2788 GFP_KERNEL);
2789 else
2790 nlmsg_free(msg);
2792 return status;
2795 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2797 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2798 struct net_device *dev = info->user_ptr[1];
2799 u16 noack_map;
2801 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2802 return -EINVAL;
2804 if (!rdev->ops->set_noack_map)
2805 return -EOPNOTSUPP;
2807 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2809 return rdev_set_noack_map(rdev, dev, noack_map);
2812 struct get_key_cookie {
2813 struct sk_buff *msg;
2814 int error;
2815 int idx;
2818 static void get_key_callback(void *c, struct key_params *params)
2820 struct nlattr *key;
2821 struct get_key_cookie *cookie = c;
2823 if ((params->key &&
2824 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2825 params->key_len, params->key)) ||
2826 (params->seq &&
2827 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2828 params->seq_len, params->seq)) ||
2829 (params->cipher &&
2830 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2831 params->cipher)))
2832 goto nla_put_failure;
2834 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2835 if (!key)
2836 goto nla_put_failure;
2838 if ((params->key &&
2839 nla_put(cookie->msg, NL80211_KEY_DATA,
2840 params->key_len, params->key)) ||
2841 (params->seq &&
2842 nla_put(cookie->msg, NL80211_KEY_SEQ,
2843 params->seq_len, params->seq)) ||
2844 (params->cipher &&
2845 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2846 params->cipher)))
2847 goto nla_put_failure;
2849 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2850 goto nla_put_failure;
2852 nla_nest_end(cookie->msg, key);
2854 return;
2855 nla_put_failure:
2856 cookie->error = 1;
2859 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2862 int err;
2863 struct net_device *dev = info->user_ptr[1];
2864 u8 key_idx = 0;
2865 const u8 *mac_addr = NULL;
2866 bool pairwise;
2867 struct get_key_cookie cookie = {
2868 .error = 0,
2870 void *hdr;
2871 struct sk_buff *msg;
2873 if (info->attrs[NL80211_ATTR_KEY_IDX])
2874 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2876 if (key_idx > 5)
2877 return -EINVAL;
2879 if (info->attrs[NL80211_ATTR_MAC])
2880 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2882 pairwise = !!mac_addr;
2883 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2884 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2885 if (kt >= NUM_NL80211_KEYTYPES)
2886 return -EINVAL;
2887 if (kt != NL80211_KEYTYPE_GROUP &&
2888 kt != NL80211_KEYTYPE_PAIRWISE)
2889 return -EINVAL;
2890 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2893 if (!rdev->ops->get_key)
2894 return -EOPNOTSUPP;
2896 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2897 return -ENOENT;
2899 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2900 if (!msg)
2901 return -ENOMEM;
2903 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
2904 NL80211_CMD_NEW_KEY);
2905 if (!hdr)
2906 goto nla_put_failure;
2908 cookie.msg = msg;
2909 cookie.idx = key_idx;
2911 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2912 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2913 goto nla_put_failure;
2914 if (mac_addr &&
2915 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2916 goto nla_put_failure;
2918 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2919 get_key_callback);
2921 if (err)
2922 goto free_msg;
2924 if (cookie.error)
2925 goto nla_put_failure;
2927 genlmsg_end(msg, hdr);
2928 return genlmsg_reply(msg, info);
2930 nla_put_failure:
2931 err = -ENOBUFS;
2932 free_msg:
2933 nlmsg_free(msg);
2934 return err;
2937 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2939 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2940 struct key_parse key;
2941 int err;
2942 struct net_device *dev = info->user_ptr[1];
2944 err = nl80211_parse_key(info, &key);
2945 if (err)
2946 return err;
2948 if (key.idx < 0)
2949 return -EINVAL;
2951 /* only support setting default key */
2952 if (!key.def && !key.defmgmt)
2953 return -EINVAL;
2955 wdev_lock(dev->ieee80211_ptr);
2957 if (key.def) {
2958 if (!rdev->ops->set_default_key) {
2959 err = -EOPNOTSUPP;
2960 goto out;
2963 err = nl80211_key_allowed(dev->ieee80211_ptr);
2964 if (err)
2965 goto out;
2967 err = rdev_set_default_key(rdev, dev, key.idx,
2968 key.def_uni, key.def_multi);
2970 if (err)
2971 goto out;
2973 #ifdef CONFIG_CFG80211_WEXT
2974 dev->ieee80211_ptr->wext.default_key = key.idx;
2975 #endif
2976 } else {
2977 if (key.def_uni || !key.def_multi) {
2978 err = -EINVAL;
2979 goto out;
2982 if (!rdev->ops->set_default_mgmt_key) {
2983 err = -EOPNOTSUPP;
2984 goto out;
2987 err = nl80211_key_allowed(dev->ieee80211_ptr);
2988 if (err)
2989 goto out;
2991 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
2992 if (err)
2993 goto out;
2995 #ifdef CONFIG_CFG80211_WEXT
2996 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2997 #endif
3000 out:
3001 wdev_unlock(dev->ieee80211_ptr);
3003 return err;
3006 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009 int err;
3010 struct net_device *dev = info->user_ptr[1];
3011 struct key_parse key;
3012 const u8 *mac_addr = NULL;
3014 err = nl80211_parse_key(info, &key);
3015 if (err)
3016 return err;
3018 if (!key.p.key)
3019 return -EINVAL;
3021 if (info->attrs[NL80211_ATTR_MAC])
3022 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3024 if (key.type == -1) {
3025 if (mac_addr)
3026 key.type = NL80211_KEYTYPE_PAIRWISE;
3027 else
3028 key.type = NL80211_KEYTYPE_GROUP;
3031 /* for now */
3032 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3033 key.type != NL80211_KEYTYPE_GROUP)
3034 return -EINVAL;
3036 if (!rdev->ops->add_key)
3037 return -EOPNOTSUPP;
3039 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3040 key.type == NL80211_KEYTYPE_PAIRWISE,
3041 mac_addr))
3042 return -EINVAL;
3044 wdev_lock(dev->ieee80211_ptr);
3045 err = nl80211_key_allowed(dev->ieee80211_ptr);
3046 if (!err)
3047 err = rdev_add_key(rdev, dev, key.idx,
3048 key.type == NL80211_KEYTYPE_PAIRWISE,
3049 mac_addr, &key.p);
3050 wdev_unlock(dev->ieee80211_ptr);
3052 return err;
3055 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3058 int err;
3059 struct net_device *dev = info->user_ptr[1];
3060 u8 *mac_addr = NULL;
3061 struct key_parse key;
3063 err = nl80211_parse_key(info, &key);
3064 if (err)
3065 return err;
3067 if (info->attrs[NL80211_ATTR_MAC])
3068 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3070 if (key.type == -1) {
3071 if (mac_addr)
3072 key.type = NL80211_KEYTYPE_PAIRWISE;
3073 else
3074 key.type = NL80211_KEYTYPE_GROUP;
3077 /* for now */
3078 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3079 key.type != NL80211_KEYTYPE_GROUP)
3080 return -EINVAL;
3082 if (!rdev->ops->del_key)
3083 return -EOPNOTSUPP;
3085 wdev_lock(dev->ieee80211_ptr);
3086 err = nl80211_key_allowed(dev->ieee80211_ptr);
3088 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3089 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3090 err = -ENOENT;
3092 if (!err)
3093 err = rdev_del_key(rdev, dev, key.idx,
3094 key.type == NL80211_KEYTYPE_PAIRWISE,
3095 mac_addr);
3097 #ifdef CONFIG_CFG80211_WEXT
3098 if (!err) {
3099 if (key.idx == dev->ieee80211_ptr->wext.default_key)
3100 dev->ieee80211_ptr->wext.default_key = -1;
3101 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3102 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3104 #endif
3105 wdev_unlock(dev->ieee80211_ptr);
3107 return err;
3110 /* This function returns an error or the number of nested attributes */
3111 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3113 struct nlattr *attr;
3114 int n_entries = 0, tmp;
3116 nla_for_each_nested(attr, nl_attr, tmp) {
3117 if (nla_len(attr) != ETH_ALEN)
3118 return -EINVAL;
3120 n_entries++;
3123 return n_entries;
3127 * This function parses ACL information and allocates memory for ACL data.
3128 * On successful return, the calling function is responsible to free the
3129 * ACL buffer returned by this function.
3131 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3132 struct genl_info *info)
3134 enum nl80211_acl_policy acl_policy;
3135 struct nlattr *attr;
3136 struct cfg80211_acl_data *acl;
3137 int i = 0, n_entries, tmp;
3139 if (!wiphy->max_acl_mac_addrs)
3140 return ERR_PTR(-EOPNOTSUPP);
3142 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3143 return ERR_PTR(-EINVAL);
3145 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3146 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3147 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3148 return ERR_PTR(-EINVAL);
3150 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3151 return ERR_PTR(-EINVAL);
3153 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3154 if (n_entries < 0)
3155 return ERR_PTR(n_entries);
3157 if (n_entries > wiphy->max_acl_mac_addrs)
3158 return ERR_PTR(-ENOTSUPP);
3160 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3161 GFP_KERNEL);
3162 if (!acl)
3163 return ERR_PTR(-ENOMEM);
3165 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3166 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3167 i++;
3170 acl->n_acl_entries = n_entries;
3171 acl->acl_policy = acl_policy;
3173 return acl;
3176 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3178 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3179 struct net_device *dev = info->user_ptr[1];
3180 struct cfg80211_acl_data *acl;
3181 int err;
3183 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3184 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3185 return -EOPNOTSUPP;
3187 if (!dev->ieee80211_ptr->beacon_interval)
3188 return -EINVAL;
3190 acl = parse_acl_data(&rdev->wiphy, info);
3191 if (IS_ERR(acl))
3192 return PTR_ERR(acl);
3194 err = rdev_set_mac_acl(rdev, dev, acl);
3196 kfree(acl);
3198 return err;
3201 static int nl80211_parse_beacon(struct nlattr *attrs[],
3202 struct cfg80211_beacon_data *bcn)
3204 bool haveinfo = false;
3206 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3207 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3208 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3209 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3210 return -EINVAL;
3212 memset(bcn, 0, sizeof(*bcn));
3214 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3215 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3216 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3217 if (!bcn->head_len)
3218 return -EINVAL;
3219 haveinfo = true;
3222 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3223 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3224 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3225 haveinfo = true;
3228 if (!haveinfo)
3229 return -EINVAL;
3231 if (attrs[NL80211_ATTR_IE]) {
3232 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3233 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3236 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3237 bcn->proberesp_ies =
3238 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3239 bcn->proberesp_ies_len =
3240 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3243 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3244 bcn->assocresp_ies =
3245 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3246 bcn->assocresp_ies_len =
3247 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3250 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3251 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3252 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3255 return 0;
3258 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3259 struct cfg80211_ap_settings *params)
3261 struct wireless_dev *wdev;
3262 bool ret = false;
3264 list_for_each_entry(wdev, &rdev->wdev_list, list) {
3265 if (wdev->iftype != NL80211_IFTYPE_AP &&
3266 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3267 continue;
3269 if (!wdev->preset_chandef.chan)
3270 continue;
3272 params->chandef = wdev->preset_chandef;
3273 ret = true;
3274 break;
3277 return ret;
3280 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3281 enum nl80211_auth_type auth_type,
3282 enum nl80211_commands cmd)
3284 if (auth_type > NL80211_AUTHTYPE_MAX)
3285 return false;
3287 switch (cmd) {
3288 case NL80211_CMD_AUTHENTICATE:
3289 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3290 auth_type == NL80211_AUTHTYPE_SAE)
3291 return false;
3292 return true;
3293 case NL80211_CMD_CONNECT:
3294 case NL80211_CMD_START_AP:
3295 /* SAE not supported yet */
3296 if (auth_type == NL80211_AUTHTYPE_SAE)
3297 return false;
3298 return true;
3299 default:
3300 return false;
3304 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3307 struct net_device *dev = info->user_ptr[1];
3308 struct wireless_dev *wdev = dev->ieee80211_ptr;
3309 struct cfg80211_ap_settings params;
3310 int err;
3312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3314 return -EOPNOTSUPP;
3316 if (!rdev->ops->start_ap)
3317 return -EOPNOTSUPP;
3319 if (wdev->beacon_interval)
3320 return -EALREADY;
3322 memset(&params, 0, sizeof(params));
3324 /* these are required for START_AP */
3325 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3326 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3327 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3328 return -EINVAL;
3330 err = nl80211_parse_beacon(info->attrs, &params.beacon);
3331 if (err)
3332 return err;
3334 params.beacon_interval =
3335 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3336 params.dtim_period =
3337 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3339 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3340 if (err)
3341 return err;
3344 * In theory, some of these attributes should be required here
3345 * but since they were not used when the command was originally
3346 * added, keep them optional for old user space programs to let
3347 * them continue to work with drivers that do not need the
3348 * additional information -- drivers must check!
3350 if (info->attrs[NL80211_ATTR_SSID]) {
3351 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3352 params.ssid_len =
3353 nla_len(info->attrs[NL80211_ATTR_SSID]);
3354 if (params.ssid_len == 0 ||
3355 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3356 return -EINVAL;
3359 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3360 params.hidden_ssid = nla_get_u32(
3361 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3362 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3363 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3364 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3365 return -EINVAL;
3368 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3370 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3371 params.auth_type = nla_get_u32(
3372 info->attrs[NL80211_ATTR_AUTH_TYPE]);
3373 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3374 NL80211_CMD_START_AP))
3375 return -EINVAL;
3376 } else
3377 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3379 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3380 NL80211_MAX_NR_CIPHER_SUITES);
3381 if (err)
3382 return err;
3384 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3385 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3386 return -EOPNOTSUPP;
3387 params.inactivity_timeout = nla_get_u16(
3388 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3391 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3392 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3393 return -EINVAL;
3394 params.p2p_ctwindow =
3395 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3396 if (params.p2p_ctwindow > 127)
3397 return -EINVAL;
3398 if (params.p2p_ctwindow != 0 &&
3399 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3400 return -EINVAL;
3403 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3404 u8 tmp;
3406 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3407 return -EINVAL;
3408 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3409 if (tmp > 1)
3410 return -EINVAL;
3411 params.p2p_opp_ps = tmp;
3412 if (params.p2p_opp_ps != 0 &&
3413 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3414 return -EINVAL;
3417 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3418 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3419 if (err)
3420 return err;
3421 } else if (wdev->preset_chandef.chan) {
3422 params.chandef = wdev->preset_chandef;
3423 } else if (!nl80211_get_ap_channel(rdev, &params))
3424 return -EINVAL;
3426 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
3427 wdev->iftype))
3428 return -EINVAL;
3430 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
3431 params.smps_mode =
3432 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
3433 switch (params.smps_mode) {
3434 case NL80211_SMPS_OFF:
3435 break;
3436 case NL80211_SMPS_STATIC:
3437 if (!(rdev->wiphy.features &
3438 NL80211_FEATURE_STATIC_SMPS))
3439 return -EINVAL;
3440 break;
3441 case NL80211_SMPS_DYNAMIC:
3442 if (!(rdev->wiphy.features &
3443 NL80211_FEATURE_DYNAMIC_SMPS))
3444 return -EINVAL;
3445 break;
3446 default:
3447 return -EINVAL;
3449 } else {
3450 params.smps_mode = NL80211_SMPS_OFF;
3453 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3454 params.acl = parse_acl_data(&rdev->wiphy, info);
3455 if (IS_ERR(params.acl))
3456 return PTR_ERR(params.acl);
3459 wdev_lock(wdev);
3460 err = rdev_start_ap(rdev, dev, &params);
3461 if (!err) {
3462 wdev->preset_chandef = params.chandef;
3463 wdev->beacon_interval = params.beacon_interval;
3464 wdev->chandef = params.chandef;
3465 wdev->ssid_len = params.ssid_len;
3466 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
3468 wdev_unlock(wdev);
3470 kfree(params.acl);
3472 return err;
3475 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3477 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3478 struct net_device *dev = info->user_ptr[1];
3479 struct wireless_dev *wdev = dev->ieee80211_ptr;
3480 struct cfg80211_beacon_data params;
3481 int err;
3483 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3484 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3485 return -EOPNOTSUPP;
3487 if (!rdev->ops->change_beacon)
3488 return -EOPNOTSUPP;
3490 if (!wdev->beacon_interval)
3491 return -EINVAL;
3493 err = nl80211_parse_beacon(info->attrs, &params);
3494 if (err)
3495 return err;
3497 wdev_lock(wdev);
3498 err = rdev_change_beacon(rdev, dev, &params);
3499 wdev_unlock(wdev);
3501 return err;
3504 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
3506 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3507 struct net_device *dev = info->user_ptr[1];
3509 return cfg80211_stop_ap(rdev, dev, false);
3512 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3513 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3514 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3515 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
3516 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
3517 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
3518 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
3521 static int parse_station_flags(struct genl_info *info,
3522 enum nl80211_iftype iftype,
3523 struct station_parameters *params)
3525 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
3526 struct nlattr *nla;
3527 int flag;
3530 * Try parsing the new attribute first so userspace
3531 * can specify both for older kernels.
3533 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3534 if (nla) {
3535 struct nl80211_sta_flag_update *sta_flags;
3537 sta_flags = nla_data(nla);
3538 params->sta_flags_mask = sta_flags->mask;
3539 params->sta_flags_set = sta_flags->set;
3540 params->sta_flags_set &= params->sta_flags_mask;
3541 if ((params->sta_flags_mask |
3542 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3543 return -EINVAL;
3544 return 0;
3547 /* if present, parse the old attribute */
3549 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
3550 if (!nla)
3551 return 0;
3553 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3554 nla, sta_flags_policy))
3555 return -EINVAL;
3558 * Only allow certain flags for interface types so that
3559 * other attributes are silently ignored. Remember that
3560 * this is backward compatibility code with old userspace
3561 * and shouldn't be hit in other cases anyway.
3563 switch (iftype) {
3564 case NL80211_IFTYPE_AP:
3565 case NL80211_IFTYPE_AP_VLAN:
3566 case NL80211_IFTYPE_P2P_GO:
3567 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3568 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3569 BIT(NL80211_STA_FLAG_WME) |
3570 BIT(NL80211_STA_FLAG_MFP);
3571 break;
3572 case NL80211_IFTYPE_P2P_CLIENT:
3573 case NL80211_IFTYPE_STATION:
3574 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3575 BIT(NL80211_STA_FLAG_TDLS_PEER);
3576 break;
3577 case NL80211_IFTYPE_MESH_POINT:
3578 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3579 BIT(NL80211_STA_FLAG_MFP) |
3580 BIT(NL80211_STA_FLAG_AUTHORIZED);
3581 break;
3582 default:
3583 return -EINVAL;
3586 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3587 if (flags[flag]) {
3588 params->sta_flags_set |= (1<<flag);
3590 /* no longer support new API additions in old API */
3591 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3592 return -EINVAL;
3596 return 0;
3599 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3600 int attr)
3602 struct nlattr *rate;
3603 u32 bitrate;
3604 u16 bitrate_compat;
3605 enum nl80211_attrs rate_flg;
3607 rate = nla_nest_start(msg, attr);
3608 if (!rate)
3609 return false;
3611 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3612 bitrate = cfg80211_calculate_bitrate(info);
3613 /* report 16-bit bitrate only if we can */
3614 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
3615 if (bitrate > 0 &&
3616 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3617 return false;
3618 if (bitrate_compat > 0 &&
3619 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3620 return false;
3622 switch (info->bw) {
3623 case RATE_INFO_BW_5:
3624 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
3625 break;
3626 case RATE_INFO_BW_10:
3627 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
3628 break;
3629 default:
3630 WARN_ON(1);
3631 /* fall through */
3632 case RATE_INFO_BW_20:
3633 rate_flg = 0;
3634 break;
3635 case RATE_INFO_BW_40:
3636 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
3637 break;
3638 case RATE_INFO_BW_80:
3639 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
3640 break;
3641 case RATE_INFO_BW_160:
3642 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
3643 break;
3646 if (rate_flg && nla_put_flag(msg, rate_flg))
3647 return false;
3649 if (info->flags & RATE_INFO_FLAGS_MCS) {
3650 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3651 return false;
3652 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3653 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3654 return false;
3655 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3656 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3657 return false;
3658 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3659 return false;
3660 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3661 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3662 return false;
3665 nla_nest_end(msg, rate);
3666 return true;
3669 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3670 int id)
3672 void *attr;
3673 int i = 0;
3675 if (!mask)
3676 return true;
3678 attr = nla_nest_start(msg, id);
3679 if (!attr)
3680 return false;
3682 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3683 if (!(mask & BIT(i)))
3684 continue;
3686 if (nla_put_u8(msg, i, signal[i]))
3687 return false;
3690 nla_nest_end(msg, attr);
3692 return true;
3695 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
3696 u32 seq, int flags,
3697 struct cfg80211_registered_device *rdev,
3698 struct net_device *dev,
3699 const u8 *mac_addr, struct station_info *sinfo)
3701 void *hdr;
3702 struct nlattr *sinfoattr, *bss_param;
3704 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3705 if (!hdr)
3706 return -1;
3708 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3709 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3710 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3711 goto nla_put_failure;
3713 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3714 if (!sinfoattr)
3715 goto nla_put_failure;
3717 #define PUT_SINFO(attr, memb, type) do { \
3718 if (sinfo->filled & BIT(NL80211_STA_INFO_ ## attr) && \
3719 nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr, \
3720 sinfo->memb)) \
3721 goto nla_put_failure; \
3722 } while (0)
3724 PUT_SINFO(CONNECTED_TIME, connected_time, u32);
3725 PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
3727 if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
3728 BIT(NL80211_STA_INFO_RX_BYTES64)) &&
3729 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
3730 (u32)sinfo->rx_bytes))
3731 goto nla_put_failure;
3733 if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
3734 BIT(NL80211_STA_INFO_TX_BYTES64)) &&
3735 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3736 (u32)sinfo->tx_bytes))
3737 goto nla_put_failure;
3739 PUT_SINFO(RX_BYTES64, rx_bytes, u64);
3740 PUT_SINFO(TX_BYTES64, tx_bytes, u64);
3741 PUT_SINFO(LLID, llid, u16);
3742 PUT_SINFO(PLID, plid, u16);
3743 PUT_SINFO(PLINK_STATE, plink_state, u8);
3745 switch (rdev->wiphy.signal_type) {
3746 case CFG80211_SIGNAL_TYPE_MBM:
3747 PUT_SINFO(SIGNAL, signal, u8);
3748 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
3749 break;
3750 default:
3751 break;
3753 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
3754 if (!nl80211_put_signal(msg, sinfo->chains,
3755 sinfo->chain_signal,
3756 NL80211_STA_INFO_CHAIN_SIGNAL))
3757 goto nla_put_failure;
3759 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
3760 if (!nl80211_put_signal(msg, sinfo->chains,
3761 sinfo->chain_signal_avg,
3762 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3763 goto nla_put_failure;
3765 if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
3766 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3767 NL80211_STA_INFO_TX_BITRATE))
3768 goto nla_put_failure;
3770 if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
3771 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3772 NL80211_STA_INFO_RX_BITRATE))
3773 goto nla_put_failure;
3776 PUT_SINFO(RX_PACKETS, rx_packets, u32);
3777 PUT_SINFO(TX_PACKETS, tx_packets, u32);
3778 PUT_SINFO(TX_RETRIES, tx_retries, u32);
3779 PUT_SINFO(TX_FAILED, tx_failed, u32);
3780 PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
3781 PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
3782 PUT_SINFO(LOCAL_PM, local_pm, u32);
3783 PUT_SINFO(PEER_PM, peer_pm, u32);
3784 PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
3786 if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
3787 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3788 if (!bss_param)
3789 goto nla_put_failure;
3791 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3792 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3793 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3794 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3795 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3796 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3797 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3798 sinfo->bss_param.dtim_period) ||
3799 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3800 sinfo->bss_param.beacon_interval))
3801 goto nla_put_failure;
3803 nla_nest_end(msg, bss_param);
3805 if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
3806 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3807 sizeof(struct nl80211_sta_flag_update),
3808 &sinfo->sta_flags))
3809 goto nla_put_failure;
3811 PUT_SINFO(T_OFFSET, t_offset, u64);
3812 PUT_SINFO(RX_DROP_MISC, rx_dropped_misc, u64);
3813 PUT_SINFO(BEACON_RX, rx_beacon, u64);
3814 PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
3816 #undef PUT_SINFO
3818 if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
3819 struct nlattr *tidsattr;
3820 int tid;
3822 tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
3823 if (!tidsattr)
3824 goto nla_put_failure;
3826 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
3827 struct cfg80211_tid_stats *tidstats;
3828 struct nlattr *tidattr;
3830 tidstats = &sinfo->pertid[tid];
3832 if (!tidstats->filled)
3833 continue;
3835 tidattr = nla_nest_start(msg, tid + 1);
3836 if (!tidattr)
3837 goto nla_put_failure;
3839 #define PUT_TIDVAL(attr, memb, type) do { \
3840 if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) && \
3841 nla_put_ ## type(msg, NL80211_TID_STATS_ ## attr, \
3842 tidstats->memb)) \
3843 goto nla_put_failure; \
3844 } while (0)
3846 PUT_TIDVAL(RX_MSDU, rx_msdu, u64);
3847 PUT_TIDVAL(TX_MSDU, tx_msdu, u64);
3848 PUT_TIDVAL(TX_MSDU_RETRIES, tx_msdu_retries, u64);
3849 PUT_TIDVAL(TX_MSDU_FAILED, tx_msdu_failed, u64);
3851 #undef PUT_TIDVAL
3852 nla_nest_end(msg, tidattr);
3855 nla_nest_end(msg, tidsattr);
3858 nla_nest_end(msg, sinfoattr);
3860 if (sinfo->assoc_req_ies_len &&
3861 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3862 sinfo->assoc_req_ies))
3863 goto nla_put_failure;
3865 genlmsg_end(msg, hdr);
3866 return 0;
3868 nla_put_failure:
3869 genlmsg_cancel(msg, hdr);
3870 return -EMSGSIZE;
3873 static int nl80211_dump_station(struct sk_buff *skb,
3874 struct netlink_callback *cb)
3876 struct station_info sinfo;
3877 struct cfg80211_registered_device *rdev;
3878 struct wireless_dev *wdev;
3879 u8 mac_addr[ETH_ALEN];
3880 int sta_idx = cb->args[2];
3881 int err;
3883 rtnl_lock();
3884 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
3885 if (err)
3886 goto out_err;
3888 if (!wdev->netdev) {
3889 err = -EINVAL;
3890 goto out_err;
3893 if (!rdev->ops->dump_station) {
3894 err = -EOPNOTSUPP;
3895 goto out_err;
3898 while (1) {
3899 memset(&sinfo, 0, sizeof(sinfo));
3900 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
3901 mac_addr, &sinfo);
3902 if (err == -ENOENT)
3903 break;
3904 if (err)
3905 goto out_err;
3907 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
3908 NETLINK_CB(cb->skb).portid,
3909 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3910 rdev, wdev->netdev, mac_addr,
3911 &sinfo) < 0)
3912 goto out;
3914 sta_idx++;
3918 out:
3919 cb->args[2] = sta_idx;
3920 err = skb->len;
3921 out_err:
3922 rtnl_unlock();
3924 return err;
3927 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3930 struct net_device *dev = info->user_ptr[1];
3931 struct station_info sinfo;
3932 struct sk_buff *msg;
3933 u8 *mac_addr = NULL;
3934 int err;
3936 memset(&sinfo, 0, sizeof(sinfo));
3938 if (!info->attrs[NL80211_ATTR_MAC])
3939 return -EINVAL;
3941 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3943 if (!rdev->ops->get_station)
3944 return -EOPNOTSUPP;
3946 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
3947 if (err)
3948 return err;
3950 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3951 if (!msg)
3952 return -ENOMEM;
3954 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
3955 info->snd_portid, info->snd_seq, 0,
3956 rdev, dev, mac_addr, &sinfo) < 0) {
3957 nlmsg_free(msg);
3958 return -ENOBUFS;
3961 return genlmsg_reply(msg, info);
3964 int cfg80211_check_station_change(struct wiphy *wiphy,
3965 struct station_parameters *params,
3966 enum cfg80211_station_type statype)
3968 if (params->listen_interval != -1 &&
3969 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
3970 return -EINVAL;
3972 if (params->aid &&
3973 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
3974 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
3975 return -EINVAL;
3977 /* When you run into this, adjust the code below for the new flag */
3978 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3980 switch (statype) {
3981 case CFG80211_STA_MESH_PEER_KERNEL:
3982 case CFG80211_STA_MESH_PEER_USER:
3984 * No ignoring the TDLS flag here -- the userspace mesh
3985 * code doesn't have the bug of including TDLS in the
3986 * mask everywhere.
3988 if (params->sta_flags_mask &
3989 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3990 BIT(NL80211_STA_FLAG_MFP) |
3991 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3992 return -EINVAL;
3993 break;
3994 case CFG80211_STA_TDLS_PEER_SETUP:
3995 case CFG80211_STA_TDLS_PEER_ACTIVE:
3996 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3997 return -EINVAL;
3998 /* ignore since it can't change */
3999 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4000 break;
4001 default:
4002 /* disallow mesh-specific things */
4003 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4004 return -EINVAL;
4005 if (params->local_pm)
4006 return -EINVAL;
4007 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4008 return -EINVAL;
4011 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4012 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4013 /* TDLS can't be set, ... */
4014 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4015 return -EINVAL;
4017 * ... but don't bother the driver with it. This works around
4018 * a hostapd/wpa_supplicant issue -- it always includes the
4019 * TLDS_PEER flag in the mask even for AP mode.
4021 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4024 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4025 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4026 /* reject other things that can't change */
4027 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4028 return -EINVAL;
4029 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4030 return -EINVAL;
4031 if (params->supported_rates)
4032 return -EINVAL;
4033 if (params->ext_capab || params->ht_capa || params->vht_capa)
4034 return -EINVAL;
4037 if (statype != CFG80211_STA_AP_CLIENT &&
4038 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4039 if (params->vlan)
4040 return -EINVAL;
4043 switch (statype) {
4044 case CFG80211_STA_AP_MLME_CLIENT:
4045 /* Use this only for authorizing/unauthorizing a station */
4046 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4047 return -EOPNOTSUPP;
4048 break;
4049 case CFG80211_STA_AP_CLIENT:
4050 case CFG80211_STA_AP_CLIENT_UNASSOC:
4051 /* accept only the listed bits */
4052 if (params->sta_flags_mask &
4053 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4054 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4055 BIT(NL80211_STA_FLAG_ASSOCIATED) |
4056 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4057 BIT(NL80211_STA_FLAG_WME) |
4058 BIT(NL80211_STA_FLAG_MFP)))
4059 return -EINVAL;
4061 /* but authenticated/associated only if driver handles it */
4062 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4063 params->sta_flags_mask &
4064 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4065 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4066 return -EINVAL;
4067 break;
4068 case CFG80211_STA_IBSS:
4069 case CFG80211_STA_AP_STA:
4070 /* reject any changes other than AUTHORIZED */
4071 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4072 return -EINVAL;
4073 break;
4074 case CFG80211_STA_TDLS_PEER_SETUP:
4075 /* reject any changes other than AUTHORIZED or WME */
4076 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4077 BIT(NL80211_STA_FLAG_WME)))
4078 return -EINVAL;
4079 /* force (at least) rates when authorizing */
4080 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4081 !params->supported_rates)
4082 return -EINVAL;
4083 break;
4084 case CFG80211_STA_TDLS_PEER_ACTIVE:
4085 /* reject any changes */
4086 return -EINVAL;
4087 case CFG80211_STA_MESH_PEER_KERNEL:
4088 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4089 return -EINVAL;
4090 break;
4091 case CFG80211_STA_MESH_PEER_USER:
4092 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4093 params->plink_action != NL80211_PLINK_ACTION_BLOCK)
4094 return -EINVAL;
4095 break;
4098 return 0;
4100 EXPORT_SYMBOL(cfg80211_check_station_change);
4103 * Get vlan interface making sure it is running and on the right wiphy.
4105 static struct net_device *get_vlan(struct genl_info *info,
4106 struct cfg80211_registered_device *rdev)
4108 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
4109 struct net_device *v;
4110 int ret;
4112 if (!vlanattr)
4113 return NULL;
4115 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4116 if (!v)
4117 return ERR_PTR(-ENODEV);
4119 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4120 ret = -EINVAL;
4121 goto error;
4124 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4125 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4126 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4127 ret = -EINVAL;
4128 goto error;
4131 if (!netif_running(v)) {
4132 ret = -ENETDOWN;
4133 goto error;
4136 return v;
4137 error:
4138 dev_put(v);
4139 return ERR_PTR(ret);
4142 static const struct nla_policy
4143 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
4144 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4145 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4148 static int nl80211_parse_sta_wme(struct genl_info *info,
4149 struct station_parameters *params)
4151 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4152 struct nlattr *nla;
4153 int err;
4155 /* parse WME attributes if present */
4156 if (!info->attrs[NL80211_ATTR_STA_WME])
4157 return 0;
4159 nla = info->attrs[NL80211_ATTR_STA_WME];
4160 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4161 nl80211_sta_wme_policy);
4162 if (err)
4163 return err;
4165 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4166 params->uapsd_queues = nla_get_u8(
4167 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4168 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4169 return -EINVAL;
4171 if (tb[NL80211_STA_WME_MAX_SP])
4172 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4174 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4175 return -EINVAL;
4177 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4179 return 0;
4182 static int nl80211_parse_sta_channel_info(struct genl_info *info,
4183 struct station_parameters *params)
4185 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4186 params->supported_channels =
4187 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4188 params->supported_channels_len =
4189 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4191 * Need to include at least one (first channel, number of
4192 * channels) tuple for each subband, and must have proper
4193 * tuples for the rest of the data as well.
4195 if (params->supported_channels_len < 2)
4196 return -EINVAL;
4197 if (params->supported_channels_len % 2)
4198 return -EINVAL;
4201 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4202 params->supported_oper_classes =
4203 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4204 params->supported_oper_classes_len =
4205 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4207 * The value of the Length field of the Supported Operating
4208 * Classes element is between 2 and 253.
4210 if (params->supported_oper_classes_len < 2 ||
4211 params->supported_oper_classes_len > 253)
4212 return -EINVAL;
4214 return 0;
4217 static int nl80211_set_station_tdls(struct genl_info *info,
4218 struct station_parameters *params)
4220 int err;
4221 /* Dummy STA entry gets updated once the peer capabilities are known */
4222 if (info->attrs[NL80211_ATTR_PEER_AID])
4223 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4224 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4225 params->ht_capa =
4226 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4227 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4228 params->vht_capa =
4229 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4231 err = nl80211_parse_sta_channel_info(info, params);
4232 if (err)
4233 return err;
4235 return nl80211_parse_sta_wme(info, params);
4238 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4240 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4241 struct net_device *dev = info->user_ptr[1];
4242 struct station_parameters params;
4243 u8 *mac_addr;
4244 int err;
4246 memset(&params, 0, sizeof(params));
4248 if (!rdev->ops->change_station)
4249 return -EOPNOTSUPP;
4252 * AID and listen_interval properties can be set only for unassociated
4253 * station. Include these parameters here and will check them in
4254 * cfg80211_check_station_change().
4256 if (info->attrs[NL80211_ATTR_PEER_AID])
4257 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4259 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4260 params.listen_interval =
4261 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4262 else
4263 params.listen_interval = -1;
4265 if (!info->attrs[NL80211_ATTR_MAC])
4266 return -EINVAL;
4268 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4270 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4271 params.supported_rates =
4272 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4273 params.supported_rates_len =
4274 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4277 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4278 params.capability =
4279 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4280 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4283 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4284 params.ext_capab =
4285 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4286 params.ext_capab_len =
4287 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4290 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4291 return -EINVAL;
4293 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4294 params.plink_action =
4295 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4296 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4297 return -EINVAL;
4300 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
4301 params.plink_state =
4302 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4303 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4304 return -EINVAL;
4305 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4308 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4309 enum nl80211_mesh_power_mode pm = nla_get_u32(
4310 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4312 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4313 pm > NL80211_MESH_POWER_MAX)
4314 return -EINVAL;
4316 params.local_pm = pm;
4319 /* Include parameters for TDLS peer (will check later) */
4320 err = nl80211_set_station_tdls(info, &params);
4321 if (err)
4322 return err;
4324 params.vlan = get_vlan(info, rdev);
4325 if (IS_ERR(params.vlan))
4326 return PTR_ERR(params.vlan);
4328 switch (dev->ieee80211_ptr->iftype) {
4329 case NL80211_IFTYPE_AP:
4330 case NL80211_IFTYPE_AP_VLAN:
4331 case NL80211_IFTYPE_P2P_GO:
4332 case NL80211_IFTYPE_P2P_CLIENT:
4333 case NL80211_IFTYPE_STATION:
4334 case NL80211_IFTYPE_ADHOC:
4335 case NL80211_IFTYPE_MESH_POINT:
4336 break;
4337 default:
4338 err = -EOPNOTSUPP;
4339 goto out_put_vlan;
4342 /* driver will call cfg80211_check_station_change() */
4343 err = rdev_change_station(rdev, dev, mac_addr, &params);
4345 out_put_vlan:
4346 if (params.vlan)
4347 dev_put(params.vlan);
4349 return err;
4352 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4355 int err;
4356 struct net_device *dev = info->user_ptr[1];
4357 struct station_parameters params;
4358 u8 *mac_addr = NULL;
4360 memset(&params, 0, sizeof(params));
4362 if (!rdev->ops->add_station)
4363 return -EOPNOTSUPP;
4365 if (!info->attrs[NL80211_ATTR_MAC])
4366 return -EINVAL;
4368 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4369 return -EINVAL;
4371 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4372 return -EINVAL;
4374 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4375 !info->attrs[NL80211_ATTR_PEER_AID])
4376 return -EINVAL;
4378 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4379 params.supported_rates =
4380 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4381 params.supported_rates_len =
4382 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4383 params.listen_interval =
4384 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4386 if (info->attrs[NL80211_ATTR_PEER_AID])
4387 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4388 else
4389 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4390 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4391 return -EINVAL;
4393 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4394 params.capability =
4395 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4396 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4399 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4400 params.ext_capab =
4401 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4402 params.ext_capab_len =
4403 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4406 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4407 params.ht_capa =
4408 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4410 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4411 params.vht_capa =
4412 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4414 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4415 params.opmode_notif_used = true;
4416 params.opmode_notif =
4417 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4420 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4421 params.plink_action =
4422 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4423 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4424 return -EINVAL;
4427 err = nl80211_parse_sta_channel_info(info, &params);
4428 if (err)
4429 return err;
4431 err = nl80211_parse_sta_wme(info, &params);
4432 if (err)
4433 return err;
4435 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4436 return -EINVAL;
4438 /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
4439 * as userspace might just pass through the capabilities from the IEs
4440 * directly, rather than enforcing this restriction and returning an
4441 * error in this case.
4443 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
4444 params.ht_capa = NULL;
4445 params.vht_capa = NULL;
4448 /* When you run into this, adjust the code below for the new flag */
4449 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4451 switch (dev->ieee80211_ptr->iftype) {
4452 case NL80211_IFTYPE_AP:
4453 case NL80211_IFTYPE_AP_VLAN:
4454 case NL80211_IFTYPE_P2P_GO:
4455 /* ignore WME attributes if iface/sta is not capable */
4456 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4457 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4458 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4460 /* TDLS peers cannot be added */
4461 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4462 info->attrs[NL80211_ATTR_PEER_AID])
4463 return -EINVAL;
4464 /* but don't bother the driver with it */
4465 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4467 /* allow authenticated/associated only if driver handles it */
4468 if (!(rdev->wiphy.features &
4469 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4470 params.sta_flags_mask &
4471 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4472 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4473 return -EINVAL;
4475 /* must be last in here for error handling */
4476 params.vlan = get_vlan(info, rdev);
4477 if (IS_ERR(params.vlan))
4478 return PTR_ERR(params.vlan);
4479 break;
4480 case NL80211_IFTYPE_MESH_POINT:
4481 /* ignore uAPSD data */
4482 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4484 /* associated is disallowed */
4485 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4486 return -EINVAL;
4487 /* TDLS peers cannot be added */
4488 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4489 info->attrs[NL80211_ATTR_PEER_AID])
4490 return -EINVAL;
4491 break;
4492 case NL80211_IFTYPE_STATION:
4493 case NL80211_IFTYPE_P2P_CLIENT:
4494 /* ignore uAPSD data */
4495 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4497 /* these are disallowed */
4498 if (params.sta_flags_mask &
4499 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4500 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
4501 return -EINVAL;
4502 /* Only TDLS peers can be added */
4503 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4504 return -EINVAL;
4505 /* Can only add if TDLS ... */
4506 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4507 return -EOPNOTSUPP;
4508 /* ... with external setup is supported */
4509 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4510 return -EOPNOTSUPP;
4512 * Older wpa_supplicant versions always mark the TDLS peer
4513 * as authorized, but it shouldn't yet be.
4515 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
4516 break;
4517 default:
4518 return -EOPNOTSUPP;
4521 /* be aware of params.vlan when changing code here */
4523 err = rdev_add_station(rdev, dev, mac_addr, &params);
4525 if (params.vlan)
4526 dev_put(params.vlan);
4527 return err;
4530 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4533 struct net_device *dev = info->user_ptr[1];
4534 struct station_del_parameters params;
4536 memset(&params, 0, sizeof(params));
4538 if (info->attrs[NL80211_ATTR_MAC])
4539 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
4541 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4542 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4543 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4544 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4545 return -EINVAL;
4547 if (!rdev->ops->del_station)
4548 return -EOPNOTSUPP;
4550 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
4551 params.subtype =
4552 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
4553 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
4554 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
4555 return -EINVAL;
4556 } else {
4557 /* Default to Deauthentication frame */
4558 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
4561 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
4562 params.reason_code =
4563 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4564 if (params.reason_code == 0)
4565 return -EINVAL; /* 0 is reserved */
4566 } else {
4567 /* Default to reason code 2 */
4568 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
4571 return rdev_del_station(rdev, dev, &params);
4574 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
4575 int flags, struct net_device *dev,
4576 u8 *dst, u8 *next_hop,
4577 struct mpath_info *pinfo)
4579 void *hdr;
4580 struct nlattr *pinfoattr;
4582 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
4583 if (!hdr)
4584 return -1;
4586 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4587 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4588 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4589 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4590 goto nla_put_failure;
4592 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4593 if (!pinfoattr)
4594 goto nla_put_failure;
4595 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4596 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4597 pinfo->frame_qlen))
4598 goto nla_put_failure;
4599 if (((pinfo->filled & MPATH_INFO_SN) &&
4600 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4601 ((pinfo->filled & MPATH_INFO_METRIC) &&
4602 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4603 pinfo->metric)) ||
4604 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4605 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4606 pinfo->exptime)) ||
4607 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4608 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4609 pinfo->flags)) ||
4610 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4611 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4612 pinfo->discovery_timeout)) ||
4613 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4614 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4615 pinfo->discovery_retries)))
4616 goto nla_put_failure;
4618 nla_nest_end(msg, pinfoattr);
4620 genlmsg_end(msg, hdr);
4621 return 0;
4623 nla_put_failure:
4624 genlmsg_cancel(msg, hdr);
4625 return -EMSGSIZE;
4628 static int nl80211_dump_mpath(struct sk_buff *skb,
4629 struct netlink_callback *cb)
4631 struct mpath_info pinfo;
4632 struct cfg80211_registered_device *rdev;
4633 struct wireless_dev *wdev;
4634 u8 dst[ETH_ALEN];
4635 u8 next_hop[ETH_ALEN];
4636 int path_idx = cb->args[2];
4637 int err;
4639 rtnl_lock();
4640 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4641 if (err)
4642 goto out_err;
4644 if (!rdev->ops->dump_mpath) {
4645 err = -EOPNOTSUPP;
4646 goto out_err;
4649 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4650 err = -EOPNOTSUPP;
4651 goto out_err;
4654 while (1) {
4655 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
4656 next_hop, &pinfo);
4657 if (err == -ENOENT)
4658 break;
4659 if (err)
4660 goto out_err;
4662 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4663 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4664 wdev->netdev, dst, next_hop,
4665 &pinfo) < 0)
4666 goto out;
4668 path_idx++;
4672 out:
4673 cb->args[2] = path_idx;
4674 err = skb->len;
4675 out_err:
4676 rtnl_unlock();
4677 return err;
4680 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4683 int err;
4684 struct net_device *dev = info->user_ptr[1];
4685 struct mpath_info pinfo;
4686 struct sk_buff *msg;
4687 u8 *dst = NULL;
4688 u8 next_hop[ETH_ALEN];
4690 memset(&pinfo, 0, sizeof(pinfo));
4692 if (!info->attrs[NL80211_ATTR_MAC])
4693 return -EINVAL;
4695 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4697 if (!rdev->ops->get_mpath)
4698 return -EOPNOTSUPP;
4700 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4701 return -EOPNOTSUPP;
4703 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
4704 if (err)
4705 return err;
4707 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4708 if (!msg)
4709 return -ENOMEM;
4711 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4712 dev, dst, next_hop, &pinfo) < 0) {
4713 nlmsg_free(msg);
4714 return -ENOBUFS;
4717 return genlmsg_reply(msg, info);
4720 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4722 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4723 struct net_device *dev = info->user_ptr[1];
4724 u8 *dst = NULL;
4725 u8 *next_hop = NULL;
4727 if (!info->attrs[NL80211_ATTR_MAC])
4728 return -EINVAL;
4730 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4731 return -EINVAL;
4733 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4734 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4736 if (!rdev->ops->change_mpath)
4737 return -EOPNOTSUPP;
4739 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4740 return -EOPNOTSUPP;
4742 return rdev_change_mpath(rdev, dev, dst, next_hop);
4745 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4747 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4748 struct net_device *dev = info->user_ptr[1];
4749 u8 *dst = NULL;
4750 u8 *next_hop = NULL;
4752 if (!info->attrs[NL80211_ATTR_MAC])
4753 return -EINVAL;
4755 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4756 return -EINVAL;
4758 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4759 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4761 if (!rdev->ops->add_mpath)
4762 return -EOPNOTSUPP;
4764 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4765 return -EOPNOTSUPP;
4767 return rdev_add_mpath(rdev, dev, dst, next_hop);
4770 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4772 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4773 struct net_device *dev = info->user_ptr[1];
4774 u8 *dst = NULL;
4776 if (info->attrs[NL80211_ATTR_MAC])
4777 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4779 if (!rdev->ops->del_mpath)
4780 return -EOPNOTSUPP;
4782 return rdev_del_mpath(rdev, dev, dst);
4785 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
4787 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4788 int err;
4789 struct net_device *dev = info->user_ptr[1];
4790 struct mpath_info pinfo;
4791 struct sk_buff *msg;
4792 u8 *dst = NULL;
4793 u8 mpp[ETH_ALEN];
4795 memset(&pinfo, 0, sizeof(pinfo));
4797 if (!info->attrs[NL80211_ATTR_MAC])
4798 return -EINVAL;
4800 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4802 if (!rdev->ops->get_mpp)
4803 return -EOPNOTSUPP;
4805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4806 return -EOPNOTSUPP;
4808 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
4809 if (err)
4810 return err;
4812 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4813 if (!msg)
4814 return -ENOMEM;
4816 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4817 dev, dst, mpp, &pinfo) < 0) {
4818 nlmsg_free(msg);
4819 return -ENOBUFS;
4822 return genlmsg_reply(msg, info);
4825 static int nl80211_dump_mpp(struct sk_buff *skb,
4826 struct netlink_callback *cb)
4828 struct mpath_info pinfo;
4829 struct cfg80211_registered_device *rdev;
4830 struct wireless_dev *wdev;
4831 u8 dst[ETH_ALEN];
4832 u8 mpp[ETH_ALEN];
4833 int path_idx = cb->args[2];
4834 int err;
4836 rtnl_lock();
4837 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4838 if (err)
4839 goto out_err;
4841 if (!rdev->ops->dump_mpp) {
4842 err = -EOPNOTSUPP;
4843 goto out_err;
4846 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4847 err = -EOPNOTSUPP;
4848 goto out_err;
4851 while (1) {
4852 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
4853 mpp, &pinfo);
4854 if (err == -ENOENT)
4855 break;
4856 if (err)
4857 goto out_err;
4859 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4860 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4861 wdev->netdev, dst, mpp,
4862 &pinfo) < 0)
4863 goto out;
4865 path_idx++;
4868 out:
4869 cb->args[2] = path_idx;
4870 err = skb->len;
4871 out_err:
4872 rtnl_unlock();
4873 return err;
4876 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4878 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4879 struct net_device *dev = info->user_ptr[1];
4880 struct wireless_dev *wdev = dev->ieee80211_ptr;
4881 struct bss_parameters params;
4882 int err;
4884 memset(&params, 0, sizeof(params));
4885 /* default to not changing parameters */
4886 params.use_cts_prot = -1;
4887 params.use_short_preamble = -1;
4888 params.use_short_slot_time = -1;
4889 params.ap_isolate = -1;
4890 params.ht_opmode = -1;
4891 params.p2p_ctwindow = -1;
4892 params.p2p_opp_ps = -1;
4894 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4895 params.use_cts_prot =
4896 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4897 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4898 params.use_short_preamble =
4899 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4900 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4901 params.use_short_slot_time =
4902 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
4903 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4904 params.basic_rates =
4905 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4906 params.basic_rates_len =
4907 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4909 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4910 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
4911 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4912 params.ht_opmode =
4913 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
4915 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4916 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4917 return -EINVAL;
4918 params.p2p_ctwindow =
4919 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4920 if (params.p2p_ctwindow < 0)
4921 return -EINVAL;
4922 if (params.p2p_ctwindow != 0 &&
4923 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4924 return -EINVAL;
4927 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4928 u8 tmp;
4930 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4931 return -EINVAL;
4932 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4933 if (tmp > 1)
4934 return -EINVAL;
4935 params.p2p_opp_ps = tmp;
4936 if (params.p2p_opp_ps &&
4937 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4938 return -EINVAL;
4941 if (!rdev->ops->change_bss)
4942 return -EOPNOTSUPP;
4944 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4945 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4946 return -EOPNOTSUPP;
4948 wdev_lock(wdev);
4949 err = rdev_change_bss(rdev, dev, &params);
4950 wdev_unlock(wdev);
4952 return err;
4955 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4957 char *data = NULL;
4958 bool is_indoor;
4959 enum nl80211_user_reg_hint_type user_reg_hint_type;
4960 u32 owner_nlportid;
4964 * You should only get this when cfg80211 hasn't yet initialized
4965 * completely when built-in to the kernel right between the time
4966 * window between nl80211_init() and regulatory_init(), if that is
4967 * even possible.
4969 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
4970 return -EINPROGRESS;
4972 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4973 user_reg_hint_type =
4974 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4975 else
4976 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4978 switch (user_reg_hint_type) {
4979 case NL80211_USER_REG_HINT_USER:
4980 case NL80211_USER_REG_HINT_CELL_BASE:
4981 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4982 return -EINVAL;
4984 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4985 return regulatory_hint_user(data, user_reg_hint_type);
4986 case NL80211_USER_REG_HINT_INDOOR:
4987 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
4988 owner_nlportid = info->snd_portid;
4989 is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
4990 } else {
4991 owner_nlportid = 0;
4992 is_indoor = true;
4995 return regulatory_hint_indoor(is_indoor, owner_nlportid);
4996 default:
4997 return -EINVAL;
5001 static int nl80211_get_mesh_config(struct sk_buff *skb,
5002 struct genl_info *info)
5004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5005 struct net_device *dev = info->user_ptr[1];
5006 struct wireless_dev *wdev = dev->ieee80211_ptr;
5007 struct mesh_config cur_params;
5008 int err = 0;
5009 void *hdr;
5010 struct nlattr *pinfoattr;
5011 struct sk_buff *msg;
5013 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5014 return -EOPNOTSUPP;
5016 if (!rdev->ops->get_mesh_config)
5017 return -EOPNOTSUPP;
5019 wdev_lock(wdev);
5020 /* If not connected, get default parameters */
5021 if (!wdev->mesh_id_len)
5022 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5023 else
5024 err = rdev_get_mesh_config(rdev, dev, &cur_params);
5025 wdev_unlock(wdev);
5027 if (err)
5028 return err;
5030 /* Draw up a netlink message to send back */
5031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5032 if (!msg)
5033 return -ENOMEM;
5034 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5035 NL80211_CMD_GET_MESH_CONFIG);
5036 if (!hdr)
5037 goto out;
5038 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
5039 if (!pinfoattr)
5040 goto nla_put_failure;
5041 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5042 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5043 cur_params.dot11MeshRetryTimeout) ||
5044 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5045 cur_params.dot11MeshConfirmTimeout) ||
5046 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5047 cur_params.dot11MeshHoldingTimeout) ||
5048 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5049 cur_params.dot11MeshMaxPeerLinks) ||
5050 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5051 cur_params.dot11MeshMaxRetries) ||
5052 nla_put_u8(msg, NL80211_MESHCONF_TTL,
5053 cur_params.dot11MeshTTL) ||
5054 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5055 cur_params.element_ttl) ||
5056 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5057 cur_params.auto_open_plinks) ||
5058 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5059 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
5060 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5061 cur_params.dot11MeshHWMPmaxPREQretries) ||
5062 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5063 cur_params.path_refresh_time) ||
5064 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5065 cur_params.min_discovery_timeout) ||
5066 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5067 cur_params.dot11MeshHWMPactivePathTimeout) ||
5068 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5069 cur_params.dot11MeshHWMPpreqMinInterval) ||
5070 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5071 cur_params.dot11MeshHWMPperrMinInterval) ||
5072 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5073 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5074 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5075 cur_params.dot11MeshHWMPRootMode) ||
5076 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5077 cur_params.dot11MeshHWMPRannInterval) ||
5078 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5079 cur_params.dot11MeshGateAnnouncementProtocol) ||
5080 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5081 cur_params.dot11MeshForwarding) ||
5082 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
5083 cur_params.rssi_threshold) ||
5084 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
5085 cur_params.ht_opmode) ||
5086 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5087 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5088 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5089 cur_params.dot11MeshHWMProotInterval) ||
5090 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5091 cur_params.dot11MeshHWMPconfirmationInterval) ||
5092 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5093 cur_params.power_mode) ||
5094 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
5095 cur_params.dot11MeshAwakeWindowDuration) ||
5096 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5097 cur_params.plink_timeout))
5098 goto nla_put_failure;
5099 nla_nest_end(msg, pinfoattr);
5100 genlmsg_end(msg, hdr);
5101 return genlmsg_reply(msg, info);
5103 nla_put_failure:
5104 genlmsg_cancel(msg, hdr);
5105 out:
5106 nlmsg_free(msg);
5107 return -ENOBUFS;
5110 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
5111 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5112 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5113 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5114 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5115 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5116 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
5117 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
5118 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
5119 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
5120 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5121 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5122 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5123 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5124 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
5125 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
5126 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
5127 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
5128 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
5129 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
5130 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
5131 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5132 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
5133 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5134 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
5135 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
5136 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5137 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
5138 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
5141 static const struct nla_policy
5142 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
5143 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
5144 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5145 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
5146 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
5147 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
5148 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
5149 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
5150 .len = IEEE80211_MAX_DATA_LEN },
5151 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
5154 static int nl80211_parse_mesh_config(struct genl_info *info,
5155 struct mesh_config *cfg,
5156 u32 *mask_out)
5158 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
5159 u32 mask = 0;
5161 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5162 do { \
5163 if (tb[attr]) { \
5164 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
5165 return -EINVAL; \
5166 cfg->param = fn(tb[attr]); \
5167 mask |= (1 << (attr - 1)); \
5169 } while (0)
5172 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
5173 return -EINVAL;
5174 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
5175 info->attrs[NL80211_ATTR_MESH_CONFIG],
5176 nl80211_meshconf_params_policy))
5177 return -EINVAL;
5179 /* This makes sure that there aren't more than 32 mesh config
5180 * parameters (otherwise our bitfield scheme would not work.) */
5181 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5183 /* Fill in the params struct */
5184 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
5185 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5186 nla_get_u16);
5187 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
5188 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5189 nla_get_u16);
5190 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
5191 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5192 nla_get_u16);
5193 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
5194 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5195 nla_get_u16);
5196 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
5197 mask, NL80211_MESHCONF_MAX_RETRIES,
5198 nla_get_u8);
5199 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
5200 mask, NL80211_MESHCONF_TTL, nla_get_u8);
5201 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
5202 mask, NL80211_MESHCONF_ELEMENT_TTL,
5203 nla_get_u8);
5204 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
5205 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5206 nla_get_u8);
5207 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
5208 1, 255, mask,
5209 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5210 nla_get_u32);
5211 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
5212 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5213 nla_get_u8);
5214 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
5215 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
5216 nla_get_u32);
5217 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
5218 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5219 nla_get_u16);
5220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
5221 1, 65535, mask,
5222 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5223 nla_get_u32);
5224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
5225 1, 65535, mask,
5226 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5227 nla_get_u16);
5228 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
5229 1, 65535, mask,
5230 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5231 nla_get_u16);
5232 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5233 dot11MeshHWMPnetDiameterTraversalTime,
5234 1, 65535, mask,
5235 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5236 nla_get_u16);
5237 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
5238 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
5239 nla_get_u8);
5240 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
5241 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5242 nla_get_u16);
5243 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5244 dot11MeshGateAnnouncementProtocol, 0, 1,
5245 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5246 nla_get_u8);
5247 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
5248 mask, NL80211_MESHCONF_FORWARDING,
5249 nla_get_u8);
5250 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
5251 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
5252 nla_get_s32);
5253 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
5254 mask, NL80211_MESHCONF_HT_OPMODE,
5255 nla_get_u16);
5256 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
5257 1, 65535, mask,
5258 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5259 nla_get_u32);
5260 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
5261 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5262 nla_get_u16);
5263 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5264 dot11MeshHWMPconfirmationInterval,
5265 1, 65535, mask,
5266 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5267 nla_get_u16);
5268 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
5269 NL80211_MESH_POWER_ACTIVE,
5270 NL80211_MESH_POWER_MAX,
5271 mask, NL80211_MESHCONF_POWER_MODE,
5272 nla_get_u32);
5273 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5274 0, 65535, mask,
5275 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
5276 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
5277 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5278 nla_get_u32);
5279 if (mask_out)
5280 *mask_out = mask;
5282 return 0;
5284 #undef FILL_IN_MESH_PARAM_IF_SET
5287 static int nl80211_parse_mesh_setup(struct genl_info *info,
5288 struct mesh_setup *setup)
5290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5291 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5293 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5294 return -EINVAL;
5295 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5296 info->attrs[NL80211_ATTR_MESH_SETUP],
5297 nl80211_mesh_setup_params_policy))
5298 return -EINVAL;
5300 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5301 setup->sync_method =
5302 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5303 IEEE80211_SYNC_METHOD_VENDOR :
5304 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5306 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5307 setup->path_sel_proto =
5308 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5309 IEEE80211_PATH_PROTOCOL_VENDOR :
5310 IEEE80211_PATH_PROTOCOL_HWMP;
5312 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5313 setup->path_metric =
5314 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5315 IEEE80211_PATH_METRIC_VENDOR :
5316 IEEE80211_PATH_METRIC_AIRTIME;
5319 if (tb[NL80211_MESH_SETUP_IE]) {
5320 struct nlattr *ieattr =
5321 tb[NL80211_MESH_SETUP_IE];
5322 if (!is_valid_ie_attr(ieattr))
5323 return -EINVAL;
5324 setup->ie = nla_data(ieattr);
5325 setup->ie_len = nla_len(ieattr);
5327 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5328 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5329 return -EINVAL;
5330 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
5331 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5332 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
5333 if (setup->is_secure)
5334 setup->user_mpm = true;
5336 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5337 if (!setup->user_mpm)
5338 return -EINVAL;
5339 setup->auth_id =
5340 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5343 return 0;
5346 static int nl80211_update_mesh_config(struct sk_buff *skb,
5347 struct genl_info *info)
5349 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5350 struct net_device *dev = info->user_ptr[1];
5351 struct wireless_dev *wdev = dev->ieee80211_ptr;
5352 struct mesh_config cfg;
5353 u32 mask;
5354 int err;
5356 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5357 return -EOPNOTSUPP;
5359 if (!rdev->ops->update_mesh_config)
5360 return -EOPNOTSUPP;
5362 err = nl80211_parse_mesh_config(info, &cfg, &mask);
5363 if (err)
5364 return err;
5366 wdev_lock(wdev);
5367 if (!wdev->mesh_id_len)
5368 err = -ENOLINK;
5370 if (!err)
5371 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
5373 wdev_unlock(wdev);
5375 return err;
5378 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
5379 struct sk_buff *msg)
5381 struct nlattr *nl_reg_rules;
5382 unsigned int i;
5384 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5385 (regdom->dfs_region &&
5386 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5387 goto nla_put_failure;
5389 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5390 if (!nl_reg_rules)
5391 goto nla_put_failure;
5393 for (i = 0; i < regdom->n_reg_rules; i++) {
5394 struct nlattr *nl_reg_rule;
5395 const struct ieee80211_reg_rule *reg_rule;
5396 const struct ieee80211_freq_range *freq_range;
5397 const struct ieee80211_power_rule *power_rule;
5398 unsigned int max_bandwidth_khz;
5400 reg_rule = &regdom->reg_rules[i];
5401 freq_range = &reg_rule->freq_range;
5402 power_rule = &reg_rule->power_rule;
5404 nl_reg_rule = nla_nest_start(msg, i);
5405 if (!nl_reg_rule)
5406 goto nla_put_failure;
5408 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5409 if (!max_bandwidth_khz)
5410 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5411 reg_rule);
5413 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5414 reg_rule->flags) ||
5415 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5416 freq_range->start_freq_khz) ||
5417 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5418 freq_range->end_freq_khz) ||
5419 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5420 max_bandwidth_khz) ||
5421 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5422 power_rule->max_antenna_gain) ||
5423 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5424 power_rule->max_eirp) ||
5425 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5426 reg_rule->dfs_cac_ms))
5427 goto nla_put_failure;
5429 nla_nest_end(msg, nl_reg_rule);
5432 nla_nest_end(msg, nl_reg_rules);
5433 return 0;
5435 nla_put_failure:
5436 return -EMSGSIZE;
5439 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
5441 const struct ieee80211_regdomain *regdom = NULL;
5442 struct cfg80211_registered_device *rdev;
5443 struct wiphy *wiphy = NULL;
5444 struct sk_buff *msg;
5445 void *hdr;
5447 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5448 if (!msg)
5449 return -ENOBUFS;
5451 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5452 NL80211_CMD_GET_REG);
5453 if (!hdr)
5454 goto put_failure;
5456 if (info->attrs[NL80211_ATTR_WIPHY]) {
5457 bool self_managed;
5459 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
5460 if (IS_ERR(rdev)) {
5461 nlmsg_free(msg);
5462 return PTR_ERR(rdev);
5465 wiphy = &rdev->wiphy;
5466 self_managed = wiphy->regulatory_flags &
5467 REGULATORY_WIPHY_SELF_MANAGED;
5468 regdom = get_wiphy_regdom(wiphy);
5470 /* a self-managed-reg device must have a private regdom */
5471 if (WARN_ON(!regdom && self_managed)) {
5472 nlmsg_free(msg);
5473 return -EINVAL;
5476 if (regdom &&
5477 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
5478 goto nla_put_failure;
5481 if (!wiphy && reg_last_request_cell_base() &&
5482 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5483 NL80211_USER_REG_HINT_CELL_BASE))
5484 goto nla_put_failure;
5486 rcu_read_lock();
5488 if (!regdom)
5489 regdom = rcu_dereference(cfg80211_regdomain);
5491 if (nl80211_put_regdom(regdom, msg))
5492 goto nla_put_failure_rcu;
5494 rcu_read_unlock();
5496 genlmsg_end(msg, hdr);
5497 return genlmsg_reply(msg, info);
5499 nla_put_failure_rcu:
5500 rcu_read_unlock();
5501 nla_put_failure:
5502 genlmsg_cancel(msg, hdr);
5503 put_failure:
5504 nlmsg_free(msg);
5505 return -EMSGSIZE;
5508 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
5509 u32 seq, int flags, struct wiphy *wiphy,
5510 const struct ieee80211_regdomain *regdom)
5512 void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
5513 NL80211_CMD_GET_REG);
5515 if (!hdr)
5516 return -1;
5518 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5520 if (nl80211_put_regdom(regdom, msg))
5521 goto nla_put_failure;
5523 if (!wiphy && reg_last_request_cell_base() &&
5524 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5525 NL80211_USER_REG_HINT_CELL_BASE))
5526 goto nla_put_failure;
5528 if (wiphy &&
5529 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
5530 goto nla_put_failure;
5532 if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
5533 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
5534 goto nla_put_failure;
5536 genlmsg_end(msg, hdr);
5537 return 0;
5539 nla_put_failure:
5540 genlmsg_cancel(msg, hdr);
5541 return -EMSGSIZE;
5544 static int nl80211_get_reg_dump(struct sk_buff *skb,
5545 struct netlink_callback *cb)
5547 const struct ieee80211_regdomain *regdom = NULL;
5548 struct cfg80211_registered_device *rdev;
5549 int err, reg_idx, start = cb->args[2];
5551 rtnl_lock();
5553 if (cfg80211_regdomain && start == 0) {
5554 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
5555 NLM_F_MULTI, NULL,
5556 rtnl_dereference(cfg80211_regdomain));
5557 if (err < 0)
5558 goto out_err;
5561 /* the global regdom is idx 0 */
5562 reg_idx = 1;
5563 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
5564 regdom = get_wiphy_regdom(&rdev->wiphy);
5565 if (!regdom)
5566 continue;
5568 if (++reg_idx <= start)
5569 continue;
5571 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
5572 NLM_F_MULTI, &rdev->wiphy, regdom);
5573 if (err < 0) {
5574 reg_idx--;
5575 break;
5579 cb->args[2] = reg_idx;
5580 err = skb->len;
5581 out_err:
5582 rtnl_unlock();
5583 return err;
5586 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
5587 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
5588 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5589 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5590 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5591 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5592 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5593 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5594 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
5597 static int parse_reg_rule(struct nlattr *tb[],
5598 struct ieee80211_reg_rule *reg_rule)
5600 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
5601 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
5603 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
5604 return -EINVAL;
5605 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
5606 return -EINVAL;
5607 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
5608 return -EINVAL;
5609 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
5610 return -EINVAL;
5611 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
5612 return -EINVAL;
5614 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
5616 freq_range->start_freq_khz =
5617 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
5618 freq_range->end_freq_khz =
5619 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
5620 freq_range->max_bandwidth_khz =
5621 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
5623 power_rule->max_eirp =
5624 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
5626 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
5627 power_rule->max_antenna_gain =
5628 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
5630 if (tb[NL80211_ATTR_DFS_CAC_TIME])
5631 reg_rule->dfs_cac_ms =
5632 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
5634 return 0;
5637 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5639 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5640 struct nlattr *nl_reg_rule;
5641 char *alpha2;
5642 int rem_reg_rules, r;
5643 u32 num_rules = 0, rule_idx = 0, size_of_regd;
5644 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
5645 struct ieee80211_regdomain *rd;
5647 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5648 return -EINVAL;
5650 if (!info->attrs[NL80211_ATTR_REG_RULES])
5651 return -EINVAL;
5653 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5655 if (info->attrs[NL80211_ATTR_DFS_REGION])
5656 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5658 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5659 rem_reg_rules) {
5660 num_rules++;
5661 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
5662 return -EINVAL;
5665 if (!reg_is_valid_request(alpha2))
5666 return -EINVAL;
5668 size_of_regd = sizeof(struct ieee80211_regdomain) +
5669 num_rules * sizeof(struct ieee80211_reg_rule);
5671 rd = kzalloc(size_of_regd, GFP_KERNEL);
5672 if (!rd)
5673 return -ENOMEM;
5675 rd->n_reg_rules = num_rules;
5676 rd->alpha2[0] = alpha2[0];
5677 rd->alpha2[1] = alpha2[1];
5680 * Disable DFS master mode if the DFS region was
5681 * not supported or known on this kernel.
5683 if (reg_supported_dfs_region(dfs_region))
5684 rd->dfs_region = dfs_region;
5686 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5687 rem_reg_rules) {
5688 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5689 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5690 reg_rule_policy);
5691 if (r)
5692 goto bad_reg;
5693 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5694 if (r)
5695 goto bad_reg;
5697 rule_idx++;
5699 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5700 r = -EINVAL;
5701 goto bad_reg;
5705 r = set_regdom(rd, REGD_SOURCE_CRDA);
5706 /* set_regdom took ownership */
5707 rd = NULL;
5709 bad_reg:
5710 kfree(rd);
5711 return r;
5713 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
5715 static int validate_scan_freqs(struct nlattr *freqs)
5717 struct nlattr *attr1, *attr2;
5718 int n_channels = 0, tmp1, tmp2;
5720 nla_for_each_nested(attr1, freqs, tmp1)
5721 if (nla_len(attr1) != sizeof(u32))
5722 return 0;
5724 nla_for_each_nested(attr1, freqs, tmp1) {
5725 n_channels++;
5727 * Some hardware has a limited channel list for
5728 * scanning, and it is pretty much nonsensical
5729 * to scan for a channel twice, so disallow that
5730 * and don't require drivers to check that the
5731 * channel list they get isn't longer than what
5732 * they can scan, as long as they can scan all
5733 * the channels they registered at once.
5735 nla_for_each_nested(attr2, freqs, tmp2)
5736 if (attr1 != attr2 &&
5737 nla_get_u32(attr1) == nla_get_u32(attr2))
5738 return 0;
5741 return n_channels;
5744 static int nl80211_parse_random_mac(struct nlattr **attrs,
5745 u8 *mac_addr, u8 *mac_addr_mask)
5747 int i;
5749 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
5750 eth_zero_addr(mac_addr);
5751 eth_zero_addr(mac_addr_mask);
5752 mac_addr[0] = 0x2;
5753 mac_addr_mask[0] = 0x3;
5755 return 0;
5758 /* need both or none */
5759 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
5760 return -EINVAL;
5762 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
5763 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
5765 /* don't allow or configure an mcast address */
5766 if (!is_multicast_ether_addr(mac_addr_mask) ||
5767 is_multicast_ether_addr(mac_addr))
5768 return -EINVAL;
5771 * allow users to pass a MAC address that has bits set outside
5772 * of the mask, but don't bother drivers with having to deal
5773 * with such bits
5775 for (i = 0; i < ETH_ALEN; i++)
5776 mac_addr[i] &= mac_addr_mask[i];
5778 return 0;
5781 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5783 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5784 struct wireless_dev *wdev = info->user_ptr[1];
5785 struct cfg80211_scan_request *request;
5786 struct nlattr *attr;
5787 struct wiphy *wiphy;
5788 int err, tmp, n_ssids = 0, n_channels, i;
5789 size_t ie_len;
5791 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5792 return -EINVAL;
5794 wiphy = &rdev->wiphy;
5796 if (!rdev->ops->scan)
5797 return -EOPNOTSUPP;
5799 if (rdev->scan_req || rdev->scan_msg) {
5800 err = -EBUSY;
5801 goto unlock;
5804 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5805 n_channels = validate_scan_freqs(
5806 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5807 if (!n_channels) {
5808 err = -EINVAL;
5809 goto unlock;
5811 } else {
5812 n_channels = ieee80211_get_num_supported_channels(wiphy);
5815 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5816 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5817 n_ssids++;
5819 if (n_ssids > wiphy->max_scan_ssids) {
5820 err = -EINVAL;
5821 goto unlock;
5824 if (info->attrs[NL80211_ATTR_IE])
5825 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5826 else
5827 ie_len = 0;
5829 if (ie_len > wiphy->max_scan_ie_len) {
5830 err = -EINVAL;
5831 goto unlock;
5834 request = kzalloc(sizeof(*request)
5835 + sizeof(*request->ssids) * n_ssids
5836 + sizeof(*request->channels) * n_channels
5837 + ie_len, GFP_KERNEL);
5838 if (!request) {
5839 err = -ENOMEM;
5840 goto unlock;
5843 if (n_ssids)
5844 request->ssids = (void *)&request->channels[n_channels];
5845 request->n_ssids = n_ssids;
5846 if (ie_len) {
5847 if (n_ssids)
5848 request->ie = (void *)(request->ssids + n_ssids);
5849 else
5850 request->ie = (void *)(request->channels + n_channels);
5853 i = 0;
5854 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5855 /* user specified, bail out if channel not found */
5856 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
5857 struct ieee80211_channel *chan;
5859 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5861 if (!chan) {
5862 err = -EINVAL;
5863 goto out_free;
5866 /* ignore disabled channels */
5867 if (chan->flags & IEEE80211_CHAN_DISABLED)
5868 continue;
5870 request->channels[i] = chan;
5871 i++;
5873 } else {
5874 enum ieee80211_band band;
5876 /* all channels */
5877 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5878 int j;
5879 if (!wiphy->bands[band])
5880 continue;
5881 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5882 struct ieee80211_channel *chan;
5884 chan = &wiphy->bands[band]->channels[j];
5886 if (chan->flags & IEEE80211_CHAN_DISABLED)
5887 continue;
5889 request->channels[i] = chan;
5890 i++;
5895 if (!i) {
5896 err = -EINVAL;
5897 goto out_free;
5900 request->n_channels = i;
5902 i = 0;
5903 if (n_ssids) {
5904 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
5905 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5906 err = -EINVAL;
5907 goto out_free;
5909 request->ssids[i].ssid_len = nla_len(attr);
5910 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
5911 i++;
5915 if (info->attrs[NL80211_ATTR_IE]) {
5916 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5917 memcpy((void *)request->ie,
5918 nla_data(info->attrs[NL80211_ATTR_IE]),
5919 request->ie_len);
5922 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
5923 if (wiphy->bands[i])
5924 request->rates[i] =
5925 (1 << wiphy->bands[i]->n_bitrates) - 1;
5927 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5928 nla_for_each_nested(attr,
5929 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5930 tmp) {
5931 enum ieee80211_band band = nla_type(attr);
5933 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
5934 err = -EINVAL;
5935 goto out_free;
5938 if (!wiphy->bands[band])
5939 continue;
5941 err = ieee80211_get_ratemask(wiphy->bands[band],
5942 nla_data(attr),
5943 nla_len(attr),
5944 &request->rates[band]);
5945 if (err)
5946 goto out_free;
5950 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5951 request->flags = nla_get_u32(
5952 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5953 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5954 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
5955 err = -EOPNOTSUPP;
5956 goto out_free;
5959 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
5960 if (!(wiphy->features &
5961 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
5962 err = -EOPNOTSUPP;
5963 goto out_free;
5966 if (wdev->current_bss) {
5967 err = -EOPNOTSUPP;
5968 goto out_free;
5971 err = nl80211_parse_random_mac(info->attrs,
5972 request->mac_addr,
5973 request->mac_addr_mask);
5974 if (err)
5975 goto out_free;
5979 request->no_cck =
5980 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5982 request->wdev = wdev;
5983 request->wiphy = &rdev->wiphy;
5984 request->scan_start = jiffies;
5986 rdev->scan_req = request;
5987 err = rdev_scan(rdev, request);
5989 if (!err) {
5990 nl80211_send_scan_start(rdev, wdev);
5991 if (wdev->netdev)
5992 dev_hold(wdev->netdev);
5993 } else {
5994 out_free:
5995 rdev->scan_req = NULL;
5996 kfree(request);
5999 unlock:
6000 return err;
6003 static int
6004 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
6005 struct cfg80211_sched_scan_request *request,
6006 struct nlattr **attrs)
6008 int tmp, err, i = 0;
6009 struct nlattr *attr;
6011 if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6012 u32 interval;
6015 * If scan plans are not specified,
6016 * %NL80211_ATTR_SCHED_SCAN_INTERVAL must be specified. In this
6017 * case one scan plan will be set with the specified scan
6018 * interval and infinite number of iterations.
6020 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6021 return -EINVAL;
6023 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
6024 if (!interval)
6025 return -EINVAL;
6027 request->scan_plans[0].interval =
6028 DIV_ROUND_UP(interval, MSEC_PER_SEC);
6029 if (!request->scan_plans[0].interval)
6030 return -EINVAL;
6032 if (request->scan_plans[0].interval >
6033 wiphy->max_sched_scan_plan_interval)
6034 request->scan_plans[0].interval =
6035 wiphy->max_sched_scan_plan_interval;
6037 return 0;
6040 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
6041 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
6043 if (WARN_ON(i >= n_plans))
6044 return -EINVAL;
6046 err = nla_parse(plan, NL80211_SCHED_SCAN_PLAN_MAX,
6047 nla_data(attr), nla_len(attr),
6048 nl80211_plan_policy);
6049 if (err)
6050 return err;
6052 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
6053 return -EINVAL;
6055 request->scan_plans[i].interval =
6056 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
6057 if (!request->scan_plans[i].interval ||
6058 request->scan_plans[i].interval >
6059 wiphy->max_sched_scan_plan_interval)
6060 return -EINVAL;
6062 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
6063 request->scan_plans[i].iterations =
6064 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
6065 if (!request->scan_plans[i].iterations ||
6066 (request->scan_plans[i].iterations >
6067 wiphy->max_sched_scan_plan_iterations))
6068 return -EINVAL;
6069 } else if (i < n_plans - 1) {
6071 * All scan plans but the last one must specify
6072 * a finite number of iterations
6074 return -EINVAL;
6077 i++;
6081 * The last scan plan must not specify the number of
6082 * iterations, it is supposed to run infinitely
6084 if (request->scan_plans[n_plans - 1].iterations)
6085 return -EINVAL;
6087 return 0;
6090 static struct cfg80211_sched_scan_request *
6091 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
6092 struct nlattr **attrs)
6094 struct cfg80211_sched_scan_request *request;
6095 struct nlattr *attr;
6096 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
6097 enum ieee80211_band band;
6098 size_t ie_len;
6099 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
6100 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
6102 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
6103 return ERR_PTR(-EINVAL);
6105 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6106 n_channels = validate_scan_freqs(
6107 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
6108 if (!n_channels)
6109 return ERR_PTR(-EINVAL);
6110 } else {
6111 n_channels = ieee80211_get_num_supported_channels(wiphy);
6114 if (attrs[NL80211_ATTR_SCAN_SSIDS])
6115 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
6116 tmp)
6117 n_ssids++;
6119 if (n_ssids > wiphy->max_sched_scan_ssids)
6120 return ERR_PTR(-EINVAL);
6123 * First, count the number of 'real' matchsets. Due to an issue with
6124 * the old implementation, matchsets containing only the RSSI attribute
6125 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
6126 * RSSI for all matchsets, rather than their own matchset for reporting
6127 * all APs with a strong RSSI. This is needed to be compatible with
6128 * older userspace that treated a matchset with only the RSSI as the
6129 * global RSSI for all other matchsets - if there are other matchsets.
6131 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
6132 nla_for_each_nested(attr,
6133 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
6134 tmp) {
6135 struct nlattr *rssi;
6137 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
6138 nla_data(attr), nla_len(attr),
6139 nl80211_match_policy);
6140 if (err)
6141 return ERR_PTR(err);
6142 /* add other standalone attributes here */
6143 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
6144 n_match_sets++;
6145 continue;
6147 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
6148 if (rssi)
6149 default_match_rssi = nla_get_s32(rssi);
6153 /* However, if there's no other matchset, add the RSSI one */
6154 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
6155 n_match_sets = 1;
6157 if (n_match_sets > wiphy->max_match_sets)
6158 return ERR_PTR(-EINVAL);
6160 if (attrs[NL80211_ATTR_IE])
6161 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
6162 else
6163 ie_len = 0;
6165 if (ie_len > wiphy->max_sched_scan_ie_len)
6166 return ERR_PTR(-EINVAL);
6168 if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6170 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
6171 * each scan plan already specifies its own interval
6173 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6174 return ERR_PTR(-EINVAL);
6176 nla_for_each_nested(attr,
6177 attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
6178 n_plans++;
6179 } else {
6181 * The scan interval attribute is kept for backward
6182 * compatibility. If no scan plans are specified and sched scan
6183 * interval is specified, one scan plan will be set with this
6184 * scan interval and infinite number of iterations.
6186 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6187 return ERR_PTR(-EINVAL);
6189 n_plans = 1;
6192 if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
6193 return ERR_PTR(-EINVAL);
6195 request = kzalloc(sizeof(*request)
6196 + sizeof(*request->ssids) * n_ssids
6197 + sizeof(*request->match_sets) * n_match_sets
6198 + sizeof(*request->scan_plans) * n_plans
6199 + sizeof(*request->channels) * n_channels
6200 + ie_len, GFP_KERNEL);
6201 if (!request)
6202 return ERR_PTR(-ENOMEM);
6204 if (n_ssids)
6205 request->ssids = (void *)&request->channels[n_channels];
6206 request->n_ssids = n_ssids;
6207 if (ie_len) {
6208 if (n_ssids)
6209 request->ie = (void *)(request->ssids + n_ssids);
6210 else
6211 request->ie = (void *)(request->channels + n_channels);
6214 if (n_match_sets) {
6215 if (request->ie)
6216 request->match_sets = (void *)(request->ie + ie_len);
6217 else if (n_ssids)
6218 request->match_sets =
6219 (void *)(request->ssids + n_ssids);
6220 else
6221 request->match_sets =
6222 (void *)(request->channels + n_channels);
6224 request->n_match_sets = n_match_sets;
6226 if (n_match_sets)
6227 request->scan_plans = (void *)(request->match_sets +
6228 n_match_sets);
6229 else if (request->ie)
6230 request->scan_plans = (void *)(request->ie + ie_len);
6231 else if (n_ssids)
6232 request->scan_plans = (void *)(request->ssids + n_ssids);
6233 else
6234 request->scan_plans = (void *)(request->channels + n_channels);
6236 request->n_scan_plans = n_plans;
6238 i = 0;
6239 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6240 /* user specified, bail out if channel not found */
6241 nla_for_each_nested(attr,
6242 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
6243 tmp) {
6244 struct ieee80211_channel *chan;
6246 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6248 if (!chan) {
6249 err = -EINVAL;
6250 goto out_free;
6253 /* ignore disabled channels */
6254 if (chan->flags & IEEE80211_CHAN_DISABLED)
6255 continue;
6257 request->channels[i] = chan;
6258 i++;
6260 } else {
6261 /* all channels */
6262 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6263 int j;
6264 if (!wiphy->bands[band])
6265 continue;
6266 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
6267 struct ieee80211_channel *chan;
6269 chan = &wiphy->bands[band]->channels[j];
6271 if (chan->flags & IEEE80211_CHAN_DISABLED)
6272 continue;
6274 request->channels[i] = chan;
6275 i++;
6280 if (!i) {
6281 err = -EINVAL;
6282 goto out_free;
6285 request->n_channels = i;
6287 i = 0;
6288 if (n_ssids) {
6289 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
6290 tmp) {
6291 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
6292 err = -EINVAL;
6293 goto out_free;
6295 request->ssids[i].ssid_len = nla_len(attr);
6296 memcpy(request->ssids[i].ssid, nla_data(attr),
6297 nla_len(attr));
6298 i++;
6302 i = 0;
6303 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
6304 nla_for_each_nested(attr,
6305 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
6306 tmp) {
6307 struct nlattr *ssid, *rssi;
6309 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
6310 nla_data(attr), nla_len(attr),
6311 nl80211_match_policy);
6312 if (err)
6313 goto out_free;
6314 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
6315 if (ssid) {
6316 if (WARN_ON(i >= n_match_sets)) {
6317 /* this indicates a programming error,
6318 * the loop above should have verified
6319 * things properly
6321 err = -EINVAL;
6322 goto out_free;
6325 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
6326 err = -EINVAL;
6327 goto out_free;
6329 memcpy(request->match_sets[i].ssid.ssid,
6330 nla_data(ssid), nla_len(ssid));
6331 request->match_sets[i].ssid.ssid_len =
6332 nla_len(ssid);
6333 /* special attribute - old implemenation w/a */
6334 request->match_sets[i].rssi_thold =
6335 default_match_rssi;
6336 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
6337 if (rssi)
6338 request->match_sets[i].rssi_thold =
6339 nla_get_s32(rssi);
6341 i++;
6344 /* there was no other matchset, so the RSSI one is alone */
6345 if (i == 0 && n_match_sets)
6346 request->match_sets[0].rssi_thold = default_match_rssi;
6348 request->min_rssi_thold = INT_MAX;
6349 for (i = 0; i < n_match_sets; i++)
6350 request->min_rssi_thold =
6351 min(request->match_sets[i].rssi_thold,
6352 request->min_rssi_thold);
6353 } else {
6354 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
6357 if (ie_len) {
6358 request->ie_len = ie_len;
6359 memcpy((void *)request->ie,
6360 nla_data(attrs[NL80211_ATTR_IE]),
6361 request->ie_len);
6364 if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
6365 request->flags = nla_get_u32(
6366 attrs[NL80211_ATTR_SCAN_FLAGS]);
6367 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6368 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
6369 err = -EOPNOTSUPP;
6370 goto out_free;
6373 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6374 u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
6376 if (!wdev) /* must be net-detect */
6377 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6379 if (!(wiphy->features & flg)) {
6380 err = -EOPNOTSUPP;
6381 goto out_free;
6384 if (wdev && wdev->current_bss) {
6385 err = -EOPNOTSUPP;
6386 goto out_free;
6389 err = nl80211_parse_random_mac(attrs, request->mac_addr,
6390 request->mac_addr_mask);
6391 if (err)
6392 goto out_free;
6396 if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
6397 request->delay =
6398 nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
6400 err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
6401 if (err)
6402 goto out_free;
6404 request->scan_start = jiffies;
6406 return request;
6408 out_free:
6409 kfree(request);
6410 return ERR_PTR(err);
6413 static int nl80211_start_sched_scan(struct sk_buff *skb,
6414 struct genl_info *info)
6416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6417 struct net_device *dev = info->user_ptr[1];
6418 struct wireless_dev *wdev = dev->ieee80211_ptr;
6419 struct cfg80211_sched_scan_request *sched_scan_req;
6420 int err;
6422 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6423 !rdev->ops->sched_scan_start)
6424 return -EOPNOTSUPP;
6426 if (rdev->sched_scan_req)
6427 return -EINPROGRESS;
6429 sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
6430 info->attrs);
6432 err = PTR_ERR_OR_ZERO(sched_scan_req);
6433 if (err)
6434 goto out_err;
6436 err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
6437 if (err)
6438 goto out_free;
6440 sched_scan_req->dev = dev;
6441 sched_scan_req->wiphy = &rdev->wiphy;
6443 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6444 sched_scan_req->owner_nlportid = info->snd_portid;
6446 rcu_assign_pointer(rdev->sched_scan_req, sched_scan_req);
6448 nl80211_send_sched_scan(rdev, dev,
6449 NL80211_CMD_START_SCHED_SCAN);
6450 return 0;
6452 out_free:
6453 kfree(sched_scan_req);
6454 out_err:
6455 return err;
6458 static int nl80211_stop_sched_scan(struct sk_buff *skb,
6459 struct genl_info *info)
6461 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6463 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6464 !rdev->ops->sched_scan_stop)
6465 return -EOPNOTSUPP;
6467 return __cfg80211_stop_sched_scan(rdev, false);
6470 static int nl80211_start_radar_detection(struct sk_buff *skb,
6471 struct genl_info *info)
6473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6474 struct net_device *dev = info->user_ptr[1];
6475 struct wireless_dev *wdev = dev->ieee80211_ptr;
6476 struct cfg80211_chan_def chandef;
6477 enum nl80211_dfs_regions dfs_region;
6478 unsigned int cac_time_ms;
6479 int err;
6481 dfs_region = reg_get_dfs_region(wdev->wiphy);
6482 if (dfs_region == NL80211_DFS_UNSET)
6483 return -EINVAL;
6485 err = nl80211_parse_chandef(rdev, info, &chandef);
6486 if (err)
6487 return err;
6489 if (netif_carrier_ok(dev))
6490 return -EBUSY;
6492 if (wdev->cac_started)
6493 return -EBUSY;
6495 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
6496 wdev->iftype);
6497 if (err < 0)
6498 return err;
6500 if (err == 0)
6501 return -EINVAL;
6503 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
6504 return -EINVAL;
6506 if (!rdev->ops->start_radar_detection)
6507 return -EOPNOTSUPP;
6509 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
6510 if (WARN_ON(!cac_time_ms))
6511 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
6513 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
6514 cac_time_ms);
6515 if (!err) {
6516 wdev->chandef = chandef;
6517 wdev->cac_started = true;
6518 wdev->cac_start_time = jiffies;
6519 wdev->cac_time_ms = cac_time_ms;
6521 return err;
6524 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
6526 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6527 struct net_device *dev = info->user_ptr[1];
6528 struct wireless_dev *wdev = dev->ieee80211_ptr;
6529 struct cfg80211_csa_settings params;
6530 /* csa_attrs is defined static to avoid waste of stack size - this
6531 * function is called under RTNL lock, so this should not be a problem.
6533 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
6534 int err;
6535 bool need_new_beacon = false;
6536 int len, i;
6537 u32 cs_count;
6539 if (!rdev->ops->channel_switch ||
6540 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
6541 return -EOPNOTSUPP;
6543 switch (dev->ieee80211_ptr->iftype) {
6544 case NL80211_IFTYPE_AP:
6545 case NL80211_IFTYPE_P2P_GO:
6546 need_new_beacon = true;
6548 /* useless if AP is not running */
6549 if (!wdev->beacon_interval)
6550 return -ENOTCONN;
6551 break;
6552 case NL80211_IFTYPE_ADHOC:
6553 if (!wdev->ssid_len)
6554 return -ENOTCONN;
6555 break;
6556 case NL80211_IFTYPE_MESH_POINT:
6557 if (!wdev->mesh_id_len)
6558 return -ENOTCONN;
6559 break;
6560 default:
6561 return -EOPNOTSUPP;
6564 memset(&params, 0, sizeof(params));
6566 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6567 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
6568 return -EINVAL;
6570 /* only important for AP, IBSS and mesh create IEs internally */
6571 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
6572 return -EINVAL;
6574 /* Even though the attribute is u32, the specification says
6575 * u8, so let's make sure we don't overflow.
6577 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
6578 if (cs_count > 255)
6579 return -EINVAL;
6581 params.count = cs_count;
6583 if (!need_new_beacon)
6584 goto skip_beacons;
6586 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
6587 if (err)
6588 return err;
6590 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
6591 info->attrs[NL80211_ATTR_CSA_IES],
6592 nl80211_policy);
6593 if (err)
6594 return err;
6596 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
6597 if (err)
6598 return err;
6600 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
6601 return -EINVAL;
6603 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6604 if (!len || (len % sizeof(u16)))
6605 return -EINVAL;
6607 params.n_counter_offsets_beacon = len / sizeof(u16);
6608 if (rdev->wiphy.max_num_csa_counters &&
6609 (params.n_counter_offsets_beacon >
6610 rdev->wiphy.max_num_csa_counters))
6611 return -EINVAL;
6613 params.counter_offsets_beacon =
6614 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6616 /* sanity checks - counters should fit and be the same */
6617 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
6618 u16 offset = params.counter_offsets_beacon[i];
6620 if (offset >= params.beacon_csa.tail_len)
6621 return -EINVAL;
6623 if (params.beacon_csa.tail[offset] != params.count)
6624 return -EINVAL;
6627 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
6628 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6629 if (!len || (len % sizeof(u16)))
6630 return -EINVAL;
6632 params.n_counter_offsets_presp = len / sizeof(u16);
6633 if (rdev->wiphy.max_num_csa_counters &&
6634 (params.n_counter_offsets_presp >
6635 rdev->wiphy.max_num_csa_counters))
6636 return -EINVAL;
6638 params.counter_offsets_presp =
6639 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6641 /* sanity checks - counters should fit and be the same */
6642 for (i = 0; i < params.n_counter_offsets_presp; i++) {
6643 u16 offset = params.counter_offsets_presp[i];
6645 if (offset >= params.beacon_csa.probe_resp_len)
6646 return -EINVAL;
6648 if (params.beacon_csa.probe_resp[offset] !=
6649 params.count)
6650 return -EINVAL;
6654 skip_beacons:
6655 err = nl80211_parse_chandef(rdev, info, &params.chandef);
6656 if (err)
6657 return err;
6659 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
6660 wdev->iftype))
6661 return -EINVAL;
6663 err = cfg80211_chandef_dfs_required(wdev->wiphy,
6664 &params.chandef,
6665 wdev->iftype);
6666 if (err < 0)
6667 return err;
6669 if (err > 0)
6670 params.radar_required = true;
6672 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
6673 params.block_tx = true;
6675 wdev_lock(wdev);
6676 err = rdev_channel_switch(rdev, dev, &params);
6677 wdev_unlock(wdev);
6679 return err;
6682 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
6683 u32 seq, int flags,
6684 struct cfg80211_registered_device *rdev,
6685 struct wireless_dev *wdev,
6686 struct cfg80211_internal_bss *intbss)
6688 struct cfg80211_bss *res = &intbss->pub;
6689 const struct cfg80211_bss_ies *ies;
6690 void *hdr;
6691 struct nlattr *bss;
6693 ASSERT_WDEV_LOCK(wdev);
6695 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6696 NL80211_CMD_NEW_SCAN_RESULTS);
6697 if (!hdr)
6698 return -1;
6700 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6702 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6703 goto nla_put_failure;
6704 if (wdev->netdev &&
6705 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6706 goto nla_put_failure;
6707 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6708 goto nla_put_failure;
6710 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6711 if (!bss)
6712 goto nla_put_failure;
6713 if ((!is_zero_ether_addr(res->bssid) &&
6714 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
6715 goto nla_put_failure;
6717 rcu_read_lock();
6718 /* indicate whether we have probe response data or not */
6719 if (rcu_access_pointer(res->proberesp_ies) &&
6720 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
6721 goto fail_unlock_rcu;
6723 /* this pointer prefers to be pointed to probe response data
6724 * but is always valid
6726 ies = rcu_dereference(res->ies);
6727 if (ies) {
6728 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6729 goto fail_unlock_rcu;
6730 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6731 ies->len, ies->data))
6732 goto fail_unlock_rcu;
6735 /* and this pointer is always (unless driver didn't know) beacon data */
6736 ies = rcu_dereference(res->beacon_ies);
6737 if (ies && ies->from_beacon) {
6738 if (nla_put_u64(msg, NL80211_BSS_BEACON_TSF, ies->tsf))
6739 goto fail_unlock_rcu;
6740 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6741 ies->len, ies->data))
6742 goto fail_unlock_rcu;
6744 rcu_read_unlock();
6746 if (res->beacon_interval &&
6747 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6748 goto nla_put_failure;
6749 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6750 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
6751 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
6752 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6753 jiffies_to_msecs(jiffies - intbss->ts)))
6754 goto nla_put_failure;
6756 if (intbss->ts_boottime &&
6757 nla_put_u64(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
6758 intbss->ts_boottime))
6759 goto nla_put_failure;
6761 switch (rdev->wiphy.signal_type) {
6762 case CFG80211_SIGNAL_TYPE_MBM:
6763 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6764 goto nla_put_failure;
6765 break;
6766 case CFG80211_SIGNAL_TYPE_UNSPEC:
6767 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6768 goto nla_put_failure;
6769 break;
6770 default:
6771 break;
6774 switch (wdev->iftype) {
6775 case NL80211_IFTYPE_P2P_CLIENT:
6776 case NL80211_IFTYPE_STATION:
6777 if (intbss == wdev->current_bss &&
6778 nla_put_u32(msg, NL80211_BSS_STATUS,
6779 NL80211_BSS_STATUS_ASSOCIATED))
6780 goto nla_put_failure;
6781 break;
6782 case NL80211_IFTYPE_ADHOC:
6783 if (intbss == wdev->current_bss &&
6784 nla_put_u32(msg, NL80211_BSS_STATUS,
6785 NL80211_BSS_STATUS_IBSS_JOINED))
6786 goto nla_put_failure;
6787 break;
6788 default:
6789 break;
6792 nla_nest_end(msg, bss);
6794 genlmsg_end(msg, hdr);
6795 return 0;
6797 fail_unlock_rcu:
6798 rcu_read_unlock();
6799 nla_put_failure:
6800 genlmsg_cancel(msg, hdr);
6801 return -EMSGSIZE;
6804 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
6806 struct cfg80211_registered_device *rdev;
6807 struct cfg80211_internal_bss *scan;
6808 struct wireless_dev *wdev;
6809 int start = cb->args[2], idx = 0;
6810 int err;
6812 rtnl_lock();
6813 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6814 if (err) {
6815 rtnl_unlock();
6816 return err;
6819 wdev_lock(wdev);
6820 spin_lock_bh(&rdev->bss_lock);
6821 cfg80211_bss_expire(rdev);
6823 cb->seq = rdev->bss_generation;
6825 list_for_each_entry(scan, &rdev->bss_list, list) {
6826 if (++idx <= start)
6827 continue;
6828 if (nl80211_send_bss(skb, cb,
6829 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6830 rdev, wdev, scan) < 0) {
6831 idx--;
6832 break;
6836 spin_unlock_bh(&rdev->bss_lock);
6837 wdev_unlock(wdev);
6839 cb->args[2] = idx;
6840 rtnl_unlock();
6842 return skb->len;
6845 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
6846 int flags, struct net_device *dev,
6847 bool allow_radio_stats,
6848 struct survey_info *survey)
6850 void *hdr;
6851 struct nlattr *infoattr;
6853 /* skip radio stats if userspace didn't request them */
6854 if (!survey->channel && !allow_radio_stats)
6855 return 0;
6857 hdr = nl80211hdr_put(msg, portid, seq, flags,
6858 NL80211_CMD_NEW_SURVEY_RESULTS);
6859 if (!hdr)
6860 return -ENOMEM;
6862 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6863 goto nla_put_failure;
6865 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6866 if (!infoattr)
6867 goto nla_put_failure;
6869 if (survey->channel &&
6870 nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6871 survey->channel->center_freq))
6872 goto nla_put_failure;
6874 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6875 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6876 goto nla_put_failure;
6877 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6878 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6879 goto nla_put_failure;
6880 if ((survey->filled & SURVEY_INFO_TIME) &&
6881 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME,
6882 survey->time))
6883 goto nla_put_failure;
6884 if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
6885 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_BUSY,
6886 survey->time_busy))
6887 goto nla_put_failure;
6888 if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
6889 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
6890 survey->time_ext_busy))
6891 goto nla_put_failure;
6892 if ((survey->filled & SURVEY_INFO_TIME_RX) &&
6893 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_RX,
6894 survey->time_rx))
6895 goto nla_put_failure;
6896 if ((survey->filled & SURVEY_INFO_TIME_TX) &&
6897 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_TX,
6898 survey->time_tx))
6899 goto nla_put_failure;
6900 if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
6901 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_SCAN,
6902 survey->time_scan))
6903 goto nla_put_failure;
6905 nla_nest_end(msg, infoattr);
6907 genlmsg_end(msg, hdr);
6908 return 0;
6910 nla_put_failure:
6911 genlmsg_cancel(msg, hdr);
6912 return -EMSGSIZE;
6915 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
6917 struct survey_info survey;
6918 struct cfg80211_registered_device *rdev;
6919 struct wireless_dev *wdev;
6920 int survey_idx = cb->args[2];
6921 int res;
6922 bool radio_stats;
6924 rtnl_lock();
6925 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6926 if (res)
6927 goto out_err;
6929 /* prepare_wdev_dump parsed the attributes */
6930 radio_stats = nl80211_fam.attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
6932 if (!wdev->netdev) {
6933 res = -EINVAL;
6934 goto out_err;
6937 if (!rdev->ops->dump_survey) {
6938 res = -EOPNOTSUPP;
6939 goto out_err;
6942 while (1) {
6943 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
6944 if (res == -ENOENT)
6945 break;
6946 if (res)
6947 goto out_err;
6949 /* don't send disabled channels, but do send non-channel data */
6950 if (survey.channel &&
6951 survey.channel->flags & IEEE80211_CHAN_DISABLED) {
6952 survey_idx++;
6953 continue;
6956 if (nl80211_send_survey(skb,
6957 NETLINK_CB(cb->skb).portid,
6958 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6959 wdev->netdev, radio_stats, &survey) < 0)
6960 goto out;
6961 survey_idx++;
6964 out:
6965 cb->args[2] = survey_idx;
6966 res = skb->len;
6967 out_err:
6968 rtnl_unlock();
6969 return res;
6972 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6974 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6975 NL80211_WPA_VERSION_2));
6978 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6980 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6981 struct net_device *dev = info->user_ptr[1];
6982 struct ieee80211_channel *chan;
6983 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6984 int err, ssid_len, ie_len = 0, sae_data_len = 0;
6985 enum nl80211_auth_type auth_type;
6986 struct key_parse key;
6987 bool local_state_change;
6989 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6990 return -EINVAL;
6992 if (!info->attrs[NL80211_ATTR_MAC])
6993 return -EINVAL;
6995 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6996 return -EINVAL;
6998 if (!info->attrs[NL80211_ATTR_SSID])
6999 return -EINVAL;
7001 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7002 return -EINVAL;
7004 err = nl80211_parse_key(info, &key);
7005 if (err)
7006 return err;
7008 if (key.idx >= 0) {
7009 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
7010 return -EINVAL;
7011 if (!key.p.key || !key.p.key_len)
7012 return -EINVAL;
7013 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
7014 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
7015 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
7016 key.p.key_len != WLAN_KEY_LEN_WEP104))
7017 return -EINVAL;
7018 if (key.idx > 4)
7019 return -EINVAL;
7020 } else {
7021 key.p.key_len = 0;
7022 key.p.key = NULL;
7025 if (key.idx >= 0) {
7026 int i;
7027 bool ok = false;
7028 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
7029 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
7030 ok = true;
7031 break;
7034 if (!ok)
7035 return -EINVAL;
7038 if (!rdev->ops->auth)
7039 return -EOPNOTSUPP;
7041 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7042 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7043 return -EOPNOTSUPP;
7045 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7046 chan = nl80211_get_valid_chan(&rdev->wiphy,
7047 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7048 if (!chan)
7049 return -EINVAL;
7051 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7052 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7054 if (info->attrs[NL80211_ATTR_IE]) {
7055 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7056 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7059 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
7060 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
7061 return -EINVAL;
7063 if (auth_type == NL80211_AUTHTYPE_SAE &&
7064 !info->attrs[NL80211_ATTR_SAE_DATA])
7065 return -EINVAL;
7067 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
7068 if (auth_type != NL80211_AUTHTYPE_SAE)
7069 return -EINVAL;
7070 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
7071 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
7072 /* need to include at least Auth Transaction and Status Code */
7073 if (sae_data_len < 4)
7074 return -EINVAL;
7077 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7080 * Since we no longer track auth state, ignore
7081 * requests to only change local state.
7083 if (local_state_change)
7084 return 0;
7086 wdev_lock(dev->ieee80211_ptr);
7087 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
7088 ssid, ssid_len, ie, ie_len,
7089 key.p.key, key.p.key_len, key.idx,
7090 sae_data, sae_data_len);
7091 wdev_unlock(dev->ieee80211_ptr);
7092 return err;
7095 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
7096 struct genl_info *info,
7097 struct cfg80211_crypto_settings *settings,
7098 int cipher_limit)
7100 memset(settings, 0, sizeof(*settings));
7102 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
7104 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
7105 u16 proto;
7106 proto = nla_get_u16(
7107 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
7108 settings->control_port_ethertype = cpu_to_be16(proto);
7109 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
7110 proto != ETH_P_PAE)
7111 return -EINVAL;
7112 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
7113 settings->control_port_no_encrypt = true;
7114 } else
7115 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
7117 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
7118 void *data;
7119 int len, i;
7121 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
7122 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
7123 settings->n_ciphers_pairwise = len / sizeof(u32);
7125 if (len % sizeof(u32))
7126 return -EINVAL;
7128 if (settings->n_ciphers_pairwise > cipher_limit)
7129 return -EINVAL;
7131 memcpy(settings->ciphers_pairwise, data, len);
7133 for (i = 0; i < settings->n_ciphers_pairwise; i++)
7134 if (!cfg80211_supported_cipher_suite(
7135 &rdev->wiphy,
7136 settings->ciphers_pairwise[i]))
7137 return -EINVAL;
7140 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
7141 settings->cipher_group =
7142 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
7143 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
7144 settings->cipher_group))
7145 return -EINVAL;
7148 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
7149 settings->wpa_versions =
7150 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
7151 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
7152 return -EINVAL;
7155 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
7156 void *data;
7157 int len;
7159 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
7160 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
7161 settings->n_akm_suites = len / sizeof(u32);
7163 if (len % sizeof(u32))
7164 return -EINVAL;
7166 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
7167 return -EINVAL;
7169 memcpy(settings->akm_suites, data, len);
7172 return 0;
7175 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
7177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7178 struct net_device *dev = info->user_ptr[1];
7179 struct ieee80211_channel *chan;
7180 struct cfg80211_assoc_request req = {};
7181 const u8 *bssid, *ssid;
7182 int err, ssid_len = 0;
7184 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7185 return -EINVAL;
7187 if (!info->attrs[NL80211_ATTR_MAC] ||
7188 !info->attrs[NL80211_ATTR_SSID] ||
7189 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
7190 return -EINVAL;
7192 if (!rdev->ops->assoc)
7193 return -EOPNOTSUPP;
7195 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7196 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7197 return -EOPNOTSUPP;
7199 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7201 chan = nl80211_get_valid_chan(&rdev->wiphy,
7202 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7203 if (!chan)
7204 return -EINVAL;
7206 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7207 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7209 if (info->attrs[NL80211_ATTR_IE]) {
7210 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7211 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7214 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7215 enum nl80211_mfp mfp =
7216 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7217 if (mfp == NL80211_MFP_REQUIRED)
7218 req.use_mfp = true;
7219 else if (mfp != NL80211_MFP_NO)
7220 return -EINVAL;
7223 if (info->attrs[NL80211_ATTR_PREV_BSSID])
7224 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
7226 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7227 req.flags |= ASSOC_REQ_DISABLE_HT;
7229 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7230 memcpy(&req.ht_capa_mask,
7231 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7232 sizeof(req.ht_capa_mask));
7234 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7235 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7236 return -EINVAL;
7237 memcpy(&req.ht_capa,
7238 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7239 sizeof(req.ht_capa));
7242 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7243 req.flags |= ASSOC_REQ_DISABLE_VHT;
7245 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7246 memcpy(&req.vht_capa_mask,
7247 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7248 sizeof(req.vht_capa_mask));
7250 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7251 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7252 return -EINVAL;
7253 memcpy(&req.vht_capa,
7254 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7255 sizeof(req.vht_capa));
7258 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7259 if (!(rdev->wiphy.features &
7260 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7261 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
7262 return -EINVAL;
7263 req.flags |= ASSOC_REQ_USE_RRM;
7266 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
7267 if (!err) {
7268 wdev_lock(dev->ieee80211_ptr);
7269 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
7270 ssid, ssid_len, &req);
7271 wdev_unlock(dev->ieee80211_ptr);
7274 return err;
7277 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
7279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7280 struct net_device *dev = info->user_ptr[1];
7281 const u8 *ie = NULL, *bssid;
7282 int ie_len = 0, err;
7283 u16 reason_code;
7284 bool local_state_change;
7286 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7287 return -EINVAL;
7289 if (!info->attrs[NL80211_ATTR_MAC])
7290 return -EINVAL;
7292 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7293 return -EINVAL;
7295 if (!rdev->ops->deauth)
7296 return -EOPNOTSUPP;
7298 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7299 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7300 return -EOPNOTSUPP;
7302 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7304 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7305 if (reason_code == 0) {
7306 /* Reason Code 0 is reserved */
7307 return -EINVAL;
7310 if (info->attrs[NL80211_ATTR_IE]) {
7311 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7312 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7315 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7317 wdev_lock(dev->ieee80211_ptr);
7318 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
7319 local_state_change);
7320 wdev_unlock(dev->ieee80211_ptr);
7321 return err;
7324 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
7326 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7327 struct net_device *dev = info->user_ptr[1];
7328 const u8 *ie = NULL, *bssid;
7329 int ie_len = 0, err;
7330 u16 reason_code;
7331 bool local_state_change;
7333 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7334 return -EINVAL;
7336 if (!info->attrs[NL80211_ATTR_MAC])
7337 return -EINVAL;
7339 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7340 return -EINVAL;
7342 if (!rdev->ops->disassoc)
7343 return -EOPNOTSUPP;
7345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7346 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7347 return -EOPNOTSUPP;
7349 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7351 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7352 if (reason_code == 0) {
7353 /* Reason Code 0 is reserved */
7354 return -EINVAL;
7357 if (info->attrs[NL80211_ATTR_IE]) {
7358 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7359 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7362 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7364 wdev_lock(dev->ieee80211_ptr);
7365 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
7366 local_state_change);
7367 wdev_unlock(dev->ieee80211_ptr);
7368 return err;
7371 static bool
7372 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
7373 int mcast_rate[IEEE80211_NUM_BANDS],
7374 int rateval)
7376 struct wiphy *wiphy = &rdev->wiphy;
7377 bool found = false;
7378 int band, i;
7380 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
7381 struct ieee80211_supported_band *sband;
7383 sband = wiphy->bands[band];
7384 if (!sband)
7385 continue;
7387 for (i = 0; i < sband->n_bitrates; i++) {
7388 if (sband->bitrates[i].bitrate == rateval) {
7389 mcast_rate[band] = i + 1;
7390 found = true;
7391 break;
7396 return found;
7399 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
7401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7402 struct net_device *dev = info->user_ptr[1];
7403 struct cfg80211_ibss_params ibss;
7404 struct wiphy *wiphy;
7405 struct cfg80211_cached_keys *connkeys = NULL;
7406 int err;
7408 memset(&ibss, 0, sizeof(ibss));
7410 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7411 return -EINVAL;
7413 if (!info->attrs[NL80211_ATTR_SSID] ||
7414 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7415 return -EINVAL;
7417 ibss.beacon_interval = 100;
7419 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7420 ibss.beacon_interval =
7421 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7422 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
7423 return -EINVAL;
7426 if (!rdev->ops->join_ibss)
7427 return -EOPNOTSUPP;
7429 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7430 return -EOPNOTSUPP;
7432 wiphy = &rdev->wiphy;
7434 if (info->attrs[NL80211_ATTR_MAC]) {
7435 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7437 if (!is_valid_ether_addr(ibss.bssid))
7438 return -EINVAL;
7440 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7441 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7443 if (info->attrs[NL80211_ATTR_IE]) {
7444 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7445 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7448 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
7449 if (err)
7450 return err;
7452 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
7453 NL80211_IFTYPE_ADHOC))
7454 return -EINVAL;
7456 switch (ibss.chandef.width) {
7457 case NL80211_CHAN_WIDTH_5:
7458 case NL80211_CHAN_WIDTH_10:
7459 case NL80211_CHAN_WIDTH_20_NOHT:
7460 break;
7461 case NL80211_CHAN_WIDTH_20:
7462 case NL80211_CHAN_WIDTH_40:
7463 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7464 return -EINVAL;
7465 break;
7466 case NL80211_CHAN_WIDTH_80:
7467 case NL80211_CHAN_WIDTH_80P80:
7468 case NL80211_CHAN_WIDTH_160:
7469 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7470 return -EINVAL;
7471 if (!wiphy_ext_feature_isset(&rdev->wiphy,
7472 NL80211_EXT_FEATURE_VHT_IBSS))
7473 return -EINVAL;
7474 break;
7475 default:
7476 return -EINVAL;
7479 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
7480 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
7482 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7483 u8 *rates =
7484 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7485 int n_rates =
7486 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7487 struct ieee80211_supported_band *sband =
7488 wiphy->bands[ibss.chandef.chan->band];
7490 err = ieee80211_get_ratemask(sband, rates, n_rates,
7491 &ibss.basic_rates);
7492 if (err)
7493 return err;
7496 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7497 memcpy(&ibss.ht_capa_mask,
7498 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7499 sizeof(ibss.ht_capa_mask));
7501 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7502 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7503 return -EINVAL;
7504 memcpy(&ibss.ht_capa,
7505 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7506 sizeof(ibss.ht_capa));
7509 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7510 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
7511 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7512 return -EINVAL;
7514 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7515 bool no_ht = false;
7517 connkeys = nl80211_parse_connkeys(rdev,
7518 info->attrs[NL80211_ATTR_KEYS],
7519 &no_ht);
7520 if (IS_ERR(connkeys))
7521 return PTR_ERR(connkeys);
7523 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
7524 no_ht) {
7525 kfree(connkeys);
7526 return -EINVAL;
7530 ibss.control_port =
7531 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
7533 ibss.userspace_handles_dfs =
7534 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
7536 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
7537 if (err)
7538 kzfree(connkeys);
7539 return err;
7542 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
7544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7545 struct net_device *dev = info->user_ptr[1];
7547 if (!rdev->ops->leave_ibss)
7548 return -EOPNOTSUPP;
7550 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7551 return -EOPNOTSUPP;
7553 return cfg80211_leave_ibss(rdev, dev, false);
7556 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
7558 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7559 struct net_device *dev = info->user_ptr[1];
7560 int mcast_rate[IEEE80211_NUM_BANDS];
7561 u32 nla_rate;
7562 int err;
7564 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
7565 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
7566 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
7567 return -EOPNOTSUPP;
7569 if (!rdev->ops->set_mcast_rate)
7570 return -EOPNOTSUPP;
7572 memset(mcast_rate, 0, sizeof(mcast_rate));
7574 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
7575 return -EINVAL;
7577 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
7578 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
7579 return -EINVAL;
7581 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
7583 return err;
7586 static struct sk_buff *
7587 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7588 struct wireless_dev *wdev, int approxlen,
7589 u32 portid, u32 seq, enum nl80211_commands cmd,
7590 enum nl80211_attrs attr,
7591 const struct nl80211_vendor_cmd_info *info,
7592 gfp_t gfp)
7594 struct sk_buff *skb;
7595 void *hdr;
7596 struct nlattr *data;
7598 skb = nlmsg_new(approxlen + 100, gfp);
7599 if (!skb)
7600 return NULL;
7602 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
7603 if (!hdr) {
7604 kfree_skb(skb);
7605 return NULL;
7608 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
7609 goto nla_put_failure;
7611 if (info) {
7612 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
7613 info->vendor_id))
7614 goto nla_put_failure;
7615 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
7616 info->subcmd))
7617 goto nla_put_failure;
7620 if (wdev) {
7621 if (nla_put_u64(skb, NL80211_ATTR_WDEV,
7622 wdev_id(wdev)))
7623 goto nla_put_failure;
7624 if (wdev->netdev &&
7625 nla_put_u32(skb, NL80211_ATTR_IFINDEX,
7626 wdev->netdev->ifindex))
7627 goto nla_put_failure;
7630 data = nla_nest_start(skb, attr);
7632 ((void **)skb->cb)[0] = rdev;
7633 ((void **)skb->cb)[1] = hdr;
7634 ((void **)skb->cb)[2] = data;
7636 return skb;
7638 nla_put_failure:
7639 kfree_skb(skb);
7640 return NULL;
7643 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7644 struct wireless_dev *wdev,
7645 enum nl80211_commands cmd,
7646 enum nl80211_attrs attr,
7647 int vendor_event_idx,
7648 int approxlen, gfp_t gfp)
7650 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
7651 const struct nl80211_vendor_cmd_info *info;
7653 switch (cmd) {
7654 case NL80211_CMD_TESTMODE:
7655 if (WARN_ON(vendor_event_idx != -1))
7656 return NULL;
7657 info = NULL;
7658 break;
7659 case NL80211_CMD_VENDOR:
7660 if (WARN_ON(vendor_event_idx < 0 ||
7661 vendor_event_idx >= wiphy->n_vendor_events))
7662 return NULL;
7663 info = &wiphy->vendor_events[vendor_event_idx];
7664 break;
7665 default:
7666 WARN_ON(1);
7667 return NULL;
7670 return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
7671 cmd, attr, info, gfp);
7673 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
7675 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
7677 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
7678 void *hdr = ((void **)skb->cb)[1];
7679 struct nlattr *data = ((void **)skb->cb)[2];
7680 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
7682 /* clear CB data for netlink core to own from now on */
7683 memset(skb->cb, 0, sizeof(skb->cb));
7685 nla_nest_end(skb, data);
7686 genlmsg_end(skb, hdr);
7688 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
7689 mcgrp = NL80211_MCGRP_VENDOR;
7691 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
7692 mcgrp, gfp);
7694 EXPORT_SYMBOL(__cfg80211_send_event_skb);
7696 #ifdef CONFIG_NL80211_TESTMODE
7697 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
7699 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7700 struct wireless_dev *wdev =
7701 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
7702 int err;
7704 if (!rdev->ops->testmode_cmd)
7705 return -EOPNOTSUPP;
7707 if (IS_ERR(wdev)) {
7708 err = PTR_ERR(wdev);
7709 if (err != -EINVAL)
7710 return err;
7711 wdev = NULL;
7712 } else if (wdev->wiphy != &rdev->wiphy) {
7713 return -EINVAL;
7716 if (!info->attrs[NL80211_ATTR_TESTDATA])
7717 return -EINVAL;
7719 rdev->cur_cmd_info = info;
7720 err = rdev_testmode_cmd(rdev, wdev,
7721 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
7722 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
7723 rdev->cur_cmd_info = NULL;
7725 return err;
7728 static int nl80211_testmode_dump(struct sk_buff *skb,
7729 struct netlink_callback *cb)
7731 struct cfg80211_registered_device *rdev;
7732 int err;
7733 long phy_idx;
7734 void *data = NULL;
7735 int data_len = 0;
7737 rtnl_lock();
7739 if (cb->args[0]) {
7741 * 0 is a valid index, but not valid for args[0],
7742 * so we need to offset by 1.
7744 phy_idx = cb->args[0] - 1;
7745 } else {
7746 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
7747 nl80211_fam.attrbuf, nl80211_fam.maxattr,
7748 nl80211_policy);
7749 if (err)
7750 goto out_err;
7752 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
7753 nl80211_fam.attrbuf);
7754 if (IS_ERR(rdev)) {
7755 err = PTR_ERR(rdev);
7756 goto out_err;
7758 phy_idx = rdev->wiphy_idx;
7759 rdev = NULL;
7761 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7762 cb->args[1] =
7763 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7766 if (cb->args[1]) {
7767 data = nla_data((void *)cb->args[1]);
7768 data_len = nla_len((void *)cb->args[1]);
7771 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7772 if (!rdev) {
7773 err = -ENOENT;
7774 goto out_err;
7777 if (!rdev->ops->testmode_dump) {
7778 err = -EOPNOTSUPP;
7779 goto out_err;
7782 while (1) {
7783 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
7784 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7785 NL80211_CMD_TESTMODE);
7786 struct nlattr *tmdata;
7788 if (!hdr)
7789 break;
7791 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
7792 genlmsg_cancel(skb, hdr);
7793 break;
7796 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7797 if (!tmdata) {
7798 genlmsg_cancel(skb, hdr);
7799 break;
7801 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
7802 nla_nest_end(skb, tmdata);
7804 if (err == -ENOBUFS || err == -ENOENT) {
7805 genlmsg_cancel(skb, hdr);
7806 break;
7807 } else if (err) {
7808 genlmsg_cancel(skb, hdr);
7809 goto out_err;
7812 genlmsg_end(skb, hdr);
7815 err = skb->len;
7816 /* see above */
7817 cb->args[0] = phy_idx + 1;
7818 out_err:
7819 rtnl_unlock();
7820 return err;
7822 #endif
7824 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7827 struct net_device *dev = info->user_ptr[1];
7828 struct cfg80211_connect_params connect;
7829 struct wiphy *wiphy;
7830 struct cfg80211_cached_keys *connkeys = NULL;
7831 int err;
7833 memset(&connect, 0, sizeof(connect));
7835 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7836 return -EINVAL;
7838 if (!info->attrs[NL80211_ATTR_SSID] ||
7839 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7840 return -EINVAL;
7842 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7843 connect.auth_type =
7844 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
7845 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7846 NL80211_CMD_CONNECT))
7847 return -EINVAL;
7848 } else
7849 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7851 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7853 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
7854 NL80211_MAX_NR_CIPHER_SUITES);
7855 if (err)
7856 return err;
7858 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7859 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7860 return -EOPNOTSUPP;
7862 wiphy = &rdev->wiphy;
7864 connect.bg_scan_period = -1;
7865 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7866 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7867 connect.bg_scan_period =
7868 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7871 if (info->attrs[NL80211_ATTR_MAC])
7872 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7873 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7874 connect.bssid_hint =
7875 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
7876 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7877 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7879 if (info->attrs[NL80211_ATTR_IE]) {
7880 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7881 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7884 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7885 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7886 if (connect.mfp != NL80211_MFP_REQUIRED &&
7887 connect.mfp != NL80211_MFP_NO)
7888 return -EINVAL;
7889 } else {
7890 connect.mfp = NL80211_MFP_NO;
7893 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7894 connect.channel = nl80211_get_valid_chan(
7895 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7896 if (!connect.channel)
7897 return -EINVAL;
7898 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
7899 connect.channel_hint = nl80211_get_valid_chan(
7900 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7901 if (!connect.channel_hint)
7902 return -EINVAL;
7905 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7906 connkeys = nl80211_parse_connkeys(rdev,
7907 info->attrs[NL80211_ATTR_KEYS], NULL);
7908 if (IS_ERR(connkeys))
7909 return PTR_ERR(connkeys);
7912 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7913 connect.flags |= ASSOC_REQ_DISABLE_HT;
7915 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7916 memcpy(&connect.ht_capa_mask,
7917 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7918 sizeof(connect.ht_capa_mask));
7920 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7921 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7922 kzfree(connkeys);
7923 return -EINVAL;
7925 memcpy(&connect.ht_capa,
7926 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7927 sizeof(connect.ht_capa));
7930 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7931 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7933 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7934 memcpy(&connect.vht_capa_mask,
7935 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7936 sizeof(connect.vht_capa_mask));
7938 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7939 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7940 kzfree(connkeys);
7941 return -EINVAL;
7943 memcpy(&connect.vht_capa,
7944 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7945 sizeof(connect.vht_capa));
7948 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7949 if (!(rdev->wiphy.features &
7950 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7951 !(rdev->wiphy.features & NL80211_FEATURE_QUIET)) {
7952 kzfree(connkeys);
7953 return -EINVAL;
7955 connect.flags |= ASSOC_REQ_USE_RRM;
7958 wdev_lock(dev->ieee80211_ptr);
7959 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7960 wdev_unlock(dev->ieee80211_ptr);
7961 if (err)
7962 kzfree(connkeys);
7963 return err;
7966 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7969 struct net_device *dev = info->user_ptr[1];
7970 u16 reason;
7971 int ret;
7973 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7974 reason = WLAN_REASON_DEAUTH_LEAVING;
7975 else
7976 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7978 if (reason == 0)
7979 return -EINVAL;
7981 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7982 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7983 return -EOPNOTSUPP;
7985 wdev_lock(dev->ieee80211_ptr);
7986 ret = cfg80211_disconnect(rdev, dev, reason, true);
7987 wdev_unlock(dev->ieee80211_ptr);
7988 return ret;
7991 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7994 struct net *net;
7995 int err;
7997 if (info->attrs[NL80211_ATTR_PID]) {
7998 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
8000 net = get_net_ns_by_pid(pid);
8001 } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
8002 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
8004 net = get_net_ns_by_fd(fd);
8005 } else {
8006 return -EINVAL;
8009 if (IS_ERR(net))
8010 return PTR_ERR(net);
8012 err = 0;
8014 /* check if anything to do */
8015 if (!net_eq(wiphy_net(&rdev->wiphy), net))
8016 err = cfg80211_switch_netns(rdev, net);
8018 put_net(net);
8019 return err;
8022 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
8024 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8025 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
8026 struct cfg80211_pmksa *pmksa) = NULL;
8027 struct net_device *dev = info->user_ptr[1];
8028 struct cfg80211_pmksa pmksa;
8030 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
8032 if (!info->attrs[NL80211_ATTR_MAC])
8033 return -EINVAL;
8035 if (!info->attrs[NL80211_ATTR_PMKID])
8036 return -EINVAL;
8038 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
8039 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8041 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8042 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8043 return -EOPNOTSUPP;
8045 switch (info->genlhdr->cmd) {
8046 case NL80211_CMD_SET_PMKSA:
8047 rdev_ops = rdev->ops->set_pmksa;
8048 break;
8049 case NL80211_CMD_DEL_PMKSA:
8050 rdev_ops = rdev->ops->del_pmksa;
8051 break;
8052 default:
8053 WARN_ON(1);
8054 break;
8057 if (!rdev_ops)
8058 return -EOPNOTSUPP;
8060 return rdev_ops(&rdev->wiphy, dev, &pmksa);
8063 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
8065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8066 struct net_device *dev = info->user_ptr[1];
8068 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8069 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8070 return -EOPNOTSUPP;
8072 if (!rdev->ops->flush_pmksa)
8073 return -EOPNOTSUPP;
8075 return rdev_flush_pmksa(rdev, dev);
8078 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
8080 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8081 struct net_device *dev = info->user_ptr[1];
8082 u8 action_code, dialog_token;
8083 u32 peer_capability = 0;
8084 u16 status_code;
8085 u8 *peer;
8086 bool initiator;
8088 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
8089 !rdev->ops->tdls_mgmt)
8090 return -EOPNOTSUPP;
8092 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
8093 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
8094 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
8095 !info->attrs[NL80211_ATTR_IE] ||
8096 !info->attrs[NL80211_ATTR_MAC])
8097 return -EINVAL;
8099 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
8100 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
8101 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
8102 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
8103 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
8104 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
8105 peer_capability =
8106 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
8108 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
8109 dialog_token, status_code, peer_capability,
8110 initiator,
8111 nla_data(info->attrs[NL80211_ATTR_IE]),
8112 nla_len(info->attrs[NL80211_ATTR_IE]));
8115 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
8117 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8118 struct net_device *dev = info->user_ptr[1];
8119 enum nl80211_tdls_operation operation;
8120 u8 *peer;
8122 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
8123 !rdev->ops->tdls_oper)
8124 return -EOPNOTSUPP;
8126 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
8127 !info->attrs[NL80211_ATTR_MAC])
8128 return -EINVAL;
8130 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
8131 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
8133 return rdev_tdls_oper(rdev, dev, peer, operation);
8136 static int nl80211_remain_on_channel(struct sk_buff *skb,
8137 struct genl_info *info)
8139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8140 struct wireless_dev *wdev = info->user_ptr[1];
8141 struct cfg80211_chan_def chandef;
8142 struct sk_buff *msg;
8143 void *hdr;
8144 u64 cookie;
8145 u32 duration;
8146 int err;
8148 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8149 !info->attrs[NL80211_ATTR_DURATION])
8150 return -EINVAL;
8152 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
8154 if (!rdev->ops->remain_on_channel ||
8155 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
8156 return -EOPNOTSUPP;
8159 * We should be on that channel for at least a minimum amount of
8160 * time (10ms) but no longer than the driver supports.
8162 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8163 duration > rdev->wiphy.max_remain_on_channel_duration)
8164 return -EINVAL;
8166 err = nl80211_parse_chandef(rdev, info, &chandef);
8167 if (err)
8168 return err;
8170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8171 if (!msg)
8172 return -ENOMEM;
8174 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8175 NL80211_CMD_REMAIN_ON_CHANNEL);
8176 if (!hdr) {
8177 err = -ENOBUFS;
8178 goto free_msg;
8181 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
8182 duration, &cookie);
8184 if (err)
8185 goto free_msg;
8187 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8188 goto nla_put_failure;
8190 genlmsg_end(msg, hdr);
8192 return genlmsg_reply(msg, info);
8194 nla_put_failure:
8195 err = -ENOBUFS;
8196 free_msg:
8197 nlmsg_free(msg);
8198 return err;
8201 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
8202 struct genl_info *info)
8204 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8205 struct wireless_dev *wdev = info->user_ptr[1];
8206 u64 cookie;
8208 if (!info->attrs[NL80211_ATTR_COOKIE])
8209 return -EINVAL;
8211 if (!rdev->ops->cancel_remain_on_channel)
8212 return -EOPNOTSUPP;
8214 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8216 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
8219 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
8220 u8 *rates, u8 rates_len)
8222 u8 i;
8223 u32 mask = 0;
8225 for (i = 0; i < rates_len; i++) {
8226 int rate = (rates[i] & 0x7f) * 5;
8227 int ridx;
8228 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
8229 struct ieee80211_rate *srate =
8230 &sband->bitrates[ridx];
8231 if (rate == srate->bitrate) {
8232 mask |= 1 << ridx;
8233 break;
8236 if (ridx == sband->n_bitrates)
8237 return 0; /* rate not found */
8240 return mask;
8243 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
8244 u8 *rates, u8 rates_len,
8245 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
8247 u8 i;
8249 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
8251 for (i = 0; i < rates_len; i++) {
8252 int ridx, rbit;
8254 ridx = rates[i] / 8;
8255 rbit = BIT(rates[i] % 8);
8257 /* check validity */
8258 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
8259 return false;
8261 /* check availability */
8262 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
8263 mcs[ridx] |= rbit;
8264 else
8265 return false;
8268 return true;
8271 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
8273 u16 mcs_mask = 0;
8275 switch (vht_mcs_map) {
8276 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
8277 break;
8278 case IEEE80211_VHT_MCS_SUPPORT_0_7:
8279 mcs_mask = 0x00FF;
8280 break;
8281 case IEEE80211_VHT_MCS_SUPPORT_0_8:
8282 mcs_mask = 0x01FF;
8283 break;
8284 case IEEE80211_VHT_MCS_SUPPORT_0_9:
8285 mcs_mask = 0x03FF;
8286 break;
8287 default:
8288 break;
8291 return mcs_mask;
8294 static void vht_build_mcs_mask(u16 vht_mcs_map,
8295 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
8297 u8 nss;
8299 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
8300 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
8301 vht_mcs_map >>= 2;
8305 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
8306 struct nl80211_txrate_vht *txrate,
8307 u16 mcs[NL80211_VHT_NSS_MAX])
8309 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8310 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
8311 u8 i;
8313 if (!sband->vht_cap.vht_supported)
8314 return false;
8316 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
8318 /* Build vht_mcs_mask from VHT capabilities */
8319 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
8321 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8322 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
8323 mcs[i] = txrate->mcs[i];
8324 else
8325 return false;
8328 return true;
8331 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
8332 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
8333 .len = NL80211_MAX_SUPP_RATES },
8334 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
8335 .len = NL80211_MAX_SUPP_HT_RATES },
8336 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
8337 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
8340 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
8341 struct genl_info *info)
8343 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
8344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8345 struct cfg80211_bitrate_mask mask;
8346 int rem, i;
8347 struct net_device *dev = info->user_ptr[1];
8348 struct nlattr *tx_rates;
8349 struct ieee80211_supported_band *sband;
8350 u16 vht_tx_mcs_map;
8352 if (!rdev->ops->set_bitrate_mask)
8353 return -EOPNOTSUPP;
8355 memset(&mask, 0, sizeof(mask));
8356 /* Default to all rates enabled */
8357 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
8358 sband = rdev->wiphy.bands[i];
8360 if (!sband)
8361 continue;
8363 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
8364 memcpy(mask.control[i].ht_mcs,
8365 sband->ht_cap.mcs.rx_mask,
8366 sizeof(mask.control[i].ht_mcs));
8368 if (!sband->vht_cap.vht_supported)
8369 continue;
8371 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8372 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
8375 /* if no rates are given set it back to the defaults */
8376 if (!info->attrs[NL80211_ATTR_TX_RATES])
8377 goto out;
8380 * The nested attribute uses enum nl80211_band as the index. This maps
8381 * directly to the enum ieee80211_band values used in cfg80211.
8383 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
8384 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
8385 enum ieee80211_band band = nla_type(tx_rates);
8386 int err;
8388 if (band < 0 || band >= IEEE80211_NUM_BANDS)
8389 return -EINVAL;
8390 sband = rdev->wiphy.bands[band];
8391 if (sband == NULL)
8392 return -EINVAL;
8393 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
8394 nla_len(tx_rates), nl80211_txattr_policy);
8395 if (err)
8396 return err;
8397 if (tb[NL80211_TXRATE_LEGACY]) {
8398 mask.control[band].legacy = rateset_to_mask(
8399 sband,
8400 nla_data(tb[NL80211_TXRATE_LEGACY]),
8401 nla_len(tb[NL80211_TXRATE_LEGACY]));
8402 if ((mask.control[band].legacy == 0) &&
8403 nla_len(tb[NL80211_TXRATE_LEGACY]))
8404 return -EINVAL;
8406 if (tb[NL80211_TXRATE_HT]) {
8407 if (!ht_rateset_to_mask(
8408 sband,
8409 nla_data(tb[NL80211_TXRATE_HT]),
8410 nla_len(tb[NL80211_TXRATE_HT]),
8411 mask.control[band].ht_mcs))
8412 return -EINVAL;
8414 if (tb[NL80211_TXRATE_VHT]) {
8415 if (!vht_set_mcs_mask(
8416 sband,
8417 nla_data(tb[NL80211_TXRATE_VHT]),
8418 mask.control[band].vht_mcs))
8419 return -EINVAL;
8421 if (tb[NL80211_TXRATE_GI]) {
8422 mask.control[band].gi =
8423 nla_get_u8(tb[NL80211_TXRATE_GI]);
8424 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
8425 return -EINVAL;
8428 if (mask.control[band].legacy == 0) {
8429 /* don't allow empty legacy rates if HT or VHT
8430 * are not even supported.
8432 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
8433 rdev->wiphy.bands[band]->vht_cap.vht_supported))
8434 return -EINVAL;
8436 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
8437 if (mask.control[band].ht_mcs[i])
8438 goto out;
8440 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
8441 if (mask.control[band].vht_mcs[i])
8442 goto out;
8444 /* legacy and mcs rates may not be both empty */
8445 return -EINVAL;
8449 out:
8450 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
8453 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
8455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8456 struct wireless_dev *wdev = info->user_ptr[1];
8457 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
8459 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
8460 return -EINVAL;
8462 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
8463 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
8465 switch (wdev->iftype) {
8466 case NL80211_IFTYPE_STATION:
8467 case NL80211_IFTYPE_ADHOC:
8468 case NL80211_IFTYPE_P2P_CLIENT:
8469 case NL80211_IFTYPE_AP:
8470 case NL80211_IFTYPE_AP_VLAN:
8471 case NL80211_IFTYPE_MESH_POINT:
8472 case NL80211_IFTYPE_P2P_GO:
8473 case NL80211_IFTYPE_P2P_DEVICE:
8474 break;
8475 default:
8476 return -EOPNOTSUPP;
8479 /* not much point in registering if we can't reply */
8480 if (!rdev->ops->mgmt_tx)
8481 return -EOPNOTSUPP;
8483 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
8484 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
8485 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
8488 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
8490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8491 struct wireless_dev *wdev = info->user_ptr[1];
8492 struct cfg80211_chan_def chandef;
8493 int err;
8494 void *hdr = NULL;
8495 u64 cookie;
8496 struct sk_buff *msg = NULL;
8497 struct cfg80211_mgmt_tx_params params = {
8498 .dont_wait_for_ack =
8499 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
8502 if (!info->attrs[NL80211_ATTR_FRAME])
8503 return -EINVAL;
8505 if (!rdev->ops->mgmt_tx)
8506 return -EOPNOTSUPP;
8508 switch (wdev->iftype) {
8509 case NL80211_IFTYPE_P2P_DEVICE:
8510 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8511 return -EINVAL;
8512 case NL80211_IFTYPE_STATION:
8513 case NL80211_IFTYPE_ADHOC:
8514 case NL80211_IFTYPE_P2P_CLIENT:
8515 case NL80211_IFTYPE_AP:
8516 case NL80211_IFTYPE_AP_VLAN:
8517 case NL80211_IFTYPE_MESH_POINT:
8518 case NL80211_IFTYPE_P2P_GO:
8519 break;
8520 default:
8521 return -EOPNOTSUPP;
8524 if (info->attrs[NL80211_ATTR_DURATION]) {
8525 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
8526 return -EINVAL;
8527 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
8530 * We should wait on the channel for at least a minimum amount
8531 * of time (10ms) but no longer than the driver supports.
8533 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8534 params.wait > rdev->wiphy.max_remain_on_channel_duration)
8535 return -EINVAL;
8539 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
8541 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
8542 return -EINVAL;
8544 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
8546 /* get the channel if any has been specified, otherwise pass NULL to
8547 * the driver. The latter will use the current one
8549 chandef.chan = NULL;
8550 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8551 err = nl80211_parse_chandef(rdev, info, &chandef);
8552 if (err)
8553 return err;
8556 if (!chandef.chan && params.offchan)
8557 return -EINVAL;
8559 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
8560 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
8562 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
8563 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8564 int i;
8566 if (len % sizeof(u16))
8567 return -EINVAL;
8569 params.n_csa_offsets = len / sizeof(u16);
8570 params.csa_offsets =
8571 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8573 /* check that all the offsets fit the frame */
8574 for (i = 0; i < params.n_csa_offsets; i++) {
8575 if (params.csa_offsets[i] >= params.len)
8576 return -EINVAL;
8580 if (!params.dont_wait_for_ack) {
8581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8582 if (!msg)
8583 return -ENOMEM;
8585 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8586 NL80211_CMD_FRAME);
8587 if (!hdr) {
8588 err = -ENOBUFS;
8589 goto free_msg;
8593 params.chan = chandef.chan;
8594 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
8595 if (err)
8596 goto free_msg;
8598 if (msg) {
8599 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8600 goto nla_put_failure;
8602 genlmsg_end(msg, hdr);
8603 return genlmsg_reply(msg, info);
8606 return 0;
8608 nla_put_failure:
8609 err = -ENOBUFS;
8610 free_msg:
8611 nlmsg_free(msg);
8612 return err;
8615 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
8617 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8618 struct wireless_dev *wdev = info->user_ptr[1];
8619 u64 cookie;
8621 if (!info->attrs[NL80211_ATTR_COOKIE])
8622 return -EINVAL;
8624 if (!rdev->ops->mgmt_tx_cancel_wait)
8625 return -EOPNOTSUPP;
8627 switch (wdev->iftype) {
8628 case NL80211_IFTYPE_STATION:
8629 case NL80211_IFTYPE_ADHOC:
8630 case NL80211_IFTYPE_P2P_CLIENT:
8631 case NL80211_IFTYPE_AP:
8632 case NL80211_IFTYPE_AP_VLAN:
8633 case NL80211_IFTYPE_P2P_GO:
8634 case NL80211_IFTYPE_P2P_DEVICE:
8635 break;
8636 default:
8637 return -EOPNOTSUPP;
8640 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8642 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
8645 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
8647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8648 struct wireless_dev *wdev;
8649 struct net_device *dev = info->user_ptr[1];
8650 u8 ps_state;
8651 bool state;
8652 int err;
8654 if (!info->attrs[NL80211_ATTR_PS_STATE])
8655 return -EINVAL;
8657 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
8659 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
8660 return -EINVAL;
8662 wdev = dev->ieee80211_ptr;
8664 if (!rdev->ops->set_power_mgmt)
8665 return -EOPNOTSUPP;
8667 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
8669 if (state == wdev->ps)
8670 return 0;
8672 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
8673 if (!err)
8674 wdev->ps = state;
8675 return err;
8678 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
8680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8681 enum nl80211_ps_state ps_state;
8682 struct wireless_dev *wdev;
8683 struct net_device *dev = info->user_ptr[1];
8684 struct sk_buff *msg;
8685 void *hdr;
8686 int err;
8688 wdev = dev->ieee80211_ptr;
8690 if (!rdev->ops->set_power_mgmt)
8691 return -EOPNOTSUPP;
8693 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8694 if (!msg)
8695 return -ENOMEM;
8697 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8698 NL80211_CMD_GET_POWER_SAVE);
8699 if (!hdr) {
8700 err = -ENOBUFS;
8701 goto free_msg;
8704 if (wdev->ps)
8705 ps_state = NL80211_PS_ENABLED;
8706 else
8707 ps_state = NL80211_PS_DISABLED;
8709 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
8710 goto nla_put_failure;
8712 genlmsg_end(msg, hdr);
8713 return genlmsg_reply(msg, info);
8715 nla_put_failure:
8716 err = -ENOBUFS;
8717 free_msg:
8718 nlmsg_free(msg);
8719 return err;
8722 static const struct nla_policy
8723 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
8724 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
8725 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
8726 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
8727 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
8728 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
8729 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
8732 static int nl80211_set_cqm_txe(struct genl_info *info,
8733 u32 rate, u32 pkts, u32 intvl)
8735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8736 struct net_device *dev = info->user_ptr[1];
8737 struct wireless_dev *wdev = dev->ieee80211_ptr;
8739 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
8740 return -EINVAL;
8742 if (!rdev->ops->set_cqm_txe_config)
8743 return -EOPNOTSUPP;
8745 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8746 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8747 return -EOPNOTSUPP;
8749 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
8752 static int nl80211_set_cqm_rssi(struct genl_info *info,
8753 s32 threshold, u32 hysteresis)
8755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8756 struct net_device *dev = info->user_ptr[1];
8757 struct wireless_dev *wdev = dev->ieee80211_ptr;
8759 if (threshold > 0)
8760 return -EINVAL;
8762 /* disabling - hysteresis should also be zero then */
8763 if (threshold == 0)
8764 hysteresis = 0;
8766 if (!rdev->ops->set_cqm_rssi_config)
8767 return -EOPNOTSUPP;
8769 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8770 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8771 return -EOPNOTSUPP;
8773 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
8776 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
8778 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
8779 struct nlattr *cqm;
8780 int err;
8782 cqm = info->attrs[NL80211_ATTR_CQM];
8783 if (!cqm)
8784 return -EINVAL;
8786 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
8787 nl80211_attr_cqm_policy);
8788 if (err)
8789 return err;
8791 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8792 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
8793 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8794 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
8796 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8799 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8800 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8801 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8802 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8803 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8804 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8806 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8809 return -EINVAL;
8812 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
8814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8815 struct net_device *dev = info->user_ptr[1];
8816 struct ocb_setup setup = {};
8817 int err;
8819 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8820 if (err)
8821 return err;
8823 return cfg80211_join_ocb(rdev, dev, &setup);
8826 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
8828 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8829 struct net_device *dev = info->user_ptr[1];
8831 return cfg80211_leave_ocb(rdev, dev);
8834 static int nl80211_join_mesh(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 struct mesh_config cfg;
8839 struct mesh_setup setup;
8840 int err;
8842 /* start with default */
8843 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
8844 memcpy(&setup, &default_mesh_setup, sizeof(setup));
8846 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
8847 /* and parse parameters if given */
8848 err = nl80211_parse_mesh_config(info, &cfg, NULL);
8849 if (err)
8850 return err;
8853 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8854 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8855 return -EINVAL;
8857 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8858 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8860 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8861 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8862 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8863 return -EINVAL;
8865 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8866 setup.beacon_interval =
8867 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8868 if (setup.beacon_interval < 10 ||
8869 setup.beacon_interval > 10000)
8870 return -EINVAL;
8873 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8874 setup.dtim_period =
8875 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8876 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8877 return -EINVAL;
8880 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8881 /* parse additional setup parameters if given */
8882 err = nl80211_parse_mesh_setup(info, &setup);
8883 if (err)
8884 return err;
8887 if (setup.user_mpm)
8888 cfg.auto_open_plinks = false;
8890 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8891 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8892 if (err)
8893 return err;
8894 } else {
8895 /* cfg80211_join_mesh() will sort it out */
8896 setup.chandef.chan = NULL;
8899 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8900 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8901 int n_rates =
8902 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8903 struct ieee80211_supported_band *sband;
8905 if (!setup.chandef.chan)
8906 return -EINVAL;
8908 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8910 err = ieee80211_get_ratemask(sband, rates, n_rates,
8911 &setup.basic_rates);
8912 if (err)
8913 return err;
8916 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
8919 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8922 struct net_device *dev = info->user_ptr[1];
8924 return cfg80211_leave_mesh(rdev, dev);
8927 #ifdef CONFIG_PM
8928 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8929 struct cfg80211_registered_device *rdev)
8931 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
8932 struct nlattr *nl_pats, *nl_pat;
8933 int i, pat_len;
8935 if (!wowlan->n_patterns)
8936 return 0;
8938 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8939 if (!nl_pats)
8940 return -ENOBUFS;
8942 for (i = 0; i < wowlan->n_patterns; i++) {
8943 nl_pat = nla_nest_start(msg, i + 1);
8944 if (!nl_pat)
8945 return -ENOBUFS;
8946 pat_len = wowlan->patterns[i].pattern_len;
8947 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
8948 wowlan->patterns[i].mask) ||
8949 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8950 wowlan->patterns[i].pattern) ||
8951 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8952 wowlan->patterns[i].pkt_offset))
8953 return -ENOBUFS;
8954 nla_nest_end(msg, nl_pat);
8956 nla_nest_end(msg, nl_pats);
8958 return 0;
8961 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8962 struct cfg80211_wowlan_tcp *tcp)
8964 struct nlattr *nl_tcp;
8966 if (!tcp)
8967 return 0;
8969 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8970 if (!nl_tcp)
8971 return -ENOBUFS;
8973 if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8974 nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8975 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8976 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8977 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8978 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8979 tcp->payload_len, tcp->payload) ||
8980 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8981 tcp->data_interval) ||
8982 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8983 tcp->wake_len, tcp->wake_data) ||
8984 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8985 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8986 return -ENOBUFS;
8988 if (tcp->payload_seq.len &&
8989 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8990 sizeof(tcp->payload_seq), &tcp->payload_seq))
8991 return -ENOBUFS;
8993 if (tcp->payload_tok.len &&
8994 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8995 sizeof(tcp->payload_tok) + tcp->tokens_size,
8996 &tcp->payload_tok))
8997 return -ENOBUFS;
8999 nla_nest_end(msg, nl_tcp);
9001 return 0;
9004 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
9005 struct cfg80211_sched_scan_request *req)
9007 struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
9008 int i;
9010 if (!req)
9011 return 0;
9013 nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
9014 if (!nd)
9015 return -ENOBUFS;
9017 if (req->n_scan_plans == 1 &&
9018 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
9019 req->scan_plans[0].interval * 1000))
9020 return -ENOBUFS;
9022 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
9023 return -ENOBUFS;
9025 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9026 if (!freqs)
9027 return -ENOBUFS;
9029 for (i = 0; i < req->n_channels; i++)
9030 nla_put_u32(msg, i, req->channels[i]->center_freq);
9032 nla_nest_end(msg, freqs);
9034 if (req->n_match_sets) {
9035 matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
9036 for (i = 0; i < req->n_match_sets; i++) {
9037 match = nla_nest_start(msg, i);
9038 nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
9039 req->match_sets[i].ssid.ssid_len,
9040 req->match_sets[i].ssid.ssid);
9041 nla_nest_end(msg, match);
9043 nla_nest_end(msg, matches);
9046 scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
9047 if (!scan_plans)
9048 return -ENOBUFS;
9050 for (i = 0; i < req->n_scan_plans; i++) {
9051 scan_plan = nla_nest_start(msg, i + 1);
9052 if (!scan_plan ||
9053 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
9054 req->scan_plans[i].interval) ||
9055 (req->scan_plans[i].iterations &&
9056 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
9057 req->scan_plans[i].iterations)))
9058 return -ENOBUFS;
9059 nla_nest_end(msg, scan_plan);
9061 nla_nest_end(msg, scan_plans);
9063 nla_nest_end(msg, nd);
9065 return 0;
9068 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
9070 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9071 struct sk_buff *msg;
9072 void *hdr;
9073 u32 size = NLMSG_DEFAULT_SIZE;
9075 if (!rdev->wiphy.wowlan)
9076 return -EOPNOTSUPP;
9078 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
9079 /* adjust size to have room for all the data */
9080 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
9081 rdev->wiphy.wowlan_config->tcp->payload_len +
9082 rdev->wiphy.wowlan_config->tcp->wake_len +
9083 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
9086 msg = nlmsg_new(size, GFP_KERNEL);
9087 if (!msg)
9088 return -ENOMEM;
9090 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9091 NL80211_CMD_GET_WOWLAN);
9092 if (!hdr)
9093 goto nla_put_failure;
9095 if (rdev->wiphy.wowlan_config) {
9096 struct nlattr *nl_wowlan;
9098 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9099 if (!nl_wowlan)
9100 goto nla_put_failure;
9102 if ((rdev->wiphy.wowlan_config->any &&
9103 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9104 (rdev->wiphy.wowlan_config->disconnect &&
9105 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9106 (rdev->wiphy.wowlan_config->magic_pkt &&
9107 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9108 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
9109 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9110 (rdev->wiphy.wowlan_config->eap_identity_req &&
9111 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9112 (rdev->wiphy.wowlan_config->four_way_handshake &&
9113 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9114 (rdev->wiphy.wowlan_config->rfkill_release &&
9115 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
9116 goto nla_put_failure;
9118 if (nl80211_send_wowlan_patterns(msg, rdev))
9119 goto nla_put_failure;
9121 if (nl80211_send_wowlan_tcp(msg,
9122 rdev->wiphy.wowlan_config->tcp))
9123 goto nla_put_failure;
9125 if (nl80211_send_wowlan_nd(
9126 msg,
9127 rdev->wiphy.wowlan_config->nd_config))
9128 goto nla_put_failure;
9130 nla_nest_end(msg, nl_wowlan);
9133 genlmsg_end(msg, hdr);
9134 return genlmsg_reply(msg, info);
9136 nla_put_failure:
9137 nlmsg_free(msg);
9138 return -ENOBUFS;
9141 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
9142 struct nlattr *attr,
9143 struct cfg80211_wowlan *trig)
9145 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
9146 struct cfg80211_wowlan_tcp *cfg;
9147 struct nl80211_wowlan_tcp_data_token *tok = NULL;
9148 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
9149 u32 size;
9150 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
9151 int err, port;
9153 if (!rdev->wiphy.wowlan->tcp)
9154 return -EINVAL;
9156 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
9157 nla_data(attr), nla_len(attr),
9158 nl80211_wowlan_tcp_policy);
9159 if (err)
9160 return err;
9162 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
9163 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
9164 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
9165 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
9166 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
9167 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
9168 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
9169 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
9170 return -EINVAL;
9172 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
9173 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
9174 return -EINVAL;
9176 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
9177 rdev->wiphy.wowlan->tcp->data_interval_max ||
9178 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
9179 return -EINVAL;
9181 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
9182 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
9183 return -EINVAL;
9185 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
9186 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
9187 return -EINVAL;
9189 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
9190 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
9192 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
9193 tokens_size = tokln - sizeof(*tok);
9195 if (!tok->len || tokens_size % tok->len)
9196 return -EINVAL;
9197 if (!rdev->wiphy.wowlan->tcp->tok)
9198 return -EINVAL;
9199 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
9200 return -EINVAL;
9201 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
9202 return -EINVAL;
9203 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
9204 return -EINVAL;
9205 if (tok->offset + tok->len > data_size)
9206 return -EINVAL;
9209 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
9210 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
9211 if (!rdev->wiphy.wowlan->tcp->seq)
9212 return -EINVAL;
9213 if (seq->len == 0 || seq->len > 4)
9214 return -EINVAL;
9215 if (seq->len + seq->offset > data_size)
9216 return -EINVAL;
9219 size = sizeof(*cfg);
9220 size += data_size;
9221 size += wake_size + wake_mask_size;
9222 size += tokens_size;
9224 cfg = kzalloc(size, GFP_KERNEL);
9225 if (!cfg)
9226 return -ENOMEM;
9227 cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
9228 cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
9229 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
9230 ETH_ALEN);
9231 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
9232 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
9233 else
9234 port = 0;
9235 #ifdef CONFIG_INET
9236 /* allocate a socket and port for it and use it */
9237 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
9238 IPPROTO_TCP, &cfg->sock, 1);
9239 if (err) {
9240 kfree(cfg);
9241 return err;
9243 if (inet_csk_get_port(cfg->sock->sk, port)) {
9244 sock_release(cfg->sock);
9245 kfree(cfg);
9246 return -EADDRINUSE;
9248 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
9249 #else
9250 if (!port) {
9251 kfree(cfg);
9252 return -EINVAL;
9254 cfg->src_port = port;
9255 #endif
9257 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
9258 cfg->payload_len = data_size;
9259 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
9260 memcpy((void *)cfg->payload,
9261 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
9262 data_size);
9263 if (seq)
9264 cfg->payload_seq = *seq;
9265 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
9266 cfg->wake_len = wake_size;
9267 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
9268 memcpy((void *)cfg->wake_data,
9269 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
9270 wake_size);
9271 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
9272 data_size + wake_size;
9273 memcpy((void *)cfg->wake_mask,
9274 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
9275 wake_mask_size);
9276 if (tok) {
9277 cfg->tokens_size = tokens_size;
9278 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
9281 trig->tcp = cfg;
9283 return 0;
9286 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
9287 const struct wiphy_wowlan_support *wowlan,
9288 struct nlattr *attr,
9289 struct cfg80211_wowlan *trig)
9291 struct nlattr **tb;
9292 int err;
9294 tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
9295 if (!tb)
9296 return -ENOMEM;
9298 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
9299 err = -EOPNOTSUPP;
9300 goto out;
9303 err = nla_parse(tb, NL80211_ATTR_MAX,
9304 nla_data(attr), nla_len(attr),
9305 nl80211_policy);
9306 if (err)
9307 goto out;
9309 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb);
9310 err = PTR_ERR_OR_ZERO(trig->nd_config);
9311 if (err)
9312 trig->nd_config = NULL;
9314 out:
9315 kfree(tb);
9316 return err;
9319 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9322 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
9323 struct cfg80211_wowlan new_triggers = {};
9324 struct cfg80211_wowlan *ntrig;
9325 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
9326 int err, i;
9327 bool prev_enabled = rdev->wiphy.wowlan_config;
9328 bool regular = false;
9330 if (!wowlan)
9331 return -EOPNOTSUPP;
9333 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
9334 cfg80211_rdev_free_wowlan(rdev);
9335 rdev->wiphy.wowlan_config = NULL;
9336 goto set_wakeup;
9339 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
9340 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
9341 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
9342 nl80211_wowlan_policy);
9343 if (err)
9344 return err;
9346 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
9347 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
9348 return -EINVAL;
9349 new_triggers.any = true;
9352 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
9353 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
9354 return -EINVAL;
9355 new_triggers.disconnect = true;
9356 regular = true;
9359 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
9360 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
9361 return -EINVAL;
9362 new_triggers.magic_pkt = true;
9363 regular = true;
9366 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
9367 return -EINVAL;
9369 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
9370 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
9371 return -EINVAL;
9372 new_triggers.gtk_rekey_failure = true;
9373 regular = true;
9376 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
9377 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
9378 return -EINVAL;
9379 new_triggers.eap_identity_req = true;
9380 regular = true;
9383 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
9384 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
9385 return -EINVAL;
9386 new_triggers.four_way_handshake = true;
9387 regular = true;
9390 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
9391 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
9392 return -EINVAL;
9393 new_triggers.rfkill_release = true;
9394 regular = true;
9397 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
9398 struct nlattr *pat;
9399 int n_patterns = 0;
9400 int rem, pat_len, mask_len, pkt_offset;
9401 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9403 regular = true;
9405 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
9406 rem)
9407 n_patterns++;
9408 if (n_patterns > wowlan->n_patterns)
9409 return -EINVAL;
9411 new_triggers.patterns = kcalloc(n_patterns,
9412 sizeof(new_triggers.patterns[0]),
9413 GFP_KERNEL);
9414 if (!new_triggers.patterns)
9415 return -ENOMEM;
9417 new_triggers.n_patterns = n_patterns;
9418 i = 0;
9420 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
9421 rem) {
9422 u8 *mask_pat;
9424 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9425 nla_len(pat), nl80211_packet_pattern_policy);
9426 err = -EINVAL;
9427 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9428 !pat_tb[NL80211_PKTPAT_PATTERN])
9429 goto error;
9430 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9431 mask_len = DIV_ROUND_UP(pat_len, 8);
9432 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9433 goto error;
9434 if (pat_len > wowlan->pattern_max_len ||
9435 pat_len < wowlan->pattern_min_len)
9436 goto error;
9438 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9439 pkt_offset = 0;
9440 else
9441 pkt_offset = nla_get_u32(
9442 pat_tb[NL80211_PKTPAT_OFFSET]);
9443 if (pkt_offset > wowlan->max_pkt_offset)
9444 goto error;
9445 new_triggers.patterns[i].pkt_offset = pkt_offset;
9447 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9448 if (!mask_pat) {
9449 err = -ENOMEM;
9450 goto error;
9452 new_triggers.patterns[i].mask = mask_pat;
9453 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9454 mask_len);
9455 mask_pat += mask_len;
9456 new_triggers.patterns[i].pattern = mask_pat;
9457 new_triggers.patterns[i].pattern_len = pat_len;
9458 memcpy(mask_pat,
9459 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9460 pat_len);
9461 i++;
9465 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
9466 regular = true;
9467 err = nl80211_parse_wowlan_tcp(
9468 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
9469 &new_triggers);
9470 if (err)
9471 goto error;
9474 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
9475 regular = true;
9476 err = nl80211_parse_wowlan_nd(
9477 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
9478 &new_triggers);
9479 if (err)
9480 goto error;
9483 /* The 'any' trigger means the device continues operating more or less
9484 * as in its normal operation mode and wakes up the host on most of the
9485 * normal interrupts (like packet RX, ...)
9486 * It therefore makes little sense to combine with the more constrained
9487 * wakeup trigger modes.
9489 if (new_triggers.any && regular) {
9490 err = -EINVAL;
9491 goto error;
9494 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
9495 if (!ntrig) {
9496 err = -ENOMEM;
9497 goto error;
9499 cfg80211_rdev_free_wowlan(rdev);
9500 rdev->wiphy.wowlan_config = ntrig;
9502 set_wakeup:
9503 if (rdev->ops->set_wakeup &&
9504 prev_enabled != !!rdev->wiphy.wowlan_config)
9505 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
9507 return 0;
9508 error:
9509 for (i = 0; i < new_triggers.n_patterns; i++)
9510 kfree(new_triggers.patterns[i].mask);
9511 kfree(new_triggers.patterns);
9512 if (new_triggers.tcp && new_triggers.tcp->sock)
9513 sock_release(new_triggers.tcp->sock);
9514 kfree(new_triggers.tcp);
9515 kfree(new_triggers.nd_config);
9516 return err;
9518 #endif
9520 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
9521 struct cfg80211_registered_device *rdev)
9523 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
9524 int i, j, pat_len;
9525 struct cfg80211_coalesce_rules *rule;
9527 if (!rdev->coalesce->n_rules)
9528 return 0;
9530 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
9531 if (!nl_rules)
9532 return -ENOBUFS;
9534 for (i = 0; i < rdev->coalesce->n_rules; i++) {
9535 nl_rule = nla_nest_start(msg, i + 1);
9536 if (!nl_rule)
9537 return -ENOBUFS;
9539 rule = &rdev->coalesce->rules[i];
9540 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
9541 rule->delay))
9542 return -ENOBUFS;
9544 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
9545 rule->condition))
9546 return -ENOBUFS;
9548 nl_pats = nla_nest_start(msg,
9549 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
9550 if (!nl_pats)
9551 return -ENOBUFS;
9553 for (j = 0; j < rule->n_patterns; j++) {
9554 nl_pat = nla_nest_start(msg, j + 1);
9555 if (!nl_pat)
9556 return -ENOBUFS;
9557 pat_len = rule->patterns[j].pattern_len;
9558 if (nla_put(msg, NL80211_PKTPAT_MASK,
9559 DIV_ROUND_UP(pat_len, 8),
9560 rule->patterns[j].mask) ||
9561 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
9562 rule->patterns[j].pattern) ||
9563 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
9564 rule->patterns[j].pkt_offset))
9565 return -ENOBUFS;
9566 nla_nest_end(msg, nl_pat);
9568 nla_nest_end(msg, nl_pats);
9569 nla_nest_end(msg, nl_rule);
9571 nla_nest_end(msg, nl_rules);
9573 return 0;
9576 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
9578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9579 struct sk_buff *msg;
9580 void *hdr;
9582 if (!rdev->wiphy.coalesce)
9583 return -EOPNOTSUPP;
9585 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9586 if (!msg)
9587 return -ENOMEM;
9589 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9590 NL80211_CMD_GET_COALESCE);
9591 if (!hdr)
9592 goto nla_put_failure;
9594 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
9595 goto nla_put_failure;
9597 genlmsg_end(msg, hdr);
9598 return genlmsg_reply(msg, info);
9600 nla_put_failure:
9601 nlmsg_free(msg);
9602 return -ENOBUFS;
9605 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
9607 struct cfg80211_coalesce *coalesce = rdev->coalesce;
9608 int i, j;
9609 struct cfg80211_coalesce_rules *rule;
9611 if (!coalesce)
9612 return;
9614 for (i = 0; i < coalesce->n_rules; i++) {
9615 rule = &coalesce->rules[i];
9616 for (j = 0; j < rule->n_patterns; j++)
9617 kfree(rule->patterns[j].mask);
9618 kfree(rule->patterns);
9620 kfree(coalesce->rules);
9621 kfree(coalesce);
9622 rdev->coalesce = NULL;
9625 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
9626 struct nlattr *rule,
9627 struct cfg80211_coalesce_rules *new_rule)
9629 int err, i;
9630 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9631 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
9632 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
9633 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9635 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
9636 nla_len(rule), nl80211_coalesce_policy);
9637 if (err)
9638 return err;
9640 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
9641 new_rule->delay =
9642 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
9643 if (new_rule->delay > coalesce->max_delay)
9644 return -EINVAL;
9646 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
9647 new_rule->condition =
9648 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
9649 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
9650 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
9651 return -EINVAL;
9653 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
9654 return -EINVAL;
9656 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9657 rem)
9658 n_patterns++;
9659 if (n_patterns > coalesce->n_patterns)
9660 return -EINVAL;
9662 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
9663 GFP_KERNEL);
9664 if (!new_rule->patterns)
9665 return -ENOMEM;
9667 new_rule->n_patterns = n_patterns;
9668 i = 0;
9670 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9671 rem) {
9672 u8 *mask_pat;
9674 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9675 nla_len(pat), nl80211_packet_pattern_policy);
9676 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9677 !pat_tb[NL80211_PKTPAT_PATTERN])
9678 return -EINVAL;
9679 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9680 mask_len = DIV_ROUND_UP(pat_len, 8);
9681 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9682 return -EINVAL;
9683 if (pat_len > coalesce->pattern_max_len ||
9684 pat_len < coalesce->pattern_min_len)
9685 return -EINVAL;
9687 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9688 pkt_offset = 0;
9689 else
9690 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
9691 if (pkt_offset > coalesce->max_pkt_offset)
9692 return -EINVAL;
9693 new_rule->patterns[i].pkt_offset = pkt_offset;
9695 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9696 if (!mask_pat)
9697 return -ENOMEM;
9699 new_rule->patterns[i].mask = mask_pat;
9700 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9701 mask_len);
9703 mask_pat += mask_len;
9704 new_rule->patterns[i].pattern = mask_pat;
9705 new_rule->patterns[i].pattern_len = pat_len;
9706 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9707 pat_len);
9708 i++;
9711 return 0;
9714 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
9716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9717 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9718 struct cfg80211_coalesce new_coalesce = {};
9719 struct cfg80211_coalesce *n_coalesce;
9720 int err, rem_rule, n_rules = 0, i, j;
9721 struct nlattr *rule;
9722 struct cfg80211_coalesce_rules *tmp_rule;
9724 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
9725 return -EOPNOTSUPP;
9727 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
9728 cfg80211_rdev_free_coalesce(rdev);
9729 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
9730 return 0;
9733 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9734 rem_rule)
9735 n_rules++;
9736 if (n_rules > coalesce->n_rules)
9737 return -EINVAL;
9739 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
9740 GFP_KERNEL);
9741 if (!new_coalesce.rules)
9742 return -ENOMEM;
9744 new_coalesce.n_rules = n_rules;
9745 i = 0;
9747 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9748 rem_rule) {
9749 err = nl80211_parse_coalesce_rule(rdev, rule,
9750 &new_coalesce.rules[i]);
9751 if (err)
9752 goto error;
9754 i++;
9757 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
9758 if (err)
9759 goto error;
9761 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
9762 if (!n_coalesce) {
9763 err = -ENOMEM;
9764 goto error;
9766 cfg80211_rdev_free_coalesce(rdev);
9767 rdev->coalesce = n_coalesce;
9769 return 0;
9770 error:
9771 for (i = 0; i < new_coalesce.n_rules; i++) {
9772 tmp_rule = &new_coalesce.rules[i];
9773 for (j = 0; j < tmp_rule->n_patterns; j++)
9774 kfree(tmp_rule->patterns[j].mask);
9775 kfree(tmp_rule->patterns);
9777 kfree(new_coalesce.rules);
9779 return err;
9782 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
9784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9785 struct net_device *dev = info->user_ptr[1];
9786 struct wireless_dev *wdev = dev->ieee80211_ptr;
9787 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
9788 struct cfg80211_gtk_rekey_data rekey_data;
9789 int err;
9791 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
9792 return -EINVAL;
9794 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
9795 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
9796 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
9797 nl80211_rekey_policy);
9798 if (err)
9799 return err;
9801 if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
9802 !tb[NL80211_REKEY_DATA_KCK])
9803 return -EINVAL;
9804 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
9805 return -ERANGE;
9806 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
9807 return -ERANGE;
9808 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
9809 return -ERANGE;
9811 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
9812 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
9813 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
9815 wdev_lock(wdev);
9816 if (!wdev->current_bss) {
9817 err = -ENOTCONN;
9818 goto out;
9821 if (!rdev->ops->set_rekey_data) {
9822 err = -EOPNOTSUPP;
9823 goto out;
9826 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
9827 out:
9828 wdev_unlock(wdev);
9829 return err;
9832 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
9833 struct genl_info *info)
9835 struct net_device *dev = info->user_ptr[1];
9836 struct wireless_dev *wdev = dev->ieee80211_ptr;
9838 if (wdev->iftype != NL80211_IFTYPE_AP &&
9839 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9840 return -EINVAL;
9842 if (wdev->ap_unexpected_nlportid)
9843 return -EBUSY;
9845 wdev->ap_unexpected_nlportid = info->snd_portid;
9846 return 0;
9849 static int nl80211_probe_client(struct sk_buff *skb,
9850 struct genl_info *info)
9852 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9853 struct net_device *dev = info->user_ptr[1];
9854 struct wireless_dev *wdev = dev->ieee80211_ptr;
9855 struct sk_buff *msg;
9856 void *hdr;
9857 const u8 *addr;
9858 u64 cookie;
9859 int err;
9861 if (wdev->iftype != NL80211_IFTYPE_AP &&
9862 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9863 return -EOPNOTSUPP;
9865 if (!info->attrs[NL80211_ATTR_MAC])
9866 return -EINVAL;
9868 if (!rdev->ops->probe_client)
9869 return -EOPNOTSUPP;
9871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9872 if (!msg)
9873 return -ENOMEM;
9875 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9876 NL80211_CMD_PROBE_CLIENT);
9877 if (!hdr) {
9878 err = -ENOBUFS;
9879 goto free_msg;
9882 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9884 err = rdev_probe_client(rdev, dev, addr, &cookie);
9885 if (err)
9886 goto free_msg;
9888 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9889 goto nla_put_failure;
9891 genlmsg_end(msg, hdr);
9893 return genlmsg_reply(msg, info);
9895 nla_put_failure:
9896 err = -ENOBUFS;
9897 free_msg:
9898 nlmsg_free(msg);
9899 return err;
9902 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
9904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9905 struct cfg80211_beacon_registration *reg, *nreg;
9906 int rv;
9908 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
9909 return -EOPNOTSUPP;
9911 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
9912 if (!nreg)
9913 return -ENOMEM;
9915 /* First, check if already registered. */
9916 spin_lock_bh(&rdev->beacon_registrations_lock);
9917 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9918 if (reg->nlportid == info->snd_portid) {
9919 rv = -EALREADY;
9920 goto out_err;
9923 /* Add it to the list */
9924 nreg->nlportid = info->snd_portid;
9925 list_add(&nreg->list, &rdev->beacon_registrations);
9927 spin_unlock_bh(&rdev->beacon_registrations_lock);
9929 return 0;
9930 out_err:
9931 spin_unlock_bh(&rdev->beacon_registrations_lock);
9932 kfree(nreg);
9933 return rv;
9936 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
9938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9939 struct wireless_dev *wdev = info->user_ptr[1];
9940 int err;
9942 if (!rdev->ops->start_p2p_device)
9943 return -EOPNOTSUPP;
9945 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9946 return -EOPNOTSUPP;
9948 if (wdev->p2p_started)
9949 return 0;
9951 if (rfkill_blocked(rdev->rfkill))
9952 return -ERFKILL;
9954 err = rdev_start_p2p_device(rdev, wdev);
9955 if (err)
9956 return err;
9958 wdev->p2p_started = true;
9959 rdev->opencount++;
9961 return 0;
9964 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9967 struct wireless_dev *wdev = info->user_ptr[1];
9969 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9970 return -EOPNOTSUPP;
9972 if (!rdev->ops->stop_p2p_device)
9973 return -EOPNOTSUPP;
9975 cfg80211_stop_p2p_device(rdev, wdev);
9977 return 0;
9980 static int nl80211_get_protocol_features(struct sk_buff *skb,
9981 struct genl_info *info)
9983 void *hdr;
9984 struct sk_buff *msg;
9986 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9987 if (!msg)
9988 return -ENOMEM;
9990 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9991 NL80211_CMD_GET_PROTOCOL_FEATURES);
9992 if (!hdr)
9993 goto nla_put_failure;
9995 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9996 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9997 goto nla_put_failure;
9999 genlmsg_end(msg, hdr);
10000 return genlmsg_reply(msg, info);
10002 nla_put_failure:
10003 kfree_skb(msg);
10004 return -ENOBUFS;
10007 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
10009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10010 struct cfg80211_update_ft_ies_params ft_params;
10011 struct net_device *dev = info->user_ptr[1];
10013 if (!rdev->ops->update_ft_ies)
10014 return -EOPNOTSUPP;
10016 if (!info->attrs[NL80211_ATTR_MDID] ||
10017 !info->attrs[NL80211_ATTR_IE] ||
10018 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
10019 return -EINVAL;
10021 memset(&ft_params, 0, sizeof(ft_params));
10022 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
10023 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10024 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10026 return rdev_update_ft_ies(rdev, dev, &ft_params);
10029 static int nl80211_crit_protocol_start(struct sk_buff *skb,
10030 struct genl_info *info)
10032 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10033 struct wireless_dev *wdev = info->user_ptr[1];
10034 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
10035 u16 duration;
10036 int ret;
10038 if (!rdev->ops->crit_proto_start)
10039 return -EOPNOTSUPP;
10041 if (WARN_ON(!rdev->ops->crit_proto_stop))
10042 return -EINVAL;
10044 if (rdev->crit_proto_nlportid)
10045 return -EBUSY;
10047 /* determine protocol if provided */
10048 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
10049 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
10051 if (proto >= NUM_NL80211_CRIT_PROTO)
10052 return -EINVAL;
10054 /* timeout must be provided */
10055 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
10056 return -EINVAL;
10058 duration =
10059 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
10061 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
10062 return -ERANGE;
10064 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
10065 if (!ret)
10066 rdev->crit_proto_nlportid = info->snd_portid;
10068 return ret;
10071 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
10072 struct genl_info *info)
10074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10075 struct wireless_dev *wdev = info->user_ptr[1];
10077 if (!rdev->ops->crit_proto_stop)
10078 return -EOPNOTSUPP;
10080 if (rdev->crit_proto_nlportid) {
10081 rdev->crit_proto_nlportid = 0;
10082 rdev_crit_proto_stop(rdev, wdev);
10084 return 0;
10087 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
10089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10090 struct wireless_dev *wdev =
10091 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
10092 int i, err;
10093 u32 vid, subcmd;
10095 if (!rdev->wiphy.vendor_commands)
10096 return -EOPNOTSUPP;
10098 if (IS_ERR(wdev)) {
10099 err = PTR_ERR(wdev);
10100 if (err != -EINVAL)
10101 return err;
10102 wdev = NULL;
10103 } else if (wdev->wiphy != &rdev->wiphy) {
10104 return -EINVAL;
10107 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
10108 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
10109 return -EINVAL;
10111 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
10112 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
10113 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
10114 const struct wiphy_vendor_command *vcmd;
10115 void *data = NULL;
10116 int len = 0;
10118 vcmd = &rdev->wiphy.vendor_commands[i];
10120 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
10121 continue;
10123 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
10124 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
10125 if (!wdev)
10126 return -EINVAL;
10127 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
10128 !wdev->netdev)
10129 return -EINVAL;
10131 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
10132 if (wdev->netdev &&
10133 !netif_running(wdev->netdev))
10134 return -ENETDOWN;
10135 if (!wdev->netdev && !wdev->p2p_started)
10136 return -ENETDOWN;
10139 if (!vcmd->doit)
10140 return -EOPNOTSUPP;
10141 } else {
10142 wdev = NULL;
10145 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
10146 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
10147 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
10150 rdev->cur_cmd_info = info;
10151 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
10152 data, len);
10153 rdev->cur_cmd_info = NULL;
10154 return err;
10157 return -EOPNOTSUPP;
10160 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
10161 struct netlink_callback *cb,
10162 struct cfg80211_registered_device **rdev,
10163 struct wireless_dev **wdev)
10165 u32 vid, subcmd;
10166 unsigned int i;
10167 int vcmd_idx = -1;
10168 int err;
10169 void *data = NULL;
10170 unsigned int data_len = 0;
10172 if (cb->args[0]) {
10173 /* subtract the 1 again here */
10174 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
10175 struct wireless_dev *tmp;
10177 if (!wiphy)
10178 return -ENODEV;
10179 *rdev = wiphy_to_rdev(wiphy);
10180 *wdev = NULL;
10182 if (cb->args[1]) {
10183 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
10184 if (tmp->identifier == cb->args[1] - 1) {
10185 *wdev = tmp;
10186 break;
10191 /* keep rtnl locked in successful case */
10192 return 0;
10195 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
10196 nl80211_fam.attrbuf, nl80211_fam.maxattr,
10197 nl80211_policy);
10198 if (err)
10199 return err;
10201 if (!nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_ID] ||
10202 !nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
10203 return -EINVAL;
10205 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
10206 nl80211_fam.attrbuf);
10207 if (IS_ERR(*wdev))
10208 *wdev = NULL;
10210 *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
10211 nl80211_fam.attrbuf);
10212 if (IS_ERR(*rdev))
10213 return PTR_ERR(*rdev);
10215 vid = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_ID]);
10216 subcmd = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
10218 for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
10219 const struct wiphy_vendor_command *vcmd;
10221 vcmd = &(*rdev)->wiphy.vendor_commands[i];
10223 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
10224 continue;
10226 if (!vcmd->dumpit)
10227 return -EOPNOTSUPP;
10229 vcmd_idx = i;
10230 break;
10233 if (vcmd_idx < 0)
10234 return -EOPNOTSUPP;
10236 if (nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]) {
10237 data = nla_data(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]);
10238 data_len = nla_len(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]);
10241 /* 0 is the first index - add 1 to parse only once */
10242 cb->args[0] = (*rdev)->wiphy_idx + 1;
10243 /* add 1 to know if it was NULL */
10244 cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
10245 cb->args[2] = vcmd_idx;
10246 cb->args[3] = (unsigned long)data;
10247 cb->args[4] = data_len;
10249 /* keep rtnl locked in successful case */
10250 return 0;
10253 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
10254 struct netlink_callback *cb)
10256 struct cfg80211_registered_device *rdev;
10257 struct wireless_dev *wdev;
10258 unsigned int vcmd_idx;
10259 const struct wiphy_vendor_command *vcmd;
10260 void *data;
10261 int data_len;
10262 int err;
10263 struct nlattr *vendor_data;
10265 rtnl_lock();
10266 err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
10267 if (err)
10268 goto out;
10270 vcmd_idx = cb->args[2];
10271 data = (void *)cb->args[3];
10272 data_len = cb->args[4];
10273 vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
10275 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
10276 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
10277 if (!wdev) {
10278 err = -EINVAL;
10279 goto out;
10281 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
10282 !wdev->netdev) {
10283 err = -EINVAL;
10284 goto out;
10287 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
10288 if (wdev->netdev &&
10289 !netif_running(wdev->netdev)) {
10290 err = -ENETDOWN;
10291 goto out;
10293 if (!wdev->netdev && !wdev->p2p_started) {
10294 err = -ENETDOWN;
10295 goto out;
10300 while (1) {
10301 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
10302 cb->nlh->nlmsg_seq, NLM_F_MULTI,
10303 NL80211_CMD_VENDOR);
10304 if (!hdr)
10305 break;
10307 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10308 (wdev && nla_put_u64(skb, NL80211_ATTR_WDEV,
10309 wdev_id(wdev)))) {
10310 genlmsg_cancel(skb, hdr);
10311 break;
10314 vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
10315 if (!vendor_data) {
10316 genlmsg_cancel(skb, hdr);
10317 break;
10320 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
10321 (unsigned long *)&cb->args[5]);
10322 nla_nest_end(skb, vendor_data);
10324 if (err == -ENOBUFS || err == -ENOENT) {
10325 genlmsg_cancel(skb, hdr);
10326 break;
10327 } else if (err) {
10328 genlmsg_cancel(skb, hdr);
10329 goto out;
10332 genlmsg_end(skb, hdr);
10335 err = skb->len;
10336 out:
10337 rtnl_unlock();
10338 return err;
10341 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
10342 enum nl80211_commands cmd,
10343 enum nl80211_attrs attr,
10344 int approxlen)
10346 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10348 if (WARN_ON(!rdev->cur_cmd_info))
10349 return NULL;
10351 return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
10352 rdev->cur_cmd_info->snd_portid,
10353 rdev->cur_cmd_info->snd_seq,
10354 cmd, attr, NULL, GFP_KERNEL);
10356 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
10358 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
10360 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
10361 void *hdr = ((void **)skb->cb)[1];
10362 struct nlattr *data = ((void **)skb->cb)[2];
10364 /* clear CB data for netlink core to own from now on */
10365 memset(skb->cb, 0, sizeof(skb->cb));
10367 if (WARN_ON(!rdev->cur_cmd_info)) {
10368 kfree_skb(skb);
10369 return -EINVAL;
10372 nla_nest_end(skb, data);
10373 genlmsg_end(skb, hdr);
10374 return genlmsg_reply(skb, rdev->cur_cmd_info);
10376 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
10379 static int nl80211_set_qos_map(struct sk_buff *skb,
10380 struct genl_info *info)
10382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10383 struct cfg80211_qos_map *qos_map = NULL;
10384 struct net_device *dev = info->user_ptr[1];
10385 u8 *pos, len, num_des, des_len, des;
10386 int ret;
10388 if (!rdev->ops->set_qos_map)
10389 return -EOPNOTSUPP;
10391 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
10392 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
10393 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
10395 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
10396 len > IEEE80211_QOS_MAP_LEN_MAX)
10397 return -EINVAL;
10399 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
10400 if (!qos_map)
10401 return -ENOMEM;
10403 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
10404 if (num_des) {
10405 des_len = num_des *
10406 sizeof(struct cfg80211_dscp_exception);
10407 memcpy(qos_map->dscp_exception, pos, des_len);
10408 qos_map->num_des = num_des;
10409 for (des = 0; des < num_des; des++) {
10410 if (qos_map->dscp_exception[des].up > 7) {
10411 kfree(qos_map);
10412 return -EINVAL;
10415 pos += des_len;
10417 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
10420 wdev_lock(dev->ieee80211_ptr);
10421 ret = nl80211_key_allowed(dev->ieee80211_ptr);
10422 if (!ret)
10423 ret = rdev_set_qos_map(rdev, dev, qos_map);
10424 wdev_unlock(dev->ieee80211_ptr);
10426 kfree(qos_map);
10427 return ret;
10430 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
10432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10433 struct net_device *dev = info->user_ptr[1];
10434 struct wireless_dev *wdev = dev->ieee80211_ptr;
10435 const u8 *peer;
10436 u8 tsid, up;
10437 u16 admitted_time = 0;
10438 int err;
10440 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
10441 return -EOPNOTSUPP;
10443 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
10444 !info->attrs[NL80211_ATTR_USER_PRIO])
10445 return -EINVAL;
10447 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
10448 if (tsid >= IEEE80211_NUM_TIDS)
10449 return -EINVAL;
10451 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
10452 if (up >= IEEE80211_NUM_UPS)
10453 return -EINVAL;
10455 /* WMM uses TIDs 0-7 even for TSPEC */
10456 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
10457 /* TODO: handle 802.11 TSPEC/admission control
10458 * need more attributes for that (e.g. BA session requirement);
10459 * change the WMM adminssion test above to allow both then
10461 return -EINVAL;
10464 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10466 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
10467 admitted_time =
10468 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
10469 if (!admitted_time)
10470 return -EINVAL;
10473 wdev_lock(wdev);
10474 switch (wdev->iftype) {
10475 case NL80211_IFTYPE_STATION:
10476 case NL80211_IFTYPE_P2P_CLIENT:
10477 if (wdev->current_bss)
10478 break;
10479 err = -ENOTCONN;
10480 goto out;
10481 default:
10482 err = -EOPNOTSUPP;
10483 goto out;
10486 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
10488 out:
10489 wdev_unlock(wdev);
10490 return err;
10493 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
10495 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10496 struct net_device *dev = info->user_ptr[1];
10497 struct wireless_dev *wdev = dev->ieee80211_ptr;
10498 const u8 *peer;
10499 u8 tsid;
10500 int err;
10502 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
10503 return -EINVAL;
10505 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
10506 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10508 wdev_lock(wdev);
10509 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
10510 wdev_unlock(wdev);
10512 return err;
10515 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
10516 struct genl_info *info)
10518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10519 struct net_device *dev = info->user_ptr[1];
10520 struct wireless_dev *wdev = dev->ieee80211_ptr;
10521 struct cfg80211_chan_def chandef = {};
10522 const u8 *addr;
10523 u8 oper_class;
10524 int err;
10526 if (!rdev->ops->tdls_channel_switch ||
10527 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
10528 return -EOPNOTSUPP;
10530 switch (dev->ieee80211_ptr->iftype) {
10531 case NL80211_IFTYPE_STATION:
10532 case NL80211_IFTYPE_P2P_CLIENT:
10533 break;
10534 default:
10535 return -EOPNOTSUPP;
10538 if (!info->attrs[NL80211_ATTR_MAC] ||
10539 !info->attrs[NL80211_ATTR_OPER_CLASS])
10540 return -EINVAL;
10542 err = nl80211_parse_chandef(rdev, info, &chandef);
10543 if (err)
10544 return err;
10547 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
10548 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
10549 * specification is not defined for them.
10551 if (chandef.chan->band == IEEE80211_BAND_2GHZ &&
10552 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
10553 chandef.width != NL80211_CHAN_WIDTH_20)
10554 return -EINVAL;
10556 /* we will be active on the TDLS link */
10557 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
10558 wdev->iftype))
10559 return -EINVAL;
10561 /* don't allow switching to DFS channels */
10562 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
10563 return -EINVAL;
10565 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
10566 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
10568 wdev_lock(wdev);
10569 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
10570 wdev_unlock(wdev);
10572 return err;
10575 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
10576 struct genl_info *info)
10578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10579 struct net_device *dev = info->user_ptr[1];
10580 struct wireless_dev *wdev = dev->ieee80211_ptr;
10581 const u8 *addr;
10583 if (!rdev->ops->tdls_channel_switch ||
10584 !rdev->ops->tdls_cancel_channel_switch ||
10585 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
10586 return -EOPNOTSUPP;
10588 switch (dev->ieee80211_ptr->iftype) {
10589 case NL80211_IFTYPE_STATION:
10590 case NL80211_IFTYPE_P2P_CLIENT:
10591 break;
10592 default:
10593 return -EOPNOTSUPP;
10596 if (!info->attrs[NL80211_ATTR_MAC])
10597 return -EINVAL;
10599 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
10601 wdev_lock(wdev);
10602 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
10603 wdev_unlock(wdev);
10605 return 0;
10608 #define NL80211_FLAG_NEED_WIPHY 0x01
10609 #define NL80211_FLAG_NEED_NETDEV 0x02
10610 #define NL80211_FLAG_NEED_RTNL 0x04
10611 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
10612 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
10613 NL80211_FLAG_CHECK_NETDEV_UP)
10614 #define NL80211_FLAG_NEED_WDEV 0x10
10615 /* If a netdev is associated, it must be UP, P2P must be started */
10616 #define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
10617 NL80211_FLAG_CHECK_NETDEV_UP)
10618 #define NL80211_FLAG_CLEAR_SKB 0x20
10620 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
10621 struct genl_info *info)
10623 struct cfg80211_registered_device *rdev;
10624 struct wireless_dev *wdev;
10625 struct net_device *dev;
10626 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
10628 if (rtnl)
10629 rtnl_lock();
10631 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
10632 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
10633 if (IS_ERR(rdev)) {
10634 if (rtnl)
10635 rtnl_unlock();
10636 return PTR_ERR(rdev);
10638 info->user_ptr[0] = rdev;
10639 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
10640 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
10641 ASSERT_RTNL();
10643 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
10644 info->attrs);
10645 if (IS_ERR(wdev)) {
10646 if (rtnl)
10647 rtnl_unlock();
10648 return PTR_ERR(wdev);
10651 dev = wdev->netdev;
10652 rdev = wiphy_to_rdev(wdev->wiphy);
10654 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
10655 if (!dev) {
10656 if (rtnl)
10657 rtnl_unlock();
10658 return -EINVAL;
10661 info->user_ptr[1] = dev;
10662 } else {
10663 info->user_ptr[1] = wdev;
10666 if (dev) {
10667 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
10668 !netif_running(dev)) {
10669 if (rtnl)
10670 rtnl_unlock();
10671 return -ENETDOWN;
10674 dev_hold(dev);
10675 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
10676 if (!wdev->p2p_started) {
10677 if (rtnl)
10678 rtnl_unlock();
10679 return -ENETDOWN;
10683 info->user_ptr[0] = rdev;
10686 return 0;
10689 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
10690 struct genl_info *info)
10692 if (info->user_ptr[1]) {
10693 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
10694 struct wireless_dev *wdev = info->user_ptr[1];
10696 if (wdev->netdev)
10697 dev_put(wdev->netdev);
10698 } else {
10699 dev_put(info->user_ptr[1]);
10703 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
10704 rtnl_unlock();
10706 /* If needed, clear the netlink message payload from the SKB
10707 * as it might contain key data that shouldn't stick around on
10708 * the heap after the SKB is freed. The netlink message header
10709 * is still needed for further processing, so leave it intact.
10711 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
10712 struct nlmsghdr *nlh = nlmsg_hdr(skb);
10714 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
10718 static const struct genl_ops nl80211_ops[] = {
10720 .cmd = NL80211_CMD_GET_WIPHY,
10721 .doit = nl80211_get_wiphy,
10722 .dumpit = nl80211_dump_wiphy,
10723 .done = nl80211_dump_wiphy_done,
10724 .policy = nl80211_policy,
10725 /* can be retrieved by unprivileged users */
10726 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10727 NL80211_FLAG_NEED_RTNL,
10730 .cmd = NL80211_CMD_SET_WIPHY,
10731 .doit = nl80211_set_wiphy,
10732 .policy = nl80211_policy,
10733 .flags = GENL_ADMIN_PERM,
10734 .internal_flags = NL80211_FLAG_NEED_RTNL,
10737 .cmd = NL80211_CMD_GET_INTERFACE,
10738 .doit = nl80211_get_interface,
10739 .dumpit = nl80211_dump_interface,
10740 .policy = nl80211_policy,
10741 /* can be retrieved by unprivileged users */
10742 .internal_flags = NL80211_FLAG_NEED_WDEV |
10743 NL80211_FLAG_NEED_RTNL,
10746 .cmd = NL80211_CMD_SET_INTERFACE,
10747 .doit = nl80211_set_interface,
10748 .policy = nl80211_policy,
10749 .flags = GENL_ADMIN_PERM,
10750 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10751 NL80211_FLAG_NEED_RTNL,
10754 .cmd = NL80211_CMD_NEW_INTERFACE,
10755 .doit = nl80211_new_interface,
10756 .policy = nl80211_policy,
10757 .flags = GENL_ADMIN_PERM,
10758 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10759 NL80211_FLAG_NEED_RTNL,
10762 .cmd = NL80211_CMD_DEL_INTERFACE,
10763 .doit = nl80211_del_interface,
10764 .policy = nl80211_policy,
10765 .flags = GENL_ADMIN_PERM,
10766 .internal_flags = NL80211_FLAG_NEED_WDEV |
10767 NL80211_FLAG_NEED_RTNL,
10770 .cmd = NL80211_CMD_GET_KEY,
10771 .doit = nl80211_get_key,
10772 .policy = nl80211_policy,
10773 .flags = GENL_ADMIN_PERM,
10774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10775 NL80211_FLAG_NEED_RTNL,
10778 .cmd = NL80211_CMD_SET_KEY,
10779 .doit = nl80211_set_key,
10780 .policy = nl80211_policy,
10781 .flags = GENL_ADMIN_PERM,
10782 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10783 NL80211_FLAG_NEED_RTNL |
10784 NL80211_FLAG_CLEAR_SKB,
10787 .cmd = NL80211_CMD_NEW_KEY,
10788 .doit = nl80211_new_key,
10789 .policy = nl80211_policy,
10790 .flags = GENL_ADMIN_PERM,
10791 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10792 NL80211_FLAG_NEED_RTNL |
10793 NL80211_FLAG_CLEAR_SKB,
10796 .cmd = NL80211_CMD_DEL_KEY,
10797 .doit = nl80211_del_key,
10798 .policy = nl80211_policy,
10799 .flags = GENL_ADMIN_PERM,
10800 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10801 NL80211_FLAG_NEED_RTNL,
10804 .cmd = NL80211_CMD_SET_BEACON,
10805 .policy = nl80211_policy,
10806 .flags = GENL_ADMIN_PERM,
10807 .doit = nl80211_set_beacon,
10808 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10809 NL80211_FLAG_NEED_RTNL,
10812 .cmd = NL80211_CMD_START_AP,
10813 .policy = nl80211_policy,
10814 .flags = GENL_ADMIN_PERM,
10815 .doit = nl80211_start_ap,
10816 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10817 NL80211_FLAG_NEED_RTNL,
10820 .cmd = NL80211_CMD_STOP_AP,
10821 .policy = nl80211_policy,
10822 .flags = GENL_ADMIN_PERM,
10823 .doit = nl80211_stop_ap,
10824 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10825 NL80211_FLAG_NEED_RTNL,
10828 .cmd = NL80211_CMD_GET_STATION,
10829 .doit = nl80211_get_station,
10830 .dumpit = nl80211_dump_station,
10831 .policy = nl80211_policy,
10832 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10833 NL80211_FLAG_NEED_RTNL,
10836 .cmd = NL80211_CMD_SET_STATION,
10837 .doit = nl80211_set_station,
10838 .policy = nl80211_policy,
10839 .flags = GENL_ADMIN_PERM,
10840 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10841 NL80211_FLAG_NEED_RTNL,
10844 .cmd = NL80211_CMD_NEW_STATION,
10845 .doit = nl80211_new_station,
10846 .policy = nl80211_policy,
10847 .flags = GENL_ADMIN_PERM,
10848 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10849 NL80211_FLAG_NEED_RTNL,
10852 .cmd = NL80211_CMD_DEL_STATION,
10853 .doit = nl80211_del_station,
10854 .policy = nl80211_policy,
10855 .flags = GENL_ADMIN_PERM,
10856 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10857 NL80211_FLAG_NEED_RTNL,
10860 .cmd = NL80211_CMD_GET_MPATH,
10861 .doit = nl80211_get_mpath,
10862 .dumpit = nl80211_dump_mpath,
10863 .policy = nl80211_policy,
10864 .flags = GENL_ADMIN_PERM,
10865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10866 NL80211_FLAG_NEED_RTNL,
10869 .cmd = NL80211_CMD_GET_MPP,
10870 .doit = nl80211_get_mpp,
10871 .dumpit = nl80211_dump_mpp,
10872 .policy = nl80211_policy,
10873 .flags = GENL_ADMIN_PERM,
10874 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10875 NL80211_FLAG_NEED_RTNL,
10878 .cmd = NL80211_CMD_SET_MPATH,
10879 .doit = nl80211_set_mpath,
10880 .policy = nl80211_policy,
10881 .flags = GENL_ADMIN_PERM,
10882 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10883 NL80211_FLAG_NEED_RTNL,
10886 .cmd = NL80211_CMD_NEW_MPATH,
10887 .doit = nl80211_new_mpath,
10888 .policy = nl80211_policy,
10889 .flags = GENL_ADMIN_PERM,
10890 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10891 NL80211_FLAG_NEED_RTNL,
10894 .cmd = NL80211_CMD_DEL_MPATH,
10895 .doit = nl80211_del_mpath,
10896 .policy = nl80211_policy,
10897 .flags = GENL_ADMIN_PERM,
10898 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10899 NL80211_FLAG_NEED_RTNL,
10902 .cmd = NL80211_CMD_SET_BSS,
10903 .doit = nl80211_set_bss,
10904 .policy = nl80211_policy,
10905 .flags = GENL_ADMIN_PERM,
10906 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10907 NL80211_FLAG_NEED_RTNL,
10910 .cmd = NL80211_CMD_GET_REG,
10911 .doit = nl80211_get_reg_do,
10912 .dumpit = nl80211_get_reg_dump,
10913 .policy = nl80211_policy,
10914 .internal_flags = NL80211_FLAG_NEED_RTNL,
10915 /* can be retrieved by unprivileged users */
10917 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
10919 .cmd = NL80211_CMD_SET_REG,
10920 .doit = nl80211_set_reg,
10921 .policy = nl80211_policy,
10922 .flags = GENL_ADMIN_PERM,
10923 .internal_flags = NL80211_FLAG_NEED_RTNL,
10925 #endif
10927 .cmd = NL80211_CMD_REQ_SET_REG,
10928 .doit = nl80211_req_set_reg,
10929 .policy = nl80211_policy,
10930 .flags = GENL_ADMIN_PERM,
10933 .cmd = NL80211_CMD_GET_MESH_CONFIG,
10934 .doit = nl80211_get_mesh_config,
10935 .policy = nl80211_policy,
10936 /* can be retrieved by unprivileged users */
10937 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10938 NL80211_FLAG_NEED_RTNL,
10941 .cmd = NL80211_CMD_SET_MESH_CONFIG,
10942 .doit = nl80211_update_mesh_config,
10943 .policy = nl80211_policy,
10944 .flags = GENL_ADMIN_PERM,
10945 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10946 NL80211_FLAG_NEED_RTNL,
10949 .cmd = NL80211_CMD_TRIGGER_SCAN,
10950 .doit = nl80211_trigger_scan,
10951 .policy = nl80211_policy,
10952 .flags = GENL_ADMIN_PERM,
10953 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10954 NL80211_FLAG_NEED_RTNL,
10957 .cmd = NL80211_CMD_GET_SCAN,
10958 .policy = nl80211_policy,
10959 .dumpit = nl80211_dump_scan,
10962 .cmd = NL80211_CMD_START_SCHED_SCAN,
10963 .doit = nl80211_start_sched_scan,
10964 .policy = nl80211_policy,
10965 .flags = GENL_ADMIN_PERM,
10966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10967 NL80211_FLAG_NEED_RTNL,
10970 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
10971 .doit = nl80211_stop_sched_scan,
10972 .policy = nl80211_policy,
10973 .flags = GENL_ADMIN_PERM,
10974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10975 NL80211_FLAG_NEED_RTNL,
10978 .cmd = NL80211_CMD_AUTHENTICATE,
10979 .doit = nl80211_authenticate,
10980 .policy = nl80211_policy,
10981 .flags = GENL_ADMIN_PERM,
10982 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10983 NL80211_FLAG_NEED_RTNL |
10984 NL80211_FLAG_CLEAR_SKB,
10987 .cmd = NL80211_CMD_ASSOCIATE,
10988 .doit = nl80211_associate,
10989 .policy = nl80211_policy,
10990 .flags = GENL_ADMIN_PERM,
10991 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10992 NL80211_FLAG_NEED_RTNL,
10995 .cmd = NL80211_CMD_DEAUTHENTICATE,
10996 .doit = nl80211_deauthenticate,
10997 .policy = nl80211_policy,
10998 .flags = GENL_ADMIN_PERM,
10999 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11000 NL80211_FLAG_NEED_RTNL,
11003 .cmd = NL80211_CMD_DISASSOCIATE,
11004 .doit = nl80211_disassociate,
11005 .policy = nl80211_policy,
11006 .flags = GENL_ADMIN_PERM,
11007 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11008 NL80211_FLAG_NEED_RTNL,
11011 .cmd = NL80211_CMD_JOIN_IBSS,
11012 .doit = nl80211_join_ibss,
11013 .policy = nl80211_policy,
11014 .flags = GENL_ADMIN_PERM,
11015 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11016 NL80211_FLAG_NEED_RTNL,
11019 .cmd = NL80211_CMD_LEAVE_IBSS,
11020 .doit = nl80211_leave_ibss,
11021 .policy = nl80211_policy,
11022 .flags = GENL_ADMIN_PERM,
11023 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11024 NL80211_FLAG_NEED_RTNL,
11026 #ifdef CONFIG_NL80211_TESTMODE
11028 .cmd = NL80211_CMD_TESTMODE,
11029 .doit = nl80211_testmode_do,
11030 .dumpit = nl80211_testmode_dump,
11031 .policy = nl80211_policy,
11032 .flags = GENL_ADMIN_PERM,
11033 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11034 NL80211_FLAG_NEED_RTNL,
11036 #endif
11038 .cmd = NL80211_CMD_CONNECT,
11039 .doit = nl80211_connect,
11040 .policy = nl80211_policy,
11041 .flags = GENL_ADMIN_PERM,
11042 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11043 NL80211_FLAG_NEED_RTNL,
11046 .cmd = NL80211_CMD_DISCONNECT,
11047 .doit = nl80211_disconnect,
11048 .policy = nl80211_policy,
11049 .flags = GENL_ADMIN_PERM,
11050 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11051 NL80211_FLAG_NEED_RTNL,
11054 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
11055 .doit = nl80211_wiphy_netns,
11056 .policy = nl80211_policy,
11057 .flags = GENL_ADMIN_PERM,
11058 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11059 NL80211_FLAG_NEED_RTNL,
11062 .cmd = NL80211_CMD_GET_SURVEY,
11063 .policy = nl80211_policy,
11064 .dumpit = nl80211_dump_survey,
11067 .cmd = NL80211_CMD_SET_PMKSA,
11068 .doit = nl80211_setdel_pmksa,
11069 .policy = nl80211_policy,
11070 .flags = GENL_ADMIN_PERM,
11071 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11072 NL80211_FLAG_NEED_RTNL,
11075 .cmd = NL80211_CMD_DEL_PMKSA,
11076 .doit = nl80211_setdel_pmksa,
11077 .policy = nl80211_policy,
11078 .flags = GENL_ADMIN_PERM,
11079 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11080 NL80211_FLAG_NEED_RTNL,
11083 .cmd = NL80211_CMD_FLUSH_PMKSA,
11084 .doit = nl80211_flush_pmksa,
11085 .policy = nl80211_policy,
11086 .flags = GENL_ADMIN_PERM,
11087 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11088 NL80211_FLAG_NEED_RTNL,
11091 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
11092 .doit = nl80211_remain_on_channel,
11093 .policy = nl80211_policy,
11094 .flags = GENL_ADMIN_PERM,
11095 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11096 NL80211_FLAG_NEED_RTNL,
11099 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
11100 .doit = nl80211_cancel_remain_on_channel,
11101 .policy = nl80211_policy,
11102 .flags = GENL_ADMIN_PERM,
11103 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11104 NL80211_FLAG_NEED_RTNL,
11107 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
11108 .doit = nl80211_set_tx_bitrate_mask,
11109 .policy = nl80211_policy,
11110 .flags = GENL_ADMIN_PERM,
11111 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11112 NL80211_FLAG_NEED_RTNL,
11115 .cmd = NL80211_CMD_REGISTER_FRAME,
11116 .doit = nl80211_register_mgmt,
11117 .policy = nl80211_policy,
11118 .flags = GENL_ADMIN_PERM,
11119 .internal_flags = NL80211_FLAG_NEED_WDEV |
11120 NL80211_FLAG_NEED_RTNL,
11123 .cmd = NL80211_CMD_FRAME,
11124 .doit = nl80211_tx_mgmt,
11125 .policy = nl80211_policy,
11126 .flags = GENL_ADMIN_PERM,
11127 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11128 NL80211_FLAG_NEED_RTNL,
11131 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
11132 .doit = nl80211_tx_mgmt_cancel_wait,
11133 .policy = nl80211_policy,
11134 .flags = GENL_ADMIN_PERM,
11135 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11136 NL80211_FLAG_NEED_RTNL,
11139 .cmd = NL80211_CMD_SET_POWER_SAVE,
11140 .doit = nl80211_set_power_save,
11141 .policy = nl80211_policy,
11142 .flags = GENL_ADMIN_PERM,
11143 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11144 NL80211_FLAG_NEED_RTNL,
11147 .cmd = NL80211_CMD_GET_POWER_SAVE,
11148 .doit = nl80211_get_power_save,
11149 .policy = nl80211_policy,
11150 /* can be retrieved by unprivileged users */
11151 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11152 NL80211_FLAG_NEED_RTNL,
11155 .cmd = NL80211_CMD_SET_CQM,
11156 .doit = nl80211_set_cqm,
11157 .policy = nl80211_policy,
11158 .flags = GENL_ADMIN_PERM,
11159 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11160 NL80211_FLAG_NEED_RTNL,
11163 .cmd = NL80211_CMD_SET_CHANNEL,
11164 .doit = nl80211_set_channel,
11165 .policy = nl80211_policy,
11166 .flags = GENL_ADMIN_PERM,
11167 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11168 NL80211_FLAG_NEED_RTNL,
11171 .cmd = NL80211_CMD_SET_WDS_PEER,
11172 .doit = nl80211_set_wds_peer,
11173 .policy = nl80211_policy,
11174 .flags = GENL_ADMIN_PERM,
11175 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11176 NL80211_FLAG_NEED_RTNL,
11179 .cmd = NL80211_CMD_JOIN_MESH,
11180 .doit = nl80211_join_mesh,
11181 .policy = nl80211_policy,
11182 .flags = GENL_ADMIN_PERM,
11183 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11184 NL80211_FLAG_NEED_RTNL,
11187 .cmd = NL80211_CMD_LEAVE_MESH,
11188 .doit = nl80211_leave_mesh,
11189 .policy = nl80211_policy,
11190 .flags = GENL_ADMIN_PERM,
11191 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11192 NL80211_FLAG_NEED_RTNL,
11195 .cmd = NL80211_CMD_JOIN_OCB,
11196 .doit = nl80211_join_ocb,
11197 .policy = nl80211_policy,
11198 .flags = GENL_ADMIN_PERM,
11199 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11200 NL80211_FLAG_NEED_RTNL,
11203 .cmd = NL80211_CMD_LEAVE_OCB,
11204 .doit = nl80211_leave_ocb,
11205 .policy = nl80211_policy,
11206 .flags = GENL_ADMIN_PERM,
11207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11208 NL80211_FLAG_NEED_RTNL,
11210 #ifdef CONFIG_PM
11212 .cmd = NL80211_CMD_GET_WOWLAN,
11213 .doit = nl80211_get_wowlan,
11214 .policy = nl80211_policy,
11215 /* can be retrieved by unprivileged users */
11216 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11217 NL80211_FLAG_NEED_RTNL,
11220 .cmd = NL80211_CMD_SET_WOWLAN,
11221 .doit = nl80211_set_wowlan,
11222 .policy = nl80211_policy,
11223 .flags = GENL_ADMIN_PERM,
11224 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11225 NL80211_FLAG_NEED_RTNL,
11227 #endif
11229 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
11230 .doit = nl80211_set_rekey_data,
11231 .policy = nl80211_policy,
11232 .flags = GENL_ADMIN_PERM,
11233 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11234 NL80211_FLAG_NEED_RTNL |
11235 NL80211_FLAG_CLEAR_SKB,
11238 .cmd = NL80211_CMD_TDLS_MGMT,
11239 .doit = nl80211_tdls_mgmt,
11240 .policy = nl80211_policy,
11241 .flags = GENL_ADMIN_PERM,
11242 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11243 NL80211_FLAG_NEED_RTNL,
11246 .cmd = NL80211_CMD_TDLS_OPER,
11247 .doit = nl80211_tdls_oper,
11248 .policy = nl80211_policy,
11249 .flags = GENL_ADMIN_PERM,
11250 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11251 NL80211_FLAG_NEED_RTNL,
11254 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
11255 .doit = nl80211_register_unexpected_frame,
11256 .policy = nl80211_policy,
11257 .flags = GENL_ADMIN_PERM,
11258 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11259 NL80211_FLAG_NEED_RTNL,
11262 .cmd = NL80211_CMD_PROBE_CLIENT,
11263 .doit = nl80211_probe_client,
11264 .policy = nl80211_policy,
11265 .flags = GENL_ADMIN_PERM,
11266 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11267 NL80211_FLAG_NEED_RTNL,
11270 .cmd = NL80211_CMD_REGISTER_BEACONS,
11271 .doit = nl80211_register_beacons,
11272 .policy = nl80211_policy,
11273 .flags = GENL_ADMIN_PERM,
11274 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11275 NL80211_FLAG_NEED_RTNL,
11278 .cmd = NL80211_CMD_SET_NOACK_MAP,
11279 .doit = nl80211_set_noack_map,
11280 .policy = nl80211_policy,
11281 .flags = GENL_ADMIN_PERM,
11282 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11283 NL80211_FLAG_NEED_RTNL,
11286 .cmd = NL80211_CMD_START_P2P_DEVICE,
11287 .doit = nl80211_start_p2p_device,
11288 .policy = nl80211_policy,
11289 .flags = GENL_ADMIN_PERM,
11290 .internal_flags = NL80211_FLAG_NEED_WDEV |
11291 NL80211_FLAG_NEED_RTNL,
11294 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
11295 .doit = nl80211_stop_p2p_device,
11296 .policy = nl80211_policy,
11297 .flags = GENL_ADMIN_PERM,
11298 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11299 NL80211_FLAG_NEED_RTNL,
11302 .cmd = NL80211_CMD_SET_MCAST_RATE,
11303 .doit = nl80211_set_mcast_rate,
11304 .policy = nl80211_policy,
11305 .flags = GENL_ADMIN_PERM,
11306 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11307 NL80211_FLAG_NEED_RTNL,
11310 .cmd = NL80211_CMD_SET_MAC_ACL,
11311 .doit = nl80211_set_mac_acl,
11312 .policy = nl80211_policy,
11313 .flags = GENL_ADMIN_PERM,
11314 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11315 NL80211_FLAG_NEED_RTNL,
11318 .cmd = NL80211_CMD_RADAR_DETECT,
11319 .doit = nl80211_start_radar_detection,
11320 .policy = nl80211_policy,
11321 .flags = GENL_ADMIN_PERM,
11322 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11323 NL80211_FLAG_NEED_RTNL,
11326 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
11327 .doit = nl80211_get_protocol_features,
11328 .policy = nl80211_policy,
11331 .cmd = NL80211_CMD_UPDATE_FT_IES,
11332 .doit = nl80211_update_ft_ies,
11333 .policy = nl80211_policy,
11334 .flags = GENL_ADMIN_PERM,
11335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11336 NL80211_FLAG_NEED_RTNL,
11339 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
11340 .doit = nl80211_crit_protocol_start,
11341 .policy = nl80211_policy,
11342 .flags = GENL_ADMIN_PERM,
11343 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11344 NL80211_FLAG_NEED_RTNL,
11347 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
11348 .doit = nl80211_crit_protocol_stop,
11349 .policy = nl80211_policy,
11350 .flags = GENL_ADMIN_PERM,
11351 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11352 NL80211_FLAG_NEED_RTNL,
11355 .cmd = NL80211_CMD_GET_COALESCE,
11356 .doit = nl80211_get_coalesce,
11357 .policy = nl80211_policy,
11358 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11359 NL80211_FLAG_NEED_RTNL,
11362 .cmd = NL80211_CMD_SET_COALESCE,
11363 .doit = nl80211_set_coalesce,
11364 .policy = nl80211_policy,
11365 .flags = GENL_ADMIN_PERM,
11366 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11367 NL80211_FLAG_NEED_RTNL,
11370 .cmd = NL80211_CMD_CHANNEL_SWITCH,
11371 .doit = nl80211_channel_switch,
11372 .policy = nl80211_policy,
11373 .flags = GENL_ADMIN_PERM,
11374 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11375 NL80211_FLAG_NEED_RTNL,
11378 .cmd = NL80211_CMD_VENDOR,
11379 .doit = nl80211_vendor_cmd,
11380 .dumpit = nl80211_vendor_cmd_dump,
11381 .policy = nl80211_policy,
11382 .flags = GENL_ADMIN_PERM,
11383 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11384 NL80211_FLAG_NEED_RTNL,
11387 .cmd = NL80211_CMD_SET_QOS_MAP,
11388 .doit = nl80211_set_qos_map,
11389 .policy = nl80211_policy,
11390 .flags = GENL_ADMIN_PERM,
11391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11392 NL80211_FLAG_NEED_RTNL,
11395 .cmd = NL80211_CMD_ADD_TX_TS,
11396 .doit = nl80211_add_tx_ts,
11397 .policy = nl80211_policy,
11398 .flags = GENL_ADMIN_PERM,
11399 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11400 NL80211_FLAG_NEED_RTNL,
11403 .cmd = NL80211_CMD_DEL_TX_TS,
11404 .doit = nl80211_del_tx_ts,
11405 .policy = nl80211_policy,
11406 .flags = GENL_ADMIN_PERM,
11407 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11408 NL80211_FLAG_NEED_RTNL,
11411 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
11412 .doit = nl80211_tdls_channel_switch,
11413 .policy = nl80211_policy,
11414 .flags = GENL_ADMIN_PERM,
11415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11416 NL80211_FLAG_NEED_RTNL,
11419 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
11420 .doit = nl80211_tdls_cancel_channel_switch,
11421 .policy = nl80211_policy,
11422 .flags = GENL_ADMIN_PERM,
11423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11424 NL80211_FLAG_NEED_RTNL,
11428 /* notification functions */
11430 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
11431 enum nl80211_commands cmd)
11433 struct sk_buff *msg;
11434 struct nl80211_dump_wiphy_state state = {};
11436 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
11437 cmd != NL80211_CMD_DEL_WIPHY);
11439 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11440 if (!msg)
11441 return;
11443 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
11444 nlmsg_free(msg);
11445 return;
11448 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11449 NL80211_MCGRP_CONFIG, GFP_KERNEL);
11452 static int nl80211_add_scan_req(struct sk_buff *msg,
11453 struct cfg80211_registered_device *rdev)
11455 struct cfg80211_scan_request *req = rdev->scan_req;
11456 struct nlattr *nest;
11457 int i;
11459 if (WARN_ON(!req))
11460 return 0;
11462 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
11463 if (!nest)
11464 goto nla_put_failure;
11465 for (i = 0; i < req->n_ssids; i++) {
11466 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
11467 goto nla_put_failure;
11469 nla_nest_end(msg, nest);
11471 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11472 if (!nest)
11473 goto nla_put_failure;
11474 for (i = 0; i < req->n_channels; i++) {
11475 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11476 goto nla_put_failure;
11478 nla_nest_end(msg, nest);
11480 if (req->ie &&
11481 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
11482 goto nla_put_failure;
11484 if (req->flags &&
11485 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
11486 goto nla_put_failure;
11488 return 0;
11489 nla_put_failure:
11490 return -ENOBUFS;
11493 static int nl80211_send_scan_msg(struct sk_buff *msg,
11494 struct cfg80211_registered_device *rdev,
11495 struct wireless_dev *wdev,
11496 u32 portid, u32 seq, int flags,
11497 u32 cmd)
11499 void *hdr;
11501 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
11502 if (!hdr)
11503 return -1;
11505 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11506 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11507 wdev->netdev->ifindex)) ||
11508 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11509 goto nla_put_failure;
11511 /* ignore errors and send incomplete event anyway */
11512 nl80211_add_scan_req(msg, rdev);
11514 genlmsg_end(msg, hdr);
11515 return 0;
11517 nla_put_failure:
11518 genlmsg_cancel(msg, hdr);
11519 return -EMSGSIZE;
11522 static int
11523 nl80211_send_sched_scan_msg(struct sk_buff *msg,
11524 struct cfg80211_registered_device *rdev,
11525 struct net_device *netdev,
11526 u32 portid, u32 seq, int flags, u32 cmd)
11528 void *hdr;
11530 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
11531 if (!hdr)
11532 return -1;
11534 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11535 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11536 goto nla_put_failure;
11538 genlmsg_end(msg, hdr);
11539 return 0;
11541 nla_put_failure:
11542 genlmsg_cancel(msg, hdr);
11543 return -EMSGSIZE;
11546 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
11547 struct wireless_dev *wdev)
11549 struct sk_buff *msg;
11551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11552 if (!msg)
11553 return;
11555 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
11556 NL80211_CMD_TRIGGER_SCAN) < 0) {
11557 nlmsg_free(msg);
11558 return;
11561 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11562 NL80211_MCGRP_SCAN, GFP_KERNEL);
11565 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
11566 struct wireless_dev *wdev, bool aborted)
11568 struct sk_buff *msg;
11570 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11571 if (!msg)
11572 return NULL;
11574 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
11575 aborted ? NL80211_CMD_SCAN_ABORTED :
11576 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
11577 nlmsg_free(msg);
11578 return NULL;
11581 return msg;
11584 void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
11585 struct sk_buff *msg)
11587 if (!msg)
11588 return;
11590 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11591 NL80211_MCGRP_SCAN, GFP_KERNEL);
11594 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
11595 struct net_device *netdev)
11597 struct sk_buff *msg;
11599 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11600 if (!msg)
11601 return;
11603 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
11604 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
11605 nlmsg_free(msg);
11606 return;
11609 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11610 NL80211_MCGRP_SCAN, GFP_KERNEL);
11613 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
11614 struct net_device *netdev, u32 cmd)
11616 struct sk_buff *msg;
11618 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11619 if (!msg)
11620 return;
11622 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
11623 nlmsg_free(msg);
11624 return;
11627 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11628 NL80211_MCGRP_SCAN, GFP_KERNEL);
11631 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
11632 struct regulatory_request *request)
11634 /* Userspace can always count this one always being set */
11635 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
11636 goto nla_put_failure;
11638 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
11639 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11640 NL80211_REGDOM_TYPE_WORLD))
11641 goto nla_put_failure;
11642 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
11643 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11644 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
11645 goto nla_put_failure;
11646 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
11647 request->intersect) {
11648 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11649 NL80211_REGDOM_TYPE_INTERSECTION))
11650 goto nla_put_failure;
11651 } else {
11652 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11653 NL80211_REGDOM_TYPE_COUNTRY) ||
11654 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
11655 request->alpha2))
11656 goto nla_put_failure;
11659 if (request->wiphy_idx != WIPHY_IDX_INVALID) {
11660 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
11662 if (wiphy &&
11663 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
11664 goto nla_put_failure;
11666 if (wiphy &&
11667 wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
11668 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
11669 goto nla_put_failure;
11672 return true;
11674 nla_put_failure:
11675 return false;
11679 * This can happen on global regulatory changes or device specific settings
11680 * based on custom regulatory domains.
11682 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
11683 struct regulatory_request *request)
11685 struct sk_buff *msg;
11686 void *hdr;
11688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11689 if (!msg)
11690 return;
11692 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
11693 if (!hdr) {
11694 nlmsg_free(msg);
11695 return;
11698 if (nl80211_reg_change_event_fill(msg, request) == false)
11699 goto nla_put_failure;
11701 genlmsg_end(msg, hdr);
11703 rcu_read_lock();
11704 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
11705 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
11706 rcu_read_unlock();
11708 return;
11710 nla_put_failure:
11711 genlmsg_cancel(msg, hdr);
11712 nlmsg_free(msg);
11715 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
11716 struct net_device *netdev,
11717 const u8 *buf, size_t len,
11718 enum nl80211_commands cmd, gfp_t gfp,
11719 int uapsd_queues)
11721 struct sk_buff *msg;
11722 void *hdr;
11724 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11725 if (!msg)
11726 return;
11728 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11729 if (!hdr) {
11730 nlmsg_free(msg);
11731 return;
11734 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11735 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11736 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
11737 goto nla_put_failure;
11739 if (uapsd_queues >= 0) {
11740 struct nlattr *nla_wmm =
11741 nla_nest_start(msg, NL80211_ATTR_STA_WME);
11742 if (!nla_wmm)
11743 goto nla_put_failure;
11745 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
11746 uapsd_queues))
11747 goto nla_put_failure;
11749 nla_nest_end(msg, nla_wmm);
11752 genlmsg_end(msg, hdr);
11754 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11755 NL80211_MCGRP_MLME, gfp);
11756 return;
11758 nla_put_failure:
11759 genlmsg_cancel(msg, hdr);
11760 nlmsg_free(msg);
11763 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
11764 struct net_device *netdev, const u8 *buf,
11765 size_t len, gfp_t gfp)
11767 nl80211_send_mlme_event(rdev, netdev, buf, len,
11768 NL80211_CMD_AUTHENTICATE, gfp, -1);
11771 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
11772 struct net_device *netdev, const u8 *buf,
11773 size_t len, gfp_t gfp, int uapsd_queues)
11775 nl80211_send_mlme_event(rdev, netdev, buf, len,
11776 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
11779 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
11780 struct net_device *netdev, const u8 *buf,
11781 size_t len, gfp_t gfp)
11783 nl80211_send_mlme_event(rdev, netdev, buf, len,
11784 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
11787 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
11788 struct net_device *netdev, const u8 *buf,
11789 size_t len, gfp_t gfp)
11791 nl80211_send_mlme_event(rdev, netdev, buf, len,
11792 NL80211_CMD_DISASSOCIATE, gfp, -1);
11795 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
11796 size_t len)
11798 struct wireless_dev *wdev = dev->ieee80211_ptr;
11799 struct wiphy *wiphy = wdev->wiphy;
11800 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11801 const struct ieee80211_mgmt *mgmt = (void *)buf;
11802 u32 cmd;
11804 if (WARN_ON(len < 2))
11805 return;
11807 if (ieee80211_is_deauth(mgmt->frame_control))
11808 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
11809 else
11810 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
11812 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
11813 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
11815 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
11817 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
11818 struct net_device *netdev, int cmd,
11819 const u8 *addr, gfp_t gfp)
11821 struct sk_buff *msg;
11822 void *hdr;
11824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11825 if (!msg)
11826 return;
11828 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11829 if (!hdr) {
11830 nlmsg_free(msg);
11831 return;
11834 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11835 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11836 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
11837 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11838 goto nla_put_failure;
11840 genlmsg_end(msg, hdr);
11842 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11843 NL80211_MCGRP_MLME, gfp);
11844 return;
11846 nla_put_failure:
11847 genlmsg_cancel(msg, hdr);
11848 nlmsg_free(msg);
11851 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
11852 struct net_device *netdev, const u8 *addr,
11853 gfp_t gfp)
11855 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
11856 addr, gfp);
11859 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
11860 struct net_device *netdev, const u8 *addr,
11861 gfp_t gfp)
11863 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
11864 addr, gfp);
11867 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
11868 struct net_device *netdev, const u8 *bssid,
11869 const u8 *req_ie, size_t req_ie_len,
11870 const u8 *resp_ie, size_t resp_ie_len,
11871 u16 status, gfp_t gfp)
11873 struct sk_buff *msg;
11874 void *hdr;
11876 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11877 if (!msg)
11878 return;
11880 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
11881 if (!hdr) {
11882 nlmsg_free(msg);
11883 return;
11886 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11887 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11888 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
11889 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
11890 (req_ie &&
11891 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11892 (resp_ie &&
11893 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11894 goto nla_put_failure;
11896 genlmsg_end(msg, hdr);
11898 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11899 NL80211_MCGRP_MLME, gfp);
11900 return;
11902 nla_put_failure:
11903 genlmsg_cancel(msg, hdr);
11904 nlmsg_free(msg);
11908 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
11909 struct net_device *netdev, const u8 *bssid,
11910 const u8 *req_ie, size_t req_ie_len,
11911 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
11913 struct sk_buff *msg;
11914 void *hdr;
11916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11917 if (!msg)
11918 return;
11920 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
11921 if (!hdr) {
11922 nlmsg_free(msg);
11923 return;
11926 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11927 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11928 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
11929 (req_ie &&
11930 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11931 (resp_ie &&
11932 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11933 goto nla_put_failure;
11935 genlmsg_end(msg, hdr);
11937 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11938 NL80211_MCGRP_MLME, gfp);
11939 return;
11941 nla_put_failure:
11942 genlmsg_cancel(msg, hdr);
11943 nlmsg_free(msg);
11947 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
11948 struct net_device *netdev, u16 reason,
11949 const u8 *ie, size_t ie_len, bool from_ap)
11951 struct sk_buff *msg;
11952 void *hdr;
11954 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11955 if (!msg)
11956 return;
11958 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
11959 if (!hdr) {
11960 nlmsg_free(msg);
11961 return;
11964 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11965 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11966 (from_ap && reason &&
11967 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
11968 (from_ap &&
11969 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
11970 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
11971 goto nla_put_failure;
11973 genlmsg_end(msg, hdr);
11975 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11976 NL80211_MCGRP_MLME, GFP_KERNEL);
11977 return;
11979 nla_put_failure:
11980 genlmsg_cancel(msg, hdr);
11981 nlmsg_free(msg);
11985 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
11986 struct net_device *netdev, const u8 *bssid,
11987 gfp_t gfp)
11989 struct sk_buff *msg;
11990 void *hdr;
11992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11993 if (!msg)
11994 return;
11996 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
11997 if (!hdr) {
11998 nlmsg_free(msg);
11999 return;
12002 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12003 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12004 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
12005 goto nla_put_failure;
12007 genlmsg_end(msg, hdr);
12009 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12010 NL80211_MCGRP_MLME, gfp);
12011 return;
12013 nla_put_failure:
12014 genlmsg_cancel(msg, hdr);
12015 nlmsg_free(msg);
12018 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
12019 const u8* ie, u8 ie_len, gfp_t gfp)
12021 struct wireless_dev *wdev = dev->ieee80211_ptr;
12022 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12023 struct sk_buff *msg;
12024 void *hdr;
12026 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
12027 return;
12029 trace_cfg80211_notify_new_peer_candidate(dev, addr);
12031 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12032 if (!msg)
12033 return;
12035 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
12036 if (!hdr) {
12037 nlmsg_free(msg);
12038 return;
12041 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12042 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12043 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12044 (ie_len && ie &&
12045 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
12046 goto nla_put_failure;
12048 genlmsg_end(msg, hdr);
12050 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12051 NL80211_MCGRP_MLME, gfp);
12052 return;
12054 nla_put_failure:
12055 genlmsg_cancel(msg, hdr);
12056 nlmsg_free(msg);
12058 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
12060 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
12061 struct net_device *netdev, const u8 *addr,
12062 enum nl80211_key_type key_type, int key_id,
12063 const u8 *tsc, gfp_t gfp)
12065 struct sk_buff *msg;
12066 void *hdr;
12068 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12069 if (!msg)
12070 return;
12072 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
12073 if (!hdr) {
12074 nlmsg_free(msg);
12075 return;
12078 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12079 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12080 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
12081 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
12082 (key_id != -1 &&
12083 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
12084 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
12085 goto nla_put_failure;
12087 genlmsg_end(msg, hdr);
12089 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12090 NL80211_MCGRP_MLME, gfp);
12091 return;
12093 nla_put_failure:
12094 genlmsg_cancel(msg, hdr);
12095 nlmsg_free(msg);
12098 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
12099 struct ieee80211_channel *channel_before,
12100 struct ieee80211_channel *channel_after)
12102 struct sk_buff *msg;
12103 void *hdr;
12104 struct nlattr *nl_freq;
12106 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
12107 if (!msg)
12108 return;
12110 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
12111 if (!hdr) {
12112 nlmsg_free(msg);
12113 return;
12117 * Since we are applying the beacon hint to a wiphy we know its
12118 * wiphy_idx is valid
12120 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
12121 goto nla_put_failure;
12123 /* Before */
12124 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
12125 if (!nl_freq)
12126 goto nla_put_failure;
12127 if (nl80211_msg_put_channel(msg, channel_before, false))
12128 goto nla_put_failure;
12129 nla_nest_end(msg, nl_freq);
12131 /* After */
12132 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
12133 if (!nl_freq)
12134 goto nla_put_failure;
12135 if (nl80211_msg_put_channel(msg, channel_after, false))
12136 goto nla_put_failure;
12137 nla_nest_end(msg, nl_freq);
12139 genlmsg_end(msg, hdr);
12141 rcu_read_lock();
12142 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
12143 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
12144 rcu_read_unlock();
12146 return;
12148 nla_put_failure:
12149 genlmsg_cancel(msg, hdr);
12150 nlmsg_free(msg);
12153 static void nl80211_send_remain_on_chan_event(
12154 int cmd, struct cfg80211_registered_device *rdev,
12155 struct wireless_dev *wdev, u64 cookie,
12156 struct ieee80211_channel *chan,
12157 unsigned int duration, gfp_t gfp)
12159 struct sk_buff *msg;
12160 void *hdr;
12162 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12163 if (!msg)
12164 return;
12166 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
12167 if (!hdr) {
12168 nlmsg_free(msg);
12169 return;
12172 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12173 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12174 wdev->netdev->ifindex)) ||
12175 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12176 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
12177 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
12178 NL80211_CHAN_NO_HT) ||
12179 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
12180 goto nla_put_failure;
12182 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
12183 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
12184 goto nla_put_failure;
12186 genlmsg_end(msg, hdr);
12188 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12189 NL80211_MCGRP_MLME, gfp);
12190 return;
12192 nla_put_failure:
12193 genlmsg_cancel(msg, hdr);
12194 nlmsg_free(msg);
12197 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
12198 struct ieee80211_channel *chan,
12199 unsigned int duration, gfp_t gfp)
12201 struct wiphy *wiphy = wdev->wiphy;
12202 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12204 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
12205 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
12206 rdev, wdev, cookie, chan,
12207 duration, gfp);
12209 EXPORT_SYMBOL(cfg80211_ready_on_channel);
12211 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
12212 struct ieee80211_channel *chan,
12213 gfp_t gfp)
12215 struct wiphy *wiphy = wdev->wiphy;
12216 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12218 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
12219 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
12220 rdev, wdev, cookie, chan, 0, gfp);
12222 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
12224 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
12225 struct station_info *sinfo, gfp_t gfp)
12227 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12228 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12229 struct sk_buff *msg;
12231 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
12233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12234 if (!msg)
12235 return;
12237 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
12238 rdev, dev, mac_addr, sinfo) < 0) {
12239 nlmsg_free(msg);
12240 return;
12243 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12244 NL80211_MCGRP_MLME, gfp);
12246 EXPORT_SYMBOL(cfg80211_new_sta);
12248 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
12249 struct station_info *sinfo, gfp_t gfp)
12251 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12252 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12253 struct sk_buff *msg;
12254 struct station_info empty_sinfo = {};
12256 if (!sinfo)
12257 sinfo = &empty_sinfo;
12259 trace_cfg80211_del_sta(dev, mac_addr);
12261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12262 if (!msg)
12263 return;
12265 if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
12266 rdev, dev, mac_addr, sinfo) < 0) {
12267 nlmsg_free(msg);
12268 return;
12271 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12272 NL80211_MCGRP_MLME, gfp);
12274 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
12276 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
12277 enum nl80211_connect_failed_reason reason,
12278 gfp_t gfp)
12280 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12281 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12282 struct sk_buff *msg;
12283 void *hdr;
12285 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
12286 if (!msg)
12287 return;
12289 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
12290 if (!hdr) {
12291 nlmsg_free(msg);
12292 return;
12295 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12296 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
12297 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
12298 goto nla_put_failure;
12300 genlmsg_end(msg, hdr);
12302 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12303 NL80211_MCGRP_MLME, gfp);
12304 return;
12306 nla_put_failure:
12307 genlmsg_cancel(msg, hdr);
12308 nlmsg_free(msg);
12310 EXPORT_SYMBOL(cfg80211_conn_failed);
12312 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
12313 const u8 *addr, gfp_t gfp)
12315 struct wireless_dev *wdev = dev->ieee80211_ptr;
12316 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12317 struct sk_buff *msg;
12318 void *hdr;
12319 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
12321 if (!nlportid)
12322 return false;
12324 msg = nlmsg_new(100, gfp);
12325 if (!msg)
12326 return true;
12328 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
12329 if (!hdr) {
12330 nlmsg_free(msg);
12331 return true;
12334 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12335 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12336 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
12337 goto nla_put_failure;
12339 genlmsg_end(msg, hdr);
12340 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12341 return true;
12343 nla_put_failure:
12344 genlmsg_cancel(msg, hdr);
12345 nlmsg_free(msg);
12346 return true;
12349 bool cfg80211_rx_spurious_frame(struct net_device *dev,
12350 const u8 *addr, gfp_t gfp)
12352 struct wireless_dev *wdev = dev->ieee80211_ptr;
12353 bool ret;
12355 trace_cfg80211_rx_spurious_frame(dev, addr);
12357 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
12358 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
12359 trace_cfg80211_return_bool(false);
12360 return false;
12362 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
12363 addr, gfp);
12364 trace_cfg80211_return_bool(ret);
12365 return ret;
12367 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
12369 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
12370 const u8 *addr, gfp_t gfp)
12372 struct wireless_dev *wdev = dev->ieee80211_ptr;
12373 bool ret;
12375 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
12377 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
12378 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
12379 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
12380 trace_cfg80211_return_bool(false);
12381 return false;
12383 ret = __nl80211_unexpected_frame(dev,
12384 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
12385 addr, gfp);
12386 trace_cfg80211_return_bool(ret);
12387 return ret;
12389 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
12391 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
12392 struct wireless_dev *wdev, u32 nlportid,
12393 int freq, int sig_dbm,
12394 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
12396 struct net_device *netdev = wdev->netdev;
12397 struct sk_buff *msg;
12398 void *hdr;
12400 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12401 if (!msg)
12402 return -ENOMEM;
12404 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12405 if (!hdr) {
12406 nlmsg_free(msg);
12407 return -ENOMEM;
12410 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12411 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12412 netdev->ifindex)) ||
12413 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12414 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
12415 (sig_dbm &&
12416 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12417 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
12418 (flags &&
12419 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
12420 goto nla_put_failure;
12422 genlmsg_end(msg, hdr);
12424 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12426 nla_put_failure:
12427 genlmsg_cancel(msg, hdr);
12428 nlmsg_free(msg);
12429 return -ENOBUFS;
12432 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
12433 const u8 *buf, size_t len, bool ack, gfp_t gfp)
12435 struct wiphy *wiphy = wdev->wiphy;
12436 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12437 struct net_device *netdev = wdev->netdev;
12438 struct sk_buff *msg;
12439 void *hdr;
12441 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
12443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12444 if (!msg)
12445 return;
12447 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
12448 if (!hdr) {
12449 nlmsg_free(msg);
12450 return;
12453 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12454 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12455 netdev->ifindex)) ||
12456 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12457 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
12458 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12459 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
12460 goto nla_put_failure;
12462 genlmsg_end(msg, hdr);
12464 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12465 NL80211_MCGRP_MLME, gfp);
12466 return;
12468 nla_put_failure:
12469 genlmsg_cancel(msg, hdr);
12470 nlmsg_free(msg);
12472 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
12474 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
12475 const char *mac, gfp_t gfp)
12477 struct wireless_dev *wdev = dev->ieee80211_ptr;
12478 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12479 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12480 void **cb;
12482 if (!msg)
12483 return NULL;
12485 cb = (void **)msg->cb;
12487 cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
12488 if (!cb[0]) {
12489 nlmsg_free(msg);
12490 return NULL;
12493 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12494 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
12495 goto nla_put_failure;
12497 if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
12498 goto nla_put_failure;
12500 cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
12501 if (!cb[1])
12502 goto nla_put_failure;
12504 cb[2] = rdev;
12506 return msg;
12507 nla_put_failure:
12508 nlmsg_free(msg);
12509 return NULL;
12512 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
12514 void **cb = (void **)msg->cb;
12515 struct cfg80211_registered_device *rdev = cb[2];
12517 nla_nest_end(msg, cb[1]);
12518 genlmsg_end(msg, cb[0]);
12520 memset(msg->cb, 0, sizeof(msg->cb));
12522 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12523 NL80211_MCGRP_MLME, gfp);
12526 void cfg80211_cqm_rssi_notify(struct net_device *dev,
12527 enum nl80211_cqm_rssi_threshold_event rssi_event,
12528 gfp_t gfp)
12530 struct sk_buff *msg;
12532 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
12534 if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
12535 rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
12536 return;
12538 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
12539 if (!msg)
12540 return;
12542 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
12543 rssi_event))
12544 goto nla_put_failure;
12546 cfg80211_send_cqm(msg, gfp);
12548 return;
12550 nla_put_failure:
12551 nlmsg_free(msg);
12553 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
12555 void cfg80211_cqm_txe_notify(struct net_device *dev,
12556 const u8 *peer, u32 num_packets,
12557 u32 rate, u32 intvl, gfp_t gfp)
12559 struct sk_buff *msg;
12561 msg = cfg80211_prepare_cqm(dev, peer, gfp);
12562 if (!msg)
12563 return;
12565 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
12566 goto nla_put_failure;
12568 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
12569 goto nla_put_failure;
12571 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
12572 goto nla_put_failure;
12574 cfg80211_send_cqm(msg, gfp);
12575 return;
12577 nla_put_failure:
12578 nlmsg_free(msg);
12580 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
12582 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
12583 const u8 *peer, u32 num_packets, gfp_t gfp)
12585 struct sk_buff *msg;
12587 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
12589 msg = cfg80211_prepare_cqm(dev, peer, gfp);
12590 if (!msg)
12591 return;
12593 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
12594 goto nla_put_failure;
12596 cfg80211_send_cqm(msg, gfp);
12597 return;
12599 nla_put_failure:
12600 nlmsg_free(msg);
12602 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
12604 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
12606 struct sk_buff *msg;
12608 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
12609 if (!msg)
12610 return;
12612 if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
12613 goto nla_put_failure;
12615 cfg80211_send_cqm(msg, gfp);
12616 return;
12618 nla_put_failure:
12619 nlmsg_free(msg);
12621 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
12623 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
12624 struct net_device *netdev, const u8 *bssid,
12625 const u8 *replay_ctr, gfp_t gfp)
12627 struct sk_buff *msg;
12628 struct nlattr *rekey_attr;
12629 void *hdr;
12631 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12632 if (!msg)
12633 return;
12635 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
12636 if (!hdr) {
12637 nlmsg_free(msg);
12638 return;
12641 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12642 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12643 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
12644 goto nla_put_failure;
12646 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
12647 if (!rekey_attr)
12648 goto nla_put_failure;
12650 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
12651 NL80211_REPLAY_CTR_LEN, replay_ctr))
12652 goto nla_put_failure;
12654 nla_nest_end(msg, rekey_attr);
12656 genlmsg_end(msg, hdr);
12658 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12659 NL80211_MCGRP_MLME, gfp);
12660 return;
12662 nla_put_failure:
12663 genlmsg_cancel(msg, hdr);
12664 nlmsg_free(msg);
12667 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
12668 const u8 *replay_ctr, gfp_t gfp)
12670 struct wireless_dev *wdev = dev->ieee80211_ptr;
12671 struct wiphy *wiphy = wdev->wiphy;
12672 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12674 trace_cfg80211_gtk_rekey_notify(dev, bssid);
12675 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
12677 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
12679 static void
12680 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
12681 struct net_device *netdev, int index,
12682 const u8 *bssid, bool preauth, gfp_t gfp)
12684 struct sk_buff *msg;
12685 struct nlattr *attr;
12686 void *hdr;
12688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12689 if (!msg)
12690 return;
12692 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
12693 if (!hdr) {
12694 nlmsg_free(msg);
12695 return;
12698 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12699 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
12700 goto nla_put_failure;
12702 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
12703 if (!attr)
12704 goto nla_put_failure;
12706 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
12707 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
12708 (preauth &&
12709 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
12710 goto nla_put_failure;
12712 nla_nest_end(msg, attr);
12714 genlmsg_end(msg, hdr);
12716 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12717 NL80211_MCGRP_MLME, gfp);
12718 return;
12720 nla_put_failure:
12721 genlmsg_cancel(msg, hdr);
12722 nlmsg_free(msg);
12725 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
12726 const u8 *bssid, bool preauth, gfp_t gfp)
12728 struct wireless_dev *wdev = dev->ieee80211_ptr;
12729 struct wiphy *wiphy = wdev->wiphy;
12730 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12732 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
12733 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
12735 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
12737 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
12738 struct net_device *netdev,
12739 struct cfg80211_chan_def *chandef,
12740 gfp_t gfp,
12741 enum nl80211_commands notif,
12742 u8 count)
12744 struct sk_buff *msg;
12745 void *hdr;
12747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12748 if (!msg)
12749 return;
12751 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
12752 if (!hdr) {
12753 nlmsg_free(msg);
12754 return;
12757 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
12758 goto nla_put_failure;
12760 if (nl80211_send_chandef(msg, chandef))
12761 goto nla_put_failure;
12763 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
12764 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
12765 goto nla_put_failure;
12767 genlmsg_end(msg, hdr);
12769 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12770 NL80211_MCGRP_MLME, gfp);
12771 return;
12773 nla_put_failure:
12774 genlmsg_cancel(msg, hdr);
12775 nlmsg_free(msg);
12778 void cfg80211_ch_switch_notify(struct net_device *dev,
12779 struct cfg80211_chan_def *chandef)
12781 struct wireless_dev *wdev = dev->ieee80211_ptr;
12782 struct wiphy *wiphy = wdev->wiphy;
12783 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12785 ASSERT_WDEV_LOCK(wdev);
12787 trace_cfg80211_ch_switch_notify(dev, chandef);
12789 wdev->chandef = *chandef;
12790 wdev->preset_chandef = *chandef;
12791 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12792 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
12794 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
12796 void cfg80211_ch_switch_started_notify(struct net_device *dev,
12797 struct cfg80211_chan_def *chandef,
12798 u8 count)
12800 struct wireless_dev *wdev = dev->ieee80211_ptr;
12801 struct wiphy *wiphy = wdev->wiphy;
12802 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12804 trace_cfg80211_ch_switch_started_notify(dev, chandef);
12806 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12807 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
12809 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
12811 void
12812 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
12813 const struct cfg80211_chan_def *chandef,
12814 enum nl80211_radar_event event,
12815 struct net_device *netdev, gfp_t gfp)
12817 struct sk_buff *msg;
12818 void *hdr;
12820 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12821 if (!msg)
12822 return;
12824 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
12825 if (!hdr) {
12826 nlmsg_free(msg);
12827 return;
12830 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
12831 goto nla_put_failure;
12833 /* NOP and radar events don't need a netdev parameter */
12834 if (netdev) {
12835 struct wireless_dev *wdev = netdev->ieee80211_ptr;
12837 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12838 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12839 goto nla_put_failure;
12842 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
12843 goto nla_put_failure;
12845 if (nl80211_send_chandef(msg, chandef))
12846 goto nla_put_failure;
12848 genlmsg_end(msg, hdr);
12850 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12851 NL80211_MCGRP_MLME, gfp);
12852 return;
12854 nla_put_failure:
12855 genlmsg_cancel(msg, hdr);
12856 nlmsg_free(msg);
12859 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
12860 u64 cookie, bool acked, gfp_t gfp)
12862 struct wireless_dev *wdev = dev->ieee80211_ptr;
12863 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12864 struct sk_buff *msg;
12865 void *hdr;
12867 trace_cfg80211_probe_status(dev, addr, cookie, acked);
12869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12871 if (!msg)
12872 return;
12874 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
12875 if (!hdr) {
12876 nlmsg_free(msg);
12877 return;
12880 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12881 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12882 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12883 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12884 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
12885 goto nla_put_failure;
12887 genlmsg_end(msg, hdr);
12889 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12890 NL80211_MCGRP_MLME, gfp);
12891 return;
12893 nla_put_failure:
12894 genlmsg_cancel(msg, hdr);
12895 nlmsg_free(msg);
12897 EXPORT_SYMBOL(cfg80211_probe_status);
12899 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
12900 const u8 *frame, size_t len,
12901 int freq, int sig_dbm)
12903 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12904 struct sk_buff *msg;
12905 void *hdr;
12906 struct cfg80211_beacon_registration *reg;
12908 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
12910 spin_lock_bh(&rdev->beacon_registrations_lock);
12911 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12912 msg = nlmsg_new(len + 100, GFP_ATOMIC);
12913 if (!msg) {
12914 spin_unlock_bh(&rdev->beacon_registrations_lock);
12915 return;
12918 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12919 if (!hdr)
12920 goto nla_put_failure;
12922 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12923 (freq &&
12924 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
12925 (sig_dbm &&
12926 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12927 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
12928 goto nla_put_failure;
12930 genlmsg_end(msg, hdr);
12932 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
12934 spin_unlock_bh(&rdev->beacon_registrations_lock);
12935 return;
12937 nla_put_failure:
12938 spin_unlock_bh(&rdev->beacon_registrations_lock);
12939 if (hdr)
12940 genlmsg_cancel(msg, hdr);
12941 nlmsg_free(msg);
12943 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
12945 #ifdef CONFIG_PM
12946 static int cfg80211_net_detect_results(struct sk_buff *msg,
12947 struct cfg80211_wowlan_wakeup *wakeup)
12949 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
12950 struct nlattr *nl_results, *nl_match, *nl_freqs;
12951 int i, j;
12953 nl_results = nla_nest_start(
12954 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
12955 if (!nl_results)
12956 return -EMSGSIZE;
12958 for (i = 0; i < nd->n_matches; i++) {
12959 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
12961 nl_match = nla_nest_start(msg, i);
12962 if (!nl_match)
12963 break;
12965 /* The SSID attribute is optional in nl80211, but for
12966 * simplicity reasons it's always present in the
12967 * cfg80211 structure. If a driver can't pass the
12968 * SSID, that needs to be changed. A zero length SSID
12969 * is still a valid SSID (wildcard), so it cannot be
12970 * used for this purpose.
12972 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
12973 match->ssid.ssid)) {
12974 nla_nest_cancel(msg, nl_match);
12975 goto out;
12978 if (match->n_channels) {
12979 nl_freqs = nla_nest_start(
12980 msg, NL80211_ATTR_SCAN_FREQUENCIES);
12981 if (!nl_freqs) {
12982 nla_nest_cancel(msg, nl_match);
12983 goto out;
12986 for (j = 0; j < match->n_channels; j++) {
12987 if (nla_put_u32(msg, j, match->channels[j])) {
12988 nla_nest_cancel(msg, nl_freqs);
12989 nla_nest_cancel(msg, nl_match);
12990 goto out;
12994 nla_nest_end(msg, nl_freqs);
12997 nla_nest_end(msg, nl_match);
13000 out:
13001 nla_nest_end(msg, nl_results);
13002 return 0;
13005 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
13006 struct cfg80211_wowlan_wakeup *wakeup,
13007 gfp_t gfp)
13009 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
13010 struct sk_buff *msg;
13011 void *hdr;
13012 int size = 200;
13014 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
13016 if (wakeup)
13017 size += wakeup->packet_present_len;
13019 msg = nlmsg_new(size, gfp);
13020 if (!msg)
13021 return;
13023 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
13024 if (!hdr)
13025 goto free_msg;
13027 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13028 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13029 goto free_msg;
13031 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13032 wdev->netdev->ifindex))
13033 goto free_msg;
13035 if (wakeup) {
13036 struct nlattr *reasons;
13038 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
13039 if (!reasons)
13040 goto free_msg;
13042 if (wakeup->disconnect &&
13043 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
13044 goto free_msg;
13045 if (wakeup->magic_pkt &&
13046 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
13047 goto free_msg;
13048 if (wakeup->gtk_rekey_failure &&
13049 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
13050 goto free_msg;
13051 if (wakeup->eap_identity_req &&
13052 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
13053 goto free_msg;
13054 if (wakeup->four_way_handshake &&
13055 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
13056 goto free_msg;
13057 if (wakeup->rfkill_release &&
13058 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
13059 goto free_msg;
13061 if (wakeup->pattern_idx >= 0 &&
13062 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
13063 wakeup->pattern_idx))
13064 goto free_msg;
13066 if (wakeup->tcp_match &&
13067 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
13068 goto free_msg;
13070 if (wakeup->tcp_connlost &&
13071 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
13072 goto free_msg;
13074 if (wakeup->tcp_nomoretokens &&
13075 nla_put_flag(msg,
13076 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
13077 goto free_msg;
13079 if (wakeup->packet) {
13080 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
13081 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
13083 if (!wakeup->packet_80211) {
13084 pkt_attr =
13085 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
13086 len_attr =
13087 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
13090 if (wakeup->packet_len &&
13091 nla_put_u32(msg, len_attr, wakeup->packet_len))
13092 goto free_msg;
13094 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
13095 wakeup->packet))
13096 goto free_msg;
13099 if (wakeup->net_detect &&
13100 cfg80211_net_detect_results(msg, wakeup))
13101 goto free_msg;
13103 nla_nest_end(msg, reasons);
13106 genlmsg_end(msg, hdr);
13108 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13109 NL80211_MCGRP_MLME, gfp);
13110 return;
13112 free_msg:
13113 nlmsg_free(msg);
13115 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
13116 #endif
13118 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
13119 enum nl80211_tdls_operation oper,
13120 u16 reason_code, gfp_t gfp)
13122 struct wireless_dev *wdev = dev->ieee80211_ptr;
13123 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
13124 struct sk_buff *msg;
13125 void *hdr;
13127 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
13128 reason_code);
13130 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13131 if (!msg)
13132 return;
13134 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
13135 if (!hdr) {
13136 nlmsg_free(msg);
13137 return;
13140 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13141 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
13142 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
13143 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
13144 (reason_code > 0 &&
13145 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
13146 goto nla_put_failure;
13148 genlmsg_end(msg, hdr);
13150 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13151 NL80211_MCGRP_MLME, gfp);
13152 return;
13154 nla_put_failure:
13155 genlmsg_cancel(msg, hdr);
13156 nlmsg_free(msg);
13158 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
13160 static int nl80211_netlink_notify(struct notifier_block * nb,
13161 unsigned long state,
13162 void *_notify)
13164 struct netlink_notify *notify = _notify;
13165 struct cfg80211_registered_device *rdev;
13166 struct wireless_dev *wdev;
13167 struct cfg80211_beacon_registration *reg, *tmp;
13169 if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
13170 return NOTIFY_DONE;
13172 rcu_read_lock();
13174 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
13175 bool schedule_destroy_work = false;
13176 struct cfg80211_sched_scan_request *sched_scan_req =
13177 rcu_dereference(rdev->sched_scan_req);
13179 if (sched_scan_req && notify->portid &&
13180 sched_scan_req->owner_nlportid == notify->portid) {
13181 sched_scan_req->owner_nlportid = 0;
13183 if (rdev->ops->sched_scan_stop &&
13184 rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
13185 schedule_work(&rdev->sched_scan_stop_wk);
13188 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
13189 cfg80211_mlme_unregister_socket(wdev, notify->portid);
13191 if (wdev->owner_nlportid == notify->portid)
13192 schedule_destroy_work = true;
13195 spin_lock_bh(&rdev->beacon_registrations_lock);
13196 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
13197 list) {
13198 if (reg->nlportid == notify->portid) {
13199 list_del(&reg->list);
13200 kfree(reg);
13201 break;
13204 spin_unlock_bh(&rdev->beacon_registrations_lock);
13206 if (schedule_destroy_work) {
13207 struct cfg80211_iface_destroy *destroy;
13209 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
13210 if (destroy) {
13211 destroy->nlportid = notify->portid;
13212 spin_lock(&rdev->destroy_list_lock);
13213 list_add(&destroy->list, &rdev->destroy_list);
13214 spin_unlock(&rdev->destroy_list_lock);
13215 schedule_work(&rdev->destroy_work);
13220 rcu_read_unlock();
13223 * It is possible that the user space process that is controlling the
13224 * indoor setting disappeared, so notify the regulatory core.
13226 regulatory_netlink_notify(notify->portid);
13227 return NOTIFY_OK;
13230 static struct notifier_block nl80211_netlink_notifier = {
13231 .notifier_call = nl80211_netlink_notify,
13234 void cfg80211_ft_event(struct net_device *netdev,
13235 struct cfg80211_ft_event_params *ft_event)
13237 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
13238 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13239 struct sk_buff *msg;
13240 void *hdr;
13242 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
13244 if (!ft_event->target_ap)
13245 return;
13247 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13248 if (!msg)
13249 return;
13251 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
13252 if (!hdr)
13253 goto out;
13255 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13256 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13257 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
13258 goto out;
13260 if (ft_event->ies &&
13261 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
13262 goto out;
13263 if (ft_event->ric_ies &&
13264 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
13265 ft_event->ric_ies))
13266 goto out;
13268 genlmsg_end(msg, hdr);
13270 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13271 NL80211_MCGRP_MLME, GFP_KERNEL);
13272 return;
13273 out:
13274 nlmsg_free(msg);
13276 EXPORT_SYMBOL(cfg80211_ft_event);
13278 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
13280 struct cfg80211_registered_device *rdev;
13281 struct sk_buff *msg;
13282 void *hdr;
13283 u32 nlportid;
13285 rdev = wiphy_to_rdev(wdev->wiphy);
13286 if (!rdev->crit_proto_nlportid)
13287 return;
13289 nlportid = rdev->crit_proto_nlportid;
13290 rdev->crit_proto_nlportid = 0;
13292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13293 if (!msg)
13294 return;
13296 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
13297 if (!hdr)
13298 goto nla_put_failure;
13300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13301 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13302 goto nla_put_failure;
13304 genlmsg_end(msg, hdr);
13306 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
13307 return;
13309 nla_put_failure:
13310 if (hdr)
13311 genlmsg_cancel(msg, hdr);
13312 nlmsg_free(msg);
13315 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
13317 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
13319 struct wiphy *wiphy = wdev->wiphy;
13320 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13321 struct sk_buff *msg;
13322 void *hdr;
13324 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13325 if (!msg)
13326 return;
13328 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
13329 if (!hdr)
13330 goto out;
13332 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13333 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
13334 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13335 goto out;
13337 genlmsg_end(msg, hdr);
13339 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
13340 NL80211_MCGRP_MLME, GFP_KERNEL);
13341 return;
13342 out:
13343 nlmsg_free(msg);
13346 /* initialisation/exit functions */
13348 int nl80211_init(void)
13350 int err;
13352 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
13353 nl80211_mcgrps);
13354 if (err)
13355 return err;
13357 err = netlink_register_notifier(&nl80211_netlink_notifier);
13358 if (err)
13359 goto err_out;
13361 return 0;
13362 err_out:
13363 genl_unregister_family(&nl80211_fam);
13364 return err;
13367 void nl80211_exit(void)
13369 netlink_unregister_notifier(&nl80211_netlink_notifier);
13370 genl_unregister_family(&nl80211_fam);