cfg80211: restructure AP/GO mode API
[linux-2.6.git] / net / wireless / nl80211.c
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  */
6
7 #include <linux/if.h>
8 #include <linux/module.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
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>
17 #include <linux/etherdevice.h>
18 #include <net/net_namespace.h>
19 #include <net/genetlink.h>
20 #include <net/cfg80211.h>
21 #include <net/sock.h>
22 #include "core.h"
23 #include "nl80211.h"
24 #include "reg.h"
25
26 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27 static 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
32 static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33                             struct genl_info *info);
34 static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35                               struct genl_info *info);
36
37 /* the netlink family */
38 static 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,
44         .netnsok = true,
45         .pre_doit = nl80211_pre_doit,
46         .post_doit = nl80211_post_doit,
47 };
48
49 /* internal helper: get rdev and dev */
50 static int get_rdev_dev_by_ifindex(struct net *netns, struct nlattr **attrs,
51                                    struct cfg80211_registered_device **rdev,
52                                    struct net_device **dev)
53 {
54         int ifindex;
55
56         if (!attrs[NL80211_ATTR_IFINDEX])
57                 return -EINVAL;
58
59         ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
60         *dev = dev_get_by_index(netns, ifindex);
61         if (!*dev)
62                 return -ENODEV;
63
64         *rdev = cfg80211_get_dev_from_ifindex(netns, ifindex);
65         if (IS_ERR(*rdev)) {
66                 dev_put(*dev);
67                 return PTR_ERR(*rdev);
68         }
69
70         return 0;
71 }
72
73 /* policy for the attributes */
74 static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
75         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
76         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
77                                       .len = 20-1 },
78         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
79         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
80         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
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 },
85         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
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 },
90
91         [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
92         [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
93
94         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
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 },
100         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
101         [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
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 },
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 },
114         [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
115         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
116         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
117         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
118                                 .len = IEEE80211_MAX_MESH_ID_LEN },
119         [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
120
121         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
122         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
123
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 },
127         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
128                                            .len = NL80211_MAX_SUPP_RATES },
129         [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
130
131         [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
132         [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
133
134         [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
135
136         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
137         [NL80211_ATTR_IE] = { .type = NLA_BINARY,
138                               .len = IEEE80211_MAX_DATA_LEN },
139         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
140         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
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 },
146         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
147         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
148         [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
149         [NL80211_ATTR_STA_FLAGS2] = {
150                 .len = sizeof(struct nl80211_sta_flag_update),
151         },
152         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
153         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
154         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
155         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
156         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
157         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
158         [NL80211_ATTR_PID] = { .type = NLA_U32 },
159         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
160         [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
161                                  .len = WLAN_PMKID_LEN },
162         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
163         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
164         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
165         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
166                                  .len = IEEE80211_MAX_DATA_LEN },
167         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
168         [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
169         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
170         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
171         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
172         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
173         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
174         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
175         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
176         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
177         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
178         [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
179         [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
180         [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
181         [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
182         [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
183         [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
184         [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
185         [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
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 },
190         [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
191         [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
192         [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
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 },
198         [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
199         [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
200                                       .len = IEEE80211_MAX_DATA_LEN },
201         [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
202         [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
203         [NL80211_ATTR_HT_CAPABILITY_MASK] = {
204                 .len = NL80211_HT_CAPABILITY_LEN
205         },
206         [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
207         [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
208         [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
209 };
210
211 /* policy for the key attributes */
212 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
213         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
214         [NL80211_KEY_IDX] = { .type = NLA_U8 },
215         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
216         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
217         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
218         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
219         [NL80211_KEY_TYPE] = { .type = NLA_U32 },
220         [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
221 };
222
223 /* policy for the key default flags */
224 static const struct nla_policy
225 nl80211_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 },
228 };
229
230 /* policy for WoWLAN attributes */
231 static const struct nla_policy
232 nl80211_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 },
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 },
241 };
242
243 /* policy for GTK rekey offload attributes */
244 static const struct nla_policy
245 nl80211_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
251 static const struct nla_policy
252 nl80211_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
257 /* ifidx get helper */
258 static 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
277 static 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);
301         if (IS_ERR(*rdev)) {
302                 err = PTR_ERR(*rdev);
303                 goto out_rtnl;
304         }
305
306         return 0;
307  out_rtnl:
308         rtnl_unlock();
309         return err;
310 }
311
312 static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
313 {
314         cfg80211_unlock_rdev(rdev);
315         rtnl_unlock();
316 }
317
318 /* IE validation */
319 static 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
348 /* message building helper */
349 static 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
356 static int nl80211_msg_put_channel(struct sk_buff *msg,
357                                    struct ieee80211_channel *chan)
358 {
359         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
360                     chan->center_freq);
361
362         if (chan->flags & IEEE80211_CHAN_DISABLED)
363                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
364         if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
365                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
366         if (chan->flags & IEEE80211_CHAN_NO_IBSS)
367                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
368         if (chan->flags & IEEE80211_CHAN_RADAR)
369                 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
370
371         NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
372                     DBM_TO_MBM(chan->max_power));
373
374         return 0;
375
376  nla_put_failure:
377         return -ENOBUFS;
378 }
379
380 /* netlink command implementations */
381
382 struct key_parse {
383         struct key_params p;
384         int idx;
385         int type;
386         bool def, defmgmt;
387         bool def_uni, def_multi;
388 };
389
390 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
391 {
392         struct nlattr *tb[NL80211_KEY_MAX + 1];
393         int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
394                                    nl80211_key_policy);
395         if (err)
396                 return err;
397
398         k->def = !!tb[NL80211_KEY_DEFAULT];
399         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
400
401         if (k->def) {
402                 k->def_uni = true;
403                 k->def_multi = true;
404         }
405         if (k->defmgmt)
406                 k->def_multi = true;
407
408         if (tb[NL80211_KEY_IDX])
409                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
410
411         if (tb[NL80211_KEY_DATA]) {
412                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
413                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
414         }
415
416         if (tb[NL80211_KEY_SEQ]) {
417                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
418                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
419         }
420
421         if (tb[NL80211_KEY_CIPHER])
422                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
423
424         if (tb[NL80211_KEY_TYPE]) {
425                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
426                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
427                         return -EINVAL;
428         }
429
430         if (tb[NL80211_KEY_DEFAULT_TYPES]) {
431                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
432                 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
433                                        tb[NL80211_KEY_DEFAULT_TYPES],
434                                        nl80211_key_default_policy);
435                 if (err)
436                         return err;
437
438                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
439                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
440         }
441
442         return 0;
443 }
444
445 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
446 {
447         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
448                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
449                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
450         }
451
452         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
453                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
454                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
455         }
456
457         if (info->attrs[NL80211_ATTR_KEY_IDX])
458                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
459
460         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
461                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
462
463         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
464         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
465
466         if (k->def) {
467                 k->def_uni = true;
468                 k->def_multi = true;
469         }
470         if (k->defmgmt)
471                 k->def_multi = true;
472
473         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
474                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
475                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
476                         return -EINVAL;
477         }
478
479         if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
480                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
481                 int err = nla_parse_nested(
482                                 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
483                                 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
484                                 nl80211_key_default_policy);
485                 if (err)
486                         return err;
487
488                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
489                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
490         }
491
492         return 0;
493 }
494
495 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
496 {
497         int err;
498
499         memset(k, 0, sizeof(*k));
500         k->idx = -1;
501         k->type = -1;
502
503         if (info->attrs[NL80211_ATTR_KEY])
504                 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
505         else
506                 err = nl80211_parse_key_old(info, k);
507
508         if (err)
509                 return err;
510
511         if (k->def && k->defmgmt)
512                 return -EINVAL;
513
514         if (k->defmgmt) {
515                 if (k->def_uni || !k->def_multi)
516                         return -EINVAL;
517         }
518
519         if (k->idx != -1) {
520                 if (k->defmgmt) {
521                         if (k->idx < 4 || k->idx > 5)
522                                 return -EINVAL;
523                 } else if (k->def) {
524                         if (k->idx < 0 || k->idx > 3)
525                                 return -EINVAL;
526                 } else {
527                         if (k->idx < 0 || k->idx > 5)
528                                 return -EINVAL;
529                 }
530         }
531
532         return 0;
533 }
534
535 static struct cfg80211_cached_keys *
536 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
537                        struct nlattr *keys)
538 {
539         struct key_parse parse;
540         struct nlattr *key;
541         struct cfg80211_cached_keys *result;
542         int rem, err, def = 0;
543
544         result = kzalloc(sizeof(*result), GFP_KERNEL);
545         if (!result)
546                 return ERR_PTR(-ENOMEM);
547
548         result->def = -1;
549         result->defmgmt = -1;
550
551         nla_for_each_nested(key, keys, rem) {
552                 memset(&parse, 0, sizeof(parse));
553                 parse.idx = -1;
554
555                 err = nl80211_parse_key_new(key, &parse);
556                 if (err)
557                         goto error;
558                 err = -EINVAL;
559                 if (!parse.p.key)
560                         goto error;
561                 if (parse.idx < 0 || parse.idx > 4)
562                         goto error;
563                 if (parse.def) {
564                         if (def)
565                                 goto error;
566                         def = 1;
567                         result->def = parse.idx;
568                         if (!parse.def_uni || !parse.def_multi)
569                                 goto error;
570                 } else if (parse.defmgmt)
571                         goto error;
572                 err = cfg80211_validate_key_settings(rdev, &parse.p,
573                                                      parse.idx, false, NULL);
574                 if (err)
575                         goto error;
576                 result->params[parse.idx].cipher = parse.p.cipher;
577                 result->params[parse.idx].key_len = parse.p.key_len;
578                 result->params[parse.idx].key = result->data[parse.idx];
579                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
580         }
581
582         return result;
583  error:
584         kfree(result);
585         return ERR_PTR(err);
586 }
587
588 static int nl80211_key_allowed(struct wireless_dev *wdev)
589 {
590         ASSERT_WDEV_LOCK(wdev);
591
592         switch (wdev->iftype) {
593         case NL80211_IFTYPE_AP:
594         case NL80211_IFTYPE_AP_VLAN:
595         case NL80211_IFTYPE_P2P_GO:
596         case NL80211_IFTYPE_MESH_POINT:
597                 break;
598         case NL80211_IFTYPE_ADHOC:
599                 if (!wdev->current_bss)
600                         return -ENOLINK;
601                 break;
602         case NL80211_IFTYPE_STATION:
603         case NL80211_IFTYPE_P2P_CLIENT:
604                 if (wdev->sme_state != CFG80211_SME_CONNECTED)
605                         return -ENOLINK;
606                 break;
607         default:
608                 return -EINVAL;
609         }
610
611         return 0;
612 }
613
614 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
615 {
616         struct nlattr *nl_modes = nla_nest_start(msg, attr);
617         int i;
618
619         if (!nl_modes)
620                 goto nla_put_failure;
621
622         i = 0;
623         while (ifmodes) {
624                 if (ifmodes & 1)
625                         NLA_PUT_FLAG(msg, i);
626                 ifmodes >>= 1;
627                 i++;
628         }
629
630         nla_nest_end(msg, nl_modes);
631         return 0;
632
633 nla_put_failure:
634         return -ENOBUFS;
635 }
636
637 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
638                                           struct sk_buff *msg)
639 {
640         struct nlattr *nl_combis;
641         int i, j;
642
643         nl_combis = nla_nest_start(msg,
644                                 NL80211_ATTR_INTERFACE_COMBINATIONS);
645         if (!nl_combis)
646                 goto nla_put_failure;
647
648         for (i = 0; i < wiphy->n_iface_combinations; i++) {
649                 const struct ieee80211_iface_combination *c;
650                 struct nlattr *nl_combi, *nl_limits;
651
652                 c = &wiphy->iface_combinations[i];
653
654                 nl_combi = nla_nest_start(msg, i + 1);
655                 if (!nl_combi)
656                         goto nla_put_failure;
657
658                 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
659                 if (!nl_limits)
660                         goto nla_put_failure;
661
662                 for (j = 0; j < c->n_limits; j++) {
663                         struct nlattr *nl_limit;
664
665                         nl_limit = nla_nest_start(msg, j + 1);
666                         if (!nl_limit)
667                                 goto nla_put_failure;
668                         NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
669                                     c->limits[j].max);
670                         if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
671                                                 c->limits[j].types))
672                                 goto nla_put_failure;
673                         nla_nest_end(msg, nl_limit);
674                 }
675
676                 nla_nest_end(msg, nl_limits);
677
678                 if (c->beacon_int_infra_match)
679                         NLA_PUT_FLAG(msg,
680                                 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
681                 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
682                             c->num_different_channels);
683                 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
684                             c->max_interfaces);
685
686                 nla_nest_end(msg, nl_combi);
687         }
688
689         nla_nest_end(msg, nl_combis);
690
691         return 0;
692 nla_put_failure:
693         return -ENOBUFS;
694 }
695
696 static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
697                               struct cfg80211_registered_device *dev)
698 {
699         void *hdr;
700         struct nlattr *nl_bands, *nl_band;
701         struct nlattr *nl_freqs, *nl_freq;
702         struct nlattr *nl_rates, *nl_rate;
703         struct nlattr *nl_cmds;
704         enum ieee80211_band band;
705         struct ieee80211_channel *chan;
706         struct ieee80211_rate *rate;
707         int i;
708         const struct ieee80211_txrx_stypes *mgmt_stypes =
709                                 dev->wiphy.mgmt_stypes;
710
711         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
712         if (!hdr)
713                 return -1;
714
715         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
716         NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
717
718         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
719                     cfg80211_rdev_list_generation);
720
721         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
722                    dev->wiphy.retry_short);
723         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
724                    dev->wiphy.retry_long);
725         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
726                     dev->wiphy.frag_threshold);
727         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
728                     dev->wiphy.rts_threshold);
729         NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
730                     dev->wiphy.coverage_class);
731         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
732                    dev->wiphy.max_scan_ssids);
733         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
734                    dev->wiphy.max_sched_scan_ssids);
735         NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
736                     dev->wiphy.max_scan_ie_len);
737         NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
738                     dev->wiphy.max_sched_scan_ie_len);
739         NLA_PUT_U8(msg, NL80211_ATTR_MAX_MATCH_SETS,
740                    dev->wiphy.max_match_sets);
741
742         if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
743                 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
744         if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
745                 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
746         if (dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD)
747                 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_AP_UAPSD);
748         if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)
749                 NLA_PUT_FLAG(msg, NL80211_ATTR_ROAM_SUPPORT);
750         if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS)
751                 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_SUPPORT);
752         if (dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP)
753                 NLA_PUT_FLAG(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP);
754
755         NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
756                 sizeof(u32) * dev->wiphy.n_cipher_suites,
757                 dev->wiphy.cipher_suites);
758
759         NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
760                    dev->wiphy.max_num_pmkids);
761
762         if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
763                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
764
765         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
766                     dev->wiphy.available_antennas_tx);
767         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
768                     dev->wiphy.available_antennas_rx);
769
770         if (dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD)
771                 NLA_PUT_U32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
772                             dev->wiphy.probe_resp_offload);
773
774         if ((dev->wiphy.available_antennas_tx ||
775              dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
776                 u32 tx_ant = 0, rx_ant = 0;
777                 int res;
778                 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
779                 if (!res) {
780                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
781                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
782                 }
783         }
784
785         if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
786                                 dev->wiphy.interface_modes))
787                 goto nla_put_failure;
788
789         nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
790         if (!nl_bands)
791                 goto nla_put_failure;
792
793         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
794                 if (!dev->wiphy.bands[band])
795                         continue;
796
797                 nl_band = nla_nest_start(msg, band);
798                 if (!nl_band)
799                         goto nla_put_failure;
800
801                 /* add HT info */
802                 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
803                         NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
804                                 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
805                                 &dev->wiphy.bands[band]->ht_cap.mcs);
806                         NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
807                                 dev->wiphy.bands[band]->ht_cap.cap);
808                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
809                                 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
810                         NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
811                                 dev->wiphy.bands[band]->ht_cap.ampdu_density);
812                 }
813
814                 /* add frequencies */
815                 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
816                 if (!nl_freqs)
817                         goto nla_put_failure;
818
819                 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
820                         nl_freq = nla_nest_start(msg, i);
821                         if (!nl_freq)
822                                 goto nla_put_failure;
823
824                         chan = &dev->wiphy.bands[band]->channels[i];
825
826                         if (nl80211_msg_put_channel(msg, chan))
827                                 goto nla_put_failure;
828
829                         nla_nest_end(msg, nl_freq);
830                 }
831
832                 nla_nest_end(msg, nl_freqs);
833
834                 /* add bitrates */
835                 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
836                 if (!nl_rates)
837                         goto nla_put_failure;
838
839                 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
840                         nl_rate = nla_nest_start(msg, i);
841                         if (!nl_rate)
842                                 goto nla_put_failure;
843
844                         rate = &dev->wiphy.bands[band]->bitrates[i];
845                         NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
846                                     rate->bitrate);
847                         if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
848                                 NLA_PUT_FLAG(msg,
849                                         NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
850
851                         nla_nest_end(msg, nl_rate);
852                 }
853
854                 nla_nest_end(msg, nl_rates);
855
856                 nla_nest_end(msg, nl_band);
857         }
858         nla_nest_end(msg, nl_bands);
859
860         nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
861         if (!nl_cmds)
862                 goto nla_put_failure;
863
864         i = 0;
865 #define CMD(op, n)                                              \
866          do {                                                   \
867                 if (dev->ops->op) {                             \
868                         i++;                                    \
869                         NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
870                 }                                               \
871         } while (0)
872
873         CMD(add_virtual_intf, NEW_INTERFACE);
874         CMD(change_virtual_intf, SET_INTERFACE);
875         CMD(add_key, NEW_KEY);
876         CMD(start_ap, START_AP);
877         CMD(add_station, NEW_STATION);
878         CMD(add_mpath, NEW_MPATH);
879         CMD(update_mesh_config, SET_MESH_CONFIG);
880         CMD(change_bss, SET_BSS);
881         CMD(auth, AUTHENTICATE);
882         CMD(assoc, ASSOCIATE);
883         CMD(deauth, DEAUTHENTICATE);
884         CMD(disassoc, DISASSOCIATE);
885         CMD(join_ibss, JOIN_IBSS);
886         CMD(join_mesh, JOIN_MESH);
887         CMD(set_pmksa, SET_PMKSA);
888         CMD(del_pmksa, DEL_PMKSA);
889         CMD(flush_pmksa, FLUSH_PMKSA);
890         if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
891                 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
892         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
893         CMD(mgmt_tx, FRAME);
894         CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
895         if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
896                 i++;
897                 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
898         }
899         CMD(set_channel, SET_CHANNEL);
900         CMD(set_wds_peer, SET_WDS_PEER);
901         if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
902                 CMD(tdls_mgmt, TDLS_MGMT);
903                 CMD(tdls_oper, TDLS_OPER);
904         }
905         if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
906                 CMD(sched_scan_start, START_SCHED_SCAN);
907         CMD(probe_client, PROBE_CLIENT);
908         CMD(set_noack_map, SET_NOACK_MAP);
909         if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
910                 i++;
911                 NLA_PUT_U32(msg, i, NL80211_CMD_REGISTER_BEACONS);
912         }
913
914 #ifdef CONFIG_NL80211_TESTMODE
915         CMD(testmode_cmd, TESTMODE);
916 #endif
917
918 #undef CMD
919
920         if (dev->ops->connect || dev->ops->auth) {
921                 i++;
922                 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
923         }
924
925         if (dev->ops->disconnect || dev->ops->deauth) {
926                 i++;
927                 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
928         }
929
930         nla_nest_end(msg, nl_cmds);
931
932         if (dev->ops->remain_on_channel &&
933             dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
934                 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
935                             dev->wiphy.max_remain_on_channel_duration);
936
937         if (dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX)
938                 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
939
940         if (mgmt_stypes) {
941                 u16 stypes;
942                 struct nlattr *nl_ftypes, *nl_ifs;
943                 enum nl80211_iftype ift;
944
945                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
946                 if (!nl_ifs)
947                         goto nla_put_failure;
948
949                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
950                         nl_ftypes = nla_nest_start(msg, ift);
951                         if (!nl_ftypes)
952                                 goto nla_put_failure;
953                         i = 0;
954                         stypes = mgmt_stypes[ift].tx;
955                         while (stypes) {
956                                 if (stypes & 1)
957                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
958                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
959                                 stypes >>= 1;
960                                 i++;
961                         }
962                         nla_nest_end(msg, nl_ftypes);
963                 }
964
965                 nla_nest_end(msg, nl_ifs);
966
967                 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
968                 if (!nl_ifs)
969                         goto nla_put_failure;
970
971                 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
972                         nl_ftypes = nla_nest_start(msg, ift);
973                         if (!nl_ftypes)
974                                 goto nla_put_failure;
975                         i = 0;
976                         stypes = mgmt_stypes[ift].rx;
977                         while (stypes) {
978                                 if (stypes & 1)
979                                         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
980                                                     (i << 4) | IEEE80211_FTYPE_MGMT);
981                                 stypes >>= 1;
982                                 i++;
983                         }
984                         nla_nest_end(msg, nl_ftypes);
985                 }
986                 nla_nest_end(msg, nl_ifs);
987         }
988
989         if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
990                 struct nlattr *nl_wowlan;
991
992                 nl_wowlan = nla_nest_start(msg,
993                                 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
994                 if (!nl_wowlan)
995                         goto nla_put_failure;
996
997                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
998                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
999                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
1000                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
1001                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
1002                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
1003                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
1004                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
1005                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
1006                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
1007                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
1008                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
1009                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
1010                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
1011                 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
1012                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
1013                 if (dev->wiphy.wowlan.n_patterns) {
1014                         struct nl80211_wowlan_pattern_support pat = {
1015                                 .max_patterns = dev->wiphy.wowlan.n_patterns,
1016                                 .min_pattern_len =
1017                                         dev->wiphy.wowlan.pattern_min_len,
1018                                 .max_pattern_len =
1019                                         dev->wiphy.wowlan.pattern_max_len,
1020                         };
1021                         NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1022                                 sizeof(pat), &pat);
1023                 }
1024
1025                 nla_nest_end(msg, nl_wowlan);
1026         }
1027
1028         if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1029                                 dev->wiphy.software_iftypes))
1030                 goto nla_put_failure;
1031
1032         if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1033                 goto nla_put_failure;
1034
1035         if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME)
1036                 NLA_PUT_U32(msg, NL80211_ATTR_DEVICE_AP_SME,
1037                             dev->wiphy.ap_sme_capa);
1038
1039         NLA_PUT_U32(msg, NL80211_ATTR_FEATURE_FLAGS, dev->wiphy.features);
1040
1041         if (dev->wiphy.ht_capa_mod_mask)
1042                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1043                         sizeof(*dev->wiphy.ht_capa_mod_mask),
1044                         dev->wiphy.ht_capa_mod_mask);
1045
1046         return genlmsg_end(msg, hdr);
1047
1048  nla_put_failure:
1049         genlmsg_cancel(msg, hdr);
1050         return -EMSGSIZE;
1051 }
1052
1053 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1054 {
1055         int idx = 0;
1056         int start = cb->args[0];
1057         struct cfg80211_registered_device *dev;
1058
1059         mutex_lock(&cfg80211_mutex);
1060         list_for_each_entry(dev, &cfg80211_rdev_list, list) {
1061                 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1062                         continue;
1063                 if (++idx <= start)
1064                         continue;
1065                 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1066                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
1067                                        dev) < 0) {
1068                         idx--;
1069                         break;
1070                 }
1071         }
1072         mutex_unlock(&cfg80211_mutex);
1073
1074         cb->args[0] = idx;
1075
1076         return skb->len;
1077 }
1078
1079 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1080 {
1081         struct sk_buff *msg;
1082         struct cfg80211_registered_device *dev = info->user_ptr[0];
1083
1084         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1085         if (!msg)
1086                 return -ENOMEM;
1087
1088         if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1089                 nlmsg_free(msg);
1090                 return -ENOBUFS;
1091         }
1092
1093         return genlmsg_reply(msg, info);
1094 }
1095
1096 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1097         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
1098         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
1099         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
1100         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
1101         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
1102 };
1103
1104 static int parse_txq_params(struct nlattr *tb[],
1105                             struct ieee80211_txq_params *txq_params)
1106 {
1107         if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1108             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1109             !tb[NL80211_TXQ_ATTR_AIFS])
1110                 return -EINVAL;
1111
1112         txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1113         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1114         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1115         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1116         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1117
1118         return 0;
1119 }
1120
1121 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1122 {
1123         /*
1124          * You can only set the channel explicitly for AP, mesh
1125          * and WDS type interfaces; all others have their channel
1126          * managed via their respective "establish a connection"
1127          * command (connect, join, ...)
1128          *
1129          * Monitors are special as they are normally slaved to
1130          * whatever else is going on, so they behave as though
1131          * you tried setting the wiphy channel itself.
1132          */
1133         return !wdev ||
1134                 wdev->iftype == NL80211_IFTYPE_AP ||
1135                 wdev->iftype == NL80211_IFTYPE_WDS ||
1136                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
1137                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1138                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
1139 }
1140
1141 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1142                                  struct wireless_dev *wdev,
1143                                  struct genl_info *info)
1144 {
1145         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1146         u32 freq;
1147         int result;
1148
1149         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1150                 return -EINVAL;
1151
1152         if (!nl80211_can_set_dev_channel(wdev))
1153                 return -EOPNOTSUPP;
1154
1155         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1156                 channel_type = nla_get_u32(info->attrs[
1157                                    NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1158                 if (channel_type != NL80211_CHAN_NO_HT &&
1159                     channel_type != NL80211_CHAN_HT20 &&
1160                     channel_type != NL80211_CHAN_HT40PLUS &&
1161                     channel_type != NL80211_CHAN_HT40MINUS)
1162                         return -EINVAL;
1163         }
1164
1165         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1166
1167         mutex_lock(&rdev->devlist_mtx);
1168         if (wdev) {
1169                 wdev_lock(wdev);
1170                 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1171                 wdev_unlock(wdev);
1172         } else {
1173                 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1174         }
1175         mutex_unlock(&rdev->devlist_mtx);
1176
1177         return result;
1178 }
1179
1180 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1181 {
1182         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1183         struct net_device *netdev = info->user_ptr[1];
1184
1185         return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
1186 }
1187
1188 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1189 {
1190         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1191         struct net_device *dev = info->user_ptr[1];
1192         struct wireless_dev *wdev = dev->ieee80211_ptr;
1193         const u8 *bssid;
1194
1195         if (!info->attrs[NL80211_ATTR_MAC])
1196                 return -EINVAL;
1197
1198         if (netif_running(dev))
1199                 return -EBUSY;
1200
1201         if (!rdev->ops->set_wds_peer)
1202                 return -EOPNOTSUPP;
1203
1204         if (wdev->iftype != NL80211_IFTYPE_WDS)
1205                 return -EOPNOTSUPP;
1206
1207         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
1208         return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
1209 }
1210
1211
1212 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1213 {
1214         struct cfg80211_registered_device *rdev;
1215         struct net_device *netdev = NULL;
1216         struct wireless_dev *wdev;
1217         int result = 0, rem_txq_params = 0;
1218         struct nlattr *nl_txq_params;
1219         u32 changed;
1220         u8 retry_short = 0, retry_long = 0;
1221         u32 frag_threshold = 0, rts_threshold = 0;
1222         u8 coverage_class = 0;
1223
1224         /*
1225          * Try to find the wiphy and netdev. Normally this
1226          * function shouldn't need the netdev, but this is
1227          * done for backward compatibility -- previously
1228          * setting the channel was done per wiphy, but now
1229          * it is per netdev. Previous userland like hostapd
1230          * also passed a netdev to set_wiphy, so that it is
1231          * possible to let that go to the right netdev!
1232          */
1233         mutex_lock(&cfg80211_mutex);
1234
1235         if (info->attrs[NL80211_ATTR_IFINDEX]) {
1236                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1237
1238                 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1239                 if (netdev && netdev->ieee80211_ptr) {
1240                         rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1241                         mutex_lock(&rdev->mtx);
1242                 } else
1243                         netdev = NULL;
1244         }
1245
1246         if (!netdev) {
1247                 rdev = __cfg80211_rdev_from_info(info);
1248                 if (IS_ERR(rdev)) {
1249                         mutex_unlock(&cfg80211_mutex);
1250                         return PTR_ERR(rdev);
1251                 }
1252                 wdev = NULL;
1253                 netdev = NULL;
1254                 result = 0;
1255
1256                 mutex_lock(&rdev->mtx);
1257         } else if (netif_running(netdev) &&
1258                    nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1259                 wdev = netdev->ieee80211_ptr;
1260         else
1261                 wdev = NULL;
1262
1263         /*
1264          * end workaround code, by now the rdev is available
1265          * and locked, and wdev may or may not be NULL.
1266          */
1267
1268         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
1269                 result = cfg80211_dev_rename(
1270                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
1271
1272         mutex_unlock(&cfg80211_mutex);
1273
1274         if (result)
1275                 goto bad_res;
1276
1277         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1278                 struct ieee80211_txq_params txq_params;
1279                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1280
1281                 if (!rdev->ops->set_txq_params) {
1282                         result = -EOPNOTSUPP;
1283                         goto bad_res;
1284                 }
1285
1286                 if (!netdev) {
1287                         result = -EINVAL;
1288                         goto bad_res;
1289                 }
1290
1291                 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1292                     netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1293                         result = -EINVAL;
1294                         goto bad_res;
1295                 }
1296
1297                 if (!netif_running(netdev)) {
1298                         result = -ENETDOWN;
1299                         goto bad_res;
1300                 }
1301
1302                 nla_for_each_nested(nl_txq_params,
1303                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1304                                     rem_txq_params) {
1305                         nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1306                                   nla_data(nl_txq_params),
1307                                   nla_len(nl_txq_params),
1308                                   txq_params_policy);
1309                         result = parse_txq_params(tb, &txq_params);
1310                         if (result)
1311                                 goto bad_res;
1312
1313                         result = rdev->ops->set_txq_params(&rdev->wiphy,
1314                                                            netdev,
1315                                                            &txq_params);
1316                         if (result)
1317                                 goto bad_res;
1318                 }
1319         }
1320
1321         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
1322                 result = __nl80211_set_channel(rdev, wdev, info);
1323                 if (result)
1324                         goto bad_res;
1325         }
1326
1327         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1328                 enum nl80211_tx_power_setting type;
1329                 int idx, mbm = 0;
1330
1331                 if (!rdev->ops->set_tx_power) {
1332                         result = -EOPNOTSUPP;
1333                         goto bad_res;
1334                 }
1335
1336                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1337                 type = nla_get_u32(info->attrs[idx]);
1338
1339                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1340                     (type != NL80211_TX_POWER_AUTOMATIC)) {
1341                         result = -EINVAL;
1342                         goto bad_res;
1343                 }
1344
1345                 if (type != NL80211_TX_POWER_AUTOMATIC) {
1346                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1347                         mbm = nla_get_u32(info->attrs[idx]);
1348                 }
1349
1350                 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1351                 if (result)
1352                         goto bad_res;
1353         }
1354
1355         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1356             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1357                 u32 tx_ant, rx_ant;
1358                 if ((!rdev->wiphy.available_antennas_tx &&
1359                      !rdev->wiphy.available_antennas_rx) ||
1360                     !rdev->ops->set_antenna) {
1361                         result = -EOPNOTSUPP;
1362                         goto bad_res;
1363                 }
1364
1365                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1366                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1367
1368                 /* reject antenna configurations which don't match the
1369                  * available antenna masks, except for the "all" mask */
1370                 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1371                     (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
1372                         result = -EINVAL;
1373                         goto bad_res;
1374                 }
1375
1376                 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1377                 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
1378
1379                 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1380                 if (result)
1381                         goto bad_res;
1382         }
1383
1384         changed = 0;
1385
1386         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1387                 retry_short = nla_get_u8(
1388                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1389                 if (retry_short == 0) {
1390                         result = -EINVAL;
1391                         goto bad_res;
1392                 }
1393                 changed |= WIPHY_PARAM_RETRY_SHORT;
1394         }
1395
1396         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1397                 retry_long = nla_get_u8(
1398                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1399                 if (retry_long == 0) {
1400                         result = -EINVAL;
1401                         goto bad_res;
1402                 }
1403                 changed |= WIPHY_PARAM_RETRY_LONG;
1404         }
1405
1406         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1407                 frag_threshold = nla_get_u32(
1408                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1409                 if (frag_threshold < 256) {
1410                         result = -EINVAL;
1411                         goto bad_res;
1412                 }
1413                 if (frag_threshold != (u32) -1) {
1414                         /*
1415                          * Fragments (apart from the last one) are required to
1416                          * have even length. Make the fragmentation code
1417                          * simpler by stripping LSB should someone try to use
1418                          * odd threshold value.
1419                          */
1420                         frag_threshold &= ~0x1;
1421                 }
1422                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1423         }
1424
1425         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1426                 rts_threshold = nla_get_u32(
1427                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1428                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1429         }
1430
1431         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1432                 coverage_class = nla_get_u8(
1433                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1434                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1435         }
1436
1437         if (changed) {
1438                 u8 old_retry_short, old_retry_long;
1439                 u32 old_frag_threshold, old_rts_threshold;
1440                 u8 old_coverage_class;
1441
1442                 if (!rdev->ops->set_wiphy_params) {
1443                         result = -EOPNOTSUPP;
1444                         goto bad_res;
1445                 }
1446
1447                 old_retry_short = rdev->wiphy.retry_short;
1448                 old_retry_long = rdev->wiphy.retry_long;
1449                 old_frag_threshold = rdev->wiphy.frag_threshold;
1450                 old_rts_threshold = rdev->wiphy.rts_threshold;
1451                 old_coverage_class = rdev->wiphy.coverage_class;
1452
1453                 if (changed & WIPHY_PARAM_RETRY_SHORT)
1454                         rdev->wiphy.retry_short = retry_short;
1455                 if (changed & WIPHY_PARAM_RETRY_LONG)
1456                         rdev->wiphy.retry_long = retry_long;
1457                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1458                         rdev->wiphy.frag_threshold = frag_threshold;
1459                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1460                         rdev->wiphy.rts_threshold = rts_threshold;
1461                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1462                         rdev->wiphy.coverage_class = coverage_class;
1463
1464                 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1465                 if (result) {
1466                         rdev->wiphy.retry_short = old_retry_short;
1467                         rdev->wiphy.retry_long = old_retry_long;
1468                         rdev->wiphy.frag_threshold = old_frag_threshold;
1469                         rdev->wiphy.rts_threshold = old_rts_threshold;
1470                         rdev->wiphy.coverage_class = old_coverage_class;
1471                 }
1472         }
1473
1474  bad_res:
1475         mutex_unlock(&rdev->mtx);
1476         if (netdev)
1477                 dev_put(netdev);
1478         return result;
1479 }
1480
1481
1482 static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
1483                               struct cfg80211_registered_device *rdev,
1484                               struct net_device *dev)
1485 {
1486         void *hdr;
1487
1488         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1489         if (!hdr)
1490                 return -1;
1491
1492         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1493         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
1494         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
1495         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
1496
1497         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1498                     rdev->devlist_generation ^
1499                         (cfg80211_rdev_list_generation << 2));
1500
1501         return genlmsg_end(msg, hdr);
1502
1503  nla_put_failure:
1504         genlmsg_cancel(msg, hdr);
1505         return -EMSGSIZE;
1506 }
1507
1508 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1509 {
1510         int wp_idx = 0;
1511         int if_idx = 0;
1512         int wp_start = cb->args[0];
1513         int if_start = cb->args[1];
1514         struct cfg80211_registered_device *rdev;
1515         struct wireless_dev *wdev;
1516
1517         mutex_lock(&cfg80211_mutex);
1518         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1519                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
1520                         continue;
1521                 if (wp_idx < wp_start) {
1522                         wp_idx++;
1523                         continue;
1524                 }
1525                 if_idx = 0;
1526
1527                 mutex_lock(&rdev->devlist_mtx);
1528                 list_for_each_entry(wdev, &rdev->netdev_list, list) {
1529                         if (if_idx < if_start) {
1530                                 if_idx++;
1531                                 continue;
1532                         }
1533                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1534                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
1535                                                rdev, wdev->netdev) < 0) {
1536                                 mutex_unlock(&rdev->devlist_mtx);
1537                                 goto out;
1538                         }
1539                         if_idx++;
1540                 }
1541                 mutex_unlock(&rdev->devlist_mtx);
1542
1543                 wp_idx++;
1544         }
1545  out:
1546         mutex_unlock(&cfg80211_mutex);
1547
1548         cb->args[0] = wp_idx;
1549         cb->args[1] = if_idx;
1550
1551         return skb->len;
1552 }
1553
1554 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1555 {
1556         struct sk_buff *msg;
1557         struct cfg80211_registered_device *dev = info->user_ptr[0];
1558         struct net_device *netdev = info->user_ptr[1];
1559
1560         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1561         if (!msg)
1562                 return -ENOMEM;
1563
1564         if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
1565                                dev, netdev) < 0) {
1566                 nlmsg_free(msg);
1567                 return -ENOBUFS;
1568         }
1569
1570         return genlmsg_reply(msg, info);
1571 }
1572
1573 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1574         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1575         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1576         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1577         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1578         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1579 };
1580
1581 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1582 {
1583         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1584         int flag;
1585
1586         *mntrflags = 0;
1587
1588         if (!nla)
1589                 return -EINVAL;
1590
1591         if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1592                              nla, mntr_flags_policy))
1593                 return -EINVAL;
1594
1595         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1596                 if (flags[flag])
1597                         *mntrflags |= (1<<flag);
1598
1599         return 0;
1600 }
1601
1602 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
1603                                struct net_device *netdev, u8 use_4addr,
1604                                enum nl80211_iftype iftype)
1605 {
1606         if (!use_4addr) {
1607                 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
1608                         return -EBUSY;
1609                 return 0;
1610         }
1611
1612         switch (iftype) {
1613         case NL80211_IFTYPE_AP_VLAN:
1614                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1615                         return 0;
1616                 break;
1617         case NL80211_IFTYPE_STATION:
1618                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1619                         return 0;
1620                 break;
1621         default:
1622                 break;
1623         }
1624
1625         return -EOPNOTSUPP;
1626 }
1627
1628 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1629 {
1630         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1631         struct vif_params params;
1632         int err;
1633         enum nl80211_iftype otype, ntype;
1634         struct net_device *dev = info->user_ptr[1];
1635         u32 _flags, *flags = NULL;
1636         bool change = false;
1637
1638         memset(&params, 0, sizeof(params));
1639
1640         otype = ntype = dev->ieee80211_ptr->iftype;
1641
1642         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1643                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1644                 if (otype != ntype)
1645                         change = true;
1646                 if (ntype > NL80211_IFTYPE_MAX)
1647                         return -EINVAL;
1648         }
1649
1650         if (info->attrs[NL80211_ATTR_MESH_ID]) {
1651                 struct wireless_dev *wdev = dev->ieee80211_ptr;
1652
1653                 if (ntype != NL80211_IFTYPE_MESH_POINT)
1654                         return -EINVAL;
1655                 if (netif_running(dev))
1656                         return -EBUSY;
1657
1658                 wdev_lock(wdev);
1659                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1660                              IEEE80211_MAX_MESH_ID_LEN);
1661                 wdev->mesh_id_up_len =
1662                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1663                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1664                        wdev->mesh_id_up_len);
1665                 wdev_unlock(wdev);
1666         }
1667
1668         if (info->attrs[NL80211_ATTR_4ADDR]) {
1669                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1670                 change = true;
1671                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
1672                 if (err)
1673                         return err;
1674         } else {
1675                 params.use_4addr = -1;
1676         }
1677
1678         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
1679                 if (ntype != NL80211_IFTYPE_MONITOR)
1680                         return -EINVAL;
1681                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1682                                           &_flags);
1683                 if (err)
1684                         return err;
1685
1686                 flags = &_flags;
1687                 change = true;
1688         }
1689
1690         if (change)
1691                 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
1692         else
1693                 err = 0;
1694
1695         if (!err && params.use_4addr != -1)
1696                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1697
1698         return err;
1699 }
1700
1701 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1702 {
1703         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1704         struct vif_params params;
1705         struct net_device *dev;
1706         int err;
1707         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
1708         u32 flags;
1709
1710         memset(&params, 0, sizeof(params));
1711
1712         if (!info->attrs[NL80211_ATTR_IFNAME])
1713                 return -EINVAL;
1714
1715         if (info->attrs[NL80211_ATTR_IFTYPE]) {
1716                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1717                 if (type > NL80211_IFTYPE_MAX)
1718                         return -EINVAL;
1719         }
1720
1721         if (!rdev->ops->add_virtual_intf ||
1722             !(rdev->wiphy.interface_modes & (1 << type)))
1723                 return -EOPNOTSUPP;
1724
1725         if (info->attrs[NL80211_ATTR_4ADDR]) {
1726                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1727                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
1728                 if (err)
1729                         return err;
1730         }
1731
1732         err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1733                                   info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1734                                   &flags);
1735         dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
1736                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
1737                 type, err ? NULL : &flags, &params);
1738         if (IS_ERR(dev))
1739                 return PTR_ERR(dev);
1740
1741         if (type == NL80211_IFTYPE_MESH_POINT &&
1742             info->attrs[NL80211_ATTR_MESH_ID]) {
1743                 struct wireless_dev *wdev = dev->ieee80211_ptr;
1744
1745                 wdev_lock(wdev);
1746                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1747                              IEEE80211_MAX_MESH_ID_LEN);
1748                 wdev->mesh_id_up_len =
1749                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1750                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1751                        wdev->mesh_id_up_len);
1752                 wdev_unlock(wdev);
1753         }
1754
1755         return 0;
1756 }
1757
1758 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1759 {
1760         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1761         struct net_device *dev = info->user_ptr[1];
1762
1763         if (!rdev->ops->del_virtual_intf)
1764                 return -EOPNOTSUPP;
1765
1766         return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
1767 }
1768
1769 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
1770 {
1771         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1772         struct net_device *dev = info->user_ptr[1];
1773         u16 noack_map;
1774
1775         if (!info->attrs[NL80211_ATTR_NOACK_MAP])
1776                 return -EINVAL;
1777
1778         if (!rdev->ops->set_noack_map)
1779                 return -EOPNOTSUPP;
1780
1781         noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
1782
1783         return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
1784 }
1785
1786 struct get_key_cookie {
1787         struct sk_buff *msg;
1788         int error;
1789         int idx;
1790 };
1791
1792 static void get_key_callback(void *c, struct key_params *params)
1793 {
1794         struct nlattr *key;
1795         struct get_key_cookie *cookie = c;
1796
1797         if (params->key)
1798                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1799                         params->key_len, params->key);
1800
1801         if (params->seq)
1802                 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1803                         params->seq_len, params->seq);
1804
1805         if (params->cipher)
1806                 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1807                             params->cipher);
1808
1809         key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1810         if (!key)
1811                 goto nla_put_failure;
1812
1813         if (params->key)
1814                 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1815                         params->key_len, params->key);
1816
1817         if (params->seq)
1818                 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1819                         params->seq_len, params->seq);
1820
1821         if (params->cipher)
1822                 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1823                             params->cipher);
1824
1825         NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1826
1827         nla_nest_end(cookie->msg, key);
1828
1829         return;
1830  nla_put_failure:
1831         cookie->error = 1;
1832 }
1833
1834 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1835 {
1836         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1837         int err;
1838         struct net_device *dev = info->user_ptr[1];
1839         u8 key_idx = 0;
1840         const u8 *mac_addr = NULL;
1841         bool pairwise;
1842         struct get_key_cookie cookie = {
1843                 .error = 0,
1844         };
1845         void *hdr;
1846         struct sk_buff *msg;
1847
1848         if (info->attrs[NL80211_ATTR_KEY_IDX])
1849                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1850
1851         if (key_idx > 5)
1852                 return -EINVAL;
1853
1854         if (info->attrs[NL80211_ATTR_MAC])
1855                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1856
1857         pairwise = !!mac_addr;
1858         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1859                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1860                 if (kt >= NUM_NL80211_KEYTYPES)
1861                         return -EINVAL;
1862                 if (kt != NL80211_KEYTYPE_GROUP &&
1863                     kt != NL80211_KEYTYPE_PAIRWISE)
1864                         return -EINVAL;
1865                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1866         }
1867
1868         if (!rdev->ops->get_key)
1869                 return -EOPNOTSUPP;
1870
1871         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1872         if (!msg)
1873                 return -ENOMEM;
1874
1875         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1876                              NL80211_CMD_NEW_KEY);
1877         if (IS_ERR(hdr))
1878                 return PTR_ERR(hdr);
1879
1880         cookie.msg = msg;
1881         cookie.idx = key_idx;
1882
1883         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1884         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1885         if (mac_addr)
1886                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1887
1888         if (pairwise && mac_addr &&
1889             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1890                 return -ENOENT;
1891
1892         err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1893                                  mac_addr, &cookie, get_key_callback);
1894
1895         if (err)
1896                 goto free_msg;
1897
1898         if (cookie.error)
1899                 goto nla_put_failure;
1900
1901         genlmsg_end(msg, hdr);
1902         return genlmsg_reply(msg, info);
1903
1904  nla_put_failure:
1905         err = -ENOBUFS;
1906  free_msg:
1907         nlmsg_free(msg);
1908         return err;
1909 }
1910
1911 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1912 {
1913         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1914         struct key_parse key;
1915         int err;
1916         struct net_device *dev = info->user_ptr[1];
1917
1918         err = nl80211_parse_key(info, &key);
1919         if (err)
1920                 return err;
1921
1922         if (key.idx < 0)
1923                 return -EINVAL;
1924
1925         /* only support setting default key */
1926         if (!key.def && !key.defmgmt)
1927                 return -EINVAL;
1928
1929         wdev_lock(dev->ieee80211_ptr);
1930
1931         if (key.def) {
1932                 if (!rdev->ops->set_default_key) {
1933                         err = -EOPNOTSUPP;
1934                         goto out;
1935                 }
1936
1937                 err = nl80211_key_allowed(dev->ieee80211_ptr);
1938                 if (err)
1939                         goto out;
1940
1941                 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1942                                                  key.def_uni, key.def_multi);
1943
1944                 if (err)
1945                         goto out;
1946
1947 #ifdef CONFIG_CFG80211_WEXT
1948                 dev->ieee80211_ptr->wext.default_key = key.idx;
1949 #endif
1950         } else {
1951                 if (key.def_uni || !key.def_multi) {
1952                         err = -EINVAL;
1953                         goto out;
1954                 }
1955
1956                 if (!rdev->ops->set_default_mgmt_key) {
1957                         err = -EOPNOTSUPP;
1958                         goto out;
1959                 }
1960
1961                 err = nl80211_key_allowed(dev->ieee80211_ptr);
1962                 if (err)
1963                         goto out;
1964
1965                 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1966                                                       dev, key.idx);
1967                 if (err)
1968                         goto out;
1969
1970 #ifdef CONFIG_CFG80211_WEXT
1971                 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1972 #endif
1973         }
1974
1975  out:
1976         wdev_unlock(dev->ieee80211_ptr);
1977
1978         return err;
1979 }
1980
1981 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1982 {
1983         struct cfg80211_registered_device *rdev = info->user_ptr[0];
1984         int err;
1985         struct net_device *dev = info->user_ptr[1];
1986         struct key_parse key;
1987         const u8 *mac_addr = NULL;
1988
1989         err = nl80211_parse_key(info, &key);
1990         if (err)
1991                 return err;
1992
1993         if (!key.p.key)
1994                 return -EINVAL;
1995
1996         if (info->attrs[NL80211_ATTR_MAC])
1997                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1998
1999         if (key.type == -1) {
2000                 if (mac_addr)
2001                         key.type = NL80211_KEYTYPE_PAIRWISE;
2002                 else
2003                         key.type = NL80211_KEYTYPE_GROUP;
2004         }
2005
2006         /* for now */
2007         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2008             key.type != NL80211_KEYTYPE_GROUP)
2009                 return -EINVAL;
2010
2011         if (!rdev->ops->add_key)
2012                 return -EOPNOTSUPP;
2013
2014         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2015                                            key.type == NL80211_KEYTYPE_PAIRWISE,
2016                                            mac_addr))
2017                 return -EINVAL;
2018
2019         wdev_lock(dev->ieee80211_ptr);
2020         err = nl80211_key_allowed(dev->ieee80211_ptr);
2021         if (!err)
2022                 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
2023                                          key.type == NL80211_KEYTYPE_PAIRWISE,
2024                                          mac_addr, &key.p);
2025         wdev_unlock(dev->ieee80211_ptr);
2026
2027         return err;
2028 }
2029
2030 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2031 {
2032         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2033         int err;
2034         struct net_device *dev = info->user_ptr[1];
2035         u8 *mac_addr = NULL;
2036         struct key_parse key;
2037
2038         err = nl80211_parse_key(info, &key);
2039         if (err)
2040                 return err;
2041
2042         if (info->attrs[NL80211_ATTR_MAC])
2043                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2044
2045         if (key.type == -1) {
2046                 if (mac_addr)
2047                         key.type = NL80211_KEYTYPE_PAIRWISE;
2048                 else
2049                         key.type = NL80211_KEYTYPE_GROUP;
2050         }
2051
2052         /* for now */
2053         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2054             key.type != NL80211_KEYTYPE_GROUP)
2055                 return -EINVAL;
2056
2057         if (!rdev->ops->del_key)
2058                 return -EOPNOTSUPP;
2059
2060         wdev_lock(dev->ieee80211_ptr);
2061         err = nl80211_key_allowed(dev->ieee80211_ptr);
2062
2063         if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2064             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2065                 err = -ENOENT;
2066
2067         if (!err)
2068                 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2069                                          key.type == NL80211_KEYTYPE_PAIRWISE,
2070                                          mac_addr);
2071
2072 #ifdef CONFIG_CFG80211_WEXT
2073         if (!err) {
2074                 if (key.idx == dev->ieee80211_ptr->wext.default_key)
2075                         dev->ieee80211_ptr->wext.default_key = -1;
2076                 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
2077                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2078         }
2079 #endif
2080         wdev_unlock(dev->ieee80211_ptr);
2081
2082         return err;
2083 }
2084
2085 static int nl80211_parse_beacon(struct genl_info *info,
2086                                 struct cfg80211_beacon_data *bcn)
2087 {
2088         bool haveinfo = false;
2089
2090         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2091             !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2092             !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2093             !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
2094                 return -EINVAL;
2095
2096         memset(bcn, 0, sizeof(*bcn));
2097
2098         if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2099                 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2100                 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2101                 if (!bcn->head_len)
2102                         return -EINVAL;
2103                 haveinfo = true;
2104         }
2105
2106         if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2107                 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2108                 bcn->tail_len =
2109                     nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2110                 haveinfo = true;
2111         }
2112
2113         if (!haveinfo)
2114                 return -EINVAL;
2115
2116         if (info->attrs[NL80211_ATTR_IE]) {
2117                 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2118                 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2119         }
2120
2121         if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2122                 bcn->proberesp_ies =
2123                         nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2124                 bcn->proberesp_ies_len =
2125                         nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2126         }
2127
2128         if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2129                 bcn->assocresp_ies =
2130                         nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2131                 bcn->assocresp_ies_len =
2132                         nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2133         }
2134
2135         if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
2136                 bcn->probe_resp =
2137                         nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
2138                 bcn->probe_resp_len =
2139                         nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2140         }
2141
2142         return 0;
2143 }
2144
2145 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2146 {
2147         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2148         struct net_device *dev = info->user_ptr[1];
2149         struct wireless_dev *wdev = dev->ieee80211_ptr;
2150         struct cfg80211_ap_settings params;
2151         int err;
2152
2153         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2154             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2155                 return -EOPNOTSUPP;
2156
2157         if (!rdev->ops->start_ap)
2158                 return -EOPNOTSUPP;
2159
2160         if (wdev->beacon_interval)
2161                 return -EALREADY;
2162
2163         memset(&params, 0, sizeof(params));
2164
2165         /* these are required for START_AP */
2166         if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2167             !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2168             !info->attrs[NL80211_ATTR_BEACON_HEAD])
2169                 return -EINVAL;
2170
2171         err = nl80211_parse_beacon(info, &params.beacon);
2172         if (err)
2173                 return err;
2174
2175         params.beacon_interval =
2176                 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2177         params.dtim_period =
2178                 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2179
2180         err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2181         if (err)
2182                 return err;
2183
2184         /*
2185          * In theory, some of these attributes should be required here
2186          * but since they were not used when the command was originally
2187          * added, keep them optional for old user space programs to let
2188          * them continue to work with drivers that do not need the
2189          * additional information -- drivers must check!
2190          */
2191         if (info->attrs[NL80211_ATTR_SSID]) {
2192                 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2193                 params.ssid_len =
2194                         nla_len(info->attrs[NL80211_ATTR_SSID]);
2195                 if (params.ssid_len == 0 ||
2196                     params.ssid_len > IEEE80211_MAX_SSID_LEN)
2197                         return -EINVAL;
2198         }
2199
2200         if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2201                 params.hidden_ssid = nla_get_u32(
2202                         info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2203                 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2204                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2205                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2206                         return -EINVAL;
2207         }
2208
2209         params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2210
2211         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2212                 params.auth_type = nla_get_u32(
2213                         info->attrs[NL80211_ATTR_AUTH_TYPE]);
2214                 if (!nl80211_valid_auth_type(params.auth_type))
2215                         return -EINVAL;
2216         } else
2217                 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2218
2219         err = nl80211_crypto_settings(rdev, info, &params.crypto,
2220                                       NL80211_MAX_NR_CIPHER_SUITES);
2221         if (err)
2222                 return err;
2223
2224         err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2225         if (!err)
2226                 wdev->beacon_interval = params.beacon_interval;
2227         return err;
2228 }
2229
2230 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2231 {
2232         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2233         struct net_device *dev = info->user_ptr[1];
2234         struct wireless_dev *wdev = dev->ieee80211_ptr;
2235         struct cfg80211_beacon_data params;
2236         int err;
2237
2238         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2239             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2240                 return -EOPNOTSUPP;
2241
2242         if (!rdev->ops->change_beacon)
2243                 return -EOPNOTSUPP;
2244
2245         if (!wdev->beacon_interval)
2246                 return -EINVAL;
2247
2248         err = nl80211_parse_beacon(info, &params);
2249         if (err)
2250                 return err;
2251
2252         return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2253 }
2254
2255 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
2256 {
2257         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2258         struct net_device *dev = info->user_ptr[1];
2259         struct wireless_dev *wdev = dev->ieee80211_ptr;
2260         int err;
2261
2262         if (!rdev->ops->stop_ap)
2263                 return -EOPNOTSUPP;
2264
2265         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2266             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2267                 return -EOPNOTSUPP;
2268
2269         if (!wdev->beacon_interval)
2270                 return -ENOENT;
2271
2272         err = rdev->ops->stop_ap(&rdev->wiphy, dev);
2273         if (!err)
2274                 wdev->beacon_interval = 0;
2275         return err;
2276 }
2277
2278 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2279         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2280         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2281         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2282         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2283         [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2284         [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
2285 };
2286
2287 static int parse_station_flags(struct genl_info *info,
2288                                enum nl80211_iftype iftype,
2289                                struct station_parameters *params)
2290 {
2291         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2292         struct nlattr *nla;
2293         int flag;
2294
2295         /*
2296          * Try parsing the new attribute first so userspace
2297          * can specify both for older kernels.
2298          */
2299         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2300         if (nla) {
2301                 struct nl80211_sta_flag_update *sta_flags;
2302
2303                 sta_flags = nla_data(nla);
2304                 params->sta_flags_mask = sta_flags->mask;
2305                 params->sta_flags_set = sta_flags->set;
2306                 if ((params->sta_flags_mask |
2307                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2308                         return -EINVAL;
2309                 return 0;
2310         }
2311
2312         /* if present, parse the old attribute */
2313
2314         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2315         if (!nla)
2316                 return 0;
2317
2318         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2319                              nla, sta_flags_policy))
2320                 return -EINVAL;
2321
2322         /*
2323          * Only allow certain flags for interface types so that
2324          * other attributes are silently ignored. Remember that
2325          * this is backward compatibility code with old userspace
2326          * and shouldn't be hit in other cases anyway.
2327          */
2328         switch (iftype) {
2329         case NL80211_IFTYPE_AP:
2330         case NL80211_IFTYPE_AP_VLAN:
2331         case NL80211_IFTYPE_P2P_GO:
2332                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2333                                          BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2334                                          BIT(NL80211_STA_FLAG_WME) |
2335                                          BIT(NL80211_STA_FLAG_MFP);
2336                 break;
2337         case NL80211_IFTYPE_P2P_CLIENT:
2338         case NL80211_IFTYPE_STATION:
2339                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2340                                          BIT(NL80211_STA_FLAG_TDLS_PEER);
2341                 break;
2342         case NL80211_IFTYPE_MESH_POINT:
2343                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2344                                          BIT(NL80211_STA_FLAG_MFP) |
2345                                          BIT(NL80211_STA_FLAG_AUTHORIZED);
2346         default:
2347                 return -EINVAL;
2348         }
2349
2350         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2351                 if (flags[flag])
2352                         params->sta_flags_set |= (1<<flag);
2353
2354         return 0;
2355 }
2356
2357 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2358                                  int attr)
2359 {
2360         struct nlattr *rate;
2361         u16 bitrate;
2362
2363         rate = nla_nest_start(msg, attr);
2364         if (!rate)
2365                 goto nla_put_failure;
2366
2367         /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2368         bitrate = cfg80211_calculate_bitrate(info);
2369         if (bitrate > 0)
2370                 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2371
2372         if (info->flags & RATE_INFO_FLAGS_MCS)
2373                 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2374         if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2375                 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2376         if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2377                 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2378
2379         nla_nest_end(msg, rate);
2380         return true;
2381
2382 nla_put_failure:
2383         return false;
2384 }
2385
2386 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2387                                 int flags,
2388                                 struct cfg80211_registered_device *rdev,
2389                                 struct net_device *dev,
2390                                 const u8 *mac_addr, struct station_info *sinfo)
2391 {
2392         void *hdr;
2393         struct nlattr *sinfoattr, *bss_param;
2394
2395         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2396         if (!hdr)
2397                 return -1;
2398
2399         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2400         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2401
2402         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2403
2404         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2405         if (!sinfoattr)
2406                 goto nla_put_failure;
2407         if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2408                 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2409                             sinfo->connected_time);
2410         if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2411                 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2412                             sinfo->inactive_time);
2413         if (sinfo->filled & STATION_INFO_RX_BYTES)
2414                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2415                             sinfo->rx_bytes);
2416         if (sinfo->filled & STATION_INFO_TX_BYTES)
2417                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2418                             sinfo->tx_bytes);
2419         if (sinfo->filled & STATION_INFO_LLID)
2420                 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2421                             sinfo->llid);
2422         if (sinfo->filled & STATION_INFO_PLID)
2423                 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2424                             sinfo->plid);
2425         if (sinfo->filled & STATION_INFO_PLINK_STATE)
2426                 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2427                             sinfo->plink_state);
2428         switch (rdev->wiphy.signal_type) {
2429         case CFG80211_SIGNAL_TYPE_MBM:
2430                 if (sinfo->filled & STATION_INFO_SIGNAL)
2431                         NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2432                                    sinfo->signal);
2433                 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2434                         NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2435                                    sinfo->signal_avg);
2436                 break;
2437         default:
2438                 break;
2439         }
2440         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2441                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2442                                           NL80211_STA_INFO_TX_BITRATE))
2443                         goto nla_put_failure;
2444         }
2445         if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2446                 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2447                                           NL80211_STA_INFO_RX_BITRATE))
2448                         goto nla_put_failure;
2449         }
2450         if (sinfo->filled & STATION_INFO_RX_PACKETS)
2451                 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2452                             sinfo->rx_packets);
2453         if (sinfo->filled & STATION_INFO_TX_PACKETS)
2454                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2455                             sinfo->tx_packets);
2456         if (sinfo->filled & STATION_INFO_TX_RETRIES)
2457                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2458                             sinfo->tx_retries);
2459         if (sinfo->filled & STATION_INFO_TX_FAILED)
2460                 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2461                             sinfo->tx_failed);
2462         if (sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT)
2463                 NLA_PUT_U32(msg, NL80211_STA_INFO_BEACON_LOSS,
2464                             sinfo->beacon_loss_count);
2465         if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2466                 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2467                 if (!bss_param)
2468                         goto nla_put_failure;
2469
2470                 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2471                         NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2472                 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2473                         NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2474                 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2475                         NLA_PUT_FLAG(msg,
2476                                      NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2477                 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2478                            sinfo->bss_param.dtim_period);
2479                 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2480                             sinfo->bss_param.beacon_interval);
2481
2482                 nla_nest_end(msg, bss_param);
2483         }
2484         if (sinfo->filled & STATION_INFO_STA_FLAGS)
2485                 NLA_PUT(msg, NL80211_STA_INFO_STA_FLAGS,
2486                         sizeof(struct nl80211_sta_flag_update),
2487                         &sinfo->sta_flags);
2488         nla_nest_end(msg, sinfoattr);
2489
2490         if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
2491                 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2492                         sinfo->assoc_req_ies);
2493
2494         return genlmsg_end(msg, hdr);
2495
2496  nla_put_failure:
2497         genlmsg_cancel(msg, hdr);
2498         return -EMSGSIZE;
2499 }
2500
2501 static int nl80211_dump_station(struct sk_buff *skb,
2502                                 struct netlink_callback *cb)
2503 {
2504         struct station_info sinfo;
2505         struct cfg80211_registered_device *dev;
2506         struct net_device *netdev;
2507         u8 mac_addr[ETH_ALEN];
2508         int sta_idx = cb->args[1];
2509         int err;
2510
2511         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2512         if (err)
2513                 return err;
2514
2515         if (!dev->ops->dump_station) {
2516                 err = -EOPNOTSUPP;
2517                 goto out_err;
2518         }
2519
2520         while (1) {
2521                 memset(&sinfo, 0, sizeof(sinfo));
2522                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2523                                              mac_addr, &sinfo);
2524                 if (err == -ENOENT)
2525                         break;
2526                 if (err)
2527                         goto out_err;
2528
2529                 if (nl80211_send_station(skb,
2530                                 NETLINK_CB(cb->skb).pid,
2531                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2532                                 dev, netdev, mac_addr,
2533                                 &sinfo) < 0)
2534                         goto out;
2535
2536                 sta_idx++;
2537         }
2538
2539
2540  out:
2541         cb->args[1] = sta_idx;
2542         err = skb->len;
2543  out_err:
2544         nl80211_finish_netdev_dump(dev);
2545
2546         return err;
2547 }
2548
2549 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2550 {
2551         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2552         struct net_device *dev = info->user_ptr[1];
2553         struct station_info sinfo;
2554         struct sk_buff *msg;
2555         u8 *mac_addr = NULL;
2556         int err;
2557
2558         memset(&sinfo, 0, sizeof(sinfo));
2559
2560         if (!info->attrs[NL80211_ATTR_MAC])
2561                 return -EINVAL;
2562
2563         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2564
2565         if (!rdev->ops->get_station)
2566                 return -EOPNOTSUPP;
2567
2568         err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2569         if (err)
2570                 return err;
2571
2572         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2573         if (!msg)
2574                 return -ENOMEM;
2575
2576         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2577                                  rdev, dev, mac_addr, &sinfo) < 0) {
2578                 nlmsg_free(msg);
2579                 return -ENOBUFS;
2580         }
2581
2582         return genlmsg_reply(msg, info);
2583 }
2584
2585 /*
2586  * Get vlan interface making sure it is running and on the right wiphy.
2587  */
2588 static struct net_device *get_vlan(struct genl_info *info,
2589                                    struct cfg80211_registered_device *rdev)
2590 {
2591         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2592         struct net_device *v;
2593         int ret;
2594
2595         if (!vlanattr)
2596                 return NULL;
2597
2598         v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2599         if (!v)
2600                 return ERR_PTR(-ENODEV);
2601
2602         if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2603                 ret = -EINVAL;
2604                 goto error;
2605         }
2606
2607         if (!netif_running(v)) {
2608                 ret = -ENETDOWN;
2609                 goto error;
2610         }
2611
2612         return v;
2613  error:
2614         dev_put(v);
2615         return ERR_PTR(ret);
2616 }
2617
2618 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2619 {
2620         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2621         int err;
2622         struct net_device *dev = info->user_ptr[1];
2623         struct station_parameters params;
2624         u8 *mac_addr = NULL;
2625
2626         memset(&params, 0, sizeof(params));
2627
2628         params.listen_interval = -1;
2629         params.plink_state = -1;
2630
2631         if (info->attrs[NL80211_ATTR_STA_AID])
2632                 return -EINVAL;
2633
2634         if (!info->attrs[NL80211_ATTR_MAC])
2635                 return -EINVAL;
2636
2637         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2638
2639         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2640                 params.supported_rates =
2641                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2642                 params.supported_rates_len =
2643                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2644         }
2645
2646         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2647                 params.listen_interval =
2648                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2649
2650         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2651                 params.ht_capa =
2652                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2653
2654         if (!rdev->ops->change_station)
2655                 return -EOPNOTSUPP;
2656
2657         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2658                 return -EINVAL;
2659
2660         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2661                 params.plink_action =
2662                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2663
2664         if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2665                 params.plink_state =
2666                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2667
2668         switch (dev->ieee80211_ptr->iftype) {
2669         case NL80211_IFTYPE_AP:
2670         case NL80211_IFTYPE_AP_VLAN:
2671         case NL80211_IFTYPE_P2P_GO:
2672                 /* disallow mesh-specific things */
2673                 if (params.plink_action)
2674                         return -EINVAL;
2675
2676                 /* TDLS can't be set, ... */
2677                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2678                         return -EINVAL;
2679                 /*
2680                  * ... but don't bother the driver with it. This works around
2681                  * a hostapd/wpa_supplicant issue -- it always includes the
2682                  * TLDS_PEER flag in the mask even for AP mode.
2683                  */
2684                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2685
2686                 /* accept only the listed bits */
2687                 if (params.sta_flags_mask &
2688                                 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2689                                   BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2690                                   BIT(NL80211_STA_FLAG_WME) |
2691                                   BIT(NL80211_STA_FLAG_MFP)))
2692                         return -EINVAL;
2693
2694                 /* must be last in here for error handling */
2695                 params.vlan = get_vlan(info, rdev);
2696                 if (IS_ERR(params.vlan))
2697                         return PTR_ERR(params.vlan);
2698                 break;
2699         case NL80211_IFTYPE_P2P_CLIENT:
2700         case NL80211_IFTYPE_STATION:
2701                 /*
2702                  * Don't allow userspace to change the TDLS_PEER flag,
2703                  * but silently ignore attempts to change it since we
2704                  * don't have state here to verify that it doesn't try
2705                  * to change the flag.
2706                  */
2707                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2708                 /* fall through */
2709         case NL80211_IFTYPE_ADHOC:
2710                 /* disallow things sta doesn't support */
2711                 if (params.plink_action)
2712                         return -EINVAL;
2713                 if (params.ht_capa)
2714                         return -EINVAL;
2715                 if (params.listen_interval >= 0)
2716                         return -EINVAL;
2717                 /* reject any changes other than AUTHORIZED */
2718                 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2719                         return -EINVAL;
2720                 break;
2721         case NL80211_IFTYPE_MESH_POINT:
2722                 /* disallow things mesh doesn't support */
2723                 if (params.vlan)
2724                         return -EINVAL;
2725                 if (params.ht_capa)
2726                         return -EINVAL;
2727                 if (params.listen_interval >= 0)
2728                         return -EINVAL;
2729                 /*
2730                  * No special handling for TDLS here -- the userspace
2731                  * mesh code doesn't have this bug.
2732                  */
2733                 if (params.sta_flags_mask &
2734                                 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2735                                   BIT(NL80211_STA_FLAG_MFP) |
2736                                   BIT(NL80211_STA_FLAG_AUTHORIZED)))
2737                         return -EINVAL;
2738                 break;
2739         default:
2740                 return -EOPNOTSUPP;
2741         }
2742
2743         /* be aware of params.vlan when changing code here */
2744
2745         err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2746
2747         if (params.vlan)
2748                 dev_put(params.vlan);
2749
2750         return err;
2751 }
2752
2753 static struct nla_policy
2754 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
2755         [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
2756         [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
2757 };
2758
2759 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2760 {
2761         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2762         int err;
2763         struct net_device *dev = info->user_ptr[1];
2764         struct station_parameters params;
2765         u8 *mac_addr = NULL;
2766
2767         memset(&params, 0, sizeof(params));
2768
2769         if (!info->attrs[NL80211_ATTR_MAC])
2770                 return -EINVAL;
2771
2772         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2773                 return -EINVAL;
2774
2775         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2776                 return -EINVAL;
2777
2778         if (!info->attrs[NL80211_ATTR_STA_AID])
2779                 return -EINVAL;
2780
2781         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2782         params.supported_rates =
2783                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2784         params.supported_rates_len =
2785                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2786         params.listen_interval =
2787                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2788
2789         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2790         if (!params.aid || params.aid > IEEE80211_MAX_AID)
2791                 return -EINVAL;
2792
2793         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2794                 params.ht_capa =
2795                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2796
2797         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2798                 params.plink_action =
2799                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2800
2801         if (!rdev->ops->add_station)
2802                 return -EOPNOTSUPP;
2803
2804         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2805                 return -EINVAL;
2806
2807         switch (dev->ieee80211_ptr->iftype) {
2808         case NL80211_IFTYPE_AP:
2809         case NL80211_IFTYPE_AP_VLAN:
2810         case NL80211_IFTYPE_P2P_GO:
2811                 /* parse WME attributes if sta is WME capable */
2812                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2813                     (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
2814                     info->attrs[NL80211_ATTR_STA_WME]) {
2815                         struct nlattr *tb[NL80211_STA_WME_MAX + 1];
2816                         struct nlattr *nla;
2817
2818                         nla = info->attrs[NL80211_ATTR_STA_WME];
2819                         err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
2820                                                nl80211_sta_wme_policy);
2821                         if (err)
2822                                 return err;
2823
2824                         if (tb[NL80211_STA_WME_UAPSD_QUEUES])
2825                                 params.uapsd_queues =
2826                                      nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
2827                         if (params.uapsd_queues &
2828                                         ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
2829                                 return -EINVAL;
2830
2831                         if (tb[NL80211_STA_WME_MAX_SP])
2832                                 params.max_sp =
2833                                      nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
2834
2835                         if (params.max_sp &
2836                                         ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
2837                                 return -EINVAL;
2838
2839                         params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
2840                 }
2841                 /* TDLS peers cannot be added */
2842                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2843                         return -EINVAL;
2844                 /* but don't bother the driver with it */
2845                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2846
2847                 /* must be last in here for error handling */
2848                 params.vlan = get_vlan(info, rdev);
2849                 if (IS_ERR(params.vlan))
2850                         return PTR_ERR(params.vlan);
2851                 break;
2852         case NL80211_IFTYPE_MESH_POINT:
2853                 /* TDLS peers cannot be added */
2854                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2855                         return -EINVAL;
2856                 break;
2857         case NL80211_IFTYPE_STATION:
2858                 /* Only TDLS peers can be added */
2859                 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
2860                         return -EINVAL;
2861                 /* Can only add if TDLS ... */
2862                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
2863                         return -EOPNOTSUPP;
2864                 /* ... with external setup is supported */
2865                 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
2866                         return -EOPNOTSUPP;
2867                 break;
2868         default:
2869                 return -EOPNOTSUPP;
2870         }
2871
2872         /* be aware of params.vlan when changing code here */
2873
2874         err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
2875
2876         if (params.vlan)
2877                 dev_put(params.vlan);
2878         return err;
2879 }
2880
2881 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2882 {
2883         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2884         struct net_device *dev = info->user_ptr[1];
2885         u8 *mac_addr = NULL;
2886
2887         if (info->attrs[NL80211_ATTR_MAC])
2888                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2889
2890         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2891             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
2892             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
2893             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2894                 return -EINVAL;
2895
2896         if (!rdev->ops->del_station)
2897                 return -EOPNOTSUPP;
2898
2899         return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
2900 }
2901
2902 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2903                                 int flags, struct net_device *dev,
2904                                 u8 *dst, u8 *next_hop,
2905                                 struct mpath_info *pinfo)
2906 {
2907         void *hdr;
2908         struct nlattr *pinfoattr;
2909
2910         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2911         if (!hdr)
2912                 return -1;
2913
2914         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2915         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2916         NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2917
2918         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2919
2920         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2921         if (!pinfoattr)
2922                 goto nla_put_failure;
2923         if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2924                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2925                             pinfo->frame_qlen);
2926         if (pinfo->filled & MPATH_INFO_SN)
2927                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2928                             pinfo->sn);
2929         if (pinfo->filled & MPATH_INFO_METRIC)
2930                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2931                             pinfo->metric);
2932         if (pinfo->filled & MPATH_INFO_EXPTIME)
2933                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2934                             pinfo->exptime);
2935         if (pinfo->filled & MPATH_INFO_FLAGS)
2936                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2937                             pinfo->flags);
2938         if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2939                 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2940                             pinfo->discovery_timeout);
2941         if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2942                 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2943                             pinfo->discovery_retries);
2944
2945         nla_nest_end(msg, pinfoattr);
2946
2947         return genlmsg_end(msg, hdr);
2948
2949  nla_put_failure:
2950         genlmsg_cancel(msg, hdr);
2951         return -EMSGSIZE;
2952 }
2953
2954 static int nl80211_dump_mpath(struct sk_buff *skb,
2955                               struct netlink_callback *cb)
2956 {
2957         struct mpath_info pinfo;
2958         struct cfg80211_registered_device *dev;
2959         struct net_device *netdev;
2960         u8 dst[ETH_ALEN];
2961         u8 next_hop[ETH_ALEN];
2962         int path_idx = cb->args[1];
2963         int err;
2964
2965         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2966         if (err)
2967                 return err;
2968
2969         if (!dev->ops->dump_mpath) {
2970                 err = -EOPNOTSUPP;
2971                 goto out_err;
2972         }
2973
2974         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2975                 err = -EOPNOTSUPP;
2976                 goto out_err;
2977         }
2978
2979         while (1) {
2980                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2981                                            dst, next_hop, &pinfo);
2982                 if (err == -ENOENT)
2983                         break;
2984                 if (err)
2985                         goto out_err;
2986
2987                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2988                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
2989                                        netdev, dst, next_hop,
2990                                        &pinfo) < 0)
2991                         goto out;
2992
2993                 path_idx++;
2994         }
2995
2996
2997  out:
2998         cb->args[1] = path_idx;
2999         err = skb->len;
3000  out_err:
3001         nl80211_finish_netdev_dump(dev);
3002         return err;
3003 }
3004
3005 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3006 {
3007         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3008         int err;
3009         struct net_device *dev = info->user_ptr[1];
3010         struct mpath_info pinfo;
3011         struct sk_buff *msg;
3012         u8 *dst = NULL;
3013         u8 next_hop[ETH_ALEN];
3014
3015         memset(&pinfo, 0, sizeof(pinfo));
3016
3017         if (!info->attrs[NL80211_ATTR_MAC])
3018                 return -EINVAL;
3019
3020         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3021
3022         if (!rdev->ops->get_mpath)
3023                 return -EOPNOTSUPP;
3024
3025         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3026                 return -EOPNOTSUPP;
3027
3028         err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
3029         if (err)
3030                 return err;
3031
3032         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3033         if (!msg)
3034                 return -ENOMEM;
3035
3036         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
3037                                  dev, dst, next_hop, &pinfo) < 0) {
3038                 nlmsg_free(msg);
3039                 return -ENOBUFS;
3040         }
3041
3042         return genlmsg_reply(msg, info);
3043 }
3044
3045 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3046 {
3047         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3048         struct net_device *dev = info->user_ptr[1];
3049         u8 *dst = NULL;
3050         u8 *next_hop = NULL;
3051
3052         if (!info->attrs[NL80211_ATTR_MAC])
3053                 return -EINVAL;
3054
3055         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3056                 return -EINVAL;
3057
3058         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3059         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3060
3061         if (!rdev->ops->change_mpath)
3062                 return -EOPNOTSUPP;
3063
3064         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3065                 return -EOPNOTSUPP;
3066
3067         return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
3068 }
3069
3070 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3071 {
3072         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3073         struct net_device *dev = info->user_ptr[1];
3074         u8 *dst = NULL;
3075         u8 *next_hop = NULL;
3076
3077         if (!info->attrs[NL80211_ATTR_MAC])
3078                 return -EINVAL;
3079
3080         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3081                 return -EINVAL;
3082
3083         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3084         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3085
3086         if (!rdev->ops->add_mpath)
3087                 return -EOPNOTSUPP;
3088
3089         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3090                 return -EOPNOTSUPP;
3091
3092         return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
3093 }
3094
3095 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3096 {
3097         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3098         struct net_device *dev = info->user_ptr[1];
3099         u8 *dst = NULL;
3100
3101         if (info->attrs[NL80211_ATTR_MAC])
3102                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3103
3104         if (!rdev->ops->del_mpath)
3105                 return -EOPNOTSUPP;
3106
3107         return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
3108 }
3109
3110 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3111 {
3112         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3113         struct net_device *dev = info->user_ptr[1];
3114         struct bss_parameters params;
3115
3116         memset(&params, 0, sizeof(params));
3117         /* default to not changing parameters */
3118         params.use_cts_prot = -1;
3119         params.use_short_preamble = -1;
3120         params.use_short_slot_time = -1;
3121         params.ap_isolate = -1;
3122         params.ht_opmode = -1;
3123
3124         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3125                 params.use_cts_prot =
3126                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3127         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3128                 params.use_short_preamble =
3129                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3130         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3131                 params.use_short_slot_time =
3132                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
3133         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3134                 params.basic_rates =
3135                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3136                 params.basic_rates_len =
3137                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3138         }
3139         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3140                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
3141         if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3142                 params.ht_opmode =
3143                         nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
3144
3145         if (!rdev->ops->change_bss)
3146                 return -EOPNOTSUPP;
3147
3148         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3149             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3150                 return -EOPNOTSUPP;
3151
3152         return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
3153 }
3154
3155 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
3156         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
3157         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
3158         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
3159         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
3160         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
3161         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
3162 };
3163
3164 static int parse_reg_rule(struct nlattr *tb[],
3165         struct ieee80211_reg_rule *reg_rule)
3166 {
3167         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3168         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3169
3170         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3171                 return -EINVAL;
3172         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3173                 return -EINVAL;
3174         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3175                 return -EINVAL;
3176         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3177                 return -EINVAL;
3178         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3179                 return -EINVAL;
3180
3181         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3182
3183         freq_range->start_freq_khz =
3184                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3185         freq_range->end_freq_khz =
3186                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3187         freq_range->max_bandwidth_khz =
3188                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3189
3190         power_rule->max_eirp =
3191                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3192
3193         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3194                 power_rule->max_antenna_gain =
3195                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3196
3197         return 0;
3198 }
3199
3200 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3201 {
3202         int r;
3203         char *data = NULL;
3204
3205         /*
3206          * You should only get this when cfg80211 hasn't yet initialized
3207          * completely when built-in to the kernel right between the time
3208          * window between nl80211_init() and regulatory_init(), if that is
3209          * even possible.
3210          */
3211         mutex_lock(&cfg80211_mutex);
3212         if (unlikely(!cfg80211_regdomain)) {
3213                 mutex_unlock(&cfg80211_mutex);
3214                 return -EINPROGRESS;
3215         }
3216         mutex_unlock(&cfg80211_mutex);
3217
3218         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3219                 return -EINVAL;
3220
3221         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3222
3223         r = regulatory_hint_user(data);
3224
3225         return r;
3226 }
3227
3228 static int nl80211_get_mesh_config(struct sk_buff *skb,
3229                                    struct genl_info *info)
3230 {
3231         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3232         struct net_device *dev = info->user_ptr[1];
3233         struct wireless_dev *wdev = dev->ieee80211_ptr;
3234         struct mesh_config cur_params;
3235         int err = 0;
3236         void *hdr;
3237         struct nlattr *pinfoattr;
3238         struct sk_buff *msg;
3239
3240         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3241                 return -EOPNOTSUPP;
3242
3243         if (!rdev->ops->get_mesh_config)
3244                 return -EOPNOTSUPP;
3245
3246         wdev_lock(wdev);
3247         /* If not connected, get default parameters */
3248         if (!wdev->mesh_id_len)
3249                 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3250         else
3251                 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
3252                                                  &cur_params);
3253         wdev_unlock(wdev);
3254
3255         if (err)
3256                 return err;
3257
3258         /* Draw up a netlink message to send back */
3259         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3260         if (!msg)
3261                 return -ENOMEM;
3262         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3263                              NL80211_CMD_GET_MESH_CONFIG);
3264         if (!hdr)
3265                 goto out;
3266         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
3267         if (!pinfoattr)
3268                 goto nla_put_failure;
3269         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3270         NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3271                         cur_params.dot11MeshRetryTimeout);
3272         NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3273                         cur_params.dot11MeshConfirmTimeout);
3274         NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3275                         cur_params.dot11MeshHoldingTimeout);
3276         NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3277                         cur_params.dot11MeshMaxPeerLinks);
3278         NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3279                         cur_params.dot11MeshMaxRetries);
3280         NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3281                         cur_params.dot11MeshTTL);
3282         NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3283                         cur_params.element_ttl);
3284         NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3285                         cur_params.auto_open_plinks);
3286         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3287                         cur_params.dot11MeshHWMPmaxPREQretries);
3288         NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3289                         cur_params.path_refresh_time);
3290         NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3291                         cur_params.min_discovery_timeout);
3292         NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3293                         cur_params.dot11MeshHWMPactivePathTimeout);
3294         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3295                         cur_params.dot11MeshHWMPpreqMinInterval);
3296         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3297                         cur_params.dot11MeshHWMPperrMinInterval);
3298         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3299                         cur_params.dot11MeshHWMPnetDiameterTraversalTime);
3300         NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3301                         cur_params.dot11MeshHWMPRootMode);
3302         NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3303                         cur_params.dot11MeshHWMPRannInterval);
3304         NLA_PUT_U8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3305                         cur_params.dot11MeshGateAnnouncementProtocol);
3306         NLA_PUT_U8(msg, NL80211_MESHCONF_FORWARDING,
3307                         cur_params.dot11MeshForwarding);
3308         NLA_PUT_U32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3309                         cur_params.rssi_threshold);
3310         nla_nest_end(msg, pinfoattr);
3311         genlmsg_end(msg, hdr);
3312         return genlmsg_reply(msg, info);
3313
3314  nla_put_failure:
3315         genlmsg_cancel(msg, hdr);
3316  out:
3317         nlmsg_free(msg);
3318         return -ENOBUFS;
3319 }
3320
3321 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
3322         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3323         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3324         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3325         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3326         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3327         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
3328         [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
3329         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3330
3331         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3332         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3333         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3334         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3335         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3336         [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
3337         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3338         [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
3339         [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
3340         [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
3341         [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
3342         [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32},
3343 };
3344
3345 static const struct nla_policy
3346         nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3347         [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3348         [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
3349         [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
3350         [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
3351                 .len = IEEE80211_MAX_DATA_LEN },
3352         [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
3353 };
3354
3355 static int nl80211_parse_mesh_config(struct genl_info *info,
3356                                      struct mesh_config *cfg,
3357                                      u32 *mask_out)
3358 {
3359         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
3360         u32 mask = 0;
3361
3362 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3363 do {\
3364         if (table[attr_num]) {\
3365                 cfg->param = nla_fn(table[attr_num]); \
3366                 mask |= (1 << (attr_num - 1)); \
3367         } \
3368 } while (0);\
3369
3370
3371         if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
3372                 return -EINVAL;
3373         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
3374                              info->attrs[NL80211_ATTR_MESH_CONFIG],
3375                              nl80211_meshconf_params_policy))
3376                 return -EINVAL;
3377
3378         /* This makes sure that there aren't more than 32 mesh config
3379          * parameters (otherwise our bitfield scheme would not work.) */
3380         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3381
3382         /* Fill in the params struct */
3383         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3384                         mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3385         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3386                         mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3387         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3388                         mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3389         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3390                         mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3391         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3392                         mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3393         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3394                         mask, NL80211_MESHCONF_TTL, nla_get_u8);
3395         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3396                         mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
3397         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3398                         mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3399         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3400                         mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3401                         nla_get_u8);
3402         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3403                         mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3404         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3405                         mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3406                         nla_get_u16);
3407         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3408                         mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3409                         nla_get_u32);
3410         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3411                         mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3412                         nla_get_u16);
3413         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3414                         mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3415                         nla_get_u16);
3416         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3417                         dot11MeshHWMPnetDiameterTraversalTime,
3418                         mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3419                         nla_get_u16);
3420         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3421                         dot11MeshHWMPRootMode, mask,
3422                         NL80211_MESHCONF_HWMP_ROOTMODE,
3423                         nla_get_u8);
3424         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3425                         dot11MeshHWMPRannInterval, mask,
3426                         NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3427                         nla_get_u16);
3428         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3429                         dot11MeshGateAnnouncementProtocol, mask,
3430                         NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3431                         nla_get_u8);
3432         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3433                         mask, NL80211_MESHCONF_FORWARDING, nla_get_u8);
3434         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3435                         mask, NL80211_MESHCONF_RSSI_THRESHOLD, nla_get_u32);
3436         if (mask_out)
3437                 *mask_out = mask;
3438
3439         return 0;
3440
3441 #undef FILL_IN_MESH_PARAM_IF_SET
3442 }
3443
3444 static int nl80211_parse_mesh_setup(struct genl_info *info,
3445                                      struct mesh_setup *setup)
3446 {
3447         struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3448
3449         if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3450                 return -EINVAL;
3451         if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3452                              info->attrs[NL80211_ATTR_MESH_SETUP],
3453                              nl80211_mesh_setup_params_policy))
3454                 return -EINVAL;
3455
3456         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3457                 setup->path_sel_proto =
3458                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3459                  IEEE80211_PATH_PROTOCOL_VENDOR :
3460                  IEEE80211_PATH_PROTOCOL_HWMP;
3461
3462         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3463                 setup->path_metric =
3464                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3465                  IEEE80211_PATH_METRIC_VENDOR :
3466                  IEEE80211_PATH_METRIC_AIRTIME;
3467
3468
3469         if (tb[NL80211_MESH_SETUP_IE]) {
3470                 struct nlattr *ieattr =
3471                         tb[NL80211_MESH_SETUP_IE];
3472                 if (!is_valid_ie_attr(ieattr))
3473                         return -EINVAL;
3474                 setup->ie = nla_data(ieattr);
3475                 setup->ie_len = nla_len(ieattr);
3476         }
3477         setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3478         setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3479
3480         return 0;
3481 }
3482
3483 static int nl80211_update_mesh_config(struct sk_buff *skb,
3484                                       struct genl_info *info)
3485 {
3486         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3487         struct net_device *dev = info->user_ptr[1];
3488         struct wireless_dev *wdev = dev->ieee80211_ptr;
3489         struct mesh_config cfg;
3490         u32 mask;
3491         int err;
3492
3493         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3494                 return -EOPNOTSUPP;
3495
3496         if (!rdev->ops->update_mesh_config)
3497                 return -EOPNOTSUPP;
3498
3499         err = nl80211_parse_mesh_config(info, &cfg, &mask);
3500         if (err)
3501                 return err;
3502
3503         wdev_lock(wdev);
3504         if (!wdev->mesh_id_len)
3505                 err = -ENOLINK;
3506
3507         if (!err)
3508                 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3509                                                     mask, &cfg);
3510
3511         wdev_unlock(wdev);
3512
3513         return err;
3514 }
3515
3516 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3517 {
3518         struct sk_buff *msg;
3519         void *hdr = NULL;
3520         struct nlattr *nl_reg_rules;
3521         unsigned int i;
3522         int err = -EINVAL;
3523
3524         mutex_lock(&cfg80211_mutex);
3525
3526         if (!cfg80211_regdomain)
3527                 goto out;
3528
3529         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3530         if (!msg) {
3531                 err = -ENOBUFS;
3532                 goto out;
3533         }
3534
3535         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3536                              NL80211_CMD_GET_REG);
3537         if (!hdr)
3538                 goto put_failure;
3539
3540         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3541                 cfg80211_regdomain->alpha2);
3542         if (cfg80211_regdomain->dfs_region)
3543                 NLA_PUT_U8(msg, NL80211_ATTR_DFS_REGION,
3544                            cfg80211_regdomain->dfs_region);
3545
3546         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3547         if (!nl_reg_rules)
3548                 goto nla_put_failure;
3549
3550         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3551                 struct nlattr *nl_reg_rule;
3552                 const struct ieee80211_reg_rule *reg_rule;
3553                 const struct ieee80211_freq_range *freq_range;
3554                 const struct ieee80211_power_rule *power_rule;
3555
3556                 reg_rule = &cfg80211_regdomain->reg_rules[i];
3557                 freq_range = &reg_rule->freq_range;
3558                 power_rule = &reg_rule->power_rule;
3559
3560                 nl_reg_rule = nla_nest_start(msg, i);
3561                 if (!nl_reg_rule)
3562                         goto nla_put_failure;
3563
3564                 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3565                         reg_rule->flags);
3566                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3567                         freq_range->start_freq_khz);
3568                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3569                         freq_range->end_freq_khz);
3570                 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3571                         freq_range->max_bandwidth_khz);
3572                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3573                         power_rule->max_antenna_gain);
3574                 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3575                         power_rule->max_eirp);
3576
3577                 nla_nest_end(msg, nl_reg_rule);
3578         }
3579
3580         nla_nest_end(msg, nl_reg_rules);
3581
3582         genlmsg_end(msg, hdr);
3583         err = genlmsg_reply(msg, info);
3584         goto out;
3585
3586 nla_put_failure:
3587         genlmsg_cancel(msg, hdr);
3588 put_failure:
3589         nlmsg_free(msg);
3590         err = -EMSGSIZE;
3591 out:
3592         mutex_unlock(&cfg80211_mutex);
3593         return err;
3594 }
3595
3596 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3597 {
3598         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3599         struct nlattr *nl_reg_rule;
3600         char *alpha2 = NULL;
3601         int rem_reg_rules = 0, r = 0;
3602         u32 num_rules = 0, rule_idx = 0, size_of_regd;
3603         u8 dfs_region = 0;
3604         struct ieee80211_regdomain *rd = NULL;
3605
3606         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3607                 return -EINVAL;
3608
3609         if (!info->attrs[NL80211_ATTR_REG_RULES])
3610                 return -EINVAL;
3611
3612         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3613
3614         if (info->attrs[NL80211_ATTR_DFS_REGION])
3615                 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3616
3617         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3618                         rem_reg_rules) {
3619                 num_rules++;
3620                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3621                         return -EINVAL;
3622         }
3623
3624         mutex_lock(&cfg80211_mutex);
3625
3626         if (!reg_is_valid_request(alpha2)) {
3627                 r = -EINVAL;
3628                 goto bad_reg;
3629         }
3630
3631         size_of_regd = sizeof(struct ieee80211_regdomain) +
3632                 (num_rules * sizeof(struct ieee80211_reg_rule));
3633
3634         rd = kzalloc(size_of_regd, GFP_KERNEL);
3635         if (!rd) {
3636                 r = -ENOMEM;
3637                 goto bad_reg;
3638         }
3639
3640         rd->n_reg_rules = num_rules;
3641         rd->alpha2[0] = alpha2[0];
3642         rd->alpha2[1] = alpha2[1];
3643
3644         /*
3645          * Disable DFS master mode if the DFS region was
3646          * not supported or known on this kernel.
3647          */
3648         if (reg_supported_dfs_region(dfs_region))
3649                 rd->dfs_region = dfs_region;
3650
3651         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3652                         rem_reg_rules) {
3653                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3654                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3655                         reg_rule_policy);
3656                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3657                 if (r)
3658                         goto bad_reg;
3659
3660                 rule_idx++;
3661
3662                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3663                         r = -EINVAL;
3664                         goto bad_reg;
3665                 }
3666         }
3667
3668         BUG_ON(rule_idx != num_rules);
3669
3670         r = set_regdom(rd);
3671
3672         mutex_unlock(&cfg80211_mutex);
3673
3674         return r;
3675
3676  bad_reg:
3677         mutex_unlock(&cfg80211_mutex);
3678         kfree(rd);
3679         return r;
3680 }
3681
3682 static int validate_scan_freqs(struct nlattr *freqs)
3683 {
3684         struct nlattr *attr1, *attr2;
3685         int n_channels = 0, tmp1, tmp2;
3686
3687         nla_for_each_nested(attr1, freqs, tmp1) {
3688                 n_channels++;
3689                 /*
3690                  * Some hardware has a limited channel list for
3691                  * scanning, and it is pretty much nonsensical
3692                  * to scan for a channel twice, so disallow that
3693                  * and don't require drivers to check that the
3694                  * channel list they get isn't longer than what
3695                  * they can scan, as long as they can scan all
3696                  * the channels they registered at once.
3697                  */
3698                 nla_for_each_nested(attr2, freqs, tmp2)
3699                         if (attr1 != attr2 &&
3700                             nla_get_u32(attr1) == nla_get_u32(attr2))
3701                                 return 0;
3702         }
3703
3704         return n_channels;
3705 }
3706
3707 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3708 {
3709         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3710         struct net_device *dev = info->user_ptr[1];
3711         struct cfg80211_scan_request *request;
3712         struct nlattr *attr;
3713         struct wiphy *wiphy;
3714         int err, tmp, n_ssids = 0, n_channels, i;
3715         size_t ie_len;
3716
3717         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3718                 return -EINVAL;
3719
3720         wiphy = &rdev->wiphy;
3721
3722         if (!rdev->ops->scan)
3723                 return -EOPNOTSUPP;
3724
3725         if (rdev->scan_req)
3726                 return -EBUSY;
3727
3728         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3729                 n_channels = validate_scan_freqs(
3730                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3731                 if (!n_channels)
3732                         return -EINVAL;
3733         } else {
3734                 enum ieee80211_band band;
3735                 n_channels = 0;
3736
3737                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3738                         if (wiphy->bands[band])
3739                                 n_channels += wiphy->bands[band]->n_channels;
3740         }
3741
3742         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3743                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3744                         n_ssids++;
3745
3746         if (n_ssids > wiphy->max_scan_ssids)
3747                 return -EINVAL;
3748
3749         if (info->attrs[NL80211_ATTR_IE])
3750                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3751         else
3752                 ie_len = 0;
3753
3754         if (ie_len > wiphy->max_scan_ie_len)
3755                 return -EINVAL;
3756
3757         request = kzalloc(sizeof(*request)
3758                         + sizeof(*request->ssids) * n_ssids
3759                         + sizeof(*request->channels) * n_channels
3760                         + ie_len, GFP_KERNEL);
3761         if (!request)
3762                 return -ENOMEM;
3763
3764         if (n_ssids)
3765                 request->ssids = (void *)&request->channels[n_channels];
3766         request->n_ssids = n_ssids;
3767         if (ie_len) {
3768                 if (request->ssids)
3769                         request->ie = (void *)(request->ssids + n_ssids);
3770                 else
3771                         request->ie = (void *)(request->channels + n_channels);
3772         }
3773
3774         i = 0;
3775         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3776                 /* user specified, bail out if channel not found */
3777                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
3778                         struct ieee80211_channel *chan;
3779
3780                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3781
3782                         if (!chan) {
3783                                 err = -EINVAL;
3784                                 goto out_free;
3785                         }
3786
3787                         /* ignore disabled channels */
3788                         if (chan->flags & IEEE80211_CHAN_DISABLED)
3789                                 continue;
3790
3791                         request->channels[i] = chan;
3792                         i++;
3793                 }
3794         } else {
3795                 enum ieee80211_band band;
3796
3797                 /* all channels */
3798                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3799                         int j;
3800                         if (!wiphy->bands[band])
3801                                 continue;
3802                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3803                                 struct ieee80211_channel *chan;
3804
3805                                 chan = &wiphy->bands[band]->channels[j];
3806
3807                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
3808                                         continue;
3809
3810                                 request->channels[i] = chan;
3811                                 i++;
3812                         }
3813                 }
3814         }
3815
3816         if (!i) {
3817                 err = -EINVAL;
3818                 goto out_free;
3819         }
3820
3821         request->n_channels = i;
3822
3823         i = 0;
3824         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3825                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3826                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
3827                                 err = -EINVAL;
3828                                 goto out_free;
3829                         }
3830                         request->ssids[i].ssid_len = nla_len(attr);
3831                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3832                         i++;
3833                 }
3834         }
3835
3836         if (info->attrs[NL80211_ATTR_IE]) {
3837                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3838                 memcpy((void *)request->ie,
3839                        nla_data(info->attrs[NL80211_ATTR_IE]),
3840                        request->ie_len);
3841         }
3842
3843         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
3844                 if (wiphy->bands[i])
3845                         request->rates[i] =
3846                                 (1 << wiphy->bands[i]->n_bitrates) - 1;
3847
3848         if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3849                 nla_for_each_nested(attr,
3850                                     info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3851                                     tmp) {
3852                         enum ieee80211_band band = nla_type(attr);
3853
3854                         if (band < 0 || band >= IEEE80211_NUM_BANDS) {
3855                                 err = -EINVAL;
3856                                 goto out_free;
3857                         }
3858                         err = ieee80211_get_ratemask(wiphy->bands[band],
3859                                                      nla_data(attr),
3860                                                      nla_len(attr),
3861                                                      &request->rates[band]);
3862                         if (err)
3863                                 goto out_free;
3864                 }
3865         }
3866
3867         request->no_cck =
3868                 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
3869
3870         request->dev = dev;
3871         request->wiphy = &rdev->wiphy;
3872
3873         rdev->scan_req = request;
3874         err = rdev->ops->scan(&rdev->wiphy, dev, request);
3875
3876         if (!err) {
3877                 nl80211_send_scan_start(rdev, dev);
3878                 dev_hold(dev);
3879         } else {
3880  out_free:
3881                 rdev->scan_req = NULL;
3882                 kfree(request);
3883         }
3884
3885         return err;
3886 }
3887
3888 static int nl80211_start_sched_scan(struct sk_buff *skb,
3889                                     struct genl_info *info)
3890 {
3891         struct cfg80211_sched_scan_request *request;
3892         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3893         struct net_device *dev = info->user_ptr[1];
3894         struct nlattr *attr;
3895         struct wiphy *wiphy;
3896         int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
3897         u32 interval;
3898         enum ieee80211_band band;
3899         size_t ie_len;
3900         struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
3901
3902         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3903             !rdev->ops->sched_scan_start)
3904                 return -EOPNOTSUPP;
3905
3906         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3907                 return -EINVAL;
3908
3909         if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3910                 return -EINVAL;
3911
3912         interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3913         if (interval == 0)
3914                 return -EINVAL;
3915
3916         wiphy = &rdev->wiphy;
3917
3918         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3919                 n_channels = validate_scan_freqs(
3920                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3921                 if (!n_channels)
3922                         return -EINVAL;
3923         } else {
3924                 n_channels = 0;
3925
3926                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3927                         if (wiphy->bands[band])
3928                                 n_channels += wiphy->bands[band]->n_channels;
3929         }
3930
3931         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3932                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3933                                     tmp)
3934                         n_ssids++;
3935
3936         if (n_ssids > wiphy->max_sched_scan_ssids)
3937                 return -EINVAL;
3938
3939         if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
3940                 nla_for_each_nested(attr,
3941                                     info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
3942                                     tmp)
3943                         n_match_sets++;
3944
3945         if (n_match_sets > wiphy->max_match_sets)
3946                 return -EINVAL;
3947
3948         if (info->attrs[NL80211_ATTR_IE])
3949                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3950         else
3951                 ie_len = 0;
3952
3953         if (ie_len > wiphy->max_sched_scan_ie_len)
3954                 return -EINVAL;
3955
3956         mutex_lock(&rdev->sched_scan_mtx);
3957
3958         if (rdev->sched_scan_req) {
3959                 err = -EINPROGRESS;
3960                 goto out;
3961         }
3962
3963         request = kzalloc(sizeof(*request)
3964                         + sizeof(*request->ssids) * n_ssids
3965                         + sizeof(*request->match_sets) * n_match_sets
3966                         + sizeof(*request->channels) * n_channels
3967                         + ie_len, GFP_KERNEL);
3968         if (!request) {
3969                 err = -ENOMEM;
3970                 goto out;
3971         }
3972
3973         if (n_ssids)
3974                 request->ssids = (void *)&request->channels[n_channels];
3975         request->n_ssids = n_ssids;
3976         if (ie_len) {
3977                 if (request->ssids)
3978                         request->ie = (void *)(request->ssids + n_ssids);
3979                 else
3980                         request->ie = (void *)(request->channels + n_channels);
3981         }
3982
3983         if (n_match_sets) {
3984                 if (request->ie)
3985                         request->match_sets = (void *)(request->ie + ie_len);
3986                 else if (request->ssids)
3987                         request->match_sets =
3988                                 (void *)(request->ssids + n_ssids);
3989                 else
3990                         request->match_sets =
3991                                 (void *)(request->channels + n_channels);
3992         }
3993         request->n_match_sets = n_match_sets;
3994
3995         i = 0;
3996         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3997                 /* user specified, bail out if channel not found */
3998                 nla_for_each_nested(attr,
3999                                     info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4000                                     tmp) {
4001                         struct ieee80211_channel *chan;
4002
4003                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4004
4005                         if (!chan) {
4006                                 err = -EINVAL;
4007                                 goto out_free;
4008                         }
4009
4010                         /* ignore disabled channels */
4011                         if (chan->flags & IEEE80211_CHAN_DISABLED)
4012                                 continue;
4013
4014                         request->channels[i] = chan;
4015                         i++;
4016                 }
4017         } else {
4018                 /* all channels */
4019                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4020                         int j;
4021                         if (!wiphy->bands[band])
4022                                 continue;
4023                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4024                                 struct ieee80211_channel *chan;
4025
4026                                 chan = &wiphy->bands[band]->channels[j];
4027
4028                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
4029                                         continue;
4030
4031                                 request->channels[i] = chan;
4032                                 i++;
4033                         }
4034                 }
4035         }
4036
4037         if (!i) {
4038                 err = -EINVAL;
4039                 goto out_free;
4040         }
4041
4042         request->n_channels = i;
4043
4044         i = 0;
4045         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4046                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4047                                     tmp) {
4048                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
4049                                 err = -EINVAL;
4050                                 goto out_free;
4051                         }
4052                         request->ssids[i].ssid_len = nla_len(attr);
4053                         memcpy(request->ssids[i].ssid, nla_data(attr),
4054                                nla_len(attr));
4055                         i++;
4056                 }
4057         }
4058
4059         i = 0;
4060         if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4061                 nla_for_each_nested(attr,
4062                                     info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4063                                     tmp) {
4064                         struct nlattr *ssid;
4065
4066                         nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4067                                   nla_data(attr), nla_len(attr),
4068                                   nl80211_match_policy);
4069                         ssid = tb[NL80211_ATTR_SCHED_SCAN_MATCH_SSID];
4070                         if (ssid) {
4071                                 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4072                                         err = -EINVAL;
4073                                         goto out_free;
4074                                 }
4075                                 memcpy(request->match_sets[i].ssid.ssid,
4076                                        nla_data(ssid), nla_len(ssid));
4077                                 request->match_sets[i].ssid.ssid_len =
4078                                         nla_len(ssid);
4079                         }
4080                         i++;
4081                 }
4082         }
4083
4084         if (info->attrs[NL80211_ATTR_IE]) {
4085                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4086                 memcpy((void *)request->ie,
4087                        nla_data(info->attrs[NL80211_ATTR_IE]),
4088                        request->ie_len);
4089         }
4090
4091         request->dev = dev;
4092         request->wiphy = &rdev->wiphy;
4093         request->interval = interval;
4094
4095         err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4096         if (!err) {
4097                 rdev->sched_scan_req = request;
4098                 nl80211_send_sched_scan(rdev, dev,
4099                                         NL80211_CMD_START_SCHED_SCAN);
4100                 goto out;
4101         }
4102
4103 out_free:
4104         kfree(request);
4105 out:
4106         mutex_unlock(&rdev->sched_scan_mtx);
4107         return err;
4108 }
4109
4110 static int nl80211_stop_sched_scan(struct sk_buff *skb,
4111                                    struct genl_info *info)
4112 {
4113         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4114         int err;
4115
4116         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4117             !rdev->ops->sched_scan_stop)
4118                 return -EOPNOTSUPP;
4119
4120         mutex_lock(&rdev->sched_scan_mtx);
4121         err = __cfg80211_stop_sched_scan(rdev, false);
4122         mutex_unlock(&rdev->sched_scan_mtx);
4123
4124         return err;
4125 }
4126
4127 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4128                             u32 seq, int flags,
4129                             struct cfg80211_registered_device *rdev,
4130                             struct wireless_dev *wdev,
4131                             struct cfg80211_internal_bss *intbss)
4132 {
4133         struct cfg80211_bss *res = &intbss->pub;
4134         void *hdr;
4135         struct nlattr *bss;
4136
4137         ASSERT_WDEV_LOCK(wdev);
4138
4139         hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
4140                              NL80211_CMD_NEW_SCAN_RESULTS);
4141         if (!hdr)
4142                 return -1;
4143
4144         genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4145
4146         NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
4147         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
4148
4149         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4150         if (!bss)
4151                 goto nla_put_failure;
4152         if (!is_zero_ether_addr(res->bssid))
4153                 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
4154         if (res->information_elements && res->len_information_elements)
4155                 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4156                         res->len_information_elements,
4157                         res->information_elements);
4158         if (res->beacon_ies && res->len_beacon_ies &&
4159             res->beacon_ies != res->information_elements)
4160                 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
4161                         res->len_beacon_ies, res->beacon_ies);
4162         if (res->tsf)
4163                 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
4164         if (res->beacon_interval)
4165                 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
4166         NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
4167         NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
4168         NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
4169                 jiffies_to_msecs(jiffies - intbss->ts));
4170
4171         switch (rdev->wiphy.signal_type) {
4172         case CFG80211_SIGNAL_TYPE_MBM:
4173                 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
4174                 break;
4175         case CFG80211_SIGNAL_TYPE_UNSPEC:
4176                 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
4177                 break;
4178         default:
4179                 break;
4180         }
4181
4182         switch (wdev->iftype) {
4183         case NL80211_IFTYPE_P2P_CLIENT:
4184         case NL80211_IFTYPE_STATION:
4185                 if (intbss == wdev->current_bss)
4186                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4187                                     NL80211_BSS_STATUS_ASSOCIATED);
4188                 break;
4189         case NL80211_IFTYPE_ADHOC:
4190                 if (intbss == wdev->current_bss)
4191                         NLA_PUT_U32(msg, NL80211_BSS_STATUS,
4192                                     NL80211_BSS_STATUS_IBSS_JOINED);
4193                 break;
4194         default:
4195                 break;
4196         }
4197
4198         nla_nest_end(msg, bss);
4199
4200         return genlmsg_end(msg, hdr);
4201
4202  nla_put_failure:
4203         genlmsg_cancel(msg, hdr);
4204         return -EMSGSIZE;
4205 }
4206
4207 static int nl80211_dump_scan(struct sk_buff *skb,
4208                              struct netlink_callback *cb)
4209 {
4210         struct cfg80211_registered_device *rdev;
4211         struct net_device *dev;
4212         struct cfg80211_internal_bss *scan;
4213         struct wireless_dev *wdev;
4214         int start = cb->args[1], idx = 0;
4215         int err;
4216
4217         err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4218         if (err)
4219                 return err;
4220
4221         wdev = dev->ieee80211_ptr;
4222
4223         wdev_lock(wdev);
4224         spin_lock_bh(&rdev->bss_lock);
4225         cfg80211_bss_expire(rdev);
4226
4227         cb->seq = rdev->bss_generation;
4228
4229         list_for_each_entry(scan, &rdev->bss_list, list) {
4230                 if (++idx <= start)
4231                         continue;
4232                 if (nl80211_send_bss(skb, cb,
4233                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4234                                 rdev, wdev, scan) < 0) {
4235                         idx--;
4236                         break;
4237                 }
4238         }
4239
4240         spin_unlock_bh(&rdev->bss_lock);
4241         wdev_unlock(wdev);
4242
4243         cb->args[1] = idx;
4244         nl80211_finish_netdev_dump(rdev);
4245
4246         return skb->len;
4247 }
4248
4249 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4250                                 int flags, struct net_device *dev,
4251                                 struct survey_info *survey)
4252 {
4253         void *hdr;
4254         struct nlattr *infoattr;
4255
4256         hdr = nl80211hdr_put(msg, pid, seq, flags,
4257                              NL80211_CMD_NEW_SURVEY_RESULTS);
4258         if (!hdr)
4259                 return -ENOMEM;
4260
4261         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
4262
4263         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4264         if (!infoattr)
4265                 goto nla_put_failure;
4266
4267         NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4268                     survey->channel->center_freq);
4269         if (survey->filled & SURVEY_INFO_NOISE_DBM)
4270                 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
4271                             survey->noise);
4272         if (survey->filled & SURVEY_INFO_IN_USE)
4273                 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
4274         if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
4275                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4276                             survey->channel_time);
4277         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
4278                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4279                             survey->channel_time_busy);
4280         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
4281                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4282                             survey->channel_time_ext_busy);
4283         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
4284                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4285                             survey->channel_time_rx);
4286         if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
4287                 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4288                             survey->channel_time_tx);
4289
4290         nla_nest_end(msg, infoattr);
4291
4292         return genlmsg_end(msg, hdr);
4293
4294  nla_put_failure:
4295         genlmsg_cancel(msg, hdr);
4296         return -EMSGSIZE;
4297 }
4298
4299 static int nl80211_dump_survey(struct sk_buff *skb,
4300                         struct netlink_callback *cb)
4301 {
4302         struct survey_info survey;
4303         struct cfg80211_registered_device *dev;
4304         struct net_device *netdev;
4305         int survey_idx = cb->args[1];
4306         int res;
4307
4308         res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4309         if (res)
4310                 return res;
4311
4312         if (!dev->ops->dump_survey) {
4313                 res = -EOPNOTSUPP;
4314                 goto out_err;
4315         }
4316
4317         while (1) {
4318                 struct ieee80211_channel *chan;
4319
4320                 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4321                                             &survey);
4322                 if (res == -ENOENT)
4323                         break;
4324                 if (res)
4325                         goto out_err;
4326
4327                 /* Survey without a channel doesn't make sense */
4328                 if (!survey.channel) {
4329                         res = -EINVAL;
4330                         goto out;
4331                 }
4332
4333                 chan = ieee80211_get_channel(&dev->wiphy,
4334                                              survey.channel->center_freq);
4335                 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4336                         survey_idx++;
4337                         continue;
4338                 }
4339
4340                 if (nl80211_send_survey(skb,
4341                                 NETLINK_CB(cb->skb).pid,
4342                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4343                                 netdev,
4344                                 &survey) < 0)
4345                         goto out;
4346                 survey_idx++;
4347         }
4348
4349  out:
4350         cb->args[1] = survey_idx;
4351         res = skb->len;
4352  out_err:
4353         nl80211_finish_netdev_dump(dev);
4354         return res;
4355 }
4356
4357 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4358 {
4359         return auth_type <= NL80211_AUTHTYPE_MAX;
4360 }
4361
4362 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4363 {
4364         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4365                                   NL80211_WPA_VERSION_2));
4366 }
4367
4368 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4369 {
4370         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4371         struct net_device *dev = info->user_ptr[1];
4372         struct ieee80211_channel *chan;
4373         const u8 *bssid, *ssid, *ie = NULL;
4374         int err, ssid_len, ie_len = 0;
4375         enum nl80211_auth_type auth_type;
4376         struct key_parse key;
4377         bool local_state_change;
4378
4379         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4380                 return -EINVAL;
4381
4382         if (!info->attrs[NL80211_ATTR_MAC])
4383                 return -EINVAL;
4384
4385         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4386                 return -EINVAL;
4387
4388         if (!info->attrs[NL80211_ATTR_SSID])
4389                 return -EINVAL;
4390
4391         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4392                 return -EINVAL;
4393
4394         err = nl80211_parse_key(info, &key);
4395         if (err)
4396                 return err;
4397
4398         if (key.idx >= 0) {
4399                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4400                         return -EINVAL;
4401                 if (!key.p.key || !key.p.key_len)
4402                         return -EINVAL;
4403                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4404                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4405                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4406                      key.p.key_len != WLAN_KEY_LEN_WEP104))
4407                         return -EINVAL;
4408                 if (key.idx > 4)
4409                         return -EINVAL;
4410         } else {
4411                 key.p.key_len = 0;
4412                 key.p.key = NULL;
4413         }
4414
4415         if (key.idx >= 0) {
4416                 int i;
4417                 bool ok = false;
4418                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4419                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4420                                 ok = true;
4421                                 break;
4422                         }
4423                 }
4424                 if (!ok)
4425                         return -EINVAL;
4426         }
4427
4428         if (!rdev->ops->auth)
4429                 return -EOPNOTSUPP;
4430
4431         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4432             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4433                 return -EOPNOTSUPP;
4434
4435         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4436         chan = ieee80211_get_channel(&rdev->wiphy,
4437                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4438         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4439                 return -EINVAL;
4440
4441         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4442         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4443
4444         if (info->attrs[NL80211_ATTR_IE]) {
4445                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4446                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4447         }
4448
4449         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4450         if (!nl80211_valid_auth_type(auth_type))
4451                 return -EINVAL;
4452
4453         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4454
4455         /*
4456          * Since we no longer track auth state, ignore
4457          * requests to only change local state.
4458          */
4459         if (local_state_change)
4460                 return 0;
4461
4462         return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4463                                   ssid, ssid_len, ie, ie_len,
4464                                   key.p.key, key.p.key_len, key.idx);
4465 }
4466
4467 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4468                                    struct genl_info *info,
4469                                    struct cfg80211_crypto_settings *settings,
4470                                    int cipher_limit)
4471 {
4472         memset(settings, 0, sizeof(*settings));
4473
4474         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4475
4476         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4477                 u16 proto;
4478                 proto = nla_get_u16(
4479                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4480                 settings->control_port_ethertype = cpu_to_be16(proto);
4481                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4482                     proto != ETH_P_PAE)
4483                         return -EINVAL;
4484                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4485                         settings->control_port_no_encrypt = true;
4486         } else
4487                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4488
4489         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4490                 void *data;
4491                 int len, i;
4492
4493                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4494                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4495                 settings->n_ciphers_pairwise = len / sizeof(u32);
4496
4497                 if (len % sizeof(u32))
4498                         return -EINVAL;
4499
4500                 if (settings->n_ciphers_pairwise > cipher_limit)
4501                         return -EINVAL;
4502
4503                 memcpy(settings->ciphers_pairwise, data, len);
4504
4505                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4506                         if (!cfg80211_supported_cipher_suite(
4507                                         &rdev->wiphy,
4508                                         settings->ciphers_pairwise[i]))
4509                                 return -EINVAL;
4510         }
4511
4512         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4513                 settings->cipher_group =
4514                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4515                 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4516                                                      settings->cipher_group))
4517                         return -EINVAL;
4518         }
4519
4520         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4521                 settings->wpa_versions =
4522                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4523                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4524                         return -EINVAL;
4525         }
4526
4527         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4528                 void *data;
4529                 int len;
4530
4531                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4532                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4533                 settings->n_akm_suites = len / sizeof(u32);
4534
4535                 if (len % sizeof(u32))
4536                         return -EINVAL;
4537
4538                 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4539                         return -EINVAL;
4540
4541                 memcpy(settings->akm_suites, data, len);
4542         }
4543
4544         return 0;
4545 }
4546
4547 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4548 {
4549         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4550         struct net_device *dev = info->user_ptr[1];
4551         struct cfg80211_crypto_settings crypto;
4552         struct ieee80211_channel *chan;
4553         const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4554         int err, ssid_len, ie_len = 0;
4555         bool use_mfp = false;
4556         u32 flags = 0;
4557         struct ieee80211_ht_cap *ht_capa = NULL;
4558         struct ieee80211_ht_cap *ht_capa_mask = NULL;
4559
4560         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4561                 return -EINVAL;
4562
4563         if (!info->attrs[NL80211_ATTR_MAC] ||
4564             !info->attrs[NL80211_ATTR_SSID] ||
4565             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4566                 return -EINVAL;
4567
4568         if (!rdev->ops->assoc)
4569                 return -EOPNOTSUPP;
4570
4571         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4572             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4573                 return -EOPNOTSUPP;
4574
4575         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4576
4577         chan = ieee80211_get_channel(&rdev->wiphy,
4578                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4579         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4580                 return -EINVAL;
4581
4582         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4583         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4584
4585         if (info->attrs[NL80211_ATTR_IE]) {
4586                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4587                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4588         }
4589
4590         if (info->attrs[NL80211_ATTR_USE_MFP]) {
4591                 enum nl80211_mfp mfp =
4592                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4593                 if (mfp == NL80211_MFP_REQUIRED)
4594                         use_mfp = true;
4595                 else if (mfp != NL80211_MFP_NO)
4596                         return -EINVAL;
4597         }
4598
4599         if (info->attrs[NL80211_ATTR_PREV_BSSID])
4600                 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4601
4602         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4603                 flags |= ASSOC_REQ_DISABLE_HT;
4604
4605         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4606                 ht_capa_mask =
4607                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4608
4609         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4610                 if (!ht_capa_mask)
4611                         return -EINVAL;
4612                 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4613         }
4614
4615         err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4616         if (!err)
4617                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4618                                           ssid, ssid_len, ie, ie_len, use_mfp,
4619                                           &crypto, flags, ht_capa,
4620                                           ht_capa_mask);
4621
4622         return err;
4623 }
4624
4625 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4626 {
4627         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4628         struct net_device *dev = info->user_ptr[1];
4629         const u8 *ie = NULL, *bssid;
4630         int ie_len = 0;
4631         u16 reason_code;
4632         bool local_state_change;
4633
4634         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4635                 return -EINVAL;
4636
4637         if (!info->attrs[NL80211_ATTR_MAC])
4638                 return -EINVAL;
4639
4640         if (!info->attrs[NL80211_ATTR_REASON_CODE])
4641                 return -EINVAL;
4642
4643         if (!rdev->ops->deauth)
4644                 return -EOPNOTSUPP;
4645
4646         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4647             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4648                 return -EOPNOTSUPP;
4649
4650         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4651
4652         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4653         if (reason_code == 0) {
4654                 /* Reason Code 0 is reserved */
4655                 return -EINVAL;
4656         }
4657
4658         if (info->attrs[NL80211_ATTR_IE]) {
4659                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4660                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4661         }
4662
4663         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4664
4665         return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4666                                     local_state_change);
4667 }
4668
4669 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4670 {
4671         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4672         struct net_device *dev = info->user_ptr[1];
4673         const u8 *ie = NULL, *bssid;
4674         int ie_len = 0;
4675         u16 reason_code;
4676         bool local_state_change;
4677
4678         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4679                 return -EINVAL;
4680
4681         if (!info->attrs[NL80211_ATTR_MAC])
4682                 return -EINVAL;
4683
4684         if (!info->attrs[NL80211_ATTR_REASON_CODE])
4685                 return -EINVAL;
4686
4687         if (!rdev->ops->disassoc)
4688                 return -EOPNOTSUPP;
4689
4690         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4691             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4692                 return -EOPNOTSUPP;
4693
4694         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4695
4696         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4697         if (reason_code == 0) {
4698                 /* Reason Code 0 is reserved */
4699                 return -EINVAL;
4700         }
4701
4702         if (info->attrs[NL80211_ATTR_IE]) {
4703                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4704                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4705         }
4706
4707         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4708
4709         return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4710                                       local_state_change);
4711 }
4712
4713 static bool
4714 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4715                          int mcast_rate[IEEE80211_NUM_BANDS],
4716                          int rateval)
4717 {
4718         struct wiphy *wiphy = &rdev->wiphy;
4719         bool found = false;
4720         int band, i;
4721
4722         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4723                 struct ieee80211_supported_band *sband;
4724
4725                 sband = wiphy->bands[band];
4726                 if (!sband)
4727                         continue;
4728
4729                 for (i = 0; i < sband->n_bitrates; i++) {
4730                         if (sband->bitrates[i].bitrate == rateval) {
4731                                 mcast_rate[band] = i + 1;
4732                                 found = true;
4733                                 break;
4734                         }
4735                 }
4736         }
4737
4738         return found;
4739 }
4740
4741 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4742 {
4743         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4744         struct net_device *dev = info->user_ptr[1];
4745         struct cfg80211_ibss_params ibss;
4746         struct wiphy *wiphy;
4747         struct cfg80211_cached_keys *connkeys = NULL;
4748         int err;
4749
4750         memset(&ibss, 0, sizeof(ibss));
4751
4752         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4753                 return -EINVAL;
4754
4755         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4756             !info->attrs[NL80211_ATTR_SSID] ||
4757             !nla_len(info->attrs[NL80211_ATTR_SSID]))
4758                 return -EINVAL;
4759
4760         ibss.beacon_interval = 100;
4761
4762         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4763                 ibss.beacon_interval =
4764                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4765                 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4766                         return -EINVAL;
4767         }
4768
4769         if (!rdev->ops->join_ibss)
4770                 return -EOPNOTSUPP;
4771
4772         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4773                 return -EOPNOTSUPP;
4774
4775         wiphy = &rdev->wiphy;
4776
4777         if (info->attrs[NL80211_ATTR_MAC]) {
4778                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4779
4780                 if (!is_valid_ether_addr(ibss.bssid))
4781                         return -EINVAL;
4782         }
4783         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4784         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4785
4786         if (info->attrs[NL80211_ATTR_IE]) {
4787                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4788                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4789         }
4790
4791         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4792                 enum nl80211_channel_type channel_type;
4793
4794                 channel_type = nla_get_u32(
4795                                 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4796                 if (channel_type != NL80211_CHAN_NO_HT &&
4797                     channel_type != NL80211_CHAN_HT20 &&
4798                     channel_type != NL80211_CHAN_HT40MINUS &&
4799                     channel_type != NL80211_CHAN_HT40PLUS)
4800                         return -EINVAL;
4801
4802                 if (channel_type != NL80211_CHAN_NO_HT &&
4803                     !(wiphy->features & NL80211_FEATURE_HT_IBSS))
4804                         return -EINVAL;
4805
4806                 ibss.channel_type = channel_type;
4807         } else {
4808                 ibss.channel_type = NL80211_CHAN_NO_HT;
4809         }
4810
4811         ibss.channel = rdev_freq_to_chan(rdev,
4812                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
4813                 ibss.channel_type);
4814         if (!ibss.channel ||
4815             ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
4816             ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4817                 return -EINVAL;
4818
4819         /* Both channels should be able to initiate communication */
4820         if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
4821              ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
4822             !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
4823                                           ibss.channel_type))
4824                 return -EINVAL;
4825
4826         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
4827         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4828
4829         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4830                 u8 *rates =
4831                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4832                 int n_rates =
4833                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4834                 struct ieee80211_supported_band *sband =
4835                         wiphy->bands[ibss.channel->band];
4836
4837                 err = ieee80211_get_ratemask(sband, rates, n_rates,
4838                                              &ibss.basic_rates);
4839                 if (err)
4840                         return err;
4841         }
4842
4843         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4844             !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4845                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4846                 return -EINVAL;
4847
4848         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4849                 connkeys = nl80211_parse_connkeys(rdev,
4850                                         info->attrs[NL80211_ATTR_KEYS]);
4851                 if (IS_ERR(connkeys))
4852                         return PTR_ERR(connkeys);
4853         }
4854
4855         ibss.control_port =
4856                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
4857
4858         err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
4859         if (err)
4860                 kfree(connkeys);
4861         return err;
4862 }
4863
4864 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4865 {
4866         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4867         struct net_device *dev = info->user_ptr[1];
4868
4869         if (!rdev->ops->leave_ibss)
4870                 return -EOPNOTSUPP;
4871
4872         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4873                 return -EOPNOTSUPP;
4874
4875         return cfg80211_leave_ibss(rdev, dev, false);
4876 }
4877
4878 #ifdef CONFIG_NL80211_TESTMODE
4879 static struct genl_multicast_group nl80211_testmode_mcgrp = {
4880         .name = "testmode",
4881 };
4882
4883 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4884 {
4885         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4886         int err;
4887
4888         if (!info->attrs[NL80211_ATTR_TESTDATA])
4889                 return -EINVAL;
4890
4891         err = -EOPNOTSUPP;
4892         if (rdev->ops->testmode_cmd) {
4893                 rdev->testmode_info = info;
4894                 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4895                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4896                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4897                 rdev->testmode_info = NULL;
4898         }
4899
4900         return err;
4901 }
4902
4903 static int nl80211_testmode_dump(struct sk_buff *skb,
4904                                  struct netlink_callback *cb)
4905 {
4906         struct cfg80211_registered_device *rdev;
4907         int err;
4908         long phy_idx;
4909         void *data = NULL;
4910         int data_len = 0;
4911
4912         if (cb->args[0]) {
4913                 /*
4914                  * 0 is a valid index, but not valid for args[0],
4915                  * so we need to offset by 1.
4916                  */
4917                 phy_idx = cb->args[0] - 1;
4918         } else {
4919                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4920                                   nl80211_fam.attrbuf, nl80211_fam.maxattr,
4921                                   nl80211_policy);
4922                 if (err)
4923                         return err;
4924                 if (nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]) {
4925                         phy_idx = nla_get_u32(
4926                                 nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4927                 } else {
4928                         struct net_device *netdev;
4929
4930                         err = get_rdev_dev_by_ifindex(sock_net(skb->sk),
4931                                                       nl80211_fam.attrbuf,
4932                                                       &rdev, &netdev);
4933                         if (err)
4934                                 return err;
4935                         dev_put(netdev);
4936                         phy_idx = rdev->wiphy_idx;
4937                         cfg80211_unlock_rdev(rdev);
4938                 }
4939                 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4940                         cb->args[1] =
4941                                 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4942         }
4943
4944         if (cb->args[1]) {
4945                 data = nla_data((void *)cb->args[1]);
4946                 data_len = nla_len((void *)cb->args[1]);
4947         }
4948
4949         mutex_lock(&cfg80211_mutex);
4950         rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4951         if (!rdev) {
4952                 mutex_unlock(&cfg80211_mutex);
4953                 return -ENOENT;
4954         }
4955         cfg80211_lock_rdev(rdev);
4956         mutex_unlock(&cfg80211_mutex);
4957
4958         if (!rdev->ops->testmode_dump) {
4959                 err = -EOPNOTSUPP;
4960                 goto out_err;
4961         }
4962
4963         while (1) {
4964                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4965                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
4966                                            NL80211_CMD_TESTMODE);
4967                 struct nlattr *tmdata;
4968
4969                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx) < 0) {
4970                         genlmsg_cancel(skb, hdr);
4971                         break;
4972                 }
4973
4974                 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4975                 if (!tmdata) {
4976                         genlmsg_cancel(skb, hdr);
4977                         break;
4978                 }
4979                 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
4980                                                data, data_len);
4981                 nla_nest_end(skb, tmdata);
4982
4983                 if (err == -ENOBUFS || err == -ENOENT) {
4984                         genlmsg_cancel(skb, hdr);
4985                         break;
4986                 } else if (err) {
4987                         genlmsg_cancel(skb, hdr);
4988                         goto out_err;
4989                 }
4990
4991                 genlmsg_end(skb, hdr);
4992         }
4993
4994         err = skb->len;
4995         /* see above */
4996         cb->args[0] = phy_idx + 1;
4997  out_err:
4998         cfg80211_unlock_rdev(rdev);
4999         return err;
5000 }
5001
5002 static struct sk_buff *
5003 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5004                               int approxlen, u32 pid, u32 seq, gfp_t gfp)
5005 {
5006         struct sk_buff *skb;
5007         void *hdr;
5008         struct nlattr *data;
5009
5010         skb = nlmsg_new(approxlen + 100, gfp);
5011         if (!skb)
5012                 return NULL;
5013
5014         hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5015         if (!hdr) {
5016                 kfree_skb(skb);
5017                 return NULL;
5018         }
5019
5020         NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5021         data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5022
5023         ((void **)skb->cb)[0] = rdev;
5024         ((void **)skb->cb)[1] = hdr;
5025         ((void **)skb->cb)[2] = data;
5026
5027         return skb;
5028
5029  nla_put_failure:
5030         kfree_skb(skb);
5031         return NULL;
5032 }
5033
5034 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5035                                                   int approxlen)
5036 {
5037         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5038
5039         if (WARN_ON(!rdev->testmode_info))
5040                 return NULL;
5041
5042         return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5043                                 rdev->testmode_info->snd_pid,
5044                                 rdev->testmode_info->snd_seq,
5045                                 GFP_KERNEL);
5046 }
5047 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5048
5049 int cfg80211_testmode_reply(struct sk_buff *skb)
5050 {
5051         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5052         void *hdr = ((void **)skb->cb)[1];
5053         struct nlattr *data = ((void **)skb->cb)[2];
5054
5055         if (WARN_ON(!rdev->testmode_info)) {
5056                 kfree_skb(skb);
5057                 return -EINVAL;
5058         }
5059
5060         nla_nest_end(skb, data);
5061         genlmsg_end(skb, hdr);
5062         return genlmsg_reply(skb, rdev->testmode_info);
5063 }
5064 EXPORT_SYMBOL(cfg80211_testmode_reply);
5065
5066 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5067                                                   int approxlen, gfp_t gfp)
5068 {
5069         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5070
5071         return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5072 }
5073 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5074
5075 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5076 {
5077         void *hdr = ((void **)skb->cb)[1];
5078         struct nlattr *data = ((void **)skb->cb)[2];
5079
5080         nla_nest_end(skb, data);
5081         genlmsg_end(skb, hdr);
5082         genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5083 }
5084 EXPORT_SYMBOL(cfg80211_testmode_event);
5085 #endif
5086
5087 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5088 {
5089         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5090         struct net_device *dev = info->user_ptr[1];
5091         struct cfg80211_connect_params connect;
5092         struct wiphy *wiphy;
5093         struct cfg80211_cached_keys *connkeys = NULL;
5094         int err;
5095
5096         memset(&connect, 0, sizeof(connect));
5097
5098         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5099                 return -EINVAL;
5100
5101         if (!info->attrs[NL80211_ATTR_SSID] ||
5102             !nla_len(info->attrs[NL80211_ATTR_SSID]))
5103                 return -EINVAL;
5104
5105         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5106                 connect.auth_type =
5107                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5108                 if (!nl80211_valid_auth_type(connect.auth_type))
5109                         return -EINVAL;
5110         } else
5111                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5112
5113         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5114
5115         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
5116                                       NL80211_MAX_NR_CIPHER_SUITES);
5117         if (err)
5118                 return err;
5119
5120         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5121             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5122                 return -EOPNOTSUPP;
5123
5124         wiphy = &rdev->wiphy;
5125
5126         connect.bg_scan_period = -1;
5127         if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5128                 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5129                 connect.bg_scan_period =
5130                         nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5131         }
5132
5133         if (info->attrs[NL80211_ATTR_MAC])
5134                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5135         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5136         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5137
5138         if (info->attrs[NL80211_ATTR_IE]) {
5139                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5140                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5141         }
5142
5143         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5144                 connect.channel =
5145                         ieee80211_get_channel(wiphy,
5146                             nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5147                 if (!connect.channel ||
5148                     connect.channel->flags & IEEE80211_CHAN_DISABLED)
5149                         return -EINVAL;
5150         }
5151
5152         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5153                 connkeys = nl80211_parse_connkeys(rdev,
5154                                         info->attrs[NL80211_ATTR_KEYS]);
5155                 if (IS_ERR(connkeys))
5156                         return PTR_ERR(connkeys);
5157         }
5158
5159         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5160                 connect.flags |= ASSOC_REQ_DISABLE_HT;
5161
5162         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5163                 memcpy(&connect.ht_capa_mask,
5164                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5165                        sizeof(connect.ht_capa_mask));
5166
5167         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5168                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5169                         return -EINVAL;
5170                 memcpy(&connect.ht_capa,
5171                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5172                        sizeof(connect.ht_capa));
5173         }
5174
5175         err = cfg80211_connect(rdev, dev, &connect, connkeys);
5176         if (err)
5177                 kfree(connkeys);
5178         return err;
5179 }
5180
5181 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5182 {
5183         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5184         struct net_device *dev = info->user_ptr[1];
5185         u16 reason;
5186
5187         if (!info->attrs[NL80211_ATTR_REASON_CODE])
5188                 reason = WLAN_REASON_DEAUTH_LEAVING;
5189         else
5190                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5191
5192         if (reason == 0)
5193                 return -EINVAL;
5194
5195         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5196             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5197                 return -EOPNOTSUPP;
5198
5199         return cfg80211_disconnect(rdev, dev, reason, true);
5200 }
5201
5202 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5203 {
5204         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5205         struct net *net;
5206         int err;
5207         u32 pid;
5208
5209         if (!info->attrs[NL80211_ATTR_PID])
5210                 return -EINVAL;
5211
5212         pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5213
5214         net = get_net_ns_by_pid(pid);
5215         if (IS_ERR(net))
5216                 return PTR_ERR(net);
5217
5218         err = 0;
5219
5220         /* check if anything to do */
5221         if (!net_eq(wiphy_net(&rdev->wiphy), net))
5222                 err = cfg80211_switch_netns(rdev, net);
5223
5224         put_net(net);
5225         return err;
5226 }
5227
5228 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5229 {
5230         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5231         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5232                         struct cfg80211_pmksa *pmksa) = NULL;
5233         struct net_device *dev = info->user_ptr[1];
5234         struct cfg80211_pmksa pmksa;
5235
5236         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5237
5238         if (!info->attrs[NL80211_ATTR_MAC])
5239                 return -EINVAL;
5240
5241         if (!info->attrs[NL80211_ATTR_PMKID])
5242                 return -EINVAL;
5243
5244         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5245         pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5246
5247         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5248             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5249                 return -EOPNOTSUPP;
5250
5251         switch (info->genlhdr->cmd) {
5252         case NL80211_CMD_SET_PMKSA:
5253                 rdev_ops = rdev->ops->set_pmksa;
5254                 break;
5255         case NL80211_CMD_DEL_PMKSA:
5256                 rdev_ops = rdev->ops->del_pmksa;
5257                 break;
5258         default:
5259                 WARN_ON(1);
5260                 break;
5261         }
5262
5263         if (!rdev_ops)
5264                 return -EOPNOTSUPP;
5265
5266         return rdev_ops(&rdev->wiphy, dev, &pmksa);
5267 }
5268
5269 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5270 {
5271         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5272         struct net_device *dev = info->user_ptr[1];
5273
5274         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5275             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5276                 return -EOPNOTSUPP;
5277
5278         if (!rdev->ops->flush_pmksa)
5279                 return -EOPNOTSUPP;
5280
5281         return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
5282 }
5283
5284 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5285 {
5286         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5287         struct net_device *dev = info->user_ptr[1];
5288         u8 action_code, dialog_token;
5289         u16 status_code;
5290         u8 *peer;
5291
5292         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5293             !rdev->ops->tdls_mgmt)
5294                 return -EOPNOTSUPP;
5295
5296         if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5297             !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5298             !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5299             !info->attrs[NL80211_ATTR_IE] ||
5300             !info->attrs[NL80211_ATTR_MAC])
5301                 return -EINVAL;
5302
5303         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5304         action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5305         status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5306         dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5307
5308         return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5309                                     dialog_token, status_code,
5310                                     nla_data(info->attrs[NL80211_ATTR_IE]),
5311                                     nla_len(info->attrs[NL80211_ATTR_IE]));
5312 }
5313
5314 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5315 {
5316         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5317         struct net_device *dev = info->user_ptr[1];
5318         enum nl80211_tdls_operation operation;
5319         u8 *peer;
5320
5321         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5322             !rdev->ops->tdls_oper)
5323                 return -EOPNOTSUPP;
5324
5325         if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5326             !info->attrs[NL80211_ATTR_MAC])
5327                 return -EINVAL;
5328
5329         operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5330         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5331
5332         return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5333 }
5334
5335 static int nl80211_remain_on_channel(struct sk_buff *skb,
5336                                      struct genl_info *info)
5337 {
5338         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5339         struct net_device *dev = info->user_ptr[1];
5340         struct ieee80211_channel *chan;
5341         struct sk_buff *msg;
5342         void *hdr;
5343         u64 cookie;
5344         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5345         u32 freq, duration;
5346         int err;
5347
5348         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5349             !info->attrs[NL80211_ATTR_DURATION])
5350                 return -EINVAL;
5351
5352         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5353
5354         /*
5355          * We should be on that channel for at least one jiffie,
5356          * and more than 5 seconds seems excessive.
5357          */
5358         if (!duration || !msecs_to_jiffies(duration) ||
5359             duration > rdev->wiphy.max_remain_on_channel_duration)
5360                 return -EINVAL;
5361
5362         if (!rdev->ops->remain_on_channel ||
5363             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
5364                 return -EOPNOTSUPP;
5365
5366         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5367                 channel_type = nla_get_u32(
5368                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5369                 if (channel_type != NL80211_CHAN_NO_HT &&
5370                     channel_type != NL80211_CHAN_HT20 &&
5371                     channel_type != NL80211_CHAN_HT40PLUS &&
5372                     channel_type != NL80211_CHAN_HT40MINUS)
5373                         return -EINVAL;
5374         }
5375
5376         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5377         chan = rdev_freq_to_chan(rdev, freq, channel_type);
5378         if (chan == NULL)
5379                 return -EINVAL;
5380
5381         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5382         if (!msg)
5383                 return -ENOMEM;
5384
5385         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5386                              NL80211_CMD_REMAIN_ON_CHANNEL);
5387
5388         if (IS_ERR(hdr)) {
5389                 err = PTR_ERR(hdr);
5390                 goto free_msg;
5391         }
5392
5393         err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5394                                            channel_type, duration, &cookie);
5395
5396         if (err)
5397                 goto free_msg;
5398
5399         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5400
5401         genlmsg_end(msg, hdr);
5402
5403         return genlmsg_reply(msg, info);
5404
5405  nla_put_failure:
5406         err = -ENOBUFS;
5407  free_msg:
5408         nlmsg_free(msg);
5409         return err;
5410 }
5411
5412 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5413                                             struct genl_info *info)
5414 {
5415         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5416         struct net_device *dev = info->user_ptr[1];
5417         u64 cookie;
5418
5419         if (!info->attrs[NL80211_ATTR_COOKIE])
5420                 return -EINVAL;
5421
5422         if (!rdev->ops->cancel_remain_on_channel)
5423                 return -EOPNOTSUPP;
5424
5425         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5426
5427         return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
5428 }
5429
5430 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5431                            u8 *rates, u8 rates_len)
5432 {
5433         u8 i;
5434         u32 mask = 0;
5435
5436         for (i = 0; i < rates_len; i++) {
5437                 int rate = (rates[i] & 0x7f) * 5;
5438                 int ridx;
5439                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5440                         struct ieee80211_rate *srate =
5441                                 &sband->bitrates[ridx];
5442                         if (rate == srate->bitrate) {
5443                                 mask |= 1 << ridx;
5444                                 break;
5445                         }
5446                 }
5447                 if (ridx == sband->n_bitrates)
5448                         return 0; /* rate not found */
5449         }
5450
5451         return mask;
5452 }
5453
5454 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5455                                u8 *rates, u8 rates_len,
5456                                u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5457 {
5458         u8 i;
5459
5460         memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5461
5462         for (i = 0; i < rates_len; i++) {
5463                 int ridx, rbit;
5464
5465                 ridx = rates[i] / 8;
5466                 rbit = BIT(rates[i] % 8);
5467
5468                 /* check validity */
5469                 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
5470                         return false;
5471
5472                 /* check availability */
5473                 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5474                         mcs[ridx] |= rbit;
5475                 else
5476                         return false;
5477         }
5478
5479         return true;
5480 }
5481
5482 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
5483         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5484                                     .len = NL80211_MAX_SUPP_RATES },
5485         [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5486                                  .len = NL80211_MAX_SUPP_HT_RATES },
5487 };
5488
5489 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5490                                        struct genl_info *info)
5491 {
5492         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5493         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5494         struct cfg80211_bitrate_mask mask;
5495         int rem, i;
5496         struct net_device *dev = info->user_ptr[1];
5497         struct nlattr *tx_rates;
5498         struct ieee80211_supported_band *sband;
5499
5500         if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5501                 return -EINVAL;
5502
5503         if (!rdev->ops->set_bitrate_mask)
5504                 return -EOPNOTSUPP;
5505
5506         memset(&mask, 0, sizeof(mask));
5507         /* Default to all rates enabled */
5508         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5509                 sband = rdev->wiphy.bands[i];
5510                 mask.control[i].legacy =
5511                         sband ? (1 << sband->n_bitrates) - 1 : 0;
5512                 if (sband)
5513                         memcpy(mask.control[i].mcs,
5514                                sband->ht_cap.mcs.rx_mask,
5515                                sizeof(mask.control[i].mcs));
5516                 else
5517                         memset(mask.control[i].mcs, 0,
5518                                sizeof(mask.control[i].mcs));
5519         }
5520
5521         /*
5522          * The nested attribute uses enum nl80211_band as the index. This maps
5523          * directly to the enum ieee80211_band values used in cfg80211.
5524          */
5525         BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5526         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5527         {
5528                 enum ieee80211_band band = nla_type(tx_rates);
5529                 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5530                         return -EINVAL;
5531                 sband = rdev->wiphy.bands[band];
5532                 if (sband == NULL)
5533                         return -EINVAL;
5534                 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5535                           nla_len(tx_rates), nl80211_txattr_policy);
5536                 if (tb[NL80211_TXRATE_LEGACY]) {
5537                         mask.control[band].legacy = rateset_to_mask(
5538                                 sband,
5539                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
5540                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
5541                 }
5542                 if (tb[NL80211_TXRATE_MCS]) {
5543                         if (!ht_rateset_to_mask(
5544                                         sband,
5545                                         nla_data(tb[NL80211_TXRATE_MCS]),
5546                                         nla_len(tb[NL80211_TXRATE_MCS]),
5547                                         mask.control[band].mcs))
5548                                 return -EINVAL;
5549                 }
5550
5551                 if (mask.control[band].legacy == 0) {
5552                         /* don't allow empty legacy rates if HT
5553                          * is not even supported. */
5554                         if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5555                                 return -EINVAL;
5556
5557                         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5558                                 if (mask.control[band].mcs[i])
5559                                         break;
5560
5561                         /* legacy and mcs rates may not be both empty */
5562                         if (i == IEEE80211_HT_MCS_MASK_LEN)
5563                                 return -EINVAL;
5564                 }
5565         }
5566
5567         return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
5568 }
5569
5570 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
5571 {
5572         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5573         struct net_device *dev = info->user_ptr[1];
5574         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
5575
5576         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5577                 return -EINVAL;
5578
5579         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5580                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
5581
5582         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5583             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5584             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5585             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5586             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5587             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5588             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5589                 return -EOPNOTSUPP;
5590
5591         /* not much point in registering if we can't reply */
5592         if (!rdev->ops->mgmt_tx)
5593                 return -EOPNOTSUPP;
5594
5595         return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
5596                         frame_type,
5597                         nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5598                         nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
5599 }
5600
5601 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
5602 {
5603         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5604         struct net_device *dev = info->user_ptr[1];
5605         struct ieee80211_channel *chan;
5606         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5607         bool channel_type_valid = false;
5608         u32 freq;
5609         int err;
5610         void *hdr = NULL;
5611         u64 cookie;
5612         struct sk_buff *msg = NULL;
5613         unsigned int wait = 0;
5614         bool offchan, no_cck, dont_wait_for_ack;
5615
5616         dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
5617
5618         if (!info->attrs[NL80211_ATTR_FRAME] ||
5619             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5620                 return -EINVAL;
5621
5622         if (!rdev->ops->mgmt_tx)
5623                 return -EOPNOTSUPP;
5624
5625         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5626             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5627             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5628             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5629             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5630             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5631             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5632                 return -EOPNOTSUPP;
5633
5634         if (info->attrs[NL80211_ATTR_DURATION]) {
5635                 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5636                         return -EINVAL;
5637                 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5638         }
5639
5640         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5641                 channel_type = nla_get_u32(
5642                         info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5643                 if (channel_type != NL80211_CHAN_NO_HT &&
5644                     channel_type != NL80211_CHAN_HT20 &&
5645                     channel_type != NL80211_CHAN_HT40PLUS &&
5646                     channel_type != NL80211_CHAN_HT40MINUS)
5647                         return -EINVAL;
5648                 channel_type_valid = true;
5649         }
5650
5651         offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5652
5653         if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
5654                 return -EINVAL;
5655
5656         no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5657
5658         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5659         chan = rdev_freq_to_chan(rdev, freq, channel_type);
5660         if (chan == NULL)
5661                 return -EINVAL;
5662
5663         if (!dont_wait_for_ack) {
5664                 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5665                 if (!msg)
5666                         return -ENOMEM;
5667
5668                 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5669                                      NL80211_CMD_FRAME);
5670
5671                 if (IS_ERR(hdr)) {
5672                         err = PTR_ERR(hdr);
5673                         goto free_msg;
5674                 }
5675         }
5676
5677         err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5678                                     channel_type_valid, wait,
5679                                     nla_data(info->attrs[NL80211_ATTR_FRAME]),
5680                                     nla_len(info->attrs[NL80211_ATTR_FRAME]),
5681                                     no_cck, dont_wait_for_ack, &cookie);
5682         if (err)
5683                 goto free_msg;
5684
5685         if (msg) {
5686                 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5687
5688                 genlmsg_end(msg, hdr);
5689                 return genlmsg_reply(msg, info);
5690         }
5691
5692         return 0;
5693
5694  nla_put_failure:
5695         err = -ENOBUFS;
5696  free_msg:
5697         nlmsg_free(msg);
5698         return err;
5699 }
5700
5701 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5702 {
5703         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5704         struct net_device *dev = info->user_ptr[1];
5705         u64 cookie;
5706
5707         if (!info->attrs[NL80211_ATTR_COOKIE])
5708                 return -EINVAL;
5709
5710         if (!rdev->ops->mgmt_tx_cancel_wait)
5711                 return -EOPNOTSUPP;
5712
5713         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5714             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5715             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5716             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5717             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5718             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5719                 return -EOPNOTSUPP;
5720
5721         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5722
5723         return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5724 }
5725
5726 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5727 {
5728         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5729         struct wireless_dev *wdev;
5730         struct net_device *dev = info->user_ptr[1];
5731         u8 ps_state;
5732         bool state;
5733         int err;
5734
5735         if (!info->attrs[NL80211_ATTR_PS_STATE])
5736                 return -EINVAL;
5737
5738         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5739
5740         if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5741                 return -EINVAL;
5742
5743         wdev = dev->ieee80211_ptr;
5744
5745         if (!rdev->ops->set_power_mgmt)
5746                 return -EOPNOTSUPP;
5747
5748         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5749
5750         if (state == wdev->ps)
5751                 return 0;
5752
5753         err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5754                                         wdev->ps_timeout);
5755         if (!err)
5756                 wdev->ps = state;
5757         return err;
5758 }
5759
5760 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5761 {
5762         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5763         enum nl80211_ps_state ps_state;
5764         struct wireless_dev *wdev;
5765         struct net_device *dev = info->user_ptr[1];
5766         struct sk_buff *msg;
5767         void *hdr;
5768         int err;
5769
5770         wdev = dev->ieee80211_ptr;
5771
5772         if (!rdev->ops->set_power_mgmt)
5773                 return -EOPNOTSUPP;
5774
5775         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5776         if (!msg)
5777                 return -ENOMEM;
5778
5779         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5780                              NL80211_CMD_GET_POWER_SAVE);
5781         if (!hdr) {
5782                 err = -ENOBUFS;
5783                 goto free_msg;
5784         }
5785
5786         if (wdev->ps)
5787                 ps_state = NL80211_PS_ENABLED;
5788         else
5789                 ps_state = NL80211_PS_DISABLED;
5790
5791         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5792
5793         genlmsg_end(msg, hdr);
5794         return genlmsg_reply(msg, info);
5795
5796  nla_put_failure:
5797         err = -ENOBUFS;
5798  free_msg:
5799         nlmsg_free(msg);
5800         return err;
5801 }
5802
5803 static struct nla_policy
5804 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5805         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5806         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5807         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5808 };
5809
5810 static int nl80211_set_cqm_rssi(struct genl_info *info,
5811                                 s32 threshold, u32 hysteresis)
5812 {
5813         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5814         struct wireless_dev *wdev;
5815         struct net_device *dev = info->user_ptr[1];
5816
5817         if (threshold > 0)
5818                 return -EINVAL;
5819
5820         wdev = dev->ieee80211_ptr;
5821
5822         if (!rdev->ops->set_cqm_rssi_config)
5823                 return -EOPNOTSUPP;
5824
5825         if (wdev->iftype != NL80211_IFTYPE_STATION &&
5826             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5827                 return -EOPNOTSUPP;
5828
5829         return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5830                                               threshold, hysteresis);
5831 }
5832
5833 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5834 {
5835         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5836         struct nlattr *cqm;
5837         int err;
5838
5839         cqm = info->attrs[NL80211_ATTR_CQM];
5840         if (!cqm) {
5841                 err = -EINVAL;
5842                 goto out;
5843         }
5844
5845         err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5846                                nl80211_attr_cqm_policy);
5847         if (err)
5848                 goto out;
5849
5850         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5851             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5852                 s32 threshold;
5853                 u32 hysteresis;
5854                 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5855                 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5856                 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5857         } else
5858                 err = -EINVAL;
5859
5860 out:
5861         return err;
5862 }
5863
5864 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5865 {
5866         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5867         struct net_device *dev = info->user_ptr[1];
5868         struct mesh_config cfg;
5869         struct mesh_setup setup;
5870         int err;
5871
5872         /* start with default */
5873         memcpy(&cfg, &default_mesh_config, sizeof(cfg));
5874         memcpy(&setup, &default_mesh_setup, sizeof(setup));
5875
5876         if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
5877                 /* and parse parameters if given */
5878                 err = nl80211_parse_mesh_config(info, &cfg, NULL);
5879                 if (err)
5880                         return err;
5881         }
5882
5883         if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5884             !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5885                 return -EINVAL;
5886
5887         setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5888         setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5889
5890         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5891             !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
5892                             nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5893                         return -EINVAL;
5894
5895         if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5896                 /* parse additional setup parameters if given */
5897                 err = nl80211_parse_mesh_setup(info, &setup);
5898                 if (err)
5899                         return err;
5900         }
5901
5902         return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
5903 }
5904
5905 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5906 {
5907         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5908         struct net_device *dev = info->user_ptr[1];
5909
5910         return cfg80211_leave_mesh(rdev, dev);
5911 }
5912
5913 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5914 {
5915         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5916         struct sk_buff *msg;
5917         void *hdr;
5918
5919         if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5920                 return -EOPNOTSUPP;
5921
5922         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5923         if (!msg)
5924                 return -ENOMEM;
5925
5926         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5927                              NL80211_CMD_GET_WOWLAN);
5928         if (!hdr)
5929                 goto nla_put_failure;
5930
5931         if (rdev->wowlan) {
5932                 struct nlattr *nl_wowlan;
5933
5934                 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5935                 if (!nl_wowlan)
5936                         goto nla_put_failure;
5937
5938                 if (rdev->wowlan->any)
5939                         NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5940                 if (rdev->wowlan->disconnect)
5941          &n