cfg80211: add support for per-chain signal strength reporting
[linux-2.6-block.git] / net / wireless / nl80211.c
CommitLineData
55682965
JB
1/*
2 * This is the new netlink-based wireless configuration interface.
3 *
026331c4 4 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
55682965
JB
5 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
5a0e3ad6 10#include <linux/slab.h>
55682965
JB
11#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
2a519311 17#include <linux/etherdevice.h>
463d0183 18#include <net/net_namespace.h>
55682965
JB
19#include <net/genetlink.h>
20#include <net/cfg80211.h>
463d0183 21#include <net/sock.h>
2a0e047e 22#include <net/inet_connection_sock.h>
55682965
JB
23#include "core.h"
24#include "nl80211.h"
b2e1b302 25#include "reg.h"
e35e4d28 26#include "rdev-ops.h"
55682965 27
5fb628e9
JM
28static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
4c476991
JB
33static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
55682965
JB
38/* the netlink family */
39static struct genl_family nl80211_fam = {
40 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = "nl80211", /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
44 .maxattr = NL80211_ATTR_MAX,
463d0183 45 .netnsok = true,
4c476991
JB
46 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
55682965
JB
48};
49
89a54e48
JB
50/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
55682965 53{
89a54e48
JB
54 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
55682965 61
89a54e48 62 assert_cfg80211_lock();
55682965 63
89a54e48
JB
64 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
55682965 66
89a54e48
JB
67 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
55682965
JB
72 }
73
89a54e48
JB
74 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
83 mutex_lock(&rdev->devlist_mtx);
84 list_for_each_entry(wdev, &rdev->wdev_list, list) {
85 if (have_ifidx && wdev->netdev &&
86 wdev->netdev->ifindex == ifidx) {
87 result = wdev;
88 break;
89 }
90 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
91 result = wdev;
92 break;
93 }
94 }
95 mutex_unlock(&rdev->devlist_mtx);
96
97 if (result)
98 break;
99 }
100
101 if (result)
102 return result;
103 return ERR_PTR(-ENODEV);
55682965
JB
104}
105
a9455408 106static struct cfg80211_registered_device *
878d9ec7 107__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
a9455408 108{
7fee4778
JB
109 struct cfg80211_registered_device *rdev = NULL, *tmp;
110 struct net_device *netdev;
a9455408
JB
111
112 assert_cfg80211_lock();
113
878d9ec7 114 if (!attrs[NL80211_ATTR_WIPHY] &&
89a54e48
JB
115 !attrs[NL80211_ATTR_IFINDEX] &&
116 !attrs[NL80211_ATTR_WDEV])
7fee4778
JB
117 return ERR_PTR(-EINVAL);
118
878d9ec7 119 if (attrs[NL80211_ATTR_WIPHY])
7fee4778 120 rdev = cfg80211_rdev_by_wiphy_idx(
878d9ec7 121 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
a9455408 122
89a54e48
JB
123 if (attrs[NL80211_ATTR_WDEV]) {
124 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
125 struct wireless_dev *wdev;
126 bool found = false;
127
128 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
129 if (tmp) {
130 /* make sure wdev exists */
131 mutex_lock(&tmp->devlist_mtx);
132 list_for_each_entry(wdev, &tmp->wdev_list, list) {
133 if (wdev->identifier != (u32)wdev_id)
134 continue;
135 found = true;
136 break;
137 }
138 mutex_unlock(&tmp->devlist_mtx);
139
140 if (!found)
141 tmp = NULL;
142
143 if (rdev && tmp != rdev)
144 return ERR_PTR(-EINVAL);
145 rdev = tmp;
146 }
147 }
148
878d9ec7
JB
149 if (attrs[NL80211_ATTR_IFINDEX]) {
150 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
4f7eff10 151 netdev = dev_get_by_index(netns, ifindex);
7fee4778
JB
152 if (netdev) {
153 if (netdev->ieee80211_ptr)
154 tmp = wiphy_to_dev(
155 netdev->ieee80211_ptr->wiphy);
156 else
157 tmp = NULL;
158
159 dev_put(netdev);
160
161 /* not wireless device -- return error */
162 if (!tmp)
163 return ERR_PTR(-EINVAL);
164
165 /* mismatch -- return error */
166 if (rdev && tmp != rdev)
167 return ERR_PTR(-EINVAL);
168
169 rdev = tmp;
a9455408 170 }
a9455408 171 }
a9455408 172
4f7eff10
JB
173 if (!rdev)
174 return ERR_PTR(-ENODEV);
a9455408 175
4f7eff10
JB
176 if (netns != wiphy_net(&rdev->wiphy))
177 return ERR_PTR(-ENODEV);
178
179 return rdev;
a9455408
JB
180}
181
182/*
183 * This function returns a pointer to the driver
184 * that the genl_info item that is passed refers to.
185 * If successful, it returns non-NULL and also locks
186 * the driver's mutex!
187 *
188 * This means that you need to call cfg80211_unlock_rdev()
189 * before being allowed to acquire &cfg80211_mutex!
190 *
191 * This is necessary because we need to lock the global
192 * mutex to get an item off the list safely, and then
193 * we lock the rdev mutex so it doesn't go away under us.
194 *
195 * We don't want to keep cfg80211_mutex locked
196 * for all the time in order to allow requests on
197 * other interfaces to go through at the same time.
198 *
199 * The result of this can be a PTR_ERR and hence must
200 * be checked with IS_ERR() for errors.
201 */
202static struct cfg80211_registered_device *
4f7eff10 203cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
a9455408
JB
204{
205 struct cfg80211_registered_device *rdev;
206
207 mutex_lock(&cfg80211_mutex);
878d9ec7 208 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
a9455408
JB
209
210 /* if it is not an error we grab the lock on
211 * it to assure it won't be going away while
212 * we operate on it */
213 if (!IS_ERR(rdev))
214 mutex_lock(&rdev->mtx);
215
216 mutex_unlock(&cfg80211_mutex);
217
218 return rdev;
219}
220
55682965 221/* policy for the attributes */
b54452b0 222static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
55682965
JB
223 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
224 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
079e24ed 225 .len = 20-1 },
31888487 226 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
3d9d1d66 227
72bdcf34 228 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
094d05dc 229 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
3d9d1d66
JB
230 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
231 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
232 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
233
b9a5f8ca
JM
234 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
235 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
236 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
237 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
81077e82 238 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
55682965
JB
239
240 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
241 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
242 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
41ade00f 243
e007b857
EP
244 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
245 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
41ade00f 246
b9454e83 247 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
41ade00f
JB
248 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
249 .len = WLAN_MAX_KEY_LEN },
250 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
251 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
252 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
81962267 253 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
e31b8213 254 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
ed1b6cc7
JB
255
256 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
257 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
258 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
259 .len = IEEE80211_MAX_DATA_LEN },
260 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
5727ef1b
JB
262 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
263 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
264 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
265 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
266 .len = NL80211_MAX_SUPP_RATES },
2ec600d6 267 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
5727ef1b 268 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
0a9542ee 269 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
2ec600d6 270 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
a4f606ea 271 .len = IEEE80211_MAX_MESH_ID_LEN },
2ec600d6 272 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
9f1ba906 273
b2e1b302
LR
274 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
275 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
276
9f1ba906
JM
277 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
278 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
279 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
90c97a04
JM
280 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
281 .len = NL80211_MAX_SUPP_RATES },
50b12f59 282 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
36aedc90 283
24bdd9f4 284 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
15d5dda6 285 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
93da9cc1 286
6c739419 287 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
9aed3cc1
JM
288
289 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
290 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
291 .len = IEEE80211_MAX_DATA_LEN },
2a519311
JB
292 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
293 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
636a5d36
JM
294
295 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
296 .len = IEEE80211_MAX_SSID_LEN },
297 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
298 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
04a773ad 299 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
1965c853 300 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
dc6382ce 301 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
eccb8e8f
JB
302 [NL80211_ATTR_STA_FLAGS2] = {
303 .len = sizeof(struct nl80211_sta_flag_update),
304 },
3f77316c 305 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
c0692b8f
JB
306 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
307 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
b23aa676
SO
308 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
309 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
310 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
463d0183 311 [NL80211_ATTR_PID] = { .type = NLA_U32 },
8b787643 312 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
67fbb16b
SO
313 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
314 .len = WLAN_PMKID_LEN },
9588bbd5
JM
315 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
316 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
13ae75b1 317 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
026331c4
JM
318 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
319 .len = IEEE80211_MAX_DATA_LEN },
320 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
ffb9eb3d 321 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
d6dc1a38 322 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
d5cdfacb 323 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
fd8aaaf3 324 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
98d2ff8b
JO
325 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
326 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
2e161f78 327 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
afe0cbf8
BR
328 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
329 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
885a46d0 330 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
f7ca38df 331 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
dbd2fd65 332 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
ff1b6e69 333 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
9c3990aa 334 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
bbe6ad6d 335 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
e5497d76 336 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
34850ab2 337 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
32e9de84 338 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
9946ecfb
JM
339 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
340 .len = IEEE80211_MAX_DATA_LEN },
341 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
342 .len = IEEE80211_MAX_DATA_LEN },
f4b34b55 343 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
a1f1c21c 344 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
e9f935e3 345 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
109086ce
AN
346 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
347 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
348 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
349 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
e247bd90 351 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
00f740e1
AN
352 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
353 .len = IEEE80211_MAX_DATA_LEN },
8b60b078 354 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
7e7c8926
BG
355 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
356 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
357 .len = NL80211_HT_CAPABILITY_LEN
358 },
1d9d9213 359 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
1b658f11 360 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
4486ea98 361 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
89a54e48 362 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
57b5ce07 363 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
e39e5b5e 364 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
f461be3e 365 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
ed473771 366 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
53cabad7
JB
367 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
368 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
77765eaf
VT
369 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
370 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
9d62a986
JM
371 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
372 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
3713b4e3 373 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
ee2aca34
JB
374 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
375 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
376 .len = NL80211_VHT_CAPABILITY_LEN,
377 },
355199e0
JM
378 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
379 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
380 .len = IEEE80211_MAX_DATA_LEN },
55682965
JB
381};
382
e31b8213 383/* policy for the key attributes */
b54452b0 384static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
fffd0934 385 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
b9454e83
JB
386 [NL80211_KEY_IDX] = { .type = NLA_U8 },
387 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
81962267 388 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
b9454e83
JB
389 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
390 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
e31b8213 391 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
dbd2fd65
JB
392 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
393};
394
395/* policy for the key default flags */
396static const struct nla_policy
397nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
398 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
399 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
b9454e83
JB
400};
401
ff1b6e69
JB
402/* policy for WoWLAN attributes */
403static const struct nla_policy
404nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
405 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
406 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
407 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
408 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
77dbbb13
JB
409 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
410 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
411 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
412 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
2a0e047e
JB
413 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
414};
415
416static const struct nla_policy
417nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
418 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
419 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
420 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
421 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
422 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
423 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
424 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
425 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
426 },
427 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
428 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
429 },
430 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
431 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
432 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
ff1b6e69
JB
433};
434
e5497d76
JB
435/* policy for GTK rekey offload attributes */
436static const struct nla_policy
437nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
438 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
439 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
440 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
441};
442
a1f1c21c
LC
443static const struct nla_policy
444nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
4a4ab0d7 445 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
a1f1c21c 446 .len = IEEE80211_MAX_SSID_LEN },
88e920b4 447 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
a1f1c21c
LC
448};
449
97990a06
JB
450static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
451 struct netlink_callback *cb,
452 struct cfg80211_registered_device **rdev,
453 struct wireless_dev **wdev)
a043897a 454{
97990a06 455 int err;
a043897a 456
97990a06
JB
457 rtnl_lock();
458 mutex_lock(&cfg80211_mutex);
a043897a 459
97990a06
JB
460 if (!cb->args[0]) {
461 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
462 nl80211_fam.attrbuf, nl80211_fam.maxattr,
463 nl80211_policy);
464 if (err)
465 goto out_unlock;
67748893 466
97990a06
JB
467 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
468 nl80211_fam.attrbuf);
469 if (IS_ERR(*wdev)) {
470 err = PTR_ERR(*wdev);
471 goto out_unlock;
472 }
473 *rdev = wiphy_to_dev((*wdev)->wiphy);
474 cb->args[0] = (*rdev)->wiphy_idx;
475 cb->args[1] = (*wdev)->identifier;
476 } else {
477 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
478 struct wireless_dev *tmp;
67748893 479
97990a06
JB
480 if (!wiphy) {
481 err = -ENODEV;
482 goto out_unlock;
483 }
484 *rdev = wiphy_to_dev(wiphy);
485 *wdev = NULL;
67748893 486
97990a06
JB
487 mutex_lock(&(*rdev)->devlist_mtx);
488 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
489 if (tmp->identifier == cb->args[1]) {
490 *wdev = tmp;
491 break;
492 }
493 }
494 mutex_unlock(&(*rdev)->devlist_mtx);
67748893 495
97990a06
JB
496 if (!*wdev) {
497 err = -ENODEV;
498 goto out_unlock;
499 }
67748893
JB
500 }
501
97990a06 502 cfg80211_lock_rdev(*rdev);
67748893 503
97990a06 504 mutex_unlock(&cfg80211_mutex);
67748893 505 return 0;
97990a06
JB
506 out_unlock:
507 mutex_unlock(&cfg80211_mutex);
67748893
JB
508 rtnl_unlock();
509 return err;
510}
511
97990a06 512static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
67748893
JB
513{
514 cfg80211_unlock_rdev(rdev);
515 rtnl_unlock();
516}
517
f4a11bb0
JB
518/* IE validation */
519static bool is_valid_ie_attr(const struct nlattr *attr)
520{
521 const u8 *pos;
522 int len;
523
524 if (!attr)
525 return true;
526
527 pos = nla_data(attr);
528 len = nla_len(attr);
529
530 while (len) {
531 u8 elemlen;
532
533 if (len < 2)
534 return false;
535 len -= 2;
536
537 elemlen = pos[1];
538 if (elemlen > len)
539 return false;
540
541 len -= elemlen;
542 pos += 2 + elemlen;
543 }
544
545 return true;
546}
547
55682965 548/* message building helper */
15e47304 549static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
55682965
JB
550 int flags, u8 cmd)
551{
552 /* since there is no private header just add the generic one */
15e47304 553 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
55682965
JB
554}
555
5dab3b8a 556static int nl80211_msg_put_channel(struct sk_buff *msg,
cdc89b97
JB
557 struct ieee80211_channel *chan,
558 bool large)
5dab3b8a 559{
9360ffd1
DM
560 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
561 chan->center_freq))
562 goto nla_put_failure;
5dab3b8a 563
9360ffd1
DM
564 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
565 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
566 goto nla_put_failure;
567 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
568 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
569 goto nla_put_failure;
570 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
571 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
572 goto nla_put_failure;
cdc89b97
JB
573 if (chan->flags & IEEE80211_CHAN_RADAR) {
574 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
575 goto nla_put_failure;
576 if (large) {
577 u32 time;
578
579 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
580
581 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
582 chan->dfs_state))
583 goto nla_put_failure;
584 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
585 time))
586 goto nla_put_failure;
587 }
588 }
5dab3b8a 589
fe1abafd
JB
590 if (large) {
591 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
592 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
593 goto nla_put_failure;
594 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
595 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
596 goto nla_put_failure;
597 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
598 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
599 goto nla_put_failure;
600 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
601 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
602 goto nla_put_failure;
603 }
604
9360ffd1
DM
605 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
606 DBM_TO_MBM(chan->max_power)))
607 goto nla_put_failure;
5dab3b8a
LR
608
609 return 0;
610
611 nla_put_failure:
612 return -ENOBUFS;
613}
614
55682965
JB
615/* netlink command implementations */
616
b9454e83
JB
617struct key_parse {
618 struct key_params p;
619 int idx;
e31b8213 620 int type;
b9454e83 621 bool def, defmgmt;
dbd2fd65 622 bool def_uni, def_multi;
b9454e83
JB
623};
624
625static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
626{
627 struct nlattr *tb[NL80211_KEY_MAX + 1];
628 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
629 nl80211_key_policy);
630 if (err)
631 return err;
632
633 k->def = !!tb[NL80211_KEY_DEFAULT];
634 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
635
dbd2fd65
JB
636 if (k->def) {
637 k->def_uni = true;
638 k->def_multi = true;
639 }
640 if (k->defmgmt)
641 k->def_multi = true;
642
b9454e83
JB
643 if (tb[NL80211_KEY_IDX])
644 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
645
646 if (tb[NL80211_KEY_DATA]) {
647 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
648 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
649 }
650
651 if (tb[NL80211_KEY_SEQ]) {
652 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
653 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
654 }
655
656 if (tb[NL80211_KEY_CIPHER])
657 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
658
e31b8213
JB
659 if (tb[NL80211_KEY_TYPE]) {
660 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
661 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
662 return -EINVAL;
663 }
664
dbd2fd65
JB
665 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
666 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
2da8f419
JB
667 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
668 tb[NL80211_KEY_DEFAULT_TYPES],
669 nl80211_key_default_policy);
dbd2fd65
JB
670 if (err)
671 return err;
672
673 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
674 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
675 }
676
b9454e83
JB
677 return 0;
678}
679
680static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
681{
682 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
683 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
684 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
685 }
686
687 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
688 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
689 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
690 }
691
692 if (info->attrs[NL80211_ATTR_KEY_IDX])
693 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
694
695 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
696 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
697
698 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
699 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
700
dbd2fd65
JB
701 if (k->def) {
702 k->def_uni = true;
703 k->def_multi = true;
704 }
705 if (k->defmgmt)
706 k->def_multi = true;
707
e31b8213
JB
708 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
709 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
710 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
711 return -EINVAL;
712 }
713
dbd2fd65
JB
714 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
715 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
716 int err = nla_parse_nested(
717 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
718 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
719 nl80211_key_default_policy);
720 if (err)
721 return err;
722
723 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
724 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
725 }
726
b9454e83
JB
727 return 0;
728}
729
730static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
731{
732 int err;
733
734 memset(k, 0, sizeof(*k));
735 k->idx = -1;
e31b8213 736 k->type = -1;
b9454e83
JB
737
738 if (info->attrs[NL80211_ATTR_KEY])
739 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
740 else
741 err = nl80211_parse_key_old(info, k);
742
743 if (err)
744 return err;
745
746 if (k->def && k->defmgmt)
747 return -EINVAL;
748
dbd2fd65
JB
749 if (k->defmgmt) {
750 if (k->def_uni || !k->def_multi)
751 return -EINVAL;
752 }
753
b9454e83
JB
754 if (k->idx != -1) {
755 if (k->defmgmt) {
756 if (k->idx < 4 || k->idx > 5)
757 return -EINVAL;
758 } else if (k->def) {
759 if (k->idx < 0 || k->idx > 3)
760 return -EINVAL;
761 } else {
762 if (k->idx < 0 || k->idx > 5)
763 return -EINVAL;
764 }
765 }
766
767 return 0;
768}
769
fffd0934
JB
770static struct cfg80211_cached_keys *
771nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
de7044ee 772 struct nlattr *keys, bool *no_ht)
fffd0934
JB
773{
774 struct key_parse parse;
775 struct nlattr *key;
776 struct cfg80211_cached_keys *result;
777 int rem, err, def = 0;
778
779 result = kzalloc(sizeof(*result), GFP_KERNEL);
780 if (!result)
781 return ERR_PTR(-ENOMEM);
782
783 result->def = -1;
784 result->defmgmt = -1;
785
786 nla_for_each_nested(key, keys, rem) {
787 memset(&parse, 0, sizeof(parse));
788 parse.idx = -1;
789
790 err = nl80211_parse_key_new(key, &parse);
791 if (err)
792 goto error;
793 err = -EINVAL;
794 if (!parse.p.key)
795 goto error;
796 if (parse.idx < 0 || parse.idx > 4)
797 goto error;
798 if (parse.def) {
799 if (def)
800 goto error;
801 def = 1;
802 result->def = parse.idx;
dbd2fd65
JB
803 if (!parse.def_uni || !parse.def_multi)
804 goto error;
fffd0934
JB
805 } else if (parse.defmgmt)
806 goto error;
807 err = cfg80211_validate_key_settings(rdev, &parse.p,
e31b8213 808 parse.idx, false, NULL);
fffd0934
JB
809 if (err)
810 goto error;
811 result->params[parse.idx].cipher = parse.p.cipher;
812 result->params[parse.idx].key_len = parse.p.key_len;
813 result->params[parse.idx].key = result->data[parse.idx];
814 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
de7044ee
SM
815
816 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
817 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
818 if (no_ht)
819 *no_ht = true;
820 }
fffd0934
JB
821 }
822
823 return result;
824 error:
825 kfree(result);
826 return ERR_PTR(err);
827}
828
829static int nl80211_key_allowed(struct wireless_dev *wdev)
830{
831 ASSERT_WDEV_LOCK(wdev);
832
fffd0934
JB
833 switch (wdev->iftype) {
834 case NL80211_IFTYPE_AP:
835 case NL80211_IFTYPE_AP_VLAN:
074ac8df 836 case NL80211_IFTYPE_P2P_GO:
ff973af7 837 case NL80211_IFTYPE_MESH_POINT:
fffd0934
JB
838 break;
839 case NL80211_IFTYPE_ADHOC:
840 if (!wdev->current_bss)
841 return -ENOLINK;
842 break;
843 case NL80211_IFTYPE_STATION:
074ac8df 844 case NL80211_IFTYPE_P2P_CLIENT:
fffd0934
JB
845 if (wdev->sme_state != CFG80211_SME_CONNECTED)
846 return -ENOLINK;
847 break;
848 default:
849 return -EINVAL;
850 }
851
852 return 0;
853}
854
7527a782
JB
855static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
856{
857 struct nlattr *nl_modes = nla_nest_start(msg, attr);
858 int i;
859
860 if (!nl_modes)
861 goto nla_put_failure;
862
863 i = 0;
864 while (ifmodes) {
9360ffd1
DM
865 if ((ifmodes & 1) && nla_put_flag(msg, i))
866 goto nla_put_failure;
7527a782
JB
867 ifmodes >>= 1;
868 i++;
869 }
870
871 nla_nest_end(msg, nl_modes);
872 return 0;
873
874nla_put_failure:
875 return -ENOBUFS;
876}
877
878static int nl80211_put_iface_combinations(struct wiphy *wiphy,
cdc89b97
JB
879 struct sk_buff *msg,
880 bool large)
7527a782
JB
881{
882 struct nlattr *nl_combis;
883 int i, j;
884
885 nl_combis = nla_nest_start(msg,
886 NL80211_ATTR_INTERFACE_COMBINATIONS);
887 if (!nl_combis)
888 goto nla_put_failure;
889
890 for (i = 0; i < wiphy->n_iface_combinations; i++) {
891 const struct ieee80211_iface_combination *c;
892 struct nlattr *nl_combi, *nl_limits;
893
894 c = &wiphy->iface_combinations[i];
895
896 nl_combi = nla_nest_start(msg, i + 1);
897 if (!nl_combi)
898 goto nla_put_failure;
899
900 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
901 if (!nl_limits)
902 goto nla_put_failure;
903
904 for (j = 0; j < c->n_limits; j++) {
905 struct nlattr *nl_limit;
906
907 nl_limit = nla_nest_start(msg, j + 1);
908 if (!nl_limit)
909 goto nla_put_failure;
9360ffd1
DM
910 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
911 c->limits[j].max))
912 goto nla_put_failure;
7527a782
JB
913 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
914 c->limits[j].types))
915 goto nla_put_failure;
916 nla_nest_end(msg, nl_limit);
917 }
918
919 nla_nest_end(msg, nl_limits);
920
9360ffd1
DM
921 if (c->beacon_int_infra_match &&
922 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
923 goto nla_put_failure;
924 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
925 c->num_different_channels) ||
926 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
927 c->max_interfaces))
928 goto nla_put_failure;
cdc89b97
JB
929 if (large &&
930 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
931 c->radar_detect_widths))
932 goto nla_put_failure;
7527a782
JB
933
934 nla_nest_end(msg, nl_combi);
935 }
936
937 nla_nest_end(msg, nl_combis);
938
939 return 0;
940nla_put_failure:
941 return -ENOBUFS;
942}
943
3713b4e3 944#ifdef CONFIG_PM
b56cf720
JB
945static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
946 struct sk_buff *msg)
947{
948 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan.tcp;
949 struct nlattr *nl_tcp;
950
951 if (!tcp)
952 return 0;
953
954 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
955 if (!nl_tcp)
956 return -ENOBUFS;
957
958 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
959 tcp->data_payload_max))
960 return -ENOBUFS;
961
962 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
963 tcp->data_payload_max))
964 return -ENOBUFS;
965
966 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
967 return -ENOBUFS;
968
969 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
970 sizeof(*tcp->tok), tcp->tok))
971 return -ENOBUFS;
972
973 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
974 tcp->data_interval_max))
975 return -ENOBUFS;
976
977 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
978 tcp->wake_payload_max))
979 return -ENOBUFS;
980
981 nla_nest_end(msg, nl_tcp);
982 return 0;
983}
984
3713b4e3 985static int nl80211_send_wowlan(struct sk_buff *msg,
b56cf720
JB
986 struct cfg80211_registered_device *dev,
987 bool large)
55682965 988{
3713b4e3 989 struct nlattr *nl_wowlan;
55682965 990
3713b4e3
JB
991 if (!dev->wiphy.wowlan.flags && !dev->wiphy.wowlan.n_patterns)
992 return 0;
55682965 993
3713b4e3
JB
994 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
995 if (!nl_wowlan)
996 return -ENOBUFS;
9360ffd1 997
3713b4e3
JB
998 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
999 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1000 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1001 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1002 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1003 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1004 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1005 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1006 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1007 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1008 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1009 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1010 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1011 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1012 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1013 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1014 return -ENOBUFS;
9360ffd1 1015
3713b4e3
JB
1016 if (dev->wiphy.wowlan.n_patterns) {
1017 struct nl80211_wowlan_pattern_support pat = {
1018 .max_patterns = dev->wiphy.wowlan.n_patterns,
1019 .min_pattern_len = dev->wiphy.wowlan.pattern_min_len,
1020 .max_pattern_len = dev->wiphy.wowlan.pattern_max_len,
1021 .max_pkt_offset = dev->wiphy.wowlan.max_pkt_offset,
1022 };
9360ffd1 1023
3713b4e3
JB
1024 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1025 sizeof(pat), &pat))
1026 return -ENOBUFS;
1027 }
9360ffd1 1028
b56cf720
JB
1029 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1030 return -ENOBUFS;
1031
3713b4e3 1032 nla_nest_end(msg, nl_wowlan);
9360ffd1 1033
3713b4e3
JB
1034 return 0;
1035}
1036#endif
9360ffd1 1037
3713b4e3
JB
1038static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1039 struct ieee80211_supported_band *sband)
1040{
1041 struct nlattr *nl_rates, *nl_rate;
1042 struct ieee80211_rate *rate;
1043 int i;
87bbbe22 1044
3713b4e3
JB
1045 /* add HT info */
1046 if (sband->ht_cap.ht_supported &&
1047 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1048 sizeof(sband->ht_cap.mcs),
1049 &sband->ht_cap.mcs) ||
1050 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1051 sband->ht_cap.cap) ||
1052 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1053 sband->ht_cap.ampdu_factor) ||
1054 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1055 sband->ht_cap.ampdu_density)))
1056 return -ENOBUFS;
afe0cbf8 1057
3713b4e3
JB
1058 /* add VHT info */
1059 if (sband->vht_cap.vht_supported &&
1060 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1061 sizeof(sband->vht_cap.vht_mcs),
1062 &sband->vht_cap.vht_mcs) ||
1063 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1064 sband->vht_cap.cap)))
1065 return -ENOBUFS;
f59ac048 1066
3713b4e3
JB
1067 /* add bitrates */
1068 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1069 if (!nl_rates)
1070 return -ENOBUFS;
ee688b00 1071
3713b4e3
JB
1072 for (i = 0; i < sband->n_bitrates; i++) {
1073 nl_rate = nla_nest_start(msg, i);
1074 if (!nl_rate)
1075 return -ENOBUFS;
ee688b00 1076
3713b4e3
JB
1077 rate = &sband->bitrates[i];
1078 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1079 rate->bitrate))
1080 return -ENOBUFS;
1081 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1082 nla_put_flag(msg,
1083 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1084 return -ENOBUFS;
ee688b00 1085
3713b4e3
JB
1086 nla_nest_end(msg, nl_rate);
1087 }
d51626df 1088
3713b4e3 1089 nla_nest_end(msg, nl_rates);
bf0c111e 1090
3713b4e3
JB
1091 return 0;
1092}
ee688b00 1093
3713b4e3
JB
1094static int
1095nl80211_send_mgmt_stypes(struct sk_buff *msg,
1096 const struct ieee80211_txrx_stypes *mgmt_stypes)
1097{
1098 u16 stypes;
1099 struct nlattr *nl_ftypes, *nl_ifs;
1100 enum nl80211_iftype ift;
1101 int i;
ee688b00 1102
3713b4e3
JB
1103 if (!mgmt_stypes)
1104 return 0;
5dab3b8a 1105
3713b4e3
JB
1106 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1107 if (!nl_ifs)
1108 return -ENOBUFS;
e2f367f2 1109
3713b4e3
JB
1110 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1111 nl_ftypes = nla_nest_start(msg, ift);
1112 if (!nl_ftypes)
1113 return -ENOBUFS;
1114 i = 0;
1115 stypes = mgmt_stypes[ift].tx;
1116 while (stypes) {
1117 if ((stypes & 1) &&
1118 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1119 (i << 4) | IEEE80211_FTYPE_MGMT))
1120 return -ENOBUFS;
1121 stypes >>= 1;
1122 i++;
ee688b00 1123 }
3713b4e3
JB
1124 nla_nest_end(msg, nl_ftypes);
1125 }
ee688b00 1126
3713b4e3 1127 nla_nest_end(msg, nl_ifs);
ee688b00 1128
3713b4e3
JB
1129 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1130 if (!nl_ifs)
1131 return -ENOBUFS;
ee688b00 1132
3713b4e3
JB
1133 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1134 nl_ftypes = nla_nest_start(msg, ift);
1135 if (!nl_ftypes)
1136 return -ENOBUFS;
1137 i = 0;
1138 stypes = mgmt_stypes[ift].rx;
1139 while (stypes) {
1140 if ((stypes & 1) &&
1141 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1142 (i << 4) | IEEE80211_FTYPE_MGMT))
1143 return -ENOBUFS;
1144 stypes >>= 1;
1145 i++;
1146 }
1147 nla_nest_end(msg, nl_ftypes);
1148 }
1149 nla_nest_end(msg, nl_ifs);
ee688b00 1150
3713b4e3
JB
1151 return 0;
1152}
ee688b00 1153
3713b4e3
JB
1154static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1155 struct sk_buff *msg, u32 portid, u32 seq,
1156 int flags, bool split, long *split_start,
1157 long *band_start, long *chan_start)
1158{
1159 void *hdr;
1160 struct nlattr *nl_bands, *nl_band;
1161 struct nlattr *nl_freqs, *nl_freq;
1162 struct nlattr *nl_cmds;
1163 enum ieee80211_band band;
1164 struct ieee80211_channel *chan;
1165 int i;
1166 const struct ieee80211_txrx_stypes *mgmt_stypes =
1167 dev->wiphy.mgmt_stypes;
1168 long start = 0, start_chan = 0, start_band = 0;
fe1abafd 1169 u32 features;
ee688b00 1170
3713b4e3
JB
1171 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
1172 if (!hdr)
1173 return -ENOBUFS;
ee688b00 1174
3713b4e3
JB
1175 /* allow always using the variables */
1176 if (!split) {
1177 split_start = &start;
1178 band_start = &start_band;
1179 chan_start = &start_chan;
ee688b00 1180 }
ee688b00 1181
3713b4e3
JB
1182 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
1183 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1184 wiphy_name(&dev->wiphy)) ||
1185 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1186 cfg80211_rdev_list_generation))
8fdc621d
JB
1187 goto nla_put_failure;
1188
3713b4e3
JB
1189 switch (*split_start) {
1190 case 0:
1191 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1192 dev->wiphy.retry_short) ||
1193 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1194 dev->wiphy.retry_long) ||
1195 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1196 dev->wiphy.frag_threshold) ||
1197 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1198 dev->wiphy.rts_threshold) ||
1199 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1200 dev->wiphy.coverage_class) ||
1201 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1202 dev->wiphy.max_scan_ssids) ||
1203 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1204 dev->wiphy.max_sched_scan_ssids) ||
1205 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1206 dev->wiphy.max_scan_ie_len) ||
1207 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1208 dev->wiphy.max_sched_scan_ie_len) ||
1209 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1210 dev->wiphy.max_match_sets))
9360ffd1 1211 goto nla_put_failure;
3713b4e3
JB
1212
1213 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1214 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
aa430da4 1215 goto nla_put_failure;
3713b4e3
JB
1216 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1217 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1218 goto nla_put_failure;
1219 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1220 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1221 goto nla_put_failure;
1222 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1223 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1224 goto nla_put_failure;
1225 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1226 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1227 goto nla_put_failure;
1228 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1229 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
9360ffd1 1230 goto nla_put_failure;
8fdc621d 1231
3713b4e3
JB
1232 (*split_start)++;
1233 if (split)
1234 break;
1235 case 1:
1236 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1237 sizeof(u32) * dev->wiphy.n_cipher_suites,
1238 dev->wiphy.cipher_suites))
1239 goto nla_put_failure;
4745fc09 1240
3713b4e3
JB
1241 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1242 dev->wiphy.max_num_pmkids))
1243 goto nla_put_failure;
b23aa676 1244
3713b4e3
JB
1245 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1246 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
9360ffd1 1247 goto nla_put_failure;
b23aa676 1248
3713b4e3
JB
1249 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1250 dev->wiphy.available_antennas_tx) ||
1251 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1252 dev->wiphy.available_antennas_rx))
9360ffd1 1253 goto nla_put_failure;
b23aa676 1254
3713b4e3
JB
1255 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1256 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1257 dev->wiphy.probe_resp_offload))
1258 goto nla_put_failure;
8fdc621d 1259
3713b4e3
JB
1260 if ((dev->wiphy.available_antennas_tx ||
1261 dev->wiphy.available_antennas_rx) &&
1262 dev->ops->get_antenna) {
1263 u32 tx_ant = 0, rx_ant = 0;
1264 int res;
1265 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1266 if (!res) {
1267 if (nla_put_u32(msg,
1268 NL80211_ATTR_WIPHY_ANTENNA_TX,
1269 tx_ant) ||
1270 nla_put_u32(msg,
1271 NL80211_ATTR_WIPHY_ANTENNA_RX,
1272 rx_ant))
1273 goto nla_put_failure;
1274 }
1275 }
a293911d 1276
3713b4e3
JB
1277 (*split_start)++;
1278 if (split)
1279 break;
1280 case 2:
1281 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1282 dev->wiphy.interface_modes))
1283 goto nla_put_failure;
1284 (*split_start)++;
1285 if (split)
1286 break;
1287 case 3:
1288 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1289 if (!nl_bands)
1290 goto nla_put_failure;
f7ca38df 1291
3713b4e3
JB
1292 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1293 struct ieee80211_supported_band *sband;
2e161f78 1294
3713b4e3 1295 sband = dev->wiphy.bands[band];
2e161f78 1296
3713b4e3
JB
1297 if (!sband)
1298 continue;
1299
1300 nl_band = nla_nest_start(msg, band);
1301 if (!nl_band)
2e161f78 1302 goto nla_put_failure;
3713b4e3
JB
1303
1304 switch (*chan_start) {
1305 case 0:
1306 if (nl80211_send_band_rateinfo(msg, sband))
9360ffd1 1307 goto nla_put_failure;
3713b4e3
JB
1308 (*chan_start)++;
1309 if (split)
1310 break;
1311 default:
1312 /* add frequencies */
1313 nl_freqs = nla_nest_start(
1314 msg, NL80211_BAND_ATTR_FREQS);
1315 if (!nl_freqs)
1316 goto nla_put_failure;
1317
1318 for (i = *chan_start - 1;
1319 i < sband->n_channels;
1320 i++) {
1321 nl_freq = nla_nest_start(msg, i);
1322 if (!nl_freq)
1323 goto nla_put_failure;
1324
1325 chan = &sband->channels[i];
1326
cdc89b97
JB
1327 if (nl80211_msg_put_channel(msg, chan,
1328 split))
3713b4e3
JB
1329 goto nla_put_failure;
1330
1331 nla_nest_end(msg, nl_freq);
1332 if (split)
1333 break;
1334 }
1335 if (i < sband->n_channels)
1336 *chan_start = i + 2;
1337 else
1338 *chan_start = 0;
1339 nla_nest_end(msg, nl_freqs);
1340 }
1341
1342 nla_nest_end(msg, nl_band);
1343
1344 if (split) {
1345 /* start again here */
1346 if (*chan_start)
1347 band--;
1348 break;
2e161f78 1349 }
2e161f78 1350 }
3713b4e3 1351 nla_nest_end(msg, nl_bands);
2e161f78 1352
3713b4e3
JB
1353 if (band < IEEE80211_NUM_BANDS)
1354 *band_start = band + 1;
1355 else
1356 *band_start = 0;
74b70a4e 1357
3713b4e3
JB
1358 /* if bands & channels are done, continue outside */
1359 if (*band_start == 0 && *chan_start == 0)
1360 (*split_start)++;
1361 if (split)
1362 break;
1363 case 4:
1364 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1365 if (!nl_cmds)
2e161f78
JB
1366 goto nla_put_failure;
1367
3713b4e3
JB
1368 i = 0;
1369#define CMD(op, n) \
1370 do { \
1371 if (dev->ops->op) { \
1372 i++; \
1373 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1374 goto nla_put_failure; \
1375 } \
1376 } while (0)
1377
1378 CMD(add_virtual_intf, NEW_INTERFACE);
1379 CMD(change_virtual_intf, SET_INTERFACE);
1380 CMD(add_key, NEW_KEY);
1381 CMD(start_ap, START_AP);
1382 CMD(add_station, NEW_STATION);
1383 CMD(add_mpath, NEW_MPATH);
1384 CMD(update_mesh_config, SET_MESH_CONFIG);
1385 CMD(change_bss, SET_BSS);
1386 CMD(auth, AUTHENTICATE);
1387 CMD(assoc, ASSOCIATE);
1388 CMD(deauth, DEAUTHENTICATE);
1389 CMD(disassoc, DISASSOCIATE);
1390 CMD(join_ibss, JOIN_IBSS);
1391 CMD(join_mesh, JOIN_MESH);
1392 CMD(set_pmksa, SET_PMKSA);
1393 CMD(del_pmksa, DEL_PMKSA);
1394 CMD(flush_pmksa, FLUSH_PMKSA);
1395 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1396 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1397 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1398 CMD(mgmt_tx, FRAME);
1399 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1400 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1401 i++;
1402 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2e161f78 1403 goto nla_put_failure;
2e161f78 1404 }
3713b4e3
JB
1405 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1406 dev->ops->join_mesh) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1409 goto nla_put_failure;
1410 }
1411 CMD(set_wds_peer, SET_WDS_PEER);
1412 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1413 CMD(tdls_mgmt, TDLS_MGMT);
1414 CMD(tdls_oper, TDLS_OPER);
1415 }
1416 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1417 CMD(sched_scan_start, START_SCHED_SCAN);
1418 CMD(probe_client, PROBE_CLIENT);
1419 CMD(set_noack_map, SET_NOACK_MAP);
1420 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1421 i++;
1422 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1423 goto nla_put_failure;
1424 }
1425 CMD(start_p2p_device, START_P2P_DEVICE);
1426 CMD(set_mcast_rate, SET_MCAST_RATE);
5de17984
AS
1427 if (split) {
1428 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1429 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1430 }
2e161f78 1431
3713b4e3
JB
1432#ifdef CONFIG_NL80211_TESTMODE
1433 CMD(testmode_cmd, TESTMODE);
1434#endif
ff1b6e69 1435
3713b4e3 1436#undef CMD
ff1b6e69 1437
3713b4e3
JB
1438 if (dev->ops->connect || dev->ops->auth) {
1439 i++;
1440 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
9360ffd1 1441 goto nla_put_failure;
ff1b6e69
JB
1442 }
1443
3713b4e3
JB
1444 if (dev->ops->disconnect || dev->ops->deauth) {
1445 i++;
1446 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1447 goto nla_put_failure;
1448 }
1449
1450 nla_nest_end(msg, nl_cmds);
1451 (*split_start)++;
1452 if (split)
1453 break;
1454 case 5:
1455 if (dev->ops->remain_on_channel &&
1456 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1457 nla_put_u32(msg,
1458 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1459 dev->wiphy.max_remain_on_channel_duration))
1460 goto nla_put_failure;
1461
1462 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1463 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1464 goto nla_put_failure;
1465
1466 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1467 goto nla_put_failure;
1468 (*split_start)++;
1469 if (split)
1470 break;
1471 case 6:
1472#ifdef CONFIG_PM
b56cf720 1473 if (nl80211_send_wowlan(msg, dev, split))
3713b4e3
JB
1474 goto nla_put_failure;
1475 (*split_start)++;
1476 if (split)
1477 break;
1478#else
1479 (*split_start)++;
dfb89c56 1480#endif
3713b4e3
JB
1481 case 7:
1482 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1483 dev->wiphy.software_iftypes))
1484 goto nla_put_failure;
ff1b6e69 1485
cdc89b97 1486 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
3713b4e3 1487 goto nla_put_failure;
7527a782 1488
3713b4e3
JB
1489 (*split_start)++;
1490 if (split)
1491 break;
1492 case 8:
1493 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1494 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1495 dev->wiphy.ap_sme_capa))
1496 goto nla_put_failure;
7527a782 1497
fe1abafd
JB
1498 features = dev->wiphy.features;
1499 /*
1500 * We can only add the per-channel limit information if the
1501 * dump is split, otherwise it makes it too big. Therefore
1502 * only advertise it in that case.
1503 */
1504 if (split)
1505 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1506 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
3713b4e3 1507 goto nla_put_failure;
562a7480 1508
3713b4e3
JB
1509 if (dev->wiphy.ht_capa_mod_mask &&
1510 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1511 sizeof(*dev->wiphy.ht_capa_mod_mask),
1512 dev->wiphy.ht_capa_mod_mask))
1513 goto nla_put_failure;
1f074bd8 1514
3713b4e3
JB
1515 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1516 dev->wiphy.max_acl_mac_addrs &&
1517 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1518 dev->wiphy.max_acl_mac_addrs))
1519 goto nla_put_failure;
7e7c8926 1520
3713b4e3
JB
1521 /*
1522 * Any information below this point is only available to
1523 * applications that can deal with it being split. This
1524 * helps ensure that newly added capabilities don't break
1525 * older tools by overrunning their buffers.
1526 *
1527 * We still increment split_start so that in the split
1528 * case we'll continue with more data in the next round,
1529 * but break unconditionally so unsplit data stops here.
1530 */
1531 (*split_start)++;
1532 break;
1533 case 9:
fe1abafd
JB
1534 if (dev->wiphy.extended_capabilities &&
1535 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1536 dev->wiphy.extended_capabilities_len,
1537 dev->wiphy.extended_capabilities) ||
1538 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1539 dev->wiphy.extended_capabilities_len,
1540 dev->wiphy.extended_capabilities_mask)))
1541 goto nla_put_failure;
a50df0c4 1542
ee2aca34
JB
1543 if (dev->wiphy.vht_capa_mod_mask &&
1544 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1545 sizeof(*dev->wiphy.vht_capa_mod_mask),
1546 dev->wiphy.vht_capa_mod_mask))
1547 goto nla_put_failure;
1548
3713b4e3
JB
1549 /* done */
1550 *split_start = 0;
1551 break;
1552 }
55682965
JB
1553 return genlmsg_end(msg, hdr);
1554
1555 nla_put_failure:
bc3ed28c
TG
1556 genlmsg_cancel(msg, hdr);
1557 return -EMSGSIZE;
55682965
JB
1558}
1559
1560static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1561{
645e77de 1562 int idx = 0, ret;
55682965
JB
1563 int start = cb->args[0];
1564 struct cfg80211_registered_device *dev;
3713b4e3
JB
1565 s64 filter_wiphy = -1;
1566 bool split = false;
1567 struct nlattr **tb = nl80211_fam.attrbuf;
1568 int res;
55682965 1569
a1794390 1570 mutex_lock(&cfg80211_mutex);
3713b4e3
JB
1571 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1572 tb, nl80211_fam.maxattr, nl80211_policy);
1573 if (res == 0) {
1574 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1575 if (tb[NL80211_ATTR_WIPHY])
1576 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1577 if (tb[NL80211_ATTR_WDEV])
1578 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1579 if (tb[NL80211_ATTR_IFINDEX]) {
1580 struct net_device *netdev;
1581 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1582
1583 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1584 if (!netdev) {
1585 mutex_unlock(&cfg80211_mutex);
1586 return -ENODEV;
1587 }
1588 if (netdev->ieee80211_ptr) {
1589 dev = wiphy_to_dev(
1590 netdev->ieee80211_ptr->wiphy);
1591 filter_wiphy = dev->wiphy_idx;
1592 }
1593 dev_put(netdev);
1594 }
1595 }
1596
79c97e97 1597 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
463d0183
JB
1598 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1599 continue;
b4637271 1600 if (++idx <= start)
55682965 1601 continue;
3713b4e3
JB
1602 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1603 continue;
1604 /* attempt to fit multiple wiphy data chunks into the skb */
1605 do {
1606 ret = nl80211_send_wiphy(dev, skb,
1607 NETLINK_CB(cb->skb).portid,
1608 cb->nlh->nlmsg_seq,
1609 NLM_F_MULTI,
1610 split, &cb->args[1],
1611 &cb->args[2],
1612 &cb->args[3]);
1613 if (ret < 0) {
1614 /*
1615 * If sending the wiphy data didn't fit (ENOBUFS
1616 * or EMSGSIZE returned), this SKB is still
1617 * empty (so it's not too big because another
1618 * wiphy dataset is already in the skb) and
1619 * we've not tried to adjust the dump allocation
1620 * yet ... then adjust the alloc size to be
1621 * bigger, and return 1 but with the empty skb.
1622 * This results in an empty message being RX'ed
1623 * in userspace, but that is ignored.
1624 *
1625 * We can then retry with the larger buffer.
1626 */
1627 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1628 !skb->len &&
1629 cb->min_dump_alloc < 4096) {
1630 cb->min_dump_alloc = 4096;
1631 mutex_unlock(&cfg80211_mutex);
1632 return 1;
1633 }
1634 idx--;
1635 break;
645e77de 1636 }
3713b4e3
JB
1637 } while (cb->args[1] > 0);
1638 break;
55682965 1639 }
a1794390 1640 mutex_unlock(&cfg80211_mutex);
55682965
JB
1641
1642 cb->args[0] = idx;
1643
1644 return skb->len;
1645}
1646
1647static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1648{
1649 struct sk_buff *msg;
4c476991 1650 struct cfg80211_registered_device *dev = info->user_ptr[0];
55682965 1651
645e77de 1652 msg = nlmsg_new(4096, GFP_KERNEL);
55682965 1653 if (!msg)
4c476991 1654 return -ENOMEM;
55682965 1655
3713b4e3
JB
1656 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1657 false, NULL, NULL, NULL) < 0) {
4c476991
JB
1658 nlmsg_free(msg);
1659 return -ENOBUFS;
1660 }
55682965 1661
134e6375 1662 return genlmsg_reply(msg, info);
55682965
JB
1663}
1664
31888487
JM
1665static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1666 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1667 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1668 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1669 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1670 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1671};
1672
1673static int parse_txq_params(struct nlattr *tb[],
1674 struct ieee80211_txq_params *txq_params)
1675{
a3304b0a 1676 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
31888487
JM
1677 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1678 !tb[NL80211_TXQ_ATTR_AIFS])
1679 return -EINVAL;
1680
a3304b0a 1681 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
31888487
JM
1682 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1683 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1684 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1685 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1686
a3304b0a
JB
1687 if (txq_params->ac >= NL80211_NUM_ACS)
1688 return -EINVAL;
1689
31888487
JM
1690 return 0;
1691}
1692
f444de05
JB
1693static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1694{
1695 /*
cc1d2806
JB
1696 * You can only set the channel explicitly for WDS interfaces,
1697 * all others have their channel managed via their respective
1698 * "establish a connection" command (connect, join, ...)
1699 *
1700 * For AP/GO and mesh mode, the channel can be set with the
1701 * channel userspace API, but is only stored and passed to the
1702 * low-level driver when the AP starts or the mesh is joined.
1703 * This is for backward compatibility, userspace can also give
1704 * the channel in the start-ap or join-mesh commands instead.
f444de05
JB
1705 *
1706 * Monitors are special as they are normally slaved to
e8c9bd5b
JB
1707 * whatever else is going on, so they have their own special
1708 * operation to set the monitor channel if possible.
f444de05
JB
1709 */
1710 return !wdev ||
1711 wdev->iftype == NL80211_IFTYPE_AP ||
f444de05 1712 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
074ac8df
JB
1713 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1714 wdev->iftype == NL80211_IFTYPE_P2P_GO;
f444de05
JB
1715}
1716
683b6d3b
JB
1717static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1718 struct genl_info *info,
1719 struct cfg80211_chan_def *chandef)
1720{
dbeca2ea 1721 u32 control_freq;
683b6d3b
JB
1722
1723 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1724 return -EINVAL;
1725
1726 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1727
1728 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
3d9d1d66
JB
1729 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1730 chandef->center_freq1 = control_freq;
1731 chandef->center_freq2 = 0;
683b6d3b
JB
1732
1733 /* Primary channel not allowed */
1734 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1735 return -EINVAL;
1736
3d9d1d66
JB
1737 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1738 enum nl80211_channel_type chantype;
1739
1740 chantype = nla_get_u32(
1741 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1742
1743 switch (chantype) {
1744 case NL80211_CHAN_NO_HT:
1745 case NL80211_CHAN_HT20:
1746 case NL80211_CHAN_HT40PLUS:
1747 case NL80211_CHAN_HT40MINUS:
1748 cfg80211_chandef_create(chandef, chandef->chan,
1749 chantype);
1750 break;
1751 default:
1752 return -EINVAL;
1753 }
1754 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1755 chandef->width =
1756 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1757 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1758 chandef->center_freq1 =
1759 nla_get_u32(
1760 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1761 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1762 chandef->center_freq2 =
1763 nla_get_u32(
1764 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1765 }
1766
9f5e8f6e 1767 if (!cfg80211_chandef_valid(chandef))
3d9d1d66
JB
1768 return -EINVAL;
1769
9f5e8f6e
JB
1770 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1771 IEEE80211_CHAN_DISABLED))
3d9d1d66
JB
1772 return -EINVAL;
1773
683b6d3b
JB
1774 return 0;
1775}
1776
f444de05
JB
1777static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1778 struct wireless_dev *wdev,
1779 struct genl_info *info)
1780{
683b6d3b 1781 struct cfg80211_chan_def chandef;
f444de05 1782 int result;
e8c9bd5b
JB
1783 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1784
1785 if (wdev)
1786 iftype = wdev->iftype;
f444de05 1787
f444de05
JB
1788 if (!nl80211_can_set_dev_channel(wdev))
1789 return -EOPNOTSUPP;
1790
683b6d3b
JB
1791 result = nl80211_parse_chandef(rdev, info, &chandef);
1792 if (result)
1793 return result;
f444de05
JB
1794
1795 mutex_lock(&rdev->devlist_mtx);
e8c9bd5b 1796 switch (iftype) {
aa430da4
JB
1797 case NL80211_IFTYPE_AP:
1798 case NL80211_IFTYPE_P2P_GO:
1799 if (wdev->beacon_interval) {
1800 result = -EBUSY;
1801 break;
1802 }
683b6d3b 1803 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
aa430da4
JB
1804 result = -EINVAL;
1805 break;
1806 }
683b6d3b 1807 wdev->preset_chandef = chandef;
aa430da4
JB
1808 result = 0;
1809 break;
cc1d2806 1810 case NL80211_IFTYPE_MESH_POINT:
683b6d3b 1811 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
cc1d2806 1812 break;
e8c9bd5b 1813 case NL80211_IFTYPE_MONITOR:
683b6d3b 1814 result = cfg80211_set_monitor_channel(rdev, &chandef);
e8c9bd5b 1815 break;
aa430da4 1816 default:
e8c9bd5b 1817 result = -EINVAL;
f444de05
JB
1818 }
1819 mutex_unlock(&rdev->devlist_mtx);
1820
1821 return result;
1822}
1823
1824static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1825{
4c476991
JB
1826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1827 struct net_device *netdev = info->user_ptr[1];
f444de05 1828
4c476991 1829 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
f444de05
JB
1830}
1831
e8347eba
BJ
1832static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1833{
43b19952
JB
1834 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1835 struct net_device *dev = info->user_ptr[1];
1836 struct wireless_dev *wdev = dev->ieee80211_ptr;
388ac775 1837 const u8 *bssid;
e8347eba
BJ
1838
1839 if (!info->attrs[NL80211_ATTR_MAC])
1840 return -EINVAL;
1841
43b19952
JB
1842 if (netif_running(dev))
1843 return -EBUSY;
e8347eba 1844
43b19952
JB
1845 if (!rdev->ops->set_wds_peer)
1846 return -EOPNOTSUPP;
e8347eba 1847
43b19952
JB
1848 if (wdev->iftype != NL80211_IFTYPE_WDS)
1849 return -EOPNOTSUPP;
e8347eba
BJ
1850
1851 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
e35e4d28 1852 return rdev_set_wds_peer(rdev, dev, bssid);
e8347eba
BJ
1853}
1854
1855
55682965
JB
1856static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1857{
1858 struct cfg80211_registered_device *rdev;
f444de05
JB
1859 struct net_device *netdev = NULL;
1860 struct wireless_dev *wdev;
a1e567c8 1861 int result = 0, rem_txq_params = 0;
31888487 1862 struct nlattr *nl_txq_params;
b9a5f8ca
JM
1863 u32 changed;
1864 u8 retry_short = 0, retry_long = 0;
1865 u32 frag_threshold = 0, rts_threshold = 0;
81077e82 1866 u8 coverage_class = 0;
55682965 1867
f444de05
JB
1868 /*
1869 * Try to find the wiphy and netdev. Normally this
1870 * function shouldn't need the netdev, but this is
1871 * done for backward compatibility -- previously
1872 * setting the channel was done per wiphy, but now
1873 * it is per netdev. Previous userland like hostapd
1874 * also passed a netdev to set_wiphy, so that it is
1875 * possible to let that go to the right netdev!
1876 */
4bbf4d56
JB
1877 mutex_lock(&cfg80211_mutex);
1878
f444de05
JB
1879 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1880 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1881
1882 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1883 if (netdev && netdev->ieee80211_ptr) {
1884 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1885 mutex_lock(&rdev->mtx);
1886 } else
1887 netdev = NULL;
4bbf4d56
JB
1888 }
1889
f444de05 1890 if (!netdev) {
878d9ec7
JB
1891 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1892 info->attrs);
f444de05
JB
1893 if (IS_ERR(rdev)) {
1894 mutex_unlock(&cfg80211_mutex);
4c476991 1895 return PTR_ERR(rdev);
f444de05
JB
1896 }
1897 wdev = NULL;
1898 netdev = NULL;
1899 result = 0;
1900
1901 mutex_lock(&rdev->mtx);
71fe96bf 1902 } else
f444de05 1903 wdev = netdev->ieee80211_ptr;
f444de05
JB
1904
1905 /*
1906 * end workaround code, by now the rdev is available
1907 * and locked, and wdev may or may not be NULL.
1908 */
4bbf4d56
JB
1909
1910 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
31888487
JM
1911 result = cfg80211_dev_rename(
1912 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
4bbf4d56
JB
1913
1914 mutex_unlock(&cfg80211_mutex);
1915
1916 if (result)
1917 goto bad_res;
31888487
JM
1918
1919 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1920 struct ieee80211_txq_params txq_params;
1921 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1922
1923 if (!rdev->ops->set_txq_params) {
1924 result = -EOPNOTSUPP;
1925 goto bad_res;
1926 }
1927
f70f01c2
EP
1928 if (!netdev) {
1929 result = -EINVAL;
1930 goto bad_res;
1931 }
1932
133a3ff2
JB
1933 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1934 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1935 result = -EINVAL;
1936 goto bad_res;
1937 }
1938
2b5f8b0b
JB
1939 if (!netif_running(netdev)) {
1940 result = -ENETDOWN;
1941 goto bad_res;
1942 }
1943
31888487
JM
1944 nla_for_each_nested(nl_txq_params,
1945 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1946 rem_txq_params) {
1947 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1948 nla_data(nl_txq_params),
1949 nla_len(nl_txq_params),
1950 txq_params_policy);
1951 result = parse_txq_params(tb, &txq_params);
1952 if (result)
1953 goto bad_res;
1954
e35e4d28
HG
1955 result = rdev_set_txq_params(rdev, netdev,
1956 &txq_params);
31888487
JM
1957 if (result)
1958 goto bad_res;
1959 }
1960 }
55682965 1961
72bdcf34 1962 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
71fe96bf
JB
1963 result = __nl80211_set_channel(rdev,
1964 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1965 info);
72bdcf34
JM
1966 if (result)
1967 goto bad_res;
1968 }
1969
98d2ff8b 1970 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
c8442118 1971 struct wireless_dev *txp_wdev = wdev;
98d2ff8b
JO
1972 enum nl80211_tx_power_setting type;
1973 int idx, mbm = 0;
1974
c8442118
JB
1975 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1976 txp_wdev = NULL;
1977
98d2ff8b 1978 if (!rdev->ops->set_tx_power) {
60ea385f 1979 result = -EOPNOTSUPP;
98d2ff8b
JO
1980 goto bad_res;
1981 }
1982
1983 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1984 type = nla_get_u32(info->attrs[idx]);
1985
1986 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1987 (type != NL80211_TX_POWER_AUTOMATIC)) {
1988 result = -EINVAL;
1989 goto bad_res;
1990 }
1991
1992 if (type != NL80211_TX_POWER_AUTOMATIC) {
1993 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1994 mbm = nla_get_u32(info->attrs[idx]);
1995 }
1996
c8442118 1997 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
98d2ff8b
JO
1998 if (result)
1999 goto bad_res;
2000 }
2001
afe0cbf8
BR
2002 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2003 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2004 u32 tx_ant, rx_ant;
7f531e03
BR
2005 if ((!rdev->wiphy.available_antennas_tx &&
2006 !rdev->wiphy.available_antennas_rx) ||
2007 !rdev->ops->set_antenna) {
afe0cbf8
BR
2008 result = -EOPNOTSUPP;
2009 goto bad_res;
2010 }
2011
2012 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2013 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2014
a7ffac95 2015 /* reject antenna configurations which don't match the
7f531e03
BR
2016 * available antenna masks, except for the "all" mask */
2017 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2018 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
a7ffac95
BR
2019 result = -EINVAL;
2020 goto bad_res;
2021 }
2022
7f531e03
BR
2023 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2024 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
a7ffac95 2025
e35e4d28 2026 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
afe0cbf8
BR
2027 if (result)
2028 goto bad_res;
2029 }
2030
b9a5f8ca
JM
2031 changed = 0;
2032
2033 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2034 retry_short = nla_get_u8(
2035 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2036 if (retry_short == 0) {
2037 result = -EINVAL;
2038 goto bad_res;
2039 }
2040 changed |= WIPHY_PARAM_RETRY_SHORT;
2041 }
2042
2043 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2044 retry_long = nla_get_u8(
2045 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2046 if (retry_long == 0) {
2047 result = -EINVAL;
2048 goto bad_res;
2049 }
2050 changed |= WIPHY_PARAM_RETRY_LONG;
2051 }
2052
2053 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2054 frag_threshold = nla_get_u32(
2055 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2056 if (frag_threshold < 256) {
2057 result = -EINVAL;
2058 goto bad_res;
2059 }
2060 if (frag_threshold != (u32) -1) {
2061 /*
2062 * Fragments (apart from the last one) are required to
2063 * have even length. Make the fragmentation code
2064 * simpler by stripping LSB should someone try to use
2065 * odd threshold value.
2066 */
2067 frag_threshold &= ~0x1;
2068 }
2069 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2070 }
2071
2072 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2073 rts_threshold = nla_get_u32(
2074 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2075 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2076 }
2077
81077e82
LT
2078 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2079 coverage_class = nla_get_u8(
2080 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2081 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2082 }
2083
b9a5f8ca
JM
2084 if (changed) {
2085 u8 old_retry_short, old_retry_long;
2086 u32 old_frag_threshold, old_rts_threshold;
81077e82 2087 u8 old_coverage_class;
b9a5f8ca
JM
2088
2089 if (!rdev->ops->set_wiphy_params) {
2090 result = -EOPNOTSUPP;
2091 goto bad_res;
2092 }
2093
2094 old_retry_short = rdev->wiphy.retry_short;
2095 old_retry_long = rdev->wiphy.retry_long;
2096 old_frag_threshold = rdev->wiphy.frag_threshold;
2097 old_rts_threshold = rdev->wiphy.rts_threshold;
81077e82 2098 old_coverage_class = rdev->wiphy.coverage_class;
b9a5f8ca
JM
2099
2100 if (changed & WIPHY_PARAM_RETRY_SHORT)
2101 rdev->wiphy.retry_short = retry_short;
2102 if (changed & WIPHY_PARAM_RETRY_LONG)
2103 rdev->wiphy.retry_long = retry_long;
2104 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2105 rdev->wiphy.frag_threshold = frag_threshold;
2106 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2107 rdev->wiphy.rts_threshold = rts_threshold;
81077e82
LT
2108 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2109 rdev->wiphy.coverage_class = coverage_class;
b9a5f8ca 2110
e35e4d28 2111 result = rdev_set_wiphy_params(rdev, changed);
b9a5f8ca
JM
2112 if (result) {
2113 rdev->wiphy.retry_short = old_retry_short;
2114 rdev->wiphy.retry_long = old_retry_long;
2115 rdev->wiphy.frag_threshold = old_frag_threshold;
2116 rdev->wiphy.rts_threshold = old_rts_threshold;
81077e82 2117 rdev->wiphy.coverage_class = old_coverage_class;
b9a5f8ca
JM
2118 }
2119 }
72bdcf34 2120
306d6112 2121 bad_res:
4bbf4d56 2122 mutex_unlock(&rdev->mtx);
f444de05
JB
2123 if (netdev)
2124 dev_put(netdev);
55682965
JB
2125 return result;
2126}
2127
71bbc994
JB
2128static inline u64 wdev_id(struct wireless_dev *wdev)
2129{
2130 return (u64)wdev->identifier |
2131 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2132}
55682965 2133
683b6d3b
JB
2134static int nl80211_send_chandef(struct sk_buff *msg,
2135 struct cfg80211_chan_def *chandef)
2136{
9f5e8f6e 2137 WARN_ON(!cfg80211_chandef_valid(chandef));
3d9d1d66 2138
683b6d3b
JB
2139 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2140 chandef->chan->center_freq))
2141 return -ENOBUFS;
3d9d1d66
JB
2142 switch (chandef->width) {
2143 case NL80211_CHAN_WIDTH_20_NOHT:
2144 case NL80211_CHAN_WIDTH_20:
2145 case NL80211_CHAN_WIDTH_40:
2146 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2147 cfg80211_get_chandef_type(chandef)))
2148 return -ENOBUFS;
2149 break;
2150 default:
2151 break;
2152 }
2153 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2154 return -ENOBUFS;
2155 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2156 return -ENOBUFS;
2157 if (chandef->center_freq2 &&
2158 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
683b6d3b
JB
2159 return -ENOBUFS;
2160 return 0;
2161}
2162
15e47304 2163static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
d726405a 2164 struct cfg80211_registered_device *rdev,
72fb2abc 2165 struct wireless_dev *wdev)
55682965 2166{
72fb2abc 2167 struct net_device *dev = wdev->netdev;
55682965
JB
2168 void *hdr;
2169
15e47304 2170 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
55682965
JB
2171 if (!hdr)
2172 return -1;
2173
72fb2abc
JB
2174 if (dev &&
2175 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
98104fde 2176 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
72fb2abc
JB
2177 goto nla_put_failure;
2178
2179 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2180 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
71bbc994 2181 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
98104fde 2182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
9360ffd1
DM
2183 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2184 rdev->devlist_generation ^
2185 (cfg80211_rdev_list_generation << 2)))
2186 goto nla_put_failure;
f5ea9120 2187
5b7ccaf3 2188 if (rdev->ops->get_channel) {
683b6d3b
JB
2189 int ret;
2190 struct cfg80211_chan_def chandef;
2191
2192 ret = rdev_get_channel(rdev, wdev, &chandef);
2193 if (ret == 0) {
2194 if (nl80211_send_chandef(msg, &chandef))
2195 goto nla_put_failure;
2196 }
d91df0e3
PF
2197 }
2198
b84e7a05
AQ
2199 if (wdev->ssid_len) {
2200 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2201 goto nla_put_failure;
2202 }
2203
55682965
JB
2204 return genlmsg_end(msg, hdr);
2205
2206 nla_put_failure:
bc3ed28c
TG
2207 genlmsg_cancel(msg, hdr);
2208 return -EMSGSIZE;
55682965
JB
2209}
2210
2211static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2212{
2213 int wp_idx = 0;
2214 int if_idx = 0;
2215 int wp_start = cb->args[0];
2216 int if_start = cb->args[1];
f5ea9120 2217 struct cfg80211_registered_device *rdev;
55682965
JB
2218 struct wireless_dev *wdev;
2219
a1794390 2220 mutex_lock(&cfg80211_mutex);
f5ea9120
JB
2221 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2222 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
463d0183 2223 continue;
bba95fef
JB
2224 if (wp_idx < wp_start) {
2225 wp_idx++;
55682965 2226 continue;
bba95fef 2227 }
55682965
JB
2228 if_idx = 0;
2229
f5ea9120 2230 mutex_lock(&rdev->devlist_mtx);
89a54e48 2231 list_for_each_entry(wdev, &rdev->wdev_list, list) {
bba95fef
JB
2232 if (if_idx < if_start) {
2233 if_idx++;
55682965 2234 continue;
bba95fef 2235 }
15e47304 2236 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
55682965 2237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
72fb2abc 2238 rdev, wdev) < 0) {
f5ea9120 2239 mutex_unlock(&rdev->devlist_mtx);
bba95fef
JB
2240 goto out;
2241 }
2242 if_idx++;
55682965 2243 }
f5ea9120 2244 mutex_unlock(&rdev->devlist_mtx);
bba95fef
JB
2245
2246 wp_idx++;
55682965 2247 }
bba95fef 2248 out:
a1794390 2249 mutex_unlock(&cfg80211_mutex);
55682965
JB
2250
2251 cb->args[0] = wp_idx;
2252 cb->args[1] = if_idx;
2253
2254 return skb->len;
2255}
2256
2257static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2258{
2259 struct sk_buff *msg;
4c476991 2260 struct cfg80211_registered_device *dev = info->user_ptr[0];
72fb2abc 2261 struct wireless_dev *wdev = info->user_ptr[1];
55682965 2262
fd2120ca 2263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
55682965 2264 if (!msg)
4c476991 2265 return -ENOMEM;
55682965 2266
15e47304 2267 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
72fb2abc 2268 dev, wdev) < 0) {
4c476991
JB
2269 nlmsg_free(msg);
2270 return -ENOBUFS;
2271 }
55682965 2272
134e6375 2273 return genlmsg_reply(msg, info);
55682965
JB
2274}
2275
66f7ac50
MW
2276static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2277 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2278 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2279 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2280 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2281 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2282};
2283
2284static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2285{
2286 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2287 int flag;
2288
2289 *mntrflags = 0;
2290
2291 if (!nla)
2292 return -EINVAL;
2293
2294 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2295 nla, mntr_flags_policy))
2296 return -EINVAL;
2297
2298 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2299 if (flags[flag])
2300 *mntrflags |= (1<<flag);
2301
2302 return 0;
2303}
2304
9bc383de 2305static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
ad4bb6f8
JB
2306 struct net_device *netdev, u8 use_4addr,
2307 enum nl80211_iftype iftype)
9bc383de 2308{
ad4bb6f8 2309 if (!use_4addr) {
f350a0a8 2310 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
ad4bb6f8 2311 return -EBUSY;
9bc383de 2312 return 0;
ad4bb6f8 2313 }
9bc383de
JB
2314
2315 switch (iftype) {
2316 case NL80211_IFTYPE_AP_VLAN:
2317 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2318 return 0;
2319 break;
2320 case NL80211_IFTYPE_STATION:
2321 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2322 return 0;
2323 break;
2324 default:
2325 break;
2326 }
2327
2328 return -EOPNOTSUPP;
2329}
2330
55682965
JB
2331static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2332{
4c476991 2333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 2334 struct vif_params params;
e36d56b6 2335 int err;
04a773ad 2336 enum nl80211_iftype otype, ntype;
4c476991 2337 struct net_device *dev = info->user_ptr[1];
92ffe055 2338 u32 _flags, *flags = NULL;
ac7f9cfa 2339 bool change = false;
55682965 2340
2ec600d6
LCC
2341 memset(&params, 0, sizeof(params));
2342
04a773ad 2343 otype = ntype = dev->ieee80211_ptr->iftype;
55682965 2344
723b038d 2345 if (info->attrs[NL80211_ATTR_IFTYPE]) {
ac7f9cfa 2346 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
04a773ad 2347 if (otype != ntype)
ac7f9cfa 2348 change = true;
4c476991
JB
2349 if (ntype > NL80211_IFTYPE_MAX)
2350 return -EINVAL;
723b038d
JB
2351 }
2352
92ffe055 2353 if (info->attrs[NL80211_ATTR_MESH_ID]) {
29cbe68c
JB
2354 struct wireless_dev *wdev = dev->ieee80211_ptr;
2355
4c476991
JB
2356 if (ntype != NL80211_IFTYPE_MESH_POINT)
2357 return -EINVAL;
29cbe68c
JB
2358 if (netif_running(dev))
2359 return -EBUSY;
2360
2361 wdev_lock(wdev);
2362 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2363 IEEE80211_MAX_MESH_ID_LEN);
2364 wdev->mesh_id_up_len =
2365 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2366 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2367 wdev->mesh_id_up_len);
2368 wdev_unlock(wdev);
2ec600d6
LCC
2369 }
2370
8b787643
FF
2371 if (info->attrs[NL80211_ATTR_4ADDR]) {
2372 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2373 change = true;
ad4bb6f8 2374 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
9bc383de 2375 if (err)
4c476991 2376 return err;
8b787643
FF
2377 } else {
2378 params.use_4addr = -1;
2379 }
2380
92ffe055 2381 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4c476991
JB
2382 if (ntype != NL80211_IFTYPE_MONITOR)
2383 return -EINVAL;
92ffe055
JB
2384 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2385 &_flags);
ac7f9cfa 2386 if (err)
4c476991 2387 return err;
ac7f9cfa
JB
2388
2389 flags = &_flags;
2390 change = true;
92ffe055 2391 }
3b85875a 2392
ac7f9cfa 2393 if (change)
3d54d255 2394 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
ac7f9cfa
JB
2395 else
2396 err = 0;
60719ffd 2397
9bc383de
JB
2398 if (!err && params.use_4addr != -1)
2399 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2400
55682965
JB
2401 return err;
2402}
2403
2404static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2405{
4c476991 2406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 2407 struct vif_params params;
84efbb84 2408 struct wireless_dev *wdev;
1c90f9d4 2409 struct sk_buff *msg;
55682965
JB
2410 int err;
2411 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
66f7ac50 2412 u32 flags;
55682965 2413
2ec600d6
LCC
2414 memset(&params, 0, sizeof(params));
2415
55682965
JB
2416 if (!info->attrs[NL80211_ATTR_IFNAME])
2417 return -EINVAL;
2418
2419 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2420 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2421 if (type > NL80211_IFTYPE_MAX)
2422 return -EINVAL;
2423 }
2424
79c97e97 2425 if (!rdev->ops->add_virtual_intf ||
4c476991
JB
2426 !(rdev->wiphy.interface_modes & (1 << type)))
2427 return -EOPNOTSUPP;
55682965 2428
1c18f145
AS
2429 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2430 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2431 ETH_ALEN);
2432 if (!is_valid_ether_addr(params.macaddr))
2433 return -EADDRNOTAVAIL;
2434 }
2435
9bc383de 2436 if (info->attrs[NL80211_ATTR_4ADDR]) {
8b787643 2437 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
ad4bb6f8 2438 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
9bc383de 2439 if (err)
4c476991 2440 return err;
9bc383de 2441 }
8b787643 2442
1c90f9d4
JB
2443 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2444 if (!msg)
2445 return -ENOMEM;
2446
66f7ac50
MW
2447 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2448 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2449 &flags);
e35e4d28
HG
2450 wdev = rdev_add_virtual_intf(rdev,
2451 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2452 type, err ? NULL : &flags, &params);
1c90f9d4
JB
2453 if (IS_ERR(wdev)) {
2454 nlmsg_free(msg);
84efbb84 2455 return PTR_ERR(wdev);
1c90f9d4 2456 }
2ec600d6 2457
98104fde
JB
2458 switch (type) {
2459 case NL80211_IFTYPE_MESH_POINT:
2460 if (!info->attrs[NL80211_ATTR_MESH_ID])
2461 break;
29cbe68c
JB
2462 wdev_lock(wdev);
2463 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2464 IEEE80211_MAX_MESH_ID_LEN);
2465 wdev->mesh_id_up_len =
2466 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2467 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2468 wdev->mesh_id_up_len);
2469 wdev_unlock(wdev);
98104fde
JB
2470 break;
2471 case NL80211_IFTYPE_P2P_DEVICE:
2472 /*
2473 * P2P Device doesn't have a netdev, so doesn't go
2474 * through the netdev notifier and must be added here
2475 */
2476 mutex_init(&wdev->mtx);
2477 INIT_LIST_HEAD(&wdev->event_list);
2478 spin_lock_init(&wdev->event_lock);
2479 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2480 spin_lock_init(&wdev->mgmt_registrations_lock);
2481
2482 mutex_lock(&rdev->devlist_mtx);
2483 wdev->identifier = ++rdev->wdev_id;
2484 list_add_rcu(&wdev->list, &rdev->wdev_list);
2485 rdev->devlist_generation++;
2486 mutex_unlock(&rdev->devlist_mtx);
2487 break;
2488 default:
2489 break;
29cbe68c
JB
2490 }
2491
15e47304 2492 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
1c90f9d4
JB
2493 rdev, wdev) < 0) {
2494 nlmsg_free(msg);
2495 return -ENOBUFS;
2496 }
2497
2498 return genlmsg_reply(msg, info);
55682965
JB
2499}
2500
2501static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2502{
4c476991 2503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
84efbb84 2504 struct wireless_dev *wdev = info->user_ptr[1];
55682965 2505
4c476991
JB
2506 if (!rdev->ops->del_virtual_intf)
2507 return -EOPNOTSUPP;
55682965 2508
84efbb84
JB
2509 /*
2510 * If we remove a wireless device without a netdev then clear
2511 * user_ptr[1] so that nl80211_post_doit won't dereference it
2512 * to check if it needs to do dev_put(). Otherwise it crashes
2513 * since the wdev has been freed, unlike with a netdev where
2514 * we need the dev_put() for the netdev to really be freed.
2515 */
2516 if (!wdev->netdev)
2517 info->user_ptr[1] = NULL;
2518
e35e4d28 2519 return rdev_del_virtual_intf(rdev, wdev);
55682965
JB
2520}
2521
1d9d9213
SW
2522static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2523{
2524 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2525 struct net_device *dev = info->user_ptr[1];
2526 u16 noack_map;
2527
2528 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2529 return -EINVAL;
2530
2531 if (!rdev->ops->set_noack_map)
2532 return -EOPNOTSUPP;
2533
2534 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2535
e35e4d28 2536 return rdev_set_noack_map(rdev, dev, noack_map);
1d9d9213
SW
2537}
2538
41ade00f
JB
2539struct get_key_cookie {
2540 struct sk_buff *msg;
2541 int error;
b9454e83 2542 int idx;
41ade00f
JB
2543};
2544
2545static void get_key_callback(void *c, struct key_params *params)
2546{
b9454e83 2547 struct nlattr *key;
41ade00f
JB
2548 struct get_key_cookie *cookie = c;
2549
9360ffd1
DM
2550 if ((params->key &&
2551 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2552 params->key_len, params->key)) ||
2553 (params->seq &&
2554 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2555 params->seq_len, params->seq)) ||
2556 (params->cipher &&
2557 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2558 params->cipher)))
2559 goto nla_put_failure;
41ade00f 2560
b9454e83
JB
2561 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2562 if (!key)
2563 goto nla_put_failure;
2564
9360ffd1
DM
2565 if ((params->key &&
2566 nla_put(cookie->msg, NL80211_KEY_DATA,
2567 params->key_len, params->key)) ||
2568 (params->seq &&
2569 nla_put(cookie->msg, NL80211_KEY_SEQ,
2570 params->seq_len, params->seq)) ||
2571 (params->cipher &&
2572 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2573 params->cipher)))
2574 goto nla_put_failure;
b9454e83 2575
9360ffd1
DM
2576 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2577 goto nla_put_failure;
b9454e83
JB
2578
2579 nla_nest_end(cookie->msg, key);
2580
41ade00f
JB
2581 return;
2582 nla_put_failure:
2583 cookie->error = 1;
2584}
2585
2586static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2587{
4c476991 2588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
41ade00f 2589 int err;
4c476991 2590 struct net_device *dev = info->user_ptr[1];
41ade00f 2591 u8 key_idx = 0;
e31b8213
JB
2592 const u8 *mac_addr = NULL;
2593 bool pairwise;
41ade00f
JB
2594 struct get_key_cookie cookie = {
2595 .error = 0,
2596 };
2597 void *hdr;
2598 struct sk_buff *msg;
2599
2600 if (info->attrs[NL80211_ATTR_KEY_IDX])
2601 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2602
3cfcf6ac 2603 if (key_idx > 5)
41ade00f
JB
2604 return -EINVAL;
2605
2606 if (info->attrs[NL80211_ATTR_MAC])
2607 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2608
e31b8213
JB
2609 pairwise = !!mac_addr;
2610 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2611 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2612 if (kt >= NUM_NL80211_KEYTYPES)
2613 return -EINVAL;
2614 if (kt != NL80211_KEYTYPE_GROUP &&
2615 kt != NL80211_KEYTYPE_PAIRWISE)
2616 return -EINVAL;
2617 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2618 }
2619
4c476991
JB
2620 if (!rdev->ops->get_key)
2621 return -EOPNOTSUPP;
41ade00f 2622
fd2120ca 2623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
2624 if (!msg)
2625 return -ENOMEM;
41ade00f 2626
15e47304 2627 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
41ade00f 2628 NL80211_CMD_NEW_KEY);
4c476991
JB
2629 if (IS_ERR(hdr))
2630 return PTR_ERR(hdr);
41ade00f
JB
2631
2632 cookie.msg = msg;
b9454e83 2633 cookie.idx = key_idx;
41ade00f 2634
9360ffd1
DM
2635 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2636 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2637 goto nla_put_failure;
2638 if (mac_addr &&
2639 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2640 goto nla_put_failure;
41ade00f 2641
e31b8213
JB
2642 if (pairwise && mac_addr &&
2643 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2644 return -ENOENT;
2645
e35e4d28
HG
2646 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2647 get_key_callback);
41ade00f
JB
2648
2649 if (err)
6c95e2a2 2650 goto free_msg;
41ade00f
JB
2651
2652 if (cookie.error)
2653 goto nla_put_failure;
2654
2655 genlmsg_end(msg, hdr);
4c476991 2656 return genlmsg_reply(msg, info);
41ade00f
JB
2657
2658 nla_put_failure:
2659 err = -ENOBUFS;
6c95e2a2 2660 free_msg:
41ade00f 2661 nlmsg_free(msg);
41ade00f
JB
2662 return err;
2663}
2664
2665static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2666{
4c476991 2667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
b9454e83 2668 struct key_parse key;
41ade00f 2669 int err;
4c476991 2670 struct net_device *dev = info->user_ptr[1];
41ade00f 2671
b9454e83
JB
2672 err = nl80211_parse_key(info, &key);
2673 if (err)
2674 return err;
41ade00f 2675
b9454e83 2676 if (key.idx < 0)
41ade00f
JB
2677 return -EINVAL;
2678
b9454e83
JB
2679 /* only support setting default key */
2680 if (!key.def && !key.defmgmt)
41ade00f
JB
2681 return -EINVAL;
2682
dbd2fd65 2683 wdev_lock(dev->ieee80211_ptr);
3cfcf6ac 2684
dbd2fd65
JB
2685 if (key.def) {
2686 if (!rdev->ops->set_default_key) {
2687 err = -EOPNOTSUPP;
2688 goto out;
2689 }
41ade00f 2690
dbd2fd65
JB
2691 err = nl80211_key_allowed(dev->ieee80211_ptr);
2692 if (err)
2693 goto out;
2694
e35e4d28 2695 err = rdev_set_default_key(rdev, dev, key.idx,
dbd2fd65
JB
2696 key.def_uni, key.def_multi);
2697
2698 if (err)
2699 goto out;
fffd0934 2700
3d23e349 2701#ifdef CONFIG_CFG80211_WEXT
dbd2fd65
JB
2702 dev->ieee80211_ptr->wext.default_key = key.idx;
2703#endif
2704 } else {
2705 if (key.def_uni || !key.def_multi) {
2706 err = -EINVAL;
2707 goto out;
2708 }
2709
2710 if (!rdev->ops->set_default_mgmt_key) {
2711 err = -EOPNOTSUPP;
2712 goto out;
2713 }
2714
2715 err = nl80211_key_allowed(dev->ieee80211_ptr);
2716 if (err)
2717 goto out;
2718
e35e4d28 2719 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
dbd2fd65
JB
2720 if (err)
2721 goto out;
2722
2723#ifdef CONFIG_CFG80211_WEXT
2724 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
08645126 2725#endif
dbd2fd65
JB
2726 }
2727
2728 out:
fffd0934 2729 wdev_unlock(dev->ieee80211_ptr);
41ade00f 2730
41ade00f
JB
2731 return err;
2732}
2733
2734static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2735{
4c476991 2736 struct cfg80211_registered_device *rdev = info->user_ptr[0];
fffd0934 2737 int err;
4c476991 2738 struct net_device *dev = info->user_ptr[1];
b9454e83 2739 struct key_parse key;
e31b8213 2740 const u8 *mac_addr = NULL;
41ade00f 2741
b9454e83
JB
2742 err = nl80211_parse_key(info, &key);
2743 if (err)
2744 return err;
41ade00f 2745
b9454e83 2746 if (!key.p.key)
41ade00f
JB
2747 return -EINVAL;
2748
41ade00f
JB
2749 if (info->attrs[NL80211_ATTR_MAC])
2750 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2751
e31b8213
JB
2752 if (key.type == -1) {
2753 if (mac_addr)
2754 key.type = NL80211_KEYTYPE_PAIRWISE;
2755 else
2756 key.type = NL80211_KEYTYPE_GROUP;
2757 }
2758
2759 /* for now */
2760 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2761 key.type != NL80211_KEYTYPE_GROUP)
2762 return -EINVAL;
2763
4c476991
JB
2764 if (!rdev->ops->add_key)
2765 return -EOPNOTSUPP;
25e47c18 2766
e31b8213
JB
2767 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2768 key.type == NL80211_KEYTYPE_PAIRWISE,
2769 mac_addr))
4c476991 2770 return -EINVAL;
41ade00f 2771
fffd0934
JB
2772 wdev_lock(dev->ieee80211_ptr);
2773 err = nl80211_key_allowed(dev->ieee80211_ptr);
2774 if (!err)
e35e4d28
HG
2775 err = rdev_add_key(rdev, dev, key.idx,
2776 key.type == NL80211_KEYTYPE_PAIRWISE,
2777 mac_addr, &key.p);
fffd0934 2778 wdev_unlock(dev->ieee80211_ptr);
41ade00f 2779
41ade00f
JB
2780 return err;
2781}
2782
2783static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2784{
4c476991 2785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
41ade00f 2786 int err;
4c476991 2787 struct net_device *dev = info->user_ptr[1];
41ade00f 2788 u8 *mac_addr = NULL;
b9454e83 2789 struct key_parse key;
41ade00f 2790
b9454e83
JB
2791 err = nl80211_parse_key(info, &key);
2792 if (err)
2793 return err;
41ade00f
JB
2794
2795 if (info->attrs[NL80211_ATTR_MAC])
2796 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2797
e31b8213
JB
2798 if (key.type == -1) {
2799 if (mac_addr)
2800 key.type = NL80211_KEYTYPE_PAIRWISE;
2801 else
2802 key.type = NL80211_KEYTYPE_GROUP;
2803 }
2804
2805 /* for now */
2806 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2807 key.type != NL80211_KEYTYPE_GROUP)
2808 return -EINVAL;
2809
4c476991
JB
2810 if (!rdev->ops->del_key)
2811 return -EOPNOTSUPP;
41ade00f 2812
fffd0934
JB
2813 wdev_lock(dev->ieee80211_ptr);
2814 err = nl80211_key_allowed(dev->ieee80211_ptr);
e31b8213
JB
2815
2816 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2817 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2818 err = -ENOENT;
2819
fffd0934 2820 if (!err)
e35e4d28
HG
2821 err = rdev_del_key(rdev, dev, key.idx,
2822 key.type == NL80211_KEYTYPE_PAIRWISE,
2823 mac_addr);
41ade00f 2824
3d23e349 2825#ifdef CONFIG_CFG80211_WEXT
08645126 2826 if (!err) {
b9454e83 2827 if (key.idx == dev->ieee80211_ptr->wext.default_key)
08645126 2828 dev->ieee80211_ptr->wext.default_key = -1;
b9454e83 2829 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
08645126
JB
2830 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2831 }
2832#endif
fffd0934 2833 wdev_unlock(dev->ieee80211_ptr);
08645126 2834
41ade00f
JB
2835 return err;
2836}
2837
77765eaf
VT
2838/* This function returns an error or the number of nested attributes */
2839static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2840{
2841 struct nlattr *attr;
2842 int n_entries = 0, tmp;
2843
2844 nla_for_each_nested(attr, nl_attr, tmp) {
2845 if (nla_len(attr) != ETH_ALEN)
2846 return -EINVAL;
2847
2848 n_entries++;
2849 }
2850
2851 return n_entries;
2852}
2853
2854/*
2855 * This function parses ACL information and allocates memory for ACL data.
2856 * On successful return, the calling function is responsible to free the
2857 * ACL buffer returned by this function.
2858 */
2859static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2860 struct genl_info *info)
2861{
2862 enum nl80211_acl_policy acl_policy;
2863 struct nlattr *attr;
2864 struct cfg80211_acl_data *acl;
2865 int i = 0, n_entries, tmp;
2866
2867 if (!wiphy->max_acl_mac_addrs)
2868 return ERR_PTR(-EOPNOTSUPP);
2869
2870 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2871 return ERR_PTR(-EINVAL);
2872
2873 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2874 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2875 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2876 return ERR_PTR(-EINVAL);
2877
2878 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2879 return ERR_PTR(-EINVAL);
2880
2881 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2882 if (n_entries < 0)
2883 return ERR_PTR(n_entries);
2884
2885 if (n_entries > wiphy->max_acl_mac_addrs)
2886 return ERR_PTR(-ENOTSUPP);
2887
2888 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2889 GFP_KERNEL);
2890 if (!acl)
2891 return ERR_PTR(-ENOMEM);
2892
2893 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2894 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2895 i++;
2896 }
2897
2898 acl->n_acl_entries = n_entries;
2899 acl->acl_policy = acl_policy;
2900
2901 return acl;
2902}
2903
2904static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2905{
2906 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2907 struct net_device *dev = info->user_ptr[1];
2908 struct cfg80211_acl_data *acl;
2909 int err;
2910
2911 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2912 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2913 return -EOPNOTSUPP;
2914
2915 if (!dev->ieee80211_ptr->beacon_interval)
2916 return -EINVAL;
2917
2918 acl = parse_acl_data(&rdev->wiphy, info);
2919 if (IS_ERR(acl))
2920 return PTR_ERR(acl);
2921
2922 err = rdev_set_mac_acl(rdev, dev, acl);
2923
2924 kfree(acl);
2925
2926 return err;
2927}
2928
8860020e
JB
2929static int nl80211_parse_beacon(struct genl_info *info,
2930 struct cfg80211_beacon_data *bcn)
ed1b6cc7 2931{
8860020e 2932 bool haveinfo = false;
ed1b6cc7 2933
9946ecfb
JM
2934 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2935 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2936 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2937 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
f4a11bb0
JB
2938 return -EINVAL;
2939
8860020e 2940 memset(bcn, 0, sizeof(*bcn));
ed1b6cc7 2941
ed1b6cc7 2942 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
8860020e
JB
2943 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2944 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2945 if (!bcn->head_len)
2946 return -EINVAL;
2947 haveinfo = true;
ed1b6cc7
JB
2948 }
2949
2950 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
8860020e
JB
2951 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2952 bcn->tail_len =
ed1b6cc7 2953 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
8860020e 2954 haveinfo = true;
ed1b6cc7
JB
2955 }
2956
4c476991
JB
2957 if (!haveinfo)
2958 return -EINVAL;
3b85875a 2959
9946ecfb 2960 if (info->attrs[NL80211_ATTR_IE]) {
8860020e
JB
2961 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2962 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9946ecfb
JM
2963 }
2964
2965 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
8860020e 2966 bcn->proberesp_ies =
9946ecfb 2967 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
8860020e 2968 bcn->proberesp_ies_len =
9946ecfb
JM
2969 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2970 }
2971
2972 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
8860020e 2973 bcn->assocresp_ies =
9946ecfb 2974 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
8860020e 2975 bcn->assocresp_ies_len =
9946ecfb
JM
2976 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2977 }
2978
00f740e1 2979 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
8860020e 2980 bcn->probe_resp =
00f740e1 2981 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
8860020e 2982 bcn->probe_resp_len =
00f740e1
AN
2983 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2984 }
2985
8860020e
JB
2986 return 0;
2987}
2988
46c1dd0c
FF
2989static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2990 struct cfg80211_ap_settings *params)
2991{
2992 struct wireless_dev *wdev;
2993 bool ret = false;
2994
2995 mutex_lock(&rdev->devlist_mtx);
2996
89a54e48 2997 list_for_each_entry(wdev, &rdev->wdev_list, list) {
46c1dd0c
FF
2998 if (wdev->iftype != NL80211_IFTYPE_AP &&
2999 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3000 continue;
3001
683b6d3b 3002 if (!wdev->preset_chandef.chan)
46c1dd0c
FF
3003 continue;
3004
683b6d3b 3005 params->chandef = wdev->preset_chandef;
46c1dd0c
FF
3006 ret = true;
3007 break;
3008 }
3009
3010 mutex_unlock(&rdev->devlist_mtx);
3011
3012 return ret;
3013}
3014
e39e5b5e
JM
3015static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3016 enum nl80211_auth_type auth_type,
3017 enum nl80211_commands cmd)
3018{
3019 if (auth_type > NL80211_AUTHTYPE_MAX)
3020 return false;
3021
3022 switch (cmd) {
3023 case NL80211_CMD_AUTHENTICATE:
3024 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3025 auth_type == NL80211_AUTHTYPE_SAE)
3026 return false;
3027 return true;
3028 case NL80211_CMD_CONNECT:
3029 case NL80211_CMD_START_AP:
3030 /* SAE not supported yet */
3031 if (auth_type == NL80211_AUTHTYPE_SAE)
3032 return false;
3033 return true;
3034 default:
3035 return false;
3036 }
3037}
3038
8860020e
JB
3039static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3040{
3041 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3042 struct net_device *dev = info->user_ptr[1];
3043 struct wireless_dev *wdev = dev->ieee80211_ptr;
3044 struct cfg80211_ap_settings params;
3045 int err;
04f39047 3046 u8 radar_detect_width = 0;
8860020e
JB
3047
3048 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3050 return -EOPNOTSUPP;
3051
3052 if (!rdev->ops->start_ap)
3053 return -EOPNOTSUPP;
3054
3055 if (wdev->beacon_interval)
3056 return -EALREADY;
3057
3058 memset(&params, 0, sizeof(params));
3059
3060 /* these are required for START_AP */
3061 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3062 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3063 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3064 return -EINVAL;
3065
3066 err = nl80211_parse_beacon(info, &params.beacon);
3067 if (err)
3068 return err;
3069
3070 params.beacon_interval =
3071 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3072 params.dtim_period =
3073 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3074
3075 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3076 if (err)
3077 return err;
3078
3079 /*
3080 * In theory, some of these attributes should be required here
3081 * but since they were not used when the command was originally
3082 * added, keep them optional for old user space programs to let
3083 * them continue to work with drivers that do not need the
3084 * additional information -- drivers must check!
3085 */
3086 if (info->attrs[NL80211_ATTR_SSID]) {
3087 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3088 params.ssid_len =
3089 nla_len(info->attrs[NL80211_ATTR_SSID]);
3090 if (params.ssid_len == 0 ||
3091 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3092 return -EINVAL;
3093 }
3094
3095 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3096 params.hidden_ssid = nla_get_u32(
3097 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3098 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3099 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3100 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3101 return -EINVAL;
3102 }
3103
3104 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3105
3106 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3107 params.auth_type = nla_get_u32(
3108 info->attrs[NL80211_ATTR_AUTH_TYPE]);
e39e5b5e
JM
3109 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3110 NL80211_CMD_START_AP))
8860020e
JB
3111 return -EINVAL;
3112 } else
3113 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3114
3115 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3116 NL80211_MAX_NR_CIPHER_SUITES);
3117 if (err)
3118 return err;
3119
1b658f11
VT
3120 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3121 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3122 return -EOPNOTSUPP;
3123 params.inactivity_timeout = nla_get_u16(
3124 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3125 }
3126
53cabad7
JB
3127 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3129 return -EINVAL;
3130 params.p2p_ctwindow =
3131 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3132 if (params.p2p_ctwindow > 127)
3133 return -EINVAL;
3134 if (params.p2p_ctwindow != 0 &&
3135 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3136 return -EINVAL;
3137 }
3138
3139 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3140 u8 tmp;
3141
3142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3143 return -EINVAL;
3144 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3145 if (tmp > 1)
3146 return -EINVAL;
3147 params.p2p_opp_ps = tmp;
3148 if (params.p2p_opp_ps != 0 &&
3149 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3150 return -EINVAL;
3151 }
3152
aa430da4 3153 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
683b6d3b
JB
3154 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3155 if (err)
3156 return err;
3157 } else if (wdev->preset_chandef.chan) {
3158 params.chandef = wdev->preset_chandef;
46c1dd0c 3159 } else if (!nl80211_get_ap_channel(rdev, &params))
aa430da4
JB
3160 return -EINVAL;
3161
683b6d3b 3162 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
aa430da4
JB
3163 return -EINVAL;
3164
04f39047
SW
3165 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3166 if (err < 0)
3167 return err;
3168 if (err) {
3169 radar_detect_width = BIT(params.chandef.width);
3170 params.radar_required = true;
3171 }
3172
e4e32459 3173 mutex_lock(&rdev->devlist_mtx);
04f39047
SW
3174 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3175 params.chandef.chan,
3176 CHAN_MODE_SHARED,
3177 radar_detect_width);
e4e32459
MK
3178 mutex_unlock(&rdev->devlist_mtx);
3179
3180 if (err)
3181 return err;
3182
77765eaf
VT
3183 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3184 params.acl = parse_acl_data(&rdev->wiphy, info);
3185 if (IS_ERR(params.acl))
3186 return PTR_ERR(params.acl);
3187 }
3188
e35e4d28 3189 err = rdev_start_ap(rdev, dev, &params);
46c1dd0c 3190 if (!err) {
683b6d3b 3191 wdev->preset_chandef = params.chandef;
8860020e 3192 wdev->beacon_interval = params.beacon_interval;
683b6d3b 3193 wdev->channel = params.chandef.chan;
06e191e2
AQ
3194 wdev->ssid_len = params.ssid_len;
3195 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
46c1dd0c 3196 }
77765eaf
VT
3197
3198 kfree(params.acl);
3199
56d1893d 3200 return err;
ed1b6cc7
JB
3201}
3202
8860020e
JB
3203static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3204{
3205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3206 struct net_device *dev = info->user_ptr[1];
3207 struct wireless_dev *wdev = dev->ieee80211_ptr;
3208 struct cfg80211_beacon_data params;
3209 int err;
3210
3211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3213 return -EOPNOTSUPP;
3214
3215 if (!rdev->ops->change_beacon)
3216 return -EOPNOTSUPP;
3217
3218 if (!wdev->beacon_interval)
3219 return -EINVAL;
3220
3221 err = nl80211_parse_beacon(info, &params);
3222 if (err)
3223 return err;
3224
e35e4d28 3225 return rdev_change_beacon(rdev, dev, &params);
8860020e
JB
3226}
3227
3228static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
ed1b6cc7 3229{
4c476991
JB
3230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3231 struct net_device *dev = info->user_ptr[1];
ed1b6cc7 3232
60771780 3233 return cfg80211_stop_ap(rdev, dev);
ed1b6cc7
JB
3234}
3235
5727ef1b
JB
3236static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3237 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3238 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3239 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
0e46724a 3240 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
b39c48fa 3241 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
d83023da 3242 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
5727ef1b
JB
3243};
3244
eccb8e8f 3245static int parse_station_flags(struct genl_info *info,
bdd3ae3d 3246 enum nl80211_iftype iftype,
eccb8e8f 3247 struct station_parameters *params)
5727ef1b
JB
3248{
3249 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
eccb8e8f 3250 struct nlattr *nla;
5727ef1b
JB
3251 int flag;
3252
eccb8e8f
JB
3253 /*
3254 * Try parsing the new attribute first so userspace
3255 * can specify both for older kernels.
3256 */
3257 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3258 if (nla) {
3259 struct nl80211_sta_flag_update *sta_flags;
3260
3261 sta_flags = nla_data(nla);
3262 params->sta_flags_mask = sta_flags->mask;
3263 params->sta_flags_set = sta_flags->set;
77ee7c89 3264 params->sta_flags_set &= params->sta_flags_mask;
eccb8e8f
JB
3265 if ((params->sta_flags_mask |
3266 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3267 return -EINVAL;
3268 return 0;
3269 }
3270
3271 /* if present, parse the old attribute */
5727ef1b 3272
eccb8e8f 3273 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
5727ef1b
JB
3274 if (!nla)
3275 return 0;
3276
3277 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3278 nla, sta_flags_policy))
3279 return -EINVAL;
3280
bdd3ae3d
JB
3281 /*
3282 * Only allow certain flags for interface types so that
3283 * other attributes are silently ignored. Remember that
3284 * this is backward compatibility code with old userspace
3285 * and shouldn't be hit in other cases anyway.
3286 */
3287 switch (iftype) {
3288 case NL80211_IFTYPE_AP:
3289 case NL80211_IFTYPE_AP_VLAN:
3290 case NL80211_IFTYPE_P2P_GO:
3291 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3292 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3293 BIT(NL80211_STA_FLAG_WME) |
3294 BIT(NL80211_STA_FLAG_MFP);
3295 break;
3296 case NL80211_IFTYPE_P2P_CLIENT:
3297 case NL80211_IFTYPE_STATION:
3298 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3299 BIT(NL80211_STA_FLAG_TDLS_PEER);
3300 break;
3301 case NL80211_IFTYPE_MESH_POINT:
3302 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3303 BIT(NL80211_STA_FLAG_MFP) |
3304 BIT(NL80211_STA_FLAG_AUTHORIZED);
3305 default:
3306 return -EINVAL;
3307 }
5727ef1b 3308
3383b5a6
JB
3309 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3310 if (flags[flag]) {
eccb8e8f 3311 params->sta_flags_set |= (1<<flag);
5727ef1b 3312
3383b5a6
JB
3313 /* no longer support new API additions in old API */
3314 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3315 return -EINVAL;
3316 }
3317 }
3318
5727ef1b
JB
3319 return 0;
3320}
3321
c8dcfd8a
FF
3322static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3323 int attr)
3324{
3325 struct nlattr *rate;
8eb41c8d
VK
3326 u32 bitrate;
3327 u16 bitrate_compat;
c8dcfd8a
FF
3328
3329 rate = nla_nest_start(msg, attr);
3330 if (!rate)
db9c64cf 3331 return false;
c8dcfd8a
FF
3332
3333 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3334 bitrate = cfg80211_calculate_bitrate(info);
8eb41c8d
VK
3335 /* report 16-bit bitrate only if we can */
3336 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
db9c64cf
JB
3337 if (bitrate > 0 &&
3338 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3339 return false;
3340 if (bitrate_compat > 0 &&
3341 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3342 return false;
3343
3344 if (info->flags & RATE_INFO_FLAGS_MCS) {
3345 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3346 return false;
3347 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3348 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3349 return false;
3350 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3351 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3352 return false;
3353 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3354 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3355 return false;
3356 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3357 return false;
3358 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3359 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3360 return false;
3361 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3362 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3363 return false;
3364 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3365 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3366 return false;
3367 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3368 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3369 return false;
3370 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3371 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3372 return false;
3373 }
c8dcfd8a
FF
3374
3375 nla_nest_end(msg, rate);
3376 return true;
c8dcfd8a
FF
3377}
3378
119363c7
FF
3379static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3380 int id)
3381{
3382 void *attr;
3383 int i = 0;
3384
3385 if (!mask)
3386 return true;
3387
3388 attr = nla_nest_start(msg, id);
3389 if (!attr)
3390 return false;
3391
3392 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3393 if (!(mask & BIT(i)))
3394 continue;
3395
3396 if (nla_put_u8(msg, i, signal[i]))
3397 return false;
3398 }
3399
3400 nla_nest_end(msg, attr);
3401
3402 return true;
3403}
3404
15e47304 3405static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
66266b3a
JL
3406 int flags,
3407 struct cfg80211_registered_device *rdev,
3408 struct net_device *dev,
98b62183 3409 const u8 *mac_addr, struct station_info *sinfo)
fd5b74dc
JB
3410{
3411 void *hdr;
f4263c98 3412 struct nlattr *sinfoattr, *bss_param;
fd5b74dc 3413
15e47304 3414 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
fd5b74dc
JB
3415 if (!hdr)
3416 return -1;
3417
9360ffd1
DM
3418 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3419 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3420 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3421 goto nla_put_failure;
f5ea9120 3422
2ec600d6
LCC
3423 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3424 if (!sinfoattr)
fd5b74dc 3425 goto nla_put_failure;
9360ffd1
DM
3426 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3427 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3428 sinfo->connected_time))
3429 goto nla_put_failure;
3430 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3431 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3432 sinfo->inactive_time))
3433 goto nla_put_failure;
42745e03
VK
3434 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3435 STATION_INFO_RX_BYTES64)) &&
9360ffd1 3436 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
42745e03 3437 (u32)sinfo->rx_bytes))
9360ffd1 3438 goto nla_put_failure;
42745e03
VK
3439 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
3440 NL80211_STA_INFO_TX_BYTES64)) &&
9360ffd1 3441 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
42745e03
VK
3442 (u32)sinfo->tx_bytes))
3443 goto nla_put_failure;
3444 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3445 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
3446 sinfo->rx_bytes))
3447 goto nla_put_failure;
3448 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3449 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
9360ffd1
DM
3450 sinfo->tx_bytes))
3451 goto nla_put_failure;
3452 if ((sinfo->filled & STATION_INFO_LLID) &&
3453 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3454 goto nla_put_failure;
3455 if ((sinfo->filled & STATION_INFO_PLID) &&
3456 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3457 goto nla_put_failure;
3458 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3459 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3460 sinfo->plink_state))
3461 goto nla_put_failure;
66266b3a
JL
3462 switch (rdev->wiphy.signal_type) {
3463 case CFG80211_SIGNAL_TYPE_MBM:
9360ffd1
DM
3464 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3465 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3466 sinfo->signal))
3467 goto nla_put_failure;
3468 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3469 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3470 sinfo->signal_avg))
3471 goto nla_put_failure;
66266b3a
JL
3472 break;
3473 default:
3474 break;
3475 }
119363c7
FF
3476 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3477 if (!nl80211_put_signal(msg, sinfo->chains,
3478 sinfo->chain_signal,
3479 NL80211_STA_INFO_CHAIN_SIGNAL))
3480 goto nla_put_failure;
3481 }
3482 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3483 if (!nl80211_put_signal(msg, sinfo->chains,
3484 sinfo->chain_signal_avg,
3485 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3486 goto nla_put_failure;
3487 }
420e7fab 3488 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
c8dcfd8a
FF
3489 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3490 NL80211_STA_INFO_TX_BITRATE))
3491 goto nla_put_failure;
3492 }
3493 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3494 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3495 NL80211_STA_INFO_RX_BITRATE))
420e7fab 3496 goto nla_put_failure;
420e7fab 3497 }
9360ffd1
DM
3498 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3499 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3500 sinfo->rx_packets))
3501 goto nla_put_failure;
3502 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3503 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3504 sinfo->tx_packets))
3505 goto nla_put_failure;
3506 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3507 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3508 sinfo->tx_retries))
3509 goto nla_put_failure;
3510 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3511 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3512 sinfo->tx_failed))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3515 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3516 sinfo->beacon_loss_count))
3517 goto nla_put_failure;
3b1c5a53
MP
3518 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3519 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3520 sinfo->local_pm))
3521 goto nla_put_failure;
3522 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3523 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3524 sinfo->peer_pm))
3525 goto nla_put_failure;
3526 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3527 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3528 sinfo->nonpeer_pm))
3529 goto nla_put_failure;
f4263c98
PS
3530 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3531 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3532 if (!bss_param)
3533 goto nla_put_failure;
3534
9360ffd1
DM
3535 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3536 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3537 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3538 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3539 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3540 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3541 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3542 sinfo->bss_param.dtim_period) ||
3543 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3544 sinfo->bss_param.beacon_interval))
3545 goto nla_put_failure;
f4263c98
PS
3546
3547 nla_nest_end(msg, bss_param);
3548 }
9360ffd1
DM
3549 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3550 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3551 sizeof(struct nl80211_sta_flag_update),
3552 &sinfo->sta_flags))
3553 goto nla_put_failure;
7eab0f64
JL
3554 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3555 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3556 sinfo->t_offset))
3557 goto nla_put_failure;
2ec600d6 3558 nla_nest_end(msg, sinfoattr);
fd5b74dc 3559
9360ffd1
DM
3560 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3561 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3562 sinfo->assoc_req_ies))
3563 goto nla_put_failure;
50d3dfb7 3564
fd5b74dc
JB
3565 return genlmsg_end(msg, hdr);
3566
3567 nla_put_failure:
bc3ed28c
TG
3568 genlmsg_cancel(msg, hdr);
3569 return -EMSGSIZE;
fd5b74dc
JB
3570}
3571
2ec600d6 3572static int nl80211_dump_station(struct sk_buff *skb,
bba95fef 3573 struct netlink_callback *cb)
2ec600d6 3574{
2ec600d6
LCC
3575 struct station_info sinfo;
3576 struct cfg80211_registered_device *dev;
97990a06 3577 struct wireless_dev *wdev;
2ec600d6 3578 u8 mac_addr[ETH_ALEN];
97990a06 3579 int sta_idx = cb->args[2];
2ec600d6 3580 int err;
2ec600d6 3581
97990a06 3582 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
67748893
JB
3583 if (err)
3584 return err;
bba95fef 3585
97990a06
JB
3586 if (!wdev->netdev) {
3587 err = -EINVAL;
3588 goto out_err;
3589 }
3590
bba95fef 3591 if (!dev->ops->dump_station) {
eec60b03 3592 err = -EOPNOTSUPP;
bba95fef
JB
3593 goto out_err;
3594 }
3595
bba95fef 3596 while (1) {
f612cedf 3597 memset(&sinfo, 0, sizeof(sinfo));
97990a06 3598 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
e35e4d28 3599 mac_addr, &sinfo);
bba95fef
JB
3600 if (err == -ENOENT)
3601 break;
3602 if (err)
3b85875a 3603 goto out_err;
bba95fef
JB
3604
3605 if (nl80211_send_station(skb,
15e47304 3606 NETLINK_CB(cb->skb).portid,
bba95fef 3607 cb->nlh->nlmsg_seq, NLM_F_MULTI,
97990a06 3608 dev, wdev->netdev, mac_addr,
bba95fef
JB
3609 &sinfo) < 0)
3610 goto out;
3611
3612 sta_idx++;
3613 }
3614
3615
3616 out:
97990a06 3617 cb->args[2] = sta_idx;
bba95fef 3618 err = skb->len;
bba95fef 3619 out_err:
97990a06 3620 nl80211_finish_wdev_dump(dev);
bba95fef
JB
3621
3622 return err;
2ec600d6 3623}
fd5b74dc 3624
5727ef1b
JB
3625static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3626{
4c476991
JB
3627 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3628 struct net_device *dev = info->user_ptr[1];
2ec600d6 3629 struct station_info sinfo;
fd5b74dc
JB
3630 struct sk_buff *msg;
3631 u8 *mac_addr = NULL;
4c476991 3632 int err;
fd5b74dc 3633
2ec600d6 3634 memset(&sinfo, 0, sizeof(sinfo));
fd5b74dc
JB
3635
3636 if (!info->attrs[NL80211_ATTR_MAC])
3637 return -EINVAL;
3638
3639 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3640
4c476991
JB
3641 if (!rdev->ops->get_station)
3642 return -EOPNOTSUPP;
3b85875a 3643
e35e4d28 3644 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
fd5b74dc 3645 if (err)
4c476991 3646 return err;
2ec600d6 3647
fd2120ca 3648 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fd5b74dc 3649 if (!msg)
4c476991 3650 return -ENOMEM;
fd5b74dc 3651
15e47304 3652 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
66266b3a 3653 rdev, dev, mac_addr, &sinfo) < 0) {
4c476991
JB
3654 nlmsg_free(msg);
3655 return -ENOBUFS;
3656 }
3b85875a 3657
4c476991 3658 return genlmsg_reply(msg, info);
5727ef1b
JB
3659}
3660
77ee7c89
JB
3661int cfg80211_check_station_change(struct wiphy *wiphy,
3662 struct station_parameters *params,
3663 enum cfg80211_station_type statype)
3664{
3665 if (params->listen_interval != -1)
3666 return -EINVAL;
3667 if (params->aid)
3668 return -EINVAL;
3669
3670 /* When you run into this, adjust the code below for the new flag */
3671 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3672
3673 switch (statype) {
eef941e6
TP
3674 case CFG80211_STA_MESH_PEER_KERNEL:
3675 case CFG80211_STA_MESH_PEER_USER:
77ee7c89
JB
3676 /*
3677 * No ignoring the TDLS flag here -- the userspace mesh
3678 * code doesn't have the bug of including TDLS in the
3679 * mask everywhere.
3680 */
3681 if (params->sta_flags_mask &
3682 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3683 BIT(NL80211_STA_FLAG_MFP) |
3684 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3685 return -EINVAL;
3686 break;
3687 case CFG80211_STA_TDLS_PEER_SETUP:
3688 case CFG80211_STA_TDLS_PEER_ACTIVE:
3689 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3690 return -EINVAL;
3691 /* ignore since it can't change */
3692 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3693 break;
3694 default:
3695 /* disallow mesh-specific things */
3696 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3697 return -EINVAL;
3698 if (params->local_pm)
3699 return -EINVAL;
3700 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3701 return -EINVAL;
3702 }
3703
3704 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3705 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3706 /* TDLS can't be set, ... */
3707 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3708 return -EINVAL;
3709 /*
3710 * ... but don't bother the driver with it. This works around
3711 * a hostapd/wpa_supplicant issue -- it always includes the
3712 * TLDS_PEER flag in the mask even for AP mode.
3713 */
3714 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3715 }
3716
3717 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3718 /* reject other things that can't change */
3719 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3720 return -EINVAL;
3721 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3722 return -EINVAL;
3723 if (params->supported_rates)
3724 return -EINVAL;
3725 if (params->ext_capab || params->ht_capa || params->vht_capa)
3726 return -EINVAL;
3727 }
3728
3729 if (statype != CFG80211_STA_AP_CLIENT) {
3730 if (params->vlan)
3731 return -EINVAL;
3732 }
3733
3734 switch (statype) {
3735 case CFG80211_STA_AP_MLME_CLIENT:
3736 /* Use this only for authorizing/unauthorizing a station */
3737 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3738 return -EOPNOTSUPP;
3739 break;
3740 case CFG80211_STA_AP_CLIENT:
3741 /* accept only the listed bits */
3742 if (params->sta_flags_mask &
3743 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3744 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3745 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3746 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3747 BIT(NL80211_STA_FLAG_WME) |
3748 BIT(NL80211_STA_FLAG_MFP)))
3749 return -EINVAL;
3750
3751 /* but authenticated/associated only if driver handles it */
3752 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3753 params->sta_flags_mask &
3754 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3755 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3756 return -EINVAL;
3757 break;
3758 case CFG80211_STA_IBSS:
3759 case CFG80211_STA_AP_STA:
3760 /* reject any changes other than AUTHORIZED */
3761 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3762 return -EINVAL;
3763 break;
3764 case CFG80211_STA_TDLS_PEER_SETUP:
3765 /* reject any changes other than AUTHORIZED or WME */
3766 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3767 BIT(NL80211_STA_FLAG_WME)))
3768 return -EINVAL;
3769 /* force (at least) rates when authorizing */
3770 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3771 !params->supported_rates)
3772 return -EINVAL;
3773 break;
3774 case CFG80211_STA_TDLS_PEER_ACTIVE:
3775 /* reject any changes */
3776 return -EINVAL;
eef941e6 3777 case CFG80211_STA_MESH_PEER_KERNEL:
77ee7c89
JB
3778 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3779 return -EINVAL;
3780 break;
eef941e6 3781 case CFG80211_STA_MESH_PEER_USER:
77ee7c89
JB
3782 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3783 return -EINVAL;
3784 break;
3785 }
3786
3787 return 0;
3788}
3789EXPORT_SYMBOL(cfg80211_check_station_change);
3790
5727ef1b 3791/*
c258d2de 3792 * Get vlan interface making sure it is running and on the right wiphy.
5727ef1b 3793 */
80b99899
JB
3794static struct net_device *get_vlan(struct genl_info *info,
3795 struct cfg80211_registered_device *rdev)
5727ef1b 3796{
463d0183 3797 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
80b99899
JB
3798 struct net_device *v;
3799 int ret;
3800
3801 if (!vlanattr)
3802 return NULL;
3803
3804 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3805 if (!v)
3806 return ERR_PTR(-ENODEV);
3807
3808 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3809 ret = -EINVAL;
3810 goto error;
5727ef1b 3811 }
80b99899 3812
77ee7c89
JB
3813 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3814 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3815 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3816 ret = -EINVAL;
3817 goto error;
3818 }
3819
80b99899
JB
3820 if (!netif_running(v)) {
3821 ret = -ENETDOWN;
3822 goto error;
3823 }
3824
3825 return v;
3826 error:
3827 dev_put(v);
3828 return ERR_PTR(ret);
5727ef1b
JB
3829}
3830
df881293
JM
3831static struct nla_policy
3832nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3833 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3834 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3835};
3836
ff276691
JB
3837static int nl80211_parse_sta_wme(struct genl_info *info,
3838 struct station_parameters *params)
df881293 3839{
df881293
JM
3840 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3841 struct nlattr *nla;
3842 int err;
3843
df881293
JM
3844 /* parse WME attributes if present */
3845 if (!info->attrs[NL80211_ATTR_STA_WME])
3846 return 0;
3847
3848 nla = info->attrs[NL80211_ATTR_STA_WME];
3849 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3850 nl80211_sta_wme_policy);
3851 if (err)
3852 return err;
3853
3854 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3855 params->uapsd_queues = nla_get_u8(
3856 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3857 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3858 return -EINVAL;
3859
3860 if (tb[NL80211_STA_WME_MAX_SP])
3861 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3862
3863 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3864 return -EINVAL;
3865
3866 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3867
3868 return 0;
3869}
3870
ff276691
JB
3871static int nl80211_set_station_tdls(struct genl_info *info,
3872 struct station_parameters *params)
3873{
3874 /* Dummy STA entry gets updated once the peer capabilities are known */
3875 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3876 params->ht_capa =
3877 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3878 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3879 params->vht_capa =
3880 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3881
3882 return nl80211_parse_sta_wme(info, params);
3883}
3884
5727ef1b
JB
3885static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3886{
4c476991 3887 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4c476991 3888 struct net_device *dev = info->user_ptr[1];
5727ef1b 3889 struct station_parameters params;
77ee7c89
JB
3890 u8 *mac_addr;
3891 int err;
5727ef1b
JB
3892
3893 memset(&params, 0, sizeof(params));
3894
3895 params.listen_interval = -1;
3896
77ee7c89
JB
3897 if (!rdev->ops->change_station)
3898 return -EOPNOTSUPP;
3899
5727ef1b
JB
3900 if (info->attrs[NL80211_ATTR_STA_AID])
3901 return -EINVAL;
3902
3903 if (!info->attrs[NL80211_ATTR_MAC])
3904 return -EINVAL;
3905
3906 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3907
3908 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3909 params.supported_rates =
3910 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3911 params.supported_rates_len =
3912 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3913 }
3914
9d62a986
JM
3915 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3916 params.capability =
3917 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3918 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3919 }
3920
3921 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3922 params.ext_capab =
3923 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3924 params.ext_capab_len =
3925 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3926 }
3927
df881293 3928 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
ba23d206 3929 return -EINVAL;
36aedc90 3930
bdd3ae3d 3931 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5727ef1b
JB
3932 return -EINVAL;
3933
f8bacc21 3934 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
2ec600d6 3935 params.plink_action =
f8bacc21
JB
3936 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3937 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3938 return -EINVAL;
3939 }
2ec600d6 3940
f8bacc21 3941 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
9c3990aa 3942 params.plink_state =
f8bacc21
JB
3943 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3944 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3945 return -EINVAL;
3946 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3947 }
9c3990aa 3948
3b1c5a53
MP
3949 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3950 enum nl80211_mesh_power_mode pm = nla_get_u32(
3951 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3952
3953 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3954 pm > NL80211_MESH_POWER_MAX)
3955 return -EINVAL;
3956
3957 params.local_pm = pm;
3958 }
3959
77ee7c89
JB
3960 /* Include parameters for TDLS peer (will check later) */
3961 err = nl80211_set_station_tdls(info, &params);
3962 if (err)
3963 return err;
3964
3965 params.vlan = get_vlan(info, rdev);
3966 if (IS_ERR(params.vlan))
3967 return PTR_ERR(params.vlan);
3968
a97f4424
JB
3969 switch (dev->ieee80211_ptr->iftype) {
3970 case NL80211_IFTYPE_AP:
3971 case NL80211_IFTYPE_AP_VLAN:
074ac8df 3972 case NL80211_IFTYPE_P2P_GO:
074ac8df 3973 case NL80211_IFTYPE_P2P_CLIENT:
a97f4424 3974 case NL80211_IFTYPE_STATION:
267335d6 3975 case NL80211_IFTYPE_ADHOC:
a97f4424 3976 case NL80211_IFTYPE_MESH_POINT:
a97f4424
JB
3977 break;
3978 default:
77ee7c89
JB
3979 err = -EOPNOTSUPP;
3980 goto out_put_vlan;
034d655e
JB
3981 }
3982
77ee7c89 3983 /* driver will call cfg80211_check_station_change() */
e35e4d28 3984 err = rdev_change_station(rdev, dev, mac_addr, &params);
5727ef1b 3985
77ee7c89 3986 out_put_vlan:
5727ef1b
JB
3987 if (params.vlan)
3988 dev_put(params.vlan);
3b85875a 3989
5727ef1b
JB
3990 return err;
3991}
3992
3993static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3994{
4c476991 3995 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5727ef1b 3996 int err;
4c476991 3997 struct net_device *dev = info->user_ptr[1];
5727ef1b
JB
3998 struct station_parameters params;
3999 u8 *mac_addr = NULL;
4000
4001 memset(&params, 0, sizeof(params));
4002
984c311b
JB
4003 if (!rdev->ops->add_station)
4004 return -EOPNOTSUPP;
4005
5727ef1b
JB
4006 if (!info->attrs[NL80211_ATTR_MAC])
4007 return -EINVAL;
4008
5727ef1b
JB
4009 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4010 return -EINVAL;
4011
4012 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4013 return -EINVAL;
4014
0e956c13
TLSC
4015 if (!info->attrs[NL80211_ATTR_STA_AID])
4016 return -EINVAL;
4017
5727ef1b
JB
4018 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4019 params.supported_rates =
4020 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4021 params.supported_rates_len =
4022 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4023 params.listen_interval =
4024 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
51b50fbe 4025
0e956c13
TLSC
4026 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4027 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4028 return -EINVAL;
51b50fbe 4029
9d62a986
JM
4030 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4031 params.capability =
4032 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4033 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4034 }
4035
4036 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4037 params.ext_capab =
4038 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4039 params.ext_capab_len =
4040 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4041 }
4042
36aedc90
JM
4043 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4044 params.ht_capa =
4045 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5727ef1b 4046
f461be3e
MP
4047 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4048 params.vht_capa =
4049 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4050
f8bacc21 4051 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
96b78dff 4052 params.plink_action =
f8bacc21
JB
4053 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4054 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4055 return -EINVAL;
4056 }
96b78dff 4057
ff276691
JB
4058 err = nl80211_parse_sta_wme(info, &params);
4059 if (err)
4060 return err;
bdd90d5e 4061
bdd3ae3d 4062 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5727ef1b
JB
4063 return -EINVAL;
4064
77ee7c89
JB
4065 /* When you run into this, adjust the code below for the new flag */
4066 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4067
bdd90d5e
JB
4068 switch (dev->ieee80211_ptr->iftype) {
4069 case NL80211_IFTYPE_AP:
4070 case NL80211_IFTYPE_AP_VLAN:
4071 case NL80211_IFTYPE_P2P_GO:
984c311b
JB
4072 /* ignore WME attributes if iface/sta is not capable */
4073 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4074 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4075 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
c75786c9 4076
bdd90d5e
JB
4077 /* TDLS peers cannot be added */
4078 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4319e193 4079 return -EINVAL;
bdd90d5e
JB
4080 /* but don't bother the driver with it */
4081 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3b9ce80c 4082
d582cffb
JB
4083 /* allow authenticated/associated only if driver handles it */
4084 if (!(rdev->wiphy.features &
4085 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4086 params.sta_flags_mask &
4087 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4088 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4089 return -EINVAL;
4090
bdd90d5e
JB
4091 /* must be last in here for error handling */
4092 params.vlan = get_vlan(info, rdev);
4093 if (IS_ERR(params.vlan))
4094 return PTR_ERR(params.vlan);
4095 break;
4096 case NL80211_IFTYPE_MESH_POINT:
984c311b
JB
4097 /* ignore uAPSD data */
4098 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4099
d582cffb
JB
4100 /* associated is disallowed */
4101 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4102 return -EINVAL;
bdd90d5e
JB
4103 /* TDLS peers cannot be added */
4104 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4105 return -EINVAL;
4106 break;
4107 case NL80211_IFTYPE_STATION:
93d08f0b 4108 case NL80211_IFTYPE_P2P_CLIENT:
984c311b
JB
4109 /* ignore uAPSD data */
4110 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4111
77ee7c89
JB
4112 /* these are disallowed */
4113 if (params.sta_flags_mask &
4114 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4115 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
d582cffb 4116 return -EINVAL;
bdd90d5e
JB
4117 /* Only TDLS peers can be added */
4118 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4119 return -EINVAL;
4120 /* Can only add if TDLS ... */
4121 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4122 return -EOPNOTSUPP;
4123 /* ... with external setup is supported */
4124 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4125 return -EOPNOTSUPP;
77ee7c89
JB
4126 /*
4127 * Older wpa_supplicant versions always mark the TDLS peer
4128 * as authorized, but it shouldn't yet be.
4129 */
4130 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
bdd90d5e
JB
4131 break;
4132 default:
4133 return -EOPNOTSUPP;
c75786c9
EP
4134 }
4135
bdd90d5e 4136 /* be aware of params.vlan when changing code here */
5727ef1b 4137
e35e4d28 4138 err = rdev_add_station(rdev, dev, mac_addr, &params);
5727ef1b 4139
5727ef1b
JB
4140 if (params.vlan)
4141 dev_put(params.vlan);
5727ef1b
JB
4142 return err;
4143}
4144
4145static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4146{
4c476991
JB
4147 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4148 struct net_device *dev = info->user_ptr[1];
5727ef1b
JB
4149 u8 *mac_addr = NULL;
4150
4151 if (info->attrs[NL80211_ATTR_MAC])
4152 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4153
e80cf853 4154 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
d5d9de02 4155 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
074ac8df 4156 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4c476991
JB
4157 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4158 return -EINVAL;
5727ef1b 4159
4c476991
JB
4160 if (!rdev->ops->del_station)
4161 return -EOPNOTSUPP;
3b85875a 4162
e35e4d28 4163 return rdev_del_station(rdev, dev, mac_addr);
5727ef1b
JB
4164}
4165
15e47304 4166static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
2ec600d6
LCC
4167 int flags, struct net_device *dev,
4168 u8 *dst, u8 *next_hop,
4169 struct mpath_info *pinfo)
4170{
4171 void *hdr;
4172 struct nlattr *pinfoattr;
4173
15e47304 4174 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
2ec600d6
LCC
4175 if (!hdr)
4176 return -1;
4177
9360ffd1
DM
4178 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4179 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4180 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4181 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4182 goto nla_put_failure;
f5ea9120 4183
2ec600d6
LCC
4184 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4185 if (!pinfoattr)
4186 goto nla_put_failure;
9360ffd1
DM
4187 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4188 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4189 pinfo->frame_qlen))
4190 goto nla_put_failure;
4191 if (((pinfo->filled & MPATH_INFO_SN) &&
4192 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4193 ((pinfo->filled & MPATH_INFO_METRIC) &&
4194 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4195 pinfo->metric)) ||
4196 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4197 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4198 pinfo->exptime)) ||
4199 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4200 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4201 pinfo->flags)) ||
4202 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4203 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4204 pinfo->discovery_timeout)) ||
4205 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4206 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4207 pinfo->discovery_retries)))
4208 goto nla_put_failure;
2ec600d6
LCC
4209
4210 nla_nest_end(msg, pinfoattr);
4211
4212 return genlmsg_end(msg, hdr);
4213
4214 nla_put_failure:
bc3ed28c
TG
4215 genlmsg_cancel(msg, hdr);
4216 return -EMSGSIZE;
2ec600d6
LCC
4217}
4218
4219static int nl80211_dump_mpath(struct sk_buff *skb,
bba95fef 4220 struct netlink_callback *cb)
2ec600d6 4221{
2ec600d6
LCC
4222 struct mpath_info pinfo;
4223 struct cfg80211_registered_device *dev;
97990a06 4224 struct wireless_dev *wdev;
2ec600d6
LCC
4225 u8 dst[ETH_ALEN];
4226 u8 next_hop[ETH_ALEN];
97990a06 4227 int path_idx = cb->args[2];
2ec600d6 4228 int err;
2ec600d6 4229
97990a06 4230 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
67748893
JB
4231 if (err)
4232 return err;
bba95fef
JB
4233
4234 if (!dev->ops->dump_mpath) {
eec60b03 4235 err = -EOPNOTSUPP;
bba95fef
JB
4236 goto out_err;
4237 }
4238
97990a06 4239 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
eec60b03 4240 err = -EOPNOTSUPP;
0448b5fc 4241 goto out_err;
eec60b03
JM
4242 }
4243
bba95fef 4244 while (1) {
97990a06
JB
4245 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4246 next_hop, &pinfo);
bba95fef 4247 if (err == -ENOENT)
2ec600d6 4248 break;
bba95fef 4249 if (err)
3b85875a 4250 goto out_err;
2ec600d6 4251
15e47304 4252 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
bba95fef 4253 cb->nlh->nlmsg_seq, NLM_F_MULTI,
97990a06 4254 wdev->netdev, dst, next_hop,
bba95fef
JB
4255 &pinfo) < 0)
4256 goto out;
2ec600d6 4257
bba95fef 4258 path_idx++;
2ec600d6 4259 }
2ec600d6 4260
2ec600d6 4261
bba95fef 4262 out:
97990a06 4263 cb->args[2] = path_idx;
bba95fef 4264 err = skb->len;
bba95fef 4265 out_err:
97990a06 4266 nl80211_finish_wdev_dump(dev);
bba95fef 4267 return err;
2ec600d6
LCC
4268}
4269
4270static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4271{
4c476991 4272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 4273 int err;
4c476991 4274 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
4275 struct mpath_info pinfo;
4276 struct sk_buff *msg;
4277 u8 *dst = NULL;
4278 u8 next_hop[ETH_ALEN];
4279
4280 memset(&pinfo, 0, sizeof(pinfo));
4281
4282 if (!info->attrs[NL80211_ATTR_MAC])
4283 return -EINVAL;
4284
4285 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4286
4c476991
JB
4287 if (!rdev->ops->get_mpath)
4288 return -EOPNOTSUPP;
2ec600d6 4289
4c476991
JB
4290 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4291 return -EOPNOTSUPP;
eec60b03 4292
e35e4d28 4293 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
2ec600d6 4294 if (err)
4c476991 4295 return err;
2ec600d6 4296
fd2120ca 4297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2ec600d6 4298 if (!msg)
4c476991 4299 return -ENOMEM;
2ec600d6 4300
15e47304 4301 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
4c476991
JB
4302 dev, dst, next_hop, &pinfo) < 0) {
4303 nlmsg_free(msg);
4304 return -ENOBUFS;
4305 }
3b85875a 4306
4c476991 4307 return genlmsg_reply(msg, info);
2ec600d6
LCC
4308}
4309
4310static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4311{
4c476991
JB
4312 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4313 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
4314 u8 *dst = NULL;
4315 u8 *next_hop = NULL;
4316
4317 if (!info->attrs[NL80211_ATTR_MAC])
4318 return -EINVAL;
4319
4320 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4321 return -EINVAL;
4322
4323 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4324 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4325
4c476991
JB
4326 if (!rdev->ops->change_mpath)
4327 return -EOPNOTSUPP;
35a8efe1 4328
4c476991
JB
4329 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4330 return -EOPNOTSUPP;
2ec600d6 4331
e35e4d28 4332 return rdev_change_mpath(rdev, dev, dst, next_hop);
2ec600d6 4333}
4c476991 4334
2ec600d6
LCC
4335static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4336{
4c476991
JB
4337 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4338 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
4339 u8 *dst = NULL;
4340 u8 *next_hop = NULL;
4341
4342 if (!info->attrs[NL80211_ATTR_MAC])
4343 return -EINVAL;
4344
4345 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4346 return -EINVAL;
4347
4348 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4349 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4350
4c476991
JB
4351 if (!rdev->ops->add_mpath)
4352 return -EOPNOTSUPP;
35a8efe1 4353
4c476991
JB
4354 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4355 return -EOPNOTSUPP;
2ec600d6 4356
e35e4d28 4357 return rdev_add_mpath(rdev, dev, dst, next_hop);
2ec600d6
LCC
4358}
4359
4360static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4361{
4c476991
JB
4362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4363 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
4364 u8 *dst = NULL;
4365
4366 if (info->attrs[NL80211_ATTR_MAC])
4367 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4368
4c476991
JB
4369 if (!rdev->ops->del_mpath)
4370 return -EOPNOTSUPP;
3b85875a 4371
e35e4d28 4372 return rdev_del_mpath(rdev, dev, dst);
2ec600d6
LCC
4373}
4374
9f1ba906
JM
4375static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4376{
4c476991
JB
4377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4378 struct net_device *dev = info->user_ptr[1];
9f1ba906
JM
4379 struct bss_parameters params;
4380
4381 memset(&params, 0, sizeof(params));
4382 /* default to not changing parameters */
4383 params.use_cts_prot = -1;
4384 params.use_short_preamble = -1;
4385 params.use_short_slot_time = -1;
fd8aaaf3 4386 params.ap_isolate = -1;
50b12f59 4387 params.ht_opmode = -1;
53cabad7
JB
4388 params.p2p_ctwindow = -1;
4389 params.p2p_opp_ps = -1;
9f1ba906
JM
4390
4391 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4392 params.use_cts_prot =
4393 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4394 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4395 params.use_short_preamble =
4396 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4397 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4398 params.use_short_slot_time =
4399 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
90c97a04
JM
4400 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4401 params.basic_rates =
4402 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4403 params.basic_rates_len =
4404 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4405 }
fd8aaaf3
FF
4406 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4407 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
50b12f59
HS
4408 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4409 params.ht_opmode =
4410 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
9f1ba906 4411
53cabad7
JB
4412 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4413 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4414 return -EINVAL;
4415 params.p2p_ctwindow =
4416 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4417 if (params.p2p_ctwindow < 0)
4418 return -EINVAL;
4419 if (params.p2p_ctwindow != 0 &&
4420 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4421 return -EINVAL;
4422 }
4423
4424 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4425 u8 tmp;
4426
4427 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4428 return -EINVAL;
4429 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4430 if (tmp > 1)
4431 return -EINVAL;
4432 params.p2p_opp_ps = tmp;
4433 if (params.p2p_opp_ps &&
4434 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4435 return -EINVAL;
4436 }
4437
4c476991
JB
4438 if (!rdev->ops->change_bss)
4439 return -EOPNOTSUPP;
9f1ba906 4440
074ac8df 4441 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4c476991
JB
4442 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4443 return -EOPNOTSUPP;
3b85875a 4444
e35e4d28 4445 return rdev_change_bss(rdev, dev, &params);
9f1ba906
JM
4446}
4447
b54452b0 4448static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
b2e1b302
LR
4449 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4450 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4451 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4452 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4453 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4454 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4455};
4456
4457static int parse_reg_rule(struct nlattr *tb[],
4458 struct ieee80211_reg_rule *reg_rule)
4459{
4460 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4461 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4462
4463 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4464 return -EINVAL;
4465 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4466 return -EINVAL;
4467 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4468 return -EINVAL;
4469 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4470 return -EINVAL;
4471 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4472 return -EINVAL;
4473
4474 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4475
4476 freq_range->start_freq_khz =
4477 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4478 freq_range->end_freq_khz =
4479 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4480 freq_range->max_bandwidth_khz =
4481 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4482
4483 power_rule->max_eirp =
4484 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4485
4486 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4487 power_rule->max_antenna_gain =
4488 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4489
4490 return 0;
4491}
4492
4493static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4494{
4495 int r;
4496 char *data = NULL;
57b5ce07 4497 enum nl80211_user_reg_hint_type user_reg_hint_type;
b2e1b302 4498
80778f18
LR
4499 /*
4500 * You should only get this when cfg80211 hasn't yet initialized
4501 * completely when built-in to the kernel right between the time
4502 * window between nl80211_init() and regulatory_init(), if that is
4503 * even possible.
4504 */
458f4f9e 4505 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
fe33eb39 4506 return -EINPROGRESS;
80778f18 4507
fe33eb39
LR
4508 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4509 return -EINVAL;
b2e1b302
LR
4510
4511 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4512
57b5ce07
LR
4513 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4514 user_reg_hint_type =
4515 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4516 else
4517 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4518
4519 switch (user_reg_hint_type) {
4520 case NL80211_USER_REG_HINT_USER:
4521 case NL80211_USER_REG_HINT_CELL_BASE:
4522 break;
4523 default:
4524 return -EINVAL;
4525 }
4526
4527 r = regulatory_hint_user(data, user_reg_hint_type);
fe33eb39 4528
b2e1b302
LR
4529 return r;
4530}
4531
24bdd9f4 4532static int nl80211_get_mesh_config(struct sk_buff *skb,
29cbe68c 4533 struct genl_info *info)
93da9cc1 4534{
4c476991 4535 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4c476991 4536 struct net_device *dev = info->user_ptr[1];
29cbe68c
JB
4537 struct wireless_dev *wdev = dev->ieee80211_ptr;
4538 struct mesh_config cur_params;
4539 int err = 0;
93da9cc1 4540 void *hdr;
4541 struct nlattr *pinfoattr;
4542 struct sk_buff *msg;
4543
29cbe68c
JB
4544 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4545 return -EOPNOTSUPP;
4546
24bdd9f4 4547 if (!rdev->ops->get_mesh_config)
4c476991 4548 return -EOPNOTSUPP;
f3f92586 4549
29cbe68c
JB
4550 wdev_lock(wdev);
4551 /* If not connected, get default parameters */
4552 if (!wdev->mesh_id_len)
4553 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4554 else
e35e4d28 4555 err = rdev_get_mesh_config(rdev, dev, &cur_params);
29cbe68c
JB
4556 wdev_unlock(wdev);
4557
93da9cc1 4558 if (err)
4c476991 4559 return err;
93da9cc1 4560
4561 /* Draw up a netlink message to send back */
fd2120ca 4562 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
4563 if (!msg)
4564 return -ENOMEM;
15e47304 4565 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
24bdd9f4 4566 NL80211_CMD_GET_MESH_CONFIG);
93da9cc1 4567 if (!hdr)
efe1cf0c 4568 goto out;
24bdd9f4 4569 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
93da9cc1 4570 if (!pinfoattr)
4571 goto nla_put_failure;
9360ffd1
DM
4572 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4573 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4574 cur_params.dot11MeshRetryTimeout) ||
4575 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4576 cur_params.dot11MeshConfirmTimeout) ||
4577 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4578 cur_params.dot11MeshHoldingTimeout) ||
4579 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4580 cur_params.dot11MeshMaxPeerLinks) ||
4581 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4582 cur_params.dot11MeshMaxRetries) ||
4583 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4584 cur_params.dot11MeshTTL) ||
4585 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4586 cur_params.element_ttl) ||
4587 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4588 cur_params.auto_open_plinks) ||
7eab0f64
JL
4589 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4590 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
9360ffd1
DM
4591 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4592 cur_params.dot11MeshHWMPmaxPREQretries) ||
4593 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4594 cur_params.path_refresh_time) ||
4595 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4596 cur_params.min_discovery_timeout) ||
4597 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4598 cur_params.dot11MeshHWMPactivePathTimeout) ||
4599 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4600 cur_params.dot11MeshHWMPpreqMinInterval) ||
4601 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4602 cur_params.dot11MeshHWMPperrMinInterval) ||
4603 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4604 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4605 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4606 cur_params.dot11MeshHWMPRootMode) ||
4607 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4608 cur_params.dot11MeshHWMPRannInterval) ||
4609 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4610 cur_params.dot11MeshGateAnnouncementProtocol) ||
4611 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4612 cur_params.dot11MeshForwarding) ||
4613 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
70c33eaa
AN
4614 cur_params.rssi_threshold) ||
4615 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
ac1073a6
CYY
4616 cur_params.ht_opmode) ||
4617 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4618 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4619 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
728b19e5
CYY
4620 cur_params.dot11MeshHWMProotInterval) ||
4621 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3b1c5a53
MP
4622 cur_params.dot11MeshHWMPconfirmationInterval) ||
4623 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4624 cur_params.power_mode) ||
4625 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4626 cur_params.dot11MeshAwakeWindowDuration))
9360ffd1 4627 goto nla_put_failure;
93da9cc1 4628 nla_nest_end(msg, pinfoattr);
4629 genlmsg_end(msg, hdr);
4c476991 4630 return genlmsg_reply(msg, info);
93da9cc1 4631
3b85875a 4632 nla_put_failure:
93da9cc1 4633 genlmsg_cancel(msg, hdr);
efe1cf0c 4634 out:
d080e275 4635 nlmsg_free(msg);
4c476991 4636 return -ENOBUFS;
93da9cc1 4637}
4638
b54452b0 4639static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
93da9cc1 4640 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4641 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4642 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4643 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4644 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4645 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
45904f21 4646 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
93da9cc1 4647 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
d299a1f2 4648 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
93da9cc1 4649 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4650 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4651 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4652 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4653 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
dca7e943 4654 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
93da9cc1 4655 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
699403db 4656 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
0507e159 4657 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
16dd7267 4658 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
94f90656 4659 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
a4f606ea
CYY
4660 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4661 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
ac1073a6
CYY
4662 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4663 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
728b19e5 4664 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
3b1c5a53
MP
4665 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4666 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
93da9cc1 4667};
4668
c80d545d
JC
4669static const struct nla_policy
4670 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
d299a1f2 4671 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
c80d545d
JC
4672 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4673 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
15d5dda6 4674 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
bb2798d4 4675 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
581a8b0f 4676 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
a4f606ea 4677 .len = IEEE80211_MAX_DATA_LEN },
b130e5ce 4678 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
c80d545d
JC
4679};
4680
24bdd9f4 4681static int nl80211_parse_mesh_config(struct genl_info *info,
bd90fdcc
JB
4682 struct mesh_config *cfg,
4683 u32 *mask_out)
93da9cc1 4684{
93da9cc1 4685 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
bd90fdcc 4686 u32 mask = 0;
93da9cc1 4687
ea54fba2
MP
4688#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4689do { \
4690 if (tb[attr]) { \
4691 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4692 return -EINVAL; \
4693 cfg->param = fn(tb[attr]); \
4694 mask |= (1 << (attr - 1)); \
4695 } \
4696} while (0)
bd90fdcc
JB
4697
4698
24bdd9f4 4699 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
93da9cc1 4700 return -EINVAL;
4701 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
24bdd9f4 4702 info->attrs[NL80211_ATTR_MESH_CONFIG],
bd90fdcc 4703 nl80211_meshconf_params_policy))
93da9cc1 4704 return -EINVAL;
4705
93da9cc1 4706 /* This makes sure that there aren't more than 32 mesh config
4707 * parameters (otherwise our bitfield scheme would not work.) */
4708 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4709
4710 /* Fill in the params struct */
ea54fba2 4711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
a4f606ea
CYY
4712 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4713 nla_get_u16);
ea54fba2 4714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
a4f606ea
CYY
4715 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4716 nla_get_u16);
ea54fba2 4717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
a4f606ea
CYY
4718 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4719 nla_get_u16);
ea54fba2 4720 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
a4f606ea
CYY
4721 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4722 nla_get_u16);
ea54fba2 4723 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
a4f606ea
CYY
4724 mask, NL80211_MESHCONF_MAX_RETRIES,
4725 nla_get_u8);
ea54fba2 4726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
a4f606ea 4727 mask, NL80211_MESHCONF_TTL, nla_get_u8);
ea54fba2 4728 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
a4f606ea
CYY
4729 mask, NL80211_MESHCONF_ELEMENT_TTL,
4730 nla_get_u8);
ea54fba2 4731 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
a4f606ea
CYY
4732 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4733 nla_get_u8);
ea54fba2
MP
4734 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4735 1, 255, mask,
a4f606ea
CYY
4736 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4737 nla_get_u32);
ea54fba2 4738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
a4f606ea
CYY
4739 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4740 nla_get_u8);
ea54fba2 4741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
a4f606ea
CYY
4742 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4743 nla_get_u32);
ea54fba2 4744 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
a4f606ea
CYY
4745 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4746 nla_get_u16);
ea54fba2
MP
4747 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4748 1, 65535, mask,
a4f606ea
CYY
4749 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4750 nla_get_u32);
93da9cc1 4751 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
ea54fba2
MP
4752 1, 65535, mask,
4753 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
a4f606ea 4754 nla_get_u16);
dca7e943 4755 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
ea54fba2
MP
4756 1, 65535, mask,
4757 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
a4f606ea 4758 nla_get_u16);
93da9cc1 4759 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
ea54fba2
MP
4760 dot11MeshHWMPnetDiameterTraversalTime,
4761 1, 65535, mask,
a4f606ea
CYY
4762 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4763 nla_get_u16);
ea54fba2
MP
4764 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4765 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4766 nla_get_u8);
4767 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4768 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
a4f606ea 4769 nla_get_u16);
63c5723b 4770 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
ea54fba2
MP
4771 dot11MeshGateAnnouncementProtocol, 0, 1,
4772 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
a4f606ea 4773 nla_get_u8);
ea54fba2 4774 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
a4f606ea
CYY
4775 mask, NL80211_MESHCONF_FORWARDING,
4776 nla_get_u8);
ea54fba2 4777 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
a4f606ea
CYY
4778 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4779 nla_get_u32);
ea54fba2 4780 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
a4f606ea 4781 mask, NL80211_MESHCONF_HT_OPMODE,
ac1073a6
CYY
4782 nla_get_u16);
4783 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
ea54fba2 4784 1, 65535, mask,
ac1073a6
CYY
4785 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4786 nla_get_u32);
ea54fba2 4787 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
ac1073a6 4788 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
728b19e5
CYY
4789 nla_get_u16);
4790 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
ea54fba2
MP
4791 dot11MeshHWMPconfirmationInterval,
4792 1, 65535, mask,
728b19e5 4793 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
a4f606ea 4794 nla_get_u16);
3b1c5a53
MP
4795 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4796 NL80211_MESH_POWER_ACTIVE,
4797 NL80211_MESH_POWER_MAX,
4798 mask, NL80211_MESHCONF_POWER_MODE,
4799 nla_get_u32);
4800 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4801 0, 65535, mask,
4802 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
bd90fdcc
JB
4803 if (mask_out)
4804 *mask_out = mask;
c80d545d 4805
bd90fdcc
JB
4806 return 0;
4807
4808#undef FILL_IN_MESH_PARAM_IF_SET
4809}
4810
c80d545d
JC
4811static int nl80211_parse_mesh_setup(struct genl_info *info,
4812 struct mesh_setup *setup)
4813{
bb2798d4 4814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
c80d545d
JC
4815 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4816
4817 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4818 return -EINVAL;
4819 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4820 info->attrs[NL80211_ATTR_MESH_SETUP],
4821 nl80211_mesh_setup_params_policy))
4822 return -EINVAL;
4823
d299a1f2
JC
4824 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4825 setup->sync_method =
4826 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4827 IEEE80211_SYNC_METHOD_VENDOR :
4828 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4829
c80d545d
JC
4830 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4831 setup->path_sel_proto =
4832 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4833 IEEE80211_PATH_PROTOCOL_VENDOR :
4834 IEEE80211_PATH_PROTOCOL_HWMP;
4835
4836 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4837 setup->path_metric =
4838 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4839 IEEE80211_PATH_METRIC_VENDOR :
4840 IEEE80211_PATH_METRIC_AIRTIME;
4841
581a8b0f
JC
4842
4843 if (tb[NL80211_MESH_SETUP_IE]) {
c80d545d 4844 struct nlattr *ieattr =
581a8b0f 4845 tb[NL80211_MESH_SETUP_IE];
c80d545d
JC
4846 if (!is_valid_ie_attr(ieattr))
4847 return -EINVAL;
581a8b0f
JC
4848 setup->ie = nla_data(ieattr);
4849 setup->ie_len = nla_len(ieattr);
c80d545d 4850 }
bb2798d4
TP
4851 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4852 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4853 return -EINVAL;
4854 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
b130e5ce
JC
4855 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4856 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
bb2798d4
TP
4857 if (setup->is_secure)
4858 setup->user_mpm = true;
c80d545d
JC
4859
4860 return 0;
4861}
4862
24bdd9f4 4863static int nl80211_update_mesh_config(struct sk_buff *skb,
29cbe68c 4864 struct genl_info *info)
bd90fdcc
JB
4865{
4866 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4867 struct net_device *dev = info->user_ptr[1];
29cbe68c 4868 struct wireless_dev *wdev = dev->ieee80211_ptr;
bd90fdcc
JB
4869 struct mesh_config cfg;
4870 u32 mask;
4871 int err;
4872
29cbe68c
JB
4873 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4874 return -EOPNOTSUPP;
4875
24bdd9f4 4876 if (!rdev->ops->update_mesh_config)
bd90fdcc
JB
4877 return -EOPNOTSUPP;
4878
24bdd9f4 4879 err = nl80211_parse_mesh_config(info, &cfg, &mask);
bd90fdcc
JB
4880 if (err)
4881 return err;
4882
29cbe68c
JB
4883 wdev_lock(wdev);
4884 if (!wdev->mesh_id_len)
4885 err = -ENOLINK;
4886
4887 if (!err)
e35e4d28 4888 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
29cbe68c
JB
4889
4890 wdev_unlock(wdev);
4891
4892 return err;
93da9cc1 4893}
4894
f130347c
LR
4895static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4896{
458f4f9e 4897 const struct ieee80211_regdomain *regdom;
f130347c
LR
4898 struct sk_buff *msg;
4899 void *hdr = NULL;
4900 struct nlattr *nl_reg_rules;
4901 unsigned int i;
4902 int err = -EINVAL;
4903
a1794390 4904 mutex_lock(&cfg80211_mutex);
f130347c
LR
4905
4906 if (!cfg80211_regdomain)
4907 goto out;
4908
fd2120ca 4909 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
f130347c
LR
4910 if (!msg) {
4911 err = -ENOBUFS;
4912 goto out;
4913 }
4914
15e47304 4915 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
f130347c
LR
4916 NL80211_CMD_GET_REG);
4917 if (!hdr)
efe1cf0c 4918 goto put_failure;
f130347c 4919
57b5ce07
LR
4920 if (reg_last_request_cell_base() &&
4921 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4922 NL80211_USER_REG_HINT_CELL_BASE))
4923 goto nla_put_failure;
4924
458f4f9e
JB
4925 rcu_read_lock();
4926 regdom = rcu_dereference(cfg80211_regdomain);
4927
4928 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4929 (regdom->dfs_region &&
4930 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4931 goto nla_put_failure_rcu;
4932
f130347c
LR
4933 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4934 if (!nl_reg_rules)
458f4f9e 4935 goto nla_put_failure_rcu;
f130347c 4936
458f4f9e 4937 for (i = 0; i < regdom->n_reg_rules; i++) {
f130347c
LR
4938 struct nlattr *nl_reg_rule;
4939 const struct ieee80211_reg_rule *reg_rule;
4940 const struct ieee80211_freq_range *freq_range;
4941 const struct ieee80211_power_rule *power_rule;
4942
458f4f9e 4943 reg_rule = &regdom->reg_rules[i];
f130347c
LR
4944 freq_range = &reg_rule->freq_range;
4945 power_rule = &reg_rule->power_rule;
4946
4947 nl_reg_rule = nla_nest_start(msg, i);
4948 if (!nl_reg_rule)
458f4f9e 4949 goto nla_put_failure_rcu;
f130347c 4950
9360ffd1
DM
4951 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4952 reg_rule->flags) ||
4953 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4954 freq_range->start_freq_khz) ||
4955 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4956 freq_range->end_freq_khz) ||
4957 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4958 freq_range->max_bandwidth_khz) ||
4959 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4960 power_rule->max_antenna_gain) ||
4961 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4962 power_rule->max_eirp))
458f4f9e 4963 goto nla_put_failure_rcu;
f130347c
LR
4964
4965 nla_nest_end(msg, nl_reg_rule);
4966 }
458f4f9e 4967 rcu_read_unlock();
f130347c
LR
4968
4969 nla_nest_end(msg, nl_reg_rules);
4970
4971 genlmsg_end(msg, hdr);
134e6375 4972 err = genlmsg_reply(msg, info);
f130347c
LR
4973 goto out;
4974
458f4f9e
JB
4975nla_put_failure_rcu:
4976 rcu_read_unlock();
f130347c
LR
4977nla_put_failure:
4978 genlmsg_cancel(msg, hdr);
efe1cf0c 4979put_failure:
d080e275 4980 nlmsg_free(msg);
f130347c
LR
4981 err = -EMSGSIZE;
4982out:
a1794390 4983 mutex_unlock(&cfg80211_mutex);
f130347c
LR
4984 return err;
4985}
4986
b2e1b302
LR
4987static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4988{
4989 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4990 struct nlattr *nl_reg_rule;
4991 char *alpha2 = NULL;
4992 int rem_reg_rules = 0, r = 0;
4993 u32 num_rules = 0, rule_idx = 0, size_of_regd;
8b60b078 4994 u8 dfs_region = 0;
b2e1b302
LR
4995 struct ieee80211_regdomain *rd = NULL;
4996
4997 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4998 return -EINVAL;
4999
5000 if (!info->attrs[NL80211_ATTR_REG_RULES])
5001 return -EINVAL;
5002
5003 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5004
8b60b078
LR
5005 if (info->attrs[NL80211_ATTR_DFS_REGION])
5006 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5007
b2e1b302 5008 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
1a919318 5009 rem_reg_rules) {
b2e1b302
LR
5010 num_rules++;
5011 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
4776c6e7 5012 return -EINVAL;
b2e1b302
LR
5013 }
5014
b2e1b302 5015 size_of_regd = sizeof(struct ieee80211_regdomain) +
1a919318 5016 num_rules * sizeof(struct ieee80211_reg_rule);
b2e1b302
LR
5017
5018 rd = kzalloc(size_of_regd, GFP_KERNEL);
6913b49a
JB
5019 if (!rd)
5020 return -ENOMEM;
b2e1b302
LR
5021
5022 rd->n_reg_rules = num_rules;
5023 rd->alpha2[0] = alpha2[0];
5024 rd->alpha2[1] = alpha2[1];
5025
8b60b078
LR
5026 /*
5027 * Disable DFS master mode if the DFS region was
5028 * not supported or known on this kernel.
5029 */
5030 if (reg_supported_dfs_region(dfs_region))
5031 rd->dfs_region = dfs_region;
5032
b2e1b302 5033 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
1a919318 5034 rem_reg_rules) {
b2e1b302 5035 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
1a919318
JB
5036 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5037 reg_rule_policy);
b2e1b302
LR
5038 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5039 if (r)
5040 goto bad_reg;
5041
5042 rule_idx++;
5043
d0e18f83
LR
5044 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5045 r = -EINVAL;
b2e1b302 5046 goto bad_reg;
d0e18f83 5047 }
b2e1b302
LR
5048 }
5049
6913b49a
JB
5050 mutex_lock(&cfg80211_mutex);
5051
b2e1b302 5052 r = set_regdom(rd);
6913b49a 5053 /* set_regdom took ownership */
1a919318 5054 rd = NULL;
6913b49a 5055 mutex_unlock(&cfg80211_mutex);
b2e1b302 5056
d2372b31 5057 bad_reg:
b2e1b302 5058 kfree(rd);
d0e18f83 5059 return r;
b2e1b302
LR
5060}
5061
83f5e2cf
JB
5062static int validate_scan_freqs(struct nlattr *freqs)
5063{
5064 struct nlattr *attr1, *attr2;
5065 int n_channels = 0, tmp1, tmp2;
5066
5067 nla_for_each_nested(attr1, freqs, tmp1) {
5068 n_channels++;
5069 /*
5070 * Some hardware has a limited channel list for
5071 * scanning, and it is pretty much nonsensical
5072 * to scan for a channel twice, so disallow that
5073 * and don't require drivers to check that the
5074 * channel list they get isn't longer than what
5075 * they can scan, as long as they can scan all
5076 * the channels they registered at once.
5077 */
5078 nla_for_each_nested(attr2, freqs, tmp2)
5079 if (attr1 != attr2 &&
5080 nla_get_u32(attr1) == nla_get_u32(attr2))
5081 return 0;
5082 }
5083
5084 return n_channels;
5085}
5086
2a519311
JB
5087static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5088{
4c476991 5089 struct cfg80211_registered_device *rdev = info->user_ptr[0];
fd014284 5090 struct wireless_dev *wdev = info->user_ptr[1];
2a519311 5091 struct cfg80211_scan_request *request;
2a519311
JB
5092 struct nlattr *attr;
5093 struct wiphy *wiphy;
83f5e2cf 5094 int err, tmp, n_ssids = 0, n_channels, i;
70692ad2 5095 size_t ie_len;
2a519311 5096
f4a11bb0
JB
5097 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5098 return -EINVAL;
5099
79c97e97 5100 wiphy = &rdev->wiphy;
2a519311 5101
4c476991
JB
5102 if (!rdev->ops->scan)
5103 return -EOPNOTSUPP;
2a519311 5104
f9f47529
JB
5105 mutex_lock(&rdev->sched_scan_mtx);
5106 if (rdev->scan_req) {
5107 err = -EBUSY;
5108 goto unlock;
5109 }
2a519311
JB
5110
5111 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
83f5e2cf
JB
5112 n_channels = validate_scan_freqs(
5113 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
f9f47529
JB
5114 if (!n_channels) {
5115 err = -EINVAL;
5116 goto unlock;
5117 }
2a519311 5118 } else {
34850ab2 5119 enum ieee80211_band band;
83f5e2cf
JB
5120 n_channels = 0;
5121
2a519311
JB
5122 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5123 if (wiphy->bands[band])
5124 n_channels += wiphy->bands[band]->n_channels;
5125 }
5126
5127 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5128 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5129 n_ssids++;
5130
f9f47529
JB
5131 if (n_ssids > wiphy->max_scan_ssids) {
5132 err = -EINVAL;
5133 goto unlock;
5134 }
2a519311 5135
70692ad2
JM
5136 if (info->attrs[NL80211_ATTR_IE])
5137 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5138 else
5139 ie_len = 0;
5140
f9f47529
JB
5141 if (ie_len > wiphy->max_scan_ie_len) {
5142 err = -EINVAL;
5143 goto unlock;
5144 }
18a83659 5145
2a519311 5146 request = kzalloc(sizeof(*request)
a2cd43c5
LC
5147 + sizeof(*request->ssids) * n_ssids
5148 + sizeof(*request->channels) * n_channels
70692ad2 5149 + ie_len, GFP_KERNEL);
f9f47529
JB
5150 if (!request) {
5151 err = -ENOMEM;
5152 goto unlock;
5153 }
2a519311 5154
2a519311 5155 if (n_ssids)
5ba63533 5156 request->ssids = (void *)&request->channels[n_channels];
2a519311 5157 request->n_ssids = n_ssids;
70692ad2
JM
5158 if (ie_len) {
5159 if (request->ssids)
5160 request->ie = (void *)(request->ssids + n_ssids);
5161 else
5162 request->ie = (void *)(request->channels + n_channels);
5163 }
2a519311 5164
584991dc 5165 i = 0;
2a519311
JB
5166 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5167 /* user specified, bail out if channel not found */
2a519311 5168 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
584991dc
JB
5169 struct ieee80211_channel *chan;
5170
5171 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5172
5173 if (!chan) {
2a519311
JB
5174 err = -EINVAL;
5175 goto out_free;
5176 }
584991dc
JB
5177
5178 /* ignore disabled channels */
5179 if (chan->flags & IEEE80211_CHAN_DISABLED)
5180 continue;
5181
5182 request->channels[i] = chan;
2a519311
JB
5183 i++;
5184 }
5185 } else {
34850ab2
JB
5186 enum ieee80211_band band;
5187
2a519311 5188 /* all channels */
2a519311
JB
5189 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5190 int j;
5191 if (!wiphy->bands[band])
5192 continue;
5193 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
5194 struct ieee80211_channel *chan;
5195
5196 chan = &wiphy->bands[band]->channels[j];
5197
5198 if (chan->flags & IEEE80211_CHAN_DISABLED)
5199 continue;
5200
5201 request->channels[i] = chan;
2a519311
JB
5202 i++;
5203 }
5204 }
5205 }
5206
584991dc
JB
5207 if (!i) {
5208 err = -EINVAL;
5209 goto out_free;
5210 }
5211
5212 request->n_channels = i;
5213
2a519311
JB
5214 i = 0;
5215 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5216 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
57a27e1d 5217 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
2a519311
JB
5218 err = -EINVAL;
5219 goto out_free;
5220 }
57a27e1d 5221 request->ssids[i].ssid_len = nla_len(attr);
2a519311 5222 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2a519311
JB
5223 i++;
5224 }
5225 }
5226
70692ad2
JM
5227 if (info->attrs[NL80211_ATTR_IE]) {
5228 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
de95a54b
JB
5229 memcpy((void *)request->ie,
5230 nla_data(info->attrs[NL80211_ATTR_IE]),
70692ad2
JM
5231 request->ie_len);
5232 }
5233
34850ab2 5234 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
a401d2bb
JB
5235 if (wiphy->bands[i])
5236 request->rates[i] =
5237 (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2
JB
5238
5239 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5240 nla_for_each_nested(attr,
5241 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5242 tmp) {
5243 enum ieee80211_band band = nla_type(attr);
5244
84404623 5245 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
34850ab2
JB
5246 err = -EINVAL;
5247 goto out_free;
5248 }
5249 err = ieee80211_get_ratemask(wiphy->bands[band],
5250 nla_data(attr),
5251 nla_len(attr),
5252 &request->rates[band]);
5253 if (err)
5254 goto out_free;
5255 }
5256 }
5257
46856bbf 5258 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
ed473771
SL
5259 request->flags = nla_get_u32(
5260 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
15d6030b
SL
5261 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5262 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5263 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5264 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
46856bbf
SL
5265 err = -EOPNOTSUPP;
5266 goto out_free;
5267 }
5268 }
ed473771 5269
e9f935e3
RM
5270 request->no_cck =
5271 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5272
fd014284 5273 request->wdev = wdev;
79c97e97 5274 request->wiphy = &rdev->wiphy;
15d6030b 5275 request->scan_start = jiffies;
2a519311 5276
79c97e97 5277 rdev->scan_req = request;
e35e4d28 5278 err = rdev_scan(rdev, request);
2a519311 5279
463d0183 5280 if (!err) {
fd014284
JB
5281 nl80211_send_scan_start(rdev, wdev);
5282 if (wdev->netdev)
5283 dev_hold(wdev->netdev);
4c476991 5284 } else {
2a519311 5285 out_free:
79c97e97 5286 rdev->scan_req = NULL;
2a519311
JB
5287 kfree(request);
5288 }
3b85875a 5289
f9f47529
JB
5290 unlock:
5291 mutex_unlock(&rdev->sched_scan_mtx);
2a519311
JB
5292 return err;
5293}
5294
807f8a8c
LC
5295static int nl80211_start_sched_scan(struct sk_buff *skb,
5296 struct genl_info *info)
5297{
5298 struct cfg80211_sched_scan_request *request;
5299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5300 struct net_device *dev = info->user_ptr[1];
807f8a8c
LC
5301 struct nlattr *attr;
5302 struct wiphy *wiphy;
a1f1c21c 5303 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
bbe6ad6d 5304 u32 interval;
807f8a8c
LC
5305 enum ieee80211_band band;
5306 size_t ie_len;
a1f1c21c 5307 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
807f8a8c
LC
5308
5309 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5310 !rdev->ops->sched_scan_start)
5311 return -EOPNOTSUPP;
5312
5313 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5314 return -EINVAL;
5315
bbe6ad6d
LC
5316 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5317 return -EINVAL;
5318
5319 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5320 if (interval == 0)
5321 return -EINVAL;
5322
807f8a8c
LC
5323 wiphy = &rdev->wiphy;
5324
5325 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5326 n_channels = validate_scan_freqs(
5327 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5328 if (!n_channels)
5329 return -EINVAL;
5330 } else {
5331 n_channels = 0;
5332
5333 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5334 if (wiphy->bands[band])
5335 n_channels += wiphy->bands[band]->n_channels;
5336 }
5337
5338 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5339 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5340 tmp)
5341 n_ssids++;
5342
93b6aa69 5343 if (n_ssids > wiphy->max_sched_scan_ssids)
807f8a8c
LC
5344 return -EINVAL;
5345
a1f1c21c
LC
5346 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5347 nla_for_each_nested(attr,
5348 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5349 tmp)
5350 n_match_sets++;
5351
5352 if (n_match_sets > wiphy->max_match_sets)
5353 return -EINVAL;
5354
807f8a8c
LC
5355 if (info->attrs[NL80211_ATTR_IE])
5356 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5357 else
5358 ie_len = 0;
5359
5a865bad 5360 if (ie_len > wiphy->max_sched_scan_ie_len)
807f8a8c
LC
5361 return -EINVAL;
5362
c10841ca
LC
5363 mutex_lock(&rdev->sched_scan_mtx);
5364
5365 if (rdev->sched_scan_req) {
5366 err = -EINPROGRESS;
5367 goto out;
5368 }
5369
807f8a8c 5370 request = kzalloc(sizeof(*request)
a2cd43c5 5371 + sizeof(*request->ssids) * n_ssids
a1f1c21c 5372 + sizeof(*request->match_sets) * n_match_sets
a2cd43c5 5373 + sizeof(*request->channels) * n_channels
807f8a8c 5374 + ie_len, GFP_KERNEL);
c10841ca
LC
5375 if (!request) {
5376 err = -ENOMEM;
5377 goto out;
5378 }
807f8a8c
LC
5379
5380 if (n_ssids)
5381 request->ssids = (void *)&request->channels[n_channels];
5382 request->n_ssids = n_ssids;
5383 if (ie_len) {
5384 if (request->ssids)
5385 request->ie = (void *)(request->ssids + n_ssids);
5386 else
5387 request->ie = (void *)(request->channels + n_channels);
5388 }
5389
a1f1c21c
LC
5390 if (n_match_sets) {
5391 if (request->ie)
5392 request->match_sets = (void *)(request->ie + ie_len);
5393 else if (request->ssids)
5394 request->match_sets =
5395 (void *)(request->ssids + n_ssids);
5396 else
5397 request->match_sets =
5398 (void *)(request->channels + n_channels);
5399 }
5400 request->n_match_sets = n_match_sets;
5401
807f8a8c
LC
5402 i = 0;
5403 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5404 /* user specified, bail out if channel not found */
5405 nla_for_each_nested(attr,
5406 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5407 tmp) {
5408 struct ieee80211_channel *chan;
5409
5410 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5411
5412 if (!chan) {
5413 err = -EINVAL;
5414 goto out_free;
5415 }
5416
5417 /* ignore disabled channels */
5418 if (chan->flags & IEEE80211_CHAN_DISABLED)
5419 continue;
5420
5421 request->channels[i] = chan;
5422 i++;
5423 }
5424 } else {
5425 /* all channels */
5426 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5427 int j;
5428 if (!wiphy->bands[band])
5429 continue;
5430 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5431 struct ieee80211_channel *chan;
5432
5433 chan = &wiphy->bands[band]->channels[j];
5434
5435 if (chan->flags & IEEE80211_CHAN_DISABLED)
5436 continue;
5437
5438 request->channels[i] = chan;
5439 i++;
5440 }
5441 }
5442 }
5443
5444 if (!i) {
5445 err = -EINVAL;
5446 goto out_free;
5447 }
5448
5449 request->n_channels = i;
5450
5451 i = 0;
5452 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5453 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5454 tmp) {
57a27e1d 5455 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
807f8a8c
LC
5456 err = -EINVAL;
5457 goto out_free;
5458 }
57a27e1d 5459 request->ssids[i].ssid_len = nla_len(attr);
807f8a8c
LC
5460 memcpy(request->ssids[i].ssid, nla_data(attr),
5461 nla_len(attr));
807f8a8c
LC
5462 i++;
5463 }
5464 }
5465
a1f1c21c
LC
5466 i = 0;
5467 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5468 nla_for_each_nested(attr,
5469 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5470 tmp) {
88e920b4 5471 struct nlattr *ssid, *rssi;
a1f1c21c
LC
5472
5473 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5474 nla_data(attr), nla_len(attr),
5475 nl80211_match_policy);
4a4ab0d7 5476 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
a1f1c21c
LC
5477 if (ssid) {
5478 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5479 err = -EINVAL;
5480 goto out_free;
5481 }
5482 memcpy(request->match_sets[i].ssid.ssid,
5483 nla_data(ssid), nla_len(ssid));
5484 request->match_sets[i].ssid.ssid_len =
5485 nla_len(ssid);
5486 }
88e920b4
TP
5487 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5488 if (rssi)
5489 request->rssi_thold = nla_get_u32(rssi);
5490 else
5491 request->rssi_thold =
5492 NL80211_SCAN_RSSI_THOLD_OFF;
a1f1c21c
LC
5493 i++;
5494 }
5495 }
5496
807f8a8c
LC
5497 if (info->attrs[NL80211_ATTR_IE]) {
5498 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5499 memcpy((void *)request->ie,
5500 nla_data(info->attrs[NL80211_ATTR_IE]),
5501 request->ie_len);
5502 }
5503
46856bbf 5504 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
ed473771
SL
5505 request->flags = nla_get_u32(
5506 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
15d6030b
SL
5507 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5508 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5509 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5510 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
46856bbf
SL
5511 err = -EOPNOTSUPP;
5512 goto out_free;
5513 }
5514 }
ed473771 5515
807f8a8c
LC
5516 request->dev = dev;
5517 request->wiphy = &rdev->wiphy;
bbe6ad6d 5518 request->interval = interval;
15d6030b 5519 request->scan_start = jiffies;
807f8a8c 5520
e35e4d28 5521 err = rdev_sched_scan_start(rdev, dev, request);
807f8a8c
LC
5522 if (!err) {
5523 rdev->sched_scan_req = request;
5524 nl80211_send_sched_scan(rdev, dev,
5525 NL80211_CMD_START_SCHED_SCAN);
5526 goto out;
5527 }
5528
5529out_free:
5530 kfree(request);
5531out:
c10841ca 5532 mutex_unlock(&rdev->sched_scan_mtx);
807f8a8c
LC
5533 return err;
5534}
5535
5536static int nl80211_stop_sched_scan(struct sk_buff *skb,
5537 struct genl_info *info)
5538{
5539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
c10841ca 5540 int err;
807f8a8c
LC
5541
5542 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5543 !rdev->ops->sched_scan_stop)
5544 return -EOPNOTSUPP;
5545
c10841ca
LC
5546 mutex_lock(&rdev->sched_scan_mtx);
5547 err = __cfg80211_stop_sched_scan(rdev, false);
5548 mutex_unlock(&rdev->sched_scan_mtx);
5549
5550 return err;
807f8a8c
LC
5551}
5552
04f39047
SW
5553static int nl80211_start_radar_detection(struct sk_buff *skb,
5554 struct genl_info *info)
5555{
5556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5557 struct net_device *dev = info->user_ptr[1];
5558 struct wireless_dev *wdev = dev->ieee80211_ptr;
5559 struct cfg80211_chan_def chandef;
5560 int err;
5561
5562 err = nl80211_parse_chandef(rdev, info, &chandef);
5563 if (err)
5564 return err;
5565
5566 if (wdev->cac_started)
5567 return -EBUSY;
5568
5569 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5570 if (err < 0)
5571 return err;
5572
5573 if (err == 0)
5574 return -EINVAL;
5575
5576 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5577 return -EINVAL;
5578
5579 if (!rdev->ops->start_radar_detection)
5580 return -EOPNOTSUPP;
5581
5582 mutex_lock(&rdev->devlist_mtx);
5583 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5584 chandef.chan, CHAN_MODE_SHARED,
5585 BIT(chandef.width));
5586 if (err)
5587 goto err_locked;
5588
5589 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5590 if (!err) {
5591 wdev->channel = chandef.chan;
5592 wdev->cac_started = true;
5593 wdev->cac_start_time = jiffies;
5594 }
5595err_locked:
5596 mutex_unlock(&rdev->devlist_mtx);
5597
5598 return err;
5599}
5600
9720bb3a
JB
5601static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5602 u32 seq, int flags,
2a519311 5603 struct cfg80211_registered_device *rdev,
48ab905d
JB
5604 struct wireless_dev *wdev,
5605 struct cfg80211_internal_bss *intbss)
2a519311 5606{
48ab905d 5607 struct cfg80211_bss *res = &intbss->pub;
9caf0364 5608 const struct cfg80211_bss_ies *ies;
2a519311
JB
5609 void *hdr;
5610 struct nlattr *bss;
8cef2c9d 5611 bool tsf = false;
48ab905d
JB
5612
5613 ASSERT_WDEV_LOCK(wdev);
2a519311 5614
15e47304 5615 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
2a519311
JB
5616 NL80211_CMD_NEW_SCAN_RESULTS);
5617 if (!hdr)
5618 return -1;
5619
9720bb3a
JB
5620 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5621
97990a06
JB
5622 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5623 goto nla_put_failure;
5624 if (wdev->netdev &&
9360ffd1
DM
5625 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5626 goto nla_put_failure;
97990a06
JB
5627 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5628 goto nla_put_failure;
2a519311
JB
5629
5630 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5631 if (!bss)
5632 goto nla_put_failure;
9360ffd1 5633 if ((!is_zero_ether_addr(res->bssid) &&
9caf0364 5634 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
9360ffd1 5635 goto nla_put_failure;
9caf0364
JB
5636
5637 rcu_read_lock();
5638 ies = rcu_dereference(res->ies);
8cef2c9d
JB
5639 if (ies) {
5640 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5641 goto fail_unlock_rcu;
5642 tsf = true;
5643 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5644 ies->len, ies->data))
5645 goto fail_unlock_rcu;
9caf0364
JB
5646 }
5647 ies = rcu_dereference(res->beacon_ies);
8cef2c9d
JB
5648 if (ies) {
5649 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5650 goto fail_unlock_rcu;
5651 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5652 ies->len, ies->data))
5653 goto fail_unlock_rcu;
9caf0364
JB
5654 }
5655 rcu_read_unlock();
5656
9360ffd1
DM
5657 if (res->beacon_interval &&
5658 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5659 goto nla_put_failure;
5660 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5661 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5662 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5663 jiffies_to_msecs(jiffies - intbss->ts)))
5664 goto nla_put_failure;
2a519311 5665
77965c97 5666 switch (rdev->wiphy.signal_type) {
2a519311 5667 case CFG80211_SIGNAL_TYPE_MBM:
9360ffd1
DM
5668 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5669 goto nla_put_failure;
2a519311
JB
5670 break;
5671 case CFG80211_SIGNAL_TYPE_UNSPEC:
9360ffd1
DM
5672 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5673 goto nla_put_failure;
2a519311
JB
5674 break;
5675 default:
5676 break;
5677 }
5678
48ab905d 5679 switch (wdev->iftype) {
074ac8df 5680 case NL80211_IFTYPE_P2P_CLIENT:
48ab905d 5681 case NL80211_IFTYPE_STATION:
9360ffd1
DM
5682 if (intbss == wdev->current_bss &&
5683 nla_put_u32(msg, NL80211_BSS_STATUS,
5684 NL80211_BSS_STATUS_ASSOCIATED))
5685 goto nla_put_failure;
48ab905d
JB
5686 break;
5687 case NL80211_IFTYPE_ADHOC:
9360ffd1
DM
5688 if (intbss == wdev->current_bss &&
5689 nla_put_u32(msg, NL80211_BSS_STATUS,
5690 NL80211_BSS_STATUS_IBSS_JOINED))
5691 goto nla_put_failure;
48ab905d
JB
5692 break;
5693 default:
5694 break;
5695 }
5696
2a519311
JB
5697 nla_nest_end(msg, bss);
5698
5699 return genlmsg_end(msg, hdr);
5700
8cef2c9d
JB
5701 fail_unlock_rcu:
5702 rcu_read_unlock();
2a519311
JB
5703 nla_put_failure:
5704 genlmsg_cancel(msg, hdr);
5705 return -EMSGSIZE;
5706}
5707
97990a06 5708static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
2a519311 5709{
48ab905d 5710 struct cfg80211_registered_device *rdev;
2a519311 5711 struct cfg80211_internal_bss *scan;
48ab905d 5712 struct wireless_dev *wdev;
97990a06 5713 int start = cb->args[2], idx = 0;
2a519311
JB
5714 int err;
5715
97990a06 5716 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
67748893
JB
5717 if (err)
5718 return err;
2a519311 5719
48ab905d
JB
5720 wdev_lock(wdev);
5721 spin_lock_bh(&rdev->bss_lock);
5722 cfg80211_bss_expire(rdev);
5723
9720bb3a
JB
5724 cb->seq = rdev->bss_generation;
5725
48ab905d 5726 list_for_each_entry(scan, &rdev->bss_list, list) {
2a519311
JB
5727 if (++idx <= start)
5728 continue;
9720bb3a 5729 if (nl80211_send_bss(skb, cb,
2a519311 5730 cb->nlh->nlmsg_seq, NLM_F_MULTI,
48ab905d 5731 rdev, wdev, scan) < 0) {
2a519311 5732 idx--;
67748893 5733 break;
2a519311
JB
5734 }
5735 }
5736
48ab905d
JB
5737 spin_unlock_bh(&rdev->bss_lock);
5738 wdev_unlock(wdev);
2a519311 5739
97990a06
JB
5740 cb->args[2] = idx;
5741 nl80211_finish_wdev_dump(rdev);
2a519311 5742
67748893 5743 return skb->len;
2a519311
JB
5744}
5745
15e47304 5746static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
61fa713c
HS
5747 int flags, struct net_device *dev,
5748 struct survey_info *survey)
5749{
5750 void *hdr;
5751 struct nlattr *infoattr;
5752
15e47304 5753 hdr = nl80211hdr_put(msg, portid, seq, flags,
61fa713c
HS
5754 NL80211_CMD_NEW_SURVEY_RESULTS);
5755 if (!hdr)
5756 return -ENOMEM;
5757
9360ffd1
DM
5758 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5759 goto nla_put_failure;
61fa713c
HS
5760
5761 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5762 if (!infoattr)
5763 goto nla_put_failure;
5764
9360ffd1
DM
5765 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5766 survey->channel->center_freq))
5767 goto nla_put_failure;
5768
5769 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5770 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5771 goto nla_put_failure;
5772 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5773 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5774 goto nla_put_failure;
5775 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5776 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5777 survey->channel_time))
5778 goto nla_put_failure;
5779 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5780 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5781 survey->channel_time_busy))
5782 goto nla_put_failure;
5783 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5784 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5785 survey->channel_time_ext_busy))
5786 goto nla_put_failure;
5787 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5788 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5789 survey->channel_time_rx))
5790 goto nla_put_failure;
5791 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5792 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5793 survey->channel_time_tx))
5794 goto nla_put_failure;
61fa713c
HS
5795
5796 nla_nest_end(msg, infoattr);
5797
5798 return genlmsg_end(msg, hdr);
5799
5800 nla_put_failure:
5801 genlmsg_cancel(msg, hdr);
5802 return -EMSGSIZE;
5803}
5804
5805static int nl80211_dump_survey(struct sk_buff *skb,
5806 struct netlink_callback *cb)
5807{
5808 struct survey_info survey;
5809 struct cfg80211_registered_device *dev;
97990a06
JB
5810 struct wireless_dev *wdev;
5811 int survey_idx = cb->args[2];
61fa713c
HS
5812 int res;
5813
97990a06 5814 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
67748893
JB
5815 if (res)
5816 return res;
61fa713c 5817
97990a06
JB
5818 if (!wdev->netdev) {
5819 res = -EINVAL;
5820 goto out_err;
5821 }
5822
61fa713c
HS
5823 if (!dev->ops->dump_survey) {
5824 res = -EOPNOTSUPP;
5825 goto out_err;
5826 }
5827
5828 while (1) {
180cdc79
LR
5829 struct ieee80211_channel *chan;
5830
97990a06 5831 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
61fa713c
HS
5832 if (res == -ENOENT)
5833 break;
5834 if (res)
5835 goto out_err;
5836
180cdc79
LR
5837 /* Survey without a channel doesn't make sense */
5838 if (!survey.channel) {
5839 res = -EINVAL;
5840 goto out;
5841 }
5842
5843 chan = ieee80211_get_channel(&dev->wiphy,
5844 survey.channel->center_freq);
5845 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5846 survey_idx++;
5847 continue;
5848 }
5849
61fa713c 5850 if (nl80211_send_survey(skb,
15e47304 5851 NETLINK_CB(cb->skb).portid,
61fa713c 5852 cb->nlh->nlmsg_seq, NLM_F_MULTI,
97990a06 5853 wdev->netdev, &survey) < 0)
61fa713c
HS
5854 goto out;
5855 survey_idx++;
5856 }
5857
5858 out:
97990a06 5859 cb->args[2] = survey_idx;
61fa713c
HS
5860 res = skb->len;
5861 out_err:
97990a06 5862 nl80211_finish_wdev_dump(dev);
61fa713c
HS
5863 return res;
5864}
5865
b23aa676
SO
5866static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5867{
5868 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5869 NL80211_WPA_VERSION_2));
5870}
5871
636a5d36
JM
5872static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5873{
4c476991
JB
5874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5875 struct net_device *dev = info->user_ptr[1];
19957bb3 5876 struct ieee80211_channel *chan;
e39e5b5e
JM
5877 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5878 int err, ssid_len, ie_len = 0, sae_data_len = 0;
19957bb3 5879 enum nl80211_auth_type auth_type;
fffd0934 5880 struct key_parse key;
d5cdfacb 5881 bool local_state_change;
636a5d36 5882
f4a11bb0
JB
5883 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5884 return -EINVAL;
5885
5886 if (!info->attrs[NL80211_ATTR_MAC])
5887 return -EINVAL;
5888
1778092e
JM
5889 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5890 return -EINVAL;
5891
19957bb3
JB
5892 if (!info->attrs[NL80211_ATTR_SSID])
5893 return -EINVAL;
5894
5895 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5896 return -EINVAL;
5897
fffd0934
JB
5898 err = nl80211_parse_key(info, &key);
5899 if (err)
5900 return err;
5901
5902 if (key.idx >= 0) {
e31b8213
JB
5903 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5904 return -EINVAL;
fffd0934
JB
5905 if (!key.p.key || !key.p.key_len)
5906 return -EINVAL;
5907 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5908 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5909 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5910 key.p.key_len != WLAN_KEY_LEN_WEP104))
5911 return -EINVAL;
5912 if (key.idx > 4)
5913 return -EINVAL;
5914 } else {
5915 key.p.key_len = 0;
5916 key.p.key = NULL;
5917 }
5918
afea0b7a
JB
5919 if (key.idx >= 0) {
5920 int i;
5921 bool ok = false;
5922 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5923 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5924 ok = true;
5925 break;
5926 }
5927 }
4c476991
JB
5928 if (!ok)
5929 return -EINVAL;
afea0b7a
JB
5930 }
5931
4c476991
JB
5932 if (!rdev->ops->auth)
5933 return -EOPNOTSUPP;
636a5d36 5934
074ac8df 5935 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5936 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5937 return -EOPNOTSUPP;
eec60b03 5938
19957bb3 5939 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
79c97e97 5940 chan = ieee80211_get_channel(&rdev->wiphy,
19957bb3 5941 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4c476991
JB
5942 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5943 return -EINVAL;
636a5d36 5944
19957bb3
JB
5945 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5946 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
636a5d36
JM
5947
5948 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
5949 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5950 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
5951 }
5952
19957bb3 5953 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
e39e5b5e 5954 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
4c476991 5955 return -EINVAL;
636a5d36 5956
e39e5b5e
JM
5957 if (auth_type == NL80211_AUTHTYPE_SAE &&
5958 !info->attrs[NL80211_ATTR_SAE_DATA])
5959 return -EINVAL;
5960
5961 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5962 if (auth_type != NL80211_AUTHTYPE_SAE)
5963 return -EINVAL;
5964 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5965 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5966 /* need to include at least Auth Transaction and Status Code */
5967 if (sae_data_len < 4)
5968 return -EINVAL;
5969 }
5970
d5cdfacb
JM
5971 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5972
95de817b
JB
5973 /*
5974 * Since we no longer track auth state, ignore
5975 * requests to only change local state.
5976 */
5977 if (local_state_change)
5978 return 0;
5979
4c476991
JB
5980 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5981 ssid, ssid_len, ie, ie_len,
e39e5b5e
JM
5982 key.p.key, key.p.key_len, key.idx,
5983 sae_data, sae_data_len);
636a5d36
JM
5984}
5985
c0692b8f
JB
5986static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5987 struct genl_info *info,
3dc27d25
JB
5988 struct cfg80211_crypto_settings *settings,
5989 int cipher_limit)
b23aa676 5990{
c0b2bbd8
JB
5991 memset(settings, 0, sizeof(*settings));
5992
b23aa676
SO
5993 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5994
c0692b8f
JB
5995 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5996 u16 proto;
5997 proto = nla_get_u16(
5998 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5999 settings->control_port_ethertype = cpu_to_be16(proto);
6000 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6001 proto != ETH_P_PAE)
6002 return -EINVAL;
6003 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6004 settings->control_port_no_encrypt = true;
6005 } else
6006 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6007
b23aa676
SO
6008 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6009 void *data;
6010 int len, i;
6011
6012 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6013 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6014 settings->n_ciphers_pairwise = len / sizeof(u32);
6015
6016 if (len % sizeof(u32))
6017 return -EINVAL;
6018
3dc27d25 6019 if (settings->n_ciphers_pairwise > cipher_limit)
b23aa676
SO
6020 return -EINVAL;
6021
6022 memcpy(settings->ciphers_pairwise, data, len);
6023
6024 for (i = 0; i < settings->n_ciphers_pairwise; i++)
38ba3c57
JM
6025 if (!cfg80211_supported_cipher_suite(
6026 &rdev->wiphy,
b23aa676
SO
6027 settings->ciphers_pairwise[i]))
6028 return -EINVAL;
6029 }
6030
6031 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6032 settings->cipher_group =
6033 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
38ba3c57
JM
6034 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6035 settings->cipher_group))
b23aa676
SO
6036 return -EINVAL;
6037 }
6038
6039 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6040 settings->wpa_versions =
6041 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6042 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6043 return -EINVAL;
6044 }
6045
6046 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6047 void *data;
6d30240e 6048 int len;
b23aa676
SO
6049
6050 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6051 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6052 settings->n_akm_suites = len / sizeof(u32);
6053
6054 if (len % sizeof(u32))
6055 return -EINVAL;
6056
1b9ca027
JM
6057 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6058 return -EINVAL;
6059
b23aa676 6060 memcpy(settings->akm_suites, data, len);
b23aa676
SO
6061 }
6062
6063 return 0;
6064}
6065
636a5d36
JM
6066static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6067{
4c476991
JB
6068 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6069 struct net_device *dev = info->user_ptr[1];
f444de05 6070 struct ieee80211_channel *chan;
f62fab73
JB
6071 struct cfg80211_assoc_request req = {};
6072 const u8 *bssid, *ssid;
6073 int err, ssid_len = 0;
636a5d36 6074
f4a11bb0
JB
6075 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6076 return -EINVAL;
6077
6078 if (!info->attrs[NL80211_ATTR_MAC] ||
19957bb3
JB
6079 !info->attrs[NL80211_ATTR_SSID] ||
6080 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
f4a11bb0
JB
6081 return -EINVAL;
6082
4c476991
JB
6083 if (!rdev->ops->assoc)
6084 return -EOPNOTSUPP;
636a5d36 6085
074ac8df 6086 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6088 return -EOPNOTSUPP;
eec60b03 6089
19957bb3 6090 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 6091
19957bb3
JB
6092 chan = ieee80211_get_channel(&rdev->wiphy,
6093 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4c476991
JB
6094 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6095 return -EINVAL;
636a5d36 6096
19957bb3
JB
6097 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6098 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
636a5d36
JM
6099
6100 if (info->attrs[NL80211_ATTR_IE]) {
f62fab73
JB
6101 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6102 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
6103 }
6104
dc6382ce 6105 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4f5dadce 6106 enum nl80211_mfp mfp =
dc6382ce 6107 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4f5dadce 6108 if (mfp == NL80211_MFP_REQUIRED)
f62fab73 6109 req.use_mfp = true;
4c476991
JB
6110 else if (mfp != NL80211_MFP_NO)
6111 return -EINVAL;
dc6382ce
JM
6112 }
6113
3e5d7649 6114 if (info->attrs[NL80211_ATTR_PREV_BSSID])
f62fab73 6115 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3e5d7649 6116
7e7c8926 6117 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
f62fab73 6118 req.flags |= ASSOC_REQ_DISABLE_HT;
7e7c8926
BG
6119
6120 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
f62fab73
JB
6121 memcpy(&req.ht_capa_mask,
6122 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6123 sizeof(req.ht_capa_mask));
7e7c8926
BG
6124
6125 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
f62fab73 6126 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
7e7c8926 6127 return -EINVAL;
f62fab73
JB
6128 memcpy(&req.ht_capa,
6129 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6130 sizeof(req.ht_capa));
7e7c8926
BG
6131 }
6132
ee2aca34 6133 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
f62fab73 6134 req.flags |= ASSOC_REQ_DISABLE_VHT;
ee2aca34
JB
6135
6136 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
f62fab73
JB
6137 memcpy(&req.vht_capa_mask,
6138 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6139 sizeof(req.vht_capa_mask));
ee2aca34
JB
6140
6141 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
f62fab73 6142 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
ee2aca34 6143 return -EINVAL;
f62fab73
JB
6144 memcpy(&req.vht_capa,
6145 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6146 sizeof(req.vht_capa));
ee2aca34
JB
6147 }
6148
f62fab73 6149 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
b23aa676 6150 if (!err)
f62fab73
JB
6151 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6152 ssid, ssid_len, &req);
636a5d36 6153
636a5d36
JM
6154 return err;
6155}
6156
6157static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6158{
4c476991
JB
6159 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6160 struct net_device *dev = info->user_ptr[1];
19957bb3 6161 const u8 *ie = NULL, *bssid;
4c476991 6162 int ie_len = 0;
19957bb3 6163 u16 reason_code;
d5cdfacb 6164 bool local_state_change;
636a5d36 6165
f4a11bb0
JB
6166 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6167 return -EINVAL;
6168
6169 if (!info->attrs[NL80211_ATTR_MAC])
6170 return -EINVAL;
6171
6172 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6173 return -EINVAL;
6174
4c476991
JB
6175 if (!rdev->ops->deauth)
6176 return -EOPNOTSUPP;
636a5d36 6177
074ac8df 6178 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6179 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6180 return -EOPNOTSUPP;
eec60b03 6181
19957bb3 6182 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 6183
19957bb3
JB
6184 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6185 if (reason_code == 0) {
f4a11bb0 6186 /* Reason Code 0 is reserved */
4c476991 6187 return -EINVAL;
255e737e 6188 }
636a5d36
JM
6189
6190 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
6191 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6192 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
6193 }
6194
d5cdfacb
JM
6195 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6196
4c476991
JB
6197 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6198 local_state_change);
636a5d36
JM
6199}
6200
6201static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6202{
4c476991
JB
6203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6204 struct net_device *dev = info->user_ptr[1];
19957bb3 6205 const u8 *ie = NULL, *bssid;
4c476991 6206 int ie_len = 0;
19957bb3 6207 u16 reason_code;
d5cdfacb 6208 bool local_state_change;
636a5d36 6209
f4a11bb0
JB
6210 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6211 return -EINVAL;
6212
6213 if (!info->attrs[NL80211_ATTR_MAC])
6214 return -EINVAL;
6215
6216 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6217 return -EINVAL;
6218
4c476991
JB
6219 if (!rdev->ops->disassoc)
6220 return -EOPNOTSUPP;
636a5d36 6221
074ac8df 6222 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6223 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6224 return -EOPNOTSUPP;
eec60b03 6225
19957bb3 6226 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 6227
19957bb3
JB
6228 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6229 if (reason_code == 0) {
f4a11bb0 6230 /* Reason Code 0 is reserved */
4c476991 6231 return -EINVAL;
255e737e 6232 }
636a5d36
JM
6233
6234 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
6235 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6236 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
6237 }
6238
d5cdfacb
JM
6239 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6240
4c476991
JB
6241 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6242 local_state_change);
636a5d36
JM
6243}
6244
dd5b4cc7
FF
6245static bool
6246nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6247 int mcast_rate[IEEE80211_NUM_BANDS],
6248 int rateval)
6249{
6250 struct wiphy *wiphy = &rdev->wiphy;
6251 bool found = false;
6252 int band, i;
6253
6254 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6255 struct ieee80211_supported_band *sband;
6256
6257 sband = wiphy->bands[band];
6258 if (!sband)
6259 continue;
6260
6261 for (i = 0; i < sband->n_bitrates; i++) {
6262 if (sband->bitrates[i].bitrate == rateval) {
6263 mcast_rate[band] = i + 1;
6264 found = true;
6265 break;
6266 }
6267 }
6268 }
6269
6270 return found;
6271}
6272
04a773ad
JB
6273static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6274{
4c476991
JB
6275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6276 struct net_device *dev = info->user_ptr[1];
04a773ad
JB
6277 struct cfg80211_ibss_params ibss;
6278 struct wiphy *wiphy;
fffd0934 6279 struct cfg80211_cached_keys *connkeys = NULL;
04a773ad
JB
6280 int err;
6281
8e30bc55
JB
6282 memset(&ibss, 0, sizeof(ibss));
6283
04a773ad
JB
6284 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6285 return -EINVAL;
6286
683b6d3b 6287 if (!info->attrs[NL80211_ATTR_SSID] ||
04a773ad
JB
6288 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6289 return -EINVAL;
6290
8e30bc55
JB
6291 ibss.beacon_interval = 100;
6292
6293 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6294 ibss.beacon_interval =
6295 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6296 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6297 return -EINVAL;
6298 }
6299
4c476991
JB
6300 if (!rdev->ops->join_ibss)
6301 return -EOPNOTSUPP;
04a773ad 6302
4c476991
JB
6303 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6304 return -EOPNOTSUPP;
04a773ad 6305
79c97e97 6306 wiphy = &rdev->wiphy;
04a773ad 6307
39193498 6308 if (info->attrs[NL80211_ATTR_MAC]) {
04a773ad 6309 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
39193498
JB
6310
6311 if (!is_valid_ether_addr(ibss.bssid))
6312 return -EINVAL;
6313 }
04a773ad
JB
6314 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6315 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6316
6317 if (info->attrs[NL80211_ATTR_IE]) {
6318 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6319 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6320 }
6321
683b6d3b
JB
6322 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6323 if (err)
6324 return err;
04a773ad 6325
683b6d3b 6326 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
54858ee5
AS
6327 return -EINVAL;
6328
db9c64cf
JB
6329 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6330 return -EINVAL;
6331 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6332 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
c04d6150 6333 return -EINVAL;
db9c64cf 6334
04a773ad 6335 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
fffd0934
JB
6336 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
6337
fbd2c8dc
TP
6338 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6339 u8 *rates =
6340 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6341 int n_rates =
6342 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6343 struct ieee80211_supported_band *sband =
683b6d3b 6344 wiphy->bands[ibss.chandef.chan->band];
fbd2c8dc 6345
34850ab2
JB
6346 err = ieee80211_get_ratemask(sband, rates, n_rates,
6347 &ibss.basic_rates);
6348 if (err)
6349 return err;
fbd2c8dc 6350 }
dd5b4cc7
FF
6351
6352 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6353 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6354 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6355 return -EINVAL;
fbd2c8dc 6356
4c476991 6357 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
de7044ee
SM
6358 bool no_ht = false;
6359
4c476991 6360 connkeys = nl80211_parse_connkeys(rdev,
de7044ee
SM
6361 info->attrs[NL80211_ATTR_KEYS],
6362 &no_ht);
4c476991
JB
6363 if (IS_ERR(connkeys))
6364 return PTR_ERR(connkeys);
de7044ee 6365
3d9d1d66
JB
6366 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6367 no_ht) {
de7044ee
SM
6368 kfree(connkeys);
6369 return -EINVAL;
6370 }
4c476991 6371 }
04a773ad 6372
267335d6
AQ
6373 ibss.control_port =
6374 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6375
4c476991 6376 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
fffd0934
JB
6377 if (err)
6378 kfree(connkeys);
04a773ad
JB
6379 return err;
6380}
6381
6382static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6383{
4c476991
JB
6384 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6385 struct net_device *dev = info->user_ptr[1];
04a773ad 6386
4c476991
JB
6387 if (!rdev->ops->leave_ibss)
6388 return -EOPNOTSUPP;
04a773ad 6389
4c476991
JB
6390 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6391 return -EOPNOTSUPP;
04a773ad 6392
4c476991 6393 return cfg80211_leave_ibss(rdev, dev, false);
04a773ad
JB
6394}
6395
f4e583c8
AQ
6396static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6397{
6398 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6399 struct net_device *dev = info->user_ptr[1];
6400 int mcast_rate[IEEE80211_NUM_BANDS];
6401 u32 nla_rate;
6402 int err;
6403
6404 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6405 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6406 return -EOPNOTSUPP;
6407
6408 if (!rdev->ops->set_mcast_rate)
6409 return -EOPNOTSUPP;
6410
6411 memset(mcast_rate, 0, sizeof(mcast_rate));
6412
6413 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6414 return -EINVAL;
6415
6416 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6417 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6418 return -EINVAL;
6419
6420 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6421
6422 return err;
6423}
6424
6425
aff89a9b
JB
6426#ifdef CONFIG_NL80211_TESTMODE
6427static struct genl_multicast_group nl80211_testmode_mcgrp = {
6428 .name = "testmode",
6429};
6430
6431static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6432{
4c476991 6433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
aff89a9b
JB
6434 int err;
6435
6436 if (!info->attrs[NL80211_ATTR_TESTDATA])
6437 return -EINVAL;
6438
aff89a9b
JB
6439 err = -EOPNOTSUPP;
6440 if (rdev->ops->testmode_cmd) {
6441 rdev->testmode_info = info;
e35e4d28 6442 err = rdev_testmode_cmd(rdev,
aff89a9b
JB
6443 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6444 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6445 rdev->testmode_info = NULL;
6446 }
6447
aff89a9b
JB
6448 return err;
6449}
6450
71063f0e
WYG
6451static int nl80211_testmode_dump(struct sk_buff *skb,
6452 struct netlink_callback *cb)
6453{
00918d33 6454 struct cfg80211_registered_device *rdev;
71063f0e
WYG
6455 int err;
6456 long phy_idx;
6457 void *data = NULL;
6458 int data_len = 0;
6459
6460 if (cb->args[0]) {
6461 /*
6462 * 0 is a valid index, but not valid for args[0],
6463 * so we need to offset by 1.
6464 */
6465 phy_idx = cb->args[0] - 1;
6466 } else {
6467 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6468 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6469 nl80211_policy);
6470 if (err)
6471 return err;
00918d33 6472
2bd7e35d
JB
6473 mutex_lock(&cfg80211_mutex);
6474 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6475 nl80211_fam.attrbuf);
6476 if (IS_ERR(rdev)) {
6477 mutex_unlock(&cfg80211_mutex);
6478 return PTR_ERR(rdev);
00918d33 6479 }
2bd7e35d
JB
6480 phy_idx = rdev->wiphy_idx;
6481 rdev = NULL;
6482 mutex_unlock(&cfg80211_mutex);
6483
71063f0e
WYG
6484 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6485 cb->args[1] =
6486 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6487 }
6488
6489 if (cb->args[1]) {
6490 data = nla_data((void *)cb->args[1]);
6491 data_len = nla_len((void *)cb->args[1]);
6492 }
6493
6494 mutex_lock(&cfg80211_mutex);
00918d33
JB
6495 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6496 if (!rdev) {
71063f0e
WYG
6497 mutex_unlock(&cfg80211_mutex);
6498 return -ENOENT;
6499 }
00918d33 6500 cfg80211_lock_rdev(rdev);
71063f0e
WYG
6501 mutex_unlock(&cfg80211_mutex);
6502
00918d33 6503 if (!rdev->ops->testmode_dump) {
71063f0e
WYG
6504 err = -EOPNOTSUPP;
6505 goto out_err;
6506 }
6507
6508 while (1) {
15e47304 6509 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
71063f0e
WYG
6510 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6511 NL80211_CMD_TESTMODE);
6512 struct nlattr *tmdata;
6513
9360ffd1 6514 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
71063f0e
WYG
6515 genlmsg_cancel(skb, hdr);
6516 break;
6517 }
6518
6519 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6520 if (!tmdata) {
6521 genlmsg_cancel(skb, hdr);
6522 break;
6523 }
e35e4d28 6524 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
71063f0e
WYG
6525 nla_nest_end(skb, tmdata);
6526
6527 if (err == -ENOBUFS || err == -ENOENT) {
6528 genlmsg_cancel(skb, hdr);
6529 break;
6530 } else if (err) {
6531 genlmsg_cancel(skb, hdr);
6532 goto out_err;
6533 }
6534
6535 genlmsg_end(skb, hdr);
6536 }
6537
6538 err = skb->len;
6539 /* see above */
6540 cb->args[0] = phy_idx + 1;
6541 out_err:
00918d33 6542 cfg80211_unlock_rdev(rdev);
71063f0e
WYG
6543 return err;
6544}
6545
aff89a9b
JB
6546static struct sk_buff *
6547__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
15e47304 6548 int approxlen, u32 portid, u32 seq, gfp_t gfp)
aff89a9b
JB
6549{
6550 struct sk_buff *skb;
6551 void *hdr;
6552 struct nlattr *data;
6553
6554 skb = nlmsg_new(approxlen + 100, gfp);
6555 if (!skb)
6556 return NULL;
6557
15e47304 6558 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
aff89a9b
JB
6559 if (!hdr) {
6560 kfree_skb(skb);
6561 return NULL;
6562 }
6563
9360ffd1
DM
6564 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6565 goto nla_put_failure;
aff89a9b
JB
6566 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6567
6568 ((void **)skb->cb)[0] = rdev;
6569 ((void **)skb->cb)[1] = hdr;
6570 ((void **)skb->cb)[2] = data;
6571
6572 return skb;
6573
6574 nla_put_failure:
6575 kfree_skb(skb);
6576 return NULL;
6577}
6578
6579struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6580 int approxlen)
6581{
6582 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6583
6584 if (WARN_ON(!rdev->testmode_info))
6585 return NULL;
6586
6587 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
15e47304 6588 rdev->testmode_info->snd_portid,
aff89a9b
JB
6589 rdev->testmode_info->snd_seq,
6590 GFP_KERNEL);
6591}
6592EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6593
6594int cfg80211_testmode_reply(struct sk_buff *skb)
6595{
6596 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6597 void *hdr = ((void **)skb->cb)[1];
6598 struct nlattr *data = ((void **)skb->cb)[2];
6599
6600 if (WARN_ON(!rdev->testmode_info)) {
6601 kfree_skb(skb);
6602 return -EINVAL;
6603 }
6604
6605 nla_nest_end(skb, data);
6606 genlmsg_end(skb, hdr);
6607 return genlmsg_reply(skb, rdev->testmode_info);
6608}
6609EXPORT_SYMBOL(cfg80211_testmode_reply);
6610
6611struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6612 int approxlen, gfp_t gfp)
6613{
6614 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6615
6616 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6617}
6618EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6619
6620void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6621{
6622 void *hdr = ((void **)skb->cb)[1];
6623 struct nlattr *data = ((void **)skb->cb)[2];
6624
6625 nla_nest_end(skb, data);
6626 genlmsg_end(skb, hdr);
6627 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6628}
6629EXPORT_SYMBOL(cfg80211_testmode_event);
6630#endif
6631
b23aa676
SO
6632static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6633{
4c476991
JB
6634 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6635 struct net_device *dev = info->user_ptr[1];
b23aa676
SO
6636 struct cfg80211_connect_params connect;
6637 struct wiphy *wiphy;
fffd0934 6638 struct cfg80211_cached_keys *connkeys = NULL;
b23aa676
SO
6639 int err;
6640
6641 memset(&connect, 0, sizeof(connect));
6642
6643 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6644 return -EINVAL;
6645
6646 if (!info->attrs[NL80211_ATTR_SSID] ||
6647 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6648 return -EINVAL;
6649
6650 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6651 connect.auth_type =
6652 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
e39e5b5e
JM
6653 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6654 NL80211_CMD_CONNECT))
b23aa676
SO
6655 return -EINVAL;
6656 } else
6657 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6658
6659 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6660
c0692b8f 6661 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
3dc27d25 6662 NL80211_MAX_NR_CIPHER_SUITES);
b23aa676
SO
6663 if (err)
6664 return err;
b23aa676 6665
074ac8df 6666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6668 return -EOPNOTSUPP;
b23aa676 6669
79c97e97 6670 wiphy = &rdev->wiphy;
b23aa676 6671
4486ea98
BS
6672 connect.bg_scan_period = -1;
6673 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6674 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6675 connect.bg_scan_period =
6676 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6677 }
6678
b23aa676
SO
6679 if (info->attrs[NL80211_ATTR_MAC])
6680 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6681 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6682 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6683
6684 if (info->attrs[NL80211_ATTR_IE]) {
6685 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6686 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6687 }
6688
cee00a95
JM
6689 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6690 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6691 if (connect.mfp != NL80211_MFP_REQUIRED &&
6692 connect.mfp != NL80211_MFP_NO)
6693 return -EINVAL;
6694 } else {
6695 connect.mfp = NL80211_MFP_NO;
6696 }
6697
b23aa676
SO
6698 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6699 connect.channel =
6700 ieee80211_get_channel(wiphy,
6701 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6702 if (!connect.channel ||
4c476991
JB
6703 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6704 return -EINVAL;
b23aa676
SO
6705 }
6706
fffd0934
JB
6707 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6708 connkeys = nl80211_parse_connkeys(rdev,
de7044ee 6709 info->attrs[NL80211_ATTR_KEYS], NULL);
4c476991
JB
6710 if (IS_ERR(connkeys))
6711 return PTR_ERR(connkeys);
fffd0934
JB
6712 }
6713
7e7c8926
BG
6714 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6715 connect.flags |= ASSOC_REQ_DISABLE_HT;
6716
6717 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6718 memcpy(&connect.ht_capa_mask,
6719 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6720 sizeof(connect.ht_capa_mask));
6721
6722 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
b4e4f47e
WY
6723 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6724 kfree(connkeys);
7e7c8926 6725 return -EINVAL;
b4e4f47e 6726 }
7e7c8926
BG
6727 memcpy(&connect.ht_capa,
6728 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6729 sizeof(connect.ht_capa));
6730 }
6731
ee2aca34
JB
6732 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6733 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6734
6735 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6736 memcpy(&connect.vht_capa_mask,
6737 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6738 sizeof(connect.vht_capa_mask));
6739
6740 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6741 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6742 kfree(connkeys);
6743 return -EINVAL;
6744 }
6745 memcpy(&connect.vht_capa,
6746 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6747 sizeof(connect.vht_capa));
6748 }
6749
fffd0934 6750 err = cfg80211_connect(rdev, dev, &connect, connkeys);
fffd0934
JB
6751 if (err)
6752 kfree(connkeys);
b23aa676
SO
6753 return err;
6754}
6755
6756static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6757{
4c476991
JB
6758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6759 struct net_device *dev = info->user_ptr[1];
b23aa676
SO
6760 u16 reason;
6761
6762 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6763 reason = WLAN_REASON_DEAUTH_LEAVING;
6764 else
6765 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6766
6767 if (reason == 0)
6768 return -EINVAL;
6769
074ac8df 6770 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6772 return -EOPNOTSUPP;
b23aa676 6773
4c476991 6774 return cfg80211_disconnect(rdev, dev, reason, true);
b23aa676
SO
6775}
6776
463d0183
JB
6777static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6778{
4c476991 6779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
463d0183
JB
6780 struct net *net;
6781 int err;
6782 u32 pid;
6783
6784 if (!info->attrs[NL80211_ATTR_PID])
6785 return -EINVAL;
6786
6787 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6788
463d0183 6789 net = get_net_ns_by_pid(pid);
4c476991
JB
6790 if (IS_ERR(net))
6791 return PTR_ERR(net);
463d0183
JB
6792
6793 err = 0;
6794
6795 /* check if anything to do */
4c476991
JB
6796 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6797 err = cfg80211_switch_netns(rdev, net);
463d0183 6798
463d0183 6799 put_net(net);
463d0183
JB
6800 return err;
6801}
6802
67fbb16b
SO
6803static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6804{
4c476991 6805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
67fbb16b
SO
6806 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6807 struct cfg80211_pmksa *pmksa) = NULL;
4c476991 6808 struct net_device *dev = info->user_ptr[1];
67fbb16b
SO
6809 struct cfg80211_pmksa pmksa;
6810
6811 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6812
6813 if (!info->attrs[NL80211_ATTR_MAC])
6814 return -EINVAL;
6815
6816 if (!info->attrs[NL80211_ATTR_PMKID])
6817 return -EINVAL;
6818
67fbb16b
SO
6819 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6820 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6821
074ac8df 6822 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6824 return -EOPNOTSUPP;
67fbb16b
SO
6825
6826 switch (info->genlhdr->cmd) {
6827 case NL80211_CMD_SET_PMKSA:
6828 rdev_ops = rdev->ops->set_pmksa;
6829 break;
6830 case NL80211_CMD_DEL_PMKSA:
6831 rdev_ops = rdev->ops->del_pmksa;
6832 break;
6833 default:
6834 WARN_ON(1);
6835 break;
6836 }
6837
4c476991
JB
6838 if (!rdev_ops)
6839 return -EOPNOTSUPP;
67fbb16b 6840
4c476991 6841 return rdev_ops(&rdev->wiphy, dev, &pmksa);
67fbb16b
SO
6842}
6843
6844static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6845{
4c476991
JB
6846 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6847 struct net_device *dev = info->user_ptr[1];
67fbb16b 6848
074ac8df 6849 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6850 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6851 return -EOPNOTSUPP;
67fbb16b 6852
4c476991
JB
6853 if (!rdev->ops->flush_pmksa)
6854 return -EOPNOTSUPP;
67fbb16b 6855
e35e4d28 6856 return rdev_flush_pmksa(rdev, dev);
67fbb16b
SO
6857}
6858
109086ce
AN
6859static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6860{
6861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6862 struct net_device *dev = info->user_ptr[1];
6863 u8 action_code, dialog_token;
6864 u16 status_code;
6865 u8 *peer;
6866
6867 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6868 !rdev->ops->tdls_mgmt)
6869 return -EOPNOTSUPP;
6870
6871 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6872 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6873 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6874 !info->attrs[NL80211_ATTR_IE] ||
6875 !info->attrs[NL80211_ATTR_MAC])
6876 return -EINVAL;
6877
6878 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6879 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6880 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6881 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6882
e35e4d28
HG
6883 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6884 dialog_token, status_code,
6885 nla_data(info->attrs[NL80211_ATTR_IE]),
6886 nla_len(info->attrs[NL80211_ATTR_IE]));
109086ce
AN
6887}
6888
6889static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6890{
6891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6892 struct net_device *dev = info->user_ptr[1];
6893 enum nl80211_tdls_operation operation;
6894 u8 *peer;
6895
6896 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6897 !rdev->ops->tdls_oper)
6898 return -EOPNOTSUPP;
6899
6900 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6901 !info->attrs[NL80211_ATTR_MAC])
6902 return -EINVAL;
6903
6904 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6905 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6906
e35e4d28 6907 return rdev_tdls_oper(rdev, dev, peer, operation);
109086ce
AN
6908}
6909
9588bbd5
JM
6910static int nl80211_remain_on_channel(struct sk_buff *skb,
6911 struct genl_info *info)
6912{
4c476991 6913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 6914 struct wireless_dev *wdev = info->user_ptr[1];
683b6d3b 6915 struct cfg80211_chan_def chandef;
9588bbd5
JM
6916 struct sk_buff *msg;
6917 void *hdr;
6918 u64 cookie;
683b6d3b 6919 u32 duration;
9588bbd5
JM
6920 int err;
6921
6922 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6923 !info->attrs[NL80211_ATTR_DURATION])
6924 return -EINVAL;
6925
6926 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6927
ebf348fc
JB
6928 if (!rdev->ops->remain_on_channel ||
6929 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
6930 return -EOPNOTSUPP;
6931
9588bbd5 6932 /*
ebf348fc
JB
6933 * We should be on that channel for at least a minimum amount of
6934 * time (10ms) but no longer than the driver supports.
9588bbd5 6935 */
ebf348fc 6936 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
a293911d 6937 duration > rdev->wiphy.max_remain_on_channel_duration)
9588bbd5
JM
6938 return -EINVAL;
6939
683b6d3b
JB
6940 err = nl80211_parse_chandef(rdev, info, &chandef);
6941 if (err)
6942 return err;
9588bbd5
JM
6943
6944 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
6945 if (!msg)
6946 return -ENOMEM;
9588bbd5 6947
15e47304 6948 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9588bbd5
JM
6949 NL80211_CMD_REMAIN_ON_CHANNEL);
6950
6951 if (IS_ERR(hdr)) {
6952 err = PTR_ERR(hdr);
6953 goto free_msg;
6954 }
6955
683b6d3b
JB
6956 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6957 duration, &cookie);
9588bbd5
JM
6958
6959 if (err)
6960 goto free_msg;
6961
9360ffd1
DM
6962 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6963 goto nla_put_failure;
9588bbd5
JM
6964
6965 genlmsg_end(msg, hdr);
4c476991
JB
6966
6967 return genlmsg_reply(msg, info);
9588bbd5
JM
6968
6969 nla_put_failure:
6970 err = -ENOBUFS;
6971 free_msg:
6972 nlmsg_free(msg);
9588bbd5
JM
6973 return err;
6974}
6975
6976static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6977 struct genl_info *info)
6978{
4c476991 6979 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 6980 struct wireless_dev *wdev = info->user_ptr[1];
9588bbd5 6981 u64 cookie;
9588bbd5
JM
6982
6983 if (!info->attrs[NL80211_ATTR_COOKIE])
6984 return -EINVAL;
6985
4c476991
JB
6986 if (!rdev->ops->cancel_remain_on_channel)
6987 return -EOPNOTSUPP;
9588bbd5 6988
9588bbd5
JM
6989 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6990
e35e4d28 6991 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
9588bbd5
JM
6992}
6993
13ae75b1
JM
6994static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6995 u8 *rates, u8 rates_len)
6996{
6997 u8 i;
6998 u32 mask = 0;
6999
7000 for (i = 0; i < rates_len; i++) {
7001 int rate = (rates[i] & 0x7f) * 5;
7002 int ridx;
7003 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7004 struct ieee80211_rate *srate =
7005 &sband->bitrates[ridx];
7006 if (rate == srate->bitrate) {
7007 mask |= 1 << ridx;
7008 break;
7009 }
7010 }
7011 if (ridx == sband->n_bitrates)
7012 return 0; /* rate not found */
7013 }
7014
7015 return mask;
7016}
7017
24db78c0
SW
7018static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7019 u8 *rates, u8 rates_len,
7020 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7021{
7022 u8 i;
7023
7024 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7025
7026 for (i = 0; i < rates_len; i++) {
7027 int ridx, rbit;
7028
7029 ridx = rates[i] / 8;
7030 rbit = BIT(rates[i] % 8);
7031
7032 /* check validity */
910570b5 7033 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
24db78c0
SW
7034 return false;
7035
7036 /* check availability */
7037 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7038 mcs[ridx] |= rbit;
7039 else
7040 return false;
7041 }
7042
7043 return true;
7044}
7045
b54452b0 7046static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
13ae75b1
JM
7047 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7048 .len = NL80211_MAX_SUPP_RATES },
24db78c0
SW
7049 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7050 .len = NL80211_MAX_SUPP_HT_RATES },
13ae75b1
JM
7051};
7052
7053static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7054 struct genl_info *info)
7055{
7056 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4c476991 7057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
13ae75b1 7058 struct cfg80211_bitrate_mask mask;
4c476991
JB
7059 int rem, i;
7060 struct net_device *dev = info->user_ptr[1];
13ae75b1
JM
7061 struct nlattr *tx_rates;
7062 struct ieee80211_supported_band *sband;
7063
7064 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7065 return -EINVAL;
7066
4c476991
JB
7067 if (!rdev->ops->set_bitrate_mask)
7068 return -EOPNOTSUPP;
13ae75b1
JM
7069
7070 memset(&mask, 0, sizeof(mask));
7071 /* Default to all rates enabled */
7072 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7073 sband = rdev->wiphy.bands[i];
7074 mask.control[i].legacy =
7075 sband ? (1 << sband->n_bitrates) - 1 : 0;
24db78c0
SW
7076 if (sband)
7077 memcpy(mask.control[i].mcs,
7078 sband->ht_cap.mcs.rx_mask,
7079 sizeof(mask.control[i].mcs));
7080 else
7081 memset(mask.control[i].mcs, 0,
7082 sizeof(mask.control[i].mcs));
13ae75b1
JM
7083 }
7084
7085 /*
7086 * The nested attribute uses enum nl80211_band as the index. This maps
7087 * directly to the enum ieee80211_band values used in cfg80211.
7088 */
24db78c0 7089 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
13ae75b1
JM
7090 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7091 {
7092 enum ieee80211_band band = nla_type(tx_rates);
4c476991
JB
7093 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7094 return -EINVAL;
13ae75b1 7095 sband = rdev->wiphy.bands[band];
4c476991
JB
7096 if (sband == NULL)
7097 return -EINVAL;
13ae75b1
JM
7098 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7099 nla_len(tx_rates), nl80211_txattr_policy);
7100 if (tb[NL80211_TXRATE_LEGACY]) {
7101 mask.control[band].legacy = rateset_to_mask(
7102 sband,
7103 nla_data(tb[NL80211_TXRATE_LEGACY]),
7104 nla_len(tb[NL80211_TXRATE_LEGACY]));
218d2e26
BS
7105 if ((mask.control[band].legacy == 0) &&
7106 nla_len(tb[NL80211_TXRATE_LEGACY]))
7107 return -EINVAL;
24db78c0
SW
7108 }
7109 if (tb[NL80211_TXRATE_MCS]) {
7110 if (!ht_rateset_to_mask(
7111 sband,
7112 nla_data(tb[NL80211_TXRATE_MCS]),
7113 nla_len(tb[NL80211_TXRATE_MCS]),
7114 mask.control[band].mcs))
7115 return -EINVAL;
7116 }
7117
7118 if (mask.control[band].legacy == 0) {
7119 /* don't allow empty legacy rates if HT
7120 * is not even supported. */
7121 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7122 return -EINVAL;
7123
7124 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7125 if (mask.control[band].mcs[i])
7126 break;
7127
7128 /* legacy and mcs rates may not be both empty */
7129 if (i == IEEE80211_HT_MCS_MASK_LEN)
4c476991 7130 return -EINVAL;
13ae75b1
JM
7131 }
7132 }
7133
e35e4d28 7134 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
13ae75b1
JM
7135}
7136
2e161f78 7137static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
026331c4 7138{
4c476991 7139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 7140 struct wireless_dev *wdev = info->user_ptr[1];
2e161f78 7141 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
026331c4
JM
7142
7143 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7144 return -EINVAL;
7145
2e161f78
JB
7146 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7147 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
026331c4 7148
71bbc994
JB
7149 switch (wdev->iftype) {
7150 case NL80211_IFTYPE_STATION:
7151 case NL80211_IFTYPE_ADHOC:
7152 case NL80211_IFTYPE_P2P_CLIENT:
7153 case NL80211_IFTYPE_AP:
7154 case NL80211_IFTYPE_AP_VLAN:
7155 case NL80211_IFTYPE_MESH_POINT:
7156 case NL80211_IFTYPE_P2P_GO:
98104fde 7157 case NL80211_IFTYPE_P2P_DEVICE:
71bbc994
JB
7158 break;
7159 default:
4c476991 7160 return -EOPNOTSUPP;
71bbc994 7161 }
026331c4
JM
7162
7163 /* not much point in registering if we can't reply */
4c476991
JB
7164 if (!rdev->ops->mgmt_tx)
7165 return -EOPNOTSUPP;
026331c4 7166
15e47304 7167 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
026331c4
JM
7168 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7169 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
026331c4
JM
7170}
7171
2e161f78 7172static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
026331c4 7173{
4c476991 7174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 7175 struct wireless_dev *wdev = info->user_ptr[1];
683b6d3b 7176 struct cfg80211_chan_def chandef;
026331c4 7177 int err;
d64d373f 7178 void *hdr = NULL;
026331c4 7179 u64 cookie;
e247bd90 7180 struct sk_buff *msg = NULL;
f7ca38df 7181 unsigned int wait = 0;
e247bd90
JB
7182 bool offchan, no_cck, dont_wait_for_ack;
7183
7184 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
026331c4 7185
683b6d3b 7186 if (!info->attrs[NL80211_ATTR_FRAME])
026331c4
JM
7187 return -EINVAL;
7188
4c476991
JB
7189 if (!rdev->ops->mgmt_tx)
7190 return -EOPNOTSUPP;
026331c4 7191
71bbc994
JB
7192 switch (wdev->iftype) {
7193 case NL80211_IFTYPE_STATION:
7194 case NL80211_IFTYPE_ADHOC:
7195 case NL80211_IFTYPE_P2P_CLIENT:
7196 case NL80211_IFTYPE_AP:
7197 case NL80211_IFTYPE_AP_VLAN:
7198 case NL80211_IFTYPE_MESH_POINT:
7199 case NL80211_IFTYPE_P2P_GO:
98104fde 7200 case NL80211_IFTYPE_P2P_DEVICE:
71bbc994
JB
7201 break;
7202 default:
4c476991 7203 return -EOPNOTSUPP;
71bbc994 7204 }
026331c4 7205
f7ca38df 7206 if (info->attrs[NL80211_ATTR_DURATION]) {
7c4ef712 7207 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
f7ca38df
JB
7208 return -EINVAL;
7209 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
ebf348fc
JB
7210
7211 /*
7212 * We should wait on the channel for at least a minimum amount
7213 * of time (10ms) but no longer than the driver supports.
7214 */
7215 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7216 wait > rdev->wiphy.max_remain_on_channel_duration)
7217 return -EINVAL;
7218
f7ca38df
JB
7219 }
7220
f7ca38df
JB
7221 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7222
7c4ef712
JB
7223 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7224 return -EINVAL;
7225
e9f935e3
RM
7226 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7227
683b6d3b
JB
7228 err = nl80211_parse_chandef(rdev, info, &chandef);
7229 if (err)
7230 return err;
026331c4 7231
e247bd90
JB
7232 if (!dont_wait_for_ack) {
7233 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7234 if (!msg)
7235 return -ENOMEM;
026331c4 7236
15e47304 7237 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
e247bd90 7238 NL80211_CMD_FRAME);
026331c4 7239
e247bd90
JB
7240 if (IS_ERR(hdr)) {
7241 err = PTR_ERR(hdr);
7242 goto free_msg;
7243 }
026331c4 7244 }
e247bd90 7245
683b6d3b 7246 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
2e161f78
JB
7247 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7248 nla_len(info->attrs[NL80211_ATTR_FRAME]),
e247bd90 7249 no_cck, dont_wait_for_ack, &cookie);
026331c4
JM
7250 if (err)
7251 goto free_msg;
7252
e247bd90 7253 if (msg) {
9360ffd1
DM
7254 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7255 goto nla_put_failure;
026331c4 7256
e247bd90
JB
7257 genlmsg_end(msg, hdr);
7258 return genlmsg_reply(msg, info);
7259 }
7260
7261 return 0;
026331c4
JM
7262
7263 nla_put_failure:
7264 err = -ENOBUFS;
7265 free_msg:
7266 nlmsg_free(msg);
026331c4
JM
7267 return err;
7268}
7269
f7ca38df
JB
7270static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7271{
7272 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 7273 struct wireless_dev *wdev = info->user_ptr[1];
f7ca38df
JB
7274 u64 cookie;
7275
7276 if (!info->attrs[NL80211_ATTR_COOKIE])
7277 return -EINVAL;
7278
7279 if (!rdev->ops->mgmt_tx_cancel_wait)
7280 return -EOPNOTSUPP;
7281
71bbc994
JB
7282 switch (wdev->iftype) {
7283 case NL80211_IFTYPE_STATION:
7284 case NL80211_IFTYPE_ADHOC:
7285 case NL80211_IFTYPE_P2P_CLIENT:
7286 case NL80211_IFTYPE_AP:
7287 case NL80211_IFTYPE_AP_VLAN:
7288 case NL80211_IFTYPE_P2P_GO:
98104fde 7289 case NL80211_IFTYPE_P2P_DEVICE:
71bbc994
JB
7290 break;
7291 default:
f7ca38df 7292 return -EOPNOTSUPP;
71bbc994 7293 }
f7ca38df
JB
7294
7295 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7296
e35e4d28 7297 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
f7ca38df
JB
7298}
7299
ffb9eb3d
KV
7300static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7301{
4c476991 7302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
ffb9eb3d 7303 struct wireless_dev *wdev;
4c476991 7304 struct net_device *dev = info->user_ptr[1];
ffb9eb3d
KV
7305 u8 ps_state;
7306 bool state;
7307 int err;
7308
4c476991
JB
7309 if (!info->attrs[NL80211_ATTR_PS_STATE])
7310 return -EINVAL;
ffb9eb3d
KV
7311
7312 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7313
4c476991
JB
7314 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7315 return -EINVAL;
ffb9eb3d
KV
7316
7317 wdev = dev->ieee80211_ptr;
7318
4c476991
JB
7319 if (!rdev->ops->set_power_mgmt)
7320 return -EOPNOTSUPP;
ffb9eb3d
KV
7321
7322 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7323
7324 if (state == wdev->ps)
4c476991 7325 return 0;
ffb9eb3d 7326
e35e4d28 7327 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
4c476991
JB
7328 if (!err)
7329 wdev->ps = state;
ffb9eb3d
KV
7330 return err;
7331}
7332
7333static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7334{
4c476991 7335 struct cfg80211_registered_device *rdev = info->user_ptr[0];
ffb9eb3d
KV
7336 enum nl80211_ps_state ps_state;
7337 struct wireless_dev *wdev;
4c476991 7338 struct net_device *dev = info->user_ptr[1];
ffb9eb3d
KV
7339 struct sk_buff *msg;
7340 void *hdr;
7341 int err;
7342
ffb9eb3d
KV
7343 wdev = dev->ieee80211_ptr;
7344
4c476991
JB
7345 if (!rdev->ops->set_power_mgmt)
7346 return -EOPNOTSUPP;
ffb9eb3d
KV
7347
7348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
7349 if (!msg)
7350 return -ENOMEM;
ffb9eb3d 7351
15e47304 7352 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
ffb9eb3d
KV
7353 NL80211_CMD_GET_POWER_SAVE);
7354 if (!hdr) {
4c476991 7355 err = -ENOBUFS;
ffb9eb3d
KV
7356 goto free_msg;
7357 }
7358
7359 if (wdev->ps)
7360 ps_state = NL80211_PS_ENABLED;
7361 else
7362 ps_state = NL80211_PS_DISABLED;
7363
9360ffd1
DM
7364 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7365 goto nla_put_failure;
ffb9eb3d
KV
7366
7367 genlmsg_end(msg, hdr);
4c476991 7368 return genlmsg_reply(msg, info);
ffb9eb3d 7369
4c476991 7370 nla_put_failure:
ffb9eb3d 7371 err = -ENOBUFS;
4c476991 7372 free_msg:
ffb9eb3d 7373 nlmsg_free(msg);
ffb9eb3d
KV
7374 return err;
7375}
7376
d6dc1a38
JO
7377static struct nla_policy
7378nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7379 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7380 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7381 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
84f10708
TP
7382 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7383 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7384 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
d6dc1a38
JO
7385};
7386
84f10708 7387static int nl80211_set_cqm_txe(struct genl_info *info,
d9d8b019 7388 u32 rate, u32 pkts, u32 intvl)
84f10708
TP
7389{
7390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7391 struct wireless_dev *wdev;
7392 struct net_device *dev = info->user_ptr[1];
7393
d9d8b019 7394 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
84f10708
TP
7395 return -EINVAL;
7396
7397 wdev = dev->ieee80211_ptr;
7398
7399 if (!rdev->ops->set_cqm_txe_config)
7400 return -EOPNOTSUPP;
7401
7402 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7403 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7404 return -EOPNOTSUPP;
7405
e35e4d28 7406 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
84f10708
TP
7407}
7408
d6dc1a38
JO
7409static int nl80211_set_cqm_rssi(struct genl_info *info,
7410 s32 threshold, u32 hysteresis)
7411{
4c476991 7412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
d6dc1a38 7413 struct wireless_dev *wdev;
4c476991 7414 struct net_device *dev = info->user_ptr[1];
d6dc1a38
JO
7415
7416 if (threshold > 0)
7417 return -EINVAL;
7418
d6dc1a38
JO
7419 wdev = dev->ieee80211_ptr;
7420
4c476991
JB
7421 if (!rdev->ops->set_cqm_rssi_config)
7422 return -EOPNOTSUPP;
d6dc1a38 7423
074ac8df 7424 if (wdev->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
7425 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7426 return -EOPNOTSUPP;
d6dc1a38 7427
e35e4d28 7428 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
d6dc1a38
JO
7429}
7430
7431static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7432{
7433 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7434 struct nlattr *cqm;
7435 int err;
7436
7437 cqm = info->attrs[NL80211_ATTR_CQM];
7438 if (!cqm) {
7439 err = -EINVAL;
7440 goto out;
7441 }
7442
7443 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7444 nl80211_attr_cqm_policy);
7445 if (err)
7446 goto out;
7447
7448 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7449 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7450 s32 threshold;
7451 u32 hysteresis;
7452 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7453 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7454 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
84f10708
TP
7455 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7456 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7457 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7458 u32 rate, pkts, intvl;
7459 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7460 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7461 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7462 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
d6dc1a38
JO
7463 } else
7464 err = -EINVAL;
7465
7466out:
7467 return err;
7468}
7469
29cbe68c
JB
7470static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7471{
7472 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7473 struct net_device *dev = info->user_ptr[1];
7474 struct mesh_config cfg;
c80d545d 7475 struct mesh_setup setup;
29cbe68c
JB
7476 int err;
7477
7478 /* start with default */
7479 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
c80d545d 7480 memcpy(&setup, &default_mesh_setup, sizeof(setup));
29cbe68c 7481
24bdd9f4 7482 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
29cbe68c 7483 /* and parse parameters if given */
24bdd9f4 7484 err = nl80211_parse_mesh_config(info, &cfg, NULL);
29cbe68c
JB
7485 if (err)
7486 return err;
7487 }
7488
7489 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7490 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7491 return -EINVAL;
7492
c80d545d
JC
7493 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7494 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7495
4bb62344
CYY
7496 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7497 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7498 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7499 return -EINVAL;
7500
9bdbf04d
MP
7501 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7502 setup.beacon_interval =
7503 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7504 if (setup.beacon_interval < 10 ||
7505 setup.beacon_interval > 10000)
7506 return -EINVAL;
7507 }
7508
7509 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7510 setup.dtim_period =
7511 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7512 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7513 return -EINVAL;
7514 }
7515
c80d545d
JC
7516 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7517 /* parse additional setup parameters if given */
7518 err = nl80211_parse_mesh_setup(info, &setup);
7519 if (err)
7520 return err;
7521 }
7522
d37bb18a
TP
7523 if (setup.user_mpm)
7524 cfg.auto_open_plinks = false;
7525
cc1d2806 7526 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
683b6d3b
JB
7527 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7528 if (err)
7529 return err;
cc1d2806
JB
7530 } else {
7531 /* cfg80211_join_mesh() will sort it out */
683b6d3b 7532 setup.chandef.chan = NULL;
cc1d2806
JB
7533 }
7534
c80d545d 7535 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
29cbe68c
JB
7536}
7537
7538static int nl80211_leave_mesh(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 return cfg80211_leave_mesh(rdev, dev);
7544}
7545
dfb89c56 7546#ifdef CONFIG_PM
bb92d199
AK
7547static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7548 struct cfg80211_registered_device *rdev)
7549{
7550 struct nlattr *nl_pats, *nl_pat;
7551 int i, pat_len;
7552
7553 if (!rdev->wowlan->n_patterns)
7554 return 0;
7555
7556 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7557 if (!nl_pats)
7558 return -ENOBUFS;
7559
7560 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
7561 nl_pat = nla_nest_start(msg, i + 1);
7562 if (!nl_pat)
7563 return -ENOBUFS;
7564 pat_len = rdev->wowlan->patterns[i].pattern_len;
7565 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7566 DIV_ROUND_UP(pat_len, 8),
7567 rdev->wowlan->patterns[i].mask) ||
7568 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
7569 pat_len, rdev->wowlan->patterns[i].pattern) ||
7570 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
7571 rdev->wowlan->patterns[i].pkt_offset))
7572 return -ENOBUFS;
7573 nla_nest_end(msg, nl_pat);
7574 }
7575 nla_nest_end(msg, nl_pats);
7576
7577 return 0;
7578}
7579
2a0e047e
JB
7580static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7581 struct cfg80211_wowlan_tcp *tcp)
7582{
7583 struct nlattr *nl_tcp;
7584
7585 if (!tcp)
7586 return 0;
7587
7588 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7589 if (!nl_tcp)
7590 return -ENOBUFS;
7591
7592 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7593 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7594 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7595 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7596 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7597 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7598 tcp->payload_len, tcp->payload) ||
7599 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7600 tcp->data_interval) ||
7601 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7602 tcp->wake_len, tcp->wake_data) ||
7603 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7604 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7605 return -ENOBUFS;
7606
7607 if (tcp->payload_seq.len &&
7608 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7609 sizeof(tcp->payload_seq), &tcp->payload_seq))
7610 return -ENOBUFS;
7611
7612 if (tcp->payload_tok.len &&
7613 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7614 sizeof(tcp->payload_tok) + tcp->tokens_size,
7615 &tcp->payload_tok))
7616 return -ENOBUFS;
7617
7618 return 0;
7619}
7620
ff1b6e69
JB
7621static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7622{
7623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7624 struct sk_buff *msg;
7625 void *hdr;
2a0e047e 7626 u32 size = NLMSG_DEFAULT_SIZE;
ff1b6e69 7627
2a0e047e
JB
7628 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7629 !rdev->wiphy.wowlan.tcp)
ff1b6e69
JB
7630 return -EOPNOTSUPP;
7631
2a0e047e
JB
7632 if (rdev->wowlan && rdev->wowlan->tcp) {
7633 /* adjust size to have room for all the data */
7634 size += rdev->wowlan->tcp->tokens_size +
7635 rdev->wowlan->tcp->payload_len +
7636 rdev->wowlan->tcp->wake_len +
7637 rdev->wowlan->tcp->wake_len / 8;
7638 }
7639
7640 msg = nlmsg_new(size, GFP_KERNEL);
ff1b6e69
JB
7641 if (!msg)
7642 return -ENOMEM;
7643
15e47304 7644 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
ff1b6e69
JB
7645 NL80211_CMD_GET_WOWLAN);
7646 if (!hdr)
7647 goto nla_put_failure;
7648
7649 if (rdev->wowlan) {
7650 struct nlattr *nl_wowlan;
7651
7652 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7653 if (!nl_wowlan)
7654 goto nla_put_failure;
7655
9360ffd1
DM
7656 if ((rdev->wowlan->any &&
7657 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7658 (rdev->wowlan->disconnect &&
7659 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7660 (rdev->wowlan->magic_pkt &&
7661 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7662 (rdev->wowlan->gtk_rekey_failure &&
7663 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7664 (rdev->wowlan->eap_identity_req &&
7665 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7666 (rdev->wowlan->four_way_handshake &&
7667 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7668 (rdev->wowlan->rfkill_release &&
7669 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7670 goto nla_put_failure;
2a0e047e 7671
bb92d199
AK
7672 if (nl80211_send_wowlan_patterns(msg, rdev))
7673 goto nla_put_failure;
2a0e047e
JB
7674
7675 if (nl80211_send_wowlan_tcp(msg, rdev->wowlan->tcp))
7676 goto nla_put_failure;
7677
ff1b6e69
JB
7678 nla_nest_end(msg, nl_wowlan);
7679 }
7680
7681 genlmsg_end(msg, hdr);
7682 return genlmsg_reply(msg, info);
7683
7684nla_put_failure:
7685 nlmsg_free(msg);
7686 return -ENOBUFS;
7687}
7688
2a0e047e
JB
7689static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7690 struct nlattr *attr,
7691 struct cfg80211_wowlan *trig)
7692{
7693 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7694 struct cfg80211_wowlan_tcp *cfg;
7695 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7696 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7697 u32 size;
7698 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7699 int err, port;
7700
7701 if (!rdev->wiphy.wowlan.tcp)
7702 return -EINVAL;
7703
7704 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7705 nla_data(attr), nla_len(attr),
7706 nl80211_wowlan_tcp_policy);
7707 if (err)
7708 return err;
7709
7710 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7711 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7712 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7713 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7714 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7715 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7716 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7717 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7718 return -EINVAL;
7719
7720 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
7721 if (data_size > rdev->wiphy.wowlan.tcp->data_payload_max)
7722 return -EINVAL;
7723
7724 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
723d568a
JB
7725 rdev->wiphy.wowlan.tcp->data_interval_max ||
7726 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
2a0e047e
JB
7727 return -EINVAL;
7728
7729 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
7730 if (wake_size > rdev->wiphy.wowlan.tcp->wake_payload_max)
7731 return -EINVAL;
7732
7733 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7734 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7735 return -EINVAL;
7736
7737 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7738 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7739
7740 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7741 tokens_size = tokln - sizeof(*tok);
7742
7743 if (!tok->len || tokens_size % tok->len)
7744 return -EINVAL;
7745 if (!rdev->wiphy.wowlan.tcp->tok)
7746 return -EINVAL;
7747 if (tok->len > rdev->wiphy.wowlan.tcp->tok->max_len)
7748 return -EINVAL;
7749 if (tok->len < rdev->wiphy.wowlan.tcp->tok->min_len)
7750 return -EINVAL;
7751 if (tokens_size > rdev->wiphy.wowlan.tcp->tok->bufsize)
7752 return -EINVAL;
7753 if (tok->offset + tok->len > data_size)
7754 return -EINVAL;
7755 }
7756
7757 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7758 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
7759 if (!rdev->wiphy.wowlan.tcp->seq)
7760 return -EINVAL;
7761 if (seq->len == 0 || seq->len > 4)
7762 return -EINVAL;
7763 if (seq->len + seq->offset > data_size)
7764 return -EINVAL;
7765 }
7766
7767 size = sizeof(*cfg);
7768 size += data_size;
7769 size += wake_size + wake_mask_size;
7770 size += tokens_size;
7771
7772 cfg = kzalloc(size, GFP_KERNEL);
7773 if (!cfg)
7774 return -ENOMEM;
7775 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7776 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7777 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7778 ETH_ALEN);
7779 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7780 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7781 else
7782 port = 0;
7783#ifdef CONFIG_INET
7784 /* allocate a socket and port for it and use it */
7785 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7786 IPPROTO_TCP, &cfg->sock, 1);
7787 if (err) {
7788 kfree(cfg);
7789 return err;
7790 }
7791 if (inet_csk_get_port(cfg->sock->sk, port)) {
7792 sock_release(cfg->sock);
7793 kfree(cfg);
7794 return -EADDRINUSE;
7795 }
7796 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7797#else
7798 if (!port) {
7799 kfree(cfg);
7800 return -EINVAL;
7801 }
7802 cfg->src_port = port;
7803#endif
7804
7805 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7806 cfg->payload_len = data_size;
7807 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7808 memcpy((void *)cfg->payload,
7809 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7810 data_size);
7811 if (seq)
7812 cfg->payload_seq = *seq;
7813 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7814 cfg->wake_len = wake_size;
7815 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7816 memcpy((void *)cfg->wake_data,
7817 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7818 wake_size);
7819 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7820 data_size + wake_size;
7821 memcpy((void *)cfg->wake_mask,
7822 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7823 wake_mask_size);
7824 if (tok) {
7825 cfg->tokens_size = tokens_size;
7826 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7827 }
7828
7829 trig->tcp = cfg;
7830
7831 return 0;
7832}
7833
ff1b6e69
JB
7834static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7835{
7836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7837 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
ff1b6e69 7838 struct cfg80211_wowlan new_triggers = {};
ae33bd81 7839 struct cfg80211_wowlan *ntrig;
ff1b6e69
JB
7840 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
7841 int err, i;
6d52563f 7842 bool prev_enabled = rdev->wowlan;
ff1b6e69 7843
2a0e047e
JB
7844 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns &&
7845 !rdev->wiphy.wowlan.tcp)
ff1b6e69
JB
7846 return -EOPNOTSUPP;
7847
ae33bd81
JB
7848 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7849 cfg80211_rdev_free_wowlan(rdev);
7850 rdev->wowlan = NULL;
7851 goto set_wakeup;
7852 }
ff1b6e69
JB
7853
7854 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7855 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7856 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7857 nl80211_wowlan_policy);
7858 if (err)
7859 return err;
7860
7861 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7862 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7863 return -EINVAL;
7864 new_triggers.any = true;
7865 }
7866
7867 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7868 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7869 return -EINVAL;
7870 new_triggers.disconnect = true;
7871 }
7872
7873 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7874 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7875 return -EINVAL;
7876 new_triggers.magic_pkt = true;
7877 }
7878
77dbbb13
JB
7879 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7880 return -EINVAL;
7881
7882 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7883 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7884 return -EINVAL;
7885 new_triggers.gtk_rekey_failure = true;
7886 }
7887
7888 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7889 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7890 return -EINVAL;
7891 new_triggers.eap_identity_req = true;
7892 }
7893
7894 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7895 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7896 return -EINVAL;
7897 new_triggers.four_way_handshake = true;
7898 }
7899
7900 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7901 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7902 return -EINVAL;
7903 new_triggers.rfkill_release = true;
7904 }
7905
ff1b6e69
JB
7906 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7907 struct nlattr *pat;
7908 int n_patterns = 0;
bb92d199 7909 int rem, pat_len, mask_len, pkt_offset;
ff1b6e69
JB
7910 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7911
7912 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7913 rem)
7914 n_patterns++;
7915 if (n_patterns > wowlan->n_patterns)
7916 return -EINVAL;
7917
7918 new_triggers.patterns = kcalloc(n_patterns,
7919 sizeof(new_triggers.patterns[0]),
7920 GFP_KERNEL);
7921 if (!new_triggers.patterns)
7922 return -ENOMEM;
7923
7924 new_triggers.n_patterns = n_patterns;
7925 i = 0;
7926
7927 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7928 rem) {
7929 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7930 nla_data(pat), nla_len(pat), NULL);
7931 err = -EINVAL;
7932 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7933 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7934 goto error;
7935 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7936 mask_len = DIV_ROUND_UP(pat_len, 8);
7937 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7938 mask_len)
7939 goto error;
7940 if (pat_len > wowlan->pattern_max_len ||
7941 pat_len < wowlan->pattern_min_len)
7942 goto error;
7943
bb92d199
AK
7944 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7945 pkt_offset = 0;
7946 else
7947 pkt_offset = nla_get_u32(
7948 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7949 if (pkt_offset > wowlan->max_pkt_offset)
7950 goto error;
7951 new_triggers.patterns[i].pkt_offset = pkt_offset;
7952
ff1b6e69
JB
7953 new_triggers.patterns[i].mask =
7954 kmalloc(mask_len + pat_len, GFP_KERNEL);
7955 if (!new_triggers.patterns[i].mask) {
7956 err = -ENOMEM;
7957 goto error;
7958 }
7959 new_triggers.patterns[i].pattern =
7960 new_triggers.patterns[i].mask + mask_len;
7961 memcpy(new_triggers.patterns[i].mask,
7962 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7963 mask_len);
7964 new_triggers.patterns[i].pattern_len = pat_len;
7965 memcpy(new_triggers.patterns[i].pattern,
7966 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7967 pat_len);
7968 i++;
7969 }
7970 }
7971
2a0e047e
JB
7972 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7973 err = nl80211_parse_wowlan_tcp(
7974 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7975 &new_triggers);
7976 if (err)
7977 goto error;
7978 }
7979
ae33bd81
JB
7980 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7981 if (!ntrig) {
7982 err = -ENOMEM;
7983 goto error;
ff1b6e69 7984 }
ae33bd81
JB
7985 cfg80211_rdev_free_wowlan(rdev);
7986 rdev->wowlan = ntrig;
ff1b6e69 7987
ae33bd81 7988 set_wakeup:
6d52563f 7989 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
e35e4d28 7990 rdev_set_wakeup(rdev, rdev->wowlan);
6d52563f 7991
ff1b6e69
JB
7992 return 0;
7993 error:
7994 for (i = 0; i < new_triggers.n_patterns; i++)
7995 kfree(new_triggers.patterns[i].mask);
7996 kfree(new_triggers.patterns);
2a0e047e
JB
7997 if (new_triggers.tcp && new_triggers.tcp->sock)
7998 sock_release(new_triggers.tcp->sock);
7999 kfree(new_triggers.tcp);
ff1b6e69
JB
8000 return err;
8001}
dfb89c56 8002#endif
ff1b6e69 8003
e5497d76
JB
8004static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8005{
8006 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8007 struct net_device *dev = info->user_ptr[1];
8008 struct wireless_dev *wdev = dev->ieee80211_ptr;
8009 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8010 struct cfg80211_gtk_rekey_data rekey_data;
8011 int err;
8012
8013 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8014 return -EINVAL;
8015
8016 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8017 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8018 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8019 nl80211_rekey_policy);
8020 if (err)
8021 return err;
8022
8023 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8024 return -ERANGE;
8025 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8026 return -ERANGE;
8027 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8028 return -ERANGE;
8029
8030 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8031 NL80211_KEK_LEN);
8032 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8033 NL80211_KCK_LEN);
8034 memcpy(rekey_data.replay_ctr,
8035 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8036 NL80211_REPLAY_CTR_LEN);
8037
8038 wdev_lock(wdev);
8039 if (!wdev->current_bss) {
8040 err = -ENOTCONN;
8041 goto out;
8042 }
8043
8044 if (!rdev->ops->set_rekey_data) {
8045 err = -EOPNOTSUPP;
8046 goto out;
8047 }
8048
e35e4d28 8049 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
e5497d76
JB
8050 out:
8051 wdev_unlock(wdev);
8052 return err;
8053}
8054
28946da7
JB
8055static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8056 struct genl_info *info)
8057{
8058 struct net_device *dev = info->user_ptr[1];
8059 struct wireless_dev *wdev = dev->ieee80211_ptr;
8060
8061 if (wdev->iftype != NL80211_IFTYPE_AP &&
8062 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8063 return -EINVAL;
8064
15e47304 8065 if (wdev->ap_unexpected_nlportid)
28946da7
JB
8066 return -EBUSY;
8067
15e47304 8068 wdev->ap_unexpected_nlportid = info->snd_portid;
28946da7
JB
8069 return 0;
8070}
8071
7f6cf311
JB
8072static int nl80211_probe_client(struct sk_buff *skb,
8073 struct genl_info *info)
8074{
8075 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8076 struct net_device *dev = info->user_ptr[1];
8077 struct wireless_dev *wdev = dev->ieee80211_ptr;
8078 struct sk_buff *msg;
8079 void *hdr;
8080 const u8 *addr;
8081 u64 cookie;
8082 int err;
8083
8084 if (wdev->iftype != NL80211_IFTYPE_AP &&
8085 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8086 return -EOPNOTSUPP;
8087
8088 if (!info->attrs[NL80211_ATTR_MAC])
8089 return -EINVAL;
8090
8091 if (!rdev->ops->probe_client)
8092 return -EOPNOTSUPP;
8093
8094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8095 if (!msg)
8096 return -ENOMEM;
8097
15e47304 8098 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7f6cf311
JB
8099 NL80211_CMD_PROBE_CLIENT);
8100
8101 if (IS_ERR(hdr)) {
8102 err = PTR_ERR(hdr);
8103 goto free_msg;
8104 }
8105
8106 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8107
e35e4d28 8108 err = rdev_probe_client(rdev, dev, addr, &cookie);
7f6cf311
JB
8109 if (err)
8110 goto free_msg;
8111
9360ffd1
DM
8112 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8113 goto nla_put_failure;
7f6cf311
JB
8114
8115 genlmsg_end(msg, hdr);
8116
8117 return genlmsg_reply(msg, info);
8118
8119 nla_put_failure:
8120 err = -ENOBUFS;
8121 free_msg:
8122 nlmsg_free(msg);
8123 return err;
8124}
8125
5e760230
JB
8126static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8127{
8128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
37c73b5f
BG
8129 struct cfg80211_beacon_registration *reg, *nreg;
8130 int rv;
5e760230
JB
8131
8132 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8133 return -EOPNOTSUPP;
8134
37c73b5f
BG
8135 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8136 if (!nreg)
8137 return -ENOMEM;
8138
8139 /* First, check if already registered. */
8140 spin_lock_bh(&rdev->beacon_registrations_lock);
8141 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8142 if (reg->nlportid == info->snd_portid) {
8143 rv = -EALREADY;
8144 goto out_err;
8145 }
8146 }
8147 /* Add it to the list */
8148 nreg->nlportid = info->snd_portid;
8149 list_add(&nreg->list, &rdev->beacon_registrations);
5e760230 8150
37c73b5f 8151 spin_unlock_bh(&rdev->beacon_registrations_lock);
5e760230
JB
8152
8153 return 0;
37c73b5f
BG
8154out_err:
8155 spin_unlock_bh(&rdev->beacon_registrations_lock);
8156 kfree(nreg);
8157 return rv;
5e760230
JB
8158}
8159
98104fde
JB
8160static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8161{
8162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8163 struct wireless_dev *wdev = info->user_ptr[1];
8164 int err;
8165
8166 if (!rdev->ops->start_p2p_device)
8167 return -EOPNOTSUPP;
8168
8169 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8170 return -EOPNOTSUPP;
8171
8172 if (wdev->p2p_started)
8173 return 0;
8174
8175 mutex_lock(&rdev->devlist_mtx);
8176 err = cfg80211_can_add_interface(rdev, wdev->iftype);
8177 mutex_unlock(&rdev->devlist_mtx);
8178 if (err)
8179 return err;
8180
eeb126e9 8181 err = rdev_start_p2p_device(rdev, wdev);
98104fde
JB
8182 if (err)
8183 return err;
8184
8185 wdev->p2p_started = true;
8186 mutex_lock(&rdev->devlist_mtx);
8187 rdev->opencount++;
8188 mutex_unlock(&rdev->devlist_mtx);
8189
8190 return 0;
8191}
8192
8193static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8194{
8195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8196 struct wireless_dev *wdev = info->user_ptr[1];
8197
8198 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8199 return -EOPNOTSUPP;
8200
8201 if (!rdev->ops->stop_p2p_device)
8202 return -EOPNOTSUPP;
8203
65e8d5b8 8204 mutex_lock(&rdev->devlist_mtx);
f9f47529
JB
8205 mutex_lock(&rdev->sched_scan_mtx);
8206 cfg80211_stop_p2p_device(rdev, wdev);
8207 mutex_unlock(&rdev->sched_scan_mtx);
65e8d5b8 8208 mutex_unlock(&rdev->devlist_mtx);
98104fde
JB
8209
8210 return 0;
8211}
8212
3713b4e3
JB
8213static int nl80211_get_protocol_features(struct sk_buff *skb,
8214 struct genl_info *info)
8215{
8216 void *hdr;
8217 struct sk_buff *msg;
8218
8219 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8220 if (!msg)
8221 return -ENOMEM;
8222
8223 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8224 NL80211_CMD_GET_PROTOCOL_FEATURES);
8225 if (!hdr)
8226 goto nla_put_failure;
8227
8228 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8229 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8230 goto nla_put_failure;
8231
8232 genlmsg_end(msg, hdr);
8233 return genlmsg_reply(msg, info);
8234
8235 nla_put_failure:
8236 kfree_skb(msg);
8237 return -ENOBUFS;
8238}
8239
355199e0
JM
8240static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8241{
8242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8243 struct cfg80211_update_ft_ies_params ft_params;
8244 struct net_device *dev = info->user_ptr[1];
8245
8246 if (!rdev->ops->update_ft_ies)
8247 return -EOPNOTSUPP;
8248
8249 if (!info->attrs[NL80211_ATTR_MDID] ||
8250 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8251 return -EINVAL;
8252
8253 memset(&ft_params, 0, sizeof(ft_params));
8254 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8255 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8256 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8257
8258 return rdev_update_ft_ies(rdev, dev, &ft_params);
8259}
8260
5de17984
AS
8261static int nl80211_crit_protocol_start(struct sk_buff *skb,
8262 struct genl_info *info)
8263{
8264 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8265 struct wireless_dev *wdev = info->user_ptr[1];
8266 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8267 u16 duration;
8268 int ret;
8269
8270 if (!rdev->ops->crit_proto_start)
8271 return -EOPNOTSUPP;
8272
8273 if (WARN_ON(!rdev->ops->crit_proto_stop))
8274 return -EINVAL;
8275
8276 if (rdev->crit_proto_nlportid)
8277 return -EBUSY;
8278
8279 /* determine protocol if provided */
8280 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8281 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8282
8283 if (proto >= NUM_NL80211_CRIT_PROTO)
8284 return -EINVAL;
8285
8286 /* timeout must be provided */
8287 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8288 return -EINVAL;
8289
8290 duration =
8291 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8292
8293 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8294 return -ERANGE;
8295
8296 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8297 if (!ret)
8298 rdev->crit_proto_nlportid = info->snd_portid;
8299
8300 return ret;
8301}
8302
8303static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8304 struct genl_info *info)
8305{
8306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8307 struct wireless_dev *wdev = info->user_ptr[1];
8308
8309 if (!rdev->ops->crit_proto_stop)
8310 return -EOPNOTSUPP;
8311
8312 if (rdev->crit_proto_nlportid) {
8313 rdev->crit_proto_nlportid = 0;
8314 rdev_crit_proto_stop(rdev, wdev);
8315 }
8316 return 0;
8317}
8318
4c476991
JB
8319#define NL80211_FLAG_NEED_WIPHY 0x01
8320#define NL80211_FLAG_NEED_NETDEV 0x02
8321#define NL80211_FLAG_NEED_RTNL 0x04
41265714
JB
8322#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8323#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8324 NL80211_FLAG_CHECK_NETDEV_UP)
1bf614ef 8325#define NL80211_FLAG_NEED_WDEV 0x10
98104fde 8326/* If a netdev is associated, it must be UP, P2P must be started */
1bf614ef
JB
8327#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8328 NL80211_FLAG_CHECK_NETDEV_UP)
4c476991
JB
8329
8330static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8331 struct genl_info *info)
8332{
8333 struct cfg80211_registered_device *rdev;
89a54e48 8334 struct wireless_dev *wdev;
4c476991 8335 struct net_device *dev;
4c476991
JB
8336 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8337
8338 if (rtnl)
8339 rtnl_lock();
8340
8341 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4f7eff10 8342 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
4c476991
JB
8343 if (IS_ERR(rdev)) {
8344 if (rtnl)
8345 rtnl_unlock();
8346 return PTR_ERR(rdev);
8347 }
8348 info->user_ptr[0] = rdev;
1bf614ef
JB
8349 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8350 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
89a54e48
JB
8351 mutex_lock(&cfg80211_mutex);
8352 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8353 info->attrs);
8354 if (IS_ERR(wdev)) {
8355 mutex_unlock(&cfg80211_mutex);
4c476991
JB
8356 if (rtnl)
8357 rtnl_unlock();
89a54e48 8358 return PTR_ERR(wdev);
4c476991 8359 }
89a54e48 8360
89a54e48
JB
8361 dev = wdev->netdev;
8362 rdev = wiphy_to_dev(wdev->wiphy);
8363
1bf614ef
JB
8364 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8365 if (!dev) {
8366 mutex_unlock(&cfg80211_mutex);
8367 if (rtnl)
8368 rtnl_unlock();
8369 return -EINVAL;
8370 }
8371
8372 info->user_ptr[1] = dev;
8373 } else {
8374 info->user_ptr[1] = wdev;
41265714 8375 }
1bf614ef
JB
8376
8377 if (dev) {
8378 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8379 !netif_running(dev)) {
8380 mutex_unlock(&cfg80211_mutex);
8381 if (rtnl)
8382 rtnl_unlock();
8383 return -ENETDOWN;
8384 }
8385
8386 dev_hold(dev);
98104fde
JB
8387 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8388 if (!wdev->p2p_started) {
8389 mutex_unlock(&cfg80211_mutex);
8390 if (rtnl)
8391 rtnl_unlock();
8392 return -ENETDOWN;
8393 }
41265714 8394 }
89a54e48 8395
89a54e48
JB
8396 cfg80211_lock_rdev(rdev);
8397
8398 mutex_unlock(&cfg80211_mutex);
8399
4c476991 8400 info->user_ptr[0] = rdev;
4c476991
JB
8401 }
8402
8403 return 0;
8404}
8405
8406static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8407 struct genl_info *info)
8408{
8409 if (info->user_ptr[0])
8410 cfg80211_unlock_rdev(info->user_ptr[0]);
1bf614ef
JB
8411 if (info->user_ptr[1]) {
8412 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8413 struct wireless_dev *wdev = info->user_ptr[1];
8414
8415 if (wdev->netdev)
8416 dev_put(wdev->netdev);
8417 } else {
8418 dev_put(info->user_ptr[1]);
8419 }
8420 }
4c476991
JB
8421 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8422 rtnl_unlock();
8423}
8424
55682965
JB
8425static struct genl_ops nl80211_ops[] = {
8426 {
8427 .cmd = NL80211_CMD_GET_WIPHY,
8428 .doit = nl80211_get_wiphy,
8429 .dumpit = nl80211_dump_wiphy,
8430 .policy = nl80211_policy,
8431 /* can be retrieved by unprivileged users */
4c476991 8432 .internal_flags = NL80211_FLAG_NEED_WIPHY,
55682965
JB
8433 },
8434 {
8435 .cmd = NL80211_CMD_SET_WIPHY,
8436 .doit = nl80211_set_wiphy,
8437 .policy = nl80211_policy,
8438 .flags = GENL_ADMIN_PERM,
4c476991 8439 .internal_flags = NL80211_FLAG_NEED_RTNL,
55682965
JB
8440 },
8441 {
8442 .cmd = NL80211_CMD_GET_INTERFACE,
8443 .doit = nl80211_get_interface,
8444 .dumpit = nl80211_dump_interface,
8445 .policy = nl80211_policy,
8446 /* can be retrieved by unprivileged users */
72fb2abc 8447 .internal_flags = NL80211_FLAG_NEED_WDEV,
55682965
JB
8448 },
8449 {
8450 .cmd = NL80211_CMD_SET_INTERFACE,
8451 .doit = nl80211_set_interface,
8452 .policy = nl80211_policy,
8453 .flags = GENL_ADMIN_PERM,
4c476991
JB
8454 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8455 NL80211_FLAG_NEED_RTNL,
55682965
JB
8456 },
8457 {
8458 .cmd = NL80211_CMD_NEW_INTERFACE,
8459 .doit = nl80211_new_interface,
8460 .policy = nl80211_policy,
8461 .flags = GENL_ADMIN_PERM,
4c476991
JB
8462 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8463 NL80211_FLAG_NEED_RTNL,
55682965
JB
8464 },
8465 {
8466 .cmd = NL80211_CMD_DEL_INTERFACE,
8467 .doit = nl80211_del_interface,
8468 .policy = nl80211_policy,
41ade00f 8469 .flags = GENL_ADMIN_PERM,
84efbb84 8470 .internal_flags = NL80211_FLAG_NEED_WDEV |
4c476991 8471 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
8472 },
8473 {
8474 .cmd = NL80211_CMD_GET_KEY,
8475 .doit = nl80211_get_key,
8476 .policy = nl80211_policy,
8477 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8478 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8479 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
8480 },
8481 {
8482 .cmd = NL80211_CMD_SET_KEY,
8483 .doit = nl80211_set_key,
8484 .policy = nl80211_policy,
8485 .flags = GENL_ADMIN_PERM,
41265714 8486 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8487 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
8488 },
8489 {
8490 .cmd = NL80211_CMD_NEW_KEY,
8491 .doit = nl80211_new_key,
8492 .policy = nl80211_policy,
8493 .flags = GENL_ADMIN_PERM,
41265714 8494 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8495 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
8496 },
8497 {
8498 .cmd = NL80211_CMD_DEL_KEY,
8499 .doit = nl80211_del_key,
8500 .policy = nl80211_policy,
55682965 8501 .flags = GENL_ADMIN_PERM,
41265714 8502 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8503 NL80211_FLAG_NEED_RTNL,
55682965 8504 },
ed1b6cc7
JB
8505 {
8506 .cmd = NL80211_CMD_SET_BEACON,
8507 .policy = nl80211_policy,
8508 .flags = GENL_ADMIN_PERM,
8860020e 8509 .doit = nl80211_set_beacon,
2b5f8b0b 8510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8511 NL80211_FLAG_NEED_RTNL,
ed1b6cc7
JB
8512 },
8513 {
8860020e 8514 .cmd = NL80211_CMD_START_AP,
ed1b6cc7
JB
8515 .policy = nl80211_policy,
8516 .flags = GENL_ADMIN_PERM,
8860020e 8517 .doit = nl80211_start_ap,
2b5f8b0b 8518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8519 NL80211_FLAG_NEED_RTNL,
ed1b6cc7
JB
8520 },
8521 {
8860020e 8522 .cmd = NL80211_CMD_STOP_AP,
ed1b6cc7
JB
8523 .policy = nl80211_policy,
8524 .flags = GENL_ADMIN_PERM,
8860020e 8525 .doit = nl80211_stop_ap,
2b5f8b0b 8526 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8527 NL80211_FLAG_NEED_RTNL,
ed1b6cc7 8528 },
5727ef1b
JB
8529 {
8530 .cmd = NL80211_CMD_GET_STATION,
8531 .doit = nl80211_get_station,
2ec600d6 8532 .dumpit = nl80211_dump_station,
5727ef1b 8533 .policy = nl80211_policy,
4c476991
JB
8534 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8535 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
8536 },
8537 {
8538 .cmd = NL80211_CMD_SET_STATION,
8539 .doit = nl80211_set_station,
8540 .policy = nl80211_policy,
8541 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8542 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8543 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
8544 },
8545 {
8546 .cmd = NL80211_CMD_NEW_STATION,
8547 .doit = nl80211_new_station,
8548 .policy = nl80211_policy,
8549 .flags = GENL_ADMIN_PERM,
41265714 8550 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8551 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
8552 },
8553 {
8554 .cmd = NL80211_CMD_DEL_STATION,
8555 .doit = nl80211_del_station,
8556 .policy = nl80211_policy,
2ec600d6 8557 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8558 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8559 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
8560 },
8561 {
8562 .cmd = NL80211_CMD_GET_MPATH,
8563 .doit = nl80211_get_mpath,
8564 .dumpit = nl80211_dump_mpath,
8565 .policy = nl80211_policy,
8566 .flags = GENL_ADMIN_PERM,
41265714 8567 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8568 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
8569 },
8570 {
8571 .cmd = NL80211_CMD_SET_MPATH,
8572 .doit = nl80211_set_mpath,
8573 .policy = nl80211_policy,
8574 .flags = GENL_ADMIN_PERM,
41265714 8575 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8576 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
8577 },
8578 {
8579 .cmd = NL80211_CMD_NEW_MPATH,
8580 .doit = nl80211_new_mpath,
8581 .policy = nl80211_policy,
8582 .flags = GENL_ADMIN_PERM,
41265714 8583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8584 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
8585 },
8586 {
8587 .cmd = NL80211_CMD_DEL_MPATH,
8588 .doit = nl80211_del_mpath,
8589 .policy = nl80211_policy,
9f1ba906 8590 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8592 NL80211_FLAG_NEED_RTNL,
9f1ba906
JM
8593 },
8594 {
8595 .cmd = NL80211_CMD_SET_BSS,
8596 .doit = nl80211_set_bss,
8597 .policy = nl80211_policy,
b2e1b302 8598 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8599 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8600 NL80211_FLAG_NEED_RTNL,
b2e1b302 8601 },
f130347c
LR
8602 {
8603 .cmd = NL80211_CMD_GET_REG,
8604 .doit = nl80211_get_reg,
8605 .policy = nl80211_policy,
8606 /* can be retrieved by unprivileged users */
8607 },
b2e1b302
LR
8608 {
8609 .cmd = NL80211_CMD_SET_REG,
8610 .doit = nl80211_set_reg,
8611 .policy = nl80211_policy,
8612 .flags = GENL_ADMIN_PERM,
8613 },
8614 {
8615 .cmd = NL80211_CMD_REQ_SET_REG,
8616 .doit = nl80211_req_set_reg,
8617 .policy = nl80211_policy,
93da9cc1 8618 .flags = GENL_ADMIN_PERM,
8619 },
8620 {
24bdd9f4
JC
8621 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8622 .doit = nl80211_get_mesh_config,
93da9cc1 8623 .policy = nl80211_policy,
8624 /* can be retrieved by unprivileged users */
2b5f8b0b 8625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8626 NL80211_FLAG_NEED_RTNL,
93da9cc1 8627 },
8628 {
24bdd9f4
JC
8629 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8630 .doit = nl80211_update_mesh_config,
93da9cc1 8631 .policy = nl80211_policy,
9aed3cc1 8632 .flags = GENL_ADMIN_PERM,
29cbe68c 8633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8634 NL80211_FLAG_NEED_RTNL,
9aed3cc1 8635 },
2a519311
JB
8636 {
8637 .cmd = NL80211_CMD_TRIGGER_SCAN,
8638 .doit = nl80211_trigger_scan,
8639 .policy = nl80211_policy,
8640 .flags = GENL_ADMIN_PERM,
fd014284 8641 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 8642 NL80211_FLAG_NEED_RTNL,
2a519311
JB
8643 },
8644 {
8645 .cmd = NL80211_CMD_GET_SCAN,
8646 .policy = nl80211_policy,
8647 .dumpit = nl80211_dump_scan,
8648 },
807f8a8c
LC
8649 {
8650 .cmd = NL80211_CMD_START_SCHED_SCAN,
8651 .doit = nl80211_start_sched_scan,
8652 .policy = nl80211_policy,
8653 .flags = GENL_ADMIN_PERM,
8654 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8655 NL80211_FLAG_NEED_RTNL,
8656 },
8657 {
8658 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8659 .doit = nl80211_stop_sched_scan,
8660 .policy = nl80211_policy,
8661 .flags = GENL_ADMIN_PERM,
8662 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8663 NL80211_FLAG_NEED_RTNL,
8664 },
636a5d36
JM
8665 {
8666 .cmd = NL80211_CMD_AUTHENTICATE,
8667 .doit = nl80211_authenticate,
8668 .policy = nl80211_policy,
8669 .flags = GENL_ADMIN_PERM,
41265714 8670 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8671 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
8672 },
8673 {
8674 .cmd = NL80211_CMD_ASSOCIATE,
8675 .doit = nl80211_associate,
8676 .policy = nl80211_policy,
8677 .flags = GENL_ADMIN_PERM,
41265714 8678 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8679 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
8680 },
8681 {
8682 .cmd = NL80211_CMD_DEAUTHENTICATE,
8683 .doit = nl80211_deauthenticate,
8684 .policy = nl80211_policy,
8685 .flags = GENL_ADMIN_PERM,
41265714 8686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8687 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
8688 },
8689 {
8690 .cmd = NL80211_CMD_DISASSOCIATE,
8691 .doit = nl80211_disassociate,
8692 .policy = nl80211_policy,
8693 .flags = GENL_ADMIN_PERM,
41265714 8694 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8695 NL80211_FLAG_NEED_RTNL,
636a5d36 8696 },
04a773ad
JB
8697 {
8698 .cmd = NL80211_CMD_JOIN_IBSS,
8699 .doit = nl80211_join_ibss,
8700 .policy = nl80211_policy,
8701 .flags = GENL_ADMIN_PERM,
41265714 8702 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8703 NL80211_FLAG_NEED_RTNL,
04a773ad
JB
8704 },
8705 {
8706 .cmd = NL80211_CMD_LEAVE_IBSS,
8707 .doit = nl80211_leave_ibss,
8708 .policy = nl80211_policy,
8709 .flags = GENL_ADMIN_PERM,
41265714 8710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8711 NL80211_FLAG_NEED_RTNL,
04a773ad 8712 },
aff89a9b
JB
8713#ifdef CONFIG_NL80211_TESTMODE
8714 {
8715 .cmd = NL80211_CMD_TESTMODE,
8716 .doit = nl80211_testmode_do,
71063f0e 8717 .dumpit = nl80211_testmode_dump,
aff89a9b
JB
8718 .policy = nl80211_policy,
8719 .flags = GENL_ADMIN_PERM,
4c476991
JB
8720 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8721 NL80211_FLAG_NEED_RTNL,
aff89a9b
JB
8722 },
8723#endif
b23aa676
SO
8724 {
8725 .cmd = NL80211_CMD_CONNECT,
8726 .doit = nl80211_connect,
8727 .policy = nl80211_policy,
8728 .flags = GENL_ADMIN_PERM,
41265714 8729 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8730 NL80211_FLAG_NEED_RTNL,
b23aa676
SO
8731 },
8732 {
8733 .cmd = NL80211_CMD_DISCONNECT,
8734 .doit = nl80211_disconnect,
8735 .policy = nl80211_policy,
8736 .flags = GENL_ADMIN_PERM,
41265714 8737 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8738 NL80211_FLAG_NEED_RTNL,
b23aa676 8739 },
463d0183
JB
8740 {
8741 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8742 .doit = nl80211_wiphy_netns,
8743 .policy = nl80211_policy,
8744 .flags = GENL_ADMIN_PERM,
4c476991
JB
8745 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8746 NL80211_FLAG_NEED_RTNL,
463d0183 8747 },
61fa713c
HS
8748 {
8749 .cmd = NL80211_CMD_GET_SURVEY,
8750 .policy = nl80211_policy,
8751 .dumpit = nl80211_dump_survey,
8752 },
67fbb16b
SO
8753 {
8754 .cmd = NL80211_CMD_SET_PMKSA,
8755 .doit = nl80211_setdel_pmksa,
8756 .policy = nl80211_policy,
8757 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8758 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8759 NL80211_FLAG_NEED_RTNL,
67fbb16b
SO
8760 },
8761 {
8762 .cmd = NL80211_CMD_DEL_PMKSA,
8763 .doit = nl80211_setdel_pmksa,
8764 .policy = nl80211_policy,
8765 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8767 NL80211_FLAG_NEED_RTNL,
67fbb16b
SO
8768 },
8769 {
8770 .cmd = NL80211_CMD_FLUSH_PMKSA,
8771 .doit = nl80211_flush_pmksa,
8772 .policy = nl80211_policy,
8773 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 8775 NL80211_FLAG_NEED_RTNL,
67fbb16b 8776 },
9588bbd5
JM
8777 {
8778 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8779 .doit = nl80211_remain_on_channel,
8780 .policy = nl80211_policy,
8781 .flags = GENL_ADMIN_PERM,
71bbc994 8782 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 8783 NL80211_FLAG_NEED_RTNL,
9588bbd5
JM
8784 },
8785 {
8786 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8787 .doit = nl80211_cancel_remain_on_channel,
8788 .policy = nl80211_policy,
8789 .flags = GENL_ADMIN_PERM,
71bbc994 8790 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 8791 NL80211_FLAG_NEED_RTNL,
9588bbd5 8792 },
13ae75b1
JM
8793 {
8794 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8795 .doit = nl80211_set_tx_bitrate_mask,
8796 .policy = nl80211_policy,
8797 .flags = GENL_ADMIN_PERM,
4c476991
JB
8798 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8799 NL80211_FLAG_NEED_RTNL,
13ae75b1 8800 },
026331c4 8801 {
2e161f78
JB
8802 .cmd = NL80211_CMD_REGISTER_FRAME,
8803 .doit = nl80211_register_mgmt,
026331c4
JM
8804 .policy = nl80211_policy,
8805 .flags = GENL_ADMIN_PERM,
71bbc994 8806 .internal_flags = NL80211_FLAG_NEED_WDEV |
4c476991 8807 NL80211_FLAG_NEED_RTNL,
026331c4
JM
8808 },
8809 {
2e161f78
JB
8810 .cmd = NL80211_CMD_FRAME,
8811 .doit = nl80211_tx_mgmt,
026331c4 8812 .policy = nl80211_policy,
f7ca38df 8813 .flags = GENL_ADMIN_PERM,
71bbc994 8814 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
f7ca38df
JB
8815 NL80211_FLAG_NEED_RTNL,
8816 },
8817 {
8818 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8819 .doit = nl80211_tx_mgmt_cancel_wait,
8820 .policy = nl80211_policy,
026331c4 8821 .flags = GENL_ADMIN_PERM,
71bbc994 8822 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 8823 NL80211_FLAG_NEED_RTNL,
026331c4 8824 },
ffb9eb3d
KV
8825 {
8826 .cmd = NL80211_CMD_SET_POWER_SAVE,
8827 .doit = nl80211_set_power_save,
8828 .policy = nl80211_policy,
8829 .flags = GENL_ADMIN_PERM,
4c476991
JB
8830 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8831 NL80211_FLAG_NEED_RTNL,
ffb9eb3d
KV
8832 },
8833 {
8834 .cmd = NL80211_CMD_GET_POWER_SAVE,
8835 .doit = nl80211_get_power_save,
8836 .policy = nl80211_policy,
8837 /* can be retrieved by unprivileged users */
4c476991
JB
8838 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8839 NL80211_FLAG_NEED_RTNL,
ffb9eb3d 8840 },
d6dc1a38
JO
8841 {
8842 .cmd = NL80211_CMD_SET_CQM,
8843 .doit = nl80211_set_cqm,
8844 .policy = nl80211_policy,
8845 .flags = GENL_ADMIN_PERM,
4c476991
JB
8846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8847 NL80211_FLAG_NEED_RTNL,
d6dc1a38 8848 },
f444de05
JB
8849 {
8850 .cmd = NL80211_CMD_SET_CHANNEL,
8851 .doit = nl80211_set_channel,
8852 .policy = nl80211_policy,
8853 .flags = GENL_ADMIN_PERM,
4c476991
JB
8854 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8855 NL80211_FLAG_NEED_RTNL,
f444de05 8856 },
e8347eba
BJ
8857 {
8858 .cmd = NL80211_CMD_SET_WDS_PEER,
8859 .doit = nl80211_set_wds_peer,
8860 .policy = nl80211_policy,
8861 .flags = GENL_ADMIN_PERM,
43b19952
JB
8862 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8863 NL80211_FLAG_NEED_RTNL,
e8347eba 8864 },
29cbe68c
JB
8865 {
8866 .cmd = NL80211_CMD_JOIN_MESH,
8867 .doit = nl80211_join_mesh,
8868 .policy = nl80211_policy,
8869 .flags = GENL_ADMIN_PERM,
8870 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8871 NL80211_FLAG_NEED_RTNL,
8872 },
8873 {
8874 .cmd = NL80211_CMD_LEAVE_MESH,
8875 .doit = nl80211_leave_mesh,
8876 .policy = nl80211_policy,
8877 .flags = GENL_ADMIN_PERM,
8878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8879 NL80211_FLAG_NEED_RTNL,
8880 },
dfb89c56 8881#ifdef CONFIG_PM
ff1b6e69
JB
8882 {
8883 .cmd = NL80211_CMD_GET_WOWLAN,
8884 .doit = nl80211_get_wowlan,
8885 .policy = nl80211_policy,
8886 /* can be retrieved by unprivileged users */
8887 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8888 NL80211_FLAG_NEED_RTNL,
8889 },
8890 {
8891 .cmd = NL80211_CMD_SET_WOWLAN,
8892 .doit = nl80211_set_wowlan,
8893 .policy = nl80211_policy,
8894 .flags = GENL_ADMIN_PERM,
8895 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8896 NL80211_FLAG_NEED_RTNL,
8897 },
dfb89c56 8898#endif
e5497d76
JB
8899 {
8900 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8901 .doit = nl80211_set_rekey_data,
8902 .policy = nl80211_policy,
8903 .flags = GENL_ADMIN_PERM,
8904 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8905 NL80211_FLAG_NEED_RTNL,
8906 },
109086ce
AN
8907 {
8908 .cmd = NL80211_CMD_TDLS_MGMT,
8909 .doit = nl80211_tdls_mgmt,
8910 .policy = nl80211_policy,
8911 .flags = GENL_ADMIN_PERM,
8912 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8913 NL80211_FLAG_NEED_RTNL,
8914 },
8915 {
8916 .cmd = NL80211_CMD_TDLS_OPER,
8917 .doit = nl80211_tdls_oper,
8918 .policy = nl80211_policy,
8919 .flags = GENL_ADMIN_PERM,
8920 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8921 NL80211_FLAG_NEED_RTNL,
8922 },
28946da7
JB
8923 {
8924 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8925 .doit = nl80211_register_unexpected_frame,
8926 .policy = nl80211_policy,
8927 .flags = GENL_ADMIN_PERM,
8928 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8929 NL80211_FLAG_NEED_RTNL,
8930 },
7f6cf311
JB
8931 {
8932 .cmd = NL80211_CMD_PROBE_CLIENT,
8933 .doit = nl80211_probe_client,
8934 .policy = nl80211_policy,
8935 .flags = GENL_ADMIN_PERM,
2b5f8b0b 8936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7f6cf311
JB
8937 NL80211_FLAG_NEED_RTNL,
8938 },
5e760230
JB
8939 {
8940 .cmd = NL80211_CMD_REGISTER_BEACONS,
8941 .doit = nl80211_register_beacons,
8942 .policy = nl80211_policy,
8943 .flags = GENL_ADMIN_PERM,
8944 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8945 NL80211_FLAG_NEED_RTNL,
8946 },
1d9d9213
SW
8947 {
8948 .cmd = NL80211_CMD_SET_NOACK_MAP,
8949 .doit = nl80211_set_noack_map,
8950 .policy = nl80211_policy,
8951 .flags = GENL_ADMIN_PERM,
8952 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8953 NL80211_FLAG_NEED_RTNL,
8954 },
98104fde
JB
8955 {
8956 .cmd = NL80211_CMD_START_P2P_DEVICE,
8957 .doit = nl80211_start_p2p_device,
8958 .policy = nl80211_policy,
8959 .flags = GENL_ADMIN_PERM,
8960 .internal_flags = NL80211_FLAG_NEED_WDEV |
8961 NL80211_FLAG_NEED_RTNL,
8962 },
8963 {
8964 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8965 .doit = nl80211_stop_p2p_device,
8966 .policy = nl80211_policy,
8967 .flags = GENL_ADMIN_PERM,
8968 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8969 NL80211_FLAG_NEED_RTNL,
8970 },
f4e583c8
AQ
8971 {
8972 .cmd = NL80211_CMD_SET_MCAST_RATE,
8973 .doit = nl80211_set_mcast_rate,
77765eaf
VT
8974 .policy = nl80211_policy,
8975 .flags = GENL_ADMIN_PERM,
8976 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8977 NL80211_FLAG_NEED_RTNL,
8978 },
8979 {
8980 .cmd = NL80211_CMD_SET_MAC_ACL,
8981 .doit = nl80211_set_mac_acl,
f4e583c8
AQ
8982 .policy = nl80211_policy,
8983 .flags = GENL_ADMIN_PERM,
8984 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8985 NL80211_FLAG_NEED_RTNL,
8986 },
04f39047
SW
8987 {
8988 .cmd = NL80211_CMD_RADAR_DETECT,
8989 .doit = nl80211_start_radar_detection,
8990 .policy = nl80211_policy,
8991 .flags = GENL_ADMIN_PERM,
8992 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8993 NL80211_FLAG_NEED_RTNL,
8994 },
3713b4e3
JB
8995 {
8996 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8997 .doit = nl80211_get_protocol_features,
8998 .policy = nl80211_policy,
8999 },
355199e0
JM
9000 {
9001 .cmd = NL80211_CMD_UPDATE_FT_IES,
9002 .doit = nl80211_update_ft_ies,
9003 .policy = nl80211_policy,
9004 .flags = GENL_ADMIN_PERM,
9005 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9006 NL80211_FLAG_NEED_RTNL,
9007 },
5de17984
AS
9008 {
9009 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9010 .doit = nl80211_crit_protocol_start,
9011 .policy = nl80211_policy,
9012 .flags = GENL_ADMIN_PERM,
9013 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9014 NL80211_FLAG_NEED_RTNL,
9015 },
9016 {
9017 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9018 .doit = nl80211_crit_protocol_stop,
9019 .policy = nl80211_policy,
9020 .flags = GENL_ADMIN_PERM,
9021 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9022 NL80211_FLAG_NEED_RTNL,
9023 }
55682965 9024};
9588bbd5 9025
6039f6d2
JM
9026static struct genl_multicast_group nl80211_mlme_mcgrp = {
9027 .name = "mlme",
9028};
55682965
JB
9029
9030/* multicast groups */
9031static struct genl_multicast_group nl80211_config_mcgrp = {
9032 .name = "config",
9033};
2a519311
JB
9034static struct genl_multicast_group nl80211_scan_mcgrp = {
9035 .name = "scan",
9036};
73d54c9e
LR
9037static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9038 .name = "regulatory",
9039};
55682965
JB
9040
9041/* notification functions */
9042
9043void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9044{
9045 struct sk_buff *msg;
9046
fd2120ca 9047 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
55682965
JB
9048 if (!msg)
9049 return;
9050
3713b4e3
JB
9051 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
9052 false, NULL, NULL, NULL) < 0) {
55682965
JB
9053 nlmsg_free(msg);
9054 return;
9055 }
9056
463d0183
JB
9057 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9058 nl80211_config_mcgrp.id, GFP_KERNEL);
55682965
JB
9059}
9060
362a415d
JB
9061static int nl80211_add_scan_req(struct sk_buff *msg,
9062 struct cfg80211_registered_device *rdev)
9063{
9064 struct cfg80211_scan_request *req = rdev->scan_req;
9065 struct nlattr *nest;
9066 int i;
9067
f9f47529 9068 lockdep_assert_held(&rdev->sched_scan_mtx);
667503dd 9069
362a415d
JB
9070 if (WARN_ON(!req))
9071 return 0;
9072
9073 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9074 if (!nest)
9075 goto nla_put_failure;
9360ffd1
DM
9076 for (i = 0; i < req->n_ssids; i++) {
9077 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9078 goto nla_put_failure;
9079 }
362a415d
JB
9080 nla_nest_end(msg, nest);
9081
9082 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9083 if (!nest)
9084 goto nla_put_failure;
9360ffd1
DM
9085 for (i = 0; i < req->n_channels; i++) {
9086 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9087 goto nla_put_failure;
9088 }
362a415d
JB
9089 nla_nest_end(msg, nest);
9090
9360ffd1
DM
9091 if (req->ie &&
9092 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9093 goto nla_put_failure;
362a415d 9094
ed473771
SL
9095 if (req->flags)
9096 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9097
362a415d
JB
9098 return 0;
9099 nla_put_failure:
9100 return -ENOBUFS;
9101}
9102
a538e2d5
JB
9103static int nl80211_send_scan_msg(struct sk_buff *msg,
9104 struct cfg80211_registered_device *rdev,
fd014284 9105 struct wireless_dev *wdev,
15e47304 9106 u32 portid, u32 seq, int flags,
a538e2d5 9107 u32 cmd)
2a519311
JB
9108{
9109 void *hdr;
9110
15e47304 9111 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2a519311
JB
9112 if (!hdr)
9113 return -1;
9114
9360ffd1 9115 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
fd014284
JB
9116 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9117 wdev->netdev->ifindex)) ||
9118 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9360ffd1 9119 goto nla_put_failure;
2a519311 9120
362a415d
JB
9121 /* ignore errors and send incomplete event anyway */
9122 nl80211_add_scan_req(msg, rdev);
2a519311
JB
9123
9124 return genlmsg_end(msg, hdr);
9125
9126 nla_put_failure:
9127 genlmsg_cancel(msg, hdr);
9128 return -EMSGSIZE;
9129}
9130
807f8a8c
LC
9131static int
9132nl80211_send_sched_scan_msg(struct sk_buff *msg,
9133 struct cfg80211_registered_device *rdev,
9134 struct net_device *netdev,
15e47304 9135 u32 portid, u32 seq, int flags, u32 cmd)
807f8a8c
LC
9136{
9137 void *hdr;
9138
15e47304 9139 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
807f8a8c
LC
9140 if (!hdr)
9141 return -1;
9142
9360ffd1
DM
9143 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9144 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9145 goto nla_put_failure;
807f8a8c
LC
9146
9147 return genlmsg_end(msg, hdr);
9148
9149 nla_put_failure:
9150 genlmsg_cancel(msg, hdr);
9151 return -EMSGSIZE;
9152}
9153
a538e2d5 9154void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
fd014284 9155 struct wireless_dev *wdev)
a538e2d5
JB
9156{
9157 struct sk_buff *msg;
9158
58050fce 9159 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
a538e2d5
JB
9160 if (!msg)
9161 return;
9162
fd014284 9163 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5
JB
9164 NL80211_CMD_TRIGGER_SCAN) < 0) {
9165 nlmsg_free(msg);
9166 return;
9167 }
9168
463d0183
JB
9169 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9170 nl80211_scan_mcgrp.id, GFP_KERNEL);
a538e2d5
JB
9171}
9172
2a519311 9173void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
fd014284 9174 struct wireless_dev *wdev)
2a519311
JB
9175{
9176 struct sk_buff *msg;
9177
fd2120ca 9178 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2a519311
JB
9179 if (!msg)
9180 return;
9181
fd014284 9182 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5 9183 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
2a519311
JB
9184 nlmsg_free(msg);
9185 return;
9186 }
9187
463d0183
JB
9188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9189 nl80211_scan_mcgrp.id, GFP_KERNEL);
2a519311
JB
9190}
9191
9192void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
fd014284 9193 struct wireless_dev *wdev)
2a519311
JB
9194{
9195 struct sk_buff *msg;
9196
fd2120ca 9197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2a519311
JB
9198 if (!msg)
9199 return;
9200
fd014284 9201 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5 9202 NL80211_CMD_SCAN_ABORTED) < 0) {
2a519311
JB
9203 nlmsg_free(msg);
9204 return;
9205 }
9206
463d0183
JB
9207 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9208 nl80211_scan_mcgrp.id, GFP_KERNEL);
2a519311
JB
9209}
9210
807f8a8c
LC
9211void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9212 struct net_device *netdev)
9213{
9214 struct sk_buff *msg;
9215
9216 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9217 if (!msg)
9218 return;
9219
9220 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9221 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9222 nlmsg_free(msg);
9223 return;
9224 }
9225
9226 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9227 nl80211_scan_mcgrp.id, GFP_KERNEL);
9228}
9229
9230void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9231 struct net_device *netdev, u32 cmd)
9232{
9233 struct sk_buff *msg;
9234
58050fce 9235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
807f8a8c
LC
9236 if (!msg)
9237 return;
9238
9239 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9240 nlmsg_free(msg);
9241 return;
9242 }
9243
9244 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9245 nl80211_scan_mcgrp.id, GFP_KERNEL);
9246}
9247
73d54c9e
LR
9248/*
9249 * This can happen on global regulatory changes or device specific settings
9250 * based on custom world regulatory domains.
9251 */
9252void nl80211_send_reg_change_event(struct regulatory_request *request)
9253{
9254 struct sk_buff *msg;
9255 void *hdr;
9256
fd2120ca 9257 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
73d54c9e
LR
9258 if (!msg)
9259 return;
9260
9261 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9262 if (!hdr) {
9263 nlmsg_free(msg);
9264 return;
9265 }
9266
9267 /* Userspace can always count this one always being set */
9360ffd1
DM
9268 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9269 goto nla_put_failure;
9270
9271 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9272 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9273 NL80211_REGDOM_TYPE_WORLD))
9274 goto nla_put_failure;
9275 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9276 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9277 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9278 goto nla_put_failure;
9279 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9280 request->intersect) {
9281 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9282 NL80211_REGDOM_TYPE_INTERSECTION))
9283 goto nla_put_failure;
9284 } else {
9285 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9286 NL80211_REGDOM_TYPE_COUNTRY) ||
9287 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9288 request->alpha2))
9289 goto nla_put_failure;
9290 }
9291
f4173766 9292 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
9360ffd1
DM
9293 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9294 goto nla_put_failure;
73d54c9e 9295
3b7b72ee 9296 genlmsg_end(msg, hdr);
73d54c9e 9297
bc43b28c 9298 rcu_read_lock();
463d0183 9299 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
bc43b28c
JB
9300 GFP_ATOMIC);
9301 rcu_read_unlock();
73d54c9e
LR
9302
9303 return;
9304
9305nla_put_failure:
9306 genlmsg_cancel(msg, hdr);
9307 nlmsg_free(msg);
9308}
9309
6039f6d2
JM
9310static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9311 struct net_device *netdev,
9312 const u8 *buf, size_t len,
e6d6e342 9313 enum nl80211_commands cmd, gfp_t gfp)
6039f6d2
JM
9314{
9315 struct sk_buff *msg;
9316 void *hdr;
9317
e6d6e342 9318 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6039f6d2
JM
9319 if (!msg)
9320 return;
9321
9322 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9323 if (!hdr) {
9324 nlmsg_free(msg);
9325 return;
9326 }
9327
9360ffd1
DM
9328 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9329 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9330 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9331 goto nla_put_failure;
6039f6d2 9332
3b7b72ee 9333 genlmsg_end(msg, hdr);
6039f6d2 9334
463d0183
JB
9335 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9336 nl80211_mlme_mcgrp.id, gfp);
6039f6d2
JM
9337 return;
9338
9339 nla_put_failure:
9340 genlmsg_cancel(msg, hdr);
9341 nlmsg_free(msg);
9342}
9343
9344void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
e6d6e342
JB
9345 struct net_device *netdev, const u8 *buf,
9346 size_t len, gfp_t gfp)
6039f6d2
JM
9347{
9348 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 9349 NL80211_CMD_AUTHENTICATE, gfp);
6039f6d2
JM
9350}
9351
9352void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9353 struct net_device *netdev, const u8 *buf,
e6d6e342 9354 size_t len, gfp_t gfp)
6039f6d2 9355{
e6d6e342
JB
9356 nl80211_send_mlme_event(rdev, netdev, buf, len,
9357 NL80211_CMD_ASSOCIATE, gfp);
6039f6d2
JM
9358}
9359
53b46b84 9360void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
e6d6e342
JB
9361 struct net_device *netdev, const u8 *buf,
9362 size_t len, gfp_t gfp)
6039f6d2
JM
9363{
9364 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 9365 NL80211_CMD_DEAUTHENTICATE, gfp);
6039f6d2
JM
9366}
9367
53b46b84
JM
9368void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9369 struct net_device *netdev, const u8 *buf,
e6d6e342 9370 size_t len, gfp_t gfp)
6039f6d2
JM
9371{
9372 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 9373 NL80211_CMD_DISASSOCIATE, gfp);
6039f6d2
JM
9374}
9375
947add36
JB
9376void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
9377 size_t len)
cf4e594e 9378{
947add36
JB
9379 struct wireless_dev *wdev = dev->ieee80211_ptr;
9380 struct wiphy *wiphy = wdev->wiphy;
9381 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9382
9383 trace_cfg80211_send_unprot_deauth(dev);
9384 nl80211_send_mlme_event(rdev, dev, buf, len,
9385 NL80211_CMD_UNPROT_DEAUTHENTICATE, GFP_ATOMIC);
cf4e594e 9386}
947add36 9387EXPORT_SYMBOL(cfg80211_send_unprot_deauth);
cf4e594e 9388
947add36
JB
9389void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
9390 size_t len)
cf4e594e 9391{
947add36
JB
9392 struct wireless_dev *wdev = dev->ieee80211_ptr;
9393 struct wiphy *wiphy = wdev->wiphy;
9394 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9395
9396 trace_cfg80211_send_unprot_disassoc(dev);
9397 nl80211_send_mlme_event(rdev, dev, buf, len,
9398 NL80211_CMD_UNPROT_DISASSOCIATE, GFP_ATOMIC);
cf4e594e 9399}
947add36 9400EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);
cf4e594e 9401
1b06bb40
LR
9402static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9403 struct net_device *netdev, int cmd,
e6d6e342 9404 const u8 *addr, gfp_t gfp)
1965c853
JM
9405{
9406 struct sk_buff *msg;
9407 void *hdr;
9408
e6d6e342 9409 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1965c853
JM
9410 if (!msg)
9411 return;
9412
9413 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9414 if (!hdr) {
9415 nlmsg_free(msg);
9416 return;
9417 }
9418
9360ffd1
DM
9419 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9420 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9421 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9422 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9423 goto nla_put_failure;
1965c853 9424
3b7b72ee 9425 genlmsg_end(msg, hdr);
1965c853 9426
463d0183
JB
9427 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9428 nl80211_mlme_mcgrp.id, gfp);
1965c853
JM
9429 return;
9430
9431 nla_put_failure:
9432 genlmsg_cancel(msg, hdr);
9433 nlmsg_free(msg);
9434}
9435
9436void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
e6d6e342
JB
9437 struct net_device *netdev, const u8 *addr,
9438 gfp_t gfp)
1965c853
JM
9439{
9440 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
e6d6e342 9441 addr, gfp);
1965c853
JM
9442}
9443
9444void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
e6d6e342
JB
9445 struct net_device *netdev, const u8 *addr,
9446 gfp_t gfp)
1965c853 9447{
e6d6e342
JB
9448 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9449 addr, gfp);
1965c853
JM
9450}
9451
b23aa676
SO
9452void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9453 struct net_device *netdev, const u8 *bssid,
9454 const u8 *req_ie, size_t req_ie_len,
9455 const u8 *resp_ie, size_t resp_ie_len,
9456 u16 status, gfp_t gfp)
9457{
9458 struct sk_buff *msg;
9459 void *hdr;
9460
58050fce 9461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
b23aa676
SO
9462 if (!msg)
9463 return;
9464
9465 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9466 if (!hdr) {
9467 nlmsg_free(msg);
9468 return;
9469 }
9470
9360ffd1
DM
9471 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9472 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9473 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9474 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9475 (req_ie &&
9476 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9477 (resp_ie &&
9478 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9479 goto nla_put_failure;
b23aa676 9480
3b7b72ee 9481 genlmsg_end(msg, hdr);
b23aa676 9482
463d0183
JB
9483 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9484 nl80211_mlme_mcgrp.id, gfp);
b23aa676
SO
9485 return;
9486
9487 nla_put_failure:
9488 genlmsg_cancel(msg, hdr);
9489 nlmsg_free(msg);
9490
9491}
9492
9493void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9494 struct net_device *netdev, const u8 *bssid,
9495 const u8 *req_ie, size_t req_ie_len,
9496 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9497{
9498 struct sk_buff *msg;
9499 void *hdr;
9500
58050fce 9501 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
b23aa676
SO
9502 if (!msg)
9503 return;
9504
9505 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9506 if (!hdr) {
9507 nlmsg_free(msg);
9508 return;
9509 }
9510
9360ffd1
DM
9511 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9512 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9513 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9514 (req_ie &&
9515 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9516 (resp_ie &&
9517 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9518 goto nla_put_failure;
b23aa676 9519
3b7b72ee 9520 genlmsg_end(msg, hdr);
b23aa676 9521
463d0183
JB
9522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9523 nl80211_mlme_mcgrp.id, gfp);
b23aa676
SO
9524 return;
9525
9526 nla_put_failure:
9527 genlmsg_cancel(msg, hdr);
9528 nlmsg_free(msg);
9529
9530}
9531
9532void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9533 struct net_device *netdev, u16 reason,
667503dd 9534 const u8 *ie, size_t ie_len, bool from_ap)
b23aa676
SO
9535{
9536 struct sk_buff *msg;
9537 void *hdr;
9538
58050fce 9539 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
b23aa676
SO
9540 if (!msg)
9541 return;
9542
9543 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9544 if (!hdr) {
9545 nlmsg_free(msg);
9546 return;
9547 }
9548
9360ffd1
DM
9549 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9550 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9551 (from_ap && reason &&
9552 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9553 (from_ap &&
9554 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9555 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9556 goto nla_put_failure;
b23aa676 9557
3b7b72ee 9558 genlmsg_end(msg, hdr);
b23aa676 9559
463d0183
JB
9560 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9561 nl80211_mlme_mcgrp.id, GFP_KERNEL);
b23aa676
SO
9562 return;
9563
9564 nla_put_failure:
9565 genlmsg_cancel(msg, hdr);
9566 nlmsg_free(msg);
9567
9568}
9569
04a773ad
JB
9570void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9571 struct net_device *netdev, const u8 *bssid,
9572 gfp_t gfp)
9573{
9574 struct sk_buff *msg;
9575 void *hdr;
9576
fd2120ca 9577 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
04a773ad
JB
9578 if (!msg)
9579 return;
9580
9581 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9582 if (!hdr) {
9583 nlmsg_free(msg);
9584 return;
9585 }
9586
9360ffd1
DM
9587 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9588 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9589 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9590 goto nla_put_failure;
04a773ad 9591
3b7b72ee 9592 genlmsg_end(msg, hdr);
04a773ad 9593
463d0183
JB
9594 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9595 nl80211_mlme_mcgrp.id, gfp);
04a773ad
JB
9596 return;
9597
9598 nla_put_failure:
9599 genlmsg_cancel(msg, hdr);
9600 nlmsg_free(msg);
9601}
9602
947add36
JB
9603void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9604 const u8* ie, u8 ie_len, gfp_t gfp)
c93b5e71 9605{
947add36
JB
9606 struct wireless_dev *wdev = dev->ieee80211_ptr;
9607 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
c93b5e71
JC
9608 struct sk_buff *msg;
9609 void *hdr;
9610
947add36
JB
9611 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9612 return;
9613
9614 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9615
c93b5e71
JC
9616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9617 if (!msg)
9618 return;
9619
9620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9621 if (!hdr) {
9622 nlmsg_free(msg);
9623 return;
9624 }
9625
9360ffd1 9626 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
947add36
JB
9627 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9628 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
9360ffd1
DM
9629 (ie_len && ie &&
9630 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9631 goto nla_put_failure;
c93b5e71 9632
3b7b72ee 9633 genlmsg_end(msg, hdr);
c93b5e71
JC
9634
9635 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9636 nl80211_mlme_mcgrp.id, gfp);
9637 return;
9638
9639 nla_put_failure:
9640 genlmsg_cancel(msg, hdr);
9641 nlmsg_free(msg);
9642}
947add36 9643EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
c93b5e71 9644
a3b8b056
JM
9645void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9646 struct net_device *netdev, const u8 *addr,
9647 enum nl80211_key_type key_type, int key_id,
e6d6e342 9648 const u8 *tsc, gfp_t gfp)
a3b8b056
JM
9649{
9650 struct sk_buff *msg;
9651 void *hdr;
9652
e6d6e342 9653 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
a3b8b056
JM
9654 if (!msg)
9655 return;
9656
9657 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9658 if (!hdr) {
9659 nlmsg_free(msg);
9660 return;
9661 }
9662
9360ffd1
DM
9663 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9664 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9665 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9666 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9667 (key_id != -1 &&
9668 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9669 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9670 goto nla_put_failure;
a3b8b056 9671
3b7b72ee 9672 genlmsg_end(msg, hdr);
a3b8b056 9673
463d0183
JB
9674 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9675 nl80211_mlme_mcgrp.id, gfp);
a3b8b056
JM
9676 return;
9677
9678 nla_put_failure:
9679 genlmsg_cancel(msg, hdr);
9680 nlmsg_free(msg);
9681}
9682
6bad8766
LR
9683void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9684 struct ieee80211_channel *channel_before,
9685 struct ieee80211_channel *channel_after)
9686{
9687 struct sk_buff *msg;
9688 void *hdr;
9689 struct nlattr *nl_freq;
9690
fd2120ca 9691 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
6bad8766
LR
9692 if (!msg)
9693 return;
9694
9695 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9696 if (!hdr) {
9697 nlmsg_free(msg);
9698 return;
9699 }
9700
9701 /*
9702 * Since we are applying the beacon hint to a wiphy we know its
9703 * wiphy_idx is valid
9704 */
9360ffd1
DM
9705 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9706 goto nla_put_failure;
6bad8766
LR
9707
9708 /* Before */
9709 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9710 if (!nl_freq)
9711 goto nla_put_failure;
cdc89b97 9712 if (nl80211_msg_put_channel(msg, channel_before, false))
6bad8766
LR
9713 goto nla_put_failure;
9714 nla_nest_end(msg, nl_freq);
9715
9716 /* After */
9717 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9718 if (!nl_freq)
9719 goto nla_put_failure;
cdc89b97 9720 if (nl80211_msg_put_channel(msg, channel_after, false))
6bad8766
LR
9721 goto nla_put_failure;
9722 nla_nest_end(msg, nl_freq);
9723
3b7b72ee 9724 genlmsg_end(msg, hdr);
6bad8766 9725
463d0183
JB
9726 rcu_read_lock();
9727 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9728 GFP_ATOMIC);
9729 rcu_read_unlock();
6bad8766
LR
9730
9731 return;
9732
9733nla_put_failure:
9734 genlmsg_cancel(msg, hdr);
9735 nlmsg_free(msg);
9736}
9737
9588bbd5
JM
9738static void nl80211_send_remain_on_chan_event(
9739 int cmd, struct cfg80211_registered_device *rdev,
71bbc994 9740 struct wireless_dev *wdev, u64 cookie,
9588bbd5 9741 struct ieee80211_channel *chan,
9588bbd5
JM
9742 unsigned int duration, gfp_t gfp)
9743{
9744 struct sk_buff *msg;
9745 void *hdr;
9746
9747 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9748 if (!msg)
9749 return;
9750
9751 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9752 if (!hdr) {
9753 nlmsg_free(msg);
9754 return;
9755 }
9756
9360ffd1 9757 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
9758 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9759 wdev->netdev->ifindex)) ||
00f53350 9760 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
9360ffd1 9761 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
42d97a59
JB
9762 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9763 NL80211_CHAN_NO_HT) ||
9360ffd1
DM
9764 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9765 goto nla_put_failure;
9588bbd5 9766
9360ffd1
DM
9767 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9768 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9769 goto nla_put_failure;
9588bbd5 9770
3b7b72ee 9771 genlmsg_end(msg, hdr);
9588bbd5
JM
9772
9773 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9774 nl80211_mlme_mcgrp.id, gfp);
9775 return;
9776
9777 nla_put_failure:
9778 genlmsg_cancel(msg, hdr);
9779 nlmsg_free(msg);
9780}
9781
947add36
JB
9782void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9783 struct ieee80211_channel *chan,
9784 unsigned int duration, gfp_t gfp)
9588bbd5 9785{
947add36
JB
9786 struct wiphy *wiphy = wdev->wiphy;
9787 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9788
9789 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
9588bbd5 9790 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
71bbc994 9791 rdev, wdev, cookie, chan,
42d97a59 9792 duration, gfp);
9588bbd5 9793}
947add36 9794EXPORT_SYMBOL(cfg80211_ready_on_channel);
9588bbd5 9795
947add36
JB
9796void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9797 struct ieee80211_channel *chan,
9798 gfp_t gfp)
9588bbd5 9799{
947add36
JB
9800 struct wiphy *wiphy = wdev->wiphy;
9801 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9802
9803 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
9588bbd5 9804 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
42d97a59 9805 rdev, wdev, cookie, chan, 0, gfp);
9588bbd5 9806}
947add36 9807EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
9588bbd5 9808
947add36
JB
9809void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9810 struct station_info *sinfo, gfp_t gfp)
98b62183 9811{
947add36
JB
9812 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9813 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
98b62183
JB
9814 struct sk_buff *msg;
9815
947add36
JB
9816 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9817
58050fce 9818 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
98b62183
JB
9819 if (!msg)
9820 return;
9821
66266b3a
JL
9822 if (nl80211_send_station(msg, 0, 0, 0,
9823 rdev, dev, mac_addr, sinfo) < 0) {
98b62183
JB
9824 nlmsg_free(msg);
9825 return;
9826 }
9827
9828 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9829 nl80211_mlme_mcgrp.id, gfp);
9830}
947add36 9831EXPORT_SYMBOL(cfg80211_new_sta);
98b62183 9832
947add36 9833void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
ec15e68b 9834{
947add36
JB
9835 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9836 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
ec15e68b
JM
9837 struct sk_buff *msg;
9838 void *hdr;
9839
947add36
JB
9840 trace_cfg80211_del_sta(dev, mac_addr);
9841
58050fce 9842 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
ec15e68b
JM
9843 if (!msg)
9844 return;
9845
9846 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9847 if (!hdr) {
9848 nlmsg_free(msg);
9849 return;
9850 }
9851
9360ffd1
DM
9852 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9853 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9854 goto nla_put_failure;
ec15e68b 9855
3b7b72ee 9856 genlmsg_end(msg, hdr);
ec15e68b
JM
9857
9858 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9859 nl80211_mlme_mcgrp.id, gfp);
9860 return;
9861
9862 nla_put_failure:
9863 genlmsg_cancel(msg, hdr);
9864 nlmsg_free(msg);
9865}
947add36 9866EXPORT_SYMBOL(cfg80211_del_sta);
ec15e68b 9867
947add36
JB
9868void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9869 enum nl80211_connect_failed_reason reason,
9870 gfp_t gfp)
ed44a951 9871{
947add36
JB
9872 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9873 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
ed44a951
PP
9874 struct sk_buff *msg;
9875 void *hdr;
9876
9877 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9878 if (!msg)
9879 return;
9880
9881 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9882 if (!hdr) {
9883 nlmsg_free(msg);
9884 return;
9885 }
9886
9887 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9888 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9889 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9890 goto nla_put_failure;
9891
9892 genlmsg_end(msg, hdr);
9893
9894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9895 nl80211_mlme_mcgrp.id, gfp);
9896 return;
9897
9898 nla_put_failure:
9899 genlmsg_cancel(msg, hdr);
9900 nlmsg_free(msg);
9901}
947add36 9902EXPORT_SYMBOL(cfg80211_conn_failed);
ed44a951 9903
b92ab5d8
JB
9904static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9905 const u8 *addr, gfp_t gfp)
28946da7
JB
9906{
9907 struct wireless_dev *wdev = dev->ieee80211_ptr;
9908 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9909 struct sk_buff *msg;
9910 void *hdr;
9911 int err;
15e47304 9912 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
28946da7 9913
15e47304 9914 if (!nlportid)
28946da7
JB
9915 return false;
9916
9917 msg = nlmsg_new(100, gfp);
9918 if (!msg)
9919 return true;
9920
b92ab5d8 9921 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
28946da7
JB
9922 if (!hdr) {
9923 nlmsg_free(msg);
9924 return true;
9925 }
9926
9360ffd1
DM
9927 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9928 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9929 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9930 goto nla_put_failure;
28946da7
JB
9931
9932 err = genlmsg_end(msg, hdr);
9933 if (err < 0) {
9934 nlmsg_free(msg);
9935 return true;
9936 }
9937
15e47304 9938 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
28946da7
JB
9939 return true;
9940
9941 nla_put_failure:
9942 genlmsg_cancel(msg, hdr);
9943 nlmsg_free(msg);
9944 return true;
9945}
9946
947add36
JB
9947bool cfg80211_rx_spurious_frame(struct net_device *dev,
9948 const u8 *addr, gfp_t gfp)
b92ab5d8 9949{
947add36
JB
9950 struct wireless_dev *wdev = dev->ieee80211_ptr;
9951 bool ret;
9952
9953 trace_cfg80211_rx_spurious_frame(dev, addr);
9954
9955 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9956 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9957 trace_cfg80211_return_bool(false);
9958 return false;
9959 }
9960 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9961 addr, gfp);
9962 trace_cfg80211_return_bool(ret);
9963 return ret;
b92ab5d8 9964}
947add36 9965EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
b92ab5d8 9966
947add36
JB
9967bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9968 const u8 *addr, gfp_t gfp)
b92ab5d8 9969{
947add36
JB
9970 struct wireless_dev *wdev = dev->ieee80211_ptr;
9971 bool ret;
9972
9973 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9974
9975 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9976 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9977 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9978 trace_cfg80211_return_bool(false);
9979 return false;
9980 }
9981 ret = __nl80211_unexpected_frame(dev,
9982 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9983 addr, gfp);
9984 trace_cfg80211_return_bool(ret);
9985 return ret;
b92ab5d8 9986}
947add36 9987EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
b92ab5d8 9988
2e161f78 9989int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15e47304 9990 struct wireless_dev *wdev, u32 nlportid,
804483e9
JB
9991 int freq, int sig_dbm,
9992 const u8 *buf, size_t len, gfp_t gfp)
026331c4 9993{
71bbc994 9994 struct net_device *netdev = wdev->netdev;
026331c4
JM
9995 struct sk_buff *msg;
9996 void *hdr;
026331c4
JM
9997
9998 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9999 if (!msg)
10000 return -ENOMEM;
10001
2e161f78 10002 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
026331c4
JM
10003 if (!hdr) {
10004 nlmsg_free(msg);
10005 return -ENOMEM;
10006 }
10007
9360ffd1 10008 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
10009 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10010 netdev->ifindex)) ||
9360ffd1
DM
10011 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10012 (sig_dbm &&
10013 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10014 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10015 goto nla_put_failure;
026331c4 10016
3b7b72ee 10017 genlmsg_end(msg, hdr);
026331c4 10018
15e47304 10019 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
026331c4
JM
10020
10021 nla_put_failure:
10022 genlmsg_cancel(msg, hdr);
10023 nlmsg_free(msg);
10024 return -ENOBUFS;
10025}
10026
947add36
JB
10027void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10028 const u8 *buf, size_t len, bool ack, gfp_t gfp)
026331c4 10029{
947add36
JB
10030 struct wiphy *wiphy = wdev->wiphy;
10031 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
71bbc994 10032 struct net_device *netdev = wdev->netdev;
026331c4
JM
10033 struct sk_buff *msg;
10034 void *hdr;
10035
947add36
JB
10036 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10037
026331c4
JM
10038 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10039 if (!msg)
10040 return;
10041
2e161f78 10042 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
026331c4
JM
10043 if (!hdr) {
10044 nlmsg_free(msg);
10045 return;
10046 }
10047
9360ffd1 10048 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
10049 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10050 netdev->ifindex)) ||
9360ffd1
DM
10051 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10052 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10053 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10054 goto nla_put_failure;
026331c4 10055
3b7b72ee 10056 genlmsg_end(msg, hdr);
026331c4
JM
10057
10058 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
10059 return;
10060
10061 nla_put_failure:
10062 genlmsg_cancel(msg, hdr);
10063 nlmsg_free(msg);
10064}
947add36 10065EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
026331c4 10066
947add36
JB
10067void cfg80211_cqm_rssi_notify(struct net_device *dev,
10068 enum nl80211_cqm_rssi_threshold_event rssi_event,
10069 gfp_t gfp)
d6dc1a38 10070{
947add36
JB
10071 struct wireless_dev *wdev = dev->ieee80211_ptr;
10072 struct wiphy *wiphy = wdev->wiphy;
10073 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
d6dc1a38
JO
10074 struct sk_buff *msg;
10075 struct nlattr *pinfoattr;
10076 void *hdr;
10077
947add36
JB
10078 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10079
58050fce 10080 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
d6dc1a38
JO
10081 if (!msg)
10082 return;
10083
10084 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10085 if (!hdr) {
10086 nlmsg_free(msg);
10087 return;
10088 }
10089
9360ffd1 10090 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
947add36 10091 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
9360ffd1 10092 goto nla_put_failure;
d6dc1a38
JO
10093
10094 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10095 if (!pinfoattr)
10096 goto nla_put_failure;
10097
9360ffd1
DM
10098 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10099 rssi_event))
10100 goto nla_put_failure;
d6dc1a38
JO
10101
10102 nla_nest_end(msg, pinfoattr);
10103
3b7b72ee 10104 genlmsg_end(msg, hdr);
d6dc1a38
JO
10105
10106 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10107 nl80211_mlme_mcgrp.id, gfp);
10108 return;
10109
10110 nla_put_failure:
10111 genlmsg_cancel(msg, hdr);
10112 nlmsg_free(msg);
10113}
947add36 10114EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
d6dc1a38 10115
947add36
JB
10116static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10117 struct net_device *netdev, const u8 *bssid,
10118 const u8 *replay_ctr, gfp_t gfp)
e5497d76
JB
10119{
10120 struct sk_buff *msg;
10121 struct nlattr *rekey_attr;
10122 void *hdr;
10123
58050fce 10124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
e5497d76
JB
10125 if (!msg)
10126 return;
10127
10128 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10129 if (!hdr) {
10130 nlmsg_free(msg);
10131 return;
10132 }
10133
9360ffd1
DM
10134 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10135 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10136 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10137 goto nla_put_failure;
e5497d76
JB
10138
10139 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10140 if (!rekey_attr)
10141 goto nla_put_failure;
10142
9360ffd1
DM
10143 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10144 NL80211_REPLAY_CTR_LEN, replay_ctr))
10145 goto nla_put_failure;
e5497d76
JB
10146
10147 nla_nest_end(msg, rekey_attr);
10148
3b7b72ee 10149 genlmsg_end(msg, hdr);
e5497d76
JB
10150
10151 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10152 nl80211_mlme_mcgrp.id, gfp);
10153 return;
10154
10155 nla_put_failure:
10156 genlmsg_cancel(msg, hdr);
10157 nlmsg_free(msg);
10158}
10159
947add36
JB
10160void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10161 const u8 *replay_ctr, gfp_t gfp)
10162{
10163 struct wireless_dev *wdev = dev->ieee80211_ptr;
10164 struct wiphy *wiphy = wdev->wiphy;
10165 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10166
10167 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10168 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10169}
10170EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10171
10172static void
10173nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10174 struct net_device *netdev, int index,
10175 const u8 *bssid, bool preauth, gfp_t gfp)
c9df56b4
JM
10176{
10177 struct sk_buff *msg;
10178 struct nlattr *attr;
10179 void *hdr;
10180
58050fce 10181 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
c9df56b4
JM
10182 if (!msg)
10183 return;
10184
10185 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10186 if (!hdr) {
10187 nlmsg_free(msg);
10188 return;
10189 }
10190
9360ffd1
DM
10191 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10192 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10193 goto nla_put_failure;
c9df56b4
JM
10194
10195 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10196 if (!attr)
10197 goto nla_put_failure;
10198
9360ffd1
DM
10199 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10200 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10201 (preauth &&
10202 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10203 goto nla_put_failure;
c9df56b4
JM
10204
10205 nla_nest_end(msg, attr);
10206
3b7b72ee 10207 genlmsg_end(msg, hdr);
c9df56b4
JM
10208
10209 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10210 nl80211_mlme_mcgrp.id, gfp);
10211 return;
10212
10213 nla_put_failure:
10214 genlmsg_cancel(msg, hdr);
10215 nlmsg_free(msg);
10216}
10217
947add36
JB
10218void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10219 const u8 *bssid, bool preauth, gfp_t gfp)
10220{
10221 struct wireless_dev *wdev = dev->ieee80211_ptr;
10222 struct wiphy *wiphy = wdev->wiphy;
10223 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10224
10225 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10226 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10227}
10228EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10229
10230static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10231 struct net_device *netdev,
10232 struct cfg80211_chan_def *chandef,
10233 gfp_t gfp)
5314526b
TP
10234{
10235 struct sk_buff *msg;
10236 void *hdr;
10237
58050fce 10238 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5314526b
TP
10239 if (!msg)
10240 return;
10241
10242 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10243 if (!hdr) {
10244 nlmsg_free(msg);
10245 return;
10246 }
10247
683b6d3b
JB
10248 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10249 goto nla_put_failure;
10250
10251 if (nl80211_send_chandef(msg, chandef))
7eab0f64 10252 goto nla_put_failure;
5314526b
TP
10253
10254 genlmsg_end(msg, hdr);
10255
10256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10257 nl80211_mlme_mcgrp.id, gfp);
10258 return;
10259
10260 nla_put_failure:
10261 genlmsg_cancel(msg, hdr);
10262 nlmsg_free(msg);
10263}
10264
947add36
JB
10265void cfg80211_ch_switch_notify(struct net_device *dev,
10266 struct cfg80211_chan_def *chandef)
84f10708 10267{
947add36
JB
10268 struct wireless_dev *wdev = dev->ieee80211_ptr;
10269 struct wiphy *wiphy = wdev->wiphy;
10270 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10271
10272 trace_cfg80211_ch_switch_notify(dev, chandef);
10273
10274 wdev_lock(wdev);
10275
10276 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10277 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10278 goto out;
10279
10280 wdev->channel = chandef->chan;
10281 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10282out:
10283 wdev_unlock(wdev);
10284 return;
10285}
10286EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10287
10288void cfg80211_cqm_txe_notify(struct net_device *dev,
10289 const u8 *peer, u32 num_packets,
10290 u32 rate, u32 intvl, gfp_t gfp)
10291{
10292 struct wireless_dev *wdev = dev->ieee80211_ptr;
10293 struct wiphy *wiphy = wdev->wiphy;
10294 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
84f10708
TP
10295 struct sk_buff *msg;
10296 struct nlattr *pinfoattr;
10297 void *hdr;
10298
10299 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10300 if (!msg)
10301 return;
10302
10303 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10304 if (!hdr) {
10305 nlmsg_free(msg);
10306 return;
10307 }
10308
10309 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
947add36 10310 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
84f10708
TP
10311 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10312 goto nla_put_failure;
10313
10314 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10315 if (!pinfoattr)
10316 goto nla_put_failure;
10317
10318 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10319 goto nla_put_failure;
10320
10321 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10322 goto nla_put_failure;
10323
10324 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10325 goto nla_put_failure;
10326
10327 nla_nest_end(msg, pinfoattr);
10328
10329 genlmsg_end(msg, hdr);
10330
10331 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10332 nl80211_mlme_mcgrp.id, gfp);
10333 return;
10334
10335 nla_put_failure:
10336 genlmsg_cancel(msg, hdr);
10337 nlmsg_free(msg);
10338}
947add36 10339EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
84f10708 10340
04f39047
SW
10341void
10342nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10343 struct cfg80211_chan_def *chandef,
10344 enum nl80211_radar_event event,
10345 struct net_device *netdev, gfp_t gfp)
10346{
10347 struct sk_buff *msg;
10348 void *hdr;
10349
10350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10351 if (!msg)
10352 return;
10353
10354 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10355 if (!hdr) {
10356 nlmsg_free(msg);
10357 return;
10358 }
10359
10360 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10361 goto nla_put_failure;
10362
10363 /* NOP and radar events don't need a netdev parameter */
10364 if (netdev) {
10365 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10366
10367 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10368 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10369 goto nla_put_failure;
10370 }
10371
10372 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10373 goto nla_put_failure;
10374
10375 if (nl80211_send_chandef(msg, chandef))
10376 goto nla_put_failure;
10377
10378 if (genlmsg_end(msg, hdr) < 0) {
10379 nlmsg_free(msg);
10380 return;
10381 }
10382
10383 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10384 nl80211_mlme_mcgrp.id, gfp);
10385 return;
10386
10387 nla_put_failure:
10388 genlmsg_cancel(msg, hdr);
10389 nlmsg_free(msg);
10390}
10391
947add36
JB
10392void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10393 const u8 *peer, u32 num_packets, gfp_t gfp)
c063dbf5 10394{
947add36
JB
10395 struct wireless_dev *wdev = dev->ieee80211_ptr;
10396 struct wiphy *wiphy = wdev->wiphy;
10397 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
c063dbf5
JB
10398 struct sk_buff *msg;
10399 struct nlattr *pinfoattr;
10400 void *hdr;
10401
947add36
JB
10402 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10403
58050fce 10404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
c063dbf5
JB
10405 if (!msg)
10406 return;
10407
10408 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10409 if (!hdr) {
10410 nlmsg_free(msg);
10411 return;
10412 }
10413
9360ffd1 10414 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
947add36 10415 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9360ffd1
DM
10416 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10417 goto nla_put_failure;
c063dbf5
JB
10418
10419 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10420 if (!pinfoattr)
10421 goto nla_put_failure;
10422
9360ffd1
DM
10423 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10424 goto nla_put_failure;
c063dbf5
JB
10425
10426 nla_nest_end(msg, pinfoattr);
10427
3b7b72ee 10428 genlmsg_end(msg, hdr);
c063dbf5
JB
10429
10430 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10431 nl80211_mlme_mcgrp.id, gfp);
10432 return;
10433
10434 nla_put_failure:
10435 genlmsg_cancel(msg, hdr);
10436 nlmsg_free(msg);
10437}
947add36 10438EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
c063dbf5 10439
7f6cf311
JB
10440void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10441 u64 cookie, bool acked, gfp_t gfp)
10442{
10443 struct wireless_dev *wdev = dev->ieee80211_ptr;
10444 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10445 struct sk_buff *msg;
10446 void *hdr;
10447 int err;
10448
4ee3e063
BL
10449 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10450
58050fce 10451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
4ee3e063 10452
7f6cf311
JB
10453 if (!msg)
10454 return;
10455
10456 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10457 if (!hdr) {
10458 nlmsg_free(msg);
10459 return;
10460 }
10461
9360ffd1
DM
10462 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10463 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10464 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10465 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10466 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10467 goto nla_put_failure;
7f6cf311
JB
10468
10469 err = genlmsg_end(msg, hdr);
10470 if (err < 0) {
10471 nlmsg_free(msg);
10472 return;
10473 }
10474
10475 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10476 nl80211_mlme_mcgrp.id, gfp);
10477 return;
10478
10479 nla_put_failure:
10480 genlmsg_cancel(msg, hdr);
10481 nlmsg_free(msg);
10482}
10483EXPORT_SYMBOL(cfg80211_probe_status);
10484
5e760230
JB
10485void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10486 const u8 *frame, size_t len,
37c73b5f 10487 int freq, int sig_dbm)
5e760230
JB
10488{
10489 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10490 struct sk_buff *msg;
10491 void *hdr;
37c73b5f 10492 struct cfg80211_beacon_registration *reg;
5e760230 10493
4ee3e063
BL
10494 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10495
37c73b5f
BG
10496 spin_lock_bh(&rdev->beacon_registrations_lock);
10497 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10498 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10499 if (!msg) {
10500 spin_unlock_bh(&rdev->beacon_registrations_lock);
10501 return;
10502 }
5e760230 10503
37c73b5f
BG
10504 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10505 if (!hdr)
10506 goto nla_put_failure;
5e760230 10507
37c73b5f
BG
10508 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10509 (freq &&
10510 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10511 (sig_dbm &&
10512 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10513 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10514 goto nla_put_failure;
5e760230 10515
37c73b5f 10516 genlmsg_end(msg, hdr);
5e760230 10517
37c73b5f
BG
10518 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
10519 }
10520 spin_unlock_bh(&rdev->beacon_registrations_lock);
5e760230
JB
10521 return;
10522
10523 nla_put_failure:
37c73b5f
BG
10524 spin_unlock_bh(&rdev->beacon_registrations_lock);
10525 if (hdr)
10526 genlmsg_cancel(msg, hdr);
5e760230
JB
10527 nlmsg_free(msg);
10528}
10529EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10530
cd8f7cb4
JB
10531#ifdef CONFIG_PM
10532void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10533 struct cfg80211_wowlan_wakeup *wakeup,
10534 gfp_t gfp)
10535{
10536 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10537 struct sk_buff *msg;
10538 void *hdr;
10539 int err, size = 200;
10540
10541 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10542
10543 if (wakeup)
10544 size += wakeup->packet_present_len;
10545
10546 msg = nlmsg_new(size, gfp);
10547 if (!msg)
10548 return;
10549
10550 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10551 if (!hdr)
10552 goto free_msg;
10553
10554 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10555 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10556 goto free_msg;
10557
10558 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10559 wdev->netdev->ifindex))
10560 goto free_msg;
10561
10562 if (wakeup) {
10563 struct nlattr *reasons;
10564
10565 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10566
10567 if (wakeup->disconnect &&
10568 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10569 goto free_msg;
10570 if (wakeup->magic_pkt &&
10571 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10572 goto free_msg;
10573 if (wakeup->gtk_rekey_failure &&
10574 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10575 goto free_msg;
10576 if (wakeup->eap_identity_req &&
10577 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10578 goto free_msg;
10579 if (wakeup->four_way_handshake &&
10580 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10581 goto free_msg;
10582 if (wakeup->rfkill_release &&
10583 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10584 goto free_msg;
10585
10586 if (wakeup->pattern_idx >= 0 &&
10587 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10588 wakeup->pattern_idx))
10589 goto free_msg;
10590
2a0e047e
JB
10591 if (wakeup->tcp_match)
10592 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10593
10594 if (wakeup->tcp_connlost)
10595 nla_put_flag(msg,
10596 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10597
10598 if (wakeup->tcp_nomoretokens)
10599 nla_put_flag(msg,
10600 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10601
cd8f7cb4
JB
10602 if (wakeup->packet) {
10603 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10604 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10605
10606 if (!wakeup->packet_80211) {
10607 pkt_attr =
10608 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10609 len_attr =
10610 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10611 }
10612
10613 if (wakeup->packet_len &&
10614 nla_put_u32(msg, len_attr, wakeup->packet_len))
10615 goto free_msg;
10616
10617 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10618 wakeup->packet))
10619 goto free_msg;
10620 }
10621
10622 nla_nest_end(msg, reasons);
10623 }
10624
10625 err = genlmsg_end(msg, hdr);
10626 if (err < 0)
10627 goto free_msg;
10628
10629 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10630 nl80211_mlme_mcgrp.id, gfp);
10631 return;
10632
10633 free_msg:
10634 nlmsg_free(msg);
10635}
10636EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10637#endif
10638
3475b094
JM
10639void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10640 enum nl80211_tdls_operation oper,
10641 u16 reason_code, gfp_t gfp)
10642{
10643 struct wireless_dev *wdev = dev->ieee80211_ptr;
10644 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10645 struct sk_buff *msg;
10646 void *hdr;
10647 int err;
10648
10649 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10650 reason_code);
10651
10652 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10653 if (!msg)
10654 return;
10655
10656 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10657 if (!hdr) {
10658 nlmsg_free(msg);
10659 return;
10660 }
10661
10662 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10663 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10664 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10665 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10666 (reason_code > 0 &&
10667 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10668 goto nla_put_failure;
10669
10670 err = genlmsg_end(msg, hdr);
10671 if (err < 0) {
10672 nlmsg_free(msg);
10673 return;
10674 }
10675
10676 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10677 nl80211_mlme_mcgrp.id, gfp);
10678 return;
10679
10680 nla_put_failure:
10681 genlmsg_cancel(msg, hdr);
10682 nlmsg_free(msg);
10683}
10684EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10685
026331c4
JM
10686static int nl80211_netlink_notify(struct notifier_block * nb,
10687 unsigned long state,
10688 void *_notify)
10689{
10690 struct netlink_notify *notify = _notify;
10691 struct cfg80211_registered_device *rdev;
10692 struct wireless_dev *wdev;
37c73b5f 10693 struct cfg80211_beacon_registration *reg, *tmp;
026331c4
JM
10694
10695 if (state != NETLINK_URELEASE)
10696 return NOTIFY_DONE;
10697
10698 rcu_read_lock();
10699
5e760230 10700 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
89a54e48 10701 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
15e47304 10702 cfg80211_mlme_unregister_socket(wdev, notify->portid);
37c73b5f
BG
10703
10704 spin_lock_bh(&rdev->beacon_registrations_lock);
10705 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10706 list) {
10707 if (reg->nlportid == notify->portid) {
10708 list_del(&reg->list);
10709 kfree(reg);
10710 break;
10711 }
10712 }
10713 spin_unlock_bh(&rdev->beacon_registrations_lock);
5e760230 10714 }
026331c4
JM
10715
10716 rcu_read_unlock();
10717
10718 return NOTIFY_DONE;
10719}
10720
10721static struct notifier_block nl80211_netlink_notifier = {
10722 .notifier_call = nl80211_netlink_notify,
10723};
10724
355199e0
JM
10725void cfg80211_ft_event(struct net_device *netdev,
10726 struct cfg80211_ft_event_params *ft_event)
10727{
10728 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10729 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10730 struct sk_buff *msg;
10731 void *hdr;
10732 int err;
10733
10734 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10735
10736 if (!ft_event->target_ap)
10737 return;
10738
10739 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10740 if (!msg)
10741 return;
10742
10743 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10744 if (!hdr) {
10745 nlmsg_free(msg);
10746 return;
10747 }
10748
10749 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10750 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10752 if (ft_event->ies)
10753 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10754 if (ft_event->ric_ies)
10755 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10756 ft_event->ric_ies);
10757
10758 err = genlmsg_end(msg, hdr);
10759 if (err < 0) {
10760 nlmsg_free(msg);
10761 return;
10762 }
10763
10764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10765 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10766}
10767EXPORT_SYMBOL(cfg80211_ft_event);
10768
5de17984
AS
10769void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10770{
10771 struct cfg80211_registered_device *rdev;
10772 struct sk_buff *msg;
10773 void *hdr;
10774 u32 nlportid;
10775
10776 rdev = wiphy_to_dev(wdev->wiphy);
10777 if (!rdev->crit_proto_nlportid)
10778 return;
10779
10780 nlportid = rdev->crit_proto_nlportid;
10781 rdev->crit_proto_nlportid = 0;
10782
10783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10784 if (!msg)
10785 return;
10786
10787 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10788 if (!hdr)
10789 goto nla_put_failure;
10790
10791 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10792 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10793 goto nla_put_failure;
10794
10795 genlmsg_end(msg, hdr);
10796
10797 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10798 return;
10799
10800 nla_put_failure:
10801 if (hdr)
10802 genlmsg_cancel(msg, hdr);
10803 nlmsg_free(msg);
10804
10805}
10806EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10807
55682965
JB
10808/* initialisation/exit functions */
10809
10810int nl80211_init(void)
10811{
0d63cbb5 10812 int err;
55682965 10813
0d63cbb5
MM
10814 err = genl_register_family_with_ops(&nl80211_fam,
10815 nl80211_ops, ARRAY_SIZE(nl80211_ops));
55682965
JB
10816 if (err)
10817 return err;
10818
55682965
JB
10819 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10820 if (err)
10821 goto err_out;
10822
2a519311
JB
10823 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10824 if (err)
10825 goto err_out;
10826
73d54c9e
LR
10827 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10828 if (err)
10829 goto err_out;
10830
6039f6d2
JM
10831 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10832 if (err)
10833 goto err_out;
10834
aff89a9b
JB
10835#ifdef CONFIG_NL80211_TESTMODE
10836 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10837 if (err)
10838 goto err_out;
10839#endif
10840
026331c4
JM
10841 err = netlink_register_notifier(&nl80211_netlink_notifier);
10842 if (err)
10843 goto err_out;
10844
55682965
JB
10845 return 0;
10846 err_out:
10847 genl_unregister_family(&nl80211_fam);
10848 return err;
10849}
10850
10851void nl80211_exit(void)
10852{
026331c4 10853 netlink_unregister_notifier(&nl80211_netlink_notifier);
55682965
JB
10854 genl_unregister_family(&nl80211_fam);
10855}