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