RISCV: configs: Enable temperature sensor defconfig
[platform/kernel/linux-starfive.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-2022 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 <linux/if_vlan.h>
24 #include <net/net_namespace.h>
25 #include <net/genetlink.h>
26 #include <net/cfg80211.h>
27 #include <net/sock.h>
28 #include <net/inet_connection_sock.h>
29 #include "core.h"
30 #include "nl80211.h"
31 #include "reg.h"
32 #include "rdev-ops.h"
33
34 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
35                                    struct genl_info *info,
36                                    struct cfg80211_crypto_settings *settings,
37                                    int cipher_limit);
38
39 /* the netlink family */
40 static struct genl_family nl80211_fam;
41
42 /* multicast groups */
43 enum nl80211_multicast_groups {
44         NL80211_MCGRP_CONFIG,
45         NL80211_MCGRP_SCAN,
46         NL80211_MCGRP_REGULATORY,
47         NL80211_MCGRP_MLME,
48         NL80211_MCGRP_VENDOR,
49         NL80211_MCGRP_NAN,
50         NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
51 };
52
53 static const struct genl_multicast_group nl80211_mcgrps[] = {
54         [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
55         [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
56         [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
57         [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
58         [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
59         [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
60 #ifdef CONFIG_NL80211_TESTMODE
61         [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
62 #endif
63 };
64
65 /* returns ERR_PTR values */
66 static struct wireless_dev *
67 __cfg80211_wdev_from_attrs(struct cfg80211_registered_device *rdev,
68                            struct net *netns, struct nlattr **attrs)
69 {
70         struct wireless_dev *result = NULL;
71         bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
72         bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
73         u64 wdev_id = 0;
74         int wiphy_idx = -1;
75         int ifidx = -1;
76
77         if (!have_ifidx && !have_wdev_id)
78                 return ERR_PTR(-EINVAL);
79
80         if (have_ifidx)
81                 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
82         if (have_wdev_id) {
83                 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
84                 wiphy_idx = wdev_id >> 32;
85         }
86
87         if (rdev) {
88                 struct wireless_dev *wdev;
89
90                 lockdep_assert_held(&rdev->wiphy.mtx);
91
92                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
93                         if (have_ifidx && wdev->netdev &&
94                             wdev->netdev->ifindex == ifidx) {
95                                 result = wdev;
96                                 break;
97                         }
98                         if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
99                                 result = wdev;
100                                 break;
101                         }
102                 }
103
104                 return result ?: ERR_PTR(-ENODEV);
105         }
106
107         ASSERT_RTNL();
108
109         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
110                 struct wireless_dev *wdev;
111
112                 if (wiphy_net(&rdev->wiphy) != netns)
113                         continue;
114
115                 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
116                         continue;
117
118                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
119                         if (have_ifidx && wdev->netdev &&
120                             wdev->netdev->ifindex == ifidx) {
121                                 result = wdev;
122                                 break;
123                         }
124                         if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
125                                 result = wdev;
126                                 break;
127                         }
128                 }
129
130                 if (result)
131                         break;
132         }
133
134         if (result)
135                 return result;
136         return ERR_PTR(-ENODEV);
137 }
138
139 static struct cfg80211_registered_device *
140 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
141 {
142         struct cfg80211_registered_device *rdev = NULL, *tmp;
143         struct net_device *netdev;
144
145         ASSERT_RTNL();
146
147         if (!attrs[NL80211_ATTR_WIPHY] &&
148             !attrs[NL80211_ATTR_IFINDEX] &&
149             !attrs[NL80211_ATTR_WDEV])
150                 return ERR_PTR(-EINVAL);
151
152         if (attrs[NL80211_ATTR_WIPHY])
153                 rdev = cfg80211_rdev_by_wiphy_idx(
154                                 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
155
156         if (attrs[NL80211_ATTR_WDEV]) {
157                 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
158                 struct wireless_dev *wdev;
159                 bool found = false;
160
161                 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
162                 if (tmp) {
163                         /* make sure wdev exists */
164                         list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
165                                 if (wdev->identifier != (u32)wdev_id)
166                                         continue;
167                                 found = true;
168                                 break;
169                         }
170
171                         if (!found)
172                                 tmp = NULL;
173
174                         if (rdev && tmp != rdev)
175                                 return ERR_PTR(-EINVAL);
176                         rdev = tmp;
177                 }
178         }
179
180         if (attrs[NL80211_ATTR_IFINDEX]) {
181                 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
182
183                 netdev = __dev_get_by_index(netns, ifindex);
184                 if (netdev) {
185                         if (netdev->ieee80211_ptr)
186                                 tmp = wiphy_to_rdev(
187                                         netdev->ieee80211_ptr->wiphy);
188                         else
189                                 tmp = NULL;
190
191                         /* not wireless device -- return error */
192                         if (!tmp)
193                                 return ERR_PTR(-EINVAL);
194
195                         /* mismatch -- return error */
196                         if (rdev && tmp != rdev)
197                                 return ERR_PTR(-EINVAL);
198
199                         rdev = tmp;
200                 }
201         }
202
203         if (!rdev)
204                 return ERR_PTR(-ENODEV);
205
206         if (netns != wiphy_net(&rdev->wiphy))
207                 return ERR_PTR(-ENODEV);
208
209         return rdev;
210 }
211
212 /*
213  * This function returns a pointer to the driver
214  * that the genl_info item that is passed refers to.
215  *
216  * The result of this can be a PTR_ERR and hence must
217  * be checked with IS_ERR() for errors.
218  */
219 static struct cfg80211_registered_device *
220 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
221 {
222         return __cfg80211_rdev_from_attrs(netns, info->attrs);
223 }
224
225 static int validate_beacon_head(const struct nlattr *attr,
226                                 struct netlink_ext_ack *extack)
227 {
228         const u8 *data = nla_data(attr);
229         unsigned int len = nla_len(attr);
230         const struct element *elem;
231         const struct ieee80211_mgmt *mgmt = (void *)data;
232         unsigned int fixedlen, hdrlen;
233         bool s1g_bcn;
234
235         if (len < offsetofend(typeof(*mgmt), frame_control))
236                 goto err;
237
238         s1g_bcn = ieee80211_is_s1g_beacon(mgmt->frame_control);
239         if (s1g_bcn) {
240                 fixedlen = offsetof(struct ieee80211_ext,
241                                     u.s1g_beacon.variable);
242                 hdrlen = offsetof(struct ieee80211_ext, u.s1g_beacon);
243         } else {
244                 fixedlen = offsetof(struct ieee80211_mgmt,
245                                     u.beacon.variable);
246                 hdrlen = offsetof(struct ieee80211_mgmt, u.beacon);
247         }
248
249         if (len < fixedlen)
250                 goto err;
251
252         if (ieee80211_hdrlen(mgmt->frame_control) != hdrlen)
253                 goto err;
254
255         data += fixedlen;
256         len -= fixedlen;
257
258         for_each_element(elem, data, len) {
259                 /* nothing */
260         }
261
262         if (for_each_element_completed(elem, data, len))
263                 return 0;
264
265 err:
266         NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
267         return -EINVAL;
268 }
269
270 static int validate_ie_attr(const struct nlattr *attr,
271                             struct netlink_ext_ack *extack)
272 {
273         const u8 *data = nla_data(attr);
274         unsigned int len = nla_len(attr);
275         const struct element *elem;
276
277         for_each_element(elem, data, len) {
278                 /* nothing */
279         }
280
281         if (for_each_element_completed(elem, data, len))
282                 return 0;
283
284         NL_SET_ERR_MSG_ATTR(extack, attr, "malformed information elements");
285         return -EINVAL;
286 }
287
288 static int validate_he_capa(const struct nlattr *attr,
289                             struct netlink_ext_ack *extack)
290 {
291         if (!ieee80211_he_capa_size_ok(nla_data(attr), nla_len(attr)))
292                 return -EINVAL;
293
294         return 0;
295 }
296
297 /* policy for the attributes */
298 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR];
299
300 static const struct nla_policy
301 nl80211_ftm_responder_policy[NL80211_FTM_RESP_ATTR_MAX + 1] = {
302         [NL80211_FTM_RESP_ATTR_ENABLED] = { .type = NLA_FLAG, },
303         [NL80211_FTM_RESP_ATTR_LCI] = { .type = NLA_BINARY,
304                                         .len = U8_MAX },
305         [NL80211_FTM_RESP_ATTR_CIVICLOC] = { .type = NLA_BINARY,
306                                              .len = U8_MAX },
307 };
308
309 static const struct nla_policy
310 nl80211_pmsr_ftm_req_attr_policy[NL80211_PMSR_FTM_REQ_ATTR_MAX + 1] = {
311         [NL80211_PMSR_FTM_REQ_ATTR_ASAP] = { .type = NLA_FLAG },
312         [NL80211_PMSR_FTM_REQ_ATTR_PREAMBLE] = { .type = NLA_U32 },
313         [NL80211_PMSR_FTM_REQ_ATTR_NUM_BURSTS_EXP] =
314                 NLA_POLICY_MAX(NLA_U8, 15),
315         [NL80211_PMSR_FTM_REQ_ATTR_BURST_PERIOD] = { .type = NLA_U16 },
316         [NL80211_PMSR_FTM_REQ_ATTR_BURST_DURATION] =
317                 NLA_POLICY_MAX(NLA_U8, 15),
318         [NL80211_PMSR_FTM_REQ_ATTR_FTMS_PER_BURST] =
319                 NLA_POLICY_MAX(NLA_U8, 31),
320         [NL80211_PMSR_FTM_REQ_ATTR_NUM_FTMR_RETRIES] = { .type = NLA_U8 },
321         [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_LCI] = { .type = NLA_FLAG },
322         [NL80211_PMSR_FTM_REQ_ATTR_REQUEST_CIVICLOC] = { .type = NLA_FLAG },
323         [NL80211_PMSR_FTM_REQ_ATTR_TRIGGER_BASED] = { .type = NLA_FLAG },
324         [NL80211_PMSR_FTM_REQ_ATTR_NON_TRIGGER_BASED] = { .type = NLA_FLAG },
325         [NL80211_PMSR_FTM_REQ_ATTR_LMR_FEEDBACK] = { .type = NLA_FLAG },
326 };
327
328 static const struct nla_policy
329 nl80211_pmsr_req_data_policy[NL80211_PMSR_TYPE_MAX + 1] = {
330         [NL80211_PMSR_TYPE_FTM] =
331                 NLA_POLICY_NESTED(nl80211_pmsr_ftm_req_attr_policy),
332 };
333
334 static const struct nla_policy
335 nl80211_pmsr_req_attr_policy[NL80211_PMSR_REQ_ATTR_MAX + 1] = {
336         [NL80211_PMSR_REQ_ATTR_DATA] =
337                 NLA_POLICY_NESTED(nl80211_pmsr_req_data_policy),
338         [NL80211_PMSR_REQ_ATTR_GET_AP_TSF] = { .type = NLA_FLAG },
339 };
340
341 static const struct nla_policy
342 nl80211_pmsr_peer_attr_policy[NL80211_PMSR_PEER_ATTR_MAX + 1] = {
343         [NL80211_PMSR_PEER_ATTR_ADDR] = NLA_POLICY_ETH_ADDR,
344         [NL80211_PMSR_PEER_ATTR_CHAN] = NLA_POLICY_NESTED(nl80211_policy),
345         [NL80211_PMSR_PEER_ATTR_REQ] =
346                 NLA_POLICY_NESTED(nl80211_pmsr_req_attr_policy),
347         [NL80211_PMSR_PEER_ATTR_RESP] = { .type = NLA_REJECT },
348 };
349
350 static const struct nla_policy
351 nl80211_pmsr_attr_policy[NL80211_PMSR_ATTR_MAX + 1] = {
352         [NL80211_PMSR_ATTR_MAX_PEERS] = { .type = NLA_REJECT },
353         [NL80211_PMSR_ATTR_REPORT_AP_TSF] = { .type = NLA_REJECT },
354         [NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR] = { .type = NLA_REJECT },
355         [NL80211_PMSR_ATTR_TYPE_CAPA] = { .type = NLA_REJECT },
356         [NL80211_PMSR_ATTR_PEERS] =
357                 NLA_POLICY_NESTED_ARRAY(nl80211_pmsr_peer_attr_policy),
358 };
359
360 static const struct nla_policy
361 he_obss_pd_policy[NL80211_HE_OBSS_PD_ATTR_MAX + 1] = {
362         [NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET] =
363                 NLA_POLICY_RANGE(NLA_U8, 1, 20),
364         [NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET] =
365                 NLA_POLICY_RANGE(NLA_U8, 1, 20),
366         [NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET] =
367                 NLA_POLICY_RANGE(NLA_U8, 1, 20),
368         [NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP] =
369                 NLA_POLICY_EXACT_LEN(8),
370         [NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP] =
371                 NLA_POLICY_EXACT_LEN(8),
372         [NL80211_HE_OBSS_PD_ATTR_SR_CTRL] = { .type = NLA_U8 },
373 };
374
375 static const struct nla_policy
376 he_bss_color_policy[NL80211_HE_BSS_COLOR_ATTR_MAX + 1] = {
377         [NL80211_HE_BSS_COLOR_ATTR_COLOR] = NLA_POLICY_RANGE(NLA_U8, 1, 63),
378         [NL80211_HE_BSS_COLOR_ATTR_DISABLED] = { .type = NLA_FLAG },
379         [NL80211_HE_BSS_COLOR_ATTR_PARTIAL] = { .type = NLA_FLAG },
380 };
381
382 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
383         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
384                                     .len = NL80211_MAX_SUPP_RATES },
385         [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
386                                 .len = NL80211_MAX_SUPP_HT_RATES },
387         [NL80211_TXRATE_VHT] = NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_txrate_vht)),
388         [NL80211_TXRATE_GI] = { .type = NLA_U8 },
389         [NL80211_TXRATE_HE] = NLA_POLICY_EXACT_LEN(sizeof(struct nl80211_txrate_he)),
390         [NL80211_TXRATE_HE_GI] =  NLA_POLICY_RANGE(NLA_U8,
391                                                    NL80211_RATE_INFO_HE_GI_0_8,
392                                                    NL80211_RATE_INFO_HE_GI_3_2),
393         [NL80211_TXRATE_HE_LTF] = NLA_POLICY_RANGE(NLA_U8,
394                                                    NL80211_RATE_INFO_HE_1XLTF,
395                                                    NL80211_RATE_INFO_HE_4XLTF),
396 };
397
398 static const struct nla_policy
399 nl80211_tid_config_attr_policy[NL80211_TID_CONFIG_ATTR_MAX + 1] = {
400         [NL80211_TID_CONFIG_ATTR_VIF_SUPP] = { .type = NLA_U64 },
401         [NL80211_TID_CONFIG_ATTR_PEER_SUPP] = { .type = NLA_U64 },
402         [NL80211_TID_CONFIG_ATTR_OVERRIDE] = { .type = NLA_FLAG },
403         [NL80211_TID_CONFIG_ATTR_TIDS] = NLA_POLICY_RANGE(NLA_U16, 1, 0xff),
404         [NL80211_TID_CONFIG_ATTR_NOACK] =
405                         NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
406         [NL80211_TID_CONFIG_ATTR_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
407         [NL80211_TID_CONFIG_ATTR_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
408         [NL80211_TID_CONFIG_ATTR_AMPDU_CTRL] =
409                         NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
410         [NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL] =
411                         NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
412         [NL80211_TID_CONFIG_ATTR_AMSDU_CTRL] =
413                         NLA_POLICY_MAX(NLA_U8, NL80211_TID_CONFIG_DISABLE),
414         [NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE] =
415                         NLA_POLICY_MAX(NLA_U8, NL80211_TX_RATE_FIXED),
416         [NL80211_TID_CONFIG_ATTR_TX_RATE] =
417                         NLA_POLICY_NESTED(nl80211_txattr_policy),
418 };
419
420 static const struct nla_policy
421 nl80211_fils_discovery_policy[NL80211_FILS_DISCOVERY_ATTR_MAX + 1] = {
422         [NL80211_FILS_DISCOVERY_ATTR_INT_MIN] = NLA_POLICY_MAX(NLA_U32, 10000),
423         [NL80211_FILS_DISCOVERY_ATTR_INT_MAX] = NLA_POLICY_MAX(NLA_U32, 10000),
424         [NL80211_FILS_DISCOVERY_ATTR_TMPL] =
425                         NLA_POLICY_RANGE(NLA_BINARY,
426                                          NL80211_FILS_DISCOVERY_TMPL_MIN_LEN,
427                                          IEEE80211_MAX_DATA_LEN),
428 };
429
430 static const struct nla_policy
431 nl80211_unsol_bcast_probe_resp_policy[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1] = {
432         [NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] = NLA_POLICY_MAX(NLA_U32, 20),
433         [NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL] = { .type = NLA_BINARY,
434                                                        .len = IEEE80211_MAX_DATA_LEN }
435 };
436
437 static const struct nla_policy
438 sar_specs_policy[NL80211_SAR_ATTR_SPECS_MAX + 1] = {
439         [NL80211_SAR_ATTR_SPECS_POWER] = { .type = NLA_S32 },
440         [NL80211_SAR_ATTR_SPECS_RANGE_INDEX] = {.type = NLA_U32 },
441 };
442
443 static const struct nla_policy
444 sar_policy[NL80211_SAR_ATTR_MAX + 1] = {
445         [NL80211_SAR_ATTR_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_SAR_TYPE),
446         [NL80211_SAR_ATTR_SPECS] = NLA_POLICY_NESTED_ARRAY(sar_specs_policy),
447 };
448
449 static const struct nla_policy
450 nl80211_mbssid_config_policy[NL80211_MBSSID_CONFIG_ATTR_MAX + 1] = {
451         [NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES] = NLA_POLICY_MIN(NLA_U8, 2),
452         [NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY] =
453                                                 NLA_POLICY_MIN(NLA_U8, 1),
454         [NL80211_MBSSID_CONFIG_ATTR_INDEX] = { .type = NLA_U8 },
455         [NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX] = { .type = NLA_U32 },
456         [NL80211_MBSSID_CONFIG_ATTR_EMA] = { .type = NLA_FLAG },
457 };
458
459 static const struct nla_policy
460 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
461         [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
462         [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
463 };
464
465 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
466         [0] = { .strict_start_type = NL80211_ATTR_HE_OBSS_PD },
467         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
468         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
469                                       .len = 20-1 },
470         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
471
472         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
473         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
474         [NL80211_ATTR_WIPHY_EDMG_CHANNELS] = NLA_POLICY_RANGE(NLA_U8,
475                                                 NL80211_EDMG_CHANNELS_MIN,
476                                                 NL80211_EDMG_CHANNELS_MAX),
477         [NL80211_ATTR_WIPHY_EDMG_BW_CONFIG] = NLA_POLICY_RANGE(NLA_U8,
478                                                 NL80211_EDMG_BW_CONFIG_MIN,
479                                                 NL80211_EDMG_BW_CONFIG_MAX),
480
481         [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
482         [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
483         [NL80211_ATTR_CENTER_FREQ1_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
484         [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
485
486         [NL80211_ATTR_WIPHY_RETRY_SHORT] = NLA_POLICY_MIN(NLA_U8, 1),
487         [NL80211_ATTR_WIPHY_RETRY_LONG] = NLA_POLICY_MIN(NLA_U8, 1),
488         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
489         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
490         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
491         [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
492
493         [NL80211_ATTR_IFTYPE] = NLA_POLICY_MAX(NLA_U32, NL80211_IFTYPE_MAX),
494         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
495         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
496
497         [NL80211_ATTR_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
498         [NL80211_ATTR_PREV_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
499
500         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
501         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
502                                     .len = WLAN_MAX_KEY_LEN },
503         [NL80211_ATTR_KEY_IDX] = NLA_POLICY_MAX(NLA_U8, 7),
504         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
505         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
506         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
507         [NL80211_ATTR_KEY_TYPE] =
508                 NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES),
509
510         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
511         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
512         [NL80211_ATTR_BEACON_HEAD] =
513                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_beacon_head,
514                                        IEEE80211_MAX_DATA_LEN),
515         [NL80211_ATTR_BEACON_TAIL] =
516                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
517                                        IEEE80211_MAX_DATA_LEN),
518         [NL80211_ATTR_STA_AID] =
519                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
520         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
521         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
522         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
523                                                .len = NL80211_MAX_SUPP_RATES },
524         [NL80211_ATTR_STA_PLINK_ACTION] =
525                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_ACTIONS - 1),
526         [NL80211_ATTR_STA_TX_POWER_SETTING] =
527                 NLA_POLICY_RANGE(NLA_U8,
528                                  NL80211_TX_POWER_AUTOMATIC,
529                                  NL80211_TX_POWER_FIXED),
530         [NL80211_ATTR_STA_TX_POWER] = { .type = NLA_S16 },
531         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
532         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
533         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
534                                    .len = IEEE80211_MAX_MESH_ID_LEN },
535         [NL80211_ATTR_MPATH_NEXT_HOP] = NLA_POLICY_ETH_ADDR_COMPAT,
536
537         /* allow 3 for NUL-termination, we used to declare this NLA_STRING */
538         [NL80211_ATTR_REG_ALPHA2] = NLA_POLICY_RANGE(NLA_BINARY, 2, 3),
539         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
540
541         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
542         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
543         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
544         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
545                                            .len = NL80211_MAX_SUPP_RATES },
546         [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
547
548         [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
549         [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
550
551         [NL80211_ATTR_HT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_HT_CAPABILITY_LEN),
552
553         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
554         [NL80211_ATTR_IE] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
555                                                    validate_ie_attr,
556                                                    IEEE80211_MAX_DATA_LEN),
557         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
558         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
559
560         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
561                                 .len = IEEE80211_MAX_SSID_LEN },
562         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
563         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
564         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
565         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
566         [NL80211_ATTR_USE_MFP] = NLA_POLICY_RANGE(NLA_U32,
567                                                   NL80211_MFP_NO,
568                                                   NL80211_MFP_OPTIONAL),
569         [NL80211_ATTR_STA_FLAGS2] =
570                 NLA_POLICY_EXACT_LEN_WARN(sizeof(struct nl80211_sta_flag_update)),
571         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
572         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
573         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
574         [NL80211_ATTR_CONTROL_PORT_OVER_NL80211] = { .type = NLA_FLAG },
575         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
576         [NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
577         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
578         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
579         [NL80211_ATTR_PID] = { .type = NLA_U32 },
580         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
581         [NL80211_ATTR_PMKID] = NLA_POLICY_EXACT_LEN_WARN(WLAN_PMKID_LEN),
582         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
583         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
584         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
585         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
586                                  .len = IEEE80211_MAX_DATA_LEN },
587         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
588         [NL80211_ATTR_PS_STATE] = NLA_POLICY_RANGE(NLA_U32,
589                                                    NL80211_PS_DISABLED,
590                                                    NL80211_PS_ENABLED),
591         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
592         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
593         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
594         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
595         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
596         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
597         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
598         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
599         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
600         [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
601         [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
602         [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
603         [NL80211_ATTR_STA_PLINK_STATE] =
604                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_PLINK_STATES - 1),
605         [NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
606         [NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
607         [NL80211_ATTR_MESH_PEER_AID] =
608                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
609         [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
610         [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
611         [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
612         [NL80211_ATTR_HIDDEN_SSID] =
613                 NLA_POLICY_RANGE(NLA_U32,
614                                  NL80211_HIDDEN_SSID_NOT_IN_USE,
615                                  NL80211_HIDDEN_SSID_ZERO_CONTENTS),
616         [NL80211_ATTR_IE_PROBE_RESP] =
617                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
618                                        IEEE80211_MAX_DATA_LEN),
619         [NL80211_ATTR_IE_ASSOC_RESP] =
620                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
621                                        IEEE80211_MAX_DATA_LEN),
622         [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
623         [NL80211_ATTR_STA_WME] = NLA_POLICY_NESTED(nl80211_sta_wme_policy),
624         [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
625         [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
626         [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
627         [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
628         [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
629         [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
630         [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
631         [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
632         [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
633         [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
634                                       .len = IEEE80211_MAX_DATA_LEN },
635         [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
636         [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
637         [NL80211_ATTR_HT_CAPABILITY_MASK] = {
638                 .len = NL80211_HT_CAPABILITY_LEN
639         },
640         [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
641         [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
642         [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
643         [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
644         [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
645
646         /* need to include at least Auth Transaction and Status Code */
647         [NL80211_ATTR_AUTH_DATA] = NLA_POLICY_MIN_LEN(4),
648
649         [NL80211_ATTR_VHT_CAPABILITY] = NLA_POLICY_EXACT_LEN_WARN(NL80211_VHT_CAPABILITY_LEN),
650         [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
651         [NL80211_ATTR_P2P_CTWINDOW] = NLA_POLICY_MAX(NLA_U8, 127),
652         [NL80211_ATTR_P2P_OPPPS] = NLA_POLICY_MAX(NLA_U8, 1),
653         [NL80211_ATTR_LOCAL_MESH_POWER_MODE] =
654                 NLA_POLICY_RANGE(NLA_U32,
655                                  NL80211_MESH_POWER_UNKNOWN + 1,
656                                  NL80211_MESH_POWER_MAX),
657         [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
658         [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
659         [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
660         [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
661         [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
662         [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
663         [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
664                 .len = NL80211_VHT_CAPABILITY_LEN,
665         },
666         [NL80211_ATTR_MDID] = { .type = NLA_U16 },
667         [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
668                                   .len = IEEE80211_MAX_DATA_LEN },
669         [NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
670         [NL80211_ATTR_MAX_CRIT_PROT_DURATION] =
671                 NLA_POLICY_MAX(NLA_U16, NL80211_CRIT_PROTO_MAX_DURATION),
672         [NL80211_ATTR_PEER_AID] =
673                 NLA_POLICY_RANGE(NLA_U16, 1, IEEE80211_MAX_AID),
674         [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
675         [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
676         [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
677         [NL80211_ATTR_CNTDWN_OFFS_BEACON] = { .type = NLA_BINARY },
678         [NL80211_ATTR_CNTDWN_OFFS_PRESP] = { .type = NLA_BINARY },
679         [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = NLA_POLICY_MIN_LEN(2),
680         /*
681          * The value of the Length field of the Supported Operating
682          * Classes element is between 2 and 253.
683          */
684         [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] =
685                 NLA_POLICY_RANGE(NLA_BINARY, 2, 253),
686         [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
687         [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
688         [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
689         [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
690         [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
691         [NL80211_ATTR_QOS_MAP] = NLA_POLICY_RANGE(NLA_BINARY,
692                                                   IEEE80211_QOS_MAP_LEN_MIN,
693                                                   IEEE80211_QOS_MAP_LEN_MAX),
694         [NL80211_ATTR_MAC_HINT] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
695         [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
696         [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
697         [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
698         [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
699         [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
700         [NL80211_ATTR_TSID] = NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_TIDS - 1),
701         [NL80211_ATTR_USER_PRIO] =
702                 NLA_POLICY_MAX(NLA_U8, IEEE80211_NUM_UPS - 1),
703         [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
704         [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
705         [NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
706         [NL80211_ATTR_MAC_MASK] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
707         [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
708         [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
709         [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
710         [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
711         [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
712         [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
713         [NL80211_ATTR_STA_SUPPORT_P2P_PS] =
714                 NLA_POLICY_MAX(NLA_U8, NUM_NL80211_P2P_PS_STATUS - 1),
715         [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
716                 .len = VHT_MUMIMO_GROUPS_DATA_LEN
717         },
718         [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
719         [NL80211_ATTR_NAN_MASTER_PREF] = NLA_POLICY_MIN(NLA_U8, 1),
720         [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
721         [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
722         [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
723                                     .len = FILS_MAX_KEK_LEN },
724         [NL80211_ATTR_FILS_NONCES] = NLA_POLICY_EXACT_LEN_WARN(2 * FILS_NONCE_LEN),
725         [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
726         [NL80211_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
727         [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
728         [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
729                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
730         },
731         [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
732         [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
733                                              .len = FILS_ERP_MAX_USERNAME_LEN },
734         [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
735                                           .len = FILS_ERP_MAX_REALM_LEN },
736         [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
737         [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
738                                         .len = FILS_ERP_MAX_RRK_LEN },
739         [NL80211_ATTR_FILS_CACHE_ID] = NLA_POLICY_EXACT_LEN_WARN(2),
740         [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
741         [NL80211_ATTR_PMKR0_NAME] = NLA_POLICY_EXACT_LEN(WLAN_PMK_NAME_LEN),
742         [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
743         [NL80211_ATTR_EXTERNAL_AUTH_SUPPORT] = { .type = NLA_FLAG },
744
745         [NL80211_ATTR_TXQ_LIMIT] = { .type = NLA_U32 },
746         [NL80211_ATTR_TXQ_MEMORY_LIMIT] = { .type = NLA_U32 },
747         [NL80211_ATTR_TXQ_QUANTUM] = { .type = NLA_U32 },
748         [NL80211_ATTR_HE_CAPABILITY] =
749                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_he_capa,
750                                        NL80211_HE_MAX_CAPABILITY_LEN),
751         [NL80211_ATTR_FTM_RESPONDER] =
752                 NLA_POLICY_NESTED(nl80211_ftm_responder_policy),
753         [NL80211_ATTR_TIMEOUT] = NLA_POLICY_MIN(NLA_U32, 1),
754         [NL80211_ATTR_PEER_MEASUREMENTS] =
755                 NLA_POLICY_NESTED(nl80211_pmsr_attr_policy),
756         [NL80211_ATTR_AIRTIME_WEIGHT] = NLA_POLICY_MIN(NLA_U16, 1),
757         [NL80211_ATTR_SAE_PASSWORD] = { .type = NLA_BINARY,
758                                         .len = SAE_PASSWORD_MAX_LEN },
759         [NL80211_ATTR_TWT_RESPONDER] = { .type = NLA_FLAG },
760         [NL80211_ATTR_HE_OBSS_PD] = NLA_POLICY_NESTED(he_obss_pd_policy),
761         [NL80211_ATTR_VLAN_ID] = NLA_POLICY_RANGE(NLA_U16, 1, VLAN_N_VID - 2),
762         [NL80211_ATTR_HE_BSS_COLOR] = NLA_POLICY_NESTED(he_bss_color_policy),
763         [NL80211_ATTR_TID_CONFIG] =
764                 NLA_POLICY_NESTED_ARRAY(nl80211_tid_config_attr_policy),
765         [NL80211_ATTR_CONTROL_PORT_NO_PREAUTH] = { .type = NLA_FLAG },
766         [NL80211_ATTR_PMK_LIFETIME] = NLA_POLICY_MIN(NLA_U32, 1),
767         [NL80211_ATTR_PMK_REAUTH_THRESHOLD] = NLA_POLICY_RANGE(NLA_U8, 1, 100),
768         [NL80211_ATTR_RECEIVE_MULTICAST] = { .type = NLA_FLAG },
769         [NL80211_ATTR_WIPHY_FREQ_OFFSET] = NLA_POLICY_RANGE(NLA_U32, 0, 999),
770         [NL80211_ATTR_SCAN_FREQ_KHZ] = { .type = NLA_NESTED },
771         [NL80211_ATTR_HE_6GHZ_CAPABILITY] =
772                 NLA_POLICY_EXACT_LEN(sizeof(struct ieee80211_he_6ghz_capa)),
773         [NL80211_ATTR_FILS_DISCOVERY] =
774                 NLA_POLICY_NESTED(nl80211_fils_discovery_policy),
775         [NL80211_ATTR_UNSOL_BCAST_PROBE_RESP] =
776                 NLA_POLICY_NESTED(nl80211_unsol_bcast_probe_resp_policy),
777         [NL80211_ATTR_S1G_CAPABILITY] =
778                 NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
779         [NL80211_ATTR_S1G_CAPABILITY_MASK] =
780                 NLA_POLICY_EXACT_LEN(IEEE80211_S1G_CAPABILITY_LEN),
781         [NL80211_ATTR_SAE_PWE] =
782                 NLA_POLICY_RANGE(NLA_U8, NL80211_SAE_PWE_HUNT_AND_PECK,
783                                  NL80211_SAE_PWE_BOTH),
784         [NL80211_ATTR_RECONNECT_REQUESTED] = { .type = NLA_REJECT },
785         [NL80211_ATTR_SAR_SPEC] = NLA_POLICY_NESTED(sar_policy),
786         [NL80211_ATTR_DISABLE_HE] = { .type = NLA_FLAG },
787         [NL80211_ATTR_OBSS_COLOR_BITMAP] = { .type = NLA_U64 },
788         [NL80211_ATTR_COLOR_CHANGE_COUNT] = { .type = NLA_U8 },
789         [NL80211_ATTR_COLOR_CHANGE_COLOR] = { .type = NLA_U8 },
790         [NL80211_ATTR_COLOR_CHANGE_ELEMS] = NLA_POLICY_NESTED(nl80211_policy),
791         [NL80211_ATTR_MBSSID_CONFIG] =
792                         NLA_POLICY_NESTED(nl80211_mbssid_config_policy),
793         [NL80211_ATTR_MBSSID_ELEMS] = { .type = NLA_NESTED },
794         [NL80211_ATTR_RADAR_BACKGROUND] = { .type = NLA_FLAG },
795         [NL80211_ATTR_AP_SETTINGS_FLAGS] = { .type = NLA_U32 },
796         [NL80211_ATTR_EHT_CAPABILITY] =
797                 NLA_POLICY_RANGE(NLA_BINARY,
798                                  NL80211_EHT_MIN_CAPABILITY_LEN,
799                                  NL80211_EHT_MAX_CAPABILITY_LEN),
800         [NL80211_ATTR_DISABLE_EHT] = { .type = NLA_FLAG },
801         [NL80211_ATTR_MLO_LINKS] =
802                 NLA_POLICY_NESTED_ARRAY(nl80211_policy),
803         [NL80211_ATTR_MLO_LINK_ID] =
804                 NLA_POLICY_RANGE(NLA_U8, 0, IEEE80211_MLD_MAX_NUM_LINKS),
805         [NL80211_ATTR_MLD_ADDR] = NLA_POLICY_EXACT_LEN(ETH_ALEN),
806         [NL80211_ATTR_MLO_SUPPORT] = { .type = NLA_FLAG },
807         [NL80211_ATTR_MAX_NUM_AKM_SUITES] = { .type = NLA_REJECT },
808 };
809
810 /* policy for the key attributes */
811 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
812         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
813         [NL80211_KEY_IDX] = { .type = NLA_U8 },
814         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
815         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
816         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
817         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
818         [NL80211_KEY_TYPE] = NLA_POLICY_MAX(NLA_U32, NUM_NL80211_KEYTYPES - 1),
819         [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
820         [NL80211_KEY_MODE] = NLA_POLICY_RANGE(NLA_U8, 0, NL80211_KEY_SET_TX),
821 };
822
823 /* policy for the key default flags */
824 static const struct nla_policy
825 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
826         [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
827         [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
828 };
829
830 #ifdef CONFIG_PM
831 /* policy for WoWLAN attributes */
832 static const struct nla_policy
833 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
834         [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
835         [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
836         [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
837         [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
838         [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
839         [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
840         [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
841         [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
842         [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
843         [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
844 };
845
846 static const struct nla_policy
847 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
848         [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
849         [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
850         [NL80211_WOWLAN_TCP_DST_MAC] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
851         [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
852         [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
853         [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
854         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
855                 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
856         },
857         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
858                 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
859         },
860         [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
861         [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = NLA_POLICY_MIN_LEN(1),
862         [NL80211_WOWLAN_TCP_WAKE_MASK] = NLA_POLICY_MIN_LEN(1),
863 };
864 #endif /* CONFIG_PM */
865
866 /* policy for coalesce rule attributes */
867 static const struct nla_policy
868 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
869         [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
870         [NL80211_ATTR_COALESCE_RULE_CONDITION] =
871                 NLA_POLICY_RANGE(NLA_U32,
872                                  NL80211_COALESCE_CONDITION_MATCH,
873                                  NL80211_COALESCE_CONDITION_NO_MATCH),
874         [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
875 };
876
877 /* policy for GTK rekey offload attributes */
878 static const struct nla_policy
879 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
880         [NL80211_REKEY_DATA_KEK] = {
881                 .type = NLA_BINARY,
882                 .len = NL80211_KEK_EXT_LEN
883         },
884         [NL80211_REKEY_DATA_KCK] = {
885                 .type = NLA_BINARY,
886                 .len = NL80211_KCK_EXT_LEN
887         },
888         [NL80211_REKEY_DATA_REPLAY_CTR] = NLA_POLICY_EXACT_LEN(NL80211_REPLAY_CTR_LEN),
889         [NL80211_REKEY_DATA_AKM] = { .type = NLA_U32 },
890 };
891
892 static const struct nla_policy
893 nl80211_match_band_rssi_policy[NUM_NL80211_BANDS] = {
894         [NL80211_BAND_2GHZ] = { .type = NLA_S32 },
895         [NL80211_BAND_5GHZ] = { .type = NLA_S32 },
896         [NL80211_BAND_6GHZ] = { .type = NLA_S32 },
897         [NL80211_BAND_60GHZ] = { .type = NLA_S32 },
898         [NL80211_BAND_LC]    = { .type = NLA_S32 },
899 };
900
901 static const struct nla_policy
902 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
903         [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
904                                                  .len = IEEE80211_MAX_SSID_LEN },
905         [NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
906         [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
907         [NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI] =
908                 NLA_POLICY_NESTED(nl80211_match_band_rssi_policy),
909 };
910
911 static const struct nla_policy
912 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
913         [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
914         [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
915 };
916
917 static const struct nla_policy
918 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
919         [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
920         [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
921         [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
922                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
923         },
924 };
925
926 /* policy for NAN function attributes */
927 static const struct nla_policy
928 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
929         [NL80211_NAN_FUNC_TYPE] =
930                 NLA_POLICY_MAX(NLA_U8, NL80211_NAN_FUNC_MAX_TYPE),
931         [NL80211_NAN_FUNC_SERVICE_ID] = {
932                                     .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
933         [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
934         [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
935         [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
936         [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
937         [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
938         [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = NLA_POLICY_EXACT_LEN_WARN(ETH_ALEN),
939         [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
940         [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
941         [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
942                         .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
943         [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
944         [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
945         [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
946         [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
947         [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
948 };
949
950 /* policy for Service Response Filter attributes */
951 static const struct nla_policy
952 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
953         [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
954         [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
955                                  .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
956         [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
957         [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
958 };
959
960 /* policy for packet pattern attributes */
961 static const struct nla_policy
962 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
963         [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
964         [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
965         [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
966 };
967
968 static int nl80211_prepare_wdev_dump(struct netlink_callback *cb,
969                                      struct cfg80211_registered_device **rdev,
970                                      struct wireless_dev **wdev,
971                                      struct nlattr **attrbuf)
972 {
973         int err;
974
975         if (!cb->args[0]) {
976                 struct nlattr **attrbuf_free = NULL;
977
978                 if (!attrbuf) {
979                         attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
980                                           GFP_KERNEL);
981                         if (!attrbuf)
982                                 return -ENOMEM;
983                         attrbuf_free = attrbuf;
984                 }
985
986                 err = nlmsg_parse_deprecated(cb->nlh,
987                                              GENL_HDRLEN + nl80211_fam.hdrsize,
988                                              attrbuf, nl80211_fam.maxattr,
989                                              nl80211_policy, NULL);
990                 if (err) {
991                         kfree(attrbuf_free);
992                         return err;
993                 }
994
995                 rtnl_lock();
996                 *wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(cb->skb->sk),
997                                                    attrbuf);
998                 kfree(attrbuf_free);
999                 if (IS_ERR(*wdev)) {
1000                         rtnl_unlock();
1001                         return PTR_ERR(*wdev);
1002                 }
1003                 *rdev = wiphy_to_rdev((*wdev)->wiphy);
1004                 mutex_lock(&(*rdev)->wiphy.mtx);
1005                 rtnl_unlock();
1006                 /* 0 is the first index - add 1 to parse only once */
1007                 cb->args[0] = (*rdev)->wiphy_idx + 1;
1008                 cb->args[1] = (*wdev)->identifier;
1009         } else {
1010                 /* subtract the 1 again here */
1011                 struct wiphy *wiphy;
1012                 struct wireless_dev *tmp;
1013
1014                 rtnl_lock();
1015                 wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
1016                 if (!wiphy) {
1017                         rtnl_unlock();
1018                         return -ENODEV;
1019                 }
1020                 *rdev = wiphy_to_rdev(wiphy);
1021                 *wdev = NULL;
1022
1023                 list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
1024                         if (tmp->identifier == cb->args[1]) {
1025                                 *wdev = tmp;
1026                                 break;
1027                         }
1028                 }
1029
1030                 if (!*wdev) {
1031                         rtnl_unlock();
1032                         return -ENODEV;
1033                 }
1034                 mutex_lock(&(*rdev)->wiphy.mtx);
1035                 rtnl_unlock();
1036         }
1037
1038         return 0;
1039 }
1040
1041 /* message building helper */
1042 void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
1043                      int flags, u8 cmd)
1044 {
1045         /* since there is no private header just add the generic one */
1046         return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
1047 }
1048
1049 static int nl80211_msg_put_wmm_rules(struct sk_buff *msg,
1050                                      const struct ieee80211_reg_rule *rule)
1051 {
1052         int j;
1053         struct nlattr *nl_wmm_rules =
1054                 nla_nest_start_noflag(msg, NL80211_FREQUENCY_ATTR_WMM);
1055
1056         if (!nl_wmm_rules)
1057                 goto nla_put_failure;
1058
1059         for (j = 0; j < IEEE80211_NUM_ACS; j++) {
1060                 struct nlattr *nl_wmm_rule = nla_nest_start_noflag(msg, j);
1061
1062                 if (!nl_wmm_rule)
1063                         goto nla_put_failure;
1064
1065                 if (nla_put_u16(msg, NL80211_WMMR_CW_MIN,
1066                                 rule->wmm_rule.client[j].cw_min) ||
1067                     nla_put_u16(msg, NL80211_WMMR_CW_MAX,
1068                                 rule->wmm_rule.client[j].cw_max) ||
1069                     nla_put_u8(msg, NL80211_WMMR_AIFSN,
1070                                rule->wmm_rule.client[j].aifsn) ||
1071                     nla_put_u16(msg, NL80211_WMMR_TXOP,
1072                                 rule->wmm_rule.client[j].cot))
1073                         goto nla_put_failure;
1074
1075                 nla_nest_end(msg, nl_wmm_rule);
1076         }
1077         nla_nest_end(msg, nl_wmm_rules);
1078
1079         return 0;
1080
1081 nla_put_failure:
1082         return -ENOBUFS;
1083 }
1084
1085 static int nl80211_msg_put_channel(struct sk_buff *msg, struct wiphy *wiphy,
1086                                    struct ieee80211_channel *chan,
1087                                    bool large)
1088 {
1089         /* Some channels must be completely excluded from the
1090          * list to protect old user-space tools from breaking
1091          */
1092         if (!large && chan->flags &
1093             (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
1094                 return 0;
1095         if (!large && chan->freq_offset)
1096                 return 0;
1097
1098         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
1099                         chan->center_freq))
1100                 goto nla_put_failure;
1101
1102         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_OFFSET, chan->freq_offset))
1103                 goto nla_put_failure;
1104
1105         if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
1106             nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
1107                 goto nla_put_failure;
1108         if (chan->flags & IEEE80211_CHAN_NO_IR) {
1109                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
1110                         goto nla_put_failure;
1111                 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
1112                         goto nla_put_failure;
1113         }
1114         if (chan->flags & IEEE80211_CHAN_RADAR) {
1115                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
1116                         goto nla_put_failure;
1117                 if (large) {
1118                         u32 time;
1119
1120                         time = elapsed_jiffies_msecs(chan->dfs_state_entered);
1121
1122                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
1123                                         chan->dfs_state))
1124                                 goto nla_put_failure;
1125                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
1126                                         time))
1127                                 goto nla_put_failure;
1128                         if (nla_put_u32(msg,
1129                                         NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
1130                                         chan->dfs_cac_ms))
1131                                 goto nla_put_failure;
1132                 }
1133         }
1134
1135         if (large) {
1136                 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
1137                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
1138                         goto nla_put_failure;
1139                 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
1140                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
1141                         goto nla_put_failure;
1142                 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
1143                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
1144                         goto nla_put_failure;
1145                 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
1146                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
1147                         goto nla_put_failure;
1148                 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
1149                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
1150                         goto nla_put_failure;
1151                 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
1152                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
1153                         goto nla_put_failure;
1154                 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
1155                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
1156                         goto nla_put_failure;
1157                 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
1158                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
1159                         goto nla_put_failure;
1160                 if ((chan->flags & IEEE80211_CHAN_NO_HE) &&
1161                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HE))
1162                         goto nla_put_failure;
1163                 if ((chan->flags & IEEE80211_CHAN_1MHZ) &&
1164                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_1MHZ))
1165                         goto nla_put_failure;
1166                 if ((chan->flags & IEEE80211_CHAN_2MHZ) &&
1167                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_2MHZ))
1168                         goto nla_put_failure;
1169                 if ((chan->flags & IEEE80211_CHAN_4MHZ) &&
1170                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_4MHZ))
1171                         goto nla_put_failure;
1172                 if ((chan->flags & IEEE80211_CHAN_8MHZ) &&
1173                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_8MHZ))
1174                         goto nla_put_failure;
1175                 if ((chan->flags & IEEE80211_CHAN_16MHZ) &&
1176                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_16MHZ))
1177                         goto nla_put_failure;
1178                 if ((chan->flags & IEEE80211_CHAN_NO_320MHZ) &&
1179                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_320MHZ))
1180                         goto nla_put_failure;
1181                 if ((chan->flags & IEEE80211_CHAN_NO_EHT) &&
1182                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_EHT))
1183                         goto nla_put_failure;
1184         }
1185
1186         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
1187                         DBM_TO_MBM(chan->max_power)))
1188                 goto nla_put_failure;
1189
1190         if (large) {
1191                 const struct ieee80211_reg_rule *rule =
1192                         freq_reg_info(wiphy, MHZ_TO_KHZ(chan->center_freq));
1193
1194                 if (!IS_ERR_OR_NULL(rule) && rule->has_wmm) {
1195                         if (nl80211_msg_put_wmm_rules(msg, rule))
1196                                 goto nla_put_failure;
1197                 }
1198         }
1199
1200         return 0;
1201
1202  nla_put_failure:
1203         return -ENOBUFS;
1204 }
1205
1206 static bool nl80211_put_txq_stats(struct sk_buff *msg,
1207                                   struct cfg80211_txq_stats *txqstats,
1208                                   int attrtype)
1209 {
1210         struct nlattr *txqattr;
1211
1212 #define PUT_TXQVAL_U32(attr, memb) do {                                   \
1213         if (txqstats->filled & BIT(NL80211_TXQ_STATS_ ## attr) &&         \
1214             nla_put_u32(msg, NL80211_TXQ_STATS_ ## attr, txqstats->memb)) \
1215                 return false;                                             \
1216         } while (0)
1217
1218         txqattr = nla_nest_start_noflag(msg, attrtype);
1219         if (!txqattr)
1220                 return false;
1221
1222         PUT_TXQVAL_U32(BACKLOG_BYTES, backlog_bytes);
1223         PUT_TXQVAL_U32(BACKLOG_PACKETS, backlog_packets);
1224         PUT_TXQVAL_U32(FLOWS, flows);
1225         PUT_TXQVAL_U32(DROPS, drops);
1226         PUT_TXQVAL_U32(ECN_MARKS, ecn_marks);
1227         PUT_TXQVAL_U32(OVERLIMIT, overlimit);
1228         PUT_TXQVAL_U32(OVERMEMORY, overmemory);
1229         PUT_TXQVAL_U32(COLLISIONS, collisions);
1230         PUT_TXQVAL_U32(TX_BYTES, tx_bytes);
1231         PUT_TXQVAL_U32(TX_PACKETS, tx_packets);
1232         PUT_TXQVAL_U32(MAX_FLOWS, max_flows);
1233         nla_nest_end(msg, txqattr);
1234
1235 #undef PUT_TXQVAL_U32
1236         return true;
1237 }
1238
1239 /* netlink command implementations */
1240
1241 /**
1242  * nl80211_link_id - return link ID
1243  * @attrs: attributes to look at
1244  *
1245  * Returns: the link ID or 0 if not given
1246  *
1247  * Note this function doesn't do any validation of the link
1248  * ID validity wrt. links that were actually added, so it must
1249  * be called only from ops with %NL80211_FLAG_MLO_VALID_LINK_ID
1250  * or if additional validation is done.
1251  */
1252 static unsigned int nl80211_link_id(struct nlattr **attrs)
1253 {
1254         struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1255
1256         if (!linkid)
1257                 return 0;
1258
1259         return nla_get_u8(linkid);
1260 }
1261
1262 static int nl80211_link_id_or_invalid(struct nlattr **attrs)
1263 {
1264         struct nlattr *linkid = attrs[NL80211_ATTR_MLO_LINK_ID];
1265
1266         if (!linkid)
1267                 return -1;
1268
1269         return nla_get_u8(linkid);
1270 }
1271
1272 struct key_parse {
1273         struct key_params p;
1274         int idx;
1275         int type;
1276         bool def, defmgmt, defbeacon;
1277         bool def_uni, def_multi;
1278 };
1279
1280 static int nl80211_parse_key_new(struct genl_info *info, struct nlattr *key,
1281                                  struct key_parse *k)
1282 {
1283         struct nlattr *tb[NL80211_KEY_MAX + 1];
1284         int err = nla_parse_nested_deprecated(tb, NL80211_KEY_MAX, key,
1285                                               nl80211_key_policy,
1286                                               info->extack);
1287         if (err)
1288                 return err;
1289
1290         k->def = !!tb[NL80211_KEY_DEFAULT];
1291         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
1292         k->defbeacon = !!tb[NL80211_KEY_DEFAULT_BEACON];
1293
1294         if (k->def) {
1295                 k->def_uni = true;
1296                 k->def_multi = true;
1297         }
1298         if (k->defmgmt || k->defbeacon)
1299                 k->def_multi = true;
1300
1301         if (tb[NL80211_KEY_IDX])
1302                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
1303
1304         if (tb[NL80211_KEY_DATA]) {
1305                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
1306                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
1307         }
1308
1309         if (tb[NL80211_KEY_SEQ]) {
1310                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
1311                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
1312         }
1313
1314         if (tb[NL80211_KEY_CIPHER])
1315                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
1316
1317         if (tb[NL80211_KEY_TYPE])
1318                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
1319
1320         if (tb[NL80211_KEY_DEFAULT_TYPES]) {
1321                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1322
1323                 err = nla_parse_nested_deprecated(kdt,
1324                                                   NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1325                                                   tb[NL80211_KEY_DEFAULT_TYPES],
1326                                                   nl80211_key_default_policy,
1327                                                   info->extack);
1328                 if (err)
1329                         return err;
1330
1331                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1332                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1333         }
1334
1335         if (tb[NL80211_KEY_MODE])
1336                 k->p.mode = nla_get_u8(tb[NL80211_KEY_MODE]);
1337
1338         return 0;
1339 }
1340
1341 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
1342 {
1343         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
1344                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
1345                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
1346         }
1347
1348         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
1349                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
1350                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
1351         }
1352
1353         if (info->attrs[NL80211_ATTR_KEY_IDX])
1354                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1355
1356         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
1357                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
1358
1359         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
1360         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
1361
1362         if (k->def) {
1363                 k->def_uni = true;
1364                 k->def_multi = true;
1365         }
1366         if (k->defmgmt)
1367                 k->def_multi = true;
1368
1369         if (info->attrs[NL80211_ATTR_KEY_TYPE])
1370                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1371
1372         if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
1373                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
1374                 int err = nla_parse_nested_deprecated(kdt,
1375                                                       NUM_NL80211_KEY_DEFAULT_TYPES - 1,
1376                                                       info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
1377                                                       nl80211_key_default_policy,
1378                                                       info->extack);
1379                 if (err)
1380                         return err;
1381
1382                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
1383                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
1384         }
1385
1386         return 0;
1387 }
1388
1389 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
1390 {
1391         int err;
1392
1393         memset(k, 0, sizeof(*k));
1394         k->idx = -1;
1395         k->type = -1;
1396
1397         if (info->attrs[NL80211_ATTR_KEY])
1398                 err = nl80211_parse_key_new(info, info->attrs[NL80211_ATTR_KEY], k);
1399         else
1400                 err = nl80211_parse_key_old(info, k);
1401
1402         if (err)
1403                 return err;
1404
1405         if ((k->def ? 1 : 0) + (k->defmgmt ? 1 : 0) +
1406             (k->defbeacon ? 1 : 0) > 1) {
1407                 GENL_SET_ERR_MSG(info,
1408                                  "key with multiple default flags is invalid");
1409                 return -EINVAL;
1410         }
1411
1412         if (k->defmgmt || k->defbeacon) {
1413                 if (k->def_uni || !k->def_multi) {
1414                         GENL_SET_ERR_MSG(info,
1415                                          "defmgmt/defbeacon key must be mcast");
1416                         return -EINVAL;
1417                 }
1418         }
1419
1420         if (k->idx != -1) {
1421                 if (k->defmgmt) {
1422                         if (k->idx < 4 || k->idx > 5) {
1423                                 GENL_SET_ERR_MSG(info,
1424                                                  "defmgmt key idx not 4 or 5");
1425                                 return -EINVAL;
1426                         }
1427                 } else if (k->defbeacon) {
1428                         if (k->idx < 6 || k->idx > 7) {
1429                                 GENL_SET_ERR_MSG(info,
1430                                                  "defbeacon key idx not 6 or 7");
1431                                 return -EINVAL;
1432                         }
1433                 } else if (k->def) {
1434                         if (k->idx < 0 || k->idx > 3) {
1435                                 GENL_SET_ERR_MSG(info, "def key idx not 0-3");
1436                                 return -EINVAL;
1437                         }
1438                 } else {
1439                         if (k->idx < 0 || k->idx > 7) {
1440                                 GENL_SET_ERR_MSG(info, "key idx not 0-7");
1441                                 return -EINVAL;
1442                         }
1443                 }
1444         }
1445
1446         return 0;
1447 }
1448
1449 static struct cfg80211_cached_keys *
1450 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
1451                        struct genl_info *info, bool *no_ht)
1452 {
1453         struct nlattr *keys = info->attrs[NL80211_ATTR_KEYS];
1454         struct key_parse parse;
1455         struct nlattr *key;
1456         struct cfg80211_cached_keys *result;
1457         int rem, err, def = 0;
1458         bool have_key = false;
1459
1460         nla_for_each_nested(key, keys, rem) {
1461                 have_key = true;
1462                 break;
1463         }
1464
1465         if (!have_key)
1466                 return NULL;
1467
1468         result = kzalloc(sizeof(*result), GFP_KERNEL);
1469         if (!result)
1470                 return ERR_PTR(-ENOMEM);
1471
1472         result->def = -1;
1473
1474         nla_for_each_nested(key, keys, rem) {
1475                 memset(&parse, 0, sizeof(parse));
1476                 parse.idx = -1;
1477
1478                 err = nl80211_parse_key_new(info, key, &parse);
1479                 if (err)
1480                         goto error;
1481                 err = -EINVAL;
1482                 if (!parse.p.key)
1483                         goto error;
1484                 if (parse.idx < 0 || parse.idx > 3) {
1485                         GENL_SET_ERR_MSG(info, "key index out of range [0-3]");
1486                         goto error;
1487                 }
1488                 if (parse.def) {
1489                         if (def) {
1490                                 GENL_SET_ERR_MSG(info,
1491                                                  "only one key can be default");
1492                                 goto error;
1493                         }
1494                         def = 1;
1495                         result->def = parse.idx;
1496                         if (!parse.def_uni || !parse.def_multi)
1497                                 goto error;
1498                 } else if (parse.defmgmt)
1499                         goto error;
1500                 err = cfg80211_validate_key_settings(rdev, &parse.p,
1501                                                      parse.idx, false, NULL);
1502                 if (err)
1503                         goto error;
1504                 if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
1505                     parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
1506                         GENL_SET_ERR_MSG(info, "connect key must be WEP");
1507                         err = -EINVAL;
1508                         goto error;
1509                 }
1510                 result->params[parse.idx].cipher = parse.p.cipher;
1511                 result->params[parse.idx].key_len = parse.p.key_len;
1512                 result->params[parse.idx].key = result->data[parse.idx];
1513                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
1514
1515                 /* must be WEP key if we got here */
1516                 if (no_ht)
1517                         *no_ht = true;
1518         }
1519
1520         if (result->def < 0) {
1521                 err = -EINVAL;
1522                 GENL_SET_ERR_MSG(info, "need a default/TX key");
1523                 goto error;
1524         }
1525
1526         return result;
1527  error:
1528         kfree(result);
1529         return ERR_PTR(err);
1530 }
1531
1532 static int nl80211_key_allowed(struct wireless_dev *wdev)
1533 {
1534         ASSERT_WDEV_LOCK(wdev);
1535
1536         switch (wdev->iftype) {
1537         case NL80211_IFTYPE_AP:
1538         case NL80211_IFTYPE_AP_VLAN:
1539         case NL80211_IFTYPE_P2P_GO:
1540         case NL80211_IFTYPE_MESH_POINT:
1541                 break;
1542         case NL80211_IFTYPE_ADHOC:
1543                 if (wdev->u.ibss.current_bss)
1544                         return 0;
1545                 return -ENOLINK;
1546         case NL80211_IFTYPE_STATION:
1547         case NL80211_IFTYPE_P2P_CLIENT:
1548                 if (wdev->connected)
1549                         return 0;
1550                 return -ENOLINK;
1551         case NL80211_IFTYPE_UNSPECIFIED:
1552         case NL80211_IFTYPE_OCB:
1553         case NL80211_IFTYPE_MONITOR:
1554         case NL80211_IFTYPE_NAN:
1555         case NL80211_IFTYPE_P2P_DEVICE:
1556         case NL80211_IFTYPE_WDS:
1557         case NUM_NL80211_IFTYPES:
1558                 return -EINVAL;
1559         }
1560
1561         return 0;
1562 }
1563
1564 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1565                                                         u32 freq)
1566 {
1567         struct ieee80211_channel *chan;
1568
1569         chan = ieee80211_get_channel_khz(wiphy, freq);
1570         if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1571                 return NULL;
1572         return chan;
1573 }
1574
1575 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1576 {
1577         struct nlattr *nl_modes = nla_nest_start_noflag(msg, attr);
1578         int i;
1579
1580         if (!nl_modes)
1581                 goto nla_put_failure;
1582
1583         i = 0;
1584         while (ifmodes) {
1585                 if ((ifmodes & 1) && nla_put_flag(msg, i))
1586                         goto nla_put_failure;
1587                 ifmodes >>= 1;
1588                 i++;
1589         }
1590
1591         nla_nest_end(msg, nl_modes);
1592         return 0;
1593
1594 nla_put_failure:
1595         return -ENOBUFS;
1596 }
1597
1598 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1599                                           struct sk_buff *msg,
1600                                           bool large)
1601 {
1602         struct nlattr *nl_combis;
1603         int i, j;
1604
1605         nl_combis = nla_nest_start_noflag(msg,
1606                                           NL80211_ATTR_INTERFACE_COMBINATIONS);
1607         if (!nl_combis)
1608                 goto nla_put_failure;
1609
1610         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1611                 const struct ieee80211_iface_combination *c;
1612                 struct nlattr *nl_combi, *nl_limits;
1613
1614                 c = &wiphy->iface_combinations[i];
1615
1616                 nl_combi = nla_nest_start_noflag(msg, i + 1);
1617                 if (!nl_combi)
1618                         goto nla_put_failure;
1619
1620                 nl_limits = nla_nest_start_noflag(msg,
1621                                                   NL80211_IFACE_COMB_LIMITS);
1622                 if (!nl_limits)
1623                         goto nla_put_failure;
1624
1625                 for (j = 0; j < c->n_limits; j++) {
1626                         struct nlattr *nl_limit;
1627
1628                         nl_limit = nla_nest_start_noflag(msg, j + 1);
1629                         if (!nl_limit)
1630                                 goto nla_put_failure;
1631                         if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1632                                         c->limits[j].max))
1633                                 goto nla_put_failure;
1634                         if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1635                                                 c->limits[j].types))
1636                                 goto nla_put_failure;
1637                         nla_nest_end(msg, nl_limit);
1638                 }
1639
1640                 nla_nest_end(msg, nl_limits);
1641
1642                 if (c->beacon_int_infra_match &&
1643                     nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1644                         goto nla_put_failure;
1645                 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1646                                 c->num_different_channels) ||
1647                     nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1648                                 c->max_interfaces))
1649                         goto nla_put_failure;
1650                 if (large &&
1651                     (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1652                                 c->radar_detect_widths) ||
1653                      nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1654                                 c->radar_detect_regions)))
1655                         goto nla_put_failure;
1656                 if (c->beacon_int_min_gcd &&
1657                     nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1658                                 c->beacon_int_min_gcd))
1659                         goto nla_put_failure;
1660
1661                 nla_nest_end(msg, nl_combi);
1662         }
1663
1664         nla_nest_end(msg, nl_combis);
1665
1666         return 0;
1667 nla_put_failure:
1668         return -ENOBUFS;
1669 }
1670
1671 #ifdef CONFIG_PM
1672 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1673                                         struct sk_buff *msg)
1674 {
1675         const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1676         struct nlattr *nl_tcp;
1677
1678         if (!tcp)
1679                 return 0;
1680
1681         nl_tcp = nla_nest_start_noflag(msg,
1682                                        NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1683         if (!nl_tcp)
1684                 return -ENOBUFS;
1685
1686         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1687                         tcp->data_payload_max))
1688                 return -ENOBUFS;
1689
1690         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1691                         tcp->data_payload_max))
1692                 return -ENOBUFS;
1693
1694         if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1695                 return -ENOBUFS;
1696
1697         if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1698                                 sizeof(*tcp->tok), tcp->tok))
1699                 return -ENOBUFS;
1700
1701         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1702                         tcp->data_interval_max))
1703                 return -ENOBUFS;
1704
1705         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1706                         tcp->wake_payload_max))
1707                 return -ENOBUFS;
1708
1709         nla_nest_end(msg, nl_tcp);
1710         return 0;
1711 }
1712
1713 static int nl80211_send_wowlan(struct sk_buff *msg,
1714                                struct cfg80211_registered_device *rdev,
1715                                bool large)
1716 {
1717         struct nlattr *nl_wowlan;
1718
1719         if (!rdev->wiphy.wowlan)
1720                 return 0;
1721
1722         nl_wowlan = nla_nest_start_noflag(msg,
1723                                           NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1724         if (!nl_wowlan)
1725                 return -ENOBUFS;
1726
1727         if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1728              nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1729             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1730              nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1731             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1732              nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1733             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1734              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1735             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1736              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1737             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1738              nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1739             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1740              nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1741             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1742              nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1743                 return -ENOBUFS;
1744
1745         if (rdev->wiphy.wowlan->n_patterns) {
1746                 struct nl80211_pattern_support pat = {
1747                         .max_patterns = rdev->wiphy.wowlan->n_patterns,
1748                         .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1749                         .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1750                         .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1751                 };
1752
1753                 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1754                             sizeof(pat), &pat))
1755                         return -ENOBUFS;
1756         }
1757
1758         if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1759             nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1760                         rdev->wiphy.wowlan->max_nd_match_sets))
1761                 return -ENOBUFS;
1762
1763         if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1764                 return -ENOBUFS;
1765
1766         nla_nest_end(msg, nl_wowlan);
1767
1768         return 0;
1769 }
1770 #endif
1771
1772 static int nl80211_send_coalesce(struct sk_buff *msg,
1773                                  struct cfg80211_registered_device *rdev)
1774 {
1775         struct nl80211_coalesce_rule_support rule;
1776
1777         if (!rdev->wiphy.coalesce)
1778                 return 0;
1779
1780         rule.max_rules = rdev->wiphy.coalesce->n_rules;
1781         rule.max_delay = rdev->wiphy.coalesce->max_delay;
1782         rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1783         rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1784         rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1785         rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1786
1787         if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1788                 return -ENOBUFS;
1789
1790         return 0;
1791 }
1792
1793 static int
1794 nl80211_send_iftype_data(struct sk_buff *msg,
1795                          const struct ieee80211_supported_band *sband,
1796                          const struct ieee80211_sband_iftype_data *iftdata)
1797 {
1798         const struct ieee80211_sta_he_cap *he_cap = &iftdata->he_cap;
1799         const struct ieee80211_sta_eht_cap *eht_cap = &iftdata->eht_cap;
1800
1801         if (nl80211_put_iftypes(msg, NL80211_BAND_IFTYPE_ATTR_IFTYPES,
1802                                 iftdata->types_mask))
1803                 return -ENOBUFS;
1804
1805         if (he_cap->has_he) {
1806                 if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MAC,
1807                             sizeof(he_cap->he_cap_elem.mac_cap_info),
1808                             he_cap->he_cap_elem.mac_cap_info) ||
1809                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PHY,
1810                             sizeof(he_cap->he_cap_elem.phy_cap_info),
1811                             he_cap->he_cap_elem.phy_cap_info) ||
1812                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_MCS_SET,
1813                             sizeof(he_cap->he_mcs_nss_supp),
1814                             &he_cap->he_mcs_nss_supp) ||
1815                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_CAP_PPE,
1816                             sizeof(he_cap->ppe_thres), he_cap->ppe_thres))
1817                         return -ENOBUFS;
1818         }
1819
1820         if (eht_cap->has_eht && he_cap->has_he) {
1821                 u8 mcs_nss_size, ppe_thresh_size;
1822                 u16 ppe_thres_hdr;
1823                 bool is_ap;
1824
1825                 is_ap = iftdata->types_mask & BIT(NL80211_IFTYPE_AP) ||
1826                         iftdata->types_mask & BIT(NL80211_IFTYPE_P2P_GO);
1827
1828                 mcs_nss_size =
1829                         ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem,
1830                                                    &eht_cap->eht_cap_elem,
1831                                                    is_ap);
1832
1833                 ppe_thres_hdr = get_unaligned_le16(&eht_cap->eht_ppe_thres[0]);
1834                 ppe_thresh_size =
1835                         ieee80211_eht_ppe_size(ppe_thres_hdr,
1836                                                eht_cap->eht_cap_elem.phy_cap_info);
1837
1838                 if (nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MAC,
1839                             sizeof(eht_cap->eht_cap_elem.mac_cap_info),
1840                             eht_cap->eht_cap_elem.mac_cap_info) ||
1841                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PHY,
1842                             sizeof(eht_cap->eht_cap_elem.phy_cap_info),
1843                             eht_cap->eht_cap_elem.phy_cap_info) ||
1844                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_MCS_SET,
1845                             mcs_nss_size, &eht_cap->eht_mcs_nss_supp) ||
1846                     nla_put(msg, NL80211_BAND_IFTYPE_ATTR_EHT_CAP_PPE,
1847                             ppe_thresh_size, eht_cap->eht_ppe_thres))
1848                         return -ENOBUFS;
1849         }
1850
1851         if (sband->band == NL80211_BAND_6GHZ &&
1852             nla_put(msg, NL80211_BAND_IFTYPE_ATTR_HE_6GHZ_CAPA,
1853                     sizeof(iftdata->he_6ghz_capa),
1854                     &iftdata->he_6ghz_capa))
1855                 return -ENOBUFS;
1856
1857         if (iftdata->vendor_elems.data && iftdata->vendor_elems.len &&
1858             nla_put(msg, NL80211_BAND_IFTYPE_ATTR_VENDOR_ELEMS,
1859                     iftdata->vendor_elems.len, iftdata->vendor_elems.data))
1860                 return -ENOBUFS;
1861
1862         return 0;
1863 }
1864
1865 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1866                                       struct ieee80211_supported_band *sband,
1867                                       bool large)
1868 {
1869         struct nlattr *nl_rates, *nl_rate;
1870         struct ieee80211_rate *rate;
1871         int i;
1872
1873         /* add HT info */
1874         if (sband->ht_cap.ht_supported &&
1875             (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1876                      sizeof(sband->ht_cap.mcs),
1877                      &sband->ht_cap.mcs) ||
1878              nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1879                          sband->ht_cap.cap) ||
1880              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1881                         sband->ht_cap.ampdu_factor) ||
1882              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1883                         sband->ht_cap.ampdu_density)))
1884                 return -ENOBUFS;
1885
1886         /* add VHT info */
1887         if (sband->vht_cap.vht_supported &&
1888             (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1889                      sizeof(sband->vht_cap.vht_mcs),
1890                      &sband->vht_cap.vht_mcs) ||
1891              nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1892                          sband->vht_cap.cap)))
1893                 return -ENOBUFS;
1894
1895         if (large && sband->n_iftype_data) {
1896                 struct nlattr *nl_iftype_data =
1897                         nla_nest_start_noflag(msg,
1898                                               NL80211_BAND_ATTR_IFTYPE_DATA);
1899                 int err;
1900
1901                 if (!nl_iftype_data)
1902                         return -ENOBUFS;
1903
1904                 for (i = 0; i < sband->n_iftype_data; i++) {
1905                         struct nlattr *iftdata;
1906
1907                         iftdata = nla_nest_start_noflag(msg, i + 1);
1908                         if (!iftdata)
1909                                 return -ENOBUFS;
1910
1911                         err = nl80211_send_iftype_data(msg, sband,
1912                                                        &sband->iftype_data[i]);
1913                         if (err)
1914                                 return err;
1915
1916                         nla_nest_end(msg, iftdata);
1917                 }
1918
1919                 nla_nest_end(msg, nl_iftype_data);
1920         }
1921
1922         /* add EDMG info */
1923         if (large && sband->edmg_cap.channels &&
1924             (nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_CHANNELS,
1925                        sband->edmg_cap.channels) ||
1926             nla_put_u8(msg, NL80211_BAND_ATTR_EDMG_BW_CONFIG,
1927                        sband->edmg_cap.bw_config)))
1928
1929                 return -ENOBUFS;
1930
1931         /* add bitrates */
1932         nl_rates = nla_nest_start_noflag(msg, NL80211_BAND_ATTR_RATES);
1933         if (!nl_rates)
1934                 return -ENOBUFS;
1935
1936         for (i = 0; i < sband->n_bitrates; i++) {
1937                 nl_rate = nla_nest_start_noflag(msg, i);
1938                 if (!nl_rate)
1939                         return -ENOBUFS;
1940
1941                 rate = &sband->bitrates[i];
1942                 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1943                                 rate->bitrate))
1944                         return -ENOBUFS;
1945                 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1946                     nla_put_flag(msg,
1947                                  NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1948                         return -ENOBUFS;
1949
1950                 nla_nest_end(msg, nl_rate);
1951         }
1952
1953         nla_nest_end(msg, nl_rates);
1954
1955         return 0;
1956 }
1957
1958 static int
1959 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1960                          const struct ieee80211_txrx_stypes *mgmt_stypes)
1961 {
1962         u16 stypes;
1963         struct nlattr *nl_ftypes, *nl_ifs;
1964         enum nl80211_iftype ift;
1965         int i;
1966
1967         if (!mgmt_stypes)
1968                 return 0;
1969
1970         nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_TX_FRAME_TYPES);
1971         if (!nl_ifs)
1972                 return -ENOBUFS;
1973
1974         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1975                 nl_ftypes = nla_nest_start_noflag(msg, ift);
1976                 if (!nl_ftypes)
1977                         return -ENOBUFS;
1978                 i = 0;
1979                 stypes = mgmt_stypes[ift].tx;
1980                 while (stypes) {
1981                         if ((stypes & 1) &&
1982                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1983                                         (i << 4) | IEEE80211_FTYPE_MGMT))
1984                                 return -ENOBUFS;
1985                         stypes >>= 1;
1986                         i++;
1987                 }
1988                 nla_nest_end(msg, nl_ftypes);
1989         }
1990
1991         nla_nest_end(msg, nl_ifs);
1992
1993         nl_ifs = nla_nest_start_noflag(msg, NL80211_ATTR_RX_FRAME_TYPES);
1994         if (!nl_ifs)
1995                 return -ENOBUFS;
1996
1997         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1998                 nl_ftypes = nla_nest_start_noflag(msg, ift);
1999                 if (!nl_ftypes)
2000                         return -ENOBUFS;
2001                 i = 0;
2002                 stypes = mgmt_stypes[ift].rx;
2003                 while (stypes) {
2004                         if ((stypes & 1) &&
2005                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
2006                                         (i << 4) | IEEE80211_FTYPE_MGMT))
2007                                 return -ENOBUFS;
2008                         stypes >>= 1;
2009                         i++;
2010                 }
2011                 nla_nest_end(msg, nl_ftypes);
2012         }
2013         nla_nest_end(msg, nl_ifs);
2014
2015         return 0;
2016 }
2017
2018 #define CMD(op, n)                                                      \
2019          do {                                                           \
2020                 if (rdev->ops->op) {                                    \
2021                         i++;                                            \
2022                         if (nla_put_u32(msg, i, NL80211_CMD_ ## n))     \
2023                                 goto nla_put_failure;                   \
2024                 }                                                       \
2025         } while (0)
2026
2027 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
2028                                         struct sk_buff *msg)
2029 {
2030         int i = 0;
2031
2032         /*
2033          * do *NOT* add anything into this function, new things need to be
2034          * advertised only to new versions of userspace that can deal with
2035          * the split (and they can't possibly care about new features...
2036          */
2037         CMD(add_virtual_intf, NEW_INTERFACE);
2038         CMD(change_virtual_intf, SET_INTERFACE);
2039         CMD(add_key, NEW_KEY);
2040         CMD(start_ap, START_AP);
2041         CMD(add_station, NEW_STATION);
2042         CMD(add_mpath, NEW_MPATH);
2043         CMD(update_mesh_config, SET_MESH_CONFIG);
2044         CMD(change_bss, SET_BSS);
2045         CMD(auth, AUTHENTICATE);
2046         CMD(assoc, ASSOCIATE);
2047         CMD(deauth, DEAUTHENTICATE);
2048         CMD(disassoc, DISASSOCIATE);
2049         CMD(join_ibss, JOIN_IBSS);
2050         CMD(join_mesh, JOIN_MESH);
2051         CMD(set_pmksa, SET_PMKSA);
2052         CMD(del_pmksa, DEL_PMKSA);
2053         CMD(flush_pmksa, FLUSH_PMKSA);
2054         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
2055                 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
2056         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
2057         CMD(mgmt_tx, FRAME);
2058         CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
2059         if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
2060                 i++;
2061                 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
2062                         goto nla_put_failure;
2063         }
2064         if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
2065             rdev->ops->join_mesh) {
2066                 i++;
2067                 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
2068                         goto nla_put_failure;
2069         }
2070         if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2071                 CMD(tdls_mgmt, TDLS_MGMT);
2072                 CMD(tdls_oper, TDLS_OPER);
2073         }
2074         if (rdev->wiphy.max_sched_scan_reqs)
2075                 CMD(sched_scan_start, START_SCHED_SCAN);
2076         CMD(probe_client, PROBE_CLIENT);
2077         CMD(set_noack_map, SET_NOACK_MAP);
2078         if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
2079                 i++;
2080                 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
2081                         goto nla_put_failure;
2082         }
2083         CMD(start_p2p_device, START_P2P_DEVICE);
2084         CMD(set_mcast_rate, SET_MCAST_RATE);
2085 #ifdef CONFIG_NL80211_TESTMODE
2086         CMD(testmode_cmd, TESTMODE);
2087 #endif
2088
2089         if (rdev->ops->connect || rdev->ops->auth) {
2090                 i++;
2091                 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
2092                         goto nla_put_failure;
2093         }
2094
2095         if (rdev->ops->disconnect || rdev->ops->deauth) {
2096                 i++;
2097                 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
2098                         goto nla_put_failure;
2099         }
2100
2101         return i;
2102  nla_put_failure:
2103         return -ENOBUFS;
2104 }
2105
2106 static int
2107 nl80211_send_pmsr_ftm_capa(const struct cfg80211_pmsr_capabilities *cap,
2108                            struct sk_buff *msg)
2109 {
2110         struct nlattr *ftm;
2111
2112         if (!cap->ftm.supported)
2113                 return 0;
2114
2115         ftm = nla_nest_start_noflag(msg, NL80211_PMSR_TYPE_FTM);
2116         if (!ftm)
2117                 return -ENOBUFS;
2118
2119         if (cap->ftm.asap && nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_ASAP))
2120                 return -ENOBUFS;
2121         if (cap->ftm.non_asap &&
2122             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_ASAP))
2123                 return -ENOBUFS;
2124         if (cap->ftm.request_lci &&
2125             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_LCI))
2126                 return -ENOBUFS;
2127         if (cap->ftm.request_civicloc &&
2128             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_REQ_CIVICLOC))
2129                 return -ENOBUFS;
2130         if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_PREAMBLES,
2131                         cap->ftm.preambles))
2132                 return -ENOBUFS;
2133         if (nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_BANDWIDTHS,
2134                         cap->ftm.bandwidths))
2135                 return -ENOBUFS;
2136         if (cap->ftm.max_bursts_exponent >= 0 &&
2137             nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_BURSTS_EXPONENT,
2138                         cap->ftm.max_bursts_exponent))
2139                 return -ENOBUFS;
2140         if (cap->ftm.max_ftms_per_burst &&
2141             nla_put_u32(msg, NL80211_PMSR_FTM_CAPA_ATTR_MAX_FTMS_PER_BURST,
2142                         cap->ftm.max_ftms_per_burst))
2143                 return -ENOBUFS;
2144         if (cap->ftm.trigger_based &&
2145             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_TRIGGER_BASED))
2146                 return -ENOBUFS;
2147         if (cap->ftm.non_trigger_based &&
2148             nla_put_flag(msg, NL80211_PMSR_FTM_CAPA_ATTR_NON_TRIGGER_BASED))
2149                 return -ENOBUFS;
2150
2151         nla_nest_end(msg, ftm);
2152         return 0;
2153 }
2154
2155 static int nl80211_send_pmsr_capa(struct cfg80211_registered_device *rdev,
2156                                   struct sk_buff *msg)
2157 {
2158         const struct cfg80211_pmsr_capabilities *cap = rdev->wiphy.pmsr_capa;
2159         struct nlattr *pmsr, *caps;
2160
2161         if (!cap)
2162                 return 0;
2163
2164         /*
2165          * we don't need to clean up anything here since the caller
2166          * will genlmsg_cancel() if we fail
2167          */
2168
2169         pmsr = nla_nest_start_noflag(msg, NL80211_ATTR_PEER_MEASUREMENTS);
2170         if (!pmsr)
2171                 return -ENOBUFS;
2172
2173         if (nla_put_u32(msg, NL80211_PMSR_ATTR_MAX_PEERS, cap->max_peers))
2174                 return -ENOBUFS;
2175
2176         if (cap->report_ap_tsf &&
2177             nla_put_flag(msg, NL80211_PMSR_ATTR_REPORT_AP_TSF))
2178                 return -ENOBUFS;
2179
2180         if (cap->randomize_mac_addr &&
2181             nla_put_flag(msg, NL80211_PMSR_ATTR_RANDOMIZE_MAC_ADDR))
2182                 return -ENOBUFS;
2183
2184         caps = nla_nest_start_noflag(msg, NL80211_PMSR_ATTR_TYPE_CAPA);
2185         if (!caps)
2186                 return -ENOBUFS;
2187
2188         if (nl80211_send_pmsr_ftm_capa(cap, msg))
2189                 return -ENOBUFS;
2190
2191         nla_nest_end(msg, caps);
2192         nla_nest_end(msg, pmsr);
2193
2194         return 0;
2195 }
2196
2197 static int
2198 nl80211_put_iftype_akm_suites(struct cfg80211_registered_device *rdev,
2199                               struct sk_buff *msg)
2200 {
2201         int i;
2202         struct nlattr *nested, *nested_akms;
2203         const struct wiphy_iftype_akm_suites *iftype_akms;
2204
2205         if (!rdev->wiphy.num_iftype_akm_suites ||
2206             !rdev->wiphy.iftype_akm_suites)
2207                 return 0;
2208
2209         nested = nla_nest_start(msg, NL80211_ATTR_IFTYPE_AKM_SUITES);
2210         if (!nested)
2211                 return -ENOBUFS;
2212
2213         for (i = 0; i < rdev->wiphy.num_iftype_akm_suites; i++) {
2214                 nested_akms = nla_nest_start(msg, i + 1);
2215                 if (!nested_akms)
2216                         return -ENOBUFS;
2217
2218                 iftype_akms = &rdev->wiphy.iftype_akm_suites[i];
2219
2220                 if (nl80211_put_iftypes(msg, NL80211_IFTYPE_AKM_ATTR_IFTYPES,
2221                                         iftype_akms->iftypes_mask))
2222                         return -ENOBUFS;
2223
2224                 if (nla_put(msg, NL80211_IFTYPE_AKM_ATTR_SUITES,
2225                             sizeof(u32) * iftype_akms->n_akm_suites,
2226                             iftype_akms->akm_suites)) {
2227                         return -ENOBUFS;
2228                 }
2229                 nla_nest_end(msg, nested_akms);
2230         }
2231
2232         nla_nest_end(msg, nested);
2233
2234         return 0;
2235 }
2236
2237 static int
2238 nl80211_put_tid_config_support(struct cfg80211_registered_device *rdev,
2239                                struct sk_buff *msg)
2240 {
2241         struct nlattr *supp;
2242
2243         if (!rdev->wiphy.tid_config_support.vif &&
2244             !rdev->wiphy.tid_config_support.peer)
2245                 return 0;
2246
2247         supp = nla_nest_start(msg, NL80211_ATTR_TID_CONFIG);
2248         if (!supp)
2249                 return -ENOSPC;
2250
2251         if (rdev->wiphy.tid_config_support.vif &&
2252             nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_VIF_SUPP,
2253                               rdev->wiphy.tid_config_support.vif,
2254                               NL80211_TID_CONFIG_ATTR_PAD))
2255                 goto fail;
2256
2257         if (rdev->wiphy.tid_config_support.peer &&
2258             nla_put_u64_64bit(msg, NL80211_TID_CONFIG_ATTR_PEER_SUPP,
2259                               rdev->wiphy.tid_config_support.peer,
2260                               NL80211_TID_CONFIG_ATTR_PAD))
2261                 goto fail;
2262
2263         /* for now we just use the same value ... makes more sense */
2264         if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_SHORT,
2265                        rdev->wiphy.tid_config_support.max_retry))
2266                 goto fail;
2267         if (nla_put_u8(msg, NL80211_TID_CONFIG_ATTR_RETRY_LONG,
2268                        rdev->wiphy.tid_config_support.max_retry))
2269                 goto fail;
2270
2271         nla_nest_end(msg, supp);
2272
2273         return 0;
2274 fail:
2275         nla_nest_cancel(msg, supp);
2276         return -ENOBUFS;
2277 }
2278
2279 static int
2280 nl80211_put_sar_specs(struct cfg80211_registered_device *rdev,
2281                       struct sk_buff *msg)
2282 {
2283         struct nlattr *sar_capa, *specs, *sub_freq_range;
2284         u8 num_freq_ranges;
2285         int i;
2286
2287         if (!rdev->wiphy.sar_capa)
2288                 return 0;
2289
2290         num_freq_ranges = rdev->wiphy.sar_capa->num_freq_ranges;
2291
2292         sar_capa = nla_nest_start(msg, NL80211_ATTR_SAR_SPEC);
2293         if (!sar_capa)
2294                 return -ENOSPC;
2295
2296         if (nla_put_u32(msg, NL80211_SAR_ATTR_TYPE, rdev->wiphy.sar_capa->type))
2297                 goto fail;
2298
2299         specs = nla_nest_start(msg, NL80211_SAR_ATTR_SPECS);
2300         if (!specs)
2301                 goto fail;
2302
2303         /* report supported freq_ranges */
2304         for (i = 0; i < num_freq_ranges; i++) {
2305                 sub_freq_range = nla_nest_start(msg, i + 1);
2306                 if (!sub_freq_range)
2307                         goto fail;
2308
2309                 if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_START_FREQ,
2310                                 rdev->wiphy.sar_capa->freq_ranges[i].start_freq))
2311                         goto fail;
2312
2313                 if (nla_put_u32(msg, NL80211_SAR_ATTR_SPECS_END_FREQ,
2314                                 rdev->wiphy.sar_capa->freq_ranges[i].end_freq))
2315                         goto fail;
2316
2317                 nla_nest_end(msg, sub_freq_range);
2318         }
2319
2320         nla_nest_end(msg, specs);
2321         nla_nest_end(msg, sar_capa);
2322
2323         return 0;
2324 fail:
2325         nla_nest_cancel(msg, sar_capa);
2326         return -ENOBUFS;
2327 }
2328
2329 static int nl80211_put_mbssid_support(struct wiphy *wiphy, struct sk_buff *msg)
2330 {
2331         struct nlattr *config;
2332
2333         if (!wiphy->mbssid_max_interfaces)
2334                 return 0;
2335
2336         config = nla_nest_start(msg, NL80211_ATTR_MBSSID_CONFIG);
2337         if (!config)
2338                 return -ENOBUFS;
2339
2340         if (nla_put_u8(msg, NL80211_MBSSID_CONFIG_ATTR_MAX_INTERFACES,
2341                        wiphy->mbssid_max_interfaces))
2342                 goto fail;
2343
2344         if (wiphy->ema_max_profile_periodicity &&
2345             nla_put_u8(msg,
2346                        NL80211_MBSSID_CONFIG_ATTR_MAX_EMA_PROFILE_PERIODICITY,
2347                        wiphy->ema_max_profile_periodicity))
2348                 goto fail;
2349
2350         nla_nest_end(msg, config);
2351         return 0;
2352
2353 fail:
2354         nla_nest_cancel(msg, config);
2355         return -ENOBUFS;
2356 }
2357
2358 struct nl80211_dump_wiphy_state {
2359         s64 filter_wiphy;
2360         long start;
2361         long split_start, band_start, chan_start, capa_start;
2362         bool split;
2363 };
2364
2365 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
2366                               enum nl80211_commands cmd,
2367                               struct sk_buff *msg, u32 portid, u32 seq,
2368                               int flags, struct nl80211_dump_wiphy_state *state)
2369 {
2370         void *hdr;
2371         struct nlattr *nl_bands, *nl_band;
2372         struct nlattr *nl_freqs, *nl_freq;
2373         struct nlattr *nl_cmds;
2374         enum nl80211_band band;
2375         struct ieee80211_channel *chan;
2376         int i;
2377         const struct ieee80211_txrx_stypes *mgmt_stypes =
2378                                 rdev->wiphy.mgmt_stypes;
2379         u32 features;
2380
2381         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2382         if (!hdr)
2383                 return -ENOBUFS;
2384
2385         if (WARN_ON(!state))
2386                 return -EINVAL;
2387
2388         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2389             nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
2390                            wiphy_name(&rdev->wiphy)) ||
2391             nla_put_u32(msg, NL80211_ATTR_GENERATION,
2392                         cfg80211_rdev_list_generation))
2393                 goto nla_put_failure;
2394
2395         if (cmd != NL80211_CMD_NEW_WIPHY)
2396                 goto finish;
2397
2398         switch (state->split_start) {
2399         case 0:
2400                 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
2401                                rdev->wiphy.retry_short) ||
2402                     nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
2403                                rdev->wiphy.retry_long) ||
2404                     nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
2405                                 rdev->wiphy.frag_threshold) ||
2406                     nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
2407                                 rdev->wiphy.rts_threshold) ||
2408                     nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
2409                                rdev->wiphy.coverage_class) ||
2410                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
2411                                rdev->wiphy.max_scan_ssids) ||
2412                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
2413                                rdev->wiphy.max_sched_scan_ssids) ||
2414                     nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
2415                                 rdev->wiphy.max_scan_ie_len) ||
2416                     nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
2417                                 rdev->wiphy.max_sched_scan_ie_len) ||
2418                     nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
2419                                rdev->wiphy.max_match_sets))
2420                         goto nla_put_failure;
2421
2422                 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
2423                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
2424                         goto nla_put_failure;
2425                 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
2426                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
2427                         goto nla_put_failure;
2428                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
2429                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
2430                         goto nla_put_failure;
2431                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
2432                     nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
2433                         goto nla_put_failure;
2434                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
2435                     nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
2436                         goto nla_put_failure;
2437                 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
2438                     nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
2439                         goto nla_put_failure;
2440                 state->split_start++;
2441                 if (state->split)
2442                         break;
2443                 fallthrough;
2444         case 1:
2445                 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
2446                             sizeof(u32) * rdev->wiphy.n_cipher_suites,
2447                             rdev->wiphy.cipher_suites))
2448                         goto nla_put_failure;
2449
2450                 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
2451                                rdev->wiphy.max_num_pmkids))
2452                         goto nla_put_failure;
2453
2454                 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
2455                     nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
2456                         goto nla_put_failure;
2457
2458                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
2459                                 rdev->wiphy.available_antennas_tx) ||
2460                     nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
2461                                 rdev->wiphy.available_antennas_rx))
2462                         goto nla_put_failure;
2463
2464                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
2465                     nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
2466                                 rdev->wiphy.probe_resp_offload))
2467                         goto nla_put_failure;
2468
2469                 if ((rdev->wiphy.available_antennas_tx ||
2470                      rdev->wiphy.available_antennas_rx) &&
2471                     rdev->ops->get_antenna) {
2472                         u32 tx_ant = 0, rx_ant = 0;
2473                         int res;
2474
2475                         res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
2476                         if (!res) {
2477                                 if (nla_put_u32(msg,
2478                                                 NL80211_ATTR_WIPHY_ANTENNA_TX,
2479                                                 tx_ant) ||
2480                                     nla_put_u32(msg,
2481                                                 NL80211_ATTR_WIPHY_ANTENNA_RX,
2482                                                 rx_ant))
2483                                         goto nla_put_failure;
2484                         }
2485                 }
2486
2487                 state->split_start++;
2488                 if (state->split)
2489                         break;
2490                 fallthrough;
2491         case 2:
2492                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
2493                                         rdev->wiphy.interface_modes))
2494                                 goto nla_put_failure;
2495                 state->split_start++;
2496                 if (state->split)
2497                         break;
2498                 fallthrough;
2499         case 3:
2500                 nl_bands = nla_nest_start_noflag(msg,
2501                                                  NL80211_ATTR_WIPHY_BANDS);
2502                 if (!nl_bands)
2503                         goto nla_put_failure;
2504
2505                 for (band = state->band_start;
2506                      band < (state->split ?
2507                                 NUM_NL80211_BANDS :
2508                                 NL80211_BAND_60GHZ + 1);
2509                      band++) {
2510                         struct ieee80211_supported_band *sband;
2511
2512                         /* omit higher bands for ancient software */
2513                         if (band > NL80211_BAND_5GHZ && !state->split)
2514                                 break;
2515
2516                         sband = rdev->wiphy.bands[band];
2517
2518                         if (!sband)
2519                                 continue;
2520
2521                         nl_band = nla_nest_start_noflag(msg, band);
2522                         if (!nl_band)
2523                                 goto nla_put_failure;
2524
2525                         switch (state->chan_start) {
2526                         case 0:
2527                                 if (nl80211_send_band_rateinfo(msg, sband,
2528                                                                state->split))
2529                                         goto nla_put_failure;
2530                                 state->chan_start++;
2531                                 if (state->split)
2532                                         break;
2533                                 fallthrough;
2534                         default:
2535                                 /* add frequencies */
2536                                 nl_freqs = nla_nest_start_noflag(msg,
2537                                                                  NL80211_BAND_ATTR_FREQS);
2538                                 if (!nl_freqs)
2539                                         goto nla_put_failure;
2540
2541                                 for (i = state->chan_start - 1;
2542                                      i < sband->n_channels;
2543                                      i++) {
2544                                         nl_freq = nla_nest_start_noflag(msg,
2545                                                                         i);
2546                                         if (!nl_freq)
2547                                                 goto nla_put_failure;
2548
2549                                         chan = &sband->channels[i];
2550
2551                                         if (nl80211_msg_put_channel(
2552                                                         msg, &rdev->wiphy, chan,
2553                                                         state->split))
2554                                                 goto nla_put_failure;
2555
2556                                         nla_nest_end(msg, nl_freq);
2557                                         if (state->split)
2558                                                 break;
2559                                 }
2560                                 if (i < sband->n_channels)
2561                                         state->chan_start = i + 2;
2562                                 else
2563                                         state->chan_start = 0;
2564                                 nla_nest_end(msg, nl_freqs);
2565                         }
2566
2567                         nla_nest_end(msg, nl_band);
2568
2569                         if (state->split) {
2570                                 /* start again here */
2571                                 if (state->chan_start)
2572                                         band--;
2573                                 break;
2574                         }
2575                 }
2576                 nla_nest_end(msg, nl_bands);
2577
2578                 if (band < NUM_NL80211_BANDS)
2579                         state->band_start = band + 1;
2580                 else
2581                         state->band_start = 0;
2582
2583                 /* if bands & channels are done, continue outside */
2584                 if (state->band_start == 0 && state->chan_start == 0)
2585                         state->split_start++;
2586                 if (state->split)
2587                         break;
2588                 fallthrough;
2589         case 4:
2590                 nl_cmds = nla_nest_start_noflag(msg,
2591                                                 NL80211_ATTR_SUPPORTED_COMMANDS);
2592                 if (!nl_cmds)
2593                         goto nla_put_failure;
2594
2595                 i = nl80211_add_commands_unsplit(rdev, msg);
2596                 if (i < 0)
2597                         goto nla_put_failure;
2598                 if (state->split) {
2599                         CMD(crit_proto_start, CRIT_PROTOCOL_START);
2600                         CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
2601                         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
2602                                 CMD(channel_switch, CHANNEL_SWITCH);
2603                         CMD(set_qos_map, SET_QOS_MAP);
2604                         if (rdev->wiphy.features &
2605                                         NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
2606                                 CMD(add_tx_ts, ADD_TX_TS);
2607                         CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
2608                         CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
2609                         CMD(update_ft_ies, UPDATE_FT_IES);
2610                         if (rdev->wiphy.sar_capa)
2611                                 CMD(set_sar_specs, SET_SAR_SPECS);
2612                 }
2613 #undef CMD
2614
2615                 nla_nest_end(msg, nl_cmds);
2616                 state->split_start++;
2617                 if (state->split)
2618                         break;
2619                 fallthrough;
2620         case 5:
2621                 if (rdev->ops->remain_on_channel &&
2622                     (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
2623                     nla_put_u32(msg,
2624                                 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
2625                                 rdev->wiphy.max_remain_on_channel_duration))
2626                         goto nla_put_failure;
2627
2628                 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
2629                     nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
2630                         goto nla_put_failure;
2631
2632                 state->split_start++;
2633                 if (state->split)
2634                         break;
2635                 fallthrough;
2636         case 6:
2637 #ifdef CONFIG_PM
2638                 if (nl80211_send_wowlan(msg, rdev, state->split))
2639                         goto nla_put_failure;
2640                 state->split_start++;
2641                 if (state->split)
2642                         break;
2643 #else
2644                 state->split_start++;
2645 #endif
2646                 fallthrough;
2647         case 7:
2648                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
2649                                         rdev->wiphy.software_iftypes))
2650                         goto nla_put_failure;
2651
2652                 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
2653                                                    state->split))
2654                         goto nla_put_failure;
2655
2656                 state->split_start++;
2657                 if (state->split)
2658                         break;
2659                 fallthrough;
2660         case 8:
2661                 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
2662                     nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
2663                                 rdev->wiphy.ap_sme_capa))
2664                         goto nla_put_failure;
2665
2666                 features = rdev->wiphy.features;
2667                 /*
2668                  * We can only add the per-channel limit information if the
2669                  * dump is split, otherwise it makes it too big. Therefore
2670                  * only advertise it in that case.
2671                  */
2672                 if (state->split)
2673                         features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
2674                 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
2675                         goto nla_put_failure;
2676
2677                 if (rdev->wiphy.ht_capa_mod_mask &&
2678                     nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
2679                             sizeof(*rdev->wiphy.ht_capa_mod_mask),
2680                             rdev->wiphy.ht_capa_mod_mask))
2681                         goto nla_put_failure;
2682
2683                 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
2684                     rdev->wiphy.max_acl_mac_addrs &&
2685                     nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
2686                                 rdev->wiphy.max_acl_mac_addrs))
2687                         goto nla_put_failure;
2688
2689                 /*
2690                  * Any information below this point is only available to
2691                  * applications that can deal with it being split. This
2692                  * helps ensure that newly added capabilities don't break
2693                  * older tools by overrunning their buffers.
2694                  *
2695                  * We still increment split_start so that in the split
2696                  * case we'll continue with more data in the next round,
2697                  * but break unconditionally so unsplit data stops here.
2698                  */
2699                 if (state->split)
2700                         state->split_start++;
2701                 else
2702                         state->split_start = 0;
2703                 break;
2704         case 9:
2705                 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
2706                         goto nla_put_failure;
2707
2708                 if (nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
2709                                 rdev->wiphy.max_sched_scan_plans) ||
2710                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
2711                                 rdev->wiphy.max_sched_scan_plan_interval) ||
2712                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
2713                                 rdev->wiphy.max_sched_scan_plan_iterations))
2714                         goto nla_put_failure;
2715
2716                 if (rdev->wiphy.extended_capabilities &&
2717                     (nla_put(msg, NL80211_ATTR_EXT_CAPA,
2718                              rdev->wiphy.extended_capabilities_len,
2719                              rdev->wiphy.extended_capabilities) ||
2720                      nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2721                              rdev->wiphy.extended_capabilities_len,
2722                              rdev->wiphy.extended_capabilities_mask)))
2723                         goto nla_put_failure;
2724
2725                 if (rdev->wiphy.vht_capa_mod_mask &&
2726                     nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
2727                             sizeof(*rdev->wiphy.vht_capa_mod_mask),
2728                             rdev->wiphy.vht_capa_mod_mask))
2729                         goto nla_put_failure;
2730
2731                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
2732                             rdev->wiphy.perm_addr))
2733                         goto nla_put_failure;
2734
2735                 if (!is_zero_ether_addr(rdev->wiphy.addr_mask) &&
2736                     nla_put(msg, NL80211_ATTR_MAC_MASK, ETH_ALEN,
2737                             rdev->wiphy.addr_mask))
2738                         goto nla_put_failure;
2739
2740                 if (rdev->wiphy.n_addresses > 1) {
2741                         void *attr;
2742
2743                         attr = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS);
2744                         if (!attr)
2745                                 goto nla_put_failure;
2746
2747                         for (i = 0; i < rdev->wiphy.n_addresses; i++)
2748                                 if (nla_put(msg, i + 1, ETH_ALEN,
2749                                             rdev->wiphy.addresses[i].addr))
2750                                         goto nla_put_failure;
2751
2752                         nla_nest_end(msg, attr);
2753                 }
2754
2755                 state->split_start++;
2756                 break;
2757         case 10:
2758                 if (nl80211_send_coalesce(msg, rdev))
2759                         goto nla_put_failure;
2760
2761                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
2762                     (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
2763                      nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
2764                         goto nla_put_failure;
2765
2766                 if (rdev->wiphy.max_ap_assoc_sta &&
2767                     nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
2768                                 rdev->wiphy.max_ap_assoc_sta))
2769                         goto nla_put_failure;
2770
2771                 state->split_start++;
2772                 break;
2773         case 11:
2774                 if (rdev->wiphy.n_vendor_commands) {
2775                         const struct nl80211_vendor_cmd_info *info;
2776                         struct nlattr *nested;
2777
2778                         nested = nla_nest_start_noflag(msg,
2779                                                        NL80211_ATTR_VENDOR_DATA);
2780                         if (!nested)
2781                                 goto nla_put_failure;
2782
2783                         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
2784                                 info = &rdev->wiphy.vendor_commands[i].info;
2785                                 if (nla_put(msg, i + 1, sizeof(*info), info))
2786                                         goto nla_put_failure;
2787                         }
2788                         nla_nest_end(msg, nested);
2789                 }
2790
2791                 if (rdev->wiphy.n_vendor_events) {
2792                         const struct nl80211_vendor_cmd_info *info;
2793                         struct nlattr *nested;
2794
2795                         nested = nla_nest_start_noflag(msg,
2796                                                        NL80211_ATTR_VENDOR_EVENTS);
2797                         if (!nested)
2798                                 goto nla_put_failure;
2799
2800                         for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
2801                                 info = &rdev->wiphy.vendor_events[i];
2802                                 if (nla_put(msg, i + 1, sizeof(*info), info))
2803                                         goto nla_put_failure;
2804                         }
2805                         nla_nest_end(msg, nested);
2806                 }
2807                 state->split_start++;
2808                 break;
2809         case 12:
2810                 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
2811                     nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
2812                                rdev->wiphy.max_num_csa_counters))
2813                         goto nla_put_failure;
2814
2815                 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
2816                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
2817                         goto nla_put_failure;
2818
2819                 if (rdev->wiphy.max_sched_scan_reqs &&
2820                     nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
2821                                 rdev->wiphy.max_sched_scan_reqs))
2822                         goto nla_put_failure;
2823
2824                 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
2825                             sizeof(rdev->wiphy.ext_features),
2826                             rdev->wiphy.ext_features))
2827                         goto nla_put_failure;
2828
2829                 if (rdev->wiphy.bss_select_support) {
2830                         struct nlattr *nested;
2831                         u32 bss_select_support = rdev->wiphy.bss_select_support;
2832
2833                         nested = nla_nest_start_noflag(msg,
2834                                                        NL80211_ATTR_BSS_SELECT);
2835                         if (!nested)
2836                                 goto nla_put_failure;
2837
2838                         i = 0;
2839                         while (bss_select_support) {
2840                                 if ((bss_select_support & 1) &&
2841                                     nla_put_flag(msg, i))
2842                                         goto nla_put_failure;
2843                                 i++;
2844                                 bss_select_support >>= 1;
2845                         }
2846                         nla_nest_end(msg, nested);
2847                 }
2848
2849                 state->split_start++;
2850                 break;
2851         case 13:
2852                 if (rdev->wiphy.num_iftype_ext_capab &&
2853                     rdev->wiphy.iftype_ext_capab) {
2854                         struct nlattr *nested_ext_capab, *nested;
2855
2856                         nested = nla_nest_start_noflag(msg,
2857                                                        NL80211_ATTR_IFTYPE_EXT_CAPA);
2858                         if (!nested)
2859                                 goto nla_put_failure;
2860
2861                         for (i = state->capa_start;
2862                              i < rdev->wiphy.num_iftype_ext_capab; i++) {
2863                                 const struct wiphy_iftype_ext_capab *capab;
2864
2865                                 capab = &rdev->wiphy.iftype_ext_capab[i];
2866
2867                                 nested_ext_capab = nla_nest_start_noflag(msg,
2868                                                                          i);
2869                                 if (!nested_ext_capab ||
2870                                     nla_put_u32(msg, NL80211_ATTR_IFTYPE,
2871                                                 capab->iftype) ||
2872                                     nla_put(msg, NL80211_ATTR_EXT_CAPA,
2873                                             capab->extended_capabilities_len,
2874                                             capab->extended_capabilities) ||
2875                                     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
2876                                             capab->extended_capabilities_len,
2877                                             capab->extended_capabilities_mask))
2878                                         goto nla_put_failure;
2879
2880                                 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO &&
2881                                     (nla_put_u16(msg,
2882                                                  NL80211_ATTR_EML_CAPABILITY,
2883                                                  capab->eml_capabilities) ||
2884                                      nla_put_u16(msg,
2885                                                  NL80211_ATTR_MLD_CAPA_AND_OPS,
2886                                                  capab->mld_capa_and_ops)))
2887                                         goto nla_put_failure;
2888
2889                                 nla_nest_end(msg, nested_ext_capab);
2890                                 if (state->split)
2891                                         break;
2892                         }
2893                         nla_nest_end(msg, nested);
2894                         if (i < rdev->wiphy.num_iftype_ext_capab) {
2895                                 state->capa_start = i + 1;
2896                                 break;
2897                         }
2898                 }
2899
2900                 if (nla_put_u32(msg, NL80211_ATTR_BANDS,
2901                                 rdev->wiphy.nan_supported_bands))
2902                         goto nla_put_failure;
2903
2904                 if (wiphy_ext_feature_isset(&rdev->wiphy,
2905                                             NL80211_EXT_FEATURE_TXQS)) {
2906                         struct cfg80211_txq_stats txqstats = {};
2907                         int res;
2908
2909                         res = rdev_get_txq_stats(rdev, NULL, &txqstats);
2910                         if (!res &&
2911                             !nl80211_put_txq_stats(msg, &txqstats,
2912                                                    NL80211_ATTR_TXQ_STATS))
2913                                 goto nla_put_failure;
2914
2915                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_LIMIT,
2916                                         rdev->wiphy.txq_limit))
2917                                 goto nla_put_failure;
2918                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_MEMORY_LIMIT,
2919                                         rdev->wiphy.txq_memory_limit))
2920                                 goto nla_put_failure;
2921                         if (nla_put_u32(msg, NL80211_ATTR_TXQ_QUANTUM,
2922                                         rdev->wiphy.txq_quantum))
2923                                 goto nla_put_failure;
2924                 }
2925
2926                 state->split_start++;
2927                 break;
2928         case 14:
2929                 if (nl80211_send_pmsr_capa(rdev, msg))
2930                         goto nla_put_failure;
2931
2932                 state->split_start++;
2933                 break;
2934         case 15:
2935                 if (rdev->wiphy.akm_suites &&
2936                     nla_put(msg, NL80211_ATTR_AKM_SUITES,
2937                             sizeof(u32) * rdev->wiphy.n_akm_suites,
2938                             rdev->wiphy.akm_suites))
2939                         goto nla_put_failure;
2940
2941                 if (nl80211_put_iftype_akm_suites(rdev, msg))
2942                         goto nla_put_failure;
2943
2944                 if (nl80211_put_tid_config_support(rdev, msg))
2945                         goto nla_put_failure;
2946                 state->split_start++;
2947                 break;
2948         case 16:
2949                 if (nl80211_put_sar_specs(rdev, msg))
2950                         goto nla_put_failure;
2951
2952                 if (nl80211_put_mbssid_support(&rdev->wiphy, msg))
2953                         goto nla_put_failure;
2954
2955                 if (nla_put_u16(msg, NL80211_ATTR_MAX_NUM_AKM_SUITES,
2956                                 rdev->wiphy.max_num_akm_suites))
2957                         goto nla_put_failure;
2958
2959                 if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO)
2960                         nla_put_flag(msg, NL80211_ATTR_MLO_SUPPORT);
2961
2962                 /* done */
2963                 state->split_start = 0;
2964                 break;
2965         }
2966  finish:
2967         genlmsg_end(msg, hdr);
2968         return 0;
2969
2970  nla_put_failure:
2971         genlmsg_cancel(msg, hdr);
2972         return -EMSGSIZE;
2973 }
2974
2975 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
2976                                     struct netlink_callback *cb,
2977                                     struct nl80211_dump_wiphy_state *state)
2978 {
2979         struct nlattr **tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
2980         int ret;
2981
2982         if (!tb)
2983                 return -ENOMEM;
2984
2985         ret = nlmsg_parse_deprecated(cb->nlh,
2986                                      GENL_HDRLEN + nl80211_fam.hdrsize,
2987                                      tb, nl80211_fam.maxattr,
2988                                      nl80211_policy, NULL);
2989         /* ignore parse errors for backward compatibility */
2990         if (ret) {
2991                 ret = 0;
2992                 goto out;
2993         }
2994
2995         state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
2996         if (tb[NL80211_ATTR_WIPHY])
2997                 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
2998         if (tb[NL80211_ATTR_WDEV])
2999                 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
3000         if (tb[NL80211_ATTR_IFINDEX]) {
3001                 struct net_device *netdev;
3002                 struct cfg80211_registered_device *rdev;
3003                 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
3004
3005                 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
3006                 if (!netdev) {
3007                         ret = -ENODEV;
3008                         goto out;
3009                 }
3010                 if (netdev->ieee80211_ptr) {
3011                         rdev = wiphy_to_rdev(
3012                                 netdev->ieee80211_ptr->wiphy);
3013                         state->filter_wiphy = rdev->wiphy_idx;
3014                 }
3015         }
3016
3017         ret = 0;
3018 out:
3019         kfree(tb);
3020         return ret;
3021 }
3022
3023 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
3024 {
3025         int idx = 0, ret;
3026         struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
3027         struct cfg80211_registered_device *rdev;
3028
3029         rtnl_lock();
3030         if (!state) {
3031                 state = kzalloc(sizeof(*state), GFP_KERNEL);
3032                 if (!state) {
3033                         rtnl_unlock();
3034                         return -ENOMEM;
3035                 }
3036                 state->filter_wiphy = -1;
3037                 ret = nl80211_dump_wiphy_parse(skb, cb, state);
3038                 if (ret) {
3039                         kfree(state);
3040                         rtnl_unlock();
3041                         return ret;
3042                 }
3043                 cb->args[0] = (long)state;
3044         }
3045
3046         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3047                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3048                         continue;
3049                 if (++idx <= state->start)
3050                         continue;
3051                 if (state->filter_wiphy != -1 &&
3052                     state->filter_wiphy != rdev->wiphy_idx)
3053                         continue;
3054                 /* attempt to fit multiple wiphy data chunks into the skb */
3055                 do {
3056                         ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
3057                                                  skb,
3058                                                  NETLINK_CB(cb->skb).portid,
3059                                                  cb->nlh->nlmsg_seq,
3060                                                  NLM_F_MULTI, state);
3061                         if (ret < 0) {
3062                                 /*
3063                                  * If sending the wiphy data didn't fit (ENOBUFS
3064                                  * or EMSGSIZE returned), this SKB is still
3065                                  * empty (so it's not too big because another
3066                                  * wiphy dataset is already in the skb) and
3067                                  * we've not tried to adjust the dump allocation
3068                                  * yet ... then adjust the alloc size to be
3069                                  * bigger, and return 1 but with the empty skb.
3070                                  * This results in an empty message being RX'ed
3071                                  * in userspace, but that is ignored.
3072                                  *
3073                                  * We can then retry with the larger buffer.
3074                                  */
3075                                 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
3076                                     !skb->len && !state->split &&
3077                                     cb->min_dump_alloc < 4096) {
3078                                         cb->min_dump_alloc = 4096;
3079                                         state->split_start = 0;
3080                                         rtnl_unlock();
3081                                         return 1;
3082                                 }
3083                                 idx--;
3084                                 break;
3085                         }
3086                 } while (state->split_start > 0);
3087                 break;
3088         }
3089         rtnl_unlock();
3090
3091         state->start = idx;
3092
3093         return skb->len;
3094 }
3095
3096 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
3097 {
3098         kfree((void *)cb->args[0]);
3099         return 0;
3100 }
3101
3102 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
3103 {
3104         struct sk_buff *msg;
3105         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3106         struct nl80211_dump_wiphy_state state = {};
3107
3108         msg = nlmsg_new(4096, GFP_KERNEL);
3109         if (!msg)
3110                 return -ENOMEM;
3111
3112         if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
3113                                info->snd_portid, info->snd_seq, 0,
3114                                &state) < 0) {
3115                 nlmsg_free(msg);
3116                 return -ENOBUFS;
3117         }
3118
3119         return genlmsg_reply(msg, info);
3120 }
3121
3122 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
3123         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
3124         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
3125         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
3126         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
3127         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
3128 };
3129
3130 static int parse_txq_params(struct nlattr *tb[],
3131                             struct ieee80211_txq_params *txq_params)
3132 {
3133         u8 ac;
3134
3135         if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
3136             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
3137             !tb[NL80211_TXQ_ATTR_AIFS])
3138                 return -EINVAL;
3139
3140         ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
3141         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
3142         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
3143         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
3144         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
3145
3146         if (ac >= NL80211_NUM_ACS)
3147                 return -EINVAL;
3148         txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
3149         return 0;
3150 }
3151
3152 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
3153 {
3154         /*
3155          * You can only set the channel explicitly for some interfaces,
3156          * most have their channel managed via their respective
3157          * "establish a connection" command (connect, join, ...)
3158          *
3159          * For AP/GO and mesh mode, the channel can be set with the
3160          * channel userspace API, but is only stored and passed to the
3161          * low-level driver when the AP starts or the mesh is joined.
3162          * This is for backward compatibility, userspace can also give
3163          * the channel in the start-ap or join-mesh commands instead.
3164          *
3165          * Monitors are special as they are normally slaved to
3166          * whatever else is going on, so they have their own special
3167          * operation to set the monitor channel if possible.
3168          */
3169         return !wdev ||
3170                 wdev->iftype == NL80211_IFTYPE_AP ||
3171                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
3172                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
3173                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
3174 }
3175
3176 int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
3177                           struct genl_info *info,
3178                           struct cfg80211_chan_def *chandef)
3179 {
3180         struct netlink_ext_ack *extack = info->extack;
3181         struct nlattr **attrs = info->attrs;
3182         u32 control_freq;
3183
3184         if (!attrs[NL80211_ATTR_WIPHY_FREQ])
3185                 return -EINVAL;
3186
3187         control_freq = MHZ_TO_KHZ(
3188                         nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3189         if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
3190                 control_freq +=
3191                     nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
3192
3193         memset(chandef, 0, sizeof(*chandef));
3194         chandef->chan = ieee80211_get_channel_khz(&rdev->wiphy, control_freq);
3195         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
3196         chandef->center_freq1 = KHZ_TO_MHZ(control_freq);
3197         chandef->freq1_offset = control_freq % 1000;
3198         chandef->center_freq2 = 0;
3199
3200         /* Primary channel not allowed */
3201         if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED) {
3202                 NL_SET_ERR_MSG_ATTR(extack, attrs[NL80211_ATTR_WIPHY_FREQ],
3203                                     "Channel is disabled");
3204                 return -EINVAL;
3205         }
3206
3207         if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
3208                 enum nl80211_channel_type chantype;
3209
3210                 chantype = nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
3211
3212                 switch (chantype) {
3213                 case NL80211_CHAN_NO_HT:
3214                 case NL80211_CHAN_HT20:
3215                 case NL80211_CHAN_HT40PLUS:
3216                 case NL80211_CHAN_HT40MINUS:
3217                         cfg80211_chandef_create(chandef, chandef->chan,
3218                                                 chantype);
3219                         /* user input for center_freq is incorrect */
3220                         if (attrs[NL80211_ATTR_CENTER_FREQ1] &&
3221                             chandef->center_freq1 != nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1])) {
3222                                 NL_SET_ERR_MSG_ATTR(extack,
3223                                                     attrs[NL80211_ATTR_CENTER_FREQ1],
3224                                                     "bad center frequency 1");
3225                                 return -EINVAL;
3226                         }
3227                         /* center_freq2 must be zero */
3228                         if (attrs[NL80211_ATTR_CENTER_FREQ2] &&
3229                             nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2])) {
3230                                 NL_SET_ERR_MSG_ATTR(extack,
3231                                                     attrs[NL80211_ATTR_CENTER_FREQ2],
3232                                                     "center frequency 2 can't be used");
3233                                 return -EINVAL;
3234                         }
3235                         break;
3236                 default:
3237                         NL_SET_ERR_MSG_ATTR(extack,
3238                                             attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE],
3239                                             "invalid channel type");
3240                         return -EINVAL;
3241                 }
3242         } else if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
3243                 chandef->width =
3244                         nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH]);
3245                 if (chandef->chan->band == NL80211_BAND_S1GHZ) {
3246                         /* User input error for channel width doesn't match channel  */
3247                         if (chandef->width != ieee80211_s1g_channel_width(chandef->chan)) {
3248                                 NL_SET_ERR_MSG_ATTR(extack,
3249                                                     attrs[NL80211_ATTR_CHANNEL_WIDTH],
3250                                                     "bad channel width");
3251                                 return -EINVAL;
3252                         }
3253                 }
3254                 if (attrs[NL80211_ATTR_CENTER_FREQ1]) {
3255                         chandef->center_freq1 =
3256                                 nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]);
3257                         if (attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET])
3258                                 chandef->freq1_offset = nla_get_u32(
3259                                       attrs[NL80211_ATTR_CENTER_FREQ1_OFFSET]);
3260                         else
3261                                 chandef->freq1_offset = 0;
3262                 }
3263                 if (attrs[NL80211_ATTR_CENTER_FREQ2])
3264                         chandef->center_freq2 =
3265                                 nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]);
3266         }
3267
3268         if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
3269                 chandef->edmg.channels =
3270                       nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
3271
3272                 if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
3273                         chandef->edmg.bw_config =
3274                      nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
3275         } else {
3276                 chandef->edmg.bw_config = 0;
3277                 chandef->edmg.channels = 0;
3278         }
3279
3280         if (!cfg80211_chandef_valid(chandef)) {
3281                 NL_SET_ERR_MSG(extack, "invalid channel definition");
3282                 return -EINVAL;
3283         }
3284
3285         if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
3286                                      IEEE80211_CHAN_DISABLED)) {
3287                 NL_SET_ERR_MSG(extack, "(extension) channel is disabled");
3288                 return -EINVAL;
3289         }
3290
3291         if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
3292              chandef->width == NL80211_CHAN_WIDTH_10) &&
3293             !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ)) {
3294                 NL_SET_ERR_MSG(extack, "5/10 MHz not supported");
3295                 return -EINVAL;
3296         }
3297
3298         return 0;
3299 }
3300
3301 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
3302                                  struct net_device *dev,
3303                                  struct genl_info *info,
3304                                  int _link_id)
3305 {
3306         struct cfg80211_chan_def chandef;
3307         int result;
3308         enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
3309         struct wireless_dev *wdev = NULL;
3310         int link_id = _link_id;
3311
3312         if (dev)
3313                 wdev = dev->ieee80211_ptr;
3314         if (!nl80211_can_set_dev_channel(wdev))
3315                 return -EOPNOTSUPP;
3316         if (wdev)
3317                 iftype = wdev->iftype;
3318
3319         if (link_id < 0) {
3320                 if (wdev && wdev->valid_links)
3321                         return -EINVAL;
3322                 link_id = 0;
3323         }
3324
3325         result = nl80211_parse_chandef(rdev, info, &chandef);
3326         if (result)
3327                 return result;
3328
3329         switch (iftype) {
3330         case NL80211_IFTYPE_AP:
3331         case NL80211_IFTYPE_P2P_GO:
3332                 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
3333                                                    iftype))
3334                         return -EINVAL;
3335                 if (wdev->links[link_id].ap.beacon_interval) {
3336                         struct ieee80211_channel *cur_chan;
3337
3338                         if (!dev || !rdev->ops->set_ap_chanwidth ||
3339                             !(rdev->wiphy.features &
3340                               NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE))
3341                                 return -EBUSY;
3342
3343                         /* Only allow dynamic channel width changes */
3344                         cur_chan = wdev->links[link_id].ap.chandef.chan;
3345                         if (chandef.chan != cur_chan)
3346                                 return -EBUSY;
3347
3348                         result = rdev_set_ap_chanwidth(rdev, dev, link_id,
3349                                                        &chandef);
3350                         if (result)
3351                                 return result;
3352                         wdev->links[link_id].ap.chandef = chandef;
3353                 } else {
3354                         wdev->u.ap.preset_chandef = chandef;
3355                 }
3356                 return 0;
3357         case NL80211_IFTYPE_MESH_POINT:
3358                 return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
3359         case NL80211_IFTYPE_MONITOR:
3360                 return cfg80211_set_monitor_channel(rdev, &chandef);
3361         default:
3362                 break;
3363         }
3364
3365         return -EINVAL;
3366 }
3367
3368 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
3369 {
3370         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3371         int link_id = nl80211_link_id_or_invalid(info->attrs);
3372         struct net_device *netdev = info->user_ptr[1];
3373         int ret;
3374
3375         wdev_lock(netdev->ieee80211_ptr);
3376         ret = __nl80211_set_channel(rdev, netdev, info, link_id);
3377         wdev_unlock(netdev->ieee80211_ptr);
3378
3379         return ret;
3380 }
3381
3382 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
3383 {
3384         struct cfg80211_registered_device *rdev = NULL;
3385         struct net_device *netdev = NULL;
3386         struct wireless_dev *wdev;
3387         int result = 0, rem_txq_params = 0;
3388         struct nlattr *nl_txq_params;
3389         u32 changed;
3390         u8 retry_short = 0, retry_long = 0;
3391         u32 frag_threshold = 0, rts_threshold = 0;
3392         u8 coverage_class = 0;
3393         u32 txq_limit = 0, txq_memory_limit = 0, txq_quantum = 0;
3394
3395         rtnl_lock();
3396         /*
3397          * Try to find the wiphy and netdev. Normally this
3398          * function shouldn't need the netdev, but this is
3399          * done for backward compatibility -- previously
3400          * setting the channel was done per wiphy, but now
3401          * it is per netdev. Previous userland like hostapd
3402          * also passed a netdev to set_wiphy, so that it is
3403          * possible to let that go to the right netdev!
3404          */
3405
3406         if (info->attrs[NL80211_ATTR_IFINDEX]) {
3407                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
3408
3409                 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
3410                 if (netdev && netdev->ieee80211_ptr)
3411                         rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
3412                 else
3413                         netdev = NULL;
3414         }
3415
3416         if (!netdev) {
3417                 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
3418                                                   info->attrs);
3419                 if (IS_ERR(rdev)) {
3420                         rtnl_unlock();
3421                         return PTR_ERR(rdev);
3422                 }
3423                 wdev = NULL;
3424                 netdev = NULL;
3425                 result = 0;
3426         } else
3427                 wdev = netdev->ieee80211_ptr;
3428
3429         wiphy_lock(&rdev->wiphy);
3430
3431         /*
3432          * end workaround code, by now the rdev is available
3433          * and locked, and wdev may or may not be NULL.
3434          */
3435
3436         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
3437                 result = cfg80211_dev_rename(
3438                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
3439         rtnl_unlock();
3440
3441         if (result)
3442                 goto out;
3443
3444         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
3445                 struct ieee80211_txq_params txq_params;
3446                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
3447
3448                 if (!rdev->ops->set_txq_params) {
3449                         result = -EOPNOTSUPP;
3450                         goto out;
3451                 }
3452
3453                 if (!netdev) {
3454                         result = -EINVAL;
3455                         goto out;
3456                 }
3457
3458                 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3459                     netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3460                         result = -EINVAL;
3461                         goto out;
3462                 }
3463
3464                 if (!netif_running(netdev)) {
3465                         result = -ENETDOWN;
3466                         goto out;
3467                 }
3468
3469                 nla_for_each_nested(nl_txq_params,
3470                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
3471                                     rem_txq_params) {
3472                         result = nla_parse_nested_deprecated(tb,
3473                                                              NL80211_TXQ_ATTR_MAX,
3474                                                              nl_txq_params,
3475                                                              txq_params_policy,
3476                                                              info->extack);
3477                         if (result)
3478                                 goto out;
3479                         result = parse_txq_params(tb, &txq_params);
3480                         if (result)
3481                                 goto out;
3482
3483                         txq_params.link_id =
3484                                 nl80211_link_id_or_invalid(info->attrs);
3485
3486                         wdev_lock(netdev->ieee80211_ptr);
3487                         if (txq_params.link_id >= 0 &&
3488                             !(netdev->ieee80211_ptr->valid_links &
3489                               BIT(txq_params.link_id)))
3490                                 result = -ENOLINK;
3491                         else if (txq_params.link_id >= 0 &&
3492                                  !netdev->ieee80211_ptr->valid_links)
3493                                 result = -EINVAL;
3494                         else
3495                                 result = rdev_set_txq_params(rdev, netdev,
3496                                                              &txq_params);
3497                         wdev_unlock(netdev->ieee80211_ptr);
3498                         if (result)
3499                                 goto out;
3500                 }
3501         }
3502
3503         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3504                 int link_id = nl80211_link_id_or_invalid(info->attrs);
3505
3506                 if (wdev) {
3507                         wdev_lock(wdev);
3508                         result = __nl80211_set_channel(
3509                                 rdev,
3510                                 nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
3511                                 info, link_id);
3512                         wdev_unlock(wdev);
3513                 } else {
3514                         result = __nl80211_set_channel(rdev, netdev, info, link_id);
3515                 }
3516
3517                 if (result)
3518                         goto out;
3519         }
3520
3521         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
3522                 struct wireless_dev *txp_wdev = wdev;
3523                 enum nl80211_tx_power_setting type;
3524                 int idx, mbm = 0;
3525
3526                 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
3527                         txp_wdev = NULL;
3528
3529                 if (!rdev->ops->set_tx_power) {
3530                         result = -EOPNOTSUPP;
3531                         goto out;
3532                 }
3533
3534                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
3535                 type = nla_get_u32(info->attrs[idx]);
3536
3537                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
3538                     (type != NL80211_TX_POWER_AUTOMATIC)) {
3539                         result = -EINVAL;
3540                         goto out;
3541                 }
3542
3543                 if (type != NL80211_TX_POWER_AUTOMATIC) {
3544                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
3545                         mbm = nla_get_u32(info->attrs[idx]);
3546                 }
3547
3548                 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
3549                 if (result)
3550                         goto out;
3551         }
3552
3553         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
3554             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
3555                 u32 tx_ant, rx_ant;
3556
3557                 if ((!rdev->wiphy.available_antennas_tx &&
3558                      !rdev->wiphy.available_antennas_rx) ||
3559                     !rdev->ops->set_antenna) {
3560                         result = -EOPNOTSUPP;
3561                         goto out;
3562                 }
3563
3564                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
3565                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
3566
3567                 /* reject antenna configurations which don't match the
3568                  * available antenna masks, except for the "all" mask */
3569                 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
3570                     (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
3571                         result = -EINVAL;
3572                         goto out;
3573                 }
3574
3575                 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
3576                 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
3577
3578                 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
3579                 if (result)
3580                         goto out;
3581         }
3582
3583         changed = 0;
3584
3585         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
3586                 retry_short = nla_get_u8(
3587                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
3588
3589                 changed |= WIPHY_PARAM_RETRY_SHORT;
3590         }
3591
3592         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
3593                 retry_long = nla_get_u8(
3594                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
3595
3596                 changed |= WIPHY_PARAM_RETRY_LONG;
3597         }
3598
3599         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
3600                 frag_threshold = nla_get_u32(
3601                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
3602                 if (frag_threshold < 256) {
3603                         result = -EINVAL;
3604                         goto out;
3605                 }
3606
3607                 if (frag_threshold != (u32) -1) {
3608                         /*
3609                          * Fragments (apart from the last one) are required to
3610                          * have even length. Make the fragmentation code
3611                          * simpler by stripping LSB should someone try to use
3612                          * odd threshold value.
3613                          */
3614                         frag_threshold &= ~0x1;
3615                 }
3616                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
3617         }
3618
3619         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
3620                 rts_threshold = nla_get_u32(
3621                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
3622                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
3623         }
3624
3625         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
3626                 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3627                         result = -EINVAL;
3628                         goto out;
3629                 }
3630
3631                 coverage_class = nla_get_u8(
3632                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
3633                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
3634         }
3635
3636         if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
3637                 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION)) {
3638                         result = -EOPNOTSUPP;
3639                         goto out;
3640                 }
3641
3642                 changed |= WIPHY_PARAM_DYN_ACK;
3643         }
3644
3645         if (info->attrs[NL80211_ATTR_TXQ_LIMIT]) {
3646                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3647                                              NL80211_EXT_FEATURE_TXQS)) {
3648                         result = -EOPNOTSUPP;
3649                         goto out;
3650                 }
3651                 txq_limit = nla_get_u32(
3652                         info->attrs[NL80211_ATTR_TXQ_LIMIT]);
3653                 changed |= WIPHY_PARAM_TXQ_LIMIT;
3654         }
3655
3656         if (info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]) {
3657                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3658                                              NL80211_EXT_FEATURE_TXQS)) {
3659                         result = -EOPNOTSUPP;
3660                         goto out;
3661                 }
3662                 txq_memory_limit = nla_get_u32(
3663                         info->attrs[NL80211_ATTR_TXQ_MEMORY_LIMIT]);
3664                 changed |= WIPHY_PARAM_TXQ_MEMORY_LIMIT;
3665         }
3666
3667         if (info->attrs[NL80211_ATTR_TXQ_QUANTUM]) {
3668                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3669                                              NL80211_EXT_FEATURE_TXQS)) {
3670                         result = -EOPNOTSUPP;
3671                         goto out;
3672                 }
3673                 txq_quantum = nla_get_u32(
3674                         info->attrs[NL80211_ATTR_TXQ_QUANTUM]);
3675                 changed |= WIPHY_PARAM_TXQ_QUANTUM;
3676         }
3677
3678         if (changed) {
3679                 u8 old_retry_short, old_retry_long;
3680                 u32 old_frag_threshold, old_rts_threshold;
3681                 u8 old_coverage_class;
3682                 u32 old_txq_limit, old_txq_memory_limit, old_txq_quantum;
3683
3684                 if (!rdev->ops->set_wiphy_params) {
3685                         result = -EOPNOTSUPP;
3686                         goto out;
3687                 }
3688
3689                 old_retry_short = rdev->wiphy.retry_short;
3690                 old_retry_long = rdev->wiphy.retry_long;
3691                 old_frag_threshold = rdev->wiphy.frag_threshold;
3692                 old_rts_threshold = rdev->wiphy.rts_threshold;
3693                 old_coverage_class = rdev->wiphy.coverage_class;
3694                 old_txq_limit = rdev->wiphy.txq_limit;
3695                 old_txq_memory_limit = rdev->wiphy.txq_memory_limit;
3696                 old_txq_quantum = rdev->wiphy.txq_quantum;
3697
3698                 if (changed & WIPHY_PARAM_RETRY_SHORT)
3699                         rdev->wiphy.retry_short = retry_short;
3700                 if (changed & WIPHY_PARAM_RETRY_LONG)
3701                         rdev->wiphy.retry_long = retry_long;
3702                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
3703                         rdev->wiphy.frag_threshold = frag_threshold;
3704                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
3705                         rdev->wiphy.rts_threshold = rts_threshold;
3706                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
3707                         rdev->wiphy.coverage_class = coverage_class;
3708                 if (changed & WIPHY_PARAM_TXQ_LIMIT)
3709                         rdev->wiphy.txq_limit = txq_limit;
3710                 if (changed & WIPHY_PARAM_TXQ_MEMORY_LIMIT)
3711                         rdev->wiphy.txq_memory_limit = txq_memory_limit;
3712                 if (changed & WIPHY_PARAM_TXQ_QUANTUM)
3713                         rdev->wiphy.txq_quantum = txq_quantum;
3714
3715                 result = rdev_set_wiphy_params(rdev, changed);
3716                 if (result) {
3717                         rdev->wiphy.retry_short = old_retry_short;
3718                         rdev->wiphy.retry_long = old_retry_long;
3719                         rdev->wiphy.frag_threshold = old_frag_threshold;
3720                         rdev->wiphy.rts_threshold = old_rts_threshold;
3721                         rdev->wiphy.coverage_class = old_coverage_class;
3722                         rdev->wiphy.txq_limit = old_txq_limit;
3723                         rdev->wiphy.txq_memory_limit = old_txq_memory_limit;
3724                         rdev->wiphy.txq_quantum = old_txq_quantum;
3725                         goto out;
3726                 }
3727         }
3728
3729         result = 0;
3730
3731 out:
3732         wiphy_unlock(&rdev->wiphy);
3733         return result;
3734 }
3735
3736 static int nl80211_send_chandef(struct sk_buff *msg,
3737                                 const struct cfg80211_chan_def *chandef)
3738 {
3739         if (WARN_ON(!cfg80211_chandef_valid(chandef)))
3740                 return -EINVAL;
3741
3742         if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
3743                         chandef->chan->center_freq))
3744                 return -ENOBUFS;
3745         if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
3746                         chandef->chan->freq_offset))
3747                 return -ENOBUFS;
3748         switch (chandef->width) {
3749         case NL80211_CHAN_WIDTH_20_NOHT:
3750         case NL80211_CHAN_WIDTH_20:
3751         case NL80211_CHAN_WIDTH_40:
3752                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
3753                                 cfg80211_get_chandef_type(chandef)))
3754                         return -ENOBUFS;
3755                 break;
3756         default:
3757                 break;
3758         }
3759         if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
3760                 return -ENOBUFS;
3761         if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
3762                 return -ENOBUFS;
3763         if (chandef->center_freq2 &&
3764             nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
3765                 return -ENOBUFS;
3766         return 0;
3767 }
3768
3769 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
3770                               struct cfg80211_registered_device *rdev,
3771                               struct wireless_dev *wdev,
3772                               enum nl80211_commands cmd)
3773 {
3774         struct net_device *dev = wdev->netdev;
3775         void *hdr;
3776
3777         WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
3778                 cmd != NL80211_CMD_DEL_INTERFACE &&
3779                 cmd != NL80211_CMD_SET_INTERFACE);
3780
3781         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
3782         if (!hdr)
3783                 return -1;
3784
3785         if (dev &&
3786             (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3787              nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
3788                 goto nla_put_failure;
3789
3790         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
3791             nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
3792             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
3793                               NL80211_ATTR_PAD) ||
3794             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
3795             nla_put_u32(msg, NL80211_ATTR_GENERATION,
3796                         rdev->devlist_generation ^
3797                         (cfg80211_rdev_list_generation << 2)) ||
3798             nla_put_u8(msg, NL80211_ATTR_4ADDR, wdev->use_4addr))
3799                 goto nla_put_failure;
3800
3801         if (rdev->ops->get_channel && !wdev->valid_links) {
3802                 struct cfg80211_chan_def chandef = {};
3803                 int ret;
3804
3805                 ret = rdev_get_channel(rdev, wdev, 0, &chandef);
3806                 if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3807                         goto nla_put_failure;
3808         }
3809
3810         if (rdev->ops->get_tx_power) {
3811                 int dbm, ret;
3812
3813                 ret = rdev_get_tx_power(rdev, wdev, &dbm);
3814                 if (ret == 0 &&
3815                     nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
3816                                 DBM_TO_MBM(dbm)))
3817                         goto nla_put_failure;
3818         }
3819
3820         wdev_lock(wdev);
3821         switch (wdev->iftype) {
3822         case NL80211_IFTYPE_AP:
3823         case NL80211_IFTYPE_P2P_GO:
3824                 if (wdev->u.ap.ssid_len &&
3825                     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ap.ssid_len,
3826                             wdev->u.ap.ssid))
3827                         goto nla_put_failure_locked;
3828                 break;
3829         case NL80211_IFTYPE_STATION:
3830         case NL80211_IFTYPE_P2P_CLIENT:
3831                 if (wdev->u.client.ssid_len &&
3832                     nla_put(msg, NL80211_ATTR_SSID, wdev->u.client.ssid_len,
3833                             wdev->u.client.ssid))
3834                         goto nla_put_failure_locked;
3835                 break;
3836         case NL80211_IFTYPE_ADHOC:
3837                 if (wdev->u.ibss.ssid_len &&
3838                     nla_put(msg, NL80211_ATTR_SSID, wdev->u.ibss.ssid_len,
3839                             wdev->u.ibss.ssid))
3840                         goto nla_put_failure_locked;
3841                 break;
3842         default:
3843                 /* nothing */
3844                 break;
3845         }
3846         wdev_unlock(wdev);
3847
3848         if (rdev->ops->get_txq_stats) {
3849                 struct cfg80211_txq_stats txqstats = {};
3850                 int ret = rdev_get_txq_stats(rdev, wdev, &txqstats);
3851
3852                 if (ret == 0 &&
3853                     !nl80211_put_txq_stats(msg, &txqstats,
3854                                            NL80211_ATTR_TXQ_STATS))
3855                         goto nla_put_failure;
3856         }
3857
3858         if (wdev->valid_links) {
3859                 unsigned int link_id;
3860                 struct nlattr *links = nla_nest_start(msg,
3861                                                       NL80211_ATTR_MLO_LINKS);
3862
3863                 if (!links)
3864                         goto nla_put_failure;
3865
3866                 for_each_valid_link(wdev, link_id) {
3867                         struct nlattr *link = nla_nest_start(msg, link_id + 1);
3868                         struct cfg80211_chan_def chandef = {};
3869                         int ret;
3870
3871                         if (!link)
3872                                 goto nla_put_failure;
3873
3874                         if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
3875                                 goto nla_put_failure;
3876                         if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
3877                                     wdev->links[link_id].addr))
3878                                 goto nla_put_failure;
3879
3880                         ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
3881                         if (ret == 0 && nl80211_send_chandef(msg, &chandef))
3882                                 goto nla_put_failure;
3883
3884                         nla_nest_end(msg, link);
3885                 }
3886
3887                 nla_nest_end(msg, links);
3888         }
3889
3890         genlmsg_end(msg, hdr);
3891         return 0;
3892
3893  nla_put_failure_locked:
3894         wdev_unlock(wdev);
3895  nla_put_failure:
3896         genlmsg_cancel(msg, hdr);
3897         return -EMSGSIZE;
3898 }
3899
3900 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
3901 {
3902         int wp_idx = 0;
3903         int if_idx = 0;
3904         int wp_start = cb->args[0];
3905         int if_start = cb->args[1];
3906         int filter_wiphy = -1;
3907         struct cfg80211_registered_device *rdev;
3908         struct wireless_dev *wdev;
3909         int ret;
3910
3911         rtnl_lock();
3912         if (!cb->args[2]) {
3913                 struct nl80211_dump_wiphy_state state = {
3914                         .filter_wiphy = -1,
3915                 };
3916
3917                 ret = nl80211_dump_wiphy_parse(skb, cb, &state);
3918                 if (ret)
3919                         goto out_unlock;
3920
3921                 filter_wiphy = state.filter_wiphy;
3922
3923                 /*
3924                  * if filtering, set cb->args[2] to +1 since 0 is the default
3925                  * value needed to determine that parsing is necessary.
3926                  */
3927                 if (filter_wiphy >= 0)
3928                         cb->args[2] = filter_wiphy + 1;
3929                 else
3930                         cb->args[2] = -1;
3931         } else if (cb->args[2] > 0) {
3932                 filter_wiphy = cb->args[2] - 1;
3933         }
3934
3935         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
3936                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
3937                         continue;
3938                 if (wp_idx < wp_start) {
3939                         wp_idx++;
3940                         continue;
3941                 }
3942
3943                 if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
3944                         continue;
3945
3946                 if_idx = 0;
3947
3948                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3949                         if (if_idx < if_start) {
3950                                 if_idx++;
3951                                 continue;
3952                         }
3953                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
3954                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
3955                                                rdev, wdev,
3956                                                NL80211_CMD_NEW_INTERFACE) < 0) {
3957                                 goto out;
3958                         }
3959                         if_idx++;
3960                 }
3961
3962                 wp_idx++;
3963         }
3964  out:
3965         cb->args[0] = wp_idx;
3966         cb->args[1] = if_idx;
3967
3968         ret = skb->len;
3969  out_unlock:
3970         rtnl_unlock();
3971
3972         return ret;
3973 }
3974
3975 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
3976 {
3977         struct sk_buff *msg;
3978         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3979         struct wireless_dev *wdev = info->user_ptr[1];
3980
3981         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3982         if (!msg)
3983                 return -ENOMEM;
3984
3985         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3986                                rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
3987                 nlmsg_free(msg);
3988                 return -ENOBUFS;
3989         }
3990
3991         return genlmsg_reply(msg, info);
3992 }
3993
3994 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
3995         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
3996         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
3997         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
3998         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
3999         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
4000         [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
4001 };
4002
4003 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
4004 {
4005         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
4006         int flag;
4007
4008         *mntrflags = 0;
4009
4010         if (!nla)
4011                 return -EINVAL;
4012
4013         if (nla_parse_nested_deprecated(flags, NL80211_MNTR_FLAG_MAX, nla, mntr_flags_policy, NULL))
4014                 return -EINVAL;
4015
4016         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
4017                 if (flags[flag])
4018                         *mntrflags |= (1<<flag);
4019
4020         *mntrflags |= MONITOR_FLAG_CHANGED;
4021
4022         return 0;
4023 }
4024
4025 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
4026                                      enum nl80211_iftype type,
4027                                      struct genl_info *info,
4028                                      struct vif_params *params)
4029 {
4030         bool change = false;
4031         int err;
4032
4033         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
4034                 if (type != NL80211_IFTYPE_MONITOR)
4035                         return -EINVAL;
4036
4037                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
4038                                           &params->flags);
4039                 if (err)
4040                         return err;
4041
4042                 change = true;
4043         }
4044
4045         if (params->flags & MONITOR_FLAG_ACTIVE &&
4046             !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
4047                 return -EOPNOTSUPP;
4048
4049         if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
4050                 const u8 *mumimo_groups;
4051                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4052
4053                 if (type != NL80211_IFTYPE_MONITOR)
4054                         return -EINVAL;
4055
4056                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4057                         return -EOPNOTSUPP;
4058
4059                 mumimo_groups =
4060                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
4061
4062                 /* bits 0 and 63 are reserved and must be zero */
4063                 if ((mumimo_groups[0] & BIT(0)) ||
4064                     (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
4065                         return -EINVAL;
4066
4067                 params->vht_mumimo_groups = mumimo_groups;
4068                 change = true;
4069         }
4070
4071         if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
4072                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
4073
4074                 if (type != NL80211_IFTYPE_MONITOR)
4075                         return -EINVAL;
4076
4077                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
4078                         return -EOPNOTSUPP;
4079
4080                 params->vht_mumimo_follow_addr =
4081                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
4082                 change = true;
4083         }
4084
4085         return change ? 1 : 0;
4086 }
4087
4088 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
4089                                struct net_device *netdev, u8 use_4addr,
4090                                enum nl80211_iftype iftype)
4091 {
4092         if (!use_4addr) {
4093                 if (netdev && netif_is_bridge_port(netdev))
4094                         return -EBUSY;
4095                 return 0;
4096         }
4097
4098         switch (iftype) {
4099         case NL80211_IFTYPE_AP_VLAN:
4100                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
4101                         return 0;
4102                 break;
4103         case NL80211_IFTYPE_STATION:
4104                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
4105                         return 0;
4106                 break;
4107         default:
4108                 break;
4109         }
4110
4111         return -EOPNOTSUPP;
4112 }
4113
4114 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
4115 {
4116         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4117         struct vif_params params;
4118         int err;
4119         enum nl80211_iftype otype, ntype;
4120         struct net_device *dev = info->user_ptr[1];
4121         bool change = false;
4122
4123         memset(&params, 0, sizeof(params));
4124
4125         otype = ntype = dev->ieee80211_ptr->iftype;
4126
4127         if (info->attrs[NL80211_ATTR_IFTYPE]) {
4128                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4129                 if (otype != ntype)
4130                         change = true;
4131         }
4132
4133         if (info->attrs[NL80211_ATTR_MESH_ID]) {
4134                 struct wireless_dev *wdev = dev->ieee80211_ptr;
4135
4136                 if (ntype != NL80211_IFTYPE_MESH_POINT)
4137                         return -EINVAL;
4138                 if (netif_running(dev))
4139                         return -EBUSY;
4140
4141                 wdev_lock(wdev);
4142                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4143                              IEEE80211_MAX_MESH_ID_LEN);
4144                 wdev->u.mesh.id_up_len =
4145                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4146                 memcpy(wdev->u.mesh.id,
4147                        nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4148                        wdev->u.mesh.id_up_len);
4149                 wdev_unlock(wdev);
4150         }
4151
4152         if (info->attrs[NL80211_ATTR_4ADDR]) {
4153                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4154                 change = true;
4155                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
4156                 if (err)
4157                         return err;
4158         } else {
4159                 params.use_4addr = -1;
4160         }
4161
4162         err = nl80211_parse_mon_options(rdev, ntype, info, &params);
4163         if (err < 0)
4164                 return err;
4165         if (err > 0)
4166                 change = true;
4167
4168         if (change)
4169                 err = cfg80211_change_iface(rdev, dev, ntype, &params);
4170         else
4171                 err = 0;
4172
4173         if (!err && params.use_4addr != -1)
4174                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
4175
4176         if (change && !err) {
4177                 struct wireless_dev *wdev = dev->ieee80211_ptr;
4178
4179                 nl80211_notify_iface(rdev, wdev, NL80211_CMD_SET_INTERFACE);
4180         }
4181
4182         return err;
4183 }
4184
4185 static int _nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4186 {
4187         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4188         struct vif_params params;
4189         struct wireless_dev *wdev;
4190         struct sk_buff *msg;
4191         int err;
4192         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
4193
4194         memset(&params, 0, sizeof(params));
4195
4196         if (!info->attrs[NL80211_ATTR_IFNAME])
4197                 return -EINVAL;
4198
4199         if (info->attrs[NL80211_ATTR_IFTYPE])
4200                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
4201
4202         if (!rdev->ops->add_virtual_intf)
4203                 return -EOPNOTSUPP;
4204
4205         if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
4206              rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
4207             info->attrs[NL80211_ATTR_MAC]) {
4208                 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
4209                            ETH_ALEN);
4210                 if (!is_valid_ether_addr(params.macaddr))
4211                         return -EADDRNOTAVAIL;
4212         }
4213
4214         if (info->attrs[NL80211_ATTR_4ADDR]) {
4215                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
4216                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
4217                 if (err)
4218                         return err;
4219         }
4220
4221         if (!cfg80211_iftype_allowed(&rdev->wiphy, type, params.use_4addr, 0))
4222                 return -EOPNOTSUPP;
4223
4224         err = nl80211_parse_mon_options(rdev, type, info, &params);
4225         if (err < 0)
4226                 return err;
4227
4228         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4229         if (!msg)
4230                 return -ENOMEM;
4231
4232         wdev = rdev_add_virtual_intf(rdev,
4233                                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
4234                                 NET_NAME_USER, type, &params);
4235         if (WARN_ON(!wdev)) {
4236                 nlmsg_free(msg);
4237                 return -EPROTO;
4238         } else if (IS_ERR(wdev)) {
4239                 nlmsg_free(msg);
4240                 return PTR_ERR(wdev);
4241         }
4242
4243         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
4244                 wdev->owner_nlportid = info->snd_portid;
4245
4246         switch (type) {
4247         case NL80211_IFTYPE_MESH_POINT:
4248                 if (!info->attrs[NL80211_ATTR_MESH_ID])
4249                         break;
4250                 wdev_lock(wdev);
4251                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
4252                              IEEE80211_MAX_MESH_ID_LEN);
4253                 wdev->u.mesh.id_up_len =
4254                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4255                 memcpy(wdev->u.mesh.id,
4256                        nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4257                        wdev->u.mesh.id_up_len);
4258                 wdev_unlock(wdev);
4259                 break;
4260         case NL80211_IFTYPE_NAN:
4261         case NL80211_IFTYPE_P2P_DEVICE:
4262                 /*
4263                  * P2P Device and NAN do not have a netdev, so don't go
4264                  * through the netdev notifier and must be added here
4265                  */
4266                 cfg80211_init_wdev(wdev);
4267                 cfg80211_register_wdev(rdev, wdev);
4268                 break;
4269         default:
4270                 break;
4271         }
4272
4273         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
4274                                rdev, wdev, NL80211_CMD_NEW_INTERFACE) < 0) {
4275                 nlmsg_free(msg);
4276                 return -ENOBUFS;
4277         }
4278
4279         return genlmsg_reply(msg, info);
4280 }
4281
4282 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
4283 {
4284         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4285         int ret;
4286
4287         /* to avoid failing a new interface creation due to pending removal */
4288         cfg80211_destroy_ifaces(rdev);
4289
4290         wiphy_lock(&rdev->wiphy);
4291         ret = _nl80211_new_interface(skb, info);
4292         wiphy_unlock(&rdev->wiphy);
4293
4294         return ret;
4295 }
4296
4297 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
4298 {
4299         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4300         struct wireless_dev *wdev = info->user_ptr[1];
4301
4302         if (!rdev->ops->del_virtual_intf)
4303                 return -EOPNOTSUPP;
4304
4305         /*
4306          * We hold RTNL, so this is safe, without RTNL opencount cannot
4307          * reach 0, and thus the rdev cannot be deleted.
4308          *
4309          * We need to do it for the dev_close(), since that will call
4310          * the netdev notifiers, and we need to acquire the mutex there
4311          * but don't know if we get there from here or from some other
4312          * place (e.g. "ip link set ... down").
4313          */
4314         mutex_unlock(&rdev->wiphy.mtx);
4315
4316         /*
4317          * If we remove a wireless device without a netdev then clear
4318          * user_ptr[1] so that nl80211_post_doit won't dereference it
4319          * to check if it needs to do dev_put(). Otherwise it crashes
4320          * since the wdev has been freed, unlike with a netdev where
4321          * we need the dev_put() for the netdev to really be freed.
4322          */
4323         if (!wdev->netdev)
4324                 info->user_ptr[1] = NULL;
4325         else
4326                 dev_close(wdev->netdev);
4327
4328         mutex_lock(&rdev->wiphy.mtx);
4329
4330         return cfg80211_remove_virtual_intf(rdev, wdev);
4331 }
4332
4333 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
4334 {
4335         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4336         struct net_device *dev = info->user_ptr[1];
4337         u16 noack_map;
4338
4339         if (!info->attrs[NL80211_ATTR_NOACK_MAP])
4340                 return -EINVAL;
4341
4342         if (!rdev->ops->set_noack_map)
4343                 return -EOPNOTSUPP;
4344
4345         noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
4346
4347         return rdev_set_noack_map(rdev, dev, noack_map);
4348 }
4349
4350 static int nl80211_validate_key_link_id(struct genl_info *info,
4351                                         struct wireless_dev *wdev,
4352                                         int link_id, bool pairwise)
4353 {
4354         if (pairwise) {
4355                 if (link_id != -1) {
4356                         GENL_SET_ERR_MSG(info,
4357                                          "link ID not allowed for pairwise key");
4358                         return -EINVAL;
4359                 }
4360
4361                 return 0;
4362         }
4363
4364         if (wdev->valid_links) {
4365                 if (link_id == -1) {
4366                         GENL_SET_ERR_MSG(info,
4367                                          "link ID must for MLO group key");
4368                         return -EINVAL;
4369                 }
4370                 if (!(wdev->valid_links & BIT(link_id))) {
4371                         GENL_SET_ERR_MSG(info, "invalid link ID for MLO group key");
4372                         return -EINVAL;
4373                 }
4374         } else if (link_id != -1) {
4375                 GENL_SET_ERR_MSG(info, "link ID not allowed for non-MLO group key");
4376                 return -EINVAL;
4377         }
4378
4379         return 0;
4380 }
4381
4382 struct get_key_cookie {
4383         struct sk_buff *msg;
4384         int error;
4385         int idx;
4386 };
4387
4388 static void get_key_callback(void *c, struct key_params *params)
4389 {
4390         struct nlattr *key;
4391         struct get_key_cookie *cookie = c;
4392
4393         if ((params->key &&
4394              nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
4395                      params->key_len, params->key)) ||
4396             (params->seq &&
4397              nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
4398                      params->seq_len, params->seq)) ||
4399             (params->cipher &&
4400              nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
4401                          params->cipher)))
4402                 goto nla_put_failure;
4403
4404         key = nla_nest_start_noflag(cookie->msg, NL80211_ATTR_KEY);
4405         if (!key)
4406                 goto nla_put_failure;
4407
4408         if ((params->key &&
4409              nla_put(cookie->msg, NL80211_KEY_DATA,
4410                      params->key_len, params->key)) ||
4411             (params->seq &&
4412              nla_put(cookie->msg, NL80211_KEY_SEQ,
4413                      params->seq_len, params->seq)) ||
4414             (params->cipher &&
4415              nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
4416                          params->cipher)))
4417                 goto nla_put_failure;
4418
4419         if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
4420                 goto nla_put_failure;
4421
4422         nla_nest_end(cookie->msg, key);
4423
4424         return;
4425  nla_put_failure:
4426         cookie->error = 1;
4427 }
4428
4429 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
4430 {
4431         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4432         int err;
4433         struct net_device *dev = info->user_ptr[1];
4434         u8 key_idx = 0;
4435         const u8 *mac_addr = NULL;
4436         bool pairwise;
4437         struct get_key_cookie cookie = {
4438                 .error = 0,
4439         };
4440         void *hdr;
4441         struct sk_buff *msg;
4442         bool bigtk_support = false;
4443         int link_id = nl80211_link_id_or_invalid(info->attrs);
4444         struct wireless_dev *wdev = dev->ieee80211_ptr;
4445
4446         if (wiphy_ext_feature_isset(&rdev->wiphy,
4447                                     NL80211_EXT_FEATURE_BEACON_PROTECTION))
4448                 bigtk_support = true;
4449
4450         if ((wdev->iftype == NL80211_IFTYPE_STATION ||
4451              wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
4452             wiphy_ext_feature_isset(&rdev->wiphy,
4453                                     NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
4454                 bigtk_support = true;
4455
4456         if (info->attrs[NL80211_ATTR_KEY_IDX]) {
4457                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
4458
4459                 if (key_idx >= 6 && key_idx <= 7 && !bigtk_support) {
4460                         GENL_SET_ERR_MSG(info, "BIGTK not supported");
4461                         return -EINVAL;
4462                 }
4463         }
4464
4465         if (info->attrs[NL80211_ATTR_MAC])
4466                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4467
4468         pairwise = !!mac_addr;
4469         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
4470                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
4471
4472                 if (kt != NL80211_KEYTYPE_GROUP &&
4473                     kt != NL80211_KEYTYPE_PAIRWISE)
4474                         return -EINVAL;
4475                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
4476         }
4477
4478         if (!rdev->ops->get_key)
4479                 return -EOPNOTSUPP;
4480
4481         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4482                 return -ENOENT;
4483
4484         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4485         if (!msg)
4486                 return -ENOMEM;
4487
4488         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
4489                              NL80211_CMD_NEW_KEY);
4490         if (!hdr)
4491                 goto nla_put_failure;
4492
4493         cookie.msg = msg;
4494         cookie.idx = key_idx;
4495
4496         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4497             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
4498                 goto nla_put_failure;
4499         if (mac_addr &&
4500             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
4501                 goto nla_put_failure;
4502
4503         err = nl80211_validate_key_link_id(info, wdev, link_id, pairwise);
4504         if (err)
4505                 goto free_msg;
4506
4507         err = rdev_get_key(rdev, dev, link_id, key_idx, pairwise, mac_addr,
4508                            &cookie, get_key_callback);
4509
4510         if (err)
4511                 goto free_msg;
4512
4513         if (cookie.error)
4514                 goto nla_put_failure;
4515
4516         genlmsg_end(msg, hdr);
4517         return genlmsg_reply(msg, info);
4518
4519  nla_put_failure:
4520         err = -ENOBUFS;
4521  free_msg:
4522         nlmsg_free(msg);
4523         return err;
4524 }
4525
4526 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
4527 {
4528         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4529         struct key_parse key;
4530         int err;
4531         struct net_device *dev = info->user_ptr[1];
4532         int link_id = nl80211_link_id_or_invalid(info->attrs);
4533         struct wireless_dev *wdev = dev->ieee80211_ptr;
4534
4535         err = nl80211_parse_key(info, &key);
4536         if (err)
4537                 return err;
4538
4539         if (key.idx < 0)
4540                 return -EINVAL;
4541
4542         /* Only support setting default key and
4543          * Extended Key ID action NL80211_KEY_SET_TX.
4544          */
4545         if (!key.def && !key.defmgmt && !key.defbeacon &&
4546             !(key.p.mode == NL80211_KEY_SET_TX))
4547                 return -EINVAL;
4548
4549         wdev_lock(wdev);
4550
4551         if (key.def) {
4552                 if (!rdev->ops->set_default_key) {
4553                         err = -EOPNOTSUPP;
4554                         goto out;
4555                 }
4556
4557                 err = nl80211_key_allowed(wdev);
4558                 if (err)
4559                         goto out;
4560
4561                 err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4562                 if (err)
4563                         goto out;
4564
4565                 err = rdev_set_default_key(rdev, dev, link_id, key.idx,
4566                                            key.def_uni, key.def_multi);
4567
4568                 if (err)
4569                         goto out;
4570
4571 #ifdef CONFIG_CFG80211_WEXT
4572                 wdev->wext.default_key = key.idx;
4573 #endif
4574         } else if (key.defmgmt) {
4575                 if (key.def_uni || !key.def_multi) {
4576                         err = -EINVAL;
4577                         goto out;
4578                 }
4579
4580                 if (!rdev->ops->set_default_mgmt_key) {
4581                         err = -EOPNOTSUPP;
4582                         goto out;
4583                 }
4584
4585                 err = nl80211_key_allowed(wdev);
4586                 if (err)
4587                         goto out;
4588
4589                 err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4590                 if (err)
4591                         goto out;
4592
4593                 err = rdev_set_default_mgmt_key(rdev, dev, link_id, key.idx);
4594                 if (err)
4595                         goto out;
4596
4597 #ifdef CONFIG_CFG80211_WEXT
4598                 wdev->wext.default_mgmt_key = key.idx;
4599 #endif
4600         } else if (key.defbeacon) {
4601                 if (key.def_uni || !key.def_multi) {
4602                         err = -EINVAL;
4603                         goto out;
4604                 }
4605
4606                 if (!rdev->ops->set_default_beacon_key) {
4607                         err = -EOPNOTSUPP;
4608                         goto out;
4609                 }
4610
4611                 err = nl80211_key_allowed(wdev);
4612                 if (err)
4613                         goto out;
4614
4615                 err = nl80211_validate_key_link_id(info, wdev, link_id, false);
4616                 if (err)
4617                         goto out;
4618
4619                 err = rdev_set_default_beacon_key(rdev, dev, link_id, key.idx);
4620                 if (err)
4621                         goto out;
4622         } else if (key.p.mode == NL80211_KEY_SET_TX &&
4623                    wiphy_ext_feature_isset(&rdev->wiphy,
4624                                            NL80211_EXT_FEATURE_EXT_KEY_ID)) {
4625                 u8 *mac_addr = NULL;
4626
4627                 if (info->attrs[NL80211_ATTR_MAC])
4628                         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4629
4630                 if (!mac_addr || key.idx < 0 || key.idx > 1) {
4631                         err = -EINVAL;
4632                         goto out;
4633                 }
4634
4635                 err = nl80211_validate_key_link_id(info, wdev, link_id, true);
4636                 if (err)
4637                         goto out;
4638
4639                 err = rdev_add_key(rdev, dev, link_id, key.idx,
4640                                    NL80211_KEYTYPE_PAIRWISE,
4641                                    mac_addr, &key.p);
4642         } else {
4643                 err = -EINVAL;
4644         }
4645  out:
4646         wdev_unlock(wdev);
4647
4648         return err;
4649 }
4650
4651 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
4652 {
4653         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4654         int err;
4655         struct net_device *dev = info->user_ptr[1];
4656         struct key_parse key;
4657         const u8 *mac_addr = NULL;
4658         int link_id = nl80211_link_id_or_invalid(info->attrs);
4659         struct wireless_dev *wdev = dev->ieee80211_ptr;
4660
4661         err = nl80211_parse_key(info, &key);
4662         if (err)
4663                 return err;
4664
4665         if (!key.p.key) {
4666                 GENL_SET_ERR_MSG(info, "no key");
4667                 return -EINVAL;
4668         }
4669
4670         if (info->attrs[NL80211_ATTR_MAC])
4671                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4672
4673         if (key.type == -1) {
4674                 if (mac_addr)
4675                         key.type = NL80211_KEYTYPE_PAIRWISE;
4676                 else
4677                         key.type = NL80211_KEYTYPE_GROUP;
4678         }
4679
4680         /* for now */
4681         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4682             key.type != NL80211_KEYTYPE_GROUP) {
4683                 GENL_SET_ERR_MSG(info, "key type not pairwise or group");
4684                 return -EINVAL;
4685         }
4686
4687         if (key.type == NL80211_KEYTYPE_GROUP &&
4688             info->attrs[NL80211_ATTR_VLAN_ID])
4689                 key.p.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
4690
4691         if (!rdev->ops->add_key)
4692                 return -EOPNOTSUPP;
4693
4694         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
4695                                            key.type == NL80211_KEYTYPE_PAIRWISE,
4696                                            mac_addr)) {
4697                 GENL_SET_ERR_MSG(info, "key setting validation failed");
4698                 return -EINVAL;
4699         }
4700
4701         wdev_lock(wdev);
4702         err = nl80211_key_allowed(wdev);
4703         if (err)
4704                 GENL_SET_ERR_MSG(info, "key not allowed");
4705
4706         if (!err)
4707                 err = nl80211_validate_key_link_id(info, wdev, link_id,
4708                                 key.type == NL80211_KEYTYPE_PAIRWISE);
4709
4710         if (!err) {
4711                 err = rdev_add_key(rdev, dev, link_id, key.idx,
4712                                    key.type == NL80211_KEYTYPE_PAIRWISE,
4713                                     mac_addr, &key.p);
4714                 if (err)
4715                         GENL_SET_ERR_MSG(info, "key addition failed");
4716         }
4717         wdev_unlock(wdev);
4718
4719         return err;
4720 }
4721
4722 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
4723 {
4724         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4725         int err;
4726         struct net_device *dev = info->user_ptr[1];
4727         u8 *mac_addr = NULL;
4728         struct key_parse key;
4729         int link_id = nl80211_link_id_or_invalid(info->attrs);
4730         struct wireless_dev *wdev = dev->ieee80211_ptr;
4731
4732         err = nl80211_parse_key(info, &key);
4733         if (err)
4734                 return err;
4735
4736         if (info->attrs[NL80211_ATTR_MAC])
4737                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4738
4739         if (key.type == -1) {
4740                 if (mac_addr)
4741                         key.type = NL80211_KEYTYPE_PAIRWISE;
4742                 else
4743                         key.type = NL80211_KEYTYPE_GROUP;
4744         }
4745
4746         /* for now */
4747         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
4748             key.type != NL80211_KEYTYPE_GROUP)
4749                 return -EINVAL;
4750
4751         if (!cfg80211_valid_key_idx(rdev, key.idx,
4752                                     key.type == NL80211_KEYTYPE_PAIRWISE))
4753                 return -EINVAL;
4754
4755         if (!rdev->ops->del_key)
4756                 return -EOPNOTSUPP;
4757
4758         wdev_lock(wdev);
4759         err = nl80211_key_allowed(wdev);
4760
4761         if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
4762             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
4763                 err = -ENOENT;
4764
4765         if (!err)
4766                 err = nl80211_validate_key_link_id(info, wdev, link_id,
4767                                 key.type == NL80211_KEYTYPE_PAIRWISE);
4768
4769         if (!err)
4770                 err = rdev_del_key(rdev, dev, link_id, key.idx,
4771                                    key.type == NL80211_KEYTYPE_PAIRWISE,
4772                                    mac_addr);
4773
4774 #ifdef CONFIG_CFG80211_WEXT
4775         if (!err) {
4776                 if (key.idx == wdev->wext.default_key)
4777                         wdev->wext.default_key = -1;
4778                 else if (key.idx == wdev->wext.default_mgmt_key)
4779                         wdev->wext.default_mgmt_key = -1;
4780         }
4781 #endif
4782         wdev_unlock(wdev);
4783
4784         return err;
4785 }
4786
4787 /* This function returns an error or the number of nested attributes */
4788 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
4789 {
4790         struct nlattr *attr;
4791         int n_entries = 0, tmp;
4792
4793         nla_for_each_nested(attr, nl_attr, tmp) {
4794                 if (nla_len(attr) != ETH_ALEN)
4795                         return -EINVAL;
4796
4797                 n_entries++;
4798         }
4799
4800         return n_entries;
4801 }
4802
4803 /*
4804  * This function parses ACL information and allocates memory for ACL data.
4805  * On successful return, the calling function is responsible to free the
4806  * ACL buffer returned by this function.
4807  */
4808 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
4809                                                 struct genl_info *info)
4810 {
4811         enum nl80211_acl_policy acl_policy;
4812         struct nlattr *attr;
4813         struct cfg80211_acl_data *acl;
4814         int i = 0, n_entries, tmp;
4815
4816         if (!wiphy->max_acl_mac_addrs)
4817                 return ERR_PTR(-EOPNOTSUPP);
4818
4819         if (!info->attrs[NL80211_ATTR_ACL_POLICY])
4820                 return ERR_PTR(-EINVAL);
4821
4822         acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
4823         if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
4824             acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
4825                 return ERR_PTR(-EINVAL);
4826
4827         if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
4828                 return ERR_PTR(-EINVAL);
4829
4830         n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
4831         if (n_entries < 0)
4832                 return ERR_PTR(n_entries);
4833
4834         if (n_entries > wiphy->max_acl_mac_addrs)
4835                 return ERR_PTR(-ENOTSUPP);
4836
4837         acl = kzalloc(struct_size(acl, mac_addrs, n_entries), GFP_KERNEL);
4838         if (!acl)
4839                 return ERR_PTR(-ENOMEM);
4840
4841         nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
4842                 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
4843                 i++;
4844         }
4845
4846         acl->n_acl_entries = n_entries;
4847         acl->acl_policy = acl_policy;
4848
4849         return acl;
4850 }
4851
4852 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
4853 {
4854         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4855         struct net_device *dev = info->user_ptr[1];
4856         struct cfg80211_acl_data *acl;
4857         int err;
4858
4859         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4860             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4861                 return -EOPNOTSUPP;
4862
4863         if (!dev->ieee80211_ptr->links[0].ap.beacon_interval)
4864                 return -EINVAL;
4865
4866         acl = parse_acl_data(&rdev->wiphy, info);
4867         if (IS_ERR(acl))
4868                 return PTR_ERR(acl);
4869
4870         err = rdev_set_mac_acl(rdev, dev, acl);
4871
4872         kfree(acl);
4873
4874         return err;
4875 }
4876
4877 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4878                            u8 *rates, u8 rates_len)
4879 {
4880         u8 i;
4881         u32 mask = 0;
4882
4883         for (i = 0; i < rates_len; i++) {
4884                 int rate = (rates[i] & 0x7f) * 5;
4885                 int ridx;
4886
4887                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4888                         struct ieee80211_rate *srate =
4889                                 &sband->bitrates[ridx];
4890                         if (rate == srate->bitrate) {
4891                                 mask |= 1 << ridx;
4892                                 break;
4893                         }
4894                 }
4895                 if (ridx == sband->n_bitrates)
4896                         return 0; /* rate not found */
4897         }
4898
4899         return mask;
4900 }
4901
4902 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
4903                                u8 *rates, u8 rates_len,
4904                                u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
4905 {
4906         u8 i;
4907
4908         memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
4909
4910         for (i = 0; i < rates_len; i++) {
4911                 int ridx, rbit;
4912
4913                 ridx = rates[i] / 8;
4914                 rbit = BIT(rates[i] % 8);
4915
4916                 /* check validity */
4917                 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
4918                         return false;
4919
4920                 /* check availability */
4921                 ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
4922                 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
4923                         mcs[ridx] |= rbit;
4924                 else
4925                         return false;
4926         }
4927
4928         return true;
4929 }
4930
4931 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
4932 {
4933         u16 mcs_mask = 0;
4934
4935         switch (vht_mcs_map) {
4936         case IEEE80211_VHT_MCS_NOT_SUPPORTED:
4937                 break;
4938         case IEEE80211_VHT_MCS_SUPPORT_0_7:
4939                 mcs_mask = 0x00FF;
4940                 break;
4941         case IEEE80211_VHT_MCS_SUPPORT_0_8:
4942                 mcs_mask = 0x01FF;
4943                 break;
4944         case IEEE80211_VHT_MCS_SUPPORT_0_9:
4945                 mcs_mask = 0x03FF;
4946                 break;
4947         default:
4948                 break;
4949         }
4950
4951         return mcs_mask;
4952 }
4953
4954 static void vht_build_mcs_mask(u16 vht_mcs_map,
4955                                u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
4956 {
4957         u8 nss;
4958
4959         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
4960                 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
4961                 vht_mcs_map >>= 2;
4962         }
4963 }
4964
4965 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
4966                              struct nl80211_txrate_vht *txrate,
4967                              u16 mcs[NL80211_VHT_NSS_MAX])
4968 {
4969         u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
4970         u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
4971         u8 i;
4972
4973         if (!sband->vht_cap.vht_supported)
4974                 return false;
4975
4976         memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
4977
4978         /* Build vht_mcs_mask from VHT capabilities */
4979         vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
4980
4981         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4982                 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
4983                         mcs[i] = txrate->mcs[i];
4984                 else
4985                         return false;
4986         }
4987
4988         return true;
4989 }
4990
4991 static u16 he_mcs_map_to_mcs_mask(u8 he_mcs_map)
4992 {
4993         switch (he_mcs_map) {
4994         case IEEE80211_HE_MCS_NOT_SUPPORTED:
4995                 return 0;
4996         case IEEE80211_HE_MCS_SUPPORT_0_7:
4997                 return 0x00FF;
4998         case IEEE80211_HE_MCS_SUPPORT_0_9:
4999                 return 0x03FF;
5000         case IEEE80211_HE_MCS_SUPPORT_0_11:
5001                 return 0xFFF;
5002         default:
5003                 break;
5004         }
5005         return 0;
5006 }
5007
5008 static void he_build_mcs_mask(u16 he_mcs_map,
5009                               u16 he_mcs_mask[NL80211_HE_NSS_MAX])
5010 {
5011         u8 nss;
5012
5013         for (nss = 0; nss < NL80211_HE_NSS_MAX; nss++) {
5014                 he_mcs_mask[nss] = he_mcs_map_to_mcs_mask(he_mcs_map & 0x03);
5015                 he_mcs_map >>= 2;
5016         }
5017 }
5018
5019 static u16 he_get_txmcsmap(struct genl_info *info, unsigned int link_id,
5020                            const struct ieee80211_sta_he_cap *he_cap)
5021 {
5022         struct net_device *dev = info->user_ptr[1];
5023         struct wireless_dev *wdev = dev->ieee80211_ptr;
5024         struct cfg80211_chan_def *chandef;
5025         __le16 tx_mcs;
5026
5027         chandef = wdev_chandef(wdev, link_id);
5028         if (!chandef) {
5029                 /*
5030                  * This is probably broken, but we never maintained
5031                  * a chandef in these cases, so it always was.
5032                  */
5033                 return le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
5034         }
5035
5036         switch (chandef->width) {
5037         case NL80211_CHAN_WIDTH_80P80:
5038                 tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80p80;
5039                 break;
5040         case NL80211_CHAN_WIDTH_160:
5041                 tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_160;
5042                 break;
5043         default:
5044                 tx_mcs = he_cap->he_mcs_nss_supp.tx_mcs_80;
5045                 break;
5046         }
5047
5048         return le16_to_cpu(tx_mcs);
5049 }
5050
5051 static bool he_set_mcs_mask(struct genl_info *info,
5052                             struct wireless_dev *wdev,
5053                             struct ieee80211_supported_band *sband,
5054                             struct nl80211_txrate_he *txrate,
5055                             u16 mcs[NL80211_HE_NSS_MAX],
5056                             unsigned int link_id)
5057 {
5058         const struct ieee80211_sta_he_cap *he_cap;
5059         u16 tx_mcs_mask[NL80211_HE_NSS_MAX] = {};
5060         u16 tx_mcs_map = 0;
5061         u8 i;
5062
5063         he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5064         if (!he_cap)
5065                 return false;
5066
5067         memset(mcs, 0, sizeof(u16) * NL80211_HE_NSS_MAX);
5068
5069         tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5070
5071         /* Build he_mcs_mask from HE capabilities */
5072         he_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
5073
5074         for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5075                 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
5076                         mcs[i] = txrate->mcs[i];
5077                 else
5078                         return false;
5079         }
5080
5081         return true;
5082 }
5083
5084 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
5085                                          struct nlattr *attrs[],
5086                                          enum nl80211_attrs attr,
5087                                          struct cfg80211_bitrate_mask *mask,
5088                                          struct net_device *dev,
5089                                          bool default_all_enabled,
5090                                          unsigned int link_id)
5091 {
5092         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
5093         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5094         struct wireless_dev *wdev = dev->ieee80211_ptr;
5095         int rem, i;
5096         struct nlattr *tx_rates;
5097         struct ieee80211_supported_band *sband;
5098         u16 vht_tx_mcs_map, he_tx_mcs_map;
5099
5100         memset(mask, 0, sizeof(*mask));
5101         /* Default to all rates enabled */
5102         for (i = 0; i < NUM_NL80211_BANDS; i++) {
5103                 const struct ieee80211_sta_he_cap *he_cap;
5104
5105                 if (!default_all_enabled)
5106                         break;
5107
5108                 sband = rdev->wiphy.bands[i];
5109
5110                 if (!sband)
5111                         continue;
5112
5113                 mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
5114                 memcpy(mask->control[i].ht_mcs,
5115                        sband->ht_cap.mcs.rx_mask,
5116                        sizeof(mask->control[i].ht_mcs));
5117
5118                 if (sband->vht_cap.vht_supported) {
5119                         vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5120                         vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
5121                 }
5122
5123                 he_cap = ieee80211_get_he_iftype_cap(sband, wdev->iftype);
5124                 if (!he_cap)
5125                         continue;
5126
5127                 he_tx_mcs_map = he_get_txmcsmap(info, link_id, he_cap);
5128                 he_build_mcs_mask(he_tx_mcs_map, mask->control[i].he_mcs);
5129
5130                 mask->control[i].he_gi = 0xFF;
5131                 mask->control[i].he_ltf = 0xFF;
5132         }
5133
5134         /* if no rates are given set it back to the defaults */
5135         if (!attrs[attr])
5136                 goto out;
5137
5138         /* The nested attribute uses enum nl80211_band as the index. This maps
5139          * directly to the enum nl80211_band values used in cfg80211.
5140          */
5141         BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
5142         nla_for_each_nested(tx_rates, attrs[attr], rem) {
5143                 enum nl80211_band band = nla_type(tx_rates);
5144                 int err;
5145
5146                 if (band < 0 || band >= NUM_NL80211_BANDS)
5147                         return -EINVAL;
5148                 sband = rdev->wiphy.bands[band];
5149                 if (sband == NULL)
5150                         return -EINVAL;
5151                 err = nla_parse_nested_deprecated(tb, NL80211_TXRATE_MAX,
5152                                                   tx_rates,
5153                                                   nl80211_txattr_policy,
5154                                                   info->extack);
5155                 if (err)
5156                         return err;
5157                 if (tb[NL80211_TXRATE_LEGACY]) {
5158                         mask->control[band].legacy = rateset_to_mask(
5159                                 sband,
5160                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
5161                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
5162                         if ((mask->control[band].legacy == 0) &&
5163                             nla_len(tb[NL80211_TXRATE_LEGACY]))
5164                                 return -EINVAL;
5165                 }
5166                 if (tb[NL80211_TXRATE_HT]) {
5167                         if (!ht_rateset_to_mask(
5168                                         sband,
5169                                         nla_data(tb[NL80211_TXRATE_HT]),
5170                                         nla_len(tb[NL80211_TXRATE_HT]),
5171                                         mask->control[band].ht_mcs))
5172                                 return -EINVAL;
5173                 }
5174
5175                 if (tb[NL80211_TXRATE_VHT]) {
5176                         if (!vht_set_mcs_mask(
5177                                         sband,
5178                                         nla_data(tb[NL80211_TXRATE_VHT]),
5179                                         mask->control[band].vht_mcs))
5180                                 return -EINVAL;
5181                 }
5182
5183                 if (tb[NL80211_TXRATE_GI]) {
5184                         mask->control[band].gi =
5185                                 nla_get_u8(tb[NL80211_TXRATE_GI]);
5186                         if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
5187                                 return -EINVAL;
5188                 }
5189                 if (tb[NL80211_TXRATE_HE] &&
5190                     !he_set_mcs_mask(info, wdev, sband,
5191                                      nla_data(tb[NL80211_TXRATE_HE]),
5192                                      mask->control[band].he_mcs,
5193                                      link_id))
5194                         return -EINVAL;
5195
5196                 if (tb[NL80211_TXRATE_HE_GI])
5197                         mask->control[band].he_gi =
5198                                 nla_get_u8(tb[NL80211_TXRATE_HE_GI]);
5199                 if (tb[NL80211_TXRATE_HE_LTF])
5200                         mask->control[band].he_ltf =
5201                                 nla_get_u8(tb[NL80211_TXRATE_HE_LTF]);
5202
5203                 if (mask->control[band].legacy == 0) {
5204                         /* don't allow empty legacy rates if HT, VHT or HE
5205                          * are not even supported.
5206                          */
5207                         if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
5208                               rdev->wiphy.bands[band]->vht_cap.vht_supported ||
5209                               ieee80211_get_he_iftype_cap(sband, wdev->iftype)))
5210                                 return -EINVAL;
5211
5212                         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5213                                 if (mask->control[band].ht_mcs[i])
5214                                         goto out;
5215
5216                         for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5217                                 if (mask->control[band].vht_mcs[i])
5218                                         goto out;
5219
5220                         for (i = 0; i < NL80211_HE_NSS_MAX; i++)
5221                                 if (mask->control[band].he_mcs[i])
5222                                         goto out;
5223
5224                         /* legacy and mcs rates may not be both empty */
5225                         return -EINVAL;
5226                 }
5227         }
5228
5229 out:
5230         return 0;
5231 }
5232
5233 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
5234                                    enum nl80211_band band,
5235                                    struct cfg80211_bitrate_mask *beacon_rate)
5236 {
5237         u32 count_ht, count_vht, count_he, i;
5238         u32 rate = beacon_rate->control[band].legacy;
5239
5240         /* Allow only one rate */
5241         if (hweight32(rate) > 1)
5242                 return -EINVAL;
5243
5244         count_ht = 0;
5245         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5246                 if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
5247                         return -EINVAL;
5248                 } else if (beacon_rate->control[band].ht_mcs[i]) {
5249                         count_ht++;
5250                         if (count_ht > 1)
5251                                 return -EINVAL;
5252                 }
5253                 if (count_ht && rate)
5254                         return -EINVAL;
5255         }
5256
5257         count_vht = 0;
5258         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5259                 if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
5260                         return -EINVAL;
5261                 } else if (beacon_rate->control[band].vht_mcs[i]) {
5262                         count_vht++;
5263                         if (count_vht > 1)
5264                                 return -EINVAL;
5265                 }
5266                 if (count_vht && rate)
5267                         return -EINVAL;
5268         }
5269
5270         count_he = 0;
5271         for (i = 0; i < NL80211_HE_NSS_MAX; i++) {
5272                 if (hweight16(beacon_rate->control[band].he_mcs[i]) > 1) {
5273                         return -EINVAL;
5274                 } else if (beacon_rate->control[band].he_mcs[i]) {
5275                         count_he++;
5276                         if (count_he > 1)
5277                                 return -EINVAL;
5278                 }
5279                 if (count_he && rate)
5280                         return -EINVAL;
5281         }
5282
5283         if ((count_ht && count_vht && count_he) ||
5284             (!rate && !count_ht && !count_vht && !count_he))
5285                 return -EINVAL;
5286
5287         if (rate &&
5288             !wiphy_ext_feature_isset(&rdev->wiphy,
5289                                      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
5290                 return -EINVAL;
5291         if (count_ht &&
5292             !wiphy_ext_feature_isset(&rdev->wiphy,
5293                                      NL80211_EXT_FEATURE_BEACON_RATE_HT))
5294                 return -EINVAL;
5295         if (count_vht &&
5296             !wiphy_ext_feature_isset(&rdev->wiphy,
5297                                      NL80211_EXT_FEATURE_BEACON_RATE_VHT))
5298                 return -EINVAL;
5299         if (count_he &&
5300             !wiphy_ext_feature_isset(&rdev->wiphy,
5301                                      NL80211_EXT_FEATURE_BEACON_RATE_HE))
5302                 return -EINVAL;
5303
5304         return 0;
5305 }
5306
5307 static int nl80211_parse_mbssid_config(struct wiphy *wiphy,
5308                                        struct net_device *dev,
5309                                        struct nlattr *attrs,
5310                                        struct cfg80211_mbssid_config *config,
5311                                        u8 num_elems)
5312 {
5313         struct nlattr *tb[NL80211_MBSSID_CONFIG_ATTR_MAX + 1];
5314
5315         if (!wiphy->mbssid_max_interfaces)
5316                 return -EOPNOTSUPP;
5317
5318         if (nla_parse_nested(tb, NL80211_MBSSID_CONFIG_ATTR_MAX, attrs, NULL,
5319                              NULL) ||
5320             !tb[NL80211_MBSSID_CONFIG_ATTR_INDEX])
5321                 return -EINVAL;
5322
5323         config->ema = nla_get_flag(tb[NL80211_MBSSID_CONFIG_ATTR_EMA]);
5324         if (config->ema) {
5325                 if (!wiphy->ema_max_profile_periodicity)
5326                         return -EOPNOTSUPP;
5327
5328                 if (num_elems > wiphy->ema_max_profile_periodicity)
5329                         return -EINVAL;
5330         }
5331
5332         config->index = nla_get_u8(tb[NL80211_MBSSID_CONFIG_ATTR_INDEX]);
5333         if (config->index >= wiphy->mbssid_max_interfaces ||
5334             (!config->index && !num_elems))
5335                 return -EINVAL;
5336
5337         if (tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]) {
5338                 u32 tx_ifindex =
5339                         nla_get_u32(tb[NL80211_MBSSID_CONFIG_ATTR_TX_IFINDEX]);
5340
5341                 if ((!config->index && tx_ifindex != dev->ifindex) ||
5342                     (config->index && tx_ifindex == dev->ifindex))
5343                         return -EINVAL;
5344
5345                 if (tx_ifindex != dev->ifindex) {
5346                         struct net_device *tx_netdev =
5347                                 dev_get_by_index(wiphy_net(wiphy), tx_ifindex);
5348
5349                         if (!tx_netdev || !tx_netdev->ieee80211_ptr ||
5350                             tx_netdev->ieee80211_ptr->wiphy != wiphy ||
5351                             tx_netdev->ieee80211_ptr->iftype !=
5352                                                         NL80211_IFTYPE_AP) {
5353                                 dev_put(tx_netdev);
5354                                 return -EINVAL;
5355                         }
5356
5357                         config->tx_wdev = tx_netdev->ieee80211_ptr;
5358                 } else {
5359                         config->tx_wdev = dev->ieee80211_ptr;
5360                 }
5361         } else if (!config->index) {
5362                 config->tx_wdev = dev->ieee80211_ptr;
5363         } else {
5364                 return -EINVAL;
5365         }
5366
5367         return 0;
5368 }
5369
5370 static struct cfg80211_mbssid_elems *
5371 nl80211_parse_mbssid_elems(struct wiphy *wiphy, struct nlattr *attrs)
5372 {
5373         struct nlattr *nl_elems;
5374         struct cfg80211_mbssid_elems *elems;
5375         int rem_elems;
5376         u8 i = 0, num_elems = 0;
5377
5378         if (!wiphy->mbssid_max_interfaces)
5379                 return ERR_PTR(-EINVAL);
5380
5381         nla_for_each_nested(nl_elems, attrs, rem_elems)
5382                 num_elems++;
5383
5384         elems = kzalloc(struct_size(elems, elem, num_elems), GFP_KERNEL);
5385         if (!elems)
5386                 return ERR_PTR(-ENOMEM);
5387
5388         nla_for_each_nested(nl_elems, attrs, rem_elems) {
5389                 elems->elem[i].data = nla_data(nl_elems);
5390                 elems->elem[i].len = nla_len(nl_elems);
5391                 i++;
5392         }
5393         elems->cnt = num_elems;
5394         return elems;
5395 }
5396
5397 static int nl80211_parse_he_bss_color(struct nlattr *attrs,
5398                                       struct cfg80211_he_bss_color *he_bss_color)
5399 {
5400         struct nlattr *tb[NL80211_HE_BSS_COLOR_ATTR_MAX + 1];
5401         int err;
5402
5403         err = nla_parse_nested(tb, NL80211_HE_BSS_COLOR_ATTR_MAX, attrs,
5404                                he_bss_color_policy, NULL);
5405         if (err)
5406                 return err;
5407
5408         if (!tb[NL80211_HE_BSS_COLOR_ATTR_COLOR])
5409                 return -EINVAL;
5410
5411         he_bss_color->color =
5412                 nla_get_u8(tb[NL80211_HE_BSS_COLOR_ATTR_COLOR]);
5413         he_bss_color->enabled =
5414                 !nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_DISABLED]);
5415         he_bss_color->partial =
5416                 nla_get_flag(tb[NL80211_HE_BSS_COLOR_ATTR_PARTIAL]);
5417
5418         return 0;
5419 }
5420
5421 static int nl80211_parse_beacon(struct cfg80211_registered_device *rdev,
5422                                 struct nlattr *attrs[],
5423                                 struct cfg80211_beacon_data *bcn)
5424 {
5425         bool haveinfo = false;
5426         int err;
5427
5428         memset(bcn, 0, sizeof(*bcn));
5429
5430         bcn->link_id = nl80211_link_id(attrs);
5431
5432         if (attrs[NL80211_ATTR_BEACON_HEAD]) {
5433                 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
5434                 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
5435                 if (!bcn->head_len)
5436                         return -EINVAL;
5437                 haveinfo = true;
5438         }
5439
5440         if (attrs[NL80211_ATTR_BEACON_TAIL]) {
5441                 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
5442                 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
5443                 haveinfo = true;
5444         }
5445
5446         if (!haveinfo)
5447                 return -EINVAL;
5448
5449         if (attrs[NL80211_ATTR_IE]) {
5450                 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
5451                 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
5452         }
5453
5454         if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
5455                 bcn->proberesp_ies =
5456                         nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5457                 bcn->proberesp_ies_len =
5458                         nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
5459         }
5460
5461         if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
5462                 bcn->assocresp_ies =
5463                         nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5464                 bcn->assocresp_ies_len =
5465                         nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
5466         }
5467
5468         if (attrs[NL80211_ATTR_PROBE_RESP]) {
5469                 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
5470                 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
5471         }
5472
5473         if (attrs[NL80211_ATTR_FTM_RESPONDER]) {
5474                 struct nlattr *tb[NL80211_FTM_RESP_ATTR_MAX + 1];
5475
5476                 err = nla_parse_nested_deprecated(tb,
5477                                                   NL80211_FTM_RESP_ATTR_MAX,
5478                                                   attrs[NL80211_ATTR_FTM_RESPONDER],
5479                                                   NULL, NULL);
5480                 if (err)
5481                         return err;
5482
5483                 if (tb[NL80211_FTM_RESP_ATTR_ENABLED] &&
5484                     wiphy_ext_feature_isset(&rdev->wiphy,
5485                                             NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER))
5486                         bcn->ftm_responder = 1;
5487                 else
5488                         return -EOPNOTSUPP;
5489
5490                 if (tb[NL80211_FTM_RESP_ATTR_LCI]) {
5491                         bcn->lci = nla_data(tb[NL80211_FTM_RESP_ATTR_LCI]);
5492                         bcn->lci_len = nla_len(tb[NL80211_FTM_RESP_ATTR_LCI]);
5493                 }
5494
5495                 if (tb[NL80211_FTM_RESP_ATTR_CIVICLOC]) {
5496                         bcn->civicloc = nla_data(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5497                         bcn->civicloc_len = nla_len(tb[NL80211_FTM_RESP_ATTR_CIVICLOC]);
5498                 }
5499         } else {
5500                 bcn->ftm_responder = -1;
5501         }
5502
5503         if (attrs[NL80211_ATTR_HE_BSS_COLOR]) {
5504                 err = nl80211_parse_he_bss_color(attrs[NL80211_ATTR_HE_BSS_COLOR],
5505                                                  &bcn->he_bss_color);
5506                 if (err)
5507                         return err;
5508                 bcn->he_bss_color_valid = true;
5509         }
5510
5511         if (attrs[NL80211_ATTR_MBSSID_ELEMS]) {
5512                 struct cfg80211_mbssid_elems *mbssid =
5513                         nl80211_parse_mbssid_elems(&rdev->wiphy,
5514                                                    attrs[NL80211_ATTR_MBSSID_ELEMS]);
5515
5516                 if (IS_ERR(mbssid))
5517                         return PTR_ERR(mbssid);
5518
5519                 bcn->mbssid_ies = mbssid;
5520         }
5521
5522         return 0;
5523 }
5524
5525 static int nl80211_parse_he_obss_pd(struct nlattr *attrs,
5526                                     struct ieee80211_he_obss_pd *he_obss_pd)
5527 {
5528         struct nlattr *tb[NL80211_HE_OBSS_PD_ATTR_MAX + 1];
5529         int err;
5530
5531         err = nla_parse_nested(tb, NL80211_HE_OBSS_PD_ATTR_MAX, attrs,
5532                                he_obss_pd_policy, NULL);
5533         if (err)
5534                 return err;
5535
5536         if (!tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL])
5537                 return -EINVAL;
5538
5539         he_obss_pd->sr_ctrl = nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_SR_CTRL]);
5540
5541         if (tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET])
5542                 he_obss_pd->min_offset =
5543                         nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MIN_OFFSET]);
5544         if (tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET])
5545                 he_obss_pd->max_offset =
5546                         nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_MAX_OFFSET]);
5547         if (tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET])
5548                 he_obss_pd->non_srg_max_offset =
5549                         nla_get_u8(tb[NL80211_HE_OBSS_PD_ATTR_NON_SRG_MAX_OFFSET]);
5550
5551         if (he_obss_pd->min_offset > he_obss_pd->max_offset)
5552                 return -EINVAL;
5553
5554         if (tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP])
5555                 memcpy(he_obss_pd->bss_color_bitmap,
5556                        nla_data(tb[NL80211_HE_OBSS_PD_ATTR_BSS_COLOR_BITMAP]),
5557                        sizeof(he_obss_pd->bss_color_bitmap));
5558
5559         if (tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP])
5560                 memcpy(he_obss_pd->partial_bssid_bitmap,
5561                        nla_data(tb[NL80211_HE_OBSS_PD_ATTR_PARTIAL_BSSID_BITMAP]),
5562                        sizeof(he_obss_pd->partial_bssid_bitmap));
5563
5564         he_obss_pd->enable = true;
5565
5566         return 0;
5567 }
5568
5569 static int nl80211_parse_fils_discovery(struct cfg80211_registered_device *rdev,
5570                                         struct nlattr *attrs,
5571                                         struct cfg80211_ap_settings *params)
5572 {
5573         struct nlattr *tb[NL80211_FILS_DISCOVERY_ATTR_MAX + 1];
5574         int ret;
5575         struct cfg80211_fils_discovery *fd = &params->fils_discovery;
5576
5577         if (!wiphy_ext_feature_isset(&rdev->wiphy,
5578                                      NL80211_EXT_FEATURE_FILS_DISCOVERY))
5579                 return -EINVAL;
5580
5581         ret = nla_parse_nested(tb, NL80211_FILS_DISCOVERY_ATTR_MAX, attrs,
5582                                NULL, NULL);
5583         if (ret)
5584                 return ret;
5585
5586         if (!tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN] ||
5587             !tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX] ||
5588             !tb[NL80211_FILS_DISCOVERY_ATTR_TMPL])
5589                 return -EINVAL;
5590
5591         fd->tmpl_len = nla_len(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5592         fd->tmpl = nla_data(tb[NL80211_FILS_DISCOVERY_ATTR_TMPL]);
5593         fd->min_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MIN]);
5594         fd->max_interval = nla_get_u32(tb[NL80211_FILS_DISCOVERY_ATTR_INT_MAX]);
5595
5596         return 0;
5597 }
5598
5599 static int
5600 nl80211_parse_unsol_bcast_probe_resp(struct cfg80211_registered_device *rdev,
5601                                      struct nlattr *attrs,
5602                                      struct cfg80211_ap_settings *params)
5603 {
5604         struct nlattr *tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX + 1];
5605         int ret;
5606         struct cfg80211_unsol_bcast_probe_resp *presp =
5607                                         &params->unsol_bcast_probe_resp;
5608
5609         if (!wiphy_ext_feature_isset(&rdev->wiphy,
5610                                      NL80211_EXT_FEATURE_UNSOL_BCAST_PROBE_RESP))
5611                 return -EINVAL;
5612
5613         ret = nla_parse_nested(tb, NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_MAX,
5614                                attrs, NULL, NULL);
5615         if (ret)
5616                 return ret;
5617
5618         if (!tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT] ||
5619             !tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL])
5620                 return -EINVAL;
5621
5622         presp->tmpl = nla_data(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5623         presp->tmpl_len = nla_len(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_TMPL]);
5624         presp->interval = nla_get_u32(tb[NL80211_UNSOL_BCAST_PROBE_RESP_ATTR_INT]);
5625         return 0;
5626 }
5627
5628 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
5629                                             const struct element *rates)
5630 {
5631         int i;
5632
5633         if (!rates)
5634                 return;
5635
5636         for (i = 0; i < rates->datalen; i++) {
5637                 if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
5638                         params->ht_required = true;
5639                 if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
5640                         params->vht_required = true;
5641                 if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_HE_PHY)
5642                         params->he_required = true;
5643                 if (rates->data[i] == BSS_MEMBERSHIP_SELECTOR_SAE_H2E)
5644                         params->sae_h2e_required = true;
5645         }
5646 }
5647
5648 /*
5649  * Since the nl80211 API didn't include, from the beginning, attributes about
5650  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
5651  * benefit of drivers that rebuild IEs in the firmware.
5652  */
5653 static int nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
5654 {
5655         const struct cfg80211_beacon_data *bcn = &params->beacon;
5656         size_t ies_len = bcn->tail_len;
5657         const u8 *ies = bcn->tail;
5658         const struct element *rates;
5659         const struct element *cap;
5660
5661         rates = cfg80211_find_elem(WLAN_EID_SUPP_RATES, ies, ies_len);
5662         nl80211_check_ap_rate_selectors(params, rates);
5663
5664         rates = cfg80211_find_elem(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
5665         nl80211_check_ap_rate_selectors(params, rates);
5666
5667         cap = cfg80211_find_elem(WLAN_EID_HT_CAPABILITY, ies, ies_len);
5668         if (cap && cap->datalen >= sizeof(*params->ht_cap))
5669                 params->ht_cap = (void *)cap->data;
5670         cap = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
5671         if (cap && cap->datalen >= sizeof(*params->vht_cap))
5672                 params->vht_cap = (void *)cap->data;
5673         cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, ies, ies_len);
5674         if (cap && cap->datalen >= sizeof(*params->he_cap) + 1)
5675                 params->he_cap = (void *)(cap->data + 1);
5676         cap = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_OPERATION, ies, ies_len);
5677         if (cap && cap->datalen >= sizeof(*params->he_oper) + 1)
5678                 params->he_oper = (void *)(cap->data + 1);
5679         cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_CAPABILITY, ies, ies_len);
5680         if (cap) {
5681                 if (!cap->datalen)
5682                         return -EINVAL;
5683                 params->eht_cap = (void *)(cap->data + 1);
5684                 if (!ieee80211_eht_capa_size_ok((const u8 *)params->he_cap,
5685                                                 (const u8 *)params->eht_cap,
5686                                                 cap->datalen - 1, true))
5687                         return -EINVAL;
5688         }
5689         cap = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, ies, ies_len);
5690         if (cap) {
5691                 if (!cap->datalen)
5692                         return -EINVAL;
5693                 params->eht_oper = (void *)(cap->data + 1);
5694                 if (!ieee80211_eht_oper_size_ok((const u8 *)params->eht_oper,
5695                                                 cap->datalen - 1))
5696                         return -EINVAL;
5697         }
5698         return 0;
5699 }
5700
5701 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
5702                                    struct cfg80211_ap_settings *params)
5703 {
5704         struct wireless_dev *wdev;
5705
5706         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
5707                 if (wdev->iftype != NL80211_IFTYPE_AP &&
5708                     wdev->iftype != NL80211_IFTYPE_P2P_GO)
5709                         continue;
5710
5711                 if (!wdev->u.ap.preset_chandef.chan)
5712                         continue;
5713
5714                 params->chandef = wdev->u.ap.preset_chandef;
5715                 return true;
5716         }
5717
5718         return false;
5719 }
5720
5721 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
5722                                     enum nl80211_auth_type auth_type,
5723                                     enum nl80211_commands cmd)
5724 {
5725         if (auth_type > NL80211_AUTHTYPE_MAX)
5726                 return false;
5727
5728         switch (cmd) {
5729         case NL80211_CMD_AUTHENTICATE:
5730                 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5731                     auth_type == NL80211_AUTHTYPE_SAE)
5732                         return false;
5733                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
5734                                              NL80211_EXT_FEATURE_FILS_STA) &&
5735                     (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5736                      auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5737                      auth_type == NL80211_AUTHTYPE_FILS_PK))
5738                         return false;
5739                 return true;
5740         case NL80211_CMD_CONNECT:
5741                 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
5742                     !wiphy_ext_feature_isset(&rdev->wiphy,
5743                                              NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
5744                     auth_type == NL80211_AUTHTYPE_SAE)
5745                         return false;
5746
5747                 /* FILS with SK PFS or PK not supported yet */
5748                 if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5749                     auth_type == NL80211_AUTHTYPE_FILS_PK)
5750                         return false;
5751                 if (!wiphy_ext_feature_isset(
5752                             &rdev->wiphy,
5753                             NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
5754                     auth_type == NL80211_AUTHTYPE_FILS_SK)
5755                         return false;
5756                 return true;
5757         case NL80211_CMD_START_AP:
5758                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
5759                                              NL80211_EXT_FEATURE_SAE_OFFLOAD_AP) &&
5760                     auth_type == NL80211_AUTHTYPE_SAE)
5761                         return false;
5762                 /* FILS not supported yet */
5763                 if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
5764                     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
5765                     auth_type == NL80211_AUTHTYPE_FILS_PK)
5766                         return false;
5767                 return true;
5768         default:
5769                 return false;
5770         }
5771 }
5772
5773 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
5774 {
5775         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5776         unsigned int link_id = nl80211_link_id(info->attrs);
5777         struct net_device *dev = info->user_ptr[1];
5778         struct wireless_dev *wdev = dev->ieee80211_ptr;
5779         struct cfg80211_ap_settings *params;
5780         int err;
5781
5782         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5783             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5784                 return -EOPNOTSUPP;
5785
5786         if (!rdev->ops->start_ap)
5787                 return -EOPNOTSUPP;
5788
5789         if (wdev->links[link_id].ap.beacon_interval)
5790                 return -EALREADY;
5791
5792         /* these are required for START_AP */
5793         if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
5794             !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
5795             !info->attrs[NL80211_ATTR_BEACON_HEAD])
5796                 return -EINVAL;
5797
5798         params = kzalloc(sizeof(*params), GFP_KERNEL);
5799         if (!params)
5800                 return -ENOMEM;
5801
5802         err = nl80211_parse_beacon(rdev, info->attrs, &params->beacon);
5803         if (err)
5804                 goto out;
5805
5806         params->beacon_interval =
5807                 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5808         params->dtim_period =
5809                 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
5810
5811         err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
5812                                            params->beacon_interval);
5813         if (err)
5814                 goto out;
5815
5816         /*
5817          * In theory, some of these attributes should be required here
5818          * but since they were not used when the command was originally
5819          * added, keep them optional for old user space programs to let
5820          * them continue to work with drivers that do not need the
5821          * additional information -- drivers must check!
5822          */
5823         if (info->attrs[NL80211_ATTR_SSID]) {
5824                 params->ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5825                 params->ssid_len =
5826                         nla_len(info->attrs[NL80211_ATTR_SSID]);
5827                 if (params->ssid_len == 0) {
5828                         err = -EINVAL;
5829                         goto out;
5830                 }
5831
5832                 if (wdev->u.ap.ssid_len &&
5833                     (wdev->u.ap.ssid_len != params->ssid_len ||
5834                      memcmp(wdev->u.ap.ssid, params->ssid, params->ssid_len))) {
5835                         /* require identical SSID for MLO */
5836                         err = -EINVAL;
5837                         goto out;
5838                 }
5839         } else if (wdev->valid_links) {
5840                 /* require SSID for MLO */
5841                 err = -EINVAL;
5842                 goto out;
5843         }
5844
5845         if (info->attrs[NL80211_ATTR_HIDDEN_SSID])
5846                 params->hidden_ssid = nla_get_u32(
5847                         info->attrs[NL80211_ATTR_HIDDEN_SSID]);
5848
5849         params->privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
5850
5851         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5852                 params->auth_type = nla_get_u32(
5853                         info->attrs[NL80211_ATTR_AUTH_TYPE]);
5854                 if (!nl80211_valid_auth_type(rdev, params->auth_type,
5855                                              NL80211_CMD_START_AP)) {
5856                         err = -EINVAL;
5857                         goto out;
5858                 }
5859         } else
5860                 params->auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5861
5862         err = nl80211_crypto_settings(rdev, info, &params->crypto,
5863                                       NL80211_MAX_NR_CIPHER_SUITES);
5864         if (err)
5865                 goto out;
5866
5867         if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
5868                 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER)) {
5869                         err = -EOPNOTSUPP;
5870                         goto out;
5871                 }
5872                 params->inactivity_timeout = nla_get_u16(
5873                         info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
5874         }
5875
5876         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5877                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5878                         err = -EINVAL;
5879                         goto out;
5880                 }
5881                 params->p2p_ctwindow =
5882                         nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5883                 if (params->p2p_ctwindow != 0 &&
5884                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN)) {
5885                         err = -EINVAL;
5886                         goto out;
5887                 }
5888         }
5889
5890         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5891                 u8 tmp;
5892
5893                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
5894                         err = -EINVAL;
5895                         goto out;
5896                 }
5897                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5898                 params->p2p_opp_ps = tmp;
5899                 if (params->p2p_opp_ps != 0 &&
5900                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS)) {
5901                         err = -EINVAL;
5902                         goto out;
5903                 }
5904         }
5905
5906         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5907                 err = nl80211_parse_chandef(rdev, info, &params->chandef);
5908                 if (err)
5909                         goto out;
5910         } else if (wdev->valid_links) {
5911                 /* with MLD need to specify the channel configuration */
5912                 err = -EINVAL;
5913                 goto out;
5914         } else if (wdev->u.ap.preset_chandef.chan) {
5915                 params->chandef = wdev->u.ap.preset_chandef;
5916         } else if (!nl80211_get_ap_channel(rdev, params)) {
5917                 err = -EINVAL;
5918                 goto out;
5919         }
5920
5921         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params->chandef,
5922                                            wdev->iftype)) {
5923                 err = -EINVAL;
5924                 goto out;
5925         }
5926
5927         wdev_lock(wdev);
5928
5929         if (info->attrs[NL80211_ATTR_TX_RATES]) {
5930                 err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
5931                                                     NL80211_ATTR_TX_RATES,
5932                                                     &params->beacon_rate,
5933                                                     dev, false, link_id);
5934                 if (err)
5935                         goto out_unlock;
5936
5937                 err = validate_beacon_tx_rate(rdev, params->chandef.chan->band,
5938                                               &params->beacon_rate);
5939                 if (err)
5940                         goto out_unlock;
5941         }
5942
5943         if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
5944                 params->smps_mode =
5945                         nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
5946                 switch (params->smps_mode) {
5947                 case NL80211_SMPS_OFF:
5948                         break;
5949                 case NL80211_SMPS_STATIC:
5950                         if (!(rdev->wiphy.features &
5951                               NL80211_FEATURE_STATIC_SMPS)) {
5952                                 err = -EINVAL;
5953                                 goto out_unlock;
5954                         }
5955                         break;
5956                 case NL80211_SMPS_DYNAMIC:
5957                         if (!(rdev->wiphy.features &
5958                               NL80211_FEATURE_DYNAMIC_SMPS)) {
5959                                 err = -EINVAL;
5960                                 goto out_unlock;
5961                         }
5962                         break;
5963                 default:
5964                         err = -EINVAL;
5965                         goto out_unlock;
5966                 }
5967         } else {
5968                 params->smps_mode = NL80211_SMPS_OFF;
5969         }
5970
5971         params->pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
5972         if (params->pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
5973                 err = -EOPNOTSUPP;
5974                 goto out_unlock;
5975         }
5976
5977         if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
5978                 params->acl = parse_acl_data(&rdev->wiphy, info);
5979                 if (IS_ERR(params->acl)) {
5980                         err = PTR_ERR(params->acl);
5981                         params->acl = NULL;
5982                         goto out_unlock;
5983                 }
5984         }
5985
5986         params->twt_responder =
5987                     nla_get_flag(info->attrs[NL80211_ATTR_TWT_RESPONDER]);
5988
5989         if (info->attrs[NL80211_ATTR_HE_OBSS_PD]) {
5990                 err = nl80211_parse_he_obss_pd(
5991                                         info->attrs[NL80211_ATTR_HE_OBSS_PD],
5992                                         &params->he_obss_pd);
5993                 if (err)
5994                         goto out_unlock;
5995         }
5996
5997         if (info->attrs[NL80211_ATTR_FILS_DISCOVERY]) {
5998                 err = nl80211_parse_fils_discovery(rdev,
5999                                                    info->attrs[NL80211_ATTR_FILS_DISCOVERY],
6000                                                    params);
6001                 if (err)
6002                         goto out_unlock;
6003         }
6004
6005         if (info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP]) {
6006                 err = nl80211_parse_unsol_bcast_probe_resp(
6007                         rdev, info->attrs[NL80211_ATTR_UNSOL_BCAST_PROBE_RESP],
6008                         params);
6009                 if (err)
6010                         goto out_unlock;
6011         }
6012
6013         if (info->attrs[NL80211_ATTR_MBSSID_CONFIG]) {
6014                 err = nl80211_parse_mbssid_config(&rdev->wiphy, dev,
6015                                                   info->attrs[NL80211_ATTR_MBSSID_CONFIG],
6016                                                   &params->mbssid_config,
6017                                                   params->beacon.mbssid_ies ?
6018                                                         params->beacon.mbssid_ies->cnt :
6019                                                         0);
6020                 if (err)
6021                         goto out_unlock;
6022         }
6023
6024         err = nl80211_calculate_ap_params(params);
6025         if (err)
6026                 goto out_unlock;
6027
6028         if (info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS])
6029                 params->flags = nla_get_u32(
6030                         info->attrs[NL80211_ATTR_AP_SETTINGS_FLAGS]);
6031         else if (info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])
6032                 params->flags |= NL80211_AP_SETTINGS_EXTERNAL_AUTH_SUPPORT;
6033
6034         if (wdev->conn_owner_nlportid &&
6035             info->attrs[NL80211_ATTR_SOCKET_OWNER] &&
6036             wdev->conn_owner_nlportid != info->snd_portid) {
6037                 err = -EINVAL;
6038                 goto out_unlock;
6039         }
6040
6041         /* FIXME: validate MLO/link-id against driver capabilities */
6042
6043         err = rdev_start_ap(rdev, dev, params);
6044         if (!err) {
6045                 wdev->links[link_id].ap.beacon_interval = params->beacon_interval;
6046                 wdev->links[link_id].ap.chandef = params->chandef;
6047                 wdev->u.ap.ssid_len = params->ssid_len;
6048                 memcpy(wdev->u.ap.ssid, params->ssid,
6049                        params->ssid_len);
6050
6051                 if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
6052                         wdev->conn_owner_nlportid = info->snd_portid;
6053         }
6054 out_unlock:
6055         wdev_unlock(wdev);
6056 out:
6057         kfree(params->acl);
6058         kfree(params->beacon.mbssid_ies);
6059         if (params->mbssid_config.tx_wdev &&
6060             params->mbssid_config.tx_wdev->netdev &&
6061             params->mbssid_config.tx_wdev->netdev != dev)
6062                 dev_put(params->mbssid_config.tx_wdev->netdev);
6063         kfree(params);
6064
6065         return err;
6066 }
6067
6068 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
6069 {
6070         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6071         unsigned int link_id = nl80211_link_id(info->attrs);
6072         struct net_device *dev = info->user_ptr[1];
6073         struct wireless_dev *wdev = dev->ieee80211_ptr;
6074         struct cfg80211_beacon_data params;
6075         int err;
6076
6077         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6078             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6079                 return -EOPNOTSUPP;
6080
6081         if (!rdev->ops->change_beacon)
6082                 return -EOPNOTSUPP;
6083
6084         if (!wdev->links[link_id].ap.beacon_interval)
6085                 return -EINVAL;
6086
6087         err = nl80211_parse_beacon(rdev, info->attrs, &params);
6088         if (err)
6089                 goto out;
6090
6091         wdev_lock(wdev);
6092         err = rdev_change_beacon(rdev, dev, &params);
6093         wdev_unlock(wdev);
6094
6095 out:
6096         kfree(params.mbssid_ies);
6097         return err;
6098 }
6099
6100 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
6101 {
6102         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6103         unsigned int link_id = nl80211_link_id(info->attrs);
6104         struct net_device *dev = info->user_ptr[1];
6105
6106         return cfg80211_stop_ap(rdev, dev, link_id, false);
6107 }
6108
6109 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
6110         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
6111         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
6112         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
6113         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
6114         [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
6115         [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
6116 };
6117
6118 static int parse_station_flags(struct genl_info *info,
6119                                enum nl80211_iftype iftype,
6120                                struct station_parameters *params)
6121 {
6122         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
6123         struct nlattr *nla;
6124         int flag;
6125
6126         /*
6127          * Try parsing the new attribute first so userspace
6128          * can specify both for older kernels.
6129          */
6130         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
6131         if (nla) {
6132                 struct nl80211_sta_flag_update *sta_flags;
6133
6134                 sta_flags = nla_data(nla);
6135                 params->sta_flags_mask = sta_flags->mask;
6136                 params->sta_flags_set = sta_flags->set;
6137                 params->sta_flags_set &= params->sta_flags_mask;
6138                 if ((params->sta_flags_mask |
6139                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
6140                         return -EINVAL;
6141                 return 0;
6142         }
6143
6144         /* if present, parse the old attribute */
6145
6146         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
6147         if (!nla)
6148                 return 0;
6149
6150         if (nla_parse_nested_deprecated(flags, NL80211_STA_FLAG_MAX, nla, sta_flags_policy, info->extack))
6151                 return -EINVAL;
6152
6153         /*
6154          * Only allow certain flags for interface types so that
6155          * other attributes are silently ignored. Remember that
6156          * this is backward compatibility code with old userspace
6157          * and shouldn't be hit in other cases anyway.
6158          */
6159         switch (iftype) {
6160         case NL80211_IFTYPE_AP:
6161         case NL80211_IFTYPE_AP_VLAN:
6162         case NL80211_IFTYPE_P2P_GO:
6163                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6164                                          BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6165                                          BIT(NL80211_STA_FLAG_WME) |
6166                                          BIT(NL80211_STA_FLAG_MFP);
6167                 break;
6168         case NL80211_IFTYPE_P2P_CLIENT:
6169         case NL80211_IFTYPE_STATION:
6170                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
6171                                          BIT(NL80211_STA_FLAG_TDLS_PEER);
6172                 break;
6173         case NL80211_IFTYPE_MESH_POINT:
6174                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6175                                          BIT(NL80211_STA_FLAG_MFP) |
6176                                          BIT(NL80211_STA_FLAG_AUTHORIZED);
6177                 break;
6178         default:
6179                 return -EINVAL;
6180         }
6181
6182         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
6183                 if (flags[flag]) {
6184                         params->sta_flags_set |= (1<<flag);
6185
6186                         /* no longer support new API additions in old API */
6187                         if (flag > NL80211_STA_FLAG_MAX_OLD_API)
6188                                 return -EINVAL;
6189                 }
6190         }
6191
6192         return 0;
6193 }
6194
6195 bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info, int attr)
6196 {
6197         struct nlattr *rate;
6198         u32 bitrate;
6199         u16 bitrate_compat;
6200         enum nl80211_rate_info rate_flg;
6201
6202         rate = nla_nest_start_noflag(msg, attr);
6203         if (!rate)
6204                 return false;
6205
6206         /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
6207         bitrate = cfg80211_calculate_bitrate(info);
6208         /* report 16-bit bitrate only if we can */
6209         bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
6210         if (bitrate > 0 &&
6211             nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
6212                 return false;
6213         if (bitrate_compat > 0 &&
6214             nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
6215                 return false;
6216
6217         switch (info->bw) {
6218         case RATE_INFO_BW_5:
6219                 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
6220                 break;
6221         case RATE_INFO_BW_10:
6222                 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
6223                 break;
6224         default:
6225                 WARN_ON(1);
6226                 fallthrough;
6227         case RATE_INFO_BW_20:
6228                 rate_flg = 0;
6229                 break;
6230         case RATE_INFO_BW_40:
6231                 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
6232                 break;
6233         case RATE_INFO_BW_80:
6234                 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
6235                 break;
6236         case RATE_INFO_BW_160:
6237                 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
6238                 break;
6239         case RATE_INFO_BW_HE_RU:
6240                 rate_flg = 0;
6241                 WARN_ON(!(info->flags & RATE_INFO_FLAGS_HE_MCS));
6242                 break;
6243         case RATE_INFO_BW_320:
6244                 rate_flg = NL80211_RATE_INFO_320_MHZ_WIDTH;
6245                 break;
6246         case RATE_INFO_BW_EHT_RU:
6247                 rate_flg = 0;
6248                 WARN_ON(!(info->flags & RATE_INFO_FLAGS_EHT_MCS));
6249                 break;
6250         }
6251
6252         if (rate_flg && nla_put_flag(msg, rate_flg))
6253                 return false;
6254
6255         if (info->flags & RATE_INFO_FLAGS_MCS) {
6256                 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
6257                         return false;
6258                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6259                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6260                         return false;
6261         } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
6262                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
6263                         return false;
6264                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
6265                         return false;
6266                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
6267                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
6268                         return false;
6269         } else if (info->flags & RATE_INFO_FLAGS_HE_MCS) {
6270                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_MCS, info->mcs))
6271                         return false;
6272                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_NSS, info->nss))
6273                         return false;
6274                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_GI, info->he_gi))
6275                         return false;
6276                 if (nla_put_u8(msg, NL80211_RATE_INFO_HE_DCM, info->he_dcm))
6277                         return false;
6278                 if (info->bw == RATE_INFO_BW_HE_RU &&
6279                     nla_put_u8(msg, NL80211_RATE_INFO_HE_RU_ALLOC,
6280                                info->he_ru_alloc))
6281                         return false;
6282         } else if (info->flags & RATE_INFO_FLAGS_EHT_MCS) {
6283                 if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_MCS, info->mcs))
6284                         return false;
6285                 if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_NSS, info->nss))
6286                         return false;
6287                 if (nla_put_u8(msg, NL80211_RATE_INFO_EHT_GI, info->eht_gi))
6288                         return false;
6289                 if (info->bw == RATE_INFO_BW_EHT_RU &&
6290                     nla_put_u8(msg, NL80211_RATE_INFO_EHT_RU_ALLOC,
6291                                info->eht_ru_alloc))
6292                         return false;
6293         }
6294
6295         nla_nest_end(msg, rate);
6296         return true;
6297 }
6298
6299 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
6300                                int id)
6301 {
6302         void *attr;
6303         int i = 0;
6304
6305         if (!mask)
6306                 return true;
6307
6308         attr = nla_nest_start_noflag(msg, id);
6309         if (!attr)
6310                 return false;
6311
6312         for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
6313                 if (!(mask & BIT(i)))
6314                         continue;
6315
6316                 if (nla_put_u8(msg, i, signal[i]))
6317                         return false;
6318         }
6319
6320         nla_nest_end(msg, attr);
6321
6322         return true;
6323 }
6324
6325 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
6326                                 u32 seq, int flags,
6327                                 struct cfg80211_registered_device *rdev,
6328                                 struct net_device *dev,
6329                                 const u8 *mac_addr, struct station_info *sinfo)
6330 {
6331         void *hdr;
6332         struct nlattr *sinfoattr, *bss_param;
6333
6334         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
6335         if (!hdr) {
6336                 cfg80211_sinfo_release_content(sinfo);
6337                 return -1;
6338         }
6339
6340         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
6341             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
6342             nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
6343                 goto nla_put_failure;
6344
6345         sinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_STA_INFO);
6346         if (!sinfoattr)
6347                 goto nla_put_failure;
6348
6349 #define PUT_SINFO(attr, memb, type) do {                                \
6350         BUILD_BUG_ON(sizeof(type) == sizeof(u64));                      \
6351         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
6352             nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,            \
6353                              sinfo->memb))                              \
6354                 goto nla_put_failure;                                   \
6355         } while (0)
6356 #define PUT_SINFO_U64(attr, memb) do {                                  \
6357         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_ ## attr) &&       \
6358             nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,           \
6359                               sinfo->memb, NL80211_STA_INFO_PAD))       \
6360                 goto nla_put_failure;                                   \
6361         } while (0)
6362
6363         PUT_SINFO(CONNECTED_TIME, connected_time, u32);
6364         PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
6365         PUT_SINFO_U64(ASSOC_AT_BOOTTIME, assoc_at);
6366
6367         if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
6368                              BIT_ULL(NL80211_STA_INFO_RX_BYTES64)) &&
6369             nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
6370                         (u32)sinfo->rx_bytes))
6371                 goto nla_put_failure;
6372
6373         if (sinfo->filled & (BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
6374                              BIT_ULL(NL80211_STA_INFO_TX_BYTES64)) &&
6375             nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
6376                         (u32)sinfo->tx_bytes))
6377                 goto nla_put_failure;
6378
6379         PUT_SINFO_U64(RX_BYTES64, rx_bytes);
6380         PUT_SINFO_U64(TX_BYTES64, tx_bytes);
6381         PUT_SINFO(LLID, llid, u16);
6382         PUT_SINFO(PLID, plid, u16);
6383         PUT_SINFO(PLINK_STATE, plink_state, u8);
6384         PUT_SINFO_U64(RX_DURATION, rx_duration);
6385         PUT_SINFO_U64(TX_DURATION, tx_duration);
6386
6387         if (wiphy_ext_feature_isset(&rdev->wiphy,
6388                                     NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
6389                 PUT_SINFO(AIRTIME_WEIGHT, airtime_weight, u16);
6390
6391         switch (rdev->wiphy.signal_type) {
6392         case CFG80211_SIGNAL_TYPE_MBM:
6393                 PUT_SINFO(SIGNAL, signal, u8);
6394                 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
6395                 break;
6396         default:
6397                 break;
6398         }
6399         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) {
6400                 if (!nl80211_put_signal(msg, sinfo->chains,
6401                                         sinfo->chain_signal,
6402                                         NL80211_STA_INFO_CHAIN_SIGNAL))
6403                         goto nla_put_failure;
6404         }
6405         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
6406                 if (!nl80211_put_signal(msg, sinfo->chains,
6407                                         sinfo->chain_signal_avg,
6408                                         NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
6409                         goto nla_put_failure;
6410         }
6411         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE)) {
6412                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
6413                                           NL80211_STA_INFO_TX_BITRATE))
6414                         goto nla_put_failure;
6415         }
6416         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) {
6417                 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
6418                                           NL80211_STA_INFO_RX_BITRATE))
6419                         goto nla_put_failure;
6420         }
6421
6422         PUT_SINFO(RX_PACKETS, rx_packets, u32);
6423         PUT_SINFO(TX_PACKETS, tx_packets, u32);
6424         PUT_SINFO(TX_RETRIES, tx_retries, u32);
6425         PUT_SINFO(TX_FAILED, tx_failed, u32);
6426         PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
6427         PUT_SINFO(AIRTIME_LINK_METRIC, airtime_link_metric, u32);
6428         PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
6429         PUT_SINFO(LOCAL_PM, local_pm, u32);
6430         PUT_SINFO(PEER_PM, peer_pm, u32);
6431         PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
6432         PUT_SINFO(CONNECTED_TO_GATE, connected_to_gate, u8);
6433         PUT_SINFO(CONNECTED_TO_AS, connected_to_as, u8);
6434
6435         if (sinfo->filled & BIT_ULL(NL80211_STA_INFO_BSS_PARAM)) {
6436                 bss_param = nla_nest_start_noflag(msg,
6437                                                   NL80211_STA_INFO_BSS_PARAM);
6438                 if (!bss_param)
6439                         goto nla_put_failure;
6440
6441                 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
6442                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
6443                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
6444                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
6445                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
6446                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
6447                     nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
6448                                sinfo->bss_param.dtim_period) ||
6449                     nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
6450                                 sinfo->bss_param.beacon_interval))
6451                         goto nla_put_failure;
6452
6453                 nla_nest_end(msg, bss_param);
6454         }
6455         if ((sinfo->filled & BIT_ULL(NL80211_STA_INFO_STA_FLAGS)) &&
6456             nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
6457                     sizeof(struct nl80211_sta_flag_update),
6458                     &sinfo->sta_flags))
6459                 goto nla_put_failure;
6460
6461         PUT_SINFO_U64(T_OFFSET, t_offset);
6462         PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
6463         PUT_SINFO_U64(BEACON_RX, rx_beacon);
6464         PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
6465         PUT_SINFO(RX_MPDUS, rx_mpdu_count, u32);
6466         PUT_SINFO(FCS_ERROR_COUNT, fcs_err_count, u32);
6467         if (wiphy_ext_feature_isset(&rdev->wiphy,
6468                                     NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT)) {
6469                 PUT_SINFO(ACK_SIGNAL, ack_signal, u8);
6470                 PUT_SINFO(ACK_SIGNAL_AVG, avg_ack_signal, s8);
6471         }
6472
6473 #undef PUT_SINFO
6474 #undef PUT_SINFO_U64
6475
6476         if (sinfo->pertid) {
6477                 struct nlattr *tidsattr;
6478                 int tid;
6479
6480                 tidsattr = nla_nest_start_noflag(msg,
6481                                                  NL80211_STA_INFO_TID_STATS);
6482                 if (!tidsattr)
6483                         goto nla_put_failure;
6484
6485                 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
6486                         struct cfg80211_tid_stats *tidstats;
6487                         struct nlattr *tidattr;
6488
6489                         tidstats = &sinfo->pertid[tid];
6490
6491                         if (!tidstats->filled)
6492                                 continue;
6493
6494                         tidattr = nla_nest_start_noflag(msg, tid + 1);
6495                         if (!tidattr)
6496                                 goto nla_put_failure;
6497
6498 #define PUT_TIDVAL_U64(attr, memb) do {                                 \
6499         if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&       \
6500             nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,          \
6501                               tidstats->memb, NL80211_TID_STATS_PAD))   \
6502                 goto nla_put_failure;                                   \
6503         } while (0)
6504
6505                         PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
6506                         PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
6507                         PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
6508                         PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
6509
6510 #undef PUT_TIDVAL_U64
6511                         if ((tidstats->filled &
6512                              BIT(NL80211_TID_STATS_TXQ_STATS)) &&
6513                             !nl80211_put_txq_stats(msg, &tidstats->txq_stats,
6514                                                    NL80211_TID_STATS_TXQ_STATS))
6515                                 goto nla_put_failure;
6516
6517                         nla_nest_end(msg, tidattr);
6518                 }
6519
6520                 nla_nest_end(msg, tidsattr);
6521         }
6522
6523         nla_nest_end(msg, sinfoattr);
6524
6525         if (sinfo->assoc_req_ies_len &&
6526             nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
6527                     sinfo->assoc_req_ies))
6528                 goto nla_put_failure;
6529
6530         cfg80211_sinfo_release_content(sinfo);
6531         genlmsg_end(msg, hdr);
6532         return 0;
6533
6534  nla_put_failure:
6535         cfg80211_sinfo_release_content(sinfo);
6536         genlmsg_cancel(msg, hdr);
6537         return -EMSGSIZE;
6538 }
6539
6540 static int nl80211_dump_station(struct sk_buff *skb,
6541                                 struct netlink_callback *cb)
6542 {
6543         struct station_info sinfo;
6544         struct cfg80211_registered_device *rdev;
6545         struct wireless_dev *wdev;
6546         u8 mac_addr[ETH_ALEN];
6547         int sta_idx = cb->args[2];
6548         int err;
6549
6550         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
6551         if (err)
6552                 return err;
6553         /* nl80211_prepare_wdev_dump acquired it in the successful case */
6554         __acquire(&rdev->wiphy.mtx);
6555
6556         if (!wdev->netdev) {
6557                 err = -EINVAL;
6558                 goto out_err;
6559         }
6560
6561         if (!rdev->ops->dump_station) {
6562                 err = -EOPNOTSUPP;
6563                 goto out_err;
6564         }
6565
6566         while (1) {
6567                 memset(&sinfo, 0, sizeof(sinfo));
6568                 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
6569                                         mac_addr, &sinfo);
6570                 if (err == -ENOENT)
6571                         break;
6572                 if (err)
6573                         goto out_err;
6574
6575                 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
6576                                 NETLINK_CB(cb->skb).portid,
6577                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6578                                 rdev, wdev->netdev, mac_addr,
6579                                 &sinfo) < 0)
6580                         goto out;
6581
6582                 sta_idx++;
6583         }
6584
6585  out:
6586         cb->args[2] = sta_idx;
6587         err = skb->len;
6588  out_err:
6589         wiphy_unlock(&rdev->wiphy);
6590
6591         return err;
6592 }
6593
6594 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
6595 {
6596         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6597         struct net_device *dev = info->user_ptr[1];
6598         struct station_info sinfo;
6599         struct sk_buff *msg;
6600         u8 *mac_addr = NULL;
6601         int err;
6602
6603         memset(&sinfo, 0, sizeof(sinfo));
6604
6605         if (!info->attrs[NL80211_ATTR_MAC])
6606                 return -EINVAL;
6607
6608         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6609
6610         if (!rdev->ops->get_station)
6611                 return -EOPNOTSUPP;
6612
6613         err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
6614         if (err)
6615                 return err;
6616
6617         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6618         if (!msg) {
6619                 cfg80211_sinfo_release_content(&sinfo);
6620                 return -ENOMEM;
6621         }
6622
6623         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
6624                                  info->snd_portid, info->snd_seq, 0,
6625                                  rdev, dev, mac_addr, &sinfo) < 0) {
6626                 nlmsg_free(msg);
6627                 return -ENOBUFS;
6628         }
6629
6630         return genlmsg_reply(msg, info);
6631 }
6632
6633 int cfg80211_check_station_change(struct wiphy *wiphy,
6634                                   struct station_parameters *params,
6635                                   enum cfg80211_station_type statype)
6636 {
6637         if (params->listen_interval != -1 &&
6638             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6639                 return -EINVAL;
6640
6641         if (params->support_p2p_ps != -1 &&
6642             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6643                 return -EINVAL;
6644
6645         if (params->aid &&
6646             !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
6647             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
6648                 return -EINVAL;
6649
6650         /* When you run into this, adjust the code below for the new flag */
6651         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
6652
6653         switch (statype) {
6654         case CFG80211_STA_MESH_PEER_KERNEL:
6655         case CFG80211_STA_MESH_PEER_USER:
6656                 /*
6657                  * No ignoring the TDLS flag here -- the userspace mesh
6658                  * code doesn't have the bug of including TDLS in the
6659                  * mask everywhere.
6660                  */
6661                 if (params->sta_flags_mask &
6662                                 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6663                                   BIT(NL80211_STA_FLAG_MFP) |
6664                                   BIT(NL80211_STA_FLAG_AUTHORIZED)))
6665                         return -EINVAL;
6666                 break;
6667         case CFG80211_STA_TDLS_PEER_SETUP:
6668         case CFG80211_STA_TDLS_PEER_ACTIVE:
6669                 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
6670                         return -EINVAL;
6671                 /* ignore since it can't change */
6672                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6673                 break;
6674         default:
6675                 /* disallow mesh-specific things */
6676                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
6677                         return -EINVAL;
6678                 if (params->local_pm)
6679                         return -EINVAL;
6680                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6681                         return -EINVAL;
6682         }
6683
6684         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6685             statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
6686                 /* TDLS can't be set, ... */
6687                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
6688                         return -EINVAL;
6689                 /*
6690                  * ... but don't bother the driver with it. This works around
6691                  * a hostapd/wpa_supplicant issue -- it always includes the
6692                  * TLDS_PEER flag in the mask even for AP mode.
6693                  */
6694                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
6695         }
6696
6697         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
6698             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6699                 /* reject other things that can't change */
6700                 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
6701                         return -EINVAL;
6702                 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
6703                         return -EINVAL;
6704                 if (params->link_sta_params.supported_rates)
6705                         return -EINVAL;
6706                 if (params->ext_capab || params->link_sta_params.ht_capa ||
6707                     params->link_sta_params.vht_capa ||
6708                     params->link_sta_params.he_capa ||
6709                     params->link_sta_params.eht_capa)
6710                         return -EINVAL;
6711         }
6712
6713         if (statype != CFG80211_STA_AP_CLIENT &&
6714             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
6715                 if (params->vlan)
6716                         return -EINVAL;
6717         }
6718
6719         switch (statype) {
6720         case CFG80211_STA_AP_MLME_CLIENT:
6721                 /* Use this only for authorizing/unauthorizing a station */
6722                 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
6723                         return -EOPNOTSUPP;
6724                 break;
6725         case CFG80211_STA_AP_CLIENT:
6726         case CFG80211_STA_AP_CLIENT_UNASSOC:
6727                 /* accept only the listed bits */
6728                 if (params->sta_flags_mask &
6729                                 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6730                                   BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6731                                   BIT(NL80211_STA_FLAG_ASSOCIATED) |
6732                                   BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
6733                                   BIT(NL80211_STA_FLAG_WME) |
6734                                   BIT(NL80211_STA_FLAG_MFP)))
6735                         return -EINVAL;
6736
6737                 /* but authenticated/associated only if driver handles it */
6738                 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
6739                     params->sta_flags_mask &
6740                                 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
6741                                  BIT(NL80211_STA_FLAG_ASSOCIATED)))
6742                         return -EINVAL;
6743                 break;
6744         case CFG80211_STA_IBSS:
6745         case CFG80211_STA_AP_STA:
6746                 /* reject any changes other than AUTHORIZED */
6747                 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
6748                         return -EINVAL;
6749                 break;
6750         case CFG80211_STA_TDLS_PEER_SETUP:
6751                 /* reject any changes other than AUTHORIZED or WME */
6752                 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
6753                                                BIT(NL80211_STA_FLAG_WME)))
6754                         return -EINVAL;
6755                 /* force (at least) rates when authorizing */
6756                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
6757                     !params->link_sta_params.supported_rates)
6758                         return -EINVAL;
6759                 break;
6760         case CFG80211_STA_TDLS_PEER_ACTIVE:
6761                 /* reject any changes */
6762                 return -EINVAL;
6763         case CFG80211_STA_MESH_PEER_KERNEL:
6764                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
6765                         return -EINVAL;
6766                 break;
6767         case CFG80211_STA_MESH_PEER_USER:
6768                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
6769                     params->plink_action != NL80211_PLINK_ACTION_BLOCK)
6770                         return -EINVAL;
6771                 break;
6772         }
6773
6774         /*
6775          * Older kernel versions ignored this attribute entirely, so don't
6776          * reject attempts to update it but mark it as unused instead so the
6777          * driver won't look at the data.
6778          */
6779         if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
6780             statype != CFG80211_STA_TDLS_PEER_SETUP)
6781                 params->link_sta_params.opmode_notif_used = false;
6782
6783         return 0;
6784 }
6785 EXPORT_SYMBOL(cfg80211_check_station_change);
6786
6787 /*
6788  * Get vlan interface making sure it is running and on the right wiphy.
6789  */
6790 static struct net_device *get_vlan(struct genl_info *info,
6791                                    struct cfg80211_registered_device *rdev)
6792 {
6793         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
6794         struct net_device *v;
6795         int ret;
6796
6797         if (!vlanattr)
6798                 return NULL;
6799
6800         v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
6801         if (!v)
6802                 return ERR_PTR(-ENODEV);
6803
6804         if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
6805                 ret = -EINVAL;
6806                 goto error;
6807         }
6808
6809         if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6810             v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6811             v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
6812                 ret = -EINVAL;
6813                 goto error;
6814         }
6815
6816         if (!netif_running(v)) {
6817                 ret = -ENETDOWN;
6818                 goto error;
6819         }
6820
6821         return v;
6822  error:
6823         dev_put(v);
6824         return ERR_PTR(ret);
6825 }
6826
6827 static int nl80211_parse_sta_wme(struct genl_info *info,
6828                                  struct station_parameters *params)
6829 {
6830         struct nlattr *tb[NL80211_STA_WME_MAX + 1];
6831         struct nlattr *nla;
6832         int err;
6833
6834         /* parse WME attributes if present */
6835         if (!info->attrs[NL80211_ATTR_STA_WME])
6836                 return 0;
6837
6838         nla = info->attrs[NL80211_ATTR_STA_WME];
6839         err = nla_parse_nested_deprecated(tb, NL80211_STA_WME_MAX, nla,
6840                                           nl80211_sta_wme_policy,
6841                                           info->extack);
6842         if (err)
6843                 return err;
6844
6845         if (tb[NL80211_STA_WME_UAPSD_QUEUES])
6846                 params->uapsd_queues = nla_get_u8(
6847                         tb[NL80211_STA_WME_UAPSD_QUEUES]);
6848         if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
6849                 return -EINVAL;
6850
6851         if (tb[NL80211_STA_WME_MAX_SP])
6852                 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
6853
6854         if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
6855                 return -EINVAL;
6856
6857         params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
6858
6859         return 0;
6860 }
6861
6862 static int nl80211_parse_sta_channel_info(struct genl_info *info,
6863                                       struct station_parameters *params)
6864 {
6865         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
6866                 params->supported_channels =
6867                      nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6868                 params->supported_channels_len =
6869                      nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
6870                 /*
6871                  * Need to include at least one (first channel, number of
6872                  * channels) tuple for each subband (checked in policy),
6873                  * and must have proper tuples for the rest of the data as well.
6874                  */
6875                 if (params->supported_channels_len % 2)
6876                         return -EINVAL;
6877         }
6878
6879         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
6880                 params->supported_oper_classes =
6881                  nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6882                 params->supported_oper_classes_len =
6883                   nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
6884         }
6885         return 0;
6886 }
6887
6888 static int nl80211_set_station_tdls(struct genl_info *info,
6889                                     struct station_parameters *params)
6890 {
6891         int err;
6892         /* Dummy STA entry gets updated once the peer capabilities are known */
6893         if (info->attrs[NL80211_ATTR_PEER_AID])
6894                 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
6895         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
6896                 params->link_sta_params.ht_capa =
6897                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
6898         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
6899                 params->link_sta_params.vht_capa =
6900                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
6901         if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
6902                 params->link_sta_params.he_capa =
6903                         nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6904                 params->link_sta_params.he_capa_len =
6905                         nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
6906
6907                 if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
6908                         params->link_sta_params.eht_capa =
6909                                 nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6910                         params->link_sta_params.eht_capa_len =
6911                                 nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
6912
6913                         if (!ieee80211_eht_capa_size_ok((const u8 *)params->link_sta_params.he_capa,
6914                                                         (const u8 *)params->link_sta_params.eht_capa,
6915                                                         params->link_sta_params.eht_capa_len,
6916                                                         false))
6917                                 return -EINVAL;
6918                 }
6919         }
6920
6921         err = nl80211_parse_sta_channel_info(info, params);
6922         if (err)
6923                 return err;
6924
6925         return nl80211_parse_sta_wme(info, params);
6926 }
6927
6928 static int nl80211_parse_sta_txpower_setting(struct genl_info *info,
6929                                              struct sta_txpwr *txpwr,
6930                                              bool *txpwr_set)
6931 {
6932         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6933         int idx;
6934
6935         if (info->attrs[NL80211_ATTR_STA_TX_POWER_SETTING]) {
6936                 if (!rdev->ops->set_tx_power ||
6937                     !wiphy_ext_feature_isset(&rdev->wiphy,
6938                                          NL80211_EXT_FEATURE_STA_TX_PWR))
6939                         return -EOPNOTSUPP;
6940
6941                 idx = NL80211_ATTR_STA_TX_POWER_SETTING;
6942                 txpwr->type = nla_get_u8(info->attrs[idx]);
6943
6944                 if (txpwr->type == NL80211_TX_POWER_LIMITED) {
6945                         idx = NL80211_ATTR_STA_TX_POWER;
6946
6947                         if (info->attrs[idx])
6948                                 txpwr->power = nla_get_s16(info->attrs[idx]);
6949                         else
6950                                 return -EINVAL;
6951                 }
6952
6953                 *txpwr_set = true;
6954         } else {
6955                 *txpwr_set = false;
6956         }
6957
6958         return 0;
6959 }
6960
6961 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
6962 {
6963         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6964         struct net_device *dev = info->user_ptr[1];
6965         struct station_parameters params;
6966         u8 *mac_addr;
6967         int err;
6968
6969         memset(&params, 0, sizeof(params));
6970
6971         if (!rdev->ops->change_station)
6972                 return -EOPNOTSUPP;
6973
6974         /*
6975          * AID and listen_interval properties can be set only for unassociated
6976          * station. Include these parameters here and will check them in
6977          * cfg80211_check_station_change().
6978          */
6979         if (info->attrs[NL80211_ATTR_STA_AID])
6980                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
6981
6982         if (info->attrs[NL80211_ATTR_VLAN_ID])
6983                 params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
6984
6985         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
6986                 params.listen_interval =
6987                      nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
6988         else
6989                 params.listen_interval = -1;
6990
6991         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS])
6992                 params.support_p2p_ps =
6993                         nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
6994         else
6995                 params.support_p2p_ps = -1;
6996
6997         if (!info->attrs[NL80211_ATTR_MAC])
6998                 return -EINVAL;
6999
7000         params.link_sta_params.link_id =
7001                 nl80211_link_id_or_invalid(info->attrs);
7002
7003         if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7004                 /* If MLD_ADDR attribute is set then this is an MLD station
7005                  * and the MLD_ADDR attribute holds the MLD address and the
7006                  * MAC attribute holds for the LINK address.
7007                  * In that case, the link_id is also expected to be valid.
7008                  */
7009                 if (params.link_sta_params.link_id < 0)
7010                         return -EINVAL;
7011
7012                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7013                 params.link_sta_params.mld_mac = mac_addr;
7014                 params.link_sta_params.link_mac =
7015                         nla_data(info->attrs[NL80211_ATTR_MAC]);
7016                 if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7017                         return -EINVAL;
7018         } else {
7019                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7020         }
7021
7022
7023         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
7024                 params.link_sta_params.supported_rates =
7025                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7026                 params.link_sta_params.supported_rates_len =
7027                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7028         }
7029
7030         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7031                 params.capability =
7032                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7033                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7034         }
7035
7036         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7037                 params.ext_capab =
7038                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7039                 params.ext_capab_len =
7040                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7041         }
7042
7043         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7044                 return -EINVAL;
7045
7046         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7047                 params.plink_action =
7048                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7049
7050         if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
7051                 params.plink_state =
7052                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
7053                 if (info->attrs[NL80211_ATTR_MESH_PEER_AID])
7054                         params.peer_aid = nla_get_u16(
7055                                 info->attrs[NL80211_ATTR_MESH_PEER_AID]);
7056                 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
7057         }
7058
7059         if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE])
7060                 params.local_pm = nla_get_u32(
7061                         info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
7062
7063         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7064                 params.link_sta_params.opmode_notif_used = true;
7065                 params.link_sta_params.opmode_notif =
7066                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7067         }
7068
7069         if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7070                 params.link_sta_params.he_6ghz_capa =
7071                         nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7072
7073         if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7074                 params.airtime_weight =
7075                         nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7076
7077         if (params.airtime_weight &&
7078             !wiphy_ext_feature_isset(&rdev->wiphy,
7079                                      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7080                 return -EOPNOTSUPP;
7081
7082         err = nl80211_parse_sta_txpower_setting(info,
7083                                                 &params.link_sta_params.txpwr,
7084                                                 &params.link_sta_params.txpwr_set);
7085         if (err)
7086                 return err;
7087
7088         /* Include parameters for TDLS peer (will check later) */
7089         err = nl80211_set_station_tdls(info, &params);
7090         if (err)
7091                 return err;
7092
7093         params.vlan = get_vlan(info, rdev);
7094         if (IS_ERR(params.vlan))
7095                 return PTR_ERR(params.vlan);
7096
7097         switch (dev->ieee80211_ptr->iftype) {
7098         case NL80211_IFTYPE_AP:
7099         case NL80211_IFTYPE_AP_VLAN:
7100         case NL80211_IFTYPE_P2P_GO:
7101         case NL80211_IFTYPE_P2P_CLIENT:
7102         case NL80211_IFTYPE_STATION:
7103         case NL80211_IFTYPE_ADHOC:
7104         case NL80211_IFTYPE_MESH_POINT:
7105                 break;
7106         default:
7107                 err = -EOPNOTSUPP;
7108                 goto out_put_vlan;
7109         }
7110
7111         /* driver will call cfg80211_check_station_change() */
7112         wdev_lock(dev->ieee80211_ptr);
7113         err = rdev_change_station(rdev, dev, mac_addr, &params);
7114         wdev_unlock(dev->ieee80211_ptr);
7115
7116  out_put_vlan:
7117         dev_put(params.vlan);
7118
7119         return err;
7120 }
7121
7122 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
7123 {
7124         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7125         int err;
7126         struct net_device *dev = info->user_ptr[1];
7127         struct wireless_dev *wdev = dev->ieee80211_ptr;
7128         struct station_parameters params;
7129         u8 *mac_addr = NULL;
7130         u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
7131                          BIT(NL80211_STA_FLAG_ASSOCIATED);
7132
7133         memset(&params, 0, sizeof(params));
7134
7135         if (!rdev->ops->add_station)
7136                 return -EOPNOTSUPP;
7137
7138         if (!info->attrs[NL80211_ATTR_MAC])
7139                 return -EINVAL;
7140
7141         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
7142                 return -EINVAL;
7143
7144         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
7145                 return -EINVAL;
7146
7147         if (!info->attrs[NL80211_ATTR_STA_AID] &&
7148             !info->attrs[NL80211_ATTR_PEER_AID])
7149                 return -EINVAL;
7150
7151         params.link_sta_params.link_id =
7152                 nl80211_link_id_or_invalid(info->attrs);
7153
7154         if (info->attrs[NL80211_ATTR_MLD_ADDR]) {
7155                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
7156                 params.link_sta_params.mld_mac = mac_addr;
7157                 params.link_sta_params.link_mac =
7158                         nla_data(info->attrs[NL80211_ATTR_MAC]);
7159                 if (!is_valid_ether_addr(params.link_sta_params.link_mac))
7160                         return -EINVAL;
7161         } else {
7162                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
7163         }
7164
7165         params.link_sta_params.supported_rates =
7166                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7167         params.link_sta_params.supported_rates_len =
7168                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
7169         params.listen_interval =
7170                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
7171
7172         if (info->attrs[NL80211_ATTR_VLAN_ID])
7173                 params.vlan_id = nla_get_u16(info->attrs[NL80211_ATTR_VLAN_ID]);
7174
7175         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
7176                 params.support_p2p_ps =
7177                         nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
7178         } else {
7179                 /*
7180                  * if not specified, assume it's supported for P2P GO interface,
7181                  * and is NOT supported for AP interface
7182                  */
7183                 params.support_p2p_ps =
7184                         dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
7185         }
7186
7187         if (info->attrs[NL80211_ATTR_PEER_AID])
7188                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
7189         else
7190                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
7191
7192         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
7193                 params.capability =
7194                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
7195                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
7196         }
7197
7198         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
7199                 params.ext_capab =
7200                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7201                 params.ext_capab_len =
7202                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
7203         }
7204
7205         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
7206                 params.link_sta_params.ht_capa =
7207                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
7208
7209         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
7210                 params.link_sta_params.vht_capa =
7211                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
7212
7213         if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
7214                 params.link_sta_params.he_capa =
7215                         nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7216                 params.link_sta_params.he_capa_len =
7217                         nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
7218
7219                 if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
7220                         params.link_sta_params.eht_capa =
7221                                 nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7222                         params.link_sta_params.eht_capa_len =
7223                                 nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
7224
7225                         if (!ieee80211_eht_capa_size_ok((const u8 *)params.link_sta_params.he_capa,
7226                                                         (const u8 *)params.link_sta_params.eht_capa,
7227                                                         params.link_sta_params.eht_capa_len,
7228                                                         false))
7229                                 return -EINVAL;
7230                 }
7231         }
7232
7233         if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
7234                 params.link_sta_params.he_6ghz_capa =
7235                         nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
7236
7237         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
7238                 params.link_sta_params.opmode_notif_used = true;
7239                 params.link_sta_params.opmode_notif =
7240                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
7241         }
7242
7243         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
7244                 params.plink_action =
7245                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
7246
7247         if (info->attrs[NL80211_ATTR_AIRTIME_WEIGHT])
7248                 params.airtime_weight =
7249                         nla_get_u16(info->attrs[NL80211_ATTR_AIRTIME_WEIGHT]);
7250
7251         if (params.airtime_weight &&
7252             !wiphy_ext_feature_isset(&rdev->wiphy,
7253                                      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
7254                 return -EOPNOTSUPP;
7255
7256         err = nl80211_parse_sta_txpower_setting(info,
7257                                                 &params.link_sta_params.txpwr,
7258                                                 &params.link_sta_params.txpwr_set);
7259         if (err)
7260                 return err;
7261
7262         err = nl80211_parse_sta_channel_info(info, &params);
7263         if (err)
7264                 return err;
7265
7266         err = nl80211_parse_sta_wme(info, &params);
7267         if (err)
7268                 return err;
7269
7270         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
7271                 return -EINVAL;
7272
7273         /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
7274          * as userspace might just pass through the capabilities from the IEs
7275          * directly, rather than enforcing this restriction and returning an
7276          * error in this case.
7277          */
7278         if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
7279                 params.link_sta_params.ht_capa = NULL;
7280                 params.link_sta_params.vht_capa = NULL;
7281
7282                 /* HE and EHT require WME */
7283                 if (params.link_sta_params.he_capa_len ||
7284                     params.link_sta_params.he_6ghz_capa ||
7285                     params.link_sta_params.eht_capa_len)
7286                         return -EINVAL;
7287         }
7288
7289         /* Ensure that HT/VHT capabilities are not set for 6 GHz HE STA */
7290         if (params.link_sta_params.he_6ghz_capa &&
7291             (params.link_sta_params.ht_capa || params.link_sta_params.vht_capa))
7292                 return -EINVAL;
7293
7294         /* When you run into this, adjust the code below for the new flag */
7295         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
7296
7297         switch (dev->ieee80211_ptr->iftype) {
7298         case NL80211_IFTYPE_AP:
7299         case NL80211_IFTYPE_AP_VLAN:
7300         case NL80211_IFTYPE_P2P_GO:
7301                 /* ignore WME attributes if iface/sta is not capable */
7302                 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
7303                     !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
7304                         params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7305
7306                 /* TDLS peers cannot be added */
7307                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7308                     info->attrs[NL80211_ATTR_PEER_AID])
7309                         return -EINVAL;
7310                 /* but don't bother the driver with it */
7311                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
7312
7313                 /* allow authenticated/associated only if driver handles it */
7314                 if (!(rdev->wiphy.features &
7315                                 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
7316                     params.sta_flags_mask & auth_assoc)
7317                         return -EINVAL;
7318
7319                 /* Older userspace, or userspace wanting to be compatible with
7320                  * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
7321                  * and assoc flags in the mask, but assumes the station will be
7322                  * added as associated anyway since this was the required driver
7323                  * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
7324                  * introduced.
7325                  * In order to not bother drivers with this quirk in the API
7326                  * set the flags in both the mask and set for new stations in
7327                  * this case.
7328                  */
7329                 if (!(params.sta_flags_mask & auth_assoc)) {
7330                         params.sta_flags_mask |= auth_assoc;
7331                         params.sta_flags_set |= auth_assoc;
7332                 }
7333
7334                 /* must be last in here for error handling */
7335                 params.vlan = get_vlan(info, rdev);
7336                 if (IS_ERR(params.vlan))
7337                         return PTR_ERR(params.vlan);
7338                 break;
7339         case NL80211_IFTYPE_MESH_POINT:
7340                 /* ignore uAPSD data */
7341                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7342
7343                 /* associated is disallowed */
7344                 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
7345                         return -EINVAL;
7346                 /* TDLS peers cannot be added */
7347                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
7348                     info->attrs[NL80211_ATTR_PEER_AID])
7349                         return -EINVAL;
7350                 break;
7351         case NL80211_IFTYPE_STATION:
7352         case NL80211_IFTYPE_P2P_CLIENT:
7353                 /* ignore uAPSD data */
7354                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
7355
7356                 /* these are disallowed */
7357                 if (params.sta_flags_mask &
7358                                 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
7359                                  BIT(NL80211_STA_FLAG_AUTHENTICATED)))
7360                         return -EINVAL;
7361                 /* Only TDLS peers can be added */
7362                 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
7363                         return -EINVAL;
7364                 /* Can only add if TDLS ... */
7365                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
7366                         return -EOPNOTSUPP;
7367                 /* ... with external setup is supported */
7368                 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
7369                         return -EOPNOTSUPP;
7370                 /*
7371                  * Older wpa_supplicant versions always mark the TDLS peer
7372                  * as authorized, but it shouldn't yet be.
7373                  */
7374                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
7375                 break;
7376         default:
7377                 return -EOPNOTSUPP;
7378         }
7379
7380         /* be aware of params.vlan when changing code here */
7381
7382         wdev_lock(dev->ieee80211_ptr);
7383         if (wdev->valid_links) {
7384                 if (params.link_sta_params.link_id < 0) {
7385                         err = -EINVAL;
7386                         goto out;
7387                 }
7388                 if (!(wdev->valid_links & BIT(params.link_sta_params.link_id))) {
7389                         err = -ENOLINK;
7390                         goto out;
7391                 }
7392         } else {
7393                 if (params.link_sta_params.link_id >= 0) {
7394                         err = -EINVAL;
7395                         goto out;
7396                 }
7397         }
7398         err = rdev_add_station(rdev, dev, mac_addr, &params);
7399 out:
7400         wdev_unlock(dev->ieee80211_ptr);
7401         dev_put(params.vlan);
7402         return err;
7403 }
7404
7405 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
7406 {
7407         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7408         struct net_device *dev = info->user_ptr[1];
7409         struct station_del_parameters params;
7410         int ret;
7411
7412         memset(&params, 0, sizeof(params));
7413
7414         if (info->attrs[NL80211_ATTR_MAC])
7415                 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
7416
7417         switch (dev->ieee80211_ptr->iftype) {
7418         case NL80211_IFTYPE_AP:
7419         case NL80211_IFTYPE_AP_VLAN:
7420         case NL80211_IFTYPE_MESH_POINT:
7421         case NL80211_IFTYPE_P2P_GO:
7422                 /* always accept these */
7423                 break;
7424         case NL80211_IFTYPE_ADHOC:
7425                 /* conditionally accept */
7426                 if (wiphy_ext_feature_isset(&rdev->wiphy,
7427                                             NL80211_EXT_FEATURE_DEL_IBSS_STA))
7428                         break;
7429                 return -EINVAL;
7430         default:
7431                 return -EINVAL;
7432         }
7433
7434         if (!rdev->ops->del_station)
7435                 return -EOPNOTSUPP;
7436
7437         if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
7438                 params.subtype =
7439                         nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
7440                 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
7441                     params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
7442                         return -EINVAL;
7443         } else {
7444                 /* Default to Deauthentication frame */
7445                 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
7446         }
7447
7448         if (info->attrs[NL80211_ATTR_REASON_CODE]) {
7449                 params.reason_code =
7450                         nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7451                 if (params.reason_code == 0)
7452                         return -EINVAL; /* 0 is reserved */
7453         } else {
7454                 /* Default to reason code 2 */
7455                 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
7456         }
7457
7458         wdev_lock(dev->ieee80211_ptr);
7459         ret = rdev_del_station(rdev, dev, &params);
7460         wdev_unlock(dev->ieee80211_ptr);
7461
7462         return ret;
7463 }
7464
7465 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
7466                                 int flags, struct net_device *dev,
7467                                 u8 *dst, u8 *next_hop,
7468                                 struct mpath_info *pinfo)
7469 {
7470         void *hdr;
7471         struct nlattr *pinfoattr;
7472
7473         hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
7474         if (!hdr)
7475                 return -1;
7476
7477         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7478             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7479             nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
7480             nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
7481                 goto nla_put_failure;
7482
7483         pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MPATH_INFO);
7484         if (!pinfoattr)
7485                 goto nla_put_failure;
7486         if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
7487             nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
7488                         pinfo->frame_qlen))
7489                 goto nla_put_failure;
7490         if (((pinfo->filled & MPATH_INFO_SN) &&
7491              nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
7492             ((pinfo->filled & MPATH_INFO_METRIC) &&
7493              nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
7494                          pinfo->metric)) ||
7495             ((pinfo->filled & MPATH_INFO_EXPTIME) &&
7496              nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
7497                          pinfo->exptime)) ||
7498             ((pinfo->filled & MPATH_INFO_FLAGS) &&
7499              nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
7500                         pinfo->flags)) ||
7501             ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
7502              nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
7503                          pinfo->discovery_timeout)) ||
7504             ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
7505              nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
7506                         pinfo->discovery_retries)) ||
7507             ((pinfo->filled & MPATH_INFO_HOP_COUNT) &&
7508              nla_put_u8(msg, NL80211_MPATH_INFO_HOP_COUNT,
7509                         pinfo->hop_count)) ||
7510             ((pinfo->filled & MPATH_INFO_PATH_CHANGE) &&
7511              nla_put_u32(msg, NL80211_MPATH_INFO_PATH_CHANGE,
7512                          pinfo->path_change_count)))
7513                 goto nla_put_failure;
7514
7515         nla_nest_end(msg, pinfoattr);
7516
7517         genlmsg_end(msg, hdr);
7518         return 0;
7519
7520  nla_put_failure:
7521         genlmsg_cancel(msg, hdr);
7522         return -EMSGSIZE;
7523 }
7524
7525 static int nl80211_dump_mpath(struct sk_buff *skb,
7526                               struct netlink_callback *cb)
7527 {
7528         struct mpath_info pinfo;
7529         struct cfg80211_registered_device *rdev;
7530         struct wireless_dev *wdev;
7531         u8 dst[ETH_ALEN];
7532         u8 next_hop[ETH_ALEN];
7533         int path_idx = cb->args[2];
7534         int err;
7535
7536         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7537         if (err)
7538                 return err;
7539         /* nl80211_prepare_wdev_dump acquired it in the successful case */
7540         __acquire(&rdev->wiphy.mtx);
7541
7542         if (!rdev->ops->dump_mpath) {
7543                 err = -EOPNOTSUPP;
7544                 goto out_err;
7545         }
7546
7547         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7548                 err = -EOPNOTSUPP;
7549                 goto out_err;
7550         }
7551
7552         while (1) {
7553                 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
7554                                       next_hop, &pinfo);
7555                 if (err == -ENOENT)
7556                         break;
7557                 if (err)
7558                         goto out_err;
7559
7560                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7561                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
7562                                        wdev->netdev, dst, next_hop,
7563                                        &pinfo) < 0)
7564                         goto out;
7565
7566                 path_idx++;
7567         }
7568
7569  out:
7570         cb->args[2] = path_idx;
7571         err = skb->len;
7572  out_err:
7573         wiphy_unlock(&rdev->wiphy);
7574         return err;
7575 }
7576
7577 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
7578 {
7579         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7580         int err;
7581         struct net_device *dev = info->user_ptr[1];
7582         struct mpath_info pinfo;
7583         struct sk_buff *msg;
7584         u8 *dst = NULL;
7585         u8 next_hop[ETH_ALEN];
7586
7587         memset(&pinfo, 0, sizeof(pinfo));
7588
7589         if (!info->attrs[NL80211_ATTR_MAC])
7590                 return -EINVAL;
7591
7592         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7593
7594         if (!rdev->ops->get_mpath)
7595                 return -EOPNOTSUPP;
7596
7597         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7598                 return -EOPNOTSUPP;
7599
7600         err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
7601         if (err)
7602                 return err;
7603
7604         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7605         if (!msg)
7606                 return -ENOMEM;
7607
7608         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7609                                  dev, dst, next_hop, &pinfo) < 0) {
7610                 nlmsg_free(msg);
7611                 return -ENOBUFS;
7612         }
7613
7614         return genlmsg_reply(msg, info);
7615 }
7616
7617 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
7618 {
7619         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7620         struct net_device *dev = info->user_ptr[1];
7621         u8 *dst = NULL;
7622         u8 *next_hop = NULL;
7623
7624         if (!info->attrs[NL80211_ATTR_MAC])
7625                 return -EINVAL;
7626
7627         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7628                 return -EINVAL;
7629
7630         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7631         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7632
7633         if (!rdev->ops->change_mpath)
7634                 return -EOPNOTSUPP;
7635
7636         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7637                 return -EOPNOTSUPP;
7638
7639         return rdev_change_mpath(rdev, dev, dst, next_hop);
7640 }
7641
7642 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
7643 {
7644         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7645         struct net_device *dev = info->user_ptr[1];
7646         u8 *dst = NULL;
7647         u8 *next_hop = NULL;
7648
7649         if (!info->attrs[NL80211_ATTR_MAC])
7650                 return -EINVAL;
7651
7652         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
7653                 return -EINVAL;
7654
7655         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7656         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
7657
7658         if (!rdev->ops->add_mpath)
7659                 return -EOPNOTSUPP;
7660
7661         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7662                 return -EOPNOTSUPP;
7663
7664         return rdev_add_mpath(rdev, dev, dst, next_hop);
7665 }
7666
7667 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
7668 {
7669         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7670         struct net_device *dev = info->user_ptr[1];
7671         u8 *dst = NULL;
7672
7673         if (info->attrs[NL80211_ATTR_MAC])
7674                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7675
7676         if (!rdev->ops->del_mpath)
7677                 return -EOPNOTSUPP;
7678
7679         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7680                 return -EOPNOTSUPP;
7681
7682         return rdev_del_mpath(rdev, dev, dst);
7683 }
7684
7685 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
7686 {
7687         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7688         int err;
7689         struct net_device *dev = info->user_ptr[1];
7690         struct mpath_info pinfo;
7691         struct sk_buff *msg;
7692         u8 *dst = NULL;
7693         u8 mpp[ETH_ALEN];
7694
7695         memset(&pinfo, 0, sizeof(pinfo));
7696
7697         if (!info->attrs[NL80211_ATTR_MAC])
7698                 return -EINVAL;
7699
7700         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
7701
7702         if (!rdev->ops->get_mpp)
7703                 return -EOPNOTSUPP;
7704
7705         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
7706                 return -EOPNOTSUPP;
7707
7708         err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
7709         if (err)
7710                 return err;
7711
7712         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7713         if (!msg)
7714                 return -ENOMEM;
7715
7716         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
7717                                dev, dst, mpp, &pinfo) < 0) {
7718                 nlmsg_free(msg);
7719                 return -ENOBUFS;
7720         }
7721
7722         return genlmsg_reply(msg, info);
7723 }
7724
7725 static int nl80211_dump_mpp(struct sk_buff *skb,
7726                             struct netlink_callback *cb)
7727 {
7728         struct mpath_info pinfo;
7729         struct cfg80211_registered_device *rdev;
7730         struct wireless_dev *wdev;
7731         u8 dst[ETH_ALEN];
7732         u8 mpp[ETH_ALEN];
7733         int path_idx = cb->args[2];
7734         int err;
7735
7736         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
7737         if (err)
7738                 return err;
7739         /* nl80211_prepare_wdev_dump acquired it in the successful case */
7740         __acquire(&rdev->wiphy.mtx);
7741
7742         if (!rdev->ops->dump_mpp) {
7743                 err = -EOPNOTSUPP;
7744                 goto out_err;
7745         }
7746
7747         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
7748                 err = -EOPNOTSUPP;
7749                 goto out_err;
7750         }
7751
7752         while (1) {
7753                 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
7754                                     mpp, &pinfo);
7755                 if (err == -ENOENT)
7756                         break;
7757                 if (err)
7758                         goto out_err;
7759
7760                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
7761                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
7762                                        wdev->netdev, dst, mpp,
7763                                        &pinfo) < 0)
7764                         goto out;
7765
7766                 path_idx++;
7767         }
7768
7769  out:
7770         cb->args[2] = path_idx;
7771         err = skb->len;
7772  out_err:
7773         wiphy_unlock(&rdev->wiphy);
7774         return err;
7775 }
7776
7777 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
7778 {
7779         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7780         struct net_device *dev = info->user_ptr[1];
7781         struct wireless_dev *wdev = dev->ieee80211_ptr;
7782         struct bss_parameters params;
7783         int err;
7784
7785         memset(&params, 0, sizeof(params));
7786         /* default to not changing parameters */
7787         params.use_cts_prot = -1;
7788         params.use_short_preamble = -1;
7789         params.use_short_slot_time = -1;
7790         params.ap_isolate = -1;
7791         params.ht_opmode = -1;
7792         params.p2p_ctwindow = -1;
7793         params.p2p_opp_ps = -1;
7794
7795         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
7796                 params.use_cts_prot =
7797                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
7798         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
7799                 params.use_short_preamble =
7800                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
7801         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
7802                 params.use_short_slot_time =
7803                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
7804         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7805                 params.basic_rates =
7806                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7807                 params.basic_rates_len =
7808                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7809         }
7810         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
7811                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
7812         if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
7813                 params.ht_opmode =
7814                         nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
7815
7816         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
7817                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7818                         return -EINVAL;
7819                 params.p2p_ctwindow =
7820                         nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
7821                 if (params.p2p_ctwindow != 0 &&
7822                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
7823                         return -EINVAL;
7824         }
7825
7826         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
7827                 u8 tmp;
7828
7829                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7830                         return -EINVAL;
7831                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
7832                 params.p2p_opp_ps = tmp;
7833                 if (params.p2p_opp_ps &&
7834                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
7835                         return -EINVAL;
7836         }
7837
7838         if (!rdev->ops->change_bss)
7839                 return -EOPNOTSUPP;
7840
7841         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
7842             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
7843                 return -EOPNOTSUPP;
7844
7845         wdev_lock(wdev);
7846         err = rdev_change_bss(rdev, dev, &params);
7847         wdev_unlock(wdev);
7848
7849         return err;
7850 }
7851
7852 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
7853 {
7854         char *data = NULL;
7855         bool is_indoor;
7856         enum nl80211_user_reg_hint_type user_reg_hint_type;
7857         u32 owner_nlportid;
7858
7859         /*
7860          * You should only get this when cfg80211 hasn't yet initialized
7861          * completely when built-in to the kernel right between the time
7862          * window between nl80211_init() and regulatory_init(), if that is
7863          * even possible.
7864          */
7865         if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
7866                 return -EINPROGRESS;
7867
7868         if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
7869                 user_reg_hint_type =
7870                   nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
7871         else
7872                 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
7873
7874         switch (user_reg_hint_type) {
7875         case NL80211_USER_REG_HINT_USER:
7876         case NL80211_USER_REG_HINT_CELL_BASE:
7877                 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
7878                         return -EINVAL;
7879
7880                 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
7881                 return regulatory_hint_user(data, user_reg_hint_type);
7882         case NL80211_USER_REG_HINT_INDOOR:
7883                 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
7884                         owner_nlportid = info->snd_portid;
7885                         is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
7886                 } else {
7887                         owner_nlportid = 0;
7888                         is_indoor = true;
7889                 }
7890
7891                 return regulatory_hint_indoor(is_indoor, owner_nlportid);
7892         default:
7893                 return -EINVAL;
7894         }
7895 }
7896
7897 static int nl80211_reload_regdb(struct sk_buff *skb, struct genl_info *info)
7898 {
7899         return reg_reload_regdb();
7900 }
7901
7902 static int nl80211_get_mesh_config(struct sk_buff *skb,
7903                                    struct genl_info *info)
7904 {
7905         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7906         struct net_device *dev = info->user_ptr[1];
7907         struct wireless_dev *wdev = dev->ieee80211_ptr;
7908         struct mesh_config cur_params;
7909         int err = 0;
7910         void *hdr;
7911         struct nlattr *pinfoattr;
7912         struct sk_buff *msg;
7913
7914         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
7915                 return -EOPNOTSUPP;
7916
7917         if (!rdev->ops->get_mesh_config)
7918                 return -EOPNOTSUPP;
7919
7920         wdev_lock(wdev);
7921         /* If not connected, get default parameters */
7922         if (!wdev->u.mesh.id_len)
7923                 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
7924         else
7925                 err = rdev_get_mesh_config(rdev, dev, &cur_params);
7926         wdev_unlock(wdev);
7927
7928         if (err)
7929                 return err;
7930
7931         /* Draw up a netlink message to send back */
7932         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7933         if (!msg)
7934                 return -ENOMEM;
7935         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
7936                              NL80211_CMD_GET_MESH_CONFIG);
7937         if (!hdr)
7938                 goto out;
7939         pinfoattr = nla_nest_start_noflag(msg, NL80211_ATTR_MESH_CONFIG);
7940         if (!pinfoattr)
7941                 goto nla_put_failure;
7942         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
7943             nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
7944                         cur_params.dot11MeshRetryTimeout) ||
7945             nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
7946                         cur_params.dot11MeshConfirmTimeout) ||
7947             nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
7948                         cur_params.dot11MeshHoldingTimeout) ||
7949             nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7950                         cur_params.dot11MeshMaxPeerLinks) ||
7951             nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
7952                        cur_params.dot11MeshMaxRetries) ||
7953             nla_put_u8(msg, NL80211_MESHCONF_TTL,
7954                        cur_params.dot11MeshTTL) ||
7955             nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
7956                        cur_params.element_ttl) ||
7957             nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
7958                        cur_params.auto_open_plinks) ||
7959             nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
7960                         cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
7961             nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
7962                        cur_params.dot11MeshHWMPmaxPREQretries) ||
7963             nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
7964                         cur_params.path_refresh_time) ||
7965             nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
7966                         cur_params.min_discovery_timeout) ||
7967             nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
7968                         cur_params.dot11MeshHWMPactivePathTimeout) ||
7969             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
7970                         cur_params.dot11MeshHWMPpreqMinInterval) ||
7971             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
7972                         cur_params.dot11MeshHWMPperrMinInterval) ||
7973             nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
7974                         cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
7975             nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
7976                        cur_params.dot11MeshHWMPRootMode) ||
7977             nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
7978                         cur_params.dot11MeshHWMPRannInterval) ||
7979             nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
7980                        cur_params.dot11MeshGateAnnouncementProtocol) ||
7981             nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
7982                        cur_params.dot11MeshForwarding) ||
7983             nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
7984                         cur_params.rssi_threshold) ||
7985             nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
7986                         cur_params.ht_opmode) ||
7987             nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
7988                         cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
7989             nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
7990                         cur_params.dot11MeshHWMProotInterval) ||
7991             nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
7992                         cur_params.dot11MeshHWMPconfirmationInterval) ||
7993             nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
7994                         cur_params.power_mode) ||
7995             nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
7996                         cur_params.dot11MeshAwakeWindowDuration) ||
7997             nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
7998                         cur_params.plink_timeout) ||
7999             nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_GATE,
8000                        cur_params.dot11MeshConnectedToMeshGate) ||
8001             nla_put_u8(msg, NL80211_MESHCONF_NOLEARN,
8002                        cur_params.dot11MeshNolearn) ||
8003             nla_put_u8(msg, NL80211_MESHCONF_CONNECTED_TO_AS,
8004                        cur_params.dot11MeshConnectedToAuthServer))
8005                 goto nla_put_failure;
8006         nla_nest_end(msg, pinfoattr);
8007         genlmsg_end(msg, hdr);
8008         return genlmsg_reply(msg, info);
8009
8010  nla_put_failure:
8011  out:
8012         nlmsg_free(msg);
8013         return -ENOBUFS;
8014 }
8015
8016 static const struct nla_policy
8017 nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
8018         [NL80211_MESHCONF_RETRY_TIMEOUT] =
8019                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
8020         [NL80211_MESHCONF_CONFIRM_TIMEOUT] =
8021                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
8022         [NL80211_MESHCONF_HOLDING_TIMEOUT] =
8023                 NLA_POLICY_RANGE(NLA_U16, 1, 255),
8024         [NL80211_MESHCONF_MAX_PEER_LINKS] =
8025                 NLA_POLICY_RANGE(NLA_U16, 0, 255),
8026         [NL80211_MESHCONF_MAX_RETRIES] = NLA_POLICY_MAX(NLA_U8, 16),
8027         [NL80211_MESHCONF_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8028         [NL80211_MESHCONF_ELEMENT_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
8029         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = NLA_POLICY_MAX(NLA_U8, 1),
8030         [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] =
8031                 NLA_POLICY_RANGE(NLA_U32, 1, 255),
8032         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
8033         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
8034         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = NLA_POLICY_MIN(NLA_U16, 1),
8035         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
8036         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] =
8037                 NLA_POLICY_MIN(NLA_U16, 1),
8038         [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] =
8039                 NLA_POLICY_MIN(NLA_U16, 1),
8040         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] =
8041                 NLA_POLICY_MIN(NLA_U16, 1),
8042         [NL80211_MESHCONF_HWMP_ROOTMODE] = NLA_POLICY_MAX(NLA_U8, 4),
8043         [NL80211_MESHCONF_HWMP_RANN_INTERVAL] =
8044                 NLA_POLICY_MIN(NLA_U16, 1),
8045         [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = NLA_POLICY_MAX(NLA_U8, 1),
8046         [NL80211_MESHCONF_FORWARDING] = NLA_POLICY_MAX(NLA_U8, 1),
8047         [NL80211_MESHCONF_RSSI_THRESHOLD] =
8048                 NLA_POLICY_RANGE(NLA_S32, -255, 0),
8049         [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
8050         [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
8051         [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] =
8052                 NLA_POLICY_MIN(NLA_U16, 1),
8053         [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] =
8054                 NLA_POLICY_MIN(NLA_U16, 1),
8055         [NL80211_MESHCONF_POWER_MODE] =
8056                 NLA_POLICY_RANGE(NLA_U32,
8057                                  NL80211_MESH_POWER_ACTIVE,
8058                                  NL80211_MESH_POWER_MAX),
8059         [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
8060         [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
8061         [NL80211_MESHCONF_CONNECTED_TO_GATE] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8062         [NL80211_MESHCONF_NOLEARN] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8063         [NL80211_MESHCONF_CONNECTED_TO_AS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
8064 };
8065
8066 static const struct nla_policy
8067         nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
8068         [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
8069         [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
8070         [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
8071         [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
8072         [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
8073         [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
8074         [NL80211_MESH_SETUP_IE] =
8075                 NLA_POLICY_VALIDATE_FN(NLA_BINARY, validate_ie_attr,
8076                                        IEEE80211_MAX_DATA_LEN),
8077         [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
8078 };
8079
8080 static int nl80211_parse_mesh_config(struct genl_info *info,
8081                                      struct mesh_config *cfg,
8082                                      u32 *mask_out)
8083 {
8084         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
8085         u32 mask = 0;
8086         u16 ht_opmode;
8087
8088 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, mask, attr, fn)       \
8089 do {                                                                    \
8090         if (tb[attr]) {                                                 \
8091                 cfg->param = fn(tb[attr]);                              \
8092                 mask |= BIT((attr) - 1);                                \
8093         }                                                               \
8094 } while (0)
8095
8096         if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
8097                 return -EINVAL;
8098         if (nla_parse_nested_deprecated(tb, NL80211_MESHCONF_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_CONFIG], nl80211_meshconf_params_policy, info->extack))
8099                 return -EINVAL;
8100
8101         /* This makes sure that there aren't more than 32 mesh config
8102          * parameters (otherwise our bitfield scheme would not work.) */
8103         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
8104
8105         /* Fill in the params struct */
8106         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, mask,
8107                                   NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
8108         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, mask,
8109                                   NL80211_MESHCONF_CONFIRM_TIMEOUT,
8110                                   nla_get_u16);
8111         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, mask,
8112                                   NL80211_MESHCONF_HOLDING_TIMEOUT,
8113                                   nla_get_u16);
8114         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, mask,
8115                                   NL80211_MESHCONF_MAX_PEER_LINKS,
8116                                   nla_get_u16);
8117         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, mask,
8118                                   NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
8119         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, mask,
8120                                   NL80211_MESHCONF_TTL, nla_get_u8);
8121         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, mask,
8122                                   NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
8123         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, mask,
8124                                   NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8125                                   nla_get_u8);
8126         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
8127                                   mask,
8128                                   NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
8129                                   nla_get_u32);
8130         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, mask,
8131                                   NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
8132                                   nla_get_u8);
8133         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, mask,
8134                                   NL80211_MESHCONF_PATH_REFRESH_TIME,
8135                                   nla_get_u32);
8136         if (mask & BIT(NL80211_MESHCONF_PATH_REFRESH_TIME) &&
8137             (cfg->path_refresh_time < 1 || cfg->path_refresh_time > 65535))
8138                 return -EINVAL;
8139         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, mask,
8140                                   NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
8141                                   nla_get_u16);
8142         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
8143                                   mask,
8144                                   NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
8145                                   nla_get_u32);
8146         if (mask & BIT(NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT) &&
8147             (cfg->dot11MeshHWMPactivePathTimeout < 1 ||
8148              cfg->dot11MeshHWMPactivePathTimeout > 65535))
8149                 return -EINVAL;
8150         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval, mask,
8151                                   NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
8152                                   nla_get_u16);
8153         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval, mask,
8154                                   NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
8155                                   nla_get_u16);
8156         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8157                                   dot11MeshHWMPnetDiameterTraversalTime, mask,
8158                                   NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
8159                                   nla_get_u16);
8160         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
8161                                   NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
8162         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
8163                                   NL80211_MESHCONF_HWMP_RANN_INTERVAL,
8164                                   nla_get_u16);
8165         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshGateAnnouncementProtocol,
8166                                   mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
8167                                   nla_get_u8);
8168         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, mask,
8169                                   NL80211_MESHCONF_FORWARDING, nla_get_u8);
8170         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, mask,
8171                                   NL80211_MESHCONF_RSSI_THRESHOLD,
8172                                   nla_get_s32);
8173         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToMeshGate, mask,
8174                                   NL80211_MESHCONF_CONNECTED_TO_GATE,
8175                                   nla_get_u8);
8176         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConnectedToAuthServer, mask,
8177                                   NL80211_MESHCONF_CONNECTED_TO_AS,
8178                                   nla_get_u8);
8179         /*
8180          * Check HT operation mode based on
8181          * IEEE 802.11-2016 9.4.2.57 HT Operation element.
8182          */
8183         if (tb[NL80211_MESHCONF_HT_OPMODE]) {
8184                 ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
8185
8186                 if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
8187                                   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
8188                                   IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
8189                         return -EINVAL;
8190
8191                 /* NON_HT_STA bit is reserved, but some programs set it */
8192                 ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
8193
8194                 cfg->ht_opmode = ht_opmode;
8195                 mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
8196         }
8197         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
8198                                   dot11MeshHWMPactivePathToRootTimeout, mask,
8199                                   NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
8200                                   nla_get_u32);
8201         if (mask & BIT(NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT) &&
8202             (cfg->dot11MeshHWMPactivePathToRootTimeout < 1 ||
8203              cfg->dot11MeshHWMPactivePathToRootTimeout > 65535))
8204                 return -EINVAL;
8205         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, mask,
8206                                   NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
8207                                   nla_get_u16);
8208         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPconfirmationInterval,
8209                                   mask,
8210                                   NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
8211                                   nla_get_u16);
8212         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode, mask,
8213                                   NL80211_MESHCONF_POWER_MODE, nla_get_u32);
8214         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, mask,
8215                                   NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
8216         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, mask,
8217                                   NL80211_MESHCONF_PLINK_TIMEOUT, nla_get_u32);
8218         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNolearn, mask,
8219                                   NL80211_MESHCONF_NOLEARN, nla_get_u8);
8220         if (mask_out)
8221                 *mask_out = mask;
8222
8223         return 0;
8224
8225 #undef FILL_IN_MESH_PARAM_IF_SET
8226 }
8227
8228 static int nl80211_parse_mesh_setup(struct genl_info *info,
8229                                      struct mesh_setup *setup)
8230 {
8231         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8232         struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
8233
8234         if (!info->attrs[NL80211_ATTR_MESH_SETUP])
8235                 return -EINVAL;
8236         if (nla_parse_nested_deprecated(tb, NL80211_MESH_SETUP_ATTR_MAX, info->attrs[NL80211_ATTR_MESH_SETUP], nl80211_mesh_setup_params_policy, info->extack))
8237                 return -EINVAL;
8238
8239         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
8240                 setup->sync_method =
8241                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
8242                  IEEE80211_SYNC_METHOD_VENDOR :
8243                  IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
8244
8245         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
8246                 setup->path_sel_proto =
8247                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
8248                  IEEE80211_PATH_PROTOCOL_VENDOR :
8249                  IEEE80211_PATH_PROTOCOL_HWMP;
8250
8251         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
8252                 setup->path_metric =
8253                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
8254                  IEEE80211_PATH_METRIC_VENDOR :
8255                  IEEE80211_PATH_METRIC_AIRTIME;
8256
8257         if (tb[NL80211_MESH_SETUP_IE]) {
8258                 struct nlattr *ieattr =
8259                         tb[NL80211_MESH_SETUP_IE];
8260                 setup->ie = nla_data(ieattr);
8261                 setup->ie_len = nla_len(ieattr);
8262         }
8263         if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
8264             !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
8265                 return -EINVAL;
8266         setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
8267         setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
8268         setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
8269         if (setup->is_secure)
8270                 setup->user_mpm = true;
8271
8272         if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
8273                 if (!setup->user_mpm)
8274                         return -EINVAL;
8275                 setup->auth_id =
8276                         nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
8277         }
8278
8279         return 0;
8280 }
8281
8282 static int nl80211_update_mesh_config(struct sk_buff *skb,
8283                                       struct genl_info *info)
8284 {
8285         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8286         struct net_device *dev = info->user_ptr[1];
8287         struct wireless_dev *wdev = dev->ieee80211_ptr;
8288         struct mesh_config cfg;
8289         u32 mask;
8290         int err;
8291
8292         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
8293                 return -EOPNOTSUPP;
8294
8295         if (!rdev->ops->update_mesh_config)
8296                 return -EOPNOTSUPP;
8297
8298         err = nl80211_parse_mesh_config(info, &cfg, &mask);
8299         if (err)
8300                 return err;
8301
8302         wdev_lock(wdev);
8303         if (!wdev->u.mesh.id_len)
8304                 err = -ENOLINK;
8305
8306         if (!err)
8307                 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
8308
8309         wdev_unlock(wdev);
8310
8311         return err;
8312 }
8313
8314 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
8315                               struct sk_buff *msg)
8316 {
8317         struct nlattr *nl_reg_rules;
8318         unsigned int i;
8319
8320         if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
8321             (regdom->dfs_region &&
8322              nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
8323                 goto nla_put_failure;
8324
8325         nl_reg_rules = nla_nest_start_noflag(msg, NL80211_ATTR_REG_RULES);
8326         if (!nl_reg_rules)
8327                 goto nla_put_failure;
8328
8329         for (i = 0; i < regdom->n_reg_rules; i++) {
8330                 struct nlattr *nl_reg_rule;
8331                 const struct ieee80211_reg_rule *reg_rule;
8332                 const struct ieee80211_freq_range *freq_range;
8333                 const struct ieee80211_power_rule *power_rule;
8334                 unsigned int max_bandwidth_khz;
8335
8336                 reg_rule = &regdom->reg_rules[i];
8337                 freq_range = &reg_rule->freq_range;
8338                 power_rule = &reg_rule->power_rule;
8339
8340                 nl_reg_rule = nla_nest_start_noflag(msg, i);
8341                 if (!nl_reg_rule)
8342                         goto nla_put_failure;
8343
8344                 max_bandwidth_khz = freq_range->max_bandwidth_khz;
8345                 if (!max_bandwidth_khz)
8346                         max_bandwidth_khz = reg_get_max_bandwidth(regdom,
8347                                                                   reg_rule);
8348
8349                 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
8350                                 reg_rule->flags) ||
8351                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
8352                                 freq_range->start_freq_khz) ||
8353                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
8354                                 freq_range->end_freq_khz) ||
8355                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
8356                                 max_bandwidth_khz) ||
8357                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
8358                                 power_rule->max_antenna_gain) ||
8359                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
8360                                 power_rule->max_eirp) ||
8361                     nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
8362                                 reg_rule->dfs_cac_ms))
8363                         goto nla_put_failure;
8364
8365                 nla_nest_end(msg, nl_reg_rule);
8366         }
8367
8368         nla_nest_end(msg, nl_reg_rules);
8369         return 0;
8370
8371 nla_put_failure:
8372         return -EMSGSIZE;
8373 }
8374
8375 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
8376 {
8377         const struct ieee80211_regdomain *regdom = NULL;
8378         struct cfg80211_registered_device *rdev;
8379         struct wiphy *wiphy = NULL;
8380         struct sk_buff *msg;
8381         int err = -EMSGSIZE;
8382         void *hdr;
8383
8384         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8385         if (!msg)
8386                 return -ENOBUFS;
8387
8388         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8389                              NL80211_CMD_GET_REG);
8390         if (!hdr)
8391                 goto put_failure;
8392
8393         rtnl_lock();
8394
8395         if (info->attrs[NL80211_ATTR_WIPHY]) {
8396                 bool self_managed;
8397
8398                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
8399                 if (IS_ERR(rdev)) {
8400                         err = PTR_ERR(rdev);
8401                         goto nla_put_failure;
8402                 }
8403
8404                 wiphy = &rdev->wiphy;
8405                 self_managed = wiphy->regulatory_flags &
8406                                REGULATORY_WIPHY_SELF_MANAGED;
8407
8408                 rcu_read_lock();
8409
8410                 regdom = get_wiphy_regdom(wiphy);
8411
8412                 /* a self-managed-reg device must have a private regdom */
8413                 if (WARN_ON(!regdom && self_managed)) {
8414                         err = -EINVAL;
8415                         goto nla_put_failure_rcu;
8416                 }
8417
8418                 if (regdom &&
8419                     nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8420                         goto nla_put_failure_rcu;
8421         } else {
8422                 rcu_read_lock();
8423         }
8424
8425         if (!wiphy && reg_last_request_cell_base() &&
8426             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8427                         NL80211_USER_REG_HINT_CELL_BASE))
8428                 goto nla_put_failure_rcu;
8429
8430         if (!regdom)
8431                 regdom = rcu_dereference(cfg80211_regdomain);
8432
8433         if (nl80211_put_regdom(regdom, msg))
8434                 goto nla_put_failure_rcu;
8435
8436         rcu_read_unlock();
8437
8438         genlmsg_end(msg, hdr);
8439         rtnl_unlock();
8440         return genlmsg_reply(msg, info);
8441
8442 nla_put_failure_rcu:
8443         rcu_read_unlock();
8444 nla_put_failure:
8445         rtnl_unlock();
8446 put_failure:
8447         nlmsg_free(msg);
8448         return err;
8449 }
8450
8451 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
8452                                u32 seq, int flags, struct wiphy *wiphy,
8453                                const struct ieee80211_regdomain *regdom)
8454 {
8455         void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
8456                                    NL80211_CMD_GET_REG);
8457
8458         if (!hdr)
8459                 return -1;
8460
8461         genl_dump_check_consistent(cb, hdr);
8462
8463         if (nl80211_put_regdom(regdom, msg))
8464                 goto nla_put_failure;
8465
8466         if (!wiphy && reg_last_request_cell_base() &&
8467             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
8468                         NL80211_USER_REG_HINT_CELL_BASE))
8469                 goto nla_put_failure;
8470
8471         if (wiphy &&
8472             nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8473                 goto nla_put_failure;
8474
8475         if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
8476             nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
8477                 goto nla_put_failure;
8478
8479         genlmsg_end(msg, hdr);
8480         return 0;
8481
8482 nla_put_failure:
8483         genlmsg_cancel(msg, hdr);
8484         return -EMSGSIZE;
8485 }
8486
8487 static int nl80211_get_reg_dump(struct sk_buff *skb,
8488                                 struct netlink_callback *cb)
8489 {
8490         const struct ieee80211_regdomain *regdom = NULL;
8491         struct cfg80211_registered_device *rdev;
8492         int err, reg_idx, start = cb->args[2];
8493
8494         rcu_read_lock();
8495
8496         if (cfg80211_regdomain && start == 0) {
8497                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8498                                           NLM_F_MULTI, NULL,
8499                                           rcu_dereference(cfg80211_regdomain));
8500                 if (err < 0)
8501                         goto out_err;
8502         }
8503
8504         /* the global regdom is idx 0 */
8505         reg_idx = 1;
8506         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
8507                 regdom = get_wiphy_regdom(&rdev->wiphy);
8508                 if (!regdom)
8509                         continue;
8510
8511                 if (++reg_idx <= start)
8512                         continue;
8513
8514                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
8515                                           NLM_F_MULTI, &rdev->wiphy, regdom);
8516                 if (err < 0) {
8517                         reg_idx--;
8518                         break;
8519                 }
8520         }
8521
8522         cb->args[2] = reg_idx;
8523         err = skb->len;
8524 out_err:
8525         rcu_read_unlock();
8526         return err;
8527 }
8528
8529 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
8530 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
8531         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
8532         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
8533         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
8534         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
8535         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
8536         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
8537         [NL80211_ATTR_DFS_CAC_TIME]             = { .type = NLA_U32 },
8538 };
8539
8540 static int parse_reg_rule(struct nlattr *tb[],
8541         struct ieee80211_reg_rule *reg_rule)
8542 {
8543         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
8544         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
8545
8546         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
8547                 return -EINVAL;
8548         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
8549                 return -EINVAL;
8550         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
8551                 return -EINVAL;
8552         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
8553                 return -EINVAL;
8554         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
8555                 return -EINVAL;
8556
8557         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
8558
8559         freq_range->start_freq_khz =
8560                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
8561         freq_range->end_freq_khz =
8562                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
8563         freq_range->max_bandwidth_khz =
8564                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
8565
8566         power_rule->max_eirp =
8567                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
8568
8569         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
8570                 power_rule->max_antenna_gain =
8571                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
8572
8573         if (tb[NL80211_ATTR_DFS_CAC_TIME])
8574                 reg_rule->dfs_cac_ms =
8575                         nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
8576
8577         return 0;
8578 }
8579
8580 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
8581 {
8582         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
8583         struct nlattr *nl_reg_rule;
8584         char *alpha2;
8585         int rem_reg_rules, r;
8586         u32 num_rules = 0, rule_idx = 0;
8587         enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
8588         struct ieee80211_regdomain *rd;
8589
8590         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
8591                 return -EINVAL;
8592
8593         if (!info->attrs[NL80211_ATTR_REG_RULES])
8594                 return -EINVAL;
8595
8596         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
8597
8598         if (info->attrs[NL80211_ATTR_DFS_REGION])
8599                 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
8600
8601         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8602                             rem_reg_rules) {
8603                 num_rules++;
8604                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
8605                         return -EINVAL;
8606         }
8607
8608         rtnl_lock();
8609         if (!reg_is_valid_request(alpha2)) {
8610                 r = -EINVAL;
8611                 goto out;
8612         }
8613
8614         rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
8615         if (!rd) {
8616                 r = -ENOMEM;
8617                 goto out;
8618         }
8619
8620         rd->n_reg_rules = num_rules;
8621         rd->alpha2[0] = alpha2[0];
8622         rd->alpha2[1] = alpha2[1];
8623
8624         /*
8625          * Disable DFS master mode if the DFS region was
8626          * not supported or known on this kernel.
8627          */
8628         if (reg_supported_dfs_region(dfs_region))
8629                 rd->dfs_region = dfs_region;
8630
8631         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
8632                             rem_reg_rules) {
8633                 r = nla_parse_nested_deprecated(tb, NL80211_REG_RULE_ATTR_MAX,
8634                                                 nl_reg_rule, reg_rule_policy,
8635                                                 info->extack);
8636                 if (r)
8637                         goto bad_reg;
8638                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
8639                 if (r)
8640                         goto bad_reg;
8641
8642                 rule_idx++;
8643
8644                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
8645                         r = -EINVAL;
8646                         goto bad_reg;
8647                 }
8648         }
8649
8650         r = set_regdom(rd, REGD_SOURCE_CRDA);
8651         /* set_regdom takes ownership of rd */
8652         rd = NULL;
8653  bad_reg:
8654         kfree(rd);
8655  out:
8656         rtnl_unlock();
8657         return r;
8658 }
8659 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
8660
8661 static int validate_scan_freqs(struct nlattr *freqs)
8662 {
8663         struct nlattr *attr1, *attr2;
8664         int n_channels = 0, tmp1, tmp2;
8665
8666         nla_for_each_nested(attr1, freqs, tmp1)
8667                 if (nla_len(attr1) != sizeof(u32))
8668                         return 0;
8669
8670         nla_for_each_nested(attr1, freqs, tmp1) {
8671                 n_channels++;
8672                 /*
8673                  * Some hardware has a limited channel list for
8674                  * scanning, and it is pretty much nonsensical
8675                  * to scan for a channel twice, so disallow that
8676                  * and don't require drivers to check that the
8677                  * channel list they get isn't longer than what
8678                  * they can scan, as long as they can scan all
8679                  * the channels they registered at once.
8680                  */
8681                 nla_for_each_nested(attr2, freqs, tmp2)
8682                         if (attr1 != attr2 &&
8683                             nla_get_u32(attr1) == nla_get_u32(attr2))
8684                                 return 0;
8685         }
8686
8687         return n_channels;
8688 }
8689
8690 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
8691 {
8692         return b < NUM_NL80211_BANDS && wiphy->bands[b];
8693 }
8694
8695 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
8696                             struct cfg80211_bss_selection *bss_select)
8697 {
8698         struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
8699         struct nlattr *nest;
8700         int err;
8701         bool found = false;
8702         int i;
8703
8704         /* only process one nested attribute */
8705         nest = nla_data(nla);
8706         if (!nla_ok(nest, nla_len(nest)))
8707                 return -EINVAL;
8708
8709         err = nla_parse_nested_deprecated(attr, NL80211_BSS_SELECT_ATTR_MAX,
8710                                           nest, nl80211_bss_select_policy,
8711                                           NULL);
8712         if (err)
8713                 return err;
8714
8715         /* only one attribute may be given */
8716         for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
8717                 if (attr[i]) {
8718                         if (found)
8719                                 return -EINVAL;
8720                         found = true;
8721                 }
8722         }
8723
8724         bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
8725
8726         if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
8727                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
8728
8729         if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
8730                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
8731                 bss_select->param.band_pref =
8732                         nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
8733                 if (!is_band_valid(wiphy, bss_select->param.band_pref))
8734                         return -EINVAL;
8735         }
8736
8737         if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
8738                 struct nl80211_bss_select_rssi_adjust *adj_param;
8739
8740                 adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
8741                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
8742                 bss_select->param.adjust.band = adj_param->band;
8743                 bss_select->param.adjust.delta = adj_param->delta;
8744                 if (!is_band_valid(wiphy, bss_select->param.adjust.band))
8745                         return -EINVAL;
8746         }
8747
8748         /* user-space did not provide behaviour attribute */
8749         if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
8750                 return -EINVAL;
8751
8752         if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
8753                 return -EINVAL;
8754
8755         return 0;
8756 }
8757
8758 int nl80211_parse_random_mac(struct nlattr **attrs,
8759                              u8 *mac_addr, u8 *mac_addr_mask)
8760 {
8761         int i;
8762
8763         if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
8764                 eth_zero_addr(mac_addr);
8765                 eth_zero_addr(mac_addr_mask);
8766                 mac_addr[0] = 0x2;
8767                 mac_addr_mask[0] = 0x3;
8768
8769                 return 0;
8770         }
8771
8772         /* need both or none */
8773         if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
8774                 return -EINVAL;
8775
8776         memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
8777         memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
8778
8779         /* don't allow or configure an mcast address */
8780         if (!is_multicast_ether_addr(mac_addr_mask) ||
8781             is_multicast_ether_addr(mac_addr))
8782                 return -EINVAL;
8783
8784         /*
8785          * allow users to pass a MAC address that has bits set outside
8786          * of the mask, but don't bother drivers with having to deal
8787          * with such bits
8788          */
8789         for (i = 0; i < ETH_ALEN; i++)
8790                 mac_addr[i] &= mac_addr_mask[i];
8791
8792         return 0;
8793 }
8794
8795 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev,
8796                                               struct ieee80211_channel *chan)
8797 {
8798         unsigned int link_id;
8799         bool all_ok = true;
8800
8801         ASSERT_WDEV_LOCK(wdev);
8802
8803         if (!cfg80211_beaconing_iface_active(wdev))
8804                 return true;
8805
8806         /*
8807          * FIXME: check if we have a free HW resource/link for chan
8808          *
8809          * This, as well as the FIXME below, requires knowing the link
8810          * capabilities of the hardware.
8811          */
8812
8813         /* we cannot leave radar channels */
8814         for_each_valid_link(wdev, link_id) {
8815                 struct cfg80211_chan_def *chandef;
8816
8817                 chandef = wdev_chandef(wdev, link_id);
8818                 if (!chandef || !chandef->chan)
8819                         continue;
8820
8821                 /*
8822                  * FIXME: don't require all_ok, but rather check only the
8823                  *        correct HW resource/link onto which 'chan' falls,
8824                  *        as only that link leaves the channel for doing
8825                  *        the off-channel operation.
8826                  */
8827
8828                 if (chandef->chan->flags & IEEE80211_CHAN_RADAR)
8829                         all_ok = false;
8830         }
8831
8832         if (all_ok)
8833                 return true;
8834
8835         return regulatory_pre_cac_allowed(wdev->wiphy);
8836 }
8837
8838 static bool nl80211_check_scan_feat(struct wiphy *wiphy, u32 flags, u32 flag,
8839                                     enum nl80211_ext_feature_index feat)
8840 {
8841         if (!(flags & flag))
8842                 return true;
8843         if (wiphy_ext_feature_isset(wiphy, feat))
8844                 return true;
8845         return false;
8846 }
8847
8848 static int
8849 nl80211_check_scan_flags(struct wiphy *wiphy, struct wireless_dev *wdev,
8850                          void *request, struct nlattr **attrs,
8851                          bool is_sched_scan)
8852 {
8853         u8 *mac_addr, *mac_addr_mask;
8854         u32 *flags;
8855         enum nl80211_feature_flags randomness_flag;
8856
8857         if (!attrs[NL80211_ATTR_SCAN_FLAGS])
8858                 return 0;
8859
8860         if (is_sched_scan) {
8861                 struct cfg80211_sched_scan_request *req = request;
8862
8863                 randomness_flag = wdev ?
8864                                   NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR :
8865                                   NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
8866                 flags = &req->flags;
8867                 mac_addr = req->mac_addr;
8868                 mac_addr_mask = req->mac_addr_mask;
8869         } else {
8870                 struct cfg80211_scan_request *req = request;
8871
8872                 randomness_flag = NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
8873                 flags = &req->flags;
8874                 mac_addr = req->mac_addr;
8875                 mac_addr_mask = req->mac_addr_mask;
8876         }
8877
8878         *flags = nla_get_u32(attrs[NL80211_ATTR_SCAN_FLAGS]);
8879
8880         if (((*flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
8881              !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
8882             !nl80211_check_scan_feat(wiphy, *flags,
8883                                      NL80211_SCAN_FLAG_LOW_SPAN,
8884                                      NL80211_EXT_FEATURE_LOW_SPAN_SCAN) ||
8885             !nl80211_check_scan_feat(wiphy, *flags,
8886                                      NL80211_SCAN_FLAG_LOW_POWER,
8887                                      NL80211_EXT_FEATURE_LOW_POWER_SCAN) ||
8888             !nl80211_check_scan_feat(wiphy, *flags,
8889                                      NL80211_SCAN_FLAG_HIGH_ACCURACY,
8890                                      NL80211_EXT_FEATURE_HIGH_ACCURACY_SCAN) ||
8891             !nl80211_check_scan_feat(wiphy, *flags,
8892                                      NL80211_SCAN_FLAG_FILS_MAX_CHANNEL_TIME,
8893                                      NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME) ||
8894             !nl80211_check_scan_feat(wiphy, *flags,
8895                                      NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP,
8896                                      NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP) ||
8897             !nl80211_check_scan_feat(wiphy, *flags,
8898                                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION,
8899                                      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION) ||
8900             !nl80211_check_scan_feat(wiphy, *flags,
8901                                      NL80211_SCAN_FLAG_OCE_PROBE_REQ_HIGH_TX_RATE,
8902                                      NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE) ||
8903             !nl80211_check_scan_feat(wiphy, *flags,
8904                                      NL80211_SCAN_FLAG_RANDOM_SN,
8905                                      NL80211_EXT_FEATURE_SCAN_RANDOM_SN) ||
8906             !nl80211_check_scan_feat(wiphy, *flags,
8907                                      NL80211_SCAN_FLAG_MIN_PREQ_CONTENT,
8908                                      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT))
8909                 return -EOPNOTSUPP;
8910
8911         if (*flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
8912                 int err;
8913
8914                 if (!(wiphy->features & randomness_flag) ||
8915                     (wdev && wdev->connected))
8916                         return -EOPNOTSUPP;
8917
8918                 err = nl80211_parse_random_mac(attrs, mac_addr, mac_addr_mask);
8919                 if (err)
8920                         return err;
8921         }
8922
8923         return 0;
8924 }
8925
8926 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
8927 {
8928         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8929         struct wireless_dev *wdev = info->user_ptr[1];
8930         struct cfg80211_scan_request *request;
8931         struct nlattr *scan_freqs = NULL;
8932         bool scan_freqs_khz = false;
8933         struct nlattr *attr;
8934         struct wiphy *wiphy;
8935         int err, tmp, n_ssids = 0, n_channels, i;
8936         size_t ie_len;
8937
8938         wiphy = &rdev->wiphy;
8939
8940         if (wdev->iftype == NL80211_IFTYPE_NAN)
8941                 return -EOPNOTSUPP;
8942
8943         if (!rdev->ops->scan)
8944                 return -EOPNOTSUPP;
8945
8946         if (rdev->scan_req || rdev->scan_msg)
8947                 return -EBUSY;
8948
8949         if (info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ]) {
8950                 if (!wiphy_ext_feature_isset(wiphy,
8951                                              NL80211_EXT_FEATURE_SCAN_FREQ_KHZ))
8952                         return -EOPNOTSUPP;
8953                 scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQ_KHZ];
8954                 scan_freqs_khz = true;
8955         } else if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES])
8956                 scan_freqs = info->attrs[NL80211_ATTR_SCAN_FREQUENCIES];
8957
8958         if (scan_freqs) {
8959                 n_channels = validate_scan_freqs(scan_freqs);
8960                 if (!n_channels)
8961                         return -EINVAL;
8962         } else {
8963                 n_channels = ieee80211_get_num_supported_channels(wiphy);
8964         }
8965
8966         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
8967                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
8968                         n_ssids++;
8969
8970         if (n_ssids > wiphy->max_scan_ssids)
8971                 return -EINVAL;
8972
8973         if (info->attrs[NL80211_ATTR_IE])
8974                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8975         else
8976                 ie_len = 0;
8977
8978         if (ie_len > wiphy->max_scan_ie_len)
8979                 return -EINVAL;
8980
8981         request = kzalloc(sizeof(*request)
8982                         + sizeof(*request->ssids) * n_ssids
8983                         + sizeof(*request->channels) * n_channels
8984                         + ie_len, GFP_KERNEL);
8985         if (!request)
8986                 return -ENOMEM;
8987
8988         if (n_ssids)
8989                 request->ssids = (void *)&request->channels[n_channels];
8990         request->n_ssids = n_ssids;
8991         if (ie_len) {
8992                 if (n_ssids)
8993                         request->ie = (void *)(request->ssids + n_ssids);
8994                 else
8995                         request->ie = (void *)(request->channels + n_channels);
8996         }
8997
8998         i = 0;
8999         if (scan_freqs) {
9000                 /* user specified, bail out if channel not found */
9001                 nla_for_each_nested(attr, scan_freqs, tmp) {
9002                         struct ieee80211_channel *chan;
9003                         int freq = nla_get_u32(attr);
9004
9005                         if (!scan_freqs_khz)
9006                                 freq = MHZ_TO_KHZ(freq);
9007
9008                         chan = ieee80211_get_channel_khz(wiphy, freq);
9009                         if (!chan) {
9010                                 err = -EINVAL;
9011                                 goto out_free;
9012                         }
9013
9014                         /* ignore disabled channels */
9015                         if (chan->flags & IEEE80211_CHAN_DISABLED)
9016                                 continue;
9017
9018                         request->channels[i] = chan;
9019                         i++;
9020                 }
9021         } else {
9022                 enum nl80211_band band;
9023
9024                 /* all channels */
9025                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
9026                         int j;
9027
9028                         if (!wiphy->bands[band])
9029                                 continue;
9030                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9031                                 struct ieee80211_channel *chan;
9032
9033                                 chan = &wiphy->bands[band]->channels[j];
9034
9035                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
9036                                         continue;
9037
9038                                 request->channels[i] = chan;
9039                                 i++;
9040                         }
9041                 }
9042         }
9043
9044         if (!i) {
9045                 err = -EINVAL;
9046                 goto out_free;
9047         }
9048
9049         request->n_channels = i;
9050
9051         wdev_lock(wdev);
9052         for (i = 0; i < request->n_channels; i++) {
9053                 struct ieee80211_channel *chan = request->channels[i];
9054
9055                 /* if we can go off-channel to the target channel we're good */
9056                 if (cfg80211_off_channel_oper_allowed(wdev, chan))
9057                         continue;
9058
9059                 if (!cfg80211_wdev_on_sub_chan(wdev, chan, true)) {
9060                         wdev_unlock(wdev);
9061                         err = -EBUSY;
9062                         goto out_free;
9063                 }
9064         }
9065         wdev_unlock(wdev);
9066
9067         i = 0;
9068         if (n_ssids) {
9069                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
9070                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9071                                 err = -EINVAL;
9072                                 goto out_free;
9073                         }
9074                         request->ssids[i].ssid_len = nla_len(attr);
9075                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
9076                         i++;
9077                 }
9078         }
9079
9080         if (info->attrs[NL80211_ATTR_IE]) {
9081                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9082                 memcpy((void *)request->ie,
9083                        nla_data(info->attrs[NL80211_ATTR_IE]),
9084                        request->ie_len);
9085         }
9086
9087         for (i = 0; i < NUM_NL80211_BANDS; i++)
9088                 if (wiphy->bands[i])
9089                         request->rates[i] =
9090                                 (1 << wiphy->bands[i]->n_bitrates) - 1;
9091
9092         if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
9093                 nla_for_each_nested(attr,
9094                                     info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
9095                                     tmp) {
9096                         enum nl80211_band band = nla_type(attr);
9097
9098                         if (band < 0 || band >= NUM_NL80211_BANDS) {
9099                                 err = -EINVAL;
9100                                 goto out_free;
9101                         }
9102
9103                         if (!wiphy->bands[band])
9104                                 continue;
9105
9106                         err = ieee80211_get_ratemask(wiphy->bands[band],
9107                                                      nla_data(attr),
9108                                                      nla_len(attr),
9109                                                      &request->rates[band]);
9110                         if (err)
9111                                 goto out_free;
9112                 }
9113         }
9114
9115         if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
9116                 request->duration =
9117                         nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
9118                 request->duration_mandatory =
9119                         nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
9120         }
9121
9122         err = nl80211_check_scan_flags(wiphy, wdev, request, info->attrs,
9123                                        false);
9124         if (err)
9125                 goto out_free;
9126
9127         request->no_cck =
9128                 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9129
9130         /* Initial implementation used NL80211_ATTR_MAC to set the specific
9131          * BSSID to scan for. This was problematic because that same attribute
9132          * was already used for another purpose (local random MAC address). The
9133          * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
9134          * compatibility with older userspace components, also use the
9135          * NL80211_ATTR_MAC value here if it can be determined to be used for
9136          * the specific BSSID use case instead of the random MAC address
9137          * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
9138          */
9139         if (info->attrs[NL80211_ATTR_BSSID])
9140                 memcpy(request->bssid,
9141                        nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
9142         else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
9143                  info->attrs[NL80211_ATTR_MAC])
9144                 memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
9145                        ETH_ALEN);
9146         else
9147                 eth_broadcast_addr(request->bssid);
9148
9149         request->wdev = wdev;
9150         request->wiphy = &rdev->wiphy;
9151         request->scan_start = jiffies;
9152
9153         rdev->scan_req = request;
9154         err = cfg80211_scan(rdev);
9155
9156         if (err)
9157                 goto out_free;
9158
9159         nl80211_send_scan_start(rdev, wdev);
9160         dev_hold(wdev->netdev);
9161
9162         return 0;
9163
9164  out_free:
9165         rdev->scan_req = NULL;
9166         kfree(request);
9167
9168         return err;
9169 }
9170
9171 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
9172 {
9173         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9174         struct wireless_dev *wdev = info->user_ptr[1];
9175
9176         if (!rdev->ops->abort_scan)
9177                 return -EOPNOTSUPP;
9178
9179         if (rdev->scan_msg)
9180                 return 0;
9181
9182         if (!rdev->scan_req)
9183                 return -ENOENT;
9184
9185         rdev_abort_scan(rdev, wdev);
9186         return 0;
9187 }
9188
9189 static int
9190 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
9191                                struct cfg80211_sched_scan_request *request,
9192                                struct nlattr **attrs)
9193 {
9194         int tmp, err, i = 0;
9195         struct nlattr *attr;
9196
9197         if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9198                 u32 interval;
9199
9200                 /*
9201                  * If scan plans are not specified,
9202                  * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
9203                  * case one scan plan will be set with the specified scan
9204                  * interval and infinite number of iterations.
9205                  */
9206                 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
9207                 if (!interval)
9208                         return -EINVAL;
9209
9210                 request->scan_plans[0].interval =
9211                         DIV_ROUND_UP(interval, MSEC_PER_SEC);
9212                 if (!request->scan_plans[0].interval)
9213                         return -EINVAL;
9214
9215                 if (request->scan_plans[0].interval >
9216                     wiphy->max_sched_scan_plan_interval)
9217                         request->scan_plans[0].interval =
9218                                 wiphy->max_sched_scan_plan_interval;
9219
9220                 return 0;
9221         }
9222
9223         nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
9224                 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
9225
9226                 if (WARN_ON(i >= n_plans))
9227                         return -EINVAL;
9228
9229                 err = nla_parse_nested_deprecated(plan,
9230                                                   NL80211_SCHED_SCAN_PLAN_MAX,
9231                                                   attr, nl80211_plan_policy,
9232                                                   NULL);
9233                 if (err)
9234                         return err;
9235
9236                 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
9237                         return -EINVAL;
9238
9239                 request->scan_plans[i].interval =
9240                         nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
9241                 if (!request->scan_plans[i].interval ||
9242                     request->scan_plans[i].interval >
9243                     wiphy->max_sched_scan_plan_interval)
9244                         return -EINVAL;
9245
9246                 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
9247                         request->scan_plans[i].iterations =
9248                                 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
9249                         if (!request->scan_plans[i].iterations ||
9250                             (request->scan_plans[i].iterations >
9251                              wiphy->max_sched_scan_plan_iterations))
9252                                 return -EINVAL;
9253                 } else if (i < n_plans - 1) {
9254                         /*
9255                          * All scan plans but the last one must specify
9256                          * a finite number of iterations
9257                          */
9258                         return -EINVAL;
9259                 }
9260
9261                 i++;
9262         }
9263
9264         /*
9265          * The last scan plan must not specify the number of
9266          * iterations, it is supposed to run infinitely
9267          */
9268         if (request->scan_plans[n_plans - 1].iterations)
9269                 return  -EINVAL;
9270
9271         return 0;
9272 }
9273
9274 static int
9275 nl80211_parse_sched_scan_per_band_rssi(struct wiphy *wiphy,
9276                                        struct cfg80211_match_set *match_sets,
9277                                        struct nlattr *tb_band_rssi,
9278                                        s32 rssi_thold)
9279 {
9280         struct nlattr *attr;
9281         int i, tmp, ret = 0;
9282
9283         if (!wiphy_ext_feature_isset(wiphy,
9284                     NL80211_EXT_FEATURE_SCHED_SCAN_BAND_SPECIFIC_RSSI_THOLD)) {
9285                 if (tb_band_rssi)
9286                         ret = -EOPNOTSUPP;
9287                 else
9288                         for (i = 0; i < NUM_NL80211_BANDS; i++)
9289                                 match_sets->per_band_rssi_thold[i] =
9290                                         NL80211_SCAN_RSSI_THOLD_OFF;
9291                 return ret;
9292         }
9293
9294         for (i = 0; i < NUM_NL80211_BANDS; i++)
9295                 match_sets->per_band_rssi_thold[i] = rssi_thold;
9296
9297         nla_for_each_nested(attr, tb_band_rssi, tmp) {
9298                 enum nl80211_band band = nla_type(attr);
9299
9300                 if (band < 0 || band >= NUM_NL80211_BANDS)
9301                         return -EINVAL;
9302
9303                 match_sets->per_band_rssi_thold[band] = nla_get_s32(attr);
9304         }
9305
9306         return 0;
9307 }
9308
9309 static struct cfg80211_sched_scan_request *
9310 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
9311                          struct nlattr **attrs, int max_match_sets)
9312 {
9313         struct cfg80211_sched_scan_request *request;
9314         struct nlattr *attr;
9315         int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
9316         enum nl80211_band band;
9317         size_t ie_len;
9318         struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
9319         s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
9320
9321         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9322                 n_channels = validate_scan_freqs(
9323                                 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
9324                 if (!n_channels)
9325                         return ERR_PTR(-EINVAL);
9326         } else {
9327                 n_channels = ieee80211_get_num_supported_channels(wiphy);
9328         }
9329
9330         if (attrs[NL80211_ATTR_SCAN_SSIDS])
9331                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9332                                     tmp)
9333                         n_ssids++;
9334
9335         if (n_ssids > wiphy->max_sched_scan_ssids)
9336                 return ERR_PTR(-EINVAL);
9337
9338         /*
9339          * First, count the number of 'real' matchsets. Due to an issue with
9340          * the old implementation, matchsets containing only the RSSI attribute
9341          * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
9342          * RSSI for all matchsets, rather than their own matchset for reporting
9343          * all APs with a strong RSSI. This is needed to be compatible with
9344          * older userspace that treated a matchset with only the RSSI as the
9345          * global RSSI for all other matchsets - if there are other matchsets.
9346          */
9347         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9348                 nla_for_each_nested(attr,
9349                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9350                                     tmp) {
9351                         struct nlattr *rssi;
9352
9353                         err = nla_parse_nested_deprecated(tb,
9354                                                           NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9355                                                           attr,
9356                                                           nl80211_match_policy,
9357                                                           NULL);
9358                         if (err)
9359                                 return ERR_PTR(err);
9360
9361                         /* SSID and BSSID are mutually exclusive */
9362                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
9363                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
9364                                 return ERR_PTR(-EINVAL);
9365
9366                         /* add other standalone attributes here */
9367                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
9368                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
9369                                 n_match_sets++;
9370                                 continue;
9371                         }
9372                         rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9373                         if (rssi)
9374                                 default_match_rssi = nla_get_s32(rssi);
9375                 }
9376         }
9377
9378         /* However, if there's no other matchset, add the RSSI one */
9379         if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
9380                 n_match_sets = 1;
9381
9382         if (n_match_sets > max_match_sets)
9383                 return ERR_PTR(-EINVAL);
9384
9385         if (attrs[NL80211_ATTR_IE])
9386                 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
9387         else
9388                 ie_len = 0;
9389
9390         if (ie_len > wiphy->max_sched_scan_ie_len)
9391                 return ERR_PTR(-EINVAL);
9392
9393         if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
9394                 /*
9395                  * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
9396                  * each scan plan already specifies its own interval
9397                  */
9398                 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9399                         return ERR_PTR(-EINVAL);
9400
9401                 nla_for_each_nested(attr,
9402                                     attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
9403                         n_plans++;
9404         } else {
9405                 /*
9406                  * The scan interval attribute is kept for backward
9407                  * compatibility. If no scan plans are specified and sched scan
9408                  * interval is specified, one scan plan will be set with this
9409                  * scan interval and infinite number of iterations.
9410                  */
9411                 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
9412                         return ERR_PTR(-EINVAL);
9413
9414                 n_plans = 1;
9415         }
9416
9417         if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
9418                 return ERR_PTR(-EINVAL);
9419
9420         if (!wiphy_ext_feature_isset(
9421                     wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
9422             (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
9423              attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
9424                 return ERR_PTR(-EINVAL);
9425
9426         request = kzalloc(sizeof(*request)
9427                         + sizeof(*request->ssids) * n_ssids
9428                         + sizeof(*request->match_sets) * n_match_sets
9429                         + sizeof(*request->scan_plans) * n_plans
9430                         + sizeof(*request->channels) * n_channels
9431                         + ie_len, GFP_KERNEL);
9432         if (!request)
9433                 return ERR_PTR(-ENOMEM);
9434
9435         if (n_ssids)
9436                 request->ssids = (void *)&request->channels[n_channels];
9437         request->n_ssids = n_ssids;
9438         if (ie_len) {
9439                 if (n_ssids)
9440                         request->ie = (void *)(request->ssids + n_ssids);
9441                 else
9442                         request->ie = (void *)(request->channels + n_channels);
9443         }
9444
9445         if (n_match_sets) {
9446                 if (request->ie)
9447                         request->match_sets = (void *)(request->ie + ie_len);
9448                 else if (n_ssids)
9449                         request->match_sets =
9450                                 (void *)(request->ssids + n_ssids);
9451                 else
9452                         request->match_sets =
9453                                 (void *)(request->channels + n_channels);
9454         }
9455         request->n_match_sets = n_match_sets;
9456
9457         if (n_match_sets)
9458                 request->scan_plans = (void *)(request->match_sets +
9459                                                n_match_sets);
9460         else if (request->ie)
9461                 request->scan_plans = (void *)(request->ie + ie_len);
9462         else if (n_ssids)
9463                 request->scan_plans = (void *)(request->ssids + n_ssids);
9464         else
9465                 request->scan_plans = (void *)(request->channels + n_channels);
9466
9467         request->n_scan_plans = n_plans;
9468
9469         i = 0;
9470         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
9471                 /* user specified, bail out if channel not found */
9472                 nla_for_each_nested(attr,
9473                                     attrs[NL80211_ATTR_SCAN_FREQUENCIES],
9474                                     tmp) {
9475                         struct ieee80211_channel *chan;
9476
9477                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
9478
9479                         if (!chan) {
9480                                 err = -EINVAL;
9481                                 goto out_free;
9482                         }
9483
9484                         /* ignore disabled channels */
9485                         if (chan->flags & IEEE80211_CHAN_DISABLED)
9486                                 continue;
9487
9488                         request->channels[i] = chan;
9489                         i++;
9490                 }
9491         } else {
9492                 /* all channels */
9493                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
9494                         int j;
9495
9496                         if (!wiphy->bands[band])
9497                                 continue;
9498                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
9499                                 struct ieee80211_channel *chan;
9500
9501                                 chan = &wiphy->bands[band]->channels[j];
9502
9503                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
9504                                         continue;
9505
9506                                 request->channels[i] = chan;
9507                                 i++;
9508                         }
9509                 }
9510         }
9511
9512         if (!i) {
9513                 err = -EINVAL;
9514                 goto out_free;
9515         }
9516
9517         request->n_channels = i;
9518
9519         i = 0;
9520         if (n_ssids) {
9521                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
9522                                     tmp) {
9523                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
9524                                 err = -EINVAL;
9525                                 goto out_free;
9526                         }
9527                         request->ssids[i].ssid_len = nla_len(attr);
9528                         memcpy(request->ssids[i].ssid, nla_data(attr),
9529                                nla_len(attr));
9530                         i++;
9531                 }
9532         }
9533
9534         i = 0;
9535         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
9536                 nla_for_each_nested(attr,
9537                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
9538                                     tmp) {
9539                         struct nlattr *ssid, *bssid, *rssi;
9540
9541                         err = nla_parse_nested_deprecated(tb,
9542                                                           NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
9543                                                           attr,
9544                                                           nl80211_match_policy,
9545                                                           NULL);
9546                         if (err)
9547                                 goto out_free;
9548                         ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
9549                         bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
9550
9551                         if (!ssid && !bssid) {
9552                                 i++;
9553                                 continue;
9554                         }
9555
9556                         if (WARN_ON(i >= n_match_sets)) {
9557                                 /* this indicates a programming error,
9558                                  * the loop above should have verified
9559                                  * things properly
9560                                  */
9561                                 err = -EINVAL;
9562                                 goto out_free;
9563                         }
9564
9565                         if (ssid) {
9566                                 memcpy(request->match_sets[i].ssid.ssid,
9567                                        nla_data(ssid), nla_len(ssid));
9568                                 request->match_sets[i].ssid.ssid_len =
9569                                         nla_len(ssid);
9570                         }
9571                         if (bssid)
9572                                 memcpy(request->match_sets[i].bssid,
9573                                        nla_data(bssid), ETH_ALEN);
9574
9575                         /* special attribute - old implementation w/a */
9576                         request->match_sets[i].rssi_thold = default_match_rssi;
9577                         rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
9578                         if (rssi)
9579                                 request->match_sets[i].rssi_thold =
9580                                         nla_get_s32(rssi);
9581
9582                         /* Parse per band RSSI attribute */
9583                         err = nl80211_parse_sched_scan_per_band_rssi(wiphy,
9584                                 &request->match_sets[i],
9585                                 tb[NL80211_SCHED_SCAN_MATCH_PER_BAND_RSSI],
9586                                 request->match_sets[i].rssi_thold);
9587                         if (err)
9588                                 goto out_free;
9589
9590                         i++;
9591                 }
9592
9593                 /* there was no other matchset, so the RSSI one is alone */
9594                 if (i == 0 && n_match_sets)
9595                         request->match_sets[0].rssi_thold = default_match_rssi;
9596
9597                 request->min_rssi_thold = INT_MAX;
9598                 for (i = 0; i < n_match_sets; i++)
9599                         request->min_rssi_thold =
9600                                 min(request->match_sets[i].rssi_thold,
9601                                     request->min_rssi_thold);
9602         } else {
9603                 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
9604         }
9605
9606         if (ie_len) {
9607                 request->ie_len = ie_len;
9608                 memcpy((void *)request->ie,
9609                        nla_data(attrs[NL80211_ATTR_IE]),
9610                        request->ie_len);
9611         }
9612
9613         err = nl80211_check_scan_flags(wiphy, wdev, request, attrs, true);
9614         if (err)
9615                 goto out_free;
9616
9617         if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
9618                 request->delay =
9619                         nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
9620
9621         if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
9622                 request->relative_rssi = nla_get_s8(
9623                         attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
9624                 request->relative_rssi_set = true;
9625         }
9626
9627         if (request->relative_rssi_set &&
9628             attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
9629                 struct nl80211_bss_select_rssi_adjust *rssi_adjust;
9630
9631                 rssi_adjust = nla_data(
9632                         attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
9633                 request->rssi_adjust.band = rssi_adjust->band;
9634                 request->rssi_adjust.delta = rssi_adjust->delta;
9635                 if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
9636                         err = -EINVAL;
9637                         goto out_free;
9638                 }
9639         }
9640
9641         err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
9642         if (err)
9643                 goto out_free;
9644
9645         request->scan_start = jiffies;
9646
9647         return request;
9648
9649 out_free:
9650         kfree(request);
9651         return ERR_PTR(err);
9652 }
9653
9654 static int nl80211_start_sched_scan(struct sk_buff *skb,
9655                                     struct genl_info *info)
9656 {
9657         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9658         struct net_device *dev = info->user_ptr[1];
9659         struct wireless_dev *wdev = dev->ieee80211_ptr;
9660         struct cfg80211_sched_scan_request *sched_scan_req;
9661         bool want_multi;
9662         int err;
9663
9664         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
9665                 return -EOPNOTSUPP;
9666
9667         want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
9668         err = cfg80211_sched_scan_req_possible(rdev, want_multi);
9669         if (err)
9670                 return err;
9671
9672         sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
9673                                                   info->attrs,
9674                                                   rdev->wiphy.max_match_sets);
9675
9676         err = PTR_ERR_OR_ZERO(sched_scan_req);
9677         if (err)
9678                 goto out_err;
9679
9680         /* leave request id zero for legacy request
9681          * or if driver does not support multi-scheduled scan
9682          */
9683         if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1)
9684                 sched_scan_req->reqid = cfg80211_assign_cookie(rdev);
9685
9686         err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
9687         if (err)
9688                 goto out_free;
9689
9690         sched_scan_req->dev = dev;
9691         sched_scan_req->wiphy = &rdev->wiphy;
9692
9693         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
9694                 sched_scan_req->owner_nlportid = info->snd_portid;
9695
9696         cfg80211_add_sched_scan_req(rdev, sched_scan_req);
9697
9698         nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
9699         return 0;
9700
9701 out_free:
9702         kfree(sched_scan_req);
9703 out_err:
9704         return err;
9705 }
9706
9707 static int nl80211_stop_sched_scan(struct sk_buff *skb,
9708                                    struct genl_info *info)
9709 {
9710         struct cfg80211_sched_scan_request *req;
9711         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9712         u64 cookie;
9713
9714         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
9715                 return -EOPNOTSUPP;
9716
9717         if (info->attrs[NL80211_ATTR_COOKIE]) {
9718                 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9719                 return __cfg80211_stop_sched_scan(rdev, cookie, false);
9720         }
9721
9722         req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
9723                                      struct cfg80211_sched_scan_request,
9724                                      list);
9725         if (!req || req->reqid ||
9726             (req->owner_nlportid &&
9727              req->owner_nlportid != info->snd_portid))
9728                 return -ENOENT;
9729
9730         return cfg80211_stop_sched_scan_req(rdev, req, false);
9731 }
9732
9733 static int nl80211_start_radar_detection(struct sk_buff *skb,
9734                                          struct genl_info *info)
9735 {
9736         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9737         struct net_device *dev = info->user_ptr[1];
9738         struct wireless_dev *wdev = dev->ieee80211_ptr;
9739         struct wiphy *wiphy = wdev->wiphy;
9740         struct cfg80211_chan_def chandef;
9741         enum nl80211_dfs_regions dfs_region;
9742         unsigned int cac_time_ms;
9743         int err = -EINVAL;
9744
9745         flush_delayed_work(&rdev->dfs_update_channels_wk);
9746
9747         wiphy_lock(wiphy);
9748
9749         dfs_region = reg_get_dfs_region(wiphy);
9750         if (dfs_region == NL80211_DFS_UNSET)
9751                 goto unlock;
9752
9753         err = nl80211_parse_chandef(rdev, info, &chandef);
9754         if (err)
9755                 goto unlock;
9756
9757         err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9758         if (err < 0)
9759                 goto unlock;
9760
9761         if (err == 0) {
9762                 err = -EINVAL;
9763                 goto unlock;
9764         }
9765
9766         if (!cfg80211_chandef_dfs_usable(wiphy, &chandef)) {
9767                 err = -EINVAL;
9768                 goto unlock;
9769         }
9770
9771         if (nla_get_flag(info->attrs[NL80211_ATTR_RADAR_BACKGROUND])) {
9772                 err = cfg80211_start_background_radar_detection(rdev, wdev,
9773                                                                 &chandef);
9774                 goto unlock;
9775         }
9776
9777         if (netif_carrier_ok(dev)) {
9778                 err = -EBUSY;
9779                 goto unlock;
9780         }
9781
9782         if (wdev->cac_started) {
9783                 err = -EBUSY;
9784                 goto unlock;
9785         }
9786
9787         /* CAC start is offloaded to HW and can't be started manually */
9788         if (wiphy_ext_feature_isset(wiphy, NL80211_EXT_FEATURE_DFS_OFFLOAD)) {
9789                 err = -EOPNOTSUPP;
9790                 goto unlock;
9791         }
9792
9793         if (!rdev->ops->start_radar_detection) {
9794                 err = -EOPNOTSUPP;
9795                 goto unlock;
9796         }
9797
9798         cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
9799         if (WARN_ON(!cac_time_ms))
9800                 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
9801
9802         err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
9803         if (!err) {
9804                 wdev->links[0].ap.chandef = chandef;
9805                 wdev->cac_started = true;
9806                 wdev->cac_start_time = jiffies;
9807                 wdev->cac_time_ms = cac_time_ms;
9808         }
9809 unlock:
9810         wiphy_unlock(wiphy);
9811
9812         return err;
9813 }
9814
9815 static int nl80211_notify_radar_detection(struct sk_buff *skb,
9816                                           struct genl_info *info)
9817 {
9818         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9819         struct net_device *dev = info->user_ptr[1];
9820         struct wireless_dev *wdev = dev->ieee80211_ptr;
9821         struct wiphy *wiphy = wdev->wiphy;
9822         struct cfg80211_chan_def chandef;
9823         enum nl80211_dfs_regions dfs_region;
9824         int err;
9825
9826         dfs_region = reg_get_dfs_region(wiphy);
9827         if (dfs_region == NL80211_DFS_UNSET) {
9828                 GENL_SET_ERR_MSG(info,
9829                                  "DFS Region is not set. Unexpected Radar indication");
9830                 return -EINVAL;
9831         }
9832
9833         err = nl80211_parse_chandef(rdev, info, &chandef);
9834         if (err) {
9835                 GENL_SET_ERR_MSG(info, "Unable to extract chandef info");
9836                 return err;
9837         }
9838
9839         err = cfg80211_chandef_dfs_required(wiphy, &chandef, wdev->iftype);
9840         if (err < 0) {
9841                 GENL_SET_ERR_MSG(info, "chandef is invalid");
9842                 return err;
9843         }
9844
9845         if (err == 0) {
9846                 GENL_SET_ERR_MSG(info,
9847                                  "Unexpected Radar indication for chandef/iftype");
9848                 return -EINVAL;
9849         }
9850
9851         /* Do not process this notification if radar is already detected
9852          * by kernel on this channel, and return success.
9853          */
9854         if (chandef.chan->dfs_state == NL80211_DFS_UNAVAILABLE)
9855                 return 0;
9856
9857         cfg80211_set_dfs_state(wiphy, &chandef, NL80211_DFS_UNAVAILABLE);
9858
9859         cfg80211_sched_dfs_chan_update(rdev);
9860
9861         rdev->radar_chandef = chandef;
9862
9863         /* Propagate this notification to other radios as well */
9864         queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
9865
9866         return 0;
9867 }
9868
9869 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
9870 {
9871         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9872         unsigned int link_id = nl80211_link_id(info->attrs);
9873         struct net_device *dev = info->user_ptr[1];
9874         struct wireless_dev *wdev = dev->ieee80211_ptr;
9875         struct cfg80211_csa_settings params;
9876         struct nlattr **csa_attrs = NULL;
9877         int err;
9878         bool need_new_beacon = false;
9879         bool need_handle_dfs_flag = true;
9880         int len, i;
9881         u32 cs_count;
9882
9883         if (!rdev->ops->channel_switch ||
9884             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
9885                 return -EOPNOTSUPP;
9886
9887         switch (dev->ieee80211_ptr->iftype) {
9888         case NL80211_IFTYPE_AP:
9889         case NL80211_IFTYPE_P2P_GO:
9890                 need_new_beacon = true;
9891                 /* For all modes except AP the handle_dfs flag needs to be
9892                  * supplied to tell the kernel that userspace will handle radar
9893                  * events when they happen. Otherwise a switch to a channel
9894                  * requiring DFS will be rejected.
9895                  */
9896                 need_handle_dfs_flag = false;
9897
9898                 /* useless if AP is not running */
9899                 if (!wdev->links[link_id].ap.beacon_interval)
9900                         return -ENOTCONN;
9901                 break;
9902         case NL80211_IFTYPE_ADHOC:
9903                 if (!wdev->u.ibss.ssid_len)
9904                         return -ENOTCONN;
9905                 break;
9906         case NL80211_IFTYPE_MESH_POINT:
9907                 if (!wdev->u.mesh.id_len)
9908                         return -ENOTCONN;
9909                 break;
9910         default:
9911                 return -EOPNOTSUPP;
9912         }
9913
9914         memset(&params, 0, sizeof(params));
9915         params.beacon_csa.ftm_responder = -1;
9916
9917         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9918             !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
9919                 return -EINVAL;
9920
9921         /* only important for AP, IBSS and mesh create IEs internally */
9922         if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
9923                 return -EINVAL;
9924
9925         /* Even though the attribute is u32, the specification says
9926          * u8, so let's make sure we don't overflow.
9927          */
9928         cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
9929         if (cs_count > 255)
9930                 return -EINVAL;
9931
9932         params.count = cs_count;
9933
9934         if (!need_new_beacon)
9935                 goto skip_beacons;
9936
9937         err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_after);
9938         if (err)
9939                 goto free;
9940
9941         csa_attrs = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*csa_attrs),
9942                             GFP_KERNEL);
9943         if (!csa_attrs) {
9944                 err = -ENOMEM;
9945                 goto free;
9946         }
9947
9948         err = nla_parse_nested_deprecated(csa_attrs, NL80211_ATTR_MAX,
9949                                           info->attrs[NL80211_ATTR_CSA_IES],
9950                                           nl80211_policy, info->extack);
9951         if (err)
9952                 goto free;
9953
9954         err = nl80211_parse_beacon(rdev, csa_attrs, &params.beacon_csa);
9955         if (err)
9956                 goto free;
9957
9958         if (!csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
9959                 err = -EINVAL;
9960                 goto free;
9961         }
9962
9963         len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9964         if (!len || (len % sizeof(u16))) {
9965                 err = -EINVAL;
9966                 goto free;
9967         }
9968
9969         params.n_counter_offsets_beacon = len / sizeof(u16);
9970         if (rdev->wiphy.max_num_csa_counters &&
9971             (params.n_counter_offsets_beacon >
9972              rdev->wiphy.max_num_csa_counters)) {
9973                 err = -EINVAL;
9974                 goto free;
9975         }
9976
9977         params.counter_offsets_beacon =
9978                 nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
9979
9980         /* sanity checks - counters should fit and be the same */
9981         for (i = 0; i < params.n_counter_offsets_beacon; i++) {
9982                 u16 offset = params.counter_offsets_beacon[i];
9983
9984                 if (offset >= params.beacon_csa.tail_len) {
9985                         err = -EINVAL;
9986                         goto free;
9987                 }
9988
9989                 if (params.beacon_csa.tail[offset] != params.count) {
9990                         err = -EINVAL;
9991                         goto free;
9992                 }
9993         }
9994
9995         if (csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
9996                 len = nla_len(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
9997                 if (!len || (len % sizeof(u16))) {
9998                         err = -EINVAL;
9999                         goto free;
10000                 }
10001
10002                 params.n_counter_offsets_presp = len / sizeof(u16);
10003                 if (rdev->wiphy.max_num_csa_counters &&
10004                     (params.n_counter_offsets_presp >
10005                      rdev->wiphy.max_num_csa_counters)) {
10006                         err = -EINVAL;
10007                         goto free;
10008                 }
10009
10010                 params.counter_offsets_presp =
10011                         nla_data(csa_attrs[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
10012
10013                 /* sanity checks - counters should fit and be the same */
10014                 for (i = 0; i < params.n_counter_offsets_presp; i++) {
10015                         u16 offset = params.counter_offsets_presp[i];
10016
10017                         if (offset >= params.beacon_csa.probe_resp_len) {
10018                                 err = -EINVAL;
10019                                 goto free;
10020                         }
10021
10022                         if (params.beacon_csa.probe_resp[offset] !=
10023                             params.count) {
10024                                 err = -EINVAL;
10025                                 goto free;
10026                         }
10027                 }
10028         }
10029
10030 skip_beacons:
10031         err = nl80211_parse_chandef(rdev, info, &params.chandef);
10032         if (err)
10033                 goto free;
10034
10035         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
10036                                            wdev->iftype)) {
10037                 err = -EINVAL;
10038                 goto free;
10039         }
10040
10041         err = cfg80211_chandef_dfs_required(wdev->wiphy,
10042                                             &params.chandef,
10043                                             wdev->iftype);
10044         if (err < 0)
10045                 goto free;
10046
10047         if (err > 0) {
10048                 params.radar_required = true;
10049                 if (need_handle_dfs_flag &&
10050                     !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
10051                         err = -EINVAL;
10052                         goto free;
10053                 }
10054         }
10055
10056         if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
10057                 params.block_tx = true;
10058
10059         wdev_lock(wdev);
10060         err = rdev_channel_switch(rdev, dev, &params);
10061         wdev_unlock(wdev);
10062
10063 free:
10064         kfree(params.beacon_after.mbssid_ies);
10065         kfree(params.beacon_csa.mbssid_ies);
10066         kfree(csa_attrs);
10067         return err;
10068 }
10069
10070 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
10071                             u32 seq, int flags,
10072                             struct cfg80211_registered_device *rdev,
10073                             struct wireless_dev *wdev,
10074                             struct cfg80211_internal_bss *intbss)
10075 {
10076         struct cfg80211_bss *res = &intbss->pub;
10077         const struct cfg80211_bss_ies *ies;
10078         unsigned int link_id;
10079         void *hdr;
10080         struct nlattr *bss;
10081
10082         ASSERT_WDEV_LOCK(wdev);
10083
10084         hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
10085                              NL80211_CMD_NEW_SCAN_RESULTS);
10086         if (!hdr)
10087                 return -1;
10088
10089         genl_dump_check_consistent(cb, hdr);
10090
10091         if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
10092                 goto nla_put_failure;
10093         if (wdev->netdev &&
10094             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
10095                 goto nla_put_failure;
10096         if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
10097                               NL80211_ATTR_PAD))
10098                 goto nla_put_failure;
10099
10100         bss = nla_nest_start_noflag(msg, NL80211_ATTR_BSS);
10101         if (!bss)
10102                 goto nla_put_failure;
10103         if ((!is_zero_ether_addr(res->bssid) &&
10104              nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
10105                 goto nla_put_failure;
10106
10107         rcu_read_lock();
10108         /* indicate whether we have probe response data or not */
10109         if (rcu_access_pointer(res->proberesp_ies) &&
10110             nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
10111                 goto fail_unlock_rcu;
10112
10113         /* this pointer prefers to be pointed to probe response data
10114          * but is always valid
10115          */
10116         ies = rcu_dereference(res->ies);
10117         if (ies) {
10118                 if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
10119                                       NL80211_BSS_PAD))
10120                         goto fail_unlock_rcu;
10121                 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
10122                                         ies->len, ies->data))
10123                         goto fail_unlock_rcu;
10124         }
10125
10126         /* and this pointer is always (unless driver didn't know) beacon data */
10127         ies = rcu_dereference(res->beacon_ies);
10128         if (ies && ies->from_beacon) {
10129                 if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
10130                                       NL80211_BSS_PAD))
10131                         goto fail_unlock_rcu;
10132                 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
10133                                         ies->len, ies->data))
10134                         goto fail_unlock_rcu;
10135         }
10136         rcu_read_unlock();
10137
10138         if (res->beacon_interval &&
10139             nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
10140                 goto nla_put_failure;
10141         if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
10142             nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
10143             nla_put_u32(msg, NL80211_BSS_FREQUENCY_OFFSET,
10144                         res->channel->freq_offset) ||
10145             nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
10146             nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
10147                         jiffies_to_msecs(jiffies - intbss->ts)))
10148                 goto nla_put_failure;
10149
10150         if (intbss->parent_tsf &&
10151             (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
10152                                intbss->parent_tsf, NL80211_BSS_PAD) ||
10153              nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
10154                      intbss->parent_bssid)))
10155                 goto nla_put_failure;
10156
10157         if (intbss->ts_boottime &&
10158             nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
10159                               intbss->ts_boottime, NL80211_BSS_PAD))
10160                 goto nla_put_failure;
10161
10162         if (!nl80211_put_signal(msg, intbss->pub.chains,
10163                                 intbss->pub.chain_signal,
10164                                 NL80211_BSS_CHAIN_SIGNAL))
10165                 goto nla_put_failure;
10166
10167         switch (rdev->wiphy.signal_type) {
10168         case CFG80211_SIGNAL_TYPE_MBM:
10169                 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
10170                         goto nla_put_failure;
10171                 break;
10172         case CFG80211_SIGNAL_TYPE_UNSPEC:
10173                 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
10174                         goto nla_put_failure;
10175                 break;
10176         default:
10177                 break;
10178         }
10179
10180         switch (wdev->iftype) {
10181         case NL80211_IFTYPE_P2P_CLIENT:
10182         case NL80211_IFTYPE_STATION:
10183                 for_each_valid_link(wdev, link_id) {
10184                         if (intbss == wdev->links[link_id].client.current_bss &&
10185                             (nla_put_u32(msg, NL80211_BSS_STATUS,
10186                                          NL80211_BSS_STATUS_ASSOCIATED) ||
10187                              (wdev->valid_links &&
10188                               (nla_put_u8(msg, NL80211_BSS_MLO_LINK_ID,
10189                                           link_id) ||
10190                                nla_put(msg, NL80211_BSS_MLD_ADDR, ETH_ALEN,
10191                                        wdev->u.client.connected_addr)))))
10192                                 goto nla_put_failure;
10193                 }
10194                 break;
10195         case NL80211_IFTYPE_ADHOC:
10196                 if (intbss == wdev->u.ibss.current_bss &&
10197                     nla_put_u32(msg, NL80211_BSS_STATUS,
10198                                 NL80211_BSS_STATUS_IBSS_JOINED))
10199                         goto nla_put_failure;
10200                 break;
10201         default:
10202                 break;
10203         }
10204
10205         nla_nest_end(msg, bss);
10206
10207         genlmsg_end(msg, hdr);
10208         return 0;
10209
10210  fail_unlock_rcu:
10211         rcu_read_unlock();
10212  nla_put_failure:
10213         genlmsg_cancel(msg, hdr);
10214         return -EMSGSIZE;
10215 }
10216
10217 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
10218 {
10219         struct cfg80211_registered_device *rdev;
10220         struct cfg80211_internal_bss *scan;
10221         struct wireless_dev *wdev;
10222         int start = cb->args[2], idx = 0;
10223         int err;
10224
10225         err = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, NULL);
10226         if (err)
10227                 return err;
10228         /* nl80211_prepare_wdev_dump acquired it in the successful case */
10229         __acquire(&rdev->wiphy.mtx);
10230
10231         wdev_lock(wdev);
10232         spin_lock_bh(&rdev->bss_lock);
10233
10234         /*
10235          * dump_scan will be called multiple times to break up the scan results
10236          * into multiple messages.  It is unlikely that any more bss-es will be
10237          * expired after the first call, so only call only call this on the
10238          * first dump_scan invocation.
10239          */
10240         if (start == 0)
10241                 cfg80211_bss_expire(rdev);
10242
10243         cb->seq = rdev->bss_generation;
10244
10245         list_for_each_entry(scan, &rdev->bss_list, list) {
10246                 if (++idx <= start)
10247                         continue;
10248                 if (nl80211_send_bss(skb, cb,
10249                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
10250                                 rdev, wdev, scan) < 0) {
10251                         idx--;
10252                         break;
10253                 }
10254         }
10255
10256         spin_unlock_bh(&rdev->bss_lock);
10257         wdev_unlock(wdev);
10258
10259         cb->args[2] = idx;
10260         wiphy_unlock(&rdev->wiphy);
10261
10262         return skb->len;
10263 }
10264
10265 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
10266                                int flags, struct net_device *dev,
10267                                bool allow_radio_stats,
10268                                struct survey_info *survey)
10269 {
10270         void *hdr;
10271         struct nlattr *infoattr;
10272
10273         /* skip radio stats if userspace didn't request them */
10274         if (!survey->channel && !allow_radio_stats)
10275                 return 0;
10276
10277         hdr = nl80211hdr_put(msg, portid, seq, flags,
10278                              NL80211_CMD_NEW_SURVEY_RESULTS);
10279         if (!hdr)
10280                 return -ENOMEM;
10281
10282         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
10283                 goto nla_put_failure;
10284
10285         infoattr = nla_nest_start_noflag(msg, NL80211_ATTR_SURVEY_INFO);
10286         if (!infoattr)
10287                 goto nla_put_failure;
10288
10289         if (survey->channel &&
10290             nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
10291                         survey->channel->center_freq))
10292                 goto nla_put_failure;
10293
10294         if (survey->channel && survey->channel->freq_offset &&
10295             nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY_OFFSET,
10296                         survey->channel->freq_offset))
10297                 goto nla_put_failure;
10298
10299         if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
10300             nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
10301                 goto nla_put_failure;
10302         if ((survey->filled & SURVEY_INFO_IN_USE) &&
10303             nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
10304                 goto nla_put_failure;
10305         if ((survey->filled & SURVEY_INFO_TIME) &&
10306             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
10307                         survey->time, NL80211_SURVEY_INFO_PAD))
10308                 goto nla_put_failure;
10309         if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
10310             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
10311                               survey->time_busy, NL80211_SURVEY_INFO_PAD))
10312                 goto nla_put_failure;
10313         if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
10314             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
10315                               survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
10316                 goto nla_put_failure;
10317         if ((survey->filled & SURVEY_INFO_TIME_RX) &&
10318             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
10319                               survey->time_rx, NL80211_SURVEY_INFO_PAD))
10320                 goto nla_put_failure;
10321         if ((survey->filled & SURVEY_INFO_TIME_TX) &&
10322             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
10323                               survey->time_tx, NL80211_SURVEY_INFO_PAD))
10324                 goto nla_put_failure;
10325         if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
10326             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
10327                               survey->time_scan, NL80211_SURVEY_INFO_PAD))
10328                 goto nla_put_failure;
10329         if ((survey->filled & SURVEY_INFO_TIME_BSS_RX) &&
10330             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BSS_RX,
10331                               survey->time_bss_rx, NL80211_SURVEY_INFO_PAD))
10332                 goto nla_put_failure;
10333
10334         nla_nest_end(msg, infoattr);
10335
10336         genlmsg_end(msg, hdr);
10337         return 0;
10338
10339  nla_put_failure:
10340         genlmsg_cancel(msg, hdr);
10341         return -EMSGSIZE;
10342 }
10343
10344 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
10345 {
10346         struct nlattr **attrbuf;
10347         struct survey_info survey;
10348         struct cfg80211_registered_device *rdev;
10349         struct wireless_dev *wdev;
10350         int survey_idx = cb->args[2];
10351         int res;
10352         bool radio_stats;
10353
10354         attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
10355         if (!attrbuf)
10356                 return -ENOMEM;
10357
10358         res = nl80211_prepare_wdev_dump(cb, &rdev, &wdev, attrbuf);
10359         if (res) {
10360                 kfree(attrbuf);
10361                 return res;
10362         }
10363         /* nl80211_prepare_wdev_dump acquired it in the successful case */
10364         __acquire(&rdev->wiphy.mtx);
10365
10366         /* prepare_wdev_dump parsed the attributes */
10367         radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
10368
10369         if (!wdev->netdev) {
10370                 res = -EINVAL;
10371                 goto out_err;
10372         }
10373
10374         if (!rdev->ops->dump_survey) {
10375                 res = -EOPNOTSUPP;
10376                 goto out_err;
10377         }
10378
10379         while (1) {
10380                 wdev_lock(wdev);
10381                 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
10382                 wdev_unlock(wdev);
10383                 if (res == -ENOENT)
10384                         break;
10385                 if (res)
10386                         goto out_err;
10387
10388                 /* don't send disabled channels, but do send non-channel data */
10389                 if (survey.channel &&
10390                     survey.channel->flags & IEEE80211_CHAN_DISABLED) {
10391                         survey_idx++;
10392                         continue;
10393                 }
10394
10395                 if (nl80211_send_survey(skb,
10396                                 NETLINK_CB(cb->skb).portid,
10397                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
10398                                 wdev->netdev, radio_stats, &survey) < 0)
10399                         goto out;
10400                 survey_idx++;
10401         }
10402
10403  out:
10404         cb->args[2] = survey_idx;
10405         res = skb->len;
10406  out_err:
10407         kfree(attrbuf);
10408         wiphy_unlock(&rdev->wiphy);
10409         return res;
10410 }
10411
10412 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
10413 {
10414         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
10415                                   NL80211_WPA_VERSION_2 |
10416                                   NL80211_WPA_VERSION_3));
10417 }
10418
10419 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
10420 {
10421         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10422         struct net_device *dev = info->user_ptr[1];
10423         struct ieee80211_channel *chan;
10424         const u8 *bssid, *ssid;
10425         int err, ssid_len;
10426         enum nl80211_auth_type auth_type;
10427         struct key_parse key;
10428         bool local_state_change;
10429         struct cfg80211_auth_request req = {};
10430         u32 freq;
10431
10432         if (!info->attrs[NL80211_ATTR_MAC])
10433                 return -EINVAL;
10434
10435         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
10436                 return -EINVAL;
10437
10438         if (!info->attrs[NL80211_ATTR_SSID])
10439                 return -EINVAL;
10440
10441         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
10442                 return -EINVAL;
10443
10444         err = nl80211_parse_key(info, &key);
10445         if (err)
10446                 return err;
10447
10448         if (key.idx >= 0) {
10449                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
10450                         return -EINVAL;
10451                 if (!key.p.key || !key.p.key_len)
10452                         return -EINVAL;
10453                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
10454                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
10455                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
10456                      key.p.key_len != WLAN_KEY_LEN_WEP104))
10457                         return -EINVAL;
10458                 if (key.idx > 3)
10459                         return -EINVAL;
10460         } else {
10461                 key.p.key_len = 0;
10462                 key.p.key = NULL;
10463         }
10464
10465         if (key.idx >= 0) {
10466                 int i;
10467                 bool ok = false;
10468
10469                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
10470                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
10471                                 ok = true;
10472                                 break;
10473                         }
10474                 }
10475                 if (!ok)
10476                         return -EINVAL;
10477         }
10478
10479         if (!rdev->ops->auth)
10480                 return -EOPNOTSUPP;
10481
10482         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10483             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10484                 return -EOPNOTSUPP;
10485
10486         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
10487         freq = MHZ_TO_KHZ(nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
10488         if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10489                 freq +=
10490                     nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10491
10492         chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10493         if (!chan)
10494                 return -EINVAL;
10495
10496         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10497         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10498
10499         if (info->attrs[NL80211_ATTR_IE]) {
10500                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10501                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10502         }
10503
10504         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
10505         if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
10506                 return -EINVAL;
10507
10508         if ((auth_type == NL80211_AUTHTYPE_SAE ||
10509              auth_type == NL80211_AUTHTYPE_FILS_SK ||
10510              auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
10511              auth_type == NL80211_AUTHTYPE_FILS_PK) &&
10512             !info->attrs[NL80211_ATTR_AUTH_DATA])
10513                 return -EINVAL;
10514
10515         if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
10516                 if (auth_type != NL80211_AUTHTYPE_SAE &&
10517                     auth_type != NL80211_AUTHTYPE_FILS_SK &&
10518                     auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
10519                     auth_type != NL80211_AUTHTYPE_FILS_PK)
10520                         return -EINVAL;
10521                 req.auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
10522                 req.auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
10523         }
10524
10525         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
10526
10527         /*
10528          * Since we no longer track auth state, ignore
10529          * requests to only change local state.
10530          */
10531         if (local_state_change)
10532                 return 0;
10533
10534         req.auth_type = auth_type;
10535         req.key = key.p.key;
10536         req.key_len = key.p.key_len;
10537         req.key_idx = key.idx;
10538         req.link_id = nl80211_link_id_or_invalid(info->attrs);
10539         if (req.link_id >= 0) {
10540                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10541                         return -EINVAL;
10542                 if (!info->attrs[NL80211_ATTR_MLD_ADDR])
10543                         return -EINVAL;
10544                 req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10545                 if (!is_valid_ether_addr(req.ap_mld_addr))
10546                         return -EINVAL;
10547         }
10548
10549         req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
10550                                    IEEE80211_BSS_TYPE_ESS,
10551                                    IEEE80211_PRIVACY_ANY);
10552         if (!req.bss)
10553                 return -ENOENT;
10554
10555         wdev_lock(dev->ieee80211_ptr);
10556         err = cfg80211_mlme_auth(rdev, dev, &req);
10557         wdev_unlock(dev->ieee80211_ptr);
10558
10559         cfg80211_put_bss(&rdev->wiphy, req.bss);
10560
10561         return err;
10562 }
10563
10564 static int validate_pae_over_nl80211(struct cfg80211_registered_device *rdev,
10565                                      struct genl_info *info)
10566 {
10567         if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10568                 GENL_SET_ERR_MSG(info, "SOCKET_OWNER not set");
10569                 return -EINVAL;
10570         }
10571
10572         if (!rdev->ops->tx_control_port ||
10573             !wiphy_ext_feature_isset(&rdev->wiphy,
10574                                      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
10575                 return -EOPNOTSUPP;
10576
10577         return 0;
10578 }
10579
10580 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
10581                                    struct genl_info *info,
10582                                    struct cfg80211_crypto_settings *settings,
10583                                    int cipher_limit)
10584 {
10585         memset(settings, 0, sizeof(*settings));
10586
10587         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
10588
10589         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
10590                 u16 proto;
10591
10592                 proto = nla_get_u16(
10593                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
10594                 settings->control_port_ethertype = cpu_to_be16(proto);
10595                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
10596                     proto != ETH_P_PAE)
10597                         return -EINVAL;
10598                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
10599                         settings->control_port_no_encrypt = true;
10600         } else
10601                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
10602
10603         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
10604                 int r = validate_pae_over_nl80211(rdev, info);
10605
10606                 if (r < 0)
10607                         return r;
10608
10609                 settings->control_port_over_nl80211 = true;
10610
10611                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_PREAUTH])
10612                         settings->control_port_no_preauth = true;
10613         }
10614
10615         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
10616                 void *data;
10617                 int len, i;
10618
10619                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10620                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
10621                 settings->n_ciphers_pairwise = len / sizeof(u32);
10622
10623                 if (len % sizeof(u32))
10624                         return -EINVAL;
10625
10626                 if (settings->n_ciphers_pairwise > cipher_limit)
10627                         return -EINVAL;
10628
10629                 memcpy(settings->ciphers_pairwise, data, len);
10630
10631                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
10632                         if (!cfg80211_supported_cipher_suite(
10633                                         &rdev->wiphy,
10634                                         settings->ciphers_pairwise[i]))
10635                                 return -EINVAL;
10636         }
10637
10638         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
10639                 settings->cipher_group =
10640                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
10641                 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
10642                                                      settings->cipher_group))
10643                         return -EINVAL;
10644         }
10645
10646         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
10647                 settings->wpa_versions =
10648                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
10649                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
10650                         return -EINVAL;
10651         }
10652
10653         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
10654                 void *data;
10655                 int len;
10656
10657                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
10658                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
10659                 settings->n_akm_suites = len / sizeof(u32);
10660
10661                 if (len % sizeof(u32))
10662                         return -EINVAL;
10663
10664                 if (settings->n_akm_suites > rdev->wiphy.max_num_akm_suites)
10665                         return -EINVAL;
10666
10667                 memcpy(settings->akm_suites, data, len);
10668         }
10669
10670         if (info->attrs[NL80211_ATTR_PMK]) {
10671                 if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
10672                         return -EINVAL;
10673                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
10674                                              NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK) &&
10675                     !wiphy_ext_feature_isset(&rdev->wiphy,
10676                                              NL80211_EXT_FEATURE_4WAY_HANDSHAKE_AP_PSK))
10677                         return -EINVAL;
10678                 settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
10679         }
10680
10681         if (info->attrs[NL80211_ATTR_SAE_PASSWORD]) {
10682                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
10683                                              NL80211_EXT_FEATURE_SAE_OFFLOAD) &&
10684                     !wiphy_ext_feature_isset(&rdev->wiphy,
10685                                              NL80211_EXT_FEATURE_SAE_OFFLOAD_AP))
10686                         return -EINVAL;
10687                 settings->sae_pwd =
10688                         nla_data(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10689                 settings->sae_pwd_len =
10690                         nla_len(info->attrs[NL80211_ATTR_SAE_PASSWORD]);
10691         }
10692
10693         if (info->attrs[NL80211_ATTR_SAE_PWE])
10694                 settings->sae_pwe =
10695                         nla_get_u8(info->attrs[NL80211_ATTR_SAE_PWE]);
10696         else
10697                 settings->sae_pwe = NL80211_SAE_PWE_UNSPECIFIED;
10698
10699         return 0;
10700 }
10701
10702 static struct cfg80211_bss *nl80211_assoc_bss(struct cfg80211_registered_device *rdev,
10703                                               const u8 *ssid, int ssid_len,
10704                                               struct nlattr **attrs)
10705 {
10706         struct ieee80211_channel *chan;
10707         struct cfg80211_bss *bss;
10708         const u8 *bssid;
10709         u32 freq;
10710
10711         if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_WIPHY_FREQ])
10712                 return ERR_PTR(-EINVAL);
10713
10714         bssid = nla_data(attrs[NL80211_ATTR_MAC]);
10715
10716         freq = MHZ_TO_KHZ(nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
10717         if (attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
10718                 freq += nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
10719
10720         chan = nl80211_get_valid_chan(&rdev->wiphy, freq);
10721         if (!chan)
10722                 return ERR_PTR(-EINVAL);
10723
10724         bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid,
10725                                ssid, ssid_len,
10726                                IEEE80211_BSS_TYPE_ESS,
10727                                IEEE80211_PRIVACY_ANY);
10728         if (!bss)
10729                 return ERR_PTR(-ENOENT);
10730
10731         return bss;
10732 }
10733
10734 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
10735 {
10736         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10737         struct net_device *dev = info->user_ptr[1];
10738         struct cfg80211_assoc_request req = {};
10739         struct nlattr **attrs = NULL;
10740         const u8 *ap_addr, *ssid;
10741         unsigned int link_id;
10742         int err, ssid_len;
10743
10744         if (dev->ieee80211_ptr->conn_owner_nlportid &&
10745             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10746                 return -EPERM;
10747
10748         if (!info->attrs[NL80211_ATTR_SSID])
10749                 return -EINVAL;
10750
10751         if (!rdev->ops->assoc)
10752                 return -EOPNOTSUPP;
10753
10754         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
10755             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
10756                 return -EOPNOTSUPP;
10757
10758         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
10759         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
10760
10761         if (info->attrs[NL80211_ATTR_IE]) {
10762                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
10763                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
10764
10765                 if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10766                                            req.ie, req.ie_len)) {
10767                         GENL_SET_ERR_MSG(info,
10768                                          "non-inheritance makes no sense");
10769                         return -EINVAL;
10770                 }
10771         }
10772
10773         if (info->attrs[NL80211_ATTR_USE_MFP]) {
10774                 enum nl80211_mfp mfp =
10775                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
10776                 if (mfp == NL80211_MFP_REQUIRED)
10777                         req.use_mfp = true;
10778                 else if (mfp != NL80211_MFP_NO)
10779                         return -EINVAL;
10780         }
10781
10782         if (info->attrs[NL80211_ATTR_PREV_BSSID])
10783                 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
10784
10785         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
10786                 req.flags |= ASSOC_REQ_DISABLE_HT;
10787
10788         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10789                 memcpy(&req.ht_capa_mask,
10790                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
10791                        sizeof(req.ht_capa_mask));
10792
10793         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
10794                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
10795                         return -EINVAL;
10796                 memcpy(&req.ht_capa,
10797                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
10798                        sizeof(req.ht_capa));
10799         }
10800
10801         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
10802                 req.flags |= ASSOC_REQ_DISABLE_VHT;
10803
10804         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
10805                 req.flags |= ASSOC_REQ_DISABLE_HE;
10806
10807         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
10808                 req.flags |= ASSOC_REQ_DISABLE_EHT;
10809
10810         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10811                 memcpy(&req.vht_capa_mask,
10812                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
10813                        sizeof(req.vht_capa_mask));
10814
10815         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
10816                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
10817                         return -EINVAL;
10818                 memcpy(&req.vht_capa,
10819                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
10820                        sizeof(req.vht_capa));
10821         }
10822
10823         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
10824                 if (!((rdev->wiphy.features &
10825                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
10826                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
10827                     !wiphy_ext_feature_isset(&rdev->wiphy,
10828                                              NL80211_EXT_FEATURE_RRM))
10829                         return -EINVAL;
10830                 req.flags |= ASSOC_REQ_USE_RRM;
10831         }
10832
10833         if (info->attrs[NL80211_ATTR_FILS_KEK]) {
10834                 req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
10835                 req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
10836                 if (!info->attrs[NL80211_ATTR_FILS_NONCES])
10837                         return -EINVAL;
10838                 req.fils_nonces =
10839                         nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
10840         }
10841
10842         if (info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]) {
10843                 if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY])
10844                         return -EINVAL;
10845                 memcpy(&req.s1g_capa_mask,
10846                        nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK]),
10847                        sizeof(req.s1g_capa_mask));
10848         }
10849
10850         if (info->attrs[NL80211_ATTR_S1G_CAPABILITY]) {
10851                 if (!info->attrs[NL80211_ATTR_S1G_CAPABILITY_MASK])
10852                         return -EINVAL;
10853                 memcpy(&req.s1g_capa,
10854                        nla_data(info->attrs[NL80211_ATTR_S1G_CAPABILITY]),
10855                        sizeof(req.s1g_capa));
10856         }
10857
10858         req.link_id = nl80211_link_id_or_invalid(info->attrs);
10859
10860         if (info->attrs[NL80211_ATTR_MLO_LINKS]) {
10861                 unsigned int attrsize = NUM_NL80211_ATTR * sizeof(*attrs);
10862                 struct nlattr *link;
10863                 int rem = 0;
10864
10865                 if (req.link_id < 0)
10866                         return -EINVAL;
10867
10868                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_MLO))
10869                         return -EINVAL;
10870
10871                 if (info->attrs[NL80211_ATTR_MAC] ||
10872                     info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
10873                     !info->attrs[NL80211_ATTR_MLD_ADDR])
10874                         return -EINVAL;
10875
10876                 req.ap_mld_addr = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
10877                 ap_addr = req.ap_mld_addr;
10878
10879                 attrs = kzalloc(attrsize, GFP_KERNEL);
10880                 if (!attrs)
10881                         return -ENOMEM;
10882
10883                 nla_for_each_nested(link,
10884                                     info->attrs[NL80211_ATTR_MLO_LINKS],
10885                                     rem) {
10886                         memset(attrs, 0, attrsize);
10887
10888                         nla_parse_nested(attrs, NL80211_ATTR_MAX,
10889                                          link, NULL, NULL);
10890
10891                         if (!attrs[NL80211_ATTR_MLO_LINK_ID]) {
10892                                 err = -EINVAL;
10893                                 goto free;
10894                         }
10895
10896                         link_id = nla_get_u8(attrs[NL80211_ATTR_MLO_LINK_ID]);
10897                         /* cannot use the same link ID again */
10898                         if (req.links[link_id].bss) {
10899                                 err = -EINVAL;
10900                                 goto free;
10901                         }
10902                         req.links[link_id].bss =
10903                                 nl80211_assoc_bss(rdev, ssid, ssid_len, attrs);
10904                         if (IS_ERR(req.links[link_id].bss)) {
10905                                 err = PTR_ERR(req.links[link_id].bss);
10906                                 req.links[link_id].bss = NULL;
10907                                 goto free;
10908                         }
10909
10910                         if (attrs[NL80211_ATTR_IE]) {
10911                                 req.links[link_id].elems =
10912                                         nla_data(attrs[NL80211_ATTR_IE]);
10913                                 req.links[link_id].elems_len =
10914                                         nla_len(attrs[NL80211_ATTR_IE]);
10915
10916                                 if (cfg80211_find_elem(WLAN_EID_FRAGMENT,
10917                                                        req.links[link_id].elems,
10918                                                        req.links[link_id].elems_len)) {
10919                                         GENL_SET_ERR_MSG(info,
10920                                                          "cannot deal with fragmentation");
10921                                         err = -EINVAL;
10922                                         goto free;
10923                                 }
10924
10925                                 if (cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
10926                                                            req.links[link_id].elems,
10927                                                            req.links[link_id].elems_len)) {
10928                                         GENL_SET_ERR_MSG(info,
10929                                                          "cannot deal with non-inheritance");
10930                                         err = -EINVAL;
10931                                         goto free;
10932                                 }
10933                         }
10934                 }
10935
10936                 if (!req.links[req.link_id].bss) {
10937                         err = -EINVAL;
10938                         goto free;
10939                 }
10940
10941                 if (req.links[req.link_id].elems_len) {
10942                         GENL_SET_ERR_MSG(info,
10943                                          "cannot have per-link elems on assoc link");
10944                         err = -EINVAL;
10945                         goto free;
10946                 }
10947
10948                 kfree(attrs);
10949                 attrs = NULL;
10950         } else {
10951                 if (req.link_id >= 0)
10952                         return -EINVAL;
10953
10954                 req.bss = nl80211_assoc_bss(rdev, ssid, ssid_len, info->attrs);
10955                 if (IS_ERR(req.bss))
10956                         return PTR_ERR(req.bss);
10957                 ap_addr = req.bss->bssid;
10958         }
10959
10960         err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
10961         if (!err) {
10962                 wdev_lock(dev->ieee80211_ptr);
10963
10964                 err = cfg80211_mlme_assoc(rdev, dev, &req);
10965
10966                 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
10967                         dev->ieee80211_ptr->conn_owner_nlportid =
10968                                 info->snd_portid;
10969                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
10970                                ap_addr, ETH_ALEN);
10971                 }
10972
10973                 wdev_unlock(dev->ieee80211_ptr);
10974         }
10975
10976 free:
10977         for (link_id = 0; link_id < ARRAY_SIZE(req.links); link_id++)
10978                 cfg80211_put_bss(&rdev->wiphy, req.links[link_id].bss);
10979         cfg80211_put_bss(&rdev->wiphy, req.bss);
10980         kfree(attrs);
10981
10982         return err;
10983 }
10984
10985 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
10986 {
10987         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10988         struct net_device *dev = info->user_ptr[1];
10989         const u8 *ie = NULL, *bssid;
10990         int ie_len = 0, err;
10991         u16 reason_code;
10992         bool local_state_change;
10993
10994         if (dev->ieee80211_ptr->conn_owner_nlportid &&
10995             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
10996                 return -EPERM;
10997
10998         if (!info->attrs[NL80211_ATTR_MAC])
10999                 return -EINVAL;
11000
11001         if (!info->attrs[NL80211_ATTR_REASON_CODE])
11002                 return -EINVAL;
11003
11004         if (!rdev->ops->deauth)
11005                 return -EOPNOTSUPP;
11006
11007         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11008             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11009                 return -EOPNOTSUPP;
11010
11011         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11012
11013         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11014         if (reason_code == 0) {
11015                 /* Reason Code 0 is reserved */
11016                 return -EINVAL;
11017         }
11018
11019         if (info->attrs[NL80211_ATTR_IE]) {
11020                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11021                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11022         }
11023
11024         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11025
11026         wdev_lock(dev->ieee80211_ptr);
11027         err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
11028                                    local_state_change);
11029         wdev_unlock(dev->ieee80211_ptr);
11030         return err;
11031 }
11032
11033 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
11034 {
11035         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11036         struct net_device *dev = info->user_ptr[1];
11037         const u8 *ie = NULL, *bssid;
11038         int ie_len = 0, err;
11039         u16 reason_code;
11040         bool local_state_change;
11041
11042         if (dev->ieee80211_ptr->conn_owner_nlportid &&
11043             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11044                 return -EPERM;
11045
11046         if (!info->attrs[NL80211_ATTR_MAC])
11047                 return -EINVAL;
11048
11049         if (!info->attrs[NL80211_ATTR_REASON_CODE])
11050                 return -EINVAL;
11051
11052         if (!rdev->ops->disassoc)
11053                 return -EOPNOTSUPP;
11054
11055         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11056             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11057                 return -EOPNOTSUPP;
11058
11059         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11060
11061         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11062         if (reason_code == 0) {
11063                 /* Reason Code 0 is reserved */
11064                 return -EINVAL;
11065         }
11066
11067         if (info->attrs[NL80211_ATTR_IE]) {
11068                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11069                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11070         }
11071
11072         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
11073
11074         wdev_lock(dev->ieee80211_ptr);
11075         err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
11076                                      local_state_change);
11077         wdev_unlock(dev->ieee80211_ptr);
11078         return err;
11079 }
11080
11081 static bool
11082 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
11083                          int mcast_rate[NUM_NL80211_BANDS],
11084                          int rateval)
11085 {
11086         struct wiphy *wiphy = &rdev->wiphy;
11087         bool found = false;
11088         int band, i;
11089
11090         for (band = 0; band < NUM_NL80211_BANDS; band++) {
11091                 struct ieee80211_supported_band *sband;
11092
11093                 sband = wiphy->bands[band];
11094                 if (!sband)
11095                         continue;
11096
11097                 for (i = 0; i < sband->n_bitrates; i++) {
11098                         if (sband->bitrates[i].bitrate == rateval) {
11099                                 mcast_rate[band] = i + 1;
11100                                 found = true;
11101                                 break;
11102                         }
11103                 }
11104         }
11105
11106         return found;
11107 }
11108
11109 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
11110 {
11111         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11112         struct net_device *dev = info->user_ptr[1];
11113         struct cfg80211_ibss_params ibss;
11114         struct wiphy *wiphy;
11115         struct cfg80211_cached_keys *connkeys = NULL;
11116         int err;
11117
11118         memset(&ibss, 0, sizeof(ibss));
11119
11120         if (!info->attrs[NL80211_ATTR_SSID] ||
11121             !nla_len(info->attrs[NL80211_ATTR_SSID]))
11122                 return -EINVAL;
11123
11124         ibss.beacon_interval = 100;
11125
11126         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
11127                 ibss.beacon_interval =
11128                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
11129
11130         err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
11131                                            ibss.beacon_interval);
11132         if (err)
11133                 return err;
11134
11135         if (!rdev->ops->join_ibss)
11136                 return -EOPNOTSUPP;
11137
11138         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11139                 return -EOPNOTSUPP;
11140
11141         wiphy = &rdev->wiphy;
11142
11143         if (info->attrs[NL80211_ATTR_MAC]) {
11144                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11145
11146                 if (!is_valid_ether_addr(ibss.bssid))
11147                         return -EINVAL;
11148         }
11149         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11150         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11151
11152         if (info->attrs[NL80211_ATTR_IE]) {
11153                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11154                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11155         }
11156
11157         err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
11158         if (err)
11159                 return err;
11160
11161         if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
11162                                      NL80211_IFTYPE_ADHOC))
11163                 return -EINVAL;
11164
11165         switch (ibss.chandef.width) {
11166         case NL80211_CHAN_WIDTH_5:
11167         case NL80211_CHAN_WIDTH_10:
11168         case NL80211_CHAN_WIDTH_20_NOHT:
11169                 break;
11170         case NL80211_CHAN_WIDTH_20:
11171         case NL80211_CHAN_WIDTH_40:
11172                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11173                         return -EINVAL;
11174                 break;
11175         case NL80211_CHAN_WIDTH_80:
11176         case NL80211_CHAN_WIDTH_80P80:
11177         case NL80211_CHAN_WIDTH_160:
11178                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
11179                         return -EINVAL;
11180                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
11181                                              NL80211_EXT_FEATURE_VHT_IBSS))
11182                         return -EINVAL;
11183                 break;
11184         case NL80211_CHAN_WIDTH_320:
11185                 return -EINVAL;
11186         default:
11187                 return -EINVAL;
11188         }
11189
11190         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
11191         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
11192
11193         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
11194                 u8 *rates =
11195                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11196                 int n_rates =
11197                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
11198                 struct ieee80211_supported_band *sband =
11199                         wiphy->bands[ibss.chandef.chan->band];
11200
11201                 err = ieee80211_get_ratemask(sband, rates, n_rates,
11202                                              &ibss.basic_rates);
11203                 if (err)
11204                         return err;
11205         }
11206
11207         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11208                 memcpy(&ibss.ht_capa_mask,
11209                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11210                        sizeof(ibss.ht_capa_mask));
11211
11212         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11213                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11214                         return -EINVAL;
11215                 memcpy(&ibss.ht_capa,
11216                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11217                        sizeof(ibss.ht_capa));
11218         }
11219
11220         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
11221             !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
11222                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
11223                 return -EINVAL;
11224
11225         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11226                 bool no_ht = false;
11227
11228                 connkeys = nl80211_parse_connkeys(rdev, info, &no_ht);
11229                 if (IS_ERR(connkeys))
11230                         return PTR_ERR(connkeys);
11231
11232                 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
11233                     no_ht) {
11234                         kfree_sensitive(connkeys);
11235                         return -EINVAL;
11236                 }
11237         }
11238
11239         ibss.control_port =
11240                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
11241
11242         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
11243                 int r = validate_pae_over_nl80211(rdev, info);
11244
11245                 if (r < 0) {
11246                         kfree_sensitive(connkeys);
11247                         return r;
11248                 }
11249
11250                 ibss.control_port_over_nl80211 = true;
11251         }
11252
11253         ibss.userspace_handles_dfs =
11254                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
11255
11256         wdev_lock(dev->ieee80211_ptr);
11257         err = __cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
11258         if (err)
11259                 kfree_sensitive(connkeys);
11260         else if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
11261                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11262         wdev_unlock(dev->ieee80211_ptr);
11263
11264         return err;
11265 }
11266
11267 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
11268 {
11269         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11270         struct net_device *dev = info->user_ptr[1];
11271
11272         if (!rdev->ops->leave_ibss)
11273                 return -EOPNOTSUPP;
11274
11275         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
11276                 return -EOPNOTSUPP;
11277
11278         return cfg80211_leave_ibss(rdev, dev, false);
11279 }
11280
11281 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
11282 {
11283         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11284         struct net_device *dev = info->user_ptr[1];
11285         int mcast_rate[NUM_NL80211_BANDS];
11286         u32 nla_rate;
11287
11288         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
11289             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
11290             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
11291                 return -EOPNOTSUPP;
11292
11293         if (!rdev->ops->set_mcast_rate)
11294                 return -EOPNOTSUPP;
11295
11296         memset(mcast_rate, 0, sizeof(mcast_rate));
11297
11298         if (!info->attrs[NL80211_ATTR_MCAST_RATE])
11299                 return -EINVAL;
11300
11301         nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
11302         if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
11303                 return -EINVAL;
11304
11305         return rdev_set_mcast_rate(rdev, dev, mcast_rate);
11306 }
11307
11308 static struct sk_buff *
11309 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
11310                             struct wireless_dev *wdev, int approxlen,
11311                             u32 portid, u32 seq, enum nl80211_commands cmd,
11312                             enum nl80211_attrs attr,
11313                             const struct nl80211_vendor_cmd_info *info,
11314                             gfp_t gfp)
11315 {
11316         struct sk_buff *skb;
11317         void *hdr;
11318         struct nlattr *data;
11319
11320         skb = nlmsg_new(approxlen + 100, gfp);
11321         if (!skb)
11322                 return NULL;
11323
11324         hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
11325         if (!hdr) {
11326                 kfree_skb(skb);
11327                 return NULL;
11328         }
11329
11330         if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11331                 goto nla_put_failure;
11332
11333         if (info) {
11334                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
11335                                 info->vendor_id))
11336                         goto nla_put_failure;
11337                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
11338                                 info->subcmd))
11339                         goto nla_put_failure;
11340         }
11341
11342         if (wdev) {
11343                 if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
11344                                       wdev_id(wdev), NL80211_ATTR_PAD))
11345                         goto nla_put_failure;
11346                 if (wdev->netdev &&
11347                     nla_put_u32(skb, NL80211_ATTR_IFINDEX,
11348                                 wdev->netdev->ifindex))
11349                         goto nla_put_failure;
11350         }
11351
11352         data = nla_nest_start_noflag(skb, attr);
11353         if (!data)
11354                 goto nla_put_failure;
11355
11356         ((void **)skb->cb)[0] = rdev;
11357         ((void **)skb->cb)[1] = hdr;
11358         ((void **)skb->cb)[2] = data;
11359
11360         return skb;
11361
11362  nla_put_failure:
11363         kfree_skb(skb);
11364         return NULL;
11365 }
11366
11367 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
11368                                            struct wireless_dev *wdev,
11369                                            enum nl80211_commands cmd,
11370                                            enum nl80211_attrs attr,
11371                                            unsigned int portid,
11372                                            int vendor_event_idx,
11373                                            int approxlen, gfp_t gfp)
11374 {
11375         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11376         const struct nl80211_vendor_cmd_info *info;
11377
11378         switch (cmd) {
11379         case NL80211_CMD_TESTMODE:
11380                 if (WARN_ON(vendor_event_idx != -1))
11381                         return NULL;
11382                 info = NULL;
11383                 break;
11384         case NL80211_CMD_VENDOR:
11385                 if (WARN_ON(vendor_event_idx < 0 ||
11386                             vendor_event_idx >= wiphy->n_vendor_events))
11387                         return NULL;
11388                 info = &wiphy->vendor_events[vendor_event_idx];
11389                 break;
11390         default:
11391                 WARN_ON(1);
11392                 return NULL;
11393         }
11394
11395         return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, portid, 0,
11396                                            cmd, attr, info, gfp);
11397 }
11398 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
11399
11400 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
11401 {
11402         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
11403         void *hdr = ((void **)skb->cb)[1];
11404         struct nlmsghdr *nlhdr = nlmsg_hdr(skb);
11405         struct nlattr *data = ((void **)skb->cb)[2];
11406         enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
11407
11408         /* clear CB data for netlink core to own from now on */
11409         memset(skb->cb, 0, sizeof(skb->cb));
11410
11411         nla_nest_end(skb, data);
11412         genlmsg_end(skb, hdr);
11413
11414         if (nlhdr->nlmsg_pid) {
11415                 genlmsg_unicast(wiphy_net(&rdev->wiphy), skb,
11416                                 nlhdr->nlmsg_pid);
11417         } else {
11418                 if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
11419                         mcgrp = NL80211_MCGRP_VENDOR;
11420
11421                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11422                                         skb, 0, mcgrp, gfp);
11423         }
11424 }
11425 EXPORT_SYMBOL(__cfg80211_send_event_skb);
11426
11427 #ifdef CONFIG_NL80211_TESTMODE
11428 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
11429 {
11430         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11431         struct wireless_dev *wdev;
11432         int err;
11433
11434         lockdep_assert_held(&rdev->wiphy.mtx);
11435
11436         wdev = __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
11437                                           info->attrs);
11438
11439         if (!rdev->ops->testmode_cmd)
11440                 return -EOPNOTSUPP;
11441
11442         if (IS_ERR(wdev)) {
11443                 err = PTR_ERR(wdev);
11444                 if (err != -EINVAL)
11445                         return err;
11446                 wdev = NULL;
11447         } else if (wdev->wiphy != &rdev->wiphy) {
11448                 return -EINVAL;
11449         }
11450
11451         if (!info->attrs[NL80211_ATTR_TESTDATA])
11452                 return -EINVAL;
11453
11454         rdev->cur_cmd_info = info;
11455         err = rdev_testmode_cmd(rdev, wdev,
11456                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
11457                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
11458         rdev->cur_cmd_info = NULL;
11459
11460         return err;
11461 }
11462
11463 static int nl80211_testmode_dump(struct sk_buff *skb,
11464                                  struct netlink_callback *cb)
11465 {
11466         struct cfg80211_registered_device *rdev;
11467         struct nlattr **attrbuf = NULL;
11468         int err;
11469         long phy_idx;
11470         void *data = NULL;
11471         int data_len = 0;
11472
11473         rtnl_lock();
11474
11475         if (cb->args[0]) {
11476                 /*
11477                  * 0 is a valid index, but not valid for args[0],
11478                  * so we need to offset by 1.
11479                  */
11480                 phy_idx = cb->args[0] - 1;
11481
11482                 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
11483                 if (!rdev) {
11484                         err = -ENOENT;
11485                         goto out_err;
11486                 }
11487         } else {
11488                 attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf),
11489                                   GFP_KERNEL);
11490                 if (!attrbuf) {
11491                         err = -ENOMEM;
11492                         goto out_err;
11493                 }
11494
11495                 err = nlmsg_parse_deprecated(cb->nlh,
11496                                              GENL_HDRLEN + nl80211_fam.hdrsize,
11497                                              attrbuf, nl80211_fam.maxattr,
11498                                              nl80211_policy, NULL);
11499                 if (err)
11500                         goto out_err;
11501
11502                 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11503                 if (IS_ERR(rdev)) {
11504                         err = PTR_ERR(rdev);
11505                         goto out_err;
11506                 }
11507                 phy_idx = rdev->wiphy_idx;
11508
11509                 if (attrbuf[NL80211_ATTR_TESTDATA])
11510                         cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
11511         }
11512
11513         if (cb->args[1]) {
11514                 data = nla_data((void *)cb->args[1]);
11515                 data_len = nla_len((void *)cb->args[1]);
11516         }
11517
11518         if (!rdev->ops->testmode_dump) {
11519                 err = -EOPNOTSUPP;
11520                 goto out_err;
11521         }
11522
11523         while (1) {
11524                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
11525                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
11526                                            NL80211_CMD_TESTMODE);
11527                 struct nlattr *tmdata;
11528
11529                 if (!hdr)
11530                         break;
11531
11532                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
11533                         genlmsg_cancel(skb, hdr);
11534                         break;
11535                 }
11536
11537                 tmdata = nla_nest_start_noflag(skb, NL80211_ATTR_TESTDATA);
11538                 if (!tmdata) {
11539                         genlmsg_cancel(skb, hdr);
11540                         break;
11541                 }
11542                 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
11543                 nla_nest_end(skb, tmdata);
11544
11545                 if (err == -ENOBUFS || err == -ENOENT) {
11546                         genlmsg_cancel(skb, hdr);
11547                         break;
11548                 } else if (err) {
11549                         genlmsg_cancel(skb, hdr);
11550                         goto out_err;
11551                 }
11552
11553                 genlmsg_end(skb, hdr);
11554         }
11555
11556         err = skb->len;
11557         /* see above */
11558         cb->args[0] = phy_idx + 1;
11559  out_err:
11560         kfree(attrbuf);
11561         rtnl_unlock();
11562         return err;
11563 }
11564 #endif
11565
11566 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
11567 {
11568         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11569         struct net_device *dev = info->user_ptr[1];
11570         struct cfg80211_connect_params connect;
11571         struct wiphy *wiphy;
11572         struct cfg80211_cached_keys *connkeys = NULL;
11573         u32 freq = 0;
11574         int err;
11575
11576         memset(&connect, 0, sizeof(connect));
11577
11578         if (!info->attrs[NL80211_ATTR_SSID] ||
11579             !nla_len(info->attrs[NL80211_ATTR_SSID]))
11580                 return -EINVAL;
11581
11582         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11583                 connect.auth_type =
11584                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11585                 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
11586                                              NL80211_CMD_CONNECT))
11587                         return -EINVAL;
11588         } else
11589                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
11590
11591         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
11592
11593         if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
11594             !wiphy_ext_feature_isset(&rdev->wiphy,
11595                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
11596                 return -EINVAL;
11597         connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
11598
11599         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
11600                                       NL80211_MAX_NR_CIPHER_SUITES);
11601         if (err)
11602                 return err;
11603
11604         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11605             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11606                 return -EOPNOTSUPP;
11607
11608         wiphy = &rdev->wiphy;
11609
11610         connect.bg_scan_period = -1;
11611         if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
11612                 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
11613                 connect.bg_scan_period =
11614                         nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
11615         }
11616
11617         if (info->attrs[NL80211_ATTR_MAC])
11618                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11619         else if (info->attrs[NL80211_ATTR_MAC_HINT])
11620                 connect.bssid_hint =
11621                         nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
11622         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11623         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11624
11625         if (info->attrs[NL80211_ATTR_IE]) {
11626                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11627                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11628         }
11629
11630         if (info->attrs[NL80211_ATTR_USE_MFP]) {
11631                 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
11632                 if (connect.mfp == NL80211_MFP_OPTIONAL &&
11633                     !wiphy_ext_feature_isset(&rdev->wiphy,
11634                                              NL80211_EXT_FEATURE_MFP_OPTIONAL))
11635                         return -EOPNOTSUPP;
11636         } else {
11637                 connect.mfp = NL80211_MFP_NO;
11638         }
11639
11640         if (info->attrs[NL80211_ATTR_PREV_BSSID])
11641                 connect.prev_bssid =
11642                         nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
11643
11644         if (info->attrs[NL80211_ATTR_WIPHY_FREQ])
11645                 freq = MHZ_TO_KHZ(nla_get_u32(
11646                                         info->attrs[NL80211_ATTR_WIPHY_FREQ]));
11647         if (info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET])
11648                 freq +=
11649                     nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_OFFSET]);
11650
11651         if (freq) {
11652                 connect.channel = nl80211_get_valid_chan(wiphy, freq);
11653                 if (!connect.channel)
11654                         return -EINVAL;
11655         } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
11656                 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
11657                 freq = MHZ_TO_KHZ(freq);
11658                 connect.channel_hint = nl80211_get_valid_chan(wiphy, freq);
11659                 if (!connect.channel_hint)
11660                         return -EINVAL;
11661         }
11662
11663         if (info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]) {
11664                 connect.edmg.channels =
11665                       nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_CHANNELS]);
11666
11667                 if (info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG])
11668                         connect.edmg.bw_config =
11669                                 nla_get_u8(info->attrs[NL80211_ATTR_WIPHY_EDMG_BW_CONFIG]);
11670         }
11671
11672         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
11673                 connkeys = nl80211_parse_connkeys(rdev, info, NULL);
11674                 if (IS_ERR(connkeys))
11675                         return PTR_ERR(connkeys);
11676         }
11677
11678         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
11679                 connect.flags |= ASSOC_REQ_DISABLE_HT;
11680
11681         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
11682                 memcpy(&connect.ht_capa_mask,
11683                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
11684                        sizeof(connect.ht_capa_mask));
11685
11686         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
11687                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
11688                         kfree_sensitive(connkeys);
11689                         return -EINVAL;
11690                 }
11691                 memcpy(&connect.ht_capa,
11692                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
11693                        sizeof(connect.ht_capa));
11694         }
11695
11696         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
11697                 connect.flags |= ASSOC_REQ_DISABLE_VHT;
11698
11699         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HE]))
11700                 connect.flags |= ASSOC_REQ_DISABLE_HE;
11701
11702         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_EHT]))
11703                 connect.flags |= ASSOC_REQ_DISABLE_EHT;
11704
11705         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
11706                 memcpy(&connect.vht_capa_mask,
11707                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
11708                        sizeof(connect.vht_capa_mask));
11709
11710         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
11711                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
11712                         kfree_sensitive(connkeys);
11713                         return -EINVAL;
11714                 }
11715                 memcpy(&connect.vht_capa,
11716                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
11717                        sizeof(connect.vht_capa));
11718         }
11719
11720         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
11721                 if (!((rdev->wiphy.features &
11722                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
11723                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
11724                     !wiphy_ext_feature_isset(&rdev->wiphy,
11725                                              NL80211_EXT_FEATURE_RRM)) {
11726                         kfree_sensitive(connkeys);
11727                         return -EINVAL;
11728                 }
11729                 connect.flags |= ASSOC_REQ_USE_RRM;
11730         }
11731
11732         connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
11733         if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
11734                 kfree_sensitive(connkeys);
11735                 return -EOPNOTSUPP;
11736         }
11737
11738         if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
11739                 /* bss selection makes no sense if bssid is set */
11740                 if (connect.bssid) {
11741                         kfree_sensitive(connkeys);
11742                         return -EINVAL;
11743                 }
11744
11745                 err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
11746                                        wiphy, &connect.bss_select);
11747                 if (err) {
11748                         kfree_sensitive(connkeys);
11749                         return err;
11750                 }
11751         }
11752
11753         if (wiphy_ext_feature_isset(&rdev->wiphy,
11754                                     NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
11755             info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11756             info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11757             info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11758             info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11759                 connect.fils_erp_username =
11760                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11761                 connect.fils_erp_username_len =
11762                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11763                 connect.fils_erp_realm =
11764                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11765                 connect.fils_erp_realm_len =
11766                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11767                 connect.fils_erp_next_seq_num =
11768                         nla_get_u16(
11769                            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11770                 connect.fils_erp_rrk =
11771                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11772                 connect.fils_erp_rrk_len =
11773                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11774         } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11775                    info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11776                    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11777                    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11778                 kfree_sensitive(connkeys);
11779                 return -EINVAL;
11780         }
11781
11782         if (nla_get_flag(info->attrs[NL80211_ATTR_EXTERNAL_AUTH_SUPPORT])) {
11783                 if (!info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11784                         kfree_sensitive(connkeys);
11785                         GENL_SET_ERR_MSG(info,
11786                                          "external auth requires connection ownership");
11787                         return -EINVAL;
11788                 }
11789                 connect.flags |= CONNECT_REQ_EXTERNAL_AUTH_SUPPORT;
11790         }
11791
11792         if (nla_get_flag(info->attrs[NL80211_ATTR_MLO_SUPPORT]))
11793                 connect.flags |= CONNECT_REQ_MLO_SUPPORT;
11794
11795         wdev_lock(dev->ieee80211_ptr);
11796
11797         err = cfg80211_connect(rdev, dev, &connect, connkeys,
11798                                connect.prev_bssid);
11799         if (err)
11800                 kfree_sensitive(connkeys);
11801
11802         if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
11803                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
11804                 if (connect.bssid)
11805                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
11806                                connect.bssid, ETH_ALEN);
11807                 else
11808                         eth_zero_addr(dev->ieee80211_ptr->disconnect_bssid);
11809         }
11810
11811         wdev_unlock(dev->ieee80211_ptr);
11812
11813         return err;
11814 }
11815
11816 static int nl80211_update_connect_params(struct sk_buff *skb,
11817                                          struct genl_info *info)
11818 {
11819         struct cfg80211_connect_params connect = {};
11820         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11821         struct net_device *dev = info->user_ptr[1];
11822         struct wireless_dev *wdev = dev->ieee80211_ptr;
11823         bool fils_sk_offload;
11824         u32 auth_type;
11825         u32 changed = 0;
11826         int ret;
11827
11828         if (!rdev->ops->update_connect_params)
11829                 return -EOPNOTSUPP;
11830
11831         if (info->attrs[NL80211_ATTR_IE]) {
11832                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11833                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11834                 changed |= UPDATE_ASSOC_IES;
11835         }
11836
11837         fils_sk_offload = wiphy_ext_feature_isset(&rdev->wiphy,
11838                                                   NL80211_EXT_FEATURE_FILS_SK_OFFLOAD);
11839
11840         /*
11841          * when driver supports fils-sk offload all attributes must be
11842          * provided. So the else covers "fils-sk-not-all" and
11843          * "no-fils-sk-any".
11844          */
11845         if (fils_sk_offload &&
11846             info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
11847             info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
11848             info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
11849             info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11850                 connect.fils_erp_username =
11851                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11852                 connect.fils_erp_username_len =
11853                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
11854                 connect.fils_erp_realm =
11855                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11856                 connect.fils_erp_realm_len =
11857                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
11858                 connect.fils_erp_next_seq_num =
11859                         nla_get_u16(
11860                            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
11861                 connect.fils_erp_rrk =
11862                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11863                 connect.fils_erp_rrk_len =
11864                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
11865                 changed |= UPDATE_FILS_ERP_INFO;
11866         } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
11867                    info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
11868                    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
11869                    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
11870                 return -EINVAL;
11871         }
11872
11873         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
11874                 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
11875                 if (!nl80211_valid_auth_type(rdev, auth_type,
11876                                              NL80211_CMD_CONNECT))
11877                         return -EINVAL;
11878
11879                 if (auth_type == NL80211_AUTHTYPE_FILS_SK &&
11880                     fils_sk_offload && !(changed & UPDATE_FILS_ERP_INFO))
11881                         return -EINVAL;
11882
11883                 connect.auth_type = auth_type;
11884                 changed |= UPDATE_AUTH_TYPE;
11885         }
11886
11887         wdev_lock(dev->ieee80211_ptr);
11888         if (!wdev->connected)
11889                 ret = -ENOLINK;
11890         else
11891                 ret = rdev_update_connect_params(rdev, dev, &connect, changed);
11892         wdev_unlock(dev->ieee80211_ptr);
11893
11894         return ret;
11895 }
11896
11897 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
11898 {
11899         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11900         struct net_device *dev = info->user_ptr[1];
11901         u16 reason;
11902         int ret;
11903
11904         if (dev->ieee80211_ptr->conn_owner_nlportid &&
11905             dev->ieee80211_ptr->conn_owner_nlportid != info->snd_portid)
11906                 return -EPERM;
11907
11908         if (!info->attrs[NL80211_ATTR_REASON_CODE])
11909                 reason = WLAN_REASON_DEAUTH_LEAVING;
11910         else
11911                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
11912
11913         if (reason == 0)
11914                 return -EINVAL;
11915
11916         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
11917             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
11918                 return -EOPNOTSUPP;
11919
11920         wdev_lock(dev->ieee80211_ptr);
11921         ret = cfg80211_disconnect(rdev, dev, reason, true);
11922         wdev_unlock(dev->ieee80211_ptr);
11923         return ret;
11924 }
11925
11926 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
11927 {
11928         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11929         struct net *net;
11930         int err;
11931
11932         if (info->attrs[NL80211_ATTR_PID]) {
11933                 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
11934
11935                 net = get_net_ns_by_pid(pid);
11936         } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
11937                 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
11938
11939                 net = get_net_ns_by_fd(fd);
11940         } else {
11941                 return -EINVAL;
11942         }
11943
11944         if (IS_ERR(net))
11945                 return PTR_ERR(net);
11946
11947         err = 0;
11948
11949         /* check if anything to do */
11950         if (!net_eq(wiphy_net(&rdev->wiphy), net))
11951                 err = cfg80211_switch_netns(rdev, net);
11952
11953         put_net(net);
11954         return err;
11955 }
11956
11957 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
11958 {
11959         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11960         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
11961                         struct cfg80211_pmksa *pmksa) = NULL;
11962         struct net_device *dev = info->user_ptr[1];
11963         struct cfg80211_pmksa pmksa;
11964
11965         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
11966
11967         if (!info->attrs[NL80211_ATTR_PMKID])
11968                 return -EINVAL;
11969
11970         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
11971
11972         if (info->attrs[NL80211_ATTR_MAC]) {
11973                 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
11974         } else if (info->attrs[NL80211_ATTR_SSID] &&
11975                    info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
11976                    (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
11977                     info->attrs[NL80211_ATTR_PMK])) {
11978                 pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
11979                 pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
11980                 pmksa.cache_id =
11981                         nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
11982         } else {
11983                 return -EINVAL;
11984         }
11985         if (info->attrs[NL80211_ATTR_PMK]) {
11986                 pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
11987                 pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
11988         }
11989
11990         if (info->attrs[NL80211_ATTR_PMK_LIFETIME])
11991                 pmksa.pmk_lifetime =
11992                         nla_get_u32(info->attrs[NL80211_ATTR_PMK_LIFETIME]);
11993
11994         if (info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD])
11995                 pmksa.pmk_reauth_threshold =
11996                         nla_get_u8(
11997                                 info->attrs[NL80211_ATTR_PMK_REAUTH_THRESHOLD]);
11998
11999         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12000             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
12001             !(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP &&
12002               wiphy_ext_feature_isset(&rdev->wiphy,
12003                                       NL80211_EXT_FEATURE_AP_PMKSA_CACHING)))
12004                 return -EOPNOTSUPP;
12005
12006         switch (info->genlhdr->cmd) {
12007         case NL80211_CMD_SET_PMKSA:
12008                 rdev_ops = rdev->ops->set_pmksa;
12009                 break;
12010         case NL80211_CMD_DEL_PMKSA:
12011                 rdev_ops = rdev->ops->del_pmksa;
12012                 break;
12013         default:
12014                 WARN_ON(1);
12015                 break;
12016         }
12017
12018         if (!rdev_ops)
12019                 return -EOPNOTSUPP;
12020
12021         return rdev_ops(&rdev->wiphy, dev, &pmksa);
12022 }
12023
12024 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
12025 {
12026         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12027         struct net_device *dev = info->user_ptr[1];
12028
12029         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
12030             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
12031                 return -EOPNOTSUPP;
12032
12033         if (!rdev->ops->flush_pmksa)
12034                 return -EOPNOTSUPP;
12035
12036         return rdev_flush_pmksa(rdev, dev);
12037 }
12038
12039 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
12040 {
12041         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12042         struct net_device *dev = info->user_ptr[1];
12043         u8 action_code, dialog_token;
12044         u32 peer_capability = 0;
12045         u16 status_code;
12046         u8 *peer;
12047         bool initiator;
12048
12049         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12050             !rdev->ops->tdls_mgmt)
12051                 return -EOPNOTSUPP;
12052
12053         if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
12054             !info->attrs[NL80211_ATTR_STATUS_CODE] ||
12055             !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
12056             !info->attrs[NL80211_ATTR_IE] ||
12057             !info->attrs[NL80211_ATTR_MAC])
12058                 return -EINVAL;
12059
12060         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12061         action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
12062         status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
12063         dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
12064         initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
12065         if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
12066                 peer_capability =
12067                         nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
12068
12069         return rdev_tdls_mgmt(rdev, dev, peer, action_code,
12070                               dialog_token, status_code, peer_capability,
12071                               initiator,
12072                               nla_data(info->attrs[NL80211_ATTR_IE]),
12073                               nla_len(info->attrs[NL80211_ATTR_IE]));
12074 }
12075
12076 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
12077 {
12078         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12079         struct net_device *dev = info->user_ptr[1];
12080         enum nl80211_tdls_operation operation;
12081         u8 *peer;
12082
12083         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
12084             !rdev->ops->tdls_oper)
12085                 return -EOPNOTSUPP;
12086
12087         if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
12088             !info->attrs[NL80211_ATTR_MAC])
12089                 return -EINVAL;
12090
12091         operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
12092         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12093
12094         return rdev_tdls_oper(rdev, dev, peer, operation);
12095 }
12096
12097 static int nl80211_remain_on_channel(struct sk_buff *skb,
12098                                      struct genl_info *info)
12099 {
12100         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12101         unsigned int link_id = nl80211_link_id(info->attrs);
12102         struct wireless_dev *wdev = info->user_ptr[1];
12103         struct cfg80211_chan_def chandef;
12104         struct sk_buff *msg;
12105         void *hdr;
12106         u64 cookie;
12107         u32 duration;
12108         int err;
12109
12110         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
12111             !info->attrs[NL80211_ATTR_DURATION])
12112                 return -EINVAL;
12113
12114         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12115
12116         if (!rdev->ops->remain_on_channel ||
12117             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
12118                 return -EOPNOTSUPP;
12119
12120         /*
12121          * We should be on that channel for at least a minimum amount of
12122          * time (10ms) but no longer than the driver supports.
12123          */
12124         if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12125             duration > rdev->wiphy.max_remain_on_channel_duration)
12126                 return -EINVAL;
12127
12128         err = nl80211_parse_chandef(rdev, info, &chandef);
12129         if (err)
12130                 return err;
12131
12132         wdev_lock(wdev);
12133         if (!cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12134                 const struct cfg80211_chan_def *oper_chandef, *compat_chandef;
12135
12136                 oper_chandef = wdev_chandef(wdev, link_id);
12137
12138                 if (WARN_ON(!oper_chandef)) {
12139                         /* cannot happen since we must beacon to get here */
12140                         WARN_ON(1);
12141                         wdev_unlock(wdev);
12142                         return -EBUSY;
12143                 }
12144
12145                 /* note: returns first one if identical chandefs */
12146                 compat_chandef = cfg80211_chandef_compatible(&chandef,
12147                                                              oper_chandef);
12148
12149                 if (compat_chandef != &chandef) {
12150                         wdev_unlock(wdev);
12151                         return -EBUSY;
12152                 }
12153         }
12154         wdev_unlock(wdev);
12155
12156         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12157         if (!msg)
12158                 return -ENOMEM;
12159
12160         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12161                              NL80211_CMD_REMAIN_ON_CHANNEL);
12162         if (!hdr) {
12163                 err = -ENOBUFS;
12164                 goto free_msg;
12165         }
12166
12167         err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
12168                                      duration, &cookie);
12169
12170         if (err)
12171                 goto free_msg;
12172
12173         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12174                               NL80211_ATTR_PAD))
12175                 goto nla_put_failure;
12176
12177         genlmsg_end(msg, hdr);
12178
12179         return genlmsg_reply(msg, info);
12180
12181  nla_put_failure:
12182         err = -ENOBUFS;
12183  free_msg:
12184         nlmsg_free(msg);
12185         return err;
12186 }
12187
12188 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
12189                                             struct genl_info *info)
12190 {
12191         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12192         struct wireless_dev *wdev = info->user_ptr[1];
12193         u64 cookie;
12194
12195         if (!info->attrs[NL80211_ATTR_COOKIE])
12196                 return -EINVAL;
12197
12198         if (!rdev->ops->cancel_remain_on_channel)
12199                 return -EOPNOTSUPP;
12200
12201         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12202
12203         return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
12204 }
12205
12206 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
12207                                        struct genl_info *info)
12208 {
12209         struct cfg80211_bitrate_mask mask;
12210         unsigned int link_id = nl80211_link_id(info->attrs);
12211         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12212         struct net_device *dev = info->user_ptr[1];
12213         struct wireless_dev *wdev = dev->ieee80211_ptr;
12214         int err;
12215
12216         if (!rdev->ops->set_bitrate_mask)
12217                 return -EOPNOTSUPP;
12218
12219         wdev_lock(wdev);
12220         err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12221                                             NL80211_ATTR_TX_RATES, &mask,
12222                                             dev, true, link_id);
12223         if (err)
12224                 goto out;
12225
12226         err = rdev_set_bitrate_mask(rdev, dev, link_id, NULL, &mask);
12227 out:
12228         wdev_unlock(wdev);
12229         return err;
12230 }
12231
12232 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
12233 {
12234         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12235         struct wireless_dev *wdev = info->user_ptr[1];
12236         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
12237
12238         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
12239                 return -EINVAL;
12240
12241         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
12242                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
12243
12244         switch (wdev->iftype) {
12245         case NL80211_IFTYPE_STATION:
12246         case NL80211_IFTYPE_ADHOC:
12247         case NL80211_IFTYPE_P2P_CLIENT:
12248         case NL80211_IFTYPE_AP:
12249         case NL80211_IFTYPE_AP_VLAN:
12250         case NL80211_IFTYPE_MESH_POINT:
12251         case NL80211_IFTYPE_P2P_GO:
12252         case NL80211_IFTYPE_P2P_DEVICE:
12253                 break;
12254         case NL80211_IFTYPE_NAN:
12255         default:
12256                 return -EOPNOTSUPP;
12257         }
12258
12259         /* not much point in registering if we can't reply */
12260         if (!rdev->ops->mgmt_tx)
12261                 return -EOPNOTSUPP;
12262
12263         if (info->attrs[NL80211_ATTR_RECEIVE_MULTICAST] &&
12264             !wiphy_ext_feature_isset(&rdev->wiphy,
12265                                      NL80211_EXT_FEATURE_MULTICAST_REGISTRATIONS)) {
12266                 GENL_SET_ERR_MSG(info,
12267                                  "multicast RX registrations are not supported");
12268                 return -EOPNOTSUPP;
12269         }
12270
12271         return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
12272                                            nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12273                                            nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]),
12274                                            info->attrs[NL80211_ATTR_RECEIVE_MULTICAST],
12275                                            info->extack);
12276 }
12277
12278 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
12279 {
12280         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12281         struct wireless_dev *wdev = info->user_ptr[1];
12282         struct cfg80211_chan_def chandef;
12283         int err;
12284         void *hdr = NULL;
12285         u64 cookie;
12286         struct sk_buff *msg = NULL;
12287         struct cfg80211_mgmt_tx_params params = {
12288                 .dont_wait_for_ack =
12289                         info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
12290         };
12291
12292         if (!info->attrs[NL80211_ATTR_FRAME])
12293                 return -EINVAL;
12294
12295         if (!rdev->ops->mgmt_tx)
12296                 return -EOPNOTSUPP;
12297
12298         switch (wdev->iftype) {
12299         case NL80211_IFTYPE_P2P_DEVICE:
12300                 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
12301                         return -EINVAL;
12302                 break;
12303         case NL80211_IFTYPE_STATION:
12304         case NL80211_IFTYPE_ADHOC:
12305         case NL80211_IFTYPE_P2P_CLIENT:
12306         case NL80211_IFTYPE_AP:
12307         case NL80211_IFTYPE_AP_VLAN:
12308         case NL80211_IFTYPE_MESH_POINT:
12309         case NL80211_IFTYPE_P2P_GO:
12310                 break;
12311         case NL80211_IFTYPE_NAN:
12312         default:
12313                 return -EOPNOTSUPP;
12314         }
12315
12316         if (info->attrs[NL80211_ATTR_DURATION]) {
12317                 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12318                         return -EINVAL;
12319                 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
12320
12321                 /*
12322                  * We should wait on the channel for at least a minimum amount
12323                  * of time (10ms) but no longer than the driver supports.
12324                  */
12325                 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
12326                     params.wait > rdev->wiphy.max_remain_on_channel_duration)
12327                         return -EINVAL;
12328         }
12329
12330         params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
12331
12332         if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
12333                 return -EINVAL;
12334
12335         params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
12336
12337         /* get the channel if any has been specified, otherwise pass NULL to
12338          * the driver. The latter will use the current one
12339          */
12340         chandef.chan = NULL;
12341         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12342                 err = nl80211_parse_chandef(rdev, info, &chandef);
12343                 if (err)
12344                         return err;
12345         }
12346
12347         if (!chandef.chan && params.offchan)
12348                 return -EINVAL;
12349
12350         wdev_lock(wdev);
12351         if (params.offchan &&
12352             !cfg80211_off_channel_oper_allowed(wdev, chandef.chan)) {
12353                 wdev_unlock(wdev);
12354                 return -EBUSY;
12355         }
12356
12357         params.link_id = nl80211_link_id_or_invalid(info->attrs);
12358         /*
12359          * This now races due to the unlock, but we cannot check
12360          * the valid links for the _station_ anyway, so that's up
12361          * to the driver.
12362          */
12363         if (params.link_id >= 0 &&
12364             !(wdev->valid_links & BIT(params.link_id))) {
12365                 wdev_unlock(wdev);
12366                 return -EINVAL;
12367         }
12368         wdev_unlock(wdev);
12369
12370         params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
12371         params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
12372
12373         if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
12374                 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12375                 int i;
12376
12377                 if (len % sizeof(u16))
12378                         return -EINVAL;
12379
12380                 params.n_csa_offsets = len / sizeof(u16);
12381                 params.csa_offsets =
12382                         nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
12383
12384                 /* check that all the offsets fit the frame */
12385                 for (i = 0; i < params.n_csa_offsets; i++) {
12386                         if (params.csa_offsets[i] >= params.len)
12387                                 return -EINVAL;
12388                 }
12389         }
12390
12391         if (!params.dont_wait_for_ack) {
12392                 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12393                 if (!msg)
12394                         return -ENOMEM;
12395
12396                 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12397                                      NL80211_CMD_FRAME);
12398                 if (!hdr) {
12399                         err = -ENOBUFS;
12400                         goto free_msg;
12401                 }
12402         }
12403
12404         params.chan = chandef.chan;
12405         err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
12406         if (err)
12407                 goto free_msg;
12408
12409         if (msg) {
12410                 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
12411                                       NL80211_ATTR_PAD))
12412                         goto nla_put_failure;
12413
12414                 genlmsg_end(msg, hdr);
12415                 return genlmsg_reply(msg, info);
12416         }
12417
12418         return 0;
12419
12420  nla_put_failure:
12421         err = -ENOBUFS;
12422  free_msg:
12423         nlmsg_free(msg);
12424         return err;
12425 }
12426
12427 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
12428 {
12429         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12430         struct wireless_dev *wdev = info->user_ptr[1];
12431         u64 cookie;
12432
12433         if (!info->attrs[NL80211_ATTR_COOKIE])
12434                 return -EINVAL;
12435
12436         if (!rdev->ops->mgmt_tx_cancel_wait)
12437                 return -EOPNOTSUPP;
12438
12439         switch (wdev->iftype) {
12440         case NL80211_IFTYPE_STATION:
12441         case NL80211_IFTYPE_ADHOC:
12442         case NL80211_IFTYPE_P2P_CLIENT:
12443         case NL80211_IFTYPE_AP:
12444         case NL80211_IFTYPE_AP_VLAN:
12445         case NL80211_IFTYPE_P2P_GO:
12446         case NL80211_IFTYPE_P2P_DEVICE:
12447                 break;
12448         case NL80211_IFTYPE_NAN:
12449         default:
12450                 return -EOPNOTSUPP;
12451         }
12452
12453         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
12454
12455         return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
12456 }
12457
12458 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
12459 {
12460         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12461         struct wireless_dev *wdev;
12462         struct net_device *dev = info->user_ptr[1];
12463         u8 ps_state;
12464         bool state;
12465         int err;
12466
12467         if (!info->attrs[NL80211_ATTR_PS_STATE])
12468                 return -EINVAL;
12469
12470         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
12471
12472         wdev = dev->ieee80211_ptr;
12473
12474         if (!rdev->ops->set_power_mgmt)
12475                 return -EOPNOTSUPP;
12476
12477         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
12478
12479         if (state == wdev->ps)
12480                 return 0;
12481
12482         err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
12483         if (!err)
12484                 wdev->ps = state;
12485         return err;
12486 }
12487
12488 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
12489 {
12490         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12491         enum nl80211_ps_state ps_state;
12492         struct wireless_dev *wdev;
12493         struct net_device *dev = info->user_ptr[1];
12494         struct sk_buff *msg;
12495         void *hdr;
12496         int err;
12497
12498         wdev = dev->ieee80211_ptr;
12499
12500         if (!rdev->ops->set_power_mgmt)
12501                 return -EOPNOTSUPP;
12502
12503         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12504         if (!msg)
12505                 return -ENOMEM;
12506
12507         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
12508                              NL80211_CMD_GET_POWER_SAVE);
12509         if (!hdr) {
12510                 err = -ENOBUFS;
12511                 goto free_msg;
12512         }
12513
12514         if (wdev->ps)
12515                 ps_state = NL80211_PS_ENABLED;
12516         else
12517                 ps_state = NL80211_PS_DISABLED;
12518
12519         if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
12520                 goto nla_put_failure;
12521
12522         genlmsg_end(msg, hdr);
12523         return genlmsg_reply(msg, info);
12524
12525  nla_put_failure:
12526         err = -ENOBUFS;
12527  free_msg:
12528         nlmsg_free(msg);
12529         return err;
12530 }
12531
12532 static const struct nla_policy
12533 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
12534         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
12535         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
12536         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
12537         [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
12538         [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
12539         [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
12540         [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
12541 };
12542
12543 static int nl80211_set_cqm_txe(struct genl_info *info,
12544                                u32 rate, u32 pkts, u32 intvl)
12545 {
12546         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12547         struct net_device *dev = info->user_ptr[1];
12548         struct wireless_dev *wdev = dev->ieee80211_ptr;
12549
12550         if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
12551                 return -EINVAL;
12552
12553         if (!rdev->ops->set_cqm_txe_config)
12554                 return -EOPNOTSUPP;
12555
12556         if (wdev->iftype != NL80211_IFTYPE_STATION &&
12557             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12558                 return -EOPNOTSUPP;
12559
12560         return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
12561 }
12562
12563 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
12564                                     struct net_device *dev)
12565 {
12566         struct wireless_dev *wdev = dev->ieee80211_ptr;
12567         s32 last, low, high;
12568         u32 hyst;
12569         int i, n, low_index;
12570         int err;
12571
12572         /* RSSI reporting disabled? */
12573         if (!wdev->cqm_config)
12574                 return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
12575
12576         /*
12577          * Obtain current RSSI value if possible, if not and no RSSI threshold
12578          * event has been received yet, we should receive an event after a
12579          * connection is established and enough beacons received to calculate
12580          * the average.
12581          */
12582         if (!wdev->cqm_config->last_rssi_event_value &&
12583             wdev->links[0].client.current_bss &&
12584             rdev->ops->get_station) {
12585                 struct station_info sinfo = {};
12586                 u8 *mac_addr;
12587
12588                 mac_addr = wdev->links[0].client.current_bss->pub.bssid;
12589
12590                 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
12591                 if (err)
12592                         return err;
12593
12594                 cfg80211_sinfo_release_content(&sinfo);
12595                 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
12596                         wdev->cqm_config->last_rssi_event_value =
12597                                 (s8) sinfo.rx_beacon_signal_avg;
12598         }
12599
12600         last = wdev->cqm_config->last_rssi_event_value;
12601         hyst = wdev->cqm_config->rssi_hyst;
12602         n = wdev->cqm_config->n_rssi_thresholds;
12603
12604         for (i = 0; i < n; i++) {
12605                 i = array_index_nospec(i, n);
12606                 if (last < wdev->cqm_config->rssi_thresholds[i])
12607                         break;
12608         }
12609
12610         low_index = i - 1;
12611         if (low_index >= 0) {
12612                 low_index = array_index_nospec(low_index, n);
12613                 low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
12614         } else {
12615                 low = S32_MIN;
12616         }
12617         if (i < n) {
12618                 i = array_index_nospec(i, n);
12619                 high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
12620         } else {
12621                 high = S32_MAX;
12622         }
12623
12624         return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
12625 }
12626
12627 static int nl80211_set_cqm_rssi(struct genl_info *info,
12628                                 const s32 *thresholds, int n_thresholds,
12629                                 u32 hysteresis)
12630 {
12631         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12632         struct net_device *dev = info->user_ptr[1];
12633         struct wireless_dev *wdev = dev->ieee80211_ptr;
12634         int i, err;
12635         s32 prev = S32_MIN;
12636
12637         /* Check all values negative and sorted */
12638         for (i = 0; i < n_thresholds; i++) {
12639                 if (thresholds[i] > 0 || thresholds[i] <= prev)
12640                         return -EINVAL;
12641
12642                 prev = thresholds[i];
12643         }
12644
12645         if (wdev->iftype != NL80211_IFTYPE_STATION &&
12646             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12647                 return -EOPNOTSUPP;
12648
12649         wdev_lock(wdev);
12650         cfg80211_cqm_config_free(wdev);
12651         wdev_unlock(wdev);
12652
12653         if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
12654                 if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
12655                         return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
12656
12657                 return rdev_set_cqm_rssi_config(rdev, dev,
12658                                                 thresholds[0], hysteresis);
12659         }
12660
12661         if (!wiphy_ext_feature_isset(&rdev->wiphy,
12662                                      NL80211_EXT_FEATURE_CQM_RSSI_LIST))
12663                 return -EOPNOTSUPP;
12664
12665         if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
12666                 n_thresholds = 0;
12667
12668         wdev_lock(wdev);
12669         if (n_thresholds) {
12670                 struct cfg80211_cqm_config *cqm_config;
12671
12672                 cqm_config = kzalloc(struct_size(cqm_config, rssi_thresholds,
12673                                                  n_thresholds),
12674                                      GFP_KERNEL);
12675                 if (!cqm_config) {
12676                         err = -ENOMEM;
12677                         goto unlock;
12678                 }
12679
12680                 cqm_config->rssi_hyst = hysteresis;
12681                 cqm_config->n_rssi_thresholds = n_thresholds;
12682                 memcpy(cqm_config->rssi_thresholds, thresholds,
12683                        flex_array_size(cqm_config, rssi_thresholds,
12684                                        n_thresholds));
12685
12686                 wdev->cqm_config = cqm_config;
12687         }
12688
12689         err = cfg80211_cqm_rssi_update(rdev, dev);
12690
12691 unlock:
12692         wdev_unlock(wdev);
12693
12694         return err;
12695 }
12696
12697 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
12698 {
12699         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
12700         struct nlattr *cqm;
12701         int err;
12702
12703         cqm = info->attrs[NL80211_ATTR_CQM];
12704         if (!cqm)
12705                 return -EINVAL;
12706
12707         err = nla_parse_nested_deprecated(attrs, NL80211_ATTR_CQM_MAX, cqm,
12708                                           nl80211_attr_cqm_policy,
12709                                           info->extack);
12710         if (err)
12711                 return err;
12712
12713         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
12714             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
12715                 const s32 *thresholds =
12716                         nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12717                 int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
12718                 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
12719
12720                 if (len % 4)
12721                         return -EINVAL;
12722
12723                 return nl80211_set_cqm_rssi(info, thresholds, len / 4,
12724                                             hysteresis);
12725         }
12726
12727         if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
12728             attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
12729             attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
12730                 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
12731                 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
12732                 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
12733
12734                 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
12735         }
12736
12737         return -EINVAL;
12738 }
12739
12740 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
12741 {
12742         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12743         struct net_device *dev = info->user_ptr[1];
12744         struct ocb_setup setup = {};
12745         int err;
12746
12747         err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12748         if (err)
12749                 return err;
12750
12751         return cfg80211_join_ocb(rdev, dev, &setup);
12752 }
12753
12754 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
12755 {
12756         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12757         struct net_device *dev = info->user_ptr[1];
12758
12759         return cfg80211_leave_ocb(rdev, dev);
12760 }
12761
12762 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
12763 {
12764         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12765         struct net_device *dev = info->user_ptr[1];
12766         struct mesh_config cfg;
12767         struct mesh_setup setup;
12768         int err;
12769
12770         /* start with default */
12771         memcpy(&cfg, &default_mesh_config, sizeof(cfg));
12772         memcpy(&setup, &default_mesh_setup, sizeof(setup));
12773
12774         if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
12775                 /* and parse parameters if given */
12776                 err = nl80211_parse_mesh_config(info, &cfg, NULL);
12777                 if (err)
12778                         return err;
12779         }
12780
12781         if (!info->attrs[NL80211_ATTR_MESH_ID] ||
12782             !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
12783                 return -EINVAL;
12784
12785         setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
12786         setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
12787
12788         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
12789             !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
12790                             nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
12791                         return -EINVAL;
12792
12793         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
12794                 setup.beacon_interval =
12795                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
12796
12797                 err = cfg80211_validate_beacon_int(rdev,
12798                                                    NL80211_IFTYPE_MESH_POINT,
12799                                                    setup.beacon_interval);
12800                 if (err)
12801                         return err;
12802         }
12803
12804         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
12805                 setup.dtim_period =
12806                         nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
12807                 if (setup.dtim_period < 1 || setup.dtim_period > 100)
12808                         return -EINVAL;
12809         }
12810
12811         if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
12812                 /* parse additional setup parameters if given */
12813                 err = nl80211_parse_mesh_setup(info, &setup);
12814                 if (err)
12815                         return err;
12816         }
12817
12818         if (setup.user_mpm)
12819                 cfg.auto_open_plinks = false;
12820
12821         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
12822                 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
12823                 if (err)
12824                         return err;
12825         } else {
12826                 /* __cfg80211_join_mesh() will sort it out */
12827                 setup.chandef.chan = NULL;
12828         }
12829
12830         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
12831                 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12832                 int n_rates =
12833                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
12834                 struct ieee80211_supported_band *sband;
12835
12836                 if (!setup.chandef.chan)
12837                         return -EINVAL;
12838
12839                 sband = rdev->wiphy.bands[setup.chandef.chan->band];
12840
12841                 err = ieee80211_get_ratemask(sband, rates, n_rates,
12842                                              &setup.basic_rates);
12843                 if (err)
12844                         return err;
12845         }
12846
12847         if (info->attrs[NL80211_ATTR_TX_RATES]) {
12848                 err = nl80211_parse_tx_bitrate_mask(info, info->attrs,
12849                                                     NL80211_ATTR_TX_RATES,
12850                                                     &setup.beacon_rate,
12851                                                     dev, false, 0);
12852                 if (err)
12853                         return err;
12854
12855                 if (!setup.chandef.chan)
12856                         return -EINVAL;
12857
12858                 err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
12859                                               &setup.beacon_rate);
12860                 if (err)
12861                         return err;
12862         }
12863
12864         setup.userspace_handles_dfs =
12865                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
12866
12867         if (info->attrs[NL80211_ATTR_CONTROL_PORT_OVER_NL80211]) {
12868                 int r = validate_pae_over_nl80211(rdev, info);
12869
12870                 if (r < 0)
12871                         return r;
12872
12873                 setup.control_port_over_nl80211 = true;
12874         }
12875
12876         wdev_lock(dev->ieee80211_ptr);
12877         err = __cfg80211_join_mesh(rdev, dev, &setup, &cfg);
12878         if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER])
12879                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
12880         wdev_unlock(dev->ieee80211_ptr);
12881
12882         return err;
12883 }
12884
12885 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
12886 {
12887         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12888         struct net_device *dev = info->user_ptr[1];
12889
12890         return cfg80211_leave_mesh(rdev, dev);
12891 }
12892
12893 #ifdef CONFIG_PM
12894 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
12895                                         struct cfg80211_registered_device *rdev)
12896 {
12897         struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
12898         struct nlattr *nl_pats, *nl_pat;
12899         int i, pat_len;
12900
12901         if (!wowlan->n_patterns)
12902                 return 0;
12903
12904         nl_pats = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
12905         if (!nl_pats)
12906                 return -ENOBUFS;
12907
12908         for (i = 0; i < wowlan->n_patterns; i++) {
12909                 nl_pat = nla_nest_start_noflag(msg, i + 1);
12910                 if (!nl_pat)
12911                         return -ENOBUFS;
12912                 pat_len = wowlan->patterns[i].pattern_len;
12913                 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
12914                             wowlan->patterns[i].mask) ||
12915                     nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
12916                             wowlan->patterns[i].pattern) ||
12917                     nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
12918                                 wowlan->patterns[i].pkt_offset))
12919                         return -ENOBUFS;
12920                 nla_nest_end(msg, nl_pat);
12921         }
12922         nla_nest_end(msg, nl_pats);
12923
12924         return 0;
12925 }
12926
12927 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
12928                                    struct cfg80211_wowlan_tcp *tcp)
12929 {
12930         struct nlattr *nl_tcp;
12931
12932         if (!tcp)
12933                 return 0;
12934
12935         nl_tcp = nla_nest_start_noflag(msg,
12936                                        NL80211_WOWLAN_TRIG_TCP_CONNECTION);
12937         if (!nl_tcp)
12938                 return -ENOBUFS;
12939
12940         if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
12941             nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
12942             nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
12943             nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
12944             nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
12945             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
12946                     tcp->payload_len, tcp->payload) ||
12947             nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
12948                         tcp->data_interval) ||
12949             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
12950                     tcp->wake_len, tcp->wake_data) ||
12951             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
12952                     DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
12953                 return -ENOBUFS;
12954
12955         if (tcp->payload_seq.len &&
12956             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
12957                     sizeof(tcp->payload_seq), &tcp->payload_seq))
12958                 return -ENOBUFS;
12959
12960         if (tcp->payload_tok.len &&
12961             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
12962                     sizeof(tcp->payload_tok) + tcp->tokens_size,
12963                     &tcp->payload_tok))
12964                 return -ENOBUFS;
12965
12966         nla_nest_end(msg, nl_tcp);
12967
12968         return 0;
12969 }
12970
12971 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
12972                                   struct cfg80211_sched_scan_request *req)
12973 {
12974         struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
12975         int i;
12976
12977         if (!req)
12978                 return 0;
12979
12980         nd = nla_nest_start_noflag(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
12981         if (!nd)
12982                 return -ENOBUFS;
12983
12984         if (req->n_scan_plans == 1 &&
12985             nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
12986                         req->scan_plans[0].interval * 1000))
12987                 return -ENOBUFS;
12988
12989         if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
12990                 return -ENOBUFS;
12991
12992         if (req->relative_rssi_set) {
12993                 struct nl80211_bss_select_rssi_adjust rssi_adjust;
12994
12995                 if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
12996                                req->relative_rssi))
12997                         return -ENOBUFS;
12998
12999                 rssi_adjust.band = req->rssi_adjust.band;
13000                 rssi_adjust.delta = req->rssi_adjust.delta;
13001                 if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
13002                             sizeof(rssi_adjust), &rssi_adjust))
13003                         return -ENOBUFS;
13004         }
13005
13006         freqs = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13007         if (!freqs)
13008                 return -ENOBUFS;
13009
13010         for (i = 0; i < req->n_channels; i++) {
13011                 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13012                         return -ENOBUFS;
13013         }
13014
13015         nla_nest_end(msg, freqs);
13016
13017         if (req->n_match_sets) {
13018                 matches = nla_nest_start_noflag(msg,
13019                                                 NL80211_ATTR_SCHED_SCAN_MATCH);
13020                 if (!matches)
13021                         return -ENOBUFS;
13022
13023                 for (i = 0; i < req->n_match_sets; i++) {
13024                         match = nla_nest_start_noflag(msg, i);
13025                         if (!match)
13026                                 return -ENOBUFS;
13027
13028                         if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
13029                                     req->match_sets[i].ssid.ssid_len,
13030                                     req->match_sets[i].ssid.ssid))
13031                                 return -ENOBUFS;
13032                         nla_nest_end(msg, match);
13033                 }
13034                 nla_nest_end(msg, matches);
13035         }
13036
13037         scan_plans = nla_nest_start_noflag(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
13038         if (!scan_plans)
13039                 return -ENOBUFS;
13040
13041         for (i = 0; i < req->n_scan_plans; i++) {
13042                 scan_plan = nla_nest_start_noflag(msg, i + 1);
13043                 if (!scan_plan)
13044                         return -ENOBUFS;
13045
13046                 if (nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
13047                                 req->scan_plans[i].interval) ||
13048                     (req->scan_plans[i].iterations &&
13049                      nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
13050                                  req->scan_plans[i].iterations)))
13051                         return -ENOBUFS;
13052                 nla_nest_end(msg, scan_plan);
13053         }
13054         nla_nest_end(msg, scan_plans);
13055
13056         nla_nest_end(msg, nd);
13057
13058         return 0;
13059 }
13060
13061 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
13062 {
13063         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13064         struct sk_buff *msg;
13065         void *hdr;
13066         u32 size = NLMSG_DEFAULT_SIZE;
13067
13068         if (!rdev->wiphy.wowlan)
13069                 return -EOPNOTSUPP;
13070
13071         if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
13072                 /* adjust size to have room for all the data */
13073                 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
13074                         rdev->wiphy.wowlan_config->tcp->payload_len +
13075                         rdev->wiphy.wowlan_config->tcp->wake_len +
13076                         rdev->wiphy.wowlan_config->tcp->wake_len / 8;
13077         }
13078
13079         msg = nlmsg_new(size, GFP_KERNEL);
13080         if (!msg)
13081                 return -ENOMEM;
13082
13083         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13084                              NL80211_CMD_GET_WOWLAN);
13085         if (!hdr)
13086                 goto nla_put_failure;
13087
13088         if (rdev->wiphy.wowlan_config) {
13089                 struct nlattr *nl_wowlan;
13090
13091                 nl_wowlan = nla_nest_start_noflag(msg,
13092                                                   NL80211_ATTR_WOWLAN_TRIGGERS);
13093                 if (!nl_wowlan)
13094                         goto nla_put_failure;
13095
13096                 if ((rdev->wiphy.wowlan_config->any &&
13097                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
13098                     (rdev->wiphy.wowlan_config->disconnect &&
13099                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
13100                     (rdev->wiphy.wowlan_config->magic_pkt &&
13101                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
13102                     (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
13103                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
13104                     (rdev->wiphy.wowlan_config->eap_identity_req &&
13105                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
13106                     (rdev->wiphy.wowlan_config->four_way_handshake &&
13107                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
13108                     (rdev->wiphy.wowlan_config->rfkill_release &&
13109                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
13110                         goto nla_put_failure;
13111
13112                 if (nl80211_send_wowlan_patterns(msg, rdev))
13113                         goto nla_put_failure;
13114
13115                 if (nl80211_send_wowlan_tcp(msg,
13116                                             rdev->wiphy.wowlan_config->tcp))
13117                         goto nla_put_failure;
13118
13119                 if (nl80211_send_wowlan_nd(
13120                             msg,
13121                             rdev->wiphy.wowlan_config->nd_config))
13122                         goto nla_put_failure;
13123
13124                 nla_nest_end(msg, nl_wowlan);
13125         }
13126
13127         genlmsg_end(msg, hdr);
13128         return genlmsg_reply(msg, info);
13129
13130 nla_put_failure:
13131         nlmsg_free(msg);
13132         return -ENOBUFS;
13133 }
13134
13135 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
13136                                     struct nlattr *attr,
13137                                     struct cfg80211_wowlan *trig)
13138 {
13139         struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
13140         struct cfg80211_wowlan_tcp *cfg;
13141         struct nl80211_wowlan_tcp_data_token *tok = NULL;
13142         struct nl80211_wowlan_tcp_data_seq *seq = NULL;
13143         u32 size;
13144         u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
13145         int err, port;
13146
13147         if (!rdev->wiphy.wowlan->tcp)
13148                 return -EINVAL;
13149
13150         err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TCP, attr,
13151                                           nl80211_wowlan_tcp_policy, NULL);
13152         if (err)
13153                 return err;
13154
13155         if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
13156             !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
13157             !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
13158             !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
13159             !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
13160             !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
13161             !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
13162             !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
13163                 return -EINVAL;
13164
13165         data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
13166         if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
13167                 return -EINVAL;
13168
13169         if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
13170                         rdev->wiphy.wowlan->tcp->data_interval_max ||
13171             nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
13172                 return -EINVAL;
13173
13174         wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
13175         if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
13176                 return -EINVAL;
13177
13178         wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
13179         if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
13180                 return -EINVAL;
13181
13182         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
13183                 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13184
13185                 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
13186                 tokens_size = tokln - sizeof(*tok);
13187
13188                 if (!tok->len || tokens_size % tok->len)
13189                         return -EINVAL;
13190                 if (!rdev->wiphy.wowlan->tcp->tok)
13191                         return -EINVAL;
13192                 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
13193                         return -EINVAL;
13194                 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
13195                         return -EINVAL;
13196                 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
13197                         return -EINVAL;
13198                 if (tok->offset + tok->len > data_size)
13199                         return -EINVAL;
13200         }
13201
13202         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
13203                 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
13204                 if (!rdev->wiphy.wowlan->tcp->seq)
13205                         return -EINVAL;
13206                 if (seq->len == 0 || seq->len > 4)
13207                         return -EINVAL;
13208                 if (seq->len + seq->offset > data_size)
13209                         return -EINVAL;
13210         }
13211
13212         size = sizeof(*cfg);
13213         size += data_size;
13214         size += wake_size + wake_mask_size;
13215         size += tokens_size;
13216
13217         cfg = kzalloc(size, GFP_KERNEL);
13218         if (!cfg)
13219                 return -ENOMEM;
13220         cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
13221         cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
13222         memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
13223                ETH_ALEN);
13224         if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
13225                 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
13226         else
13227                 port = 0;
13228 #ifdef CONFIG_INET
13229         /* allocate a socket and port for it and use it */
13230         err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
13231                             IPPROTO_TCP, &cfg->sock, 1);
13232         if (err) {
13233                 kfree(cfg);
13234                 return err;
13235         }
13236         if (inet_csk_get_port(cfg->sock->sk, port)) {
13237                 sock_release(cfg->sock);
13238                 kfree(cfg);
13239                 return -EADDRINUSE;
13240         }
13241         cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
13242 #else
13243         if (!port) {
13244                 kfree(cfg);
13245                 return -EINVAL;
13246         }
13247         cfg->src_port = port;
13248 #endif
13249
13250         cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
13251         cfg->payload_len = data_size;
13252         cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
13253         memcpy((void *)cfg->payload,
13254                nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
13255                data_size);
13256         if (seq)
13257                 cfg->payload_seq = *seq;
13258         cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
13259         cfg->wake_len = wake_size;
13260         cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
13261         memcpy((void *)cfg->wake_data,
13262                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
13263                wake_size);
13264         cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
13265                          data_size + wake_size;
13266         memcpy((void *)cfg->wake_mask,
13267                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
13268                wake_mask_size);
13269         if (tok) {
13270                 cfg->tokens_size = tokens_size;
13271                 cfg->payload_tok = *tok;
13272                 memcpy(cfg->payload_tok.token_stream, tok->token_stream,
13273                        tokens_size);
13274         }
13275
13276         trig->tcp = cfg;
13277
13278         return 0;
13279 }
13280
13281 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
13282                                    const struct wiphy_wowlan_support *wowlan,
13283                                    struct nlattr *attr,
13284                                    struct cfg80211_wowlan *trig)
13285 {
13286         struct nlattr **tb;
13287         int err;
13288
13289         tb = kcalloc(NUM_NL80211_ATTR, sizeof(*tb), GFP_KERNEL);
13290         if (!tb)
13291                 return -ENOMEM;
13292
13293         if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
13294                 err = -EOPNOTSUPP;
13295                 goto out;
13296         }
13297
13298         err = nla_parse_nested_deprecated(tb, NL80211_ATTR_MAX, attr,
13299                                           nl80211_policy, NULL);
13300         if (err)
13301                 goto out;
13302
13303         trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
13304                                                    wowlan->max_nd_match_sets);
13305         err = PTR_ERR_OR_ZERO(trig->nd_config);
13306         if (err)
13307                 trig->nd_config = NULL;
13308
13309 out:
13310         kfree(tb);
13311         return err;
13312 }
13313
13314 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
13315 {
13316         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13317         struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
13318         struct cfg80211_wowlan new_triggers = {};
13319         struct cfg80211_wowlan *ntrig;
13320         const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
13321         int err, i;
13322         bool prev_enabled = rdev->wiphy.wowlan_config;
13323         bool regular = false;
13324
13325         if (!wowlan)
13326                 return -EOPNOTSUPP;
13327
13328         if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
13329                 cfg80211_rdev_free_wowlan(rdev);
13330                 rdev->wiphy.wowlan_config = NULL;
13331                 goto set_wakeup;
13332         }
13333
13334         err = nla_parse_nested_deprecated(tb, MAX_NL80211_WOWLAN_TRIG,
13335                                           info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
13336                                           nl80211_wowlan_policy, info->extack);
13337         if (err)
13338                 return err;
13339
13340         if (tb[NL80211_WOWLAN_TRIG_ANY]) {
13341                 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
13342                         return -EINVAL;
13343                 new_triggers.any = true;
13344         }
13345
13346         if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
13347                 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
13348                         return -EINVAL;
13349                 new_triggers.disconnect = true;
13350                 regular = true;
13351         }
13352
13353         if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
13354                 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
13355                         return -EINVAL;
13356                 new_triggers.magic_pkt = true;
13357                 regular = true;
13358         }
13359
13360         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
13361                 return -EINVAL;
13362
13363         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
13364                 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
13365                         return -EINVAL;
13366                 new_triggers.gtk_rekey_failure = true;
13367                 regular = true;
13368         }
13369
13370         if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
13371                 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
13372                         return -EINVAL;
13373                 new_triggers.eap_identity_req = true;
13374                 regular = true;
13375         }
13376
13377         if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
13378                 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
13379                         return -EINVAL;
13380                 new_triggers.four_way_handshake = true;
13381                 regular = true;
13382         }
13383
13384         if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
13385                 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
13386                         return -EINVAL;
13387                 new_triggers.rfkill_release = true;
13388                 regular = true;
13389         }
13390
13391         if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
13392                 struct nlattr *pat;
13393                 int n_patterns = 0;
13394                 int rem, pat_len, mask_len, pkt_offset;
13395                 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13396
13397                 regular = true;
13398
13399                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13400                                     rem)
13401                         n_patterns++;
13402                 if (n_patterns > wowlan->n_patterns)
13403                         return -EINVAL;
13404
13405                 new_triggers.patterns = kcalloc(n_patterns,
13406                                                 sizeof(new_triggers.patterns[0]),
13407                                                 GFP_KERNEL);
13408                 if (!new_triggers.patterns)
13409                         return -ENOMEM;
13410
13411                 new_triggers.n_patterns = n_patterns;
13412                 i = 0;
13413
13414                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
13415                                     rem) {
13416                         u8 *mask_pat;
13417
13418                         err = nla_parse_nested_deprecated(pat_tb,
13419                                                           MAX_NL80211_PKTPAT,
13420                                                           pat,
13421                                                           nl80211_packet_pattern_policy,
13422                                                           info->extack);
13423                         if (err)
13424                                 goto error;
13425
13426                         err = -EINVAL;
13427                         if (!pat_tb[NL80211_PKTPAT_MASK] ||
13428                             !pat_tb[NL80211_PKTPAT_PATTERN])
13429                                 goto error;
13430                         pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13431                         mask_len = DIV_ROUND_UP(pat_len, 8);
13432                         if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13433                                 goto error;
13434                         if (pat_len > wowlan->pattern_max_len ||
13435                             pat_len < wowlan->pattern_min_len)
13436                                 goto error;
13437
13438                         if (!pat_tb[NL80211_PKTPAT_OFFSET])
13439                                 pkt_offset = 0;
13440                         else
13441                                 pkt_offset = nla_get_u32(
13442                                         pat_tb[NL80211_PKTPAT_OFFSET]);
13443                         if (pkt_offset > wowlan->max_pkt_offset)
13444                                 goto error;
13445                         new_triggers.patterns[i].pkt_offset = pkt_offset;
13446
13447                         mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13448                         if (!mask_pat) {
13449                                 err = -ENOMEM;
13450                                 goto error;
13451                         }
13452                         new_triggers.patterns[i].mask = mask_pat;
13453                         memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13454                                mask_len);
13455                         mask_pat += mask_len;
13456                         new_triggers.patterns[i].pattern = mask_pat;
13457                         new_triggers.patterns[i].pattern_len = pat_len;
13458                         memcpy(mask_pat,
13459                                nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13460                                pat_len);
13461                         i++;
13462                 }
13463         }
13464
13465         if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
13466                 regular = true;
13467                 err = nl80211_parse_wowlan_tcp(
13468                         rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
13469                         &new_triggers);
13470                 if (err)
13471                         goto error;
13472         }
13473
13474         if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
13475                 regular = true;
13476                 err = nl80211_parse_wowlan_nd(
13477                         rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
13478                         &new_triggers);
13479                 if (err)
13480                         goto error;
13481         }
13482
13483         /* The 'any' trigger means the device continues operating more or less
13484          * as in its normal operation mode and wakes up the host on most of the
13485          * normal interrupts (like packet RX, ...)
13486          * It therefore makes little sense to combine with the more constrained
13487          * wakeup trigger modes.
13488          */
13489         if (new_triggers.any && regular) {
13490                 err = -EINVAL;
13491                 goto error;
13492         }
13493
13494         ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
13495         if (!ntrig) {
13496                 err = -ENOMEM;
13497                 goto error;
13498         }
13499         cfg80211_rdev_free_wowlan(rdev);
13500         rdev->wiphy.wowlan_config = ntrig;
13501
13502  set_wakeup:
13503         if (rdev->ops->set_wakeup &&
13504             prev_enabled != !!rdev->wiphy.wowlan_config)
13505                 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
13506
13507         return 0;
13508  error:
13509         for (i = 0; i < new_triggers.n_patterns; i++)
13510                 kfree(new_triggers.patterns[i].mask);
13511         kfree(new_triggers.patterns);
13512         if (new_triggers.tcp && new_triggers.tcp->sock)
13513                 sock_release(new_triggers.tcp->sock);
13514         kfree(new_triggers.tcp);
13515         kfree(new_triggers.nd_config);
13516         return err;
13517 }
13518 #endif
13519
13520 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
13521                                        struct cfg80211_registered_device *rdev)
13522 {
13523         struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
13524         int i, j, pat_len;
13525         struct cfg80211_coalesce_rules *rule;
13526
13527         if (!rdev->coalesce->n_rules)
13528                 return 0;
13529
13530         nl_rules = nla_nest_start_noflag(msg, NL80211_ATTR_COALESCE_RULE);
13531         if (!nl_rules)
13532                 return -ENOBUFS;
13533
13534         for (i = 0; i < rdev->coalesce->n_rules; i++) {
13535                 nl_rule = nla_nest_start_noflag(msg, i + 1);
13536                 if (!nl_rule)
13537                         return -ENOBUFS;
13538
13539                 rule = &rdev->coalesce->rules[i];
13540                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
13541                                 rule->delay))
13542                         return -ENOBUFS;
13543
13544                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
13545                                 rule->condition))
13546                         return -ENOBUFS;
13547
13548                 nl_pats = nla_nest_start_noflag(msg,
13549                                                 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
13550                 if (!nl_pats)
13551                         return -ENOBUFS;
13552
13553                 for (j = 0; j < rule->n_patterns; j++) {
13554                         nl_pat = nla_nest_start_noflag(msg, j + 1);
13555                         if (!nl_pat)
13556                                 return -ENOBUFS;
13557                         pat_len = rule->patterns[j].pattern_len;
13558                         if (nla_put(msg, NL80211_PKTPAT_MASK,
13559                                     DIV_ROUND_UP(pat_len, 8),
13560                                     rule->patterns[j].mask) ||
13561                             nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
13562                                     rule->patterns[j].pattern) ||
13563                             nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
13564                                         rule->patterns[j].pkt_offset))
13565                                 return -ENOBUFS;
13566                         nla_nest_end(msg, nl_pat);
13567                 }
13568                 nla_nest_end(msg, nl_pats);
13569                 nla_nest_end(msg, nl_rule);
13570         }
13571         nla_nest_end(msg, nl_rules);
13572
13573         return 0;
13574 }
13575
13576 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
13577 {
13578         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13579         struct sk_buff *msg;
13580         void *hdr;
13581
13582         if (!rdev->wiphy.coalesce)
13583                 return -EOPNOTSUPP;
13584
13585         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13586         if (!msg)
13587                 return -ENOMEM;
13588
13589         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13590                              NL80211_CMD_GET_COALESCE);
13591         if (!hdr)
13592                 goto nla_put_failure;
13593
13594         if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
13595                 goto nla_put_failure;
13596
13597         genlmsg_end(msg, hdr);
13598         return genlmsg_reply(msg, info);
13599
13600 nla_put_failure:
13601         nlmsg_free(msg);
13602         return -ENOBUFS;
13603 }
13604
13605 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
13606 {
13607         struct cfg80211_coalesce *coalesce = rdev->coalesce;
13608         int i, j;
13609         struct cfg80211_coalesce_rules *rule;
13610
13611         if (!coalesce)
13612                 return;
13613
13614         for (i = 0; i < coalesce->n_rules; i++) {
13615                 rule = &coalesce->rules[i];
13616                 for (j = 0; j < rule->n_patterns; j++)
13617                         kfree(rule->patterns[j].mask);
13618                 kfree(rule->patterns);
13619         }
13620         kfree(coalesce->rules);
13621         kfree(coalesce);
13622         rdev->coalesce = NULL;
13623 }
13624
13625 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
13626                                        struct nlattr *rule,
13627                                        struct cfg80211_coalesce_rules *new_rule)
13628 {
13629         int err, i;
13630         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13631         struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
13632         int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
13633         struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
13634
13635         err = nla_parse_nested_deprecated(tb, NL80211_ATTR_COALESCE_RULE_MAX,
13636                                           rule, nl80211_coalesce_policy, NULL);
13637         if (err)
13638                 return err;
13639
13640         if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
13641                 new_rule->delay =
13642                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
13643         if (new_rule->delay > coalesce->max_delay)
13644                 return -EINVAL;
13645
13646         if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
13647                 new_rule->condition =
13648                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
13649
13650         if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
13651                 return -EINVAL;
13652
13653         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13654                             rem)
13655                 n_patterns++;
13656         if (n_patterns > coalesce->n_patterns)
13657                 return -EINVAL;
13658
13659         new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
13660                                      GFP_KERNEL);
13661         if (!new_rule->patterns)
13662                 return -ENOMEM;
13663
13664         new_rule->n_patterns = n_patterns;
13665         i = 0;
13666
13667         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
13668                             rem) {
13669                 u8 *mask_pat;
13670
13671                 err = nla_parse_nested_deprecated(pat_tb, MAX_NL80211_PKTPAT,
13672                                                   pat,
13673                                                   nl80211_packet_pattern_policy,
13674                                                   NULL);
13675                 if (err)
13676                         return err;
13677
13678                 if (!pat_tb[NL80211_PKTPAT_MASK] ||
13679                     !pat_tb[NL80211_PKTPAT_PATTERN])
13680                         return -EINVAL;
13681                 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
13682                 mask_len = DIV_ROUND_UP(pat_len, 8);
13683                 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
13684                         return -EINVAL;
13685                 if (pat_len > coalesce->pattern_max_len ||
13686                     pat_len < coalesce->pattern_min_len)
13687                         return -EINVAL;
13688
13689                 if (!pat_tb[NL80211_PKTPAT_OFFSET])
13690                         pkt_offset = 0;
13691                 else
13692                         pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
13693                 if (pkt_offset > coalesce->max_pkt_offset)
13694                         return -EINVAL;
13695                 new_rule->patterns[i].pkt_offset = pkt_offset;
13696
13697                 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
13698                 if (!mask_pat)
13699                         return -ENOMEM;
13700
13701                 new_rule->patterns[i].mask = mask_pat;
13702                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
13703                        mask_len);
13704
13705                 mask_pat += mask_len;
13706                 new_rule->patterns[i].pattern = mask_pat;
13707                 new_rule->patterns[i].pattern_len = pat_len;
13708                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
13709                        pat_len);
13710                 i++;
13711         }
13712
13713         return 0;
13714 }
13715
13716 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
13717 {
13718         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13719         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
13720         struct cfg80211_coalesce new_coalesce = {};
13721         struct cfg80211_coalesce *n_coalesce;
13722         int err, rem_rule, n_rules = 0, i, j;
13723         struct nlattr *rule;
13724         struct cfg80211_coalesce_rules *tmp_rule;
13725
13726         if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
13727                 return -EOPNOTSUPP;
13728
13729         if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
13730                 cfg80211_rdev_free_coalesce(rdev);
13731                 rdev_set_coalesce(rdev, NULL);
13732                 return 0;
13733         }
13734
13735         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13736                             rem_rule)
13737                 n_rules++;
13738         if (n_rules > coalesce->n_rules)
13739                 return -EINVAL;
13740
13741         new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
13742                                      GFP_KERNEL);
13743         if (!new_coalesce.rules)
13744                 return -ENOMEM;
13745
13746         new_coalesce.n_rules = n_rules;
13747         i = 0;
13748
13749         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
13750                             rem_rule) {
13751                 err = nl80211_parse_coalesce_rule(rdev, rule,
13752                                                   &new_coalesce.rules[i]);
13753                 if (err)
13754                         goto error;
13755
13756                 i++;
13757         }
13758
13759         err = rdev_set_coalesce(rdev, &new_coalesce);
13760         if (err)
13761                 goto error;
13762
13763         n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
13764         if (!n_coalesce) {
13765                 err = -ENOMEM;
13766                 goto error;
13767         }
13768         cfg80211_rdev_free_coalesce(rdev);
13769         rdev->coalesce = n_coalesce;
13770
13771         return 0;
13772 error:
13773         for (i = 0; i < new_coalesce.n_rules; i++) {
13774                 tmp_rule = &new_coalesce.rules[i];
13775                 for (j = 0; j < tmp_rule->n_patterns; j++)
13776                         kfree(tmp_rule->patterns[j].mask);
13777                 kfree(tmp_rule->patterns);
13778         }
13779         kfree(new_coalesce.rules);
13780
13781         return err;
13782 }
13783
13784 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
13785 {
13786         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13787         struct net_device *dev = info->user_ptr[1];
13788         struct wireless_dev *wdev = dev->ieee80211_ptr;
13789         struct nlattr *tb[NUM_NL80211_REKEY_DATA];
13790         struct cfg80211_gtk_rekey_data rekey_data = {};
13791         int err;
13792
13793         if (!info->attrs[NL80211_ATTR_REKEY_DATA])
13794                 return -EINVAL;
13795
13796         err = nla_parse_nested_deprecated(tb, MAX_NL80211_REKEY_DATA,
13797                                           info->attrs[NL80211_ATTR_REKEY_DATA],
13798                                           nl80211_rekey_policy, info->extack);
13799         if (err)
13800                 return err;
13801
13802         if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
13803             !tb[NL80211_REKEY_DATA_KCK])
13804                 return -EINVAL;
13805         if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN &&
13806             !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13807               nla_len(tb[NL80211_REKEY_DATA_KEK]) == NL80211_KEK_EXT_LEN))
13808                 return -ERANGE;
13809         if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN &&
13810             !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK &&
13811               nla_len(tb[NL80211_REKEY_DATA_KCK]) == NL80211_KCK_EXT_LEN))
13812                 return -ERANGE;
13813
13814         rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
13815         rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
13816         rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
13817         rekey_data.kek_len = nla_len(tb[NL80211_REKEY_DATA_KEK]);
13818         rekey_data.kck_len = nla_len(tb[NL80211_REKEY_DATA_KCK]);
13819         if (tb[NL80211_REKEY_DATA_AKM])
13820                 rekey_data.akm = nla_get_u32(tb[NL80211_REKEY_DATA_AKM]);
13821
13822         wdev_lock(wdev);
13823         if (!wdev->connected) {
13824                 err = -ENOTCONN;
13825                 goto out;
13826         }
13827
13828         if (!rdev->ops->set_rekey_data) {
13829                 err = -EOPNOTSUPP;
13830                 goto out;
13831         }
13832
13833         err = rdev_set_rekey_data(rdev, dev, &rekey_data);
13834  out:
13835         wdev_unlock(wdev);
13836         return err;
13837 }
13838
13839 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
13840                                              struct genl_info *info)
13841 {
13842         struct net_device *dev = info->user_ptr[1];
13843         struct wireless_dev *wdev = dev->ieee80211_ptr;
13844
13845         if (wdev->iftype != NL80211_IFTYPE_AP &&
13846             wdev->iftype != NL80211_IFTYPE_P2P_GO)
13847                 return -EINVAL;
13848
13849         if (wdev->ap_unexpected_nlportid)
13850                 return -EBUSY;
13851
13852         wdev->ap_unexpected_nlportid = info->snd_portid;
13853         return 0;
13854 }
13855
13856 static int nl80211_probe_client(struct sk_buff *skb,
13857                                 struct genl_info *info)
13858 {
13859         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13860         struct net_device *dev = info->user_ptr[1];
13861         struct wireless_dev *wdev = dev->ieee80211_ptr;
13862         struct sk_buff *msg;
13863         void *hdr;
13864         const u8 *addr;
13865         u64 cookie;
13866         int err;
13867
13868         if (wdev->iftype != NL80211_IFTYPE_AP &&
13869             wdev->iftype != NL80211_IFTYPE_P2P_GO)
13870                 return -EOPNOTSUPP;
13871
13872         if (!info->attrs[NL80211_ATTR_MAC])
13873                 return -EINVAL;
13874
13875         if (!rdev->ops->probe_client)
13876                 return -EOPNOTSUPP;
13877
13878         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13879         if (!msg)
13880                 return -ENOMEM;
13881
13882         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
13883                              NL80211_CMD_PROBE_CLIENT);
13884         if (!hdr) {
13885                 err = -ENOBUFS;
13886                 goto free_msg;
13887         }
13888
13889         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
13890
13891         err = rdev_probe_client(rdev, dev, addr, &cookie);
13892         if (err)
13893                 goto free_msg;
13894
13895         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
13896                               NL80211_ATTR_PAD))
13897                 goto nla_put_failure;
13898
13899         genlmsg_end(msg, hdr);
13900
13901         return genlmsg_reply(msg, info);
13902
13903  nla_put_failure:
13904         err = -ENOBUFS;
13905  free_msg:
13906         nlmsg_free(msg);
13907         return err;
13908 }
13909
13910 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
13911 {
13912         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13913         struct cfg80211_beacon_registration *reg, *nreg;
13914         int rv;
13915
13916         if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
13917                 return -EOPNOTSUPP;
13918
13919         nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
13920         if (!nreg)
13921                 return -ENOMEM;
13922
13923         /* First, check if already registered. */
13924         spin_lock_bh(&rdev->beacon_registrations_lock);
13925         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
13926                 if (reg->nlportid == info->snd_portid) {
13927                         rv = -EALREADY;
13928                         goto out_err;
13929                 }
13930         }
13931         /* Add it to the list */
13932         nreg->nlportid = info->snd_portid;
13933         list_add(&nreg->list, &rdev->beacon_registrations);
13934
13935         spin_unlock_bh(&rdev->beacon_registrations_lock);
13936
13937         return 0;
13938 out_err:
13939         spin_unlock_bh(&rdev->beacon_registrations_lock);
13940         kfree(nreg);
13941         return rv;
13942 }
13943
13944 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
13945 {
13946         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13947         struct wireless_dev *wdev = info->user_ptr[1];
13948         int err;
13949
13950         if (!rdev->ops->start_p2p_device)
13951                 return -EOPNOTSUPP;
13952
13953         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13954                 return -EOPNOTSUPP;
13955
13956         if (wdev_running(wdev))
13957                 return 0;
13958
13959         if (rfkill_blocked(rdev->wiphy.rfkill))
13960                 return -ERFKILL;
13961
13962         err = rdev_start_p2p_device(rdev, wdev);
13963         if (err)
13964                 return err;
13965
13966         wdev->is_running = true;
13967         rdev->opencount++;
13968
13969         return 0;
13970 }
13971
13972 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
13973 {
13974         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13975         struct wireless_dev *wdev = info->user_ptr[1];
13976
13977         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
13978                 return -EOPNOTSUPP;
13979
13980         if (!rdev->ops->stop_p2p_device)
13981                 return -EOPNOTSUPP;
13982
13983         cfg80211_stop_p2p_device(rdev, wdev);
13984
13985         return 0;
13986 }
13987
13988 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
13989 {
13990         struct cfg80211_registered_device *rdev = info->user_ptr[0];
13991         struct wireless_dev *wdev = info->user_ptr[1];
13992         struct cfg80211_nan_conf conf = {};
13993         int err;
13994
13995         if (wdev->iftype != NL80211_IFTYPE_NAN)
13996                 return -EOPNOTSUPP;
13997
13998         if (wdev_running(wdev))
13999                 return -EEXIST;
14000
14001         if (rfkill_blocked(rdev->wiphy.rfkill))
14002                 return -ERFKILL;
14003
14004         if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
14005                 return -EINVAL;
14006
14007         conf.master_pref =
14008                 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14009
14010         if (info->attrs[NL80211_ATTR_BANDS]) {
14011                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14012
14013                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14014                         return -EOPNOTSUPP;
14015
14016                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14017                         return -EINVAL;
14018
14019                 conf.bands = bands;
14020         }
14021
14022         err = rdev_start_nan(rdev, wdev, &conf);
14023         if (err)
14024                 return err;
14025
14026         wdev->is_running = true;
14027         rdev->opencount++;
14028
14029         return 0;
14030 }
14031
14032 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
14033 {
14034         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14035         struct wireless_dev *wdev = info->user_ptr[1];
14036
14037         if (wdev->iftype != NL80211_IFTYPE_NAN)
14038                 return -EOPNOTSUPP;
14039
14040         cfg80211_stop_nan(rdev, wdev);
14041
14042         return 0;
14043 }
14044
14045 static int validate_nan_filter(struct nlattr *filter_attr)
14046 {
14047         struct nlattr *attr;
14048         int len = 0, n_entries = 0, rem;
14049
14050         nla_for_each_nested(attr, filter_attr, rem) {
14051                 len += nla_len(attr);
14052                 n_entries++;
14053         }
14054
14055         if (len >= U8_MAX)
14056                 return -EINVAL;
14057
14058         return n_entries;
14059 }
14060
14061 static int handle_nan_filter(struct nlattr *attr_filter,
14062                              struct cfg80211_nan_func *func,
14063                              bool tx)
14064 {
14065         struct nlattr *attr;
14066         int n_entries, rem, i;
14067         struct cfg80211_nan_func_filter *filter;
14068
14069         n_entries = validate_nan_filter(attr_filter);
14070         if (n_entries < 0)
14071                 return n_entries;
14072
14073         BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
14074
14075         filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
14076         if (!filter)
14077                 return -ENOMEM;
14078
14079         i = 0;
14080         nla_for_each_nested(attr, attr_filter, rem) {
14081                 filter[i].filter = nla_memdup(attr, GFP_KERNEL);
14082                 if (!filter[i].filter)
14083                         goto err;
14084
14085                 filter[i].len = nla_len(attr);
14086                 i++;
14087         }
14088         if (tx) {
14089                 func->num_tx_filters = n_entries;
14090                 func->tx_filters = filter;
14091         } else {
14092                 func->num_rx_filters = n_entries;
14093                 func->rx_filters = filter;
14094         }
14095
14096         return 0;
14097
14098 err:
14099         i = 0;
14100         nla_for_each_nested(attr, attr_filter, rem) {
14101                 kfree(filter[i].filter);
14102                 i++;
14103         }
14104         kfree(filter);
14105         return -ENOMEM;
14106 }
14107
14108 static int nl80211_nan_add_func(struct sk_buff *skb,
14109                                 struct genl_info *info)
14110 {
14111         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14112         struct wireless_dev *wdev = info->user_ptr[1];
14113         struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
14114         struct cfg80211_nan_func *func;
14115         struct sk_buff *msg = NULL;
14116         void *hdr = NULL;
14117         int err = 0;
14118
14119         if (wdev->iftype != NL80211_IFTYPE_NAN)
14120                 return -EOPNOTSUPP;
14121
14122         if (!wdev_running(wdev))
14123                 return -ENOTCONN;
14124
14125         if (!info->attrs[NL80211_ATTR_NAN_FUNC])
14126                 return -EINVAL;
14127
14128         err = nla_parse_nested_deprecated(tb, NL80211_NAN_FUNC_ATTR_MAX,
14129                                           info->attrs[NL80211_ATTR_NAN_FUNC],
14130                                           nl80211_nan_func_policy,
14131                                           info->extack);
14132         if (err)
14133                 return err;
14134
14135         func = kzalloc(sizeof(*func), GFP_KERNEL);
14136         if (!func)
14137                 return -ENOMEM;
14138
14139         func->cookie = cfg80211_assign_cookie(rdev);
14140
14141         if (!tb[NL80211_NAN_FUNC_TYPE]) {
14142                 err = -EINVAL;
14143                 goto out;
14144         }
14145
14146
14147         func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
14148
14149         if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
14150                 err = -EINVAL;
14151                 goto out;
14152         }
14153
14154         memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
14155                sizeof(func->service_id));
14156
14157         func->close_range =
14158                 nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
14159
14160         if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
14161                 func->serv_spec_info_len =
14162                         nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
14163                 func->serv_spec_info =
14164                         kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
14165                                 func->serv_spec_info_len,
14166                                 GFP_KERNEL);
14167                 if (!func->serv_spec_info) {
14168                         err = -ENOMEM;
14169                         goto out;
14170                 }
14171         }
14172
14173         if (tb[NL80211_NAN_FUNC_TTL])
14174                 func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
14175
14176         switch (func->type) {
14177         case NL80211_NAN_FUNC_PUBLISH:
14178                 if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
14179                         err = -EINVAL;
14180                         goto out;
14181                 }
14182
14183                 func->publish_type =
14184                         nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
14185                 func->publish_bcast =
14186                         nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
14187
14188                 if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
14189                         func->publish_bcast) {
14190                         err = -EINVAL;
14191                         goto out;
14192                 }
14193                 break;
14194         case NL80211_NAN_FUNC_SUBSCRIBE:
14195                 func->subscribe_active =
14196                         nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
14197                 break;
14198         case NL80211_NAN_FUNC_FOLLOW_UP:
14199                 if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
14200                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
14201                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
14202                         err = -EINVAL;
14203                         goto out;
14204                 }
14205
14206                 func->followup_id =
14207                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
14208                 func->followup_reqid =
14209                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
14210                 memcpy(func->followup_dest.addr,
14211                        nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
14212                        sizeof(func->followup_dest.addr));
14213                 if (func->ttl) {
14214                         err = -EINVAL;
14215                         goto out;
14216                 }
14217                 break;
14218         default:
14219                 err = -EINVAL;
14220                 goto out;
14221         }
14222
14223         if (tb[NL80211_NAN_FUNC_SRF]) {
14224                 struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
14225
14226                 err = nla_parse_nested_deprecated(srf_tb,
14227                                                   NL80211_NAN_SRF_ATTR_MAX,
14228                                                   tb[NL80211_NAN_FUNC_SRF],
14229                                                   nl80211_nan_srf_policy,
14230                                                   info->extack);
14231                 if (err)
14232                         goto out;
14233
14234                 func->srf_include =
14235                         nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
14236
14237                 if (srf_tb[NL80211_NAN_SRF_BF]) {
14238                         if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
14239                             !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
14240                                 err = -EINVAL;
14241                                 goto out;
14242                         }
14243
14244                         func->srf_bf_len =
14245                                 nla_len(srf_tb[NL80211_NAN_SRF_BF]);
14246                         func->srf_bf =
14247                                 kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
14248                                         func->srf_bf_len, GFP_KERNEL);
14249                         if (!func->srf_bf) {
14250                                 err = -ENOMEM;
14251                                 goto out;
14252                         }
14253
14254                         func->srf_bf_idx =
14255                                 nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
14256                 } else {
14257                         struct nlattr *attr, *mac_attr =
14258                                 srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
14259                         int n_entries, rem, i = 0;
14260
14261                         if (!mac_attr) {
14262                                 err = -EINVAL;
14263                                 goto out;
14264                         }
14265
14266                         n_entries = validate_acl_mac_addrs(mac_attr);
14267                         if (n_entries <= 0) {
14268                                 err = -EINVAL;
14269                                 goto out;
14270                         }
14271
14272                         func->srf_num_macs = n_entries;
14273                         func->srf_macs =
14274                                 kcalloc(n_entries, sizeof(*func->srf_macs),
14275                                         GFP_KERNEL);
14276                         if (!func->srf_macs) {
14277                                 err = -ENOMEM;
14278                                 goto out;
14279                         }
14280
14281                         nla_for_each_nested(attr, mac_attr, rem)
14282                                 memcpy(func->srf_macs[i++].addr, nla_data(attr),
14283                                        sizeof(*func->srf_macs));
14284                 }
14285         }
14286
14287         if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
14288                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
14289                                         func, true);
14290                 if (err)
14291                         goto out;
14292         }
14293
14294         if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
14295                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
14296                                         func, false);
14297                 if (err)
14298                         goto out;
14299         }
14300
14301         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14302         if (!msg) {
14303                 err = -ENOMEM;
14304                 goto out;
14305         }
14306
14307         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14308                              NL80211_CMD_ADD_NAN_FUNCTION);
14309         /* This can't really happen - we just allocated 4KB */
14310         if (WARN_ON(!hdr)) {
14311                 err = -ENOMEM;
14312                 goto out;
14313         }
14314
14315         err = rdev_add_nan_func(rdev, wdev, func);
14316 out:
14317         if (err < 0) {
14318                 cfg80211_free_nan_func(func);
14319                 nlmsg_free(msg);
14320                 return err;
14321         }
14322
14323         /* propagate the instance id and cookie to userspace  */
14324         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
14325                               NL80211_ATTR_PAD))
14326                 goto nla_put_failure;
14327
14328         func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14329         if (!func_attr)
14330                 goto nla_put_failure;
14331
14332         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
14333                        func->instance_id))
14334                 goto nla_put_failure;
14335
14336         nla_nest_end(msg, func_attr);
14337
14338         genlmsg_end(msg, hdr);
14339         return genlmsg_reply(msg, info);
14340
14341 nla_put_failure:
14342         nlmsg_free(msg);
14343         return -ENOBUFS;
14344 }
14345
14346 static int nl80211_nan_del_func(struct sk_buff *skb,
14347                                struct genl_info *info)
14348 {
14349         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14350         struct wireless_dev *wdev = info->user_ptr[1];
14351         u64 cookie;
14352
14353         if (wdev->iftype != NL80211_IFTYPE_NAN)
14354                 return -EOPNOTSUPP;
14355
14356         if (!wdev_running(wdev))
14357                 return -ENOTCONN;
14358
14359         if (!info->attrs[NL80211_ATTR_COOKIE])
14360                 return -EINVAL;
14361
14362         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
14363
14364         rdev_del_nan_func(rdev, wdev, cookie);
14365
14366         return 0;
14367 }
14368
14369 static int nl80211_nan_change_config(struct sk_buff *skb,
14370                                      struct genl_info *info)
14371 {
14372         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14373         struct wireless_dev *wdev = info->user_ptr[1];
14374         struct cfg80211_nan_conf conf = {};
14375         u32 changed = 0;
14376
14377         if (wdev->iftype != NL80211_IFTYPE_NAN)
14378                 return -EOPNOTSUPP;
14379
14380         if (!wdev_running(wdev))
14381                 return -ENOTCONN;
14382
14383         if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
14384                 conf.master_pref =
14385                         nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
14386                 if (conf.master_pref <= 1 || conf.master_pref == 255)
14387                         return -EINVAL;
14388
14389                 changed |= CFG80211_NAN_CONF_CHANGED_PREF;
14390         }
14391
14392         if (info->attrs[NL80211_ATTR_BANDS]) {
14393                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
14394
14395                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
14396                         return -EOPNOTSUPP;
14397
14398                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
14399                         return -EINVAL;
14400
14401                 conf.bands = bands;
14402                 changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
14403         }
14404
14405         if (!changed)
14406                 return -EINVAL;
14407
14408         return rdev_nan_change_conf(rdev, wdev, &conf, changed);
14409 }
14410
14411 void cfg80211_nan_match(struct wireless_dev *wdev,
14412                         struct cfg80211_nan_match_params *match, gfp_t gfp)
14413 {
14414         struct wiphy *wiphy = wdev->wiphy;
14415         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14416         struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
14417         struct sk_buff *msg;
14418         void *hdr;
14419
14420         if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
14421                 return;
14422
14423         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14424         if (!msg)
14425                 return;
14426
14427         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
14428         if (!hdr) {
14429                 nlmsg_free(msg);
14430                 return;
14431         }
14432
14433         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14434             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14435                                          wdev->netdev->ifindex)) ||
14436             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14437                               NL80211_ATTR_PAD))
14438                 goto nla_put_failure;
14439
14440         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
14441                               NL80211_ATTR_PAD) ||
14442             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
14443                 goto nla_put_failure;
14444
14445         match_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_MATCH);
14446         if (!match_attr)
14447                 goto nla_put_failure;
14448
14449         local_func_attr = nla_nest_start_noflag(msg,
14450                                                 NL80211_NAN_MATCH_FUNC_LOCAL);
14451         if (!local_func_attr)
14452                 goto nla_put_failure;
14453
14454         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
14455                 goto nla_put_failure;
14456
14457         nla_nest_end(msg, local_func_attr);
14458
14459         peer_func_attr = nla_nest_start_noflag(msg,
14460                                                NL80211_NAN_MATCH_FUNC_PEER);
14461         if (!peer_func_attr)
14462                 goto nla_put_failure;
14463
14464         if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
14465             nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
14466                 goto nla_put_failure;
14467
14468         if (match->info && match->info_len &&
14469             nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
14470                     match->info))
14471                 goto nla_put_failure;
14472
14473         nla_nest_end(msg, peer_func_attr);
14474         nla_nest_end(msg, match_attr);
14475         genlmsg_end(msg, hdr);
14476
14477         if (!wdev->owner_nlportid)
14478                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14479                                         msg, 0, NL80211_MCGRP_NAN, gfp);
14480         else
14481                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14482                                 wdev->owner_nlportid);
14483
14484         return;
14485
14486 nla_put_failure:
14487         nlmsg_free(msg);
14488 }
14489 EXPORT_SYMBOL(cfg80211_nan_match);
14490
14491 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
14492                                   u8 inst_id,
14493                                   enum nl80211_nan_func_term_reason reason,
14494                                   u64 cookie, gfp_t gfp)
14495 {
14496         struct wiphy *wiphy = wdev->wiphy;
14497         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14498         struct sk_buff *msg;
14499         struct nlattr *func_attr;
14500         void *hdr;
14501
14502         if (WARN_ON(!inst_id))
14503                 return;
14504
14505         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14506         if (!msg)
14507                 return;
14508
14509         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
14510         if (!hdr) {
14511                 nlmsg_free(msg);
14512                 return;
14513         }
14514
14515         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14516             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14517                                          wdev->netdev->ifindex)) ||
14518             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14519                               NL80211_ATTR_PAD))
14520                 goto nla_put_failure;
14521
14522         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14523                               NL80211_ATTR_PAD))
14524                 goto nla_put_failure;
14525
14526         func_attr = nla_nest_start_noflag(msg, NL80211_ATTR_NAN_FUNC);
14527         if (!func_attr)
14528                 goto nla_put_failure;
14529
14530         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
14531             nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
14532                 goto nla_put_failure;
14533
14534         nla_nest_end(msg, func_attr);
14535         genlmsg_end(msg, hdr);
14536
14537         if (!wdev->owner_nlportid)
14538                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
14539                                         msg, 0, NL80211_MCGRP_NAN, gfp);
14540         else
14541                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
14542                                 wdev->owner_nlportid);
14543
14544         return;
14545
14546 nla_put_failure:
14547         nlmsg_free(msg);
14548 }
14549 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
14550
14551 static int nl80211_get_protocol_features(struct sk_buff *skb,
14552                                          struct genl_info *info)
14553 {
14554         void *hdr;
14555         struct sk_buff *msg;
14556
14557         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
14558         if (!msg)
14559                 return -ENOMEM;
14560
14561         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
14562                              NL80211_CMD_GET_PROTOCOL_FEATURES);
14563         if (!hdr)
14564                 goto nla_put_failure;
14565
14566         if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
14567                         NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
14568                 goto nla_put_failure;
14569
14570         genlmsg_end(msg, hdr);
14571         return genlmsg_reply(msg, info);
14572
14573  nla_put_failure:
14574         kfree_skb(msg);
14575         return -ENOBUFS;
14576 }
14577
14578 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
14579 {
14580         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14581         struct cfg80211_update_ft_ies_params ft_params;
14582         struct net_device *dev = info->user_ptr[1];
14583
14584         if (!rdev->ops->update_ft_ies)
14585                 return -EOPNOTSUPP;
14586
14587         if (!info->attrs[NL80211_ATTR_MDID] ||
14588             !info->attrs[NL80211_ATTR_IE])
14589                 return -EINVAL;
14590
14591         memset(&ft_params, 0, sizeof(ft_params));
14592         ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
14593         ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
14594         ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
14595
14596         return rdev_update_ft_ies(rdev, dev, &ft_params);
14597 }
14598
14599 static int nl80211_crit_protocol_start(struct sk_buff *skb,
14600                                        struct genl_info *info)
14601 {
14602         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14603         struct wireless_dev *wdev = info->user_ptr[1];
14604         enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
14605         u16 duration;
14606         int ret;
14607
14608         if (!rdev->ops->crit_proto_start)
14609                 return -EOPNOTSUPP;
14610
14611         if (WARN_ON(!rdev->ops->crit_proto_stop))
14612                 return -EINVAL;
14613
14614         if (rdev->crit_proto_nlportid)
14615                 return -EBUSY;
14616
14617         /* determine protocol if provided */
14618         if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
14619                 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
14620
14621         if (proto >= NUM_NL80211_CRIT_PROTO)
14622                 return -EINVAL;
14623
14624         /* timeout must be provided */
14625         if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
14626                 return -EINVAL;
14627
14628         duration =
14629                 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
14630
14631         ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
14632         if (!ret)
14633                 rdev->crit_proto_nlportid = info->snd_portid;
14634
14635         return ret;
14636 }
14637
14638 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
14639                                       struct genl_info *info)
14640 {
14641         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14642         struct wireless_dev *wdev = info->user_ptr[1];
14643
14644         if (!rdev->ops->crit_proto_stop)
14645                 return -EOPNOTSUPP;
14646
14647         if (rdev->crit_proto_nlportid) {
14648                 rdev->crit_proto_nlportid = 0;
14649                 rdev_crit_proto_stop(rdev, wdev);
14650         }
14651         return 0;
14652 }
14653
14654 static int nl80211_vendor_check_policy(const struct wiphy_vendor_command *vcmd,
14655                                        struct nlattr *attr,
14656                                        struct netlink_ext_ack *extack)
14657 {
14658         if (vcmd->policy == VENDOR_CMD_RAW_DATA) {
14659                 if (attr->nla_type & NLA_F_NESTED) {
14660                         NL_SET_ERR_MSG_ATTR(extack, attr,
14661                                             "unexpected nested data");
14662                         return -EINVAL;
14663                 }
14664
14665                 return 0;
14666         }
14667
14668         if (!(attr->nla_type & NLA_F_NESTED)) {
14669                 NL_SET_ERR_MSG_ATTR(extack, attr, "expected nested data");
14670                 return -EINVAL;
14671         }
14672
14673         return nla_validate_nested(attr, vcmd->maxattr, vcmd->policy, extack);
14674 }
14675
14676 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
14677 {
14678         struct cfg80211_registered_device *rdev = info->user_ptr[0];
14679         struct wireless_dev *wdev =
14680                 __cfg80211_wdev_from_attrs(rdev, genl_info_net(info),
14681                                            info->attrs);
14682         int i, err;
14683         u32 vid, subcmd;
14684
14685         if (!rdev->wiphy.vendor_commands)
14686                 return -EOPNOTSUPP;
14687
14688         if (IS_ERR(wdev)) {
14689                 err = PTR_ERR(wdev);
14690                 if (err != -EINVAL)
14691                         return err;
14692                 wdev = NULL;
14693         } else if (wdev->wiphy != &rdev->wiphy) {
14694                 return -EINVAL;
14695         }
14696
14697         if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
14698             !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
14699                 return -EINVAL;
14700
14701         vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
14702         subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
14703         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
14704                 const struct wiphy_vendor_command *vcmd;
14705                 void *data = NULL;
14706                 int len = 0;
14707
14708                 vcmd = &rdev->wiphy.vendor_commands[i];
14709
14710                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14711                         continue;
14712
14713                 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14714                                    WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14715                         if (!wdev)
14716                                 return -EINVAL;
14717                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14718                             !wdev->netdev)
14719                                 return -EINVAL;
14720
14721                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14722                                 if (!wdev_running(wdev))
14723                                         return -ENETDOWN;
14724                         }
14725                 } else {
14726                         wdev = NULL;
14727                 }
14728
14729                 if (!vcmd->doit)
14730                         return -EOPNOTSUPP;
14731
14732                 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
14733                         data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14734                         len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
14735
14736                         err = nl80211_vendor_check_policy(vcmd,
14737                                         info->attrs[NL80211_ATTR_VENDOR_DATA],
14738                                         info->extack);
14739                         if (err)
14740                                 return err;
14741                 }
14742
14743                 rdev->cur_cmd_info = info;
14744                 err = vcmd->doit(&rdev->wiphy, wdev, data, len);
14745                 rdev->cur_cmd_info = NULL;
14746                 return err;
14747         }
14748
14749         return -EOPNOTSUPP;
14750 }
14751
14752 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
14753                                        struct netlink_callback *cb,
14754                                        struct cfg80211_registered_device **rdev,
14755                                        struct wireless_dev **wdev)
14756 {
14757         struct nlattr **attrbuf;
14758         u32 vid, subcmd;
14759         unsigned int i;
14760         int vcmd_idx = -1;
14761         int err;
14762         void *data = NULL;
14763         unsigned int data_len = 0;
14764
14765         if (cb->args[0]) {
14766                 /* subtract the 1 again here */
14767                 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
14768                 struct wireless_dev *tmp;
14769
14770                 if (!wiphy)
14771                         return -ENODEV;
14772                 *rdev = wiphy_to_rdev(wiphy);
14773                 *wdev = NULL;
14774
14775                 if (cb->args[1]) {
14776                         list_for_each_entry(tmp, &wiphy->wdev_list, list) {
14777                                 if (tmp->identifier == cb->args[1] - 1) {
14778                                         *wdev = tmp;
14779                                         break;
14780                                 }
14781                         }
14782                 }
14783
14784                 /* keep rtnl locked in successful case */
14785                 return 0;
14786         }
14787
14788         attrbuf = kcalloc(NUM_NL80211_ATTR, sizeof(*attrbuf), GFP_KERNEL);
14789         if (!attrbuf)
14790                 return -ENOMEM;
14791
14792         err = nlmsg_parse_deprecated(cb->nlh,
14793                                      GENL_HDRLEN + nl80211_fam.hdrsize,
14794                                      attrbuf, nl80211_fam.maxattr,
14795                                      nl80211_policy, NULL);
14796         if (err)
14797                 goto out;
14798
14799         if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
14800             !attrbuf[NL80211_ATTR_VENDOR_SUBCMD]) {
14801                 err = -EINVAL;
14802                 goto out;
14803         }
14804
14805         *wdev = __cfg80211_wdev_from_attrs(NULL, sock_net(skb->sk), attrbuf);
14806         if (IS_ERR(*wdev))
14807                 *wdev = NULL;
14808
14809         *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
14810         if (IS_ERR(*rdev)) {
14811                 err = PTR_ERR(*rdev);
14812                 goto out;
14813         }
14814
14815         vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
14816         subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
14817
14818         for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
14819                 const struct wiphy_vendor_command *vcmd;
14820
14821                 vcmd = &(*rdev)->wiphy.vendor_commands[i];
14822
14823                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
14824                         continue;
14825
14826                 if (!vcmd->dumpit) {
14827                         err = -EOPNOTSUPP;
14828                         goto out;
14829                 }
14830
14831                 vcmd_idx = i;
14832                 break;
14833         }
14834
14835         if (vcmd_idx < 0) {
14836                 err = -EOPNOTSUPP;
14837                 goto out;
14838         }
14839
14840         if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
14841                 data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14842                 data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
14843
14844                 err = nl80211_vendor_check_policy(
14845                                 &(*rdev)->wiphy.vendor_commands[vcmd_idx],
14846                                 attrbuf[NL80211_ATTR_VENDOR_DATA],
14847                                 cb->extack);
14848                 if (err)
14849                         goto out;
14850         }
14851
14852         /* 0 is the first index - add 1 to parse only once */
14853         cb->args[0] = (*rdev)->wiphy_idx + 1;
14854         /* add 1 to know if it was NULL */
14855         cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
14856         cb->args[2] = vcmd_idx;
14857         cb->args[3] = (unsigned long)data;
14858         cb->args[4] = data_len;
14859
14860         /* keep rtnl locked in successful case */
14861         err = 0;
14862 out:
14863         kfree(attrbuf);
14864         return err;
14865 }
14866
14867 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
14868                                    struct netlink_callback *cb)
14869 {
14870         struct cfg80211_registered_device *rdev;
14871         struct wireless_dev *wdev;
14872         unsigned int vcmd_idx;
14873         const struct wiphy_vendor_command *vcmd;
14874         void *data;
14875         int data_len;
14876         int err;
14877         struct nlattr *vendor_data;
14878
14879         rtnl_lock();
14880         err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
14881         if (err)
14882                 goto out;
14883
14884         vcmd_idx = cb->args[2];
14885         data = (void *)cb->args[3];
14886         data_len = cb->args[4];
14887         vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
14888
14889         if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
14890                            WIPHY_VENDOR_CMD_NEED_NETDEV)) {
14891                 if (!wdev) {
14892                         err = -EINVAL;
14893                         goto out;
14894                 }
14895                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
14896                     !wdev->netdev) {
14897                         err = -EINVAL;
14898                         goto out;
14899                 }
14900
14901                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
14902                         if (!wdev_running(wdev)) {
14903                                 err = -ENETDOWN;
14904                                 goto out;
14905                         }
14906                 }
14907         }
14908
14909         while (1) {
14910                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
14911                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
14912                                            NL80211_CMD_VENDOR);
14913                 if (!hdr)
14914                         break;
14915
14916                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14917                     (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
14918                                                wdev_id(wdev),
14919                                                NL80211_ATTR_PAD))) {
14920                         genlmsg_cancel(skb, hdr);
14921                         break;
14922                 }
14923
14924                 vendor_data = nla_nest_start_noflag(skb,
14925                                                     NL80211_ATTR_VENDOR_DATA);
14926                 if (!vendor_data) {
14927                         genlmsg_cancel(skb, hdr);
14928                         break;
14929                 }
14930
14931                 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
14932                                    (unsigned long *)&cb->args[5]);
14933                 nla_nest_end(skb, vendor_data);
14934
14935                 if (err == -ENOBUFS || err == -ENOENT) {
14936                         genlmsg_cancel(skb, hdr);
14937                         break;
14938                 } else if (err <= 0) {
14939                         genlmsg_cancel(skb, hdr);
14940                         goto out;
14941                 }
14942
14943                 genlmsg_end(skb, hdr);
14944         }
14945
14946         err = skb->len;
14947  out:
14948         rtnl_unlock();
14949         return err;
14950 }
14951
14952 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
14953                                            enum nl80211_commands cmd,
14954                                            enum nl80211_attrs attr,
14955                                            int approxlen)
14956 {
14957         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14958
14959         if (WARN_ON(!rdev->cur_cmd_info))
14960                 return NULL;
14961
14962         return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
14963                                            rdev->cur_cmd_info->snd_portid,
14964                                            rdev->cur_cmd_info->snd_seq,
14965                                            cmd, attr, NULL, GFP_KERNEL);
14966 }
14967 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
14968
14969 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
14970 {
14971         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
14972         void *hdr = ((void **)skb->cb)[1];
14973         struct nlattr *data = ((void **)skb->cb)[2];
14974
14975         /* clear CB data for netlink core to own from now on */
14976         memset(skb->cb, 0, sizeof(skb->cb));
14977
14978         if (WARN_ON(!rdev->cur_cmd_info)) {
14979                 kfree_skb(skb);
14980                 return -EINVAL;
14981         }
14982
14983         nla_nest_end(skb, data);
14984         genlmsg_end(skb, hdr);
14985         return genlmsg_reply(skb, rdev->cur_cmd_info);
14986 }
14987 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
14988
14989 unsigned int cfg80211_vendor_cmd_get_sender(struct wiphy *wiphy)
14990 {
14991         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14992
14993         if (WARN_ON(!rdev->cur_cmd_info))
14994                 return 0;
14995
14996         return rdev->cur_cmd_info->snd_portid;
14997 }
14998 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_get_sender);
14999
15000 static int nl80211_set_qos_map(struct sk_buff *skb,
15001                                struct genl_info *info)
15002 {
15003         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15004         struct cfg80211_qos_map *qos_map = NULL;
15005         struct net_device *dev = info->user_ptr[1];
15006         u8 *pos, len, num_des, des_len, des;
15007         int ret;
15008
15009         if (!rdev->ops->set_qos_map)
15010                 return -EOPNOTSUPP;
15011
15012         if (info->attrs[NL80211_ATTR_QOS_MAP]) {
15013                 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
15014                 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
15015
15016                 if (len % 2)
15017                         return -EINVAL;
15018
15019                 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
15020                 if (!qos_map)
15021                         return -ENOMEM;
15022
15023                 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
15024                 if (num_des) {
15025                         des_len = num_des *
15026                                 sizeof(struct cfg80211_dscp_exception);
15027                         memcpy(qos_map->dscp_exception, pos, des_len);
15028                         qos_map->num_des = num_des;
15029                         for (des = 0; des < num_des; des++) {
15030                                 if (qos_map->dscp_exception[des].up > 7) {
15031                                         kfree(qos_map);
15032                                         return -EINVAL;
15033                                 }
15034                         }
15035                         pos += des_len;
15036                 }
15037                 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
15038         }
15039
15040         wdev_lock(dev->ieee80211_ptr);
15041         ret = nl80211_key_allowed(dev->ieee80211_ptr);
15042         if (!ret)
15043                 ret = rdev_set_qos_map(rdev, dev, qos_map);
15044         wdev_unlock(dev->ieee80211_ptr);
15045
15046         kfree(qos_map);
15047         return ret;
15048 }
15049
15050 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
15051 {
15052         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15053         struct net_device *dev = info->user_ptr[1];
15054         struct wireless_dev *wdev = dev->ieee80211_ptr;
15055         const u8 *peer;
15056         u8 tsid, up;
15057         u16 admitted_time = 0;
15058         int err;
15059
15060         if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
15061                 return -EOPNOTSUPP;
15062
15063         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
15064             !info->attrs[NL80211_ATTR_USER_PRIO])
15065                 return -EINVAL;
15066
15067         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15068         up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
15069
15070         /* WMM uses TIDs 0-7 even for TSPEC */
15071         if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
15072                 /* TODO: handle 802.11 TSPEC/admission control
15073                  * need more attributes for that (e.g. BA session requirement);
15074                  * change the WMM adminssion test above to allow both then
15075                  */
15076                 return -EINVAL;
15077         }
15078
15079         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15080
15081         if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
15082                 admitted_time =
15083                         nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
15084                 if (!admitted_time)
15085                         return -EINVAL;
15086         }
15087
15088         wdev_lock(wdev);
15089         switch (wdev->iftype) {
15090         case NL80211_IFTYPE_STATION:
15091         case NL80211_IFTYPE_P2P_CLIENT:
15092                 if (wdev->connected)
15093                         break;
15094                 err = -ENOTCONN;
15095                 goto out;
15096         default:
15097                 err = -EOPNOTSUPP;
15098                 goto out;
15099         }
15100
15101         err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
15102
15103  out:
15104         wdev_unlock(wdev);
15105         return err;
15106 }
15107
15108 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
15109 {
15110         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15111         struct net_device *dev = info->user_ptr[1];
15112         struct wireless_dev *wdev = dev->ieee80211_ptr;
15113         const u8 *peer;
15114         u8 tsid;
15115         int err;
15116
15117         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
15118                 return -EINVAL;
15119
15120         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
15121         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15122
15123         wdev_lock(wdev);
15124         err = rdev_del_tx_ts(rdev, dev, tsid, peer);
15125         wdev_unlock(wdev);
15126
15127         return err;
15128 }
15129
15130 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
15131                                        struct genl_info *info)
15132 {
15133         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15134         struct net_device *dev = info->user_ptr[1];
15135         struct wireless_dev *wdev = dev->ieee80211_ptr;
15136         struct cfg80211_chan_def chandef = {};
15137         const u8 *addr;
15138         u8 oper_class;
15139         int err;
15140
15141         if (!rdev->ops->tdls_channel_switch ||
15142             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15143                 return -EOPNOTSUPP;
15144
15145         switch (dev->ieee80211_ptr->iftype) {
15146         case NL80211_IFTYPE_STATION:
15147         case NL80211_IFTYPE_P2P_CLIENT:
15148                 break;
15149         default:
15150                 return -EOPNOTSUPP;
15151         }
15152
15153         if (!info->attrs[NL80211_ATTR_MAC] ||
15154             !info->attrs[NL80211_ATTR_OPER_CLASS])
15155                 return -EINVAL;
15156
15157         err = nl80211_parse_chandef(rdev, info, &chandef);
15158         if (err)
15159                 return err;
15160
15161         /*
15162          * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
15163          * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
15164          * specification is not defined for them.
15165          */
15166         if (chandef.chan->band == NL80211_BAND_2GHZ &&
15167             chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
15168             chandef.width != NL80211_CHAN_WIDTH_20)
15169                 return -EINVAL;
15170
15171         /* we will be active on the TDLS link */
15172         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
15173                                            wdev->iftype))
15174                 return -EINVAL;
15175
15176         /* don't allow switching to DFS channels */
15177         if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
15178                 return -EINVAL;
15179
15180         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15181         oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
15182
15183         wdev_lock(wdev);
15184         err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
15185         wdev_unlock(wdev);
15186
15187         return err;
15188 }
15189
15190 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
15191                                               struct genl_info *info)
15192 {
15193         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15194         struct net_device *dev = info->user_ptr[1];
15195         struct wireless_dev *wdev = dev->ieee80211_ptr;
15196         const u8 *addr;
15197
15198         if (!rdev->ops->tdls_channel_switch ||
15199             !rdev->ops->tdls_cancel_channel_switch ||
15200             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
15201                 return -EOPNOTSUPP;
15202
15203         switch (dev->ieee80211_ptr->iftype) {
15204         case NL80211_IFTYPE_STATION:
15205         case NL80211_IFTYPE_P2P_CLIENT:
15206                 break;
15207         default:
15208                 return -EOPNOTSUPP;
15209         }
15210
15211         if (!info->attrs[NL80211_ATTR_MAC])
15212                 return -EINVAL;
15213
15214         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15215
15216         wdev_lock(wdev);
15217         rdev_tdls_cancel_channel_switch(rdev, dev, addr);
15218         wdev_unlock(wdev);
15219
15220         return 0;
15221 }
15222
15223 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
15224                                             struct genl_info *info)
15225 {
15226         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15227         struct net_device *dev = info->user_ptr[1];
15228         struct wireless_dev *wdev = dev->ieee80211_ptr;
15229         const struct nlattr *nla;
15230         bool enabled;
15231
15232         if (!rdev->ops->set_multicast_to_unicast)
15233                 return -EOPNOTSUPP;
15234
15235         if (wdev->iftype != NL80211_IFTYPE_AP &&
15236             wdev->iftype != NL80211_IFTYPE_P2P_GO)
15237                 return -EOPNOTSUPP;
15238
15239         nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
15240         enabled = nla_get_flag(nla);
15241
15242         return rdev_set_multicast_to_unicast(rdev, dev, enabled);
15243 }
15244
15245 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
15246 {
15247         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15248         struct net_device *dev = info->user_ptr[1];
15249         struct wireless_dev *wdev = dev->ieee80211_ptr;
15250         struct cfg80211_pmk_conf pmk_conf = {};
15251         int ret;
15252
15253         if (wdev->iftype != NL80211_IFTYPE_STATION &&
15254             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15255                 return -EOPNOTSUPP;
15256
15257         if (!wiphy_ext_feature_isset(&rdev->wiphy,
15258                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15259                 return -EOPNOTSUPP;
15260
15261         if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
15262                 return -EINVAL;
15263
15264         wdev_lock(wdev);
15265         if (!wdev->connected) {
15266                 ret = -ENOTCONN;
15267                 goto out;
15268         }
15269
15270         pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15271         if (memcmp(pmk_conf.aa, wdev->u.client.connected_addr, ETH_ALEN)) {
15272                 ret = -EINVAL;
15273                 goto out;
15274         }
15275
15276         pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
15277         pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
15278         if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
15279             pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
15280                 ret = -EINVAL;
15281                 goto out;
15282         }
15283
15284         if (info->attrs[NL80211_ATTR_PMKR0_NAME])
15285                 pmk_conf.pmk_r0_name =
15286                         nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
15287
15288         ret = rdev_set_pmk(rdev, dev, &pmk_conf);
15289 out:
15290         wdev_unlock(wdev);
15291         return ret;
15292 }
15293
15294 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
15295 {
15296         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15297         struct net_device *dev = info->user_ptr[1];
15298         struct wireless_dev *wdev = dev->ieee80211_ptr;
15299         const u8 *aa;
15300         int ret;
15301
15302         if (wdev->iftype != NL80211_IFTYPE_STATION &&
15303             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
15304                 return -EOPNOTSUPP;
15305
15306         if (!wiphy_ext_feature_isset(&rdev->wiphy,
15307                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
15308                 return -EOPNOTSUPP;
15309
15310         if (!info->attrs[NL80211_ATTR_MAC])
15311                 return -EINVAL;
15312
15313         wdev_lock(wdev);
15314         aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
15315         ret = rdev_del_pmk(rdev, dev, aa);
15316         wdev_unlock(wdev);
15317
15318         return ret;
15319 }
15320
15321 static int nl80211_external_auth(struct sk_buff *skb, struct genl_info *info)
15322 {
15323         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15324         struct net_device *dev = info->user_ptr[1];
15325         struct cfg80211_external_auth_params params;
15326
15327         if (!rdev->ops->external_auth)
15328                 return -EOPNOTSUPP;
15329
15330         if (!info->attrs[NL80211_ATTR_SSID] &&
15331             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
15332             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
15333                 return -EINVAL;
15334
15335         if (!info->attrs[NL80211_ATTR_BSSID])
15336                 return -EINVAL;
15337
15338         if (!info->attrs[NL80211_ATTR_STATUS_CODE])
15339                 return -EINVAL;
15340
15341         memset(&params, 0, sizeof(params));
15342
15343         if (info->attrs[NL80211_ATTR_SSID]) {
15344                 params.ssid.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
15345                 if (params.ssid.ssid_len == 0)
15346                         return -EINVAL;
15347                 memcpy(params.ssid.ssid,
15348                        nla_data(info->attrs[NL80211_ATTR_SSID]),
15349                        params.ssid.ssid_len);
15350         }
15351
15352         memcpy(params.bssid, nla_data(info->attrs[NL80211_ATTR_BSSID]),
15353                ETH_ALEN);
15354
15355         params.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15356
15357         if (info->attrs[NL80211_ATTR_PMKID])
15358                 params.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
15359
15360         return rdev_external_auth(rdev, dev, &params);
15361 }
15362
15363 static int nl80211_tx_control_port(struct sk_buff *skb, struct genl_info *info)
15364 {
15365         bool dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
15366         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15367         struct net_device *dev = info->user_ptr[1];
15368         struct wireless_dev *wdev = dev->ieee80211_ptr;
15369         const u8 *buf;
15370         size_t len;
15371         u8 *dest;
15372         u16 proto;
15373         bool noencrypt;
15374         u64 cookie = 0;
15375         int link_id;
15376         int err;
15377
15378         if (!wiphy_ext_feature_isset(&rdev->wiphy,
15379                                      NL80211_EXT_FEATURE_CONTROL_PORT_OVER_NL80211))
15380                 return -EOPNOTSUPP;
15381
15382         if (!rdev->ops->tx_control_port)
15383                 return -EOPNOTSUPP;
15384
15385         if (!info->attrs[NL80211_ATTR_FRAME] ||
15386             !info->attrs[NL80211_ATTR_MAC] ||
15387             !info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
15388                 GENL_SET_ERR_MSG(info, "Frame, MAC or ethertype missing");
15389                 return -EINVAL;
15390         }
15391
15392         wdev_lock(wdev);
15393
15394         switch (wdev->iftype) {
15395         case NL80211_IFTYPE_AP:
15396         case NL80211_IFTYPE_P2P_GO:
15397         case NL80211_IFTYPE_MESH_POINT:
15398                 break;
15399         case NL80211_IFTYPE_ADHOC:
15400                 if (wdev->u.ibss.current_bss)
15401                         break;
15402                 err = -ENOTCONN;
15403                 goto out;
15404         case NL80211_IFTYPE_STATION:
15405         case NL80211_IFTYPE_P2P_CLIENT:
15406                 if (wdev->connected)
15407                         break;
15408                 err = -ENOTCONN;
15409                 goto out;
15410         default:
15411                 err = -EOPNOTSUPP;
15412                 goto out;
15413         }
15414
15415         wdev_unlock(wdev);
15416
15417         buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15418         len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15419         dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15420         proto = nla_get_u16(info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
15421         noencrypt =
15422                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT]);
15423
15424         link_id = nl80211_link_id_or_invalid(info->attrs);
15425
15426         err = rdev_tx_control_port(rdev, dev, buf, len,
15427                                    dest, cpu_to_be16(proto), noencrypt, link_id,
15428                                    dont_wait_for_ack ? NULL : &cookie);
15429         if (!err && !dont_wait_for_ack)
15430                 nl_set_extack_cookie_u64(info->extack, cookie);
15431         return err;
15432  out:
15433         wdev_unlock(wdev);
15434         return err;
15435 }
15436
15437 static int nl80211_get_ftm_responder_stats(struct sk_buff *skb,
15438                                            struct genl_info *info)
15439 {
15440         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15441         struct net_device *dev = info->user_ptr[1];
15442         struct wireless_dev *wdev = dev->ieee80211_ptr;
15443         struct cfg80211_ftm_responder_stats ftm_stats = {};
15444         unsigned int link_id = nl80211_link_id(info->attrs);
15445         struct sk_buff *msg;
15446         void *hdr;
15447         struct nlattr *ftm_stats_attr;
15448         int err;
15449
15450         if (wdev->iftype != NL80211_IFTYPE_AP ||
15451             !wdev->links[link_id].ap.beacon_interval)
15452                 return -EOPNOTSUPP;
15453
15454         err = rdev_get_ftm_responder_stats(rdev, dev, &ftm_stats);
15455         if (err)
15456                 return err;
15457
15458         if (!ftm_stats.filled)
15459                 return -ENODATA;
15460
15461         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15462         if (!msg)
15463                 return -ENOMEM;
15464
15465         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
15466                              NL80211_CMD_GET_FTM_RESPONDER_STATS);
15467         if (!hdr)
15468                 goto nla_put_failure;
15469
15470         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
15471                 goto nla_put_failure;
15472
15473         ftm_stats_attr = nla_nest_start_noflag(msg,
15474                                                NL80211_ATTR_FTM_RESPONDER_STATS);
15475         if (!ftm_stats_attr)
15476                 goto nla_put_failure;
15477
15478 #define SET_FTM(field, name, type)                                       \
15479         do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15480             nla_put_ ## type(msg, NL80211_FTM_STATS_ ## name,            \
15481                              ftm_stats.field))                           \
15482                 goto nla_put_failure; } while (0)
15483 #define SET_FTM_U64(field, name)                                         \
15484         do { if ((ftm_stats.filled & BIT(NL80211_FTM_STATS_ ## name)) && \
15485             nla_put_u64_64bit(msg, NL80211_FTM_STATS_ ## name,           \
15486                               ftm_stats.field, NL80211_FTM_STATS_PAD))   \
15487                 goto nla_put_failure; } while (0)
15488
15489         SET_FTM(success_num, SUCCESS_NUM, u32);
15490         SET_FTM(partial_num, PARTIAL_NUM, u32);
15491         SET_FTM(failed_num, FAILED_NUM, u32);
15492         SET_FTM(asap_num, ASAP_NUM, u32);
15493         SET_FTM(non_asap_num, NON_ASAP_NUM, u32);
15494         SET_FTM_U64(total_duration_ms, TOTAL_DURATION_MSEC);
15495         SET_FTM(unknown_triggers_num, UNKNOWN_TRIGGERS_NUM, u32);
15496         SET_FTM(reschedule_requests_num, RESCHEDULE_REQUESTS_NUM, u32);
15497         SET_FTM(out_of_window_triggers_num, OUT_OF_WINDOW_TRIGGERS_NUM, u32);
15498 #undef SET_FTM
15499
15500         nla_nest_end(msg, ftm_stats_attr);
15501
15502         genlmsg_end(msg, hdr);
15503         return genlmsg_reply(msg, info);
15504
15505 nla_put_failure:
15506         nlmsg_free(msg);
15507         return -ENOBUFS;
15508 }
15509
15510 static int nl80211_update_owe_info(struct sk_buff *skb, struct genl_info *info)
15511 {
15512         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15513         struct cfg80211_update_owe_info owe_info;
15514         struct net_device *dev = info->user_ptr[1];
15515
15516         if (!rdev->ops->update_owe_info)
15517                 return -EOPNOTSUPP;
15518
15519         if (!info->attrs[NL80211_ATTR_STATUS_CODE] ||
15520             !info->attrs[NL80211_ATTR_MAC])
15521                 return -EINVAL;
15522
15523         memset(&owe_info, 0, sizeof(owe_info));
15524         owe_info.status = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
15525         nla_memcpy(owe_info.peer, info->attrs[NL80211_ATTR_MAC], ETH_ALEN);
15526
15527         if (info->attrs[NL80211_ATTR_IE]) {
15528                 owe_info.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
15529                 owe_info.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
15530         }
15531
15532         return rdev_update_owe_info(rdev, dev, &owe_info);
15533 }
15534
15535 static int nl80211_probe_mesh_link(struct sk_buff *skb, struct genl_info *info)
15536 {
15537         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15538         struct net_device *dev = info->user_ptr[1];
15539         struct wireless_dev *wdev = dev->ieee80211_ptr;
15540         struct station_info sinfo = {};
15541         const u8 *buf;
15542         size_t len;
15543         u8 *dest;
15544         int err;
15545
15546         if (!rdev->ops->probe_mesh_link || !rdev->ops->get_station)
15547                 return -EOPNOTSUPP;
15548
15549         if (!info->attrs[NL80211_ATTR_MAC] ||
15550             !info->attrs[NL80211_ATTR_FRAME]) {
15551                 GENL_SET_ERR_MSG(info, "Frame or MAC missing");
15552                 return -EINVAL;
15553         }
15554
15555         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
15556                 return -EOPNOTSUPP;
15557
15558         dest = nla_data(info->attrs[NL80211_ATTR_MAC]);
15559         buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
15560         len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
15561
15562         if (len < sizeof(struct ethhdr))
15563                 return -EINVAL;
15564
15565         if (!ether_addr_equal(buf, dest) || is_multicast_ether_addr(buf) ||
15566             !ether_addr_equal(buf + ETH_ALEN, dev->dev_addr))
15567                 return -EINVAL;
15568
15569         err = rdev_get_station(rdev, dev, dest, &sinfo);
15570         if (err)
15571                 return err;
15572
15573         cfg80211_sinfo_release_content(&sinfo);
15574
15575         return rdev_probe_mesh_link(rdev, dev, dest, buf, len);
15576 }
15577
15578 static int parse_tid_conf(struct cfg80211_registered_device *rdev,
15579                           struct nlattr *attrs[], struct net_device *dev,
15580                           struct cfg80211_tid_cfg *tid_conf,
15581                           struct genl_info *info, const u8 *peer,
15582                           unsigned int link_id)
15583 {
15584         struct netlink_ext_ack *extack = info->extack;
15585         u64 mask;
15586         int err;
15587
15588         if (!attrs[NL80211_TID_CONFIG_ATTR_TIDS])
15589                 return -EINVAL;
15590
15591         tid_conf->config_override =
15592                         nla_get_flag(attrs[NL80211_TID_CONFIG_ATTR_OVERRIDE]);
15593         tid_conf->tids = nla_get_u16(attrs[NL80211_TID_CONFIG_ATTR_TIDS]);
15594
15595         if (tid_conf->config_override) {
15596                 if (rdev->ops->reset_tid_config) {
15597                         err = rdev_reset_tid_config(rdev, dev, peer,
15598                                                     tid_conf->tids);
15599                         if (err)
15600                                 return err;
15601                 } else {
15602                         return -EINVAL;
15603                 }
15604         }
15605
15606         if (attrs[NL80211_TID_CONFIG_ATTR_NOACK]) {
15607                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_NOACK);
15608                 tid_conf->noack =
15609                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_NOACK]);
15610         }
15611
15612         if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]) {
15613                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT);
15614                 tid_conf->retry_short =
15615                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_SHORT]);
15616
15617                 if (tid_conf->retry_short > rdev->wiphy.max_data_retry_count)
15618                         return -EINVAL;
15619         }
15620
15621         if (attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]) {
15622                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
15623                 tid_conf->retry_long =
15624                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RETRY_LONG]);
15625
15626                 if (tid_conf->retry_long > rdev->wiphy.max_data_retry_count)
15627                         return -EINVAL;
15628         }
15629
15630         if (attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]) {
15631                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
15632                 tid_conf->ampdu =
15633                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMPDU_CTRL]);
15634         }
15635
15636         if (attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]) {
15637                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
15638                 tid_conf->rtscts =
15639                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL]);
15640         }
15641
15642         if (attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]) {
15643                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_AMSDU_CTRL);
15644                 tid_conf->amsdu =
15645                         nla_get_u8(attrs[NL80211_TID_CONFIG_ATTR_AMSDU_CTRL]);
15646         }
15647
15648         if (attrs[NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE]) {
15649                 u32 idx = NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE, attr;
15650
15651                 tid_conf->txrate_type = nla_get_u8(attrs[idx]);
15652
15653                 if (tid_conf->txrate_type != NL80211_TX_RATE_AUTOMATIC) {
15654                         attr = NL80211_TID_CONFIG_ATTR_TX_RATE;
15655                         err = nl80211_parse_tx_bitrate_mask(info, attrs, attr,
15656                                                     &tid_conf->txrate_mask, dev,
15657                                                     true, link_id);
15658                         if (err)
15659                                 return err;
15660
15661                         tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE);
15662                 }
15663                 tid_conf->mask |= BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
15664         }
15665
15666         if (peer)
15667                 mask = rdev->wiphy.tid_config_support.peer;
15668         else
15669                 mask = rdev->wiphy.tid_config_support.vif;
15670
15671         if (tid_conf->mask & ~mask) {
15672                 NL_SET_ERR_MSG(extack, "unsupported TID configuration");
15673                 return -ENOTSUPP;
15674         }
15675
15676         return 0;
15677 }
15678
15679 static int nl80211_set_tid_config(struct sk_buff *skb,
15680                                   struct genl_info *info)
15681 {
15682         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15683         struct nlattr *attrs[NL80211_TID_CONFIG_ATTR_MAX + 1];
15684         unsigned int link_id = nl80211_link_id(info->attrs);
15685         struct net_device *dev = info->user_ptr[1];
15686         struct cfg80211_tid_config *tid_config;
15687         struct nlattr *tid;
15688         int conf_idx = 0, rem_conf;
15689         int ret = -EINVAL;
15690         u32 num_conf = 0;
15691
15692         if (!info->attrs[NL80211_ATTR_TID_CONFIG])
15693                 return -EINVAL;
15694
15695         if (!rdev->ops->set_tid_config)
15696                 return -EOPNOTSUPP;
15697
15698         nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15699                             rem_conf)
15700                 num_conf++;
15701
15702         tid_config = kzalloc(struct_size(tid_config, tid_conf, num_conf),
15703                              GFP_KERNEL);
15704         if (!tid_config)
15705                 return -ENOMEM;
15706
15707         tid_config->n_tid_conf = num_conf;
15708
15709         if (info->attrs[NL80211_ATTR_MAC])
15710                 tid_config->peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
15711
15712         wdev_lock(dev->ieee80211_ptr);
15713
15714         nla_for_each_nested(tid, info->attrs[NL80211_ATTR_TID_CONFIG],
15715                             rem_conf) {
15716                 ret = nla_parse_nested(attrs, NL80211_TID_CONFIG_ATTR_MAX,
15717                                        tid, NULL, NULL);
15718
15719                 if (ret)
15720                         goto bad_tid_conf;
15721
15722                 ret = parse_tid_conf(rdev, attrs, dev,
15723                                      &tid_config->tid_conf[conf_idx],
15724                                      info, tid_config->peer, link_id);
15725                 if (ret)
15726                         goto bad_tid_conf;
15727
15728                 conf_idx++;
15729         }
15730
15731         ret = rdev_set_tid_config(rdev, dev, tid_config);
15732
15733 bad_tid_conf:
15734         kfree(tid_config);
15735         wdev_unlock(dev->ieee80211_ptr);
15736         return ret;
15737 }
15738
15739 static int nl80211_color_change(struct sk_buff *skb, struct genl_info *info)
15740 {
15741         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15742         struct cfg80211_color_change_settings params = {};
15743         struct net_device *dev = info->user_ptr[1];
15744         struct wireless_dev *wdev = dev->ieee80211_ptr;
15745         struct nlattr **tb;
15746         u16 offset;
15747         int err;
15748
15749         if (!rdev->ops->color_change)
15750                 return -EOPNOTSUPP;
15751
15752         if (!wiphy_ext_feature_isset(&rdev->wiphy,
15753                                      NL80211_EXT_FEATURE_BSS_COLOR))
15754                 return -EOPNOTSUPP;
15755
15756         if (wdev->iftype != NL80211_IFTYPE_AP)
15757                 return -EOPNOTSUPP;
15758
15759         if (!info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT] ||
15760             !info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR] ||
15761             !info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS])
15762                 return -EINVAL;
15763
15764         params.count = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COUNT]);
15765         params.color = nla_get_u8(info->attrs[NL80211_ATTR_COLOR_CHANGE_COLOR]);
15766
15767         err = nl80211_parse_beacon(rdev, info->attrs, &params.beacon_next);
15768         if (err)
15769                 return err;
15770
15771         tb = kcalloc(NL80211_ATTR_MAX + 1, sizeof(*tb), GFP_KERNEL);
15772         if (!tb)
15773                 return -ENOMEM;
15774
15775         err = nla_parse_nested(tb, NL80211_ATTR_MAX,
15776                                info->attrs[NL80211_ATTR_COLOR_CHANGE_ELEMS],
15777                                nl80211_policy, info->extack);
15778         if (err)
15779                 goto out;
15780
15781         err = nl80211_parse_beacon(rdev, tb, &params.beacon_color_change);
15782         if (err)
15783                 goto out;
15784
15785         if (!tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) {
15786                 err = -EINVAL;
15787                 goto out;
15788         }
15789
15790         if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]) != sizeof(u16)) {
15791                 err = -EINVAL;
15792                 goto out;
15793         }
15794
15795         offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_BEACON]);
15796         if (offset >= params.beacon_color_change.tail_len) {
15797                 err = -EINVAL;
15798                 goto out;
15799         }
15800
15801         if (params.beacon_color_change.tail[offset] != params.count) {
15802                 err = -EINVAL;
15803                 goto out;
15804         }
15805
15806         params.counter_offset_beacon = offset;
15807
15808         if (tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) {
15809                 if (nla_len(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]) !=
15810                     sizeof(u16)) {
15811                         err = -EINVAL;
15812                         goto out;
15813                 }
15814
15815                 offset = nla_get_u16(tb[NL80211_ATTR_CNTDWN_OFFS_PRESP]);
15816                 if (offset >= params.beacon_color_change.probe_resp_len) {
15817                         err = -EINVAL;
15818                         goto out;
15819                 }
15820
15821                 if (params.beacon_color_change.probe_resp[offset] !=
15822                     params.count) {
15823                         err = -EINVAL;
15824                         goto out;
15825                 }
15826
15827                 params.counter_offset_presp = offset;
15828         }
15829
15830         wdev_lock(wdev);
15831         err = rdev_color_change(rdev, dev, &params);
15832         wdev_unlock(wdev);
15833
15834 out:
15835         kfree(params.beacon_next.mbssid_ies);
15836         kfree(params.beacon_color_change.mbssid_ies);
15837         kfree(tb);
15838         return err;
15839 }
15840
15841 static int nl80211_set_fils_aad(struct sk_buff *skb,
15842                                 struct genl_info *info)
15843 {
15844         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15845         struct net_device *dev = info->user_ptr[1];
15846         struct cfg80211_fils_aad fils_aad = {};
15847         u8 *nonces;
15848
15849         if (!info->attrs[NL80211_ATTR_MAC] ||
15850             !info->attrs[NL80211_ATTR_FILS_KEK] ||
15851             !info->attrs[NL80211_ATTR_FILS_NONCES])
15852                 return -EINVAL;
15853
15854         fils_aad.macaddr = nla_data(info->attrs[NL80211_ATTR_MAC]);
15855         fils_aad.kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
15856         fils_aad.kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
15857         nonces = nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
15858         fils_aad.snonce = nonces;
15859         fils_aad.anonce = nonces + FILS_NONCE_LEN;
15860
15861         return rdev_set_fils_aad(rdev, dev, &fils_aad);
15862 }
15863
15864 static int nl80211_add_link(struct sk_buff *skb, struct genl_info *info)
15865 {
15866         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15867         unsigned int link_id = nl80211_link_id(info->attrs);
15868         struct net_device *dev = info->user_ptr[1];
15869         struct wireless_dev *wdev = dev->ieee80211_ptr;
15870         int ret;
15871
15872         if (!(wdev->wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO))
15873                 return -EINVAL;
15874
15875         switch (wdev->iftype) {
15876         case NL80211_IFTYPE_AP:
15877                 break;
15878         default:
15879                 return -EINVAL;
15880         }
15881
15882         if (!info->attrs[NL80211_ATTR_MAC] ||
15883             !is_valid_ether_addr(nla_data(info->attrs[NL80211_ATTR_MAC])))
15884                 return -EINVAL;
15885
15886         wdev_lock(wdev);
15887         wdev->valid_links |= BIT(link_id);
15888         ether_addr_copy(wdev->links[link_id].addr,
15889                         nla_data(info->attrs[NL80211_ATTR_MAC]));
15890
15891         ret = rdev_add_intf_link(rdev, wdev, link_id);
15892         if (ret) {
15893                 wdev->valid_links &= ~BIT(link_id);
15894                 eth_zero_addr(wdev->links[link_id].addr);
15895         }
15896         wdev_unlock(wdev);
15897
15898         return ret;
15899 }
15900
15901 static int nl80211_remove_link(struct sk_buff *skb, struct genl_info *info)
15902 {
15903         unsigned int link_id = nl80211_link_id(info->attrs);
15904         struct net_device *dev = info->user_ptr[1];
15905         struct wireless_dev *wdev = dev->ieee80211_ptr;
15906
15907         /* cannot remove if there's no link */
15908         if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15909                 return -EINVAL;
15910
15911         switch (wdev->iftype) {
15912         case NL80211_IFTYPE_AP:
15913                 break;
15914         default:
15915                 return -EINVAL;
15916         }
15917
15918         wdev_lock(wdev);
15919         cfg80211_remove_link(wdev, link_id);
15920         wdev_unlock(wdev);
15921
15922         return 0;
15923 }
15924
15925 static int
15926 nl80211_add_mod_link_station(struct sk_buff *skb, struct genl_info *info,
15927                              bool add)
15928 {
15929         struct link_station_parameters params = {};
15930         struct cfg80211_registered_device *rdev = info->user_ptr[0];
15931         struct net_device *dev = info->user_ptr[1];
15932         int err;
15933
15934         if ((add && !rdev->ops->add_link_station) ||
15935             (!add && !rdev->ops->mod_link_station))
15936                 return -EOPNOTSUPP;
15937
15938         if (add && !info->attrs[NL80211_ATTR_MAC])
15939                 return -EINVAL;
15940
15941         if (!info->attrs[NL80211_ATTR_MLD_ADDR])
15942                 return -EINVAL;
15943
15944         if (add && !info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
15945                 return -EINVAL;
15946
15947         params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
15948
15949         if (info->attrs[NL80211_ATTR_MAC]) {
15950                 params.link_mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
15951                 if (!is_valid_ether_addr(params.link_mac))
15952                         return -EINVAL;
15953         }
15954
15955         if (!info->attrs[NL80211_ATTR_MLO_LINK_ID])
15956                 return -EINVAL;
15957
15958         params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
15959
15960         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
15961                 params.supported_rates =
15962                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15963                 params.supported_rates_len =
15964                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
15965         }
15966
15967         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
15968                 params.ht_capa =
15969                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
15970
15971         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
15972                 params.vht_capa =
15973                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
15974
15975         if (info->attrs[NL80211_ATTR_HE_CAPABILITY]) {
15976                 params.he_capa =
15977                         nla_data(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15978                 params.he_capa_len =
15979                         nla_len(info->attrs[NL80211_ATTR_HE_CAPABILITY]);
15980
15981                 if (info->attrs[NL80211_ATTR_EHT_CAPABILITY]) {
15982                         params.eht_capa =
15983                                 nla_data(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15984                         params.eht_capa_len =
15985                                 nla_len(info->attrs[NL80211_ATTR_EHT_CAPABILITY]);
15986
15987                         if (!ieee80211_eht_capa_size_ok((const u8 *)params.he_capa,
15988                                                         (const u8 *)params.eht_capa,
15989                                                         params.eht_capa_len,
15990                                                         false))
15991                                 return -EINVAL;
15992                 }
15993         }
15994
15995         if (info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY])
15996                 params.he_6ghz_capa =
15997                         nla_data(info->attrs[NL80211_ATTR_HE_6GHZ_CAPABILITY]);
15998
15999         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
16000                 params.opmode_notif_used = true;
16001                 params.opmode_notif =
16002                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
16003         }
16004
16005         err = nl80211_parse_sta_txpower_setting(info, &params.txpwr,
16006                                                 &params.txpwr_set);
16007         if (err)
16008                 return err;
16009
16010         wdev_lock(dev->ieee80211_ptr);
16011         if (add)
16012                 err = rdev_add_link_station(rdev, dev, &params);
16013         else
16014                 err = rdev_mod_link_station(rdev, dev, &params);
16015         wdev_unlock(dev->ieee80211_ptr);
16016
16017         return err;
16018 }
16019
16020 static int
16021 nl80211_add_link_station(struct sk_buff *skb, struct genl_info *info)
16022 {
16023         return nl80211_add_mod_link_station(skb, info, true);
16024 }
16025
16026 static int
16027 nl80211_modify_link_station(struct sk_buff *skb, struct genl_info *info)
16028 {
16029         return nl80211_add_mod_link_station(skb, info, false);
16030 }
16031
16032 static int
16033 nl80211_remove_link_station(struct sk_buff *skb, struct genl_info *info)
16034 {
16035         struct link_station_del_parameters params = {};
16036         struct cfg80211_registered_device *rdev = info->user_ptr[0];
16037         struct net_device *dev = info->user_ptr[1];
16038         int ret;
16039
16040         if (!rdev->ops->del_link_station)
16041                 return -EOPNOTSUPP;
16042
16043         if (!info->attrs[NL80211_ATTR_MLD_ADDR] ||
16044             !info->attrs[NL80211_ATTR_MLO_LINK_ID])
16045                 return -EINVAL;
16046
16047         params.mld_mac = nla_data(info->attrs[NL80211_ATTR_MLD_ADDR]);
16048         params.link_id = nla_get_u8(info->attrs[NL80211_ATTR_MLO_LINK_ID]);
16049
16050         wdev_lock(dev->ieee80211_ptr);
16051         ret = rdev_del_link_station(rdev, dev, &params);
16052         wdev_unlock(dev->ieee80211_ptr);
16053
16054         return ret;
16055 }
16056
16057 #define NL80211_FLAG_NEED_WIPHY         0x01
16058 #define NL80211_FLAG_NEED_NETDEV        0x02
16059 #define NL80211_FLAG_NEED_RTNL          0x04
16060 #define NL80211_FLAG_CHECK_NETDEV_UP    0x08
16061 #define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
16062                                          NL80211_FLAG_CHECK_NETDEV_UP)
16063 #define NL80211_FLAG_NEED_WDEV          0x10
16064 /* If a netdev is associated, it must be UP, P2P must be started */
16065 #define NL80211_FLAG_NEED_WDEV_UP       (NL80211_FLAG_NEED_WDEV |\
16066                                          NL80211_FLAG_CHECK_NETDEV_UP)
16067 #define NL80211_FLAG_CLEAR_SKB          0x20
16068 #define NL80211_FLAG_NO_WIPHY_MTX       0x40
16069 #define NL80211_FLAG_MLO_VALID_LINK_ID  0x80
16070 #define NL80211_FLAG_MLO_UNSUPPORTED    0x100
16071
16072 #define INTERNAL_FLAG_SELECTORS(__sel)                  \
16073         SELECTOR(__sel, NONE, 0) /* must be first */    \
16074         SELECTOR(__sel, WIPHY,                          \
16075                  NL80211_FLAG_NEED_WIPHY)               \
16076         SELECTOR(__sel, WDEV,                           \
16077                  NL80211_FLAG_NEED_WDEV)                \
16078         SELECTOR(__sel, NETDEV,                         \
16079                  NL80211_FLAG_NEED_NETDEV)              \
16080         SELECTOR(__sel, NETDEV_LINK,                    \
16081                  NL80211_FLAG_NEED_NETDEV |             \
16082                  NL80211_FLAG_MLO_VALID_LINK_ID)        \
16083         SELECTOR(__sel, NETDEV_NO_MLO,                  \
16084                  NL80211_FLAG_NEED_NETDEV |             \
16085                  NL80211_FLAG_MLO_UNSUPPORTED)  \
16086         SELECTOR(__sel, WIPHY_RTNL,                     \
16087                  NL80211_FLAG_NEED_WIPHY |              \
16088                  NL80211_FLAG_NEED_RTNL)                \
16089         SELECTOR(__sel, WIPHY_RTNL_NOMTX,               \
16090                  NL80211_FLAG_NEED_WIPHY |              \
16091                  NL80211_FLAG_NEED_RTNL |               \
16092                  NL80211_FLAG_NO_WIPHY_MTX)             \
16093         SELECTOR(__sel, WDEV_RTNL,                      \
16094                  NL80211_FLAG_NEED_WDEV |               \
16095                  NL80211_FLAG_NEED_RTNL)                \
16096         SELECTOR(__sel, NETDEV_RTNL,                    \
16097                  NL80211_FLAG_NEED_NETDEV |             \
16098                  NL80211_FLAG_NEED_RTNL)                \
16099         SELECTOR(__sel, NETDEV_UP,                      \
16100                  NL80211_FLAG_NEED_NETDEV_UP)           \
16101         SELECTOR(__sel, NETDEV_UP_LINK,                 \
16102                  NL80211_FLAG_NEED_NETDEV_UP |          \
16103                  NL80211_FLAG_MLO_VALID_LINK_ID)        \
16104         SELECTOR(__sel, NETDEV_UP_NO_MLO,               \
16105                  NL80211_FLAG_NEED_NETDEV_UP |          \
16106                  NL80211_FLAG_MLO_UNSUPPORTED)          \
16107         SELECTOR(__sel, NETDEV_UP_NO_MLO_CLEAR,         \
16108                  NL80211_FLAG_NEED_NETDEV_UP |          \
16109                  NL80211_FLAG_CLEAR_SKB |               \
16110                  NL80211_FLAG_MLO_UNSUPPORTED)          \
16111         SELECTOR(__sel, NETDEV_UP_NOTMX,                \
16112                  NL80211_FLAG_NEED_NETDEV_UP |          \
16113                  NL80211_FLAG_NO_WIPHY_MTX)             \
16114         SELECTOR(__sel, NETDEV_UP_NOTMX_NOMLO,          \
16115                  NL80211_FLAG_NEED_NETDEV_UP |          \
16116                  NL80211_FLAG_NO_WIPHY_MTX |            \
16117                  NL80211_FLAG_MLO_UNSUPPORTED)          \
16118         SELECTOR(__sel, NETDEV_UP_CLEAR,                \
16119                  NL80211_FLAG_NEED_NETDEV_UP |          \
16120                  NL80211_FLAG_CLEAR_SKB)                \
16121         SELECTOR(__sel, WDEV_UP,                        \
16122                  NL80211_FLAG_NEED_WDEV_UP)             \
16123         SELECTOR(__sel, WDEV_UP_LINK,                   \
16124                  NL80211_FLAG_NEED_WDEV_UP |            \
16125                  NL80211_FLAG_MLO_VALID_LINK_ID)        \
16126         SELECTOR(__sel, WDEV_UP_RTNL,                   \
16127                  NL80211_FLAG_NEED_WDEV_UP |            \
16128                  NL80211_FLAG_NEED_RTNL)                \
16129         SELECTOR(__sel, WIPHY_CLEAR,                    \
16130                  NL80211_FLAG_NEED_WIPHY |              \
16131                  NL80211_FLAG_CLEAR_SKB)
16132
16133 enum nl80211_internal_flags_selector {
16134 #define SELECTOR(_, name, value)        NL80211_IFL_SEL_##name,
16135         INTERNAL_FLAG_SELECTORS(_)
16136 #undef SELECTOR
16137 };
16138
16139 static u32 nl80211_internal_flags[] = {
16140 #define SELECTOR(_, name, value)        [NL80211_IFL_SEL_##name] = value,
16141         INTERNAL_FLAG_SELECTORS(_)
16142 #undef SELECTOR
16143 };
16144
16145 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
16146                             struct genl_info *info)
16147 {
16148         struct cfg80211_registered_device *rdev = NULL;
16149         struct wireless_dev *wdev = NULL;
16150         struct net_device *dev = NULL;
16151         u32 internal_flags;
16152         int err;
16153
16154         if (WARN_ON(ops->internal_flags >= ARRAY_SIZE(nl80211_internal_flags)))
16155                 return -EINVAL;
16156
16157         internal_flags = nl80211_internal_flags[ops->internal_flags];
16158
16159         rtnl_lock();
16160         if (internal_flags & NL80211_FLAG_NEED_WIPHY) {
16161                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
16162                 if (IS_ERR(rdev)) {
16163                         err = PTR_ERR(rdev);
16164                         goto out_unlock;
16165                 }
16166                 info->user_ptr[0] = rdev;
16167         } else if (internal_flags & NL80211_FLAG_NEED_NETDEV ||
16168                    internal_flags & NL80211_FLAG_NEED_WDEV) {
16169                 wdev = __cfg80211_wdev_from_attrs(NULL, genl_info_net(info),
16170                                                   info->attrs);
16171                 if (IS_ERR(wdev)) {
16172                         err = PTR_ERR(wdev);
16173                         goto out_unlock;
16174                 }
16175
16176                 dev = wdev->netdev;
16177                 dev_hold(dev);
16178                 rdev = wiphy_to_rdev(wdev->wiphy);
16179
16180                 if (internal_flags & NL80211_FLAG_NEED_NETDEV) {
16181                         if (!dev) {
16182                                 err = -EINVAL;
16183                                 goto out_unlock;
16184                         }
16185
16186                         info->user_ptr[1] = dev;
16187                 } else {
16188                         info->user_ptr[1] = wdev;
16189                 }
16190
16191                 if (internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
16192                     !wdev_running(wdev)) {
16193                         err = -ENETDOWN;
16194                         goto out_unlock;
16195                 }
16196
16197                 info->user_ptr[0] = rdev;
16198         }
16199
16200         if (internal_flags & NL80211_FLAG_MLO_VALID_LINK_ID) {
16201                 struct nlattr *link_id = info->attrs[NL80211_ATTR_MLO_LINK_ID];
16202
16203                 if (!wdev) {
16204                         err = -EINVAL;
16205                         goto out_unlock;
16206                 }
16207
16208                 /* MLO -> require valid link ID */
16209                 if (wdev->valid_links &&
16210                     (!link_id ||
16211                      !(wdev->valid_links & BIT(nla_get_u8(link_id))))) {
16212                         err = -EINVAL;
16213                         goto out_unlock;
16214                 }
16215
16216                 /* non-MLO -> no link ID attribute accepted */
16217                 if (!wdev->valid_links && link_id) {
16218                         err = -EINVAL;
16219                         goto out_unlock;
16220                 }
16221         }
16222
16223         if (internal_flags & NL80211_FLAG_MLO_UNSUPPORTED) {
16224                 if (info->attrs[NL80211_ATTR_MLO_LINK_ID] ||
16225                     (wdev && wdev->valid_links)) {
16226                         err = -EINVAL;
16227                         goto out_unlock;
16228                 }
16229         }
16230
16231         if (rdev && !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16232                 wiphy_lock(&rdev->wiphy);
16233                 /* we keep the mutex locked until post_doit */
16234                 __release(&rdev->wiphy.mtx);
16235         }
16236         if (!(internal_flags & NL80211_FLAG_NEED_RTNL))
16237                 rtnl_unlock();
16238
16239         return 0;
16240 out_unlock:
16241         rtnl_unlock();
16242         dev_put(dev);
16243         return err;
16244 }
16245
16246 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
16247                               struct genl_info *info)
16248 {
16249         u32 internal_flags = nl80211_internal_flags[ops->internal_flags];
16250
16251         if (info->user_ptr[1]) {
16252                 if (internal_flags & NL80211_FLAG_NEED_WDEV) {
16253                         struct wireless_dev *wdev = info->user_ptr[1];
16254
16255                         dev_put(wdev->netdev);
16256                 } else {
16257                         dev_put(info->user_ptr[1]);
16258                 }
16259         }
16260
16261         if (info->user_ptr[0] &&
16262             !(internal_flags & NL80211_FLAG_NO_WIPHY_MTX)) {
16263                 struct cfg80211_registered_device *rdev = info->user_ptr[0];
16264
16265                 /* we kept the mutex locked since pre_doit */
16266                 __acquire(&rdev->wiphy.mtx);
16267                 wiphy_unlock(&rdev->wiphy);
16268         }
16269
16270         if (internal_flags & NL80211_FLAG_NEED_RTNL)
16271                 rtnl_unlock();
16272
16273         /* If needed, clear the netlink message payload from the SKB
16274          * as it might contain key data that shouldn't stick around on
16275          * the heap after the SKB is freed. The netlink message header
16276          * is still needed for further processing, so leave it intact.
16277          */
16278         if (internal_flags & NL80211_FLAG_CLEAR_SKB) {
16279                 struct nlmsghdr *nlh = nlmsg_hdr(skb);
16280
16281                 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
16282         }
16283 }
16284
16285 static int nl80211_set_sar_sub_specs(struct cfg80211_registered_device *rdev,
16286                                      struct cfg80211_sar_specs *sar_specs,
16287                                      struct nlattr *spec[], int index)
16288 {
16289         u32 range_index, i;
16290
16291         if (!sar_specs || !spec)
16292                 return -EINVAL;
16293
16294         if (!spec[NL80211_SAR_ATTR_SPECS_POWER] ||
16295             !spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX])
16296                 return -EINVAL;
16297
16298         range_index = nla_get_u32(spec[NL80211_SAR_ATTR_SPECS_RANGE_INDEX]);
16299
16300         /* check if range_index exceeds num_freq_ranges */
16301         if (range_index >= rdev->wiphy.sar_capa->num_freq_ranges)
16302                 return -EINVAL;
16303
16304         /* check if range_index duplicates */
16305         for (i = 0; i < index; i++) {
16306                 if (sar_specs->sub_specs[i].freq_range_index == range_index)
16307                         return -EINVAL;
16308         }
16309
16310         sar_specs->sub_specs[index].power =
16311                 nla_get_s32(spec[NL80211_SAR_ATTR_SPECS_POWER]);
16312
16313         sar_specs->sub_specs[index].freq_range_index = range_index;
16314
16315         return 0;
16316 }
16317
16318 static int nl80211_set_sar_specs(struct sk_buff *skb, struct genl_info *info)
16319 {
16320         struct cfg80211_registered_device *rdev = info->user_ptr[0];
16321         struct nlattr *spec[NL80211_SAR_ATTR_SPECS_MAX + 1];
16322         struct nlattr *tb[NL80211_SAR_ATTR_MAX + 1];
16323         struct cfg80211_sar_specs *sar_spec;
16324         enum nl80211_sar_type type;
16325         struct nlattr *spec_list;
16326         u32 specs;
16327         int rem, err;
16328
16329         if (!rdev->wiphy.sar_capa || !rdev->ops->set_sar_specs)
16330                 return -EOPNOTSUPP;
16331
16332         if (!info->attrs[NL80211_ATTR_SAR_SPEC])
16333                 return -EINVAL;
16334
16335         nla_parse_nested(tb, NL80211_SAR_ATTR_MAX,
16336                          info->attrs[NL80211_ATTR_SAR_SPEC],
16337                          NULL, NULL);
16338
16339         if (!tb[NL80211_SAR_ATTR_TYPE] || !tb[NL80211_SAR_ATTR_SPECS])
16340                 return -EINVAL;
16341
16342         type = nla_get_u32(tb[NL80211_SAR_ATTR_TYPE]);
16343         if (type != rdev->wiphy.sar_capa->type)
16344                 return -EINVAL;
16345
16346         specs = 0;
16347         nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem)
16348                 specs++;
16349
16350         if (specs > rdev->wiphy.sar_capa->num_freq_ranges)
16351                 return -EINVAL;
16352
16353         sar_spec = kzalloc(struct_size(sar_spec, sub_specs, specs), GFP_KERNEL);
16354         if (!sar_spec)
16355                 return -ENOMEM;
16356
16357         sar_spec->type = type;
16358         specs = 0;
16359         nla_for_each_nested(spec_list, tb[NL80211_SAR_ATTR_SPECS], rem) {
16360                 nla_parse_nested(spec, NL80211_SAR_ATTR_SPECS_MAX,
16361                                  spec_list, NULL, NULL);
16362
16363                 switch (type) {
16364                 case NL80211_SAR_TYPE_POWER:
16365                         if (nl80211_set_sar_sub_specs(rdev, sar_spec,
16366                                                       spec, specs)) {
16367                                 err = -EINVAL;
16368                                 goto error;
16369                         }
16370                         break;
16371                 default:
16372                         err = -EINVAL;
16373                         goto error;
16374                 }
16375                 specs++;
16376         }
16377
16378         sar_spec->num_sub_specs = specs;
16379
16380         rdev->cur_cmd_info = info;
16381         err = rdev_set_sar_specs(rdev, sar_spec);
16382         rdev->cur_cmd_info = NULL;
16383 error:
16384         kfree(sar_spec);
16385         return err;
16386 }
16387
16388 #define SELECTOR(__sel, name, value) \
16389         ((__sel) == (value)) ? NL80211_IFL_SEL_##name :
16390 int __missing_selector(void);
16391 #define IFLAGS(__val) INTERNAL_FLAG_SELECTORS(__val) __missing_selector()
16392
16393 static const struct genl_ops nl80211_ops[] = {
16394         {
16395                 .cmd = NL80211_CMD_GET_WIPHY,
16396                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16397                 .doit = nl80211_get_wiphy,
16398                 .dumpit = nl80211_dump_wiphy,
16399                 .done = nl80211_dump_wiphy_done,
16400                 /* can be retrieved by unprivileged users */
16401                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16402         },
16403 };
16404
16405 static const struct genl_small_ops nl80211_small_ops[] = {
16406         {
16407                 .cmd = NL80211_CMD_SET_WIPHY,
16408                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16409                 .doit = nl80211_set_wiphy,
16410                 .flags = GENL_UNS_ADMIN_PERM,
16411         },
16412         {
16413                 .cmd = NL80211_CMD_GET_INTERFACE,
16414                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16415                 .doit = nl80211_get_interface,
16416                 .dumpit = nl80211_dump_interface,
16417                 /* can be retrieved by unprivileged users */
16418                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16419         },
16420         {
16421                 .cmd = NL80211_CMD_SET_INTERFACE,
16422                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16423                 .doit = nl80211_set_interface,
16424                 .flags = GENL_UNS_ADMIN_PERM,
16425                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16426                                          NL80211_FLAG_NEED_RTNL),
16427         },
16428         {
16429                 .cmd = NL80211_CMD_NEW_INTERFACE,
16430                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16431                 .doit = nl80211_new_interface,
16432                 .flags = GENL_UNS_ADMIN_PERM,
16433                 .internal_flags =
16434                         IFLAGS(NL80211_FLAG_NEED_WIPHY |
16435                                NL80211_FLAG_NEED_RTNL |
16436                                /* we take the wiphy mutex later ourselves */
16437                                NL80211_FLAG_NO_WIPHY_MTX),
16438         },
16439         {
16440                 .cmd = NL80211_CMD_DEL_INTERFACE,
16441                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16442                 .doit = nl80211_del_interface,
16443                 .flags = GENL_UNS_ADMIN_PERM,
16444                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16445                                          NL80211_FLAG_NEED_RTNL),
16446         },
16447         {
16448                 .cmd = NL80211_CMD_GET_KEY,
16449                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16450                 .doit = nl80211_get_key,
16451                 .flags = GENL_UNS_ADMIN_PERM,
16452                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16453         },
16454         {
16455                 .cmd = NL80211_CMD_SET_KEY,
16456                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16457                 .doit = nl80211_set_key,
16458                 .flags = GENL_UNS_ADMIN_PERM,
16459                 /* cannot use NL80211_FLAG_MLO_VALID_LINK_ID, depends on key */
16460                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16461                                          NL80211_FLAG_CLEAR_SKB),
16462         },
16463         {
16464                 .cmd = NL80211_CMD_NEW_KEY,
16465                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16466                 .doit = nl80211_new_key,
16467                 .flags = GENL_UNS_ADMIN_PERM,
16468                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16469                                          NL80211_FLAG_CLEAR_SKB),
16470         },
16471         {
16472                 .cmd = NL80211_CMD_DEL_KEY,
16473                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16474                 .doit = nl80211_del_key,
16475                 .flags = GENL_UNS_ADMIN_PERM,
16476                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16477         },
16478         {
16479                 .cmd = NL80211_CMD_SET_BEACON,
16480                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16481                 .flags = GENL_UNS_ADMIN_PERM,
16482                 .doit = nl80211_set_beacon,
16483                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16484                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16485         },
16486         {
16487                 .cmd = NL80211_CMD_START_AP,
16488                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16489                 .flags = GENL_UNS_ADMIN_PERM,
16490                 .doit = nl80211_start_ap,
16491                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16492                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16493         },
16494         {
16495                 .cmd = NL80211_CMD_STOP_AP,
16496                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16497                 .flags = GENL_UNS_ADMIN_PERM,
16498                 .doit = nl80211_stop_ap,
16499                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16500                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16501         },
16502         {
16503                 .cmd = NL80211_CMD_GET_STATION,
16504                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16505                 .doit = nl80211_get_station,
16506                 .dumpit = nl80211_dump_station,
16507                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16508         },
16509         {
16510                 .cmd = NL80211_CMD_SET_STATION,
16511                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16512                 .doit = nl80211_set_station,
16513                 .flags = GENL_UNS_ADMIN_PERM,
16514                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16515         },
16516         {
16517                 .cmd = NL80211_CMD_NEW_STATION,
16518                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16519                 .doit = nl80211_new_station,
16520                 .flags = GENL_UNS_ADMIN_PERM,
16521                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16522         },
16523         {
16524                 .cmd = NL80211_CMD_DEL_STATION,
16525                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16526                 .doit = nl80211_del_station,
16527                 .flags = GENL_UNS_ADMIN_PERM,
16528                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16529         },
16530         {
16531                 .cmd = NL80211_CMD_GET_MPATH,
16532                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16533                 .doit = nl80211_get_mpath,
16534                 .dumpit = nl80211_dump_mpath,
16535                 .flags = GENL_UNS_ADMIN_PERM,
16536                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16537         },
16538         {
16539                 .cmd = NL80211_CMD_GET_MPP,
16540                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16541                 .doit = nl80211_get_mpp,
16542                 .dumpit = nl80211_dump_mpp,
16543                 .flags = GENL_UNS_ADMIN_PERM,
16544                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16545         },
16546         {
16547                 .cmd = NL80211_CMD_SET_MPATH,
16548                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16549                 .doit = nl80211_set_mpath,
16550                 .flags = GENL_UNS_ADMIN_PERM,
16551                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16552         },
16553         {
16554                 .cmd = NL80211_CMD_NEW_MPATH,
16555                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16556                 .doit = nl80211_new_mpath,
16557                 .flags = GENL_UNS_ADMIN_PERM,
16558                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16559         },
16560         {
16561                 .cmd = NL80211_CMD_DEL_MPATH,
16562                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16563                 .doit = nl80211_del_mpath,
16564                 .flags = GENL_UNS_ADMIN_PERM,
16565                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16566         },
16567         {
16568                 .cmd = NL80211_CMD_SET_BSS,
16569                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16570                 .doit = nl80211_set_bss,
16571                 .flags = GENL_UNS_ADMIN_PERM,
16572                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16573         },
16574         {
16575                 .cmd = NL80211_CMD_GET_REG,
16576                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16577                 .doit = nl80211_get_reg_do,
16578                 .dumpit = nl80211_get_reg_dump,
16579                 /* can be retrieved by unprivileged users */
16580         },
16581 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
16582         {
16583                 .cmd = NL80211_CMD_SET_REG,
16584                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16585                 .doit = nl80211_set_reg,
16586                 .flags = GENL_ADMIN_PERM,
16587         },
16588 #endif
16589         {
16590                 .cmd = NL80211_CMD_REQ_SET_REG,
16591                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16592                 .doit = nl80211_req_set_reg,
16593                 .flags = GENL_ADMIN_PERM,
16594         },
16595         {
16596                 .cmd = NL80211_CMD_RELOAD_REGDB,
16597                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16598                 .doit = nl80211_reload_regdb,
16599                 .flags = GENL_ADMIN_PERM,
16600         },
16601         {
16602                 .cmd = NL80211_CMD_GET_MESH_CONFIG,
16603                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16604                 .doit = nl80211_get_mesh_config,
16605                 /* can be retrieved by unprivileged users */
16606                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16607         },
16608         {
16609                 .cmd = NL80211_CMD_SET_MESH_CONFIG,
16610                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16611                 .doit = nl80211_update_mesh_config,
16612                 .flags = GENL_UNS_ADMIN_PERM,
16613                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16614         },
16615         {
16616                 .cmd = NL80211_CMD_TRIGGER_SCAN,
16617                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16618                 .doit = nl80211_trigger_scan,
16619                 .flags = GENL_UNS_ADMIN_PERM,
16620                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16621         },
16622         {
16623                 .cmd = NL80211_CMD_ABORT_SCAN,
16624                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16625                 .doit = nl80211_abort_scan,
16626                 .flags = GENL_UNS_ADMIN_PERM,
16627                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16628         },
16629         {
16630                 .cmd = NL80211_CMD_GET_SCAN,
16631                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16632                 .dumpit = nl80211_dump_scan,
16633         },
16634         {
16635                 .cmd = NL80211_CMD_START_SCHED_SCAN,
16636                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16637                 .doit = nl80211_start_sched_scan,
16638                 .flags = GENL_UNS_ADMIN_PERM,
16639                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16640         },
16641         {
16642                 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
16643                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16644                 .doit = nl80211_stop_sched_scan,
16645                 .flags = GENL_UNS_ADMIN_PERM,
16646                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16647         },
16648         {
16649                 .cmd = NL80211_CMD_AUTHENTICATE,
16650                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16651                 .doit = nl80211_authenticate,
16652                 .flags = GENL_UNS_ADMIN_PERM,
16653                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16654                                          NL80211_FLAG_CLEAR_SKB),
16655         },
16656         {
16657                 .cmd = NL80211_CMD_ASSOCIATE,
16658                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16659                 .doit = nl80211_associate,
16660                 .flags = GENL_UNS_ADMIN_PERM,
16661                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16662                                          NL80211_FLAG_CLEAR_SKB),
16663         },
16664         {
16665                 .cmd = NL80211_CMD_DEAUTHENTICATE,
16666                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16667                 .doit = nl80211_deauthenticate,
16668                 .flags = GENL_UNS_ADMIN_PERM,
16669                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16670         },
16671         {
16672                 .cmd = NL80211_CMD_DISASSOCIATE,
16673                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16674                 .doit = nl80211_disassociate,
16675                 .flags = GENL_UNS_ADMIN_PERM,
16676                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16677         },
16678         {
16679                 .cmd = NL80211_CMD_JOIN_IBSS,
16680                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16681                 .doit = nl80211_join_ibss,
16682                 .flags = GENL_UNS_ADMIN_PERM,
16683                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16684         },
16685         {
16686                 .cmd = NL80211_CMD_LEAVE_IBSS,
16687                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16688                 .doit = nl80211_leave_ibss,
16689                 .flags = GENL_UNS_ADMIN_PERM,
16690                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16691         },
16692 #ifdef CONFIG_NL80211_TESTMODE
16693         {
16694                 .cmd = NL80211_CMD_TESTMODE,
16695                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16696                 .doit = nl80211_testmode_do,
16697                 .dumpit = nl80211_testmode_dump,
16698                 .flags = GENL_UNS_ADMIN_PERM,
16699                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16700         },
16701 #endif
16702         {
16703                 .cmd = NL80211_CMD_CONNECT,
16704                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16705                 .doit = nl80211_connect,
16706                 .flags = GENL_UNS_ADMIN_PERM,
16707                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16708                                          NL80211_FLAG_CLEAR_SKB),
16709         },
16710         {
16711                 .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
16712                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16713                 .doit = nl80211_update_connect_params,
16714                 .flags = GENL_ADMIN_PERM,
16715                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16716                                          NL80211_FLAG_CLEAR_SKB),
16717         },
16718         {
16719                 .cmd = NL80211_CMD_DISCONNECT,
16720                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16721                 .doit = nl80211_disconnect,
16722                 .flags = GENL_UNS_ADMIN_PERM,
16723                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16724         },
16725         {
16726                 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
16727                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16728                 .doit = nl80211_wiphy_netns,
16729                 .flags = GENL_UNS_ADMIN_PERM,
16730                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
16731                                          NL80211_FLAG_NEED_RTNL |
16732                                          NL80211_FLAG_NO_WIPHY_MTX),
16733         },
16734         {
16735                 .cmd = NL80211_CMD_GET_SURVEY,
16736                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16737                 .dumpit = nl80211_dump_survey,
16738         },
16739         {
16740                 .cmd = NL80211_CMD_SET_PMKSA,
16741                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16742                 .doit = nl80211_setdel_pmksa,
16743                 .flags = GENL_UNS_ADMIN_PERM,
16744                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16745                                          NL80211_FLAG_CLEAR_SKB),
16746         },
16747         {
16748                 .cmd = NL80211_CMD_DEL_PMKSA,
16749                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16750                 .doit = nl80211_setdel_pmksa,
16751                 .flags = GENL_UNS_ADMIN_PERM,
16752                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16753         },
16754         {
16755                 .cmd = NL80211_CMD_FLUSH_PMKSA,
16756                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16757                 .doit = nl80211_flush_pmksa,
16758                 .flags = GENL_UNS_ADMIN_PERM,
16759                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16760         },
16761         {
16762                 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
16763                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16764                 .doit = nl80211_remain_on_channel,
16765                 .flags = GENL_UNS_ADMIN_PERM,
16766                 /* FIXME: requiring a link ID here is probably not good */
16767                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16768                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16769         },
16770         {
16771                 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
16772                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16773                 .doit = nl80211_cancel_remain_on_channel,
16774                 .flags = GENL_UNS_ADMIN_PERM,
16775                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16776         },
16777         {
16778                 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
16779                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16780                 .doit = nl80211_set_tx_bitrate_mask,
16781                 .flags = GENL_UNS_ADMIN_PERM,
16782                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16783                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16784         },
16785         {
16786                 .cmd = NL80211_CMD_REGISTER_FRAME,
16787                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16788                 .doit = nl80211_register_mgmt,
16789                 .flags = GENL_UNS_ADMIN_PERM,
16790                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV),
16791         },
16792         {
16793                 .cmd = NL80211_CMD_FRAME,
16794                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16795                 .doit = nl80211_tx_mgmt,
16796                 .flags = GENL_UNS_ADMIN_PERM,
16797                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16798         },
16799         {
16800                 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
16801                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16802                 .doit = nl80211_tx_mgmt_cancel_wait,
16803                 .flags = GENL_UNS_ADMIN_PERM,
16804                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16805         },
16806         {
16807                 .cmd = NL80211_CMD_SET_POWER_SAVE,
16808                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16809                 .doit = nl80211_set_power_save,
16810                 .flags = GENL_UNS_ADMIN_PERM,
16811                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16812         },
16813         {
16814                 .cmd = NL80211_CMD_GET_POWER_SAVE,
16815                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16816                 .doit = nl80211_get_power_save,
16817                 /* can be retrieved by unprivileged users */
16818                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16819         },
16820         {
16821                 .cmd = NL80211_CMD_SET_CQM,
16822                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16823                 .doit = nl80211_set_cqm,
16824                 .flags = GENL_UNS_ADMIN_PERM,
16825                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16826         },
16827         {
16828                 .cmd = NL80211_CMD_SET_CHANNEL,
16829                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16830                 .doit = nl80211_set_channel,
16831                 .flags = GENL_UNS_ADMIN_PERM,
16832                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16833                                          NL80211_FLAG_MLO_VALID_LINK_ID),
16834         },
16835         {
16836                 .cmd = NL80211_CMD_JOIN_MESH,
16837                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16838                 .doit = nl80211_join_mesh,
16839                 .flags = GENL_UNS_ADMIN_PERM,
16840                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16841         },
16842         {
16843                 .cmd = NL80211_CMD_LEAVE_MESH,
16844                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16845                 .doit = nl80211_leave_mesh,
16846                 .flags = GENL_UNS_ADMIN_PERM,
16847                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16848         },
16849         {
16850                 .cmd = NL80211_CMD_JOIN_OCB,
16851                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16852                 .doit = nl80211_join_ocb,
16853                 .flags = GENL_UNS_ADMIN_PERM,
16854                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16855         },
16856         {
16857                 .cmd = NL80211_CMD_LEAVE_OCB,
16858                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16859                 .doit = nl80211_leave_ocb,
16860                 .flags = GENL_UNS_ADMIN_PERM,
16861                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16862         },
16863 #ifdef CONFIG_PM
16864         {
16865                 .cmd = NL80211_CMD_GET_WOWLAN,
16866                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16867                 .doit = nl80211_get_wowlan,
16868                 /* can be retrieved by unprivileged users */
16869                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16870         },
16871         {
16872                 .cmd = NL80211_CMD_SET_WOWLAN,
16873                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16874                 .doit = nl80211_set_wowlan,
16875                 .flags = GENL_UNS_ADMIN_PERM,
16876                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16877         },
16878 #endif
16879         {
16880                 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
16881                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16882                 .doit = nl80211_set_rekey_data,
16883                 .flags = GENL_UNS_ADMIN_PERM,
16884                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
16885                                          NL80211_FLAG_CLEAR_SKB),
16886         },
16887         {
16888                 .cmd = NL80211_CMD_TDLS_MGMT,
16889                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16890                 .doit = nl80211_tdls_mgmt,
16891                 .flags = GENL_UNS_ADMIN_PERM,
16892                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16893         },
16894         {
16895                 .cmd = NL80211_CMD_TDLS_OPER,
16896                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16897                 .doit = nl80211_tdls_oper,
16898                 .flags = GENL_UNS_ADMIN_PERM,
16899                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16900         },
16901         {
16902                 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
16903                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16904                 .doit = nl80211_register_unexpected_frame,
16905                 .flags = GENL_UNS_ADMIN_PERM,
16906                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16907         },
16908         {
16909                 .cmd = NL80211_CMD_PROBE_CLIENT,
16910                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16911                 .doit = nl80211_probe_client,
16912                 .flags = GENL_UNS_ADMIN_PERM,
16913                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
16914         },
16915         {
16916                 .cmd = NL80211_CMD_REGISTER_BEACONS,
16917                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16918                 .doit = nl80211_register_beacons,
16919                 .flags = GENL_UNS_ADMIN_PERM,
16920                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
16921         },
16922         {
16923                 .cmd = NL80211_CMD_SET_NOACK_MAP,
16924                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16925                 .doit = nl80211_set_noack_map,
16926                 .flags = GENL_UNS_ADMIN_PERM,
16927                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16928         },
16929         {
16930                 .cmd = NL80211_CMD_START_P2P_DEVICE,
16931                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16932                 .doit = nl80211_start_p2p_device,
16933                 .flags = GENL_UNS_ADMIN_PERM,
16934                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16935                                          NL80211_FLAG_NEED_RTNL),
16936         },
16937         {
16938                 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
16939                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16940                 .doit = nl80211_stop_p2p_device,
16941                 .flags = GENL_UNS_ADMIN_PERM,
16942                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16943                                          NL80211_FLAG_NEED_RTNL),
16944         },
16945         {
16946                 .cmd = NL80211_CMD_START_NAN,
16947                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16948                 .doit = nl80211_start_nan,
16949                 .flags = GENL_ADMIN_PERM,
16950                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV |
16951                                          NL80211_FLAG_NEED_RTNL),
16952         },
16953         {
16954                 .cmd = NL80211_CMD_STOP_NAN,
16955                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16956                 .doit = nl80211_stop_nan,
16957                 .flags = GENL_ADMIN_PERM,
16958                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP |
16959                                          NL80211_FLAG_NEED_RTNL),
16960         },
16961         {
16962                 .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
16963                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16964                 .doit = nl80211_nan_add_func,
16965                 .flags = GENL_ADMIN_PERM,
16966                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16967         },
16968         {
16969                 .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
16970                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16971                 .doit = nl80211_nan_del_func,
16972                 .flags = GENL_ADMIN_PERM,
16973                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16974         },
16975         {
16976                 .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
16977                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16978                 .doit = nl80211_nan_change_config,
16979                 .flags = GENL_ADMIN_PERM,
16980                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
16981         },
16982         {
16983                 .cmd = NL80211_CMD_SET_MCAST_RATE,
16984                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16985                 .doit = nl80211_set_mcast_rate,
16986                 .flags = GENL_UNS_ADMIN_PERM,
16987                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
16988         },
16989         {
16990                 .cmd = NL80211_CMD_SET_MAC_ACL,
16991                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
16992                 .doit = nl80211_set_mac_acl,
16993                 .flags = GENL_UNS_ADMIN_PERM,
16994                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
16995                                          NL80211_FLAG_MLO_UNSUPPORTED),
16996         },
16997         {
16998                 .cmd = NL80211_CMD_RADAR_DETECT,
16999                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17000                 .doit = nl80211_start_radar_detection,
17001                 .flags = GENL_UNS_ADMIN_PERM,
17002                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17003                                          NL80211_FLAG_NO_WIPHY_MTX |
17004                                          NL80211_FLAG_MLO_UNSUPPORTED),
17005         },
17006         {
17007                 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
17008                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17009                 .doit = nl80211_get_protocol_features,
17010         },
17011         {
17012                 .cmd = NL80211_CMD_UPDATE_FT_IES,
17013                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17014                 .doit = nl80211_update_ft_ies,
17015                 .flags = GENL_UNS_ADMIN_PERM,
17016                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17017         },
17018         {
17019                 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
17020                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17021                 .doit = nl80211_crit_protocol_start,
17022                 .flags = GENL_UNS_ADMIN_PERM,
17023                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17024         },
17025         {
17026                 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
17027                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17028                 .doit = nl80211_crit_protocol_stop,
17029                 .flags = GENL_UNS_ADMIN_PERM,
17030                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17031         },
17032         {
17033                 .cmd = NL80211_CMD_GET_COALESCE,
17034                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17035                 .doit = nl80211_get_coalesce,
17036                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17037         },
17038         {
17039                 .cmd = NL80211_CMD_SET_COALESCE,
17040                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17041                 .doit = nl80211_set_coalesce,
17042                 .flags = GENL_UNS_ADMIN_PERM,
17043                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY),
17044         },
17045         {
17046                 .cmd = NL80211_CMD_CHANNEL_SWITCH,
17047                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17048                 .doit = nl80211_channel_switch,
17049                 .flags = GENL_UNS_ADMIN_PERM,
17050                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17051                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17052         },
17053         {
17054                 .cmd = NL80211_CMD_VENDOR,
17055                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17056                 .doit = nl80211_vendor_cmd,
17057                 .dumpit = nl80211_vendor_cmd_dump,
17058                 .flags = GENL_UNS_ADMIN_PERM,
17059                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17060                                          NL80211_FLAG_CLEAR_SKB),
17061         },
17062         {
17063                 .cmd = NL80211_CMD_SET_QOS_MAP,
17064                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17065                 .doit = nl80211_set_qos_map,
17066                 .flags = GENL_UNS_ADMIN_PERM,
17067                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17068         },
17069         {
17070                 .cmd = NL80211_CMD_ADD_TX_TS,
17071                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17072                 .doit = nl80211_add_tx_ts,
17073                 .flags = GENL_UNS_ADMIN_PERM,
17074                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17075                                          NL80211_FLAG_MLO_UNSUPPORTED),
17076         },
17077         {
17078                 .cmd = NL80211_CMD_DEL_TX_TS,
17079                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17080                 .doit = nl80211_del_tx_ts,
17081                 .flags = GENL_UNS_ADMIN_PERM,
17082                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17083         },
17084         {
17085                 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
17086                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17087                 .doit = nl80211_tdls_channel_switch,
17088                 .flags = GENL_UNS_ADMIN_PERM,
17089                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17090         },
17091         {
17092                 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
17093                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17094                 .doit = nl80211_tdls_cancel_channel_switch,
17095                 .flags = GENL_UNS_ADMIN_PERM,
17096                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17097         },
17098         {
17099                 .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
17100                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17101                 .doit = nl80211_set_multicast_to_unicast,
17102                 .flags = GENL_UNS_ADMIN_PERM,
17103                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV),
17104         },
17105         {
17106                 .cmd = NL80211_CMD_SET_PMK,
17107                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17108                 .doit = nl80211_set_pmk,
17109                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17110                                          NL80211_FLAG_CLEAR_SKB),
17111         },
17112         {
17113                 .cmd = NL80211_CMD_DEL_PMK,
17114                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17115                 .doit = nl80211_del_pmk,
17116                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17117         },
17118         {
17119                 .cmd = NL80211_CMD_EXTERNAL_AUTH,
17120                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17121                 .doit = nl80211_external_auth,
17122                 .flags = GENL_ADMIN_PERM,
17123                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17124         },
17125         {
17126                 .cmd = NL80211_CMD_CONTROL_PORT_FRAME,
17127                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17128                 .doit = nl80211_tx_control_port,
17129                 .flags = GENL_UNS_ADMIN_PERM,
17130                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17131         },
17132         {
17133                 .cmd = NL80211_CMD_GET_FTM_RESPONDER_STATS,
17134                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17135                 .doit = nl80211_get_ftm_responder_stats,
17136                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17137                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17138         },
17139         {
17140                 .cmd = NL80211_CMD_PEER_MEASUREMENT_START,
17141                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17142                 .doit = nl80211_pmsr_start,
17143                 .flags = GENL_UNS_ADMIN_PERM,
17144                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WDEV_UP),
17145         },
17146         {
17147                 .cmd = NL80211_CMD_NOTIFY_RADAR,
17148                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17149                 .doit = nl80211_notify_radar_detection,
17150                 .flags = GENL_UNS_ADMIN_PERM,
17151                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17152         },
17153         {
17154                 .cmd = NL80211_CMD_UPDATE_OWE_INFO,
17155                 .doit = nl80211_update_owe_info,
17156                 .flags = GENL_ADMIN_PERM,
17157                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17158         },
17159         {
17160                 .cmd = NL80211_CMD_PROBE_MESH_LINK,
17161                 .doit = nl80211_probe_mesh_link,
17162                 .flags = GENL_UNS_ADMIN_PERM,
17163                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17164         },
17165         {
17166                 .cmd = NL80211_CMD_SET_TID_CONFIG,
17167                 .doit = nl80211_set_tid_config,
17168                 .flags = GENL_UNS_ADMIN_PERM,
17169                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV |
17170                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17171         },
17172         {
17173                 .cmd = NL80211_CMD_SET_SAR_SPECS,
17174                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17175                 .doit = nl80211_set_sar_specs,
17176                 .flags = GENL_UNS_ADMIN_PERM,
17177                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_WIPHY |
17178                                          NL80211_FLAG_NEED_RTNL),
17179         },
17180         {
17181                 .cmd = NL80211_CMD_COLOR_CHANGE_REQUEST,
17182                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17183                 .doit = nl80211_color_change,
17184                 .flags = GENL_UNS_ADMIN_PERM,
17185                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17186         },
17187         {
17188                 .cmd = NL80211_CMD_SET_FILS_AAD,
17189                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
17190                 .doit = nl80211_set_fils_aad,
17191                 .flags = GENL_UNS_ADMIN_PERM,
17192                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17193         },
17194         {
17195                 .cmd = NL80211_CMD_ADD_LINK,
17196                 .doit = nl80211_add_link,
17197                 .flags = GENL_UNS_ADMIN_PERM,
17198                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP),
17199         },
17200         {
17201                 .cmd = NL80211_CMD_REMOVE_LINK,
17202                 .doit = nl80211_remove_link,
17203                 .flags = GENL_UNS_ADMIN_PERM,
17204                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17205                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17206         },
17207         {
17208                 .cmd = NL80211_CMD_ADD_LINK_STA,
17209                 .doit = nl80211_add_link_station,
17210                 .flags = GENL_UNS_ADMIN_PERM,
17211                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17212                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17213         },
17214         {
17215                 .cmd = NL80211_CMD_MODIFY_LINK_STA,
17216                 .doit = nl80211_modify_link_station,
17217                 .flags = GENL_UNS_ADMIN_PERM,
17218                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17219                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17220         },
17221         {
17222                 .cmd = NL80211_CMD_REMOVE_LINK_STA,
17223                 .doit = nl80211_remove_link_station,
17224                 .flags = GENL_UNS_ADMIN_PERM,
17225                 .internal_flags = IFLAGS(NL80211_FLAG_NEED_NETDEV_UP |
17226                                          NL80211_FLAG_MLO_VALID_LINK_ID),
17227         },
17228 };
17229
17230 static struct genl_family nl80211_fam __ro_after_init = {
17231         .name = NL80211_GENL_NAME,      /* have users key off the name instead */
17232         .hdrsize = 0,                   /* no private header */
17233         .version = 1,                   /* no particular meaning now */
17234         .maxattr = NL80211_ATTR_MAX,
17235         .policy = nl80211_policy,
17236         .netnsok = true,
17237         .pre_doit = nl80211_pre_doit,
17238         .post_doit = nl80211_post_doit,
17239         .module = THIS_MODULE,
17240         .ops = nl80211_ops,
17241         .n_ops = ARRAY_SIZE(nl80211_ops),
17242         .small_ops = nl80211_small_ops,
17243         .n_small_ops = ARRAY_SIZE(nl80211_small_ops),
17244         .resv_start_op = NL80211_CMD_REMOVE_LINK_STA + 1,
17245         .mcgrps = nl80211_mcgrps,
17246         .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
17247         .parallel_ops = true,
17248 };
17249
17250 /* notification functions */
17251
17252 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
17253                           enum nl80211_commands cmd)
17254 {
17255         struct sk_buff *msg;
17256         struct nl80211_dump_wiphy_state state = {};
17257
17258         WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
17259                 cmd != NL80211_CMD_DEL_WIPHY);
17260
17261         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17262         if (!msg)
17263                 return;
17264
17265         if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
17266                 nlmsg_free(msg);
17267                 return;
17268         }
17269
17270         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17271                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
17272 }
17273
17274 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
17275                                 struct wireless_dev *wdev,
17276                                 enum nl80211_commands cmd)
17277 {
17278         struct sk_buff *msg;
17279
17280         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17281         if (!msg)
17282                 return;
17283
17284         if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev, cmd) < 0) {
17285                 nlmsg_free(msg);
17286                 return;
17287         }
17288
17289         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17290                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
17291 }
17292
17293 static int nl80211_add_scan_req(struct sk_buff *msg,
17294                                 struct cfg80211_registered_device *rdev)
17295 {
17296         struct cfg80211_scan_request *req = rdev->scan_req;
17297         struct nlattr *nest;
17298         int i;
17299         struct cfg80211_scan_info *info;
17300
17301         if (WARN_ON(!req))
17302                 return 0;
17303
17304         nest = nla_nest_start_noflag(msg, NL80211_ATTR_SCAN_SSIDS);
17305         if (!nest)
17306                 goto nla_put_failure;
17307         for (i = 0; i < req->n_ssids; i++) {
17308                 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
17309                         goto nla_put_failure;
17310         }
17311         nla_nest_end(msg, nest);
17312
17313         if (req->flags & NL80211_SCAN_FLAG_FREQ_KHZ) {
17314                 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQ_KHZ);
17315                 if (!nest)
17316                         goto nla_put_failure;
17317                 for (i = 0; i < req->n_channels; i++) {
17318                         if (nla_put_u32(msg, i,
17319                                    ieee80211_channel_to_khz(req->channels[i])))
17320                                 goto nla_put_failure;
17321                 }
17322                 nla_nest_end(msg, nest);
17323         } else {
17324                 nest = nla_nest_start_noflag(msg,
17325                                              NL80211_ATTR_SCAN_FREQUENCIES);
17326                 if (!nest)
17327                         goto nla_put_failure;
17328                 for (i = 0; i < req->n_channels; i++) {
17329                         if (nla_put_u32(msg, i, req->channels[i]->center_freq))
17330                                 goto nla_put_failure;
17331                 }
17332                 nla_nest_end(msg, nest);
17333         }
17334
17335         if (req->ie &&
17336             nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
17337                 goto nla_put_failure;
17338
17339         if (req->flags &&
17340             nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
17341                 goto nla_put_failure;
17342
17343         info = rdev->int_scan_req ? &rdev->int_scan_req->info :
17344                 &rdev->scan_req->info;
17345         if (info->scan_start_tsf &&
17346             (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
17347                                info->scan_start_tsf, NL80211_BSS_PAD) ||
17348              nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
17349                      info->tsf_bssid)))
17350                 goto nla_put_failure;
17351
17352         return 0;
17353  nla_put_failure:
17354         return -ENOBUFS;
17355 }
17356
17357 static int nl80211_prep_scan_msg(struct sk_buff *msg,
17358                                  struct cfg80211_registered_device *rdev,
17359                                  struct wireless_dev *wdev,
17360                                  u32 portid, u32 seq, int flags,
17361                                  u32 cmd)
17362 {
17363         void *hdr;
17364
17365         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
17366         if (!hdr)
17367                 return -1;
17368
17369         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17370             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
17371                                          wdev->netdev->ifindex)) ||
17372             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
17373                               NL80211_ATTR_PAD))
17374                 goto nla_put_failure;
17375
17376         /* ignore errors and send incomplete event anyway */
17377         nl80211_add_scan_req(msg, rdev);
17378
17379         genlmsg_end(msg, hdr);
17380         return 0;
17381
17382  nla_put_failure:
17383         genlmsg_cancel(msg, hdr);
17384         return -EMSGSIZE;
17385 }
17386
17387 static int
17388 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
17389                             struct cfg80211_sched_scan_request *req, u32 cmd)
17390 {
17391         void *hdr;
17392
17393         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17394         if (!hdr)
17395                 return -1;
17396
17397         if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
17398                         wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
17399             nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
17400             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
17401                               NL80211_ATTR_PAD))
17402                 goto nla_put_failure;
17403
17404         genlmsg_end(msg, hdr);
17405         return 0;
17406
17407  nla_put_failure:
17408         genlmsg_cancel(msg, hdr);
17409         return -EMSGSIZE;
17410 }
17411
17412 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
17413                              struct wireless_dev *wdev)
17414 {
17415         struct sk_buff *msg;
17416
17417         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17418         if (!msg)
17419                 return;
17420
17421         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17422                                   NL80211_CMD_TRIGGER_SCAN) < 0) {
17423                 nlmsg_free(msg);
17424                 return;
17425         }
17426
17427         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17428                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
17429 }
17430
17431 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
17432                                        struct wireless_dev *wdev, bool aborted)
17433 {
17434         struct sk_buff *msg;
17435
17436         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17437         if (!msg)
17438                 return NULL;
17439
17440         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
17441                                   aborted ? NL80211_CMD_SCAN_ABORTED :
17442                                             NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
17443                 nlmsg_free(msg);
17444                 return NULL;
17445         }
17446
17447         return msg;
17448 }
17449
17450 /* send message created by nl80211_build_scan_msg() */
17451 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
17452                            struct sk_buff *msg)
17453 {
17454         if (!msg)
17455                 return;
17456
17457         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17458                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
17459 }
17460
17461 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
17462 {
17463         struct sk_buff *msg;
17464
17465         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17466         if (!msg)
17467                 return;
17468
17469         if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
17470                 nlmsg_free(msg);
17471                 return;
17472         }
17473
17474         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
17475                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
17476 }
17477
17478 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
17479                                           struct regulatory_request *request)
17480 {
17481         /* Userspace can always count this one always being set */
17482         if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
17483                 goto nla_put_failure;
17484
17485         if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
17486                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17487                                NL80211_REGDOM_TYPE_WORLD))
17488                         goto nla_put_failure;
17489         } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
17490                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17491                                NL80211_REGDOM_TYPE_CUSTOM_WORLD))
17492                         goto nla_put_failure;
17493         } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
17494                    request->intersect) {
17495                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17496                                NL80211_REGDOM_TYPE_INTERSECTION))
17497                         goto nla_put_failure;
17498         } else {
17499                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
17500                                NL80211_REGDOM_TYPE_COUNTRY) ||
17501                     nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
17502                                    request->alpha2))
17503                         goto nla_put_failure;
17504         }
17505
17506         if (request->wiphy_idx != WIPHY_IDX_INVALID) {
17507                 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
17508
17509                 if (wiphy &&
17510                     nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
17511                         goto nla_put_failure;
17512
17513                 if (wiphy &&
17514                     wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
17515                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
17516                         goto nla_put_failure;
17517         }
17518
17519         return true;
17520
17521 nla_put_failure:
17522         return false;
17523 }
17524
17525 /*
17526  * This can happen on global regulatory changes or device specific settings
17527  * based on custom regulatory domains.
17528  */
17529 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
17530                                      struct regulatory_request *request)
17531 {
17532         struct sk_buff *msg;
17533         void *hdr;
17534
17535         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17536         if (!msg)
17537                 return;
17538
17539         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
17540         if (!hdr)
17541                 goto nla_put_failure;
17542
17543         if (!nl80211_reg_change_event_fill(msg, request))
17544                 goto nla_put_failure;
17545
17546         genlmsg_end(msg, hdr);
17547
17548         rcu_read_lock();
17549         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17550                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17551         rcu_read_unlock();
17552
17553         return;
17554
17555 nla_put_failure:
17556         nlmsg_free(msg);
17557 }
17558
17559 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
17560                                     struct net_device *netdev,
17561                                     const u8 *buf, size_t len,
17562                                     enum nl80211_commands cmd, gfp_t gfp,
17563                                     int uapsd_queues, const u8 *req_ies,
17564                                     size_t req_ies_len, bool reconnect)
17565 {
17566         struct sk_buff *msg;
17567         void *hdr;
17568
17569         msg = nlmsg_new(100 + len + req_ies_len, gfp);
17570         if (!msg)
17571                 return;
17572
17573         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17574         if (!hdr) {
17575                 nlmsg_free(msg);
17576                 return;
17577         }
17578
17579         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17580             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17581             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
17582             (req_ies &&
17583              nla_put(msg, NL80211_ATTR_REQ_IE, req_ies_len, req_ies)))
17584                 goto nla_put_failure;
17585
17586         if (reconnect && nla_put_flag(msg, NL80211_ATTR_RECONNECT_REQUESTED))
17587                 goto nla_put_failure;
17588
17589         if (uapsd_queues >= 0) {
17590                 struct nlattr *nla_wmm =
17591                         nla_nest_start_noflag(msg, NL80211_ATTR_STA_WME);
17592                 if (!nla_wmm)
17593                         goto nla_put_failure;
17594
17595                 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
17596                                uapsd_queues))
17597                         goto nla_put_failure;
17598
17599                 nla_nest_end(msg, nla_wmm);
17600         }
17601
17602         genlmsg_end(msg, hdr);
17603
17604         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17605                                 NL80211_MCGRP_MLME, gfp);
17606         return;
17607
17608  nla_put_failure:
17609         nlmsg_free(msg);
17610 }
17611
17612 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
17613                           struct net_device *netdev, const u8 *buf,
17614                           size_t len, gfp_t gfp)
17615 {
17616         nl80211_send_mlme_event(rdev, netdev, buf, len,
17617                                 NL80211_CMD_AUTHENTICATE, gfp, -1, NULL, 0,
17618                                 false);
17619 }
17620
17621 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
17622                            struct net_device *netdev,
17623                            struct cfg80211_rx_assoc_resp *data)
17624 {
17625         nl80211_send_mlme_event(rdev, netdev, data->buf, data->len,
17626                                 NL80211_CMD_ASSOCIATE, GFP_KERNEL,
17627                                 data->uapsd_queues,
17628                                 data->req_ies, data->req_ies_len, false);
17629 }
17630
17631 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
17632                          struct net_device *netdev, const u8 *buf,
17633                          size_t len, bool reconnect, gfp_t gfp)
17634 {
17635         nl80211_send_mlme_event(rdev, netdev, buf, len,
17636                                 NL80211_CMD_DEAUTHENTICATE, gfp, -1, NULL, 0,
17637                                 reconnect);
17638 }
17639
17640 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
17641                            struct net_device *netdev, const u8 *buf,
17642                            size_t len, bool reconnect, gfp_t gfp)
17643 {
17644         nl80211_send_mlme_event(rdev, netdev, buf, len,
17645                                 NL80211_CMD_DISASSOCIATE, gfp, -1, NULL, 0,
17646                                 reconnect);
17647 }
17648
17649 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
17650                                   size_t len)
17651 {
17652         struct wireless_dev *wdev = dev->ieee80211_ptr;
17653         struct wiphy *wiphy = wdev->wiphy;
17654         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
17655         const struct ieee80211_mgmt *mgmt = (void *)buf;
17656         u32 cmd;
17657
17658         if (WARN_ON(len < 2))
17659                 return;
17660
17661         if (ieee80211_is_deauth(mgmt->frame_control)) {
17662                 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
17663         } else if (ieee80211_is_disassoc(mgmt->frame_control)) {
17664                 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
17665         } else if (ieee80211_is_beacon(mgmt->frame_control)) {
17666                 if (wdev->unprot_beacon_reported &&
17667                     elapsed_jiffies_msecs(wdev->unprot_beacon_reported) < 10000)
17668                         return;
17669                 cmd = NL80211_CMD_UNPROT_BEACON;
17670                 wdev->unprot_beacon_reported = jiffies;
17671         } else {
17672                 return;
17673         }
17674
17675         trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
17676         nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1,
17677                                 NULL, 0, false);
17678 }
17679 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
17680
17681 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
17682                                       struct net_device *netdev, int cmd,
17683                                       const u8 *addr, gfp_t gfp)
17684 {
17685         struct sk_buff *msg;
17686         void *hdr;
17687
17688         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
17689         if (!msg)
17690                 return;
17691
17692         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
17693         if (!hdr) {
17694                 nlmsg_free(msg);
17695                 return;
17696         }
17697
17698         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17699             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17700             nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17701             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
17702                 goto nla_put_failure;
17703
17704         genlmsg_end(msg, hdr);
17705
17706         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17707                                 NL80211_MCGRP_MLME, gfp);
17708         return;
17709
17710  nla_put_failure:
17711         nlmsg_free(msg);
17712 }
17713
17714 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
17715                                struct net_device *netdev, const u8 *addr,
17716                                gfp_t gfp)
17717 {
17718         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
17719                                   addr, gfp);
17720 }
17721
17722 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
17723                                 struct net_device *netdev, const u8 *addr,
17724                                 gfp_t gfp)
17725 {
17726         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
17727                                   addr, gfp);
17728 }
17729
17730 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
17731                                  struct net_device *netdev,
17732                                  struct cfg80211_connect_resp_params *cr,
17733                                  gfp_t gfp)
17734 {
17735         struct sk_buff *msg;
17736         void *hdr;
17737         unsigned int link;
17738         size_t link_info_size = 0;
17739         const u8 *connected_addr = cr->valid_links ?
17740                                    cr->ap_mld_addr : cr->links[0].bssid;
17741
17742         if (cr->valid_links) {
17743                 for_each_valid_link(cr, link) {
17744                         /* Nested attribute header */
17745                         link_info_size += NLA_HDRLEN;
17746                         /* Link ID */
17747                         link_info_size += nla_total_size(sizeof(u8));
17748                         link_info_size += cr->links[link].addr ?
17749                                           nla_total_size(ETH_ALEN) : 0;
17750                         link_info_size += (cr->links[link].bssid ||
17751                                            cr->links[link].bss) ?
17752                                           nla_total_size(ETH_ALEN) : 0;
17753                 }
17754         }
17755
17756         msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
17757                         cr->fils.kek_len + cr->fils.pmk_len +
17758                         (cr->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
17759                         gfp);
17760         if (!msg)
17761                 return;
17762
17763         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
17764         if (!hdr) {
17765                 nlmsg_free(msg);
17766                 return;
17767         }
17768
17769         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17770             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17771             (connected_addr &&
17772              nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr)) ||
17773             nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
17774                         cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
17775                         cr->status) ||
17776             (cr->status < 0 &&
17777              (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
17778               nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
17779                           cr->timeout_reason))) ||
17780             (cr->req_ie &&
17781              nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
17782             (cr->resp_ie &&
17783              nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
17784                      cr->resp_ie)) ||
17785             (cr->fils.update_erp_next_seq_num &&
17786              nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17787                          cr->fils.erp_next_seq_num)) ||
17788             (cr->status == WLAN_STATUS_SUCCESS &&
17789              ((cr->fils.kek &&
17790                nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils.kek_len,
17791                        cr->fils.kek)) ||
17792               (cr->fils.pmk &&
17793                nla_put(msg, NL80211_ATTR_PMK, cr->fils.pmk_len, cr->fils.pmk)) ||
17794               (cr->fils.pmkid &&
17795                nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->fils.pmkid)))))
17796                 goto nla_put_failure;
17797
17798         if (cr->valid_links) {
17799                 int i = 1;
17800                 struct nlattr *nested;
17801
17802                 nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17803                 if (!nested)
17804                         goto nla_put_failure;
17805
17806                 for_each_valid_link(cr, link) {
17807                         struct nlattr *nested_mlo_links;
17808                         const u8 *bssid = cr->links[link].bss ?
17809                                           cr->links[link].bss->bssid :
17810                                           cr->links[link].bssid;
17811
17812                         nested_mlo_links = nla_nest_start(msg, i);
17813                         if (!nested_mlo_links)
17814                                 goto nla_put_failure;
17815
17816                         if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17817                             (bssid &&
17818                              nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17819                             (cr->links[link].addr &&
17820                              nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17821                                      cr->links[link].addr)))
17822                                 goto nla_put_failure;
17823
17824                         nla_nest_end(msg, nested_mlo_links);
17825                         i++;
17826                 }
17827                 nla_nest_end(msg, nested);
17828         }
17829
17830         genlmsg_end(msg, hdr);
17831
17832         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17833                                 NL80211_MCGRP_MLME, gfp);
17834         return;
17835
17836  nla_put_failure:
17837         nlmsg_free(msg);
17838 }
17839
17840 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
17841                          struct net_device *netdev,
17842                          struct cfg80211_roam_info *info, gfp_t gfp)
17843 {
17844         struct sk_buff *msg;
17845         void *hdr;
17846         size_t link_info_size = 0;
17847         unsigned int link;
17848         const u8 *connected_addr = info->ap_mld_addr ?
17849                                    info->ap_mld_addr :
17850                                    (info->links[0].bss ?
17851                                     info->links[0].bss->bssid :
17852                                     info->links[0].bssid);
17853
17854         if (info->valid_links) {
17855                 for_each_valid_link(info, link) {
17856                         /* Nested attribute header */
17857                         link_info_size += NLA_HDRLEN;
17858                         /* Link ID */
17859                         link_info_size += nla_total_size(sizeof(u8));
17860                         link_info_size += info->links[link].addr ?
17861                                           nla_total_size(ETH_ALEN) : 0;
17862                         link_info_size += (info->links[link].bssid ||
17863                                            info->links[link].bss) ?
17864                                           nla_total_size(ETH_ALEN) : 0;
17865                 }
17866         }
17867
17868         msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len +
17869                         info->fils.kek_len + info->fils.pmk_len +
17870                         (info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
17871                         link_info_size, gfp);
17872         if (!msg)
17873                 return;
17874
17875         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
17876         if (!hdr) {
17877                 nlmsg_free(msg);
17878                 return;
17879         }
17880
17881         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17882             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17883             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, connected_addr) ||
17884             (info->req_ie &&
17885              nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
17886                      info->req_ie)) ||
17887             (info->resp_ie &&
17888              nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
17889                      info->resp_ie)) ||
17890             (info->fils.update_erp_next_seq_num &&
17891              nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
17892                          info->fils.erp_next_seq_num)) ||
17893             (info->fils.kek &&
17894              nla_put(msg, NL80211_ATTR_FILS_KEK, info->fils.kek_len,
17895                      info->fils.kek)) ||
17896             (info->fils.pmk &&
17897              nla_put(msg, NL80211_ATTR_PMK, info->fils.pmk_len, info->fils.pmk)) ||
17898             (info->fils.pmkid &&
17899              nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, info->fils.pmkid)))
17900                 goto nla_put_failure;
17901
17902         if (info->valid_links) {
17903                 int i = 1;
17904                 struct nlattr *nested;
17905
17906                 nested = nla_nest_start(msg, NL80211_ATTR_MLO_LINKS);
17907                 if (!nested)
17908                         goto nla_put_failure;
17909
17910                 for_each_valid_link(info, link) {
17911                         struct nlattr *nested_mlo_links;
17912                         const u8 *bssid = info->links[link].bss ?
17913                                           info->links[link].bss->bssid :
17914                                           info->links[link].bssid;
17915
17916                         nested_mlo_links = nla_nest_start(msg, i);
17917                         if (!nested_mlo_links)
17918                                 goto nla_put_failure;
17919
17920                         if (nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link) ||
17921                             (bssid &&
17922                              nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, bssid)) ||
17923                             (info->links[link].addr &&
17924                              nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN,
17925                                      info->links[link].addr)))
17926                                 goto nla_put_failure;
17927
17928                         nla_nest_end(msg, nested_mlo_links);
17929                         i++;
17930                 }
17931                 nla_nest_end(msg, nested);
17932         }
17933
17934         genlmsg_end(msg, hdr);
17935
17936         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17937                                 NL80211_MCGRP_MLME, gfp);
17938         return;
17939
17940  nla_put_failure:
17941         nlmsg_free(msg);
17942 }
17943
17944 void nl80211_send_port_authorized(struct cfg80211_registered_device *rdev,
17945                                   struct net_device *netdev, const u8 *bssid)
17946 {
17947         struct sk_buff *msg;
17948         void *hdr;
17949
17950         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
17951         if (!msg)
17952                 return;
17953
17954         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PORT_AUTHORIZED);
17955         if (!hdr) {
17956                 nlmsg_free(msg);
17957                 return;
17958         }
17959
17960         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17961             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17962             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
17963                 goto nla_put_failure;
17964
17965         genlmsg_end(msg, hdr);
17966
17967         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
17968                                 NL80211_MCGRP_MLME, GFP_KERNEL);
17969         return;
17970
17971  nla_put_failure:
17972         nlmsg_free(msg);
17973 }
17974
17975 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
17976                                struct net_device *netdev, u16 reason,
17977                                const u8 *ie, size_t ie_len, bool from_ap)
17978 {
17979         struct sk_buff *msg;
17980         void *hdr;
17981
17982         msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
17983         if (!msg)
17984                 return;
17985
17986         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
17987         if (!hdr) {
17988                 nlmsg_free(msg);
17989                 return;
17990         }
17991
17992         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
17993             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
17994             (reason &&
17995              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
17996             (from_ap &&
17997              nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
17998             (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
17999                 goto nla_put_failure;
18000
18001         genlmsg_end(msg, hdr);
18002
18003         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18004                                 NL80211_MCGRP_MLME, GFP_KERNEL);
18005         return;
18006
18007  nla_put_failure:
18008         nlmsg_free(msg);
18009 }
18010
18011 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
18012                              struct net_device *netdev, const u8 *bssid,
18013                              gfp_t gfp)
18014 {
18015         struct sk_buff *msg;
18016         void *hdr;
18017
18018         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18019         if (!msg)
18020                 return;
18021
18022         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
18023         if (!hdr) {
18024                 nlmsg_free(msg);
18025                 return;
18026         }
18027
18028         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18029             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18030             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18031                 goto nla_put_failure;
18032
18033         genlmsg_end(msg, hdr);
18034
18035         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18036                                 NL80211_MCGRP_MLME, gfp);
18037         return;
18038
18039  nla_put_failure:
18040         nlmsg_free(msg);
18041 }
18042
18043 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
18044                                         const u8 *ie, u8 ie_len,
18045                                         int sig_dbm, gfp_t gfp)
18046 {
18047         struct wireless_dev *wdev = dev->ieee80211_ptr;
18048         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18049         struct sk_buff *msg;
18050         void *hdr;
18051
18052         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
18053                 return;
18054
18055         trace_cfg80211_notify_new_peer_candidate(dev, addr);
18056
18057         msg = nlmsg_new(100 + ie_len, gfp);
18058         if (!msg)
18059                 return;
18060
18061         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
18062         if (!hdr) {
18063                 nlmsg_free(msg);
18064                 return;
18065         }
18066
18067         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18068             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18069             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18070             (ie_len && ie &&
18071              nla_put(msg, NL80211_ATTR_IE, ie_len, ie)) ||
18072             (sig_dbm &&
18073              nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)))
18074                 goto nla_put_failure;
18075
18076         genlmsg_end(msg, hdr);
18077
18078         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18079                                 NL80211_MCGRP_MLME, gfp);
18080         return;
18081
18082  nla_put_failure:
18083         nlmsg_free(msg);
18084 }
18085 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
18086
18087 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
18088                                  struct net_device *netdev, const u8 *addr,
18089                                  enum nl80211_key_type key_type, int key_id,
18090                                  const u8 *tsc, gfp_t gfp)
18091 {
18092         struct sk_buff *msg;
18093         void *hdr;
18094
18095         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18096         if (!msg)
18097                 return;
18098
18099         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
18100         if (!hdr) {
18101                 nlmsg_free(msg);
18102                 return;
18103         }
18104
18105         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18106             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18107             (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
18108             nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
18109             (key_id != -1 &&
18110              nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
18111             (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
18112                 goto nla_put_failure;
18113
18114         genlmsg_end(msg, hdr);
18115
18116         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18117                                 NL80211_MCGRP_MLME, gfp);
18118         return;
18119
18120  nla_put_failure:
18121         nlmsg_free(msg);
18122 }
18123
18124 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
18125                                     struct ieee80211_channel *channel_before,
18126                                     struct ieee80211_channel *channel_after)
18127 {
18128         struct sk_buff *msg;
18129         void *hdr;
18130         struct nlattr *nl_freq;
18131
18132         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
18133         if (!msg)
18134                 return;
18135
18136         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
18137         if (!hdr) {
18138                 nlmsg_free(msg);
18139                 return;
18140         }
18141
18142         /*
18143          * Since we are applying the beacon hint to a wiphy we know its
18144          * wiphy_idx is valid
18145          */
18146         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
18147                 goto nla_put_failure;
18148
18149         /* Before */
18150         nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_BEFORE);
18151         if (!nl_freq)
18152                 goto nla_put_failure;
18153
18154         if (nl80211_msg_put_channel(msg, wiphy, channel_before, false))
18155                 goto nla_put_failure;
18156         nla_nest_end(msg, nl_freq);
18157
18158         /* After */
18159         nl_freq = nla_nest_start_noflag(msg, NL80211_ATTR_FREQ_AFTER);
18160         if (!nl_freq)
18161                 goto nla_put_failure;
18162
18163         if (nl80211_msg_put_channel(msg, wiphy, channel_after, false))
18164                 goto nla_put_failure;
18165         nla_nest_end(msg, nl_freq);
18166
18167         genlmsg_end(msg, hdr);
18168
18169         rcu_read_lock();
18170         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18171                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18172         rcu_read_unlock();
18173
18174         return;
18175
18176 nla_put_failure:
18177         nlmsg_free(msg);
18178 }
18179
18180 static void nl80211_send_remain_on_chan_event(
18181         int cmd, struct cfg80211_registered_device *rdev,
18182         struct wireless_dev *wdev, u64 cookie,
18183         struct ieee80211_channel *chan,
18184         unsigned int duration, gfp_t gfp)
18185 {
18186         struct sk_buff *msg;
18187         void *hdr;
18188
18189         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18190         if (!msg)
18191                 return;
18192
18193         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18194         if (!hdr) {
18195                 nlmsg_free(msg);
18196                 return;
18197         }
18198
18199         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18200             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18201                                          wdev->netdev->ifindex)) ||
18202             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18203                               NL80211_ATTR_PAD) ||
18204             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
18205             nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
18206                         NL80211_CHAN_NO_HT) ||
18207             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
18208                               NL80211_ATTR_PAD))
18209                 goto nla_put_failure;
18210
18211         if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
18212             nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
18213                 goto nla_put_failure;
18214
18215         genlmsg_end(msg, hdr);
18216
18217         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18218                                 NL80211_MCGRP_MLME, gfp);
18219         return;
18220
18221  nla_put_failure:
18222         nlmsg_free(msg);
18223 }
18224
18225 void cfg80211_assoc_comeback(struct net_device *netdev,
18226                              const u8 *ap_addr, u32 timeout)
18227 {
18228         struct wireless_dev *wdev = netdev->ieee80211_ptr;
18229         struct wiphy *wiphy = wdev->wiphy;
18230         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18231         struct sk_buff *msg;
18232         void *hdr;
18233
18234         trace_cfg80211_assoc_comeback(wdev, ap_addr, timeout);
18235
18236         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
18237         if (!msg)
18238                 return;
18239
18240         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ASSOC_COMEBACK);
18241         if (!hdr) {
18242                 nlmsg_free(msg);
18243                 return;
18244         }
18245
18246         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18247             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18248             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ap_addr) ||
18249             nla_put_u32(msg, NL80211_ATTR_TIMEOUT, timeout))
18250                 goto nla_put_failure;
18251
18252         genlmsg_end(msg, hdr);
18253
18254         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18255                                 NL80211_MCGRP_MLME, GFP_KERNEL);
18256         return;
18257
18258  nla_put_failure:
18259         nlmsg_free(msg);
18260 }
18261 EXPORT_SYMBOL(cfg80211_assoc_comeback);
18262
18263 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
18264                                struct ieee80211_channel *chan,
18265                                unsigned int duration, gfp_t gfp)
18266 {
18267         struct wiphy *wiphy = wdev->wiphy;
18268         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18269
18270         trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
18271         nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
18272                                           rdev, wdev, cookie, chan,
18273                                           duration, gfp);
18274 }
18275 EXPORT_SYMBOL(cfg80211_ready_on_channel);
18276
18277 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
18278                                         struct ieee80211_channel *chan,
18279                                         gfp_t gfp)
18280 {
18281         struct wiphy *wiphy = wdev->wiphy;
18282         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18283
18284         trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
18285         nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
18286                                           rdev, wdev, cookie, chan, 0, gfp);
18287 }
18288 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
18289
18290 void cfg80211_tx_mgmt_expired(struct wireless_dev *wdev, u64 cookie,
18291                                         struct ieee80211_channel *chan,
18292                                         gfp_t gfp)
18293 {
18294         struct wiphy *wiphy = wdev->wiphy;
18295         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18296
18297         trace_cfg80211_tx_mgmt_expired(wdev, cookie, chan);
18298         nl80211_send_remain_on_chan_event(NL80211_CMD_FRAME_WAIT_CANCEL,
18299                                           rdev, wdev, cookie, chan, 0, gfp);
18300 }
18301 EXPORT_SYMBOL(cfg80211_tx_mgmt_expired);
18302
18303 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
18304                       struct station_info *sinfo, gfp_t gfp)
18305 {
18306         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18307         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18308         struct sk_buff *msg;
18309
18310         trace_cfg80211_new_sta(dev, mac_addr, sinfo);
18311
18312         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18313         if (!msg)
18314                 return;
18315
18316         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
18317                                  rdev, dev, mac_addr, sinfo) < 0) {
18318                 nlmsg_free(msg);
18319                 return;
18320         }
18321
18322         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18323                                 NL80211_MCGRP_MLME, gfp);
18324 }
18325 EXPORT_SYMBOL(cfg80211_new_sta);
18326
18327 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
18328                             struct station_info *sinfo, gfp_t gfp)
18329 {
18330         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18331         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18332         struct sk_buff *msg;
18333         struct station_info empty_sinfo = {};
18334
18335         if (!sinfo)
18336                 sinfo = &empty_sinfo;
18337
18338         trace_cfg80211_del_sta(dev, mac_addr);
18339
18340         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18341         if (!msg) {
18342                 cfg80211_sinfo_release_content(sinfo);
18343                 return;
18344         }
18345
18346         if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
18347                                  rdev, dev, mac_addr, sinfo) < 0) {
18348                 nlmsg_free(msg);
18349                 return;
18350         }
18351
18352         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18353                                 NL80211_MCGRP_MLME, gfp);
18354 }
18355 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
18356
18357 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
18358                           enum nl80211_connect_failed_reason reason,
18359                           gfp_t gfp)
18360 {
18361         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
18362         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18363         struct sk_buff *msg;
18364         void *hdr;
18365
18366         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
18367         if (!msg)
18368                 return;
18369
18370         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
18371         if (!hdr) {
18372                 nlmsg_free(msg);
18373                 return;
18374         }
18375
18376         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18377             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
18378             nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
18379                 goto nla_put_failure;
18380
18381         genlmsg_end(msg, hdr);
18382
18383         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18384                                 NL80211_MCGRP_MLME, gfp);
18385         return;
18386
18387  nla_put_failure:
18388         nlmsg_free(msg);
18389 }
18390 EXPORT_SYMBOL(cfg80211_conn_failed);
18391
18392 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
18393                                        const u8 *addr, gfp_t gfp)
18394 {
18395         struct wireless_dev *wdev = dev->ieee80211_ptr;
18396         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18397         struct sk_buff *msg;
18398         void *hdr;
18399         u32 nlportid = READ_ONCE(wdev->ap_unexpected_nlportid);
18400
18401         if (!nlportid)
18402                 return false;
18403
18404         msg = nlmsg_new(100, gfp);
18405         if (!msg)
18406                 return true;
18407
18408         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
18409         if (!hdr) {
18410                 nlmsg_free(msg);
18411                 return true;
18412         }
18413
18414         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18415             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18416             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
18417                 goto nla_put_failure;
18418
18419         genlmsg_end(msg, hdr);
18420         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18421         return true;
18422
18423  nla_put_failure:
18424         nlmsg_free(msg);
18425         return true;
18426 }
18427
18428 bool cfg80211_rx_spurious_frame(struct net_device *dev,
18429                                 const u8 *addr, gfp_t gfp)
18430 {
18431         struct wireless_dev *wdev = dev->ieee80211_ptr;
18432         bool ret;
18433
18434         trace_cfg80211_rx_spurious_frame(dev, addr);
18435
18436         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18437                     wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
18438                 trace_cfg80211_return_bool(false);
18439                 return false;
18440         }
18441         ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
18442                                          addr, gfp);
18443         trace_cfg80211_return_bool(ret);
18444         return ret;
18445 }
18446 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
18447
18448 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
18449                                         const u8 *addr, gfp_t gfp)
18450 {
18451         struct wireless_dev *wdev = dev->ieee80211_ptr;
18452         bool ret;
18453
18454         trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
18455
18456         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
18457                     wdev->iftype != NL80211_IFTYPE_P2P_GO &&
18458                     wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
18459                 trace_cfg80211_return_bool(false);
18460                 return false;
18461         }
18462         ret = __nl80211_unexpected_frame(dev,
18463                                          NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
18464                                          addr, gfp);
18465         trace_cfg80211_return_bool(ret);
18466         return ret;
18467 }
18468 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
18469
18470 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
18471                       struct wireless_dev *wdev, u32 nlportid,
18472                       struct cfg80211_rx_info *info, gfp_t gfp)
18473 {
18474         struct net_device *netdev = wdev->netdev;
18475         struct sk_buff *msg;
18476         void *hdr;
18477
18478         msg = nlmsg_new(100 + info->len, gfp);
18479         if (!msg)
18480                 return -ENOMEM;
18481
18482         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
18483         if (!hdr) {
18484                 nlmsg_free(msg);
18485                 return -ENOMEM;
18486         }
18487
18488         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18489             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18490                                         netdev->ifindex)) ||
18491             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18492                               NL80211_ATTR_PAD) ||
18493             (info->have_link_id &&
18494              nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, info->link_id)) ||
18495             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, KHZ_TO_MHZ(info->freq)) ||
18496             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET, info->freq % 1000) ||
18497             (info->sig_dbm &&
18498              nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, info->sig_dbm)) ||
18499             nla_put(msg, NL80211_ATTR_FRAME, info->len, info->buf) ||
18500             (info->flags &&
18501              nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, info->flags)) ||
18502             (info->rx_tstamp && nla_put_u64_64bit(msg,
18503                                                   NL80211_ATTR_RX_HW_TIMESTAMP,
18504                                                   info->rx_tstamp,
18505                                                   NL80211_ATTR_PAD)) ||
18506             (info->ack_tstamp && nla_put_u64_64bit(msg,
18507                                                    NL80211_ATTR_TX_HW_TIMESTAMP,
18508                                                    info->ack_tstamp,
18509                                                    NL80211_ATTR_PAD)))
18510                 goto nla_put_failure;
18511
18512         genlmsg_end(msg, hdr);
18513
18514         return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18515
18516  nla_put_failure:
18517         nlmsg_free(msg);
18518         return -ENOBUFS;
18519 }
18520
18521 static void nl80211_frame_tx_status(struct wireless_dev *wdev,
18522                                     struct cfg80211_tx_status *status,
18523                                     gfp_t gfp, enum nl80211_commands command)
18524 {
18525         struct wiphy *wiphy = wdev->wiphy;
18526         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18527         struct net_device *netdev = wdev->netdev;
18528         struct sk_buff *msg;
18529         void *hdr;
18530
18531         if (command == NL80211_CMD_FRAME_TX_STATUS)
18532                 trace_cfg80211_mgmt_tx_status(wdev, status->cookie,
18533                                               status->ack);
18534         else
18535                 trace_cfg80211_control_port_tx_status(wdev, status->cookie,
18536                                                       status->ack);
18537
18538         msg = nlmsg_new(100 + status->len, gfp);
18539         if (!msg)
18540                 return;
18541
18542         hdr = nl80211hdr_put(msg, 0, 0, 0, command);
18543         if (!hdr) {
18544                 nlmsg_free(msg);
18545                 return;
18546         }
18547
18548         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18549             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
18550                                    netdev->ifindex)) ||
18551             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18552                               NL80211_ATTR_PAD) ||
18553             nla_put(msg, NL80211_ATTR_FRAME, status->len, status->buf) ||
18554             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, status->cookie,
18555                               NL80211_ATTR_PAD) ||
18556             (status->ack && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
18557             (status->tx_tstamp &&
18558              nla_put_u64_64bit(msg, NL80211_ATTR_TX_HW_TIMESTAMP,
18559                                status->tx_tstamp, NL80211_ATTR_PAD)) ||
18560             (status->ack_tstamp &&
18561              nla_put_u64_64bit(msg, NL80211_ATTR_RX_HW_TIMESTAMP,
18562                                status->ack_tstamp, NL80211_ATTR_PAD)))
18563                 goto nla_put_failure;
18564
18565         genlmsg_end(msg, hdr);
18566
18567         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18568                                 NL80211_MCGRP_MLME, gfp);
18569         return;
18570
18571 nla_put_failure:
18572         nlmsg_free(msg);
18573 }
18574
18575 void cfg80211_control_port_tx_status(struct wireless_dev *wdev, u64 cookie,
18576                                      const u8 *buf, size_t len, bool ack,
18577                                      gfp_t gfp)
18578 {
18579         struct cfg80211_tx_status status = {
18580                 .cookie = cookie,
18581                 .buf = buf,
18582                 .len = len,
18583                 .ack = ack
18584         };
18585
18586         nl80211_frame_tx_status(wdev, &status, gfp,
18587                                 NL80211_CMD_CONTROL_PORT_FRAME_TX_STATUS);
18588 }
18589 EXPORT_SYMBOL(cfg80211_control_port_tx_status);
18590
18591 void cfg80211_mgmt_tx_status_ext(struct wireless_dev *wdev,
18592                                  struct cfg80211_tx_status *status, gfp_t gfp)
18593 {
18594         nl80211_frame_tx_status(wdev, status, gfp, NL80211_CMD_FRAME_TX_STATUS);
18595 }
18596 EXPORT_SYMBOL(cfg80211_mgmt_tx_status_ext);
18597
18598 static int __nl80211_rx_control_port(struct net_device *dev,
18599                                      struct sk_buff *skb,
18600                                      bool unencrypted, gfp_t gfp)
18601 {
18602         struct wireless_dev *wdev = dev->ieee80211_ptr;
18603         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18604         struct ethhdr *ehdr = eth_hdr(skb);
18605         const u8 *addr = ehdr->h_source;
18606         u16 proto = be16_to_cpu(skb->protocol);
18607         struct sk_buff *msg;
18608         void *hdr;
18609         struct nlattr *frame;
18610
18611         u32 nlportid = READ_ONCE(wdev->conn_owner_nlportid);
18612
18613         if (!nlportid)
18614                 return -ENOENT;
18615
18616         msg = nlmsg_new(100 + skb->len, gfp);
18617         if (!msg)
18618                 return -ENOMEM;
18619
18620         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONTROL_PORT_FRAME);
18621         if (!hdr) {
18622                 nlmsg_free(msg);
18623                 return -ENOBUFS;
18624         }
18625
18626         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18627             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
18628             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
18629                               NL80211_ATTR_PAD) ||
18630             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
18631             nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, proto) ||
18632             (unencrypted && nla_put_flag(msg,
18633                                          NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
18634                 goto nla_put_failure;
18635
18636         frame = nla_reserve(msg, NL80211_ATTR_FRAME, skb->len);
18637         if (!frame)
18638                 goto nla_put_failure;
18639
18640         skb_copy_bits(skb, 0, nla_data(frame), skb->len);
18641         genlmsg_end(msg, hdr);
18642
18643         return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
18644
18645  nla_put_failure:
18646         nlmsg_free(msg);
18647         return -ENOBUFS;
18648 }
18649
18650 bool cfg80211_rx_control_port(struct net_device *dev,
18651                               struct sk_buff *skb, bool unencrypted)
18652 {
18653         int ret;
18654
18655         trace_cfg80211_rx_control_port(dev, skb, unencrypted);
18656         ret = __nl80211_rx_control_port(dev, skb, unencrypted, GFP_ATOMIC);
18657         trace_cfg80211_return_bool(ret == 0);
18658         return ret == 0;
18659 }
18660 EXPORT_SYMBOL(cfg80211_rx_control_port);
18661
18662 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
18663                                             const char *mac, gfp_t gfp)
18664 {
18665         struct wireless_dev *wdev = dev->ieee80211_ptr;
18666         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18667         struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18668         void **cb;
18669
18670         if (!msg)
18671                 return NULL;
18672
18673         cb = (void **)msg->cb;
18674
18675         cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
18676         if (!cb[0]) {
18677                 nlmsg_free(msg);
18678                 return NULL;
18679         }
18680
18681         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18682             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
18683                 goto nla_put_failure;
18684
18685         if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
18686                 goto nla_put_failure;
18687
18688         cb[1] = nla_nest_start_noflag(msg, NL80211_ATTR_CQM);
18689         if (!cb[1])
18690                 goto nla_put_failure;
18691
18692         cb[2] = rdev;
18693
18694         return msg;
18695  nla_put_failure:
18696         nlmsg_free(msg);
18697         return NULL;
18698 }
18699
18700 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
18701 {
18702         void **cb = (void **)msg->cb;
18703         struct cfg80211_registered_device *rdev = cb[2];
18704
18705         nla_nest_end(msg, cb[1]);
18706         genlmsg_end(msg, cb[0]);
18707
18708         memset(msg->cb, 0, sizeof(msg->cb));
18709
18710         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18711                                 NL80211_MCGRP_MLME, gfp);
18712 }
18713
18714 void cfg80211_cqm_rssi_notify(struct net_device *dev,
18715                               enum nl80211_cqm_rssi_threshold_event rssi_event,
18716                               s32 rssi_level, gfp_t gfp)
18717 {
18718         struct sk_buff *msg;
18719         struct wireless_dev *wdev = dev->ieee80211_ptr;
18720         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
18721
18722         trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
18723
18724         if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
18725                     rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
18726                 return;
18727
18728         if (wdev->cqm_config) {
18729                 wdev->cqm_config->last_rssi_event_value = rssi_level;
18730
18731                 cfg80211_cqm_rssi_update(rdev, dev);
18732
18733                 if (rssi_level == 0)
18734                         rssi_level = wdev->cqm_config->last_rssi_event_value;
18735         }
18736
18737         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18738         if (!msg)
18739                 return;
18740
18741         if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
18742                         rssi_event))
18743                 goto nla_put_failure;
18744
18745         if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
18746                                       rssi_level))
18747                 goto nla_put_failure;
18748
18749         cfg80211_send_cqm(msg, gfp);
18750
18751         return;
18752
18753  nla_put_failure:
18754         nlmsg_free(msg);
18755 }
18756 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
18757
18758 void cfg80211_cqm_txe_notify(struct net_device *dev,
18759                              const u8 *peer, u32 num_packets,
18760                              u32 rate, u32 intvl, gfp_t gfp)
18761 {
18762         struct sk_buff *msg;
18763
18764         msg = cfg80211_prepare_cqm(dev, peer, gfp);
18765         if (!msg)
18766                 return;
18767
18768         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
18769                 goto nla_put_failure;
18770
18771         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
18772                 goto nla_put_failure;
18773
18774         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
18775                 goto nla_put_failure;
18776
18777         cfg80211_send_cqm(msg, gfp);
18778         return;
18779
18780  nla_put_failure:
18781         nlmsg_free(msg);
18782 }
18783 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
18784
18785 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
18786                                  const u8 *peer, u32 num_packets, gfp_t gfp)
18787 {
18788         struct sk_buff *msg;
18789
18790         trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
18791
18792         msg = cfg80211_prepare_cqm(dev, peer, gfp);
18793         if (!msg)
18794                 return;
18795
18796         if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
18797                 goto nla_put_failure;
18798
18799         cfg80211_send_cqm(msg, gfp);
18800         return;
18801
18802  nla_put_failure:
18803         nlmsg_free(msg);
18804 }
18805 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
18806
18807 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
18808 {
18809         struct sk_buff *msg;
18810
18811         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
18812         if (!msg)
18813                 return;
18814
18815         if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
18816                 goto nla_put_failure;
18817
18818         cfg80211_send_cqm(msg, gfp);
18819         return;
18820
18821  nla_put_failure:
18822         nlmsg_free(msg);
18823 }
18824 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
18825
18826 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
18827                                      struct net_device *netdev, const u8 *bssid,
18828                                      const u8 *replay_ctr, gfp_t gfp)
18829 {
18830         struct sk_buff *msg;
18831         struct nlattr *rekey_attr;
18832         void *hdr;
18833
18834         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18835         if (!msg)
18836                 return;
18837
18838         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
18839         if (!hdr) {
18840                 nlmsg_free(msg);
18841                 return;
18842         }
18843
18844         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18845             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
18846             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
18847                 goto nla_put_failure;
18848
18849         rekey_attr = nla_nest_start_noflag(msg, NL80211_ATTR_REKEY_DATA);
18850         if (!rekey_attr)
18851                 goto nla_put_failure;
18852
18853         if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
18854                     NL80211_REPLAY_CTR_LEN, replay_ctr))
18855                 goto nla_put_failure;
18856
18857         nla_nest_end(msg, rekey_attr);
18858
18859         genlmsg_end(msg, hdr);
18860
18861         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18862                                 NL80211_MCGRP_MLME, gfp);
18863         return;
18864
18865  nla_put_failure:
18866         nlmsg_free(msg);
18867 }
18868
18869 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
18870                                const u8 *replay_ctr, gfp_t gfp)
18871 {
18872         struct wireless_dev *wdev = dev->ieee80211_ptr;
18873         struct wiphy *wiphy = wdev->wiphy;
18874         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18875
18876         trace_cfg80211_gtk_rekey_notify(dev, bssid);
18877         nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
18878 }
18879 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
18880
18881 static void
18882 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
18883                                struct net_device *netdev, int index,
18884                                const u8 *bssid, bool preauth, gfp_t gfp)
18885 {
18886         struct sk_buff *msg;
18887         struct nlattr *attr;
18888         void *hdr;
18889
18890         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18891         if (!msg)
18892                 return;
18893
18894         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
18895         if (!hdr) {
18896                 nlmsg_free(msg);
18897                 return;
18898         }
18899
18900         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
18901             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18902                 goto nla_put_failure;
18903
18904         attr = nla_nest_start_noflag(msg, NL80211_ATTR_PMKSA_CANDIDATE);
18905         if (!attr)
18906                 goto nla_put_failure;
18907
18908         if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
18909             nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
18910             (preauth &&
18911              nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
18912                 goto nla_put_failure;
18913
18914         nla_nest_end(msg, attr);
18915
18916         genlmsg_end(msg, hdr);
18917
18918         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18919                                 NL80211_MCGRP_MLME, gfp);
18920         return;
18921
18922  nla_put_failure:
18923         nlmsg_free(msg);
18924 }
18925
18926 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
18927                                      const u8 *bssid, bool preauth, gfp_t gfp)
18928 {
18929         struct wireless_dev *wdev = dev->ieee80211_ptr;
18930         struct wiphy *wiphy = wdev->wiphy;
18931         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18932
18933         trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
18934         nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
18935 }
18936 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
18937
18938 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
18939                                      struct net_device *netdev,
18940                                      unsigned int link_id,
18941                                      struct cfg80211_chan_def *chandef,
18942                                      gfp_t gfp,
18943                                      enum nl80211_commands notif,
18944                                      u8 count, bool quiet)
18945 {
18946         struct wireless_dev *wdev = netdev->ieee80211_ptr;
18947         struct sk_buff *msg;
18948         void *hdr;
18949
18950         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
18951         if (!msg)
18952                 return;
18953
18954         hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
18955         if (!hdr) {
18956                 nlmsg_free(msg);
18957                 return;
18958         }
18959
18960         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
18961                 goto nla_put_failure;
18962
18963         if (wdev->valid_links &&
18964             nla_put_u8(msg, NL80211_ATTR_MLO_LINK_ID, link_id))
18965                 goto nla_put_failure;
18966
18967         if (nl80211_send_chandef(msg, chandef))
18968                 goto nla_put_failure;
18969
18970         if (notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) {
18971                 if (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count))
18972                         goto nla_put_failure;
18973                 if (quiet &&
18974                     nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX))
18975                         goto nla_put_failure;
18976         }
18977
18978         genlmsg_end(msg, hdr);
18979
18980         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
18981                                 NL80211_MCGRP_MLME, gfp);
18982         return;
18983
18984  nla_put_failure:
18985         nlmsg_free(msg);
18986 }
18987
18988 void cfg80211_ch_switch_notify(struct net_device *dev,
18989                                struct cfg80211_chan_def *chandef,
18990                                unsigned int link_id)
18991 {
18992         struct wireless_dev *wdev = dev->ieee80211_ptr;
18993         struct wiphy *wiphy = wdev->wiphy;
18994         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
18995
18996         ASSERT_WDEV_LOCK(wdev);
18997         WARN_INVALID_LINK_ID(wdev, link_id);
18998
18999         trace_cfg80211_ch_switch_notify(dev, chandef, link_id);
19000
19001         switch (wdev->iftype) {
19002         case NL80211_IFTYPE_STATION:
19003         case NL80211_IFTYPE_P2P_CLIENT:
19004                 if (!WARN_ON(!wdev->links[link_id].client.current_bss))
19005                         cfg80211_update_assoc_bss_entry(wdev, link_id,
19006                                                         chandef->chan);
19007                 break;
19008         case NL80211_IFTYPE_MESH_POINT:
19009                 wdev->u.mesh.chandef = *chandef;
19010                 wdev->u.mesh.preset_chandef = *chandef;
19011                 break;
19012         case NL80211_IFTYPE_AP:
19013         case NL80211_IFTYPE_P2P_GO:
19014                 wdev->links[link_id].ap.chandef = *chandef;
19015                 break;
19016         case NL80211_IFTYPE_ADHOC:
19017                 wdev->u.ibss.chandef = *chandef;
19018                 break;
19019         default:
19020                 WARN_ON(1);
19021                 break;
19022         }
19023
19024         cfg80211_sched_dfs_chan_update(rdev);
19025
19026         nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19027                                  NL80211_CMD_CH_SWITCH_NOTIFY, 0, false);
19028 }
19029 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
19030
19031 void cfg80211_ch_switch_started_notify(struct net_device *dev,
19032                                        struct cfg80211_chan_def *chandef,
19033                                        unsigned int link_id, u8 count,
19034                                        bool quiet)
19035 {
19036         struct wireless_dev *wdev = dev->ieee80211_ptr;
19037         struct wiphy *wiphy = wdev->wiphy;
19038         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19039
19040         ASSERT_WDEV_LOCK(wdev);
19041         WARN_INVALID_LINK_ID(wdev, link_id);
19042
19043         trace_cfg80211_ch_switch_started_notify(dev, chandef, link_id);
19044
19045         nl80211_ch_switch_notify(rdev, dev, link_id, chandef, GFP_KERNEL,
19046                                  NL80211_CMD_CH_SWITCH_STARTED_NOTIFY,
19047                                  count, quiet);
19048 }
19049 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
19050
19051 int cfg80211_bss_color_notify(struct net_device *dev, gfp_t gfp,
19052                               enum nl80211_commands cmd, u8 count,
19053                               u64 color_bitmap)
19054 {
19055         struct wireless_dev *wdev = dev->ieee80211_ptr;
19056         struct wiphy *wiphy = wdev->wiphy;
19057         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19058         struct sk_buff *msg;
19059         void *hdr;
19060
19061         ASSERT_WDEV_LOCK(wdev);
19062
19063         trace_cfg80211_bss_color_notify(dev, cmd, count, color_bitmap);
19064
19065         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19066         if (!msg)
19067                 return -ENOMEM;
19068
19069         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
19070         if (!hdr)
19071                 goto nla_put_failure;
19072
19073         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19074                 goto nla_put_failure;
19075
19076         if (cmd == NL80211_CMD_COLOR_CHANGE_STARTED &&
19077             nla_put_u32(msg, NL80211_ATTR_COLOR_CHANGE_COUNT, count))
19078                 goto nla_put_failure;
19079
19080         if (cmd == NL80211_CMD_OBSS_COLOR_COLLISION &&
19081             nla_put_u64_64bit(msg, NL80211_ATTR_OBSS_COLOR_BITMAP,
19082                               color_bitmap, NL80211_ATTR_PAD))
19083                 goto nla_put_failure;
19084
19085         genlmsg_end(msg, hdr);
19086
19087         return genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
19088                                        msg, 0, NL80211_MCGRP_MLME, gfp);
19089
19090 nla_put_failure:
19091         nlmsg_free(msg);
19092         return -EINVAL;
19093 }
19094 EXPORT_SYMBOL(cfg80211_bss_color_notify);
19095
19096 void
19097 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
19098                      const struct cfg80211_chan_def *chandef,
19099                      enum nl80211_radar_event event,
19100                      struct net_device *netdev, gfp_t gfp)
19101 {
19102         struct sk_buff *msg;
19103         void *hdr;
19104
19105         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19106         if (!msg)
19107                 return;
19108
19109         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
19110         if (!hdr) {
19111                 nlmsg_free(msg);
19112                 return;
19113         }
19114
19115         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19116                 goto nla_put_failure;
19117
19118         /* NOP and radar events don't need a netdev parameter */
19119         if (netdev) {
19120                 struct wireless_dev *wdev = netdev->ieee80211_ptr;
19121
19122                 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19123                     nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19124                                       NL80211_ATTR_PAD))
19125                         goto nla_put_failure;
19126         }
19127
19128         if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
19129                 goto nla_put_failure;
19130
19131         if (nl80211_send_chandef(msg, chandef))
19132                 goto nla_put_failure;
19133
19134         genlmsg_end(msg, hdr);
19135
19136         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19137                                 NL80211_MCGRP_MLME, gfp);
19138         return;
19139
19140  nla_put_failure:
19141         nlmsg_free(msg);
19142 }
19143
19144 void cfg80211_sta_opmode_change_notify(struct net_device *dev, const u8 *mac,
19145                                        struct sta_opmode_info *sta_opmode,
19146                                        gfp_t gfp)
19147 {
19148         struct sk_buff *msg;
19149         struct wireless_dev *wdev = dev->ieee80211_ptr;
19150         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19151         void *hdr;
19152
19153         if (WARN_ON(!mac))
19154                 return;
19155
19156         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19157         if (!msg)
19158                 return;
19159
19160         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STA_OPMODE_CHANGED);
19161         if (!hdr) {
19162                 nlmsg_free(msg);
19163                 return;
19164         }
19165
19166         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
19167                 goto nla_put_failure;
19168
19169         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
19170                 goto nla_put_failure;
19171
19172         if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
19173                 goto nla_put_failure;
19174
19175         if ((sta_opmode->changed & STA_OPMODE_SMPS_MODE_CHANGED) &&
19176             nla_put_u8(msg, NL80211_ATTR_SMPS_MODE, sta_opmode->smps_mode))
19177                 goto nla_put_failure;
19178
19179         if ((sta_opmode->changed & STA_OPMODE_MAX_BW_CHANGED) &&
19180             nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, sta_opmode->bw))
19181                 goto nla_put_failure;
19182
19183         if ((sta_opmode->changed & STA_OPMODE_N_SS_CHANGED) &&
19184             nla_put_u8(msg, NL80211_ATTR_NSS, sta_opmode->rx_nss))
19185                 goto nla_put_failure;
19186
19187         genlmsg_end(msg, hdr);
19188
19189         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19190                                 NL80211_MCGRP_MLME, gfp);
19191
19192         return;
19193
19194 nla_put_failure:
19195         nlmsg_free(msg);
19196 }
19197 EXPORT_SYMBOL(cfg80211_sta_opmode_change_notify);
19198
19199 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
19200                            u64 cookie, bool acked, s32 ack_signal,
19201                            bool is_valid_ack_signal, gfp_t gfp)
19202 {
19203         struct wireless_dev *wdev = dev->ieee80211_ptr;
19204         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19205         struct sk_buff *msg;
19206         void *hdr;
19207
19208         trace_cfg80211_probe_status(dev, addr, cookie, acked);
19209
19210         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19211
19212         if (!msg)
19213                 return;
19214
19215         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
19216         if (!hdr) {
19217                 nlmsg_free(msg);
19218                 return;
19219         }
19220
19221         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19222             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19223             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
19224             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
19225                               NL80211_ATTR_PAD) ||
19226             (acked && nla_put_flag(msg, NL80211_ATTR_ACK)) ||
19227             (is_valid_ack_signal && nla_put_s32(msg, NL80211_ATTR_ACK_SIGNAL,
19228                                                 ack_signal)))
19229                 goto nla_put_failure;
19230
19231         genlmsg_end(msg, hdr);
19232
19233         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19234                                 NL80211_MCGRP_MLME, gfp);
19235         return;
19236
19237  nla_put_failure:
19238         nlmsg_free(msg);
19239 }
19240 EXPORT_SYMBOL(cfg80211_probe_status);
19241
19242 void cfg80211_report_obss_beacon_khz(struct wiphy *wiphy, const u8 *frame,
19243                                      size_t len, int freq, int sig_dbm)
19244 {
19245         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19246         struct sk_buff *msg;
19247         void *hdr;
19248         struct cfg80211_beacon_registration *reg;
19249
19250         trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
19251
19252         spin_lock_bh(&rdev->beacon_registrations_lock);
19253         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
19254                 msg = nlmsg_new(len + 100, GFP_ATOMIC);
19255                 if (!msg) {
19256                         spin_unlock_bh(&rdev->beacon_registrations_lock);
19257                         return;
19258                 }
19259
19260                 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
19261                 if (!hdr)
19262                         goto nla_put_failure;
19263
19264                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19265                     (freq &&
19266                      (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
19267                                   KHZ_TO_MHZ(freq)) ||
19268                       nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_OFFSET,
19269                                   freq % 1000))) ||
19270                     (sig_dbm &&
19271                      nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
19272                     nla_put(msg, NL80211_ATTR_FRAME, len, frame))
19273                         goto nla_put_failure;
19274
19275                 genlmsg_end(msg, hdr);
19276
19277                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
19278         }
19279         spin_unlock_bh(&rdev->beacon_registrations_lock);
19280         return;
19281
19282  nla_put_failure:
19283         spin_unlock_bh(&rdev->beacon_registrations_lock);
19284         nlmsg_free(msg);
19285 }
19286 EXPORT_SYMBOL(cfg80211_report_obss_beacon_khz);
19287
19288 #ifdef CONFIG_PM
19289 static int cfg80211_net_detect_results(struct sk_buff *msg,
19290                                        struct cfg80211_wowlan_wakeup *wakeup)
19291 {
19292         struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
19293         struct nlattr *nl_results, *nl_match, *nl_freqs;
19294         int i, j;
19295
19296         nl_results = nla_nest_start_noflag(msg,
19297                                            NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
19298         if (!nl_results)
19299                 return -EMSGSIZE;
19300
19301         for (i = 0; i < nd->n_matches; i++) {
19302                 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
19303
19304                 nl_match = nla_nest_start_noflag(msg, i);
19305                 if (!nl_match)
19306                         break;
19307
19308                 /* The SSID attribute is optional in nl80211, but for
19309                  * simplicity reasons it's always present in the
19310                  * cfg80211 structure.  If a driver can't pass the
19311                  * SSID, that needs to be changed.  A zero length SSID
19312                  * is still a valid SSID (wildcard), so it cannot be
19313                  * used for this purpose.
19314                  */
19315                 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
19316                             match->ssid.ssid)) {
19317                         nla_nest_cancel(msg, nl_match);
19318                         goto out;
19319                 }
19320
19321                 if (match->n_channels) {
19322                         nl_freqs = nla_nest_start_noflag(msg,
19323                                                          NL80211_ATTR_SCAN_FREQUENCIES);
19324                         if (!nl_freqs) {
19325                                 nla_nest_cancel(msg, nl_match);
19326                                 goto out;
19327                         }
19328
19329                         for (j = 0; j < match->n_channels; j++) {
19330                                 if (nla_put_u32(msg, j, match->channels[j])) {
19331                                         nla_nest_cancel(msg, nl_freqs);
19332                                         nla_nest_cancel(msg, nl_match);
19333                                         goto out;
19334                                 }
19335                         }
19336
19337                         nla_nest_end(msg, nl_freqs);
19338                 }
19339
19340                 nla_nest_end(msg, nl_match);
19341         }
19342
19343 out:
19344         nla_nest_end(msg, nl_results);
19345         return 0;
19346 }
19347
19348 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
19349                                    struct cfg80211_wowlan_wakeup *wakeup,
19350                                    gfp_t gfp)
19351 {
19352         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19353         struct sk_buff *msg;
19354         void *hdr;
19355         int size = 200;
19356
19357         trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
19358
19359         if (wakeup)
19360                 size += wakeup->packet_present_len;
19361
19362         msg = nlmsg_new(size, gfp);
19363         if (!msg)
19364                 return;
19365
19366         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
19367         if (!hdr)
19368                 goto free_msg;
19369
19370         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19371             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19372                               NL80211_ATTR_PAD))
19373                 goto free_msg;
19374
19375         if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
19376                                         wdev->netdev->ifindex))
19377                 goto free_msg;
19378
19379         if (wakeup) {
19380                 struct nlattr *reasons;
19381
19382                 reasons = nla_nest_start_noflag(msg,
19383                                                 NL80211_ATTR_WOWLAN_TRIGGERS);
19384                 if (!reasons)
19385                         goto free_msg;
19386
19387                 if (wakeup->disconnect &&
19388                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
19389                         goto free_msg;
19390                 if (wakeup->magic_pkt &&
19391                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
19392                         goto free_msg;
19393                 if (wakeup->gtk_rekey_failure &&
19394                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
19395                         goto free_msg;
19396                 if (wakeup->eap_identity_req &&
19397                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
19398                         goto free_msg;
19399                 if (wakeup->four_way_handshake &&
19400                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
19401                         goto free_msg;
19402                 if (wakeup->rfkill_release &&
19403                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
19404                         goto free_msg;
19405
19406                 if (wakeup->pattern_idx >= 0 &&
19407                     nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
19408                                 wakeup->pattern_idx))
19409                         goto free_msg;
19410
19411                 if (wakeup->tcp_match &&
19412                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
19413                         goto free_msg;
19414
19415                 if (wakeup->tcp_connlost &&
19416                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
19417                         goto free_msg;
19418
19419                 if (wakeup->tcp_nomoretokens &&
19420                     nla_put_flag(msg,
19421                                  NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
19422                         goto free_msg;
19423
19424                 if (wakeup->packet) {
19425                         u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
19426                         u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
19427
19428                         if (!wakeup->packet_80211) {
19429                                 pkt_attr =
19430                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
19431                                 len_attr =
19432                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
19433                         }
19434
19435                         if (wakeup->packet_len &&
19436                             nla_put_u32(msg, len_attr, wakeup->packet_len))
19437                                 goto free_msg;
19438
19439                         if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
19440                                     wakeup->packet))
19441                                 goto free_msg;
19442                 }
19443
19444                 if (wakeup->net_detect &&
19445                     cfg80211_net_detect_results(msg, wakeup))
19446                                 goto free_msg;
19447
19448                 nla_nest_end(msg, reasons);
19449         }
19450
19451         genlmsg_end(msg, hdr);
19452
19453         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19454                                 NL80211_MCGRP_MLME, gfp);
19455         return;
19456
19457  free_msg:
19458         nlmsg_free(msg);
19459 }
19460 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
19461 #endif
19462
19463 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
19464                                 enum nl80211_tdls_operation oper,
19465                                 u16 reason_code, gfp_t gfp)
19466 {
19467         struct wireless_dev *wdev = dev->ieee80211_ptr;
19468         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19469         struct sk_buff *msg;
19470         void *hdr;
19471
19472         trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
19473                                          reason_code);
19474
19475         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19476         if (!msg)
19477                 return;
19478
19479         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
19480         if (!hdr) {
19481                 nlmsg_free(msg);
19482                 return;
19483         }
19484
19485         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19486             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19487             nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
19488             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
19489             (reason_code > 0 &&
19490              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
19491                 goto nla_put_failure;
19492
19493         genlmsg_end(msg, hdr);
19494
19495         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19496                                 NL80211_MCGRP_MLME, gfp);
19497         return;
19498
19499  nla_put_failure:
19500         nlmsg_free(msg);
19501 }
19502 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
19503
19504 static int nl80211_netlink_notify(struct notifier_block * nb,
19505                                   unsigned long state,
19506                                   void *_notify)
19507 {
19508         struct netlink_notify *notify = _notify;
19509         struct cfg80211_registered_device *rdev;
19510         struct wireless_dev *wdev;
19511         struct cfg80211_beacon_registration *reg, *tmp;
19512
19513         if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
19514                 return NOTIFY_DONE;
19515
19516         rcu_read_lock();
19517
19518         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
19519                 struct cfg80211_sched_scan_request *sched_scan_req;
19520
19521                 list_for_each_entry_rcu(sched_scan_req,
19522                                         &rdev->sched_scan_req_list,
19523                                         list) {
19524                         if (sched_scan_req->owner_nlportid == notify->portid) {
19525                                 sched_scan_req->nl_owner_dead = true;
19526                                 schedule_work(&rdev->sched_scan_stop_wk);
19527                         }
19528                 }
19529
19530                 list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
19531                         cfg80211_mlme_unregister_socket(wdev, notify->portid);
19532
19533                         if (wdev->owner_nlportid == notify->portid) {
19534                                 wdev->nl_owner_dead = true;
19535                                 schedule_work(&rdev->destroy_work);
19536                         } else if (wdev->conn_owner_nlportid == notify->portid) {
19537                                 schedule_work(&wdev->disconnect_wk);
19538                         }
19539
19540                         cfg80211_release_pmsr(wdev, notify->portid);
19541                 }
19542
19543                 spin_lock_bh(&rdev->beacon_registrations_lock);
19544                 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
19545                                          list) {
19546                         if (reg->nlportid == notify->portid) {
19547                                 list_del(&reg->list);
19548                                 kfree(reg);
19549                                 break;
19550                         }
19551                 }
19552                 spin_unlock_bh(&rdev->beacon_registrations_lock);
19553         }
19554
19555         rcu_read_unlock();
19556
19557         /*
19558          * It is possible that the user space process that is controlling the
19559          * indoor setting disappeared, so notify the regulatory core.
19560          */
19561         regulatory_netlink_notify(notify->portid);
19562         return NOTIFY_OK;
19563 }
19564
19565 static struct notifier_block nl80211_netlink_notifier = {
19566         .notifier_call = nl80211_netlink_notify,
19567 };
19568
19569 void cfg80211_ft_event(struct net_device *netdev,
19570                        struct cfg80211_ft_event_params *ft_event)
19571 {
19572         struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19573         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19574         struct sk_buff *msg;
19575         void *hdr;
19576
19577         trace_cfg80211_ft_event(wiphy, netdev, ft_event);
19578
19579         if (!ft_event->target_ap)
19580                 return;
19581
19582         msg = nlmsg_new(100 + ft_event->ies_len + ft_event->ric_ies_len,
19583                         GFP_KERNEL);
19584         if (!msg)
19585                 return;
19586
19587         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
19588         if (!hdr)
19589                 goto out;
19590
19591         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19592             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19593             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
19594                 goto out;
19595
19596         if (ft_event->ies &&
19597             nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
19598                 goto out;
19599         if (ft_event->ric_ies &&
19600             nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
19601                     ft_event->ric_ies))
19602                 goto out;
19603
19604         genlmsg_end(msg, hdr);
19605
19606         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19607                                 NL80211_MCGRP_MLME, GFP_KERNEL);
19608         return;
19609  out:
19610         nlmsg_free(msg);
19611 }
19612 EXPORT_SYMBOL(cfg80211_ft_event);
19613
19614 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
19615 {
19616         struct cfg80211_registered_device *rdev;
19617         struct sk_buff *msg;
19618         void *hdr;
19619         u32 nlportid;
19620
19621         rdev = wiphy_to_rdev(wdev->wiphy);
19622         if (!rdev->crit_proto_nlportid)
19623                 return;
19624
19625         nlportid = rdev->crit_proto_nlportid;
19626         rdev->crit_proto_nlportid = 0;
19627
19628         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19629         if (!msg)
19630                 return;
19631
19632         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
19633         if (!hdr)
19634                 goto nla_put_failure;
19635
19636         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19637             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19638                               NL80211_ATTR_PAD))
19639                 goto nla_put_failure;
19640
19641         genlmsg_end(msg, hdr);
19642
19643         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
19644         return;
19645
19646  nla_put_failure:
19647         nlmsg_free(msg);
19648 }
19649 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
19650
19651 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
19652 {
19653         struct wiphy *wiphy = wdev->wiphy;
19654         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19655         struct sk_buff *msg;
19656         void *hdr;
19657
19658         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
19659         if (!msg)
19660                 return;
19661
19662         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
19663         if (!hdr)
19664                 goto out;
19665
19666         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19667             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
19668             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
19669                               NL80211_ATTR_PAD))
19670                 goto out;
19671
19672         genlmsg_end(msg, hdr);
19673
19674         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
19675                                 NL80211_MCGRP_MLME, GFP_KERNEL);
19676         return;
19677  out:
19678         nlmsg_free(msg);
19679 }
19680
19681 int cfg80211_external_auth_request(struct net_device *dev,
19682                                    struct cfg80211_external_auth_params *params,
19683                                    gfp_t gfp)
19684 {
19685         struct wireless_dev *wdev = dev->ieee80211_ptr;
19686         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
19687         struct sk_buff *msg;
19688         void *hdr;
19689
19690         if (!wdev->conn_owner_nlportid)
19691                 return -EINVAL;
19692
19693         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19694         if (!msg)
19695                 return -ENOMEM;
19696
19697         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_EXTERNAL_AUTH);
19698         if (!hdr)
19699                 goto nla_put_failure;
19700
19701         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19702             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
19703             nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, params->key_mgmt_suite) ||
19704             nla_put_u32(msg, NL80211_ATTR_EXTERNAL_AUTH_ACTION,
19705                         params->action) ||
19706             nla_put(msg, NL80211_ATTR_BSSID, ETH_ALEN, params->bssid) ||
19707             nla_put(msg, NL80211_ATTR_SSID, params->ssid.ssid_len,
19708                     params->ssid.ssid))
19709                 goto nla_put_failure;
19710
19711         genlmsg_end(msg, hdr);
19712         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
19713                         wdev->conn_owner_nlportid);
19714         return 0;
19715
19716  nla_put_failure:
19717         nlmsg_free(msg);
19718         return -ENOBUFS;
19719 }
19720 EXPORT_SYMBOL(cfg80211_external_auth_request);
19721
19722 void cfg80211_update_owe_info_event(struct net_device *netdev,
19723                                     struct cfg80211_update_owe_info *owe_info,
19724                                     gfp_t gfp)
19725 {
19726         struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
19727         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
19728         struct sk_buff *msg;
19729         void *hdr;
19730
19731         trace_cfg80211_update_owe_info_event(wiphy, netdev, owe_info);
19732
19733         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
19734         if (!msg)
19735                 return;
19736
19737         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_UPDATE_OWE_INFO);
19738         if (!hdr)
19739                 goto nla_put_failure;
19740
19741         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
19742             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
19743             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, owe_info->peer))
19744                 goto nla_put_failure;
19745
19746         if (!owe_info->ie_len ||
19747             nla_put(msg, NL80211_ATTR_IE, owe_info->ie_len, owe_info->ie))
19748                 goto nla_put_failure;
19749
19750         genlmsg_end(msg, hdr);
19751
19752         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
19753                                 NL80211_MCGRP_MLME, gfp);
19754         return;
19755
19756 nla_put_failure:
19757         genlmsg_cancel(msg, hdr);
19758         nlmsg_free(msg);
19759 }
19760 EXPORT_SYMBOL(cfg80211_update_owe_info_event);
19761
19762 /* initialisation/exit functions */
19763
19764 int __init nl80211_init(void)
19765 {
19766         int err;
19767
19768         err = genl_register_family(&nl80211_fam);
19769         if (err)
19770                 return err;
19771
19772         err = netlink_register_notifier(&nl80211_netlink_notifier);
19773         if (err)
19774                 goto err_out;
19775
19776         return 0;
19777  err_out:
19778         genl_unregister_family(&nl80211_fam);
19779         return err;
19780 }
19781
19782 void nl80211_exit(void)
19783 {
19784         netlink_unregister_notifier(&nl80211_netlink_notifier);
19785         genl_unregister_family(&nl80211_fam);
19786 }