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