IB/srp: Let srp_abort() return FAST_IO_FAIL if TL offline
[linux/fpc-iii.git] / net / wireless / nl80211.c
blobea74b9dd9d82cf3541ad7aee4a9de34facad7dec
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(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35 static void nl80211_post_doit(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 /* returns ERR_PTR values */
51 static struct wireless_dev *
52 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
54 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
62 ASSERT_RTNL();
64 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
67 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
74 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
95 if (result)
96 break;
99 if (result)
100 return result;
101 return ERR_PTR(-ENODEV);
104 static struct cfg80211_registered_device *
105 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
107 struct cfg80211_registered_device *rdev = NULL, *tmp;
108 struct net_device *netdev;
110 ASSERT_RTNL();
112 if (!attrs[NL80211_ATTR_WIPHY] &&
113 !attrs[NL80211_ATTR_IFINDEX] &&
114 !attrs[NL80211_ATTR_WDEV])
115 return ERR_PTR(-EINVAL);
117 if (attrs[NL80211_ATTR_WIPHY])
118 rdev = cfg80211_rdev_by_wiphy_idx(
119 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
121 if (attrs[NL80211_ATTR_WDEV]) {
122 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
123 struct wireless_dev *wdev;
124 bool found = false;
126 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
127 if (tmp) {
128 /* make sure wdev exists */
129 list_for_each_entry(wdev, &tmp->wdev_list, list) {
130 if (wdev->identifier != (u32)wdev_id)
131 continue;
132 found = true;
133 break;
136 if (!found)
137 tmp = NULL;
139 if (rdev && tmp != rdev)
140 return ERR_PTR(-EINVAL);
141 rdev = tmp;
145 if (attrs[NL80211_ATTR_IFINDEX]) {
146 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
147 netdev = dev_get_by_index(netns, ifindex);
148 if (netdev) {
149 if (netdev->ieee80211_ptr)
150 tmp = wiphy_to_dev(
151 netdev->ieee80211_ptr->wiphy);
152 else
153 tmp = NULL;
155 dev_put(netdev);
157 /* not wireless device -- return error */
158 if (!tmp)
159 return ERR_PTR(-EINVAL);
161 /* mismatch -- return error */
162 if (rdev && tmp != rdev)
163 return ERR_PTR(-EINVAL);
165 rdev = tmp;
169 if (!rdev)
170 return ERR_PTR(-ENODEV);
172 if (netns != wiphy_net(&rdev->wiphy))
173 return ERR_PTR(-ENODEV);
175 return rdev;
179 * This function returns a pointer to the driver
180 * that the genl_info item that is passed refers to.
182 * The result of this can be a PTR_ERR and hence must
183 * be checked with IS_ERR() for errors.
185 static struct cfg80211_registered_device *
186 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
188 return __cfg80211_rdev_from_attrs(netns, info->attrs);
191 /* policy for the attributes */
192 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
193 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
194 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
195 .len = 20-1 },
196 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
198 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
199 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
200 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
201 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
202 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
204 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
205 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
206 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
207 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
208 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
210 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
211 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
212 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
214 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
215 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
217 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
218 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
219 .len = WLAN_MAX_KEY_LEN },
220 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
221 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
222 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
223 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
224 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
226 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
227 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
228 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
229 .len = IEEE80211_MAX_DATA_LEN },
230 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
231 .len = IEEE80211_MAX_DATA_LEN },
232 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
233 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
234 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
235 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
236 .len = NL80211_MAX_SUPP_RATES },
237 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
238 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
239 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
240 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
241 .len = IEEE80211_MAX_MESH_ID_LEN },
242 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
244 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
245 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
247 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
248 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
249 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
250 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
251 .len = NL80211_MAX_SUPP_RATES },
252 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
254 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
255 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
257 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
259 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
260 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
262 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
263 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
265 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
266 .len = IEEE80211_MAX_SSID_LEN },
267 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
268 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
269 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
270 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
271 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
272 [NL80211_ATTR_STA_FLAGS2] = {
273 .len = sizeof(struct nl80211_sta_flag_update),
275 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
276 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
277 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
278 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
279 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
280 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
281 [NL80211_ATTR_PID] = { .type = NLA_U32 },
282 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
283 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
284 .len = WLAN_PMKID_LEN },
285 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
286 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
287 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
288 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
289 .len = IEEE80211_MAX_DATA_LEN },
290 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
291 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
292 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
293 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
294 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
295 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
296 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
297 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
298 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
299 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
300 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
301 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
302 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
303 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
304 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
305 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
306 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
307 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
308 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
309 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
310 .len = IEEE80211_MAX_DATA_LEN },
311 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
312 .len = IEEE80211_MAX_DATA_LEN },
313 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
314 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
315 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
316 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
317 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
318 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
319 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
320 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
321 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
322 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
323 .len = IEEE80211_MAX_DATA_LEN },
324 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
325 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
326 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
327 .len = NL80211_HT_CAPABILITY_LEN
329 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
330 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
331 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
332 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
333 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
334 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
335 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
336 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
337 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
338 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
339 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
340 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
341 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
342 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
343 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
344 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
346 .len = NL80211_VHT_CAPABILITY_LEN,
348 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
349 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
350 .len = IEEE80211_MAX_DATA_LEN },
351 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
354 /* policy for the key attributes */
355 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
356 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
357 [NL80211_KEY_IDX] = { .type = NLA_U8 },
358 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
359 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
360 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
361 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
362 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
363 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
366 /* policy for the key default flags */
367 static const struct nla_policy
368 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
369 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
370 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
373 /* policy for WoWLAN attributes */
374 static const struct nla_policy
375 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
376 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
377 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
378 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
379 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
380 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
381 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
387 static const struct nla_policy
388 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
389 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
390 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
391 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
392 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
393 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
394 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
395 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
396 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
398 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
399 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
401 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
402 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
403 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
406 /* policy for GTK rekey offload attributes */
407 static const struct nla_policy
408 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
409 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
410 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
411 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
414 static const struct nla_policy
415 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
416 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
417 .len = IEEE80211_MAX_SSID_LEN },
418 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
421 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
422 struct netlink_callback *cb,
423 struct cfg80211_registered_device **rdev,
424 struct wireless_dev **wdev)
426 int err;
428 rtnl_lock();
430 if (!cb->args[0]) {
431 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
432 nl80211_fam.attrbuf, nl80211_fam.maxattr,
433 nl80211_policy);
434 if (err)
435 goto out_unlock;
437 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
438 nl80211_fam.attrbuf);
439 if (IS_ERR(*wdev)) {
440 err = PTR_ERR(*wdev);
441 goto out_unlock;
443 *rdev = wiphy_to_dev((*wdev)->wiphy);
444 cb->args[0] = (*rdev)->wiphy_idx;
445 cb->args[1] = (*wdev)->identifier;
446 } else {
447 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
448 struct wireless_dev *tmp;
450 if (!wiphy) {
451 err = -ENODEV;
452 goto out_unlock;
454 *rdev = wiphy_to_dev(wiphy);
455 *wdev = NULL;
457 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
458 if (tmp->identifier == cb->args[1]) {
459 *wdev = tmp;
460 break;
464 if (!*wdev) {
465 err = -ENODEV;
466 goto out_unlock;
470 return 0;
471 out_unlock:
472 rtnl_unlock();
473 return err;
476 static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
478 rtnl_unlock();
481 /* IE validation */
482 static bool is_valid_ie_attr(const struct nlattr *attr)
484 const u8 *pos;
485 int len;
487 if (!attr)
488 return true;
490 pos = nla_data(attr);
491 len = nla_len(attr);
493 while (len) {
494 u8 elemlen;
496 if (len < 2)
497 return false;
498 len -= 2;
500 elemlen = pos[1];
501 if (elemlen > len)
502 return false;
504 len -= elemlen;
505 pos += 2 + elemlen;
508 return true;
511 /* message building helper */
512 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
513 int flags, u8 cmd)
515 /* since there is no private header just add the generic one */
516 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
519 static int nl80211_msg_put_channel(struct sk_buff *msg,
520 struct ieee80211_channel *chan,
521 bool large)
523 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
524 chan->center_freq))
525 goto nla_put_failure;
527 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
528 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
529 goto nla_put_failure;
530 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
531 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
532 goto nla_put_failure;
533 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
534 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
535 goto nla_put_failure;
536 if (chan->flags & IEEE80211_CHAN_RADAR) {
537 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
538 goto nla_put_failure;
539 if (large) {
540 u32 time;
542 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
545 chan->dfs_state))
546 goto nla_put_failure;
547 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
548 time))
549 goto nla_put_failure;
553 if (large) {
554 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
556 goto nla_put_failure;
557 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
558 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
559 goto nla_put_failure;
560 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
561 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
562 goto nla_put_failure;
563 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
564 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
565 goto nla_put_failure;
568 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
569 DBM_TO_MBM(chan->max_power)))
570 goto nla_put_failure;
572 return 0;
574 nla_put_failure:
575 return -ENOBUFS;
578 /* netlink command implementations */
580 struct key_parse {
581 struct key_params p;
582 int idx;
583 int type;
584 bool def, defmgmt;
585 bool def_uni, def_multi;
588 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
590 struct nlattr *tb[NL80211_KEY_MAX + 1];
591 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
592 nl80211_key_policy);
593 if (err)
594 return err;
596 k->def = !!tb[NL80211_KEY_DEFAULT];
597 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
599 if (k->def) {
600 k->def_uni = true;
601 k->def_multi = true;
603 if (k->defmgmt)
604 k->def_multi = true;
606 if (tb[NL80211_KEY_IDX])
607 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
609 if (tb[NL80211_KEY_DATA]) {
610 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
611 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
614 if (tb[NL80211_KEY_SEQ]) {
615 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
616 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
619 if (tb[NL80211_KEY_CIPHER])
620 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
622 if (tb[NL80211_KEY_TYPE]) {
623 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
624 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
625 return -EINVAL;
628 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
629 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
630 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
631 tb[NL80211_KEY_DEFAULT_TYPES],
632 nl80211_key_default_policy);
633 if (err)
634 return err;
636 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
637 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
640 return 0;
643 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
645 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
646 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
647 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
650 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
651 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
652 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
655 if (info->attrs[NL80211_ATTR_KEY_IDX])
656 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
658 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
659 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
661 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
662 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
664 if (k->def) {
665 k->def_uni = true;
666 k->def_multi = true;
668 if (k->defmgmt)
669 k->def_multi = true;
671 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
672 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
673 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
674 return -EINVAL;
677 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
678 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
679 int err = nla_parse_nested(
680 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
681 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
682 nl80211_key_default_policy);
683 if (err)
684 return err;
686 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
687 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
690 return 0;
693 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
695 int err;
697 memset(k, 0, sizeof(*k));
698 k->idx = -1;
699 k->type = -1;
701 if (info->attrs[NL80211_ATTR_KEY])
702 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
703 else
704 err = nl80211_parse_key_old(info, k);
706 if (err)
707 return err;
709 if (k->def && k->defmgmt)
710 return -EINVAL;
712 if (k->defmgmt) {
713 if (k->def_uni || !k->def_multi)
714 return -EINVAL;
717 if (k->idx != -1) {
718 if (k->defmgmt) {
719 if (k->idx < 4 || k->idx > 5)
720 return -EINVAL;
721 } else if (k->def) {
722 if (k->idx < 0 || k->idx > 3)
723 return -EINVAL;
724 } else {
725 if (k->idx < 0 || k->idx > 5)
726 return -EINVAL;
730 return 0;
733 static struct cfg80211_cached_keys *
734 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
735 struct nlattr *keys, bool *no_ht)
737 struct key_parse parse;
738 struct nlattr *key;
739 struct cfg80211_cached_keys *result;
740 int rem, err, def = 0;
742 result = kzalloc(sizeof(*result), GFP_KERNEL);
743 if (!result)
744 return ERR_PTR(-ENOMEM);
746 result->def = -1;
747 result->defmgmt = -1;
749 nla_for_each_nested(key, keys, rem) {
750 memset(&parse, 0, sizeof(parse));
751 parse.idx = -1;
753 err = nl80211_parse_key_new(key, &parse);
754 if (err)
755 goto error;
756 err = -EINVAL;
757 if (!parse.p.key)
758 goto error;
759 if (parse.idx < 0 || parse.idx > 4)
760 goto error;
761 if (parse.def) {
762 if (def)
763 goto error;
764 def = 1;
765 result->def = parse.idx;
766 if (!parse.def_uni || !parse.def_multi)
767 goto error;
768 } else if (parse.defmgmt)
769 goto error;
770 err = cfg80211_validate_key_settings(rdev, &parse.p,
771 parse.idx, false, NULL);
772 if (err)
773 goto error;
774 result->params[parse.idx].cipher = parse.p.cipher;
775 result->params[parse.idx].key_len = parse.p.key_len;
776 result->params[parse.idx].key = result->data[parse.idx];
777 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
779 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
780 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
781 if (no_ht)
782 *no_ht = true;
786 return result;
787 error:
788 kfree(result);
789 return ERR_PTR(err);
792 static int nl80211_key_allowed(struct wireless_dev *wdev)
794 ASSERT_WDEV_LOCK(wdev);
796 switch (wdev->iftype) {
797 case NL80211_IFTYPE_AP:
798 case NL80211_IFTYPE_AP_VLAN:
799 case NL80211_IFTYPE_P2P_GO:
800 case NL80211_IFTYPE_MESH_POINT:
801 break;
802 case NL80211_IFTYPE_ADHOC:
803 if (!wdev->current_bss)
804 return -ENOLINK;
805 break;
806 case NL80211_IFTYPE_STATION:
807 case NL80211_IFTYPE_P2P_CLIENT:
808 if (wdev->sme_state != CFG80211_SME_CONNECTED)
809 return -ENOLINK;
810 break;
811 default:
812 return -EINVAL;
815 return 0;
818 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
820 struct nlattr *nl_modes = nla_nest_start(msg, attr);
821 int i;
823 if (!nl_modes)
824 goto nla_put_failure;
826 i = 0;
827 while (ifmodes) {
828 if ((ifmodes & 1) && nla_put_flag(msg, i))
829 goto nla_put_failure;
830 ifmodes >>= 1;
831 i++;
834 nla_nest_end(msg, nl_modes);
835 return 0;
837 nla_put_failure:
838 return -ENOBUFS;
841 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
842 struct sk_buff *msg,
843 bool large)
845 struct nlattr *nl_combis;
846 int i, j;
848 nl_combis = nla_nest_start(msg,
849 NL80211_ATTR_INTERFACE_COMBINATIONS);
850 if (!nl_combis)
851 goto nla_put_failure;
853 for (i = 0; i < wiphy->n_iface_combinations; i++) {
854 const struct ieee80211_iface_combination *c;
855 struct nlattr *nl_combi, *nl_limits;
857 c = &wiphy->iface_combinations[i];
859 nl_combi = nla_nest_start(msg, i + 1);
860 if (!nl_combi)
861 goto nla_put_failure;
863 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
864 if (!nl_limits)
865 goto nla_put_failure;
867 for (j = 0; j < c->n_limits; j++) {
868 struct nlattr *nl_limit;
870 nl_limit = nla_nest_start(msg, j + 1);
871 if (!nl_limit)
872 goto nla_put_failure;
873 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
874 c->limits[j].max))
875 goto nla_put_failure;
876 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
877 c->limits[j].types))
878 goto nla_put_failure;
879 nla_nest_end(msg, nl_limit);
882 nla_nest_end(msg, nl_limits);
884 if (c->beacon_int_infra_match &&
885 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
886 goto nla_put_failure;
887 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
888 c->num_different_channels) ||
889 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
890 c->max_interfaces))
891 goto nla_put_failure;
892 if (large &&
893 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
894 c->radar_detect_widths))
895 goto nla_put_failure;
897 nla_nest_end(msg, nl_combi);
900 nla_nest_end(msg, nl_combis);
902 return 0;
903 nla_put_failure:
904 return -ENOBUFS;
907 #ifdef CONFIG_PM
908 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
909 struct sk_buff *msg)
911 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
912 struct nlattr *nl_tcp;
914 if (!tcp)
915 return 0;
917 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
918 if (!nl_tcp)
919 return -ENOBUFS;
921 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
922 tcp->data_payload_max))
923 return -ENOBUFS;
925 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
926 tcp->data_payload_max))
927 return -ENOBUFS;
929 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
930 return -ENOBUFS;
932 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
933 sizeof(*tcp->tok), tcp->tok))
934 return -ENOBUFS;
936 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
937 tcp->data_interval_max))
938 return -ENOBUFS;
940 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
941 tcp->wake_payload_max))
942 return -ENOBUFS;
944 nla_nest_end(msg, nl_tcp);
945 return 0;
948 static int nl80211_send_wowlan(struct sk_buff *msg,
949 struct cfg80211_registered_device *dev,
950 bool large)
952 struct nlattr *nl_wowlan;
954 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
955 return 0;
957 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
958 if (!nl_wowlan)
959 return -ENOBUFS;
961 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
962 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
963 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
964 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
965 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
966 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
967 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
968 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
969 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
970 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
971 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
972 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
973 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
974 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
975 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
976 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
977 return -ENOBUFS;
979 if (dev->wiphy.wowlan.n_patterns) {
980 struct nl80211_wowlan_pattern_support pat = {
981 .max_patterns = dev->wiphy.wowlan.n_patterns,
982 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
983 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
984 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
987 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
988 sizeof(pat), &pat))
989 return -ENOBUFS;
992 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
993 return -ENOBUFS;
995 nla_nest_end(msg, nl_wowlan);
997 return 0;
999 #endif
1001 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1002 struct ieee80211_supported_band *sband)
1004 struct nlattr *nl_rates, *nl_rate;
1005 struct ieee80211_rate *rate;
1006 int i;
1008 /* add HT info */
1009 if (sband->ht_cap.ht_supported &&
1010 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1011 sizeof(sband->ht_cap.mcs),
1012 &sband->ht_cap.mcs) ||
1013 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1014 sband->ht_cap.cap) ||
1015 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1016 sband->ht_cap.ampdu_factor) ||
1017 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1018 sband->ht_cap.ampdu_density)))
1019 return -ENOBUFS;
1021 /* add VHT info */
1022 if (sband->vht_cap.vht_supported &&
1023 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1024 sizeof(sband->vht_cap.vht_mcs),
1025 &sband->vht_cap.vht_mcs) ||
1026 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1027 sband->vht_cap.cap)))
1028 return -ENOBUFS;
1030 /* add bitrates */
1031 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1032 if (!nl_rates)
1033 return -ENOBUFS;
1035 for (i = 0; i < sband->n_bitrates; i++) {
1036 nl_rate = nla_nest_start(msg, i);
1037 if (!nl_rate)
1038 return -ENOBUFS;
1040 rate = &sband->bitrates[i];
1041 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1042 rate->bitrate))
1043 return -ENOBUFS;
1044 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1045 nla_put_flag(msg,
1046 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1047 return -ENOBUFS;
1049 nla_nest_end(msg, nl_rate);
1052 nla_nest_end(msg, nl_rates);
1054 return 0;
1057 static int
1058 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1059 const struct ieee80211_txrx_stypes *mgmt_stypes)
1061 u16 stypes;
1062 struct nlattr *nl_ftypes, *nl_ifs;
1063 enum nl80211_iftype ift;
1064 int i;
1066 if (!mgmt_stypes)
1067 return 0;
1069 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1070 if (!nl_ifs)
1071 return -ENOBUFS;
1073 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1074 nl_ftypes = nla_nest_start(msg, ift);
1075 if (!nl_ftypes)
1076 return -ENOBUFS;
1077 i = 0;
1078 stypes = mgmt_stypes[ift].tx;
1079 while (stypes) {
1080 if ((stypes & 1) &&
1081 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1082 (i << 4) | IEEE80211_FTYPE_MGMT))
1083 return -ENOBUFS;
1084 stypes >>= 1;
1085 i++;
1087 nla_nest_end(msg, nl_ftypes);
1090 nla_nest_end(msg, nl_ifs);
1092 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1093 if (!nl_ifs)
1094 return -ENOBUFS;
1096 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1097 nl_ftypes = nla_nest_start(msg, ift);
1098 if (!nl_ftypes)
1099 return -ENOBUFS;
1100 i = 0;
1101 stypes = mgmt_stypes[ift].rx;
1102 while (stypes) {
1103 if ((stypes & 1) &&
1104 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1105 (i << 4) | IEEE80211_FTYPE_MGMT))
1106 return -ENOBUFS;
1107 stypes >>= 1;
1108 i++;
1110 nla_nest_end(msg, nl_ftypes);
1112 nla_nest_end(msg, nl_ifs);
1114 return 0;
1117 static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1118 struct sk_buff *msg, u32 portid, u32 seq,
1119 int flags, bool split, long *split_start,
1120 long *band_start, long *chan_start)
1122 void *hdr;
1123 struct nlattr *nl_bands, *nl_band;
1124 struct nlattr *nl_freqs, *nl_freq;
1125 struct nlattr *nl_cmds;
1126 enum ieee80211_band band;
1127 struct ieee80211_channel *chan;
1128 int i;
1129 const struct ieee80211_txrx_stypes *mgmt_stypes =
1130 dev->wiphy.mgmt_stypes;
1131 long start = 0, start_chan = 0, start_band = 0;
1132 u32 features;
1134 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
1135 if (!hdr)
1136 return -ENOBUFS;
1138 /* allow always using the variables */
1139 if (!split) {
1140 split_start = &start;
1141 band_start = &start_band;
1142 chan_start = &start_chan;
1145 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
1146 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1147 wiphy_name(&dev->wiphy)) ||
1148 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1149 cfg80211_rdev_list_generation))
1150 goto nla_put_failure;
1152 switch (*split_start) {
1153 case 0:
1154 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1155 dev->wiphy.retry_short) ||
1156 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1157 dev->wiphy.retry_long) ||
1158 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1159 dev->wiphy.frag_threshold) ||
1160 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1161 dev->wiphy.rts_threshold) ||
1162 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1163 dev->wiphy.coverage_class) ||
1164 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1165 dev->wiphy.max_scan_ssids) ||
1166 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1167 dev->wiphy.max_sched_scan_ssids) ||
1168 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1169 dev->wiphy.max_scan_ie_len) ||
1170 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1171 dev->wiphy.max_sched_scan_ie_len) ||
1172 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1173 dev->wiphy.max_match_sets))
1174 goto nla_put_failure;
1176 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1177 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1178 goto nla_put_failure;
1179 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1180 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1181 goto nla_put_failure;
1182 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1183 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1184 goto nla_put_failure;
1185 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1186 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1187 goto nla_put_failure;
1188 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1189 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1190 goto nla_put_failure;
1191 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1192 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1193 goto nla_put_failure;
1195 (*split_start)++;
1196 if (split)
1197 break;
1198 case 1:
1199 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1200 sizeof(u32) * dev->wiphy.n_cipher_suites,
1201 dev->wiphy.cipher_suites))
1202 goto nla_put_failure;
1204 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1205 dev->wiphy.max_num_pmkids))
1206 goto nla_put_failure;
1208 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1209 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1210 goto nla_put_failure;
1212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1213 dev->wiphy.available_antennas_tx) ||
1214 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1215 dev->wiphy.available_antennas_rx))
1216 goto nla_put_failure;
1218 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1219 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1220 dev->wiphy.probe_resp_offload))
1221 goto nla_put_failure;
1223 if ((dev->wiphy.available_antennas_tx ||
1224 dev->wiphy.available_antennas_rx) &&
1225 dev->ops->get_antenna) {
1226 u32 tx_ant = 0, rx_ant = 0;
1227 int res;
1228 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1229 if (!res) {
1230 if (nla_put_u32(msg,
1231 NL80211_ATTR_WIPHY_ANTENNA_TX,
1232 tx_ant) ||
1233 nla_put_u32(msg,
1234 NL80211_ATTR_WIPHY_ANTENNA_RX,
1235 rx_ant))
1236 goto nla_put_failure;
1240 (*split_start)++;
1241 if (split)
1242 break;
1243 case 2:
1244 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1245 dev->wiphy.interface_modes))
1246 goto nla_put_failure;
1247 (*split_start)++;
1248 if (split)
1249 break;
1250 case 3:
1251 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1252 if (!nl_bands)
1253 goto nla_put_failure;
1255 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1256 struct ieee80211_supported_band *sband;
1258 sband = dev->wiphy.bands[band];
1260 if (!sband)
1261 continue;
1263 nl_band = nla_nest_start(msg, band);
1264 if (!nl_band)
1265 goto nla_put_failure;
1267 switch (*chan_start) {
1268 case 0:
1269 if (nl80211_send_band_rateinfo(msg, sband))
1270 goto nla_put_failure;
1271 (*chan_start)++;
1272 if (split)
1273 break;
1274 default:
1275 /* add frequencies */
1276 nl_freqs = nla_nest_start(
1277 msg, NL80211_BAND_ATTR_FREQS);
1278 if (!nl_freqs)
1279 goto nla_put_failure;
1281 for (i = *chan_start - 1;
1282 i < sband->n_channels;
1283 i++) {
1284 nl_freq = nla_nest_start(msg, i);
1285 if (!nl_freq)
1286 goto nla_put_failure;
1288 chan = &sband->channels[i];
1290 if (nl80211_msg_put_channel(msg, chan,
1291 split))
1292 goto nla_put_failure;
1294 nla_nest_end(msg, nl_freq);
1295 if (split)
1296 break;
1298 if (i < sband->n_channels)
1299 *chan_start = i + 2;
1300 else
1301 *chan_start = 0;
1302 nla_nest_end(msg, nl_freqs);
1305 nla_nest_end(msg, nl_band);
1307 if (split) {
1308 /* start again here */
1309 if (*chan_start)
1310 band--;
1311 break;
1314 nla_nest_end(msg, nl_bands);
1316 if (band < IEEE80211_NUM_BANDS)
1317 *band_start = band + 1;
1318 else
1319 *band_start = 0;
1321 /* if bands & channels are done, continue outside */
1322 if (*band_start == 0 && *chan_start == 0)
1323 (*split_start)++;
1324 if (split)
1325 break;
1326 case 4:
1327 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1328 if (!nl_cmds)
1329 goto nla_put_failure;
1331 i = 0;
1332 #define CMD(op, n) \
1333 do { \
1334 if (dev->ops->op) { \
1335 i++; \
1336 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1337 goto nla_put_failure; \
1339 } while (0)
1341 CMD(add_virtual_intf, NEW_INTERFACE);
1342 CMD(change_virtual_intf, SET_INTERFACE);
1343 CMD(add_key, NEW_KEY);
1344 CMD(start_ap, START_AP);
1345 CMD(add_station, NEW_STATION);
1346 CMD(add_mpath, NEW_MPATH);
1347 CMD(update_mesh_config, SET_MESH_CONFIG);
1348 CMD(change_bss, SET_BSS);
1349 CMD(auth, AUTHENTICATE);
1350 CMD(assoc, ASSOCIATE);
1351 CMD(deauth, DEAUTHENTICATE);
1352 CMD(disassoc, DISASSOCIATE);
1353 CMD(join_ibss, JOIN_IBSS);
1354 CMD(join_mesh, JOIN_MESH);
1355 CMD(set_pmksa, SET_PMKSA);
1356 CMD(del_pmksa, DEL_PMKSA);
1357 CMD(flush_pmksa, FLUSH_PMKSA);
1358 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1359 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1360 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1361 CMD(mgmt_tx, FRAME);
1362 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1363 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1364 i++;
1365 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1366 goto nla_put_failure;
1368 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1369 dev->ops->join_mesh) {
1370 i++;
1371 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1372 goto nla_put_failure;
1374 CMD(set_wds_peer, SET_WDS_PEER);
1375 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1376 CMD(tdls_mgmt, TDLS_MGMT);
1377 CMD(tdls_oper, TDLS_OPER);
1379 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1380 CMD(sched_scan_start, START_SCHED_SCAN);
1381 CMD(probe_client, PROBE_CLIENT);
1382 CMD(set_noack_map, SET_NOACK_MAP);
1383 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1384 i++;
1385 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1386 goto nla_put_failure;
1388 CMD(start_p2p_device, START_P2P_DEVICE);
1389 CMD(set_mcast_rate, SET_MCAST_RATE);
1390 if (split) {
1391 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1392 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1395 #ifdef CONFIG_NL80211_TESTMODE
1396 CMD(testmode_cmd, TESTMODE);
1397 #endif
1399 #undef CMD
1401 if (dev->ops->connect || dev->ops->auth) {
1402 i++;
1403 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1404 goto nla_put_failure;
1407 if (dev->ops->disconnect || dev->ops->deauth) {
1408 i++;
1409 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1410 goto nla_put_failure;
1413 nla_nest_end(msg, nl_cmds);
1414 (*split_start)++;
1415 if (split)
1416 break;
1417 case 5:
1418 if (dev->ops->remain_on_channel &&
1419 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1420 nla_put_u32(msg,
1421 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1422 dev->wiphy.max_remain_on_channel_duration))
1423 goto nla_put_failure;
1425 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1426 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1427 goto nla_put_failure;
1429 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1430 goto nla_put_failure;
1431 (*split_start)++;
1432 if (split)
1433 break;
1434 case 6:
1435 #ifdef CONFIG_PM
1436 if (nl80211_send_wowlan(msg, dev, split))
1437 goto nla_put_failure;
1438 (*split_start)++;
1439 if (split)
1440 break;
1441 #else
1442 (*split_start)++;
1443 #endif
1444 case 7:
1445 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1446 dev->wiphy.software_iftypes))
1447 goto nla_put_failure;
1449 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
1450 goto nla_put_failure;
1452 (*split_start)++;
1453 if (split)
1454 break;
1455 case 8:
1456 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1457 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1458 dev->wiphy.ap_sme_capa))
1459 goto nla_put_failure;
1461 features = dev->wiphy.features;
1463 * We can only add the per-channel limit information if the
1464 * dump is split, otherwise it makes it too big. Therefore
1465 * only advertise it in that case.
1467 if (split)
1468 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1469 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1470 goto nla_put_failure;
1472 if (dev->wiphy.ht_capa_mod_mask &&
1473 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1474 sizeof(*dev->wiphy.ht_capa_mod_mask),
1475 dev->wiphy.ht_capa_mod_mask))
1476 goto nla_put_failure;
1478 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1479 dev->wiphy.max_acl_mac_addrs &&
1480 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1481 dev->wiphy.max_acl_mac_addrs))
1482 goto nla_put_failure;
1485 * Any information below this point is only available to
1486 * applications that can deal with it being split. This
1487 * helps ensure that newly added capabilities don't break
1488 * older tools by overrunning their buffers.
1490 * We still increment split_start so that in the split
1491 * case we'll continue with more data in the next round,
1492 * but break unconditionally so unsplit data stops here.
1494 (*split_start)++;
1495 break;
1496 case 9:
1497 if (dev->wiphy.extended_capabilities &&
1498 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1499 dev->wiphy.extended_capabilities_len,
1500 dev->wiphy.extended_capabilities) ||
1501 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1502 dev->wiphy.extended_capabilities_len,
1503 dev->wiphy.extended_capabilities_mask)))
1504 goto nla_put_failure;
1506 if (dev->wiphy.vht_capa_mod_mask &&
1507 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1508 sizeof(*dev->wiphy.vht_capa_mod_mask),
1509 dev->wiphy.vht_capa_mod_mask))
1510 goto nla_put_failure;
1512 /* done */
1513 *split_start = 0;
1514 break;
1516 return genlmsg_end(msg, hdr);
1518 nla_put_failure:
1519 genlmsg_cancel(msg, hdr);
1520 return -EMSGSIZE;
1523 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1525 int idx = 0, ret;
1526 int start = cb->args[0];
1527 struct cfg80211_registered_device *dev;
1528 s64 filter_wiphy = -1;
1529 bool split = false;
1530 struct nlattr **tb;
1531 int res;
1533 /* will be zeroed in nlmsg_parse() */
1534 tb = kmalloc(sizeof(*tb) * (NL80211_ATTR_MAX + 1), GFP_KERNEL);
1535 if (!tb)
1536 return -ENOMEM;
1538 rtnl_lock();
1540 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1541 tb, NL80211_ATTR_MAX, nl80211_policy);
1542 if (res == 0) {
1543 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1544 if (tb[NL80211_ATTR_WIPHY])
1545 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1546 if (tb[NL80211_ATTR_WDEV])
1547 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1548 if (tb[NL80211_ATTR_IFINDEX]) {
1549 struct net_device *netdev;
1550 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1552 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1553 if (!netdev) {
1554 rtnl_unlock();
1555 kfree(tb);
1556 return -ENODEV;
1558 if (netdev->ieee80211_ptr) {
1559 dev = wiphy_to_dev(
1560 netdev->ieee80211_ptr->wiphy);
1561 filter_wiphy = dev->wiphy_idx;
1563 dev_put(netdev);
1566 kfree(tb);
1568 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
1569 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1570 continue;
1571 if (++idx <= start)
1572 continue;
1573 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1574 continue;
1575 /* attempt to fit multiple wiphy data chunks into the skb */
1576 do {
1577 ret = nl80211_send_wiphy(dev, skb,
1578 NETLINK_CB(cb->skb).portid,
1579 cb->nlh->nlmsg_seq,
1580 NLM_F_MULTI,
1581 split, &cb->args[1],
1582 &cb->args[2],
1583 &cb->args[3]);
1584 if (ret < 0) {
1586 * If sending the wiphy data didn't fit (ENOBUFS
1587 * or EMSGSIZE returned), this SKB is still
1588 * empty (so it's not too big because another
1589 * wiphy dataset is already in the skb) and
1590 * we've not tried to adjust the dump allocation
1591 * yet ... then adjust the alloc size to be
1592 * bigger, and return 1 but with the empty skb.
1593 * This results in an empty message being RX'ed
1594 * in userspace, but that is ignored.
1596 * We can then retry with the larger buffer.
1598 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1599 !skb->len &&
1600 cb->min_dump_alloc < 4096) {
1601 cb->min_dump_alloc = 4096;
1602 rtnl_unlock();
1603 return 1;
1605 idx--;
1606 break;
1608 } while (cb->args[1] > 0);
1609 break;
1611 rtnl_unlock();
1613 cb->args[0] = idx;
1615 return skb->len;
1618 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1620 struct sk_buff *msg;
1621 struct cfg80211_registered_device *dev = info->user_ptr[0];
1623 msg = nlmsg_new(4096, GFP_KERNEL);
1624 if (!msg)
1625 return -ENOMEM;
1627 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1628 false, NULL, NULL, NULL) < 0) {
1629 nlmsg_free(msg);
1630 return -ENOBUFS;
1633 return genlmsg_reply(msg, info);
1636 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1637 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1638 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1639 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1640 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1641 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1644 static int parse_txq_params(struct nlattr *tb[],
1645 struct ieee80211_txq_params *txq_params)
1647 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1648 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1649 !tb[NL80211_TXQ_ATTR_AIFS])
1650 return -EINVAL;
1652 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
1653 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1654 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1655 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1656 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1658 if (txq_params->ac >= NL80211_NUM_ACS)
1659 return -EINVAL;
1661 return 0;
1664 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1667 * You can only set the channel explicitly for WDS interfaces,
1668 * all others have their channel managed via their respective
1669 * "establish a connection" command (connect, join, ...)
1671 * For AP/GO and mesh mode, the channel can be set with the
1672 * channel userspace API, but is only stored and passed to the
1673 * low-level driver when the AP starts or the mesh is joined.
1674 * This is for backward compatibility, userspace can also give
1675 * the channel in the start-ap or join-mesh commands instead.
1677 * Monitors are special as they are normally slaved to
1678 * whatever else is going on, so they have their own special
1679 * operation to set the monitor channel if possible.
1681 return !wdev ||
1682 wdev->iftype == NL80211_IFTYPE_AP ||
1683 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1684 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1685 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1688 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1689 struct genl_info *info,
1690 struct cfg80211_chan_def *chandef)
1692 u32 control_freq;
1694 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1695 return -EINVAL;
1697 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1699 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
1700 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1701 chandef->center_freq1 = control_freq;
1702 chandef->center_freq2 = 0;
1704 /* Primary channel not allowed */
1705 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1706 return -EINVAL;
1708 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1709 enum nl80211_channel_type chantype;
1711 chantype = nla_get_u32(
1712 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1714 switch (chantype) {
1715 case NL80211_CHAN_NO_HT:
1716 case NL80211_CHAN_HT20:
1717 case NL80211_CHAN_HT40PLUS:
1718 case NL80211_CHAN_HT40MINUS:
1719 cfg80211_chandef_create(chandef, chandef->chan,
1720 chantype);
1721 break;
1722 default:
1723 return -EINVAL;
1725 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1726 chandef->width =
1727 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1728 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1729 chandef->center_freq1 =
1730 nla_get_u32(
1731 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1732 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1733 chandef->center_freq2 =
1734 nla_get_u32(
1735 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1738 if (!cfg80211_chandef_valid(chandef))
1739 return -EINVAL;
1741 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1742 IEEE80211_CHAN_DISABLED))
1743 return -EINVAL;
1745 return 0;
1748 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1749 struct wireless_dev *wdev,
1750 struct genl_info *info)
1752 struct cfg80211_chan_def chandef;
1753 int result;
1754 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1756 if (wdev)
1757 iftype = wdev->iftype;
1759 if (!nl80211_can_set_dev_channel(wdev))
1760 return -EOPNOTSUPP;
1762 result = nl80211_parse_chandef(rdev, info, &chandef);
1763 if (result)
1764 return result;
1766 switch (iftype) {
1767 case NL80211_IFTYPE_AP:
1768 case NL80211_IFTYPE_P2P_GO:
1769 if (wdev->beacon_interval) {
1770 result = -EBUSY;
1771 break;
1773 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
1774 result = -EINVAL;
1775 break;
1777 wdev->preset_chandef = chandef;
1778 result = 0;
1779 break;
1780 case NL80211_IFTYPE_MESH_POINT:
1781 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
1782 break;
1783 case NL80211_IFTYPE_MONITOR:
1784 result = cfg80211_set_monitor_channel(rdev, &chandef);
1785 break;
1786 default:
1787 result = -EINVAL;
1790 return result;
1793 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1795 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1796 struct net_device *netdev = info->user_ptr[1];
1798 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1801 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1804 struct net_device *dev = info->user_ptr[1];
1805 struct wireless_dev *wdev = dev->ieee80211_ptr;
1806 const u8 *bssid;
1808 if (!info->attrs[NL80211_ATTR_MAC])
1809 return -EINVAL;
1811 if (netif_running(dev))
1812 return -EBUSY;
1814 if (!rdev->ops->set_wds_peer)
1815 return -EOPNOTSUPP;
1817 if (wdev->iftype != NL80211_IFTYPE_WDS)
1818 return -EOPNOTSUPP;
1820 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1821 return rdev_set_wds_peer(rdev, dev, bssid);
1825 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1827 struct cfg80211_registered_device *rdev;
1828 struct net_device *netdev = NULL;
1829 struct wireless_dev *wdev;
1830 int result = 0, rem_txq_params = 0;
1831 struct nlattr *nl_txq_params;
1832 u32 changed;
1833 u8 retry_short = 0, retry_long = 0;
1834 u32 frag_threshold = 0, rts_threshold = 0;
1835 u8 coverage_class = 0;
1837 ASSERT_RTNL();
1840 * Try to find the wiphy and netdev. Normally this
1841 * function shouldn't need the netdev, but this is
1842 * done for backward compatibility -- previously
1843 * setting the channel was done per wiphy, but now
1844 * it is per netdev. Previous userland like hostapd
1845 * also passed a netdev to set_wiphy, so that it is
1846 * possible to let that go to the right netdev!
1849 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1850 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1852 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1853 if (netdev && netdev->ieee80211_ptr)
1854 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1855 else
1856 netdev = NULL;
1859 if (!netdev) {
1860 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1861 info->attrs);
1862 if (IS_ERR(rdev))
1863 return PTR_ERR(rdev);
1864 wdev = NULL;
1865 netdev = NULL;
1866 result = 0;
1867 } else
1868 wdev = netdev->ieee80211_ptr;
1871 * end workaround code, by now the rdev is available
1872 * and locked, and wdev may or may not be NULL.
1875 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1876 result = cfg80211_dev_rename(
1877 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1879 if (result)
1880 goto bad_res;
1882 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1883 struct ieee80211_txq_params txq_params;
1884 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1886 if (!rdev->ops->set_txq_params) {
1887 result = -EOPNOTSUPP;
1888 goto bad_res;
1891 if (!netdev) {
1892 result = -EINVAL;
1893 goto bad_res;
1896 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1897 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1898 result = -EINVAL;
1899 goto bad_res;
1902 if (!netif_running(netdev)) {
1903 result = -ENETDOWN;
1904 goto bad_res;
1907 nla_for_each_nested(nl_txq_params,
1908 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1909 rem_txq_params) {
1910 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1911 nla_data(nl_txq_params),
1912 nla_len(nl_txq_params),
1913 txq_params_policy);
1914 result = parse_txq_params(tb, &txq_params);
1915 if (result)
1916 goto bad_res;
1918 result = rdev_set_txq_params(rdev, netdev,
1919 &txq_params);
1920 if (result)
1921 goto bad_res;
1925 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1926 result = __nl80211_set_channel(rdev,
1927 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1928 info);
1929 if (result)
1930 goto bad_res;
1933 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1934 struct wireless_dev *txp_wdev = wdev;
1935 enum nl80211_tx_power_setting type;
1936 int idx, mbm = 0;
1938 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1939 txp_wdev = NULL;
1941 if (!rdev->ops->set_tx_power) {
1942 result = -EOPNOTSUPP;
1943 goto bad_res;
1946 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1947 type = nla_get_u32(info->attrs[idx]);
1949 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1950 (type != NL80211_TX_POWER_AUTOMATIC)) {
1951 result = -EINVAL;
1952 goto bad_res;
1955 if (type != NL80211_TX_POWER_AUTOMATIC) {
1956 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1957 mbm = nla_get_u32(info->attrs[idx]);
1960 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
1961 if (result)
1962 goto bad_res;
1965 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1966 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1967 u32 tx_ant, rx_ant;
1968 if ((!rdev->wiphy.available_antennas_tx &&
1969 !rdev->wiphy.available_antennas_rx) ||
1970 !rdev->ops->set_antenna) {
1971 result = -EOPNOTSUPP;
1972 goto bad_res;
1975 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1976 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1978 /* reject antenna configurations which don't match the
1979 * available antenna masks, except for the "all" mask */
1980 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1981 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1982 result = -EINVAL;
1983 goto bad_res;
1986 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1987 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1989 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
1990 if (result)
1991 goto bad_res;
1994 changed = 0;
1996 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1997 retry_short = nla_get_u8(
1998 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1999 if (retry_short == 0) {
2000 result = -EINVAL;
2001 goto bad_res;
2003 changed |= WIPHY_PARAM_RETRY_SHORT;
2006 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2007 retry_long = nla_get_u8(
2008 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2009 if (retry_long == 0) {
2010 result = -EINVAL;
2011 goto bad_res;
2013 changed |= WIPHY_PARAM_RETRY_LONG;
2016 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2017 frag_threshold = nla_get_u32(
2018 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2019 if (frag_threshold < 256) {
2020 result = -EINVAL;
2021 goto bad_res;
2023 if (frag_threshold != (u32) -1) {
2025 * Fragments (apart from the last one) are required to
2026 * have even length. Make the fragmentation code
2027 * simpler by stripping LSB should someone try to use
2028 * odd threshold value.
2030 frag_threshold &= ~0x1;
2032 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2035 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2036 rts_threshold = nla_get_u32(
2037 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2038 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2041 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2042 coverage_class = nla_get_u8(
2043 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2044 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2047 if (changed) {
2048 u8 old_retry_short, old_retry_long;
2049 u32 old_frag_threshold, old_rts_threshold;
2050 u8 old_coverage_class;
2052 if (!rdev->ops->set_wiphy_params) {
2053 result = -EOPNOTSUPP;
2054 goto bad_res;
2057 old_retry_short = rdev->wiphy.retry_short;
2058 old_retry_long = rdev->wiphy.retry_long;
2059 old_frag_threshold = rdev->wiphy.frag_threshold;
2060 old_rts_threshold = rdev->wiphy.rts_threshold;
2061 old_coverage_class = rdev->wiphy.coverage_class;
2063 if (changed & WIPHY_PARAM_RETRY_SHORT)
2064 rdev->wiphy.retry_short = retry_short;
2065 if (changed & WIPHY_PARAM_RETRY_LONG)
2066 rdev->wiphy.retry_long = retry_long;
2067 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2068 rdev->wiphy.frag_threshold = frag_threshold;
2069 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2070 rdev->wiphy.rts_threshold = rts_threshold;
2071 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2072 rdev->wiphy.coverage_class = coverage_class;
2074 result = rdev_set_wiphy_params(rdev, changed);
2075 if (result) {
2076 rdev->wiphy.retry_short = old_retry_short;
2077 rdev->wiphy.retry_long = old_retry_long;
2078 rdev->wiphy.frag_threshold = old_frag_threshold;
2079 rdev->wiphy.rts_threshold = old_rts_threshold;
2080 rdev->wiphy.coverage_class = old_coverage_class;
2084 bad_res:
2085 if (netdev)
2086 dev_put(netdev);
2087 return result;
2090 static inline u64 wdev_id(struct wireless_dev *wdev)
2092 return (u64)wdev->identifier |
2093 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2096 static int nl80211_send_chandef(struct sk_buff *msg,
2097 struct cfg80211_chan_def *chandef)
2099 WARN_ON(!cfg80211_chandef_valid(chandef));
2101 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2102 chandef->chan->center_freq))
2103 return -ENOBUFS;
2104 switch (chandef->width) {
2105 case NL80211_CHAN_WIDTH_20_NOHT:
2106 case NL80211_CHAN_WIDTH_20:
2107 case NL80211_CHAN_WIDTH_40:
2108 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2109 cfg80211_get_chandef_type(chandef)))
2110 return -ENOBUFS;
2111 break;
2112 default:
2113 break;
2115 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2116 return -ENOBUFS;
2117 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2118 return -ENOBUFS;
2119 if (chandef->center_freq2 &&
2120 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2121 return -ENOBUFS;
2122 return 0;
2125 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2126 struct cfg80211_registered_device *rdev,
2127 struct wireless_dev *wdev)
2129 struct net_device *dev = wdev->netdev;
2130 void *hdr;
2132 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
2133 if (!hdr)
2134 return -1;
2136 if (dev &&
2137 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2138 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2139 goto nla_put_failure;
2141 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2142 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2143 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
2144 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2145 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2146 rdev->devlist_generation ^
2147 (cfg80211_rdev_list_generation << 2)))
2148 goto nla_put_failure;
2150 if (rdev->ops->get_channel) {
2151 int ret;
2152 struct cfg80211_chan_def chandef;
2154 ret = rdev_get_channel(rdev, wdev, &chandef);
2155 if (ret == 0) {
2156 if (nl80211_send_chandef(msg, &chandef))
2157 goto nla_put_failure;
2161 if (wdev->ssid_len) {
2162 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2163 goto nla_put_failure;
2166 return genlmsg_end(msg, hdr);
2168 nla_put_failure:
2169 genlmsg_cancel(msg, hdr);
2170 return -EMSGSIZE;
2173 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2175 int wp_idx = 0;
2176 int if_idx = 0;
2177 int wp_start = cb->args[0];
2178 int if_start = cb->args[1];
2179 struct cfg80211_registered_device *rdev;
2180 struct wireless_dev *wdev;
2182 rtnl_lock();
2183 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2184 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2185 continue;
2186 if (wp_idx < wp_start) {
2187 wp_idx++;
2188 continue;
2190 if_idx = 0;
2192 list_for_each_entry(wdev, &rdev->wdev_list, list) {
2193 if (if_idx < if_start) {
2194 if_idx++;
2195 continue;
2197 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2198 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2199 rdev, wdev) < 0) {
2200 goto out;
2202 if_idx++;
2205 wp_idx++;
2207 out:
2208 rtnl_unlock();
2210 cb->args[0] = wp_idx;
2211 cb->args[1] = if_idx;
2213 return skb->len;
2216 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2218 struct sk_buff *msg;
2219 struct cfg80211_registered_device *dev = info->user_ptr[0];
2220 struct wireless_dev *wdev = info->user_ptr[1];
2222 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2223 if (!msg)
2224 return -ENOMEM;
2226 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2227 dev, wdev) < 0) {
2228 nlmsg_free(msg);
2229 return -ENOBUFS;
2232 return genlmsg_reply(msg, info);
2235 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2236 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2237 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2238 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2239 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2240 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2241 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2244 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2246 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2247 int flag;
2249 *mntrflags = 0;
2251 if (!nla)
2252 return -EINVAL;
2254 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2255 nla, mntr_flags_policy))
2256 return -EINVAL;
2258 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2259 if (flags[flag])
2260 *mntrflags |= (1<<flag);
2262 return 0;
2265 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2266 struct net_device *netdev, u8 use_4addr,
2267 enum nl80211_iftype iftype)
2269 if (!use_4addr) {
2270 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2271 return -EBUSY;
2272 return 0;
2275 switch (iftype) {
2276 case NL80211_IFTYPE_AP_VLAN:
2277 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2278 return 0;
2279 break;
2280 case NL80211_IFTYPE_STATION:
2281 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2282 return 0;
2283 break;
2284 default:
2285 break;
2288 return -EOPNOTSUPP;
2291 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2293 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2294 struct vif_params params;
2295 int err;
2296 enum nl80211_iftype otype, ntype;
2297 struct net_device *dev = info->user_ptr[1];
2298 u32 _flags, *flags = NULL;
2299 bool change = false;
2301 memset(&params, 0, sizeof(params));
2303 otype = ntype = dev->ieee80211_ptr->iftype;
2305 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2306 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2307 if (otype != ntype)
2308 change = true;
2309 if (ntype > NL80211_IFTYPE_MAX)
2310 return -EINVAL;
2313 if (info->attrs[NL80211_ATTR_MESH_ID]) {
2314 struct wireless_dev *wdev = dev->ieee80211_ptr;
2316 if (ntype != NL80211_IFTYPE_MESH_POINT)
2317 return -EINVAL;
2318 if (netif_running(dev))
2319 return -EBUSY;
2321 wdev_lock(wdev);
2322 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2323 IEEE80211_MAX_MESH_ID_LEN);
2324 wdev->mesh_id_up_len =
2325 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2326 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2327 wdev->mesh_id_up_len);
2328 wdev_unlock(wdev);
2331 if (info->attrs[NL80211_ATTR_4ADDR]) {
2332 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2333 change = true;
2334 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2335 if (err)
2336 return err;
2337 } else {
2338 params.use_4addr = -1;
2341 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2342 if (ntype != NL80211_IFTYPE_MONITOR)
2343 return -EINVAL;
2344 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2345 &_flags);
2346 if (err)
2347 return err;
2349 flags = &_flags;
2350 change = true;
2353 if (flags && (*flags & NL80211_MNTR_FLAG_ACTIVE) &&
2354 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2355 return -EOPNOTSUPP;
2357 if (change)
2358 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
2359 else
2360 err = 0;
2362 if (!err && params.use_4addr != -1)
2363 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2365 return err;
2368 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2370 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2371 struct vif_params params;
2372 struct wireless_dev *wdev;
2373 struct sk_buff *msg;
2374 int err;
2375 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2376 u32 flags;
2378 memset(&params, 0, sizeof(params));
2380 if (!info->attrs[NL80211_ATTR_IFNAME])
2381 return -EINVAL;
2383 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2384 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2385 if (type > NL80211_IFTYPE_MAX)
2386 return -EINVAL;
2389 if (!rdev->ops->add_virtual_intf ||
2390 !(rdev->wiphy.interface_modes & (1 << type)))
2391 return -EOPNOTSUPP;
2393 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2394 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2395 ETH_ALEN);
2396 if (!is_valid_ether_addr(params.macaddr))
2397 return -EADDRNOTAVAIL;
2400 if (info->attrs[NL80211_ATTR_4ADDR]) {
2401 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2402 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2403 if (err)
2404 return err;
2407 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2408 if (!msg)
2409 return -ENOMEM;
2411 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2412 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2413 &flags);
2415 if (!err && (flags & NL80211_MNTR_FLAG_ACTIVE) &&
2416 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2417 return -EOPNOTSUPP;
2419 wdev = rdev_add_virtual_intf(rdev,
2420 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2421 type, err ? NULL : &flags, &params);
2422 if (IS_ERR(wdev)) {
2423 nlmsg_free(msg);
2424 return PTR_ERR(wdev);
2427 switch (type) {
2428 case NL80211_IFTYPE_MESH_POINT:
2429 if (!info->attrs[NL80211_ATTR_MESH_ID])
2430 break;
2431 wdev_lock(wdev);
2432 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2433 IEEE80211_MAX_MESH_ID_LEN);
2434 wdev->mesh_id_up_len =
2435 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2436 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2437 wdev->mesh_id_up_len);
2438 wdev_unlock(wdev);
2439 break;
2440 case NL80211_IFTYPE_P2P_DEVICE:
2442 * P2P Device doesn't have a netdev, so doesn't go
2443 * through the netdev notifier and must be added here
2445 mutex_init(&wdev->mtx);
2446 INIT_LIST_HEAD(&wdev->event_list);
2447 spin_lock_init(&wdev->event_lock);
2448 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2449 spin_lock_init(&wdev->mgmt_registrations_lock);
2451 wdev->identifier = ++rdev->wdev_id;
2452 list_add_rcu(&wdev->list, &rdev->wdev_list);
2453 rdev->devlist_generation++;
2454 break;
2455 default:
2456 break;
2459 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2460 rdev, wdev) < 0) {
2461 nlmsg_free(msg);
2462 return -ENOBUFS;
2465 return genlmsg_reply(msg, info);
2468 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2470 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2471 struct wireless_dev *wdev = info->user_ptr[1];
2473 if (!rdev->ops->del_virtual_intf)
2474 return -EOPNOTSUPP;
2477 * If we remove a wireless device without a netdev then clear
2478 * user_ptr[1] so that nl80211_post_doit won't dereference it
2479 * to check if it needs to do dev_put(). Otherwise it crashes
2480 * since the wdev has been freed, unlike with a netdev where
2481 * we need the dev_put() for the netdev to really be freed.
2483 if (!wdev->netdev)
2484 info->user_ptr[1] = NULL;
2486 return rdev_del_virtual_intf(rdev, wdev);
2489 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2492 struct net_device *dev = info->user_ptr[1];
2493 u16 noack_map;
2495 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2496 return -EINVAL;
2498 if (!rdev->ops->set_noack_map)
2499 return -EOPNOTSUPP;
2501 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2503 return rdev_set_noack_map(rdev, dev, noack_map);
2506 struct get_key_cookie {
2507 struct sk_buff *msg;
2508 int error;
2509 int idx;
2512 static void get_key_callback(void *c, struct key_params *params)
2514 struct nlattr *key;
2515 struct get_key_cookie *cookie = c;
2517 if ((params->key &&
2518 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2519 params->key_len, params->key)) ||
2520 (params->seq &&
2521 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2522 params->seq_len, params->seq)) ||
2523 (params->cipher &&
2524 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2525 params->cipher)))
2526 goto nla_put_failure;
2528 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2529 if (!key)
2530 goto nla_put_failure;
2532 if ((params->key &&
2533 nla_put(cookie->msg, NL80211_KEY_DATA,
2534 params->key_len, params->key)) ||
2535 (params->seq &&
2536 nla_put(cookie->msg, NL80211_KEY_SEQ,
2537 params->seq_len, params->seq)) ||
2538 (params->cipher &&
2539 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2540 params->cipher)))
2541 goto nla_put_failure;
2543 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2544 goto nla_put_failure;
2546 nla_nest_end(cookie->msg, key);
2548 return;
2549 nla_put_failure:
2550 cookie->error = 1;
2553 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2555 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2556 int err;
2557 struct net_device *dev = info->user_ptr[1];
2558 u8 key_idx = 0;
2559 const u8 *mac_addr = NULL;
2560 bool pairwise;
2561 struct get_key_cookie cookie = {
2562 .error = 0,
2564 void *hdr;
2565 struct sk_buff *msg;
2567 if (info->attrs[NL80211_ATTR_KEY_IDX])
2568 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2570 if (key_idx > 5)
2571 return -EINVAL;
2573 if (info->attrs[NL80211_ATTR_MAC])
2574 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2576 pairwise = !!mac_addr;
2577 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2578 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2579 if (kt >= NUM_NL80211_KEYTYPES)
2580 return -EINVAL;
2581 if (kt != NL80211_KEYTYPE_GROUP &&
2582 kt != NL80211_KEYTYPE_PAIRWISE)
2583 return -EINVAL;
2584 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2587 if (!rdev->ops->get_key)
2588 return -EOPNOTSUPP;
2590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2591 if (!msg)
2592 return -ENOMEM;
2594 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
2595 NL80211_CMD_NEW_KEY);
2596 if (IS_ERR(hdr))
2597 return PTR_ERR(hdr);
2599 cookie.msg = msg;
2600 cookie.idx = key_idx;
2602 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2603 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2604 goto nla_put_failure;
2605 if (mac_addr &&
2606 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2607 goto nla_put_failure;
2609 if (pairwise && mac_addr &&
2610 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2611 return -ENOENT;
2613 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2614 get_key_callback);
2616 if (err)
2617 goto free_msg;
2619 if (cookie.error)
2620 goto nla_put_failure;
2622 genlmsg_end(msg, hdr);
2623 return genlmsg_reply(msg, info);
2625 nla_put_failure:
2626 err = -ENOBUFS;
2627 free_msg:
2628 nlmsg_free(msg);
2629 return err;
2632 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2635 struct key_parse key;
2636 int err;
2637 struct net_device *dev = info->user_ptr[1];
2639 err = nl80211_parse_key(info, &key);
2640 if (err)
2641 return err;
2643 if (key.idx < 0)
2644 return -EINVAL;
2646 /* only support setting default key */
2647 if (!key.def && !key.defmgmt)
2648 return -EINVAL;
2650 wdev_lock(dev->ieee80211_ptr);
2652 if (key.def) {
2653 if (!rdev->ops->set_default_key) {
2654 err = -EOPNOTSUPP;
2655 goto out;
2658 err = nl80211_key_allowed(dev->ieee80211_ptr);
2659 if (err)
2660 goto out;
2662 err = rdev_set_default_key(rdev, dev, key.idx,
2663 key.def_uni, key.def_multi);
2665 if (err)
2666 goto out;
2668 #ifdef CONFIG_CFG80211_WEXT
2669 dev->ieee80211_ptr->wext.default_key = key.idx;
2670 #endif
2671 } else {
2672 if (key.def_uni || !key.def_multi) {
2673 err = -EINVAL;
2674 goto out;
2677 if (!rdev->ops->set_default_mgmt_key) {
2678 err = -EOPNOTSUPP;
2679 goto out;
2682 err = nl80211_key_allowed(dev->ieee80211_ptr);
2683 if (err)
2684 goto out;
2686 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
2687 if (err)
2688 goto out;
2690 #ifdef CONFIG_CFG80211_WEXT
2691 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2692 #endif
2695 out:
2696 wdev_unlock(dev->ieee80211_ptr);
2698 return err;
2701 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2703 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2704 int err;
2705 struct net_device *dev = info->user_ptr[1];
2706 struct key_parse key;
2707 const u8 *mac_addr = NULL;
2709 err = nl80211_parse_key(info, &key);
2710 if (err)
2711 return err;
2713 if (!key.p.key)
2714 return -EINVAL;
2716 if (info->attrs[NL80211_ATTR_MAC])
2717 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2719 if (key.type == -1) {
2720 if (mac_addr)
2721 key.type = NL80211_KEYTYPE_PAIRWISE;
2722 else
2723 key.type = NL80211_KEYTYPE_GROUP;
2726 /* for now */
2727 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2728 key.type != NL80211_KEYTYPE_GROUP)
2729 return -EINVAL;
2731 if (!rdev->ops->add_key)
2732 return -EOPNOTSUPP;
2734 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2735 key.type == NL80211_KEYTYPE_PAIRWISE,
2736 mac_addr))
2737 return -EINVAL;
2739 wdev_lock(dev->ieee80211_ptr);
2740 err = nl80211_key_allowed(dev->ieee80211_ptr);
2741 if (!err)
2742 err = rdev_add_key(rdev, dev, key.idx,
2743 key.type == NL80211_KEYTYPE_PAIRWISE,
2744 mac_addr, &key.p);
2745 wdev_unlock(dev->ieee80211_ptr);
2747 return err;
2750 static int nl80211_del_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 *mac_addr = NULL;
2756 struct key_parse key;
2758 err = nl80211_parse_key(info, &key);
2759 if (err)
2760 return err;
2762 if (info->attrs[NL80211_ATTR_MAC])
2763 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2765 if (key.type == -1) {
2766 if (mac_addr)
2767 key.type = NL80211_KEYTYPE_PAIRWISE;
2768 else
2769 key.type = NL80211_KEYTYPE_GROUP;
2772 /* for now */
2773 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2774 key.type != NL80211_KEYTYPE_GROUP)
2775 return -EINVAL;
2777 if (!rdev->ops->del_key)
2778 return -EOPNOTSUPP;
2780 wdev_lock(dev->ieee80211_ptr);
2781 err = nl80211_key_allowed(dev->ieee80211_ptr);
2783 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2784 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2785 err = -ENOENT;
2787 if (!err)
2788 err = rdev_del_key(rdev, dev, key.idx,
2789 key.type == NL80211_KEYTYPE_PAIRWISE,
2790 mac_addr);
2792 #ifdef CONFIG_CFG80211_WEXT
2793 if (!err) {
2794 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2795 dev->ieee80211_ptr->wext.default_key = -1;
2796 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2797 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2799 #endif
2800 wdev_unlock(dev->ieee80211_ptr);
2802 return err;
2805 /* This function returns an error or the number of nested attributes */
2806 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2808 struct nlattr *attr;
2809 int n_entries = 0, tmp;
2811 nla_for_each_nested(attr, nl_attr, tmp) {
2812 if (nla_len(attr) != ETH_ALEN)
2813 return -EINVAL;
2815 n_entries++;
2818 return n_entries;
2822 * This function parses ACL information and allocates memory for ACL data.
2823 * On successful return, the calling function is responsible to free the
2824 * ACL buffer returned by this function.
2826 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2827 struct genl_info *info)
2829 enum nl80211_acl_policy acl_policy;
2830 struct nlattr *attr;
2831 struct cfg80211_acl_data *acl;
2832 int i = 0, n_entries, tmp;
2834 if (!wiphy->max_acl_mac_addrs)
2835 return ERR_PTR(-EOPNOTSUPP);
2837 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2838 return ERR_PTR(-EINVAL);
2840 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2841 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2842 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2843 return ERR_PTR(-EINVAL);
2845 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2846 return ERR_PTR(-EINVAL);
2848 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2849 if (n_entries < 0)
2850 return ERR_PTR(n_entries);
2852 if (n_entries > wiphy->max_acl_mac_addrs)
2853 return ERR_PTR(-ENOTSUPP);
2855 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2856 GFP_KERNEL);
2857 if (!acl)
2858 return ERR_PTR(-ENOMEM);
2860 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2861 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2862 i++;
2865 acl->n_acl_entries = n_entries;
2866 acl->acl_policy = acl_policy;
2868 return acl;
2871 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2873 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2874 struct net_device *dev = info->user_ptr[1];
2875 struct cfg80211_acl_data *acl;
2876 int err;
2878 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2879 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2880 return -EOPNOTSUPP;
2882 if (!dev->ieee80211_ptr->beacon_interval)
2883 return -EINVAL;
2885 acl = parse_acl_data(&rdev->wiphy, info);
2886 if (IS_ERR(acl))
2887 return PTR_ERR(acl);
2889 err = rdev_set_mac_acl(rdev, dev, acl);
2891 kfree(acl);
2893 return err;
2896 static int nl80211_parse_beacon(struct genl_info *info,
2897 struct cfg80211_beacon_data *bcn)
2899 bool haveinfo = false;
2901 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2902 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2903 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2904 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
2905 return -EINVAL;
2907 memset(bcn, 0, sizeof(*bcn));
2909 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2910 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2911 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2912 if (!bcn->head_len)
2913 return -EINVAL;
2914 haveinfo = true;
2917 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2918 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2919 bcn->tail_len =
2920 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2921 haveinfo = true;
2924 if (!haveinfo)
2925 return -EINVAL;
2927 if (info->attrs[NL80211_ATTR_IE]) {
2928 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2929 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2932 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2933 bcn->proberesp_ies =
2934 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2935 bcn->proberesp_ies_len =
2936 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2939 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2940 bcn->assocresp_ies =
2941 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2942 bcn->assocresp_ies_len =
2943 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2946 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2947 bcn->probe_resp =
2948 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2949 bcn->probe_resp_len =
2950 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2953 return 0;
2956 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2957 struct cfg80211_ap_settings *params)
2959 struct wireless_dev *wdev;
2960 bool ret = false;
2962 list_for_each_entry(wdev, &rdev->wdev_list, list) {
2963 if (wdev->iftype != NL80211_IFTYPE_AP &&
2964 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2965 continue;
2967 if (!wdev->preset_chandef.chan)
2968 continue;
2970 params->chandef = wdev->preset_chandef;
2971 ret = true;
2972 break;
2975 return ret;
2978 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2979 enum nl80211_auth_type auth_type,
2980 enum nl80211_commands cmd)
2982 if (auth_type > NL80211_AUTHTYPE_MAX)
2983 return false;
2985 switch (cmd) {
2986 case NL80211_CMD_AUTHENTICATE:
2987 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2988 auth_type == NL80211_AUTHTYPE_SAE)
2989 return false;
2990 return true;
2991 case NL80211_CMD_CONNECT:
2992 case NL80211_CMD_START_AP:
2993 /* SAE not supported yet */
2994 if (auth_type == NL80211_AUTHTYPE_SAE)
2995 return false;
2996 return true;
2997 default:
2998 return false;
3002 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3005 struct net_device *dev = info->user_ptr[1];
3006 struct wireless_dev *wdev = dev->ieee80211_ptr;
3007 struct cfg80211_ap_settings params;
3008 int err;
3009 u8 radar_detect_width = 0;
3011 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3012 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3013 return -EOPNOTSUPP;
3015 if (!rdev->ops->start_ap)
3016 return -EOPNOTSUPP;
3018 if (wdev->beacon_interval)
3019 return -EALREADY;
3021 memset(&params, 0, sizeof(params));
3023 /* these are required for START_AP */
3024 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3025 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3026 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3027 return -EINVAL;
3029 err = nl80211_parse_beacon(info, &params.beacon);
3030 if (err)
3031 return err;
3033 params.beacon_interval =
3034 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3035 params.dtim_period =
3036 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3038 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3039 if (err)
3040 return err;
3043 * In theory, some of these attributes should be required here
3044 * but since they were not used when the command was originally
3045 * added, keep them optional for old user space programs to let
3046 * them continue to work with drivers that do not need the
3047 * additional information -- drivers must check!
3049 if (info->attrs[NL80211_ATTR_SSID]) {
3050 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3051 params.ssid_len =
3052 nla_len(info->attrs[NL80211_ATTR_SSID]);
3053 if (params.ssid_len == 0 ||
3054 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3055 return -EINVAL;
3058 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3059 params.hidden_ssid = nla_get_u32(
3060 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3061 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3062 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3063 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3064 return -EINVAL;
3067 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3069 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3070 params.auth_type = nla_get_u32(
3071 info->attrs[NL80211_ATTR_AUTH_TYPE]);
3072 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3073 NL80211_CMD_START_AP))
3074 return -EINVAL;
3075 } else
3076 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3078 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3079 NL80211_MAX_NR_CIPHER_SUITES);
3080 if (err)
3081 return err;
3083 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3084 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3085 return -EOPNOTSUPP;
3086 params.inactivity_timeout = nla_get_u16(
3087 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3090 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3091 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3092 return -EINVAL;
3093 params.p2p_ctwindow =
3094 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3095 if (params.p2p_ctwindow > 127)
3096 return -EINVAL;
3097 if (params.p2p_ctwindow != 0 &&
3098 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3099 return -EINVAL;
3102 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3103 u8 tmp;
3105 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3106 return -EINVAL;
3107 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3108 if (tmp > 1)
3109 return -EINVAL;
3110 params.p2p_opp_ps = tmp;
3111 if (params.p2p_opp_ps != 0 &&
3112 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3113 return -EINVAL;
3116 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3117 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3118 if (err)
3119 return err;
3120 } else if (wdev->preset_chandef.chan) {
3121 params.chandef = wdev->preset_chandef;
3122 } else if (!nl80211_get_ap_channel(rdev, &params))
3123 return -EINVAL;
3125 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
3126 return -EINVAL;
3128 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3129 if (err < 0)
3130 return err;
3131 if (err) {
3132 radar_detect_width = BIT(params.chandef.width);
3133 params.radar_required = true;
3136 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3137 params.chandef.chan,
3138 CHAN_MODE_SHARED,
3139 radar_detect_width);
3140 if (err)
3141 return err;
3143 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3144 params.acl = parse_acl_data(&rdev->wiphy, info);
3145 if (IS_ERR(params.acl))
3146 return PTR_ERR(params.acl);
3149 err = rdev_start_ap(rdev, dev, &params);
3150 if (!err) {
3151 wdev->preset_chandef = params.chandef;
3152 wdev->beacon_interval = params.beacon_interval;
3153 wdev->channel = params.chandef.chan;
3154 wdev->ssid_len = params.ssid_len;
3155 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
3158 kfree(params.acl);
3160 return err;
3163 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3166 struct net_device *dev = info->user_ptr[1];
3167 struct wireless_dev *wdev = dev->ieee80211_ptr;
3168 struct cfg80211_beacon_data params;
3169 int err;
3171 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3172 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3173 return -EOPNOTSUPP;
3175 if (!rdev->ops->change_beacon)
3176 return -EOPNOTSUPP;
3178 if (!wdev->beacon_interval)
3179 return -EINVAL;
3181 err = nl80211_parse_beacon(info, &params);
3182 if (err)
3183 return err;
3185 return rdev_change_beacon(rdev, dev, &params);
3188 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
3190 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3191 struct net_device *dev = info->user_ptr[1];
3193 return cfg80211_stop_ap(rdev, dev);
3196 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3197 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3198 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3199 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
3200 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
3201 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
3202 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
3205 static int parse_station_flags(struct genl_info *info,
3206 enum nl80211_iftype iftype,
3207 struct station_parameters *params)
3209 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
3210 struct nlattr *nla;
3211 int flag;
3214 * Try parsing the new attribute first so userspace
3215 * can specify both for older kernels.
3217 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3218 if (nla) {
3219 struct nl80211_sta_flag_update *sta_flags;
3221 sta_flags = nla_data(nla);
3222 params->sta_flags_mask = sta_flags->mask;
3223 params->sta_flags_set = sta_flags->set;
3224 params->sta_flags_set &= params->sta_flags_mask;
3225 if ((params->sta_flags_mask |
3226 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3227 return -EINVAL;
3228 return 0;
3231 /* if present, parse the old attribute */
3233 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
3234 if (!nla)
3235 return 0;
3237 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3238 nla, sta_flags_policy))
3239 return -EINVAL;
3242 * Only allow certain flags for interface types so that
3243 * other attributes are silently ignored. Remember that
3244 * this is backward compatibility code with old userspace
3245 * and shouldn't be hit in other cases anyway.
3247 switch (iftype) {
3248 case NL80211_IFTYPE_AP:
3249 case NL80211_IFTYPE_AP_VLAN:
3250 case NL80211_IFTYPE_P2P_GO:
3251 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3252 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3253 BIT(NL80211_STA_FLAG_WME) |
3254 BIT(NL80211_STA_FLAG_MFP);
3255 break;
3256 case NL80211_IFTYPE_P2P_CLIENT:
3257 case NL80211_IFTYPE_STATION:
3258 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3259 BIT(NL80211_STA_FLAG_TDLS_PEER);
3260 break;
3261 case NL80211_IFTYPE_MESH_POINT:
3262 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3263 BIT(NL80211_STA_FLAG_MFP) |
3264 BIT(NL80211_STA_FLAG_AUTHORIZED);
3265 default:
3266 return -EINVAL;
3269 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3270 if (flags[flag]) {
3271 params->sta_flags_set |= (1<<flag);
3273 /* no longer support new API additions in old API */
3274 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3275 return -EINVAL;
3279 return 0;
3282 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3283 int attr)
3285 struct nlattr *rate;
3286 u32 bitrate;
3287 u16 bitrate_compat;
3289 rate = nla_nest_start(msg, attr);
3290 if (!rate)
3291 return false;
3293 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3294 bitrate = cfg80211_calculate_bitrate(info);
3295 /* report 16-bit bitrate only if we can */
3296 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
3297 if (bitrate > 0 &&
3298 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3299 return false;
3300 if (bitrate_compat > 0 &&
3301 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3302 return false;
3304 if (info->flags & RATE_INFO_FLAGS_MCS) {
3305 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3306 return false;
3307 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3308 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3309 return false;
3310 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3311 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3312 return false;
3313 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3314 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3315 return false;
3316 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3317 return false;
3318 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3319 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3320 return false;
3321 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3322 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3323 return false;
3324 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3325 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3326 return false;
3327 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3328 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3329 return false;
3330 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3331 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3332 return false;
3335 nla_nest_end(msg, rate);
3336 return true;
3339 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3340 int id)
3342 void *attr;
3343 int i = 0;
3345 if (!mask)
3346 return true;
3348 attr = nla_nest_start(msg, id);
3349 if (!attr)
3350 return false;
3352 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3353 if (!(mask & BIT(i)))
3354 continue;
3356 if (nla_put_u8(msg, i, signal[i]))
3357 return false;
3360 nla_nest_end(msg, attr);
3362 return true;
3365 static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
3366 int flags,
3367 struct cfg80211_registered_device *rdev,
3368 struct net_device *dev,
3369 const u8 *mac_addr, struct station_info *sinfo)
3371 void *hdr;
3372 struct nlattr *sinfoattr, *bss_param;
3374 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
3375 if (!hdr)
3376 return -1;
3378 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3379 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3380 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3381 goto nla_put_failure;
3383 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3384 if (!sinfoattr)
3385 goto nla_put_failure;
3386 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3387 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3388 sinfo->connected_time))
3389 goto nla_put_failure;
3390 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3391 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3392 sinfo->inactive_time))
3393 goto nla_put_failure;
3394 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3395 STATION_INFO_RX_BYTES64)) &&
3396 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
3397 (u32)sinfo->rx_bytes))
3398 goto nla_put_failure;
3399 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3400 STATION_INFO_TX_BYTES64)) &&
3401 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3402 (u32)sinfo->tx_bytes))
3403 goto nla_put_failure;
3404 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3405 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
3406 sinfo->rx_bytes))
3407 goto nla_put_failure;
3408 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3409 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
3410 sinfo->tx_bytes))
3411 goto nla_put_failure;
3412 if ((sinfo->filled & STATION_INFO_LLID) &&
3413 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3414 goto nla_put_failure;
3415 if ((sinfo->filled & STATION_INFO_PLID) &&
3416 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3417 goto nla_put_failure;
3418 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3419 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3420 sinfo->plink_state))
3421 goto nla_put_failure;
3422 switch (rdev->wiphy.signal_type) {
3423 case CFG80211_SIGNAL_TYPE_MBM:
3424 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3425 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3426 sinfo->signal))
3427 goto nla_put_failure;
3428 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3429 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3430 sinfo->signal_avg))
3431 goto nla_put_failure;
3432 break;
3433 default:
3434 break;
3436 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3437 if (!nl80211_put_signal(msg, sinfo->chains,
3438 sinfo->chain_signal,
3439 NL80211_STA_INFO_CHAIN_SIGNAL))
3440 goto nla_put_failure;
3442 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3443 if (!nl80211_put_signal(msg, sinfo->chains,
3444 sinfo->chain_signal_avg,
3445 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3446 goto nla_put_failure;
3448 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
3449 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3450 NL80211_STA_INFO_TX_BITRATE))
3451 goto nla_put_failure;
3453 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3454 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3455 NL80211_STA_INFO_RX_BITRATE))
3456 goto nla_put_failure;
3458 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3459 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3460 sinfo->rx_packets))
3461 goto nla_put_failure;
3462 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3463 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3464 sinfo->tx_packets))
3465 goto nla_put_failure;
3466 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3467 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3468 sinfo->tx_retries))
3469 goto nla_put_failure;
3470 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3471 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3472 sinfo->tx_failed))
3473 goto nla_put_failure;
3474 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3475 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3476 sinfo->beacon_loss_count))
3477 goto nla_put_failure;
3478 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3479 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3480 sinfo->local_pm))
3481 goto nla_put_failure;
3482 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3483 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3484 sinfo->peer_pm))
3485 goto nla_put_failure;
3486 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3487 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3488 sinfo->nonpeer_pm))
3489 goto nla_put_failure;
3490 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3491 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3492 if (!bss_param)
3493 goto nla_put_failure;
3495 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3496 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3497 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3498 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3499 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3500 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3501 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3502 sinfo->bss_param.dtim_period) ||
3503 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3504 sinfo->bss_param.beacon_interval))
3505 goto nla_put_failure;
3507 nla_nest_end(msg, bss_param);
3509 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3510 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3511 sizeof(struct nl80211_sta_flag_update),
3512 &sinfo->sta_flags))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3515 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3516 sinfo->t_offset))
3517 goto nla_put_failure;
3518 nla_nest_end(msg, sinfoattr);
3520 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3521 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3522 sinfo->assoc_req_ies))
3523 goto nla_put_failure;
3525 return genlmsg_end(msg, hdr);
3527 nla_put_failure:
3528 genlmsg_cancel(msg, hdr);
3529 return -EMSGSIZE;
3532 static int nl80211_dump_station(struct sk_buff *skb,
3533 struct netlink_callback *cb)
3535 struct station_info sinfo;
3536 struct cfg80211_registered_device *dev;
3537 struct wireless_dev *wdev;
3538 u8 mac_addr[ETH_ALEN];
3539 int sta_idx = cb->args[2];
3540 int err;
3542 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
3543 if (err)
3544 return err;
3546 if (!wdev->netdev) {
3547 err = -EINVAL;
3548 goto out_err;
3551 if (!dev->ops->dump_station) {
3552 err = -EOPNOTSUPP;
3553 goto out_err;
3556 while (1) {
3557 memset(&sinfo, 0, sizeof(sinfo));
3558 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
3559 mac_addr, &sinfo);
3560 if (err == -ENOENT)
3561 break;
3562 if (err)
3563 goto out_err;
3565 if (nl80211_send_station(skb,
3566 NETLINK_CB(cb->skb).portid,
3567 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3568 dev, wdev->netdev, mac_addr,
3569 &sinfo) < 0)
3570 goto out;
3572 sta_idx++;
3576 out:
3577 cb->args[2] = sta_idx;
3578 err = skb->len;
3579 out_err:
3580 nl80211_finish_wdev_dump(dev);
3582 return err;
3585 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3587 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3588 struct net_device *dev = info->user_ptr[1];
3589 struct station_info sinfo;
3590 struct sk_buff *msg;
3591 u8 *mac_addr = NULL;
3592 int err;
3594 memset(&sinfo, 0, sizeof(sinfo));
3596 if (!info->attrs[NL80211_ATTR_MAC])
3597 return -EINVAL;
3599 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3601 if (!rdev->ops->get_station)
3602 return -EOPNOTSUPP;
3604 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
3605 if (err)
3606 return err;
3608 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3609 if (!msg)
3610 return -ENOMEM;
3612 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
3613 rdev, dev, mac_addr, &sinfo) < 0) {
3614 nlmsg_free(msg);
3615 return -ENOBUFS;
3618 return genlmsg_reply(msg, info);
3621 int cfg80211_check_station_change(struct wiphy *wiphy,
3622 struct station_parameters *params,
3623 enum cfg80211_station_type statype)
3625 if (params->listen_interval != -1)
3626 return -EINVAL;
3627 if (params->aid)
3628 return -EINVAL;
3630 /* When you run into this, adjust the code below for the new flag */
3631 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3633 switch (statype) {
3634 case CFG80211_STA_MESH_PEER_KERNEL:
3635 case CFG80211_STA_MESH_PEER_USER:
3637 * No ignoring the TDLS flag here -- the userspace mesh
3638 * code doesn't have the bug of including TDLS in the
3639 * mask everywhere.
3641 if (params->sta_flags_mask &
3642 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3643 BIT(NL80211_STA_FLAG_MFP) |
3644 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3645 return -EINVAL;
3646 break;
3647 case CFG80211_STA_TDLS_PEER_SETUP:
3648 case CFG80211_STA_TDLS_PEER_ACTIVE:
3649 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3650 return -EINVAL;
3651 /* ignore since it can't change */
3652 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3653 break;
3654 default:
3655 /* disallow mesh-specific things */
3656 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3657 return -EINVAL;
3658 if (params->local_pm)
3659 return -EINVAL;
3660 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3661 return -EINVAL;
3664 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3665 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3666 /* TDLS can't be set, ... */
3667 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3668 return -EINVAL;
3670 * ... but don't bother the driver with it. This works around
3671 * a hostapd/wpa_supplicant issue -- it always includes the
3672 * TLDS_PEER flag in the mask even for AP mode.
3674 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3677 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3678 /* reject other things that can't change */
3679 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3680 return -EINVAL;
3681 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3682 return -EINVAL;
3683 if (params->supported_rates)
3684 return -EINVAL;
3685 if (params->ext_capab || params->ht_capa || params->vht_capa)
3686 return -EINVAL;
3689 if (statype != CFG80211_STA_AP_CLIENT) {
3690 if (params->vlan)
3691 return -EINVAL;
3694 switch (statype) {
3695 case CFG80211_STA_AP_MLME_CLIENT:
3696 /* Use this only for authorizing/unauthorizing a station */
3697 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3698 return -EOPNOTSUPP;
3699 break;
3700 case CFG80211_STA_AP_CLIENT:
3701 /* accept only the listed bits */
3702 if (params->sta_flags_mask &
3703 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3704 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3705 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3706 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3707 BIT(NL80211_STA_FLAG_WME) |
3708 BIT(NL80211_STA_FLAG_MFP)))
3709 return -EINVAL;
3711 /* but authenticated/associated only if driver handles it */
3712 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3713 params->sta_flags_mask &
3714 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3715 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3716 return -EINVAL;
3717 break;
3718 case CFG80211_STA_IBSS:
3719 case CFG80211_STA_AP_STA:
3720 /* reject any changes other than AUTHORIZED */
3721 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3722 return -EINVAL;
3723 break;
3724 case CFG80211_STA_TDLS_PEER_SETUP:
3725 /* reject any changes other than AUTHORIZED or WME */
3726 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3727 BIT(NL80211_STA_FLAG_WME)))
3728 return -EINVAL;
3729 /* force (at least) rates when authorizing */
3730 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3731 !params->supported_rates)
3732 return -EINVAL;
3733 break;
3734 case CFG80211_STA_TDLS_PEER_ACTIVE:
3735 /* reject any changes */
3736 return -EINVAL;
3737 case CFG80211_STA_MESH_PEER_KERNEL:
3738 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3739 return -EINVAL;
3740 break;
3741 case CFG80211_STA_MESH_PEER_USER:
3742 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3743 return -EINVAL;
3744 break;
3747 return 0;
3749 EXPORT_SYMBOL(cfg80211_check_station_change);
3752 * Get vlan interface making sure it is running and on the right wiphy.
3754 static struct net_device *get_vlan(struct genl_info *info,
3755 struct cfg80211_registered_device *rdev)
3757 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
3758 struct net_device *v;
3759 int ret;
3761 if (!vlanattr)
3762 return NULL;
3764 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3765 if (!v)
3766 return ERR_PTR(-ENODEV);
3768 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3769 ret = -EINVAL;
3770 goto error;
3773 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3774 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3775 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3776 ret = -EINVAL;
3777 goto error;
3780 if (!netif_running(v)) {
3781 ret = -ENETDOWN;
3782 goto error;
3785 return v;
3786 error:
3787 dev_put(v);
3788 return ERR_PTR(ret);
3791 static struct nla_policy
3792 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3793 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3794 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3797 static int nl80211_parse_sta_wme(struct genl_info *info,
3798 struct station_parameters *params)
3800 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3801 struct nlattr *nla;
3802 int err;
3804 /* parse WME attributes if present */
3805 if (!info->attrs[NL80211_ATTR_STA_WME])
3806 return 0;
3808 nla = info->attrs[NL80211_ATTR_STA_WME];
3809 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3810 nl80211_sta_wme_policy);
3811 if (err)
3812 return err;
3814 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3815 params->uapsd_queues = nla_get_u8(
3816 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3817 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3818 return -EINVAL;
3820 if (tb[NL80211_STA_WME_MAX_SP])
3821 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3823 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3824 return -EINVAL;
3826 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3828 return 0;
3831 static int nl80211_set_station_tdls(struct genl_info *info,
3832 struct station_parameters *params)
3834 /* Dummy STA entry gets updated once the peer capabilities are known */
3835 if (info->attrs[NL80211_ATTR_PEER_AID])
3836 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
3837 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3838 params->ht_capa =
3839 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3840 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3841 params->vht_capa =
3842 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3844 return nl80211_parse_sta_wme(info, params);
3847 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3849 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3850 struct net_device *dev = info->user_ptr[1];
3851 struct station_parameters params;
3852 u8 *mac_addr;
3853 int err;
3855 memset(&params, 0, sizeof(params));
3857 params.listen_interval = -1;
3859 if (!rdev->ops->change_station)
3860 return -EOPNOTSUPP;
3862 if (info->attrs[NL80211_ATTR_STA_AID])
3863 return -EINVAL;
3865 if (!info->attrs[NL80211_ATTR_MAC])
3866 return -EINVAL;
3868 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3870 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3871 params.supported_rates =
3872 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3873 params.supported_rates_len =
3874 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3877 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3878 params.capability =
3879 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3880 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3883 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3884 params.ext_capab =
3885 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3886 params.ext_capab_len =
3887 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3890 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3891 return -EINVAL;
3893 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
3894 return -EINVAL;
3896 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
3897 params.plink_action =
3898 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3899 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3900 return -EINVAL;
3903 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
3904 params.plink_state =
3905 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3906 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3907 return -EINVAL;
3908 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3911 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3912 enum nl80211_mesh_power_mode pm = nla_get_u32(
3913 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3915 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3916 pm > NL80211_MESH_POWER_MAX)
3917 return -EINVAL;
3919 params.local_pm = pm;
3922 /* Include parameters for TDLS peer (will check later) */
3923 err = nl80211_set_station_tdls(info, &params);
3924 if (err)
3925 return err;
3927 params.vlan = get_vlan(info, rdev);
3928 if (IS_ERR(params.vlan))
3929 return PTR_ERR(params.vlan);
3931 switch (dev->ieee80211_ptr->iftype) {
3932 case NL80211_IFTYPE_AP:
3933 case NL80211_IFTYPE_AP_VLAN:
3934 case NL80211_IFTYPE_P2P_GO:
3935 case NL80211_IFTYPE_P2P_CLIENT:
3936 case NL80211_IFTYPE_STATION:
3937 case NL80211_IFTYPE_ADHOC:
3938 case NL80211_IFTYPE_MESH_POINT:
3939 break;
3940 default:
3941 err = -EOPNOTSUPP;
3942 goto out_put_vlan;
3945 /* driver will call cfg80211_check_station_change() */
3946 err = rdev_change_station(rdev, dev, mac_addr, &params);
3948 out_put_vlan:
3949 if (params.vlan)
3950 dev_put(params.vlan);
3952 return err;
3955 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3958 int err;
3959 struct net_device *dev = info->user_ptr[1];
3960 struct station_parameters params;
3961 u8 *mac_addr = NULL;
3963 memset(&params, 0, sizeof(params));
3965 if (!rdev->ops->add_station)
3966 return -EOPNOTSUPP;
3968 if (!info->attrs[NL80211_ATTR_MAC])
3969 return -EINVAL;
3971 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3972 return -EINVAL;
3974 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3975 return -EINVAL;
3977 if (!info->attrs[NL80211_ATTR_STA_AID] &&
3978 !info->attrs[NL80211_ATTR_PEER_AID])
3979 return -EINVAL;
3981 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3982 params.supported_rates =
3983 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3984 params.supported_rates_len =
3985 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3986 params.listen_interval =
3987 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3989 if (info->attrs[NL80211_ATTR_STA_AID])
3990 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3991 else
3992 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
3993 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3994 return -EINVAL;
3996 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3997 params.capability =
3998 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3999 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4002 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4003 params.ext_capab =
4004 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4005 params.ext_capab_len =
4006 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4009 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4010 params.ht_capa =
4011 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4013 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4014 params.vht_capa =
4015 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4017 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4018 params.plink_action =
4019 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4020 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4021 return -EINVAL;
4024 err = nl80211_parse_sta_wme(info, &params);
4025 if (err)
4026 return err;
4028 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4029 return -EINVAL;
4031 /* When you run into this, adjust the code below for the new flag */
4032 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4034 switch (dev->ieee80211_ptr->iftype) {
4035 case NL80211_IFTYPE_AP:
4036 case NL80211_IFTYPE_AP_VLAN:
4037 case NL80211_IFTYPE_P2P_GO:
4038 /* ignore WME attributes if iface/sta is not capable */
4039 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4040 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4041 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4043 /* TDLS peers cannot be added */
4044 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4045 return -EINVAL;
4046 /* but don't bother the driver with it */
4047 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4049 /* allow authenticated/associated only if driver handles it */
4050 if (!(rdev->wiphy.features &
4051 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4052 params.sta_flags_mask &
4053 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4054 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4055 return -EINVAL;
4057 /* must be last in here for error handling */
4058 params.vlan = get_vlan(info, rdev);
4059 if (IS_ERR(params.vlan))
4060 return PTR_ERR(params.vlan);
4061 break;
4062 case NL80211_IFTYPE_MESH_POINT:
4063 /* ignore uAPSD data */
4064 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4066 /* associated is disallowed */
4067 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4068 return -EINVAL;
4069 /* TDLS peers cannot be added */
4070 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4071 return -EINVAL;
4072 break;
4073 case NL80211_IFTYPE_STATION:
4074 case NL80211_IFTYPE_P2P_CLIENT:
4075 /* ignore uAPSD data */
4076 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4078 /* these are disallowed */
4079 if (params.sta_flags_mask &
4080 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4081 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
4082 return -EINVAL;
4083 /* Only TDLS peers can be added */
4084 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4085 return -EINVAL;
4086 /* Can only add if TDLS ... */
4087 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4088 return -EOPNOTSUPP;
4089 /* ... with external setup is supported */
4090 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4091 return -EOPNOTSUPP;
4093 * Older wpa_supplicant versions always mark the TDLS peer
4094 * as authorized, but it shouldn't yet be.
4096 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
4097 break;
4098 default:
4099 return -EOPNOTSUPP;
4102 /* be aware of params.vlan when changing code here */
4104 err = rdev_add_station(rdev, dev, mac_addr, &params);
4106 if (params.vlan)
4107 dev_put(params.vlan);
4108 return err;
4111 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4114 struct net_device *dev = info->user_ptr[1];
4115 u8 *mac_addr = NULL;
4117 if (info->attrs[NL80211_ATTR_MAC])
4118 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4120 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4121 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4122 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4123 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4124 return -EINVAL;
4126 if (!rdev->ops->del_station)
4127 return -EOPNOTSUPP;
4129 return rdev_del_station(rdev, dev, mac_addr);
4132 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
4133 int flags, struct net_device *dev,
4134 u8 *dst, u8 *next_hop,
4135 struct mpath_info *pinfo)
4137 void *hdr;
4138 struct nlattr *pinfoattr;
4140 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
4141 if (!hdr)
4142 return -1;
4144 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4145 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4146 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4147 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4148 goto nla_put_failure;
4150 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4151 if (!pinfoattr)
4152 goto nla_put_failure;
4153 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4154 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4155 pinfo->frame_qlen))
4156 goto nla_put_failure;
4157 if (((pinfo->filled & MPATH_INFO_SN) &&
4158 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4159 ((pinfo->filled & MPATH_INFO_METRIC) &&
4160 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4161 pinfo->metric)) ||
4162 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4163 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4164 pinfo->exptime)) ||
4165 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4166 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4167 pinfo->flags)) ||
4168 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4169 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4170 pinfo->discovery_timeout)) ||
4171 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4172 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4173 pinfo->discovery_retries)))
4174 goto nla_put_failure;
4176 nla_nest_end(msg, pinfoattr);
4178 return genlmsg_end(msg, hdr);
4180 nla_put_failure:
4181 genlmsg_cancel(msg, hdr);
4182 return -EMSGSIZE;
4185 static int nl80211_dump_mpath(struct sk_buff *skb,
4186 struct netlink_callback *cb)
4188 struct mpath_info pinfo;
4189 struct cfg80211_registered_device *dev;
4190 struct wireless_dev *wdev;
4191 u8 dst[ETH_ALEN];
4192 u8 next_hop[ETH_ALEN];
4193 int path_idx = cb->args[2];
4194 int err;
4196 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
4197 if (err)
4198 return err;
4200 if (!dev->ops->dump_mpath) {
4201 err = -EOPNOTSUPP;
4202 goto out_err;
4205 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4206 err = -EOPNOTSUPP;
4207 goto out_err;
4210 while (1) {
4211 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4212 next_hop, &pinfo);
4213 if (err == -ENOENT)
4214 break;
4215 if (err)
4216 goto out_err;
4218 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4219 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4220 wdev->netdev, dst, next_hop,
4221 &pinfo) < 0)
4222 goto out;
4224 path_idx++;
4228 out:
4229 cb->args[2] = path_idx;
4230 err = skb->len;
4231 out_err:
4232 nl80211_finish_wdev_dump(dev);
4233 return err;
4236 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4239 int err;
4240 struct net_device *dev = info->user_ptr[1];
4241 struct mpath_info pinfo;
4242 struct sk_buff *msg;
4243 u8 *dst = NULL;
4244 u8 next_hop[ETH_ALEN];
4246 memset(&pinfo, 0, sizeof(pinfo));
4248 if (!info->attrs[NL80211_ATTR_MAC])
4249 return -EINVAL;
4251 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4253 if (!rdev->ops->get_mpath)
4254 return -EOPNOTSUPP;
4256 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4257 return -EOPNOTSUPP;
4259 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
4260 if (err)
4261 return err;
4263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4264 if (!msg)
4265 return -ENOMEM;
4267 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4268 dev, dst, next_hop, &pinfo) < 0) {
4269 nlmsg_free(msg);
4270 return -ENOBUFS;
4273 return genlmsg_reply(msg, info);
4276 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4278 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4279 struct net_device *dev = info->user_ptr[1];
4280 u8 *dst = NULL;
4281 u8 *next_hop = NULL;
4283 if (!info->attrs[NL80211_ATTR_MAC])
4284 return -EINVAL;
4286 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4287 return -EINVAL;
4289 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4290 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4292 if (!rdev->ops->change_mpath)
4293 return -EOPNOTSUPP;
4295 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4296 return -EOPNOTSUPP;
4298 return rdev_change_mpath(rdev, dev, dst, next_hop);
4301 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4304 struct net_device *dev = info->user_ptr[1];
4305 u8 *dst = NULL;
4306 u8 *next_hop = NULL;
4308 if (!info->attrs[NL80211_ATTR_MAC])
4309 return -EINVAL;
4311 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4312 return -EINVAL;
4314 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4315 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4317 if (!rdev->ops->add_mpath)
4318 return -EOPNOTSUPP;
4320 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4321 return -EOPNOTSUPP;
4323 return rdev_add_mpath(rdev, dev, dst, next_hop);
4326 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4329 struct net_device *dev = info->user_ptr[1];
4330 u8 *dst = NULL;
4332 if (info->attrs[NL80211_ATTR_MAC])
4333 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4335 if (!rdev->ops->del_mpath)
4336 return -EOPNOTSUPP;
4338 return rdev_del_mpath(rdev, dev, dst);
4341 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4343 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4344 struct net_device *dev = info->user_ptr[1];
4345 struct bss_parameters params;
4347 memset(&params, 0, sizeof(params));
4348 /* default to not changing parameters */
4349 params.use_cts_prot = -1;
4350 params.use_short_preamble = -1;
4351 params.use_short_slot_time = -1;
4352 params.ap_isolate = -1;
4353 params.ht_opmode = -1;
4354 params.p2p_ctwindow = -1;
4355 params.p2p_opp_ps = -1;
4357 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4358 params.use_cts_prot =
4359 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4360 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4361 params.use_short_preamble =
4362 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4363 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4364 params.use_short_slot_time =
4365 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
4366 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4367 params.basic_rates =
4368 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4369 params.basic_rates_len =
4370 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4372 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4373 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
4374 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4375 params.ht_opmode =
4376 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
4378 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4379 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4380 return -EINVAL;
4381 params.p2p_ctwindow =
4382 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4383 if (params.p2p_ctwindow < 0)
4384 return -EINVAL;
4385 if (params.p2p_ctwindow != 0 &&
4386 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4387 return -EINVAL;
4390 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4391 u8 tmp;
4393 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4394 return -EINVAL;
4395 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4396 if (tmp > 1)
4397 return -EINVAL;
4398 params.p2p_opp_ps = tmp;
4399 if (params.p2p_opp_ps &&
4400 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4401 return -EINVAL;
4404 if (!rdev->ops->change_bss)
4405 return -EOPNOTSUPP;
4407 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4408 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4409 return -EOPNOTSUPP;
4411 return rdev_change_bss(rdev, dev, &params);
4414 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
4415 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4416 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4417 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4418 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4419 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4420 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4423 static int parse_reg_rule(struct nlattr *tb[],
4424 struct ieee80211_reg_rule *reg_rule)
4426 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4427 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4429 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4430 return -EINVAL;
4431 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4432 return -EINVAL;
4433 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4434 return -EINVAL;
4435 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4436 return -EINVAL;
4437 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4438 return -EINVAL;
4440 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4442 freq_range->start_freq_khz =
4443 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4444 freq_range->end_freq_khz =
4445 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4446 freq_range->max_bandwidth_khz =
4447 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4449 power_rule->max_eirp =
4450 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4452 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4453 power_rule->max_antenna_gain =
4454 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4456 return 0;
4459 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4461 int r;
4462 char *data = NULL;
4463 enum nl80211_user_reg_hint_type user_reg_hint_type;
4466 * You should only get this when cfg80211 hasn't yet initialized
4467 * completely when built-in to the kernel right between the time
4468 * window between nl80211_init() and regulatory_init(), if that is
4469 * even possible.
4471 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
4472 return -EINPROGRESS;
4474 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4475 return -EINVAL;
4477 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4479 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4480 user_reg_hint_type =
4481 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4482 else
4483 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4485 switch (user_reg_hint_type) {
4486 case NL80211_USER_REG_HINT_USER:
4487 case NL80211_USER_REG_HINT_CELL_BASE:
4488 break;
4489 default:
4490 return -EINVAL;
4493 r = regulatory_hint_user(data, user_reg_hint_type);
4495 return r;
4498 static int nl80211_get_mesh_config(struct sk_buff *skb,
4499 struct genl_info *info)
4501 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4502 struct net_device *dev = info->user_ptr[1];
4503 struct wireless_dev *wdev = dev->ieee80211_ptr;
4504 struct mesh_config cur_params;
4505 int err = 0;
4506 void *hdr;
4507 struct nlattr *pinfoattr;
4508 struct sk_buff *msg;
4510 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4511 return -EOPNOTSUPP;
4513 if (!rdev->ops->get_mesh_config)
4514 return -EOPNOTSUPP;
4516 wdev_lock(wdev);
4517 /* If not connected, get default parameters */
4518 if (!wdev->mesh_id_len)
4519 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4520 else
4521 err = rdev_get_mesh_config(rdev, dev, &cur_params);
4522 wdev_unlock(wdev);
4524 if (err)
4525 return err;
4527 /* Draw up a netlink message to send back */
4528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4529 if (!msg)
4530 return -ENOMEM;
4531 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4532 NL80211_CMD_GET_MESH_CONFIG);
4533 if (!hdr)
4534 goto out;
4535 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
4536 if (!pinfoattr)
4537 goto nla_put_failure;
4538 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4539 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4540 cur_params.dot11MeshRetryTimeout) ||
4541 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4542 cur_params.dot11MeshConfirmTimeout) ||
4543 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4544 cur_params.dot11MeshHoldingTimeout) ||
4545 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4546 cur_params.dot11MeshMaxPeerLinks) ||
4547 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4548 cur_params.dot11MeshMaxRetries) ||
4549 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4550 cur_params.dot11MeshTTL) ||
4551 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4552 cur_params.element_ttl) ||
4553 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4554 cur_params.auto_open_plinks) ||
4555 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4556 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
4557 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4558 cur_params.dot11MeshHWMPmaxPREQretries) ||
4559 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4560 cur_params.path_refresh_time) ||
4561 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4562 cur_params.min_discovery_timeout) ||
4563 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4564 cur_params.dot11MeshHWMPactivePathTimeout) ||
4565 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4566 cur_params.dot11MeshHWMPpreqMinInterval) ||
4567 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4568 cur_params.dot11MeshHWMPperrMinInterval) ||
4569 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4570 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4571 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4572 cur_params.dot11MeshHWMPRootMode) ||
4573 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4574 cur_params.dot11MeshHWMPRannInterval) ||
4575 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4576 cur_params.dot11MeshGateAnnouncementProtocol) ||
4577 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4578 cur_params.dot11MeshForwarding) ||
4579 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
4580 cur_params.rssi_threshold) ||
4581 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
4582 cur_params.ht_opmode) ||
4583 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4584 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4585 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4586 cur_params.dot11MeshHWMProotInterval) ||
4587 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4588 cur_params.dot11MeshHWMPconfirmationInterval) ||
4589 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4590 cur_params.power_mode) ||
4591 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4592 cur_params.dot11MeshAwakeWindowDuration))
4593 goto nla_put_failure;
4594 nla_nest_end(msg, pinfoattr);
4595 genlmsg_end(msg, hdr);
4596 return genlmsg_reply(msg, info);
4598 nla_put_failure:
4599 genlmsg_cancel(msg, hdr);
4600 out:
4601 nlmsg_free(msg);
4602 return -ENOBUFS;
4605 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
4606 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4607 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4608 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4609 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4610 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4611 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
4612 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
4613 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
4614 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
4615 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4616 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4617 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4618 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4619 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
4620 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
4621 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
4622 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
4623 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
4624 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
4625 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
4626 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4627 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
4628 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4629 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
4630 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
4631 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4632 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
4635 static const struct nla_policy
4636 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
4637 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
4638 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4639 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
4640 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
4641 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
4642 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
4643 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
4644 .len = IEEE80211_MAX_DATA_LEN },
4645 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
4648 static int nl80211_parse_mesh_config(struct genl_info *info,
4649 struct mesh_config *cfg,
4650 u32 *mask_out)
4652 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
4653 u32 mask = 0;
4655 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4656 do { \
4657 if (tb[attr]) { \
4658 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4659 return -EINVAL; \
4660 cfg->param = fn(tb[attr]); \
4661 mask |= (1 << (attr - 1)); \
4663 } while (0)
4666 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
4667 return -EINVAL;
4668 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
4669 info->attrs[NL80211_ATTR_MESH_CONFIG],
4670 nl80211_meshconf_params_policy))
4671 return -EINVAL;
4673 /* This makes sure that there aren't more than 32 mesh config
4674 * parameters (otherwise our bitfield scheme would not work.) */
4675 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4677 /* Fill in the params struct */
4678 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
4679 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4680 nla_get_u16);
4681 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
4682 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4683 nla_get_u16);
4684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
4685 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4686 nla_get_u16);
4687 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
4688 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4689 nla_get_u16);
4690 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
4691 mask, NL80211_MESHCONF_MAX_RETRIES,
4692 nla_get_u8);
4693 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
4694 mask, NL80211_MESHCONF_TTL, nla_get_u8);
4695 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
4696 mask, NL80211_MESHCONF_ELEMENT_TTL,
4697 nla_get_u8);
4698 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
4699 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4700 nla_get_u8);
4701 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4702 1, 255, mask,
4703 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4704 nla_get_u32);
4705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
4706 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4707 nla_get_u8);
4708 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
4709 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4710 nla_get_u32);
4711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
4712 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4713 nla_get_u16);
4714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4715 1, 65535, mask,
4716 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4717 nla_get_u32);
4718 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
4719 1, 65535, mask,
4720 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4721 nla_get_u16);
4722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
4723 1, 65535, mask,
4724 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4725 nla_get_u16);
4726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4727 dot11MeshHWMPnetDiameterTraversalTime,
4728 1, 65535, mask,
4729 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4730 nla_get_u16);
4731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4732 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4733 nla_get_u8);
4734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4735 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4736 nla_get_u16);
4737 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4738 dot11MeshGateAnnouncementProtocol, 0, 1,
4739 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4740 nla_get_u8);
4741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
4742 mask, NL80211_MESHCONF_FORWARDING,
4743 nla_get_u8);
4744 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
4745 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4746 nla_get_u32);
4747 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
4748 mask, NL80211_MESHCONF_HT_OPMODE,
4749 nla_get_u16);
4750 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
4751 1, 65535, mask,
4752 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4753 nla_get_u32);
4754 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
4755 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4756 nla_get_u16);
4757 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
4758 dot11MeshHWMPconfirmationInterval,
4759 1, 65535, mask,
4760 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4761 nla_get_u16);
4762 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4763 NL80211_MESH_POWER_ACTIVE,
4764 NL80211_MESH_POWER_MAX,
4765 mask, NL80211_MESHCONF_POWER_MODE,
4766 nla_get_u32);
4767 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4768 0, 65535, mask,
4769 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
4770 if (mask_out)
4771 *mask_out = mask;
4773 return 0;
4775 #undef FILL_IN_MESH_PARAM_IF_SET
4778 static int nl80211_parse_mesh_setup(struct genl_info *info,
4779 struct mesh_setup *setup)
4781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4782 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4784 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4785 return -EINVAL;
4786 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4787 info->attrs[NL80211_ATTR_MESH_SETUP],
4788 nl80211_mesh_setup_params_policy))
4789 return -EINVAL;
4791 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4792 setup->sync_method =
4793 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4794 IEEE80211_SYNC_METHOD_VENDOR :
4795 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4797 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4798 setup->path_sel_proto =
4799 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4800 IEEE80211_PATH_PROTOCOL_VENDOR :
4801 IEEE80211_PATH_PROTOCOL_HWMP;
4803 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4804 setup->path_metric =
4805 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4806 IEEE80211_PATH_METRIC_VENDOR :
4807 IEEE80211_PATH_METRIC_AIRTIME;
4810 if (tb[NL80211_MESH_SETUP_IE]) {
4811 struct nlattr *ieattr =
4812 tb[NL80211_MESH_SETUP_IE];
4813 if (!is_valid_ie_attr(ieattr))
4814 return -EINVAL;
4815 setup->ie = nla_data(ieattr);
4816 setup->ie_len = nla_len(ieattr);
4818 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4819 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4820 return -EINVAL;
4821 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
4822 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4823 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
4824 if (setup->is_secure)
4825 setup->user_mpm = true;
4827 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4828 if (!setup->user_mpm)
4829 return -EINVAL;
4830 setup->auth_id =
4831 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4834 return 0;
4837 static int nl80211_update_mesh_config(struct sk_buff *skb,
4838 struct genl_info *info)
4840 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4841 struct net_device *dev = info->user_ptr[1];
4842 struct wireless_dev *wdev = dev->ieee80211_ptr;
4843 struct mesh_config cfg;
4844 u32 mask;
4845 int err;
4847 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4848 return -EOPNOTSUPP;
4850 if (!rdev->ops->update_mesh_config)
4851 return -EOPNOTSUPP;
4853 err = nl80211_parse_mesh_config(info, &cfg, &mask);
4854 if (err)
4855 return err;
4857 wdev_lock(wdev);
4858 if (!wdev->mesh_id_len)
4859 err = -ENOLINK;
4861 if (!err)
4862 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
4864 wdev_unlock(wdev);
4866 return err;
4869 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4871 const struct ieee80211_regdomain *regdom;
4872 struct sk_buff *msg;
4873 void *hdr = NULL;
4874 struct nlattr *nl_reg_rules;
4875 unsigned int i;
4877 if (!cfg80211_regdomain)
4878 return -EINVAL;
4880 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4881 if (!msg)
4882 return -ENOBUFS;
4884 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4885 NL80211_CMD_GET_REG);
4886 if (!hdr)
4887 goto put_failure;
4889 if (reg_last_request_cell_base() &&
4890 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4891 NL80211_USER_REG_HINT_CELL_BASE))
4892 goto nla_put_failure;
4894 rcu_read_lock();
4895 regdom = rcu_dereference(cfg80211_regdomain);
4897 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4898 (regdom->dfs_region &&
4899 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4900 goto nla_put_failure_rcu;
4902 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4903 if (!nl_reg_rules)
4904 goto nla_put_failure_rcu;
4906 for (i = 0; i < regdom->n_reg_rules; i++) {
4907 struct nlattr *nl_reg_rule;
4908 const struct ieee80211_reg_rule *reg_rule;
4909 const struct ieee80211_freq_range *freq_range;
4910 const struct ieee80211_power_rule *power_rule;
4912 reg_rule = &regdom->reg_rules[i];
4913 freq_range = &reg_rule->freq_range;
4914 power_rule = &reg_rule->power_rule;
4916 nl_reg_rule = nla_nest_start(msg, i);
4917 if (!nl_reg_rule)
4918 goto nla_put_failure_rcu;
4920 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4921 reg_rule->flags) ||
4922 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4923 freq_range->start_freq_khz) ||
4924 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4925 freq_range->end_freq_khz) ||
4926 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4927 freq_range->max_bandwidth_khz) ||
4928 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4929 power_rule->max_antenna_gain) ||
4930 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4931 power_rule->max_eirp))
4932 goto nla_put_failure_rcu;
4934 nla_nest_end(msg, nl_reg_rule);
4936 rcu_read_unlock();
4938 nla_nest_end(msg, nl_reg_rules);
4940 genlmsg_end(msg, hdr);
4941 return genlmsg_reply(msg, info);
4943 nla_put_failure_rcu:
4944 rcu_read_unlock();
4945 nla_put_failure:
4946 genlmsg_cancel(msg, hdr);
4947 put_failure:
4948 nlmsg_free(msg);
4949 return -EMSGSIZE;
4952 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4954 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4955 struct nlattr *nl_reg_rule;
4956 char *alpha2 = NULL;
4957 int rem_reg_rules = 0, r = 0;
4958 u32 num_rules = 0, rule_idx = 0, size_of_regd;
4959 u8 dfs_region = 0;
4960 struct ieee80211_regdomain *rd = NULL;
4962 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4963 return -EINVAL;
4965 if (!info->attrs[NL80211_ATTR_REG_RULES])
4966 return -EINVAL;
4968 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4970 if (info->attrs[NL80211_ATTR_DFS_REGION])
4971 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4973 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4974 rem_reg_rules) {
4975 num_rules++;
4976 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
4977 return -EINVAL;
4980 size_of_regd = sizeof(struct ieee80211_regdomain) +
4981 num_rules * sizeof(struct ieee80211_reg_rule);
4983 rd = kzalloc(size_of_regd, GFP_KERNEL);
4984 if (!rd)
4985 return -ENOMEM;
4987 rd->n_reg_rules = num_rules;
4988 rd->alpha2[0] = alpha2[0];
4989 rd->alpha2[1] = alpha2[1];
4992 * Disable DFS master mode if the DFS region was
4993 * not supported or known on this kernel.
4995 if (reg_supported_dfs_region(dfs_region))
4996 rd->dfs_region = dfs_region;
4998 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4999 rem_reg_rules) {
5000 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5001 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5002 reg_rule_policy);
5003 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5004 if (r)
5005 goto bad_reg;
5007 rule_idx++;
5009 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5010 r = -EINVAL;
5011 goto bad_reg;
5015 r = set_regdom(rd);
5016 /* set_regdom took ownership */
5017 rd = NULL;
5019 bad_reg:
5020 kfree(rd);
5021 return r;
5024 static int validate_scan_freqs(struct nlattr *freqs)
5026 struct nlattr *attr1, *attr2;
5027 int n_channels = 0, tmp1, tmp2;
5029 nla_for_each_nested(attr1, freqs, tmp1) {
5030 n_channels++;
5032 * Some hardware has a limited channel list for
5033 * scanning, and it is pretty much nonsensical
5034 * to scan for a channel twice, so disallow that
5035 * and don't require drivers to check that the
5036 * channel list they get isn't longer than what
5037 * they can scan, as long as they can scan all
5038 * the channels they registered at once.
5040 nla_for_each_nested(attr2, freqs, tmp2)
5041 if (attr1 != attr2 &&
5042 nla_get_u32(attr1) == nla_get_u32(attr2))
5043 return 0;
5046 return n_channels;
5049 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5052 struct wireless_dev *wdev = info->user_ptr[1];
5053 struct cfg80211_scan_request *request;
5054 struct nlattr *attr;
5055 struct wiphy *wiphy;
5056 int err, tmp, n_ssids = 0, n_channels, i;
5057 size_t ie_len;
5059 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5060 return -EINVAL;
5062 wiphy = &rdev->wiphy;
5064 if (!rdev->ops->scan)
5065 return -EOPNOTSUPP;
5067 if (rdev->scan_req) {
5068 err = -EBUSY;
5069 goto unlock;
5072 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5073 n_channels = validate_scan_freqs(
5074 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5075 if (!n_channels) {
5076 err = -EINVAL;
5077 goto unlock;
5079 } else {
5080 enum ieee80211_band band;
5081 n_channels = 0;
5083 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5084 if (wiphy->bands[band])
5085 n_channels += wiphy->bands[band]->n_channels;
5088 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5089 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5090 n_ssids++;
5092 if (n_ssids > wiphy->max_scan_ssids) {
5093 err = -EINVAL;
5094 goto unlock;
5097 if (info->attrs[NL80211_ATTR_IE])
5098 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5099 else
5100 ie_len = 0;
5102 if (ie_len > wiphy->max_scan_ie_len) {
5103 err = -EINVAL;
5104 goto unlock;
5107 request = kzalloc(sizeof(*request)
5108 + sizeof(*request->ssids) * n_ssids
5109 + sizeof(*request->channels) * n_channels
5110 + ie_len, GFP_KERNEL);
5111 if (!request) {
5112 err = -ENOMEM;
5113 goto unlock;
5116 if (n_ssids)
5117 request->ssids = (void *)&request->channels[n_channels];
5118 request->n_ssids = n_ssids;
5119 if (ie_len) {
5120 if (request->ssids)
5121 request->ie = (void *)(request->ssids + n_ssids);
5122 else
5123 request->ie = (void *)(request->channels + n_channels);
5126 i = 0;
5127 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5128 /* user specified, bail out if channel not found */
5129 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
5130 struct ieee80211_channel *chan;
5132 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5134 if (!chan) {
5135 err = -EINVAL;
5136 goto out_free;
5139 /* ignore disabled channels */
5140 if (chan->flags & IEEE80211_CHAN_DISABLED)
5141 continue;
5143 request->channels[i] = chan;
5144 i++;
5146 } else {
5147 enum ieee80211_band band;
5149 /* all channels */
5150 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5151 int j;
5152 if (!wiphy->bands[band])
5153 continue;
5154 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5155 struct ieee80211_channel *chan;
5157 chan = &wiphy->bands[band]->channels[j];
5159 if (chan->flags & IEEE80211_CHAN_DISABLED)
5160 continue;
5162 request->channels[i] = chan;
5163 i++;
5168 if (!i) {
5169 err = -EINVAL;
5170 goto out_free;
5173 request->n_channels = i;
5175 i = 0;
5176 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5177 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
5178 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5179 err = -EINVAL;
5180 goto out_free;
5182 request->ssids[i].ssid_len = nla_len(attr);
5183 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
5184 i++;
5188 if (info->attrs[NL80211_ATTR_IE]) {
5189 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5190 memcpy((void *)request->ie,
5191 nla_data(info->attrs[NL80211_ATTR_IE]),
5192 request->ie_len);
5195 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
5196 if (wiphy->bands[i])
5197 request->rates[i] =
5198 (1 << wiphy->bands[i]->n_bitrates) - 1;
5200 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5201 nla_for_each_nested(attr,
5202 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5203 tmp) {
5204 enum ieee80211_band band = nla_type(attr);
5206 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
5207 err = -EINVAL;
5208 goto out_free;
5210 err = ieee80211_get_ratemask(wiphy->bands[band],
5211 nla_data(attr),
5212 nla_len(attr),
5213 &request->rates[band]);
5214 if (err)
5215 goto out_free;
5219 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5220 request->flags = nla_get_u32(
5221 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5222 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5223 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5224 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5225 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
5226 err = -EOPNOTSUPP;
5227 goto out_free;
5231 request->no_cck =
5232 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5234 request->wdev = wdev;
5235 request->wiphy = &rdev->wiphy;
5236 request->scan_start = jiffies;
5238 rdev->scan_req = request;
5239 err = rdev_scan(rdev, request);
5241 if (!err) {
5242 nl80211_send_scan_start(rdev, wdev);
5243 if (wdev->netdev)
5244 dev_hold(wdev->netdev);
5245 } else {
5246 out_free:
5247 rdev->scan_req = NULL;
5248 kfree(request);
5251 unlock:
5252 return err;
5255 static int nl80211_start_sched_scan(struct sk_buff *skb,
5256 struct genl_info *info)
5258 struct cfg80211_sched_scan_request *request;
5259 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5260 struct net_device *dev = info->user_ptr[1];
5261 struct nlattr *attr;
5262 struct wiphy *wiphy;
5263 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
5264 u32 interval;
5265 enum ieee80211_band band;
5266 size_t ie_len;
5267 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
5269 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5270 !rdev->ops->sched_scan_start)
5271 return -EOPNOTSUPP;
5273 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5274 return -EINVAL;
5276 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5277 return -EINVAL;
5279 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5280 if (interval == 0)
5281 return -EINVAL;
5283 wiphy = &rdev->wiphy;
5285 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5286 n_channels = validate_scan_freqs(
5287 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5288 if (!n_channels)
5289 return -EINVAL;
5290 } else {
5291 n_channels = 0;
5293 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5294 if (wiphy->bands[band])
5295 n_channels += wiphy->bands[band]->n_channels;
5298 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5299 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5300 tmp)
5301 n_ssids++;
5303 if (n_ssids > wiphy->max_sched_scan_ssids)
5304 return -EINVAL;
5306 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5307 nla_for_each_nested(attr,
5308 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5309 tmp)
5310 n_match_sets++;
5312 if (n_match_sets > wiphy->max_match_sets)
5313 return -EINVAL;
5315 if (info->attrs[NL80211_ATTR_IE])
5316 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5317 else
5318 ie_len = 0;
5320 if (ie_len > wiphy->max_sched_scan_ie_len)
5321 return -EINVAL;
5323 if (rdev->sched_scan_req) {
5324 err = -EINPROGRESS;
5325 goto out;
5328 request = kzalloc(sizeof(*request)
5329 + sizeof(*request->ssids) * n_ssids
5330 + sizeof(*request->match_sets) * n_match_sets
5331 + sizeof(*request->channels) * n_channels
5332 + ie_len, GFP_KERNEL);
5333 if (!request) {
5334 err = -ENOMEM;
5335 goto out;
5338 if (n_ssids)
5339 request->ssids = (void *)&request->channels[n_channels];
5340 request->n_ssids = n_ssids;
5341 if (ie_len) {
5342 if (request->ssids)
5343 request->ie = (void *)(request->ssids + n_ssids);
5344 else
5345 request->ie = (void *)(request->channels + n_channels);
5348 if (n_match_sets) {
5349 if (request->ie)
5350 request->match_sets = (void *)(request->ie + ie_len);
5351 else if (request->ssids)
5352 request->match_sets =
5353 (void *)(request->ssids + n_ssids);
5354 else
5355 request->match_sets =
5356 (void *)(request->channels + n_channels);
5358 request->n_match_sets = n_match_sets;
5360 i = 0;
5361 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5362 /* user specified, bail out if channel not found */
5363 nla_for_each_nested(attr,
5364 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5365 tmp) {
5366 struct ieee80211_channel *chan;
5368 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5370 if (!chan) {
5371 err = -EINVAL;
5372 goto out_free;
5375 /* ignore disabled channels */
5376 if (chan->flags & IEEE80211_CHAN_DISABLED)
5377 continue;
5379 request->channels[i] = chan;
5380 i++;
5382 } else {
5383 /* all channels */
5384 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5385 int j;
5386 if (!wiphy->bands[band])
5387 continue;
5388 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5389 struct ieee80211_channel *chan;
5391 chan = &wiphy->bands[band]->channels[j];
5393 if (chan->flags & IEEE80211_CHAN_DISABLED)
5394 continue;
5396 request->channels[i] = chan;
5397 i++;
5402 if (!i) {
5403 err = -EINVAL;
5404 goto out_free;
5407 request->n_channels = i;
5409 i = 0;
5410 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5411 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5412 tmp) {
5413 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5414 err = -EINVAL;
5415 goto out_free;
5417 request->ssids[i].ssid_len = nla_len(attr);
5418 memcpy(request->ssids[i].ssid, nla_data(attr),
5419 nla_len(attr));
5420 i++;
5424 i = 0;
5425 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5426 nla_for_each_nested(attr,
5427 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5428 tmp) {
5429 struct nlattr *ssid, *rssi;
5431 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5432 nla_data(attr), nla_len(attr),
5433 nl80211_match_policy);
5434 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
5435 if (ssid) {
5436 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5437 err = -EINVAL;
5438 goto out_free;
5440 memcpy(request->match_sets[i].ssid.ssid,
5441 nla_data(ssid), nla_len(ssid));
5442 request->match_sets[i].ssid.ssid_len =
5443 nla_len(ssid);
5445 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5446 if (rssi)
5447 request->rssi_thold = nla_get_u32(rssi);
5448 else
5449 request->rssi_thold =
5450 NL80211_SCAN_RSSI_THOLD_OFF;
5451 i++;
5455 if (info->attrs[NL80211_ATTR_IE]) {
5456 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5457 memcpy((void *)request->ie,
5458 nla_data(info->attrs[NL80211_ATTR_IE]),
5459 request->ie_len);
5462 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5463 request->flags = nla_get_u32(
5464 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5465 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5466 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5467 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5468 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
5469 err = -EOPNOTSUPP;
5470 goto out_free;
5474 request->dev = dev;
5475 request->wiphy = &rdev->wiphy;
5476 request->interval = interval;
5477 request->scan_start = jiffies;
5479 err = rdev_sched_scan_start(rdev, dev, request);
5480 if (!err) {
5481 rdev->sched_scan_req = request;
5482 nl80211_send_sched_scan(rdev, dev,
5483 NL80211_CMD_START_SCHED_SCAN);
5484 goto out;
5487 out_free:
5488 kfree(request);
5489 out:
5490 return err;
5493 static int nl80211_stop_sched_scan(struct sk_buff *skb,
5494 struct genl_info *info)
5496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5498 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5499 !rdev->ops->sched_scan_stop)
5500 return -EOPNOTSUPP;
5502 return __cfg80211_stop_sched_scan(rdev, false);
5505 static int nl80211_start_radar_detection(struct sk_buff *skb,
5506 struct genl_info *info)
5508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5509 struct net_device *dev = info->user_ptr[1];
5510 struct wireless_dev *wdev = dev->ieee80211_ptr;
5511 struct cfg80211_chan_def chandef;
5512 int err;
5514 err = nl80211_parse_chandef(rdev, info, &chandef);
5515 if (err)
5516 return err;
5518 if (wdev->cac_started)
5519 return -EBUSY;
5521 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5522 if (err < 0)
5523 return err;
5525 if (err == 0)
5526 return -EINVAL;
5528 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5529 return -EINVAL;
5531 if (!rdev->ops->start_radar_detection)
5532 return -EOPNOTSUPP;
5534 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5535 chandef.chan, CHAN_MODE_SHARED,
5536 BIT(chandef.width));
5537 if (err)
5538 return err;
5540 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5541 if (!err) {
5542 wdev->channel = chandef.chan;
5543 wdev->cac_started = true;
5544 wdev->cac_start_time = jiffies;
5546 return err;
5549 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5550 u32 seq, int flags,
5551 struct cfg80211_registered_device *rdev,
5552 struct wireless_dev *wdev,
5553 struct cfg80211_internal_bss *intbss)
5555 struct cfg80211_bss *res = &intbss->pub;
5556 const struct cfg80211_bss_ies *ies;
5557 void *hdr;
5558 struct nlattr *bss;
5559 bool tsf = false;
5561 ASSERT_WDEV_LOCK(wdev);
5563 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
5564 NL80211_CMD_NEW_SCAN_RESULTS);
5565 if (!hdr)
5566 return -1;
5568 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5570 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5571 goto nla_put_failure;
5572 if (wdev->netdev &&
5573 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5574 goto nla_put_failure;
5575 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5576 goto nla_put_failure;
5578 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5579 if (!bss)
5580 goto nla_put_failure;
5581 if ((!is_zero_ether_addr(res->bssid) &&
5582 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
5583 goto nla_put_failure;
5585 rcu_read_lock();
5586 ies = rcu_dereference(res->ies);
5587 if (ies) {
5588 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5589 goto fail_unlock_rcu;
5590 tsf = true;
5591 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5592 ies->len, ies->data))
5593 goto fail_unlock_rcu;
5595 ies = rcu_dereference(res->beacon_ies);
5596 if (ies) {
5597 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5598 goto fail_unlock_rcu;
5599 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5600 ies->len, ies->data))
5601 goto fail_unlock_rcu;
5603 rcu_read_unlock();
5605 if (res->beacon_interval &&
5606 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5607 goto nla_put_failure;
5608 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5609 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5610 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5611 jiffies_to_msecs(jiffies - intbss->ts)))
5612 goto nla_put_failure;
5614 switch (rdev->wiphy.signal_type) {
5615 case CFG80211_SIGNAL_TYPE_MBM:
5616 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5617 goto nla_put_failure;
5618 break;
5619 case CFG80211_SIGNAL_TYPE_UNSPEC:
5620 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5621 goto nla_put_failure;
5622 break;
5623 default:
5624 break;
5627 switch (wdev->iftype) {
5628 case NL80211_IFTYPE_P2P_CLIENT:
5629 case NL80211_IFTYPE_STATION:
5630 if (intbss == wdev->current_bss &&
5631 nla_put_u32(msg, NL80211_BSS_STATUS,
5632 NL80211_BSS_STATUS_ASSOCIATED))
5633 goto nla_put_failure;
5634 break;
5635 case NL80211_IFTYPE_ADHOC:
5636 if (intbss == wdev->current_bss &&
5637 nla_put_u32(msg, NL80211_BSS_STATUS,
5638 NL80211_BSS_STATUS_IBSS_JOINED))
5639 goto nla_put_failure;
5640 break;
5641 default:
5642 break;
5645 nla_nest_end(msg, bss);
5647 return genlmsg_end(msg, hdr);
5649 fail_unlock_rcu:
5650 rcu_read_unlock();
5651 nla_put_failure:
5652 genlmsg_cancel(msg, hdr);
5653 return -EMSGSIZE;
5656 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
5658 struct cfg80211_registered_device *rdev;
5659 struct cfg80211_internal_bss *scan;
5660 struct wireless_dev *wdev;
5661 int start = cb->args[2], idx = 0;
5662 int err;
5664 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5665 if (err)
5666 return err;
5668 wdev_lock(wdev);
5669 spin_lock_bh(&rdev->bss_lock);
5670 cfg80211_bss_expire(rdev);
5672 cb->seq = rdev->bss_generation;
5674 list_for_each_entry(scan, &rdev->bss_list, list) {
5675 if (++idx <= start)
5676 continue;
5677 if (nl80211_send_bss(skb, cb,
5678 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5679 rdev, wdev, scan) < 0) {
5680 idx--;
5681 break;
5685 spin_unlock_bh(&rdev->bss_lock);
5686 wdev_unlock(wdev);
5688 cb->args[2] = idx;
5689 nl80211_finish_wdev_dump(rdev);
5691 return skb->len;
5694 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
5695 int flags, struct net_device *dev,
5696 struct survey_info *survey)
5698 void *hdr;
5699 struct nlattr *infoattr;
5701 hdr = nl80211hdr_put(msg, portid, seq, flags,
5702 NL80211_CMD_NEW_SURVEY_RESULTS);
5703 if (!hdr)
5704 return -ENOMEM;
5706 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5707 goto nla_put_failure;
5709 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5710 if (!infoattr)
5711 goto nla_put_failure;
5713 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5714 survey->channel->center_freq))
5715 goto nla_put_failure;
5717 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5718 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5719 goto nla_put_failure;
5720 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5721 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5722 goto nla_put_failure;
5723 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5724 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5725 survey->channel_time))
5726 goto nla_put_failure;
5727 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5728 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5729 survey->channel_time_busy))
5730 goto nla_put_failure;
5731 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5732 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5733 survey->channel_time_ext_busy))
5734 goto nla_put_failure;
5735 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5736 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5737 survey->channel_time_rx))
5738 goto nla_put_failure;
5739 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5740 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5741 survey->channel_time_tx))
5742 goto nla_put_failure;
5744 nla_nest_end(msg, infoattr);
5746 return genlmsg_end(msg, hdr);
5748 nla_put_failure:
5749 genlmsg_cancel(msg, hdr);
5750 return -EMSGSIZE;
5753 static int nl80211_dump_survey(struct sk_buff *skb,
5754 struct netlink_callback *cb)
5756 struct survey_info survey;
5757 struct cfg80211_registered_device *dev;
5758 struct wireless_dev *wdev;
5759 int survey_idx = cb->args[2];
5760 int res;
5762 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
5763 if (res)
5764 return res;
5766 if (!wdev->netdev) {
5767 res = -EINVAL;
5768 goto out_err;
5771 if (!dev->ops->dump_survey) {
5772 res = -EOPNOTSUPP;
5773 goto out_err;
5776 while (1) {
5777 struct ieee80211_channel *chan;
5779 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
5780 if (res == -ENOENT)
5781 break;
5782 if (res)
5783 goto out_err;
5785 /* Survey without a channel doesn't make sense */
5786 if (!survey.channel) {
5787 res = -EINVAL;
5788 goto out;
5791 chan = ieee80211_get_channel(&dev->wiphy,
5792 survey.channel->center_freq);
5793 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5794 survey_idx++;
5795 continue;
5798 if (nl80211_send_survey(skb,
5799 NETLINK_CB(cb->skb).portid,
5800 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5801 wdev->netdev, &survey) < 0)
5802 goto out;
5803 survey_idx++;
5806 out:
5807 cb->args[2] = survey_idx;
5808 res = skb->len;
5809 out_err:
5810 nl80211_finish_wdev_dump(dev);
5811 return res;
5814 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5816 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5817 NL80211_WPA_VERSION_2));
5820 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5823 struct net_device *dev = info->user_ptr[1];
5824 struct ieee80211_channel *chan;
5825 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5826 int err, ssid_len, ie_len = 0, sae_data_len = 0;
5827 enum nl80211_auth_type auth_type;
5828 struct key_parse key;
5829 bool local_state_change;
5831 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5832 return -EINVAL;
5834 if (!info->attrs[NL80211_ATTR_MAC])
5835 return -EINVAL;
5837 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5838 return -EINVAL;
5840 if (!info->attrs[NL80211_ATTR_SSID])
5841 return -EINVAL;
5843 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5844 return -EINVAL;
5846 err = nl80211_parse_key(info, &key);
5847 if (err)
5848 return err;
5850 if (key.idx >= 0) {
5851 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5852 return -EINVAL;
5853 if (!key.p.key || !key.p.key_len)
5854 return -EINVAL;
5855 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5856 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5857 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5858 key.p.key_len != WLAN_KEY_LEN_WEP104))
5859 return -EINVAL;
5860 if (key.idx > 4)
5861 return -EINVAL;
5862 } else {
5863 key.p.key_len = 0;
5864 key.p.key = NULL;
5867 if (key.idx >= 0) {
5868 int i;
5869 bool ok = false;
5870 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5871 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5872 ok = true;
5873 break;
5876 if (!ok)
5877 return -EINVAL;
5880 if (!rdev->ops->auth)
5881 return -EOPNOTSUPP;
5883 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5884 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5885 return -EOPNOTSUPP;
5887 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5888 chan = ieee80211_get_channel(&rdev->wiphy,
5889 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5890 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5891 return -EINVAL;
5893 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5894 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5896 if (info->attrs[NL80211_ATTR_IE]) {
5897 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5898 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5901 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5902 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
5903 return -EINVAL;
5905 if (auth_type == NL80211_AUTHTYPE_SAE &&
5906 !info->attrs[NL80211_ATTR_SAE_DATA])
5907 return -EINVAL;
5909 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5910 if (auth_type != NL80211_AUTHTYPE_SAE)
5911 return -EINVAL;
5912 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5913 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5914 /* need to include at least Auth Transaction and Status Code */
5915 if (sae_data_len < 4)
5916 return -EINVAL;
5919 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5922 * Since we no longer track auth state, ignore
5923 * requests to only change local state.
5925 if (local_state_change)
5926 return 0;
5928 wdev_lock(dev->ieee80211_ptr);
5929 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5930 ssid, ssid_len, ie, ie_len,
5931 key.p.key, key.p.key_len, key.idx,
5932 sae_data, sae_data_len);
5933 wdev_unlock(dev->ieee80211_ptr);
5934 return err;
5937 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5938 struct genl_info *info,
5939 struct cfg80211_crypto_settings *settings,
5940 int cipher_limit)
5942 memset(settings, 0, sizeof(*settings));
5944 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5946 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5947 u16 proto;
5948 proto = nla_get_u16(
5949 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5950 settings->control_port_ethertype = cpu_to_be16(proto);
5951 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5952 proto != ETH_P_PAE)
5953 return -EINVAL;
5954 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5955 settings->control_port_no_encrypt = true;
5956 } else
5957 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5959 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5960 void *data;
5961 int len, i;
5963 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5964 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5965 settings->n_ciphers_pairwise = len / sizeof(u32);
5967 if (len % sizeof(u32))
5968 return -EINVAL;
5970 if (settings->n_ciphers_pairwise > cipher_limit)
5971 return -EINVAL;
5973 memcpy(settings->ciphers_pairwise, data, len);
5975 for (i = 0; i < settings->n_ciphers_pairwise; i++)
5976 if (!cfg80211_supported_cipher_suite(
5977 &rdev->wiphy,
5978 settings->ciphers_pairwise[i]))
5979 return -EINVAL;
5982 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5983 settings->cipher_group =
5984 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
5985 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5986 settings->cipher_group))
5987 return -EINVAL;
5990 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5991 settings->wpa_versions =
5992 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5993 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5994 return -EINVAL;
5997 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5998 void *data;
5999 int len;
6001 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6002 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6003 settings->n_akm_suites = len / sizeof(u32);
6005 if (len % sizeof(u32))
6006 return -EINVAL;
6008 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6009 return -EINVAL;
6011 memcpy(settings->akm_suites, data, len);
6014 return 0;
6017 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6019 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6020 struct net_device *dev = info->user_ptr[1];
6021 struct ieee80211_channel *chan;
6022 struct cfg80211_assoc_request req = {};
6023 const u8 *bssid, *ssid;
6024 int err, ssid_len = 0;
6026 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6027 return -EINVAL;
6029 if (!info->attrs[NL80211_ATTR_MAC] ||
6030 !info->attrs[NL80211_ATTR_SSID] ||
6031 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6032 return -EINVAL;
6034 if (!rdev->ops->assoc)
6035 return -EOPNOTSUPP;
6037 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6038 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6039 return -EOPNOTSUPP;
6041 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6043 chan = ieee80211_get_channel(&rdev->wiphy,
6044 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6045 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6046 return -EINVAL;
6048 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6049 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6051 if (info->attrs[NL80211_ATTR_IE]) {
6052 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6053 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6056 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6057 enum nl80211_mfp mfp =
6058 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6059 if (mfp == NL80211_MFP_REQUIRED)
6060 req.use_mfp = true;
6061 else if (mfp != NL80211_MFP_NO)
6062 return -EINVAL;
6065 if (info->attrs[NL80211_ATTR_PREV_BSSID])
6066 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
6068 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6069 req.flags |= ASSOC_REQ_DISABLE_HT;
6071 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6072 memcpy(&req.ht_capa_mask,
6073 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6074 sizeof(req.ht_capa_mask));
6076 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6077 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6078 return -EINVAL;
6079 memcpy(&req.ht_capa,
6080 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6081 sizeof(req.ht_capa));
6084 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6085 req.flags |= ASSOC_REQ_DISABLE_VHT;
6087 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6088 memcpy(&req.vht_capa_mask,
6089 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6090 sizeof(req.vht_capa_mask));
6092 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6093 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6094 return -EINVAL;
6095 memcpy(&req.vht_capa,
6096 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6097 sizeof(req.vht_capa));
6100 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
6101 if (!err) {
6102 wdev_lock(dev->ieee80211_ptr);
6103 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6104 ssid, ssid_len, &req);
6105 wdev_unlock(dev->ieee80211_ptr);
6108 return err;
6111 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6114 struct net_device *dev = info->user_ptr[1];
6115 const u8 *ie = NULL, *bssid;
6116 int ie_len = 0, err;
6117 u16 reason_code;
6118 bool local_state_change;
6120 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6121 return -EINVAL;
6123 if (!info->attrs[NL80211_ATTR_MAC])
6124 return -EINVAL;
6126 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6127 return -EINVAL;
6129 if (!rdev->ops->deauth)
6130 return -EOPNOTSUPP;
6132 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6133 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6134 return -EOPNOTSUPP;
6136 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6138 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6139 if (reason_code == 0) {
6140 /* Reason Code 0 is reserved */
6141 return -EINVAL;
6144 if (info->attrs[NL80211_ATTR_IE]) {
6145 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6146 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6149 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6151 wdev_lock(dev->ieee80211_ptr);
6152 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6153 local_state_change);
6154 wdev_unlock(dev->ieee80211_ptr);
6155 return err;
6158 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6161 struct net_device *dev = info->user_ptr[1];
6162 const u8 *ie = NULL, *bssid;
6163 int ie_len = 0, err;
6164 u16 reason_code;
6165 bool local_state_change;
6167 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6168 return -EINVAL;
6170 if (!info->attrs[NL80211_ATTR_MAC])
6171 return -EINVAL;
6173 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6174 return -EINVAL;
6176 if (!rdev->ops->disassoc)
6177 return -EOPNOTSUPP;
6179 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6180 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6181 return -EOPNOTSUPP;
6183 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6185 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6186 if (reason_code == 0) {
6187 /* Reason Code 0 is reserved */
6188 return -EINVAL;
6191 if (info->attrs[NL80211_ATTR_IE]) {
6192 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6193 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6196 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6198 wdev_lock(dev->ieee80211_ptr);
6199 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6200 local_state_change);
6201 wdev_unlock(dev->ieee80211_ptr);
6202 return err;
6205 static bool
6206 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6207 int mcast_rate[IEEE80211_NUM_BANDS],
6208 int rateval)
6210 struct wiphy *wiphy = &rdev->wiphy;
6211 bool found = false;
6212 int band, i;
6214 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6215 struct ieee80211_supported_band *sband;
6217 sband = wiphy->bands[band];
6218 if (!sband)
6219 continue;
6221 for (i = 0; i < sband->n_bitrates; i++) {
6222 if (sband->bitrates[i].bitrate == rateval) {
6223 mcast_rate[band] = i + 1;
6224 found = true;
6225 break;
6230 return found;
6233 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6235 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6236 struct net_device *dev = info->user_ptr[1];
6237 struct cfg80211_ibss_params ibss;
6238 struct wiphy *wiphy;
6239 struct cfg80211_cached_keys *connkeys = NULL;
6240 int err;
6242 memset(&ibss, 0, sizeof(ibss));
6244 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6245 return -EINVAL;
6247 if (!info->attrs[NL80211_ATTR_SSID] ||
6248 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6249 return -EINVAL;
6251 ibss.beacon_interval = 100;
6253 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6254 ibss.beacon_interval =
6255 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6256 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6257 return -EINVAL;
6260 if (!rdev->ops->join_ibss)
6261 return -EOPNOTSUPP;
6263 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6264 return -EOPNOTSUPP;
6266 wiphy = &rdev->wiphy;
6268 if (info->attrs[NL80211_ATTR_MAC]) {
6269 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6271 if (!is_valid_ether_addr(ibss.bssid))
6272 return -EINVAL;
6274 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6275 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6277 if (info->attrs[NL80211_ATTR_IE]) {
6278 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6279 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6282 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6283 if (err)
6284 return err;
6286 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
6287 return -EINVAL;
6289 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6290 return -EINVAL;
6291 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6292 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
6293 return -EINVAL;
6295 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
6296 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6298 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6299 u8 *rates =
6300 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6301 int n_rates =
6302 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6303 struct ieee80211_supported_band *sband =
6304 wiphy->bands[ibss.chandef.chan->band];
6306 err = ieee80211_get_ratemask(sband, rates, n_rates,
6307 &ibss.basic_rates);
6308 if (err)
6309 return err;
6312 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6313 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6314 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6315 return -EINVAL;
6317 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6318 bool no_ht = false;
6320 connkeys = nl80211_parse_connkeys(rdev,
6321 info->attrs[NL80211_ATTR_KEYS],
6322 &no_ht);
6323 if (IS_ERR(connkeys))
6324 return PTR_ERR(connkeys);
6326 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6327 no_ht) {
6328 kfree(connkeys);
6329 return -EINVAL;
6333 ibss.control_port =
6334 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6336 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
6337 if (err)
6338 kfree(connkeys);
6339 return err;
6342 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6344 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6345 struct net_device *dev = info->user_ptr[1];
6347 if (!rdev->ops->leave_ibss)
6348 return -EOPNOTSUPP;
6350 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6351 return -EOPNOTSUPP;
6353 return cfg80211_leave_ibss(rdev, dev, false);
6356 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6359 struct net_device *dev = info->user_ptr[1];
6360 int mcast_rate[IEEE80211_NUM_BANDS];
6361 u32 nla_rate;
6362 int err;
6364 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6365 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6366 return -EOPNOTSUPP;
6368 if (!rdev->ops->set_mcast_rate)
6369 return -EOPNOTSUPP;
6371 memset(mcast_rate, 0, sizeof(mcast_rate));
6373 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6374 return -EINVAL;
6376 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6377 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6378 return -EINVAL;
6380 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6382 return err;
6386 #ifdef CONFIG_NL80211_TESTMODE
6387 static struct genl_multicast_group nl80211_testmode_mcgrp = {
6388 .name = "testmode",
6391 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6393 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6394 int err;
6396 if (!info->attrs[NL80211_ATTR_TESTDATA])
6397 return -EINVAL;
6399 err = -EOPNOTSUPP;
6400 if (rdev->ops->testmode_cmd) {
6401 rdev->testmode_info = info;
6402 err = rdev_testmode_cmd(rdev,
6403 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6404 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6405 rdev->testmode_info = NULL;
6408 return err;
6411 static int nl80211_testmode_dump(struct sk_buff *skb,
6412 struct netlink_callback *cb)
6414 struct cfg80211_registered_device *rdev;
6415 int err;
6416 long phy_idx;
6417 void *data = NULL;
6418 int data_len = 0;
6420 rtnl_lock();
6422 if (cb->args[0]) {
6424 * 0 is a valid index, but not valid for args[0],
6425 * so we need to offset by 1.
6427 phy_idx = cb->args[0] - 1;
6428 } else {
6429 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6430 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6431 nl80211_policy);
6432 if (err)
6433 goto out_err;
6435 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6436 nl80211_fam.attrbuf);
6437 if (IS_ERR(rdev)) {
6438 err = PTR_ERR(rdev);
6439 goto out_err;
6441 phy_idx = rdev->wiphy_idx;
6442 rdev = NULL;
6444 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6445 cb->args[1] =
6446 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6449 if (cb->args[1]) {
6450 data = nla_data((void *)cb->args[1]);
6451 data_len = nla_len((void *)cb->args[1]);
6454 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6455 if (!rdev) {
6456 err = -ENOENT;
6457 goto out_err;
6460 if (!rdev->ops->testmode_dump) {
6461 err = -EOPNOTSUPP;
6462 goto out_err;
6465 while (1) {
6466 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
6467 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6468 NL80211_CMD_TESTMODE);
6469 struct nlattr *tmdata;
6471 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
6472 genlmsg_cancel(skb, hdr);
6473 break;
6476 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6477 if (!tmdata) {
6478 genlmsg_cancel(skb, hdr);
6479 break;
6481 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
6482 nla_nest_end(skb, tmdata);
6484 if (err == -ENOBUFS || err == -ENOENT) {
6485 genlmsg_cancel(skb, hdr);
6486 break;
6487 } else if (err) {
6488 genlmsg_cancel(skb, hdr);
6489 goto out_err;
6492 genlmsg_end(skb, hdr);
6495 err = skb->len;
6496 /* see above */
6497 cb->args[0] = phy_idx + 1;
6498 out_err:
6499 rtnl_unlock();
6500 return err;
6503 static struct sk_buff *
6504 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
6505 int approxlen, u32 portid, u32 seq, gfp_t gfp)
6507 struct sk_buff *skb;
6508 void *hdr;
6509 struct nlattr *data;
6511 skb = nlmsg_new(approxlen + 100, gfp);
6512 if (!skb)
6513 return NULL;
6515 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
6516 if (!hdr) {
6517 kfree_skb(skb);
6518 return NULL;
6521 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6522 goto nla_put_failure;
6523 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6525 ((void **)skb->cb)[0] = rdev;
6526 ((void **)skb->cb)[1] = hdr;
6527 ((void **)skb->cb)[2] = data;
6529 return skb;
6531 nla_put_failure:
6532 kfree_skb(skb);
6533 return NULL;
6536 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6537 int approxlen)
6539 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6541 if (WARN_ON(!rdev->testmode_info))
6542 return NULL;
6544 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
6545 rdev->testmode_info->snd_portid,
6546 rdev->testmode_info->snd_seq,
6547 GFP_KERNEL);
6549 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6551 int cfg80211_testmode_reply(struct sk_buff *skb)
6553 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6554 void *hdr = ((void **)skb->cb)[1];
6555 struct nlattr *data = ((void **)skb->cb)[2];
6557 if (WARN_ON(!rdev->testmode_info)) {
6558 kfree_skb(skb);
6559 return -EINVAL;
6562 nla_nest_end(skb, data);
6563 genlmsg_end(skb, hdr);
6564 return genlmsg_reply(skb, rdev->testmode_info);
6566 EXPORT_SYMBOL(cfg80211_testmode_reply);
6568 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6569 int approxlen, gfp_t gfp)
6571 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6573 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6575 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6577 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6579 void *hdr = ((void **)skb->cb)[1];
6580 struct nlattr *data = ((void **)skb->cb)[2];
6582 nla_nest_end(skb, data);
6583 genlmsg_end(skb, hdr);
6584 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6586 EXPORT_SYMBOL(cfg80211_testmode_event);
6587 #endif
6589 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6592 struct net_device *dev = info->user_ptr[1];
6593 struct cfg80211_connect_params connect;
6594 struct wiphy *wiphy;
6595 struct cfg80211_cached_keys *connkeys = NULL;
6596 int err;
6598 memset(&connect, 0, sizeof(connect));
6600 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6601 return -EINVAL;
6603 if (!info->attrs[NL80211_ATTR_SSID] ||
6604 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6605 return -EINVAL;
6607 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6608 connect.auth_type =
6609 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
6610 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6611 NL80211_CMD_CONNECT))
6612 return -EINVAL;
6613 } else
6614 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6616 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6618 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
6619 NL80211_MAX_NR_CIPHER_SUITES);
6620 if (err)
6621 return err;
6623 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6624 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6625 return -EOPNOTSUPP;
6627 wiphy = &rdev->wiphy;
6629 connect.bg_scan_period = -1;
6630 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6631 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6632 connect.bg_scan_period =
6633 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6636 if (info->attrs[NL80211_ATTR_MAC])
6637 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6638 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6639 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6641 if (info->attrs[NL80211_ATTR_IE]) {
6642 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6643 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6646 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6647 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6648 if (connect.mfp != NL80211_MFP_REQUIRED &&
6649 connect.mfp != NL80211_MFP_NO)
6650 return -EINVAL;
6651 } else {
6652 connect.mfp = NL80211_MFP_NO;
6655 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6656 connect.channel =
6657 ieee80211_get_channel(wiphy,
6658 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6659 if (!connect.channel ||
6660 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6661 return -EINVAL;
6664 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6665 connkeys = nl80211_parse_connkeys(rdev,
6666 info->attrs[NL80211_ATTR_KEYS], NULL);
6667 if (IS_ERR(connkeys))
6668 return PTR_ERR(connkeys);
6671 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6672 connect.flags |= ASSOC_REQ_DISABLE_HT;
6674 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6675 memcpy(&connect.ht_capa_mask,
6676 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6677 sizeof(connect.ht_capa_mask));
6679 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6680 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6681 kfree(connkeys);
6682 return -EINVAL;
6684 memcpy(&connect.ht_capa,
6685 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6686 sizeof(connect.ht_capa));
6689 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6690 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6692 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6693 memcpy(&connect.vht_capa_mask,
6694 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6695 sizeof(connect.vht_capa_mask));
6697 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6698 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6699 kfree(connkeys);
6700 return -EINVAL;
6702 memcpy(&connect.vht_capa,
6703 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6704 sizeof(connect.vht_capa));
6707 wdev_lock(dev->ieee80211_ptr);
6708 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
6709 wdev_unlock(dev->ieee80211_ptr);
6710 if (err)
6711 kfree(connkeys);
6712 return err;
6715 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6718 struct net_device *dev = info->user_ptr[1];
6719 u16 reason;
6720 int ret;
6722 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6723 reason = WLAN_REASON_DEAUTH_LEAVING;
6724 else
6725 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6727 if (reason == 0)
6728 return -EINVAL;
6730 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6731 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6732 return -EOPNOTSUPP;
6734 wdev_lock(dev->ieee80211_ptr);
6735 ret = cfg80211_disconnect(rdev, dev, reason, true);
6736 wdev_unlock(dev->ieee80211_ptr);
6737 return ret;
6740 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6743 struct net *net;
6744 int err;
6745 u32 pid;
6747 if (!info->attrs[NL80211_ATTR_PID])
6748 return -EINVAL;
6750 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6752 net = get_net_ns_by_pid(pid);
6753 if (IS_ERR(net))
6754 return PTR_ERR(net);
6756 err = 0;
6758 /* check if anything to do */
6759 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6760 err = cfg80211_switch_netns(rdev, net);
6762 put_net(net);
6763 return err;
6766 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6769 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6770 struct cfg80211_pmksa *pmksa) = NULL;
6771 struct net_device *dev = info->user_ptr[1];
6772 struct cfg80211_pmksa pmksa;
6774 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6776 if (!info->attrs[NL80211_ATTR_MAC])
6777 return -EINVAL;
6779 if (!info->attrs[NL80211_ATTR_PMKID])
6780 return -EINVAL;
6782 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6783 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6785 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6786 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6787 return -EOPNOTSUPP;
6789 switch (info->genlhdr->cmd) {
6790 case NL80211_CMD_SET_PMKSA:
6791 rdev_ops = rdev->ops->set_pmksa;
6792 break;
6793 case NL80211_CMD_DEL_PMKSA:
6794 rdev_ops = rdev->ops->del_pmksa;
6795 break;
6796 default:
6797 WARN_ON(1);
6798 break;
6801 if (!rdev_ops)
6802 return -EOPNOTSUPP;
6804 return rdev_ops(&rdev->wiphy, dev, &pmksa);
6807 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6809 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6810 struct net_device *dev = info->user_ptr[1];
6812 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6813 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6814 return -EOPNOTSUPP;
6816 if (!rdev->ops->flush_pmksa)
6817 return -EOPNOTSUPP;
6819 return rdev_flush_pmksa(rdev, dev);
6822 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6825 struct net_device *dev = info->user_ptr[1];
6826 u8 action_code, dialog_token;
6827 u16 status_code;
6828 u8 *peer;
6830 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6831 !rdev->ops->tdls_mgmt)
6832 return -EOPNOTSUPP;
6834 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6835 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6836 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6837 !info->attrs[NL80211_ATTR_IE] ||
6838 !info->attrs[NL80211_ATTR_MAC])
6839 return -EINVAL;
6841 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6842 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6843 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6844 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6846 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6847 dialog_token, status_code,
6848 nla_data(info->attrs[NL80211_ATTR_IE]),
6849 nla_len(info->attrs[NL80211_ATTR_IE]));
6852 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6855 struct net_device *dev = info->user_ptr[1];
6856 enum nl80211_tdls_operation operation;
6857 u8 *peer;
6859 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6860 !rdev->ops->tdls_oper)
6861 return -EOPNOTSUPP;
6863 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6864 !info->attrs[NL80211_ATTR_MAC])
6865 return -EINVAL;
6867 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6868 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6870 return rdev_tdls_oper(rdev, dev, peer, operation);
6873 static int nl80211_remain_on_channel(struct sk_buff *skb,
6874 struct genl_info *info)
6876 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6877 struct wireless_dev *wdev = info->user_ptr[1];
6878 struct cfg80211_chan_def chandef;
6879 struct sk_buff *msg;
6880 void *hdr;
6881 u64 cookie;
6882 u32 duration;
6883 int err;
6885 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6886 !info->attrs[NL80211_ATTR_DURATION])
6887 return -EINVAL;
6889 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6891 if (!rdev->ops->remain_on_channel ||
6892 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
6893 return -EOPNOTSUPP;
6896 * We should be on that channel for at least a minimum amount of
6897 * time (10ms) but no longer than the driver supports.
6899 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6900 duration > rdev->wiphy.max_remain_on_channel_duration)
6901 return -EINVAL;
6903 err = nl80211_parse_chandef(rdev, info, &chandef);
6904 if (err)
6905 return err;
6907 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6908 if (!msg)
6909 return -ENOMEM;
6911 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6912 NL80211_CMD_REMAIN_ON_CHANNEL);
6914 if (IS_ERR(hdr)) {
6915 err = PTR_ERR(hdr);
6916 goto free_msg;
6919 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6920 duration, &cookie);
6922 if (err)
6923 goto free_msg;
6925 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6926 goto nla_put_failure;
6928 genlmsg_end(msg, hdr);
6930 return genlmsg_reply(msg, info);
6932 nla_put_failure:
6933 err = -ENOBUFS;
6934 free_msg:
6935 nlmsg_free(msg);
6936 return err;
6939 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6940 struct genl_info *info)
6942 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6943 struct wireless_dev *wdev = info->user_ptr[1];
6944 u64 cookie;
6946 if (!info->attrs[NL80211_ATTR_COOKIE])
6947 return -EINVAL;
6949 if (!rdev->ops->cancel_remain_on_channel)
6950 return -EOPNOTSUPP;
6952 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6954 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
6957 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6958 u8 *rates, u8 rates_len)
6960 u8 i;
6961 u32 mask = 0;
6963 for (i = 0; i < rates_len; i++) {
6964 int rate = (rates[i] & 0x7f) * 5;
6965 int ridx;
6966 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6967 struct ieee80211_rate *srate =
6968 &sband->bitrates[ridx];
6969 if (rate == srate->bitrate) {
6970 mask |= 1 << ridx;
6971 break;
6974 if (ridx == sband->n_bitrates)
6975 return 0; /* rate not found */
6978 return mask;
6981 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6982 u8 *rates, u8 rates_len,
6983 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6985 u8 i;
6987 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6989 for (i = 0; i < rates_len; i++) {
6990 int ridx, rbit;
6992 ridx = rates[i] / 8;
6993 rbit = BIT(rates[i] % 8);
6995 /* check validity */
6996 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
6997 return false;
6999 /* check availability */
7000 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7001 mcs[ridx] |= rbit;
7002 else
7003 return false;
7006 return true;
7009 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
7010 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7011 .len = NL80211_MAX_SUPP_RATES },
7012 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7013 .len = NL80211_MAX_SUPP_HT_RATES },
7016 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7017 struct genl_info *info)
7019 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
7020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7021 struct cfg80211_bitrate_mask mask;
7022 int rem, i;
7023 struct net_device *dev = info->user_ptr[1];
7024 struct nlattr *tx_rates;
7025 struct ieee80211_supported_band *sband;
7027 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7028 return -EINVAL;
7030 if (!rdev->ops->set_bitrate_mask)
7031 return -EOPNOTSUPP;
7033 memset(&mask, 0, sizeof(mask));
7034 /* Default to all rates enabled */
7035 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7036 sband = rdev->wiphy.bands[i];
7037 mask.control[i].legacy =
7038 sband ? (1 << sband->n_bitrates) - 1 : 0;
7039 if (sband)
7040 memcpy(mask.control[i].mcs,
7041 sband->ht_cap.mcs.rx_mask,
7042 sizeof(mask.control[i].mcs));
7043 else
7044 memset(mask.control[i].mcs, 0,
7045 sizeof(mask.control[i].mcs));
7049 * The nested attribute uses enum nl80211_band as the index. This maps
7050 * directly to the enum ieee80211_band values used in cfg80211.
7052 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
7053 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7055 enum ieee80211_band band = nla_type(tx_rates);
7056 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7057 return -EINVAL;
7058 sband = rdev->wiphy.bands[band];
7059 if (sband == NULL)
7060 return -EINVAL;
7061 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7062 nla_len(tx_rates), nl80211_txattr_policy);
7063 if (tb[NL80211_TXRATE_LEGACY]) {
7064 mask.control[band].legacy = rateset_to_mask(
7065 sband,
7066 nla_data(tb[NL80211_TXRATE_LEGACY]),
7067 nla_len(tb[NL80211_TXRATE_LEGACY]));
7068 if ((mask.control[band].legacy == 0) &&
7069 nla_len(tb[NL80211_TXRATE_LEGACY]))
7070 return -EINVAL;
7072 if (tb[NL80211_TXRATE_MCS]) {
7073 if (!ht_rateset_to_mask(
7074 sband,
7075 nla_data(tb[NL80211_TXRATE_MCS]),
7076 nla_len(tb[NL80211_TXRATE_MCS]),
7077 mask.control[band].mcs))
7078 return -EINVAL;
7081 if (mask.control[band].legacy == 0) {
7082 /* don't allow empty legacy rates if HT
7083 * is not even supported. */
7084 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7085 return -EINVAL;
7087 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7088 if (mask.control[band].mcs[i])
7089 break;
7091 /* legacy and mcs rates may not be both empty */
7092 if (i == IEEE80211_HT_MCS_MASK_LEN)
7093 return -EINVAL;
7097 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
7100 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
7102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7103 struct wireless_dev *wdev = info->user_ptr[1];
7104 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
7106 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7107 return -EINVAL;
7109 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7110 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
7112 switch (wdev->iftype) {
7113 case NL80211_IFTYPE_STATION:
7114 case NL80211_IFTYPE_ADHOC:
7115 case NL80211_IFTYPE_P2P_CLIENT:
7116 case NL80211_IFTYPE_AP:
7117 case NL80211_IFTYPE_AP_VLAN:
7118 case NL80211_IFTYPE_MESH_POINT:
7119 case NL80211_IFTYPE_P2P_GO:
7120 case NL80211_IFTYPE_P2P_DEVICE:
7121 break;
7122 default:
7123 return -EOPNOTSUPP;
7126 /* not much point in registering if we can't reply */
7127 if (!rdev->ops->mgmt_tx)
7128 return -EOPNOTSUPP;
7130 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
7131 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7132 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
7135 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
7137 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7138 struct wireless_dev *wdev = info->user_ptr[1];
7139 struct cfg80211_chan_def chandef;
7140 int err;
7141 void *hdr = NULL;
7142 u64 cookie;
7143 struct sk_buff *msg = NULL;
7144 unsigned int wait = 0;
7145 bool offchan, no_cck, dont_wait_for_ack;
7147 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
7149 if (!info->attrs[NL80211_ATTR_FRAME])
7150 return -EINVAL;
7152 if (!rdev->ops->mgmt_tx)
7153 return -EOPNOTSUPP;
7155 switch (wdev->iftype) {
7156 case NL80211_IFTYPE_STATION:
7157 case NL80211_IFTYPE_ADHOC:
7158 case NL80211_IFTYPE_P2P_CLIENT:
7159 case NL80211_IFTYPE_AP:
7160 case NL80211_IFTYPE_AP_VLAN:
7161 case NL80211_IFTYPE_MESH_POINT:
7162 case NL80211_IFTYPE_P2P_GO:
7163 case NL80211_IFTYPE_P2P_DEVICE:
7164 break;
7165 default:
7166 return -EOPNOTSUPP;
7169 if (info->attrs[NL80211_ATTR_DURATION]) {
7170 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7171 return -EINVAL;
7172 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7175 * We should wait on the channel for at least a minimum amount
7176 * of time (10ms) but no longer than the driver supports.
7178 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7179 wait > rdev->wiphy.max_remain_on_channel_duration)
7180 return -EINVAL;
7184 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7186 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7187 return -EINVAL;
7189 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7191 err = nl80211_parse_chandef(rdev, info, &chandef);
7192 if (err)
7193 return err;
7195 if (!dont_wait_for_ack) {
7196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7197 if (!msg)
7198 return -ENOMEM;
7200 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7201 NL80211_CMD_FRAME);
7203 if (IS_ERR(hdr)) {
7204 err = PTR_ERR(hdr);
7205 goto free_msg;
7209 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
7210 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7211 nla_len(info->attrs[NL80211_ATTR_FRAME]),
7212 no_cck, dont_wait_for_ack, &cookie);
7213 if (err)
7214 goto free_msg;
7216 if (msg) {
7217 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7218 goto nla_put_failure;
7220 genlmsg_end(msg, hdr);
7221 return genlmsg_reply(msg, info);
7224 return 0;
7226 nla_put_failure:
7227 err = -ENOBUFS;
7228 free_msg:
7229 nlmsg_free(msg);
7230 return err;
7233 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7235 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7236 struct wireless_dev *wdev = info->user_ptr[1];
7237 u64 cookie;
7239 if (!info->attrs[NL80211_ATTR_COOKIE])
7240 return -EINVAL;
7242 if (!rdev->ops->mgmt_tx_cancel_wait)
7243 return -EOPNOTSUPP;
7245 switch (wdev->iftype) {
7246 case NL80211_IFTYPE_STATION:
7247 case NL80211_IFTYPE_ADHOC:
7248 case NL80211_IFTYPE_P2P_CLIENT:
7249 case NL80211_IFTYPE_AP:
7250 case NL80211_IFTYPE_AP_VLAN:
7251 case NL80211_IFTYPE_P2P_GO:
7252 case NL80211_IFTYPE_P2P_DEVICE:
7253 break;
7254 default:
7255 return -EOPNOTSUPP;
7258 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7260 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
7263 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7265 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7266 struct wireless_dev *wdev;
7267 struct net_device *dev = info->user_ptr[1];
7268 u8 ps_state;
7269 bool state;
7270 int err;
7272 if (!info->attrs[NL80211_ATTR_PS_STATE])
7273 return -EINVAL;
7275 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7277 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7278 return -EINVAL;
7280 wdev = dev->ieee80211_ptr;
7282 if (!rdev->ops->set_power_mgmt)
7283 return -EOPNOTSUPP;
7285 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7287 if (state == wdev->ps)
7288 return 0;
7290 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
7291 if (!err)
7292 wdev->ps = state;
7293 return err;
7296 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7298 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7299 enum nl80211_ps_state ps_state;
7300 struct wireless_dev *wdev;
7301 struct net_device *dev = info->user_ptr[1];
7302 struct sk_buff *msg;
7303 void *hdr;
7304 int err;
7306 wdev = dev->ieee80211_ptr;
7308 if (!rdev->ops->set_power_mgmt)
7309 return -EOPNOTSUPP;
7311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7312 if (!msg)
7313 return -ENOMEM;
7315 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7316 NL80211_CMD_GET_POWER_SAVE);
7317 if (!hdr) {
7318 err = -ENOBUFS;
7319 goto free_msg;
7322 if (wdev->ps)
7323 ps_state = NL80211_PS_ENABLED;
7324 else
7325 ps_state = NL80211_PS_DISABLED;
7327 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7328 goto nla_put_failure;
7330 genlmsg_end(msg, hdr);
7331 return genlmsg_reply(msg, info);
7333 nla_put_failure:
7334 err = -ENOBUFS;
7335 free_msg:
7336 nlmsg_free(msg);
7337 return err;
7340 static struct nla_policy
7341 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7342 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7343 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7344 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
7345 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7346 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7347 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
7350 static int nl80211_set_cqm_txe(struct genl_info *info,
7351 u32 rate, u32 pkts, u32 intvl)
7353 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7354 struct wireless_dev *wdev;
7355 struct net_device *dev = info->user_ptr[1];
7357 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
7358 return -EINVAL;
7360 wdev = dev->ieee80211_ptr;
7362 if (!rdev->ops->set_cqm_txe_config)
7363 return -EOPNOTSUPP;
7365 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7366 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7367 return -EOPNOTSUPP;
7369 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
7372 static int nl80211_set_cqm_rssi(struct genl_info *info,
7373 s32 threshold, u32 hysteresis)
7375 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7376 struct wireless_dev *wdev;
7377 struct net_device *dev = info->user_ptr[1];
7379 if (threshold > 0)
7380 return -EINVAL;
7382 wdev = dev->ieee80211_ptr;
7384 if (!rdev->ops->set_cqm_rssi_config)
7385 return -EOPNOTSUPP;
7387 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7388 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7389 return -EOPNOTSUPP;
7391 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
7394 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7396 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7397 struct nlattr *cqm;
7398 int err;
7400 cqm = info->attrs[NL80211_ATTR_CQM];
7401 if (!cqm) {
7402 err = -EINVAL;
7403 goto out;
7406 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7407 nl80211_attr_cqm_policy);
7408 if (err)
7409 goto out;
7411 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7412 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7413 s32 threshold;
7414 u32 hysteresis;
7415 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7416 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7417 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
7418 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7419 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7420 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7421 u32 rate, pkts, intvl;
7422 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7423 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7424 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7425 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
7426 } else
7427 err = -EINVAL;
7429 out:
7430 return err;
7433 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7436 struct net_device *dev = info->user_ptr[1];
7437 struct mesh_config cfg;
7438 struct mesh_setup setup;
7439 int err;
7441 /* start with default */
7442 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
7443 memcpy(&setup, &default_mesh_setup, sizeof(setup));
7445 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
7446 /* and parse parameters if given */
7447 err = nl80211_parse_mesh_config(info, &cfg, NULL);
7448 if (err)
7449 return err;
7452 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7453 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7454 return -EINVAL;
7456 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7457 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7459 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7460 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7461 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7462 return -EINVAL;
7464 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7465 setup.beacon_interval =
7466 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7467 if (setup.beacon_interval < 10 ||
7468 setup.beacon_interval > 10000)
7469 return -EINVAL;
7472 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7473 setup.dtim_period =
7474 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7475 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7476 return -EINVAL;
7479 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7480 /* parse additional setup parameters if given */
7481 err = nl80211_parse_mesh_setup(info, &setup);
7482 if (err)
7483 return err;
7486 if (setup.user_mpm)
7487 cfg.auto_open_plinks = false;
7489 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7490 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7491 if (err)
7492 return err;
7493 } else {
7494 /* cfg80211_join_mesh() will sort it out */
7495 setup.chandef.chan = NULL;
7498 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
7501 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7504 struct net_device *dev = info->user_ptr[1];
7506 return cfg80211_leave_mesh(rdev, dev);
7509 #ifdef CONFIG_PM
7510 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7511 struct cfg80211_registered_device *rdev)
7513 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
7514 struct nlattr *nl_pats, *nl_pat;
7515 int i, pat_len;
7517 if (!wowlan->n_patterns)
7518 return 0;
7520 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7521 if (!nl_pats)
7522 return -ENOBUFS;
7524 for (i = 0; i < wowlan->n_patterns; i++) {
7525 nl_pat = nla_nest_start(msg, i + 1);
7526 if (!nl_pat)
7527 return -ENOBUFS;
7528 pat_len = wowlan->patterns[i].pattern_len;
7529 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7530 DIV_ROUND_UP(pat_len, 8),
7531 wowlan->patterns[i].mask) ||
7532 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7533 pat_len, wowlan->patterns[i].pattern) ||
7534 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7535 wowlan->patterns[i].pkt_offset))
7536 return -ENOBUFS;
7537 nla_nest_end(msg, nl_pat);
7539 nla_nest_end(msg, nl_pats);
7541 return 0;
7544 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7545 struct cfg80211_wowlan_tcp *tcp)
7547 struct nlattr *nl_tcp;
7549 if (!tcp)
7550 return 0;
7552 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7553 if (!nl_tcp)
7554 return -ENOBUFS;
7556 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7557 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7558 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7559 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7560 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7561 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7562 tcp->payload_len, tcp->payload) ||
7563 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7564 tcp->data_interval) ||
7565 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7566 tcp->wake_len, tcp->wake_data) ||
7567 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7568 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7569 return -ENOBUFS;
7571 if (tcp->payload_seq.len &&
7572 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7573 sizeof(tcp->payload_seq), &tcp->payload_seq))
7574 return -ENOBUFS;
7576 if (tcp->payload_tok.len &&
7577 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7578 sizeof(tcp->payload_tok) + tcp->tokens_size,
7579 &tcp->payload_tok))
7580 return -ENOBUFS;
7582 nla_nest_end(msg, nl_tcp);
7584 return 0;
7587 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7590 struct sk_buff *msg;
7591 void *hdr;
7592 u32 size = NLMSG_DEFAULT_SIZE;
7594 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7595 !rdev->wiphy.wowlan.tcp)
7596 return -EOPNOTSUPP;
7598 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
7599 /* adjust size to have room for all the data */
7600 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
7601 rdev->wiphy.wowlan_config->tcp->payload_len +
7602 rdev->wiphy.wowlan_config->tcp->wake_len +
7603 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
7606 msg = nlmsg_new(size, GFP_KERNEL);
7607 if (!msg)
7608 return -ENOMEM;
7610 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7611 NL80211_CMD_GET_WOWLAN);
7612 if (!hdr)
7613 goto nla_put_failure;
7615 if (rdev->wiphy.wowlan_config) {
7616 struct nlattr *nl_wowlan;
7618 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7619 if (!nl_wowlan)
7620 goto nla_put_failure;
7622 if ((rdev->wiphy.wowlan_config->any &&
7623 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7624 (rdev->wiphy.wowlan_config->disconnect &&
7625 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7626 (rdev->wiphy.wowlan_config->magic_pkt &&
7627 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7628 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
7629 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7630 (rdev->wiphy.wowlan_config->eap_identity_req &&
7631 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7632 (rdev->wiphy.wowlan_config->four_way_handshake &&
7633 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7634 (rdev->wiphy.wowlan_config->rfkill_release &&
7635 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7636 goto nla_put_failure;
7638 if (nl80211_send_wowlan_patterns(msg, rdev))
7639 goto nla_put_failure;
7641 if (nl80211_send_wowlan_tcp(msg,
7642 rdev->wiphy.wowlan_config->tcp))
7643 goto nla_put_failure;
7645 nla_nest_end(msg, nl_wowlan);
7648 genlmsg_end(msg, hdr);
7649 return genlmsg_reply(msg, info);
7651 nla_put_failure:
7652 nlmsg_free(msg);
7653 return -ENOBUFS;
7656 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7657 struct nlattr *attr,
7658 struct cfg80211_wowlan *trig)
7660 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7661 struct cfg80211_wowlan_tcp *cfg;
7662 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7663 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7664 u32 size;
7665 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7666 int err, port;
7668 if (!rdev->wiphy.wowlan.tcp)
7669 return -EINVAL;
7671 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7672 nla_data(attr), nla_len(attr),
7673 nl80211_wowlan_tcp_policy);
7674 if (err)
7675 return err;
7677 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7678 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7679 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7680 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7681 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7682 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7683 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7684 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7685 return -EINVAL;
7687 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7688 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7689 return -EINVAL;
7691 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
7692 rdev->wiphy.wowlan.tcp->data_interval_max ||
7693 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
7694 return -EINVAL;
7696 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7697 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7698 return -EINVAL;
7700 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7701 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7702 return -EINVAL;
7704 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7705 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7707 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7708 tokens_size = tokln - sizeof(*tok);
7710 if (!tok->len || tokens_size % tok->len)
7711 return -EINVAL;
7712 if (!rdev->wiphy.wowlan.tcp->tok)
7713 return -EINVAL;
7714 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7715 return -EINVAL;
7716 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7717 return -EINVAL;
7718 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7719 return -EINVAL;
7720 if (tok->offset + tok->len > data_size)
7721 return -EINVAL;
7724 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7725 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7726 if (!rdev->wiphy.wowlan.tcp->seq)
7727 return -EINVAL;
7728 if (seq->len == 0 || seq->len > 4)
7729 return -EINVAL;
7730 if (seq->len + seq->offset > data_size)
7731 return -EINVAL;
7734 size = sizeof(*cfg);
7735 size += data_size;
7736 size += wake_size + wake_mask_size;
7737 size += tokens_size;
7739 cfg = kzalloc(size, GFP_KERNEL);
7740 if (!cfg)
7741 return -ENOMEM;
7742 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7743 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7744 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7745 ETH_ALEN);
7746 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7747 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7748 else
7749 port = 0;
7750 #ifdef CONFIG_INET
7751 /* allocate a socket and port for it and use it */
7752 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7753 IPPROTO_TCP, &cfg->sock, 1);
7754 if (err) {
7755 kfree(cfg);
7756 return err;
7758 if (inet_csk_get_port(cfg->sock->sk, port)) {
7759 sock_release(cfg->sock);
7760 kfree(cfg);
7761 return -EADDRINUSE;
7763 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7764 #else
7765 if (!port) {
7766 kfree(cfg);
7767 return -EINVAL;
7769 cfg->src_port = port;
7770 #endif
7772 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7773 cfg->payload_len = data_size;
7774 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7775 memcpy((void *)cfg->payload,
7776 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7777 data_size);
7778 if (seq)
7779 cfg->payload_seq = *seq;
7780 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7781 cfg->wake_len = wake_size;
7782 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7783 memcpy((void *)cfg->wake_data,
7784 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7785 wake_size);
7786 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7787 data_size + wake_size;
7788 memcpy((void *)cfg->wake_mask,
7789 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7790 wake_mask_size);
7791 if (tok) {
7792 cfg->tokens_size = tokens_size;
7793 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7796 trig->tcp = cfg;
7798 return 0;
7801 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7804 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
7805 struct cfg80211_wowlan new_triggers = {};
7806 struct cfg80211_wowlan *ntrig;
7807 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7808 int err, i;
7809 bool prev_enabled = rdev->wiphy.wowlan_config;
7811 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7812 !rdev->wiphy.wowlan.tcp)
7813 return -EOPNOTSUPP;
7815 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7816 cfg80211_rdev_free_wowlan(rdev);
7817 rdev->wiphy.wowlan_config = NULL;
7818 goto set_wakeup;
7821 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7822 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7823 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7824 nl80211_wowlan_policy);
7825 if (err)
7826 return err;
7828 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7829 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7830 return -EINVAL;
7831 new_triggers.any = true;
7834 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7835 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7836 return -EINVAL;
7837 new_triggers.disconnect = true;
7840 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7841 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7842 return -EINVAL;
7843 new_triggers.magic_pkt = true;
7846 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7847 return -EINVAL;
7849 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7850 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7851 return -EINVAL;
7852 new_triggers.gtk_rekey_failure = true;
7855 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7856 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7857 return -EINVAL;
7858 new_triggers.eap_identity_req = true;
7861 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7862 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7863 return -EINVAL;
7864 new_triggers.four_way_handshake = true;
7867 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7868 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7869 return -EINVAL;
7870 new_triggers.rfkill_release = true;
7873 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7874 struct nlattr *pat;
7875 int n_patterns = 0;
7876 int rem, pat_len, mask_len, pkt_offset;
7877 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7879 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7880 rem)
7881 n_patterns++;
7882 if (n_patterns > wowlan->n_patterns)
7883 return -EINVAL;
7885 new_triggers.patterns = kcalloc(n_patterns,
7886 sizeof(new_triggers.patterns[0]),
7887 GFP_KERNEL);
7888 if (!new_triggers.patterns)
7889 return -ENOMEM;
7891 new_triggers.n_patterns = n_patterns;
7892 i = 0;
7894 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7895 rem) {
7896 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7897 nla_data(pat), nla_len(pat), NULL);
7898 err = -EINVAL;
7899 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7900 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7901 goto error;
7902 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7903 mask_len = DIV_ROUND_UP(pat_len, 8);
7904 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7905 mask_len)
7906 goto error;
7907 if (pat_len > wowlan->pattern_max_len ||
7908 pat_len < wowlan->pattern_min_len)
7909 goto error;
7911 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7912 pkt_offset = 0;
7913 else
7914 pkt_offset = nla_get_u32(
7915 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7916 if (pkt_offset > wowlan->max_pkt_offset)
7917 goto error;
7918 new_triggers.patterns[i].pkt_offset = pkt_offset;
7920 new_triggers.patterns[i].mask =
7921 kmalloc(mask_len + pat_len, GFP_KERNEL);
7922 if (!new_triggers.patterns[i].mask) {
7923 err = -ENOMEM;
7924 goto error;
7926 new_triggers.patterns[i].pattern =
7927 new_triggers.patterns[i].mask + mask_len;
7928 memcpy(new_triggers.patterns[i].mask,
7929 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7930 mask_len);
7931 new_triggers.patterns[i].pattern_len = pat_len;
7932 memcpy(new_triggers.patterns[i].pattern,
7933 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7934 pat_len);
7935 i++;
7939 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7940 err = nl80211_parse_wowlan_tcp(
7941 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7942 &new_triggers);
7943 if (err)
7944 goto error;
7947 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7948 if (!ntrig) {
7949 err = -ENOMEM;
7950 goto error;
7952 cfg80211_rdev_free_wowlan(rdev);
7953 rdev->wiphy.wowlan_config = ntrig;
7955 set_wakeup:
7956 if (rdev->ops->set_wakeup &&
7957 prev_enabled != !!rdev->wiphy.wowlan_config)
7958 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
7960 return 0;
7961 error:
7962 for (i = 0; i < new_triggers.n_patterns; i++)
7963 kfree(new_triggers.patterns[i].mask);
7964 kfree(new_triggers.patterns);
7965 if (new_triggers.tcp && new_triggers.tcp->sock)
7966 sock_release(new_triggers.tcp->sock);
7967 kfree(new_triggers.tcp);
7968 return err;
7970 #endif
7972 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7974 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7975 struct net_device *dev = info->user_ptr[1];
7976 struct wireless_dev *wdev = dev->ieee80211_ptr;
7977 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7978 struct cfg80211_gtk_rekey_data rekey_data;
7979 int err;
7981 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7982 return -EINVAL;
7984 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7985 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7986 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7987 nl80211_rekey_policy);
7988 if (err)
7989 return err;
7991 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7992 return -ERANGE;
7993 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7994 return -ERANGE;
7995 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7996 return -ERANGE;
7998 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7999 NL80211_KEK_LEN);
8000 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8001 NL80211_KCK_LEN);
8002 memcpy(rekey_data.replay_ctr,
8003 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8004 NL80211_REPLAY_CTR_LEN);
8006 wdev_lock(wdev);
8007 if (!wdev->current_bss) {
8008 err = -ENOTCONN;
8009 goto out;
8012 if (!rdev->ops->set_rekey_data) {
8013 err = -EOPNOTSUPP;
8014 goto out;
8017 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
8018 out:
8019 wdev_unlock(wdev);
8020 return err;
8023 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8024 struct genl_info *info)
8026 struct net_device *dev = info->user_ptr[1];
8027 struct wireless_dev *wdev = dev->ieee80211_ptr;
8029 if (wdev->iftype != NL80211_IFTYPE_AP &&
8030 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8031 return -EINVAL;
8033 if (wdev->ap_unexpected_nlportid)
8034 return -EBUSY;
8036 wdev->ap_unexpected_nlportid = info->snd_portid;
8037 return 0;
8040 static int nl80211_probe_client(struct sk_buff *skb,
8041 struct genl_info *info)
8043 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8044 struct net_device *dev = info->user_ptr[1];
8045 struct wireless_dev *wdev = dev->ieee80211_ptr;
8046 struct sk_buff *msg;
8047 void *hdr;
8048 const u8 *addr;
8049 u64 cookie;
8050 int err;
8052 if (wdev->iftype != NL80211_IFTYPE_AP &&
8053 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8054 return -EOPNOTSUPP;
8056 if (!info->attrs[NL80211_ATTR_MAC])
8057 return -EINVAL;
8059 if (!rdev->ops->probe_client)
8060 return -EOPNOTSUPP;
8062 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8063 if (!msg)
8064 return -ENOMEM;
8066 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8067 NL80211_CMD_PROBE_CLIENT);
8069 if (IS_ERR(hdr)) {
8070 err = PTR_ERR(hdr);
8071 goto free_msg;
8074 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8076 err = rdev_probe_client(rdev, dev, addr, &cookie);
8077 if (err)
8078 goto free_msg;
8080 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8081 goto nla_put_failure;
8083 genlmsg_end(msg, hdr);
8085 return genlmsg_reply(msg, info);
8087 nla_put_failure:
8088 err = -ENOBUFS;
8089 free_msg:
8090 nlmsg_free(msg);
8091 return err;
8094 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8096 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8097 struct cfg80211_beacon_registration *reg, *nreg;
8098 int rv;
8100 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8101 return -EOPNOTSUPP;
8103 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8104 if (!nreg)
8105 return -ENOMEM;
8107 /* First, check if already registered. */
8108 spin_lock_bh(&rdev->beacon_registrations_lock);
8109 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8110 if (reg->nlportid == info->snd_portid) {
8111 rv = -EALREADY;
8112 goto out_err;
8115 /* Add it to the list */
8116 nreg->nlportid = info->snd_portid;
8117 list_add(&nreg->list, &rdev->beacon_registrations);
8119 spin_unlock_bh(&rdev->beacon_registrations_lock);
8121 return 0;
8122 out_err:
8123 spin_unlock_bh(&rdev->beacon_registrations_lock);
8124 kfree(nreg);
8125 return rv;
8128 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8130 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8131 struct wireless_dev *wdev = info->user_ptr[1];
8132 int err;
8134 if (!rdev->ops->start_p2p_device)
8135 return -EOPNOTSUPP;
8137 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8138 return -EOPNOTSUPP;
8140 if (wdev->p2p_started)
8141 return 0;
8143 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8144 if (err)
8145 return err;
8147 err = rdev_start_p2p_device(rdev, wdev);
8148 if (err)
8149 return err;
8151 wdev->p2p_started = true;
8152 rdev->opencount++;
8154 return 0;
8157 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8160 struct wireless_dev *wdev = info->user_ptr[1];
8162 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8163 return -EOPNOTSUPP;
8165 if (!rdev->ops->stop_p2p_device)
8166 return -EOPNOTSUPP;
8168 cfg80211_stop_p2p_device(rdev, wdev);
8170 return 0;
8173 static int nl80211_get_protocol_features(struct sk_buff *skb,
8174 struct genl_info *info)
8176 void *hdr;
8177 struct sk_buff *msg;
8179 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8180 if (!msg)
8181 return -ENOMEM;
8183 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8184 NL80211_CMD_GET_PROTOCOL_FEATURES);
8185 if (!hdr)
8186 goto nla_put_failure;
8188 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8189 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8190 goto nla_put_failure;
8192 genlmsg_end(msg, hdr);
8193 return genlmsg_reply(msg, info);
8195 nla_put_failure:
8196 kfree_skb(msg);
8197 return -ENOBUFS;
8200 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8202 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8203 struct cfg80211_update_ft_ies_params ft_params;
8204 struct net_device *dev = info->user_ptr[1];
8206 if (!rdev->ops->update_ft_ies)
8207 return -EOPNOTSUPP;
8209 if (!info->attrs[NL80211_ATTR_MDID] ||
8210 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8211 return -EINVAL;
8213 memset(&ft_params, 0, sizeof(ft_params));
8214 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8215 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8216 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8218 return rdev_update_ft_ies(rdev, dev, &ft_params);
8221 static int nl80211_crit_protocol_start(struct sk_buff *skb,
8222 struct genl_info *info)
8224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8225 struct wireless_dev *wdev = info->user_ptr[1];
8226 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8227 u16 duration;
8228 int ret;
8230 if (!rdev->ops->crit_proto_start)
8231 return -EOPNOTSUPP;
8233 if (WARN_ON(!rdev->ops->crit_proto_stop))
8234 return -EINVAL;
8236 if (rdev->crit_proto_nlportid)
8237 return -EBUSY;
8239 /* determine protocol if provided */
8240 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8241 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8243 if (proto >= NUM_NL80211_CRIT_PROTO)
8244 return -EINVAL;
8246 /* timeout must be provided */
8247 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8248 return -EINVAL;
8250 duration =
8251 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8253 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8254 return -ERANGE;
8256 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8257 if (!ret)
8258 rdev->crit_proto_nlportid = info->snd_portid;
8260 return ret;
8263 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8264 struct genl_info *info)
8266 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8267 struct wireless_dev *wdev = info->user_ptr[1];
8269 if (!rdev->ops->crit_proto_stop)
8270 return -EOPNOTSUPP;
8272 if (rdev->crit_proto_nlportid) {
8273 rdev->crit_proto_nlportid = 0;
8274 rdev_crit_proto_stop(rdev, wdev);
8276 return 0;
8279 #define NL80211_FLAG_NEED_WIPHY 0x01
8280 #define NL80211_FLAG_NEED_NETDEV 0x02
8281 #define NL80211_FLAG_NEED_RTNL 0x04
8282 #define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8283 #define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8284 NL80211_FLAG_CHECK_NETDEV_UP)
8285 #define NL80211_FLAG_NEED_WDEV 0x10
8286 /* If a netdev is associated, it must be UP, P2P must be started */
8287 #define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8288 NL80211_FLAG_CHECK_NETDEV_UP)
8290 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8291 struct genl_info *info)
8293 struct cfg80211_registered_device *rdev;
8294 struct wireless_dev *wdev;
8295 struct net_device *dev;
8296 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8298 if (rtnl)
8299 rtnl_lock();
8301 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
8302 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8303 if (IS_ERR(rdev)) {
8304 if (rtnl)
8305 rtnl_unlock();
8306 return PTR_ERR(rdev);
8308 info->user_ptr[0] = rdev;
8309 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8310 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8311 ASSERT_RTNL();
8313 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8314 info->attrs);
8315 if (IS_ERR(wdev)) {
8316 if (rtnl)
8317 rtnl_unlock();
8318 return PTR_ERR(wdev);
8321 dev = wdev->netdev;
8322 rdev = wiphy_to_dev(wdev->wiphy);
8324 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8325 if (!dev) {
8326 if (rtnl)
8327 rtnl_unlock();
8328 return -EINVAL;
8331 info->user_ptr[1] = dev;
8332 } else {
8333 info->user_ptr[1] = wdev;
8336 if (dev) {
8337 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8338 !netif_running(dev)) {
8339 if (rtnl)
8340 rtnl_unlock();
8341 return -ENETDOWN;
8344 dev_hold(dev);
8345 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8346 if (!wdev->p2p_started) {
8347 if (rtnl)
8348 rtnl_unlock();
8349 return -ENETDOWN;
8353 info->user_ptr[0] = rdev;
8356 return 0;
8359 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8360 struct genl_info *info)
8362 if (info->user_ptr[1]) {
8363 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8364 struct wireless_dev *wdev = info->user_ptr[1];
8366 if (wdev->netdev)
8367 dev_put(wdev->netdev);
8368 } else {
8369 dev_put(info->user_ptr[1]);
8372 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8373 rtnl_unlock();
8376 static struct genl_ops nl80211_ops[] = {
8378 .cmd = NL80211_CMD_GET_WIPHY,
8379 .doit = nl80211_get_wiphy,
8380 .dumpit = nl80211_dump_wiphy,
8381 .policy = nl80211_policy,
8382 /* can be retrieved by unprivileged users */
8383 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8384 NL80211_FLAG_NEED_RTNL,
8387 .cmd = NL80211_CMD_SET_WIPHY,
8388 .doit = nl80211_set_wiphy,
8389 .policy = nl80211_policy,
8390 .flags = GENL_ADMIN_PERM,
8391 .internal_flags = NL80211_FLAG_NEED_RTNL,
8394 .cmd = NL80211_CMD_GET_INTERFACE,
8395 .doit = nl80211_get_interface,
8396 .dumpit = nl80211_dump_interface,
8397 .policy = nl80211_policy,
8398 /* can be retrieved by unprivileged users */
8399 .internal_flags = NL80211_FLAG_NEED_WDEV |
8400 NL80211_FLAG_NEED_RTNL,
8403 .cmd = NL80211_CMD_SET_INTERFACE,
8404 .doit = nl80211_set_interface,
8405 .policy = nl80211_policy,
8406 .flags = GENL_ADMIN_PERM,
8407 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8408 NL80211_FLAG_NEED_RTNL,
8411 .cmd = NL80211_CMD_NEW_INTERFACE,
8412 .doit = nl80211_new_interface,
8413 .policy = nl80211_policy,
8414 .flags = GENL_ADMIN_PERM,
8415 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8416 NL80211_FLAG_NEED_RTNL,
8419 .cmd = NL80211_CMD_DEL_INTERFACE,
8420 .doit = nl80211_del_interface,
8421 .policy = nl80211_policy,
8422 .flags = GENL_ADMIN_PERM,
8423 .internal_flags = NL80211_FLAG_NEED_WDEV |
8424 NL80211_FLAG_NEED_RTNL,
8427 .cmd = NL80211_CMD_GET_KEY,
8428 .doit = nl80211_get_key,
8429 .policy = nl80211_policy,
8430 .flags = GENL_ADMIN_PERM,
8431 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8432 NL80211_FLAG_NEED_RTNL,
8435 .cmd = NL80211_CMD_SET_KEY,
8436 .doit = nl80211_set_key,
8437 .policy = nl80211_policy,
8438 .flags = GENL_ADMIN_PERM,
8439 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8440 NL80211_FLAG_NEED_RTNL,
8443 .cmd = NL80211_CMD_NEW_KEY,
8444 .doit = nl80211_new_key,
8445 .policy = nl80211_policy,
8446 .flags = GENL_ADMIN_PERM,
8447 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8448 NL80211_FLAG_NEED_RTNL,
8451 .cmd = NL80211_CMD_DEL_KEY,
8452 .doit = nl80211_del_key,
8453 .policy = nl80211_policy,
8454 .flags = GENL_ADMIN_PERM,
8455 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8456 NL80211_FLAG_NEED_RTNL,
8459 .cmd = NL80211_CMD_SET_BEACON,
8460 .policy = nl80211_policy,
8461 .flags = GENL_ADMIN_PERM,
8462 .doit = nl80211_set_beacon,
8463 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8464 NL80211_FLAG_NEED_RTNL,
8467 .cmd = NL80211_CMD_START_AP,
8468 .policy = nl80211_policy,
8469 .flags = GENL_ADMIN_PERM,
8470 .doit = nl80211_start_ap,
8471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8472 NL80211_FLAG_NEED_RTNL,
8475 .cmd = NL80211_CMD_STOP_AP,
8476 .policy = nl80211_policy,
8477 .flags = GENL_ADMIN_PERM,
8478 .doit = nl80211_stop_ap,
8479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8480 NL80211_FLAG_NEED_RTNL,
8483 .cmd = NL80211_CMD_GET_STATION,
8484 .doit = nl80211_get_station,
8485 .dumpit = nl80211_dump_station,
8486 .policy = nl80211_policy,
8487 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8488 NL80211_FLAG_NEED_RTNL,
8491 .cmd = NL80211_CMD_SET_STATION,
8492 .doit = nl80211_set_station,
8493 .policy = nl80211_policy,
8494 .flags = GENL_ADMIN_PERM,
8495 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8496 NL80211_FLAG_NEED_RTNL,
8499 .cmd = NL80211_CMD_NEW_STATION,
8500 .doit = nl80211_new_station,
8501 .policy = nl80211_policy,
8502 .flags = GENL_ADMIN_PERM,
8503 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8504 NL80211_FLAG_NEED_RTNL,
8507 .cmd = NL80211_CMD_DEL_STATION,
8508 .doit = nl80211_del_station,
8509 .policy = nl80211_policy,
8510 .flags = GENL_ADMIN_PERM,
8511 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8512 NL80211_FLAG_NEED_RTNL,
8515 .cmd = NL80211_CMD_GET_MPATH,
8516 .doit = nl80211_get_mpath,
8517 .dumpit = nl80211_dump_mpath,
8518 .policy = nl80211_policy,
8519 .flags = GENL_ADMIN_PERM,
8520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8521 NL80211_FLAG_NEED_RTNL,
8524 .cmd = NL80211_CMD_SET_MPATH,
8525 .doit = nl80211_set_mpath,
8526 .policy = nl80211_policy,
8527 .flags = GENL_ADMIN_PERM,
8528 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8529 NL80211_FLAG_NEED_RTNL,
8532 .cmd = NL80211_CMD_NEW_MPATH,
8533 .doit = nl80211_new_mpath,
8534 .policy = nl80211_policy,
8535 .flags = GENL_ADMIN_PERM,
8536 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8537 NL80211_FLAG_NEED_RTNL,
8540 .cmd = NL80211_CMD_DEL_MPATH,
8541 .doit = nl80211_del_mpath,
8542 .policy = nl80211_policy,
8543 .flags = GENL_ADMIN_PERM,
8544 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8545 NL80211_FLAG_NEED_RTNL,
8548 .cmd = NL80211_CMD_SET_BSS,
8549 .doit = nl80211_set_bss,
8550 .policy = nl80211_policy,
8551 .flags = GENL_ADMIN_PERM,
8552 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8553 NL80211_FLAG_NEED_RTNL,
8556 .cmd = NL80211_CMD_GET_REG,
8557 .doit = nl80211_get_reg,
8558 .policy = nl80211_policy,
8559 .internal_flags = NL80211_FLAG_NEED_RTNL,
8560 /* can be retrieved by unprivileged users */
8563 .cmd = NL80211_CMD_SET_REG,
8564 .doit = nl80211_set_reg,
8565 .policy = nl80211_policy,
8566 .flags = GENL_ADMIN_PERM,
8567 .internal_flags = NL80211_FLAG_NEED_RTNL,
8570 .cmd = NL80211_CMD_REQ_SET_REG,
8571 .doit = nl80211_req_set_reg,
8572 .policy = nl80211_policy,
8573 .flags = GENL_ADMIN_PERM,
8576 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8577 .doit = nl80211_get_mesh_config,
8578 .policy = nl80211_policy,
8579 /* can be retrieved by unprivileged users */
8580 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8581 NL80211_FLAG_NEED_RTNL,
8584 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8585 .doit = nl80211_update_mesh_config,
8586 .policy = nl80211_policy,
8587 .flags = GENL_ADMIN_PERM,
8588 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8589 NL80211_FLAG_NEED_RTNL,
8592 .cmd = NL80211_CMD_TRIGGER_SCAN,
8593 .doit = nl80211_trigger_scan,
8594 .policy = nl80211_policy,
8595 .flags = GENL_ADMIN_PERM,
8596 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8597 NL80211_FLAG_NEED_RTNL,
8600 .cmd = NL80211_CMD_GET_SCAN,
8601 .policy = nl80211_policy,
8602 .dumpit = nl80211_dump_scan,
8605 .cmd = NL80211_CMD_START_SCHED_SCAN,
8606 .doit = nl80211_start_sched_scan,
8607 .policy = nl80211_policy,
8608 .flags = GENL_ADMIN_PERM,
8609 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8610 NL80211_FLAG_NEED_RTNL,
8613 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8614 .doit = nl80211_stop_sched_scan,
8615 .policy = nl80211_policy,
8616 .flags = GENL_ADMIN_PERM,
8617 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8618 NL80211_FLAG_NEED_RTNL,
8621 .cmd = NL80211_CMD_AUTHENTICATE,
8622 .doit = nl80211_authenticate,
8623 .policy = nl80211_policy,
8624 .flags = GENL_ADMIN_PERM,
8625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8626 NL80211_FLAG_NEED_RTNL,
8629 .cmd = NL80211_CMD_ASSOCIATE,
8630 .doit = nl80211_associate,
8631 .policy = nl80211_policy,
8632 .flags = GENL_ADMIN_PERM,
8633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8634 NL80211_FLAG_NEED_RTNL,
8637 .cmd = NL80211_CMD_DEAUTHENTICATE,
8638 .doit = nl80211_deauthenticate,
8639 .policy = nl80211_policy,
8640 .flags = GENL_ADMIN_PERM,
8641 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8642 NL80211_FLAG_NEED_RTNL,
8645 .cmd = NL80211_CMD_DISASSOCIATE,
8646 .doit = nl80211_disassociate,
8647 .policy = nl80211_policy,
8648 .flags = GENL_ADMIN_PERM,
8649 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8650 NL80211_FLAG_NEED_RTNL,
8653 .cmd = NL80211_CMD_JOIN_IBSS,
8654 .doit = nl80211_join_ibss,
8655 .policy = nl80211_policy,
8656 .flags = GENL_ADMIN_PERM,
8657 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8658 NL80211_FLAG_NEED_RTNL,
8661 .cmd = NL80211_CMD_LEAVE_IBSS,
8662 .doit = nl80211_leave_ibss,
8663 .policy = nl80211_policy,
8664 .flags = GENL_ADMIN_PERM,
8665 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8666 NL80211_FLAG_NEED_RTNL,
8668 #ifdef CONFIG_NL80211_TESTMODE
8670 .cmd = NL80211_CMD_TESTMODE,
8671 .doit = nl80211_testmode_do,
8672 .dumpit = nl80211_testmode_dump,
8673 .policy = nl80211_policy,
8674 .flags = GENL_ADMIN_PERM,
8675 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8676 NL80211_FLAG_NEED_RTNL,
8678 #endif
8680 .cmd = NL80211_CMD_CONNECT,
8681 .doit = nl80211_connect,
8682 .policy = nl80211_policy,
8683 .flags = GENL_ADMIN_PERM,
8684 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8685 NL80211_FLAG_NEED_RTNL,
8688 .cmd = NL80211_CMD_DISCONNECT,
8689 .doit = nl80211_disconnect,
8690 .policy = nl80211_policy,
8691 .flags = GENL_ADMIN_PERM,
8692 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8693 NL80211_FLAG_NEED_RTNL,
8696 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8697 .doit = nl80211_wiphy_netns,
8698 .policy = nl80211_policy,
8699 .flags = GENL_ADMIN_PERM,
8700 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8701 NL80211_FLAG_NEED_RTNL,
8704 .cmd = NL80211_CMD_GET_SURVEY,
8705 .policy = nl80211_policy,
8706 .dumpit = nl80211_dump_survey,
8709 .cmd = NL80211_CMD_SET_PMKSA,
8710 .doit = nl80211_setdel_pmksa,
8711 .policy = nl80211_policy,
8712 .flags = GENL_ADMIN_PERM,
8713 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8714 NL80211_FLAG_NEED_RTNL,
8717 .cmd = NL80211_CMD_DEL_PMKSA,
8718 .doit = nl80211_setdel_pmksa,
8719 .policy = nl80211_policy,
8720 .flags = GENL_ADMIN_PERM,
8721 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8722 NL80211_FLAG_NEED_RTNL,
8725 .cmd = NL80211_CMD_FLUSH_PMKSA,
8726 .doit = nl80211_flush_pmksa,
8727 .policy = nl80211_policy,
8728 .flags = GENL_ADMIN_PERM,
8729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8730 NL80211_FLAG_NEED_RTNL,
8733 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8734 .doit = nl80211_remain_on_channel,
8735 .policy = nl80211_policy,
8736 .flags = GENL_ADMIN_PERM,
8737 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8738 NL80211_FLAG_NEED_RTNL,
8741 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8742 .doit = nl80211_cancel_remain_on_channel,
8743 .policy = nl80211_policy,
8744 .flags = GENL_ADMIN_PERM,
8745 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8746 NL80211_FLAG_NEED_RTNL,
8749 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8750 .doit = nl80211_set_tx_bitrate_mask,
8751 .policy = nl80211_policy,
8752 .flags = GENL_ADMIN_PERM,
8753 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8754 NL80211_FLAG_NEED_RTNL,
8757 .cmd = NL80211_CMD_REGISTER_FRAME,
8758 .doit = nl80211_register_mgmt,
8759 .policy = nl80211_policy,
8760 .flags = GENL_ADMIN_PERM,
8761 .internal_flags = NL80211_FLAG_NEED_WDEV |
8762 NL80211_FLAG_NEED_RTNL,
8765 .cmd = NL80211_CMD_FRAME,
8766 .doit = nl80211_tx_mgmt,
8767 .policy = nl80211_policy,
8768 .flags = GENL_ADMIN_PERM,
8769 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8770 NL80211_FLAG_NEED_RTNL,
8773 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8774 .doit = nl80211_tx_mgmt_cancel_wait,
8775 .policy = nl80211_policy,
8776 .flags = GENL_ADMIN_PERM,
8777 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8778 NL80211_FLAG_NEED_RTNL,
8781 .cmd = NL80211_CMD_SET_POWER_SAVE,
8782 .doit = nl80211_set_power_save,
8783 .policy = nl80211_policy,
8784 .flags = GENL_ADMIN_PERM,
8785 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8786 NL80211_FLAG_NEED_RTNL,
8789 .cmd = NL80211_CMD_GET_POWER_SAVE,
8790 .doit = nl80211_get_power_save,
8791 .policy = nl80211_policy,
8792 /* can be retrieved by unprivileged users */
8793 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8794 NL80211_FLAG_NEED_RTNL,
8797 .cmd = NL80211_CMD_SET_CQM,
8798 .doit = nl80211_set_cqm,
8799 .policy = nl80211_policy,
8800 .flags = GENL_ADMIN_PERM,
8801 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8802 NL80211_FLAG_NEED_RTNL,
8805 .cmd = NL80211_CMD_SET_CHANNEL,
8806 .doit = nl80211_set_channel,
8807 .policy = nl80211_policy,
8808 .flags = GENL_ADMIN_PERM,
8809 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8810 NL80211_FLAG_NEED_RTNL,
8813 .cmd = NL80211_CMD_SET_WDS_PEER,
8814 .doit = nl80211_set_wds_peer,
8815 .policy = nl80211_policy,
8816 .flags = GENL_ADMIN_PERM,
8817 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8818 NL80211_FLAG_NEED_RTNL,
8821 .cmd = NL80211_CMD_JOIN_MESH,
8822 .doit = nl80211_join_mesh,
8823 .policy = nl80211_policy,
8824 .flags = GENL_ADMIN_PERM,
8825 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8826 NL80211_FLAG_NEED_RTNL,
8829 .cmd = NL80211_CMD_LEAVE_MESH,
8830 .doit = nl80211_leave_mesh,
8831 .policy = nl80211_policy,
8832 .flags = GENL_ADMIN_PERM,
8833 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8834 NL80211_FLAG_NEED_RTNL,
8836 #ifdef CONFIG_PM
8838 .cmd = NL80211_CMD_GET_WOWLAN,
8839 .doit = nl80211_get_wowlan,
8840 .policy = nl80211_policy,
8841 /* can be retrieved by unprivileged users */
8842 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8843 NL80211_FLAG_NEED_RTNL,
8846 .cmd = NL80211_CMD_SET_WOWLAN,
8847 .doit = nl80211_set_wowlan,
8848 .policy = nl80211_policy,
8849 .flags = GENL_ADMIN_PERM,
8850 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8851 NL80211_FLAG_NEED_RTNL,
8853 #endif
8855 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8856 .doit = nl80211_set_rekey_data,
8857 .policy = nl80211_policy,
8858 .flags = GENL_ADMIN_PERM,
8859 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8860 NL80211_FLAG_NEED_RTNL,
8863 .cmd = NL80211_CMD_TDLS_MGMT,
8864 .doit = nl80211_tdls_mgmt,
8865 .policy = nl80211_policy,
8866 .flags = GENL_ADMIN_PERM,
8867 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8868 NL80211_FLAG_NEED_RTNL,
8871 .cmd = NL80211_CMD_TDLS_OPER,
8872 .doit = nl80211_tdls_oper,
8873 .policy = nl80211_policy,
8874 .flags = GENL_ADMIN_PERM,
8875 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8876 NL80211_FLAG_NEED_RTNL,
8879 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8880 .doit = nl80211_register_unexpected_frame,
8881 .policy = nl80211_policy,
8882 .flags = GENL_ADMIN_PERM,
8883 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8884 NL80211_FLAG_NEED_RTNL,
8887 .cmd = NL80211_CMD_PROBE_CLIENT,
8888 .doit = nl80211_probe_client,
8889 .policy = nl80211_policy,
8890 .flags = GENL_ADMIN_PERM,
8891 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8892 NL80211_FLAG_NEED_RTNL,
8895 .cmd = NL80211_CMD_REGISTER_BEACONS,
8896 .doit = nl80211_register_beacons,
8897 .policy = nl80211_policy,
8898 .flags = GENL_ADMIN_PERM,
8899 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8900 NL80211_FLAG_NEED_RTNL,
8903 .cmd = NL80211_CMD_SET_NOACK_MAP,
8904 .doit = nl80211_set_noack_map,
8905 .policy = nl80211_policy,
8906 .flags = GENL_ADMIN_PERM,
8907 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8908 NL80211_FLAG_NEED_RTNL,
8911 .cmd = NL80211_CMD_START_P2P_DEVICE,
8912 .doit = nl80211_start_p2p_device,
8913 .policy = nl80211_policy,
8914 .flags = GENL_ADMIN_PERM,
8915 .internal_flags = NL80211_FLAG_NEED_WDEV |
8916 NL80211_FLAG_NEED_RTNL,
8919 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8920 .doit = nl80211_stop_p2p_device,
8921 .policy = nl80211_policy,
8922 .flags = GENL_ADMIN_PERM,
8923 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8924 NL80211_FLAG_NEED_RTNL,
8927 .cmd = NL80211_CMD_SET_MCAST_RATE,
8928 .doit = nl80211_set_mcast_rate,
8929 .policy = nl80211_policy,
8930 .flags = GENL_ADMIN_PERM,
8931 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8932 NL80211_FLAG_NEED_RTNL,
8935 .cmd = NL80211_CMD_SET_MAC_ACL,
8936 .doit = nl80211_set_mac_acl,
8937 .policy = nl80211_policy,
8938 .flags = GENL_ADMIN_PERM,
8939 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8940 NL80211_FLAG_NEED_RTNL,
8943 .cmd = NL80211_CMD_RADAR_DETECT,
8944 .doit = nl80211_start_radar_detection,
8945 .policy = nl80211_policy,
8946 .flags = GENL_ADMIN_PERM,
8947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8948 NL80211_FLAG_NEED_RTNL,
8951 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8952 .doit = nl80211_get_protocol_features,
8953 .policy = nl80211_policy,
8956 .cmd = NL80211_CMD_UPDATE_FT_IES,
8957 .doit = nl80211_update_ft_ies,
8958 .policy = nl80211_policy,
8959 .flags = GENL_ADMIN_PERM,
8960 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8961 NL80211_FLAG_NEED_RTNL,
8964 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
8965 .doit = nl80211_crit_protocol_start,
8966 .policy = nl80211_policy,
8967 .flags = GENL_ADMIN_PERM,
8968 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8969 NL80211_FLAG_NEED_RTNL,
8972 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
8973 .doit = nl80211_crit_protocol_stop,
8974 .policy = nl80211_policy,
8975 .flags = GENL_ADMIN_PERM,
8976 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8977 NL80211_FLAG_NEED_RTNL,
8981 static struct genl_multicast_group nl80211_mlme_mcgrp = {
8982 .name = "mlme",
8985 /* multicast groups */
8986 static struct genl_multicast_group nl80211_config_mcgrp = {
8987 .name = "config",
8989 static struct genl_multicast_group nl80211_scan_mcgrp = {
8990 .name = "scan",
8992 static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8993 .name = "regulatory",
8996 /* notification functions */
8998 void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9000 struct sk_buff *msg;
9002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9003 if (!msg)
9004 return;
9006 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9007 false, NULL, NULL, NULL) < 0) {
9008 nlmsg_free(msg);
9009 return;
9012 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9013 nl80211_config_mcgrp.id, GFP_KERNEL);
9016 static int nl80211_add_scan_req(struct sk_buff *msg,
9017 struct cfg80211_registered_device *rdev)
9019 struct cfg80211_scan_request *req = rdev->scan_req;
9020 struct nlattr *nest;
9021 int i;
9023 if (WARN_ON(!req))
9024 return 0;
9026 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9027 if (!nest)
9028 goto nla_put_failure;
9029 for (i = 0; i < req->n_ssids; i++) {
9030 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9031 goto nla_put_failure;
9033 nla_nest_end(msg, nest);
9035 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9036 if (!nest)
9037 goto nla_put_failure;
9038 for (i = 0; i < req->n_channels; i++) {
9039 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9040 goto nla_put_failure;
9042 nla_nest_end(msg, nest);
9044 if (req->ie &&
9045 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9046 goto nla_put_failure;
9048 if (req->flags)
9049 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9051 return 0;
9052 nla_put_failure:
9053 return -ENOBUFS;
9056 static int nl80211_send_scan_msg(struct sk_buff *msg,
9057 struct cfg80211_registered_device *rdev,
9058 struct wireless_dev *wdev,
9059 u32 portid, u32 seq, int flags,
9060 u32 cmd)
9062 void *hdr;
9064 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
9065 if (!hdr)
9066 return -1;
9068 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9069 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9070 wdev->netdev->ifindex)) ||
9071 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9072 goto nla_put_failure;
9074 /* ignore errors and send incomplete event anyway */
9075 nl80211_add_scan_req(msg, rdev);
9077 return genlmsg_end(msg, hdr);
9079 nla_put_failure:
9080 genlmsg_cancel(msg, hdr);
9081 return -EMSGSIZE;
9084 static int
9085 nl80211_send_sched_scan_msg(struct sk_buff *msg,
9086 struct cfg80211_registered_device *rdev,
9087 struct net_device *netdev,
9088 u32 portid, u32 seq, int flags, u32 cmd)
9090 void *hdr;
9092 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
9093 if (!hdr)
9094 return -1;
9096 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9097 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9098 goto nla_put_failure;
9100 return genlmsg_end(msg, hdr);
9102 nla_put_failure:
9103 genlmsg_cancel(msg, hdr);
9104 return -EMSGSIZE;
9107 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
9108 struct wireless_dev *wdev)
9110 struct sk_buff *msg;
9112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9113 if (!msg)
9114 return;
9116 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
9117 NL80211_CMD_TRIGGER_SCAN) < 0) {
9118 nlmsg_free(msg);
9119 return;
9122 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9123 nl80211_scan_mcgrp.id, GFP_KERNEL);
9126 void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
9127 struct wireless_dev *wdev)
9129 struct sk_buff *msg;
9131 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9132 if (!msg)
9133 return;
9135 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
9136 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
9137 nlmsg_free(msg);
9138 return;
9141 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9142 nl80211_scan_mcgrp.id, GFP_KERNEL);
9145 void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
9146 struct wireless_dev *wdev)
9148 struct sk_buff *msg;
9150 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9151 if (!msg)
9152 return;
9154 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
9155 NL80211_CMD_SCAN_ABORTED) < 0) {
9156 nlmsg_free(msg);
9157 return;
9160 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9161 nl80211_scan_mcgrp.id, GFP_KERNEL);
9164 void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9165 struct net_device *netdev)
9167 struct sk_buff *msg;
9169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9170 if (!msg)
9171 return;
9173 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9174 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9175 nlmsg_free(msg);
9176 return;
9179 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9180 nl80211_scan_mcgrp.id, GFP_KERNEL);
9183 void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9184 struct net_device *netdev, u32 cmd)
9186 struct sk_buff *msg;
9188 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9189 if (!msg)
9190 return;
9192 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9193 nlmsg_free(msg);
9194 return;
9197 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9198 nl80211_scan_mcgrp.id, GFP_KERNEL);
9202 * This can happen on global regulatory changes or device specific settings
9203 * based on custom world regulatory domains.
9205 void nl80211_send_reg_change_event(struct regulatory_request *request)
9207 struct sk_buff *msg;
9208 void *hdr;
9210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9211 if (!msg)
9212 return;
9214 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9215 if (!hdr) {
9216 nlmsg_free(msg);
9217 return;
9220 /* Userspace can always count this one always being set */
9221 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9222 goto nla_put_failure;
9224 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9225 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9226 NL80211_REGDOM_TYPE_WORLD))
9227 goto nla_put_failure;
9228 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9229 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9230 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9231 goto nla_put_failure;
9232 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9233 request->intersect) {
9234 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9235 NL80211_REGDOM_TYPE_INTERSECTION))
9236 goto nla_put_failure;
9237 } else {
9238 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9239 NL80211_REGDOM_TYPE_COUNTRY) ||
9240 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9241 request->alpha2))
9242 goto nla_put_failure;
9245 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
9246 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9247 goto nla_put_failure;
9249 genlmsg_end(msg, hdr);
9251 rcu_read_lock();
9252 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9253 GFP_ATOMIC);
9254 rcu_read_unlock();
9256 return;
9258 nla_put_failure:
9259 genlmsg_cancel(msg, hdr);
9260 nlmsg_free(msg);
9263 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9264 struct net_device *netdev,
9265 const u8 *buf, size_t len,
9266 enum nl80211_commands cmd, gfp_t gfp)
9268 struct sk_buff *msg;
9269 void *hdr;
9271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9272 if (!msg)
9273 return;
9275 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9276 if (!hdr) {
9277 nlmsg_free(msg);
9278 return;
9281 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9282 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9283 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9284 goto nla_put_failure;
9286 genlmsg_end(msg, hdr);
9288 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9289 nl80211_mlme_mcgrp.id, gfp);
9290 return;
9292 nla_put_failure:
9293 genlmsg_cancel(msg, hdr);
9294 nlmsg_free(msg);
9297 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
9298 struct net_device *netdev, const u8 *buf,
9299 size_t len, gfp_t gfp)
9301 nl80211_send_mlme_event(rdev, netdev, buf, len,
9302 NL80211_CMD_AUTHENTICATE, gfp);
9305 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9306 struct net_device *netdev, const u8 *buf,
9307 size_t len, gfp_t gfp)
9309 nl80211_send_mlme_event(rdev, netdev, buf, len,
9310 NL80211_CMD_ASSOCIATE, gfp);
9313 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
9314 struct net_device *netdev, const u8 *buf,
9315 size_t len, gfp_t gfp)
9317 nl80211_send_mlme_event(rdev, netdev, buf, len,
9318 NL80211_CMD_DEAUTHENTICATE, gfp);
9321 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9322 struct net_device *netdev, const u8 *buf,
9323 size_t len, gfp_t gfp)
9325 nl80211_send_mlme_event(rdev, netdev, buf, len,
9326 NL80211_CMD_DISASSOCIATE, gfp);
9329 void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9330 size_t len)
9332 struct wireless_dev *wdev = dev->ieee80211_ptr;
9333 struct wiphy *wiphy = wdev->wiphy;
9334 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9336 trace_cfg80211_send_unprot_deauth(dev);
9337 nl80211_send_mlme_event(rdev, dev, buf, len,
9338 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
9340 EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
9342 void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9343 size_t len)
9345 struct wireless_dev *wdev = dev->ieee80211_ptr;
9346 struct wiphy *wiphy = wdev->wiphy;
9347 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9349 trace_cfg80211_send_unprot_disassoc(dev);
9350 nl80211_send_mlme_event(rdev, dev, buf, len,
9351 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
9353 EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
9355 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9356 struct net_device *netdev, int cmd,
9357 const u8 *addr, gfp_t gfp)
9359 struct sk_buff *msg;
9360 void *hdr;
9362 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9363 if (!msg)
9364 return;
9366 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9367 if (!hdr) {
9368 nlmsg_free(msg);
9369 return;
9372 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9373 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9374 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9375 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9376 goto nla_put_failure;
9378 genlmsg_end(msg, hdr);
9380 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9381 nl80211_mlme_mcgrp.id, gfp);
9382 return;
9384 nla_put_failure:
9385 genlmsg_cancel(msg, hdr);
9386 nlmsg_free(msg);
9389 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
9390 struct net_device *netdev, const u8 *addr,
9391 gfp_t gfp)
9393 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
9394 addr, gfp);
9397 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
9398 struct net_device *netdev, const u8 *addr,
9399 gfp_t gfp)
9401 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9402 addr, gfp);
9405 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9406 struct net_device *netdev, const u8 *bssid,
9407 const u8 *req_ie, size_t req_ie_len,
9408 const u8 *resp_ie, size_t resp_ie_len,
9409 u16 status, gfp_t gfp)
9411 struct sk_buff *msg;
9412 void *hdr;
9414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9415 if (!msg)
9416 return;
9418 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9419 if (!hdr) {
9420 nlmsg_free(msg);
9421 return;
9424 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9425 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9426 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9427 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9428 (req_ie &&
9429 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9430 (resp_ie &&
9431 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9432 goto nla_put_failure;
9434 genlmsg_end(msg, hdr);
9436 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9437 nl80211_mlme_mcgrp.id, gfp);
9438 return;
9440 nla_put_failure:
9441 genlmsg_cancel(msg, hdr);
9442 nlmsg_free(msg);
9446 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9447 struct net_device *netdev, const u8 *bssid,
9448 const u8 *req_ie, size_t req_ie_len,
9449 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9451 struct sk_buff *msg;
9452 void *hdr;
9454 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9455 if (!msg)
9456 return;
9458 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9459 if (!hdr) {
9460 nlmsg_free(msg);
9461 return;
9464 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9465 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9466 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9467 (req_ie &&
9468 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9469 (resp_ie &&
9470 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9471 goto nla_put_failure;
9473 genlmsg_end(msg, hdr);
9475 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9476 nl80211_mlme_mcgrp.id, gfp);
9477 return;
9479 nla_put_failure:
9480 genlmsg_cancel(msg, hdr);
9481 nlmsg_free(msg);
9485 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9486 struct net_device *netdev, u16 reason,
9487 const u8 *ie, size_t ie_len, bool from_ap)
9489 struct sk_buff *msg;
9490 void *hdr;
9492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9493 if (!msg)
9494 return;
9496 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9497 if (!hdr) {
9498 nlmsg_free(msg);
9499 return;
9502 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9503 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9504 (from_ap && reason &&
9505 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9506 (from_ap &&
9507 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9508 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9509 goto nla_put_failure;
9511 genlmsg_end(msg, hdr);
9513 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9514 nl80211_mlme_mcgrp.id, GFP_KERNEL);
9515 return;
9517 nla_put_failure:
9518 genlmsg_cancel(msg, hdr);
9519 nlmsg_free(msg);
9523 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9524 struct net_device *netdev, const u8 *bssid,
9525 gfp_t gfp)
9527 struct sk_buff *msg;
9528 void *hdr;
9530 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9531 if (!msg)
9532 return;
9534 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9535 if (!hdr) {
9536 nlmsg_free(msg);
9537 return;
9540 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9541 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9542 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9543 goto nla_put_failure;
9545 genlmsg_end(msg, hdr);
9547 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9548 nl80211_mlme_mcgrp.id, gfp);
9549 return;
9551 nla_put_failure:
9552 genlmsg_cancel(msg, hdr);
9553 nlmsg_free(msg);
9556 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9557 const u8* ie, u8 ie_len, gfp_t gfp)
9559 struct wireless_dev *wdev = dev->ieee80211_ptr;
9560 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9561 struct sk_buff *msg;
9562 void *hdr;
9564 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9565 return;
9567 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9570 if (!msg)
9571 return;
9573 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9574 if (!hdr) {
9575 nlmsg_free(msg);
9576 return;
9579 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9580 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9581 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9582 (ie_len && ie &&
9583 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9584 goto nla_put_failure;
9586 genlmsg_end(msg, hdr);
9588 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9589 nl80211_mlme_mcgrp.id, gfp);
9590 return;
9592 nla_put_failure:
9593 genlmsg_cancel(msg, hdr);
9594 nlmsg_free(msg);
9596 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
9598 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9599 struct net_device *netdev, const u8 *addr,
9600 enum nl80211_key_type key_type, int key_id,
9601 const u8 *tsc, gfp_t gfp)
9603 struct sk_buff *msg;
9604 void *hdr;
9606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9607 if (!msg)
9608 return;
9610 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9611 if (!hdr) {
9612 nlmsg_free(msg);
9613 return;
9616 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9617 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9618 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9619 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9620 (key_id != -1 &&
9621 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9622 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9623 goto nla_put_failure;
9625 genlmsg_end(msg, hdr);
9627 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9628 nl80211_mlme_mcgrp.id, gfp);
9629 return;
9631 nla_put_failure:
9632 genlmsg_cancel(msg, hdr);
9633 nlmsg_free(msg);
9636 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9637 struct ieee80211_channel *channel_before,
9638 struct ieee80211_channel *channel_after)
9640 struct sk_buff *msg;
9641 void *hdr;
9642 struct nlattr *nl_freq;
9644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
9645 if (!msg)
9646 return;
9648 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9649 if (!hdr) {
9650 nlmsg_free(msg);
9651 return;
9655 * Since we are applying the beacon hint to a wiphy we know its
9656 * wiphy_idx is valid
9658 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9659 goto nla_put_failure;
9661 /* Before */
9662 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9663 if (!nl_freq)
9664 goto nla_put_failure;
9665 if (nl80211_msg_put_channel(msg, channel_before, false))
9666 goto nla_put_failure;
9667 nla_nest_end(msg, nl_freq);
9669 /* After */
9670 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9671 if (!nl_freq)
9672 goto nla_put_failure;
9673 if (nl80211_msg_put_channel(msg, channel_after, false))
9674 goto nla_put_failure;
9675 nla_nest_end(msg, nl_freq);
9677 genlmsg_end(msg, hdr);
9679 rcu_read_lock();
9680 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9681 GFP_ATOMIC);
9682 rcu_read_unlock();
9684 return;
9686 nla_put_failure:
9687 genlmsg_cancel(msg, hdr);
9688 nlmsg_free(msg);
9691 static void nl80211_send_remain_on_chan_event(
9692 int cmd, struct cfg80211_registered_device *rdev,
9693 struct wireless_dev *wdev, u64 cookie,
9694 struct ieee80211_channel *chan,
9695 unsigned int duration, gfp_t gfp)
9697 struct sk_buff *msg;
9698 void *hdr;
9700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9701 if (!msg)
9702 return;
9704 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9705 if (!hdr) {
9706 nlmsg_free(msg);
9707 return;
9710 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9711 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9712 wdev->netdev->ifindex)) ||
9713 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
9714 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
9715 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9716 NL80211_CHAN_NO_HT) ||
9717 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9718 goto nla_put_failure;
9720 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9721 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9722 goto nla_put_failure;
9724 genlmsg_end(msg, hdr);
9726 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9727 nl80211_mlme_mcgrp.id, gfp);
9728 return;
9730 nla_put_failure:
9731 genlmsg_cancel(msg, hdr);
9732 nlmsg_free(msg);
9735 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9736 struct ieee80211_channel *chan,
9737 unsigned int duration, gfp_t gfp)
9739 struct wiphy *wiphy = wdev->wiphy;
9740 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9742 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
9743 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
9744 rdev, wdev, cookie, chan,
9745 duration, gfp);
9747 EXPORT_SYMBOL(cfg80211_ready_on_channel);
9749 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9750 struct ieee80211_channel *chan,
9751 gfp_t gfp)
9753 struct wiphy *wiphy = wdev->wiphy;
9754 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9756 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
9757 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9758 rdev, wdev, cookie, chan, 0, gfp);
9760 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
9762 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9763 struct station_info *sinfo, gfp_t gfp)
9765 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9766 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9767 struct sk_buff *msg;
9769 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9771 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9772 if (!msg)
9773 return;
9775 if (nl80211_send_station(msg, 0, 0, 0,
9776 rdev, dev, mac_addr, sinfo) < 0) {
9777 nlmsg_free(msg);
9778 return;
9781 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9782 nl80211_mlme_mcgrp.id, gfp);
9784 EXPORT_SYMBOL(cfg80211_new_sta);
9786 void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
9788 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9789 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9790 struct sk_buff *msg;
9791 void *hdr;
9793 trace_cfg80211_del_sta(dev, mac_addr);
9795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9796 if (!msg)
9797 return;
9799 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9800 if (!hdr) {
9801 nlmsg_free(msg);
9802 return;
9805 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9806 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9807 goto nla_put_failure;
9809 genlmsg_end(msg, hdr);
9811 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9812 nl80211_mlme_mcgrp.id, gfp);
9813 return;
9815 nla_put_failure:
9816 genlmsg_cancel(msg, hdr);
9817 nlmsg_free(msg);
9819 EXPORT_SYMBOL(cfg80211_del_sta);
9821 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9822 enum nl80211_connect_failed_reason reason,
9823 gfp_t gfp)
9825 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9826 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9827 struct sk_buff *msg;
9828 void *hdr;
9830 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9831 if (!msg)
9832 return;
9834 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9835 if (!hdr) {
9836 nlmsg_free(msg);
9837 return;
9840 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9841 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9842 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9843 goto nla_put_failure;
9845 genlmsg_end(msg, hdr);
9847 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9848 nl80211_mlme_mcgrp.id, gfp);
9849 return;
9851 nla_put_failure:
9852 genlmsg_cancel(msg, hdr);
9853 nlmsg_free(msg);
9855 EXPORT_SYMBOL(cfg80211_conn_failed);
9857 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9858 const u8 *addr, gfp_t gfp)
9860 struct wireless_dev *wdev = dev->ieee80211_ptr;
9861 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9862 struct sk_buff *msg;
9863 void *hdr;
9864 int err;
9865 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
9867 if (!nlportid)
9868 return false;
9870 msg = nlmsg_new(100, gfp);
9871 if (!msg)
9872 return true;
9874 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9875 if (!hdr) {
9876 nlmsg_free(msg);
9877 return true;
9880 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9881 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9882 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9883 goto nla_put_failure;
9885 err = genlmsg_end(msg, hdr);
9886 if (err < 0) {
9887 nlmsg_free(msg);
9888 return true;
9891 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
9892 return true;
9894 nla_put_failure:
9895 genlmsg_cancel(msg, hdr);
9896 nlmsg_free(msg);
9897 return true;
9900 bool cfg80211_rx_spurious_frame(struct net_device *dev,
9901 const u8 *addr, gfp_t gfp)
9903 struct wireless_dev *wdev = dev->ieee80211_ptr;
9904 bool ret;
9906 trace_cfg80211_rx_spurious_frame(dev, addr);
9908 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9909 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9910 trace_cfg80211_return_bool(false);
9911 return false;
9913 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9914 addr, gfp);
9915 trace_cfg80211_return_bool(ret);
9916 return ret;
9918 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9920 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9921 const u8 *addr, gfp_t gfp)
9923 struct wireless_dev *wdev = dev->ieee80211_ptr;
9924 bool ret;
9926 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9928 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9929 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9930 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9931 trace_cfg80211_return_bool(false);
9932 return false;
9934 ret = __nl80211_unexpected_frame(dev,
9935 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9936 addr, gfp);
9937 trace_cfg80211_return_bool(ret);
9938 return ret;
9940 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
9942 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
9943 struct wireless_dev *wdev, u32 nlportid,
9944 int freq, int sig_dbm,
9945 const u8 *buf, size_t len, gfp_t gfp)
9947 struct net_device *netdev = wdev->netdev;
9948 struct sk_buff *msg;
9949 void *hdr;
9951 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9952 if (!msg)
9953 return -ENOMEM;
9955 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
9956 if (!hdr) {
9957 nlmsg_free(msg);
9958 return -ENOMEM;
9961 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9962 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9963 netdev->ifindex)) ||
9964 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
9965 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9966 (sig_dbm &&
9967 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9968 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9969 goto nla_put_failure;
9971 genlmsg_end(msg, hdr);
9973 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
9975 nla_put_failure:
9976 genlmsg_cancel(msg, hdr);
9977 nlmsg_free(msg);
9978 return -ENOBUFS;
9981 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
9982 const u8 *buf, size_t len, bool ack, gfp_t gfp)
9984 struct wiphy *wiphy = wdev->wiphy;
9985 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9986 struct net_device *netdev = wdev->netdev;
9987 struct sk_buff *msg;
9988 void *hdr;
9990 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
9992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9993 if (!msg)
9994 return;
9996 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
9997 if (!hdr) {
9998 nlmsg_free(msg);
9999 return;
10002 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10003 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10004 netdev->ifindex)) ||
10005 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
10006 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10007 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10008 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10009 goto nla_put_failure;
10011 genlmsg_end(msg, hdr);
10013 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10014 return;
10016 nla_put_failure:
10017 genlmsg_cancel(msg, hdr);
10018 nlmsg_free(msg);
10020 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
10022 void cfg80211_cqm_rssi_notify(struct net_device *dev,
10023 enum nl80211_cqm_rssi_threshold_event rssi_event,
10024 gfp_t gfp)
10026 struct wireless_dev *wdev = dev->ieee80211_ptr;
10027 struct wiphy *wiphy = wdev->wiphy;
10028 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10029 struct sk_buff *msg;
10030 struct nlattr *pinfoattr;
10031 void *hdr;
10033 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10036 if (!msg)
10037 return;
10039 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10040 if (!hdr) {
10041 nlmsg_free(msg);
10042 return;
10045 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10046 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10047 goto nla_put_failure;
10049 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10050 if (!pinfoattr)
10051 goto nla_put_failure;
10053 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10054 rssi_event))
10055 goto nla_put_failure;
10057 nla_nest_end(msg, pinfoattr);
10059 genlmsg_end(msg, hdr);
10061 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10062 nl80211_mlme_mcgrp.id, gfp);
10063 return;
10065 nla_put_failure:
10066 genlmsg_cancel(msg, hdr);
10067 nlmsg_free(msg);
10069 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
10071 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10072 struct net_device *netdev, const u8 *bssid,
10073 const u8 *replay_ctr, gfp_t gfp)
10075 struct sk_buff *msg;
10076 struct nlattr *rekey_attr;
10077 void *hdr;
10079 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10080 if (!msg)
10081 return;
10083 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10084 if (!hdr) {
10085 nlmsg_free(msg);
10086 return;
10089 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10090 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10091 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10092 goto nla_put_failure;
10094 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10095 if (!rekey_attr)
10096 goto nla_put_failure;
10098 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10099 NL80211_REPLAY_CTR_LEN, replay_ctr))
10100 goto nla_put_failure;
10102 nla_nest_end(msg, rekey_attr);
10104 genlmsg_end(msg, hdr);
10106 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10107 nl80211_mlme_mcgrp.id, gfp);
10108 return;
10110 nla_put_failure:
10111 genlmsg_cancel(msg, hdr);
10112 nlmsg_free(msg);
10115 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10116 const u8 *replay_ctr, gfp_t gfp)
10118 struct wireless_dev *wdev = dev->ieee80211_ptr;
10119 struct wiphy *wiphy = wdev->wiphy;
10120 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10122 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10123 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10125 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10127 static void
10128 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10129 struct net_device *netdev, int index,
10130 const u8 *bssid, bool preauth, gfp_t gfp)
10132 struct sk_buff *msg;
10133 struct nlattr *attr;
10134 void *hdr;
10136 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10137 if (!msg)
10138 return;
10140 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10141 if (!hdr) {
10142 nlmsg_free(msg);
10143 return;
10146 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10147 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10148 goto nla_put_failure;
10150 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10151 if (!attr)
10152 goto nla_put_failure;
10154 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10155 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10156 (preauth &&
10157 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10158 goto nla_put_failure;
10160 nla_nest_end(msg, attr);
10162 genlmsg_end(msg, hdr);
10164 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10165 nl80211_mlme_mcgrp.id, gfp);
10166 return;
10168 nla_put_failure:
10169 genlmsg_cancel(msg, hdr);
10170 nlmsg_free(msg);
10173 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10174 const u8 *bssid, bool preauth, gfp_t gfp)
10176 struct wireless_dev *wdev = dev->ieee80211_ptr;
10177 struct wiphy *wiphy = wdev->wiphy;
10178 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10180 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10181 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10183 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10185 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10186 struct net_device *netdev,
10187 struct cfg80211_chan_def *chandef,
10188 gfp_t gfp)
10190 struct sk_buff *msg;
10191 void *hdr;
10193 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10194 if (!msg)
10195 return;
10197 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10198 if (!hdr) {
10199 nlmsg_free(msg);
10200 return;
10203 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10204 goto nla_put_failure;
10206 if (nl80211_send_chandef(msg, chandef))
10207 goto nla_put_failure;
10209 genlmsg_end(msg, hdr);
10211 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10212 nl80211_mlme_mcgrp.id, gfp);
10213 return;
10215 nla_put_failure:
10216 genlmsg_cancel(msg, hdr);
10217 nlmsg_free(msg);
10220 void cfg80211_ch_switch_notify(struct net_device *dev,
10221 struct cfg80211_chan_def *chandef)
10223 struct wireless_dev *wdev = dev->ieee80211_ptr;
10224 struct wiphy *wiphy = wdev->wiphy;
10225 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10227 trace_cfg80211_ch_switch_notify(dev, chandef);
10229 wdev_lock(wdev);
10231 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10232 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10233 goto out;
10235 wdev->channel = chandef->chan;
10236 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10237 out:
10238 wdev_unlock(wdev);
10239 return;
10241 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10243 void cfg80211_cqm_txe_notify(struct net_device *dev,
10244 const u8 *peer, u32 num_packets,
10245 u32 rate, u32 intvl, gfp_t gfp)
10247 struct wireless_dev *wdev = dev->ieee80211_ptr;
10248 struct wiphy *wiphy = wdev->wiphy;
10249 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10250 struct sk_buff *msg;
10251 struct nlattr *pinfoattr;
10252 void *hdr;
10254 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10255 if (!msg)
10256 return;
10258 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10259 if (!hdr) {
10260 nlmsg_free(msg);
10261 return;
10264 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10265 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10266 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10267 goto nla_put_failure;
10269 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10270 if (!pinfoattr)
10271 goto nla_put_failure;
10273 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10274 goto nla_put_failure;
10276 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10277 goto nla_put_failure;
10279 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10280 goto nla_put_failure;
10282 nla_nest_end(msg, pinfoattr);
10284 genlmsg_end(msg, hdr);
10286 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10287 nl80211_mlme_mcgrp.id, gfp);
10288 return;
10290 nla_put_failure:
10291 genlmsg_cancel(msg, hdr);
10292 nlmsg_free(msg);
10294 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
10296 void
10297 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10298 struct cfg80211_chan_def *chandef,
10299 enum nl80211_radar_event event,
10300 struct net_device *netdev, gfp_t gfp)
10302 struct sk_buff *msg;
10303 void *hdr;
10305 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10306 if (!msg)
10307 return;
10309 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10310 if (!hdr) {
10311 nlmsg_free(msg);
10312 return;
10315 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10316 goto nla_put_failure;
10318 /* NOP and radar events don't need a netdev parameter */
10319 if (netdev) {
10320 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10322 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10323 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10324 goto nla_put_failure;
10327 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10328 goto nla_put_failure;
10330 if (nl80211_send_chandef(msg, chandef))
10331 goto nla_put_failure;
10333 if (genlmsg_end(msg, hdr) < 0) {
10334 nlmsg_free(msg);
10335 return;
10338 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10339 nl80211_mlme_mcgrp.id, gfp);
10340 return;
10342 nla_put_failure:
10343 genlmsg_cancel(msg, hdr);
10344 nlmsg_free(msg);
10347 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10348 const u8 *peer, u32 num_packets, gfp_t gfp)
10350 struct wireless_dev *wdev = dev->ieee80211_ptr;
10351 struct wiphy *wiphy = wdev->wiphy;
10352 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10353 struct sk_buff *msg;
10354 struct nlattr *pinfoattr;
10355 void *hdr;
10357 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10360 if (!msg)
10361 return;
10363 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10364 if (!hdr) {
10365 nlmsg_free(msg);
10366 return;
10369 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10370 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10371 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10372 goto nla_put_failure;
10374 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10375 if (!pinfoattr)
10376 goto nla_put_failure;
10378 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10379 goto nla_put_failure;
10381 nla_nest_end(msg, pinfoattr);
10383 genlmsg_end(msg, hdr);
10385 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10386 nl80211_mlme_mcgrp.id, gfp);
10387 return;
10389 nla_put_failure:
10390 genlmsg_cancel(msg, hdr);
10391 nlmsg_free(msg);
10393 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
10395 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10396 u64 cookie, bool acked, gfp_t gfp)
10398 struct wireless_dev *wdev = dev->ieee80211_ptr;
10399 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10400 struct sk_buff *msg;
10401 void *hdr;
10402 int err;
10404 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10408 if (!msg)
10409 return;
10411 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10412 if (!hdr) {
10413 nlmsg_free(msg);
10414 return;
10417 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10418 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10419 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10420 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10421 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10422 goto nla_put_failure;
10424 err = genlmsg_end(msg, hdr);
10425 if (err < 0) {
10426 nlmsg_free(msg);
10427 return;
10430 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10431 nl80211_mlme_mcgrp.id, gfp);
10432 return;
10434 nla_put_failure:
10435 genlmsg_cancel(msg, hdr);
10436 nlmsg_free(msg);
10438 EXPORT_SYMBOL(cfg80211_probe_status);
10440 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10441 const u8 *frame, size_t len,
10442 int freq, int sig_dbm)
10444 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10445 struct sk_buff *msg;
10446 void *hdr;
10447 struct cfg80211_beacon_registration *reg;
10449 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10451 spin_lock_bh(&rdev->beacon_registrations_lock);
10452 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10453 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10454 if (!msg) {
10455 spin_unlock_bh(&rdev->beacon_registrations_lock);
10456 return;
10459 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10460 if (!hdr)
10461 goto nla_put_failure;
10463 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10464 (freq &&
10465 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10466 (sig_dbm &&
10467 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10468 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10469 goto nla_put_failure;
10471 genlmsg_end(msg, hdr);
10473 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
10475 spin_unlock_bh(&rdev->beacon_registrations_lock);
10476 return;
10478 nla_put_failure:
10479 spin_unlock_bh(&rdev->beacon_registrations_lock);
10480 if (hdr)
10481 genlmsg_cancel(msg, hdr);
10482 nlmsg_free(msg);
10484 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10486 #ifdef CONFIG_PM
10487 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10488 struct cfg80211_wowlan_wakeup *wakeup,
10489 gfp_t gfp)
10491 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10492 struct sk_buff *msg;
10493 void *hdr;
10494 int err, size = 200;
10496 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10498 if (wakeup)
10499 size += wakeup->packet_present_len;
10501 msg = nlmsg_new(size, gfp);
10502 if (!msg)
10503 return;
10505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10506 if (!hdr)
10507 goto free_msg;
10509 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10510 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10511 goto free_msg;
10513 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10514 wdev->netdev->ifindex))
10515 goto free_msg;
10517 if (wakeup) {
10518 struct nlattr *reasons;
10520 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10522 if (wakeup->disconnect &&
10523 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10524 goto free_msg;
10525 if (wakeup->magic_pkt &&
10526 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10527 goto free_msg;
10528 if (wakeup->gtk_rekey_failure &&
10529 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10530 goto free_msg;
10531 if (wakeup->eap_identity_req &&
10532 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10533 goto free_msg;
10534 if (wakeup->four_way_handshake &&
10535 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10536 goto free_msg;
10537 if (wakeup->rfkill_release &&
10538 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10539 goto free_msg;
10541 if (wakeup->pattern_idx >= 0 &&
10542 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10543 wakeup->pattern_idx))
10544 goto free_msg;
10546 if (wakeup->tcp_match)
10547 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10549 if (wakeup->tcp_connlost)
10550 nla_put_flag(msg,
10551 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10553 if (wakeup->tcp_nomoretokens)
10554 nla_put_flag(msg,
10555 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10557 if (wakeup->packet) {
10558 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10559 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10561 if (!wakeup->packet_80211) {
10562 pkt_attr =
10563 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10564 len_attr =
10565 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10568 if (wakeup->packet_len &&
10569 nla_put_u32(msg, len_attr, wakeup->packet_len))
10570 goto free_msg;
10572 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10573 wakeup->packet))
10574 goto free_msg;
10577 nla_nest_end(msg, reasons);
10580 err = genlmsg_end(msg, hdr);
10581 if (err < 0)
10582 goto free_msg;
10584 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10585 nl80211_mlme_mcgrp.id, gfp);
10586 return;
10588 free_msg:
10589 nlmsg_free(msg);
10591 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10592 #endif
10594 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10595 enum nl80211_tdls_operation oper,
10596 u16 reason_code, gfp_t gfp)
10598 struct wireless_dev *wdev = dev->ieee80211_ptr;
10599 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10600 struct sk_buff *msg;
10601 void *hdr;
10602 int err;
10604 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10605 reason_code);
10607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10608 if (!msg)
10609 return;
10611 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10612 if (!hdr) {
10613 nlmsg_free(msg);
10614 return;
10617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10618 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10619 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10620 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10621 (reason_code > 0 &&
10622 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10623 goto nla_put_failure;
10625 err = genlmsg_end(msg, hdr);
10626 if (err < 0) {
10627 nlmsg_free(msg);
10628 return;
10631 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10632 nl80211_mlme_mcgrp.id, gfp);
10633 return;
10635 nla_put_failure:
10636 genlmsg_cancel(msg, hdr);
10637 nlmsg_free(msg);
10639 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10641 static int nl80211_netlink_notify(struct notifier_block * nb,
10642 unsigned long state,
10643 void *_notify)
10645 struct netlink_notify *notify = _notify;
10646 struct cfg80211_registered_device *rdev;
10647 struct wireless_dev *wdev;
10648 struct cfg80211_beacon_registration *reg, *tmp;
10650 if (state != NETLINK_URELEASE)
10651 return NOTIFY_DONE;
10653 rcu_read_lock();
10655 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
10656 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
10657 cfg80211_mlme_unregister_socket(wdev, notify->portid);
10659 spin_lock_bh(&rdev->beacon_registrations_lock);
10660 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10661 list) {
10662 if (reg->nlportid == notify->portid) {
10663 list_del(&reg->list);
10664 kfree(reg);
10665 break;
10668 spin_unlock_bh(&rdev->beacon_registrations_lock);
10671 rcu_read_unlock();
10673 return NOTIFY_DONE;
10676 static struct notifier_block nl80211_netlink_notifier = {
10677 .notifier_call = nl80211_netlink_notify,
10680 void cfg80211_ft_event(struct net_device *netdev,
10681 struct cfg80211_ft_event_params *ft_event)
10683 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10684 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10685 struct sk_buff *msg;
10686 void *hdr;
10687 int err;
10689 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10691 if (!ft_event->target_ap)
10692 return;
10694 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10695 if (!msg)
10696 return;
10698 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10699 if (!hdr) {
10700 nlmsg_free(msg);
10701 return;
10704 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10705 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10706 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10707 if (ft_event->ies)
10708 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10709 if (ft_event->ric_ies)
10710 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10711 ft_event->ric_ies);
10713 err = genlmsg_end(msg, hdr);
10714 if (err < 0) {
10715 nlmsg_free(msg);
10716 return;
10719 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10720 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10722 EXPORT_SYMBOL(cfg80211_ft_event);
10724 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10726 struct cfg80211_registered_device *rdev;
10727 struct sk_buff *msg;
10728 void *hdr;
10729 u32 nlportid;
10731 rdev = wiphy_to_dev(wdev->wiphy);
10732 if (!rdev->crit_proto_nlportid)
10733 return;
10735 nlportid = rdev->crit_proto_nlportid;
10736 rdev->crit_proto_nlportid = 0;
10738 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10739 if (!msg)
10740 return;
10742 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10743 if (!hdr)
10744 goto nla_put_failure;
10746 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10747 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10748 goto nla_put_failure;
10750 genlmsg_end(msg, hdr);
10752 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10753 return;
10755 nla_put_failure:
10756 if (hdr)
10757 genlmsg_cancel(msg, hdr);
10758 nlmsg_free(msg);
10761 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10763 /* initialisation/exit functions */
10765 int nl80211_init(void)
10767 int err;
10769 err = genl_register_family_with_ops(&nl80211_fam,
10770 nl80211_ops, ARRAY_SIZE(nl80211_ops));
10771 if (err)
10772 return err;
10774 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10775 if (err)
10776 goto err_out;
10778 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10779 if (err)
10780 goto err_out;
10782 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10783 if (err)
10784 goto err_out;
10786 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10787 if (err)
10788 goto err_out;
10790 #ifdef CONFIG_NL80211_TESTMODE
10791 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10792 if (err)
10793 goto err_out;
10794 #endif
10796 err = netlink_register_notifier(&nl80211_netlink_notifier);
10797 if (err)
10798 goto err_out;
10800 return 0;
10801 err_out:
10802 genl_unregister_family(&nl80211_fam);
10803 return err;
10806 void nl80211_exit(void)
10808 netlink_unregister_notifier(&nl80211_netlink_notifier);
10809 genl_unregister_family(&nl80211_fam);