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