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