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