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