~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/wireless/nl80211.c

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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