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