mac80211_hwsim: fix race condition with sta/vif pointers
[deliverable/linux.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>
55682965
JB
22#include "core.h"
23#include "nl80211.h"
b2e1b302 24#include "reg.h"
55682965 25
5fb628e9
JM
26static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
4c476991
JB
32static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
55682965
JB
37/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
463d0183 44 .netnsok = true,
4c476991
JB
45 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
55682965
JB
47};
48
89a54e48
JB
49/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
55682965 52{
89a54e48
JB
53 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
55682965 60
89a54e48 61 assert_cfg80211_lock();
55682965 62
89a54e48
JB
63 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
55682965 65
89a54e48
JB
66 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
55682965
JB
71 }
72
89a54e48
JB
73 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
55682965
JB
103}
104
a9455408 105static struct cfg80211_registered_device *
878d9ec7 106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
a9455408 107{
7fee4778
JB
108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
a9455408
JB
110
111 assert_cfg80211_lock();
112
878d9ec7 113 if (!attrs[NL80211_ATTR_WIPHY] &&
89a54e48
JB
114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
7fee4778
JB
116 return ERR_PTR(-EINVAL);
117
878d9ec7 118 if (attrs[NL80211_ATTR_WIPHY])
7fee4778 119 rdev = cfg80211_rdev_by_wiphy_idx(
878d9ec7 120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
a9455408 121
89a54e48
JB
122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
878d9ec7
JB
148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
4f7eff10 150 netdev = dev_get_by_index(netns, ifindex);
7fee4778
JB
151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
a9455408 169 }
a9455408 170 }
a9455408 171
4f7eff10
JB
172 if (!rdev)
173 return ERR_PTR(-ENODEV);
a9455408 174
4f7eff10
JB
175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
a9455408
JB
179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
4f7eff10 202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
a9455408
JB
203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
878d9ec7 207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
a9455408
JB
208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
55682965 220/* policy for the attributes */
b54452b0 221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
55682965
JB
222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
079e24ed 224 .len = 20-1 },
31888487 225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
72bdcf34 226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
094d05dc 227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
b9a5f8ca
JM
228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
81077e82 232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
55682965
JB
233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
41ade00f 237
e007b857
EP
238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
41ade00f 240
b9454e83 241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
41ade00f
JB
242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
81962267 247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
e31b8213 248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
ed1b6cc7
JB
249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
5727ef1b
JB
256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
2ec600d6 261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
5727ef1b 262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
0a9542ee 263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
2ec600d6 264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
a4f606ea 265 .len = IEEE80211_MAX_MESH_ID_LEN },
2ec600d6 266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
9f1ba906 267
b2e1b302
LR
268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
9f1ba906
JM
271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
90c97a04
JM
274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
50b12f59 276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
36aedc90 277
24bdd9f4 278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
15d5dda6 279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
93da9cc1 280
6c739419 281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
9aed3cc1
JM
282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
2a519311
JB
286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
636a5d36
JM
288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
04a773ad 293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
1965c853 294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
dc6382ce 295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
eccb8e8f
JB
296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
3f77316c 299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
c0692b8f
JB
300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
b23aa676
SO
302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
463d0183 305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
8b787643 306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
67fbb16b
SO
307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
9588bbd5
JM
309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
13ae75b1 311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
026331c4
JM
312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
ffb9eb3d 315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
d6dc1a38 316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
d5cdfacb 317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
fd8aaaf3 318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
98d2ff8b
JO
319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
2e161f78 321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
afe0cbf8
BR
322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
885a46d0 324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
f7ca38df 325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
dbd2fd65 326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
ff1b6e69 327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
9c3990aa 328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
bbe6ad6d 329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
e5497d76 330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
34850ab2 331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
32e9de84 332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
9946ecfb
JM
333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
f4b34b55 337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
a1f1c21c 338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
e9f935e3 339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
109086ce
AN
340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
e247bd90 345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
00f740e1
AN
346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
8b60b078 348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
7e7c8926
BG
349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
1d9d9213 353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
1b658f11 354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
4486ea98 355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
89a54e48 356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
55682965
JB
357};
358
e31b8213 359/* policy for the key attributes */
b54452b0 360static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
fffd0934 361 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
b9454e83
JB
362 [NL80211_KEY_IDX] = { .type = NLA_U8 },
363 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
81962267 364 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
b9454e83
JB
365 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
366 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
e31b8213 367 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
dbd2fd65
JB
368 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
369};
370
371/* policy for the key default flags */
372static const struct nla_policy
373nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
374 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
b9454e83
JB
376};
377
ff1b6e69
JB
378/* policy for WoWLAN attributes */
379static const struct nla_policy
380nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
381 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
77dbbb13
JB
385 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
ff1b6e69
JB
389};
390
e5497d76
JB
391/* policy for GTK rekey offload attributes */
392static const struct nla_policy
393nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
394 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
395 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
396 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
397};
398
a1f1c21c
LC
399static const struct nla_policy
400nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
4a4ab0d7 401 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
a1f1c21c 402 .len = IEEE80211_MAX_SSID_LEN },
88e920b4 403 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
a1f1c21c
LC
404};
405
a043897a
HS
406/* ifidx get helper */
407static int nl80211_get_ifidx(struct netlink_callback *cb)
408{
409 int res;
410
411 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
412 nl80211_fam.attrbuf, nl80211_fam.maxattr,
413 nl80211_policy);
414 if (res)
415 return res;
416
417 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
418 return -EINVAL;
419
420 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
421 if (!res)
422 return -EINVAL;
423 return res;
424}
425
67748893
JB
426static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
427 struct netlink_callback *cb,
428 struct cfg80211_registered_device **rdev,
429 struct net_device **dev)
430{
431 int ifidx = cb->args[0];
432 int err;
433
434 if (!ifidx)
435 ifidx = nl80211_get_ifidx(cb);
436 if (ifidx < 0)
437 return ifidx;
438
439 cb->args[0] = ifidx;
440
441 rtnl_lock();
442
443 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
444 if (!*dev) {
445 err = -ENODEV;
446 goto out_rtnl;
447 }
448
449 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
3cc25e51
FF
450 if (IS_ERR(*rdev)) {
451 err = PTR_ERR(*rdev);
67748893
JB
452 goto out_rtnl;
453 }
454
455 return 0;
456 out_rtnl:
457 rtnl_unlock();
458 return err;
459}
460
461static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
462{
463 cfg80211_unlock_rdev(rdev);
464 rtnl_unlock();
465}
466
f4a11bb0
JB
467/* IE validation */
468static bool is_valid_ie_attr(const struct nlattr *attr)
469{
470 const u8 *pos;
471 int len;
472
473 if (!attr)
474 return true;
475
476 pos = nla_data(attr);
477 len = nla_len(attr);
478
479 while (len) {
480 u8 elemlen;
481
482 if (len < 2)
483 return false;
484 len -= 2;
485
486 elemlen = pos[1];
487 if (elemlen > len)
488 return false;
489
490 len -= elemlen;
491 pos += 2 + elemlen;
492 }
493
494 return true;
495}
496
55682965
JB
497/* message building helper */
498static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
499 int flags, u8 cmd)
500{
501 /* since there is no private header just add the generic one */
502 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
503}
504
5dab3b8a
LR
505static int nl80211_msg_put_channel(struct sk_buff *msg,
506 struct ieee80211_channel *chan)
507{
9360ffd1
DM
508 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
509 chan->center_freq))
510 goto nla_put_failure;
5dab3b8a 511
9360ffd1
DM
512 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
513 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
514 goto nla_put_failure;
515 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
523 goto nla_put_failure;
5dab3b8a 524
9360ffd1
DM
525 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
526 DBM_TO_MBM(chan->max_power)))
527 goto nla_put_failure;
5dab3b8a
LR
528
529 return 0;
530
531 nla_put_failure:
532 return -ENOBUFS;
533}
534
55682965
JB
535/* netlink command implementations */
536
b9454e83
JB
537struct key_parse {
538 struct key_params p;
539 int idx;
e31b8213 540 int type;
b9454e83 541 bool def, defmgmt;
dbd2fd65 542 bool def_uni, def_multi;
b9454e83
JB
543};
544
545static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
546{
547 struct nlattr *tb[NL80211_KEY_MAX + 1];
548 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
549 nl80211_key_policy);
550 if (err)
551 return err;
552
553 k->def = !!tb[NL80211_KEY_DEFAULT];
554 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
555
dbd2fd65
JB
556 if (k->def) {
557 k->def_uni = true;
558 k->def_multi = true;
559 }
560 if (k->defmgmt)
561 k->def_multi = true;
562
b9454e83
JB
563 if (tb[NL80211_KEY_IDX])
564 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
565
566 if (tb[NL80211_KEY_DATA]) {
567 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
568 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
569 }
570
571 if (tb[NL80211_KEY_SEQ]) {
572 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
573 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
574 }
575
576 if (tb[NL80211_KEY_CIPHER])
577 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
578
e31b8213
JB
579 if (tb[NL80211_KEY_TYPE]) {
580 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
581 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
582 return -EINVAL;
583 }
584
dbd2fd65
JB
585 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
586 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
2da8f419
JB
587 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
588 tb[NL80211_KEY_DEFAULT_TYPES],
589 nl80211_key_default_policy);
dbd2fd65
JB
590 if (err)
591 return err;
592
593 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
594 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
595 }
596
b9454e83
JB
597 return 0;
598}
599
600static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
601{
602 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
603 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
604 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
605 }
606
607 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
608 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
609 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
610 }
611
612 if (info->attrs[NL80211_ATTR_KEY_IDX])
613 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
614
615 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
616 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
617
618 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
619 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
620
dbd2fd65
JB
621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
e31b8213
JB
628 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
629 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
630 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
631 return -EINVAL;
632 }
633
dbd2fd65
JB
634 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
635 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
636 int err = nla_parse_nested(
637 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
638 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
639 nl80211_key_default_policy);
640 if (err)
641 return err;
642
643 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
644 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
645 }
646
b9454e83
JB
647 return 0;
648}
649
650static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
651{
652 int err;
653
654 memset(k, 0, sizeof(*k));
655 k->idx = -1;
e31b8213 656 k->type = -1;
b9454e83
JB
657
658 if (info->attrs[NL80211_ATTR_KEY])
659 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
660 else
661 err = nl80211_parse_key_old(info, k);
662
663 if (err)
664 return err;
665
666 if (k->def && k->defmgmt)
667 return -EINVAL;
668
dbd2fd65
JB
669 if (k->defmgmt) {
670 if (k->def_uni || !k->def_multi)
671 return -EINVAL;
672 }
673
b9454e83
JB
674 if (k->idx != -1) {
675 if (k->defmgmt) {
676 if (k->idx < 4 || k->idx > 5)
677 return -EINVAL;
678 } else if (k->def) {
679 if (k->idx < 0 || k->idx > 3)
680 return -EINVAL;
681 } else {
682 if (k->idx < 0 || k->idx > 5)
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
fffd0934
JB
690static struct cfg80211_cached_keys *
691nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
692 struct nlattr *keys)
693{
694 struct key_parse parse;
695 struct nlattr *key;
696 struct cfg80211_cached_keys *result;
697 int rem, err, def = 0;
698
699 result = kzalloc(sizeof(*result), GFP_KERNEL);
700 if (!result)
701 return ERR_PTR(-ENOMEM);
702
703 result->def = -1;
704 result->defmgmt = -1;
705
706 nla_for_each_nested(key, keys, rem) {
707 memset(&parse, 0, sizeof(parse));
708 parse.idx = -1;
709
710 err = nl80211_parse_key_new(key, &parse);
711 if (err)
712 goto error;
713 err = -EINVAL;
714 if (!parse.p.key)
715 goto error;
716 if (parse.idx < 0 || parse.idx > 4)
717 goto error;
718 if (parse.def) {
719 if (def)
720 goto error;
721 def = 1;
722 result->def = parse.idx;
dbd2fd65
JB
723 if (!parse.def_uni || !parse.def_multi)
724 goto error;
fffd0934
JB
725 } else if (parse.defmgmt)
726 goto error;
727 err = cfg80211_validate_key_settings(rdev, &parse.p,
e31b8213 728 parse.idx, false, NULL);
fffd0934
JB
729 if (err)
730 goto error;
731 result->params[parse.idx].cipher = parse.p.cipher;
732 result->params[parse.idx].key_len = parse.p.key_len;
733 result->params[parse.idx].key = result->data[parse.idx];
734 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
735 }
736
737 return result;
738 error:
739 kfree(result);
740 return ERR_PTR(err);
741}
742
743static int nl80211_key_allowed(struct wireless_dev *wdev)
744{
745 ASSERT_WDEV_LOCK(wdev);
746
fffd0934
JB
747 switch (wdev->iftype) {
748 case NL80211_IFTYPE_AP:
749 case NL80211_IFTYPE_AP_VLAN:
074ac8df 750 case NL80211_IFTYPE_P2P_GO:
ff973af7 751 case NL80211_IFTYPE_MESH_POINT:
fffd0934
JB
752 break;
753 case NL80211_IFTYPE_ADHOC:
754 if (!wdev->current_bss)
755 return -ENOLINK;
756 break;
757 case NL80211_IFTYPE_STATION:
074ac8df 758 case NL80211_IFTYPE_P2P_CLIENT:
fffd0934
JB
759 if (wdev->sme_state != CFG80211_SME_CONNECTED)
760 return -ENOLINK;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 return 0;
767}
768
7527a782
JB
769static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
770{
771 struct nlattr *nl_modes = nla_nest_start(msg, attr);
772 int i;
773
774 if (!nl_modes)
775 goto nla_put_failure;
776
777 i = 0;
778 while (ifmodes) {
9360ffd1
DM
779 if ((ifmodes & 1) && nla_put_flag(msg, i))
780 goto nla_put_failure;
7527a782
JB
781 ifmodes >>= 1;
782 i++;
783 }
784
785 nla_nest_end(msg, nl_modes);
786 return 0;
787
788nla_put_failure:
789 return -ENOBUFS;
790}
791
792static int nl80211_put_iface_combinations(struct wiphy *wiphy,
793 struct sk_buff *msg)
794{
795 struct nlattr *nl_combis;
796 int i, j;
797
798 nl_combis = nla_nest_start(msg,
799 NL80211_ATTR_INTERFACE_COMBINATIONS);
800 if (!nl_combis)
801 goto nla_put_failure;
802
803 for (i = 0; i < wiphy->n_iface_combinations; i++) {
804 const struct ieee80211_iface_combination *c;
805 struct nlattr *nl_combi, *nl_limits;
806
807 c = &wiphy->iface_combinations[i];
808
809 nl_combi = nla_nest_start(msg, i + 1);
810 if (!nl_combi)
811 goto nla_put_failure;
812
813 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
814 if (!nl_limits)
815 goto nla_put_failure;
816
817 for (j = 0; j < c->n_limits; j++) {
818 struct nlattr *nl_limit;
819
820 nl_limit = nla_nest_start(msg, j + 1);
821 if (!nl_limit)
822 goto nla_put_failure;
9360ffd1
DM
823 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
824 c->limits[j].max))
825 goto nla_put_failure;
7527a782
JB
826 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
827 c->limits[j].types))
828 goto nla_put_failure;
829 nla_nest_end(msg, nl_limit);
830 }
831
832 nla_nest_end(msg, nl_limits);
833
9360ffd1
DM
834 if (c->beacon_int_infra_match &&
835 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
836 goto nla_put_failure;
837 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
838 c->num_different_channels) ||
839 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
840 c->max_interfaces))
841 goto nla_put_failure;
7527a782
JB
842
843 nla_nest_end(msg, nl_combi);
844 }
845
846 nla_nest_end(msg, nl_combis);
847
848 return 0;
849nla_put_failure:
850 return -ENOBUFS;
851}
852
55682965
JB
853static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
854 struct cfg80211_registered_device *dev)
855{
856 void *hdr;
ee688b00
JB
857 struct nlattr *nl_bands, *nl_band;
858 struct nlattr *nl_freqs, *nl_freq;
859 struct nlattr *nl_rates, *nl_rate;
8fdc621d 860 struct nlattr *nl_cmds;
ee688b00
JB
861 enum ieee80211_band band;
862 struct ieee80211_channel *chan;
863 struct ieee80211_rate *rate;
864 int i;
2e161f78
JB
865 const struct ieee80211_txrx_stypes *mgmt_stypes =
866 dev->wiphy.mgmt_stypes;
55682965
JB
867
868 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
869 if (!hdr)
870 return -1;
871
9360ffd1
DM
872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
873 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
874 nla_put_u32(msg, NL80211_ATTR_GENERATION,
875 cfg80211_rdev_list_generation) ||
876 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
877 dev->wiphy.retry_short) ||
878 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
879 dev->wiphy.retry_long) ||
880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
881 dev->wiphy.frag_threshold) ||
882 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
883 dev->wiphy.rts_threshold) ||
884 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
885 dev->wiphy.coverage_class) ||
886 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
887 dev->wiphy.max_scan_ssids) ||
888 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
889 dev->wiphy.max_sched_scan_ssids) ||
890 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
891 dev->wiphy.max_scan_ie_len) ||
892 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
893 dev->wiphy.max_sched_scan_ie_len) ||
894 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
895 dev->wiphy.max_match_sets))
896 goto nla_put_failure;
897
898 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
899 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
900 goto nla_put_failure;
901 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
908 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
911 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
915 goto nla_put_failure;
916
917 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
918 sizeof(u32) * dev->wiphy.n_cipher_suites,
919 dev->wiphy.cipher_suites))
920 goto nla_put_failure;
921
922 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
923 dev->wiphy.max_num_pmkids))
924 goto nla_put_failure;
925
926 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
927 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
928 goto nla_put_failure;
929
930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
931 dev->wiphy.available_antennas_tx) ||
932 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
933 dev->wiphy.available_antennas_rx))
934 goto nla_put_failure;
935
936 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
937 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
938 dev->wiphy.probe_resp_offload))
939 goto nla_put_failure;
87bbbe22 940
7f531e03
BR
941 if ((dev->wiphy.available_antennas_tx ||
942 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
afe0cbf8
BR
943 u32 tx_ant = 0, rx_ant = 0;
944 int res;
945 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
946 if (!res) {
9360ffd1
DM
947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
948 tx_ant) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
950 rx_ant))
951 goto nla_put_failure;
afe0cbf8
BR
952 }
953 }
954
7527a782
JB
955 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
956 dev->wiphy.interface_modes))
f59ac048
LR
957 goto nla_put_failure;
958
ee688b00
JB
959 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
960 if (!nl_bands)
961 goto nla_put_failure;
962
963 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
964 if (!dev->wiphy.bands[band])
965 continue;
966
967 nl_band = nla_nest_start(msg, band);
968 if (!nl_band)
969 goto nla_put_failure;
970
d51626df 971 /* add HT info */
9360ffd1
DM
972 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
973 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
974 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
975 &dev->wiphy.bands[band]->ht_cap.mcs) ||
976 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
977 dev->wiphy.bands[band]->ht_cap.cap) ||
978 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
979 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
980 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
981 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
982 goto nla_put_failure;
d51626df 983
bf0c111e
MP
984 /* add VHT info */
985 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
986 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
987 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
988 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
989 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
990 dev->wiphy.bands[band]->vht_cap.cap)))
991 goto nla_put_failure;
992
ee688b00
JB
993 /* add frequencies */
994 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
995 if (!nl_freqs)
996 goto nla_put_failure;
997
998 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
999 nl_freq = nla_nest_start(msg, i);
1000 if (!nl_freq)
1001 goto nla_put_failure;
1002
1003 chan = &dev->wiphy.bands[band]->channels[i];
5dab3b8a
LR
1004
1005 if (nl80211_msg_put_channel(msg, chan))
1006 goto nla_put_failure;
e2f367f2 1007
ee688b00
JB
1008 nla_nest_end(msg, nl_freq);
1009 }
1010
1011 nla_nest_end(msg, nl_freqs);
1012
1013 /* add bitrates */
1014 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1015 if (!nl_rates)
1016 goto nla_put_failure;
1017
1018 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1019 nl_rate = nla_nest_start(msg, i);
1020 if (!nl_rate)
1021 goto nla_put_failure;
1022
1023 rate = &dev->wiphy.bands[band]->bitrates[i];
9360ffd1
DM
1024 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1025 rate->bitrate))
1026 goto nla_put_failure;
1027 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1028 nla_put_flag(msg,
1029 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1030 goto nla_put_failure;
ee688b00
JB
1031
1032 nla_nest_end(msg, nl_rate);
1033 }
1034
1035 nla_nest_end(msg, nl_rates);
1036
1037 nla_nest_end(msg, nl_band);
1038 }
1039 nla_nest_end(msg, nl_bands);
1040
8fdc621d
JB
1041 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1042 if (!nl_cmds)
1043 goto nla_put_failure;
1044
1045 i = 0;
1046#define CMD(op, n) \
1047 do { \
1048 if (dev->ops->op) { \
1049 i++; \
9360ffd1
DM
1050 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1051 goto nla_put_failure; \
8fdc621d
JB
1052 } \
1053 } while (0)
1054
1055 CMD(add_virtual_intf, NEW_INTERFACE);
1056 CMD(change_virtual_intf, SET_INTERFACE);
1057 CMD(add_key, NEW_KEY);
8860020e 1058 CMD(start_ap, START_AP);
8fdc621d
JB
1059 CMD(add_station, NEW_STATION);
1060 CMD(add_mpath, NEW_MPATH);
24bdd9f4 1061 CMD(update_mesh_config, SET_MESH_CONFIG);
8fdc621d 1062 CMD(change_bss, SET_BSS);
636a5d36
JM
1063 CMD(auth, AUTHENTICATE);
1064 CMD(assoc, ASSOCIATE);
1065 CMD(deauth, DEAUTHENTICATE);
1066 CMD(disassoc, DISASSOCIATE);
04a773ad 1067 CMD(join_ibss, JOIN_IBSS);
29cbe68c 1068 CMD(join_mesh, JOIN_MESH);
67fbb16b
SO
1069 CMD(set_pmksa, SET_PMKSA);
1070 CMD(del_pmksa, DEL_PMKSA);
1071 CMD(flush_pmksa, FLUSH_PMKSA);
7c4ef712
JB
1072 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1073 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
13ae75b1 1074 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2e161f78 1075 CMD(mgmt_tx, FRAME);
f7ca38df 1076 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
5be83de5 1077 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
463d0183 1078 i++;
9360ffd1
DM
1079 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1080 goto nla_put_failure;
463d0183 1081 }
e8c9bd5b 1082 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
cc1d2806 1083 dev->ops->join_mesh) {
aa430da4
JB
1084 i++;
1085 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1086 goto nla_put_failure;
1087 }
e8347eba 1088 CMD(set_wds_peer, SET_WDS_PEER);
109086ce
AN
1089 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1090 CMD(tdls_mgmt, TDLS_MGMT);
1091 CMD(tdls_oper, TDLS_OPER);
1092 }
807f8a8c
LC
1093 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1094 CMD(sched_scan_start, START_SCHED_SCAN);
7f6cf311 1095 CMD(probe_client, PROBE_CLIENT);
1d9d9213 1096 CMD(set_noack_map, SET_NOACK_MAP);
5e760230
JB
1097 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1098 i++;
9360ffd1
DM
1099 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1100 goto nla_put_failure;
5e760230 1101 }
8fdc621d 1102
4745fc09
KV
1103#ifdef CONFIG_NL80211_TESTMODE
1104 CMD(testmode_cmd, TESTMODE);
1105#endif
1106
8fdc621d 1107#undef CMD
b23aa676 1108
6829c878 1109 if (dev->ops->connect || dev->ops->auth) {
b23aa676 1110 i++;
9360ffd1
DM
1111 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1112 goto nla_put_failure;
b23aa676
SO
1113 }
1114
6829c878 1115 if (dev->ops->disconnect || dev->ops->deauth) {
b23aa676 1116 i++;
9360ffd1
DM
1117 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1118 goto nla_put_failure;
b23aa676
SO
1119 }
1120
8fdc621d
JB
1121 nla_nest_end(msg, nl_cmds);
1122
7c4ef712 1123 if (dev->ops->remain_on_channel &&
9360ffd1
DM
1124 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1125 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1126 dev->wiphy.max_remain_on_channel_duration))
1127 goto nla_put_failure;
a293911d 1128
9360ffd1
DM
1129 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1130 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1131 goto nla_put_failure;
f7ca38df 1132
2e161f78
JB
1133 if (mgmt_stypes) {
1134 u16 stypes;
1135 struct nlattr *nl_ftypes, *nl_ifs;
1136 enum nl80211_iftype ift;
1137
1138 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1139 if (!nl_ifs)
1140 goto nla_put_failure;
1141
1142 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1143 nl_ftypes = nla_nest_start(msg, ift);
1144 if (!nl_ftypes)
1145 goto nla_put_failure;
1146 i = 0;
1147 stypes = mgmt_stypes[ift].tx;
1148 while (stypes) {
9360ffd1
DM
1149 if ((stypes & 1) &&
1150 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1151 (i << 4) | IEEE80211_FTYPE_MGMT))
1152 goto nla_put_failure;
2e161f78
JB
1153 stypes >>= 1;
1154 i++;
1155 }
1156 nla_nest_end(msg, nl_ftypes);
1157 }
1158
74b70a4e
JB
1159 nla_nest_end(msg, nl_ifs);
1160
2e161f78
JB
1161 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1162 if (!nl_ifs)
1163 goto nla_put_failure;
1164
1165 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1166 nl_ftypes = nla_nest_start(msg, ift);
1167 if (!nl_ftypes)
1168 goto nla_put_failure;
1169 i = 0;
1170 stypes = mgmt_stypes[ift].rx;
1171 while (stypes) {
9360ffd1
DM
1172 if ((stypes & 1) &&
1173 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1174 (i << 4) | IEEE80211_FTYPE_MGMT))
1175 goto nla_put_failure;
2e161f78
JB
1176 stypes >>= 1;
1177 i++;
1178 }
1179 nla_nest_end(msg, nl_ftypes);
1180 }
1181 nla_nest_end(msg, nl_ifs);
1182 }
1183
dfb89c56 1184#ifdef CONFIG_PM
ff1b6e69
JB
1185 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1186 struct nlattr *nl_wowlan;
1187
1188 nl_wowlan = nla_nest_start(msg,
1189 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1190 if (!nl_wowlan)
1191 goto nla_put_failure;
1192
9360ffd1
DM
1193 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1194 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1195 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1196 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1197 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1199 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1200 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1201 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1202 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1203 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1209 goto nla_put_failure;
ff1b6e69
JB
1210 if (dev->wiphy.wowlan.n_patterns) {
1211 struct nl80211_wowlan_pattern_support pat = {
1212 .max_patterns = dev->wiphy.wowlan.n_patterns,
1213 .min_pattern_len =
1214 dev->wiphy.wowlan.pattern_min_len,
1215 .max_pattern_len =
1216 dev->wiphy.wowlan.pattern_max_len,
1217 };
9360ffd1
DM
1218 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1219 sizeof(pat), &pat))
1220 goto nla_put_failure;
ff1b6e69
JB
1221 }
1222
1223 nla_nest_end(msg, nl_wowlan);
1224 }
dfb89c56 1225#endif
ff1b6e69 1226
7527a782
JB
1227 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1228 dev->wiphy.software_iftypes))
1229 goto nla_put_failure;
1230
1231 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1232 goto nla_put_failure;
1233
9360ffd1
DM
1234 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1235 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1236 dev->wiphy.ap_sme_capa))
1237 goto nla_put_failure;
562a7480 1238
9360ffd1
DM
1239 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1240 dev->wiphy.features))
1241 goto nla_put_failure;
1f074bd8 1242
9360ffd1
DM
1243 if (dev->wiphy.ht_capa_mod_mask &&
1244 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1245 sizeof(*dev->wiphy.ht_capa_mod_mask),
1246 dev->wiphy.ht_capa_mod_mask))
1247 goto nla_put_failure;
7e7c8926 1248
55682965
JB
1249 return genlmsg_end(msg, hdr);
1250
1251 nla_put_failure:
bc3ed28c
TG
1252 genlmsg_cancel(msg, hdr);
1253 return -EMSGSIZE;
55682965
JB
1254}
1255
1256static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1257{
1258 int idx = 0;
1259 int start = cb->args[0];
1260 struct cfg80211_registered_device *dev;
1261
a1794390 1262 mutex_lock(&cfg80211_mutex);
79c97e97 1263 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
463d0183
JB
1264 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1265 continue;
b4637271 1266 if (++idx <= start)
55682965
JB
1267 continue;
1268 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
b4637271
JV
1270 dev) < 0) {
1271 idx--;
55682965 1272 break;
b4637271 1273 }
55682965 1274 }
a1794390 1275 mutex_unlock(&cfg80211_mutex);
55682965
JB
1276
1277 cb->args[0] = idx;
1278
1279 return skb->len;
1280}
1281
1282static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1283{
1284 struct sk_buff *msg;
4c476991 1285 struct cfg80211_registered_device *dev = info->user_ptr[0];
55682965 1286
fd2120ca 1287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
55682965 1288 if (!msg)
4c476991 1289 return -ENOMEM;
55682965 1290
4c476991
JB
1291 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1292 nlmsg_free(msg);
1293 return -ENOBUFS;
1294 }
55682965 1295
134e6375 1296 return genlmsg_reply(msg, info);
55682965
JB
1297}
1298
31888487
JM
1299static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1300 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1301 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1302 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1303 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1304 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1305};
1306
1307static int parse_txq_params(struct nlattr *tb[],
1308 struct ieee80211_txq_params *txq_params)
1309{
a3304b0a 1310 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
31888487
JM
1311 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1312 !tb[NL80211_TXQ_ATTR_AIFS])
1313 return -EINVAL;
1314
a3304b0a 1315 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
31888487
JM
1316 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1317 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1318 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1319 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1320
a3304b0a
JB
1321 if (txq_params->ac >= NL80211_NUM_ACS)
1322 return -EINVAL;
1323
31888487
JM
1324 return 0;
1325}
1326
f444de05
JB
1327static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1328{
1329 /*
cc1d2806
JB
1330 * You can only set the channel explicitly for WDS interfaces,
1331 * all others have their channel managed via their respective
1332 * "establish a connection" command (connect, join, ...)
1333 *
1334 * For AP/GO and mesh mode, the channel can be set with the
1335 * channel userspace API, but is only stored and passed to the
1336 * low-level driver when the AP starts or the mesh is joined.
1337 * This is for backward compatibility, userspace can also give
1338 * the channel in the start-ap or join-mesh commands instead.
f444de05
JB
1339 *
1340 * Monitors are special as they are normally slaved to
e8c9bd5b
JB
1341 * whatever else is going on, so they have their own special
1342 * operation to set the monitor channel if possible.
f444de05
JB
1343 */
1344 return !wdev ||
1345 wdev->iftype == NL80211_IFTYPE_AP ||
f444de05 1346 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
074ac8df
JB
1347 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1348 wdev->iftype == NL80211_IFTYPE_P2P_GO;
f444de05
JB
1349}
1350
cd6c6598
JB
1351static bool nl80211_valid_channel_type(struct genl_info *info,
1352 enum nl80211_channel_type *channel_type)
1353{
1354 enum nl80211_channel_type tmp;
1355
1356 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1357 return false;
1358
1359 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1360 if (tmp != NL80211_CHAN_NO_HT &&
1361 tmp != NL80211_CHAN_HT20 &&
1362 tmp != NL80211_CHAN_HT40PLUS &&
1363 tmp != NL80211_CHAN_HT40MINUS)
1364 return false;
1365
1366 if (channel_type)
1367 *channel_type = tmp;
1368
1369 return true;
1370}
1371
f444de05
JB
1372static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1373 struct wireless_dev *wdev,
1374 struct genl_info *info)
1375{
aa430da4 1376 struct ieee80211_channel *channel;
f444de05
JB
1377 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1378 u32 freq;
1379 int result;
e8c9bd5b
JB
1380 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1381
1382 if (wdev)
1383 iftype = wdev->iftype;
f444de05
JB
1384
1385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1386 return -EINVAL;
1387
1388 if (!nl80211_can_set_dev_channel(wdev))
1389 return -EOPNOTSUPP;
1390
cd6c6598
JB
1391 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1392 !nl80211_valid_channel_type(info, &channel_type))
1393 return -EINVAL;
f444de05
JB
1394
1395 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1396
1397 mutex_lock(&rdev->devlist_mtx);
e8c9bd5b 1398 switch (iftype) {
aa430da4
JB
1399 case NL80211_IFTYPE_AP:
1400 case NL80211_IFTYPE_P2P_GO:
1401 if (wdev->beacon_interval) {
1402 result = -EBUSY;
1403 break;
1404 }
1405 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1406 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1407 channel,
1408 channel_type)) {
1409 result = -EINVAL;
1410 break;
1411 }
1412 wdev->preset_chan = channel;
1413 wdev->preset_chantype = channel_type;
1414 result = 0;
1415 break;
cc1d2806
JB
1416 case NL80211_IFTYPE_MESH_POINT:
1417 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1418 break;
e8c9bd5b
JB
1419 case NL80211_IFTYPE_MONITOR:
1420 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1421 break;
aa430da4 1422 default:
e8c9bd5b 1423 result = -EINVAL;
f444de05
JB
1424 }
1425 mutex_unlock(&rdev->devlist_mtx);
1426
1427 return result;
1428}
1429
1430static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1431{
4c476991
JB
1432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1433 struct net_device *netdev = info->user_ptr[1];
f444de05 1434
4c476991 1435 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
f444de05
JB
1436}
1437
e8347eba
BJ
1438static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1439{
43b19952
JB
1440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1441 struct net_device *dev = info->user_ptr[1];
1442 struct wireless_dev *wdev = dev->ieee80211_ptr;
388ac775 1443 const u8 *bssid;
e8347eba
BJ
1444
1445 if (!info->attrs[NL80211_ATTR_MAC])
1446 return -EINVAL;
1447
43b19952
JB
1448 if (netif_running(dev))
1449 return -EBUSY;
e8347eba 1450
43b19952
JB
1451 if (!rdev->ops->set_wds_peer)
1452 return -EOPNOTSUPP;
e8347eba 1453
43b19952
JB
1454 if (wdev->iftype != NL80211_IFTYPE_WDS)
1455 return -EOPNOTSUPP;
e8347eba
BJ
1456
1457 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
43b19952 1458 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
e8347eba
BJ
1459}
1460
1461
55682965
JB
1462static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct cfg80211_registered_device *rdev;
f444de05
JB
1465 struct net_device *netdev = NULL;
1466 struct wireless_dev *wdev;
a1e567c8 1467 int result = 0, rem_txq_params = 0;
31888487 1468 struct nlattr *nl_txq_params;
b9a5f8ca
JM
1469 u32 changed;
1470 u8 retry_short = 0, retry_long = 0;
1471 u32 frag_threshold = 0, rts_threshold = 0;
81077e82 1472 u8 coverage_class = 0;
55682965 1473
f444de05
JB
1474 /*
1475 * Try to find the wiphy and netdev. Normally this
1476 * function shouldn't need the netdev, but this is
1477 * done for backward compatibility -- previously
1478 * setting the channel was done per wiphy, but now
1479 * it is per netdev. Previous userland like hostapd
1480 * also passed a netdev to set_wiphy, so that it is
1481 * possible to let that go to the right netdev!
1482 */
4bbf4d56
JB
1483 mutex_lock(&cfg80211_mutex);
1484
f444de05
JB
1485 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1486 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1487
1488 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1489 if (netdev && netdev->ieee80211_ptr) {
1490 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1491 mutex_lock(&rdev->mtx);
1492 } else
1493 netdev = NULL;
4bbf4d56
JB
1494 }
1495
f444de05 1496 if (!netdev) {
878d9ec7
JB
1497 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1498 info->attrs);
f444de05
JB
1499 if (IS_ERR(rdev)) {
1500 mutex_unlock(&cfg80211_mutex);
4c476991 1501 return PTR_ERR(rdev);
f444de05
JB
1502 }
1503 wdev = NULL;
1504 netdev = NULL;
1505 result = 0;
1506
1507 mutex_lock(&rdev->mtx);
cc1d2806 1508 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
f444de05
JB
1509 wdev = netdev->ieee80211_ptr;
1510 else
1511 wdev = NULL;
1512
1513 /*
1514 * end workaround code, by now the rdev is available
1515 * and locked, and wdev may or may not be NULL.
1516 */
4bbf4d56
JB
1517
1518 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
31888487
JM
1519 result = cfg80211_dev_rename(
1520 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
4bbf4d56
JB
1521
1522 mutex_unlock(&cfg80211_mutex);
1523
1524 if (result)
1525 goto bad_res;
31888487
JM
1526
1527 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1528 struct ieee80211_txq_params txq_params;
1529 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1530
1531 if (!rdev->ops->set_txq_params) {
1532 result = -EOPNOTSUPP;
1533 goto bad_res;
1534 }
1535
f70f01c2
EP
1536 if (!netdev) {
1537 result = -EINVAL;
1538 goto bad_res;
1539 }
1540
133a3ff2
JB
1541 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1542 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1543 result = -EINVAL;
1544 goto bad_res;
1545 }
1546
2b5f8b0b
JB
1547 if (!netif_running(netdev)) {
1548 result = -ENETDOWN;
1549 goto bad_res;
1550 }
1551
31888487
JM
1552 nla_for_each_nested(nl_txq_params,
1553 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1554 rem_txq_params) {
1555 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1556 nla_data(nl_txq_params),
1557 nla_len(nl_txq_params),
1558 txq_params_policy);
1559 result = parse_txq_params(tb, &txq_params);
1560 if (result)
1561 goto bad_res;
1562
1563 result = rdev->ops->set_txq_params(&rdev->wiphy,
f70f01c2 1564 netdev,
31888487
JM
1565 &txq_params);
1566 if (result)
1567 goto bad_res;
1568 }
1569 }
55682965 1570
72bdcf34 1571 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
f444de05 1572 result = __nl80211_set_channel(rdev, wdev, info);
72bdcf34
JM
1573 if (result)
1574 goto bad_res;
1575 }
1576
98d2ff8b
JO
1577 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1578 enum nl80211_tx_power_setting type;
1579 int idx, mbm = 0;
1580
1581 if (!rdev->ops->set_tx_power) {
60ea385f 1582 result = -EOPNOTSUPP;
98d2ff8b
JO
1583 goto bad_res;
1584 }
1585
1586 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1587 type = nla_get_u32(info->attrs[idx]);
1588
1589 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1590 (type != NL80211_TX_POWER_AUTOMATIC)) {
1591 result = -EINVAL;
1592 goto bad_res;
1593 }
1594
1595 if (type != NL80211_TX_POWER_AUTOMATIC) {
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1597 mbm = nla_get_u32(info->attrs[idx]);
1598 }
1599
1600 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1601 if (result)
1602 goto bad_res;
1603 }
1604
afe0cbf8
BR
1605 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1606 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1607 u32 tx_ant, rx_ant;
7f531e03
BR
1608 if ((!rdev->wiphy.available_antennas_tx &&
1609 !rdev->wiphy.available_antennas_rx) ||
1610 !rdev->ops->set_antenna) {
afe0cbf8
BR
1611 result = -EOPNOTSUPP;
1612 goto bad_res;
1613 }
1614
1615 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1616 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1617
a7ffac95 1618 /* reject antenna configurations which don't match the
7f531e03
BR
1619 * available antenna masks, except for the "all" mask */
1620 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1621 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
a7ffac95
BR
1622 result = -EINVAL;
1623 goto bad_res;
1624 }
1625
7f531e03
BR
1626 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1627 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
a7ffac95 1628
afe0cbf8
BR
1629 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1630 if (result)
1631 goto bad_res;
1632 }
1633
b9a5f8ca
JM
1634 changed = 0;
1635
1636 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1637 retry_short = nla_get_u8(
1638 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1639 if (retry_short == 0) {
1640 result = -EINVAL;
1641 goto bad_res;
1642 }
1643 changed |= WIPHY_PARAM_RETRY_SHORT;
1644 }
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1647 retry_long = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1649 if (retry_long == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_LONG;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1657 frag_threshold = nla_get_u32(
1658 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1659 if (frag_threshold < 256) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 if (frag_threshold != (u32) -1) {
1664 /*
1665 * Fragments (apart from the last one) are required to
1666 * have even length. Make the fragmentation code
1667 * simpler by stripping LSB should someone try to use
1668 * odd threshold value.
1669 */
1670 frag_threshold &= ~0x1;
1671 }
1672 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1673 }
1674
1675 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1676 rts_threshold = nla_get_u32(
1677 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1678 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1679 }
1680
81077e82
LT
1681 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1682 coverage_class = nla_get_u8(
1683 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1684 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1685 }
1686
b9a5f8ca
JM
1687 if (changed) {
1688 u8 old_retry_short, old_retry_long;
1689 u32 old_frag_threshold, old_rts_threshold;
81077e82 1690 u8 old_coverage_class;
b9a5f8ca
JM
1691
1692 if (!rdev->ops->set_wiphy_params) {
1693 result = -EOPNOTSUPP;
1694 goto bad_res;
1695 }
1696
1697 old_retry_short = rdev->wiphy.retry_short;
1698 old_retry_long = rdev->wiphy.retry_long;
1699 old_frag_threshold = rdev->wiphy.frag_threshold;
1700 old_rts_threshold = rdev->wiphy.rts_threshold;
81077e82 1701 old_coverage_class = rdev->wiphy.coverage_class;
b9a5f8ca
JM
1702
1703 if (changed & WIPHY_PARAM_RETRY_SHORT)
1704 rdev->wiphy.retry_short = retry_short;
1705 if (changed & WIPHY_PARAM_RETRY_LONG)
1706 rdev->wiphy.retry_long = retry_long;
1707 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1708 rdev->wiphy.frag_threshold = frag_threshold;
1709 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1710 rdev->wiphy.rts_threshold = rts_threshold;
81077e82
LT
1711 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1712 rdev->wiphy.coverage_class = coverage_class;
b9a5f8ca
JM
1713
1714 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1715 if (result) {
1716 rdev->wiphy.retry_short = old_retry_short;
1717 rdev->wiphy.retry_long = old_retry_long;
1718 rdev->wiphy.frag_threshold = old_frag_threshold;
1719 rdev->wiphy.rts_threshold = old_rts_threshold;
81077e82 1720 rdev->wiphy.coverage_class = old_coverage_class;
b9a5f8ca
JM
1721 }
1722 }
72bdcf34 1723
306d6112 1724 bad_res:
4bbf4d56 1725 mutex_unlock(&rdev->mtx);
f444de05
JB
1726 if (netdev)
1727 dev_put(netdev);
55682965
JB
1728 return result;
1729}
1730
71bbc994
JB
1731static inline u64 wdev_id(struct wireless_dev *wdev)
1732{
1733 return (u64)wdev->identifier |
1734 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1735}
55682965
JB
1736
1737static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
d726405a 1738 struct cfg80211_registered_device *rdev,
72fb2abc 1739 struct wireless_dev *wdev)
55682965 1740{
72fb2abc 1741 struct net_device *dev = wdev->netdev;
55682965
JB
1742 void *hdr;
1743
1744 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1745 if (!hdr)
1746 return -1;
1747
72fb2abc
JB
1748 if (dev &&
1749 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1750 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dev->dev_addr)))
1752 goto nla_put_failure;
1753
1754 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1755 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
71bbc994 1756 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
9360ffd1
DM
1757 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1758 rdev->devlist_generation ^
1759 (cfg80211_rdev_list_generation << 2)))
1760 goto nla_put_failure;
f5ea9120 1761
5b7ccaf3
JB
1762 if (rdev->ops->get_channel) {
1763 struct ieee80211_channel *chan;
1764 enum nl80211_channel_type channel_type;
1765
1766 chan = rdev->ops->get_channel(&rdev->wiphy, wdev,
1767 &channel_type);
1768 if (chan &&
1769 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1770 chan->center_freq) ||
1771 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1772 channel_type)))
59ef43e6 1773 goto nla_put_failure;
d91df0e3
PF
1774 }
1775
55682965
JB
1776 return genlmsg_end(msg, hdr);
1777
1778 nla_put_failure:
bc3ed28c
TG
1779 genlmsg_cancel(msg, hdr);
1780 return -EMSGSIZE;
55682965
JB
1781}
1782
1783static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1784{
1785 int wp_idx = 0;
1786 int if_idx = 0;
1787 int wp_start = cb->args[0];
1788 int if_start = cb->args[1];
f5ea9120 1789 struct cfg80211_registered_device *rdev;
55682965
JB
1790 struct wireless_dev *wdev;
1791
a1794390 1792 mutex_lock(&cfg80211_mutex);
f5ea9120
JB
1793 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1794 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
463d0183 1795 continue;
bba95fef
JB
1796 if (wp_idx < wp_start) {
1797 wp_idx++;
55682965 1798 continue;
bba95fef 1799 }
55682965
JB
1800 if_idx = 0;
1801
f5ea9120 1802 mutex_lock(&rdev->devlist_mtx);
89a54e48 1803 list_for_each_entry(wdev, &rdev->wdev_list, list) {
bba95fef
JB
1804 if (if_idx < if_start) {
1805 if_idx++;
55682965 1806 continue;
bba95fef 1807 }
55682965
JB
1808 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1809 cb->nlh->nlmsg_seq, NLM_F_MULTI,
72fb2abc 1810 rdev, wdev) < 0) {
f5ea9120 1811 mutex_unlock(&rdev->devlist_mtx);
bba95fef
JB
1812 goto out;
1813 }
1814 if_idx++;
55682965 1815 }
f5ea9120 1816 mutex_unlock(&rdev->devlist_mtx);
bba95fef
JB
1817
1818 wp_idx++;
55682965 1819 }
bba95fef 1820 out:
a1794390 1821 mutex_unlock(&cfg80211_mutex);
55682965
JB
1822
1823 cb->args[0] = wp_idx;
1824 cb->args[1] = if_idx;
1825
1826 return skb->len;
1827}
1828
1829static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1830{
1831 struct sk_buff *msg;
4c476991 1832 struct cfg80211_registered_device *dev = info->user_ptr[0];
72fb2abc 1833 struct wireless_dev *wdev = info->user_ptr[1];
55682965 1834
fd2120ca 1835 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
55682965 1836 if (!msg)
4c476991 1837 return -ENOMEM;
55682965 1838
d726405a 1839 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
72fb2abc 1840 dev, wdev) < 0) {
4c476991
JB
1841 nlmsg_free(msg);
1842 return -ENOBUFS;
1843 }
55682965 1844
134e6375 1845 return genlmsg_reply(msg, info);
55682965
JB
1846}
1847
66f7ac50
MW
1848static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1849 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1850 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1851 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1852 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1853 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1854};
1855
1856static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1857{
1858 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1859 int flag;
1860
1861 *mntrflags = 0;
1862
1863 if (!nla)
1864 return -EINVAL;
1865
1866 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1867 nla, mntr_flags_policy))
1868 return -EINVAL;
1869
1870 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1871 if (flags[flag])
1872 *mntrflags |= (1<<flag);
1873
1874 return 0;
1875}
1876
9bc383de 1877static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
ad4bb6f8
JB
1878 struct net_device *netdev, u8 use_4addr,
1879 enum nl80211_iftype iftype)
9bc383de 1880{
ad4bb6f8 1881 if (!use_4addr) {
f350a0a8 1882 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
ad4bb6f8 1883 return -EBUSY;
9bc383de 1884 return 0;
ad4bb6f8 1885 }
9bc383de
JB
1886
1887 switch (iftype) {
1888 case NL80211_IFTYPE_AP_VLAN:
1889 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1890 return 0;
1891 break;
1892 case NL80211_IFTYPE_STATION:
1893 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1894 return 0;
1895 break;
1896 default:
1897 break;
1898 }
1899
1900 return -EOPNOTSUPP;
1901}
1902
55682965
JB
1903static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1904{
4c476991 1905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 1906 struct vif_params params;
e36d56b6 1907 int err;
04a773ad 1908 enum nl80211_iftype otype, ntype;
4c476991 1909 struct net_device *dev = info->user_ptr[1];
92ffe055 1910 u32 _flags, *flags = NULL;
ac7f9cfa 1911 bool change = false;
55682965 1912
2ec600d6
LCC
1913 memset(&params, 0, sizeof(params));
1914
04a773ad 1915 otype = ntype = dev->ieee80211_ptr->iftype;
55682965 1916
723b038d 1917 if (info->attrs[NL80211_ATTR_IFTYPE]) {
ac7f9cfa 1918 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
04a773ad 1919 if (otype != ntype)
ac7f9cfa 1920 change = true;
4c476991
JB
1921 if (ntype > NL80211_IFTYPE_MAX)
1922 return -EINVAL;
723b038d
JB
1923 }
1924
92ffe055 1925 if (info->attrs[NL80211_ATTR_MESH_ID]) {
29cbe68c
JB
1926 struct wireless_dev *wdev = dev->ieee80211_ptr;
1927
4c476991
JB
1928 if (ntype != NL80211_IFTYPE_MESH_POINT)
1929 return -EINVAL;
29cbe68c
JB
1930 if (netif_running(dev))
1931 return -EBUSY;
1932
1933 wdev_lock(wdev);
1934 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1935 IEEE80211_MAX_MESH_ID_LEN);
1936 wdev->mesh_id_up_len =
1937 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1938 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1939 wdev->mesh_id_up_len);
1940 wdev_unlock(wdev);
2ec600d6
LCC
1941 }
1942
8b787643
FF
1943 if (info->attrs[NL80211_ATTR_4ADDR]) {
1944 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1945 change = true;
ad4bb6f8 1946 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
9bc383de 1947 if (err)
4c476991 1948 return err;
8b787643
FF
1949 } else {
1950 params.use_4addr = -1;
1951 }
1952
92ffe055 1953 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4c476991
JB
1954 if (ntype != NL80211_IFTYPE_MONITOR)
1955 return -EINVAL;
92ffe055
JB
1956 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1957 &_flags);
ac7f9cfa 1958 if (err)
4c476991 1959 return err;
ac7f9cfa
JB
1960
1961 flags = &_flags;
1962 change = true;
92ffe055 1963 }
3b85875a 1964
ac7f9cfa 1965 if (change)
3d54d255 1966 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
ac7f9cfa
JB
1967 else
1968 err = 0;
60719ffd 1969
9bc383de
JB
1970 if (!err && params.use_4addr != -1)
1971 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1972
55682965
JB
1973 return err;
1974}
1975
1976static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1977{
4c476991 1978 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 1979 struct vif_params params;
84efbb84 1980 struct wireless_dev *wdev;
1c90f9d4 1981 struct sk_buff *msg;
55682965
JB
1982 int err;
1983 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
66f7ac50 1984 u32 flags;
55682965 1985
2ec600d6
LCC
1986 memset(&params, 0, sizeof(params));
1987
55682965
JB
1988 if (!info->attrs[NL80211_ATTR_IFNAME])
1989 return -EINVAL;
1990
1991 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1992 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1993 if (type > NL80211_IFTYPE_MAX)
1994 return -EINVAL;
1995 }
1996
79c97e97 1997 if (!rdev->ops->add_virtual_intf ||
4c476991
JB
1998 !(rdev->wiphy.interface_modes & (1 << type)))
1999 return -EOPNOTSUPP;
55682965 2000
9bc383de 2001 if (info->attrs[NL80211_ATTR_4ADDR]) {
8b787643 2002 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
ad4bb6f8 2003 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
9bc383de 2004 if (err)
4c476991 2005 return err;
9bc383de 2006 }
8b787643 2007
1c90f9d4
JB
2008 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2009 if (!msg)
2010 return -ENOMEM;
2011
66f7ac50
MW
2012 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2013 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2014 &flags);
84efbb84 2015 wdev = rdev->ops->add_virtual_intf(&rdev->wiphy,
66f7ac50 2016 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2ec600d6 2017 type, err ? NULL : &flags, &params);
1c90f9d4
JB
2018 if (IS_ERR(wdev)) {
2019 nlmsg_free(msg);
84efbb84 2020 return PTR_ERR(wdev);
1c90f9d4 2021 }
2ec600d6 2022
29cbe68c
JB
2023 if (type == NL80211_IFTYPE_MESH_POINT &&
2024 info->attrs[NL80211_ATTR_MESH_ID]) {
29cbe68c
JB
2025 wdev_lock(wdev);
2026 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2027 IEEE80211_MAX_MESH_ID_LEN);
2028 wdev->mesh_id_up_len =
2029 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2030 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2031 wdev->mesh_id_up_len);
2032 wdev_unlock(wdev);
2033 }
2034
1c90f9d4
JB
2035 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
2036 rdev, wdev) < 0) {
2037 nlmsg_free(msg);
2038 return -ENOBUFS;
2039 }
2040
2041 return genlmsg_reply(msg, info);
55682965
JB
2042}
2043
2044static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2045{
4c476991 2046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
84efbb84 2047 struct wireless_dev *wdev = info->user_ptr[1];
55682965 2048
4c476991
JB
2049 if (!rdev->ops->del_virtual_intf)
2050 return -EOPNOTSUPP;
55682965 2051
84efbb84
JB
2052 /*
2053 * If we remove a wireless device without a netdev then clear
2054 * user_ptr[1] so that nl80211_post_doit won't dereference it
2055 * to check if it needs to do dev_put(). Otherwise it crashes
2056 * since the wdev has been freed, unlike with a netdev where
2057 * we need the dev_put() for the netdev to really be freed.
2058 */
2059 if (!wdev->netdev)
2060 info->user_ptr[1] = NULL;
2061
2062 return rdev->ops->del_virtual_intf(&rdev->wiphy, wdev);
55682965
JB
2063}
2064
1d9d9213
SW
2065static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2066{
2067 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2068 struct net_device *dev = info->user_ptr[1];
2069 u16 noack_map;
2070
2071 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2072 return -EINVAL;
2073
2074 if (!rdev->ops->set_noack_map)
2075 return -EOPNOTSUPP;
2076
2077 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2078
2079 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2080}
2081
41ade00f
JB
2082struct get_key_cookie {
2083 struct sk_buff *msg;
2084 int error;
b9454e83 2085 int idx;
41ade00f
JB
2086};
2087
2088static void get_key_callback(void *c, struct key_params *params)
2089{
b9454e83 2090 struct nlattr *key;
41ade00f
JB
2091 struct get_key_cookie *cookie = c;
2092
9360ffd1
DM
2093 if ((params->key &&
2094 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2095 params->key_len, params->key)) ||
2096 (params->seq &&
2097 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2098 params->seq_len, params->seq)) ||
2099 (params->cipher &&
2100 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2101 params->cipher)))
2102 goto nla_put_failure;
41ade00f 2103
b9454e83
JB
2104 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2105 if (!key)
2106 goto nla_put_failure;
2107
9360ffd1
DM
2108 if ((params->key &&
2109 nla_put(cookie->msg, NL80211_KEY_DATA,
2110 params->key_len, params->key)) ||
2111 (params->seq &&
2112 nla_put(cookie->msg, NL80211_KEY_SEQ,
2113 params->seq_len, params->seq)) ||
2114 (params->cipher &&
2115 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2116 params->cipher)))
2117 goto nla_put_failure;
b9454e83 2118
9360ffd1
DM
2119 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2120 goto nla_put_failure;
b9454e83
JB
2121
2122 nla_nest_end(cookie->msg, key);
2123
41ade00f
JB
2124 return;
2125 nla_put_failure:
2126 cookie->error = 1;
2127}
2128
2129static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2130{
4c476991 2131 struct cfg80211_registered_device *rdev = info->user_ptr[0];
41ade00f 2132 int err;
4c476991 2133 struct net_device *dev = info->user_ptr[1];
41ade00f 2134 u8 key_idx = 0;
e31b8213
JB
2135 const u8 *mac_addr = NULL;
2136 bool pairwise;
41ade00f
JB
2137 struct get_key_cookie cookie = {
2138 .error = 0,
2139 };
2140 void *hdr;
2141 struct sk_buff *msg;
2142
2143 if (info->attrs[NL80211_ATTR_KEY_IDX])
2144 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2145
3cfcf6ac 2146 if (key_idx > 5)
41ade00f
JB
2147 return -EINVAL;
2148
2149 if (info->attrs[NL80211_ATTR_MAC])
2150 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2151
e31b8213
JB
2152 pairwise = !!mac_addr;
2153 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2154 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2155 if (kt >= NUM_NL80211_KEYTYPES)
2156 return -EINVAL;
2157 if (kt != NL80211_KEYTYPE_GROUP &&
2158 kt != NL80211_KEYTYPE_PAIRWISE)
2159 return -EINVAL;
2160 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2161 }
2162
4c476991
JB
2163 if (!rdev->ops->get_key)
2164 return -EOPNOTSUPP;
41ade00f 2165
fd2120ca 2166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
2167 if (!msg)
2168 return -ENOMEM;
41ade00f
JB
2169
2170 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2171 NL80211_CMD_NEW_KEY);
4c476991
JB
2172 if (IS_ERR(hdr))
2173 return PTR_ERR(hdr);
41ade00f
JB
2174
2175 cookie.msg = msg;
b9454e83 2176 cookie.idx = key_idx;
41ade00f 2177
9360ffd1
DM
2178 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2179 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2180 goto nla_put_failure;
2181 if (mac_addr &&
2182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2183 goto nla_put_failure;
41ade00f 2184
e31b8213
JB
2185 if (pairwise && mac_addr &&
2186 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2187 return -ENOENT;
2188
2189 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2190 mac_addr, &cookie, get_key_callback);
41ade00f
JB
2191
2192 if (err)
6c95e2a2 2193 goto free_msg;
41ade00f
JB
2194
2195 if (cookie.error)
2196 goto nla_put_failure;
2197
2198 genlmsg_end(msg, hdr);
4c476991 2199 return genlmsg_reply(msg, info);
41ade00f
JB
2200
2201 nla_put_failure:
2202 err = -ENOBUFS;
6c95e2a2 2203 free_msg:
41ade00f 2204 nlmsg_free(msg);
41ade00f
JB
2205 return err;
2206}
2207
2208static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2209{
4c476991 2210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
b9454e83 2211 struct key_parse key;
41ade00f 2212 int err;
4c476991 2213 struct net_device *dev = info->user_ptr[1];
41ade00f 2214
b9454e83
JB
2215 err = nl80211_parse_key(info, &key);
2216 if (err)
2217 return err;
41ade00f 2218
b9454e83 2219 if (key.idx < 0)
41ade00f
JB
2220 return -EINVAL;
2221
b9454e83
JB
2222 /* only support setting default key */
2223 if (!key.def && !key.defmgmt)
41ade00f
JB
2224 return -EINVAL;
2225
dbd2fd65 2226 wdev_lock(dev->ieee80211_ptr);
3cfcf6ac 2227
dbd2fd65
JB
2228 if (key.def) {
2229 if (!rdev->ops->set_default_key) {
2230 err = -EOPNOTSUPP;
2231 goto out;
2232 }
41ade00f 2233
dbd2fd65
JB
2234 err = nl80211_key_allowed(dev->ieee80211_ptr);
2235 if (err)
2236 goto out;
2237
dbd2fd65
JB
2238 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2239 key.def_uni, key.def_multi);
2240
2241 if (err)
2242 goto out;
fffd0934 2243
3d23e349 2244#ifdef CONFIG_CFG80211_WEXT
dbd2fd65
JB
2245 dev->ieee80211_ptr->wext.default_key = key.idx;
2246#endif
2247 } else {
2248 if (key.def_uni || !key.def_multi) {
2249 err = -EINVAL;
2250 goto out;
2251 }
2252
2253 if (!rdev->ops->set_default_mgmt_key) {
2254 err = -EOPNOTSUPP;
2255 goto out;
2256 }
2257
2258 err = nl80211_key_allowed(dev->ieee80211_ptr);
2259 if (err)
2260 goto out;
2261
2262 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2263 dev, key.idx);
2264 if (err)
2265 goto out;
2266
2267#ifdef CONFIG_CFG80211_WEXT
2268 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
08645126 2269#endif
dbd2fd65
JB
2270 }
2271
2272 out:
fffd0934 2273 wdev_unlock(dev->ieee80211_ptr);
41ade00f 2274
41ade00f
JB
2275 return err;
2276}
2277
2278static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2279{
4c476991 2280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
fffd0934 2281 int err;
4c476991 2282 struct net_device *dev = info->user_ptr[1];
b9454e83 2283 struct key_parse key;
e31b8213 2284 const u8 *mac_addr = NULL;
41ade00f 2285
b9454e83
JB
2286 err = nl80211_parse_key(info, &key);
2287 if (err)
2288 return err;
41ade00f 2289
b9454e83 2290 if (!key.p.key)
41ade00f
JB
2291 return -EINVAL;
2292
41ade00f
JB
2293 if (info->attrs[NL80211_ATTR_MAC])
2294 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2295
e31b8213
JB
2296 if (key.type == -1) {
2297 if (mac_addr)
2298 key.type = NL80211_KEYTYPE_PAIRWISE;
2299 else
2300 key.type = NL80211_KEYTYPE_GROUP;
2301 }
2302
2303 /* for now */
2304 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2305 key.type != NL80211_KEYTYPE_GROUP)
2306 return -EINVAL;
2307
4c476991
JB
2308 if (!rdev->ops->add_key)
2309 return -EOPNOTSUPP;
25e47c18 2310
e31b8213
JB
2311 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2312 key.type == NL80211_KEYTYPE_PAIRWISE,
2313 mac_addr))
4c476991 2314 return -EINVAL;
41ade00f 2315
fffd0934
JB
2316 wdev_lock(dev->ieee80211_ptr);
2317 err = nl80211_key_allowed(dev->ieee80211_ptr);
2318 if (!err)
2319 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
e31b8213 2320 key.type == NL80211_KEYTYPE_PAIRWISE,
fffd0934
JB
2321 mac_addr, &key.p);
2322 wdev_unlock(dev->ieee80211_ptr);
41ade00f 2323
41ade00f
JB
2324 return err;
2325}
2326
2327static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2328{
4c476991 2329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
41ade00f 2330 int err;
4c476991 2331 struct net_device *dev = info->user_ptr[1];
41ade00f 2332 u8 *mac_addr = NULL;
b9454e83 2333 struct key_parse key;
41ade00f 2334
b9454e83
JB
2335 err = nl80211_parse_key(info, &key);
2336 if (err)
2337 return err;
41ade00f
JB
2338
2339 if (info->attrs[NL80211_ATTR_MAC])
2340 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2341
e31b8213
JB
2342 if (key.type == -1) {
2343 if (mac_addr)
2344 key.type = NL80211_KEYTYPE_PAIRWISE;
2345 else
2346 key.type = NL80211_KEYTYPE_GROUP;
2347 }
2348
2349 /* for now */
2350 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2351 key.type != NL80211_KEYTYPE_GROUP)
2352 return -EINVAL;
2353
4c476991
JB
2354 if (!rdev->ops->del_key)
2355 return -EOPNOTSUPP;
41ade00f 2356
fffd0934
JB
2357 wdev_lock(dev->ieee80211_ptr);
2358 err = nl80211_key_allowed(dev->ieee80211_ptr);
e31b8213
JB
2359
2360 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2361 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2362 err = -ENOENT;
2363
fffd0934 2364 if (!err)
e31b8213
JB
2365 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2366 key.type == NL80211_KEYTYPE_PAIRWISE,
2367 mac_addr);
41ade00f 2368
3d23e349 2369#ifdef CONFIG_CFG80211_WEXT
08645126 2370 if (!err) {
b9454e83 2371 if (key.idx == dev->ieee80211_ptr->wext.default_key)
08645126 2372 dev->ieee80211_ptr->wext.default_key = -1;
b9454e83 2373 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
08645126
JB
2374 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2375 }
2376#endif
fffd0934 2377 wdev_unlock(dev->ieee80211_ptr);
08645126 2378
41ade00f
JB
2379 return err;
2380}
2381
8860020e
JB
2382static int nl80211_parse_beacon(struct genl_info *info,
2383 struct cfg80211_beacon_data *bcn)
ed1b6cc7 2384{
8860020e 2385 bool haveinfo = false;
ed1b6cc7 2386
9946ecfb
JM
2387 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2388 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2389 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2390 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
f4a11bb0
JB
2391 return -EINVAL;
2392
8860020e 2393 memset(bcn, 0, sizeof(*bcn));
ed1b6cc7 2394
ed1b6cc7 2395 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
8860020e
JB
2396 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2397 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2398 if (!bcn->head_len)
2399 return -EINVAL;
2400 haveinfo = true;
ed1b6cc7
JB
2401 }
2402
2403 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
8860020e
JB
2404 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2405 bcn->tail_len =
ed1b6cc7 2406 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
8860020e 2407 haveinfo = true;
ed1b6cc7
JB
2408 }
2409
4c476991
JB
2410 if (!haveinfo)
2411 return -EINVAL;
3b85875a 2412
9946ecfb 2413 if (info->attrs[NL80211_ATTR_IE]) {
8860020e
JB
2414 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2415 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9946ecfb
JM
2416 }
2417
2418 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
8860020e 2419 bcn->proberesp_ies =
9946ecfb 2420 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
8860020e 2421 bcn->proberesp_ies_len =
9946ecfb
JM
2422 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2423 }
2424
2425 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
8860020e 2426 bcn->assocresp_ies =
9946ecfb 2427 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
8860020e 2428 bcn->assocresp_ies_len =
9946ecfb
JM
2429 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2430 }
2431
00f740e1 2432 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
8860020e 2433 bcn->probe_resp =
00f740e1 2434 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
8860020e 2435 bcn->probe_resp_len =
00f740e1
AN
2436 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2437 }
2438
8860020e
JB
2439 return 0;
2440}
2441
46c1dd0c
FF
2442static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2443 struct cfg80211_ap_settings *params)
2444{
2445 struct wireless_dev *wdev;
2446 bool ret = false;
2447
2448 mutex_lock(&rdev->devlist_mtx);
2449
89a54e48 2450 list_for_each_entry(wdev, &rdev->wdev_list, list) {
46c1dd0c
FF
2451 if (wdev->iftype != NL80211_IFTYPE_AP &&
2452 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2453 continue;
2454
2455 if (!wdev->preset_chan)
2456 continue;
2457
2458 params->channel = wdev->preset_chan;
2459 params->channel_type = wdev->preset_chantype;
2460 ret = true;
2461 break;
2462 }
2463
2464 mutex_unlock(&rdev->devlist_mtx);
2465
2466 return ret;
2467}
2468
8860020e
JB
2469static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2470{
2471 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2472 struct net_device *dev = info->user_ptr[1];
2473 struct wireless_dev *wdev = dev->ieee80211_ptr;
2474 struct cfg80211_ap_settings params;
2475 int err;
2476
2477 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2478 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2479 return -EOPNOTSUPP;
2480
2481 if (!rdev->ops->start_ap)
2482 return -EOPNOTSUPP;
2483
2484 if (wdev->beacon_interval)
2485 return -EALREADY;
2486
2487 memset(&params, 0, sizeof(params));
2488
2489 /* these are required for START_AP */
2490 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2491 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2492 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2493 return -EINVAL;
2494
2495 err = nl80211_parse_beacon(info, &params.beacon);
2496 if (err)
2497 return err;
2498
2499 params.beacon_interval =
2500 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2501 params.dtim_period =
2502 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2503
2504 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2505 if (err)
2506 return err;
2507
2508 /*
2509 * In theory, some of these attributes should be required here
2510 * but since they were not used when the command was originally
2511 * added, keep them optional for old user space programs to let
2512 * them continue to work with drivers that do not need the
2513 * additional information -- drivers must check!
2514 */
2515 if (info->attrs[NL80211_ATTR_SSID]) {
2516 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2517 params.ssid_len =
2518 nla_len(info->attrs[NL80211_ATTR_SSID]);
2519 if (params.ssid_len == 0 ||
2520 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2521 return -EINVAL;
2522 }
2523
2524 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2525 params.hidden_ssid = nla_get_u32(
2526 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2527 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2528 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2529 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2530 return -EINVAL;
2531 }
2532
2533 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2534
2535 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2536 params.auth_type = nla_get_u32(
2537 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2538 if (!nl80211_valid_auth_type(params.auth_type))
2539 return -EINVAL;
2540 } else
2541 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2542
2543 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2544 NL80211_MAX_NR_CIPHER_SUITES);
2545 if (err)
2546 return err;
2547
1b658f11
VT
2548 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2549 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2550 return -EOPNOTSUPP;
2551 params.inactivity_timeout = nla_get_u16(
2552 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2553 }
2554
aa430da4
JB
2555 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2556 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2557
2558 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2559 !nl80211_valid_channel_type(info, &channel_type))
2560 return -EINVAL;
2561
2562 params.channel = rdev_freq_to_chan(rdev,
2563 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2564 channel_type);
2565 if (!params.channel)
2566 return -EINVAL;
2567 params.channel_type = channel_type;
2568 } else if (wdev->preset_chan) {
2569 params.channel = wdev->preset_chan;
2570 params.channel_type = wdev->preset_chantype;
46c1dd0c 2571 } else if (!nl80211_get_ap_channel(rdev, &params))
aa430da4
JB
2572 return -EINVAL;
2573
2574 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2575 params.channel_type))
2576 return -EINVAL;
2577
e4e32459
MK
2578 mutex_lock(&rdev->devlist_mtx);
2579 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2580 CHAN_MODE_SHARED);
2581 mutex_unlock(&rdev->devlist_mtx);
2582
2583 if (err)
2584 return err;
2585
8860020e 2586 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
46c1dd0c
FF
2587 if (!err) {
2588 wdev->preset_chan = params.channel;
2589 wdev->preset_chantype = params.channel_type;
8860020e 2590 wdev->beacon_interval = params.beacon_interval;
f4489ebe 2591 wdev->channel = params.channel;
46c1dd0c 2592 }
56d1893d 2593 return err;
ed1b6cc7
JB
2594}
2595
8860020e
JB
2596static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2597{
2598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2599 struct net_device *dev = info->user_ptr[1];
2600 struct wireless_dev *wdev = dev->ieee80211_ptr;
2601 struct cfg80211_beacon_data params;
2602 int err;
2603
2604 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2605 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2606 return -EOPNOTSUPP;
2607
2608 if (!rdev->ops->change_beacon)
2609 return -EOPNOTSUPP;
2610
2611 if (!wdev->beacon_interval)
2612 return -EINVAL;
2613
2614 err = nl80211_parse_beacon(info, &params);
2615 if (err)
2616 return err;
2617
2618 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2619}
2620
2621static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
ed1b6cc7 2622{
4c476991
JB
2623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2624 struct net_device *dev = info->user_ptr[1];
ed1b6cc7 2625
60771780 2626 return cfg80211_stop_ap(rdev, dev);
ed1b6cc7
JB
2627}
2628
5727ef1b
JB
2629static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2630 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2631 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2632 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
0e46724a 2633 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
b39c48fa 2634 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
d83023da 2635 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
5727ef1b
JB
2636};
2637
eccb8e8f 2638static int parse_station_flags(struct genl_info *info,
bdd3ae3d 2639 enum nl80211_iftype iftype,
eccb8e8f 2640 struct station_parameters *params)
5727ef1b
JB
2641{
2642 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
eccb8e8f 2643 struct nlattr *nla;
5727ef1b
JB
2644 int flag;
2645
eccb8e8f
JB
2646 /*
2647 * Try parsing the new attribute first so userspace
2648 * can specify both for older kernels.
2649 */
2650 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2651 if (nla) {
2652 struct nl80211_sta_flag_update *sta_flags;
2653
2654 sta_flags = nla_data(nla);
2655 params->sta_flags_mask = sta_flags->mask;
2656 params->sta_flags_set = sta_flags->set;
2657 if ((params->sta_flags_mask |
2658 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2659 return -EINVAL;
2660 return 0;
2661 }
2662
2663 /* if present, parse the old attribute */
5727ef1b 2664
eccb8e8f 2665 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
5727ef1b
JB
2666 if (!nla)
2667 return 0;
2668
2669 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2670 nla, sta_flags_policy))
2671 return -EINVAL;
2672
bdd3ae3d
JB
2673 /*
2674 * Only allow certain flags for interface types so that
2675 * other attributes are silently ignored. Remember that
2676 * this is backward compatibility code with old userspace
2677 * and shouldn't be hit in other cases anyway.
2678 */
2679 switch (iftype) {
2680 case NL80211_IFTYPE_AP:
2681 case NL80211_IFTYPE_AP_VLAN:
2682 case NL80211_IFTYPE_P2P_GO:
2683 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2684 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2685 BIT(NL80211_STA_FLAG_WME) |
2686 BIT(NL80211_STA_FLAG_MFP);
2687 break;
2688 case NL80211_IFTYPE_P2P_CLIENT:
2689 case NL80211_IFTYPE_STATION:
2690 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2691 BIT(NL80211_STA_FLAG_TDLS_PEER);
2692 break;
2693 case NL80211_IFTYPE_MESH_POINT:
2694 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2695 BIT(NL80211_STA_FLAG_MFP) |
2696 BIT(NL80211_STA_FLAG_AUTHORIZED);
2697 default:
2698 return -EINVAL;
2699 }
5727ef1b 2700
3383b5a6
JB
2701 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2702 if (flags[flag]) {
eccb8e8f 2703 params->sta_flags_set |= (1<<flag);
5727ef1b 2704
3383b5a6
JB
2705 /* no longer support new API additions in old API */
2706 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2707 return -EINVAL;
2708 }
2709 }
2710
5727ef1b
JB
2711 return 0;
2712}
2713
c8dcfd8a
FF
2714static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2715 int attr)
2716{
2717 struct nlattr *rate;
8eb41c8d
VK
2718 u32 bitrate;
2719 u16 bitrate_compat;
c8dcfd8a
FF
2720
2721 rate = nla_nest_start(msg, attr);
2722 if (!rate)
2723 goto nla_put_failure;
2724
2725 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2726 bitrate = cfg80211_calculate_bitrate(info);
8eb41c8d
VK
2727 /* report 16-bit bitrate only if we can */
2728 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
9360ffd1 2729 if ((bitrate > 0 &&
8eb41c8d
VK
2730 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2731 (bitrate_compat > 0 &&
2732 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
9360ffd1
DM
2733 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2734 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2735 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2736 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2737 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2738 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2739 goto nla_put_failure;
c8dcfd8a
FF
2740
2741 nla_nest_end(msg, rate);
2742 return true;
2743
2744nla_put_failure:
2745 return false;
2746}
2747
fd5b74dc 2748static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
66266b3a
JL
2749 int flags,
2750 struct cfg80211_registered_device *rdev,
2751 struct net_device *dev,
98b62183 2752 const u8 *mac_addr, struct station_info *sinfo)
fd5b74dc
JB
2753{
2754 void *hdr;
f4263c98 2755 struct nlattr *sinfoattr, *bss_param;
fd5b74dc
JB
2756
2757 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2758 if (!hdr)
2759 return -1;
2760
9360ffd1
DM
2761 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2762 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2763 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2764 goto nla_put_failure;
f5ea9120 2765
2ec600d6
LCC
2766 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2767 if (!sinfoattr)
fd5b74dc 2768 goto nla_put_failure;
9360ffd1
DM
2769 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2770 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2771 sinfo->connected_time))
2772 goto nla_put_failure;
2773 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2774 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2775 sinfo->inactive_time))
2776 goto nla_put_failure;
2777 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2778 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2779 sinfo->rx_bytes))
2780 goto nla_put_failure;
2781 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2782 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2783 sinfo->tx_bytes))
2784 goto nla_put_failure;
2785 if ((sinfo->filled & STATION_INFO_LLID) &&
2786 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2787 goto nla_put_failure;
2788 if ((sinfo->filled & STATION_INFO_PLID) &&
2789 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2790 goto nla_put_failure;
2791 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2792 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2793 sinfo->plink_state))
2794 goto nla_put_failure;
66266b3a
JL
2795 switch (rdev->wiphy.signal_type) {
2796 case CFG80211_SIGNAL_TYPE_MBM:
9360ffd1
DM
2797 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2798 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2799 sinfo->signal))
2800 goto nla_put_failure;
2801 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2802 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2803 sinfo->signal_avg))
2804 goto nla_put_failure;
66266b3a
JL
2805 break;
2806 default:
2807 break;
2808 }
420e7fab 2809 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
c8dcfd8a
FF
2810 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2811 NL80211_STA_INFO_TX_BITRATE))
2812 goto nla_put_failure;
2813 }
2814 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2815 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2816 NL80211_STA_INFO_RX_BITRATE))
420e7fab 2817 goto nla_put_failure;
420e7fab 2818 }
9360ffd1
DM
2819 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2820 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2821 sinfo->rx_packets))
2822 goto nla_put_failure;
2823 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2824 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2825 sinfo->tx_packets))
2826 goto nla_put_failure;
2827 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2828 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2829 sinfo->tx_retries))
2830 goto nla_put_failure;
2831 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2832 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2833 sinfo->tx_failed))
2834 goto nla_put_failure;
2835 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2836 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2837 sinfo->beacon_loss_count))
2838 goto nla_put_failure;
f4263c98
PS
2839 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2840 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2841 if (!bss_param)
2842 goto nla_put_failure;
2843
9360ffd1
DM
2844 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2845 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2846 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2847 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2848 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2849 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2850 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2851 sinfo->bss_param.dtim_period) ||
2852 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2853 sinfo->bss_param.beacon_interval))
2854 goto nla_put_failure;
f4263c98
PS
2855
2856 nla_nest_end(msg, bss_param);
2857 }
9360ffd1
DM
2858 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2859 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2860 sizeof(struct nl80211_sta_flag_update),
2861 &sinfo->sta_flags))
2862 goto nla_put_failure;
7eab0f64
JL
2863 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2864 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2865 sinfo->t_offset))
2866 goto nla_put_failure;
2ec600d6 2867 nla_nest_end(msg, sinfoattr);
fd5b74dc 2868
9360ffd1
DM
2869 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2870 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2871 sinfo->assoc_req_ies))
2872 goto nla_put_failure;
50d3dfb7 2873
fd5b74dc
JB
2874 return genlmsg_end(msg, hdr);
2875
2876 nla_put_failure:
bc3ed28c
TG
2877 genlmsg_cancel(msg, hdr);
2878 return -EMSGSIZE;
fd5b74dc
JB
2879}
2880
2ec600d6 2881static int nl80211_dump_station(struct sk_buff *skb,
bba95fef 2882 struct netlink_callback *cb)
2ec600d6 2883{
2ec600d6
LCC
2884 struct station_info sinfo;
2885 struct cfg80211_registered_device *dev;
bba95fef 2886 struct net_device *netdev;
2ec600d6 2887 u8 mac_addr[ETH_ALEN];
bba95fef 2888 int sta_idx = cb->args[1];
2ec600d6 2889 int err;
2ec600d6 2890
67748893
JB
2891 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2892 if (err)
2893 return err;
bba95fef
JB
2894
2895 if (!dev->ops->dump_station) {
eec60b03 2896 err = -EOPNOTSUPP;
bba95fef
JB
2897 goto out_err;
2898 }
2899
bba95fef 2900 while (1) {
f612cedf 2901 memset(&sinfo, 0, sizeof(sinfo));
bba95fef
JB
2902 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2903 mac_addr, &sinfo);
2904 if (err == -ENOENT)
2905 break;
2906 if (err)
3b85875a 2907 goto out_err;
bba95fef
JB
2908
2909 if (nl80211_send_station(skb,
2910 NETLINK_CB(cb->skb).pid,
2911 cb->nlh->nlmsg_seq, NLM_F_MULTI,
66266b3a 2912 dev, netdev, mac_addr,
bba95fef
JB
2913 &sinfo) < 0)
2914 goto out;
2915
2916 sta_idx++;
2917 }
2918
2919
2920 out:
2921 cb->args[1] = sta_idx;
2922 err = skb->len;
bba95fef 2923 out_err:
67748893 2924 nl80211_finish_netdev_dump(dev);
bba95fef
JB
2925
2926 return err;
2ec600d6 2927}
fd5b74dc 2928
5727ef1b
JB
2929static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2930{
4c476991
JB
2931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2932 struct net_device *dev = info->user_ptr[1];
2ec600d6 2933 struct station_info sinfo;
fd5b74dc
JB
2934 struct sk_buff *msg;
2935 u8 *mac_addr = NULL;
4c476991 2936 int err;
fd5b74dc 2937
2ec600d6 2938 memset(&sinfo, 0, sizeof(sinfo));
fd5b74dc
JB
2939
2940 if (!info->attrs[NL80211_ATTR_MAC])
2941 return -EINVAL;
2942
2943 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2944
4c476991
JB
2945 if (!rdev->ops->get_station)
2946 return -EOPNOTSUPP;
3b85875a 2947
4c476991 2948 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
fd5b74dc 2949 if (err)
4c476991 2950 return err;
2ec600d6 2951
fd2120ca 2952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
fd5b74dc 2953 if (!msg)
4c476991 2954 return -ENOMEM;
fd5b74dc
JB
2955
2956 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
66266b3a 2957 rdev, dev, mac_addr, &sinfo) < 0) {
4c476991
JB
2958 nlmsg_free(msg);
2959 return -ENOBUFS;
2960 }
3b85875a 2961
4c476991 2962 return genlmsg_reply(msg, info);
5727ef1b
JB
2963}
2964
2965/*
c258d2de 2966 * Get vlan interface making sure it is running and on the right wiphy.
5727ef1b 2967 */
80b99899
JB
2968static struct net_device *get_vlan(struct genl_info *info,
2969 struct cfg80211_registered_device *rdev)
5727ef1b 2970{
463d0183 2971 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
80b99899
JB
2972 struct net_device *v;
2973 int ret;
2974
2975 if (!vlanattr)
2976 return NULL;
2977
2978 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2979 if (!v)
2980 return ERR_PTR(-ENODEV);
2981
2982 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2983 ret = -EINVAL;
2984 goto error;
5727ef1b 2985 }
80b99899
JB
2986
2987 if (!netif_running(v)) {
2988 ret = -ENETDOWN;
2989 goto error;
2990 }
2991
2992 return v;
2993 error:
2994 dev_put(v);
2995 return ERR_PTR(ret);
5727ef1b
JB
2996}
2997
2998static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2999{
4c476991 3000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5727ef1b 3001 int err;
4c476991 3002 struct net_device *dev = info->user_ptr[1];
5727ef1b
JB
3003 struct station_parameters params;
3004 u8 *mac_addr = NULL;
3005
3006 memset(&params, 0, sizeof(params));
3007
3008 params.listen_interval = -1;
57cf8043 3009 params.plink_state = -1;
5727ef1b
JB
3010
3011 if (info->attrs[NL80211_ATTR_STA_AID])
3012 return -EINVAL;
3013
3014 if (!info->attrs[NL80211_ATTR_MAC])
3015 return -EINVAL;
3016
3017 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3018
3019 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3020 params.supported_rates =
3021 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3022 params.supported_rates_len =
3023 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3024 }
3025
3026 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3027 params.listen_interval =
3028 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3029
36aedc90
JM
3030 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3031 params.ht_capa =
3032 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3033
bdd90d5e
JB
3034 if (!rdev->ops->change_station)
3035 return -EOPNOTSUPP;
3036
bdd3ae3d 3037 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5727ef1b
JB
3038 return -EINVAL;
3039
2ec600d6
LCC
3040 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3041 params.plink_action =
3042 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3043
9c3990aa
JC
3044 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3045 params.plink_state =
3046 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3047
a97f4424
JB
3048 switch (dev->ieee80211_ptr->iftype) {
3049 case NL80211_IFTYPE_AP:
3050 case NL80211_IFTYPE_AP_VLAN:
074ac8df 3051 case NL80211_IFTYPE_P2P_GO:
a97f4424
JB
3052 /* disallow mesh-specific things */
3053 if (params.plink_action)
bdd90d5e
JB
3054 return -EINVAL;
3055
3056 /* TDLS can't be set, ... */
3057 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3058 return -EINVAL;
3059 /*
3060 * ... but don't bother the driver with it. This works around
3061 * a hostapd/wpa_supplicant issue -- it always includes the
3062 * TLDS_PEER flag in the mask even for AP mode.
3063 */
3064 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3065
3066 /* accept only the listed bits */
3067 if (params.sta_flags_mask &
3068 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3069 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3070 BIT(NL80211_STA_FLAG_WME) |
3071 BIT(NL80211_STA_FLAG_MFP)))
3072 return -EINVAL;
3073
3074 /* must be last in here for error handling */
3075 params.vlan = get_vlan(info, rdev);
3076 if (IS_ERR(params.vlan))
3077 return PTR_ERR(params.vlan);
a97f4424 3078 break;
074ac8df 3079 case NL80211_IFTYPE_P2P_CLIENT:
a97f4424 3080 case NL80211_IFTYPE_STATION:
bdd90d5e
JB
3081 /*
3082 * Don't allow userspace to change the TDLS_PEER flag,
3083 * but silently ignore attempts to change it since we
3084 * don't have state here to verify that it doesn't try
3085 * to change the flag.
3086 */
3087 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
267335d6
AQ
3088 /* fall through */
3089 case NL80211_IFTYPE_ADHOC:
3090 /* disallow things sta doesn't support */
3091 if (params.plink_action)
3092 return -EINVAL;
3093 if (params.ht_capa)
3094 return -EINVAL;
3095 if (params.listen_interval >= 0)
3096 return -EINVAL;
bdd90d5e
JB
3097 /* reject any changes other than AUTHORIZED */
3098 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3099 return -EINVAL;
a97f4424
JB
3100 break;
3101 case NL80211_IFTYPE_MESH_POINT:
3102 /* disallow things mesh doesn't support */
3103 if (params.vlan)
bdd90d5e 3104 return -EINVAL;
a97f4424 3105 if (params.ht_capa)
bdd90d5e 3106 return -EINVAL;
a97f4424 3107 if (params.listen_interval >= 0)
bdd90d5e
JB
3108 return -EINVAL;
3109 /*
3110 * No special handling for TDLS here -- the userspace
3111 * mesh code doesn't have this bug.
3112 */
b39c48fa
JC
3113 if (params.sta_flags_mask &
3114 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
8429828e 3115 BIT(NL80211_STA_FLAG_MFP) |
b39c48fa 3116 BIT(NL80211_STA_FLAG_AUTHORIZED)))
bdd90d5e 3117 return -EINVAL;
a97f4424
JB
3118 break;
3119 default:
bdd90d5e 3120 return -EOPNOTSUPP;
034d655e
JB
3121 }
3122
bdd90d5e 3123 /* be aware of params.vlan when changing code here */
5727ef1b 3124
79c97e97 3125 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
5727ef1b 3126
5727ef1b
JB
3127 if (params.vlan)
3128 dev_put(params.vlan);
3b85875a 3129
5727ef1b
JB
3130 return err;
3131}
3132
c75786c9
EP
3133static struct nla_policy
3134nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3135 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3136 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3137};
3138
5727ef1b
JB
3139static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3140{
4c476991 3141 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5727ef1b 3142 int err;
4c476991 3143 struct net_device *dev = info->user_ptr[1];
5727ef1b
JB
3144 struct station_parameters params;
3145 u8 *mac_addr = NULL;
3146
3147 memset(&params, 0, sizeof(params));
3148
3149 if (!info->attrs[NL80211_ATTR_MAC])
3150 return -EINVAL;
3151
5727ef1b
JB
3152 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3153 return -EINVAL;
3154
3155 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3156 return -EINVAL;
3157
0e956c13
TLSC
3158 if (!info->attrs[NL80211_ATTR_STA_AID])
3159 return -EINVAL;
3160
5727ef1b
JB
3161 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3162 params.supported_rates =
3163 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3164 params.supported_rates_len =
3165 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3166 params.listen_interval =
3167 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
51b50fbe 3168
0e956c13
TLSC
3169 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3170 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3171 return -EINVAL;
51b50fbe 3172
36aedc90
JM
3173 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3174 params.ht_capa =
3175 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5727ef1b 3176
96b78dff
JC
3177 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3178 params.plink_action =
3179 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3180
bdd90d5e
JB
3181 if (!rdev->ops->add_station)
3182 return -EOPNOTSUPP;
3183
bdd3ae3d 3184 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5727ef1b
JB
3185 return -EINVAL;
3186
bdd90d5e
JB
3187 switch (dev->ieee80211_ptr->iftype) {
3188 case NL80211_IFTYPE_AP:
3189 case NL80211_IFTYPE_AP_VLAN:
3190 case NL80211_IFTYPE_P2P_GO:
3191 /* parse WME attributes if sta is WME capable */
3192 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3193 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3194 info->attrs[NL80211_ATTR_STA_WME]) {
3195 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3196 struct nlattr *nla;
3197
3198 nla = info->attrs[NL80211_ATTR_STA_WME];
3199 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3200 nl80211_sta_wme_policy);
3201 if (err)
3202 return err;
3203
3204 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3205 params.uapsd_queues =
3206 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3207 if (params.uapsd_queues &
3208 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3209 return -EINVAL;
c75786c9 3210
bdd90d5e
JB
3211 if (tb[NL80211_STA_WME_MAX_SP])
3212 params.max_sp =
3213 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
c75786c9 3214
bdd90d5e
JB
3215 if (params.max_sp &
3216 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3217 return -EINVAL;
4319e193 3218
bdd90d5e
JB
3219 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3220 }
3221 /* TDLS peers cannot be added */
3222 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4319e193 3223 return -EINVAL;
bdd90d5e
JB
3224 /* but don't bother the driver with it */
3225 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3b9ce80c 3226
bdd90d5e
JB
3227 /* must be last in here for error handling */
3228 params.vlan = get_vlan(info, rdev);
3229 if (IS_ERR(params.vlan))
3230 return PTR_ERR(params.vlan);
3231 break;
3232 case NL80211_IFTYPE_MESH_POINT:
3233 /* TDLS peers cannot be added */
3234 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3235 return -EINVAL;
3236 break;
3237 case NL80211_IFTYPE_STATION:
3238 /* Only TDLS peers can be added */
3239 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3240 return -EINVAL;
3241 /* Can only add if TDLS ... */
3242 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3243 return -EOPNOTSUPP;
3244 /* ... with external setup is supported */
3245 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3246 return -EOPNOTSUPP;
3247 break;
3248 default:
3249 return -EOPNOTSUPP;
c75786c9
EP
3250 }
3251
bdd90d5e 3252 /* be aware of params.vlan when changing code here */
5727ef1b 3253
79c97e97 3254 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
5727ef1b 3255
5727ef1b
JB
3256 if (params.vlan)
3257 dev_put(params.vlan);
5727ef1b
JB
3258 return err;
3259}
3260
3261static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3262{
4c476991
JB
3263 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3264 struct net_device *dev = info->user_ptr[1];
5727ef1b
JB
3265 u8 *mac_addr = NULL;
3266
3267 if (info->attrs[NL80211_ATTR_MAC])
3268 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3269
e80cf853 3270 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
d5d9de02 3271 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
074ac8df 3272 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
4c476991
JB
3273 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3274 return -EINVAL;
5727ef1b 3275
4c476991
JB
3276 if (!rdev->ops->del_station)
3277 return -EOPNOTSUPP;
3b85875a 3278
4c476991 3279 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
5727ef1b
JB
3280}
3281
2ec600d6
LCC
3282static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3283 int flags, struct net_device *dev,
3284 u8 *dst, u8 *next_hop,
3285 struct mpath_info *pinfo)
3286{
3287 void *hdr;
3288 struct nlattr *pinfoattr;
3289
3290 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3291 if (!hdr)
3292 return -1;
3293
9360ffd1
DM
3294 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3295 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3296 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3297 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3298 goto nla_put_failure;
f5ea9120 3299
2ec600d6
LCC
3300 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3301 if (!pinfoattr)
3302 goto nla_put_failure;
9360ffd1
DM
3303 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3304 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3305 pinfo->frame_qlen))
3306 goto nla_put_failure;
3307 if (((pinfo->filled & MPATH_INFO_SN) &&
3308 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3309 ((pinfo->filled & MPATH_INFO_METRIC) &&
3310 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3311 pinfo->metric)) ||
3312 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3313 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3314 pinfo->exptime)) ||
3315 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3316 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3317 pinfo->flags)) ||
3318 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3319 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3320 pinfo->discovery_timeout)) ||
3321 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3322 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3323 pinfo->discovery_retries)))
3324 goto nla_put_failure;
2ec600d6
LCC
3325
3326 nla_nest_end(msg, pinfoattr);
3327
3328 return genlmsg_end(msg, hdr);
3329
3330 nla_put_failure:
bc3ed28c
TG
3331 genlmsg_cancel(msg, hdr);
3332 return -EMSGSIZE;
2ec600d6
LCC
3333}
3334
3335static int nl80211_dump_mpath(struct sk_buff *skb,
bba95fef 3336 struct netlink_callback *cb)
2ec600d6 3337{
2ec600d6
LCC
3338 struct mpath_info pinfo;
3339 struct cfg80211_registered_device *dev;
bba95fef 3340 struct net_device *netdev;
2ec600d6
LCC
3341 u8 dst[ETH_ALEN];
3342 u8 next_hop[ETH_ALEN];
bba95fef 3343 int path_idx = cb->args[1];
2ec600d6 3344 int err;
2ec600d6 3345
67748893
JB
3346 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3347 if (err)
3348 return err;
bba95fef
JB
3349
3350 if (!dev->ops->dump_mpath) {
eec60b03 3351 err = -EOPNOTSUPP;
bba95fef
JB
3352 goto out_err;
3353 }
3354
eec60b03
JM
3355 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3356 err = -EOPNOTSUPP;
0448b5fc 3357 goto out_err;
eec60b03
JM
3358 }
3359
bba95fef
JB
3360 while (1) {
3361 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3362 dst, next_hop, &pinfo);
3363 if (err == -ENOENT)
2ec600d6 3364 break;
bba95fef 3365 if (err)
3b85875a 3366 goto out_err;
2ec600d6 3367
bba95fef
JB
3368 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3369 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3370 netdev, dst, next_hop,
3371 &pinfo) < 0)
3372 goto out;
2ec600d6 3373
bba95fef 3374 path_idx++;
2ec600d6 3375 }
2ec600d6 3376
2ec600d6 3377
bba95fef
JB
3378 out:
3379 cb->args[1] = path_idx;
3380 err = skb->len;
bba95fef 3381 out_err:
67748893 3382 nl80211_finish_netdev_dump(dev);
bba95fef 3383 return err;
2ec600d6
LCC
3384}
3385
3386static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3387{
4c476991 3388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2ec600d6 3389 int err;
4c476991 3390 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
3391 struct mpath_info pinfo;
3392 struct sk_buff *msg;
3393 u8 *dst = NULL;
3394 u8 next_hop[ETH_ALEN];
3395
3396 memset(&pinfo, 0, sizeof(pinfo));
3397
3398 if (!info->attrs[NL80211_ATTR_MAC])
3399 return -EINVAL;
3400
3401 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3402
4c476991
JB
3403 if (!rdev->ops->get_mpath)
3404 return -EOPNOTSUPP;
2ec600d6 3405
4c476991
JB
3406 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3407 return -EOPNOTSUPP;
eec60b03 3408
79c97e97 3409 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
2ec600d6 3410 if (err)
4c476991 3411 return err;
2ec600d6 3412
fd2120ca 3413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2ec600d6 3414 if (!msg)
4c476991 3415 return -ENOMEM;
2ec600d6
LCC
3416
3417 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
4c476991
JB
3418 dev, dst, next_hop, &pinfo) < 0) {
3419 nlmsg_free(msg);
3420 return -ENOBUFS;
3421 }
3b85875a 3422
4c476991 3423 return genlmsg_reply(msg, info);
2ec600d6
LCC
3424}
3425
3426static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3427{
4c476991
JB
3428 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3429 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
3430 u8 *dst = NULL;
3431 u8 *next_hop = NULL;
3432
3433 if (!info->attrs[NL80211_ATTR_MAC])
3434 return -EINVAL;
3435
3436 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3437 return -EINVAL;
3438
3439 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3440 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3441
4c476991
JB
3442 if (!rdev->ops->change_mpath)
3443 return -EOPNOTSUPP;
35a8efe1 3444
4c476991
JB
3445 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3446 return -EOPNOTSUPP;
2ec600d6 3447
4c476991 3448 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
2ec600d6 3449}
4c476991 3450
2ec600d6
LCC
3451static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3452{
4c476991
JB
3453 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3454 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
3455 u8 *dst = NULL;
3456 u8 *next_hop = NULL;
3457
3458 if (!info->attrs[NL80211_ATTR_MAC])
3459 return -EINVAL;
3460
3461 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3462 return -EINVAL;
3463
3464 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3465 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3466
4c476991
JB
3467 if (!rdev->ops->add_mpath)
3468 return -EOPNOTSUPP;
35a8efe1 3469
4c476991
JB
3470 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3471 return -EOPNOTSUPP;
2ec600d6 3472
4c476991 3473 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
2ec600d6
LCC
3474}
3475
3476static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3477{
4c476991
JB
3478 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3479 struct net_device *dev = info->user_ptr[1];
2ec600d6
LCC
3480 u8 *dst = NULL;
3481
3482 if (info->attrs[NL80211_ATTR_MAC])
3483 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3484
4c476991
JB
3485 if (!rdev->ops->del_mpath)
3486 return -EOPNOTSUPP;
3b85875a 3487
4c476991 3488 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
2ec600d6
LCC
3489}
3490
9f1ba906
JM
3491static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3492{
4c476991
JB
3493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3494 struct net_device *dev = info->user_ptr[1];
9f1ba906
JM
3495 struct bss_parameters params;
3496
3497 memset(&params, 0, sizeof(params));
3498 /* default to not changing parameters */
3499 params.use_cts_prot = -1;
3500 params.use_short_preamble = -1;
3501 params.use_short_slot_time = -1;
fd8aaaf3 3502 params.ap_isolate = -1;
50b12f59 3503 params.ht_opmode = -1;
9f1ba906
JM
3504
3505 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3506 params.use_cts_prot =
3507 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3508 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3509 params.use_short_preamble =
3510 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3511 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3512 params.use_short_slot_time =
3513 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
90c97a04
JM
3514 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3515 params.basic_rates =
3516 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3517 params.basic_rates_len =
3518 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3519 }
fd8aaaf3
FF
3520 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3521 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
50b12f59
HS
3522 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3523 params.ht_opmode =
3524 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
9f1ba906 3525
4c476991
JB
3526 if (!rdev->ops->change_bss)
3527 return -EOPNOTSUPP;
9f1ba906 3528
074ac8df 3529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4c476991
JB
3530 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3531 return -EOPNOTSUPP;
3b85875a 3532
4c476991 3533 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
9f1ba906
JM
3534}
3535
b54452b0 3536static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
b2e1b302
LR
3537 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3538 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3539 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3540 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3541 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3542 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3543};
3544
3545static int parse_reg_rule(struct nlattr *tb[],
3546 struct ieee80211_reg_rule *reg_rule)
3547{
3548 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3549 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3550
3551 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3552 return -EINVAL;
3553 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3554 return -EINVAL;
3555 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3556 return -EINVAL;
3557 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3558 return -EINVAL;
3559 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3560 return -EINVAL;
3561
3562 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3563
3564 freq_range->start_freq_khz =
3565 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3566 freq_range->end_freq_khz =
3567 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3568 freq_range->max_bandwidth_khz =
3569 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3570
3571 power_rule->max_eirp =
3572 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3573
3574 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3575 power_rule->max_antenna_gain =
3576 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3577
3578 return 0;
3579}
3580
3581static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3582{
3583 int r;
3584 char *data = NULL;
3585
80778f18
LR
3586 /*
3587 * You should only get this when cfg80211 hasn't yet initialized
3588 * completely when built-in to the kernel right between the time
3589 * window between nl80211_init() and regulatory_init(), if that is
3590 * even possible.
3591 */
3592 mutex_lock(&cfg80211_mutex);
3593 if (unlikely(!cfg80211_regdomain)) {
fe33eb39
LR
3594 mutex_unlock(&cfg80211_mutex);
3595 return -EINPROGRESS;
80778f18 3596 }
fe33eb39 3597 mutex_unlock(&cfg80211_mutex);
80778f18 3598
fe33eb39
LR
3599 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3600 return -EINVAL;
b2e1b302
LR
3601
3602 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3603
fe33eb39
LR
3604 r = regulatory_hint_user(data);
3605
b2e1b302
LR
3606 return r;
3607}
3608
24bdd9f4 3609static int nl80211_get_mesh_config(struct sk_buff *skb,
29cbe68c 3610 struct genl_info *info)
93da9cc1 3611{
4c476991 3612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4c476991 3613 struct net_device *dev = info->user_ptr[1];
29cbe68c
JB
3614 struct wireless_dev *wdev = dev->ieee80211_ptr;
3615 struct mesh_config cur_params;
3616 int err = 0;
93da9cc1 3617 void *hdr;
3618 struct nlattr *pinfoattr;
3619 struct sk_buff *msg;
3620
29cbe68c
JB
3621 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3622 return -EOPNOTSUPP;
3623
24bdd9f4 3624 if (!rdev->ops->get_mesh_config)
4c476991 3625 return -EOPNOTSUPP;
f3f92586 3626
29cbe68c
JB
3627 wdev_lock(wdev);
3628 /* If not connected, get default parameters */
3629 if (!wdev->mesh_id_len)
3630 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3631 else
24bdd9f4 3632 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
29cbe68c
JB
3633 &cur_params);
3634 wdev_unlock(wdev);
3635
93da9cc1 3636 if (err)
4c476991 3637 return err;
93da9cc1 3638
3639 /* Draw up a netlink message to send back */
fd2120ca 3640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
3641 if (!msg)
3642 return -ENOMEM;
93da9cc1 3643 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
24bdd9f4 3644 NL80211_CMD_GET_MESH_CONFIG);
93da9cc1 3645 if (!hdr)
efe1cf0c 3646 goto out;
24bdd9f4 3647 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
93da9cc1 3648 if (!pinfoattr)
3649 goto nla_put_failure;
9360ffd1
DM
3650 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3651 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3652 cur_params.dot11MeshRetryTimeout) ||
3653 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3654 cur_params.dot11MeshConfirmTimeout) ||
3655 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3656 cur_params.dot11MeshHoldingTimeout) ||
3657 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3658 cur_params.dot11MeshMaxPeerLinks) ||
3659 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3660 cur_params.dot11MeshMaxRetries) ||
3661 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3662 cur_params.dot11MeshTTL) ||
3663 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3664 cur_params.element_ttl) ||
3665 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3666 cur_params.auto_open_plinks) ||
7eab0f64
JL
3667 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3668 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
9360ffd1
DM
3669 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3670 cur_params.dot11MeshHWMPmaxPREQretries) ||
3671 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3672 cur_params.path_refresh_time) ||
3673 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3674 cur_params.min_discovery_timeout) ||
3675 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3676 cur_params.dot11MeshHWMPactivePathTimeout) ||
3677 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3678 cur_params.dot11MeshHWMPpreqMinInterval) ||
3679 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3680 cur_params.dot11MeshHWMPperrMinInterval) ||
3681 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3682 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3683 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3684 cur_params.dot11MeshHWMPRootMode) ||
3685 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3686 cur_params.dot11MeshHWMPRannInterval) ||
3687 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3688 cur_params.dot11MeshGateAnnouncementProtocol) ||
3689 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3690 cur_params.dot11MeshForwarding) ||
3691 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
70c33eaa
AN
3692 cur_params.rssi_threshold) ||
3693 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
ac1073a6
CYY
3694 cur_params.ht_opmode) ||
3695 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3696 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3697 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
728b19e5
CYY
3698 cur_params.dot11MeshHWMProotInterval) ||
3699 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3700 cur_params.dot11MeshHWMPconfirmationInterval))
9360ffd1 3701 goto nla_put_failure;
93da9cc1 3702 nla_nest_end(msg, pinfoattr);
3703 genlmsg_end(msg, hdr);
4c476991 3704 return genlmsg_reply(msg, info);
93da9cc1 3705
3b85875a 3706 nla_put_failure:
93da9cc1 3707 genlmsg_cancel(msg, hdr);
efe1cf0c 3708 out:
d080e275 3709 nlmsg_free(msg);
4c476991 3710 return -ENOBUFS;
93da9cc1 3711}
3712
b54452b0 3713static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
93da9cc1 3714 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3715 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3716 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3717 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3718 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3719 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
45904f21 3720 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
93da9cc1 3721 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
d299a1f2 3722 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
93da9cc1 3723 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3724 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3725 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3726 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3727 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
dca7e943 3728 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
93da9cc1 3729 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
699403db 3730 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
0507e159 3731 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
16dd7267 3732 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
94f90656 3733 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
a4f606ea
CYY
3734 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3735 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
ac1073a6
CYY
3736 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3737 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
728b19e5 3738 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
93da9cc1 3739};
3740
c80d545d
JC
3741static const struct nla_policy
3742 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
d299a1f2 3743 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
c80d545d
JC
3744 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3745 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
15d5dda6 3746 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
581a8b0f 3747 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
a4f606ea 3748 .len = IEEE80211_MAX_DATA_LEN },
b130e5ce 3749 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
c80d545d
JC
3750};
3751
24bdd9f4 3752static int nl80211_parse_mesh_config(struct genl_info *info,
bd90fdcc
JB
3753 struct mesh_config *cfg,
3754 u32 *mask_out)
93da9cc1 3755{
93da9cc1 3756 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
bd90fdcc 3757 u32 mask = 0;
93da9cc1 3758
bd90fdcc
JB
3759#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3760do {\
3761 if (table[attr_num]) {\
3762 cfg->param = nla_fn(table[attr_num]); \
3763 mask |= (1 << (attr_num - 1)); \
3764 } \
3765} while (0);\
3766
3767
24bdd9f4 3768 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
93da9cc1 3769 return -EINVAL;
3770 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
24bdd9f4 3771 info->attrs[NL80211_ATTR_MESH_CONFIG],
bd90fdcc 3772 nl80211_meshconf_params_policy))
93da9cc1 3773 return -EINVAL;
3774
93da9cc1 3775 /* This makes sure that there aren't more than 32 mesh config
3776 * parameters (otherwise our bitfield scheme would not work.) */
3777 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3778
3779 /* Fill in the params struct */
93da9cc1 3780 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
a4f606ea
CYY
3781 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3782 nla_get_u16);
93da9cc1 3783 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
a4f606ea
CYY
3784 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3785 nla_get_u16);
93da9cc1 3786 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
a4f606ea
CYY
3787 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3788 nla_get_u16);
93da9cc1 3789 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
a4f606ea
CYY
3790 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3791 nla_get_u16);
93da9cc1 3792 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
a4f606ea
CYY
3793 mask, NL80211_MESHCONF_MAX_RETRIES,
3794 nla_get_u8);
93da9cc1 3795 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
a4f606ea 3796 mask, NL80211_MESHCONF_TTL, nla_get_u8);
45904f21 3797 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
a4f606ea
CYY
3798 mask, NL80211_MESHCONF_ELEMENT_TTL,
3799 nla_get_u8);
93da9cc1 3800 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
a4f606ea
CYY
3801 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3802 nla_get_u8);
3803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3804 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3805 nla_get_u32);
93da9cc1 3806 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
a4f606ea
CYY
3807 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3808 nla_get_u8);
93da9cc1 3809 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
a4f606ea
CYY
3810 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3811 nla_get_u32);
93da9cc1 3812 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
a4f606ea
CYY
3813 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3814 nla_get_u16);
3815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3816 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3817 nla_get_u32);
93da9cc1 3818 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
a4f606ea
CYY
3819 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3820 nla_get_u16);
dca7e943 3821 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
a4f606ea
CYY
3822 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3823 nla_get_u16);
0507e159 3824 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
a4f606ea
CYY
3825 dot11MeshHWMPnetDiameterTraversalTime, mask,
3826 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3827 nla_get_u16);
3828 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3829 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3830 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3831 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3832 nla_get_u16);
16dd7267 3833 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
a4f606ea
CYY
3834 dot11MeshGateAnnouncementProtocol, mask,
3835 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3836 nla_get_u8);
94f90656 3837 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
a4f606ea
CYY
3838 mask, NL80211_MESHCONF_FORWARDING,
3839 nla_get_u8);
55335137 3840 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
a4f606ea
CYY
3841 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3842 nla_get_u32);
70c33eaa 3843 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
a4f606ea 3844 mask, NL80211_MESHCONF_HT_OPMODE,
ac1073a6
CYY
3845 nla_get_u16);
3846 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3847 mask,
3848 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3849 nla_get_u32);
3850 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3851 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
728b19e5
CYY
3852 nla_get_u16);
3853 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3854 dot11MeshHWMPconfirmationInterval, mask,
3855 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
a4f606ea 3856 nla_get_u16);
bd90fdcc
JB
3857 if (mask_out)
3858 *mask_out = mask;
c80d545d 3859
bd90fdcc
JB
3860 return 0;
3861
3862#undef FILL_IN_MESH_PARAM_IF_SET
3863}
3864
c80d545d
JC
3865static int nl80211_parse_mesh_setup(struct genl_info *info,
3866 struct mesh_setup *setup)
3867{
3868 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3869
3870 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3871 return -EINVAL;
3872 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3873 info->attrs[NL80211_ATTR_MESH_SETUP],
3874 nl80211_mesh_setup_params_policy))
3875 return -EINVAL;
3876
d299a1f2
JC
3877 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3878 setup->sync_method =
3879 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3880 IEEE80211_SYNC_METHOD_VENDOR :
3881 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3882
c80d545d
JC
3883 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3884 setup->path_sel_proto =
3885 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3886 IEEE80211_PATH_PROTOCOL_VENDOR :
3887 IEEE80211_PATH_PROTOCOL_HWMP;
3888
3889 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3890 setup->path_metric =
3891 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3892 IEEE80211_PATH_METRIC_VENDOR :
3893 IEEE80211_PATH_METRIC_AIRTIME;
3894
581a8b0f
JC
3895
3896 if (tb[NL80211_MESH_SETUP_IE]) {
c80d545d 3897 struct nlattr *ieattr =
581a8b0f 3898 tb[NL80211_MESH_SETUP_IE];
c80d545d
JC
3899 if (!is_valid_ie_attr(ieattr))
3900 return -EINVAL;
581a8b0f
JC
3901 setup->ie = nla_data(ieattr);
3902 setup->ie_len = nla_len(ieattr);
c80d545d 3903 }
b130e5ce
JC
3904 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3905 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
c80d545d
JC
3906
3907 return 0;
3908}
3909
24bdd9f4 3910static int nl80211_update_mesh_config(struct sk_buff *skb,
29cbe68c 3911 struct genl_info *info)
bd90fdcc
JB
3912{
3913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3914 struct net_device *dev = info->user_ptr[1];
29cbe68c 3915 struct wireless_dev *wdev = dev->ieee80211_ptr;
bd90fdcc
JB
3916 struct mesh_config cfg;
3917 u32 mask;
3918 int err;
3919
29cbe68c
JB
3920 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3921 return -EOPNOTSUPP;
3922
24bdd9f4 3923 if (!rdev->ops->update_mesh_config)
bd90fdcc
JB
3924 return -EOPNOTSUPP;
3925
24bdd9f4 3926 err = nl80211_parse_mesh_config(info, &cfg, &mask);
bd90fdcc
JB
3927 if (err)
3928 return err;
3929
29cbe68c
JB
3930 wdev_lock(wdev);
3931 if (!wdev->mesh_id_len)
3932 err = -ENOLINK;
3933
3934 if (!err)
24bdd9f4 3935 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
29cbe68c
JB
3936 mask, &cfg);
3937
3938 wdev_unlock(wdev);
3939
3940 return err;
93da9cc1 3941}
3942
f130347c
LR
3943static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3944{
3945 struct sk_buff *msg;
3946 void *hdr = NULL;
3947 struct nlattr *nl_reg_rules;
3948 unsigned int i;
3949 int err = -EINVAL;
3950
a1794390 3951 mutex_lock(&cfg80211_mutex);
f130347c
LR
3952
3953 if (!cfg80211_regdomain)
3954 goto out;
3955
fd2120ca 3956 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
f130347c
LR
3957 if (!msg) {
3958 err = -ENOBUFS;
3959 goto out;
3960 }
3961
3962 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3963 NL80211_CMD_GET_REG);
3964 if (!hdr)
efe1cf0c 3965 goto put_failure;
f130347c 3966
9360ffd1
DM
3967 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3968 cfg80211_regdomain->alpha2) ||
3969 (cfg80211_regdomain->dfs_region &&
3970 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3971 cfg80211_regdomain->dfs_region)))
3972 goto nla_put_failure;
f130347c
LR
3973
3974 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3975 if (!nl_reg_rules)
3976 goto nla_put_failure;
3977
3978 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3979 struct nlattr *nl_reg_rule;
3980 const struct ieee80211_reg_rule *reg_rule;
3981 const struct ieee80211_freq_range *freq_range;
3982 const struct ieee80211_power_rule *power_rule;
3983
3984 reg_rule = &cfg80211_regdomain->reg_rules[i];
3985 freq_range = &reg_rule->freq_range;
3986 power_rule = &reg_rule->power_rule;
3987
3988 nl_reg_rule = nla_nest_start(msg, i);
3989 if (!nl_reg_rule)
3990 goto nla_put_failure;
3991
9360ffd1
DM
3992 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3993 reg_rule->flags) ||
3994 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3995 freq_range->start_freq_khz) ||
3996 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3997 freq_range->end_freq_khz) ||
3998 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3999 freq_range->max_bandwidth_khz) ||
4000 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4001 power_rule->max_antenna_gain) ||
4002 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4003 power_rule->max_eirp))
4004 goto nla_put_failure;
f130347c
LR
4005
4006 nla_nest_end(msg, nl_reg_rule);
4007 }
4008
4009 nla_nest_end(msg, nl_reg_rules);
4010
4011 genlmsg_end(msg, hdr);
134e6375 4012 err = genlmsg_reply(msg, info);
f130347c
LR
4013 goto out;
4014
4015nla_put_failure:
4016 genlmsg_cancel(msg, hdr);
efe1cf0c 4017put_failure:
d080e275 4018 nlmsg_free(msg);
f130347c
LR
4019 err = -EMSGSIZE;
4020out:
a1794390 4021 mutex_unlock(&cfg80211_mutex);
f130347c
LR
4022 return err;
4023}
4024
b2e1b302
LR
4025static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4026{
4027 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4028 struct nlattr *nl_reg_rule;
4029 char *alpha2 = NULL;
4030 int rem_reg_rules = 0, r = 0;
4031 u32 num_rules = 0, rule_idx = 0, size_of_regd;
8b60b078 4032 u8 dfs_region = 0;
b2e1b302
LR
4033 struct ieee80211_regdomain *rd = NULL;
4034
4035 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4036 return -EINVAL;
4037
4038 if (!info->attrs[NL80211_ATTR_REG_RULES])
4039 return -EINVAL;
4040
4041 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4042
8b60b078
LR
4043 if (info->attrs[NL80211_ATTR_DFS_REGION])
4044 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4045
b2e1b302
LR
4046 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4047 rem_reg_rules) {
4048 num_rules++;
4049 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
4776c6e7 4050 return -EINVAL;
b2e1b302
LR
4051 }
4052
61405e97
LR
4053 mutex_lock(&cfg80211_mutex);
4054
d0e18f83
LR
4055 if (!reg_is_valid_request(alpha2)) {
4056 r = -EINVAL;
4057 goto bad_reg;
4058 }
b2e1b302
LR
4059
4060 size_of_regd = sizeof(struct ieee80211_regdomain) +
4061 (num_rules * sizeof(struct ieee80211_reg_rule));
4062
4063 rd = kzalloc(size_of_regd, GFP_KERNEL);
d0e18f83
LR
4064 if (!rd) {
4065 r = -ENOMEM;
4066 goto bad_reg;
4067 }
b2e1b302
LR
4068
4069 rd->n_reg_rules = num_rules;
4070 rd->alpha2[0] = alpha2[0];
4071 rd->alpha2[1] = alpha2[1];
4072
8b60b078
LR
4073 /*
4074 * Disable DFS master mode if the DFS region was
4075 * not supported or known on this kernel.
4076 */
4077 if (reg_supported_dfs_region(dfs_region))
4078 rd->dfs_region = dfs_region;
4079
b2e1b302
LR
4080 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4081 rem_reg_rules) {
4082 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4083 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4084 reg_rule_policy);
4085 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4086 if (r)
4087 goto bad_reg;
4088
4089 rule_idx++;
4090
d0e18f83
LR
4091 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4092 r = -EINVAL;
b2e1b302 4093 goto bad_reg;
d0e18f83 4094 }
b2e1b302
LR
4095 }
4096
4097 BUG_ON(rule_idx != num_rules);
4098
b2e1b302 4099 r = set_regdom(rd);
61405e97 4100
a1794390 4101 mutex_unlock(&cfg80211_mutex);
d0e18f83 4102
b2e1b302
LR
4103 return r;
4104
d2372b31 4105 bad_reg:
61405e97 4106 mutex_unlock(&cfg80211_mutex);
b2e1b302 4107 kfree(rd);
d0e18f83 4108 return r;
b2e1b302
LR
4109}
4110
83f5e2cf
JB
4111static int validate_scan_freqs(struct nlattr *freqs)
4112{
4113 struct nlattr *attr1, *attr2;
4114 int n_channels = 0, tmp1, tmp2;
4115
4116 nla_for_each_nested(attr1, freqs, tmp1) {
4117 n_channels++;
4118 /*
4119 * Some hardware has a limited channel list for
4120 * scanning, and it is pretty much nonsensical
4121 * to scan for a channel twice, so disallow that
4122 * and don't require drivers to check that the
4123 * channel list they get isn't longer than what
4124 * they can scan, as long as they can scan all
4125 * the channels they registered at once.
4126 */
4127 nla_for_each_nested(attr2, freqs, tmp2)
4128 if (attr1 != attr2 &&
4129 nla_get_u32(attr1) == nla_get_u32(attr2))
4130 return 0;
4131 }
4132
4133 return n_channels;
4134}
4135
2a519311
JB
4136static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4137{
4c476991 4138 struct cfg80211_registered_device *rdev = info->user_ptr[0];
fd014284 4139 struct wireless_dev *wdev = info->user_ptr[1];
2a519311 4140 struct cfg80211_scan_request *request;
2a519311
JB
4141 struct nlattr *attr;
4142 struct wiphy *wiphy;
83f5e2cf 4143 int err, tmp, n_ssids = 0, n_channels, i;
70692ad2 4144 size_t ie_len;
2a519311 4145
f4a11bb0
JB
4146 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4147 return -EINVAL;
4148
79c97e97 4149 wiphy = &rdev->wiphy;
2a519311 4150
4c476991
JB
4151 if (!rdev->ops->scan)
4152 return -EOPNOTSUPP;
2a519311 4153
4c476991
JB
4154 if (rdev->scan_req)
4155 return -EBUSY;
2a519311
JB
4156
4157 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
83f5e2cf
JB
4158 n_channels = validate_scan_freqs(
4159 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4c476991
JB
4160 if (!n_channels)
4161 return -EINVAL;
2a519311 4162 } else {
34850ab2 4163 enum ieee80211_band band;
83f5e2cf
JB
4164 n_channels = 0;
4165
2a519311
JB
4166 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4167 if (wiphy->bands[band])
4168 n_channels += wiphy->bands[band]->n_channels;
4169 }
4170
4171 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4172 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4173 n_ssids++;
4174
4c476991
JB
4175 if (n_ssids > wiphy->max_scan_ssids)
4176 return -EINVAL;
2a519311 4177
70692ad2
JM
4178 if (info->attrs[NL80211_ATTR_IE])
4179 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4180 else
4181 ie_len = 0;
4182
4c476991
JB
4183 if (ie_len > wiphy->max_scan_ie_len)
4184 return -EINVAL;
18a83659 4185
2a519311 4186 request = kzalloc(sizeof(*request)
a2cd43c5
LC
4187 + sizeof(*request->ssids) * n_ssids
4188 + sizeof(*request->channels) * n_channels
70692ad2 4189 + ie_len, GFP_KERNEL);
4c476991
JB
4190 if (!request)
4191 return -ENOMEM;
2a519311 4192
2a519311 4193 if (n_ssids)
5ba63533 4194 request->ssids = (void *)&request->channels[n_channels];
2a519311 4195 request->n_ssids = n_ssids;
70692ad2
JM
4196 if (ie_len) {
4197 if (request->ssids)
4198 request->ie = (void *)(request->ssids + n_ssids);
4199 else
4200 request->ie = (void *)(request->channels + n_channels);
4201 }
2a519311 4202
584991dc 4203 i = 0;
2a519311
JB
4204 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4205 /* user specified, bail out if channel not found */
2a519311 4206 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
584991dc
JB
4207 struct ieee80211_channel *chan;
4208
4209 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4210
4211 if (!chan) {
2a519311
JB
4212 err = -EINVAL;
4213 goto out_free;
4214 }
584991dc
JB
4215
4216 /* ignore disabled channels */
4217 if (chan->flags & IEEE80211_CHAN_DISABLED)
4218 continue;
4219
4220 request->channels[i] = chan;
2a519311
JB
4221 i++;
4222 }
4223 } else {
34850ab2
JB
4224 enum ieee80211_band band;
4225
2a519311 4226 /* all channels */
2a519311
JB
4227 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4228 int j;
4229 if (!wiphy->bands[band])
4230 continue;
4231 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
584991dc
JB
4232 struct ieee80211_channel *chan;
4233
4234 chan = &wiphy->bands[band]->channels[j];
4235
4236 if (chan->flags & IEEE80211_CHAN_DISABLED)
4237 continue;
4238
4239 request->channels[i] = chan;
2a519311
JB
4240 i++;
4241 }
4242 }
4243 }
4244
584991dc
JB
4245 if (!i) {
4246 err = -EINVAL;
4247 goto out_free;
4248 }
4249
4250 request->n_channels = i;
4251
2a519311
JB
4252 i = 0;
4253 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4254 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
57a27e1d 4255 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
2a519311
JB
4256 err = -EINVAL;
4257 goto out_free;
4258 }
57a27e1d 4259 request->ssids[i].ssid_len = nla_len(attr);
2a519311 4260 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
2a519311
JB
4261 i++;
4262 }
4263 }
4264
70692ad2
JM
4265 if (info->attrs[NL80211_ATTR_IE]) {
4266 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
de95a54b
JB
4267 memcpy((void *)request->ie,
4268 nla_data(info->attrs[NL80211_ATTR_IE]),
70692ad2
JM
4269 request->ie_len);
4270 }
4271
34850ab2 4272 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
a401d2bb
JB
4273 if (wiphy->bands[i])
4274 request->rates[i] =
4275 (1 << wiphy->bands[i]->n_bitrates) - 1;
34850ab2
JB
4276
4277 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4278 nla_for_each_nested(attr,
4279 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4280 tmp) {
4281 enum ieee80211_band band = nla_type(attr);
4282
84404623 4283 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
34850ab2
JB
4284 err = -EINVAL;
4285 goto out_free;
4286 }
4287 err = ieee80211_get_ratemask(wiphy->bands[band],
4288 nla_data(attr),
4289 nla_len(attr),
4290 &request->rates[band]);
4291 if (err)
4292 goto out_free;
4293 }
4294 }
4295
e9f935e3
RM
4296 request->no_cck =
4297 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4298
fd014284 4299 request->wdev = wdev;
79c97e97 4300 request->wiphy = &rdev->wiphy;
2a519311 4301
79c97e97 4302 rdev->scan_req = request;
fd014284 4303 err = rdev->ops->scan(&rdev->wiphy, request);
2a519311 4304
463d0183 4305 if (!err) {
fd014284
JB
4306 nl80211_send_scan_start(rdev, wdev);
4307 if (wdev->netdev)
4308 dev_hold(wdev->netdev);
4c476991 4309 } else {
2a519311 4310 out_free:
79c97e97 4311 rdev->scan_req = NULL;
2a519311
JB
4312 kfree(request);
4313 }
3b85875a 4314
2a519311
JB
4315 return err;
4316}
4317
807f8a8c
LC
4318static int nl80211_start_sched_scan(struct sk_buff *skb,
4319 struct genl_info *info)
4320{
4321 struct cfg80211_sched_scan_request *request;
4322 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4323 struct net_device *dev = info->user_ptr[1];
807f8a8c
LC
4324 struct nlattr *attr;
4325 struct wiphy *wiphy;
a1f1c21c 4326 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
bbe6ad6d 4327 u32 interval;
807f8a8c
LC
4328 enum ieee80211_band band;
4329 size_t ie_len;
a1f1c21c 4330 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
807f8a8c
LC
4331
4332 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4333 !rdev->ops->sched_scan_start)
4334 return -EOPNOTSUPP;
4335
4336 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4337 return -EINVAL;
4338
bbe6ad6d
LC
4339 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4340 return -EINVAL;
4341
4342 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4343 if (interval == 0)
4344 return -EINVAL;
4345
807f8a8c
LC
4346 wiphy = &rdev->wiphy;
4347
4348 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4349 n_channels = validate_scan_freqs(
4350 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4351 if (!n_channels)
4352 return -EINVAL;
4353 } else {
4354 n_channels = 0;
4355
4356 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4357 if (wiphy->bands[band])
4358 n_channels += wiphy->bands[band]->n_channels;
4359 }
4360
4361 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4362 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4363 tmp)
4364 n_ssids++;
4365
93b6aa69 4366 if (n_ssids > wiphy->max_sched_scan_ssids)
807f8a8c
LC
4367 return -EINVAL;
4368
a1f1c21c
LC
4369 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4370 nla_for_each_nested(attr,
4371 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4372 tmp)
4373 n_match_sets++;
4374
4375 if (n_match_sets > wiphy->max_match_sets)
4376 return -EINVAL;
4377
807f8a8c
LC
4378 if (info->attrs[NL80211_ATTR_IE])
4379 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4380 else
4381 ie_len = 0;
4382
5a865bad 4383 if (ie_len > wiphy->max_sched_scan_ie_len)
807f8a8c
LC
4384 return -EINVAL;
4385
c10841ca
LC
4386 mutex_lock(&rdev->sched_scan_mtx);
4387
4388 if (rdev->sched_scan_req) {
4389 err = -EINPROGRESS;
4390 goto out;
4391 }
4392
807f8a8c 4393 request = kzalloc(sizeof(*request)
a2cd43c5 4394 + sizeof(*request->ssids) * n_ssids
a1f1c21c 4395 + sizeof(*request->match_sets) * n_match_sets
a2cd43c5 4396 + sizeof(*request->channels) * n_channels
807f8a8c 4397 + ie_len, GFP_KERNEL);
c10841ca
LC
4398 if (!request) {
4399 err = -ENOMEM;
4400 goto out;
4401 }
807f8a8c
LC
4402
4403 if (n_ssids)
4404 request->ssids = (void *)&request->channels[n_channels];
4405 request->n_ssids = n_ssids;
4406 if (ie_len) {
4407 if (request->ssids)
4408 request->ie = (void *)(request->ssids + n_ssids);
4409 else
4410 request->ie = (void *)(request->channels + n_channels);
4411 }
4412
a1f1c21c
LC
4413 if (n_match_sets) {
4414 if (request->ie)
4415 request->match_sets = (void *)(request->ie + ie_len);
4416 else if (request->ssids)
4417 request->match_sets =
4418 (void *)(request->ssids + n_ssids);
4419 else
4420 request->match_sets =
4421 (void *)(request->channels + n_channels);
4422 }
4423 request->n_match_sets = n_match_sets;
4424
807f8a8c
LC
4425 i = 0;
4426 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4427 /* user specified, bail out if channel not found */
4428 nla_for_each_nested(attr,
4429 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4430 tmp) {
4431 struct ieee80211_channel *chan;
4432
4433 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4434
4435 if (!chan) {
4436 err = -EINVAL;
4437 goto out_free;
4438 }
4439
4440 /* ignore disabled channels */
4441 if (chan->flags & IEEE80211_CHAN_DISABLED)
4442 continue;
4443
4444 request->channels[i] = chan;
4445 i++;
4446 }
4447 } else {
4448 /* all channels */
4449 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4450 int j;
4451 if (!wiphy->bands[band])
4452 continue;
4453 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4454 struct ieee80211_channel *chan;
4455
4456 chan = &wiphy->bands[band]->channels[j];
4457
4458 if (chan->flags & IEEE80211_CHAN_DISABLED)
4459 continue;
4460
4461 request->channels[i] = chan;
4462 i++;
4463 }
4464 }
4465 }
4466
4467 if (!i) {
4468 err = -EINVAL;
4469 goto out_free;
4470 }
4471
4472 request->n_channels = i;
4473
4474 i = 0;
4475 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4476 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4477 tmp) {
57a27e1d 4478 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
807f8a8c
LC
4479 err = -EINVAL;
4480 goto out_free;
4481 }
57a27e1d 4482 request->ssids[i].ssid_len = nla_len(attr);
807f8a8c
LC
4483 memcpy(request->ssids[i].ssid, nla_data(attr),
4484 nla_len(attr));
807f8a8c
LC
4485 i++;
4486 }
4487 }
4488
a1f1c21c
LC
4489 i = 0;
4490 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4491 nla_for_each_nested(attr,
4492 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4493 tmp) {
88e920b4 4494 struct nlattr *ssid, *rssi;
a1f1c21c
LC
4495
4496 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4497 nla_data(attr), nla_len(attr),
4498 nl80211_match_policy);
4a4ab0d7 4499 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
a1f1c21c
LC
4500 if (ssid) {
4501 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4502 err = -EINVAL;
4503 goto out_free;
4504 }
4505 memcpy(request->match_sets[i].ssid.ssid,
4506 nla_data(ssid), nla_len(ssid));
4507 request->match_sets[i].ssid.ssid_len =
4508 nla_len(ssid);
4509 }
88e920b4
TP
4510 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4511 if (rssi)
4512 request->rssi_thold = nla_get_u32(rssi);
4513 else
4514 request->rssi_thold =
4515 NL80211_SCAN_RSSI_THOLD_OFF;
a1f1c21c
LC
4516 i++;
4517 }
4518 }
4519
807f8a8c
LC
4520 if (info->attrs[NL80211_ATTR_IE]) {
4521 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4522 memcpy((void *)request->ie,
4523 nla_data(info->attrs[NL80211_ATTR_IE]),
4524 request->ie_len);
4525 }
4526
4527 request->dev = dev;
4528 request->wiphy = &rdev->wiphy;
bbe6ad6d 4529 request->interval = interval;
807f8a8c
LC
4530
4531 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4532 if (!err) {
4533 rdev->sched_scan_req = request;
4534 nl80211_send_sched_scan(rdev, dev,
4535 NL80211_CMD_START_SCHED_SCAN);
4536 goto out;
4537 }
4538
4539out_free:
4540 kfree(request);
4541out:
c10841ca 4542 mutex_unlock(&rdev->sched_scan_mtx);
807f8a8c
LC
4543 return err;
4544}
4545
4546static int nl80211_stop_sched_scan(struct sk_buff *skb,
4547 struct genl_info *info)
4548{
4549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
c10841ca 4550 int err;
807f8a8c
LC
4551
4552 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4553 !rdev->ops->sched_scan_stop)
4554 return -EOPNOTSUPP;
4555
c10841ca
LC
4556 mutex_lock(&rdev->sched_scan_mtx);
4557 err = __cfg80211_stop_sched_scan(rdev, false);
4558 mutex_unlock(&rdev->sched_scan_mtx);
4559
4560 return err;
807f8a8c
LC
4561}
4562
9720bb3a
JB
4563static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4564 u32 seq, int flags,
2a519311 4565 struct cfg80211_registered_device *rdev,
48ab905d
JB
4566 struct wireless_dev *wdev,
4567 struct cfg80211_internal_bss *intbss)
2a519311 4568{
48ab905d 4569 struct cfg80211_bss *res = &intbss->pub;
2a519311
JB
4570 void *hdr;
4571 struct nlattr *bss;
48ab905d
JB
4572
4573 ASSERT_WDEV_LOCK(wdev);
2a519311 4574
9720bb3a 4575 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
2a519311
JB
4576 NL80211_CMD_NEW_SCAN_RESULTS);
4577 if (!hdr)
4578 return -1;
4579
9720bb3a
JB
4580 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4581
9360ffd1
DM
4582 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4583 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4584 goto nla_put_failure;
2a519311
JB
4585
4586 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4587 if (!bss)
4588 goto nla_put_failure;
9360ffd1
DM
4589 if ((!is_zero_ether_addr(res->bssid) &&
4590 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4591 (res->information_elements && res->len_information_elements &&
4592 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4593 res->len_information_elements,
4594 res->information_elements)) ||
4595 (res->beacon_ies && res->len_beacon_ies &&
4596 res->beacon_ies != res->information_elements &&
4597 nla_put(msg, NL80211_BSS_BEACON_IES,
4598 res->len_beacon_ies, res->beacon_ies)))
4599 goto nla_put_failure;
4600 if (res->tsf &&
4601 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4602 goto nla_put_failure;
4603 if (res->beacon_interval &&
4604 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4605 goto nla_put_failure;
4606 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4607 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4608 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4609 jiffies_to_msecs(jiffies - intbss->ts)))
4610 goto nla_put_failure;
2a519311 4611
77965c97 4612 switch (rdev->wiphy.signal_type) {
2a519311 4613 case CFG80211_SIGNAL_TYPE_MBM:
9360ffd1
DM
4614 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4615 goto nla_put_failure;
2a519311
JB
4616 break;
4617 case CFG80211_SIGNAL_TYPE_UNSPEC:
9360ffd1
DM
4618 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4619 goto nla_put_failure;
2a519311
JB
4620 break;
4621 default:
4622 break;
4623 }
4624
48ab905d 4625 switch (wdev->iftype) {
074ac8df 4626 case NL80211_IFTYPE_P2P_CLIENT:
48ab905d 4627 case NL80211_IFTYPE_STATION:
9360ffd1
DM
4628 if (intbss == wdev->current_bss &&
4629 nla_put_u32(msg, NL80211_BSS_STATUS,
4630 NL80211_BSS_STATUS_ASSOCIATED))
4631 goto nla_put_failure;
48ab905d
JB
4632 break;
4633 case NL80211_IFTYPE_ADHOC:
9360ffd1
DM
4634 if (intbss == wdev->current_bss &&
4635 nla_put_u32(msg, NL80211_BSS_STATUS,
4636 NL80211_BSS_STATUS_IBSS_JOINED))
4637 goto nla_put_failure;
48ab905d
JB
4638 break;
4639 default:
4640 break;
4641 }
4642
2a519311
JB
4643 nla_nest_end(msg, bss);
4644
4645 return genlmsg_end(msg, hdr);
4646
4647 nla_put_failure:
4648 genlmsg_cancel(msg, hdr);
4649 return -EMSGSIZE;
4650}
4651
4652static int nl80211_dump_scan(struct sk_buff *skb,
4653 struct netlink_callback *cb)
4654{
48ab905d
JB
4655 struct cfg80211_registered_device *rdev;
4656 struct net_device *dev;
2a519311 4657 struct cfg80211_internal_bss *scan;
48ab905d 4658 struct wireless_dev *wdev;
2a519311
JB
4659 int start = cb->args[1], idx = 0;
4660 int err;
4661
67748893
JB
4662 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4663 if (err)
4664 return err;
2a519311 4665
48ab905d 4666 wdev = dev->ieee80211_ptr;
2a519311 4667
48ab905d
JB
4668 wdev_lock(wdev);
4669 spin_lock_bh(&rdev->bss_lock);
4670 cfg80211_bss_expire(rdev);
4671
9720bb3a
JB
4672 cb->seq = rdev->bss_generation;
4673
48ab905d 4674 list_for_each_entry(scan, &rdev->bss_list, list) {
2a519311
JB
4675 if (++idx <= start)
4676 continue;
9720bb3a 4677 if (nl80211_send_bss(skb, cb,
2a519311 4678 cb->nlh->nlmsg_seq, NLM_F_MULTI,
48ab905d 4679 rdev, wdev, scan) < 0) {
2a519311 4680 idx--;
67748893 4681 break;
2a519311
JB
4682 }
4683 }
4684
48ab905d
JB
4685 spin_unlock_bh(&rdev->bss_lock);
4686 wdev_unlock(wdev);
2a519311
JB
4687
4688 cb->args[1] = idx;
67748893 4689 nl80211_finish_netdev_dump(rdev);
2a519311 4690
67748893 4691 return skb->len;
2a519311
JB
4692}
4693
61fa713c
HS
4694static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4695 int flags, struct net_device *dev,
4696 struct survey_info *survey)
4697{
4698 void *hdr;
4699 struct nlattr *infoattr;
4700
61fa713c
HS
4701 hdr = nl80211hdr_put(msg, pid, seq, flags,
4702 NL80211_CMD_NEW_SURVEY_RESULTS);
4703 if (!hdr)
4704 return -ENOMEM;
4705
9360ffd1
DM
4706 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4707 goto nla_put_failure;
61fa713c
HS
4708
4709 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4710 if (!infoattr)
4711 goto nla_put_failure;
4712
9360ffd1
DM
4713 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4714 survey->channel->center_freq))
4715 goto nla_put_failure;
4716
4717 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4718 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4719 goto nla_put_failure;
4720 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4721 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4722 goto nla_put_failure;
4723 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4724 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4725 survey->channel_time))
4726 goto nla_put_failure;
4727 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4728 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4729 survey->channel_time_busy))
4730 goto nla_put_failure;
4731 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4732 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4733 survey->channel_time_ext_busy))
4734 goto nla_put_failure;
4735 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4736 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4737 survey->channel_time_rx))
4738 goto nla_put_failure;
4739 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4740 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4741 survey->channel_time_tx))
4742 goto nla_put_failure;
61fa713c
HS
4743
4744 nla_nest_end(msg, infoattr);
4745
4746 return genlmsg_end(msg, hdr);
4747
4748 nla_put_failure:
4749 genlmsg_cancel(msg, hdr);
4750 return -EMSGSIZE;
4751}
4752
4753static int nl80211_dump_survey(struct sk_buff *skb,
4754 struct netlink_callback *cb)
4755{
4756 struct survey_info survey;
4757 struct cfg80211_registered_device *dev;
4758 struct net_device *netdev;
61fa713c
HS
4759 int survey_idx = cb->args[1];
4760 int res;
4761
67748893
JB
4762 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4763 if (res)
4764 return res;
61fa713c
HS
4765
4766 if (!dev->ops->dump_survey) {
4767 res = -EOPNOTSUPP;
4768 goto out_err;
4769 }
4770
4771 while (1) {
180cdc79
LR
4772 struct ieee80211_channel *chan;
4773
61fa713c
HS
4774 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4775 &survey);
4776 if (res == -ENOENT)
4777 break;
4778 if (res)
4779 goto out_err;
4780
180cdc79
LR
4781 /* Survey without a channel doesn't make sense */
4782 if (!survey.channel) {
4783 res = -EINVAL;
4784 goto out;
4785 }
4786
4787 chan = ieee80211_get_channel(&dev->wiphy,
4788 survey.channel->center_freq);
4789 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4790 survey_idx++;
4791 continue;
4792 }
4793
61fa713c
HS
4794 if (nl80211_send_survey(skb,
4795 NETLINK_CB(cb->skb).pid,
4796 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4797 netdev,
4798 &survey) < 0)
4799 goto out;
4800 survey_idx++;
4801 }
4802
4803 out:
4804 cb->args[1] = survey_idx;
4805 res = skb->len;
4806 out_err:
67748893 4807 nl80211_finish_netdev_dump(dev);
61fa713c
HS
4808 return res;
4809}
4810
255e737e
JM
4811static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4812{
b23aa676
SO
4813 return auth_type <= NL80211_AUTHTYPE_MAX;
4814}
4815
4816static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4817{
4818 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4819 NL80211_WPA_VERSION_2));
4820}
4821
636a5d36
JM
4822static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4823{
4c476991
JB
4824 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4825 struct net_device *dev = info->user_ptr[1];
19957bb3
JB
4826 struct ieee80211_channel *chan;
4827 const u8 *bssid, *ssid, *ie = NULL;
4828 int err, ssid_len, ie_len = 0;
4829 enum nl80211_auth_type auth_type;
fffd0934 4830 struct key_parse key;
d5cdfacb 4831 bool local_state_change;
636a5d36 4832
f4a11bb0
JB
4833 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4834 return -EINVAL;
4835
4836 if (!info->attrs[NL80211_ATTR_MAC])
4837 return -EINVAL;
4838
1778092e
JM
4839 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4840 return -EINVAL;
4841
19957bb3
JB
4842 if (!info->attrs[NL80211_ATTR_SSID])
4843 return -EINVAL;
4844
4845 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4846 return -EINVAL;
4847
fffd0934
JB
4848 err = nl80211_parse_key(info, &key);
4849 if (err)
4850 return err;
4851
4852 if (key.idx >= 0) {
e31b8213
JB
4853 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4854 return -EINVAL;
fffd0934
JB
4855 if (!key.p.key || !key.p.key_len)
4856 return -EINVAL;
4857 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4858 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4859 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4860 key.p.key_len != WLAN_KEY_LEN_WEP104))
4861 return -EINVAL;
4862 if (key.idx > 4)
4863 return -EINVAL;
4864 } else {
4865 key.p.key_len = 0;
4866 key.p.key = NULL;
4867 }
4868
afea0b7a
JB
4869 if (key.idx >= 0) {
4870 int i;
4871 bool ok = false;
4872 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4873 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4874 ok = true;
4875 break;
4876 }
4877 }
4c476991
JB
4878 if (!ok)
4879 return -EINVAL;
afea0b7a
JB
4880 }
4881
4c476991
JB
4882 if (!rdev->ops->auth)
4883 return -EOPNOTSUPP;
636a5d36 4884
074ac8df 4885 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
4886 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4887 return -EOPNOTSUPP;
eec60b03 4888
19957bb3 4889 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
79c97e97 4890 chan = ieee80211_get_channel(&rdev->wiphy,
19957bb3 4891 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4c476991
JB
4892 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4893 return -EINVAL;
636a5d36 4894
19957bb3
JB
4895 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4896 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
636a5d36
JM
4897
4898 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
4899 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4900 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
4901 }
4902
19957bb3 4903 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4c476991
JB
4904 if (!nl80211_valid_auth_type(auth_type))
4905 return -EINVAL;
636a5d36 4906
d5cdfacb
JM
4907 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4908
95de817b
JB
4909 /*
4910 * Since we no longer track auth state, ignore
4911 * requests to only change local state.
4912 */
4913 if (local_state_change)
4914 return 0;
4915
4c476991
JB
4916 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4917 ssid, ssid_len, ie, ie_len,
95de817b 4918 key.p.key, key.p.key_len, key.idx);
636a5d36
JM
4919}
4920
c0692b8f
JB
4921static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4922 struct genl_info *info,
3dc27d25
JB
4923 struct cfg80211_crypto_settings *settings,
4924 int cipher_limit)
b23aa676 4925{
c0b2bbd8
JB
4926 memset(settings, 0, sizeof(*settings));
4927
b23aa676
SO
4928 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4929
c0692b8f
JB
4930 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4931 u16 proto;
4932 proto = nla_get_u16(
4933 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4934 settings->control_port_ethertype = cpu_to_be16(proto);
4935 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4936 proto != ETH_P_PAE)
4937 return -EINVAL;
4938 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4939 settings->control_port_no_encrypt = true;
4940 } else
4941 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4942
b23aa676
SO
4943 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4944 void *data;
4945 int len, i;
4946
4947 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4948 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4949 settings->n_ciphers_pairwise = len / sizeof(u32);
4950
4951 if (len % sizeof(u32))
4952 return -EINVAL;
4953
3dc27d25 4954 if (settings->n_ciphers_pairwise > cipher_limit)
b23aa676
SO
4955 return -EINVAL;
4956
4957 memcpy(settings->ciphers_pairwise, data, len);
4958
4959 for (i = 0; i < settings->n_ciphers_pairwise; i++)
38ba3c57
JM
4960 if (!cfg80211_supported_cipher_suite(
4961 &rdev->wiphy,
b23aa676
SO
4962 settings->ciphers_pairwise[i]))
4963 return -EINVAL;
4964 }
4965
4966 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4967 settings->cipher_group =
4968 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
38ba3c57
JM
4969 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4970 settings->cipher_group))
b23aa676
SO
4971 return -EINVAL;
4972 }
4973
4974 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4975 settings->wpa_versions =
4976 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4977 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4978 return -EINVAL;
4979 }
4980
4981 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4982 void *data;
6d30240e 4983 int len;
b23aa676
SO
4984
4985 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4986 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4987 settings->n_akm_suites = len / sizeof(u32);
4988
4989 if (len % sizeof(u32))
4990 return -EINVAL;
4991
1b9ca027
JM
4992 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4993 return -EINVAL;
4994
b23aa676 4995 memcpy(settings->akm_suites, data, len);
b23aa676
SO
4996 }
4997
4998 return 0;
4999}
5000
636a5d36
JM
5001static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5002{
4c476991
JB
5003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5004 struct net_device *dev = info->user_ptr[1];
19957bb3 5005 struct cfg80211_crypto_settings crypto;
f444de05 5006 struct ieee80211_channel *chan;
3e5d7649 5007 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
19957bb3
JB
5008 int err, ssid_len, ie_len = 0;
5009 bool use_mfp = false;
7e7c8926
BG
5010 u32 flags = 0;
5011 struct ieee80211_ht_cap *ht_capa = NULL;
5012 struct ieee80211_ht_cap *ht_capa_mask = NULL;
636a5d36 5013
f4a11bb0
JB
5014 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5015 return -EINVAL;
5016
5017 if (!info->attrs[NL80211_ATTR_MAC] ||
19957bb3
JB
5018 !info->attrs[NL80211_ATTR_SSID] ||
5019 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
f4a11bb0
JB
5020 return -EINVAL;
5021
4c476991
JB
5022 if (!rdev->ops->assoc)
5023 return -EOPNOTSUPP;
636a5d36 5024
074ac8df 5025 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5026 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5027 return -EOPNOTSUPP;
eec60b03 5028
19957bb3 5029 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 5030
19957bb3
JB
5031 chan = ieee80211_get_channel(&rdev->wiphy,
5032 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4c476991
JB
5033 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5034 return -EINVAL;
636a5d36 5035
19957bb3
JB
5036 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5037 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
636a5d36
JM
5038
5039 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
5040 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5041 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
5042 }
5043
dc6382ce 5044 if (info->attrs[NL80211_ATTR_USE_MFP]) {
4f5dadce 5045 enum nl80211_mfp mfp =
dc6382ce 5046 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4f5dadce 5047 if (mfp == NL80211_MFP_REQUIRED)
19957bb3 5048 use_mfp = true;
4c476991
JB
5049 else if (mfp != NL80211_MFP_NO)
5050 return -EINVAL;
dc6382ce
JM
5051 }
5052
3e5d7649
JB
5053 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5054 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5055
7e7c8926
BG
5056 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5057 flags |= ASSOC_REQ_DISABLE_HT;
5058
5059 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5060 ht_capa_mask =
5061 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5062
5063 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5064 if (!ht_capa_mask)
5065 return -EINVAL;
5066 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5067 }
5068
c0692b8f 5069 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
b23aa676 5070 if (!err)
3e5d7649
JB
5071 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5072 ssid, ssid_len, ie, ie_len, use_mfp,
7e7c8926
BG
5073 &crypto, flags, ht_capa,
5074 ht_capa_mask);
636a5d36 5075
636a5d36
JM
5076 return err;
5077}
5078
5079static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5080{
4c476991
JB
5081 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5082 struct net_device *dev = info->user_ptr[1];
19957bb3 5083 const u8 *ie = NULL, *bssid;
4c476991 5084 int ie_len = 0;
19957bb3 5085 u16 reason_code;
d5cdfacb 5086 bool local_state_change;
636a5d36 5087
f4a11bb0
JB
5088 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5089 return -EINVAL;
5090
5091 if (!info->attrs[NL80211_ATTR_MAC])
5092 return -EINVAL;
5093
5094 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5095 return -EINVAL;
5096
4c476991
JB
5097 if (!rdev->ops->deauth)
5098 return -EOPNOTSUPP;
636a5d36 5099
074ac8df 5100 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5101 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5102 return -EOPNOTSUPP;
eec60b03 5103
19957bb3 5104 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 5105
19957bb3
JB
5106 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5107 if (reason_code == 0) {
f4a11bb0 5108 /* Reason Code 0 is reserved */
4c476991 5109 return -EINVAL;
255e737e 5110 }
636a5d36
JM
5111
5112 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
5113 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5114 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
5115 }
5116
d5cdfacb
JM
5117 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5118
4c476991
JB
5119 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5120 local_state_change);
636a5d36
JM
5121}
5122
5123static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5124{
4c476991
JB
5125 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5126 struct net_device *dev = info->user_ptr[1];
19957bb3 5127 const u8 *ie = NULL, *bssid;
4c476991 5128 int ie_len = 0;
19957bb3 5129 u16 reason_code;
d5cdfacb 5130 bool local_state_change;
636a5d36 5131
f4a11bb0
JB
5132 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5133 return -EINVAL;
5134
5135 if (!info->attrs[NL80211_ATTR_MAC])
5136 return -EINVAL;
5137
5138 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5139 return -EINVAL;
5140
4c476991
JB
5141 if (!rdev->ops->disassoc)
5142 return -EOPNOTSUPP;
636a5d36 5143
074ac8df 5144 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5145 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5146 return -EOPNOTSUPP;
eec60b03 5147
19957bb3 5148 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
636a5d36 5149
19957bb3
JB
5150 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5151 if (reason_code == 0) {
f4a11bb0 5152 /* Reason Code 0 is reserved */
4c476991 5153 return -EINVAL;
255e737e 5154 }
636a5d36
JM
5155
5156 if (info->attrs[NL80211_ATTR_IE]) {
19957bb3
JB
5157 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5158 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
636a5d36
JM
5159 }
5160
d5cdfacb
JM
5161 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5162
4c476991
JB
5163 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5164 local_state_change);
636a5d36
JM
5165}
5166
dd5b4cc7
FF
5167static bool
5168nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5169 int mcast_rate[IEEE80211_NUM_BANDS],
5170 int rateval)
5171{
5172 struct wiphy *wiphy = &rdev->wiphy;
5173 bool found = false;
5174 int band, i;
5175
5176 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5177 struct ieee80211_supported_band *sband;
5178
5179 sband = wiphy->bands[band];
5180 if (!sband)
5181 continue;
5182
5183 for (i = 0; i < sband->n_bitrates; i++) {
5184 if (sband->bitrates[i].bitrate == rateval) {
5185 mcast_rate[band] = i + 1;
5186 found = true;
5187 break;
5188 }
5189 }
5190 }
5191
5192 return found;
5193}
5194
04a773ad
JB
5195static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5196{
4c476991
JB
5197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5198 struct net_device *dev = info->user_ptr[1];
04a773ad
JB
5199 struct cfg80211_ibss_params ibss;
5200 struct wiphy *wiphy;
fffd0934 5201 struct cfg80211_cached_keys *connkeys = NULL;
04a773ad
JB
5202 int err;
5203
8e30bc55
JB
5204 memset(&ibss, 0, sizeof(ibss));
5205
04a773ad
JB
5206 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5207 return -EINVAL;
5208
5209 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5210 !info->attrs[NL80211_ATTR_SSID] ||
5211 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5212 return -EINVAL;
5213
8e30bc55
JB
5214 ibss.beacon_interval = 100;
5215
5216 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5217 ibss.beacon_interval =
5218 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5219 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5220 return -EINVAL;
5221 }
5222
4c476991
JB
5223 if (!rdev->ops->join_ibss)
5224 return -EOPNOTSUPP;
04a773ad 5225
4c476991
JB
5226 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5227 return -EOPNOTSUPP;
04a773ad 5228
79c97e97 5229 wiphy = &rdev->wiphy;
04a773ad 5230
39193498 5231 if (info->attrs[NL80211_ATTR_MAC]) {
04a773ad 5232 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
39193498
JB
5233
5234 if (!is_valid_ether_addr(ibss.bssid))
5235 return -EINVAL;
5236 }
04a773ad
JB
5237 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5238 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5239
5240 if (info->attrs[NL80211_ATTR_IE]) {
5241 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5242 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5243 }
5244
54858ee5
AS
5245 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5246 enum nl80211_channel_type channel_type;
5247
cd6c6598 5248 if (!nl80211_valid_channel_type(info, &channel_type))
54858ee5
AS
5249 return -EINVAL;
5250
5251 if (channel_type != NL80211_CHAN_NO_HT &&
5252 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5253 return -EINVAL;
5254
5255 ibss.channel_type = channel_type;
5256 } else {
5257 ibss.channel_type = NL80211_CHAN_NO_HT;
5258 }
5259
5260 ibss.channel = rdev_freq_to_chan(rdev,
5261 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5262 ibss.channel_type);
04a773ad
JB
5263 if (!ibss.channel ||
5264 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4c476991
JB
5265 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5266 return -EINVAL;
04a773ad 5267
54858ee5
AS
5268 /* Both channels should be able to initiate communication */
5269 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5270 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5271 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5272 ibss.channel_type))
5273 return -EINVAL;
5274
04a773ad 5275 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
fffd0934
JB
5276 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5277
fbd2c8dc
TP
5278 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5279 u8 *rates =
5280 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5281 int n_rates =
5282 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5283 struct ieee80211_supported_band *sband =
5284 wiphy->bands[ibss.channel->band];
fbd2c8dc 5285
34850ab2
JB
5286 err = ieee80211_get_ratemask(sband, rates, n_rates,
5287 &ibss.basic_rates);
5288 if (err)
5289 return err;
fbd2c8dc 5290 }
dd5b4cc7
FF
5291
5292 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5293 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5294 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5295 return -EINVAL;
fbd2c8dc 5296
4c476991
JB
5297 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5298 connkeys = nl80211_parse_connkeys(rdev,
5299 info->attrs[NL80211_ATTR_KEYS]);
5300 if (IS_ERR(connkeys))
5301 return PTR_ERR(connkeys);
5302 }
04a773ad 5303
267335d6
AQ
5304 ibss.control_port =
5305 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5306
4c476991 5307 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
fffd0934
JB
5308 if (err)
5309 kfree(connkeys);
04a773ad
JB
5310 return err;
5311}
5312
5313static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5314{
4c476991
JB
5315 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5316 struct net_device *dev = info->user_ptr[1];
04a773ad 5317
4c476991
JB
5318 if (!rdev->ops->leave_ibss)
5319 return -EOPNOTSUPP;
04a773ad 5320
4c476991
JB
5321 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5322 return -EOPNOTSUPP;
04a773ad 5323
4c476991 5324 return cfg80211_leave_ibss(rdev, dev, false);
04a773ad
JB
5325}
5326
aff89a9b
JB
5327#ifdef CONFIG_NL80211_TESTMODE
5328static struct genl_multicast_group nl80211_testmode_mcgrp = {
5329 .name = "testmode",
5330};
5331
5332static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5333{
4c476991 5334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
aff89a9b
JB
5335 int err;
5336
5337 if (!info->attrs[NL80211_ATTR_TESTDATA])
5338 return -EINVAL;
5339
aff89a9b
JB
5340 err = -EOPNOTSUPP;
5341 if (rdev->ops->testmode_cmd) {
5342 rdev->testmode_info = info;
5343 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5344 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5345 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5346 rdev->testmode_info = NULL;
5347 }
5348
aff89a9b
JB
5349 return err;
5350}
5351
71063f0e
WYG
5352static int nl80211_testmode_dump(struct sk_buff *skb,
5353 struct netlink_callback *cb)
5354{
00918d33 5355 struct cfg80211_registered_device *rdev;
71063f0e
WYG
5356 int err;
5357 long phy_idx;
5358 void *data = NULL;
5359 int data_len = 0;
5360
5361 if (cb->args[0]) {
5362 /*
5363 * 0 is a valid index, but not valid for args[0],
5364 * so we need to offset by 1.
5365 */
5366 phy_idx = cb->args[0] - 1;
5367 } else {
5368 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5369 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5370 nl80211_policy);
5371 if (err)
5372 return err;
00918d33 5373
2bd7e35d
JB
5374 mutex_lock(&cfg80211_mutex);
5375 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5376 nl80211_fam.attrbuf);
5377 if (IS_ERR(rdev)) {
5378 mutex_unlock(&cfg80211_mutex);
5379 return PTR_ERR(rdev);
00918d33 5380 }
2bd7e35d
JB
5381 phy_idx = rdev->wiphy_idx;
5382 rdev = NULL;
5383 mutex_unlock(&cfg80211_mutex);
5384
71063f0e
WYG
5385 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5386 cb->args[1] =
5387 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5388 }
5389
5390 if (cb->args[1]) {
5391 data = nla_data((void *)cb->args[1]);
5392 data_len = nla_len((void *)cb->args[1]);
5393 }
5394
5395 mutex_lock(&cfg80211_mutex);
00918d33
JB
5396 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5397 if (!rdev) {
71063f0e
WYG
5398 mutex_unlock(&cfg80211_mutex);
5399 return -ENOENT;
5400 }
00918d33 5401 cfg80211_lock_rdev(rdev);
71063f0e
WYG
5402 mutex_unlock(&cfg80211_mutex);
5403
00918d33 5404 if (!rdev->ops->testmode_dump) {
71063f0e
WYG
5405 err = -EOPNOTSUPP;
5406 goto out_err;
5407 }
5408
5409 while (1) {
5410 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5411 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5412 NL80211_CMD_TESTMODE);
5413 struct nlattr *tmdata;
5414
9360ffd1 5415 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
71063f0e
WYG
5416 genlmsg_cancel(skb, hdr);
5417 break;
5418 }
5419
5420 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5421 if (!tmdata) {
5422 genlmsg_cancel(skb, hdr);
5423 break;
5424 }
00918d33
JB
5425 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5426 data, data_len);
71063f0e
WYG
5427 nla_nest_end(skb, tmdata);
5428
5429 if (err == -ENOBUFS || err == -ENOENT) {
5430 genlmsg_cancel(skb, hdr);
5431 break;
5432 } else if (err) {
5433 genlmsg_cancel(skb, hdr);
5434 goto out_err;
5435 }
5436
5437 genlmsg_end(skb, hdr);
5438 }
5439
5440 err = skb->len;
5441 /* see above */
5442 cb->args[0] = phy_idx + 1;
5443 out_err:
00918d33 5444 cfg80211_unlock_rdev(rdev);
71063f0e
WYG
5445 return err;
5446}
5447
aff89a9b
JB
5448static struct sk_buff *
5449__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5450 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5451{
5452 struct sk_buff *skb;
5453 void *hdr;
5454 struct nlattr *data;
5455
5456 skb = nlmsg_new(approxlen + 100, gfp);
5457 if (!skb)
5458 return NULL;
5459
5460 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5461 if (!hdr) {
5462 kfree_skb(skb);
5463 return NULL;
5464 }
5465
9360ffd1
DM
5466 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5467 goto nla_put_failure;
aff89a9b
JB
5468 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5469
5470 ((void **)skb->cb)[0] = rdev;
5471 ((void **)skb->cb)[1] = hdr;
5472 ((void **)skb->cb)[2] = data;
5473
5474 return skb;
5475
5476 nla_put_failure:
5477 kfree_skb(skb);
5478 return NULL;
5479}
5480
5481struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5482 int approxlen)
5483{
5484 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5485
5486 if (WARN_ON(!rdev->testmode_info))
5487 return NULL;
5488
5489 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5490 rdev->testmode_info->snd_pid,
5491 rdev->testmode_info->snd_seq,
5492 GFP_KERNEL);
5493}
5494EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5495
5496int cfg80211_testmode_reply(struct sk_buff *skb)
5497{
5498 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5499 void *hdr = ((void **)skb->cb)[1];
5500 struct nlattr *data = ((void **)skb->cb)[2];
5501
5502 if (WARN_ON(!rdev->testmode_info)) {
5503 kfree_skb(skb);
5504 return -EINVAL;
5505 }
5506
5507 nla_nest_end(skb, data);
5508 genlmsg_end(skb, hdr);
5509 return genlmsg_reply(skb, rdev->testmode_info);
5510}
5511EXPORT_SYMBOL(cfg80211_testmode_reply);
5512
5513struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5514 int approxlen, gfp_t gfp)
5515{
5516 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5517
5518 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5519}
5520EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5521
5522void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5523{
5524 void *hdr = ((void **)skb->cb)[1];
5525 struct nlattr *data = ((void **)skb->cb)[2];
5526
5527 nla_nest_end(skb, data);
5528 genlmsg_end(skb, hdr);
5529 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5530}
5531EXPORT_SYMBOL(cfg80211_testmode_event);
5532#endif
5533
b23aa676
SO
5534static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5535{
4c476991
JB
5536 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5537 struct net_device *dev = info->user_ptr[1];
b23aa676
SO
5538 struct cfg80211_connect_params connect;
5539 struct wiphy *wiphy;
fffd0934 5540 struct cfg80211_cached_keys *connkeys = NULL;
b23aa676
SO
5541 int err;
5542
5543 memset(&connect, 0, sizeof(connect));
5544
5545 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5546 return -EINVAL;
5547
5548 if (!info->attrs[NL80211_ATTR_SSID] ||
5549 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5550 return -EINVAL;
5551
5552 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5553 connect.auth_type =
5554 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5555 if (!nl80211_valid_auth_type(connect.auth_type))
5556 return -EINVAL;
5557 } else
5558 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5559
5560 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5561
c0692b8f 5562 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
3dc27d25 5563 NL80211_MAX_NR_CIPHER_SUITES);
b23aa676
SO
5564 if (err)
5565 return err;
b23aa676 5566
074ac8df 5567 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5568 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5569 return -EOPNOTSUPP;
b23aa676 5570
79c97e97 5571 wiphy = &rdev->wiphy;
b23aa676 5572
4486ea98
BS
5573 connect.bg_scan_period = -1;
5574 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5575 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5576 connect.bg_scan_period =
5577 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5578 }
5579
b23aa676
SO
5580 if (info->attrs[NL80211_ATTR_MAC])
5581 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5582 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5583 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5584
5585 if (info->attrs[NL80211_ATTR_IE]) {
5586 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5587 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5588 }
5589
5590 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5591 connect.channel =
5592 ieee80211_get_channel(wiphy,
5593 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5594 if (!connect.channel ||
4c476991
JB
5595 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5596 return -EINVAL;
b23aa676
SO
5597 }
5598
fffd0934
JB
5599 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5600 connkeys = nl80211_parse_connkeys(rdev,
5601 info->attrs[NL80211_ATTR_KEYS]);
4c476991
JB
5602 if (IS_ERR(connkeys))
5603 return PTR_ERR(connkeys);
fffd0934
JB
5604 }
5605
7e7c8926
BG
5606 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5607 connect.flags |= ASSOC_REQ_DISABLE_HT;
5608
5609 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5610 memcpy(&connect.ht_capa_mask,
5611 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5612 sizeof(connect.ht_capa_mask));
5613
5614 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5615 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5616 return -EINVAL;
5617 memcpy(&connect.ht_capa,
5618 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5619 sizeof(connect.ht_capa));
5620 }
5621
fffd0934 5622 err = cfg80211_connect(rdev, dev, &connect, connkeys);
fffd0934
JB
5623 if (err)
5624 kfree(connkeys);
b23aa676
SO
5625 return err;
5626}
5627
5628static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5629{
4c476991
JB
5630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5631 struct net_device *dev = info->user_ptr[1];
b23aa676
SO
5632 u16 reason;
5633
5634 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5635 reason = WLAN_REASON_DEAUTH_LEAVING;
5636 else
5637 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5638
5639 if (reason == 0)
5640 return -EINVAL;
5641
074ac8df 5642 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5643 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5644 return -EOPNOTSUPP;
b23aa676 5645
4c476991 5646 return cfg80211_disconnect(rdev, dev, reason, true);
b23aa676
SO
5647}
5648
463d0183
JB
5649static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5650{
4c476991 5651 struct cfg80211_registered_device *rdev = info->user_ptr[0];
463d0183
JB
5652 struct net *net;
5653 int err;
5654 u32 pid;
5655
5656 if (!info->attrs[NL80211_ATTR_PID])
5657 return -EINVAL;
5658
5659 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5660
463d0183 5661 net = get_net_ns_by_pid(pid);
4c476991
JB
5662 if (IS_ERR(net))
5663 return PTR_ERR(net);
463d0183
JB
5664
5665 err = 0;
5666
5667 /* check if anything to do */
4c476991
JB
5668 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5669 err = cfg80211_switch_netns(rdev, net);
463d0183 5670
463d0183 5671 put_net(net);
463d0183
JB
5672 return err;
5673}
5674
67fbb16b
SO
5675static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5676{
4c476991 5677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
67fbb16b
SO
5678 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5679 struct cfg80211_pmksa *pmksa) = NULL;
4c476991 5680 struct net_device *dev = info->user_ptr[1];
67fbb16b
SO
5681 struct cfg80211_pmksa pmksa;
5682
5683 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5684
5685 if (!info->attrs[NL80211_ATTR_MAC])
5686 return -EINVAL;
5687
5688 if (!info->attrs[NL80211_ATTR_PMKID])
5689 return -EINVAL;
5690
67fbb16b
SO
5691 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5692 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5693
074ac8df 5694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5696 return -EOPNOTSUPP;
67fbb16b
SO
5697
5698 switch (info->genlhdr->cmd) {
5699 case NL80211_CMD_SET_PMKSA:
5700 rdev_ops = rdev->ops->set_pmksa;
5701 break;
5702 case NL80211_CMD_DEL_PMKSA:
5703 rdev_ops = rdev->ops->del_pmksa;
5704 break;
5705 default:
5706 WARN_ON(1);
5707 break;
5708 }
5709
4c476991
JB
5710 if (!rdev_ops)
5711 return -EOPNOTSUPP;
67fbb16b 5712
4c476991 5713 return rdev_ops(&rdev->wiphy, dev, &pmksa);
67fbb16b
SO
5714}
5715
5716static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5717{
4c476991
JB
5718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5719 struct net_device *dev = info->user_ptr[1];
67fbb16b 5720
074ac8df 5721 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
5722 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5723 return -EOPNOTSUPP;
67fbb16b 5724
4c476991
JB
5725 if (!rdev->ops->flush_pmksa)
5726 return -EOPNOTSUPP;
67fbb16b 5727
4c476991 5728 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
67fbb16b
SO
5729}
5730
109086ce
AN
5731static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5732{
5733 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5734 struct net_device *dev = info->user_ptr[1];
5735 u8 action_code, dialog_token;
5736 u16 status_code;
5737 u8 *peer;
5738
5739 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5740 !rdev->ops->tdls_mgmt)
5741 return -EOPNOTSUPP;
5742
5743 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5744 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5745 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5746 !info->attrs[NL80211_ATTR_IE] ||
5747 !info->attrs[NL80211_ATTR_MAC])
5748 return -EINVAL;
5749
5750 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5751 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5752 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5753 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5754
5755 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5756 dialog_token, status_code,
5757 nla_data(info->attrs[NL80211_ATTR_IE]),
5758 nla_len(info->attrs[NL80211_ATTR_IE]));
5759}
5760
5761static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5762{
5763 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5764 struct net_device *dev = info->user_ptr[1];
5765 enum nl80211_tdls_operation operation;
5766 u8 *peer;
5767
5768 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5769 !rdev->ops->tdls_oper)
5770 return -EOPNOTSUPP;
5771
5772 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5773 !info->attrs[NL80211_ATTR_MAC])
5774 return -EINVAL;
5775
5776 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5777 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5778
5779 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5780}
5781
9588bbd5
JM
5782static int nl80211_remain_on_channel(struct sk_buff *skb,
5783 struct genl_info *info)
5784{
4c476991 5785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 5786 struct wireless_dev *wdev = info->user_ptr[1];
9588bbd5
JM
5787 struct ieee80211_channel *chan;
5788 struct sk_buff *msg;
5789 void *hdr;
5790 u64 cookie;
5791 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5792 u32 freq, duration;
5793 int err;
5794
5795 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5796 !info->attrs[NL80211_ATTR_DURATION])
5797 return -EINVAL;
5798
5799 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5800
ebf348fc
JB
5801 if (!rdev->ops->remain_on_channel ||
5802 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
5803 return -EOPNOTSUPP;
5804
9588bbd5 5805 /*
ebf348fc
JB
5806 * We should be on that channel for at least a minimum amount of
5807 * time (10ms) but no longer than the driver supports.
9588bbd5 5808 */
ebf348fc 5809 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
a293911d 5810 duration > rdev->wiphy.max_remain_on_channel_duration)
9588bbd5
JM
5811 return -EINVAL;
5812
cd6c6598
JB
5813 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5814 !nl80211_valid_channel_type(info, &channel_type))
5815 return -EINVAL;
9588bbd5
JM
5816
5817 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5818 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4c476991
JB
5819 if (chan == NULL)
5820 return -EINVAL;
9588bbd5
JM
5821
5822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
5823 if (!msg)
5824 return -ENOMEM;
9588bbd5
JM
5825
5826 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5827 NL80211_CMD_REMAIN_ON_CHANNEL);
5828
5829 if (IS_ERR(hdr)) {
5830 err = PTR_ERR(hdr);
5831 goto free_msg;
5832 }
5833
71bbc994 5834 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
9588bbd5
JM
5835 channel_type, duration, &cookie);
5836
5837 if (err)
5838 goto free_msg;
5839
9360ffd1
DM
5840 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5841 goto nla_put_failure;
9588bbd5
JM
5842
5843 genlmsg_end(msg, hdr);
4c476991
JB
5844
5845 return genlmsg_reply(msg, info);
9588bbd5
JM
5846
5847 nla_put_failure:
5848 err = -ENOBUFS;
5849 free_msg:
5850 nlmsg_free(msg);
9588bbd5
JM
5851 return err;
5852}
5853
5854static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5855 struct genl_info *info)
5856{
4c476991 5857 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 5858 struct wireless_dev *wdev = info->user_ptr[1];
9588bbd5 5859 u64 cookie;
9588bbd5
JM
5860
5861 if (!info->attrs[NL80211_ATTR_COOKIE])
5862 return -EINVAL;
5863
4c476991
JB
5864 if (!rdev->ops->cancel_remain_on_channel)
5865 return -EOPNOTSUPP;
9588bbd5 5866
9588bbd5
JM
5867 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5868
71bbc994 5869 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
9588bbd5
JM
5870}
5871
13ae75b1
JM
5872static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5873 u8 *rates, u8 rates_len)
5874{
5875 u8 i;
5876 u32 mask = 0;
5877
5878 for (i = 0; i < rates_len; i++) {
5879 int rate = (rates[i] & 0x7f) * 5;
5880 int ridx;
5881 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5882 struct ieee80211_rate *srate =
5883 &sband->bitrates[ridx];
5884 if (rate == srate->bitrate) {
5885 mask |= 1 << ridx;
5886 break;
5887 }
5888 }
5889 if (ridx == sband->n_bitrates)
5890 return 0; /* rate not found */
5891 }
5892
5893 return mask;
5894}
5895
24db78c0
SW
5896static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5897 u8 *rates, u8 rates_len,
5898 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5899{
5900 u8 i;
5901
5902 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5903
5904 for (i = 0; i < rates_len; i++) {
5905 int ridx, rbit;
5906
5907 ridx = rates[i] / 8;
5908 rbit = BIT(rates[i] % 8);
5909
5910 /* check validity */
910570b5 5911 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
24db78c0
SW
5912 return false;
5913
5914 /* check availability */
5915 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5916 mcs[ridx] |= rbit;
5917 else
5918 return false;
5919 }
5920
5921 return true;
5922}
5923
b54452b0 5924static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
13ae75b1
JM
5925 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5926 .len = NL80211_MAX_SUPP_RATES },
24db78c0
SW
5927 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5928 .len = NL80211_MAX_SUPP_HT_RATES },
13ae75b1
JM
5929};
5930
5931static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5932 struct genl_info *info)
5933{
5934 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4c476991 5935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
13ae75b1 5936 struct cfg80211_bitrate_mask mask;
4c476991
JB
5937 int rem, i;
5938 struct net_device *dev = info->user_ptr[1];
13ae75b1
JM
5939 struct nlattr *tx_rates;
5940 struct ieee80211_supported_band *sband;
5941
5942 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5943 return -EINVAL;
5944
4c476991
JB
5945 if (!rdev->ops->set_bitrate_mask)
5946 return -EOPNOTSUPP;
13ae75b1
JM
5947
5948 memset(&mask, 0, sizeof(mask));
5949 /* Default to all rates enabled */
5950 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5951 sband = rdev->wiphy.bands[i];
5952 mask.control[i].legacy =
5953 sband ? (1 << sband->n_bitrates) - 1 : 0;
24db78c0
SW
5954 if (sband)
5955 memcpy(mask.control[i].mcs,
5956 sband->ht_cap.mcs.rx_mask,
5957 sizeof(mask.control[i].mcs));
5958 else
5959 memset(mask.control[i].mcs, 0,
5960 sizeof(mask.control[i].mcs));
13ae75b1
JM
5961 }
5962
5963 /*
5964 * The nested attribute uses enum nl80211_band as the index. This maps
5965 * directly to the enum ieee80211_band values used in cfg80211.
5966 */
24db78c0 5967 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
13ae75b1
JM
5968 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5969 {
5970 enum ieee80211_band band = nla_type(tx_rates);
4c476991
JB
5971 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5972 return -EINVAL;
13ae75b1 5973 sband = rdev->wiphy.bands[band];
4c476991
JB
5974 if (sband == NULL)
5975 return -EINVAL;
13ae75b1
JM
5976 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5977 nla_len(tx_rates), nl80211_txattr_policy);
5978 if (tb[NL80211_TXRATE_LEGACY]) {
5979 mask.control[band].legacy = rateset_to_mask(
5980 sband,
5981 nla_data(tb[NL80211_TXRATE_LEGACY]),
5982 nla_len(tb[NL80211_TXRATE_LEGACY]));
218d2e26
BS
5983 if ((mask.control[band].legacy == 0) &&
5984 nla_len(tb[NL80211_TXRATE_LEGACY]))
5985 return -EINVAL;
24db78c0
SW
5986 }
5987 if (tb[NL80211_TXRATE_MCS]) {
5988 if (!ht_rateset_to_mask(
5989 sband,
5990 nla_data(tb[NL80211_TXRATE_MCS]),
5991 nla_len(tb[NL80211_TXRATE_MCS]),
5992 mask.control[band].mcs))
5993 return -EINVAL;
5994 }
5995
5996 if (mask.control[band].legacy == 0) {
5997 /* don't allow empty legacy rates if HT
5998 * is not even supported. */
5999 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6000 return -EINVAL;
6001
6002 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6003 if (mask.control[band].mcs[i])
6004 break;
6005
6006 /* legacy and mcs rates may not be both empty */
6007 if (i == IEEE80211_HT_MCS_MASK_LEN)
4c476991 6008 return -EINVAL;
13ae75b1
JM
6009 }
6010 }
6011
4c476991 6012 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
13ae75b1
JM
6013}
6014
2e161f78 6015static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
026331c4 6016{
4c476991 6017 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 6018 struct wireless_dev *wdev = info->user_ptr[1];
2e161f78 6019 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
026331c4
JM
6020
6021 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6022 return -EINVAL;
6023
2e161f78
JB
6024 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6025 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
026331c4 6026
71bbc994
JB
6027 switch (wdev->iftype) {
6028 case NL80211_IFTYPE_STATION:
6029 case NL80211_IFTYPE_ADHOC:
6030 case NL80211_IFTYPE_P2P_CLIENT:
6031 case NL80211_IFTYPE_AP:
6032 case NL80211_IFTYPE_AP_VLAN:
6033 case NL80211_IFTYPE_MESH_POINT:
6034 case NL80211_IFTYPE_P2P_GO:
6035 break;
6036 default:
4c476991 6037 return -EOPNOTSUPP;
71bbc994 6038 }
026331c4
JM
6039
6040 /* not much point in registering if we can't reply */
4c476991
JB
6041 if (!rdev->ops->mgmt_tx)
6042 return -EOPNOTSUPP;
026331c4 6043
71bbc994 6044 return cfg80211_mlme_register_mgmt(wdev, info->snd_pid, frame_type,
026331c4
JM
6045 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6046 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
026331c4
JM
6047}
6048
2e161f78 6049static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
026331c4 6050{
4c476991 6051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 6052 struct wireless_dev *wdev = info->user_ptr[1];
026331c4
JM
6053 struct ieee80211_channel *chan;
6054 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
252aa631 6055 bool channel_type_valid = false;
026331c4
JM
6056 u32 freq;
6057 int err;
d64d373f 6058 void *hdr = NULL;
026331c4 6059 u64 cookie;
e247bd90 6060 struct sk_buff *msg = NULL;
f7ca38df 6061 unsigned int wait = 0;
e247bd90
JB
6062 bool offchan, no_cck, dont_wait_for_ack;
6063
6064 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
026331c4
JM
6065
6066 if (!info->attrs[NL80211_ATTR_FRAME] ||
6067 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6068 return -EINVAL;
6069
4c476991
JB
6070 if (!rdev->ops->mgmt_tx)
6071 return -EOPNOTSUPP;
026331c4 6072
71bbc994
JB
6073 switch (wdev->iftype) {
6074 case NL80211_IFTYPE_STATION:
6075 case NL80211_IFTYPE_ADHOC:
6076 case NL80211_IFTYPE_P2P_CLIENT:
6077 case NL80211_IFTYPE_AP:
6078 case NL80211_IFTYPE_AP_VLAN:
6079 case NL80211_IFTYPE_MESH_POINT:
6080 case NL80211_IFTYPE_P2P_GO:
6081 break;
6082 default:
4c476991 6083 return -EOPNOTSUPP;
71bbc994 6084 }
026331c4 6085
f7ca38df 6086 if (info->attrs[NL80211_ATTR_DURATION]) {
7c4ef712 6087 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
f7ca38df
JB
6088 return -EINVAL;
6089 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
ebf348fc
JB
6090
6091 /*
6092 * We should wait on the channel for at least a minimum amount
6093 * of time (10ms) but no longer than the driver supports.
6094 */
6095 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6096 wait > rdev->wiphy.max_remain_on_channel_duration)
6097 return -EINVAL;
6098
f7ca38df
JB
6099 }
6100
026331c4 6101 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
cd6c6598 6102 if (!nl80211_valid_channel_type(info, &channel_type))
4c476991 6103 return -EINVAL;
252aa631 6104 channel_type_valid = true;
026331c4
JM
6105 }
6106
f7ca38df
JB
6107 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6108
7c4ef712
JB
6109 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6110 return -EINVAL;
6111
e9f935e3
RM
6112 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6113
026331c4
JM
6114 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6115 chan = rdev_freq_to_chan(rdev, freq, channel_type);
4c476991
JB
6116 if (chan == NULL)
6117 return -EINVAL;
026331c4 6118
e247bd90
JB
6119 if (!dont_wait_for_ack) {
6120 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6121 if (!msg)
6122 return -ENOMEM;
026331c4 6123
e247bd90
JB
6124 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6125 NL80211_CMD_FRAME);
026331c4 6126
e247bd90
JB
6127 if (IS_ERR(hdr)) {
6128 err = PTR_ERR(hdr);
6129 goto free_msg;
6130 }
026331c4 6131 }
e247bd90 6132
71bbc994 6133 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
f7ca38df 6134 channel_type_valid, wait,
2e161f78
JB
6135 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6136 nla_len(info->attrs[NL80211_ATTR_FRAME]),
e247bd90 6137 no_cck, dont_wait_for_ack, &cookie);
026331c4
JM
6138 if (err)
6139 goto free_msg;
6140
e247bd90 6141 if (msg) {
9360ffd1
DM
6142 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6143 goto nla_put_failure;
026331c4 6144
e247bd90
JB
6145 genlmsg_end(msg, hdr);
6146 return genlmsg_reply(msg, info);
6147 }
6148
6149 return 0;
026331c4
JM
6150
6151 nla_put_failure:
6152 err = -ENOBUFS;
6153 free_msg:
6154 nlmsg_free(msg);
026331c4
JM
6155 return err;
6156}
6157
f7ca38df
JB
6158static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6159{
6160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
71bbc994 6161 struct wireless_dev *wdev = info->user_ptr[1];
f7ca38df
JB
6162 u64 cookie;
6163
6164 if (!info->attrs[NL80211_ATTR_COOKIE])
6165 return -EINVAL;
6166
6167 if (!rdev->ops->mgmt_tx_cancel_wait)
6168 return -EOPNOTSUPP;
6169
71bbc994
JB
6170 switch (wdev->iftype) {
6171 case NL80211_IFTYPE_STATION:
6172 case NL80211_IFTYPE_ADHOC:
6173 case NL80211_IFTYPE_P2P_CLIENT:
6174 case NL80211_IFTYPE_AP:
6175 case NL80211_IFTYPE_AP_VLAN:
6176 case NL80211_IFTYPE_P2P_GO:
6177 break;
6178 default:
f7ca38df 6179 return -EOPNOTSUPP;
71bbc994 6180 }
f7ca38df
JB
6181
6182 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6183
71bbc994 6184 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
f7ca38df
JB
6185}
6186
ffb9eb3d
KV
6187static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6188{
4c476991 6189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
ffb9eb3d 6190 struct wireless_dev *wdev;
4c476991 6191 struct net_device *dev = info->user_ptr[1];
ffb9eb3d
KV
6192 u8 ps_state;
6193 bool state;
6194 int err;
6195
4c476991
JB
6196 if (!info->attrs[NL80211_ATTR_PS_STATE])
6197 return -EINVAL;
ffb9eb3d
KV
6198
6199 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6200
4c476991
JB
6201 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6202 return -EINVAL;
ffb9eb3d
KV
6203
6204 wdev = dev->ieee80211_ptr;
6205
4c476991
JB
6206 if (!rdev->ops->set_power_mgmt)
6207 return -EOPNOTSUPP;
ffb9eb3d
KV
6208
6209 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6210
6211 if (state == wdev->ps)
4c476991 6212 return 0;
ffb9eb3d 6213
4c476991
JB
6214 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6215 wdev->ps_timeout);
6216 if (!err)
6217 wdev->ps = state;
ffb9eb3d
KV
6218 return err;
6219}
6220
6221static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6222{
4c476991 6223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
ffb9eb3d
KV
6224 enum nl80211_ps_state ps_state;
6225 struct wireless_dev *wdev;
4c476991 6226 struct net_device *dev = info->user_ptr[1];
ffb9eb3d
KV
6227 struct sk_buff *msg;
6228 void *hdr;
6229 int err;
6230
ffb9eb3d
KV
6231 wdev = dev->ieee80211_ptr;
6232
4c476991
JB
6233 if (!rdev->ops->set_power_mgmt)
6234 return -EOPNOTSUPP;
ffb9eb3d
KV
6235
6236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4c476991
JB
6237 if (!msg)
6238 return -ENOMEM;
ffb9eb3d
KV
6239
6240 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6241 NL80211_CMD_GET_POWER_SAVE);
6242 if (!hdr) {
4c476991 6243 err = -ENOBUFS;
ffb9eb3d
KV
6244 goto free_msg;
6245 }
6246
6247 if (wdev->ps)
6248 ps_state = NL80211_PS_ENABLED;
6249 else
6250 ps_state = NL80211_PS_DISABLED;
6251
9360ffd1
DM
6252 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6253 goto nla_put_failure;
ffb9eb3d
KV
6254
6255 genlmsg_end(msg, hdr);
4c476991 6256 return genlmsg_reply(msg, info);
ffb9eb3d 6257
4c476991 6258 nla_put_failure:
ffb9eb3d 6259 err = -ENOBUFS;
4c476991 6260 free_msg:
ffb9eb3d 6261 nlmsg_free(msg);
ffb9eb3d
KV
6262 return err;
6263}
6264
d6dc1a38
JO
6265static struct nla_policy
6266nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6267 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6268 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6269 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6270};
6271
6272static int nl80211_set_cqm_rssi(struct genl_info *info,
6273 s32 threshold, u32 hysteresis)
6274{
4c476991 6275 struct cfg80211_registered_device *rdev = info->user_ptr[0];
d6dc1a38 6276 struct wireless_dev *wdev;
4c476991 6277 struct net_device *dev = info->user_ptr[1];
d6dc1a38
JO
6278
6279 if (threshold > 0)
6280 return -EINVAL;
6281
d6dc1a38
JO
6282 wdev = dev->ieee80211_ptr;
6283
4c476991
JB
6284 if (!rdev->ops->set_cqm_rssi_config)
6285 return -EOPNOTSUPP;
d6dc1a38 6286
074ac8df 6287 if (wdev->iftype != NL80211_IFTYPE_STATION &&
4c476991
JB
6288 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6289 return -EOPNOTSUPP;
d6dc1a38 6290
4c476991
JB
6291 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6292 threshold, hysteresis);
d6dc1a38
JO
6293}
6294
6295static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6296{
6297 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6298 struct nlattr *cqm;
6299 int err;
6300
6301 cqm = info->attrs[NL80211_ATTR_CQM];
6302 if (!cqm) {
6303 err = -EINVAL;
6304 goto out;
6305 }
6306
6307 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6308 nl80211_attr_cqm_policy);
6309 if (err)
6310 goto out;
6311
6312 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6313 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6314 s32 threshold;
6315 u32 hysteresis;
6316 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6317 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6318 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6319 } else
6320 err = -EINVAL;
6321
6322out:
6323 return err;
6324}
6325
29cbe68c
JB
6326static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6327{
6328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6329 struct net_device *dev = info->user_ptr[1];
6330 struct mesh_config cfg;
c80d545d 6331 struct mesh_setup setup;
29cbe68c
JB
6332 int err;
6333
6334 /* start with default */
6335 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
c80d545d 6336 memcpy(&setup, &default_mesh_setup, sizeof(setup));
29cbe68c 6337
24bdd9f4 6338 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
29cbe68c 6339 /* and parse parameters if given */
24bdd9f4 6340 err = nl80211_parse_mesh_config(info, &cfg, NULL);
29cbe68c
JB
6341 if (err)
6342 return err;
6343 }
6344
6345 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6346 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6347 return -EINVAL;
6348
c80d545d
JC
6349 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6350 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6351
4bb62344
CYY
6352 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6353 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6354 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6355 return -EINVAL;
6356
c80d545d
JC
6357 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6358 /* parse additional setup parameters if given */
6359 err = nl80211_parse_mesh_setup(info, &setup);
6360 if (err)
6361 return err;
6362 }
6363
cc1d2806
JB
6364 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6365 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6366
6367 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6368 !nl80211_valid_channel_type(info, &channel_type))
6369 return -EINVAL;
6370
6371 setup.channel = rdev_freq_to_chan(rdev,
6372 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6373 channel_type);
6374 if (!setup.channel)
6375 return -EINVAL;
6376 setup.channel_type = channel_type;
6377 } else {
6378 /* cfg80211_join_mesh() will sort it out */
6379 setup.channel = NULL;
6380 }
6381
c80d545d 6382 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
29cbe68c
JB
6383}
6384
6385static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6386{
6387 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6388 struct net_device *dev = info->user_ptr[1];
6389
6390 return cfg80211_leave_mesh(rdev, dev);
6391}
6392
dfb89c56 6393#ifdef CONFIG_PM
ff1b6e69
JB
6394static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6395{
6396 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6397 struct sk_buff *msg;
6398 void *hdr;
6399
6400 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6401 return -EOPNOTSUPP;
6402
6403 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6404 if (!msg)
6405 return -ENOMEM;
6406
6407 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6408 NL80211_CMD_GET_WOWLAN);
6409 if (!hdr)
6410 goto nla_put_failure;
6411
6412 if (rdev->wowlan) {
6413 struct nlattr *nl_wowlan;
6414
6415 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6416 if (!nl_wowlan)
6417 goto nla_put_failure;
6418
9360ffd1
DM
6419 if ((rdev->wowlan->any &&
6420 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6421 (rdev->wowlan->disconnect &&
6422 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6423 (rdev->wowlan->magic_pkt &&
6424 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6425 (rdev->wowlan->gtk_rekey_failure &&
6426 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6427 (rdev->wowlan->eap_identity_req &&
6428 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6429 (rdev->wowlan->four_way_handshake &&
6430 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6431 (rdev->wowlan->rfkill_release &&
6432 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6433 goto nla_put_failure;
ff1b6e69
JB
6434 if (rdev->wowlan->n_patterns) {
6435 struct nlattr *nl_pats, *nl_pat;
6436 int i, pat_len;
6437
6438 nl_pats = nla_nest_start(msg,
6439 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6440 if (!nl_pats)
6441 goto nla_put_failure;
6442
6443 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6444 nl_pat = nla_nest_start(msg, i + 1);
6445 if (!nl_pat)
6446 goto nla_put_failure;
6447 pat_len = rdev->wowlan->patterns[i].pattern_len;
9360ffd1
DM
6448 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6449 DIV_ROUND_UP(pat_len, 8),
6450 rdev->wowlan->patterns[i].mask) ||
6451 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6452 pat_len,
6453 rdev->wowlan->patterns[i].pattern))
6454 goto nla_put_failure;
ff1b6e69
JB
6455 nla_nest_end(msg, nl_pat);
6456 }
6457 nla_nest_end(msg, nl_pats);
6458 }
6459
6460 nla_nest_end(msg, nl_wowlan);
6461 }
6462
6463 genlmsg_end(msg, hdr);
6464 return genlmsg_reply(msg, info);
6465
6466nla_put_failure:
6467 nlmsg_free(msg);
6468 return -ENOBUFS;
6469}
6470
6471static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6472{
6473 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6474 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
ff1b6e69 6475 struct cfg80211_wowlan new_triggers = {};
ae33bd81 6476 struct cfg80211_wowlan *ntrig;
ff1b6e69
JB
6477 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6478 int err, i;
6d52563f 6479 bool prev_enabled = rdev->wowlan;
ff1b6e69
JB
6480
6481 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6482 return -EOPNOTSUPP;
6483
ae33bd81
JB
6484 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6485 cfg80211_rdev_free_wowlan(rdev);
6486 rdev->wowlan = NULL;
6487 goto set_wakeup;
6488 }
ff1b6e69
JB
6489
6490 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6491 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6492 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6493 nl80211_wowlan_policy);
6494 if (err)
6495 return err;
6496
6497 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6498 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6499 return -EINVAL;
6500 new_triggers.any = true;
6501 }
6502
6503 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6504 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6505 return -EINVAL;
6506 new_triggers.disconnect = true;
6507 }
6508
6509 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6510 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6511 return -EINVAL;
6512 new_triggers.magic_pkt = true;
6513 }
6514
77dbbb13
JB
6515 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6516 return -EINVAL;
6517
6518 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6519 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6520 return -EINVAL;
6521 new_triggers.gtk_rekey_failure = true;
6522 }
6523
6524 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6525 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6526 return -EINVAL;
6527 new_triggers.eap_identity_req = true;
6528 }
6529
6530 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6531 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6532 return -EINVAL;
6533 new_triggers.four_way_handshake = true;
6534 }
6535
6536 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6537 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6538 return -EINVAL;
6539 new_triggers.rfkill_release = true;
6540 }
6541
ff1b6e69
JB
6542 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6543 struct nlattr *pat;
6544 int n_patterns = 0;
6545 int rem, pat_len, mask_len;
6546 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6547
6548 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6549 rem)
6550 n_patterns++;
6551 if (n_patterns > wowlan->n_patterns)
6552 return -EINVAL;
6553
6554 new_triggers.patterns = kcalloc(n_patterns,
6555 sizeof(new_triggers.patterns[0]),
6556 GFP_KERNEL);
6557 if (!new_triggers.patterns)
6558 return -ENOMEM;
6559
6560 new_triggers.n_patterns = n_patterns;
6561 i = 0;
6562
6563 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6564 rem) {
6565 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6566 nla_data(pat), nla_len(pat), NULL);
6567 err = -EINVAL;
6568 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6569 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6570 goto error;
6571 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6572 mask_len = DIV_ROUND_UP(pat_len, 8);
6573 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6574 mask_len)
6575 goto error;
6576 if (pat_len > wowlan->pattern_max_len ||
6577 pat_len < wowlan->pattern_min_len)
6578 goto error;
6579
6580 new_triggers.patterns[i].mask =
6581 kmalloc(mask_len + pat_len, GFP_KERNEL);
6582 if (!new_triggers.patterns[i].mask) {
6583 err = -ENOMEM;
6584 goto error;
6585 }
6586 new_triggers.patterns[i].pattern =
6587 new_triggers.patterns[i].mask + mask_len;
6588 memcpy(new_triggers.patterns[i].mask,
6589 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6590 mask_len);
6591 new_triggers.patterns[i].pattern_len = pat_len;
6592 memcpy(new_triggers.patterns[i].pattern,
6593 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6594 pat_len);
6595 i++;
6596 }
6597 }
6598
ae33bd81
JB
6599 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6600 if (!ntrig) {
6601 err = -ENOMEM;
6602 goto error;
ff1b6e69 6603 }
ae33bd81
JB
6604 cfg80211_rdev_free_wowlan(rdev);
6605 rdev->wowlan = ntrig;
ff1b6e69 6606
ae33bd81 6607 set_wakeup:
6d52563f
JB
6608 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6609 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6610
ff1b6e69
JB
6611 return 0;
6612 error:
6613 for (i = 0; i < new_triggers.n_patterns; i++)
6614 kfree(new_triggers.patterns[i].mask);
6615 kfree(new_triggers.patterns);
6616 return err;
6617}
dfb89c56 6618#endif
ff1b6e69 6619
e5497d76
JB
6620static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6621{
6622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6623 struct net_device *dev = info->user_ptr[1];
6624 struct wireless_dev *wdev = dev->ieee80211_ptr;
6625 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6626 struct cfg80211_gtk_rekey_data rekey_data;
6627 int err;
6628
6629 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6630 return -EINVAL;
6631
6632 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6633 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6634 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6635 nl80211_rekey_policy);
6636 if (err)
6637 return err;
6638
6639 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6640 return -ERANGE;
6641 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6642 return -ERANGE;
6643 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6644 return -ERANGE;
6645
6646 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6647 NL80211_KEK_LEN);
6648 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6649 NL80211_KCK_LEN);
6650 memcpy(rekey_data.replay_ctr,
6651 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6652 NL80211_REPLAY_CTR_LEN);
6653
6654 wdev_lock(wdev);
6655 if (!wdev->current_bss) {
6656 err = -ENOTCONN;
6657 goto out;
6658 }
6659
6660 if (!rdev->ops->set_rekey_data) {
6661 err = -EOPNOTSUPP;
6662 goto out;
6663 }
6664
6665 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6666 out:
6667 wdev_unlock(wdev);
6668 return err;
6669}
6670
28946da7
JB
6671static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6672 struct genl_info *info)
6673{
6674 struct net_device *dev = info->user_ptr[1];
6675 struct wireless_dev *wdev = dev->ieee80211_ptr;
6676
6677 if (wdev->iftype != NL80211_IFTYPE_AP &&
6678 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6679 return -EINVAL;
6680
6681 if (wdev->ap_unexpected_nlpid)
6682 return -EBUSY;
6683
6684 wdev->ap_unexpected_nlpid = info->snd_pid;
6685 return 0;
6686}
6687
7f6cf311
JB
6688static int nl80211_probe_client(struct sk_buff *skb,
6689 struct genl_info *info)
6690{
6691 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6692 struct net_device *dev = info->user_ptr[1];
6693 struct wireless_dev *wdev = dev->ieee80211_ptr;
6694 struct sk_buff *msg;
6695 void *hdr;
6696 const u8 *addr;
6697 u64 cookie;
6698 int err;
6699
6700 if (wdev->iftype != NL80211_IFTYPE_AP &&
6701 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6702 return -EOPNOTSUPP;
6703
6704 if (!info->attrs[NL80211_ATTR_MAC])
6705 return -EINVAL;
6706
6707 if (!rdev->ops->probe_client)
6708 return -EOPNOTSUPP;
6709
6710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6711 if (!msg)
6712 return -ENOMEM;
6713
6714 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6715 NL80211_CMD_PROBE_CLIENT);
6716
6717 if (IS_ERR(hdr)) {
6718 err = PTR_ERR(hdr);
6719 goto free_msg;
6720 }
6721
6722 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6723
6724 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6725 if (err)
6726 goto free_msg;
6727
9360ffd1
DM
6728 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6729 goto nla_put_failure;
7f6cf311
JB
6730
6731 genlmsg_end(msg, hdr);
6732
6733 return genlmsg_reply(msg, info);
6734
6735 nla_put_failure:
6736 err = -ENOBUFS;
6737 free_msg:
6738 nlmsg_free(msg);
6739 return err;
6740}
6741
5e760230
JB
6742static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6743{
6744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6745
6746 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6747 return -EOPNOTSUPP;
6748
6749 if (rdev->ap_beacons_nlpid)
6750 return -EBUSY;
6751
6752 rdev->ap_beacons_nlpid = info->snd_pid;
6753
6754 return 0;
6755}
6756
4c476991
JB
6757#define NL80211_FLAG_NEED_WIPHY 0x01
6758#define NL80211_FLAG_NEED_NETDEV 0x02
6759#define NL80211_FLAG_NEED_RTNL 0x04
41265714
JB
6760#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6761#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6762 NL80211_FLAG_CHECK_NETDEV_UP)
1bf614ef
JB
6763#define NL80211_FLAG_NEED_WDEV 0x10
6764/* If a netdev is associated, it must be UP */
6765#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6766 NL80211_FLAG_CHECK_NETDEV_UP)
4c476991
JB
6767
6768static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6769 struct genl_info *info)
6770{
6771 struct cfg80211_registered_device *rdev;
89a54e48 6772 struct wireless_dev *wdev;
4c476991 6773 struct net_device *dev;
4c476991
JB
6774 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6775
6776 if (rtnl)
6777 rtnl_lock();
6778
6779 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4f7eff10 6780 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
4c476991
JB
6781 if (IS_ERR(rdev)) {
6782 if (rtnl)
6783 rtnl_unlock();
6784 return PTR_ERR(rdev);
6785 }
6786 info->user_ptr[0] = rdev;
1bf614ef
JB
6787 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6788 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
89a54e48
JB
6789 mutex_lock(&cfg80211_mutex);
6790 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6791 info->attrs);
6792 if (IS_ERR(wdev)) {
6793 mutex_unlock(&cfg80211_mutex);
4c476991
JB
6794 if (rtnl)
6795 rtnl_unlock();
89a54e48 6796 return PTR_ERR(wdev);
4c476991 6797 }
89a54e48 6798
89a54e48
JB
6799 dev = wdev->netdev;
6800 rdev = wiphy_to_dev(wdev->wiphy);
6801
1bf614ef
JB
6802 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6803 if (!dev) {
6804 mutex_unlock(&cfg80211_mutex);
6805 if (rtnl)
6806 rtnl_unlock();
6807 return -EINVAL;
6808 }
6809
6810 info->user_ptr[1] = dev;
6811 } else {
6812 info->user_ptr[1] = wdev;
6813 }
6814
6815 if (dev) {
6816 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6817 !netif_running(dev)) {
6818 mutex_unlock(&cfg80211_mutex);
6819 if (rtnl)
6820 rtnl_unlock();
6821 return -ENETDOWN;
6822 }
6823
6824 dev_hold(dev);
41265714 6825 }
89a54e48 6826
89a54e48
JB
6827 cfg80211_lock_rdev(rdev);
6828
6829 mutex_unlock(&cfg80211_mutex);
6830
4c476991 6831 info->user_ptr[0] = rdev;
4c476991
JB
6832 }
6833
6834 return 0;
6835}
6836
6837static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6838 struct genl_info *info)
6839{
6840 if (info->user_ptr[0])
6841 cfg80211_unlock_rdev(info->user_ptr[0]);
1bf614ef
JB
6842 if (info->user_ptr[1]) {
6843 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6844 struct wireless_dev *wdev = info->user_ptr[1];
6845
6846 if (wdev->netdev)
6847 dev_put(wdev->netdev);
6848 } else {
6849 dev_put(info->user_ptr[1]);
6850 }
6851 }
4c476991
JB
6852 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6853 rtnl_unlock();
6854}
6855
55682965
JB
6856static struct genl_ops nl80211_ops[] = {
6857 {
6858 .cmd = NL80211_CMD_GET_WIPHY,
6859 .doit = nl80211_get_wiphy,
6860 .dumpit = nl80211_dump_wiphy,
6861 .policy = nl80211_policy,
6862 /* can be retrieved by unprivileged users */
4c476991 6863 .internal_flags = NL80211_FLAG_NEED_WIPHY,
55682965
JB
6864 },
6865 {
6866 .cmd = NL80211_CMD_SET_WIPHY,
6867 .doit = nl80211_set_wiphy,
6868 .policy = nl80211_policy,
6869 .flags = GENL_ADMIN_PERM,
4c476991 6870 .internal_flags = NL80211_FLAG_NEED_RTNL,
55682965
JB
6871 },
6872 {
6873 .cmd = NL80211_CMD_GET_INTERFACE,
6874 .doit = nl80211_get_interface,
6875 .dumpit = nl80211_dump_interface,
6876 .policy = nl80211_policy,
6877 /* can be retrieved by unprivileged users */
72fb2abc 6878 .internal_flags = NL80211_FLAG_NEED_WDEV,
55682965
JB
6879 },
6880 {
6881 .cmd = NL80211_CMD_SET_INTERFACE,
6882 .doit = nl80211_set_interface,
6883 .policy = nl80211_policy,
6884 .flags = GENL_ADMIN_PERM,
4c476991
JB
6885 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6886 NL80211_FLAG_NEED_RTNL,
55682965
JB
6887 },
6888 {
6889 .cmd = NL80211_CMD_NEW_INTERFACE,
6890 .doit = nl80211_new_interface,
6891 .policy = nl80211_policy,
6892 .flags = GENL_ADMIN_PERM,
4c476991
JB
6893 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6894 NL80211_FLAG_NEED_RTNL,
55682965
JB
6895 },
6896 {
6897 .cmd = NL80211_CMD_DEL_INTERFACE,
6898 .doit = nl80211_del_interface,
6899 .policy = nl80211_policy,
41ade00f 6900 .flags = GENL_ADMIN_PERM,
84efbb84 6901 .internal_flags = NL80211_FLAG_NEED_WDEV |
4c476991 6902 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
6903 },
6904 {
6905 .cmd = NL80211_CMD_GET_KEY,
6906 .doit = nl80211_get_key,
6907 .policy = nl80211_policy,
6908 .flags = GENL_ADMIN_PERM,
2b5f8b0b 6909 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6910 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
6911 },
6912 {
6913 .cmd = NL80211_CMD_SET_KEY,
6914 .doit = nl80211_set_key,
6915 .policy = nl80211_policy,
6916 .flags = GENL_ADMIN_PERM,
41265714 6917 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6918 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
6919 },
6920 {
6921 .cmd = NL80211_CMD_NEW_KEY,
6922 .doit = nl80211_new_key,
6923 .policy = nl80211_policy,
6924 .flags = GENL_ADMIN_PERM,
41265714 6925 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6926 NL80211_FLAG_NEED_RTNL,
41ade00f
JB
6927 },
6928 {
6929 .cmd = NL80211_CMD_DEL_KEY,
6930 .doit = nl80211_del_key,
6931 .policy = nl80211_policy,
55682965 6932 .flags = GENL_ADMIN_PERM,
41265714 6933 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6934 NL80211_FLAG_NEED_RTNL,
55682965 6935 },
ed1b6cc7
JB
6936 {
6937 .cmd = NL80211_CMD_SET_BEACON,
6938 .policy = nl80211_policy,
6939 .flags = GENL_ADMIN_PERM,
8860020e 6940 .doit = nl80211_set_beacon,
2b5f8b0b 6941 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6942 NL80211_FLAG_NEED_RTNL,
ed1b6cc7
JB
6943 },
6944 {
8860020e 6945 .cmd = NL80211_CMD_START_AP,
ed1b6cc7
JB
6946 .policy = nl80211_policy,
6947 .flags = GENL_ADMIN_PERM,
8860020e 6948 .doit = nl80211_start_ap,
2b5f8b0b 6949 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6950 NL80211_FLAG_NEED_RTNL,
ed1b6cc7
JB
6951 },
6952 {
8860020e 6953 .cmd = NL80211_CMD_STOP_AP,
ed1b6cc7
JB
6954 .policy = nl80211_policy,
6955 .flags = GENL_ADMIN_PERM,
8860020e 6956 .doit = nl80211_stop_ap,
2b5f8b0b 6957 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6958 NL80211_FLAG_NEED_RTNL,
ed1b6cc7 6959 },
5727ef1b
JB
6960 {
6961 .cmd = NL80211_CMD_GET_STATION,
6962 .doit = nl80211_get_station,
2ec600d6 6963 .dumpit = nl80211_dump_station,
5727ef1b 6964 .policy = nl80211_policy,
4c476991
JB
6965 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6966 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
6967 },
6968 {
6969 .cmd = NL80211_CMD_SET_STATION,
6970 .doit = nl80211_set_station,
6971 .policy = nl80211_policy,
6972 .flags = GENL_ADMIN_PERM,
2b5f8b0b 6973 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6974 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
6975 },
6976 {
6977 .cmd = NL80211_CMD_NEW_STATION,
6978 .doit = nl80211_new_station,
6979 .policy = nl80211_policy,
6980 .flags = GENL_ADMIN_PERM,
41265714 6981 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6982 NL80211_FLAG_NEED_RTNL,
5727ef1b
JB
6983 },
6984 {
6985 .cmd = NL80211_CMD_DEL_STATION,
6986 .doit = nl80211_del_station,
6987 .policy = nl80211_policy,
2ec600d6 6988 .flags = GENL_ADMIN_PERM,
2b5f8b0b 6989 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6990 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
6991 },
6992 {
6993 .cmd = NL80211_CMD_GET_MPATH,
6994 .doit = nl80211_get_mpath,
6995 .dumpit = nl80211_dump_mpath,
6996 .policy = nl80211_policy,
6997 .flags = GENL_ADMIN_PERM,
41265714 6998 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 6999 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
7000 },
7001 {
7002 .cmd = NL80211_CMD_SET_MPATH,
7003 .doit = nl80211_set_mpath,
7004 .policy = nl80211_policy,
7005 .flags = GENL_ADMIN_PERM,
41265714 7006 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7007 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
7008 },
7009 {
7010 .cmd = NL80211_CMD_NEW_MPATH,
7011 .doit = nl80211_new_mpath,
7012 .policy = nl80211_policy,
7013 .flags = GENL_ADMIN_PERM,
41265714 7014 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7015 NL80211_FLAG_NEED_RTNL,
2ec600d6
LCC
7016 },
7017 {
7018 .cmd = NL80211_CMD_DEL_MPATH,
7019 .doit = nl80211_del_mpath,
7020 .policy = nl80211_policy,
9f1ba906 7021 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7022 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7023 NL80211_FLAG_NEED_RTNL,
9f1ba906
JM
7024 },
7025 {
7026 .cmd = NL80211_CMD_SET_BSS,
7027 .doit = nl80211_set_bss,
7028 .policy = nl80211_policy,
b2e1b302 7029 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7031 NL80211_FLAG_NEED_RTNL,
b2e1b302 7032 },
f130347c
LR
7033 {
7034 .cmd = NL80211_CMD_GET_REG,
7035 .doit = nl80211_get_reg,
7036 .policy = nl80211_policy,
7037 /* can be retrieved by unprivileged users */
7038 },
b2e1b302
LR
7039 {
7040 .cmd = NL80211_CMD_SET_REG,
7041 .doit = nl80211_set_reg,
7042 .policy = nl80211_policy,
7043 .flags = GENL_ADMIN_PERM,
7044 },
7045 {
7046 .cmd = NL80211_CMD_REQ_SET_REG,
7047 .doit = nl80211_req_set_reg,
7048 .policy = nl80211_policy,
93da9cc1 7049 .flags = GENL_ADMIN_PERM,
7050 },
7051 {
24bdd9f4
JC
7052 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7053 .doit = nl80211_get_mesh_config,
93da9cc1 7054 .policy = nl80211_policy,
7055 /* can be retrieved by unprivileged users */
2b5f8b0b 7056 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7057 NL80211_FLAG_NEED_RTNL,
93da9cc1 7058 },
7059 {
24bdd9f4
JC
7060 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7061 .doit = nl80211_update_mesh_config,
93da9cc1 7062 .policy = nl80211_policy,
9aed3cc1 7063 .flags = GENL_ADMIN_PERM,
29cbe68c 7064 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7065 NL80211_FLAG_NEED_RTNL,
9aed3cc1 7066 },
2a519311
JB
7067 {
7068 .cmd = NL80211_CMD_TRIGGER_SCAN,
7069 .doit = nl80211_trigger_scan,
7070 .policy = nl80211_policy,
7071 .flags = GENL_ADMIN_PERM,
fd014284 7072 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 7073 NL80211_FLAG_NEED_RTNL,
2a519311
JB
7074 },
7075 {
7076 .cmd = NL80211_CMD_GET_SCAN,
7077 .policy = nl80211_policy,
7078 .dumpit = nl80211_dump_scan,
7079 },
807f8a8c
LC
7080 {
7081 .cmd = NL80211_CMD_START_SCHED_SCAN,
7082 .doit = nl80211_start_sched_scan,
7083 .policy = nl80211_policy,
7084 .flags = GENL_ADMIN_PERM,
7085 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7086 NL80211_FLAG_NEED_RTNL,
7087 },
7088 {
7089 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7090 .doit = nl80211_stop_sched_scan,
7091 .policy = nl80211_policy,
7092 .flags = GENL_ADMIN_PERM,
7093 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7094 NL80211_FLAG_NEED_RTNL,
7095 },
636a5d36
JM
7096 {
7097 .cmd = NL80211_CMD_AUTHENTICATE,
7098 .doit = nl80211_authenticate,
7099 .policy = nl80211_policy,
7100 .flags = GENL_ADMIN_PERM,
41265714 7101 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7102 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
7103 },
7104 {
7105 .cmd = NL80211_CMD_ASSOCIATE,
7106 .doit = nl80211_associate,
7107 .policy = nl80211_policy,
7108 .flags = GENL_ADMIN_PERM,
41265714 7109 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7110 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
7111 },
7112 {
7113 .cmd = NL80211_CMD_DEAUTHENTICATE,
7114 .doit = nl80211_deauthenticate,
7115 .policy = nl80211_policy,
7116 .flags = GENL_ADMIN_PERM,
41265714 7117 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7118 NL80211_FLAG_NEED_RTNL,
636a5d36
JM
7119 },
7120 {
7121 .cmd = NL80211_CMD_DISASSOCIATE,
7122 .doit = nl80211_disassociate,
7123 .policy = nl80211_policy,
7124 .flags = GENL_ADMIN_PERM,
41265714 7125 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7126 NL80211_FLAG_NEED_RTNL,
636a5d36 7127 },
04a773ad
JB
7128 {
7129 .cmd = NL80211_CMD_JOIN_IBSS,
7130 .doit = nl80211_join_ibss,
7131 .policy = nl80211_policy,
7132 .flags = GENL_ADMIN_PERM,
41265714 7133 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7134 NL80211_FLAG_NEED_RTNL,
04a773ad
JB
7135 },
7136 {
7137 .cmd = NL80211_CMD_LEAVE_IBSS,
7138 .doit = nl80211_leave_ibss,
7139 .policy = nl80211_policy,
7140 .flags = GENL_ADMIN_PERM,
41265714 7141 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7142 NL80211_FLAG_NEED_RTNL,
04a773ad 7143 },
aff89a9b
JB
7144#ifdef CONFIG_NL80211_TESTMODE
7145 {
7146 .cmd = NL80211_CMD_TESTMODE,
7147 .doit = nl80211_testmode_do,
71063f0e 7148 .dumpit = nl80211_testmode_dump,
aff89a9b
JB
7149 .policy = nl80211_policy,
7150 .flags = GENL_ADMIN_PERM,
4c476991
JB
7151 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7152 NL80211_FLAG_NEED_RTNL,
aff89a9b
JB
7153 },
7154#endif
b23aa676
SO
7155 {
7156 .cmd = NL80211_CMD_CONNECT,
7157 .doit = nl80211_connect,
7158 .policy = nl80211_policy,
7159 .flags = GENL_ADMIN_PERM,
41265714 7160 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7161 NL80211_FLAG_NEED_RTNL,
b23aa676
SO
7162 },
7163 {
7164 .cmd = NL80211_CMD_DISCONNECT,
7165 .doit = nl80211_disconnect,
7166 .policy = nl80211_policy,
7167 .flags = GENL_ADMIN_PERM,
41265714 7168 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7169 NL80211_FLAG_NEED_RTNL,
b23aa676 7170 },
463d0183
JB
7171 {
7172 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7173 .doit = nl80211_wiphy_netns,
7174 .policy = nl80211_policy,
7175 .flags = GENL_ADMIN_PERM,
4c476991
JB
7176 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7177 NL80211_FLAG_NEED_RTNL,
463d0183 7178 },
61fa713c
HS
7179 {
7180 .cmd = NL80211_CMD_GET_SURVEY,
7181 .policy = nl80211_policy,
7182 .dumpit = nl80211_dump_survey,
7183 },
67fbb16b
SO
7184 {
7185 .cmd = NL80211_CMD_SET_PMKSA,
7186 .doit = nl80211_setdel_pmksa,
7187 .policy = nl80211_policy,
7188 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7190 NL80211_FLAG_NEED_RTNL,
67fbb16b
SO
7191 },
7192 {
7193 .cmd = NL80211_CMD_DEL_PMKSA,
7194 .doit = nl80211_setdel_pmksa,
7195 .policy = nl80211_policy,
7196 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7197 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7198 NL80211_FLAG_NEED_RTNL,
67fbb16b
SO
7199 },
7200 {
7201 .cmd = NL80211_CMD_FLUSH_PMKSA,
7202 .doit = nl80211_flush_pmksa,
7203 .policy = nl80211_policy,
7204 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7205 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
4c476991 7206 NL80211_FLAG_NEED_RTNL,
67fbb16b 7207 },
9588bbd5
JM
7208 {
7209 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7210 .doit = nl80211_remain_on_channel,
7211 .policy = nl80211_policy,
7212 .flags = GENL_ADMIN_PERM,
71bbc994 7213 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 7214 NL80211_FLAG_NEED_RTNL,
9588bbd5
JM
7215 },
7216 {
7217 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7218 .doit = nl80211_cancel_remain_on_channel,
7219 .policy = nl80211_policy,
7220 .flags = GENL_ADMIN_PERM,
71bbc994 7221 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 7222 NL80211_FLAG_NEED_RTNL,
9588bbd5 7223 },
13ae75b1
JM
7224 {
7225 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7226 .doit = nl80211_set_tx_bitrate_mask,
7227 .policy = nl80211_policy,
7228 .flags = GENL_ADMIN_PERM,
4c476991
JB
7229 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7230 NL80211_FLAG_NEED_RTNL,
13ae75b1 7231 },
026331c4 7232 {
2e161f78
JB
7233 .cmd = NL80211_CMD_REGISTER_FRAME,
7234 .doit = nl80211_register_mgmt,
026331c4
JM
7235 .policy = nl80211_policy,
7236 .flags = GENL_ADMIN_PERM,
71bbc994 7237 .internal_flags = NL80211_FLAG_NEED_WDEV |
4c476991 7238 NL80211_FLAG_NEED_RTNL,
026331c4
JM
7239 },
7240 {
2e161f78
JB
7241 .cmd = NL80211_CMD_FRAME,
7242 .doit = nl80211_tx_mgmt,
026331c4 7243 .policy = nl80211_policy,
f7ca38df 7244 .flags = GENL_ADMIN_PERM,
71bbc994 7245 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
f7ca38df
JB
7246 NL80211_FLAG_NEED_RTNL,
7247 },
7248 {
7249 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7250 .doit = nl80211_tx_mgmt_cancel_wait,
7251 .policy = nl80211_policy,
026331c4 7252 .flags = GENL_ADMIN_PERM,
71bbc994 7253 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
4c476991 7254 NL80211_FLAG_NEED_RTNL,
026331c4 7255 },
ffb9eb3d
KV
7256 {
7257 .cmd = NL80211_CMD_SET_POWER_SAVE,
7258 .doit = nl80211_set_power_save,
7259 .policy = nl80211_policy,
7260 .flags = GENL_ADMIN_PERM,
4c476991
JB
7261 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7262 NL80211_FLAG_NEED_RTNL,
ffb9eb3d
KV
7263 },
7264 {
7265 .cmd = NL80211_CMD_GET_POWER_SAVE,
7266 .doit = nl80211_get_power_save,
7267 .policy = nl80211_policy,
7268 /* can be retrieved by unprivileged users */
4c476991
JB
7269 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7270 NL80211_FLAG_NEED_RTNL,
ffb9eb3d 7271 },
d6dc1a38
JO
7272 {
7273 .cmd = NL80211_CMD_SET_CQM,
7274 .doit = nl80211_set_cqm,
7275 .policy = nl80211_policy,
7276 .flags = GENL_ADMIN_PERM,
4c476991
JB
7277 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7278 NL80211_FLAG_NEED_RTNL,
d6dc1a38 7279 },
f444de05
JB
7280 {
7281 .cmd = NL80211_CMD_SET_CHANNEL,
7282 .doit = nl80211_set_channel,
7283 .policy = nl80211_policy,
7284 .flags = GENL_ADMIN_PERM,
4c476991
JB
7285 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7286 NL80211_FLAG_NEED_RTNL,
f444de05 7287 },
e8347eba
BJ
7288 {
7289 .cmd = NL80211_CMD_SET_WDS_PEER,
7290 .doit = nl80211_set_wds_peer,
7291 .policy = nl80211_policy,
7292 .flags = GENL_ADMIN_PERM,
43b19952
JB
7293 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7294 NL80211_FLAG_NEED_RTNL,
e8347eba 7295 },
29cbe68c
JB
7296 {
7297 .cmd = NL80211_CMD_JOIN_MESH,
7298 .doit = nl80211_join_mesh,
7299 .policy = nl80211_policy,
7300 .flags = GENL_ADMIN_PERM,
7301 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7302 NL80211_FLAG_NEED_RTNL,
7303 },
7304 {
7305 .cmd = NL80211_CMD_LEAVE_MESH,
7306 .doit = nl80211_leave_mesh,
7307 .policy = nl80211_policy,
7308 .flags = GENL_ADMIN_PERM,
7309 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7310 NL80211_FLAG_NEED_RTNL,
7311 },
dfb89c56 7312#ifdef CONFIG_PM
ff1b6e69
JB
7313 {
7314 .cmd = NL80211_CMD_GET_WOWLAN,
7315 .doit = nl80211_get_wowlan,
7316 .policy = nl80211_policy,
7317 /* can be retrieved by unprivileged users */
7318 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7319 NL80211_FLAG_NEED_RTNL,
7320 },
7321 {
7322 .cmd = NL80211_CMD_SET_WOWLAN,
7323 .doit = nl80211_set_wowlan,
7324 .policy = nl80211_policy,
7325 .flags = GENL_ADMIN_PERM,
7326 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7327 NL80211_FLAG_NEED_RTNL,
7328 },
dfb89c56 7329#endif
e5497d76
JB
7330 {
7331 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7332 .doit = nl80211_set_rekey_data,
7333 .policy = nl80211_policy,
7334 .flags = GENL_ADMIN_PERM,
7335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7336 NL80211_FLAG_NEED_RTNL,
7337 },
109086ce
AN
7338 {
7339 .cmd = NL80211_CMD_TDLS_MGMT,
7340 .doit = nl80211_tdls_mgmt,
7341 .policy = nl80211_policy,
7342 .flags = GENL_ADMIN_PERM,
7343 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7344 NL80211_FLAG_NEED_RTNL,
7345 },
7346 {
7347 .cmd = NL80211_CMD_TDLS_OPER,
7348 .doit = nl80211_tdls_oper,
7349 .policy = nl80211_policy,
7350 .flags = GENL_ADMIN_PERM,
7351 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7352 NL80211_FLAG_NEED_RTNL,
7353 },
28946da7
JB
7354 {
7355 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7356 .doit = nl80211_register_unexpected_frame,
7357 .policy = nl80211_policy,
7358 .flags = GENL_ADMIN_PERM,
7359 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7360 NL80211_FLAG_NEED_RTNL,
7361 },
7f6cf311
JB
7362 {
7363 .cmd = NL80211_CMD_PROBE_CLIENT,
7364 .doit = nl80211_probe_client,
7365 .policy = nl80211_policy,
7366 .flags = GENL_ADMIN_PERM,
2b5f8b0b 7367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7f6cf311
JB
7368 NL80211_FLAG_NEED_RTNL,
7369 },
5e760230
JB
7370 {
7371 .cmd = NL80211_CMD_REGISTER_BEACONS,
7372 .doit = nl80211_register_beacons,
7373 .policy = nl80211_policy,
7374 .flags = GENL_ADMIN_PERM,
7375 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7376 NL80211_FLAG_NEED_RTNL,
7377 },
1d9d9213
SW
7378 {
7379 .cmd = NL80211_CMD_SET_NOACK_MAP,
7380 .doit = nl80211_set_noack_map,
7381 .policy = nl80211_policy,
7382 .flags = GENL_ADMIN_PERM,
7383 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7384 NL80211_FLAG_NEED_RTNL,
7385 },
7386
55682965 7387};
9588bbd5 7388
6039f6d2
JM
7389static struct genl_multicast_group nl80211_mlme_mcgrp = {
7390 .name = "mlme",
7391};
55682965
JB
7392
7393/* multicast groups */
7394static struct genl_multicast_group nl80211_config_mcgrp = {
7395 .name = "config",
7396};
2a519311
JB
7397static struct genl_multicast_group nl80211_scan_mcgrp = {
7398 .name = "scan",
7399};
73d54c9e
LR
7400static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7401 .name = "regulatory",
7402};
55682965
JB
7403
7404/* notification functions */
7405
7406void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7407{
7408 struct sk_buff *msg;
7409
fd2120ca 7410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
55682965
JB
7411 if (!msg)
7412 return;
7413
7414 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7415 nlmsg_free(msg);
7416 return;
7417 }
7418
463d0183
JB
7419 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7420 nl80211_config_mcgrp.id, GFP_KERNEL);
55682965
JB
7421}
7422
362a415d
JB
7423static int nl80211_add_scan_req(struct sk_buff *msg,
7424 struct cfg80211_registered_device *rdev)
7425{
7426 struct cfg80211_scan_request *req = rdev->scan_req;
7427 struct nlattr *nest;
7428 int i;
7429
667503dd
JB
7430 ASSERT_RDEV_LOCK(rdev);
7431
362a415d
JB
7432 if (WARN_ON(!req))
7433 return 0;
7434
7435 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7436 if (!nest)
7437 goto nla_put_failure;
9360ffd1
DM
7438 for (i = 0; i < req->n_ssids; i++) {
7439 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7440 goto nla_put_failure;
7441 }
362a415d
JB
7442 nla_nest_end(msg, nest);
7443
7444 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7445 if (!nest)
7446 goto nla_put_failure;
9360ffd1
DM
7447 for (i = 0; i < req->n_channels; i++) {
7448 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7449 goto nla_put_failure;
7450 }
362a415d
JB
7451 nla_nest_end(msg, nest);
7452
9360ffd1
DM
7453 if (req->ie &&
7454 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7455 goto nla_put_failure;
362a415d
JB
7456
7457 return 0;
7458 nla_put_failure:
7459 return -ENOBUFS;
7460}
7461
a538e2d5
JB
7462static int nl80211_send_scan_msg(struct sk_buff *msg,
7463 struct cfg80211_registered_device *rdev,
fd014284 7464 struct wireless_dev *wdev,
a538e2d5
JB
7465 u32 pid, u32 seq, int flags,
7466 u32 cmd)
2a519311
JB
7467{
7468 void *hdr;
7469
7470 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7471 if (!hdr)
7472 return -1;
7473
9360ffd1 7474 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
fd014284
JB
7475 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7476 wdev->netdev->ifindex)) ||
7477 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
9360ffd1 7478 goto nla_put_failure;
2a519311 7479
362a415d
JB
7480 /* ignore errors and send incomplete event anyway */
7481 nl80211_add_scan_req(msg, rdev);
2a519311
JB
7482
7483 return genlmsg_end(msg, hdr);
7484
7485 nla_put_failure:
7486 genlmsg_cancel(msg, hdr);
7487 return -EMSGSIZE;
7488}
7489
807f8a8c
LC
7490static int
7491nl80211_send_sched_scan_msg(struct sk_buff *msg,
7492 struct cfg80211_registered_device *rdev,
7493 struct net_device *netdev,
7494 u32 pid, u32 seq, int flags, u32 cmd)
7495{
7496 void *hdr;
7497
7498 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7499 if (!hdr)
7500 return -1;
7501
9360ffd1
DM
7502 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7503 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7504 goto nla_put_failure;
807f8a8c
LC
7505
7506 return genlmsg_end(msg, hdr);
7507
7508 nla_put_failure:
7509 genlmsg_cancel(msg, hdr);
7510 return -EMSGSIZE;
7511}
7512
a538e2d5 7513void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
fd014284 7514 struct wireless_dev *wdev)
a538e2d5
JB
7515{
7516 struct sk_buff *msg;
7517
7518 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7519 if (!msg)
7520 return;
7521
fd014284 7522 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5
JB
7523 NL80211_CMD_TRIGGER_SCAN) < 0) {
7524 nlmsg_free(msg);
7525 return;
7526 }
7527
463d0183
JB
7528 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7529 nl80211_scan_mcgrp.id, GFP_KERNEL);
a538e2d5
JB
7530}
7531
2a519311 7532void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
fd014284 7533 struct wireless_dev *wdev)
2a519311
JB
7534{
7535 struct sk_buff *msg;
7536
fd2120ca 7537 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2a519311
JB
7538 if (!msg)
7539 return;
7540
fd014284 7541 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5 7542 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
2a519311
JB
7543 nlmsg_free(msg);
7544 return;
7545 }
7546
463d0183
JB
7547 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7548 nl80211_scan_mcgrp.id, GFP_KERNEL);
2a519311
JB
7549}
7550
7551void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
fd014284 7552 struct wireless_dev *wdev)
2a519311
JB
7553{
7554 struct sk_buff *msg;
7555
fd2120ca 7556 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2a519311
JB
7557 if (!msg)
7558 return;
7559
fd014284 7560 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
a538e2d5 7561 NL80211_CMD_SCAN_ABORTED) < 0) {
2a519311
JB
7562 nlmsg_free(msg);
7563 return;
7564 }
7565
463d0183
JB
7566 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7567 nl80211_scan_mcgrp.id, GFP_KERNEL);
2a519311
JB
7568}
7569
807f8a8c
LC
7570void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7571 struct net_device *netdev)
7572{
7573 struct sk_buff *msg;
7574
7575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7576 if (!msg)
7577 return;
7578
7579 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7580 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7581 nlmsg_free(msg);
7582 return;
7583 }
7584
7585 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7586 nl80211_scan_mcgrp.id, GFP_KERNEL);
7587}
7588
7589void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7590 struct net_device *netdev, u32 cmd)
7591{
7592 struct sk_buff *msg;
7593
7594 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7595 if (!msg)
7596 return;
7597
7598 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7599 nlmsg_free(msg);
7600 return;
7601 }
7602
7603 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7604 nl80211_scan_mcgrp.id, GFP_KERNEL);
7605}
7606
73d54c9e
LR
7607/*
7608 * This can happen on global regulatory changes or device specific settings
7609 * based on custom world regulatory domains.
7610 */
7611void nl80211_send_reg_change_event(struct regulatory_request *request)
7612{
7613 struct sk_buff *msg;
7614 void *hdr;
7615
fd2120ca 7616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
73d54c9e
LR
7617 if (!msg)
7618 return;
7619
7620 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7621 if (!hdr) {
7622 nlmsg_free(msg);
7623 return;
7624 }
7625
7626 /* Userspace can always count this one always being set */
9360ffd1
DM
7627 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7628 goto nla_put_failure;
7629
7630 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7631 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7632 NL80211_REGDOM_TYPE_WORLD))
7633 goto nla_put_failure;
7634 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7635 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7636 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7637 goto nla_put_failure;
7638 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7639 request->intersect) {
7640 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7641 NL80211_REGDOM_TYPE_INTERSECTION))
7642 goto nla_put_failure;
7643 } else {
7644 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7645 NL80211_REGDOM_TYPE_COUNTRY) ||
7646 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7647 request->alpha2))
7648 goto nla_put_failure;
7649 }
7650
7651 if (wiphy_idx_valid(request->wiphy_idx) &&
7652 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7653 goto nla_put_failure;
73d54c9e 7654
3b7b72ee 7655 genlmsg_end(msg, hdr);
73d54c9e 7656
bc43b28c 7657 rcu_read_lock();
463d0183 7658 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
bc43b28c
JB
7659 GFP_ATOMIC);
7660 rcu_read_unlock();
73d54c9e
LR
7661
7662 return;
7663
7664nla_put_failure:
7665 genlmsg_cancel(msg, hdr);
7666 nlmsg_free(msg);
7667}
7668
6039f6d2
JM
7669static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7670 struct net_device *netdev,
7671 const u8 *buf, size_t len,
e6d6e342 7672 enum nl80211_commands cmd, gfp_t gfp)
6039f6d2
JM
7673{
7674 struct sk_buff *msg;
7675 void *hdr;
7676
e6d6e342 7677 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6039f6d2
JM
7678 if (!msg)
7679 return;
7680
7681 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7682 if (!hdr) {
7683 nlmsg_free(msg);
7684 return;
7685 }
7686
9360ffd1
DM
7687 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7688 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7689 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7690 goto nla_put_failure;
6039f6d2 7691
3b7b72ee 7692 genlmsg_end(msg, hdr);
6039f6d2 7693
463d0183
JB
7694 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7695 nl80211_mlme_mcgrp.id, gfp);
6039f6d2
JM
7696 return;
7697
7698 nla_put_failure:
7699 genlmsg_cancel(msg, hdr);
7700 nlmsg_free(msg);
7701}
7702
7703void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
e6d6e342
JB
7704 struct net_device *netdev, const u8 *buf,
7705 size_t len, gfp_t gfp)
6039f6d2
JM
7706{
7707 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 7708 NL80211_CMD_AUTHENTICATE, gfp);
6039f6d2
JM
7709}
7710
7711void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7712 struct net_device *netdev, const u8 *buf,
e6d6e342 7713 size_t len, gfp_t gfp)
6039f6d2 7714{
e6d6e342
JB
7715 nl80211_send_mlme_event(rdev, netdev, buf, len,
7716 NL80211_CMD_ASSOCIATE, gfp);
6039f6d2
JM
7717}
7718
53b46b84 7719void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
e6d6e342
JB
7720 struct net_device *netdev, const u8 *buf,
7721 size_t len, gfp_t gfp)
6039f6d2
JM
7722{
7723 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 7724 NL80211_CMD_DEAUTHENTICATE, gfp);
6039f6d2
JM
7725}
7726
53b46b84
JM
7727void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7728 struct net_device *netdev, const u8 *buf,
e6d6e342 7729 size_t len, gfp_t gfp)
6039f6d2
JM
7730{
7731 nl80211_send_mlme_event(rdev, netdev, buf, len,
e6d6e342 7732 NL80211_CMD_DISASSOCIATE, gfp);
6039f6d2
JM
7733}
7734
cf4e594e
JM
7735void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7736 struct net_device *netdev, const u8 *buf,
7737 size_t len, gfp_t gfp)
7738{
7739 nl80211_send_mlme_event(rdev, netdev, buf, len,
7740 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7741}
7742
7743void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7744 struct net_device *netdev, const u8 *buf,
7745 size_t len, gfp_t gfp)
7746{
7747 nl80211_send_mlme_event(rdev, netdev, buf, len,
7748 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7749}
7750
1b06bb40
LR
7751static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7752 struct net_device *netdev, int cmd,
e6d6e342 7753 const u8 *addr, gfp_t gfp)
1965c853
JM
7754{
7755 struct sk_buff *msg;
7756 void *hdr;
7757
e6d6e342 7758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
1965c853
JM
7759 if (!msg)
7760 return;
7761
7762 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7763 if (!hdr) {
7764 nlmsg_free(msg);
7765 return;
7766 }
7767
9360ffd1
DM
7768 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7769 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7770 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7771 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7772 goto nla_put_failure;
1965c853 7773
3b7b72ee 7774 genlmsg_end(msg, hdr);
1965c853 7775
463d0183
JB
7776 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7777 nl80211_mlme_mcgrp.id, gfp);
1965c853
JM
7778 return;
7779
7780 nla_put_failure:
7781 genlmsg_cancel(msg, hdr);
7782 nlmsg_free(msg);
7783}
7784
7785void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
e6d6e342
JB
7786 struct net_device *netdev, const u8 *addr,
7787 gfp_t gfp)
1965c853
JM
7788{
7789 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
e6d6e342 7790 addr, gfp);
1965c853
JM
7791}
7792
7793void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
e6d6e342
JB
7794 struct net_device *netdev, const u8 *addr,
7795 gfp_t gfp)
1965c853 7796{
e6d6e342
JB
7797 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7798 addr, gfp);
1965c853
JM
7799}
7800
b23aa676
SO
7801void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7802 struct net_device *netdev, const u8 *bssid,
7803 const u8 *req_ie, size_t req_ie_len,
7804 const u8 *resp_ie, size_t resp_ie_len,
7805 u16 status, gfp_t gfp)
7806{
7807 struct sk_buff *msg;
7808 void *hdr;
7809
7810 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7811 if (!msg)
7812 return;
7813
7814 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7815 if (!hdr) {
7816 nlmsg_free(msg);
7817 return;
7818 }
7819
9360ffd1
DM
7820 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7821 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7822 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7823 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7824 (req_ie &&
7825 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7826 (resp_ie &&
7827 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7828 goto nla_put_failure;
b23aa676 7829
3b7b72ee 7830 genlmsg_end(msg, hdr);
b23aa676 7831
463d0183
JB
7832 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7833 nl80211_mlme_mcgrp.id, gfp);
b23aa676
SO
7834 return;
7835
7836 nla_put_failure:
7837 genlmsg_cancel(msg, hdr);
7838 nlmsg_free(msg);
7839
7840}
7841
7842void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7843 struct net_device *netdev, const u8 *bssid,
7844 const u8 *req_ie, size_t req_ie_len,
7845 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7846{
7847 struct sk_buff *msg;
7848 void *hdr;
7849
7850 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7851 if (!msg)
7852 return;
7853
7854 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7855 if (!hdr) {
7856 nlmsg_free(msg);
7857 return;
7858 }
7859
9360ffd1
DM
7860 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7861 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7862 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7863 (req_ie &&
7864 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7865 (resp_ie &&
7866 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7867 goto nla_put_failure;
b23aa676 7868
3b7b72ee 7869 genlmsg_end(msg, hdr);
b23aa676 7870
463d0183
JB
7871 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7872 nl80211_mlme_mcgrp.id, gfp);
b23aa676
SO
7873 return;
7874
7875 nla_put_failure:
7876 genlmsg_cancel(msg, hdr);
7877 nlmsg_free(msg);
7878
7879}
7880
7881void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7882 struct net_device *netdev, u16 reason,
667503dd 7883 const u8 *ie, size_t ie_len, bool from_ap)
b23aa676
SO
7884{
7885 struct sk_buff *msg;
7886 void *hdr;
7887
667503dd 7888 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
b23aa676
SO
7889 if (!msg)
7890 return;
7891
7892 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7893 if (!hdr) {
7894 nlmsg_free(msg);
7895 return;
7896 }
7897
9360ffd1
DM
7898 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7899 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7900 (from_ap && reason &&
7901 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7902 (from_ap &&
7903 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7904 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7905 goto nla_put_failure;
b23aa676 7906
3b7b72ee 7907 genlmsg_end(msg, hdr);
b23aa676 7908
463d0183
JB
7909 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7910 nl80211_mlme_mcgrp.id, GFP_KERNEL);
b23aa676
SO
7911 return;
7912
7913 nla_put_failure:
7914 genlmsg_cancel(msg, hdr);
7915 nlmsg_free(msg);
7916
7917}
7918
04a773ad
JB
7919void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7920 struct net_device *netdev, const u8 *bssid,
7921 gfp_t gfp)
7922{
7923 struct sk_buff *msg;
7924 void *hdr;
7925
fd2120ca 7926 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
04a773ad
JB
7927 if (!msg)
7928 return;
7929
7930 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7931 if (!hdr) {
7932 nlmsg_free(msg);
7933 return;
7934 }
7935
9360ffd1
DM
7936 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7937 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7938 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7939 goto nla_put_failure;
04a773ad 7940
3b7b72ee 7941 genlmsg_end(msg, hdr);
04a773ad 7942
463d0183
JB
7943 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7944 nl80211_mlme_mcgrp.id, gfp);
04a773ad
JB
7945 return;
7946
7947 nla_put_failure:
7948 genlmsg_cancel(msg, hdr);
7949 nlmsg_free(msg);
7950}
7951
c93b5e71
JC
7952void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7953 struct net_device *netdev,
7954 const u8 *macaddr, const u8* ie, u8 ie_len,
7955 gfp_t gfp)
7956{
7957 struct sk_buff *msg;
7958 void *hdr;
7959
7960 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7961 if (!msg)
7962 return;
7963
7964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7965 if (!hdr) {
7966 nlmsg_free(msg);
7967 return;
7968 }
7969
9360ffd1
DM
7970 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7971 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7972 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7973 (ie_len && ie &&
7974 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7975 goto nla_put_failure;
c93b5e71 7976
3b7b72ee 7977 genlmsg_end(msg, hdr);
c93b5e71
JC
7978
7979 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7980 nl80211_mlme_mcgrp.id, gfp);
7981 return;
7982
7983 nla_put_failure:
7984 genlmsg_cancel(msg, hdr);
7985 nlmsg_free(msg);
7986}
7987
a3b8b056
JM
7988void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7989 struct net_device *netdev, const u8 *addr,
7990 enum nl80211_key_type key_type, int key_id,
e6d6e342 7991 const u8 *tsc, gfp_t gfp)
a3b8b056
JM
7992{
7993 struct sk_buff *msg;
7994 void *hdr;
7995
e6d6e342 7996 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
a3b8b056
JM
7997 if (!msg)
7998 return;
7999
8000 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8001 if (!hdr) {
8002 nlmsg_free(msg);
8003 return;
8004 }
8005
9360ffd1
DM
8006 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8007 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8008 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8009 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8010 (key_id != -1 &&
8011 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8012 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8013 goto nla_put_failure;
a3b8b056 8014
3b7b72ee 8015 genlmsg_end(msg, hdr);
a3b8b056 8016
463d0183
JB
8017 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8018 nl80211_mlme_mcgrp.id, gfp);
a3b8b056
JM
8019 return;
8020
8021 nla_put_failure:
8022 genlmsg_cancel(msg, hdr);
8023 nlmsg_free(msg);
8024}
8025
6bad8766
LR
8026void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8027 struct ieee80211_channel *channel_before,
8028 struct ieee80211_channel *channel_after)
8029{
8030 struct sk_buff *msg;
8031 void *hdr;
8032 struct nlattr *nl_freq;
8033
fd2120ca 8034 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
6bad8766
LR
8035 if (!msg)
8036 return;
8037
8038 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8039 if (!hdr) {
8040 nlmsg_free(msg);
8041 return;
8042 }
8043
8044 /*
8045 * Since we are applying the beacon hint to a wiphy we know its
8046 * wiphy_idx is valid
8047 */
9360ffd1
DM
8048 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8049 goto nla_put_failure;
6bad8766
LR
8050
8051 /* Before */
8052 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8053 if (!nl_freq)
8054 goto nla_put_failure;
8055 if (nl80211_msg_put_channel(msg, channel_before))
8056 goto nla_put_failure;
8057 nla_nest_end(msg, nl_freq);
8058
8059 /* After */
8060 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8061 if (!nl_freq)
8062 goto nla_put_failure;
8063 if (nl80211_msg_put_channel(msg, channel_after))
8064 goto nla_put_failure;
8065 nla_nest_end(msg, nl_freq);
8066
3b7b72ee 8067 genlmsg_end(msg, hdr);
6bad8766 8068
463d0183
JB
8069 rcu_read_lock();
8070 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8071 GFP_ATOMIC);
8072 rcu_read_unlock();
6bad8766
LR
8073
8074 return;
8075
8076nla_put_failure:
8077 genlmsg_cancel(msg, hdr);
8078 nlmsg_free(msg);
8079}
8080
9588bbd5
JM
8081static void nl80211_send_remain_on_chan_event(
8082 int cmd, struct cfg80211_registered_device *rdev,
71bbc994 8083 struct wireless_dev *wdev, u64 cookie,
9588bbd5
JM
8084 struct ieee80211_channel *chan,
8085 enum nl80211_channel_type channel_type,
8086 unsigned int duration, gfp_t gfp)
8087{
8088 struct sk_buff *msg;
8089 void *hdr;
8090
8091 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8092 if (!msg)
8093 return;
8094
8095 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8096 if (!hdr) {
8097 nlmsg_free(msg);
8098 return;
8099 }
8100
9360ffd1 8101 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
8102 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8103 wdev->netdev->ifindex)) ||
8104 nla_put_u32(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
9360ffd1
DM
8105 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8106 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8107 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8108 goto nla_put_failure;
9588bbd5 8109
9360ffd1
DM
8110 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8111 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8112 goto nla_put_failure;
9588bbd5 8113
3b7b72ee 8114 genlmsg_end(msg, hdr);
9588bbd5
JM
8115
8116 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8117 nl80211_mlme_mcgrp.id, gfp);
8118 return;
8119
8120 nla_put_failure:
8121 genlmsg_cancel(msg, hdr);
8122 nlmsg_free(msg);
8123}
8124
8125void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
71bbc994 8126 struct wireless_dev *wdev, u64 cookie,
9588bbd5
JM
8127 struct ieee80211_channel *chan,
8128 enum nl80211_channel_type channel_type,
8129 unsigned int duration, gfp_t gfp)
8130{
8131 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
71bbc994 8132 rdev, wdev, cookie, chan,
9588bbd5
JM
8133 channel_type, duration, gfp);
8134}
8135
8136void nl80211_send_remain_on_channel_cancel(
71bbc994
JB
8137 struct cfg80211_registered_device *rdev,
8138 struct wireless_dev *wdev,
9588bbd5
JM
8139 u64 cookie, struct ieee80211_channel *chan,
8140 enum nl80211_channel_type channel_type, gfp_t gfp)
8141{
8142 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
71bbc994 8143 rdev, wdev, cookie, chan,
9588bbd5
JM
8144 channel_type, 0, gfp);
8145}
8146
98b62183
JB
8147void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8148 struct net_device *dev, const u8 *mac_addr,
8149 struct station_info *sinfo, gfp_t gfp)
8150{
8151 struct sk_buff *msg;
8152
8153 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8154 if (!msg)
8155 return;
8156
66266b3a
JL
8157 if (nl80211_send_station(msg, 0, 0, 0,
8158 rdev, dev, mac_addr, sinfo) < 0) {
98b62183
JB
8159 nlmsg_free(msg);
8160 return;
8161 }
8162
8163 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8164 nl80211_mlme_mcgrp.id, gfp);
8165}
8166
ec15e68b
JM
8167void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8168 struct net_device *dev, const u8 *mac_addr,
8169 gfp_t gfp)
8170{
8171 struct sk_buff *msg;
8172 void *hdr;
8173
8174 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8175 if (!msg)
8176 return;
8177
8178 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8179 if (!hdr) {
8180 nlmsg_free(msg);
8181 return;
8182 }
8183
9360ffd1
DM
8184 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8185 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8186 goto nla_put_failure;
ec15e68b 8187
3b7b72ee 8188 genlmsg_end(msg, hdr);
ec15e68b
JM
8189
8190 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8191 nl80211_mlme_mcgrp.id, gfp);
8192 return;
8193
8194 nla_put_failure:
8195 genlmsg_cancel(msg, hdr);
8196 nlmsg_free(msg);
8197}
8198
b92ab5d8
JB
8199static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8200 const u8 *addr, gfp_t gfp)
28946da7
JB
8201{
8202 struct wireless_dev *wdev = dev->ieee80211_ptr;
8203 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8204 struct sk_buff *msg;
8205 void *hdr;
8206 int err;
8207 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8208
8209 if (!nlpid)
8210 return false;
8211
8212 msg = nlmsg_new(100, gfp);
8213 if (!msg)
8214 return true;
8215
b92ab5d8 8216 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
28946da7
JB
8217 if (!hdr) {
8218 nlmsg_free(msg);
8219 return true;
8220 }
8221
9360ffd1
DM
8222 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8223 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8224 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8225 goto nla_put_failure;
28946da7
JB
8226
8227 err = genlmsg_end(msg, hdr);
8228 if (err < 0) {
8229 nlmsg_free(msg);
8230 return true;
8231 }
8232
8233 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8234 return true;
8235
8236 nla_put_failure:
8237 genlmsg_cancel(msg, hdr);
8238 nlmsg_free(msg);
8239 return true;
8240}
8241
b92ab5d8
JB
8242bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8243{
8244 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8245 addr, gfp);
8246}
8247
8248bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8249 const u8 *addr, gfp_t gfp)
8250{
8251 return __nl80211_unexpected_frame(dev,
8252 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8253 addr, gfp);
8254}
8255
2e161f78 8256int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
71bbc994 8257 struct wireless_dev *wdev, u32 nlpid,
804483e9
JB
8258 int freq, int sig_dbm,
8259 const u8 *buf, size_t len, gfp_t gfp)
026331c4 8260{
71bbc994 8261 struct net_device *netdev = wdev->netdev;
026331c4
JM
8262 struct sk_buff *msg;
8263 void *hdr;
026331c4
JM
8264
8265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8266 if (!msg)
8267 return -ENOMEM;
8268
2e161f78 8269 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
026331c4
JM
8270 if (!hdr) {
8271 nlmsg_free(msg);
8272 return -ENOMEM;
8273 }
8274
9360ffd1 8275 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
8276 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8277 netdev->ifindex)) ||
9360ffd1
DM
8278 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8279 (sig_dbm &&
8280 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8281 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8282 goto nla_put_failure;
026331c4 8283
3b7b72ee 8284 genlmsg_end(msg, hdr);
026331c4 8285
3b7b72ee 8286 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
026331c4
JM
8287
8288 nla_put_failure:
8289 genlmsg_cancel(msg, hdr);
8290 nlmsg_free(msg);
8291 return -ENOBUFS;
8292}
8293
2e161f78 8294void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
71bbc994 8295 struct wireless_dev *wdev, u64 cookie,
2e161f78
JB
8296 const u8 *buf, size_t len, bool ack,
8297 gfp_t gfp)
026331c4 8298{
71bbc994 8299 struct net_device *netdev = wdev->netdev;
026331c4
JM
8300 struct sk_buff *msg;
8301 void *hdr;
8302
8303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8304 if (!msg)
8305 return;
8306
2e161f78 8307 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
026331c4
JM
8308 if (!hdr) {
8309 nlmsg_free(msg);
8310 return;
8311 }
8312
9360ffd1 8313 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
71bbc994
JB
8314 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8315 netdev->ifindex)) ||
9360ffd1
DM
8316 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8317 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8318 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8319 goto nla_put_failure;
026331c4 8320
3b7b72ee 8321 genlmsg_end(msg, hdr);
026331c4
JM
8322
8323 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8324 return;
8325
8326 nla_put_failure:
8327 genlmsg_cancel(msg, hdr);
8328 nlmsg_free(msg);
8329}
8330
d6dc1a38
JO
8331void
8332nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8333 struct net_device *netdev,
8334 enum nl80211_cqm_rssi_threshold_event rssi_event,
8335 gfp_t gfp)
8336{
8337 struct sk_buff *msg;
8338 struct nlattr *pinfoattr;
8339 void *hdr;
8340
8341 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8342 if (!msg)
8343 return;
8344
8345 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8346 if (!hdr) {
8347 nlmsg_free(msg);
8348 return;
8349 }
8350
9360ffd1
DM
8351 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8352 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8353 goto nla_put_failure;
d6dc1a38
JO
8354
8355 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8356 if (!pinfoattr)
8357 goto nla_put_failure;
8358
9360ffd1
DM
8359 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8360 rssi_event))
8361 goto nla_put_failure;
d6dc1a38
JO
8362
8363 nla_nest_end(msg, pinfoattr);
8364
3b7b72ee 8365 genlmsg_end(msg, hdr);
d6dc1a38
JO
8366
8367 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8368 nl80211_mlme_mcgrp.id, gfp);
8369 return;
8370
8371 nla_put_failure:
8372 genlmsg_cancel(msg, hdr);
8373 nlmsg_free(msg);
8374}
8375
e5497d76
JB
8376void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8377 struct net_device *netdev, const u8 *bssid,
8378 const u8 *replay_ctr, gfp_t gfp)
8379{
8380 struct sk_buff *msg;
8381 struct nlattr *rekey_attr;
8382 void *hdr;
8383
8384 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8385 if (!msg)
8386 return;
8387
8388 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8389 if (!hdr) {
8390 nlmsg_free(msg);
8391 return;
8392 }
8393
9360ffd1
DM
8394 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8395 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8396 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8397 goto nla_put_failure;
e5497d76
JB
8398
8399 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8400 if (!rekey_attr)
8401 goto nla_put_failure;
8402
9360ffd1
DM
8403 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8404 NL80211_REPLAY_CTR_LEN, replay_ctr))
8405 goto nla_put_failure;
e5497d76
JB
8406
8407 nla_nest_end(msg, rekey_attr);
8408
3b7b72ee 8409 genlmsg_end(msg, hdr);
e5497d76
JB
8410
8411 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8412 nl80211_mlme_mcgrp.id, gfp);
8413 return;
8414
8415 nla_put_failure:
8416 genlmsg_cancel(msg, hdr);
8417 nlmsg_free(msg);
8418}
8419
c9df56b4
JM
8420void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8421 struct net_device *netdev, int index,
8422 const u8 *bssid, bool preauth, gfp_t gfp)
8423{
8424 struct sk_buff *msg;
8425 struct nlattr *attr;
8426 void *hdr;
8427
8428 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8429 if (!msg)
8430 return;
8431
8432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8433 if (!hdr) {
8434 nlmsg_free(msg);
8435 return;
8436 }
8437
9360ffd1
DM
8438 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8439 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8440 goto nla_put_failure;
c9df56b4
JM
8441
8442 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8443 if (!attr)
8444 goto nla_put_failure;
8445
9360ffd1
DM
8446 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8447 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8448 (preauth &&
8449 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8450 goto nla_put_failure;
c9df56b4
JM
8451
8452 nla_nest_end(msg, attr);
8453
3b7b72ee 8454 genlmsg_end(msg, hdr);
c9df56b4
JM
8455
8456 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8457 nl80211_mlme_mcgrp.id, gfp);
8458 return;
8459
8460 nla_put_failure:
8461 genlmsg_cancel(msg, hdr);
8462 nlmsg_free(msg);
8463}
8464
5314526b
TP
8465void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8466 struct net_device *netdev, int freq,
8467 enum nl80211_channel_type type, gfp_t gfp)
8468{
8469 struct sk_buff *msg;
8470 void *hdr;
8471
8472 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8473 if (!msg)
8474 return;
8475
8476 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8477 if (!hdr) {
8478 nlmsg_free(msg);
8479 return;
8480 }
8481
7eab0f64
JL
8482 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8483 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8484 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8485 goto nla_put_failure;
5314526b
TP
8486
8487 genlmsg_end(msg, hdr);
8488
8489 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8490 nl80211_mlme_mcgrp.id, gfp);
8491 return;
8492
8493 nla_put_failure:
8494 genlmsg_cancel(msg, hdr);
8495 nlmsg_free(msg);
8496}
8497
c063dbf5
JB
8498void
8499nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8500 struct net_device *netdev, const u8 *peer,
8501 u32 num_packets, gfp_t gfp)
8502{
8503 struct sk_buff *msg;
8504 struct nlattr *pinfoattr;
8505 void *hdr;
8506
8507 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8508 if (!msg)
8509 return;
8510
8511 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8512 if (!hdr) {
8513 nlmsg_free(msg);
8514 return;
8515 }
8516
9360ffd1
DM
8517 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8518 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8519 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8520 goto nla_put_failure;
c063dbf5
JB
8521
8522 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8523 if (!pinfoattr)
8524 goto nla_put_failure;
8525
9360ffd1
DM
8526 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8527 goto nla_put_failure;
c063dbf5
JB
8528
8529 nla_nest_end(msg, pinfoattr);
8530
3b7b72ee 8531 genlmsg_end(msg, hdr);
c063dbf5
JB
8532
8533 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8534 nl80211_mlme_mcgrp.id, gfp);
8535 return;
8536
8537 nla_put_failure:
8538 genlmsg_cancel(msg, hdr);
8539 nlmsg_free(msg);
8540}
8541
7f6cf311
JB
8542void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8543 u64 cookie, bool acked, gfp_t gfp)
8544{
8545 struct wireless_dev *wdev = dev->ieee80211_ptr;
8546 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8547 struct sk_buff *msg;
8548 void *hdr;
8549 int err;
8550
8551 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8552 if (!msg)
8553 return;
8554
8555 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8556 if (!hdr) {
8557 nlmsg_free(msg);
8558 return;
8559 }
8560
9360ffd1
DM
8561 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8562 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8563 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8564 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8565 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8566 goto nla_put_failure;
7f6cf311
JB
8567
8568 err = genlmsg_end(msg, hdr);
8569 if (err < 0) {
8570 nlmsg_free(msg);
8571 return;
8572 }
8573
8574 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8575 nl80211_mlme_mcgrp.id, gfp);
8576 return;
8577
8578 nla_put_failure:
8579 genlmsg_cancel(msg, hdr);
8580 nlmsg_free(msg);
8581}
8582EXPORT_SYMBOL(cfg80211_probe_status);
8583
5e760230
JB
8584void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8585 const u8 *frame, size_t len,
804483e9 8586 int freq, int sig_dbm, gfp_t gfp)
5e760230
JB
8587{
8588 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8589 struct sk_buff *msg;
8590 void *hdr;
8591 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8592
8593 if (!nlpid)
8594 return;
8595
8596 msg = nlmsg_new(len + 100, gfp);
8597 if (!msg)
8598 return;
8599
8600 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8601 if (!hdr) {
8602 nlmsg_free(msg);
8603 return;
8604 }
8605
9360ffd1
DM
8606 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8607 (freq &&
8608 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8609 (sig_dbm &&
8610 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8611 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8612 goto nla_put_failure;
5e760230
JB
8613
8614 genlmsg_end(msg, hdr);
8615
8616 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8617 return;
8618
8619 nla_put_failure:
8620 genlmsg_cancel(msg, hdr);
8621 nlmsg_free(msg);
8622}
8623EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8624
026331c4
JM
8625static int nl80211_netlink_notify(struct notifier_block * nb,
8626 unsigned long state,
8627 void *_notify)
8628{
8629 struct netlink_notify *notify = _notify;
8630 struct cfg80211_registered_device *rdev;
8631 struct wireless_dev *wdev;
8632
8633 if (state != NETLINK_URELEASE)
8634 return NOTIFY_DONE;
8635
8636 rcu_read_lock();
8637
5e760230 8638 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
89a54e48 8639 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
2e161f78 8640 cfg80211_mlme_unregister_socket(wdev, notify->pid);
5e760230
JB
8641 if (rdev->ap_beacons_nlpid == notify->pid)
8642 rdev->ap_beacons_nlpid = 0;
8643 }
026331c4
JM
8644
8645 rcu_read_unlock();
8646
8647 return NOTIFY_DONE;
8648}
8649
8650static struct notifier_block nl80211_netlink_notifier = {
8651 .notifier_call = nl80211_netlink_notify,
8652};
8653
55682965
JB
8654/* initialisation/exit functions */
8655
8656int nl80211_init(void)
8657{
0d63cbb5 8658 int err;
55682965 8659
0d63cbb5
MM
8660 err = genl_register_family_with_ops(&nl80211_fam,
8661 nl80211_ops, ARRAY_SIZE(nl80211_ops));
55682965
JB
8662 if (err)
8663 return err;
8664
55682965
JB
8665 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8666 if (err)
8667 goto err_out;
8668
2a519311
JB
8669 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8670 if (err)
8671 goto err_out;
8672
73d54c9e
LR
8673 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8674 if (err)
8675 goto err_out;
8676
6039f6d2
JM
8677 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8678 if (err)
8679 goto err_out;
8680
aff89a9b
JB
8681#ifdef CONFIG_NL80211_TESTMODE
8682 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8683 if (err)
8684 goto err_out;
8685#endif
8686
026331c4
JM
8687 err = netlink_register_notifier(&nl80211_netlink_notifier);
8688 if (err)
8689 goto err_out;
8690
55682965
JB
8691 return 0;
8692 err_out:
8693 genl_unregister_family(&nl80211_fam);
8694 return err;
8695}
8696
8697void nl80211_exit(void)
8698{
026331c4 8699 netlink_unregister_notifier(&nl80211_netlink_notifier);
55682965
JB
8700 genl_unregister_family(&nl80211_fam);
8701}
This page took 1.144379 seconds and 5 git commands to generate.