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