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