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

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

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