blob: 7950506395a81a558e4d901ebb405a1de5c108c0 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2013-2014 Intel Mobile Communications GmbH
6 * Copyright 2015 Intel Deutschland GmbH
7 */
8
9#include <linux/if.h>
10#include <linux/module.h>
11#include <linux/err.h>
12#include <linux/slab.h>
13#include <linux/list.h>
14#include <linux/if_ether.h>
15#include <linux/ieee80211.h>
16#include <linux/nl80211.h>
17#include <linux/rtnetlink.h>
18#include <linux/netlink.h>
19#include <linux/etherdevice.h>
20#include <net/net_namespace.h>
21#include <net/genetlink.h>
22#include <net/cfg80211.h>
23#include <net/sock.h>
24#include <net/inet_connection_sock.h>
25#include "core.h"
26#include "nl80211.h"
27#include "reg.h"
28#include "rdev-ops.h"
29
30static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
31 struct genl_info *info,
32 struct cfg80211_crypto_settings *settings,
33 int cipher_limit);
34
35static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
38 struct genl_info *info);
39
40/* the netlink family */
41static struct genl_family nl80211_fam = {
42 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
43 .name = NL80211_GENL_NAME, /* have users key off the name instead */
44 .hdrsize = 0, /* no private header */
45 .version = 1, /* no particular meaning now */
46 .maxattr = NL80211_ATTR_MAX,
47 .netnsok = true,
48 .pre_doit = nl80211_pre_doit,
49 .post_doit = nl80211_post_doit,
50};
51
52/* multicast groups */
53enum nl80211_multicast_groups {
54 NL80211_MCGRP_CONFIG,
55 NL80211_MCGRP_SCAN,
56 NL80211_MCGRP_REGULATORY,
57 NL80211_MCGRP_MLME,
58 NL80211_MCGRP_VENDOR,
59 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
60};
61
62static const struct genl_multicast_group nl80211_mcgrps[] = {
63 [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
64 [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
65 [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
66 [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
67 [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
68#ifdef CONFIG_NL80211_TESTMODE
69 [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
70#endif
71};
72
73/* returns ERR_PTR values */
74static struct wireless_dev *
75__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
76{
77 struct cfg80211_registered_device *rdev;
78 struct wireless_dev *result = NULL;
79 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
80 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
81 u64 wdev_id;
82 int wiphy_idx = -1;
83 int ifidx = -1;
84
85 ASSERT_RTNL();
86
87 if (!have_ifidx && !have_wdev_id)
88 return ERR_PTR(-EINVAL);
89
90 if (have_ifidx)
91 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
92 if (have_wdev_id) {
93 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
94 wiphy_idx = wdev_id >> 32;
95 }
96
97 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
98 struct wireless_dev *wdev;
99
100 if (wiphy_net(&rdev->wiphy) != netns)
101 continue;
102
103 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
104 continue;
105
106 list_for_each_entry(wdev, &rdev->wdev_list, list) {
107 if (have_ifidx && wdev->netdev &&
108 wdev->netdev->ifindex == ifidx) {
109 result = wdev;
110 break;
111 }
112 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
113 result = wdev;
114 break;
115 }
116 }
117
118 if (result)
119 break;
120 }
121
122 if (result)
123 return result;
124 return ERR_PTR(-ENODEV);
125}
126
127static struct cfg80211_registered_device *
128__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
129{
130 struct cfg80211_registered_device *rdev = NULL, *tmp;
131 struct net_device *netdev;
132
133 ASSERT_RTNL();
134
135 if (!attrs[NL80211_ATTR_WIPHY] &&
136 !attrs[NL80211_ATTR_IFINDEX] &&
137 !attrs[NL80211_ATTR_WDEV])
138 return ERR_PTR(-EINVAL);
139
140 if (attrs[NL80211_ATTR_WIPHY])
141 rdev = cfg80211_rdev_by_wiphy_idx(
142 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
143
144 if (attrs[NL80211_ATTR_WDEV]) {
145 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
146 struct wireless_dev *wdev;
147 bool found = false;
148
149 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
150 if (tmp) {
151 /* make sure wdev exists */
152 list_for_each_entry(wdev, &tmp->wdev_list, list) {
153 if (wdev->identifier != (u32)wdev_id)
154 continue;
155 found = true;
156 break;
157 }
158
159 if (!found)
160 tmp = NULL;
161
162 if (rdev && tmp != rdev)
163 return ERR_PTR(-EINVAL);
164 rdev = tmp;
165 }
166 }
167
168 if (attrs[NL80211_ATTR_IFINDEX]) {
169 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
170 netdev = __dev_get_by_index(netns, ifindex);
171 if (netdev) {
172 if (netdev->ieee80211_ptr)
173 tmp = wiphy_to_rdev(
174 netdev->ieee80211_ptr->wiphy);
175 else
176 tmp = NULL;
177
178 /* not wireless device -- return error */
179 if (!tmp)
180 return ERR_PTR(-EINVAL);
181
182 /* mismatch -- return error */
183 if (rdev && tmp != rdev)
184 return ERR_PTR(-EINVAL);
185
186 rdev = tmp;
187 }
188 }
189
190 if (!rdev)
191 return ERR_PTR(-ENODEV);
192
193 if (netns != wiphy_net(&rdev->wiphy))
194 return ERR_PTR(-ENODEV);
195
196 return rdev;
197}
198
199/*
200 * This function returns a pointer to the driver
201 * that the genl_info item that is passed refers to.
202 *
203 * The result of this can be a PTR_ERR and hence must
204 * be checked with IS_ERR() for errors.
205 */
206static struct cfg80211_registered_device *
207cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
208{
209 return __cfg80211_rdev_from_attrs(netns, info->attrs);
210}
211
212/* policy for the attributes */
213static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
214 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
215 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
216 .len = 20-1 },
217 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
218
219 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
220 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
221 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
222 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
223 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
224
225 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
226 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
227 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
228 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
229 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
231
232 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
233 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
234 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
235
236 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
237 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
238
239 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
240 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
241 .len = WLAN_MAX_KEY_LEN },
242 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
243 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
244 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
245 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
246 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
247
248 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
249 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
250 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
251 .len = IEEE80211_MAX_DATA_LEN },
252 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
255 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
256 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
258 .len = NL80211_MAX_SUPP_RATES },
259 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
260 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
261 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
262 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
263 .len = IEEE80211_MAX_MESH_ID_LEN },
264 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
265
266 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
267 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
268
269 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
270 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
271 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
273 .len = NL80211_MAX_SUPP_RATES },
274 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
275
276 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
277 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
278
279 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
280
281 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
282 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
283 .len = IEEE80211_MAX_DATA_LEN },
284 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
285 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
286
287 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
288 .len = IEEE80211_MAX_SSID_LEN },
289 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
290 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
291 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
292 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
293 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
294 [NL80211_ATTR_STA_FLAGS2] = {
295 .len = sizeof(struct nl80211_sta_flag_update),
296 },
297 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
298 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
299 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
300 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
301 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
302 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
303 [NL80211_ATTR_PID] = { .type = NLA_U32 },
304 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
305 [NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
306 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
307 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
308 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
309 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
310 .len = IEEE80211_MAX_DATA_LEN },
311 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
312 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
313 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
314 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
315 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
316 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
317 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
318 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
319 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
321 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
322 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
323 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
324 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
325 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
326 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
327 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
328 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
329 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
330 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
331 .len = IEEE80211_MAX_DATA_LEN },
332 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
333 .len = IEEE80211_MAX_DATA_LEN },
334 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
335 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
336 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
337 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
338 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
339 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
340 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
341 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
342 [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
343 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
344 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
345 .len = IEEE80211_MAX_DATA_LEN },
346 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
347 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
348 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
349 .len = NL80211_HT_CAPABILITY_LEN
350 },
351 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
352 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
353 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
354 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
355 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
356 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
357 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
358 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
359 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
360 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
361 [NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
362 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
363 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
364 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
365 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
366 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
367 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
368 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
369 .len = NL80211_VHT_CAPABILITY_LEN,
370 },
371 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
372 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
373 .len = IEEE80211_MAX_DATA_LEN },
374 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
375 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
376 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
377 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
378 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
379 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
380 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
381 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
382 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
383 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
384 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
385 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
386 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
387 [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
388 .len = IEEE80211_QOS_MAP_LEN_MAX },
389 [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
390 [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
391 [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
392 [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
393 [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
394 [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
395 [NL80211_ATTR_TSID] = { .type = NLA_U8 },
396 [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
397 [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
398 [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
399 [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
400 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
401 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
402 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
403 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
404};
405
406/* policy for the key attributes */
407static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
408 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
409 [NL80211_KEY_IDX] = { .type = NLA_U8 },
410 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
411 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
412 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
413 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
414 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
415 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
416};
417
418/* policy for the key default flags */
419static const struct nla_policy
420nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
421 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
422 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
423};
424
425/* policy for WoWLAN attributes */
426static const struct nla_policy
427nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
428 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
429 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
430 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
431 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
432 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
433 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
434 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
435 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
436 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
437 [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
438};
439
440static const struct nla_policy
441nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
442 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
443 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
444 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
445 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
446 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
447 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
448 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
449 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
450 },
451 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
452 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
453 },
454 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
455 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
456 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
457};
458
459/* policy for coalesce rule attributes */
460static const struct nla_policy
461nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
462 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
463 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
464 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
465};
466
467/* policy for GTK rekey offload attributes */
468static const struct nla_policy
469nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
470 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
471 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
472 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
473};
474
475static const struct nla_policy
476nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
477 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
478 .len = IEEE80211_MAX_SSID_LEN },
479 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
480};
481
482static const struct nla_policy
483nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
484 [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
485 [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
486};
487
488/* policy for packet pattern attributes */
489static const struct nla_policy
490nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
491 [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
492 [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
493 [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
494};
495
496static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
497 struct netlink_callback *cb,
498 struct cfg80211_registered_device **rdev,
499 struct wireless_dev **wdev)
500{
501 int err;
502
503 if (!cb->args[0]) {
504 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
505 nl80211_fam.attrbuf, nl80211_fam.maxattr,
506 nl80211_policy);
507 if (err)
508 return err;
509
510 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
511 nl80211_fam.attrbuf);
512 if (IS_ERR(*wdev))
513 return PTR_ERR(*wdev);
514 *rdev = wiphy_to_rdev((*wdev)->wiphy);
515 /* 0 is the first index - add 1 to parse only once */
516 cb->args[0] = (*rdev)->wiphy_idx + 1;
517 cb->args[1] = (*wdev)->identifier;
518 } else {
519 /* subtract the 1 again here */
520 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
521 struct wireless_dev *tmp;
522
523 if (!wiphy)
524 return -ENODEV;
525 *rdev = wiphy_to_rdev(wiphy);
526 *wdev = NULL;
527
528 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
529 if (tmp->identifier == cb->args[1]) {
530 *wdev = tmp;
531 break;
532 }
533 }
534
535 if (!*wdev)
536 return -ENODEV;
537 }
538
539 return 0;
540}
541
542/* IE validation */
543static bool is_valid_ie_attr(const struct nlattr *attr)
544{
545 const u8 *pos;
546 int len;
547
548 if (!attr)
549 return true;
550
551 pos = nla_data(attr);
552 len = nla_len(attr);
553
554 while (len) {
555 u8 elemlen;
556
557 if (len < 2)
558 return false;
559 len -= 2;
560
561 elemlen = pos[1];
562 if (elemlen > len)
563 return false;
564
565 len -= elemlen;
566 pos += 2 + elemlen;
567 }
568
569 return true;
570}
571
572/* message building helper */
573static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
574 int flags, u8 cmd)
575{
576 /* since there is no private header just add the generic one */
577 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
578}
579
580static int nl80211_msg_put_channel(struct sk_buff *msg,
581 struct ieee80211_channel *chan,
582 bool large)
583{
584 /* Some channels must be completely excluded from the
585 * list to protect old user-space tools from breaking
586 */
587 if (!large && chan->flags &
588 (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
589 return 0;
590
591 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
592 chan->center_freq))
593 goto nla_put_failure;
594
595 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
596 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
597 goto nla_put_failure;
598 if (chan->flags & IEEE80211_CHAN_NO_IR) {
599 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
600 goto nla_put_failure;
601 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
602 goto nla_put_failure;
603 }
604 if (chan->flags & IEEE80211_CHAN_RADAR) {
605 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
606 goto nla_put_failure;
607 if (large) {
608 u32 time;
609
610 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
611
612 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
613 chan->dfs_state))
614 goto nla_put_failure;
615 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
616 time))
617 goto nla_put_failure;
618 if (nla_put_u32(msg,
619 NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
620 chan->dfs_cac_ms))
621 goto nla_put_failure;
622 }
623 }
624
625 if (large) {
626 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
627 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
628 goto nla_put_failure;
629 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
630 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
631 goto nla_put_failure;
632 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
633 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
634 goto nla_put_failure;
635 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
636 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
637 goto nla_put_failure;
638 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
639 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
640 goto nla_put_failure;
641 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
642 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
643 goto nla_put_failure;
644 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
645 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
646 goto nla_put_failure;
647 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
648 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
649 goto nla_put_failure;
650 }
651
652 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
653 DBM_TO_MBM(chan->max_power)))
654 goto nla_put_failure;
655
656 return 0;
657
658 nla_put_failure:
659 return -ENOBUFS;
660}
661
662/* netlink command implementations */
663
664struct key_parse {
665 struct key_params p;
666 int idx;
667 int type;
668 bool def, defmgmt;
669 bool def_uni, def_multi;
670};
671
672static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
673{
674 struct nlattr *tb[NL80211_KEY_MAX + 1];
675 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
676 nl80211_key_policy);
677 if (err)
678 return err;
679
680 k->def = !!tb[NL80211_KEY_DEFAULT];
681 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
682
683 if (k->def) {
684 k->def_uni = true;
685 k->def_multi = true;
686 }
687 if (k->defmgmt)
688 k->def_multi = true;
689
690 if (tb[NL80211_KEY_IDX])
691 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
692
693 if (tb[NL80211_KEY_DATA]) {
694 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
695 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
696 }
697
698 if (tb[NL80211_KEY_SEQ]) {
699 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
700 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
701 }
702
703 if (tb[NL80211_KEY_CIPHER])
704 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
705
706 if (tb[NL80211_KEY_TYPE]) {
707 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
708 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
709 return -EINVAL;
710 }
711
712 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
713 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
714 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
715 tb[NL80211_KEY_DEFAULT_TYPES],
716 nl80211_key_default_policy);
717 if (err)
718 return err;
719
720 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
721 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
722 }
723
724 return 0;
725}
726
727static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
728{
729 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
730 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
731 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
732 }
733
734 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
735 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
736 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
737 }
738
739 if (info->attrs[NL80211_ATTR_KEY_IDX])
740 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
741
742 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
743 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
744
745 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
746 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
747
748 if (k->def) {
749 k->def_uni = true;
750 k->def_multi = true;
751 }
752 if (k->defmgmt)
753 k->def_multi = true;
754
755 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
756 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
757 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
758 return -EINVAL;
759 }
760
761 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
762 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
763 int err = nla_parse_nested(
764 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
765 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
766 nl80211_key_default_policy);
767 if (err)
768 return err;
769
770 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
771 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
772 }
773
774 return 0;
775}
776
777static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
778{
779 int err;
780
781 memset(k, 0, sizeof(*k));
782 k->idx = -1;
783 k->type = -1;
784
785 if (info->attrs[NL80211_ATTR_KEY])
786 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
787 else
788 err = nl80211_parse_key_old(info, k);
789
790 if (err)
791 return err;
792
793 if (k->def && k->defmgmt)
794 return -EINVAL;
795
796 if (k->defmgmt) {
797 if (k->def_uni || !k->def_multi)
798 return -EINVAL;
799 }
800
801 if (k->idx != -1) {
802 if (k->defmgmt) {
803 if (k->idx < 4 || k->idx > 5)
804 return -EINVAL;
805 } else if (k->def) {
806 if (k->idx < 0 || k->idx > 3)
807 return -EINVAL;
808 } else {
809 if (k->idx < 0 || k->idx > 5)
810 return -EINVAL;
811 }
812 }
813
814 return 0;
815}
816
817static struct cfg80211_cached_keys *
818nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
819 struct nlattr *keys, bool *no_ht)
820{
821 struct key_parse parse;
822 struct nlattr *key;
823 struct cfg80211_cached_keys *result;
824 int rem, err, def = 0;
825
826 result = kzalloc(sizeof(*result), GFP_KERNEL);
827 if (!result)
828 return ERR_PTR(-ENOMEM);
829
830 result->def = -1;
831 result->defmgmt = -1;
832
833 nla_for_each_nested(key, keys, rem) {
834 memset(&parse, 0, sizeof(parse));
835 parse.idx = -1;
836
837 err = nl80211_parse_key_new(key, &parse);
838 if (err)
839 goto error;
840 err = -EINVAL;
841 if (!parse.p.key)
842 goto error;
843 if (parse.idx < 0 || parse.idx > 4)
844 goto error;
845 if (parse.def) {
846 if (def)
847 goto error;
848 def = 1;
849 result->def = parse.idx;
850 if (!parse.def_uni || !parse.def_multi)
851 goto error;
852 } else if (parse.defmgmt)
853 goto error;
854 err = cfg80211_validate_key_settings(rdev, &parse.p,
855 parse.idx, false, NULL);
856 if (err)
857 goto error;
858 result->params[parse.idx].cipher = parse.p.cipher;
859 result->params[parse.idx].key_len = parse.p.key_len;
860 result->params[parse.idx].key = result->data[parse.idx];
861 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
862
863 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
864 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
865 if (no_ht)
866 *no_ht = true;
867 }
868 }
869
870 return result;
871 error:
872 kfree(result);
873 return ERR_PTR(err);
874}
875
876static int nl80211_key_allowed(struct wireless_dev *wdev)
877{
878 ASSERT_WDEV_LOCK(wdev);
879
880 switch (wdev->iftype) {
881 case NL80211_IFTYPE_AP:
882 case NL80211_IFTYPE_AP_VLAN:
883 case NL80211_IFTYPE_P2P_GO:
884 case NL80211_IFTYPE_MESH_POINT:
885 break;
886 case NL80211_IFTYPE_ADHOC:
887 case NL80211_IFTYPE_STATION:
888 case NL80211_IFTYPE_P2P_CLIENT:
889 if (!wdev->current_bss)
890 return -ENOLINK;
891 break;
892 case NL80211_IFTYPE_UNSPECIFIED:
893 case NL80211_IFTYPE_OCB:
894 case NL80211_IFTYPE_MONITOR:
895 case NL80211_IFTYPE_P2P_DEVICE:
896 case NL80211_IFTYPE_WDS:
897 case NUM_NL80211_IFTYPES:
898 return -EINVAL;
899 }
900
901 return 0;
902}
903
904static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
905 struct nlattr *tb)
906{
907 struct ieee80211_channel *chan;
908
909 if (tb == NULL)
910 return NULL;
911 chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
912 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
913 return NULL;
914 return chan;
915}
916
917static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
918{
919 struct nlattr *nl_modes = nla_nest_start(msg, attr);
920 int i;
921
922 if (!nl_modes)
923 goto nla_put_failure;
924
925 i = 0;
926 while (ifmodes) {
927 if ((ifmodes & 1) && nla_put_flag(msg, i))
928 goto nla_put_failure;
929 ifmodes >>= 1;
930 i++;
931 }
932
933 nla_nest_end(msg, nl_modes);
934 return 0;
935
936nla_put_failure:
937 return -ENOBUFS;
938}
939
940static int nl80211_put_iface_combinations(struct wiphy *wiphy,
941 struct sk_buff *msg,
942 bool large)
943{
944 struct nlattr *nl_combis;
945 int i, j;
946
947 nl_combis = nla_nest_start(msg,
948 NL80211_ATTR_INTERFACE_COMBINATIONS);
949 if (!nl_combis)
950 goto nla_put_failure;
951
952 for (i = 0; i < wiphy->n_iface_combinations; i++) {
953 const struct ieee80211_iface_combination *c;
954 struct nlattr *nl_combi, *nl_limits;
955
956 c = &wiphy->iface_combinations[i];
957
958 nl_combi = nla_nest_start(msg, i + 1);
959 if (!nl_combi)
960 goto nla_put_failure;
961
962 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
963 if (!nl_limits)
964 goto nla_put_failure;
965
966 for (j = 0; j < c->n_limits; j++) {
967 struct nlattr *nl_limit;
968
969 nl_limit = nla_nest_start(msg, j + 1);
970 if (!nl_limit)
971 goto nla_put_failure;
972 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
973 c->limits[j].max))
974 goto nla_put_failure;
975 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
976 c->limits[j].types))
977 goto nla_put_failure;
978 nla_nest_end(msg, nl_limit);
979 }
980
981 nla_nest_end(msg, nl_limits);
982
983 if (c->beacon_int_infra_match &&
984 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
985 goto nla_put_failure;
986 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
987 c->num_different_channels) ||
988 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
989 c->max_interfaces))
990 goto nla_put_failure;
991 if (large &&
992 (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
993 c->radar_detect_widths) ||
994 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
995 c->radar_detect_regions)))
996 goto nla_put_failure;
997
998 nla_nest_end(msg, nl_combi);
999 }
1000
1001 nla_nest_end(msg, nl_combis);
1002
1003 return 0;
1004nla_put_failure:
1005 return -ENOBUFS;
1006}
1007
1008#ifdef CONFIG_PM
1009static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1010 struct sk_buff *msg)
1011{
1012 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1013 struct nlattr *nl_tcp;
1014
1015 if (!tcp)
1016 return 0;
1017
1018 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1019 if (!nl_tcp)
1020 return -ENOBUFS;
1021
1022 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1023 tcp->data_payload_max))
1024 return -ENOBUFS;
1025
1026 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1027 tcp->data_payload_max))
1028 return -ENOBUFS;
1029
1030 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1031 return -ENOBUFS;
1032
1033 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1034 sizeof(*tcp->tok), tcp->tok))
1035 return -ENOBUFS;
1036
1037 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1038 tcp->data_interval_max))
1039 return -ENOBUFS;
1040
1041 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1042 tcp->wake_payload_max))
1043 return -ENOBUFS;
1044
1045 nla_nest_end(msg, nl_tcp);
1046 return 0;
1047}
1048
1049static int nl80211_send_wowlan(struct sk_buff *msg,
1050 struct cfg80211_registered_device *rdev,
1051 bool large)
1052{
1053 struct nlattr *nl_wowlan;
1054
1055 if (!rdev->wiphy.wowlan)
1056 return 0;
1057
1058 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1059 if (!nl_wowlan)
1060 return -ENOBUFS;
1061
1062 if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1063 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1064 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1065 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1066 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1067 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1068 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1069 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1070 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1071 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1072 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1073 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1074 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1075 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1076 ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1077 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1078 return -ENOBUFS;
1079
1080 if (rdev->wiphy.wowlan->n_patterns) {
1081 struct nl80211_pattern_support pat = {
1082 .max_patterns = rdev->wiphy.wowlan->n_patterns,
1083 .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1084 .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1085 .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1086 };
1087
1088 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1089 sizeof(pat), &pat))
1090 return -ENOBUFS;
1091 }
1092
1093 if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1094 nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1095 rdev->wiphy.wowlan->max_nd_match_sets))
1096 return -ENOBUFS;
1097
1098 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1099 return -ENOBUFS;
1100
1101 nla_nest_end(msg, nl_wowlan);
1102
1103 return 0;
1104}
1105#endif
1106
1107static int nl80211_send_coalesce(struct sk_buff *msg,
1108 struct cfg80211_registered_device *rdev)
1109{
1110 struct nl80211_coalesce_rule_support rule;
1111
1112 if (!rdev->wiphy.coalesce)
1113 return 0;
1114
1115 rule.max_rules = rdev->wiphy.coalesce->n_rules;
1116 rule.max_delay = rdev->wiphy.coalesce->max_delay;
1117 rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1118 rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1119 rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1120 rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1121
1122 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1123 return -ENOBUFS;
1124
1125 return 0;
1126}
1127
1128static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1129 struct ieee80211_supported_band *sband)
1130{
1131 struct nlattr *nl_rates, *nl_rate;
1132 struct ieee80211_rate *rate;
1133 int i;
1134
1135 /* add HT info */
1136 if (sband->ht_cap.ht_supported &&
1137 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1138 sizeof(sband->ht_cap.mcs),
1139 &sband->ht_cap.mcs) ||
1140 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1141 sband->ht_cap.cap) ||
1142 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1143 sband->ht_cap.ampdu_factor) ||
1144 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1145 sband->ht_cap.ampdu_density)))
1146 return -ENOBUFS;
1147
1148 /* add VHT info */
1149 if (sband->vht_cap.vht_supported &&
1150 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1151 sizeof(sband->vht_cap.vht_mcs),
1152 &sband->vht_cap.vht_mcs) ||
1153 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1154 sband->vht_cap.cap)))
1155 return -ENOBUFS;
1156
1157 /* add bitrates */
1158 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1159 if (!nl_rates)
1160 return -ENOBUFS;
1161
1162 for (i = 0; i < sband->n_bitrates; i++) {
1163 nl_rate = nla_nest_start(msg, i);
1164 if (!nl_rate)
1165 return -ENOBUFS;
1166
1167 rate = &sband->bitrates[i];
1168 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1169 rate->bitrate))
1170 return -ENOBUFS;
1171 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1172 nla_put_flag(msg,
1173 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1174 return -ENOBUFS;
1175
1176 nla_nest_end(msg, nl_rate);
1177 }
1178
1179 nla_nest_end(msg, nl_rates);
1180
1181 return 0;
1182}
1183
1184static int
1185nl80211_send_mgmt_stypes(struct sk_buff *msg,
1186 const struct ieee80211_txrx_stypes *mgmt_stypes)
1187{
1188 u16 stypes;
1189 struct nlattr *nl_ftypes, *nl_ifs;
1190 enum nl80211_iftype ift;
1191 int i;
1192
1193 if (!mgmt_stypes)
1194 return 0;
1195
1196 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1197 if (!nl_ifs)
1198 return -ENOBUFS;
1199
1200 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1201 nl_ftypes = nla_nest_start(msg, ift);
1202 if (!nl_ftypes)
1203 return -ENOBUFS;
1204 i = 0;
1205 stypes = mgmt_stypes[ift].tx;
1206 while (stypes) {
1207 if ((stypes & 1) &&
1208 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1209 (i << 4) | IEEE80211_FTYPE_MGMT))
1210 return -ENOBUFS;
1211 stypes >>= 1;
1212 i++;
1213 }
1214 nla_nest_end(msg, nl_ftypes);
1215 }
1216
1217 nla_nest_end(msg, nl_ifs);
1218
1219 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1220 if (!nl_ifs)
1221 return -ENOBUFS;
1222
1223 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1224 nl_ftypes = nla_nest_start(msg, ift);
1225 if (!nl_ftypes)
1226 return -ENOBUFS;
1227 i = 0;
1228 stypes = mgmt_stypes[ift].rx;
1229 while (stypes) {
1230 if ((stypes & 1) &&
1231 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1232 (i << 4) | IEEE80211_FTYPE_MGMT))
1233 return -ENOBUFS;
1234 stypes >>= 1;
1235 i++;
1236 }
1237 nla_nest_end(msg, nl_ftypes);
1238 }
1239 nla_nest_end(msg, nl_ifs);
1240
1241 return 0;
1242}
1243
1244struct nl80211_dump_wiphy_state {
1245 s64 filter_wiphy;
1246 long start;
1247 long split_start, band_start, chan_start;
1248 bool split;
1249};
1250
1251static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1252 enum nl80211_commands cmd,
1253 struct sk_buff *msg, u32 portid, u32 seq,
1254 int flags, struct nl80211_dump_wiphy_state *state)
1255{
1256 void *hdr;
1257 struct nlattr *nl_bands, *nl_band;
1258 struct nlattr *nl_freqs, *nl_freq;
1259 struct nlattr *nl_cmds;
1260 enum ieee80211_band band;
1261 struct ieee80211_channel *chan;
1262 int i;
1263 const struct ieee80211_txrx_stypes *mgmt_stypes =
1264 rdev->wiphy.mgmt_stypes;
1265 u32 features;
1266
1267 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1268 if (!hdr)
1269 return -ENOBUFS;
1270
1271 if (WARN_ON(!state))
1272 return -EINVAL;
1273
1274 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1275 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1276 wiphy_name(&rdev->wiphy)) ||
1277 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1278 cfg80211_rdev_list_generation))
1279 goto nla_put_failure;
1280
1281 if (cmd != NL80211_CMD_NEW_WIPHY)
1282 goto finish;
1283
1284 switch (state->split_start) {
1285 case 0:
1286 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1287 rdev->wiphy.retry_short) ||
1288 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1289 rdev->wiphy.retry_long) ||
1290 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1291 rdev->wiphy.frag_threshold) ||
1292 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1293 rdev->wiphy.rts_threshold) ||
1294 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1295 rdev->wiphy.coverage_class) ||
1296 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1297 rdev->wiphy.max_scan_ssids) ||
1298 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1299 rdev->wiphy.max_sched_scan_ssids) ||
1300 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1301 rdev->wiphy.max_scan_ie_len) ||
1302 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1303 rdev->wiphy.max_sched_scan_ie_len) ||
1304 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1305 rdev->wiphy.max_match_sets) ||
1306 nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1307 rdev->wiphy.max_sched_scan_plans) ||
1308 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1309 rdev->wiphy.max_sched_scan_plan_interval) ||
1310 nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1311 rdev->wiphy.max_sched_scan_plan_iterations))
1312 goto nla_put_failure;
1313
1314 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1315 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1316 goto nla_put_failure;
1317 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1318 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1319 goto nla_put_failure;
1320 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1321 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1322 goto nla_put_failure;
1323 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1324 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1325 goto nla_put_failure;
1326 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1327 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1328 goto nla_put_failure;
1329 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1330 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1331 goto nla_put_failure;
1332 state->split_start++;
1333 if (state->split)
1334 break;
1335 case 1:
1336 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1337 sizeof(u32) * rdev->wiphy.n_cipher_suites,
1338 rdev->wiphy.cipher_suites))
1339 goto nla_put_failure;
1340
1341 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1342 rdev->wiphy.max_num_pmkids))
1343 goto nla_put_failure;
1344
1345 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1346 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1347 goto nla_put_failure;
1348
1349 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1350 rdev->wiphy.available_antennas_tx) ||
1351 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1352 rdev->wiphy.available_antennas_rx))
1353 goto nla_put_failure;
1354
1355 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1356 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1357 rdev->wiphy.probe_resp_offload))
1358 goto nla_put_failure;
1359
1360 if ((rdev->wiphy.available_antennas_tx ||
1361 rdev->wiphy.available_antennas_rx) &&
1362 rdev->ops->get_antenna) {
1363 u32 tx_ant = 0, rx_ant = 0;
1364 int res;
1365 res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1366 if (!res) {
1367 if (nla_put_u32(msg,
1368 NL80211_ATTR_WIPHY_ANTENNA_TX,
1369 tx_ant) ||
1370 nla_put_u32(msg,
1371 NL80211_ATTR_WIPHY_ANTENNA_RX,
1372 rx_ant))
1373 goto nla_put_failure;
1374 }
1375 }
1376
1377 state->split_start++;
1378 if (state->split)
1379 break;
1380 case 2:
1381 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1382 rdev->wiphy.interface_modes))
1383 goto nla_put_failure;
1384 state->split_start++;
1385 if (state->split)
1386 break;
1387 case 3:
1388 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1389 if (!nl_bands)
1390 goto nla_put_failure;
1391
1392 for (band = state->band_start;
1393 band < IEEE80211_NUM_BANDS; band++) {
1394 struct ieee80211_supported_band *sband;
1395
1396 sband = rdev->wiphy.bands[band];
1397
1398 if (!sband)
1399 continue;
1400
1401 nl_band = nla_nest_start(msg, band);
1402 if (!nl_band)
1403 goto nla_put_failure;
1404
1405 switch (state->chan_start) {
1406 case 0:
1407 if (nl80211_send_band_rateinfo(msg, sband))
1408 goto nla_put_failure;
1409 state->chan_start++;
1410 if (state->split)
1411 break;
1412 default:
1413 /* add frequencies */
1414 nl_freqs = nla_nest_start(
1415 msg, NL80211_BAND_ATTR_FREQS);
1416 if (!nl_freqs)
1417 goto nla_put_failure;
1418
1419 for (i = state->chan_start - 1;
1420 i < sband->n_channels;
1421 i++) {
1422 nl_freq = nla_nest_start(msg, i);
1423 if (!nl_freq)
1424 goto nla_put_failure;
1425
1426 chan = &sband->channels[i];
1427
1428 if (nl80211_msg_put_channel(
1429 msg, chan,
1430 state->split))
1431 goto nla_put_failure;
1432
1433 nla_nest_end(msg, nl_freq);
1434 if (state->split)
1435 break;
1436 }
1437 if (i < sband->n_channels)
1438 state->chan_start = i + 2;
1439 else
1440 state->chan_start = 0;
1441 nla_nest_end(msg, nl_freqs);
1442 }
1443
1444 nla_nest_end(msg, nl_band);
1445
1446 if (state->split) {
1447 /* start again here */
1448 if (state->chan_start)
1449 band--;
1450 break;
1451 }
1452 }
1453 nla_nest_end(msg, nl_bands);
1454
1455 if (band < IEEE80211_NUM_BANDS)
1456 state->band_start = band + 1;
1457 else
1458 state->band_start = 0;
1459
1460 /* if bands & channels are done, continue outside */
1461 if (state->band_start == 0 && state->chan_start == 0)
1462 state->split_start++;
1463 if (state->split)
1464 break;
1465 case 4:
1466 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1467 if (!nl_cmds)
1468 goto nla_put_failure;
1469
1470 i = 0;
1471#define CMD(op, n) \
1472 do { \
1473 if (rdev->ops->op) { \
1474 i++; \
1475 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1476 goto nla_put_failure; \
1477 } \
1478 } while (0)
1479
1480 CMD(add_virtual_intf, NEW_INTERFACE);
1481 CMD(change_virtual_intf, SET_INTERFACE);
1482 CMD(add_key, NEW_KEY);
1483 CMD(start_ap, START_AP);
1484 CMD(add_station, NEW_STATION);
1485 CMD(add_mpath, NEW_MPATH);
1486 CMD(update_mesh_config, SET_MESH_CONFIG);
1487 CMD(change_bss, SET_BSS);
1488 CMD(auth, AUTHENTICATE);
1489 CMD(assoc, ASSOCIATE);
1490 CMD(deauth, DEAUTHENTICATE);
1491 CMD(disassoc, DISASSOCIATE);
1492 CMD(join_ibss, JOIN_IBSS);
1493 CMD(join_mesh, JOIN_MESH);
1494 CMD(set_pmksa, SET_PMKSA);
1495 CMD(del_pmksa, DEL_PMKSA);
1496 CMD(flush_pmksa, FLUSH_PMKSA);
1497 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1498 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1499 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1500 CMD(mgmt_tx, FRAME);
1501 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1502 if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1503 i++;
1504 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1505 goto nla_put_failure;
1506 }
1507 if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1508 rdev->ops->join_mesh) {
1509 i++;
1510 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1511 goto nla_put_failure;
1512 }
1513 CMD(set_wds_peer, SET_WDS_PEER);
1514 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1515 CMD(tdls_mgmt, TDLS_MGMT);
1516 CMD(tdls_oper, TDLS_OPER);
1517 }
1518 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1519 CMD(sched_scan_start, START_SCHED_SCAN);
1520 CMD(probe_client, PROBE_CLIENT);
1521 CMD(set_noack_map, SET_NOACK_MAP);
1522 if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1523 i++;
1524 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1525 goto nla_put_failure;
1526 }
1527 CMD(start_p2p_device, START_P2P_DEVICE);
1528 CMD(set_mcast_rate, SET_MCAST_RATE);
1529#ifdef CONFIG_NL80211_TESTMODE
1530 CMD(testmode_cmd, TESTMODE);
1531#endif
1532 if (state->split) {
1533 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1534 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1535 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1536 CMD(channel_switch, CHANNEL_SWITCH);
1537 CMD(set_qos_map, SET_QOS_MAP);
1538 if (rdev->wiphy.features &
1539 NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1540 CMD(add_tx_ts, ADD_TX_TS);
1541 }
1542 /* add into the if now */
1543#undef CMD
1544
1545 if (rdev->ops->connect || rdev->ops->auth) {
1546 i++;
1547 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1548 goto nla_put_failure;
1549 }
1550
1551 if (rdev->ops->disconnect || rdev->ops->deauth) {
1552 i++;
1553 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1554 goto nla_put_failure;
1555 }
1556
1557 nla_nest_end(msg, nl_cmds);
1558 state->split_start++;
1559 if (state->split)
1560 break;
1561 case 5:
1562 if (rdev->ops->remain_on_channel &&
1563 (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1564 nla_put_u32(msg,
1565 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1566 rdev->wiphy.max_remain_on_channel_duration))
1567 goto nla_put_failure;
1568
1569 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1570 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1571 goto nla_put_failure;
1572
1573 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1574 goto nla_put_failure;
1575 state->split_start++;
1576 if (state->split)
1577 break;
1578 case 6:
1579#ifdef CONFIG_PM
1580 if (nl80211_send_wowlan(msg, rdev, state->split))
1581 goto nla_put_failure;
1582 state->split_start++;
1583 if (state->split)
1584 break;
1585#else
1586 state->split_start++;
1587#endif
1588 case 7:
1589 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1590 rdev->wiphy.software_iftypes))
1591 goto nla_put_failure;
1592
1593 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1594 state->split))
1595 goto nla_put_failure;
1596
1597 state->split_start++;
1598 if (state->split)
1599 break;
1600 case 8:
1601 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1602 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1603 rdev->wiphy.ap_sme_capa))
1604 goto nla_put_failure;
1605
1606 features = rdev->wiphy.features;
1607 /*
1608 * We can only add the per-channel limit information if the
1609 * dump is split, otherwise it makes it too big. Therefore
1610 * only advertise it in that case.
1611 */
1612 if (state->split)
1613 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1614 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1615 goto nla_put_failure;
1616
1617 if (rdev->wiphy.ht_capa_mod_mask &&
1618 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1619 sizeof(*rdev->wiphy.ht_capa_mod_mask),
1620 rdev->wiphy.ht_capa_mod_mask))
1621 goto nla_put_failure;
1622
1623 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1624 rdev->wiphy.max_acl_mac_addrs &&
1625 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1626 rdev->wiphy.max_acl_mac_addrs))
1627 goto nla_put_failure;
1628
1629 /*
1630 * Any information below this point is only available to
1631 * applications that can deal with it being split. This
1632 * helps ensure that newly added capabilities don't break
1633 * older tools by overrunning their buffers.
1634 *
1635 * We still increment split_start so that in the split
1636 * case we'll continue with more data in the next round,
1637 * but break unconditionally so unsplit data stops here.
1638 */
1639 state->split_start++;
1640 break;
1641 case 9:
1642 if (rdev->wiphy.extended_capabilities &&
1643 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1644 rdev->wiphy.extended_capabilities_len,
1645 rdev->wiphy.extended_capabilities) ||
1646 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1647 rdev->wiphy.extended_capabilities_len,
1648 rdev->wiphy.extended_capabilities_mask)))
1649 goto nla_put_failure;
1650
1651 if (rdev->wiphy.vht_capa_mod_mask &&
1652 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1653 sizeof(*rdev->wiphy.vht_capa_mod_mask),
1654 rdev->wiphy.vht_capa_mod_mask))
1655 goto nla_put_failure;
1656
1657 state->split_start++;
1658 break;
1659 case 10:
1660 if (nl80211_send_coalesce(msg, rdev))
1661 goto nla_put_failure;
1662
1663 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1664 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1665 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1666 goto nla_put_failure;
1667
1668 if (rdev->wiphy.max_ap_assoc_sta &&
1669 nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1670 rdev->wiphy.max_ap_assoc_sta))
1671 goto nla_put_failure;
1672
1673 state->split_start++;
1674 break;
1675 case 11:
1676 if (rdev->wiphy.n_vendor_commands) {
1677 const struct nl80211_vendor_cmd_info *info;
1678 struct nlattr *nested;
1679
1680 nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1681 if (!nested)
1682 goto nla_put_failure;
1683
1684 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1685 info = &rdev->wiphy.vendor_commands[i].info;
1686 if (nla_put(msg, i + 1, sizeof(*info), info))
1687 goto nla_put_failure;
1688 }
1689 nla_nest_end(msg, nested);
1690 }
1691
1692 if (rdev->wiphy.n_vendor_events) {
1693 const struct nl80211_vendor_cmd_info *info;
1694 struct nlattr *nested;
1695
1696 nested = nla_nest_start(msg,
1697 NL80211_ATTR_VENDOR_EVENTS);
1698 if (!nested)
1699 goto nla_put_failure;
1700
1701 for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1702 info = &rdev->wiphy.vendor_events[i];
1703 if (nla_put(msg, i + 1, sizeof(*info), info))
1704 goto nla_put_failure;
1705 }
1706 nla_nest_end(msg, nested);
1707 }
1708 state->split_start++;
1709 break;
1710 case 12:
1711 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1712 nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1713 rdev->wiphy.max_num_csa_counters))
1714 goto nla_put_failure;
1715
1716 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1717 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1718 goto nla_put_failure;
1719
1720 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1721 sizeof(rdev->wiphy.ext_features),
1722 rdev->wiphy.ext_features))
1723 goto nla_put_failure;
1724
1725 /* done */
1726 state->split_start = 0;
1727 break;
1728 }
1729 finish:
1730 genlmsg_end(msg, hdr);
1731 return 0;
1732
1733 nla_put_failure:
1734 genlmsg_cancel(msg, hdr);
1735 return -EMSGSIZE;
1736}
1737
1738static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1739 struct netlink_callback *cb,
1740 struct nl80211_dump_wiphy_state *state)
1741{
1742 struct nlattr **tb = nl80211_fam.attrbuf;
1743 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1744 tb, nl80211_fam.maxattr, nl80211_policy);
1745 /* ignore parse errors for backward compatibility */
1746 if (ret)
1747 return 0;
1748
1749 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1750 if (tb[NL80211_ATTR_WIPHY])
1751 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1752 if (tb[NL80211_ATTR_WDEV])
1753 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1754 if (tb[NL80211_ATTR_IFINDEX]) {
1755 struct net_device *netdev;
1756 struct cfg80211_registered_device *rdev;
1757 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1758
1759 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1760 if (!netdev)
1761 return -ENODEV;
1762 if (netdev->ieee80211_ptr) {
1763 rdev = wiphy_to_rdev(
1764 netdev->ieee80211_ptr->wiphy);
1765 state->filter_wiphy = rdev->wiphy_idx;
1766 }
1767 }
1768
1769 return 0;
1770}
1771
1772static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1773{
1774 int idx = 0, ret;
1775 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1776 struct cfg80211_registered_device *rdev;
1777
1778 rtnl_lock();
1779 if (!state) {
1780 state = kzalloc(sizeof(*state), GFP_KERNEL);
1781 if (!state) {
1782 rtnl_unlock();
1783 return -ENOMEM;
1784 }
1785 state->filter_wiphy = -1;
1786 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1787 if (ret) {
1788 kfree(state);
1789 rtnl_unlock();
1790 return ret;
1791 }
1792 cb->args[0] = (long)state;
1793 }
1794
1795 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1796 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1797 continue;
1798 if (++idx <= state->start)
1799 continue;
1800 if (state->filter_wiphy != -1 &&
1801 state->filter_wiphy != rdev->wiphy_idx)
1802 continue;
1803 /* attempt to fit multiple wiphy data chunks into the skb */
1804 do {
1805 ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
1806 skb,
1807 NETLINK_CB(cb->skb).portid,
1808 cb->nlh->nlmsg_seq,
1809 NLM_F_MULTI, state);
1810 if (ret < 0) {
1811 /*
1812 * If sending the wiphy data didn't fit (ENOBUFS
1813 * or EMSGSIZE returned), this SKB is still
1814 * empty (so it's not too big because another
1815 * wiphy dataset is already in the skb) and
1816 * we've not tried to adjust the dump allocation
1817 * yet ... then adjust the alloc size to be
1818 * bigger, and return 1 but with the empty skb.
1819 * This results in an empty message being RX'ed
1820 * in userspace, but that is ignored.
1821 *
1822 * We can then retry with the larger buffer.
1823 */
1824 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1825 !skb->len && !state->split &&
1826 cb->min_dump_alloc < 4096) {
1827 cb->min_dump_alloc = 4096;
1828 state->split_start = 0;
1829 rtnl_unlock();
1830 return 1;
1831 }
1832 idx--;
1833 break;
1834 }
1835 } while (state->split_start > 0);
1836 break;
1837 }
1838 rtnl_unlock();
1839
1840 state->start = idx;
1841
1842 return skb->len;
1843}
1844
1845static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1846{
1847 kfree((void *)cb->args[0]);
1848 return 0;
1849}
1850
1851static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1852{
1853 struct sk_buff *msg;
1854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1855 struct nl80211_dump_wiphy_state state = {};
1856
1857 msg = nlmsg_new(4096, GFP_KERNEL);
1858 if (!msg)
1859 return -ENOMEM;
1860
1861 if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
1862 info->snd_portid, info->snd_seq, 0,
1863 &state) < 0) {
1864 nlmsg_free(msg);
1865 return -ENOBUFS;
1866 }
1867
1868 return genlmsg_reply(msg, info);
1869}
1870
1871static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1872 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1873 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1874 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1875 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1876 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1877};
1878
1879static int parse_txq_params(struct nlattr *tb[],
1880 struct ieee80211_txq_params *txq_params)
1881{
1882 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1883 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1884 !tb[NL80211_TXQ_ATTR_AIFS])
1885 return -EINVAL;
1886
1887 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
1888 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1889 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1890 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1891 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1892
1893 if (txq_params->ac >= NL80211_NUM_ACS)
1894 return -EINVAL;
1895
1896 return 0;
1897}
1898
1899static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1900{
1901 /*
1902 * You can only set the channel explicitly for WDS interfaces,
1903 * all others have their channel managed via their respective
1904 * "establish a connection" command (connect, join, ...)
1905 *
1906 * For AP/GO and mesh mode, the channel can be set with the
1907 * channel userspace API, but is only stored and passed to the
1908 * low-level driver when the AP starts or the mesh is joined.
1909 * This is for backward compatibility, userspace can also give
1910 * the channel in the start-ap or join-mesh commands instead.
1911 *
1912 * Monitors are special as they are normally slaved to
1913 * whatever else is going on, so they have their own special
1914 * operation to set the monitor channel if possible.
1915 */
1916 return !wdev ||
1917 wdev->iftype == NL80211_IFTYPE_AP ||
1918 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1919 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1920 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1921}
1922
1923static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1924 struct genl_info *info,
1925 struct cfg80211_chan_def *chandef)
1926{
1927 u32 control_freq;
1928
1929 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1930 return -EINVAL;
1931
1932 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1933
1934 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
1935 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1936 chandef->center_freq1 = control_freq;
1937 chandef->center_freq2 = 0;
1938
1939 /* Primary channel not allowed */
1940 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1941 return -EINVAL;
1942
1943 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1944 enum nl80211_channel_type chantype;
1945
1946 chantype = nla_get_u32(
1947 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1948
1949 switch (chantype) {
1950 case NL80211_CHAN_NO_HT:
1951 case NL80211_CHAN_HT20:
1952 case NL80211_CHAN_HT40PLUS:
1953 case NL80211_CHAN_HT40MINUS:
1954 cfg80211_chandef_create(chandef, chandef->chan,
1955 chantype);
1956 break;
1957 default:
1958 return -EINVAL;
1959 }
1960 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1961 chandef->width =
1962 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1963 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1964 chandef->center_freq1 =
1965 nla_get_u32(
1966 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1967 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1968 chandef->center_freq2 =
1969 nla_get_u32(
1970 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1971 }
1972
1973 if (!cfg80211_chandef_valid(chandef))
1974 return -EINVAL;
1975
1976 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1977 IEEE80211_CHAN_DISABLED))
1978 return -EINVAL;
1979
1980 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1981 chandef->width == NL80211_CHAN_WIDTH_10) &&
1982 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1983 return -EINVAL;
1984
1985 return 0;
1986}
1987
1988static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1989 struct net_device *dev,
1990 struct genl_info *info)
1991{
1992 struct cfg80211_chan_def chandef;
1993 int result;
1994 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1995 struct wireless_dev *wdev = NULL;
1996
1997 if (dev)
1998 wdev = dev->ieee80211_ptr;
1999 if (!nl80211_can_set_dev_channel(wdev))
2000 return -EOPNOTSUPP;
2001 if (wdev)
2002 iftype = wdev->iftype;
2003
2004 result = nl80211_parse_chandef(rdev, info, &chandef);
2005 if (result)
2006 return result;
2007
2008 switch (iftype) {
2009 case NL80211_IFTYPE_AP:
2010 case NL80211_IFTYPE_P2P_GO:
2011 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2012 iftype)) {
2013 result = -EINVAL;
2014 break;
2015 }
2016 if (wdev->beacon_interval) {
2017 if (!dev || !rdev->ops->set_ap_chanwidth ||
2018 !(rdev->wiphy.features &
2019 NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2020 result = -EBUSY;
2021 break;
2022 }
2023
2024 /* Only allow dynamic channel width changes */
2025 if (chandef.chan != wdev->preset_chandef.chan) {
2026 result = -EBUSY;
2027 break;
2028 }
2029 result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2030 if (result)
2031 break;
2032 }
2033 wdev->preset_chandef = chandef;
2034 result = 0;
2035 break;
2036 case NL80211_IFTYPE_MESH_POINT:
2037 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2038 break;
2039 case NL80211_IFTYPE_MONITOR:
2040 result = cfg80211_set_monitor_channel(rdev, &chandef);
2041 break;
2042 default:
2043 result = -EINVAL;
2044 }
2045
2046 return result;
2047}
2048
2049static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2050{
2051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2052 struct net_device *netdev = info->user_ptr[1];
2053
2054 return __nl80211_set_channel(rdev, netdev, info);
2055}
2056
2057static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2058{
2059 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2060 struct net_device *dev = info->user_ptr[1];
2061 struct wireless_dev *wdev = dev->ieee80211_ptr;
2062 const u8 *bssid;
2063
2064 if (!info->attrs[NL80211_ATTR_MAC])
2065 return -EINVAL;
2066
2067 if (netif_running(dev))
2068 return -EBUSY;
2069
2070 if (!rdev->ops->set_wds_peer)
2071 return -EOPNOTSUPP;
2072
2073 if (wdev->iftype != NL80211_IFTYPE_WDS)
2074 return -EOPNOTSUPP;
2075
2076 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2077 return rdev_set_wds_peer(rdev, dev, bssid);
2078}
2079
2080
2081static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2082{
2083 struct cfg80211_registered_device *rdev;
2084 struct net_device *netdev = NULL;
2085 struct wireless_dev *wdev;
2086 int result = 0, rem_txq_params = 0;
2087 struct nlattr *nl_txq_params;
2088 u32 changed;
2089 u8 retry_short = 0, retry_long = 0;
2090 u32 frag_threshold = 0, rts_threshold = 0;
2091 u8 coverage_class = 0;
2092
2093 ASSERT_RTNL();
2094
2095 /*
2096 * Try to find the wiphy and netdev. Normally this
2097 * function shouldn't need the netdev, but this is
2098 * done for backward compatibility -- previously
2099 * setting the channel was done per wiphy, but now
2100 * it is per netdev. Previous userland like hostapd
2101 * also passed a netdev to set_wiphy, so that it is
2102 * possible to let that go to the right netdev!
2103 */
2104
2105 if (info->attrs[NL80211_ATTR_IFINDEX]) {
2106 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2107
2108 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2109 if (netdev && netdev->ieee80211_ptr)
2110 rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2111 else
2112 netdev = NULL;
2113 }
2114
2115 if (!netdev) {
2116 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2117 info->attrs);
2118 if (IS_ERR(rdev))
2119 return PTR_ERR(rdev);
2120 wdev = NULL;
2121 netdev = NULL;
2122 result = 0;
2123 } else
2124 wdev = netdev->ieee80211_ptr;
2125
2126 /*
2127 * end workaround code, by now the rdev is available
2128 * and locked, and wdev may or may not be NULL.
2129 */
2130
2131 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2132 result = cfg80211_dev_rename(
2133 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2134
2135 if (result)
2136 return result;
2137
2138 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2139 struct ieee80211_txq_params txq_params;
2140 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2141
2142 if (!rdev->ops->set_txq_params)
2143 return -EOPNOTSUPP;
2144
2145 if (!netdev)
2146 return -EINVAL;
2147
2148 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2149 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2150 return -EINVAL;
2151
2152 if (!netif_running(netdev))
2153 return -ENETDOWN;
2154
2155 nla_for_each_nested(nl_txq_params,
2156 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2157 rem_txq_params) {
2158 result = nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2159 nla_data(nl_txq_params),
2160 nla_len(nl_txq_params),
2161 txq_params_policy);
2162 if (result)
2163 return result;
2164 result = parse_txq_params(tb, &txq_params);
2165 if (result)
2166 return result;
2167
2168 result = rdev_set_txq_params(rdev, netdev,
2169 &txq_params);
2170 if (result)
2171 return result;
2172 }
2173 }
2174
2175 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2176 result = __nl80211_set_channel(
2177 rdev,
2178 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2179 info);
2180 if (result)
2181 return result;
2182 }
2183
2184 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2185 struct wireless_dev *txp_wdev = wdev;
2186 enum nl80211_tx_power_setting type;
2187 int idx, mbm = 0;
2188
2189 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2190 txp_wdev = NULL;
2191
2192 if (!rdev->ops->set_tx_power)
2193 return -EOPNOTSUPP;
2194
2195 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2196 type = nla_get_u32(info->attrs[idx]);
2197
2198 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2199 (type != NL80211_TX_POWER_AUTOMATIC))
2200 return -EINVAL;
2201
2202 if (type != NL80211_TX_POWER_AUTOMATIC) {
2203 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2204 mbm = nla_get_u32(info->attrs[idx]);
2205 }
2206
2207 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2208 if (result)
2209 return result;
2210 }
2211
2212 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2213 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2214 u32 tx_ant, rx_ant;
2215 if ((!rdev->wiphy.available_antennas_tx &&
2216 !rdev->wiphy.available_antennas_rx) ||
2217 !rdev->ops->set_antenna)
2218 return -EOPNOTSUPP;
2219
2220 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2221 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2222
2223 /* reject antenna configurations which don't match the
2224 * available antenna masks, except for the "all" mask */
2225 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2226 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2227 return -EINVAL;
2228
2229 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2230 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2231
2232 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2233 if (result)
2234 return result;
2235 }
2236
2237 changed = 0;
2238
2239 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2240 retry_short = nla_get_u8(
2241 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2242 if (retry_short == 0)
2243 return -EINVAL;
2244
2245 changed |= WIPHY_PARAM_RETRY_SHORT;
2246 }
2247
2248 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2249 retry_long = nla_get_u8(
2250 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2251 if (retry_long == 0)
2252 return -EINVAL;
2253
2254 changed |= WIPHY_PARAM_RETRY_LONG;
2255 }
2256
2257 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2258 frag_threshold = nla_get_u32(
2259 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2260 if (frag_threshold < 256)
2261 return -EINVAL;
2262
2263 if (frag_threshold != (u32) -1) {
2264 /*
2265 * Fragments (apart from the last one) are required to
2266 * have even length. Make the fragmentation code
2267 * simpler by stripping LSB should someone try to use
2268 * odd threshold value.
2269 */
2270 frag_threshold &= ~0x1;
2271 }
2272 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2273 }
2274
2275 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2276 rts_threshold = nla_get_u32(
2277 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2278 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2279 }
2280
2281 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2282 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2283 return -EINVAL;
2284
2285 coverage_class = nla_get_u8(
2286 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2287 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2288 }
2289
2290 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2291 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2292 return -EOPNOTSUPP;
2293
2294 changed |= WIPHY_PARAM_DYN_ACK;
2295 }
2296
2297 if (changed) {
2298 u8 old_retry_short, old_retry_long;
2299 u32 old_frag_threshold, old_rts_threshold;
2300 u8 old_coverage_class;
2301
2302 if (!rdev->ops->set_wiphy_params)
2303 return -EOPNOTSUPP;
2304
2305 old_retry_short = rdev->wiphy.retry_short;
2306 old_retry_long = rdev->wiphy.retry_long;
2307 old_frag_threshold = rdev->wiphy.frag_threshold;
2308 old_rts_threshold = rdev->wiphy.rts_threshold;
2309 old_coverage_class = rdev->wiphy.coverage_class;
2310
2311 if (changed & WIPHY_PARAM_RETRY_SHORT)
2312 rdev->wiphy.retry_short = retry_short;
2313 if (changed & WIPHY_PARAM_RETRY_LONG)
2314 rdev->wiphy.retry_long = retry_long;
2315 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2316 rdev->wiphy.frag_threshold = frag_threshold;
2317 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2318 rdev->wiphy.rts_threshold = rts_threshold;
2319 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2320 rdev->wiphy.coverage_class = coverage_class;
2321
2322 result = rdev_set_wiphy_params(rdev, changed);
2323 if (result) {
2324 rdev->wiphy.retry_short = old_retry_short;
2325 rdev->wiphy.retry_long = old_retry_long;
2326 rdev->wiphy.frag_threshold = old_frag_threshold;
2327 rdev->wiphy.rts_threshold = old_rts_threshold;
2328 rdev->wiphy.coverage_class = old_coverage_class;
2329 return result;
2330 }
2331 }
2332 return 0;
2333}
2334
2335static inline u64 wdev_id(struct wireless_dev *wdev)
2336{
2337 return (u64)wdev->identifier |
2338 ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2339}
2340
2341static int nl80211_send_chandef(struct sk_buff *msg,
2342 const struct cfg80211_chan_def *chandef)
2343{
2344 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2345 return -EINVAL;
2346
2347 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2348 chandef->chan->center_freq))
2349 return -ENOBUFS;
2350 switch (chandef->width) {
2351 case NL80211_CHAN_WIDTH_20_NOHT:
2352 case NL80211_CHAN_WIDTH_20:
2353 case NL80211_CHAN_WIDTH_40:
2354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2355 cfg80211_get_chandef_type(chandef)))
2356 return -ENOBUFS;
2357 break;
2358 default:
2359 break;
2360 }
2361 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2362 return -ENOBUFS;
2363 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2364 return -ENOBUFS;
2365 if (chandef->center_freq2 &&
2366 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2367 return -ENOBUFS;
2368 return 0;
2369}
2370
2371static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2372 struct cfg80211_registered_device *rdev,
2373 struct wireless_dev *wdev, bool removal)
2374{
2375 struct net_device *dev = wdev->netdev;
2376 u8 cmd = NL80211_CMD_NEW_INTERFACE;
2377 void *hdr;
2378
2379 if (removal)
2380 cmd = NL80211_CMD_DEL_INTERFACE;
2381
2382 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2383 if (!hdr)
2384 return -1;
2385
2386 if (dev &&
2387 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2388 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2389 goto nla_put_failure;
2390
2391 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2392 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2393 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
2394 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2395 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2396 rdev->devlist_generation ^
2397 (cfg80211_rdev_list_generation << 2)))
2398 goto nla_put_failure;
2399
2400 if (rdev->ops->get_channel) {
2401 int ret;
2402 struct cfg80211_chan_def chandef;
2403
2404 ret = rdev_get_channel(rdev, wdev, &chandef);
2405 if (ret == 0) {
2406 if (nl80211_send_chandef(msg, &chandef))
2407 goto nla_put_failure;
2408 }
2409 }
2410
2411 if (rdev->ops->get_tx_power) {
2412 int dbm, ret;
2413
2414 ret = rdev_get_tx_power(rdev, wdev, &dbm);
2415 if (ret == 0 &&
2416 nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2417 DBM_TO_MBM(dbm)))
2418 goto nla_put_failure;
2419 }
2420
2421 if (wdev->ssid_len) {
2422 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2423 goto nla_put_failure;
2424 }
2425
2426 genlmsg_end(msg, hdr);
2427 return 0;
2428
2429 nla_put_failure:
2430 genlmsg_cancel(msg, hdr);
2431 return -EMSGSIZE;
2432}
2433
2434static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2435{
2436 int wp_idx = 0;
2437 int if_idx = 0;
2438 int wp_start = cb->args[0];
2439 int if_start = cb->args[1];
2440 struct cfg80211_registered_device *rdev;
2441 struct wireless_dev *wdev;
2442
2443 rtnl_lock();
2444 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2445 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2446 continue;
2447 if (wp_idx < wp_start) {
2448 wp_idx++;
2449 continue;
2450 }
2451 if_idx = 0;
2452
2453 list_for_each_entry(wdev, &rdev->wdev_list, list) {
2454 if (if_idx < if_start) {
2455 if_idx++;
2456 continue;
2457 }
2458 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2459 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2460 rdev, wdev, false) < 0) {
2461 goto out;
2462 }
2463 if_idx++;
2464 }
2465
2466 wp_idx++;
2467 }
2468 out:
2469 rtnl_unlock();
2470
2471 cb->args[0] = wp_idx;
2472 cb->args[1] = if_idx;
2473
2474 return skb->len;
2475}
2476
2477static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2478{
2479 struct sk_buff *msg;
2480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2481 struct wireless_dev *wdev = info->user_ptr[1];
2482
2483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2484 if (!msg)
2485 return -ENOMEM;
2486
2487 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2488 rdev, wdev, false) < 0) {
2489 nlmsg_free(msg);
2490 return -ENOBUFS;
2491 }
2492
2493 return genlmsg_reply(msg, info);
2494}
2495
2496static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2497 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2498 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2499 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2500 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2501 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2502 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2503};
2504
2505static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2506{
2507 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2508 int flag;
2509
2510 *mntrflags = 0;
2511
2512 if (!nla)
2513 return -EINVAL;
2514
2515 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2516 nla, mntr_flags_policy))
2517 return -EINVAL;
2518
2519 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2520 if (flags[flag])
2521 *mntrflags |= (1<<flag);
2522
2523 return 0;
2524}
2525
2526static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2527 struct net_device *netdev, u8 use_4addr,
2528 enum nl80211_iftype iftype)
2529{
2530 if (!use_4addr) {
2531 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2532 return -EBUSY;
2533 return 0;
2534 }
2535
2536 switch (iftype) {
2537 case NL80211_IFTYPE_AP_VLAN:
2538 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2539 return 0;
2540 break;
2541 case NL80211_IFTYPE_STATION:
2542 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2543 return 0;
2544 break;
2545 default:
2546 break;
2547 }
2548
2549 return -EOPNOTSUPP;
2550}
2551
2552static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2553{
2554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2555 struct vif_params params;
2556 int err;
2557 enum nl80211_iftype otype, ntype;
2558 struct net_device *dev = info->user_ptr[1];
2559 u32 _flags, *flags = NULL;
2560 bool change = false;
2561
2562 memset(&params, 0, sizeof(params));
2563
2564 otype = ntype = dev->ieee80211_ptr->iftype;
2565
2566 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2567 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2568 if (otype != ntype)
2569 change = true;
2570 if (ntype > NL80211_IFTYPE_MAX)
2571 return -EINVAL;
2572 }
2573
2574 if (info->attrs[NL80211_ATTR_MESH_ID]) {
2575 struct wireless_dev *wdev = dev->ieee80211_ptr;
2576
2577 if (ntype != NL80211_IFTYPE_MESH_POINT)
2578 return -EINVAL;
2579 if (netif_running(dev))
2580 return -EBUSY;
2581
2582 wdev_lock(wdev);
2583 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2584 IEEE80211_MAX_MESH_ID_LEN);
2585 wdev->mesh_id_up_len =
2586 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2587 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2588 wdev->mesh_id_up_len);
2589 wdev_unlock(wdev);
2590 }
2591
2592 if (info->attrs[NL80211_ATTR_4ADDR]) {
2593 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2594 change = true;
2595 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2596 if (err)
2597 return err;
2598 } else {
2599 params.use_4addr = -1;
2600 }
2601
2602 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2603 if (ntype != NL80211_IFTYPE_MONITOR)
2604 return -EINVAL;
2605 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2606 &_flags);
2607 if (err)
2608 return err;
2609
2610 flags = &_flags;
2611 change = true;
2612 }
2613
2614 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
2615 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2616 return -EOPNOTSUPP;
2617
2618 if (change)
2619 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
2620 else
2621 err = 0;
2622
2623 if (!err && params.use_4addr != -1)
2624 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2625
2626 return err;
2627}
2628
2629static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2630{
2631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2632 struct vif_params params;
2633 struct wireless_dev *wdev;
2634 struct sk_buff *msg, *event;
2635 int err;
2636 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2637 u32 flags;
2638
2639 /* to avoid failing a new interface creation due to pending removal */
2640 cfg80211_destroy_ifaces(rdev);
2641
2642 memset(&params, 0, sizeof(params));
2643
2644 if (!info->attrs[NL80211_ATTR_IFNAME])
2645 return -EINVAL;
2646
2647 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2648 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2649 if (type > NL80211_IFTYPE_MAX)
2650 return -EINVAL;
2651 }
2652
2653 if (!rdev->ops->add_virtual_intf ||
2654 !(rdev->wiphy.interface_modes & (1 << type)))
2655 return -EOPNOTSUPP;
2656
2657 if ((type == NL80211_IFTYPE_P2P_DEVICE ||
2658 rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2659 info->attrs[NL80211_ATTR_MAC]) {
2660 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2661 ETH_ALEN);
2662 if (!is_valid_ether_addr(params.macaddr))
2663 return -EADDRNOTAVAIL;
2664 }
2665
2666 if (info->attrs[NL80211_ATTR_4ADDR]) {
2667 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2668 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2669 if (err)
2670 return err;
2671 }
2672
2673 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2674 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2675 &flags);
2676
2677 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
2678 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2679 return -EOPNOTSUPP;
2680
2681 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2682 if (!msg)
2683 return -ENOMEM;
2684
2685 wdev = rdev_add_virtual_intf(rdev,
2686 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2687 NET_NAME_USER, type, err ? NULL : &flags,
2688 &params);
2689 if (WARN_ON(!wdev)) {
2690 nlmsg_free(msg);
2691 return -EPROTO;
2692 } else if (IS_ERR(wdev)) {
2693 nlmsg_free(msg);
2694 return PTR_ERR(wdev);
2695 }
2696
2697 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
2698 wdev->owner_nlportid = info->snd_portid;
2699
2700 switch (type) {
2701 case NL80211_IFTYPE_MESH_POINT:
2702 if (!info->attrs[NL80211_ATTR_MESH_ID])
2703 break;
2704 wdev_lock(wdev);
2705 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2706 IEEE80211_MAX_MESH_ID_LEN);
2707 wdev->mesh_id_up_len =
2708 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2709 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2710 wdev->mesh_id_up_len);
2711 wdev_unlock(wdev);
2712 break;
2713 case NL80211_IFTYPE_P2P_DEVICE:
2714 /*
2715 * P2P Device doesn't have a netdev, so doesn't go
2716 * through the netdev notifier and must be added here
2717 */
2718 mutex_init(&wdev->mtx);
2719 INIT_LIST_HEAD(&wdev->event_list);
2720 spin_lock_init(&wdev->event_lock);
2721 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2722 spin_lock_init(&wdev->mgmt_registrations_lock);
2723
2724 wdev->identifier = ++rdev->wdev_id;
2725 list_add_rcu(&wdev->list, &rdev->wdev_list);
2726 rdev->devlist_generation++;
2727 break;
2728 default:
2729 break;
2730 }
2731
2732 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2733 rdev, wdev, false) < 0) {
2734 nlmsg_free(msg);
2735 return -ENOBUFS;
2736 }
2737
2738 event = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2739 if (event) {
2740 if (nl80211_send_iface(event, 0, 0, 0,
2741 rdev, wdev, false) < 0) {
2742 nlmsg_free(event);
2743 goto out;
2744 }
2745
2746 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2747 event, 0, NL80211_MCGRP_CONFIG,
2748 GFP_KERNEL);
2749 }
2750
2751out:
2752 return genlmsg_reply(msg, info);
2753}
2754
2755static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2756{
2757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2758 struct wireless_dev *wdev = info->user_ptr[1];
2759 struct sk_buff *msg;
2760 int status;
2761
2762 if (!rdev->ops->del_virtual_intf)
2763 return -EOPNOTSUPP;
2764
2765 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2766 if (msg && nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, true) < 0) {
2767 nlmsg_free(msg);
2768 msg = NULL;
2769 }
2770
2771 /*
2772 * If we remove a wireless device without a netdev then clear
2773 * user_ptr[1] so that nl80211_post_doit won't dereference it
2774 * to check if it needs to do dev_put(). Otherwise it crashes
2775 * since the wdev has been freed, unlike with a netdev where
2776 * we need the dev_put() for the netdev to really be freed.
2777 */
2778 if (!wdev->netdev)
2779 info->user_ptr[1] = NULL;
2780
2781 status = rdev_del_virtual_intf(rdev, wdev);
2782 if (status >= 0 && msg)
2783 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
2784 msg, 0, NL80211_MCGRP_CONFIG,
2785 GFP_KERNEL);
2786 else
2787 nlmsg_free(msg);
2788
2789 return status;
2790}
2791
2792static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2793{
2794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2795 struct net_device *dev = info->user_ptr[1];
2796 u16 noack_map;
2797
2798 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2799 return -EINVAL;
2800
2801 if (!rdev->ops->set_noack_map)
2802 return -EOPNOTSUPP;
2803
2804 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2805
2806 return rdev_set_noack_map(rdev, dev, noack_map);
2807}
2808
2809struct get_key_cookie {
2810 struct sk_buff *msg;
2811 int error;
2812 int idx;
2813};
2814
2815static void get_key_callback(void *c, struct key_params *params)
2816{
2817 struct nlattr *key;
2818 struct get_key_cookie *cookie = c;
2819
2820 if ((params->key &&
2821 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2822 params->key_len, params->key)) ||
2823 (params->seq &&
2824 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2825 params->seq_len, params->seq)) ||
2826 (params->cipher &&
2827 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2828 params->cipher)))
2829 goto nla_put_failure;
2830
2831 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2832 if (!key)
2833 goto nla_put_failure;
2834
2835 if ((params->key &&
2836 nla_put(cookie->msg, NL80211_KEY_DATA,
2837 params->key_len, params->key)) ||
2838 (params->seq &&
2839 nla_put(cookie->msg, NL80211_KEY_SEQ,
2840 params->seq_len, params->seq)) ||
2841 (params->cipher &&
2842 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2843 params->cipher)))
2844 goto nla_put_failure;
2845
2846 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2847 goto nla_put_failure;
2848
2849 nla_nest_end(cookie->msg, key);
2850
2851 return;
2852 nla_put_failure:
2853 cookie->error = 1;
2854}
2855
2856static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2857{
2858 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2859 int err;
2860 struct net_device *dev = info->user_ptr[1];
2861 u8 key_idx = 0;
2862 const u8 *mac_addr = NULL;
2863 bool pairwise;
2864 struct get_key_cookie cookie = {
2865 .error = 0,
2866 };
2867 void *hdr;
2868 struct sk_buff *msg;
2869
2870 if (info->attrs[NL80211_ATTR_KEY_IDX])
2871 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2872
2873 if (key_idx > 5)
2874 return -EINVAL;
2875
2876 if (info->attrs[NL80211_ATTR_MAC])
2877 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2878
2879 pairwise = !!mac_addr;
2880 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2881 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2882 if (kt >= NUM_NL80211_KEYTYPES)
2883 return -EINVAL;
2884 if (kt != NL80211_KEYTYPE_GROUP &&
2885 kt != NL80211_KEYTYPE_PAIRWISE)
2886 return -EINVAL;
2887 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2888 }
2889
2890 if (!rdev->ops->get_key)
2891 return -EOPNOTSUPP;
2892
2893 if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2894 return -ENOENT;
2895
2896 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2897 if (!msg)
2898 return -ENOMEM;
2899
2900 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
2901 NL80211_CMD_NEW_KEY);
2902 if (!hdr)
2903 goto nla_put_failure;
2904
2905 cookie.msg = msg;
2906 cookie.idx = key_idx;
2907
2908 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2909 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2910 goto nla_put_failure;
2911 if (mac_addr &&
2912 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2913 goto nla_put_failure;
2914
2915 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2916 get_key_callback);
2917
2918 if (err)
2919 goto free_msg;
2920
2921 if (cookie.error)
2922 goto nla_put_failure;
2923
2924 genlmsg_end(msg, hdr);
2925 return genlmsg_reply(msg, info);
2926
2927 nla_put_failure:
2928 err = -ENOBUFS;
2929 free_msg:
2930 nlmsg_free(msg);
2931 return err;
2932}
2933
2934static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2935{
2936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2937 struct key_parse key;
2938 int err;
2939 struct net_device *dev = info->user_ptr[1];
2940
2941 err = nl80211_parse_key(info, &key);
2942 if (err)
2943 return err;
2944
2945 if (key.idx < 0)
2946 return -EINVAL;
2947
2948 /* only support setting default key */
2949 if (!key.def && !key.defmgmt)
2950 return -EINVAL;
2951
2952 wdev_lock(dev->ieee80211_ptr);
2953
2954 if (key.def) {
2955 if (!rdev->ops->set_default_key) {
2956 err = -EOPNOTSUPP;
2957 goto out;
2958 }
2959
2960 err = nl80211_key_allowed(dev->ieee80211_ptr);
2961 if (err)
2962 goto out;
2963
2964 err = rdev_set_default_key(rdev, dev, key.idx,
2965 key.def_uni, key.def_multi);
2966
2967 if (err)
2968 goto out;
2969
2970#ifdef CONFIG_CFG80211_WEXT
2971 dev->ieee80211_ptr->wext.default_key = key.idx;
2972#endif
2973 } else {
2974 if (key.def_uni || !key.def_multi) {
2975 err = -EINVAL;
2976 goto out;
2977 }
2978
2979 if (!rdev->ops->set_default_mgmt_key) {
2980 err = -EOPNOTSUPP;
2981 goto out;
2982 }
2983
2984 err = nl80211_key_allowed(dev->ieee80211_ptr);
2985 if (err)
2986 goto out;
2987
2988 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
2989 if (err)
2990 goto out;
2991
2992#ifdef CONFIG_CFG80211_WEXT
2993 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2994#endif
2995 }
2996
2997 out:
2998 wdev_unlock(dev->ieee80211_ptr);
2999
3000 return err;
3001}
3002
3003static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3004{
3005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3006 int err;
3007 struct net_device *dev = info->user_ptr[1];
3008 struct key_parse key;
3009 const u8 *mac_addr = NULL;
3010
3011 err = nl80211_parse_key(info, &key);
3012 if (err)
3013 return err;
3014
3015 if (!key.p.key)
3016 return -EINVAL;
3017
3018 if (info->attrs[NL80211_ATTR_MAC])
3019 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3020
3021 if (key.type == -1) {
3022 if (mac_addr)
3023 key.type = NL80211_KEYTYPE_PAIRWISE;
3024 else
3025 key.type = NL80211_KEYTYPE_GROUP;
3026 }
3027
3028 /* for now */
3029 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3030 key.type != NL80211_KEYTYPE_GROUP)
3031 return -EINVAL;
3032
3033 if (!rdev->ops->add_key)
3034 return -EOPNOTSUPP;
3035
3036 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3037 key.type == NL80211_KEYTYPE_PAIRWISE,
3038 mac_addr))
3039 return -EINVAL;
3040
3041 wdev_lock(dev->ieee80211_ptr);
3042 err = nl80211_key_allowed(dev->ieee80211_ptr);
3043 if (!err)
3044 err = rdev_add_key(rdev, dev, key.idx,
3045 key.type == NL80211_KEYTYPE_PAIRWISE,
3046 mac_addr, &key.p);
3047 wdev_unlock(dev->ieee80211_ptr);
3048
3049 return err;
3050}
3051
3052static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3053{
3054 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3055 int err;
3056 struct net_device *dev = info->user_ptr[1];
3057 u8 *mac_addr = NULL;
3058 struct key_parse key;
3059
3060 err = nl80211_parse_key(info, &key);
3061 if (err)
3062 return err;
3063
3064 if (info->attrs[NL80211_ATTR_MAC])
3065 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3066
3067 if (key.type == -1) {
3068 if (mac_addr)
3069 key.type = NL80211_KEYTYPE_PAIRWISE;
3070 else
3071 key.type = NL80211_KEYTYPE_GROUP;
3072 }
3073
3074 /* for now */
3075 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3076 key.type != NL80211_KEYTYPE_GROUP)
3077 return -EINVAL;
3078
3079 if (!rdev->ops->del_key)
3080 return -EOPNOTSUPP;
3081
3082 wdev_lock(dev->ieee80211_ptr);
3083 err = nl80211_key_allowed(dev->ieee80211_ptr);
3084
3085 if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3086 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3087 err = -ENOENT;
3088
3089 if (!err)
3090 err = rdev_del_key(rdev, dev, key.idx,
3091 key.type == NL80211_KEYTYPE_PAIRWISE,
3092 mac_addr);
3093
3094#ifdef CONFIG_CFG80211_WEXT
3095 if (!err) {
3096 if (key.idx == dev->ieee80211_ptr->wext.default_key)
3097 dev->ieee80211_ptr->wext.default_key = -1;
3098 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3099 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3100 }
3101#endif
3102 wdev_unlock(dev->ieee80211_ptr);
3103
3104 return err;
3105}
3106
3107/* This function returns an error or the number of nested attributes */
3108static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3109{
3110 struct nlattr *attr;
3111 int n_entries = 0, tmp;
3112
3113 nla_for_each_nested(attr, nl_attr, tmp) {
3114 if (nla_len(attr) != ETH_ALEN)
3115 return -EINVAL;
3116
3117 n_entries++;
3118 }
3119
3120 return n_entries;
3121}
3122
3123/*
3124 * This function parses ACL information and allocates memory for ACL data.
3125 * On successful return, the calling function is responsible to free the
3126 * ACL buffer returned by this function.
3127 */
3128static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3129 struct genl_info *info)
3130{
3131 enum nl80211_acl_policy acl_policy;
3132 struct nlattr *attr;
3133 struct cfg80211_acl_data *acl;
3134 int i = 0, n_entries, tmp;
3135
3136 if (!wiphy->max_acl_mac_addrs)
3137 return ERR_PTR(-EOPNOTSUPP);
3138
3139 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3140 return ERR_PTR(-EINVAL);
3141
3142 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3143 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3144 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3145 return ERR_PTR(-EINVAL);
3146
3147 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3148 return ERR_PTR(-EINVAL);
3149
3150 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3151 if (n_entries < 0)
3152 return ERR_PTR(n_entries);
3153
3154 if (n_entries > wiphy->max_acl_mac_addrs)
3155 return ERR_PTR(-ENOTSUPP);
3156
3157 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3158 GFP_KERNEL);
3159 if (!acl)
3160 return ERR_PTR(-ENOMEM);
3161
3162 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3163 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3164 i++;
3165 }
3166
3167 acl->n_acl_entries = n_entries;
3168 acl->acl_policy = acl_policy;
3169
3170 return acl;
3171}
3172
3173static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3174{
3175 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3176 struct net_device *dev = info->user_ptr[1];
3177 struct cfg80211_acl_data *acl;
3178 int err;
3179
3180 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3181 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3182 return -EOPNOTSUPP;
3183
3184 if (!dev->ieee80211_ptr->beacon_interval)
3185 return -EINVAL;
3186
3187 acl = parse_acl_data(&rdev->wiphy, info);
3188 if (IS_ERR(acl))
3189 return PTR_ERR(acl);
3190
3191 err = rdev_set_mac_acl(rdev, dev, acl);
3192
3193 kfree(acl);
3194
3195 return err;
3196}
3197
3198static int nl80211_parse_beacon(struct nlattr *attrs[],
3199 struct cfg80211_beacon_data *bcn)
3200{
3201 bool haveinfo = false;
3202
3203 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3204 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3205 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3206 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3207 return -EINVAL;
3208
3209 memset(bcn, 0, sizeof(*bcn));
3210
3211 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3212 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3213 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3214 if (!bcn->head_len)
3215 return -EINVAL;
3216 haveinfo = true;
3217 }
3218
3219 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3220 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3221 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3222 haveinfo = true;
3223 }
3224
3225 if (!haveinfo)
3226 return -EINVAL;
3227
3228 if (attrs[NL80211_ATTR_IE]) {
3229 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3230 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3231 }
3232
3233 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3234 bcn->proberesp_ies =
3235 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3236 bcn->proberesp_ies_len =
3237 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3238 }
3239
3240 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3241 bcn->assocresp_ies =
3242 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3243 bcn->assocresp_ies_len =
3244 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3245 }
3246
3247 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3248 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3249 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3250 }
3251
3252 return 0;
3253}
3254
3255static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3256 struct cfg80211_ap_settings *params)
3257{
3258 struct wireless_dev *wdev;
3259 bool ret = false;
3260
3261 list_for_each_entry(wdev, &rdev->wdev_list, list) {
3262 if (wdev->iftype != NL80211_IFTYPE_AP &&
3263 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3264 continue;
3265
3266 if (!wdev->preset_chandef.chan)
3267 continue;
3268
3269 params->chandef = wdev->preset_chandef;
3270 ret = true;
3271 break;
3272 }
3273
3274 return ret;
3275}
3276
3277static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3278 enum nl80211_auth_type auth_type,
3279 enum nl80211_commands cmd)
3280{
3281 if (auth_type > NL80211_AUTHTYPE_MAX)
3282 return false;
3283
3284 switch (cmd) {
3285 case NL80211_CMD_AUTHENTICATE:
3286 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3287 auth_type == NL80211_AUTHTYPE_SAE)
3288 return false;
3289 return true;
3290 case NL80211_CMD_CONNECT:
3291 case NL80211_CMD_START_AP:
3292 /* SAE not supported yet */
3293 if (auth_type == NL80211_AUTHTYPE_SAE)
3294 return false;
3295 return true;
3296 default:
3297 return false;
3298 }
3299}
3300
3301static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3302{
3303 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3304 struct net_device *dev = info->user_ptr[1];
3305 struct wireless_dev *wdev = dev->ieee80211_ptr;
3306 struct cfg80211_ap_settings params;
3307 int err;
3308
3309 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3310 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3311 return -EOPNOTSUPP;
3312
3313 if (!rdev->ops->start_ap)
3314 return -EOPNOTSUPP;
3315
3316 if (wdev->beacon_interval)
3317 return -EALREADY;
3318
3319 memset(&params, 0, sizeof(params));
3320
3321 /* these are required for START_AP */
3322 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3323 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3324 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3325 return -EINVAL;
3326
3327 err = nl80211_parse_beacon(info->attrs, &params.beacon);
3328 if (err)
3329 return err;
3330
3331 params.beacon_interval =
3332 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3333 params.dtim_period =
3334 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3335
3336 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3337 if (err)
3338 return err;
3339
3340 /*
3341 * In theory, some of these attributes should be required here
3342 * but since they were not used when the command was originally
3343 * added, keep them optional for old user space programs to let
3344 * them continue to work with drivers that do not need the
3345 * additional information -- drivers must check!
3346 */
3347 if (info->attrs[NL80211_ATTR_SSID]) {
3348 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3349 params.ssid_len =
3350 nla_len(info->attrs[NL80211_ATTR_SSID]);
3351 if (params.ssid_len == 0 ||
3352 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3353 return -EINVAL;
3354 }
3355
3356 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3357 params.hidden_ssid = nla_get_u32(
3358 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3359 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3360 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3361 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3362 return -EINVAL;
3363 }
3364
3365 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3366
3367 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3368 params.auth_type = nla_get_u32(
3369 info->attrs[NL80211_ATTR_AUTH_TYPE]);
3370 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3371 NL80211_CMD_START_AP))
3372 return -EINVAL;
3373 } else
3374 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3375
3376 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3377 NL80211_MAX_NR_CIPHER_SUITES);
3378 if (err)
3379 return err;
3380
3381 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3382 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3383 return -EOPNOTSUPP;
3384 params.inactivity_timeout = nla_get_u16(
3385 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3386 }
3387
3388 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3389 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3390 return -EINVAL;
3391 params.p2p_ctwindow =
3392 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3393 if (params.p2p_ctwindow > 127)
3394 return -EINVAL;
3395 if (params.p2p_ctwindow != 0 &&
3396 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3397 return -EINVAL;
3398 }
3399
3400 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3401 u8 tmp;
3402
3403 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3404 return -EINVAL;
3405 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3406 if (tmp > 1)
3407 return -EINVAL;
3408 params.p2p_opp_ps = tmp;
3409 if (params.p2p_opp_ps != 0 &&
3410 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3411 return -EINVAL;
3412 }
3413
3414 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3415 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3416 if (err)
3417 return err;
3418 } else if (wdev->preset_chandef.chan) {
3419 params.chandef = wdev->preset_chandef;
3420 } else if (!nl80211_get_ap_channel(rdev, &params))
3421 return -EINVAL;
3422
3423 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
3424 wdev->iftype))
3425 return -EINVAL;
3426
3427 if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
3428 params.smps_mode =
3429 nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
3430 switch (params.smps_mode) {
3431 case NL80211_SMPS_OFF:
3432 break;
3433 case NL80211_SMPS_STATIC:
3434 if (!(rdev->wiphy.features &
3435 NL80211_FEATURE_STATIC_SMPS))
3436 return -EINVAL;
3437 break;
3438 case NL80211_SMPS_DYNAMIC:
3439 if (!(rdev->wiphy.features &
3440 NL80211_FEATURE_DYNAMIC_SMPS))
3441 return -EINVAL;
3442 break;
3443 default:
3444 return -EINVAL;
3445 }
3446 } else {
3447 params.smps_mode = NL80211_SMPS_OFF;
3448 }
3449
3450 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3451 params.acl = parse_acl_data(&rdev->wiphy, info);
3452 if (IS_ERR(params.acl))
3453 return PTR_ERR(params.acl);
3454 }
3455
3456 wdev_lock(wdev);
3457 err = rdev_start_ap(rdev, dev, &params);
3458 if (!err) {
3459 wdev->preset_chandef = params.chandef;
3460 wdev->beacon_interval = params.beacon_interval;
3461 wdev->chandef = params.chandef;
3462 wdev->ssid_len = params.ssid_len;
3463 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
3464 }
3465 wdev_unlock(wdev);
3466
3467 kfree(params.acl);
3468
3469 return err;
3470}
3471
3472static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3473{
3474 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3475 struct net_device *dev = info->user_ptr[1];
3476 struct wireless_dev *wdev = dev->ieee80211_ptr;
3477 struct cfg80211_beacon_data params;
3478 int err;
3479
3480 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3481 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3482 return -EOPNOTSUPP;
3483
3484 if (!rdev->ops->change_beacon)
3485 return -EOPNOTSUPP;
3486
3487 if (!wdev->beacon_interval)
3488 return -EINVAL;
3489
3490 err = nl80211_parse_beacon(info->attrs, &params);
3491 if (err)
3492 return err;
3493
3494 wdev_lock(wdev);
3495 err = rdev_change_beacon(rdev, dev, &params);
3496 wdev_unlock(wdev);
3497
3498 return err;
3499}
3500
3501static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
3502{
3503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3504 struct net_device *dev = info->user_ptr[1];
3505
3506 return cfg80211_stop_ap(rdev, dev, false);
3507}
3508
3509static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3510 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3511 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3512 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
3513 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
3514 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
3515 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
3516};
3517
3518static int parse_station_flags(struct genl_info *info,
3519 enum nl80211_iftype iftype,
3520 struct station_parameters *params)
3521{
3522 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
3523 struct nlattr *nla;
3524 int flag;
3525
3526 /*
3527 * Try parsing the new attribute first so userspace
3528 * can specify both for older kernels.
3529 */
3530 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3531 if (nla) {
3532 struct nl80211_sta_flag_update *sta_flags;
3533
3534 sta_flags = nla_data(nla);
3535 params->sta_flags_mask = sta_flags->mask;
3536 params->sta_flags_set = sta_flags->set;
3537 params->sta_flags_set &= params->sta_flags_mask;
3538 if ((params->sta_flags_mask |
3539 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3540 return -EINVAL;
3541 return 0;
3542 }
3543
3544 /* if present, parse the old attribute */
3545
3546 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
3547 if (!nla)
3548 return 0;
3549
3550 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3551 nla, sta_flags_policy))
3552 return -EINVAL;
3553
3554 /*
3555 * Only allow certain flags for interface types so that
3556 * other attributes are silently ignored. Remember that
3557 * this is backward compatibility code with old userspace
3558 * and shouldn't be hit in other cases anyway.
3559 */
3560 switch (iftype) {
3561 case NL80211_IFTYPE_AP:
3562 case NL80211_IFTYPE_AP_VLAN:
3563 case NL80211_IFTYPE_P2P_GO:
3564 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3565 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3566 BIT(NL80211_STA_FLAG_WME) |
3567 BIT(NL80211_STA_FLAG_MFP);
3568 break;
3569 case NL80211_IFTYPE_P2P_CLIENT:
3570 case NL80211_IFTYPE_STATION:
3571 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3572 BIT(NL80211_STA_FLAG_TDLS_PEER);
3573 break;
3574 case NL80211_IFTYPE_MESH_POINT:
3575 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3576 BIT(NL80211_STA_FLAG_MFP) |
3577 BIT(NL80211_STA_FLAG_AUTHORIZED);
3578 default:
3579 return -EINVAL;
3580 }
3581
3582 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3583 if (flags[flag]) {
3584 params->sta_flags_set |= (1<<flag);
3585
3586 /* no longer support new API additions in old API */
3587 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3588 return -EINVAL;
3589 }
3590 }
3591
3592 return 0;
3593}
3594
3595static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3596 int attr)
3597{
3598 struct nlattr *rate;
3599 u32 bitrate;
3600 u16 bitrate_compat;
3601 enum nl80211_attrs rate_flg;
3602
3603 rate = nla_nest_start(msg, attr);
3604 if (!rate)
3605 return false;
3606
3607 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3608 bitrate = cfg80211_calculate_bitrate(info);
3609 /* report 16-bit bitrate only if we can */
3610 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
3611 if (bitrate > 0 &&
3612 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3613 return false;
3614 if (bitrate_compat > 0 &&
3615 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3616 return false;
3617
3618 switch (info->bw) {
3619 case RATE_INFO_BW_5:
3620 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
3621 break;
3622 case RATE_INFO_BW_10:
3623 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
3624 break;
3625 default:
3626 WARN_ON(1);
3627 /* fall through */
3628 case RATE_INFO_BW_20:
3629 rate_flg = 0;
3630 break;
3631 case RATE_INFO_BW_40:
3632 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
3633 break;
3634 case RATE_INFO_BW_80:
3635 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
3636 break;
3637 case RATE_INFO_BW_160:
3638 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
3639 break;
3640 }
3641
3642 if (rate_flg && nla_put_flag(msg, rate_flg))
3643 return false;
3644
3645 if (info->flags & RATE_INFO_FLAGS_MCS) {
3646 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3647 return false;
3648 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3649 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3650 return false;
3651 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3652 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3653 return false;
3654 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3655 return false;
3656 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3657 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3658 return false;
3659 }
3660
3661 nla_nest_end(msg, rate);
3662 return true;
3663}
3664
3665static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3666 int id)
3667{
3668 void *attr;
3669 int i = 0;
3670
3671 if (!mask)
3672 return true;
3673
3674 attr = nla_nest_start(msg, id);
3675 if (!attr)
3676 return false;
3677
3678 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3679 if (!(mask & BIT(i)))
3680 continue;
3681
3682 if (nla_put_u8(msg, i, signal[i]))
3683 return false;
3684 }
3685
3686 nla_nest_end(msg, attr);
3687
3688 return true;
3689}
3690
3691static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
3692 u32 seq, int flags,
3693 struct cfg80211_registered_device *rdev,
3694 struct net_device *dev,
3695 const u8 *mac_addr, struct station_info *sinfo)
3696{
3697 void *hdr;
3698 struct nlattr *sinfoattr, *bss_param;
3699
3700 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3701 if (!hdr)
3702 return -1;
3703
3704 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3705 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3706 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3707 goto nla_put_failure;
3708
3709 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3710 if (!sinfoattr)
3711 goto nla_put_failure;
3712
3713#define PUT_SINFO(attr, memb, type) do { \
3714 if (sinfo->filled & BIT(NL80211_STA_INFO_ ## attr) && \
3715 nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr, \
3716 sinfo->memb)) \
3717 goto nla_put_failure; \
3718 } while (0)
3719
3720 PUT_SINFO(CONNECTED_TIME, connected_time, u32);
3721 PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
3722
3723 if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
3724 BIT(NL80211_STA_INFO_RX_BYTES64)) &&
3725 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
3726 (u32)sinfo->rx_bytes))
3727 goto nla_put_failure;
3728
3729 if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
3730 BIT(NL80211_STA_INFO_TX_BYTES64)) &&
3731 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3732 (u32)sinfo->tx_bytes))
3733 goto nla_put_failure;
3734
3735 PUT_SINFO(RX_BYTES64, rx_bytes, u64);
3736 PUT_SINFO(TX_BYTES64, tx_bytes, u64);
3737 PUT_SINFO(LLID, llid, u16);
3738 PUT_SINFO(PLID, plid, u16);
3739 PUT_SINFO(PLINK_STATE, plink_state, u8);
3740
3741 switch (rdev->wiphy.signal_type) {
3742 case CFG80211_SIGNAL_TYPE_MBM:
3743 PUT_SINFO(SIGNAL, signal, u8);
3744 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
3745 break;
3746 default:
3747 break;
3748 }
3749 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
3750 if (!nl80211_put_signal(msg, sinfo->chains,
3751 sinfo->chain_signal,
3752 NL80211_STA_INFO_CHAIN_SIGNAL))
3753 goto nla_put_failure;
3754 }
3755 if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
3756 if (!nl80211_put_signal(msg, sinfo->chains,
3757 sinfo->chain_signal_avg,
3758 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3759 goto nla_put_failure;
3760 }
3761 if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
3762 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3763 NL80211_STA_INFO_TX_BITRATE))
3764 goto nla_put_failure;
3765 }
3766 if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
3767 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3768 NL80211_STA_INFO_RX_BITRATE))
3769 goto nla_put_failure;
3770 }
3771
3772 PUT_SINFO(RX_PACKETS, rx_packets, u32);
3773 PUT_SINFO(TX_PACKETS, tx_packets, u32);
3774 PUT_SINFO(TX_RETRIES, tx_retries, u32);
3775 PUT_SINFO(TX_FAILED, tx_failed, u32);
3776 PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
3777 PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
3778 PUT_SINFO(LOCAL_PM, local_pm, u32);
3779 PUT_SINFO(PEER_PM, peer_pm, u32);
3780 PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
3781
3782 if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
3783 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3784 if (!bss_param)
3785 goto nla_put_failure;
3786
3787 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3788 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3789 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3790 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3791 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3792 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3793 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3794 sinfo->bss_param.dtim_period) ||
3795 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3796 sinfo->bss_param.beacon_interval))
3797 goto nla_put_failure;
3798
3799 nla_nest_end(msg, bss_param);
3800 }
3801 if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
3802 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3803 sizeof(struct nl80211_sta_flag_update),
3804 &sinfo->sta_flags))
3805 goto nla_put_failure;
3806
3807 PUT_SINFO(T_OFFSET, t_offset, u64);
3808 PUT_SINFO(RX_DROP_MISC, rx_dropped_misc, u64);
3809 PUT_SINFO(BEACON_RX, rx_beacon, u64);
3810 PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
3811
3812#undef PUT_SINFO
3813
3814 if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
3815 struct nlattr *tidsattr;
3816 int tid;
3817
3818 tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
3819 if (!tidsattr)
3820 goto nla_put_failure;
3821
3822 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
3823 struct cfg80211_tid_stats *tidstats;
3824 struct nlattr *tidattr;
3825
3826 tidstats = &sinfo->pertid[tid];
3827
3828 if (!tidstats->filled)
3829 continue;
3830
3831 tidattr = nla_nest_start(msg, tid + 1);
3832 if (!tidattr)
3833 goto nla_put_failure;
3834
3835#define PUT_TIDVAL(attr, memb, type) do { \
3836 if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) && \
3837 nla_put_ ## type(msg, NL80211_TID_STATS_ ## attr, \
3838 tidstats->memb)) \
3839 goto nla_put_failure; \
3840 } while (0)
3841
3842 PUT_TIDVAL(RX_MSDU, rx_msdu, u64);
3843 PUT_TIDVAL(TX_MSDU, tx_msdu, u64);
3844 PUT_TIDVAL(TX_MSDU_RETRIES, tx_msdu_retries, u64);
3845 PUT_TIDVAL(TX_MSDU_FAILED, tx_msdu_failed, u64);
3846
3847#undef PUT_TIDVAL
3848 nla_nest_end(msg, tidattr);
3849 }
3850
3851 nla_nest_end(msg, tidsattr);
3852 }
3853
3854 nla_nest_end(msg, sinfoattr);
3855
3856 if (sinfo->assoc_req_ies_len &&
3857 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3858 sinfo->assoc_req_ies))
3859 goto nla_put_failure;
3860
3861 genlmsg_end(msg, hdr);
3862 return 0;
3863
3864 nla_put_failure:
3865 genlmsg_cancel(msg, hdr);
3866 return -EMSGSIZE;
3867}
3868
3869static int nl80211_dump_station(struct sk_buff *skb,
3870 struct netlink_callback *cb)
3871{
3872 struct station_info sinfo;
3873 struct cfg80211_registered_device *rdev;
3874 struct wireless_dev *wdev;
3875 u8 mac_addr[ETH_ALEN];
3876 int sta_idx = cb->args[2];
3877 int err;
3878
3879 rtnl_lock();
3880 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
3881 if (err)
3882 goto out_err;
3883
3884 if (!wdev->netdev) {
3885 err = -EINVAL;
3886 goto out_err;
3887 }
3888
3889 if (!rdev->ops->dump_station) {
3890 err = -EOPNOTSUPP;
3891 goto out_err;
3892 }
3893
3894 while (1) {
3895 memset(&sinfo, 0, sizeof(sinfo));
3896 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
3897 mac_addr, &sinfo);
3898 if (err == -ENOENT)
3899 break;
3900 if (err)
3901 goto out_err;
3902
3903 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
3904 NETLINK_CB(cb->skb).portid,
3905 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3906 rdev, wdev->netdev, mac_addr,
3907 &sinfo) < 0)
3908 goto out;
3909
3910 sta_idx++;
3911 }
3912
3913
3914 out:
3915 cb->args[2] = sta_idx;
3916 err = skb->len;
3917 out_err:
3918 rtnl_unlock();
3919
3920 return err;
3921}
3922
3923static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3924{
3925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3926 struct net_device *dev = info->user_ptr[1];
3927 struct station_info sinfo;
3928 struct sk_buff *msg;
3929 u8 *mac_addr = NULL;
3930 int err;
3931
3932 memset(&sinfo, 0, sizeof(sinfo));
3933
3934 if (!info->attrs[NL80211_ATTR_MAC])
3935 return -EINVAL;
3936
3937 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3938
3939 if (!rdev->ops->get_station)
3940 return -EOPNOTSUPP;
3941
3942 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
3943 if (err)
3944 return err;
3945
3946 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3947 if (!msg)
3948 return -ENOMEM;
3949
3950 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
3951 info->snd_portid, info->snd_seq, 0,
3952 rdev, dev, mac_addr, &sinfo) < 0) {
3953 nlmsg_free(msg);
3954 return -ENOBUFS;
3955 }
3956
3957 return genlmsg_reply(msg, info);
3958}
3959
3960int cfg80211_check_station_change(struct wiphy *wiphy,
3961 struct station_parameters *params,
3962 enum cfg80211_station_type statype)
3963{
3964 if (params->listen_interval != -1 &&
3965 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
3966 return -EINVAL;
3967
3968 if (params->aid &&
3969 !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
3970 statype != CFG80211_STA_AP_CLIENT_UNASSOC)
3971 return -EINVAL;
3972
3973 /* When you run into this, adjust the code below for the new flag */
3974 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3975
3976 switch (statype) {
3977 case CFG80211_STA_MESH_PEER_KERNEL:
3978 case CFG80211_STA_MESH_PEER_USER:
3979 /*
3980 * No ignoring the TDLS flag here -- the userspace mesh
3981 * code doesn't have the bug of including TDLS in the
3982 * mask everywhere.
3983 */
3984 if (params->sta_flags_mask &
3985 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3986 BIT(NL80211_STA_FLAG_MFP) |
3987 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3988 return -EINVAL;
3989 break;
3990 case CFG80211_STA_TDLS_PEER_SETUP:
3991 case CFG80211_STA_TDLS_PEER_ACTIVE:
3992 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3993 return -EINVAL;
3994 /* ignore since it can't change */
3995 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3996 break;
3997 default:
3998 /* disallow mesh-specific things */
3999 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4000 return -EINVAL;
4001 if (params->local_pm)
4002 return -EINVAL;
4003 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4004 return -EINVAL;
4005 }
4006
4007 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4008 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4009 /* TDLS can't be set, ... */
4010 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4011 return -EINVAL;
4012 /*
4013 * ... but don't bother the driver with it. This works around
4014 * a hostapd/wpa_supplicant issue -- it always includes the
4015 * TLDS_PEER flag in the mask even for AP mode.
4016 */
4017 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4018 }
4019
4020 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4021 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4022 /* reject other things that can't change */
4023 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4024 return -EINVAL;
4025 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4026 return -EINVAL;
4027 if (params->supported_rates)
4028 return -EINVAL;
4029 if (params->ext_capab || params->ht_capa || params->vht_capa)
4030 return -EINVAL;
4031 }
4032
4033 if (statype != CFG80211_STA_AP_CLIENT &&
4034 statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4035 if (params->vlan)
4036 return -EINVAL;
4037 }
4038
4039 switch (statype) {
4040 case CFG80211_STA_AP_MLME_CLIENT:
4041 /* Use this only for authorizing/unauthorizing a station */
4042 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4043 return -EOPNOTSUPP;
4044 break;
4045 case CFG80211_STA_AP_CLIENT:
4046 case CFG80211_STA_AP_CLIENT_UNASSOC:
4047 /* accept only the listed bits */
4048 if (params->sta_flags_mask &
4049 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4050 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4051 BIT(NL80211_STA_FLAG_ASSOCIATED) |
4052 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4053 BIT(NL80211_STA_FLAG_WME) |
4054 BIT(NL80211_STA_FLAG_MFP)))
4055 return -EINVAL;
4056
4057 /* but authenticated/associated only if driver handles it */
4058 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4059 params->sta_flags_mask &
4060 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4061 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4062 return -EINVAL;
4063 break;
4064 case CFG80211_STA_IBSS:
4065 case CFG80211_STA_AP_STA:
4066 /* reject any changes other than AUTHORIZED */
4067 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4068 return -EINVAL;
4069 break;
4070 case CFG80211_STA_TDLS_PEER_SETUP:
4071 /* reject any changes other than AUTHORIZED or WME */
4072 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4073 BIT(NL80211_STA_FLAG_WME)))
4074 return -EINVAL;
4075 /* force (at least) rates when authorizing */
4076 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4077 !params->supported_rates)
4078 return -EINVAL;
4079 break;
4080 case CFG80211_STA_TDLS_PEER_ACTIVE:
4081 /* reject any changes */
4082 return -EINVAL;
4083 case CFG80211_STA_MESH_PEER_KERNEL:
4084 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4085 return -EINVAL;
4086 break;
4087 case CFG80211_STA_MESH_PEER_USER:
4088 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4089 params->plink_action != NL80211_PLINK_ACTION_BLOCK)
4090 return -EINVAL;
4091 break;
4092 }
4093
4094 return 0;
4095}
4096EXPORT_SYMBOL(cfg80211_check_station_change);
4097
4098/*
4099 * Get vlan interface making sure it is running and on the right wiphy.
4100 */
4101static struct net_device *get_vlan(struct genl_info *info,
4102 struct cfg80211_registered_device *rdev)
4103{
4104 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
4105 struct net_device *v;
4106 int ret;
4107
4108 if (!vlanattr)
4109 return NULL;
4110
4111 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4112 if (!v)
4113 return ERR_PTR(-ENODEV);
4114
4115 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4116 ret = -EINVAL;
4117 goto error;
4118 }
4119
4120 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4121 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4122 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4123 ret = -EINVAL;
4124 goto error;
4125 }
4126
4127 if (!netif_running(v)) {
4128 ret = -ENETDOWN;
4129 goto error;
4130 }
4131
4132 return v;
4133 error:
4134 dev_put(v);
4135 return ERR_PTR(ret);
4136}
4137
4138static const struct nla_policy
4139nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
4140 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4141 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4142};
4143
4144static int nl80211_parse_sta_wme(struct genl_info *info,
4145 struct station_parameters *params)
4146{
4147 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4148 struct nlattr *nla;
4149 int err;
4150
4151 /* parse WME attributes if present */
4152 if (!info->attrs[NL80211_ATTR_STA_WME])
4153 return 0;
4154
4155 nla = info->attrs[NL80211_ATTR_STA_WME];
4156 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4157 nl80211_sta_wme_policy);
4158 if (err)
4159 return err;
4160
4161 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4162 params->uapsd_queues = nla_get_u8(
4163 tb[NL80211_STA_WME_UAPSD_QUEUES]);
4164 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4165 return -EINVAL;
4166
4167 if (tb[NL80211_STA_WME_MAX_SP])
4168 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4169
4170 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4171 return -EINVAL;
4172
4173 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4174
4175 return 0;
4176}
4177
4178static int nl80211_parse_sta_channel_info(struct genl_info *info,
4179 struct station_parameters *params)
4180{
4181 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4182 params->supported_channels =
4183 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4184 params->supported_channels_len =
4185 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4186 /*
4187 * Need to include at least one (first channel, number of
4188 * channels) tuple for each subband, and must have proper
4189 * tuples for the rest of the data as well.
4190 */
4191 if (params->supported_channels_len < 2)
4192 return -EINVAL;
4193 if (params->supported_channels_len % 2)
4194 return -EINVAL;
4195 }
4196
4197 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4198 params->supported_oper_classes =
4199 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4200 params->supported_oper_classes_len =
4201 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4202 /*
4203 * The value of the Length field of the Supported Operating
4204 * Classes element is between 2 and 253.
4205 */
4206 if (params->supported_oper_classes_len < 2 ||
4207 params->supported_oper_classes_len > 253)
4208 return -EINVAL;
4209 }
4210 return 0;
4211}
4212
4213static int nl80211_set_station_tdls(struct genl_info *info,
4214 struct station_parameters *params)
4215{
4216 int err;
4217 /* Dummy STA entry gets updated once the peer capabilities are known */
4218 if (info->attrs[NL80211_ATTR_PEER_AID])
4219 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4220 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4221 params->ht_capa =
4222 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4223 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4224 params->vht_capa =
4225 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4226
4227 err = nl80211_parse_sta_channel_info(info, params);
4228 if (err)
4229 return err;
4230
4231 return nl80211_parse_sta_wme(info, params);
4232}
4233
4234static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4235{
4236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4237 struct net_device *dev = info->user_ptr[1];
4238 struct station_parameters params;
4239 u8 *mac_addr;
4240 int err;
4241
4242 memset(&params, 0, sizeof(params));
4243
4244 if (!rdev->ops->change_station)
4245 return -EOPNOTSUPP;
4246
4247 /*
4248 * AID and listen_interval properties can be set only for unassociated
4249 * station. Include these parameters here and will check them in
4250 * cfg80211_check_station_change().
4251 */
4252 if (info->attrs[NL80211_ATTR_PEER_AID])
4253 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4254
4255 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4256 params.listen_interval =
4257 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4258 else
4259 params.listen_interval = -1;
4260
4261 if (!info->attrs[NL80211_ATTR_MAC])
4262 return -EINVAL;
4263
4264 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4265
4266 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4267 params.supported_rates =
4268 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4269 params.supported_rates_len =
4270 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4271 }
4272
4273 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4274 params.capability =
4275 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4276 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4277 }
4278
4279 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4280 params.ext_capab =
4281 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4282 params.ext_capab_len =
4283 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4284 }
4285
4286 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4287 return -EINVAL;
4288
4289 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4290 params.plink_action =
4291 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4292 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4293 return -EINVAL;
4294 }
4295
4296 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
4297 params.plink_state =
4298 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4299 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4300 return -EINVAL;
4301 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4302 }
4303
4304 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4305 enum nl80211_mesh_power_mode pm = nla_get_u32(
4306 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4307
4308 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4309 pm > NL80211_MESH_POWER_MAX)
4310 return -EINVAL;
4311
4312 params.local_pm = pm;
4313 }
4314
4315 /* Include parameters for TDLS peer (will check later) */
4316 err = nl80211_set_station_tdls(info, &params);
4317 if (err)
4318 return err;
4319
4320 params.vlan = get_vlan(info, rdev);
4321 if (IS_ERR(params.vlan))
4322 return PTR_ERR(params.vlan);
4323
4324 switch (dev->ieee80211_ptr->iftype) {
4325 case NL80211_IFTYPE_AP:
4326 case NL80211_IFTYPE_AP_VLAN:
4327 case NL80211_IFTYPE_P2P_GO:
4328 case NL80211_IFTYPE_P2P_CLIENT:
4329 case NL80211_IFTYPE_STATION:
4330 case NL80211_IFTYPE_ADHOC:
4331 case NL80211_IFTYPE_MESH_POINT:
4332 break;
4333 default:
4334 err = -EOPNOTSUPP;
4335 goto out_put_vlan;
4336 }
4337
4338 /* driver will call cfg80211_check_station_change() */
4339 err = rdev_change_station(rdev, dev, mac_addr, &params);
4340
4341 out_put_vlan:
4342 if (params.vlan)
4343 dev_put(params.vlan);
4344
4345 return err;
4346}
4347
4348static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4349{
4350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4351 int err;
4352 struct net_device *dev = info->user_ptr[1];
4353 struct station_parameters params;
4354 u8 *mac_addr = NULL;
4355
4356 memset(&params, 0, sizeof(params));
4357
4358 if (!rdev->ops->add_station)
4359 return -EOPNOTSUPP;
4360
4361 if (!info->attrs[NL80211_ATTR_MAC])
4362 return -EINVAL;
4363
4364 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4365 return -EINVAL;
4366
4367 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4368 return -EINVAL;
4369
4370 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4371 !info->attrs[NL80211_ATTR_PEER_AID])
4372 return -EINVAL;
4373
4374 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4375 params.supported_rates =
4376 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4377 params.supported_rates_len =
4378 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4379 params.listen_interval =
4380 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4381
4382 if (info->attrs[NL80211_ATTR_PEER_AID])
4383 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4384 else
4385 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4386 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4387 return -EINVAL;
4388
4389 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4390 params.capability =
4391 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4392 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4393 }
4394
4395 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4396 params.ext_capab =
4397 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4398 params.ext_capab_len =
4399 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4400 }
4401
4402 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4403 params.ht_capa =
4404 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4405
4406 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4407 params.vht_capa =
4408 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4409
4410 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4411 params.opmode_notif_used = true;
4412 params.opmode_notif =
4413 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4414 }
4415
4416 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4417 params.plink_action =
4418 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4419 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4420 return -EINVAL;
4421 }
4422
4423 err = nl80211_parse_sta_channel_info(info, &params);
4424 if (err)
4425 return err;
4426
4427 err = nl80211_parse_sta_wme(info, &params);
4428 if (err)
4429 return err;
4430
4431 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4432 return -EINVAL;
4433
4434 /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
4435 * as userspace might just pass through the capabilities from the IEs
4436 * directly, rather than enforcing this restriction and returning an
4437 * error in this case.
4438 */
4439 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
4440 params.ht_capa = NULL;
4441 params.vht_capa = NULL;
4442 }
4443
4444 /* When you run into this, adjust the code below for the new flag */
4445 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4446
4447 switch (dev->ieee80211_ptr->iftype) {
4448 case NL80211_IFTYPE_AP:
4449 case NL80211_IFTYPE_AP_VLAN:
4450 case NL80211_IFTYPE_P2P_GO:
4451 /* ignore WME attributes if iface/sta is not capable */
4452 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4453 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4454 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4455
4456 /* TDLS peers cannot be added */
4457 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4458 info->attrs[NL80211_ATTR_PEER_AID])
4459 return -EINVAL;
4460 /* but don't bother the driver with it */
4461 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4462
4463 /* allow authenticated/associated only if driver handles it */
4464 if (!(rdev->wiphy.features &
4465 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4466 params.sta_flags_mask &
4467 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4468 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4469 return -EINVAL;
4470
4471 /* must be last in here for error handling */
4472 params.vlan = get_vlan(info, rdev);
4473 if (IS_ERR(params.vlan))
4474 return PTR_ERR(params.vlan);
4475 break;
4476 case NL80211_IFTYPE_MESH_POINT:
4477 /* ignore uAPSD data */
4478 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4479
4480 /* associated is disallowed */
4481 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4482 return -EINVAL;
4483 /* TDLS peers cannot be added */
4484 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4485 info->attrs[NL80211_ATTR_PEER_AID])
4486 return -EINVAL;
4487 break;
4488 case NL80211_IFTYPE_STATION:
4489 case NL80211_IFTYPE_P2P_CLIENT:
4490 /* ignore uAPSD data */
4491 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4492
4493 /* these are disallowed */
4494 if (params.sta_flags_mask &
4495 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4496 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
4497 return -EINVAL;
4498 /* Only TDLS peers can be added */
4499 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4500 return -EINVAL;
4501 /* Can only add if TDLS ... */
4502 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4503 return -EOPNOTSUPP;
4504 /* ... with external setup is supported */
4505 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4506 return -EOPNOTSUPP;
4507 /*
4508 * Older wpa_supplicant versions always mark the TDLS peer
4509 * as authorized, but it shouldn't yet be.
4510 */
4511 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
4512 break;
4513 default:
4514 return -EOPNOTSUPP;
4515 }
4516
4517 /* be aware of params.vlan when changing code here */
4518
4519 err = rdev_add_station(rdev, dev, mac_addr, &params);
4520
4521 if (params.vlan)
4522 dev_put(params.vlan);
4523 return err;
4524}
4525
4526static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4527{
4528 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4529 struct net_device *dev = info->user_ptr[1];
4530 struct station_del_parameters params;
4531
4532 memset(&params, 0, sizeof(params));
4533
4534 if (info->attrs[NL80211_ATTR_MAC])
4535 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
4536
4537 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4538 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4539 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4540 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4541 return -EINVAL;
4542
4543 if (!rdev->ops->del_station)
4544 return -EOPNOTSUPP;
4545
4546 if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
4547 params.subtype =
4548 nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
4549 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
4550 params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
4551 return -EINVAL;
4552 } else {
4553 /* Default to Deauthentication frame */
4554 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
4555 }
4556
4557 if (info->attrs[NL80211_ATTR_REASON_CODE]) {
4558 params.reason_code =
4559 nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4560 if (params.reason_code == 0)
4561 return -EINVAL; /* 0 is reserved */
4562 } else {
4563 /* Default to reason code 2 */
4564 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
4565 }
4566
4567 return rdev_del_station(rdev, dev, &params);
4568}
4569
4570static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
4571 int flags, struct net_device *dev,
4572 u8 *dst, u8 *next_hop,
4573 struct mpath_info *pinfo)
4574{
4575 void *hdr;
4576 struct nlattr *pinfoattr;
4577
4578 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
4579 if (!hdr)
4580 return -1;
4581
4582 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4583 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4584 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4585 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4586 goto nla_put_failure;
4587
4588 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4589 if (!pinfoattr)
4590 goto nla_put_failure;
4591 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4592 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4593 pinfo->frame_qlen))
4594 goto nla_put_failure;
4595 if (((pinfo->filled & MPATH_INFO_SN) &&
4596 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4597 ((pinfo->filled & MPATH_INFO_METRIC) &&
4598 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4599 pinfo->metric)) ||
4600 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4601 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4602 pinfo->exptime)) ||
4603 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4604 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4605 pinfo->flags)) ||
4606 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4607 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4608 pinfo->discovery_timeout)) ||
4609 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4610 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4611 pinfo->discovery_retries)))
4612 goto nla_put_failure;
4613
4614 nla_nest_end(msg, pinfoattr);
4615
4616 genlmsg_end(msg, hdr);
4617 return 0;
4618
4619 nla_put_failure:
4620 genlmsg_cancel(msg, hdr);
4621 return -EMSGSIZE;
4622}
4623
4624static int nl80211_dump_mpath(struct sk_buff *skb,
4625 struct netlink_callback *cb)
4626{
4627 struct mpath_info pinfo;
4628 struct cfg80211_registered_device *rdev;
4629 struct wireless_dev *wdev;
4630 u8 dst[ETH_ALEN];
4631 u8 next_hop[ETH_ALEN];
4632 int path_idx = cb->args[2];
4633 int err;
4634
4635 rtnl_lock();
4636 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4637 if (err)
4638 goto out_err;
4639
4640 if (!rdev->ops->dump_mpath) {
4641 err = -EOPNOTSUPP;
4642 goto out_err;
4643 }
4644
4645 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4646 err = -EOPNOTSUPP;
4647 goto out_err;
4648 }
4649
4650 while (1) {
4651 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
4652 next_hop, &pinfo);
4653 if (err == -ENOENT)
4654 break;
4655 if (err)
4656 goto out_err;
4657
4658 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4659 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4660 wdev->netdev, dst, next_hop,
4661 &pinfo) < 0)
4662 goto out;
4663
4664 path_idx++;
4665 }
4666
4667
4668 out:
4669 cb->args[2] = path_idx;
4670 err = skb->len;
4671 out_err:
4672 rtnl_unlock();
4673 return err;
4674}
4675
4676static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4677{
4678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4679 int err;
4680 struct net_device *dev = info->user_ptr[1];
4681 struct mpath_info pinfo;
4682 struct sk_buff *msg;
4683 u8 *dst = NULL;
4684 u8 next_hop[ETH_ALEN];
4685
4686 memset(&pinfo, 0, sizeof(pinfo));
4687
4688 if (!info->attrs[NL80211_ATTR_MAC])
4689 return -EINVAL;
4690
4691 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4692
4693 if (!rdev->ops->get_mpath)
4694 return -EOPNOTSUPP;
4695
4696 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4697 return -EOPNOTSUPP;
4698
4699 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
4700 if (err)
4701 return err;
4702
4703 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4704 if (!msg)
4705 return -ENOMEM;
4706
4707 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4708 dev, dst, next_hop, &pinfo) < 0) {
4709 nlmsg_free(msg);
4710 return -ENOBUFS;
4711 }
4712
4713 return genlmsg_reply(msg, info);
4714}
4715
4716static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4717{
4718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4719 struct net_device *dev = info->user_ptr[1];
4720 u8 *dst = NULL;
4721 u8 *next_hop = NULL;
4722
4723 if (!info->attrs[NL80211_ATTR_MAC])
4724 return -EINVAL;
4725
4726 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4727 return -EINVAL;
4728
4729 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4730 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4731
4732 if (!rdev->ops->change_mpath)
4733 return -EOPNOTSUPP;
4734
4735 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4736 return -EOPNOTSUPP;
4737
4738 return rdev_change_mpath(rdev, dev, dst, next_hop);
4739}
4740
4741static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4742{
4743 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4744 struct net_device *dev = info->user_ptr[1];
4745 u8 *dst = NULL;
4746 u8 *next_hop = NULL;
4747
4748 if (!info->attrs[NL80211_ATTR_MAC])
4749 return -EINVAL;
4750
4751 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4752 return -EINVAL;
4753
4754 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4755 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4756
4757 if (!rdev->ops->add_mpath)
4758 return -EOPNOTSUPP;
4759
4760 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4761 return -EOPNOTSUPP;
4762
4763 return rdev_add_mpath(rdev, dev, dst, next_hop);
4764}
4765
4766static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4767{
4768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4769 struct net_device *dev = info->user_ptr[1];
4770 u8 *dst = NULL;
4771
4772 if (info->attrs[NL80211_ATTR_MAC])
4773 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4774
4775 if (!rdev->ops->del_mpath)
4776 return -EOPNOTSUPP;
4777
4778 return rdev_del_mpath(rdev, dev, dst);
4779}
4780
4781static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
4782{
4783 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4784 int err;
4785 struct net_device *dev = info->user_ptr[1];
4786 struct mpath_info pinfo;
4787 struct sk_buff *msg;
4788 u8 *dst = NULL;
4789 u8 mpp[ETH_ALEN];
4790
4791 memset(&pinfo, 0, sizeof(pinfo));
4792
4793 if (!info->attrs[NL80211_ATTR_MAC])
4794 return -EINVAL;
4795
4796 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4797
4798 if (!rdev->ops->get_mpp)
4799 return -EOPNOTSUPP;
4800
4801 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4802 return -EOPNOTSUPP;
4803
4804 err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
4805 if (err)
4806 return err;
4807
4808 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4809 if (!msg)
4810 return -ENOMEM;
4811
4812 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4813 dev, dst, mpp, &pinfo) < 0) {
4814 nlmsg_free(msg);
4815 return -ENOBUFS;
4816 }
4817
4818 return genlmsg_reply(msg, info);
4819}
4820
4821static int nl80211_dump_mpp(struct sk_buff *skb,
4822 struct netlink_callback *cb)
4823{
4824 struct mpath_info pinfo;
4825 struct cfg80211_registered_device *rdev;
4826 struct wireless_dev *wdev;
4827 u8 dst[ETH_ALEN];
4828 u8 mpp[ETH_ALEN];
4829 int path_idx = cb->args[2];
4830 int err;
4831
4832 rtnl_lock();
4833 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4834 if (err)
4835 goto out_err;
4836
4837 if (!rdev->ops->dump_mpp) {
4838 err = -EOPNOTSUPP;
4839 goto out_err;
4840 }
4841
4842 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
4843 err = -EOPNOTSUPP;
4844 goto out_err;
4845 }
4846
4847 while (1) {
4848 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
4849 mpp, &pinfo);
4850 if (err == -ENOENT)
4851 break;
4852 if (err)
4853 goto out_err;
4854
4855 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
4856 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4857 wdev->netdev, dst, mpp,
4858 &pinfo) < 0)
4859 goto out;
4860
4861 path_idx++;
4862 }
4863
4864 out:
4865 cb->args[2] = path_idx;
4866 err = skb->len;
4867 out_err:
4868 rtnl_unlock();
4869 return err;
4870}
4871
4872static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4873{
4874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4875 struct net_device *dev = info->user_ptr[1];
4876 struct wireless_dev *wdev = dev->ieee80211_ptr;
4877 struct bss_parameters params;
4878 int err;
4879
4880 memset(&params, 0, sizeof(params));
4881 /* default to not changing parameters */
4882 params.use_cts_prot = -1;
4883 params.use_short_preamble = -1;
4884 params.use_short_slot_time = -1;
4885 params.ap_isolate = -1;
4886 params.ht_opmode = -1;
4887 params.p2p_ctwindow = -1;
4888 params.p2p_opp_ps = -1;
4889
4890 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4891 params.use_cts_prot =
4892 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4893 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4894 params.use_short_preamble =
4895 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4896 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4897 params.use_short_slot_time =
4898 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
4899 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4900 params.basic_rates =
4901 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4902 params.basic_rates_len =
4903 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4904 }
4905 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4906 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
4907 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4908 params.ht_opmode =
4909 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
4910
4911 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4912 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4913 return -EINVAL;
4914 params.p2p_ctwindow =
4915 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4916 if (params.p2p_ctwindow < 0)
4917 return -EINVAL;
4918 if (params.p2p_ctwindow != 0 &&
4919 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4920 return -EINVAL;
4921 }
4922
4923 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4924 u8 tmp;
4925
4926 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4927 return -EINVAL;
4928 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4929 if (tmp > 1)
4930 return -EINVAL;
4931 params.p2p_opp_ps = tmp;
4932 if (params.p2p_opp_ps &&
4933 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4934 return -EINVAL;
4935 }
4936
4937 if (!rdev->ops->change_bss)
4938 return -EOPNOTSUPP;
4939
4940 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4941 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4942 return -EOPNOTSUPP;
4943
4944 wdev_lock(wdev);
4945 err = rdev_change_bss(rdev, dev, &params);
4946 wdev_unlock(wdev);
4947
4948 return err;
4949}
4950
4951static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4952{
4953 char *data = NULL;
4954 bool is_indoor;
4955 enum nl80211_user_reg_hint_type user_reg_hint_type;
4956 u32 owner_nlportid;
4957
4958
4959 /*
4960 * You should only get this when cfg80211 hasn't yet initialized
4961 * completely when built-in to the kernel right between the time
4962 * window between nl80211_init() and regulatory_init(), if that is
4963 * even possible.
4964 */
4965 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
4966 return -EINPROGRESS;
4967
4968 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4969 user_reg_hint_type =
4970 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4971 else
4972 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4973
4974 switch (user_reg_hint_type) {
4975 case NL80211_USER_REG_HINT_USER:
4976 case NL80211_USER_REG_HINT_CELL_BASE:
4977 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4978 return -EINVAL;
4979
4980 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4981 return regulatory_hint_user(data, user_reg_hint_type);
4982 case NL80211_USER_REG_HINT_INDOOR:
4983 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
4984 owner_nlportid = info->snd_portid;
4985 is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
4986 } else {
4987 owner_nlportid = 0;
4988 is_indoor = true;
4989 }
4990
4991 return regulatory_hint_indoor(is_indoor, owner_nlportid);
4992 default:
4993 return -EINVAL;
4994 }
4995}
4996
4997static int nl80211_get_mesh_config(struct sk_buff *skb,
4998 struct genl_info *info)
4999{
5000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5001 struct net_device *dev = info->user_ptr[1];
5002 struct wireless_dev *wdev = dev->ieee80211_ptr;
5003 struct mesh_config cur_params;
5004 int err = 0;
5005 void *hdr;
5006 struct nlattr *pinfoattr;
5007 struct sk_buff *msg;
5008
5009 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5010 return -EOPNOTSUPP;
5011
5012 if (!rdev->ops->get_mesh_config)
5013 return -EOPNOTSUPP;
5014
5015 wdev_lock(wdev);
5016 /* If not connected, get default parameters */
5017 if (!wdev->mesh_id_len)
5018 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5019 else
5020 err = rdev_get_mesh_config(rdev, dev, &cur_params);
5021 wdev_unlock(wdev);
5022
5023 if (err)
5024 return err;
5025
5026 /* Draw up a netlink message to send back */
5027 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5028 if (!msg)
5029 return -ENOMEM;
5030 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5031 NL80211_CMD_GET_MESH_CONFIG);
5032 if (!hdr)
5033 goto out;
5034 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
5035 if (!pinfoattr)
5036 goto nla_put_failure;
5037 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5038 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5039 cur_params.dot11MeshRetryTimeout) ||
5040 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5041 cur_params.dot11MeshConfirmTimeout) ||
5042 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5043 cur_params.dot11MeshHoldingTimeout) ||
5044 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5045 cur_params.dot11MeshMaxPeerLinks) ||
5046 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5047 cur_params.dot11MeshMaxRetries) ||
5048 nla_put_u8(msg, NL80211_MESHCONF_TTL,
5049 cur_params.dot11MeshTTL) ||
5050 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5051 cur_params.element_ttl) ||
5052 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5053 cur_params.auto_open_plinks) ||
5054 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5055 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
5056 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5057 cur_params.dot11MeshHWMPmaxPREQretries) ||
5058 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5059 cur_params.path_refresh_time) ||
5060 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5061 cur_params.min_discovery_timeout) ||
5062 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5063 cur_params.dot11MeshHWMPactivePathTimeout) ||
5064 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5065 cur_params.dot11MeshHWMPpreqMinInterval) ||
5066 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5067 cur_params.dot11MeshHWMPperrMinInterval) ||
5068 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5069 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5070 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5071 cur_params.dot11MeshHWMPRootMode) ||
5072 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5073 cur_params.dot11MeshHWMPRannInterval) ||
5074 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5075 cur_params.dot11MeshGateAnnouncementProtocol) ||
5076 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5077 cur_params.dot11MeshForwarding) ||
5078 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
5079 cur_params.rssi_threshold) ||
5080 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
5081 cur_params.ht_opmode) ||
5082 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5083 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5084 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5085 cur_params.dot11MeshHWMProotInterval) ||
5086 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5087 cur_params.dot11MeshHWMPconfirmationInterval) ||
5088 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5089 cur_params.power_mode) ||
5090 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
5091 cur_params.dot11MeshAwakeWindowDuration) ||
5092 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5093 cur_params.plink_timeout))
5094 goto nla_put_failure;
5095 nla_nest_end(msg, pinfoattr);
5096 genlmsg_end(msg, hdr);
5097 return genlmsg_reply(msg, info);
5098
5099 nla_put_failure:
5100 genlmsg_cancel(msg, hdr);
5101 out:
5102 nlmsg_free(msg);
5103 return -ENOBUFS;
5104}
5105
5106static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
5107 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5108 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5109 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5110 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5111 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5112 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
5113 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
5114 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
5115 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
5116 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5117 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5118 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5119 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5120 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
5121 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
5122 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
5123 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
5124 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
5125 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
5126 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
5127 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5128 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
5129 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5130 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
5131 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
5132 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5133 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
5134 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
5135};
5136
5137static const struct nla_policy
5138 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
5139 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
5140 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5141 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
5142 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
5143 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
5144 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
5145 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
5146 .len = IEEE80211_MAX_DATA_LEN },
5147 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
5148};
5149
5150static int nl80211_parse_mesh_config(struct genl_info *info,
5151 struct mesh_config *cfg,
5152 u32 *mask_out)
5153{
5154 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
5155 u32 mask = 0;
5156
5157#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5158do { \
5159 if (tb[attr]) { \
5160 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
5161 return -EINVAL; \
5162 cfg->param = fn(tb[attr]); \
5163 mask |= (1 << (attr - 1)); \
5164 } \
5165} while (0)
5166
5167
5168 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
5169 return -EINVAL;
5170 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
5171 info->attrs[NL80211_ATTR_MESH_CONFIG],
5172 nl80211_meshconf_params_policy))
5173 return -EINVAL;
5174
5175 /* This makes sure that there aren't more than 32 mesh config
5176 * parameters (otherwise our bitfield scheme would not work.) */
5177 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5178
5179 /* Fill in the params struct */
5180 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
5181 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5182 nla_get_u16);
5183 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
5184 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5185 nla_get_u16);
5186 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
5187 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5188 nla_get_u16);
5189 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
5190 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5191 nla_get_u16);
5192 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
5193 mask, NL80211_MESHCONF_MAX_RETRIES,
5194 nla_get_u8);
5195 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
5196 mask, NL80211_MESHCONF_TTL, nla_get_u8);
5197 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
5198 mask, NL80211_MESHCONF_ELEMENT_TTL,
5199 nla_get_u8);
5200 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
5201 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5202 nla_get_u8);
5203 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
5204 1, 255, mask,
5205 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5206 nla_get_u32);
5207 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
5208 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5209 nla_get_u8);
5210 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
5211 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
5212 nla_get_u32);
5213 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
5214 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5215 nla_get_u16);
5216 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
5217 1, 65535, mask,
5218 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5219 nla_get_u32);
5220 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
5221 1, 65535, mask,
5222 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5223 nla_get_u16);
5224 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
5225 1, 65535, mask,
5226 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5227 nla_get_u16);
5228 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5229 dot11MeshHWMPnetDiameterTraversalTime,
5230 1, 65535, mask,
5231 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5232 nla_get_u16);
5233 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
5234 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
5235 nla_get_u8);
5236 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
5237 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5238 nla_get_u16);
5239 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5240 dot11MeshGateAnnouncementProtocol, 0, 1,
5241 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5242 nla_get_u8);
5243 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
5244 mask, NL80211_MESHCONF_FORWARDING,
5245 nla_get_u8);
5246 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
5247 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
5248 nla_get_s32);
5249 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
5250 mask, NL80211_MESHCONF_HT_OPMODE,
5251 nla_get_u16);
5252 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
5253 1, 65535, mask,
5254 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5255 nla_get_u32);
5256 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
5257 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5258 nla_get_u16);
5259 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
5260 dot11MeshHWMPconfirmationInterval,
5261 1, 65535, mask,
5262 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5263 nla_get_u16);
5264 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
5265 NL80211_MESH_POWER_ACTIVE,
5266 NL80211_MESH_POWER_MAX,
5267 mask, NL80211_MESHCONF_POWER_MODE,
5268 nla_get_u32);
5269 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5270 0, 65535, mask,
5271 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
5272 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
5273 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5274 nla_get_u32);
5275 if (mask_out)
5276 *mask_out = mask;
5277
5278 return 0;
5279
5280#undef FILL_IN_MESH_PARAM_IF_SET
5281}
5282
5283static int nl80211_parse_mesh_setup(struct genl_info *info,
5284 struct mesh_setup *setup)
5285{
5286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5287 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
5288
5289 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
5290 return -EINVAL;
5291 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
5292 info->attrs[NL80211_ATTR_MESH_SETUP],
5293 nl80211_mesh_setup_params_policy))
5294 return -EINVAL;
5295
5296 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
5297 setup->sync_method =
5298 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
5299 IEEE80211_SYNC_METHOD_VENDOR :
5300 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
5301
5302 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
5303 setup->path_sel_proto =
5304 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
5305 IEEE80211_PATH_PROTOCOL_VENDOR :
5306 IEEE80211_PATH_PROTOCOL_HWMP;
5307
5308 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
5309 setup->path_metric =
5310 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
5311 IEEE80211_PATH_METRIC_VENDOR :
5312 IEEE80211_PATH_METRIC_AIRTIME;
5313
5314
5315 if (tb[NL80211_MESH_SETUP_IE]) {
5316 struct nlattr *ieattr =
5317 tb[NL80211_MESH_SETUP_IE];
5318 if (!is_valid_ie_attr(ieattr))
5319 return -EINVAL;
5320 setup->ie = nla_data(ieattr);
5321 setup->ie_len = nla_len(ieattr);
5322 }
5323 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
5324 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
5325 return -EINVAL;
5326 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
5327 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
5328 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
5329 if (setup->is_secure)
5330 setup->user_mpm = true;
5331
5332 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
5333 if (!setup->user_mpm)
5334 return -EINVAL;
5335 setup->auth_id =
5336 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
5337 }
5338
5339 return 0;
5340}
5341
5342static int nl80211_update_mesh_config(struct sk_buff *skb,
5343 struct genl_info *info)
5344{
5345 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5346 struct net_device *dev = info->user_ptr[1];
5347 struct wireless_dev *wdev = dev->ieee80211_ptr;
5348 struct mesh_config cfg;
5349 u32 mask;
5350 int err;
5351
5352 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5353 return -EOPNOTSUPP;
5354
5355 if (!rdev->ops->update_mesh_config)
5356 return -EOPNOTSUPP;
5357
5358 err = nl80211_parse_mesh_config(info, &cfg, &mask);
5359 if (err)
5360 return err;
5361
5362 wdev_lock(wdev);
5363 if (!wdev->mesh_id_len)
5364 err = -ENOLINK;
5365
5366 if (!err)
5367 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
5368
5369 wdev_unlock(wdev);
5370
5371 return err;
5372}
5373
5374static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
5375 struct sk_buff *msg)
5376{
5377 struct nlattr *nl_reg_rules;
5378 unsigned int i;
5379
5380 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5381 (regdom->dfs_region &&
5382 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5383 goto nla_put_failure;
5384
5385 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5386 if (!nl_reg_rules)
5387 goto nla_put_failure;
5388
5389 for (i = 0; i < regdom->n_reg_rules; i++) {
5390 struct nlattr *nl_reg_rule;
5391 const struct ieee80211_reg_rule *reg_rule;
5392 const struct ieee80211_freq_range *freq_range;
5393 const struct ieee80211_power_rule *power_rule;
5394 unsigned int max_bandwidth_khz;
5395
5396 reg_rule = &regdom->reg_rules[i];
5397 freq_range = &reg_rule->freq_range;
5398 power_rule = &reg_rule->power_rule;
5399
5400 nl_reg_rule = nla_nest_start(msg, i);
5401 if (!nl_reg_rule)
5402 goto nla_put_failure;
5403
5404 max_bandwidth_khz = freq_range->max_bandwidth_khz;
5405 if (!max_bandwidth_khz)
5406 max_bandwidth_khz = reg_get_max_bandwidth(regdom,
5407 reg_rule);
5408
5409 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5410 reg_rule->flags) ||
5411 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5412 freq_range->start_freq_khz) ||
5413 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5414 freq_range->end_freq_khz) ||
5415 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5416 max_bandwidth_khz) ||
5417 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5418 power_rule->max_antenna_gain) ||
5419 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5420 power_rule->max_eirp) ||
5421 nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
5422 reg_rule->dfs_cac_ms))
5423 goto nla_put_failure;
5424
5425 nla_nest_end(msg, nl_reg_rule);
5426 }
5427
5428 nla_nest_end(msg, nl_reg_rules);
5429 return 0;
5430
5431nla_put_failure:
5432 return -EMSGSIZE;
5433}
5434
5435static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
5436{
5437 const struct ieee80211_regdomain *regdom = NULL;
5438 struct cfg80211_registered_device *rdev;
5439 struct wiphy *wiphy = NULL;
5440 struct sk_buff *msg;
5441 void *hdr;
5442
5443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5444 if (!msg)
5445 return -ENOBUFS;
5446
5447 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5448 NL80211_CMD_GET_REG);
5449 if (!hdr)
5450 goto put_failure;
5451
5452 if (info->attrs[NL80211_ATTR_WIPHY]) {
5453 bool self_managed;
5454
5455 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
5456 if (IS_ERR(rdev)) {
5457 nlmsg_free(msg);
5458 return PTR_ERR(rdev);
5459 }
5460
5461 wiphy = &rdev->wiphy;
5462 self_managed = wiphy->regulatory_flags &
5463 REGULATORY_WIPHY_SELF_MANAGED;
5464 regdom = get_wiphy_regdom(wiphy);
5465
5466 /* a self-managed-reg device must have a private regdom */
5467 if (WARN_ON(!regdom && self_managed)) {
5468 nlmsg_free(msg);
5469 return -EINVAL;
5470 }
5471
5472 if (regdom &&
5473 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
5474 goto nla_put_failure;
5475 }
5476
5477 if (!wiphy && reg_last_request_cell_base() &&
5478 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5479 NL80211_USER_REG_HINT_CELL_BASE))
5480 goto nla_put_failure;
5481
5482 rcu_read_lock();
5483
5484 if (!regdom)
5485 regdom = rcu_dereference(cfg80211_regdomain);
5486
5487 if (nl80211_put_regdom(regdom, msg))
5488 goto nla_put_failure_rcu;
5489
5490 rcu_read_unlock();
5491
5492 genlmsg_end(msg, hdr);
5493 return genlmsg_reply(msg, info);
5494
5495nla_put_failure_rcu:
5496 rcu_read_unlock();
5497nla_put_failure:
5498 genlmsg_cancel(msg, hdr);
5499put_failure:
5500 nlmsg_free(msg);
5501 return -EMSGSIZE;
5502}
5503
5504static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
5505 u32 seq, int flags, struct wiphy *wiphy,
5506 const struct ieee80211_regdomain *regdom)
5507{
5508 void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
5509 NL80211_CMD_GET_REG);
5510
5511 if (!hdr)
5512 return -1;
5513
5514 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5515
5516 if (nl80211_put_regdom(regdom, msg))
5517 goto nla_put_failure;
5518
5519 if (!wiphy && reg_last_request_cell_base() &&
5520 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5521 NL80211_USER_REG_HINT_CELL_BASE))
5522 goto nla_put_failure;
5523
5524 if (wiphy &&
5525 nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
5526 goto nla_put_failure;
5527
5528 if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
5529 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
5530 goto nla_put_failure;
5531
5532 genlmsg_end(msg, hdr);
5533 return 0;
5534
5535nla_put_failure:
5536 genlmsg_cancel(msg, hdr);
5537 return -EMSGSIZE;
5538}
5539
5540static int nl80211_get_reg_dump(struct sk_buff *skb,
5541 struct netlink_callback *cb)
5542{
5543 const struct ieee80211_regdomain *regdom = NULL;
5544 struct cfg80211_registered_device *rdev;
5545 int err, reg_idx, start = cb->args[2];
5546
5547 rtnl_lock();
5548
5549 if (cfg80211_regdomain && start == 0) {
5550 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
5551 NLM_F_MULTI, NULL,
5552 rtnl_dereference(cfg80211_regdomain));
5553 if (err < 0)
5554 goto out_err;
5555 }
5556
5557 /* the global regdom is idx 0 */
5558 reg_idx = 1;
5559 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
5560 regdom = get_wiphy_regdom(&rdev->wiphy);
5561 if (!regdom)
5562 continue;
5563
5564 if (++reg_idx <= start)
5565 continue;
5566
5567 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
5568 NLM_F_MULTI, &rdev->wiphy, regdom);
5569 if (err < 0) {
5570 reg_idx--;
5571 break;
5572 }
5573 }
5574
5575 cb->args[2] = reg_idx;
5576 err = skb->len;
5577out_err:
5578 rtnl_unlock();
5579 return err;
5580}
5581
5582#ifdef CONFIG_CFG80211_CRDA_SUPPORT
5583static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
5584 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5585 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5586 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5587 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5588 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5589 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5590 [NL80211_ATTR_DFS_CAC_TIME] = { .type = NLA_U32 },
5591};
5592
5593static int parse_reg_rule(struct nlattr *tb[],
5594 struct ieee80211_reg_rule *reg_rule)
5595{
5596 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
5597 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
5598
5599 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
5600 return -EINVAL;
5601 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
5602 return -EINVAL;
5603 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
5604 return -EINVAL;
5605 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
5606 return -EINVAL;
5607 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
5608 return -EINVAL;
5609
5610 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
5611
5612 freq_range->start_freq_khz =
5613 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
5614 freq_range->end_freq_khz =
5615 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
5616 freq_range->max_bandwidth_khz =
5617 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
5618
5619 power_rule->max_eirp =
5620 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
5621
5622 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
5623 power_rule->max_antenna_gain =
5624 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
5625
5626 if (tb[NL80211_ATTR_DFS_CAC_TIME])
5627 reg_rule->dfs_cac_ms =
5628 nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
5629
5630 return 0;
5631}
5632
5633static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5634{
5635 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5636 struct nlattr *nl_reg_rule;
5637 char *alpha2;
5638 int rem_reg_rules, r;
5639 u32 num_rules = 0, rule_idx = 0, size_of_regd;
5640 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
5641 struct ieee80211_regdomain *rd;
5642
5643 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5644 return -EINVAL;
5645
5646 if (!info->attrs[NL80211_ATTR_REG_RULES])
5647 return -EINVAL;
5648
5649 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5650
5651 if (info->attrs[NL80211_ATTR_DFS_REGION])
5652 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5653
5654 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5655 rem_reg_rules) {
5656 num_rules++;
5657 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
5658 return -EINVAL;
5659 }
5660
5661 if (!reg_is_valid_request(alpha2))
5662 return -EINVAL;
5663
5664 size_of_regd = sizeof(struct ieee80211_regdomain) +
5665 num_rules * sizeof(struct ieee80211_reg_rule);
5666
5667 rd = kzalloc(size_of_regd, GFP_KERNEL);
5668 if (!rd)
5669 return -ENOMEM;
5670
5671 rd->n_reg_rules = num_rules;
5672 rd->alpha2[0] = alpha2[0];
5673 rd->alpha2[1] = alpha2[1];
5674
5675 /*
5676 * Disable DFS master mode if the DFS region was
5677 * not supported or known on this kernel.
5678 */
5679 if (reg_supported_dfs_region(dfs_region))
5680 rd->dfs_region = dfs_region;
5681
5682 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
5683 rem_reg_rules) {
5684 r = nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
5685 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5686 reg_rule_policy);
5687 if (r)
5688 goto bad_reg;
5689 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5690 if (r)
5691 goto bad_reg;
5692
5693 rule_idx++;
5694
5695 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5696 r = -EINVAL;
5697 goto bad_reg;
5698 }
5699 }
5700
5701 r = set_regdom(rd, REGD_SOURCE_CRDA);
5702 /* set_regdom took ownership */
5703 rd = NULL;
5704
5705 bad_reg:
5706 kfree(rd);
5707 return r;
5708}
5709#endif /* CONFIG_CFG80211_CRDA_SUPPORT */
5710
5711static int validate_scan_freqs(struct nlattr *freqs)
5712{
5713 struct nlattr *attr1, *attr2;
5714 int n_channels = 0, tmp1, tmp2;
5715
5716 nla_for_each_nested(attr1, freqs, tmp1)
5717 if (nla_len(attr1) != sizeof(u32))
5718 return 0;
5719
5720 nla_for_each_nested(attr1, freqs, tmp1) {
5721 n_channels++;
5722 /*
5723 * Some hardware has a limited channel list for
5724 * scanning, and it is pretty much nonsensical
5725 * to scan for a channel twice, so disallow that
5726 * and don't require drivers to check that the
5727 * channel list they get isn't longer than what
5728 * they can scan, as long as they can scan all
5729 * the channels they registered at once.
5730 */
5731 nla_for_each_nested(attr2, freqs, tmp2)
5732 if (attr1 != attr2 &&
5733 nla_get_u32(attr1) == nla_get_u32(attr2))
5734 return 0;
5735 }
5736
5737 return n_channels;
5738}
5739
5740static int nl80211_parse_random_mac(struct nlattr **attrs,
5741 u8 *mac_addr, u8 *mac_addr_mask)
5742{
5743 int i;
5744
5745 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
5746 eth_zero_addr(mac_addr);
5747 eth_zero_addr(mac_addr_mask);
5748 mac_addr[0] = 0x2;
5749 mac_addr_mask[0] = 0x3;
5750
5751 return 0;
5752 }
5753
5754 /* need both or none */
5755 if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
5756 return -EINVAL;
5757
5758 memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
5759 memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
5760
5761 /* don't allow or configure an mcast address */
5762 if (!is_multicast_ether_addr(mac_addr_mask) ||
5763 is_multicast_ether_addr(mac_addr))
5764 return -EINVAL;
5765
5766 /*
5767 * allow users to pass a MAC address that has bits set outside
5768 * of the mask, but don't bother drivers with having to deal
5769 * with such bits
5770 */
5771 for (i = 0; i < ETH_ALEN; i++)
5772 mac_addr[i] &= mac_addr_mask[i];
5773
5774 return 0;
5775}
5776
5777static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5778{
5779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5780 struct wireless_dev *wdev = info->user_ptr[1];
5781 struct cfg80211_scan_request *request;
5782 struct nlattr *attr;
5783 struct wiphy *wiphy;
5784 int err, tmp, n_ssids = 0, n_channels, i;
5785 size_t ie_len;
5786
5787 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5788 return -EINVAL;
5789
5790 wiphy = &rdev->wiphy;
5791
5792 if (!rdev->ops->scan)
5793 return -EOPNOTSUPP;
5794
5795 if (rdev->scan_req || rdev->scan_msg) {
5796 err = -EBUSY;
5797 goto unlock;
5798 }
5799
5800 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5801 n_channels = validate_scan_freqs(
5802 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5803 if (!n_channels) {
5804 err = -EINVAL;
5805 goto unlock;
5806 }
5807 } else {
5808 n_channels = ieee80211_get_num_supported_channels(wiphy);
5809 }
5810
5811 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5812 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5813 n_ssids++;
5814
5815 if (n_ssids > wiphy->max_scan_ssids) {
5816 err = -EINVAL;
5817 goto unlock;
5818 }
5819
5820 if (info->attrs[NL80211_ATTR_IE])
5821 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5822 else
5823 ie_len = 0;
5824
5825 if (ie_len > wiphy->max_scan_ie_len) {
5826 err = -EINVAL;
5827 goto unlock;
5828 }
5829
5830 request = kzalloc(sizeof(*request)
5831 + sizeof(*request->ssids) * n_ssids
5832 + sizeof(*request->channels) * n_channels
5833 + ie_len, GFP_KERNEL);
5834 if (!request) {
5835 err = -ENOMEM;
5836 goto unlock;
5837 }
5838
5839 if (n_ssids)
5840 request->ssids = (void *)&request->channels[n_channels];
5841 request->n_ssids = n_ssids;
5842 if (ie_len) {
5843 if (n_ssids)
5844 request->ie = (void *)(request->ssids + n_ssids);
5845 else
5846 request->ie = (void *)(request->channels + n_channels);
5847 }
5848
5849 i = 0;
5850 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5851 /* user specified, bail out if channel not found */
5852 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
5853 struct ieee80211_channel *chan;
5854
5855 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5856
5857 if (!chan) {
5858 err = -EINVAL;
5859 goto out_free;
5860 }
5861
5862 /* ignore disabled channels */
5863 if (chan->flags & IEEE80211_CHAN_DISABLED)
5864 continue;
5865
5866 request->channels[i] = chan;
5867 i++;
5868 }
5869 } else {
5870 enum ieee80211_band band;
5871
5872 /* all channels */
5873 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5874 int j;
5875 if (!wiphy->bands[band])
5876 continue;
5877 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5878 struct ieee80211_channel *chan;
5879
5880 chan = &wiphy->bands[band]->channels[j];
5881
5882 if (chan->flags & IEEE80211_CHAN_DISABLED)
5883 continue;
5884
5885 request->channels[i] = chan;
5886 i++;
5887 }
5888 }
5889 }
5890
5891 if (!i) {
5892 err = -EINVAL;
5893 goto out_free;
5894 }
5895
5896 request->n_channels = i;
5897
5898 i = 0;
5899 if (n_ssids) {
5900 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
5901 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
5902 err = -EINVAL;
5903 goto out_free;
5904 }
5905 request->ssids[i].ssid_len = nla_len(attr);
5906 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
5907 i++;
5908 }
5909 }
5910
5911 if (info->attrs[NL80211_ATTR_IE]) {
5912 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5913 memcpy((void *)request->ie,
5914 nla_data(info->attrs[NL80211_ATTR_IE]),
5915 request->ie_len);
5916 }
5917
5918 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
5919 if (wiphy->bands[i])
5920 request->rates[i] =
5921 (1 << wiphy->bands[i]->n_bitrates) - 1;
5922
5923 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5924 nla_for_each_nested(attr,
5925 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5926 tmp) {
5927 enum ieee80211_band band = nla_type(attr);
5928
5929 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
5930 err = -EINVAL;
5931 goto out_free;
5932 }
5933
5934 if (!wiphy->bands[band])
5935 continue;
5936
5937 err = ieee80211_get_ratemask(wiphy->bands[band],
5938 nla_data(attr),
5939 nla_len(attr),
5940 &request->rates[band]);
5941 if (err)
5942 goto out_free;
5943 }
5944 }
5945
5946 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
5947 request->flags = nla_get_u32(
5948 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
5949 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5950 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
5951 err = -EOPNOTSUPP;
5952 goto out_free;
5953 }
5954
5955 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
5956 if (!(wiphy->features &
5957 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
5958 err = -EOPNOTSUPP;
5959 goto out_free;
5960 }
5961
5962 if (wdev->current_bss) {
5963 err = -EOPNOTSUPP;
5964 goto out_free;
5965 }
5966
5967 err = nl80211_parse_random_mac(info->attrs,
5968 request->mac_addr,
5969 request->mac_addr_mask);
5970 if (err)
5971 goto out_free;
5972 }
5973 }
5974
5975 request->no_cck =
5976 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5977
5978 request->wdev = wdev;
5979 request->wiphy = &rdev->wiphy;
5980 request->scan_start = jiffies;
5981
5982 rdev->scan_req = request;
5983 err = rdev_scan(rdev, request);
5984
5985 if (!err) {
5986 nl80211_send_scan_start(rdev, wdev);
5987 if (wdev->netdev)
5988 dev_hold(wdev->netdev);
5989 } else {
5990 out_free:
5991 rdev->scan_req = NULL;
5992 kfree(request);
5993 }
5994
5995 unlock:
5996 return err;
5997}
5998
5999static int
6000nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
6001 struct cfg80211_sched_scan_request *request,
6002 struct nlattr **attrs)
6003{
6004 int tmp, err, i = 0;
6005 struct nlattr *attr;
6006
6007 if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6008 u32 interval;
6009
6010 /*
6011 * If scan plans are not specified,
6012 * %NL80211_ATTR_SCHED_SCAN_INTERVAL must be specified. In this
6013 * case one scan plan will be set with the specified scan
6014 * interval and infinite number of iterations.
6015 */
6016 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6017 return -EINVAL;
6018
6019 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
6020 if (!interval)
6021 return -EINVAL;
6022
6023 request->scan_plans[0].interval =
6024 DIV_ROUND_UP(interval, MSEC_PER_SEC);
6025 if (!request->scan_plans[0].interval)
6026 return -EINVAL;
6027
6028 if (request->scan_plans[0].interval >
6029 wiphy->max_sched_scan_plan_interval)
6030 request->scan_plans[0].interval =
6031 wiphy->max_sched_scan_plan_interval;
6032
6033 return 0;
6034 }
6035
6036 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
6037 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
6038
6039 if (WARN_ON(i >= n_plans))
6040 return -EINVAL;
6041
6042 err = nla_parse(plan, NL80211_SCHED_SCAN_PLAN_MAX,
6043 nla_data(attr), nla_len(attr),
6044 nl80211_plan_policy);
6045 if (err)
6046 return err;
6047
6048 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
6049 return -EINVAL;
6050
6051 request->scan_plans[i].interval =
6052 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
6053 if (!request->scan_plans[i].interval ||
6054 request->scan_plans[i].interval >
6055 wiphy->max_sched_scan_plan_interval)
6056 return -EINVAL;
6057
6058 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
6059 request->scan_plans[i].iterations =
6060 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
6061 if (!request->scan_plans[i].iterations ||
6062 (request->scan_plans[i].iterations >
6063 wiphy->max_sched_scan_plan_iterations))
6064 return -EINVAL;
6065 } else if (i < n_plans - 1) {
6066 /*
6067 * All scan plans but the last one must specify
6068 * a finite number of iterations
6069 */
6070 return -EINVAL;
6071 }
6072
6073 i++;
6074 }
6075
6076 /*
6077 * The last scan plan must not specify the number of
6078 * iterations, it is supposed to run infinitely
6079 */
6080 if (request->scan_plans[n_plans - 1].iterations)
6081 return -EINVAL;
6082
6083 return 0;
6084}
6085
6086static struct cfg80211_sched_scan_request *
6087nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
6088 struct nlattr **attrs)
6089{
6090 struct cfg80211_sched_scan_request *request;
6091 struct nlattr *attr;
6092 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
6093 enum ieee80211_band band;
6094 size_t ie_len;
6095 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
6096 s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
6097
6098 if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
6099 return ERR_PTR(-EINVAL);
6100
6101 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6102 n_channels = validate_scan_freqs(
6103 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
6104 if (!n_channels)
6105 return ERR_PTR(-EINVAL);
6106 } else {
6107 n_channels = ieee80211_get_num_supported_channels(wiphy);
6108 }
6109
6110 if (attrs[NL80211_ATTR_SCAN_SSIDS])
6111 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
6112 tmp)
6113 n_ssids++;
6114
6115 if (n_ssids > wiphy->max_sched_scan_ssids)
6116 return ERR_PTR(-EINVAL);
6117
6118 /*
6119 * First, count the number of 'real' matchsets. Due to an issue with
6120 * the old implementation, matchsets containing only the RSSI attribute
6121 * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
6122 * RSSI for all matchsets, rather than their own matchset for reporting
6123 * all APs with a strong RSSI. This is needed to be compatible with
6124 * older userspace that treated a matchset with only the RSSI as the
6125 * global RSSI for all other matchsets - if there are other matchsets.
6126 */
6127 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
6128 nla_for_each_nested(attr,
6129 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
6130 tmp) {
6131 struct nlattr *rssi;
6132
6133 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
6134 nla_data(attr), nla_len(attr),
6135 nl80211_match_policy);
6136 if (err)
6137 return ERR_PTR(err);
6138 /* add other standalone attributes here */
6139 if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID]) {
6140 n_match_sets++;
6141 continue;
6142 }
6143 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
6144 if (rssi)
6145 default_match_rssi = nla_get_s32(rssi);
6146 }
6147 }
6148
6149 /* However, if there's no other matchset, add the RSSI one */
6150 if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
6151 n_match_sets = 1;
6152
6153 if (n_match_sets > wiphy->max_match_sets)
6154 return ERR_PTR(-EINVAL);
6155
6156 if (attrs[NL80211_ATTR_IE])
6157 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
6158 else
6159 ie_len = 0;
6160
6161 if (ie_len > wiphy->max_sched_scan_ie_len)
6162 return ERR_PTR(-EINVAL);
6163
6164 if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6165 /*
6166 * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
6167 * each scan plan already specifies its own interval
6168 */
6169 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6170 return ERR_PTR(-EINVAL);
6171
6172 nla_for_each_nested(attr,
6173 attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
6174 n_plans++;
6175 } else {
6176 /*
6177 * The scan interval attribute is kept for backward
6178 * compatibility. If no scan plans are specified and sched scan
6179 * interval is specified, one scan plan will be set with this
6180 * scan interval and infinite number of iterations.
6181 */
6182 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
6183 return ERR_PTR(-EINVAL);
6184
6185 n_plans = 1;
6186 }
6187
6188 if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
6189 return ERR_PTR(-EINVAL);
6190
6191 request = kzalloc(sizeof(*request)
6192 + sizeof(*request->ssids) * n_ssids
6193 + sizeof(*request->match_sets) * n_match_sets
6194 + sizeof(*request->scan_plans) * n_plans
6195 + sizeof(*request->channels) * n_channels
6196 + ie_len, GFP_KERNEL);
6197 if (!request)
6198 return ERR_PTR(-ENOMEM);
6199
6200 if (n_ssids)
6201 request->ssids = (void *)&request->channels[n_channels];
6202 request->n_ssids = n_ssids;
6203 if (ie_len) {
6204 if (n_ssids)
6205 request->ie = (void *)(request->ssids + n_ssids);
6206 else
6207 request->ie = (void *)(request->channels + n_channels);
6208 }
6209
6210 if (n_match_sets) {
6211 if (request->ie)
6212 request->match_sets = (void *)(request->ie + ie_len);
6213 else if (n_ssids)
6214 request->match_sets =
6215 (void *)(request->ssids + n_ssids);
6216 else
6217 request->match_sets =
6218 (void *)(request->channels + n_channels);
6219 }
6220 request->n_match_sets = n_match_sets;
6221
6222 if (n_match_sets)
6223 request->scan_plans = (void *)(request->match_sets +
6224 n_match_sets);
6225 else if (request->ie)
6226 request->scan_plans = (void *)(request->ie + ie_len);
6227 else if (n_ssids)
6228 request->scan_plans = (void *)(request->ssids + n_ssids);
6229 else
6230 request->scan_plans = (void *)(request->channels + n_channels);
6231
6232 request->n_scan_plans = n_plans;
6233
6234 i = 0;
6235 if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6236 /* user specified, bail out if channel not found */
6237 nla_for_each_nested(attr,
6238 attrs[NL80211_ATTR_SCAN_FREQUENCIES],
6239 tmp) {
6240 struct ieee80211_channel *chan;
6241
6242 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6243
6244 if (!chan) {
6245 err = -EINVAL;
6246 goto out_free;
6247 }
6248
6249 /* ignore disabled channels */
6250 if (chan->flags & IEEE80211_CHAN_DISABLED)
6251 continue;
6252
6253 request->channels[i] = chan;
6254 i++;
6255 }
6256 } else {
6257 /* all channels */
6258 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6259 int j;
6260 if (!wiphy->bands[band])
6261 continue;
6262 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
6263 struct ieee80211_channel *chan;
6264
6265 chan = &wiphy->bands[band]->channels[j];
6266
6267 if (chan->flags & IEEE80211_CHAN_DISABLED)
6268 continue;
6269
6270 request->channels[i] = chan;
6271 i++;
6272 }
6273 }
6274 }
6275
6276 if (!i) {
6277 err = -EINVAL;
6278 goto out_free;
6279 }
6280
6281 request->n_channels = i;
6282
6283 i = 0;
6284 if (n_ssids) {
6285 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
6286 tmp) {
6287 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
6288 err = -EINVAL;
6289 goto out_free;
6290 }
6291 request->ssids[i].ssid_len = nla_len(attr);
6292 memcpy(request->ssids[i].ssid, nla_data(attr),
6293 nla_len(attr));
6294 i++;
6295 }
6296 }
6297
6298 i = 0;
6299 if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
6300 nla_for_each_nested(attr,
6301 attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
6302 tmp) {
6303 struct nlattr *ssid, *rssi;
6304
6305 err = nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
6306 nla_data(attr), nla_len(attr),
6307 nl80211_match_policy);
6308 if (err)
6309 goto out_free;
6310 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
6311 if (ssid) {
6312 if (WARN_ON(i >= n_match_sets)) {
6313 /* this indicates a programming error,
6314 * the loop above should have verified
6315 * things properly
6316 */
6317 err = -EINVAL;
6318 goto out_free;
6319 }
6320
6321 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
6322 err = -EINVAL;
6323 goto out_free;
6324 }
6325 memcpy(request->match_sets[i].ssid.ssid,
6326 nla_data(ssid), nla_len(ssid));
6327 request->match_sets[i].ssid.ssid_len =
6328 nla_len(ssid);
6329 /* special attribute - old implemenation w/a */
6330 request->match_sets[i].rssi_thold =
6331 default_match_rssi;
6332 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
6333 if (rssi)
6334 request->match_sets[i].rssi_thold =
6335 nla_get_s32(rssi);
6336 }
6337 i++;
6338 }
6339
6340 /* there was no other matchset, so the RSSI one is alone */
6341 if (i == 0 && n_match_sets)
6342 request->match_sets[0].rssi_thold = default_match_rssi;
6343
6344 request->min_rssi_thold = INT_MAX;
6345 for (i = 0; i < n_match_sets; i++)
6346 request->min_rssi_thold =
6347 min(request->match_sets[i].rssi_thold,
6348 request->min_rssi_thold);
6349 } else {
6350 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
6351 }
6352
6353 if (ie_len) {
6354 request->ie_len = ie_len;
6355 memcpy((void *)request->ie,
6356 nla_data(attrs[NL80211_ATTR_IE]),
6357 request->ie_len);
6358 }
6359
6360 if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
6361 request->flags = nla_get_u32(
6362 attrs[NL80211_ATTR_SCAN_FLAGS]);
6363 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6364 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
6365 err = -EOPNOTSUPP;
6366 goto out_free;
6367 }
6368
6369 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6370 u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
6371
6372 if (!wdev) /* must be net-detect */
6373 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
6374
6375 if (!(wiphy->features & flg)) {
6376 err = -EOPNOTSUPP;
6377 goto out_free;
6378 }
6379
6380 if (wdev && wdev->current_bss) {
6381 err = -EOPNOTSUPP;
6382 goto out_free;
6383 }
6384
6385 err = nl80211_parse_random_mac(attrs, request->mac_addr,
6386 request->mac_addr_mask);
6387 if (err)
6388 goto out_free;
6389 }
6390 }
6391
6392 if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
6393 request->delay =
6394 nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
6395
6396 err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
6397 if (err)
6398 goto out_free;
6399
6400 request->scan_start = jiffies;
6401
6402 return request;
6403
6404out_free:
6405 kfree(request);
6406 return ERR_PTR(err);
6407}
6408
6409static int nl80211_start_sched_scan(struct sk_buff *skb,
6410 struct genl_info *info)
6411{
6412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6413 struct net_device *dev = info->user_ptr[1];
6414 struct wireless_dev *wdev = dev->ieee80211_ptr;
6415 struct cfg80211_sched_scan_request *sched_scan_req;
6416 int err;
6417
6418 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6419 !rdev->ops->sched_scan_start)
6420 return -EOPNOTSUPP;
6421
6422 if (rdev->sched_scan_req)
6423 return -EINPROGRESS;
6424
6425 sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
6426 info->attrs);
6427
6428 err = PTR_ERR_OR_ZERO(sched_scan_req);
6429 if (err)
6430 goto out_err;
6431
6432 err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
6433 if (err)
6434 goto out_free;
6435
6436 sched_scan_req->dev = dev;
6437 sched_scan_req->wiphy = &rdev->wiphy;
6438
6439 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6440 sched_scan_req->owner_nlportid = info->snd_portid;
6441
6442 rcu_assign_pointer(rdev->sched_scan_req, sched_scan_req);
6443
6444 nl80211_send_sched_scan(rdev, dev,
6445 NL80211_CMD_START_SCHED_SCAN);
6446 return 0;
6447
6448out_free:
6449 kfree(sched_scan_req);
6450out_err:
6451 return err;
6452}
6453
6454static int nl80211_stop_sched_scan(struct sk_buff *skb,
6455 struct genl_info *info)
6456{
6457 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6458
6459 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
6460 !rdev->ops->sched_scan_stop)
6461 return -EOPNOTSUPP;
6462
6463 return __cfg80211_stop_sched_scan(rdev, false);
6464}
6465
6466static int nl80211_start_radar_detection(struct sk_buff *skb,
6467 struct genl_info *info)
6468{
6469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6470 struct net_device *dev = info->user_ptr[1];
6471 struct wireless_dev *wdev = dev->ieee80211_ptr;
6472 struct cfg80211_chan_def chandef;
6473 enum nl80211_dfs_regions dfs_region;
6474 unsigned int cac_time_ms;
6475 int err;
6476
6477 dfs_region = reg_get_dfs_region(wdev->wiphy);
6478 if (dfs_region == NL80211_DFS_UNSET)
6479 return -EINVAL;
6480
6481 err = nl80211_parse_chandef(rdev, info, &chandef);
6482 if (err)
6483 return err;
6484
6485 if (netif_carrier_ok(dev))
6486 return -EBUSY;
6487
6488 if (wdev->cac_started)
6489 return -EBUSY;
6490
6491 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
6492 wdev->iftype);
6493 if (err < 0)
6494 return err;
6495
6496 if (err == 0)
6497 return -EINVAL;
6498
6499 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
6500 return -EINVAL;
6501
6502 if (!rdev->ops->start_radar_detection)
6503 return -EOPNOTSUPP;
6504
6505 cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
6506 if (WARN_ON(!cac_time_ms))
6507 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
6508
6509 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef,
6510 cac_time_ms);
6511 if (!err) {
6512 wdev->chandef = chandef;
6513 wdev->cac_started = true;
6514 wdev->cac_start_time = jiffies;
6515 wdev->cac_time_ms = cac_time_ms;
6516 }
6517 return err;
6518}
6519
6520static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
6521{
6522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6523 struct net_device *dev = info->user_ptr[1];
6524 struct wireless_dev *wdev = dev->ieee80211_ptr;
6525 struct cfg80211_csa_settings params;
6526 /* csa_attrs is defined static to avoid waste of stack size - this
6527 * function is called under RTNL lock, so this should not be a problem.
6528 */
6529 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
6530 int err;
6531 bool need_new_beacon = false;
6532 int len, i;
6533 u32 cs_count;
6534
6535 if (!rdev->ops->channel_switch ||
6536 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
6537 return -EOPNOTSUPP;
6538
6539 switch (dev->ieee80211_ptr->iftype) {
6540 case NL80211_IFTYPE_AP:
6541 case NL80211_IFTYPE_P2P_GO:
6542 need_new_beacon = true;
6543
6544 /* useless if AP is not running */
6545 if (!wdev->beacon_interval)
6546 return -ENOTCONN;
6547 break;
6548 case NL80211_IFTYPE_ADHOC:
6549 if (!wdev->ssid_len)
6550 return -ENOTCONN;
6551 break;
6552 case NL80211_IFTYPE_MESH_POINT:
6553 if (!wdev->mesh_id_len)
6554 return -ENOTCONN;
6555 break;
6556 default:
6557 return -EOPNOTSUPP;
6558 }
6559
6560 memset(&params, 0, sizeof(params));
6561
6562 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6563 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
6564 return -EINVAL;
6565
6566 /* only important for AP, IBSS and mesh create IEs internally */
6567 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
6568 return -EINVAL;
6569
6570 /* Even though the attribute is u32, the specification says
6571 * u8, so let's make sure we don't overflow.
6572 */
6573 cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
6574 if (cs_count > 255)
6575 return -EINVAL;
6576
6577 params.count = cs_count;
6578
6579 if (!need_new_beacon)
6580 goto skip_beacons;
6581
6582 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
6583 if (err)
6584 return err;
6585
6586 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
6587 info->attrs[NL80211_ATTR_CSA_IES],
6588 nl80211_policy);
6589 if (err)
6590 return err;
6591
6592 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
6593 if (err)
6594 return err;
6595
6596 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
6597 return -EINVAL;
6598
6599 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6600 if (!len || (len % sizeof(u16)))
6601 return -EINVAL;
6602
6603 params.n_counter_offsets_beacon = len / sizeof(u16);
6604 if (rdev->wiphy.max_num_csa_counters &&
6605 (params.n_counter_offsets_beacon >
6606 rdev->wiphy.max_num_csa_counters))
6607 return -EINVAL;
6608
6609 params.counter_offsets_beacon =
6610 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
6611
6612 /* sanity checks - counters should fit and be the same */
6613 for (i = 0; i < params.n_counter_offsets_beacon; i++) {
6614 u16 offset = params.counter_offsets_beacon[i];
6615
6616 if (offset >= params.beacon_csa.tail_len)
6617 return -EINVAL;
6618
6619 if (params.beacon_csa.tail[offset] != params.count)
6620 return -EINVAL;
6621 }
6622
6623 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
6624 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6625 if (!len || (len % sizeof(u16)))
6626 return -EINVAL;
6627
6628 params.n_counter_offsets_presp = len / sizeof(u16);
6629 if (rdev->wiphy.max_num_csa_counters &&
6630 (params.n_counter_offsets_presp >
6631 rdev->wiphy.max_num_csa_counters))
6632 return -EINVAL;
6633
6634 params.counter_offsets_presp =
6635 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
6636
6637 /* sanity checks - counters should fit and be the same */
6638 for (i = 0; i < params.n_counter_offsets_presp; i++) {
6639 u16 offset = params.counter_offsets_presp[i];
6640
6641 if (offset >= params.beacon_csa.probe_resp_len)
6642 return -EINVAL;
6643
6644 if (params.beacon_csa.probe_resp[offset] !=
6645 params.count)
6646 return -EINVAL;
6647 }
6648 }
6649
6650skip_beacons:
6651 err = nl80211_parse_chandef(rdev, info, &params.chandef);
6652 if (err)
6653 return err;
6654
6655 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
6656 wdev->iftype))
6657 return -EINVAL;
6658
6659 err = cfg80211_chandef_dfs_required(wdev->wiphy,
6660 &params.chandef,
6661 wdev->iftype);
6662 if (err < 0)
6663 return err;
6664
6665 if (err > 0)
6666 params.radar_required = true;
6667
6668 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
6669 params.block_tx = true;
6670
6671 wdev_lock(wdev);
6672 err = rdev_channel_switch(rdev, dev, &params);
6673 wdev_unlock(wdev);
6674
6675 return err;
6676}
6677
6678static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
6679 u32 seq, int flags,
6680 struct cfg80211_registered_device *rdev,
6681 struct wireless_dev *wdev,
6682 struct cfg80211_internal_bss *intbss)
6683{
6684 struct cfg80211_bss *res = &intbss->pub;
6685 const struct cfg80211_bss_ies *ies;
6686 void *hdr;
6687 struct nlattr *bss;
6688
6689 ASSERT_WDEV_LOCK(wdev);
6690
6691 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6692 NL80211_CMD_NEW_SCAN_RESULTS);
6693 if (!hdr)
6694 return -1;
6695
6696 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6697
6698 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
6699 goto nla_put_failure;
6700 if (wdev->netdev &&
6701 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
6702 goto nla_put_failure;
6703 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
6704 goto nla_put_failure;
6705
6706 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
6707 if (!bss)
6708 goto nla_put_failure;
6709 if ((!is_zero_ether_addr(res->bssid) &&
6710 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
6711 goto nla_put_failure;
6712
6713 rcu_read_lock();
6714 /* indicate whether we have probe response data or not */
6715 if (rcu_access_pointer(res->proberesp_ies) &&
6716 nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
6717 goto fail_unlock_rcu;
6718
6719 /* this pointer prefers to be pointed to probe response data
6720 * but is always valid
6721 */
6722 ies = rcu_dereference(res->ies);
6723 if (ies) {
6724 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
6725 goto fail_unlock_rcu;
6726 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
6727 ies->len, ies->data))
6728 goto fail_unlock_rcu;
6729 }
6730
6731 /* and this pointer is always (unless driver didn't know) beacon data */
6732 ies = rcu_dereference(res->beacon_ies);
6733 if (ies && ies->from_beacon) {
6734 if (nla_put_u64(msg, NL80211_BSS_BEACON_TSF, ies->tsf))
6735 goto fail_unlock_rcu;
6736 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
6737 ies->len, ies->data))
6738 goto fail_unlock_rcu;
6739 }
6740 rcu_read_unlock();
6741
6742 if (res->beacon_interval &&
6743 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
6744 goto nla_put_failure;
6745 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
6746 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
6747 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
6748 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
6749 jiffies_to_msecs(jiffies - intbss->ts)))
6750 goto nla_put_failure;
6751
6752 if (intbss->ts_boottime &&
6753 nla_put_u64(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
6754 intbss->ts_boottime))
6755 goto nla_put_failure;
6756
6757 switch (rdev->wiphy.signal_type) {
6758 case CFG80211_SIGNAL_TYPE_MBM:
6759 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
6760 goto nla_put_failure;
6761 break;
6762 case CFG80211_SIGNAL_TYPE_UNSPEC:
6763 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
6764 goto nla_put_failure;
6765 break;
6766 default:
6767 break;
6768 }
6769
6770 switch (wdev->iftype) {
6771 case NL80211_IFTYPE_P2P_CLIENT:
6772 case NL80211_IFTYPE_STATION:
6773 if (intbss == wdev->current_bss &&
6774 nla_put_u32(msg, NL80211_BSS_STATUS,
6775 NL80211_BSS_STATUS_ASSOCIATED))
6776 goto nla_put_failure;
6777 break;
6778 case NL80211_IFTYPE_ADHOC:
6779 if (intbss == wdev->current_bss &&
6780 nla_put_u32(msg, NL80211_BSS_STATUS,
6781 NL80211_BSS_STATUS_IBSS_JOINED))
6782 goto nla_put_failure;
6783 break;
6784 default:
6785 break;
6786 }
6787
6788 nla_nest_end(msg, bss);
6789
6790 genlmsg_end(msg, hdr);
6791 return 0;
6792
6793 fail_unlock_rcu:
6794 rcu_read_unlock();
6795 nla_put_failure:
6796 genlmsg_cancel(msg, hdr);
6797 return -EMSGSIZE;
6798}
6799
6800static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
6801{
6802 struct cfg80211_registered_device *rdev;
6803 struct cfg80211_internal_bss *scan;
6804 struct wireless_dev *wdev;
6805 int start = cb->args[2], idx = 0;
6806 int err;
6807
6808 rtnl_lock();
6809 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6810 if (err) {
6811 rtnl_unlock();
6812 return err;
6813 }
6814
6815 wdev_lock(wdev);
6816 spin_lock_bh(&rdev->bss_lock);
6817 cfg80211_bss_expire(rdev);
6818
6819 cb->seq = rdev->bss_generation;
6820
6821 list_for_each_entry(scan, &rdev->bss_list, list) {
6822 if (++idx <= start)
6823 continue;
6824 if (nl80211_send_bss(skb, cb,
6825 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6826 rdev, wdev, scan) < 0) {
6827 idx--;
6828 break;
6829 }
6830 }
6831
6832 spin_unlock_bh(&rdev->bss_lock);
6833 wdev_unlock(wdev);
6834
6835 cb->args[2] = idx;
6836 rtnl_unlock();
6837
6838 return skb->len;
6839}
6840
6841static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
6842 int flags, struct net_device *dev,
6843 bool allow_radio_stats,
6844 struct survey_info *survey)
6845{
6846 void *hdr;
6847 struct nlattr *infoattr;
6848
6849 /* skip radio stats if userspace didn't request them */
6850 if (!survey->channel && !allow_radio_stats)
6851 return 0;
6852
6853 hdr = nl80211hdr_put(msg, portid, seq, flags,
6854 NL80211_CMD_NEW_SURVEY_RESULTS);
6855 if (!hdr)
6856 return -ENOMEM;
6857
6858 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6859 goto nla_put_failure;
6860
6861 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6862 if (!infoattr)
6863 goto nla_put_failure;
6864
6865 if (survey->channel &&
6866 nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6867 survey->channel->center_freq))
6868 goto nla_put_failure;
6869
6870 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6871 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6872 goto nla_put_failure;
6873 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6874 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6875 goto nla_put_failure;
6876 if ((survey->filled & SURVEY_INFO_TIME) &&
6877 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME,
6878 survey->time))
6879 goto nla_put_failure;
6880 if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
6881 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_BUSY,
6882 survey->time_busy))
6883 goto nla_put_failure;
6884 if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
6885 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
6886 survey->time_ext_busy))
6887 goto nla_put_failure;
6888 if ((survey->filled & SURVEY_INFO_TIME_RX) &&
6889 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_RX,
6890 survey->time_rx))
6891 goto nla_put_failure;
6892 if ((survey->filled & SURVEY_INFO_TIME_TX) &&
6893 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_TX,
6894 survey->time_tx))
6895 goto nla_put_failure;
6896 if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
6897 nla_put_u64(msg, NL80211_SURVEY_INFO_TIME_SCAN,
6898 survey->time_scan))
6899 goto nla_put_failure;
6900
6901 nla_nest_end(msg, infoattr);
6902
6903 genlmsg_end(msg, hdr);
6904 return 0;
6905
6906 nla_put_failure:
6907 genlmsg_cancel(msg, hdr);
6908 return -EMSGSIZE;
6909}
6910
6911static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
6912{
6913 struct survey_info survey;
6914 struct cfg80211_registered_device *rdev;
6915 struct wireless_dev *wdev;
6916 int survey_idx = cb->args[2];
6917 int res;
6918 bool radio_stats;
6919
6920 rtnl_lock();
6921 res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
6922 if (res)
6923 goto out_err;
6924
6925 /* prepare_wdev_dump parsed the attributes */
6926 radio_stats = nl80211_fam.attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
6927
6928 if (!wdev->netdev) {
6929 res = -EINVAL;
6930 goto out_err;
6931 }
6932
6933 if (!rdev->ops->dump_survey) {
6934 res = -EOPNOTSUPP;
6935 goto out_err;
6936 }
6937
6938 while (1) {
6939 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
6940 if (res == -ENOENT)
6941 break;
6942 if (res)
6943 goto out_err;
6944
6945 /* don't send disabled channels, but do send non-channel data */
6946 if (survey.channel &&
6947 survey.channel->flags & IEEE80211_CHAN_DISABLED) {
6948 survey_idx++;
6949 continue;
6950 }
6951
6952 if (nl80211_send_survey(skb,
6953 NETLINK_CB(cb->skb).portid,
6954 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6955 wdev->netdev, radio_stats, &survey) < 0)
6956 goto out;
6957 survey_idx++;
6958 }
6959
6960 out:
6961 cb->args[2] = survey_idx;
6962 res = skb->len;
6963 out_err:
6964 rtnl_unlock();
6965 return res;
6966}
6967
6968static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6969{
6970 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6971 NL80211_WPA_VERSION_2));
6972}
6973
6974static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6975{
6976 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6977 struct net_device *dev = info->user_ptr[1];
6978 struct ieee80211_channel *chan;
6979 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6980 int err, ssid_len, ie_len = 0, sae_data_len = 0;
6981 enum nl80211_auth_type auth_type;
6982 struct key_parse key;
6983 bool local_state_change;
6984
6985 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6986 return -EINVAL;
6987
6988 if (!info->attrs[NL80211_ATTR_MAC])
6989 return -EINVAL;
6990
6991 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6992 return -EINVAL;
6993
6994 if (!info->attrs[NL80211_ATTR_SSID])
6995 return -EINVAL;
6996
6997 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6998 return -EINVAL;
6999
7000 err = nl80211_parse_key(info, &key);
7001 if (err)
7002 return err;
7003
7004 if (key.idx >= 0) {
7005 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
7006 return -EINVAL;
7007 if (!key.p.key || !key.p.key_len)
7008 return -EINVAL;
7009 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
7010 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
7011 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
7012 key.p.key_len != WLAN_KEY_LEN_WEP104))
7013 return -EINVAL;
7014 if (key.idx > 4)
7015 return -EINVAL;
7016 } else {
7017 key.p.key_len = 0;
7018 key.p.key = NULL;
7019 }
7020
7021 if (key.idx >= 0) {
7022 int i;
7023 bool ok = false;
7024 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
7025 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
7026 ok = true;
7027 break;
7028 }
7029 }
7030 if (!ok)
7031 return -EINVAL;
7032 }
7033
7034 if (!rdev->ops->auth)
7035 return -EOPNOTSUPP;
7036
7037 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7038 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7039 return -EOPNOTSUPP;
7040
7041 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7042 chan = nl80211_get_valid_chan(&rdev->wiphy,
7043 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7044 if (!chan)
7045 return -EINVAL;
7046
7047 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7048 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7049
7050 if (info->attrs[NL80211_ATTR_IE]) {
7051 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7052 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7053 }
7054
7055 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
7056 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
7057 return -EINVAL;
7058
7059 if (auth_type == NL80211_AUTHTYPE_SAE &&
7060 !info->attrs[NL80211_ATTR_SAE_DATA])
7061 return -EINVAL;
7062
7063 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
7064 if (auth_type != NL80211_AUTHTYPE_SAE)
7065 return -EINVAL;
7066 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
7067 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
7068 /* need to include at least Auth Transaction and Status Code */
7069 if (sae_data_len < 4)
7070 return -EINVAL;
7071 }
7072
7073 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7074
7075 /*
7076 * Since we no longer track auth state, ignore
7077 * requests to only change local state.
7078 */
7079 if (local_state_change)
7080 return 0;
7081
7082 wdev_lock(dev->ieee80211_ptr);
7083 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
7084 ssid, ssid_len, ie, ie_len,
7085 key.p.key, key.p.key_len, key.idx,
7086 sae_data, sae_data_len);
7087 wdev_unlock(dev->ieee80211_ptr);
7088 return err;
7089}
7090
7091static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
7092 struct genl_info *info,
7093 struct cfg80211_crypto_settings *settings,
7094 int cipher_limit)
7095{
7096 memset(settings, 0, sizeof(*settings));
7097
7098 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
7099
7100 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
7101 u16 proto;
7102 proto = nla_get_u16(
7103 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
7104 settings->control_port_ethertype = cpu_to_be16(proto);
7105 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
7106 proto != ETH_P_PAE)
7107 return -EINVAL;
7108 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
7109 settings->control_port_no_encrypt = true;
7110 } else
7111 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
7112
7113 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
7114 void *data;
7115 int len, i;
7116
7117 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
7118 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
7119 settings->n_ciphers_pairwise = len / sizeof(u32);
7120
7121 if (len % sizeof(u32))
7122 return -EINVAL;
7123
7124 if (settings->n_ciphers_pairwise > cipher_limit)
7125 return -EINVAL;
7126
7127 memcpy(settings->ciphers_pairwise, data, len);
7128
7129 for (i = 0; i < settings->n_ciphers_pairwise; i++)
7130 if (!cfg80211_supported_cipher_suite(
7131 &rdev->wiphy,
7132 settings->ciphers_pairwise[i]))
7133 return -EINVAL;
7134 }
7135
7136 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
7137 settings->cipher_group =
7138 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
7139 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
7140 settings->cipher_group))
7141 return -EINVAL;
7142 }
7143
7144 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
7145 settings->wpa_versions =
7146 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
7147 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
7148 return -EINVAL;
7149 }
7150
7151 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
7152 void *data;
7153 int len;
7154
7155 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
7156 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
7157 settings->n_akm_suites = len / sizeof(u32);
7158
7159 if (len % sizeof(u32))
7160 return -EINVAL;
7161
7162 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
7163 return -EINVAL;
7164
7165 memcpy(settings->akm_suites, data, len);
7166 }
7167
7168 return 0;
7169}
7170
7171static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
7172{
7173 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7174 struct net_device *dev = info->user_ptr[1];
7175 struct ieee80211_channel *chan;
7176 struct cfg80211_assoc_request req = {};
7177 const u8 *bssid, *ssid;
7178 int err, ssid_len = 0;
7179
7180 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7181 return -EINVAL;
7182
7183 if (!info->attrs[NL80211_ATTR_MAC] ||
7184 !info->attrs[NL80211_ATTR_SSID] ||
7185 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
7186 return -EINVAL;
7187
7188 if (!rdev->ops->assoc)
7189 return -EOPNOTSUPP;
7190
7191 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7192 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7193 return -EOPNOTSUPP;
7194
7195 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7196
7197 chan = nl80211_get_valid_chan(&rdev->wiphy,
7198 info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7199 if (!chan)
7200 return -EINVAL;
7201
7202 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7203 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7204
7205 if (info->attrs[NL80211_ATTR_IE]) {
7206 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7207 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7208 }
7209
7210 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7211 enum nl80211_mfp mfp =
7212 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7213 if (mfp == NL80211_MFP_REQUIRED)
7214 req.use_mfp = true;
7215 else if (mfp != NL80211_MFP_NO)
7216 return -EINVAL;
7217 }
7218
7219 if (info->attrs[NL80211_ATTR_PREV_BSSID])
7220 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
7221
7222 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7223 req.flags |= ASSOC_REQ_DISABLE_HT;
7224
7225 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7226 memcpy(&req.ht_capa_mask,
7227 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7228 sizeof(req.ht_capa_mask));
7229
7230 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7231 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7232 return -EINVAL;
7233 memcpy(&req.ht_capa,
7234 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7235 sizeof(req.ht_capa));
7236 }
7237
7238 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7239 req.flags |= ASSOC_REQ_DISABLE_VHT;
7240
7241 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7242 memcpy(&req.vht_capa_mask,
7243 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7244 sizeof(req.vht_capa_mask));
7245
7246 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7247 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7248 return -EINVAL;
7249 memcpy(&req.vht_capa,
7250 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7251 sizeof(req.vht_capa));
7252 }
7253
7254 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7255 if (!(rdev->wiphy.features &
7256 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7257 !(rdev->wiphy.features & NL80211_FEATURE_QUIET))
7258 return -EINVAL;
7259 req.flags |= ASSOC_REQ_USE_RRM;
7260 }
7261
7262 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
7263 if (!err) {
7264 wdev_lock(dev->ieee80211_ptr);
7265 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
7266 ssid, ssid_len, &req);
7267 wdev_unlock(dev->ieee80211_ptr);
7268 }
7269
7270 return err;
7271}
7272
7273static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
7274{
7275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7276 struct net_device *dev = info->user_ptr[1];
7277 const u8 *ie = NULL, *bssid;
7278 int ie_len = 0, err;
7279 u16 reason_code;
7280 bool local_state_change;
7281
7282 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7283 return -EINVAL;
7284
7285 if (!info->attrs[NL80211_ATTR_MAC])
7286 return -EINVAL;
7287
7288 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7289 return -EINVAL;
7290
7291 if (!rdev->ops->deauth)
7292 return -EOPNOTSUPP;
7293
7294 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7295 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7296 return -EOPNOTSUPP;
7297
7298 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7299
7300 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7301 if (reason_code == 0) {
7302 /* Reason Code 0 is reserved */
7303 return -EINVAL;
7304 }
7305
7306 if (info->attrs[NL80211_ATTR_IE]) {
7307 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7308 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7309 }
7310
7311 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7312
7313 wdev_lock(dev->ieee80211_ptr);
7314 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
7315 local_state_change);
7316 wdev_unlock(dev->ieee80211_ptr);
7317 return err;
7318}
7319
7320static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
7321{
7322 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7323 struct net_device *dev = info->user_ptr[1];
7324 const u8 *ie = NULL, *bssid;
7325 int ie_len = 0, err;
7326 u16 reason_code;
7327 bool local_state_change;
7328
7329 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7330 return -EINVAL;
7331
7332 if (!info->attrs[NL80211_ATTR_MAC])
7333 return -EINVAL;
7334
7335 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7336 return -EINVAL;
7337
7338 if (!rdev->ops->disassoc)
7339 return -EOPNOTSUPP;
7340
7341 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7342 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7343 return -EOPNOTSUPP;
7344
7345 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7346
7347 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7348 if (reason_code == 0) {
7349 /* Reason Code 0 is reserved */
7350 return -EINVAL;
7351 }
7352
7353 if (info->attrs[NL80211_ATTR_IE]) {
7354 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7355 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7356 }
7357
7358 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
7359
7360 wdev_lock(dev->ieee80211_ptr);
7361 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
7362 local_state_change);
7363 wdev_unlock(dev->ieee80211_ptr);
7364 return err;
7365}
7366
7367static bool
7368nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
7369 int mcast_rate[IEEE80211_NUM_BANDS],
7370 int rateval)
7371{
7372 struct wiphy *wiphy = &rdev->wiphy;
7373 bool found = false;
7374 int band, i;
7375
7376 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
7377 struct ieee80211_supported_band *sband;
7378
7379 sband = wiphy->bands[band];
7380 if (!sband)
7381 continue;
7382
7383 for (i = 0; i < sband->n_bitrates; i++) {
7384 if (sband->bitrates[i].bitrate == rateval) {
7385 mcast_rate[band] = i + 1;
7386 found = true;
7387 break;
7388 }
7389 }
7390 }
7391
7392 return found;
7393}
7394
7395static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
7396{
7397 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7398 struct net_device *dev = info->user_ptr[1];
7399 struct cfg80211_ibss_params ibss;
7400 struct wiphy *wiphy;
7401 struct cfg80211_cached_keys *connkeys = NULL;
7402 int err;
7403
7404 memset(&ibss, 0, sizeof(ibss));
7405
7406 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7407 return -EINVAL;
7408
7409 if (!info->attrs[NL80211_ATTR_SSID] ||
7410 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7411 return -EINVAL;
7412
7413 ibss.beacon_interval = 100;
7414
7415 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7416 ibss.beacon_interval =
7417 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7418 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
7419 return -EINVAL;
7420 }
7421
7422 if (!rdev->ops->join_ibss)
7423 return -EOPNOTSUPP;
7424
7425 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7426 return -EOPNOTSUPP;
7427
7428 wiphy = &rdev->wiphy;
7429
7430 if (info->attrs[NL80211_ATTR_MAC]) {
7431 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7432
7433 if (!is_valid_ether_addr(ibss.bssid))
7434 return -EINVAL;
7435 }
7436 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7437 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7438
7439 if (info->attrs[NL80211_ATTR_IE]) {
7440 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7441 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7442 }
7443
7444 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
7445 if (err)
7446 return err;
7447
7448 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
7449 NL80211_IFTYPE_ADHOC))
7450 return -EINVAL;
7451
7452 switch (ibss.chandef.width) {
7453 case NL80211_CHAN_WIDTH_5:
7454 case NL80211_CHAN_WIDTH_10:
7455 case NL80211_CHAN_WIDTH_20_NOHT:
7456 break;
7457 case NL80211_CHAN_WIDTH_20:
7458 case NL80211_CHAN_WIDTH_40:
7459 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7460 return -EINVAL;
7461 break;
7462 case NL80211_CHAN_WIDTH_80:
7463 case NL80211_CHAN_WIDTH_80P80:
7464 case NL80211_CHAN_WIDTH_160:
7465 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7466 return -EINVAL;
7467 if (!wiphy_ext_feature_isset(&rdev->wiphy,
7468 NL80211_EXT_FEATURE_VHT_IBSS))
7469 return -EINVAL;
7470 break;
7471 default:
7472 return -EINVAL;
7473 }
7474
7475 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
7476 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
7477
7478 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7479 u8 *rates =
7480 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7481 int n_rates =
7482 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7483 struct ieee80211_supported_band *sband =
7484 wiphy->bands[ibss.chandef.chan->band];
7485
7486 err = ieee80211_get_ratemask(sband, rates, n_rates,
7487 &ibss.basic_rates);
7488 if (err)
7489 return err;
7490 }
7491
7492 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7493 memcpy(&ibss.ht_capa_mask,
7494 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7495 sizeof(ibss.ht_capa_mask));
7496
7497 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7498 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7499 return -EINVAL;
7500 memcpy(&ibss.ht_capa,
7501 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7502 sizeof(ibss.ht_capa));
7503 }
7504
7505 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7506 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
7507 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7508 return -EINVAL;
7509
7510 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7511 bool no_ht = false;
7512
7513 connkeys = nl80211_parse_connkeys(rdev,
7514 info->attrs[NL80211_ATTR_KEYS],
7515 &no_ht);
7516 if (IS_ERR(connkeys))
7517 return PTR_ERR(connkeys);
7518
7519 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
7520 no_ht) {
7521 kfree(connkeys);
7522 return -EINVAL;
7523 }
7524 }
7525
7526 ibss.control_port =
7527 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
7528
7529 ibss.userspace_handles_dfs =
7530 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
7531
7532 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
7533 if (err)
7534 kzfree(connkeys);
7535 return err;
7536}
7537
7538static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
7539{
7540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7541 struct net_device *dev = info->user_ptr[1];
7542
7543 if (!rdev->ops->leave_ibss)
7544 return -EOPNOTSUPP;
7545
7546 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
7547 return -EOPNOTSUPP;
7548
7549 return cfg80211_leave_ibss(rdev, dev, false);
7550}
7551
7552static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
7553{
7554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7555 struct net_device *dev = info->user_ptr[1];
7556 int mcast_rate[IEEE80211_NUM_BANDS];
7557 u32 nla_rate;
7558 int err;
7559
7560 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
7561 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
7562 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
7563 return -EOPNOTSUPP;
7564
7565 if (!rdev->ops->set_mcast_rate)
7566 return -EOPNOTSUPP;
7567
7568 memset(mcast_rate, 0, sizeof(mcast_rate));
7569
7570 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
7571 return -EINVAL;
7572
7573 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
7574 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
7575 return -EINVAL;
7576
7577 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
7578
7579 return err;
7580}
7581
7582static struct sk_buff *
7583__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7584 struct wireless_dev *wdev, int approxlen,
7585 u32 portid, u32 seq, enum nl80211_commands cmd,
7586 enum nl80211_attrs attr,
7587 const struct nl80211_vendor_cmd_info *info,
7588 gfp_t gfp)
7589{
7590 struct sk_buff *skb;
7591 void *hdr;
7592 struct nlattr *data;
7593
7594 skb = nlmsg_new(approxlen + 100, gfp);
7595 if (!skb)
7596 return NULL;
7597
7598 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
7599 if (!hdr) {
7600 kfree_skb(skb);
7601 return NULL;
7602 }
7603
7604 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
7605 goto nla_put_failure;
7606
7607 if (info) {
7608 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
7609 info->vendor_id))
7610 goto nla_put_failure;
7611 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
7612 info->subcmd))
7613 goto nla_put_failure;
7614 }
7615
7616 if (wdev) {
7617 if (nla_put_u64(skb, NL80211_ATTR_WDEV,
7618 wdev_id(wdev)))
7619 goto nla_put_failure;
7620 if (wdev->netdev &&
7621 nla_put_u32(skb, NL80211_ATTR_IFINDEX,
7622 wdev->netdev->ifindex))
7623 goto nla_put_failure;
7624 }
7625
7626 data = nla_nest_start(skb, attr);
7627
7628 ((void **)skb->cb)[0] = rdev;
7629 ((void **)skb->cb)[1] = hdr;
7630 ((void **)skb->cb)[2] = data;
7631
7632 return skb;
7633
7634 nla_put_failure:
7635 kfree_skb(skb);
7636 return NULL;
7637}
7638
7639struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7640 struct wireless_dev *wdev,
7641 enum nl80211_commands cmd,
7642 enum nl80211_attrs attr,
7643 int vendor_event_idx,
7644 int approxlen, gfp_t gfp)
7645{
7646 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
7647 const struct nl80211_vendor_cmd_info *info;
7648
7649 switch (cmd) {
7650 case NL80211_CMD_TESTMODE:
7651 if (WARN_ON(vendor_event_idx != -1))
7652 return NULL;
7653 info = NULL;
7654 break;
7655 case NL80211_CMD_VENDOR:
7656 if (WARN_ON(vendor_event_idx < 0 ||
7657 vendor_event_idx >= wiphy->n_vendor_events))
7658 return NULL;
7659 info = &wiphy->vendor_events[vendor_event_idx];
7660 break;
7661 default:
7662 WARN_ON(1);
7663 return NULL;
7664 }
7665
7666 return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
7667 cmd, attr, info, gfp);
7668}
7669EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
7670
7671void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
7672{
7673 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
7674 void *hdr = ((void **)skb->cb)[1];
7675 struct nlattr *data = ((void **)skb->cb)[2];
7676 enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
7677
7678 /* clear CB data for netlink core to own from now on */
7679 memset(skb->cb, 0, sizeof(skb->cb));
7680
7681 nla_nest_end(skb, data);
7682 genlmsg_end(skb, hdr);
7683
7684 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
7685 mcgrp = NL80211_MCGRP_VENDOR;
7686
7687 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
7688 mcgrp, gfp);
7689}
7690EXPORT_SYMBOL(__cfg80211_send_event_skb);
7691
7692#ifdef CONFIG_NL80211_TESTMODE
7693static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
7694{
7695 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7696 struct wireless_dev *wdev =
7697 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
7698 int err;
7699
7700 if (!rdev->ops->testmode_cmd)
7701 return -EOPNOTSUPP;
7702
7703 if (IS_ERR(wdev)) {
7704 err = PTR_ERR(wdev);
7705 if (err != -EINVAL)
7706 return err;
7707 wdev = NULL;
7708 } else if (wdev->wiphy != &rdev->wiphy) {
7709 return -EINVAL;
7710 }
7711
7712 if (!info->attrs[NL80211_ATTR_TESTDATA])
7713 return -EINVAL;
7714
7715 rdev->cur_cmd_info = info;
7716 err = rdev_testmode_cmd(rdev, wdev,
7717 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
7718 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
7719 rdev->cur_cmd_info = NULL;
7720
7721 return err;
7722}
7723
7724static int nl80211_testmode_dump(struct sk_buff *skb,
7725 struct netlink_callback *cb)
7726{
7727 struct cfg80211_registered_device *rdev;
7728 int err;
7729 long phy_idx;
7730 void *data = NULL;
7731 int data_len = 0;
7732
7733 rtnl_lock();
7734
7735 if (cb->args[0]) {
7736 /*
7737 * 0 is a valid index, but not valid for args[0],
7738 * so we need to offset by 1.
7739 */
7740 phy_idx = cb->args[0] - 1;
7741 } else {
7742 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
7743 nl80211_fam.attrbuf, nl80211_fam.maxattr,
7744 nl80211_policy);
7745 if (err)
7746 goto out_err;
7747
7748 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
7749 nl80211_fam.attrbuf);
7750 if (IS_ERR(rdev)) {
7751 err = PTR_ERR(rdev);
7752 goto out_err;
7753 }
7754 phy_idx = rdev->wiphy_idx;
7755 rdev = NULL;
7756
7757 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
7758 cb->args[1] =
7759 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
7760 }
7761
7762 if (cb->args[1]) {
7763 data = nla_data((void *)cb->args[1]);
7764 data_len = nla_len((void *)cb->args[1]);
7765 }
7766
7767 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
7768 if (!rdev) {
7769 err = -ENOENT;
7770 goto out_err;
7771 }
7772
7773 if (!rdev->ops->testmode_dump) {
7774 err = -EOPNOTSUPP;
7775 goto out_err;
7776 }
7777
7778 while (1) {
7779 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
7780 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7781 NL80211_CMD_TESTMODE);
7782 struct nlattr *tmdata;
7783
7784 if (!hdr)
7785 break;
7786
7787 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
7788 genlmsg_cancel(skb, hdr);
7789 break;
7790 }
7791
7792 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
7793 if (!tmdata) {
7794 genlmsg_cancel(skb, hdr);
7795 break;
7796 }
7797 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
7798 nla_nest_end(skb, tmdata);
7799
7800 if (err == -ENOBUFS || err == -ENOENT) {
7801 genlmsg_cancel(skb, hdr);
7802 break;
7803 } else if (err) {
7804 genlmsg_cancel(skb, hdr);
7805 goto out_err;
7806 }
7807
7808 genlmsg_end(skb, hdr);
7809 }
7810
7811 err = skb->len;
7812 /* see above */
7813 cb->args[0] = phy_idx + 1;
7814 out_err:
7815 rtnl_unlock();
7816 return err;
7817}
7818#endif
7819
7820static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
7821{
7822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7823 struct net_device *dev = info->user_ptr[1];
7824 struct cfg80211_connect_params connect;
7825 struct wiphy *wiphy;
7826 struct cfg80211_cached_keys *connkeys = NULL;
7827 int err;
7828
7829 memset(&connect, 0, sizeof(connect));
7830
7831 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
7832 return -EINVAL;
7833
7834 if (!info->attrs[NL80211_ATTR_SSID] ||
7835 !nla_len(info->attrs[NL80211_ATTR_SSID]))
7836 return -EINVAL;
7837
7838 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
7839 connect.auth_type =
7840 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
7841 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
7842 NL80211_CMD_CONNECT))
7843 return -EINVAL;
7844 } else
7845 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
7846
7847 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
7848
7849 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
7850 NL80211_MAX_NR_CIPHER_SUITES);
7851 if (err)
7852 return err;
7853
7854 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7855 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7856 return -EOPNOTSUPP;
7857
7858 wiphy = &rdev->wiphy;
7859
7860 connect.bg_scan_period = -1;
7861 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
7862 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
7863 connect.bg_scan_period =
7864 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
7865 }
7866
7867 if (info->attrs[NL80211_ATTR_MAC])
7868 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7869 else if (info->attrs[NL80211_ATTR_MAC_HINT])
7870 connect.bssid_hint =
7871 nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
7872 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
7873 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
7874
7875 if (info->attrs[NL80211_ATTR_IE]) {
7876 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
7877 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7878 }
7879
7880 if (info->attrs[NL80211_ATTR_USE_MFP]) {
7881 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
7882 if (connect.mfp != NL80211_MFP_REQUIRED &&
7883 connect.mfp != NL80211_MFP_NO)
7884 return -EINVAL;
7885 } else {
7886 connect.mfp = NL80211_MFP_NO;
7887 }
7888
7889 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7890 connect.channel = nl80211_get_valid_chan(
7891 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
7892 if (!connect.channel)
7893 return -EINVAL;
7894 } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
7895 connect.channel_hint = nl80211_get_valid_chan(
7896 wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
7897 if (!connect.channel_hint)
7898 return -EINVAL;
7899 }
7900
7901 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
7902 connkeys = nl80211_parse_connkeys(rdev,
7903 info->attrs[NL80211_ATTR_KEYS], NULL);
7904 if (IS_ERR(connkeys))
7905 return PTR_ERR(connkeys);
7906 }
7907
7908 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
7909 connect.flags |= ASSOC_REQ_DISABLE_HT;
7910
7911 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7912 memcpy(&connect.ht_capa_mask,
7913 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
7914 sizeof(connect.ht_capa_mask));
7915
7916 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
7917 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
7918 kzfree(connkeys);
7919 return -EINVAL;
7920 }
7921 memcpy(&connect.ht_capa,
7922 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
7923 sizeof(connect.ht_capa));
7924 }
7925
7926 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
7927 connect.flags |= ASSOC_REQ_DISABLE_VHT;
7928
7929 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
7930 memcpy(&connect.vht_capa_mask,
7931 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
7932 sizeof(connect.vht_capa_mask));
7933
7934 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7935 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7936 kzfree(connkeys);
7937 return -EINVAL;
7938 }
7939 memcpy(&connect.vht_capa,
7940 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7941 sizeof(connect.vht_capa));
7942 }
7943
7944 if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
7945 if (!(rdev->wiphy.features &
7946 NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) ||
7947 !(rdev->wiphy.features & NL80211_FEATURE_QUIET)) {
7948 kzfree(connkeys);
7949 return -EINVAL;
7950 }
7951 connect.flags |= ASSOC_REQ_USE_RRM;
7952 }
7953
7954 wdev_lock(dev->ieee80211_ptr);
7955 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7956 wdev_unlock(dev->ieee80211_ptr);
7957 if (err)
7958 kzfree(connkeys);
7959 return err;
7960}
7961
7962static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7963{
7964 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7965 struct net_device *dev = info->user_ptr[1];
7966 u16 reason;
7967 int ret;
7968
7969 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7970 reason = WLAN_REASON_DEAUTH_LEAVING;
7971 else
7972 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7973
7974 if (reason == 0)
7975 return -EINVAL;
7976
7977 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
7978 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7979 return -EOPNOTSUPP;
7980
7981 wdev_lock(dev->ieee80211_ptr);
7982 ret = cfg80211_disconnect(rdev, dev, reason, true);
7983 wdev_unlock(dev->ieee80211_ptr);
7984 return ret;
7985}
7986
7987static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7988{
7989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7990 struct net *net;
7991 int err;
7992
7993 if (info->attrs[NL80211_ATTR_PID]) {
7994 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7995
7996 net = get_net_ns_by_pid(pid);
7997 } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
7998 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
7999
8000 net = get_net_ns_by_fd(fd);
8001 } else {
8002 return -EINVAL;
8003 }
8004
8005 if (IS_ERR(net))
8006 return PTR_ERR(net);
8007
8008 err = 0;
8009
8010 /* check if anything to do */
8011 if (!net_eq(wiphy_net(&rdev->wiphy), net))
8012 err = cfg80211_switch_netns(rdev, net);
8013
8014 put_net(net);
8015 return err;
8016}
8017
8018static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
8019{
8020 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8021 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
8022 struct cfg80211_pmksa *pmksa) = NULL;
8023 struct net_device *dev = info->user_ptr[1];
8024 struct cfg80211_pmksa pmksa;
8025
8026 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
8027
8028 if (!info->attrs[NL80211_ATTR_MAC])
8029 return -EINVAL;
8030
8031 if (!info->attrs[NL80211_ATTR_PMKID])
8032 return -EINVAL;
8033
8034 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
8035 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8036
8037 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8038 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8039 return -EOPNOTSUPP;
8040
8041 switch (info->genlhdr->cmd) {
8042 case NL80211_CMD_SET_PMKSA:
8043 rdev_ops = rdev->ops->set_pmksa;
8044 break;
8045 case NL80211_CMD_DEL_PMKSA:
8046 rdev_ops = rdev->ops->del_pmksa;
8047 break;
8048 default:
8049 WARN_ON(1);
8050 break;
8051 }
8052
8053 if (!rdev_ops)
8054 return -EOPNOTSUPP;
8055
8056 return rdev_ops(&rdev->wiphy, dev, &pmksa);
8057}
8058
8059static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
8060{
8061 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8062 struct net_device *dev = info->user_ptr[1];
8063
8064 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8065 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8066 return -EOPNOTSUPP;
8067
8068 if (!rdev->ops->flush_pmksa)
8069 return -EOPNOTSUPP;
8070
8071 return rdev_flush_pmksa(rdev, dev);
8072}
8073
8074static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
8075{
8076 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8077 struct net_device *dev = info->user_ptr[1];
8078 u8 action_code, dialog_token;
8079 u32 peer_capability = 0;
8080 u16 status_code;
8081 u8 *peer;
8082 bool initiator;
8083
8084 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
8085 !rdev->ops->tdls_mgmt)
8086 return -EOPNOTSUPP;
8087
8088 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
8089 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
8090 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
8091 !info->attrs[NL80211_ATTR_IE] ||
8092 !info->attrs[NL80211_ATTR_MAC])
8093 return -EINVAL;
8094
8095 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
8096 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
8097 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
8098 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
8099 initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
8100 if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
8101 peer_capability =
8102 nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
8103
8104 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
8105 dialog_token, status_code, peer_capability,
8106 initiator,
8107 nla_data(info->attrs[NL80211_ATTR_IE]),
8108 nla_len(info->attrs[NL80211_ATTR_IE]));
8109}
8110
8111static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
8112{
8113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8114 struct net_device *dev = info->user_ptr[1];
8115 enum nl80211_tdls_operation operation;
8116 u8 *peer;
8117
8118 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
8119 !rdev->ops->tdls_oper)
8120 return -EOPNOTSUPP;
8121
8122 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
8123 !info->attrs[NL80211_ATTR_MAC])
8124 return -EINVAL;
8125
8126 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
8127 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
8128
8129 return rdev_tdls_oper(rdev, dev, peer, operation);
8130}
8131
8132static int nl80211_remain_on_channel(struct sk_buff *skb,
8133 struct genl_info *info)
8134{
8135 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8136 struct wireless_dev *wdev = info->user_ptr[1];
8137 struct cfg80211_chan_def chandef;
8138 struct sk_buff *msg;
8139 void *hdr;
8140 u64 cookie;
8141 u32 duration;
8142 int err;
8143
8144 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8145 !info->attrs[NL80211_ATTR_DURATION])
8146 return -EINVAL;
8147
8148 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
8149
8150 if (!rdev->ops->remain_on_channel ||
8151 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
8152 return -EOPNOTSUPP;
8153
8154 /*
8155 * We should be on that channel for at least a minimum amount of
8156 * time (10ms) but no longer than the driver supports.
8157 */
8158 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8159 duration > rdev->wiphy.max_remain_on_channel_duration)
8160 return -EINVAL;
8161
8162 err = nl80211_parse_chandef(rdev, info, &chandef);
8163 if (err)
8164 return err;
8165
8166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8167 if (!msg)
8168 return -ENOMEM;
8169
8170 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8171 NL80211_CMD_REMAIN_ON_CHANNEL);
8172 if (!hdr) {
8173 err = -ENOBUFS;
8174 goto free_msg;
8175 }
8176
8177 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
8178 duration, &cookie);
8179
8180 if (err)
8181 goto free_msg;
8182
8183 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8184 goto nla_put_failure;
8185
8186 genlmsg_end(msg, hdr);
8187
8188 return genlmsg_reply(msg, info);
8189
8190 nla_put_failure:
8191 err = -ENOBUFS;
8192 free_msg:
8193 nlmsg_free(msg);
8194 return err;
8195}
8196
8197static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
8198 struct genl_info *info)
8199{
8200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8201 struct wireless_dev *wdev = info->user_ptr[1];
8202 u64 cookie;
8203
8204 if (!info->attrs[NL80211_ATTR_COOKIE])
8205 return -EINVAL;
8206
8207 if (!rdev->ops->cancel_remain_on_channel)
8208 return -EOPNOTSUPP;
8209
8210 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8211
8212 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
8213}
8214
8215static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
8216 u8 *rates, u8 rates_len)
8217{
8218 u8 i;
8219 u32 mask = 0;
8220
8221 for (i = 0; i < rates_len; i++) {
8222 int rate = (rates[i] & 0x7f) * 5;
8223 int ridx;
8224 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
8225 struct ieee80211_rate *srate =
8226 &sband->bitrates[ridx];
8227 if (rate == srate->bitrate) {
8228 mask |= 1 << ridx;
8229 break;
8230 }
8231 }
8232 if (ridx == sband->n_bitrates)
8233 return 0; /* rate not found */
8234 }
8235
8236 return mask;
8237}
8238
8239static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
8240 u8 *rates, u8 rates_len,
8241 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
8242{
8243 u8 i;
8244
8245 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
8246
8247 for (i = 0; i < rates_len; i++) {
8248 int ridx, rbit;
8249
8250 ridx = rates[i] / 8;
8251 rbit = BIT(rates[i] % 8);
8252
8253 /* check validity */
8254 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
8255 return false;
8256
8257 /* check availability */
8258 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
8259 mcs[ridx] |= rbit;
8260 else
8261 return false;
8262 }
8263
8264 return true;
8265}
8266
8267static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
8268{
8269 u16 mcs_mask = 0;
8270
8271 switch (vht_mcs_map) {
8272 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
8273 break;
8274 case IEEE80211_VHT_MCS_SUPPORT_0_7:
8275 mcs_mask = 0x00FF;
8276 break;
8277 case IEEE80211_VHT_MCS_SUPPORT_0_8:
8278 mcs_mask = 0x01FF;
8279 break;
8280 case IEEE80211_VHT_MCS_SUPPORT_0_9:
8281 mcs_mask = 0x03FF;
8282 break;
8283 default:
8284 break;
8285 }
8286
8287 return mcs_mask;
8288}
8289
8290static void vht_build_mcs_mask(u16 vht_mcs_map,
8291 u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
8292{
8293 u8 nss;
8294
8295 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
8296 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
8297 vht_mcs_map >>= 2;
8298 }
8299}
8300
8301static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
8302 struct nl80211_txrate_vht *txrate,
8303 u16 mcs[NL80211_VHT_NSS_MAX])
8304{
8305 u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8306 u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
8307 u8 i;
8308
8309 if (!sband->vht_cap.vht_supported)
8310 return false;
8311
8312 memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
8313
8314 /* Build vht_mcs_mask from VHT capabilities */
8315 vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
8316
8317 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8318 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
8319 mcs[i] = txrate->mcs[i];
8320 else
8321 return false;
8322 }
8323
8324 return true;
8325}
8326
8327static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
8328 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
8329 .len = NL80211_MAX_SUPP_RATES },
8330 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
8331 .len = NL80211_MAX_SUPP_HT_RATES },
8332 [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
8333 [NL80211_TXRATE_GI] = { .type = NLA_U8 },
8334};
8335
8336static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
8337 struct genl_info *info)
8338{
8339 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
8340 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8341 struct cfg80211_bitrate_mask mask;
8342 int rem, i;
8343 struct net_device *dev = info->user_ptr[1];
8344 struct nlattr *tx_rates;
8345 struct ieee80211_supported_band *sband;
8346 u16 vht_tx_mcs_map;
8347
8348 if (!rdev->ops->set_bitrate_mask)
8349 return -EOPNOTSUPP;
8350
8351 memset(&mask, 0, sizeof(mask));
8352 /* Default to all rates enabled */
8353 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
8354 sband = rdev->wiphy.bands[i];
8355
8356 if (!sband)
8357 continue;
8358
8359 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
8360 memcpy(mask.control[i].ht_mcs,
8361 sband->ht_cap.mcs.rx_mask,
8362 sizeof(mask.control[i].ht_mcs));
8363
8364 if (!sband->vht_cap.vht_supported)
8365 continue;
8366
8367 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8368 vht_build_mcs_mask(vht_tx_mcs_map, mask.control[i].vht_mcs);
8369 }
8370
8371 /* if no rates are given set it back to the defaults */
8372 if (!info->attrs[NL80211_ATTR_TX_RATES])
8373 goto out;
8374
8375 /*
8376 * The nested attribute uses enum nl80211_band as the index. This maps
8377 * directly to the enum ieee80211_band values used in cfg80211.
8378 */
8379 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
8380 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
8381 enum ieee80211_band band = nla_type(tx_rates);
8382 int err;
8383
8384 if (band < 0 || band >= IEEE80211_NUM_BANDS)
8385 return -EINVAL;
8386 sband = rdev->wiphy.bands[band];
8387 if (sband == NULL)
8388 return -EINVAL;
8389 err = nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
8390 nla_len(tx_rates), nl80211_txattr_policy);
8391 if (err)
8392 return err;
8393 if (tb[NL80211_TXRATE_LEGACY]) {
8394 mask.control[band].legacy = rateset_to_mask(
8395 sband,
8396 nla_data(tb[NL80211_TXRATE_LEGACY]),
8397 nla_len(tb[NL80211_TXRATE_LEGACY]));
8398 if ((mask.control[band].legacy == 0) &&
8399 nla_len(tb[NL80211_TXRATE_LEGACY]))
8400 return -EINVAL;
8401 }
8402 if (tb[NL80211_TXRATE_HT]) {
8403 if (!ht_rateset_to_mask(
8404 sband,
8405 nla_data(tb[NL80211_TXRATE_HT]),
8406 nla_len(tb[NL80211_TXRATE_HT]),
8407 mask.control[band].ht_mcs))
8408 return -EINVAL;
8409 }
8410 if (tb[NL80211_TXRATE_VHT]) {
8411 if (!vht_set_mcs_mask(
8412 sband,
8413 nla_data(tb[NL80211_TXRATE_VHT]),
8414 mask.control[band].vht_mcs))
8415 return -EINVAL;
8416 }
8417 if (tb[NL80211_TXRATE_GI]) {
8418 mask.control[band].gi =
8419 nla_get_u8(tb[NL80211_TXRATE_GI]);
8420 if (mask.control[band].gi > NL80211_TXRATE_FORCE_LGI)
8421 return -EINVAL;
8422 }
8423
8424 if (mask.control[band].legacy == 0) {
8425 /* don't allow empty legacy rates if HT or VHT
8426 * are not even supported.
8427 */
8428 if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
8429 rdev->wiphy.bands[band]->vht_cap.vht_supported))
8430 return -EINVAL;
8431
8432 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
8433 if (mask.control[band].ht_mcs[i])
8434 goto out;
8435
8436 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
8437 if (mask.control[band].vht_mcs[i])
8438 goto out;
8439
8440 /* legacy and mcs rates may not be both empty */
8441 return -EINVAL;
8442 }
8443 }
8444
8445out:
8446 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
8447}
8448
8449static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
8450{
8451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8452 struct wireless_dev *wdev = info->user_ptr[1];
8453 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
8454
8455 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
8456 return -EINVAL;
8457
8458 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
8459 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
8460
8461 switch (wdev->iftype) {
8462 case NL80211_IFTYPE_STATION:
8463 case NL80211_IFTYPE_ADHOC:
8464 case NL80211_IFTYPE_P2P_CLIENT:
8465 case NL80211_IFTYPE_AP:
8466 case NL80211_IFTYPE_AP_VLAN:
8467 case NL80211_IFTYPE_MESH_POINT:
8468 case NL80211_IFTYPE_P2P_GO:
8469 case NL80211_IFTYPE_P2P_DEVICE:
8470 break;
8471 default:
8472 return -EOPNOTSUPP;
8473 }
8474
8475 /* not much point in registering if we can't reply */
8476 if (!rdev->ops->mgmt_tx)
8477 return -EOPNOTSUPP;
8478
8479 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
8480 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
8481 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
8482}
8483
8484static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
8485{
8486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8487 struct wireless_dev *wdev = info->user_ptr[1];
8488 struct cfg80211_chan_def chandef;
8489 int err;
8490 void *hdr = NULL;
8491 u64 cookie;
8492 struct sk_buff *msg = NULL;
8493 struct cfg80211_mgmt_tx_params params = {
8494 .dont_wait_for_ack =
8495 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
8496 };
8497
8498 if (!info->attrs[NL80211_ATTR_FRAME])
8499 return -EINVAL;
8500
8501 if (!rdev->ops->mgmt_tx)
8502 return -EOPNOTSUPP;
8503
8504 switch (wdev->iftype) {
8505 case NL80211_IFTYPE_P2P_DEVICE:
8506 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8507 return -EINVAL;
8508 case NL80211_IFTYPE_STATION:
8509 case NL80211_IFTYPE_ADHOC:
8510 case NL80211_IFTYPE_P2P_CLIENT:
8511 case NL80211_IFTYPE_AP:
8512 case NL80211_IFTYPE_AP_VLAN:
8513 case NL80211_IFTYPE_MESH_POINT:
8514 case NL80211_IFTYPE_P2P_GO:
8515 break;
8516 default:
8517 return -EOPNOTSUPP;
8518 }
8519
8520 if (info->attrs[NL80211_ATTR_DURATION]) {
8521 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
8522 return -EINVAL;
8523 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
8524
8525 /*
8526 * We should wait on the channel for at least a minimum amount
8527 * of time (10ms) but no longer than the driver supports.
8528 */
8529 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
8530 params.wait > rdev->wiphy.max_remain_on_channel_duration)
8531 return -EINVAL;
8532
8533 }
8534
8535 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
8536
8537 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
8538 return -EINVAL;
8539
8540 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
8541
8542 /* get the channel if any has been specified, otherwise pass NULL to
8543 * the driver. The latter will use the current one
8544 */
8545 chandef.chan = NULL;
8546 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8547 err = nl80211_parse_chandef(rdev, info, &chandef);
8548 if (err)
8549 return err;
8550 }
8551
8552 if (!chandef.chan && params.offchan)
8553 return -EINVAL;
8554
8555 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
8556 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
8557
8558 if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
8559 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8560 int i;
8561
8562 if (len % sizeof(u16))
8563 return -EINVAL;
8564
8565 params.n_csa_offsets = len / sizeof(u16);
8566 params.csa_offsets =
8567 nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
8568
8569 /* check that all the offsets fit the frame */
8570 for (i = 0; i < params.n_csa_offsets; i++) {
8571 if (params.csa_offsets[i] >= params.len)
8572 return -EINVAL;
8573 }
8574 }
8575
8576 if (!params.dont_wait_for_ack) {
8577 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8578 if (!msg)
8579 return -ENOMEM;
8580
8581 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8582 NL80211_CMD_FRAME);
8583 if (!hdr) {
8584 err = -ENOBUFS;
8585 goto free_msg;
8586 }
8587 }
8588
8589 params.chan = chandef.chan;
8590 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
8591 if (err)
8592 goto free_msg;
8593
8594 if (msg) {
8595 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8596 goto nla_put_failure;
8597
8598 genlmsg_end(msg, hdr);
8599 return genlmsg_reply(msg, info);
8600 }
8601
8602 return 0;
8603
8604 nla_put_failure:
8605 err = -ENOBUFS;
8606 free_msg:
8607 nlmsg_free(msg);
8608 return err;
8609}
8610
8611static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
8612{
8613 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8614 struct wireless_dev *wdev = info->user_ptr[1];
8615 u64 cookie;
8616
8617 if (!info->attrs[NL80211_ATTR_COOKIE])
8618 return -EINVAL;
8619
8620 if (!rdev->ops->mgmt_tx_cancel_wait)
8621 return -EOPNOTSUPP;
8622
8623 switch (wdev->iftype) {
8624 case NL80211_IFTYPE_STATION:
8625 case NL80211_IFTYPE_ADHOC:
8626 case NL80211_IFTYPE_P2P_CLIENT:
8627 case NL80211_IFTYPE_AP:
8628 case NL80211_IFTYPE_AP_VLAN:
8629 case NL80211_IFTYPE_P2P_GO:
8630 case NL80211_IFTYPE_P2P_DEVICE:
8631 break;
8632 default:
8633 return -EOPNOTSUPP;
8634 }
8635
8636 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8637
8638 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
8639}
8640
8641static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
8642{
8643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8644 struct wireless_dev *wdev;
8645 struct net_device *dev = info->user_ptr[1];
8646 u8 ps_state;
8647 bool state;
8648 int err;
8649
8650 if (!info->attrs[NL80211_ATTR_PS_STATE])
8651 return -EINVAL;
8652
8653 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
8654
8655 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
8656 return -EINVAL;
8657
8658 wdev = dev->ieee80211_ptr;
8659
8660 if (!rdev->ops->set_power_mgmt)
8661 return -EOPNOTSUPP;
8662
8663 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
8664
8665 if (state == wdev->ps)
8666 return 0;
8667
8668 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
8669 if (!err)
8670 wdev->ps = state;
8671 return err;
8672}
8673
8674static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
8675{
8676 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8677 enum nl80211_ps_state ps_state;
8678 struct wireless_dev *wdev;
8679 struct net_device *dev = info->user_ptr[1];
8680 struct sk_buff *msg;
8681 void *hdr;
8682 int err;
8683
8684 wdev = dev->ieee80211_ptr;
8685
8686 if (!rdev->ops->set_power_mgmt)
8687 return -EOPNOTSUPP;
8688
8689 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8690 if (!msg)
8691 return -ENOMEM;
8692
8693 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8694 NL80211_CMD_GET_POWER_SAVE);
8695 if (!hdr) {
8696 err = -ENOBUFS;
8697 goto free_msg;
8698 }
8699
8700 if (wdev->ps)
8701 ps_state = NL80211_PS_ENABLED;
8702 else
8703 ps_state = NL80211_PS_DISABLED;
8704
8705 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
8706 goto nla_put_failure;
8707
8708 genlmsg_end(msg, hdr);
8709 return genlmsg_reply(msg, info);
8710
8711 nla_put_failure:
8712 err = -ENOBUFS;
8713 free_msg:
8714 nlmsg_free(msg);
8715 return err;
8716}
8717
8718static const struct nla_policy
8719nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
8720 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
8721 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
8722 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
8723 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
8724 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
8725 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
8726};
8727
8728static int nl80211_set_cqm_txe(struct genl_info *info,
8729 u32 rate, u32 pkts, u32 intvl)
8730{
8731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8732 struct net_device *dev = info->user_ptr[1];
8733 struct wireless_dev *wdev = dev->ieee80211_ptr;
8734
8735 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
8736 return -EINVAL;
8737
8738 if (!rdev->ops->set_cqm_txe_config)
8739 return -EOPNOTSUPP;
8740
8741 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8742 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8743 return -EOPNOTSUPP;
8744
8745 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
8746}
8747
8748static int nl80211_set_cqm_rssi(struct genl_info *info,
8749 s32 threshold, u32 hysteresis)
8750{
8751 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8752 struct net_device *dev = info->user_ptr[1];
8753 struct wireless_dev *wdev = dev->ieee80211_ptr;
8754
8755 if (threshold > 0)
8756 return -EINVAL;
8757
8758 /* disabling - hysteresis should also be zero then */
8759 if (threshold == 0)
8760 hysteresis = 0;
8761
8762 if (!rdev->ops->set_cqm_rssi_config)
8763 return -EOPNOTSUPP;
8764
8765 if (wdev->iftype != NL80211_IFTYPE_STATION &&
8766 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
8767 return -EOPNOTSUPP;
8768
8769 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
8770}
8771
8772static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
8773{
8774 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
8775 struct nlattr *cqm;
8776 int err;
8777
8778 cqm = info->attrs[NL80211_ATTR_CQM];
8779 if (!cqm)
8780 return -EINVAL;
8781
8782 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
8783 nl80211_attr_cqm_policy);
8784 if (err)
8785 return err;
8786
8787 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
8788 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
8789 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
8790 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
8791
8792 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
8793 }
8794
8795 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
8796 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
8797 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
8798 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
8799 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
8800 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
8801
8802 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
8803 }
8804
8805 return -EINVAL;
8806}
8807
8808static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
8809{
8810 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8811 struct net_device *dev = info->user_ptr[1];
8812 struct ocb_setup setup = {};
8813 int err;
8814
8815 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8816 if (err)
8817 return err;
8818
8819 return cfg80211_join_ocb(rdev, dev, &setup);
8820}
8821
8822static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
8823{
8824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8825 struct net_device *dev = info->user_ptr[1];
8826
8827 return cfg80211_leave_ocb(rdev, dev);
8828}
8829
8830static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
8831{
8832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8833 struct net_device *dev = info->user_ptr[1];
8834 struct mesh_config cfg;
8835 struct mesh_setup setup;
8836 int err;
8837
8838 /* start with default */
8839 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
8840 memcpy(&setup, &default_mesh_setup, sizeof(setup));
8841
8842 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
8843 /* and parse parameters if given */
8844 err = nl80211_parse_mesh_config(info, &cfg, NULL);
8845 if (err)
8846 return err;
8847 }
8848
8849 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
8850 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
8851 return -EINVAL;
8852
8853 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
8854 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
8855
8856 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8857 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
8858 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8859 return -EINVAL;
8860
8861 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
8862 setup.beacon_interval =
8863 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8864 if (setup.beacon_interval < 10 ||
8865 setup.beacon_interval > 10000)
8866 return -EINVAL;
8867 }
8868
8869 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
8870 setup.dtim_period =
8871 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
8872 if (setup.dtim_period < 1 || setup.dtim_period > 100)
8873 return -EINVAL;
8874 }
8875
8876 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
8877 /* parse additional setup parameters if given */
8878 err = nl80211_parse_mesh_setup(info, &setup);
8879 if (err)
8880 return err;
8881 }
8882
8883 if (setup.user_mpm)
8884 cfg.auto_open_plinks = false;
8885
8886 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8887 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
8888 if (err)
8889 return err;
8890 } else {
8891 /* cfg80211_join_mesh() will sort it out */
8892 setup.chandef.chan = NULL;
8893 }
8894
8895 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8896 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8897 int n_rates =
8898 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8899 struct ieee80211_supported_band *sband;
8900
8901 if (!setup.chandef.chan)
8902 return -EINVAL;
8903
8904 sband = rdev->wiphy.bands[setup.chandef.chan->band];
8905
8906 err = ieee80211_get_ratemask(sband, rates, n_rates,
8907 &setup.basic_rates);
8908 if (err)
8909 return err;
8910 }
8911
8912 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
8913}
8914
8915static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
8916{
8917 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8918 struct net_device *dev = info->user_ptr[1];
8919
8920 return cfg80211_leave_mesh(rdev, dev);
8921}
8922
8923#ifdef CONFIG_PM
8924static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
8925 struct cfg80211_registered_device *rdev)
8926{
8927 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
8928 struct nlattr *nl_pats, *nl_pat;
8929 int i, pat_len;
8930
8931 if (!wowlan->n_patterns)
8932 return 0;
8933
8934 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
8935 if (!nl_pats)
8936 return -ENOBUFS;
8937
8938 for (i = 0; i < wowlan->n_patterns; i++) {
8939 nl_pat = nla_nest_start(msg, i + 1);
8940 if (!nl_pat)
8941 return -ENOBUFS;
8942 pat_len = wowlan->patterns[i].pattern_len;
8943 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
8944 wowlan->patterns[i].mask) ||
8945 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8946 wowlan->patterns[i].pattern) ||
8947 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8948 wowlan->patterns[i].pkt_offset))
8949 return -ENOBUFS;
8950 nla_nest_end(msg, nl_pat);
8951 }
8952 nla_nest_end(msg, nl_pats);
8953
8954 return 0;
8955}
8956
8957static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8958 struct cfg80211_wowlan_tcp *tcp)
8959{
8960 struct nlattr *nl_tcp;
8961
8962 if (!tcp)
8963 return 0;
8964
8965 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
8966 if (!nl_tcp)
8967 return -ENOBUFS;
8968
8969 if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8970 nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8971 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8972 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8973 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
8974 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
8975 tcp->payload_len, tcp->payload) ||
8976 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
8977 tcp->data_interval) ||
8978 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
8979 tcp->wake_len, tcp->wake_data) ||
8980 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
8981 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
8982 return -ENOBUFS;
8983
8984 if (tcp->payload_seq.len &&
8985 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
8986 sizeof(tcp->payload_seq), &tcp->payload_seq))
8987 return -ENOBUFS;
8988
8989 if (tcp->payload_tok.len &&
8990 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
8991 sizeof(tcp->payload_tok) + tcp->tokens_size,
8992 &tcp->payload_tok))
8993 return -ENOBUFS;
8994
8995 nla_nest_end(msg, nl_tcp);
8996
8997 return 0;
8998}
8999
9000static int nl80211_send_wowlan_nd(struct sk_buff *msg,
9001 struct cfg80211_sched_scan_request *req)
9002{
9003 struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
9004 int i;
9005
9006 if (!req)
9007 return 0;
9008
9009 nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
9010 if (!nd)
9011 return -ENOBUFS;
9012
9013 if (req->n_scan_plans == 1 &&
9014 nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
9015 req->scan_plans[0].interval * 1000))
9016 return -ENOBUFS;
9017
9018 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
9019 return -ENOBUFS;
9020
9021 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9022 if (!freqs)
9023 return -ENOBUFS;
9024
9025 for (i = 0; i < req->n_channels; i++)
9026 nla_put_u32(msg, i, req->channels[i]->center_freq);
9027
9028 nla_nest_end(msg, freqs);
9029
9030 if (req->n_match_sets) {
9031 matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
9032 for (i = 0; i < req->n_match_sets; i++) {
9033 match = nla_nest_start(msg, i);
9034 nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
9035 req->match_sets[i].ssid.ssid_len,
9036 req->match_sets[i].ssid.ssid);
9037 nla_nest_end(msg, match);
9038 }
9039 nla_nest_end(msg, matches);
9040 }
9041
9042 scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
9043 if (!scan_plans)
9044 return -ENOBUFS;
9045
9046 for (i = 0; i < req->n_scan_plans; i++) {
9047 scan_plan = nla_nest_start(msg, i + 1);
9048 if (!scan_plan ||
9049 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
9050 req->scan_plans[i].interval) ||
9051 (req->scan_plans[i].iterations &&
9052 nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
9053 req->scan_plans[i].iterations)))
9054 return -ENOBUFS;
9055 nla_nest_end(msg, scan_plan);
9056 }
9057 nla_nest_end(msg, scan_plans);
9058
9059 nla_nest_end(msg, nd);
9060
9061 return 0;
9062}
9063
9064static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
9065{
9066 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9067 struct sk_buff *msg;
9068 void *hdr;
9069 u32 size = NLMSG_DEFAULT_SIZE;
9070
9071 if (!rdev->wiphy.wowlan)
9072 return -EOPNOTSUPP;
9073
9074 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
9075 /* adjust size to have room for all the data */
9076 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
9077 rdev->wiphy.wowlan_config->tcp->payload_len +
9078 rdev->wiphy.wowlan_config->tcp->wake_len +
9079 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
9080 }
9081
9082 msg = nlmsg_new(size, GFP_KERNEL);
9083 if (!msg)
9084 return -ENOMEM;
9085
9086 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9087 NL80211_CMD_GET_WOWLAN);
9088 if (!hdr)
9089 goto nla_put_failure;
9090
9091 if (rdev->wiphy.wowlan_config) {
9092 struct nlattr *nl_wowlan;
9093
9094 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
9095 if (!nl_wowlan)
9096 goto nla_put_failure;
9097
9098 if ((rdev->wiphy.wowlan_config->any &&
9099 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
9100 (rdev->wiphy.wowlan_config->disconnect &&
9101 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
9102 (rdev->wiphy.wowlan_config->magic_pkt &&
9103 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
9104 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
9105 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
9106 (rdev->wiphy.wowlan_config->eap_identity_req &&
9107 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
9108 (rdev->wiphy.wowlan_config->four_way_handshake &&
9109 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
9110 (rdev->wiphy.wowlan_config->rfkill_release &&
9111 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
9112 goto nla_put_failure;
9113
9114 if (nl80211_send_wowlan_patterns(msg, rdev))
9115 goto nla_put_failure;
9116
9117 if (nl80211_send_wowlan_tcp(msg,
9118 rdev->wiphy.wowlan_config->tcp))
9119 goto nla_put_failure;
9120
9121 if (nl80211_send_wowlan_nd(
9122 msg,
9123 rdev->wiphy.wowlan_config->nd_config))
9124 goto nla_put_failure;
9125
9126 nla_nest_end(msg, nl_wowlan);
9127 }
9128
9129 genlmsg_end(msg, hdr);
9130 return genlmsg_reply(msg, info);
9131
9132nla_put_failure:
9133 nlmsg_free(msg);
9134 return -ENOBUFS;
9135}
9136
9137static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
9138 struct nlattr *attr,
9139 struct cfg80211_wowlan *trig)
9140{
9141 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
9142 struct cfg80211_wowlan_tcp *cfg;
9143 struct nl80211_wowlan_tcp_data_token *tok = NULL;
9144 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
9145 u32 size;
9146 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
9147 int err, port;
9148
9149 if (!rdev->wiphy.wowlan->tcp)
9150 return -EINVAL;
9151
9152 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
9153 nla_data(attr), nla_len(attr),
9154 nl80211_wowlan_tcp_policy);
9155 if (err)
9156 return err;
9157
9158 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
9159 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
9160 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
9161 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
9162 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
9163 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
9164 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
9165 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
9166 return -EINVAL;
9167
9168 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
9169 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
9170 return -EINVAL;
9171
9172 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
9173 rdev->wiphy.wowlan->tcp->data_interval_max ||
9174 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
9175 return -EINVAL;
9176
9177 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
9178 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
9179 return -EINVAL;
9180
9181 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
9182 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
9183 return -EINVAL;
9184
9185 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
9186 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
9187
9188 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
9189 tokens_size = tokln - sizeof(*tok);
9190
9191 if (!tok->len || tokens_size % tok->len)
9192 return -EINVAL;
9193 if (!rdev->wiphy.wowlan->tcp->tok)
9194 return -EINVAL;
9195 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
9196 return -EINVAL;
9197 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
9198 return -EINVAL;
9199 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
9200 return -EINVAL;
9201 if (tok->offset + tok->len > data_size)
9202 return -EINVAL;
9203 }
9204
9205 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
9206 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
9207 if (!rdev->wiphy.wowlan->tcp->seq)
9208 return -EINVAL;
9209 if (seq->len == 0 || seq->len > 4)
9210 return -EINVAL;
9211 if (seq->len + seq->offset > data_size)
9212 return -EINVAL;
9213 }
9214
9215 size = sizeof(*cfg);
9216 size += data_size;
9217 size += wake_size + wake_mask_size;
9218 size += tokens_size;
9219
9220 cfg = kzalloc(size, GFP_KERNEL);
9221 if (!cfg)
9222 return -ENOMEM;
9223 cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
9224 cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
9225 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
9226 ETH_ALEN);
9227 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
9228 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
9229 else
9230 port = 0;
9231#ifdef CONFIG_INET
9232 /* allocate a socket and port for it and use it */
9233 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
9234 IPPROTO_TCP, &cfg->sock, 1);
9235 if (err) {
9236 kfree(cfg);
9237 return err;
9238 }
9239 if (inet_csk_get_port(cfg->sock->sk, port)) {
9240 sock_release(cfg->sock);
9241 kfree(cfg);
9242 return -EADDRINUSE;
9243 }
9244 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
9245#else
9246 if (!port) {
9247 kfree(cfg);
9248 return -EINVAL;
9249 }
9250 cfg->src_port = port;
9251#endif
9252
9253 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
9254 cfg->payload_len = data_size;
9255 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
9256 memcpy((void *)cfg->payload,
9257 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
9258 data_size);
9259 if (seq)
9260 cfg->payload_seq = *seq;
9261 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
9262 cfg->wake_len = wake_size;
9263 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
9264 memcpy((void *)cfg->wake_data,
9265 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
9266 wake_size);
9267 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
9268 data_size + wake_size;
9269 memcpy((void *)cfg->wake_mask,
9270 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
9271 wake_mask_size);
9272 if (tok) {
9273 cfg->tokens_size = tokens_size;
9274 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
9275 }
9276
9277 trig->tcp = cfg;
9278
9279 return 0;
9280}
9281
9282static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
9283 const struct wiphy_wowlan_support *wowlan,
9284 struct nlattr *attr,
9285 struct cfg80211_wowlan *trig)
9286{
9287 struct nlattr **tb;
9288 int err;
9289
9290 tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
9291 if (!tb)
9292 return -ENOMEM;
9293
9294 if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
9295 err = -EOPNOTSUPP;
9296 goto out;
9297 }
9298
9299 err = nla_parse(tb, NL80211_ATTR_MAX,
9300 nla_data(attr), nla_len(attr),
9301 nl80211_policy);
9302 if (err)
9303 goto out;
9304
9305 trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb);
9306 err = PTR_ERR_OR_ZERO(trig->nd_config);
9307 if (err)
9308 trig->nd_config = NULL;
9309
9310out:
9311 kfree(tb);
9312 return err;
9313}
9314
9315static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9316{
9317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9318 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
9319 struct cfg80211_wowlan new_triggers = {};
9320 struct cfg80211_wowlan *ntrig;
9321 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
9322 int err, i;
9323 bool prev_enabled = rdev->wiphy.wowlan_config;
9324 bool regular = false;
9325
9326 if (!wowlan)
9327 return -EOPNOTSUPP;
9328
9329 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
9330 cfg80211_rdev_free_wowlan(rdev);
9331 rdev->wiphy.wowlan_config = NULL;
9332 goto set_wakeup;
9333 }
9334
9335 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
9336 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
9337 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
9338 nl80211_wowlan_policy);
9339 if (err)
9340 return err;
9341
9342 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
9343 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
9344 return -EINVAL;
9345 new_triggers.any = true;
9346 }
9347
9348 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
9349 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
9350 return -EINVAL;
9351 new_triggers.disconnect = true;
9352 regular = true;
9353 }
9354
9355 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
9356 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
9357 return -EINVAL;
9358 new_triggers.magic_pkt = true;
9359 regular = true;
9360 }
9361
9362 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
9363 return -EINVAL;
9364
9365 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
9366 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
9367 return -EINVAL;
9368 new_triggers.gtk_rekey_failure = true;
9369 regular = true;
9370 }
9371
9372 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
9373 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
9374 return -EINVAL;
9375 new_triggers.eap_identity_req = true;
9376 regular = true;
9377 }
9378
9379 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
9380 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
9381 return -EINVAL;
9382 new_triggers.four_way_handshake = true;
9383 regular = true;
9384 }
9385
9386 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
9387 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
9388 return -EINVAL;
9389 new_triggers.rfkill_release = true;
9390 regular = true;
9391 }
9392
9393 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
9394 struct nlattr *pat;
9395 int n_patterns = 0;
9396 int rem, pat_len, mask_len, pkt_offset;
9397 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9398
9399 regular = true;
9400
9401 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
9402 rem)
9403 n_patterns++;
9404 if (n_patterns > wowlan->n_patterns)
9405 return -EINVAL;
9406
9407 new_triggers.patterns = kcalloc(n_patterns,
9408 sizeof(new_triggers.patterns[0]),
9409 GFP_KERNEL);
9410 if (!new_triggers.patterns)
9411 return -ENOMEM;
9412
9413 new_triggers.n_patterns = n_patterns;
9414 i = 0;
9415
9416 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
9417 rem) {
9418 u8 *mask_pat;
9419
9420 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9421 nla_len(pat), nl80211_packet_pattern_policy);
9422 err = -EINVAL;
9423 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9424 !pat_tb[NL80211_PKTPAT_PATTERN])
9425 goto error;
9426 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9427 mask_len = DIV_ROUND_UP(pat_len, 8);
9428 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9429 goto error;
9430 if (pat_len > wowlan->pattern_max_len ||
9431 pat_len < wowlan->pattern_min_len)
9432 goto error;
9433
9434 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9435 pkt_offset = 0;
9436 else
9437 pkt_offset = nla_get_u32(
9438 pat_tb[NL80211_PKTPAT_OFFSET]);
9439 if (pkt_offset > wowlan->max_pkt_offset)
9440 goto error;
9441 new_triggers.patterns[i].pkt_offset = pkt_offset;
9442
9443 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9444 if (!mask_pat) {
9445 err = -ENOMEM;
9446 goto error;
9447 }
9448 new_triggers.patterns[i].mask = mask_pat;
9449 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9450 mask_len);
9451 mask_pat += mask_len;
9452 new_triggers.patterns[i].pattern = mask_pat;
9453 new_triggers.patterns[i].pattern_len = pat_len;
9454 memcpy(mask_pat,
9455 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9456 pat_len);
9457 i++;
9458 }
9459 }
9460
9461 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
9462 regular = true;
9463 err = nl80211_parse_wowlan_tcp(
9464 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
9465 &new_triggers);
9466 if (err)
9467 goto error;
9468 }
9469
9470 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
9471 regular = true;
9472 err = nl80211_parse_wowlan_nd(
9473 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
9474 &new_triggers);
9475 if (err)
9476 goto error;
9477 }
9478
9479 /* The 'any' trigger means the device continues operating more or less
9480 * as in its normal operation mode and wakes up the host on most of the
9481 * normal interrupts (like packet RX, ...)
9482 * It therefore makes little sense to combine with the more constrained
9483 * wakeup trigger modes.
9484 */
9485 if (new_triggers.any && regular) {
9486 err = -EINVAL;
9487 goto error;
9488 }
9489
9490 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
9491 if (!ntrig) {
9492 err = -ENOMEM;
9493 goto error;
9494 }
9495 cfg80211_rdev_free_wowlan(rdev);
9496 rdev->wiphy.wowlan_config = ntrig;
9497
9498 set_wakeup:
9499 if (rdev->ops->set_wakeup &&
9500 prev_enabled != !!rdev->wiphy.wowlan_config)
9501 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
9502
9503 return 0;
9504 error:
9505 for (i = 0; i < new_triggers.n_patterns; i++)
9506 kfree(new_triggers.patterns[i].mask);
9507 kfree(new_triggers.patterns);
9508 if (new_triggers.tcp && new_triggers.tcp->sock)
9509 sock_release(new_triggers.tcp->sock);
9510 kfree(new_triggers.tcp);
9511 kfree(new_triggers.nd_config);
9512 return err;
9513}
9514#endif
9515
9516static int nl80211_send_coalesce_rules(struct sk_buff *msg,
9517 struct cfg80211_registered_device *rdev)
9518{
9519 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
9520 int i, j, pat_len;
9521 struct cfg80211_coalesce_rules *rule;
9522
9523 if (!rdev->coalesce->n_rules)
9524 return 0;
9525
9526 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
9527 if (!nl_rules)
9528 return -ENOBUFS;
9529
9530 for (i = 0; i < rdev->coalesce->n_rules; i++) {
9531 nl_rule = nla_nest_start(msg, i + 1);
9532 if (!nl_rule)
9533 return -ENOBUFS;
9534
9535 rule = &rdev->coalesce->rules[i];
9536 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
9537 rule->delay))
9538 return -ENOBUFS;
9539
9540 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
9541 rule->condition))
9542 return -ENOBUFS;
9543
9544 nl_pats = nla_nest_start(msg,
9545 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
9546 if (!nl_pats)
9547 return -ENOBUFS;
9548
9549 for (j = 0; j < rule->n_patterns; j++) {
9550 nl_pat = nla_nest_start(msg, j + 1);
9551 if (!nl_pat)
9552 return -ENOBUFS;
9553 pat_len = rule->patterns[j].pattern_len;
9554 if (nla_put(msg, NL80211_PKTPAT_MASK,
9555 DIV_ROUND_UP(pat_len, 8),
9556 rule->patterns[j].mask) ||
9557 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
9558 rule->patterns[j].pattern) ||
9559 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
9560 rule->patterns[j].pkt_offset))
9561 return -ENOBUFS;
9562 nla_nest_end(msg, nl_pat);
9563 }
9564 nla_nest_end(msg, nl_pats);
9565 nla_nest_end(msg, nl_rule);
9566 }
9567 nla_nest_end(msg, nl_rules);
9568
9569 return 0;
9570}
9571
9572static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
9573{
9574 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9575 struct sk_buff *msg;
9576 void *hdr;
9577
9578 if (!rdev->wiphy.coalesce)
9579 return -EOPNOTSUPP;
9580
9581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9582 if (!msg)
9583 return -ENOMEM;
9584
9585 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9586 NL80211_CMD_GET_COALESCE);
9587 if (!hdr)
9588 goto nla_put_failure;
9589
9590 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
9591 goto nla_put_failure;
9592
9593 genlmsg_end(msg, hdr);
9594 return genlmsg_reply(msg, info);
9595
9596nla_put_failure:
9597 nlmsg_free(msg);
9598 return -ENOBUFS;
9599}
9600
9601void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
9602{
9603 struct cfg80211_coalesce *coalesce = rdev->coalesce;
9604 int i, j;
9605 struct cfg80211_coalesce_rules *rule;
9606
9607 if (!coalesce)
9608 return;
9609
9610 for (i = 0; i < coalesce->n_rules; i++) {
9611 rule = &coalesce->rules[i];
9612 for (j = 0; j < rule->n_patterns; j++)
9613 kfree(rule->patterns[j].mask);
9614 kfree(rule->patterns);
9615 }
9616 kfree(coalesce->rules);
9617 kfree(coalesce);
9618 rdev->coalesce = NULL;
9619}
9620
9621static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
9622 struct nlattr *rule,
9623 struct cfg80211_coalesce_rules *new_rule)
9624{
9625 int err, i;
9626 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9627 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
9628 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
9629 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9630
9631 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
9632 nla_len(rule), nl80211_coalesce_policy);
9633 if (err)
9634 return err;
9635
9636 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
9637 new_rule->delay =
9638 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
9639 if (new_rule->delay > coalesce->max_delay)
9640 return -EINVAL;
9641
9642 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
9643 new_rule->condition =
9644 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
9645 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
9646 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
9647 return -EINVAL;
9648
9649 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
9650 return -EINVAL;
9651
9652 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9653 rem)
9654 n_patterns++;
9655 if (n_patterns > coalesce->n_patterns)
9656 return -EINVAL;
9657
9658 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
9659 GFP_KERNEL);
9660 if (!new_rule->patterns)
9661 return -ENOMEM;
9662
9663 new_rule->n_patterns = n_patterns;
9664 i = 0;
9665
9666 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
9667 rem) {
9668 u8 *mask_pat;
9669
9670 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
9671 nla_len(pat), nl80211_packet_pattern_policy);
9672 if (!pat_tb[NL80211_PKTPAT_MASK] ||
9673 !pat_tb[NL80211_PKTPAT_PATTERN])
9674 return -EINVAL;
9675 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
9676 mask_len = DIV_ROUND_UP(pat_len, 8);
9677 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
9678 return -EINVAL;
9679 if (pat_len > coalesce->pattern_max_len ||
9680 pat_len < coalesce->pattern_min_len)
9681 return -EINVAL;
9682
9683 if (!pat_tb[NL80211_PKTPAT_OFFSET])
9684 pkt_offset = 0;
9685 else
9686 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
9687 if (pkt_offset > coalesce->max_pkt_offset)
9688 return -EINVAL;
9689 new_rule->patterns[i].pkt_offset = pkt_offset;
9690
9691 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
9692 if (!mask_pat)
9693 return -ENOMEM;
9694
9695 new_rule->patterns[i].mask = mask_pat;
9696 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
9697 mask_len);
9698
9699 mask_pat += mask_len;
9700 new_rule->patterns[i].pattern = mask_pat;
9701 new_rule->patterns[i].pattern_len = pat_len;
9702 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
9703 pat_len);
9704 i++;
9705 }
9706
9707 return 0;
9708}
9709
9710static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
9711{
9712 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9713 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
9714 struct cfg80211_coalesce new_coalesce = {};
9715 struct cfg80211_coalesce *n_coalesce;
9716 int err, rem_rule, n_rules = 0, i, j;
9717 struct nlattr *rule;
9718 struct cfg80211_coalesce_rules *tmp_rule;
9719
9720 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
9721 return -EOPNOTSUPP;
9722
9723 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
9724 cfg80211_rdev_free_coalesce(rdev);
9725 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
9726 return 0;
9727 }
9728
9729 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9730 rem_rule)
9731 n_rules++;
9732 if (n_rules > coalesce->n_rules)
9733 return -EINVAL;
9734
9735 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
9736 GFP_KERNEL);
9737 if (!new_coalesce.rules)
9738 return -ENOMEM;
9739
9740 new_coalesce.n_rules = n_rules;
9741 i = 0;
9742
9743 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
9744 rem_rule) {
9745 err = nl80211_parse_coalesce_rule(rdev, rule,
9746 &new_coalesce.rules[i]);
9747 if (err)
9748 goto error;
9749
9750 i++;
9751 }
9752
9753 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
9754 if (err)
9755 goto error;
9756
9757 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
9758 if (!n_coalesce) {
9759 err = -ENOMEM;
9760 goto error;
9761 }
9762 cfg80211_rdev_free_coalesce(rdev);
9763 rdev->coalesce = n_coalesce;
9764
9765 return 0;
9766error:
9767 for (i = 0; i < new_coalesce.n_rules; i++) {
9768 tmp_rule = &new_coalesce.rules[i];
9769 for (j = 0; j < tmp_rule->n_patterns; j++)
9770 kfree(tmp_rule->patterns[j].mask);
9771 kfree(tmp_rule->patterns);
9772 }
9773 kfree(new_coalesce.rules);
9774
9775 return err;
9776}
9777
9778static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
9779{
9780 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9781 struct net_device *dev = info->user_ptr[1];
9782 struct wireless_dev *wdev = dev->ieee80211_ptr;
9783 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
9784 struct cfg80211_gtk_rekey_data rekey_data;
9785 int err;
9786
9787 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
9788 return -EINVAL;
9789
9790 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
9791 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
9792 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
9793 nl80211_rekey_policy);
9794 if (err)
9795 return err;
9796
9797 if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
9798 !tb[NL80211_REKEY_DATA_KCK])
9799 return -EINVAL;
9800 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
9801 return -ERANGE;
9802 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
9803 return -ERANGE;
9804 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
9805 return -ERANGE;
9806
9807 rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
9808 rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
9809 rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
9810
9811 wdev_lock(wdev);
9812 if (!wdev->current_bss) {
9813 err = -ENOTCONN;
9814 goto out;
9815 }
9816
9817 if (!rdev->ops->set_rekey_data) {
9818 err = -EOPNOTSUPP;
9819 goto out;
9820 }
9821
9822 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
9823 out:
9824 wdev_unlock(wdev);
9825 return err;
9826}
9827
9828static int nl80211_register_unexpected_frame(struct sk_buff *skb,
9829 struct genl_info *info)
9830{
9831 struct net_device *dev = info->user_ptr[1];
9832 struct wireless_dev *wdev = dev->ieee80211_ptr;
9833
9834 if (wdev->iftype != NL80211_IFTYPE_AP &&
9835 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9836 return -EINVAL;
9837
9838 if (wdev->ap_unexpected_nlportid)
9839 return -EBUSY;
9840
9841 wdev->ap_unexpected_nlportid = info->snd_portid;
9842 return 0;
9843}
9844
9845static int nl80211_probe_client(struct sk_buff *skb,
9846 struct genl_info *info)
9847{
9848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9849 struct net_device *dev = info->user_ptr[1];
9850 struct wireless_dev *wdev = dev->ieee80211_ptr;
9851 struct sk_buff *msg;
9852 void *hdr;
9853 const u8 *addr;
9854 u64 cookie;
9855 int err;
9856
9857 if (wdev->iftype != NL80211_IFTYPE_AP &&
9858 wdev->iftype != NL80211_IFTYPE_P2P_GO)
9859 return -EOPNOTSUPP;
9860
9861 if (!info->attrs[NL80211_ATTR_MAC])
9862 return -EINVAL;
9863
9864 if (!rdev->ops->probe_client)
9865 return -EOPNOTSUPP;
9866
9867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9868 if (!msg)
9869 return -ENOMEM;
9870
9871 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9872 NL80211_CMD_PROBE_CLIENT);
9873 if (!hdr) {
9874 err = -ENOBUFS;
9875 goto free_msg;
9876 }
9877
9878 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
9879
9880 err = rdev_probe_client(rdev, dev, addr, &cookie);
9881 if (err)
9882 goto free_msg;
9883
9884 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9885 goto nla_put_failure;
9886
9887 genlmsg_end(msg, hdr);
9888
9889 return genlmsg_reply(msg, info);
9890
9891 nla_put_failure:
9892 err = -ENOBUFS;
9893 free_msg:
9894 nlmsg_free(msg);
9895 return err;
9896}
9897
9898static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
9899{
9900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9901 struct cfg80211_beacon_registration *reg, *nreg;
9902 int rv;
9903
9904 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
9905 return -EOPNOTSUPP;
9906
9907 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
9908 if (!nreg)
9909 return -ENOMEM;
9910
9911 /* First, check if already registered. */
9912 spin_lock_bh(&rdev->beacon_registrations_lock);
9913 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
9914 if (reg->nlportid == info->snd_portid) {
9915 rv = -EALREADY;
9916 goto out_err;
9917 }
9918 }
9919 /* Add it to the list */
9920 nreg->nlportid = info->snd_portid;
9921 list_add(&nreg->list, &rdev->beacon_registrations);
9922
9923 spin_unlock_bh(&rdev->beacon_registrations_lock);
9924
9925 return 0;
9926out_err:
9927 spin_unlock_bh(&rdev->beacon_registrations_lock);
9928 kfree(nreg);
9929 return rv;
9930}
9931
9932static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
9933{
9934 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9935 struct wireless_dev *wdev = info->user_ptr[1];
9936 int err;
9937
9938 if (!rdev->ops->start_p2p_device)
9939 return -EOPNOTSUPP;
9940
9941 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9942 return -EOPNOTSUPP;
9943
9944 if (wdev->p2p_started)
9945 return 0;
9946
9947 if (rfkill_blocked(rdev->rfkill))
9948 return -ERFKILL;
9949
9950 err = rdev_start_p2p_device(rdev, wdev);
9951 if (err)
9952 return err;
9953
9954 wdev->p2p_started = true;
9955 rdev->opencount++;
9956
9957 return 0;
9958}
9959
9960static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
9961{
9962 struct cfg80211_registered_device *rdev = info->user_ptr[0];
9963 struct wireless_dev *wdev = info->user_ptr[1];
9964
9965 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
9966 return -EOPNOTSUPP;
9967
9968 if (!rdev->ops->stop_p2p_device)
9969 return -EOPNOTSUPP;
9970
9971 cfg80211_stop_p2p_device(rdev, wdev);
9972
9973 return 0;
9974}
9975
9976static int nl80211_get_protocol_features(struct sk_buff *skb,
9977 struct genl_info *info)
9978{
9979 void *hdr;
9980 struct sk_buff *msg;
9981
9982 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9983 if (!msg)
9984 return -ENOMEM;
9985
9986 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9987 NL80211_CMD_GET_PROTOCOL_FEATURES);
9988 if (!hdr)
9989 goto nla_put_failure;
9990
9991 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
9992 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
9993 goto nla_put_failure;
9994
9995 genlmsg_end(msg, hdr);
9996 return genlmsg_reply(msg, info);
9997
9998 nla_put_failure:
9999 kfree_skb(msg);
10000 return -ENOBUFS;
10001}
10002
10003static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
10004{
10005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10006 struct cfg80211_update_ft_ies_params ft_params;
10007 struct net_device *dev = info->user_ptr[1];
10008
10009 if (!rdev->ops->update_ft_ies)
10010 return -EOPNOTSUPP;
10011
10012 if (!info->attrs[NL80211_ATTR_MDID] ||
10013 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
10014 return -EINVAL;
10015
10016 memset(&ft_params, 0, sizeof(ft_params));
10017 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
10018 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10019 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10020
10021 return rdev_update_ft_ies(rdev, dev, &ft_params);
10022}
10023
10024static int nl80211_crit_protocol_start(struct sk_buff *skb,
10025 struct genl_info *info)
10026{
10027 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10028 struct wireless_dev *wdev = info->user_ptr[1];
10029 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
10030 u16 duration;
10031 int ret;
10032
10033 if (!rdev->ops->crit_proto_start)
10034 return -EOPNOTSUPP;
10035
10036 if (WARN_ON(!rdev->ops->crit_proto_stop))
10037 return -EINVAL;
10038
10039 if (rdev->crit_proto_nlportid)
10040 return -EBUSY;
10041
10042 /* determine protocol if provided */
10043 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
10044 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
10045
10046 if (proto >= NUM_NL80211_CRIT_PROTO)
10047 return -EINVAL;
10048
10049 /* timeout must be provided */
10050 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
10051 return -EINVAL;
10052
10053 duration =
10054 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
10055
10056 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
10057 return -ERANGE;
10058
10059 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
10060 if (!ret)
10061 rdev->crit_proto_nlportid = info->snd_portid;
10062
10063 return ret;
10064}
10065
10066static int nl80211_crit_protocol_stop(struct sk_buff *skb,
10067 struct genl_info *info)
10068{
10069 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10070 struct wireless_dev *wdev = info->user_ptr[1];
10071
10072 if (!rdev->ops->crit_proto_stop)
10073 return -EOPNOTSUPP;
10074
10075 if (rdev->crit_proto_nlportid) {
10076 rdev->crit_proto_nlportid = 0;
10077 rdev_crit_proto_stop(rdev, wdev);
10078 }
10079 return 0;
10080}
10081
10082static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
10083{
10084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10085 struct wireless_dev *wdev =
10086 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
10087 int i, err;
10088 u32 vid, subcmd;
10089
10090 if (!rdev->wiphy.vendor_commands)
10091 return -EOPNOTSUPP;
10092
10093 if (IS_ERR(wdev)) {
10094 err = PTR_ERR(wdev);
10095 if (err != -EINVAL)
10096 return err;
10097 wdev = NULL;
10098 } else if (wdev->wiphy != &rdev->wiphy) {
10099 return -EINVAL;
10100 }
10101
10102 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
10103 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
10104 return -EINVAL;
10105
10106 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
10107 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
10108 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
10109 const struct wiphy_vendor_command *vcmd;
10110 void *data = NULL;
10111 int len = 0;
10112
10113 vcmd = &rdev->wiphy.vendor_commands[i];
10114
10115 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
10116 continue;
10117
10118 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
10119 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
10120 if (!wdev)
10121 return -EINVAL;
10122 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
10123 !wdev->netdev)
10124 return -EINVAL;
10125
10126 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
10127 if (wdev->netdev &&
10128 !netif_running(wdev->netdev))
10129 return -ENETDOWN;
10130 if (!wdev->netdev && !wdev->p2p_started)
10131 return -ENETDOWN;
10132 }
10133
10134 if (!vcmd->doit)
10135 return -EOPNOTSUPP;
10136 } else {
10137 wdev = NULL;
10138 }
10139
10140 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
10141 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
10142 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
10143 }
10144
10145 rdev->cur_cmd_info = info;
10146 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
10147 data, len);
10148 rdev->cur_cmd_info = NULL;
10149 return err;
10150 }
10151
10152 return -EOPNOTSUPP;
10153}
10154
10155static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
10156 struct netlink_callback *cb,
10157 struct cfg80211_registered_device **rdev,
10158 struct wireless_dev **wdev)
10159{
10160 u32 vid, subcmd;
10161 unsigned int i;
10162 int vcmd_idx = -1;
10163 int err;
10164 void *data = NULL;
10165 unsigned int data_len = 0;
10166
10167 if (cb->args[0]) {
10168 /* subtract the 1 again here */
10169 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
10170 struct wireless_dev *tmp;
10171
10172 if (!wiphy)
10173 return -ENODEV;
10174 *rdev = wiphy_to_rdev(wiphy);
10175 *wdev = NULL;
10176
10177 if (cb->args[1]) {
10178 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
10179 if (tmp->identifier == cb->args[1] - 1) {
10180 *wdev = tmp;
10181 break;
10182 }
10183 }
10184 }
10185
10186 /* keep rtnl locked in successful case */
10187 return 0;
10188 }
10189
10190 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
10191 nl80211_fam.attrbuf, nl80211_fam.maxattr,
10192 nl80211_policy);
10193 if (err)
10194 return err;
10195
10196 if (!nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_ID] ||
10197 !nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
10198 return -EINVAL;
10199
10200 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
10201 nl80211_fam.attrbuf);
10202 if (IS_ERR(*wdev))
10203 *wdev = NULL;
10204
10205 *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
10206 nl80211_fam.attrbuf);
10207 if (IS_ERR(*rdev))
10208 return PTR_ERR(*rdev);
10209
10210 vid = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_ID]);
10211 subcmd = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
10212
10213 for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
10214 const struct wiphy_vendor_command *vcmd;
10215
10216 vcmd = &(*rdev)->wiphy.vendor_commands[i];
10217
10218 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
10219 continue;
10220
10221 if (!vcmd->dumpit)
10222 return -EOPNOTSUPP;
10223
10224 vcmd_idx = i;
10225 break;
10226 }
10227
10228 if (vcmd_idx < 0)
10229 return -EOPNOTSUPP;
10230
10231 if (nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]) {
10232 data = nla_data(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]);
10233 data_len = nla_len(nl80211_fam.attrbuf[NL80211_ATTR_VENDOR_DATA]);
10234 }
10235
10236 /* 0 is the first index - add 1 to parse only once */
10237 cb->args[0] = (*rdev)->wiphy_idx + 1;
10238 /* add 1 to know if it was NULL */
10239 cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
10240 cb->args[2] = vcmd_idx;
10241 cb->args[3] = (unsigned long)data;
10242 cb->args[4] = data_len;
10243
10244 /* keep rtnl locked in successful case */
10245 return 0;
10246}
10247
10248static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
10249 struct netlink_callback *cb)
10250{
10251 struct cfg80211_registered_device *rdev;
10252 struct wireless_dev *wdev;
10253 unsigned int vcmd_idx;
10254 const struct wiphy_vendor_command *vcmd;
10255 void *data;
10256 int data_len;
10257 int err;
10258 struct nlattr *vendor_data;
10259
10260 rtnl_lock();
10261 err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
10262 if (err)
10263 goto out;
10264
10265 vcmd_idx = cb->args[2];
10266 data = (void *)cb->args[3];
10267 data_len = cb->args[4];
10268 vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
10269
10270 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
10271 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
10272 if (!wdev) {
10273 err = -EINVAL;
10274 goto out;
10275 }
10276 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
10277 !wdev->netdev) {
10278 err = -EINVAL;
10279 goto out;
10280 }
10281
10282 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
10283 if (wdev->netdev &&
10284 !netif_running(wdev->netdev)) {
10285 err = -ENETDOWN;
10286 goto out;
10287 }
10288 if (!wdev->netdev && !wdev->p2p_started) {
10289 err = -ENETDOWN;
10290 goto out;
10291 }
10292 }
10293 }
10294
10295 while (1) {
10296 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
10297 cb->nlh->nlmsg_seq, NLM_F_MULTI,
10298 NL80211_CMD_VENDOR);
10299 if (!hdr)
10300 break;
10301
10302 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10303 (wdev && nla_put_u64(skb, NL80211_ATTR_WDEV,
10304 wdev_id(wdev)))) {
10305 genlmsg_cancel(skb, hdr);
10306 break;
10307 }
10308
10309 vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
10310 if (!vendor_data) {
10311 genlmsg_cancel(skb, hdr);
10312 break;
10313 }
10314
10315 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
10316 (unsigned long *)&cb->args[5]);
10317 nla_nest_end(skb, vendor_data);
10318
10319 if (err == -ENOBUFS || err == -ENOENT) {
10320 genlmsg_cancel(skb, hdr);
10321 break;
10322 } else if (err) {
10323 genlmsg_cancel(skb, hdr);
10324 goto out;
10325 }
10326
10327 genlmsg_end(skb, hdr);
10328 }
10329
10330 err = skb->len;
10331 out:
10332 rtnl_unlock();
10333 return err;
10334}
10335
10336struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
10337 enum nl80211_commands cmd,
10338 enum nl80211_attrs attr,
10339 int approxlen)
10340{
10341 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
10342
10343 if (WARN_ON(!rdev->cur_cmd_info))
10344 return NULL;
10345
10346 return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
10347 rdev->cur_cmd_info->snd_portid,
10348 rdev->cur_cmd_info->snd_seq,
10349 cmd, attr, NULL, GFP_KERNEL);
10350}
10351EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
10352
10353int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
10354{
10355 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
10356 void *hdr = ((void **)skb->cb)[1];
10357 struct nlattr *data = ((void **)skb->cb)[2];
10358
10359 /* clear CB data for netlink core to own from now on */
10360 memset(skb->cb, 0, sizeof(skb->cb));
10361
10362 if (WARN_ON(!rdev->cur_cmd_info)) {
10363 kfree_skb(skb);
10364 return -EINVAL;
10365 }
10366
10367 nla_nest_end(skb, data);
10368 genlmsg_end(skb, hdr);
10369 return genlmsg_reply(skb, rdev->cur_cmd_info);
10370}
10371EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
10372
10373
10374static int nl80211_set_qos_map(struct sk_buff *skb,
10375 struct genl_info *info)
10376{
10377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10378 struct cfg80211_qos_map *qos_map = NULL;
10379 struct net_device *dev = info->user_ptr[1];
10380 u8 *pos, len, num_des, des_len, des;
10381 int ret;
10382
10383 if (!rdev->ops->set_qos_map)
10384 return -EOPNOTSUPP;
10385
10386 if (info->attrs[NL80211_ATTR_QOS_MAP]) {
10387 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
10388 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
10389
10390 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
10391 len > IEEE80211_QOS_MAP_LEN_MAX)
10392 return -EINVAL;
10393
10394 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
10395 if (!qos_map)
10396 return -ENOMEM;
10397
10398 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
10399 if (num_des) {
10400 des_len = num_des *
10401 sizeof(struct cfg80211_dscp_exception);
10402 memcpy(qos_map->dscp_exception, pos, des_len);
10403 qos_map->num_des = num_des;
10404 for (des = 0; des < num_des; des++) {
10405 if (qos_map->dscp_exception[des].up > 7) {
10406 kfree(qos_map);
10407 return -EINVAL;
10408 }
10409 }
10410 pos += des_len;
10411 }
10412 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
10413 }
10414
10415 wdev_lock(dev->ieee80211_ptr);
10416 ret = nl80211_key_allowed(dev->ieee80211_ptr);
10417 if (!ret)
10418 ret = rdev_set_qos_map(rdev, dev, qos_map);
10419 wdev_unlock(dev->ieee80211_ptr);
10420
10421 kfree(qos_map);
10422 return ret;
10423}
10424
10425static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
10426{
10427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10428 struct net_device *dev = info->user_ptr[1];
10429 struct wireless_dev *wdev = dev->ieee80211_ptr;
10430 const u8 *peer;
10431 u8 tsid, up;
10432 u16 admitted_time = 0;
10433 int err;
10434
10435 if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
10436 return -EOPNOTSUPP;
10437
10438 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
10439 !info->attrs[NL80211_ATTR_USER_PRIO])
10440 return -EINVAL;
10441
10442 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
10443 if (tsid >= IEEE80211_NUM_TIDS)
10444 return -EINVAL;
10445
10446 up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
10447 if (up >= IEEE80211_NUM_UPS)
10448 return -EINVAL;
10449
10450 /* WMM uses TIDs 0-7 even for TSPEC */
10451 if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
10452 /* TODO: handle 802.11 TSPEC/admission control
10453 * need more attributes for that (e.g. BA session requirement);
10454 * change the WMM adminssion test above to allow both then
10455 */
10456 return -EINVAL;
10457 }
10458
10459 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10460
10461 if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
10462 admitted_time =
10463 nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
10464 if (!admitted_time)
10465 return -EINVAL;
10466 }
10467
10468 wdev_lock(wdev);
10469 switch (wdev->iftype) {
10470 case NL80211_IFTYPE_STATION:
10471 case NL80211_IFTYPE_P2P_CLIENT:
10472 if (wdev->current_bss)
10473 break;
10474 err = -ENOTCONN;
10475 goto out;
10476 default:
10477 err = -EOPNOTSUPP;
10478 goto out;
10479 }
10480
10481 err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
10482
10483 out:
10484 wdev_unlock(wdev);
10485 return err;
10486}
10487
10488static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
10489{
10490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10491 struct net_device *dev = info->user_ptr[1];
10492 struct wireless_dev *wdev = dev->ieee80211_ptr;
10493 const u8 *peer;
10494 u8 tsid;
10495 int err;
10496
10497 if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
10498 return -EINVAL;
10499
10500 tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
10501 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10502
10503 wdev_lock(wdev);
10504 err = rdev_del_tx_ts(rdev, dev, tsid, peer);
10505 wdev_unlock(wdev);
10506
10507 return err;
10508}
10509
10510static int nl80211_tdls_channel_switch(struct sk_buff *skb,
10511 struct genl_info *info)
10512{
10513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10514 struct net_device *dev = info->user_ptr[1];
10515 struct wireless_dev *wdev = dev->ieee80211_ptr;
10516 struct cfg80211_chan_def chandef = {};
10517 const u8 *addr;
10518 u8 oper_class;
10519 int err;
10520
10521 if (!rdev->ops->tdls_channel_switch ||
10522 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
10523 return -EOPNOTSUPP;
10524
10525 switch (dev->ieee80211_ptr->iftype) {
10526 case NL80211_IFTYPE_STATION:
10527 case NL80211_IFTYPE_P2P_CLIENT:
10528 break;
10529 default:
10530 return -EOPNOTSUPP;
10531 }
10532
10533 if (!info->attrs[NL80211_ATTR_MAC] ||
10534 !info->attrs[NL80211_ATTR_OPER_CLASS])
10535 return -EINVAL;
10536
10537 err = nl80211_parse_chandef(rdev, info, &chandef);
10538 if (err)
10539 return err;
10540
10541 /*
10542 * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
10543 * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
10544 * specification is not defined for them.
10545 */
10546 if (chandef.chan->band == IEEE80211_BAND_2GHZ &&
10547 chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
10548 chandef.width != NL80211_CHAN_WIDTH_20)
10549 return -EINVAL;
10550
10551 /* we will be active on the TDLS link */
10552 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
10553 wdev->iftype))
10554 return -EINVAL;
10555
10556 /* don't allow switching to DFS channels */
10557 if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
10558 return -EINVAL;
10559
10560 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
10561 oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
10562
10563 wdev_lock(wdev);
10564 err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
10565 wdev_unlock(wdev);
10566
10567 return err;
10568}
10569
10570static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
10571 struct genl_info *info)
10572{
10573 struct cfg80211_registered_device *rdev = info->user_ptr[0];
10574 struct net_device *dev = info->user_ptr[1];
10575 struct wireless_dev *wdev = dev->ieee80211_ptr;
10576 const u8 *addr;
10577
10578 if (!rdev->ops->tdls_channel_switch ||
10579 !rdev->ops->tdls_cancel_channel_switch ||
10580 !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
10581 return -EOPNOTSUPP;
10582
10583 switch (dev->ieee80211_ptr->iftype) {
10584 case NL80211_IFTYPE_STATION:
10585 case NL80211_IFTYPE_P2P_CLIENT:
10586 break;
10587 default:
10588 return -EOPNOTSUPP;
10589 }
10590
10591 if (!info->attrs[NL80211_ATTR_MAC])
10592 return -EINVAL;
10593
10594 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
10595
10596 wdev_lock(wdev);
10597 rdev_tdls_cancel_channel_switch(rdev, dev, addr);
10598 wdev_unlock(wdev);
10599
10600 return 0;
10601}
10602
10603#define NL80211_FLAG_NEED_WIPHY 0x01
10604#define NL80211_FLAG_NEED_NETDEV 0x02
10605#define NL80211_FLAG_NEED_RTNL 0x04
10606#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
10607#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
10608 NL80211_FLAG_CHECK_NETDEV_UP)
10609#define NL80211_FLAG_NEED_WDEV 0x10
10610/* If a netdev is associated, it must be UP, P2P must be started */
10611#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
10612 NL80211_FLAG_CHECK_NETDEV_UP)
10613#define NL80211_FLAG_CLEAR_SKB 0x20
10614
10615static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
10616 struct genl_info *info)
10617{
10618 struct cfg80211_registered_device *rdev;
10619 struct wireless_dev *wdev;
10620 struct net_device *dev;
10621 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
10622
10623 if (rtnl)
10624 rtnl_lock();
10625
10626 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
10627 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
10628 if (IS_ERR(rdev)) {
10629 if (rtnl)
10630 rtnl_unlock();
10631 return PTR_ERR(rdev);
10632 }
10633 info->user_ptr[0] = rdev;
10634 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
10635 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
10636 ASSERT_RTNL();
10637
10638 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
10639 info->attrs);
10640 if (IS_ERR(wdev)) {
10641 if (rtnl)
10642 rtnl_unlock();
10643 return PTR_ERR(wdev);
10644 }
10645
10646 dev = wdev->netdev;
10647 rdev = wiphy_to_rdev(wdev->wiphy);
10648
10649 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
10650 if (!dev) {
10651 if (rtnl)
10652 rtnl_unlock();
10653 return -EINVAL;
10654 }
10655
10656 info->user_ptr[1] = dev;
10657 } else {
10658 info->user_ptr[1] = wdev;
10659 }
10660
10661 if (dev) {
10662 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
10663 !netif_running(dev)) {
10664 if (rtnl)
10665 rtnl_unlock();
10666 return -ENETDOWN;
10667 }
10668
10669 dev_hold(dev);
10670 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
10671 if (!wdev->p2p_started) {
10672 if (rtnl)
10673 rtnl_unlock();
10674 return -ENETDOWN;
10675 }
10676 }
10677
10678 info->user_ptr[0] = rdev;
10679 }
10680
10681 return 0;
10682}
10683
10684static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
10685 struct genl_info *info)
10686{
10687 if (info->user_ptr[1]) {
10688 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
10689 struct wireless_dev *wdev = info->user_ptr[1];
10690
10691 if (wdev->netdev)
10692 dev_put(wdev->netdev);
10693 } else {
10694 dev_put(info->user_ptr[1]);
10695 }
10696 }
10697
10698 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
10699 rtnl_unlock();
10700
10701 /* If needed, clear the netlink message payload from the SKB
10702 * as it might contain key data that shouldn't stick around on
10703 * the heap after the SKB is freed. The netlink message header
10704 * is still needed for further processing, so leave it intact.
10705 */
10706 if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
10707 struct nlmsghdr *nlh = nlmsg_hdr(skb);
10708
10709 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
10710 }
10711}
10712
10713static const struct genl_ops nl80211_ops[] = {
10714 {
10715 .cmd = NL80211_CMD_GET_WIPHY,
10716 .doit = nl80211_get_wiphy,
10717 .dumpit = nl80211_dump_wiphy,
10718 .done = nl80211_dump_wiphy_done,
10719 .policy = nl80211_policy,
10720 /* can be retrieved by unprivileged users */
10721 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10722 NL80211_FLAG_NEED_RTNL,
10723 },
10724 {
10725 .cmd = NL80211_CMD_SET_WIPHY,
10726 .doit = nl80211_set_wiphy,
10727 .policy = nl80211_policy,
10728 .flags = GENL_ADMIN_PERM,
10729 .internal_flags = NL80211_FLAG_NEED_RTNL,
10730 },
10731 {
10732 .cmd = NL80211_CMD_GET_INTERFACE,
10733 .doit = nl80211_get_interface,
10734 .dumpit = nl80211_dump_interface,
10735 .policy = nl80211_policy,
10736 /* can be retrieved by unprivileged users */
10737 .internal_flags = NL80211_FLAG_NEED_WDEV |
10738 NL80211_FLAG_NEED_RTNL,
10739 },
10740 {
10741 .cmd = NL80211_CMD_SET_INTERFACE,
10742 .doit = nl80211_set_interface,
10743 .policy = nl80211_policy,
10744 .flags = GENL_ADMIN_PERM,
10745 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10746 NL80211_FLAG_NEED_RTNL,
10747 },
10748 {
10749 .cmd = NL80211_CMD_NEW_INTERFACE,
10750 .doit = nl80211_new_interface,
10751 .policy = nl80211_policy,
10752 .flags = GENL_ADMIN_PERM,
10753 .internal_flags = NL80211_FLAG_NEED_WIPHY |
10754 NL80211_FLAG_NEED_RTNL,
10755 },
10756 {
10757 .cmd = NL80211_CMD_DEL_INTERFACE,
10758 .doit = nl80211_del_interface,
10759 .policy = nl80211_policy,
10760 .flags = GENL_ADMIN_PERM,
10761 .internal_flags = NL80211_FLAG_NEED_WDEV |
10762 NL80211_FLAG_NEED_RTNL,
10763 },
10764 {
10765 .cmd = NL80211_CMD_GET_KEY,
10766 .doit = nl80211_get_key,
10767 .policy = nl80211_policy,
10768 .flags = GENL_ADMIN_PERM,
10769 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10770 NL80211_FLAG_NEED_RTNL,
10771 },
10772 {
10773 .cmd = NL80211_CMD_SET_KEY,
10774 .doit = nl80211_set_key,
10775 .policy = nl80211_policy,
10776 .flags = GENL_ADMIN_PERM,
10777 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10778 NL80211_FLAG_NEED_RTNL |
10779 NL80211_FLAG_CLEAR_SKB,
10780 },
10781 {
10782 .cmd = NL80211_CMD_NEW_KEY,
10783 .doit = nl80211_new_key,
10784 .policy = nl80211_policy,
10785 .flags = GENL_ADMIN_PERM,
10786 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10787 NL80211_FLAG_NEED_RTNL |
10788 NL80211_FLAG_CLEAR_SKB,
10789 },
10790 {
10791 .cmd = NL80211_CMD_DEL_KEY,
10792 .doit = nl80211_del_key,
10793 .policy = nl80211_policy,
10794 .flags = GENL_ADMIN_PERM,
10795 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10796 NL80211_FLAG_NEED_RTNL,
10797 },
10798 {
10799 .cmd = NL80211_CMD_SET_BEACON,
10800 .policy = nl80211_policy,
10801 .flags = GENL_ADMIN_PERM,
10802 .doit = nl80211_set_beacon,
10803 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10804 NL80211_FLAG_NEED_RTNL,
10805 },
10806 {
10807 .cmd = NL80211_CMD_START_AP,
10808 .policy = nl80211_policy,
10809 .flags = GENL_ADMIN_PERM,
10810 .doit = nl80211_start_ap,
10811 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10812 NL80211_FLAG_NEED_RTNL,
10813 },
10814 {
10815 .cmd = NL80211_CMD_STOP_AP,
10816 .policy = nl80211_policy,
10817 .flags = GENL_ADMIN_PERM,
10818 .doit = nl80211_stop_ap,
10819 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10820 NL80211_FLAG_NEED_RTNL,
10821 },
10822 {
10823 .cmd = NL80211_CMD_GET_STATION,
10824 .doit = nl80211_get_station,
10825 .dumpit = nl80211_dump_station,
10826 .policy = nl80211_policy,
10827 .internal_flags = NL80211_FLAG_NEED_NETDEV |
10828 NL80211_FLAG_NEED_RTNL,
10829 },
10830 {
10831 .cmd = NL80211_CMD_SET_STATION,
10832 .doit = nl80211_set_station,
10833 .policy = nl80211_policy,
10834 .flags = GENL_ADMIN_PERM,
10835 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10836 NL80211_FLAG_NEED_RTNL,
10837 },
10838 {
10839 .cmd = NL80211_CMD_NEW_STATION,
10840 .doit = nl80211_new_station,
10841 .policy = nl80211_policy,
10842 .flags = GENL_ADMIN_PERM,
10843 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10844 NL80211_FLAG_NEED_RTNL,
10845 },
10846 {
10847 .cmd = NL80211_CMD_DEL_STATION,
10848 .doit = nl80211_del_station,
10849 .policy = nl80211_policy,
10850 .flags = GENL_ADMIN_PERM,
10851 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10852 NL80211_FLAG_NEED_RTNL,
10853 },
10854 {
10855 .cmd = NL80211_CMD_GET_MPATH,
10856 .doit = nl80211_get_mpath,
10857 .dumpit = nl80211_dump_mpath,
10858 .policy = nl80211_policy,
10859 .flags = GENL_ADMIN_PERM,
10860 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10861 NL80211_FLAG_NEED_RTNL,
10862 },
10863 {
10864 .cmd = NL80211_CMD_GET_MPP,
10865 .doit = nl80211_get_mpp,
10866 .dumpit = nl80211_dump_mpp,
10867 .policy = nl80211_policy,
10868 .flags = GENL_ADMIN_PERM,
10869 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10870 NL80211_FLAG_NEED_RTNL,
10871 },
10872 {
10873 .cmd = NL80211_CMD_SET_MPATH,
10874 .doit = nl80211_set_mpath,
10875 .policy = nl80211_policy,
10876 .flags = GENL_ADMIN_PERM,
10877 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10878 NL80211_FLAG_NEED_RTNL,
10879 },
10880 {
10881 .cmd = NL80211_CMD_NEW_MPATH,
10882 .doit = nl80211_new_mpath,
10883 .policy = nl80211_policy,
10884 .flags = GENL_ADMIN_PERM,
10885 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10886 NL80211_FLAG_NEED_RTNL,
10887 },
10888 {
10889 .cmd = NL80211_CMD_DEL_MPATH,
10890 .doit = nl80211_del_mpath,
10891 .policy = nl80211_policy,
10892 .flags = GENL_ADMIN_PERM,
10893 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10894 NL80211_FLAG_NEED_RTNL,
10895 },
10896 {
10897 .cmd = NL80211_CMD_SET_BSS,
10898 .doit = nl80211_set_bss,
10899 .policy = nl80211_policy,
10900 .flags = GENL_ADMIN_PERM,
10901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10902 NL80211_FLAG_NEED_RTNL,
10903 },
10904 {
10905 .cmd = NL80211_CMD_GET_REG,
10906 .doit = nl80211_get_reg_do,
10907 .dumpit = nl80211_get_reg_dump,
10908 .policy = nl80211_policy,
10909 .internal_flags = NL80211_FLAG_NEED_RTNL,
10910 /* can be retrieved by unprivileged users */
10911 },
10912#ifdef CONFIG_CFG80211_CRDA_SUPPORT
10913 {
10914 .cmd = NL80211_CMD_SET_REG,
10915 .doit = nl80211_set_reg,
10916 .policy = nl80211_policy,
10917 .flags = GENL_ADMIN_PERM,
10918 .internal_flags = NL80211_FLAG_NEED_RTNL,
10919 },
10920#endif
10921 {
10922 .cmd = NL80211_CMD_REQ_SET_REG,
10923 .doit = nl80211_req_set_reg,
10924 .policy = nl80211_policy,
10925 .flags = GENL_ADMIN_PERM,
10926 },
10927 {
10928 .cmd = NL80211_CMD_GET_MESH_CONFIG,
10929 .doit = nl80211_get_mesh_config,
10930 .policy = nl80211_policy,
10931 /* can be retrieved by unprivileged users */
10932 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10933 NL80211_FLAG_NEED_RTNL,
10934 },
10935 {
10936 .cmd = NL80211_CMD_SET_MESH_CONFIG,
10937 .doit = nl80211_update_mesh_config,
10938 .policy = nl80211_policy,
10939 .flags = GENL_ADMIN_PERM,
10940 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10941 NL80211_FLAG_NEED_RTNL,
10942 },
10943 {
10944 .cmd = NL80211_CMD_TRIGGER_SCAN,
10945 .doit = nl80211_trigger_scan,
10946 .policy = nl80211_policy,
10947 .flags = GENL_ADMIN_PERM,
10948 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
10949 NL80211_FLAG_NEED_RTNL,
10950 },
10951 {
10952 .cmd = NL80211_CMD_GET_SCAN,
10953 .policy = nl80211_policy,
10954 .dumpit = nl80211_dump_scan,
10955 },
10956 {
10957 .cmd = NL80211_CMD_START_SCHED_SCAN,
10958 .doit = nl80211_start_sched_scan,
10959 .policy = nl80211_policy,
10960 .flags = GENL_ADMIN_PERM,
10961 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10962 NL80211_FLAG_NEED_RTNL,
10963 },
10964 {
10965 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
10966 .doit = nl80211_stop_sched_scan,
10967 .policy = nl80211_policy,
10968 .flags = GENL_ADMIN_PERM,
10969 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10970 NL80211_FLAG_NEED_RTNL,
10971 },
10972 {
10973 .cmd = NL80211_CMD_AUTHENTICATE,
10974 .doit = nl80211_authenticate,
10975 .policy = nl80211_policy,
10976 .flags = GENL_ADMIN_PERM,
10977 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10978 NL80211_FLAG_NEED_RTNL |
10979 NL80211_FLAG_CLEAR_SKB,
10980 },
10981 {
10982 .cmd = NL80211_CMD_ASSOCIATE,
10983 .doit = nl80211_associate,
10984 .policy = nl80211_policy,
10985 .flags = GENL_ADMIN_PERM,
10986 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10987 NL80211_FLAG_NEED_RTNL,
10988 },
10989 {
10990 .cmd = NL80211_CMD_DEAUTHENTICATE,
10991 .doit = nl80211_deauthenticate,
10992 .policy = nl80211_policy,
10993 .flags = GENL_ADMIN_PERM,
10994 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
10995 NL80211_FLAG_NEED_RTNL,
10996 },
10997 {
10998 .cmd = NL80211_CMD_DISASSOCIATE,
10999 .doit = nl80211_disassociate,
11000 .policy = nl80211_policy,
11001 .flags = GENL_ADMIN_PERM,
11002 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11003 NL80211_FLAG_NEED_RTNL,
11004 },
11005 {
11006 .cmd = NL80211_CMD_JOIN_IBSS,
11007 .doit = nl80211_join_ibss,
11008 .policy = nl80211_policy,
11009 .flags = GENL_ADMIN_PERM,
11010 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11011 NL80211_FLAG_NEED_RTNL,
11012 },
11013 {
11014 .cmd = NL80211_CMD_LEAVE_IBSS,
11015 .doit = nl80211_leave_ibss,
11016 .policy = nl80211_policy,
11017 .flags = GENL_ADMIN_PERM,
11018 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11019 NL80211_FLAG_NEED_RTNL,
11020 },
11021#ifdef CONFIG_NL80211_TESTMODE
11022 {
11023 .cmd = NL80211_CMD_TESTMODE,
11024 .doit = nl80211_testmode_do,
11025 .dumpit = nl80211_testmode_dump,
11026 .policy = nl80211_policy,
11027 .flags = GENL_ADMIN_PERM,
11028 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11029 NL80211_FLAG_NEED_RTNL,
11030 },
11031#endif
11032 {
11033 .cmd = NL80211_CMD_CONNECT,
11034 .doit = nl80211_connect,
11035 .policy = nl80211_policy,
11036 .flags = GENL_ADMIN_PERM,
11037 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11038 NL80211_FLAG_NEED_RTNL,
11039 },
11040 {
11041 .cmd = NL80211_CMD_DISCONNECT,
11042 .doit = nl80211_disconnect,
11043 .policy = nl80211_policy,
11044 .flags = GENL_ADMIN_PERM,
11045 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11046 NL80211_FLAG_NEED_RTNL,
11047 },
11048 {
11049 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
11050 .doit = nl80211_wiphy_netns,
11051 .policy = nl80211_policy,
11052 .flags = GENL_ADMIN_PERM,
11053 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11054 NL80211_FLAG_NEED_RTNL,
11055 },
11056 {
11057 .cmd = NL80211_CMD_GET_SURVEY,
11058 .policy = nl80211_policy,
11059 .dumpit = nl80211_dump_survey,
11060 },
11061 {
11062 .cmd = NL80211_CMD_SET_PMKSA,
11063 .doit = nl80211_setdel_pmksa,
11064 .policy = nl80211_policy,
11065 .flags = GENL_ADMIN_PERM,
11066 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11067 NL80211_FLAG_NEED_RTNL,
11068 },
11069 {
11070 .cmd = NL80211_CMD_DEL_PMKSA,
11071 .doit = nl80211_setdel_pmksa,
11072 .policy = nl80211_policy,
11073 .flags = GENL_ADMIN_PERM,
11074 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11075 NL80211_FLAG_NEED_RTNL,
11076 },
11077 {
11078 .cmd = NL80211_CMD_FLUSH_PMKSA,
11079 .doit = nl80211_flush_pmksa,
11080 .policy = nl80211_policy,
11081 .flags = GENL_ADMIN_PERM,
11082 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11083 NL80211_FLAG_NEED_RTNL,
11084 },
11085 {
11086 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
11087 .doit = nl80211_remain_on_channel,
11088 .policy = nl80211_policy,
11089 .flags = GENL_ADMIN_PERM,
11090 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11091 NL80211_FLAG_NEED_RTNL,
11092 },
11093 {
11094 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
11095 .doit = nl80211_cancel_remain_on_channel,
11096 .policy = nl80211_policy,
11097 .flags = GENL_ADMIN_PERM,
11098 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11099 NL80211_FLAG_NEED_RTNL,
11100 },
11101 {
11102 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
11103 .doit = nl80211_set_tx_bitrate_mask,
11104 .policy = nl80211_policy,
11105 .flags = GENL_ADMIN_PERM,
11106 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11107 NL80211_FLAG_NEED_RTNL,
11108 },
11109 {
11110 .cmd = NL80211_CMD_REGISTER_FRAME,
11111 .doit = nl80211_register_mgmt,
11112 .policy = nl80211_policy,
11113 .flags = GENL_ADMIN_PERM,
11114 .internal_flags = NL80211_FLAG_NEED_WDEV |
11115 NL80211_FLAG_NEED_RTNL,
11116 },
11117 {
11118 .cmd = NL80211_CMD_FRAME,
11119 .doit = nl80211_tx_mgmt,
11120 .policy = nl80211_policy,
11121 .flags = GENL_ADMIN_PERM,
11122 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11123 NL80211_FLAG_NEED_RTNL,
11124 },
11125 {
11126 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
11127 .doit = nl80211_tx_mgmt_cancel_wait,
11128 .policy = nl80211_policy,
11129 .flags = GENL_ADMIN_PERM,
11130 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11131 NL80211_FLAG_NEED_RTNL,
11132 },
11133 {
11134 .cmd = NL80211_CMD_SET_POWER_SAVE,
11135 .doit = nl80211_set_power_save,
11136 .policy = nl80211_policy,
11137 .flags = GENL_ADMIN_PERM,
11138 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11139 NL80211_FLAG_NEED_RTNL,
11140 },
11141 {
11142 .cmd = NL80211_CMD_GET_POWER_SAVE,
11143 .doit = nl80211_get_power_save,
11144 .policy = nl80211_policy,
11145 /* can be retrieved by unprivileged users */
11146 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11147 NL80211_FLAG_NEED_RTNL,
11148 },
11149 {
11150 .cmd = NL80211_CMD_SET_CQM,
11151 .doit = nl80211_set_cqm,
11152 .policy = nl80211_policy,
11153 .flags = GENL_ADMIN_PERM,
11154 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11155 NL80211_FLAG_NEED_RTNL,
11156 },
11157 {
11158 .cmd = NL80211_CMD_SET_CHANNEL,
11159 .doit = nl80211_set_channel,
11160 .policy = nl80211_policy,
11161 .flags = GENL_ADMIN_PERM,
11162 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11163 NL80211_FLAG_NEED_RTNL,
11164 },
11165 {
11166 .cmd = NL80211_CMD_SET_WDS_PEER,
11167 .doit = nl80211_set_wds_peer,
11168 .policy = nl80211_policy,
11169 .flags = GENL_ADMIN_PERM,
11170 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11171 NL80211_FLAG_NEED_RTNL,
11172 },
11173 {
11174 .cmd = NL80211_CMD_JOIN_MESH,
11175 .doit = nl80211_join_mesh,
11176 .policy = nl80211_policy,
11177 .flags = GENL_ADMIN_PERM,
11178 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11179 NL80211_FLAG_NEED_RTNL,
11180 },
11181 {
11182 .cmd = NL80211_CMD_LEAVE_MESH,
11183 .doit = nl80211_leave_mesh,
11184 .policy = nl80211_policy,
11185 .flags = GENL_ADMIN_PERM,
11186 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11187 NL80211_FLAG_NEED_RTNL,
11188 },
11189 {
11190 .cmd = NL80211_CMD_JOIN_OCB,
11191 .doit = nl80211_join_ocb,
11192 .policy = nl80211_policy,
11193 .flags = GENL_ADMIN_PERM,
11194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11195 NL80211_FLAG_NEED_RTNL,
11196 },
11197 {
11198 .cmd = NL80211_CMD_LEAVE_OCB,
11199 .doit = nl80211_leave_ocb,
11200 .policy = nl80211_policy,
11201 .flags = GENL_ADMIN_PERM,
11202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11203 NL80211_FLAG_NEED_RTNL,
11204 },
11205#ifdef CONFIG_PM
11206 {
11207 .cmd = NL80211_CMD_GET_WOWLAN,
11208 .doit = nl80211_get_wowlan,
11209 .policy = nl80211_policy,
11210 /* can be retrieved by unprivileged users */
11211 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11212 NL80211_FLAG_NEED_RTNL,
11213 },
11214 {
11215 .cmd = NL80211_CMD_SET_WOWLAN,
11216 .doit = nl80211_set_wowlan,
11217 .policy = nl80211_policy,
11218 .flags = GENL_ADMIN_PERM,
11219 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11220 NL80211_FLAG_NEED_RTNL,
11221 },
11222#endif
11223 {
11224 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
11225 .doit = nl80211_set_rekey_data,
11226 .policy = nl80211_policy,
11227 .flags = GENL_ADMIN_PERM,
11228 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11229 NL80211_FLAG_NEED_RTNL |
11230 NL80211_FLAG_CLEAR_SKB,
11231 },
11232 {
11233 .cmd = NL80211_CMD_TDLS_MGMT,
11234 .doit = nl80211_tdls_mgmt,
11235 .policy = nl80211_policy,
11236 .flags = GENL_ADMIN_PERM,
11237 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11238 NL80211_FLAG_NEED_RTNL,
11239 },
11240 {
11241 .cmd = NL80211_CMD_TDLS_OPER,
11242 .doit = nl80211_tdls_oper,
11243 .policy = nl80211_policy,
11244 .flags = GENL_ADMIN_PERM,
11245 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11246 NL80211_FLAG_NEED_RTNL,
11247 },
11248 {
11249 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
11250 .doit = nl80211_register_unexpected_frame,
11251 .policy = nl80211_policy,
11252 .flags = GENL_ADMIN_PERM,
11253 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11254 NL80211_FLAG_NEED_RTNL,
11255 },
11256 {
11257 .cmd = NL80211_CMD_PROBE_CLIENT,
11258 .doit = nl80211_probe_client,
11259 .policy = nl80211_policy,
11260 .flags = GENL_ADMIN_PERM,
11261 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11262 NL80211_FLAG_NEED_RTNL,
11263 },
11264 {
11265 .cmd = NL80211_CMD_REGISTER_BEACONS,
11266 .doit = nl80211_register_beacons,
11267 .policy = nl80211_policy,
11268 .flags = GENL_ADMIN_PERM,
11269 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11270 NL80211_FLAG_NEED_RTNL,
11271 },
11272 {
11273 .cmd = NL80211_CMD_SET_NOACK_MAP,
11274 .doit = nl80211_set_noack_map,
11275 .policy = nl80211_policy,
11276 .flags = GENL_ADMIN_PERM,
11277 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11278 NL80211_FLAG_NEED_RTNL,
11279 },
11280 {
11281 .cmd = NL80211_CMD_START_P2P_DEVICE,
11282 .doit = nl80211_start_p2p_device,
11283 .policy = nl80211_policy,
11284 .flags = GENL_ADMIN_PERM,
11285 .internal_flags = NL80211_FLAG_NEED_WDEV |
11286 NL80211_FLAG_NEED_RTNL,
11287 },
11288 {
11289 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
11290 .doit = nl80211_stop_p2p_device,
11291 .policy = nl80211_policy,
11292 .flags = GENL_ADMIN_PERM,
11293 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11294 NL80211_FLAG_NEED_RTNL,
11295 },
11296 {
11297 .cmd = NL80211_CMD_SET_MCAST_RATE,
11298 .doit = nl80211_set_mcast_rate,
11299 .policy = nl80211_policy,
11300 .flags = GENL_ADMIN_PERM,
11301 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11302 NL80211_FLAG_NEED_RTNL,
11303 },
11304 {
11305 .cmd = NL80211_CMD_SET_MAC_ACL,
11306 .doit = nl80211_set_mac_acl,
11307 .policy = nl80211_policy,
11308 .flags = GENL_ADMIN_PERM,
11309 .internal_flags = NL80211_FLAG_NEED_NETDEV |
11310 NL80211_FLAG_NEED_RTNL,
11311 },
11312 {
11313 .cmd = NL80211_CMD_RADAR_DETECT,
11314 .doit = nl80211_start_radar_detection,
11315 .policy = nl80211_policy,
11316 .flags = GENL_ADMIN_PERM,
11317 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11318 NL80211_FLAG_NEED_RTNL,
11319 },
11320 {
11321 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
11322 .doit = nl80211_get_protocol_features,
11323 .policy = nl80211_policy,
11324 },
11325 {
11326 .cmd = NL80211_CMD_UPDATE_FT_IES,
11327 .doit = nl80211_update_ft_ies,
11328 .policy = nl80211_policy,
11329 .flags = GENL_ADMIN_PERM,
11330 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11331 NL80211_FLAG_NEED_RTNL,
11332 },
11333 {
11334 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
11335 .doit = nl80211_crit_protocol_start,
11336 .policy = nl80211_policy,
11337 .flags = GENL_ADMIN_PERM,
11338 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11339 NL80211_FLAG_NEED_RTNL,
11340 },
11341 {
11342 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
11343 .doit = nl80211_crit_protocol_stop,
11344 .policy = nl80211_policy,
11345 .flags = GENL_ADMIN_PERM,
11346 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
11347 NL80211_FLAG_NEED_RTNL,
11348 },
11349 {
11350 .cmd = NL80211_CMD_GET_COALESCE,
11351 .doit = nl80211_get_coalesce,
11352 .policy = nl80211_policy,
11353 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11354 NL80211_FLAG_NEED_RTNL,
11355 },
11356 {
11357 .cmd = NL80211_CMD_SET_COALESCE,
11358 .doit = nl80211_set_coalesce,
11359 .policy = nl80211_policy,
11360 .flags = GENL_ADMIN_PERM,
11361 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11362 NL80211_FLAG_NEED_RTNL,
11363 },
11364 {
11365 .cmd = NL80211_CMD_CHANNEL_SWITCH,
11366 .doit = nl80211_channel_switch,
11367 .policy = nl80211_policy,
11368 .flags = GENL_ADMIN_PERM,
11369 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11370 NL80211_FLAG_NEED_RTNL,
11371 },
11372 {
11373 .cmd = NL80211_CMD_VENDOR,
11374 .doit = nl80211_vendor_cmd,
11375 .dumpit = nl80211_vendor_cmd_dump,
11376 .policy = nl80211_policy,
11377 .flags = GENL_ADMIN_PERM,
11378 .internal_flags = NL80211_FLAG_NEED_WIPHY |
11379 NL80211_FLAG_NEED_RTNL,
11380 },
11381 {
11382 .cmd = NL80211_CMD_SET_QOS_MAP,
11383 .doit = nl80211_set_qos_map,
11384 .policy = nl80211_policy,
11385 .flags = GENL_ADMIN_PERM,
11386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11387 NL80211_FLAG_NEED_RTNL,
11388 },
11389 {
11390 .cmd = NL80211_CMD_ADD_TX_TS,
11391 .doit = nl80211_add_tx_ts,
11392 .policy = nl80211_policy,
11393 .flags = GENL_ADMIN_PERM,
11394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11395 NL80211_FLAG_NEED_RTNL,
11396 },
11397 {
11398 .cmd = NL80211_CMD_DEL_TX_TS,
11399 .doit = nl80211_del_tx_ts,
11400 .policy = nl80211_policy,
11401 .flags = GENL_ADMIN_PERM,
11402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11403 NL80211_FLAG_NEED_RTNL,
11404 },
11405 {
11406 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
11407 .doit = nl80211_tdls_channel_switch,
11408 .policy = nl80211_policy,
11409 .flags = GENL_ADMIN_PERM,
11410 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11411 NL80211_FLAG_NEED_RTNL,
11412 },
11413 {
11414 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
11415 .doit = nl80211_tdls_cancel_channel_switch,
11416 .policy = nl80211_policy,
11417 .flags = GENL_ADMIN_PERM,
11418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
11419 NL80211_FLAG_NEED_RTNL,
11420 },
11421};
11422
11423/* notification functions */
11424
11425void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
11426 enum nl80211_commands cmd)
11427{
11428 struct sk_buff *msg;
11429 struct nl80211_dump_wiphy_state state = {};
11430
11431 WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
11432 cmd != NL80211_CMD_DEL_WIPHY);
11433
11434 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11435 if (!msg)
11436 return;
11437
11438 if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
11439 nlmsg_free(msg);
11440 return;
11441 }
11442
11443 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11444 NL80211_MCGRP_CONFIG, GFP_KERNEL);
11445}
11446
11447static int nl80211_add_scan_req(struct sk_buff *msg,
11448 struct cfg80211_registered_device *rdev)
11449{
11450 struct cfg80211_scan_request *req = rdev->scan_req;
11451 struct nlattr *nest;
11452 int i;
11453
11454 if (WARN_ON(!req))
11455 return 0;
11456
11457 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
11458 if (!nest)
11459 goto nla_put_failure;
11460 for (i = 0; i < req->n_ssids; i++) {
11461 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
11462 goto nla_put_failure;
11463 }
11464 nla_nest_end(msg, nest);
11465
11466 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11467 if (!nest)
11468 goto nla_put_failure;
11469 for (i = 0; i < req->n_channels; i++) {
11470 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11471 goto nla_put_failure;
11472 }
11473 nla_nest_end(msg, nest);
11474
11475 if (req->ie &&
11476 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
11477 goto nla_put_failure;
11478
11479 if (req->flags &&
11480 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
11481 goto nla_put_failure;
11482
11483 return 0;
11484 nla_put_failure:
11485 return -ENOBUFS;
11486}
11487
11488static int nl80211_send_scan_msg(struct sk_buff *msg,
11489 struct cfg80211_registered_device *rdev,
11490 struct wireless_dev *wdev,
11491 u32 portid, u32 seq, int flags,
11492 u32 cmd)
11493{
11494 void *hdr;
11495
11496 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
11497 if (!hdr)
11498 return -1;
11499
11500 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11501 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11502 wdev->netdev->ifindex)) ||
11503 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11504 goto nla_put_failure;
11505
11506 /* ignore errors and send incomplete event anyway */
11507 nl80211_add_scan_req(msg, rdev);
11508
11509 genlmsg_end(msg, hdr);
11510 return 0;
11511
11512 nla_put_failure:
11513 genlmsg_cancel(msg, hdr);
11514 return -EMSGSIZE;
11515}
11516
11517static int
11518nl80211_send_sched_scan_msg(struct sk_buff *msg,
11519 struct cfg80211_registered_device *rdev,
11520 struct net_device *netdev,
11521 u32 portid, u32 seq, int flags, u32 cmd)
11522{
11523 void *hdr;
11524
11525 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
11526 if (!hdr)
11527 return -1;
11528
11529 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11530 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
11531 goto nla_put_failure;
11532
11533 genlmsg_end(msg, hdr);
11534 return 0;
11535
11536 nla_put_failure:
11537 genlmsg_cancel(msg, hdr);
11538 return -EMSGSIZE;
11539}
11540
11541void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
11542 struct wireless_dev *wdev)
11543{
11544 struct sk_buff *msg;
11545
11546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11547 if (!msg)
11548 return;
11549
11550 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
11551 NL80211_CMD_TRIGGER_SCAN) < 0) {
11552 nlmsg_free(msg);
11553 return;
11554 }
11555
11556 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11557 NL80211_MCGRP_SCAN, GFP_KERNEL);
11558}
11559
11560struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
11561 struct wireless_dev *wdev, bool aborted)
11562{
11563 struct sk_buff *msg;
11564
11565 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11566 if (!msg)
11567 return NULL;
11568
11569 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
11570 aborted ? NL80211_CMD_SCAN_ABORTED :
11571 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
11572 nlmsg_free(msg);
11573 return NULL;
11574 }
11575
11576 return msg;
11577}
11578
11579void nl80211_send_scan_result(struct cfg80211_registered_device *rdev,
11580 struct sk_buff *msg)
11581{
11582 if (!msg)
11583 return;
11584
11585 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11586 NL80211_MCGRP_SCAN, GFP_KERNEL);
11587}
11588
11589void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
11590 struct net_device *netdev)
11591{
11592 struct sk_buff *msg;
11593
11594 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11595 if (!msg)
11596 return;
11597
11598 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
11599 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
11600 nlmsg_free(msg);
11601 return;
11602 }
11603
11604 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11605 NL80211_MCGRP_SCAN, GFP_KERNEL);
11606}
11607
11608void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
11609 struct net_device *netdev, u32 cmd)
11610{
11611 struct sk_buff *msg;
11612
11613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11614 if (!msg)
11615 return;
11616
11617 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
11618 nlmsg_free(msg);
11619 return;
11620 }
11621
11622 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11623 NL80211_MCGRP_SCAN, GFP_KERNEL);
11624}
11625
11626static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
11627 struct regulatory_request *request)
11628{
11629 /* Userspace can always count this one always being set */
11630 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
11631 goto nla_put_failure;
11632
11633 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
11634 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11635 NL80211_REGDOM_TYPE_WORLD))
11636 goto nla_put_failure;
11637 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
11638 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11639 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
11640 goto nla_put_failure;
11641 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
11642 request->intersect) {
11643 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11644 NL80211_REGDOM_TYPE_INTERSECTION))
11645 goto nla_put_failure;
11646 } else {
11647 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
11648 NL80211_REGDOM_TYPE_COUNTRY) ||
11649 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
11650 request->alpha2))
11651 goto nla_put_failure;
11652 }
11653
11654 if (request->wiphy_idx != WIPHY_IDX_INVALID) {
11655 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
11656
11657 if (wiphy &&
11658 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
11659 goto nla_put_failure;
11660
11661 if (wiphy &&
11662 wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
11663 nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
11664 goto nla_put_failure;
11665 }
11666
11667 return true;
11668
11669nla_put_failure:
11670 return false;
11671}
11672
11673/*
11674 * This can happen on global regulatory changes or device specific settings
11675 * based on custom regulatory domains.
11676 */
11677void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
11678 struct regulatory_request *request)
11679{
11680 struct sk_buff *msg;
11681 void *hdr;
11682
11683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11684 if (!msg)
11685 return;
11686
11687 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
11688 if (!hdr) {
11689 nlmsg_free(msg);
11690 return;
11691 }
11692
11693 if (nl80211_reg_change_event_fill(msg, request) == false)
11694 goto nla_put_failure;
11695
11696 genlmsg_end(msg, hdr);
11697
11698 rcu_read_lock();
11699 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
11700 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
11701 rcu_read_unlock();
11702
11703 return;
11704
11705nla_put_failure:
11706 genlmsg_cancel(msg, hdr);
11707 nlmsg_free(msg);
11708}
11709
11710static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
11711 struct net_device *netdev,
11712 const u8 *buf, size_t len,
11713 enum nl80211_commands cmd, gfp_t gfp,
11714 int uapsd_queues)
11715{
11716 struct sk_buff *msg;
11717 void *hdr;
11718
11719 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11720 if (!msg)
11721 return;
11722
11723 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11724 if (!hdr) {
11725 nlmsg_free(msg);
11726 return;
11727 }
11728
11729 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11730 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11731 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
11732 goto nla_put_failure;
11733
11734 if (uapsd_queues >= 0) {
11735 struct nlattr *nla_wmm =
11736 nla_nest_start(msg, NL80211_ATTR_STA_WME);
11737 if (!nla_wmm)
11738 goto nla_put_failure;
11739
11740 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
11741 uapsd_queues))
11742 goto nla_put_failure;
11743
11744 nla_nest_end(msg, nla_wmm);
11745 }
11746
11747 genlmsg_end(msg, hdr);
11748
11749 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11750 NL80211_MCGRP_MLME, gfp);
11751 return;
11752
11753 nla_put_failure:
11754 genlmsg_cancel(msg, hdr);
11755 nlmsg_free(msg);
11756}
11757
11758void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
11759 struct net_device *netdev, const u8 *buf,
11760 size_t len, gfp_t gfp)
11761{
11762 nl80211_send_mlme_event(rdev, netdev, buf, len,
11763 NL80211_CMD_AUTHENTICATE, gfp, -1);
11764}
11765
11766void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
11767 struct net_device *netdev, const u8 *buf,
11768 size_t len, gfp_t gfp, int uapsd_queues)
11769{
11770 nl80211_send_mlme_event(rdev, netdev, buf, len,
11771 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
11772}
11773
11774void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
11775 struct net_device *netdev, const u8 *buf,
11776 size_t len, gfp_t gfp)
11777{
11778 nl80211_send_mlme_event(rdev, netdev, buf, len,
11779 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
11780}
11781
11782void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
11783 struct net_device *netdev, const u8 *buf,
11784 size_t len, gfp_t gfp)
11785{
11786 nl80211_send_mlme_event(rdev, netdev, buf, len,
11787 NL80211_CMD_DISASSOCIATE, gfp, -1);
11788}
11789
11790void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
11791 size_t len)
11792{
11793 struct wireless_dev *wdev = dev->ieee80211_ptr;
11794 struct wiphy *wiphy = wdev->wiphy;
11795 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11796 const struct ieee80211_mgmt *mgmt = (void *)buf;
11797 u32 cmd;
11798
11799 if (WARN_ON(len < 2))
11800 return;
11801
11802 if (ieee80211_is_deauth(mgmt->frame_control))
11803 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
11804 else
11805 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
11806
11807 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
11808 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
11809}
11810EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
11811
11812static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
11813 struct net_device *netdev, int cmd,
11814 const u8 *addr, gfp_t gfp)
11815{
11816 struct sk_buff *msg;
11817 void *hdr;
11818
11819 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11820 if (!msg)
11821 return;
11822
11823 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
11824 if (!hdr) {
11825 nlmsg_free(msg);
11826 return;
11827 }
11828
11829 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11830 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11831 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
11832 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
11833 goto nla_put_failure;
11834
11835 genlmsg_end(msg, hdr);
11836
11837 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11838 NL80211_MCGRP_MLME, gfp);
11839 return;
11840
11841 nla_put_failure:
11842 genlmsg_cancel(msg, hdr);
11843 nlmsg_free(msg);
11844}
11845
11846void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
11847 struct net_device *netdev, const u8 *addr,
11848 gfp_t gfp)
11849{
11850 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
11851 addr, gfp);
11852}
11853
11854void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
11855 struct net_device *netdev, const u8 *addr,
11856 gfp_t gfp)
11857{
11858 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
11859 addr, gfp);
11860}
11861
11862void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
11863 struct net_device *netdev, const u8 *bssid,
11864 const u8 *req_ie, size_t req_ie_len,
11865 const u8 *resp_ie, size_t resp_ie_len,
11866 u16 status, gfp_t gfp)
11867{
11868 struct sk_buff *msg;
11869 void *hdr;
11870
11871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11872 if (!msg)
11873 return;
11874
11875 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
11876 if (!hdr) {
11877 nlmsg_free(msg);
11878 return;
11879 }
11880
11881 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11882 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11883 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
11884 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
11885 (req_ie &&
11886 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11887 (resp_ie &&
11888 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11889 goto nla_put_failure;
11890
11891 genlmsg_end(msg, hdr);
11892
11893 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11894 NL80211_MCGRP_MLME, gfp);
11895 return;
11896
11897 nla_put_failure:
11898 genlmsg_cancel(msg, hdr);
11899 nlmsg_free(msg);
11900
11901}
11902
11903void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
11904 struct net_device *netdev, const u8 *bssid,
11905 const u8 *req_ie, size_t req_ie_len,
11906 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
11907{
11908 struct sk_buff *msg;
11909 void *hdr;
11910
11911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11912 if (!msg)
11913 return;
11914
11915 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
11916 if (!hdr) {
11917 nlmsg_free(msg);
11918 return;
11919 }
11920
11921 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11922 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11923 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
11924 (req_ie &&
11925 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
11926 (resp_ie &&
11927 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
11928 goto nla_put_failure;
11929
11930 genlmsg_end(msg, hdr);
11931
11932 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11933 NL80211_MCGRP_MLME, gfp);
11934 return;
11935
11936 nla_put_failure:
11937 genlmsg_cancel(msg, hdr);
11938 nlmsg_free(msg);
11939
11940}
11941
11942void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
11943 struct net_device *netdev, u16 reason,
11944 const u8 *ie, size_t ie_len, bool from_ap)
11945{
11946 struct sk_buff *msg;
11947 void *hdr;
11948
11949 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11950 if (!msg)
11951 return;
11952
11953 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
11954 if (!hdr) {
11955 nlmsg_free(msg);
11956 return;
11957 }
11958
11959 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11960 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11961 (from_ap && reason &&
11962 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
11963 (from_ap &&
11964 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
11965 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
11966 goto nla_put_failure;
11967
11968 genlmsg_end(msg, hdr);
11969
11970 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11971 NL80211_MCGRP_MLME, GFP_KERNEL);
11972 return;
11973
11974 nla_put_failure:
11975 genlmsg_cancel(msg, hdr);
11976 nlmsg_free(msg);
11977
11978}
11979
11980void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
11981 struct net_device *netdev, const u8 *bssid,
11982 gfp_t gfp)
11983{
11984 struct sk_buff *msg;
11985 void *hdr;
11986
11987 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11988 if (!msg)
11989 return;
11990
11991 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
11992 if (!hdr) {
11993 nlmsg_free(msg);
11994 return;
11995 }
11996
11997 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11998 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11999 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
12000 goto nla_put_failure;
12001
12002 genlmsg_end(msg, hdr);
12003
12004 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12005 NL80211_MCGRP_MLME, gfp);
12006 return;
12007
12008 nla_put_failure:
12009 genlmsg_cancel(msg, hdr);
12010 nlmsg_free(msg);
12011}
12012
12013void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
12014 const u8* ie, u8 ie_len, gfp_t gfp)
12015{
12016 struct wireless_dev *wdev = dev->ieee80211_ptr;
12017 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12018 struct sk_buff *msg;
12019 void *hdr;
12020
12021 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
12022 return;
12023
12024 trace_cfg80211_notify_new_peer_candidate(dev, addr);
12025
12026 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12027 if (!msg)
12028 return;
12029
12030 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
12031 if (!hdr) {
12032 nlmsg_free(msg);
12033 return;
12034 }
12035
12036 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12037 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12038 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12039 (ie_len && ie &&
12040 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
12041 goto nla_put_failure;
12042
12043 genlmsg_end(msg, hdr);
12044
12045 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12046 NL80211_MCGRP_MLME, gfp);
12047 return;
12048
12049 nla_put_failure:
12050 genlmsg_cancel(msg, hdr);
12051 nlmsg_free(msg);
12052}
12053EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
12054
12055void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
12056 struct net_device *netdev, const u8 *addr,
12057 enum nl80211_key_type key_type, int key_id,
12058 const u8 *tsc, gfp_t gfp)
12059{
12060 struct sk_buff *msg;
12061 void *hdr;
12062
12063 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12064 if (!msg)
12065 return;
12066
12067 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
12068 if (!hdr) {
12069 nlmsg_free(msg);
12070 return;
12071 }
12072
12073 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12074 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12075 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
12076 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
12077 (key_id != -1 &&
12078 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
12079 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
12080 goto nla_put_failure;
12081
12082 genlmsg_end(msg, hdr);
12083
12084 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12085 NL80211_MCGRP_MLME, gfp);
12086 return;
12087
12088 nla_put_failure:
12089 genlmsg_cancel(msg, hdr);
12090 nlmsg_free(msg);
12091}
12092
12093void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
12094 struct ieee80211_channel *channel_before,
12095 struct ieee80211_channel *channel_after)
12096{
12097 struct sk_buff *msg;
12098 void *hdr;
12099 struct nlattr *nl_freq;
12100
12101 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
12102 if (!msg)
12103 return;
12104
12105 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
12106 if (!hdr) {
12107 nlmsg_free(msg);
12108 return;
12109 }
12110
12111 /*
12112 * Since we are applying the beacon hint to a wiphy we know its
12113 * wiphy_idx is valid
12114 */
12115 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
12116 goto nla_put_failure;
12117
12118 /* Before */
12119 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
12120 if (!nl_freq)
12121 goto nla_put_failure;
12122 if (nl80211_msg_put_channel(msg, channel_before, false))
12123 goto nla_put_failure;
12124 nla_nest_end(msg, nl_freq);
12125
12126 /* After */
12127 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
12128 if (!nl_freq)
12129 goto nla_put_failure;
12130 if (nl80211_msg_put_channel(msg, channel_after, false))
12131 goto nla_put_failure;
12132 nla_nest_end(msg, nl_freq);
12133
12134 genlmsg_end(msg, hdr);
12135
12136 rcu_read_lock();
12137 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
12138 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
12139 rcu_read_unlock();
12140
12141 return;
12142
12143nla_put_failure:
12144 genlmsg_cancel(msg, hdr);
12145 nlmsg_free(msg);
12146}
12147
12148static void nl80211_send_remain_on_chan_event(
12149 int cmd, struct cfg80211_registered_device *rdev,
12150 struct wireless_dev *wdev, u64 cookie,
12151 struct ieee80211_channel *chan,
12152 unsigned int duration, gfp_t gfp)
12153{
12154 struct sk_buff *msg;
12155 void *hdr;
12156
12157 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12158 if (!msg)
12159 return;
12160
12161 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
12162 if (!hdr) {
12163 nlmsg_free(msg);
12164 return;
12165 }
12166
12167 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12168 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12169 wdev->netdev->ifindex)) ||
12170 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12171 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
12172 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
12173 NL80211_CHAN_NO_HT) ||
12174 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
12175 goto nla_put_failure;
12176
12177 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
12178 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
12179 goto nla_put_failure;
12180
12181 genlmsg_end(msg, hdr);
12182
12183 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12184 NL80211_MCGRP_MLME, gfp);
12185 return;
12186
12187 nla_put_failure:
12188 genlmsg_cancel(msg, hdr);
12189 nlmsg_free(msg);
12190}
12191
12192void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
12193 struct ieee80211_channel *chan,
12194 unsigned int duration, gfp_t gfp)
12195{
12196 struct wiphy *wiphy = wdev->wiphy;
12197 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12198
12199 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
12200 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
12201 rdev, wdev, cookie, chan,
12202 duration, gfp);
12203}
12204EXPORT_SYMBOL(cfg80211_ready_on_channel);
12205
12206void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
12207 struct ieee80211_channel *chan,
12208 gfp_t gfp)
12209{
12210 struct wiphy *wiphy = wdev->wiphy;
12211 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12212
12213 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
12214 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
12215 rdev, wdev, cookie, chan, 0, gfp);
12216}
12217EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
12218
12219void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
12220 struct station_info *sinfo, gfp_t gfp)
12221{
12222 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12223 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12224 struct sk_buff *msg;
12225
12226 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
12227
12228 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12229 if (!msg)
12230 return;
12231
12232 if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
12233 rdev, dev, mac_addr, sinfo) < 0) {
12234 nlmsg_free(msg);
12235 return;
12236 }
12237
12238 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12239 NL80211_MCGRP_MLME, gfp);
12240}
12241EXPORT_SYMBOL(cfg80211_new_sta);
12242
12243void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
12244 struct station_info *sinfo, gfp_t gfp)
12245{
12246 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12247 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12248 struct sk_buff *msg;
12249 struct station_info empty_sinfo = {};
12250
12251 if (!sinfo)
12252 sinfo = &empty_sinfo;
12253
12254 trace_cfg80211_del_sta(dev, mac_addr);
12255
12256 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12257 if (!msg)
12258 return;
12259
12260 if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
12261 rdev, dev, mac_addr, sinfo) < 0) {
12262 nlmsg_free(msg);
12263 return;
12264 }
12265
12266 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12267 NL80211_MCGRP_MLME, gfp);
12268}
12269EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
12270
12271void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
12272 enum nl80211_connect_failed_reason reason,
12273 gfp_t gfp)
12274{
12275 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
12276 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12277 struct sk_buff *msg;
12278 void *hdr;
12279
12280 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
12281 if (!msg)
12282 return;
12283
12284 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
12285 if (!hdr) {
12286 nlmsg_free(msg);
12287 return;
12288 }
12289
12290 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12291 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
12292 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
12293 goto nla_put_failure;
12294
12295 genlmsg_end(msg, hdr);
12296
12297 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12298 NL80211_MCGRP_MLME, gfp);
12299 return;
12300
12301 nla_put_failure:
12302 genlmsg_cancel(msg, hdr);
12303 nlmsg_free(msg);
12304}
12305EXPORT_SYMBOL(cfg80211_conn_failed);
12306
12307static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
12308 const u8 *addr, gfp_t gfp)
12309{
12310 struct wireless_dev *wdev = dev->ieee80211_ptr;
12311 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12312 struct sk_buff *msg;
12313 void *hdr;
12314 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
12315
12316 if (!nlportid)
12317 return false;
12318
12319 msg = nlmsg_new(100, gfp);
12320 if (!msg)
12321 return true;
12322
12323 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
12324 if (!hdr) {
12325 nlmsg_free(msg);
12326 return true;
12327 }
12328
12329 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12330 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12331 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
12332 goto nla_put_failure;
12333
12334 genlmsg_end(msg, hdr);
12335 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12336 return true;
12337
12338 nla_put_failure:
12339 genlmsg_cancel(msg, hdr);
12340 nlmsg_free(msg);
12341 return true;
12342}
12343
12344bool cfg80211_rx_spurious_frame(struct net_device *dev,
12345 const u8 *addr, gfp_t gfp)
12346{
12347 struct wireless_dev *wdev = dev->ieee80211_ptr;
12348 bool ret;
12349
12350 trace_cfg80211_rx_spurious_frame(dev, addr);
12351
12352 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
12353 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
12354 trace_cfg80211_return_bool(false);
12355 return false;
12356 }
12357 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
12358 addr, gfp);
12359 trace_cfg80211_return_bool(ret);
12360 return ret;
12361}
12362EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
12363
12364bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
12365 const u8 *addr, gfp_t gfp)
12366{
12367 struct wireless_dev *wdev = dev->ieee80211_ptr;
12368 bool ret;
12369
12370 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
12371
12372 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
12373 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
12374 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
12375 trace_cfg80211_return_bool(false);
12376 return false;
12377 }
12378 ret = __nl80211_unexpected_frame(dev,
12379 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
12380 addr, gfp);
12381 trace_cfg80211_return_bool(ret);
12382 return ret;
12383}
12384EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
12385
12386int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
12387 struct wireless_dev *wdev, u32 nlportid,
12388 int freq, int sig_dbm,
12389 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
12390{
12391 struct net_device *netdev = wdev->netdev;
12392 struct sk_buff *msg;
12393 void *hdr;
12394
12395 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12396 if (!msg)
12397 return -ENOMEM;
12398
12399 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12400 if (!hdr) {
12401 nlmsg_free(msg);
12402 return -ENOMEM;
12403 }
12404
12405 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12406 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12407 netdev->ifindex)) ||
12408 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12409 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
12410 (sig_dbm &&
12411 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12412 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
12413 (flags &&
12414 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
12415 goto nla_put_failure;
12416
12417 genlmsg_end(msg, hdr);
12418
12419 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
12420
12421 nla_put_failure:
12422 genlmsg_cancel(msg, hdr);
12423 nlmsg_free(msg);
12424 return -ENOBUFS;
12425}
12426
12427void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
12428 const u8 *buf, size_t len, bool ack, gfp_t gfp)
12429{
12430 struct wiphy *wiphy = wdev->wiphy;
12431 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12432 struct net_device *netdev = wdev->netdev;
12433 struct sk_buff *msg;
12434 void *hdr;
12435
12436 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
12437
12438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12439 if (!msg)
12440 return;
12441
12442 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
12443 if (!hdr) {
12444 nlmsg_free(msg);
12445 return;
12446 }
12447
12448 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12449 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12450 netdev->ifindex)) ||
12451 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
12452 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
12453 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12454 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
12455 goto nla_put_failure;
12456
12457 genlmsg_end(msg, hdr);
12458
12459 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12460 NL80211_MCGRP_MLME, gfp);
12461 return;
12462
12463 nla_put_failure:
12464 genlmsg_cancel(msg, hdr);
12465 nlmsg_free(msg);
12466}
12467EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
12468
12469static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
12470 const char *mac, gfp_t gfp)
12471{
12472 struct wireless_dev *wdev = dev->ieee80211_ptr;
12473 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12474 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12475 void **cb;
12476
12477 if (!msg)
12478 return NULL;
12479
12480 cb = (void **)msg->cb;
12481
12482 cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
12483 if (!cb[0]) {
12484 nlmsg_free(msg);
12485 return NULL;
12486 }
12487
12488 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12489 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
12490 goto nla_put_failure;
12491
12492 if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
12493 goto nla_put_failure;
12494
12495 cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
12496 if (!cb[1])
12497 goto nla_put_failure;
12498
12499 cb[2] = rdev;
12500
12501 return msg;
12502 nla_put_failure:
12503 nlmsg_free(msg);
12504 return NULL;
12505}
12506
12507static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
12508{
12509 void **cb = (void **)msg->cb;
12510 struct cfg80211_registered_device *rdev = cb[2];
12511
12512 nla_nest_end(msg, cb[1]);
12513 genlmsg_end(msg, cb[0]);
12514
12515 memset(msg->cb, 0, sizeof(msg->cb));
12516
12517 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12518 NL80211_MCGRP_MLME, gfp);
12519}
12520
12521void cfg80211_cqm_rssi_notify(struct net_device *dev,
12522 enum nl80211_cqm_rssi_threshold_event rssi_event,
12523 gfp_t gfp)
12524{
12525 struct sk_buff *msg;
12526
12527 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
12528
12529 if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
12530 rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
12531 return;
12532
12533 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
12534 if (!msg)
12535 return;
12536
12537 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
12538 rssi_event))
12539 goto nla_put_failure;
12540
12541 cfg80211_send_cqm(msg, gfp);
12542
12543 return;
12544
12545 nla_put_failure:
12546 nlmsg_free(msg);
12547}
12548EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
12549
12550void cfg80211_cqm_txe_notify(struct net_device *dev,
12551 const u8 *peer, u32 num_packets,
12552 u32 rate, u32 intvl, gfp_t gfp)
12553{
12554 struct sk_buff *msg;
12555
12556 msg = cfg80211_prepare_cqm(dev, peer, gfp);
12557 if (!msg)
12558 return;
12559
12560 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
12561 goto nla_put_failure;
12562
12563 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
12564 goto nla_put_failure;
12565
12566 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
12567 goto nla_put_failure;
12568
12569 cfg80211_send_cqm(msg, gfp);
12570 return;
12571
12572 nla_put_failure:
12573 nlmsg_free(msg);
12574}
12575EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
12576
12577void cfg80211_cqm_pktloss_notify(struct net_device *dev,
12578 const u8 *peer, u32 num_packets, gfp_t gfp)
12579{
12580 struct sk_buff *msg;
12581
12582 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
12583
12584 msg = cfg80211_prepare_cqm(dev, peer, gfp);
12585 if (!msg)
12586 return;
12587
12588 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
12589 goto nla_put_failure;
12590
12591 cfg80211_send_cqm(msg, gfp);
12592 return;
12593
12594 nla_put_failure:
12595 nlmsg_free(msg);
12596}
12597EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
12598
12599void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
12600{
12601 struct sk_buff *msg;
12602
12603 msg = cfg80211_prepare_cqm(dev, NULL, gfp);
12604 if (!msg)
12605 return;
12606
12607 if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
12608 goto nla_put_failure;
12609
12610 cfg80211_send_cqm(msg, gfp);
12611 return;
12612
12613 nla_put_failure:
12614 nlmsg_free(msg);
12615}
12616EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
12617
12618static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
12619 struct net_device *netdev, const u8 *bssid,
12620 const u8 *replay_ctr, gfp_t gfp)
12621{
12622 struct sk_buff *msg;
12623 struct nlattr *rekey_attr;
12624 void *hdr;
12625
12626 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12627 if (!msg)
12628 return;
12629
12630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
12631 if (!hdr) {
12632 nlmsg_free(msg);
12633 return;
12634 }
12635
12636 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12637 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12638 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
12639 goto nla_put_failure;
12640
12641 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
12642 if (!rekey_attr)
12643 goto nla_put_failure;
12644
12645 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
12646 NL80211_REPLAY_CTR_LEN, replay_ctr))
12647 goto nla_put_failure;
12648
12649 nla_nest_end(msg, rekey_attr);
12650
12651 genlmsg_end(msg, hdr);
12652
12653 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12654 NL80211_MCGRP_MLME, gfp);
12655 return;
12656
12657 nla_put_failure:
12658 genlmsg_cancel(msg, hdr);
12659 nlmsg_free(msg);
12660}
12661
12662void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
12663 const u8 *replay_ctr, gfp_t gfp)
12664{
12665 struct wireless_dev *wdev = dev->ieee80211_ptr;
12666 struct wiphy *wiphy = wdev->wiphy;
12667 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12668
12669 trace_cfg80211_gtk_rekey_notify(dev, bssid);
12670 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
12671}
12672EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
12673
12674static void
12675nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
12676 struct net_device *netdev, int index,
12677 const u8 *bssid, bool preauth, gfp_t gfp)
12678{
12679 struct sk_buff *msg;
12680 struct nlattr *attr;
12681 void *hdr;
12682
12683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12684 if (!msg)
12685 return;
12686
12687 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
12688 if (!hdr) {
12689 nlmsg_free(msg);
12690 return;
12691 }
12692
12693 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12694 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
12695 goto nla_put_failure;
12696
12697 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
12698 if (!attr)
12699 goto nla_put_failure;
12700
12701 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
12702 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
12703 (preauth &&
12704 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
12705 goto nla_put_failure;
12706
12707 nla_nest_end(msg, attr);
12708
12709 genlmsg_end(msg, hdr);
12710
12711 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12712 NL80211_MCGRP_MLME, gfp);
12713 return;
12714
12715 nla_put_failure:
12716 genlmsg_cancel(msg, hdr);
12717 nlmsg_free(msg);
12718}
12719
12720void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
12721 const u8 *bssid, bool preauth, gfp_t gfp)
12722{
12723 struct wireless_dev *wdev = dev->ieee80211_ptr;
12724 struct wiphy *wiphy = wdev->wiphy;
12725 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12726
12727 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
12728 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
12729}
12730EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
12731
12732static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
12733 struct net_device *netdev,
12734 struct cfg80211_chan_def *chandef,
12735 gfp_t gfp,
12736 enum nl80211_commands notif,
12737 u8 count)
12738{
12739 struct sk_buff *msg;
12740 void *hdr;
12741
12742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12743 if (!msg)
12744 return;
12745
12746 hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
12747 if (!hdr) {
12748 nlmsg_free(msg);
12749 return;
12750 }
12751
12752 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
12753 goto nla_put_failure;
12754
12755 if (nl80211_send_chandef(msg, chandef))
12756 goto nla_put_failure;
12757
12758 if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
12759 (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
12760 goto nla_put_failure;
12761
12762 genlmsg_end(msg, hdr);
12763
12764 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12765 NL80211_MCGRP_MLME, gfp);
12766 return;
12767
12768 nla_put_failure:
12769 genlmsg_cancel(msg, hdr);
12770 nlmsg_free(msg);
12771}
12772
12773void cfg80211_ch_switch_notify(struct net_device *dev,
12774 struct cfg80211_chan_def *chandef)
12775{
12776 struct wireless_dev *wdev = dev->ieee80211_ptr;
12777 struct wiphy *wiphy = wdev->wiphy;
12778 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12779
12780 ASSERT_WDEV_LOCK(wdev);
12781
12782 trace_cfg80211_ch_switch_notify(dev, chandef);
12783
12784 wdev->chandef = *chandef;
12785 wdev->preset_chandef = *chandef;
12786 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12787 NL80211_CMD_CH_SWITCH_NOTIFY, 0);
12788}
12789EXPORT_SYMBOL(cfg80211_ch_switch_notify);
12790
12791void cfg80211_ch_switch_started_notify(struct net_device *dev,
12792 struct cfg80211_chan_def *chandef,
12793 u8 count)
12794{
12795 struct wireless_dev *wdev = dev->ieee80211_ptr;
12796 struct wiphy *wiphy = wdev->wiphy;
12797 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12798
12799 trace_cfg80211_ch_switch_started_notify(dev, chandef);
12800
12801 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
12802 NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
12803}
12804EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
12805
12806void
12807nl80211_radar_notify(struct cfg80211_registered_device *rdev,
12808 const struct cfg80211_chan_def *chandef,
12809 enum nl80211_radar_event event,
12810 struct net_device *netdev, gfp_t gfp)
12811{
12812 struct sk_buff *msg;
12813 void *hdr;
12814
12815 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12816 if (!msg)
12817 return;
12818
12819 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
12820 if (!hdr) {
12821 nlmsg_free(msg);
12822 return;
12823 }
12824
12825 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
12826 goto nla_put_failure;
12827
12828 /* NOP and radar events don't need a netdev parameter */
12829 if (netdev) {
12830 struct wireless_dev *wdev = netdev->ieee80211_ptr;
12831
12832 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
12833 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
12834 goto nla_put_failure;
12835 }
12836
12837 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
12838 goto nla_put_failure;
12839
12840 if (nl80211_send_chandef(msg, chandef))
12841 goto nla_put_failure;
12842
12843 genlmsg_end(msg, hdr);
12844
12845 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12846 NL80211_MCGRP_MLME, gfp);
12847 return;
12848
12849 nla_put_failure:
12850 genlmsg_cancel(msg, hdr);
12851 nlmsg_free(msg);
12852}
12853
12854void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
12855 u64 cookie, bool acked, gfp_t gfp)
12856{
12857 struct wireless_dev *wdev = dev->ieee80211_ptr;
12858 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
12859 struct sk_buff *msg;
12860 void *hdr;
12861
12862 trace_cfg80211_probe_status(dev, addr, cookie, acked);
12863
12864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12865
12866 if (!msg)
12867 return;
12868
12869 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
12870 if (!hdr) {
12871 nlmsg_free(msg);
12872 return;
12873 }
12874
12875 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12876 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
12877 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
12878 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
12879 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
12880 goto nla_put_failure;
12881
12882 genlmsg_end(msg, hdr);
12883
12884 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
12885 NL80211_MCGRP_MLME, gfp);
12886 return;
12887
12888 nla_put_failure:
12889 genlmsg_cancel(msg, hdr);
12890 nlmsg_free(msg);
12891}
12892EXPORT_SYMBOL(cfg80211_probe_status);
12893
12894void cfg80211_report_obss_beacon(struct wiphy *wiphy,
12895 const u8 *frame, size_t len,
12896 int freq, int sig_dbm)
12897{
12898 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12899 struct sk_buff *msg;
12900 void *hdr;
12901 struct cfg80211_beacon_registration *reg;
12902
12903 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
12904
12905 spin_lock_bh(&rdev->beacon_registrations_lock);
12906 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12907 msg = nlmsg_new(len + 100, GFP_ATOMIC);
12908 if (!msg) {
12909 spin_unlock_bh(&rdev->beacon_registrations_lock);
12910 return;
12911 }
12912
12913 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
12914 if (!hdr)
12915 goto nla_put_failure;
12916
12917 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12918 (freq &&
12919 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
12920 (sig_dbm &&
12921 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
12922 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
12923 goto nla_put_failure;
12924
12925 genlmsg_end(msg, hdr);
12926
12927 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
12928 }
12929 spin_unlock_bh(&rdev->beacon_registrations_lock);
12930 return;
12931
12932 nla_put_failure:
12933 spin_unlock_bh(&rdev->beacon_registrations_lock);
12934 if (hdr)
12935 genlmsg_cancel(msg, hdr);
12936 nlmsg_free(msg);
12937}
12938EXPORT_SYMBOL(cfg80211_report_obss_beacon);
12939
12940#ifdef CONFIG_PM
12941static int cfg80211_net_detect_results(struct sk_buff *msg,
12942 struct cfg80211_wowlan_wakeup *wakeup)
12943{
12944 struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
12945 struct nlattr *nl_results, *nl_match, *nl_freqs;
12946 int i, j;
12947
12948 nl_results = nla_nest_start(
12949 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
12950 if (!nl_results)
12951 return -EMSGSIZE;
12952
12953 for (i = 0; i < nd->n_matches; i++) {
12954 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
12955
12956 nl_match = nla_nest_start(msg, i);
12957 if (!nl_match)
12958 break;
12959
12960 /* The SSID attribute is optional in nl80211, but for
12961 * simplicity reasons it's always present in the
12962 * cfg80211 structure. If a driver can't pass the
12963 * SSID, that needs to be changed. A zero length SSID
12964 * is still a valid SSID (wildcard), so it cannot be
12965 * used for this purpose.
12966 */
12967 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
12968 match->ssid.ssid)) {
12969 nla_nest_cancel(msg, nl_match);
12970 goto out;
12971 }
12972
12973 if (match->n_channels) {
12974 nl_freqs = nla_nest_start(
12975 msg, NL80211_ATTR_SCAN_FREQUENCIES);
12976 if (!nl_freqs) {
12977 nla_nest_cancel(msg, nl_match);
12978 goto out;
12979 }
12980
12981 for (j = 0; j < match->n_channels; j++) {
12982 if (nla_put_u32(msg, j, match->channels[j])) {
12983 nla_nest_cancel(msg, nl_freqs);
12984 nla_nest_cancel(msg, nl_match);
12985 goto out;
12986 }
12987 }
12988
12989 nla_nest_end(msg, nl_freqs);
12990 }
12991
12992 nla_nest_end(msg, nl_match);
12993 }
12994
12995out:
12996 nla_nest_end(msg, nl_results);
12997 return 0;
12998}
12999
13000void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
13001 struct cfg80211_wowlan_wakeup *wakeup,
13002 gfp_t gfp)
13003{
13004 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
13005 struct sk_buff *msg;
13006 void *hdr;
13007 int size = 200;
13008
13009 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
13010
13011 if (wakeup)
13012 size += wakeup->packet_present_len;
13013
13014 msg = nlmsg_new(size, gfp);
13015 if (!msg)
13016 return;
13017
13018 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
13019 if (!hdr)
13020 goto free_msg;
13021
13022 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13023 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13024 goto free_msg;
13025
13026 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13027 wdev->netdev->ifindex))
13028 goto free_msg;
13029
13030 if (wakeup) {
13031 struct nlattr *reasons;
13032
13033 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
13034 if (!reasons)
13035 goto free_msg;
13036
13037 if (wakeup->disconnect &&
13038 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
13039 goto free_msg;
13040 if (wakeup->magic_pkt &&
13041 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
13042 goto free_msg;
13043 if (wakeup->gtk_rekey_failure &&
13044 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
13045 goto free_msg;
13046 if (wakeup->eap_identity_req &&
13047 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
13048 goto free_msg;
13049 if (wakeup->four_way_handshake &&
13050 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
13051 goto free_msg;
13052 if (wakeup->rfkill_release &&
13053 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
13054 goto free_msg;
13055
13056 if (wakeup->pattern_idx >= 0 &&
13057 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
13058 wakeup->pattern_idx))
13059 goto free_msg;
13060
13061 if (wakeup->tcp_match &&
13062 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
13063 goto free_msg;
13064
13065 if (wakeup->tcp_connlost &&
13066 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
13067 goto free_msg;
13068
13069 if (wakeup->tcp_nomoretokens &&
13070 nla_put_flag(msg,
13071 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
13072 goto free_msg;
13073
13074 if (wakeup->packet) {
13075 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
13076 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
13077
13078 if (!wakeup->packet_80211) {
13079 pkt_attr =
13080 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
13081 len_attr =
13082 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
13083 }
13084
13085 if (wakeup->packet_len &&
13086 nla_put_u32(msg, len_attr, wakeup->packet_len))
13087 goto free_msg;
13088
13089 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
13090 wakeup->packet))
13091 goto free_msg;
13092 }
13093
13094 if (wakeup->net_detect &&
13095 cfg80211_net_detect_results(msg, wakeup))
13096 goto free_msg;
13097
13098 nla_nest_end(msg, reasons);
13099 }
13100
13101 genlmsg_end(msg, hdr);
13102
13103 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13104 NL80211_MCGRP_MLME, gfp);
13105 return;
13106
13107 free_msg:
13108 nlmsg_free(msg);
13109}
13110EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
13111#endif
13112
13113void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
13114 enum nl80211_tdls_operation oper,
13115 u16 reason_code, gfp_t gfp)
13116{
13117 struct wireless_dev *wdev = dev->ieee80211_ptr;
13118 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
13119 struct sk_buff *msg;
13120 void *hdr;
13121
13122 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
13123 reason_code);
13124
13125 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13126 if (!msg)
13127 return;
13128
13129 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
13130 if (!hdr) {
13131 nlmsg_free(msg);
13132 return;
13133 }
13134
13135 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13136 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
13137 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
13138 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
13139 (reason_code > 0 &&
13140 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
13141 goto nla_put_failure;
13142
13143 genlmsg_end(msg, hdr);
13144
13145 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13146 NL80211_MCGRP_MLME, gfp);
13147 return;
13148
13149 nla_put_failure:
13150 genlmsg_cancel(msg, hdr);
13151 nlmsg_free(msg);
13152}
13153EXPORT_SYMBOL(cfg80211_tdls_oper_request);
13154
13155static int nl80211_netlink_notify(struct notifier_block * nb,
13156 unsigned long state,
13157 void *_notify)
13158{
13159 struct netlink_notify *notify = _notify;
13160 struct cfg80211_registered_device *rdev;
13161 struct wireless_dev *wdev;
13162 struct cfg80211_beacon_registration *reg, *tmp;
13163
13164 if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
13165 return NOTIFY_DONE;
13166
13167 rcu_read_lock();
13168
13169 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
13170 bool schedule_destroy_work = false;
13171 struct cfg80211_sched_scan_request *sched_scan_req =
13172 rcu_dereference(rdev->sched_scan_req);
13173
13174 if (sched_scan_req && notify->portid &&
13175 sched_scan_req->owner_nlportid == notify->portid) {
13176 sched_scan_req->owner_nlportid = 0;
13177
13178 if (rdev->ops->sched_scan_stop &&
13179 rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
13180 schedule_work(&rdev->sched_scan_stop_wk);
13181 }
13182
13183 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list) {
13184 cfg80211_mlme_unregister_socket(wdev, notify->portid);
13185
13186 if (wdev->owner_nlportid == notify->portid)
13187 schedule_destroy_work = true;
13188 }
13189
13190 spin_lock_bh(&rdev->beacon_registrations_lock);
13191 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
13192 list) {
13193 if (reg->nlportid == notify->portid) {
13194 list_del(&reg->list);
13195 kfree(reg);
13196 break;
13197 }
13198 }
13199 spin_unlock_bh(&rdev->beacon_registrations_lock);
13200
13201 if (schedule_destroy_work) {
13202 struct cfg80211_iface_destroy *destroy;
13203
13204 destroy = kzalloc(sizeof(*destroy), GFP_ATOMIC);
13205 if (destroy) {
13206 destroy->nlportid = notify->portid;
13207 spin_lock(&rdev->destroy_list_lock);
13208 list_add(&destroy->list, &rdev->destroy_list);
13209 spin_unlock(&rdev->destroy_list_lock);
13210 schedule_work(&rdev->destroy_work);
13211 }
13212 }
13213 }
13214
13215 rcu_read_unlock();
13216
13217 /*
13218 * It is possible that the user space process that is controlling the
13219 * indoor setting disappeared, so notify the regulatory core.
13220 */
13221 regulatory_netlink_notify(notify->portid);
13222 return NOTIFY_OK;
13223}
13224
13225static struct notifier_block nl80211_netlink_notifier = {
13226 .notifier_call = nl80211_netlink_notify,
13227};
13228
13229void cfg80211_ft_event(struct net_device *netdev,
13230 struct cfg80211_ft_event_params *ft_event)
13231{
13232 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
13233 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13234 struct sk_buff *msg;
13235 void *hdr;
13236
13237 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
13238
13239 if (!ft_event->target_ap)
13240 return;
13241
13242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13243 if (!msg)
13244 return;
13245
13246 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
13247 if (!hdr)
13248 goto out;
13249
13250 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13251 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13252 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
13253 goto out;
13254
13255 if (ft_event->ies &&
13256 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
13257 goto out;
13258 if (ft_event->ric_ies &&
13259 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
13260 ft_event->ric_ies))
13261 goto out;
13262
13263 genlmsg_end(msg, hdr);
13264
13265 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13266 NL80211_MCGRP_MLME, GFP_KERNEL);
13267 return;
13268 out:
13269 nlmsg_free(msg);
13270}
13271EXPORT_SYMBOL(cfg80211_ft_event);
13272
13273void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
13274{
13275 struct cfg80211_registered_device *rdev;
13276 struct sk_buff *msg;
13277 void *hdr;
13278 u32 nlportid;
13279
13280 rdev = wiphy_to_rdev(wdev->wiphy);
13281 if (!rdev->crit_proto_nlportid)
13282 return;
13283
13284 nlportid = rdev->crit_proto_nlportid;
13285 rdev->crit_proto_nlportid = 0;
13286
13287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13288 if (!msg)
13289 return;
13290
13291 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
13292 if (!hdr)
13293 goto nla_put_failure;
13294
13295 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13296 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13297 goto nla_put_failure;
13298
13299 genlmsg_end(msg, hdr);
13300
13301 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
13302 return;
13303
13304 nla_put_failure:
13305 if (hdr)
13306 genlmsg_cancel(msg, hdr);
13307 nlmsg_free(msg);
13308
13309}
13310EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
13311
13312void nl80211_send_ap_stopped(struct wireless_dev *wdev)
13313{
13314 struct wiphy *wiphy = wdev->wiphy;
13315 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13316 struct sk_buff *msg;
13317 void *hdr;
13318
13319 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13320 if (!msg)
13321 return;
13322
13323 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
13324 if (!hdr)
13325 goto out;
13326
13327 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13328 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
13329 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
13330 goto out;
13331
13332 genlmsg_end(msg, hdr);
13333
13334 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
13335 NL80211_MCGRP_MLME, GFP_KERNEL);
13336 return;
13337 out:
13338 nlmsg_free(msg);
13339}
13340
13341/* initialisation/exit functions */
13342
13343int nl80211_init(void)
13344{
13345 int err;
13346
13347 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
13348 nl80211_mcgrps);
13349 if (err)
13350 return err;
13351
13352 err = netlink_register_notifier(&nl80211_netlink_notifier);
13353 if (err)
13354 goto err_out;
13355
13356 return 0;
13357 err_out:
13358 genl_unregister_family(&nl80211_fam);
13359 return err;
13360}
13361
13362void nl80211_exit(void)
13363{
13364 netlink_unregister_notifier(&nl80211_netlink_notifier);
13365 genl_unregister_family(&nl80211_fam);
13366}