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