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