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