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