mdio-sun4i: oops in error handling in probe
[linux/fpc-iii.git] / net / wireless / nl80211.c
blobd316a956823869696c55ad8589b4822070cace9a
1 /*
2 * This is the new netlink-based wireless configuration interface.
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 */
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/if_ether.h>
13 #include <linux/ieee80211.h>
14 #include <linux/nl80211.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
21 #include <net/sock.h>
22 #include <net/inet_connection_sock.h>
23 #include "core.h"
24 #include "nl80211.h"
25 #include "reg.h"
26 #include "rdev-ops.h"
28 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
33 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
38 /* the netlink family */
39 static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
45 .netnsok = true,
46 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
50 /* multicast groups */
51 enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
56 NL80211_MCGRP_VENDOR,
57 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
60 static const struct genl_multicast_group nl80211_mcgrps[] = {
61 [NL80211_MCGRP_CONFIG] = { .name = "config", },
62 [NL80211_MCGRP_SCAN] = { .name = "scan", },
63 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
64 [NL80211_MCGRP_MLME] = { .name = "mlme", },
65 [NL80211_MCGRP_VENDOR] = { .name = "vendor", },
66 #ifdef CONFIG_NL80211_TESTMODE
67 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
68 #endif
71 /* returns ERR_PTR values */
72 static struct wireless_dev *
73 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
75 struct cfg80211_registered_device *rdev;
76 struct wireless_dev *result = NULL;
77 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
78 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
79 u64 wdev_id;
80 int wiphy_idx = -1;
81 int ifidx = -1;
83 ASSERT_RTNL();
85 if (!have_ifidx && !have_wdev_id)
86 return ERR_PTR(-EINVAL);
88 if (have_ifidx)
89 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
90 if (have_wdev_id) {
91 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
92 wiphy_idx = wdev_id >> 32;
95 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
96 struct wireless_dev *wdev;
98 if (wiphy_net(&rdev->wiphy) != netns)
99 continue;
101 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
102 continue;
104 list_for_each_entry(wdev, &rdev->wdev_list, list) {
105 if (have_ifidx && wdev->netdev &&
106 wdev->netdev->ifindex == ifidx) {
107 result = wdev;
108 break;
110 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
111 result = wdev;
112 break;
116 if (result)
117 break;
120 if (result)
121 return result;
122 return ERR_PTR(-ENODEV);
125 static struct cfg80211_registered_device *
126 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
128 struct cfg80211_registered_device *rdev = NULL, *tmp;
129 struct net_device *netdev;
131 ASSERT_RTNL();
133 if (!attrs[NL80211_ATTR_WIPHY] &&
134 !attrs[NL80211_ATTR_IFINDEX] &&
135 !attrs[NL80211_ATTR_WDEV])
136 return ERR_PTR(-EINVAL);
138 if (attrs[NL80211_ATTR_WIPHY])
139 rdev = cfg80211_rdev_by_wiphy_idx(
140 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
142 if (attrs[NL80211_ATTR_WDEV]) {
143 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
144 struct wireless_dev *wdev;
145 bool found = false;
147 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
148 if (tmp) {
149 /* make sure wdev exists */
150 list_for_each_entry(wdev, &tmp->wdev_list, list) {
151 if (wdev->identifier != (u32)wdev_id)
152 continue;
153 found = true;
154 break;
157 if (!found)
158 tmp = NULL;
160 if (rdev && tmp != rdev)
161 return ERR_PTR(-EINVAL);
162 rdev = tmp;
166 if (attrs[NL80211_ATTR_IFINDEX]) {
167 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
168 netdev = __dev_get_by_index(netns, ifindex);
169 if (netdev) {
170 if (netdev->ieee80211_ptr)
171 tmp = wiphy_to_rdev(
172 netdev->ieee80211_ptr->wiphy);
173 else
174 tmp = NULL;
176 /* not wireless device -- return error */
177 if (!tmp)
178 return ERR_PTR(-EINVAL);
180 /* mismatch -- return error */
181 if (rdev && tmp != rdev)
182 return ERR_PTR(-EINVAL);
184 rdev = tmp;
188 if (!rdev)
189 return ERR_PTR(-ENODEV);
191 if (netns != wiphy_net(&rdev->wiphy))
192 return ERR_PTR(-ENODEV);
194 return rdev;
198 * This function returns a pointer to the driver
199 * that the genl_info item that is passed refers to.
201 * The result of this can be a PTR_ERR and hence must
202 * be checked with IS_ERR() for errors.
204 static struct cfg80211_registered_device *
205 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
207 return __cfg80211_rdev_from_attrs(netns, info->attrs);
210 /* policy for the attributes */
211 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
212 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
213 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
214 .len = 20-1 },
215 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
217 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
218 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
219 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
220 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
221 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
224 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
225 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
226 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
227 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
229 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
230 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
231 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
233 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
234 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
236 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
237 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
238 .len = WLAN_MAX_KEY_LEN },
239 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
240 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
241 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
242 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
243 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
245 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
246 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
247 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
248 .len = IEEE80211_MAX_DATA_LEN },
249 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
250 .len = IEEE80211_MAX_DATA_LEN },
251 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
252 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
253 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
254 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
255 .len = NL80211_MAX_SUPP_RATES },
256 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
257 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
258 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
259 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
260 .len = IEEE80211_MAX_MESH_ID_LEN },
261 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
263 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
264 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
266 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
267 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
268 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
269 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
270 .len = NL80211_MAX_SUPP_RATES },
271 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
273 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
274 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
276 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
278 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
279 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
280 .len = IEEE80211_MAX_DATA_LEN },
281 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
282 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
284 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_SSID_LEN },
286 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
287 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
288 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
289 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
290 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
291 [NL80211_ATTR_STA_FLAGS2] = {
292 .len = sizeof(struct nl80211_sta_flag_update),
294 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
295 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
296 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
297 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
299 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
300 [NL80211_ATTR_PID] = { .type = NLA_U32 },
301 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
302 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
303 .len = WLAN_PMKID_LEN },
304 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
305 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
306 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
307 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
308 .len = IEEE80211_MAX_DATA_LEN },
309 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
310 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
311 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
312 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
313 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
314 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
315 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
316 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
317 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
318 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
319 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
320 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
321 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
322 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
323 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
324 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
325 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
326 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
327 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
328 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
329 .len = IEEE80211_MAX_DATA_LEN },
330 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
332 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
333 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
334 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
335 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
336 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
337 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
339 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
340 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
341 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
343 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
344 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
346 .len = NL80211_HT_CAPABILITY_LEN
348 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
349 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
350 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
351 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
352 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
353 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
354 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
355 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
356 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
357 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
358 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
359 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
360 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
361 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
362 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
363 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
364 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
365 .len = NL80211_VHT_CAPABILITY_LEN,
367 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
368 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
369 .len = IEEE80211_MAX_DATA_LEN },
370 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
371 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
372 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
379 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
380 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
381 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
382 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
383 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
384 .len = IEEE80211_QOS_MAP_LEN_MAX },
385 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
386 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
387 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
388 [NL80211_ATTR_IFACE_SOCKET_OWNER] = { .type = NLA_FLAG },
389 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
392 /* policy for the key attributes */
393 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
394 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
395 [NL80211_KEY_IDX] = { .type = NLA_U8 },
396 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
397 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
398 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
400 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
401 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
404 /* policy for the key default flags */
405 static const struct nla_policy
406 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
407 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
408 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
411 /* policy for WoWLAN attributes */
412 static const struct nla_policy
413 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
414 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
415 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
416 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
417 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
418 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
419 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
420 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
421 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
422 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
425 static const struct nla_policy
426 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
427 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
428 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
429 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
430 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
431 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
432 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
433 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
434 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
436 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
437 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
439 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
440 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
441 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
444 /* policy for coalesce rule attributes */
445 static const struct nla_policy
446 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
447 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
448 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
449 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
452 /* policy for GTK rekey offload attributes */
453 static const struct nla_policy
454 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
455 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
456 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
457 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
460 static const struct nla_policy
461 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
462 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
463 .len = IEEE80211_MAX_SSID_LEN },
464 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
467 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
468 struct netlink_callback *cb,
469 struct cfg80211_registered_device **rdev,
470 struct wireless_dev **wdev)
472 int err;
474 rtnl_lock();
476 if (!cb->args[0]) {
477 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
478 nl80211_fam.attrbuf, nl80211_fam.maxattr,
479 nl80211_policy);
480 if (err)
481 goto out_unlock;
483 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
484 nl80211_fam.attrbuf);
485 if (IS_ERR(*wdev)) {
486 err = PTR_ERR(*wdev);
487 goto out_unlock;
489 *rdev = wiphy_to_rdev((*wdev)->wiphy);
490 /* 0 is the first index - add 1 to parse only once */
491 cb->args[0] = (*rdev)->wiphy_idx + 1;
492 cb->args[1] = (*wdev)->identifier;
493 } else {
494 /* subtract the 1 again here */
495 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
496 struct wireless_dev *tmp;
498 if (!wiphy) {
499 err = -ENODEV;
500 goto out_unlock;
502 *rdev = wiphy_to_rdev(wiphy);
503 *wdev = NULL;
505 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
506 if (tmp->identifier == cb->args[1]) {
507 *wdev = tmp;
508 break;
512 if (!*wdev) {
513 err = -ENODEV;
514 goto out_unlock;
518 return 0;
519 out_unlock:
520 rtnl_unlock();
521 return err;
524 static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
526 rtnl_unlock();
529 /* IE validation */
530 static bool is_valid_ie_attr(const struct nlattr *attr)
532 const u8 *pos;
533 int len;
535 if (!attr)
536 return true;
538 pos = nla_data(attr);
539 len = nla_len(attr);
541 while (len) {
542 u8 elemlen;
544 if (len < 2)
545 return false;
546 len -= 2;
548 elemlen = pos[1];
549 if (elemlen > len)
550 return false;
552 len -= elemlen;
553 pos += 2 + elemlen;
556 return true;
559 /* message building helper */
560 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
561 int flags, u8 cmd)
563 /* since there is no private header just add the generic one */
564 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
567 static int nl80211_msg_put_channel(struct sk_buff *msg,
568 struct ieee80211_channel *chan,
569 bool large)
571 /* Some channels must be completely excluded from the
572 * list to protect old user-space tools from breaking
574 if (!large && chan->flags &
575 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
576 return 0;
578 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
579 chan->center_freq))
580 goto nla_put_failure;
582 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
583 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
584 goto nla_put_failure;
585 if (chan->flags & IEEE80211_CHAN_NO_IR) {
586 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
587 goto nla_put_failure;
588 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
589 goto nla_put_failure;
591 if (chan->flags & IEEE80211_CHAN_RADAR) {
592 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
593 goto nla_put_failure;
594 if (large) {
595 u32 time;
597 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
599 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
600 chan->dfs_state))
601 goto nla_put_failure;
602 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
603 time))
604 goto nla_put_failure;
605 if (nla_put_u32(msg,
606 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
607 chan->dfs_cac_ms))
608 goto nla_put_failure;
612 if (large) {
613 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
614 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
615 goto nla_put_failure;
616 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
617 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
618 goto nla_put_failure;
619 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
620 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
621 goto nla_put_failure;
622 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
623 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
624 goto nla_put_failure;
625 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
626 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
627 goto nla_put_failure;
628 if ((chan->flags & IEEE80211_CHAN_GO_CONCURRENT) &&
629 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_GO_CONCURRENT))
630 goto nla_put_failure;
631 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
632 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
633 goto nla_put_failure;
634 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
635 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
636 goto nla_put_failure;
639 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
640 DBM_TO_MBM(chan->max_power)))
641 goto nla_put_failure;
643 return 0;
645 nla_put_failure:
646 return -ENOBUFS;
649 /* netlink command implementations */
651 struct key_parse {
652 struct key_params p;
653 int idx;
654 int type;
655 bool def, defmgmt;
656 bool def_uni, def_multi;
659 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
661 struct nlattr *tb[NL80211_KEY_MAX + 1];
662 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
663 nl80211_key_policy);
664 if (err)
665 return err;
667 k->def = !!tb[NL80211_KEY_DEFAULT];
668 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
670 if (k->def) {
671 k->def_uni = true;
672 k->def_multi = true;
674 if (k->defmgmt)
675 k->def_multi = true;
677 if (tb[NL80211_KEY_IDX])
678 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
680 if (tb[NL80211_KEY_DATA]) {
681 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
682 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
685 if (tb[NL80211_KEY_SEQ]) {
686 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
687 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
690 if (tb[NL80211_KEY_CIPHER])
691 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
693 if (tb[NL80211_KEY_TYPE]) {
694 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
695 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
696 return -EINVAL;
699 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
700 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
701 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
702 tb[NL80211_KEY_DEFAULT_TYPES],
703 nl80211_key_default_policy);
704 if (err)
705 return err;
707 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
708 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
711 return 0;
714 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
716 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
717 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
718 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
721 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
722 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
723 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
726 if (info->attrs[NL80211_ATTR_KEY_IDX])
727 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
729 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
730 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
732 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
733 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
735 if (k->def) {
736 k->def_uni = true;
737 k->def_multi = true;
739 if (k->defmgmt)
740 k->def_multi = true;
742 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
743 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
744 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
745 return -EINVAL;
748 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
749 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
750 int err = nla_parse_nested(
751 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
752 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
753 nl80211_key_default_policy);
754 if (err)
755 return err;
757 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
758 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
761 return 0;
764 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
766 int err;
768 memset(k, 0, sizeof(*k));
769 k->idx = -1;
770 k->type = -1;
772 if (info->attrs[NL80211_ATTR_KEY])
773 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
774 else
775 err = nl80211_parse_key_old(info, k);
777 if (err)
778 return err;
780 if (k->def && k->defmgmt)
781 return -EINVAL;
783 if (k->defmgmt) {
784 if (k->def_uni || !k->def_multi)
785 return -EINVAL;
788 if (k->idx != -1) {
789 if (k->defmgmt) {
790 if (k->idx < 4 || k->idx > 5)
791 return -EINVAL;
792 } else if (k->def) {
793 if (k->idx < 0 || k->idx > 3)
794 return -EINVAL;
795 } else {
796 if (k->idx < 0 || k->idx > 5)
797 return -EINVAL;
801 return 0;
804 static struct cfg80211_cached_keys *
805 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
806 struct nlattr *keys, bool *no_ht)
808 struct key_parse parse;
809 struct nlattr *key;
810 struct cfg80211_cached_keys *result;
811 int rem, err, def = 0;
813 result = kzalloc(sizeof(*result), GFP_KERNEL);
814 if (!result)
815 return ERR_PTR(-ENOMEM);
817 result->def = -1;
818 result->defmgmt = -1;
820 nla_for_each_nested(key, keys, rem) {
821 memset(&parse, 0, sizeof(parse));
822 parse.idx = -1;
824 err = nl80211_parse_key_new(key, &parse);
825 if (err)
826 goto error;
827 err = -EINVAL;
828 if (!parse.p.key)
829 goto error;
830 if (parse.idx < 0 || parse.idx > 4)
831 goto error;
832 if (parse.def) {
833 if (def)
834 goto error;
835 def = 1;
836 result->def = parse.idx;
837 if (!parse.def_uni || !parse.def_multi)
838 goto error;
839 } else if (parse.defmgmt)
840 goto error;
841 err = cfg80211_validate_key_settings(rdev, &parse.p,
842 parse.idx, false, NULL);
843 if (err)
844 goto error;
845 result->params[parse.idx].cipher = parse.p.cipher;
846 result->params[parse.idx].key_len = parse.p.key_len;
847 result->params[parse.idx].key = result->data[parse.idx];
848 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
850 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
851 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
852 if (no_ht)
853 *no_ht = true;
857 return result;
858 error:
859 kfree(result);
860 return ERR_PTR(err);
863 static int nl80211_key_allowed(struct wireless_dev *wdev)
865 ASSERT_WDEV_LOCK(wdev);
867 switch (wdev->iftype) {
868 case NL80211_IFTYPE_AP:
869 case NL80211_IFTYPE_AP_VLAN:
870 case NL80211_IFTYPE_P2P_GO:
871 case NL80211_IFTYPE_MESH_POINT:
872 break;
873 case NL80211_IFTYPE_ADHOC:
874 case NL80211_IFTYPE_STATION:
875 case NL80211_IFTYPE_P2P_CLIENT:
876 if (!wdev->current_bss)
877 return -ENOLINK;
878 break;
879 default:
880 return -EINVAL;
883 return 0;
886 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
887 struct nlattr *tb)
889 struct ieee80211_channel *chan;
891 if (tb == NULL)
892 return NULL;
893 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
894 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
895 return NULL;
896 return chan;
899 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
901 struct nlattr *nl_modes = nla_nest_start(msg, attr);
902 int i;
904 if (!nl_modes)
905 goto nla_put_failure;
907 i = 0;
908 while (ifmodes) {
909 if ((ifmodes & 1) && nla_put_flag(msg, i))
910 goto nla_put_failure;
911 ifmodes >>= 1;
912 i++;
915 nla_nest_end(msg, nl_modes);
916 return 0;
918 nla_put_failure:
919 return -ENOBUFS;
922 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
923 struct sk_buff *msg,
924 bool large)
926 struct nlattr *nl_combis;
927 int i, j;
929 nl_combis = nla_nest_start(msg,
930 NL80211_ATTR_INTERFACE_COMBINATIONS);
931 if (!nl_combis)
932 goto nla_put_failure;
934 for (i = 0; i < wiphy->n_iface_combinations; i++) {
935 const struct ieee80211_iface_combination *c;
936 struct nlattr *nl_combi, *nl_limits;
938 c = &wiphy->iface_combinations[i];
940 nl_combi = nla_nest_start(msg, i + 1);
941 if (!nl_combi)
942 goto nla_put_failure;
944 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
945 if (!nl_limits)
946 goto nla_put_failure;
948 for (j = 0; j < c->n_limits; j++) {
949 struct nlattr *nl_limit;
951 nl_limit = nla_nest_start(msg, j + 1);
952 if (!nl_limit)
953 goto nla_put_failure;
954 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
955 c->limits[j].max))
956 goto nla_put_failure;
957 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
958 c->limits[j].types))
959 goto nla_put_failure;
960 nla_nest_end(msg, nl_limit);
963 nla_nest_end(msg, nl_limits);
965 if (c->beacon_int_infra_match &&
966 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
967 goto nla_put_failure;
968 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
969 c->num_different_channels) ||
970 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
971 c->max_interfaces))
972 goto nla_put_failure;
973 if (large &&
974 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
975 c->radar_detect_widths) ||
976 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
977 c->radar_detect_regions)))
978 goto nla_put_failure;
980 nla_nest_end(msg, nl_combi);
983 nla_nest_end(msg, nl_combis);
985 return 0;
986 nla_put_failure:
987 return -ENOBUFS;
990 #ifdef CONFIG_PM
991 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
992 struct sk_buff *msg)
994 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
995 struct nlattr *nl_tcp;
997 if (!tcp)
998 return 0;
1000 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1001 if (!nl_tcp)
1002 return -ENOBUFS;
1004 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1005 tcp->data_payload_max))
1006 return -ENOBUFS;
1008 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1009 tcp->data_payload_max))
1010 return -ENOBUFS;
1012 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1013 return -ENOBUFS;
1015 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1016 sizeof(*tcp->tok), tcp->tok))
1017 return -ENOBUFS;
1019 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1020 tcp->data_interval_max))
1021 return -ENOBUFS;
1023 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1024 tcp->wake_payload_max))
1025 return -ENOBUFS;
1027 nla_nest_end(msg, nl_tcp);
1028 return 0;
1031 static int nl80211_send_wowlan(struct sk_buff *msg,
1032 struct cfg80211_registered_device *rdev,
1033 bool large)
1035 struct nlattr *nl_wowlan;
1037 if (!rdev->wiphy.wowlan)
1038 return 0;
1040 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1041 if (!nl_wowlan)
1042 return -ENOBUFS;
1044 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1045 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1046 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1047 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1048 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1049 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1050 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1051 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1052 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1053 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1054 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1055 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1056 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1057 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1058 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1059 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1060 return -ENOBUFS;
1062 if (rdev->wiphy.wowlan->n_patterns) {
1063 struct nl80211_pattern_support pat = {
1064 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1065 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1066 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1067 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1070 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1071 sizeof(pat), &pat))
1072 return -ENOBUFS;
1075 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1076 return -ENOBUFS;
1078 nla_nest_end(msg, nl_wowlan);
1080 return 0;
1082 #endif
1084 static int nl80211_send_coalesce(struct sk_buff *msg,
1085 struct cfg80211_registered_device *rdev)
1087 struct nl80211_coalesce_rule_support rule;
1089 if (!rdev->wiphy.coalesce)
1090 return 0;
1092 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1093 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1094 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1095 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1096 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1097 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1099 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1100 return -ENOBUFS;
1102 return 0;
1105 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1106 struct ieee80211_supported_band *sband)
1108 struct nlattr *nl_rates, *nl_rate;
1109 struct ieee80211_rate *rate;
1110 int i;
1112 /* add HT info */
1113 if (sband->ht_cap.ht_supported &&
1114 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1115 sizeof(sband->ht_cap.mcs),
1116 &sband->ht_cap.mcs) ||
1117 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1118 sband->ht_cap.cap) ||
1119 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1120 sband->ht_cap.ampdu_factor) ||
1121 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1122 sband->ht_cap.ampdu_density)))
1123 return -ENOBUFS;
1125 /* add VHT info */
1126 if (sband->vht_cap.vht_supported &&
1127 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1128 sizeof(sband->vht_cap.vht_mcs),
1129 &sband->vht_cap.vht_mcs) ||
1130 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1131 sband->vht_cap.cap)))
1132 return -ENOBUFS;
1134 /* add bitrates */
1135 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1136 if (!nl_rates)
1137 return -ENOBUFS;
1139 for (i = 0; i < sband->n_bitrates; i++) {
1140 nl_rate = nla_nest_start(msg, i);
1141 if (!nl_rate)
1142 return -ENOBUFS;
1144 rate = &sband->bitrates[i];
1145 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1146 rate->bitrate))
1147 return -ENOBUFS;
1148 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1149 nla_put_flag(msg,
1150 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1151 return -ENOBUFS;
1153 nla_nest_end(msg, nl_rate);
1156 nla_nest_end(msg, nl_rates);
1158 return 0;
1161 static int
1162 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1163 const struct ieee80211_txrx_stypes *mgmt_stypes)
1165 u16 stypes;
1166 struct nlattr *nl_ftypes, *nl_ifs;
1167 enum nl80211_iftype ift;
1168 int i;
1170 if (!mgmt_stypes)
1171 return 0;
1173 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1174 if (!nl_ifs)
1175 return -ENOBUFS;
1177 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1178 nl_ftypes = nla_nest_start(msg, ift);
1179 if (!nl_ftypes)
1180 return -ENOBUFS;
1181 i = 0;
1182 stypes = mgmt_stypes[ift].tx;
1183 while (stypes) {
1184 if ((stypes & 1) &&
1185 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1186 (i << 4) | IEEE80211_FTYPE_MGMT))
1187 return -ENOBUFS;
1188 stypes >>= 1;
1189 i++;
1191 nla_nest_end(msg, nl_ftypes);
1194 nla_nest_end(msg, nl_ifs);
1196 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1197 if (!nl_ifs)
1198 return -ENOBUFS;
1200 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1201 nl_ftypes = nla_nest_start(msg, ift);
1202 if (!nl_ftypes)
1203 return -ENOBUFS;
1204 i = 0;
1205 stypes = mgmt_stypes[ift].rx;
1206 while (stypes) {
1207 if ((stypes & 1) &&
1208 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1209 (i << 4) | IEEE80211_FTYPE_MGMT))
1210 return -ENOBUFS;
1211 stypes >>= 1;
1212 i++;
1214 nla_nest_end(msg, nl_ftypes);
1216 nla_nest_end(msg, nl_ifs);
1218 return 0;
1221 struct nl80211_dump_wiphy_state {
1222 s64 filter_wiphy;
1223 long start;
1224 long split_start, band_start, chan_start;
1225 bool split;
1228 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1229 enum nl80211_commands cmd,
1230 struct sk_buff *msg, u32 portid, u32 seq,
1231 int flags, struct nl80211_dump_wiphy_state *state)
1233 void *hdr;
1234 struct nlattr *nl_bands, *nl_band;
1235 struct nlattr *nl_freqs, *nl_freq;
1236 struct nlattr *nl_cmds;
1237 enum ieee80211_band band;
1238 struct ieee80211_channel *chan;
1239 int i;
1240 const struct ieee80211_txrx_stypes *mgmt_stypes =
1241 rdev->wiphy.mgmt_stypes;
1242 u32 features;
1244 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1245 if (!hdr)
1246 return -ENOBUFS;
1248 if (WARN_ON(!state))
1249 return -EINVAL;
1251 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1252 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1253 wiphy_name(&rdev->wiphy)) ||
1254 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1255 cfg80211_rdev_list_generation))
1256 goto nla_put_failure;
1258 if (cmd != NL80211_CMD_NEW_WIPHY)
1259 goto finish;
1261 switch (state->split_start) {
1262 case 0:
1263 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1264 rdev->wiphy.retry_short) ||
1265 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1266 rdev->wiphy.retry_long) ||
1267 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1268 rdev->wiphy.frag_threshold) ||
1269 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1270 rdev->wiphy.rts_threshold) ||
1271 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1272 rdev->wiphy.coverage_class) ||
1273 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1274 rdev->wiphy.max_scan_ssids) ||
1275 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1276 rdev->wiphy.max_sched_scan_ssids) ||
1277 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1278 rdev->wiphy.max_scan_ie_len) ||
1279 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1280 rdev->wiphy.max_sched_scan_ie_len) ||
1281 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1282 rdev->wiphy.max_match_sets))
1283 goto nla_put_failure;
1285 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1286 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1287 goto nla_put_failure;
1288 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1289 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1290 goto nla_put_failure;
1291 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1292 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1293 goto nla_put_failure;
1294 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1295 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1296 goto nla_put_failure;
1297 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1298 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1299 goto nla_put_failure;
1300 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1301 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1302 goto nla_put_failure;
1303 state->split_start++;
1304 if (state->split)
1305 break;
1306 case 1:
1307 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1308 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1309 rdev->wiphy.cipher_suites))
1310 goto nla_put_failure;
1312 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1313 rdev->wiphy.max_num_pmkids))
1314 goto nla_put_failure;
1316 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1317 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1318 goto nla_put_failure;
1320 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1321 rdev->wiphy.available_antennas_tx) ||
1322 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1323 rdev->wiphy.available_antennas_rx))
1324 goto nla_put_failure;
1326 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1327 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1328 rdev->wiphy.probe_resp_offload))
1329 goto nla_put_failure;
1331 if ((rdev->wiphy.available_antennas_tx ||
1332 rdev->wiphy.available_antennas_rx) &&
1333 rdev->ops->get_antenna) {
1334 u32 tx_ant = 0, rx_ant = 0;
1335 int res;
1336 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1337 if (!res) {
1338 if (nla_put_u32(msg,
1339 NL80211_ATTR_WIPHY_ANTENNA_TX,
1340 tx_ant) ||
1341 nla_put_u32(msg,
1342 NL80211_ATTR_WIPHY_ANTENNA_RX,
1343 rx_ant))
1344 goto nla_put_failure;
1348 state->split_start++;
1349 if (state->split)
1350 break;
1351 case 2:
1352 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1353 rdev->wiphy.interface_modes))
1354 goto nla_put_failure;
1355 state->split_start++;
1356 if (state->split)
1357 break;
1358 case 3:
1359 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1360 if (!nl_bands)
1361 goto nla_put_failure;
1363 for (band = state->band_start;
1364 band < IEEE80211_NUM_BANDS; band++) {
1365 struct ieee80211_supported_band *sband;
1367 sband = rdev->wiphy.bands[band];
1369 if (!sband)
1370 continue;
1372 nl_band = nla_nest_start(msg, band);
1373 if (!nl_band)
1374 goto nla_put_failure;
1376 switch (state->chan_start) {
1377 case 0:
1378 if (nl80211_send_band_rateinfo(msg, sband))
1379 goto nla_put_failure;
1380 state->chan_start++;
1381 if (state->split)
1382 break;
1383 default:
1384 /* add frequencies */
1385 nl_freqs = nla_nest_start(
1386 msg, NL80211_BAND_ATTR_FREQS);
1387 if (!nl_freqs)
1388 goto nla_put_failure;
1390 for (i = state->chan_start - 1;
1391 i < sband->n_channels;
1392 i++) {
1393 nl_freq = nla_nest_start(msg, i);
1394 if (!nl_freq)
1395 goto nla_put_failure;
1397 chan = &sband->channels[i];
1399 if (nl80211_msg_put_channel(
1400 msg, chan,
1401 state->split))
1402 goto nla_put_failure;
1404 nla_nest_end(msg, nl_freq);
1405 if (state->split)
1406 break;
1408 if (i < sband->n_channels)
1409 state->chan_start = i + 2;
1410 else
1411 state->chan_start = 0;
1412 nla_nest_end(msg, nl_freqs);
1415 nla_nest_end(msg, nl_band);
1417 if (state->split) {
1418 /* start again here */
1419 if (state->chan_start)
1420 band--;
1421 break;
1424 nla_nest_end(msg, nl_bands);
1426 if (band < IEEE80211_NUM_BANDS)
1427 state->band_start = band + 1;
1428 else
1429 state->band_start = 0;
1431 /* if bands & channels are done, continue outside */
1432 if (state->band_start == 0 && state->chan_start == 0)
1433 state->split_start++;
1434 if (state->split)
1435 break;
1436 case 4:
1437 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1438 if (!nl_cmds)
1439 goto nla_put_failure;
1441 i = 0;
1442 #define CMD(op, n) \
1443 do { \
1444 if (rdev->ops->op) { \
1445 i++; \
1446 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1447 goto nla_put_failure; \
1449 } while (0)
1451 CMD(add_virtual_intf, NEW_INTERFACE);
1452 CMD(change_virtual_intf, SET_INTERFACE);
1453 CMD(add_key, NEW_KEY);
1454 CMD(start_ap, START_AP);
1455 CMD(add_station, NEW_STATION);
1456 CMD(add_mpath, NEW_MPATH);
1457 CMD(update_mesh_config, SET_MESH_CONFIG);
1458 CMD(change_bss, SET_BSS);
1459 CMD(auth, AUTHENTICATE);
1460 CMD(assoc, ASSOCIATE);
1461 CMD(deauth, DEAUTHENTICATE);
1462 CMD(disassoc, DISASSOCIATE);
1463 CMD(join_ibss, JOIN_IBSS);
1464 CMD(join_mesh, JOIN_MESH);
1465 CMD(set_pmksa, SET_PMKSA);
1466 CMD(del_pmksa, DEL_PMKSA);
1467 CMD(flush_pmksa, FLUSH_PMKSA);
1468 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1469 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1470 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1471 CMD(mgmt_tx, FRAME);
1472 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1473 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1474 i++;
1475 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1476 goto nla_put_failure;
1478 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1479 rdev->ops->join_mesh) {
1480 i++;
1481 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1482 goto nla_put_failure;
1484 CMD(set_wds_peer, SET_WDS_PEER);
1485 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1486 CMD(tdls_mgmt, TDLS_MGMT);
1487 CMD(tdls_oper, TDLS_OPER);
1489 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1490 CMD(sched_scan_start, START_SCHED_SCAN);
1491 CMD(probe_client, PROBE_CLIENT);
1492 CMD(set_noack_map, SET_NOACK_MAP);
1493 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1494 i++;
1495 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1496 goto nla_put_failure;
1498 CMD(start_p2p_device, START_P2P_DEVICE);
1499 CMD(set_mcast_rate, SET_MCAST_RATE);
1500 #ifdef CONFIG_NL80211_TESTMODE
1501 CMD(testmode_cmd, TESTMODE);
1502 #endif
1503 if (state->split) {
1504 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1505 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1506 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1507 CMD(channel_switch, CHANNEL_SWITCH);
1508 CMD(set_qos_map, SET_QOS_MAP);
1510 /* add into the if now */
1511 #undef CMD
1513 if (rdev->ops->connect || rdev->ops->auth) {
1514 i++;
1515 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1516 goto nla_put_failure;
1519 if (rdev->ops->disconnect || rdev->ops->deauth) {
1520 i++;
1521 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1522 goto nla_put_failure;
1525 nla_nest_end(msg, nl_cmds);
1526 state->split_start++;
1527 if (state->split)
1528 break;
1529 case 5:
1530 if (rdev->ops->remain_on_channel &&
1531 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1532 nla_put_u32(msg,
1533 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1534 rdev->wiphy.max_remain_on_channel_duration))
1535 goto nla_put_failure;
1537 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1538 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1539 goto nla_put_failure;
1541 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1542 goto nla_put_failure;
1543 state->split_start++;
1544 if (state->split)
1545 break;
1546 case 6:
1547 #ifdef CONFIG_PM
1548 if (nl80211_send_wowlan(msg, rdev, state->split))
1549 goto nla_put_failure;
1550 state->split_start++;
1551 if (state->split)
1552 break;
1553 #else
1554 state->split_start++;
1555 #endif
1556 case 7:
1557 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1558 rdev->wiphy.software_iftypes))
1559 goto nla_put_failure;
1561 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1562 state->split))
1563 goto nla_put_failure;
1565 state->split_start++;
1566 if (state->split)
1567 break;
1568 case 8:
1569 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1570 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1571 rdev->wiphy.ap_sme_capa))
1572 goto nla_put_failure;
1574 features = rdev->wiphy.features;
1576 * We can only add the per-channel limit information if the
1577 * dump is split, otherwise it makes it too big. Therefore
1578 * only advertise it in that case.
1580 if (state->split)
1581 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1582 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1583 goto nla_put_failure;
1585 if (rdev->wiphy.ht_capa_mod_mask &&
1586 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1587 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1588 rdev->wiphy.ht_capa_mod_mask))
1589 goto nla_put_failure;
1591 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1592 rdev->wiphy.max_acl_mac_addrs &&
1593 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1594 rdev->wiphy.max_acl_mac_addrs))
1595 goto nla_put_failure;
1598 * Any information below this point is only available to
1599 * applications that can deal with it being split. This
1600 * helps ensure that newly added capabilities don't break
1601 * older tools by overrunning their buffers.
1603 * We still increment split_start so that in the split
1604 * case we'll continue with more data in the next round,
1605 * but break unconditionally so unsplit data stops here.
1607 state->split_start++;
1608 break;
1609 case 9:
1610 if (rdev->wiphy.extended_capabilities &&
1611 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1612 rdev->wiphy.extended_capabilities_len,
1613 rdev->wiphy.extended_capabilities) ||
1614 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1615 rdev->wiphy.extended_capabilities_len,
1616 rdev->wiphy.extended_capabilities_mask)))
1617 goto nla_put_failure;
1619 if (rdev->wiphy.vht_capa_mod_mask &&
1620 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1621 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1622 rdev->wiphy.vht_capa_mod_mask))
1623 goto nla_put_failure;
1625 state->split_start++;
1626 break;
1627 case 10:
1628 if (nl80211_send_coalesce(msg, rdev))
1629 goto nla_put_failure;
1631 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1632 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1633 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1634 goto nla_put_failure;
1636 if (rdev->wiphy.max_ap_assoc_sta &&
1637 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1638 rdev->wiphy.max_ap_assoc_sta))
1639 goto nla_put_failure;
1641 state->split_start++;
1642 break;
1643 case 11:
1644 if (rdev->wiphy.n_vendor_commands) {
1645 const struct nl80211_vendor_cmd_info *info;
1646 struct nlattr *nested;
1648 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1649 if (!nested)
1650 goto nla_put_failure;
1652 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1653 info = &rdev->wiphy.vendor_commands[i].info;
1654 if (nla_put(msg, i + 1, sizeof(*info), info))
1655 goto nla_put_failure;
1657 nla_nest_end(msg, nested);
1660 if (rdev->wiphy.n_vendor_events) {
1661 const struct nl80211_vendor_cmd_info *info;
1662 struct nlattr *nested;
1664 nested = nla_nest_start(msg,
1665 NL80211_ATTR_VENDOR_EVENTS);
1666 if (!nested)
1667 goto nla_put_failure;
1669 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1670 info = &rdev->wiphy.vendor_events[i];
1671 if (nla_put(msg, i + 1, sizeof(*info), info))
1672 goto nla_put_failure;
1674 nla_nest_end(msg, nested);
1676 state->split_start++;
1677 break;
1678 case 12:
1679 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1680 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1681 rdev->wiphy.max_num_csa_counters))
1682 goto nla_put_failure;
1684 /* done */
1685 state->split_start = 0;
1686 break;
1688 finish:
1689 return genlmsg_end(msg, hdr);
1691 nla_put_failure:
1692 genlmsg_cancel(msg, hdr);
1693 return -EMSGSIZE;
1696 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1697 struct netlink_callback *cb,
1698 struct nl80211_dump_wiphy_state *state)
1700 struct nlattr **tb = nl80211_fam.attrbuf;
1701 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1702 tb, nl80211_fam.maxattr, nl80211_policy);
1703 /* ignore parse errors for backward compatibility */
1704 if (ret)
1705 return 0;
1707 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1708 if (tb[NL80211_ATTR_WIPHY])
1709 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1710 if (tb[NL80211_ATTR_WDEV])
1711 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1712 if (tb[NL80211_ATTR_IFINDEX]) {
1713 struct net_device *netdev;
1714 struct cfg80211_registered_device *rdev;
1715 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1717 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1718 if (!netdev)
1719 return -ENODEV;
1720 if (netdev->ieee80211_ptr) {
1721 rdev = wiphy_to_rdev(
1722 netdev->ieee80211_ptr->wiphy);
1723 state->filter_wiphy = rdev->wiphy_idx;
1727 return 0;
1730 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1732 int idx = 0, ret;
1733 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1734 struct cfg80211_registered_device *rdev;
1736 rtnl_lock();
1737 if (!state) {
1738 state = kzalloc(sizeof(*state), GFP_KERNEL);
1739 if (!state) {
1740 rtnl_unlock();
1741 return -ENOMEM;
1743 state->filter_wiphy = -1;
1744 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1745 if (ret) {
1746 kfree(state);
1747 rtnl_unlock();
1748 return ret;
1750 cb->args[0] = (long)state;
1753 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1754 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1755 continue;
1756 if (++idx <= state->start)
1757 continue;
1758 if (state->filter_wiphy != -1 &&
1759 state->filter_wiphy != rdev->wiphy_idx)
1760 continue;
1761 /* attempt to fit multiple wiphy data chunks into the skb */
1762 do {
1763 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1764 skb,
1765 NETLINK_CB(cb->skb).portid,
1766 cb->nlh->nlmsg_seq,
1767 NLM_F_MULTI, state);
1768 if (ret < 0) {
1770 * If sending the wiphy data didn't fit (ENOBUFS
1771 * or EMSGSIZE returned), this SKB is still
1772 * empty (so it's not too big because another
1773 * wiphy dataset is already in the skb) and
1774 * we've not tried to adjust the dump allocation
1775 * yet ... then adjust the alloc size to be
1776 * bigger, and return 1 but with the empty skb.
1777 * This results in an empty message being RX'ed
1778 * in userspace, but that is ignored.
1780 * We can then retry with the larger buffer.
1782 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1783 !skb->len && !state->split &&
1784 cb->min_dump_alloc < 4096) {
1785 cb->min_dump_alloc = 4096;
1786 state->split_start = 0;
1787 rtnl_unlock();
1788 return 1;
1790 idx--;
1791 break;
1793 } while (state->split_start > 0);
1794 break;
1796 rtnl_unlock();
1798 state->start = idx;
1800 return skb->len;
1803 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1805 kfree((void *)cb->args[0]);
1806 return 0;
1809 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1811 struct sk_buff *msg;
1812 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1813 struct nl80211_dump_wiphy_state state = {};
1815 msg = nlmsg_new(4096, GFP_KERNEL);
1816 if (!msg)
1817 return -ENOMEM;
1819 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
1820 info->snd_portid, info->snd_seq, 0,
1821 &state) < 0) {
1822 nlmsg_free(msg);
1823 return -ENOBUFS;
1826 return genlmsg_reply(msg, info);
1829 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1830 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1831 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1832 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1833 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1834 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1837 static int parse_txq_params(struct nlattr *tb[],
1838 struct ieee80211_txq_params *txq_params)
1840 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1841 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1842 !tb[NL80211_TXQ_ATTR_AIFS])
1843 return -EINVAL;
1845 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
1846 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1847 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1848 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1849 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1851 if (txq_params->ac >= NL80211_NUM_ACS)
1852 return -EINVAL;
1854 return 0;
1857 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1860 * You can only set the channel explicitly for WDS interfaces,
1861 * all others have their channel managed via their respective
1862 * "establish a connection" command (connect, join, ...)
1864 * For AP/GO and mesh mode, the channel can be set with the
1865 * channel userspace API, but is only stored and passed to the
1866 * low-level driver when the AP starts or the mesh is joined.
1867 * This is for backward compatibility, userspace can also give
1868 * the channel in the start-ap or join-mesh commands instead.
1870 * Monitors are special as they are normally slaved to
1871 * whatever else is going on, so they have their own special
1872 * operation to set the monitor channel if possible.
1874 return !wdev ||
1875 wdev->iftype == NL80211_IFTYPE_AP ||
1876 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1877 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1878 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1881 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1882 struct genl_info *info,
1883 struct cfg80211_chan_def *chandef)
1885 u32 control_freq;
1887 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1888 return -EINVAL;
1890 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1892 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
1893 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1894 chandef->center_freq1 = control_freq;
1895 chandef->center_freq2 = 0;
1897 /* Primary channel not allowed */
1898 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1899 return -EINVAL;
1901 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1902 enum nl80211_channel_type chantype;
1904 chantype = nla_get_u32(
1905 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1907 switch (chantype) {
1908 case NL80211_CHAN_NO_HT:
1909 case NL80211_CHAN_HT20:
1910 case NL80211_CHAN_HT40PLUS:
1911 case NL80211_CHAN_HT40MINUS:
1912 cfg80211_chandef_create(chandef, chandef->chan,
1913 chantype);
1914 break;
1915 default:
1916 return -EINVAL;
1918 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1919 chandef->width =
1920 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1921 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1922 chandef->center_freq1 =
1923 nla_get_u32(
1924 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1925 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1926 chandef->center_freq2 =
1927 nla_get_u32(
1928 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1931 if (!cfg80211_chandef_valid(chandef))
1932 return -EINVAL;
1934 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1935 IEEE80211_CHAN_DISABLED))
1936 return -EINVAL;
1938 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1939 chandef->width == NL80211_CHAN_WIDTH_10) &&
1940 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1941 return -EINVAL;
1943 return 0;
1946 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1947 struct net_device *dev,
1948 struct genl_info *info)
1950 struct cfg80211_chan_def chandef;
1951 int result;
1952 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1953 struct wireless_dev *wdev = NULL;
1955 if (dev)
1956 wdev = dev->ieee80211_ptr;
1957 if (!nl80211_can_set_dev_channel(wdev))
1958 return -EOPNOTSUPP;
1959 if (wdev)
1960 iftype = wdev->iftype;
1962 result = nl80211_parse_chandef(rdev, info, &chandef);
1963 if (result)
1964 return result;
1966 switch (iftype) {
1967 case NL80211_IFTYPE_AP:
1968 case NL80211_IFTYPE_P2P_GO:
1969 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef, iftype)) {
1970 result = -EINVAL;
1971 break;
1973 if (wdev->beacon_interval) {
1974 if (!dev || !rdev->ops->set_ap_chanwidth ||
1975 !(rdev->wiphy.features &
1976 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
1977 result = -EBUSY;
1978 break;
1981 /* Only allow dynamic channel width changes */
1982 if (chandef.chan != wdev->preset_chandef.chan) {
1983 result = -EBUSY;
1984 break;
1986 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
1987 if (result)
1988 break;
1990 wdev->preset_chandef = chandef;
1991 result = 0;
1992 break;
1993 case NL80211_IFTYPE_MESH_POINT:
1994 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
1995 break;
1996 case NL80211_IFTYPE_MONITOR:
1997 result = cfg80211_set_monitor_channel(rdev, &chandef);
1998 break;
1999 default:
2000 result = -EINVAL;
2003 return result;
2006 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2009 struct net_device *netdev = info->user_ptr[1];
2011 return __nl80211_set_channel(rdev, netdev, info);
2014 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2016 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2017 struct net_device *dev = info->user_ptr[1];
2018 struct wireless_dev *wdev = dev->ieee80211_ptr;
2019 const u8 *bssid;
2021 if (!info->attrs[NL80211_ATTR_MAC])
2022 return -EINVAL;
2024 if (netif_running(dev))
2025 return -EBUSY;
2027 if (!rdev->ops->set_wds_peer)
2028 return -EOPNOTSUPP;
2030 if (wdev->iftype != NL80211_IFTYPE_WDS)
2031 return -EOPNOTSUPP;
2033 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2034 return rdev_set_wds_peer(rdev, dev, bssid);
2038 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2040 struct cfg80211_registered_device *rdev;
2041 struct net_device *netdev = NULL;
2042 struct wireless_dev *wdev;
2043 int result = 0, rem_txq_params = 0;
2044 struct nlattr *nl_txq_params;
2045 u32 changed;
2046 u8 retry_short = 0, retry_long = 0;
2047 u32 frag_threshold = 0, rts_threshold = 0;
2048 u8 coverage_class = 0;
2050 ASSERT_RTNL();
2053 * Try to find the wiphy and netdev. Normally this
2054 * function shouldn't need the netdev, but this is
2055 * done for backward compatibility -- previously
2056 * setting the channel was done per wiphy, but now
2057 * it is per netdev. Previous userland like hostapd
2058 * also passed a netdev to set_wiphy, so that it is
2059 * possible to let that go to the right netdev!
2062 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2063 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2065 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2066 if (netdev && netdev->ieee80211_ptr)
2067 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2068 else
2069 netdev = NULL;
2072 if (!netdev) {
2073 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2074 info->attrs);
2075 if (IS_ERR(rdev))
2076 return PTR_ERR(rdev);
2077 wdev = NULL;
2078 netdev = NULL;
2079 result = 0;
2080 } else
2081 wdev = netdev->ieee80211_ptr;
2084 * end workaround code, by now the rdev is available
2085 * and locked, and wdev may or may not be NULL.
2088 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2089 result = cfg80211_dev_rename(
2090 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2092 if (result)
2093 return result;
2095 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2096 struct ieee80211_txq_params txq_params;
2097 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2099 if (!rdev->ops->set_txq_params)
2100 return -EOPNOTSUPP;
2102 if (!netdev)
2103 return -EINVAL;
2105 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2106 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2107 return -EINVAL;
2109 if (!netif_running(netdev))
2110 return -ENETDOWN;
2112 nla_for_each_nested(nl_txq_params,
2113 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2114 rem_txq_params) {
2115 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2116 nla_data(nl_txq_params),
2117 nla_len(nl_txq_params),
2118 txq_params_policy);
2119 if (result)
2120 return result;
2121 result = parse_txq_params(tb, &txq_params);
2122 if (result)
2123 return result;
2125 result = rdev_set_txq_params(rdev, netdev,
2126 &txq_params);
2127 if (result)
2128 return result;
2132 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2133 result = __nl80211_set_channel(
2134 rdev,
2135 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2136 info);
2137 if (result)
2138 return result;
2141 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2142 struct wireless_dev *txp_wdev = wdev;
2143 enum nl80211_tx_power_setting type;
2144 int idx, mbm = 0;
2146 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2147 txp_wdev = NULL;
2149 if (!rdev->ops->set_tx_power)
2150 return -EOPNOTSUPP;
2152 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2153 type = nla_get_u32(info->attrs[idx]);
2155 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2156 (type != NL80211_TX_POWER_AUTOMATIC))
2157 return -EINVAL;
2159 if (type != NL80211_TX_POWER_AUTOMATIC) {
2160 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2161 mbm = nla_get_u32(info->attrs[idx]);
2164 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2165 if (result)
2166 return result;
2169 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2170 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2171 u32 tx_ant, rx_ant;
2172 if ((!rdev->wiphy.available_antennas_tx &&
2173 !rdev->wiphy.available_antennas_rx) ||
2174 !rdev->ops->set_antenna)
2175 return -EOPNOTSUPP;
2177 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2178 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2180 /* reject antenna configurations which don't match the
2181 * available antenna masks, except for the "all" mask */
2182 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2183 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2184 return -EINVAL;
2186 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2187 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2189 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2190 if (result)
2191 return result;
2194 changed = 0;
2196 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2197 retry_short = nla_get_u8(
2198 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2199 if (retry_short == 0)
2200 return -EINVAL;
2202 changed |= WIPHY_PARAM_RETRY_SHORT;
2205 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2206 retry_long = nla_get_u8(
2207 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2208 if (retry_long == 0)
2209 return -EINVAL;
2211 changed |= WIPHY_PARAM_RETRY_LONG;
2214 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2215 frag_threshold = nla_get_u32(
2216 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2217 if (frag_threshold < 256)
2218 return -EINVAL;
2220 if (frag_threshold != (u32) -1) {
2222 * Fragments (apart from the last one) are required to
2223 * have even length. Make the fragmentation code
2224 * simpler by stripping LSB should someone try to use
2225 * odd threshold value.
2227 frag_threshold &= ~0x1;
2229 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2232 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2233 rts_threshold = nla_get_u32(
2234 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2235 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2238 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2239 coverage_class = nla_get_u8(
2240 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2241 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2244 if (changed) {
2245 u8 old_retry_short, old_retry_long;
2246 u32 old_frag_threshold, old_rts_threshold;
2247 u8 old_coverage_class;
2249 if (!rdev->ops->set_wiphy_params)
2250 return -EOPNOTSUPP;
2252 old_retry_short = rdev->wiphy.retry_short;
2253 old_retry_long = rdev->wiphy.retry_long;
2254 old_frag_threshold = rdev->wiphy.frag_threshold;
2255 old_rts_threshold = rdev->wiphy.rts_threshold;
2256 old_coverage_class = rdev->wiphy.coverage_class;
2258 if (changed & WIPHY_PARAM_RETRY_SHORT)
2259 rdev->wiphy.retry_short = retry_short;
2260 if (changed & WIPHY_PARAM_RETRY_LONG)
2261 rdev->wiphy.retry_long = retry_long;
2262 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2263 rdev->wiphy.frag_threshold = frag_threshold;
2264 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2265 rdev->wiphy.rts_threshold = rts_threshold;
2266 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2267 rdev->wiphy.coverage_class = coverage_class;
2269 result = rdev_set_wiphy_params(rdev, changed);
2270 if (result) {
2271 rdev->wiphy.retry_short = old_retry_short;
2272 rdev->wiphy.retry_long = old_retry_long;
2273 rdev->wiphy.frag_threshold = old_frag_threshold;
2274 rdev->wiphy.rts_threshold = old_rts_threshold;
2275 rdev->wiphy.coverage_class = old_coverage_class;
2278 return 0;
2281 static inline u64 wdev_id(struct wireless_dev *wdev)
2283 return (u64)wdev->identifier |
2284 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2287 static int nl80211_send_chandef(struct sk_buff *msg,
2288 const struct cfg80211_chan_def *chandef)
2290 WARN_ON(!cfg80211_chandef_valid(chandef));
2292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2293 chandef->chan->center_freq))
2294 return -ENOBUFS;
2295 switch (chandef->width) {
2296 case NL80211_CHAN_WIDTH_20_NOHT:
2297 case NL80211_CHAN_WIDTH_20:
2298 case NL80211_CHAN_WIDTH_40:
2299 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2300 cfg80211_get_chandef_type(chandef)))
2301 return -ENOBUFS;
2302 break;
2303 default:
2304 break;
2306 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2307 return -ENOBUFS;
2308 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2309 return -ENOBUFS;
2310 if (chandef->center_freq2 &&
2311 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2312 return -ENOBUFS;
2313 return 0;
2316 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2317 struct cfg80211_registered_device *rdev,
2318 struct wireless_dev *wdev)
2320 struct net_device *dev = wdev->netdev;
2321 void *hdr;
2323 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
2324 if (!hdr)
2325 return -1;
2327 if (dev &&
2328 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2329 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2330 goto nla_put_failure;
2332 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2333 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2334 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
2335 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2336 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2337 rdev->devlist_generation ^
2338 (cfg80211_rdev_list_generation << 2)))
2339 goto nla_put_failure;
2341 if (rdev->ops->get_channel) {
2342 int ret;
2343 struct cfg80211_chan_def chandef;
2345 ret = rdev_get_channel(rdev, wdev, &chandef);
2346 if (ret == 0) {
2347 if (nl80211_send_chandef(msg, &chandef))
2348 goto nla_put_failure;
2352 if (wdev->ssid_len) {
2353 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2354 goto nla_put_failure;
2357 return genlmsg_end(msg, hdr);
2359 nla_put_failure:
2360 genlmsg_cancel(msg, hdr);
2361 return -EMSGSIZE;
2364 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2366 int wp_idx = 0;
2367 int if_idx = 0;
2368 int wp_start = cb->args[0];
2369 int if_start = cb->args[1];
2370 struct cfg80211_registered_device *rdev;
2371 struct wireless_dev *wdev;
2373 rtnl_lock();
2374 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2375 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2376 continue;
2377 if (wp_idx < wp_start) {
2378 wp_idx++;
2379 continue;
2381 if_idx = 0;
2383 list_for_each_entry(wdev, &rdev->wdev_list, list) {
2384 if (if_idx < if_start) {
2385 if_idx++;
2386 continue;
2388 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2389 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2390 rdev, wdev) < 0) {
2391 goto out;
2393 if_idx++;
2396 wp_idx++;
2398 out:
2399 rtnl_unlock();
2401 cb->args[0] = wp_idx;
2402 cb->args[1] = if_idx;
2404 return skb->len;
2407 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2409 struct sk_buff *msg;
2410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2411 struct wireless_dev *wdev = info->user_ptr[1];
2413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2414 if (!msg)
2415 return -ENOMEM;
2417 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2418 rdev, wdev) < 0) {
2419 nlmsg_free(msg);
2420 return -ENOBUFS;
2423 return genlmsg_reply(msg, info);
2426 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2427 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2428 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2429 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2430 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2431 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2432 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2435 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2437 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2438 int flag;
2440 *mntrflags = 0;
2442 if (!nla)
2443 return -EINVAL;
2445 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2446 nla, mntr_flags_policy))
2447 return -EINVAL;
2449 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2450 if (flags[flag])
2451 *mntrflags |= (1<<flag);
2453 return 0;
2456 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2457 struct net_device *netdev, u8 use_4addr,
2458 enum nl80211_iftype iftype)
2460 if (!use_4addr) {
2461 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2462 return -EBUSY;
2463 return 0;
2466 switch (iftype) {
2467 case NL80211_IFTYPE_AP_VLAN:
2468 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2469 return 0;
2470 break;
2471 case NL80211_IFTYPE_STATION:
2472 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2473 return 0;
2474 break;
2475 default:
2476 break;
2479 return -EOPNOTSUPP;
2482 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2484 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2485 struct vif_params params;
2486 int err;
2487 enum nl80211_iftype otype, ntype;
2488 struct net_device *dev = info->user_ptr[1];
2489 u32 _flags, *flags = NULL;
2490 bool change = false;
2492 memset(&params, 0, sizeof(params));
2494 otype = ntype = dev->ieee80211_ptr->iftype;
2496 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2497 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2498 if (otype != ntype)
2499 change = true;
2500 if (ntype > NL80211_IFTYPE_MAX)
2501 return -EINVAL;
2504 if (info->attrs[NL80211_ATTR_MESH_ID]) {
2505 struct wireless_dev *wdev = dev->ieee80211_ptr;
2507 if (ntype != NL80211_IFTYPE_MESH_POINT)
2508 return -EINVAL;
2509 if (netif_running(dev))
2510 return -EBUSY;
2512 wdev_lock(wdev);
2513 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2514 IEEE80211_MAX_MESH_ID_LEN);
2515 wdev->mesh_id_up_len =
2516 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2517 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2518 wdev->mesh_id_up_len);
2519 wdev_unlock(wdev);
2522 if (info->attrs[NL80211_ATTR_4ADDR]) {
2523 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2524 change = true;
2525 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2526 if (err)
2527 return err;
2528 } else {
2529 params.use_4addr = -1;
2532 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2533 if (ntype != NL80211_IFTYPE_MONITOR)
2534 return -EINVAL;
2535 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2536 &_flags);
2537 if (err)
2538 return err;
2540 flags = &_flags;
2541 change = true;
2544 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
2545 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2546 return -EOPNOTSUPP;
2548 if (change)
2549 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
2550 else
2551 err = 0;
2553 if (!err && params.use_4addr != -1)
2554 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2556 return err;
2559 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2561 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2562 struct vif_params params;
2563 struct wireless_dev *wdev;
2564 struct sk_buff *msg;
2565 int err;
2566 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2567 u32 flags;
2569 /* to avoid failing a new interface creation due to pending removal */
2570 cfg80211_destroy_ifaces(rdev);
2572 memset(&params, 0, sizeof(params));
2574 if (!info->attrs[NL80211_ATTR_IFNAME])
2575 return -EINVAL;
2577 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2578 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2579 if (type > NL80211_IFTYPE_MAX)
2580 return -EINVAL;
2583 if (!rdev->ops->add_virtual_intf ||
2584 !(rdev->wiphy.interface_modes & (1 << type)))
2585 return -EOPNOTSUPP;
2587 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2588 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2589 ETH_ALEN);
2590 if (!is_valid_ether_addr(params.macaddr))
2591 return -EADDRNOTAVAIL;
2594 if (info->attrs[NL80211_ATTR_4ADDR]) {
2595 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2596 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2597 if (err)
2598 return err;
2601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2602 if (!msg)
2603 return -ENOMEM;
2605 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2606 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2607 &flags);
2609 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
2610 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2611 return -EOPNOTSUPP;
2613 wdev = rdev_add_virtual_intf(rdev,
2614 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2615 type, err ? NULL : &flags, &params);
2616 if (IS_ERR(wdev)) {
2617 nlmsg_free(msg);
2618 return PTR_ERR(wdev);
2621 if (info->attrs[NL80211_ATTR_IFACE_SOCKET_OWNER])
2622 wdev->owner_nlportid = info->snd_portid;
2624 switch (type) {
2625 case NL80211_IFTYPE_MESH_POINT:
2626 if (!info->attrs[NL80211_ATTR_MESH_ID])
2627 break;
2628 wdev_lock(wdev);
2629 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2630 IEEE80211_MAX_MESH_ID_LEN);
2631 wdev->mesh_id_up_len =
2632 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2633 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2634 wdev->mesh_id_up_len);
2635 wdev_unlock(wdev);
2636 break;
2637 case NL80211_IFTYPE_P2P_DEVICE:
2639 * P2P Device doesn't have a netdev, so doesn't go
2640 * through the netdev notifier and must be added here
2642 mutex_init(&wdev->mtx);
2643 INIT_LIST_HEAD(&wdev->event_list);
2644 spin_lock_init(&wdev->event_lock);
2645 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2646 spin_lock_init(&wdev->mgmt_registrations_lock);
2648 wdev->identifier = ++rdev->wdev_id;
2649 list_add_rcu(&wdev->list, &rdev->wdev_list);
2650 rdev->devlist_generation++;
2651 break;
2652 default:
2653 break;
2656 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2657 rdev, wdev) < 0) {
2658 nlmsg_free(msg);
2659 return -ENOBUFS;
2662 return genlmsg_reply(msg, info);
2665 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2668 struct wireless_dev *wdev = info->user_ptr[1];
2670 if (!rdev->ops->del_virtual_intf)
2671 return -EOPNOTSUPP;
2674 * If we remove a wireless device without a netdev then clear
2675 * user_ptr[1] so that nl80211_post_doit won't dereference it
2676 * to check if it needs to do dev_put(). Otherwise it crashes
2677 * since the wdev has been freed, unlike with a netdev where
2678 * we need the dev_put() for the netdev to really be freed.
2680 if (!wdev->netdev)
2681 info->user_ptr[1] = NULL;
2683 return rdev_del_virtual_intf(rdev, wdev);
2686 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2689 struct net_device *dev = info->user_ptr[1];
2690 u16 noack_map;
2692 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2693 return -EINVAL;
2695 if (!rdev->ops->set_noack_map)
2696 return -EOPNOTSUPP;
2698 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2700 return rdev_set_noack_map(rdev, dev, noack_map);
2703 struct get_key_cookie {
2704 struct sk_buff *msg;
2705 int error;
2706 int idx;
2709 static void get_key_callback(void *c, struct key_params *params)
2711 struct nlattr *key;
2712 struct get_key_cookie *cookie = c;
2714 if ((params->key &&
2715 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2716 params->key_len, params->key)) ||
2717 (params->seq &&
2718 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2719 params->seq_len, params->seq)) ||
2720 (params->cipher &&
2721 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2722 params->cipher)))
2723 goto nla_put_failure;
2725 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2726 if (!key)
2727 goto nla_put_failure;
2729 if ((params->key &&
2730 nla_put(cookie->msg, NL80211_KEY_DATA,
2731 params->key_len, params->key)) ||
2732 (params->seq &&
2733 nla_put(cookie->msg, NL80211_KEY_SEQ,
2734 params->seq_len, params->seq)) ||
2735 (params->cipher &&
2736 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2737 params->cipher)))
2738 goto nla_put_failure;
2740 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2741 goto nla_put_failure;
2743 nla_nest_end(cookie->msg, key);
2745 return;
2746 nla_put_failure:
2747 cookie->error = 1;
2750 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2752 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2753 int err;
2754 struct net_device *dev = info->user_ptr[1];
2755 u8 key_idx = 0;
2756 const u8 *mac_addr = NULL;
2757 bool pairwise;
2758 struct get_key_cookie cookie = {
2759 .error = 0,
2761 void *hdr;
2762 struct sk_buff *msg;
2764 if (info->attrs[NL80211_ATTR_KEY_IDX])
2765 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2767 if (key_idx > 5)
2768 return -EINVAL;
2770 if (info->attrs[NL80211_ATTR_MAC])
2771 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2773 pairwise = !!mac_addr;
2774 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2775 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2776 if (kt >= NUM_NL80211_KEYTYPES)
2777 return -EINVAL;
2778 if (kt != NL80211_KEYTYPE_GROUP &&
2779 kt != NL80211_KEYTYPE_PAIRWISE)
2780 return -EINVAL;
2781 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2784 if (!rdev->ops->get_key)
2785 return -EOPNOTSUPP;
2787 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2788 return -ENOENT;
2790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2791 if (!msg)
2792 return -ENOMEM;
2794 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
2795 NL80211_CMD_NEW_KEY);
2796 if (!hdr)
2797 goto nla_put_failure;
2799 cookie.msg = msg;
2800 cookie.idx = key_idx;
2802 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2803 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2804 goto nla_put_failure;
2805 if (mac_addr &&
2806 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2807 goto nla_put_failure;
2809 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2810 get_key_callback);
2812 if (err)
2813 goto free_msg;
2815 if (cookie.error)
2816 goto nla_put_failure;
2818 genlmsg_end(msg, hdr);
2819 return genlmsg_reply(msg, info);
2821 nla_put_failure:
2822 err = -ENOBUFS;
2823 free_msg:
2824 nlmsg_free(msg);
2825 return err;
2828 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2830 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2831 struct key_parse key;
2832 int err;
2833 struct net_device *dev = info->user_ptr[1];
2835 err = nl80211_parse_key(info, &key);
2836 if (err)
2837 return err;
2839 if (key.idx < 0)
2840 return -EINVAL;
2842 /* only support setting default key */
2843 if (!key.def && !key.defmgmt)
2844 return -EINVAL;
2846 wdev_lock(dev->ieee80211_ptr);
2848 if (key.def) {
2849 if (!rdev->ops->set_default_key) {
2850 err = -EOPNOTSUPP;
2851 goto out;
2854 err = nl80211_key_allowed(dev->ieee80211_ptr);
2855 if (err)
2856 goto out;
2858 err = rdev_set_default_key(rdev, dev, key.idx,
2859 key.def_uni, key.def_multi);
2861 if (err)
2862 goto out;
2864 #ifdef CONFIG_CFG80211_WEXT
2865 dev->ieee80211_ptr->wext.default_key = key.idx;
2866 #endif
2867 } else {
2868 if (key.def_uni || !key.def_multi) {
2869 err = -EINVAL;
2870 goto out;
2873 if (!rdev->ops->set_default_mgmt_key) {
2874 err = -EOPNOTSUPP;
2875 goto out;
2878 err = nl80211_key_allowed(dev->ieee80211_ptr);
2879 if (err)
2880 goto out;
2882 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
2883 if (err)
2884 goto out;
2886 #ifdef CONFIG_CFG80211_WEXT
2887 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2888 #endif
2891 out:
2892 wdev_unlock(dev->ieee80211_ptr);
2894 return err;
2897 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2900 int err;
2901 struct net_device *dev = info->user_ptr[1];
2902 struct key_parse key;
2903 const u8 *mac_addr = NULL;
2905 err = nl80211_parse_key(info, &key);
2906 if (err)
2907 return err;
2909 if (!key.p.key)
2910 return -EINVAL;
2912 if (info->attrs[NL80211_ATTR_MAC])
2913 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2915 if (key.type == -1) {
2916 if (mac_addr)
2917 key.type = NL80211_KEYTYPE_PAIRWISE;
2918 else
2919 key.type = NL80211_KEYTYPE_GROUP;
2922 /* for now */
2923 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2924 key.type != NL80211_KEYTYPE_GROUP)
2925 return -EINVAL;
2927 if (!rdev->ops->add_key)
2928 return -EOPNOTSUPP;
2930 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2931 key.type == NL80211_KEYTYPE_PAIRWISE,
2932 mac_addr))
2933 return -EINVAL;
2935 wdev_lock(dev->ieee80211_ptr);
2936 err = nl80211_key_allowed(dev->ieee80211_ptr);
2937 if (!err)
2938 err = rdev_add_key(rdev, dev, key.idx,
2939 key.type == NL80211_KEYTYPE_PAIRWISE,
2940 mac_addr, &key.p);
2941 wdev_unlock(dev->ieee80211_ptr);
2943 return err;
2946 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2948 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2949 int err;
2950 struct net_device *dev = info->user_ptr[1];
2951 u8 *mac_addr = NULL;
2952 struct key_parse key;
2954 err = nl80211_parse_key(info, &key);
2955 if (err)
2956 return err;
2958 if (info->attrs[NL80211_ATTR_MAC])
2959 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2961 if (key.type == -1) {
2962 if (mac_addr)
2963 key.type = NL80211_KEYTYPE_PAIRWISE;
2964 else
2965 key.type = NL80211_KEYTYPE_GROUP;
2968 /* for now */
2969 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2970 key.type != NL80211_KEYTYPE_GROUP)
2971 return -EINVAL;
2973 if (!rdev->ops->del_key)
2974 return -EOPNOTSUPP;
2976 wdev_lock(dev->ieee80211_ptr);
2977 err = nl80211_key_allowed(dev->ieee80211_ptr);
2979 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
2980 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2981 err = -ENOENT;
2983 if (!err)
2984 err = rdev_del_key(rdev, dev, key.idx,
2985 key.type == NL80211_KEYTYPE_PAIRWISE,
2986 mac_addr);
2988 #ifdef CONFIG_CFG80211_WEXT
2989 if (!err) {
2990 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2991 dev->ieee80211_ptr->wext.default_key = -1;
2992 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2993 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2995 #endif
2996 wdev_unlock(dev->ieee80211_ptr);
2998 return err;
3001 /* This function returns an error or the number of nested attributes */
3002 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3004 struct nlattr *attr;
3005 int n_entries = 0, tmp;
3007 nla_for_each_nested(attr, nl_attr, tmp) {
3008 if (nla_len(attr) != ETH_ALEN)
3009 return -EINVAL;
3011 n_entries++;
3014 return n_entries;
3018 * This function parses ACL information and allocates memory for ACL data.
3019 * On successful return, the calling function is responsible to free the
3020 * ACL buffer returned by this function.
3022 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3023 struct genl_info *info)
3025 enum nl80211_acl_policy acl_policy;
3026 struct nlattr *attr;
3027 struct cfg80211_acl_data *acl;
3028 int i = 0, n_entries, tmp;
3030 if (!wiphy->max_acl_mac_addrs)
3031 return ERR_PTR(-EOPNOTSUPP);
3033 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3034 return ERR_PTR(-EINVAL);
3036 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3037 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3038 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3039 return ERR_PTR(-EINVAL);
3041 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3042 return ERR_PTR(-EINVAL);
3044 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3045 if (n_entries < 0)
3046 return ERR_PTR(n_entries);
3048 if (n_entries > wiphy->max_acl_mac_addrs)
3049 return ERR_PTR(-ENOTSUPP);
3051 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3052 GFP_KERNEL);
3053 if (!acl)
3054 return ERR_PTR(-ENOMEM);
3056 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3057 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3058 i++;
3061 acl->n_acl_entries = n_entries;
3062 acl->acl_policy = acl_policy;
3064 return acl;
3067 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3069 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3070 struct net_device *dev = info->user_ptr[1];
3071 struct cfg80211_acl_data *acl;
3072 int err;
3074 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3075 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3076 return -EOPNOTSUPP;
3078 if (!dev->ieee80211_ptr->beacon_interval)
3079 return -EINVAL;
3081 acl = parse_acl_data(&rdev->wiphy, info);
3082 if (IS_ERR(acl))
3083 return PTR_ERR(acl);
3085 err = rdev_set_mac_acl(rdev, dev, acl);
3087 kfree(acl);
3089 return err;
3092 static int nl80211_parse_beacon(struct nlattr *attrs[],
3093 struct cfg80211_beacon_data *bcn)
3095 bool haveinfo = false;
3097 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3098 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3099 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3100 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3101 return -EINVAL;
3103 memset(bcn, 0, sizeof(*bcn));
3105 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3106 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3107 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3108 if (!bcn->head_len)
3109 return -EINVAL;
3110 haveinfo = true;
3113 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3114 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3115 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3116 haveinfo = true;
3119 if (!haveinfo)
3120 return -EINVAL;
3122 if (attrs[NL80211_ATTR_IE]) {
3123 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3124 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3127 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3128 bcn->proberesp_ies =
3129 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3130 bcn->proberesp_ies_len =
3131 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3134 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3135 bcn->assocresp_ies =
3136 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3137 bcn->assocresp_ies_len =
3138 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3141 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3142 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3143 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3146 return 0;
3149 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3150 struct cfg80211_ap_settings *params)
3152 struct wireless_dev *wdev;
3153 bool ret = false;
3155 list_for_each_entry(wdev, &rdev->wdev_list, list) {
3156 if (wdev->iftype != NL80211_IFTYPE_AP &&
3157 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3158 continue;
3160 if (!wdev->preset_chandef.chan)
3161 continue;
3163 params->chandef = wdev->preset_chandef;
3164 ret = true;
3165 break;
3168 return ret;
3171 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3172 enum nl80211_auth_type auth_type,
3173 enum nl80211_commands cmd)
3175 if (auth_type > NL80211_AUTHTYPE_MAX)
3176 return false;
3178 switch (cmd) {
3179 case NL80211_CMD_AUTHENTICATE:
3180 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3181 auth_type == NL80211_AUTHTYPE_SAE)
3182 return false;
3183 return true;
3184 case NL80211_CMD_CONNECT:
3185 case NL80211_CMD_START_AP:
3186 /* SAE not supported yet */
3187 if (auth_type == NL80211_AUTHTYPE_SAE)
3188 return false;
3189 return true;
3190 default:
3191 return false;
3195 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3198 struct net_device *dev = info->user_ptr[1];
3199 struct wireless_dev *wdev = dev->ieee80211_ptr;
3200 struct cfg80211_ap_settings params;
3201 int err;
3203 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3204 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3205 return -EOPNOTSUPP;
3207 if (!rdev->ops->start_ap)
3208 return -EOPNOTSUPP;
3210 if (wdev->beacon_interval)
3211 return -EALREADY;
3213 memset(&params, 0, sizeof(params));
3215 /* these are required for START_AP */
3216 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3217 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3218 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3219 return -EINVAL;
3221 err = nl80211_parse_beacon(info->attrs, &params.beacon);
3222 if (err)
3223 return err;
3225 params.beacon_interval =
3226 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3227 params.dtim_period =
3228 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3230 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3231 if (err)
3232 return err;
3235 * In theory, some of these attributes should be required here
3236 * but since they were not used when the command was originally
3237 * added, keep them optional for old user space programs to let
3238 * them continue to work with drivers that do not need the
3239 * additional information -- drivers must check!
3241 if (info->attrs[NL80211_ATTR_SSID]) {
3242 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3243 params.ssid_len =
3244 nla_len(info->attrs[NL80211_ATTR_SSID]);
3245 if (params.ssid_len == 0 ||
3246 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3247 return -EINVAL;
3250 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3251 params.hidden_ssid = nla_get_u32(
3252 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3253 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3254 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3255 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3256 return -EINVAL;
3259 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3261 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3262 params.auth_type = nla_get_u32(
3263 info->attrs[NL80211_ATTR_AUTH_TYPE]);
3264 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3265 NL80211_CMD_START_AP))
3266 return -EINVAL;
3267 } else
3268 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3270 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3271 NL80211_MAX_NR_CIPHER_SUITES);
3272 if (err)
3273 return err;
3275 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3276 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3277 return -EOPNOTSUPP;
3278 params.inactivity_timeout = nla_get_u16(
3279 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3282 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3283 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3284 return -EINVAL;
3285 params.p2p_ctwindow =
3286 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3287 if (params.p2p_ctwindow > 127)
3288 return -EINVAL;
3289 if (params.p2p_ctwindow != 0 &&
3290 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3291 return -EINVAL;
3294 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3295 u8 tmp;
3297 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3298 return -EINVAL;
3299 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3300 if (tmp > 1)
3301 return -EINVAL;
3302 params.p2p_opp_ps = tmp;
3303 if (params.p2p_opp_ps != 0 &&
3304 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3305 return -EINVAL;
3308 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3309 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3310 if (err)
3311 return err;
3312 } else if (wdev->preset_chandef.chan) {
3313 params.chandef = wdev->preset_chandef;
3314 } else if (!nl80211_get_ap_channel(rdev, &params))
3315 return -EINVAL;
3317 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
3318 wdev->iftype))
3319 return -EINVAL;
3321 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3322 params.acl = parse_acl_data(&rdev->wiphy, info);
3323 if (IS_ERR(params.acl))
3324 return PTR_ERR(params.acl);
3327 wdev_lock(wdev);
3328 err = rdev_start_ap(rdev, dev, &params);
3329 if (!err) {
3330 wdev->preset_chandef = params.chandef;
3331 wdev->beacon_interval = params.beacon_interval;
3332 wdev->chandef = params.chandef;
3333 wdev->ssid_len = params.ssid_len;
3334 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
3336 wdev_unlock(wdev);
3338 kfree(params.acl);
3340 return err;
3343 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346 struct net_device *dev = info->user_ptr[1];
3347 struct wireless_dev *wdev = dev->ieee80211_ptr;
3348 struct cfg80211_beacon_data params;
3349 int err;
3351 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3352 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3353 return -EOPNOTSUPP;
3355 if (!rdev->ops->change_beacon)
3356 return -EOPNOTSUPP;
3358 if (!wdev->beacon_interval)
3359 return -EINVAL;
3361 err = nl80211_parse_beacon(info->attrs, &params);
3362 if (err)
3363 return err;
3365 wdev_lock(wdev);
3366 err = rdev_change_beacon(rdev, dev, &params);
3367 wdev_unlock(wdev);
3369 return err;
3372 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
3374 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3375 struct net_device *dev = info->user_ptr[1];
3377 return cfg80211_stop_ap(rdev, dev, false);
3380 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3381 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3382 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3383 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
3384 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
3385 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
3386 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
3389 static int parse_station_flags(struct genl_info *info,
3390 enum nl80211_iftype iftype,
3391 struct station_parameters *params)
3393 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
3394 struct nlattr *nla;
3395 int flag;
3398 * Try parsing the new attribute first so userspace
3399 * can specify both for older kernels.
3401 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3402 if (nla) {
3403 struct nl80211_sta_flag_update *sta_flags;
3405 sta_flags = nla_data(nla);
3406 params->sta_flags_mask = sta_flags->mask;
3407 params->sta_flags_set = sta_flags->set;
3408 params->sta_flags_set &= params->sta_flags_mask;
3409 if ((params->sta_flags_mask |
3410 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3411 return -EINVAL;
3412 return 0;
3415 /* if present, parse the old attribute */
3417 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
3418 if (!nla)
3419 return 0;
3421 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3422 nla, sta_flags_policy))
3423 return -EINVAL;
3426 * Only allow certain flags for interface types so that
3427 * other attributes are silently ignored. Remember that
3428 * this is backward compatibility code with old userspace
3429 * and shouldn't be hit in other cases anyway.
3431 switch (iftype) {
3432 case NL80211_IFTYPE_AP:
3433 case NL80211_IFTYPE_AP_VLAN:
3434 case NL80211_IFTYPE_P2P_GO:
3435 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3436 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3437 BIT(NL80211_STA_FLAG_WME) |
3438 BIT(NL80211_STA_FLAG_MFP);
3439 break;
3440 case NL80211_IFTYPE_P2P_CLIENT:
3441 case NL80211_IFTYPE_STATION:
3442 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3443 BIT(NL80211_STA_FLAG_TDLS_PEER);
3444 break;
3445 case NL80211_IFTYPE_MESH_POINT:
3446 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3447 BIT(NL80211_STA_FLAG_MFP) |
3448 BIT(NL80211_STA_FLAG_AUTHORIZED);
3449 default:
3450 return -EINVAL;
3453 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3454 if (flags[flag]) {
3455 params->sta_flags_set |= (1<<flag);
3457 /* no longer support new API additions in old API */
3458 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3459 return -EINVAL;
3463 return 0;
3466 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3467 int attr)
3469 struct nlattr *rate;
3470 u32 bitrate;
3471 u16 bitrate_compat;
3473 rate = nla_nest_start(msg, attr);
3474 if (!rate)
3475 return false;
3477 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3478 bitrate = cfg80211_calculate_bitrate(info);
3479 /* report 16-bit bitrate only if we can */
3480 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
3481 if (bitrate > 0 &&
3482 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3483 return false;
3484 if (bitrate_compat > 0 &&
3485 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3486 return false;
3488 if (info->flags & RATE_INFO_FLAGS_MCS) {
3489 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3490 return false;
3491 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3492 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3493 return false;
3494 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3495 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3496 return false;
3497 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3498 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3499 return false;
3500 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3501 return false;
3502 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3503 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3504 return false;
3505 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3506 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3507 return false;
3508 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3509 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3510 return false;
3511 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3512 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3513 return false;
3514 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3515 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3516 return false;
3519 nla_nest_end(msg, rate);
3520 return true;
3523 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3524 int id)
3526 void *attr;
3527 int i = 0;
3529 if (!mask)
3530 return true;
3532 attr = nla_nest_start(msg, id);
3533 if (!attr)
3534 return false;
3536 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3537 if (!(mask & BIT(i)))
3538 continue;
3540 if (nla_put_u8(msg, i, signal[i]))
3541 return false;
3544 nla_nest_end(msg, attr);
3546 return true;
3549 static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
3550 int flags,
3551 struct cfg80211_registered_device *rdev,
3552 struct net_device *dev,
3553 const u8 *mac_addr, struct station_info *sinfo)
3555 void *hdr;
3556 struct nlattr *sinfoattr, *bss_param;
3558 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
3559 if (!hdr)
3560 return -1;
3562 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3563 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3564 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3565 goto nla_put_failure;
3567 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3568 if (!sinfoattr)
3569 goto nla_put_failure;
3570 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3571 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3572 sinfo->connected_time))
3573 goto nla_put_failure;
3574 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3575 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3576 sinfo->inactive_time))
3577 goto nla_put_failure;
3578 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3579 STATION_INFO_RX_BYTES64)) &&
3580 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
3581 (u32)sinfo->rx_bytes))
3582 goto nla_put_failure;
3583 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3584 STATION_INFO_TX_BYTES64)) &&
3585 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3586 (u32)sinfo->tx_bytes))
3587 goto nla_put_failure;
3588 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3589 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
3590 sinfo->rx_bytes))
3591 goto nla_put_failure;
3592 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3593 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
3594 sinfo->tx_bytes))
3595 goto nla_put_failure;
3596 if ((sinfo->filled & STATION_INFO_LLID) &&
3597 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3598 goto nla_put_failure;
3599 if ((sinfo->filled & STATION_INFO_PLID) &&
3600 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3601 goto nla_put_failure;
3602 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3603 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3604 sinfo->plink_state))
3605 goto nla_put_failure;
3606 switch (rdev->wiphy.signal_type) {
3607 case CFG80211_SIGNAL_TYPE_MBM:
3608 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3609 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3610 sinfo->signal))
3611 goto nla_put_failure;
3612 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3613 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3614 sinfo->signal_avg))
3615 goto nla_put_failure;
3616 break;
3617 default:
3618 break;
3620 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3621 if (!nl80211_put_signal(msg, sinfo->chains,
3622 sinfo->chain_signal,
3623 NL80211_STA_INFO_CHAIN_SIGNAL))
3624 goto nla_put_failure;
3626 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3627 if (!nl80211_put_signal(msg, sinfo->chains,
3628 sinfo->chain_signal_avg,
3629 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3630 goto nla_put_failure;
3632 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
3633 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3634 NL80211_STA_INFO_TX_BITRATE))
3635 goto nla_put_failure;
3637 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3638 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3639 NL80211_STA_INFO_RX_BITRATE))
3640 goto nla_put_failure;
3642 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3643 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3644 sinfo->rx_packets))
3645 goto nla_put_failure;
3646 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3647 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3648 sinfo->tx_packets))
3649 goto nla_put_failure;
3650 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3651 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3652 sinfo->tx_retries))
3653 goto nla_put_failure;
3654 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3655 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3656 sinfo->tx_failed))
3657 goto nla_put_failure;
3658 if ((sinfo->filled & STATION_INFO_EXPECTED_THROUGHPUT) &&
3659 nla_put_u32(msg, NL80211_STA_INFO_EXPECTED_THROUGHPUT,
3660 sinfo->expected_throughput))
3661 goto nla_put_failure;
3662 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3663 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3664 sinfo->beacon_loss_count))
3665 goto nla_put_failure;
3666 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3667 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3668 sinfo->local_pm))
3669 goto nla_put_failure;
3670 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3671 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3672 sinfo->peer_pm))
3673 goto nla_put_failure;
3674 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3675 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3676 sinfo->nonpeer_pm))
3677 goto nla_put_failure;
3678 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3679 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3680 if (!bss_param)
3681 goto nla_put_failure;
3683 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3684 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3685 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3686 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3687 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3688 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3689 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3690 sinfo->bss_param.dtim_period) ||
3691 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3692 sinfo->bss_param.beacon_interval))
3693 goto nla_put_failure;
3695 nla_nest_end(msg, bss_param);
3697 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3698 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3699 sizeof(struct nl80211_sta_flag_update),
3700 &sinfo->sta_flags))
3701 goto nla_put_failure;
3702 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3703 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3704 sinfo->t_offset))
3705 goto nla_put_failure;
3706 nla_nest_end(msg, sinfoattr);
3708 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3709 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3710 sinfo->assoc_req_ies))
3711 goto nla_put_failure;
3713 return genlmsg_end(msg, hdr);
3715 nla_put_failure:
3716 genlmsg_cancel(msg, hdr);
3717 return -EMSGSIZE;
3720 static int nl80211_dump_station(struct sk_buff *skb,
3721 struct netlink_callback *cb)
3723 struct station_info sinfo;
3724 struct cfg80211_registered_device *rdev;
3725 struct wireless_dev *wdev;
3726 u8 mac_addr[ETH_ALEN];
3727 int sta_idx = cb->args[2];
3728 int err;
3730 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
3731 if (err)
3732 return err;
3734 if (!wdev->netdev) {
3735 err = -EINVAL;
3736 goto out_err;
3739 if (!rdev->ops->dump_station) {
3740 err = -EOPNOTSUPP;
3741 goto out_err;
3744 while (1) {
3745 memset(&sinfo, 0, sizeof(sinfo));
3746 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
3747 mac_addr, &sinfo);
3748 if (err == -ENOENT)
3749 break;
3750 if (err)
3751 goto out_err;
3753 if (nl80211_send_station(skb,
3754 NETLINK_CB(cb->skb).portid,
3755 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3756 rdev, wdev->netdev, mac_addr,
3757 &sinfo) < 0)
3758 goto out;
3760 sta_idx++;
3764 out:
3765 cb->args[2] = sta_idx;
3766 err = skb->len;
3767 out_err:
3768 nl80211_finish_wdev_dump(rdev);
3770 return err;
3773 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3775 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3776 struct net_device *dev = info->user_ptr[1];
3777 struct station_info sinfo;
3778 struct sk_buff *msg;
3779 u8 *mac_addr = NULL;
3780 int err;
3782 memset(&sinfo, 0, sizeof(sinfo));
3784 if (!info->attrs[NL80211_ATTR_MAC])
3785 return -EINVAL;
3787 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3789 if (!rdev->ops->get_station)
3790 return -EOPNOTSUPP;
3792 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
3793 if (err)
3794 return err;
3796 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3797 if (!msg)
3798 return -ENOMEM;
3800 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
3801 rdev, dev, mac_addr, &sinfo) < 0) {
3802 nlmsg_free(msg);
3803 return -ENOBUFS;
3806 return genlmsg_reply(msg, info);
3809 int cfg80211_check_station_change(struct wiphy *wiphy,
3810 struct station_parameters *params,
3811 enum cfg80211_station_type statype)
3813 if (params->listen_interval != -1)
3814 return -EINVAL;
3815 if (params->aid)
3816 return -EINVAL;
3818 /* When you run into this, adjust the code below for the new flag */
3819 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3821 switch (statype) {
3822 case CFG80211_STA_MESH_PEER_KERNEL:
3823 case CFG80211_STA_MESH_PEER_USER:
3825 * No ignoring the TDLS flag here -- the userspace mesh
3826 * code doesn't have the bug of including TDLS in the
3827 * mask everywhere.
3829 if (params->sta_flags_mask &
3830 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3831 BIT(NL80211_STA_FLAG_MFP) |
3832 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3833 return -EINVAL;
3834 break;
3835 case CFG80211_STA_TDLS_PEER_SETUP:
3836 case CFG80211_STA_TDLS_PEER_ACTIVE:
3837 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3838 return -EINVAL;
3839 /* ignore since it can't change */
3840 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3841 break;
3842 default:
3843 /* disallow mesh-specific things */
3844 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3845 return -EINVAL;
3846 if (params->local_pm)
3847 return -EINVAL;
3848 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3849 return -EINVAL;
3852 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3853 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3854 /* TDLS can't be set, ... */
3855 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3856 return -EINVAL;
3858 * ... but don't bother the driver with it. This works around
3859 * a hostapd/wpa_supplicant issue -- it always includes the
3860 * TLDS_PEER flag in the mask even for AP mode.
3862 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3865 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3866 /* reject other things that can't change */
3867 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3868 return -EINVAL;
3869 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3870 return -EINVAL;
3871 if (params->supported_rates)
3872 return -EINVAL;
3873 if (params->ext_capab || params->ht_capa || params->vht_capa)
3874 return -EINVAL;
3877 if (statype != CFG80211_STA_AP_CLIENT) {
3878 if (params->vlan)
3879 return -EINVAL;
3882 switch (statype) {
3883 case CFG80211_STA_AP_MLME_CLIENT:
3884 /* Use this only for authorizing/unauthorizing a station */
3885 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3886 return -EOPNOTSUPP;
3887 break;
3888 case CFG80211_STA_AP_CLIENT:
3889 /* accept only the listed bits */
3890 if (params->sta_flags_mask &
3891 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3892 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3893 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3894 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3895 BIT(NL80211_STA_FLAG_WME) |
3896 BIT(NL80211_STA_FLAG_MFP)))
3897 return -EINVAL;
3899 /* but authenticated/associated only if driver handles it */
3900 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3901 params->sta_flags_mask &
3902 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3903 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3904 return -EINVAL;
3905 break;
3906 case CFG80211_STA_IBSS:
3907 case CFG80211_STA_AP_STA:
3908 /* reject any changes other than AUTHORIZED */
3909 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3910 return -EINVAL;
3911 break;
3912 case CFG80211_STA_TDLS_PEER_SETUP:
3913 /* reject any changes other than AUTHORIZED or WME */
3914 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3915 BIT(NL80211_STA_FLAG_WME)))
3916 return -EINVAL;
3917 /* force (at least) rates when authorizing */
3918 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3919 !params->supported_rates)
3920 return -EINVAL;
3921 break;
3922 case CFG80211_STA_TDLS_PEER_ACTIVE:
3923 /* reject any changes */
3924 return -EINVAL;
3925 case CFG80211_STA_MESH_PEER_KERNEL:
3926 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3927 return -EINVAL;
3928 break;
3929 case CFG80211_STA_MESH_PEER_USER:
3930 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3931 return -EINVAL;
3932 break;
3935 return 0;
3937 EXPORT_SYMBOL(cfg80211_check_station_change);
3940 * Get vlan interface making sure it is running and on the right wiphy.
3942 static struct net_device *get_vlan(struct genl_info *info,
3943 struct cfg80211_registered_device *rdev)
3945 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
3946 struct net_device *v;
3947 int ret;
3949 if (!vlanattr)
3950 return NULL;
3952 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3953 if (!v)
3954 return ERR_PTR(-ENODEV);
3956 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3957 ret = -EINVAL;
3958 goto error;
3961 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3962 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3963 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3964 ret = -EINVAL;
3965 goto error;
3968 if (!netif_running(v)) {
3969 ret = -ENETDOWN;
3970 goto error;
3973 return v;
3974 error:
3975 dev_put(v);
3976 return ERR_PTR(ret);
3979 static const struct nla_policy
3980 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
3981 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3982 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3985 static int nl80211_parse_sta_wme(struct genl_info *info,
3986 struct station_parameters *params)
3988 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3989 struct nlattr *nla;
3990 int err;
3992 /* parse WME attributes if present */
3993 if (!info->attrs[NL80211_ATTR_STA_WME])
3994 return 0;
3996 nla = info->attrs[NL80211_ATTR_STA_WME];
3997 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3998 nl80211_sta_wme_policy);
3999 if (err)
4000 return err;
4002 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4003 params->uapsd_queues = nla_get_u8(
4004 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4005 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4006 return -EINVAL;
4008 if (tb[NL80211_STA_WME_MAX_SP])
4009 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4011 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4012 return -EINVAL;
4014 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4016 return 0;
4019 static int nl80211_parse_sta_channel_info(struct genl_info *info,
4020 struct station_parameters *params)
4022 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4023 params->supported_channels =
4024 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4025 params->supported_channels_len =
4026 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4028 * Need to include at least one (first channel, number of
4029 * channels) tuple for each subband, and must have proper
4030 * tuples for the rest of the data as well.
4032 if (params->supported_channels_len < 2)
4033 return -EINVAL;
4034 if (params->supported_channels_len % 2)
4035 return -EINVAL;
4038 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4039 params->supported_oper_classes =
4040 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4041 params->supported_oper_classes_len =
4042 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4044 * The value of the Length field of the Supported Operating
4045 * Classes element is between 2 and 253.
4047 if (params->supported_oper_classes_len < 2 ||
4048 params->supported_oper_classes_len > 253)
4049 return -EINVAL;
4051 return 0;
4054 static int nl80211_set_station_tdls(struct genl_info *info,
4055 struct station_parameters *params)
4057 int err;
4058 /* Dummy STA entry gets updated once the peer capabilities are known */
4059 if (info->attrs[NL80211_ATTR_PEER_AID])
4060 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4061 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4062 params->ht_capa =
4063 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4064 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4065 params->vht_capa =
4066 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4068 err = nl80211_parse_sta_channel_info(info, params);
4069 if (err)
4070 return err;
4072 return nl80211_parse_sta_wme(info, params);
4075 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4077 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4078 struct net_device *dev = info->user_ptr[1];
4079 struct station_parameters params;
4080 u8 *mac_addr;
4081 int err;
4083 memset(&params, 0, sizeof(params));
4085 params.listen_interval = -1;
4087 if (!rdev->ops->change_station)
4088 return -EOPNOTSUPP;
4090 if (info->attrs[NL80211_ATTR_STA_AID])
4091 return -EINVAL;
4093 if (!info->attrs[NL80211_ATTR_MAC])
4094 return -EINVAL;
4096 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4098 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4099 params.supported_rates =
4100 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4101 params.supported_rates_len =
4102 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4105 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4106 params.capability =
4107 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4108 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4111 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4112 params.ext_capab =
4113 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4114 params.ext_capab_len =
4115 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4118 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4119 return -EINVAL;
4121 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4122 return -EINVAL;
4124 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4125 params.plink_action =
4126 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4127 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4128 return -EINVAL;
4131 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
4132 params.plink_state =
4133 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4134 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4135 return -EINVAL;
4136 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4139 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4140 enum nl80211_mesh_power_mode pm = nla_get_u32(
4141 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4143 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4144 pm > NL80211_MESH_POWER_MAX)
4145 return -EINVAL;
4147 params.local_pm = pm;
4150 /* Include parameters for TDLS peer (will check later) */
4151 err = nl80211_set_station_tdls(info, &params);
4152 if (err)
4153 return err;
4155 params.vlan = get_vlan(info, rdev);
4156 if (IS_ERR(params.vlan))
4157 return PTR_ERR(params.vlan);
4159 switch (dev->ieee80211_ptr->iftype) {
4160 case NL80211_IFTYPE_AP:
4161 case NL80211_IFTYPE_AP_VLAN:
4162 case NL80211_IFTYPE_P2P_GO:
4163 case NL80211_IFTYPE_P2P_CLIENT:
4164 case NL80211_IFTYPE_STATION:
4165 case NL80211_IFTYPE_ADHOC:
4166 case NL80211_IFTYPE_MESH_POINT:
4167 break;
4168 default:
4169 err = -EOPNOTSUPP;
4170 goto out_put_vlan;
4173 /* driver will call cfg80211_check_station_change() */
4174 err = rdev_change_station(rdev, dev, mac_addr, &params);
4176 out_put_vlan:
4177 if (params.vlan)
4178 dev_put(params.vlan);
4180 return err;
4183 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4185 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4186 int err;
4187 struct net_device *dev = info->user_ptr[1];
4188 struct station_parameters params;
4189 u8 *mac_addr = NULL;
4191 memset(&params, 0, sizeof(params));
4193 if (!rdev->ops->add_station)
4194 return -EOPNOTSUPP;
4196 if (!info->attrs[NL80211_ATTR_MAC])
4197 return -EINVAL;
4199 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4200 return -EINVAL;
4202 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4203 return -EINVAL;
4205 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4206 !info->attrs[NL80211_ATTR_PEER_AID])
4207 return -EINVAL;
4209 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4210 params.supported_rates =
4211 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4212 params.supported_rates_len =
4213 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4214 params.listen_interval =
4215 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4217 if (info->attrs[NL80211_ATTR_PEER_AID])
4218 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4219 else
4220 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4221 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4222 return -EINVAL;
4224 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4225 params.capability =
4226 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4227 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4230 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4231 params.ext_capab =
4232 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4233 params.ext_capab_len =
4234 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4237 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4238 params.ht_capa =
4239 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4241 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4242 params.vht_capa =
4243 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4245 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4246 params.opmode_notif_used = true;
4247 params.opmode_notif =
4248 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4251 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4252 params.plink_action =
4253 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4254 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4255 return -EINVAL;
4258 err = nl80211_parse_sta_channel_info(info, &params);
4259 if (err)
4260 return err;
4262 err = nl80211_parse_sta_wme(info, &params);
4263 if (err)
4264 return err;
4266 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4267 return -EINVAL;
4269 /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
4270 * as userspace might just pass through the capabilities from the IEs
4271 * directly, rather than enforcing this restriction and returning an
4272 * error in this case.
4274 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
4275 params.ht_capa = NULL;
4276 params.vht_capa = NULL;
4279 /* When you run into this, adjust the code below for the new flag */
4280 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4282 switch (dev->ieee80211_ptr->iftype) {
4283 case NL80211_IFTYPE_AP:
4284 case NL80211_IFTYPE_AP_VLAN:
4285 case NL80211_IFTYPE_P2P_GO:
4286 /* ignore WME attributes if iface/sta is not capable */
4287 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4288 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4289 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4291 /* TDLS peers cannot be added */
4292 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4293 info->attrs[NL80211_ATTR_PEER_AID])
4294 return -EINVAL;
4295 /* but don't bother the driver with it */
4296 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4298 /* allow authenticated/associated only if driver handles it */
4299 if (!(rdev->wiphy.features &
4300 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4301 params.sta_flags_mask &
4302 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4303 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4304 return -EINVAL;
4306 /* must be last in here for error handling */
4307 params.vlan = get_vlan(info, rdev);
4308 if (IS_ERR(params.vlan))
4309 return PTR_ERR(params.vlan);
4310 break;
4311 case NL80211_IFTYPE_MESH_POINT:
4312 /* ignore uAPSD data */
4313 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4315 /* associated is disallowed */
4316 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4317 return -EINVAL;
4318 /* TDLS peers cannot be added */
4319 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4320 info->attrs[NL80211_ATTR_PEER_AID])
4321 return -EINVAL;
4322 break;
4323 case NL80211_IFTYPE_STATION:
4324 case NL80211_IFTYPE_P2P_CLIENT:
4325 /* ignore uAPSD data */
4326 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4328 /* these are disallowed */
4329 if (params.sta_flags_mask &
4330 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4331 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
4332 return -EINVAL;
4333 /* Only TDLS peers can be added */
4334 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4335 return -EINVAL;
4336 /* Can only add if TDLS ... */
4337 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4338 return -EOPNOTSUPP;
4339 /* ... with external setup is supported */
4340 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4341 return -EOPNOTSUPP;
4343 * Older wpa_supplicant versions always mark the TDLS peer
4344 * as authorized, but it shouldn't yet be.
4346 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
4347 break;
4348 default:
4349 return -EOPNOTSUPP;
4352 /* be aware of params.vlan when changing code here */
4354 err = rdev_add_station(rdev, dev, mac_addr, &params);
4356 if (params.vlan)
4357 dev_put(params.vlan);
4358 return err;
4361 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4363 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4364 struct net_device *dev = info->user_ptr[1];
4365 u8 *mac_addr = NULL;
4367 if (info->attrs[NL80211_ATTR_MAC])
4368 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4370 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4371 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4372 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4373 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4374 return -EINVAL;
4376 if (!rdev->ops->del_station)
4377 return -EOPNOTSUPP;
4379 return rdev_del_station(rdev, dev, mac_addr);
4382 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
4383 int flags, struct net_device *dev,
4384 u8 *dst, u8 *next_hop,
4385 struct mpath_info *pinfo)
4387 void *hdr;
4388 struct nlattr *pinfoattr;
4390 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
4391 if (!hdr)
4392 return -1;
4394 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4395 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4396 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4397 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4398 goto nla_put_failure;
4400 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4401 if (!pinfoattr)
4402 goto nla_put_failure;
4403 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4404 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4405 pinfo->frame_qlen))
4406 goto nla_put_failure;
4407 if (((pinfo->filled & MPATH_INFO_SN) &&
4408 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4409 ((pinfo->filled & MPATH_INFO_METRIC) &&
4410 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4411 pinfo->metric)) ||
4412 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4413 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4414 pinfo->exptime)) ||
4415 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4416 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4417 pinfo->flags)) ||
4418 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4419 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4420 pinfo->discovery_timeout)) ||
4421 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4422 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4423 pinfo->discovery_retries)))
4424 goto nla_put_failure;
4426 nla_nest_end(msg, pinfoattr);
4428 return genlmsg_end(msg, hdr);
4430 nla_put_failure:
4431 genlmsg_cancel(msg, hdr);
4432 return -EMSGSIZE;
4435 static int nl80211_dump_mpath(struct sk_buff *skb,
4436 struct netlink_callback *cb)
4438 struct mpath_info pinfo;
4439 struct cfg80211_registered_device *rdev;
4440 struct wireless_dev *wdev;
4441 u8 dst[ETH_ALEN];
4442 u8 next_hop[ETH_ALEN];
4443 int path_idx = cb->args[2];
4444 int err;
4446 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4447 if (err)
4448 return err;
4450 if (!rdev->ops->dump_mpath) {
4451 err = -EOPNOTSUPP;
4452 goto out_err;
4455 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4456 err = -EOPNOTSUPP;
4457 goto out_err;
4460 while (1) {
4461 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
4462 next_hop, &pinfo);
4463 if (err == -ENOENT)
4464 break;
4465 if (err)
4466 goto out_err;
4468 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4469 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4470 wdev->netdev, dst, next_hop,
4471 &pinfo) < 0)
4472 goto out;
4474 path_idx++;
4478 out:
4479 cb->args[2] = path_idx;
4480 err = skb->len;
4481 out_err:
4482 nl80211_finish_wdev_dump(rdev);
4483 return err;
4486 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4489 int err;
4490 struct net_device *dev = info->user_ptr[1];
4491 struct mpath_info pinfo;
4492 struct sk_buff *msg;
4493 u8 *dst = NULL;
4494 u8 next_hop[ETH_ALEN];
4496 memset(&pinfo, 0, sizeof(pinfo));
4498 if (!info->attrs[NL80211_ATTR_MAC])
4499 return -EINVAL;
4501 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4503 if (!rdev->ops->get_mpath)
4504 return -EOPNOTSUPP;
4506 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4507 return -EOPNOTSUPP;
4509 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
4510 if (err)
4511 return err;
4513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4514 if (!msg)
4515 return -ENOMEM;
4517 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4518 dev, dst, next_hop, &pinfo) < 0) {
4519 nlmsg_free(msg);
4520 return -ENOBUFS;
4523 return genlmsg_reply(msg, info);
4526 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4528 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4529 struct net_device *dev = info->user_ptr[1];
4530 u8 *dst = NULL;
4531 u8 *next_hop = NULL;
4533 if (!info->attrs[NL80211_ATTR_MAC])
4534 return -EINVAL;
4536 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4537 return -EINVAL;
4539 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4540 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4542 if (!rdev->ops->change_mpath)
4543 return -EOPNOTSUPP;
4545 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4546 return -EOPNOTSUPP;
4548 return rdev_change_mpath(rdev, dev, dst, next_hop);
4551 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4554 struct net_device *dev = info->user_ptr[1];
4555 u8 *dst = NULL;
4556 u8 *next_hop = NULL;
4558 if (!info->attrs[NL80211_ATTR_MAC])
4559 return -EINVAL;
4561 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4562 return -EINVAL;
4564 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4565 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4567 if (!rdev->ops->add_mpath)
4568 return -EOPNOTSUPP;
4570 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4571 return -EOPNOTSUPP;
4573 return rdev_add_mpath(rdev, dev, dst, next_hop);
4576 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4579 struct net_device *dev = info->user_ptr[1];
4580 u8 *dst = NULL;
4582 if (info->attrs[NL80211_ATTR_MAC])
4583 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4585 if (!rdev->ops->del_mpath)
4586 return -EOPNOTSUPP;
4588 return rdev_del_mpath(rdev, dev, dst);
4591 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4594 struct net_device *dev = info->user_ptr[1];
4595 struct wireless_dev *wdev = dev->ieee80211_ptr;
4596 struct bss_parameters params;
4597 int err;
4599 memset(&params, 0, sizeof(params));
4600 /* default to not changing parameters */
4601 params.use_cts_prot = -1;
4602 params.use_short_preamble = -1;
4603 params.use_short_slot_time = -1;
4604 params.ap_isolate = -1;
4605 params.ht_opmode = -1;
4606 params.p2p_ctwindow = -1;
4607 params.p2p_opp_ps = -1;
4609 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4610 params.use_cts_prot =
4611 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4612 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4613 params.use_short_preamble =
4614 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4615 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4616 params.use_short_slot_time =
4617 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
4618 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4619 params.basic_rates =
4620 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4621 params.basic_rates_len =
4622 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4624 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4625 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
4626 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4627 params.ht_opmode =
4628 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
4630 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4631 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4632 return -EINVAL;
4633 params.p2p_ctwindow =
4634 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4635 if (params.p2p_ctwindow < 0)
4636 return -EINVAL;
4637 if (params.p2p_ctwindow != 0 &&
4638 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4639 return -EINVAL;
4642 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4643 u8 tmp;
4645 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4646 return -EINVAL;
4647 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4648 if (tmp > 1)
4649 return -EINVAL;
4650 params.p2p_opp_ps = tmp;
4651 if (params.p2p_opp_ps &&
4652 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4653 return -EINVAL;
4656 if (!rdev->ops->change_bss)
4657 return -EOPNOTSUPP;
4659 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4660 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4661 return -EOPNOTSUPP;
4663 wdev_lock(wdev);
4664 err = rdev_change_bss(rdev, dev, &params);
4665 wdev_unlock(wdev);
4667 return err;
4670 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
4671 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4672 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4673 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4674 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4675 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4676 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4677 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
4680 static int parse_reg_rule(struct nlattr *tb[],
4681 struct ieee80211_reg_rule *reg_rule)
4683 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4684 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4686 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4687 return -EINVAL;
4688 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4689 return -EINVAL;
4690 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4691 return -EINVAL;
4692 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4693 return -EINVAL;
4694 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4695 return -EINVAL;
4697 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4699 freq_range->start_freq_khz =
4700 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4701 freq_range->end_freq_khz =
4702 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4703 freq_range->max_bandwidth_khz =
4704 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4706 power_rule->max_eirp =
4707 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4709 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4710 power_rule->max_antenna_gain =
4711 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4713 if (tb[NL80211_ATTR_DFS_CAC_TIME])
4714 reg_rule->dfs_cac_ms =
4715 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
4717 return 0;
4720 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4722 char *data = NULL;
4723 enum nl80211_user_reg_hint_type user_reg_hint_type;
4726 * You should only get this when cfg80211 hasn't yet initialized
4727 * completely when built-in to the kernel right between the time
4728 * window between nl80211_init() and regulatory_init(), if that is
4729 * even possible.
4731 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
4732 return -EINPROGRESS;
4734 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4735 user_reg_hint_type =
4736 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4737 else
4738 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4740 switch (user_reg_hint_type) {
4741 case NL80211_USER_REG_HINT_USER:
4742 case NL80211_USER_REG_HINT_CELL_BASE:
4743 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4744 return -EINVAL;
4746 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4747 return regulatory_hint_user(data, user_reg_hint_type);
4748 case NL80211_USER_REG_HINT_INDOOR:
4749 return regulatory_hint_indoor_user();
4750 default:
4751 return -EINVAL;
4755 static int nl80211_get_mesh_config(struct sk_buff *skb,
4756 struct genl_info *info)
4758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4759 struct net_device *dev = info->user_ptr[1];
4760 struct wireless_dev *wdev = dev->ieee80211_ptr;
4761 struct mesh_config cur_params;
4762 int err = 0;
4763 void *hdr;
4764 struct nlattr *pinfoattr;
4765 struct sk_buff *msg;
4767 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4768 return -EOPNOTSUPP;
4770 if (!rdev->ops->get_mesh_config)
4771 return -EOPNOTSUPP;
4773 wdev_lock(wdev);
4774 /* If not connected, get default parameters */
4775 if (!wdev->mesh_id_len)
4776 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4777 else
4778 err = rdev_get_mesh_config(rdev, dev, &cur_params);
4779 wdev_unlock(wdev);
4781 if (err)
4782 return err;
4784 /* Draw up a netlink message to send back */
4785 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4786 if (!msg)
4787 return -ENOMEM;
4788 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4789 NL80211_CMD_GET_MESH_CONFIG);
4790 if (!hdr)
4791 goto out;
4792 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
4793 if (!pinfoattr)
4794 goto nla_put_failure;
4795 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4796 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4797 cur_params.dot11MeshRetryTimeout) ||
4798 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4799 cur_params.dot11MeshConfirmTimeout) ||
4800 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4801 cur_params.dot11MeshHoldingTimeout) ||
4802 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4803 cur_params.dot11MeshMaxPeerLinks) ||
4804 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4805 cur_params.dot11MeshMaxRetries) ||
4806 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4807 cur_params.dot11MeshTTL) ||
4808 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4809 cur_params.element_ttl) ||
4810 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4811 cur_params.auto_open_plinks) ||
4812 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4813 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
4814 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4815 cur_params.dot11MeshHWMPmaxPREQretries) ||
4816 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4817 cur_params.path_refresh_time) ||
4818 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4819 cur_params.min_discovery_timeout) ||
4820 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4821 cur_params.dot11MeshHWMPactivePathTimeout) ||
4822 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4823 cur_params.dot11MeshHWMPpreqMinInterval) ||
4824 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4825 cur_params.dot11MeshHWMPperrMinInterval) ||
4826 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4827 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4828 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4829 cur_params.dot11MeshHWMPRootMode) ||
4830 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4831 cur_params.dot11MeshHWMPRannInterval) ||
4832 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4833 cur_params.dot11MeshGateAnnouncementProtocol) ||
4834 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4835 cur_params.dot11MeshForwarding) ||
4836 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
4837 cur_params.rssi_threshold) ||
4838 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
4839 cur_params.ht_opmode) ||
4840 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4841 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4842 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4843 cur_params.dot11MeshHWMProotInterval) ||
4844 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4845 cur_params.dot11MeshHWMPconfirmationInterval) ||
4846 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4847 cur_params.power_mode) ||
4848 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4849 cur_params.dot11MeshAwakeWindowDuration) ||
4850 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4851 cur_params.plink_timeout))
4852 goto nla_put_failure;
4853 nla_nest_end(msg, pinfoattr);
4854 genlmsg_end(msg, hdr);
4855 return genlmsg_reply(msg, info);
4857 nla_put_failure:
4858 genlmsg_cancel(msg, hdr);
4859 out:
4860 nlmsg_free(msg);
4861 return -ENOBUFS;
4864 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
4865 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4866 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4867 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4868 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4869 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4870 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
4871 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
4872 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
4873 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
4874 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4875 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4876 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4877 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4878 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
4879 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
4880 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
4881 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
4882 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
4883 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
4884 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
4885 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4886 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
4887 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4888 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
4889 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
4890 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4891 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
4892 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
4895 static const struct nla_policy
4896 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
4897 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
4898 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4899 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
4900 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
4901 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
4902 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
4903 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
4904 .len = IEEE80211_MAX_DATA_LEN },
4905 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
4908 static int nl80211_parse_mesh_config(struct genl_info *info,
4909 struct mesh_config *cfg,
4910 u32 *mask_out)
4912 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
4913 u32 mask = 0;
4915 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4916 do { \
4917 if (tb[attr]) { \
4918 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4919 return -EINVAL; \
4920 cfg->param = fn(tb[attr]); \
4921 mask |= (1 << (attr - 1)); \
4923 } while (0)
4926 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
4927 return -EINVAL;
4928 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
4929 info->attrs[NL80211_ATTR_MESH_CONFIG],
4930 nl80211_meshconf_params_policy))
4931 return -EINVAL;
4933 /* This makes sure that there aren't more than 32 mesh config
4934 * parameters (otherwise our bitfield scheme would not work.) */
4935 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4937 /* Fill in the params struct */
4938 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
4939 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4940 nla_get_u16);
4941 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
4942 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4943 nla_get_u16);
4944 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
4945 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4946 nla_get_u16);
4947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
4948 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4949 nla_get_u16);
4950 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
4951 mask, NL80211_MESHCONF_MAX_RETRIES,
4952 nla_get_u8);
4953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
4954 mask, NL80211_MESHCONF_TTL, nla_get_u8);
4955 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
4956 mask, NL80211_MESHCONF_ELEMENT_TTL,
4957 nla_get_u8);
4958 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
4959 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4960 nla_get_u8);
4961 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4962 1, 255, mask,
4963 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4964 nla_get_u32);
4965 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
4966 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4967 nla_get_u8);
4968 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
4969 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4970 nla_get_u32);
4971 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
4972 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4973 nla_get_u16);
4974 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4975 1, 65535, mask,
4976 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4977 nla_get_u32);
4978 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
4979 1, 65535, mask,
4980 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4981 nla_get_u16);
4982 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
4983 1, 65535, mask,
4984 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4985 nla_get_u16);
4986 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4987 dot11MeshHWMPnetDiameterTraversalTime,
4988 1, 65535, mask,
4989 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4990 nla_get_u16);
4991 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4992 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4993 nla_get_u8);
4994 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4995 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4996 nla_get_u16);
4997 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4998 dot11MeshGateAnnouncementProtocol, 0, 1,
4999 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5000 nla_get_u8);
5001 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
5002 mask, NL80211_MESHCONF_FORWARDING,
5003 nla_get_u8);
5004 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
5005 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
5006 nla_get_s32);
5007 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
5008 mask, NL80211_MESHCONF_HT_OPMODE,
5009 nla_get_u16);
5010 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
5011 1, 65535, mask,
5012 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5013 nla_get_u32);
5014 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
5015 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5016 nla_get_u16);
5017 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5018 dot11MeshHWMPconfirmationInterval,
5019 1, 65535, mask,
5020 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5021 nla_get_u16);
5022 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
5023 NL80211_MESH_POWER_ACTIVE,
5024 NL80211_MESH_POWER_MAX,
5025 mask, NL80211_MESHCONF_POWER_MODE,
5026 nla_get_u32);
5027 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5028 0, 65535, mask,
5029 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
5030 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
5031 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5032 nla_get_u32);
5033 if (mask_out)
5034 *mask_out = mask;
5036 return 0;
5038 #undef FILL_IN_MESH_PARAM_IF_SET
5041 static int nl80211_parse_mesh_setup(struct genl_info *info,
5042 struct mesh_setup *setup)
5044 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5045 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5047 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5048 return -EINVAL;
5049 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5050 info->attrs[NL80211_ATTR_MESH_SETUP],
5051 nl80211_mesh_setup_params_policy))
5052 return -EINVAL;
5054 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5055 setup->sync_method =
5056 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5057 IEEE80211_SYNC_METHOD_VENDOR :
5058 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5060 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5061 setup->path_sel_proto =
5062 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5063 IEEE80211_PATH_PROTOCOL_VENDOR :
5064 IEEE80211_PATH_PROTOCOL_HWMP;
5066 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5067 setup->path_metric =
5068 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5069 IEEE80211_PATH_METRIC_VENDOR :
5070 IEEE80211_PATH_METRIC_AIRTIME;
5073 if (tb[NL80211_MESH_SETUP_IE]) {
5074 struct nlattr *ieattr =
5075 tb[NL80211_MESH_SETUP_IE];
5076 if (!is_valid_ie_attr(ieattr))
5077 return -EINVAL;
5078 setup->ie = nla_data(ieattr);
5079 setup->ie_len = nla_len(ieattr);
5081 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5082 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5083 return -EINVAL;
5084 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
5085 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5086 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
5087 if (setup->is_secure)
5088 setup->user_mpm = true;
5090 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5091 if (!setup->user_mpm)
5092 return -EINVAL;
5093 setup->auth_id =
5094 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5097 return 0;
5100 static int nl80211_update_mesh_config(struct sk_buff *skb,
5101 struct genl_info *info)
5103 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5104 struct net_device *dev = info->user_ptr[1];
5105 struct wireless_dev *wdev = dev->ieee80211_ptr;
5106 struct mesh_config cfg;
5107 u32 mask;
5108 int err;
5110 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5111 return -EOPNOTSUPP;
5113 if (!rdev->ops->update_mesh_config)
5114 return -EOPNOTSUPP;
5116 err = nl80211_parse_mesh_config(info, &cfg, &mask);
5117 if (err)
5118 return err;
5120 wdev_lock(wdev);
5121 if (!wdev->mesh_id_len)
5122 err = -ENOLINK;
5124 if (!err)
5125 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
5127 wdev_unlock(wdev);
5129 return err;
5132 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5134 const struct ieee80211_regdomain *regdom;
5135 struct sk_buff *msg;
5136 void *hdr = NULL;
5137 struct nlattr *nl_reg_rules;
5138 unsigned int i;
5140 if (!cfg80211_regdomain)
5141 return -EINVAL;
5143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5144 if (!msg)
5145 return -ENOBUFS;
5147 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5148 NL80211_CMD_GET_REG);
5149 if (!hdr)
5150 goto put_failure;
5152 if (reg_last_request_cell_base() &&
5153 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5154 NL80211_USER_REG_HINT_CELL_BASE))
5155 goto nla_put_failure;
5157 rcu_read_lock();
5158 regdom = rcu_dereference(cfg80211_regdomain);
5160 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5161 (regdom->dfs_region &&
5162 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5163 goto nla_put_failure_rcu;
5165 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5166 if (!nl_reg_rules)
5167 goto nla_put_failure_rcu;
5169 for (i = 0; i < regdom->n_reg_rules; i++) {
5170 struct nlattr *nl_reg_rule;
5171 const struct ieee80211_reg_rule *reg_rule;
5172 const struct ieee80211_freq_range *freq_range;
5173 const struct ieee80211_power_rule *power_rule;
5174 unsigned int max_bandwidth_khz;
5176 reg_rule = &regdom->reg_rules[i];
5177 freq_range = &reg_rule->freq_range;
5178 power_rule = &reg_rule->power_rule;
5180 nl_reg_rule = nla_nest_start(msg, i);
5181 if (!nl_reg_rule)
5182 goto nla_put_failure_rcu;
5184 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5185 if (!max_bandwidth_khz)
5186 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5187 reg_rule);
5189 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5190 reg_rule->flags) ||
5191 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5192 freq_range->start_freq_khz) ||
5193 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5194 freq_range->end_freq_khz) ||
5195 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5196 max_bandwidth_khz) ||
5197 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5198 power_rule->max_antenna_gain) ||
5199 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5200 power_rule->max_eirp) ||
5201 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5202 reg_rule->dfs_cac_ms))
5203 goto nla_put_failure_rcu;
5205 nla_nest_end(msg, nl_reg_rule);
5207 rcu_read_unlock();
5209 nla_nest_end(msg, nl_reg_rules);
5211 genlmsg_end(msg, hdr);
5212 return genlmsg_reply(msg, info);
5214 nla_put_failure_rcu:
5215 rcu_read_unlock();
5216 nla_put_failure:
5217 genlmsg_cancel(msg, hdr);
5218 put_failure:
5219 nlmsg_free(msg);
5220 return -EMSGSIZE;
5223 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5225 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5226 struct nlattr *nl_reg_rule;
5227 char *alpha2 = NULL;
5228 int rem_reg_rules = 0, r = 0;
5229 u32 num_rules = 0, rule_idx = 0, size_of_regd;
5230 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
5231 struct ieee80211_regdomain *rd = NULL;
5233 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5234 return -EINVAL;
5236 if (!info->attrs[NL80211_ATTR_REG_RULES])
5237 return -EINVAL;
5239 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5241 if (info->attrs[NL80211_ATTR_DFS_REGION])
5242 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5244 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5245 rem_reg_rules) {
5246 num_rules++;
5247 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
5248 return -EINVAL;
5251 if (!reg_is_valid_request(alpha2))
5252 return -EINVAL;
5254 size_of_regd = sizeof(struct ieee80211_regdomain) +
5255 num_rules * sizeof(struct ieee80211_reg_rule);
5257 rd = kzalloc(size_of_regd, GFP_KERNEL);
5258 if (!rd)
5259 return -ENOMEM;
5261 rd->n_reg_rules = num_rules;
5262 rd->alpha2[0] = alpha2[0];
5263 rd->alpha2[1] = alpha2[1];
5266 * Disable DFS master mode if the DFS region was
5267 * not supported or known on this kernel.
5269 if (reg_supported_dfs_region(dfs_region))
5270 rd->dfs_region = dfs_region;
5272 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5273 rem_reg_rules) {
5274 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5275 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5276 reg_rule_policy);
5277 if (r)
5278 goto bad_reg;
5279 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5280 if (r)
5281 goto bad_reg;
5283 rule_idx++;
5285 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5286 r = -EINVAL;
5287 goto bad_reg;
5291 r = set_regdom(rd);
5292 /* set_regdom took ownership */
5293 rd = NULL;
5295 bad_reg:
5296 kfree(rd);
5297 return r;
5300 static int validate_scan_freqs(struct nlattr *freqs)
5302 struct nlattr *attr1, *attr2;
5303 int n_channels = 0, tmp1, tmp2;
5305 nla_for_each_nested(attr1, freqs, tmp1) {
5306 n_channels++;
5308 * Some hardware has a limited channel list for
5309 * scanning, and it is pretty much nonsensical
5310 * to scan for a channel twice, so disallow that
5311 * and don't require drivers to check that the
5312 * channel list they get isn't longer than what
5313 * they can scan, as long as they can scan all
5314 * the channels they registered at once.
5316 nla_for_each_nested(attr2, freqs, tmp2)
5317 if (attr1 != attr2 &&
5318 nla_get_u32(attr1) == nla_get_u32(attr2))
5319 return 0;
5322 return n_channels;
5325 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5327 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5328 struct wireless_dev *wdev = info->user_ptr[1];
5329 struct cfg80211_scan_request *request;
5330 struct nlattr *attr;
5331 struct wiphy *wiphy;
5332 int err, tmp, n_ssids = 0, n_channels, i;
5333 size_t ie_len;
5335 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5336 return -EINVAL;
5338 wiphy = &rdev->wiphy;
5340 if (!rdev->ops->scan)
5341 return -EOPNOTSUPP;
5343 if (rdev->scan_req || rdev->scan_msg) {
5344 err = -EBUSY;
5345 goto unlock;
5348 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5349 n_channels = validate_scan_freqs(
5350 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5351 if (!n_channels) {
5352 err = -EINVAL;
5353 goto unlock;
5355 } else {
5356 n_channels = ieee80211_get_num_supported_channels(wiphy);
5359 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5360 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5361 n_ssids++;
5363 if (n_ssids > wiphy->max_scan_ssids) {
5364 err = -EINVAL;
5365 goto unlock;
5368 if (info->attrs[NL80211_ATTR_IE])
5369 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5370 else
5371 ie_len = 0;
5373 if (ie_len > wiphy->max_scan_ie_len) {
5374 err = -EINVAL;
5375 goto unlock;
5378 request = kzalloc(sizeof(*request)
5379 + sizeof(*request->ssids) * n_ssids
5380 + sizeof(*request->channels) * n_channels
5381 + ie_len, GFP_KERNEL);
5382 if (!request) {
5383 err = -ENOMEM;
5384 goto unlock;
5387 if (n_ssids)
5388 request->ssids = (void *)&request->channels[n_channels];
5389 request->n_ssids = n_ssids;
5390 if (ie_len) {
5391 if (request->ssids)
5392 request->ie = (void *)(request->ssids + n_ssids);
5393 else
5394 request->ie = (void *)(request->channels + n_channels);
5397 i = 0;
5398 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5399 /* user specified, bail out if channel not found */
5400 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
5401 struct ieee80211_channel *chan;
5403 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5405 if (!chan) {
5406 err = -EINVAL;
5407 goto out_free;
5410 /* ignore disabled channels */
5411 if (chan->flags & IEEE80211_CHAN_DISABLED)
5412 continue;
5414 request->channels[i] = chan;
5415 i++;
5417 } else {
5418 enum ieee80211_band band;
5420 /* all channels */
5421 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5422 int j;
5423 if (!wiphy->bands[band])
5424 continue;
5425 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5426 struct ieee80211_channel *chan;
5428 chan = &wiphy->bands[band]->channels[j];
5430 if (chan->flags & IEEE80211_CHAN_DISABLED)
5431 continue;
5433 request->channels[i] = chan;
5434 i++;
5439 if (!i) {
5440 err = -EINVAL;
5441 goto out_free;
5444 request->n_channels = i;
5446 i = 0;
5447 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5448 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
5449 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5450 err = -EINVAL;
5451 goto out_free;
5453 request->ssids[i].ssid_len = nla_len(attr);
5454 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
5455 i++;
5459 if (info->attrs[NL80211_ATTR_IE]) {
5460 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5461 memcpy((void *)request->ie,
5462 nla_data(info->attrs[NL80211_ATTR_IE]),
5463 request->ie_len);
5466 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
5467 if (wiphy->bands[i])
5468 request->rates[i] =
5469 (1 << wiphy->bands[i]->n_bitrates) - 1;
5471 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5472 nla_for_each_nested(attr,
5473 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5474 tmp) {
5475 enum ieee80211_band band = nla_type(attr);
5477 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
5478 err = -EINVAL;
5479 goto out_free;
5482 if (!wiphy->bands[band])
5483 continue;
5485 err = ieee80211_get_ratemask(wiphy->bands[band],
5486 nla_data(attr),
5487 nla_len(attr),
5488 &request->rates[band]);
5489 if (err)
5490 goto out_free;
5494 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5495 request->flags = nla_get_u32(
5496 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5497 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5498 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
5499 err = -EOPNOTSUPP;
5500 goto out_free;
5504 request->no_cck =
5505 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5507 request->wdev = wdev;
5508 request->wiphy = &rdev->wiphy;
5509 request->scan_start = jiffies;
5511 rdev->scan_req = request;
5512 err = rdev_scan(rdev, request);
5514 if (!err) {
5515 nl80211_send_scan_start(rdev, wdev);
5516 if (wdev->netdev)
5517 dev_hold(wdev->netdev);
5518 } else {
5519 out_free:
5520 rdev->scan_req = NULL;
5521 kfree(request);
5524 unlock:
5525 return err;
5528 static int nl80211_start_sched_scan(struct sk_buff *skb,
5529 struct genl_info *info)
5531 struct cfg80211_sched_scan_request *request;
5532 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5533 struct net_device *dev = info->user_ptr[1];
5534 struct nlattr *attr;
5535 struct wiphy *wiphy;
5536 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
5537 u32 interval;
5538 enum ieee80211_band band;
5539 size_t ie_len;
5540 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
5541 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
5543 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5544 !rdev->ops->sched_scan_start)
5545 return -EOPNOTSUPP;
5547 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5548 return -EINVAL;
5550 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5551 return -EINVAL;
5553 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5554 if (interval == 0)
5555 return -EINVAL;
5557 wiphy = &rdev->wiphy;
5559 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5560 n_channels = validate_scan_freqs(
5561 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5562 if (!n_channels)
5563 return -EINVAL;
5564 } else {
5565 n_channels = ieee80211_get_num_supported_channels(wiphy);
5568 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5569 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5570 tmp)
5571 n_ssids++;
5573 if (n_ssids > wiphy->max_sched_scan_ssids)
5574 return -EINVAL;
5577 * First, count the number of 'real' matchsets. Due to an issue with
5578 * the old implementation, matchsets containing only the RSSI attribute
5579 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
5580 * RSSI for all matchsets, rather than their own matchset for reporting
5581 * all APs with a strong RSSI. This is needed to be compatible with
5582 * older userspace that treated a matchset with only the RSSI as the
5583 * global RSSI for all other matchsets - if there are other matchsets.
5585 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5586 nla_for_each_nested(attr,
5587 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5588 tmp) {
5589 struct nlattr *rssi;
5591 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5592 nla_data(attr), nla_len(attr),
5593 nl80211_match_policy);
5594 if (err)
5595 return err;
5596 /* add other standalone attributes here */
5597 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
5598 n_match_sets++;
5599 continue;
5601 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5602 if (rssi)
5603 default_match_rssi = nla_get_s32(rssi);
5607 /* However, if there's no other matchset, add the RSSI one */
5608 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
5609 n_match_sets = 1;
5611 if (n_match_sets > wiphy->max_match_sets)
5612 return -EINVAL;
5614 if (info->attrs[NL80211_ATTR_IE])
5615 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5616 else
5617 ie_len = 0;
5619 if (ie_len > wiphy->max_sched_scan_ie_len)
5620 return -EINVAL;
5622 if (rdev->sched_scan_req) {
5623 err = -EINPROGRESS;
5624 goto out;
5627 request = kzalloc(sizeof(*request)
5628 + sizeof(*request->ssids) * n_ssids
5629 + sizeof(*request->match_sets) * n_match_sets
5630 + sizeof(*request->channels) * n_channels
5631 + ie_len, GFP_KERNEL);
5632 if (!request) {
5633 err = -ENOMEM;
5634 goto out;
5637 if (n_ssids)
5638 request->ssids = (void *)&request->channels[n_channels];
5639 request->n_ssids = n_ssids;
5640 if (ie_len) {
5641 if (request->ssids)
5642 request->ie = (void *)(request->ssids + n_ssids);
5643 else
5644 request->ie = (void *)(request->channels + n_channels);
5647 if (n_match_sets) {
5648 if (request->ie)
5649 request->match_sets = (void *)(request->ie + ie_len);
5650 else if (request->ssids)
5651 request->match_sets =
5652 (void *)(request->ssids + n_ssids);
5653 else
5654 request->match_sets =
5655 (void *)(request->channels + n_channels);
5657 request->n_match_sets = n_match_sets;
5659 i = 0;
5660 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5661 /* user specified, bail out if channel not found */
5662 nla_for_each_nested(attr,
5663 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5664 tmp) {
5665 struct ieee80211_channel *chan;
5667 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5669 if (!chan) {
5670 err = -EINVAL;
5671 goto out_free;
5674 /* ignore disabled channels */
5675 if (chan->flags & IEEE80211_CHAN_DISABLED)
5676 continue;
5678 request->channels[i] = chan;
5679 i++;
5681 } else {
5682 /* all channels */
5683 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5684 int j;
5685 if (!wiphy->bands[band])
5686 continue;
5687 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5688 struct ieee80211_channel *chan;
5690 chan = &wiphy->bands[band]->channels[j];
5692 if (chan->flags & IEEE80211_CHAN_DISABLED)
5693 continue;
5695 request->channels[i] = chan;
5696 i++;
5701 if (!i) {
5702 err = -EINVAL;
5703 goto out_free;
5706 request->n_channels = i;
5708 i = 0;
5709 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5710 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5711 tmp) {
5712 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5713 err = -EINVAL;
5714 goto out_free;
5716 request->ssids[i].ssid_len = nla_len(attr);
5717 memcpy(request->ssids[i].ssid, nla_data(attr),
5718 nla_len(attr));
5719 i++;
5723 i = 0;
5724 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5725 nla_for_each_nested(attr,
5726 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5727 tmp) {
5728 struct nlattr *ssid, *rssi;
5730 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5731 nla_data(attr), nla_len(attr),
5732 nl80211_match_policy);
5733 if (err)
5734 goto out_free;
5735 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
5736 if (ssid) {
5737 if (WARN_ON(i >= n_match_sets)) {
5738 /* this indicates a programming error,
5739 * the loop above should have verified
5740 * things properly
5742 err = -EINVAL;
5743 goto out_free;
5746 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5747 err = -EINVAL;
5748 goto out_free;
5750 memcpy(request->match_sets[i].ssid.ssid,
5751 nla_data(ssid), nla_len(ssid));
5752 request->match_sets[i].ssid.ssid_len =
5753 nla_len(ssid);
5754 /* special attribute - old implemenation w/a */
5755 request->match_sets[i].rssi_thold =
5756 default_match_rssi;
5757 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5758 if (rssi)
5759 request->match_sets[i].rssi_thold =
5760 nla_get_s32(rssi);
5762 i++;
5765 /* there was no other matchset, so the RSSI one is alone */
5766 if (i == 0 && n_match_sets)
5767 request->match_sets[0].rssi_thold = default_match_rssi;
5769 request->min_rssi_thold = INT_MAX;
5770 for (i = 0; i < n_match_sets; i++)
5771 request->min_rssi_thold =
5772 min(request->match_sets[i].rssi_thold,
5773 request->min_rssi_thold);
5774 } else {
5775 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
5778 if (ie_len) {
5779 request->ie_len = ie_len;
5780 memcpy((void *)request->ie,
5781 nla_data(info->attrs[NL80211_ATTR_IE]),
5782 request->ie_len);
5785 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5786 request->flags = nla_get_u32(
5787 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5788 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5789 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
5790 err = -EOPNOTSUPP;
5791 goto out_free;
5795 request->dev = dev;
5796 request->wiphy = &rdev->wiphy;
5797 request->interval = interval;
5798 request->scan_start = jiffies;
5800 err = rdev_sched_scan_start(rdev, dev, request);
5801 if (!err) {
5802 rdev->sched_scan_req = request;
5803 nl80211_send_sched_scan(rdev, dev,
5804 NL80211_CMD_START_SCHED_SCAN);
5805 goto out;
5808 out_free:
5809 kfree(request);
5810 out:
5811 return err;
5814 static int nl80211_stop_sched_scan(struct sk_buff *skb,
5815 struct genl_info *info)
5817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5819 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5820 !rdev->ops->sched_scan_stop)
5821 return -EOPNOTSUPP;
5823 return __cfg80211_stop_sched_scan(rdev, false);
5826 static int nl80211_start_radar_detection(struct sk_buff *skb,
5827 struct genl_info *info)
5829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5830 struct net_device *dev = info->user_ptr[1];
5831 struct wireless_dev *wdev = dev->ieee80211_ptr;
5832 struct cfg80211_chan_def chandef;
5833 enum nl80211_dfs_regions dfs_region;
5834 unsigned int cac_time_ms;
5835 int err;
5837 dfs_region = reg_get_dfs_region(wdev->wiphy);
5838 if (dfs_region == NL80211_DFS_UNSET)
5839 return -EINVAL;
5841 err = nl80211_parse_chandef(rdev, info, &chandef);
5842 if (err)
5843 return err;
5845 if (netif_carrier_ok(dev))
5846 return -EBUSY;
5848 if (wdev->cac_started)
5849 return -EBUSY;
5851 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
5852 wdev->iftype);
5853 if (err < 0)
5854 return err;
5856 if (err == 0)
5857 return -EINVAL;
5859 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
5860 return -EINVAL;
5862 if (!rdev->ops->start_radar_detection)
5863 return -EOPNOTSUPP;
5865 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
5866 if (WARN_ON(!cac_time_ms))
5867 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
5869 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
5870 cac_time_ms);
5871 if (!err) {
5872 wdev->chandef = chandef;
5873 wdev->cac_started = true;
5874 wdev->cac_start_time = jiffies;
5875 wdev->cac_time_ms = cac_time_ms;
5877 return err;
5880 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5882 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5883 struct net_device *dev = info->user_ptr[1];
5884 struct wireless_dev *wdev = dev->ieee80211_ptr;
5885 struct cfg80211_csa_settings params;
5886 /* csa_attrs is defined static to avoid waste of stack size - this
5887 * function is called under RTNL lock, so this should not be a problem.
5889 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5890 u8 radar_detect_width = 0;
5891 int err;
5892 bool need_new_beacon = false;
5893 int len, i;
5895 if (!rdev->ops->channel_switch ||
5896 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5897 return -EOPNOTSUPP;
5899 switch (dev->ieee80211_ptr->iftype) {
5900 case NL80211_IFTYPE_AP:
5901 case NL80211_IFTYPE_P2P_GO:
5902 need_new_beacon = true;
5904 /* useless if AP is not running */
5905 if (!wdev->beacon_interval)
5906 return -ENOTCONN;
5907 break;
5908 case NL80211_IFTYPE_ADHOC:
5909 if (!wdev->ssid_len)
5910 return -ENOTCONN;
5911 break;
5912 case NL80211_IFTYPE_MESH_POINT:
5913 if (!wdev->mesh_id_len)
5914 return -ENOTCONN;
5915 break;
5916 default:
5917 return -EOPNOTSUPP;
5920 memset(&params, 0, sizeof(params));
5922 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5923 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5924 return -EINVAL;
5926 /* only important for AP, IBSS and mesh create IEs internally */
5927 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
5928 return -EINVAL;
5930 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5932 if (!need_new_beacon)
5933 goto skip_beacons;
5935 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5936 if (err)
5937 return err;
5939 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5940 info->attrs[NL80211_ATTR_CSA_IES],
5941 nl80211_policy);
5942 if (err)
5943 return err;
5945 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5946 if (err)
5947 return err;
5949 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5950 return -EINVAL;
5952 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5953 if (!len || (len % sizeof(u16)))
5954 return -EINVAL;
5956 params.n_counter_offsets_beacon = len / sizeof(u16);
5957 if (rdev->wiphy.max_num_csa_counters &&
5958 (params.n_counter_offsets_beacon >
5959 rdev->wiphy.max_num_csa_counters))
5960 return -EINVAL;
5962 params.counter_offsets_beacon =
5963 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5965 /* sanity checks - counters should fit and be the same */
5966 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
5967 u16 offset = params.counter_offsets_beacon[i];
5969 if (offset >= params.beacon_csa.tail_len)
5970 return -EINVAL;
5972 if (params.beacon_csa.tail[offset] != params.count)
5973 return -EINVAL;
5976 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5977 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5978 if (!len || (len % sizeof(u16)))
5979 return -EINVAL;
5981 params.n_counter_offsets_presp = len / sizeof(u16);
5982 if (rdev->wiphy.max_num_csa_counters &&
5983 (params.n_counter_offsets_beacon >
5984 rdev->wiphy.max_num_csa_counters))
5985 return -EINVAL;
5987 params.counter_offsets_presp =
5988 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5990 /* sanity checks - counters should fit and be the same */
5991 for (i = 0; i < params.n_counter_offsets_presp; i++) {
5992 u16 offset = params.counter_offsets_presp[i];
5994 if (offset >= params.beacon_csa.probe_resp_len)
5995 return -EINVAL;
5997 if (params.beacon_csa.probe_resp[offset] !=
5998 params.count)
5999 return -EINVAL;
6003 skip_beacons:
6004 err = nl80211_parse_chandef(rdev, info, &params.chandef);
6005 if (err)
6006 return err;
6008 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef,
6009 wdev->iftype))
6010 return -EINVAL;
6012 err = cfg80211_chandef_dfs_required(wdev->wiphy,
6013 &params.chandef,
6014 wdev->iftype);
6015 if (err < 0)
6016 return err;
6018 if (err > 0) {
6019 radar_detect_width = BIT(params.chandef.width);
6020 params.radar_required = true;
6023 /* TODO: I left this here for now. With channel switch, the
6024 * verification is a bit more complicated, because we only do
6025 * it later when the channel switch really happens.
6027 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
6028 params.chandef.chan,
6029 CHAN_MODE_SHARED,
6030 radar_detect_width);
6031 if (err)
6032 return err;
6034 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
6035 params.block_tx = true;
6037 wdev_lock(wdev);
6038 err = rdev_channel_switch(rdev, dev, &params);
6039 wdev_unlock(wdev);
6041 return err;
6044 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
6045 u32 seq, int flags,
6046 struct cfg80211_registered_device *rdev,
6047 struct wireless_dev *wdev,
6048 struct cfg80211_internal_bss *intbss)
6050 struct cfg80211_bss *res = &intbss->pub;
6051 const struct cfg80211_bss_ies *ies;
6052 void *hdr;
6053 struct nlattr *bss;
6054 bool tsf = false;
6056 ASSERT_WDEV_LOCK(wdev);
6058 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6059 NL80211_CMD_NEW_SCAN_RESULTS);
6060 if (!hdr)
6061 return -1;
6063 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6065 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6066 goto nla_put_failure;
6067 if (wdev->netdev &&
6068 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6069 goto nla_put_failure;
6070 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6071 goto nla_put_failure;
6073 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6074 if (!bss)
6075 goto nla_put_failure;
6076 if ((!is_zero_ether_addr(res->bssid) &&
6077 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
6078 goto nla_put_failure;
6080 rcu_read_lock();
6081 ies = rcu_dereference(res->ies);
6082 if (ies) {
6083 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6084 goto fail_unlock_rcu;
6085 tsf = true;
6086 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6087 ies->len, ies->data))
6088 goto fail_unlock_rcu;
6090 ies = rcu_dereference(res->beacon_ies);
6091 if (ies) {
6092 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6093 goto fail_unlock_rcu;
6094 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6095 ies->len, ies->data))
6096 goto fail_unlock_rcu;
6098 rcu_read_unlock();
6100 if (res->beacon_interval &&
6101 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6102 goto nla_put_failure;
6103 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6104 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
6105 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
6106 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6107 jiffies_to_msecs(jiffies - intbss->ts)))
6108 goto nla_put_failure;
6110 switch (rdev->wiphy.signal_type) {
6111 case CFG80211_SIGNAL_TYPE_MBM:
6112 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6113 goto nla_put_failure;
6114 break;
6115 case CFG80211_SIGNAL_TYPE_UNSPEC:
6116 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6117 goto nla_put_failure;
6118 break;
6119 default:
6120 break;
6123 switch (wdev->iftype) {
6124 case NL80211_IFTYPE_P2P_CLIENT:
6125 case NL80211_IFTYPE_STATION:
6126 if (intbss == wdev->current_bss &&
6127 nla_put_u32(msg, NL80211_BSS_STATUS,
6128 NL80211_BSS_STATUS_ASSOCIATED))
6129 goto nla_put_failure;
6130 break;
6131 case NL80211_IFTYPE_ADHOC:
6132 if (intbss == wdev->current_bss &&
6133 nla_put_u32(msg, NL80211_BSS_STATUS,
6134 NL80211_BSS_STATUS_IBSS_JOINED))
6135 goto nla_put_failure;
6136 break;
6137 default:
6138 break;
6141 nla_nest_end(msg, bss);
6143 return genlmsg_end(msg, hdr);
6145 fail_unlock_rcu:
6146 rcu_read_unlock();
6147 nla_put_failure:
6148 genlmsg_cancel(msg, hdr);
6149 return -EMSGSIZE;
6152 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
6154 struct cfg80211_registered_device *rdev;
6155 struct cfg80211_internal_bss *scan;
6156 struct wireless_dev *wdev;
6157 int start = cb->args[2], idx = 0;
6158 int err;
6160 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6161 if (err)
6162 return err;
6164 wdev_lock(wdev);
6165 spin_lock_bh(&rdev->bss_lock);
6166 cfg80211_bss_expire(rdev);
6168 cb->seq = rdev->bss_generation;
6170 list_for_each_entry(scan, &rdev->bss_list, list) {
6171 if (++idx <= start)
6172 continue;
6173 if (nl80211_send_bss(skb, cb,
6174 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6175 rdev, wdev, scan) < 0) {
6176 idx--;
6177 break;
6181 spin_unlock_bh(&rdev->bss_lock);
6182 wdev_unlock(wdev);
6184 cb->args[2] = idx;
6185 nl80211_finish_wdev_dump(rdev);
6187 return skb->len;
6190 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
6191 int flags, struct net_device *dev,
6192 struct survey_info *survey)
6194 void *hdr;
6195 struct nlattr *infoattr;
6197 hdr = nl80211hdr_put(msg, portid, seq, flags,
6198 NL80211_CMD_NEW_SURVEY_RESULTS);
6199 if (!hdr)
6200 return -ENOMEM;
6202 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6203 goto nla_put_failure;
6205 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6206 if (!infoattr)
6207 goto nla_put_failure;
6209 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6210 survey->channel->center_freq))
6211 goto nla_put_failure;
6213 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6214 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6215 goto nla_put_failure;
6216 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6217 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6218 goto nla_put_failure;
6219 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6220 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6221 survey->channel_time))
6222 goto nla_put_failure;
6223 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6224 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6225 survey->channel_time_busy))
6226 goto nla_put_failure;
6227 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6228 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6229 survey->channel_time_ext_busy))
6230 goto nla_put_failure;
6231 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6232 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6233 survey->channel_time_rx))
6234 goto nla_put_failure;
6235 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6236 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6237 survey->channel_time_tx))
6238 goto nla_put_failure;
6240 nla_nest_end(msg, infoattr);
6242 return genlmsg_end(msg, hdr);
6244 nla_put_failure:
6245 genlmsg_cancel(msg, hdr);
6246 return -EMSGSIZE;
6249 static int nl80211_dump_survey(struct sk_buff *skb,
6250 struct netlink_callback *cb)
6252 struct survey_info survey;
6253 struct cfg80211_registered_device *rdev;
6254 struct wireless_dev *wdev;
6255 int survey_idx = cb->args[2];
6256 int res;
6258 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6259 if (res)
6260 return res;
6262 if (!wdev->netdev) {
6263 res = -EINVAL;
6264 goto out_err;
6267 if (!rdev->ops->dump_survey) {
6268 res = -EOPNOTSUPP;
6269 goto out_err;
6272 while (1) {
6273 struct ieee80211_channel *chan;
6275 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
6276 if (res == -ENOENT)
6277 break;
6278 if (res)
6279 goto out_err;
6281 /* Survey without a channel doesn't make sense */
6282 if (!survey.channel) {
6283 res = -EINVAL;
6284 goto out;
6287 chan = ieee80211_get_channel(&rdev->wiphy,
6288 survey.channel->center_freq);
6289 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6290 survey_idx++;
6291 continue;
6294 if (nl80211_send_survey(skb,
6295 NETLINK_CB(cb->skb).portid,
6296 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6297 wdev->netdev, &survey) < 0)
6298 goto out;
6299 survey_idx++;
6302 out:
6303 cb->args[2] = survey_idx;
6304 res = skb->len;
6305 out_err:
6306 nl80211_finish_wdev_dump(rdev);
6307 return res;
6310 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6312 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6313 NL80211_WPA_VERSION_2));
6316 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6318 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6319 struct net_device *dev = info->user_ptr[1];
6320 struct ieee80211_channel *chan;
6321 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6322 int err, ssid_len, ie_len = 0, sae_data_len = 0;
6323 enum nl80211_auth_type auth_type;
6324 struct key_parse key;
6325 bool local_state_change;
6327 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6328 return -EINVAL;
6330 if (!info->attrs[NL80211_ATTR_MAC])
6331 return -EINVAL;
6333 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6334 return -EINVAL;
6336 if (!info->attrs[NL80211_ATTR_SSID])
6337 return -EINVAL;
6339 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6340 return -EINVAL;
6342 err = nl80211_parse_key(info, &key);
6343 if (err)
6344 return err;
6346 if (key.idx >= 0) {
6347 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6348 return -EINVAL;
6349 if (!key.p.key || !key.p.key_len)
6350 return -EINVAL;
6351 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6352 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6353 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6354 key.p.key_len != WLAN_KEY_LEN_WEP104))
6355 return -EINVAL;
6356 if (key.idx > 4)
6357 return -EINVAL;
6358 } else {
6359 key.p.key_len = 0;
6360 key.p.key = NULL;
6363 if (key.idx >= 0) {
6364 int i;
6365 bool ok = false;
6366 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6367 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6368 ok = true;
6369 break;
6372 if (!ok)
6373 return -EINVAL;
6376 if (!rdev->ops->auth)
6377 return -EOPNOTSUPP;
6379 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6380 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6381 return -EOPNOTSUPP;
6383 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6384 chan = nl80211_get_valid_chan(&rdev->wiphy,
6385 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6386 if (!chan)
6387 return -EINVAL;
6389 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6390 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6392 if (info->attrs[NL80211_ATTR_IE]) {
6393 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6394 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6397 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
6398 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
6399 return -EINVAL;
6401 if (auth_type == NL80211_AUTHTYPE_SAE &&
6402 !info->attrs[NL80211_ATTR_SAE_DATA])
6403 return -EINVAL;
6405 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6406 if (auth_type != NL80211_AUTHTYPE_SAE)
6407 return -EINVAL;
6408 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6409 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6410 /* need to include at least Auth Transaction and Status Code */
6411 if (sae_data_len < 4)
6412 return -EINVAL;
6415 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6418 * Since we no longer track auth state, ignore
6419 * requests to only change local state.
6421 if (local_state_change)
6422 return 0;
6424 wdev_lock(dev->ieee80211_ptr);
6425 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6426 ssid, ssid_len, ie, ie_len,
6427 key.p.key, key.p.key_len, key.idx,
6428 sae_data, sae_data_len);
6429 wdev_unlock(dev->ieee80211_ptr);
6430 return err;
6433 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6434 struct genl_info *info,
6435 struct cfg80211_crypto_settings *settings,
6436 int cipher_limit)
6438 memset(settings, 0, sizeof(*settings));
6440 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6442 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6443 u16 proto;
6444 proto = nla_get_u16(
6445 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6446 settings->control_port_ethertype = cpu_to_be16(proto);
6447 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6448 proto != ETH_P_PAE)
6449 return -EINVAL;
6450 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6451 settings->control_port_no_encrypt = true;
6452 } else
6453 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6455 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6456 void *data;
6457 int len, i;
6459 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6460 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6461 settings->n_ciphers_pairwise = len / sizeof(u32);
6463 if (len % sizeof(u32))
6464 return -EINVAL;
6466 if (settings->n_ciphers_pairwise > cipher_limit)
6467 return -EINVAL;
6469 memcpy(settings->ciphers_pairwise, data, len);
6471 for (i = 0; i < settings->n_ciphers_pairwise; i++)
6472 if (!cfg80211_supported_cipher_suite(
6473 &rdev->wiphy,
6474 settings->ciphers_pairwise[i]))
6475 return -EINVAL;
6478 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6479 settings->cipher_group =
6480 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
6481 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6482 settings->cipher_group))
6483 return -EINVAL;
6486 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6487 settings->wpa_versions =
6488 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6489 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6490 return -EINVAL;
6493 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6494 void *data;
6495 int len;
6497 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6498 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6499 settings->n_akm_suites = len / sizeof(u32);
6501 if (len % sizeof(u32))
6502 return -EINVAL;
6504 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6505 return -EINVAL;
6507 memcpy(settings->akm_suites, data, len);
6510 return 0;
6513 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6515 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6516 struct net_device *dev = info->user_ptr[1];
6517 struct ieee80211_channel *chan;
6518 struct cfg80211_assoc_request req = {};
6519 const u8 *bssid, *ssid;
6520 int err, ssid_len = 0;
6522 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6523 return -EINVAL;
6525 if (!info->attrs[NL80211_ATTR_MAC] ||
6526 !info->attrs[NL80211_ATTR_SSID] ||
6527 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6528 return -EINVAL;
6530 if (!rdev->ops->assoc)
6531 return -EOPNOTSUPP;
6533 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6534 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6535 return -EOPNOTSUPP;
6537 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6539 chan = nl80211_get_valid_chan(&rdev->wiphy,
6540 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6541 if (!chan)
6542 return -EINVAL;
6544 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6545 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6547 if (info->attrs[NL80211_ATTR_IE]) {
6548 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6549 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6552 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6553 enum nl80211_mfp mfp =
6554 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6555 if (mfp == NL80211_MFP_REQUIRED)
6556 req.use_mfp = true;
6557 else if (mfp != NL80211_MFP_NO)
6558 return -EINVAL;
6561 if (info->attrs[NL80211_ATTR_PREV_BSSID])
6562 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
6564 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6565 req.flags |= ASSOC_REQ_DISABLE_HT;
6567 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6568 memcpy(&req.ht_capa_mask,
6569 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6570 sizeof(req.ht_capa_mask));
6572 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6573 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6574 return -EINVAL;
6575 memcpy(&req.ht_capa,
6576 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6577 sizeof(req.ht_capa));
6580 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6581 req.flags |= ASSOC_REQ_DISABLE_VHT;
6583 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6584 memcpy(&req.vht_capa_mask,
6585 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6586 sizeof(req.vht_capa_mask));
6588 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6589 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6590 return -EINVAL;
6591 memcpy(&req.vht_capa,
6592 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6593 sizeof(req.vht_capa));
6596 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
6597 if (!err) {
6598 wdev_lock(dev->ieee80211_ptr);
6599 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6600 ssid, ssid_len, &req);
6601 wdev_unlock(dev->ieee80211_ptr);
6604 return err;
6607 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6609 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6610 struct net_device *dev = info->user_ptr[1];
6611 const u8 *ie = NULL, *bssid;
6612 int ie_len = 0, err;
6613 u16 reason_code;
6614 bool local_state_change;
6616 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6617 return -EINVAL;
6619 if (!info->attrs[NL80211_ATTR_MAC])
6620 return -EINVAL;
6622 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6623 return -EINVAL;
6625 if (!rdev->ops->deauth)
6626 return -EOPNOTSUPP;
6628 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6629 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6630 return -EOPNOTSUPP;
6632 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6634 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6635 if (reason_code == 0) {
6636 /* Reason Code 0 is reserved */
6637 return -EINVAL;
6640 if (info->attrs[NL80211_ATTR_IE]) {
6641 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6642 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6645 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6647 wdev_lock(dev->ieee80211_ptr);
6648 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6649 local_state_change);
6650 wdev_unlock(dev->ieee80211_ptr);
6651 return err;
6654 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6657 struct net_device *dev = info->user_ptr[1];
6658 const u8 *ie = NULL, *bssid;
6659 int ie_len = 0, err;
6660 u16 reason_code;
6661 bool local_state_change;
6663 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6664 return -EINVAL;
6666 if (!info->attrs[NL80211_ATTR_MAC])
6667 return -EINVAL;
6669 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6670 return -EINVAL;
6672 if (!rdev->ops->disassoc)
6673 return -EOPNOTSUPP;
6675 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6676 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6677 return -EOPNOTSUPP;
6679 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6681 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6682 if (reason_code == 0) {
6683 /* Reason Code 0 is reserved */
6684 return -EINVAL;
6687 if (info->attrs[NL80211_ATTR_IE]) {
6688 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6689 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6692 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6694 wdev_lock(dev->ieee80211_ptr);
6695 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6696 local_state_change);
6697 wdev_unlock(dev->ieee80211_ptr);
6698 return err;
6701 static bool
6702 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6703 int mcast_rate[IEEE80211_NUM_BANDS],
6704 int rateval)
6706 struct wiphy *wiphy = &rdev->wiphy;
6707 bool found = false;
6708 int band, i;
6710 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6711 struct ieee80211_supported_band *sband;
6713 sband = wiphy->bands[band];
6714 if (!sband)
6715 continue;
6717 for (i = 0; i < sband->n_bitrates; i++) {
6718 if (sband->bitrates[i].bitrate == rateval) {
6719 mcast_rate[band] = i + 1;
6720 found = true;
6721 break;
6726 return found;
6729 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6732 struct net_device *dev = info->user_ptr[1];
6733 struct cfg80211_ibss_params ibss;
6734 struct wiphy *wiphy;
6735 struct cfg80211_cached_keys *connkeys = NULL;
6736 int err;
6738 memset(&ibss, 0, sizeof(ibss));
6740 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6741 return -EINVAL;
6743 if (!info->attrs[NL80211_ATTR_SSID] ||
6744 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6745 return -EINVAL;
6747 ibss.beacon_interval = 100;
6749 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6750 ibss.beacon_interval =
6751 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6752 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6753 return -EINVAL;
6756 if (!rdev->ops->join_ibss)
6757 return -EOPNOTSUPP;
6759 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6760 return -EOPNOTSUPP;
6762 wiphy = &rdev->wiphy;
6764 if (info->attrs[NL80211_ATTR_MAC]) {
6765 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6767 if (!is_valid_ether_addr(ibss.bssid))
6768 return -EINVAL;
6770 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6771 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6773 if (info->attrs[NL80211_ATTR_IE]) {
6774 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6775 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6778 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6779 if (err)
6780 return err;
6782 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
6783 NL80211_IFTYPE_ADHOC))
6784 return -EINVAL;
6786 switch (ibss.chandef.width) {
6787 case NL80211_CHAN_WIDTH_5:
6788 case NL80211_CHAN_WIDTH_10:
6789 case NL80211_CHAN_WIDTH_20_NOHT:
6790 break;
6791 case NL80211_CHAN_WIDTH_20:
6792 case NL80211_CHAN_WIDTH_40:
6793 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6794 break;
6795 default:
6796 return -EINVAL;
6799 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
6800 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6802 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6803 u8 *rates =
6804 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6805 int n_rates =
6806 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6807 struct ieee80211_supported_band *sband =
6808 wiphy->bands[ibss.chandef.chan->band];
6810 err = ieee80211_get_ratemask(sband, rates, n_rates,
6811 &ibss.basic_rates);
6812 if (err)
6813 return err;
6816 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6817 memcpy(&ibss.ht_capa_mask,
6818 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6819 sizeof(ibss.ht_capa_mask));
6821 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6822 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6823 return -EINVAL;
6824 memcpy(&ibss.ht_capa,
6825 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6826 sizeof(ibss.ht_capa));
6829 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6830 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6831 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6832 return -EINVAL;
6834 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6835 bool no_ht = false;
6837 connkeys = nl80211_parse_connkeys(rdev,
6838 info->attrs[NL80211_ATTR_KEYS],
6839 &no_ht);
6840 if (IS_ERR(connkeys))
6841 return PTR_ERR(connkeys);
6843 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6844 no_ht) {
6845 kfree(connkeys);
6846 return -EINVAL;
6850 ibss.control_port =
6851 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6853 ibss.userspace_handles_dfs =
6854 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6856 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
6857 if (err)
6858 kfree(connkeys);
6859 return err;
6862 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6864 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6865 struct net_device *dev = info->user_ptr[1];
6867 if (!rdev->ops->leave_ibss)
6868 return -EOPNOTSUPP;
6870 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6871 return -EOPNOTSUPP;
6873 return cfg80211_leave_ibss(rdev, dev, false);
6876 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6878 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6879 struct net_device *dev = info->user_ptr[1];
6880 int mcast_rate[IEEE80211_NUM_BANDS];
6881 u32 nla_rate;
6882 int err;
6884 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6885 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6886 return -EOPNOTSUPP;
6888 if (!rdev->ops->set_mcast_rate)
6889 return -EOPNOTSUPP;
6891 memset(mcast_rate, 0, sizeof(mcast_rate));
6893 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6894 return -EINVAL;
6896 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6897 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6898 return -EINVAL;
6900 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6902 return err;
6905 static struct sk_buff *
6906 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6907 int approxlen, u32 portid, u32 seq,
6908 enum nl80211_commands cmd,
6909 enum nl80211_attrs attr,
6910 const struct nl80211_vendor_cmd_info *info,
6911 gfp_t gfp)
6913 struct sk_buff *skb;
6914 void *hdr;
6915 struct nlattr *data;
6917 skb = nlmsg_new(approxlen + 100, gfp);
6918 if (!skb)
6919 return NULL;
6921 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6922 if (!hdr) {
6923 kfree_skb(skb);
6924 return NULL;
6927 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6928 goto nla_put_failure;
6930 if (info) {
6931 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
6932 info->vendor_id))
6933 goto nla_put_failure;
6934 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
6935 info->subcmd))
6936 goto nla_put_failure;
6939 data = nla_nest_start(skb, attr);
6941 ((void **)skb->cb)[0] = rdev;
6942 ((void **)skb->cb)[1] = hdr;
6943 ((void **)skb->cb)[2] = data;
6945 return skb;
6947 nla_put_failure:
6948 kfree_skb(skb);
6949 return NULL;
6952 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
6953 enum nl80211_commands cmd,
6954 enum nl80211_attrs attr,
6955 int vendor_event_idx,
6956 int approxlen, gfp_t gfp)
6958 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
6959 const struct nl80211_vendor_cmd_info *info;
6961 switch (cmd) {
6962 case NL80211_CMD_TESTMODE:
6963 if (WARN_ON(vendor_event_idx != -1))
6964 return NULL;
6965 info = NULL;
6966 break;
6967 case NL80211_CMD_VENDOR:
6968 if (WARN_ON(vendor_event_idx < 0 ||
6969 vendor_event_idx >= wiphy->n_vendor_events))
6970 return NULL;
6971 info = &wiphy->vendor_events[vendor_event_idx];
6972 break;
6973 default:
6974 WARN_ON(1);
6975 return NULL;
6978 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6979 cmd, attr, info, gfp);
6981 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
6983 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
6985 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6986 void *hdr = ((void **)skb->cb)[1];
6987 struct nlattr *data = ((void **)skb->cb)[2];
6988 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
6990 /* clear CB data for netlink core to own from now on */
6991 memset(skb->cb, 0, sizeof(skb->cb));
6993 nla_nest_end(skb, data);
6994 genlmsg_end(skb, hdr);
6996 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
6997 mcgrp = NL80211_MCGRP_VENDOR;
6999 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
7000 mcgrp, gfp);
7002 EXPORT_SYMBOL(__cfg80211_send_event_skb);
7004 #ifdef CONFIG_NL80211_TESTMODE
7005 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
7007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7008 struct wireless_dev *wdev =
7009 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
7010 int err;
7012 if (!rdev->ops->testmode_cmd)
7013 return -EOPNOTSUPP;
7015 if (IS_ERR(wdev)) {
7016 err = PTR_ERR(wdev);
7017 if (err != -EINVAL)
7018 return err;
7019 wdev = NULL;
7020 } else if (wdev->wiphy != &rdev->wiphy) {
7021 return -EINVAL;
7024 if (!info->attrs[NL80211_ATTR_TESTDATA])
7025 return -EINVAL;
7027 rdev->cur_cmd_info = info;
7028 err = rdev_testmode_cmd(rdev, wdev,
7029 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
7030 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
7031 rdev->cur_cmd_info = NULL;
7033 return err;
7036 static int nl80211_testmode_dump(struct sk_buff *skb,
7037 struct netlink_callback *cb)
7039 struct cfg80211_registered_device *rdev;
7040 int err;
7041 long phy_idx;
7042 void *data = NULL;
7043 int data_len = 0;
7045 rtnl_lock();
7047 if (cb->args[0]) {
7049 * 0 is a valid index, but not valid for args[0],
7050 * so we need to offset by 1.
7052 phy_idx = cb->args[0] - 1;
7053 } else {
7054 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
7055 nl80211_fam.attrbuf, nl80211_fam.maxattr,
7056 nl80211_policy);
7057 if (err)
7058 goto out_err;
7060 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
7061 nl80211_fam.attrbuf);
7062 if (IS_ERR(rdev)) {
7063 err = PTR_ERR(rdev);
7064 goto out_err;
7066 phy_idx = rdev->wiphy_idx;
7067 rdev = NULL;
7069 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7070 cb->args[1] =
7071 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7074 if (cb->args[1]) {
7075 data = nla_data((void *)cb->args[1]);
7076 data_len = nla_len((void *)cb->args[1]);
7079 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7080 if (!rdev) {
7081 err = -ENOENT;
7082 goto out_err;
7085 if (!rdev->ops->testmode_dump) {
7086 err = -EOPNOTSUPP;
7087 goto out_err;
7090 while (1) {
7091 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
7092 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7093 NL80211_CMD_TESTMODE);
7094 struct nlattr *tmdata;
7096 if (!hdr)
7097 break;
7099 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
7100 genlmsg_cancel(skb, hdr);
7101 break;
7104 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7105 if (!tmdata) {
7106 genlmsg_cancel(skb, hdr);
7107 break;
7109 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
7110 nla_nest_end(skb, tmdata);
7112 if (err == -ENOBUFS || err == -ENOENT) {
7113 genlmsg_cancel(skb, hdr);
7114 break;
7115 } else if (err) {
7116 genlmsg_cancel(skb, hdr);
7117 goto out_err;
7120 genlmsg_end(skb, hdr);
7123 err = skb->len;
7124 /* see above */
7125 cb->args[0] = phy_idx + 1;
7126 out_err:
7127 rtnl_unlock();
7128 return err;
7130 #endif
7132 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7134 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7135 struct net_device *dev = info->user_ptr[1];
7136 struct cfg80211_connect_params connect;
7137 struct wiphy *wiphy;
7138 struct cfg80211_cached_keys *connkeys = NULL;
7139 int err;
7141 memset(&connect, 0, sizeof(connect));
7143 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7144 return -EINVAL;
7146 if (!info->attrs[NL80211_ATTR_SSID] ||
7147 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7148 return -EINVAL;
7150 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7151 connect.auth_type =
7152 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
7153 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7154 NL80211_CMD_CONNECT))
7155 return -EINVAL;
7156 } else
7157 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7159 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7161 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
7162 NL80211_MAX_NR_CIPHER_SUITES);
7163 if (err)
7164 return err;
7166 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7167 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7168 return -EOPNOTSUPP;
7170 wiphy = &rdev->wiphy;
7172 connect.bg_scan_period = -1;
7173 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7174 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7175 connect.bg_scan_period =
7176 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7179 if (info->attrs[NL80211_ATTR_MAC])
7180 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7181 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7182 connect.bssid_hint =
7183 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
7184 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7185 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7187 if (info->attrs[NL80211_ATTR_IE]) {
7188 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7189 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7192 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7193 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7194 if (connect.mfp != NL80211_MFP_REQUIRED &&
7195 connect.mfp != NL80211_MFP_NO)
7196 return -EINVAL;
7197 } else {
7198 connect.mfp = NL80211_MFP_NO;
7201 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7202 connect.channel = nl80211_get_valid_chan(
7203 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7204 if (!connect.channel)
7205 return -EINVAL;
7206 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
7207 connect.channel_hint = nl80211_get_valid_chan(
7208 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7209 if (!connect.channel_hint)
7210 return -EINVAL;
7213 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7214 connkeys = nl80211_parse_connkeys(rdev,
7215 info->attrs[NL80211_ATTR_KEYS], NULL);
7216 if (IS_ERR(connkeys))
7217 return PTR_ERR(connkeys);
7220 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7221 connect.flags |= ASSOC_REQ_DISABLE_HT;
7223 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7224 memcpy(&connect.ht_capa_mask,
7225 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7226 sizeof(connect.ht_capa_mask));
7228 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7229 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7230 kfree(connkeys);
7231 return -EINVAL;
7233 memcpy(&connect.ht_capa,
7234 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7235 sizeof(connect.ht_capa));
7238 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7239 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7241 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7242 memcpy(&connect.vht_capa_mask,
7243 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7244 sizeof(connect.vht_capa_mask));
7246 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7247 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7248 kfree(connkeys);
7249 return -EINVAL;
7251 memcpy(&connect.vht_capa,
7252 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7253 sizeof(connect.vht_capa));
7256 wdev_lock(dev->ieee80211_ptr);
7257 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7258 wdev_unlock(dev->ieee80211_ptr);
7259 if (err)
7260 kfree(connkeys);
7261 return err;
7264 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7266 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7267 struct net_device *dev = info->user_ptr[1];
7268 u16 reason;
7269 int ret;
7271 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7272 reason = WLAN_REASON_DEAUTH_LEAVING;
7273 else
7274 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7276 if (reason == 0)
7277 return -EINVAL;
7279 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7280 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7281 return -EOPNOTSUPP;
7283 wdev_lock(dev->ieee80211_ptr);
7284 ret = cfg80211_disconnect(rdev, dev, reason, true);
7285 wdev_unlock(dev->ieee80211_ptr);
7286 return ret;
7289 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7291 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7292 struct net *net;
7293 int err;
7294 u32 pid;
7296 if (!info->attrs[NL80211_ATTR_PID])
7297 return -EINVAL;
7299 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7301 net = get_net_ns_by_pid(pid);
7302 if (IS_ERR(net))
7303 return PTR_ERR(net);
7305 err = 0;
7307 /* check if anything to do */
7308 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7309 err = cfg80211_switch_netns(rdev, net);
7311 put_net(net);
7312 return err;
7315 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7318 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7319 struct cfg80211_pmksa *pmksa) = NULL;
7320 struct net_device *dev = info->user_ptr[1];
7321 struct cfg80211_pmksa pmksa;
7323 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7325 if (!info->attrs[NL80211_ATTR_MAC])
7326 return -EINVAL;
7328 if (!info->attrs[NL80211_ATTR_PMKID])
7329 return -EINVAL;
7331 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7332 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7334 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7335 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7336 return -EOPNOTSUPP;
7338 switch (info->genlhdr->cmd) {
7339 case NL80211_CMD_SET_PMKSA:
7340 rdev_ops = rdev->ops->set_pmksa;
7341 break;
7342 case NL80211_CMD_DEL_PMKSA:
7343 rdev_ops = rdev->ops->del_pmksa;
7344 break;
7345 default:
7346 WARN_ON(1);
7347 break;
7350 if (!rdev_ops)
7351 return -EOPNOTSUPP;
7353 return rdev_ops(&rdev->wiphy, dev, &pmksa);
7356 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7359 struct net_device *dev = info->user_ptr[1];
7361 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7362 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7363 return -EOPNOTSUPP;
7365 if (!rdev->ops->flush_pmksa)
7366 return -EOPNOTSUPP;
7368 return rdev_flush_pmksa(rdev, dev);
7371 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7373 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7374 struct net_device *dev = info->user_ptr[1];
7375 u8 action_code, dialog_token;
7376 u32 peer_capability = 0;
7377 u16 status_code;
7378 u8 *peer;
7380 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7381 !rdev->ops->tdls_mgmt)
7382 return -EOPNOTSUPP;
7384 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7385 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7386 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7387 !info->attrs[NL80211_ATTR_IE] ||
7388 !info->attrs[NL80211_ATTR_MAC])
7389 return -EINVAL;
7391 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7392 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7393 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7394 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
7395 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
7396 peer_capability =
7397 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
7399 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
7400 dialog_token, status_code, peer_capability,
7401 nla_data(info->attrs[NL80211_ATTR_IE]),
7402 nla_len(info->attrs[NL80211_ATTR_IE]));
7405 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7408 struct net_device *dev = info->user_ptr[1];
7409 enum nl80211_tdls_operation operation;
7410 u8 *peer;
7412 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7413 !rdev->ops->tdls_oper)
7414 return -EOPNOTSUPP;
7416 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7417 !info->attrs[NL80211_ATTR_MAC])
7418 return -EINVAL;
7420 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7421 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7423 return rdev_tdls_oper(rdev, dev, peer, operation);
7426 static int nl80211_remain_on_channel(struct sk_buff *skb,
7427 struct genl_info *info)
7429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7430 struct wireless_dev *wdev = info->user_ptr[1];
7431 struct cfg80211_chan_def chandef;
7432 struct sk_buff *msg;
7433 void *hdr;
7434 u64 cookie;
7435 u32 duration;
7436 int err;
7438 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7439 !info->attrs[NL80211_ATTR_DURATION])
7440 return -EINVAL;
7442 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7444 if (!rdev->ops->remain_on_channel ||
7445 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
7446 return -EOPNOTSUPP;
7449 * We should be on that channel for at least a minimum amount of
7450 * time (10ms) but no longer than the driver supports.
7452 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7453 duration > rdev->wiphy.max_remain_on_channel_duration)
7454 return -EINVAL;
7456 err = nl80211_parse_chandef(rdev, info, &chandef);
7457 if (err)
7458 return err;
7460 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7461 if (!msg)
7462 return -ENOMEM;
7464 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7465 NL80211_CMD_REMAIN_ON_CHANNEL);
7466 if (!hdr) {
7467 err = -ENOBUFS;
7468 goto free_msg;
7471 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7472 duration, &cookie);
7474 if (err)
7475 goto free_msg;
7477 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7478 goto nla_put_failure;
7480 genlmsg_end(msg, hdr);
7482 return genlmsg_reply(msg, info);
7484 nla_put_failure:
7485 err = -ENOBUFS;
7486 free_msg:
7487 nlmsg_free(msg);
7488 return err;
7491 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7492 struct genl_info *info)
7494 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7495 struct wireless_dev *wdev = info->user_ptr[1];
7496 u64 cookie;
7498 if (!info->attrs[NL80211_ATTR_COOKIE])
7499 return -EINVAL;
7501 if (!rdev->ops->cancel_remain_on_channel)
7502 return -EOPNOTSUPP;
7504 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7506 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
7509 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7510 u8 *rates, u8 rates_len)
7512 u8 i;
7513 u32 mask = 0;
7515 for (i = 0; i < rates_len; i++) {
7516 int rate = (rates[i] & 0x7f) * 5;
7517 int ridx;
7518 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7519 struct ieee80211_rate *srate =
7520 &sband->bitrates[ridx];
7521 if (rate == srate->bitrate) {
7522 mask |= 1 << ridx;
7523 break;
7526 if (ridx == sband->n_bitrates)
7527 return 0; /* rate not found */
7530 return mask;
7533 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7534 u8 *rates, u8 rates_len,
7535 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7537 u8 i;
7539 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7541 for (i = 0; i < rates_len; i++) {
7542 int ridx, rbit;
7544 ridx = rates[i] / 8;
7545 rbit = BIT(rates[i] % 8);
7547 /* check validity */
7548 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
7549 return false;
7551 /* check availability */
7552 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7553 mcs[ridx] |= rbit;
7554 else
7555 return false;
7558 return true;
7561 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
7563 u16 mcs_mask = 0;
7565 switch (vht_mcs_map) {
7566 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
7567 break;
7568 case IEEE80211_VHT_MCS_SUPPORT_0_7:
7569 mcs_mask = 0x00FF;
7570 break;
7571 case IEEE80211_VHT_MCS_SUPPORT_0_8:
7572 mcs_mask = 0x01FF;
7573 break;
7574 case IEEE80211_VHT_MCS_SUPPORT_0_9:
7575 mcs_mask = 0x03FF;
7576 break;
7577 default:
7578 break;
7581 return mcs_mask;
7584 static void vht_build_mcs_mask(u16 vht_mcs_map,
7585 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
7587 u8 nss;
7589 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
7590 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
7591 vht_mcs_map >>= 2;
7595 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
7596 struct nl80211_txrate_vht *txrate,
7597 u16 mcs[NL80211_VHT_NSS_MAX])
7599 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7600 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
7601 u8 i;
7603 if (!sband->vht_cap.vht_supported)
7604 return false;
7606 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
7608 /* Build vht_mcs_mask from VHT capabilities */
7609 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
7611 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7612 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
7613 mcs[i] = txrate->mcs[i];
7614 else
7615 return false;
7618 return true;
7621 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
7622 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7623 .len = NL80211_MAX_SUPP_RATES },
7624 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7625 .len = NL80211_MAX_SUPP_HT_RATES },
7626 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
7627 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
7630 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7631 struct genl_info *info)
7633 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
7634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7635 struct cfg80211_bitrate_mask mask;
7636 int rem, i;
7637 struct net_device *dev = info->user_ptr[1];
7638 struct nlattr *tx_rates;
7639 struct ieee80211_supported_band *sband;
7640 u16 vht_tx_mcs_map;
7642 if (!rdev->ops->set_bitrate_mask)
7643 return -EOPNOTSUPP;
7645 memset(&mask, 0, sizeof(mask));
7646 /* Default to all rates enabled */
7647 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7648 sband = rdev->wiphy.bands[i];
7650 if (!sband)
7651 continue;
7653 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
7654 memcpy(mask.control[i].ht_mcs,
7655 sband->ht_cap.mcs.rx_mask,
7656 sizeof(mask.control[i].ht_mcs));
7658 if (!sband->vht_cap.vht_supported)
7659 continue;
7661 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7662 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
7665 /* if no rates are given set it back to the defaults */
7666 if (!info->attrs[NL80211_ATTR_TX_RATES])
7667 goto out;
7670 * The nested attribute uses enum nl80211_band as the index. This maps
7671 * directly to the enum ieee80211_band values used in cfg80211.
7673 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
7674 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
7675 enum ieee80211_band band = nla_type(tx_rates);
7676 int err;
7678 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7679 return -EINVAL;
7680 sband = rdev->wiphy.bands[band];
7681 if (sband == NULL)
7682 return -EINVAL;
7683 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7684 nla_len(tx_rates), nl80211_txattr_policy);
7685 if (err)
7686 return err;
7687 if (tb[NL80211_TXRATE_LEGACY]) {
7688 mask.control[band].legacy = rateset_to_mask(
7689 sband,
7690 nla_data(tb[NL80211_TXRATE_LEGACY]),
7691 nla_len(tb[NL80211_TXRATE_LEGACY]));
7692 if ((mask.control[band].legacy == 0) &&
7693 nla_len(tb[NL80211_TXRATE_LEGACY]))
7694 return -EINVAL;
7696 if (tb[NL80211_TXRATE_HT]) {
7697 if (!ht_rateset_to_mask(
7698 sband,
7699 nla_data(tb[NL80211_TXRATE_HT]),
7700 nla_len(tb[NL80211_TXRATE_HT]),
7701 mask.control[band].ht_mcs))
7702 return -EINVAL;
7704 if (tb[NL80211_TXRATE_VHT]) {
7705 if (!vht_set_mcs_mask(
7706 sband,
7707 nla_data(tb[NL80211_TXRATE_VHT]),
7708 mask.control[band].vht_mcs))
7709 return -EINVAL;
7711 if (tb[NL80211_TXRATE_GI]) {
7712 mask.control[band].gi =
7713 nla_get_u8(tb[NL80211_TXRATE_GI]);
7714 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
7715 return -EINVAL;
7718 if (mask.control[band].legacy == 0) {
7719 /* don't allow empty legacy rates if HT or VHT
7720 * are not even supported.
7722 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
7723 rdev->wiphy.bands[band]->vht_cap.vht_supported))
7724 return -EINVAL;
7726 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7727 if (mask.control[band].ht_mcs[i])
7728 goto out;
7730 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
7731 if (mask.control[band].vht_mcs[i])
7732 goto out;
7734 /* legacy and mcs rates may not be both empty */
7735 return -EINVAL;
7739 out:
7740 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
7743 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
7745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7746 struct wireless_dev *wdev = info->user_ptr[1];
7747 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
7749 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7750 return -EINVAL;
7752 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7753 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
7755 switch (wdev->iftype) {
7756 case NL80211_IFTYPE_STATION:
7757 case NL80211_IFTYPE_ADHOC:
7758 case NL80211_IFTYPE_P2P_CLIENT:
7759 case NL80211_IFTYPE_AP:
7760 case NL80211_IFTYPE_AP_VLAN:
7761 case NL80211_IFTYPE_MESH_POINT:
7762 case NL80211_IFTYPE_P2P_GO:
7763 case NL80211_IFTYPE_P2P_DEVICE:
7764 break;
7765 default:
7766 return -EOPNOTSUPP;
7769 /* not much point in registering if we can't reply */
7770 if (!rdev->ops->mgmt_tx)
7771 return -EOPNOTSUPP;
7773 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
7774 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7775 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
7778 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
7780 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7781 struct wireless_dev *wdev = info->user_ptr[1];
7782 struct cfg80211_chan_def chandef;
7783 int err;
7784 void *hdr = NULL;
7785 u64 cookie;
7786 struct sk_buff *msg = NULL;
7787 struct cfg80211_mgmt_tx_params params = {
7788 .dont_wait_for_ack =
7789 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7792 if (!info->attrs[NL80211_ATTR_FRAME])
7793 return -EINVAL;
7795 if (!rdev->ops->mgmt_tx)
7796 return -EOPNOTSUPP;
7798 switch (wdev->iftype) {
7799 case NL80211_IFTYPE_P2P_DEVICE:
7800 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7801 return -EINVAL;
7802 case NL80211_IFTYPE_STATION:
7803 case NL80211_IFTYPE_ADHOC:
7804 case NL80211_IFTYPE_P2P_CLIENT:
7805 case NL80211_IFTYPE_AP:
7806 case NL80211_IFTYPE_AP_VLAN:
7807 case NL80211_IFTYPE_MESH_POINT:
7808 case NL80211_IFTYPE_P2P_GO:
7809 break;
7810 default:
7811 return -EOPNOTSUPP;
7814 if (info->attrs[NL80211_ATTR_DURATION]) {
7815 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7816 return -EINVAL;
7817 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7820 * We should wait on the channel for at least a minimum amount
7821 * of time (10ms) but no longer than the driver supports.
7823 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7824 params.wait > rdev->wiphy.max_remain_on_channel_duration)
7825 return -EINVAL;
7829 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7831 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7832 return -EINVAL;
7834 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7836 /* get the channel if any has been specified, otherwise pass NULL to
7837 * the driver. The latter will use the current one
7839 chandef.chan = NULL;
7840 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7841 err = nl80211_parse_chandef(rdev, info, &chandef);
7842 if (err)
7843 return err;
7846 if (!chandef.chan && params.offchan)
7847 return -EINVAL;
7849 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7850 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7852 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
7853 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
7854 int i;
7856 if (len % sizeof(u16))
7857 return -EINVAL;
7859 params.n_csa_offsets = len / sizeof(u16);
7860 params.csa_offsets =
7861 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
7863 /* check that all the offsets fit the frame */
7864 for (i = 0; i < params.n_csa_offsets; i++) {
7865 if (params.csa_offsets[i] >= params.len)
7866 return -EINVAL;
7870 if (!params.dont_wait_for_ack) {
7871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7872 if (!msg)
7873 return -ENOMEM;
7875 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7876 NL80211_CMD_FRAME);
7877 if (!hdr) {
7878 err = -ENOBUFS;
7879 goto free_msg;
7883 params.chan = chandef.chan;
7884 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
7885 if (err)
7886 goto free_msg;
7888 if (msg) {
7889 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7890 goto nla_put_failure;
7892 genlmsg_end(msg, hdr);
7893 return genlmsg_reply(msg, info);
7896 return 0;
7898 nla_put_failure:
7899 err = -ENOBUFS;
7900 free_msg:
7901 nlmsg_free(msg);
7902 return err;
7905 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7908 struct wireless_dev *wdev = info->user_ptr[1];
7909 u64 cookie;
7911 if (!info->attrs[NL80211_ATTR_COOKIE])
7912 return -EINVAL;
7914 if (!rdev->ops->mgmt_tx_cancel_wait)
7915 return -EOPNOTSUPP;
7917 switch (wdev->iftype) {
7918 case NL80211_IFTYPE_STATION:
7919 case NL80211_IFTYPE_ADHOC:
7920 case NL80211_IFTYPE_P2P_CLIENT:
7921 case NL80211_IFTYPE_AP:
7922 case NL80211_IFTYPE_AP_VLAN:
7923 case NL80211_IFTYPE_P2P_GO:
7924 case NL80211_IFTYPE_P2P_DEVICE:
7925 break;
7926 default:
7927 return -EOPNOTSUPP;
7930 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7932 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
7935 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7937 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7938 struct wireless_dev *wdev;
7939 struct net_device *dev = info->user_ptr[1];
7940 u8 ps_state;
7941 bool state;
7942 int err;
7944 if (!info->attrs[NL80211_ATTR_PS_STATE])
7945 return -EINVAL;
7947 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7949 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7950 return -EINVAL;
7952 wdev = dev->ieee80211_ptr;
7954 if (!rdev->ops->set_power_mgmt)
7955 return -EOPNOTSUPP;
7957 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7959 if (state == wdev->ps)
7960 return 0;
7962 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
7963 if (!err)
7964 wdev->ps = state;
7965 return err;
7968 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7971 enum nl80211_ps_state ps_state;
7972 struct wireless_dev *wdev;
7973 struct net_device *dev = info->user_ptr[1];
7974 struct sk_buff *msg;
7975 void *hdr;
7976 int err;
7978 wdev = dev->ieee80211_ptr;
7980 if (!rdev->ops->set_power_mgmt)
7981 return -EOPNOTSUPP;
7983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7984 if (!msg)
7985 return -ENOMEM;
7987 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7988 NL80211_CMD_GET_POWER_SAVE);
7989 if (!hdr) {
7990 err = -ENOBUFS;
7991 goto free_msg;
7994 if (wdev->ps)
7995 ps_state = NL80211_PS_ENABLED;
7996 else
7997 ps_state = NL80211_PS_DISABLED;
7999 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
8000 goto nla_put_failure;
8002 genlmsg_end(msg, hdr);
8003 return genlmsg_reply(msg, info);
8005 nla_put_failure:
8006 err = -ENOBUFS;
8007 free_msg:
8008 nlmsg_free(msg);
8009 return err;
8012 static const struct nla_policy
8013 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
8014 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
8015 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
8016 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
8017 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
8018 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
8019 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
8022 static int nl80211_set_cqm_txe(struct genl_info *info,
8023 u32 rate, u32 pkts, u32 intvl)
8025 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8026 struct net_device *dev = info->user_ptr[1];
8027 struct wireless_dev *wdev = dev->ieee80211_ptr;
8029 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
8030 return -EINVAL;
8032 if (!rdev->ops->set_cqm_txe_config)
8033 return -EOPNOTSUPP;
8035 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8036 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8037 return -EOPNOTSUPP;
8039 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
8042 static int nl80211_set_cqm_rssi(struct genl_info *info,
8043 s32 threshold, u32 hysteresis)
8045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8046 struct net_device *dev = info->user_ptr[1];
8047 struct wireless_dev *wdev = dev->ieee80211_ptr;
8049 if (threshold > 0)
8050 return -EINVAL;
8052 /* disabling - hysteresis should also be zero then */
8053 if (threshold == 0)
8054 hysteresis = 0;
8056 if (!rdev->ops->set_cqm_rssi_config)
8057 return -EOPNOTSUPP;
8059 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8060 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8061 return -EOPNOTSUPP;
8063 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
8066 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
8068 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
8069 struct nlattr *cqm;
8070 int err;
8072 cqm = info->attrs[NL80211_ATTR_CQM];
8073 if (!cqm)
8074 return -EINVAL;
8076 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
8077 nl80211_attr_cqm_policy);
8078 if (err)
8079 return err;
8081 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8082 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
8083 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8084 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
8086 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8089 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8090 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8091 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8092 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8093 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8094 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8096 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8099 return -EINVAL;
8102 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8105 struct net_device *dev = info->user_ptr[1];
8106 struct mesh_config cfg;
8107 struct mesh_setup setup;
8108 int err;
8110 /* start with default */
8111 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
8112 memcpy(&setup, &default_mesh_setup, sizeof(setup));
8114 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
8115 /* and parse parameters if given */
8116 err = nl80211_parse_mesh_config(info, &cfg, NULL);
8117 if (err)
8118 return err;
8121 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8122 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8123 return -EINVAL;
8125 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8126 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8128 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8129 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8130 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8131 return -EINVAL;
8133 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8134 setup.beacon_interval =
8135 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8136 if (setup.beacon_interval < 10 ||
8137 setup.beacon_interval > 10000)
8138 return -EINVAL;
8141 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8142 setup.dtim_period =
8143 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8144 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8145 return -EINVAL;
8148 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8149 /* parse additional setup parameters if given */
8150 err = nl80211_parse_mesh_setup(info, &setup);
8151 if (err)
8152 return err;
8155 if (setup.user_mpm)
8156 cfg.auto_open_plinks = false;
8158 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8159 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8160 if (err)
8161 return err;
8162 } else {
8163 /* cfg80211_join_mesh() will sort it out */
8164 setup.chandef.chan = NULL;
8167 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8168 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8169 int n_rates =
8170 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8171 struct ieee80211_supported_band *sband;
8173 if (!setup.chandef.chan)
8174 return -EINVAL;
8176 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8178 err = ieee80211_get_ratemask(sband, rates, n_rates,
8179 &setup.basic_rates);
8180 if (err)
8181 return err;
8184 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
8187 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8190 struct net_device *dev = info->user_ptr[1];
8192 return cfg80211_leave_mesh(rdev, dev);
8195 #ifdef CONFIG_PM
8196 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8197 struct cfg80211_registered_device *rdev)
8199 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
8200 struct nlattr *nl_pats, *nl_pat;
8201 int i, pat_len;
8203 if (!wowlan->n_patterns)
8204 return 0;
8206 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8207 if (!nl_pats)
8208 return -ENOBUFS;
8210 for (i = 0; i < wowlan->n_patterns; i++) {
8211 nl_pat = nla_nest_start(msg, i + 1);
8212 if (!nl_pat)
8213 return -ENOBUFS;
8214 pat_len = wowlan->patterns[i].pattern_len;
8215 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
8216 wowlan->patterns[i].mask) ||
8217 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8218 wowlan->patterns[i].pattern) ||
8219 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8220 wowlan->patterns[i].pkt_offset))
8221 return -ENOBUFS;
8222 nla_nest_end(msg, nl_pat);
8224 nla_nest_end(msg, nl_pats);
8226 return 0;
8229 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8230 struct cfg80211_wowlan_tcp *tcp)
8232 struct nlattr *nl_tcp;
8234 if (!tcp)
8235 return 0;
8237 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8238 if (!nl_tcp)
8239 return -ENOBUFS;
8241 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8242 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8243 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8244 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8245 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8246 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8247 tcp->payload_len, tcp->payload) ||
8248 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8249 tcp->data_interval) ||
8250 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8251 tcp->wake_len, tcp->wake_data) ||
8252 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8253 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8254 return -ENOBUFS;
8256 if (tcp->payload_seq.len &&
8257 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8258 sizeof(tcp->payload_seq), &tcp->payload_seq))
8259 return -ENOBUFS;
8261 if (tcp->payload_tok.len &&
8262 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8263 sizeof(tcp->payload_tok) + tcp->tokens_size,
8264 &tcp->payload_tok))
8265 return -ENOBUFS;
8267 nla_nest_end(msg, nl_tcp);
8269 return 0;
8272 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
8274 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8275 struct sk_buff *msg;
8276 void *hdr;
8277 u32 size = NLMSG_DEFAULT_SIZE;
8279 if (!rdev->wiphy.wowlan)
8280 return -EOPNOTSUPP;
8282 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
8283 /* adjust size to have room for all the data */
8284 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
8285 rdev->wiphy.wowlan_config->tcp->payload_len +
8286 rdev->wiphy.wowlan_config->tcp->wake_len +
8287 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
8290 msg = nlmsg_new(size, GFP_KERNEL);
8291 if (!msg)
8292 return -ENOMEM;
8294 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8295 NL80211_CMD_GET_WOWLAN);
8296 if (!hdr)
8297 goto nla_put_failure;
8299 if (rdev->wiphy.wowlan_config) {
8300 struct nlattr *nl_wowlan;
8302 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8303 if (!nl_wowlan)
8304 goto nla_put_failure;
8306 if ((rdev->wiphy.wowlan_config->any &&
8307 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8308 (rdev->wiphy.wowlan_config->disconnect &&
8309 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8310 (rdev->wiphy.wowlan_config->magic_pkt &&
8311 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8312 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
8313 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8314 (rdev->wiphy.wowlan_config->eap_identity_req &&
8315 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8316 (rdev->wiphy.wowlan_config->four_way_handshake &&
8317 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8318 (rdev->wiphy.wowlan_config->rfkill_release &&
8319 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
8320 goto nla_put_failure;
8322 if (nl80211_send_wowlan_patterns(msg, rdev))
8323 goto nla_put_failure;
8325 if (nl80211_send_wowlan_tcp(msg,
8326 rdev->wiphy.wowlan_config->tcp))
8327 goto nla_put_failure;
8329 nla_nest_end(msg, nl_wowlan);
8332 genlmsg_end(msg, hdr);
8333 return genlmsg_reply(msg, info);
8335 nla_put_failure:
8336 nlmsg_free(msg);
8337 return -ENOBUFS;
8340 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8341 struct nlattr *attr,
8342 struct cfg80211_wowlan *trig)
8344 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
8345 struct cfg80211_wowlan_tcp *cfg;
8346 struct nl80211_wowlan_tcp_data_token *tok = NULL;
8347 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
8348 u32 size;
8349 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
8350 int err, port;
8352 if (!rdev->wiphy.wowlan->tcp)
8353 return -EINVAL;
8355 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
8356 nla_data(attr), nla_len(attr),
8357 nl80211_wowlan_tcp_policy);
8358 if (err)
8359 return err;
8361 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8362 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8363 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8364 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8365 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8366 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8367 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8368 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8369 return -EINVAL;
8371 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
8372 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
8373 return -EINVAL;
8375 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
8376 rdev->wiphy.wowlan->tcp->data_interval_max ||
8377 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
8378 return -EINVAL;
8380 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
8381 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
8382 return -EINVAL;
8384 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8385 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8386 return -EINVAL;
8388 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8389 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8391 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8392 tokens_size = tokln - sizeof(*tok);
8394 if (!tok->len || tokens_size % tok->len)
8395 return -EINVAL;
8396 if (!rdev->wiphy.wowlan->tcp->tok)
8397 return -EINVAL;
8398 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
8399 return -EINVAL;
8400 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
8401 return -EINVAL;
8402 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
8403 return -EINVAL;
8404 if (tok->offset + tok->len > data_size)
8405 return -EINVAL;
8408 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8409 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
8410 if (!rdev->wiphy.wowlan->tcp->seq)
8411 return -EINVAL;
8412 if (seq->len == 0 || seq->len > 4)
8413 return -EINVAL;
8414 if (seq->len + seq->offset > data_size)
8415 return -EINVAL;
8418 size = sizeof(*cfg);
8419 size += data_size;
8420 size += wake_size + wake_mask_size;
8421 size += tokens_size;
8423 cfg = kzalloc(size, GFP_KERNEL);
8424 if (!cfg)
8425 return -ENOMEM;
8426 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8427 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8428 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8429 ETH_ALEN);
8430 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8431 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8432 else
8433 port = 0;
8434 #ifdef CONFIG_INET
8435 /* allocate a socket and port for it and use it */
8436 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8437 IPPROTO_TCP, &cfg->sock, 1);
8438 if (err) {
8439 kfree(cfg);
8440 return err;
8442 if (inet_csk_get_port(cfg->sock->sk, port)) {
8443 sock_release(cfg->sock);
8444 kfree(cfg);
8445 return -EADDRINUSE;
8447 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8448 #else
8449 if (!port) {
8450 kfree(cfg);
8451 return -EINVAL;
8453 cfg->src_port = port;
8454 #endif
8456 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8457 cfg->payload_len = data_size;
8458 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8459 memcpy((void *)cfg->payload,
8460 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8461 data_size);
8462 if (seq)
8463 cfg->payload_seq = *seq;
8464 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8465 cfg->wake_len = wake_size;
8466 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8467 memcpy((void *)cfg->wake_data,
8468 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8469 wake_size);
8470 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8471 data_size + wake_size;
8472 memcpy((void *)cfg->wake_mask,
8473 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8474 wake_mask_size);
8475 if (tok) {
8476 cfg->tokens_size = tokens_size;
8477 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8480 trig->tcp = cfg;
8482 return 0;
8485 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8488 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
8489 struct cfg80211_wowlan new_triggers = {};
8490 struct cfg80211_wowlan *ntrig;
8491 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
8492 int err, i;
8493 bool prev_enabled = rdev->wiphy.wowlan_config;
8495 if (!wowlan)
8496 return -EOPNOTSUPP;
8498 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8499 cfg80211_rdev_free_wowlan(rdev);
8500 rdev->wiphy.wowlan_config = NULL;
8501 goto set_wakeup;
8504 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8505 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8506 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8507 nl80211_wowlan_policy);
8508 if (err)
8509 return err;
8511 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8512 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8513 return -EINVAL;
8514 new_triggers.any = true;
8517 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8518 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8519 return -EINVAL;
8520 new_triggers.disconnect = true;
8523 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8524 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8525 return -EINVAL;
8526 new_triggers.magic_pkt = true;
8529 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8530 return -EINVAL;
8532 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8533 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8534 return -EINVAL;
8535 new_triggers.gtk_rekey_failure = true;
8538 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8539 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8540 return -EINVAL;
8541 new_triggers.eap_identity_req = true;
8544 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8545 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8546 return -EINVAL;
8547 new_triggers.four_way_handshake = true;
8550 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8551 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8552 return -EINVAL;
8553 new_triggers.rfkill_release = true;
8556 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8557 struct nlattr *pat;
8558 int n_patterns = 0;
8559 int rem, pat_len, mask_len, pkt_offset;
8560 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8562 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8563 rem)
8564 n_patterns++;
8565 if (n_patterns > wowlan->n_patterns)
8566 return -EINVAL;
8568 new_triggers.patterns = kcalloc(n_patterns,
8569 sizeof(new_triggers.patterns[0]),
8570 GFP_KERNEL);
8571 if (!new_triggers.patterns)
8572 return -ENOMEM;
8574 new_triggers.n_patterns = n_patterns;
8575 i = 0;
8577 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8578 rem) {
8579 u8 *mask_pat;
8581 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8582 nla_len(pat), NULL);
8583 err = -EINVAL;
8584 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8585 !pat_tb[NL80211_PKTPAT_PATTERN])
8586 goto error;
8587 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8588 mask_len = DIV_ROUND_UP(pat_len, 8);
8589 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8590 goto error;
8591 if (pat_len > wowlan->pattern_max_len ||
8592 pat_len < wowlan->pattern_min_len)
8593 goto error;
8595 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8596 pkt_offset = 0;
8597 else
8598 pkt_offset = nla_get_u32(
8599 pat_tb[NL80211_PKTPAT_OFFSET]);
8600 if (pkt_offset > wowlan->max_pkt_offset)
8601 goto error;
8602 new_triggers.patterns[i].pkt_offset = pkt_offset;
8604 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
8605 if (!mask_pat) {
8606 err = -ENOMEM;
8607 goto error;
8609 new_triggers.patterns[i].mask = mask_pat;
8610 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
8611 mask_len);
8612 mask_pat += mask_len;
8613 new_triggers.patterns[i].pattern = mask_pat;
8614 new_triggers.patterns[i].pattern_len = pat_len;
8615 memcpy(mask_pat,
8616 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
8617 pat_len);
8618 i++;
8622 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8623 err = nl80211_parse_wowlan_tcp(
8624 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8625 &new_triggers);
8626 if (err)
8627 goto error;
8630 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8631 if (!ntrig) {
8632 err = -ENOMEM;
8633 goto error;
8635 cfg80211_rdev_free_wowlan(rdev);
8636 rdev->wiphy.wowlan_config = ntrig;
8638 set_wakeup:
8639 if (rdev->ops->set_wakeup &&
8640 prev_enabled != !!rdev->wiphy.wowlan_config)
8641 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
8643 return 0;
8644 error:
8645 for (i = 0; i < new_triggers.n_patterns; i++)
8646 kfree(new_triggers.patterns[i].mask);
8647 kfree(new_triggers.patterns);
8648 if (new_triggers.tcp && new_triggers.tcp->sock)
8649 sock_release(new_triggers.tcp->sock);
8650 kfree(new_triggers.tcp);
8651 return err;
8653 #endif
8655 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8656 struct cfg80211_registered_device *rdev)
8658 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8659 int i, j, pat_len;
8660 struct cfg80211_coalesce_rules *rule;
8662 if (!rdev->coalesce->n_rules)
8663 return 0;
8665 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8666 if (!nl_rules)
8667 return -ENOBUFS;
8669 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8670 nl_rule = nla_nest_start(msg, i + 1);
8671 if (!nl_rule)
8672 return -ENOBUFS;
8674 rule = &rdev->coalesce->rules[i];
8675 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8676 rule->delay))
8677 return -ENOBUFS;
8679 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8680 rule->condition))
8681 return -ENOBUFS;
8683 nl_pats = nla_nest_start(msg,
8684 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8685 if (!nl_pats)
8686 return -ENOBUFS;
8688 for (j = 0; j < rule->n_patterns; j++) {
8689 nl_pat = nla_nest_start(msg, j + 1);
8690 if (!nl_pat)
8691 return -ENOBUFS;
8692 pat_len = rule->patterns[j].pattern_len;
8693 if (nla_put(msg, NL80211_PKTPAT_MASK,
8694 DIV_ROUND_UP(pat_len, 8),
8695 rule->patterns[j].mask) ||
8696 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8697 rule->patterns[j].pattern) ||
8698 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8699 rule->patterns[j].pkt_offset))
8700 return -ENOBUFS;
8701 nla_nest_end(msg, nl_pat);
8703 nla_nest_end(msg, nl_pats);
8704 nla_nest_end(msg, nl_rule);
8706 nla_nest_end(msg, nl_rules);
8708 return 0;
8711 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8713 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8714 struct sk_buff *msg;
8715 void *hdr;
8717 if (!rdev->wiphy.coalesce)
8718 return -EOPNOTSUPP;
8720 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8721 if (!msg)
8722 return -ENOMEM;
8724 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8725 NL80211_CMD_GET_COALESCE);
8726 if (!hdr)
8727 goto nla_put_failure;
8729 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8730 goto nla_put_failure;
8732 genlmsg_end(msg, hdr);
8733 return genlmsg_reply(msg, info);
8735 nla_put_failure:
8736 nlmsg_free(msg);
8737 return -ENOBUFS;
8740 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8742 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8743 int i, j;
8744 struct cfg80211_coalesce_rules *rule;
8746 if (!coalesce)
8747 return;
8749 for (i = 0; i < coalesce->n_rules; i++) {
8750 rule = &coalesce->rules[i];
8751 for (j = 0; j < rule->n_patterns; j++)
8752 kfree(rule->patterns[j].mask);
8753 kfree(rule->patterns);
8755 kfree(coalesce->rules);
8756 kfree(coalesce);
8757 rdev->coalesce = NULL;
8760 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8761 struct nlattr *rule,
8762 struct cfg80211_coalesce_rules *new_rule)
8764 int err, i;
8765 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8766 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8767 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8768 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8770 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8771 nla_len(rule), nl80211_coalesce_policy);
8772 if (err)
8773 return err;
8775 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8776 new_rule->delay =
8777 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8778 if (new_rule->delay > coalesce->max_delay)
8779 return -EINVAL;
8781 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8782 new_rule->condition =
8783 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8784 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8785 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8786 return -EINVAL;
8788 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8789 return -EINVAL;
8791 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8792 rem)
8793 n_patterns++;
8794 if (n_patterns > coalesce->n_patterns)
8795 return -EINVAL;
8797 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8798 GFP_KERNEL);
8799 if (!new_rule->patterns)
8800 return -ENOMEM;
8802 new_rule->n_patterns = n_patterns;
8803 i = 0;
8805 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8806 rem) {
8807 u8 *mask_pat;
8809 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8810 nla_len(pat), NULL);
8811 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8812 !pat_tb[NL80211_PKTPAT_PATTERN])
8813 return -EINVAL;
8814 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8815 mask_len = DIV_ROUND_UP(pat_len, 8);
8816 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8817 return -EINVAL;
8818 if (pat_len > coalesce->pattern_max_len ||
8819 pat_len < coalesce->pattern_min_len)
8820 return -EINVAL;
8822 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8823 pkt_offset = 0;
8824 else
8825 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8826 if (pkt_offset > coalesce->max_pkt_offset)
8827 return -EINVAL;
8828 new_rule->patterns[i].pkt_offset = pkt_offset;
8830 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
8831 if (!mask_pat)
8832 return -ENOMEM;
8834 new_rule->patterns[i].mask = mask_pat;
8835 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
8836 mask_len);
8838 mask_pat += mask_len;
8839 new_rule->patterns[i].pattern = mask_pat;
8840 new_rule->patterns[i].pattern_len = pat_len;
8841 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
8842 pat_len);
8843 i++;
8846 return 0;
8849 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8852 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8853 struct cfg80211_coalesce new_coalesce = {};
8854 struct cfg80211_coalesce *n_coalesce;
8855 int err, rem_rule, n_rules = 0, i, j;
8856 struct nlattr *rule;
8857 struct cfg80211_coalesce_rules *tmp_rule;
8859 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8860 return -EOPNOTSUPP;
8862 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8863 cfg80211_rdev_free_coalesce(rdev);
8864 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8865 return 0;
8868 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8869 rem_rule)
8870 n_rules++;
8871 if (n_rules > coalesce->n_rules)
8872 return -EINVAL;
8874 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8875 GFP_KERNEL);
8876 if (!new_coalesce.rules)
8877 return -ENOMEM;
8879 new_coalesce.n_rules = n_rules;
8880 i = 0;
8882 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8883 rem_rule) {
8884 err = nl80211_parse_coalesce_rule(rdev, rule,
8885 &new_coalesce.rules[i]);
8886 if (err)
8887 goto error;
8889 i++;
8892 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8893 if (err)
8894 goto error;
8896 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8897 if (!n_coalesce) {
8898 err = -ENOMEM;
8899 goto error;
8901 cfg80211_rdev_free_coalesce(rdev);
8902 rdev->coalesce = n_coalesce;
8904 return 0;
8905 error:
8906 for (i = 0; i < new_coalesce.n_rules; i++) {
8907 tmp_rule = &new_coalesce.rules[i];
8908 for (j = 0; j < tmp_rule->n_patterns; j++)
8909 kfree(tmp_rule->patterns[j].mask);
8910 kfree(tmp_rule->patterns);
8912 kfree(new_coalesce.rules);
8914 return err;
8917 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8920 struct net_device *dev = info->user_ptr[1];
8921 struct wireless_dev *wdev = dev->ieee80211_ptr;
8922 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8923 struct cfg80211_gtk_rekey_data rekey_data;
8924 int err;
8926 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8927 return -EINVAL;
8929 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8930 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8931 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8932 nl80211_rekey_policy);
8933 if (err)
8934 return err;
8936 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8937 return -ERANGE;
8938 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8939 return -ERANGE;
8940 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8941 return -ERANGE;
8943 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8944 NL80211_KEK_LEN);
8945 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8946 NL80211_KCK_LEN);
8947 memcpy(rekey_data.replay_ctr,
8948 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8949 NL80211_REPLAY_CTR_LEN);
8951 wdev_lock(wdev);
8952 if (!wdev->current_bss) {
8953 err = -ENOTCONN;
8954 goto out;
8957 if (!rdev->ops->set_rekey_data) {
8958 err = -EOPNOTSUPP;
8959 goto out;
8962 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
8963 out:
8964 wdev_unlock(wdev);
8965 return err;
8968 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8969 struct genl_info *info)
8971 struct net_device *dev = info->user_ptr[1];
8972 struct wireless_dev *wdev = dev->ieee80211_ptr;
8974 if (wdev->iftype != NL80211_IFTYPE_AP &&
8975 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8976 return -EINVAL;
8978 if (wdev->ap_unexpected_nlportid)
8979 return -EBUSY;
8981 wdev->ap_unexpected_nlportid = info->snd_portid;
8982 return 0;
8985 static int nl80211_probe_client(struct sk_buff *skb,
8986 struct genl_info *info)
8988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8989 struct net_device *dev = info->user_ptr[1];
8990 struct wireless_dev *wdev = dev->ieee80211_ptr;
8991 struct sk_buff *msg;
8992 void *hdr;
8993 const u8 *addr;
8994 u64 cookie;
8995 int err;
8997 if (wdev->iftype != NL80211_IFTYPE_AP &&
8998 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8999 return -EOPNOTSUPP;
9001 if (!info->attrs[NL80211_ATTR_MAC])
9002 return -EINVAL;
9004 if (!rdev->ops->probe_client)
9005 return -EOPNOTSUPP;
9007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9008 if (!msg)
9009 return -ENOMEM;
9011 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9012 NL80211_CMD_PROBE_CLIENT);
9013 if (!hdr) {
9014 err = -ENOBUFS;
9015 goto free_msg;
9018 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9020 err = rdev_probe_client(rdev, dev, addr, &cookie);
9021 if (err)
9022 goto free_msg;
9024 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9025 goto nla_put_failure;
9027 genlmsg_end(msg, hdr);
9029 return genlmsg_reply(msg, info);
9031 nla_put_failure:
9032 err = -ENOBUFS;
9033 free_msg:
9034 nlmsg_free(msg);
9035 return err;
9038 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
9040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9041 struct cfg80211_beacon_registration *reg, *nreg;
9042 int rv;
9044 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
9045 return -EOPNOTSUPP;
9047 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
9048 if (!nreg)
9049 return -ENOMEM;
9051 /* First, check if already registered. */
9052 spin_lock_bh(&rdev->beacon_registrations_lock);
9053 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9054 if (reg->nlportid == info->snd_portid) {
9055 rv = -EALREADY;
9056 goto out_err;
9059 /* Add it to the list */
9060 nreg->nlportid = info->snd_portid;
9061 list_add(&nreg->list, &rdev->beacon_registrations);
9063 spin_unlock_bh(&rdev->beacon_registrations_lock);
9065 return 0;
9066 out_err:
9067 spin_unlock_bh(&rdev->beacon_registrations_lock);
9068 kfree(nreg);
9069 return rv;
9072 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
9074 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9075 struct wireless_dev *wdev = info->user_ptr[1];
9076 int err;
9078 if (!rdev->ops->start_p2p_device)
9079 return -EOPNOTSUPP;
9081 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9082 return -EOPNOTSUPP;
9084 if (wdev->p2p_started)
9085 return 0;
9087 if (rfkill_blocked(rdev->rfkill))
9088 return -ERFKILL;
9090 err = rdev_start_p2p_device(rdev, wdev);
9091 if (err)
9092 return err;
9094 wdev->p2p_started = true;
9095 rdev->opencount++;
9097 return 0;
9100 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9103 struct wireless_dev *wdev = info->user_ptr[1];
9105 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9106 return -EOPNOTSUPP;
9108 if (!rdev->ops->stop_p2p_device)
9109 return -EOPNOTSUPP;
9111 cfg80211_stop_p2p_device(rdev, wdev);
9113 return 0;
9116 static int nl80211_get_protocol_features(struct sk_buff *skb,
9117 struct genl_info *info)
9119 void *hdr;
9120 struct sk_buff *msg;
9122 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9123 if (!msg)
9124 return -ENOMEM;
9126 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9127 NL80211_CMD_GET_PROTOCOL_FEATURES);
9128 if (!hdr)
9129 goto nla_put_failure;
9131 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9132 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9133 goto nla_put_failure;
9135 genlmsg_end(msg, hdr);
9136 return genlmsg_reply(msg, info);
9138 nla_put_failure:
9139 kfree_skb(msg);
9140 return -ENOBUFS;
9143 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
9145 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9146 struct cfg80211_update_ft_ies_params ft_params;
9147 struct net_device *dev = info->user_ptr[1];
9149 if (!rdev->ops->update_ft_ies)
9150 return -EOPNOTSUPP;
9152 if (!info->attrs[NL80211_ATTR_MDID] ||
9153 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9154 return -EINVAL;
9156 memset(&ft_params, 0, sizeof(ft_params));
9157 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
9158 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9159 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9161 return rdev_update_ft_ies(rdev, dev, &ft_params);
9164 static int nl80211_crit_protocol_start(struct sk_buff *skb,
9165 struct genl_info *info)
9167 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9168 struct wireless_dev *wdev = info->user_ptr[1];
9169 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
9170 u16 duration;
9171 int ret;
9173 if (!rdev->ops->crit_proto_start)
9174 return -EOPNOTSUPP;
9176 if (WARN_ON(!rdev->ops->crit_proto_stop))
9177 return -EINVAL;
9179 if (rdev->crit_proto_nlportid)
9180 return -EBUSY;
9182 /* determine protocol if provided */
9183 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
9184 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
9186 if (proto >= NUM_NL80211_CRIT_PROTO)
9187 return -EINVAL;
9189 /* timeout must be provided */
9190 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
9191 return -EINVAL;
9193 duration =
9194 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
9196 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
9197 return -ERANGE;
9199 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
9200 if (!ret)
9201 rdev->crit_proto_nlportid = info->snd_portid;
9203 return ret;
9206 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
9207 struct genl_info *info)
9209 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9210 struct wireless_dev *wdev = info->user_ptr[1];
9212 if (!rdev->ops->crit_proto_stop)
9213 return -EOPNOTSUPP;
9215 if (rdev->crit_proto_nlportid) {
9216 rdev->crit_proto_nlportid = 0;
9217 rdev_crit_proto_stop(rdev, wdev);
9219 return 0;
9222 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
9224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9225 struct wireless_dev *wdev =
9226 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9227 int i, err;
9228 u32 vid, subcmd;
9230 if (!rdev->wiphy.vendor_commands)
9231 return -EOPNOTSUPP;
9233 if (IS_ERR(wdev)) {
9234 err = PTR_ERR(wdev);
9235 if (err != -EINVAL)
9236 return err;
9237 wdev = NULL;
9238 } else if (wdev->wiphy != &rdev->wiphy) {
9239 return -EINVAL;
9242 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
9243 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
9244 return -EINVAL;
9246 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
9247 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
9248 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
9249 const struct wiphy_vendor_command *vcmd;
9250 void *data = NULL;
9251 int len = 0;
9253 vcmd = &rdev->wiphy.vendor_commands[i];
9255 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
9256 continue;
9258 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
9259 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
9260 if (!wdev)
9261 return -EINVAL;
9262 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
9263 !wdev->netdev)
9264 return -EINVAL;
9266 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
9267 if (wdev->netdev &&
9268 !netif_running(wdev->netdev))
9269 return -ENETDOWN;
9270 if (!wdev->netdev && !wdev->p2p_started)
9271 return -ENETDOWN;
9273 } else {
9274 wdev = NULL;
9277 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
9278 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9279 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
9282 rdev->cur_cmd_info = info;
9283 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
9284 data, len);
9285 rdev->cur_cmd_info = NULL;
9286 return err;
9289 return -EOPNOTSUPP;
9292 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9293 enum nl80211_commands cmd,
9294 enum nl80211_attrs attr,
9295 int approxlen)
9297 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9299 if (WARN_ON(!rdev->cur_cmd_info))
9300 return NULL;
9302 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
9303 rdev->cur_cmd_info->snd_portid,
9304 rdev->cur_cmd_info->snd_seq,
9305 cmd, attr, NULL, GFP_KERNEL);
9307 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
9309 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
9311 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9312 void *hdr = ((void **)skb->cb)[1];
9313 struct nlattr *data = ((void **)skb->cb)[2];
9315 /* clear CB data for netlink core to own from now on */
9316 memset(skb->cb, 0, sizeof(skb->cb));
9318 if (WARN_ON(!rdev->cur_cmd_info)) {
9319 kfree_skb(skb);
9320 return -EINVAL;
9323 nla_nest_end(skb, data);
9324 genlmsg_end(skb, hdr);
9325 return genlmsg_reply(skb, rdev->cur_cmd_info);
9327 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
9330 static int nl80211_set_qos_map(struct sk_buff *skb,
9331 struct genl_info *info)
9333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9334 struct cfg80211_qos_map *qos_map = NULL;
9335 struct net_device *dev = info->user_ptr[1];
9336 u8 *pos, len, num_des, des_len, des;
9337 int ret;
9339 if (!rdev->ops->set_qos_map)
9340 return -EOPNOTSUPP;
9342 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
9343 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
9344 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
9346 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
9347 len > IEEE80211_QOS_MAP_LEN_MAX)
9348 return -EINVAL;
9350 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
9351 if (!qos_map)
9352 return -ENOMEM;
9354 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
9355 if (num_des) {
9356 des_len = num_des *
9357 sizeof(struct cfg80211_dscp_exception);
9358 memcpy(qos_map->dscp_exception, pos, des_len);
9359 qos_map->num_des = num_des;
9360 for (des = 0; des < num_des; des++) {
9361 if (qos_map->dscp_exception[des].up > 7) {
9362 kfree(qos_map);
9363 return -EINVAL;
9366 pos += des_len;
9368 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
9371 wdev_lock(dev->ieee80211_ptr);
9372 ret = nl80211_key_allowed(dev->ieee80211_ptr);
9373 if (!ret)
9374 ret = rdev_set_qos_map(rdev, dev, qos_map);
9375 wdev_unlock(dev->ieee80211_ptr);
9377 kfree(qos_map);
9378 return ret;
9381 #define NL80211_FLAG_NEED_WIPHY 0x01
9382 #define NL80211_FLAG_NEED_NETDEV 0x02
9383 #define NL80211_FLAG_NEED_RTNL 0x04
9384 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
9385 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
9386 NL80211_FLAG_CHECK_NETDEV_UP)
9387 #define NL80211_FLAG_NEED_WDEV 0x10
9388 /* If a netdev is associated, it must be UP, P2P must be started */
9389 #define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
9390 NL80211_FLAG_CHECK_NETDEV_UP)
9392 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
9393 struct genl_info *info)
9395 struct cfg80211_registered_device *rdev;
9396 struct wireless_dev *wdev;
9397 struct net_device *dev;
9398 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
9400 if (rtnl)
9401 rtnl_lock();
9403 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
9404 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
9405 if (IS_ERR(rdev)) {
9406 if (rtnl)
9407 rtnl_unlock();
9408 return PTR_ERR(rdev);
9410 info->user_ptr[0] = rdev;
9411 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
9412 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9413 ASSERT_RTNL();
9415 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9416 info->attrs);
9417 if (IS_ERR(wdev)) {
9418 if (rtnl)
9419 rtnl_unlock();
9420 return PTR_ERR(wdev);
9423 dev = wdev->netdev;
9424 rdev = wiphy_to_rdev(wdev->wiphy);
9426 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9427 if (!dev) {
9428 if (rtnl)
9429 rtnl_unlock();
9430 return -EINVAL;
9433 info->user_ptr[1] = dev;
9434 } else {
9435 info->user_ptr[1] = wdev;
9438 if (dev) {
9439 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9440 !netif_running(dev)) {
9441 if (rtnl)
9442 rtnl_unlock();
9443 return -ENETDOWN;
9446 dev_hold(dev);
9447 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9448 if (!wdev->p2p_started) {
9449 if (rtnl)
9450 rtnl_unlock();
9451 return -ENETDOWN;
9455 info->user_ptr[0] = rdev;
9458 return 0;
9461 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
9462 struct genl_info *info)
9464 if (info->user_ptr[1]) {
9465 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9466 struct wireless_dev *wdev = info->user_ptr[1];
9468 if (wdev->netdev)
9469 dev_put(wdev->netdev);
9470 } else {
9471 dev_put(info->user_ptr[1]);
9474 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9475 rtnl_unlock();
9478 static const struct genl_ops nl80211_ops[] = {
9480 .cmd = NL80211_CMD_GET_WIPHY,
9481 .doit = nl80211_get_wiphy,
9482 .dumpit = nl80211_dump_wiphy,
9483 .done = nl80211_dump_wiphy_done,
9484 .policy = nl80211_policy,
9485 /* can be retrieved by unprivileged users */
9486 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9487 NL80211_FLAG_NEED_RTNL,
9490 .cmd = NL80211_CMD_SET_WIPHY,
9491 .doit = nl80211_set_wiphy,
9492 .policy = nl80211_policy,
9493 .flags = GENL_ADMIN_PERM,
9494 .internal_flags = NL80211_FLAG_NEED_RTNL,
9497 .cmd = NL80211_CMD_GET_INTERFACE,
9498 .doit = nl80211_get_interface,
9499 .dumpit = nl80211_dump_interface,
9500 .policy = nl80211_policy,
9501 /* can be retrieved by unprivileged users */
9502 .internal_flags = NL80211_FLAG_NEED_WDEV |
9503 NL80211_FLAG_NEED_RTNL,
9506 .cmd = NL80211_CMD_SET_INTERFACE,
9507 .doit = nl80211_set_interface,
9508 .policy = nl80211_policy,
9509 .flags = GENL_ADMIN_PERM,
9510 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9511 NL80211_FLAG_NEED_RTNL,
9514 .cmd = NL80211_CMD_NEW_INTERFACE,
9515 .doit = nl80211_new_interface,
9516 .policy = nl80211_policy,
9517 .flags = GENL_ADMIN_PERM,
9518 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9519 NL80211_FLAG_NEED_RTNL,
9522 .cmd = NL80211_CMD_DEL_INTERFACE,
9523 .doit = nl80211_del_interface,
9524 .policy = nl80211_policy,
9525 .flags = GENL_ADMIN_PERM,
9526 .internal_flags = NL80211_FLAG_NEED_WDEV |
9527 NL80211_FLAG_NEED_RTNL,
9530 .cmd = NL80211_CMD_GET_KEY,
9531 .doit = nl80211_get_key,
9532 .policy = nl80211_policy,
9533 .flags = GENL_ADMIN_PERM,
9534 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9535 NL80211_FLAG_NEED_RTNL,
9538 .cmd = NL80211_CMD_SET_KEY,
9539 .doit = nl80211_set_key,
9540 .policy = nl80211_policy,
9541 .flags = GENL_ADMIN_PERM,
9542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9543 NL80211_FLAG_NEED_RTNL,
9546 .cmd = NL80211_CMD_NEW_KEY,
9547 .doit = nl80211_new_key,
9548 .policy = nl80211_policy,
9549 .flags = GENL_ADMIN_PERM,
9550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9551 NL80211_FLAG_NEED_RTNL,
9554 .cmd = NL80211_CMD_DEL_KEY,
9555 .doit = nl80211_del_key,
9556 .policy = nl80211_policy,
9557 .flags = GENL_ADMIN_PERM,
9558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9559 NL80211_FLAG_NEED_RTNL,
9562 .cmd = NL80211_CMD_SET_BEACON,
9563 .policy = nl80211_policy,
9564 .flags = GENL_ADMIN_PERM,
9565 .doit = nl80211_set_beacon,
9566 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9567 NL80211_FLAG_NEED_RTNL,
9570 .cmd = NL80211_CMD_START_AP,
9571 .policy = nl80211_policy,
9572 .flags = GENL_ADMIN_PERM,
9573 .doit = nl80211_start_ap,
9574 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9575 NL80211_FLAG_NEED_RTNL,
9578 .cmd = NL80211_CMD_STOP_AP,
9579 .policy = nl80211_policy,
9580 .flags = GENL_ADMIN_PERM,
9581 .doit = nl80211_stop_ap,
9582 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9583 NL80211_FLAG_NEED_RTNL,
9586 .cmd = NL80211_CMD_GET_STATION,
9587 .doit = nl80211_get_station,
9588 .dumpit = nl80211_dump_station,
9589 .policy = nl80211_policy,
9590 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9591 NL80211_FLAG_NEED_RTNL,
9594 .cmd = NL80211_CMD_SET_STATION,
9595 .doit = nl80211_set_station,
9596 .policy = nl80211_policy,
9597 .flags = GENL_ADMIN_PERM,
9598 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9599 NL80211_FLAG_NEED_RTNL,
9602 .cmd = NL80211_CMD_NEW_STATION,
9603 .doit = nl80211_new_station,
9604 .policy = nl80211_policy,
9605 .flags = GENL_ADMIN_PERM,
9606 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9607 NL80211_FLAG_NEED_RTNL,
9610 .cmd = NL80211_CMD_DEL_STATION,
9611 .doit = nl80211_del_station,
9612 .policy = nl80211_policy,
9613 .flags = GENL_ADMIN_PERM,
9614 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9615 NL80211_FLAG_NEED_RTNL,
9618 .cmd = NL80211_CMD_GET_MPATH,
9619 .doit = nl80211_get_mpath,
9620 .dumpit = nl80211_dump_mpath,
9621 .policy = nl80211_policy,
9622 .flags = GENL_ADMIN_PERM,
9623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9624 NL80211_FLAG_NEED_RTNL,
9627 .cmd = NL80211_CMD_SET_MPATH,
9628 .doit = nl80211_set_mpath,
9629 .policy = nl80211_policy,
9630 .flags = GENL_ADMIN_PERM,
9631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9632 NL80211_FLAG_NEED_RTNL,
9635 .cmd = NL80211_CMD_NEW_MPATH,
9636 .doit = nl80211_new_mpath,
9637 .policy = nl80211_policy,
9638 .flags = GENL_ADMIN_PERM,
9639 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9640 NL80211_FLAG_NEED_RTNL,
9643 .cmd = NL80211_CMD_DEL_MPATH,
9644 .doit = nl80211_del_mpath,
9645 .policy = nl80211_policy,
9646 .flags = GENL_ADMIN_PERM,
9647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9648 NL80211_FLAG_NEED_RTNL,
9651 .cmd = NL80211_CMD_SET_BSS,
9652 .doit = nl80211_set_bss,
9653 .policy = nl80211_policy,
9654 .flags = GENL_ADMIN_PERM,
9655 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9656 NL80211_FLAG_NEED_RTNL,
9659 .cmd = NL80211_CMD_GET_REG,
9660 .doit = nl80211_get_reg,
9661 .policy = nl80211_policy,
9662 .internal_flags = NL80211_FLAG_NEED_RTNL,
9663 /* can be retrieved by unprivileged users */
9666 .cmd = NL80211_CMD_SET_REG,
9667 .doit = nl80211_set_reg,
9668 .policy = nl80211_policy,
9669 .flags = GENL_ADMIN_PERM,
9670 .internal_flags = NL80211_FLAG_NEED_RTNL,
9673 .cmd = NL80211_CMD_REQ_SET_REG,
9674 .doit = nl80211_req_set_reg,
9675 .policy = nl80211_policy,
9676 .flags = GENL_ADMIN_PERM,
9679 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9680 .doit = nl80211_get_mesh_config,
9681 .policy = nl80211_policy,
9682 /* can be retrieved by unprivileged users */
9683 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9684 NL80211_FLAG_NEED_RTNL,
9687 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9688 .doit = nl80211_update_mesh_config,
9689 .policy = nl80211_policy,
9690 .flags = GENL_ADMIN_PERM,
9691 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9692 NL80211_FLAG_NEED_RTNL,
9695 .cmd = NL80211_CMD_TRIGGER_SCAN,
9696 .doit = nl80211_trigger_scan,
9697 .policy = nl80211_policy,
9698 .flags = GENL_ADMIN_PERM,
9699 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9700 NL80211_FLAG_NEED_RTNL,
9703 .cmd = NL80211_CMD_GET_SCAN,
9704 .policy = nl80211_policy,
9705 .dumpit = nl80211_dump_scan,
9708 .cmd = NL80211_CMD_START_SCHED_SCAN,
9709 .doit = nl80211_start_sched_scan,
9710 .policy = nl80211_policy,
9711 .flags = GENL_ADMIN_PERM,
9712 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9713 NL80211_FLAG_NEED_RTNL,
9716 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9717 .doit = nl80211_stop_sched_scan,
9718 .policy = nl80211_policy,
9719 .flags = GENL_ADMIN_PERM,
9720 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9721 NL80211_FLAG_NEED_RTNL,
9724 .cmd = NL80211_CMD_AUTHENTICATE,
9725 .doit = nl80211_authenticate,
9726 .policy = nl80211_policy,
9727 .flags = GENL_ADMIN_PERM,
9728 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9729 NL80211_FLAG_NEED_RTNL,
9732 .cmd = NL80211_CMD_ASSOCIATE,
9733 .doit = nl80211_associate,
9734 .policy = nl80211_policy,
9735 .flags = GENL_ADMIN_PERM,
9736 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9737 NL80211_FLAG_NEED_RTNL,
9740 .cmd = NL80211_CMD_DEAUTHENTICATE,
9741 .doit = nl80211_deauthenticate,
9742 .policy = nl80211_policy,
9743 .flags = GENL_ADMIN_PERM,
9744 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9745 NL80211_FLAG_NEED_RTNL,
9748 .cmd = NL80211_CMD_DISASSOCIATE,
9749 .doit = nl80211_disassociate,
9750 .policy = nl80211_policy,
9751 .flags = GENL_ADMIN_PERM,
9752 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9753 NL80211_FLAG_NEED_RTNL,
9756 .cmd = NL80211_CMD_JOIN_IBSS,
9757 .doit = nl80211_join_ibss,
9758 .policy = nl80211_policy,
9759 .flags = GENL_ADMIN_PERM,
9760 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9761 NL80211_FLAG_NEED_RTNL,
9764 .cmd = NL80211_CMD_LEAVE_IBSS,
9765 .doit = nl80211_leave_ibss,
9766 .policy = nl80211_policy,
9767 .flags = GENL_ADMIN_PERM,
9768 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9769 NL80211_FLAG_NEED_RTNL,
9771 #ifdef CONFIG_NL80211_TESTMODE
9773 .cmd = NL80211_CMD_TESTMODE,
9774 .doit = nl80211_testmode_do,
9775 .dumpit = nl80211_testmode_dump,
9776 .policy = nl80211_policy,
9777 .flags = GENL_ADMIN_PERM,
9778 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9779 NL80211_FLAG_NEED_RTNL,
9781 #endif
9783 .cmd = NL80211_CMD_CONNECT,
9784 .doit = nl80211_connect,
9785 .policy = nl80211_policy,
9786 .flags = GENL_ADMIN_PERM,
9787 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9788 NL80211_FLAG_NEED_RTNL,
9791 .cmd = NL80211_CMD_DISCONNECT,
9792 .doit = nl80211_disconnect,
9793 .policy = nl80211_policy,
9794 .flags = GENL_ADMIN_PERM,
9795 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9796 NL80211_FLAG_NEED_RTNL,
9799 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9800 .doit = nl80211_wiphy_netns,
9801 .policy = nl80211_policy,
9802 .flags = GENL_ADMIN_PERM,
9803 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9804 NL80211_FLAG_NEED_RTNL,
9807 .cmd = NL80211_CMD_GET_SURVEY,
9808 .policy = nl80211_policy,
9809 .dumpit = nl80211_dump_survey,
9812 .cmd = NL80211_CMD_SET_PMKSA,
9813 .doit = nl80211_setdel_pmksa,
9814 .policy = nl80211_policy,
9815 .flags = GENL_ADMIN_PERM,
9816 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9817 NL80211_FLAG_NEED_RTNL,
9820 .cmd = NL80211_CMD_DEL_PMKSA,
9821 .doit = nl80211_setdel_pmksa,
9822 .policy = nl80211_policy,
9823 .flags = GENL_ADMIN_PERM,
9824 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9825 NL80211_FLAG_NEED_RTNL,
9828 .cmd = NL80211_CMD_FLUSH_PMKSA,
9829 .doit = nl80211_flush_pmksa,
9830 .policy = nl80211_policy,
9831 .flags = GENL_ADMIN_PERM,
9832 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9833 NL80211_FLAG_NEED_RTNL,
9836 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9837 .doit = nl80211_remain_on_channel,
9838 .policy = nl80211_policy,
9839 .flags = GENL_ADMIN_PERM,
9840 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9841 NL80211_FLAG_NEED_RTNL,
9844 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9845 .doit = nl80211_cancel_remain_on_channel,
9846 .policy = nl80211_policy,
9847 .flags = GENL_ADMIN_PERM,
9848 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9849 NL80211_FLAG_NEED_RTNL,
9852 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9853 .doit = nl80211_set_tx_bitrate_mask,
9854 .policy = nl80211_policy,
9855 .flags = GENL_ADMIN_PERM,
9856 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9857 NL80211_FLAG_NEED_RTNL,
9860 .cmd = NL80211_CMD_REGISTER_FRAME,
9861 .doit = nl80211_register_mgmt,
9862 .policy = nl80211_policy,
9863 .flags = GENL_ADMIN_PERM,
9864 .internal_flags = NL80211_FLAG_NEED_WDEV |
9865 NL80211_FLAG_NEED_RTNL,
9868 .cmd = NL80211_CMD_FRAME,
9869 .doit = nl80211_tx_mgmt,
9870 .policy = nl80211_policy,
9871 .flags = GENL_ADMIN_PERM,
9872 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9873 NL80211_FLAG_NEED_RTNL,
9876 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9877 .doit = nl80211_tx_mgmt_cancel_wait,
9878 .policy = nl80211_policy,
9879 .flags = GENL_ADMIN_PERM,
9880 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9881 NL80211_FLAG_NEED_RTNL,
9884 .cmd = NL80211_CMD_SET_POWER_SAVE,
9885 .doit = nl80211_set_power_save,
9886 .policy = nl80211_policy,
9887 .flags = GENL_ADMIN_PERM,
9888 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9889 NL80211_FLAG_NEED_RTNL,
9892 .cmd = NL80211_CMD_GET_POWER_SAVE,
9893 .doit = nl80211_get_power_save,
9894 .policy = nl80211_policy,
9895 /* can be retrieved by unprivileged users */
9896 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9897 NL80211_FLAG_NEED_RTNL,
9900 .cmd = NL80211_CMD_SET_CQM,
9901 .doit = nl80211_set_cqm,
9902 .policy = nl80211_policy,
9903 .flags = GENL_ADMIN_PERM,
9904 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9905 NL80211_FLAG_NEED_RTNL,
9908 .cmd = NL80211_CMD_SET_CHANNEL,
9909 .doit = nl80211_set_channel,
9910 .policy = nl80211_policy,
9911 .flags = GENL_ADMIN_PERM,
9912 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9913 NL80211_FLAG_NEED_RTNL,
9916 .cmd = NL80211_CMD_SET_WDS_PEER,
9917 .doit = nl80211_set_wds_peer,
9918 .policy = nl80211_policy,
9919 .flags = GENL_ADMIN_PERM,
9920 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9921 NL80211_FLAG_NEED_RTNL,
9924 .cmd = NL80211_CMD_JOIN_MESH,
9925 .doit = nl80211_join_mesh,
9926 .policy = nl80211_policy,
9927 .flags = GENL_ADMIN_PERM,
9928 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9929 NL80211_FLAG_NEED_RTNL,
9932 .cmd = NL80211_CMD_LEAVE_MESH,
9933 .doit = nl80211_leave_mesh,
9934 .policy = nl80211_policy,
9935 .flags = GENL_ADMIN_PERM,
9936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9937 NL80211_FLAG_NEED_RTNL,
9939 #ifdef CONFIG_PM
9941 .cmd = NL80211_CMD_GET_WOWLAN,
9942 .doit = nl80211_get_wowlan,
9943 .policy = nl80211_policy,
9944 /* can be retrieved by unprivileged users */
9945 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9946 NL80211_FLAG_NEED_RTNL,
9949 .cmd = NL80211_CMD_SET_WOWLAN,
9950 .doit = nl80211_set_wowlan,
9951 .policy = nl80211_policy,
9952 .flags = GENL_ADMIN_PERM,
9953 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9954 NL80211_FLAG_NEED_RTNL,
9956 #endif
9958 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9959 .doit = nl80211_set_rekey_data,
9960 .policy = nl80211_policy,
9961 .flags = GENL_ADMIN_PERM,
9962 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9963 NL80211_FLAG_NEED_RTNL,
9966 .cmd = NL80211_CMD_TDLS_MGMT,
9967 .doit = nl80211_tdls_mgmt,
9968 .policy = nl80211_policy,
9969 .flags = GENL_ADMIN_PERM,
9970 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9971 NL80211_FLAG_NEED_RTNL,
9974 .cmd = NL80211_CMD_TDLS_OPER,
9975 .doit = nl80211_tdls_oper,
9976 .policy = nl80211_policy,
9977 .flags = GENL_ADMIN_PERM,
9978 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9979 NL80211_FLAG_NEED_RTNL,
9982 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9983 .doit = nl80211_register_unexpected_frame,
9984 .policy = nl80211_policy,
9985 .flags = GENL_ADMIN_PERM,
9986 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9987 NL80211_FLAG_NEED_RTNL,
9990 .cmd = NL80211_CMD_PROBE_CLIENT,
9991 .doit = nl80211_probe_client,
9992 .policy = nl80211_policy,
9993 .flags = GENL_ADMIN_PERM,
9994 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9995 NL80211_FLAG_NEED_RTNL,
9998 .cmd = NL80211_CMD_REGISTER_BEACONS,
9999 .doit = nl80211_register_beacons,
10000 .policy = nl80211_policy,
10001 .flags = GENL_ADMIN_PERM,
10002 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10003 NL80211_FLAG_NEED_RTNL,
10006 .cmd = NL80211_CMD_SET_NOACK_MAP,
10007 .doit = nl80211_set_noack_map,
10008 .policy = nl80211_policy,
10009 .flags = GENL_ADMIN_PERM,
10010 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10011 NL80211_FLAG_NEED_RTNL,
10014 .cmd = NL80211_CMD_START_P2P_DEVICE,
10015 .doit = nl80211_start_p2p_device,
10016 .policy = nl80211_policy,
10017 .flags = GENL_ADMIN_PERM,
10018 .internal_flags = NL80211_FLAG_NEED_WDEV |
10019 NL80211_FLAG_NEED_RTNL,
10022 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
10023 .doit = nl80211_stop_p2p_device,
10024 .policy = nl80211_policy,
10025 .flags = GENL_ADMIN_PERM,
10026 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10027 NL80211_FLAG_NEED_RTNL,
10030 .cmd = NL80211_CMD_SET_MCAST_RATE,
10031 .doit = nl80211_set_mcast_rate,
10032 .policy = nl80211_policy,
10033 .flags = GENL_ADMIN_PERM,
10034 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10035 NL80211_FLAG_NEED_RTNL,
10038 .cmd = NL80211_CMD_SET_MAC_ACL,
10039 .doit = nl80211_set_mac_acl,
10040 .policy = nl80211_policy,
10041 .flags = GENL_ADMIN_PERM,
10042 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10043 NL80211_FLAG_NEED_RTNL,
10046 .cmd = NL80211_CMD_RADAR_DETECT,
10047 .doit = nl80211_start_radar_detection,
10048 .policy = nl80211_policy,
10049 .flags = GENL_ADMIN_PERM,
10050 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10051 NL80211_FLAG_NEED_RTNL,
10054 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
10055 .doit = nl80211_get_protocol_features,
10056 .policy = nl80211_policy,
10059 .cmd = NL80211_CMD_UPDATE_FT_IES,
10060 .doit = nl80211_update_ft_ies,
10061 .policy = nl80211_policy,
10062 .flags = GENL_ADMIN_PERM,
10063 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10064 NL80211_FLAG_NEED_RTNL,
10067 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
10068 .doit = nl80211_crit_protocol_start,
10069 .policy = nl80211_policy,
10070 .flags = GENL_ADMIN_PERM,
10071 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10072 NL80211_FLAG_NEED_RTNL,
10075 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
10076 .doit = nl80211_crit_protocol_stop,
10077 .policy = nl80211_policy,
10078 .flags = GENL_ADMIN_PERM,
10079 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10080 NL80211_FLAG_NEED_RTNL,
10083 .cmd = NL80211_CMD_GET_COALESCE,
10084 .doit = nl80211_get_coalesce,
10085 .policy = nl80211_policy,
10086 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10087 NL80211_FLAG_NEED_RTNL,
10090 .cmd = NL80211_CMD_SET_COALESCE,
10091 .doit = nl80211_set_coalesce,
10092 .policy = nl80211_policy,
10093 .flags = GENL_ADMIN_PERM,
10094 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10095 NL80211_FLAG_NEED_RTNL,
10098 .cmd = NL80211_CMD_CHANNEL_SWITCH,
10099 .doit = nl80211_channel_switch,
10100 .policy = nl80211_policy,
10101 .flags = GENL_ADMIN_PERM,
10102 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10103 NL80211_FLAG_NEED_RTNL,
10106 .cmd = NL80211_CMD_VENDOR,
10107 .doit = nl80211_vendor_cmd,
10108 .policy = nl80211_policy,
10109 .flags = GENL_ADMIN_PERM,
10110 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10111 NL80211_FLAG_NEED_RTNL,
10114 .cmd = NL80211_CMD_SET_QOS_MAP,
10115 .doit = nl80211_set_qos_map,
10116 .policy = nl80211_policy,
10117 .flags = GENL_ADMIN_PERM,
10118 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10119 NL80211_FLAG_NEED_RTNL,
10123 /* notification functions */
10125 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
10126 enum nl80211_commands cmd)
10128 struct sk_buff *msg;
10129 struct nl80211_dump_wiphy_state state = {};
10131 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
10132 cmd != NL80211_CMD_DEL_WIPHY);
10134 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10135 if (!msg)
10136 return;
10138 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
10139 nlmsg_free(msg);
10140 return;
10143 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10144 NL80211_MCGRP_CONFIG, GFP_KERNEL);
10147 static int nl80211_add_scan_req(struct sk_buff *msg,
10148 struct cfg80211_registered_device *rdev)
10150 struct cfg80211_scan_request *req = rdev->scan_req;
10151 struct nlattr *nest;
10152 int i;
10154 if (WARN_ON(!req))
10155 return 0;
10157 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
10158 if (!nest)
10159 goto nla_put_failure;
10160 for (i = 0; i < req->n_ssids; i++) {
10161 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
10162 goto nla_put_failure;
10164 nla_nest_end(msg, nest);
10166 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10167 if (!nest)
10168 goto nla_put_failure;
10169 for (i = 0; i < req->n_channels; i++) {
10170 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10171 goto nla_put_failure;
10173 nla_nest_end(msg, nest);
10175 if (req->ie &&
10176 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
10177 goto nla_put_failure;
10179 if (req->flags &&
10180 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
10181 goto nla_put_failure;
10183 return 0;
10184 nla_put_failure:
10185 return -ENOBUFS;
10188 static int nl80211_send_scan_msg(struct sk_buff *msg,
10189 struct cfg80211_registered_device *rdev,
10190 struct wireless_dev *wdev,
10191 u32 portid, u32 seq, int flags,
10192 u32 cmd)
10194 void *hdr;
10196 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
10197 if (!hdr)
10198 return -1;
10200 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10201 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10202 wdev->netdev->ifindex)) ||
10203 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10204 goto nla_put_failure;
10206 /* ignore errors and send incomplete event anyway */
10207 nl80211_add_scan_req(msg, rdev);
10209 return genlmsg_end(msg, hdr);
10211 nla_put_failure:
10212 genlmsg_cancel(msg, hdr);
10213 return -EMSGSIZE;
10216 static int
10217 nl80211_send_sched_scan_msg(struct sk_buff *msg,
10218 struct cfg80211_registered_device *rdev,
10219 struct net_device *netdev,
10220 u32 portid, u32 seq, int flags, u32 cmd)
10222 void *hdr;
10224 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
10225 if (!hdr)
10226 return -1;
10228 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10229 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10230 goto nla_put_failure;
10232 return genlmsg_end(msg, hdr);
10234 nla_put_failure:
10235 genlmsg_cancel(msg, hdr);
10236 return -EMSGSIZE;
10239 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
10240 struct wireless_dev *wdev)
10242 struct sk_buff *msg;
10244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10245 if (!msg)
10246 return;
10248 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
10249 NL80211_CMD_TRIGGER_SCAN) < 0) {
10250 nlmsg_free(msg);
10251 return;
10254 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10255 NL80211_MCGRP_SCAN, GFP_KERNEL);
10258 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
10259 struct wireless_dev *wdev, bool aborted)
10261 struct sk_buff *msg;
10263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10264 if (!msg)
10265 return NULL;
10267 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
10268 aborted ? NL80211_CMD_SCAN_ABORTED :
10269 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
10270 nlmsg_free(msg);
10271 return NULL;
10274 return msg;
10277 void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
10278 struct sk_buff *msg)
10280 if (!msg)
10281 return;
10283 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10284 NL80211_MCGRP_SCAN, GFP_KERNEL);
10287 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
10288 struct net_device *netdev)
10290 struct sk_buff *msg;
10292 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10293 if (!msg)
10294 return;
10296 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
10297 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
10298 nlmsg_free(msg);
10299 return;
10302 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10303 NL80211_MCGRP_SCAN, GFP_KERNEL);
10306 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
10307 struct net_device *netdev, u32 cmd)
10309 struct sk_buff *msg;
10311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10312 if (!msg)
10313 return;
10315 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
10316 nlmsg_free(msg);
10317 return;
10320 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10321 NL80211_MCGRP_SCAN, GFP_KERNEL);
10325 * This can happen on global regulatory changes or device specific settings
10326 * based on custom world regulatory domains.
10328 void nl80211_send_reg_change_event(struct regulatory_request *request)
10330 struct sk_buff *msg;
10331 void *hdr;
10333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10334 if (!msg)
10335 return;
10337 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
10338 if (!hdr) {
10339 nlmsg_free(msg);
10340 return;
10343 /* Userspace can always count this one always being set */
10344 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
10345 goto nla_put_failure;
10347 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
10348 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10349 NL80211_REGDOM_TYPE_WORLD))
10350 goto nla_put_failure;
10351 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
10352 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10353 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
10354 goto nla_put_failure;
10355 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
10356 request->intersect) {
10357 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10358 NL80211_REGDOM_TYPE_INTERSECTION))
10359 goto nla_put_failure;
10360 } else {
10361 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
10362 NL80211_REGDOM_TYPE_COUNTRY) ||
10363 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
10364 request->alpha2))
10365 goto nla_put_failure;
10368 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
10369 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
10370 goto nla_put_failure;
10372 genlmsg_end(msg, hdr);
10374 rcu_read_lock();
10375 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
10376 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
10377 rcu_read_unlock();
10379 return;
10381 nla_put_failure:
10382 genlmsg_cancel(msg, hdr);
10383 nlmsg_free(msg);
10386 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
10387 struct net_device *netdev,
10388 const u8 *buf, size_t len,
10389 enum nl80211_commands cmd, gfp_t gfp)
10391 struct sk_buff *msg;
10392 void *hdr;
10394 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10395 if (!msg)
10396 return;
10398 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10399 if (!hdr) {
10400 nlmsg_free(msg);
10401 return;
10404 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10405 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10406 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10407 goto nla_put_failure;
10409 genlmsg_end(msg, hdr);
10411 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10412 NL80211_MCGRP_MLME, gfp);
10413 return;
10415 nla_put_failure:
10416 genlmsg_cancel(msg, hdr);
10417 nlmsg_free(msg);
10420 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
10421 struct net_device *netdev, const u8 *buf,
10422 size_t len, gfp_t gfp)
10424 nl80211_send_mlme_event(rdev, netdev, buf, len,
10425 NL80211_CMD_AUTHENTICATE, gfp);
10428 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10429 struct net_device *netdev, const u8 *buf,
10430 size_t len, gfp_t gfp)
10432 nl80211_send_mlme_event(rdev, netdev, buf, len,
10433 NL80211_CMD_ASSOCIATE, gfp);
10436 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
10437 struct net_device *netdev, const u8 *buf,
10438 size_t len, gfp_t gfp)
10440 nl80211_send_mlme_event(rdev, netdev, buf, len,
10441 NL80211_CMD_DEAUTHENTICATE, gfp);
10444 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10445 struct net_device *netdev, const u8 *buf,
10446 size_t len, gfp_t gfp)
10448 nl80211_send_mlme_event(rdev, netdev, buf, len,
10449 NL80211_CMD_DISASSOCIATE, gfp);
10452 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10453 size_t len)
10455 struct wireless_dev *wdev = dev->ieee80211_ptr;
10456 struct wiphy *wiphy = wdev->wiphy;
10457 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10458 const struct ieee80211_mgmt *mgmt = (void *)buf;
10459 u32 cmd;
10461 if (WARN_ON(len < 2))
10462 return;
10464 if (ieee80211_is_deauth(mgmt->frame_control))
10465 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10466 else
10467 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10469 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10470 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
10472 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
10474 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10475 struct net_device *netdev, int cmd,
10476 const u8 *addr, gfp_t gfp)
10478 struct sk_buff *msg;
10479 void *hdr;
10481 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10482 if (!msg)
10483 return;
10485 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10486 if (!hdr) {
10487 nlmsg_free(msg);
10488 return;
10491 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10492 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10493 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10494 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10495 goto nla_put_failure;
10497 genlmsg_end(msg, hdr);
10499 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10500 NL80211_MCGRP_MLME, gfp);
10501 return;
10503 nla_put_failure:
10504 genlmsg_cancel(msg, hdr);
10505 nlmsg_free(msg);
10508 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
10509 struct net_device *netdev, const u8 *addr,
10510 gfp_t gfp)
10512 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
10513 addr, gfp);
10516 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
10517 struct net_device *netdev, const u8 *addr,
10518 gfp_t gfp)
10520 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10521 addr, gfp);
10524 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10525 struct net_device *netdev, const u8 *bssid,
10526 const u8 *req_ie, size_t req_ie_len,
10527 const u8 *resp_ie, size_t resp_ie_len,
10528 u16 status, gfp_t gfp)
10530 struct sk_buff *msg;
10531 void *hdr;
10533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10534 if (!msg)
10535 return;
10537 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10538 if (!hdr) {
10539 nlmsg_free(msg);
10540 return;
10543 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10544 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10545 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10546 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10547 (req_ie &&
10548 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10549 (resp_ie &&
10550 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10551 goto nla_put_failure;
10553 genlmsg_end(msg, hdr);
10555 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10556 NL80211_MCGRP_MLME, gfp);
10557 return;
10559 nla_put_failure:
10560 genlmsg_cancel(msg, hdr);
10561 nlmsg_free(msg);
10565 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10566 struct net_device *netdev, const u8 *bssid,
10567 const u8 *req_ie, size_t req_ie_len,
10568 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10570 struct sk_buff *msg;
10571 void *hdr;
10573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10574 if (!msg)
10575 return;
10577 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10578 if (!hdr) {
10579 nlmsg_free(msg);
10580 return;
10583 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10584 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10585 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10586 (req_ie &&
10587 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10588 (resp_ie &&
10589 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10590 goto nla_put_failure;
10592 genlmsg_end(msg, hdr);
10594 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10595 NL80211_MCGRP_MLME, gfp);
10596 return;
10598 nla_put_failure:
10599 genlmsg_cancel(msg, hdr);
10600 nlmsg_free(msg);
10604 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10605 struct net_device *netdev, u16 reason,
10606 const u8 *ie, size_t ie_len, bool from_ap)
10608 struct sk_buff *msg;
10609 void *hdr;
10611 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10612 if (!msg)
10613 return;
10615 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10616 if (!hdr) {
10617 nlmsg_free(msg);
10618 return;
10621 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10622 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10623 (from_ap && reason &&
10624 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10625 (from_ap &&
10626 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10627 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10628 goto nla_put_failure;
10630 genlmsg_end(msg, hdr);
10632 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10633 NL80211_MCGRP_MLME, GFP_KERNEL);
10634 return;
10636 nla_put_failure:
10637 genlmsg_cancel(msg, hdr);
10638 nlmsg_free(msg);
10642 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10643 struct net_device *netdev, const u8 *bssid,
10644 gfp_t gfp)
10646 struct sk_buff *msg;
10647 void *hdr;
10649 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10650 if (!msg)
10651 return;
10653 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10654 if (!hdr) {
10655 nlmsg_free(msg);
10656 return;
10659 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10660 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10661 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10662 goto nla_put_failure;
10664 genlmsg_end(msg, hdr);
10666 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10667 NL80211_MCGRP_MLME, gfp);
10668 return;
10670 nla_put_failure:
10671 genlmsg_cancel(msg, hdr);
10672 nlmsg_free(msg);
10675 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10676 const u8* ie, u8 ie_len, gfp_t gfp)
10678 struct wireless_dev *wdev = dev->ieee80211_ptr;
10679 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
10680 struct sk_buff *msg;
10681 void *hdr;
10683 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10684 return;
10686 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10688 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10689 if (!msg)
10690 return;
10692 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10693 if (!hdr) {
10694 nlmsg_free(msg);
10695 return;
10698 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10699 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10700 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10701 (ie_len && ie &&
10702 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10703 goto nla_put_failure;
10705 genlmsg_end(msg, hdr);
10707 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10708 NL80211_MCGRP_MLME, gfp);
10709 return;
10711 nla_put_failure:
10712 genlmsg_cancel(msg, hdr);
10713 nlmsg_free(msg);
10715 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
10717 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10718 struct net_device *netdev, const u8 *addr,
10719 enum nl80211_key_type key_type, int key_id,
10720 const u8 *tsc, gfp_t gfp)
10722 struct sk_buff *msg;
10723 void *hdr;
10725 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10726 if (!msg)
10727 return;
10729 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10730 if (!hdr) {
10731 nlmsg_free(msg);
10732 return;
10735 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10736 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10737 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10738 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10739 (key_id != -1 &&
10740 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10741 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10742 goto nla_put_failure;
10744 genlmsg_end(msg, hdr);
10746 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10747 NL80211_MCGRP_MLME, gfp);
10748 return;
10750 nla_put_failure:
10751 genlmsg_cancel(msg, hdr);
10752 nlmsg_free(msg);
10755 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10756 struct ieee80211_channel *channel_before,
10757 struct ieee80211_channel *channel_after)
10759 struct sk_buff *msg;
10760 void *hdr;
10761 struct nlattr *nl_freq;
10763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
10764 if (!msg)
10765 return;
10767 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10768 if (!hdr) {
10769 nlmsg_free(msg);
10770 return;
10774 * Since we are applying the beacon hint to a wiphy we know its
10775 * wiphy_idx is valid
10777 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10778 goto nla_put_failure;
10780 /* Before */
10781 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10782 if (!nl_freq)
10783 goto nla_put_failure;
10784 if (nl80211_msg_put_channel(msg, channel_before, false))
10785 goto nla_put_failure;
10786 nla_nest_end(msg, nl_freq);
10788 /* After */
10789 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10790 if (!nl_freq)
10791 goto nla_put_failure;
10792 if (nl80211_msg_put_channel(msg, channel_after, false))
10793 goto nla_put_failure;
10794 nla_nest_end(msg, nl_freq);
10796 genlmsg_end(msg, hdr);
10798 rcu_read_lock();
10799 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
10800 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
10801 rcu_read_unlock();
10803 return;
10805 nla_put_failure:
10806 genlmsg_cancel(msg, hdr);
10807 nlmsg_free(msg);
10810 static void nl80211_send_remain_on_chan_event(
10811 int cmd, struct cfg80211_registered_device *rdev,
10812 struct wireless_dev *wdev, u64 cookie,
10813 struct ieee80211_channel *chan,
10814 unsigned int duration, gfp_t gfp)
10816 struct sk_buff *msg;
10817 void *hdr;
10819 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10820 if (!msg)
10821 return;
10823 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10824 if (!hdr) {
10825 nlmsg_free(msg);
10826 return;
10829 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10830 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10831 wdev->netdev->ifindex)) ||
10832 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
10833 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
10834 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10835 NL80211_CHAN_NO_HT) ||
10836 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10837 goto nla_put_failure;
10839 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10840 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10841 goto nla_put_failure;
10843 genlmsg_end(msg, hdr);
10845 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10846 NL80211_MCGRP_MLME, gfp);
10847 return;
10849 nla_put_failure:
10850 genlmsg_cancel(msg, hdr);
10851 nlmsg_free(msg);
10854 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10855 struct ieee80211_channel *chan,
10856 unsigned int duration, gfp_t gfp)
10858 struct wiphy *wiphy = wdev->wiphy;
10859 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10861 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
10862 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
10863 rdev, wdev, cookie, chan,
10864 duration, gfp);
10866 EXPORT_SYMBOL(cfg80211_ready_on_channel);
10868 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10869 struct ieee80211_channel *chan,
10870 gfp_t gfp)
10872 struct wiphy *wiphy = wdev->wiphy;
10873 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10875 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
10876 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
10877 rdev, wdev, cookie, chan, 0, gfp);
10879 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
10881 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10882 struct station_info *sinfo, gfp_t gfp)
10884 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10885 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10886 struct sk_buff *msg;
10888 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10890 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10891 if (!msg)
10892 return;
10894 if (nl80211_send_station(msg, 0, 0, 0,
10895 rdev, dev, mac_addr, sinfo) < 0) {
10896 nlmsg_free(msg);
10897 return;
10900 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10901 NL80211_MCGRP_MLME, gfp);
10903 EXPORT_SYMBOL(cfg80211_new_sta);
10905 void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
10907 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10908 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10909 struct sk_buff *msg;
10910 void *hdr;
10912 trace_cfg80211_del_sta(dev, mac_addr);
10914 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10915 if (!msg)
10916 return;
10918 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10919 if (!hdr) {
10920 nlmsg_free(msg);
10921 return;
10924 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10925 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10926 goto nla_put_failure;
10928 genlmsg_end(msg, hdr);
10930 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10931 NL80211_MCGRP_MLME, gfp);
10932 return;
10934 nla_put_failure:
10935 genlmsg_cancel(msg, hdr);
10936 nlmsg_free(msg);
10938 EXPORT_SYMBOL(cfg80211_del_sta);
10940 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10941 enum nl80211_connect_failed_reason reason,
10942 gfp_t gfp)
10944 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10945 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10946 struct sk_buff *msg;
10947 void *hdr;
10949 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10950 if (!msg)
10951 return;
10953 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10954 if (!hdr) {
10955 nlmsg_free(msg);
10956 return;
10959 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10960 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10961 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10962 goto nla_put_failure;
10964 genlmsg_end(msg, hdr);
10966 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10967 NL80211_MCGRP_MLME, gfp);
10968 return;
10970 nla_put_failure:
10971 genlmsg_cancel(msg, hdr);
10972 nlmsg_free(msg);
10974 EXPORT_SYMBOL(cfg80211_conn_failed);
10976 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10977 const u8 *addr, gfp_t gfp)
10979 struct wireless_dev *wdev = dev->ieee80211_ptr;
10980 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
10981 struct sk_buff *msg;
10982 void *hdr;
10983 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
10985 if (!nlportid)
10986 return false;
10988 msg = nlmsg_new(100, gfp);
10989 if (!msg)
10990 return true;
10992 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10993 if (!hdr) {
10994 nlmsg_free(msg);
10995 return true;
10998 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10999 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11000 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11001 goto nla_put_failure;
11003 genlmsg_end(msg, hdr);
11004 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11005 return true;
11007 nla_put_failure:
11008 genlmsg_cancel(msg, hdr);
11009 nlmsg_free(msg);
11010 return true;
11013 bool cfg80211_rx_spurious_frame(struct net_device *dev,
11014 const u8 *addr, gfp_t gfp)
11016 struct wireless_dev *wdev = dev->ieee80211_ptr;
11017 bool ret;
11019 trace_cfg80211_rx_spurious_frame(dev, addr);
11021 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11022 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
11023 trace_cfg80211_return_bool(false);
11024 return false;
11026 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
11027 addr, gfp);
11028 trace_cfg80211_return_bool(ret);
11029 return ret;
11031 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
11033 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
11034 const u8 *addr, gfp_t gfp)
11036 struct wireless_dev *wdev = dev->ieee80211_ptr;
11037 bool ret;
11039 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
11041 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11042 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
11043 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
11044 trace_cfg80211_return_bool(false);
11045 return false;
11047 ret = __nl80211_unexpected_frame(dev,
11048 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
11049 addr, gfp);
11050 trace_cfg80211_return_bool(ret);
11051 return ret;
11053 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
11055 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
11056 struct wireless_dev *wdev, u32 nlportid,
11057 int freq, int sig_dbm,
11058 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
11060 struct net_device *netdev = wdev->netdev;
11061 struct sk_buff *msg;
11062 void *hdr;
11064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11065 if (!msg)
11066 return -ENOMEM;
11068 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11069 if (!hdr) {
11070 nlmsg_free(msg);
11071 return -ENOMEM;
11074 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11075 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11076 netdev->ifindex)) ||
11077 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
11078 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
11079 (sig_dbm &&
11080 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11081 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11082 (flags &&
11083 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
11084 goto nla_put_failure;
11086 genlmsg_end(msg, hdr);
11088 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11090 nla_put_failure:
11091 genlmsg_cancel(msg, hdr);
11092 nlmsg_free(msg);
11093 return -ENOBUFS;
11096 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
11097 const u8 *buf, size_t len, bool ack, gfp_t gfp)
11099 struct wiphy *wiphy = wdev->wiphy;
11100 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11101 struct net_device *netdev = wdev->netdev;
11102 struct sk_buff *msg;
11103 void *hdr;
11105 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
11107 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11108 if (!msg)
11109 return;
11111 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
11112 if (!hdr) {
11113 nlmsg_free(msg);
11114 return;
11117 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11118 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11119 netdev->ifindex)) ||
11120 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
11121 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
11122 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11123 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
11124 goto nla_put_failure;
11126 genlmsg_end(msg, hdr);
11128 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11129 NL80211_MCGRP_MLME, gfp);
11130 return;
11132 nla_put_failure:
11133 genlmsg_cancel(msg, hdr);
11134 nlmsg_free(msg);
11136 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
11138 void cfg80211_cqm_rssi_notify(struct net_device *dev,
11139 enum nl80211_cqm_rssi_threshold_event rssi_event,
11140 gfp_t gfp)
11142 struct wireless_dev *wdev = dev->ieee80211_ptr;
11143 struct wiphy *wiphy = wdev->wiphy;
11144 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11145 struct sk_buff *msg;
11146 struct nlattr *pinfoattr;
11147 void *hdr;
11149 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
11151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11152 if (!msg)
11153 return;
11155 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11156 if (!hdr) {
11157 nlmsg_free(msg);
11158 return;
11161 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11162 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
11163 goto nla_put_failure;
11165 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11166 if (!pinfoattr)
11167 goto nla_put_failure;
11169 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
11170 rssi_event))
11171 goto nla_put_failure;
11173 nla_nest_end(msg, pinfoattr);
11175 genlmsg_end(msg, hdr);
11177 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11178 NL80211_MCGRP_MLME, gfp);
11179 return;
11181 nla_put_failure:
11182 genlmsg_cancel(msg, hdr);
11183 nlmsg_free(msg);
11185 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
11187 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
11188 struct net_device *netdev, const u8 *bssid,
11189 const u8 *replay_ctr, gfp_t gfp)
11191 struct sk_buff *msg;
11192 struct nlattr *rekey_attr;
11193 void *hdr;
11195 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11196 if (!msg)
11197 return;
11199 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
11200 if (!hdr) {
11201 nlmsg_free(msg);
11202 return;
11205 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11206 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11207 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
11208 goto nla_put_failure;
11210 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
11211 if (!rekey_attr)
11212 goto nla_put_failure;
11214 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
11215 NL80211_REPLAY_CTR_LEN, replay_ctr))
11216 goto nla_put_failure;
11218 nla_nest_end(msg, rekey_attr);
11220 genlmsg_end(msg, hdr);
11222 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11223 NL80211_MCGRP_MLME, gfp);
11224 return;
11226 nla_put_failure:
11227 genlmsg_cancel(msg, hdr);
11228 nlmsg_free(msg);
11231 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
11232 const u8 *replay_ctr, gfp_t gfp)
11234 struct wireless_dev *wdev = dev->ieee80211_ptr;
11235 struct wiphy *wiphy = wdev->wiphy;
11236 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11238 trace_cfg80211_gtk_rekey_notify(dev, bssid);
11239 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
11241 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
11243 static void
11244 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
11245 struct net_device *netdev, int index,
11246 const u8 *bssid, bool preauth, gfp_t gfp)
11248 struct sk_buff *msg;
11249 struct nlattr *attr;
11250 void *hdr;
11252 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11253 if (!msg)
11254 return;
11256 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
11257 if (!hdr) {
11258 nlmsg_free(msg);
11259 return;
11262 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11263 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11264 goto nla_put_failure;
11266 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
11267 if (!attr)
11268 goto nla_put_failure;
11270 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
11271 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
11272 (preauth &&
11273 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
11274 goto nla_put_failure;
11276 nla_nest_end(msg, attr);
11278 genlmsg_end(msg, hdr);
11280 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11281 NL80211_MCGRP_MLME, gfp);
11282 return;
11284 nla_put_failure:
11285 genlmsg_cancel(msg, hdr);
11286 nlmsg_free(msg);
11289 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
11290 const u8 *bssid, bool preauth, gfp_t gfp)
11292 struct wireless_dev *wdev = dev->ieee80211_ptr;
11293 struct wiphy *wiphy = wdev->wiphy;
11294 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11296 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
11297 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
11299 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
11301 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
11302 struct net_device *netdev,
11303 struct cfg80211_chan_def *chandef,
11304 gfp_t gfp)
11306 struct sk_buff *msg;
11307 void *hdr;
11309 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11310 if (!msg)
11311 return;
11313 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
11314 if (!hdr) {
11315 nlmsg_free(msg);
11316 return;
11319 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11320 goto nla_put_failure;
11322 if (nl80211_send_chandef(msg, chandef))
11323 goto nla_put_failure;
11325 genlmsg_end(msg, hdr);
11327 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11328 NL80211_MCGRP_MLME, gfp);
11329 return;
11331 nla_put_failure:
11332 genlmsg_cancel(msg, hdr);
11333 nlmsg_free(msg);
11336 void cfg80211_ch_switch_notify(struct net_device *dev,
11337 struct cfg80211_chan_def *chandef)
11339 struct wireless_dev *wdev = dev->ieee80211_ptr;
11340 struct wiphy *wiphy = wdev->wiphy;
11341 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11343 ASSERT_WDEV_LOCK(wdev);
11345 trace_cfg80211_ch_switch_notify(dev, chandef);
11347 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
11348 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
11349 wdev->iftype != NL80211_IFTYPE_ADHOC &&
11350 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
11351 return;
11353 wdev->chandef = *chandef;
11354 wdev->preset_chandef = *chandef;
11355 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
11357 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
11359 void cfg80211_cqm_txe_notify(struct net_device *dev,
11360 const u8 *peer, u32 num_packets,
11361 u32 rate, u32 intvl, gfp_t gfp)
11363 struct wireless_dev *wdev = dev->ieee80211_ptr;
11364 struct wiphy *wiphy = wdev->wiphy;
11365 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11366 struct sk_buff *msg;
11367 struct nlattr *pinfoattr;
11368 void *hdr;
11370 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
11371 if (!msg)
11372 return;
11374 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11375 if (!hdr) {
11376 nlmsg_free(msg);
11377 return;
11380 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11381 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11382 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11383 goto nla_put_failure;
11385 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11386 if (!pinfoattr)
11387 goto nla_put_failure;
11389 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
11390 goto nla_put_failure;
11392 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
11393 goto nla_put_failure;
11395 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
11396 goto nla_put_failure;
11398 nla_nest_end(msg, pinfoattr);
11400 genlmsg_end(msg, hdr);
11402 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11403 NL80211_MCGRP_MLME, gfp);
11404 return;
11406 nla_put_failure:
11407 genlmsg_cancel(msg, hdr);
11408 nlmsg_free(msg);
11410 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
11412 void
11413 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
11414 const struct cfg80211_chan_def *chandef,
11415 enum nl80211_radar_event event,
11416 struct net_device *netdev, gfp_t gfp)
11418 struct sk_buff *msg;
11419 void *hdr;
11421 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11422 if (!msg)
11423 return;
11425 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11426 if (!hdr) {
11427 nlmsg_free(msg);
11428 return;
11431 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11432 goto nla_put_failure;
11434 /* NOP and radar events don't need a netdev parameter */
11435 if (netdev) {
11436 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11438 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11439 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11440 goto nla_put_failure;
11443 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11444 goto nla_put_failure;
11446 if (nl80211_send_chandef(msg, chandef))
11447 goto nla_put_failure;
11449 genlmsg_end(msg, hdr);
11451 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11452 NL80211_MCGRP_MLME, gfp);
11453 return;
11455 nla_put_failure:
11456 genlmsg_cancel(msg, hdr);
11457 nlmsg_free(msg);
11460 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11461 const u8 *peer, u32 num_packets, gfp_t gfp)
11463 struct wireless_dev *wdev = dev->ieee80211_ptr;
11464 struct wiphy *wiphy = wdev->wiphy;
11465 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11466 struct sk_buff *msg;
11467 struct nlattr *pinfoattr;
11468 void *hdr;
11470 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11472 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11473 if (!msg)
11474 return;
11476 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11477 if (!hdr) {
11478 nlmsg_free(msg);
11479 return;
11482 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11483 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11484 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11485 goto nla_put_failure;
11487 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11488 if (!pinfoattr)
11489 goto nla_put_failure;
11491 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11492 goto nla_put_failure;
11494 nla_nest_end(msg, pinfoattr);
11496 genlmsg_end(msg, hdr);
11498 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11499 NL80211_MCGRP_MLME, gfp);
11500 return;
11502 nla_put_failure:
11503 genlmsg_cancel(msg, hdr);
11504 nlmsg_free(msg);
11506 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
11508 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11509 u64 cookie, bool acked, gfp_t gfp)
11511 struct wireless_dev *wdev = dev->ieee80211_ptr;
11512 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
11513 struct sk_buff *msg;
11514 void *hdr;
11516 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11520 if (!msg)
11521 return;
11523 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11524 if (!hdr) {
11525 nlmsg_free(msg);
11526 return;
11529 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11530 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11531 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11532 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11533 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11534 goto nla_put_failure;
11536 genlmsg_end(msg, hdr);
11538 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11539 NL80211_MCGRP_MLME, gfp);
11540 return;
11542 nla_put_failure:
11543 genlmsg_cancel(msg, hdr);
11544 nlmsg_free(msg);
11546 EXPORT_SYMBOL(cfg80211_probe_status);
11548 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11549 const u8 *frame, size_t len,
11550 int freq, int sig_dbm)
11552 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11553 struct sk_buff *msg;
11554 void *hdr;
11555 struct cfg80211_beacon_registration *reg;
11557 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11559 spin_lock_bh(&rdev->beacon_registrations_lock);
11560 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11561 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11562 if (!msg) {
11563 spin_unlock_bh(&rdev->beacon_registrations_lock);
11564 return;
11567 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11568 if (!hdr)
11569 goto nla_put_failure;
11571 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11572 (freq &&
11573 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11574 (sig_dbm &&
11575 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11576 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11577 goto nla_put_failure;
11579 genlmsg_end(msg, hdr);
11581 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
11583 spin_unlock_bh(&rdev->beacon_registrations_lock);
11584 return;
11586 nla_put_failure:
11587 spin_unlock_bh(&rdev->beacon_registrations_lock);
11588 if (hdr)
11589 genlmsg_cancel(msg, hdr);
11590 nlmsg_free(msg);
11592 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11594 #ifdef CONFIG_PM
11595 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11596 struct cfg80211_wowlan_wakeup *wakeup,
11597 gfp_t gfp)
11599 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
11600 struct sk_buff *msg;
11601 void *hdr;
11602 int size = 200;
11604 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11606 if (wakeup)
11607 size += wakeup->packet_present_len;
11609 msg = nlmsg_new(size, gfp);
11610 if (!msg)
11611 return;
11613 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11614 if (!hdr)
11615 goto free_msg;
11617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11618 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11619 goto free_msg;
11621 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11622 wdev->netdev->ifindex))
11623 goto free_msg;
11625 if (wakeup) {
11626 struct nlattr *reasons;
11628 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
11629 if (!reasons)
11630 goto free_msg;
11632 if (wakeup->disconnect &&
11633 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11634 goto free_msg;
11635 if (wakeup->magic_pkt &&
11636 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11637 goto free_msg;
11638 if (wakeup->gtk_rekey_failure &&
11639 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11640 goto free_msg;
11641 if (wakeup->eap_identity_req &&
11642 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11643 goto free_msg;
11644 if (wakeup->four_way_handshake &&
11645 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11646 goto free_msg;
11647 if (wakeup->rfkill_release &&
11648 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11649 goto free_msg;
11651 if (wakeup->pattern_idx >= 0 &&
11652 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11653 wakeup->pattern_idx))
11654 goto free_msg;
11656 if (wakeup->tcp_match &&
11657 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11658 goto free_msg;
11660 if (wakeup->tcp_connlost &&
11661 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11662 goto free_msg;
11664 if (wakeup->tcp_nomoretokens &&
11665 nla_put_flag(msg,
11666 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11667 goto free_msg;
11669 if (wakeup->packet) {
11670 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11671 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11673 if (!wakeup->packet_80211) {
11674 pkt_attr =
11675 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11676 len_attr =
11677 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11680 if (wakeup->packet_len &&
11681 nla_put_u32(msg, len_attr, wakeup->packet_len))
11682 goto free_msg;
11684 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11685 wakeup->packet))
11686 goto free_msg;
11689 nla_nest_end(msg, reasons);
11692 genlmsg_end(msg, hdr);
11694 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11695 NL80211_MCGRP_MLME, gfp);
11696 return;
11698 free_msg:
11699 nlmsg_free(msg);
11701 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11702 #endif
11704 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11705 enum nl80211_tdls_operation oper,
11706 u16 reason_code, gfp_t gfp)
11708 struct wireless_dev *wdev = dev->ieee80211_ptr;
11709 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
11710 struct sk_buff *msg;
11711 void *hdr;
11713 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11714 reason_code);
11716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11717 if (!msg)
11718 return;
11720 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11721 if (!hdr) {
11722 nlmsg_free(msg);
11723 return;
11726 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11727 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11728 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11729 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11730 (reason_code > 0 &&
11731 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11732 goto nla_put_failure;
11734 genlmsg_end(msg, hdr);
11736 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11737 NL80211_MCGRP_MLME, gfp);
11738 return;
11740 nla_put_failure:
11741 genlmsg_cancel(msg, hdr);
11742 nlmsg_free(msg);
11744 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11746 static int nl80211_netlink_notify(struct notifier_block * nb,
11747 unsigned long state,
11748 void *_notify)
11750 struct netlink_notify *notify = _notify;
11751 struct cfg80211_registered_device *rdev;
11752 struct wireless_dev *wdev;
11753 struct cfg80211_beacon_registration *reg, *tmp;
11755 if (state != NETLINK_URELEASE)
11756 return NOTIFY_DONE;
11758 rcu_read_lock();
11760 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
11761 bool schedule_destroy_work = false;
11763 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
11764 cfg80211_mlme_unregister_socket(wdev, notify->portid);
11766 if (wdev->owner_nlportid == notify->portid)
11767 schedule_destroy_work = true;
11770 spin_lock_bh(&rdev->beacon_registrations_lock);
11771 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11772 list) {
11773 if (reg->nlportid == notify->portid) {
11774 list_del(&reg->list);
11775 kfree(reg);
11776 break;
11779 spin_unlock_bh(&rdev->beacon_registrations_lock);
11781 if (schedule_destroy_work) {
11782 struct cfg80211_iface_destroy *destroy;
11784 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
11785 if (destroy) {
11786 destroy->nlportid = notify->portid;
11787 spin_lock(&rdev->destroy_list_lock);
11788 list_add(&destroy->list, &rdev->destroy_list);
11789 spin_unlock(&rdev->destroy_list_lock);
11790 schedule_work(&rdev->destroy_work);
11795 rcu_read_unlock();
11797 return NOTIFY_OK;
11800 static struct notifier_block nl80211_netlink_notifier = {
11801 .notifier_call = nl80211_netlink_notify,
11804 void cfg80211_ft_event(struct net_device *netdev,
11805 struct cfg80211_ft_event_params *ft_event)
11807 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11808 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11809 struct sk_buff *msg;
11810 void *hdr;
11812 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11814 if (!ft_event->target_ap)
11815 return;
11817 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11818 if (!msg)
11819 return;
11821 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
11822 if (!hdr)
11823 goto out;
11825 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11826 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11827 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11828 goto out;
11830 if (ft_event->ies &&
11831 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11832 goto out;
11833 if (ft_event->ric_ies &&
11834 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11835 ft_event->ric_ies))
11836 goto out;
11838 genlmsg_end(msg, hdr);
11840 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11841 NL80211_MCGRP_MLME, GFP_KERNEL);
11842 return;
11843 out:
11844 nlmsg_free(msg);
11846 EXPORT_SYMBOL(cfg80211_ft_event);
11848 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11850 struct cfg80211_registered_device *rdev;
11851 struct sk_buff *msg;
11852 void *hdr;
11853 u32 nlportid;
11855 rdev = wiphy_to_rdev(wdev->wiphy);
11856 if (!rdev->crit_proto_nlportid)
11857 return;
11859 nlportid = rdev->crit_proto_nlportid;
11860 rdev->crit_proto_nlportid = 0;
11862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11863 if (!msg)
11864 return;
11866 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11867 if (!hdr)
11868 goto nla_put_failure;
11870 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11871 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11872 goto nla_put_failure;
11874 genlmsg_end(msg, hdr);
11876 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11877 return;
11879 nla_put_failure:
11880 if (hdr)
11881 genlmsg_cancel(msg, hdr);
11882 nlmsg_free(msg);
11885 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11887 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
11889 struct wiphy *wiphy = wdev->wiphy;
11890 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11891 struct sk_buff *msg;
11892 void *hdr;
11894 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11895 if (!msg)
11896 return;
11898 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
11899 if (!hdr)
11900 goto out;
11902 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11903 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
11904 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11905 goto out;
11907 genlmsg_end(msg, hdr);
11909 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
11910 NL80211_MCGRP_MLME, GFP_KERNEL);
11911 return;
11912 out:
11913 nlmsg_free(msg);
11916 /* initialisation/exit functions */
11918 int nl80211_init(void)
11920 int err;
11922 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11923 nl80211_mcgrps);
11924 if (err)
11925 return err;
11927 err = netlink_register_notifier(&nl80211_netlink_notifier);
11928 if (err)
11929 goto err_out;
11931 return 0;
11932 err_out:
11933 genl_unregister_family(&nl80211_fam);
11934 return err;
11937 void nl80211_exit(void)
11939 netlink_unregister_notifier(&nl80211_netlink_notifier);
11940 genl_unregister_family(&nl80211_fam);