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