nl80211: use __cfg80211_rdev_from_attrs for testmode
[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 int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2339 {
2340         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2341         struct net_device *dev = info->user_ptr[1];
2342         struct wireless_dev *wdev = dev->ieee80211_ptr;
2343         struct cfg80211_ap_settings params;
2344         int err;
2345
2346         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2347             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2348                 return -EOPNOTSUPP;
2349
2350         if (!rdev->ops->start_ap)
2351                 return -EOPNOTSUPP;
2352
2353         if (wdev->beacon_interval)
2354                 return -EALREADY;
2355
2356         memset(&params, 0, sizeof(params));
2357
2358         /* these are required for START_AP */
2359         if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2360             !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2361             !info->attrs[NL80211_ATTR_BEACON_HEAD])
2362                 return -EINVAL;
2363
2364         err = nl80211_parse_beacon(info, &params.beacon);
2365         if (err)
2366                 return err;
2367
2368         params.beacon_interval =
2369                 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2370         params.dtim_period =
2371                 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2372
2373         err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2374         if (err)
2375                 return err;
2376
2377         /*
2378          * In theory, some of these attributes should be required here
2379          * but since they were not used when the command was originally
2380          * added, keep them optional for old user space programs to let
2381          * them continue to work with drivers that do not need the
2382          * additional information -- drivers must check!
2383          */
2384         if (info->attrs[NL80211_ATTR_SSID]) {
2385                 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2386                 params.ssid_len =
2387                         nla_len(info->attrs[NL80211_ATTR_SSID]);
2388                 if (params.ssid_len == 0 ||
2389                     params.ssid_len > IEEE80211_MAX_SSID_LEN)
2390                         return -EINVAL;
2391         }
2392
2393         if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2394                 params.hidden_ssid = nla_get_u32(
2395                         info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2396                 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2397                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2398                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2399                         return -EINVAL;
2400         }
2401
2402         params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2403
2404         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2405                 params.auth_type = nla_get_u32(
2406                         info->attrs[NL80211_ATTR_AUTH_TYPE]);
2407                 if (!nl80211_valid_auth_type(params.auth_type))
2408                         return -EINVAL;
2409         } else
2410                 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2411
2412         err = nl80211_crypto_settings(rdev, info, &params.crypto,
2413                                       NL80211_MAX_NR_CIPHER_SUITES);
2414         if (err)
2415                 return err;
2416
2417         if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2418                 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2419                         return -EOPNOTSUPP;
2420                 params.inactivity_timeout = nla_get_u16(
2421                         info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2422         }
2423
2424         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2425                 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2426
2427                 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2428                     !nl80211_valid_channel_type(info, &channel_type))
2429                         return -EINVAL;
2430
2431                 params.channel = rdev_freq_to_chan(rdev,
2432                         nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2433                         channel_type);
2434                 if (!params.channel)
2435                         return -EINVAL;
2436                 params.channel_type = channel_type;
2437         } else if (wdev->preset_chan) {
2438                 params.channel = wdev->preset_chan;
2439                 params.channel_type = wdev->preset_chantype;
2440         } else
2441                 return -EINVAL;
2442
2443         if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2444                                           params.channel_type))
2445                 return -EINVAL;
2446
2447         err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
2448         if (!err)
2449                 wdev->beacon_interval = params.beacon_interval;
2450         return err;
2451 }
2452
2453 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2454 {
2455         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2456         struct net_device *dev = info->user_ptr[1];
2457         struct wireless_dev *wdev = dev->ieee80211_ptr;
2458         struct cfg80211_beacon_data params;
2459         int err;
2460
2461         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2462             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2463                 return -EOPNOTSUPP;
2464
2465         if (!rdev->ops->change_beacon)
2466                 return -EOPNOTSUPP;
2467
2468         if (!wdev->beacon_interval)
2469                 return -EINVAL;
2470
2471         err = nl80211_parse_beacon(info, &params);
2472         if (err)
2473                 return err;
2474
2475         return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2476 }
2477
2478 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
2479 {
2480         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2481         struct net_device *dev = info->user_ptr[1];
2482         struct wireless_dev *wdev = dev->ieee80211_ptr;
2483         int err;
2484
2485         if (!rdev->ops->stop_ap)
2486                 return -EOPNOTSUPP;
2487
2488         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2489             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2490                 return -EOPNOTSUPP;
2491
2492         if (!wdev->beacon_interval)
2493                 return -ENOENT;
2494
2495         err = rdev->ops->stop_ap(&rdev->wiphy, dev);
2496         if (!err)
2497                 wdev->beacon_interval = 0;
2498         return err;
2499 }
2500
2501 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2502         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2503         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2504         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
2505         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
2506         [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
2507         [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
2508 };
2509
2510 static int parse_station_flags(struct genl_info *info,
2511                                enum nl80211_iftype iftype,
2512                                struct station_parameters *params)
2513 {
2514         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
2515         struct nlattr *nla;
2516         int flag;
2517
2518         /*
2519          * Try parsing the new attribute first so userspace
2520          * can specify both for older kernels.
2521          */
2522         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2523         if (nla) {
2524                 struct nl80211_sta_flag_update *sta_flags;
2525
2526                 sta_flags = nla_data(nla);
2527                 params->sta_flags_mask = sta_flags->mask;
2528                 params->sta_flags_set = sta_flags->set;
2529                 if ((params->sta_flags_mask |
2530                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2531                         return -EINVAL;
2532                 return 0;
2533         }
2534
2535         /* if present, parse the old attribute */
2536
2537         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
2538         if (!nla)
2539                 return 0;
2540
2541         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2542                              nla, sta_flags_policy))
2543                 return -EINVAL;
2544
2545         /*
2546          * Only allow certain flags for interface types so that
2547          * other attributes are silently ignored. Remember that
2548          * this is backward compatibility code with old userspace
2549          * and shouldn't be hit in other cases anyway.
2550          */
2551         switch (iftype) {
2552         case NL80211_IFTYPE_AP:
2553         case NL80211_IFTYPE_AP_VLAN:
2554         case NL80211_IFTYPE_P2P_GO:
2555                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2556                                          BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2557                                          BIT(NL80211_STA_FLAG_WME) |
2558                                          BIT(NL80211_STA_FLAG_MFP);
2559                 break;
2560         case NL80211_IFTYPE_P2P_CLIENT:
2561         case NL80211_IFTYPE_STATION:
2562                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2563                                          BIT(NL80211_STA_FLAG_TDLS_PEER);
2564                 break;
2565         case NL80211_IFTYPE_MESH_POINT:
2566                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2567                                          BIT(NL80211_STA_FLAG_MFP) |
2568                                          BIT(NL80211_STA_FLAG_AUTHORIZED);
2569         default:
2570                 return -EINVAL;
2571         }
2572
2573         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2574                 if (flags[flag]) {
2575                         params->sta_flags_set |= (1<<flag);
2576
2577                         /* no longer support new API additions in old API */
2578                         if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2579                                 return -EINVAL;
2580                 }
2581         }
2582
2583         return 0;
2584 }
2585
2586 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2587                                  int attr)
2588 {
2589         struct nlattr *rate;
2590         u16 bitrate;
2591
2592         rate = nla_nest_start(msg, attr);
2593         if (!rate)
2594                 goto nla_put_failure;
2595
2596         /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2597         bitrate = cfg80211_calculate_bitrate(info);
2598         if ((bitrate > 0 &&
2599              nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate)) ||
2600             ((info->flags & RATE_INFO_FLAGS_MCS) &&
2601              nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2602             ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2603              nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2604             ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2605              nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2606                 goto nla_put_failure;
2607
2608         nla_nest_end(msg, rate);
2609         return true;
2610
2611 nla_put_failure:
2612         return false;
2613 }
2614
2615 static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2616                                 int flags,
2617                                 struct cfg80211_registered_device *rdev,
2618                                 struct net_device *dev,
2619                                 const u8 *mac_addr, struct station_info *sinfo)
2620 {
2621         void *hdr;
2622         struct nlattr *sinfoattr, *bss_param;
2623
2624         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2625         if (!hdr)
2626                 return -1;
2627
2628         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2629             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2630             nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2631                 goto nla_put_failure;
2632
2633         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2634         if (!sinfoattr)
2635                 goto nla_put_failure;
2636         if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2637             nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2638                         sinfo->connected_time))
2639                 goto nla_put_failure;
2640         if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2641             nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2642                         sinfo->inactive_time))
2643                 goto nla_put_failure;
2644         if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2645             nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2646                         sinfo->rx_bytes))
2647                 goto nla_put_failure;
2648         if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2649             nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2650                         sinfo->tx_bytes))
2651                 goto nla_put_failure;
2652         if ((sinfo->filled & STATION_INFO_LLID) &&
2653             nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2654                 goto nla_put_failure;
2655         if ((sinfo->filled & STATION_INFO_PLID) &&
2656             nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2657                 goto nla_put_failure;
2658         if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2659             nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2660                        sinfo->plink_state))
2661                 goto nla_put_failure;
2662         switch (rdev->wiphy.signal_type) {
2663         case CFG80211_SIGNAL_TYPE_MBM:
2664                 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2665                     nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2666                                sinfo->signal))
2667                         goto nla_put_failure;
2668                 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2669                     nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2670                                sinfo->signal_avg))
2671                         goto nla_put_failure;
2672                 break;
2673         default:
2674                 break;
2675         }
2676         if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2677                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2678                                           NL80211_STA_INFO_TX_BITRATE))
2679                         goto nla_put_failure;
2680         }
2681         if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2682                 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2683                                           NL80211_STA_INFO_RX_BITRATE))
2684                         goto nla_put_failure;
2685         }
2686         if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2687             nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2688                         sinfo->rx_packets))
2689                 goto nla_put_failure;
2690         if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2691             nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2692                         sinfo->tx_packets))
2693                 goto nla_put_failure;
2694         if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2695             nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2696                         sinfo->tx_retries))
2697                 goto nla_put_failure;
2698         if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2699             nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2700                         sinfo->tx_failed))
2701                 goto nla_put_failure;
2702         if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2703             nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2704                         sinfo->beacon_loss_count))
2705                 goto nla_put_failure;
2706         if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2707                 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2708                 if (!bss_param)
2709                         goto nla_put_failure;
2710
2711                 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2712                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2713                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2714                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2715                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2716                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2717                     nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2718                                sinfo->bss_param.dtim_period) ||
2719                     nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2720                                 sinfo->bss_param.beacon_interval))
2721                         goto nla_put_failure;
2722
2723                 nla_nest_end(msg, bss_param);
2724         }
2725         if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2726             nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2727                     sizeof(struct nl80211_sta_flag_update),
2728                     &sinfo->sta_flags))
2729                 goto nla_put_failure;
2730         if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2731                 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2732                             sinfo->t_offset))
2733                 goto nla_put_failure;
2734         nla_nest_end(msg, sinfoattr);
2735
2736         if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2737             nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2738                     sinfo->assoc_req_ies))
2739                 goto nla_put_failure;
2740
2741         return genlmsg_end(msg, hdr);
2742
2743  nla_put_failure:
2744         genlmsg_cancel(msg, hdr);
2745         return -EMSGSIZE;
2746 }
2747
2748 static int nl80211_dump_station(struct sk_buff *skb,
2749                                 struct netlink_callback *cb)
2750 {
2751         struct station_info sinfo;
2752         struct cfg80211_registered_device *dev;
2753         struct net_device *netdev;
2754         u8 mac_addr[ETH_ALEN];
2755         int sta_idx = cb->args[1];
2756         int err;
2757
2758         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2759         if (err)
2760                 return err;
2761
2762         if (!dev->ops->dump_station) {
2763                 err = -EOPNOTSUPP;
2764                 goto out_err;
2765         }
2766
2767         while (1) {
2768                 memset(&sinfo, 0, sizeof(sinfo));
2769                 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2770                                              mac_addr, &sinfo);
2771                 if (err == -ENOENT)
2772                         break;
2773                 if (err)
2774                         goto out_err;
2775
2776                 if (nl80211_send_station(skb,
2777                                 NETLINK_CB(cb->skb).pid,
2778                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2779                                 dev, netdev, mac_addr,
2780                                 &sinfo) < 0)
2781                         goto out;
2782
2783                 sta_idx++;
2784         }
2785
2786
2787  out:
2788         cb->args[1] = sta_idx;
2789         err = skb->len;
2790  out_err:
2791         nl80211_finish_netdev_dump(dev);
2792
2793         return err;
2794 }
2795
2796 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2797 {
2798         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2799         struct net_device *dev = info->user_ptr[1];
2800         struct station_info sinfo;
2801         struct sk_buff *msg;
2802         u8 *mac_addr = NULL;
2803         int err;
2804
2805         memset(&sinfo, 0, sizeof(sinfo));
2806
2807         if (!info->attrs[NL80211_ATTR_MAC])
2808                 return -EINVAL;
2809
2810         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2811
2812         if (!rdev->ops->get_station)
2813                 return -EOPNOTSUPP;
2814
2815         err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
2816         if (err)
2817                 return err;
2818
2819         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2820         if (!msg)
2821                 return -ENOMEM;
2822
2823         if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
2824                                  rdev, dev, mac_addr, &sinfo) < 0) {
2825                 nlmsg_free(msg);
2826                 return -ENOBUFS;
2827         }
2828
2829         return genlmsg_reply(msg, info);
2830 }
2831
2832 /*
2833  * Get vlan interface making sure it is running and on the right wiphy.
2834  */
2835 static struct net_device *get_vlan(struct genl_info *info,
2836                                    struct cfg80211_registered_device *rdev)
2837 {
2838         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
2839         struct net_device *v;
2840         int ret;
2841
2842         if (!vlanattr)
2843                 return NULL;
2844
2845         v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2846         if (!v)
2847                 return ERR_PTR(-ENODEV);
2848
2849         if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2850                 ret = -EINVAL;
2851                 goto error;
2852         }
2853
2854         if (!netif_running(v)) {
2855                 ret = -ENETDOWN;
2856                 goto error;
2857         }
2858
2859         return v;
2860  error:
2861         dev_put(v);
2862         return ERR_PTR(ret);
2863 }
2864
2865 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2866 {
2867         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2868         int err;
2869         struct net_device *dev = info->user_ptr[1];
2870         struct station_parameters params;
2871         u8 *mac_addr = NULL;
2872
2873         memset(&params, 0, sizeof(params));
2874
2875         params.listen_interval = -1;
2876         params.plink_state = -1;
2877
2878         if (info->attrs[NL80211_ATTR_STA_AID])
2879                 return -EINVAL;
2880
2881         if (!info->attrs[NL80211_ATTR_MAC])
2882                 return -EINVAL;
2883
2884         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2885
2886         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2887                 params.supported_rates =
2888                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2889                 params.supported_rates_len =
2890                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2891         }
2892
2893         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2894                 params.listen_interval =
2895                     nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2896
2897         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2898                 params.ht_capa =
2899                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2900
2901         if (!rdev->ops->change_station)
2902                 return -EOPNOTSUPP;
2903
2904         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
2905                 return -EINVAL;
2906
2907         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2908                 params.plink_action =
2909                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2910
2911         if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2912                 params.plink_state =
2913                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2914
2915         switch (dev->ieee80211_ptr->iftype) {
2916         case NL80211_IFTYPE_AP:
2917         case NL80211_IFTYPE_AP_VLAN:
2918         case NL80211_IFTYPE_P2P_GO:
2919                 /* disallow mesh-specific things */
2920                 if (params.plink_action)
2921                         return -EINVAL;
2922
2923                 /* TDLS can't be set, ... */
2924                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
2925                         return -EINVAL;
2926                 /*
2927                  * ... but don't bother the driver with it. This works around
2928                  * a hostapd/wpa_supplicant issue -- it always includes the
2929                  * TLDS_PEER flag in the mask even for AP mode.
2930                  */
2931                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2932
2933                 /* accept only the listed bits */
2934                 if (params.sta_flags_mask &
2935                                 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
2936                                   BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2937                                   BIT(NL80211_STA_FLAG_WME) |
2938                                   BIT(NL80211_STA_FLAG_MFP)))
2939                         return -EINVAL;
2940
2941                 /* must be last in here for error handling */
2942                 params.vlan = get_vlan(info, rdev);
2943                 if (IS_ERR(params.vlan))
2944                         return PTR_ERR(params.vlan);
2945                 break;
2946         case NL80211_IFTYPE_P2P_CLIENT:
2947         case NL80211_IFTYPE_STATION:
2948                 /*
2949                  * Don't allow userspace to change the TDLS_PEER flag,
2950                  * but silently ignore attempts to change it since we
2951                  * don't have state here to verify that it doesn't try
2952                  * to change the flag.
2953                  */
2954                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2955                 /* fall through */
2956         case NL80211_IFTYPE_ADHOC:
2957                 /* disallow things sta doesn't support */
2958                 if (params.plink_action)
2959                         return -EINVAL;
2960                 if (params.ht_capa)
2961                         return -EINVAL;
2962                 if (params.listen_interval >= 0)
2963                         return -EINVAL;
2964                 /* reject any changes other than AUTHORIZED */
2965                 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2966                         return -EINVAL;
2967                 break;
2968         case NL80211_IFTYPE_MESH_POINT:
2969                 /* disallow things mesh doesn't support */
2970                 if (params.vlan)
2971                         return -EINVAL;
2972                 if (params.ht_capa)
2973                         return -EINVAL;
2974                 if (params.listen_interval >= 0)
2975                         return -EINVAL;
2976                 /*
2977                  * No special handling for TDLS here -- the userspace
2978                  * mesh code doesn't have this bug.
2979                  */
2980                 if (params.sta_flags_mask &
2981                                 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2982                                   BIT(NL80211_STA_FLAG_MFP) |
2983                                   BIT(NL80211_STA_FLAG_AUTHORIZED)))
2984                         return -EINVAL;
2985                 break;
2986         default:
2987                 return -EOPNOTSUPP;
2988         }
2989
2990         /* be aware of params.vlan when changing code here */
2991
2992         err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
2993
2994         if (params.vlan)
2995                 dev_put(params.vlan);
2996
2997         return err;
2998 }
2999
3000 static struct nla_policy
3001 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3002         [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3003         [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3004 };
3005
3006 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3007 {
3008         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3009         int err;
3010         struct net_device *dev = info->user_ptr[1];
3011         struct station_parameters params;
3012         u8 *mac_addr = NULL;
3013
3014         memset(&params, 0, sizeof(params));
3015
3016         if (!info->attrs[NL80211_ATTR_MAC])
3017                 return -EINVAL;
3018
3019         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3020                 return -EINVAL;
3021
3022         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3023                 return -EINVAL;
3024
3025         if (!info->attrs[NL80211_ATTR_STA_AID])
3026                 return -EINVAL;
3027
3028         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3029         params.supported_rates =
3030                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3031         params.supported_rates_len =
3032                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3033         params.listen_interval =
3034                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3035
3036         params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3037         if (!params.aid || params.aid > IEEE80211_MAX_AID)
3038                 return -EINVAL;
3039
3040         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3041                 params.ht_capa =
3042                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3043
3044         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3045                 params.plink_action =
3046                     nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3047
3048         if (!rdev->ops->add_station)
3049                 return -EOPNOTSUPP;
3050
3051         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
3052                 return -EINVAL;
3053
3054         switch (dev->ieee80211_ptr->iftype) {
3055         case NL80211_IFTYPE_AP:
3056         case NL80211_IFTYPE_AP_VLAN:
3057         case NL80211_IFTYPE_P2P_GO:
3058                 /* parse WME attributes if sta is WME capable */
3059                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3060                     (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3061                     info->attrs[NL80211_ATTR_STA_WME]) {
3062                         struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3063                         struct nlattr *nla;
3064
3065                         nla = info->attrs[NL80211_ATTR_STA_WME];
3066                         err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3067                                                nl80211_sta_wme_policy);
3068                         if (err)
3069                                 return err;
3070
3071                         if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3072                                 params.uapsd_queues =
3073                                      nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3074                         if (params.uapsd_queues &
3075                                         ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3076                                 return -EINVAL;
3077
3078                         if (tb[NL80211_STA_WME_MAX_SP])
3079                                 params.max_sp =
3080                                      nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3081
3082                         if (params.max_sp &
3083                                         ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3084                                 return -EINVAL;
3085
3086                         params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3087                 }
3088                 /* TDLS peers cannot be added */
3089                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3090                         return -EINVAL;
3091                 /* but don't bother the driver with it */
3092                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3093
3094                 /* must be last in here for error handling */
3095                 params.vlan = get_vlan(info, rdev);
3096                 if (IS_ERR(params.vlan))
3097                         return PTR_ERR(params.vlan);
3098                 break;
3099         case NL80211_IFTYPE_MESH_POINT:
3100                 /* TDLS peers cannot be added */
3101                 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3102                         return -EINVAL;
3103                 break;
3104         case NL80211_IFTYPE_STATION:
3105                 /* Only TDLS peers can be added */
3106                 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3107                         return -EINVAL;
3108                 /* Can only add if TDLS ... */
3109                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3110                         return -EOPNOTSUPP;
3111                 /* ... with external setup is supported */
3112                 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3113                         return -EOPNOTSUPP;
3114                 break;
3115         default:
3116                 return -EOPNOTSUPP;
3117         }
3118
3119         /* be aware of params.vlan when changing code here */
3120
3121         err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
3122
3123         if (params.vlan)
3124                 dev_put(params.vlan);
3125         return err;
3126 }
3127
3128 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3129 {
3130         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3131         struct net_device *dev = info->user_ptr[1];
3132         u8 *mac_addr = NULL;
3133
3134         if (info->attrs[NL80211_ATTR_MAC])
3135                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3136
3137         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3138             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3139             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
3140             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3141                 return -EINVAL;
3142
3143         if (!rdev->ops->del_station)
3144                 return -EOPNOTSUPP;
3145
3146         return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
3147 }
3148
3149 static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3150                                 int flags, struct net_device *dev,
3151                                 u8 *dst, u8 *next_hop,
3152                                 struct mpath_info *pinfo)
3153 {
3154         void *hdr;
3155         struct nlattr *pinfoattr;
3156
3157         hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3158         if (!hdr)
3159                 return -1;
3160
3161         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3162             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3163             nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3164             nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3165                 goto nla_put_failure;
3166
3167         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3168         if (!pinfoattr)
3169                 goto nla_put_failure;
3170         if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3171             nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3172                         pinfo->frame_qlen))
3173                 goto nla_put_failure;
3174         if (((pinfo->filled & MPATH_INFO_SN) &&
3175              nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3176             ((pinfo->filled & MPATH_INFO_METRIC) &&
3177              nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3178                          pinfo->metric)) ||
3179             ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3180              nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3181                          pinfo->exptime)) ||
3182             ((pinfo->filled & MPATH_INFO_FLAGS) &&
3183              nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3184                         pinfo->flags)) ||
3185             ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3186              nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3187                          pinfo->discovery_timeout)) ||
3188             ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3189              nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3190                         pinfo->discovery_retries)))
3191                 goto nla_put_failure;
3192
3193         nla_nest_end(msg, pinfoattr);
3194
3195         return genlmsg_end(msg, hdr);
3196
3197  nla_put_failure:
3198         genlmsg_cancel(msg, hdr);
3199         return -EMSGSIZE;
3200 }
3201
3202 static int nl80211_dump_mpath(struct sk_buff *skb,
3203                               struct netlink_callback *cb)
3204 {
3205         struct mpath_info pinfo;
3206         struct cfg80211_registered_device *dev;
3207         struct net_device *netdev;
3208         u8 dst[ETH_ALEN];
3209         u8 next_hop[ETH_ALEN];
3210         int path_idx = cb->args[1];
3211         int err;
3212
3213         err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3214         if (err)
3215                 return err;
3216
3217         if (!dev->ops->dump_mpath) {
3218                 err = -EOPNOTSUPP;
3219                 goto out_err;
3220         }
3221
3222         if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3223                 err = -EOPNOTSUPP;
3224                 goto out_err;
3225         }
3226
3227         while (1) {
3228                 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3229                                            dst, next_hop, &pinfo);
3230                 if (err == -ENOENT)
3231                         break;
3232                 if (err)
3233                         goto out_err;
3234
3235                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3236                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
3237                                        netdev, dst, next_hop,
3238                                        &pinfo) < 0)
3239                         goto out;
3240
3241                 path_idx++;
3242         }
3243
3244
3245  out:
3246         cb->args[1] = path_idx;
3247         err = skb->len;
3248  out_err:
3249         nl80211_finish_netdev_dump(dev);
3250         return err;
3251 }
3252
3253 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3254 {
3255         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3256         int err;
3257         struct net_device *dev = info->user_ptr[1];
3258         struct mpath_info pinfo;
3259         struct sk_buff *msg;
3260         u8 *dst = NULL;
3261         u8 next_hop[ETH_ALEN];
3262
3263         memset(&pinfo, 0, sizeof(pinfo));
3264
3265         if (!info->attrs[NL80211_ATTR_MAC])
3266                 return -EINVAL;
3267
3268         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3269
3270         if (!rdev->ops->get_mpath)
3271                 return -EOPNOTSUPP;
3272
3273         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3274                 return -EOPNOTSUPP;
3275
3276         err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
3277         if (err)
3278                 return err;
3279
3280         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3281         if (!msg)
3282                 return -ENOMEM;
3283
3284         if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
3285                                  dev, dst, next_hop, &pinfo) < 0) {
3286                 nlmsg_free(msg);
3287                 return -ENOBUFS;
3288         }
3289
3290         return genlmsg_reply(msg, info);
3291 }
3292
3293 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3294 {
3295         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3296         struct net_device *dev = info->user_ptr[1];
3297         u8 *dst = NULL;
3298         u8 *next_hop = NULL;
3299
3300         if (!info->attrs[NL80211_ATTR_MAC])
3301                 return -EINVAL;
3302
3303         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3304                 return -EINVAL;
3305
3306         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3307         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3308
3309         if (!rdev->ops->change_mpath)
3310                 return -EOPNOTSUPP;
3311
3312         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3313                 return -EOPNOTSUPP;
3314
3315         return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
3316 }
3317
3318 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3319 {
3320         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3321         struct net_device *dev = info->user_ptr[1];
3322         u8 *dst = NULL;
3323         u8 *next_hop = NULL;
3324
3325         if (!info->attrs[NL80211_ATTR_MAC])
3326                 return -EINVAL;
3327
3328         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3329                 return -EINVAL;
3330
3331         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3332         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3333
3334         if (!rdev->ops->add_mpath)
3335                 return -EOPNOTSUPP;
3336
3337         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3338                 return -EOPNOTSUPP;
3339
3340         return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
3341 }
3342
3343 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3344 {
3345         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3346         struct net_device *dev = info->user_ptr[1];
3347         u8 *dst = NULL;
3348
3349         if (info->attrs[NL80211_ATTR_MAC])
3350                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3351
3352         if (!rdev->ops->del_mpath)
3353                 return -EOPNOTSUPP;
3354
3355         return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
3356 }
3357
3358 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3359 {
3360         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3361         struct net_device *dev = info->user_ptr[1];
3362         struct bss_parameters params;
3363
3364         memset(&params, 0, sizeof(params));
3365         /* default to not changing parameters */
3366         params.use_cts_prot = -1;
3367         params.use_short_preamble = -1;
3368         params.use_short_slot_time = -1;
3369         params.ap_isolate = -1;
3370         params.ht_opmode = -1;
3371
3372         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3373                 params.use_cts_prot =
3374                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3375         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3376                 params.use_short_preamble =
3377                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3378         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3379                 params.use_short_slot_time =
3380                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
3381         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3382                 params.basic_rates =
3383                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3384                 params.basic_rates_len =
3385                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3386         }
3387         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3388                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
3389         if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3390                 params.ht_opmode =
3391                         nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
3392
3393         if (!rdev->ops->change_bss)
3394                 return -EOPNOTSUPP;
3395
3396         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3397             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3398                 return -EOPNOTSUPP;
3399
3400         return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
3401 }
3402
3403 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
3404         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
3405         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
3406         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
3407         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
3408         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
3409         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
3410 };
3411
3412 static int parse_reg_rule(struct nlattr *tb[],
3413         struct ieee80211_reg_rule *reg_rule)
3414 {
3415         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3416         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3417
3418         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3419                 return -EINVAL;
3420         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3421                 return -EINVAL;
3422         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3423                 return -EINVAL;
3424         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3425                 return -EINVAL;
3426         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3427                 return -EINVAL;
3428
3429         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3430
3431         freq_range->start_freq_khz =
3432                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3433         freq_range->end_freq_khz =
3434                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3435         freq_range->max_bandwidth_khz =
3436                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3437
3438         power_rule->max_eirp =
3439                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3440
3441         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3442                 power_rule->max_antenna_gain =
3443                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3444
3445         return 0;
3446 }
3447
3448 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3449 {
3450         int r;
3451         char *data = NULL;
3452
3453         /*
3454          * You should only get this when cfg80211 hasn't yet initialized
3455          * completely when built-in to the kernel right between the time
3456          * window between nl80211_init() and regulatory_init(), if that is
3457          * even possible.
3458          */
3459         mutex_lock(&cfg80211_mutex);
3460         if (unlikely(!cfg80211_regdomain)) {
3461                 mutex_unlock(&cfg80211_mutex);
3462                 return -EINPROGRESS;
3463         }
3464         mutex_unlock(&cfg80211_mutex);
3465
3466         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3467                 return -EINVAL;
3468
3469         data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3470
3471         r = regulatory_hint_user(data);
3472
3473         return r;
3474 }
3475
3476 static int nl80211_get_mesh_config(struct sk_buff *skb,
3477                                    struct genl_info *info)
3478 {
3479         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3480         struct net_device *dev = info->user_ptr[1];
3481         struct wireless_dev *wdev = dev->ieee80211_ptr;
3482         struct mesh_config cur_params;
3483         int err = 0;
3484         void *hdr;
3485         struct nlattr *pinfoattr;
3486         struct sk_buff *msg;
3487
3488         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3489                 return -EOPNOTSUPP;
3490
3491         if (!rdev->ops->get_mesh_config)
3492                 return -EOPNOTSUPP;
3493
3494         wdev_lock(wdev);
3495         /* If not connected, get default parameters */
3496         if (!wdev->mesh_id_len)
3497                 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3498         else
3499                 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
3500                                                  &cur_params);
3501         wdev_unlock(wdev);
3502
3503         if (err)
3504                 return err;
3505
3506         /* Draw up a netlink message to send back */
3507         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3508         if (!msg)
3509                 return -ENOMEM;
3510         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3511                              NL80211_CMD_GET_MESH_CONFIG);
3512         if (!hdr)
3513                 goto out;
3514         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
3515         if (!pinfoattr)
3516                 goto nla_put_failure;
3517         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3518             nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3519                         cur_params.dot11MeshRetryTimeout) ||
3520             nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3521                         cur_params.dot11MeshConfirmTimeout) ||
3522             nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3523                         cur_params.dot11MeshHoldingTimeout) ||
3524             nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3525                         cur_params.dot11MeshMaxPeerLinks) ||
3526             nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3527                        cur_params.dot11MeshMaxRetries) ||
3528             nla_put_u8(msg, NL80211_MESHCONF_TTL,
3529                        cur_params.dot11MeshTTL) ||
3530             nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3531                        cur_params.element_ttl) ||
3532             nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3533                        cur_params.auto_open_plinks) ||
3534             nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3535                         cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
3536             nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3537                        cur_params.dot11MeshHWMPmaxPREQretries) ||
3538             nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3539                         cur_params.path_refresh_time) ||
3540             nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3541                         cur_params.min_discovery_timeout) ||
3542             nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3543                         cur_params.dot11MeshHWMPactivePathTimeout) ||
3544             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3545                         cur_params.dot11MeshHWMPpreqMinInterval) ||
3546             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3547                         cur_params.dot11MeshHWMPperrMinInterval) ||
3548             nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3549                         cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3550             nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3551                        cur_params.dot11MeshHWMPRootMode) ||
3552             nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3553                         cur_params.dot11MeshHWMPRannInterval) ||
3554             nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3555                        cur_params.dot11MeshGateAnnouncementProtocol) ||
3556             nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3557                        cur_params.dot11MeshForwarding) ||
3558             nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
3559                         cur_params.rssi_threshold) ||
3560             nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
3561                         cur_params.ht_opmode) ||
3562             nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3563                         cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3564             nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3565                         cur_params.dot11MeshHWMProotInterval) ||
3566             nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3567                         cur_params.dot11MeshHWMPconfirmationInterval))
3568                 goto nla_put_failure;
3569         nla_nest_end(msg, pinfoattr);
3570         genlmsg_end(msg, hdr);
3571         return genlmsg_reply(msg, info);
3572
3573  nla_put_failure:
3574         genlmsg_cancel(msg, hdr);
3575  out:
3576         nlmsg_free(msg);
3577         return -ENOBUFS;
3578 }
3579
3580 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
3581         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3582         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3583         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3584         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3585         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3586         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
3587         [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
3588         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3589         [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
3590         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3591         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3592         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3593         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3594         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3595         [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
3596         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3597         [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
3598         [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
3599         [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
3600         [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
3601         [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3602         [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
3603         [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3604         [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
3605         [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
3606 };
3607
3608 static const struct nla_policy
3609         nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3610         [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
3611         [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3612         [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
3613         [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
3614         [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
3615                                     .len = IEEE80211_MAX_DATA_LEN },
3616         [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
3617 };
3618
3619 static int nl80211_parse_mesh_config(struct genl_info *info,
3620                                      struct mesh_config *cfg,
3621                                      u32 *mask_out)
3622 {
3623         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
3624         u32 mask = 0;
3625
3626 #define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3627 do {\
3628         if (table[attr_num]) {\
3629                 cfg->param = nla_fn(table[attr_num]); \
3630                 mask |= (1 << (attr_num - 1)); \
3631         } \
3632 } while (0);\
3633
3634
3635         if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
3636                 return -EINVAL;
3637         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
3638                              info->attrs[NL80211_ATTR_MESH_CONFIG],
3639                              nl80211_meshconf_params_policy))
3640                 return -EINVAL;
3641
3642         /* This makes sure that there aren't more than 32 mesh config
3643          * parameters (otherwise our bitfield scheme would not work.) */
3644         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3645
3646         /* Fill in the params struct */
3647         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3648                                   mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3649                                   nla_get_u16);
3650         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3651                                   mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3652                                   nla_get_u16);
3653         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3654                                   mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3655                                   nla_get_u16);
3656         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3657                                   mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3658                                   nla_get_u16);
3659         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3660                                   mask, NL80211_MESHCONF_MAX_RETRIES,
3661                                   nla_get_u8);
3662         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3663                                   mask, NL80211_MESHCONF_TTL, nla_get_u8);
3664         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3665                                   mask, NL80211_MESHCONF_ELEMENT_TTL,
3666                                   nla_get_u8);
3667         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3668                                   mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3669                                   nla_get_u8);
3670         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3671                                   NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3672                                   nla_get_u32);
3673         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3674                                   mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3675                                   nla_get_u8);
3676         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3677                                   mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3678                                   nla_get_u32);
3679         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3680                                   mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3681                                   nla_get_u16);
3682         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3683                                   NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3684                                   nla_get_u32);
3685         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3686                                   mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3687                                   nla_get_u16);
3688         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
3689                                   mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3690                                   nla_get_u16);
3691         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3692                                   dot11MeshHWMPnetDiameterTraversalTime, mask,
3693                                   NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3694                                   nla_get_u16);
3695         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3696                                   NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3697         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3698                                   NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3699                                   nla_get_u16);
3700         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3701                                   dot11MeshGateAnnouncementProtocol, mask,
3702                                   NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3703                                   nla_get_u8);
3704         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
3705                                   mask, NL80211_MESHCONF_FORWARDING,
3706                                   nla_get_u8);
3707         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
3708                                   mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3709                                   nla_get_u32);
3710         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
3711                                   mask, NL80211_MESHCONF_HT_OPMODE,
3712                                   nla_get_u16);
3713         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3714                                   mask,
3715                                   NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3716                                   nla_get_u32);
3717         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3718                                   mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3719                                   nla_get_u16);
3720         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3721                                   dot11MeshHWMPconfirmationInterval, mask,
3722                                   NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3723                                   nla_get_u16);
3724         if (mask_out)
3725                 *mask_out = mask;
3726
3727         return 0;
3728
3729 #undef FILL_IN_MESH_PARAM_IF_SET
3730 }
3731
3732 static int nl80211_parse_mesh_setup(struct genl_info *info,
3733                                      struct mesh_setup *setup)
3734 {
3735         struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3736
3737         if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3738                 return -EINVAL;
3739         if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3740                              info->attrs[NL80211_ATTR_MESH_SETUP],
3741                              nl80211_mesh_setup_params_policy))
3742                 return -EINVAL;
3743
3744         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3745                 setup->sync_method =
3746                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3747                  IEEE80211_SYNC_METHOD_VENDOR :
3748                  IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3749
3750         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3751                 setup->path_sel_proto =
3752                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3753                  IEEE80211_PATH_PROTOCOL_VENDOR :
3754                  IEEE80211_PATH_PROTOCOL_HWMP;
3755
3756         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3757                 setup->path_metric =
3758                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3759                  IEEE80211_PATH_METRIC_VENDOR :
3760                  IEEE80211_PATH_METRIC_AIRTIME;
3761
3762
3763         if (tb[NL80211_MESH_SETUP_IE]) {
3764                 struct nlattr *ieattr =
3765                         tb[NL80211_MESH_SETUP_IE];
3766                 if (!is_valid_ie_attr(ieattr))
3767                         return -EINVAL;
3768                 setup->ie = nla_data(ieattr);
3769                 setup->ie_len = nla_len(ieattr);
3770         }
3771         setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3772         setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
3773
3774         return 0;
3775 }
3776
3777 static int nl80211_update_mesh_config(struct sk_buff *skb,
3778                                       struct genl_info *info)
3779 {
3780         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3781         struct net_device *dev = info->user_ptr[1];
3782         struct wireless_dev *wdev = dev->ieee80211_ptr;
3783         struct mesh_config cfg;
3784         u32 mask;
3785         int err;
3786
3787         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3788                 return -EOPNOTSUPP;
3789
3790         if (!rdev->ops->update_mesh_config)
3791                 return -EOPNOTSUPP;
3792
3793         err = nl80211_parse_mesh_config(info, &cfg, &mask);
3794         if (err)
3795                 return err;
3796
3797         wdev_lock(wdev);
3798         if (!wdev->mesh_id_len)
3799                 err = -ENOLINK;
3800
3801         if (!err)
3802                 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
3803                                                     mask, &cfg);
3804
3805         wdev_unlock(wdev);
3806
3807         return err;
3808 }
3809
3810 static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3811 {
3812         struct sk_buff *msg;
3813         void *hdr = NULL;
3814         struct nlattr *nl_reg_rules;
3815         unsigned int i;
3816         int err = -EINVAL;
3817
3818         mutex_lock(&cfg80211_mutex);
3819
3820         if (!cfg80211_regdomain)
3821                 goto out;
3822
3823         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3824         if (!msg) {
3825                 err = -ENOBUFS;
3826                 goto out;
3827         }
3828
3829         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3830                              NL80211_CMD_GET_REG);
3831         if (!hdr)
3832                 goto put_failure;
3833
3834         if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3835                            cfg80211_regdomain->alpha2) ||
3836             (cfg80211_regdomain->dfs_region &&
3837              nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3838                         cfg80211_regdomain->dfs_region)))
3839                 goto nla_put_failure;
3840
3841         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3842         if (!nl_reg_rules)
3843                 goto nla_put_failure;
3844
3845         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3846                 struct nlattr *nl_reg_rule;
3847                 const struct ieee80211_reg_rule *reg_rule;
3848                 const struct ieee80211_freq_range *freq_range;
3849                 const struct ieee80211_power_rule *power_rule;
3850
3851                 reg_rule = &cfg80211_regdomain->reg_rules[i];
3852                 freq_range = &reg_rule->freq_range;
3853                 power_rule = &reg_rule->power_rule;
3854
3855                 nl_reg_rule = nla_nest_start(msg, i);
3856                 if (!nl_reg_rule)
3857                         goto nla_put_failure;
3858
3859                 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3860                                 reg_rule->flags) ||
3861                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3862                                 freq_range->start_freq_khz) ||
3863                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3864                                 freq_range->end_freq_khz) ||
3865                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3866                                 freq_range->max_bandwidth_khz) ||
3867                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3868                                 power_rule->max_antenna_gain) ||
3869                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3870                                 power_rule->max_eirp))
3871                         goto nla_put_failure;
3872
3873                 nla_nest_end(msg, nl_reg_rule);
3874         }
3875
3876         nla_nest_end(msg, nl_reg_rules);
3877
3878         genlmsg_end(msg, hdr);
3879         err = genlmsg_reply(msg, info);
3880         goto out;
3881
3882 nla_put_failure:
3883         genlmsg_cancel(msg, hdr);
3884 put_failure:
3885         nlmsg_free(msg);
3886         err = -EMSGSIZE;
3887 out:
3888         mutex_unlock(&cfg80211_mutex);
3889         return err;
3890 }
3891
3892 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3893 {
3894         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3895         struct nlattr *nl_reg_rule;
3896         char *alpha2 = NULL;
3897         int rem_reg_rules = 0, r = 0;
3898         u32 num_rules = 0, rule_idx = 0, size_of_regd;
3899         u8 dfs_region = 0;
3900         struct ieee80211_regdomain *rd = NULL;
3901
3902         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3903                 return -EINVAL;
3904
3905         if (!info->attrs[NL80211_ATTR_REG_RULES])
3906                 return -EINVAL;
3907
3908         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3909
3910         if (info->attrs[NL80211_ATTR_DFS_REGION])
3911                 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
3912
3913         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3914                         rem_reg_rules) {
3915                 num_rules++;
3916                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
3917                         return -EINVAL;
3918         }
3919
3920         mutex_lock(&cfg80211_mutex);
3921
3922         if (!reg_is_valid_request(alpha2)) {
3923                 r = -EINVAL;
3924                 goto bad_reg;
3925         }
3926
3927         size_of_regd = sizeof(struct ieee80211_regdomain) +
3928                 (num_rules * sizeof(struct ieee80211_reg_rule));
3929
3930         rd = kzalloc(size_of_regd, GFP_KERNEL);
3931         if (!rd) {
3932                 r = -ENOMEM;
3933                 goto bad_reg;
3934         }
3935
3936         rd->n_reg_rules = num_rules;
3937         rd->alpha2[0] = alpha2[0];
3938         rd->alpha2[1] = alpha2[1];
3939
3940         /*
3941          * Disable DFS master mode if the DFS region was
3942          * not supported or known on this kernel.
3943          */
3944         if (reg_supported_dfs_region(dfs_region))
3945                 rd->dfs_region = dfs_region;
3946
3947         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3948                         rem_reg_rules) {
3949                 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3950                         nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3951                         reg_rule_policy);
3952                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3953                 if (r)
3954                         goto bad_reg;
3955
3956                 rule_idx++;
3957
3958                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3959                         r = -EINVAL;
3960                         goto bad_reg;
3961                 }
3962         }
3963
3964         BUG_ON(rule_idx != num_rules);
3965
3966         r = set_regdom(rd);
3967
3968         mutex_unlock(&cfg80211_mutex);
3969
3970         return r;
3971
3972  bad_reg:
3973         mutex_unlock(&cfg80211_mutex);
3974         kfree(rd);
3975         return r;
3976 }
3977
3978 static int validate_scan_freqs(struct nlattr *freqs)
3979 {
3980         struct nlattr *attr1, *attr2;
3981         int n_channels = 0, tmp1, tmp2;
3982
3983         nla_for_each_nested(attr1, freqs, tmp1) {
3984                 n_channels++;
3985                 /*
3986                  * Some hardware has a limited channel list for
3987                  * scanning, and it is pretty much nonsensical
3988                  * to scan for a channel twice, so disallow that
3989                  * and don't require drivers to check that the
3990                  * channel list they get isn't longer than what
3991                  * they can scan, as long as they can scan all
3992                  * the channels they registered at once.
3993                  */
3994                 nla_for_each_nested(attr2, freqs, tmp2)
3995                         if (attr1 != attr2 &&
3996                             nla_get_u32(attr1) == nla_get_u32(attr2))
3997                                 return 0;
3998         }
3999
4000         return n_channels;
4001 }
4002
4003 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4004 {
4005         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4006         struct net_device *dev = info->user_ptr[1];
4007         struct cfg80211_scan_request *request;
4008         struct nlattr *attr;
4009         struct wiphy *wiphy;
4010         int err, tmp, n_ssids = 0, n_channels, i;
4011         size_t ie_len;
4012
4013         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4014                 return -EINVAL;
4015
4016         wiphy = &rdev->wiphy;
4017
4018         if (!rdev->ops->scan)
4019                 return -EOPNOTSUPP;
4020
4021         if (rdev->scan_req)
4022                 return -EBUSY;
4023
4024         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4025                 n_channels = validate_scan_freqs(
4026                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4027                 if (!n_channels)
4028                         return -EINVAL;
4029         } else {
4030                 enum ieee80211_band band;
4031                 n_channels = 0;
4032
4033                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4034                         if (wiphy->bands[band])
4035                                 n_channels += wiphy->bands[band]->n_channels;
4036         }
4037
4038         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4039                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4040                         n_ssids++;
4041
4042         if (n_ssids > wiphy->max_scan_ssids)
4043                 return -EINVAL;
4044
4045         if (info->attrs[NL80211_ATTR_IE])
4046                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4047         else
4048                 ie_len = 0;
4049
4050         if (ie_len > wiphy->max_scan_ie_len)
4051                 return -EINVAL;
4052
4053         request = kzalloc(sizeof(*request)
4054                         + sizeof(*request->ssids) * n_ssids
4055                         + sizeof(*request->channels) * n_channels
4056                         + ie_len, GFP_KERNEL);
4057         if (!request)
4058                 return -ENOMEM;
4059
4060         if (n_ssids)
4061                 request->ssids = (void *)&request->channels[n_channels];
4062         request->n_ssids = n_ssids;
4063         if (ie_len) {
4064                 if (request->ssids)
4065                         request->ie = (void *)(request->ssids + n_ssids);
4066                 else
4067                         request->ie = (void *)(request->channels + n_channels);
4068         }
4069
4070         i = 0;
4071         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4072                 /* user specified, bail out if channel not found */
4073                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
4074                         struct ieee80211_channel *chan;
4075
4076                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4077
4078                         if (!chan) {
4079                                 err = -EINVAL;
4080                                 goto out_free;
4081                         }
4082
4083                         /* ignore disabled channels */
4084                         if (chan->flags & IEEE80211_CHAN_DISABLED)
4085                                 continue;
4086
4087                         request->channels[i] = chan;
4088                         i++;
4089                 }
4090         } else {
4091                 enum ieee80211_band band;
4092
4093                 /* all channels */
4094                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4095                         int j;
4096                         if (!wiphy->bands[band])
4097                                 continue;
4098                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4099                                 struct ieee80211_channel *chan;
4100
4101                                 chan = &wiphy->bands[band]->channels[j];
4102
4103                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
4104                                         continue;
4105
4106                                 request->channels[i] = chan;
4107                                 i++;
4108                         }
4109                 }
4110         }
4111
4112         if (!i) {
4113                 err = -EINVAL;
4114                 goto out_free;
4115         }
4116
4117         request->n_channels = i;
4118
4119         i = 0;
4120         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4121                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
4122                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
4123                                 err = -EINVAL;
4124                                 goto out_free;
4125                         }
4126                         request->ssids[i].ssid_len = nla_len(attr);
4127                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
4128                         i++;
4129                 }
4130         }
4131
4132         if (info->attrs[NL80211_ATTR_IE]) {
4133                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4134                 memcpy((void *)request->ie,
4135                        nla_data(info->attrs[NL80211_ATTR_IE]),
4136                        request->ie_len);
4137         }
4138
4139         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
4140                 if (wiphy->bands[i])
4141                         request->rates[i] =
4142                                 (1 << wiphy->bands[i]->n_bitrates) - 1;
4143
4144         if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4145                 nla_for_each_nested(attr,
4146                                     info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4147                                     tmp) {
4148                         enum ieee80211_band band = nla_type(attr);
4149
4150                         if (band < 0 || band >= IEEE80211_NUM_BANDS) {
4151                                 err = -EINVAL;
4152                                 goto out_free;
4153                         }
4154                         err = ieee80211_get_ratemask(wiphy->bands[band],
4155                                                      nla_data(attr),
4156                                                      nla_len(attr),
4157                                                      &request->rates[band]);
4158                         if (err)
4159                                 goto out_free;
4160                 }
4161         }
4162
4163         request->no_cck =
4164                 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4165
4166         request->dev = dev;
4167         request->wiphy = &rdev->wiphy;
4168
4169         rdev->scan_req = request;
4170         err = rdev->ops->scan(&rdev->wiphy, dev, request);
4171
4172         if (!err) {
4173                 nl80211_send_scan_start(rdev, dev);
4174                 dev_hold(dev);
4175         } else {
4176  out_free:
4177                 rdev->scan_req = NULL;
4178                 kfree(request);
4179         }
4180
4181         return err;
4182 }
4183
4184 static int nl80211_start_sched_scan(struct sk_buff *skb,
4185                                     struct genl_info *info)
4186 {
4187         struct cfg80211_sched_scan_request *request;
4188         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4189         struct net_device *dev = info->user_ptr[1];
4190         struct nlattr *attr;
4191         struct wiphy *wiphy;
4192         int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
4193         u32 interval;
4194         enum ieee80211_band band;
4195         size_t ie_len;
4196         struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
4197
4198         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4199             !rdev->ops->sched_scan_start)
4200                 return -EOPNOTSUPP;
4201
4202         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4203                 return -EINVAL;
4204
4205         if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4206                 return -EINVAL;
4207
4208         interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4209         if (interval == 0)
4210                 return -EINVAL;
4211
4212         wiphy = &rdev->wiphy;
4213
4214         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4215                 n_channels = validate_scan_freqs(
4216                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4217                 if (!n_channels)
4218                         return -EINVAL;
4219         } else {
4220                 n_channels = 0;
4221
4222                 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4223                         if (wiphy->bands[band])
4224                                 n_channels += wiphy->bands[band]->n_channels;
4225         }
4226
4227         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4228                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4229                                     tmp)
4230                         n_ssids++;
4231
4232         if (n_ssids > wiphy->max_sched_scan_ssids)
4233                 return -EINVAL;
4234
4235         if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4236                 nla_for_each_nested(attr,
4237                                     info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4238                                     tmp)
4239                         n_match_sets++;
4240
4241         if (n_match_sets > wiphy->max_match_sets)
4242                 return -EINVAL;
4243
4244         if (info->attrs[NL80211_ATTR_IE])
4245                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4246         else
4247                 ie_len = 0;
4248
4249         if (ie_len > wiphy->max_sched_scan_ie_len)
4250                 return -EINVAL;
4251
4252         mutex_lock(&rdev->sched_scan_mtx);
4253
4254         if (rdev->sched_scan_req) {
4255                 err = -EINPROGRESS;
4256                 goto out;
4257         }
4258
4259         request = kzalloc(sizeof(*request)
4260                         + sizeof(*request->ssids) * n_ssids
4261                         + sizeof(*request->match_sets) * n_match_sets
4262                         + sizeof(*request->channels) * n_channels
4263                         + ie_len, GFP_KERNEL);
4264         if (!request) {
4265                 err = -ENOMEM;
4266                 goto out;
4267         }
4268
4269         if (n_ssids)
4270                 request->ssids = (void *)&request->channels[n_channels];
4271         request->n_ssids = n_ssids;
4272         if (ie_len) {
4273                 if (request->ssids)
4274                         request->ie = (void *)(request->ssids + n_ssids);
4275                 else
4276                         request->ie = (void *)(request->channels + n_channels);
4277         }
4278
4279         if (n_match_sets) {
4280                 if (request->ie)
4281                         request->match_sets = (void *)(request->ie + ie_len);
4282                 else if (request->ssids)
4283                         request->match_sets =
4284                                 (void *)(request->ssids + n_ssids);
4285                 else
4286                         request->match_sets =
4287                                 (void *)(request->channels + n_channels);
4288         }
4289         request->n_match_sets = n_match_sets;
4290
4291         i = 0;
4292         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4293                 /* user specified, bail out if channel not found */
4294                 nla_for_each_nested(attr,
4295                                     info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4296                                     tmp) {
4297                         struct ieee80211_channel *chan;
4298
4299                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4300
4301                         if (!chan) {
4302                                 err = -EINVAL;
4303                                 goto out_free;
4304                         }
4305
4306                         /* ignore disabled channels */
4307                         if (chan->flags & IEEE80211_CHAN_DISABLED)
4308                                 continue;
4309
4310                         request->channels[i] = chan;
4311                         i++;
4312                 }
4313         } else {
4314                 /* all channels */
4315                 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4316                         int j;
4317                         if (!wiphy->bands[band])
4318                                 continue;
4319                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4320                                 struct ieee80211_channel *chan;
4321
4322                                 chan = &wiphy->bands[band]->channels[j];
4323
4324                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
4325                                         continue;
4326
4327                                 request->channels[i] = chan;
4328                                 i++;
4329                         }
4330                 }
4331         }
4332
4333         if (!i) {
4334                 err = -EINVAL;
4335                 goto out_free;
4336         }
4337
4338         request->n_channels = i;
4339
4340         i = 0;
4341         if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4342                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4343                                     tmp) {
4344                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
4345                                 err = -EINVAL;
4346                                 goto out_free;
4347                         }
4348                         request->ssids[i].ssid_len = nla_len(attr);
4349                         memcpy(request->ssids[i].ssid, nla_data(attr),
4350                                nla_len(attr));
4351                         i++;
4352                 }
4353         }
4354
4355         i = 0;
4356         if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4357                 nla_for_each_nested(attr,
4358                                     info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4359                                     tmp) {
4360                         struct nlattr *ssid;
4361
4362                         nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4363                                   nla_data(attr), nla_len(attr),
4364                                   nl80211_match_policy);
4365                         ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
4366                         if (ssid) {
4367                                 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4368                                         err = -EINVAL;
4369                                         goto out_free;
4370                                 }
4371                                 memcpy(request->match_sets[i].ssid.ssid,
4372                                        nla_data(ssid), nla_len(ssid));
4373                                 request->match_sets[i].ssid.ssid_len =
4374                                         nla_len(ssid);
4375                         }
4376                         i++;
4377                 }
4378         }
4379
4380         if (info->attrs[NL80211_ATTR_IE]) {
4381                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4382                 memcpy((void *)request->ie,
4383                        nla_data(info->attrs[NL80211_ATTR_IE]),
4384                        request->ie_len);
4385         }
4386
4387         request->dev = dev;
4388         request->wiphy = &rdev->wiphy;
4389         request->interval = interval;
4390
4391         err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4392         if (!err) {
4393                 rdev->sched_scan_req = request;
4394                 nl80211_send_sched_scan(rdev, dev,
4395                                         NL80211_CMD_START_SCHED_SCAN);
4396                 goto out;
4397         }
4398
4399 out_free:
4400         kfree(request);
4401 out:
4402         mutex_unlock(&rdev->sched_scan_mtx);
4403         return err;
4404 }
4405
4406 static int nl80211_stop_sched_scan(struct sk_buff *skb,
4407                                    struct genl_info *info)
4408 {
4409         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4410         int err;
4411
4412         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4413             !rdev->ops->sched_scan_stop)
4414                 return -EOPNOTSUPP;
4415
4416         mutex_lock(&rdev->sched_scan_mtx);
4417         err = __cfg80211_stop_sched_scan(rdev, false);
4418         mutex_unlock(&rdev->sched_scan_mtx);
4419
4420         return err;
4421 }
4422
4423 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4424                             u32 seq, int flags,
4425                             struct cfg80211_registered_device *rdev,
4426                             struct wireless_dev *wdev,
4427                             struct cfg80211_internal_bss *intbss)
4428 {
4429         struct cfg80211_bss *res = &intbss->pub;
4430         void *hdr;
4431         struct nlattr *bss;
4432
4433         ASSERT_WDEV_LOCK(wdev);
4434
4435         hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
4436                              NL80211_CMD_NEW_SCAN_RESULTS);
4437         if (!hdr)
4438                 return -1;
4439
4440         genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4441
4442         if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4443             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4444                 goto nla_put_failure;
4445
4446         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4447         if (!bss)
4448                 goto nla_put_failure;
4449         if ((!is_zero_ether_addr(res->bssid) &&
4450              nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4451             (res->information_elements && res->len_information_elements &&
4452              nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4453                      res->len_information_elements,
4454                      res->information_elements)) ||
4455             (res->beacon_ies && res->len_beacon_ies &&
4456              res->beacon_ies != res->information_elements &&
4457              nla_put(msg, NL80211_BSS_BEACON_IES,
4458                      res->len_beacon_ies, res->beacon_ies)))
4459                 goto nla_put_failure;
4460         if (res->tsf &&
4461             nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4462                 goto nla_put_failure;
4463         if (res->beacon_interval &&
4464             nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4465                 goto nla_put_failure;
4466         if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4467             nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4468             nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4469                         jiffies_to_msecs(jiffies - intbss->ts)))
4470                 goto nla_put_failure;
4471
4472         switch (rdev->wiphy.signal_type) {
4473         case CFG80211_SIGNAL_TYPE_MBM:
4474                 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4475                         goto nla_put_failure;
4476                 break;
4477         case CFG80211_SIGNAL_TYPE_UNSPEC:
4478                 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4479                         goto nla_put_failure;
4480                 break;
4481         default:
4482                 break;
4483         }
4484
4485         switch (wdev->iftype) {
4486         case NL80211_IFTYPE_P2P_CLIENT:
4487         case NL80211_IFTYPE_STATION:
4488                 if (intbss == wdev->current_bss &&
4489                     nla_put_u32(msg, NL80211_BSS_STATUS,
4490                                 NL80211_BSS_STATUS_ASSOCIATED))
4491                         goto nla_put_failure;
4492                 break;
4493         case NL80211_IFTYPE_ADHOC:
4494                 if (intbss == wdev->current_bss &&
4495                     nla_put_u32(msg, NL80211_BSS_STATUS,
4496                                 NL80211_BSS_STATUS_IBSS_JOINED))
4497                         goto nla_put_failure;
4498                 break;
4499         default:
4500                 break;
4501         }
4502
4503         nla_nest_end(msg, bss);
4504
4505         return genlmsg_end(msg, hdr);
4506
4507  nla_put_failure:
4508         genlmsg_cancel(msg, hdr);
4509         return -EMSGSIZE;
4510 }
4511
4512 static int nl80211_dump_scan(struct sk_buff *skb,
4513                              struct netlink_callback *cb)
4514 {
4515         struct cfg80211_registered_device *rdev;
4516         struct net_device *dev;
4517         struct cfg80211_internal_bss *scan;
4518         struct wireless_dev *wdev;
4519         int start = cb->args[1], idx = 0;
4520         int err;
4521
4522         err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4523         if (err)
4524                 return err;
4525
4526         wdev = dev->ieee80211_ptr;
4527
4528         wdev_lock(wdev);
4529         spin_lock_bh(&rdev->bss_lock);
4530         cfg80211_bss_expire(rdev);
4531
4532         cb->seq = rdev->bss_generation;
4533
4534         list_for_each_entry(scan, &rdev->bss_list, list) {
4535                 if (++idx <= start)
4536                         continue;
4537                 if (nl80211_send_bss(skb, cb,
4538                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4539                                 rdev, wdev, scan) < 0) {
4540                         idx--;
4541                         break;
4542                 }
4543         }
4544
4545         spin_unlock_bh(&rdev->bss_lock);
4546         wdev_unlock(wdev);
4547
4548         cb->args[1] = idx;
4549         nl80211_finish_netdev_dump(rdev);
4550
4551         return skb->len;
4552 }
4553
4554 static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4555                                 int flags, struct net_device *dev,
4556                                 struct survey_info *survey)
4557 {
4558         void *hdr;
4559         struct nlattr *infoattr;
4560
4561         hdr = nl80211hdr_put(msg, pid, seq, flags,
4562                              NL80211_CMD_NEW_SURVEY_RESULTS);
4563         if (!hdr)
4564                 return -ENOMEM;
4565
4566         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4567                 goto nla_put_failure;
4568
4569         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4570         if (!infoattr)
4571                 goto nla_put_failure;
4572
4573         if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4574                         survey->channel->center_freq))
4575                 goto nla_put_failure;
4576
4577         if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4578             nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4579                 goto nla_put_failure;
4580         if ((survey->filled & SURVEY_INFO_IN_USE) &&
4581             nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4582                 goto nla_put_failure;
4583         if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4584             nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4585                         survey->channel_time))
4586                 goto nla_put_failure;
4587         if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4588             nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4589                         survey->channel_time_busy))
4590                 goto nla_put_failure;
4591         if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4592             nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4593                         survey->channel_time_ext_busy))
4594                 goto nla_put_failure;
4595         if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4596             nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4597                         survey->channel_time_rx))
4598                 goto nla_put_failure;
4599         if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4600             nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4601                         survey->channel_time_tx))
4602                 goto nla_put_failure;
4603
4604         nla_nest_end(msg, infoattr);
4605
4606         return genlmsg_end(msg, hdr);
4607
4608  nla_put_failure:
4609         genlmsg_cancel(msg, hdr);
4610         return -EMSGSIZE;
4611 }
4612
4613 static int nl80211_dump_survey(struct sk_buff *skb,
4614                         struct netlink_callback *cb)
4615 {
4616         struct survey_info survey;
4617         struct cfg80211_registered_device *dev;
4618         struct net_device *netdev;
4619         int survey_idx = cb->args[1];
4620         int res;
4621
4622         res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4623         if (res)
4624                 return res;
4625
4626         if (!dev->ops->dump_survey) {
4627                 res = -EOPNOTSUPP;
4628                 goto out_err;
4629         }
4630
4631         while (1) {
4632                 struct ieee80211_channel *chan;
4633
4634                 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4635                                             &survey);
4636                 if (res == -ENOENT)
4637                         break;
4638                 if (res)
4639                         goto out_err;
4640
4641                 /* Survey without a channel doesn't make sense */
4642                 if (!survey.channel) {
4643                         res = -EINVAL;
4644                         goto out;
4645                 }
4646
4647                 chan = ieee80211_get_channel(&dev->wiphy,
4648                                              survey.channel->center_freq);
4649                 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4650                         survey_idx++;
4651                         continue;
4652                 }
4653
4654                 if (nl80211_send_survey(skb,
4655                                 NETLINK_CB(cb->skb).pid,
4656                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4657                                 netdev,
4658                                 &survey) < 0)
4659                         goto out;
4660                 survey_idx++;
4661         }
4662
4663  out:
4664         cb->args[1] = survey_idx;
4665         res = skb->len;
4666  out_err:
4667         nl80211_finish_netdev_dump(dev);
4668         return res;
4669 }
4670
4671 static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4672 {
4673         return auth_type <= NL80211_AUTHTYPE_MAX;
4674 }
4675
4676 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4677 {
4678         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4679                                   NL80211_WPA_VERSION_2));
4680 }
4681
4682 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4683 {
4684         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4685         struct net_device *dev = info->user_ptr[1];
4686         struct ieee80211_channel *chan;
4687         const u8 *bssid, *ssid, *ie = NULL;
4688         int err, ssid_len, ie_len = 0;
4689         enum nl80211_auth_type auth_type;
4690         struct key_parse key;
4691         bool local_state_change;
4692
4693         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4694                 return -EINVAL;
4695
4696         if (!info->attrs[NL80211_ATTR_MAC])
4697                 return -EINVAL;
4698
4699         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4700                 return -EINVAL;
4701
4702         if (!info->attrs[NL80211_ATTR_SSID])
4703                 return -EINVAL;
4704
4705         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4706                 return -EINVAL;
4707
4708         err = nl80211_parse_key(info, &key);
4709         if (err)
4710                 return err;
4711
4712         if (key.idx >= 0) {
4713                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4714                         return -EINVAL;
4715                 if (!key.p.key || !key.p.key_len)
4716                         return -EINVAL;
4717                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4718                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4719                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4720                      key.p.key_len != WLAN_KEY_LEN_WEP104))
4721                         return -EINVAL;
4722                 if (key.idx > 4)
4723                         return -EINVAL;
4724         } else {
4725                 key.p.key_len = 0;
4726                 key.p.key = NULL;
4727         }
4728
4729         if (key.idx >= 0) {
4730                 int i;
4731                 bool ok = false;
4732                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4733                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4734                                 ok = true;
4735                                 break;
4736                         }
4737                 }
4738                 if (!ok)
4739                         return -EINVAL;
4740         }
4741
4742         if (!rdev->ops->auth)
4743                 return -EOPNOTSUPP;
4744
4745         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4746             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4747                 return -EOPNOTSUPP;
4748
4749         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4750         chan = ieee80211_get_channel(&rdev->wiphy,
4751                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4752         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4753                 return -EINVAL;
4754
4755         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4756         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4757
4758         if (info->attrs[NL80211_ATTR_IE]) {
4759                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4760                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4761         }
4762
4763         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4764         if (!nl80211_valid_auth_type(auth_type))
4765                 return -EINVAL;
4766
4767         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4768
4769         /*
4770          * Since we no longer track auth state, ignore
4771          * requests to only change local state.
4772          */
4773         if (local_state_change)
4774                 return 0;
4775
4776         return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4777                                   ssid, ssid_len, ie, ie_len,
4778                                   key.p.key, key.p.key_len, key.idx);
4779 }
4780
4781 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4782                                    struct genl_info *info,
4783                                    struct cfg80211_crypto_settings *settings,
4784                                    int cipher_limit)
4785 {
4786         memset(settings, 0, sizeof(*settings));
4787
4788         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4789
4790         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4791                 u16 proto;
4792                 proto = nla_get_u16(
4793                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4794                 settings->control_port_ethertype = cpu_to_be16(proto);
4795                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4796                     proto != ETH_P_PAE)
4797                         return -EINVAL;
4798                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4799                         settings->control_port_no_encrypt = true;
4800         } else
4801                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4802
4803         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4804                 void *data;
4805                 int len, i;
4806
4807                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4808                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4809                 settings->n_ciphers_pairwise = len / sizeof(u32);
4810
4811                 if (len % sizeof(u32))
4812                         return -EINVAL;
4813
4814                 if (settings->n_ciphers_pairwise > cipher_limit)
4815                         return -EINVAL;
4816
4817                 memcpy(settings->ciphers_pairwise, data, len);
4818
4819                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4820                         if (!cfg80211_supported_cipher_suite(
4821                                         &rdev->wiphy,
4822                                         settings->ciphers_pairwise[i]))
4823                                 return -EINVAL;
4824         }
4825
4826         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4827                 settings->cipher_group =
4828                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4829                 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4830                                                      settings->cipher_group))
4831                         return -EINVAL;
4832         }
4833
4834         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4835                 settings->wpa_versions =
4836                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4837                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4838                         return -EINVAL;
4839         }
4840
4841         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4842                 void *data;
4843                 int len;
4844
4845                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4846                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4847                 settings->n_akm_suites = len / sizeof(u32);
4848
4849                 if (len % sizeof(u32))
4850                         return -EINVAL;
4851
4852                 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4853                         return -EINVAL;
4854
4855                 memcpy(settings->akm_suites, data, len);
4856         }
4857
4858         return 0;
4859 }
4860
4861 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4862 {
4863         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4864         struct net_device *dev = info->user_ptr[1];
4865         struct cfg80211_crypto_settings crypto;
4866         struct ieee80211_channel *chan;
4867         const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
4868         int err, ssid_len, ie_len = 0;
4869         bool use_mfp = false;
4870         u32 flags = 0;
4871         struct ieee80211_ht_cap *ht_capa = NULL;
4872         struct ieee80211_ht_cap *ht_capa_mask = NULL;
4873
4874         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4875                 return -EINVAL;
4876
4877         if (!info->attrs[NL80211_ATTR_MAC] ||
4878             !info->attrs[NL80211_ATTR_SSID] ||
4879             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4880                 return -EINVAL;
4881
4882         if (!rdev->ops->assoc)
4883                 return -EOPNOTSUPP;
4884
4885         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4886             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4887                 return -EOPNOTSUPP;
4888
4889         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4890
4891         chan = ieee80211_get_channel(&rdev->wiphy,
4892                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4893         if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4894                 return -EINVAL;
4895
4896         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4897         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4898
4899         if (info->attrs[NL80211_ATTR_IE]) {
4900                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4901                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4902         }
4903
4904         if (info->attrs[NL80211_ATTR_USE_MFP]) {
4905                 enum nl80211_mfp mfp =
4906                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
4907                 if (mfp == NL80211_MFP_REQUIRED)
4908                         use_mfp = true;
4909                 else if (mfp != NL80211_MFP_NO)
4910                         return -EINVAL;
4911         }
4912
4913         if (info->attrs[NL80211_ATTR_PREV_BSSID])
4914                 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4915
4916         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
4917                 flags |= ASSOC_REQ_DISABLE_HT;
4918
4919         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
4920                 ht_capa_mask =
4921                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
4922
4923         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
4924                 if (!ht_capa_mask)
4925                         return -EINVAL;
4926                 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4927         }
4928
4929         err = nl80211_crypto_settings(rdev, info, &crypto, 1);
4930         if (!err)
4931                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4932                                           ssid, ssid_len, ie, ie_len, use_mfp,
4933                                           &crypto, flags, ht_capa,
4934                                           ht_capa_mask);
4935
4936         return err;
4937 }
4938
4939 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4940 {
4941         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4942         struct net_device *dev = info->user_ptr[1];
4943         const u8 *ie = NULL, *bssid;
4944         int ie_len = 0;
4945         u16 reason_code;
4946         bool local_state_change;
4947
4948         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4949                 return -EINVAL;
4950
4951         if (!info->attrs[NL80211_ATTR_MAC])
4952                 return -EINVAL;
4953
4954         if (!info->attrs[NL80211_ATTR_REASON_CODE])
4955                 return -EINVAL;
4956
4957         if (!rdev->ops->deauth)
4958                 return -EOPNOTSUPP;
4959
4960         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4961             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4962                 return -EOPNOTSUPP;
4963
4964         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4965
4966         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4967         if (reason_code == 0) {
4968                 /* Reason Code 0 is reserved */
4969                 return -EINVAL;
4970         }
4971
4972         if (info->attrs[NL80211_ATTR_IE]) {
4973                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4974                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4975         }
4976
4977         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4978
4979         return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4980                                     local_state_change);
4981 }
4982
4983 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4984 {
4985         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4986         struct net_device *dev = info->user_ptr[1];
4987         const u8 *ie = NULL, *bssid;
4988         int ie_len = 0;
4989         u16 reason_code;
4990         bool local_state_change;
4991
4992         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4993                 return -EINVAL;
4994
4995         if (!info->attrs[NL80211_ATTR_MAC])
4996                 return -EINVAL;
4997
4998         if (!info->attrs[NL80211_ATTR_REASON_CODE])
4999                 return -EINVAL;
5000
5001         if (!rdev->ops->disassoc)
5002                 return -EOPNOTSUPP;
5003
5004         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5005             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5006                 return -EOPNOTSUPP;
5007
5008         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5009
5010         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5011         if (reason_code == 0) {
5012                 /* Reason Code 0 is reserved */
5013                 return -EINVAL;
5014         }
5015
5016         if (info->attrs[NL80211_ATTR_IE]) {
5017                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5018                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5019         }
5020
5021         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5022
5023         return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5024                                       local_state_change);
5025 }
5026
5027 static bool
5028 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5029                          int mcast_rate[IEEE80211_NUM_BANDS],
5030                          int rateval)
5031 {
5032         struct wiphy *wiphy = &rdev->wiphy;
5033         bool found = false;
5034         int band, i;
5035
5036         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5037                 struct ieee80211_supported_band *sband;
5038
5039                 sband = wiphy->bands[band];
5040                 if (!sband)
5041                         continue;
5042
5043                 for (i = 0; i < sband->n_bitrates; i++) {
5044                         if (sband->bitrates[i].bitrate == rateval) {
5045                                 mcast_rate[band] = i + 1;
5046                                 found = true;
5047                                 break;
5048                         }
5049                 }
5050         }
5051
5052         return found;
5053 }
5054
5055 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5056 {
5057         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5058         struct net_device *dev = info->user_ptr[1];
5059         struct cfg80211_ibss_params ibss;
5060         struct wiphy *wiphy;
5061         struct cfg80211_cached_keys *connkeys = NULL;
5062         int err;
5063
5064         memset(&ibss, 0, sizeof(ibss));
5065
5066         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5067                 return -EINVAL;
5068
5069         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5070             !info->attrs[NL80211_ATTR_SSID] ||
5071             !nla_len(info->attrs[NL80211_ATTR_SSID]))
5072                 return -EINVAL;
5073
5074         ibss.beacon_interval = 100;
5075
5076         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5077                 ibss.beacon_interval =
5078                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5079                 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5080                         return -EINVAL;
5081         }
5082
5083         if (!rdev->ops->join_ibss)
5084                 return -EOPNOTSUPP;
5085
5086         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5087                 return -EOPNOTSUPP;
5088
5089         wiphy = &rdev->wiphy;
5090
5091         if (info->attrs[NL80211_ATTR_MAC]) {
5092                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5093
5094                 if (!is_valid_ether_addr(ibss.bssid))
5095                         return -EINVAL;
5096         }
5097         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5098         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5099
5100         if (info->attrs[NL80211_ATTR_IE]) {
5101                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5102                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5103         }
5104
5105         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5106                 enum nl80211_channel_type channel_type;
5107
5108                 if (!nl80211_valid_channel_type(info, &channel_type))
5109                         return -EINVAL;
5110
5111                 if (channel_type != NL80211_CHAN_NO_HT &&
5112                     !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5113                         return -EINVAL;
5114
5115                 ibss.channel_type = channel_type;
5116         } else {
5117                 ibss.channel_type = NL80211_CHAN_NO_HT;
5118         }
5119
5120         ibss.channel = rdev_freq_to_chan(rdev,
5121                 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5122                 ibss.channel_type);
5123         if (!ibss.channel ||
5124             ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
5125             ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5126                 return -EINVAL;
5127
5128         /* Both channels should be able to initiate communication */
5129         if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5130              ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5131             !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5132                                           ibss.channel_type))
5133                 return -EINVAL;
5134
5135         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
5136         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5137
5138         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5139                 u8 *rates =
5140                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5141                 int n_rates =
5142                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5143                 struct ieee80211_supported_band *sband =
5144                         wiphy->bands[ibss.channel->band];
5145
5146                 err = ieee80211_get_ratemask(sband, rates, n_rates,
5147                                              &ibss.basic_rates);
5148                 if (err)
5149                         return err;
5150         }
5151
5152         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5153             !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5154                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5155                 return -EINVAL;
5156
5157         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5158                 connkeys = nl80211_parse_connkeys(rdev,
5159                                         info->attrs[NL80211_ATTR_KEYS]);
5160                 if (IS_ERR(connkeys))
5161                         return PTR_ERR(connkeys);
5162         }
5163
5164         ibss.control_port =
5165                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5166
5167         err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
5168         if (err)
5169                 kfree(connkeys);
5170         return err;
5171 }
5172
5173 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5174 {
5175         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5176         struct net_device *dev = info->user_ptr[1];
5177
5178         if (!rdev->ops->leave_ibss)
5179                 return -EOPNOTSUPP;
5180
5181         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5182                 return -EOPNOTSUPP;
5183
5184         return cfg80211_leave_ibss(rdev, dev, false);
5185 }
5186
5187 #ifdef CONFIG_NL80211_TESTMODE
5188 static struct genl_multicast_group nl80211_testmode_mcgrp = {
5189         .name = "testmode",
5190 };
5191
5192 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5193 {
5194         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5195         int err;
5196
5197         if (!info->attrs[NL80211_ATTR_TESTDATA])
5198                 return -EINVAL;
5199
5200         err = -EOPNOTSUPP;
5201         if (rdev->ops->testmode_cmd) {
5202                 rdev->testmode_info = info;
5203                 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5204                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5205                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5206                 rdev->testmode_info = NULL;
5207         }
5208
5209         return err;
5210 }
5211
5212 static int nl80211_testmode_dump(struct sk_buff *skb,
5213                                  struct netlink_callback *cb)
5214 {
5215         struct cfg80211_registered_device *rdev;
5216         int err;
5217         long phy_idx;
5218         void *data = NULL;
5219         int data_len = 0;
5220
5221         if (cb->args[0]) {
5222                 /*
5223                  * 0 is a valid index, but not valid for args[0],
5224                  * so we need to offset by 1.
5225                  */
5226                 phy_idx = cb->args[0] - 1;
5227         } else {
5228                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5229                                   nl80211_fam.attrbuf, nl80211_fam.maxattr,
5230                                   nl80211_policy);
5231                 if (err)
5232                         return err;
5233
5234                 mutex_lock(&cfg80211_mutex);
5235                 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5236                                                   nl80211_fam.attrbuf);
5237                 if (IS_ERR(rdev)) {
5238                         mutex_unlock(&cfg80211_mutex);
5239                         return PTR_ERR(rdev);
5240                 }
5241                 phy_idx = rdev->wiphy_idx;
5242                 rdev = NULL;
5243                 mutex_unlock(&cfg80211_mutex);
5244
5245                 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5246                         cb->args[1] =
5247                                 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5248         }
5249
5250         if (cb->args[1]) {
5251                 data = nla_data((void *)cb->args[1]);
5252                 data_len = nla_len((void *)cb->args[1]);
5253         }
5254
5255         mutex_lock(&cfg80211_mutex);
5256         rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5257         if (!rdev) {
5258                 mutex_unlock(&cfg80211_mutex);
5259                 return -ENOENT;
5260         }
5261         cfg80211_lock_rdev(rdev);
5262         mutex_unlock(&cfg80211_mutex);
5263
5264         if (!rdev->ops->testmode_dump) {
5265                 err = -EOPNOTSUPP;
5266                 goto out_err;
5267         }
5268
5269         while (1) {
5270                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5271                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
5272                                            NL80211_CMD_TESTMODE);
5273                 struct nlattr *tmdata;
5274
5275                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
5276                         genlmsg_cancel(skb, hdr);
5277                         break;
5278                 }
5279
5280                 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5281                 if (!tmdata) {
5282                         genlmsg_cancel(skb, hdr);
5283                         break;
5284                 }
5285                 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5286                                                data, data_len);
5287                 nla_nest_end(skb, tmdata);
5288
5289                 if (err == -ENOBUFS || err == -ENOENT) {
5290                         genlmsg_cancel(skb, hdr);
5291                         break;
5292                 } else if (err) {
5293                         genlmsg_cancel(skb, hdr);
5294                         goto out_err;
5295                 }
5296
5297                 genlmsg_end(skb, hdr);
5298         }
5299
5300         err = skb->len;
5301         /* see above */
5302         cb->args[0] = phy_idx + 1;
5303  out_err:
5304         cfg80211_unlock_rdev(rdev);
5305         return err;
5306 }
5307
5308 static struct sk_buff *
5309 __cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5310                               int approxlen, u32 pid, u32 seq, gfp_t gfp)
5311 {
5312         struct sk_buff *skb;
5313         void *hdr;
5314         struct nlattr *data;
5315
5316         skb = nlmsg_new(approxlen + 100, gfp);
5317         if (!skb)
5318                 return NULL;
5319
5320         hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5321         if (!hdr) {
5322                 kfree_skb(skb);
5323                 return NULL;
5324         }
5325
5326         if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5327                 goto nla_put_failure;
5328         data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5329
5330         ((void **)skb->cb)[0] = rdev;
5331         ((void **)skb->cb)[1] = hdr;
5332         ((void **)skb->cb)[2] = data;
5333
5334         return skb;
5335
5336  nla_put_failure:
5337         kfree_skb(skb);
5338         return NULL;
5339 }
5340
5341 struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5342                                                   int approxlen)
5343 {
5344         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5345
5346         if (WARN_ON(!rdev->testmode_info))
5347                 return NULL;
5348
5349         return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5350                                 rdev->testmode_info->snd_pid,
5351                                 rdev->testmode_info->snd_seq,
5352                                 GFP_KERNEL);
5353 }
5354 EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5355
5356 int cfg80211_testmode_reply(struct sk_buff *skb)
5357 {
5358         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5359         void *hdr = ((void **)skb->cb)[1];
5360         struct nlattr *data = ((void **)skb->cb)[2];
5361
5362         if (WARN_ON(!rdev->testmode_info)) {
5363                 kfree_skb(skb);
5364                 return -EINVAL;
5365         }
5366
5367         nla_nest_end(skb, data);
5368         genlmsg_end(skb, hdr);
5369         return genlmsg_reply(skb, rdev->testmode_info);
5370 }
5371 EXPORT_SYMBOL(cfg80211_testmode_reply);
5372
5373 struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5374                                                   int approxlen, gfp_t gfp)
5375 {
5376         struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5377
5378         return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5379 }
5380 EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5381
5382 void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5383 {
5384         void *hdr = ((void **)skb->cb)[1];
5385         struct nlattr *data = ((void **)skb->cb)[2];
5386
5387         nla_nest_end(skb, data);
5388         genlmsg_end(skb, hdr);
5389         genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5390 }
5391 EXPORT_SYMBOL(cfg80211_testmode_event);
5392 #endif
5393
5394 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5395 {
5396         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5397         struct net_device *dev = info->user_ptr[1];
5398         struct cfg80211_connect_params connect;
5399         struct wiphy *wiphy;
5400         struct cfg80211_cached_keys *connkeys = NULL;
5401         int err;
5402
5403         memset(&connect, 0, sizeof(connect));
5404
5405         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5406                 return -EINVAL;
5407
5408         if (!info->attrs[NL80211_ATTR_SSID] ||
5409             !nla_len(info->attrs[NL80211_ATTR_SSID]))
5410                 return -EINVAL;
5411
5412         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5413                 connect.auth_type =
5414                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5415                 if (!nl80211_valid_auth_type(connect.auth_type))
5416                         return -EINVAL;
5417         } else
5418                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5419
5420         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5421
5422         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
5423                                       NL80211_MAX_NR_CIPHER_SUITES);
5424         if (err)
5425                 return err;
5426
5427         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5428             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5429                 return -EOPNOTSUPP;
5430
5431         wiphy = &rdev->wiphy;
5432
5433         connect.bg_scan_period = -1;
5434         if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5435                 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5436                 connect.bg_scan_period =
5437                         nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5438         }
5439
5440         if (info->attrs[NL80211_ATTR_MAC])
5441                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5442         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5443         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5444
5445         if (info->attrs[NL80211_ATTR_IE]) {
5446                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5447                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5448         }
5449
5450         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5451                 connect.channel =
5452                         ieee80211_get_channel(wiphy,
5453                             nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5454                 if (!connect.channel ||
5455                     connect.channel->flags & IEEE80211_CHAN_DISABLED)
5456                         return -EINVAL;
5457         }
5458
5459         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5460                 connkeys = nl80211_parse_connkeys(rdev,
5461                                         info->attrs[NL80211_ATTR_KEYS]);
5462                 if (IS_ERR(connkeys))
5463                         return PTR_ERR(connkeys);
5464         }
5465
5466         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5467                 connect.flags |= ASSOC_REQ_DISABLE_HT;
5468
5469         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5470                 memcpy(&connect.ht_capa_mask,
5471                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5472                        sizeof(connect.ht_capa_mask));
5473
5474         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5475                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5476                         return -EINVAL;
5477                 memcpy(&connect.ht_capa,
5478                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5479                        sizeof(connect.ht_capa));
5480         }
5481
5482         err = cfg80211_connect(rdev, dev, &connect, connkeys);
5483         if (err)
5484                 kfree(connkeys);
5485         return err;
5486 }
5487
5488 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5489 {
5490         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5491         struct net_device *dev = info->user_ptr[1];
5492         u16 reason;
5493
5494         if (!info->attrs[NL80211_ATTR_REASON_CODE])
5495                 reason = WLAN_REASON_DEAUTH_LEAVING;
5496         else
5497                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5498
5499         if (reason == 0)
5500                 return -EINVAL;
5501
5502         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5503             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5504                 return -EOPNOTSUPP;
5505
5506         return cfg80211_disconnect(rdev, dev, reason, true);
5507 }
5508
5509 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5510 {
5511         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5512         struct net *net;
5513         int err;
5514         u32 pid;
5515
5516         if (!info->attrs[NL80211_ATTR_PID])
5517                 return -EINVAL;
5518
5519         pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5520
5521         net = get_net_ns_by_pid(pid);
5522         if (IS_ERR(net))
5523                 return PTR_ERR(net);
5524
5525         err = 0;
5526
5527         /* check if anything to do */
5528         if (!net_eq(wiphy_net(&rdev->wiphy), net))
5529                 err = cfg80211_switch_netns(rdev, net);
5530
5531         put_net(net);
5532         return err;
5533 }
5534
5535 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5536 {
5537         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5538         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5539                         struct cfg80211_pmksa *pmksa) = NULL;
5540         struct net_device *dev = info->user_ptr[1];
5541         struct cfg80211_pmksa pmksa;
5542
5543         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5544
5545         if (!info->attrs[NL80211_ATTR_MAC])
5546                 return -EINVAL;
5547
5548         if (!info->attrs[NL80211_ATTR_PMKID])
5549                 return -EINVAL;
5550
5551         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5552         pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5553
5554         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5555             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5556                 return -EOPNOTSUPP;
5557
5558         switch (info->genlhdr->cmd) {
5559         case NL80211_CMD_SET_PMKSA:
5560                 rdev_ops = rdev->ops->set_pmksa;
5561                 break;
5562         case NL80211_CMD_DEL_PMKSA:
5563                 rdev_ops = rdev->ops->del_pmksa;
5564                 break;
5565         default:
5566                 WARN_ON(1);
5567                 break;
5568         }
5569
5570         if (!rdev_ops)
5571                 return -EOPNOTSUPP;
5572
5573         return rdev_ops(&rdev->wiphy, dev, &pmksa);
5574 }
5575
5576 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5577 {
5578         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5579         struct net_device *dev = info->user_ptr[1];
5580
5581         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5582             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5583                 return -EOPNOTSUPP;
5584
5585         if (!rdev->ops->flush_pmksa)
5586                 return -EOPNOTSUPP;
5587
5588         return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
5589 }
5590
5591 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5592 {
5593         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5594         struct net_device *dev = info->user_ptr[1];
5595         u8 action_code, dialog_token;
5596         u16 status_code;
5597         u8 *peer;
5598
5599         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5600             !rdev->ops->tdls_mgmt)
5601                 return -EOPNOTSUPP;
5602
5603         if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5604             !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5605             !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5606             !info->attrs[NL80211_ATTR_IE] ||
5607             !info->attrs[NL80211_ATTR_MAC])
5608                 return -EINVAL;
5609
5610         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5611         action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5612         status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5613         dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5614
5615         return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5616                                     dialog_token, status_code,
5617                                     nla_data(info->attrs[NL80211_ATTR_IE]),
5618                                     nla_len(info->attrs[NL80211_ATTR_IE]));
5619 }
5620
5621 static int nl80211_tdls_oper(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         enum nl80211_tdls_operation operation;
5626         u8 *peer;
5627
5628         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5629             !rdev->ops->tdls_oper)
5630                 return -EOPNOTSUPP;
5631
5632         if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5633             !info->attrs[NL80211_ATTR_MAC])
5634                 return -EINVAL;
5635
5636         operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5637         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5638
5639         return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5640 }
5641
5642 static int nl80211_remain_on_channel(struct sk_buff *skb,
5643                                      struct genl_info *info)
5644 {
5645         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5646         struct net_device *dev = info->user_ptr[1];
5647         struct ieee80211_channel *chan;
5648         struct sk_buff *msg;
5649         void *hdr;
5650         u64 cookie;
5651         enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5652         u32 freq, duration;
5653         int err;
5654
5655         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5656             !info->attrs[NL80211_ATTR_DURATION])
5657                 return -EINVAL;
5658
5659         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5660
5661         if (!rdev->ops->remain_on_channel ||
5662             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
5663                 return -EOPNOTSUPP;
5664
5665         /*
5666          * We should be on that channel for at least a minimum amount of
5667          * time (10ms) but no longer than the driver supports.
5668          */
5669         if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5670             duration > rdev->wiphy.max_remain_on_channel_duration)
5671                 return -EINVAL;
5672
5673         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5674             !nl80211_valid_channel_type(info, &channel_type))
5675                 return -EINVAL;
5676
5677         freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5678         chan = rdev_freq_to_chan(rdev, freq, channel_type);
5679         if (chan == NULL)
5680                 return -EINVAL;
5681
5682         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5683         if (!msg)
5684                 return -ENOMEM;
5685
5686         hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5687                              NL80211_CMD_REMAIN_ON_CHANNEL);
5688
5689         if (IS_ERR(hdr)) {
5690                 err = PTR_ERR(hdr);
5691                 goto free_msg;
5692         }
5693
5694         err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5695                                            channel_type, duration, &cookie);
5696
5697         if (err)
5698                 goto free_msg;
5699
5700         if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5701                 goto nla_put_failure;
5702
5703         genlmsg_end(msg, hdr);
5704
5705         return genlmsg_reply(msg, info);
5706
5707  nla_put_failure:
5708         err = -ENOBUFS;
5709  free_msg:
5710         nlmsg_free(msg);
5711         return err;
5712 }
5713
5714 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5715                                             struct genl_info *info)
5716 {
5717         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5718         struct net_device *dev = info->user_ptr[1];
5719         u64 cookie;
5720
5721         if (!info->attrs[NL80211_ATTR_COOKIE])
5722                 return -EINVAL;
5723
5724         if (!rdev->ops->cancel_remain_on_channel)
5725                 return -EOPNOTSUPP;
5726
5727         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5728
5729         return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
5730 }
5731
5732 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5733                            u8 *rates, u8 rates_len)
5734 {
5735         u8 i;
5736         u32 mask = 0;
5737
5738         for (i = 0; i < rates_len; i++) {
5739                 int rate = (rates[i] & 0x7f) * 5;
5740                 int ridx;
5741                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5742                         struct ieee80211_rate *srate =
5743                                 &sband->bitrates[ridx];
5744                         if (rate == srate->bitrate) {
5745                                 mask |= 1 << ridx;
5746                                 break;
5747                         }
5748                 }
5749                 if (ridx == sband->n_bitrates)
5750                         return 0; /* rate not found */
5751         }
5752
5753         return mask;
5754 }
5755
5756 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5757                                u8 *rates, u8 rates_len,
5758                                u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5759 {
5760         u8 i;
5761
5762         memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5763
5764         for (i = 0; i < rates_len; i++) {
5765                 int ridx, rbit;
5766
5767                 ridx = rates[i] / 8;
5768                 rbit = BIT(rates[i] % 8);
5769
5770                 /* check validity */
5771                 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
5772                         return false;
5773
5774                 /* check availability */
5775                 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5776                         mcs[ridx] |= rbit;
5777                 else
5778                         return false;
5779         }
5780
5781         return true;
5782 }
5783
5784 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
5785         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5786                                     .len = NL80211_MAX_SUPP_RATES },
5787         [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5788                                  .len = NL80211_MAX_SUPP_HT_RATES },
5789 };
5790
5791 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5792                                        struct genl_info *info)
5793 {
5794         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5795         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5796         struct cfg80211_bitrate_mask mask;
5797         int rem, i;
5798         struct net_device *dev = info->user_ptr[1];
5799         struct nlattr *tx_rates;
5800         struct ieee80211_supported_band *sband;
5801
5802         if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5803                 return -EINVAL;
5804
5805         if (!rdev->ops->set_bitrate_mask)
5806                 return -EOPNOTSUPP;
5807
5808         memset(&mask, 0, sizeof(mask));
5809         /* Default to all rates enabled */
5810         for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5811                 sband = rdev->wiphy.bands[i];
5812                 mask.control[i].legacy =
5813                         sband ? (1 << sband->n_bitrates) - 1 : 0;
5814                 if (sband)
5815                         memcpy(mask.control[i].mcs,
5816                                sband->ht_cap.mcs.rx_mask,
5817                                sizeof(mask.control[i].mcs));
5818                 else
5819                         memset(mask.control[i].mcs, 0,
5820                                sizeof(mask.control[i].mcs));
5821         }
5822
5823         /*
5824          * The nested attribute uses enum nl80211_band as the index. This maps
5825          * directly to the enum ieee80211_band values used in cfg80211.
5826          */
5827         BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5828         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5829         {
5830                 enum ieee80211_band band = nla_type(tx_rates);
5831                 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5832                         return -EINVAL;
5833                 sband = rdev->wiphy.bands[band];
5834                 if (sband == NULL)
5835                         return -EINVAL;
5836                 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5837                           nla_len(tx_rates), nl80211_txattr_policy);
5838                 if (tb[NL80211_TXRATE_LEGACY]) {
5839                         mask.control[band].legacy = rateset_to_mask(
5840                                 sband,
5841                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
5842                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
5843                         if ((mask.control[band].legacy == 0) &&
5844                             nla_len(tb[NL80211_TXRATE_LEGACY]))
5845                                 return -EINVAL;
5846                 }
5847                 if (tb[NL80211_TXRATE_MCS]) {
5848                         if (!ht_rateset_to_mask(
5849                                         sband,
5850                                         nla_data(tb[NL80211_TXRATE_MCS]),
5851                                         nla_len(tb[NL80211_TXRATE_MCS]),
5852                                         mask.control[band].mcs))