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