Merge branch 'arch/nohz' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[platform/kernel/linux-rpi.git] / net / wireless / nl80211.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the new netlink-based wireless configuration interface.
4  *
5  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
6  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  * Copyright 2015-2017  Intel Deutschland GmbH
8  * Copyright (C) 2018-2019 Intel Corporation
9  */
10
11 #include <linux/if.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/list.h>
16 #include <linux/if_ether.h>
17 #include <linux/ieee80211.h>
18 #include <linux/nl80211.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/netlink.h>
21 #include <linux/nospec.h>
22 #include <linux/etherdevice.h>
23 #include <net/net_namespace.h>
24 #include <net/genetlink.h>
25 #include <net/cfg80211.h>
26 #include <net/sock.h>
27 #include <net/inet_connection_sock.h>
28 #include "core.h"
29 #include "nl80211.h"
30 #include "reg.h"
31 #include "rdev-ops.h"
32
33 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
34                                    struct genl_info *info,
35                                    struct cfg80211_crypto_settings *settings,
36                                    int cipher_limit);
37
38 /* the netlink family */
39 static struct genl_family nl80211_fam;
40
41 /* multicast groups */
42 enum nl80211_multicast_groups {
43         NL80211_MCGRP_CONFIG,
44         NL80211_MCGRP_SCAN,
45         NL80211_MCGRP_REGULATORY,
46         NL80211_MCGRP_MLME,
47         NL80211_MCGRP_VENDOR,
48         NL80211_MCGRP_NAN,
49         NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
50 };
51
52 static const struct genl_multicast_group nl80211_mcgrps[] = {
53         [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
54         [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
55         [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
56         [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
57         [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
58         [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
59 #ifdef CONFIG_NL80211_TESTMODE
60         [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
61 #endif
62 };
63
64 /* returns ERR_PTR values */
65 static struct wireless_dev *
66 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
67 {
68         struct cfg80211_registered_device *rdev;
69         struct wireless_dev *result = NULL;
70         bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
71         bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
72         u64 wdev_id;
73         int wiphy_idx = -1;
74         int ifidx = -1;
75
76         ASSERT_RTNL();
77
78         if (!have_ifidx && !have_wdev_id)
79                 return ERR_PTR(-EINVAL);
80
81         if (have_ifidx)
82                 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
83         if (have_wdev_id) {
84                 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
85                 wiphy_idx = wdev_id >> 32;
86         }
87
88         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
89                 struct wireless_dev *wdev;
90
91                 if (wiphy_net(&rdev->wiphy) != netns)
92                         continue;
93
94                 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
95                         continue;
96
97                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
98                         if (have_ifidx && wdev->netdev &&
99                             wdev->netdev->ifindex == ifidx) {
100                                 result = wdev;
101                                 break;
102                         }
103                         if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
104                                 result = wdev;
105                                 break;
106                         }
107                 }
108
109                 if (result)
110                         break;
111         }
112
113         if (result)
114                 return result;
115         return ERR_PTR(-ENODEV);
116 }
117
118 static struct cfg80211_registered_device *
119 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
120 {
121         struct cfg80211_registered_device *rdev = NULL, *tmp;
122         struct net_device *netdev;
123
124         ASSERT_RTNL();
125
126         if (!attrs[NL80211_ATTR_WIPHY] &&
127             !attrs[NL80211_ATTR_IFINDEX] &&
128             !attrs[NL80211_ATTR_WDEV])
129                 return ERR_PTR(-EINVAL);
130
131         if (attrs[NL80211_ATTR_WIPHY])
132                 rdev = cfg80211_rdev_by_wiphy_idx(
133                                 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
134
135         if (attrs[NL80211_ATTR_WDEV]) {
136                 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
137                 struct wireless_dev *wdev;
138                 bool found = false;
139
140                 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
141                 if (tmp) {
142                         /* make sure wdev exists */
143                         list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
144                                 if (wdev->identifier != (u32)wdev_id)
145                                         continue;
146                                 found = true;
147                                 break;
148                         }
149
150                         if (!found)
151                                 tmp = NULL;
152
153                         if (rdev && tmp != rdev)
154                                 return ERR_PTR(-EINVAL);
155                         rdev = tmp;
156                 }
157         }
158
159         if (attrs[NL80211_ATTR_IFINDEX]) {
160                 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
161
162                 netdev = __dev_get_by_index(netns, ifindex);
163                 if (netdev) {
164                         if (netdev->ieee80211_ptr)
165                                 tmp = wiphy_to_rdev(
166                                         netdev->ieee80211_ptr->wiphy);
167                         else
168                                 tmp = NULL;
169
170                         /* not wireless device -- return error */
171                         if (!tmp)
172                                 return ERR_PTR(-EINVAL);
173
174                         /* mismatch -- return error */
175                         if (rdev && tmp != rdev)
176                                 return ERR_PTR(-EINVAL);
177
178                         rdev = tmp;
179                 }
180         }
181
182         if (!rdev)
183                 return ERR_PTR(-ENODEV);
184
185         if (netns != wiphy_net(&rdev->wiphy))
186                 return ERR_PTR(-ENODEV);
187
188         return rdev;
189 }
190
191 /*
192  * This function returns a pointer to the driver
193  * that the genl_info item that is passed refers to.
194  *
195  * The result of this can be a PTR_ERR and hence must
196  * be checked with IS_ERR() for errors.
197  */
198 static struct cfg80211_registered_device *
199 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
200 {
201         return __cfg80211_rdev_from_attrs(netns, info->attrs);
202 }
203
204 static int validate_beacon_head(const struct nlattr *attr,
205                                 struct netlink_ext_ack *extack)
206 {
207         const u8 *data = nla_data(attr);
208         unsigned int len = nla_len(attr);
209         const struct element *elem;
210         const struct ieee80211_mgmt *mgmt = (void *)data;
211         unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
212                                          u.beacon.variable);
213
214         if (len < fixedlen)
215                 goto err;
216
217         if (ieee80211_hdrlen(mgmt->frame_control) !=
218             offsetof(struct ieee80211_mgmt, u.beacon))
219                 goto err;
220
221         data += fixedlen;
222         len -= fixedlen;
223
224         for_each_element(elem, data, len) {
225                 /* nothing */
226         }
227
228         if (for_each_element_completed(elem, data, len))
229                 return 0;
230
231 err:
232         NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
233         return -EINVAL;
234 }
235
236 static int validate_ie_attr(const struct nlattr *attr,
237                             struct netlink_ext_ack *extack)
238 {
239         const u8 *data = nla_data(attr);
240         unsigned int len = nla_len(attr);
241         const struct element *elem;
242
243         for_each_element(elem, data, len) {
244                 /* nothing */
245         }
246
247         if (for_each_element_completed(elem, data, len))
248                 return 0;
249
250         NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
251         return -EINVAL;
252 }
253
254 /* policy for the attributes */
255 static const struct nla_policy
256 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
257         [NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
258         [NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
259                                         .len = U8_MAX },
260         [NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
261                                              .len = U8_MAX },
262 };
263
264 static const struct nla_policy
265 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
266         [NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
267         [NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
268         [NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
269                 NLA_POLICY_MAX(NLA_U8, 15),
270         [NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
271         [NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
272                 NLA_POLICY_MAX(NLA_U8, 15),
273         [NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
274                 NLA_POLICY_MAX(NLA_U8, 31),
275         [NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
276         [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
277         [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
278 };
279
280 static const struct nla_policy
281 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
282         [NL80211_PMSR_TYPE_FTM] =
283                 NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
284 };
285
286 static const struct nla_policy
287 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
288         [NL80211_PMSR_REQ_ATTR_DATA] =
289                 NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
290         [NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
291 };
292
293 static const struct nla_policy
294 nl80211_psmr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
295         [NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
296         /*
297          * we could specify this again to be the top-level policy,
298          * but that would open us up to recursion problems ...
299          */
300         [NL80211_PMSR_PEER_ATTR_CHAN] = { .type = NLA_NESTED },
301         [NL80211_PMSR_PEER_ATTR_REQ] =
302                 NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
303         [NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
304 };
305
306 static const struct nla_policy
307 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
308         [NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
309         [NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
310         [NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
311         [NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
312         [NL80211_PMSR_ATTR_PEERS] =
313                 NLA_POLICY_NESTED_ARRAY(nl80211_psmr_peer_attr_policy),
314 };
315
316 static const struct nla_policy
317 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
318         [NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
319                 NLA_POLICY_RANGE(NLA_U8, 1, 20),
320         [NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
321                 NLA_POLICY_RANGE(NLA_U8, 1, 20),
322 };
323
324 const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
325         [0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
326         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
327         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
328                                       .len = 20-1 },
329         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
330
331         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
332         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
333         [NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
334                                                 NL80211_EDMG_CHANNELS_MIN,
335                                                 NL80211_EDMG_CHANNELS_MAX),
336         [NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
337                                                 NL80211_EDMG_BW_CONFIG_MIN,
338                                                 NL80211_EDMG_BW_CONFIG_MAX),
339
340         [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
341         [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
342         [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
343
344         [NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
345         [NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
346         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
347         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
348         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
349         [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
350
351         [NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
352         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
353         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
354
355         [NL80211_ATTR_MAC] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
356         [NL80211_ATTR_PREV_BSSID] = {
357                 .type = NLA_EXACT_LEN_WARN,
358                 .len = ETH_ALEN
359         },
360
361         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
362         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
363                                     .len = WLAN_MAX_KEY_LEN },
364         [NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 5),
365         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
366         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
367         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
368         [NL80211_ATTR_KEY_TYPE] =
369                 NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
370
371         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
372         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
373         [NL80211_ATTR_BEACON_HEAD] =
374                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
375                                        IEEE80211_MAX_DATA_LEN),
376         [NL80211_ATTR_BEACON_TAIL] =
377                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
378                                        IEEE80211_MAX_DATA_LEN),
379         [NL80211_ATTR_STA_AID] =
380                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
381         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
382         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
383         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
384                                                .len = NL80211_MAX_SUPP_RATES },
385         [NL80211_ATTR_STA_PLINK_ACTION] =
386                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
387         [NL80211_ATTR_STA_TX_POWER_SETTING] =
388                 NLA_POLICY_RANGE(NLA_U8,
389                                  NL80211_TX_POWER_AUTOMATIC,
390                                  NL80211_TX_POWER_FIXED),
391         [NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
392         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
393         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
394         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
395                                    .len = IEEE80211_MAX_MESH_ID_LEN },
396         [NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
397
398         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
399         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
400
401         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
402         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
403         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
404         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
405                                            .len = NL80211_MAX_SUPP_RATES },
406         [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
407
408         [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
409         [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
410
411         [NL80211_ATTR_HT_CAPABILITY] = {
412                 .type = NLA_EXACT_LEN_WARN,
413                 .len = NL80211_HT_CAPABILITY_LEN
414         },
415
416         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
417         [NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
418                                                    validate_ie_attr,
419                                                    IEEE80211_MAX_DATA_LEN),
420         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
421         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
422
423         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
424                                 .len = IEEE80211_MAX_SSID_LEN },
425         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
426         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
427         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
428         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
429         [NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
430                                                   NL80211_MFP_NO,
431                                                   NL80211_MFP_OPTIONAL),
432         [NL80211_ATTR_STA_FLAGS2] = {
433                 .len = sizeof(struct nl80211_sta_flag_update),
434         },
435         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
436         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
437         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
438         [NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
439         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
440         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
441         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
442         [NL80211_ATTR_PID] = { .type = NLA_U32 },
443         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
444         [NL80211_ATTR_PMKID] = {
445                 .type = NLA_EXACT_LEN_WARN,
446                 .len = WLAN_PMKID_LEN
447         },
448         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
449         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
450         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
451         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
452                                  .len = IEEE80211_MAX_DATA_LEN },
453         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
454         [NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
455                                                    NL80211_PS_DISABLED,
456                                                    NL80211_PS_ENABLED),
457         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
458         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
459         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
460         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
461         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
462         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
463         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
464         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
465         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
466         [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
467         [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
468         [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
469         [NL80211_ATTR_STA_PLINK_STATE] =
470                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
471         [NL80211_ATTR_MESH_PEER_AID] =
472                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
473         [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
474         [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
475         [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
476         [NL80211_ATTR_HIDDEN_SSID] =
477                 NLA_POLICY_RANGE(NLA_U32,
478                                  NL80211_HIDDEN_SSID_NOT_IN_USE,
479                                  NL80211_HIDDEN_SSID_ZERO_CONTENTS),
480         [NL80211_ATTR_IE_PROBE_RESP] =
481                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
482                                        IEEE80211_MAX_DATA_LEN),
483         [NL80211_ATTR_IE_ASSOC_RESP] =
484                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
485                                        IEEE80211_MAX_DATA_LEN),
486         [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
487         [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
488         [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
489         [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
490         [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
491         [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
492         [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
493         [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
494         [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
495         [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
496         [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
497                                       .len = IEEE80211_MAX_DATA_LEN },
498         [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
499         [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
500         [NL80211_ATTR_HT_CAPABILITY_MASK] = {
501                 .len = NL80211_HT_CAPABILITY_LEN
502         },
503         [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
504         [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
505         [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
506         [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
507         [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
508         [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
509         [NL80211_ATTR_VHT_CAPABILITY] = {
510                 .type = NLA_EXACT_LEN_WARN,
511                 .len = NL80211_VHT_CAPABILITY_LEN
512         },
513         [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
514         [NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
515         [NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
516         [NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
517                 NLA_POLICY_RANGE(NLA_U32,
518                                  NL80211_MESH_POWER_UNKNOWN + 1,
519                                  NL80211_MESH_POWER_MAX),
520         [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
521         [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
522         [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
523         [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
524         [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
525         [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
526         [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
527                 .len = NL80211_VHT_CAPABILITY_LEN,
528         },
529         [NL80211_ATTR_MDID] = { .type = NLA_U16 },
530         [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
531                                   .len = IEEE80211_MAX_DATA_LEN },
532         [NL80211_ATTR_PEER_AID] =
533                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
534         [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
535         [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
536         [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
537         [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
538         [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
539         [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
540         [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
541         [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
542         [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
543         [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
544         [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
545         [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
546         [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
547                                    .len = IEEE80211_QOS_MAP_LEN_MAX },
548         [NL80211_ATTR_MAC_HINT] = {
549                 .type = NLA_EXACT_LEN_WARN,
550                 .len = ETH_ALEN
551         },
552         [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
553         [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
554         [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
555         [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
556         [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
557         [NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
558         [NL80211_ATTR_USER_PRIO] =
559                 NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
560         [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
561         [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
562         [NL80211_ATTR_MAC_MASK] = {
563                 .type = NLA_EXACT_LEN_WARN,
564                 .len = ETH_ALEN
565         },
566         [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
567         [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
568         [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
569         [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
570         [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
571         [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
572         [NL80211_ATTR_STA_SUPPORT_P2P_PS] =
573                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
574         [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
575                 .len = VHT_MUMIMO_GROUPS_DATA_LEN
576         },
577         [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = {
578                 .type = NLA_EXACT_LEN_WARN,
579                 .len = ETH_ALEN
580         },
581         [NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
582         [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
583         [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
584         [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
585                                     .len = FILS_MAX_KEK_LEN },
586         [NL80211_ATTR_FILS_NONCES] = {
587                 .type = NLA_EXACT_LEN_WARN,
588                 .len = 2 * FILS_NONCE_LEN
589         },
590         [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
591         [NL80211_ATTR_BSSID] = { .type = NLA_EXACT_LEN_WARN, .len = ETH_ALEN },
592         [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
593         [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
594                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
595         },
596         [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
597         [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
598                                              .len = FILS_ERP_MAX_USERNAME_LEN },
599         [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
600                                           .len = FILS_ERP_MAX_REALM_LEN },
601         [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
602         [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
603                                         .len = FILS_ERP_MAX_RRK_LEN },
604         [NL80211_ATTR_FILS_CACHE_ID] = { .type = NLA_EXACT_LEN_WARN, .len = 2 },
605         [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
606         [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
607         [NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
608
609         [NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
610         [NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
611         [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
612         [NL80211_ATTR_HE_CAPABILITY] = { .type = NLA_BINARY,
613                                          .len = NL80211_HE_MAX_CAPABILITY_LEN },
614
615         [NL80211_ATTR_FTM_RESPONDER] = {
616                 .type = NLA_NESTED,
617                 .validation_data = nl80211_ftm_responder_policy,
618         },
619         [NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
620         [NL80211_ATTR_PEER_MEASUREMENTS] =
621                 NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
622         [NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
623         [NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
624                                         .len = SAE_PASSWORD_MAX_LEN },
625         [NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
626         [NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
627         [NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
628 };
629
630 /* policy for the key attributes */
631 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
632         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
633         [NL80211_KEY_IDX] = { .type = NLA_U8 },
634         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
635         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
636         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
637         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
638         [NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
639         [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
640         [NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
641 };
642
643 /* policy for the key default flags */
644 static const struct nla_policy
645 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
646         [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
647         [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
648 };
649
650 #ifdef CONFIG_PM
651 /* policy for WoWLAN attributes */
652 static const struct nla_policy
653 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
654         [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
655         [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
656         [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
657         [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
658         [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
659         [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
660         [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
661         [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
662         [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
663         [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
664 };
665
666 static const struct nla_policy
667 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
668         [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
669         [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
670         [NL80211_WOWLAN_TCP_DST_MAC] = {
671                 .type = NLA_EXACT_LEN_WARN,
672                 .len = ETH_ALEN
673         },
674         [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
675         [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
676         [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
677         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
678                 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
679         },
680         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
681                 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
682         },
683         [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
684         [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .type = NLA_MIN_LEN, .len = 1 },
685         [NL80211_WOWLAN_TCP_WAKE_MASK] = { .type = NLA_MIN_LEN, .len = 1 },
686 };
687 #endif /* CONFIG_PM */
688
689 /* policy for coalesce rule attributes */
690 static const struct nla_policy
691 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
692         [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
693         [NL80211_ATTR_COALESCE_RULE_CONDITION] =
694                 NLA_POLICY_RANGE(NLA_U32,
695                                  NL80211_COALESCE_CONDITION_MATCH,
696                                  NL80211_COALESCE_CONDITION_NO_MATCH),
697         [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
698 };
699
700 /* policy for GTK rekey offload attributes */
701 static const struct nla_policy
702 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
703         [NL80211_REKEY_DATA_KEK] = {
704                 .type = NLA_EXACT_LEN_WARN,
705                 .len = NL80211_KEK_LEN,
706         },
707         [NL80211_REKEY_DATA_KCK] = {
708                 .type = NLA_EXACT_LEN_WARN,
709                 .len = NL80211_KCK_LEN,
710         },
711         [NL80211_REKEY_DATA_REPLAY_CTR] = {
712                 .type = NLA_EXACT_LEN_WARN,
713                 .len = NL80211_REPLAY_CTR_LEN
714         },
715 };
716
717 static const struct nla_policy
718 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
719         [NL80211_BAND_2GHZ] = { .type = NLA_S32 },
720         [NL80211_BAND_5GHZ] = { .type = NLA_S32 },
721         [NL80211_BAND_6GHZ] = { .type = NLA_S32 },
722         [NL80211_BAND_60GHZ] = { .type = NLA_S32 },
723 };
724
725 static const struct nla_policy
726 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
727         [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
728                                                  .len = IEEE80211_MAX_SSID_LEN },
729         [NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = {
730                 .type = NLA_EXACT_LEN_WARN,
731                 .len = ETH_ALEN
732         },
733         [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
734         [NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
735                 NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
736 };
737
738 static const struct nla_policy
739 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
740         [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
741         [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
742 };
743
744 static const struct nla_policy
745 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
746         [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
747         [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
748         [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
749                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
750         },
751 };
752
753 /* policy for NAN function attributes */
754 static const struct nla_policy
755 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
756         [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
757         [NL80211_NAN_FUNC_SERVICE_ID] = {
758                                     .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
759         [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
760         [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
761         [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
762         [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
763         [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
764         [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = {
765                 .type = NLA_EXACT_LEN_WARN,
766                 .len = ETH_ALEN
767         },
768         [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
769         [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
770         [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
771                         .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
772         [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
773         [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
774         [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
775         [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
776         [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
777 };
778
779 /* policy for Service Response Filter attributes */
780 static const struct nla_policy
781 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
782         [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
783         [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
784                                  .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
785         [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
786         [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
787 };
788
789 /* policy for packet pattern attributes */
790 static const struct nla_policy
791 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
792         [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
793         [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
794         [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
795 };
796
797 int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
798                               struct cfg80211_registered_device **rdev,
799                               struct wireless_dev **wdev)
800 {
801         int err;
802
803         if (!cb->args[0]) {
804                 struct nlattr **attrbuf;
805
806                 attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
807                                   GFP_KERNEL);
808                 if (!attrbuf)
809                         return -ENOMEM;
810
811                 err = nlmsg_parse_deprecated(cb->nlh,
812                                              GENL_HDRLEN + nl80211_fam.hdrsize,
813                                              attrbuf, nl80211_fam.maxattr,
814                                              nl80211_policy, NULL);
815                 if (err) {
816                         kfree(attrbuf);
817                         return err;
818                 }
819
820                 *wdev = __cfg80211_wdev_from_attrs(sock_net(cb->skb->sk),
821                                                    attrbuf);
822                 kfree(attrbuf);
823                 if (IS_ERR(*wdev))
824                         return PTR_ERR(*wdev);
825                 *rdev = wiphy_to_rdev((*wdev)->wiphy);
826                 /* 0 is the first index - add 1 to parse only once */
827                 cb->args[0] = (*rdev)->wiphy_idx + 1;
828                 cb->args[1] = (*wdev)->identifier;
829         } else {
830                 /* subtract the 1 again here */
831                 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
832                 struct wireless_dev *tmp;
833
834                 if (!wiphy)
835                         return -ENODEV;
836                 *rdev = wiphy_to_rdev(wiphy);
837                 *wdev = NULL;
838
839                 list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
840                         if (tmp->identifier == cb->args[1]) {
841                                 *wdev = tmp;
842                                 break;
843                         }
844                 }
845
846                 if (!*wdev)
847                         return -ENODEV;
848         }
849
850         return 0;
851 }
852
853 /* message building helper */
854 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
855                      int flags, u8 cmd)
856 {
857         /* since there is no private header just add the generic one */
858         return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
859 }
860
861 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
862                                      const struct ieee80211_reg_rule *rule)
863 {
864         int j;
865         struct nlattr *nl_wmm_rules =
866                 nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
867
868         if (!nl_wmm_rules)
869                 goto nla_put_failure;
870
871         for (j = 0; j < IEEE80211_NUM_ACS; j++) {
872                 struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
873
874                 if (!nl_wmm_rule)
875                         goto nla_put_failure;
876
877                 if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
878                                 rule->wmm_rule.client[j].cw_min) ||
879                     nla_put_u16(msg, NL80211_WMMR_CW_MAX,
880                                 rule->wmm_rule.client[j].cw_max) ||
881                     nla_put_u8(msg, NL80211_WMMR_AIFSN,
882                                rule->wmm_rule.client[j].aifsn) ||
883                     nla_put_u16(msg, NL80211_WMMR_TXOP,
884                                 rule->wmm_rule.client[j].cot))
885                         goto nla_put_failure;
886
887                 nla_nest_end(msg, nl_wmm_rule);
888         }
889         nla_nest_end(msg, nl_wmm_rules);
890
891         return 0;
892
893 nla_put_failure:
894         return -ENOBUFS;
895 }
896
897 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
898                                    struct ieee80211_channel *chan,
899                                    bool large)
900 {
901         /* Some channels must be completely excluded from the
902          * list to protect old user-space tools from breaking
903          */
904         if (!large && chan->flags &
905             (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
906                 return 0;
907
908         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
909                         chan->center_freq))
910                 goto nla_put_failure;
911
912         if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
913             nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
914                 goto nla_put_failure;
915         if (chan->flags & IEEE80211_CHAN_NO_IR) {
916                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
917                         goto nla_put_failure;
918                 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
919                         goto nla_put_failure;
920         }
921         if (chan->flags & IEEE80211_CHAN_RADAR) {
922                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
923                         goto nla_put_failure;
924                 if (large) {
925                         u32 time;
926
927                         time = elapsed_jiffies_msecs(chan->dfs_state_entered);
928
929                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
930                                         chan->dfs_state))
931                                 goto nla_put_failure;
932                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
933                                         time))
934                                 goto nla_put_failure;
935                         if (nla_put_u32(msg,
936                                         NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
937                                         chan->dfs_cac_ms))
938                                 goto nla_put_failure;
939                 }
940         }
941
942         if (large) {
943                 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
944                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
945                         goto nla_put_failure;
946                 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
947                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
948                         goto nla_put_failure;
949                 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
950                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
951                         goto nla_put_failure;
952                 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
953                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
954                         goto nla_put_failure;
955                 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
956                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
957                         goto nla_put_failure;
958                 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
959                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
960                         goto nla_put_failure;
961                 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
962                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
963                         goto nla_put_failure;
964                 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
965                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
966                         goto nla_put_failure;
967         }
968
969         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
970                         DBM_TO_MBM(chan->max_power)))
971                 goto nla_put_failure;
972
973         if (large) {
974                 const struct ieee80211_reg_rule *rule =
975                         freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
976
977                 if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
978                         if (nl80211_msg_put_wmm_rules(msg, rule))
979                                 goto nla_put_failure;
980                 }
981         }
982
983         return 0;
984
985  nla_put_failure:
986         return -ENOBUFS;
987 }
988
989 static bool nl80211_put_txq_stats(struct sk_buff *msg,
990                                   struct cfg80211_txq_stats *txqstats,
991                                   int attrtype)
992 {
993         struct nlattr *txqattr;
994
995 #define PUT_TXQVAL_U32(attr, memb) do {                                   \
996         if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&         \
997             nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
998                 return false;                                             \
999         } while (0)
1000
1001         txqattr = nla_nest_start_noflag(msg, attrtype);
1002         if (!txqattr)
1003                 return false;
1004
1005         PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1006         PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1007         PUT_TXQVAL_U32(FLOWS, flows);
1008         PUT_TXQVAL_U32(DROPS, drops);
1009         PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1010         PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1011         PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1012         PUT_TXQVAL_U32(COLLISIONS, collisions);
1013         PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1014         PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1015         PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1016         nla_nest_end(msg, txqattr);
1017
1018 #undef PUT_TXQVAL_U32
1019         return true;
1020 }
1021
1022 /* netlink command implementations */
1023
1024 struct key_parse {
1025         struct key_params p;
1026         int idx;
1027         int type;
1028         bool def, defmgmt;
1029         bool def_uni, def_multi;
1030 };
1031
1032 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1033                                  struct key_parse *k)
1034 {
1035         struct nlattr *tb[NL80211_KEY_MAX + 1];
1036         int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1037                                               nl80211_key_policy,
1038                                               info->extack);
1039         if (err)
1040                 return err;
1041
1042         k->def = !!tb[NL80211_KEY_DEFAULT];
1043         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1044
1045         if (k->def) {
1046                 k->def_uni = true;
1047                 k->def_multi = true;
1048         }
1049         if (k->defmgmt)
1050                 k->def_multi = true;
1051
1052         if (tb[NL80211_KEY_IDX])
1053                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1054
1055         if (tb[NL80211_KEY_DATA]) {
1056                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1057                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1058         }
1059
1060         if (tb[NL80211_KEY_SEQ]) {
1061                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1062                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1063         }
1064
1065         if (tb[NL80211_KEY_CIPHER])
1066                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1067
1068         if (tb[NL80211_KEY_TYPE])
1069                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1070
1071         if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1072                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1073
1074                 err = nla_parse_nested_deprecated(kdt,
1075                                                   NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1076                                                   tb[NL80211_KEY_DEFAULT_TYPES],
1077                                                   nl80211_key_default_policy,
1078                                                   info->extack);
1079                 if (err)
1080                         return err;
1081
1082                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1083                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1084         }
1085
1086         if (tb[NL80211_KEY_MODE])
1087                 k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1088
1089         return 0;
1090 }
1091
1092 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1093 {
1094         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1095                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1096                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1097         }
1098
1099         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1100                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1101                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1102         }
1103
1104         if (info->attrs[NL80211_ATTR_KEY_IDX])
1105                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1106
1107         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1108                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1109
1110         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1111         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1112
1113         if (k->def) {
1114                 k->def_uni = true;
1115                 k->def_multi = true;
1116         }
1117         if (k->defmgmt)
1118                 k->def_multi = true;
1119
1120         if (info->attrs[NL80211_ATTR_KEY_TYPE])
1121                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1122
1123         if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1124                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1125                 int err = nla_parse_nested_deprecated(kdt,
1126                                                       NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1127                                                       info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1128                                                       nl80211_key_default_policy,
1129                                                       info->extack);
1130                 if (err)
1131                         return err;
1132
1133                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1134                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1135         }
1136
1137         return 0;
1138 }
1139
1140 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1141 {
1142         int err;
1143
1144         memset(k, 0, sizeof(*k));
1145         k->idx = -1;
1146         k->type = -1;
1147
1148         if (info->attrs[NL80211_ATTR_KEY])
1149                 err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1150         else
1151                 err = nl80211_parse_key_old(info, k);
1152
1153         if (err)
1154                 return err;
1155
1156         if (k->def && k->defmgmt) {
1157                 GENL_SET_ERR_MSG(info, "key with def && defmgmt is invalid");
1158                 return -EINVAL;
1159         }
1160
1161         if (k->defmgmt) {
1162                 if (k->def_uni || !k->def_multi) {
1163                         GENL_SET_ERR_MSG(info, "defmgmt key must be mcast");
1164                         return -EINVAL;
1165                 }
1166         }
1167
1168         if (k->idx != -1) {
1169                 if (k->defmgmt) {
1170                         if (k->idx < 4 || k->idx > 5) {
1171                                 GENL_SET_ERR_MSG(info,
1172                                                  "defmgmt key idx not 4 or 5");
1173                                 return -EINVAL;
1174                         }
1175                 } else if (k->def) {
1176                         if (k->idx < 0 || k->idx > 3) {
1177                                 GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1178                                 return -EINVAL;
1179                         }
1180                 } else {
1181                         if (k->idx < 0 || k->idx > 5) {
1182                                 GENL_SET_ERR_MSG(info, "key idx not 0-5");
1183                                 return -EINVAL;
1184                         }
1185                 }
1186         }
1187
1188         return 0;
1189 }
1190
1191 static struct cfg80211_cached_keys *
1192 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1193                        struct genl_info *info, bool *no_ht)
1194 {
1195         struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1196         struct key_parse parse;
1197         struct nlattr *key;
1198         struct cfg80211_cached_keys *result;
1199         int rem, err, def = 0;
1200         bool have_key = false;
1201
1202         nla_for_each_nested(key, keys, rem) {
1203                 have_key = true;
1204                 break;
1205         }
1206
1207         if (!have_key)
1208                 return NULL;
1209
1210         result = kzalloc(sizeof(*result), GFP_KERNEL);
1211         if (!result)
1212                 return ERR_PTR(-ENOMEM);
1213
1214         result->def = -1;
1215
1216         nla_for_each_nested(key, keys, rem) {
1217                 memset(&parse, 0, sizeof(parse));
1218                 parse.idx = -1;
1219
1220                 err = nl80211_parse_key_new(info, key, &parse);
1221                 if (err)
1222                         goto error;
1223                 err = -EINVAL;
1224                 if (!parse.p.key)
1225                         goto error;
1226                 if (parse.idx < 0 || parse.idx > 3) {
1227                         GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1228                         goto error;
1229                 }
1230                 if (parse.def) {
1231                         if (def) {
1232                                 GENL_SET_ERR_MSG(info,
1233                                                  "only one key can be default");
1234                                 goto error;
1235                         }
1236                         def = 1;
1237                         result->def = parse.idx;
1238                         if (!parse.def_uni || !parse.def_multi)
1239                                 goto error;
1240                 } else if (parse.defmgmt)
1241                         goto error;
1242                 err = cfg80211_validate_key_settings(rdev, &parse.p,
1243                                                      parse.idx, false, NULL);
1244                 if (err)
1245                         goto error;
1246                 if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1247                     parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1248                         GENL_SET_ERR_MSG(info, "connect key must be WEP");
1249                         err = -EINVAL;
1250                         goto error;
1251                 }
1252                 result->params[parse.idx].cipher = parse.p.cipher;
1253                 result->params[parse.idx].key_len = parse.p.key_len;
1254                 result->params[parse.idx].key = result->data[parse.idx];
1255                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1256
1257                 /* must be WEP key if we got here */
1258                 if (no_ht)
1259                         *no_ht = true;
1260         }
1261
1262         if (result->def < 0) {
1263                 err = -EINVAL;
1264                 GENL_SET_ERR_MSG(info, "need a default/TX key");
1265                 goto error;
1266         }
1267
1268         return result;
1269  error:
1270         kfree(result);
1271         return ERR_PTR(err);
1272 }
1273
1274 static int nl80211_key_allowed(struct wireless_dev *wdev)
1275 {
1276         ASSERT_WDEV_LOCK(wdev);
1277
1278         switch (wdev->iftype) {
1279         case NL80211_IFTYPE_AP:
1280         case NL80211_IFTYPE_AP_VLAN:
1281         case NL80211_IFTYPE_P2P_GO:
1282         case NL80211_IFTYPE_MESH_POINT:
1283                 break;
1284         case NL80211_IFTYPE_ADHOC:
1285         case NL80211_IFTYPE_STATION:
1286         case NL80211_IFTYPE_P2P_CLIENT:
1287                 if (!wdev->current_bss)
1288                         return -ENOLINK;
1289                 break;
1290         case NL80211_IFTYPE_UNSPECIFIED:
1291         case NL80211_IFTYPE_OCB:
1292         case NL80211_IFTYPE_MONITOR:
1293         case NL80211_IFTYPE_NAN:
1294         case NL80211_IFTYPE_P2P_DEVICE:
1295         case NL80211_IFTYPE_WDS:
1296         case NUM_NL80211_IFTYPES:
1297                 return -EINVAL;
1298         }
1299
1300         return 0;
1301 }
1302
1303 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1304                                                         struct nlattr *tb)
1305 {
1306         struct ieee80211_channel *chan;
1307
1308         if (tb == NULL)
1309                 return NULL;
1310         chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1311         if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1312                 return NULL;
1313         return chan;
1314 }
1315
1316 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1317 {
1318         struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1319         int i;
1320
1321         if (!nl_modes)
1322                 goto nla_put_failure;
1323
1324         i = 0;
1325         while (ifmodes) {
1326                 if ((ifmodes & 1) && nla_put_flag(msg, i))
1327                         goto nla_put_failure;
1328                 ifmodes >>= 1;
1329                 i++;
1330         }
1331
1332         nla_nest_end(msg, nl_modes);
1333         return 0;
1334
1335 nla_put_failure:
1336         return -ENOBUFS;
1337 }
1338
1339 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1340                                           struct sk_buff *msg,
1341                                           bool large)
1342 {
1343         struct nlattr *nl_combis;
1344         int i, j;
1345
1346         nl_combis = nla_nest_start_noflag(msg,
1347                                           NL80211_ATTR_INTERFACE_COMBINATIONS);
1348         if (!nl_combis)
1349                 goto nla_put_failure;
1350
1351         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1352                 const struct ieee80211_iface_combination *c;
1353                 struct nlattr *nl_combi, *nl_limits;
1354
1355                 c = &wiphy->iface_combinations[i];
1356
1357                 nl_combi = nla_nest_start_noflag(msg, i + 1);
1358                 if (!nl_combi)
1359                         goto nla_put_failure;
1360
1361                 nl_limits = nla_nest_start_noflag(msg,
1362                                                   NL80211_IFACE_COMB_LIMITS);
1363                 if (!nl_limits)
1364                         goto nla_put_failure;
1365
1366                 for (j = 0; j < c->n_limits; j++) {
1367                         struct nlattr *nl_limit;
1368
1369                         nl_limit = nla_nest_start_noflag(msg, j + 1);
1370                         if (!nl_limit)
1371                                 goto nla_put_failure;
1372                         if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1373                                         c->limits[j].max))
1374                                 goto nla_put_failure;
1375                         if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1376                                                 c->limits[j].types))
1377                                 goto nla_put_failure;
1378                         nla_nest_end(msg, nl_limit);
1379                 }
1380
1381                 nla_nest_end(msg, nl_limits);
1382
1383                 if (c->beacon_int_infra_match &&
1384                     nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1385                         goto nla_put_failure;
1386                 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1387                                 c->num_different_channels) ||
1388                     nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1389                                 c->max_interfaces))
1390                         goto nla_put_failure;
1391                 if (large &&
1392                     (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1393                                 c->radar_detect_widths) ||
1394                      nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1395                                 c->radar_detect_regions)))
1396                         goto nla_put_failure;
1397                 if (c->beacon_int_min_gcd &&
1398                     nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1399                                 c->beacon_int_min_gcd))
1400                         goto nla_put_failure;
1401
1402                 nla_nest_end(msg, nl_combi);
1403         }
1404
1405         nla_nest_end(msg, nl_combis);
1406
1407         return 0;
1408 nla_put_failure:
1409         return -ENOBUFS;
1410 }
1411
1412 #ifdef CONFIG_PM
1413 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1414                                         struct sk_buff *msg)
1415 {
1416         const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1417         struct nlattr *nl_tcp;
1418
1419         if (!tcp)
1420                 return 0;
1421
1422         nl_tcp = nla_nest_start_noflag(msg,
1423                                        NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1424         if (!nl_tcp)
1425                 return -ENOBUFS;
1426
1427         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1428                         tcp->data_payload_max))
1429                 return -ENOBUFS;
1430
1431         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1432                         tcp->data_payload_max))
1433                 return -ENOBUFS;
1434
1435         if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1436                 return -ENOBUFS;
1437
1438         if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1439                                 sizeof(*tcp->tok), tcp->tok))
1440                 return -ENOBUFS;
1441
1442         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1443                         tcp->data_interval_max))
1444                 return -ENOBUFS;
1445
1446         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1447                         tcp->wake_payload_max))
1448                 return -ENOBUFS;
1449
1450         nla_nest_end(msg, nl_tcp);
1451         return 0;
1452 }
1453
1454 static int nl80211_send_wowlan(struct sk_buff *msg,
1455                                struct cfg80211_registered_device *rdev,
1456                                bool large)
1457 {
1458         struct nlattr *nl_wowlan;
1459
1460         if (!rdev->wiphy.wowlan)
1461                 return 0;
1462
1463         nl_wowlan = nla_nest_start_noflag(msg,
1464                                           NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1465         if (!nl_wowlan)
1466                 return -ENOBUFS;
1467
1468         if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1469              nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1470             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1471              nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1472             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1473              nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1474             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1475              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1476             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1477              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1478             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1479              nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1480             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1481              nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1482             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1483              nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1484                 return -ENOBUFS;
1485
1486         if (rdev->wiphy.wowlan->n_patterns) {
1487                 struct nl80211_pattern_support pat = {
1488                         .max_patterns = rdev->wiphy.wowlan->n_patterns,
1489                         .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1490                         .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1491                         .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1492                 };
1493
1494                 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1495                             sizeof(pat), &pat))
1496                         return -ENOBUFS;
1497         }
1498
1499         if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1500             nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1501                         rdev->wiphy.wowlan->max_nd_match_sets))
1502                 return -ENOBUFS;
1503
1504         if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1505                 return -ENOBUFS;
1506
1507         nla_nest_end(msg, nl_wowlan);
1508
1509         return 0;
1510 }
1511 #endif
1512
1513 static int nl80211_send_coalesce(struct sk_buff *msg,
1514                                  struct cfg80211_registered_device *rdev)
1515 {
1516         struct nl80211_coalesce_rule_support rule;
1517
1518         if (!rdev->wiphy.coalesce)
1519                 return 0;
1520
1521         rule.max_rules = rdev->wiphy.coalesce->n_rules;
1522         rule.max_delay = rdev->wiphy.coalesce->max_delay;
1523         rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1524         rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1525         rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1526         rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1527
1528         if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1529                 return -ENOBUFS;
1530
1531         return 0;
1532 }
1533
1534 static int
1535 nl80211_send_iftype_data(struct sk_buff *msg,
1536                          const struct ieee80211_sband_iftype_data *iftdata)
1537 {
1538         const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1539
1540         if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1541                                 iftdata->types_mask))
1542                 return -ENOBUFS;
1543
1544         if (he_cap->has_he) {
1545                 if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1546                             sizeof(he_cap->he_cap_elem.mac_cap_info),
1547                             he_cap->he_cap_elem.mac_cap_info) ||
1548                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1549                             sizeof(he_cap->he_cap_elem.phy_cap_info),
1550                             he_cap->he_cap_elem.phy_cap_info) ||
1551                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1552                             sizeof(he_cap->he_mcs_nss_supp),
1553                             &he_cap->he_mcs_nss_supp) ||
1554                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1555                             sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1556                         return -ENOBUFS;
1557         }
1558
1559         return 0;
1560 }
1561
1562 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1563                                       struct ieee80211_supported_band *sband)
1564 {
1565         struct nlattr *nl_rates, *nl_rate;
1566         struct ieee80211_rate *rate;
1567         int i;
1568
1569         /* add HT info */
1570         if (sband->ht_cap.ht_supported &&
1571             (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1572                      sizeof(sband->ht_cap.mcs),
1573                      &sband->ht_cap.mcs) ||
1574              nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1575                          sband->ht_cap.cap) ||
1576              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1577                         sband->ht_cap.ampdu_factor) ||
1578              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1579                         sband->ht_cap.ampdu_density)))
1580                 return -ENOBUFS;
1581
1582         /* add VHT info */
1583         if (sband->vht_cap.vht_supported &&
1584             (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1585                      sizeof(sband->vht_cap.vht_mcs),
1586                      &sband->vht_cap.vht_mcs) ||
1587              nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1588                          sband->vht_cap.cap)))
1589                 return -ENOBUFS;
1590
1591         if (sband->n_iftype_data) {
1592                 struct nlattr *nl_iftype_data =
1593                         nla_nest_start_noflag(msg,
1594                                               NL80211_BAND_ATTR_IFTYPE_DATA);
1595                 int err;
1596
1597                 if (!nl_iftype_data)
1598                         return -ENOBUFS;
1599
1600                 for (i = 0; i < sband->n_iftype_data; i++) {
1601                         struct nlattr *iftdata;
1602
1603                         iftdata = nla_nest_start_noflag(msg, i + 1);
1604                         if (!iftdata)
1605                                 return -ENOBUFS;
1606
1607                         err = nl80211_send_iftype_data(msg,
1608                                                        &sband->iftype_data[i]);
1609                         if (err)
1610                                 return err;
1611
1612                         nla_nest_end(msg, iftdata);
1613                 }
1614
1615                 nla_nest_end(msg, nl_iftype_data);
1616         }
1617
1618         /* add EDMG info */
1619         if (sband->edmg_cap.channels &&
1620             (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1621                        sband->edmg_cap.channels) ||
1622             nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1623                        sband->edmg_cap.bw_config)))
1624
1625                 return -ENOBUFS;
1626
1627         /* add bitrates */
1628         nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1629         if (!nl_rates)
1630                 return -ENOBUFS;
1631
1632         for (i = 0; i < sband->n_bitrates; i++) {
1633                 nl_rate = nla_nest_start_noflag(msg, i);
1634                 if (!nl_rate)
1635                         return -ENOBUFS;
1636
1637                 rate = &sband->bitrates[i];
1638                 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1639                                 rate->bitrate))
1640                         return -ENOBUFS;
1641                 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1642                     nla_put_flag(msg,
1643                                  NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1644                         return -ENOBUFS;
1645
1646                 nla_nest_end(msg, nl_rate);
1647         }
1648
1649         nla_nest_end(msg, nl_rates);
1650
1651         return 0;
1652 }
1653
1654 static int
1655 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1656                          const struct ieee80211_txrx_stypes *mgmt_stypes)
1657 {
1658         u16 stypes;
1659         struct nlattr *nl_ftypes, *nl_ifs;
1660         enum nl80211_iftype ift;
1661         int i;
1662
1663         if (!mgmt_stypes)
1664                 return 0;
1665
1666         nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1667         if (!nl_ifs)
1668                 return -ENOBUFS;
1669
1670         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1671                 nl_ftypes = nla_nest_start_noflag(msg, ift);
1672                 if (!nl_ftypes)
1673                         return -ENOBUFS;
1674                 i = 0;
1675                 stypes = mgmt_stypes[ift].tx;
1676                 while (stypes) {
1677                         if ((stypes & 1) &&
1678                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1679                                         (i << 4) | IEEE80211_FTYPE_MGMT))
1680                                 return -ENOBUFS;
1681                         stypes >>= 1;
1682                         i++;
1683                 }
1684                 nla_nest_end(msg, nl_ftypes);
1685         }
1686
1687         nla_nest_end(msg, nl_ifs);
1688
1689         nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1690         if (!nl_ifs)
1691                 return -ENOBUFS;
1692
1693         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1694                 nl_ftypes = nla_nest_start_noflag(msg, ift);
1695                 if (!nl_ftypes)
1696                         return -ENOBUFS;
1697                 i = 0;
1698                 stypes = mgmt_stypes[ift].rx;
1699                 while (stypes) {
1700                         if ((stypes & 1) &&
1701                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1702                                         (i << 4) | IEEE80211_FTYPE_MGMT))
1703                                 return -ENOBUFS;
1704                         stypes >>= 1;
1705                         i++;
1706                 }
1707                 nla_nest_end(msg, nl_ftypes);
1708         }
1709         nla_nest_end(msg, nl_ifs);
1710
1711         return 0;
1712 }
1713
1714 #define CMD(op, n)                                                      \
1715          do {                                                           \
1716                 if (rdev->ops->op) {                                    \
1717                         i++;                                            \
1718                         if (nla_put_u32(msg, i, NL80211_CMD_ ## n))     \
1719                                 goto nla_put_failure;                   \
1720                 }                                                       \
1721         } while (0)
1722
1723 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1724                                         struct sk_buff *msg)
1725 {
1726         int i = 0;
1727
1728         /*
1729          * do *NOT* add anything into this function, new things need to be
1730          * advertised only to new versions of userspace that can deal with
1731          * the split (and they can't possibly care about new features...
1732          */
1733         CMD(add_virtual_intf, NEW_INTERFACE);
1734         CMD(change_virtual_intf, SET_INTERFACE);
1735         CMD(add_key, NEW_KEY);
1736         CMD(start_ap, START_AP);
1737         CMD(add_station, NEW_STATION);
1738         CMD(add_mpath, NEW_MPATH);
1739         CMD(update_mesh_config, SET_MESH_CONFIG);
1740         CMD(change_bss, SET_BSS);
1741         CMD(auth, AUTHENTICATE);
1742         CMD(assoc, ASSOCIATE);
1743         CMD(deauth, DEAUTHENTICATE);
1744         CMD(disassoc, DISASSOCIATE);
1745         CMD(join_ibss, JOIN_IBSS);
1746         CMD(join_mesh, JOIN_MESH);
1747         CMD(set_pmksa, SET_PMKSA);
1748         CMD(del_pmksa, DEL_PMKSA);
1749         CMD(flush_pmksa, FLUSH_PMKSA);
1750         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1751                 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1752         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1753         CMD(mgmt_tx, FRAME);
1754         CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1755         if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1756                 i++;
1757                 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1758                         goto nla_put_failure;
1759         }
1760         if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1761             rdev->ops->join_mesh) {
1762                 i++;
1763                 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1764                         goto nla_put_failure;
1765         }
1766         CMD(set_wds_peer, SET_WDS_PEER);
1767         if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1768                 CMD(tdls_mgmt, TDLS_MGMT);
1769                 CMD(tdls_oper, TDLS_OPER);
1770         }
1771         if (rdev->wiphy.max_sched_scan_reqs)
1772                 CMD(sched_scan_start, START_SCHED_SCAN);
1773         CMD(probe_client, PROBE_CLIENT);
1774         CMD(set_noack_map, SET_NOACK_MAP);
1775         if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1776                 i++;
1777                 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1778                         goto nla_put_failure;
1779         }
1780         CMD(start_p2p_device, START_P2P_DEVICE);
1781         CMD(set_mcast_rate, SET_MCAST_RATE);
1782 #ifdef CONFIG_NL80211_TESTMODE
1783         CMD(testmode_cmd, TESTMODE);
1784 #endif
1785
1786         if (rdev->ops->connect || rdev->ops->auth) {
1787                 i++;
1788                 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1789                         goto nla_put_failure;
1790         }
1791
1792         if (rdev->ops->disconnect || rdev->ops->deauth) {
1793                 i++;
1794                 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1795                         goto nla_put_failure;
1796         }
1797
1798         return i;
1799  nla_put_failure:
1800         return -ENOBUFS;
1801 }
1802
1803 static int
1804 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
1805                            struct sk_buff *msg)
1806 {
1807         struct nlattr *ftm;
1808
1809         if (!cap->ftm.supported)
1810                 return 0;
1811
1812         ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
1813         if (!ftm)
1814                 return -ENOBUFS;
1815
1816         if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
1817                 return -ENOBUFS;
1818         if (cap->ftm.non_asap &&
1819             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
1820                 return -ENOBUFS;
1821         if (cap->ftm.request_lci &&
1822             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
1823                 return -ENOBUFS;
1824         if (cap->ftm.request_civicloc &&
1825             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
1826                 return -ENOBUFS;
1827         if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
1828                         cap->ftm.preambles))
1829                 return -ENOBUFS;
1830         if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
1831                         cap->ftm.bandwidths))
1832                 return -ENOBUFS;
1833         if (cap->ftm.max_bursts_exponent >= 0 &&
1834             nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
1835                         cap->ftm.max_bursts_exponent))
1836                 return -ENOBUFS;
1837         if (cap->ftm.max_ftms_per_burst &&
1838             nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
1839                         cap->ftm.max_ftms_per_burst))
1840                 return -ENOBUFS;
1841
1842         nla_nest_end(msg, ftm);
1843         return 0;
1844 }
1845
1846 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
1847                                   struct sk_buff *msg)
1848 {
1849         const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
1850         struct nlattr *pmsr, *caps;
1851
1852         if (!cap)
1853                 return 0;
1854
1855         /*
1856          * we don't need to clean up anything here since the caller
1857          * will genlmsg_cancel() if we fail
1858          */
1859
1860         pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
1861         if (!pmsr)
1862                 return -ENOBUFS;
1863
1864         if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
1865                 return -ENOBUFS;
1866
1867         if (cap->report_ap_tsf &&
1868             nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
1869                 return -ENOBUFS;
1870
1871         if (cap->randomize_mac_addr &&
1872             nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
1873                 return -ENOBUFS;
1874
1875         caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
1876         if (!caps)
1877                 return -ENOBUFS;
1878
1879         if (nl80211_send_pmsr_ftm_capa(cap, msg))
1880                 return -ENOBUFS;
1881
1882         nla_nest_end(msg, caps);
1883         nla_nest_end(msg, pmsr);
1884
1885         return 0;
1886 }
1887
1888 struct nl80211_dump_wiphy_state {
1889         s64 filter_wiphy;
1890         long start;
1891         long split_start, band_start, chan_start, capa_start;
1892         bool split;
1893 };
1894
1895 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1896                               enum nl80211_commands cmd,
1897                               struct sk_buff *msg, u32 portid, u32 seq,
1898                               int flags, struct nl80211_dump_wiphy_state *state)
1899 {
1900         void *hdr;
1901         struct nlattr *nl_bands, *nl_band;
1902         struct nlattr *nl_freqs, *nl_freq;
1903         struct nlattr *nl_cmds;
1904         enum nl80211_band band;
1905         struct ieee80211_channel *chan;
1906         int i;
1907         const struct ieee80211_txrx_stypes *mgmt_stypes =
1908                                 rdev->wiphy.mgmt_stypes;
1909         u32 features;
1910
1911         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1912         if (!hdr)
1913                 return -ENOBUFS;
1914
1915         if (WARN_ON(!state))
1916                 return -EINVAL;
1917
1918         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1919             nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1920                            wiphy_name(&rdev->wiphy)) ||
1921             nla_put_u32(msg, NL80211_ATTR_GENERATION,
1922                         cfg80211_rdev_list_generation))
1923                 goto nla_put_failure;
1924
1925         if (cmd != NL80211_CMD_NEW_WIPHY)
1926                 goto finish;
1927
1928         switch (state->split_start) {
1929         case 0:
1930                 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1931                                rdev->wiphy.retry_short) ||
1932                     nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1933                                rdev->wiphy.retry_long) ||
1934                     nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1935                                 rdev->wiphy.frag_threshold) ||
1936                     nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1937                                 rdev->wiphy.rts_threshold) ||
1938                     nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1939                                rdev->wiphy.coverage_class) ||
1940                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1941                                rdev->wiphy.max_scan_ssids) ||
1942                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1943                                rdev->wiphy.max_sched_scan_ssids) ||
1944                     nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1945                                 rdev->wiphy.max_scan_ie_len) ||
1946                     nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1947                                 rdev->wiphy.max_sched_scan_ie_len) ||
1948                     nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1949                                rdev->wiphy.max_match_sets) ||
1950                     nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1951                                 rdev->wiphy.max_sched_scan_plans) ||
1952                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1953                                 rdev->wiphy.max_sched_scan_plan_interval) ||
1954                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1955                                 rdev->wiphy.max_sched_scan_plan_iterations))
1956                         goto nla_put_failure;
1957
1958                 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1959                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1960                         goto nla_put_failure;
1961                 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1962                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1963                         goto nla_put_failure;
1964                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1965                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1966                         goto nla_put_failure;
1967                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1968                     nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1969                         goto nla_put_failure;
1970                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1971                     nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1972                         goto nla_put_failure;
1973                 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1974                     nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1975                         goto nla_put_failure;
1976                 state->split_start++;
1977                 if (state->split)
1978                         break;
1979                 /* fall through */
1980         case 1:
1981                 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1982                             sizeof(u32) * rdev->wiphy.n_cipher_suites,
1983                             rdev->wiphy.cipher_suites))
1984                         goto nla_put_failure;
1985
1986                 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1987                                rdev->wiphy.max_num_pmkids))
1988                         goto nla_put_failure;
1989
1990                 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1991                     nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1992                         goto nla_put_failure;
1993
1994                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1995                                 rdev->wiphy.available_antennas_tx) ||
1996                     nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1997                                 rdev->wiphy.available_antennas_rx))
1998                         goto nla_put_failure;
1999
2000                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2001                     nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2002                                 rdev->wiphy.probe_resp_offload))
2003                         goto nla_put_failure;
2004
2005                 if ((rdev->wiphy.available_antennas_tx ||
2006                      rdev->wiphy.available_antennas_rx) &&
2007                     rdev->ops->get_antenna) {
2008                         u32 tx_ant = 0, rx_ant = 0;
2009                         int res;
2010
2011                         res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2012                         if (!res) {
2013                                 if (nla_put_u32(msg,
2014                                                 NL80211_ATTR_WIPHY_ANTENNA_TX,
2015                                                 tx_ant) ||
2016                                     nla_put_u32(msg,
2017                                                 NL80211_ATTR_WIPHY_ANTENNA_RX,
2018                                                 rx_ant))
2019                                         goto nla_put_failure;
2020                         }
2021                 }
2022
2023                 state->split_start++;
2024                 if (state->split)
2025                         break;
2026                 /* fall through */
2027         case 2:
2028                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2029                                         rdev->wiphy.interface_modes))
2030                                 goto nla_put_failure;
2031                 state->split_start++;
2032                 if (state->split)
2033                         break;
2034                 /* fall through */
2035         case 3:
2036                 nl_bands = nla_nest_start_noflag(msg,
2037                                                  NL80211_ATTR_WIPHY_BANDS);
2038                 if (!nl_bands)
2039                         goto nla_put_failure;
2040
2041                 for (band = state->band_start;
2042                      band < NUM_NL80211_BANDS; band++) {
2043                         struct ieee80211_supported_band *sband;
2044
2045                         sband = rdev->wiphy.bands[band];
2046
2047                         if (!sband)
2048                                 continue;
2049
2050                         nl_band = nla_nest_start_noflag(msg, band);
2051                         if (!nl_band)
2052                                 goto nla_put_failure;
2053
2054                         switch (state->chan_start) {
2055                         case 0:
2056                                 if (nl80211_send_band_rateinfo(msg, sband))
2057                                         goto nla_put_failure;
2058                                 state->chan_start++;
2059                                 if (state->split)
2060                                         break;
2061                                 /* fall through */
2062                         default:
2063                                 /* add frequencies */
2064                                 nl_freqs = nla_nest_start_noflag(msg,
2065                                                                  NL80211_BAND_ATTR_FREQS);
2066                                 if (!nl_freqs)
2067                                         goto nla_put_failure;
2068
2069                                 for (i = state->chan_start - 1;
2070                                      i < sband->n_channels;
2071                                      i++) {
2072                                         nl_freq = nla_nest_start_noflag(msg,
2073                                                                         i);
2074                                         if (!nl_freq)
2075                                                 goto nla_put_failure;
2076
2077                                         chan = &sband->channels[i];
2078
2079                                         if (nl80211_msg_put_channel(
2080                                                         msg, &rdev->wiphy, chan,
2081                                                         state->split))
2082                                                 goto nla_put_failure;
2083
2084                                         nla_nest_end(msg, nl_freq);
2085                                         if (state->split)
2086                                                 break;
2087                                 }
2088                                 if (i < sband->n_channels)
2089                                         state->chan_start = i + 2;
2090                                 else
2091                                         state->chan_start = 0;
2092                                 nla_nest_end(msg, nl_freqs);
2093                         }
2094
2095                         nla_nest_end(msg, nl_band);
2096
2097                         if (state->split) {
2098                                 /* start again here */
2099                                 if (state->chan_start)
2100                                         band--;
2101                                 break;
2102                         }
2103                 }
2104                 nla_nest_end(msg, nl_bands);
2105
2106                 if (band < NUM_NL80211_BANDS)
2107                         state->band_start = band + 1;
2108                 else
2109                         state->band_start = 0;
2110
2111                 /* if bands & channels are done, continue outside */
2112                 if (state->band_start == 0 && state->chan_start == 0)
2113                         state->split_start++;
2114                 if (state->split)
2115                         break;
2116                 /* fall through */
2117         case 4:
2118                 nl_cmds = nla_nest_start_noflag(msg,
2119                                                 NL80211_ATTR_SUPPORTED_COMMANDS);
2120                 if (!nl_cmds)
2121                         goto nla_put_failure;
2122
2123                 i = nl80211_add_commands_unsplit(rdev, msg);
2124                 if (i < 0)
2125                         goto nla_put_failure;
2126                 if (state->split) {
2127                         CMD(crit_proto_start, CRIT_PROTOCOL_START);
2128                         CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2129                         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2130                                 CMD(channel_switch, CHANNEL_SWITCH);
2131                         CMD(set_qos_map, SET_QOS_MAP);
2132                         if (rdev->wiphy.features &
2133                                         NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2134                                 CMD(add_tx_ts, ADD_TX_TS);
2135                         CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2136                         CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2137                         CMD(update_ft_ies, UPDATE_FT_IES);
2138                 }
2139 #undef CMD
2140
2141                 nla_nest_end(msg, nl_cmds);
2142                 state->split_start++;
2143                 if (state->split)
2144                         break;
2145                 /* fall through */
2146         case 5:
2147                 if (rdev->ops->remain_on_channel &&
2148                     (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2149                     nla_put_u32(msg,
2150                                 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2151                                 rdev->wiphy.max_remain_on_channel_duration))
2152                         goto nla_put_failure;
2153
2154                 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2155                     nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2156                         goto nla_put_failure;
2157
2158                 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2159                         goto nla_put_failure;
2160                 state->split_start++;
2161                 if (state->split)
2162                         break;
2163                 /* fall through */
2164         case 6:
2165 #ifdef CONFIG_PM
2166                 if (nl80211_send_wowlan(msg, rdev, state->split))
2167                         goto nla_put_failure;
2168                 state->split_start++;
2169                 if (state->split)
2170                         break;
2171 #else
2172                 state->split_start++;
2173 #endif
2174                 /* fall through */
2175         case 7:
2176                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2177                                         rdev->wiphy.software_iftypes))
2178                         goto nla_put_failure;
2179
2180                 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2181                                                    state->split))
2182                         goto nla_put_failure;
2183
2184                 state->split_start++;
2185                 if (state->split)
2186                         break;
2187                 /* fall through */
2188         case 8:
2189                 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2190                     nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2191                                 rdev->wiphy.ap_sme_capa))
2192                         goto nla_put_failure;
2193
2194                 features = rdev->wiphy.features;
2195                 /*
2196                  * We can only add the per-channel limit information if the
2197                  * dump is split, otherwise it makes it too big. Therefore
2198                  * only advertise it in that case.
2199                  */
2200                 if (state->split)
2201                         features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2202                 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2203                         goto nla_put_failure;
2204
2205                 if (rdev->wiphy.ht_capa_mod_mask &&
2206                     nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2207                             sizeof(*rdev->wiphy.ht_capa_mod_mask),
2208                             rdev->wiphy.ht_capa_mod_mask))
2209                         goto nla_put_failure;
2210
2211                 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2212                     rdev->wiphy.max_acl_mac_addrs &&
2213                     nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2214                                 rdev->wiphy.max_acl_mac_addrs))
2215                         goto nla_put_failure;
2216
2217                 /*
2218                  * Any information below this point is only available to
2219                  * applications that can deal with it being split. This
2220                  * helps ensure that newly added capabilities don't break
2221                  * older tools by overrunning their buffers.
2222                  *
2223                  * We still increment split_start so that in the split
2224                  * case we'll continue with more data in the next round,
2225                  * but break unconditionally so unsplit data stops here.
2226                  */
2227                 state->split_start++;
2228                 break;
2229         case 9:
2230                 if (rdev->wiphy.extended_capabilities &&
2231                     (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2232                              rdev->wiphy.extended_capabilities_len,
2233                              rdev->wiphy.extended_capabilities) ||
2234                      nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2235                              rdev->wiphy.extended_capabilities_len,
2236                              rdev->wiphy.extended_capabilities_mask)))
2237                         goto nla_put_failure;
2238
2239                 if (rdev->wiphy.vht_capa_mod_mask &&
2240                     nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2241                             sizeof(*rdev->wiphy.vht_capa_mod_mask),
2242                             rdev->wiphy.vht_capa_mod_mask))
2243                         goto nla_put_failure;
2244
2245                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2246                             rdev->wiphy.perm_addr))
2247                         goto nla_put_failure;
2248
2249                 if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2250                     nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2251                             rdev->wiphy.addr_mask))
2252                         goto nla_put_failure;
2253
2254                 if (rdev->wiphy.n_addresses > 1) {
2255                         void *attr;
2256
2257                         attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2258                         if (!attr)
2259                                 goto nla_put_failure;
2260
2261                         for (i = 0; i < rdev->wiphy.n_addresses; i++)
2262                                 if (nla_put(msg, i + 1, ETH_ALEN,
2263                                             rdev->wiphy.addresses[i].addr))
2264                                         goto nla_put_failure;
2265
2266                         nla_nest_end(msg, attr);
2267                 }
2268
2269                 state->split_start++;
2270                 break;
2271         case 10:
2272                 if (nl80211_send_coalesce(msg, rdev))
2273                         goto nla_put_failure;
2274
2275                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2276                     (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2277                      nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2278                         goto nla_put_failure;
2279
2280                 if (rdev->wiphy.max_ap_assoc_sta &&
2281                     nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2282                                 rdev->wiphy.max_ap_assoc_sta))
2283                         goto nla_put_failure;
2284
2285                 state->split_start++;
2286                 break;
2287         case 11:
2288                 if (rdev->wiphy.n_vendor_commands) {
2289                         const struct nl80211_vendor_cmd_info *info;
2290                         struct nlattr *nested;
2291
2292                         nested = nla_nest_start_noflag(msg,
2293                                                        NL80211_ATTR_VENDOR_DATA);
2294                         if (!nested)
2295                                 goto nla_put_failure;
2296
2297                         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2298                                 info = &rdev->wiphy.vendor_commands[i].info;
2299                                 if (nla_put(msg, i + 1, sizeof(*info), info))
2300                                         goto nla_put_failure;
2301                         }
2302                         nla_nest_end(msg, nested);
2303                 }
2304
2305                 if (rdev->wiphy.n_vendor_events) {
2306                         const struct nl80211_vendor_cmd_info *info;
2307                         struct nlattr *nested;
2308
2309                         nested = nla_nest_start_noflag(msg,
2310                                                        NL80211_ATTR_VENDOR_EVENTS);
2311                         if (!nested)
2312                                 goto nla_put_failure;
2313
2314                         for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2315                                 info = &rdev->wiphy.vendor_events[i];
2316                                 if (nla_put(msg, i + 1, sizeof(*info), info))
2317                                         goto nla_put_failure;
2318                         }
2319                         nla_nest_end(msg, nested);
2320                 }
2321                 state->split_start++;
2322                 break;
2323         case 12:
2324                 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2325                     nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2326                                rdev->wiphy.max_num_csa_counters))
2327                         goto nla_put_failure;
2328
2329                 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2330                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2331                         goto nla_put_failure;
2332
2333                 if (rdev->wiphy.max_sched_scan_reqs &&
2334                     nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2335                                 rdev->wiphy.max_sched_scan_reqs))
2336                         goto nla_put_failure;
2337
2338                 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2339                             sizeof(rdev->wiphy.ext_features),
2340                             rdev->wiphy.ext_features))
2341                         goto nla_put_failure;
2342
2343                 if (rdev->wiphy.bss_select_support) {
2344                         struct nlattr *nested;
2345                         u32 bss_select_support = rdev->wiphy.bss_select_support;
2346
2347                         nested = nla_nest_start_noflag(msg,
2348                                                        NL80211_ATTR_BSS_SELECT);
2349                         if (!nested)
2350                                 goto nla_put_failure;
2351
2352                         i = 0;
2353                         while (bss_select_support) {
2354                                 if ((bss_select_support & 1) &&
2355                                     nla_put_flag(msg, i))
2356                                         goto nla_put_failure;
2357                                 i++;
2358                                 bss_select_support >>= 1;
2359                         }
2360                         nla_nest_end(msg, nested);
2361                 }
2362
2363                 state->split_start++;
2364                 break;
2365         case 13:
2366                 if (rdev->wiphy.num_iftype_ext_capab &&
2367                     rdev->wiphy.iftype_ext_capab) {
2368                         struct nlattr *nested_ext_capab, *nested;
2369
2370                         nested = nla_nest_start_noflag(msg,
2371                                                        NL80211_ATTR_IFTYPE_EXT_CAPA);
2372                         if (!nested)
2373                                 goto nla_put_failure;
2374
2375                         for (i = state->capa_start;
2376                              i < rdev->wiphy.num_iftype_ext_capab; i++) {
2377                                 const struct wiphy_iftype_ext_capab *capab;
2378
2379                                 capab = &rdev->wiphy.iftype_ext_capab[i];
2380
2381                                 nested_ext_capab = nla_nest_start_noflag(msg,
2382                                                                          i);
2383                                 if (!nested_ext_capab ||
2384                                     nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2385                                                 capab->iftype) ||
2386                                     nla_put(msg, NL80211_ATTR_EXT_CAPA,
2387                                             capab->extended_capabilities_len,
2388                                             capab->extended_capabilities) ||
2389                                     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2390                                             capab->extended_capabilities_len,
2391                                             capab->extended_capabilities_mask))
2392                                         goto nla_put_failure;
2393
2394                                 nla_nest_end(msg, nested_ext_capab);
2395                                 if (state->split)
2396                                         break;
2397                         }
2398                         nla_nest_end(msg, nested);
2399                         if (i < rdev->wiphy.num_iftype_ext_capab) {
2400                                 state->capa_start = i + 1;
2401                                 break;
2402                         }
2403                 }
2404
2405                 if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2406                                 rdev->wiphy.nan_supported_bands))
2407                         goto nla_put_failure;
2408
2409                 if (wiphy_ext_feature_isset(&rdev->wiphy,
2410                                             NL80211_EXT_FEATURE_TXQS)) {
2411                         struct cfg80211_txq_stats txqstats = {};
2412                         int res;
2413
2414                         res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2415                         if (!res &&
2416                             !nl80211_put_txq_stats(msg, &txqstats,
2417                                                    NL80211_ATTR_TXQ_STATS))
2418                                 goto nla_put_failure;
2419
2420                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2421                                         rdev->wiphy.txq_limit))
2422                                 goto nla_put_failure;
2423                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2424                                         rdev->wiphy.txq_memory_limit))
2425                                 goto nla_put_failure;
2426                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2427                                         rdev->wiphy.txq_quantum))
2428                                 goto nla_put_failure;
2429                 }
2430
2431                 state->split_start++;
2432                 break;
2433         case 14:
2434                 if (nl80211_send_pmsr_capa(rdev, msg))
2435                         goto nla_put_failure;
2436
2437                 state->split_start++;
2438                 break;
2439         case 15:
2440                 if (rdev->wiphy.akm_suites &&
2441                     nla_put(msg, NL80211_ATTR_AKM_SUITES,
2442                             sizeof(u32) * rdev->wiphy.n_akm_suites,
2443                             rdev->wiphy.akm_suites))
2444                         goto nla_put_failure;
2445
2446                 /* done */
2447                 state->split_start = 0;
2448                 break;
2449         }
2450  finish:
2451         genlmsg_end(msg, hdr);
2452         return 0;
2453
2454  nla_put_failure:
2455         genlmsg_cancel(msg, hdr);
2456         return -EMSGSIZE;
2457 }
2458
2459 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2460                                     struct netlink_callback *cb,
2461                                     struct nl80211_dump_wiphy_state *state)
2462 {
2463         struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2464         int ret;
2465
2466         if (!tb)
2467                 return -ENOMEM;
2468
2469         ret = nlmsg_parse_deprecated(cb->nlh,
2470                                      GENL_HDRLEN + nl80211_fam.hdrsize,
2471                                      tb, nl80211_fam.maxattr,
2472                                      nl80211_policy, NULL);
2473         /* ignore parse errors for backward compatibility */
2474         if (ret) {
2475                 ret = 0;
2476                 goto out;
2477         }
2478
2479         state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2480         if (tb[NL80211_ATTR_WIPHY])
2481                 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2482         if (tb[NL80211_ATTR_WDEV])
2483                 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
2484         if (tb[NL80211_ATTR_IFINDEX]) {
2485                 struct net_device *netdev;
2486                 struct cfg80211_registered_device *rdev;
2487                 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2488
2489                 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
2490                 if (!netdev) {
2491                         ret = -ENODEV;
2492                         goto out;
2493                 }
2494                 if (netdev->ieee80211_ptr) {
2495                         rdev = wiphy_to_rdev(
2496                                 netdev->ieee80211_ptr->wiphy);
2497                         state->filter_wiphy = rdev->wiphy_idx;
2498                 }
2499         }
2500
2501         ret = 0;
2502 out:
2503         kfree(tb);
2504         return ret;
2505 }
2506
2507 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
2508 {
2509         int idx = 0, ret;
2510         struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
2511         struct cfg80211_registered_device *rdev;
2512
2513         rtnl_lock();
2514         if (!state) {
2515                 state = kzalloc(sizeof(*state), GFP_KERNEL);
2516                 if (!state) {
2517                         rtnl_unlock();
2518                         return -ENOMEM;
2519                 }
2520                 state->filter_wiphy = -1;
2521                 ret = nl80211_dump_wiphy_parse(skb, cb, state);
2522                 if (ret) {
2523                         kfree(state);
2524                         rtnl_unlock();
2525                         return ret;
2526                 }
2527                 cb->args[0] = (long)state;
2528         }
2529
2530         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2531                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2532                         continue;
2533                 if (++idx <= state->start)
2534                         continue;
2535                 if (state->filter_wiphy != -1 &&
2536                     state->filter_wiphy != rdev->wiphy_idx)
2537                         continue;
2538                 /* attempt to fit multiple wiphy data chunks into the skb */
2539                 do {
2540                         ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2541                                                  skb,
2542                                                  NETLINK_CB(cb->skb).portid,
2543                                                  cb->nlh->nlmsg_seq,
2544                                                  NLM_F_MULTI, state);
2545                         if (ret < 0) {
2546                                 /*
2547                                  * If sending the wiphy data didn't fit (ENOBUFS
2548                                  * or EMSGSIZE returned), this SKB is still
2549                                  * empty (so it's not too big because another
2550                                  * wiphy dataset is already in the skb) and
2551                                  * we've not tried to adjust the dump allocation
2552                                  * yet ... then adjust the alloc size to be
2553                                  * bigger, and return 1 but with the empty skb.
2554                                  * This results in an empty message being RX'ed
2555                                  * in userspace, but that is ignored.
2556                                  *
2557                                  * We can then retry with the larger buffer.
2558                                  */
2559                                 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2560                                     !skb->len && !state->split &&
2561                                     cb->min_dump_alloc < 4096) {
2562                                         cb->min_dump_alloc = 4096;
2563                                         state->split_start = 0;
2564                                         rtnl_unlock();
2565                                         return 1;
2566                                 }
2567                                 idx--;
2568                                 break;
2569                         }
2570                 } while (state->split_start > 0);
2571                 break;
2572         }
2573         rtnl_unlock();
2574
2575         state->start = idx;
2576
2577         return skb->len;
2578 }
2579
2580 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2581 {
2582         kfree((void *)cb->args[0]);
2583         return 0;
2584 }
2585
2586 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2587 {
2588         struct sk_buff *msg;
2589         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2590         struct nl80211_dump_wiphy_state state = {};
2591
2592         msg = nlmsg_new(4096, GFP_KERNEL);
2593         if (!msg)
2594                 return -ENOMEM;
2595
2596         if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2597                                info->snd_portid, info->snd_seq, 0,
2598                                &state) < 0) {
2599                 nlmsg_free(msg);
2600                 return -ENOBUFS;
2601         }
2602
2603         return genlmsg_reply(msg, info);
2604 }
2605
2606 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2607         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
2608         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
2609         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
2610         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
2611         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
2612 };
2613
2614 static int parse_txq_params(struct nlattr *tb[],
2615                             struct ieee80211_txq_params *txq_params)
2616 {
2617         u8 ac;
2618
2619         if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2620             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2621             !tb[NL80211_TXQ_ATTR_AIFS])
2622                 return -EINVAL;
2623
2624         ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2625         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2626         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2627         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2628         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2629
2630         if (ac >= NL80211_NUM_ACS)
2631                 return -EINVAL;
2632         txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2633         return 0;
2634 }
2635
2636 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2637 {
2638         /*
2639          * You can only set the channel explicitly for WDS interfaces,
2640          * all others have their channel managed via their respective
2641          * "establish a connection" command (connect, join, ...)
2642          *
2643          * For AP/GO and mesh mode, the channel can be set with the
2644          * channel userspace API, but is only stored and passed to the
2645          * low-level driver when the AP starts or the mesh is joined.
2646          * This is for backward compatibility, userspace can also give
2647          * the channel in the start-ap or join-mesh commands instead.
2648          *
2649          * Monitors are special as they are normally slaved to
2650          * whatever else is going on, so they have their own special
2651          * operation to set the monitor channel if possible.
2652          */
2653         return !wdev ||
2654                 wdev->iftype == NL80211_IFTYPE_AP ||
2655                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2656                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
2657                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
2658 }
2659
2660 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2661                           struct genl_info *info,
2662                           struct cfg80211_chan_def *chandef)
2663 {
2664         struct netlink_ext_ack *extack = info->extack;
2665         struct nlattr **attrs = info->attrs;
2666         u32 control_freq;
2667
2668         if (!attrs[NL80211_ATTR_WIPHY_FREQ])
2669                 return -EINVAL;
2670
2671         control_freq = nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]);
2672
2673         memset(chandef, 0, sizeof(*chandef));
2674
2675         chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2676         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2677         chandef->center_freq1 = control_freq;
2678         chandef->center_freq2 = 0;
2679
2680         /* Primary channel not allowed */
2681         if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
2682                 NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
2683                                     "Channel is disabled");
2684                 return -EINVAL;
2685         }
2686
2687         if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2688                 enum nl80211_channel_type chantype;
2689
2690                 chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2691
2692                 switch (chantype) {
2693                 case NL80211_CHAN_NO_HT:
2694                 case NL80211_CHAN_HT20:
2695                 case NL80211_CHAN_HT40PLUS:
2696                 case NL80211_CHAN_HT40MINUS:
2697                         cfg80211_chandef_create(chandef, chandef->chan,
2698                                                 chantype);
2699                         /* user input for center_freq is incorrect */
2700                         if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
2701                             chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
2702                                 NL_SET_ERR_MSG_ATTR(extack,
2703                                                     attrs[NL80211_ATTR_CENTER_FREQ1],
2704                                                     "bad center frequency 1");
2705                                 return -EINVAL;
2706                         }
2707                         /* center_freq2 must be zero */
2708                         if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
2709                             nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
2710                                 NL_SET_ERR_MSG_ATTR(extack,
2711                                                     attrs[NL80211_ATTR_CENTER_FREQ2],
2712                                                     "center frequency 2 can't be used");
2713                                 return -EINVAL;
2714                         }
2715                         break;
2716                 default:
2717                         NL_SET_ERR_MSG_ATTR(extack,
2718                                             attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
2719                                             "invalid channel type");
2720                         return -EINVAL;
2721                 }
2722         } else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2723                 chandef->width =
2724                         nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2725                 if (attrs[NL80211_ATTR_CENTER_FREQ1])
2726                         chandef->center_freq1 =
2727                                 nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
2728                 if (attrs[NL80211_ATTR_CENTER_FREQ2])
2729                         chandef->center_freq2 =
2730                                 nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
2731         }
2732
2733         if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
2734                 chandef->edmg.channels =
2735                       nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
2736
2737                 if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
2738                         chandef->edmg.bw_config =
2739                      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
2740         } else {
2741                 chandef->edmg.bw_config = 0;
2742                 chandef->edmg.channels = 0;
2743         }
2744
2745         if (!cfg80211_chandef_valid(chandef)) {
2746                 NL_SET_ERR_MSG(extack, "invalid channel definition");
2747                 return -EINVAL;
2748         }
2749
2750         if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2751                                      IEEE80211_CHAN_DISABLED)) {
2752                 NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
2753                 return -EINVAL;
2754         }
2755
2756         if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2757              chandef->width == NL80211_CHAN_WIDTH_10) &&
2758             !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
2759                 NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
2760                 return -EINVAL;
2761         }
2762
2763         return 0;
2764 }
2765
2766 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2767                                  struct net_device *dev,
2768                                  struct genl_info *info)
2769 {
2770         struct cfg80211_chan_def chandef;
2771         int result;
2772         enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2773         struct wireless_dev *wdev = NULL;
2774
2775         if (dev)
2776                 wdev = dev->ieee80211_ptr;
2777         if (!nl80211_can_set_dev_channel(wdev))
2778                 return -EOPNOTSUPP;
2779         if (wdev)
2780                 iftype = wdev->iftype;
2781
2782         result = nl80211_parse_chandef(rdev, info, &chandef);
2783         if (result)
2784                 return result;
2785
2786         switch (iftype) {
2787         case NL80211_IFTYPE_AP:
2788         case NL80211_IFTYPE_P2P_GO:
2789                 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2790                                                    iftype)) {
2791                         result = -EINVAL;
2792                         break;
2793                 }
2794                 if (wdev->beacon_interval) {
2795                         if (!dev || !rdev->ops->set_ap_chanwidth ||
2796                             !(rdev->wiphy.features &
2797                               NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2798                                 result = -EBUSY;
2799                                 break;
2800                         }
2801
2802                         /* Only allow dynamic channel width changes */
2803                         if (chandef.chan != wdev->preset_chandef.chan) {
2804                                 result = -EBUSY;
2805                                 break;
2806                         }
2807                         result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2808                         if (result)
2809                                 break;
2810                 }
2811                 wdev->preset_chandef = chandef;
2812                 result = 0;
2813                 break;
2814         case NL80211_IFTYPE_MESH_POINT:
2815                 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2816                 break;
2817         case NL80211_IFTYPE_MONITOR:
2818                 result = cfg80211_set_monitor_channel(rdev, &chandef);
2819                 break;
2820         default:
2821                 result = -EINVAL;
2822         }
2823
2824         return result;
2825 }
2826
2827 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2828 {
2829         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2830         struct net_device *netdev = info->user_ptr[1];
2831
2832         return __nl80211_set_channel(rdev, netdev, info);
2833 }
2834
2835 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2836 {
2837         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2838         struct net_device *dev = info->user_ptr[1];
2839         struct wireless_dev *wdev = dev->ieee80211_ptr;
2840         const u8 *bssid;
2841
2842         if (!info->attrs[NL80211_ATTR_MAC])
2843                 return -EINVAL;
2844
2845         if (netif_running(dev))
2846                 return -EBUSY;
2847
2848         if (!rdev->ops->set_wds_peer)
2849                 return -EOPNOTSUPP;
2850
2851         if (wdev->iftype != NL80211_IFTYPE_WDS)
2852                 return -EOPNOTSUPP;
2853
2854         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2855         return rdev_set_wds_peer(rdev, dev, bssid);
2856 }
2857
2858 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2859 {
2860         struct cfg80211_registered_device *rdev;
2861         struct net_device *netdev = NULL;
2862         struct wireless_dev *wdev;
2863         int result = 0, rem_txq_params = 0;
2864         struct nlattr *nl_txq_params;
2865         u32 changed;
2866         u8 retry_short = 0, retry_long = 0;
2867         u32 frag_threshold = 0, rts_threshold = 0;
2868         u8 coverage_class = 0;
2869         u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
2870
2871         ASSERT_RTNL();
2872
2873         /*
2874          * Try to find the wiphy and netdev. Normally this
2875          * function shouldn't need the netdev, but this is
2876          * done for backward compatibility -- previously
2877          * setting the channel was done per wiphy, but now
2878          * it is per netdev. Previous userland like hostapd
2879          * also passed a netdev to set_wiphy, so that it is
2880          * possible to let that go to the right netdev!
2881          */
2882
2883         if (info->attrs[NL80211_ATTR_IFINDEX]) {
2884                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2885
2886                 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2887                 if (netdev && netdev->ieee80211_ptr)
2888                         rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2889                 else
2890                         netdev = NULL;
2891         }
2892
2893         if (!netdev) {
2894                 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2895                                                   info->attrs);
2896                 if (IS_ERR(rdev))
2897                         return PTR_ERR(rdev);
2898                 wdev = NULL;
2899                 netdev = NULL;
2900                 result = 0;
2901         } else
2902                 wdev = netdev->ieee80211_ptr;
2903
2904         /*
2905          * end workaround code, by now the rdev is available
2906          * and locked, and wdev may or may not be NULL.
2907          */
2908
2909         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2910                 result = cfg80211_dev_rename(
2911                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2912
2913         if (result)
2914                 return result;
2915
2916         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2917                 struct ieee80211_txq_params txq_params;
2918                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2919
2920                 if (!rdev->ops->set_txq_params)
2921                         return -EOPNOTSUPP;
2922
2923                 if (!netdev)
2924                         return -EINVAL;
2925
2926                 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2927                     netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2928                         return -EINVAL;
2929
2930                 if (!netif_running(netdev))
2931                         return -ENETDOWN;
2932
2933                 nla_for_each_nested(nl_txq_params,
2934                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2935                                     rem_txq_params) {
2936                         result = nla_parse_nested_deprecated(tb,
2937                                                              NL80211_TXQ_ATTR_MAX,
2938                                                              nl_txq_params,
2939                                                              txq_params_policy,
2940                                                              info->extack);
2941                         if (result)
2942                                 return result;
2943                         result = parse_txq_params(tb, &txq_params);
2944                         if (result)
2945                                 return result;
2946
2947                         result = rdev_set_txq_params(rdev, netdev,
2948                                                      &txq_params);
2949                         if (result)
2950                                 return result;
2951                 }
2952         }
2953
2954         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2955                 result = __nl80211_set_channel(
2956                         rdev,
2957                         nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2958                         info);
2959                 if (result)
2960                         return result;
2961         }
2962
2963         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2964                 struct wireless_dev *txp_wdev = wdev;
2965                 enum nl80211_tx_power_setting type;
2966                 int idx, mbm = 0;
2967
2968                 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2969                         txp_wdev = NULL;
2970
2971                 if (!rdev->ops->set_tx_power)
2972                         return -EOPNOTSUPP;
2973
2974                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2975                 type = nla_get_u32(info->attrs[idx]);
2976
2977                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2978                     (type != NL80211_TX_POWER_AUTOMATIC))
2979                         return -EINVAL;
2980
2981                 if (type != NL80211_TX_POWER_AUTOMATIC) {
2982                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2983                         mbm = nla_get_u32(info->attrs[idx]);
2984                 }
2985
2986                 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2987                 if (result)
2988                         return result;
2989         }
2990
2991         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2992             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2993                 u32 tx_ant, rx_ant;
2994
2995                 if ((!rdev->wiphy.available_antennas_tx &&
2996                      !rdev->wiphy.available_antennas_rx) ||
2997                     !rdev->ops->set_antenna)
2998                         return -EOPNOTSUPP;
2999
3000                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3001                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3002
3003                 /* reject antenna configurations which don't match the
3004                  * available antenna masks, except for the "all" mask */
3005                 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3006                     (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
3007                         return -EINVAL;
3008
3009                 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3010                 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3011
3012                 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3013                 if (result)
3014                         return result;
3015         }
3016
3017         changed = 0;
3018
3019         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3020                 retry_short = nla_get_u8(
3021                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3022
3023                 changed |= WIPHY_PARAM_RETRY_SHORT;
3024         }
3025
3026         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3027                 retry_long = nla_get_u8(
3028                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3029
3030                 changed |= WIPHY_PARAM_RETRY_LONG;
3031         }
3032
3033         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3034                 frag_threshold = nla_get_u32(
3035                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3036                 if (frag_threshold < 256)
3037                         return -EINVAL;
3038
3039                 if (frag_threshold != (u32) -1) {
3040                         /*
3041                          * Fragments (apart from the last one) are required to
3042                          * have even length. Make the fragmentation code
3043                          * simpler by stripping LSB should someone try to use
3044                          * odd threshold value.
3045                          */
3046                         frag_threshold &= ~0x1;
3047                 }
3048                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3049         }
3050
3051         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3052                 rts_threshold = nla_get_u32(
3053                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3054                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
3055         }
3056
3057         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3058                 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
3059                         return -EINVAL;
3060
3061                 coverage_class = nla_get_u8(
3062                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3063                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
3064         }
3065
3066         if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3067                 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
3068                         return -EOPNOTSUPP;
3069
3070                 changed |= WIPHY_PARAM_DYN_ACK;
3071         }
3072
3073         if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3074                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3075                                              NL80211_EXT_FEATURE_TXQS))
3076                         return -EOPNOTSUPP;
3077                 txq_limit = nla_get_u32(
3078                         info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3079                 changed |= WIPHY_PARAM_TXQ_LIMIT;
3080         }
3081
3082         if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3083                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3084                                              NL80211_EXT_FEATURE_TXQS))
3085                         return -EOPNOTSUPP;
3086                 txq_memory_limit = nla_get_u32(
3087                         info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3088                 changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3089         }
3090
3091         if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3092                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3093                                              NL80211_EXT_FEATURE_TXQS))
3094                         return -EOPNOTSUPP;
3095                 txq_quantum = nla_get_u32(
3096                         info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3097                 changed |= WIPHY_PARAM_TXQ_QUANTUM;
3098         }
3099
3100         if (changed) {
3101                 u8 old_retry_short, old_retry_long;
3102                 u32 old_frag_threshold, old_rts_threshold;
3103                 u8 old_coverage_class;
3104                 u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3105
3106                 if (!rdev->ops->set_wiphy_params)
3107                         return -EOPNOTSUPP;
3108
3109                 old_retry_short = rdev->wiphy.retry_short;
3110                 old_retry_long = rdev->wiphy.retry_long;
3111                 old_frag_threshold = rdev->wiphy.frag_threshold;
3112                 old_rts_threshold = rdev->wiphy.rts_threshold;
3113                 old_coverage_class = rdev->wiphy.coverage_class;
3114                 old_txq_limit = rdev->wiphy.txq_limit;
3115                 old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3116                 old_txq_quantum = rdev->wiphy.txq_quantum;
3117
3118                 if (changed & WIPHY_PARAM_RETRY_SHORT)
3119                         rdev->wiphy.retry_short = retry_short;
3120                 if (changed & WIPHY_PARAM_RETRY_LONG)
3121                         rdev->wiphy.retry_long = retry_long;
3122                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3123                         rdev->wiphy.frag_threshold = frag_threshold;
3124                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3125                         rdev->wiphy.rts_threshold = rts_threshold;
3126                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3127                         rdev->wiphy.coverage_class = coverage_class;
3128                 if (changed & WIPHY_PARAM_TXQ_LIMIT)
3129                         rdev->wiphy.txq_limit = txq_limit;
3130                 if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3131                         rdev->wiphy.txq_memory_limit = txq_memory_limit;
3132                 if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3133                         rdev->wiphy.txq_quantum = txq_quantum;
3134
3135                 result = rdev_set_wiphy_params(rdev, changed);
3136                 if (result) {
3137                         rdev->wiphy.retry_short = old_retry_short;
3138                         rdev->wiphy.retry_long = old_retry_long;
3139                         rdev->wiphy.frag_threshold = old_frag_threshold;
3140                         rdev->wiphy.rts_threshold = old_rts_threshold;
3141                         rdev->wiphy.coverage_class = old_coverage_class;
3142                         rdev->wiphy.txq_limit = old_txq_limit;
3143                         rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3144                         rdev->wiphy.txq_quantum = old_txq_quantum;
3145                         return result;
3146                 }
3147         }
3148         return 0;
3149 }
3150
3151 static int nl80211_send_chandef(struct sk_buff *msg,
3152                                 const struct cfg80211_chan_def *chandef)
3153 {
3154         if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3155                 return -EINVAL;
3156
3157         if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3158                         chandef->chan->center_freq))
3159                 return -ENOBUFS;
3160         switch (chandef->width) {
3161         case NL80211_CHAN_WIDTH_20_NOHT:
3162         case NL80211_CHAN_WIDTH_20:
3163         case NL80211_CHAN_WIDTH_40:
3164                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3165                                 cfg80211_get_chandef_type(chandef)))
3166                         return -ENOBUFS;
3167                 break;
3168         default:
3169                 break;
3170         }
3171         if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3172                 return -ENOBUFS;
3173         if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3174                 return -ENOBUFS;
3175         if (chandef->center_freq2 &&
3176             nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3177                 return -ENOBUFS;
3178         return 0;
3179 }
3180
3181 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3182                               struct cfg80211_registered_device *rdev,
3183                               struct wireless_dev *wdev,
3184                               enum nl80211_commands cmd)
3185 {
3186         struct net_device *dev = wdev->netdev;
3187         void *hdr;
3188
3189         WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3190                 cmd != NL80211_CMD_DEL_INTERFACE &&
3191                 cmd != NL80211_CMD_SET_INTERFACE);
3192
3193         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3194         if (!hdr)
3195                 return -1;
3196
3197         if (dev &&
3198             (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3199              nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3200                 goto nla_put_failure;
3201
3202         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3203             nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3204             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3205                               NL80211_ATTR_PAD) ||
3206             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3207             nla_put_u32(msg, NL80211_ATTR_GENERATION,
3208                         rdev->devlist_generation ^
3209                         (cfg80211_rdev_list_generation << 2)) ||
3210             nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3211                 goto nla_put_failure;
3212
3213         if (rdev->ops->get_channel) {
3214                 int ret;
3215                 struct cfg80211_chan_def chandef = {};
3216
3217                 ret = rdev_get_channel(rdev, wdev, &chandef);
3218                 if (ret == 0) {
3219                         if (nl80211_send_chandef(msg, &chandef))
3220                                 goto nla_put_failure;
3221                 }
3222         }
3223
3224         if (rdev->ops->get_tx_power) {
3225                 int dbm, ret;
3226
3227                 ret = rdev_get_tx_power(rdev, wdev, &dbm);
3228                 if (ret == 0 &&
3229                     nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3230                                 DBM_TO_MBM(dbm)))
3231                         goto nla_put_failure;
3232         }
3233
3234         wdev_lock(wdev);
3235         switch (wdev->iftype) {
3236         case NL80211_IFTYPE_AP:
3237                 if (wdev->ssid_len &&
3238                     nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
3239                         goto nla_put_failure_locked;
3240                 break;
3241         case NL80211_IFTYPE_STATION:
3242         case NL80211_IFTYPE_P2P_CLIENT:
3243         case NL80211_IFTYPE_ADHOC: {
3244                 const u8 *ssid_ie;
3245                 if (!wdev->current_bss)
3246                         break;
3247                 rcu_read_lock();
3248                 ssid_ie = ieee80211_bss_get_ie(&wdev->current_bss->pub,
3249                                                WLAN_EID_SSID);
3250                 if (ssid_ie &&
3251                     nla_put(msg, NL80211_ATTR_SSID, ssid_ie[1], ssid_ie + 2))
3252                         goto nla_put_failure_rcu_locked;
3253                 rcu_read_unlock();
3254                 break;
3255                 }
3256         default:
3257                 /* nothing */
3258                 break;
3259         }
3260         wdev_unlock(wdev);
3261
3262         if (rdev->ops->get_txq_stats) {
3263                 struct cfg80211_txq_stats txqstats = {};
3264                 int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3265
3266                 if (ret == 0 &&
3267                     !nl80211_put_txq_stats(msg, &txqstats,
3268                                            NL80211_ATTR_TXQ_STATS))
3269                         goto nla_put_failure;
3270         }
3271
3272         genlmsg_end(msg, hdr);
3273         return 0;
3274
3275  nla_put_failure_rcu_locked:
3276         rcu_read_unlock();
3277  nla_put_failure_locked:
3278         wdev_unlock(wdev);
3279  nla_put_failure:
3280         genlmsg_cancel(msg, hdr);
3281         return -EMSGSIZE;
3282 }
3283
3284 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3285 {
3286         int wp_idx = 0;
3287         int if_idx = 0;
3288         int wp_start = cb->args[0];
3289         int if_start = cb->args[1];
3290         int filter_wiphy = -1;
3291         struct cfg80211_registered_device *rdev;
3292         struct wireless_dev *wdev;
3293         int ret;
3294
3295         rtnl_lock();
3296         if (!cb->args[2]) {
3297                 struct nl80211_dump_wiphy_state state = {
3298                         .filter_wiphy = -1,
3299                 };
3300
3301                 ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3302                 if (ret)
3303                         goto out_unlock;
3304
3305                 filter_wiphy = state.filter_wiphy;
3306
3307                 /*
3308                  * if filtering, set cb->args[2] to +1 since 0 is the default
3309                  * value needed to determine that parsing is necessary.
3310                  */
3311                 if (filter_wiphy >= 0)
3312                         cb->args[2] = filter_wiphy + 1;
3313                 else
3314                         cb->args[2] = -1;
3315         } else if (cb->args[2] > 0) {
3316                 filter_wiphy = cb->args[2] - 1;
3317         }
3318
3319         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3320                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3321                         continue;
3322                 if (wp_idx < wp_start) {
3323                         wp_idx++;
3324                         continue;
3325                 }
3326
3327                 if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3328                         continue;
3329
3330                 if_idx = 0;
3331
3332                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3333                         if (if_idx < if_start) {
3334                                 if_idx++;
3335                                 continue;
3336                         }
3337                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3338                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
3339                                                rdev, wdev,
3340                                                NL80211_CMD_NEW_INTERFACE) < 0) {
3341                                 goto out;
3342                         }
3343                         if_idx++;
3344                 }
3345
3346                 wp_idx++;
3347         }
3348  out:
3349         cb->args[0] = wp_idx;
3350         cb->args[1] = if_idx;
3351
3352         ret = skb->len;
3353  out_unlock:
3354         rtnl_unlock();
3355
3356         return ret;
3357 }
3358
3359 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3360 {
3361         struct sk_buff *msg;
3362         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3363         struct wireless_dev *wdev = info->user_ptr[1];
3364
3365         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3366         if (!msg)
3367                 return -ENOMEM;
3368
3369         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3370                                rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3371                 nlmsg_free(msg);
3372                 return -ENOBUFS;
3373         }
3374
3375         return genlmsg_reply(msg, info);
3376 }
3377
3378 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3379         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3380         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3381         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3382         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3383         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
3384         [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
3385 };
3386
3387 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
3388 {
3389         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
3390         int flag;
3391
3392         *mntrflags = 0;
3393
3394         if (!nla)
3395                 return -EINVAL;
3396
3397         if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
3398                 return -EINVAL;
3399
3400         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
3401                 if (flags[flag])
3402                         *mntrflags |= (1<<flag);
3403
3404         *mntrflags |= MONITOR_FLAG_CHANGED;
3405
3406         return 0;
3407 }
3408
3409 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
3410                                      enum nl80211_iftype type,
3411                                      struct genl_info *info,
3412                                      struct vif_params *params)
3413 {
3414         bool change = false;
3415         int err;
3416
3417         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
3418                 if (type != NL80211_IFTYPE_MONITOR)
3419                         return -EINVAL;
3420
3421                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
3422                                           &params->flags);
3423                 if (err)
3424                         return err;
3425
3426                 change = true;
3427         }
3428
3429         if (params->flags & MONITOR_FLAG_ACTIVE &&
3430             !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
3431                 return -EOPNOTSUPP;
3432
3433         if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
3434                 const u8 *mumimo_groups;
3435                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3436
3437                 if (type != NL80211_IFTYPE_MONITOR)
3438                         return -EINVAL;
3439
3440                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3441                         return -EOPNOTSUPP;
3442
3443                 mumimo_groups =
3444                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
3445
3446                 /* bits 0 and 63 are reserved and must be zero */
3447                 if ((mumimo_groups[0] & BIT(0)) ||
3448                     (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
3449                         return -EINVAL;
3450
3451                 params->vht_mumimo_groups = mumimo_groups;
3452                 change = true;
3453         }
3454
3455         if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
3456                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
3457
3458                 if (type != NL80211_IFTYPE_MONITOR)
3459                         return -EINVAL;
3460
3461                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
3462                         return -EOPNOTSUPP;
3463
3464                 params->vht_mumimo_follow_addr =
3465                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
3466                 change = true;
3467         }
3468
3469         return change ? 1 : 0;
3470 }
3471
3472 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
3473                                struct net_device *netdev, u8 use_4addr,
3474                                enum nl80211_iftype iftype)
3475 {
3476         if (!use_4addr) {
3477                 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
3478                         return -EBUSY;
3479                 return 0;
3480         }
3481
3482         switch (iftype) {
3483         case NL80211_IFTYPE_AP_VLAN:
3484                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
3485                         return 0;
3486                 break;
3487         case NL80211_IFTYPE_STATION:
3488                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
3489                         return 0;
3490                 break;
3491         default:
3492                 break;
3493         }
3494
3495         return -EOPNOTSUPP;
3496 }
3497
3498 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
3499 {
3500         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3501         struct vif_params params;
3502         int err;
3503         enum nl80211_iftype otype, ntype;
3504         struct net_device *dev = info->user_ptr[1];
3505         bool change = false;
3506
3507         memset(&params, 0, sizeof(params));
3508
3509         otype = ntype = dev->ieee80211_ptr->iftype;
3510
3511         if (info->attrs[NL80211_ATTR_IFTYPE]) {
3512                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3513                 if (otype != ntype)
3514                         change = true;
3515         }
3516
3517         if (info->attrs[NL80211_ATTR_MESH_ID]) {
3518                 struct wireless_dev *wdev = dev->ieee80211_ptr;
3519
3520                 if (ntype != NL80211_IFTYPE_MESH_POINT)
3521                         return -EINVAL;
3522                 if (netif_running(dev))
3523                         return -EBUSY;
3524
3525                 wdev_lock(wdev);
3526                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3527                              IEEE80211_MAX_MESH_ID_LEN);
3528                 wdev->mesh_id_up_len =
3529                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3530                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3531                        wdev->mesh_id_up_len);
3532                 wdev_unlock(wdev);
3533         }
3534
3535         if (info->attrs[NL80211_ATTR_4ADDR]) {
3536                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3537                 change = true;
3538                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
3539                 if (err)
3540                         return err;
3541         } else {
3542                 params.use_4addr = -1;
3543         }
3544
3545         err = nl80211_parse_mon_options(rdev, ntype, info, &params);
3546         if (err < 0)
3547                 return err;
3548         if (err > 0)
3549                 change = true;
3550
3551         if (change)
3552                 err = cfg80211_change_iface(rdev, dev, ntype, &params);
3553         else
3554                 err = 0;
3555
3556         if (!err && params.use_4addr != -1)
3557                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
3558
3559         if (change && !err) {
3560                 struct wireless_dev *wdev = dev->ieee80211_ptr;
3561
3562                 nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
3563         }
3564
3565         return err;
3566 }
3567
3568 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
3569 {
3570         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3571         struct vif_params params;
3572         struct wireless_dev *wdev;
3573         struct sk_buff *msg;
3574         int err;
3575         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
3576
3577         /* to avoid failing a new interface creation due to pending removal */
3578         cfg80211_destroy_ifaces(rdev);
3579
3580         memset(&params, 0, sizeof(params));
3581
3582         if (!info->attrs[NL80211_ATTR_IFNAME])
3583                 return -EINVAL;
3584
3585         if (info->attrs[NL80211_ATTR_IFTYPE])
3586                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
3587
3588         if (!rdev->ops->add_virtual_intf)
3589                 return -EOPNOTSUPP;
3590
3591         if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
3592              rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
3593             info->attrs[NL80211_ATTR_MAC]) {
3594                 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
3595                            ETH_ALEN);
3596                 if (!is_valid_ether_addr(params.macaddr))
3597                         return -EADDRNOTAVAIL;
3598         }
3599
3600         if (info->attrs[NL80211_ATTR_4ADDR]) {
3601                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
3602                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
3603                 if (err)
3604                         return err;
3605         }
3606
3607         if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
3608                 return -EOPNOTSUPP;
3609
3610         err = nl80211_parse_mon_options(rdev, type, info, &params);
3611         if (err < 0)
3612                 return err;
3613
3614         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3615         if (!msg)
3616                 return -ENOMEM;
3617
3618         wdev = rdev_add_virtual_intf(rdev,
3619                                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
3620                                 NET_NAME_USER, type, &params);
3621         if (WARN_ON(!wdev)) {
3622                 nlmsg_free(msg);
3623                 return -EPROTO;
3624         } else if (IS_ERR(wdev)) {
3625                 nlmsg_free(msg);
3626                 return PTR_ERR(wdev);
3627         }
3628
3629         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3630                 wdev->owner_nlportid = info->snd_portid;
3631
3632         switch (type) {
3633         case NL80211_IFTYPE_MESH_POINT:
3634                 if (!info->attrs[NL80211_ATTR_MESH_ID])
3635                         break;
3636                 wdev_lock(wdev);
3637                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3638                              IEEE80211_MAX_MESH_ID_LEN);
3639                 wdev->mesh_id_up_len =
3640                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3641                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3642                        wdev->mesh_id_up_len);
3643                 wdev_unlock(wdev);
3644                 break;
3645         case NL80211_IFTYPE_NAN:
3646         case NL80211_IFTYPE_P2P_DEVICE:
3647                 /*
3648                  * P2P Device and NAN do not have a netdev, so don't go
3649                  * through the netdev notifier and must be added here
3650                  */
3651                 cfg80211_init_wdev(rdev, wdev);
3652                 break;
3653         default:
3654                 break;
3655         }
3656
3657         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3658                                rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3659                 nlmsg_free(msg);
3660                 return -ENOBUFS;
3661         }
3662
3663         return genlmsg_reply(msg, info);
3664 }
3665
3666 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3667 {
3668         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3669         struct wireless_dev *wdev = info->user_ptr[1];
3670
3671         if (!rdev->ops->del_virtual_intf)
3672                 return -EOPNOTSUPP;
3673
3674         /*
3675          * If we remove a wireless device without a netdev then clear
3676          * user_ptr[1] so that nl80211_post_doit won't dereference it
3677          * to check if it needs to do dev_put(). Otherwise it crashes
3678          * since the wdev has been freed, unlike with a netdev where
3679          * we need the dev_put() for the netdev to really be freed.
3680          */
3681         if (!wdev->netdev)
3682                 info->user_ptr[1] = NULL;
3683
3684         return rdev_del_virtual_intf(rdev, wdev);
3685 }
3686
3687 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3688 {
3689         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3690         struct net_device *dev = info->user_ptr[1];
3691         u16 noack_map;
3692
3693         if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3694                 return -EINVAL;
3695
3696         if (!rdev->ops->set_noack_map)
3697                 return -EOPNOTSUPP;
3698
3699         noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3700
3701         return rdev_set_noack_map(rdev, dev, noack_map);
3702 }
3703
3704 struct get_key_cookie {
3705         struct sk_buff *msg;
3706         int error;
3707         int idx;
3708 };
3709
3710 static void get_key_callback(void *c, struct key_params *params)
3711 {
3712         struct nlattr *key;
3713         struct get_key_cookie *cookie = c;
3714
3715         if ((params->key &&
3716              nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3717                      params->key_len, params->key)) ||
3718             (params->seq &&
3719              nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3720                      params->seq_len, params->seq)) ||
3721             (params->cipher &&
3722              nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3723                          params->cipher)))
3724                 goto nla_put_failure;
3725
3726         key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
3727         if (!key)
3728                 goto nla_put_failure;
3729
3730         if ((params->key &&
3731              nla_put(cookie->msg, NL80211_KEY_DATA,
3732                      params->key_len, params->key)) ||
3733             (params->seq &&
3734              nla_put(cookie->msg, NL80211_KEY_SEQ,
3735                      params->seq_len, params->seq)) ||
3736             (params->cipher &&
3737              nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3738                          params->cipher)))
3739                 goto nla_put_failure;
3740
3741         if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3742                 goto nla_put_failure;
3743
3744         nla_nest_end(cookie->msg, key);
3745
3746         return;
3747  nla_put_failure:
3748         cookie->error = 1;
3749 }
3750
3751 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3752 {
3753         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3754         int err;
3755         struct net_device *dev = info->user_ptr[1];
3756         u8 key_idx = 0;
3757         const u8 *mac_addr = NULL;
3758         bool pairwise;
3759         struct get_key_cookie cookie = {
3760                 .error = 0,
3761         };
3762         void *hdr;
3763         struct sk_buff *msg;
3764
3765         if (info->attrs[NL80211_ATTR_KEY_IDX])
3766                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3767
3768         if (info->attrs[NL80211_ATTR_MAC])
3769                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3770
3771         pairwise = !!mac_addr;
3772         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3773                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3774
3775                 if (kt != NL80211_KEYTYPE_GROUP &&
3776                     kt != NL80211_KEYTYPE_PAIRWISE)
3777                         return -EINVAL;
3778                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3779         }
3780
3781         if (!rdev->ops->get_key)
3782                 return -EOPNOTSUPP;
3783
3784         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3785                 return -ENOENT;
3786
3787         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3788         if (!msg)
3789                 return -ENOMEM;
3790
3791         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3792                              NL80211_CMD_NEW_KEY);
3793         if (!hdr)
3794                 goto nla_put_failure;
3795
3796         cookie.msg = msg;
3797         cookie.idx = key_idx;
3798
3799         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3800             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3801                 goto nla_put_failure;
3802         if (mac_addr &&
3803             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3804                 goto nla_put_failure;
3805
3806         err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3807                            get_key_callback);
3808
3809         if (err)
3810                 goto free_msg;
3811
3812         if (cookie.error)
3813                 goto nla_put_failure;
3814
3815         genlmsg_end(msg, hdr);
3816         return genlmsg_reply(msg, info);
3817
3818  nla_put_failure:
3819         err = -ENOBUFS;
3820  free_msg:
3821         nlmsg_free(msg);
3822         return err;
3823 }
3824
3825 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3826 {
3827         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3828         struct key_parse key;
3829         int err;
3830         struct net_device *dev = info->user_ptr[1];
3831
3832         err = nl80211_parse_key(info, &key);
3833         if (err)
3834                 return err;
3835
3836         if (key.idx < 0)
3837                 return -EINVAL;
3838
3839         /* Only support setting default key and
3840          * Extended Key ID action NL80211_KEY_SET_TX.
3841          */
3842         if (!key.def && !key.defmgmt &&
3843             !(key.p.mode == NL80211_KEY_SET_TX))
3844                 return -EINVAL;
3845
3846         wdev_lock(dev->ieee80211_ptr);
3847
3848         if (key.def) {
3849                 if (!rdev->ops->set_default_key) {
3850                         err = -EOPNOTSUPP;
3851                         goto out;
3852                 }
3853
3854                 err = nl80211_key_allowed(dev->ieee80211_ptr);
3855                 if (err)
3856                         goto out;
3857
3858                 err = rdev_set_default_key(rdev, dev, key.idx,
3859                                                  key.def_uni, key.def_multi);
3860
3861                 if (err)
3862                         goto out;
3863
3864 #ifdef CONFIG_CFG80211_WEXT
3865                 dev->ieee80211_ptr->wext.default_key = key.idx;
3866 #endif
3867         } else if (key.defmgmt) {
3868                 if (key.def_uni || !key.def_multi) {
3869                         err = -EINVAL;
3870                         goto out;
3871                 }
3872
3873                 if (!rdev->ops->set_default_mgmt_key) {
3874                         err = -EOPNOTSUPP;
3875                         goto out;
3876                 }
3877
3878                 err = nl80211_key_allowed(dev->ieee80211_ptr);
3879                 if (err)
3880                         goto out;
3881
3882                 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3883                 if (err)
3884                         goto out;
3885
3886 #ifdef CONFIG_CFG80211_WEXT
3887                 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3888 #endif
3889         } else if (key.p.mode == NL80211_KEY_SET_TX &&
3890                    wiphy_ext_feature_isset(&rdev->wiphy,
3891                                            NL80211_EXT_FEATURE_EXT_KEY_ID)) {
3892                 u8 *mac_addr = NULL;
3893
3894                 if (info->attrs[NL80211_ATTR_MAC])
3895                         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3896
3897                 if (!mac_addr || key.idx < 0 || key.idx > 1) {
3898                         err = -EINVAL;
3899                         goto out;
3900                 }
3901
3902                 err = rdev_add_key(rdev, dev, key.idx,
3903                                    NL80211_KEYTYPE_PAIRWISE,
3904                                    mac_addr, &key.p);
3905         } else {
3906                 err = -EINVAL;
3907         }
3908  out:
3909         wdev_unlock(dev->ieee80211_ptr);
3910
3911         return err;
3912 }
3913
3914 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3915 {
3916         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3917         int err;
3918         struct net_device *dev = info->user_ptr[1];
3919         struct key_parse key;
3920         const u8 *mac_addr = NULL;
3921
3922         err = nl80211_parse_key(info, &key);
3923         if (err)
3924                 return err;
3925
3926         if (!key.p.key)
3927                 return -EINVAL;
3928
3929         if (info->attrs[NL80211_ATTR_MAC])
3930                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3931
3932         if (key.type == -1) {
3933                 if (mac_addr)
3934                         key.type = NL80211_KEYTYPE_PAIRWISE;
3935                 else
3936                         key.type = NL80211_KEYTYPE_GROUP;
3937         }
3938
3939         /* for now */
3940         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3941             key.type != NL80211_KEYTYPE_GROUP)
3942                 return -EINVAL;
3943
3944         if (key.type == NL80211_KEYTYPE_GROUP &&
3945             info->attrs[NL80211_ATTR_VLAN_ID])
3946                 key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
3947
3948         if (!rdev->ops->add_key)
3949                 return -EOPNOTSUPP;
3950
3951         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3952                                            key.type == NL80211_KEYTYPE_PAIRWISE,
3953                                            mac_addr))
3954                 return -EINVAL;
3955
3956         wdev_lock(dev->ieee80211_ptr);
3957         err = nl80211_key_allowed(dev->ieee80211_ptr);
3958         if (!err)
3959                 err = rdev_add_key(rdev, dev, key.idx,
3960                                    key.type == NL80211_KEYTYPE_PAIRWISE,
3961                                     mac_addr, &key.p);
3962         wdev_unlock(dev->ieee80211_ptr);
3963
3964         return err;
3965 }
3966
3967 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3968 {
3969         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3970         int err;
3971         struct net_device *dev = info->user_ptr[1];
3972         u8 *mac_addr = NULL;
3973         struct key_parse key;
3974
3975         err = nl80211_parse_key(info, &key);
3976         if (err)
3977                 return err;
3978
3979         if (info->attrs[NL80211_ATTR_MAC])
3980                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3981
3982         if (key.type == -1) {
3983                 if (mac_addr)
3984                         key.type = NL80211_KEYTYPE_PAIRWISE;
3985                 else
3986                         key.type = NL80211_KEYTYPE_GROUP;
3987         }
3988
3989         /* for now */
3990         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3991             key.type != NL80211_KEYTYPE_GROUP)
3992                 return -EINVAL;
3993
3994         if (!rdev->ops->del_key)
3995                 return -EOPNOTSUPP;
3996
3997         wdev_lock(dev->ieee80211_ptr);
3998         err = nl80211_key_allowed(dev->ieee80211_ptr);
3999
4000         if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4001             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4002                 err = -ENOENT;
4003
4004         if (!err)
4005                 err = rdev_del_key(rdev, dev, key.idx,
4006                                    key.type == NL80211_KEYTYPE_PAIRWISE,
4007                                    mac_addr);
4008
4009 #ifdef CONFIG_CFG80211_WEXT
4010         if (!err) {
4011                 if (key.idx == dev->ieee80211_ptr->wext.default_key)
4012                         dev->ieee80211_ptr->wext.default_key = -1;
4013                 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
4014                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
4015         }
4016 #endif
4017         wdev_unlock(dev->ieee80211_ptr);
4018
4019         return err;
4020 }
4021
4022 /* This function returns an error or the number of nested attributes */
4023 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4024 {
4025         struct nlattr *attr;
4026         int n_entries = 0, tmp;
4027
4028         nla_for_each_nested(attr, nl_attr, tmp) {
4029                 if (nla_len(attr) != ETH_ALEN)
4030                         return -EINVAL;
4031
4032                 n_entries++;
4033         }
4034
4035         return n_entries;
4036 }
4037
4038 /*
4039  * This function parses ACL information and allocates memory for ACL data.
4040  * On successful return, the calling function is responsible to free the
4041  * ACL buffer returned by this function.
4042  */
4043 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4044                                                 struct genl_info *info)
4045 {
4046         enum nl80211_acl_policy acl_policy;
4047         struct nlattr *attr;
4048         struct cfg80211_acl_data *acl;
4049         int i = 0, n_entries, tmp;
4050
4051         if (!wiphy->max_acl_mac_addrs)
4052                 return ERR_PTR(-EOPNOTSUPP);
4053
4054         if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4055                 return ERR_PTR(-EINVAL);
4056
4057         acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4058         if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4059             acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4060                 return ERR_PTR(-EINVAL);
4061
4062         if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4063                 return ERR_PTR(-EINVAL);
4064
4065         n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4066         if (n_entries < 0)
4067                 return ERR_PTR(n_entries);
4068
4069         if (n_entries > wiphy->max_acl_mac_addrs)
4070                 return ERR_PTR(-ENOTSUPP);
4071
4072         acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4073         if (!acl)
4074                 return ERR_PTR(-ENOMEM);
4075
4076         nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4077                 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4078                 i++;
4079         }
4080
4081         acl->n_acl_entries = n_entries;
4082         acl->acl_policy = acl_policy;
4083
4084         return acl;
4085 }
4086
4087 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4088 {
4089         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4090         struct net_device *dev = info->user_ptr[1];
4091         struct cfg80211_acl_data *acl;
4092         int err;
4093
4094         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4095             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4096                 return -EOPNOTSUPP;
4097
4098         if (!dev->ieee80211_ptr->beacon_interval)
4099                 return -EINVAL;
4100
4101         acl = parse_acl_data(&rdev->wiphy, info);
4102         if (IS_ERR(acl))
4103                 return PTR_ERR(acl);
4104
4105         err = rdev_set_mac_acl(rdev, dev, acl);
4106
4107         kfree(acl);
4108
4109         return err;
4110 }
4111
4112 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4113                            u8 *rates, u8 rates_len)
4114 {
4115         u8 i;
4116         u32 mask = 0;
4117
4118         for (i = 0; i < rates_len; i++) {
4119                 int rate = (rates[i] & 0x7f) * 5;
4120                 int ridx;
4121
4122                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4123                         struct ieee80211_rate *srate =
4124                                 &sband->bitrates[ridx];
4125                         if (rate == srate->bitrate) {
4126                                 mask |= 1 << ridx;
4127                                 break;
4128                         }
4129                 }
4130                 if (ridx == sband->n_bitrates)
4131                         return 0; /* rate not found */
4132         }
4133
4134         return mask;
4135 }
4136
4137 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4138                                u8 *rates, u8 rates_len,
4139                                u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4140 {
4141         u8 i;
4142
4143         memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4144
4145         for (i = 0; i < rates_len; i++) {
4146                 int ridx, rbit;
4147
4148                 ridx = rates[i] / 8;
4149                 rbit = BIT(rates[i] % 8);
4150
4151                 /* check validity */
4152                 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4153                         return false;
4154
4155                 /* check availability */
4156                 ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4157                 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4158                         mcs[ridx] |= rbit;
4159                 else
4160                         return false;
4161         }
4162
4163         return true;
4164 }
4165
4166 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4167 {
4168         u16 mcs_mask = 0;
4169
4170         switch (vht_mcs_map) {
4171         case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4172                 break;
4173         case IEEE80211_VHT_MCS_SUPPORT_0_7:
4174                 mcs_mask = 0x00FF;
4175                 break;
4176         case IEEE80211_VHT_MCS_SUPPORT_0_8:
4177                 mcs_mask = 0x01FF;
4178                 break;
4179         case IEEE80211_VHT_MCS_SUPPORT_0_9:
4180                 mcs_mask = 0x03FF;
4181                 break;
4182         default:
4183                 break;
4184         }
4185
4186         return mcs_mask;
4187 }
4188
4189 static void vht_build_mcs_mask(u16 vht_mcs_map,
4190                                u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4191 {
4192         u8 nss;
4193
4194         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4195                 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4196                 vht_mcs_map >>= 2;
4197         }
4198 }
4199
4200 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4201                              struct nl80211_txrate_vht *txrate,
4202                              u16 mcs[NL80211_VHT_NSS_MAX])
4203 {
4204         u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4205         u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4206         u8 i;
4207
4208         if (!sband->vht_cap.vht_supported)
4209                 return false;
4210
4211         memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4212
4213         /* Build vht_mcs_mask from VHT capabilities */
4214         vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4215
4216         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4217                 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4218                         mcs[i] = txrate->mcs[i];
4219                 else
4220                         return false;
4221         }
4222
4223         return true;
4224 }
4225
4226 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
4227         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4228                                     .len = NL80211_MAX_SUPP_RATES },
4229         [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
4230                                 .len = NL80211_MAX_SUPP_HT_RATES },
4231         [NL80211_TXRATE_VHT] = {
4232                 .type = NLA_EXACT_LEN_WARN,
4233                 .len = sizeof(struct nl80211_txrate_vht),
4234         },
4235         [NL80211_TXRATE_GI] = { .type = NLA_U8 },
4236 };
4237
4238 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
4239                                          struct cfg80211_bitrate_mask *mask)
4240 {
4241         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
4242         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4243         int rem, i;
4244         struct nlattr *tx_rates;
4245         struct ieee80211_supported_band *sband;
4246         u16 vht_tx_mcs_map;
4247
4248         memset(mask, 0, sizeof(*mask));
4249         /* Default to all rates enabled */
4250         for (i = 0; i < NUM_NL80211_BANDS; i++) {
4251                 sband = rdev->wiphy.bands[i];
4252
4253                 if (!sband)
4254                         continue;
4255
4256                 mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
4257                 memcpy(mask->control[i].ht_mcs,
4258                        sband->ht_cap.mcs.rx_mask,
4259                        sizeof(mask->control[i].ht_mcs));
4260
4261                 if (!sband->vht_cap.vht_supported)
4262                         continue;
4263
4264                 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4265                 vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
4266         }
4267
4268         /* if no rates are given set it back to the defaults */
4269         if (!info->attrs[NL80211_ATTR_TX_RATES])
4270                 goto out;
4271
4272         /* The nested attribute uses enum nl80211_band as the index. This maps
4273          * directly to the enum nl80211_band values used in cfg80211.
4274          */
4275         BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
4276         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
4277                 enum nl80211_band band = nla_type(tx_rates);
4278                 int err;
4279
4280                 if (band < 0 || band >= NUM_NL80211_BANDS)
4281                         return -EINVAL;
4282                 sband = rdev->wiphy.bands[band];
4283                 if (sband == NULL)
4284                         return -EINVAL;
4285                 err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
4286                                                   tx_rates,
4287                                                   nl80211_txattr_policy,
4288                                                   info->extack);
4289                 if (err)
4290                         return err;
4291                 if (tb[NL80211_TXRATE_LEGACY]) {
4292                         mask->control[band].legacy = rateset_to_mask(
4293                                 sband,
4294                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
4295                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
4296                         if ((mask->control[band].legacy == 0) &&
4297                             nla_len(tb[NL80211_TXRATE_LEGACY]))
4298                                 return -EINVAL;
4299                 }
4300                 if (tb[NL80211_TXRATE_HT]) {
4301                         if (!ht_rateset_to_mask(
4302                                         sband,
4303                                         nla_data(tb[NL80211_TXRATE_HT]),
4304                                         nla_len(tb[NL80211_TXRATE_HT]),
4305                                         mask->control[band].ht_mcs))
4306                                 return -EINVAL;
4307                 }
4308                 if (tb[NL80211_TXRATE_VHT]) {
4309                         if (!vht_set_mcs_mask(
4310                                         sband,
4311                                         nla_data(tb[NL80211_TXRATE_VHT]),
4312                                         mask->control[band].vht_mcs))
4313                                 return -EINVAL;
4314                 }
4315                 if (tb[NL80211_TXRATE_GI]) {
4316                         mask->control[band].gi =
4317                                 nla_get_u8(tb[NL80211_TXRATE_GI]);
4318                         if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
4319                                 return -EINVAL;
4320                 }
4321
4322                 if (mask->control[band].legacy == 0) {
4323                         /* don't allow empty legacy rates if HT or VHT
4324                          * are not even supported.
4325                          */
4326                         if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
4327                               rdev->wiphy.bands[band]->vht_cap.vht_supported))
4328                                 return -EINVAL;
4329
4330                         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4331                                 if (mask->control[band].ht_mcs[i])
4332                                         goto out;
4333
4334                         for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4335                                 if (mask->control[band].vht_mcs[i])
4336                                         goto out;
4337
4338                         /* legacy and mcs rates may not be both empty */
4339                         return -EINVAL;
4340                 }
4341         }
4342
4343 out:
4344         return 0;
4345 }
4346
4347 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
4348                                    enum nl80211_band band,
4349                                    struct cfg80211_bitrate_mask *beacon_rate)
4350 {
4351         u32 count_ht, count_vht, i;
4352         u32 rate = beacon_rate->control[band].legacy;
4353
4354         /* Allow only one rate */
4355         if (hweight32(rate) > 1)
4356                 return -EINVAL;
4357
4358         count_ht = 0;
4359         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4360                 if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
4361                         return -EINVAL;
4362                 } else if (beacon_rate->control[band].ht_mcs[i]) {
4363                         count_ht++;
4364                         if (count_ht > 1)
4365                                 return -EINVAL;
4366                 }
4367                 if (count_ht && rate)
4368                         return -EINVAL;
4369         }
4370
4371         count_vht = 0;
4372         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4373                 if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
4374                         return -EINVAL;
4375                 } else if (beacon_rate->control[band].vht_mcs[i]) {
4376                         count_vht++;
4377                         if (count_vht > 1)
4378                                 return -EINVAL;
4379                 }
4380                 if (count_vht && rate)
4381                         return -EINVAL;
4382         }
4383
4384         if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
4385                 return -EINVAL;
4386
4387         if (rate &&
4388             !wiphy_ext_feature_isset(&rdev->wiphy,
4389                                      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
4390                 return -EINVAL;
4391         if (count_ht &&
4392             !wiphy_ext_feature_isset(&rdev->wiphy,
4393                                      NL80211_EXT_FEATURE_BEACON_RATE_HT))
4394                 return -EINVAL;
4395         if (count_vht &&
4396             !wiphy_ext_feature_isset(&rdev->wiphy,
4397                                      NL80211_EXT_FEATURE_BEACON_RATE_VHT))
4398                 return -EINVAL;
4399
4400         return 0;
4401 }
4402
4403 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
4404                                 struct nlattr *attrs[],
4405                                 struct cfg80211_beacon_data *bcn)
4406 {
4407         bool haveinfo = false;
4408         int err;
4409
4410         memset(bcn, 0, sizeof(*bcn));
4411
4412         if (attrs[NL80211_ATTR_BEACON_HEAD]) {
4413                 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
4414                 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
4415                 if (!bcn->head_len)
4416                         return -EINVAL;
4417                 haveinfo = true;
4418         }
4419
4420         if (attrs[NL80211_ATTR_BEACON_TAIL]) {
4421                 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
4422                 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
4423                 haveinfo = true;
4424         }
4425
4426         if (!haveinfo)
4427                 return -EINVAL;
4428
4429         if (attrs[NL80211_ATTR_IE]) {
4430                 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
4431                 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
4432         }
4433
4434         if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
4435                 bcn->proberesp_ies =
4436                         nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4437                 bcn->proberesp_ies_len =
4438                         nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
4439         }
4440
4441         if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
4442                 bcn->assocresp_ies =
4443                         nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4444                 bcn->assocresp_ies_len =
4445                         nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
4446         }
4447
4448         if (attrs[NL80211_ATTR_PROBE_RESP]) {
4449                 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
4450                 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
4451         }
4452
4453         if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
4454                 struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
4455
4456                 err = nla_parse_nested_deprecated(tb,
4457                                                   NL80211_FTM_RESP_ATTR_MAX,
4458                                                   attrs[NL80211_ATTR_FTM_RESPONDER],
4459                                                   NULL, NULL);
4460                 if (err)
4461                         return err;
4462
4463                 if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
4464                     wiphy_ext_feature_isset(&rdev->wiphy,
4465                                             NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
4466                         bcn->ftm_responder = 1;
4467                 else
4468                         return -EOPNOTSUPP;
4469
4470                 if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
4471                         bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
4472                         bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
4473                 }
4474
4475                 if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
4476                         bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4477                         bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
4478                 }
4479         } else {
4480                 bcn->ftm_responder = -1;
4481         }
4482
4483         return 0;
4484 }
4485
4486 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
4487                                     struct ieee80211_he_obss_pd *he_obss_pd)
4488 {
4489         struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
4490         int err;
4491
4492         err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
4493                                he_obss_pd_policy, NULL);
4494         if (err)
4495                 return err;
4496
4497         if (!tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] ||
4498             !tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
4499                 return -EINVAL;
4500
4501         he_obss_pd->min_offset =
4502                 nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
4503         he_obss_pd->max_offset =
4504                 nla_get_u32(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
4505
4506         if (he_obss_pd->min_offset >= he_obss_pd->max_offset)
4507                 return -EINVAL;
4508
4509         he_obss_pd->enable = true;
4510
4511         return 0;
4512 }
4513
4514 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
4515                                             const u8 *rates)
4516 {
4517         int i;
4518
4519         if (!rates)
4520                 return;
4521
4522         for (i = 0; i < rates[1]; i++) {
4523                 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
4524                         params->ht_required = true;
4525                 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
4526                         params->vht_required = true;
4527         }
4528 }
4529
4530 /*
4531  * Since the nl80211 API didn't include, from the beginning, attributes about
4532  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
4533  * benefit of drivers that rebuild IEs in the firmware.
4534  */
4535 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
4536 {
4537         const struct cfg80211_beacon_data *bcn = &params->beacon;
4538         size_t ies_len = bcn->tail_len;
4539         const u8 *ies = bcn->tail;
4540         const u8 *rates;
4541         const u8 *cap;
4542
4543         rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
4544         nl80211_check_ap_rate_selectors(params, rates);
4545
4546         rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
4547         nl80211_check_ap_rate_selectors(params, rates);
4548
4549         cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
4550         if (cap && cap[1] >= sizeof(*params->ht_cap))
4551                 params->ht_cap = (void *)(cap + 2);
4552         cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
4553         if (cap && cap[1] >= sizeof(*params->vht_cap))
4554                 params->vht_cap = (void *)(cap + 2);
4555         cap = cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
4556         if (cap && cap[1] >= sizeof(*params->he_cap) + 1)
4557                 params->he_cap = (void *)(cap + 3);
4558 }
4559
4560 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
4561                                    struct cfg80211_ap_settings *params)
4562 {
4563         struct wireless_dev *wdev;
4564         bool ret = false;
4565
4566         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
4567                 if (wdev->iftype != NL80211_IFTYPE_AP &&
4568                     wdev->iftype != NL80211_IFTYPE_P2P_GO)
4569                         continue;
4570
4571                 if (!wdev->preset_chandef.chan)
4572                         continue;
4573
4574                 params->chandef = wdev->preset_chandef;
4575                 ret = true;
4576                 break;
4577         }
4578
4579         return ret;
4580 }
4581
4582 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
4583                                     enum nl80211_auth_type auth_type,
4584                                     enum nl80211_commands cmd)
4585 {
4586         if (auth_type > NL80211_AUTHTYPE_MAX)
4587                 return false;
4588
4589         switch (cmd) {
4590         case NL80211_CMD_AUTHENTICATE:
4591                 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4592                     auth_type == NL80211_AUTHTYPE_SAE)
4593                         return false;
4594                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
4595                                              NL80211_EXT_FEATURE_FILS_STA) &&
4596                     (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4597                      auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4598                      auth_type == NL80211_AUTHTYPE_FILS_PK))
4599                         return false;
4600                 return true;
4601         case NL80211_CMD_CONNECT:
4602                 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
4603                     !wiphy_ext_feature_isset(&rdev->wiphy,
4604                                              NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
4605                     auth_type == NL80211_AUTHTYPE_SAE)
4606                         return false;
4607
4608                 /* FILS with SK PFS or PK not supported yet */
4609                 if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4610                     auth_type == NL80211_AUTHTYPE_FILS_PK)
4611                         return false;
4612                 if (!wiphy_ext_feature_isset(
4613                             &rdev->wiphy,
4614                             NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
4615                     auth_type == NL80211_AUTHTYPE_FILS_SK)
4616                         return false;
4617                 return true;
4618         case NL80211_CMD_START_AP:
4619                 /* SAE not supported yet */
4620                 if (auth_type == NL80211_AUTHTYPE_SAE)
4621                         return false;
4622                 /* FILS not supported yet */
4623                 if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
4624                     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
4625                     auth_type == NL80211_AUTHTYPE_FILS_PK)
4626                         return false;
4627                 return true;
4628         default:
4629                 return false;
4630         }
4631 }
4632
4633 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
4634 {
4635         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4636         struct net_device *dev = info->user_ptr[1];
4637         struct wireless_dev *wdev = dev->ieee80211_ptr;
4638         struct cfg80211_ap_settings params;
4639         int err;
4640
4641         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4642             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4643                 return -EOPNOTSUPP;
4644
4645         if (!rdev->ops->start_ap)
4646                 return -EOPNOTSUPP;
4647
4648         if (wdev->beacon_interval)
4649                 return -EALREADY;
4650
4651         memset(&params, 0, sizeof(params));
4652
4653         /* these are required for START_AP */
4654         if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
4655             !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
4656             !info->attrs[NL80211_ATTR_BEACON_HEAD])
4657                 return -EINVAL;
4658
4659         err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon);
4660         if (err)
4661                 return err;
4662
4663         params.beacon_interval =
4664                 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4665         params.dtim_period =
4666                 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
4667
4668         err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
4669                                            params.beacon_interval);
4670         if (err)
4671                 return err;
4672
4673         /*
4674          * In theory, some of these attributes should be required here
4675          * but since they were not used when the command was originally
4676          * added, keep them optional for old user space programs to let
4677          * them continue to work with drivers that do not need the
4678          * additional information -- drivers must check!
4679          */
4680         if (info->attrs[NL80211_ATTR_SSID]) {
4681                 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4682                 params.ssid_len =
4683                         nla_len(info->attrs[NL80211_ATTR_SSID]);
4684                 if (params.ssid_len == 0 ||
4685                     params.ssid_len > IEEE80211_MAX_SSID_LEN)
4686                         return -EINVAL;
4687         }
4688
4689         if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
4690                 params.hidden_ssid = nla_get_u32(
4691                         info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4692
4693         params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4694
4695         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4696                 params.auth_type = nla_get_u32(
4697                         info->attrs[NL80211_ATTR_AUTH_TYPE]);
4698                 if (!nl80211_valid_auth_type(rdev, params.auth_type,
4699                                              NL80211_CMD_START_AP))
4700                         return -EINVAL;
4701         } else
4702                 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4703
4704         err = nl80211_crypto_settings(rdev, info, &params.crypto,
4705                                       NL80211_MAX_NR_CIPHER_SUITES);
4706         if (err)
4707                 return err;
4708
4709         if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4710                 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4711                         return -EOPNOTSUPP;
4712                 params.inactivity_timeout = nla_get_u16(
4713                         info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4714         }
4715
4716         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4717                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4718                         return -EINVAL;
4719                 params.p2p_ctwindow =
4720                         nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4721                 if (params.p2p_ctwindow != 0 &&
4722                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4723                         return -EINVAL;
4724         }
4725
4726         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4727                 u8 tmp;
4728
4729                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4730                         return -EINVAL;
4731                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4732                 params.p2p_opp_ps = tmp;
4733                 if (params.p2p_opp_ps != 0 &&
4734                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4735                         return -EINVAL;
4736         }
4737
4738         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4739                 err = nl80211_parse_chandef(rdev, info, &params.chandef);
4740                 if (err)
4741                         return err;
4742         } else if (wdev->preset_chandef.chan) {
4743                 params.chandef = wdev->preset_chandef;
4744         } else if (!nl80211_get_ap_channel(rdev, &params))
4745                 return -EINVAL;
4746
4747         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4748                                            wdev->iftype))
4749                 return -EINVAL;
4750
4751         if (info->attrs[NL80211_ATTR_TX_RATES]) {
4752                 err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4753                 if (err)
4754                         return err;
4755
4756                 err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4757                                               &params.beacon_rate);
4758                 if (err)
4759                         return err;
4760         }
4761
4762         if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4763                 params.smps_mode =
4764                         nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4765                 switch (params.smps_mode) {
4766                 case NL80211_SMPS_OFF:
4767                         break;
4768                 case NL80211_SMPS_STATIC:
4769                         if (!(rdev->wiphy.features &
4770                               NL80211_FEATURE_STATIC_SMPS))
4771                                 return -EINVAL;
4772                         break;
4773                 case NL80211_SMPS_DYNAMIC:
4774                         if (!(rdev->wiphy.features &
4775                               NL80211_FEATURE_DYNAMIC_SMPS))
4776                                 return -EINVAL;
4777                         break;
4778                 default:
4779                         return -EINVAL;
4780                 }
4781         } else {
4782                 params.smps_mode = NL80211_SMPS_OFF;
4783         }
4784
4785         params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4786         if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4787                 return -EOPNOTSUPP;
4788
4789         if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4790                 params.acl = parse_acl_data(&rdev->wiphy, info);
4791                 if (IS_ERR(params.acl))
4792                         return PTR_ERR(params.acl);
4793         }
4794
4795         params.twt_responder =
4796                     nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
4797
4798         if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
4799                 err = nl80211_parse_he_obss_pd(
4800                                         info->attrs[NL80211_ATTR_HE_OBSS_PD],
4801                                         &params.he_obss_pd);
4802                 if (err)
4803                         return err;
4804         }
4805
4806         nl80211_calculate_ap_params(&params);
4807
4808         if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
4809                 params.flags |= AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
4810
4811         wdev_lock(wdev);
4812         err = rdev_start_ap(rdev, dev, &params);
4813         if (!err) {
4814                 wdev->preset_chandef = params.chandef;
4815                 wdev->beacon_interval = params.beacon_interval;
4816                 wdev->chandef = params.chandef;
4817                 wdev->ssid_len = params.ssid_len;
4818                 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4819
4820                 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4821                         wdev->conn_owner_nlportid = info->snd_portid;
4822         }
4823         wdev_unlock(wdev);
4824
4825         kfree(params.acl);
4826
4827         return err;
4828 }
4829
4830 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4831 {
4832         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4833         struct net_device *dev = info->user_ptr[1];
4834         struct wireless_dev *wdev = dev->ieee80211_ptr;
4835         struct cfg80211_beacon_data params;
4836         int err;
4837
4838         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4839             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4840                 return -EOPNOTSUPP;
4841
4842         if (!rdev->ops->change_beacon)
4843                 return -EOPNOTSUPP;
4844
4845         if (!wdev->beacon_interval)
4846                 return -EINVAL;
4847
4848         err = nl80211_parse_beacon(rdev, info->attrs, &params);
4849         if (err)
4850                 return err;
4851
4852         wdev_lock(wdev);
4853         err = rdev_change_beacon(rdev, dev, &params);
4854         wdev_unlock(wdev);
4855
4856         return err;
4857 }
4858
4859 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4860 {
4861         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4862         struct net_device *dev = info->user_ptr[1];
4863
4864         return cfg80211_stop_ap(rdev, dev, false);
4865 }
4866
4867 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4868         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4869         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4870         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4871         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4872         [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4873         [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4874 };
4875
4876 static int parse_station_flags(struct genl_info *info,
4877                                enum nl80211_iftype iftype,
4878                                struct station_parameters *params)
4879 {
4880         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4881         struct nlattr *nla;
4882         int flag;
4883
4884         /*
4885          * Try parsing the new attribute first so userspace
4886          * can specify both for older kernels.
4887          */
4888         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4889         if (nla) {
4890                 struct nl80211_sta_flag_update *sta_flags;
4891
4892                 sta_flags = nla_data(nla);
4893                 params->sta_flags_mask = sta_flags->mask;
4894                 params->sta_flags_set = sta_flags->set;
4895                 params->sta_flags_set &= params->sta_flags_mask;
4896                 if ((params->sta_flags_mask |
4897                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4898                         return -EINVAL;
4899                 return 0;
4900         }
4901
4902         /* if present, parse the old attribute */
4903
4904         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4905         if (!nla)
4906                 return 0;
4907
4908         if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
4909                 return -EINVAL;
4910
4911         /*
4912          * Only allow certain flags for interface types so that
4913          * other attributes are silently ignored. Remember that
4914          * this is backward compatibility code with old userspace
4915          * and shouldn't be hit in other cases anyway.
4916          */
4917         switch (iftype) {
4918         case NL80211_IFTYPE_AP:
4919         case NL80211_IFTYPE_AP_VLAN:
4920         case NL80211_IFTYPE_P2P_GO:
4921                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4922                                          BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4923                                          BIT(NL80211_STA_FLAG_WME) |
4924                                          BIT(NL80211_STA_FLAG_MFP);
4925                 break;
4926         case NL80211_IFTYPE_P2P_CLIENT:
4927         case NL80211_IFTYPE_STATION:
4928                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4929                                          BIT(NL80211_STA_FLAG_TDLS_PEER);
4930                 break;
4931         case NL80211_IFTYPE_MESH_POINT:
4932                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4933                                          BIT(NL80211_STA_FLAG_MFP) |
4934                                          BIT(NL80211_STA_FLAG_AUTHORIZED);
4935                 break;
4936         default:
4937                 return -EINVAL;
4938         }
4939
4940         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4941                 if (flags[flag]) {
4942                         params->sta_flags_set |= (1<<flag);
4943
4944                         /* no longer support new API additions in old API */
4945                         if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4946                                 return -EINVAL;
4947                 }
4948         }
4949
4950         return 0;
4951 }
4952
4953 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
4954 {
4955         struct nlattr *rate;
4956         u32 bitrate;
4957         u16 bitrate_compat;
4958         enum nl80211_rate_info rate_flg;
4959
4960         rate = nla_nest_start_noflag(msg, attr);
4961         if (!rate)
4962                 return false;
4963
4964         /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4965         bitrate = cfg80211_calculate_bitrate(info);
4966         /* report 16-bit bitrate only if we can */
4967         bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4968         if (bitrate > 0 &&
4969             nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4970                 return false;
4971         if (bitrate_compat > 0 &&
4972             nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4973                 return false;
4974
4975         switch (info->bw) {
4976         case RATE_INFO_BW_5:
4977                 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4978                 break;
4979         case RATE_INFO_BW_10:
4980                 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4981                 break;
4982         default:
4983                 WARN_ON(1);
4984                 /* fall through */
4985         case RATE_INFO_BW_20:
4986                 rate_flg = 0;
4987                 break;
4988         case RATE_INFO_BW_40:
4989                 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4990                 break;
4991         case RATE_INFO_BW_80:
4992                 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4993                 break;
4994         case RATE_INFO_BW_160:
4995                 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4996                 break;
4997         case RATE_INFO_BW_HE_RU:
4998                 rate_flg = 0;
4999                 WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
5000         }
5001
5002         if (rate_flg && nla_put_flag(msg, rate_flg))
5003                 return false;
5004
5005         if (info->flags & RATE_INFO_FLAGS_MCS) {
5006                 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
5007                         return false;
5008                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5009                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5010                         return false;
5011         } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
5012                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
5013                         return false;
5014                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
5015                         return false;
5016                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
5017                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
5018                         return false;
5019         } else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
5020                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
5021                         return false;
5022                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
5023                         return false;
5024                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
5025                         return false;
5026                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
5027                         return false;
5028                 if (info->bw == RATE_INFO_BW_HE_RU &&
5029                     nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
5030                                info->he_ru_alloc))
5031                         return false;
5032         }
5033
5034         nla_nest_end(msg, rate);
5035         return true;
5036 }
5037
5038 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
5039                                int id)
5040 {
5041         void *attr;
5042         int i = 0;
5043
5044         if (!mask)
5045                 return true;
5046
5047         attr = nla_nest_start_noflag(msg, id);
5048         if (!attr)
5049                 return false;
5050
5051         for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
5052                 if (!(mask & BIT(i)))
5053                         continue;
5054
5055                 if (nla_put_u8(msg, i, signal[i]))
5056                         return false;
5057         }
5058
5059         nla_nest_end(msg, attr);
5060
5061         return true;
5062 }
5063
5064 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
5065                                 u32 seq, int flags,
5066                                 struct cfg80211_registered_device *rdev,
5067                                 struct net_device *dev,
5068                                 const u8 *mac_addr, struct station_info *sinfo)
5069 {
5070         void *hdr;
5071         struct nlattr *sinfoattr, *bss_param;
5072
5073         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
5074         if (!hdr) {
5075                 cfg80211_sinfo_release_content(sinfo);
5076                 return -1;
5077         }
5078
5079         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5080             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
5081             nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
5082                 goto nla_put_failure;
5083
5084         sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
5085         if (!sinfoattr)
5086                 goto nla_put_failure;
5087
5088 #define PUT_SINFO(attr, memb, type) do {                                \
5089         BUILD_BUG_ON(sizeof(type) == sizeof(u64));                      \
5090         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
5091             nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,            \
5092                              sinfo->memb))                              \
5093                 goto nla_put_failure;                                   \
5094         } while (0)
5095 #define PUT_SINFO_U64(attr, memb) do {                                  \
5096         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
5097             nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,           \
5098                               sinfo->memb, NL80211_STA_INFO_PAD))       \
5099                 goto nla_put_failure;                                   \
5100         } while (0)
5101
5102         PUT_SINFO(CONNECTED_TIME, connected_time, u32);
5103         PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
5104         PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
5105
5106         if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
5107                              BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
5108             nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
5109                         (u32)sinfo->rx_bytes))
5110                 goto nla_put_failure;
5111
5112         if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
5113                              BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
5114             nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
5115                         (u32)sinfo->tx_bytes))
5116                 goto nla_put_failure;
5117
5118         PUT_SINFO_U64(RX_BYTES64, rx_bytes);
5119         PUT_SINFO_U64(TX_BYTES64, tx_bytes);
5120         PUT_SINFO(LLID, llid, u16);
5121         PUT_SINFO(PLID, plid, u16);
5122         PUT_SINFO(PLINK_STATE, plink_state, u8);
5123         PUT_SINFO_U64(RX_DURATION, rx_duration);
5124         PUT_SINFO_U64(TX_DURATION, tx_duration);
5125
5126         if (wiphy_ext_feature_isset(&rdev->wiphy,
5127                                     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5128                 PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
5129
5130         switch (rdev->wiphy.signal_type) {
5131         case CFG80211_SIGNAL_TYPE_MBM:
5132                 PUT_SINFO(SIGNAL, signal, u8);
5133                 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
5134                 break;
5135         default:
5136                 break;
5137         }
5138         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
5139                 if (!nl80211_put_signal(msg, sinfo->chains,
5140                                         sinfo->chain_signal,
5141                                         NL80211_STA_INFO_CHAIN_SIGNAL))
5142                         goto nla_put_failure;
5143         }
5144         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
5145                 if (!nl80211_put_signal(msg, sinfo->chains,
5146                                         sinfo->chain_signal_avg,
5147                                         NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
5148                         goto nla_put_failure;
5149         }
5150         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
5151                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
5152                                           NL80211_STA_INFO_TX_BITRATE))
5153                         goto nla_put_failure;
5154         }
5155         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
5156                 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
5157                                           NL80211_STA_INFO_RX_BITRATE))
5158                         goto nla_put_failure;
5159         }
5160
5161         PUT_SINFO(RX_PACKETS, rx_packets, u32);
5162         PUT_SINFO(TX_PACKETS, tx_packets, u32);
5163         PUT_SINFO(TX_RETRIES, tx_retries, u32);
5164         PUT_SINFO(TX_FAILED, tx_failed, u32);
5165         PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
5166         PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
5167         PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
5168         PUT_SINFO(LOCAL_PM, local_pm, u32);
5169         PUT_SINFO(PEER_PM, peer_pm, u32);
5170         PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
5171         PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
5172
5173         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
5174                 bss_param = nla_nest_start_noflag(msg,
5175                                                   NL80211_STA_INFO_BSS_PARAM);
5176                 if (!bss_param)
5177                         goto nla_put_failure;
5178
5179                 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
5180                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
5181                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
5182                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
5183                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
5184                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
5185                     nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
5186                                sinfo->bss_param.dtim_period) ||
5187                     nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
5188                                 sinfo->bss_param.beacon_interval))
5189                         goto nla_put_failure;
5190
5191                 nla_nest_end(msg, bss_param);
5192         }
5193         if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
5194             nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
5195                     sizeof(struct nl80211_sta_flag_update),
5196                     &sinfo->sta_flags))
5197                 goto nla_put_failure;
5198
5199         PUT_SINFO_U64(T_OFFSET, t_offset);
5200         PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
5201         PUT_SINFO_U64(BEACON_RX, rx_beacon);
5202         PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
5203         PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
5204         PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
5205         if (wiphy_ext_feature_isset(&rdev->wiphy,
5206                                     NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
5207                 PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
5208                 PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
5209         }
5210
5211 #undef PUT_SINFO
5212 #undef PUT_SINFO_U64
5213
5214         if (sinfo->pertid) {
5215                 struct nlattr *tidsattr;
5216                 int tid;
5217
5218                 tidsattr = nla_nest_start_noflag(msg,
5219                                                  NL80211_STA_INFO_TID_STATS);
5220                 if (!tidsattr)
5221                         goto nla_put_failure;
5222
5223                 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
5224                         struct cfg80211_tid_stats *tidstats;
5225                         struct nlattr *tidattr;
5226
5227                         tidstats = &sinfo->pertid[tid];
5228
5229                         if (!tidstats->filled)
5230                                 continue;
5231
5232                         tidattr = nla_nest_start_noflag(msg, tid + 1);
5233                         if (!tidattr)
5234                                 goto nla_put_failure;
5235
5236 #define PUT_TIDVAL_U64(attr, memb) do {                                 \
5237         if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&       \
5238             nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,          \
5239                               tidstats->memb, NL80211_TID_STATS_PAD))   \
5240                 goto nla_put_failure;                                   \
5241         } while (0)
5242
5243                         PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
5244                         PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
5245                         PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
5246                         PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
5247
5248 #undef PUT_TIDVAL_U64
5249                         if ((tidstats->filled &
5250                              BIT(NL80211_TID_STATS_TXQ_STATS)) &&
5251                             !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
5252                                                    NL80211_TID_STATS_TXQ_STATS))
5253                                 goto nla_put_failure;
5254
5255                         nla_nest_end(msg, tidattr);
5256                 }
5257
5258                 nla_nest_end(msg, tidsattr);
5259         }
5260
5261         nla_nest_end(msg, sinfoattr);
5262
5263         if (sinfo->assoc_req_ies_len &&
5264             nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
5265                     sinfo->assoc_req_ies))
5266                 goto nla_put_failure;
5267
5268         cfg80211_sinfo_release_content(sinfo);
5269         genlmsg_end(msg, hdr);
5270         return 0;
5271
5272  nla_put_failure:
5273         cfg80211_sinfo_release_content(sinfo);
5274         genlmsg_cancel(msg, hdr);
5275         return -EMSGSIZE;
5276 }
5277
5278 static int nl80211_dump_station(struct sk_buff *skb,
5279                                 struct netlink_callback *cb)
5280 {
5281         struct station_info sinfo;
5282         struct cfg80211_registered_device *rdev;
5283         struct wireless_dev *wdev;
5284         u8 mac_addr[ETH_ALEN];
5285         int sta_idx = cb->args[2];
5286         int err;
5287
5288         rtnl_lock();
5289         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
5290         if (err)
5291                 goto out_err;
5292
5293         if (!wdev->netdev) {
5294                 err = -EINVAL;
5295                 goto out_err;
5296         }
5297
5298         if (!rdev->ops->dump_station) {
5299                 err = -EOPNOTSUPP;
5300                 goto out_err;
5301         }
5302
5303         while (1) {
5304                 memset(&sinfo, 0, sizeof(sinfo));
5305                 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
5306                                         mac_addr, &sinfo);
5307                 if (err == -ENOENT)
5308                         break;
5309                 if (err)
5310                         goto out_err;
5311
5312                 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
5313                                 NETLINK_CB(cb->skb).portid,
5314                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5315                                 rdev, wdev->netdev, mac_addr,
5316                                 &sinfo) < 0)
5317                         goto out;
5318
5319                 sta_idx++;
5320         }
5321
5322  out:
5323         cb->args[2] = sta_idx;
5324         err = skb->len;
5325  out_err:
5326         rtnl_unlock();
5327
5328         return err;
5329 }
5330
5331 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
5332 {
5333         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5334         struct net_device *dev = info->user_ptr[1];
5335         struct station_info sinfo;
5336         struct sk_buff *msg;
5337         u8 *mac_addr = NULL;
5338         int err;
5339
5340         memset(&sinfo, 0, sizeof(sinfo));
5341
5342         if (!info->attrs[NL80211_ATTR_MAC])
5343                 return -EINVAL;
5344
5345         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5346
5347         if (!rdev->ops->get_station)
5348                 return -EOPNOTSUPP;
5349
5350         err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
5351         if (err)
5352                 return err;
5353
5354         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5355         if (!msg) {
5356                 cfg80211_sinfo_release_content(&sinfo);
5357                 return -ENOMEM;
5358         }
5359
5360         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
5361                                  info->snd_portid, info->snd_seq, 0,
5362                                  rdev, dev, mac_addr, &sinfo) < 0) {
5363                 nlmsg_free(msg);
5364                 return -ENOBUFS;
5365         }
5366
5367         return genlmsg_reply(msg, info);
5368 }
5369
5370 int cfg80211_check_station_change(struct wiphy *wiphy,
5371                                   struct station_parameters *params,
5372                                   enum cfg80211_station_type statype)
5373 {
5374         if (params->listen_interval != -1 &&
5375             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5376                 return -EINVAL;
5377
5378         if (params->support_p2p_ps != -1 &&
5379             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5380                 return -EINVAL;
5381
5382         if (params->aid &&
5383             !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
5384             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
5385                 return -EINVAL;
5386
5387         /* When you run into this, adjust the code below for the new flag */
5388         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5389
5390         switch (statype) {
5391         case CFG80211_STA_MESH_PEER_KERNEL:
5392         case CFG80211_STA_MESH_PEER_USER:
5393                 /*
5394                  * No ignoring the TDLS flag here -- the userspace mesh
5395                  * code doesn't have the bug of including TDLS in the
5396                  * mask everywhere.
5397                  */
5398                 if (params->sta_flags_mask &
5399                                 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5400                                   BIT(NL80211_STA_FLAG_MFP) |
5401                                   BIT(NL80211_STA_FLAG_AUTHORIZED)))
5402                         return -EINVAL;
5403                 break;
5404         case CFG80211_STA_TDLS_PEER_SETUP:
5405         case CFG80211_STA_TDLS_PEER_ACTIVE:
5406                 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5407                         return -EINVAL;
5408                 /* ignore since it can't change */
5409                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5410                 break;
5411         default:
5412                 /* disallow mesh-specific things */
5413                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
5414                         return -EINVAL;
5415                 if (params->local_pm)
5416                         return -EINVAL;
5417                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5418                         return -EINVAL;
5419         }
5420
5421         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5422             statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
5423                 /* TDLS can't be set, ... */
5424                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
5425                         return -EINVAL;
5426                 /*
5427                  * ... but don't bother the driver with it. This works around
5428                  * a hostapd/wpa_supplicant issue -- it always includes the
5429                  * TLDS_PEER flag in the mask even for AP mode.
5430                  */
5431                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5432         }
5433
5434         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
5435             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5436                 /* reject other things that can't change */
5437                 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
5438                         return -EINVAL;
5439                 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
5440                         return -EINVAL;
5441                 if (params->supported_rates)
5442                         return -EINVAL;
5443                 if (params->ext_capab || params->ht_capa || params->vht_capa ||
5444                     params->he_capa)
5445                         return -EINVAL;
5446         }
5447
5448         if (statype != CFG80211_STA_AP_CLIENT &&
5449             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
5450                 if (params->vlan)
5451                         return -EINVAL;
5452         }
5453
5454         switch (statype) {
5455         case CFG80211_STA_AP_MLME_CLIENT:
5456                 /* Use this only for authorizing/unauthorizing a station */
5457                 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5458                         return -EOPNOTSUPP;
5459                 break;
5460         case CFG80211_STA_AP_CLIENT:
5461         case CFG80211_STA_AP_CLIENT_UNASSOC:
5462                 /* accept only the listed bits */
5463                 if (params->sta_flags_mask &
5464                                 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5465                                   BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5466                                   BIT(NL80211_STA_FLAG_ASSOCIATED) |
5467                                   BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
5468                                   BIT(NL80211_STA_FLAG_WME) |
5469                                   BIT(NL80211_STA_FLAG_MFP)))
5470                         return -EINVAL;
5471
5472                 /* but authenticated/associated only if driver handles it */
5473                 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5474                     params->sta_flags_mask &
5475                                 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5476                                  BIT(NL80211_STA_FLAG_ASSOCIATED)))
5477                         return -EINVAL;
5478                 break;
5479         case CFG80211_STA_IBSS:
5480         case CFG80211_STA_AP_STA:
5481                 /* reject any changes other than AUTHORIZED */
5482                 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
5483                         return -EINVAL;
5484                 break;
5485         case CFG80211_STA_TDLS_PEER_SETUP:
5486                 /* reject any changes other than AUTHORIZED or WME */
5487                 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
5488                                                BIT(NL80211_STA_FLAG_WME)))
5489                         return -EINVAL;
5490                 /* force (at least) rates when authorizing */
5491                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
5492                     !params->supported_rates)
5493                         return -EINVAL;
5494                 break;
5495         case CFG80211_STA_TDLS_PEER_ACTIVE:
5496                 /* reject any changes */
5497                 return -EINVAL;
5498         case CFG80211_STA_MESH_PEER_KERNEL:
5499                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
5500                         return -EINVAL;
5501                 break;
5502         case CFG80211_STA_MESH_PEER_USER:
5503                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
5504                     params->plink_action != NL80211_PLINK_ACTION_BLOCK)
5505                         return -EINVAL;
5506                 break;
5507         }
5508
5509         /*
5510          * Older kernel versions ignored this attribute entirely, so don't
5511          * reject attempts to update it but mark it as unused instead so the
5512          * driver won't look at the data.
5513          */
5514         if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
5515             statype != CFG80211_STA_TDLS_PEER_SETUP)
5516                 params->opmode_notif_used = false;
5517
5518         return 0;
5519 }
5520 EXPORT_SYMBOL(cfg80211_check_station_change);
5521
5522 /*
5523  * Get vlan interface making sure it is running and on the right wiphy.
5524  */
5525 static struct net_device *get_vlan(struct genl_info *info,
5526                                    struct cfg80211_registered_device *rdev)
5527 {
5528         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
5529         struct net_device *v;
5530         int ret;
5531
5532         if (!vlanattr)
5533                 return NULL;
5534
5535         v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
5536         if (!v)
5537                 return ERR_PTR(-ENODEV);
5538
5539         if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
5540                 ret = -EINVAL;
5541                 goto error;
5542         }
5543
5544         if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5545             v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5546             v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5547                 ret = -EINVAL;
5548                 goto error;
5549         }
5550
5551         if (!netif_running(v)) {
5552                 ret = -ENETDOWN;
5553                 goto error;
5554         }
5555
5556         return v;
5557  error:
5558         dev_put(v);
5559         return ERR_PTR(ret);
5560 }
5561
5562 static const struct nla_policy
5563 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
5564         [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
5565         [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
5566 };
5567
5568 static int nl80211_parse_sta_wme(struct genl_info *info,
5569                                  struct station_parameters *params)
5570 {
5571         struct nlattr *tb[NL80211_STA_WME_MAX + 1];
5572         struct nlattr *nla;
5573         int err;
5574
5575         /* parse WME attributes if present */
5576         if (!info->attrs[NL80211_ATTR_STA_WME])
5577                 return 0;
5578
5579         nla = info->attrs[NL80211_ATTR_STA_WME];
5580         err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
5581                                           nl80211_sta_wme_policy,
5582                                           info->extack);
5583         if (err)
5584                 return err;
5585
5586         if (tb[NL80211_STA_WME_UAPSD_QUEUES])
5587                 params->uapsd_queues = nla_get_u8(
5588                         tb[NL80211_STA_WME_UAPSD_QUEUES]);
5589         if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
5590                 return -EINVAL;
5591
5592         if (tb[NL80211_STA_WME_MAX_SP])
5593                 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
5594
5595         if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
5596                 return -EINVAL;
5597
5598         params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
5599
5600         return 0;
5601 }
5602
5603 static int nl80211_parse_sta_channel_info(struct genl_info *info,
5604                                       struct station_parameters *params)
5605 {
5606         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
5607                 params->supported_channels =
5608                      nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5609                 params->supported_channels_len =
5610                      nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
5611                 /*
5612                  * Need to include at least one (first channel, number of
5613                  * channels) tuple for each subband, and must have proper
5614                  * tuples for the rest of the data as well.
5615                  */
5616                 if (params->supported_channels_len < 2)
5617                         return -EINVAL;
5618                 if (params->supported_channels_len % 2)
5619                         return -EINVAL;
5620         }
5621
5622         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
5623                 params->supported_oper_classes =
5624                  nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5625                 params->supported_oper_classes_len =
5626                   nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
5627                 /*
5628                  * The value of the Length field of the Supported Operating
5629                  * Classes element is between 2 and 253.
5630                  */
5631                 if (params->supported_oper_classes_len < 2 ||
5632                     params->supported_oper_classes_len > 253)
5633                         return -EINVAL;
5634         }
5635         return 0;
5636 }
5637
5638 static int nl80211_set_station_tdls(struct genl_info *info,
5639                                     struct station_parameters *params)
5640 {
5641         int err;
5642         /* Dummy STA entry gets updated once the peer capabilities are known */
5643         if (info->attrs[NL80211_ATTR_PEER_AID])
5644                 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5645         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5646                 params->ht_capa =
5647                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5648         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5649                 params->vht_capa =
5650                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5651         if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5652                 params->he_capa =
5653                         nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5654                 params->he_capa_len =
5655                         nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5656
5657                 if (params->he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5658                         return -EINVAL;
5659         }
5660
5661         err = nl80211_parse_sta_channel_info(info, params);
5662         if (err)
5663                 return err;
5664
5665         return nl80211_parse_sta_wme(info, params);
5666 }
5667
5668 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
5669                                              struct station_parameters *params)
5670 {
5671         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5672         int idx;
5673
5674         if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
5675                 if (!rdev->ops->set_tx_power ||
5676                     !wiphy_ext_feature_isset(&rdev->wiphy,
5677                                          NL80211_EXT_FEATURE_STA_TX_PWR))
5678                         return -EOPNOTSUPP;
5679
5680                 idx = NL80211_ATTR_STA_TX_POWER_SETTING;
5681                 params->txpwr.type = nla_get_u8(info->attrs[idx]);
5682
5683                 if (params->txpwr.type == NL80211_TX_POWER_LIMITED) {
5684                         idx = NL80211_ATTR_STA_TX_POWER;
5685
5686                         if (info->attrs[idx])
5687                                 params->txpwr.power =
5688                                         nla_get_s16(info->attrs[idx]);
5689                         else
5690                                 return -EINVAL;
5691                 }
5692                 params->sta_modify_mask |= STATION_PARAM_APPLY_STA_TXPOWER;
5693         }
5694
5695         return 0;
5696 }
5697
5698 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
5699 {
5700         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5701         struct net_device *dev = info->user_ptr[1];
5702         struct station_parameters params;
5703         u8 *mac_addr;
5704         int err;
5705
5706         memset(&params, 0, sizeof(params));
5707
5708         if (!rdev->ops->change_station)
5709                 return -EOPNOTSUPP;
5710
5711         /*
5712          * AID and listen_interval properties can be set only for unassociated
5713          * station. Include these parameters here and will check them in
5714          * cfg80211_check_station_change().
5715          */
5716         if (info->attrs[NL80211_ATTR_STA_AID])
5717                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5718
5719         if (info->attrs[NL80211_ATTR_VLAN_ID])
5720                 params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
5721
5722         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5723                 params.listen_interval =
5724                      nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5725         else
5726                 params.listen_interval = -1;
5727
5728         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
5729                 params.support_p2p_ps =
5730                         nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5731         else
5732                 params.support_p2p_ps = -1;
5733
5734         if (!info->attrs[NL80211_ATTR_MAC])
5735                 return -EINVAL;
5736
5737         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5738
5739         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
5740                 params.supported_rates =
5741                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5742                 params.supported_rates_len =
5743                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5744         }
5745
5746         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5747                 params.capability =
5748                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5749                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5750         }
5751
5752         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5753                 params.ext_capab =
5754                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5755                 params.ext_capab_len =
5756                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5757         }
5758
5759         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5760                 return -EINVAL;
5761
5762         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5763                 params.plink_action =
5764                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5765
5766         if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
5767                 params.plink_state =
5768                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
5769                 if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
5770                         params.peer_aid = nla_get_u16(
5771                                 info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5772                 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5773         }
5774
5775         if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
5776                 params.local_pm = nla_get_u32(
5777                         info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5778
5779         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5780                 params.opmode_notif_used = true;
5781                 params.opmode_notif =
5782                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5783         }
5784
5785         if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5786                 params.airtime_weight =
5787                         nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5788
5789         if (params.airtime_weight &&
5790             !wiphy_ext_feature_isset(&rdev->wiphy,
5791                                      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5792                 return -EOPNOTSUPP;
5793
5794         err = nl80211_parse_sta_txpower_setting(info, &params);
5795         if (err)
5796                 return err;
5797
5798         /* Include parameters for TDLS peer (will check later) */
5799         err = nl80211_set_station_tdls(info, &params);
5800         if (err)
5801                 return err;
5802
5803         params.vlan = get_vlan(info, rdev);
5804         if (IS_ERR(params.vlan))
5805                 return PTR_ERR(params.vlan);
5806
5807         switch (dev->ieee80211_ptr->iftype) {
5808         case NL80211_IFTYPE_AP:
5809         case NL80211_IFTYPE_AP_VLAN:
5810         case NL80211_IFTYPE_P2P_GO:
5811         case NL80211_IFTYPE_P2P_CLIENT:
5812         case NL80211_IFTYPE_STATION:
5813         case NL80211_IFTYPE_ADHOC:
5814         case NL80211_IFTYPE_MESH_POINT:
5815                 break;
5816         default:
5817                 err = -EOPNOTSUPP;
5818                 goto out_put_vlan;
5819         }
5820
5821         /* driver will call cfg80211_check_station_change() */
5822         err = rdev_change_station(rdev, dev, mac_addr, &params);
5823
5824  out_put_vlan:
5825         if (params.vlan)
5826                 dev_put(params.vlan);
5827
5828         return err;
5829 }
5830
5831 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5832 {
5833         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5834         int err;
5835         struct net_device *dev = info->user_ptr[1];
5836         struct station_parameters params;
5837         u8 *mac_addr = NULL;
5838         u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5839                          BIT(NL80211_STA_FLAG_ASSOCIATED);
5840
5841         memset(&params, 0, sizeof(params));
5842
5843         if (!rdev->ops->add_station)
5844                 return -EOPNOTSUPP;
5845
5846         if (!info->attrs[NL80211_ATTR_MAC])
5847                 return -EINVAL;
5848
5849         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5850                 return -EINVAL;
5851
5852         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5853                 return -EINVAL;
5854
5855         if (!info->attrs[NL80211_ATTR_STA_AID] &&
5856             !info->attrs[NL80211_ATTR_PEER_AID])
5857                 return -EINVAL;
5858
5859         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5860         params.supported_rates =
5861                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5862         params.supported_rates_len =
5863                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5864         params.listen_interval =
5865                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5866
5867         if (info->attrs[NL80211_ATTR_VLAN_ID])
5868                 params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
5869
5870         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5871                 params.support_p2p_ps =
5872                         nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5873         } else {
5874                 /*
5875                  * if not specified, assume it's supported for P2P GO interface,
5876                  * and is NOT supported for AP interface
5877                  */
5878                 params.support_p2p_ps =
5879                         dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5880         }
5881
5882         if (info->attrs[NL80211_ATTR_PEER_AID])
5883                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5884         else
5885                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5886
5887         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5888                 params.capability =
5889                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5890                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5891         }
5892
5893         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5894                 params.ext_capab =
5895                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5896                 params.ext_capab_len =
5897                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5898         }
5899
5900         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5901                 params.ht_capa =
5902                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5903
5904         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5905                 params.vht_capa =
5906                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5907
5908         if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
5909                 params.he_capa =
5910                         nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5911                 params.he_capa_len =
5912                         nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
5913
5914                 /* max len is validated in nla policy */
5915                 if (params.he_capa_len < NL80211_HE_MIN_CAPABILITY_LEN)
5916                         return -EINVAL;
5917         }
5918
5919         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5920                 params.opmode_notif_used = true;
5921                 params.opmode_notif =
5922                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5923         }
5924
5925         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
5926                 params.plink_action =
5927                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5928
5929         if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
5930                 params.airtime_weight =
5931                         nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
5932
5933         if (params.airtime_weight &&
5934             !wiphy_ext_feature_isset(&rdev->wiphy,
5935                                      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
5936                 return -EOPNOTSUPP;
5937
5938         err = nl80211_parse_sta_txpower_setting(info, &params);
5939         if (err)
5940                 return err;
5941
5942         err = nl80211_parse_sta_channel_info(info, &params);
5943         if (err)
5944                 return err;
5945
5946         err = nl80211_parse_sta_wme(info, &params);
5947         if (err)
5948                 return err;
5949
5950         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5951                 return -EINVAL;
5952
5953         /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5954          * as userspace might just pass through the capabilities from the IEs
5955          * directly, rather than enforcing this restriction and returning an
5956          * error in this case.
5957          */
5958         if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5959                 params.ht_capa = NULL;
5960                 params.vht_capa = NULL;
5961
5962                 /* HE requires WME */
5963                 if (params.he_capa_len)
5964                         return -EINVAL;
5965         }
5966
5967         /* When you run into this, adjust the code below for the new flag */
5968         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5969
5970         switch (dev->ieee80211_ptr->iftype) {
5971         case NL80211_IFTYPE_AP:
5972         case NL80211_IFTYPE_AP_VLAN:
5973         case NL80211_IFTYPE_P2P_GO:
5974                 /* ignore WME attributes if iface/sta is not capable */
5975                 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5976                     !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5977                         params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5978
5979                 /* TDLS peers cannot be added */
5980                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5981                     info->attrs[NL80211_ATTR_PEER_AID])
5982                         return -EINVAL;
5983                 /* but don't bother the driver with it */
5984                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5985
5986                 /* allow authenticated/associated only if driver handles it */
5987                 if (!(rdev->wiphy.features &
5988                                 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5989                     params.sta_flags_mask & auth_assoc)
5990                         return -EINVAL;
5991
5992                 /* Older userspace, or userspace wanting to be compatible with
5993                  * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5994                  * and assoc flags in the mask, but assumes the station will be
5995                  * added as associated anyway since this was the required driver
5996                  * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5997                  * introduced.
5998                  * In order to not bother drivers with this quirk in the API
5999                  * set the flags in both the mask and set for new stations in
6000                  * this case.
6001                  */
6002                 if (!(params.sta_flags_mask & auth_assoc)) {
6003                         params.sta_flags_mask |= auth_assoc;
6004                         params.sta_flags_set |= auth_assoc;
6005                 }
6006
6007                 /* must be last in here for error handling */
6008                 params.vlan = get_vlan(info, rdev);
6009                 if (IS_ERR(params.vlan))
6010                         return PTR_ERR(params.vlan);
6011                 break;
6012         case NL80211_IFTYPE_MESH_POINT:
6013                 /* ignore uAPSD data */
6014                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6015
6016                 /* associated is disallowed */
6017                 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
6018                         return -EINVAL;
6019                 /* TDLS peers cannot be added */
6020                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
6021                     info->attrs[NL80211_ATTR_PEER_AID])
6022                         return -EINVAL;
6023                 break;
6024         case NL80211_IFTYPE_STATION:
6025         case NL80211_IFTYPE_P2P_CLIENT:
6026                 /* ignore uAPSD data */
6027                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
6028
6029                 /* these are disallowed */
6030                 if (params.sta_flags_mask &
6031                                 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
6032                                  BIT(NL80211_STA_FLAG_AUTHENTICATED)))
6033                         return -EINVAL;
6034                 /* Only TDLS peers can be added */
6035                 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6036                         return -EINVAL;
6037                 /* Can only add if TDLS ... */
6038                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
6039                         return -EOPNOTSUPP;
6040                 /* ... with external setup is supported */
6041                 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
6042                         return -EOPNOTSUPP;
6043                 /*
6044                  * Older wpa_supplicant versions always mark the TDLS peer
6045                  * as authorized, but it shouldn't yet be.
6046                  */
6047                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
6048                 break;
6049         default:
6050                 return -EOPNOTSUPP;
6051         }
6052
6053         /* be aware of params.vlan when changing code here */
6054
6055         err = rdev_add_station(rdev, dev, mac_addr, &params);
6056
6057         if (params.vlan)
6058                 dev_put(params.vlan);
6059         return err;
6060 }
6061
6062 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
6063 {
6064         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6065         struct net_device *dev = info->user_ptr[1];
6066         struct station_del_parameters params;
6067
6068         memset(&params, 0, sizeof(params));
6069
6070         if (info->attrs[NL80211_ATTR_MAC])
6071                 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
6072
6073         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6074             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6075             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
6076             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6077                 return -EINVAL;
6078
6079         if (!rdev->ops->del_station)
6080                 return -EOPNOTSUPP;
6081
6082         if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
6083                 params.subtype =
6084                         nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
6085                 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
6086                     params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
6087                         return -EINVAL;
6088         } else {
6089                 /* Default to Deauthentication frame */
6090                 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
6091         }
6092
6093         if (info->attrs[NL80211_ATTR_REASON_CODE]) {
6094                 params.reason_code =
6095                         nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6096                 if (params.reason_code == 0)
6097                         return -EINVAL; /* 0 is reserved */
6098         } else {
6099                 /* Default to reason code 2 */
6100                 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
6101         }
6102
6103         return rdev_del_station(rdev, dev, &params);
6104 }
6105
6106 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
6107                                 int flags, struct net_device *dev,
6108                                 u8 *dst, u8 *next_hop,
6109                                 struct mpath_info *pinfo)
6110 {
6111         void *hdr;
6112         struct nlattr *pinfoattr;
6113
6114         hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
6115         if (!hdr)
6116                 return -1;
6117
6118         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6119             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6120             nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
6121             nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
6122                 goto nla_put_failure;
6123
6124         pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
6125         if (!pinfoattr)
6126                 goto nla_put_failure;
6127         if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
6128             nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
6129                         pinfo->frame_qlen))
6130                 goto nla_put_failure;
6131         if (((pinfo->filled & MPATH_INFO_SN) &&
6132              nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
6133             ((pinfo->filled & MPATH_INFO_METRIC) &&
6134              nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
6135                          pinfo->metric)) ||
6136             ((pinfo->filled & MPATH_INFO_EXPTIME) &&
6137              nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
6138                          pinfo->exptime)) ||
6139             ((pinfo->filled & MPATH_INFO_FLAGS) &&
6140              nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
6141                         pinfo->flags)) ||
6142             ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
6143              nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
6144                          pinfo->discovery_timeout)) ||
6145             ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
6146              nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
6147                         pinfo->discovery_retries)) ||
6148             ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
6149              nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
6150                         pinfo->hop_count)) ||
6151             ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
6152              nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
6153                          pinfo->path_change_count)))
6154                 goto nla_put_failure;
6155
6156         nla_nest_end(msg, pinfoattr);
6157
6158         genlmsg_end(msg, hdr);
6159         return 0;
6160
6161  nla_put_failure:
6162         genlmsg_cancel(msg, hdr);
6163         return -EMSGSIZE;
6164 }
6165
6166 static int nl80211_dump_mpath(struct sk_buff *skb,
6167                               struct netlink_callback *cb)
6168 {
6169         struct mpath_info pinfo;
6170         struct cfg80211_registered_device *rdev;
6171         struct wireless_dev *wdev;
6172         u8 dst[ETH_ALEN];
6173         u8 next_hop[ETH_ALEN];
6174         int path_idx = cb->args[2];
6175         int err;
6176
6177         rtnl_lock();
6178         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6179         if (err)
6180                 goto out_err;
6181
6182         if (!rdev->ops->dump_mpath) {
6183                 err = -EOPNOTSUPP;
6184                 goto out_err;
6185         }
6186
6187         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6188                 err = -EOPNOTSUPP;
6189                 goto out_err;
6190         }
6191
6192         while (1) {
6193                 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
6194                                       next_hop, &pinfo);
6195                 if (err == -ENOENT)
6196                         break;
6197                 if (err)
6198                         goto out_err;
6199
6200                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6201                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
6202                                        wdev->netdev, dst, next_hop,
6203                                        &pinfo) < 0)
6204                         goto out;
6205
6206                 path_idx++;
6207         }
6208
6209  out:
6210         cb->args[2] = path_idx;
6211         err = skb->len;
6212  out_err:
6213         rtnl_unlock();
6214         return err;
6215 }
6216
6217 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
6218 {
6219         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6220         int err;
6221         struct net_device *dev = info->user_ptr[1];
6222         struct mpath_info pinfo;
6223         struct sk_buff *msg;
6224         u8 *dst = NULL;
6225         u8 next_hop[ETH_ALEN];
6226
6227         memset(&pinfo, 0, sizeof(pinfo));
6228
6229         if (!info->attrs[NL80211_ATTR_MAC])
6230                 return -EINVAL;
6231
6232         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6233
6234         if (!rdev->ops->get_mpath)
6235                 return -EOPNOTSUPP;
6236
6237         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6238                 return -EOPNOTSUPP;
6239
6240         err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
6241         if (err)
6242                 return err;
6243
6244         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6245         if (!msg)
6246                 return -ENOMEM;
6247
6248         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6249                                  dev, dst, next_hop, &pinfo) < 0) {
6250                 nlmsg_free(msg);
6251                 return -ENOBUFS;
6252         }
6253
6254         return genlmsg_reply(msg, info);
6255 }
6256
6257 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
6258 {
6259         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6260         struct net_device *dev = info->user_ptr[1];
6261         u8 *dst = NULL;
6262         u8 *next_hop = NULL;
6263
6264         if (!info->attrs[NL80211_ATTR_MAC])
6265                 return -EINVAL;
6266
6267         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6268                 return -EINVAL;
6269
6270         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6271         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6272
6273         if (!rdev->ops->change_mpath)
6274                 return -EOPNOTSUPP;
6275
6276         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6277                 return -EOPNOTSUPP;
6278
6279         return rdev_change_mpath(rdev, dev, dst, next_hop);
6280 }
6281
6282 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
6283 {
6284         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6285         struct net_device *dev = info->user_ptr[1];
6286         u8 *dst = NULL;
6287         u8 *next_hop = NULL;
6288
6289         if (!info->attrs[NL80211_ATTR_MAC])
6290                 return -EINVAL;
6291
6292         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
6293                 return -EINVAL;
6294
6295         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6296         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
6297
6298         if (!rdev->ops->add_mpath)
6299                 return -EOPNOTSUPP;
6300
6301         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6302                 return -EOPNOTSUPP;
6303
6304         return rdev_add_mpath(rdev, dev, dst, next_hop);
6305 }
6306
6307 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
6308 {
6309         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6310         struct net_device *dev = info->user_ptr[1];
6311         u8 *dst = NULL;
6312
6313         if (info->attrs[NL80211_ATTR_MAC])
6314                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6315
6316         if (!rdev->ops->del_mpath)
6317                 return -EOPNOTSUPP;
6318
6319         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6320                 return -EOPNOTSUPP;
6321
6322         return rdev_del_mpath(rdev, dev, dst);
6323 }
6324
6325 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
6326 {
6327         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6328         int err;
6329         struct net_device *dev = info->user_ptr[1];
6330         struct mpath_info pinfo;
6331         struct sk_buff *msg;
6332         u8 *dst = NULL;
6333         u8 mpp[ETH_ALEN];
6334
6335         memset(&pinfo, 0, sizeof(pinfo));
6336
6337         if (!info->attrs[NL80211_ATTR_MAC])
6338                 return -EINVAL;
6339
6340         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
6341
6342         if (!rdev->ops->get_mpp)
6343                 return -EOPNOTSUPP;
6344
6345         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6346                 return -EOPNOTSUPP;
6347
6348         err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
6349         if (err)
6350                 return err;
6351
6352         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6353         if (!msg)
6354                 return -ENOMEM;
6355
6356         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
6357                                dev, dst, mpp, &pinfo) < 0) {
6358                 nlmsg_free(msg);
6359                 return -ENOBUFS;
6360         }
6361
6362         return genlmsg_reply(msg, info);
6363 }
6364
6365 static int nl80211_dump_mpp(struct sk_buff *skb,
6366                             struct netlink_callback *cb)
6367 {
6368         struct mpath_info pinfo;
6369         struct cfg80211_registered_device *rdev;
6370         struct wireless_dev *wdev;
6371         u8 dst[ETH_ALEN];
6372         u8 mpp[ETH_ALEN];
6373         int path_idx = cb->args[2];
6374         int err;
6375
6376         rtnl_lock();
6377         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
6378         if (err)
6379                 goto out_err;
6380
6381         if (!rdev->ops->dump_mpp) {
6382                 err = -EOPNOTSUPP;
6383                 goto out_err;
6384         }
6385
6386         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
6387                 err = -EOPNOTSUPP;
6388                 goto out_err;
6389         }
6390
6391         while (1) {
6392                 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
6393                                     mpp, &pinfo);
6394                 if (err == -ENOENT)
6395                         break;
6396                 if (err)
6397                         goto out_err;
6398
6399                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
6400                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
6401                                        wdev->netdev, dst, mpp,
6402                                        &pinfo) < 0)
6403                         goto out;
6404
6405                 path_idx++;
6406         }
6407
6408  out:
6409         cb->args[2] = path_idx;
6410         err = skb->len;
6411  out_err:
6412         rtnl_unlock();
6413         return err;
6414 }
6415
6416 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
6417 {
6418         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6419         struct net_device *dev = info->user_ptr[1];
6420         struct wireless_dev *wdev = dev->ieee80211_ptr;
6421         struct bss_parameters params;
6422         int err;
6423
6424         memset(&params, 0, sizeof(params));
6425         /* default to not changing parameters */
6426         params.use_cts_prot = -1;
6427         params.use_short_preamble = -1;
6428         params.use_short_slot_time = -1;
6429         params.ap_isolate = -1;
6430         params.ht_opmode = -1;
6431         params.p2p_ctwindow = -1;
6432         params.p2p_opp_ps = -1;
6433
6434         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
6435                 params.use_cts_prot =
6436                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
6437         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
6438                 params.use_short_preamble =
6439                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
6440         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
6441                 params.use_short_slot_time =
6442                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
6443         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6444                 params.basic_rates =
6445                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6446                 params.basic_rates_len =
6447                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6448         }
6449         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
6450                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
6451         if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
6452                 params.ht_opmode =
6453                         nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
6454
6455         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
6456                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6457                         return -EINVAL;
6458                 params.p2p_ctwindow =
6459                         nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
6460                 if (params.p2p_ctwindow != 0 &&
6461                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
6462                         return -EINVAL;
6463         }
6464
6465         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
6466                 u8 tmp;
6467
6468                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6469                         return -EINVAL;
6470                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
6471                 params.p2p_opp_ps = tmp;
6472                 if (params.p2p_opp_ps &&
6473                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
6474                         return -EINVAL;
6475         }
6476
6477         if (!rdev->ops->change_bss)
6478                 return -EOPNOTSUPP;
6479
6480         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6481             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6482                 return -EOPNOTSUPP;
6483
6484         wdev_lock(wdev);
6485         err = rdev_change_bss(rdev, dev, &params);
6486         wdev_unlock(wdev);
6487
6488         return err;
6489 }
6490
6491 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
6492 {
6493         char *data = NULL;
6494         bool is_indoor;
6495         enum nl80211_user_reg_hint_type user_reg_hint_type;
6496         u32 owner_nlportid;
6497
6498         /*
6499          * You should only get this when cfg80211 hasn't yet initialized
6500          * completely when built-in to the kernel right between the time
6501          * window between nl80211_init() and regulatory_init(), if that is
6502          * even possible.
6503          */
6504         if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
6505                 return -EINPROGRESS;
6506
6507         if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
6508                 user_reg_hint_type =
6509                   nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
6510         else
6511                 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
6512
6513         switch (user_reg_hint_type) {
6514         case NL80211_USER_REG_HINT_USER:
6515         case NL80211_USER_REG_HINT_CELL_BASE:
6516                 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6517                         return -EINVAL;
6518
6519                 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6520                 return regulatory_hint_user(data, user_reg_hint_type);
6521         case NL80211_USER_REG_HINT_INDOOR:
6522                 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
6523                         owner_nlportid = info->snd_portid;
6524                         is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
6525                 } else {
6526                         owner_nlportid = 0;
6527                         is_indoor = true;
6528                 }
6529
6530                 return regulatory_hint_indoor(is_indoor, owner_nlportid);
6531         default:
6532                 return -EINVAL;
6533         }
6534 }
6535
6536 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
6537 {
6538         return reg_reload_regdb();
6539 }
6540
6541 static int nl80211_get_mesh_config(struct sk_buff *skb,
6542                                    struct genl_info *info)
6543 {
6544         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6545         struct net_device *dev = info->user_ptr[1];
6546         struct wireless_dev *wdev = dev->ieee80211_ptr;
6547         struct mesh_config cur_params;
6548         int err = 0;
6549         void *hdr;
6550         struct nlattr *pinfoattr;
6551         struct sk_buff *msg;
6552
6553         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6554                 return -EOPNOTSUPP;
6555
6556         if (!rdev->ops->get_mesh_config)
6557                 return -EOPNOTSUPP;
6558
6559         wdev_lock(wdev);
6560         /* If not connected, get default parameters */
6561         if (!wdev->mesh_id_len)
6562                 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
6563         else
6564                 err = rdev_get_mesh_config(rdev, dev, &cur_params);
6565         wdev_unlock(wdev);
6566
6567         if (err)
6568                 return err;
6569
6570         /* Draw up a netlink message to send back */
6571         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6572         if (!msg)
6573                 return -ENOMEM;
6574         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6575                              NL80211_CMD_GET_MESH_CONFIG);
6576         if (!hdr)
6577                 goto out;
6578         pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
6579         if (!pinfoattr)
6580                 goto nla_put_failure;
6581         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6582             nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
6583                         cur_params.dot11MeshRetryTimeout) ||
6584             nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
6585                         cur_params.dot11MeshConfirmTimeout) ||
6586             nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
6587                         cur_params.dot11MeshHoldingTimeout) ||
6588             nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
6589                         cur_params.dot11MeshMaxPeerLinks) ||
6590             nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
6591                        cur_params.dot11MeshMaxRetries) ||
6592             nla_put_u8(msg, NL80211_MESHCONF_TTL,
6593                        cur_params.dot11MeshTTL) ||
6594             nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
6595                        cur_params.element_ttl) ||
6596             nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6597                        cur_params.auto_open_plinks) ||
6598             nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6599                         cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
6600             nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6601                        cur_params.dot11MeshHWMPmaxPREQretries) ||
6602             nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
6603                         cur_params.path_refresh_time) ||
6604             nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6605                         cur_params.min_discovery_timeout) ||
6606             nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6607                         cur_params.dot11MeshHWMPactivePathTimeout) ||
6608             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6609                         cur_params.dot11MeshHWMPpreqMinInterval) ||
6610             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6611                         cur_params.dot11MeshHWMPperrMinInterval) ||
6612             nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6613                         cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
6614             nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
6615                        cur_params.dot11MeshHWMPRootMode) ||
6616             nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6617                         cur_params.dot11MeshHWMPRannInterval) ||
6618             nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6619                        cur_params.dot11MeshGateAnnouncementProtocol) ||
6620             nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
6621                        cur_params.dot11MeshForwarding) ||
6622             nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
6623                         cur_params.rssi_threshold) ||
6624             nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
6625                         cur_params.ht_opmode) ||
6626             nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6627                         cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
6628             nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6629                         cur_params.dot11MeshHWMProotInterval) ||
6630             nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6631                         cur_params.dot11MeshHWMPconfirmationInterval) ||
6632             nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
6633                         cur_params.power_mode) ||
6634             nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
6635                         cur_params.dot11MeshAwakeWindowDuration) ||
6636             nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
6637                         cur_params.plink_timeout) ||
6638             nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
6639                        cur_params.dot11MeshConnectedToMeshGate))
6640                 goto nla_put_failure;
6641         nla_nest_end(msg, pinfoattr);
6642         genlmsg_end(msg, hdr);
6643         return genlmsg_reply(msg, info);
6644
6645  nla_put_failure:
6646  out:
6647         nlmsg_free(msg);
6648         return -ENOBUFS;
6649 }
6650
6651 static const struct nla_policy
6652 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
6653         [NL80211_MESHCONF_RETRY_TIMEOUT] =
6654                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
6655         [NL80211_MESHCONF_CONFIRM_TIMEOUT] =
6656                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
6657         [NL80211_MESHCONF_HOLDING_TIMEOUT] =
6658                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
6659         [NL80211_MESHCONF_MAX_PEER_LINKS] =
6660                 NLA_POLICY_RANGE(NLA_U16, 0, 255),
6661         [NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
6662         [NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6663         [NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
6664         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
6665         [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
6666                 NLA_POLICY_RANGE(NLA_U32, 1, 255),
6667         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
6668         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
6669         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
6670         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
6671         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
6672                 NLA_POLICY_MIN(NLA_U16, 1),
6673         [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
6674                 NLA_POLICY_MIN(NLA_U16, 1),
6675         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
6676                 NLA_POLICY_MIN(NLA_U16, 1),
6677         [NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
6678         [NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
6679                 NLA_POLICY_MIN(NLA_U16, 1),
6680         [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
6681         [NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
6682         [NL80211_MESHCONF_RSSI_THRESHOLD] =
6683                 NLA_POLICY_RANGE(NLA_S32, -255, 0),
6684         [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
6685         [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
6686         [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
6687                 NLA_POLICY_MIN(NLA_U16, 1),
6688         [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
6689                 NLA_POLICY_MIN(NLA_U16, 1),
6690         [NL80211_MESHCONF_POWER_MODE] =
6691                 NLA_POLICY_RANGE(NLA_U32,
6692                                  NL80211_MESH_POWER_ACTIVE,
6693                                  NL80211_MESH_POWER_MAX),
6694         [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
6695         [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
6696         [NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
6697 };
6698
6699 static const struct nla_policy
6700         nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
6701         [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
6702         [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
6703         [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
6704         [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
6705         [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
6706         [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
6707         [NL80211_MESH_SETUP_IE] =
6708                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
6709                                        IEEE80211_MAX_DATA_LEN),
6710         [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
6711 };
6712
6713 static int nl80211_parse_mesh_config(struct genl_info *info,
6714                                      struct mesh_config *cfg,
6715                                      u32 *mask_out)
6716 {
6717         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
6718         u32 mask = 0;
6719         u16 ht_opmode;
6720
6721 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)       \
6722 do {                                                                    \
6723         if (tb[attr]) {                                                 \
6724                 cfg->param = fn(tb[attr]);                              \
6725                 mask |= BIT((attr) - 1);                                \
6726         }                                                               \
6727 } while (0)
6728
6729         if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
6730                 return -EINVAL;
6731         if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
6732                 return -EINVAL;
6733
6734         /* This makes sure that there aren't more than 32 mesh config
6735          * parameters (otherwise our bitfield scheme would not work.) */
6736         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
6737
6738         /* Fill in the params struct */
6739         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
6740                                   NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
6741         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
6742                                   NL80211_MESHCONF_CONFIRM_TIMEOUT,
6743                                   nla_get_u16);
6744         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
6745                                   NL80211_MESHCONF_HOLDING_TIMEOUT,
6746                                   nla_get_u16);
6747         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
6748                                   NL80211_MESHCONF_MAX_PEER_LINKS,
6749                                   nla_get_u16);
6750         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
6751                                   NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
6752         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
6753                                   NL80211_MESHCONF_TTL, nla_get_u8);
6754         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
6755                                   NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
6756         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
6757                                   NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6758                                   nla_get_u8);
6759         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6760                                   mask,
6761                                   NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6762                                   nla_get_u32);
6763         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
6764                                   NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6765                                   nla_get_u8);
6766         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
6767                                   NL80211_MESHCONF_PATH_REFRESH_TIME,
6768                                   nla_get_u32);
6769         if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
6770             (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
6771                 return -EINVAL;
6772         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
6773                                   NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6774                                   nla_get_u16);
6775         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6776                                   mask,
6777                                   NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6778                                   nla_get_u32);
6779         if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
6780             (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
6781              cfg->dot11MeshHWMPactivePathTimeout > 65535))
6782                 return -EINVAL;
6783         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
6784                                   NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6785                                   nla_get_u16);
6786         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
6787                                   NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6788                                   nla_get_u16);
6789         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6790                                   dot11MeshHWMPnetDiameterTraversalTime, mask,
6791                                   NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6792                                   nla_get_u16);
6793         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
6794                                   NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
6795         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
6796                                   NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6797                                   nla_get_u16);
6798         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
6799                                   mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6800                                   nla_get_u8);
6801         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
6802                                   NL80211_MESHCONF_FORWARDING, nla_get_u8);
6803         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
6804                                   NL80211_MESHCONF_RSSI_THRESHOLD,
6805                                   nla_get_s32);
6806         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
6807                                   NL80211_MESHCONF_CONNECTED_TO_GATE,
6808                                   nla_get_u8);
6809         /*
6810          * Check HT operation mode based on
6811          * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6812          */
6813         if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6814                 ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6815
6816                 if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6817                                   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6818                                   IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6819                         return -EINVAL;
6820
6821                 /* NON_HT_STA bit is reserved, but some programs set it */
6822                 ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6823
6824                 cfg->ht_opmode = ht_opmode;
6825                 mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6826         }
6827         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6828                                   dot11MeshHWMPactivePathToRootTimeout, mask,
6829                                   NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6830                                   nla_get_u32);
6831         if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
6832             (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
6833              cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
6834                 return -EINVAL;
6835         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
6836                                   NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6837                                   nla_get_u16);
6838         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
6839                                   mask,
6840                                   NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6841                                   nla_get_u16);
6842         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
6843                                   NL80211_MESHCONF_POWER_MODE, nla_get_u32);
6844         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
6845                                   NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
6846         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
6847                                   NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
6848         if (mask_out)
6849                 *mask_out = mask;
6850
6851         return 0;
6852
6853 #undef FILL_IN_MESH_PARAM_IF_SET
6854 }
6855
6856 static int nl80211_parse_mesh_setup(struct genl_info *info,
6857                                      struct mesh_setup *setup)
6858 {
6859         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6860         struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6861
6862         if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6863                 return -EINVAL;
6864         if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
6865                 return -EINVAL;
6866
6867         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6868                 setup->sync_method =
6869                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6870                  IEEE80211_SYNC_METHOD_VENDOR :
6871                  IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6872
6873         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6874                 setup->path_sel_proto =
6875                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6876                  IEEE80211_PATH_PROTOCOL_VENDOR :
6877                  IEEE80211_PATH_PROTOCOL_HWMP;
6878
6879         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6880                 setup->path_metric =
6881                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6882                  IEEE80211_PATH_METRIC_VENDOR :
6883                  IEEE80211_PATH_METRIC_AIRTIME;
6884
6885         if (tb[NL80211_MESH_SETUP_IE]) {
6886                 struct nlattr *ieattr =
6887                         tb[NL80211_MESH_SETUP_IE];
6888                 setup->ie = nla_data(ieattr);
6889                 setup->ie_len = nla_len(ieattr);
6890         }
6891         if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6892             !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6893                 return -EINVAL;
6894         setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6895         setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6896         setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6897         if (setup->is_secure)
6898                 setup->user_mpm = true;
6899
6900         if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6901                 if (!setup->user_mpm)
6902                         return -EINVAL;
6903                 setup->auth_id =
6904                         nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6905         }
6906
6907         return 0;
6908 }
6909
6910 static int nl80211_update_mesh_config(struct sk_buff *skb,
6911                                       struct genl_info *info)
6912 {
6913         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6914         struct net_device *dev = info->user_ptr[1];
6915         struct wireless_dev *wdev = dev->ieee80211_ptr;
6916         struct mesh_config cfg;
6917         u32 mask;
6918         int err;
6919
6920         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6921                 return -EOPNOTSUPP;
6922
6923         if (!rdev->ops->update_mesh_config)
6924                 return -EOPNOTSUPP;
6925
6926         err = nl80211_parse_mesh_config(info, &cfg, &mask);
6927         if (err)
6928                 return err;
6929
6930         wdev_lock(wdev);
6931         if (!wdev->mesh_id_len)
6932                 err = -ENOLINK;
6933
6934         if (!err)
6935                 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6936
6937         wdev_unlock(wdev);
6938
6939         return err;
6940 }
6941
6942 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6943                               struct sk_buff *msg)
6944 {
6945         struct nlattr *nl_reg_rules;
6946         unsigned int i;
6947
6948         if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6949             (regdom->dfs_region &&
6950              nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6951                 goto nla_put_failure;
6952
6953         nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
6954         if (!nl_reg_rules)
6955                 goto nla_put_failure;
6956
6957         for (i = 0; i < regdom->n_reg_rules; i++) {
6958                 struct nlattr *nl_reg_rule;
6959                 const struct ieee80211_reg_rule *reg_rule;
6960                 const struct ieee80211_freq_range *freq_range;
6961                 const struct ieee80211_power_rule *power_rule;
6962                 unsigned int max_bandwidth_khz;
6963
6964                 reg_rule = &regdom->reg_rules[i];
6965                 freq_range = &reg_rule->freq_range;
6966                 power_rule = &reg_rule->power_rule;
6967
6968                 nl_reg_rule = nla_nest_start_noflag(msg, i);
6969                 if (!nl_reg_rule)
6970                         goto nla_put_failure;
6971
6972                 max_bandwidth_khz = freq_range->max_bandwidth_khz;
6973                 if (!max_bandwidth_khz)
6974                         max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6975                                                                   reg_rule);
6976
6977                 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6978                                 reg_rule->flags) ||
6979                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6980                                 freq_range->start_freq_khz) ||
6981                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6982                                 freq_range->end_freq_khz) ||
6983                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6984                                 max_bandwidth_khz) ||
6985                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6986                                 power_rule->max_antenna_gain) ||
6987                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6988                                 power_rule->max_eirp) ||
6989                     nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6990                                 reg_rule->dfs_cac_ms))
6991                         goto nla_put_failure;
6992
6993                 nla_nest_end(msg, nl_reg_rule);
6994         }
6995
6996         nla_nest_end(msg, nl_reg_rules);
6997         return 0;
6998
6999 nla_put_failure:
7000         return -EMSGSIZE;
7001 }
7002
7003 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
7004 {
7005         const struct ieee80211_regdomain *regdom = NULL;
7006         struct cfg80211_registered_device *rdev;
7007         struct wiphy *wiphy = NULL;
7008         struct sk_buff *msg;
7009         void *hdr;
7010
7011         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7012         if (!msg)
7013                 return -ENOBUFS;
7014
7015         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7016                              NL80211_CMD_GET_REG);
7017         if (!hdr)
7018                 goto put_failure;
7019
7020         if (info->attrs[NL80211_ATTR_WIPHY]) {
7021                 bool self_managed;
7022
7023                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
7024                 if (IS_ERR(rdev)) {
7025                         nlmsg_free(msg);
7026                         return PTR_ERR(rdev);
7027                 }
7028
7029                 wiphy = &rdev->wiphy;
7030                 self_managed = wiphy->regulatory_flags &
7031                                REGULATORY_WIPHY_SELF_MANAGED;
7032                 regdom = get_wiphy_regdom(wiphy);
7033
7034                 /* a self-managed-reg device must have a private regdom */
7035                 if (WARN_ON(!regdom && self_managed)) {
7036                         nlmsg_free(msg);
7037                         return -EINVAL;
7038                 }
7039
7040                 if (regdom &&
7041                     nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7042                         goto nla_put_failure;
7043         }
7044
7045         if (!wiphy && reg_last_request_cell_base() &&
7046             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7047                         NL80211_USER_REG_HINT_CELL_BASE))
7048                 goto nla_put_failure;
7049
7050         rcu_read_lock();
7051
7052         if (!regdom)
7053                 regdom = rcu_dereference(cfg80211_regdomain);
7054
7055         if (nl80211_put_regdom(regdom, msg))
7056                 goto nla_put_failure_rcu;
7057
7058         rcu_read_unlock();
7059
7060         genlmsg_end(msg, hdr);
7061         return genlmsg_reply(msg, info);
7062
7063 nla_put_failure_rcu:
7064         rcu_read_unlock();
7065 nla_put_failure:
7066 put_failure:
7067         nlmsg_free(msg);
7068         return -EMSGSIZE;
7069 }
7070
7071 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
7072                                u32 seq, int flags, struct wiphy *wiphy,
7073                                const struct ieee80211_regdomain *regdom)
7074 {
7075         void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7076                                    NL80211_CMD_GET_REG);
7077
7078         if (!hdr)
7079                 return -1;
7080
7081         genl_dump_check_consistent(cb, hdr);
7082
7083         if (nl80211_put_regdom(regdom, msg))
7084                 goto nla_put_failure;
7085
7086         if (!wiphy && reg_last_request_cell_base() &&
7087             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
7088                         NL80211_USER_REG_HINT_CELL_BASE))
7089                 goto nla_put_failure;
7090
7091         if (wiphy &&
7092             nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
7093                 goto nla_put_failure;
7094
7095         if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
7096             nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
7097                 goto nla_put_failure;
7098
7099         genlmsg_end(msg, hdr);
7100         return 0;
7101
7102 nla_put_failure:
7103         genlmsg_cancel(msg, hdr);
7104         return -EMSGSIZE;
7105 }
7106
7107 static int nl80211_get_reg_dump(struct sk_buff *skb,
7108                                 struct netlink_callback *cb)
7109 {
7110         const struct ieee80211_regdomain *regdom = NULL;
7111         struct cfg80211_registered_device *rdev;
7112         int err, reg_idx, start = cb->args[2];
7113
7114         rtnl_lock();
7115
7116         if (cfg80211_regdomain && start == 0) {
7117                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7118                                           NLM_F_MULTI, NULL,
7119                                           rtnl_dereference(cfg80211_regdomain));
7120                 if (err < 0)
7121                         goto out_err;
7122         }
7123
7124         /* the global regdom is idx 0 */
7125         reg_idx = 1;
7126         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
7127                 regdom = get_wiphy_regdom(&rdev->wiphy);
7128                 if (!regdom)
7129                         continue;
7130
7131                 if (++reg_idx <= start)
7132                         continue;
7133
7134                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
7135                                           NLM_F_MULTI, &rdev->wiphy, regdom);
7136                 if (err < 0) {
7137                         reg_idx--;
7138                         break;
7139                 }
7140         }
7141
7142         cb->args[2] = reg_idx;
7143         err = skb->len;
7144 out_err:
7145         rtnl_unlock();
7146         return err;
7147 }
7148
7149 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
7150 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
7151         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
7152         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
7153         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
7154         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
7155         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
7156         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
7157         [NL80211_ATTR_DFS_CAC_TIME]             = { .type = NLA_U32 },
7158 };
7159
7160 static int parse_reg_rule(struct nlattr *tb[],
7161         struct ieee80211_reg_rule *reg_rule)
7162 {
7163         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
7164         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
7165
7166         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
7167                 return -EINVAL;
7168         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
7169                 return -EINVAL;
7170         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
7171                 return -EINVAL;
7172         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
7173                 return -EINVAL;
7174         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
7175                 return -EINVAL;
7176
7177         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
7178
7179         freq_range->start_freq_khz =
7180                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
7181         freq_range->end_freq_khz =
7182                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
7183         freq_range->max_bandwidth_khz =
7184                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
7185
7186         power_rule->max_eirp =
7187                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
7188
7189         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
7190                 power_rule->max_antenna_gain =
7191                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
7192
7193         if (tb[NL80211_ATTR_DFS_CAC_TIME])
7194                 reg_rule->dfs_cac_ms =
7195                         nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
7196
7197         return 0;
7198 }
7199
7200 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
7201 {
7202         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
7203         struct nlattr *nl_reg_rule;
7204         char *alpha2;
7205         int rem_reg_rules, r;
7206         u32 num_rules = 0, rule_idx = 0;
7207         enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
7208         struct ieee80211_regdomain *rd;
7209
7210         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7211                 return -EINVAL;
7212
7213         if (!info->attrs[NL80211_ATTR_REG_RULES])
7214                 return -EINVAL;
7215
7216         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7217
7218         if (info->attrs[NL80211_ATTR_DFS_REGION])
7219                 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
7220
7221         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7222                             rem_reg_rules) {
7223                 num_rules++;
7224                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
7225                         return -EINVAL;
7226         }
7227
7228         if (!reg_is_valid_request(alpha2))
7229                 return -EINVAL;
7230
7231         rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
7232         if (!rd)
7233                 return -ENOMEM;
7234
7235         rd->n_reg_rules = num_rules;
7236         rd->alpha2[0] = alpha2[0];
7237         rd->alpha2[1] = alpha2[1];
7238
7239         /*
7240          * Disable DFS master mode if the DFS region was
7241          * not supported or known on this kernel.
7242          */
7243         if (reg_supported_dfs_region(dfs_region))
7244                 rd->dfs_region = dfs_region;
7245
7246         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
7247                             rem_reg_rules) {
7248                 r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
7249                                                 nl_reg_rule, reg_rule_policy,
7250                                                 info->extack);
7251                 if (r)
7252                         goto bad_reg;
7253                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
7254                 if (r)
7255                         goto bad_reg;
7256
7257                 rule_idx++;
7258
7259                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
7260                         r = -EINVAL;
7261                         goto bad_reg;
7262                 }
7263         }
7264
7265         /* set_regdom takes ownership of rd */
7266         return set_regdom(rd, REGD_SOURCE_CRDA);
7267  bad_reg:
7268         kfree(rd);
7269         return r;
7270 }
7271 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
7272
7273 static int validate_scan_freqs(struct nlattr *freqs)
7274 {
7275         struct nlattr *attr1, *attr2;
7276         int n_channels = 0, tmp1, tmp2;
7277
7278         nla_for_each_nested(attr1, freqs, tmp1)
7279                 if (nla_len(attr1) != sizeof(u32))
7280                         return 0;
7281
7282         nla_for_each_nested(attr1, freqs, tmp1) {
7283                 n_channels++;
7284                 /*
7285                  * Some hardware has a limited channel list for
7286                  * scanning, and it is pretty much nonsensical
7287                  * to scan for a channel twice, so disallow that
7288                  * and don't require drivers to check that the
7289                  * channel list they get isn't longer than what
7290                  * they can scan, as long as they can scan all
7291                  * the channels they registered at once.
7292                  */
7293                 nla_for_each_nested(attr2, freqs, tmp2)
7294                         if (attr1 != attr2 &&
7295                             nla_get_u32(attr1) == nla_get_u32(attr2))
7296                                 return 0;
7297         }
7298
7299         return n_channels;
7300 }
7301
7302 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
7303 {
7304         return b < NUM_NL80211_BANDS && wiphy->bands[b];
7305 }
7306
7307 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
7308                             struct cfg80211_bss_selection *bss_select)
7309 {
7310         struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
7311         struct nlattr *nest;
7312         int err;
7313         bool found = false;
7314         int i;
7315
7316         /* only process one nested attribute */
7317         nest = nla_data(nla);
7318         if (!nla_ok(nest, nla_len(nest)))
7319                 return -EINVAL;
7320
7321         err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
7322                                           nest, nl80211_bss_select_policy,
7323                                           NULL);
7324         if (err)
7325                 return err;
7326
7327         /* only one attribute may be given */
7328         for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
7329                 if (attr[i]) {
7330                         if (found)
7331                                 return -EINVAL;
7332                         found = true;
7333                 }
7334         }
7335
7336         bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
7337
7338         if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
7339                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
7340
7341         if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
7342                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
7343                 bss_select->param.band_pref =
7344                         nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
7345                 if (!is_band_valid(wiphy, bss_select->param.band_pref))
7346                         return -EINVAL;
7347         }
7348
7349         if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
7350                 struct nl80211_bss_select_rssi_adjust *adj_param;
7351
7352                 adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
7353                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
7354                 bss_select->param.adjust.band = adj_param->band;
7355                 bss_select->param.adjust.delta = adj_param->delta;
7356                 if (!is_band_valid(wiphy, bss_select->param.adjust.band))
7357                         return -EINVAL;
7358         }
7359
7360         /* user-space did not provide behaviour attribute */
7361         if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
7362                 return -EINVAL;
7363
7364         if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
7365                 return -EINVAL;
7366
7367         return 0;
7368 }
7369
7370 int nl80211_parse_random_mac(struct nlattr **attrs,
7371                              u8 *mac_addr, u8 *mac_addr_mask)
7372 {
7373         int i;
7374
7375         if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
7376                 eth_zero_addr(mac_addr);
7377                 eth_zero_addr(mac_addr_mask);
7378                 mac_addr[0] = 0x2;
7379                 mac_addr_mask[0] = 0x3;
7380
7381                 return 0;
7382         }
7383
7384         /* need both or none */
7385         if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
7386                 return -EINVAL;
7387
7388         memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
7389         memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
7390
7391         /* don't allow or configure an mcast address */
7392         if (!is_multicast_ether_addr(mac_addr_mask) ||
7393             is_multicast_ether_addr(mac_addr))
7394                 return -EINVAL;
7395
7396         /*
7397          * allow users to pass a MAC address that has bits set outside
7398          * of the mask, but don't bother drivers with having to deal
7399          * with such bits
7400          */
7401         for (i = 0; i < ETH_ALEN; i++)
7402                 mac_addr[i] &= mac_addr_mask[i];
7403
7404         return 0;
7405 }
7406
7407 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
7408 {
7409         ASSERT_WDEV_LOCK(wdev);
7410
7411         if (!cfg80211_beaconing_iface_active(wdev))
7412                 return true;
7413
7414         if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
7415                 return true;
7416
7417         return regulatory_pre_cac_allowed(wdev->wiphy);
7418 }
7419
7420 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
7421                                     enum nl80211_ext_feature_index feat)
7422 {
7423         if (!(flags & flag))
7424                 return true;
7425         if (wiphy_ext_feature_isset(wiphy, feat))
7426                 return true;
7427         return false;
7428 }
7429
7430 static int
7431 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
7432                          void *request, struct nlattr **attrs,
7433                          bool is_sched_scan)
7434 {
7435         u8 *mac_addr, *mac_addr_mask;
7436         u32 *flags;
7437         enum nl80211_feature_flags randomness_flag;
7438
7439         if (!attrs[NL80211_ATTR_SCAN_FLAGS])
7440                 return 0;
7441
7442         if (is_sched_scan) {
7443                 struct cfg80211_sched_scan_request *req = request;
7444
7445                 randomness_flag = wdev ?
7446                                   NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
7447                                   NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7448                 flags = &req->flags;
7449                 mac_addr = req->mac_addr;
7450                 mac_addr_mask = req->mac_addr_mask;
7451         } else {
7452                 struct cfg80211_scan_request *req = request;
7453
7454                 randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
7455                 flags = &req->flags;
7456                 mac_addr = req->mac_addr;
7457                 mac_addr_mask = req->mac_addr_mask;
7458         }
7459
7460         *flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
7461
7462         if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7463              !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
7464             !nl80211_check_scan_feat(wiphy, *flags,
7465                                      NL80211_SCAN_FLAG_LOW_SPAN,
7466                                      NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
7467             !nl80211_check_scan_feat(wiphy, *flags,
7468                                      NL80211_SCAN_FLAG_LOW_POWER,
7469                                      NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
7470             !nl80211_check_scan_feat(wiphy, *flags,
7471                                      NL80211_SCAN_FLAG_HIGH_ACCURACY,
7472                                      NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
7473             !nl80211_check_scan_feat(wiphy, *flags,
7474                                      NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
7475                                      NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
7476             !nl80211_check_scan_feat(wiphy, *flags,
7477                                      NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
7478                                      NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
7479             !nl80211_check_scan_feat(wiphy, *flags,
7480                                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
7481                                      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
7482             !nl80211_check_scan_feat(wiphy, *flags,
7483                                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
7484                                      NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
7485             !nl80211_check_scan_feat(wiphy, *flags,
7486                                      NL80211_SCAN_FLAG_RANDOM_SN,
7487                                      NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
7488             !nl80211_check_scan_feat(wiphy, *flags,
7489                                      NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
7490                                      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
7491                 return -EOPNOTSUPP;
7492
7493         if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7494                 int err;
7495
7496                 if (!(wiphy->features & randomness_flag) ||
7497                     (wdev && wdev->current_bss))
7498                         return -EOPNOTSUPP;
7499
7500                 err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
7501                 if (err)
7502                         return err;
7503         }
7504
7505         return 0;
7506 }
7507
7508 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
7509 {
7510         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7511         struct wireless_dev *wdev = info->user_ptr[1];
7512         struct cfg80211_scan_request *request;
7513         struct nlattr *attr;
7514         struct wiphy *wiphy;
7515         int err, tmp, n_ssids = 0, n_channels, i;
7516         size_t ie_len;
7517
7518         wiphy = &rdev->wiphy;
7519
7520         if (wdev->iftype == NL80211_IFTYPE_NAN)
7521                 return -EOPNOTSUPP;
7522
7523         if (!rdev->ops->scan)
7524                 return -EOPNOTSUPP;
7525
7526         if (rdev->scan_req || rdev->scan_msg) {
7527                 err = -EBUSY;
7528                 goto unlock;
7529         }
7530
7531         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7532                 n_channels = validate_scan_freqs(
7533                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7534                 if (!n_channels) {
7535                         err = -EINVAL;
7536                         goto unlock;
7537                 }
7538         } else {
7539                 n_channels = ieee80211_get_num_supported_channels(wiphy);
7540         }
7541
7542         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
7543                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
7544                         n_ssids++;
7545
7546         if (n_ssids > wiphy->max_scan_ssids) {
7547                 err = -EINVAL;
7548                 goto unlock;
7549         }
7550
7551         if (info->attrs[NL80211_ATTR_IE])
7552                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7553         else
7554                 ie_len = 0;
7555
7556         if (ie_len > wiphy->max_scan_ie_len) {
7557                 err = -EINVAL;
7558                 goto unlock;
7559         }
7560
7561         request = kzalloc(sizeof(*request)
7562                         + sizeof(*request->ssids) * n_ssids
7563                         + sizeof(*request->channels) * n_channels
7564                         + ie_len, GFP_KERNEL);
7565         if (!request) {
7566                 err = -ENOMEM;
7567                 goto unlock;
7568         }
7569
7570         if (n_ssids)
7571                 request->ssids = (void *)&request->channels[n_channels];
7572         request->n_ssids = n_ssids;
7573         if (ie_len) {
7574                 if (n_ssids)
7575                         request->ie = (void *)(request->ssids + n_ssids);
7576                 else
7577                         request->ie = (void *)(request->channels + n_channels);
7578         }
7579
7580         i = 0;
7581         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7582                 /* user specified, bail out if channel not found */
7583                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
7584                         struct ieee80211_channel *chan;
7585
7586                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7587
7588                         if (!chan) {
7589                                 err = -EINVAL;
7590                                 goto out_free;
7591                         }
7592
7593                         /* ignore disabled channels */
7594                         if (chan->flags & IEEE80211_CHAN_DISABLED)
7595                                 continue;
7596
7597                         request->channels[i] = chan;
7598                         i++;
7599                 }
7600         } else {
7601                 enum nl80211_band band;
7602
7603                 /* all channels */
7604                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7605                         int j;
7606
7607                         if (!wiphy->bands[band])
7608                                 continue;
7609                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7610                                 struct ieee80211_channel *chan;
7611
7612                                 chan = &wiphy->bands[band]->channels[j];
7613
7614                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
7615                                         continue;
7616
7617                                 request->channels[i] = chan;
7618                                 i++;
7619                         }
7620                 }
7621         }
7622
7623         if (!i) {
7624                 err = -EINVAL;
7625                 goto out_free;
7626         }
7627
7628         request->n_channels = i;
7629
7630         wdev_lock(wdev);
7631         if (!cfg80211_off_channel_oper_allowed(wdev)) {
7632                 struct ieee80211_channel *chan;
7633
7634                 if (request->n_channels != 1) {
7635                         wdev_unlock(wdev);
7636                         err = -EBUSY;
7637                         goto out_free;
7638                 }
7639
7640                 chan = request->channels[0];
7641                 if (chan->center_freq != wdev->chandef.chan->center_freq) {
7642                         wdev_unlock(wdev);
7643                         err = -EBUSY;
7644                         goto out_free;
7645                 }
7646         }
7647         wdev_unlock(wdev);
7648
7649         i = 0;
7650         if (n_ssids) {
7651                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
7652                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7653                                 err = -EINVAL;
7654                                 goto out_free;
7655                         }
7656                         request->ssids[i].ssid_len = nla_len(attr);
7657                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
7658                         i++;
7659                 }
7660         }
7661
7662         if (info->attrs[NL80211_ATTR_IE]) {
7663                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
7664                 memcpy((void *)request->ie,
7665                        nla_data(info->attrs[NL80211_ATTR_IE]),
7666                        request->ie_len);
7667         }
7668
7669         for (i = 0; i < NUM_NL80211_BANDS; i++)
7670                 if (wiphy->bands[i])
7671                         request->rates[i] =
7672                                 (1 << wiphy->bands[i]->n_bitrates) - 1;
7673
7674         if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
7675                 nla_for_each_nested(attr,
7676                                     info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
7677                                     tmp) {
7678                         enum nl80211_band band = nla_type(attr);
7679
7680                         if (band < 0 || band >= NUM_NL80211_BANDS) {
7681                                 err = -EINVAL;
7682                                 goto out_free;
7683                         }
7684
7685                         if (!wiphy->bands[band])
7686                                 continue;
7687
7688                         err = ieee80211_get_ratemask(wiphy->bands[band],
7689                                                      nla_data(attr),
7690                                                      nla_len(attr),
7691                                                      &request->rates[band]);
7692                         if (err)
7693                                 goto out_free;
7694                 }
7695         }
7696
7697         if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
7698                 if (!wiphy_ext_feature_isset(wiphy,
7699                                         NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
7700                         err = -EOPNOTSUPP;
7701                         goto out_free;
7702                 }
7703
7704                 request->duration =
7705                         nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
7706                 request->duration_mandatory =
7707                         nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
7708         }
7709
7710         err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
7711                                        false);
7712         if (err)
7713                 goto out_free;
7714
7715         request->no_cck =
7716                 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7717
7718         /* Initial implementation used NL80211_ATTR_MAC to set the specific
7719          * BSSID to scan for. This was problematic because that same attribute
7720          * was already used for another purpose (local random MAC address). The
7721          * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
7722          * compatibility with older userspace components, also use the
7723          * NL80211_ATTR_MAC value here if it can be determined to be used for
7724          * the specific BSSID use case instead of the random MAC address
7725          * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
7726          */
7727         if (info->attrs[NL80211_ATTR_BSSID])
7728                 memcpy(request->bssid,
7729                        nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
7730         else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
7731                  info->attrs[NL80211_ATTR_MAC])
7732                 memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
7733                        ETH_ALEN);
7734         else
7735                 eth_broadcast_addr(request->bssid);
7736
7737         request->wdev = wdev;
7738         request->wiphy = &rdev->wiphy;
7739         request->scan_start = jiffies;
7740
7741         rdev->scan_req = request;
7742         err = rdev_scan(rdev, request);
7743
7744         if (!err) {
7745                 nl80211_send_scan_start(rdev, wdev);
7746                 if (wdev->netdev)
7747                         dev_hold(wdev->netdev);
7748         } else {
7749  out_free:
7750                 rdev->scan_req = NULL;
7751                 kfree(request);
7752         }
7753
7754  unlock:
7755         return err;
7756 }
7757
7758 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
7759 {
7760         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7761         struct wireless_dev *wdev = info->user_ptr[1];
7762
7763         if (!rdev->ops->abort_scan)
7764                 return -EOPNOTSUPP;
7765
7766         if (rdev->scan_msg)
7767                 return 0;
7768
7769         if (!rdev->scan_req)
7770                 return -ENOENT;
7771
7772         rdev_abort_scan(rdev, wdev);
7773         return 0;
7774 }
7775
7776 static int
7777 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
7778                                struct cfg80211_sched_scan_request *request,
7779                                struct nlattr **attrs)
7780 {
7781         int tmp, err, i = 0;
7782         struct nlattr *attr;
7783
7784         if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7785                 u32 interval;
7786
7787                 /*
7788                  * If scan plans are not specified,
7789                  * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
7790                  * case one scan plan will be set with the specified scan
7791                  * interval and infinite number of iterations.
7792                  */
7793                 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
7794                 if (!interval)
7795                         return -EINVAL;
7796
7797                 request->scan_plans[0].interval =
7798                         DIV_ROUND_UP(interval, MSEC_PER_SEC);
7799                 if (!request->scan_plans[0].interval)
7800                         return -EINVAL;
7801
7802                 if (request->scan_plans[0].interval >
7803                     wiphy->max_sched_scan_plan_interval)
7804                         request->scan_plans[0].interval =
7805                                 wiphy->max_sched_scan_plan_interval;
7806
7807                 return 0;
7808         }
7809
7810         nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
7811                 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7812
7813                 if (WARN_ON(i >= n_plans))
7814                         return -EINVAL;
7815
7816                 err = nla_parse_nested_deprecated(plan,
7817                                                   NL80211_SCHED_SCAN_PLAN_MAX,
7818                                                   attr, nl80211_plan_policy,
7819                                                   NULL);
7820                 if (err)
7821                         return err;
7822
7823                 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7824                         return -EINVAL;
7825
7826                 request->scan_plans[i].interval =
7827                         nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7828                 if (!request->scan_plans[i].interval ||
7829                     request->scan_plans[i].interval >
7830                     wiphy->max_sched_scan_plan_interval)
7831                         return -EINVAL;
7832
7833                 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7834                         request->scan_plans[i].iterations =
7835                                 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7836                         if (!request->scan_plans[i].iterations ||
7837                             (request->scan_plans[i].iterations >
7838                              wiphy->max_sched_scan_plan_iterations))
7839                                 return -EINVAL;
7840                 } else if (i < n_plans - 1) {
7841                         /*
7842                          * All scan plans but the last one must specify
7843                          * a finite number of iterations
7844                          */
7845                         return -EINVAL;
7846                 }
7847
7848                 i++;
7849         }
7850
7851         /*
7852          * The last scan plan must not specify the number of
7853          * iterations, it is supposed to run infinitely
7854          */
7855         if (request->scan_plans[n_plans - 1].iterations)
7856                 return  -EINVAL;
7857
7858         return 0;
7859 }
7860
7861 static int
7862 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
7863                                        struct cfg80211_match_set *match_sets,
7864                                        struct nlattr *tb_band_rssi,
7865                                        s32 rssi_thold)
7866 {
7867         struct nlattr *attr;
7868         int i, tmp, ret = 0;
7869
7870         if (!wiphy_ext_feature_isset(wiphy,
7871                     NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
7872                 if (tb_band_rssi)
7873                         ret = -EOPNOTSUPP;
7874                 else
7875                         for (i = 0; i < NUM_NL80211_BANDS; i++)
7876                                 match_sets->per_band_rssi_thold[i] =
7877                                         NL80211_SCAN_RSSI_THOLD_OFF;
7878                 return ret;
7879         }
7880
7881         for (i = 0; i < NUM_NL80211_BANDS; i++)
7882                 match_sets->per_band_rssi_thold[i] = rssi_thold;
7883
7884         nla_for_each_nested(attr, tb_band_rssi, tmp) {
7885                 enum nl80211_band band = nla_type(attr);
7886
7887                 if (band < 0 || band >= NUM_NL80211_BANDS)
7888                         return -EINVAL;
7889
7890                 match_sets->per_band_rssi_thold[band] = nla_get_s32(attr);
7891         }
7892
7893         return 0;
7894 }
7895
7896 static struct cfg80211_sched_scan_request *
7897 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7898                          struct nlattr **attrs, int max_match_sets)
7899 {
7900         struct cfg80211_sched_scan_request *request;
7901         struct nlattr *attr;
7902         int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7903         enum nl80211_band band;
7904         size_t ie_len;
7905         struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7906         s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7907
7908         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7909                 n_channels = validate_scan_freqs(
7910                                 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7911                 if (!n_channels)
7912                         return ERR_PTR(-EINVAL);
7913         } else {
7914                 n_channels = ieee80211_get_num_supported_channels(wiphy);
7915         }
7916
7917         if (attrs[NL80211_ATTR_SCAN_SSIDS])
7918                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7919                                     tmp)
7920                         n_ssids++;
7921
7922         if (n_ssids > wiphy->max_sched_scan_ssids)
7923                 return ERR_PTR(-EINVAL);
7924
7925         /*
7926          * First, count the number of 'real' matchsets. Due to an issue with
7927          * the old implementation, matchsets containing only the RSSI attribute
7928          * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7929          * RSSI for all matchsets, rather than their own matchset for reporting
7930          * all APs with a strong RSSI. This is needed to be compatible with
7931          * older userspace that treated a matchset with only the RSSI as the
7932          * global RSSI for all other matchsets - if there are other matchsets.
7933          */
7934         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7935                 nla_for_each_nested(attr,
7936                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7937                                     tmp) {
7938                         struct nlattr *rssi;
7939
7940                         err = nla_parse_nested_deprecated(tb,
7941                                                           NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7942                                                           attr,
7943                                                           nl80211_match_policy,
7944                                                           NULL);
7945                         if (err)
7946                                 return ERR_PTR(err);
7947
7948                         /* SSID and BSSID are mutually exclusive */
7949                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7950                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7951                                 return ERR_PTR(-EINVAL);
7952
7953                         /* add other standalone attributes here */
7954                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7955                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7956                                 n_match_sets++;
7957                                 continue;
7958                         }
7959                         rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7960                         if (rssi)
7961                                 default_match_rssi = nla_get_s32(rssi);
7962                 }
7963         }
7964
7965         /* However, if there's no other matchset, add the RSSI one */
7966         if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7967                 n_match_sets = 1;
7968
7969         if (n_match_sets > max_match_sets)
7970                 return ERR_PTR(-EINVAL);
7971
7972         if (attrs[NL80211_ATTR_IE])
7973                 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7974         else
7975                 ie_len = 0;
7976
7977         if (ie_len > wiphy->max_sched_scan_ie_len)
7978                 return ERR_PTR(-EINVAL);
7979
7980         if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7981                 /*
7982                  * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7983                  * each scan plan already specifies its own interval
7984                  */
7985                 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7986                         return ERR_PTR(-EINVAL);
7987
7988                 nla_for_each_nested(attr,
7989                                     attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7990                         n_plans++;
7991         } else {
7992                 /*
7993                  * The scan interval attribute is kept for backward
7994                  * compatibility. If no scan plans are specified and sched scan
7995                  * interval is specified, one scan plan will be set with this
7996                  * scan interval and infinite number of iterations.
7997                  */
7998                 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7999                         return ERR_PTR(-EINVAL);
8000
8001                 n_plans = 1;
8002         }
8003
8004         if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
8005                 return ERR_PTR(-EINVAL);
8006
8007         if (!wiphy_ext_feature_isset(
8008                     wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
8009             (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
8010              attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
8011                 return ERR_PTR(-EINVAL);
8012
8013         request = kzalloc(sizeof(*request)
8014                         + sizeof(*request->ssids) * n_ssids
8015                         + sizeof(*request->match_sets) * n_match_sets
8016                         + sizeof(*request->scan_plans) * n_plans
8017                         + sizeof(*request->channels) * n_channels
8018                         + ie_len, GFP_KERNEL);
8019         if (!request)
8020                 return ERR_PTR(-ENOMEM);
8021
8022         if (n_ssids)
8023                 request->ssids = (void *)&request->channels[n_channels];
8024         request->n_ssids = n_ssids;
8025         if (ie_len) {
8026                 if (n_ssids)
8027                         request->ie = (void *)(request->ssids + n_ssids);
8028                 else
8029                         request->ie = (void *)(request->channels + n_channels);
8030         }
8031
8032         if (n_match_sets) {
8033                 if (request->ie)
8034                         request->match_sets = (void *)(request->ie + ie_len);
8035                 else if (n_ssids)
8036                         request->match_sets =
8037                                 (void *)(request->ssids + n_ssids);
8038                 else
8039                         request->match_sets =
8040                                 (void *)(request->channels + n_channels);
8041         }
8042         request->n_match_sets = n_match_sets;
8043
8044         if (n_match_sets)
8045                 request->scan_plans = (void *)(request->match_sets +
8046                                                n_match_sets);
8047         else if (request->ie)
8048                 request->scan_plans = (void *)(request->ie + ie_len);
8049         else if (n_ssids)
8050                 request->scan_plans = (void *)(request->ssids + n_ssids);
8051         else
8052                 request->scan_plans = (void *)(request->channels + n_channels);
8053
8054         request->n_scan_plans = n_plans;
8055
8056         i = 0;
8057         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
8058                 /* user specified, bail out if channel not found */
8059                 nla_for_each_nested(attr,
8060                                     attrs[NL80211_ATTR_SCAN_FREQUENCIES],
8061                                     tmp) {
8062                         struct ieee80211_channel *chan;
8063
8064                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
8065
8066                         if (!chan) {
8067                                 err = -EINVAL;
8068                                 goto out_free;
8069                         }
8070
8071                         /* ignore disabled channels */
8072                         if (chan->flags & IEEE80211_CHAN_DISABLED)
8073                                 continue;
8074
8075                         request->channels[i] = chan;
8076                         i++;
8077                 }
8078         } else {
8079                 /* all channels */
8080                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
8081                         int j;
8082
8083                         if (!wiphy->bands[band])
8084                                 continue;
8085                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
8086                                 struct ieee80211_channel *chan;
8087
8088                                 chan = &wiphy->bands[band]->channels[j];
8089
8090                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
8091                                         continue;
8092
8093                                 request->channels[i] = chan;
8094                                 i++;
8095                         }
8096                 }
8097         }
8098
8099         if (!i) {
8100                 err = -EINVAL;
8101                 goto out_free;
8102         }
8103
8104         request->n_channels = i;
8105
8106         i = 0;
8107         if (n_ssids) {
8108                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
8109                                     tmp) {
8110                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
8111                                 err = -EINVAL;
8112                                 goto out_free;
8113                         }
8114                         request->ssids[i].ssid_len = nla_len(attr);
8115                         memcpy(request->ssids[i].ssid, nla_data(attr),
8116                                nla_len(attr));
8117                         i++;
8118                 }
8119         }
8120
8121         i = 0;
8122         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
8123                 nla_for_each_nested(attr,
8124                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
8125                                     tmp) {
8126                         struct nlattr *ssid, *bssid, *rssi;
8127
8128                         err = nla_parse_nested_deprecated(tb,
8129                                                           NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
8130                                                           attr,
8131                                                           nl80211_match_policy,
8132                                                           NULL);
8133                         if (err)
8134                                 goto out_free;
8135                         ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
8136                         bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
8137
8138                         if (!ssid && !bssid) {
8139                                 i++;
8140                                 continue;
8141                         }
8142
8143                         if (WARN_ON(i >= n_match_sets)) {
8144                                 /* this indicates a programming error,
8145                                  * the loop above should have verified
8146                                  * things properly
8147                                  */
8148                                 err = -EINVAL;
8149                                 goto out_free;
8150                         }
8151
8152                         if (ssid) {
8153                                 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
8154                                         err = -EINVAL;
8155                                         goto out_free;
8156                                 }
8157                                 memcpy(request->match_sets[i].ssid.ssid,
8158                                        nla_data(ssid), nla_len(ssid));
8159                                 request->match_sets[i].ssid.ssid_len =
8160                                         nla_len(ssid);
8161                         }
8162                         if (bssid) {
8163                                 if (nla_len(bssid) != ETH_ALEN) {
8164                                         err = -EINVAL;
8165                                         goto out_free;
8166                                 }
8167                                 memcpy(request->match_sets[i].bssid,
8168                                        nla_data(bssid), ETH_ALEN);
8169                         }
8170
8171                         /* special attribute - old implementation w/a */
8172                         request->match_sets[i].rssi_thold = default_match_rssi;
8173                         rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
8174                         if (rssi)
8175                                 request->match_sets[i].rssi_thold =
8176                                         nla_get_s32(rssi);
8177
8178                         /* Parse per band RSSI attribute */
8179                         err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
8180                                 &request->match_sets[i],
8181                                 tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
8182                                 request->match_sets[i].rssi_thold);
8183                         if (err)
8184                                 goto out_free;
8185
8186                         i++;
8187                 }
8188
8189                 /* there was no other matchset, so the RSSI one is alone */
8190                 if (i == 0 && n_match_sets)
8191                         request->match_sets[0].rssi_thold = default_match_rssi;
8192
8193                 request->min_rssi_thold = INT_MAX;
8194                 for (i = 0; i < n_match_sets; i++)
8195                         request->min_rssi_thold =
8196                                 min(request->match_sets[i].rssi_thold,
8197                                     request->min_rssi_thold);
8198         } else {
8199                 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
8200         }
8201
8202         if (ie_len) {
8203                 request->ie_len = ie_len;
8204                 memcpy((void *)request->ie,
8205                        nla_data(attrs[NL80211_ATTR_IE]),
8206                        request->ie_len);
8207         }
8208
8209         err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
8210         if (err)
8211                 goto out_free;
8212
8213         if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
8214                 request->delay =
8215                         nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
8216
8217         if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
8218                 request->relative_rssi = nla_get_s8(
8219                         attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
8220                 request->relative_rssi_set = true;
8221         }
8222
8223         if (request->relative_rssi_set &&
8224             attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
8225                 struct nl80211_bss_select_rssi_adjust *rssi_adjust;
8226
8227                 rssi_adjust = nla_data(
8228                         attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
8229                 request->rssi_adjust.band = rssi_adjust->band;
8230                 request->rssi_adjust.delta = rssi_adjust->delta;
8231                 if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
8232                         err = -EINVAL;
8233                         goto out_free;
8234                 }
8235         }
8236
8237         err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
8238         if (err)
8239                 goto out_free;
8240
8241         request->scan_start = jiffies;
8242
8243         return request;
8244
8245 out_free:
8246         kfree(request);
8247         return ERR_PTR(err);
8248 }
8249
8250 static int nl80211_start_sched_scan(struct sk_buff *skb,
8251                                     struct genl_info *info)
8252 {
8253         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8254         struct net_device *dev = info->user_ptr[1];
8255         struct wireless_dev *wdev = dev->ieee80211_ptr;
8256         struct cfg80211_sched_scan_request *sched_scan_req;
8257         bool want_multi;
8258         int err;
8259
8260         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
8261                 return -EOPNOTSUPP;
8262
8263         want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
8264         err = cfg80211_sched_scan_req_possible(rdev, want_multi);
8265         if (err)
8266                 return err;
8267
8268         sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
8269                                                   info->attrs,
8270                                                   rdev->wiphy.max_match_sets);
8271
8272         err = PTR_ERR_OR_ZERO(sched_scan_req);
8273         if (err)
8274                 goto out_err;
8275
8276         /* leave request id zero for legacy request
8277          * or if driver does not support multi-scheduled scan
8278          */
8279         if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
8280                 sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
8281
8282         err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
8283         if (err)
8284                 goto out_free;
8285
8286         sched_scan_req->dev = dev;
8287         sched_scan_req->wiphy = &rdev->wiphy;
8288
8289         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
8290                 sched_scan_req->owner_nlportid = info->snd_portid;
8291
8292         cfg80211_add_sched_scan_req(rdev, sched_scan_req);
8293
8294         nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
8295         return 0;
8296
8297 out_free:
8298         kfree(sched_scan_req);
8299 out_err:
8300         return err;
8301 }
8302
8303 static int nl80211_stop_sched_scan(struct sk_buff *skb,
8304                                    struct genl_info *info)
8305 {
8306         struct cfg80211_sched_scan_request *req;
8307         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8308         u64 cookie;
8309
8310         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
8311                 return -EOPNOTSUPP;
8312
8313         if (info->attrs[NL80211_ATTR_COOKIE]) {
8314                 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
8315                 return __cfg80211_stop_sched_scan(rdev, cookie, false);
8316         }
8317
8318         req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
8319                                      struct cfg80211_sched_scan_request,
8320                                      list);
8321         if (!req || req->reqid ||
8322             (req->owner_nlportid &&
8323              req->owner_nlportid != info->snd_portid))
8324                 return -ENOENT;
8325
8326         return cfg80211_stop_sched_scan_req(rdev, req, false);
8327 }
8328
8329 static int nl80211_start_radar_detection(struct sk_buff *skb,
8330                                          struct genl_info *info)
8331 {
8332         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8333         struct net_device *dev = info->user_ptr[1];
8334         struct wireless_dev *wdev = dev->ieee80211_ptr;
8335         struct wiphy *wiphy = wdev->wiphy;
8336         struct cfg80211_chan_def chandef;
8337         enum nl80211_dfs_regions dfs_region;
8338         unsigned int cac_time_ms;
8339         int err;
8340
8341         dfs_region = reg_get_dfs_region(wiphy);
8342         if (dfs_region == NL80211_DFS_UNSET)
8343                 return -EINVAL;
8344
8345         err = nl80211_parse_chandef(rdev, info, &chandef);
8346         if (err)
8347                 return err;
8348
8349         if (netif_carrier_ok(dev))
8350                 return -EBUSY;
8351
8352         if (wdev->cac_started)
8353                 return -EBUSY;
8354
8355         err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8356         if (err < 0)
8357                 return err;
8358
8359         if (err == 0)
8360                 return -EINVAL;
8361
8362         if (!cfg80211_chandef_dfs_usable(wiphy, &chandef))
8363                 return -EINVAL;
8364
8365         /* CAC start is offloaded to HW and can't be started manually */
8366         if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD))
8367                 return -EOPNOTSUPP;
8368
8369         if (!rdev->ops->start_radar_detection)
8370                 return -EOPNOTSUPP;
8371
8372         cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
8373         if (WARN_ON(!cac_time_ms))
8374                 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
8375
8376         err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
8377         if (!err) {
8378                 wdev->chandef = chandef;
8379                 wdev->cac_started = true;
8380                 wdev->cac_start_time = jiffies;
8381                 wdev->cac_time_ms = cac_time_ms;
8382         }
8383         return err;
8384 }
8385
8386 static int nl80211_notify_radar_detection(struct sk_buff *skb,
8387                                           struct genl_info *info)
8388 {
8389         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8390         struct net_device *dev = info->user_ptr[1];
8391         struct wireless_dev *wdev = dev->ieee80211_ptr;
8392         struct wiphy *wiphy = wdev->wiphy;
8393         struct cfg80211_chan_def chandef;
8394         enum nl80211_dfs_regions dfs_region;
8395         int err;
8396
8397         dfs_region = reg_get_dfs_region(wiphy);
8398         if (dfs_region == NL80211_DFS_UNSET) {
8399                 GENL_SET_ERR_MSG(info,
8400                                  "DFS Region is not set. Unexpected Radar indication");
8401                 return -EINVAL;
8402         }
8403
8404         err = nl80211_parse_chandef(rdev, info, &chandef);
8405         if (err) {
8406                 GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
8407                 return err;
8408         }
8409
8410         err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
8411         if (err < 0) {
8412                 GENL_SET_ERR_MSG(info, "chandef is invalid");
8413                 return err;
8414         }
8415
8416         if (err == 0) {
8417                 GENL_SET_ERR_MSG(info,
8418                                  "Unexpected Radar indication for chandef/iftype");
8419                 return -EINVAL;
8420         }
8421
8422         /* Do not process this notification if radar is already detected
8423          * by kernel on this channel, and return success.
8424          */
8425         if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
8426                 return 0;
8427
8428         cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
8429
8430         cfg80211_sched_dfs_chan_update(rdev);
8431
8432         rdev->radar_chandef = chandef;
8433
8434         /* Propagate this notification to other radios as well */
8435         queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
8436
8437         return 0;
8438 }
8439
8440 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
8441 {
8442         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8443         struct net_device *dev = info->user_ptr[1];
8444         struct wireless_dev *wdev = dev->ieee80211_ptr;
8445         struct cfg80211_csa_settings params;
8446         /* csa_attrs is defined static to avoid waste of stack size - this
8447          * function is called under RTNL lock, so this should not be a problem.
8448          */
8449         static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
8450         int err;
8451         bool need_new_beacon = false;
8452         bool need_handle_dfs_flag = true;
8453         int len, i;
8454         u32 cs_count;
8455
8456         if (!rdev->ops->channel_switch ||
8457             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
8458                 return -EOPNOTSUPP;
8459
8460         switch (dev->ieee80211_ptr->iftype) {
8461         case NL80211_IFTYPE_AP:
8462         case NL80211_IFTYPE_P2P_GO:
8463                 need_new_beacon = true;
8464                 /* For all modes except AP the handle_dfs flag needs to be
8465                  * supplied to tell the kernel that userspace will handle radar
8466                  * events when they happen. Otherwise a switch to a channel
8467                  * requiring DFS will be rejected.
8468                  */
8469                 need_handle_dfs_flag = false;
8470
8471                 /* useless if AP is not running */
8472                 if (!wdev->beacon_interval)
8473                         return -ENOTCONN;
8474                 break;
8475         case NL80211_IFTYPE_ADHOC:
8476                 if (!wdev->ssid_len)
8477                         return -ENOTCONN;
8478                 break;
8479         case NL80211_IFTYPE_MESH_POINT:
8480                 if (!wdev->mesh_id_len)
8481                         return -ENOTCONN;
8482                 break;
8483         default:
8484                 return -EOPNOTSUPP;
8485         }
8486
8487         memset(&params, 0, sizeof(params));
8488         params.beacon_csa.ftm_responder = -1;
8489
8490         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
8491             !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
8492                 return -EINVAL;
8493
8494         /* only important for AP, IBSS and mesh create IEs internally */
8495         if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
8496                 return -EINVAL;
8497
8498         /* Even though the attribute is u32, the specification says
8499          * u8, so let's make sure we don't overflow.
8500          */
8501         cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
8502         if (cs_count > 255)
8503                 return -EINVAL;
8504
8505         params.count = cs_count;
8506
8507         if (!need_new_beacon)
8508                 goto skip_beacons;
8509
8510         err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
8511         if (err)
8512                 return err;
8513
8514         err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
8515                                           info->attrs[NL80211_ATTR_CSA_IES],
8516                                           nl80211_policy, info->extack);
8517         if (err)
8518                 return err;
8519
8520         err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
8521         if (err)
8522                 return err;
8523
8524         if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
8525                 return -EINVAL;
8526
8527         len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8528         if (!len || (len % sizeof(u16)))
8529                 return -EINVAL;
8530
8531         params.n_counter_offsets_beacon = len / sizeof(u16);
8532         if (rdev->wiphy.max_num_csa_counters &&
8533             (params.n_counter_offsets_beacon >
8534              rdev->wiphy.max_num_csa_counters))
8535                 return -EINVAL;
8536
8537         params.counter_offsets_beacon =
8538                 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
8539
8540         /* sanity checks - counters should fit and be the same */
8541         for (i = 0; i < params.n_counter_offsets_beacon; i++) {
8542                 u16 offset = params.counter_offsets_beacon[i];
8543
8544                 if (offset >= params.beacon_csa.tail_len)
8545                         return -EINVAL;
8546
8547                 if (params.beacon_csa.tail[offset] != params.count)
8548                         return -EINVAL;
8549         }
8550
8551         if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
8552                 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8553                 if (!len || (len % sizeof(u16)))
8554                         return -EINVAL;
8555
8556                 params.n_counter_offsets_presp = len / sizeof(u16);
8557                 if (rdev->wiphy.max_num_csa_counters &&
8558                     (params.n_counter_offsets_presp >
8559                      rdev->wiphy.max_num_csa_counters))
8560                         return -EINVAL;
8561
8562                 params.counter_offsets_presp =
8563                         nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
8564
8565                 /* sanity checks - counters should fit and be the same */
8566                 for (i = 0; i < params.n_counter_offsets_presp; i++) {
8567                         u16 offset = params.counter_offsets_presp[i];
8568
8569                         if (offset >= params.beacon_csa.probe_resp_len)
8570                                 return -EINVAL;
8571
8572                         if (params.beacon_csa.probe_resp[offset] !=
8573                             params.count)
8574                                 return -EINVAL;
8575                 }
8576         }
8577
8578 skip_beacons:
8579         err = nl80211_parse_chandef(rdev, info, &params.chandef);
8580         if (err)
8581                 return err;
8582
8583         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
8584                                            wdev->iftype))
8585                 return -EINVAL;
8586
8587         err = cfg80211_chandef_dfs_required(wdev->wiphy,
8588                                             &params.chandef,
8589                                             wdev->iftype);
8590         if (err < 0)
8591                 return err;
8592
8593         if (err > 0) {
8594                 params.radar_required = true;
8595                 if (need_handle_dfs_flag &&
8596                     !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
8597                         return -EINVAL;
8598                 }
8599         }
8600
8601         if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
8602                 params.block_tx = true;
8603
8604         wdev_lock(wdev);
8605         err = rdev_channel_switch(rdev, dev, &params);
8606         wdev_unlock(wdev);
8607
8608         return err;
8609 }
8610
8611 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
8612                             u32 seq, int flags,
8613                             struct cfg80211_registered_device *rdev,
8614                             struct wireless_dev *wdev,
8615                             struct cfg80211_internal_bss *intbss)
8616 {
8617         struct cfg80211_bss *res = &intbss->pub;
8618         const struct cfg80211_bss_ies *ies;
8619         void *hdr;
8620         struct nlattr *bss;
8621
8622         ASSERT_WDEV_LOCK(wdev);
8623
8624         hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8625                              NL80211_CMD_NEW_SCAN_RESULTS);
8626         if (!hdr)
8627                 return -1;
8628
8629         genl_dump_check_consistent(cb, hdr);
8630
8631         if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
8632                 goto nla_put_failure;
8633         if (wdev->netdev &&
8634             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
8635                 goto nla_put_failure;
8636         if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
8637                               NL80211_ATTR_PAD))
8638                 goto nla_put_failure;
8639
8640         bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
8641         if (!bss)
8642                 goto nla_put_failure;
8643         if ((!is_zero_ether_addr(res->bssid) &&
8644              nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
8645                 goto nla_put_failure;
8646
8647         rcu_read_lock();
8648         /* indicate whether we have probe response data or not */
8649         if (rcu_access_pointer(res->proberesp_ies) &&
8650             nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
8651                 goto fail_unlock_rcu;
8652
8653         /* this pointer prefers to be pointed to probe response data
8654          * but is always valid
8655          */
8656         ies = rcu_dereference(res->ies);
8657         if (ies) {
8658                 if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
8659                                       NL80211_BSS_PAD))
8660                         goto fail_unlock_rcu;
8661                 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
8662                                         ies->len, ies->data))
8663                         goto fail_unlock_rcu;
8664         }
8665
8666         /* and this pointer is always (unless driver didn't know) beacon data */
8667         ies = rcu_dereference(res->beacon_ies);
8668         if (ies && ies->from_beacon) {
8669                 if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
8670                                       NL80211_BSS_PAD))
8671                         goto fail_unlock_rcu;
8672                 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
8673                                         ies->len, ies->data))
8674                         goto fail_unlock_rcu;
8675         }
8676         rcu_read_unlock();
8677
8678         if (res->beacon_interval &&
8679             nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
8680                 goto nla_put_failure;
8681         if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
8682             nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
8683             nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
8684             nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
8685                         jiffies_to_msecs(jiffies - intbss->ts)))
8686                 goto nla_put_failure;
8687
8688         if (intbss->parent_tsf &&
8689             (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
8690                                intbss->parent_tsf, NL80211_BSS_PAD) ||
8691              nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
8692                      intbss->parent_bssid)))
8693                 goto nla_put_failure;
8694
8695         if (intbss->ts_boottime &&
8696             nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
8697                               intbss->ts_boottime, NL80211_BSS_PAD))
8698                 goto nla_put_failure;
8699
8700         if (!nl80211_put_signal(msg, intbss->pub.chains,
8701                                 intbss->pub.chain_signal,
8702                                 NL80211_BSS_CHAIN_SIGNAL))
8703                 goto nla_put_failure;
8704
8705         switch (rdev->wiphy.signal_type) {
8706         case CFG80211_SIGNAL_TYPE_MBM:
8707                 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
8708                         goto nla_put_failure;
8709                 break;
8710         case CFG80211_SIGNAL_TYPE_UNSPEC:
8711                 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
8712                         goto nla_put_failure;
8713                 break;
8714         default:
8715                 break;
8716         }
8717
8718         switch (wdev->iftype) {
8719         case NL80211_IFTYPE_P2P_CLIENT:
8720         case NL80211_IFTYPE_STATION:
8721                 if (intbss == wdev->current_bss &&
8722                     nla_put_u32(msg, NL80211_BSS_STATUS,
8723                                 NL80211_BSS_STATUS_ASSOCIATED))
8724                         goto nla_put_failure;
8725                 break;
8726         case NL80211_IFTYPE_ADHOC:
8727                 if (intbss == wdev->current_bss &&
8728                     nla_put_u32(msg, NL80211_BSS_STATUS,
8729                                 NL80211_BSS_STATUS_IBSS_JOINED))
8730                         goto nla_put_failure;
8731                 break;
8732         default:
8733                 break;
8734         }
8735
8736         nla_nest_end(msg, bss);
8737
8738         genlmsg_end(msg, hdr);
8739         return 0;
8740
8741  fail_unlock_rcu:
8742         rcu_read_unlock();
8743  nla_put_failure:
8744         genlmsg_cancel(msg, hdr);
8745         return -EMSGSIZE;
8746 }
8747
8748 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
8749 {
8750         struct cfg80211_registered_device *rdev;
8751         struct cfg80211_internal_bss *scan;
8752         struct wireless_dev *wdev;
8753         int start = cb->args[2], idx = 0;
8754         int err;
8755
8756         rtnl_lock();
8757         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8758         if (err) {
8759                 rtnl_unlock();
8760                 return err;
8761         }
8762
8763         wdev_lock(wdev);
8764         spin_lock_bh(&rdev->bss_lock);
8765
8766         /*
8767          * dump_scan will be called multiple times to break up the scan results
8768          * into multiple messages.  It is unlikely that any more bss-es will be
8769          * expired after the first call, so only call only call this on the
8770          * first dump_scan invocation.
8771          */
8772         if (start == 0)
8773                 cfg80211_bss_expire(rdev);
8774
8775         cb->seq = rdev->bss_generation;
8776
8777         list_for_each_entry(scan, &rdev->bss_list, list) {
8778                 if (++idx <= start)
8779                         continue;
8780                 if (nl80211_send_bss(skb, cb,
8781                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8782                                 rdev, wdev, scan) < 0) {
8783                         idx--;
8784                         break;
8785                 }
8786         }
8787
8788         spin_unlock_bh(&rdev->bss_lock);
8789         wdev_unlock(wdev);
8790
8791         cb->args[2] = idx;
8792         rtnl_unlock();
8793
8794         return skb->len;
8795 }
8796
8797 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
8798                                int flags, struct net_device *dev,
8799                                bool allow_radio_stats,
8800                                struct survey_info *survey)
8801 {
8802         void *hdr;
8803         struct nlattr *infoattr;
8804
8805         /* skip radio stats if userspace didn't request them */
8806         if (!survey->channel && !allow_radio_stats)
8807                 return 0;
8808
8809         hdr = nl80211hdr_put(msg, portid, seq, flags,
8810                              NL80211_CMD_NEW_SURVEY_RESULTS);
8811         if (!hdr)
8812                 return -ENOMEM;
8813
8814         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
8815                 goto nla_put_failure;
8816
8817         infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
8818         if (!infoattr)
8819                 goto nla_put_failure;
8820
8821         if (survey->channel &&
8822             nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
8823                         survey->channel->center_freq))
8824                 goto nla_put_failure;
8825
8826         if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
8827             nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
8828                 goto nla_put_failure;
8829         if ((survey->filled & SURVEY_INFO_IN_USE) &&
8830             nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
8831                 goto nla_put_failure;
8832         if ((survey->filled & SURVEY_INFO_TIME) &&
8833             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
8834                         survey->time, NL80211_SURVEY_INFO_PAD))
8835                 goto nla_put_failure;
8836         if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
8837             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
8838                               survey->time_busy, NL80211_SURVEY_INFO_PAD))
8839                 goto nla_put_failure;
8840         if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
8841             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
8842                               survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
8843                 goto nla_put_failure;
8844         if ((survey->filled & SURVEY_INFO_TIME_RX) &&
8845             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
8846                               survey->time_rx, NL80211_SURVEY_INFO_PAD))
8847                 goto nla_put_failure;
8848         if ((survey->filled & SURVEY_INFO_TIME_TX) &&
8849             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
8850                               survey->time_tx, NL80211_SURVEY_INFO_PAD))
8851                 goto nla_put_failure;
8852         if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
8853             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
8854                               survey->time_scan, NL80211_SURVEY_INFO_PAD))
8855                 goto nla_put_failure;
8856         if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
8857             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
8858                               survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
8859                 goto nla_put_failure;
8860
8861         nla_nest_end(msg, infoattr);
8862
8863         genlmsg_end(msg, hdr);
8864         return 0;
8865
8866  nla_put_failure:
8867         genlmsg_cancel(msg, hdr);
8868         return -EMSGSIZE;
8869 }
8870
8871 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
8872 {
8873         struct nlattr **attrbuf;
8874         struct survey_info survey;
8875         struct cfg80211_registered_device *rdev;
8876         struct wireless_dev *wdev;
8877         int survey_idx = cb->args[2];
8878         int res;
8879         bool radio_stats;
8880
8881         attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
8882         if (!attrbuf)
8883                 return -ENOMEM;
8884
8885         rtnl_lock();
8886         res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev);
8887         if (res)
8888                 goto out_err;
8889
8890         /* prepare_wdev_dump parsed the attributes */
8891         radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
8892
8893         if (!wdev->netdev) {
8894                 res = -EINVAL;
8895                 goto out_err;
8896         }
8897
8898         if (!rdev->ops->dump_survey) {
8899                 res = -EOPNOTSUPP;
8900                 goto out_err;
8901         }
8902
8903         while (1) {
8904                 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
8905                 if (res == -ENOENT)
8906                         break;
8907                 if (res)
8908                         goto out_err;
8909
8910                 /* don't send disabled channels, but do send non-channel data */
8911                 if (survey.channel &&
8912                     survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8913                         survey_idx++;
8914                         continue;
8915                 }
8916
8917                 if (nl80211_send_survey(skb,
8918                                 NETLINK_CB(cb->skb).portid,
8919                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8920                                 wdev->netdev, radio_stats, &survey) < 0)
8921                         goto out;
8922                 survey_idx++;
8923         }
8924
8925  out:
8926         cb->args[2] = survey_idx;
8927         res = skb->len;
8928  out_err:
8929         kfree(attrbuf);
8930         rtnl_unlock();
8931         return res;
8932 }
8933
8934 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8935 {
8936         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8937                                   NL80211_WPA_VERSION_2 |
8938                                   NL80211_WPA_VERSION_3));
8939 }
8940
8941 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8942 {
8943         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8944         struct net_device *dev = info->user_ptr[1];
8945         struct ieee80211_channel *chan;
8946         const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8947         int err, ssid_len, ie_len = 0, auth_data_len = 0;
8948         enum nl80211_auth_type auth_type;
8949         struct key_parse key;
8950         bool local_state_change;
8951
8952         if (!info->attrs[NL80211_ATTR_MAC])
8953                 return -EINVAL;
8954
8955         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8956                 return -EINVAL;
8957
8958         if (!info->attrs[NL80211_ATTR_SSID])
8959                 return -EINVAL;
8960
8961         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8962                 return -EINVAL;
8963
8964         err = nl80211_parse_key(info, &key);
8965         if (err)
8966                 return err;
8967
8968         if (key.idx >= 0) {
8969                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8970                         return -EINVAL;
8971                 if (!key.p.key || !key.p.key_len)
8972                         return -EINVAL;
8973                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8974                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8975                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8976                      key.p.key_len != WLAN_KEY_LEN_WEP104))
8977                         return -EINVAL;
8978                 if (key.idx > 3)
8979                         return -EINVAL;
8980         } else {
8981                 key.p.key_len = 0;
8982                 key.p.key = NULL;
8983         }
8984
8985         if (key.idx >= 0) {
8986                 int i;
8987                 bool ok = false;
8988
8989                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8990                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8991                                 ok = true;
8992                                 break;
8993                         }
8994                 }
8995                 if (!ok)
8996                         return -EINVAL;
8997         }
8998
8999         if (!rdev->ops->auth)
9000                 return -EOPNOTSUPP;
9001
9002         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9003             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9004                 return -EOPNOTSUPP;
9005
9006         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9007         chan = nl80211_get_valid_chan(&rdev->wiphy,
9008                                       info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9009         if (!chan)
9010                 return -EINVAL;
9011
9012         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9013         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9014
9015         if (info->attrs[NL80211_ATTR_IE]) {
9016                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9017                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9018         }
9019
9020         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9021         if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
9022                 return -EINVAL;
9023
9024         if ((auth_type == NL80211_AUTHTYPE_SAE ||
9025              auth_type == NL80211_AUTHTYPE_FILS_SK ||
9026              auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
9027              auth_type == NL80211_AUTHTYPE_FILS_PK) &&
9028             !info->attrs[NL80211_ATTR_AUTH_DATA])
9029                 return -EINVAL;
9030
9031         if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
9032                 if (auth_type != NL80211_AUTHTYPE_SAE &&
9033                     auth_type != NL80211_AUTHTYPE_FILS_SK &&
9034                     auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
9035                     auth_type != NL80211_AUTHTYPE_FILS_PK)
9036                         return -EINVAL;
9037                 auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
9038                 auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
9039                 /* need to include at least Auth Transaction and Status Code */
9040                 if (auth_data_len < 4)
9041                         return -EINVAL;
9042         }
9043
9044         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9045
9046         /*
9047          * Since we no longer track auth state, ignore
9048          * requests to only change local state.
9049          */
9050         if (local_state_change)
9051                 return 0;
9052
9053         wdev_lock(dev->ieee80211_ptr);
9054         err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
9055                                  ssid, ssid_len, ie, ie_len,
9056                                  key.p.key, key.p.key_len, key.idx,
9057                                  auth_data, auth_data_len);
9058         wdev_unlock(dev->ieee80211_ptr);
9059         return err;
9060 }
9061
9062 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
9063                                      struct genl_info *info)
9064 {
9065         if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9066                 GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
9067                 return -EINVAL;
9068         }
9069
9070         if (!rdev->ops->tx_control_port ||
9071             !wiphy_ext_feature_isset(&rdev->wiphy,
9072                                      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
9073                 return -EOPNOTSUPP;
9074
9075         return 0;
9076 }
9077
9078 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
9079                                    struct genl_info *info,
9080                                    struct cfg80211_crypto_settings *settings,
9081                                    int cipher_limit)
9082 {
9083         memset(settings, 0, sizeof(*settings));
9084
9085         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
9086
9087         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
9088                 u16 proto;
9089
9090                 proto = nla_get_u16(
9091                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
9092                 settings->control_port_ethertype = cpu_to_be16(proto);
9093                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
9094                     proto != ETH_P_PAE)
9095                         return -EINVAL;
9096                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
9097                         settings->control_port_no_encrypt = true;
9098         } else
9099                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
9100
9101         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9102                 int r = validate_pae_over_nl80211(rdev, info);
9103
9104                 if (r < 0)
9105                         return r;
9106
9107                 settings->control_port_over_nl80211 = true;
9108         }
9109
9110         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
9111                 void *data;
9112                 int len, i;
9113
9114                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9115                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
9116                 settings->n_ciphers_pairwise = len / sizeof(u32);
9117
9118                 if (len % sizeof(u32))
9119                         return -EINVAL;
9120
9121                 if (settings->n_ciphers_pairwise > cipher_limit)
9122                         return -EINVAL;
9123
9124                 memcpy(settings->ciphers_pairwise, data, len);
9125
9126                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
9127                         if (!cfg80211_supported_cipher_suite(
9128                                         &rdev->wiphy,
9129                                         settings->ciphers_pairwise[i]))
9130                                 return -EINVAL;
9131         }
9132
9133         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
9134                 settings->cipher_group =
9135                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
9136                 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
9137                                                      settings->cipher_group))
9138                         return -EINVAL;
9139         }
9140
9141         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
9142                 settings->wpa_versions =
9143                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
9144                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
9145                         return -EINVAL;
9146         }
9147
9148         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
9149                 void *data;
9150                 int len;
9151
9152                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
9153                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
9154                 settings->n_akm_suites = len / sizeof(u32);
9155
9156                 if (len % sizeof(u32))
9157                         return -EINVAL;
9158
9159                 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
9160                         return -EINVAL;
9161
9162                 memcpy(settings->akm_suites, data, len);
9163         }
9164
9165         if (info->attrs[NL80211_ATTR_PMK]) {
9166                 if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
9167                         return -EINVAL;
9168                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
9169                                              NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
9170                         return -EINVAL;
9171                 settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9172         }
9173
9174         if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
9175                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
9176                                              NL80211_EXT_FEATURE_SAE_OFFLOAD))
9177                         return -EINVAL;
9178                 settings->sae_pwd =
9179                         nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9180                 settings->sae_pwd_len =
9181                         nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
9182         }
9183
9184         return 0;
9185 }
9186
9187 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
9188 {
9189         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9190         struct net_device *dev = info->user_ptr[1];
9191         struct ieee80211_channel *chan;
9192         struct cfg80211_assoc_request req = {};
9193         const u8 *bssid, *ssid;
9194         int err, ssid_len = 0;
9195
9196         if (dev->ieee80211_ptr->conn_owner_nlportid &&
9197             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9198                 return -EPERM;
9199
9200         if (!info->attrs[NL80211_ATTR_MAC] ||
9201             !info->attrs[NL80211_ATTR_SSID] ||
9202             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
9203                 return -EINVAL;
9204
9205         if (!rdev->ops->assoc)
9206                 return -EOPNOTSUPP;
9207
9208         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9209             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9210                 return -EOPNOTSUPP;
9211
9212         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9213
9214         chan = nl80211_get_valid_chan(&rdev->wiphy,
9215                                       info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9216         if (!chan)
9217                 return -EINVAL;
9218
9219         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9220         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9221
9222         if (info->attrs[NL80211_ATTR_IE]) {
9223                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9224                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9225         }
9226
9227         if (info->attrs[NL80211_ATTR_USE_MFP]) {
9228                 enum nl80211_mfp mfp =
9229                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9230                 if (mfp == NL80211_MFP_REQUIRED)
9231                         req.use_mfp = true;
9232                 else if (mfp != NL80211_MFP_NO)
9233                         return -EINVAL;
9234         }
9235
9236         if (info->attrs[NL80211_ATTR_PREV_BSSID])
9237                 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9238
9239         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9240                 req.flags |= ASSOC_REQ_DISABLE_HT;
9241
9242         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9243                 memcpy(&req.ht_capa_mask,
9244                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9245                        sizeof(req.ht_capa_mask));
9246
9247         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9248                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9249                         return -EINVAL;
9250                 memcpy(&req.ht_capa,
9251                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9252                        sizeof(req.ht_capa));
9253         }
9254
9255         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9256                 req.flags |= ASSOC_REQ_DISABLE_VHT;
9257
9258         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9259                 memcpy(&req.vht_capa_mask,
9260                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9261                        sizeof(req.vht_capa_mask));
9262
9263         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9264                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9265                         return -EINVAL;
9266                 memcpy(&req.vht_capa,
9267                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9268                        sizeof(req.vht_capa));
9269         }
9270
9271         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9272                 if (!((rdev->wiphy.features &
9273                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9274                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9275                     !wiphy_ext_feature_isset(&rdev->wiphy,
9276                                              NL80211_EXT_FEATURE_RRM))
9277                         return -EINVAL;
9278                 req.flags |= ASSOC_REQ_USE_RRM;
9279         }
9280
9281         if (info->attrs[NL80211_ATTR_FILS_KEK]) {
9282                 req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
9283                 req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
9284                 if (!info->attrs[NL80211_ATTR_FILS_NONCES])
9285                         return -EINVAL;
9286                 req.fils_nonces =
9287                         nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
9288         }
9289
9290         err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
9291         if (!err) {
9292                 wdev_lock(dev->ieee80211_ptr);
9293
9294                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
9295                                           ssid, ssid_len, &req);
9296
9297                 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9298                         dev->ieee80211_ptr->conn_owner_nlportid =
9299                                 info->snd_portid;
9300                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
9301                                bssid, ETH_ALEN);
9302                 }
9303
9304                 wdev_unlock(dev->ieee80211_ptr);
9305         }
9306
9307         return err;
9308 }
9309
9310 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
9311 {
9312         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9313         struct net_device *dev = info->user_ptr[1];
9314         const u8 *ie = NULL, *bssid;
9315         int ie_len = 0, err;
9316         u16 reason_code;
9317         bool local_state_change;
9318
9319         if (dev->ieee80211_ptr->conn_owner_nlportid &&
9320             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9321                 return -EPERM;
9322
9323         if (!info->attrs[NL80211_ATTR_MAC])
9324                 return -EINVAL;
9325
9326         if (!info->attrs[NL80211_ATTR_REASON_CODE])
9327                 return -EINVAL;
9328
9329         if (!rdev->ops->deauth)
9330                 return -EOPNOTSUPP;
9331
9332         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9333             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9334                 return -EOPNOTSUPP;
9335
9336         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9337
9338         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9339         if (reason_code == 0) {
9340                 /* Reason Code 0 is reserved */
9341                 return -EINVAL;
9342         }
9343
9344         if (info->attrs[NL80211_ATTR_IE]) {
9345                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9346                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9347         }
9348
9349         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9350
9351         wdev_lock(dev->ieee80211_ptr);
9352         err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
9353                                    local_state_change);
9354         wdev_unlock(dev->ieee80211_ptr);
9355         return err;
9356 }
9357
9358 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
9359 {
9360         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9361         struct net_device *dev = info->user_ptr[1];
9362         const u8 *ie = NULL, *bssid;
9363         int ie_len = 0, err;
9364         u16 reason_code;
9365         bool local_state_change;
9366
9367         if (dev->ieee80211_ptr->conn_owner_nlportid &&
9368             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
9369                 return -EPERM;
9370
9371         if (!info->attrs[NL80211_ATTR_MAC])
9372                 return -EINVAL;
9373
9374         if (!info->attrs[NL80211_ATTR_REASON_CODE])
9375                 return -EINVAL;
9376
9377         if (!rdev->ops->disassoc)
9378                 return -EOPNOTSUPP;
9379
9380         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9381             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9382                 return -EOPNOTSUPP;
9383
9384         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9385
9386         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9387         if (reason_code == 0) {
9388                 /* Reason Code 0 is reserved */
9389                 return -EINVAL;
9390         }
9391
9392         if (info->attrs[NL80211_ATTR_IE]) {
9393                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9394                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9395         }
9396
9397         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
9398
9399         wdev_lock(dev->ieee80211_ptr);
9400         err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
9401                                      local_state_change);
9402         wdev_unlock(dev->ieee80211_ptr);
9403         return err;
9404 }
9405
9406 static bool
9407 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
9408                          int mcast_rate[NUM_NL80211_BANDS],
9409                          int rateval)
9410 {
9411         struct wiphy *wiphy = &rdev->wiphy;
9412         bool found = false;
9413         int band, i;
9414
9415         for (band = 0; band < NUM_NL80211_BANDS; band++) {
9416                 struct ieee80211_supported_band *sband;
9417
9418                 sband = wiphy->bands[band];
9419                 if (!sband)
9420                         continue;
9421
9422                 for (i = 0; i < sband->n_bitrates; i++) {
9423                         if (sband->bitrates[i].bitrate == rateval) {
9424                                 mcast_rate[band] = i + 1;
9425                                 found = true;
9426                                 break;
9427                         }
9428                 }
9429         }
9430
9431         return found;
9432 }
9433
9434 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
9435 {
9436         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9437         struct net_device *dev = info->user_ptr[1];
9438         struct cfg80211_ibss_params ibss;
9439         struct wiphy *wiphy;
9440         struct cfg80211_cached_keys *connkeys = NULL;
9441         int err;
9442
9443         memset(&ibss, 0, sizeof(ibss));
9444
9445         if (!info->attrs[NL80211_ATTR_SSID] ||
9446             !nla_len(info->attrs[NL80211_ATTR_SSID]))
9447                 return -EINVAL;
9448
9449         ibss.beacon_interval = 100;
9450
9451         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
9452                 ibss.beacon_interval =
9453                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
9454
9455         err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
9456                                            ibss.beacon_interval);
9457         if (err)
9458                 return err;
9459
9460         if (!rdev->ops->join_ibss)
9461                 return -EOPNOTSUPP;
9462
9463         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9464                 return -EOPNOTSUPP;
9465
9466         wiphy = &rdev->wiphy;
9467
9468         if (info->attrs[NL80211_ATTR_MAC]) {
9469                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9470
9471                 if (!is_valid_ether_addr(ibss.bssid))
9472                         return -EINVAL;
9473         }
9474         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9475         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9476
9477         if (info->attrs[NL80211_ATTR_IE]) {
9478                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9479                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9480         }
9481
9482         err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
9483         if (err)
9484                 return err;
9485
9486         if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
9487                                      NL80211_IFTYPE_ADHOC))
9488                 return -EINVAL;
9489
9490         switch (ibss.chandef.width) {
9491         case NL80211_CHAN_WIDTH_5:
9492         case NL80211_CHAN_WIDTH_10:
9493         case NL80211_CHAN_WIDTH_20_NOHT:
9494                 break;
9495         case NL80211_CHAN_WIDTH_20:
9496         case NL80211_CHAN_WIDTH_40:
9497                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9498                         return -EINVAL;
9499                 break;
9500         case NL80211_CHAN_WIDTH_80:
9501         case NL80211_CHAN_WIDTH_80P80:
9502         case NL80211_CHAN_WIDTH_160:
9503                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
9504                         return -EINVAL;
9505                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
9506                                              NL80211_EXT_FEATURE_VHT_IBSS))
9507                         return -EINVAL;
9508                 break;
9509         default:
9510                 return -EINVAL;
9511         }
9512
9513         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
9514         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
9515
9516         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
9517                 u8 *rates =
9518                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9519                 int n_rates =
9520                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
9521                 struct ieee80211_supported_band *sband =
9522                         wiphy->bands[ibss.chandef.chan->band];
9523
9524                 err = ieee80211_get_ratemask(sband, rates, n_rates,
9525                                              &ibss.basic_rates);
9526                 if (err)
9527                         return err;
9528         }
9529
9530         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9531                 memcpy(&ibss.ht_capa_mask,
9532                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9533                        sizeof(ibss.ht_capa_mask));
9534
9535         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9536                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9537                         return -EINVAL;
9538                 memcpy(&ibss.ht_capa,
9539                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9540                        sizeof(ibss.ht_capa));
9541         }
9542
9543         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9544             !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
9545                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9546                 return -EINVAL;
9547
9548         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9549                 bool no_ht = false;
9550
9551                 connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
9552                 if (IS_ERR(connkeys))
9553                         return PTR_ERR(connkeys);
9554
9555                 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
9556                     no_ht) {
9557                         kzfree(connkeys);
9558                         return -EINVAL;
9559                 }
9560         }
9561
9562         ibss.control_port =
9563                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
9564
9565         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
9566                 int r = validate_pae_over_nl80211(rdev, info);
9567
9568                 if (r < 0) {
9569                         kzfree(connkeys);
9570                         return r;
9571                 }
9572
9573                 ibss.control_port_over_nl80211 = true;
9574         }
9575
9576         ibss.userspace_handles_dfs =
9577                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
9578
9579         wdev_lock(dev->ieee80211_ptr);
9580         err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
9581         if (err)
9582                 kzfree(connkeys);
9583         else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9584                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9585         wdev_unlock(dev->ieee80211_ptr);
9586
9587         return err;
9588 }
9589
9590 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
9591 {
9592         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9593         struct net_device *dev = info->user_ptr[1];
9594
9595         if (!rdev->ops->leave_ibss)
9596                 return -EOPNOTSUPP;
9597
9598         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
9599                 return -EOPNOTSUPP;
9600
9601         return cfg80211_leave_ibss(rdev, dev, false);
9602 }
9603
9604 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
9605 {
9606         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9607         struct net_device *dev = info->user_ptr[1];
9608         int mcast_rate[NUM_NL80211_BANDS];
9609         u32 nla_rate;
9610         int err;
9611
9612         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
9613             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
9614             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
9615                 return -EOPNOTSUPP;
9616
9617         if (!rdev->ops->set_mcast_rate)
9618                 return -EOPNOTSUPP;
9619
9620         memset(mcast_rate, 0, sizeof(mcast_rate));
9621
9622         if (!info->attrs[NL80211_ATTR_MCAST_RATE])
9623                 return -EINVAL;
9624
9625         nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
9626         if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
9627                 return -EINVAL;
9628
9629         err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
9630
9631         return err;
9632 }
9633
9634 static struct sk_buff *
9635 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
9636                             struct wireless_dev *wdev, int approxlen,
9637                             u32 portid, u32 seq, enum nl80211_commands cmd,
9638                             enum nl80211_attrs attr,
9639                             const struct nl80211_vendor_cmd_info *info,
9640                             gfp_t gfp)
9641 {
9642         struct sk_buff *skb;
9643         void *hdr;
9644         struct nlattr *data;
9645
9646         skb = nlmsg_new(approxlen + 100, gfp);
9647         if (!skb)
9648                 return NULL;
9649
9650         hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
9651         if (!hdr) {
9652                 kfree_skb(skb);
9653                 return NULL;
9654         }
9655
9656         if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
9657                 goto nla_put_failure;
9658
9659         if (info) {
9660                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
9661                                 info->vendor_id))
9662                         goto nla_put_failure;
9663                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
9664                                 info->subcmd))
9665                         goto nla_put_failure;
9666         }
9667
9668         if (wdev) {
9669                 if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
9670                                       wdev_id(wdev), NL80211_ATTR_PAD))
9671                         goto nla_put_failure;
9672                 if (wdev->netdev &&
9673                     nla_put_u32(skb, NL80211_ATTR_IFINDEX,
9674                                 wdev->netdev->ifindex))
9675                         goto nla_put_failure;
9676         }
9677
9678         data = nla_nest_start_noflag(skb, attr);
9679         if (!data)
9680                 goto nla_put_failure;
9681
9682         ((void **)skb->cb)[0] = rdev;
9683         ((void **)skb->cb)[1] = hdr;
9684         ((void **)skb->cb)[2] = data;
9685
9686         return skb;
9687
9688  nla_put_failure:
9689         kfree_skb(skb);
9690         return NULL;
9691 }
9692
9693 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
9694                                            struct wireless_dev *wdev,
9695                                            enum nl80211_commands cmd,
9696                                            enum nl80211_attrs attr,
9697                                            unsigned int portid,
9698                                            int vendor_event_idx,
9699                                            int approxlen, gfp_t gfp)
9700 {
9701         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
9702         const struct nl80211_vendor_cmd_info *info;
9703
9704         switch (cmd) {
9705         case NL80211_CMD_TESTMODE:
9706                 if (WARN_ON(vendor_event_idx != -1))
9707                         return NULL;
9708                 info = NULL;
9709                 break;
9710         case NL80211_CMD_VENDOR:
9711                 if (WARN_ON(vendor_event_idx < 0 ||
9712                             vendor_event_idx >= wiphy->n_vendor_events))
9713                         return NULL;
9714                 info = &wiphy->vendor_events[vendor_event_idx];
9715                 break;
9716         default:
9717                 WARN_ON(1);
9718                 return NULL;
9719         }
9720
9721         return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
9722                                            cmd, attr, info, gfp);
9723 }
9724 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
9725
9726 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
9727 {
9728         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
9729         void *hdr = ((void **)skb->cb)[1];
9730         struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
9731         struct nlattr *data = ((void **)skb->cb)[2];
9732         enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
9733
9734         /* clear CB data for netlink core to own from now on */
9735         memset(skb->cb, 0, sizeof(skb->cb));
9736
9737         nla_nest_end(skb, data);
9738         genlmsg_end(skb, hdr);
9739
9740         if (nlhdr->nlmsg_pid) {
9741                 genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
9742                                 nlhdr->nlmsg_pid);
9743         } else {
9744                 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
9745                         mcgrp = NL80211_MCGRP_VENDOR;
9746
9747                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
9748                                         skb, 0, mcgrp, gfp);
9749         }
9750 }
9751 EXPORT_SYMBOL(__cfg80211_send_event_skb);
9752
9753 #ifdef CONFIG_NL80211_TESTMODE
9754 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
9755 {
9756         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9757         struct wireless_dev *wdev =
9758                 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
9759         int err;
9760
9761         if (!rdev->ops->testmode_cmd)
9762                 return -EOPNOTSUPP;
9763
9764         if (IS_ERR(wdev)) {
9765                 err = PTR_ERR(wdev);
9766                 if (err != -EINVAL)
9767                         return err;
9768                 wdev = NULL;
9769         } else if (wdev->wiphy != &rdev->wiphy) {
9770                 return -EINVAL;
9771         }
9772
9773         if (!info->attrs[NL80211_ATTR_TESTDATA])
9774                 return -EINVAL;
9775
9776         rdev->cur_cmd_info = info;
9777         err = rdev_testmode_cmd(rdev, wdev,
9778                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
9779                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
9780         rdev->cur_cmd_info = NULL;
9781
9782         return err;
9783 }
9784
9785 static int nl80211_testmode_dump(struct sk_buff *skb,
9786                                  struct netlink_callback *cb)
9787 {
9788         struct cfg80211_registered_device *rdev;
9789         struct nlattr **attrbuf = NULL;
9790         int err;
9791         long phy_idx;
9792         void *data = NULL;
9793         int data_len = 0;
9794
9795         rtnl_lock();
9796
9797         if (cb->args[0]) {
9798                 /*
9799                  * 0 is a valid index, but not valid for args[0],
9800                  * so we need to offset by 1.
9801                  */
9802                 phy_idx = cb->args[0] - 1;
9803
9804                 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
9805                 if (!rdev) {
9806                         err = -ENOENT;
9807                         goto out_err;
9808                 }
9809         } else {
9810                 attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
9811                                   GFP_KERNEL);
9812                 if (!attrbuf) {
9813                         err = -ENOMEM;
9814                         goto out_err;
9815                 }
9816
9817                 err = nlmsg_parse_deprecated(cb->nlh,
9818                                              GENL_HDRLEN + nl80211_fam.hdrsize,
9819                                              attrbuf, nl80211_fam.maxattr,
9820                                              nl80211_policy, NULL);
9821                 if (err)
9822                         goto out_err;
9823
9824                 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
9825                 if (IS_ERR(rdev)) {
9826                         err = PTR_ERR(rdev);
9827                         goto out_err;
9828                 }
9829                 phy_idx = rdev->wiphy_idx;
9830
9831                 if (attrbuf[NL80211_ATTR_TESTDATA])
9832                         cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
9833         }
9834
9835         if (cb->args[1]) {
9836                 data = nla_data((void *)cb->args[1]);
9837                 data_len = nla_len((void *)cb->args[1]);
9838         }
9839
9840         if (!rdev->ops->testmode_dump) {
9841                 err = -EOPNOTSUPP;
9842                 goto out_err;
9843         }
9844
9845         while (1) {
9846                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
9847                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
9848                                            NL80211_CMD_TESTMODE);
9849                 struct nlattr *tmdata;
9850
9851                 if (!hdr)
9852                         break;
9853
9854                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
9855                         genlmsg_cancel(skb, hdr);
9856                         break;
9857                 }
9858
9859                 tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
9860                 if (!tmdata) {
9861                         genlmsg_cancel(skb, hdr);
9862                         break;
9863                 }
9864                 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
9865                 nla_nest_end(skb, tmdata);
9866
9867                 if (err == -ENOBUFS || err == -ENOENT) {
9868                         genlmsg_cancel(skb, hdr);
9869                         break;
9870                 } else if (err) {
9871                         genlmsg_cancel(skb, hdr);
9872                         goto out_err;
9873                 }
9874
9875                 genlmsg_end(skb, hdr);
9876         }
9877
9878         err = skb->len;
9879         /* see above */
9880         cb->args[0] = phy_idx + 1;
9881  out_err:
9882         kfree(attrbuf);
9883         rtnl_unlock();
9884         return err;
9885 }
9886 #endif
9887
9888 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
9889 {
9890         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9891         struct net_device *dev = info->user_ptr[1];
9892         struct cfg80211_connect_params connect;
9893         struct wiphy *wiphy;
9894         struct cfg80211_cached_keys *connkeys = NULL;
9895         int err;
9896
9897         memset(&connect, 0, sizeof(connect));
9898
9899         if (!info->attrs[NL80211_ATTR_SSID] ||
9900             !nla_len(info->attrs[NL80211_ATTR_SSID]))
9901                 return -EINVAL;
9902
9903         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
9904                 connect.auth_type =
9905                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
9906                 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
9907                                              NL80211_CMD_CONNECT))
9908                         return -EINVAL;
9909         } else
9910                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
9911
9912         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
9913
9914         if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
9915             !wiphy_ext_feature_isset(&rdev->wiphy,
9916                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
9917                 return -EINVAL;
9918         connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
9919
9920         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
9921                                       NL80211_MAX_NR_CIPHER_SUITES);
9922         if (err)
9923                 return err;
9924
9925         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9926             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9927                 return -EOPNOTSUPP;
9928
9929         wiphy = &rdev->wiphy;
9930
9931         connect.bg_scan_period = -1;
9932         if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
9933                 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
9934                 connect.bg_scan_period =
9935                         nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
9936         }
9937
9938         if (info->attrs[NL80211_ATTR_MAC])
9939                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9940         else if (info->attrs[NL80211_ATTR_MAC_HINT])
9941                 connect.bssid_hint =
9942                         nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
9943         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9944         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9945
9946         if (info->attrs[NL80211_ATTR_IE]) {
9947                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9948                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9949         }
9950
9951         if (info->attrs[NL80211_ATTR_USE_MFP]) {
9952                 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
9953                 if (connect.mfp == NL80211_MFP_OPTIONAL &&
9954                     !wiphy_ext_feature_isset(&rdev->wiphy,
9955                                              NL80211_EXT_FEATURE_MFP_OPTIONAL))
9956                         return -EOPNOTSUPP;
9957         } else {
9958                 connect.mfp = NL80211_MFP_NO;
9959         }
9960
9961         if (info->attrs[NL80211_ATTR_PREV_BSSID])
9962                 connect.prev_bssid =
9963                         nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
9964
9965         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9966                 connect.channel = nl80211_get_valid_chan(
9967                         wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
9968                 if (!connect.channel)
9969                         return -EINVAL;
9970         } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9971                 connect.channel_hint = nl80211_get_valid_chan(
9972                         wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9973                 if (!connect.channel_hint)
9974                         return -EINVAL;
9975         }
9976
9977         if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
9978                 connect.edmg.channels =
9979                       nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
9980
9981                 if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
9982                         connect.edmg.bw_config =
9983                                 nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
9984         }
9985
9986         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9987                 connkeys = nl80211_parse_connkeys(rdev, info, NULL);
9988                 if (IS_ERR(connkeys))
9989                         return PTR_ERR(connkeys);
9990         }
9991
9992         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9993                 connect.flags |= ASSOC_REQ_DISABLE_HT;
9994
9995         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9996                 memcpy(&connect.ht_capa_mask,
9997                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9998                        sizeof(connect.ht_capa_mask));
9999
10000         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10001                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
10002                         kzfree(connkeys);
10003                         return -EINVAL;
10004                 }
10005                 memcpy(&connect.ht_capa,
10006                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10007                        sizeof(connect.ht_capa));
10008         }
10009
10010         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10011                 connect.flags |= ASSOC_REQ_DISABLE_VHT;
10012
10013         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10014                 memcpy(&connect.vht_capa_mask,
10015                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10016                        sizeof(connect.vht_capa_mask));
10017
10018         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10019                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
10020                         kzfree(connkeys);
10021                         return -EINVAL;
10022                 }
10023                 memcpy(&connect.vht_capa,
10024                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10025                        sizeof(connect.vht_capa));
10026         }
10027
10028         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10029                 if (!((rdev->wiphy.features &
10030                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10031                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10032                     !wiphy_ext_feature_isset(&rdev->wiphy,
10033                                              NL80211_EXT_FEATURE_RRM)) {
10034                         kzfree(connkeys);
10035                         return -EINVAL;
10036                 }
10037                 connect.flags |= ASSOC_REQ_USE_RRM;
10038         }
10039
10040         connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
10041         if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
10042                 kzfree(connkeys);
10043                 return -EOPNOTSUPP;
10044         }
10045
10046         if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
10047                 /* bss selection makes no sense if bssid is set */
10048                 if (connect.bssid) {
10049                         kzfree(connkeys);
10050                         return -EINVAL;
10051                 }
10052
10053                 err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
10054                                        wiphy, &connect.bss_select);
10055                 if (err) {
10056                         kzfree(connkeys);
10057                         return err;
10058                 }
10059         }
10060
10061         if (wiphy_ext_feature_isset(&rdev->wiphy,
10062                                     NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
10063             info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10064             info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10065             info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10066             info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10067                 connect.fils_erp_username =
10068                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10069                 connect.fils_erp_username_len =
10070                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10071                 connect.fils_erp_realm =
10072                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10073                 connect.fils_erp_realm_len =
10074                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10075                 connect.fils_erp_next_seq_num =
10076                         nla_get_u16(
10077                            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10078                 connect.fils_erp_rrk =
10079                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10080                 connect.fils_erp_rrk_len =
10081                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10082         } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10083                    info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10084                    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10085                    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10086                 kzfree(connkeys);
10087                 return -EINVAL;
10088         }
10089
10090         if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
10091                 if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10092                         kzfree(connkeys);
10093                         GENL_SET_ERR_MSG(info,
10094                                          "external auth requires connection ownership");
10095                         return -EINVAL;
10096                 }
10097                 connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
10098         }
10099
10100         wdev_lock(dev->ieee80211_ptr);
10101
10102         err = cfg80211_connect(rdev, dev, &connect, connkeys,
10103                                connect.prev_bssid);
10104         if (err)
10105                 kzfree(connkeys);
10106
10107         if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10108                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
10109                 if (connect.bssid)
10110                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
10111                                connect.bssid, ETH_ALEN);
10112                 else
10113                         memset(dev->ieee80211_ptr->disconnect_bssid,
10114                                0, ETH_ALEN);
10115         }
10116
10117         wdev_unlock(dev->ieee80211_ptr);
10118
10119         return err;
10120 }
10121
10122 static int nl80211_update_connect_params(struct sk_buff *skb,
10123                                          struct genl_info *info)
10124 {
10125         struct cfg80211_connect_params connect = {};
10126         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10127         struct net_device *dev = info->user_ptr[1];
10128         struct wireless_dev *wdev = dev->ieee80211_ptr;
10129         bool fils_sk_offload;
10130         u32 auth_type;
10131         u32 changed = 0;
10132         int ret;
10133
10134         if (!rdev->ops->update_connect_params)
10135                 return -EOPNOTSUPP;
10136
10137         if (info->attrs[NL80211_ATTR_IE]) {
10138                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10139                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10140                 changed |= UPDATE_ASSOC_IES;
10141         }
10142
10143         fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
10144                                                   NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
10145
10146         /*
10147          * when driver supports fils-sk offload all attributes must be
10148          * provided. So the else covers "fils-sk-not-all" and
10149          * "no-fils-sk-any".
10150          */
10151         if (fils_sk_offload &&
10152             info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
10153             info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
10154             info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
10155             info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10156                 connect.fils_erp_username =
10157                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10158                 connect.fils_erp_username_len =
10159                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
10160                 connect.fils_erp_realm =
10161                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10162                 connect.fils_erp_realm_len =
10163                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
10164                 connect.fils_erp_next_seq_num =
10165                         nla_get_u16(
10166                            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
10167                 connect.fils_erp_rrk =
10168                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10169                 connect.fils_erp_rrk_len =
10170                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
10171                 changed |= UPDATE_FILS_ERP_INFO;
10172         } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
10173                    info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
10174                    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
10175                    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
10176                 return -EINVAL;
10177         }
10178
10179         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
10180                 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10181                 if (!nl80211_valid_auth_type(rdev, auth_type,
10182                                              NL80211_CMD_CONNECT))
10183                         return -EINVAL;
10184
10185                 if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
10186                     fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
10187                         return -EINVAL;
10188
10189                 connect.auth_type = auth_type;
10190                 changed |= UPDATE_AUTH_TYPE;
10191         }
10192
10193         wdev_lock(dev->ieee80211_ptr);
10194         if (!wdev->current_bss)
10195                 ret = -ENOLINK;
10196         else
10197                 ret = rdev_update_connect_params(rdev, dev, &connect, changed);
10198         wdev_unlock(dev->ieee80211_ptr);
10199
10200         return ret;
10201 }
10202
10203 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
10204 {
10205         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10206         struct net_device *dev = info->user_ptr[1];
10207         u16 reason;
10208         int ret;
10209
10210         if (dev->ieee80211_ptr->conn_owner_nlportid &&
10211             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10212                 return -EPERM;
10213
10214         if (!info->attrs[NL80211_ATTR_REASON_CODE])
10215                 reason = WLAN_REASON_DEAUTH_LEAVING;
10216         else
10217                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
10218
10219         if (reason == 0)
10220                 return -EINVAL;
10221
10222         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10223             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10224                 return -EOPNOTSUPP;
10225
10226         wdev_lock(dev->ieee80211_ptr);
10227         ret = cfg80211_disconnect(rdev, dev, reason, true);
10228         wdev_unlock(dev->ieee80211_ptr);
10229         return ret;
10230 }
10231
10232 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
10233 {
10234         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10235         struct net *net;
10236         int err;
10237
10238         if (info->attrs[NL80211_ATTR_PID]) {
10239                 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
10240
10241                 net = get_net_ns_by_pid(pid);
10242         } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
10243                 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
10244
10245                 net = get_net_ns_by_fd(fd);
10246         } else {
10247                 return -EINVAL;
10248         }
10249
10250         if (IS_ERR(net))
10251                 return PTR_ERR(net);
10252
10253         err = 0;
10254
10255         /* check if anything to do */
10256         if (!net_eq(wiphy_net(&rdev->wiphy), net))
10257                 err = cfg80211_switch_netns(rdev, net);
10258
10259         put_net(net);
10260         return err;
10261 }
10262
10263 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
10264 {
10265         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10266         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
10267                         struct cfg80211_pmksa *pmksa) = NULL;
10268         struct net_device *dev = info->user_ptr[1];
10269         struct cfg80211_pmksa pmksa;
10270
10271         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
10272
10273         if (!info->attrs[NL80211_ATTR_PMKID])
10274                 return -EINVAL;
10275
10276         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
10277
10278         if (info->attrs[NL80211_ATTR_MAC]) {
10279                 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10280         } else if (info->attrs[NL80211_ATTR_SSID] &&
10281                    info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
10282                    (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
10283                     info->attrs[NL80211_ATTR_PMK])) {
10284                 pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10285                 pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10286                 pmksa.cache_id =
10287                         nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
10288         } else {
10289                 return -EINVAL;
10290         }
10291         if (info->attrs[NL80211_ATTR_PMK]) {
10292                 pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10293                 pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
10294         }
10295
10296         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10297             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
10298             !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
10299               wiphy_ext_feature_isset(&rdev->wiphy,
10300                                       NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
10301                 return -EOPNOTSUPP;
10302
10303         switch (info->genlhdr->cmd) {
10304         case NL80211_CMD_SET_PMKSA:
10305                 rdev_ops = rdev->ops->set_pmksa;
10306                 break;
10307         case NL80211_CMD_DEL_PMKSA:
10308                 rdev_ops = rdev->ops->del_pmksa;
10309                 break;
10310         default:
10311                 WARN_ON(1);
10312                 break;
10313         }
10314
10315         if (!rdev_ops)
10316                 return -EOPNOTSUPP;
10317
10318         return rdev_ops(&rdev->wiphy, dev, &pmksa);
10319 }
10320
10321 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
10322 {
10323         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10324         struct net_device *dev = info->user_ptr[1];
10325
10326         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10327             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10328                 return -EOPNOTSUPP;
10329
10330         if (!rdev->ops->flush_pmksa)
10331                 return -EOPNOTSUPP;
10332
10333         return rdev_flush_pmksa(rdev, dev);
10334 }
10335
10336 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
10337 {
10338         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10339         struct net_device *dev = info->user_ptr[1];
10340         u8 action_code, dialog_token;
10341         u32 peer_capability = 0;
10342         u16 status_code;
10343         u8 *peer;
10344         bool initiator;
10345
10346         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10347             !rdev->ops->tdls_mgmt)
10348                 return -EOPNOTSUPP;
10349
10350         if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
10351             !info->attrs[NL80211_ATTR_STATUS_CODE] ||
10352             !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
10353             !info->attrs[NL80211_ATTR_IE] ||
10354             !info->attrs[NL80211_ATTR_MAC])
10355                 return -EINVAL;
10356
10357         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10358         action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
10359         status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
10360         dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
10361         initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
10362         if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
10363                 peer_capability =
10364                         nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
10365
10366         return rdev_tdls_mgmt(rdev, dev, peer, action_code,
10367                               dialog_token, status_code, peer_capability,
10368                               initiator,
10369                               nla_data(info->attrs[NL80211_ATTR_IE]),
10370                               nla_len(info->attrs[NL80211_ATTR_IE]));
10371 }
10372
10373 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
10374 {
10375         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10376         struct net_device *dev = info->user_ptr[1];
10377         enum nl80211_tdls_operation operation;
10378         u8 *peer;
10379
10380         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
10381             !rdev->ops->tdls_oper)
10382                 return -EOPNOTSUPP;
10383
10384         if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
10385             !info->attrs[NL80211_ATTR_MAC])
10386                 return -EINVAL;
10387
10388         operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
10389         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
10390
10391         return rdev_tdls_oper(rdev, dev, peer, operation);
10392 }
10393
10394 static int nl80211_remain_on_channel(struct sk_buff *skb,
10395                                      struct genl_info *info)
10396 {
10397         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10398         struct wireless_dev *wdev = info->user_ptr[1];
10399         struct cfg80211_chan_def chandef;
10400         const struct cfg80211_chan_def *compat_chandef;
10401         struct sk_buff *msg;
10402         void *hdr;
10403         u64 cookie;
10404         u32 duration;
10405         int err;
10406
10407         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10408             !info->attrs[NL80211_ATTR_DURATION])
10409                 return -EINVAL;
10410
10411         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10412
10413         if (!rdev->ops->remain_on_channel ||
10414             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
10415                 return -EOPNOTSUPP;
10416
10417         /*
10418          * We should be on that channel for at least a minimum amount of
10419          * time (10ms) but no longer than the driver supports.
10420          */
10421         if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10422             duration > rdev->wiphy.max_remain_on_channel_duration)
10423                 return -EINVAL;
10424
10425         err = nl80211_parse_chandef(rdev, info, &chandef);
10426         if (err)
10427                 return err;
10428
10429         wdev_lock(wdev);
10430         if (!cfg80211_off_channel_oper_allowed(wdev) &&
10431             !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
10432                 compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
10433                                                              &chandef);
10434                 if (compat_chandef != &chandef) {
10435                         wdev_unlock(wdev);
10436                         return -EBUSY;
10437                 }
10438         }
10439         wdev_unlock(wdev);
10440
10441         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10442         if (!msg)
10443                 return -ENOMEM;
10444
10445         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10446                              NL80211_CMD_REMAIN_ON_CHANNEL);
10447         if (!hdr) {
10448                 err = -ENOBUFS;
10449                 goto free_msg;
10450         }
10451
10452         err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
10453                                      duration, &cookie);
10454
10455         if (err)
10456                 goto free_msg;
10457
10458         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10459                               NL80211_ATTR_PAD))
10460                 goto nla_put_failure;
10461
10462         genlmsg_end(msg, hdr);
10463
10464         return genlmsg_reply(msg, info);
10465
10466  nla_put_failure:
10467         err = -ENOBUFS;
10468  free_msg:
10469         nlmsg_free(msg);
10470         return err;
10471 }
10472
10473 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
10474                                             struct genl_info *info)
10475 {
10476         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10477         struct wireless_dev *wdev = info->user_ptr[1];
10478         u64 cookie;
10479
10480         if (!info->attrs[NL80211_ATTR_COOKIE])
10481                 return -EINVAL;
10482
10483         if (!rdev->ops->cancel_remain_on_channel)
10484                 return -EOPNOTSUPP;
10485
10486         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10487
10488         return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
10489 }
10490
10491 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
10492                                        struct genl_info *info)
10493 {
10494         struct cfg80211_bitrate_mask mask;
10495         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10496         struct net_device *dev = info->user_ptr[1];
10497         int err;
10498
10499         if (!rdev->ops->set_bitrate_mask)
10500                 return -EOPNOTSUPP;
10501
10502         err = nl80211_parse_tx_bitrate_mask(info, &mask);
10503         if (err)
10504                 return err;
10505
10506         return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
10507 }
10508
10509 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
10510 {
10511         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10512         struct wireless_dev *wdev = info->user_ptr[1];
10513         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
10514
10515         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
10516                 return -EINVAL;
10517
10518         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
10519                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
10520
10521         switch (wdev->iftype) {
10522         case NL80211_IFTYPE_STATION:
10523         case NL80211_IFTYPE_ADHOC:
10524         case NL80211_IFTYPE_P2P_CLIENT:
10525         case NL80211_IFTYPE_AP:
10526         case NL80211_IFTYPE_AP_VLAN:
10527         case NL80211_IFTYPE_MESH_POINT:
10528         case NL80211_IFTYPE_P2P_GO:
10529         case NL80211_IFTYPE_P2P_DEVICE:
10530                 break;
10531         case NL80211_IFTYPE_NAN:
10532         default:
10533                 return -EOPNOTSUPP;
10534         }
10535
10536         /* not much point in registering if we can't reply */
10537         if (!rdev->ops->mgmt_tx)
10538                 return -EOPNOTSUPP;
10539
10540         return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
10541                         nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
10542                         nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
10543 }
10544
10545 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
10546 {
10547         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10548         struct wireless_dev *wdev = info->user_ptr[1];
10549         struct cfg80211_chan_def chandef;
10550         int err;
10551         void *hdr = NULL;
10552         u64 cookie;
10553         struct sk_buff *msg = NULL;
10554         struct cfg80211_mgmt_tx_params params = {
10555                 .dont_wait_for_ack =
10556                         info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
10557         };
10558
10559         if (!info->attrs[NL80211_ATTR_FRAME])
10560                 return -EINVAL;
10561
10562         if (!rdev->ops->mgmt_tx)
10563                 return -EOPNOTSUPP;
10564
10565         switch (wdev->iftype) {
10566         case NL80211_IFTYPE_P2P_DEVICE:
10567                 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10568                         return -EINVAL;
10569         case NL80211_IFTYPE_STATION:
10570         case NL80211_IFTYPE_ADHOC:
10571         case NL80211_IFTYPE_P2P_CLIENT:
10572         case NL80211_IFTYPE_AP:
10573         case NL80211_IFTYPE_AP_VLAN:
10574         case NL80211_IFTYPE_MESH_POINT:
10575         case NL80211_IFTYPE_P2P_GO:
10576                 break;
10577         case NL80211_IFTYPE_NAN:
10578         default:
10579                 return -EOPNOTSUPP;
10580         }
10581
10582         if (info->attrs[NL80211_ATTR_DURATION]) {
10583                 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10584                         return -EINVAL;
10585                 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
10586
10587                 /*
10588                  * We should wait on the channel for at least a minimum amount
10589                  * of time (10ms) but no longer than the driver supports.
10590                  */
10591                 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
10592                     params.wait > rdev->wiphy.max_remain_on_channel_duration)
10593                         return -EINVAL;
10594         }
10595
10596         params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
10597
10598         if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
10599                 return -EINVAL;
10600
10601         params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
10602
10603         /* get the channel if any has been specified, otherwise pass NULL to
10604          * the driver. The latter will use the current one
10605          */
10606         chandef.chan = NULL;
10607         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10608                 err = nl80211_parse_chandef(rdev, info, &chandef);
10609                 if (err)
10610                         return err;
10611         }
10612
10613         if (!chandef.chan && params.offchan)
10614                 return -EINVAL;
10615
10616         wdev_lock(wdev);
10617         if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
10618                 wdev_unlock(wdev);
10619                 return -EBUSY;
10620         }
10621         wdev_unlock(wdev);
10622
10623         params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
10624         params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
10625
10626         if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
10627                 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10628                 int i;
10629
10630                 if (len % sizeof(u16))
10631                         return -EINVAL;
10632
10633                 params.n_csa_offsets = len / sizeof(u16);
10634                 params.csa_offsets =
10635                         nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
10636
10637                 /* check that all the offsets fit the frame */
10638                 for (i = 0; i < params.n_csa_offsets; i++) {
10639                         if (params.csa_offsets[i] >= params.len)
10640                                 return -EINVAL;
10641                 }
10642         }
10643
10644         if (!params.dont_wait_for_ack) {
10645                 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10646                 if (!msg)
10647                         return -ENOMEM;
10648
10649                 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10650                                      NL80211_CMD_FRAME);
10651                 if (!hdr) {
10652                         err = -ENOBUFS;
10653                         goto free_msg;
10654                 }
10655         }
10656
10657         params.chan = chandef.chan;
10658         err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
10659         if (err)
10660                 goto free_msg;
10661
10662         if (msg) {
10663                 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
10664                                       NL80211_ATTR_PAD))
10665                         goto nla_put_failure;
10666
10667                 genlmsg_end(msg, hdr);
10668                 return genlmsg_reply(msg, info);
10669         }
10670
10671         return 0;
10672
10673  nla_put_failure:
10674         err = -ENOBUFS;
10675  free_msg:
10676         nlmsg_free(msg);
10677         return err;
10678 }
10679
10680 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
10681 {
10682         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10683         struct wireless_dev *wdev = info->user_ptr[1];
10684         u64 cookie;
10685
10686         if (!info->attrs[NL80211_ATTR_COOKIE])
10687                 return -EINVAL;
10688
10689         if (!rdev->ops->mgmt_tx_cancel_wait)
10690                 return -EOPNOTSUPP;
10691
10692         switch (wdev->iftype) {
10693         case NL80211_IFTYPE_STATION:
10694         case NL80211_IFTYPE_ADHOC:
10695         case NL80211_IFTYPE_P2P_CLIENT:
10696         case NL80211_IFTYPE_AP:
10697         case NL80211_IFTYPE_AP_VLAN:
10698         case NL80211_IFTYPE_P2P_GO:
10699         case NL80211_IFTYPE_P2P_DEVICE:
10700                 break;
10701         case NL80211_IFTYPE_NAN:
10702         default:
10703                 return -EOPNOTSUPP;
10704         }
10705
10706         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
10707
10708         return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
10709 }
10710
10711 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
10712 {
10713         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10714         struct wireless_dev *wdev;
10715         struct net_device *dev = info->user_ptr[1];
10716         u8 ps_state;
10717         bool state;
10718         int err;
10719
10720         if (!info->attrs[NL80211_ATTR_PS_STATE])
10721                 return -EINVAL;
10722
10723         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
10724
10725         wdev = dev->ieee80211_ptr;
10726
10727         if (!rdev->ops->set_power_mgmt)
10728                 return -EOPNOTSUPP;
10729
10730         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
10731
10732         if (state == wdev->ps)
10733                 return 0;
10734
10735         err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
10736         if (!err)
10737                 wdev->ps = state;
10738         return err;
10739 }
10740
10741 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
10742 {
10743         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10744         enum nl80211_ps_state ps_state;
10745         struct wireless_dev *wdev;
10746         struct net_device *dev = info->user_ptr[1];
10747         struct sk_buff *msg;
10748         void *hdr;
10749         int err;
10750
10751         wdev = dev->ieee80211_ptr;
10752
10753         if (!rdev->ops->set_power_mgmt)
10754                 return -EOPNOTSUPP;
10755
10756         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10757         if (!msg)
10758                 return -ENOMEM;
10759
10760         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10761                              NL80211_CMD_GET_POWER_SAVE);
10762         if (!hdr) {
10763                 err = -ENOBUFS;
10764                 goto free_msg;
10765         }
10766
10767         if (wdev->ps)
10768                 ps_state = NL80211_PS_ENABLED;
10769         else
10770                 ps_state = NL80211_PS_DISABLED;
10771
10772         if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
10773                 goto nla_put_failure;
10774
10775         genlmsg_end(msg, hdr);
10776         return genlmsg_reply(msg, info);
10777
10778  nla_put_failure:
10779         err = -ENOBUFS;
10780  free_msg:
10781         nlmsg_free(msg);
10782         return err;
10783 }
10784
10785 static const struct nla_policy
10786 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
10787         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
10788         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
10789         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
10790         [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
10791         [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
10792         [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
10793         [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
10794 };
10795
10796 static int nl80211_set_cqm_txe(struct genl_info *info,
10797                                u32 rate, u32 pkts, u32 intvl)
10798 {
10799         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10800         struct net_device *dev = info->user_ptr[1];
10801         struct wireless_dev *wdev = dev->ieee80211_ptr;
10802
10803         if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
10804                 return -EINVAL;
10805
10806         if (!rdev->ops->set_cqm_txe_config)
10807                 return -EOPNOTSUPP;
10808
10809         if (wdev->iftype != NL80211_IFTYPE_STATION &&
10810             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10811                 return -EOPNOTSUPP;
10812
10813         return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
10814 }
10815
10816 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
10817                                     struct net_device *dev)
10818 {
10819         struct wireless_dev *wdev = dev->ieee80211_ptr;
10820         s32 last, low, high;
10821         u32 hyst;
10822         int i, n, low_index;
10823         int err;
10824
10825         /* RSSI reporting disabled? */
10826         if (!wdev->cqm_config)
10827                 return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
10828
10829         /*
10830          * Obtain current RSSI value if possible, if not and no RSSI threshold
10831          * event has been received yet, we should receive an event after a
10832          * connection is established and enough beacons received to calculate
10833          * the average.
10834          */
10835         if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
10836             rdev->ops->get_station) {
10837                 struct station_info sinfo = {};
10838                 u8 *mac_addr;
10839
10840                 mac_addr = wdev->current_bss->pub.bssid;
10841
10842                 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
10843                 if (err)
10844                         return err;
10845
10846                 cfg80211_sinfo_release_content(&sinfo);
10847                 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
10848                         wdev->cqm_config->last_rssi_event_value =
10849                                 (s8) sinfo.rx_beacon_signal_avg;
10850         }
10851
10852         last = wdev->cqm_config->last_rssi_event_value;
10853         hyst = wdev->cqm_config->rssi_hyst;
10854         n = wdev->cqm_config->n_rssi_thresholds;
10855
10856         for (i = 0; i < n; i++) {
10857                 i = array_index_nospec(i, n);
10858                 if (last < wdev->cqm_config->rssi_thresholds[i])
10859                         break;
10860         }
10861
10862         low_index = i - 1;
10863         if (low_index >= 0) {
10864                 low_index = array_index_nospec(low_index, n);
10865                 low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
10866         } else {
10867                 low = S32_MIN;
10868         }
10869         if (i < n) {
10870                 i = array_index_nospec(i, n);
10871                 high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
10872         } else {
10873                 high = S32_MAX;
10874         }
10875
10876         return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
10877 }
10878
10879 static int nl80211_set_cqm_rssi(struct genl_info *info,
10880                                 const s32 *thresholds, int n_thresholds,
10881                                 u32 hysteresis)
10882 {
10883         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10884         struct net_device *dev = info->user_ptr[1];
10885         struct wireless_dev *wdev = dev->ieee80211_ptr;
10886         int i, err;
10887         s32 prev = S32_MIN;
10888
10889         /* Check all values negative and sorted */
10890         for (i = 0; i < n_thresholds; i++) {
10891                 if (thresholds[i] > 0 || thresholds[i] <= prev)
10892                         return -EINVAL;
10893
10894                 prev = thresholds[i];
10895         }
10896
10897         if (wdev->iftype != NL80211_IFTYPE_STATION &&
10898             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
10899                 return -EOPNOTSUPP;
10900
10901         wdev_lock(wdev);
10902         cfg80211_cqm_config_free(wdev);
10903         wdev_unlock(wdev);
10904
10905         if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
10906                 if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
10907                         return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
10908
10909                 return rdev_set_cqm_rssi_config(rdev, dev,
10910                                                 thresholds[0], hysteresis);
10911         }
10912
10913         if (!wiphy_ext_feature_isset(&rdev->wiphy,
10914                                      NL80211_EXT_FEATURE_CQM_RSSI_LIST))
10915                 return -EOPNOTSUPP;
10916
10917         if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
10918                 n_thresholds = 0;
10919
10920         wdev_lock(wdev);
10921         if (n_thresholds) {
10922                 struct cfg80211_cqm_config *cqm_config;
10923
10924                 cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
10925                                      n_thresholds * sizeof(s32), GFP_KERNEL);
10926                 if (!cqm_config) {
10927                         err = -ENOMEM;
10928                         goto unlock;
10929                 }
10930
10931                 cqm_config->rssi_hyst = hysteresis;
10932                 cqm_config->n_rssi_thresholds = n_thresholds;
10933                 memcpy(cqm_config->rssi_thresholds, thresholds,
10934                        n_thresholds * sizeof(s32));
10935
10936                 wdev->cqm_config = cqm_config;
10937         }
10938
10939         err = cfg80211_cqm_rssi_update(rdev, dev);
10940
10941 unlock:
10942         wdev_unlock(wdev);
10943
10944         return err;
10945 }
10946
10947 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
10948 {
10949         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
10950         struct nlattr *cqm;
10951         int err;
10952
10953         cqm = info->attrs[NL80211_ATTR_CQM];
10954         if (!cqm)
10955                 return -EINVAL;
10956
10957         err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
10958                                           nl80211_attr_cqm_policy,
10959                                           info->extack);
10960         if (err)
10961                 return err;
10962
10963         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
10964             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
10965                 const s32 *thresholds =
10966                         nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10967                 int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
10968                 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
10969
10970                 if (len % 4)
10971                         return -EINVAL;
10972
10973                 return nl80211_set_cqm_rssi(info, thresholds, len / 4,
10974                                             hysteresis);
10975         }
10976
10977         if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
10978             attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
10979             attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
10980                 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
10981                 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
10982                 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
10983
10984                 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
10985         }
10986
10987         return -EINVAL;
10988 }
10989
10990 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
10991 {
10992         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10993         struct net_device *dev = info->user_ptr[1];
10994         struct ocb_setup setup = {};
10995         int err;
10996
10997         err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10998         if (err)
10999                 return err;
11000
11001         return cfg80211_join_ocb(rdev, dev, &setup);
11002 }
11003
11004 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
11005 {
11006         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11007         struct net_device *dev = info->user_ptr[1];
11008
11009         return cfg80211_leave_ocb(rdev, dev);
11010 }
11011
11012 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
11013 {
11014         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11015         struct net_device *dev = info->user_ptr[1];
11016         struct mesh_config cfg;
11017         struct mesh_setup setup;
11018         int err;
11019
11020         /* start with default */
11021         memcpy(&cfg, &default_mesh_config, sizeof(cfg));
11022         memcpy(&setup, &default_mesh_setup, sizeof(setup));
11023
11024         if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
11025                 /* and parse parameters if given */
11026                 err = nl80211_parse_mesh_config(info, &cfg, NULL);
11027                 if (err)
11028                         return err;
11029         }
11030
11031         if (!info->attrs[NL80211_ATTR_MESH_ID] ||
11032             !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
11033                 return -EINVAL;
11034
11035         setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
11036         setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
11037
11038         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11039             !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
11040                             nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11041                         return -EINVAL;
11042
11043         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
11044                 setup.beacon_interval =
11045                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11046
11047                 err = cfg80211_validate_beacon_int(rdev,
11048                                                    NL80211_IFTYPE_MESH_POINT,
11049                                                    setup.beacon_interval);
11050                 if (err)
11051                         return err;
11052         }
11053
11054         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
11055                 setup.dtim_period =
11056                         nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
11057                 if (setup.dtim_period < 1 || setup.dtim_period > 100)
11058                         return -EINVAL;
11059         }
11060
11061         if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
11062                 /* parse additional setup parameters if given */
11063                 err = nl80211_parse_mesh_setup(info, &setup);
11064                 if (err)
11065                         return err;
11066         }
11067
11068         if (setup.user_mpm)
11069                 cfg.auto_open_plinks = false;
11070
11071         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
11072                 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
11073                 if (err)
11074                         return err;
11075         } else {
11076                 /* __cfg80211_join_mesh() will sort it out */
11077                 setup.chandef.chan = NULL;
11078         }
11079
11080         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11081                 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11082                 int n_rates =
11083                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11084                 struct ieee80211_supported_band *sband;
11085
11086                 if (!setup.chandef.chan)
11087                         return -EINVAL;
11088
11089                 sband = rdev->wiphy.bands[setup.chandef.chan->band];
11090
11091                 err = ieee80211_get_ratemask(sband, rates, n_rates,
11092                                              &setup.basic_rates);
11093                 if (err)
11094                         return err;
11095         }
11096
11097         if (info->attrs[NL80211_ATTR_TX_RATES]) {
11098                 err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
11099                 if (err)
11100                         return err;
11101
11102                 if (!setup.chandef.chan)
11103                         return -EINVAL;
11104
11105                 err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
11106                                               &setup.beacon_rate);
11107                 if (err)
11108                         return err;
11109         }
11110
11111         setup.userspace_handles_dfs =
11112                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11113
11114         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11115                 int r = validate_pae_over_nl80211(rdev, info);
11116
11117                 if (r < 0)
11118                         return r;
11119
11120                 setup.control_port_over_nl80211 = true;
11121         }
11122
11123         wdev_lock(dev->ieee80211_ptr);
11124         err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
11125         if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
11126                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11127         wdev_unlock(dev->ieee80211_ptr);
11128
11129         return err;
11130 }
11131
11132 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
11133 {
11134         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11135         struct net_device *dev = info->user_ptr[1];
11136
11137         return cfg80211_leave_mesh(rdev, dev);
11138 }
11139
11140 #ifdef CONFIG_PM
11141 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
11142                                         struct cfg80211_registered_device *rdev)
11143 {
11144         struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
11145         struct nlattr *nl_pats, *nl_pat;
11146         int i, pat_len;
11147
11148         if (!wowlan->n_patterns)
11149                 return 0;
11150
11151         nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
11152         if (!nl_pats)
11153                 return -ENOBUFS;
11154
11155         for (i = 0; i < wowlan->n_patterns; i++) {
11156                 nl_pat = nla_nest_start_noflag(msg, i + 1);
11157                 if (!nl_pat)
11158                         return -ENOBUFS;
11159                 pat_len = wowlan->patterns[i].pattern_len;
11160                 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
11161                             wowlan->patterns[i].mask) ||
11162                     nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11163                             wowlan->patterns[i].pattern) ||
11164                     nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11165                                 wowlan->patterns[i].pkt_offset))
11166                         return -ENOBUFS;
11167                 nla_nest_end(msg, nl_pat);
11168         }
11169         nla_nest_end(msg, nl_pats);
11170
11171         return 0;
11172 }
11173
11174 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
11175                                    struct cfg80211_wowlan_tcp *tcp)
11176 {
11177         struct nlattr *nl_tcp;
11178
11179         if (!tcp)
11180                 return 0;
11181
11182         nl_tcp = nla_nest_start_noflag(msg,
11183                                        NL80211_WOWLAN_TRIG_TCP_CONNECTION);
11184         if (!nl_tcp)
11185                 return -ENOBUFS;
11186
11187         if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
11188             nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
11189             nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
11190             nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
11191             nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
11192             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
11193                     tcp->payload_len, tcp->payload) ||
11194             nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
11195                         tcp->data_interval) ||
11196             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
11197                     tcp->wake_len, tcp->wake_data) ||
11198             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
11199                     DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
11200                 return -ENOBUFS;
11201
11202         if (tcp->payload_seq.len &&
11203             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
11204                     sizeof(tcp->payload_seq), &tcp->payload_seq))
11205                 return -ENOBUFS;
11206
11207         if (tcp->payload_tok.len &&
11208             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
11209                     sizeof(tcp->payload_tok) + tcp->tokens_size,
11210                     &tcp->payload_tok))
11211                 return -ENOBUFS;
11212
11213         nla_nest_end(msg, nl_tcp);
11214
11215         return 0;
11216 }
11217
11218 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
11219                                   struct cfg80211_sched_scan_request *req)
11220 {
11221         struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
11222         int i;
11223
11224         if (!req)
11225                 return 0;
11226
11227         nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
11228         if (!nd)
11229                 return -ENOBUFS;
11230
11231         if (req->n_scan_plans == 1 &&
11232             nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
11233                         req->scan_plans[0].interval * 1000))
11234                 return -ENOBUFS;
11235
11236         if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
11237                 return -ENOBUFS;
11238
11239         if (req->relative_rssi_set) {
11240                 struct nl80211_bss_select_rssi_adjust rssi_adjust;
11241
11242                 if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
11243                                req->relative_rssi))
11244                         return -ENOBUFS;
11245
11246                 rssi_adjust.band = req->rssi_adjust.band;
11247                 rssi_adjust.delta = req->rssi_adjust.delta;
11248                 if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
11249                             sizeof(rssi_adjust), &rssi_adjust))
11250                         return -ENOBUFS;
11251         }
11252
11253         freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
11254         if (!freqs)
11255                 return -ENOBUFS;
11256
11257         for (i = 0; i < req->n_channels; i++) {
11258                 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
11259                         return -ENOBUFS;
11260         }
11261
11262         nla_nest_end(msg, freqs);
11263
11264         if (req->n_match_sets) {
11265                 matches = nla_nest_start_noflag(msg,
11266                                                 NL80211_ATTR_SCHED_SCAN_MATCH);
11267                 if (!matches)
11268                         return -ENOBUFS;
11269
11270                 for (i = 0; i < req->n_match_sets; i++) {
11271                         match = nla_nest_start_noflag(msg, i);
11272                         if (!match)
11273                                 return -ENOBUFS;
11274
11275                         if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
11276                                     req->match_sets[i].ssid.ssid_len,
11277                                     req->match_sets[i].ssid.ssid))
11278                                 return -ENOBUFS;
11279                         nla_nest_end(msg, match);
11280                 }
11281                 nla_nest_end(msg, matches);
11282         }
11283
11284         scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
11285         if (!scan_plans)
11286                 return -ENOBUFS;
11287
11288         for (i = 0; i < req->n_scan_plans; i++) {
11289                 scan_plan = nla_nest_start_noflag(msg, i + 1);
11290                 if (!scan_plan)
11291                         return -ENOBUFS;
11292
11293                 if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
11294                                 req->scan_plans[i].interval) ||
11295                     (req->scan_plans[i].iterations &&
11296                      nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
11297                                  req->scan_plans[i].iterations)))
11298                         return -ENOBUFS;
11299                 nla_nest_end(msg, scan_plan);
11300         }
11301         nla_nest_end(msg, scan_plans);
11302
11303         nla_nest_end(msg, nd);
11304
11305         return 0;
11306 }
11307
11308 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
11309 {
11310         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11311         struct sk_buff *msg;
11312         void *hdr;
11313         u32 size = NLMSG_DEFAULT_SIZE;
11314
11315         if (!rdev->wiphy.wowlan)
11316                 return -EOPNOTSUPP;
11317
11318         if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
11319                 /* adjust size to have room for all the data */
11320                 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
11321                         rdev->wiphy.wowlan_config->tcp->payload_len +
11322                         rdev->wiphy.wowlan_config->tcp->wake_len +
11323                         rdev->wiphy.wowlan_config->tcp->wake_len / 8;
11324         }
11325
11326         msg = nlmsg_new(size, GFP_KERNEL);
11327         if (!msg)
11328                 return -ENOMEM;
11329
11330         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11331                              NL80211_CMD_GET_WOWLAN);
11332         if (!hdr)
11333                 goto nla_put_failure;
11334
11335         if (rdev->wiphy.wowlan_config) {
11336                 struct nlattr *nl_wowlan;
11337
11338                 nl_wowlan = nla_nest_start_noflag(msg,
11339                                                   NL80211_ATTR_WOWLAN_TRIGGERS);
11340                 if (!nl_wowlan)
11341                         goto nla_put_failure;
11342
11343                 if ((rdev->wiphy.wowlan_config->any &&
11344                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
11345                     (rdev->wiphy.wowlan_config->disconnect &&
11346                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
11347                     (rdev->wiphy.wowlan_config->magic_pkt &&
11348                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
11349                     (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
11350                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
11351                     (rdev->wiphy.wowlan_config->eap_identity_req &&
11352                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
11353                     (rdev->wiphy.wowlan_config->four_way_handshake &&
11354                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
11355                     (rdev->wiphy.wowlan_config->rfkill_release &&
11356                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
11357                         goto nla_put_failure;
11358
11359                 if (nl80211_send_wowlan_patterns(msg, rdev))
11360                         goto nla_put_failure;
11361
11362                 if (nl80211_send_wowlan_tcp(msg,
11363                                             rdev->wiphy.wowlan_config->tcp))
11364                         goto nla_put_failure;
11365
11366                 if (nl80211_send_wowlan_nd(
11367                             msg,
11368                             rdev->wiphy.wowlan_config->nd_config))
11369                         goto nla_put_failure;
11370
11371                 nla_nest_end(msg, nl_wowlan);
11372         }
11373
11374         genlmsg_end(msg, hdr);
11375         return genlmsg_reply(msg, info);
11376
11377 nla_put_failure:
11378         nlmsg_free(msg);
11379         return -ENOBUFS;
11380 }
11381
11382 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
11383                                     struct nlattr *attr,
11384                                     struct cfg80211_wowlan *trig)
11385 {
11386         struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
11387         struct cfg80211_wowlan_tcp *cfg;
11388         struct nl80211_wowlan_tcp_data_token *tok = NULL;
11389         struct nl80211_wowlan_tcp_data_seq *seq = NULL;
11390         u32 size;
11391         u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
11392         int err, port;
11393
11394         if (!rdev->wiphy.wowlan->tcp)
11395                 return -EINVAL;
11396
11397         err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
11398                                           nl80211_wowlan_tcp_policy, NULL);
11399         if (err)
11400                 return err;
11401
11402         if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
11403             !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
11404             !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
11405             !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
11406             !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
11407             !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
11408             !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
11409             !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
11410                 return -EINVAL;
11411
11412         data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
11413         if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
11414                 return -EINVAL;
11415
11416         if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
11417                         rdev->wiphy.wowlan->tcp->data_interval_max ||
11418             nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
11419                 return -EINVAL;
11420
11421         wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
11422         if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
11423                 return -EINVAL;
11424
11425         wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
11426         if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
11427                 return -EINVAL;
11428
11429         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
11430                 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11431
11432                 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
11433                 tokens_size = tokln - sizeof(*tok);
11434
11435                 if (!tok->len || tokens_size % tok->len)
11436                         return -EINVAL;
11437                 if (!rdev->wiphy.wowlan->tcp->tok)
11438                         return -EINVAL;
11439                 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
11440                         return -EINVAL;
11441                 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
11442                         return -EINVAL;
11443                 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
11444                         return -EINVAL;
11445                 if (tok->offset + tok->len > data_size)
11446                         return -EINVAL;
11447         }
11448
11449         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
11450                 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
11451                 if (!rdev->wiphy.wowlan->tcp->seq)
11452                         return -EINVAL;
11453                 if (seq->len == 0 || seq->len > 4)
11454                         return -EINVAL;
11455                 if (seq->len + seq->offset > data_size)
11456                         return -EINVAL;
11457         }
11458
11459         size = sizeof(*cfg);
11460         size += data_size;
11461         size += wake_size + wake_mask_size;
11462         size += tokens_size;
11463
11464         cfg = kzalloc(size, GFP_KERNEL);
11465         if (!cfg)
11466                 return -ENOMEM;
11467         cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
11468         cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
11469         memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
11470                ETH_ALEN);
11471         if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
11472                 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
11473         else
11474                 port = 0;
11475 #ifdef CONFIG_INET
11476         /* allocate a socket and port for it and use it */
11477         err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
11478                             IPPROTO_TCP, &cfg->sock, 1);
11479         if (err) {
11480                 kfree(cfg);
11481                 return err;
11482         }
11483         if (inet_csk_get_port(cfg->sock->sk, port)) {
11484                 sock_release(cfg->sock);
11485                 kfree(cfg);
11486                 return -EADDRINUSE;
11487         }
11488         cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
11489 #else
11490         if (!port) {
11491                 kfree(cfg);
11492                 return -EINVAL;
11493         }
11494         cfg->src_port = port;
11495 #endif
11496
11497         cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
11498         cfg->payload_len = data_size;
11499         cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
11500         memcpy((void *)cfg->payload,
11501                nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
11502                data_size);
11503         if (seq)
11504                 cfg->payload_seq = *seq;
11505         cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
11506         cfg->wake_len = wake_size;
11507         cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
11508         memcpy((void *)cfg->wake_data,
11509                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
11510                wake_size);
11511         cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
11512                          data_size + wake_size;
11513         memcpy((void *)cfg->wake_mask,
11514                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
11515                wake_mask_size);
11516         if (tok) {
11517                 cfg->tokens_size = tokens_size;
11518                 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
11519         }
11520
11521         trig->tcp = cfg;
11522
11523         return 0;
11524 }
11525
11526 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
11527                                    const struct wiphy_wowlan_support *wowlan,
11528                                    struct nlattr *attr,
11529                                    struct cfg80211_wowlan *trig)
11530 {
11531         struct nlattr **tb;
11532         int err;
11533
11534         tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
11535         if (!tb)
11536                 return -ENOMEM;
11537
11538         if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
11539                 err = -EOPNOTSUPP;
11540                 goto out;
11541         }
11542
11543         err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
11544                                           nl80211_policy, NULL);
11545         if (err)
11546                 goto out;
11547
11548         trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
11549                                                    wowlan->max_nd_match_sets);
11550         err = PTR_ERR_OR_ZERO(trig->nd_config);
11551         if (err)
11552                 trig->nd_config = NULL;
11553
11554 out:
11555         kfree(tb);
11556         return err;
11557 }
11558
11559 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
11560 {
11561         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11562         struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
11563         struct cfg80211_wowlan new_triggers = {};
11564         struct cfg80211_wowlan *ntrig;
11565         const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
11566         int err, i;
11567         bool prev_enabled = rdev->wiphy.wowlan_config;
11568         bool regular = false;
11569
11570         if (!wowlan)
11571                 return -EOPNOTSUPP;
11572
11573         if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
11574                 cfg80211_rdev_free_wowlan(rdev);
11575                 rdev->wiphy.wowlan_config = NULL;
11576                 goto set_wakeup;
11577         }
11578
11579         err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
11580                                           info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
11581                                           nl80211_wowlan_policy, info->extack);
11582         if (err)
11583                 return err;
11584
11585         if (tb[NL80211_WOWLAN_TRIG_ANY]) {
11586                 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
11587                         return -EINVAL;
11588                 new_triggers.any = true;
11589         }
11590
11591         if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
11592                 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
11593                         return -EINVAL;
11594                 new_triggers.disconnect = true;
11595                 regular = true;
11596         }
11597
11598         if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
11599                 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
11600                         return -EINVAL;
11601                 new_triggers.magic_pkt = true;
11602                 regular = true;
11603         }
11604
11605         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
11606                 return -EINVAL;
11607
11608         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
11609                 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
11610                         return -EINVAL;
11611                 new_triggers.gtk_rekey_failure = true;
11612                 regular = true;
11613         }
11614
11615         if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
11616                 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
11617                         return -EINVAL;
11618                 new_triggers.eap_identity_req = true;
11619                 regular = true;
11620         }
11621
11622         if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
11623                 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
11624                         return -EINVAL;
11625                 new_triggers.four_way_handshake = true;
11626                 regular = true;
11627         }
11628
11629         if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
11630                 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
11631                         return -EINVAL;
11632                 new_triggers.rfkill_release = true;
11633                 regular = true;
11634         }
11635
11636         if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
11637                 struct nlattr *pat;
11638                 int n_patterns = 0;
11639                 int rem, pat_len, mask_len, pkt_offset;
11640                 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11641
11642                 regular = true;
11643
11644                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11645                                     rem)
11646                         n_patterns++;
11647                 if (n_patterns > wowlan->n_patterns)
11648                         return -EINVAL;
11649
11650                 new_triggers.patterns = kcalloc(n_patterns,
11651                                                 sizeof(new_triggers.patterns[0]),
11652                                                 GFP_KERNEL);
11653                 if (!new_triggers.patterns)
11654                         return -ENOMEM;
11655
11656                 new_triggers.n_patterns = n_patterns;
11657                 i = 0;
11658
11659                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
11660                                     rem) {
11661                         u8 *mask_pat;
11662
11663                         err = nla_parse_nested_deprecated(pat_tb,
11664                                                           MAX_NL80211_PKTPAT,
11665                                                           pat,
11666                                                           nl80211_packet_pattern_policy,
11667                                                           info->extack);
11668                         if (err)
11669                                 goto error;
11670
11671                         err = -EINVAL;
11672                         if (!pat_tb[NL80211_PKTPAT_MASK] ||
11673                             !pat_tb[NL80211_PKTPAT_PATTERN])
11674                                 goto error;
11675                         pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11676                         mask_len = DIV_ROUND_UP(pat_len, 8);
11677                         if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11678                                 goto error;
11679                         if (pat_len > wowlan->pattern_max_len ||
11680                             pat_len < wowlan->pattern_min_len)
11681                                 goto error;
11682
11683                         if (!pat_tb[NL80211_PKTPAT_OFFSET])
11684                                 pkt_offset = 0;
11685                         else
11686                                 pkt_offset = nla_get_u32(
11687                                         pat_tb[NL80211_PKTPAT_OFFSET]);
11688                         if (pkt_offset > wowlan->max_pkt_offset)
11689                                 goto error;
11690                         new_triggers.patterns[i].pkt_offset = pkt_offset;
11691
11692                         mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11693                         if (!mask_pat) {
11694                                 err = -ENOMEM;
11695                                 goto error;
11696                         }
11697                         new_triggers.patterns[i].mask = mask_pat;
11698                         memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11699                                mask_len);
11700                         mask_pat += mask_len;
11701                         new_triggers.patterns[i].pattern = mask_pat;
11702                         new_triggers.patterns[i].pattern_len = pat_len;
11703                         memcpy(mask_pat,
11704                                nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11705                                pat_len);
11706                         i++;
11707                 }
11708         }
11709
11710         if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
11711                 regular = true;
11712                 err = nl80211_parse_wowlan_tcp(
11713                         rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
11714                         &new_triggers);
11715                 if (err)
11716                         goto error;
11717         }
11718
11719         if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
11720                 regular = true;
11721                 err = nl80211_parse_wowlan_nd(
11722                         rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
11723                         &new_triggers);
11724                 if (err)
11725                         goto error;
11726         }
11727
11728         /* The 'any' trigger means the device continues operating more or less
11729          * as in its normal operation mode and wakes up the host on most of the
11730          * normal interrupts (like packet RX, ...)
11731          * It therefore makes little sense to combine with the more constrained
11732          * wakeup trigger modes.
11733          */
11734         if (new_triggers.any && regular) {
11735                 err = -EINVAL;
11736                 goto error;
11737         }
11738
11739         ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
11740         if (!ntrig) {
11741                 err = -ENOMEM;
11742                 goto error;
11743         }
11744         cfg80211_rdev_free_wowlan(rdev);
11745         rdev->wiphy.wowlan_config = ntrig;
11746
11747  set_wakeup:
11748         if (rdev->ops->set_wakeup &&
11749             prev_enabled != !!rdev->wiphy.wowlan_config)
11750                 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
11751
11752         return 0;
11753  error:
11754         for (i = 0; i < new_triggers.n_patterns; i++)
11755                 kfree(new_triggers.patterns[i].mask);
11756         kfree(new_triggers.patterns);
11757         if (new_triggers.tcp && new_triggers.tcp->sock)
11758                 sock_release(new_triggers.tcp->sock);
11759         kfree(new_triggers.tcp);
11760         kfree(new_triggers.nd_config);
11761         return err;
11762 }
11763 #endif
11764
11765 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
11766                                        struct cfg80211_registered_device *rdev)
11767 {
11768         struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
11769         int i, j, pat_len;
11770         struct cfg80211_coalesce_rules *rule;
11771
11772         if (!rdev->coalesce->n_rules)
11773                 return 0;
11774
11775         nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
11776         if (!nl_rules)
11777                 return -ENOBUFS;
11778
11779         for (i = 0; i < rdev->coalesce->n_rules; i++) {
11780                 nl_rule = nla_nest_start_noflag(msg, i + 1);
11781                 if (!nl_rule)
11782                         return -ENOBUFS;
11783
11784                 rule = &rdev->coalesce->rules[i];
11785                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
11786                                 rule->delay))
11787                         return -ENOBUFS;
11788
11789                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
11790                                 rule->condition))
11791                         return -ENOBUFS;
11792
11793                 nl_pats = nla_nest_start_noflag(msg,
11794                                                 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
11795                 if (!nl_pats)
11796                         return -ENOBUFS;
11797
11798                 for (j = 0; j < rule->n_patterns; j++) {
11799                         nl_pat = nla_nest_start_noflag(msg, j + 1);
11800                         if (!nl_pat)
11801                                 return -ENOBUFS;
11802                         pat_len = rule->patterns[j].pattern_len;
11803                         if (nla_put(msg, NL80211_PKTPAT_MASK,
11804                                     DIV_ROUND_UP(pat_len, 8),
11805                                     rule->patterns[j].mask) ||
11806                             nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
11807                                     rule->patterns[j].pattern) ||
11808                             nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
11809                                         rule->patterns[j].pkt_offset))
11810                                 return -ENOBUFS;
11811                         nla_nest_end(msg, nl_pat);
11812                 }
11813                 nla_nest_end(msg, nl_pats);
11814                 nla_nest_end(msg, nl_rule);
11815         }
11816         nla_nest_end(msg, nl_rules);
11817
11818         return 0;
11819 }
11820
11821 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
11822 {
11823         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11824         struct sk_buff *msg;
11825         void *hdr;
11826
11827         if (!rdev->wiphy.coalesce)
11828                 return -EOPNOTSUPP;
11829
11830         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11831         if (!msg)
11832                 return -ENOMEM;
11833
11834         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11835                              NL80211_CMD_GET_COALESCE);
11836         if (!hdr)
11837                 goto nla_put_failure;
11838
11839         if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
11840                 goto nla_put_failure;
11841
11842         genlmsg_end(msg, hdr);
11843         return genlmsg_reply(msg, info);
11844
11845 nla_put_failure:
11846         nlmsg_free(msg);
11847         return -ENOBUFS;
11848 }
11849
11850 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
11851 {
11852         struct cfg80211_coalesce *coalesce = rdev->coalesce;
11853         int i, j;
11854         struct cfg80211_coalesce_rules *rule;
11855
11856         if (!coalesce)
11857                 return;
11858
11859         for (i = 0; i < coalesce->n_rules; i++) {
11860                 rule = &coalesce->rules[i];
11861                 for (j = 0; j < rule->n_patterns; j++)
11862                         kfree(rule->patterns[j].mask);
11863                 kfree(rule->patterns);
11864         }
11865         kfree(coalesce->rules);
11866         kfree(coalesce);
11867         rdev->coalesce = NULL;
11868 }
11869
11870 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
11871                                        struct nlattr *rule,
11872                                        struct cfg80211_coalesce_rules *new_rule)
11873 {
11874         int err, i;
11875         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11876         struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
11877         int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
11878         struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
11879
11880         err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
11881                                           rule, nl80211_coalesce_policy, NULL);
11882         if (err)
11883                 return err;
11884
11885         if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
11886                 new_rule->delay =
11887                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
11888         if (new_rule->delay > coalesce->max_delay)
11889                 return -EINVAL;
11890
11891         if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
11892                 new_rule->condition =
11893                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
11894
11895         if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
11896                 return -EINVAL;
11897
11898         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11899                             rem)
11900                 n_patterns++;
11901         if (n_patterns > coalesce->n_patterns)
11902                 return -EINVAL;
11903
11904         new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
11905                                      GFP_KERNEL);
11906         if (!new_rule->patterns)
11907                 return -ENOMEM;
11908
11909         new_rule->n_patterns = n_patterns;
11910         i = 0;
11911
11912         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
11913                             rem) {
11914                 u8 *mask_pat;
11915
11916                 err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
11917                                                   pat,
11918                                                   nl80211_packet_pattern_policy,
11919                                                   NULL);
11920                 if (err)
11921                         return err;
11922
11923                 if (!pat_tb[NL80211_PKTPAT_MASK] ||
11924                     !pat_tb[NL80211_PKTPAT_PATTERN])
11925                         return -EINVAL;
11926                 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
11927                 mask_len = DIV_ROUND_UP(pat_len, 8);
11928                 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
11929                         return -EINVAL;
11930                 if (pat_len > coalesce->pattern_max_len ||
11931                     pat_len < coalesce->pattern_min_len)
11932                         return -EINVAL;
11933
11934                 if (!pat_tb[NL80211_PKTPAT_OFFSET])
11935                         pkt_offset = 0;
11936                 else
11937                         pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
11938                 if (pkt_offset > coalesce->max_pkt_offset)
11939                         return -EINVAL;
11940                 new_rule->patterns[i].pkt_offset = pkt_offset;
11941
11942                 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
11943                 if (!mask_pat)
11944                         return -ENOMEM;
11945
11946                 new_rule->patterns[i].mask = mask_pat;
11947                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
11948                        mask_len);
11949
11950                 mask_pat += mask_len;
11951                 new_rule->patterns[i].pattern = mask_pat;
11952                 new_rule->patterns[i].pattern_len = pat_len;
11953                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
11954                        pat_len);
11955                 i++;
11956         }
11957
11958         return 0;
11959 }
11960
11961 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
11962 {
11963         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11964         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
11965         struct cfg80211_coalesce new_coalesce = {};
11966         struct cfg80211_coalesce *n_coalesce;
11967         int err, rem_rule, n_rules = 0, i, j;
11968         struct nlattr *rule;
11969         struct cfg80211_coalesce_rules *tmp_rule;
11970
11971         if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
11972                 return -EOPNOTSUPP;
11973
11974         if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
11975                 cfg80211_rdev_free_coalesce(rdev);
11976                 rdev_set_coalesce(rdev, NULL);
11977                 return 0;
11978         }
11979
11980         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11981                             rem_rule)
11982                 n_rules++;
11983         if (n_rules > coalesce->n_rules)
11984                 return -EINVAL;
11985
11986         new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
11987                                      GFP_KERNEL);
11988         if (!new_coalesce.rules)
11989                 return -ENOMEM;
11990
11991         new_coalesce.n_rules = n_rules;
11992         i = 0;
11993
11994         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
11995                             rem_rule) {
11996                 err = nl80211_parse_coalesce_rule(rdev, rule,
11997                                                   &new_coalesce.rules[i]);
11998                 if (err)
11999                         goto error;
12000
12001                 i++;
12002         }
12003
12004         err = rdev_set_coalesce(rdev, &new_coalesce);
12005         if (err)
12006                 goto error;
12007
12008         n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
12009         if (!n_coalesce) {
12010                 err = -ENOMEM;
12011                 goto error;
12012         }
12013         cfg80211_rdev_free_coalesce(rdev);
12014         rdev->coalesce = n_coalesce;
12015
12016         return 0;
12017 error:
12018         for (i = 0; i < new_coalesce.n_rules; i++) {
12019                 tmp_rule = &new_coalesce.rules[i];
12020                 for (j = 0; j < tmp_rule->n_patterns; j++)
12021                         kfree(tmp_rule->patterns[j].mask);
12022                 kfree(tmp_rule->patterns);
12023         }
12024         kfree(new_coalesce.rules);
12025
12026         return err;
12027 }
12028
12029 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
12030 {
12031         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12032         struct net_device *dev = info->user_ptr[1];
12033         struct wireless_dev *wdev = dev->ieee80211_ptr;
12034         struct nlattr *tb[NUM_NL80211_REKEY_DATA];
12035         struct cfg80211_gtk_rekey_data rekey_data;
12036         int err;
12037
12038         if (!info->attrs[NL80211_ATTR_REKEY_DATA])
12039                 return -EINVAL;
12040
12041         err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
12042                                           info->attrs[NL80211_ATTR_REKEY_DATA],
12043                                           nl80211_rekey_policy, info->extack);
12044         if (err)
12045                 return err;
12046
12047         if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
12048             !tb[NL80211_REKEY_DATA_KCK])
12049                 return -EINVAL;
12050         if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
12051                 return -ERANGE;
12052         if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
12053                 return -ERANGE;
12054         if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
12055                 return -ERANGE;
12056
12057         rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
12058         rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
12059         rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
12060
12061         wdev_lock(wdev);
12062         if (!wdev->current_bss) {
12063                 err = -ENOTCONN;
12064                 goto out;
12065         }
12066
12067         if (!rdev->ops->set_rekey_data) {
12068                 err = -EOPNOTSUPP;
12069                 goto out;
12070         }
12071
12072         err = rdev_set_rekey_data(rdev, dev, &rekey_data);
12073  out:
12074         wdev_unlock(wdev);
12075         return err;
12076 }
12077
12078 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
12079                                              struct genl_info *info)
12080 {
12081         struct net_device *dev = info->user_ptr[1];
12082         struct wireless_dev *wdev = dev->ieee80211_ptr;
12083
12084         if (wdev->iftype != NL80211_IFTYPE_AP &&
12085             wdev->iftype != NL80211_IFTYPE_P2P_GO)
12086                 return -EINVAL;
12087
12088         if (wdev->ap_unexpected_nlportid)
12089                 return -EBUSY;
12090
12091         wdev->ap_unexpected_nlportid = info->snd_portid;
12092         return 0;
12093 }
12094
12095 static int nl80211_probe_client(struct sk_buff *skb,
12096                                 struct genl_info *info)
12097 {
12098         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12099         struct net_device *dev = info->user_ptr[1];
12100         struct wireless_dev *wdev = dev->ieee80211_ptr;
12101         struct sk_buff *msg;
12102         void *hdr;
12103         const u8 *addr;
12104         u64 cookie;
12105         int err;
12106
12107         if (wdev->iftype != NL80211_IFTYPE_AP &&
12108             wdev->iftype != NL80211_IFTYPE_P2P_GO)
12109                 return -EOPNOTSUPP;
12110
12111         if (!info->attrs[NL80211_ATTR_MAC])
12112                 return -EINVAL;
12113
12114         if (!rdev->ops->probe_client)
12115                 return -EOPNOTSUPP;
12116
12117         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12118         if (!msg)
12119                 return -ENOMEM;
12120
12121         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12122                              NL80211_CMD_PROBE_CLIENT);
12123         if (!hdr) {
12124                 err = -ENOBUFS;
12125                 goto free_msg;
12126         }
12127
12128         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12129
12130         err = rdev_probe_client(rdev, dev, addr, &cookie);
12131         if (err)
12132                 goto free_msg;
12133
12134         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12135                               NL80211_ATTR_PAD))
12136                 goto nla_put_failure;
12137
12138         genlmsg_end(msg, hdr);
12139
12140         return genlmsg_reply(msg, info);
12141
12142  nla_put_failure:
12143         err = -ENOBUFS;
12144  free_msg:
12145         nlmsg_free(msg);
12146         return err;
12147 }
12148
12149 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
12150 {
12151         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12152         struct cfg80211_beacon_registration *reg, *nreg;
12153         int rv;
12154
12155         if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
12156                 return -EOPNOTSUPP;
12157
12158         nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
12159         if (!nreg)
12160                 return -ENOMEM;
12161
12162         /* First, check if already registered. */
12163         spin_lock_bh(&rdev->beacon_registrations_lock);
12164         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
12165                 if (reg->nlportid == info->snd_portid) {
12166                         rv = -EALREADY;
12167                         goto out_err;
12168                 }
12169         }
12170         /* Add it to the list */
12171         nreg->nlportid = info->snd_portid;
12172         list_add(&nreg->list, &rdev->beacon_registrations);
12173
12174         spin_unlock_bh(&rdev->beacon_registrations_lock);
12175
12176         return 0;
12177 out_err:
12178         spin_unlock_bh(&rdev->beacon_registrations_lock);
12179         kfree(nreg);
12180         return rv;
12181 }
12182
12183 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
12184 {
12185         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12186         struct wireless_dev *wdev = info->user_ptr[1];
12187         int err;
12188
12189         if (!rdev->ops->start_p2p_device)
12190                 return -EOPNOTSUPP;
12191
12192         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12193                 return -EOPNOTSUPP;
12194
12195         if (wdev_running(wdev))
12196                 return 0;
12197
12198         if (rfkill_blocked(rdev->rfkill))
12199                 return -ERFKILL;
12200
12201         err = rdev_start_p2p_device(rdev, wdev);
12202         if (err)
12203                 return err;
12204
12205         wdev->is_running = true;
12206         rdev->opencount++;
12207
12208         return 0;
12209 }
12210
12211 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
12212 {
12213         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12214         struct wireless_dev *wdev = info->user_ptr[1];
12215
12216         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
12217                 return -EOPNOTSUPP;
12218
12219         if (!rdev->ops->stop_p2p_device)
12220                 return -EOPNOTSUPP;
12221
12222         cfg80211_stop_p2p_device(rdev, wdev);
12223
12224         return 0;
12225 }
12226
12227 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
12228 {
12229         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12230         struct wireless_dev *wdev = info->user_ptr[1];
12231         struct cfg80211_nan_conf conf = {};
12232         int err;
12233
12234         if (wdev->iftype != NL80211_IFTYPE_NAN)
12235                 return -EOPNOTSUPP;
12236
12237         if (wdev_running(wdev))
12238                 return -EEXIST;
12239
12240         if (rfkill_blocked(rdev->rfkill))
12241                 return -ERFKILL;
12242
12243         if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
12244                 return -EINVAL;
12245
12246         conf.master_pref =
12247                 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12248
12249         if (info->attrs[NL80211_ATTR_BANDS]) {
12250                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12251
12252                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12253                         return -EOPNOTSUPP;
12254
12255                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12256                         return -EINVAL;
12257
12258                 conf.bands = bands;
12259         }
12260
12261         err = rdev_start_nan(rdev, wdev, &conf);
12262         if (err)
12263                 return err;
12264
12265         wdev->is_running = true;
12266         rdev->opencount++;
12267
12268         return 0;
12269 }
12270
12271 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
12272 {
12273         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12274         struct wireless_dev *wdev = info->user_ptr[1];
12275
12276         if (wdev->iftype != NL80211_IFTYPE_NAN)
12277                 return -EOPNOTSUPP;
12278
12279         cfg80211_stop_nan(rdev, wdev);
12280
12281         return 0;
12282 }
12283
12284 static int validate_nan_filter(struct nlattr *filter_attr)
12285 {
12286         struct nlattr *attr;
12287         int len = 0, n_entries = 0, rem;
12288
12289         nla_for_each_nested(attr, filter_attr, rem) {
12290                 len += nla_len(attr);
12291                 n_entries++;
12292         }
12293
12294         if (len >= U8_MAX)
12295                 return -EINVAL;
12296
12297         return n_entries;
12298 }
12299
12300 static int handle_nan_filter(struct nlattr *attr_filter,
12301                              struct cfg80211_nan_func *func,
12302                              bool tx)
12303 {
12304         struct nlattr *attr;
12305         int n_entries, rem, i;
12306         struct cfg80211_nan_func_filter *filter;
12307
12308         n_entries = validate_nan_filter(attr_filter);
12309         if (n_entries < 0)
12310                 return n_entries;
12311
12312         BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
12313
12314         filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
12315         if (!filter)
12316                 return -ENOMEM;
12317
12318         i = 0;
12319         nla_for_each_nested(attr, attr_filter, rem) {
12320                 filter[i].filter = nla_memdup(attr, GFP_KERNEL);
12321                 filter[i].len = nla_len(attr);
12322                 i++;
12323         }
12324         if (tx) {
12325                 func->num_tx_filters = n_entries;
12326                 func->tx_filters = filter;
12327         } else {
12328                 func->num_rx_filters = n_entries;
12329                 func->rx_filters = filter;
12330         }
12331
12332         return 0;
12333 }
12334
12335 static int nl80211_nan_add_func(struct sk_buff *skb,
12336                                 struct genl_info *info)
12337 {
12338         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12339         struct wireless_dev *wdev = info->user_ptr[1];
12340         struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
12341         struct cfg80211_nan_func *func;
12342         struct sk_buff *msg = NULL;
12343         void *hdr = NULL;
12344         int err = 0;
12345
12346         if (wdev->iftype != NL80211_IFTYPE_NAN)
12347                 return -EOPNOTSUPP;
12348
12349         if (!wdev_running(wdev))
12350                 return -ENOTCONN;
12351
12352         if (!info->attrs[NL80211_ATTR_NAN_FUNC])
12353                 return -EINVAL;
12354
12355         err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
12356                                           info->attrs[NL80211_ATTR_NAN_FUNC],
12357                                           nl80211_nan_func_policy,
12358                                           info->extack);
12359         if (err)
12360                 return err;
12361
12362         func = kzalloc(sizeof(*func), GFP_KERNEL);
12363         if (!func)
12364                 return -ENOMEM;
12365
12366         func->cookie = cfg80211_assign_cookie(rdev);
12367
12368         if (!tb[NL80211_NAN_FUNC_TYPE] ||
12369             nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
12370                 err = -EINVAL;
12371                 goto out;
12372         }
12373
12374
12375         func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
12376
12377         if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
12378                 err = -EINVAL;
12379                 goto out;
12380         }
12381
12382         memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
12383                sizeof(func->service_id));
12384
12385         func->close_range =
12386                 nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
12387
12388         if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
12389                 func->serv_spec_info_len =
12390                         nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
12391                 func->serv_spec_info =
12392                         kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
12393                                 func->serv_spec_info_len,
12394                                 GFP_KERNEL);
12395                 if (!func->serv_spec_info) {
12396                         err = -ENOMEM;
12397                         goto out;
12398                 }
12399         }
12400
12401         if (tb[NL80211_NAN_FUNC_TTL])
12402                 func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
12403
12404         switch (func->type) {
12405         case NL80211_NAN_FUNC_PUBLISH:
12406                 if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
12407                         err = -EINVAL;
12408                         goto out;
12409                 }
12410
12411                 func->publish_type =
12412                         nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
12413                 func->publish_bcast =
12414                         nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
12415
12416                 if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
12417                         func->publish_bcast) {
12418                         err = -EINVAL;
12419                         goto out;
12420                 }
12421                 break;
12422         case NL80211_NAN_FUNC_SUBSCRIBE:
12423                 func->subscribe_active =
12424                         nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
12425                 break;
12426         case NL80211_NAN_FUNC_FOLLOW_UP:
12427                 if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
12428                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
12429                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
12430                         err = -EINVAL;
12431                         goto out;
12432                 }
12433
12434                 func->followup_id =
12435                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
12436                 func->followup_reqid =
12437                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
12438                 memcpy(func->followup_dest.addr,
12439                        nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
12440                        sizeof(func->followup_dest.addr));
12441                 if (func->ttl) {
12442                         err = -EINVAL;
12443                         goto out;
12444                 }
12445                 break;
12446         default:
12447                 err = -EINVAL;
12448                 goto out;
12449         }
12450
12451         if (tb[NL80211_NAN_FUNC_SRF]) {
12452                 struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
12453
12454                 err = nla_parse_nested_deprecated(srf_tb,
12455                                                   NL80211_NAN_SRF_ATTR_MAX,
12456                                                   tb[NL80211_NAN_FUNC_SRF],
12457                                                   nl80211_nan_srf_policy,
12458                                                   info->extack);
12459                 if (err)
12460                         goto out;
12461
12462                 func->srf_include =
12463                         nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
12464
12465                 if (srf_tb[NL80211_NAN_SRF_BF]) {
12466                         if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
12467                             !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
12468                                 err = -EINVAL;
12469                                 goto out;
12470                         }
12471
12472                         func->srf_bf_len =
12473                                 nla_len(srf_tb[NL80211_NAN_SRF_BF]);
12474                         func->srf_bf =
12475                                 kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
12476                                         func->srf_bf_len, GFP_KERNEL);
12477                         if (!func->srf_bf) {
12478                                 err = -ENOMEM;
12479                                 goto out;
12480                         }
12481
12482                         func->srf_bf_idx =
12483                                 nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
12484                 } else {
12485                         struct nlattr *attr, *mac_attr =
12486                                 srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
12487                         int n_entries, rem, i = 0;
12488
12489                         if (!mac_attr) {
12490                                 err = -EINVAL;
12491                                 goto out;
12492                         }
12493
12494                         n_entries = validate_acl_mac_addrs(mac_attr);
12495                         if (n_entries <= 0) {
12496                                 err = -EINVAL;
12497                                 goto out;
12498                         }
12499
12500                         func->srf_num_macs = n_entries;
12501                         func->srf_macs =
12502                                 kcalloc(n_entries, sizeof(*func->srf_macs),
12503                                         GFP_KERNEL);
12504                         if (!func->srf_macs) {
12505                                 err = -ENOMEM;
12506                                 goto out;
12507                         }
12508
12509                         nla_for_each_nested(attr, mac_attr, rem)
12510                                 memcpy(func->srf_macs[i++].addr, nla_data(attr),
12511                                        sizeof(*func->srf_macs));
12512                 }
12513         }
12514
12515         if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
12516                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
12517                                         func, true);
12518                 if (err)
12519                         goto out;
12520         }
12521
12522         if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
12523                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
12524                                         func, false);
12525                 if (err)
12526                         goto out;
12527         }
12528
12529         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12530         if (!msg) {
12531                 err = -ENOMEM;
12532                 goto out;
12533         }
12534
12535         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12536                              NL80211_CMD_ADD_NAN_FUNCTION);
12537         /* This can't really happen - we just allocated 4KB */
12538         if (WARN_ON(!hdr)) {
12539                 err = -ENOMEM;
12540                 goto out;
12541         }
12542
12543         err = rdev_add_nan_func(rdev, wdev, func);
12544 out:
12545         if (err < 0) {
12546                 cfg80211_free_nan_func(func);
12547                 nlmsg_free(msg);
12548                 return err;
12549         }
12550
12551         /* propagate the instance id and cookie to userspace  */
12552         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
12553                               NL80211_ATTR_PAD))
12554                 goto nla_put_failure;
12555
12556         func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12557         if (!func_attr)
12558                 goto nla_put_failure;
12559
12560         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
12561                        func->instance_id))
12562                 goto nla_put_failure;
12563
12564         nla_nest_end(msg, func_attr);
12565
12566         genlmsg_end(msg, hdr);
12567         return genlmsg_reply(msg, info);
12568
12569 nla_put_failure:
12570         nlmsg_free(msg);
12571         return -ENOBUFS;
12572 }
12573
12574 static int nl80211_nan_del_func(struct sk_buff *skb,
12575                                struct genl_info *info)
12576 {
12577         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12578         struct wireless_dev *wdev = info->user_ptr[1];
12579         u64 cookie;
12580
12581         if (wdev->iftype != NL80211_IFTYPE_NAN)
12582                 return -EOPNOTSUPP;
12583
12584         if (!wdev_running(wdev))
12585                 return -ENOTCONN;
12586
12587         if (!info->attrs[NL80211_ATTR_COOKIE])
12588                 return -EINVAL;
12589
12590         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12591
12592         rdev_del_nan_func(rdev, wdev, cookie);
12593
12594         return 0;
12595 }
12596
12597 static int nl80211_nan_change_config(struct sk_buff *skb,
12598                                      struct genl_info *info)
12599 {
12600         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12601         struct wireless_dev *wdev = info->user_ptr[1];
12602         struct cfg80211_nan_conf conf = {};
12603         u32 changed = 0;
12604
12605         if (wdev->iftype != NL80211_IFTYPE_NAN)
12606                 return -EOPNOTSUPP;
12607
12608         if (!wdev_running(wdev))
12609                 return -ENOTCONN;
12610
12611         if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
12612                 conf.master_pref =
12613                         nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
12614                 if (conf.master_pref <= 1 || conf.master_pref == 255)
12615                         return -EINVAL;
12616
12617                 changed |= CFG80211_NAN_CONF_CHANGED_PREF;
12618         }
12619
12620         if (info->attrs[NL80211_ATTR_BANDS]) {
12621                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
12622
12623                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
12624                         return -EOPNOTSUPP;
12625
12626                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
12627                         return -EINVAL;
12628
12629                 conf.bands = bands;
12630                 changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
12631         }
12632
12633         if (!changed)
12634                 return -EINVAL;
12635
12636         return rdev_nan_change_conf(rdev, wdev, &conf, changed);
12637 }
12638
12639 void cfg80211_nan_match(struct wireless_dev *wdev,
12640                         struct cfg80211_nan_match_params *match, gfp_t gfp)
12641 {
12642         struct wiphy *wiphy = wdev->wiphy;
12643         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12644         struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
12645         struct sk_buff *msg;
12646         void *hdr;
12647
12648         if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
12649                 return;
12650
12651         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12652         if (!msg)
12653                 return;
12654
12655         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
12656         if (!hdr) {
12657                 nlmsg_free(msg);
12658                 return;
12659         }
12660
12661         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12662             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12663                                          wdev->netdev->ifindex)) ||
12664             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12665                               NL80211_ATTR_PAD))
12666                 goto nla_put_failure;
12667
12668         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
12669                               NL80211_ATTR_PAD) ||
12670             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
12671                 goto nla_put_failure;
12672
12673         match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
12674         if (!match_attr)
12675                 goto nla_put_failure;
12676
12677         local_func_attr = nla_nest_start_noflag(msg,
12678                                                 NL80211_NAN_MATCH_FUNC_LOCAL);
12679         if (!local_func_attr)
12680                 goto nla_put_failure;
12681
12682         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
12683                 goto nla_put_failure;
12684
12685         nla_nest_end(msg, local_func_attr);
12686
12687         peer_func_attr = nla_nest_start_noflag(msg,
12688                                                NL80211_NAN_MATCH_FUNC_PEER);
12689         if (!peer_func_attr)
12690                 goto nla_put_failure;
12691
12692         if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
12693             nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
12694                 goto nla_put_failure;
12695
12696         if (match->info && match->info_len &&
12697             nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
12698                     match->info))
12699                 goto nla_put_failure;
12700
12701         nla_nest_end(msg, peer_func_attr);
12702         nla_nest_end(msg, match_attr);
12703         genlmsg_end(msg, hdr);
12704
12705         if (!wdev->owner_nlportid)
12706                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12707                                         msg, 0, NL80211_MCGRP_NAN, gfp);
12708         else
12709                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12710                                 wdev->owner_nlportid);
12711
12712         return;
12713
12714 nla_put_failure:
12715         nlmsg_free(msg);
12716 }
12717 EXPORT_SYMBOL(cfg80211_nan_match);
12718
12719 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
12720                                   u8 inst_id,
12721                                   enum nl80211_nan_func_term_reason reason,
12722                                   u64 cookie, gfp_t gfp)
12723 {
12724         struct wiphy *wiphy = wdev->wiphy;
12725         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12726         struct sk_buff *msg;
12727         struct nlattr *func_attr;
12728         void *hdr;
12729
12730         if (WARN_ON(!inst_id))
12731                 return;
12732
12733         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
12734         if (!msg)
12735                 return;
12736
12737         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
12738         if (!hdr) {
12739                 nlmsg_free(msg);
12740                 return;
12741         }
12742
12743         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12744             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
12745                                          wdev->netdev->ifindex)) ||
12746             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
12747                               NL80211_ATTR_PAD))
12748                 goto nla_put_failure;
12749
12750         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12751                               NL80211_ATTR_PAD))
12752                 goto nla_put_failure;
12753
12754         func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
12755         if (!func_attr)
12756                 goto nla_put_failure;
12757
12758         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
12759             nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
12760                 goto nla_put_failure;
12761
12762         nla_nest_end(msg, func_attr);
12763         genlmsg_end(msg, hdr);
12764
12765         if (!wdev->owner_nlportid)
12766                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
12767                                         msg, 0, NL80211_MCGRP_NAN, gfp);
12768         else
12769                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
12770                                 wdev->owner_nlportid);
12771
12772         return;
12773
12774 nla_put_failure:
12775         nlmsg_free(msg);
12776 }
12777 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
12778
12779 static int nl80211_get_protocol_features(struct sk_buff *skb,
12780                                          struct genl_info *info)
12781 {
12782         void *hdr;
12783         struct sk_buff *msg;
12784
12785         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12786         if (!msg)
12787                 return -ENOMEM;
12788
12789         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12790                              NL80211_CMD_GET_PROTOCOL_FEATURES);
12791         if (!hdr)
12792                 goto nla_put_failure;
12793
12794         if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
12795                         NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
12796                 goto nla_put_failure;
12797
12798         genlmsg_end(msg, hdr);
12799         return genlmsg_reply(msg, info);
12800
12801  nla_put_failure:
12802         kfree_skb(msg);
12803         return -ENOBUFS;
12804 }
12805
12806 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
12807 {
12808         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12809         struct cfg80211_update_ft_ies_params ft_params;
12810         struct net_device *dev = info->user_ptr[1];
12811
12812         if (!rdev->ops->update_ft_ies)
12813                 return -EOPNOTSUPP;
12814
12815         if (!info->attrs[NL80211_ATTR_MDID] ||
12816             !info->attrs[NL80211_ATTR_IE])
12817                 return -EINVAL;
12818
12819         memset(&ft_params, 0, sizeof(ft_params));
12820         ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
12821         ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
12822         ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
12823
12824         return rdev_update_ft_ies(rdev, dev, &ft_params);
12825 }
12826
12827 static int nl80211_crit_protocol_start(struct sk_buff *skb,
12828                                        struct genl_info *info)
12829 {
12830         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12831         struct wireless_dev *wdev = info->user_ptr[1];
12832         enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
12833         u16 duration;
12834         int ret;
12835
12836         if (!rdev->ops->crit_proto_start)
12837                 return -EOPNOTSUPP;
12838
12839         if (WARN_ON(!rdev->ops->crit_proto_stop))
12840                 return -EINVAL;
12841
12842         if (rdev->crit_proto_nlportid)
12843                 return -EBUSY;
12844
12845         /* determine protocol if provided */
12846         if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
12847                 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
12848
12849         if (proto >= NUM_NL80211_CRIT_PROTO)
12850                 return -EINVAL;
12851
12852         /* timeout must be provided */
12853         if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
12854                 return -EINVAL;
12855
12856         duration =
12857                 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
12858
12859         if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
12860                 return -ERANGE;
12861
12862         ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
12863         if (!ret)
12864                 rdev->crit_proto_nlportid = info->snd_portid;
12865
12866         return ret;
12867 }
12868
12869 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
12870                                       struct genl_info *info)
12871 {
12872         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12873         struct wireless_dev *wdev = info->user_ptr[1];
12874
12875         if (!rdev->ops->crit_proto_stop)
12876                 return -EOPNOTSUPP;
12877
12878         if (rdev->crit_proto_nlportid) {
12879                 rdev->crit_proto_nlportid = 0;
12880                 rdev_crit_proto_stop(rdev, wdev);
12881         }
12882         return 0;
12883 }
12884
12885 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
12886                                        struct nlattr *attr,
12887                                        struct netlink_ext_ack *extack)
12888 {
12889         if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
12890                 if (attr->nla_type & NLA_F_NESTED) {
12891                         NL_SET_ERR_MSG_ATTR(extack, attr,
12892                                             "unexpected nested data");
12893                         return -EINVAL;
12894                 }
12895
12896                 return 0;
12897         }
12898
12899         if (!(attr->nla_type & NLA_F_NESTED)) {
12900                 NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
12901                 return -EINVAL;
12902         }
12903
12904         return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
12905 }
12906
12907 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
12908 {
12909         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12910         struct wireless_dev *wdev =
12911                 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
12912         int i, err;
12913         u32 vid, subcmd;
12914
12915         if (!rdev->wiphy.vendor_commands)
12916                 return -EOPNOTSUPP;
12917
12918         if (IS_ERR(wdev)) {
12919                 err = PTR_ERR(wdev);
12920                 if (err != -EINVAL)
12921                         return err;
12922                 wdev = NULL;
12923         } else if (wdev->wiphy != &rdev->wiphy) {
12924                 return -EINVAL;
12925         }
12926
12927         if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
12928             !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
12929                 return -EINVAL;
12930
12931         vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
12932         subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
12933         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
12934                 const struct wiphy_vendor_command *vcmd;
12935                 void *data = NULL;
12936                 int len = 0;
12937
12938                 vcmd = &rdev->wiphy.vendor_commands[i];
12939
12940                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
12941                         continue;
12942
12943                 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12944                                    WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12945                         if (!wdev)
12946                                 return -EINVAL;
12947                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12948                             !wdev->netdev)
12949                                 return -EINVAL;
12950
12951                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12952                                 if (!wdev_running(wdev))
12953                                         return -ENETDOWN;
12954                         }
12955
12956                         if (!vcmd->doit)
12957                                 return -EOPNOTSUPP;
12958                 } else {
12959                         wdev = NULL;
12960                 }
12961
12962                 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
12963                         data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12964                         len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
12965
12966                         err = nl80211_vendor_check_policy(vcmd,
12967                                         info->attrs[NL80211_ATTR_VENDOR_DATA],
12968                                         info->extack);
12969                         if (err)
12970                                 return err;
12971                 }
12972
12973                 rdev->cur_cmd_info = info;
12974                 err = vcmd->doit(&rdev->wiphy, wdev, data, len);
12975                 rdev->cur_cmd_info = NULL;
12976                 return err;
12977         }
12978
12979         return -EOPNOTSUPP;
12980 }
12981
12982 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
12983                                        struct netlink_callback *cb,
12984                                        struct cfg80211_registered_device **rdev,
12985                                        struct wireless_dev **wdev)
12986 {
12987         struct nlattr **attrbuf;
12988         u32 vid, subcmd;
12989         unsigned int i;
12990         int vcmd_idx = -1;
12991         int err;
12992         void *data = NULL;
12993         unsigned int data_len = 0;
12994
12995         if (cb->args[0]) {
12996                 /* subtract the 1 again here */
12997                 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
12998                 struct wireless_dev *tmp;
12999
13000                 if (!wiphy)
13001                         return -ENODEV;
13002                 *rdev = wiphy_to_rdev(wiphy);
13003                 *wdev = NULL;
13004
13005                 if (cb->args[1]) {
13006                         list_for_each_entry(tmp, &wiphy->wdev_list, list) {
13007                                 if (tmp->identifier == cb->args[1] - 1) {
13008                                         *wdev = tmp;
13009                                         break;
13010                                 }
13011                         }
13012                 }
13013
13014                 /* keep rtnl locked in successful case */
13015                 return 0;
13016         }
13017
13018         attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
13019         if (!attrbuf)
13020                 return -ENOMEM;
13021
13022         err = nlmsg_parse_deprecated(cb->nlh,
13023                                      GENL_HDRLEN + nl80211_fam.hdrsize,
13024                                      attrbuf, nl80211_fam.maxattr,
13025                                      nl80211_policy, NULL);
13026         if (err)
13027                 goto out;
13028
13029         if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
13030             !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
13031                 err = -EINVAL;
13032                 goto out;
13033         }
13034
13035         *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
13036         if (IS_ERR(*wdev))
13037                 *wdev = NULL;
13038
13039         *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
13040         if (IS_ERR(*rdev)) {
13041                 err = PTR_ERR(*rdev);
13042                 goto out;
13043         }
13044
13045         vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
13046         subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
13047
13048         for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
13049                 const struct wiphy_vendor_command *vcmd;
13050
13051                 vcmd = &(*rdev)->wiphy.vendor_commands[i];
13052
13053                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
13054                         continue;
13055
13056                 if (!vcmd->dumpit) {
13057                         err = -EOPNOTSUPP;
13058                         goto out;
13059                 }
13060
13061                 vcmd_idx = i;
13062                 break;
13063         }
13064
13065         if (vcmd_idx < 0) {
13066                 err = -EOPNOTSUPP;
13067                 goto out;
13068         }
13069
13070         if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
13071                 data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13072                 data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
13073
13074                 err = nl80211_vendor_check_policy(
13075                                 &(*rdev)->wiphy.vendor_commands[vcmd_idx],
13076                                 attrbuf[NL80211_ATTR_VENDOR_DATA],
13077                                 cb->extack);
13078                 if (err)
13079                         goto out;
13080         }
13081
13082         /* 0 is the first index - add 1 to parse only once */
13083         cb->args[0] = (*rdev)->wiphy_idx + 1;
13084         /* add 1 to know if it was NULL */
13085         cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
13086         cb->args[2] = vcmd_idx;
13087         cb->args[3] = (unsigned long)data;
13088         cb->args[4] = data_len;
13089
13090         /* keep rtnl locked in successful case */
13091         err = 0;
13092 out:
13093         kfree(attrbuf);
13094         return err;
13095 }
13096
13097 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
13098                                    struct netlink_callback *cb)
13099 {
13100         struct cfg80211_registered_device *rdev;
13101         struct wireless_dev *wdev;
13102         unsigned int vcmd_idx;
13103         const struct wiphy_vendor_command *vcmd;
13104         void *data;
13105         int data_len;
13106         int err;
13107         struct nlattr *vendor_data;
13108
13109         rtnl_lock();
13110         err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
13111         if (err)
13112                 goto out;
13113
13114         vcmd_idx = cb->args[2];
13115         data = (void *)cb->args[3];
13116         data_len = cb->args[4];
13117         vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
13118
13119         if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
13120                            WIPHY_VENDOR_CMD_NEED_NETDEV)) {
13121                 if (!wdev) {
13122                         err = -EINVAL;
13123                         goto out;
13124                 }
13125                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
13126                     !wdev->netdev) {
13127                         err = -EINVAL;
13128                         goto out;
13129                 }
13130
13131                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
13132                         if (!wdev_running(wdev)) {
13133                                 err = -ENETDOWN;
13134                                 goto out;
13135                         }
13136                 }
13137         }
13138
13139         while (1) {
13140                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
13141                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
13142                                            NL80211_CMD_VENDOR);
13143                 if (!hdr)
13144                         break;
13145
13146                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13147                     (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
13148                                                wdev_id(wdev),
13149                                                NL80211_ATTR_PAD))) {
13150                         genlmsg_cancel(skb, hdr);
13151                         break;
13152                 }
13153
13154                 vendor_data = nla_nest_start_noflag(skb,
13155                                                     NL80211_ATTR_VENDOR_DATA);
13156                 if (!vendor_data) {
13157                         genlmsg_cancel(skb, hdr);
13158                         break;
13159                 }
13160
13161                 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
13162                                    (unsigned long *)&cb->args[5]);
13163                 nla_nest_end(skb, vendor_data);
13164
13165                 if (err == -ENOBUFS || err == -ENOENT) {
13166                         genlmsg_cancel(skb, hdr);
13167                         break;
13168                 } else if (err) {
13169                         genlmsg_cancel(skb, hdr);
13170                         goto out;
13171                 }
13172
13173                 genlmsg_end(skb, hdr);
13174         }
13175
13176         err = skb->len;
13177  out:
13178         rtnl_unlock();
13179         return err;
13180 }
13181
13182 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
13183                                            enum nl80211_commands cmd,
13184                                            enum nl80211_attrs attr,
13185                                            int approxlen)
13186 {
13187         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13188
13189         if (WARN_ON(!rdev->cur_cmd_info))
13190                 return NULL;
13191
13192         return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
13193                                            rdev->cur_cmd_info->snd_portid,
13194                                            rdev->cur_cmd_info->snd_seq,
13195                                            cmd, attr, NULL, GFP_KERNEL);
13196 }
13197 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
13198
13199 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
13200 {
13201         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
13202         void *hdr = ((void **)skb->cb)[1];
13203         struct nlattr *data = ((void **)skb->cb)[2];
13204
13205         /* clear CB data for netlink core to own from now on */
13206         memset(skb->cb, 0, sizeof(skb->cb));
13207
13208         if (WARN_ON(!rdev->cur_cmd_info)) {
13209                 kfree_skb(skb);
13210                 return -EINVAL;
13211         }
13212
13213         nla_nest_end(skb, data);
13214         genlmsg_end(skb, hdr);
13215         return genlmsg_reply(skb, rdev->cur_cmd_info);
13216 }
13217 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
13218
13219 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
13220 {
13221         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13222
13223         if (WARN_ON(!rdev->cur_cmd_info))
13224                 return 0;
13225
13226         return rdev->cur_cmd_info->snd_portid;
13227 }
13228 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
13229
13230 static int nl80211_set_qos_map(struct sk_buff *skb,
13231                                struct genl_info *info)
13232 {
13233         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13234         struct cfg80211_qos_map *qos_map = NULL;
13235         struct net_device *dev = info->user_ptr[1];
13236         u8 *pos, len, num_des, des_len, des;
13237         int ret;
13238
13239         if (!rdev->ops->set_qos_map)
13240                 return -EOPNOTSUPP;
13241
13242         if (info->attrs[NL80211_ATTR_QOS_MAP]) {
13243                 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
13244                 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
13245
13246                 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
13247                     len > IEEE80211_QOS_MAP_LEN_MAX)
13248                         return -EINVAL;
13249
13250                 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
13251                 if (!qos_map)
13252                         return -ENOMEM;
13253
13254                 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
13255                 if (num_des) {
13256                         des_len = num_des *
13257                                 sizeof(struct cfg80211_dscp_exception);
13258                         memcpy(qos_map->dscp_exception, pos, des_len);
13259                         qos_map->num_des = num_des;
13260                         for (des = 0; des < num_des; des++) {
13261                                 if (qos_map->dscp_exception[des].up > 7) {
13262                                         kfree(qos_map);
13263                                         return -EINVAL;
13264                                 }
13265                         }
13266                         pos += des_len;
13267                 }
13268                 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
13269         }
13270
13271         wdev_lock(dev->ieee80211_ptr);
13272         ret = nl80211_key_allowed(dev->ieee80211_ptr);
13273         if (!ret)
13274                 ret = rdev_set_qos_map(rdev, dev, qos_map);
13275         wdev_unlock(dev->ieee80211_ptr);
13276
13277         kfree(qos_map);
13278         return ret;
13279 }
13280
13281 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
13282 {
13283         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13284         struct net_device *dev = info->user_ptr[1];
13285         struct wireless_dev *wdev = dev->ieee80211_ptr;
13286         const u8 *peer;
13287         u8 tsid, up;
13288         u16 admitted_time = 0;
13289         int err;
13290
13291         if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
13292                 return -EOPNOTSUPP;
13293
13294         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
13295             !info->attrs[NL80211_ATTR_USER_PRIO])
13296                 return -EINVAL;
13297
13298         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13299         up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
13300
13301         /* WMM uses TIDs 0-7 even for TSPEC */
13302         if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
13303                 /* TODO: handle 802.11 TSPEC/admission control
13304                  * need more attributes for that (e.g. BA session requirement);
13305                  * change the WMM adminssion test above to allow both then
13306                  */
13307                 return -EINVAL;
13308         }
13309
13310         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13311
13312         if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
13313                 admitted_time =
13314                         nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
13315                 if (!admitted_time)
13316                         return -EINVAL;
13317         }
13318
13319         wdev_lock(wdev);
13320         switch (wdev->iftype) {
13321         case NL80211_IFTYPE_STATION:
13322         case NL80211_IFTYPE_P2P_CLIENT:
13323                 if (wdev->current_bss)
13324                         break;
13325                 err = -ENOTCONN;
13326                 goto out;
13327         default:
13328                 err = -EOPNOTSUPP;
13329                 goto out;
13330         }
13331
13332         err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
13333
13334  out:
13335         wdev_unlock(wdev);
13336         return err;
13337 }
13338
13339 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
13340 {
13341         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13342         struct net_device *dev = info->user_ptr[1];
13343         struct wireless_dev *wdev = dev->ieee80211_ptr;
13344         const u8 *peer;
13345         u8 tsid;
13346         int err;
13347
13348         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
13349                 return -EINVAL;
13350
13351         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
13352         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
13353
13354         wdev_lock(wdev);
13355         err = rdev_del_tx_ts(rdev, dev, tsid, peer);
13356         wdev_unlock(wdev);
13357
13358         return err;
13359 }
13360
13361 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
13362                                        struct genl_info *info)
13363 {
13364         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13365         struct net_device *dev = info->user_ptr[1];
13366         struct wireless_dev *wdev = dev->ieee80211_ptr;
13367         struct cfg80211_chan_def chandef = {};
13368         const u8 *addr;
13369         u8 oper_class;
13370         int err;
13371
13372         if (!rdev->ops->tdls_channel_switch ||
13373             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13374                 return -EOPNOTSUPP;
13375
13376         switch (dev->ieee80211_ptr->iftype) {
13377         case NL80211_IFTYPE_STATION:
13378         case NL80211_IFTYPE_P2P_CLIENT:
13379                 break;
13380         default:
13381                 return -EOPNOTSUPP;
13382         }
13383
13384         if (!info->attrs[NL80211_ATTR_MAC] ||
13385             !info->attrs[NL80211_ATTR_OPER_CLASS])
13386                 return -EINVAL;
13387
13388         err = nl80211_parse_chandef(rdev, info, &chandef);
13389         if (err)
13390                 return err;
13391
13392         /*
13393          * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
13394          * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
13395          * specification is not defined for them.
13396          */
13397         if (chandef.chan->band == NL80211_BAND_2GHZ &&
13398             chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
13399             chandef.width != NL80211_CHAN_WIDTH_20)
13400                 return -EINVAL;
13401
13402         /* we will be active on the TDLS link */
13403         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
13404                                            wdev->iftype))
13405                 return -EINVAL;
13406
13407         /* don't allow switching to DFS channels */
13408         if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
13409                 return -EINVAL;
13410
13411         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13412         oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
13413
13414         wdev_lock(wdev);
13415         err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
13416         wdev_unlock(wdev);
13417
13418         return err;
13419 }
13420
13421 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
13422                                               struct genl_info *info)
13423 {
13424         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13425         struct net_device *dev = info->user_ptr[1];
13426         struct wireless_dev *wdev = dev->ieee80211_ptr;
13427         const u8 *addr;
13428
13429         if (!rdev->ops->tdls_channel_switch ||
13430             !rdev->ops->tdls_cancel_channel_switch ||
13431             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
13432                 return -EOPNOTSUPP;
13433
13434         switch (dev->ieee80211_ptr->iftype) {
13435         case NL80211_IFTYPE_STATION:
13436         case NL80211_IFTYPE_P2P_CLIENT:
13437                 break;
13438         default:
13439                 return -EOPNOTSUPP;
13440         }
13441
13442         if (!info->attrs[NL80211_ATTR_MAC])
13443                 return -EINVAL;
13444
13445         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13446
13447         wdev_lock(wdev);
13448         rdev_tdls_cancel_channel_switch(rdev, dev, addr);
13449         wdev_unlock(wdev);
13450
13451         return 0;
13452 }
13453
13454 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
13455                                             struct genl_info *info)
13456 {
13457         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13458         struct net_device *dev = info->user_ptr[1];
13459         struct wireless_dev *wdev = dev->ieee80211_ptr;
13460         const struct nlattr *nla;
13461         bool enabled;
13462
13463         if (!rdev->ops->set_multicast_to_unicast)
13464                 return -EOPNOTSUPP;
13465
13466         if (wdev->iftype != NL80211_IFTYPE_AP &&
13467             wdev->iftype != NL80211_IFTYPE_P2P_GO)
13468                 return -EOPNOTSUPP;
13469
13470         nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
13471         enabled = nla_get_flag(nla);
13472
13473         return rdev_set_multicast_to_unicast(rdev, dev, enabled);
13474 }
13475
13476 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
13477 {
13478         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13479         struct net_device *dev = info->user_ptr[1];
13480         struct wireless_dev *wdev = dev->ieee80211_ptr;
13481         struct cfg80211_pmk_conf pmk_conf = {};
13482         int ret;
13483
13484         if (wdev->iftype != NL80211_IFTYPE_STATION &&
13485             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13486                 return -EOPNOTSUPP;
13487
13488         if (!wiphy_ext_feature_isset(&rdev->wiphy,
13489                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13490                 return -EOPNOTSUPP;
13491
13492         if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
13493                 return -EINVAL;
13494
13495         wdev_lock(wdev);
13496         if (!wdev->current_bss) {
13497                 ret = -ENOTCONN;
13498                 goto out;
13499         }
13500
13501         pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13502         if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
13503                 ret = -EINVAL;
13504                 goto out;
13505         }
13506
13507         pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
13508         pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
13509         if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
13510             pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
13511                 ret = -EINVAL;
13512                 goto out;
13513         }
13514
13515         if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
13516                 int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13517
13518                 if (r0_name_len != WLAN_PMK_NAME_LEN) {
13519                         ret = -EINVAL;
13520                         goto out;
13521                 }
13522
13523                 pmk_conf.pmk_r0_name =
13524                         nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
13525         }
13526
13527         ret = rdev_set_pmk(rdev, dev, &pmk_conf);
13528 out:
13529         wdev_unlock(wdev);
13530         return ret;
13531 }
13532
13533 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
13534 {
13535         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13536         struct net_device *dev = info->user_ptr[1];
13537         struct wireless_dev *wdev = dev->ieee80211_ptr;
13538         const u8 *aa;
13539         int ret;
13540
13541         if (wdev->iftype != NL80211_IFTYPE_STATION &&
13542             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
13543                 return -EOPNOTSUPP;
13544
13545         if (!wiphy_ext_feature_isset(&rdev->wiphy,
13546                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
13547                 return -EOPNOTSUPP;
13548
13549         if (!info->attrs[NL80211_ATTR_MAC])
13550                 return -EINVAL;
13551
13552         wdev_lock(wdev);
13553         aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
13554         ret = rdev_del_pmk(rdev, dev, aa);
13555         wdev_unlock(wdev);
13556
13557         return ret;
13558 }
13559
13560 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
13561 {
13562         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13563         struct net_device *dev = info->user_ptr[1];
13564         struct cfg80211_external_auth_params params;
13565
13566         if (!rdev->ops->external_auth)
13567                 return -EOPNOTSUPP;
13568
13569         if (!info->attrs[NL80211_ATTR_SSID] &&
13570             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
13571             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
13572                 return -EINVAL;
13573
13574         if (!info->attrs[NL80211_ATTR_BSSID])
13575                 return -EINVAL;
13576
13577         if (!info->attrs[NL80211_ATTR_STATUS_CODE])
13578                 return -EINVAL;
13579
13580         memset(&params, 0, sizeof(params));
13581
13582         if (info->attrs[NL80211_ATTR_SSID]) {
13583                 params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
13584                 if (params.ssid.ssid_len == 0 ||
13585                     params.ssid.ssid_len > IEEE80211_MAX_SSID_LEN)
13586                         return -EINVAL;
13587                 memcpy(params.ssid.ssid,
13588                        nla_data(info->attrs[NL80211_ATTR_SSID]),
13589                        params.ssid.ssid_len);
13590         }
13591
13592         memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
13593                ETH_ALEN);
13594
13595         params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13596
13597         if (info->attrs[NL80211_ATTR_PMKID])
13598                 params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
13599
13600         return rdev_external_auth(rdev, dev, &params);
13601 }
13602
13603 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
13604 {
13605         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13606         struct net_device *dev = info->user_ptr[1];
13607         struct wireless_dev *wdev = dev->ieee80211_ptr;
13608         const u8 *buf;
13609         size_t len;
13610         u8 *dest;
13611         u16 proto;
13612         bool noencrypt;
13613         int err;
13614
13615         if (!wiphy_ext_feature_isset(&rdev->wiphy,
13616                                      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
13617                 return -EOPNOTSUPP;
13618
13619         if (!rdev->ops->tx_control_port)
13620                 return -EOPNOTSUPP;
13621
13622         if (!info->attrs[NL80211_ATTR_FRAME] ||
13623             !info->attrs[NL80211_ATTR_MAC] ||
13624             !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
13625                 GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
13626                 return -EINVAL;
13627         }
13628
13629         wdev_lock(wdev);
13630
13631         switch (wdev->iftype) {
13632         case NL80211_IFTYPE_AP:
13633         case NL80211_IFTYPE_P2P_GO:
13634         case NL80211_IFTYPE_MESH_POINT:
13635                 break;
13636         case NL80211_IFTYPE_ADHOC:
13637         case NL80211_IFTYPE_STATION:
13638         case NL80211_IFTYPE_P2P_CLIENT:
13639                 if (wdev->current_bss)
13640                         break;
13641                 err = -ENOTCONN;
13642                 goto out;
13643         default:
13644                 err = -EOPNOTSUPP;
13645                 goto out;
13646         }
13647
13648         wdev_unlock(wdev);
13649
13650         buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13651         len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13652         dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13653         proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
13654         noencrypt =
13655                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
13656
13657         return rdev_tx_control_port(rdev, dev, buf, len,
13658                                     dest, cpu_to_be16(proto), noencrypt);
13659
13660  out:
13661         wdev_unlock(wdev);
13662         return err;
13663 }
13664
13665 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
13666                                            struct genl_info *info)
13667 {
13668         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13669         struct net_device *dev = info->user_ptr[1];
13670         struct wireless_dev *wdev = dev->ieee80211_ptr;
13671         struct cfg80211_ftm_responder_stats ftm_stats = {};
13672         struct sk_buff *msg;
13673         void *hdr;
13674         struct nlattr *ftm_stats_attr;
13675         int err;
13676
13677         if (wdev->iftype != NL80211_IFTYPE_AP || !wdev->beacon_interval)
13678                 return -EOPNOTSUPP;
13679
13680         err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
13681         if (err)
13682                 return err;
13683
13684         if (!ftm_stats.filled)
13685                 return -ENODATA;
13686
13687         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13688         if (!msg)
13689                 return -ENOMEM;
13690
13691         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13692                              NL80211_CMD_GET_FTM_RESPONDER_STATS);
13693         if (!hdr)
13694                 goto nla_put_failure;
13695
13696         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
13697                 goto nla_put_failure;
13698
13699         ftm_stats_attr = nla_nest_start_noflag(msg,
13700                                                NL80211_ATTR_FTM_RESPONDER_STATS);
13701         if (!ftm_stats_attr)
13702                 goto nla_put_failure;
13703
13704 #define SET_FTM(field, name, type)                                       \
13705         do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13706             nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,            \
13707                              ftm_stats.field))                           \
13708                 goto nla_put_failure; } while (0)
13709 #define SET_FTM_U64(field, name)                                         \
13710         do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
13711             nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,           \
13712                               ftm_stats.field, NL80211_FTM_STATS_PAD))   \
13713                 goto nla_put_failure; } while (0)
13714
13715         SET_FTM(success_num, SUCCESS_NUM, u32);
13716         SET_FTM(partial_num, PARTIAL_NUM, u32);
13717         SET_FTM(failed_num, FAILED_NUM, u32);
13718         SET_FTM(asap_num, ASAP_NUM, u32);
13719         SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
13720         SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
13721         SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
13722         SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
13723         SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
13724 #undef SET_FTM
13725
13726         nla_nest_end(msg, ftm_stats_attr);
13727
13728         genlmsg_end(msg, hdr);
13729         return genlmsg_reply(msg, info);
13730
13731 nla_put_failure:
13732         nlmsg_free(msg);
13733         return -ENOBUFS;
13734 }
13735
13736 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
13737 {
13738         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13739         struct cfg80211_update_owe_info owe_info;
13740         struct net_device *dev = info->user_ptr[1];
13741
13742         if (!rdev->ops->update_owe_info)
13743                 return -EOPNOTSUPP;
13744
13745         if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
13746             !info->attrs[NL80211_ATTR_MAC])
13747                 return -EINVAL;
13748
13749         memset(&owe_info, 0, sizeof(owe_info));
13750         owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
13751         nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
13752
13753         if (info->attrs[NL80211_ATTR_IE]) {
13754                 owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
13755                 owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
13756         }
13757
13758         return rdev_update_owe_info(rdev, dev, &owe_info);
13759 }
13760
13761 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
13762 {
13763         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13764         struct net_device *dev = info->user_ptr[1];
13765         struct wireless_dev *wdev = dev->ieee80211_ptr;
13766         struct station_info sinfo = {};
13767         const u8 *buf;
13768         size_t len;
13769         u8 *dest;
13770         int err;
13771
13772         if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
13773                 return -EOPNOTSUPP;
13774
13775         if (!info->attrs[NL80211_ATTR_MAC] ||
13776             !info->attrs[NL80211_ATTR_FRAME]) {
13777                 GENL_SET_ERR_MSG(info, "Frame or MAC missing");
13778                 return -EINVAL;
13779         }
13780
13781         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
13782                 return -EOPNOTSUPP;
13783
13784         dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
13785         buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
13786         len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
13787
13788         if (len < sizeof(struct ethhdr))
13789                 return -EINVAL;
13790
13791         if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
13792             !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
13793                 return -EINVAL;
13794
13795         err = rdev_get_station(rdev, dev, dest, &sinfo);
13796         if (err)
13797                 return err;
13798
13799         cfg80211_sinfo_release_content(&sinfo);
13800
13801         return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
13802 }
13803
13804 #define NL80211_FLAG_NEED_WIPHY         0x01
13805 #define NL80211_FLAG_NEED_NETDEV        0x02
13806 #define NL80211_FLAG_NEED_RTNL          0x04
13807 #define NL80211_FLAG_CHECK_NETDEV_UP    0x08
13808 #define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
13809                                          NL80211_FLAG_CHECK_NETDEV_UP)
13810 #define NL80211_FLAG_NEED_WDEV          0x10
13811 /* If a netdev is associated, it must be UP, P2P must be started */
13812 #define NL80211_FLAG_NEED_WDEV_UP       (NL80211_FLAG_NEED_WDEV |\
13813                                          NL80211_FLAG_CHECK_NETDEV_UP)
13814 #define NL80211_FLAG_CLEAR_SKB          0x20
13815
13816 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
13817                             struct genl_info *info)
13818 {
13819         struct cfg80211_registered_device *rdev;
13820         struct wireless_dev *wdev;
13821         struct net_device *dev;
13822         bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
13823
13824         if (rtnl)
13825                 rtnl_lock();
13826
13827         if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
13828                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
13829                 if (IS_ERR(rdev)) {
13830                         if (rtnl)
13831                                 rtnl_unlock();
13832                         return PTR_ERR(rdev);
13833                 }
13834                 info->user_ptr[0] = rdev;
13835         } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
13836                    ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13837                 ASSERT_RTNL();
13838
13839                 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
13840                                                   info->attrs);
13841                 if (IS_ERR(wdev)) {
13842                         if (rtnl)
13843                                 rtnl_unlock();
13844                         return PTR_ERR(wdev);
13845                 }
13846
13847                 dev = wdev->netdev;
13848                 rdev = wiphy_to_rdev(wdev->wiphy);
13849
13850                 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
13851                         if (!dev) {
13852                                 if (rtnl)
13853                                         rtnl_unlock();
13854                                 return -EINVAL;
13855                         }
13856
13857                         info->user_ptr[1] = dev;
13858                 } else {
13859                         info->user_ptr[1] = wdev;
13860                 }
13861
13862                 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
13863                     !wdev_running(wdev)) {
13864                         if (rtnl)
13865                                 rtnl_unlock();
13866                         return -ENETDOWN;
13867                 }
13868
13869                 if (dev)
13870                         dev_hold(dev);
13871
13872                 info->user_ptr[0] = rdev;
13873         }
13874
13875         return 0;
13876 }
13877
13878 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
13879                               struct genl_info *info)
13880 {
13881         if (info->user_ptr[1]) {
13882                 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
13883                         struct wireless_dev *wdev = info->user_ptr[1];
13884
13885                         if (wdev->netdev)
13886                                 dev_put(wdev->netdev);
13887                 } else {
13888                         dev_put(info->user_ptr[1]);
13889                 }
13890         }
13891
13892         if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
13893                 rtnl_unlock();
13894
13895         /* If needed, clear the netlink message payload from the SKB
13896          * as it might contain key data that shouldn't stick around on
13897          * the heap after the SKB is freed. The netlink message header
13898          * is still needed for further processing, so leave it intact.
13899          */
13900         if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
13901                 struct nlmsghdr *nlh = nlmsg_hdr(skb);
13902
13903                 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
13904         }
13905 }
13906
13907 static const struct genl_ops nl80211_ops[] = {
13908         {
13909                 .cmd = NL80211_CMD_GET_WIPHY,
13910                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13911                 .doit = nl80211_get_wiphy,
13912                 .dumpit = nl80211_dump_wiphy,
13913                 .done = nl80211_dump_wiphy_done,
13914                 /* can be retrieved by unprivileged users */
13915                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13916                                   NL80211_FLAG_NEED_RTNL,
13917         },
13918         {
13919                 .cmd = NL80211_CMD_SET_WIPHY,
13920                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13921                 .doit = nl80211_set_wiphy,
13922                 .flags = GENL_UNS_ADMIN_PERM,
13923                 .internal_flags = NL80211_FLAG_NEED_RTNL,
13924         },
13925         {
13926                 .cmd = NL80211_CMD_GET_INTERFACE,
13927                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13928                 .doit = nl80211_get_interface,
13929                 .dumpit = nl80211_dump_interface,
13930                 /* can be retrieved by unprivileged users */
13931                 .internal_flags = NL80211_FLAG_NEED_WDEV |
13932                                   NL80211_FLAG_NEED_RTNL,
13933         },
13934         {
13935                 .cmd = NL80211_CMD_SET_INTERFACE,
13936                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13937                 .doit = nl80211_set_interface,
13938                 .flags = GENL_UNS_ADMIN_PERM,
13939                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13940                                   NL80211_FLAG_NEED_RTNL,
13941         },
13942         {
13943                 .cmd = NL80211_CMD_NEW_INTERFACE,
13944                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13945                 .doit = nl80211_new_interface,
13946                 .flags = GENL_UNS_ADMIN_PERM,
13947                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13948                                   NL80211_FLAG_NEED_RTNL,
13949         },
13950         {
13951                 .cmd = NL80211_CMD_DEL_INTERFACE,
13952                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13953                 .doit = nl80211_del_interface,
13954                 .flags = GENL_UNS_ADMIN_PERM,
13955                 .internal_flags = NL80211_FLAG_NEED_WDEV |
13956                                   NL80211_FLAG_NEED_RTNL,
13957         },
13958         {
13959                 .cmd = NL80211_CMD_GET_KEY,
13960                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13961                 .doit = nl80211_get_key,
13962                 .flags = GENL_UNS_ADMIN_PERM,
13963                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13964                                   NL80211_FLAG_NEED_RTNL,
13965         },
13966         {
13967                 .cmd = NL80211_CMD_SET_KEY,
13968                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13969                 .doit = nl80211_set_key,
13970                 .flags = GENL_UNS_ADMIN_PERM,
13971                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13972                                   NL80211_FLAG_NEED_RTNL |
13973                                   NL80211_FLAG_CLEAR_SKB,
13974         },
13975         {
13976                 .cmd = NL80211_CMD_NEW_KEY,
13977                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13978                 .doit = nl80211_new_key,
13979                 .flags = GENL_UNS_ADMIN_PERM,
13980                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13981                                   NL80211_FLAG_NEED_RTNL |
13982                                   NL80211_FLAG_CLEAR_SKB,
13983         },
13984         {
13985                 .cmd = NL80211_CMD_DEL_KEY,
13986                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13987                 .doit = nl80211_del_key,
13988                 .flags = GENL_UNS_ADMIN_PERM,
13989                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13990                                   NL80211_FLAG_NEED_RTNL,
13991         },
13992         {
13993                 .cmd = NL80211_CMD_SET_BEACON,
13994                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
13995                 .flags = GENL_UNS_ADMIN_PERM,
13996                 .doit = nl80211_set_beacon,
13997                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13998                                   NL80211_FLAG_NEED_RTNL,
13999         },
14000         {
14001                 .cmd = NL80211_CMD_START_AP,
14002                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14003                 .flags = GENL_UNS_ADMIN_PERM,
14004                 .doit = nl80211_start_ap,
14005                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14006                                   NL80211_FLAG_NEED_RTNL,
14007         },
14008         {
14009                 .cmd = NL80211_CMD_STOP_AP,
14010                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14011                 .flags = GENL_UNS_ADMIN_PERM,
14012                 .doit = nl80211_stop_ap,
14013                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14014                                   NL80211_FLAG_NEED_RTNL,
14015         },
14016         {
14017                 .cmd = NL80211_CMD_GET_STATION,
14018                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14019                 .doit = nl80211_get_station,
14020                 .dumpit = nl80211_dump_station,
14021                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14022                                   NL80211_FLAG_NEED_RTNL,
14023         },
14024         {
14025                 .cmd = NL80211_CMD_SET_STATION,
14026                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14027                 .doit = nl80211_set_station,
14028                 .flags = GENL_UNS_ADMIN_PERM,
14029                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14030                                   NL80211_FLAG_NEED_RTNL,
14031         },
14032         {
14033                 .cmd = NL80211_CMD_NEW_STATION,
14034                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14035                 .doit = nl80211_new_station,
14036                 .flags = GENL_UNS_ADMIN_PERM,
14037                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14038                                   NL80211_FLAG_NEED_RTNL,
14039         },
14040         {
14041                 .cmd = NL80211_CMD_DEL_STATION,
14042                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14043                 .doit = nl80211_del_station,
14044                 .flags = GENL_UNS_ADMIN_PERM,
14045                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14046                                   NL80211_FLAG_NEED_RTNL,
14047         },
14048         {
14049                 .cmd = NL80211_CMD_GET_MPATH,
14050                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14051                 .doit = nl80211_get_mpath,
14052                 .dumpit = nl80211_dump_mpath,
14053                 .flags = GENL_UNS_ADMIN_PERM,
14054                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14055                                   NL80211_FLAG_NEED_RTNL,
14056         },
14057         {
14058                 .cmd = NL80211_CMD_GET_MPP,
14059                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14060                 .doit = nl80211_get_mpp,
14061                 .dumpit = nl80211_dump_mpp,
14062                 .flags = GENL_UNS_ADMIN_PERM,
14063                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14064                                   NL80211_FLAG_NEED_RTNL,
14065         },
14066         {
14067                 .cmd = NL80211_CMD_SET_MPATH,
14068                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14069                 .doit = nl80211_set_mpath,
14070                 .flags = GENL_UNS_ADMIN_PERM,
14071                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14072                                   NL80211_FLAG_NEED_RTNL,
14073         },
14074         {
14075                 .cmd = NL80211_CMD_NEW_MPATH,
14076                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14077                 .doit = nl80211_new_mpath,
14078                 .flags = GENL_UNS_ADMIN_PERM,
14079                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14080                                   NL80211_FLAG_NEED_RTNL,
14081         },
14082         {
14083                 .cmd = NL80211_CMD_DEL_MPATH,
14084                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14085                 .doit = nl80211_del_mpath,
14086                 .flags = GENL_UNS_ADMIN_PERM,
14087                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14088                                   NL80211_FLAG_NEED_RTNL,
14089         },
14090         {
14091                 .cmd = NL80211_CMD_SET_BSS,
14092                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14093                 .doit = nl80211_set_bss,
14094                 .flags = GENL_UNS_ADMIN_PERM,
14095                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14096                                   NL80211_FLAG_NEED_RTNL,
14097         },
14098         {
14099                 .cmd = NL80211_CMD_GET_REG,
14100                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14101                 .doit = nl80211_get_reg_do,
14102                 .dumpit = nl80211_get_reg_dump,
14103                 .internal_flags = NL80211_FLAG_NEED_RTNL,
14104                 /* can be retrieved by unprivileged users */
14105         },
14106 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
14107         {
14108                 .cmd = NL80211_CMD_SET_REG,
14109                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14110                 .doit = nl80211_set_reg,
14111                 .flags = GENL_ADMIN_PERM,
14112                 .internal_flags = NL80211_FLAG_NEED_RTNL,
14113         },
14114 #endif
14115         {
14116                 .cmd = NL80211_CMD_REQ_SET_REG,
14117                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14118                 .doit = nl80211_req_set_reg,
14119                 .flags = GENL_ADMIN_PERM,
14120         },
14121         {
14122                 .cmd = NL80211_CMD_RELOAD_REGDB,
14123                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14124                 .doit = nl80211_reload_regdb,
14125                 .flags = GENL_ADMIN_PERM,
14126         },
14127         {
14128                 .cmd = NL80211_CMD_GET_MESH_CONFIG,
14129                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14130                 .doit = nl80211_get_mesh_config,
14131                 /* can be retrieved by unprivileged users */
14132                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14133                                   NL80211_FLAG_NEED_RTNL,
14134         },
14135         {
14136                 .cmd = NL80211_CMD_SET_MESH_CONFIG,
14137                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14138                 .doit = nl80211_update_mesh_config,
14139                 .flags = GENL_UNS_ADMIN_PERM,
14140                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14141                                   NL80211_FLAG_NEED_RTNL,
14142         },
14143         {
14144                 .cmd = NL80211_CMD_TRIGGER_SCAN,
14145                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14146                 .doit = nl80211_trigger_scan,
14147                 .flags = GENL_UNS_ADMIN_PERM,
14148                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14149                                   NL80211_FLAG_NEED_RTNL,
14150         },
14151         {
14152                 .cmd = NL80211_CMD_ABORT_SCAN,
14153                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14154                 .doit = nl80211_abort_scan,
14155                 .flags = GENL_UNS_ADMIN_PERM,
14156                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14157                                   NL80211_FLAG_NEED_RTNL,
14158         },
14159         {
14160                 .cmd = NL80211_CMD_GET_SCAN,
14161                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14162                 .dumpit = nl80211_dump_scan,
14163         },
14164         {
14165                 .cmd = NL80211_CMD_START_SCHED_SCAN,
14166                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14167                 .doit = nl80211_start_sched_scan,
14168                 .flags = GENL_UNS_ADMIN_PERM,
14169                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14170                                   NL80211_FLAG_NEED_RTNL,
14171         },
14172         {
14173                 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
14174                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14175                 .doit = nl80211_stop_sched_scan,
14176                 .flags = GENL_UNS_ADMIN_PERM,
14177                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14178                                   NL80211_FLAG_NEED_RTNL,
14179         },
14180         {
14181                 .cmd = NL80211_CMD_AUTHENTICATE,
14182                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14183                 .doit = nl80211_authenticate,
14184                 .flags = GENL_UNS_ADMIN_PERM,
14185                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14186                                   NL80211_FLAG_NEED_RTNL |
14187                                   NL80211_FLAG_CLEAR_SKB,
14188         },
14189         {
14190                 .cmd = NL80211_CMD_ASSOCIATE,
14191                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14192                 .doit = nl80211_associate,
14193                 .flags = GENL_UNS_ADMIN_PERM,
14194                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14195                                   NL80211_FLAG_NEED_RTNL |
14196                                   NL80211_FLAG_CLEAR_SKB,
14197         },
14198         {
14199                 .cmd = NL80211_CMD_DEAUTHENTICATE,
14200                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14201                 .doit = nl80211_deauthenticate,
14202                 .flags = GENL_UNS_ADMIN_PERM,
14203                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14204                                   NL80211_FLAG_NEED_RTNL,
14205         },
14206         {
14207                 .cmd = NL80211_CMD_DISASSOCIATE,
14208                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14209                 .doit = nl80211_disassociate,
14210                 .flags = GENL_UNS_ADMIN_PERM,
14211                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14212                                   NL80211_FLAG_NEED_RTNL,
14213         },
14214         {
14215                 .cmd = NL80211_CMD_JOIN_IBSS,
14216                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14217                 .doit = nl80211_join_ibss,
14218                 .flags = GENL_UNS_ADMIN_PERM,
14219                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14220                                   NL80211_FLAG_NEED_RTNL,
14221         },
14222         {
14223                 .cmd = NL80211_CMD_LEAVE_IBSS,
14224                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14225                 .doit = nl80211_leave_ibss,
14226                 .flags = GENL_UNS_ADMIN_PERM,
14227                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14228                                   NL80211_FLAG_NEED_RTNL,
14229         },
14230 #ifdef CONFIG_NL80211_TESTMODE
14231         {
14232                 .cmd = NL80211_CMD_TESTMODE,
14233                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14234                 .doit = nl80211_testmode_do,
14235                 .dumpit = nl80211_testmode_dump,
14236                 .flags = GENL_UNS_ADMIN_PERM,
14237                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14238                                   NL80211_FLAG_NEED_RTNL,
14239         },
14240 #endif
14241         {
14242                 .cmd = NL80211_CMD_CONNECT,
14243                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14244                 .doit = nl80211_connect,
14245                 .flags = GENL_UNS_ADMIN_PERM,
14246                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14247                                   NL80211_FLAG_NEED_RTNL |
14248                                   NL80211_FLAG_CLEAR_SKB,
14249         },
14250         {
14251                 .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
14252                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14253                 .doit = nl80211_update_connect_params,
14254                 .flags = GENL_ADMIN_PERM,
14255                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14256                                   NL80211_FLAG_NEED_RTNL |
14257                                   NL80211_FLAG_CLEAR_SKB,
14258         },
14259         {
14260                 .cmd = NL80211_CMD_DISCONNECT,
14261                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14262                 .doit = nl80211_disconnect,
14263                 .flags = GENL_UNS_ADMIN_PERM,
14264                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14265                                   NL80211_FLAG_NEED_RTNL,
14266         },
14267         {
14268                 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
14269                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14270                 .doit = nl80211_wiphy_netns,
14271                 .flags = GENL_UNS_ADMIN_PERM,
14272                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14273                                   NL80211_FLAG_NEED_RTNL,
14274         },
14275         {
14276                 .cmd = NL80211_CMD_GET_SURVEY,
14277                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14278                 .dumpit = nl80211_dump_survey,
14279         },
14280         {
14281                 .cmd = NL80211_CMD_SET_PMKSA,
14282                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14283                 .doit = nl80211_setdel_pmksa,
14284                 .flags = GENL_UNS_ADMIN_PERM,
14285                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14286                                   NL80211_FLAG_NEED_RTNL |
14287                                   NL80211_FLAG_CLEAR_SKB,
14288         },
14289         {
14290                 .cmd = NL80211_CMD_DEL_PMKSA,
14291                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14292                 .doit = nl80211_setdel_pmksa,
14293                 .flags = GENL_UNS_ADMIN_PERM,
14294                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14295                                   NL80211_FLAG_NEED_RTNL,
14296         },
14297         {
14298                 .cmd = NL80211_CMD_FLUSH_PMKSA,
14299                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14300                 .doit = nl80211_flush_pmksa,
14301                 .flags = GENL_UNS_ADMIN_PERM,
14302                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14303                                   NL80211_FLAG_NEED_RTNL,
14304         },
14305         {
14306                 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
14307                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14308                 .doit = nl80211_remain_on_channel,
14309                 .flags = GENL_UNS_ADMIN_PERM,
14310                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14311                                   NL80211_FLAG_NEED_RTNL,
14312         },
14313         {
14314                 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14315                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14316                 .doit = nl80211_cancel_remain_on_channel,
14317                 .flags = GENL_UNS_ADMIN_PERM,
14318                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14319                                   NL80211_FLAG_NEED_RTNL,
14320         },
14321         {
14322                 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
14323                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14324                 .doit = nl80211_set_tx_bitrate_mask,
14325                 .flags = GENL_UNS_ADMIN_PERM,
14326                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14327                                   NL80211_FLAG_NEED_RTNL,
14328         },
14329         {
14330                 .cmd = NL80211_CMD_REGISTER_FRAME,
14331                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14332                 .doit = nl80211_register_mgmt,
14333                 .flags = GENL_UNS_ADMIN_PERM,
14334                 .internal_flags = NL80211_FLAG_NEED_WDEV |
14335                                   NL80211_FLAG_NEED_RTNL,
14336         },
14337         {
14338                 .cmd = NL80211_CMD_FRAME,
14339                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14340                 .doit = nl80211_tx_mgmt,
14341                 .flags = GENL_UNS_ADMIN_PERM,
14342                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14343                                   NL80211_FLAG_NEED_RTNL,
14344         },
14345         {
14346                 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
14347                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14348                 .doit = nl80211_tx_mgmt_cancel_wait,
14349                 .flags = GENL_UNS_ADMIN_PERM,
14350                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14351                                   NL80211_FLAG_NEED_RTNL,
14352         },
14353         {
14354                 .cmd = NL80211_CMD_SET_POWER_SAVE,
14355                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14356                 .doit = nl80211_set_power_save,
14357                 .flags = GENL_UNS_ADMIN_PERM,
14358                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14359                                   NL80211_FLAG_NEED_RTNL,
14360         },
14361         {
14362                 .cmd = NL80211_CMD_GET_POWER_SAVE,
14363                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14364                 .doit = nl80211_get_power_save,
14365                 /* can be retrieved by unprivileged users */
14366                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14367                                   NL80211_FLAG_NEED_RTNL,
14368         },
14369         {
14370                 .cmd = NL80211_CMD_SET_CQM,
14371                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14372                 .doit = nl80211_set_cqm,
14373                 .flags = GENL_UNS_ADMIN_PERM,
14374                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14375                                   NL80211_FLAG_NEED_RTNL,
14376         },
14377         {
14378                 .cmd = NL80211_CMD_SET_CHANNEL,
14379                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14380                 .doit = nl80211_set_channel,
14381                 .flags = GENL_UNS_ADMIN_PERM,
14382                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14383                                   NL80211_FLAG_NEED_RTNL,
14384         },
14385         {
14386                 .cmd = NL80211_CMD_SET_WDS_PEER,
14387                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14388                 .doit = nl80211_set_wds_peer,
14389                 .flags = GENL_UNS_ADMIN_PERM,
14390                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14391                                   NL80211_FLAG_NEED_RTNL,
14392         },
14393         {
14394                 .cmd = NL80211_CMD_JOIN_MESH,
14395                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14396                 .doit = nl80211_join_mesh,
14397                 .flags = GENL_UNS_ADMIN_PERM,
14398                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14399                                   NL80211_FLAG_NEED_RTNL,
14400         },
14401         {
14402                 .cmd = NL80211_CMD_LEAVE_MESH,
14403                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14404                 .doit = nl80211_leave_mesh,
14405                 .flags = GENL_UNS_ADMIN_PERM,
14406                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14407                                   NL80211_FLAG_NEED_RTNL,
14408         },
14409         {
14410                 .cmd = NL80211_CMD_JOIN_OCB,
14411                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14412                 .doit = nl80211_join_ocb,
14413                 .flags = GENL_UNS_ADMIN_PERM,
14414                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14415                                   NL80211_FLAG_NEED_RTNL,
14416         },
14417         {
14418                 .cmd = NL80211_CMD_LEAVE_OCB,
14419                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14420                 .doit = nl80211_leave_ocb,
14421                 .flags = GENL_UNS_ADMIN_PERM,
14422                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14423                                   NL80211_FLAG_NEED_RTNL,
14424         },
14425 #ifdef CONFIG_PM
14426         {
14427                 .cmd = NL80211_CMD_GET_WOWLAN,
14428                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14429                 .doit = nl80211_get_wowlan,
14430                 /* can be retrieved by unprivileged users */
14431                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14432                                   NL80211_FLAG_NEED_RTNL,
14433         },
14434         {
14435                 .cmd = NL80211_CMD_SET_WOWLAN,
14436                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14437                 .doit = nl80211_set_wowlan,
14438                 .flags = GENL_UNS_ADMIN_PERM,
14439                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14440                                   NL80211_FLAG_NEED_RTNL,
14441         },
14442 #endif
14443         {
14444                 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
14445                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14446                 .doit = nl80211_set_rekey_data,
14447                 .flags = GENL_UNS_ADMIN_PERM,
14448                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14449                                   NL80211_FLAG_NEED_RTNL |
14450                                   NL80211_FLAG_CLEAR_SKB,
14451         },
14452         {
14453                 .cmd = NL80211_CMD_TDLS_MGMT,
14454                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14455                 .doit = nl80211_tdls_mgmt,
14456                 .flags = GENL_UNS_ADMIN_PERM,
14457                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14458                                   NL80211_FLAG_NEED_RTNL,
14459         },
14460         {
14461                 .cmd = NL80211_CMD_TDLS_OPER,
14462                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14463                 .doit = nl80211_tdls_oper,
14464                 .flags = GENL_UNS_ADMIN_PERM,
14465                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14466                                   NL80211_FLAG_NEED_RTNL,
14467         },
14468         {
14469                 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
14470                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14471                 .doit = nl80211_register_unexpected_frame,
14472                 .flags = GENL_UNS_ADMIN_PERM,
14473                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14474                                   NL80211_FLAG_NEED_RTNL,
14475         },
14476         {
14477                 .cmd = NL80211_CMD_PROBE_CLIENT,
14478                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14479                 .doit = nl80211_probe_client,
14480                 .flags = GENL_UNS_ADMIN_PERM,
14481                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14482                                   NL80211_FLAG_NEED_RTNL,
14483         },
14484         {
14485                 .cmd = NL80211_CMD_REGISTER_BEACONS,
14486                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14487                 .doit = nl80211_register_beacons,
14488                 .flags = GENL_UNS_ADMIN_PERM,
14489                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14490                                   NL80211_FLAG_NEED_RTNL,
14491         },
14492         {
14493                 .cmd = NL80211_CMD_SET_NOACK_MAP,
14494                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14495                 .doit = nl80211_set_noack_map,
14496                 .flags = GENL_UNS_ADMIN_PERM,
14497                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14498                                   NL80211_FLAG_NEED_RTNL,
14499         },
14500         {
14501                 .cmd = NL80211_CMD_START_P2P_DEVICE,
14502                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14503                 .doit = nl80211_start_p2p_device,
14504                 .flags = GENL_UNS_ADMIN_PERM,
14505                 .internal_flags = NL80211_FLAG_NEED_WDEV |
14506                                   NL80211_FLAG_NEED_RTNL,
14507         },
14508         {
14509                 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
14510                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14511                 .doit = nl80211_stop_p2p_device,
14512                 .flags = GENL_UNS_ADMIN_PERM,
14513                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14514                                   NL80211_FLAG_NEED_RTNL,
14515         },
14516         {
14517                 .cmd = NL80211_CMD_START_NAN,
14518                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14519                 .doit = nl80211_start_nan,
14520                 .flags = GENL_ADMIN_PERM,
14521                 .internal_flags = NL80211_FLAG_NEED_WDEV |
14522                                   NL80211_FLAG_NEED_RTNL,
14523         },
14524         {
14525                 .cmd = NL80211_CMD_STOP_NAN,
14526                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14527                 .doit = nl80211_stop_nan,
14528                 .flags = GENL_ADMIN_PERM,
14529                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14530                                   NL80211_FLAG_NEED_RTNL,
14531         },
14532         {
14533                 .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
14534                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14535                 .doit = nl80211_nan_add_func,
14536                 .flags = GENL_ADMIN_PERM,
14537                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14538                                   NL80211_FLAG_NEED_RTNL,
14539         },
14540         {
14541                 .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
14542                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14543                 .doit = nl80211_nan_del_func,
14544                 .flags = GENL_ADMIN_PERM,
14545                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14546                                   NL80211_FLAG_NEED_RTNL,
14547         },
14548         {
14549                 .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
14550                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14551                 .doit = nl80211_nan_change_config,
14552                 .flags = GENL_ADMIN_PERM,
14553                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14554                                   NL80211_FLAG_NEED_RTNL,
14555         },
14556         {
14557                 .cmd = NL80211_CMD_SET_MCAST_RATE,
14558                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14559                 .doit = nl80211_set_mcast_rate,
14560                 .flags = GENL_UNS_ADMIN_PERM,
14561                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14562                                   NL80211_FLAG_NEED_RTNL,
14563         },
14564         {
14565                 .cmd = NL80211_CMD_SET_MAC_ACL,
14566                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14567                 .doit = nl80211_set_mac_acl,
14568                 .flags = GENL_UNS_ADMIN_PERM,
14569                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14570                                   NL80211_FLAG_NEED_RTNL,
14571         },
14572         {
14573                 .cmd = NL80211_CMD_RADAR_DETECT,
14574                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14575                 .doit = nl80211_start_radar_detection,
14576                 .flags = GENL_UNS_ADMIN_PERM,
14577                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14578                                   NL80211_FLAG_NEED_RTNL,
14579         },
14580         {
14581                 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
14582                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14583                 .doit = nl80211_get_protocol_features,
14584         },
14585         {
14586                 .cmd = NL80211_CMD_UPDATE_FT_IES,
14587                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14588                 .doit = nl80211_update_ft_ies,
14589                 .flags = GENL_UNS_ADMIN_PERM,
14590                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14591                                   NL80211_FLAG_NEED_RTNL,
14592         },
14593         {
14594                 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
14595                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14596                 .doit = nl80211_crit_protocol_start,
14597                 .flags = GENL_UNS_ADMIN_PERM,
14598                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14599                                   NL80211_FLAG_NEED_RTNL,
14600         },
14601         {
14602                 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
14603                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14604                 .doit = nl80211_crit_protocol_stop,
14605                 .flags = GENL_UNS_ADMIN_PERM,
14606                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14607                                   NL80211_FLAG_NEED_RTNL,
14608         },
14609         {
14610                 .cmd = NL80211_CMD_GET_COALESCE,
14611                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14612                 .doit = nl80211_get_coalesce,
14613                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14614                                   NL80211_FLAG_NEED_RTNL,
14615         },
14616         {
14617                 .cmd = NL80211_CMD_SET_COALESCE,
14618                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14619                 .doit = nl80211_set_coalesce,
14620                 .flags = GENL_UNS_ADMIN_PERM,
14621                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14622                                   NL80211_FLAG_NEED_RTNL,
14623         },
14624         {
14625                 .cmd = NL80211_CMD_CHANNEL_SWITCH,
14626                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14627                 .doit = nl80211_channel_switch,
14628                 .flags = GENL_UNS_ADMIN_PERM,
14629                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14630                                   NL80211_FLAG_NEED_RTNL,
14631         },
14632         {
14633                 .cmd = NL80211_CMD_VENDOR,
14634                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14635                 .doit = nl80211_vendor_cmd,
14636                 .dumpit = nl80211_vendor_cmd_dump,
14637                 .flags = GENL_UNS_ADMIN_PERM,
14638                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
14639                                   NL80211_FLAG_NEED_RTNL |
14640                                   NL80211_FLAG_CLEAR_SKB,
14641         },
14642         {
14643                 .cmd = NL80211_CMD_SET_QOS_MAP,
14644                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14645                 .doit = nl80211_set_qos_map,
14646                 .flags = GENL_UNS_ADMIN_PERM,
14647                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14648                                   NL80211_FLAG_NEED_RTNL,
14649         },
14650         {
14651                 .cmd = NL80211_CMD_ADD_TX_TS,
14652                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14653                 .doit = nl80211_add_tx_ts,
14654                 .flags = GENL_UNS_ADMIN_PERM,
14655                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14656                                   NL80211_FLAG_NEED_RTNL,
14657         },
14658         {
14659                 .cmd = NL80211_CMD_DEL_TX_TS,
14660                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14661                 .doit = nl80211_del_tx_ts,
14662                 .flags = GENL_UNS_ADMIN_PERM,
14663                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14664                                   NL80211_FLAG_NEED_RTNL,
14665         },
14666         {
14667                 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
14668                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14669                 .doit = nl80211_tdls_channel_switch,
14670                 .flags = GENL_UNS_ADMIN_PERM,
14671                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14672                                   NL80211_FLAG_NEED_RTNL,
14673         },
14674         {
14675                 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
14676                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14677                 .doit = nl80211_tdls_cancel_channel_switch,
14678                 .flags = GENL_UNS_ADMIN_PERM,
14679                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14680                                   NL80211_FLAG_NEED_RTNL,
14681         },
14682         {
14683                 .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
14684                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14685                 .doit = nl80211_set_multicast_to_unicast,
14686                 .flags = GENL_UNS_ADMIN_PERM,
14687                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14688                                   NL80211_FLAG_NEED_RTNL,
14689         },
14690         {
14691                 .cmd = NL80211_CMD_SET_PMK,
14692                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14693                 .doit = nl80211_set_pmk,
14694                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14695                                   NL80211_FLAG_NEED_RTNL |
14696                                   NL80211_FLAG_CLEAR_SKB,
14697         },
14698         {
14699                 .cmd = NL80211_CMD_DEL_PMK,
14700                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14701                 .doit = nl80211_del_pmk,
14702                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14703                                   NL80211_FLAG_NEED_RTNL,
14704         },
14705         {
14706                 .cmd = NL80211_CMD_EXTERNAL_AUTH,
14707                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14708                 .doit = nl80211_external_auth,
14709                 .flags = GENL_ADMIN_PERM,
14710                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14711                                   NL80211_FLAG_NEED_RTNL,
14712         },
14713         {
14714                 .cmd = NL80211_CMD_CONTROL_PORT_FRAME,
14715                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14716                 .doit = nl80211_tx_control_port,
14717                 .flags = GENL_UNS_ADMIN_PERM,
14718                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14719                                   NL80211_FLAG_NEED_RTNL,
14720         },
14721         {
14722                 .cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
14723                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14724                 .doit = nl80211_get_ftm_responder_stats,
14725                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
14726                                   NL80211_FLAG_NEED_RTNL,
14727         },
14728         {
14729                 .cmd = NL80211_CMD_PEER_MEASUREMENT_START,
14730                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14731                 .doit = nl80211_pmsr_start,
14732                 .flags = GENL_UNS_ADMIN_PERM,
14733                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
14734                                   NL80211_FLAG_NEED_RTNL,
14735         },
14736         {
14737                 .cmd = NL80211_CMD_NOTIFY_RADAR,
14738                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
14739                 .doit = nl80211_notify_radar_detection,
14740                 .flags = GENL_UNS_ADMIN_PERM,
14741                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14742                                   NL80211_FLAG_NEED_RTNL,
14743         },
14744         {
14745                 .cmd = NL80211_CMD_UPDATE_OWE_INFO,
14746                 .doit = nl80211_update_owe_info,
14747                 .flags = GENL_ADMIN_PERM,
14748                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14749                                   NL80211_FLAG_NEED_RTNL,
14750         },
14751         {
14752                 .cmd = NL80211_CMD_PROBE_MESH_LINK,
14753                 .doit = nl80211_probe_mesh_link,
14754                 .flags = GENL_UNS_ADMIN_PERM,
14755                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
14756                                   NL80211_FLAG_NEED_RTNL,
14757         },
14758 };
14759
14760 static struct genl_family nl80211_fam __ro_after_init = {
14761         .name = NL80211_GENL_NAME,      /* have users key off the name instead */
14762         .hdrsize = 0,                   /* no private header */
14763         .version = 1,                   /* no particular meaning now */
14764         .maxattr = NL80211_ATTR_MAX,
14765         .policy = nl80211_policy,
14766         .netnsok = true,
14767         .pre_doit = nl80211_pre_doit,
14768         .post_doit = nl80211_post_doit,
14769         .module = THIS_MODULE,
14770         .ops = nl80211_ops,
14771         .n_ops = ARRAY_SIZE(nl80211_ops),
14772         .mcgrps = nl80211_mcgrps,
14773         .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
14774         .parallel_ops = true,
14775 };
14776
14777 /* notification functions */
14778
14779 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
14780                           enum nl80211_commands cmd)
14781 {
14782         struct sk_buff *msg;
14783         struct nl80211_dump_wiphy_state state = {};
14784
14785         WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
14786                 cmd != NL80211_CMD_DEL_WIPHY);
14787
14788         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14789         if (!msg)
14790                 return;
14791
14792         if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
14793                 nlmsg_free(msg);
14794                 return;
14795         }
14796
14797         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14798                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
14799 }
14800
14801 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
14802                                 struct wireless_dev *wdev,
14803                                 enum nl80211_commands cmd)
14804 {
14805         struct sk_buff *msg;
14806
14807         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14808         if (!msg)
14809                 return;
14810
14811         if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
14812                 nlmsg_free(msg);
14813                 return;
14814         }
14815
14816         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14817                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
14818 }
14819
14820 static int nl80211_add_scan_req(struct sk_buff *msg,
14821                                 struct cfg80211_registered_device *rdev)
14822 {
14823         struct cfg80211_scan_request *req = rdev->scan_req;
14824         struct nlattr *nest;
14825         int i;
14826
14827         if (WARN_ON(!req))
14828                 return 0;
14829
14830         nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
14831         if (!nest)
14832                 goto nla_put_failure;
14833         for (i = 0; i < req->n_ssids; i++) {
14834                 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
14835                         goto nla_put_failure;
14836         }
14837         nla_nest_end(msg, nest);
14838
14839         nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
14840         if (!nest)
14841                 goto nla_put_failure;
14842         for (i = 0; i < req->n_channels; i++) {
14843                 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
14844                         goto nla_put_failure;
14845         }
14846         nla_nest_end(msg, nest);
14847
14848         if (req->ie &&
14849             nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
14850                 goto nla_put_failure;
14851
14852         if (req->flags &&
14853             nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
14854                 goto nla_put_failure;
14855
14856         if (req->info.scan_start_tsf &&
14857             (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
14858                                req->info.scan_start_tsf, NL80211_BSS_PAD) ||
14859              nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
14860                      req->info.tsf_bssid)))
14861                 goto nla_put_failure;
14862
14863         return 0;
14864  nla_put_failure:
14865         return -ENOBUFS;
14866 }
14867
14868 static int nl80211_prep_scan_msg(struct sk_buff *msg,
14869                                  struct cfg80211_registered_device *rdev,
14870                                  struct wireless_dev *wdev,
14871                                  u32 portid, u32 seq, int flags,
14872                                  u32 cmd)
14873 {
14874         void *hdr;
14875
14876         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
14877         if (!hdr)
14878                 return -1;
14879
14880         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14881             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14882                                          wdev->netdev->ifindex)) ||
14883             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14884                               NL80211_ATTR_PAD))
14885                 goto nla_put_failure;
14886
14887         /* ignore errors and send incomplete event anyway */
14888         nl80211_add_scan_req(msg, rdev);
14889
14890         genlmsg_end(msg, hdr);
14891         return 0;
14892
14893  nla_put_failure:
14894         genlmsg_cancel(msg, hdr);
14895         return -EMSGSIZE;
14896 }
14897
14898 static int
14899 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
14900                             struct cfg80211_sched_scan_request *req, u32 cmd)
14901 {
14902         void *hdr;
14903
14904         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14905         if (!hdr)
14906                 return -1;
14907
14908         if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
14909                         wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
14910             nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
14911             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
14912                               NL80211_ATTR_PAD))
14913                 goto nla_put_failure;
14914
14915         genlmsg_end(msg, hdr);
14916         return 0;
14917
14918  nla_put_failure:
14919         genlmsg_cancel(msg, hdr);
14920         return -EMSGSIZE;
14921 }
14922
14923 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
14924                              struct wireless_dev *wdev)
14925 {
14926         struct sk_buff *msg;
14927
14928         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14929         if (!msg)
14930                 return;
14931
14932         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14933                                   NL80211_CMD_TRIGGER_SCAN) < 0) {
14934                 nlmsg_free(msg);
14935                 return;
14936         }
14937
14938         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14939                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
14940 }
14941
14942 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
14943                                        struct wireless_dev *wdev, bool aborted)
14944 {
14945         struct sk_buff *msg;
14946
14947         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14948         if (!msg)
14949                 return NULL;
14950
14951         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
14952                                   aborted ? NL80211_CMD_SCAN_ABORTED :
14953                                             NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
14954                 nlmsg_free(msg);
14955                 return NULL;
14956         }
14957
14958         return msg;
14959 }
14960
14961 /* send message created by nl80211_build_scan_msg() */
14962 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
14963                            struct sk_buff *msg)
14964 {
14965         if (!msg)
14966                 return;
14967
14968         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14969                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
14970 }
14971
14972 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
14973 {
14974         struct sk_buff *msg;
14975
14976         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14977         if (!msg)
14978                 return;
14979
14980         if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
14981                 nlmsg_free(msg);
14982                 return;
14983         }
14984
14985         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
14986                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
14987 }
14988
14989 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
14990                                           struct regulatory_request *request)
14991 {
14992         /* Userspace can always count this one always being set */
14993         if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
14994                 goto nla_put_failure;
14995
14996         if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
14997                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
14998                                NL80211_REGDOM_TYPE_WORLD))
14999                         goto nla_put_failure;
15000         } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
15001                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15002                                NL80211_REGDOM_TYPE_CUSTOM_WORLD))
15003                         goto nla_put_failure;
15004         } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
15005                    request->intersect) {
15006                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15007                                NL80211_REGDOM_TYPE_INTERSECTION))
15008                         goto nla_put_failure;
15009         } else {
15010                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
15011                                NL80211_REGDOM_TYPE_COUNTRY) ||
15012                     nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
15013                                    request->alpha2))
15014                         goto nla_put_failure;
15015         }
15016
15017         if (request->wiphy_idx != WIPHY_IDX_INVALID) {
15018                 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
15019
15020                 if (wiphy &&
15021                     nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
15022                         goto nla_put_failure;
15023
15024                 if (wiphy &&
15025                     wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
15026                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
15027                         goto nla_put_failure;
15028         }
15029
15030         return true;
15031
15032 nla_put_failure:
15033         return false;
15034 }
15035
15036 /*
15037  * This can happen on global regulatory changes or device specific settings
15038  * based on custom regulatory domains.
15039  */
15040 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
15041                                      struct regulatory_request *request)
15042 {
15043         struct sk_buff *msg;
15044         void *hdr;
15045
15046         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15047         if (!msg)
15048                 return;
15049
15050         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
15051         if (!hdr)
15052                 goto nla_put_failure;
15053
15054         if (!nl80211_reg_change_event_fill(msg, request))
15055                 goto nla_put_failure;
15056
15057         genlmsg_end(msg, hdr);
15058
15059         rcu_read_lock();
15060         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15061                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15062         rcu_read_unlock();
15063
15064         return;
15065
15066 nla_put_failure:
15067         nlmsg_free(msg);
15068 }
15069
15070 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
15071                                     struct net_device *netdev,
15072                                     const u8 *buf, size_t len,
15073                                     enum nl80211_commands cmd, gfp_t gfp,
15074                                     int uapsd_queues, const u8 *req_ies,
15075                                     size_t req_ies_len)
15076 {
15077         struct sk_buff *msg;
15078         void *hdr;
15079
15080         msg = nlmsg_new(100 + len + req_ies_len, gfp);
15081         if (!msg)
15082                 return;
15083
15084         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15085         if (!hdr) {
15086                 nlmsg_free(msg);
15087                 return;
15088         }
15089
15090         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15091             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15092             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15093             (req_ies &&
15094              nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
15095                 goto nla_put_failure;
15096
15097         if (uapsd_queues >= 0) {
15098                 struct nlattr *nla_wmm =
15099                         nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
15100                 if (!nla_wmm)
15101                         goto nla_put_failure;
15102
15103                 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
15104                                uapsd_queues))
15105                         goto nla_put_failure;
15106
15107                 nla_nest_end(msg, nla_wmm);
15108         }
15109
15110         genlmsg_end(msg, hdr);
15111
15112         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15113                                 NL80211_MCGRP_MLME, gfp);
15114         return;
15115
15116  nla_put_failure:
15117         nlmsg_free(msg);
15118 }
15119
15120 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
15121                           struct net_device *netdev, const u8 *buf,
15122                           size_t len, gfp_t gfp)
15123 {
15124         nl80211_send_mlme_event(rdev, netdev, buf, len,
15125                                 NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0);
15126 }
15127
15128 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
15129                            struct net_device *netdev, const u8 *buf,
15130                            size_t len, gfp_t gfp, int uapsd_queues,
15131                            const u8 *req_ies, size_t req_ies_len)
15132 {
15133         nl80211_send_mlme_event(rdev, netdev, buf, len,
15134                                 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues,
15135                                 req_ies, req_ies_len);
15136 }
15137
15138 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
15139                          struct net_device *netdev, const u8 *buf,
15140                          size_t len, gfp_t gfp)
15141 {
15142         nl80211_send_mlme_event(rdev, netdev, buf, len,
15143                                 NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0);
15144 }
15145
15146 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
15147                            struct net_device *netdev, const u8 *buf,
15148                            size_t len, gfp_t gfp)
15149 {
15150         nl80211_send_mlme_event(rdev, netdev, buf, len,
15151                                 NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0);
15152 }
15153
15154 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
15155                                   size_t len)
15156 {
15157         struct wireless_dev *wdev = dev->ieee80211_ptr;
15158         struct wiphy *wiphy = wdev->wiphy;
15159         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15160         const struct ieee80211_mgmt *mgmt = (void *)buf;
15161         u32 cmd;
15162
15163         if (WARN_ON(len < 2))
15164                 return;
15165
15166         if (ieee80211_is_deauth(mgmt->frame_control))
15167                 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
15168         else
15169                 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
15170
15171         trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
15172         nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
15173                                 NULL, 0);
15174 }
15175 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
15176
15177 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
15178                                       struct net_device *netdev, int cmd,
15179                                       const u8 *addr, gfp_t gfp)
15180 {
15181         struct sk_buff *msg;
15182         void *hdr;
15183
15184         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15185         if (!msg)
15186                 return;
15187
15188         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15189         if (!hdr) {
15190                 nlmsg_free(msg);
15191                 return;
15192         }
15193
15194         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15195             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15196             nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15197             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15198                 goto nla_put_failure;
15199
15200         genlmsg_end(msg, hdr);
15201
15202         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15203                                 NL80211_MCGRP_MLME, gfp);
15204         return;
15205
15206  nla_put_failure:
15207         nlmsg_free(msg);
15208 }
15209
15210 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
15211                                struct net_device *netdev, const u8 *addr,
15212                                gfp_t gfp)
15213 {
15214         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
15215                                   addr, gfp);
15216 }
15217
15218 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
15219                                 struct net_device *netdev, const u8 *addr,
15220                                 gfp_t gfp)
15221 {
15222         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
15223                                   addr, gfp);
15224 }
15225
15226 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
15227                                  struct net_device *netdev,
15228                                  struct cfg80211_connect_resp_params *cr,
15229                                  gfp_t gfp)
15230 {
15231         struct sk_buff *msg;
15232         void *hdr;
15233
15234         msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
15235                         cr->fils.kek_len + cr->fils.pmk_len +
15236                         (cr->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15237         if (!msg)
15238                 return;
15239
15240         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
15241         if (!hdr) {
15242                 nlmsg_free(msg);
15243                 return;
15244         }
15245
15246         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15247             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15248             (cr->bssid &&
15249              nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
15250             nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
15251                         cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
15252                         cr->status) ||
15253             (cr->status < 0 &&
15254              (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
15255               nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
15256                           cr->timeout_reason))) ||
15257             (cr->req_ie &&
15258              nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
15259             (cr->resp_ie &&
15260              nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
15261                      cr->resp_ie)) ||
15262             (cr->fils.update_erp_next_seq_num &&
15263              nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15264                          cr->fils.erp_next_seq_num)) ||
15265             (cr->status == WLAN_STATUS_SUCCESS &&
15266              ((cr->fils.kek &&
15267                nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
15268                        cr->fils.kek)) ||
15269               (cr->fils.pmk &&
15270                nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
15271               (cr->fils.pmkid &&
15272                nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
15273                 goto nla_put_failure;
15274
15275         genlmsg_end(msg, hdr);
15276
15277         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15278                                 NL80211_MCGRP_MLME, gfp);
15279         return;
15280
15281  nla_put_failure:
15282         nlmsg_free(msg);
15283 }
15284
15285 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
15286                          struct net_device *netdev,
15287                          struct cfg80211_roam_info *info, gfp_t gfp)
15288 {
15289         struct sk_buff *msg;
15290         void *hdr;
15291         const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
15292
15293         msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
15294                         info->fils.kek_len + info->fils.pmk_len +
15295                         (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
15296         if (!msg)
15297                 return;
15298
15299         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
15300         if (!hdr) {
15301                 nlmsg_free(msg);
15302                 return;
15303         }
15304
15305         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15306             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15307             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
15308             (info->req_ie &&
15309              nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
15310                      info->req_ie)) ||
15311             (info->resp_ie &&
15312              nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
15313                      info->resp_ie)) ||
15314             (info->fils.update_erp_next_seq_num &&
15315              nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
15316                          info->fils.erp_next_seq_num)) ||
15317             (info->fils.kek &&
15318              nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
15319                      info->fils.kek)) ||
15320             (info->fils.pmk &&
15321              nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
15322             (info->fils.pmkid &&
15323              nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
15324                 goto nla_put_failure;
15325
15326         genlmsg_end(msg, hdr);
15327
15328         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15329                                 NL80211_MCGRP_MLME, gfp);
15330         return;
15331
15332  nla_put_failure:
15333         nlmsg_free(msg);
15334 }
15335
15336 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
15337                                   struct net_device *netdev, const u8 *bssid)
15338 {
15339         struct sk_buff *msg;
15340         void *hdr;
15341
15342         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15343         if (!msg)
15344                 return;
15345
15346         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
15347         if (!hdr) {
15348                 nlmsg_free(msg);
15349                 return;
15350         }
15351
15352         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15353             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15354             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15355                 goto nla_put_failure;
15356
15357         genlmsg_end(msg, hdr);
15358
15359         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15360                                 NL80211_MCGRP_MLME, GFP_KERNEL);
15361         return;
15362
15363  nla_put_failure:
15364         nlmsg_free(msg);
15365 }
15366
15367 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
15368                                struct net_device *netdev, u16 reason,
15369                                const u8 *ie, size_t ie_len, bool from_ap)
15370 {
15371         struct sk_buff *msg;
15372         void *hdr;
15373
15374         msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
15375         if (!msg)
15376                 return;
15377
15378         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
15379         if (!hdr) {
15380                 nlmsg_free(msg);
15381                 return;
15382         }
15383
15384         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15385             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15386             (reason &&
15387              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
15388             (from_ap &&
15389              nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
15390             (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
15391                 goto nla_put_failure;
15392
15393         genlmsg_end(msg, hdr);
15394
15395         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15396                                 NL80211_MCGRP_MLME, GFP_KERNEL);
15397         return;
15398
15399  nla_put_failure:
15400         nlmsg_free(msg);
15401 }
15402
15403 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
15404                              struct net_device *netdev, const u8 *bssid,
15405                              gfp_t gfp)
15406 {
15407         struct sk_buff *msg;
15408         void *hdr;
15409
15410         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15411         if (!msg)
15412                 return;
15413
15414         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
15415         if (!hdr) {
15416                 nlmsg_free(msg);
15417                 return;
15418         }
15419
15420         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15421             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15422             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
15423                 goto nla_put_failure;
15424
15425         genlmsg_end(msg, hdr);
15426
15427         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15428                                 NL80211_MCGRP_MLME, gfp);
15429         return;
15430
15431  nla_put_failure:
15432         nlmsg_free(msg);
15433 }
15434
15435 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
15436                                         const u8 *ie, u8 ie_len,
15437                                         int sig_dbm, gfp_t gfp)
15438 {
15439         struct wireless_dev *wdev = dev->ieee80211_ptr;
15440         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15441         struct sk_buff *msg;
15442         void *hdr;
15443
15444         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
15445                 return;
15446
15447         trace_cfg80211_notify_new_peer_candidate(dev, addr);
15448
15449         msg = nlmsg_new(100 + ie_len, gfp);
15450         if (!msg)
15451                 return;
15452
15453         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
15454         if (!hdr) {
15455                 nlmsg_free(msg);
15456                 return;
15457         }
15458
15459         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15460             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15461             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15462             (ie_len && ie &&
15463              nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
15464             (sig_dbm &&
15465              nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
15466                 goto nla_put_failure;
15467
15468         genlmsg_end(msg, hdr);
15469
15470         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15471                                 NL80211_MCGRP_MLME, gfp);
15472         return;
15473
15474  nla_put_failure:
15475         nlmsg_free(msg);
15476 }
15477 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
15478
15479 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
15480                                  struct net_device *netdev, const u8 *addr,
15481                                  enum nl80211_key_type key_type, int key_id,
15482                                  const u8 *tsc, gfp_t gfp)
15483 {
15484         struct sk_buff *msg;
15485         void *hdr;
15486
15487         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15488         if (!msg)
15489                 return;
15490
15491         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
15492         if (!hdr) {
15493                 nlmsg_free(msg);
15494                 return;
15495         }
15496
15497         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15498             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15499             (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
15500             nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
15501             (key_id != -1 &&
15502              nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
15503             (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
15504                 goto nla_put_failure;
15505
15506         genlmsg_end(msg, hdr);
15507
15508         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15509                                 NL80211_MCGRP_MLME, gfp);
15510         return;
15511
15512  nla_put_failure:
15513         nlmsg_free(msg);
15514 }
15515
15516 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
15517                                     struct ieee80211_channel *channel_before,
15518                                     struct ieee80211_channel *channel_after)
15519 {
15520         struct sk_buff *msg;
15521         void *hdr;
15522         struct nlattr *nl_freq;
15523
15524         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
15525         if (!msg)
15526                 return;
15527
15528         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
15529         if (!hdr) {
15530                 nlmsg_free(msg);
15531                 return;
15532         }
15533
15534         /*
15535          * Since we are applying the beacon hint to a wiphy we know its
15536          * wiphy_idx is valid
15537          */
15538         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
15539                 goto nla_put_failure;
15540
15541         /* Before */
15542         nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
15543         if (!nl_freq)
15544                 goto nla_put_failure;
15545
15546         if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
15547                 goto nla_put_failure;
15548         nla_nest_end(msg, nl_freq);
15549
15550         /* After */
15551         nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
15552         if (!nl_freq)
15553                 goto nla_put_failure;
15554
15555         if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
15556                 goto nla_put_failure;
15557         nla_nest_end(msg, nl_freq);
15558
15559         genlmsg_end(msg, hdr);
15560
15561         rcu_read_lock();
15562         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
15563                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
15564         rcu_read_unlock();
15565
15566         return;
15567
15568 nla_put_failure:
15569         nlmsg_free(msg);
15570 }
15571
15572 static void nl80211_send_remain_on_chan_event(
15573         int cmd, struct cfg80211_registered_device *rdev,
15574         struct wireless_dev *wdev, u64 cookie,
15575         struct ieee80211_channel *chan,
15576         unsigned int duration, gfp_t gfp)
15577 {
15578         struct sk_buff *msg;
15579         void *hdr;
15580
15581         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15582         if (!msg)
15583                 return;
15584
15585         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15586         if (!hdr) {
15587                 nlmsg_free(msg);
15588                 return;
15589         }
15590
15591         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15592             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15593                                          wdev->netdev->ifindex)) ||
15594             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15595                               NL80211_ATTR_PAD) ||
15596             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
15597             nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
15598                         NL80211_CHAN_NO_HT) ||
15599             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15600                               NL80211_ATTR_PAD))
15601                 goto nla_put_failure;
15602
15603         if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
15604             nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
15605                 goto nla_put_failure;
15606
15607         genlmsg_end(msg, hdr);
15608
15609         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15610                                 NL80211_MCGRP_MLME, gfp);
15611         return;
15612
15613  nla_put_failure:
15614         nlmsg_free(msg);
15615 }
15616
15617 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
15618                                struct ieee80211_channel *chan,
15619                                unsigned int duration, gfp_t gfp)
15620 {
15621         struct wiphy *wiphy = wdev->wiphy;
15622         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15623
15624         trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
15625         nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
15626                                           rdev, wdev, cookie, chan,
15627                                           duration, gfp);
15628 }
15629 EXPORT_SYMBOL(cfg80211_ready_on_channel);
15630
15631 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
15632                                         struct ieee80211_channel *chan,
15633                                         gfp_t gfp)
15634 {
15635         struct wiphy *wiphy = wdev->wiphy;
15636         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15637
15638         trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
15639         nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
15640                                           rdev, wdev, cookie, chan, 0, gfp);
15641 }
15642 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
15643
15644 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
15645                                         struct ieee80211_channel *chan,
15646                                         gfp_t gfp)
15647 {
15648         struct wiphy *wiphy = wdev->wiphy;
15649         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15650
15651         trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
15652         nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
15653                                           rdev, wdev, cookie, chan, 0, gfp);
15654 }
15655 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
15656
15657 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
15658                       struct station_info *sinfo, gfp_t gfp)
15659 {
15660         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15661         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15662         struct sk_buff *msg;
15663
15664         trace_cfg80211_new_sta(dev, mac_addr, sinfo);
15665
15666         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15667         if (!msg)
15668                 return;
15669
15670         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
15671                                  rdev, dev, mac_addr, sinfo) < 0) {
15672                 nlmsg_free(msg);
15673                 return;
15674         }
15675
15676         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15677                                 NL80211_MCGRP_MLME, gfp);
15678 }
15679 EXPORT_SYMBOL(cfg80211_new_sta);
15680
15681 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
15682                             struct station_info *sinfo, gfp_t gfp)
15683 {
15684         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15685         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15686         struct sk_buff *msg;
15687         struct station_info empty_sinfo = {};
15688
15689         if (!sinfo)
15690                 sinfo = &empty_sinfo;
15691
15692         trace_cfg80211_del_sta(dev, mac_addr);
15693
15694         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15695         if (!msg) {
15696                 cfg80211_sinfo_release_content(sinfo);
15697                 return;
15698         }
15699
15700         if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
15701                                  rdev, dev, mac_addr, sinfo) < 0) {
15702                 nlmsg_free(msg);
15703                 return;
15704         }
15705
15706         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15707                                 NL80211_MCGRP_MLME, gfp);
15708 }
15709 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
15710
15711 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
15712                           enum nl80211_connect_failed_reason reason,
15713                           gfp_t gfp)
15714 {
15715         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
15716         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15717         struct sk_buff *msg;
15718         void *hdr;
15719
15720         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
15721         if (!msg)
15722                 return;
15723
15724         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
15725         if (!hdr) {
15726                 nlmsg_free(msg);
15727                 return;
15728         }
15729
15730         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15731             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
15732             nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
15733                 goto nla_put_failure;
15734
15735         genlmsg_end(msg, hdr);
15736
15737         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15738                                 NL80211_MCGRP_MLME, gfp);
15739         return;
15740
15741  nla_put_failure:
15742         nlmsg_free(msg);
15743 }
15744 EXPORT_SYMBOL(cfg80211_conn_failed);
15745
15746 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
15747                                        const u8 *addr, gfp_t gfp)
15748 {
15749         struct wireless_dev *wdev = dev->ieee80211_ptr;
15750         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15751         struct sk_buff *msg;
15752         void *hdr;
15753         u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
15754
15755         if (!nlportid)
15756                 return false;
15757
15758         msg = nlmsg_new(100, gfp);
15759         if (!msg)
15760                 return true;
15761
15762         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
15763         if (!hdr) {
15764                 nlmsg_free(msg);
15765                 return true;
15766         }
15767
15768         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15769             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15770             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
15771                 goto nla_put_failure;
15772
15773         genlmsg_end(msg, hdr);
15774         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15775         return true;
15776
15777  nla_put_failure:
15778         nlmsg_free(msg);
15779         return true;
15780 }
15781
15782 bool cfg80211_rx_spurious_frame(struct net_device *dev,
15783                                 const u8 *addr, gfp_t gfp)
15784 {
15785         struct wireless_dev *wdev = dev->ieee80211_ptr;
15786         bool ret;
15787
15788         trace_cfg80211_rx_spurious_frame(dev, addr);
15789
15790         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15791                     wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
15792                 trace_cfg80211_return_bool(false);
15793                 return false;
15794         }
15795         ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
15796                                          addr, gfp);
15797         trace_cfg80211_return_bool(ret);
15798         return ret;
15799 }
15800 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
15801
15802 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
15803                                         const u8 *addr, gfp_t gfp)
15804 {
15805         struct wireless_dev *wdev = dev->ieee80211_ptr;
15806         bool ret;
15807
15808         trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
15809
15810         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
15811                     wdev->iftype != NL80211_IFTYPE_P2P_GO &&
15812                     wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
15813                 trace_cfg80211_return_bool(false);
15814                 return false;
15815         }
15816         ret = __nl80211_unexpected_frame(dev,
15817                                          NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
15818                                          addr, gfp);
15819         trace_cfg80211_return_bool(ret);
15820         return ret;
15821 }
15822 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
15823
15824 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
15825                       struct wireless_dev *wdev, u32 nlportid,
15826                       int freq, int sig_dbm,
15827                       const u8 *buf, size_t len, u32 flags, gfp_t gfp)
15828 {
15829         struct net_device *netdev = wdev->netdev;
15830         struct sk_buff *msg;
15831         void *hdr;
15832
15833         msg = nlmsg_new(100 + len, gfp);
15834         if (!msg)
15835                 return -ENOMEM;
15836
15837         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
15838         if (!hdr) {
15839                 nlmsg_free(msg);
15840                 return -ENOMEM;
15841         }
15842
15843         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15844             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15845                                         netdev->ifindex)) ||
15846             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15847                               NL80211_ATTR_PAD) ||
15848             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
15849             (sig_dbm &&
15850              nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
15851             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15852             (flags &&
15853              nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
15854                 goto nla_put_failure;
15855
15856         genlmsg_end(msg, hdr);
15857
15858         return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15859
15860  nla_put_failure:
15861         nlmsg_free(msg);
15862         return -ENOBUFS;
15863 }
15864
15865 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
15866                              const u8 *buf, size_t len, bool ack, gfp_t gfp)
15867 {
15868         struct wiphy *wiphy = wdev->wiphy;
15869         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15870         struct net_device *netdev = wdev->netdev;
15871         struct sk_buff *msg;
15872         void *hdr;
15873
15874         trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
15875
15876         msg = nlmsg_new(100 + len, gfp);
15877         if (!msg)
15878                 return;
15879
15880         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
15881         if (!hdr) {
15882                 nlmsg_free(msg);
15883                 return;
15884         }
15885
15886         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15887             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15888                                    netdev->ifindex)) ||
15889             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15890                               NL80211_ATTR_PAD) ||
15891             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
15892             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
15893                               NL80211_ATTR_PAD) ||
15894             (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
15895                 goto nla_put_failure;
15896
15897         genlmsg_end(msg, hdr);
15898
15899         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15900                                 NL80211_MCGRP_MLME, gfp);
15901         return;
15902
15903  nla_put_failure:
15904         nlmsg_free(msg);
15905 }
15906 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
15907
15908 static int __nl80211_rx_control_port(struct net_device *dev,
15909                                      struct sk_buff *skb,
15910                                      bool unencrypted, gfp_t gfp)
15911 {
15912         struct wireless_dev *wdev = dev->ieee80211_ptr;
15913         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15914         struct ethhdr *ehdr = eth_hdr(skb);
15915         const u8 *addr = ehdr->h_source;
15916         u16 proto = be16_to_cpu(skb->protocol);
15917         struct sk_buff *msg;
15918         void *hdr;
15919         struct nlattr *frame;
15920
15921         u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
15922
15923         if (!nlportid)
15924                 return -ENOENT;
15925
15926         msg = nlmsg_new(100 + skb->len, gfp);
15927         if (!msg)
15928                 return -ENOMEM;
15929
15930         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
15931         if (!hdr) {
15932                 nlmsg_free(msg);
15933                 return -ENOBUFS;
15934         }
15935
15936         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15937             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15938             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15939                               NL80211_ATTR_PAD) ||
15940             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
15941             nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
15942             (unencrypted && nla_put_flag(msg,
15943                                          NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
15944                 goto nla_put_failure;
15945
15946         frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
15947         if (!frame)
15948                 goto nla_put_failure;
15949
15950         skb_copy_bits(skb, 0, nla_data(frame), skb->len);
15951         genlmsg_end(msg, hdr);
15952
15953         return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15954
15955  nla_put_failure:
15956         nlmsg_free(msg);
15957         return -ENOBUFS;
15958 }
15959
15960 bool cfg80211_rx_control_port(struct net_device *dev,
15961                               struct sk_buff *skb, bool unencrypted)
15962 {
15963         int ret;
15964
15965         trace_cfg80211_rx_control_port(dev, skb, unencrypted);
15966         ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
15967         trace_cfg80211_return_bool(ret == 0);
15968         return ret == 0;
15969 }
15970 EXPORT_SYMBOL(cfg80211_rx_control_port);
15971
15972 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
15973                                             const char *mac, gfp_t gfp)
15974 {
15975         struct wireless_dev *wdev = dev->ieee80211_ptr;
15976         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15977         struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15978         void **cb;
15979
15980         if (!msg)
15981                 return NULL;
15982
15983         cb = (void **)msg->cb;
15984
15985         cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
15986         if (!cb[0]) {
15987                 nlmsg_free(msg);
15988                 return NULL;
15989         }
15990
15991         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15992             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15993                 goto nla_put_failure;
15994
15995         if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
15996                 goto nla_put_failure;
15997
15998         cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
15999         if (!cb[1])
16000                 goto nla_put_failure;
16001
16002         cb[2] = rdev;
16003
16004         return msg;
16005  nla_put_failure:
16006         nlmsg_free(msg);
16007         return NULL;
16008 }
16009
16010 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
16011 {
16012         void **cb = (void **)msg->cb;
16013         struct cfg80211_registered_device *rdev = cb[2];
16014
16015         nla_nest_end(msg, cb[1]);
16016         genlmsg_end(msg, cb[0]);
16017
16018         memset(msg->cb, 0, sizeof(msg->cb));
16019
16020         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16021                                 NL80211_MCGRP_MLME, gfp);
16022 }
16023
16024 void cfg80211_cqm_rssi_notify(struct net_device *dev,
16025                               enum nl80211_cqm_rssi_threshold_event rssi_event,
16026                               s32 rssi_level, gfp_t gfp)
16027 {
16028         struct sk_buff *msg;
16029         struct wireless_dev *wdev = dev->ieee80211_ptr;
16030         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16031
16032         trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
16033
16034         if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
16035                     rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
16036                 return;
16037
16038         if (wdev->cqm_config) {
16039                 wdev->cqm_config->last_rssi_event_value = rssi_level;
16040
16041                 cfg80211_cqm_rssi_update(rdev, dev);
16042
16043                 if (rssi_level == 0)
16044                         rssi_level = wdev->cqm_config->last_rssi_event_value;
16045         }
16046
16047         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16048         if (!msg)
16049                 return;
16050
16051         if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
16052                         rssi_event))
16053                 goto nla_put_failure;
16054
16055         if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
16056                                       rssi_level))
16057                 goto nla_put_failure;
16058
16059         cfg80211_send_cqm(msg, gfp);
16060
16061         return;
16062
16063  nla_put_failure:
16064         nlmsg_free(msg);
16065 }
16066 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
16067
16068 void cfg80211_cqm_txe_notify(struct net_device *dev,
16069                              const u8 *peer, u32 num_packets,
16070                              u32 rate, u32 intvl, gfp_t gfp)
16071 {
16072         struct sk_buff *msg;
16073
16074         msg = cfg80211_prepare_cqm(dev, peer, gfp);
16075         if (!msg)
16076                 return;
16077
16078         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
16079                 goto nla_put_failure;
16080
16081         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
16082                 goto nla_put_failure;
16083
16084         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
16085                 goto nla_put_failure;
16086
16087         cfg80211_send_cqm(msg, gfp);
16088         return;
16089
16090  nla_put_failure:
16091         nlmsg_free(msg);
16092 }
16093 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
16094
16095 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
16096                                  const u8 *peer, u32 num_packets, gfp_t gfp)
16097 {
16098         struct sk_buff *msg;
16099
16100         trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
16101
16102         msg = cfg80211_prepare_cqm(dev, peer, gfp);
16103         if (!msg)
16104                 return;
16105
16106         if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
16107                 goto nla_put_failure;
16108
16109         cfg80211_send_cqm(msg, gfp);
16110         return;
16111
16112  nla_put_failure:
16113         nlmsg_free(msg);
16114 }
16115 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
16116
16117 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
16118 {
16119         struct sk_buff *msg;
16120
16121         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
16122         if (!msg)
16123                 return;
16124
16125         if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
16126                 goto nla_put_failure;
16127
16128         cfg80211_send_cqm(msg, gfp);
16129         return;
16130
16131  nla_put_failure:
16132         nlmsg_free(msg);
16133 }
16134 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
16135
16136 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
16137                                      struct net_device *netdev, const u8 *bssid,
16138                                      const u8 *replay_ctr, gfp_t gfp)
16139 {
16140         struct sk_buff *msg;
16141         struct nlattr *rekey_attr;
16142         void *hdr;
16143
16144         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16145         if (!msg)
16146                 return;
16147
16148         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
16149         if (!hdr) {
16150                 nlmsg_free(msg);
16151                 return;
16152         }
16153
16154         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16155             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16156             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
16157                 goto nla_put_failure;
16158
16159         rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
16160         if (!rekey_attr)
16161                 goto nla_put_failure;
16162
16163         if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
16164                     NL80211_REPLAY_CTR_LEN, replay_ctr))
16165                 goto nla_put_failure;
16166
16167         nla_nest_end(msg, rekey_attr);
16168
16169         genlmsg_end(msg, hdr);
16170
16171         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16172                                 NL80211_MCGRP_MLME, gfp);
16173         return;
16174
16175  nla_put_failure:
16176         nlmsg_free(msg);
16177 }
16178
16179 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
16180                                const u8 *replay_ctr, gfp_t gfp)
16181 {
16182         struct wireless_dev *wdev = dev->ieee80211_ptr;
16183         struct wiphy *wiphy = wdev->wiphy;
16184         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16185
16186         trace_cfg80211_gtk_rekey_notify(dev, bssid);
16187         nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
16188 }
16189 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
16190
16191 static void
16192 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
16193                                struct net_device *netdev, int index,
16194                                const u8 *bssid, bool preauth, gfp_t gfp)
16195 {
16196         struct sk_buff *msg;
16197         struct nlattr *attr;
16198         void *hdr;
16199
16200         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16201         if (!msg)
16202                 return;
16203
16204         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
16205         if (!hdr) {
16206                 nlmsg_free(msg);
16207                 return;
16208         }
16209
16210         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16211             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16212                 goto nla_put_failure;
16213
16214         attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
16215         if (!attr)
16216                 goto nla_put_failure;
16217
16218         if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
16219             nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
16220             (preauth &&
16221              nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
16222                 goto nla_put_failure;
16223
16224         nla_nest_end(msg, attr);
16225
16226         genlmsg_end(msg, hdr);
16227
16228         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16229                                 NL80211_MCGRP_MLME, gfp);
16230         return;
16231
16232  nla_put_failure:
16233         nlmsg_free(msg);
16234 }
16235
16236 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
16237                                      const u8 *bssid, bool preauth, gfp_t gfp)
16238 {
16239         struct wireless_dev *wdev = dev->ieee80211_ptr;
16240         struct wiphy *wiphy = wdev->wiphy;
16241         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16242
16243         trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
16244         nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
16245 }
16246 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
16247
16248 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
16249                                      struct net_device *netdev,
16250                                      struct cfg80211_chan_def *chandef,
16251                                      gfp_t gfp,
16252                                      enum nl80211_commands notif,
16253                                      u8 count)
16254 {
16255         struct sk_buff *msg;
16256         void *hdr;
16257
16258         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16259         if (!msg)
16260                 return;
16261
16262         hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
16263         if (!hdr) {
16264                 nlmsg_free(msg);
16265                 return;
16266         }
16267
16268         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
16269                 goto nla_put_failure;
16270
16271         if (nl80211_send_chandef(msg, chandef))
16272                 goto nla_put_failure;
16273
16274         if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
16275             (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
16276                         goto nla_put_failure;
16277
16278         genlmsg_end(msg, hdr);
16279
16280         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16281                                 NL80211_MCGRP_MLME, gfp);
16282         return;
16283
16284  nla_put_failure:
16285         nlmsg_free(msg);
16286 }
16287
16288 void cfg80211_ch_switch_notify(struct net_device *dev,
16289                                struct cfg80211_chan_def *chandef)
16290 {
16291         struct wireless_dev *wdev = dev->ieee80211_ptr;
16292         struct wiphy *wiphy = wdev->wiphy;
16293         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16294
16295         ASSERT_WDEV_LOCK(wdev);
16296
16297         trace_cfg80211_ch_switch_notify(dev, chandef);
16298
16299         wdev->chandef = *chandef;
16300         wdev->preset_chandef = *chandef;
16301
16302         if (wdev->iftype == NL80211_IFTYPE_STATION &&
16303             !WARN_ON(!wdev->current_bss))
16304                 cfg80211_update_assoc_bss_entry(wdev, chandef->chan);
16305
16306         cfg80211_sched_dfs_chan_update(rdev);
16307
16308         nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16309                                  NL80211_CMD_CH_SWITCH_NOTIFY, 0);
16310 }
16311 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
16312
16313 void cfg80211_ch_switch_started_notify(struct net_device *dev,
16314                                        struct cfg80211_chan_def *chandef,
16315                                        u8 count)
16316 {
16317         struct wireless_dev *wdev = dev->ieee80211_ptr;
16318         struct wiphy *wiphy = wdev->wiphy;
16319         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16320
16321         trace_cfg80211_ch_switch_started_notify(dev, chandef);
16322
16323         nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
16324                                  NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
16325 }
16326 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
16327
16328 void
16329 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
16330                      const struct cfg80211_chan_def *chandef,
16331                      enum nl80211_radar_event event,
16332                      struct net_device *netdev, gfp_t gfp)
16333 {
16334         struct sk_buff *msg;
16335         void *hdr;
16336
16337         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16338         if (!msg)
16339                 return;
16340
16341         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
16342         if (!hdr) {
16343                 nlmsg_free(msg);
16344                 return;
16345         }
16346
16347         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16348                 goto nla_put_failure;
16349
16350         /* NOP and radar events don't need a netdev parameter */
16351         if (netdev) {
16352                 struct wireless_dev *wdev = netdev->ieee80211_ptr;
16353
16354                 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16355                     nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16356                                       NL80211_ATTR_PAD))
16357                         goto nla_put_failure;
16358         }
16359
16360         if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
16361                 goto nla_put_failure;
16362
16363         if (nl80211_send_chandef(msg, chandef))
16364                 goto nla_put_failure;
16365
16366         genlmsg_end(msg, hdr);
16367
16368         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16369                                 NL80211_MCGRP_MLME, gfp);
16370         return;
16371
16372  nla_put_failure:
16373         nlmsg_free(msg);
16374 }
16375
16376 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
16377                                        struct sta_opmode_info *sta_opmode,
16378                                        gfp_t gfp)
16379 {
16380         struct sk_buff *msg;
16381         struct wireless_dev *wdev = dev->ieee80211_ptr;
16382         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16383         void *hdr;
16384
16385         if (WARN_ON(!mac))
16386                 return;
16387
16388         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16389         if (!msg)
16390                 return;
16391
16392         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
16393         if (!hdr) {
16394                 nlmsg_free(msg);
16395                 return;
16396         }
16397
16398         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
16399                 goto nla_put_failure;
16400
16401         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
16402                 goto nla_put_failure;
16403
16404         if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
16405                 goto nla_put_failure;
16406
16407         if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
16408             nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
16409                 goto nla_put_failure;
16410
16411         if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
16412             nla_put_u8(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
16413                 goto nla_put_failure;
16414
16415         if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
16416             nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
16417                 goto nla_put_failure;
16418
16419         genlmsg_end(msg, hdr);
16420
16421         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16422                                 NL80211_MCGRP_MLME, gfp);
16423
16424         return;
16425
16426 nla_put_failure:
16427         nlmsg_free(msg);
16428 }
16429 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
16430
16431 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
16432                            u64 cookie, bool acked, s32 ack_signal,
16433                            bool is_valid_ack_signal, gfp_t gfp)
16434 {
16435         struct wireless_dev *wdev = dev->ieee80211_ptr;
16436         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16437         struct sk_buff *msg;
16438         void *hdr;
16439
16440         trace_cfg80211_probe_status(dev, addr, cookie, acked);
16441
16442         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16443
16444         if (!msg)
16445                 return;
16446
16447         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
16448         if (!hdr) {
16449                 nlmsg_free(msg);
16450                 return;
16451         }
16452
16453         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16454             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16455             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
16456             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
16457                               NL80211_ATTR_PAD) ||
16458             (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
16459             (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
16460                                                 ack_signal)))
16461                 goto nla_put_failure;
16462
16463         genlmsg_end(msg, hdr);
16464
16465         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16466                                 NL80211_MCGRP_MLME, gfp);
16467         return;
16468
16469  nla_put_failure:
16470         nlmsg_free(msg);
16471 }
16472 EXPORT_SYMBOL(cfg80211_probe_status);
16473
16474 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
16475                                  const u8 *frame, size_t len,
16476                                  int freq, int sig_dbm)
16477 {
16478         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16479         struct sk_buff *msg;
16480         void *hdr;
16481         struct cfg80211_beacon_registration *reg;
16482
16483         trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
16484
16485         spin_lock_bh(&rdev->beacon_registrations_lock);
16486         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
16487                 msg = nlmsg_new(len + 100, GFP_ATOMIC);
16488                 if (!msg) {
16489                         spin_unlock_bh(&rdev->beacon_registrations_lock);
16490                         return;
16491                 }
16492
16493                 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
16494                 if (!hdr)
16495                         goto nla_put_failure;
16496
16497                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16498                     (freq &&
16499                      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
16500                     (sig_dbm &&
16501                      nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
16502                     nla_put(msg, NL80211_ATTR_FRAME, len, frame))
16503                         goto nla_put_failure;
16504
16505                 genlmsg_end(msg, hdr);
16506
16507                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
16508         }
16509         spin_unlock_bh(&rdev->beacon_registrations_lock);
16510         return;
16511
16512  nla_put_failure:
16513         spin_unlock_bh(&rdev->beacon_registrations_lock);
16514         nlmsg_free(msg);
16515 }
16516 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
16517
16518 #ifdef CONFIG_PM
16519 static int cfg80211_net_detect_results(struct sk_buff *msg,
16520                                        struct cfg80211_wowlan_wakeup *wakeup)
16521 {
16522         struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
16523         struct nlattr *nl_results, *nl_match, *nl_freqs;
16524         int i, j;
16525
16526         nl_results = nla_nest_start_noflag(msg,
16527                                            NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
16528         if (!nl_results)
16529                 return -EMSGSIZE;
16530
16531         for (i = 0; i < nd->n_matches; i++) {
16532                 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
16533
16534                 nl_match = nla_nest_start_noflag(msg, i);
16535                 if (!nl_match)
16536                         break;
16537
16538                 /* The SSID attribute is optional in nl80211, but for
16539                  * simplicity reasons it's always present in the
16540                  * cfg80211 structure.  If a driver can't pass the
16541                  * SSID, that needs to be changed.  A zero length SSID
16542                  * is still a valid SSID (wildcard), so it cannot be
16543                  * used for this purpose.
16544                  */
16545                 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
16546                             match->ssid.ssid)) {
16547                         nla_nest_cancel(msg, nl_match);
16548                         goto out;
16549                 }
16550
16551                 if (match->n_channels) {
16552                         nl_freqs = nla_nest_start_noflag(msg,
16553                                                          NL80211_ATTR_SCAN_FREQUENCIES);
16554                         if (!nl_freqs) {
16555                                 nla_nest_cancel(msg, nl_match);
16556                                 goto out;
16557                         }
16558
16559                         for (j = 0; j < match->n_channels; j++) {
16560                                 if (nla_put_u32(msg, j, match->channels[j])) {
16561                                         nla_nest_cancel(msg, nl_freqs);
16562                                         nla_nest_cancel(msg, nl_match);
16563                                         goto out;
16564                                 }
16565                         }
16566
16567                         nla_nest_end(msg, nl_freqs);
16568                 }
16569
16570                 nla_nest_end(msg, nl_match);
16571         }
16572
16573 out:
16574         nla_nest_end(msg, nl_results);
16575         return 0;
16576 }
16577
16578 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
16579                                    struct cfg80211_wowlan_wakeup *wakeup,
16580                                    gfp_t gfp)
16581 {
16582         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16583         struct sk_buff *msg;
16584         void *hdr;
16585         int size = 200;
16586
16587         trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
16588
16589         if (wakeup)
16590                 size += wakeup->packet_present_len;
16591
16592         msg = nlmsg_new(size, gfp);
16593         if (!msg)
16594                 return;
16595
16596         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
16597         if (!hdr)
16598                 goto free_msg;
16599
16600         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16601             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16602                               NL80211_ATTR_PAD))
16603                 goto free_msg;
16604
16605         if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
16606                                         wdev->netdev->ifindex))
16607                 goto free_msg;
16608
16609         if (wakeup) {
16610                 struct nlattr *reasons;
16611
16612                 reasons = nla_nest_start_noflag(msg,
16613                                                 NL80211_ATTR_WOWLAN_TRIGGERS);
16614                 if (!reasons)
16615                         goto free_msg;
16616
16617                 if (wakeup->disconnect &&
16618                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
16619                         goto free_msg;
16620                 if (wakeup->magic_pkt &&
16621                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
16622                         goto free_msg;
16623                 if (wakeup->gtk_rekey_failure &&
16624                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
16625                         goto free_msg;
16626                 if (wakeup->eap_identity_req &&
16627                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
16628                         goto free_msg;
16629                 if (wakeup->four_way_handshake &&
16630                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
16631                         goto free_msg;
16632                 if (wakeup->rfkill_release &&
16633                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
16634                         goto free_msg;
16635
16636                 if (wakeup->pattern_idx >= 0 &&
16637                     nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
16638                                 wakeup->pattern_idx))
16639                         goto free_msg;
16640
16641                 if (wakeup->tcp_match &&
16642                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
16643                         goto free_msg;
16644
16645                 if (wakeup->tcp_connlost &&
16646                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
16647                         goto free_msg;
16648
16649                 if (wakeup->tcp_nomoretokens &&
16650                     nla_put_flag(msg,
16651                                  NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
16652                         goto free_msg;
16653
16654                 if (wakeup->packet) {
16655                         u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
16656                         u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
16657
16658                         if (!wakeup->packet_80211) {
16659                                 pkt_attr =
16660                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
16661                                 len_attr =
16662                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
16663                         }
16664
16665                         if (wakeup->packet_len &&
16666                             nla_put_u32(msg, len_attr, wakeup->packet_len))
16667                                 goto free_msg;
16668
16669                         if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
16670                                     wakeup->packet))
16671                                 goto free_msg;
16672                 }
16673
16674                 if (wakeup->net_detect &&
16675                     cfg80211_net_detect_results(msg, wakeup))
16676                                 goto free_msg;
16677
16678                 nla_nest_end(msg, reasons);
16679         }
16680
16681         genlmsg_end(msg, hdr);
16682
16683         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16684                                 NL80211_MCGRP_MLME, gfp);
16685         return;
16686
16687  free_msg:
16688         nlmsg_free(msg);
16689 }
16690 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
16691 #endif
16692
16693 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
16694                                 enum nl80211_tdls_operation oper,
16695                                 u16 reason_code, gfp_t gfp)
16696 {
16697         struct wireless_dev *wdev = dev->ieee80211_ptr;
16698         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16699         struct sk_buff *msg;
16700         void *hdr;
16701
16702         trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
16703                                          reason_code);
16704
16705         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16706         if (!msg)
16707                 return;
16708
16709         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
16710         if (!hdr) {
16711                 nlmsg_free(msg);
16712                 return;
16713         }
16714
16715         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16716             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16717             nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
16718             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
16719             (reason_code > 0 &&
16720              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
16721                 goto nla_put_failure;
16722
16723         genlmsg_end(msg, hdr);
16724
16725         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16726                                 NL80211_MCGRP_MLME, gfp);
16727         return;
16728
16729  nla_put_failure:
16730         nlmsg_free(msg);
16731 }
16732 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
16733
16734 static int nl80211_netlink_notify(struct notifier_block * nb,
16735                                   unsigned long state,
16736                                   void *_notify)
16737 {
16738         struct netlink_notify *notify = _notify;
16739         struct cfg80211_registered_device *rdev;
16740         struct wireless_dev *wdev;
16741         struct cfg80211_beacon_registration *reg, *tmp;
16742
16743         if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
16744                 return NOTIFY_DONE;
16745
16746         rcu_read_lock();
16747
16748         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
16749                 struct cfg80211_sched_scan_request *sched_scan_req;
16750
16751                 list_for_each_entry_rcu(sched_scan_req,
16752                                         &rdev->sched_scan_req_list,
16753                                         list) {
16754                         if (sched_scan_req->owner_nlportid == notify->portid) {
16755                                 sched_scan_req->nl_owner_dead = true;
16756                                 schedule_work(&rdev->sched_scan_stop_wk);
16757                         }
16758                 }
16759
16760                 list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
16761                         cfg80211_mlme_unregister_socket(wdev, notify->portid);
16762
16763                         if (wdev->owner_nlportid == notify->portid) {
16764                                 wdev->nl_owner_dead = true;
16765                                 schedule_work(&rdev->destroy_work);
16766                         } else if (wdev->conn_owner_nlportid == notify->portid) {
16767                                 schedule_work(&wdev->disconnect_wk);
16768                         }
16769
16770                         cfg80211_release_pmsr(wdev, notify->portid);
16771                 }
16772
16773                 spin_lock_bh(&rdev->beacon_registrations_lock);
16774                 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
16775                                          list) {
16776                         if (reg->nlportid == notify->portid) {
16777                                 list_del(&reg->list);
16778                                 kfree(reg);
16779                                 break;
16780                         }
16781                 }
16782                 spin_unlock_bh(&rdev->beacon_registrations_lock);
16783         }
16784
16785         rcu_read_unlock();
16786
16787         /*
16788          * It is possible that the user space process that is controlling the
16789          * indoor setting disappeared, so notify the regulatory core.
16790          */
16791         regulatory_netlink_notify(notify->portid);
16792         return NOTIFY_OK;
16793 }
16794
16795 static struct notifier_block nl80211_netlink_notifier = {
16796         .notifier_call = nl80211_netlink_notify,
16797 };
16798
16799 void cfg80211_ft_event(struct net_device *netdev,
16800                        struct cfg80211_ft_event_params *ft_event)
16801 {
16802         struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16803         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16804         struct sk_buff *msg;
16805         void *hdr;
16806
16807         trace_cfg80211_ft_event(wiphy, netdev, ft_event);
16808
16809         if (!ft_event->target_ap)
16810                 return;
16811
16812         msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
16813                         GFP_KERNEL);
16814         if (!msg)
16815                 return;
16816
16817         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
16818         if (!hdr)
16819                 goto out;
16820
16821         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16822             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16823             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
16824                 goto out;
16825
16826         if (ft_event->ies &&
16827             nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
16828                 goto out;
16829         if (ft_event->ric_ies &&
16830             nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
16831                     ft_event->ric_ies))
16832                 goto out;
16833
16834         genlmsg_end(msg, hdr);
16835
16836         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16837                                 NL80211_MCGRP_MLME, GFP_KERNEL);
16838         return;
16839  out:
16840         nlmsg_free(msg);
16841 }
16842 EXPORT_SYMBOL(cfg80211_ft_event);
16843
16844 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
16845 {
16846         struct cfg80211_registered_device *rdev;
16847         struct sk_buff *msg;
16848         void *hdr;
16849         u32 nlportid;
16850
16851         rdev = wiphy_to_rdev(wdev->wiphy);
16852         if (!rdev->crit_proto_nlportid)
16853                 return;
16854
16855         nlportid = rdev->crit_proto_nlportid;
16856         rdev->crit_proto_nlportid = 0;
16857
16858         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16859         if (!msg)
16860                 return;
16861
16862         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
16863         if (!hdr)
16864                 goto nla_put_failure;
16865
16866         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16867             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16868                               NL80211_ATTR_PAD))
16869                 goto nla_put_failure;
16870
16871         genlmsg_end(msg, hdr);
16872
16873         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
16874         return;
16875
16876  nla_put_failure:
16877         nlmsg_free(msg);
16878 }
16879 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
16880
16881 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
16882 {
16883         struct wiphy *wiphy = wdev->wiphy;
16884         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16885         struct sk_buff *msg;
16886         void *hdr;
16887
16888         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
16889         if (!msg)
16890                 return;
16891
16892         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
16893         if (!hdr)
16894                 goto out;
16895
16896         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16897             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
16898             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
16899                               NL80211_ATTR_PAD))
16900                 goto out;
16901
16902         genlmsg_end(msg, hdr);
16903
16904         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
16905                                 NL80211_MCGRP_MLME, GFP_KERNEL);
16906         return;
16907  out:
16908         nlmsg_free(msg);
16909 }
16910
16911 int cfg80211_external_auth_request(struct net_device *dev,
16912                                    struct cfg80211_external_auth_params *params,
16913                                    gfp_t gfp)
16914 {
16915         struct wireless_dev *wdev = dev->ieee80211_ptr;
16916         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
16917         struct sk_buff *msg;
16918         void *hdr;
16919
16920         if (!wdev->conn_owner_nlportid)
16921                 return -EINVAL;
16922
16923         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16924         if (!msg)
16925                 return -ENOMEM;
16926
16927         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
16928         if (!hdr)
16929                 goto nla_put_failure;
16930
16931         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16932             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
16933             nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
16934             nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
16935                         params->action) ||
16936             nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
16937             nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
16938                     params->ssid.ssid))
16939                 goto nla_put_failure;
16940
16941         genlmsg_end(msg, hdr);
16942         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
16943                         wdev->conn_owner_nlportid);
16944         return 0;
16945
16946  nla_put_failure:
16947         nlmsg_free(msg);
16948         return -ENOBUFS;
16949 }
16950 EXPORT_SYMBOL(cfg80211_external_auth_request);
16951
16952 void cfg80211_update_owe_info_event(struct net_device *netdev,
16953                                     struct cfg80211_update_owe_info *owe_info,
16954                                     gfp_t gfp)
16955 {
16956         struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
16957         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
16958         struct sk_buff *msg;
16959         void *hdr;
16960
16961         trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
16962
16963         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
16964         if (!msg)
16965                 return;
16966
16967         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
16968         if (!hdr)
16969                 goto nla_put_failure;
16970
16971         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
16972             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
16973             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
16974                 goto nla_put_failure;
16975
16976         if (!owe_info->ie_len ||
16977             nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
16978                 goto nla_put_failure;
16979
16980         genlmsg_end(msg, hdr);
16981
16982         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
16983                                 NL80211_MCGRP_MLME, gfp);
16984         return;
16985
16986 nla_put_failure:
16987         genlmsg_cancel(msg, hdr);
16988         nlmsg_free(msg);
16989 }
16990 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
16991
16992 /* initialisation/exit functions */
16993
16994 int __init nl80211_init(void)
16995 {
16996         int err;
16997
16998         err = genl_register_family(&nl80211_fam);
16999         if (err)
17000                 return err;
17001
17002         err = netlink_register_notifier(&nl80211_netlink_notifier);
17003         if (err)
17004                 goto err_out;
17005
17006         return 0;
17007  err_out:
17008         genl_unregister_family(&nl80211_fam);
17009         return err;
17010 }
17011
17012 void nl80211_exit(void)
17013 {
17014         netlink_unregister_notifier(&nl80211_netlink_notifier);
17015         genl_unregister_family(&nl80211_fam);
17016 }