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