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