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