GNU Linux-libre 4.14.266-gnu1
[releases.git] / net / wireless / nl80211.c
1 /*
2  * This is the new netlink-based wireless configuration interface.
3  *
4  * Copyright 2006-2010  Johannes Berg <johannes@sipsolutions.net>
5  * Copyright 2013-2014  Intel Mobile Communications GmbH
6  * Copyright 2015-2017  Intel Deutschland GmbH
7  */
8
9 #include <linux/if.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
13 #include <linux/list.h>
14 #include <linux/if_ether.h>
15 #include <linux/ieee80211.h>
16 #include <linux/nl80211.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/netlink.h>
19 #include <linux/nospec.h>
20 #include <linux/etherdevice.h>
21 #include <net/net_namespace.h>
22 #include <net/genetlink.h>
23 #include <net/cfg80211.h>
24 #include <net/sock.h>
25 #include <net/inet_connection_sock.h>
26 #include "core.h"
27 #include "nl80211.h"
28 #include "reg.h"
29 #include "rdev-ops.h"
30
31 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
32                                    struct genl_info *info,
33                                    struct cfg80211_crypto_settings *settings,
34                                    int cipher_limit);
35
36 /* the netlink family */
37 static struct genl_family nl80211_fam;
38
39 /* multicast groups */
40 enum nl80211_multicast_groups {
41         NL80211_MCGRP_CONFIG,
42         NL80211_MCGRP_SCAN,
43         NL80211_MCGRP_REGULATORY,
44         NL80211_MCGRP_MLME,
45         NL80211_MCGRP_VENDOR,
46         NL80211_MCGRP_NAN,
47         NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
48 };
49
50 static const struct genl_multicast_group nl80211_mcgrps[] = {
51         [NL80211_MCGRP_CONFIG] = { .name = NL80211_MULTICAST_GROUP_CONFIG },
52         [NL80211_MCGRP_SCAN] = { .name = NL80211_MULTICAST_GROUP_SCAN },
53         [NL80211_MCGRP_REGULATORY] = { .name = NL80211_MULTICAST_GROUP_REG },
54         [NL80211_MCGRP_MLME] = { .name = NL80211_MULTICAST_GROUP_MLME },
55         [NL80211_MCGRP_VENDOR] = { .name = NL80211_MULTICAST_GROUP_VENDOR },
56         [NL80211_MCGRP_NAN] = { .name = NL80211_MULTICAST_GROUP_NAN },
57 #ifdef CONFIG_NL80211_TESTMODE
58         [NL80211_MCGRP_TESTMODE] = { .name = NL80211_MULTICAST_GROUP_TESTMODE }
59 #endif
60 };
61
62 /* returns ERR_PTR values */
63 static struct wireless_dev *
64 __cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
65 {
66         struct cfg80211_registered_device *rdev;
67         struct wireless_dev *result = NULL;
68         bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
69         bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
70         u64 wdev_id;
71         int wiphy_idx = -1;
72         int ifidx = -1;
73
74         ASSERT_RTNL();
75
76         if (!have_ifidx && !have_wdev_id)
77                 return ERR_PTR(-EINVAL);
78
79         if (have_ifidx)
80                 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
81         if (have_wdev_id) {
82                 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
83                 wiphy_idx = wdev_id >> 32;
84         }
85
86         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
87                 struct wireless_dev *wdev;
88
89                 if (wiphy_net(&rdev->wiphy) != netns)
90                         continue;
91
92                 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
93                         continue;
94
95                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
96                         if (have_ifidx && wdev->netdev &&
97                             wdev->netdev->ifindex == ifidx) {
98                                 result = wdev;
99                                 break;
100                         }
101                         if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
102                                 result = wdev;
103                                 break;
104                         }
105                 }
106
107                 if (result)
108                         break;
109         }
110
111         if (result)
112                 return result;
113         return ERR_PTR(-ENODEV);
114 }
115
116 static struct cfg80211_registered_device *
117 __cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
118 {
119         struct cfg80211_registered_device *rdev = NULL, *tmp;
120         struct net_device *netdev;
121
122         ASSERT_RTNL();
123
124         if (!attrs[NL80211_ATTR_WIPHY] &&
125             !attrs[NL80211_ATTR_IFINDEX] &&
126             !attrs[NL80211_ATTR_WDEV])
127                 return ERR_PTR(-EINVAL);
128
129         if (attrs[NL80211_ATTR_WIPHY])
130                 rdev = cfg80211_rdev_by_wiphy_idx(
131                                 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
132
133         if (attrs[NL80211_ATTR_WDEV]) {
134                 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
135                 struct wireless_dev *wdev;
136                 bool found = false;
137
138                 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
139                 if (tmp) {
140                         /* make sure wdev exists */
141                         list_for_each_entry(wdev, &tmp->wiphy.wdev_list, list) {
142                                 if (wdev->identifier != (u32)wdev_id)
143                                         continue;
144                                 found = true;
145                                 break;
146                         }
147
148                         if (!found)
149                                 tmp = NULL;
150
151                         if (rdev && tmp != rdev)
152                                 return ERR_PTR(-EINVAL);
153                         rdev = tmp;
154                 }
155         }
156
157         if (attrs[NL80211_ATTR_IFINDEX]) {
158                 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
159
160                 netdev = __dev_get_by_index(netns, ifindex);
161                 if (netdev) {
162                         if (netdev->ieee80211_ptr)
163                                 tmp = wiphy_to_rdev(
164                                         netdev->ieee80211_ptr->wiphy);
165                         else
166                                 tmp = NULL;
167
168                         /* not wireless device -- return error */
169                         if (!tmp)
170                                 return ERR_PTR(-EINVAL);
171
172                         /* mismatch -- return error */
173                         if (rdev && tmp != rdev)
174                                 return ERR_PTR(-EINVAL);
175
176                         rdev = tmp;
177                 }
178         }
179
180         if (!rdev)
181                 return ERR_PTR(-ENODEV);
182
183         if (netns != wiphy_net(&rdev->wiphy))
184                 return ERR_PTR(-ENODEV);
185
186         return rdev;
187 }
188
189 /*
190  * This function returns a pointer to the driver
191  * that the genl_info item that is passed refers to.
192  *
193  * The result of this can be a PTR_ERR and hence must
194  * be checked with IS_ERR() for errors.
195  */
196 static struct cfg80211_registered_device *
197 cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
198 {
199         return __cfg80211_rdev_from_attrs(netns, info->attrs);
200 }
201
202 static int validate_beacon_head(const struct nlattr *attr,
203                                 struct netlink_ext_ack *extack)
204 {
205         const u8 *data = nla_data(attr);
206         unsigned int len = nla_len(attr);
207         const struct element *elem;
208         const struct ieee80211_mgmt *mgmt = (void *)data;
209         unsigned int fixedlen = offsetof(struct ieee80211_mgmt,
210                                          u.beacon.variable);
211
212         if (len < fixedlen)
213                 goto err;
214
215         if (ieee80211_hdrlen(mgmt->frame_control) !=
216             offsetof(struct ieee80211_mgmt, u.beacon))
217                 goto err;
218
219         data += fixedlen;
220         len -= fixedlen;
221
222         for_each_element(elem, data, len) {
223                 /* nothing */
224         }
225
226         if (for_each_element_completed(elem, data, len))
227                 return 0;
228
229 err:
230         NL_SET_ERR_MSG_ATTR(extack, attr, "malformed beacon head");
231         return -EINVAL;
232 }
233
234 /* policy for the attributes */
235 static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
236         [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
237         [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
238                                       .len = 20-1 },
239         [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
240
241         [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
242         [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
243         [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
244         [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
245         [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
246
247         [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
248         [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
249         [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
250         [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
251         [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
252         [NL80211_ATTR_WIPHY_DYN_ACK] = { .type = NLA_FLAG },
253
254         [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
255         [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
256         [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
257
258         [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
259         [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
260
261         [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
262         [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
263                                     .len = WLAN_MAX_KEY_LEN },
264         [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
265         [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
266         [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
267         [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
268         [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
269
270         [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
271         [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
272         [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
273                                        .len = IEEE80211_MAX_DATA_LEN },
274         [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
275                                        .len = IEEE80211_MAX_DATA_LEN },
276         [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
277         [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
278         [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
279         [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
280                                                .len = NL80211_MAX_SUPP_RATES },
281         [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
282         [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
283         [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
284         [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
285                                    .len = IEEE80211_MAX_MESH_ID_LEN },
286         [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_BINARY,
287                                           .len = ETH_ALEN },
288
289         [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
290         [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
291
292         [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
293         [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
294         [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
295         [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
296                                            .len = NL80211_MAX_SUPP_RATES },
297         [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
298
299         [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
300         [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
301
302         [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
303
304         [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
305         [NL80211_ATTR_IE] = { .type = NLA_BINARY,
306                               .len = IEEE80211_MAX_DATA_LEN },
307         [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
308         [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
309
310         [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
311                                 .len = IEEE80211_MAX_SSID_LEN },
312         [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
313         [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
314         [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
315         [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
316         [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
317         [NL80211_ATTR_STA_FLAGS2] = {
318                 .len = sizeof(struct nl80211_sta_flag_update),
319         },
320         [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
321         [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
322         [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
323         [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
324         [NL80211_ATTR_STATUS_CODE] = { .type = NLA_U16 },
325         [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
326         [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
327         [NL80211_ATTR_PID] = { .type = NLA_U32 },
328         [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
329         [NL80211_ATTR_PMKID] = { .len = WLAN_PMKID_LEN },
330         [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
331         [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
332         [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
333         [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
334                                  .len = IEEE80211_MAX_DATA_LEN },
335         [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
336         [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
337         [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
338         [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
339         [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
340         [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
341         [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
342         [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
343         [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
344         [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
345         [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
346         [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
347         [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
348         [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
349         [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
350         [NL80211_ATTR_MEASUREMENT_DURATION] = { .type = NLA_U16 },
351         [NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY] = { .type = NLA_FLAG },
352         [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
353         [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
354         [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
355         [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
356         [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
357                                          .len = IEEE80211_MAX_DATA_LEN },
358         [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
359                                          .len = IEEE80211_MAX_DATA_LEN },
360         [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
361         [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
362         [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
363         [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
364         [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
365         [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
366         [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
367         [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
368         [NL80211_ATTR_TDLS_INITIATOR] = { .type = NLA_FLAG },
369         [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
370         [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
371                                       .len = IEEE80211_MAX_DATA_LEN },
372         [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
373         [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
374         [NL80211_ATTR_HT_CAPABILITY_MASK] = {
375                 .len = NL80211_HT_CAPABILITY_LEN
376         },
377         [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
378         [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
379         [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
380         [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
381         [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
382         [NL80211_ATTR_AUTH_DATA] = { .type = NLA_BINARY, },
383         [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
384         [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
385         [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
386         [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
387         [NL80211_ATTR_LOCAL_MESH_POWER_MODE] = {. type = NLA_U32 },
388         [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
389         [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
390         [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
391         [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
392         [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
393         [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
394         [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
395                 .len = NL80211_VHT_CAPABILITY_LEN,
396         },
397         [NL80211_ATTR_MDID] = { .type = NLA_U16 },
398         [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
399                                   .len = IEEE80211_MAX_DATA_LEN },
400         [NL80211_ATTR_CRIT_PROT_ID] = { .type = NLA_U16 },
401         [NL80211_ATTR_MAX_CRIT_PROT_DURATION] = { .type = NLA_U16 },
402         [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
403         [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
404         [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
405         [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
406         [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_BINARY },
407         [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_BINARY },
408         [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
409         [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
410         [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
411         [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
412         [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
413         [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
414         [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
415         [NL80211_ATTR_QOS_MAP] = { .type = NLA_BINARY,
416                                    .len = IEEE80211_QOS_MAP_LEN_MAX },
417         [NL80211_ATTR_MAC_HINT] = { .len = ETH_ALEN },
418         [NL80211_ATTR_WIPHY_FREQ_HINT] = { .type = NLA_U32 },
419         [NL80211_ATTR_TDLS_PEER_CAPABILITY] = { .type = NLA_U32 },
420         [NL80211_ATTR_SOCKET_OWNER] = { .type = NLA_FLAG },
421         [NL80211_ATTR_CSA_C_OFFSETS_TX] = { .type = NLA_BINARY },
422         [NL80211_ATTR_USE_RRM] = { .type = NLA_FLAG },
423         [NL80211_ATTR_TSID] = { .type = NLA_U8 },
424         [NL80211_ATTR_USER_PRIO] = { .type = NLA_U8 },
425         [NL80211_ATTR_ADMITTED_TIME] = { .type = NLA_U16 },
426         [NL80211_ATTR_SMPS_MODE] = { .type = NLA_U8 },
427         [NL80211_ATTR_OPER_CLASS] = { .type = NLA_U8 },
428         [NL80211_ATTR_MAC_MASK] = { .len = ETH_ALEN },
429         [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
430         [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
431         [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
432         [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
433         [NL80211_ATTR_PBSS] = { .type = NLA_FLAG },
434         [NL80211_ATTR_BSS_SELECT] = { .type = NLA_NESTED },
435         [NL80211_ATTR_STA_SUPPORT_P2P_PS] = { .type = NLA_U8 },
436         [NL80211_ATTR_MU_MIMO_GROUP_DATA] = {
437                 .len = VHT_MUMIMO_GROUPS_DATA_LEN
438         },
439         [NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR] = { .len = ETH_ALEN },
440         [NL80211_ATTR_NAN_MASTER_PREF] = { .type = NLA_U8 },
441         [NL80211_ATTR_BANDS] = { .type = NLA_U32 },
442         [NL80211_ATTR_NAN_FUNC] = { .type = NLA_NESTED },
443         [NL80211_ATTR_FILS_KEK] = { .type = NLA_BINARY,
444                                     .len = FILS_MAX_KEK_LEN },
445         [NL80211_ATTR_FILS_NONCES] = { .len = 2 * FILS_NONCE_LEN },
446         [NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED] = { .type = NLA_FLAG, },
447         [NL80211_ATTR_BSSID] = { .len = ETH_ALEN },
448         [NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] = { .type = NLA_S8 },
449         [NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST] = {
450                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
451         },
452         [NL80211_ATTR_TIMEOUT_REASON] = { .type = NLA_U32 },
453         [NL80211_ATTR_FILS_ERP_USERNAME] = { .type = NLA_BINARY,
454                                              .len = FILS_ERP_MAX_USERNAME_LEN },
455         [NL80211_ATTR_FILS_ERP_REALM] = { .type = NLA_BINARY,
456                                           .len = FILS_ERP_MAX_REALM_LEN },
457         [NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] = { .type = NLA_U16 },
458         [NL80211_ATTR_FILS_ERP_RRK] = { .type = NLA_BINARY,
459                                         .len = FILS_ERP_MAX_RRK_LEN },
460         [NL80211_ATTR_FILS_CACHE_ID] = { .len = 2 },
461         [NL80211_ATTR_PMK] = { .type = NLA_BINARY, .len = PMK_MAX_LEN },
462         [NL80211_ATTR_SCHED_SCAN_MULTI] = { .type = NLA_FLAG },
463 };
464
465 /* policy for the key attributes */
466 static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
467         [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
468         [NL80211_KEY_IDX] = { .type = NLA_U8 },
469         [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
470         [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
471         [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
472         [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
473         [NL80211_KEY_TYPE] = { .type = NLA_U32 },
474         [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
475 };
476
477 /* policy for the key default flags */
478 static const struct nla_policy
479 nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
480         [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
481         [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
482 };
483
484 #ifdef CONFIG_PM
485 /* policy for WoWLAN attributes */
486 static const struct nla_policy
487 nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
488         [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
489         [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
490         [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
491         [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
492         [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
493         [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
494         [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
495         [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
496         [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
497         [NL80211_WOWLAN_TRIG_NET_DETECT] = { .type = NLA_NESTED },
498 };
499
500 static const struct nla_policy
501 nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
502         [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
503         [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
504         [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
505         [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
506         [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
507         [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
508         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
509                 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
510         },
511         [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
512                 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
513         },
514         [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
515         [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
516         [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
517 };
518 #endif /* CONFIG_PM */
519
520 /* policy for coalesce rule attributes */
521 static const struct nla_policy
522 nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
523         [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
524         [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
525         [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
526 };
527
528 /* policy for GTK rekey offload attributes */
529 static const struct nla_policy
530 nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
531         [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
532         [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
533         [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
534 };
535
536 static const struct nla_policy
537 nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
538         [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
539                                                  .len = IEEE80211_MAX_SSID_LEN },
540         [NL80211_SCHED_SCAN_MATCH_ATTR_BSSID] = { .len = ETH_ALEN },
541         [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
542 };
543
544 static const struct nla_policy
545 nl80211_plan_policy[NL80211_SCHED_SCAN_PLAN_MAX + 1] = {
546         [NL80211_SCHED_SCAN_PLAN_INTERVAL] = { .type = NLA_U32 },
547         [NL80211_SCHED_SCAN_PLAN_ITERATIONS] = { .type = NLA_U32 },
548 };
549
550 static const struct nla_policy
551 nl80211_bss_select_policy[NL80211_BSS_SELECT_ATTR_MAX + 1] = {
552         [NL80211_BSS_SELECT_ATTR_RSSI] = { .type = NLA_FLAG },
553         [NL80211_BSS_SELECT_ATTR_BAND_PREF] = { .type = NLA_U32 },
554         [NL80211_BSS_SELECT_ATTR_RSSI_ADJUST] = {
555                 .len = sizeof(struct nl80211_bss_select_rssi_adjust)
556         },
557 };
558
559 /* policy for NAN function attributes */
560 static const struct nla_policy
561 nl80211_nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
562         [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
563         [NL80211_NAN_FUNC_SERVICE_ID] = {
564                                     .len = NL80211_NAN_FUNC_SERVICE_ID_LEN },
565         [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
566         [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
567         [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
568         [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
569         [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
570         [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { .len = ETH_ALEN },
571         [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
572         [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
573         [NL80211_NAN_FUNC_SERVICE_INFO] = { .type = NLA_BINARY,
574                         .len = NL80211_NAN_FUNC_SERVICE_SPEC_INFO_MAX_LEN },
575         [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
576         [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
577         [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
578         [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8 },
579         [NL80211_NAN_FUNC_TERM_REASON] = { .type = NLA_U8 },
580 };
581
582 /* policy for Service Response Filter attributes */
583 static const struct nla_policy
584 nl80211_nan_srf_policy[NL80211_NAN_SRF_ATTR_MAX + 1] = {
585         [NL80211_NAN_SRF_INCLUDE] = { .type = NLA_FLAG },
586         [NL80211_NAN_SRF_BF] = { .type = NLA_BINARY,
587                                  .len =  NL80211_NAN_FUNC_SRF_MAX_LEN },
588         [NL80211_NAN_SRF_BF_IDX] = { .type = NLA_U8 },
589         [NL80211_NAN_SRF_MAC_ADDRS] = { .type = NLA_NESTED },
590 };
591
592 /* policy for packet pattern attributes */
593 static const struct nla_policy
594 nl80211_packet_pattern_policy[MAX_NL80211_PKTPAT + 1] = {
595         [NL80211_PKTPAT_MASK] = { .type = NLA_BINARY, },
596         [NL80211_PKTPAT_PATTERN] = { .type = NLA_BINARY, },
597         [NL80211_PKTPAT_OFFSET] = { .type = NLA_U32 },
598 };
599
600 static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
601                                      struct netlink_callback *cb,
602                                      struct cfg80211_registered_device **rdev,
603                                      struct wireless_dev **wdev)
604 {
605         int err;
606
607         if (!cb->args[0]) {
608                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
609                                   genl_family_attrbuf(&nl80211_fam),
610                                   nl80211_fam.maxattr, nl80211_policy, NULL);
611                 if (err)
612                         return err;
613
614                 *wdev = __cfg80211_wdev_from_attrs(
615                                         sock_net(skb->sk),
616                                         genl_family_attrbuf(&nl80211_fam));
617                 if (IS_ERR(*wdev))
618                         return PTR_ERR(*wdev);
619                 *rdev = wiphy_to_rdev((*wdev)->wiphy);
620                 /* 0 is the first index - add 1 to parse only once */
621                 cb->args[0] = (*rdev)->wiphy_idx + 1;
622                 cb->args[1] = (*wdev)->identifier;
623         } else {
624                 /* subtract the 1 again here */
625                 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
626                 struct wireless_dev *tmp;
627
628                 if (!wiphy)
629                         return -ENODEV;
630                 *rdev = wiphy_to_rdev(wiphy);
631                 *wdev = NULL;
632
633                 list_for_each_entry(tmp, &(*rdev)->wiphy.wdev_list, list) {
634                         if (tmp->identifier == cb->args[1]) {
635                                 *wdev = tmp;
636                                 break;
637                         }
638                 }
639
640                 if (!*wdev)
641                         return -ENODEV;
642         }
643
644         return 0;
645 }
646
647 /* IE validation */
648 static bool is_valid_ie_attr(const struct nlattr *attr)
649 {
650         const u8 *pos;
651         int len;
652
653         if (!attr)
654                 return true;
655
656         pos = nla_data(attr);
657         len = nla_len(attr);
658
659         while (len) {
660                 u8 elemlen;
661
662                 if (len < 2)
663                         return false;
664                 len -= 2;
665
666                 elemlen = pos[1];
667                 if (elemlen > len)
668                         return false;
669
670                 len -= elemlen;
671                 pos += 2 + elemlen;
672         }
673
674         return true;
675 }
676
677 /* message building helper */
678 static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
679                                    int flags, u8 cmd)
680 {
681         /* since there is no private header just add the generic one */
682         return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
683 }
684
685 static int nl80211_msg_put_channel(struct sk_buff *msg,
686                                    struct ieee80211_channel *chan,
687                                    bool large)
688 {
689         /* Some channels must be completely excluded from the
690          * list to protect old user-space tools from breaking
691          */
692         if (!large && chan->flags &
693             (IEEE80211_CHAN_NO_10MHZ | IEEE80211_CHAN_NO_20MHZ))
694                 return 0;
695
696         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
697                         chan->center_freq))
698                 goto nla_put_failure;
699
700         if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
701             nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
702                 goto nla_put_failure;
703         if (chan->flags & IEEE80211_CHAN_NO_IR) {
704                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
705                         goto nla_put_failure;
706                 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
707                         goto nla_put_failure;
708         }
709         if (chan->flags & IEEE80211_CHAN_RADAR) {
710                 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
711                         goto nla_put_failure;
712                 if (large) {
713                         u32 time;
714
715                         time = elapsed_jiffies_msecs(chan->dfs_state_entered);
716
717                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
718                                         chan->dfs_state))
719                                 goto nla_put_failure;
720                         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
721                                         time))
722                                 goto nla_put_failure;
723                         if (nla_put_u32(msg,
724                                         NL80211_FREQUENCY_ATTR_DFS_CAC_TIME,
725                                         chan->dfs_cac_ms))
726                                 goto nla_put_failure;
727                 }
728         }
729
730         if (large) {
731                 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
732                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
733                         goto nla_put_failure;
734                 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
735                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
736                         goto nla_put_failure;
737                 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
738                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
739                         goto nla_put_failure;
740                 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
741                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
742                         goto nla_put_failure;
743                 if ((chan->flags & IEEE80211_CHAN_INDOOR_ONLY) &&
744                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_INDOOR_ONLY))
745                         goto nla_put_failure;
746                 if ((chan->flags & IEEE80211_CHAN_IR_CONCURRENT) &&
747                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_IR_CONCURRENT))
748                         goto nla_put_failure;
749                 if ((chan->flags & IEEE80211_CHAN_NO_20MHZ) &&
750                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_20MHZ))
751                         goto nla_put_failure;
752                 if ((chan->flags & IEEE80211_CHAN_NO_10MHZ) &&
753                     nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_10MHZ))
754                         goto nla_put_failure;
755         }
756
757         if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
758                         DBM_TO_MBM(chan->max_power)))
759                 goto nla_put_failure;
760
761         return 0;
762
763  nla_put_failure:
764         return -ENOBUFS;
765 }
766
767 /* netlink command implementations */
768
769 struct key_parse {
770         struct key_params p;
771         int idx;
772         int type;
773         bool def, defmgmt;
774         bool def_uni, def_multi;
775 };
776
777 static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
778 {
779         struct nlattr *tb[NL80211_KEY_MAX + 1];
780         int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
781                                    nl80211_key_policy, NULL);
782         if (err)
783                 return err;
784
785         k->def = !!tb[NL80211_KEY_DEFAULT];
786         k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
787
788         if (k->def) {
789                 k->def_uni = true;
790                 k->def_multi = true;
791         }
792         if (k->defmgmt)
793                 k->def_multi = true;
794
795         if (tb[NL80211_KEY_IDX])
796                 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
797
798         if (tb[NL80211_KEY_DATA]) {
799                 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
800                 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
801         }
802
803         if (tb[NL80211_KEY_SEQ]) {
804                 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
805                 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
806         }
807
808         if (tb[NL80211_KEY_CIPHER])
809                 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
810
811         if (tb[NL80211_KEY_TYPE]) {
812                 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
813                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
814                         return -EINVAL;
815         }
816
817         if (tb[NL80211_KEY_DEFAULT_TYPES]) {
818                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
819
820                 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
821                                        tb[NL80211_KEY_DEFAULT_TYPES],
822                                        nl80211_key_default_policy, NULL);
823                 if (err)
824                         return err;
825
826                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
827                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
828         }
829
830         return 0;
831 }
832
833 static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
834 {
835         if (info->attrs[NL80211_ATTR_KEY_DATA]) {
836                 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
837                 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
838         }
839
840         if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
841                 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
842                 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
843         }
844
845         if (info->attrs[NL80211_ATTR_KEY_IDX])
846                 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
847
848         if (info->attrs[NL80211_ATTR_KEY_CIPHER])
849                 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
850
851         k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
852         k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
853
854         if (k->def) {
855                 k->def_uni = true;
856                 k->def_multi = true;
857         }
858         if (k->defmgmt)
859                 k->def_multi = true;
860
861         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
862                 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
863                 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
864                         return -EINVAL;
865         }
866
867         if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
868                 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
869                 int err = nla_parse_nested(kdt,
870                                            NUM_NL80211_KEY_DEFAULT_TYPES - 1,
871                                            info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
872                                            nl80211_key_default_policy,
873                                            info->extack);
874                 if (err)
875                         return err;
876
877                 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
878                 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
879         }
880
881         return 0;
882 }
883
884 static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
885 {
886         int err;
887
888         memset(k, 0, sizeof(*k));
889         k->idx = -1;
890         k->type = -1;
891
892         if (info->attrs[NL80211_ATTR_KEY])
893                 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
894         else
895                 err = nl80211_parse_key_old(info, k);
896
897         if (err)
898                 return err;
899
900         if (k->def && k->defmgmt)
901                 return -EINVAL;
902
903         if (k->defmgmt) {
904                 if (k->def_uni || !k->def_multi)
905                         return -EINVAL;
906         }
907
908         if (k->idx != -1) {
909                 if (k->defmgmt) {
910                         if (k->idx < 4 || k->idx > 5)
911                                 return -EINVAL;
912                 } else if (k->def) {
913                         if (k->idx < 0 || k->idx > 3)
914                                 return -EINVAL;
915                 } else {
916                         if (k->idx < 0 || k->idx > 5)
917                                 return -EINVAL;
918                 }
919         }
920
921         return 0;
922 }
923
924 static struct cfg80211_cached_keys *
925 nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
926                        struct nlattr *keys, bool *no_ht)
927 {
928         struct key_parse parse;
929         struct nlattr *key;
930         struct cfg80211_cached_keys *result;
931         int rem, err, def = 0;
932         bool have_key = false;
933
934         nla_for_each_nested(key, keys, rem) {
935                 have_key = true;
936                 break;
937         }
938
939         if (!have_key)
940                 return NULL;
941
942         result = kzalloc(sizeof(*result), GFP_KERNEL);
943         if (!result)
944                 return ERR_PTR(-ENOMEM);
945
946         result->def = -1;
947
948         nla_for_each_nested(key, keys, rem) {
949                 memset(&parse, 0, sizeof(parse));
950                 parse.idx = -1;
951
952                 err = nl80211_parse_key_new(key, &parse);
953                 if (err)
954                         goto error;
955                 err = -EINVAL;
956                 if (!parse.p.key)
957                         goto error;
958                 if (parse.idx < 0 || parse.idx > 3)
959                         goto error;
960                 if (parse.def) {
961                         if (def)
962                                 goto error;
963                         def = 1;
964                         result->def = parse.idx;
965                         if (!parse.def_uni || !parse.def_multi)
966                                 goto error;
967                 } else if (parse.defmgmt)
968                         goto error;
969                 err = cfg80211_validate_key_settings(rdev, &parse.p,
970                                                      parse.idx, false, NULL);
971                 if (err)
972                         goto error;
973                 if (parse.p.cipher != WLAN_CIPHER_SUITE_WEP40 &&
974                     parse.p.cipher != WLAN_CIPHER_SUITE_WEP104) {
975                         err = -EINVAL;
976                         goto error;
977                 }
978                 result->params[parse.idx].cipher = parse.p.cipher;
979                 result->params[parse.idx].key_len = parse.p.key_len;
980                 result->params[parse.idx].key = result->data[parse.idx];
981                 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
982
983                 /* must be WEP key if we got here */
984                 if (no_ht)
985                         *no_ht = true;
986         }
987
988         if (result->def < 0) {
989                 err = -EINVAL;
990                 goto error;
991         }
992
993         return result;
994  error:
995         kfree(result);
996         return ERR_PTR(err);
997 }
998
999 static int nl80211_key_allowed(struct wireless_dev *wdev)
1000 {
1001         ASSERT_WDEV_LOCK(wdev);
1002
1003         switch (wdev->iftype) {
1004         case NL80211_IFTYPE_AP:
1005         case NL80211_IFTYPE_AP_VLAN:
1006         case NL80211_IFTYPE_P2P_GO:
1007         case NL80211_IFTYPE_MESH_POINT:
1008                 break;
1009         case NL80211_IFTYPE_ADHOC:
1010         case NL80211_IFTYPE_STATION:
1011         case NL80211_IFTYPE_P2P_CLIENT:
1012                 if (!wdev->current_bss)
1013                         return -ENOLINK;
1014                 break;
1015         case NL80211_IFTYPE_UNSPECIFIED:
1016         case NL80211_IFTYPE_OCB:
1017         case NL80211_IFTYPE_MONITOR:
1018         case NL80211_IFTYPE_NAN:
1019         case NL80211_IFTYPE_P2P_DEVICE:
1020         case NL80211_IFTYPE_WDS:
1021         case NUM_NL80211_IFTYPES:
1022                 return -EINVAL;
1023         }
1024
1025         return 0;
1026 }
1027
1028 static struct ieee80211_channel *nl80211_get_valid_chan(struct wiphy *wiphy,
1029                                                         struct nlattr *tb)
1030 {
1031         struct ieee80211_channel *chan;
1032
1033         if (tb == NULL)
1034                 return NULL;
1035         chan = ieee80211_get_channel(wiphy, nla_get_u32(tb));
1036         if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1037                 return NULL;
1038         return chan;
1039 }
1040
1041 static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
1042 {
1043         struct nlattr *nl_modes = nla_nest_start(msg, attr);
1044         int i;
1045
1046         if (!nl_modes)
1047                 goto nla_put_failure;
1048
1049         i = 0;
1050         while (ifmodes) {
1051                 if ((ifmodes & 1) && nla_put_flag(msg, i))
1052                         goto nla_put_failure;
1053                 ifmodes >>= 1;
1054                 i++;
1055         }
1056
1057         nla_nest_end(msg, nl_modes);
1058         return 0;
1059
1060 nla_put_failure:
1061         return -ENOBUFS;
1062 }
1063
1064 static int nl80211_put_iface_combinations(struct wiphy *wiphy,
1065                                           struct sk_buff *msg,
1066                                           bool large)
1067 {
1068         struct nlattr *nl_combis;
1069         int i, j;
1070
1071         nl_combis = nla_nest_start(msg,
1072                                 NL80211_ATTR_INTERFACE_COMBINATIONS);
1073         if (!nl_combis)
1074                 goto nla_put_failure;
1075
1076         for (i = 0; i < wiphy->n_iface_combinations; i++) {
1077                 const struct ieee80211_iface_combination *c;
1078                 struct nlattr *nl_combi, *nl_limits;
1079
1080                 c = &wiphy->iface_combinations[i];
1081
1082                 nl_combi = nla_nest_start(msg, i + 1);
1083                 if (!nl_combi)
1084                         goto nla_put_failure;
1085
1086                 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
1087                 if (!nl_limits)
1088                         goto nla_put_failure;
1089
1090                 for (j = 0; j < c->n_limits; j++) {
1091                         struct nlattr *nl_limit;
1092
1093                         nl_limit = nla_nest_start(msg, j + 1);
1094                         if (!nl_limit)
1095                                 goto nla_put_failure;
1096                         if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
1097                                         c->limits[j].max))
1098                                 goto nla_put_failure;
1099                         if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
1100                                                 c->limits[j].types))
1101                                 goto nla_put_failure;
1102                         nla_nest_end(msg, nl_limit);
1103                 }
1104
1105                 nla_nest_end(msg, nl_limits);
1106
1107                 if (c->beacon_int_infra_match &&
1108                     nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
1109                         goto nla_put_failure;
1110                 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
1111                                 c->num_different_channels) ||
1112                     nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
1113                                 c->max_interfaces))
1114                         goto nla_put_failure;
1115                 if (large &&
1116                     (nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
1117                                 c->radar_detect_widths) ||
1118                      nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_REGIONS,
1119                                 c->radar_detect_regions)))
1120                         goto nla_put_failure;
1121                 if (c->beacon_int_min_gcd &&
1122                     nla_put_u32(msg, NL80211_IFACE_COMB_BI_MIN_GCD,
1123                                 c->beacon_int_min_gcd))
1124                         goto nla_put_failure;
1125
1126                 nla_nest_end(msg, nl_combi);
1127         }
1128
1129         nla_nest_end(msg, nl_combis);
1130
1131         return 0;
1132 nla_put_failure:
1133         return -ENOBUFS;
1134 }
1135
1136 #ifdef CONFIG_PM
1137 static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
1138                                         struct sk_buff *msg)
1139 {
1140         const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
1141         struct nlattr *nl_tcp;
1142
1143         if (!tcp)
1144                 return 0;
1145
1146         nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
1147         if (!nl_tcp)
1148                 return -ENOBUFS;
1149
1150         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1151                         tcp->data_payload_max))
1152                 return -ENOBUFS;
1153
1154         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
1155                         tcp->data_payload_max))
1156                 return -ENOBUFS;
1157
1158         if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
1159                 return -ENOBUFS;
1160
1161         if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
1162                                 sizeof(*tcp->tok), tcp->tok))
1163                 return -ENOBUFS;
1164
1165         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
1166                         tcp->data_interval_max))
1167                 return -ENOBUFS;
1168
1169         if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
1170                         tcp->wake_payload_max))
1171                 return -ENOBUFS;
1172
1173         nla_nest_end(msg, nl_tcp);
1174         return 0;
1175 }
1176
1177 static int nl80211_send_wowlan(struct sk_buff *msg,
1178                                struct cfg80211_registered_device *rdev,
1179                                bool large)
1180 {
1181         struct nlattr *nl_wowlan;
1182
1183         if (!rdev->wiphy.wowlan)
1184                 return 0;
1185
1186         nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1187         if (!nl_wowlan)
1188                 return -ENOBUFS;
1189
1190         if (((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
1191              nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1192             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
1193              nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1194             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1195              nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1196             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1197              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1198             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1199              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1200             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1201              nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1202             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1203              nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1204             ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1205              nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1206                 return -ENOBUFS;
1207
1208         if (rdev->wiphy.wowlan->n_patterns) {
1209                 struct nl80211_pattern_support pat = {
1210                         .max_patterns = rdev->wiphy.wowlan->n_patterns,
1211                         .min_pattern_len = rdev->wiphy.wowlan->pattern_min_len,
1212                         .max_pattern_len = rdev->wiphy.wowlan->pattern_max_len,
1213                         .max_pkt_offset = rdev->wiphy.wowlan->max_pkt_offset,
1214                 };
1215
1216                 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1217                             sizeof(pat), &pat))
1218                         return -ENOBUFS;
1219         }
1220
1221         if ((rdev->wiphy.wowlan->flags & WIPHY_WOWLAN_NET_DETECT) &&
1222             nla_put_u32(msg, NL80211_WOWLAN_TRIG_NET_DETECT,
1223                         rdev->wiphy.wowlan->max_nd_match_sets))
1224                 return -ENOBUFS;
1225
1226         if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1227                 return -ENOBUFS;
1228
1229         nla_nest_end(msg, nl_wowlan);
1230
1231         return 0;
1232 }
1233 #endif
1234
1235 static int nl80211_send_coalesce(struct sk_buff *msg,
1236                                  struct cfg80211_registered_device *rdev)
1237 {
1238         struct nl80211_coalesce_rule_support rule;
1239
1240         if (!rdev->wiphy.coalesce)
1241                 return 0;
1242
1243         rule.max_rules = rdev->wiphy.coalesce->n_rules;
1244         rule.max_delay = rdev->wiphy.coalesce->max_delay;
1245         rule.pat.max_patterns = rdev->wiphy.coalesce->n_patterns;
1246         rule.pat.min_pattern_len = rdev->wiphy.coalesce->pattern_min_len;
1247         rule.pat.max_pattern_len = rdev->wiphy.coalesce->pattern_max_len;
1248         rule.pat.max_pkt_offset = rdev->wiphy.coalesce->max_pkt_offset;
1249
1250         if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1251                 return -ENOBUFS;
1252
1253         return 0;
1254 }
1255
1256 static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1257                                       struct ieee80211_supported_band *sband)
1258 {
1259         struct nlattr *nl_rates, *nl_rate;
1260         struct ieee80211_rate *rate;
1261         int i;
1262
1263         /* add HT info */
1264         if (sband->ht_cap.ht_supported &&
1265             (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1266                      sizeof(sband->ht_cap.mcs),
1267                      &sband->ht_cap.mcs) ||
1268              nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1269                          sband->ht_cap.cap) ||
1270              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1271                         sband->ht_cap.ampdu_factor) ||
1272              nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1273                         sband->ht_cap.ampdu_density)))
1274                 return -ENOBUFS;
1275
1276         /* add VHT info */
1277         if (sband->vht_cap.vht_supported &&
1278             (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1279                      sizeof(sband->vht_cap.vht_mcs),
1280                      &sband->vht_cap.vht_mcs) ||
1281              nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1282                          sband->vht_cap.cap)))
1283                 return -ENOBUFS;
1284
1285         /* add bitrates */
1286         nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1287         if (!nl_rates)
1288                 return -ENOBUFS;
1289
1290         for (i = 0; i < sband->n_bitrates; i++) {
1291                 nl_rate = nla_nest_start(msg, i);
1292                 if (!nl_rate)
1293                         return -ENOBUFS;
1294
1295                 rate = &sband->bitrates[i];
1296                 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1297                                 rate->bitrate))
1298                         return -ENOBUFS;
1299                 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1300                     nla_put_flag(msg,
1301                                  NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1302                         return -ENOBUFS;
1303
1304                 nla_nest_end(msg, nl_rate);
1305         }
1306
1307         nla_nest_end(msg, nl_rates);
1308
1309         return 0;
1310 }
1311
1312 static int
1313 nl80211_send_mgmt_stypes(struct sk_buff *msg,
1314                          const struct ieee80211_txrx_stypes *mgmt_stypes)
1315 {
1316         u16 stypes;
1317         struct nlattr *nl_ftypes, *nl_ifs;
1318         enum nl80211_iftype ift;
1319         int i;
1320
1321         if (!mgmt_stypes)
1322                 return 0;
1323
1324         nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1325         if (!nl_ifs)
1326                 return -ENOBUFS;
1327
1328         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1329                 nl_ftypes = nla_nest_start(msg, ift);
1330                 if (!nl_ftypes)
1331                         return -ENOBUFS;
1332                 i = 0;
1333                 stypes = mgmt_stypes[ift].tx;
1334                 while (stypes) {
1335                         if ((stypes & 1) &&
1336                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1337                                         (i << 4) | IEEE80211_FTYPE_MGMT))
1338                                 return -ENOBUFS;
1339                         stypes >>= 1;
1340                         i++;
1341                 }
1342                 nla_nest_end(msg, nl_ftypes);
1343         }
1344
1345         nla_nest_end(msg, nl_ifs);
1346
1347         nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1348         if (!nl_ifs)
1349                 return -ENOBUFS;
1350
1351         for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1352                 nl_ftypes = nla_nest_start(msg, ift);
1353                 if (!nl_ftypes)
1354                         return -ENOBUFS;
1355                 i = 0;
1356                 stypes = mgmt_stypes[ift].rx;
1357                 while (stypes) {
1358                         if ((stypes & 1) &&
1359                             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1360                                         (i << 4) | IEEE80211_FTYPE_MGMT))
1361                                 return -ENOBUFS;
1362                         stypes >>= 1;
1363                         i++;
1364                 }
1365                 nla_nest_end(msg, nl_ftypes);
1366         }
1367         nla_nest_end(msg, nl_ifs);
1368
1369         return 0;
1370 }
1371
1372 #define CMD(op, n)                                                      \
1373          do {                                                           \
1374                 if (rdev->ops->op) {                                    \
1375                         i++;                                            \
1376                         if (nla_put_u32(msg, i, NL80211_CMD_ ## n))     \
1377                                 goto nla_put_failure;                   \
1378                 }                                                       \
1379         } while (0)
1380
1381 static int nl80211_add_commands_unsplit(struct cfg80211_registered_device *rdev,
1382                                         struct sk_buff *msg)
1383 {
1384         int i = 0;
1385
1386         /*
1387          * do *NOT* add anything into this function, new things need to be
1388          * advertised only to new versions of userspace that can deal with
1389          * the split (and they can't possibly care about new features...
1390          */
1391         CMD(add_virtual_intf, NEW_INTERFACE);
1392         CMD(change_virtual_intf, SET_INTERFACE);
1393         CMD(add_key, NEW_KEY);
1394         CMD(start_ap, START_AP);
1395         CMD(add_station, NEW_STATION);
1396         CMD(add_mpath, NEW_MPATH);
1397         CMD(update_mesh_config, SET_MESH_CONFIG);
1398         CMD(change_bss, SET_BSS);
1399         CMD(auth, AUTHENTICATE);
1400         CMD(assoc, ASSOCIATE);
1401         CMD(deauth, DEAUTHENTICATE);
1402         CMD(disassoc, DISASSOCIATE);
1403         CMD(join_ibss, JOIN_IBSS);
1404         CMD(join_mesh, JOIN_MESH);
1405         CMD(set_pmksa, SET_PMKSA);
1406         CMD(del_pmksa, DEL_PMKSA);
1407         CMD(flush_pmksa, FLUSH_PMKSA);
1408         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1409                 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1410         CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1411         CMD(mgmt_tx, FRAME);
1412         CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1413         if (rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1414                 i++;
1415                 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1416                         goto nla_put_failure;
1417         }
1418         if (rdev->ops->set_monitor_channel || rdev->ops->start_ap ||
1419             rdev->ops->join_mesh) {
1420                 i++;
1421                 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1422                         goto nla_put_failure;
1423         }
1424         CMD(set_wds_peer, SET_WDS_PEER);
1425         if (rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1426                 CMD(tdls_mgmt, TDLS_MGMT);
1427                 CMD(tdls_oper, TDLS_OPER);
1428         }
1429         if (rdev->wiphy.max_sched_scan_reqs)
1430                 CMD(sched_scan_start, START_SCHED_SCAN);
1431         CMD(probe_client, PROBE_CLIENT);
1432         CMD(set_noack_map, SET_NOACK_MAP);
1433         if (rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1434                 i++;
1435                 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1436                         goto nla_put_failure;
1437         }
1438         CMD(start_p2p_device, START_P2P_DEVICE);
1439         CMD(set_mcast_rate, SET_MCAST_RATE);
1440 #ifdef CONFIG_NL80211_TESTMODE
1441         CMD(testmode_cmd, TESTMODE);
1442 #endif
1443
1444         if (rdev->ops->connect || rdev->ops->auth) {
1445                 i++;
1446                 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1447                         goto nla_put_failure;
1448         }
1449
1450         if (rdev->ops->disconnect || rdev->ops->deauth) {
1451                 i++;
1452                 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1453                         goto nla_put_failure;
1454         }
1455
1456         return i;
1457  nla_put_failure:
1458         return -ENOBUFS;
1459 }
1460
1461 struct nl80211_dump_wiphy_state {
1462         s64 filter_wiphy;
1463         long start;
1464         long split_start, band_start, chan_start, capa_start;
1465         bool split;
1466 };
1467
1468 static int nl80211_send_wiphy(struct cfg80211_registered_device *rdev,
1469                               enum nl80211_commands cmd,
1470                               struct sk_buff *msg, u32 portid, u32 seq,
1471                               int flags, struct nl80211_dump_wiphy_state *state)
1472 {
1473         void *hdr;
1474         struct nlattr *nl_bands, *nl_band;
1475         struct nlattr *nl_freqs, *nl_freq;
1476         struct nlattr *nl_cmds;
1477         enum nl80211_band band;
1478         struct ieee80211_channel *chan;
1479         int i;
1480         const struct ieee80211_txrx_stypes *mgmt_stypes =
1481                                 rdev->wiphy.mgmt_stypes;
1482         u32 features;
1483
1484         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
1485         if (!hdr)
1486                 return -ENOBUFS;
1487
1488         if (WARN_ON(!state))
1489                 return -EINVAL;
1490
1491         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1492             nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1493                            wiphy_name(&rdev->wiphy)) ||
1494             nla_put_u32(msg, NL80211_ATTR_GENERATION,
1495                         cfg80211_rdev_list_generation))
1496                 goto nla_put_failure;
1497
1498         if (cmd != NL80211_CMD_NEW_WIPHY)
1499                 goto finish;
1500
1501         switch (state->split_start) {
1502         case 0:
1503                 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1504                                rdev->wiphy.retry_short) ||
1505                     nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1506                                rdev->wiphy.retry_long) ||
1507                     nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1508                                 rdev->wiphy.frag_threshold) ||
1509                     nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1510                                 rdev->wiphy.rts_threshold) ||
1511                     nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1512                                rdev->wiphy.coverage_class) ||
1513                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1514                                rdev->wiphy.max_scan_ssids) ||
1515                     nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1516                                rdev->wiphy.max_sched_scan_ssids) ||
1517                     nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1518                                 rdev->wiphy.max_scan_ie_len) ||
1519                     nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1520                                 rdev->wiphy.max_sched_scan_ie_len) ||
1521                     nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1522                                rdev->wiphy.max_match_sets) ||
1523                     nla_put_u32(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_PLANS,
1524                                 rdev->wiphy.max_sched_scan_plans) ||
1525                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_INTERVAL,
1526                                 rdev->wiphy.max_sched_scan_plan_interval) ||
1527                     nla_put_u32(msg, NL80211_ATTR_MAX_SCAN_PLAN_ITERATIONS,
1528                                 rdev->wiphy.max_sched_scan_plan_iterations))
1529                         goto nla_put_failure;
1530
1531                 if ((rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1532                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1533                         goto nla_put_failure;
1534                 if ((rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1535                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1536                         goto nla_put_failure;
1537                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1538                     nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1539                         goto nla_put_failure;
1540                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1541                     nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1542                         goto nla_put_failure;
1543                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1544                     nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1545                         goto nla_put_failure;
1546                 if ((rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1547                     nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
1548                         goto nla_put_failure;
1549                 state->split_start++;
1550                 if (state->split)
1551                         break;
1552         case 1:
1553                 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1554                             sizeof(u32) * rdev->wiphy.n_cipher_suites,
1555                             rdev->wiphy.cipher_suites))
1556                         goto nla_put_failure;
1557
1558                 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1559                                rdev->wiphy.max_num_pmkids))
1560                         goto nla_put_failure;
1561
1562                 if ((rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1563                     nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1564                         goto nla_put_failure;
1565
1566                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1567                                 rdev->wiphy.available_antennas_tx) ||
1568                     nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1569                                 rdev->wiphy.available_antennas_rx))
1570                         goto nla_put_failure;
1571
1572                 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1573                     nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1574                                 rdev->wiphy.probe_resp_offload))
1575                         goto nla_put_failure;
1576
1577                 if ((rdev->wiphy.available_antennas_tx ||
1578                      rdev->wiphy.available_antennas_rx) &&
1579                     rdev->ops->get_antenna) {
1580                         u32 tx_ant = 0, rx_ant = 0;
1581                         int res;
1582
1583                         res = rdev_get_antenna(rdev, &tx_ant, &rx_ant);
1584                         if (!res) {
1585                                 if (nla_put_u32(msg,
1586                                                 NL80211_ATTR_WIPHY_ANTENNA_TX,
1587                                                 tx_ant) ||
1588                                     nla_put_u32(msg,
1589                                                 NL80211_ATTR_WIPHY_ANTENNA_RX,
1590                                                 rx_ant))
1591                                         goto nla_put_failure;
1592                         }
1593                 }
1594
1595                 state->split_start++;
1596                 if (state->split)
1597                         break;
1598         case 2:
1599                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1600                                         rdev->wiphy.interface_modes))
1601                                 goto nla_put_failure;
1602                 state->split_start++;
1603                 if (state->split)
1604                         break;
1605         case 3:
1606                 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1607                 if (!nl_bands)
1608                         goto nla_put_failure;
1609
1610                 for (band = state->band_start;
1611                      band < NUM_NL80211_BANDS; band++) {
1612                         struct ieee80211_supported_band *sband;
1613
1614                         sband = rdev->wiphy.bands[band];
1615
1616                         if (!sband)
1617                                 continue;
1618
1619                         nl_band = nla_nest_start(msg, band);
1620                         if (!nl_band)
1621                                 goto nla_put_failure;
1622
1623                         switch (state->chan_start) {
1624                         case 0:
1625                                 if (nl80211_send_band_rateinfo(msg, sband))
1626                                         goto nla_put_failure;
1627                                 state->chan_start++;
1628                                 if (state->split)
1629                                         break;
1630                         default:
1631                                 /* add frequencies */
1632                                 nl_freqs = nla_nest_start(
1633                                         msg, NL80211_BAND_ATTR_FREQS);
1634                                 if (!nl_freqs)
1635                                         goto nla_put_failure;
1636
1637                                 for (i = state->chan_start - 1;
1638                                      i < sband->n_channels;
1639                                      i++) {
1640                                         nl_freq = nla_nest_start(msg, i);
1641                                         if (!nl_freq)
1642                                                 goto nla_put_failure;
1643
1644                                         chan = &sband->channels[i];
1645
1646                                         if (nl80211_msg_put_channel(
1647                                                         msg, chan,
1648                                                         state->split))
1649                                                 goto nla_put_failure;
1650
1651                                         nla_nest_end(msg, nl_freq);
1652                                         if (state->split)
1653                                                 break;
1654                                 }
1655                                 if (i < sband->n_channels)
1656                                         state->chan_start = i + 2;
1657                                 else
1658                                         state->chan_start = 0;
1659                                 nla_nest_end(msg, nl_freqs);
1660                         }
1661
1662                         nla_nest_end(msg, nl_band);
1663
1664                         if (state->split) {
1665                                 /* start again here */
1666                                 if (state->chan_start)
1667                                         band--;
1668                                 break;
1669                         }
1670                 }
1671                 nla_nest_end(msg, nl_bands);
1672
1673                 if (band < NUM_NL80211_BANDS)
1674                         state->band_start = band + 1;
1675                 else
1676                         state->band_start = 0;
1677
1678                 /* if bands & channels are done, continue outside */
1679                 if (state->band_start == 0 && state->chan_start == 0)
1680                         state->split_start++;
1681                 if (state->split)
1682                         break;
1683         case 4:
1684                 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1685                 if (!nl_cmds)
1686                         goto nla_put_failure;
1687
1688                 i = nl80211_add_commands_unsplit(rdev, msg);
1689                 if (i < 0)
1690                         goto nla_put_failure;
1691                 if (state->split) {
1692                         CMD(crit_proto_start, CRIT_PROTOCOL_START);
1693                         CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1694                         if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1695                                 CMD(channel_switch, CHANNEL_SWITCH);
1696                         CMD(set_qos_map, SET_QOS_MAP);
1697                         if (rdev->wiphy.features &
1698                                         NL80211_FEATURE_SUPPORTS_WMM_ADMISSION)
1699                                 CMD(add_tx_ts, ADD_TX_TS);
1700                         CMD(set_multicast_to_unicast, SET_MULTICAST_TO_UNICAST);
1701                         CMD(update_connect_params, UPDATE_CONNECT_PARAMS);
1702                 }
1703 #undef CMD
1704
1705                 nla_nest_end(msg, nl_cmds);
1706                 state->split_start++;
1707                 if (state->split)
1708                         break;
1709         case 5:
1710                 if (rdev->ops->remain_on_channel &&
1711                     (rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1712                     nla_put_u32(msg,
1713                                 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1714                                 rdev->wiphy.max_remain_on_channel_duration))
1715                         goto nla_put_failure;
1716
1717                 if ((rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1718                     nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1719                         goto nla_put_failure;
1720
1721                 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1722                         goto nla_put_failure;
1723                 state->split_start++;
1724                 if (state->split)
1725                         break;
1726         case 6:
1727 #ifdef CONFIG_PM
1728                 if (nl80211_send_wowlan(msg, rdev, state->split))
1729                         goto nla_put_failure;
1730                 state->split_start++;
1731                 if (state->split)
1732                         break;
1733 #else
1734                 state->split_start++;
1735 #endif
1736         case 7:
1737                 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1738                                         rdev->wiphy.software_iftypes))
1739                         goto nla_put_failure;
1740
1741                 if (nl80211_put_iface_combinations(&rdev->wiphy, msg,
1742                                                    state->split))
1743                         goto nla_put_failure;
1744
1745                 state->split_start++;
1746                 if (state->split)
1747                         break;
1748         case 8:
1749                 if ((rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1750                     nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1751                                 rdev->wiphy.ap_sme_capa))
1752                         goto nla_put_failure;
1753
1754                 features = rdev->wiphy.features;
1755                 /*
1756                  * We can only add the per-channel limit information if the
1757                  * dump is split, otherwise it makes it too big. Therefore
1758                  * only advertise it in that case.
1759                  */
1760                 if (state->split)
1761                         features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1762                 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
1763                         goto nla_put_failure;
1764
1765                 if (rdev->wiphy.ht_capa_mod_mask &&
1766                     nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1767                             sizeof(*rdev->wiphy.ht_capa_mod_mask),
1768                             rdev->wiphy.ht_capa_mod_mask))
1769                         goto nla_put_failure;
1770
1771                 if (rdev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1772                     rdev->wiphy.max_acl_mac_addrs &&
1773                     nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1774                                 rdev->wiphy.max_acl_mac_addrs))
1775                         goto nla_put_failure;
1776
1777                 /*
1778                  * Any information below this point is only available to
1779                  * applications that can deal with it being split. This
1780                  * helps ensure that newly added capabilities don't break
1781                  * older tools by overrunning their buffers.
1782                  *
1783                  * We still increment split_start so that in the split
1784                  * case we'll continue with more data in the next round,
1785                  * but break unconditionally so unsplit data stops here.
1786                  */
1787                 if (state->split)
1788                         state->split_start++;
1789                 else
1790                         state->split_start = 0;
1791                 break;
1792         case 9:
1793                 if (rdev->wiphy.extended_capabilities &&
1794                     (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1795                              rdev->wiphy.extended_capabilities_len,
1796                              rdev->wiphy.extended_capabilities) ||
1797                      nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1798                              rdev->wiphy.extended_capabilities_len,
1799                              rdev->wiphy.extended_capabilities_mask)))
1800                         goto nla_put_failure;
1801
1802                 if (rdev->wiphy.vht_capa_mod_mask &&
1803                     nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1804                             sizeof(*rdev->wiphy.vht_capa_mod_mask),
1805                             rdev->wiphy.vht_capa_mod_mask))
1806                         goto nla_put_failure;
1807
1808                 state->split_start++;
1809                 break;
1810         case 10:
1811                 if (nl80211_send_coalesce(msg, rdev))
1812                         goto nla_put_failure;
1813
1814                 if ((rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1815                     (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1816                      nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1817                         goto nla_put_failure;
1818
1819                 if (rdev->wiphy.max_ap_assoc_sta &&
1820                     nla_put_u32(msg, NL80211_ATTR_MAX_AP_ASSOC_STA,
1821                                 rdev->wiphy.max_ap_assoc_sta))
1822                         goto nla_put_failure;
1823
1824                 state->split_start++;
1825                 break;
1826         case 11:
1827                 if (rdev->wiphy.n_vendor_commands) {
1828                         const struct nl80211_vendor_cmd_info *info;
1829                         struct nlattr *nested;
1830
1831                         nested = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1832                         if (!nested)
1833                                 goto nla_put_failure;
1834
1835                         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
1836                                 info = &rdev->wiphy.vendor_commands[i].info;
1837                                 if (nla_put(msg, i + 1, sizeof(*info), info))
1838                                         goto nla_put_failure;
1839                         }
1840                         nla_nest_end(msg, nested);
1841                 }
1842
1843                 if (rdev->wiphy.n_vendor_events) {
1844                         const struct nl80211_vendor_cmd_info *info;
1845                         struct nlattr *nested;
1846
1847                         nested = nla_nest_start(msg,
1848                                                 NL80211_ATTR_VENDOR_EVENTS);
1849                         if (!nested)
1850                                 goto nla_put_failure;
1851
1852                         for (i = 0; i < rdev->wiphy.n_vendor_events; i++) {
1853                                 info = &rdev->wiphy.vendor_events[i];
1854                                 if (nla_put(msg, i + 1, sizeof(*info), info))
1855                                         goto nla_put_failure;
1856                         }
1857                         nla_nest_end(msg, nested);
1858                 }
1859                 state->split_start++;
1860                 break;
1861         case 12:
1862                 if (rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH &&
1863                     nla_put_u8(msg, NL80211_ATTR_MAX_CSA_COUNTERS,
1864                                rdev->wiphy.max_num_csa_counters))
1865                         goto nla_put_failure;
1866
1867                 if (rdev->wiphy.regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
1868                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
1869                         goto nla_put_failure;
1870
1871                 if (rdev->wiphy.max_sched_scan_reqs &&
1872                     nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_MAX_REQS,
1873                                 rdev->wiphy.max_sched_scan_reqs))
1874                         goto nla_put_failure;
1875
1876                 if (nla_put(msg, NL80211_ATTR_EXT_FEATURES,
1877                             sizeof(rdev->wiphy.ext_features),
1878                             rdev->wiphy.ext_features))
1879                         goto nla_put_failure;
1880
1881                 if (rdev->wiphy.bss_select_support) {
1882                         struct nlattr *nested;
1883                         u32 bss_select_support = rdev->wiphy.bss_select_support;
1884
1885                         nested = nla_nest_start(msg, NL80211_ATTR_BSS_SELECT);
1886                         if (!nested)
1887                                 goto nla_put_failure;
1888
1889                         i = 0;
1890                         while (bss_select_support) {
1891                                 if ((bss_select_support & 1) &&
1892                                     nla_put_flag(msg, i))
1893                                         goto nla_put_failure;
1894                                 i++;
1895                                 bss_select_support >>= 1;
1896                         }
1897                         nla_nest_end(msg, nested);
1898                 }
1899
1900                 state->split_start++;
1901                 break;
1902         case 13:
1903                 if (rdev->wiphy.num_iftype_ext_capab &&
1904                     rdev->wiphy.iftype_ext_capab) {
1905                         struct nlattr *nested_ext_capab, *nested;
1906
1907                         nested = nla_nest_start(msg,
1908                                                 NL80211_ATTR_IFTYPE_EXT_CAPA);
1909                         if (!nested)
1910                                 goto nla_put_failure;
1911
1912                         for (i = state->capa_start;
1913                              i < rdev->wiphy.num_iftype_ext_capab; i++) {
1914                                 const struct wiphy_iftype_ext_capab *capab;
1915
1916                                 capab = &rdev->wiphy.iftype_ext_capab[i];
1917
1918                                 nested_ext_capab = nla_nest_start(msg, i);
1919                                 if (!nested_ext_capab ||
1920                                     nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1921                                                 capab->iftype) ||
1922                                     nla_put(msg, NL80211_ATTR_EXT_CAPA,
1923                                             capab->extended_capabilities_len,
1924                                             capab->extended_capabilities) ||
1925                                     nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1926                                             capab->extended_capabilities_len,
1927                                             capab->extended_capabilities_mask))
1928                                         goto nla_put_failure;
1929
1930                                 nla_nest_end(msg, nested_ext_capab);
1931                                 if (state->split)
1932                                         break;
1933                         }
1934                         nla_nest_end(msg, nested);
1935                         if (i < rdev->wiphy.num_iftype_ext_capab) {
1936                                 state->capa_start = i + 1;
1937                                 break;
1938                         }
1939                 }
1940
1941                 if (nla_put_u32(msg, NL80211_ATTR_BANDS,
1942                                 rdev->wiphy.nan_supported_bands))
1943                         goto nla_put_failure;
1944
1945                 /* done */
1946                 state->split_start = 0;
1947                 break;
1948         }
1949  finish:
1950         genlmsg_end(msg, hdr);
1951         return 0;
1952
1953  nla_put_failure:
1954         genlmsg_cancel(msg, hdr);
1955         return -EMSGSIZE;
1956 }
1957
1958 static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1959                                     struct netlink_callback *cb,
1960                                     struct nl80211_dump_wiphy_state *state)
1961 {
1962         struct nlattr **tb = genl_family_attrbuf(&nl80211_fam);
1963         int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, tb,
1964                               nl80211_fam.maxattr, nl80211_policy, NULL);
1965         /* ignore parse errors for backward compatibility */
1966         if (ret)
1967                 return 0;
1968
1969         state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1970         if (tb[NL80211_ATTR_WIPHY])
1971                 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1972         if (tb[NL80211_ATTR_WDEV])
1973                 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1974         if (tb[NL80211_ATTR_IFINDEX]) {
1975                 struct net_device *netdev;
1976                 struct cfg80211_registered_device *rdev;
1977                 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1978
1979                 netdev = __dev_get_by_index(sock_net(skb->sk), ifidx);
1980                 if (!netdev)
1981                         return -ENODEV;
1982                 if (netdev->ieee80211_ptr) {
1983                         rdev = wiphy_to_rdev(
1984                                 netdev->ieee80211_ptr->wiphy);
1985                         state->filter_wiphy = rdev->wiphy_idx;
1986                 }
1987         }
1988
1989         return 0;
1990 }
1991
1992 static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1993 {
1994         int idx = 0, ret;
1995         struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
1996         struct cfg80211_registered_device *rdev;
1997
1998         rtnl_lock();
1999         if (!state) {
2000                 state = kzalloc(sizeof(*state), GFP_KERNEL);
2001                 if (!state) {
2002                         rtnl_unlock();
2003                         return -ENOMEM;
2004                 }
2005                 state->filter_wiphy = -1;
2006                 ret = nl80211_dump_wiphy_parse(skb, cb, state);
2007                 if (ret) {
2008                         kfree(state);
2009                         rtnl_unlock();
2010                         return ret;
2011                 }
2012                 cb->args[0] = (long)state;
2013         }
2014
2015         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2016                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2017                         continue;
2018                 if (++idx <= state->start)
2019                         continue;
2020                 if (state->filter_wiphy != -1 &&
2021                     state->filter_wiphy != rdev->wiphy_idx)
2022                         continue;
2023                 /* attempt to fit multiple wiphy data chunks into the skb */
2024                 do {
2025                         ret = nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY,
2026                                                  skb,
2027                                                  NETLINK_CB(cb->skb).portid,
2028                                                  cb->nlh->nlmsg_seq,
2029                                                  NLM_F_MULTI, state);
2030                         if (ret < 0) {
2031                                 /*
2032                                  * If sending the wiphy data didn't fit (ENOBUFS
2033                                  * or EMSGSIZE returned), this SKB is still
2034                                  * empty (so it's not too big because another
2035                                  * wiphy dataset is already in the skb) and
2036                                  * we've not tried to adjust the dump allocation
2037                                  * yet ... then adjust the alloc size to be
2038                                  * bigger, and return 1 but with the empty skb.
2039                                  * This results in an empty message being RX'ed
2040                                  * in userspace, but that is ignored.
2041                                  *
2042                                  * We can then retry with the larger buffer.
2043                                  */
2044                                 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
2045                                     !skb->len && !state->split &&
2046                                     cb->min_dump_alloc < 4096) {
2047                                         cb->min_dump_alloc = 4096;
2048                                         state->split_start = 0;
2049                                         rtnl_unlock();
2050                                         return 1;
2051                                 }
2052                                 idx--;
2053                                 break;
2054                         }
2055                 } while (state->split_start > 0);
2056                 break;
2057         }
2058         rtnl_unlock();
2059
2060         state->start = idx;
2061
2062         return skb->len;
2063 }
2064
2065 static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
2066 {
2067         kfree((void *)cb->args[0]);
2068         return 0;
2069 }
2070
2071 static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
2072 {
2073         struct sk_buff *msg;
2074         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2075         struct nl80211_dump_wiphy_state state = {};
2076
2077         msg = nlmsg_new(4096, GFP_KERNEL);
2078         if (!msg)
2079                 return -ENOMEM;
2080
2081         if (nl80211_send_wiphy(rdev, NL80211_CMD_NEW_WIPHY, msg,
2082                                info->snd_portid, info->snd_seq, 0,
2083                                &state) < 0) {
2084                 nlmsg_free(msg);
2085                 return -ENOBUFS;
2086         }
2087
2088         return genlmsg_reply(msg, info);
2089 }
2090
2091 static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
2092         [NL80211_TXQ_ATTR_QUEUE]                = { .type = NLA_U8 },
2093         [NL80211_TXQ_ATTR_TXOP]                 = { .type = NLA_U16 },
2094         [NL80211_TXQ_ATTR_CWMIN]                = { .type = NLA_U16 },
2095         [NL80211_TXQ_ATTR_CWMAX]                = { .type = NLA_U16 },
2096         [NL80211_TXQ_ATTR_AIFS]                 = { .type = NLA_U8 },
2097 };
2098
2099 static int parse_txq_params(struct nlattr *tb[],
2100                             struct ieee80211_txq_params *txq_params)
2101 {
2102         u8 ac;
2103
2104         if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
2105             !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
2106             !tb[NL80211_TXQ_ATTR_AIFS])
2107                 return -EINVAL;
2108
2109         ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
2110         txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
2111         txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
2112         txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
2113         txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
2114
2115         if (ac >= NL80211_NUM_ACS)
2116                 return -EINVAL;
2117         txq_params->ac = array_index_nospec(ac, NL80211_NUM_ACS);
2118         return 0;
2119 }
2120
2121 static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
2122 {
2123         /*
2124          * You can only set the channel explicitly for WDS interfaces,
2125          * all others have their channel managed via their respective
2126          * "establish a connection" command (connect, join, ...)
2127          *
2128          * For AP/GO and mesh mode, the channel can be set with the
2129          * channel userspace API, but is only stored and passed to the
2130          * low-level driver when the AP starts or the mesh is joined.
2131          * This is for backward compatibility, userspace can also give
2132          * the channel in the start-ap or join-mesh commands instead.
2133          *
2134          * Monitors are special as they are normally slaved to
2135          * whatever else is going on, so they have their own special
2136          * operation to set the monitor channel if possible.
2137          */
2138         return !wdev ||
2139                 wdev->iftype == NL80211_IFTYPE_AP ||
2140                 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
2141                 wdev->iftype == NL80211_IFTYPE_MONITOR ||
2142                 wdev->iftype == NL80211_IFTYPE_P2P_GO;
2143 }
2144
2145 static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
2146                                  struct genl_info *info,
2147                                  struct cfg80211_chan_def *chandef)
2148 {
2149         u32 control_freq;
2150
2151         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
2152                 return -EINVAL;
2153
2154         control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
2155
2156         memset(chandef, 0, sizeof(*chandef));
2157
2158         chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
2159         chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
2160         chandef->center_freq1 = control_freq;
2161         chandef->center_freq2 = 0;
2162
2163         /* Primary channel not allowed */
2164         if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
2165                 return -EINVAL;
2166
2167         if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
2168                 enum nl80211_channel_type chantype;
2169
2170                 chantype = nla_get_u32(
2171                                 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
2172
2173                 switch (chantype) {
2174                 case NL80211_CHAN_NO_HT:
2175                 case NL80211_CHAN_HT20:
2176                 case NL80211_CHAN_HT40PLUS:
2177                 case NL80211_CHAN_HT40MINUS:
2178                         cfg80211_chandef_create(chandef, chandef->chan,
2179                                                 chantype);
2180                         break;
2181                 default:
2182                         return -EINVAL;
2183                 }
2184         } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
2185                 chandef->width =
2186                         nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
2187                 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
2188                         chandef->center_freq1 =
2189                                 nla_get_u32(
2190                                         info->attrs[NL80211_ATTR_CENTER_FREQ1]);
2191                 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
2192                         chandef->center_freq2 =
2193                                 nla_get_u32(
2194                                         info->attrs[NL80211_ATTR_CENTER_FREQ2]);
2195         }
2196
2197         if (!cfg80211_chandef_valid(chandef))
2198                 return -EINVAL;
2199
2200         if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
2201                                      IEEE80211_CHAN_DISABLED))
2202                 return -EINVAL;
2203
2204         if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
2205              chandef->width == NL80211_CHAN_WIDTH_10) &&
2206             !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
2207                 return -EINVAL;
2208
2209         return 0;
2210 }
2211
2212 static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
2213                                  struct net_device *dev,
2214                                  struct genl_info *info)
2215 {
2216         struct cfg80211_chan_def chandef;
2217         int result;
2218         enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
2219         struct wireless_dev *wdev = NULL;
2220
2221         if (dev)
2222                 wdev = dev->ieee80211_ptr;
2223         if (!nl80211_can_set_dev_channel(wdev))
2224                 return -EOPNOTSUPP;
2225         if (wdev)
2226                 iftype = wdev->iftype;
2227
2228         result = nl80211_parse_chandef(rdev, info, &chandef);
2229         if (result)
2230                 return result;
2231
2232         switch (iftype) {
2233         case NL80211_IFTYPE_AP:
2234         case NL80211_IFTYPE_P2P_GO:
2235                 if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
2236                                                    iftype)) {
2237                         result = -EINVAL;
2238                         break;
2239                 }
2240                 if (wdev->beacon_interval) {
2241                         if (!dev || !rdev->ops->set_ap_chanwidth ||
2242                             !(rdev->wiphy.features &
2243                               NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE)) {
2244                                 result = -EBUSY;
2245                                 break;
2246                         }
2247
2248                         /* Only allow dynamic channel width changes */
2249                         if (chandef.chan != wdev->preset_chandef.chan) {
2250                                 result = -EBUSY;
2251                                 break;
2252                         }
2253                         result = rdev_set_ap_chanwidth(rdev, dev, &chandef);
2254                         if (result)
2255                                 break;
2256                 }
2257                 wdev->preset_chandef = chandef;
2258                 result = 0;
2259                 break;
2260         case NL80211_IFTYPE_MESH_POINT:
2261                 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
2262                 break;
2263         case NL80211_IFTYPE_MONITOR:
2264                 result = cfg80211_set_monitor_channel(rdev, &chandef);
2265                 break;
2266         default:
2267                 result = -EINVAL;
2268         }
2269
2270         return result;
2271 }
2272
2273 static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
2274 {
2275         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2276         struct net_device *netdev = info->user_ptr[1];
2277
2278         return __nl80211_set_channel(rdev, netdev, info);
2279 }
2280
2281 static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
2282 {
2283         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2284         struct net_device *dev = info->user_ptr[1];
2285         struct wireless_dev *wdev = dev->ieee80211_ptr;
2286         const u8 *bssid;
2287
2288         if (!info->attrs[NL80211_ATTR_MAC])
2289                 return -EINVAL;
2290
2291         if (netif_running(dev))
2292                 return -EBUSY;
2293
2294         if (!rdev->ops->set_wds_peer)
2295                 return -EOPNOTSUPP;
2296
2297         if (wdev->iftype != NL80211_IFTYPE_WDS)
2298                 return -EOPNOTSUPP;
2299
2300         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
2301         return rdev_set_wds_peer(rdev, dev, bssid);
2302 }
2303
2304 static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
2305 {
2306         struct cfg80211_registered_device *rdev;
2307         struct net_device *netdev = NULL;
2308         struct wireless_dev *wdev;
2309         int result = 0, rem_txq_params = 0;
2310         struct nlattr *nl_txq_params;
2311         u32 changed;
2312         u8 retry_short = 0, retry_long = 0;
2313         u32 frag_threshold = 0, rts_threshold = 0;
2314         u8 coverage_class = 0;
2315
2316         ASSERT_RTNL();
2317
2318         /*
2319          * Try to find the wiphy and netdev. Normally this
2320          * function shouldn't need the netdev, but this is
2321          * done for backward compatibility -- previously
2322          * setting the channel was done per wiphy, but now
2323          * it is per netdev. Previous userland like hostapd
2324          * also passed a netdev to set_wiphy, so that it is
2325          * possible to let that go to the right netdev!
2326          */
2327
2328         if (info->attrs[NL80211_ATTR_IFINDEX]) {
2329                 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
2330
2331                 netdev = __dev_get_by_index(genl_info_net(info), ifindex);
2332                 if (netdev && netdev->ieee80211_ptr)
2333                         rdev = wiphy_to_rdev(netdev->ieee80211_ptr->wiphy);
2334                 else
2335                         netdev = NULL;
2336         }
2337
2338         if (!netdev) {
2339                 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
2340                                                   info->attrs);
2341                 if (IS_ERR(rdev))
2342                         return PTR_ERR(rdev);
2343                 wdev = NULL;
2344                 netdev = NULL;
2345                 result = 0;
2346         } else
2347                 wdev = netdev->ieee80211_ptr;
2348
2349         /*
2350          * end workaround code, by now the rdev is available
2351          * and locked, and wdev may or may not be NULL.
2352          */
2353
2354         if (info->attrs[NL80211_ATTR_WIPHY_NAME])
2355                 result = cfg80211_dev_rename(
2356                         rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
2357
2358         if (result)
2359                 return result;
2360
2361         if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
2362                 struct ieee80211_txq_params txq_params;
2363                 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
2364
2365                 if (!rdev->ops->set_txq_params)
2366                         return -EOPNOTSUPP;
2367
2368                 if (!netdev)
2369                         return -EINVAL;
2370
2371                 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2372                     netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2373                         return -EINVAL;
2374
2375                 if (!netif_running(netdev))
2376                         return -ENETDOWN;
2377
2378                 nla_for_each_nested(nl_txq_params,
2379                                     info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2380                                     rem_txq_params) {
2381                         result = nla_parse_nested(tb, NL80211_TXQ_ATTR_MAX,
2382                                                   nl_txq_params,
2383                                                   txq_params_policy,
2384                                                   info->extack);
2385                         if (result)
2386                                 return result;
2387                         result = parse_txq_params(tb, &txq_params);
2388                         if (result)
2389                                 return result;
2390
2391                         result = rdev_set_txq_params(rdev, netdev,
2392                                                      &txq_params);
2393                         if (result)
2394                                 return result;
2395                 }
2396         }
2397
2398         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2399                 result = __nl80211_set_channel(
2400                         rdev,
2401                         nl80211_can_set_dev_channel(wdev) ? netdev : NULL,
2402                         info);
2403                 if (result)
2404                         return result;
2405         }
2406
2407         if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
2408                 struct wireless_dev *txp_wdev = wdev;
2409                 enum nl80211_tx_power_setting type;
2410                 int idx, mbm = 0;
2411
2412                 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2413                         txp_wdev = NULL;
2414
2415                 if (!rdev->ops->set_tx_power)
2416                         return -EOPNOTSUPP;
2417
2418                 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2419                 type = nla_get_u32(info->attrs[idx]);
2420
2421                 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2422                     (type != NL80211_TX_POWER_AUTOMATIC))
2423                         return -EINVAL;
2424
2425                 if (type != NL80211_TX_POWER_AUTOMATIC) {
2426                         idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2427                         mbm = nla_get_u32(info->attrs[idx]);
2428                 }
2429
2430                 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
2431                 if (result)
2432                         return result;
2433         }
2434
2435         if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2436             info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2437                 u32 tx_ant, rx_ant;
2438
2439                 if ((!rdev->wiphy.available_antennas_tx &&
2440                      !rdev->wiphy.available_antennas_rx) ||
2441                     !rdev->ops->set_antenna)
2442                         return -EOPNOTSUPP;
2443
2444                 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2445                 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2446
2447                 /* reject antenna configurations which don't match the
2448                  * available antenna masks, except for the "all" mask */
2449                 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2450                     (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx)))
2451                         return -EINVAL;
2452
2453                 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2454                 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
2455
2456                 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
2457                 if (result)
2458                         return result;
2459         }
2460
2461         changed = 0;
2462
2463         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2464                 retry_short = nla_get_u8(
2465                         info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2466                 if (retry_short == 0)
2467                         return -EINVAL;
2468
2469                 changed |= WIPHY_PARAM_RETRY_SHORT;
2470         }
2471
2472         if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2473                 retry_long = nla_get_u8(
2474                         info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2475                 if (retry_long == 0)
2476                         return -EINVAL;
2477
2478                 changed |= WIPHY_PARAM_RETRY_LONG;
2479         }
2480
2481         if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2482                 frag_threshold = nla_get_u32(
2483                         info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2484                 if (frag_threshold < 256)
2485                         return -EINVAL;
2486
2487                 if (frag_threshold != (u32) -1) {
2488                         /*
2489                          * Fragments (apart from the last one) are required to
2490                          * have even length. Make the fragmentation code
2491                          * simpler by stripping LSB should someone try to use
2492                          * odd threshold value.
2493                          */
2494                         frag_threshold &= ~0x1;
2495                 }
2496                 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2497         }
2498
2499         if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2500                 rts_threshold = nla_get_u32(
2501                         info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2502                 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2503         }
2504
2505         if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2506                 if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK])
2507                         return -EINVAL;
2508
2509                 coverage_class = nla_get_u8(
2510                         info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2511                 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2512         }
2513
2514         if (info->attrs[NL80211_ATTR_WIPHY_DYN_ACK]) {
2515                 if (!(rdev->wiphy.features & NL80211_FEATURE_ACKTO_ESTIMATION))
2516                         return -EOPNOTSUPP;
2517
2518                 changed |= WIPHY_PARAM_DYN_ACK;
2519         }
2520
2521         if (changed) {
2522                 u8 old_retry_short, old_retry_long;
2523                 u32 old_frag_threshold, old_rts_threshold;
2524                 u8 old_coverage_class;
2525
2526                 if (!rdev->ops->set_wiphy_params)
2527                         return -EOPNOTSUPP;
2528
2529                 old_retry_short = rdev->wiphy.retry_short;
2530                 old_retry_long = rdev->wiphy.retry_long;
2531                 old_frag_threshold = rdev->wiphy.frag_threshold;
2532                 old_rts_threshold = rdev->wiphy.rts_threshold;
2533                 old_coverage_class = rdev->wiphy.coverage_class;
2534
2535                 if (changed & WIPHY_PARAM_RETRY_SHORT)
2536                         rdev->wiphy.retry_short = retry_short;
2537                 if (changed & WIPHY_PARAM_RETRY_LONG)
2538                         rdev->wiphy.retry_long = retry_long;
2539                 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2540                         rdev->wiphy.frag_threshold = frag_threshold;
2541                 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2542                         rdev->wiphy.rts_threshold = rts_threshold;
2543                 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2544                         rdev->wiphy.coverage_class = coverage_class;
2545
2546                 result = rdev_set_wiphy_params(rdev, changed);
2547                 if (result) {
2548                         rdev->wiphy.retry_short = old_retry_short;
2549                         rdev->wiphy.retry_long = old_retry_long;
2550                         rdev->wiphy.frag_threshold = old_frag_threshold;
2551                         rdev->wiphy.rts_threshold = old_rts_threshold;
2552                         rdev->wiphy.coverage_class = old_coverage_class;
2553                         return result;
2554                 }
2555         }
2556         return 0;
2557 }
2558
2559 static inline u64 wdev_id(struct wireless_dev *wdev)
2560 {
2561         return (u64)wdev->identifier |
2562                ((u64)wiphy_to_rdev(wdev->wiphy)->wiphy_idx << 32);
2563 }
2564
2565 static int nl80211_send_chandef(struct sk_buff *msg,
2566                                 const struct cfg80211_chan_def *chandef)
2567 {
2568         if (WARN_ON(!cfg80211_chandef_valid(chandef)))
2569                 return -EINVAL;
2570
2571         if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2572                         chandef->chan->center_freq))
2573                 return -ENOBUFS;
2574         switch (chandef->width) {
2575         case NL80211_CHAN_WIDTH_20_NOHT:
2576         case NL80211_CHAN_WIDTH_20:
2577         case NL80211_CHAN_WIDTH_40:
2578                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2579                                 cfg80211_get_chandef_type(chandef)))
2580                         return -ENOBUFS;
2581                 break;
2582         default:
2583                 break;
2584         }
2585         if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2586                 return -ENOBUFS;
2587         if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2588                 return -ENOBUFS;
2589         if (chandef->center_freq2 &&
2590             nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
2591                 return -ENOBUFS;
2592         return 0;
2593 }
2594
2595 static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
2596                               struct cfg80211_registered_device *rdev,
2597                               struct wireless_dev *wdev, bool removal)
2598 {
2599         struct net_device *dev = wdev->netdev;
2600         u8 cmd = NL80211_CMD_NEW_INTERFACE;
2601         void *hdr;
2602
2603         if (removal)
2604                 cmd = NL80211_CMD_DEL_INTERFACE;
2605
2606         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
2607         if (!hdr)
2608                 return -1;
2609
2610         if (dev &&
2611             (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2612              nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
2613                 goto nla_put_failure;
2614
2615         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2616             nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
2617             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
2618                               NL80211_ATTR_PAD) ||
2619             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
2620             nla_put_u32(msg, NL80211_ATTR_GENERATION,
2621                         rdev->devlist_generation ^
2622                         (cfg80211_rdev_list_generation << 2)))
2623                 goto nla_put_failure;
2624
2625         if (rdev->ops->get_channel) {
2626                 int ret;
2627                 struct cfg80211_chan_def chandef = {};
2628
2629                 ret = rdev_get_channel(rdev, wdev, &chandef);
2630                 if (ret == 0) {
2631                         if (nl80211_send_chandef(msg, &chandef))
2632                                 goto nla_put_failure;
2633                 }
2634         }
2635
2636         if (rdev->ops->get_tx_power) {
2637                 int dbm, ret;
2638
2639                 ret = rdev_get_tx_power(rdev, wdev, &dbm);
2640                 if (ret == 0 &&
2641                     nla_put_u32(msg, NL80211_ATTR_WIPHY_TX_POWER_LEVEL,
2642                                 DBM_TO_MBM(dbm)))
2643                         goto nla_put_failure;
2644         }
2645
2646         if (wdev->ssid_len) {
2647                 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2648                         goto nla_put_failure;
2649         }
2650
2651         genlmsg_end(msg, hdr);
2652         return 0;
2653
2654  nla_put_failure:
2655         genlmsg_cancel(msg, hdr);
2656         return -EMSGSIZE;
2657 }
2658
2659 static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2660 {
2661         int wp_idx = 0;
2662         int if_idx = 0;
2663         int wp_start = cb->args[0];
2664         int if_start = cb->args[1];
2665         int filter_wiphy = -1;
2666         struct cfg80211_registered_device *rdev;
2667         struct wireless_dev *wdev;
2668         int ret;
2669
2670         rtnl_lock();
2671         if (!cb->args[2]) {
2672                 struct nl80211_dump_wiphy_state state = {
2673                         .filter_wiphy = -1,
2674                 };
2675
2676                 ret = nl80211_dump_wiphy_parse(skb, cb, &state);
2677                 if (ret)
2678                         goto out_unlock;
2679
2680                 filter_wiphy = state.filter_wiphy;
2681
2682                 /*
2683                  * if filtering, set cb->args[2] to +1 since 0 is the default
2684                  * value needed to determine that parsing is necessary.
2685                  */
2686                 if (filter_wiphy >= 0)
2687                         cb->args[2] = filter_wiphy + 1;
2688                 else
2689                         cb->args[2] = -1;
2690         } else if (cb->args[2] > 0) {
2691                 filter_wiphy = cb->args[2] - 1;
2692         }
2693
2694         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2695                 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
2696                         continue;
2697                 if (wp_idx < wp_start) {
2698                         wp_idx++;
2699                         continue;
2700                 }
2701
2702                 if (filter_wiphy >= 0 && filter_wiphy != rdev->wiphy_idx)
2703                         continue;
2704
2705                 if_idx = 0;
2706
2707                 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
2708                         if (if_idx < if_start) {
2709                                 if_idx++;
2710                                 continue;
2711                         }
2712                         if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
2713                                                cb->nlh->nlmsg_seq, NLM_F_MULTI,
2714                                                rdev, wdev, false) < 0) {
2715                                 goto out;
2716                         }
2717                         if_idx++;
2718                 }
2719
2720                 wp_idx++;
2721         }
2722  out:
2723         cb->args[0] = wp_idx;
2724         cb->args[1] = if_idx;
2725
2726         ret = skb->len;
2727  out_unlock:
2728         rtnl_unlock();
2729
2730         return ret;
2731 }
2732
2733 static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2734 {
2735         struct sk_buff *msg;
2736         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2737         struct wireless_dev *wdev = info->user_ptr[1];
2738
2739         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2740         if (!msg)
2741                 return -ENOMEM;
2742
2743         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
2744                                rdev, wdev, false) < 0) {
2745                 nlmsg_free(msg);
2746                 return -ENOBUFS;
2747         }
2748
2749         return genlmsg_reply(msg, info);
2750 }
2751
2752 static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2753         [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2754         [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2755         [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2756         [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2757         [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
2758         [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
2759 };
2760
2761 static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2762 {
2763         struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2764         int flag;
2765
2766         *mntrflags = 0;
2767
2768         if (!nla)
2769                 return -EINVAL;
2770
2771         if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX, nla,
2772                              mntr_flags_policy, NULL))
2773                 return -EINVAL;
2774
2775         for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2776                 if (flags[flag])
2777                         *mntrflags |= (1<<flag);
2778
2779         *mntrflags |= MONITOR_FLAG_CHANGED;
2780
2781         return 0;
2782 }
2783
2784 static int nl80211_parse_mon_options(struct cfg80211_registered_device *rdev,
2785                                      enum nl80211_iftype type,
2786                                      struct genl_info *info,
2787                                      struct vif_params *params)
2788 {
2789         bool change = false;
2790         int err;
2791
2792         if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
2793                 if (type != NL80211_IFTYPE_MONITOR)
2794                         return -EINVAL;
2795
2796                 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2797                                           &params->flags);
2798                 if (err)
2799                         return err;
2800
2801                 change = true;
2802         }
2803
2804         if (params->flags & MONITOR_FLAG_ACTIVE &&
2805             !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2806                 return -EOPNOTSUPP;
2807
2808         if (info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]) {
2809                 const u8 *mumimo_groups;
2810                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2811
2812                 if (type != NL80211_IFTYPE_MONITOR)
2813                         return -EINVAL;
2814
2815                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2816                         return -EOPNOTSUPP;
2817
2818                 mumimo_groups =
2819                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_GROUP_DATA]);
2820
2821                 /* bits 0 and 63 are reserved and must be zero */
2822                 if ((mumimo_groups[0] & BIT(0)) ||
2823                     (mumimo_groups[VHT_MUMIMO_GROUPS_DATA_LEN - 1] & BIT(7)))
2824                         return -EINVAL;
2825
2826                 params->vht_mumimo_groups = mumimo_groups;
2827                 change = true;
2828         }
2829
2830         if (info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]) {
2831                 u32 cap_flag = NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER;
2832
2833                 if (type != NL80211_IFTYPE_MONITOR)
2834                         return -EINVAL;
2835
2836                 if (!wiphy_ext_feature_isset(&rdev->wiphy, cap_flag))
2837                         return -EOPNOTSUPP;
2838
2839                 params->vht_mumimo_follow_addr =
2840                         nla_data(info->attrs[NL80211_ATTR_MU_MIMO_FOLLOW_MAC_ADDR]);
2841                 change = true;
2842         }
2843
2844         return change ? 1 : 0;
2845 }
2846
2847 static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
2848                                struct net_device *netdev, u8 use_4addr,
2849                                enum nl80211_iftype iftype)
2850 {
2851         if (!use_4addr) {
2852                 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
2853                         return -EBUSY;
2854                 return 0;
2855         }
2856
2857         switch (iftype) {
2858         case NL80211_IFTYPE_AP_VLAN:
2859                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2860                         return 0;
2861                 break;
2862         case NL80211_IFTYPE_STATION:
2863                 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2864                         return 0;
2865                 break;
2866         default:
2867                 break;
2868         }
2869
2870         return -EOPNOTSUPP;
2871 }
2872
2873 static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2874 {
2875         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2876         struct vif_params params;
2877         int err;
2878         enum nl80211_iftype otype, ntype;
2879         struct net_device *dev = info->user_ptr[1];
2880         bool change = false;
2881
2882         memset(&params, 0, sizeof(params));
2883
2884         otype = ntype = dev->ieee80211_ptr->iftype;
2885
2886         if (info->attrs[NL80211_ATTR_IFTYPE]) {
2887                 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2888                 if (otype != ntype)
2889                         change = true;
2890                 if (ntype > NL80211_IFTYPE_MAX)
2891                         return -EINVAL;
2892         }
2893
2894         if (info->attrs[NL80211_ATTR_MESH_ID]) {
2895                 struct wireless_dev *wdev = dev->ieee80211_ptr;
2896
2897                 if (ntype != NL80211_IFTYPE_MESH_POINT)
2898                         return -EINVAL;
2899                 if (netif_running(dev))
2900                         return -EBUSY;
2901
2902                 wdev_lock(wdev);
2903                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2904                              IEEE80211_MAX_MESH_ID_LEN);
2905                 wdev->mesh_id_up_len =
2906                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2907                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2908                        wdev->mesh_id_up_len);
2909                 wdev_unlock(wdev);
2910         }
2911
2912         if (info->attrs[NL80211_ATTR_4ADDR]) {
2913                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2914                 change = true;
2915                 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
2916                 if (err)
2917                         return err;
2918         } else {
2919                 params.use_4addr = -1;
2920         }
2921
2922         err = nl80211_parse_mon_options(rdev, ntype, info, &params);
2923         if (err < 0)
2924                 return err;
2925         if (err > 0)
2926                 change = true;
2927
2928         if (change)
2929                 err = cfg80211_change_iface(rdev, dev, ntype, &params);
2930         else
2931                 err = 0;
2932
2933         if (!err && params.use_4addr != -1)
2934                 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2935
2936         return err;
2937 }
2938
2939 static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2940 {
2941         struct cfg80211_registered_device *rdev = info->user_ptr[0];
2942         struct vif_params params;
2943         struct wireless_dev *wdev;
2944         struct sk_buff *msg;
2945         int err;
2946         enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
2947
2948         /* to avoid failing a new interface creation due to pending removal */
2949         cfg80211_destroy_ifaces(rdev);
2950
2951         memset(&params, 0, sizeof(params));
2952
2953         if (!info->attrs[NL80211_ATTR_IFNAME])
2954                 return -EINVAL;
2955
2956         if (info->attrs[NL80211_ATTR_IFTYPE]) {
2957                 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2958                 if (type > NL80211_IFTYPE_MAX)
2959                         return -EINVAL;
2960         }
2961
2962         if (!rdev->ops->add_virtual_intf ||
2963             !(rdev->wiphy.interface_modes & (1 << type)))
2964                 return -EOPNOTSUPP;
2965
2966         if ((type == NL80211_IFTYPE_P2P_DEVICE || type == NL80211_IFTYPE_NAN ||
2967              rdev->wiphy.features & NL80211_FEATURE_MAC_ON_CREATE) &&
2968             info->attrs[NL80211_ATTR_MAC]) {
2969                 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2970                            ETH_ALEN);
2971                 if (!is_valid_ether_addr(params.macaddr))
2972                         return -EADDRNOTAVAIL;
2973         }
2974
2975         if (info->attrs[NL80211_ATTR_4ADDR]) {
2976                 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2977                 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
2978                 if (err)
2979                         return err;
2980         }
2981
2982         err = nl80211_parse_mon_options(rdev, type, info, &params);
2983         if (err < 0)
2984                 return err;
2985
2986         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2987         if (!msg)
2988                 return -ENOMEM;
2989
2990         wdev = rdev_add_virtual_intf(rdev,
2991                                 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2992                                 NET_NAME_USER, type, &params);
2993         if (WARN_ON(!wdev)) {
2994                 nlmsg_free(msg);
2995                 return -EPROTO;
2996         } else if (IS_ERR(wdev)) {
2997                 nlmsg_free(msg);
2998                 return PTR_ERR(wdev);
2999         }
3000
3001         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
3002                 wdev->owner_nlportid = info->snd_portid;
3003
3004         switch (type) {
3005         case NL80211_IFTYPE_MESH_POINT:
3006                 if (!info->attrs[NL80211_ATTR_MESH_ID])
3007                         break;
3008                 wdev_lock(wdev);
3009                 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
3010                              IEEE80211_MAX_MESH_ID_LEN);
3011                 wdev->mesh_id_up_len =
3012                         nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
3013                 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
3014                        wdev->mesh_id_up_len);
3015                 wdev_unlock(wdev);
3016                 break;
3017         case NL80211_IFTYPE_NAN:
3018         case NL80211_IFTYPE_P2P_DEVICE:
3019                 /*
3020                  * P2P Device and NAN do not have a netdev, so don't go
3021                  * through the netdev notifier and must be added here
3022                  */
3023                 mutex_init(&wdev->mtx);
3024                 INIT_LIST_HEAD(&wdev->event_list);
3025                 spin_lock_init(&wdev->event_lock);
3026                 INIT_LIST_HEAD(&wdev->mgmt_registrations);
3027                 spin_lock_init(&wdev->mgmt_registrations_lock);
3028
3029                 wdev->identifier = ++rdev->wdev_id;
3030                 list_add_rcu(&wdev->list, &rdev->wiphy.wdev_list);
3031                 rdev->devlist_generation++;
3032                 break;
3033         default:
3034                 break;
3035         }
3036
3037         if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
3038                                rdev, wdev, false) < 0) {
3039                 nlmsg_free(msg);
3040                 return -ENOBUFS;
3041         }
3042
3043         /*
3044          * For wdevs which have no associated netdev object (e.g. of type
3045          * NL80211_IFTYPE_P2P_DEVICE), emit the NEW_INTERFACE event here.
3046          * For all other types, the event will be generated from the
3047          * netdev notifier
3048          */
3049         if (!wdev->netdev)
3050                 nl80211_notify_iface(rdev, wdev, NL80211_CMD_NEW_INTERFACE);
3051
3052         return genlmsg_reply(msg, info);
3053 }
3054
3055 static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
3056 {
3057         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3058         struct wireless_dev *wdev = info->user_ptr[1];
3059
3060         if (!rdev->ops->del_virtual_intf)
3061                 return -EOPNOTSUPP;
3062
3063         /*
3064          * If we remove a wireless device without a netdev then clear
3065          * user_ptr[1] so that nl80211_post_doit won't dereference it
3066          * to check if it needs to do dev_put(). Otherwise it crashes
3067          * since the wdev has been freed, unlike with a netdev where
3068          * we need the dev_put() for the netdev to really be freed.
3069          */
3070         if (!wdev->netdev)
3071                 info->user_ptr[1] = NULL;
3072
3073         return rdev_del_virtual_intf(rdev, wdev);
3074 }
3075
3076 static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
3077 {
3078         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3079         struct net_device *dev = info->user_ptr[1];
3080         u16 noack_map;
3081
3082         if (!info->attrs[NL80211_ATTR_NOACK_MAP])
3083                 return -EINVAL;
3084
3085         if (!rdev->ops->set_noack_map)
3086                 return -EOPNOTSUPP;
3087
3088         noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
3089
3090         return rdev_set_noack_map(rdev, dev, noack_map);
3091 }
3092
3093 struct get_key_cookie {
3094         struct sk_buff *msg;
3095         int error;
3096         int idx;
3097 };
3098
3099 static void get_key_callback(void *c, struct key_params *params)
3100 {
3101         struct nlattr *key;
3102         struct get_key_cookie *cookie = c;
3103
3104         if ((params->key &&
3105              nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
3106                      params->key_len, params->key)) ||
3107             (params->seq &&
3108              nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
3109                      params->seq_len, params->seq)) ||
3110             (params->cipher &&
3111              nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
3112                          params->cipher)))
3113                 goto nla_put_failure;
3114
3115         key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
3116         if (!key)
3117                 goto nla_put_failure;
3118
3119         if ((params->key &&
3120              nla_put(cookie->msg, NL80211_KEY_DATA,
3121                      params->key_len, params->key)) ||
3122             (params->seq &&
3123              nla_put(cookie->msg, NL80211_KEY_SEQ,
3124                      params->seq_len, params->seq)) ||
3125             (params->cipher &&
3126              nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
3127                          params->cipher)))
3128                 goto nla_put_failure;
3129
3130         if (nla_put_u8(cookie->msg, NL80211_KEY_IDX, cookie->idx))
3131                 goto nla_put_failure;
3132
3133         nla_nest_end(cookie->msg, key);
3134
3135         return;
3136  nla_put_failure:
3137         cookie->error = 1;
3138 }
3139
3140 static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
3141 {
3142         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3143         int err;
3144         struct net_device *dev = info->user_ptr[1];
3145         u8 key_idx = 0;
3146         const u8 *mac_addr = NULL;
3147         bool pairwise;
3148         struct get_key_cookie cookie = {
3149                 .error = 0,
3150         };
3151         void *hdr;
3152         struct sk_buff *msg;
3153
3154         if (info->attrs[NL80211_ATTR_KEY_IDX])
3155                 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
3156
3157         if (key_idx > 5)
3158                 return -EINVAL;
3159
3160         if (info->attrs[NL80211_ATTR_MAC])
3161                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3162
3163         pairwise = !!mac_addr;
3164         if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
3165                 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
3166
3167                 if (kt >= NUM_NL80211_KEYTYPES)
3168                         return -EINVAL;
3169                 if (kt != NL80211_KEYTYPE_GROUP &&
3170                     kt != NL80211_KEYTYPE_PAIRWISE)
3171                         return -EINVAL;
3172                 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
3173         }
3174
3175         if (!rdev->ops->get_key)
3176                 return -EOPNOTSUPP;
3177
3178         if (!pairwise && mac_addr && !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3179                 return -ENOENT;
3180
3181         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3182         if (!msg)
3183                 return -ENOMEM;
3184
3185         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
3186                              NL80211_CMD_NEW_KEY);
3187         if (!hdr)
3188                 goto nla_put_failure;
3189
3190         cookie.msg = msg;
3191         cookie.idx = key_idx;
3192
3193         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3194             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
3195                 goto nla_put_failure;
3196         if (mac_addr &&
3197             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
3198                 goto nla_put_failure;
3199
3200         err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
3201                            get_key_callback);
3202
3203         if (err)
3204                 goto free_msg;
3205
3206         if (cookie.error)
3207                 goto nla_put_failure;
3208
3209         genlmsg_end(msg, hdr);
3210         return genlmsg_reply(msg, info);
3211
3212  nla_put_failure:
3213         err = -ENOBUFS;
3214  free_msg:
3215         nlmsg_free(msg);
3216         return err;
3217 }
3218
3219 static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
3220 {
3221         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3222         struct key_parse key;
3223         int err;
3224         struct net_device *dev = info->user_ptr[1];
3225
3226         err = nl80211_parse_key(info, &key);
3227         if (err)
3228                 return err;
3229
3230         if (key.idx < 0)
3231                 return -EINVAL;
3232
3233         /* only support setting default key */
3234         if (!key.def && !key.defmgmt)
3235                 return -EINVAL;
3236
3237         wdev_lock(dev->ieee80211_ptr);
3238
3239         if (key.def) {
3240                 if (!rdev->ops->set_default_key) {
3241                         err = -EOPNOTSUPP;
3242                         goto out;
3243                 }
3244
3245                 err = nl80211_key_allowed(dev->ieee80211_ptr);
3246                 if (err)
3247                         goto out;
3248
3249                 err = rdev_set_default_key(rdev, dev, key.idx,
3250                                                  key.def_uni, key.def_multi);
3251
3252                 if (err)
3253                         goto out;
3254
3255 #ifdef CONFIG_CFG80211_WEXT
3256                 dev->ieee80211_ptr->wext.default_key = key.idx;
3257 #endif
3258         } else {
3259                 if (key.def_uni || !key.def_multi) {
3260                         err = -EINVAL;
3261                         goto out;
3262                 }
3263
3264                 if (!rdev->ops->set_default_mgmt_key) {
3265                         err = -EOPNOTSUPP;
3266                         goto out;
3267                 }
3268
3269                 err = nl80211_key_allowed(dev->ieee80211_ptr);
3270                 if (err)
3271                         goto out;
3272
3273                 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
3274                 if (err)
3275                         goto out;
3276
3277 #ifdef CONFIG_CFG80211_WEXT
3278                 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
3279 #endif
3280         }
3281
3282  out:
3283         wdev_unlock(dev->ieee80211_ptr);
3284
3285         return err;
3286 }
3287
3288 static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
3289 {
3290         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3291         int err;
3292         struct net_device *dev = info->user_ptr[1];
3293         struct key_parse key;
3294         const u8 *mac_addr = NULL;
3295
3296         err = nl80211_parse_key(info, &key);
3297         if (err)
3298                 return err;
3299
3300         if (!key.p.key)
3301                 return -EINVAL;
3302
3303         if (info->attrs[NL80211_ATTR_MAC])
3304                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3305
3306         if (key.type == -1) {
3307                 if (mac_addr)
3308                         key.type = NL80211_KEYTYPE_PAIRWISE;
3309                 else
3310                         key.type = NL80211_KEYTYPE_GROUP;
3311         }
3312
3313         /* for now */
3314         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3315             key.type != NL80211_KEYTYPE_GROUP)
3316                 return -EINVAL;
3317
3318         if (!rdev->ops->add_key)
3319                 return -EOPNOTSUPP;
3320
3321         if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
3322                                            key.type == NL80211_KEYTYPE_PAIRWISE,
3323                                            mac_addr))
3324                 return -EINVAL;
3325
3326         wdev_lock(dev->ieee80211_ptr);
3327         err = nl80211_key_allowed(dev->ieee80211_ptr);
3328         if (!err)
3329                 err = rdev_add_key(rdev, dev, key.idx,
3330                                    key.type == NL80211_KEYTYPE_PAIRWISE,
3331                                     mac_addr, &key.p);
3332         wdev_unlock(dev->ieee80211_ptr);
3333
3334         return err;
3335 }
3336
3337 static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
3338 {
3339         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3340         int err;
3341         struct net_device *dev = info->user_ptr[1];
3342         u8 *mac_addr = NULL;
3343         struct key_parse key;
3344
3345         err = nl80211_parse_key(info, &key);
3346         if (err)
3347                 return err;
3348
3349         if (key.idx < 0)
3350                 return -EINVAL;
3351
3352         if (info->attrs[NL80211_ATTR_MAC])
3353                 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3354
3355         if (key.type == -1) {
3356                 if (mac_addr)
3357                         key.type = NL80211_KEYTYPE_PAIRWISE;
3358                 else
3359                         key.type = NL80211_KEYTYPE_GROUP;
3360         }
3361
3362         /* for now */
3363         if (key.type != NL80211_KEYTYPE_PAIRWISE &&
3364             key.type != NL80211_KEYTYPE_GROUP)
3365                 return -EINVAL;
3366
3367         if (!rdev->ops->del_key)
3368                 return -EOPNOTSUPP;
3369
3370         wdev_lock(dev->ieee80211_ptr);
3371         err = nl80211_key_allowed(dev->ieee80211_ptr);
3372
3373         if (key.type == NL80211_KEYTYPE_GROUP && mac_addr &&
3374             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
3375                 err = -ENOENT;
3376
3377         if (!err)
3378                 err = rdev_del_key(rdev, dev, key.idx,
3379                                    key.type == NL80211_KEYTYPE_PAIRWISE,
3380                                    mac_addr);
3381
3382 #ifdef CONFIG_CFG80211_WEXT
3383         if (!err) {
3384                 if (key.idx == dev->ieee80211_ptr->wext.default_key)
3385                         dev->ieee80211_ptr->wext.default_key = -1;
3386                 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
3387                         dev->ieee80211_ptr->wext.default_mgmt_key = -1;
3388         }
3389 #endif
3390         wdev_unlock(dev->ieee80211_ptr);
3391
3392         return err;
3393 }
3394
3395 /* This function returns an error or the number of nested attributes */
3396 static int validate_acl_mac_addrs(struct nlattr *nl_attr)
3397 {
3398         struct nlattr *attr;
3399         int n_entries = 0, tmp;
3400
3401         nla_for_each_nested(attr, nl_attr, tmp) {
3402                 if (nla_len(attr) != ETH_ALEN)
3403                         return -EINVAL;
3404
3405                 n_entries++;
3406         }
3407
3408         return n_entries;
3409 }
3410
3411 /*
3412  * This function parses ACL information and allocates memory for ACL data.
3413  * On successful return, the calling function is responsible to free the
3414  * ACL buffer returned by this function.
3415  */
3416 static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
3417                                                 struct genl_info *info)
3418 {
3419         enum nl80211_acl_policy acl_policy;
3420         struct nlattr *attr;
3421         struct cfg80211_acl_data *acl;
3422         int i = 0, n_entries, tmp;
3423
3424         if (!wiphy->max_acl_mac_addrs)
3425                 return ERR_PTR(-EOPNOTSUPP);
3426
3427         if (!info->attrs[NL80211_ATTR_ACL_POLICY])
3428                 return ERR_PTR(-EINVAL);
3429
3430         acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
3431         if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
3432             acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
3433                 return ERR_PTR(-EINVAL);
3434
3435         if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
3436                 return ERR_PTR(-EINVAL);
3437
3438         n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
3439         if (n_entries < 0)
3440                 return ERR_PTR(n_entries);
3441
3442         if (n_entries > wiphy->max_acl_mac_addrs)
3443                 return ERR_PTR(-ENOTSUPP);
3444
3445         acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
3446                       GFP_KERNEL);
3447         if (!acl)
3448                 return ERR_PTR(-ENOMEM);
3449
3450         nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
3451                 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
3452                 i++;
3453         }
3454
3455         acl->n_acl_entries = n_entries;
3456         acl->acl_policy = acl_policy;
3457
3458         return acl;
3459 }
3460
3461 static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
3462 {
3463         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3464         struct net_device *dev = info->user_ptr[1];
3465         struct cfg80211_acl_data *acl;
3466         int err;
3467
3468         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3469             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3470                 return -EOPNOTSUPP;
3471
3472         if (!dev->ieee80211_ptr->beacon_interval)
3473                 return -EINVAL;
3474
3475         acl = parse_acl_data(&rdev->wiphy, info);
3476         if (IS_ERR(acl))
3477                 return PTR_ERR(acl);
3478
3479         err = rdev_set_mac_acl(rdev, dev, acl);
3480
3481         kfree(acl);
3482
3483         return err;
3484 }
3485
3486 static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
3487                            u8 *rates, u8 rates_len)
3488 {
3489         u8 i;
3490         u32 mask = 0;
3491
3492         for (i = 0; i < rates_len; i++) {
3493                 int rate = (rates[i] & 0x7f) * 5;
3494                 int ridx;
3495
3496                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
3497                         struct ieee80211_rate *srate =
3498                                 &sband->bitrates[ridx];
3499                         if (rate == srate->bitrate) {
3500                                 mask |= 1 << ridx;
3501                                 break;
3502                         }
3503                 }
3504                 if (ridx == sband->n_bitrates)
3505                         return 0; /* rate not found */
3506         }
3507
3508         return mask;
3509 }
3510
3511 static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
3512                                u8 *rates, u8 rates_len,
3513                                u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
3514 {
3515         u8 i;
3516
3517         memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
3518
3519         for (i = 0; i < rates_len; i++) {
3520                 int ridx, rbit;
3521
3522                 ridx = rates[i] / 8;
3523                 rbit = BIT(rates[i] % 8);
3524
3525                 /* check validity */
3526                 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
3527                         return false;
3528
3529                 /* check availability */
3530                 ridx = array_index_nospec(ridx, IEEE80211_HT_MCS_MASK_LEN);
3531                 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
3532                         mcs[ridx] |= rbit;
3533                 else
3534                         return false;
3535         }
3536
3537         return true;
3538 }
3539
3540 static u16 vht_mcs_map_to_mcs_mask(u8 vht_mcs_map)
3541 {
3542         u16 mcs_mask = 0;
3543
3544         switch (vht_mcs_map) {
3545         case IEEE80211_VHT_MCS_NOT_SUPPORTED:
3546                 break;
3547         case IEEE80211_VHT_MCS_SUPPORT_0_7:
3548                 mcs_mask = 0x00FF;
3549                 break;
3550         case IEEE80211_VHT_MCS_SUPPORT_0_8:
3551                 mcs_mask = 0x01FF;
3552                 break;
3553         case IEEE80211_VHT_MCS_SUPPORT_0_9:
3554                 mcs_mask = 0x03FF;
3555                 break;
3556         default:
3557                 break;
3558         }
3559
3560         return mcs_mask;
3561 }
3562
3563 static void vht_build_mcs_mask(u16 vht_mcs_map,
3564                                u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
3565 {
3566         u8 nss;
3567
3568         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
3569                 vht_mcs_mask[nss] = vht_mcs_map_to_mcs_mask(vht_mcs_map & 0x03);
3570                 vht_mcs_map >>= 2;
3571         }
3572 }
3573
3574 static bool vht_set_mcs_mask(struct ieee80211_supported_band *sband,
3575                              struct nl80211_txrate_vht *txrate,
3576                              u16 mcs[NL80211_VHT_NSS_MAX])
3577 {
3578         u16 tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3579         u16 tx_mcs_mask[NL80211_VHT_NSS_MAX] = {};
3580         u8 i;
3581
3582         if (!sband->vht_cap.vht_supported)
3583                 return false;
3584
3585         memset(mcs, 0, sizeof(u16) * NL80211_VHT_NSS_MAX);
3586
3587         /* Build vht_mcs_mask from VHT capabilities */
3588         vht_build_mcs_mask(tx_mcs_map, tx_mcs_mask);
3589
3590         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3591                 if ((tx_mcs_mask[i] & txrate->mcs[i]) == txrate->mcs[i])
3592                         mcs[i] = txrate->mcs[i];
3593                 else
3594                         return false;
3595         }
3596
3597         return true;
3598 }
3599
3600 static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
3601         [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
3602                                     .len = NL80211_MAX_SUPP_RATES },
3603         [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
3604                                 .len = NL80211_MAX_SUPP_HT_RATES },
3605         [NL80211_TXRATE_VHT] = { .len = sizeof(struct nl80211_txrate_vht)},
3606         [NL80211_TXRATE_GI] = { .type = NLA_U8 },
3607 };
3608
3609 static int nl80211_parse_tx_bitrate_mask(struct genl_info *info,
3610                                          struct cfg80211_bitrate_mask *mask)
3611 {
3612         struct nlattr *tb[NL80211_TXRATE_MAX + 1];
3613         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3614         int rem, i;
3615         struct nlattr *tx_rates;
3616         struct ieee80211_supported_band *sband;
3617         u16 vht_tx_mcs_map;
3618
3619         memset(mask, 0, sizeof(*mask));
3620         /* Default to all rates enabled */
3621         for (i = 0; i < NUM_NL80211_BANDS; i++) {
3622                 sband = rdev->wiphy.bands[i];
3623
3624                 if (!sband)
3625                         continue;
3626
3627                 mask->control[i].legacy = (1 << sband->n_bitrates) - 1;
3628                 memcpy(mask->control[i].ht_mcs,
3629                        sband->ht_cap.mcs.rx_mask,
3630                        sizeof(mask->control[i].ht_mcs));
3631
3632                 if (!sband->vht_cap.vht_supported)
3633                         continue;
3634
3635                 vht_tx_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
3636                 vht_build_mcs_mask(vht_tx_mcs_map, mask->control[i].vht_mcs);
3637         }
3638
3639         /* if no rates are given set it back to the defaults */
3640         if (!info->attrs[NL80211_ATTR_TX_RATES])
3641                 goto out;
3642
3643         /* The nested attribute uses enum nl80211_band as the index. This maps
3644          * directly to the enum nl80211_band values used in cfg80211.
3645          */
3646         BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
3647         nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem) {
3648                 enum nl80211_band band = nla_type(tx_rates);
3649                 int err;
3650
3651                 if (band < 0 || band >= NUM_NL80211_BANDS)
3652                         return -EINVAL;
3653                 sband = rdev->wiphy.bands[band];
3654                 if (sband == NULL)
3655                         return -EINVAL;
3656                 err = nla_parse_nested(tb, NL80211_TXRATE_MAX, tx_rates,
3657                                        nl80211_txattr_policy, info->extack);
3658                 if (err)
3659                         return err;
3660                 if (tb[NL80211_TXRATE_LEGACY]) {
3661                         mask->control[band].legacy = rateset_to_mask(
3662                                 sband,
3663                                 nla_data(tb[NL80211_TXRATE_LEGACY]),
3664                                 nla_len(tb[NL80211_TXRATE_LEGACY]));
3665                         if ((mask->control[band].legacy == 0) &&
3666                             nla_len(tb[NL80211_TXRATE_LEGACY]))
3667                                 return -EINVAL;
3668                 }
3669                 if (tb[NL80211_TXRATE_HT]) {
3670                         if (!ht_rateset_to_mask(
3671                                         sband,
3672                                         nla_data(tb[NL80211_TXRATE_HT]),
3673                                         nla_len(tb[NL80211_TXRATE_HT]),
3674                                         mask->control[band].ht_mcs))
3675                                 return -EINVAL;
3676                 }
3677                 if (tb[NL80211_TXRATE_VHT]) {
3678                         if (!vht_set_mcs_mask(
3679                                         sband,
3680                                         nla_data(tb[NL80211_TXRATE_VHT]),
3681                                         mask->control[band].vht_mcs))
3682                                 return -EINVAL;
3683                 }
3684                 if (tb[NL80211_TXRATE_GI]) {
3685                         mask->control[band].gi =
3686                                 nla_get_u8(tb[NL80211_TXRATE_GI]);
3687                         if (mask->control[band].gi > NL80211_TXRATE_FORCE_LGI)
3688                                 return -EINVAL;
3689                 }
3690
3691                 if (mask->control[band].legacy == 0) {
3692                         /* don't allow empty legacy rates if HT or VHT
3693                          * are not even supported.
3694                          */
3695                         if (!(rdev->wiphy.bands[band]->ht_cap.ht_supported ||
3696                               rdev->wiphy.bands[band]->vht_cap.vht_supported))
3697                                 return -EINVAL;
3698
3699                         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3700                                 if (mask->control[band].ht_mcs[i])
3701                                         goto out;
3702
3703                         for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3704                                 if (mask->control[band].vht_mcs[i])
3705                                         goto out;
3706
3707                         /* legacy and mcs rates may not be both empty */
3708                         return -EINVAL;
3709                 }
3710         }
3711
3712 out:
3713         return 0;
3714 }
3715
3716 static int validate_beacon_tx_rate(struct cfg80211_registered_device *rdev,
3717                                    enum nl80211_band band,
3718                                    struct cfg80211_bitrate_mask *beacon_rate)
3719 {
3720         u32 count_ht, count_vht, i;
3721         u32 rate = beacon_rate->control[band].legacy;
3722
3723         /* Allow only one rate */
3724         if (hweight32(rate) > 1)
3725                 return -EINVAL;
3726
3727         count_ht = 0;
3728         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3729                 if (hweight8(beacon_rate->control[band].ht_mcs[i]) > 1) {
3730                         return -EINVAL;
3731                 } else if (beacon_rate->control[band].ht_mcs[i]) {
3732                         count_ht++;
3733                         if (count_ht > 1)
3734                                 return -EINVAL;
3735                 }
3736                 if (count_ht && rate)
3737                         return -EINVAL;
3738         }
3739
3740         count_vht = 0;
3741         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3742                 if (hweight16(beacon_rate->control[band].vht_mcs[i]) > 1) {
3743                         return -EINVAL;
3744                 } else if (beacon_rate->control[band].vht_mcs[i]) {
3745                         count_vht++;
3746                         if (count_vht > 1)
3747                                 return -EINVAL;
3748                 }
3749                 if (count_vht && rate)
3750                         return -EINVAL;
3751         }
3752
3753         if ((count_ht && count_vht) || (!rate && !count_ht && !count_vht))
3754                 return -EINVAL;
3755
3756         if (rate &&
3757             !wiphy_ext_feature_isset(&rdev->wiphy,
3758                                      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY))
3759                 return -EINVAL;
3760         if (count_ht &&
3761             !wiphy_ext_feature_isset(&rdev->wiphy,
3762                                      NL80211_EXT_FEATURE_BEACON_RATE_HT))
3763                 return -EINVAL;
3764         if (count_vht &&
3765             !wiphy_ext_feature_isset(&rdev->wiphy,
3766                                      NL80211_EXT_FEATURE_BEACON_RATE_VHT))
3767                 return -EINVAL;
3768
3769         return 0;
3770 }
3771
3772 static int nl80211_parse_beacon(struct nlattr *attrs[],
3773                                 struct cfg80211_beacon_data *bcn)
3774 {
3775         bool haveinfo = false;
3776
3777         if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
3778             !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
3779             !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
3780             !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
3781                 return -EINVAL;
3782
3783         memset(bcn, 0, sizeof(*bcn));
3784
3785         if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3786                 int ret = validate_beacon_head(attrs[NL80211_ATTR_BEACON_HEAD],
3787                                                NULL);
3788
3789                 if (ret)
3790                         return ret;
3791
3792                 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3793                 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
3794                 if (!bcn->head_len)
3795                         return -EINVAL;
3796                 haveinfo = true;
3797         }
3798
3799         if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3800                 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3801                 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
3802                 haveinfo = true;
3803         }
3804
3805         if (!haveinfo)
3806                 return -EINVAL;
3807
3808         if (attrs[NL80211_ATTR_IE]) {
3809                 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3810                 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
3811         }
3812
3813         if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
3814                 bcn->proberesp_ies =
3815                         nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3816                 bcn->proberesp_ies_len =
3817                         nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
3818         }
3819
3820         if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
3821                 bcn->assocresp_ies =
3822                         nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3823                 bcn->assocresp_ies_len =
3824                         nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
3825         }
3826
3827         if (attrs[NL80211_ATTR_PROBE_RESP]) {
3828                 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3829                 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
3830         }
3831
3832         return 0;
3833 }
3834
3835 static void nl80211_check_ap_rate_selectors(struct cfg80211_ap_settings *params,
3836                                             const u8 *rates)
3837 {
3838         int i;
3839
3840         if (!rates)
3841                 return;
3842
3843         for (i = 0; i < rates[1]; i++) {
3844                 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
3845                         params->ht_required = true;
3846                 if (rates[2 + i] == BSS_MEMBERSHIP_SELECTOR_VHT_PHY)
3847                         params->vht_required = true;
3848         }
3849 }
3850
3851 /*
3852  * Since the nl80211 API didn't include, from the beginning, attributes about
3853  * HT/VHT requirements/capabilities, we parse them out of the IEs for the
3854  * benefit of drivers that rebuild IEs in the firmware.
3855  */
3856 static void nl80211_calculate_ap_params(struct cfg80211_ap_settings *params)
3857 {
3858         const struct cfg80211_beacon_data *bcn = &params->beacon;
3859         size_t ies_len = bcn->tail_len;
3860         const u8 *ies = bcn->tail;
3861         const u8 *rates;
3862         const u8 *cap;
3863
3864         rates = cfg80211_find_ie(WLAN_EID_SUPP_RATES, ies, ies_len);
3865         nl80211_check_ap_rate_selectors(params, rates);
3866
3867         rates = cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES, ies, ies_len);
3868         nl80211_check_ap_rate_selectors(params, rates);
3869
3870         cap = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, ies, ies_len);
3871         if (cap && cap[1] >= sizeof(*params->ht_cap))
3872                 params->ht_cap = (void *)(cap + 2);
3873         cap = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, ies, ies_len);
3874         if (cap && cap[1] >= sizeof(*params->vht_cap))
3875                 params->vht_cap = (void *)(cap + 2);
3876 }
3877
3878 static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3879                                    struct cfg80211_ap_settings *params)
3880 {
3881         struct wireless_dev *wdev;
3882         bool ret = false;
3883
3884         list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
3885                 if (wdev->iftype != NL80211_IFTYPE_AP &&
3886                     wdev->iftype != NL80211_IFTYPE_P2P_GO)
3887                         continue;
3888
3889                 if (!wdev->preset_chandef.chan)
3890                         continue;
3891
3892                 params->chandef = wdev->preset_chandef;
3893                 ret = true;
3894                 break;
3895         }
3896
3897         return ret;
3898 }
3899
3900 static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3901                                     enum nl80211_auth_type auth_type,
3902                                     enum nl80211_commands cmd)
3903 {
3904         if (auth_type > NL80211_AUTHTYPE_MAX)
3905                 return false;
3906
3907         switch (cmd) {
3908         case NL80211_CMD_AUTHENTICATE:
3909                 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3910                     auth_type == NL80211_AUTHTYPE_SAE)
3911                         return false;
3912                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
3913                                              NL80211_EXT_FEATURE_FILS_STA) &&
3914                     (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3915                      auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3916                      auth_type == NL80211_AUTHTYPE_FILS_PK))
3917                         return false;
3918                 return true;
3919         case NL80211_CMD_CONNECT:
3920                 /* SAE not supported yet */
3921                 if (auth_type == NL80211_AUTHTYPE_SAE)
3922                         return false;
3923                 /* FILS with SK PFS or PK not supported yet */
3924                 if (auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3925                     auth_type == NL80211_AUTHTYPE_FILS_PK)
3926                         return false;
3927                 if (!wiphy_ext_feature_isset(
3928                             &rdev->wiphy,
3929                             NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
3930                     auth_type == NL80211_AUTHTYPE_FILS_SK)
3931                         return false;
3932                 return true;
3933         case NL80211_CMD_START_AP:
3934                 /* SAE not supported yet */
3935                 if (auth_type == NL80211_AUTHTYPE_SAE)
3936                         return false;
3937                 /* FILS not supported yet */
3938                 if (auth_type == NL80211_AUTHTYPE_FILS_SK ||
3939                     auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
3940                     auth_type == NL80211_AUTHTYPE_FILS_PK)
3941                         return false;
3942                 return true;
3943         default:
3944                 return false;
3945         }
3946 }
3947
3948 static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3949 {
3950         struct cfg80211_registered_device *rdev = info->user_ptr[0];
3951         struct net_device *dev = info->user_ptr[1];
3952         struct wireless_dev *wdev = dev->ieee80211_ptr;
3953         struct cfg80211_ap_settings params;
3954         int err;
3955
3956         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3957             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3958                 return -EOPNOTSUPP;
3959
3960         if (!rdev->ops->start_ap)
3961                 return -EOPNOTSUPP;
3962
3963         if (wdev->beacon_interval)
3964                 return -EALREADY;
3965
3966         memset(&params, 0, sizeof(params));
3967
3968         /* these are required for START_AP */
3969         if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3970             !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3971             !info->attrs[NL80211_ATTR_BEACON_HEAD])
3972                 return -EINVAL;
3973
3974         err = nl80211_parse_beacon(info->attrs, &params.beacon);
3975         if (err)
3976                 return err;
3977
3978         params.beacon_interval =
3979                 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3980         params.dtim_period =
3981                 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3982
3983         err = cfg80211_validate_beacon_int(rdev, dev->ieee80211_ptr->iftype,
3984                                            params.beacon_interval);
3985         if (err)
3986                 return err;
3987
3988         /*
3989          * In theory, some of these attributes should be required here
3990          * but since they were not used when the command was originally
3991          * added, keep them optional for old user space programs to let
3992          * them continue to work with drivers that do not need the
3993          * additional information -- drivers must check!
3994          */
3995         if (info->attrs[NL80211_ATTR_SSID]) {
3996                 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3997                 params.ssid_len =
3998                         nla_len(info->attrs[NL80211_ATTR_SSID]);
3999                 if (params.ssid_len == 0 ||
4000                     params.ssid_len > IEEE80211_MAX_SSID_LEN)
4001                         return -EINVAL;
4002         }
4003
4004         if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
4005                 params.hidden_ssid = nla_get_u32(
4006                         info->attrs[NL80211_ATTR_HIDDEN_SSID]);
4007                 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
4008                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
4009                     params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
4010                         return -EINVAL;
4011         }
4012
4013         params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
4014
4015         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4016                 params.auth_type = nla_get_u32(
4017                         info->attrs[NL80211_ATTR_AUTH_TYPE]);
4018                 if (!nl80211_valid_auth_type(rdev, params.auth_type,
4019                                              NL80211_CMD_START_AP))
4020                         return -EINVAL;
4021         } else
4022                 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4023
4024         err = nl80211_crypto_settings(rdev, info, &params.crypto,
4025                                       NL80211_MAX_NR_CIPHER_SUITES);
4026         if (err)
4027                 return err;
4028
4029         if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
4030                 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
4031                         return -EOPNOTSUPP;
4032                 params.inactivity_timeout = nla_get_u16(
4033                         info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
4034         }
4035
4036         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4037                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4038                         return -EINVAL;
4039                 params.p2p_ctwindow =
4040                         nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4041                 if (params.p2p_ctwindow > 127)
4042                         return -EINVAL;
4043                 if (params.p2p_ctwindow != 0 &&
4044                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4045                         return -EINVAL;
4046         }
4047
4048         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4049                 u8 tmp;
4050
4051                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4052                         return -EINVAL;
4053                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4054                 if (tmp > 1)
4055                         return -EINVAL;
4056                 params.p2p_opp_ps = tmp;
4057                 if (params.p2p_opp_ps != 0 &&
4058                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4059                         return -EINVAL;
4060         }
4061
4062         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4063                 err = nl80211_parse_chandef(rdev, info, &params.chandef);
4064                 if (err)
4065                         return err;
4066         } else if (wdev->preset_chandef.chan) {
4067                 params.chandef = wdev->preset_chandef;
4068         } else if (!nl80211_get_ap_channel(rdev, &params))
4069                 return -EINVAL;
4070
4071         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
4072                                            wdev->iftype))
4073                 return -EINVAL;
4074
4075         if (info->attrs[NL80211_ATTR_TX_RATES]) {
4076                 err = nl80211_parse_tx_bitrate_mask(info, &params.beacon_rate);
4077                 if (err)
4078                         return err;
4079
4080                 err = validate_beacon_tx_rate(rdev, params.chandef.chan->band,
4081                                               &params.beacon_rate);
4082                 if (err)
4083                         return err;
4084         }
4085
4086         if (info->attrs[NL80211_ATTR_SMPS_MODE]) {
4087                 params.smps_mode =
4088                         nla_get_u8(info->attrs[NL80211_ATTR_SMPS_MODE]);
4089                 switch (params.smps_mode) {
4090                 case NL80211_SMPS_OFF:
4091                         break;
4092                 case NL80211_SMPS_STATIC:
4093                         if (!(rdev->wiphy.features &
4094                               NL80211_FEATURE_STATIC_SMPS))
4095                                 return -EINVAL;
4096                         break;
4097                 case NL80211_SMPS_DYNAMIC:
4098                         if (!(rdev->wiphy.features &
4099                               NL80211_FEATURE_DYNAMIC_SMPS))
4100                                 return -EINVAL;
4101                         break;
4102                 default:
4103                         return -EINVAL;
4104                 }
4105         } else {
4106                 params.smps_mode = NL80211_SMPS_OFF;
4107         }
4108
4109         params.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
4110         if (params.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ])
4111                 return -EOPNOTSUPP;
4112
4113         if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
4114                 params.acl = parse_acl_data(&rdev->wiphy, info);
4115                 if (IS_ERR(params.acl))
4116                         return PTR_ERR(params.acl);
4117         }
4118
4119         nl80211_calculate_ap_params(&params);
4120
4121         wdev_lock(wdev);
4122         err = rdev_start_ap(rdev, dev, &params);
4123         if (!err) {
4124                 wdev->preset_chandef = params.chandef;
4125                 wdev->beacon_interval = params.beacon_interval;
4126                 wdev->chandef = params.chandef;
4127                 wdev->ssid_len = params.ssid_len;
4128                 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
4129         }
4130         wdev_unlock(wdev);
4131
4132         kfree(params.acl);
4133
4134         return err;
4135 }
4136
4137 static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
4138 {
4139         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4140         struct net_device *dev = info->user_ptr[1];
4141         struct wireless_dev *wdev = dev->ieee80211_ptr;
4142         struct cfg80211_beacon_data params;
4143         int err;
4144
4145         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4146             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4147                 return -EOPNOTSUPP;
4148
4149         if (!rdev->ops->change_beacon)
4150                 return -EOPNOTSUPP;
4151
4152         if (!wdev->beacon_interval)
4153                 return -EINVAL;
4154
4155         err = nl80211_parse_beacon(info->attrs, &params);
4156         if (err)
4157                 return err;
4158
4159         wdev_lock(wdev);
4160         err = rdev_change_beacon(rdev, dev, &params);
4161         wdev_unlock(wdev);
4162
4163         return err;
4164 }
4165
4166 static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
4167 {
4168         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4169         struct net_device *dev = info->user_ptr[1];
4170
4171         return cfg80211_stop_ap(rdev, dev, false);
4172 }
4173
4174 static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
4175         [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
4176         [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
4177         [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
4178         [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
4179         [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
4180         [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
4181 };
4182
4183 static int parse_station_flags(struct genl_info *info,
4184                                enum nl80211_iftype iftype,
4185                                struct station_parameters *params)
4186 {
4187         struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
4188         struct nlattr *nla;
4189         int flag;
4190
4191         /*
4192          * Try parsing the new attribute first so userspace
4193          * can specify both for older kernels.
4194          */
4195         nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
4196         if (nla) {
4197                 struct nl80211_sta_flag_update *sta_flags;
4198
4199                 sta_flags = nla_data(nla);
4200                 params->sta_flags_mask = sta_flags->mask;
4201                 params->sta_flags_set = sta_flags->set;
4202                 params->sta_flags_set &= params->sta_flags_mask;
4203                 if ((params->sta_flags_mask |
4204                      params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
4205                         return -EINVAL;
4206                 return 0;
4207         }
4208
4209         /* if present, parse the old attribute */
4210
4211         nla = info->attrs[NL80211_ATTR_STA_FLAGS];
4212         if (!nla)
4213                 return 0;
4214
4215         if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX, nla,
4216                              sta_flags_policy, info->extack))
4217                 return -EINVAL;
4218
4219         /*
4220          * Only allow certain flags for interface types so that
4221          * other attributes are silently ignored. Remember that
4222          * this is backward compatibility code with old userspace
4223          * and shouldn't be hit in other cases anyway.
4224          */
4225         switch (iftype) {
4226         case NL80211_IFTYPE_AP:
4227         case NL80211_IFTYPE_AP_VLAN:
4228         case NL80211_IFTYPE_P2P_GO:
4229                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4230                                          BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4231                                          BIT(NL80211_STA_FLAG_WME) |
4232                                          BIT(NL80211_STA_FLAG_MFP);
4233                 break;
4234         case NL80211_IFTYPE_P2P_CLIENT:
4235         case NL80211_IFTYPE_STATION:
4236                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
4237                                          BIT(NL80211_STA_FLAG_TDLS_PEER);
4238                 break;
4239         case NL80211_IFTYPE_MESH_POINT:
4240                 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4241                                          BIT(NL80211_STA_FLAG_MFP) |
4242                                          BIT(NL80211_STA_FLAG_AUTHORIZED);
4243                 break;
4244         default:
4245                 return -EINVAL;
4246         }
4247
4248         for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
4249                 if (flags[flag]) {
4250                         params->sta_flags_set |= (1<<flag);
4251
4252                         /* no longer support new API additions in old API */
4253                         if (flag > NL80211_STA_FLAG_MAX_OLD_API)
4254                                 return -EINVAL;
4255                 }
4256         }
4257
4258         return 0;
4259 }
4260
4261 static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
4262                                  int attr)
4263 {
4264         struct nlattr *rate;
4265         u32 bitrate;
4266         u16 bitrate_compat;
4267         enum nl80211_rate_info rate_flg;
4268
4269         rate = nla_nest_start(msg, attr);
4270         if (!rate)
4271                 return false;
4272
4273         /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
4274         bitrate = cfg80211_calculate_bitrate(info);
4275         /* report 16-bit bitrate only if we can */
4276         bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
4277         if (bitrate > 0 &&
4278             nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
4279                 return false;
4280         if (bitrate_compat > 0 &&
4281             nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
4282                 return false;
4283
4284         switch (info->bw) {
4285         case RATE_INFO_BW_5:
4286                 rate_flg = NL80211_RATE_INFO_5_MHZ_WIDTH;
4287                 break;
4288         case RATE_INFO_BW_10:
4289                 rate_flg = NL80211_RATE_INFO_10_MHZ_WIDTH;
4290                 break;
4291         default:
4292                 WARN_ON(1);
4293                 /* fall through */
4294         case RATE_INFO_BW_20:
4295                 rate_flg = 0;
4296                 break;
4297         case RATE_INFO_BW_40:
4298                 rate_flg = NL80211_RATE_INFO_40_MHZ_WIDTH;
4299                 break;
4300         case RATE_INFO_BW_80:
4301                 rate_flg = NL80211_RATE_INFO_80_MHZ_WIDTH;
4302                 break;
4303         case RATE_INFO_BW_160:
4304                 rate_flg = NL80211_RATE_INFO_160_MHZ_WIDTH;
4305                 break;
4306         }
4307
4308         if (rate_flg && nla_put_flag(msg, rate_flg))
4309                 return false;
4310
4311         if (info->flags & RATE_INFO_FLAGS_MCS) {
4312                 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
4313                         return false;
4314                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4315                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4316                         return false;
4317         } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
4318                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
4319                         return false;
4320                 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
4321                         return false;
4322                 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
4323                     nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
4324                         return false;
4325         }
4326
4327         nla_nest_end(msg, rate);
4328         return true;
4329 }
4330
4331 static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
4332                                int id)
4333 {
4334         void *attr;
4335         int i = 0;
4336
4337         if (!mask)
4338                 return true;
4339
4340         attr = nla_nest_start(msg, id);
4341         if (!attr)
4342                 return false;
4343
4344         for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
4345                 if (!(mask & BIT(i)))
4346                         continue;
4347
4348                 if (nla_put_u8(msg, i, signal[i]))
4349                         return false;
4350         }
4351
4352         nla_nest_end(msg, attr);
4353
4354         return true;
4355 }
4356
4357 static int nl80211_send_station(struct sk_buff *msg, u32 cmd, u32 portid,
4358                                 u32 seq, int flags,
4359                                 struct cfg80211_registered_device *rdev,
4360                                 struct net_device *dev,
4361                                 const u8 *mac_addr, struct station_info *sinfo)
4362 {
4363         void *hdr;
4364         struct nlattr *sinfoattr, *bss_param;
4365
4366         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
4367         if (!hdr)
4368                 return -1;
4369
4370         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4371             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
4372             nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
4373                 goto nla_put_failure;
4374
4375         sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
4376         if (!sinfoattr)
4377                 goto nla_put_failure;
4378
4379 #define PUT_SINFO(attr, memb, type) do {                                \
4380         BUILD_BUG_ON(sizeof(type) == sizeof(u64));                      \
4381         if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&      \
4382             nla_put_ ## type(msg, NL80211_STA_INFO_ ## attr,            \
4383                              sinfo->memb))                              \
4384                 goto nla_put_failure;                                   \
4385         } while (0)
4386 #define PUT_SINFO_U64(attr, memb) do {                                  \
4387         if (sinfo->filled & (1ULL << NL80211_STA_INFO_ ## attr) &&      \
4388             nla_put_u64_64bit(msg, NL80211_STA_INFO_ ## attr,           \
4389                               sinfo->memb, NL80211_STA_INFO_PAD))       \
4390                 goto nla_put_failure;                                   \
4391         } while (0)
4392
4393         PUT_SINFO(CONNECTED_TIME, connected_time, u32);
4394         PUT_SINFO(INACTIVE_TIME, inactive_time, u32);
4395
4396         if (sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES) |
4397                              BIT(NL80211_STA_INFO_RX_BYTES64)) &&
4398             nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
4399                         (u32)sinfo->rx_bytes))
4400                 goto nla_put_failure;
4401
4402         if (sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES) |
4403                              BIT(NL80211_STA_INFO_TX_BYTES64)) &&
4404             nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
4405                         (u32)sinfo->tx_bytes))
4406                 goto nla_put_failure;
4407
4408         PUT_SINFO_U64(RX_BYTES64, rx_bytes);
4409         PUT_SINFO_U64(TX_BYTES64, tx_bytes);
4410         PUT_SINFO(LLID, llid, u16);
4411         PUT_SINFO(PLID, plid, u16);
4412         PUT_SINFO(PLINK_STATE, plink_state, u8);
4413         PUT_SINFO_U64(RX_DURATION, rx_duration);
4414
4415         switch (rdev->wiphy.signal_type) {
4416         case CFG80211_SIGNAL_TYPE_MBM:
4417                 PUT_SINFO(SIGNAL, signal, u8);
4418                 PUT_SINFO(SIGNAL_AVG, signal_avg, u8);
4419                 break;
4420         default:
4421                 break;
4422         }
4423         if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL)) {
4424                 if (!nl80211_put_signal(msg, sinfo->chains,
4425                                         sinfo->chain_signal,
4426                                         NL80211_STA_INFO_CHAIN_SIGNAL))
4427                         goto nla_put_failure;
4428         }
4429         if (sinfo->filled & BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)) {
4430                 if (!nl80211_put_signal(msg, sinfo->chains,
4431                                         sinfo->chain_signal_avg,
4432                                         NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
4433                         goto nla_put_failure;
4434         }
4435         if (sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE)) {
4436                 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
4437                                           NL80211_STA_INFO_TX_BITRATE))
4438                         goto nla_put_failure;
4439         }
4440         if (sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE)) {
4441                 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
4442                                           NL80211_STA_INFO_RX_BITRATE))
4443                         goto nla_put_failure;
4444         }
4445
4446         PUT_SINFO(RX_PACKETS, rx_packets, u32);
4447         PUT_SINFO(TX_PACKETS, tx_packets, u32);
4448         PUT_SINFO(TX_RETRIES, tx_retries, u32);
4449         PUT_SINFO(TX_FAILED, tx_failed, u32);
4450         PUT_SINFO(EXPECTED_THROUGHPUT, expected_throughput, u32);
4451         PUT_SINFO(BEACON_LOSS, beacon_loss_count, u32);
4452         PUT_SINFO(LOCAL_PM, local_pm, u32);
4453         PUT_SINFO(PEER_PM, peer_pm, u32);
4454         PUT_SINFO(NONPEER_PM, nonpeer_pm, u32);
4455
4456         if (sinfo->filled & BIT(NL80211_STA_INFO_BSS_PARAM)) {
4457                 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
4458                 if (!bss_param)
4459                         goto nla_put_failure;
4460
4461                 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
4462                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
4463                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
4464                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
4465                     ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
4466                      nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
4467                     nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
4468                                sinfo->bss_param.dtim_period) ||
4469                     nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
4470                                 sinfo->bss_param.beacon_interval))
4471                         goto nla_put_failure;
4472
4473                 nla_nest_end(msg, bss_param);
4474         }
4475         if ((sinfo->filled & BIT(NL80211_STA_INFO_STA_FLAGS)) &&
4476             nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
4477                     sizeof(struct nl80211_sta_flag_update),
4478                     &sinfo->sta_flags))
4479                 goto nla_put_failure;
4480
4481         PUT_SINFO_U64(T_OFFSET, t_offset);
4482         PUT_SINFO_U64(RX_DROP_MISC, rx_dropped_misc);
4483         PUT_SINFO_U64(BEACON_RX, rx_beacon);
4484         PUT_SINFO(BEACON_SIGNAL_AVG, rx_beacon_signal_avg, u8);
4485
4486 #undef PUT_SINFO
4487 #undef PUT_SINFO_U64
4488
4489         if (sinfo->filled & BIT(NL80211_STA_INFO_TID_STATS)) {
4490                 struct nlattr *tidsattr;
4491                 int tid;
4492
4493                 tidsattr = nla_nest_start(msg, NL80211_STA_INFO_TID_STATS);
4494                 if (!tidsattr)
4495                         goto nla_put_failure;
4496
4497                 for (tid = 0; tid < IEEE80211_NUM_TIDS + 1; tid++) {
4498                         struct cfg80211_tid_stats *tidstats;
4499                         struct nlattr *tidattr;
4500
4501                         tidstats = &sinfo->pertid[tid];
4502
4503                         if (!tidstats->filled)
4504                                 continue;
4505
4506                         tidattr = nla_nest_start(msg, tid + 1);
4507                         if (!tidattr)
4508                                 goto nla_put_failure;
4509
4510 #define PUT_TIDVAL_U64(attr, memb) do {                                 \
4511         if (tidstats->filled & BIT(NL80211_TID_STATS_ ## attr) &&       \
4512             nla_put_u64_64bit(msg, NL80211_TID_STATS_ ## attr,          \
4513                               tidstats->memb, NL80211_TID_STATS_PAD))   \
4514                 goto nla_put_failure;                                   \
4515         } while (0)
4516
4517                         PUT_TIDVAL_U64(RX_MSDU, rx_msdu);
4518                         PUT_TIDVAL_U64(TX_MSDU, tx_msdu);
4519                         PUT_TIDVAL_U64(TX_MSDU_RETRIES, tx_msdu_retries);
4520                         PUT_TIDVAL_U64(TX_MSDU_FAILED, tx_msdu_failed);
4521
4522 #undef PUT_TIDVAL_U64
4523                         nla_nest_end(msg, tidattr);
4524                 }
4525
4526                 nla_nest_end(msg, tidsattr);
4527         }
4528
4529         nla_nest_end(msg, sinfoattr);
4530
4531         if (sinfo->assoc_req_ies_len &&
4532             nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
4533                     sinfo->assoc_req_ies))
4534                 goto nla_put_failure;
4535
4536         genlmsg_end(msg, hdr);
4537         return 0;
4538
4539  nla_put_failure:
4540         genlmsg_cancel(msg, hdr);
4541         return -EMSGSIZE;
4542 }
4543
4544 static int nl80211_dump_station(struct sk_buff *skb,
4545                                 struct netlink_callback *cb)
4546 {
4547         struct station_info sinfo;
4548         struct cfg80211_registered_device *rdev;
4549         struct wireless_dev *wdev;
4550         u8 mac_addr[ETH_ALEN];
4551         int sta_idx = cb->args[2];
4552         int err;
4553
4554         rtnl_lock();
4555         err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
4556         if (err)
4557                 goto out_err;
4558
4559         if (!wdev->netdev) {
4560                 err = -EINVAL;
4561                 goto out_err;
4562         }
4563
4564         if (!rdev->ops->dump_station) {
4565                 err = -EOPNOTSUPP;
4566                 goto out_err;
4567         }
4568
4569         while (1) {
4570                 memset(&sinfo, 0, sizeof(sinfo));
4571                 err = rdev_dump_station(rdev, wdev->netdev, sta_idx,
4572                                         mac_addr, &sinfo);
4573                 if (err == -ENOENT)
4574                         break;
4575                 if (err)
4576                         goto out_err;
4577
4578                 if (nl80211_send_station(skb, NL80211_CMD_NEW_STATION,
4579                                 NETLINK_CB(cb->skb).portid,
4580                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4581                                 rdev, wdev->netdev, mac_addr,
4582                                 &sinfo) < 0)
4583                         goto out;
4584
4585                 sta_idx++;
4586         }
4587
4588  out:
4589         cb->args[2] = sta_idx;
4590         err = skb->len;
4591  out_err:
4592         rtnl_unlock();
4593
4594         return err;
4595 }
4596
4597 static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
4598 {
4599         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4600         struct net_device *dev = info->user_ptr[1];
4601         struct station_info sinfo;
4602         struct sk_buff *msg;
4603         u8 *mac_addr = NULL;
4604         int err;
4605
4606         memset(&sinfo, 0, sizeof(sinfo));
4607
4608         if (!info->attrs[NL80211_ATTR_MAC])
4609                 return -EINVAL;
4610
4611         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4612
4613         if (!rdev->ops->get_station)
4614                 return -EOPNOTSUPP;
4615
4616         err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
4617         if (err)
4618                 return err;
4619
4620         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4621         if (!msg)
4622                 return -ENOMEM;
4623
4624         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION,
4625                                  info->snd_portid, info->snd_seq, 0,
4626                                  rdev, dev, mac_addr, &sinfo) < 0) {
4627                 nlmsg_free(msg);
4628                 return -ENOBUFS;
4629         }
4630
4631         return genlmsg_reply(msg, info);
4632 }
4633
4634 int cfg80211_check_station_change(struct wiphy *wiphy,
4635                                   struct station_parameters *params,
4636                                   enum cfg80211_station_type statype)
4637 {
4638         if (params->listen_interval != -1 &&
4639             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4640                 return -EINVAL;
4641
4642         if (params->support_p2p_ps != -1 &&
4643             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4644                 return -EINVAL;
4645
4646         if (params->aid &&
4647             !(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) &&
4648             statype != CFG80211_STA_AP_CLIENT_UNASSOC)
4649                 return -EINVAL;
4650
4651         /* When you run into this, adjust the code below for the new flag */
4652         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4653
4654         switch (statype) {
4655         case CFG80211_STA_MESH_PEER_KERNEL:
4656         case CFG80211_STA_MESH_PEER_USER:
4657                 /*
4658                  * No ignoring the TDLS flag here -- the userspace mesh
4659                  * code doesn't have the bug of including TDLS in the
4660                  * mask everywhere.
4661                  */
4662                 if (params->sta_flags_mask &
4663                                 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4664                                   BIT(NL80211_STA_FLAG_MFP) |
4665                                   BIT(NL80211_STA_FLAG_AUTHORIZED)))
4666                         return -EINVAL;
4667                 break;
4668         case CFG80211_STA_TDLS_PEER_SETUP:
4669         case CFG80211_STA_TDLS_PEER_ACTIVE:
4670                 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4671                         return -EINVAL;
4672                 /* ignore since it can't change */
4673                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4674                 break;
4675         default:
4676                 /* disallow mesh-specific things */
4677                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
4678                         return -EINVAL;
4679                 if (params->local_pm)
4680                         return -EINVAL;
4681                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4682                         return -EINVAL;
4683         }
4684
4685         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4686             statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
4687                 /* TDLS can't be set, ... */
4688                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
4689                         return -EINVAL;
4690                 /*
4691                  * ... but don't bother the driver with it. This works around
4692                  * a hostapd/wpa_supplicant issue -- it always includes the
4693                  * TLDS_PEER flag in the mask even for AP mode.
4694                  */
4695                 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
4696         }
4697
4698         if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
4699             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4700                 /* reject other things that can't change */
4701                 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
4702                         return -EINVAL;
4703                 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
4704                         return -EINVAL;
4705                 if (params->supported_rates)
4706                         return -EINVAL;
4707                 if (params->ext_capab || params->ht_capa || params->vht_capa)
4708                         return -EINVAL;
4709         }
4710
4711         if (statype != CFG80211_STA_AP_CLIENT &&
4712             statype != CFG80211_STA_AP_CLIENT_UNASSOC) {
4713                 if (params->vlan)
4714                         return -EINVAL;
4715         }
4716
4717         switch (statype) {
4718         case CFG80211_STA_AP_MLME_CLIENT:
4719                 /* Use this only for authorizing/unauthorizing a station */
4720                 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4721                         return -EOPNOTSUPP;
4722                 break;
4723         case CFG80211_STA_AP_CLIENT:
4724         case CFG80211_STA_AP_CLIENT_UNASSOC:
4725                 /* accept only the listed bits */
4726                 if (params->sta_flags_mask &
4727                                 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4728                                   BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4729                                   BIT(NL80211_STA_FLAG_ASSOCIATED) |
4730                                   BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
4731                                   BIT(NL80211_STA_FLAG_WME) |
4732                                   BIT(NL80211_STA_FLAG_MFP)))
4733                         return -EINVAL;
4734
4735                 /* but authenticated/associated only if driver handles it */
4736                 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4737                     params->sta_flags_mask &
4738                                 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4739                                  BIT(NL80211_STA_FLAG_ASSOCIATED)))
4740                         return -EINVAL;
4741                 break;
4742         case CFG80211_STA_IBSS:
4743         case CFG80211_STA_AP_STA:
4744                 /* reject any changes other than AUTHORIZED */
4745                 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
4746                         return -EINVAL;
4747                 break;
4748         case CFG80211_STA_TDLS_PEER_SETUP:
4749                 /* reject any changes other than AUTHORIZED or WME */
4750                 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
4751                                                BIT(NL80211_STA_FLAG_WME)))
4752                         return -EINVAL;
4753                 /* force (at least) rates when authorizing */
4754                 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
4755                     !params->supported_rates)
4756                         return -EINVAL;
4757                 break;
4758         case CFG80211_STA_TDLS_PEER_ACTIVE:
4759                 /* reject any changes */
4760                 return -EINVAL;
4761         case CFG80211_STA_MESH_PEER_KERNEL:
4762                 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
4763                         return -EINVAL;
4764                 break;
4765         case CFG80211_STA_MESH_PEER_USER:
4766                 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION &&
4767                     params->plink_action != NL80211_PLINK_ACTION_BLOCK)
4768                         return -EINVAL;
4769                 break;
4770         }
4771
4772         /*
4773          * Older kernel versions ignored this attribute entirely, so don't
4774          * reject attempts to update it but mark it as unused instead so the
4775          * driver won't look at the data.
4776          */
4777         if (statype != CFG80211_STA_AP_CLIENT_UNASSOC &&
4778             statype != CFG80211_STA_TDLS_PEER_SETUP)
4779                 params->opmode_notif_used = false;
4780
4781         return 0;
4782 }
4783 EXPORT_SYMBOL(cfg80211_check_station_change);
4784
4785 /*
4786  * Get vlan interface making sure it is running and on the right wiphy.
4787  */
4788 static struct net_device *get_vlan(struct genl_info *info,
4789                                    struct cfg80211_registered_device *rdev)
4790 {
4791         struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
4792         struct net_device *v;
4793         int ret;
4794
4795         if (!vlanattr)
4796                 return NULL;
4797
4798         v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
4799         if (!v)
4800                 return ERR_PTR(-ENODEV);
4801
4802         if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
4803                 ret = -EINVAL;
4804                 goto error;
4805         }
4806
4807         if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4808             v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4809             v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
4810                 ret = -EINVAL;
4811                 goto error;
4812         }
4813
4814         if (!netif_running(v)) {
4815                 ret = -ENETDOWN;
4816                 goto error;
4817         }
4818
4819         return v;
4820  error:
4821         dev_put(v);
4822         return ERR_PTR(ret);
4823 }
4824
4825 static const struct nla_policy
4826 nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] = {
4827         [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
4828         [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
4829 };
4830
4831 static int nl80211_parse_sta_wme(struct genl_info *info,
4832                                  struct station_parameters *params)
4833 {
4834         struct nlattr *tb[NL80211_STA_WME_MAX + 1];
4835         struct nlattr *nla;
4836         int err;
4837
4838         /* parse WME attributes if present */
4839         if (!info->attrs[NL80211_ATTR_STA_WME])
4840                 return 0;
4841
4842         nla = info->attrs[NL80211_ATTR_STA_WME];
4843         err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
4844                                nl80211_sta_wme_policy, info->extack);
4845         if (err)
4846                 return err;
4847
4848         if (tb[NL80211_STA_WME_UAPSD_QUEUES])
4849                 params->uapsd_queues = nla_get_u8(
4850                         tb[NL80211_STA_WME_UAPSD_QUEUES]);
4851         if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
4852                 return -EINVAL;
4853
4854         if (tb[NL80211_STA_WME_MAX_SP])
4855                 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
4856
4857         if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
4858                 return -EINVAL;
4859
4860         params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
4861
4862         return 0;
4863 }
4864
4865 static int nl80211_parse_sta_channel_info(struct genl_info *info,
4866                                       struct station_parameters *params)
4867 {
4868         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
4869                 params->supported_channels =
4870                      nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4871                 params->supported_channels_len =
4872                      nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
4873                 /*
4874                  * Need to include at least one (first channel, number of
4875                  * channels) tuple for each subband, and must have proper
4876                  * tuples for the rest of the data as well.
4877                  */
4878                 if (params->supported_channels_len < 2)
4879                         return -EINVAL;
4880                 if (params->supported_channels_len % 2)
4881                         return -EINVAL;
4882         }
4883
4884         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
4885                 params->supported_oper_classes =
4886                  nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4887                 params->supported_oper_classes_len =
4888                   nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
4889                 /*
4890                  * The value of the Length field of the Supported Operating
4891                  * Classes element is between 2 and 253.
4892                  */
4893                 if (params->supported_oper_classes_len < 2 ||
4894                     params->supported_oper_classes_len > 253)
4895                         return -EINVAL;
4896         }
4897         return 0;
4898 }
4899
4900 static int nl80211_set_station_tdls(struct genl_info *info,
4901                                     struct station_parameters *params)
4902 {
4903         int err;
4904         /* Dummy STA entry gets updated once the peer capabilities are known */
4905         if (info->attrs[NL80211_ATTR_PEER_AID])
4906                 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
4907         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4908                 params->ht_capa =
4909                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
4910         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4911                 params->vht_capa =
4912                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4913
4914         err = nl80211_parse_sta_channel_info(info, params);
4915         if (err)
4916                 return err;
4917
4918         return nl80211_parse_sta_wme(info, params);
4919 }
4920
4921 static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
4922 {
4923         struct cfg80211_registered_device *rdev = info->user_ptr[0];
4924         struct net_device *dev = info->user_ptr[1];
4925         struct station_parameters params;
4926         u8 *mac_addr;
4927         int err;
4928
4929         memset(&params, 0, sizeof(params));
4930
4931         if (!rdev->ops->change_station)
4932                 return -EOPNOTSUPP;
4933
4934         /*
4935          * AID and listen_interval properties can be set only for unassociated
4936          * station. Include these parameters here and will check them in
4937          * cfg80211_check_station_change().
4938          */
4939         if (info->attrs[NL80211_ATTR_STA_AID])
4940                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
4941
4942         if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4943                 params.listen_interval =
4944                      nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
4945         else
4946                 params.listen_interval = -1;
4947
4948         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
4949                 u8 tmp;
4950
4951                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
4952                 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
4953                         return -EINVAL;
4954
4955                 params.support_p2p_ps = tmp;
4956         } else {
4957                 params.support_p2p_ps = -1;
4958         }
4959
4960         if (!info->attrs[NL80211_ATTR_MAC])
4961                 return -EINVAL;
4962
4963         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4964
4965         if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4966                 params.supported_rates =
4967                         nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4968                 params.supported_rates_len =
4969                         nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4970         }
4971
4972         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4973                 params.capability =
4974                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4975                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4976         }
4977
4978         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4979                 params.ext_capab =
4980                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4981                 params.ext_capab_len =
4982                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4983         }
4984
4985         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
4986                 return -EINVAL;
4987
4988         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
4989                 params.plink_action =
4990                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4991                 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4992                         return -EINVAL;
4993         }
4994
4995         if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
4996                 params.plink_state =
4997                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4998                 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4999                         return -EINVAL;
5000                 if (info->attrs[NL80211_ATTR_MESH_PEER_AID]) {
5001                         params.peer_aid = nla_get_u16(
5002                                 info->attrs[NL80211_ATTR_MESH_PEER_AID]);
5003                         if (params.peer_aid > IEEE80211_MAX_AID)
5004                                 return -EINVAL;
5005                 }
5006                 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
5007         }
5008
5009         if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
5010                 enum nl80211_mesh_power_mode pm = nla_get_u32(
5011                         info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
5012
5013                 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
5014                     pm > NL80211_MESH_POWER_MAX)
5015                         return -EINVAL;
5016
5017                 params.local_pm = pm;
5018         }
5019
5020         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5021                 params.opmode_notif_used = true;
5022                 params.opmode_notif =
5023                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5024         }
5025
5026         /* Include parameters for TDLS peer (will check later) */
5027         err = nl80211_set_station_tdls(info, &params);
5028         if (err)
5029                 return err;
5030
5031         params.vlan = get_vlan(info, rdev);
5032         if (IS_ERR(params.vlan))
5033                 return PTR_ERR(params.vlan);
5034
5035         switch (dev->ieee80211_ptr->iftype) {
5036         case NL80211_IFTYPE_AP:
5037         case NL80211_IFTYPE_AP_VLAN:
5038         case NL80211_IFTYPE_P2P_GO:
5039         case NL80211_IFTYPE_P2P_CLIENT:
5040         case NL80211_IFTYPE_STATION:
5041         case NL80211_IFTYPE_ADHOC:
5042         case NL80211_IFTYPE_MESH_POINT:
5043                 break;
5044         default:
5045                 err = -EOPNOTSUPP;
5046                 goto out_put_vlan;
5047         }
5048
5049         /* driver will call cfg80211_check_station_change() */
5050         err = rdev_change_station(rdev, dev, mac_addr, &params);
5051
5052  out_put_vlan:
5053         if (params.vlan)
5054                 dev_put(params.vlan);
5055
5056         return err;
5057 }
5058
5059 static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
5060 {
5061         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5062         int err;
5063         struct net_device *dev = info->user_ptr[1];
5064         struct station_parameters params;
5065         u8 *mac_addr = NULL;
5066         u32 auth_assoc = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
5067                          BIT(NL80211_STA_FLAG_ASSOCIATED);
5068
5069         memset(&params, 0, sizeof(params));
5070
5071         if (!rdev->ops->add_station)
5072                 return -EOPNOTSUPP;
5073
5074         if (!info->attrs[NL80211_ATTR_MAC])
5075                 return -EINVAL;
5076
5077         if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
5078                 return -EINVAL;
5079
5080         if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
5081                 return -EINVAL;
5082
5083         if (!info->attrs[NL80211_ATTR_STA_AID] &&
5084             !info->attrs[NL80211_ATTR_PEER_AID])
5085                 return -EINVAL;
5086
5087         mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
5088         params.supported_rates =
5089                 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5090         params.supported_rates_len =
5091                 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
5092         params.listen_interval =
5093                 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
5094
5095         if (info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]) {
5096                 u8 tmp;
5097
5098                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_STA_SUPPORT_P2P_PS]);
5099                 if (tmp >= NUM_NL80211_P2P_PS_STATUS)
5100                         return -EINVAL;
5101
5102                 params.support_p2p_ps = tmp;
5103         } else {
5104                 /*
5105                  * if not specified, assume it's supported for P2P GO interface,
5106                  * and is NOT supported for AP interface
5107                  */
5108                 params.support_p2p_ps =
5109                         dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO;
5110         }
5111
5112         if (info->attrs[NL80211_ATTR_PEER_AID])
5113                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
5114         else
5115                 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
5116         if (!params.aid || params.aid > IEEE80211_MAX_AID)
5117                 return -EINVAL;
5118
5119         if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
5120                 params.capability =
5121                         nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
5122                 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
5123         }
5124
5125         if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
5126                 params.ext_capab =
5127                         nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5128                 params.ext_capab_len =
5129                         nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
5130         }
5131
5132         if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
5133                 params.ht_capa =
5134                         nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5135
5136         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
5137                 params.vht_capa =
5138                         nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
5139
5140         if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
5141                 params.opmode_notif_used = true;
5142                 params.opmode_notif =
5143                         nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
5144         }
5145
5146         if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
5147                 params.plink_action =
5148                         nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
5149                 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
5150                         return -EINVAL;
5151         }
5152
5153         err = nl80211_parse_sta_channel_info(info, &params);
5154         if (err)
5155                 return err;
5156
5157         err = nl80211_parse_sta_wme(info, &params);
5158         if (err)
5159                 return err;
5160
5161         if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
5162                 return -EINVAL;
5163
5164         /* HT/VHT requires QoS, but if we don't have that just ignore HT/VHT
5165          * as userspace might just pass through the capabilities from the IEs
5166          * directly, rather than enforcing this restriction and returning an
5167          * error in this case.
5168          */
5169         if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME))) {
5170                 params.ht_capa = NULL;
5171                 params.vht_capa = NULL;
5172         }
5173
5174         /* When you run into this, adjust the code below for the new flag */
5175         BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
5176
5177         switch (dev->ieee80211_ptr->iftype) {
5178         case NL80211_IFTYPE_AP:
5179         case NL80211_IFTYPE_AP_VLAN:
5180         case NL80211_IFTYPE_P2P_GO:
5181                 /* ignore WME attributes if iface/sta is not capable */
5182                 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
5183                     !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
5184                         params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5185
5186                 /* TDLS peers cannot be added */
5187                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5188                     info->attrs[NL80211_ATTR_PEER_AID])
5189                         return -EINVAL;
5190                 /* but don't bother the driver with it */
5191                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
5192
5193                 /* allow authenticated/associated only if driver handles it */
5194                 if (!(rdev->wiphy.features &
5195                                 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
5196                     params.sta_flags_mask & auth_assoc)
5197                         return -EINVAL;
5198
5199                 /* Older userspace, or userspace wanting to be compatible with
5200                  * !NL80211_FEATURE_FULL_AP_CLIENT_STATE, will not set the auth
5201                  * and assoc flags in the mask, but assumes the station will be
5202                  * added as associated anyway since this was the required driver
5203                  * behaviour before NL80211_FEATURE_FULL_AP_CLIENT_STATE was
5204                  * introduced.
5205                  * In order to not bother drivers with this quirk in the API
5206                  * set the flags in both the mask and set for new stations in
5207                  * this case.
5208                  */
5209                 if (!(params.sta_flags_mask & auth_assoc)) {
5210                         params.sta_flags_mask |= auth_assoc;
5211                         params.sta_flags_set |= auth_assoc;
5212                 }
5213
5214                 /* must be last in here for error handling */
5215                 params.vlan = get_vlan(info, rdev);
5216                 if (IS_ERR(params.vlan))
5217                         return PTR_ERR(params.vlan);
5218                 break;
5219         case NL80211_IFTYPE_MESH_POINT:
5220                 /* ignore uAPSD data */
5221                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5222
5223                 /* associated is disallowed */
5224                 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
5225                         return -EINVAL;
5226                 /* TDLS peers cannot be added */
5227                 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
5228                     info->attrs[NL80211_ATTR_PEER_AID])
5229                         return -EINVAL;
5230                 break;
5231         case NL80211_IFTYPE_STATION:
5232         case NL80211_IFTYPE_P2P_CLIENT:
5233                 /* ignore uAPSD data */
5234                 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
5235
5236                 /* these are disallowed */
5237                 if (params.sta_flags_mask &
5238                                 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
5239                                  BIT(NL80211_STA_FLAG_AUTHENTICATED)))
5240                         return -EINVAL;
5241                 /* Only TDLS peers can be added */
5242                 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
5243                         return -EINVAL;
5244                 /* Can only add if TDLS ... */
5245                 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
5246                         return -EOPNOTSUPP;
5247                 /* ... with external setup is supported */
5248                 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
5249                         return -EOPNOTSUPP;
5250                 /*
5251                  * Older wpa_supplicant versions always mark the TDLS peer
5252                  * as authorized, but it shouldn't yet be.
5253                  */
5254                 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
5255                 break;
5256         default:
5257                 return -EOPNOTSUPP;
5258         }
5259
5260         /* be aware of params.vlan when changing code here */
5261
5262         err = rdev_add_station(rdev, dev, mac_addr, &params);
5263
5264         if (params.vlan)
5265                 dev_put(params.vlan);
5266         return err;
5267 }
5268
5269 static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
5270 {
5271         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5272         struct net_device *dev = info->user_ptr[1];
5273         struct station_del_parameters params;
5274
5275         memset(&params, 0, sizeof(params));
5276
5277         if (info->attrs[NL80211_ATTR_MAC])
5278                 params.mac = nla_data(info->attrs[NL80211_ATTR_MAC]);
5279
5280         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5281             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5282             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
5283             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5284                 return -EINVAL;
5285
5286         if (!rdev->ops->del_station)
5287                 return -EOPNOTSUPP;
5288
5289         if (info->attrs[NL80211_ATTR_MGMT_SUBTYPE]) {
5290                 params.subtype =
5291                         nla_get_u8(info->attrs[NL80211_ATTR_MGMT_SUBTYPE]);
5292                 if (params.subtype != IEEE80211_STYPE_DISASSOC >> 4 &&
5293                     params.subtype != IEEE80211_STYPE_DEAUTH >> 4)
5294                         return -EINVAL;
5295         } else {
5296                 /* Default to Deauthentication frame */
5297                 params.subtype = IEEE80211_STYPE_DEAUTH >> 4;
5298         }
5299
5300         if (info->attrs[NL80211_ATTR_REASON_CODE]) {
5301                 params.reason_code =
5302                         nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5303                 if (params.reason_code == 0)
5304                         return -EINVAL; /* 0 is reserved */
5305         } else {
5306                 /* Default to reason code 2 */
5307                 params.reason_code = WLAN_REASON_PREV_AUTH_NOT_VALID;
5308         }
5309
5310         return rdev_del_station(rdev, dev, &params);
5311 }
5312
5313 static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
5314                                 int flags, struct net_device *dev,
5315                                 u8 *dst, u8 *next_hop,
5316                                 struct mpath_info *pinfo)
5317 {
5318         void *hdr;
5319         struct nlattr *pinfoattr;
5320
5321         hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_MPATH);
5322         if (!hdr)
5323                 return -1;
5324
5325         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5326             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
5327             nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
5328             nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
5329                 goto nla_put_failure;
5330
5331         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
5332         if (!pinfoattr)
5333                 goto nla_put_failure;
5334         if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
5335             nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
5336                         pinfo->frame_qlen))
5337                 goto nla_put_failure;
5338         if (((pinfo->filled & MPATH_INFO_SN) &&
5339              nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
5340             ((pinfo->filled & MPATH_INFO_METRIC) &&
5341              nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
5342                          pinfo->metric)) ||
5343             ((pinfo->filled & MPATH_INFO_EXPTIME) &&
5344              nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
5345                          pinfo->exptime)) ||
5346             ((pinfo->filled & MPATH_INFO_FLAGS) &&
5347              nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
5348                         pinfo->flags)) ||
5349             ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
5350              nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
5351                          pinfo->discovery_timeout)) ||
5352             ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
5353              nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
5354                         pinfo->discovery_retries)))
5355                 goto nla_put_failure;
5356
5357         nla_nest_end(msg, pinfoattr);
5358
5359         genlmsg_end(msg, hdr);
5360         return 0;
5361
5362  nla_put_failure:
5363         genlmsg_cancel(msg, hdr);
5364         return -EMSGSIZE;
5365 }
5366
5367 static int nl80211_dump_mpath(struct sk_buff *skb,
5368                               struct netlink_callback *cb)
5369 {
5370         struct mpath_info pinfo;
5371         struct cfg80211_registered_device *rdev;
5372         struct wireless_dev *wdev;
5373         u8 dst[ETH_ALEN];
5374         u8 next_hop[ETH_ALEN];
5375         int path_idx = cb->args[2];
5376         int err;
5377
5378         rtnl_lock();
5379         err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5380         if (err)
5381                 goto out_err;
5382
5383         if (!rdev->ops->dump_mpath) {
5384                 err = -EOPNOTSUPP;
5385                 goto out_err;
5386         }
5387
5388         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5389                 err = -EOPNOTSUPP;
5390                 goto out_err;
5391         }
5392
5393         while (1) {
5394                 err = rdev_dump_mpath(rdev, wdev->netdev, path_idx, dst,
5395                                       next_hop, &pinfo);
5396                 if (err == -ENOENT)
5397                         break;
5398                 if (err)
5399                         goto out_err;
5400
5401                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5402                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
5403                                        wdev->netdev, dst, next_hop,
5404                                        &pinfo) < 0)
5405                         goto out;
5406
5407                 path_idx++;
5408         }
5409
5410  out:
5411         cb->args[2] = path_idx;
5412         err = skb->len;
5413  out_err:
5414         rtnl_unlock();
5415         return err;
5416 }
5417
5418 static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
5419 {
5420         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5421         int err;
5422         struct net_device *dev = info->user_ptr[1];
5423         struct mpath_info pinfo;
5424         struct sk_buff *msg;
5425         u8 *dst = NULL;
5426         u8 next_hop[ETH_ALEN];
5427
5428         memset(&pinfo, 0, sizeof(pinfo));
5429
5430         if (!info->attrs[NL80211_ATTR_MAC])
5431                 return -EINVAL;
5432
5433         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5434
5435         if (!rdev->ops->get_mpath)
5436                 return -EOPNOTSUPP;
5437
5438         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5439                 return -EOPNOTSUPP;
5440
5441         err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
5442         if (err)
5443                 return err;
5444
5445         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5446         if (!msg)
5447                 return -ENOMEM;
5448
5449         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5450                                  dev, dst, next_hop, &pinfo) < 0) {
5451                 nlmsg_free(msg);
5452                 return -ENOBUFS;
5453         }
5454
5455         return genlmsg_reply(msg, info);
5456 }
5457
5458 static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
5459 {
5460         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5461         struct net_device *dev = info->user_ptr[1];
5462         u8 *dst = NULL;
5463         u8 *next_hop = NULL;
5464
5465         if (!info->attrs[NL80211_ATTR_MAC])
5466                 return -EINVAL;
5467
5468         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5469                 return -EINVAL;
5470
5471         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5472         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5473
5474         if (!rdev->ops->change_mpath)
5475                 return -EOPNOTSUPP;
5476
5477         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5478                 return -EOPNOTSUPP;
5479
5480         return rdev_change_mpath(rdev, dev, dst, next_hop);
5481 }
5482
5483 static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
5484 {
5485         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5486         struct net_device *dev = info->user_ptr[1];
5487         u8 *dst = NULL;
5488         u8 *next_hop = NULL;
5489
5490         if (!info->attrs[NL80211_ATTR_MAC])
5491                 return -EINVAL;
5492
5493         if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
5494                 return -EINVAL;
5495
5496         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5497         next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
5498
5499         if (!rdev->ops->add_mpath)
5500                 return -EOPNOTSUPP;
5501
5502         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5503                 return -EOPNOTSUPP;
5504
5505         return rdev_add_mpath(rdev, dev, dst, next_hop);
5506 }
5507
5508 static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
5509 {
5510         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5511         struct net_device *dev = info->user_ptr[1];
5512         u8 *dst = NULL;
5513
5514         if (info->attrs[NL80211_ATTR_MAC])
5515                 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5516
5517         if (!rdev->ops->del_mpath)
5518                 return -EOPNOTSUPP;
5519
5520         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5521                 return -EOPNOTSUPP;
5522
5523         return rdev_del_mpath(rdev, dev, dst);
5524 }
5525
5526 static int nl80211_get_mpp(struct sk_buff *skb, struct genl_info *info)
5527 {
5528         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5529         int err;
5530         struct net_device *dev = info->user_ptr[1];
5531         struct mpath_info pinfo;
5532         struct sk_buff *msg;
5533         u8 *dst = NULL;
5534         u8 mpp[ETH_ALEN];
5535
5536         memset(&pinfo, 0, sizeof(pinfo));
5537
5538         if (!info->attrs[NL80211_ATTR_MAC])
5539                 return -EINVAL;
5540
5541         dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
5542
5543         if (!rdev->ops->get_mpp)
5544                 return -EOPNOTSUPP;
5545
5546         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5547                 return -EOPNOTSUPP;
5548
5549         err = rdev_get_mpp(rdev, dev, dst, mpp, &pinfo);
5550         if (err)
5551                 return err;
5552
5553         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5554         if (!msg)
5555                 return -ENOMEM;
5556
5557         if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
5558                                dev, dst, mpp, &pinfo) < 0) {
5559                 nlmsg_free(msg);
5560                 return -ENOBUFS;
5561         }
5562
5563         return genlmsg_reply(msg, info);
5564 }
5565
5566 static int nl80211_dump_mpp(struct sk_buff *skb,
5567                             struct netlink_callback *cb)
5568 {
5569         struct mpath_info pinfo;
5570         struct cfg80211_registered_device *rdev;
5571         struct wireless_dev *wdev;
5572         u8 dst[ETH_ALEN];
5573         u8 mpp[ETH_ALEN];
5574         int path_idx = cb->args[2];
5575         int err;
5576
5577         rtnl_lock();
5578         err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
5579         if (err)
5580                 goto out_err;
5581
5582         if (!rdev->ops->dump_mpp) {
5583                 err = -EOPNOTSUPP;
5584                 goto out_err;
5585         }
5586
5587         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
5588                 err = -EOPNOTSUPP;
5589                 goto out_err;
5590         }
5591
5592         while (1) {
5593                 err = rdev_dump_mpp(rdev, wdev->netdev, path_idx, dst,
5594                                     mpp, &pinfo);
5595                 if (err == -ENOENT)
5596                         break;
5597                 if (err)
5598                         goto out_err;
5599
5600                 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
5601                                        cb->nlh->nlmsg_seq, NLM_F_MULTI,
5602                                        wdev->netdev, dst, mpp,
5603                                        &pinfo) < 0)
5604                         goto out;
5605
5606                 path_idx++;
5607         }
5608
5609  out:
5610         cb->args[2] = path_idx;
5611         err = skb->len;
5612  out_err:
5613         rtnl_unlock();
5614         return err;
5615 }
5616
5617 static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
5618 {
5619         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5620         struct net_device *dev = info->user_ptr[1];
5621         struct wireless_dev *wdev = dev->ieee80211_ptr;
5622         struct bss_parameters params;
5623         int err;
5624
5625         memset(&params, 0, sizeof(params));
5626         /* default to not changing parameters */
5627         params.use_cts_prot = -1;
5628         params.use_short_preamble = -1;
5629         params.use_short_slot_time = -1;
5630         params.ap_isolate = -1;
5631         params.ht_opmode = -1;
5632         params.p2p_ctwindow = -1;
5633         params.p2p_opp_ps = -1;
5634
5635         if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
5636                 params.use_cts_prot =
5637                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
5638         if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
5639                 params.use_short_preamble =
5640                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
5641         if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
5642                 params.use_short_slot_time =
5643                     nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
5644         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5645                 params.basic_rates =
5646                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5647                 params.basic_rates_len =
5648                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5649         }
5650         if (info->attrs[NL80211_ATTR_AP_ISOLATE])
5651                 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
5652         if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
5653                 params.ht_opmode =
5654                         nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
5655
5656         if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
5657                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5658                         return -EINVAL;
5659                 params.p2p_ctwindow =
5660                         nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
5661                 if (params.p2p_ctwindow < 0)
5662                         return -EINVAL;
5663                 if (params.p2p_ctwindow != 0 &&
5664                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
5665                         return -EINVAL;
5666         }
5667
5668         if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
5669                 u8 tmp;
5670
5671                 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5672                         return -EINVAL;
5673                 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
5674                 if (tmp > 1)
5675                         return -EINVAL;
5676                 params.p2p_opp_ps = tmp;
5677                 if (params.p2p_opp_ps &&
5678                     !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
5679                         return -EINVAL;
5680         }
5681
5682         if (!rdev->ops->change_bss)
5683                 return -EOPNOTSUPP;
5684
5685         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5686             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5687                 return -EOPNOTSUPP;
5688
5689         wdev_lock(wdev);
5690         err = rdev_change_bss(rdev, dev, &params);
5691         wdev_unlock(wdev);
5692
5693         return err;
5694 }
5695
5696 static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
5697 {
5698         char *data = NULL;
5699         bool is_indoor;
5700         enum nl80211_user_reg_hint_type user_reg_hint_type;
5701         u32 owner_nlportid;
5702
5703         /*
5704          * You should only get this when cfg80211 hasn't yet initialized
5705          * completely when built-in to the kernel right between the time
5706          * window between nl80211_init() and regulatory_init(), if that is
5707          * even possible.
5708          */
5709         if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
5710                 return -EINPROGRESS;
5711
5712         if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
5713                 user_reg_hint_type =
5714                   nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
5715         else
5716                 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
5717
5718         switch (user_reg_hint_type) {
5719         case NL80211_USER_REG_HINT_USER:
5720         case NL80211_USER_REG_HINT_CELL_BASE:
5721                 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5722                         return -EINVAL;
5723
5724                 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5725                 return regulatory_hint_user(data, user_reg_hint_type);
5726         case NL80211_USER_REG_HINT_INDOOR:
5727                 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5728                         owner_nlportid = info->snd_portid;
5729                         is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
5730                 } else {
5731                         owner_nlportid = 0;
5732                         is_indoor = true;
5733                 }
5734
5735                 return regulatory_hint_indoor(is_indoor, owner_nlportid);
5736         default:
5737                 return -EINVAL;
5738         }
5739 }
5740
5741 static int nl80211_get_mesh_config(struct sk_buff *skb,
5742                                    struct genl_info *info)
5743 {
5744         struct cfg80211_registered_device *rdev = info->user_ptr[0];
5745         struct net_device *dev = info->user_ptr[1];
5746         struct wireless_dev *wdev = dev->ieee80211_ptr;
5747         struct mesh_config cur_params;
5748         int err = 0;
5749         void *hdr;
5750         struct nlattr *pinfoattr;
5751         struct sk_buff *msg;
5752
5753         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5754                 return -EOPNOTSUPP;
5755
5756         if (!rdev->ops->get_mesh_config)
5757                 return -EOPNOTSUPP;
5758
5759         wdev_lock(wdev);
5760         /* If not connected, get default parameters */
5761         if (!wdev->mesh_id_len)
5762                 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
5763         else
5764                 err = rdev_get_mesh_config(rdev, dev, &cur_params);
5765         wdev_unlock(wdev);
5766
5767         if (err)
5768                 return err;
5769
5770         /* Draw up a netlink message to send back */
5771         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5772         if (!msg)
5773                 return -ENOMEM;
5774         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
5775                              NL80211_CMD_GET_MESH_CONFIG);
5776         if (!hdr)
5777                 goto out;
5778         pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
5779         if (!pinfoattr)
5780                 goto nla_put_failure;
5781         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
5782             nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
5783                         cur_params.dot11MeshRetryTimeout) ||
5784             nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5785                         cur_params.dot11MeshConfirmTimeout) ||
5786             nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
5787                         cur_params.dot11MeshHoldingTimeout) ||
5788             nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
5789                         cur_params.dot11MeshMaxPeerLinks) ||
5790             nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
5791                        cur_params.dot11MeshMaxRetries) ||
5792             nla_put_u8(msg, NL80211_MESHCONF_TTL,
5793                        cur_params.dot11MeshTTL) ||
5794             nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
5795                        cur_params.element_ttl) ||
5796             nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
5797                        cur_params.auto_open_plinks) ||
5798             nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
5799                         cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
5800             nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
5801                        cur_params.dot11MeshHWMPmaxPREQretries) ||
5802             nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
5803                         cur_params.path_refresh_time) ||
5804             nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
5805                         cur_params.min_discovery_timeout) ||
5806             nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
5807                         cur_params.dot11MeshHWMPactivePathTimeout) ||
5808             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
5809                         cur_params.dot11MeshHWMPpreqMinInterval) ||
5810             nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
5811                         cur_params.dot11MeshHWMPperrMinInterval) ||
5812             nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
5813                         cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
5814             nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
5815                        cur_params.dot11MeshHWMPRootMode) ||
5816             nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
5817                         cur_params.dot11MeshHWMPRannInterval) ||
5818             nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
5819                        cur_params.dot11MeshGateAnnouncementProtocol) ||
5820             nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
5821                        cur_params.dot11MeshForwarding) ||
5822             nla_put_s32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
5823                         cur_params.rssi_threshold) ||
5824             nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
5825                         cur_params.ht_opmode) ||
5826             nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
5827                         cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
5828             nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
5829                         cur_params.dot11MeshHWMProotInterval) ||
5830             nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
5831                         cur_params.dot11MeshHWMPconfirmationInterval) ||
5832             nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
5833                         cur_params.power_mode) ||
5834             nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
5835                         cur_params.dot11MeshAwakeWindowDuration) ||
5836             nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
5837                         cur_params.plink_timeout))
5838                 goto nla_put_failure;
5839         nla_nest_end(msg, pinfoattr);
5840         genlmsg_end(msg, hdr);
5841         return genlmsg_reply(msg, info);
5842
5843  nla_put_failure:
5844         genlmsg_cancel(msg, hdr);
5845  out:
5846         nlmsg_free(msg);
5847         return -ENOBUFS;
5848 }
5849
5850 static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
5851         [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
5852         [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
5853         [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
5854         [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
5855         [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
5856         [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
5857         [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
5858         [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
5859         [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
5860         [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
5861         [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
5862         [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
5863         [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
5864         [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
5865         [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
5866         [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
5867         [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
5868         [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
5869         [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
5870         [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
5871         [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
5872         [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
5873         [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
5874         [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
5875         [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
5876         [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
5877         [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
5878         [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
5879 };
5880
5881 static const struct nla_policy
5882         nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
5883         [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
5884         [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
5885         [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
5886         [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
5887         [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
5888         [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
5889         [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
5890                                     .len = IEEE80211_MAX_DATA_LEN },
5891         [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
5892 };
5893
5894 static int nl80211_check_bool(const struct nlattr *nla, u8 min, u8 max, bool *out)
5895 {
5896         u8 val = nla_get_u8(nla);
5897         if (val < min || val > max)
5898                 return -EINVAL;
5899         *out = val;
5900         return 0;
5901 }
5902
5903 static int nl80211_check_u8(const struct nlattr *nla, u8 min, u8 max, u8 *out)
5904 {
5905         u8 val = nla_get_u8(nla);
5906         if (val < min || val > max)
5907                 return -EINVAL;
5908         *out = val;
5909         return 0;
5910 }
5911
5912 static int nl80211_check_u16(const struct nlattr *nla, u16 min, u16 max, u16 *out)
5913 {
5914         u16 val = nla_get_u16(nla);
5915         if (val < min || val > max)
5916                 return -EINVAL;
5917         *out = val;
5918         return 0;
5919 }
5920
5921 static int nl80211_check_u32(const struct nlattr *nla, u32 min, u32 max, u32 *out)
5922 {
5923         u32 val = nla_get_u32(nla);
5924         if (val < min || val > max)
5925                 return -EINVAL;
5926         *out = val;
5927         return 0;
5928 }
5929
5930 static int nl80211_check_s32(const struct nlattr *nla, s32 min, s32 max, s32 *out)
5931 {
5932         s32 val = nla_get_s32(nla);
5933         if (val < min || val > max)
5934                 return -EINVAL;
5935         *out = val;
5936         return 0;
5937 }
5938
5939 static int nl80211_check_power_mode(const struct nlattr *nla,
5940                                     enum nl80211_mesh_power_mode min,
5941                                     enum nl80211_mesh_power_mode max,
5942                                     enum nl80211_mesh_power_mode *out)
5943 {
5944         u32 val = nla_get_u32(nla);
5945         if (val < min || val > max)
5946                 return -EINVAL;
5947         *out = val;
5948         return 0;
5949 }
5950
5951 static int nl80211_parse_mesh_config(struct genl_info *info,
5952                                      struct mesh_config *cfg,
5953                                      u32 *mask_out)
5954 {
5955         struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
5956         u32 mask = 0;
5957         u16 ht_opmode;
5958
5959 #define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
5960 do {                                                                        \
5961         if (tb[attr]) {                                                     \
5962                 if (fn(tb[attr], min, max, &cfg->param))                    \
5963                         return -EINVAL;                                     \
5964                 mask |= (1 << (attr - 1));                                  \
5965         }                                                                   \
5966 } while (0)
5967
5968         if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
5969                 return -EINVAL;
5970         if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
5971                              info->attrs[NL80211_ATTR_MESH_CONFIG],
5972                              nl80211_meshconf_params_policy, info->extack))
5973                 return -EINVAL;
5974
5975         /* This makes sure that there aren't more than 32 mesh config
5976          * parameters (otherwise our bitfield scheme would not work.) */
5977         BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
5978
5979         /* Fill in the params struct */
5980         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
5981                                   mask, NL80211_MESHCONF_RETRY_TIMEOUT,
5982                                   nl80211_check_u16);
5983         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
5984                                   mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
5985                                   nl80211_check_u16);
5986         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
5987                                   mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
5988                                   nl80211_check_u16);
5989         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
5990                                   mask, NL80211_MESHCONF_MAX_PEER_LINKS,
5991                                   nl80211_check_u16);
5992         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
5993                                   mask, NL80211_MESHCONF_MAX_RETRIES,
5994                                   nl80211_check_u8);
5995         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
5996                                   mask, NL80211_MESHCONF_TTL, nl80211_check_u8);
5997         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
5998                                   mask, NL80211_MESHCONF_ELEMENT_TTL,
5999                                   nl80211_check_u8);
6000         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
6001                                   mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
6002                                   nl80211_check_bool);
6003         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
6004                                   1, 255, mask,
6005                                   NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
6006                                   nl80211_check_u32);
6007         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
6008                                   mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
6009                                   nl80211_check_u8);
6010         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
6011                                   mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
6012                                   nl80211_check_u32);
6013         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
6014                                   mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
6015                                   nl80211_check_u16);
6016         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
6017                                   1, 65535, mask,
6018                                   NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
6019                                   nl80211_check_u32);
6020         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
6021                                   1, 65535, mask,
6022                                   NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
6023                                   nl80211_check_u16);
6024         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
6025                                   1, 65535, mask,
6026                                   NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
6027                                   nl80211_check_u16);
6028         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6029                                   dot11MeshHWMPnetDiameterTraversalTime,
6030                                   1, 65535, mask,
6031                                   NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
6032                                   nl80211_check_u16);
6033         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
6034                                   mask, NL80211_MESHCONF_HWMP_ROOTMODE,
6035                                   nl80211_check_u8);
6036         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
6037                                   mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
6038                                   nl80211_check_u16);
6039         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6040                                   dot11MeshGateAnnouncementProtocol, 0, 1,
6041                                   mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
6042                                   nl80211_check_bool);
6043         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
6044                                   mask, NL80211_MESHCONF_FORWARDING,
6045                                   nl80211_check_bool);
6046         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
6047                                   mask, NL80211_MESHCONF_RSSI_THRESHOLD,
6048                                   nl80211_check_s32);
6049         /*
6050          * Check HT operation mode based on
6051          * IEEE 802.11-2016 9.4.2.57 HT Operation element.
6052          */
6053         if (tb[NL80211_MESHCONF_HT_OPMODE]) {
6054                 ht_opmode = nla_get_u16(tb[NL80211_MESHCONF_HT_OPMODE]);
6055
6056                 if (ht_opmode & ~(IEEE80211_HT_OP_MODE_PROTECTION |
6057                                   IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT |
6058                                   IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT))
6059                         return -EINVAL;
6060
6061                 /* NON_HT_STA bit is reserved, but some programs set it */
6062                 ht_opmode &= ~IEEE80211_HT_OP_MODE_NON_HT_STA_PRSNT;
6063
6064                 cfg->ht_opmode = ht_opmode;
6065                 mask |= (1 << (NL80211_MESHCONF_HT_OPMODE - 1));
6066         }
6067         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
6068                                   1, 65535, mask,
6069                                   NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
6070                                   nl80211_check_u32);
6071         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
6072                                   mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
6073                                   nl80211_check_u16);
6074         FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
6075                                   dot11MeshHWMPconfirmationInterval,
6076                                   1, 65535, mask,
6077                                   NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
6078                                   nl80211_check_u16);
6079         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
6080                                   NL80211_MESH_POWER_ACTIVE,
6081                                   NL80211_MESH_POWER_MAX,
6082                                   mask, NL80211_MESHCONF_POWER_MODE,
6083                                   nl80211_check_power_mode);
6084         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
6085                                   0, 65535, mask,
6086                                   NL80211_MESHCONF_AWAKE_WINDOW, nl80211_check_u16);
6087         FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
6088                                   mask, NL80211_MESHCONF_PLINK_TIMEOUT,
6089                                   nl80211_check_u32);
6090         if (mask_out)
6091                 *mask_out = mask;
6092
6093         return 0;
6094
6095 #undef FILL_IN_MESH_PARAM_IF_SET
6096 }
6097
6098 static int nl80211_parse_mesh_setup(struct genl_info *info,
6099                                      struct mesh_setup *setup)
6100 {
6101         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6102         struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
6103
6104         if (!info->attrs[NL80211_ATTR_MESH_SETUP])
6105                 return -EINVAL;
6106         if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
6107                              info->attrs[NL80211_ATTR_MESH_SETUP],
6108                              nl80211_mesh_setup_params_policy, info->extack))
6109                 return -EINVAL;
6110
6111         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
6112                 setup->sync_method =
6113                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
6114                  IEEE80211_SYNC_METHOD_VENDOR :
6115                  IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
6116
6117         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
6118                 setup->path_sel_proto =
6119                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
6120                  IEEE80211_PATH_PROTOCOL_VENDOR :
6121                  IEEE80211_PATH_PROTOCOL_HWMP;
6122
6123         if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
6124                 setup->path_metric =
6125                 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
6126                  IEEE80211_PATH_METRIC_VENDOR :
6127                  IEEE80211_PATH_METRIC_AIRTIME;
6128
6129         if (tb[NL80211_MESH_SETUP_IE]) {
6130                 struct nlattr *ieattr =
6131                         tb[NL80211_MESH_SETUP_IE];
6132                 if (!is_valid_ie_attr(ieattr))
6133                         return -EINVAL;
6134                 setup->ie = nla_data(ieattr);
6135                 setup->ie_len = nla_len(ieattr);
6136         }
6137         if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
6138             !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
6139                 return -EINVAL;
6140         setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
6141         setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
6142         setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
6143         if (setup->is_secure)
6144                 setup->user_mpm = true;
6145
6146         if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
6147                 if (!setup->user_mpm)
6148                         return -EINVAL;
6149                 setup->auth_id =
6150                         nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
6151         }
6152
6153         return 0;
6154 }
6155
6156 static int nl80211_update_mesh_config(struct sk_buff *skb,
6157                                       struct genl_info *info)
6158 {
6159         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6160         struct net_device *dev = info->user_ptr[1];
6161         struct wireless_dev *wdev = dev->ieee80211_ptr;
6162         struct mesh_config cfg;
6163         u32 mask;
6164         int err;
6165
6166         if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
6167                 return -EOPNOTSUPP;
6168
6169         if (!rdev->ops->update_mesh_config)
6170                 return -EOPNOTSUPP;
6171
6172         err = nl80211_parse_mesh_config(info, &cfg, &mask);
6173         if (err)
6174                 return err;
6175
6176         wdev_lock(wdev);
6177         if (!wdev->mesh_id_len)
6178                 err = -ENOLINK;
6179
6180         if (!err)
6181                 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
6182
6183         wdev_unlock(wdev);
6184
6185         return err;
6186 }
6187
6188 static int nl80211_put_regdom(const struct ieee80211_regdomain *regdom,
6189                               struct sk_buff *msg)
6190 {
6191         struct nlattr *nl_reg_rules;
6192         unsigned int i;
6193
6194         if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
6195             (regdom->dfs_region &&
6196              nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
6197                 goto nla_put_failure;
6198
6199         nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
6200         if (!nl_reg_rules)
6201                 goto nla_put_failure;
6202
6203         for (i = 0; i < regdom->n_reg_rules; i++) {
6204                 struct nlattr *nl_reg_rule;
6205                 const struct ieee80211_reg_rule *reg_rule;
6206                 const struct ieee80211_freq_range *freq_range;
6207                 const struct ieee80211_power_rule *power_rule;
6208                 unsigned int max_bandwidth_khz;
6209
6210                 reg_rule = &regdom->reg_rules[i];
6211                 freq_range = &reg_rule->freq_range;
6212                 power_rule = &reg_rule->power_rule;
6213
6214                 nl_reg_rule = nla_nest_start(msg, i);
6215                 if (!nl_reg_rule)
6216                         goto nla_put_failure;
6217
6218                 max_bandwidth_khz = freq_range->max_bandwidth_khz;
6219                 if (!max_bandwidth_khz)
6220                         max_bandwidth_khz = reg_get_max_bandwidth(regdom,
6221                                                                   reg_rule);
6222
6223                 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
6224                                 reg_rule->flags) ||
6225                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
6226                                 freq_range->start_freq_khz) ||
6227                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
6228                                 freq_range->end_freq_khz) ||
6229                     nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
6230                                 max_bandwidth_khz) ||
6231                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
6232                                 power_rule->max_antenna_gain) ||
6233                     nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
6234                                 power_rule->max_eirp) ||
6235                     nla_put_u32(msg, NL80211_ATTR_DFS_CAC_TIME,
6236                                 reg_rule->dfs_cac_ms))
6237                         goto nla_put_failure;
6238
6239                 nla_nest_end(msg, nl_reg_rule);
6240         }
6241
6242         nla_nest_end(msg, nl_reg_rules);
6243         return 0;
6244
6245 nla_put_failure:
6246         return -EMSGSIZE;
6247 }
6248
6249 static int nl80211_get_reg_do(struct sk_buff *skb, struct genl_info *info)
6250 {
6251         const struct ieee80211_regdomain *regdom = NULL;
6252         struct cfg80211_registered_device *rdev;
6253         struct wiphy *wiphy = NULL;
6254         struct sk_buff *msg;
6255         void *hdr;
6256
6257         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6258         if (!msg)
6259                 return -ENOBUFS;
6260
6261         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
6262                              NL80211_CMD_GET_REG);
6263         if (!hdr)
6264                 goto put_failure;
6265
6266         if (info->attrs[NL80211_ATTR_WIPHY]) {
6267                 bool self_managed;
6268
6269                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
6270                 if (IS_ERR(rdev)) {
6271                         nlmsg_free(msg);
6272                         return PTR_ERR(rdev);
6273                 }
6274
6275                 wiphy = &rdev->wiphy;
6276                 self_managed = wiphy->regulatory_flags &
6277                                REGULATORY_WIPHY_SELF_MANAGED;
6278                 regdom = get_wiphy_regdom(wiphy);
6279
6280                 /* a self-managed-reg device must have a private regdom */
6281                 if (WARN_ON(!regdom && self_managed)) {
6282                         nlmsg_free(msg);
6283                         return -EINVAL;
6284                 }
6285
6286                 if (regdom &&
6287                     nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6288                         goto nla_put_failure;
6289         }
6290
6291         if (!wiphy && reg_last_request_cell_base() &&
6292             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6293                         NL80211_USER_REG_HINT_CELL_BASE))
6294                 goto nla_put_failure;
6295
6296         rcu_read_lock();
6297
6298         if (!regdom)
6299                 regdom = rcu_dereference(cfg80211_regdomain);
6300
6301         if (nl80211_put_regdom(regdom, msg))
6302                 goto nla_put_failure_rcu;
6303
6304         rcu_read_unlock();
6305
6306         genlmsg_end(msg, hdr);
6307         return genlmsg_reply(msg, info);
6308
6309 nla_put_failure_rcu:
6310         rcu_read_unlock();
6311 nla_put_failure:
6312         genlmsg_cancel(msg, hdr);
6313 put_failure:
6314         nlmsg_free(msg);
6315         return -EMSGSIZE;
6316 }
6317
6318 static int nl80211_send_regdom(struct sk_buff *msg, struct netlink_callback *cb,
6319                                u32 seq, int flags, struct wiphy *wiphy,
6320                                const struct ieee80211_regdomain *regdom)
6321 {
6322         void *hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
6323                                    NL80211_CMD_GET_REG);
6324
6325         if (!hdr)
6326                 return -1;
6327
6328         genl_dump_check_consistent(cb, hdr, &nl80211_fam);
6329
6330         if (nl80211_put_regdom(regdom, msg))
6331                 goto nla_put_failure;
6332
6333         if (!wiphy && reg_last_request_cell_base() &&
6334             nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
6335                         NL80211_USER_REG_HINT_CELL_BASE))
6336                 goto nla_put_failure;
6337
6338         if (wiphy &&
6339             nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
6340                 goto nla_put_failure;
6341
6342         if (wiphy && wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
6343             nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
6344                 goto nla_put_failure;
6345
6346         genlmsg_end(msg, hdr);
6347         return 0;
6348
6349 nla_put_failure:
6350         genlmsg_cancel(msg, hdr);
6351         return -EMSGSIZE;
6352 }
6353
6354 static int nl80211_get_reg_dump(struct sk_buff *skb,
6355                                 struct netlink_callback *cb)
6356 {
6357         const struct ieee80211_regdomain *regdom = NULL;
6358         struct cfg80211_registered_device *rdev;
6359         int err, reg_idx, start = cb->args[2];
6360
6361         rtnl_lock();
6362
6363         if (cfg80211_regdomain && start == 0) {
6364                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6365                                           NLM_F_MULTI, NULL,
6366                                           rtnl_dereference(cfg80211_regdomain));
6367                 if (err < 0)
6368                         goto out_err;
6369         }
6370
6371         /* the global regdom is idx 0 */
6372         reg_idx = 1;
6373         list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
6374                 regdom = get_wiphy_regdom(&rdev->wiphy);
6375                 if (!regdom)
6376                         continue;
6377
6378                 if (++reg_idx <= start)
6379                         continue;
6380
6381                 err = nl80211_send_regdom(skb, cb, cb->nlh->nlmsg_seq,
6382                                           NLM_F_MULTI, &rdev->wiphy, regdom);
6383                 if (err < 0) {
6384                         reg_idx--;
6385                         break;
6386                 }
6387         }
6388
6389         cb->args[2] = reg_idx;
6390         err = skb->len;
6391 out_err:
6392         rtnl_unlock();
6393         return err;
6394 }
6395
6396 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
6397 static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
6398         [NL80211_ATTR_REG_RULE_FLAGS]           = { .type = NLA_U32 },
6399         [NL80211_ATTR_FREQ_RANGE_START]         = { .type = NLA_U32 },
6400         [NL80211_ATTR_FREQ_RANGE_END]           = { .type = NLA_U32 },
6401         [NL80211_ATTR_FREQ_RANGE_MAX_BW]        = { .type = NLA_U32 },
6402         [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]  = { .type = NLA_U32 },
6403         [NL80211_ATTR_POWER_RULE_MAX_EIRP]      = { .type = NLA_U32 },
6404         [NL80211_ATTR_DFS_CAC_TIME]             = { .type = NLA_U32 },
6405 };
6406
6407 static int parse_reg_rule(struct nlattr *tb[],
6408         struct ieee80211_reg_rule *reg_rule)
6409 {
6410         struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
6411         struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
6412
6413         if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
6414                 return -EINVAL;
6415         if (!tb[NL80211_ATTR_FREQ_RANGE_START])
6416                 return -EINVAL;
6417         if (!tb[NL80211_ATTR_FREQ_RANGE_END])
6418                 return -EINVAL;
6419         if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
6420                 return -EINVAL;
6421         if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
6422                 return -EINVAL;
6423
6424         reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
6425
6426         freq_range->start_freq_khz =
6427                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
6428         freq_range->end_freq_khz =
6429                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
6430         freq_range->max_bandwidth_khz =
6431                 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
6432
6433         power_rule->max_eirp =
6434                 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
6435
6436         if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
6437                 power_rule->max_antenna_gain =
6438                         nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
6439
6440         if (tb[NL80211_ATTR_DFS_CAC_TIME])
6441                 reg_rule->dfs_cac_ms =
6442                         nla_get_u32(tb[NL80211_ATTR_DFS_CAC_TIME]);
6443
6444         return 0;
6445 }
6446
6447 static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
6448 {
6449         struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
6450         struct nlattr *nl_reg_rule;
6451         char *alpha2;
6452         int rem_reg_rules, r;
6453         u32 num_rules = 0, rule_idx = 0, size_of_regd;
6454         enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
6455         struct ieee80211_regdomain *rd;
6456
6457         if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
6458                 return -EINVAL;
6459
6460         if (!info->attrs[NL80211_ATTR_REG_RULES])
6461                 return -EINVAL;
6462
6463         alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
6464
6465         if (info->attrs[NL80211_ATTR_DFS_REGION])
6466                 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
6467
6468         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6469                             rem_reg_rules) {
6470                 num_rules++;
6471                 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
6472                         return -EINVAL;
6473         }
6474
6475         if (!reg_is_valid_request(alpha2))
6476                 return -EINVAL;
6477
6478         size_of_regd = sizeof(struct ieee80211_regdomain) +
6479                        num_rules * sizeof(struct ieee80211_reg_rule);
6480
6481         rd = kzalloc(size_of_regd, GFP_KERNEL);
6482         if (!rd)
6483                 return -ENOMEM;
6484
6485         rd->n_reg_rules = num_rules;
6486         rd->alpha2[0] = alpha2[0];
6487         rd->alpha2[1] = alpha2[1];
6488
6489         /*
6490          * Disable DFS master mode if the DFS region was
6491          * not supported or known on this kernel.
6492          */
6493         if (reg_supported_dfs_region(dfs_region))
6494                 rd->dfs_region = dfs_region;
6495
6496         nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
6497                             rem_reg_rules) {
6498                 r = nla_parse_nested(tb, NL80211_REG_RULE_ATTR_MAX,
6499                                      nl_reg_rule, reg_rule_policy,
6500                                      info->extack);
6501                 if (r)
6502                         goto bad_reg;
6503                 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
6504                 if (r)
6505                         goto bad_reg;
6506
6507                 rule_idx++;
6508
6509                 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
6510                         r = -EINVAL;
6511                         goto bad_reg;
6512                 }
6513         }
6514
6515         /* set_regdom takes ownership of rd */
6516         return set_regdom(rd, REGD_SOURCE_CRDA);
6517  bad_reg:
6518         kfree(rd);
6519         return r;
6520 }
6521 #endif /* CONFIG_CFG80211_CRDA_SUPPORT */
6522
6523 static int validate_scan_freqs(struct nlattr *freqs)
6524 {
6525         struct nlattr *attr1, *attr2;
6526         int n_channels = 0, tmp1, tmp2;
6527
6528         nla_for_each_nested(attr1, freqs, tmp1)
6529                 if (nla_len(attr1) != sizeof(u32))
6530                         return 0;
6531
6532         nla_for_each_nested(attr1, freqs, tmp1) {
6533                 n_channels++;
6534                 /*
6535                  * Some hardware has a limited channel list for
6536                  * scanning, and it is pretty much nonsensical
6537                  * to scan for a channel twice, so disallow that
6538                  * and don't require drivers to check that the
6539                  * channel list they get isn't longer than what
6540                  * they can scan, as long as they can scan all
6541                  * the channels they registered at once.
6542                  */
6543                 nla_for_each_nested(attr2, freqs, tmp2)
6544                         if (attr1 != attr2 &&
6545                             nla_get_u32(attr1) == nla_get_u32(attr2))
6546                                 return 0;
6547         }
6548
6549         return n_channels;
6550 }
6551
6552 static bool is_band_valid(struct wiphy *wiphy, enum nl80211_band b)
6553 {
6554         return b < NUM_NL80211_BANDS && wiphy->bands[b];
6555 }
6556
6557 static int parse_bss_select(struct nlattr *nla, struct wiphy *wiphy,
6558                             struct cfg80211_bss_selection *bss_select)
6559 {
6560         struct nlattr *attr[NL80211_BSS_SELECT_ATTR_MAX + 1];
6561         struct nlattr *nest;
6562         int err;
6563         bool found = false;
6564         int i;
6565
6566         /* only process one nested attribute */
6567         nest = nla_data(nla);
6568         if (!nla_ok(nest, nla_len(nest)))
6569                 return -EINVAL;
6570
6571         err = nla_parse_nested(attr, NL80211_BSS_SELECT_ATTR_MAX, nest,
6572                                nl80211_bss_select_policy, NULL);
6573         if (err)
6574                 return err;
6575
6576         /* only one attribute may be given */
6577         for (i = 0; i <= NL80211_BSS_SELECT_ATTR_MAX; i++) {
6578                 if (attr[i]) {
6579                         if (found)
6580                                 return -EINVAL;
6581                         found = true;
6582                 }
6583         }
6584
6585         bss_select->behaviour = __NL80211_BSS_SELECT_ATTR_INVALID;
6586
6587         if (attr[NL80211_BSS_SELECT_ATTR_RSSI])
6588                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI;
6589
6590         if (attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]) {
6591                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_BAND_PREF;
6592                 bss_select->param.band_pref =
6593                         nla_get_u32(attr[NL80211_BSS_SELECT_ATTR_BAND_PREF]);
6594                 if (!is_band_valid(wiphy, bss_select->param.band_pref))
6595                         return -EINVAL;
6596         }
6597
6598         if (attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]) {
6599                 struct nl80211_bss_select_rssi_adjust *adj_param;
6600
6601                 adj_param = nla_data(attr[NL80211_BSS_SELECT_ATTR_RSSI_ADJUST]);
6602                 bss_select->behaviour = NL80211_BSS_SELECT_ATTR_RSSI_ADJUST;
6603                 bss_select->param.adjust.band = adj_param->band;
6604                 bss_select->param.adjust.delta = adj_param->delta;
6605                 if (!is_band_valid(wiphy, bss_select->param.adjust.band))
6606                         return -EINVAL;
6607         }
6608
6609         /* user-space did not provide behaviour attribute */
6610         if (bss_select->behaviour == __NL80211_BSS_SELECT_ATTR_INVALID)
6611                 return -EINVAL;
6612
6613         if (!(wiphy->bss_select_support & BIT(bss_select->behaviour)))
6614                 return -EINVAL;
6615
6616         return 0;
6617 }
6618
6619 static int nl80211_parse_random_mac(struct nlattr **attrs,
6620                                     u8 *mac_addr, u8 *mac_addr_mask)
6621 {
6622         int i;
6623
6624         if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
6625                 eth_zero_addr(mac_addr);
6626                 eth_zero_addr(mac_addr_mask);
6627                 mac_addr[0] = 0x2;
6628                 mac_addr_mask[0] = 0x3;
6629
6630                 return 0;
6631         }
6632
6633         /* need both or none */
6634         if (!attrs[NL80211_ATTR_MAC] || !attrs[NL80211_ATTR_MAC_MASK])
6635                 return -EINVAL;
6636
6637         memcpy(mac_addr, nla_data(attrs[NL80211_ATTR_MAC]), ETH_ALEN);
6638         memcpy(mac_addr_mask, nla_data(attrs[NL80211_ATTR_MAC_MASK]), ETH_ALEN);
6639
6640         /* don't allow or configure an mcast address */
6641         if (!is_multicast_ether_addr(mac_addr_mask) ||
6642             is_multicast_ether_addr(mac_addr))
6643                 return -EINVAL;
6644
6645         /*
6646          * allow users to pass a MAC address that has bits set outside
6647          * of the mask, but don't bother drivers with having to deal
6648          * with such bits
6649          */
6650         for (i = 0; i < ETH_ALEN; i++)
6651                 mac_addr[i] &= mac_addr_mask[i];
6652
6653         return 0;
6654 }
6655
6656 static bool cfg80211_off_channel_oper_allowed(struct wireless_dev *wdev)
6657 {
6658         ASSERT_WDEV_LOCK(wdev);
6659
6660         if (!cfg80211_beaconing_iface_active(wdev))
6661                 return true;
6662
6663         if (!(wdev->chandef.chan->flags & IEEE80211_CHAN_RADAR))
6664                 return true;
6665
6666         return regulatory_pre_cac_allowed(wdev->wiphy);
6667 }
6668
6669 static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
6670 {
6671         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6672         struct wireless_dev *wdev = info->user_ptr[1];
6673         struct cfg80211_scan_request *request;
6674         struct nlattr *attr;
6675         struct wiphy *wiphy;
6676         int err, tmp, n_ssids = 0, n_channels, i;
6677         size_t ie_len;
6678
6679         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6680                 return -EINVAL;
6681
6682         wiphy = &rdev->wiphy;
6683
6684         if (wdev->iftype == NL80211_IFTYPE_NAN)
6685                 return -EOPNOTSUPP;
6686
6687         if (!rdev->ops->scan)
6688                 return -EOPNOTSUPP;
6689
6690         if (rdev->scan_req || rdev->scan_msg) {
6691                 err = -EBUSY;
6692                 goto unlock;
6693         }
6694
6695         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6696                 n_channels = validate_scan_freqs(
6697                                 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
6698                 if (!n_channels) {
6699                         err = -EINVAL;
6700                         goto unlock;
6701                 }
6702         } else {
6703                 n_channels = ieee80211_get_num_supported_channels(wiphy);
6704         }
6705
6706         if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
6707                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
6708                         n_ssids++;
6709
6710         if (n_ssids > wiphy->max_scan_ssids) {
6711                 err = -EINVAL;
6712                 goto unlock;
6713         }
6714
6715         if (info->attrs[NL80211_ATTR_IE])
6716                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6717         else
6718                 ie_len = 0;
6719
6720         if (ie_len > wiphy->max_scan_ie_len) {
6721                 err = -EINVAL;
6722                 goto unlock;
6723         }
6724
6725         request = kzalloc(sizeof(*request)
6726                         + sizeof(*request->ssids) * n_ssids
6727                         + sizeof(*request->channels) * n_channels
6728                         + ie_len, GFP_KERNEL);
6729         if (!request) {
6730                 err = -ENOMEM;
6731                 goto unlock;
6732         }
6733
6734         if (n_ssids)
6735                 request->ssids = (void *)&request->channels[n_channels];
6736         request->n_ssids = n_ssids;
6737         if (ie_len) {
6738                 if (n_ssids)
6739                         request->ie = (void *)(request->ssids + n_ssids);
6740                 else
6741                         request->ie = (void *)(request->channels + n_channels);
6742         }
6743
6744         i = 0;
6745         if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
6746                 /* user specified, bail out if channel not found */
6747                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
6748                         struct ieee80211_channel *chan;
6749
6750                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
6751
6752                         if (!chan) {
6753                                 err = -EINVAL;
6754                                 goto out_free;
6755                         }
6756
6757                         /* ignore disabled channels */
6758                         if (chan->flags & IEEE80211_CHAN_DISABLED)
6759                                 continue;
6760
6761                         request->channels[i] = chan;
6762                         i++;
6763                 }
6764         } else {
6765                 enum nl80211_band band;
6766
6767                 /* all channels */
6768                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
6769                         int j;
6770
6771                         if (!wiphy->bands[band])
6772                                 continue;
6773                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
6774                                 struct ieee80211_channel *chan;
6775
6776                                 chan = &wiphy->bands[band]->channels[j];
6777
6778                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
6779                                         continue;
6780
6781                                 request->channels[i] = chan;
6782                                 i++;
6783                         }
6784                 }
6785         }
6786
6787         if (!i) {
6788                 err = -EINVAL;
6789                 goto out_free;
6790         }
6791
6792         request->n_channels = i;
6793
6794         wdev_lock(wdev);
6795         if (!cfg80211_off_channel_oper_allowed(wdev)) {
6796                 struct ieee80211_channel *chan;
6797
6798                 if (request->n_channels != 1) {
6799                         wdev_unlock(wdev);
6800                         err = -EBUSY;
6801                         goto out_free;
6802                 }
6803
6804                 chan = request->channels[0];
6805                 if (chan->center_freq != wdev->chandef.chan->center_freq) {
6806                         wdev_unlock(wdev);
6807                         err = -EBUSY;
6808                         goto out_free;
6809                 }
6810         }
6811         wdev_unlock(wdev);
6812
6813         i = 0;
6814         if (n_ssids) {
6815                 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
6816                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
6817                                 err = -EINVAL;
6818                                 goto out_free;
6819                         }
6820                         request->ssids[i].ssid_len = nla_len(attr);
6821                         memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
6822                         i++;
6823                 }
6824         }
6825
6826         if (info->attrs[NL80211_ATTR_IE]) {
6827                 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6828                 memcpy((void *)request->ie,
6829                        nla_data(info->attrs[NL80211_ATTR_IE]),
6830                        request->ie_len);
6831         }
6832
6833         for (i = 0; i < NUM_NL80211_BANDS; i++)
6834                 if (wiphy->bands[i])
6835                         request->rates[i] =
6836                                 (1 << wiphy->bands[i]->n_bitrates) - 1;
6837
6838         if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
6839                 nla_for_each_nested(attr,
6840                                     info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
6841                                     tmp) {
6842                         enum nl80211_band band = nla_type(attr);
6843
6844                         if (band < 0 || band >= NUM_NL80211_BANDS) {
6845                                 err = -EINVAL;
6846                                 goto out_free;
6847                         }
6848
6849                         if (!wiphy->bands[band])
6850                                 continue;
6851
6852                         err = ieee80211_get_ratemask(wiphy->bands[band],
6853                                                      nla_data(attr),
6854                                                      nla_len(attr),
6855                                                      &request->rates[band]);
6856                         if (err)
6857                                 goto out_free;
6858                 }
6859         }
6860
6861         if (info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]) {
6862                 if (!wiphy_ext_feature_isset(wiphy,
6863                                         NL80211_EXT_FEATURE_SET_SCAN_DWELL)) {
6864                         err = -EOPNOTSUPP;
6865                         goto out_free;
6866                 }
6867
6868                 request->duration =
6869                         nla_get_u16(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION]);
6870                 request->duration_mandatory =
6871                         nla_get_flag(info->attrs[NL80211_ATTR_MEASUREMENT_DURATION_MANDATORY]);
6872         }
6873
6874         if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
6875                 request->flags = nla_get_u32(
6876                         info->attrs[NL80211_ATTR_SCAN_FLAGS]);
6877                 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
6878                     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
6879                         err = -EOPNOTSUPP;
6880                         goto out_free;
6881                 }
6882
6883                 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6884                         if (!(wiphy->features &
6885                                         NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR)) {
6886                                 err = -EOPNOTSUPP;
6887                                 goto out_free;
6888                         }
6889
6890                         if (wdev->current_bss) {
6891                                 err = -EOPNOTSUPP;
6892                                 goto out_free;
6893                         }
6894
6895                         err = nl80211_parse_random_mac(info->attrs,
6896                                                        request->mac_addr,
6897                                                        request->mac_addr_mask);
6898                         if (err)
6899                                 goto out_free;
6900                 }
6901         }
6902
6903         request->no_cck =
6904                 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6905
6906         /* Initial implementation used NL80211_ATTR_MAC to set the specific
6907          * BSSID to scan for. This was problematic because that same attribute
6908          * was already used for another purpose (local random MAC address). The
6909          * NL80211_ATTR_BSSID attribute was added to fix this. For backwards
6910          * compatibility with older userspace components, also use the
6911          * NL80211_ATTR_MAC value here if it can be determined to be used for
6912          * the specific BSSID use case instead of the random MAC address
6913          * (NL80211_ATTR_SCAN_FLAGS is used to enable random MAC address use).
6914          */
6915         if (info->attrs[NL80211_ATTR_BSSID])
6916                 memcpy(request->bssid,
6917                        nla_data(info->attrs[NL80211_ATTR_BSSID]), ETH_ALEN);
6918         else if (!(request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) &&
6919                  info->attrs[NL80211_ATTR_MAC])
6920                 memcpy(request->bssid, nla_data(info->attrs[NL80211_ATTR_MAC]),
6921                        ETH_ALEN);
6922         else
6923                 eth_broadcast_addr(request->bssid);
6924
6925         request->wdev = wdev;
6926         request->wiphy = &rdev->wiphy;
6927         request->scan_start = jiffies;
6928
6929         rdev->scan_req = request;
6930         err = rdev_scan(rdev, request);
6931
6932         if (!err) {
6933                 nl80211_send_scan_start(rdev, wdev);
6934                 if (wdev->netdev)
6935                         dev_hold(wdev->netdev);
6936         } else {
6937  out_free:
6938                 rdev->scan_req = NULL;
6939                 kfree(request);
6940         }
6941
6942  unlock:
6943         return err;
6944 }
6945
6946 static int nl80211_abort_scan(struct sk_buff *skb, struct genl_info *info)
6947 {
6948         struct cfg80211_registered_device *rdev = info->user_ptr[0];
6949         struct wireless_dev *wdev = info->user_ptr[1];
6950
6951         if (!rdev->ops->abort_scan)
6952                 return -EOPNOTSUPP;
6953
6954         if (rdev->scan_msg)
6955                 return 0;
6956
6957         if (!rdev->scan_req)
6958                 return -ENOENT;
6959
6960         rdev_abort_scan(rdev, wdev);
6961         return 0;
6962 }
6963
6964 static int
6965 nl80211_parse_sched_scan_plans(struct wiphy *wiphy, int n_plans,
6966                                struct cfg80211_sched_scan_request *request,
6967                                struct nlattr **attrs)
6968 {
6969         int tmp, err, i = 0;
6970         struct nlattr *attr;
6971
6972         if (!attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
6973                 u32 interval;
6974
6975                 /*
6976                  * If scan plans are not specified,
6977                  * %NL80211_ATTR_SCHED_SCAN_INTERVAL will be specified. In this
6978                  * case one scan plan will be set with the specified scan
6979                  * interval and infinite number of iterations.
6980                  */
6981                 interval = nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
6982                 if (!interval)
6983                         return -EINVAL;
6984
6985                 request->scan_plans[0].interval =
6986                         DIV_ROUND_UP(interval, MSEC_PER_SEC);
6987                 if (!request->scan_plans[0].interval)
6988                         return -EINVAL;
6989
6990                 if (request->scan_plans[0].interval >
6991                     wiphy->max_sched_scan_plan_interval)
6992                         request->scan_plans[0].interval =
6993                                 wiphy->max_sched_scan_plan_interval;
6994
6995                 return 0;
6996         }
6997
6998         nla_for_each_nested(attr, attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp) {
6999                 struct nlattr *plan[NL80211_SCHED_SCAN_PLAN_MAX + 1];
7000
7001                 if (WARN_ON(i >= n_plans))
7002                         return -EINVAL;
7003
7004                 err = nla_parse_nested(plan, NL80211_SCHED_SCAN_PLAN_MAX,
7005                                        attr, nl80211_plan_policy, NULL);
7006                 if (err)
7007                         return err;
7008
7009                 if (!plan[NL80211_SCHED_SCAN_PLAN_INTERVAL])
7010                         return -EINVAL;
7011
7012                 request->scan_plans[i].interval =
7013                         nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_INTERVAL]);
7014                 if (!request->scan_plans[i].interval ||
7015                     request->scan_plans[i].interval >
7016                     wiphy->max_sched_scan_plan_interval)
7017                         return -EINVAL;
7018
7019                 if (plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]) {
7020                         request->scan_plans[i].iterations =
7021                                 nla_get_u32(plan[NL80211_SCHED_SCAN_PLAN_ITERATIONS]);
7022                         if (!request->scan_plans[i].iterations ||
7023                             (request->scan_plans[i].iterations >
7024                              wiphy->max_sched_scan_plan_iterations))
7025                                 return -EINVAL;
7026                 } else if (i < n_plans - 1) {
7027                         /*
7028                          * All scan plans but the last one must specify
7029                          * a finite number of iterations
7030                          */
7031                         return -EINVAL;
7032                 }
7033
7034                 i++;
7035         }
7036
7037         /*
7038          * The last scan plan must not specify the number of
7039          * iterations, it is supposed to run infinitely
7040          */
7041         if (request->scan_plans[n_plans - 1].iterations)
7042                 return  -EINVAL;
7043
7044         return 0;
7045 }
7046
7047 static struct cfg80211_sched_scan_request *
7048 nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
7049                          struct nlattr **attrs, int max_match_sets)
7050 {
7051         struct cfg80211_sched_scan_request *request;
7052         struct nlattr *attr;
7053         int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i, n_plans = 0;
7054         enum nl80211_band band;
7055         size_t ie_len;
7056         struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
7057         s32 default_match_rssi = NL80211_SCAN_RSSI_THOLD_OFF;
7058
7059         if (!is_valid_ie_attr(attrs[NL80211_ATTR_IE]))
7060                 return ERR_PTR(-EINVAL);
7061
7062         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7063                 n_channels = validate_scan_freqs(
7064                                 attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
7065                 if (!n_channels)
7066                         return ERR_PTR(-EINVAL);
7067         } else {
7068                 n_channels = ieee80211_get_num_supported_channels(wiphy);
7069         }
7070
7071         if (attrs[NL80211_ATTR_SCAN_SSIDS])
7072                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7073                                     tmp)
7074                         n_ssids++;
7075
7076         if (n_ssids > wiphy->max_sched_scan_ssids)
7077                 return ERR_PTR(-EINVAL);
7078
7079         /*
7080          * First, count the number of 'real' matchsets. Due to an issue with
7081          * the old implementation, matchsets containing only the RSSI attribute
7082          * (NL80211_SCHED_SCAN_MATCH_ATTR_RSSI) are considered as the 'default'
7083          * RSSI for all matchsets, rather than their own matchset for reporting
7084          * all APs with a strong RSSI. This is needed to be compatible with
7085          * older userspace that treated a matchset with only the RSSI as the
7086          * global RSSI for all other matchsets - if there are other matchsets.
7087          */
7088         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7089                 nla_for_each_nested(attr,
7090                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7091                                     tmp) {
7092                         struct nlattr *rssi;
7093
7094                         err = nla_parse_nested(tb,
7095                                                NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7096                                                attr, nl80211_match_policy,
7097                                                NULL);
7098                         if (err)
7099                                 return ERR_PTR(err);
7100
7101                         /* SSID and BSSID are mutually exclusive */
7102                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] &&
7103                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID])
7104                                 return ERR_PTR(-EINVAL);
7105
7106                         /* add other standalone attributes here */
7107                         if (tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID] ||
7108                             tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID]) {
7109                                 n_match_sets++;
7110                                 continue;
7111                         }
7112                         rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7113                         if (rssi)
7114                                 default_match_rssi = nla_get_s32(rssi);
7115                 }
7116         }
7117
7118         /* However, if there's no other matchset, add the RSSI one */
7119         if (!n_match_sets && default_match_rssi != NL80211_SCAN_RSSI_THOLD_OFF)
7120                 n_match_sets = 1;
7121
7122         if (n_match_sets > max_match_sets)
7123                 return ERR_PTR(-EINVAL);
7124
7125         if (attrs[NL80211_ATTR_IE])
7126                 ie_len = nla_len(attrs[NL80211_ATTR_IE]);
7127         else
7128                 ie_len = 0;
7129
7130         if (ie_len > wiphy->max_sched_scan_ie_len)
7131                 return ERR_PTR(-EINVAL);
7132
7133         if (attrs[NL80211_ATTR_SCHED_SCAN_PLANS]) {
7134                 /*
7135                  * NL80211_ATTR_SCHED_SCAN_INTERVAL must not be specified since
7136                  * each scan plan already specifies its own interval
7137                  */
7138                 if (attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7139                         return ERR_PTR(-EINVAL);
7140
7141                 nla_for_each_nested(attr,
7142                                     attrs[NL80211_ATTR_SCHED_SCAN_PLANS], tmp)
7143                         n_plans++;
7144         } else {
7145                 /*
7146                  * The scan interval attribute is kept for backward
7147                  * compatibility. If no scan plans are specified and sched scan
7148                  * interval is specified, one scan plan will be set with this
7149                  * scan interval and infinite number of iterations.
7150                  */
7151                 if (!attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
7152                         return ERR_PTR(-EINVAL);
7153
7154                 n_plans = 1;
7155         }
7156
7157         if (!n_plans || n_plans > wiphy->max_sched_scan_plans)
7158                 return ERR_PTR(-EINVAL);
7159
7160         if (!wiphy_ext_feature_isset(
7161                     wiphy, NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI) &&
7162             (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI] ||
7163              attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
7164                 return ERR_PTR(-EINVAL);
7165
7166         request = kzalloc(sizeof(*request)
7167                         + sizeof(*request->ssids) * n_ssids
7168                         + sizeof(*request->match_sets) * n_match_sets
7169                         + sizeof(*request->scan_plans) * n_plans
7170                         + sizeof(*request->channels) * n_channels
7171                         + ie_len, GFP_KERNEL);
7172         if (!request)
7173                 return ERR_PTR(-ENOMEM);
7174
7175         if (n_ssids)
7176                 request->ssids = (void *)&request->channels[n_channels];
7177         request->n_ssids = n_ssids;
7178         if (ie_len) {
7179                 if (n_ssids)
7180                         request->ie = (void *)(request->ssids + n_ssids);
7181                 else
7182                         request->ie = (void *)(request->channels + n_channels);
7183         }
7184
7185         if (n_match_sets) {
7186                 if (request->ie)
7187                         request->match_sets = (void *)(request->ie + ie_len);
7188                 else if (n_ssids)
7189                         request->match_sets =
7190                                 (void *)(request->ssids + n_ssids);
7191                 else
7192                         request->match_sets =
7193                                 (void *)(request->channels + n_channels);
7194         }
7195         request->n_match_sets = n_match_sets;
7196
7197         if (n_match_sets)
7198                 request->scan_plans = (void *)(request->match_sets +
7199                                                n_match_sets);
7200         else if (request->ie)
7201                 request->scan_plans = (void *)(request->ie + ie_len);
7202         else if (n_ssids)
7203                 request->scan_plans = (void *)(request->ssids + n_ssids);
7204         else
7205                 request->scan_plans = (void *)(request->channels + n_channels);
7206
7207         request->n_scan_plans = n_plans;
7208
7209         i = 0;
7210         if (attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
7211                 /* user specified, bail out if channel not found */
7212                 nla_for_each_nested(attr,
7213                                     attrs[NL80211_ATTR_SCAN_FREQUENCIES],
7214                                     tmp) {
7215                         struct ieee80211_channel *chan;
7216
7217                         chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
7218
7219                         if (!chan) {
7220                                 err = -EINVAL;
7221                                 goto out_free;
7222                         }
7223
7224                         /* ignore disabled channels */
7225                         if (chan->flags & IEEE80211_CHAN_DISABLED)
7226                                 continue;
7227
7228                         request->channels[i] = chan;
7229                         i++;
7230                 }
7231         } else {
7232                 /* all channels */
7233                 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7234                         int j;
7235
7236                         if (!wiphy->bands[band])
7237                                 continue;
7238                         for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
7239                                 struct ieee80211_channel *chan;
7240
7241                                 chan = &wiphy->bands[band]->channels[j];
7242
7243                                 if (chan->flags & IEEE80211_CHAN_DISABLED)
7244                                         continue;
7245
7246                                 request->channels[i] = chan;
7247                                 i++;
7248                         }
7249                 }
7250         }
7251
7252         if (!i) {
7253                 err = -EINVAL;
7254                 goto out_free;
7255         }
7256
7257         request->n_channels = i;
7258
7259         i = 0;
7260         if (n_ssids) {
7261                 nla_for_each_nested(attr, attrs[NL80211_ATTR_SCAN_SSIDS],
7262                                     tmp) {
7263                         if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
7264                                 err = -EINVAL;
7265                                 goto out_free;
7266                         }
7267                         request->ssids[i].ssid_len = nla_len(attr);
7268                         memcpy(request->ssids[i].ssid, nla_data(attr),
7269                                nla_len(attr));
7270                         i++;
7271                 }
7272         }
7273
7274         i = 0;
7275         if (attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
7276                 nla_for_each_nested(attr,
7277                                     attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
7278                                     tmp) {
7279                         struct nlattr *ssid, *bssid, *rssi;
7280
7281                         err = nla_parse_nested(tb,
7282                                                NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
7283                                                attr, nl80211_match_policy,
7284                                                NULL);
7285                         if (err)
7286                                 goto out_free;
7287                         ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
7288                         bssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_BSSID];
7289                         if (ssid || bssid) {
7290                                 if (WARN_ON(i >= n_match_sets)) {
7291                                         /* this indicates a programming error,
7292                                          * the loop above should have verified
7293                                          * things properly
7294                                          */
7295                                         err = -EINVAL;
7296                                         goto out_free;
7297                                 }
7298
7299                                 if (ssid) {
7300                                         if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
7301                                                 err = -EINVAL;
7302                                                 goto out_free;
7303                                         }
7304                                         memcpy(request->match_sets[i].ssid.ssid,
7305                                                nla_data(ssid), nla_len(ssid));
7306                                         request->match_sets[i].ssid.ssid_len =
7307                                                 nla_len(ssid);
7308                                 }
7309                                 if (bssid) {
7310                                         if (nla_len(bssid) != ETH_ALEN) {
7311                                                 err = -EINVAL;
7312                                                 goto out_free;
7313                                         }
7314                                         memcpy(request->match_sets[i].bssid,
7315                                                nla_data(bssid), ETH_ALEN);
7316                                 }
7317
7318                                 /* special attribute - old implementation w/a */
7319                                 request->match_sets[i].rssi_thold =
7320                                         default_match_rssi;
7321                                 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
7322                                 if (rssi)
7323                                         request->match_sets[i].rssi_thold =
7324                                                 nla_get_s32(rssi);
7325                         }
7326                         i++;
7327                 }
7328
7329                 /* there was no other matchset, so the RSSI one is alone */
7330                 if (i == 0 && n_match_sets)
7331                         request->match_sets[0].rssi_thold = default_match_rssi;
7332
7333                 request->min_rssi_thold = INT_MAX;
7334                 for (i = 0; i < n_match_sets; i++)
7335                         request->min_rssi_thold =
7336                                 min(request->match_sets[i].rssi_thold,
7337                                     request->min_rssi_thold);
7338         } else {
7339                 request->min_rssi_thold = NL80211_SCAN_RSSI_THOLD_OFF;
7340         }
7341
7342         if (ie_len) {
7343                 request->ie_len = ie_len;
7344                 memcpy((void *)request->ie,
7345                        nla_data(attrs[NL80211_ATTR_IE]),
7346                        request->ie_len);
7347         }
7348
7349         if (attrs[NL80211_ATTR_SCAN_FLAGS]) {
7350                 request->flags = nla_get_u32(
7351                         attrs[NL80211_ATTR_SCAN_FLAGS]);
7352                 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
7353                     !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
7354                         err = -EOPNOTSUPP;
7355                         goto out_free;
7356                 }
7357
7358                 if (request->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
7359                         u32 flg = NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
7360
7361                         if (!wdev) /* must be net-detect */
7362                                 flg = NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7363
7364                         if (!(wiphy->features & flg)) {
7365                                 err = -EOPNOTSUPP;
7366                                 goto out_free;
7367                         }
7368
7369                         if (wdev && wdev->current_bss) {
7370                                 err = -EOPNOTSUPP;
7371                                 goto out_free;
7372                         }
7373
7374                         err = nl80211_parse_random_mac(attrs, request->mac_addr,
7375                                                        request->mac_addr_mask);
7376                         if (err)
7377                                 goto out_free;
7378                 }
7379         }
7380
7381         if (attrs[NL80211_ATTR_SCHED_SCAN_DELAY])
7382                 request->delay =
7383                         nla_get_u32(attrs[NL80211_ATTR_SCHED_SCAN_DELAY]);
7384
7385         if (attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]) {
7386                 request->relative_rssi = nla_get_s8(
7387                         attrs[NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI]);
7388                 request->relative_rssi_set = true;
7389         }
7390
7391         if (request->relative_rssi_set &&
7392             attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]) {
7393                 struct nl80211_bss_select_rssi_adjust *rssi_adjust;
7394
7395                 rssi_adjust = nla_data(
7396                         attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]);
7397                 request->rssi_adjust.band = rssi_adjust->band;
7398                 request->rssi_adjust.delta = rssi_adjust->delta;
7399                 if (!is_band_valid(wiphy, request->rssi_adjust.band)) {
7400                         err = -EINVAL;
7401                         goto out_free;
7402                 }
7403         }
7404
7405         err = nl80211_parse_sched_scan_plans(wiphy, n_plans, request, attrs);
7406         if (err)
7407                 goto out_free;
7408
7409         request->scan_start = jiffies;
7410
7411         return request;
7412
7413 out_free:
7414         kfree(request);
7415         return ERR_PTR(err);
7416 }
7417
7418 static int nl80211_start_sched_scan(struct sk_buff *skb,
7419                                     struct genl_info *info)
7420 {
7421         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7422         struct net_device *dev = info->user_ptr[1];
7423         struct wireless_dev *wdev = dev->ieee80211_ptr;
7424         struct cfg80211_sched_scan_request *sched_scan_req;
7425         bool want_multi;
7426         int err;
7427
7428         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_start)
7429                 return -EOPNOTSUPP;
7430
7431         want_multi = info->attrs[NL80211_ATTR_SCHED_SCAN_MULTI];
7432         err = cfg80211_sched_scan_req_possible(rdev, want_multi);
7433         if (err)
7434                 return err;
7435
7436         sched_scan_req = nl80211_parse_sched_scan(&rdev->wiphy, wdev,
7437                                                   info->attrs,
7438                                                   rdev->wiphy.max_match_sets);
7439
7440         err = PTR_ERR_OR_ZERO(sched_scan_req);
7441         if (err)
7442                 goto out_err;
7443
7444         /* leave request id zero for legacy request
7445          * or if driver does not support multi-scheduled scan
7446          */
7447         if (want_multi && rdev->wiphy.max_sched_scan_reqs > 1) {
7448                 while (!sched_scan_req->reqid)
7449                         sched_scan_req->reqid = rdev->wiphy.cookie_counter++;
7450         }
7451
7452         err = rdev_sched_scan_start(rdev, dev, sched_scan_req);
7453         if (err)
7454                 goto out_free;
7455
7456         sched_scan_req->dev = dev;
7457         sched_scan_req->wiphy = &rdev->wiphy;
7458
7459         if (info->attrs[NL80211_ATTR_SOCKET_OWNER])
7460                 sched_scan_req->owner_nlportid = info->snd_portid;
7461
7462         cfg80211_add_sched_scan_req(rdev, sched_scan_req);
7463
7464         nl80211_send_sched_scan(sched_scan_req, NL80211_CMD_START_SCHED_SCAN);
7465         return 0;
7466
7467 out_free:
7468         kfree(sched_scan_req);
7469 out_err:
7470         return err;
7471 }
7472
7473 static int nl80211_stop_sched_scan(struct sk_buff *skb,
7474                                    struct genl_info *info)
7475 {
7476         struct cfg80211_sched_scan_request *req;
7477         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7478         u64 cookie;
7479
7480         if (!rdev->wiphy.max_sched_scan_reqs || !rdev->ops->sched_scan_stop)
7481                 return -EOPNOTSUPP;
7482
7483         if (info->attrs[NL80211_ATTR_COOKIE]) {
7484                 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7485                 return __cfg80211_stop_sched_scan(rdev, cookie, false);
7486         }
7487
7488         req = list_first_or_null_rcu(&rdev->sched_scan_req_list,
7489                                      struct cfg80211_sched_scan_request,
7490                                      list);
7491         if (!req || req->reqid ||
7492             (req->owner_nlportid &&
7493              req->owner_nlportid != info->snd_portid))
7494                 return -ENOENT;
7495
7496         return cfg80211_stop_sched_scan_req(rdev, req, false);
7497 }
7498
7499 static int nl80211_start_radar_detection(struct sk_buff *skb,
7500                                          struct genl_info *info)
7501 {
7502         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7503         struct net_device *dev = info->user_ptr[1];
7504         struct wireless_dev *wdev = dev->ieee80211_ptr;
7505         struct cfg80211_chan_def chandef;
7506         enum nl80211_dfs_regions dfs_region;
7507         unsigned int cac_time_ms;
7508         int err;
7509
7510         dfs_region = reg_get_dfs_region(wdev->wiphy);
7511         if (dfs_region == NL80211_DFS_UNSET)
7512                 return -EINVAL;
7513
7514         err = nl80211_parse_chandef(rdev, info, &chandef);
7515         if (err)
7516                 return err;
7517
7518         if (netif_carrier_ok(dev))
7519                 return -EBUSY;
7520
7521         if (wdev->cac_started)
7522                 return -EBUSY;
7523
7524         err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef,
7525                                             wdev->iftype);
7526         if (err < 0)
7527                 return err;
7528
7529         if (err == 0)
7530                 return -EINVAL;
7531
7532         if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
7533                 return -EINVAL;
7534
7535         if (!rdev->ops->start_radar_detection)
7536                 return -EOPNOTSUPP;
7537
7538         cac_time_ms = cfg80211_chandef_dfs_cac_time(&rdev->wiphy, &chandef);
7539         if (WARN_ON(!cac_time_ms))
7540                 cac_time_ms = IEEE80211_DFS_MIN_CAC_TIME_MS;
7541
7542         err = rdev_start_radar_detection(rdev, dev, &chandef, cac_time_ms);
7543         if (!err) {
7544                 wdev->chandef = chandef;
7545                 wdev->cac_started = true;
7546                 wdev->cac_start_time = jiffies;
7547                 wdev->cac_time_ms = cac_time_ms;
7548         }
7549         return err;
7550 }
7551
7552 static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
7553 {
7554         struct cfg80211_registered_device *rdev = info->user_ptr[0];
7555         struct net_device *dev = info->user_ptr[1];
7556         struct wireless_dev *wdev = dev->ieee80211_ptr;
7557         struct cfg80211_csa_settings params;
7558         /* csa_attrs is defined static to avoid waste of stack size - this
7559          * function is called under RTNL lock, so this should not be a problem.
7560          */
7561         static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
7562         int err;
7563         bool need_new_beacon = false;
7564         bool need_handle_dfs_flag = true;
7565         int len, i;
7566         u32 cs_count;
7567
7568         if (!rdev->ops->channel_switch ||
7569             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
7570                 return -EOPNOTSUPP;
7571
7572         switch (dev->ieee80211_ptr->iftype) {
7573         case NL80211_IFTYPE_AP:
7574         case NL80211_IFTYPE_P2P_GO:
7575                 need_new_beacon = true;
7576                 /* For all modes except AP the handle_dfs flag needs to be
7577                  * supplied to tell the kernel that userspace will handle radar
7578                  * events when they happen. Otherwise a switch to a channel
7579                  * requiring DFS will be rejected.
7580                  */
7581                 need_handle_dfs_flag = false;
7582
7583                 /* useless if AP is not running */
7584                 if (!wdev->beacon_interval)
7585                         return -ENOTCONN;
7586                 break;
7587         case NL80211_IFTYPE_ADHOC:
7588                 if (!wdev->ssid_len)
7589                         return -ENOTCONN;
7590                 break;
7591         case NL80211_IFTYPE_MESH_POINT:
7592                 if (!wdev->mesh_id_len)
7593                         return -ENOTCONN;
7594                 break;
7595         default:
7596                 return -EOPNOTSUPP;
7597         }
7598
7599         memset(&params, 0, sizeof(params));
7600
7601         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7602             !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
7603                 return -EINVAL;
7604
7605         /* only important for AP, IBSS and mesh create IEs internally */
7606         if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
7607                 return -EINVAL;
7608
7609         /* Even though the attribute is u32, the specification says
7610          * u8, so let's make sure we don't overflow.
7611          */
7612         cs_count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
7613         if (cs_count > 255)
7614                 return -EINVAL;
7615
7616         params.count = cs_count;
7617
7618         if (!need_new_beacon)
7619                 goto skip_beacons;
7620
7621         err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
7622         if (err)
7623                 return err;
7624
7625         err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
7626                                info->attrs[NL80211_ATTR_CSA_IES],
7627                                nl80211_policy, info->extack);
7628         if (err)
7629                 return err;
7630
7631         err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
7632         if (err)
7633                 return err;
7634
7635         if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
7636                 return -EINVAL;
7637
7638         len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7639         if (!len || (len % sizeof(u16)))
7640                 return -EINVAL;
7641
7642         params.n_counter_offsets_beacon = len / sizeof(u16);
7643         if (rdev->wiphy.max_num_csa_counters &&
7644             (params.n_counter_offsets_beacon >
7645              rdev->wiphy.max_num_csa_counters))
7646                 return -EINVAL;
7647
7648         params.counter_offsets_beacon =
7649                 nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
7650
7651         /* sanity checks - counters should fit and be the same */
7652         for (i = 0; i < params.n_counter_offsets_beacon; i++) {
7653                 u16 offset = params.counter_offsets_beacon[i];
7654
7655                 if (offset >= params.beacon_csa.tail_len)
7656                         return -EINVAL;
7657
7658                 if (params.beacon_csa.tail[offset] != params.count)
7659                         return -EINVAL;
7660         }
7661
7662         if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
7663                 len = nla_len(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7664                 if (!len || (len % sizeof(u16)))
7665                         return -EINVAL;
7666
7667                 params.n_counter_offsets_presp = len / sizeof(u16);
7668                 if (rdev->wiphy.max_num_csa_counters &&
7669                     (params.n_counter_offsets_presp >
7670                      rdev->wiphy.max_num_csa_counters))
7671                         return -EINVAL;
7672
7673                 params.counter_offsets_presp =
7674                         nla_data(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
7675
7676                 /* sanity checks - counters should fit and be the same */
7677                 for (i = 0; i < params.n_counter_offsets_presp; i++) {
7678                         u16 offset = params.counter_offsets_presp[i];
7679
7680                         if (offset >= params.beacon_csa.probe_resp_len)
7681                                 return -EINVAL;
7682
7683                         if (params.beacon_csa.probe_resp[offset] !=
7684                             params.count)
7685                                 return -EINVAL;
7686                 }
7687         }
7688
7689 skip_beacons:
7690         err = nl80211_parse_chandef(rdev, info, &params.chandef);
7691         if (err)
7692                 return err;
7693
7694         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &params.chandef,
7695                                            wdev->iftype))
7696                 return -EINVAL;
7697
7698         err = cfg80211_chandef_dfs_required(wdev->wiphy,
7699                                             &params.chandef,
7700                                             wdev->iftype);
7701         if (err < 0)
7702                 return err;
7703
7704         if (err > 0) {
7705                 params.radar_required = true;
7706                 if (need_handle_dfs_flag &&
7707                     !nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS])) {
7708                         return -EINVAL;
7709                 }
7710         }
7711
7712         if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
7713                 params.block_tx = true;
7714
7715         wdev_lock(wdev);
7716         err = rdev_channel_switch(rdev, dev, &params);
7717         wdev_unlock(wdev);
7718
7719         return err;
7720 }
7721
7722 static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
7723                             u32 seq, int flags,
7724                             struct cfg80211_registered_device *rdev,
7725                             struct wireless_dev *wdev,
7726                             struct cfg80211_internal_bss *intbss)
7727 {
7728         struct cfg80211_bss *res = &intbss->pub;
7729         const struct cfg80211_bss_ies *ies;
7730         void *hdr;
7731         struct nlattr *bss;
7732
7733         ASSERT_WDEV_LOCK(wdev);
7734
7735         hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
7736                              NL80211_CMD_NEW_SCAN_RESULTS);
7737         if (!hdr)
7738                 return -1;
7739
7740         genl_dump_check_consistent(cb, hdr, &nl80211_fam);
7741
7742         if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
7743                 goto nla_put_failure;
7744         if (wdev->netdev &&
7745             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
7746                 goto nla_put_failure;
7747         if (nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
7748                               NL80211_ATTR_PAD))
7749                 goto nla_put_failure;
7750
7751         bss = nla_nest_start(msg, NL80211_ATTR_BSS);
7752         if (!bss)
7753                 goto nla_put_failure;
7754         if ((!is_zero_ether_addr(res->bssid) &&
7755              nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
7756                 goto nla_put_failure;
7757
7758         rcu_read_lock();
7759         /* indicate whether we have probe response data or not */
7760         if (rcu_access_pointer(res->proberesp_ies) &&
7761             nla_put_flag(msg, NL80211_BSS_PRESP_DATA))
7762                 goto fail_unlock_rcu;
7763
7764         /* this pointer prefers to be pointed to probe response data
7765          * but is always valid
7766          */
7767         ies = rcu_dereference(res->ies);
7768         if (ies) {
7769                 if (nla_put_u64_64bit(msg, NL80211_BSS_TSF, ies->tsf,
7770                                       NL80211_BSS_PAD))
7771                         goto fail_unlock_rcu;
7772                 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
7773                                         ies->len, ies->data))
7774                         goto fail_unlock_rcu;
7775         }
7776
7777         /* and this pointer is always (unless driver didn't know) beacon data */
7778         ies = rcu_dereference(res->beacon_ies);
7779         if (ies && ies->from_beacon) {
7780                 if (nla_put_u64_64bit(msg, NL80211_BSS_BEACON_TSF, ies->tsf,
7781                                       NL80211_BSS_PAD))
7782                         goto fail_unlock_rcu;
7783                 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
7784                                         ies->len, ies->data))
7785                         goto fail_unlock_rcu;
7786         }
7787         rcu_read_unlock();
7788
7789         if (res->beacon_interval &&
7790             nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
7791                 goto nla_put_failure;
7792         if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
7793             nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
7794             nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
7795             nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
7796                         jiffies_to_msecs(jiffies - intbss->ts)))
7797                 goto nla_put_failure;
7798
7799         if (intbss->parent_tsf &&
7800             (nla_put_u64_64bit(msg, NL80211_BSS_PARENT_TSF,
7801                                intbss->parent_tsf, NL80211_BSS_PAD) ||
7802              nla_put(msg, NL80211_BSS_PARENT_BSSID, ETH_ALEN,
7803                      intbss->parent_bssid)))
7804                 goto nla_put_failure;
7805
7806         if (intbss->ts_boottime &&
7807             nla_put_u64_64bit(msg, NL80211_BSS_LAST_SEEN_BOOTTIME,
7808                               intbss->ts_boottime, NL80211_BSS_PAD))
7809                 goto nla_put_failure;
7810
7811         switch (rdev->wiphy.signal_type) {
7812         case CFG80211_SIGNAL_TYPE_MBM:
7813                 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
7814                         goto nla_put_failure;
7815                 break;
7816         case CFG80211_SIGNAL_TYPE_UNSPEC:
7817                 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
7818                         goto nla_put_failure;
7819                 break;
7820         default:
7821                 break;
7822         }
7823
7824         switch (wdev->iftype) {
7825         case NL80211_IFTYPE_P2P_CLIENT:
7826         case NL80211_IFTYPE_STATION:
7827                 if (intbss == wdev->current_bss &&
7828                     nla_put_u32(msg, NL80211_BSS_STATUS,
7829                                 NL80211_BSS_STATUS_ASSOCIATED))
7830                         goto nla_put_failure;
7831                 break;
7832         case NL80211_IFTYPE_ADHOC:
7833                 if (intbss == wdev->current_bss &&
7834                     nla_put_u32(msg, NL80211_BSS_STATUS,
7835                                 NL80211_BSS_STATUS_IBSS_JOINED))
7836                         goto nla_put_failure;
7837                 break;
7838         default:
7839                 break;
7840         }
7841
7842         nla_nest_end(msg, bss);
7843
7844         genlmsg_end(msg, hdr);
7845         return 0;
7846
7847  fail_unlock_rcu:
7848         rcu_read_unlock();
7849  nla_put_failure:
7850         genlmsg_cancel(msg, hdr);
7851         return -EMSGSIZE;
7852 }
7853
7854 static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
7855 {
7856         struct cfg80211_registered_device *rdev;
7857         struct cfg80211_internal_bss *scan;
7858         struct wireless_dev *wdev;
7859         int start = cb->args[2], idx = 0;
7860         int err;
7861
7862         rtnl_lock();
7863         err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
7864         if (err) {
7865                 rtnl_unlock();
7866                 return err;
7867         }
7868
7869         wdev_lock(wdev);
7870         spin_lock_bh(&rdev->bss_lock);
7871         cfg80211_bss_expire(rdev);
7872
7873         cb->seq = rdev->bss_generation;
7874
7875         list_for_each_entry(scan, &rdev->bss_list, list) {
7876                 if (++idx <= start)
7877                         continue;
7878                 if (nl80211_send_bss(skb, cb,
7879                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
7880                                 rdev, wdev, scan) < 0) {
7881                         idx--;
7882                         break;
7883                 }
7884         }
7885
7886         spin_unlock_bh(&rdev->bss_lock);
7887         wdev_unlock(wdev);
7888
7889         cb->args[2] = idx;
7890         rtnl_unlock();
7891
7892         return skb->len;
7893 }
7894
7895 static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
7896                                int flags, struct net_device *dev,
7897                                bool allow_radio_stats,
7898                                struct survey_info *survey)
7899 {
7900         void *hdr;
7901         struct nlattr *infoattr;
7902
7903         /* skip radio stats if userspace didn't request them */
7904         if (!survey->channel && !allow_radio_stats)
7905                 return 0;
7906
7907         hdr = nl80211hdr_put(msg, portid, seq, flags,
7908                              NL80211_CMD_NEW_SURVEY_RESULTS);
7909         if (!hdr)
7910                 return -ENOMEM;
7911
7912         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
7913                 goto nla_put_failure;
7914
7915         infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
7916         if (!infoattr)
7917                 goto nla_put_failure;
7918
7919         if (survey->channel &&
7920             nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
7921                         survey->channel->center_freq))
7922                 goto nla_put_failure;
7923
7924         if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
7925             nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
7926                 goto nla_put_failure;
7927         if ((survey->filled & SURVEY_INFO_IN_USE) &&
7928             nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
7929                 goto nla_put_failure;
7930         if ((survey->filled & SURVEY_INFO_TIME) &&
7931             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME,
7932                         survey->time, NL80211_SURVEY_INFO_PAD))
7933                 goto nla_put_failure;
7934         if ((survey->filled & SURVEY_INFO_TIME_BUSY) &&
7935             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_BUSY,
7936                               survey->time_busy, NL80211_SURVEY_INFO_PAD))
7937                 goto nla_put_failure;
7938         if ((survey->filled & SURVEY_INFO_TIME_EXT_BUSY) &&
7939             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_EXT_BUSY,
7940                               survey->time_ext_busy, NL80211_SURVEY_INFO_PAD))
7941                 goto nla_put_failure;
7942         if ((survey->filled & SURVEY_INFO_TIME_RX) &&
7943             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_RX,
7944                               survey->time_rx, NL80211_SURVEY_INFO_PAD))
7945                 goto nla_put_failure;
7946         if ((survey->filled & SURVEY_INFO_TIME_TX) &&
7947             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_TX,
7948                               survey->time_tx, NL80211_SURVEY_INFO_PAD))
7949                 goto nla_put_failure;
7950         if ((survey->filled & SURVEY_INFO_TIME_SCAN) &&
7951             nla_put_u64_64bit(msg, NL80211_SURVEY_INFO_TIME_SCAN,
7952                               survey->time_scan, NL80211_SURVEY_INFO_PAD))
7953                 goto nla_put_failure;
7954
7955         nla_nest_end(msg, infoattr);
7956
7957         genlmsg_end(msg, hdr);
7958         return 0;
7959
7960  nla_put_failure:
7961         genlmsg_cancel(msg, hdr);
7962         return -EMSGSIZE;
7963 }
7964
7965 static int nl80211_dump_survey(struct sk_buff *skb, struct netlink_callback *cb)
7966 {
7967         struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
7968         struct survey_info survey;
7969         struct cfg80211_registered_device *rdev;
7970         struct wireless_dev *wdev;
7971         int survey_idx = cb->args[2];
7972         int res;
7973         bool radio_stats;
7974
7975         rtnl_lock();
7976         res = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
7977         if (res)
7978                 goto out_err;
7979
7980         /* prepare_wdev_dump parsed the attributes */
7981         radio_stats = attrbuf[NL80211_ATTR_SURVEY_RADIO_STATS];
7982
7983         if (!wdev->netdev) {
7984                 res = -EINVAL;
7985                 goto out_err;
7986         }
7987
7988         if (!rdev->ops->dump_survey) {
7989                 res = -EOPNOTSUPP;
7990                 goto out_err;
7991         }
7992
7993         while (1) {
7994                 res = rdev_dump_survey(rdev, wdev->netdev, survey_idx, &survey);
7995                 if (res == -ENOENT)
7996                         break;
7997                 if (res)
7998                         goto out_err;
7999
8000                 /* don't send disabled channels, but do send non-channel data */
8001                 if (survey.channel &&
8002                     survey.channel->flags & IEEE80211_CHAN_DISABLED) {
8003                         survey_idx++;
8004                         continue;
8005                 }
8006
8007                 if (nl80211_send_survey(skb,
8008                                 NETLINK_CB(cb->skb).portid,
8009                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
8010                                 wdev->netdev, radio_stats, &survey) < 0)
8011                         goto out;
8012                 survey_idx++;
8013         }
8014
8015  out:
8016         cb->args[2] = survey_idx;
8017         res = skb->len;
8018  out_err:
8019         rtnl_unlock();
8020         return res;
8021 }
8022
8023 static bool nl80211_valid_wpa_versions(u32 wpa_versions)
8024 {
8025         return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
8026                                   NL80211_WPA_VERSION_2));
8027 }
8028
8029 static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
8030 {
8031         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8032         struct net_device *dev = info->user_ptr[1];
8033         struct ieee80211_channel *chan;
8034         const u8 *bssid, *ssid, *ie = NULL, *auth_data = NULL;
8035         int err, ssid_len, ie_len = 0, auth_data_len = 0;
8036         enum nl80211_auth_type auth_type;
8037         struct key_parse key;
8038         bool local_state_change;
8039
8040         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8041                 return -EINVAL;
8042
8043         if (!info->attrs[NL80211_ATTR_MAC])
8044                 return -EINVAL;
8045
8046         if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
8047                 return -EINVAL;
8048
8049         if (!info->attrs[NL80211_ATTR_SSID])
8050                 return -EINVAL;
8051
8052         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
8053                 return -EINVAL;
8054
8055         err = nl80211_parse_key(info, &key);
8056         if (err)
8057                 return err;
8058
8059         if (key.idx >= 0) {
8060                 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
8061                         return -EINVAL;
8062                 if (!key.p.key || !key.p.key_len)
8063                         return -EINVAL;
8064                 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
8065                      key.p.key_len != WLAN_KEY_LEN_WEP40) &&
8066                     (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
8067                      key.p.key_len != WLAN_KEY_LEN_WEP104))
8068                         return -EINVAL;
8069                 if (key.idx > 3)
8070                         return -EINVAL;
8071         } else {
8072                 key.p.key_len = 0;
8073                 key.p.key = NULL;
8074         }
8075
8076         if (key.idx >= 0) {
8077                 int i;
8078                 bool ok = false;
8079
8080                 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
8081                         if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
8082                                 ok = true;
8083                                 break;
8084                         }
8085                 }
8086                 if (!ok)
8087                         return -EINVAL;
8088         }
8089
8090         if (!rdev->ops->auth)
8091                 return -EOPNOTSUPP;
8092
8093         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8094             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8095                 return -EOPNOTSUPP;
8096
8097         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8098         chan = nl80211_get_valid_chan(&rdev->wiphy,
8099                                       info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8100         if (!chan)
8101                 return -EINVAL;
8102
8103         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8104         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8105
8106         if (info->attrs[NL80211_ATTR_IE]) {
8107                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8108                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8109         }
8110
8111         auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8112         if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
8113                 return -EINVAL;
8114
8115         if ((auth_type == NL80211_AUTHTYPE_SAE ||
8116              auth_type == NL80211_AUTHTYPE_FILS_SK ||
8117              auth_type == NL80211_AUTHTYPE_FILS_SK_PFS ||
8118              auth_type == NL80211_AUTHTYPE_FILS_PK) &&
8119             !info->attrs[NL80211_ATTR_AUTH_DATA])
8120                 return -EINVAL;
8121
8122         if (info->attrs[NL80211_ATTR_AUTH_DATA]) {
8123                 if (auth_type != NL80211_AUTHTYPE_SAE &&
8124                     auth_type != NL80211_AUTHTYPE_FILS_SK &&
8125                     auth_type != NL80211_AUTHTYPE_FILS_SK_PFS &&
8126                     auth_type != NL80211_AUTHTYPE_FILS_PK)
8127                         return -EINVAL;
8128                 auth_data = nla_data(info->attrs[NL80211_ATTR_AUTH_DATA]);
8129                 auth_data_len = nla_len(info->attrs[NL80211_ATTR_AUTH_DATA]);
8130                 /* need to include at least Auth Transaction and Status Code */
8131                 if (auth_data_len < 4)
8132                         return -EINVAL;
8133         }
8134
8135         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8136
8137         /*
8138          * Since we no longer track auth state, ignore
8139          * requests to only change local state.
8140          */
8141         if (local_state_change)
8142                 return 0;
8143
8144         wdev_lock(dev->ieee80211_ptr);
8145         err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
8146                                  ssid, ssid_len, ie, ie_len,
8147                                  key.p.key, key.p.key_len, key.idx,
8148                                  auth_data, auth_data_len);
8149         wdev_unlock(dev->ieee80211_ptr);
8150         return err;
8151 }
8152
8153 static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
8154                                    struct genl_info *info,
8155                                    struct cfg80211_crypto_settings *settings,
8156                                    int cipher_limit)
8157 {
8158         memset(settings, 0, sizeof(*settings));
8159
8160         settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
8161
8162         if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
8163                 u16 proto;
8164
8165                 proto = nla_get_u16(
8166                         info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
8167                 settings->control_port_ethertype = cpu_to_be16(proto);
8168                 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
8169                     proto != ETH_P_PAE)
8170                         return -EINVAL;
8171                 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
8172                         settings->control_port_no_encrypt = true;
8173         } else
8174                 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
8175
8176         if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
8177                 void *data;
8178                 int len, i;
8179
8180                 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8181                 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
8182                 settings->n_ciphers_pairwise = len / sizeof(u32);
8183
8184                 if (len % sizeof(u32))
8185                         return -EINVAL;
8186
8187                 if (settings->n_ciphers_pairwise > cipher_limit)
8188                         return -EINVAL;
8189
8190                 memcpy(settings->ciphers_pairwise, data, len);
8191
8192                 for (i = 0; i < settings->n_ciphers_pairwise; i++)
8193                         if (!cfg80211_supported_cipher_suite(
8194                                         &rdev->wiphy,
8195                                         settings->ciphers_pairwise[i]))
8196                                 return -EINVAL;
8197         }
8198
8199         if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
8200                 settings->cipher_group =
8201                         nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
8202                 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
8203                                                      settings->cipher_group))
8204                         return -EINVAL;
8205         }
8206
8207         if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
8208                 settings->wpa_versions =
8209                         nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
8210                 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
8211                         return -EINVAL;
8212         }
8213
8214         if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
8215                 void *data;
8216                 int len;
8217
8218                 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
8219                 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
8220                 settings->n_akm_suites = len / sizeof(u32);
8221
8222                 if (len % sizeof(u32))
8223                         return -EINVAL;
8224
8225                 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
8226                         return -EINVAL;
8227
8228                 memcpy(settings->akm_suites, data, len);
8229         }
8230
8231         if (info->attrs[NL80211_ATTR_PMK]) {
8232                 if (nla_len(info->attrs[NL80211_ATTR_PMK]) != WLAN_PMK_LEN)
8233                         return -EINVAL;
8234                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
8235                                              NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK))
8236                         return -EINVAL;
8237                 settings->psk = nla_data(info->attrs[NL80211_ATTR_PMK]);
8238         }
8239
8240         return 0;
8241 }
8242
8243 static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
8244 {
8245         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8246         struct net_device *dev = info->user_ptr[1];
8247         struct ieee80211_channel *chan;
8248         struct cfg80211_assoc_request req = {};
8249         const u8 *bssid, *ssid;
8250         int err, ssid_len = 0;
8251
8252         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8253                 return -EINVAL;
8254
8255         if (!info->attrs[NL80211_ATTR_MAC] ||
8256             !info->attrs[NL80211_ATTR_SSID] ||
8257             !info->attrs[NL80211_ATTR_WIPHY_FREQ])
8258                 return -EINVAL;
8259
8260         if (!rdev->ops->assoc)
8261                 return -EOPNOTSUPP;
8262
8263         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8264             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8265                 return -EOPNOTSUPP;
8266
8267         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8268
8269         chan = nl80211_get_valid_chan(&rdev->wiphy,
8270                                       info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8271         if (!chan)
8272                 return -EINVAL;
8273
8274         ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8275         ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8276
8277         if (info->attrs[NL80211_ATTR_IE]) {
8278                 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8279                 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8280         }
8281
8282         if (info->attrs[NL80211_ATTR_USE_MFP]) {
8283                 enum nl80211_mfp mfp =
8284                         nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8285                 if (mfp == NL80211_MFP_REQUIRED)
8286                         req.use_mfp = true;
8287                 else if (mfp != NL80211_MFP_NO)
8288                         return -EINVAL;
8289         }
8290
8291         if (info->attrs[NL80211_ATTR_PREV_BSSID])
8292                 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8293
8294         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
8295                 req.flags |= ASSOC_REQ_DISABLE_HT;
8296
8297         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8298                 memcpy(&req.ht_capa_mask,
8299                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8300                        sizeof(req.ht_capa_mask));
8301
8302         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8303                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8304                         return -EINVAL;
8305                 memcpy(&req.ht_capa,
8306                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8307                        sizeof(req.ht_capa));
8308         }
8309
8310         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
8311                 req.flags |= ASSOC_REQ_DISABLE_VHT;
8312
8313         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8314                 memcpy(&req.vht_capa_mask,
8315                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
8316                        sizeof(req.vht_capa_mask));
8317
8318         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
8319                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
8320                         return -EINVAL;
8321                 memcpy(&req.vht_capa,
8322                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
8323                        sizeof(req.vht_capa));
8324         }
8325
8326         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
8327                 if (!((rdev->wiphy.features &
8328                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
8329                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
8330                     !wiphy_ext_feature_isset(&rdev->wiphy,
8331                                              NL80211_EXT_FEATURE_RRM))
8332                         return -EINVAL;
8333                 req.flags |= ASSOC_REQ_USE_RRM;
8334         }
8335
8336         if (info->attrs[NL80211_ATTR_FILS_KEK]) {
8337                 req.fils_kek = nla_data(info->attrs[NL80211_ATTR_FILS_KEK]);
8338                 req.fils_kek_len = nla_len(info->attrs[NL80211_ATTR_FILS_KEK]);
8339                 if (!info->attrs[NL80211_ATTR_FILS_NONCES])
8340                         return -EINVAL;
8341                 req.fils_nonces =
8342                         nla_data(info->attrs[NL80211_ATTR_FILS_NONCES]);
8343         }
8344
8345         err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
8346         if (!err) {
8347                 wdev_lock(dev->ieee80211_ptr);
8348
8349                 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
8350                                           ssid, ssid_len, &req);
8351
8352                 if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
8353                         dev->ieee80211_ptr->conn_owner_nlportid =
8354                                 info->snd_portid;
8355                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
8356                                bssid, ETH_ALEN);
8357                 }
8358
8359                 wdev_unlock(dev->ieee80211_ptr);
8360         }
8361
8362         return err;
8363 }
8364
8365 static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
8366 {
8367         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8368         struct net_device *dev = info->user_ptr[1];
8369         const u8 *ie = NULL, *bssid;
8370         int ie_len = 0, err;
8371         u16 reason_code;
8372         bool local_state_change;
8373
8374         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8375                 return -EINVAL;
8376
8377         if (!info->attrs[NL80211_ATTR_MAC])
8378                 return -EINVAL;
8379
8380         if (!info->attrs[NL80211_ATTR_REASON_CODE])
8381                 return -EINVAL;
8382
8383         if (!rdev->ops->deauth)
8384                 return -EOPNOTSUPP;
8385
8386         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8387             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8388                 return -EOPNOTSUPP;
8389
8390         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8391
8392         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8393         if (reason_code == 0) {
8394                 /* Reason Code 0 is reserved */
8395                 return -EINVAL;
8396         }
8397
8398         if (info->attrs[NL80211_ATTR_IE]) {
8399                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8400                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8401         }
8402
8403         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8404
8405         wdev_lock(dev->ieee80211_ptr);
8406         err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
8407                                    local_state_change);
8408         wdev_unlock(dev->ieee80211_ptr);
8409         return err;
8410 }
8411
8412 static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
8413 {
8414         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8415         struct net_device *dev = info->user_ptr[1];
8416         const u8 *ie = NULL, *bssid;
8417         int ie_len = 0, err;
8418         u16 reason_code;
8419         bool local_state_change;
8420
8421         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8422                 return -EINVAL;
8423
8424         if (!info->attrs[NL80211_ATTR_MAC])
8425                 return -EINVAL;
8426
8427         if (!info->attrs[NL80211_ATTR_REASON_CODE])
8428                 return -EINVAL;
8429
8430         if (!rdev->ops->disassoc)
8431                 return -EOPNOTSUPP;
8432
8433         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8434             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8435                 return -EOPNOTSUPP;
8436
8437         bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8438
8439         reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
8440         if (reason_code == 0) {
8441                 /* Reason Code 0 is reserved */
8442                 return -EINVAL;
8443         }
8444
8445         if (info->attrs[NL80211_ATTR_IE]) {
8446                 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8447                 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8448         }
8449
8450         local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
8451
8452         wdev_lock(dev->ieee80211_ptr);
8453         err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
8454                                      local_state_change);
8455         wdev_unlock(dev->ieee80211_ptr);
8456         return err;
8457 }
8458
8459 static bool
8460 nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
8461                          int mcast_rate[NUM_NL80211_BANDS],
8462                          int rateval)
8463 {
8464         struct wiphy *wiphy = &rdev->wiphy;
8465         bool found = false;
8466         int band, i;
8467
8468         for (band = 0; band < NUM_NL80211_BANDS; band++) {
8469                 struct ieee80211_supported_band *sband;
8470
8471                 sband = wiphy->bands[band];
8472                 if (!sband)
8473                         continue;
8474
8475                 for (i = 0; i < sband->n_bitrates; i++) {
8476                         if (sband->bitrates[i].bitrate == rateval) {
8477                                 mcast_rate[band] = i + 1;
8478                                 found = true;
8479                                 break;
8480                         }
8481                 }
8482         }
8483
8484         return found;
8485 }
8486
8487 static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
8488 {
8489         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8490         struct net_device *dev = info->user_ptr[1];
8491         struct cfg80211_ibss_params ibss;
8492         struct wiphy *wiphy;
8493         struct cfg80211_cached_keys *connkeys = NULL;
8494         int err;
8495
8496         memset(&ibss, 0, sizeof(ibss));
8497
8498         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8499                 return -EINVAL;
8500
8501         if (!info->attrs[NL80211_ATTR_SSID] ||
8502             !nla_len(info->attrs[NL80211_ATTR_SSID]))
8503                 return -EINVAL;
8504
8505         ibss.beacon_interval = 100;
8506
8507         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL])
8508                 ibss.beacon_interval =
8509                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
8510
8511         err = cfg80211_validate_beacon_int(rdev, NL80211_IFTYPE_ADHOC,
8512                                            ibss.beacon_interval);
8513         if (err)
8514                 return err;
8515
8516         if (!rdev->ops->join_ibss)
8517                 return -EOPNOTSUPP;
8518
8519         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8520                 return -EOPNOTSUPP;
8521
8522         wiphy = &rdev->wiphy;
8523
8524         if (info->attrs[NL80211_ATTR_MAC]) {
8525                 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8526
8527                 if (!is_valid_ether_addr(ibss.bssid))
8528                         return -EINVAL;
8529         }
8530         ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8531         ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8532
8533         if (info->attrs[NL80211_ATTR_IE]) {
8534                 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8535                 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8536         }
8537
8538         err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
8539         if (err)
8540                 return err;
8541
8542         if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef,
8543                                      NL80211_IFTYPE_ADHOC))
8544                 return -EINVAL;
8545
8546         switch (ibss.chandef.width) {
8547         case NL80211_CHAN_WIDTH_5:
8548         case NL80211_CHAN_WIDTH_10:
8549         case NL80211_CHAN_WIDTH_20_NOHT:
8550                 break;
8551         case NL80211_CHAN_WIDTH_20:
8552         case NL80211_CHAN_WIDTH_40:
8553                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8554                         return -EINVAL;
8555                 break;
8556         case NL80211_CHAN_WIDTH_80:
8557         case NL80211_CHAN_WIDTH_80P80:
8558         case NL80211_CHAN_WIDTH_160:
8559                 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
8560                         return -EINVAL;
8561                 if (!wiphy_ext_feature_isset(&rdev->wiphy,
8562                                              NL80211_EXT_FEATURE_VHT_IBSS))
8563                         return -EINVAL;
8564                 break;
8565         default:
8566                 return -EINVAL;
8567         }
8568
8569         ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
8570         ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
8571
8572         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
8573                 u8 *rates =
8574                         nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8575                 int n_rates =
8576                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
8577                 struct ieee80211_supported_band *sband =
8578                         wiphy->bands[ibss.chandef.chan->band];
8579
8580                 err = ieee80211_get_ratemask(sband, rates, n_rates,
8581                                              &ibss.basic_rates);
8582                 if (err)
8583                         return err;
8584         }
8585
8586         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8587                 memcpy(&ibss.ht_capa_mask,
8588                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
8589                        sizeof(ibss.ht_capa_mask));
8590
8591         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
8592                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
8593                         return -EINVAL;
8594                 memcpy(&ibss.ht_capa,
8595                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
8596                        sizeof(ibss.ht_capa));
8597         }
8598
8599         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
8600             !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
8601                         nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
8602                 return -EINVAL;
8603
8604         if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
8605                 bool no_ht = false;
8606
8607                 connkeys = nl80211_parse_connkeys(rdev,
8608                                           info->attrs[NL80211_ATTR_KEYS],
8609                                           &no_ht);
8610                 if (IS_ERR(connkeys))
8611                         return PTR_ERR(connkeys);
8612
8613                 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
8614                     no_ht) {
8615                         kzfree(connkeys);
8616                         return -EINVAL;
8617                 }
8618         }
8619
8620         ibss.control_port =
8621                 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
8622
8623         ibss.userspace_handles_dfs =
8624                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
8625
8626         err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
8627         if (err)
8628                 kzfree(connkeys);
8629         return err;
8630 }
8631
8632 static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
8633 {
8634         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8635         struct net_device *dev = info->user_ptr[1];
8636
8637         if (!rdev->ops->leave_ibss)
8638                 return -EOPNOTSUPP;
8639
8640         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
8641                 return -EOPNOTSUPP;
8642
8643         return cfg80211_leave_ibss(rdev, dev, false);
8644 }
8645
8646 static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
8647 {
8648         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8649         struct net_device *dev = info->user_ptr[1];
8650         int mcast_rate[NUM_NL80211_BANDS];
8651         u32 nla_rate;
8652         int err;
8653
8654         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
8655             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
8656             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_OCB)
8657                 return -EOPNOTSUPP;
8658
8659         if (!rdev->ops->set_mcast_rate)
8660                 return -EOPNOTSUPP;
8661
8662         memset(mcast_rate, 0, sizeof(mcast_rate));
8663
8664         if (!info->attrs[NL80211_ATTR_MCAST_RATE])
8665                 return -EINVAL;
8666
8667         nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
8668         if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
8669                 return -EINVAL;
8670
8671         err = rdev_set_mcast_rate(rdev, dev, mcast_rate);
8672
8673         return err;
8674 }
8675
8676 static struct sk_buff *
8677 __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
8678                             struct wireless_dev *wdev, int approxlen,
8679                             u32 portid, u32 seq, enum nl80211_commands cmd,
8680                             enum nl80211_attrs attr,
8681                             const struct nl80211_vendor_cmd_info *info,
8682                             gfp_t gfp)
8683 {
8684         struct sk_buff *skb;
8685         void *hdr;
8686         struct nlattr *data;
8687
8688         skb = nlmsg_new(approxlen + 100, gfp);
8689         if (!skb)
8690                 return NULL;
8691
8692         hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
8693         if (!hdr) {
8694                 kfree_skb(skb);
8695                 return NULL;
8696         }
8697
8698         if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
8699                 goto nla_put_failure;
8700
8701         if (info) {
8702                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_ID,
8703                                 info->vendor_id))
8704                         goto nla_put_failure;
8705                 if (nla_put_u32(skb, NL80211_ATTR_VENDOR_SUBCMD,
8706                                 info->subcmd))
8707                         goto nla_put_failure;
8708         }
8709
8710         if (wdev) {
8711                 if (nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
8712                                       wdev_id(wdev), NL80211_ATTR_PAD))
8713                         goto nla_put_failure;
8714                 if (wdev->netdev &&
8715                     nla_put_u32(skb, NL80211_ATTR_IFINDEX,
8716                                 wdev->netdev->ifindex))
8717                         goto nla_put_failure;
8718         }
8719
8720         data = nla_nest_start(skb, attr);
8721         if (!data)
8722                 goto nla_put_failure;
8723
8724         ((void **)skb->cb)[0] = rdev;
8725         ((void **)skb->cb)[1] = hdr;
8726         ((void **)skb->cb)[2] = data;
8727
8728         return skb;
8729
8730  nla_put_failure:
8731         kfree_skb(skb);
8732         return NULL;
8733 }
8734
8735 struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
8736                                            struct wireless_dev *wdev,
8737                                            enum nl80211_commands cmd,
8738                                            enum nl80211_attrs attr,
8739                                            int vendor_event_idx,
8740                                            int approxlen, gfp_t gfp)
8741 {
8742         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
8743         const struct nl80211_vendor_cmd_info *info;
8744
8745         switch (cmd) {
8746         case NL80211_CMD_TESTMODE:
8747                 if (WARN_ON(vendor_event_idx != -1))
8748                         return NULL;
8749                 info = NULL;
8750                 break;
8751         case NL80211_CMD_VENDOR:
8752                 if (WARN_ON(vendor_event_idx < 0 ||
8753                             vendor_event_idx >= wiphy->n_vendor_events))
8754                         return NULL;
8755                 info = &wiphy->vendor_events[vendor_event_idx];
8756                 break;
8757         default:
8758                 WARN_ON(1);
8759                 return NULL;
8760         }
8761
8762         return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
8763                                            cmd, attr, info, gfp);
8764 }
8765 EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
8766
8767 void __cfg80211_send_event_skb(struct sk_buff *skb, gfp_t gfp)
8768 {
8769         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8770         void *hdr = ((void **)skb->cb)[1];
8771         struct nlattr *data = ((void **)skb->cb)[2];
8772         enum nl80211_multicast_groups mcgrp = NL80211_MCGRP_TESTMODE;
8773
8774         /* clear CB data for netlink core to own from now on */
8775         memset(skb->cb, 0, sizeof(skb->cb));
8776
8777         nla_nest_end(skb, data);
8778         genlmsg_end(skb, hdr);
8779
8780         if (data->nla_type == NL80211_ATTR_VENDOR_DATA)
8781                 mcgrp = NL80211_MCGRP_VENDOR;
8782
8783         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
8784                                 mcgrp, gfp);
8785 }
8786 EXPORT_SYMBOL(__cfg80211_send_event_skb);
8787
8788 #ifdef CONFIG_NL80211_TESTMODE
8789 static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
8790 {
8791         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8792         struct wireless_dev *wdev =
8793                 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8794         int err;
8795
8796         if (!rdev->ops->testmode_cmd)
8797                 return -EOPNOTSUPP;
8798
8799         if (IS_ERR(wdev)) {
8800                 err = PTR_ERR(wdev);
8801                 if (err != -EINVAL)
8802                         return err;
8803                 wdev = NULL;
8804         } else if (wdev->wiphy != &rdev->wiphy) {
8805                 return -EINVAL;
8806         }
8807
8808         if (!info->attrs[NL80211_ATTR_TESTDATA])
8809                 return -EINVAL;
8810
8811         rdev->cur_cmd_info = info;
8812         err = rdev_testmode_cmd(rdev, wdev,
8813                                 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
8814                                 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
8815         rdev->cur_cmd_info = NULL;
8816
8817         return err;
8818 }
8819
8820 static int nl80211_testmode_dump(struct sk_buff *skb,
8821                                  struct netlink_callback *cb)
8822 {
8823         struct cfg80211_registered_device *rdev;
8824         int err;
8825         long phy_idx;
8826         void *data = NULL;
8827         int data_len = 0;
8828
8829         rtnl_lock();
8830
8831         if (cb->args[0]) {
8832                 /*
8833                  * 0 is a valid index, but not valid for args[0],
8834                  * so we need to offset by 1.
8835                  */
8836                 phy_idx = cb->args[0] - 1;
8837
8838                 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
8839                 if (!rdev) {
8840                         err = -ENOENT;
8841                         goto out_err;
8842                 }
8843         } else {
8844                 struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
8845
8846                 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
8847                                   attrbuf, nl80211_fam.maxattr,
8848                                   nl80211_policy, NULL);
8849                 if (err)
8850                         goto out_err;
8851
8852                 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
8853                 if (IS_ERR(rdev)) {
8854                         err = PTR_ERR(rdev);
8855                         goto out_err;
8856                 }
8857                 phy_idx = rdev->wiphy_idx;
8858
8859                 if (attrbuf[NL80211_ATTR_TESTDATA])
8860                         cb->args[1] = (long)attrbuf[NL80211_ATTR_TESTDATA];
8861         }
8862
8863         if (cb->args[1]) {
8864                 data = nla_data((void *)cb->args[1]);
8865                 data_len = nla_len((void *)cb->args[1]);
8866         }
8867
8868         if (!rdev->ops->testmode_dump) {
8869                 err = -EOPNOTSUPP;
8870                 goto out_err;
8871         }
8872
8873         while (1) {
8874                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
8875                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
8876                                            NL80211_CMD_TESTMODE);
8877                 struct nlattr *tmdata;
8878
8879                 if (!hdr)
8880                         break;
8881
8882                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
8883                         genlmsg_cancel(skb, hdr);
8884                         break;
8885                 }
8886
8887                 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
8888                 if (!tmdata) {
8889                         genlmsg_cancel(skb, hdr);
8890                         break;
8891                 }
8892                 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
8893                 nla_nest_end(skb, tmdata);
8894
8895                 if (err == -ENOBUFS || err == -ENOENT) {
8896                         genlmsg_cancel(skb, hdr);
8897                         break;
8898                 } else if (err) {
8899                         genlmsg_cancel(skb, hdr);
8900                         goto out_err;
8901                 }
8902
8903                 genlmsg_end(skb, hdr);
8904         }
8905
8906         err = skb->len;
8907         /* see above */
8908         cb->args[0] = phy_idx + 1;
8909  out_err:
8910         rtnl_unlock();
8911         return err;
8912 }
8913 #endif
8914
8915 static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
8916 {
8917         struct cfg80211_registered_device *rdev = info->user_ptr[0];
8918         struct net_device *dev = info->user_ptr[1];
8919         struct cfg80211_connect_params connect;
8920         struct wiphy *wiphy;
8921         struct cfg80211_cached_keys *connkeys = NULL;
8922         int err;
8923
8924         memset(&connect, 0, sizeof(connect));
8925
8926         if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8927                 return -EINVAL;
8928
8929         if (!info->attrs[NL80211_ATTR_SSID] ||
8930             !nla_len(info->attrs[NL80211_ATTR_SSID]))
8931                 return -EINVAL;
8932
8933         if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
8934                 connect.auth_type =
8935                         nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
8936                 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
8937                                              NL80211_CMD_CONNECT))
8938                         return -EINVAL;
8939         } else
8940                 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
8941
8942         connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
8943
8944         if (info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS] &&
8945             !wiphy_ext_feature_isset(&rdev->wiphy,
8946                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
8947                 return -EINVAL;
8948         connect.want_1x = info->attrs[NL80211_ATTR_WANT_1X_4WAY_HS];
8949
8950         err = nl80211_crypto_settings(rdev, info, &connect.crypto,
8951                                       NL80211_MAX_NR_CIPHER_SUITES);
8952         if (err)
8953                 return err;
8954
8955         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
8956             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
8957                 return -EOPNOTSUPP;
8958
8959         wiphy = &rdev->wiphy;
8960
8961         connect.bg_scan_period = -1;
8962         if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
8963                 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
8964                 connect.bg_scan_period =
8965                         nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
8966         }
8967
8968         if (info->attrs[NL80211_ATTR_MAC])
8969                 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
8970         else if (info->attrs[NL80211_ATTR_MAC_HINT])
8971                 connect.bssid_hint =
8972                         nla_data(info->attrs[NL80211_ATTR_MAC_HINT]);
8973         connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
8974         connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
8975
8976         if (info->attrs[NL80211_ATTR_IE]) {
8977                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8978                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8979         }
8980
8981         if (info->attrs[NL80211_ATTR_USE_MFP]) {
8982                 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
8983                 if (connect.mfp != NL80211_MFP_REQUIRED &&
8984                     connect.mfp != NL80211_MFP_NO)
8985                         return -EINVAL;
8986         } else {
8987                 connect.mfp = NL80211_MFP_NO;
8988         }
8989
8990         if (info->attrs[NL80211_ATTR_PREV_BSSID])
8991                 connect.prev_bssid =
8992                         nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
8993
8994         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
8995                 connect.channel = nl80211_get_valid_chan(
8996                         wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ]);
8997                 if (!connect.channel)
8998                         return -EINVAL;
8999         } else if (info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]) {
9000                 connect.channel_hint = nl80211_get_valid_chan(
9001                         wiphy, info->attrs[NL80211_ATTR_WIPHY_FREQ_HINT]);
9002                 if (!connect.channel_hint)
9003                         return -EINVAL;
9004         }
9005
9006         if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
9007                 connkeys = nl80211_parse_connkeys(rdev,
9008                                           info->attrs[NL80211_ATTR_KEYS], NULL);
9009                 if (IS_ERR(connkeys))
9010                         return PTR_ERR(connkeys);
9011         }
9012
9013         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
9014                 connect.flags |= ASSOC_REQ_DISABLE_HT;
9015
9016         if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
9017                 memcpy(&connect.ht_capa_mask,
9018                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
9019                        sizeof(connect.ht_capa_mask));
9020
9021         if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
9022                 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
9023                         kzfree(connkeys);
9024                         return -EINVAL;
9025                 }
9026                 memcpy(&connect.ht_capa,
9027                        nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
9028                        sizeof(connect.ht_capa));
9029         }
9030
9031         if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
9032                 connect.flags |= ASSOC_REQ_DISABLE_VHT;
9033
9034         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
9035                 memcpy(&connect.vht_capa_mask,
9036                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
9037                        sizeof(connect.vht_capa_mask));
9038
9039         if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
9040                 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
9041                         kzfree(connkeys);
9042                         return -EINVAL;
9043                 }
9044                 memcpy(&connect.vht_capa,
9045                        nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
9046                        sizeof(connect.vht_capa));
9047         }
9048
9049         if (nla_get_flag(info->attrs[NL80211_ATTR_USE_RRM])) {
9050                 if (!((rdev->wiphy.features &
9051                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) &&
9052                        (rdev->wiphy.features & NL80211_FEATURE_QUIET)) &&
9053                     !wiphy_ext_feature_isset(&rdev->wiphy,
9054                                              NL80211_EXT_FEATURE_RRM)) {
9055                         kzfree(connkeys);
9056                         return -EINVAL;
9057                 }
9058                 connect.flags |= ASSOC_REQ_USE_RRM;
9059         }
9060
9061         connect.pbss = nla_get_flag(info->attrs[NL80211_ATTR_PBSS]);
9062         if (connect.pbss && !rdev->wiphy.bands[NL80211_BAND_60GHZ]) {
9063                 kzfree(connkeys);
9064                 return -EOPNOTSUPP;
9065         }
9066
9067         if (info->attrs[NL80211_ATTR_BSS_SELECT]) {
9068                 /* bss selection makes no sense if bssid is set */
9069                 if (connect.bssid) {
9070                         kzfree(connkeys);
9071                         return -EINVAL;
9072                 }
9073
9074                 err = parse_bss_select(info->attrs[NL80211_ATTR_BSS_SELECT],
9075                                        wiphy, &connect.bss_select);
9076                 if (err) {
9077                         kzfree(connkeys);
9078                         return err;
9079                 }
9080         }
9081
9082         if (wiphy_ext_feature_isset(&rdev->wiphy,
9083                                     NL80211_EXT_FEATURE_FILS_SK_OFFLOAD) &&
9084             info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] &&
9085             info->attrs[NL80211_ATTR_FILS_ERP_REALM] &&
9086             info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] &&
9087             info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9088                 connect.fils_erp_username =
9089                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9090                 connect.fils_erp_username_len =
9091                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_USERNAME]);
9092                 connect.fils_erp_realm =
9093                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9094                 connect.fils_erp_realm_len =
9095                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_REALM]);
9096                 connect.fils_erp_next_seq_num =
9097                         nla_get_u16(
9098                            info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM]);
9099                 connect.fils_erp_rrk =
9100                         nla_data(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9101                 connect.fils_erp_rrk_len =
9102                         nla_len(info->attrs[NL80211_ATTR_FILS_ERP_RRK]);
9103         } else if (info->attrs[NL80211_ATTR_FILS_ERP_USERNAME] ||
9104                    info->attrs[NL80211_ATTR_FILS_ERP_REALM] ||
9105                    info->attrs[NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM] ||
9106                    info->attrs[NL80211_ATTR_FILS_ERP_RRK]) {
9107                 kzfree(connkeys);
9108                 return -EINVAL;
9109         }
9110
9111         wdev_lock(dev->ieee80211_ptr);
9112
9113         err = cfg80211_connect(rdev, dev, &connect, connkeys,
9114                                connect.prev_bssid);
9115         if (err)
9116                 kzfree(connkeys);
9117
9118         if (!err && info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
9119                 dev->ieee80211_ptr->conn_owner_nlportid = info->snd_portid;
9120                 if (connect.bssid)
9121                         memcpy(dev->ieee80211_ptr->disconnect_bssid,
9122                                connect.bssid, ETH_ALEN);
9123                 else
9124                         memset(dev->ieee80211_ptr->disconnect_bssid,
9125                                0, ETH_ALEN);
9126         }
9127
9128         wdev_unlock(dev->ieee80211_ptr);
9129
9130         return err;
9131 }
9132
9133 static int nl80211_update_connect_params(struct sk_buff *skb,
9134                                          struct genl_info *info)
9135 {
9136         struct cfg80211_connect_params connect = {};
9137         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9138         struct net_device *dev = info->user_ptr[1];
9139         struct wireless_dev *wdev = dev->ieee80211_ptr;
9140         u32 changed = 0;
9141         int ret;
9142
9143         if (!rdev->ops->update_connect_params)
9144                 return -EOPNOTSUPP;
9145
9146         if (info->attrs[NL80211_ATTR_IE]) {
9147                 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
9148                         return -EINVAL;
9149                 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
9150                 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
9151                 changed |= UPDATE_ASSOC_IES;
9152         }
9153
9154         wdev_lock(dev->ieee80211_ptr);
9155         if (!wdev->current_bss)
9156                 ret = -ENOLINK;
9157         else
9158                 ret = rdev_update_connect_params(rdev, dev, &connect, changed);
9159         wdev_unlock(dev->ieee80211_ptr);
9160
9161         return ret;
9162 }
9163
9164 static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
9165 {
9166         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9167         struct net_device *dev = info->user_ptr[1];
9168         u16 reason;
9169         int ret;
9170
9171         if (!info->attrs[NL80211_ATTR_REASON_CODE])
9172                 reason = WLAN_REASON_DEAUTH_LEAVING;
9173         else
9174                 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
9175
9176         if (reason == 0)
9177                 return -EINVAL;
9178
9179         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9180             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9181                 return -EOPNOTSUPP;
9182
9183         wdev_lock(dev->ieee80211_ptr);
9184         ret = cfg80211_disconnect(rdev, dev, reason, true);
9185         wdev_unlock(dev->ieee80211_ptr);
9186         return ret;
9187 }
9188
9189 static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
9190 {
9191         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9192         struct net *net;
9193         int err;
9194
9195         if (info->attrs[NL80211_ATTR_PID]) {
9196                 u32 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
9197
9198                 net = get_net_ns_by_pid(pid);
9199         } else if (info->attrs[NL80211_ATTR_NETNS_FD]) {
9200                 u32 fd = nla_get_u32(info->attrs[NL80211_ATTR_NETNS_FD]);
9201
9202                 net = get_net_ns_by_fd(fd);
9203         } else {
9204                 return -EINVAL;
9205         }
9206
9207         if (IS_ERR(net))
9208                 return PTR_ERR(net);
9209
9210         err = 0;
9211
9212         /* check if anything to do */
9213         if (!net_eq(wiphy_net(&rdev->wiphy), net))
9214                 err = cfg80211_switch_netns(rdev, net);
9215
9216         put_net(net);
9217         return err;
9218 }
9219
9220 static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
9221 {
9222         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9223         int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
9224                         struct cfg80211_pmksa *pmksa) = NULL;
9225         struct net_device *dev = info->user_ptr[1];
9226         struct cfg80211_pmksa pmksa;
9227
9228         memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
9229
9230         if (!info->attrs[NL80211_ATTR_PMKID])
9231                 return -EINVAL;
9232
9233         pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
9234
9235         if (info->attrs[NL80211_ATTR_MAC]) {
9236                 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
9237         } else if (info->attrs[NL80211_ATTR_SSID] &&
9238                    info->attrs[NL80211_ATTR_FILS_CACHE_ID] &&
9239                    (info->genlhdr->cmd == NL80211_CMD_DEL_PMKSA ||
9240                     info->attrs[NL80211_ATTR_PMK])) {
9241                 pmksa.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
9242                 pmksa.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
9243                 pmksa.cache_id =
9244                         nla_data(info->attrs[NL80211_ATTR_FILS_CACHE_ID]);
9245         } else {
9246                 return -EINVAL;
9247         }
9248         if (info->attrs[NL80211_ATTR_PMK]) {
9249                 pmksa.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
9250                 pmksa.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
9251         }
9252
9253         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9254             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9255                 return -EOPNOTSUPP;
9256
9257         switch (info->genlhdr->cmd) {
9258         case NL80211_CMD_SET_PMKSA:
9259                 rdev_ops = rdev->ops->set_pmksa;
9260                 break;
9261         case NL80211_CMD_DEL_PMKSA:
9262                 rdev_ops = rdev->ops->del_pmksa;
9263                 break;
9264         default:
9265                 WARN_ON(1);
9266                 break;
9267         }
9268
9269         if (!rdev_ops)
9270                 return -EOPNOTSUPP;
9271
9272         return rdev_ops(&rdev->wiphy, dev, &pmksa);
9273 }
9274
9275 static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
9276 {
9277         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9278         struct net_device *dev = info->user_ptr[1];
9279
9280         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
9281             dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
9282                 return -EOPNOTSUPP;
9283
9284         if (!rdev->ops->flush_pmksa)
9285                 return -EOPNOTSUPP;
9286
9287         return rdev_flush_pmksa(rdev, dev);
9288 }
9289
9290 static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
9291 {
9292         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9293         struct net_device *dev = info->user_ptr[1];
9294         u8 action_code, dialog_token;
9295         u32 peer_capability = 0;
9296         u16 status_code;
9297         u8 *peer;
9298         bool initiator;
9299
9300         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9301             !rdev->ops->tdls_mgmt)
9302                 return -EOPNOTSUPP;
9303
9304         if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
9305             !info->attrs[NL80211_ATTR_STATUS_CODE] ||
9306             !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
9307             !info->attrs[NL80211_ATTR_IE] ||
9308             !info->attrs[NL80211_ATTR_MAC])
9309                 return -EINVAL;
9310
9311         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9312         action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
9313         status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
9314         dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
9315         initiator = nla_get_flag(info->attrs[NL80211_ATTR_TDLS_INITIATOR]);
9316         if (info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY])
9317                 peer_capability =
9318                         nla_get_u32(info->attrs[NL80211_ATTR_TDLS_PEER_CAPABILITY]);
9319
9320         return rdev_tdls_mgmt(rdev, dev, peer, action_code,
9321                               dialog_token, status_code, peer_capability,
9322                               initiator,
9323                               nla_data(info->attrs[NL80211_ATTR_IE]),
9324                               nla_len(info->attrs[NL80211_ATTR_IE]));
9325 }
9326
9327 static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
9328 {
9329         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9330         struct net_device *dev = info->user_ptr[1];
9331         enum nl80211_tdls_operation operation;
9332         u8 *peer;
9333
9334         if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
9335             !rdev->ops->tdls_oper)
9336                 return -EOPNOTSUPP;
9337
9338         if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
9339             !info->attrs[NL80211_ATTR_MAC])
9340                 return -EINVAL;
9341
9342         operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
9343         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
9344
9345         return rdev_tdls_oper(rdev, dev, peer, operation);
9346 }
9347
9348 static int nl80211_remain_on_channel(struct sk_buff *skb,
9349                                      struct genl_info *info)
9350 {
9351         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9352         struct wireless_dev *wdev = info->user_ptr[1];
9353         struct cfg80211_chan_def chandef;
9354         const struct cfg80211_chan_def *compat_chandef;
9355         struct sk_buff *msg;
9356         void *hdr;
9357         u64 cookie;
9358         u32 duration;
9359         int err;
9360
9361         if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
9362             !info->attrs[NL80211_ATTR_DURATION])
9363                 return -EINVAL;
9364
9365         duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9366
9367         if (!rdev->ops->remain_on_channel ||
9368             !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
9369                 return -EOPNOTSUPP;
9370
9371         /*
9372          * We should be on that channel for at least a minimum amount of
9373          * time (10ms) but no longer than the driver supports.
9374          */
9375         if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9376             duration > rdev->wiphy.max_remain_on_channel_duration)
9377                 return -EINVAL;
9378
9379         err = nl80211_parse_chandef(rdev, info, &chandef);
9380         if (err)
9381                 return err;
9382
9383         wdev_lock(wdev);
9384         if (!cfg80211_off_channel_oper_allowed(wdev) &&
9385             !cfg80211_chandef_identical(&wdev->chandef, &chandef)) {
9386                 compat_chandef = cfg80211_chandef_compatible(&wdev->chandef,
9387                                                              &chandef);
9388                 if (compat_chandef != &chandef) {
9389                         wdev_unlock(wdev);
9390                         return -EBUSY;
9391                 }
9392         }
9393         wdev_unlock(wdev);
9394
9395         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9396         if (!msg)
9397                 return -ENOMEM;
9398
9399         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9400                              NL80211_CMD_REMAIN_ON_CHANNEL);
9401         if (!hdr) {
9402                 err = -ENOBUFS;
9403                 goto free_msg;
9404         }
9405
9406         err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
9407                                      duration, &cookie);
9408
9409         if (err)
9410                 goto free_msg;
9411
9412         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9413                               NL80211_ATTR_PAD))
9414                 goto nla_put_failure;
9415
9416         genlmsg_end(msg, hdr);
9417
9418         return genlmsg_reply(msg, info);
9419
9420  nla_put_failure:
9421         err = -ENOBUFS;
9422  free_msg:
9423         nlmsg_free(msg);
9424         return err;
9425 }
9426
9427 static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
9428                                             struct genl_info *info)
9429 {
9430         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9431         struct wireless_dev *wdev = info->user_ptr[1];
9432         u64 cookie;
9433
9434         if (!info->attrs[NL80211_ATTR_COOKIE])
9435                 return -EINVAL;
9436
9437         if (!rdev->ops->cancel_remain_on_channel)
9438                 return -EOPNOTSUPP;
9439
9440         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9441
9442         return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
9443 }
9444
9445 static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
9446                                        struct genl_info *info)
9447 {
9448         struct cfg80211_bitrate_mask mask;
9449         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9450         struct net_device *dev = info->user_ptr[1];
9451         int err;
9452
9453         if (!rdev->ops->set_bitrate_mask)
9454                 return -EOPNOTSUPP;
9455
9456         err = nl80211_parse_tx_bitrate_mask(info, &mask);
9457         if (err)
9458                 return err;
9459
9460         return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
9461 }
9462
9463 static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
9464 {
9465         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9466         struct wireless_dev *wdev = info->user_ptr[1];
9467         u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
9468
9469         if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
9470                 return -EINVAL;
9471
9472         if (info->attrs[NL80211_ATTR_FRAME_TYPE])
9473                 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
9474
9475         switch (wdev->iftype) {
9476         case NL80211_IFTYPE_STATION:
9477         case NL80211_IFTYPE_ADHOC:
9478         case NL80211_IFTYPE_P2P_CLIENT:
9479         case NL80211_IFTYPE_AP:
9480         case NL80211_IFTYPE_AP_VLAN:
9481         case NL80211_IFTYPE_MESH_POINT:
9482         case NL80211_IFTYPE_P2P_GO:
9483         case NL80211_IFTYPE_P2P_DEVICE:
9484                 break;
9485         case NL80211_IFTYPE_NAN:
9486         default:
9487                 return -EOPNOTSUPP;
9488         }
9489
9490         /* not much point in registering if we can't reply */
9491         if (!rdev->ops->mgmt_tx)
9492                 return -EOPNOTSUPP;
9493
9494         return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
9495                         nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
9496                         nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
9497 }
9498
9499 static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
9500 {
9501         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9502         struct wireless_dev *wdev = info->user_ptr[1];
9503         struct cfg80211_chan_def chandef;
9504         int err;
9505         void *hdr = NULL;
9506         u64 cookie;
9507         struct sk_buff *msg = NULL;
9508         struct cfg80211_mgmt_tx_params params = {
9509                 .dont_wait_for_ack =
9510                         info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
9511         };
9512
9513         if (!info->attrs[NL80211_ATTR_FRAME])
9514                 return -EINVAL;
9515
9516         if (!rdev->ops->mgmt_tx)
9517                 return -EOPNOTSUPP;
9518
9519         switch (wdev->iftype) {
9520         case NL80211_IFTYPE_P2P_DEVICE:
9521                 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
9522                         return -EINVAL;
9523         case NL80211_IFTYPE_STATION:
9524         case NL80211_IFTYPE_ADHOC:
9525         case NL80211_IFTYPE_P2P_CLIENT:
9526         case NL80211_IFTYPE_AP:
9527         case NL80211_IFTYPE_AP_VLAN:
9528         case NL80211_IFTYPE_MESH_POINT:
9529         case NL80211_IFTYPE_P2P_GO:
9530                 break;
9531         case NL80211_IFTYPE_NAN:
9532         default:
9533                 return -EOPNOTSUPP;
9534         }
9535
9536         if (info->attrs[NL80211_ATTR_DURATION]) {
9537                 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9538                         return -EINVAL;
9539                 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
9540
9541                 /*
9542                  * We should wait on the channel for at least a minimum amount
9543                  * of time (10ms) but no longer than the driver supports.
9544                  */
9545                 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
9546                     params.wait > rdev->wiphy.max_remain_on_channel_duration)
9547                         return -EINVAL;
9548         }
9549
9550         params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
9551
9552         if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
9553                 return -EINVAL;
9554
9555         params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
9556
9557         /* get the channel if any has been specified, otherwise pass NULL to
9558          * the driver. The latter will use the current one
9559          */
9560         chandef.chan = NULL;
9561         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
9562                 err = nl80211_parse_chandef(rdev, info, &chandef);
9563                 if (err)
9564                         return err;
9565         }
9566
9567         if (!chandef.chan && params.offchan)
9568                 return -EINVAL;
9569
9570         wdev_lock(wdev);
9571         if (params.offchan && !cfg80211_off_channel_oper_allowed(wdev)) {
9572                 wdev_unlock(wdev);
9573                 return -EBUSY;
9574         }
9575         wdev_unlock(wdev);
9576
9577         params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
9578         params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
9579
9580         if (info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]) {
9581                 int len = nla_len(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9582                 int i;
9583
9584                 if (len % sizeof(u16))
9585                         return -EINVAL;
9586
9587                 params.n_csa_offsets = len / sizeof(u16);
9588                 params.csa_offsets =
9589                         nla_data(info->attrs[NL80211_ATTR_CSA_C_OFFSETS_TX]);
9590
9591                 /* check that all the offsets fit the frame */
9592                 for (i = 0; i < params.n_csa_offsets; i++) {
9593                         if (params.csa_offsets[i] >= params.len)
9594                                 return -EINVAL;
9595                 }
9596         }
9597
9598         if (!params.dont_wait_for_ack) {
9599                 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9600                 if (!msg)
9601                         return -ENOMEM;
9602
9603                 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9604                                      NL80211_CMD_FRAME);
9605                 if (!hdr) {
9606                         err = -ENOBUFS;
9607                         goto free_msg;
9608                 }
9609         }
9610
9611         params.chan = chandef.chan;
9612         err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
9613         if (err)
9614                 goto free_msg;
9615
9616         if (msg) {
9617                 if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
9618                                       NL80211_ATTR_PAD))
9619                         goto nla_put_failure;
9620
9621                 genlmsg_end(msg, hdr);
9622                 return genlmsg_reply(msg, info);
9623         }
9624
9625         return 0;
9626
9627  nla_put_failure:
9628         err = -ENOBUFS;
9629  free_msg:
9630         nlmsg_free(msg);
9631         return err;
9632 }
9633
9634 static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
9635 {
9636         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9637         struct wireless_dev *wdev = info->user_ptr[1];
9638         u64 cookie;
9639
9640         if (!info->attrs[NL80211_ATTR_COOKIE])
9641                 return -EINVAL;
9642
9643         if (!rdev->ops->mgmt_tx_cancel_wait)
9644                 return -EOPNOTSUPP;
9645
9646         switch (wdev->iftype) {
9647         case NL80211_IFTYPE_STATION:
9648         case NL80211_IFTYPE_ADHOC:
9649         case NL80211_IFTYPE_P2P_CLIENT:
9650         case NL80211_IFTYPE_AP:
9651         case NL80211_IFTYPE_AP_VLAN:
9652         case NL80211_IFTYPE_P2P_GO:
9653         case NL80211_IFTYPE_P2P_DEVICE:
9654                 break;
9655         case NL80211_IFTYPE_NAN:
9656         default:
9657                 return -EOPNOTSUPP;
9658         }
9659
9660         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
9661
9662         return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
9663 }
9664
9665 static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
9666 {
9667         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9668         struct wireless_dev *wdev;
9669         struct net_device *dev = info->user_ptr[1];
9670         u8 ps_state;
9671         bool state;
9672         int err;
9673
9674         if (!info->attrs[NL80211_ATTR_PS_STATE])
9675                 return -EINVAL;
9676
9677         ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
9678
9679         if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
9680                 return -EINVAL;
9681
9682         wdev = dev->ieee80211_ptr;
9683
9684         if (!rdev->ops->set_power_mgmt)
9685                 return -EOPNOTSUPP;
9686
9687         state = (ps_state == NL80211_PS_ENABLED) ? true : false;
9688
9689         if (state == wdev->ps)
9690                 return 0;
9691
9692         err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
9693         if (!err)
9694                 wdev->ps = state;
9695         return err;
9696 }
9697
9698 static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
9699 {
9700         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9701         enum nl80211_ps_state ps_state;
9702         struct wireless_dev *wdev;
9703         struct net_device *dev = info->user_ptr[1];
9704         struct sk_buff *msg;
9705         void *hdr;
9706         int err;
9707
9708         wdev = dev->ieee80211_ptr;
9709
9710         if (!rdev->ops->set_power_mgmt)
9711                 return -EOPNOTSUPP;
9712
9713         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9714         if (!msg)
9715                 return -ENOMEM;
9716
9717         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
9718                              NL80211_CMD_GET_POWER_SAVE);
9719         if (!hdr) {
9720                 err = -ENOBUFS;
9721                 goto free_msg;
9722         }
9723
9724         if (wdev->ps)
9725                 ps_state = NL80211_PS_ENABLED;
9726         else
9727                 ps_state = NL80211_PS_DISABLED;
9728
9729         if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
9730                 goto nla_put_failure;
9731
9732         genlmsg_end(msg, hdr);
9733         return genlmsg_reply(msg, info);
9734
9735  nla_put_failure:
9736         err = -ENOBUFS;
9737  free_msg:
9738         nlmsg_free(msg);
9739         return err;
9740 }
9741
9742 static const struct nla_policy
9743 nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
9744         [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_BINARY },
9745         [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
9746         [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
9747         [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
9748         [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
9749         [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
9750         [NL80211_ATTR_CQM_RSSI_LEVEL] = { .type = NLA_S32 },
9751 };
9752
9753 static int nl80211_set_cqm_txe(struct genl_info *info,
9754                                u32 rate, u32 pkts, u32 intvl)
9755 {
9756         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9757         struct net_device *dev = info->user_ptr[1];
9758         struct wireless_dev *wdev = dev->ieee80211_ptr;
9759
9760         if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
9761                 return -EINVAL;
9762
9763         if (!rdev->ops->set_cqm_txe_config)
9764                 return -EOPNOTSUPP;
9765
9766         if (wdev->iftype != NL80211_IFTYPE_STATION &&
9767             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9768                 return -EOPNOTSUPP;
9769
9770         return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
9771 }
9772
9773 static int cfg80211_cqm_rssi_update(struct cfg80211_registered_device *rdev,
9774                                     struct net_device *dev)
9775 {
9776         struct wireless_dev *wdev = dev->ieee80211_ptr;
9777         s32 last, low, high;
9778         u32 hyst;
9779         int i, n, low_index;
9780         int err;
9781
9782         /* RSSI reporting disabled? */
9783         if (!wdev->cqm_config)
9784                 return rdev_set_cqm_rssi_range_config(rdev, dev, 0, 0);
9785
9786         /*
9787          * Obtain current RSSI value if possible, if not and no RSSI threshold
9788          * event has been received yet, we should receive an event after a
9789          * connection is established and enough beacons received to calculate
9790          * the average.
9791          */
9792         if (!wdev->cqm_config->last_rssi_event_value && wdev->current_bss &&
9793             rdev->ops->get_station) {
9794                 struct station_info sinfo;
9795                 u8 *mac_addr;
9796
9797                 mac_addr = wdev->current_bss->pub.bssid;
9798
9799                 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
9800                 if (err)
9801                         return err;
9802
9803                 if (sinfo.filled & BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG))
9804                         wdev->cqm_config->last_rssi_event_value =
9805                                 (s8) sinfo.rx_beacon_signal_avg;
9806         }
9807
9808         last = wdev->cqm_config->last_rssi_event_value;
9809         hyst = wdev->cqm_config->rssi_hyst;
9810         n = wdev->cqm_config->n_rssi_thresholds;
9811
9812         for (i = 0; i < n; i++) {
9813                 i = array_index_nospec(i, n);
9814                 if (last < wdev->cqm_config->rssi_thresholds[i])
9815                         break;
9816         }
9817
9818         low_index = i - 1;
9819         if (low_index >= 0) {
9820                 low_index = array_index_nospec(low_index, n);
9821                 low = wdev->cqm_config->rssi_thresholds[low_index] - hyst;
9822         } else {
9823                 low = S32_MIN;
9824         }
9825         if (i < n) {
9826                 i = array_index_nospec(i, n);
9827                 high = wdev->cqm_config->rssi_thresholds[i] + hyst - 1;
9828         } else {
9829                 high = S32_MAX;
9830         }
9831
9832         return rdev_set_cqm_rssi_range_config(rdev, dev, low, high);
9833 }
9834
9835 static int nl80211_set_cqm_rssi(struct genl_info *info,
9836                                 const s32 *thresholds, int n_thresholds,
9837                                 u32 hysteresis)
9838 {
9839         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9840         struct net_device *dev = info->user_ptr[1];
9841         struct wireless_dev *wdev = dev->ieee80211_ptr;
9842         int i, err;
9843         s32 prev = S32_MIN;
9844
9845         /* Check all values negative and sorted */
9846         for (i = 0; i < n_thresholds; i++) {
9847                 if (thresholds[i] > 0 || thresholds[i] <= prev)
9848                         return -EINVAL;
9849
9850                 prev = thresholds[i];
9851         }
9852
9853         if (wdev->iftype != NL80211_IFTYPE_STATION &&
9854             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
9855                 return -EOPNOTSUPP;
9856
9857         wdev_lock(wdev);
9858         cfg80211_cqm_config_free(wdev);
9859         wdev_unlock(wdev);
9860
9861         if (n_thresholds <= 1 && rdev->ops->set_cqm_rssi_config) {
9862                 if (n_thresholds == 0 || thresholds[0] == 0) /* Disabling */
9863                         return rdev_set_cqm_rssi_config(rdev, dev, 0, 0);
9864
9865                 return rdev_set_cqm_rssi_config(rdev, dev,
9866                                                 thresholds[0], hysteresis);
9867         }
9868
9869         if (!wiphy_ext_feature_isset(&rdev->wiphy,
9870                                      NL80211_EXT_FEATURE_CQM_RSSI_LIST))
9871                 return -EOPNOTSUPP;
9872
9873         if (n_thresholds == 1 && thresholds[0] == 0) /* Disabling */
9874                 n_thresholds = 0;
9875
9876         wdev_lock(wdev);
9877         if (n_thresholds) {
9878                 struct cfg80211_cqm_config *cqm_config;
9879
9880                 cqm_config = kzalloc(sizeof(struct cfg80211_cqm_config) +
9881                                      n_thresholds * sizeof(s32), GFP_KERNEL);
9882                 if (!cqm_config) {
9883                         err = -ENOMEM;
9884                         goto unlock;
9885                 }
9886
9887                 cqm_config->rssi_hyst = hysteresis;
9888                 cqm_config->n_rssi_thresholds = n_thresholds;
9889                 memcpy(cqm_config->rssi_thresholds, thresholds,
9890                        n_thresholds * sizeof(s32));
9891
9892                 wdev->cqm_config = cqm_config;
9893         }
9894
9895         err = cfg80211_cqm_rssi_update(rdev, dev);
9896
9897 unlock:
9898         wdev_unlock(wdev);
9899
9900         return err;
9901 }
9902
9903 static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
9904 {
9905         struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
9906         struct nlattr *cqm;
9907         int err;
9908
9909         cqm = info->attrs[NL80211_ATTR_CQM];
9910         if (!cqm)
9911                 return -EINVAL;
9912
9913         err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
9914                                nl80211_attr_cqm_policy, info->extack);
9915         if (err)
9916                 return err;
9917
9918         if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
9919             attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
9920                 const s32 *thresholds =
9921                         nla_data(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
9922                 int len = nla_len(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
9923                 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
9924
9925                 if (len % 4)
9926                         return -EINVAL;
9927
9928                 return nl80211_set_cqm_rssi(info, thresholds, len / 4,
9929                                             hysteresis);
9930         }
9931
9932         if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
9933             attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
9934             attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
9935                 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
9936                 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
9937                 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
9938
9939                 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
9940         }
9941
9942         return -EINVAL;
9943 }
9944
9945 static int nl80211_join_ocb(struct sk_buff *skb, struct genl_info *info)
9946 {
9947         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9948         struct net_device *dev = info->user_ptr[1];
9949         struct ocb_setup setup = {};
9950         int err;
9951
9952         err = nl80211_parse_chandef(rdev, info, &setup.chandef);
9953         if (err)
9954                 return err;
9955
9956         return cfg80211_join_ocb(rdev, dev, &setup);
9957 }
9958
9959 static int nl80211_leave_ocb(struct sk_buff *skb, struct genl_info *info)
9960 {
9961         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9962         struct net_device *dev = info->user_ptr[1];
9963
9964         return cfg80211_leave_ocb(rdev, dev);
9965 }
9966
9967 static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
9968 {
9969         struct cfg80211_registered_device *rdev = info->user_ptr[0];
9970         struct net_device *dev = info->user_ptr[1];
9971         struct mesh_config cfg;
9972         struct mesh_setup setup;
9973         int err;
9974
9975         /* start with default */
9976         memcpy(&cfg, &default_mesh_config, sizeof(cfg));
9977         memcpy(&setup, &default_mesh_setup, sizeof(setup));
9978
9979         if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
9980                 /* and parse parameters if given */
9981                 err = nl80211_parse_mesh_config(info, &cfg, NULL);
9982                 if (err)
9983                         return err;
9984         }
9985
9986         if (!info->attrs[NL80211_ATTR_MESH_ID] ||
9987             !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
9988                 return -EINVAL;
9989
9990         setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
9991         setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
9992
9993         if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
9994             !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
9995                             nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
9996                         return -EINVAL;
9997
9998         if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
9999                 setup.beacon_interval =
10000                         nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
10001
10002                 err = cfg80211_validate_beacon_int(rdev,
10003                                                    NL80211_IFTYPE_MESH_POINT,
10004                                                    setup.beacon_interval);
10005                 if (err)
10006                         return err;
10007         }
10008
10009         if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
10010                 setup.dtim_period =
10011                         nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
10012                 if (setup.dtim_period < 1 || setup.dtim_period > 100)
10013                         return -EINVAL;
10014         }
10015
10016         if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
10017                 /* parse additional setup parameters if given */
10018                 err = nl80211_parse_mesh_setup(info, &setup);
10019                 if (err)
10020                         return err;
10021         }
10022
10023         if (setup.user_mpm)
10024                 cfg.auto_open_plinks = false;
10025
10026         if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
10027                 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
10028                 if (err)
10029                         return err;
10030         } else {
10031                 /* cfg80211_join_mesh() will sort it out */
10032                 setup.chandef.chan = NULL;
10033         }
10034
10035         if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
10036                 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10037                 int n_rates =
10038                         nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
10039                 struct ieee80211_supported_band *sband;
10040
10041                 if (!setup.chandef.chan)
10042                         return -EINVAL;
10043
10044                 sband = rdev->wiphy.bands[setup.chandef.chan->band];
10045
10046                 err = ieee80211_get_ratemask(sband, rates, n_rates,
10047                                              &setup.basic_rates);
10048                 if (err)
10049                         return err;
10050         }
10051
10052         if (info->attrs[NL80211_ATTR_TX_RATES]) {
10053                 err = nl80211_parse_tx_bitrate_mask(info, &setup.beacon_rate);
10054                 if (err)
10055                         return err;
10056
10057                 if (!setup.chandef.chan)
10058                         return -EINVAL;
10059
10060                 err = validate_beacon_tx_rate(rdev, setup.chandef.chan->band,
10061                                               &setup.beacon_rate);
10062                 if (err)
10063                         return err;
10064         }
10065
10066         setup.userspace_handles_dfs =
10067                 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
10068
10069         return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
10070 }
10071
10072 static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
10073 {
10074         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10075         struct net_device *dev = info->user_ptr[1];
10076
10077         return cfg80211_leave_mesh(rdev, dev);
10078 }
10079
10080 #ifdef CONFIG_PM
10081 static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
10082                                         struct cfg80211_registered_device *rdev)
10083 {
10084         struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
10085         struct nlattr *nl_pats, *nl_pat;
10086         int i, pat_len;
10087
10088         if (!wowlan->n_patterns)
10089                 return 0;
10090
10091         nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
10092         if (!nl_pats)
10093                 return -ENOBUFS;
10094
10095         for (i = 0; i < wowlan->n_patterns; i++) {
10096                 nl_pat = nla_nest_start(msg, i + 1);
10097                 if (!nl_pat)
10098                         return -ENOBUFS;
10099                 pat_len = wowlan->patterns[i].pattern_len;
10100                 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
10101                             wowlan->patterns[i].mask) ||
10102                     nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10103                             wowlan->patterns[i].pattern) ||
10104                     nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10105                                 wowlan->patterns[i].pkt_offset))
10106                         return -ENOBUFS;
10107                 nla_nest_end(msg, nl_pat);
10108         }
10109         nla_nest_end(msg, nl_pats);
10110
10111         return 0;
10112 }
10113
10114 static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
10115                                    struct cfg80211_wowlan_tcp *tcp)
10116 {
10117         struct nlattr *nl_tcp;
10118
10119         if (!tcp)
10120                 return 0;
10121
10122         nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
10123         if (!nl_tcp)
10124                 return -ENOBUFS;
10125
10126         if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
10127             nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
10128             nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
10129             nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
10130             nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
10131             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
10132                     tcp->payload_len, tcp->payload) ||
10133             nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
10134                         tcp->data_interval) ||
10135             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
10136                     tcp->wake_len, tcp->wake_data) ||
10137             nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
10138                     DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
10139                 return -ENOBUFS;
10140
10141         if (tcp->payload_seq.len &&
10142             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
10143                     sizeof(tcp->payload_seq), &tcp->payload_seq))
10144                 return -ENOBUFS;
10145
10146         if (tcp->payload_tok.len &&
10147             nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
10148                     sizeof(tcp->payload_tok) + tcp->tokens_size,
10149                     &tcp->payload_tok))
10150                 return -ENOBUFS;
10151
10152         nla_nest_end(msg, nl_tcp);
10153
10154         return 0;
10155 }
10156
10157 static int nl80211_send_wowlan_nd(struct sk_buff *msg,
10158                                   struct cfg80211_sched_scan_request *req)
10159 {
10160         struct nlattr *nd, *freqs, *matches, *match, *scan_plans, *scan_plan;
10161         int i;
10162
10163         if (!req)
10164                 return 0;
10165
10166         nd = nla_nest_start(msg, NL80211_WOWLAN_TRIG_NET_DETECT);
10167         if (!nd)
10168                 return -ENOBUFS;
10169
10170         if (req->n_scan_plans == 1 &&
10171             nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL,
10172                         req->scan_plans[0].interval * 1000))
10173                 return -ENOBUFS;
10174
10175         if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
10176                 return -ENOBUFS;
10177
10178         if (req->relative_rssi_set) {
10179                 struct nl80211_bss_select_rssi_adjust rssi_adjust;
10180
10181                 if (nla_put_s8(msg, NL80211_ATTR_SCHED_SCAN_RELATIVE_RSSI,
10182                                req->relative_rssi))
10183                         return -ENOBUFS;
10184
10185                 rssi_adjust.band = req->rssi_adjust.band;
10186                 rssi_adjust.delta = req->rssi_adjust.delta;
10187                 if (nla_put(msg, NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
10188                             sizeof(rssi_adjust), &rssi_adjust))
10189                         return -ENOBUFS;
10190         }
10191
10192         freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
10193         if (!freqs)
10194                 return -ENOBUFS;
10195
10196         for (i = 0; i < req->n_channels; i++) {
10197                 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
10198                         return -ENOBUFS;
10199         }
10200
10201         nla_nest_end(msg, freqs);
10202
10203         if (req->n_match_sets) {
10204                 matches = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_MATCH);
10205                 if (!matches)
10206                         return -ENOBUFS;
10207
10208                 for (i = 0; i < req->n_match_sets; i++) {
10209                         match = nla_nest_start(msg, i);
10210                         if (!match)
10211                                 return -ENOBUFS;
10212
10213                         if (nla_put(msg, NL80211_SCHED_SCAN_MATCH_ATTR_SSID,
10214                                     req->match_sets[i].ssid.ssid_len,
10215                                     req->match_sets[i].ssid.ssid))
10216                                 return -ENOBUFS;
10217                         nla_nest_end(msg, match);
10218                 }
10219                 nla_nest_end(msg, matches);
10220         }
10221
10222         scan_plans = nla_nest_start(msg, NL80211_ATTR_SCHED_SCAN_PLANS);
10223         if (!scan_plans)
10224                 return -ENOBUFS;
10225
10226         for (i = 0; i < req->n_scan_plans; i++) {
10227                 scan_plan = nla_nest_start(msg, i + 1);
10228                 if (!scan_plan)
10229                         return -ENOBUFS;
10230
10231                 if (!scan_plan ||
10232                     nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_INTERVAL,
10233                                 req->scan_plans[i].interval) ||
10234                     (req->scan_plans[i].iterations &&
10235                      nla_put_u32(msg, NL80211_SCHED_SCAN_PLAN_ITERATIONS,
10236                                  req->scan_plans[i].iterations)))
10237                         return -ENOBUFS;
10238                 nla_nest_end(msg, scan_plan);
10239         }
10240         nla_nest_end(msg, scan_plans);
10241
10242         nla_nest_end(msg, nd);
10243
10244         return 0;
10245 }
10246
10247 static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
10248 {
10249         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10250         struct sk_buff *msg;
10251         void *hdr;
10252         u32 size = NLMSG_DEFAULT_SIZE;
10253
10254         if (!rdev->wiphy.wowlan)
10255                 return -EOPNOTSUPP;
10256
10257         if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
10258                 /* adjust size to have room for all the data */
10259                 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
10260                         rdev->wiphy.wowlan_config->tcp->payload_len +
10261                         rdev->wiphy.wowlan_config->tcp->wake_len +
10262                         rdev->wiphy.wowlan_config->tcp->wake_len / 8;
10263         }
10264
10265         msg = nlmsg_new(size, GFP_KERNEL);
10266         if (!msg)
10267                 return -ENOMEM;
10268
10269         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10270                              NL80211_CMD_GET_WOWLAN);
10271         if (!hdr)
10272                 goto nla_put_failure;
10273
10274         if (rdev->wiphy.wowlan_config) {
10275                 struct nlattr *nl_wowlan;
10276
10277                 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10278                 if (!nl_wowlan)
10279                         goto nla_put_failure;
10280
10281                 if ((rdev->wiphy.wowlan_config->any &&
10282                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
10283                     (rdev->wiphy.wowlan_config->disconnect &&
10284                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
10285                     (rdev->wiphy.wowlan_config->magic_pkt &&
10286                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
10287                     (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
10288                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
10289                     (rdev->wiphy.wowlan_config->eap_identity_req &&
10290                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
10291                     (rdev->wiphy.wowlan_config->four_way_handshake &&
10292                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
10293                     (rdev->wiphy.wowlan_config->rfkill_release &&
10294                      nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
10295                         goto nla_put_failure;
10296
10297                 if (nl80211_send_wowlan_patterns(msg, rdev))
10298                         goto nla_put_failure;
10299
10300                 if (nl80211_send_wowlan_tcp(msg,
10301                                             rdev->wiphy.wowlan_config->tcp))
10302                         goto nla_put_failure;
10303
10304                 if (nl80211_send_wowlan_nd(
10305                             msg,
10306                             rdev->wiphy.wowlan_config->nd_config))
10307                         goto nla_put_failure;
10308
10309                 nla_nest_end(msg, nl_wowlan);
10310         }
10311
10312         genlmsg_end(msg, hdr);
10313         return genlmsg_reply(msg, info);
10314
10315 nla_put_failure:
10316         nlmsg_free(msg);
10317         return -ENOBUFS;
10318 }
10319
10320 static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
10321                                     struct nlattr *attr,
10322                                     struct cfg80211_wowlan *trig)
10323 {
10324         struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
10325         struct cfg80211_wowlan_tcp *cfg;
10326         struct nl80211_wowlan_tcp_data_token *tok = NULL;
10327         struct nl80211_wowlan_tcp_data_seq *seq = NULL;
10328         u32 size;
10329         u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
10330         int err, port;
10331
10332         if (!rdev->wiphy.wowlan->tcp)
10333                 return -EINVAL;
10334
10335         err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TCP, attr,
10336                                nl80211_wowlan_tcp_policy, NULL);
10337         if (err)
10338                 return err;
10339
10340         if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
10341             !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
10342             !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
10343             !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
10344             !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
10345             !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
10346             !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
10347             !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
10348                 return -EINVAL;
10349
10350         data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
10351         if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
10352                 return -EINVAL;
10353
10354         if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
10355                         rdev->wiphy.wowlan->tcp->data_interval_max ||
10356             nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
10357                 return -EINVAL;
10358
10359         wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
10360         if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
10361                 return -EINVAL;
10362
10363         wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
10364         if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
10365                 return -EINVAL;
10366
10367         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
10368                 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10369
10370                 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
10371                 tokens_size = tokln - sizeof(*tok);
10372
10373                 if (!tok->len || tokens_size % tok->len)
10374                         return -EINVAL;
10375                 if (!rdev->wiphy.wowlan->tcp->tok)
10376                         return -EINVAL;
10377                 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
10378                         return -EINVAL;
10379                 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
10380                         return -EINVAL;
10381                 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
10382                         return -EINVAL;
10383                 if (tok->offset + tok->len > data_size)
10384                         return -EINVAL;
10385         }
10386
10387         if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
10388                 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
10389                 if (!rdev->wiphy.wowlan->tcp->seq)
10390                         return -EINVAL;
10391                 if (seq->len == 0 || seq->len > 4)
10392                         return -EINVAL;
10393                 if (seq->len + seq->offset > data_size)
10394                         return -EINVAL;
10395         }
10396
10397         size = sizeof(*cfg);
10398         size += data_size;
10399         size += wake_size + wake_mask_size;
10400         size += tokens_size;
10401
10402         cfg = kzalloc(size, GFP_KERNEL);
10403         if (!cfg)
10404                 return -ENOMEM;
10405         cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
10406         cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
10407         memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
10408                ETH_ALEN);
10409         if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
10410                 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
10411         else
10412                 port = 0;
10413 #ifdef CONFIG_INET
10414         /* allocate a socket and port for it and use it */
10415         err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
10416                             IPPROTO_TCP, &cfg->sock, 1);
10417         if (err) {
10418                 kfree(cfg);
10419                 return err;
10420         }
10421         if (inet_csk_get_port(cfg->sock->sk, port)) {
10422                 sock_release(cfg->sock);
10423                 kfree(cfg);
10424                 return -EADDRINUSE;
10425         }
10426         cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
10427 #else
10428         if (!port) {
10429                 kfree(cfg);
10430                 return -EINVAL;
10431         }
10432         cfg->src_port = port;
10433 #endif
10434
10435         cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
10436         cfg->payload_len = data_size;
10437         cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
10438         memcpy((void *)cfg->payload,
10439                nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
10440                data_size);
10441         if (seq)
10442                 cfg->payload_seq = *seq;
10443         cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
10444         cfg->wake_len = wake_size;
10445         cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
10446         memcpy((void *)cfg->wake_data,
10447                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
10448                wake_size);
10449         cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
10450                          data_size + wake_size;
10451         memcpy((void *)cfg->wake_mask,
10452                nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
10453                wake_mask_size);
10454         if (tok) {
10455                 cfg->tokens_size = tokens_size;
10456                 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
10457         }
10458
10459         trig->tcp = cfg;
10460
10461         return 0;
10462 }
10463
10464 static int nl80211_parse_wowlan_nd(struct cfg80211_registered_device *rdev,
10465                                    const struct wiphy_wowlan_support *wowlan,
10466                                    struct nlattr *attr,
10467                                    struct cfg80211_wowlan *trig)
10468 {
10469         struct nlattr **tb;
10470         int err;
10471
10472         tb = kzalloc(NUM_NL80211_ATTR * sizeof(*tb), GFP_KERNEL);
10473         if (!tb)
10474                 return -ENOMEM;
10475
10476         if (!(wowlan->flags & WIPHY_WOWLAN_NET_DETECT)) {
10477                 err = -EOPNOTSUPP;
10478                 goto out;
10479         }
10480
10481         err = nla_parse_nested(tb, NL80211_ATTR_MAX, attr, nl80211_policy,
10482                                NULL);
10483         if (err)
10484                 goto out;
10485
10486         trig->nd_config = nl80211_parse_sched_scan(&rdev->wiphy, NULL, tb,
10487                                                    wowlan->max_nd_match_sets);
10488         err = PTR_ERR_OR_ZERO(trig->nd_config);
10489         if (err)
10490                 trig->nd_config = NULL;
10491
10492 out:
10493         kfree(tb);
10494         return err;
10495 }
10496
10497 static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
10498 {
10499         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10500         struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
10501         struct cfg80211_wowlan new_triggers = {};
10502         struct cfg80211_wowlan *ntrig;
10503         const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
10504         int err, i;
10505         bool prev_enabled = rdev->wiphy.wowlan_config;
10506         bool regular = false;
10507
10508         if (!wowlan)
10509                 return -EOPNOTSUPP;
10510
10511         if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
10512                 cfg80211_rdev_free_wowlan(rdev);
10513                 rdev->wiphy.wowlan_config = NULL;
10514                 goto set_wakeup;
10515         }
10516
10517         err = nla_parse_nested(tb, MAX_NL80211_WOWLAN_TRIG,
10518                                info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS],
10519                                nl80211_wowlan_policy, info->extack);
10520         if (err)
10521                 return err;
10522
10523         if (tb[NL80211_WOWLAN_TRIG_ANY]) {
10524                 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
10525                         return -EINVAL;
10526                 new_triggers.any = true;
10527         }
10528
10529         if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
10530                 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
10531                         return -EINVAL;
10532                 new_triggers.disconnect = true;
10533                 regular = true;
10534         }
10535
10536         if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
10537                 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
10538                         return -EINVAL;
10539                 new_triggers.magic_pkt = true;
10540                 regular = true;
10541         }
10542
10543         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
10544                 return -EINVAL;
10545
10546         if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
10547                 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
10548                         return -EINVAL;
10549                 new_triggers.gtk_rekey_failure = true;
10550                 regular = true;
10551         }
10552
10553         if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
10554                 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
10555                         return -EINVAL;
10556                 new_triggers.eap_identity_req = true;
10557                 regular = true;
10558         }
10559
10560         if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
10561                 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
10562                         return -EINVAL;
10563                 new_triggers.four_way_handshake = true;
10564                 regular = true;
10565         }
10566
10567         if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
10568                 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
10569                         return -EINVAL;
10570                 new_triggers.rfkill_release = true;
10571                 regular = true;
10572         }
10573
10574         if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
10575                 struct nlattr *pat;
10576                 int n_patterns = 0;
10577                 int rem, pat_len, mask_len, pkt_offset;
10578                 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10579
10580                 regular = true;
10581
10582                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10583                                     rem)
10584                         n_patterns++;
10585                 if (n_patterns > wowlan->n_patterns)
10586                         return -EINVAL;
10587
10588                 new_triggers.patterns = kcalloc(n_patterns,
10589                                                 sizeof(new_triggers.patterns[0]),
10590                                                 GFP_KERNEL);
10591                 if (!new_triggers.patterns)
10592                         return -ENOMEM;
10593
10594                 new_triggers.n_patterns = n_patterns;
10595                 i = 0;
10596
10597                 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
10598                                     rem) {
10599                         u8 *mask_pat;
10600
10601                         err = nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10602                                                nl80211_packet_pattern_policy,
10603                                                info->extack);
10604                         if (err)
10605                                 goto error;
10606
10607                         err = -EINVAL;
10608                         if (!pat_tb[NL80211_PKTPAT_MASK] ||
10609                             !pat_tb[NL80211_PKTPAT_PATTERN])
10610                                 goto error;
10611                         pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10612                         mask_len = DIV_ROUND_UP(pat_len, 8);
10613                         if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10614                                 goto error;
10615                         if (pat_len > wowlan->pattern_max_len ||
10616                             pat_len < wowlan->pattern_min_len)
10617                                 goto error;
10618
10619                         if (!pat_tb[NL80211_PKTPAT_OFFSET])
10620                                 pkt_offset = 0;
10621                         else
10622                                 pkt_offset = nla_get_u32(
10623                                         pat_tb[NL80211_PKTPAT_OFFSET]);
10624                         if (pkt_offset > wowlan->max_pkt_offset)
10625                                 goto error;
10626                         new_triggers.patterns[i].pkt_offset = pkt_offset;
10627
10628                         mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10629                         if (!mask_pat) {
10630                                 err = -ENOMEM;
10631                                 goto error;
10632                         }
10633                         new_triggers.patterns[i].mask = mask_pat;
10634                         memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10635                                mask_len);
10636                         mask_pat += mask_len;
10637                         new_triggers.patterns[i].pattern = mask_pat;
10638                         new_triggers.patterns[i].pattern_len = pat_len;
10639                         memcpy(mask_pat,
10640                                nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10641                                pat_len);
10642                         i++;
10643                 }
10644         }
10645
10646         if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
10647                 regular = true;
10648                 err = nl80211_parse_wowlan_tcp(
10649                         rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
10650                         &new_triggers);
10651                 if (err)
10652                         goto error;
10653         }
10654
10655         if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
10656                 regular = true;
10657                 err = nl80211_parse_wowlan_nd(
10658                         rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
10659                         &new_triggers);
10660                 if (err)
10661                         goto error;
10662         }
10663
10664         /* The 'any' trigger means the device continues operating more or less
10665          * as in its normal operation mode and wakes up the host on most of the
10666          * normal interrupts (like packet RX, ...)
10667          * It therefore makes little sense to combine with the more constrained
10668          * wakeup trigger modes.
10669          */
10670         if (new_triggers.any && regular) {
10671                 err = -EINVAL;
10672                 goto error;
10673         }
10674
10675         ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
10676         if (!ntrig) {
10677                 err = -ENOMEM;
10678                 goto error;
10679         }
10680         cfg80211_rdev_free_wowlan(rdev);
10681         rdev->wiphy.wowlan_config = ntrig;
10682
10683  set_wakeup:
10684         if (rdev->ops->set_wakeup &&
10685             prev_enabled != !!rdev->wiphy.wowlan_config)
10686                 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
10687
10688         return 0;
10689  error:
10690         for (i = 0; i < new_triggers.n_patterns; i++)
10691                 kfree(new_triggers.patterns[i].mask);
10692         kfree(new_triggers.patterns);
10693         if (new_triggers.tcp && new_triggers.tcp->sock)
10694                 sock_release(new_triggers.tcp->sock);
10695         kfree(new_triggers.tcp);
10696         kfree(new_triggers.nd_config);
10697         return err;
10698 }
10699 #endif
10700
10701 static int nl80211_send_coalesce_rules(struct sk_buff *msg,
10702                                        struct cfg80211_registered_device *rdev)
10703 {
10704         struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
10705         int i, j, pat_len;
10706         struct cfg80211_coalesce_rules *rule;
10707
10708         if (!rdev->coalesce->n_rules)
10709                 return 0;
10710
10711         nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
10712         if (!nl_rules)
10713                 return -ENOBUFS;
10714
10715         for (i = 0; i < rdev->coalesce->n_rules; i++) {
10716                 nl_rule = nla_nest_start(msg, i + 1);
10717                 if (!nl_rule)
10718                         return -ENOBUFS;
10719
10720                 rule = &rdev->coalesce->rules[i];
10721                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
10722                                 rule->delay))
10723                         return -ENOBUFS;
10724
10725                 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
10726                                 rule->condition))
10727                         return -ENOBUFS;
10728
10729                 nl_pats = nla_nest_start(msg,
10730                                 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
10731                 if (!nl_pats)
10732                         return -ENOBUFS;
10733
10734                 for (j = 0; j < rule->n_patterns; j++) {
10735                         nl_pat = nla_nest_start(msg, j + 1);
10736                         if (!nl_pat)
10737                                 return -ENOBUFS;
10738                         pat_len = rule->patterns[j].pattern_len;
10739                         if (nla_put(msg, NL80211_PKTPAT_MASK,
10740                                     DIV_ROUND_UP(pat_len, 8),
10741                                     rule->patterns[j].mask) ||
10742                             nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
10743                                     rule->patterns[j].pattern) ||
10744                             nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
10745                                         rule->patterns[j].pkt_offset))
10746                                 return -ENOBUFS;
10747                         nla_nest_end(msg, nl_pat);
10748                 }
10749                 nla_nest_end(msg, nl_pats);
10750                 nla_nest_end(msg, nl_rule);
10751         }
10752         nla_nest_end(msg, nl_rules);
10753
10754         return 0;
10755 }
10756
10757 static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
10758 {
10759         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10760         struct sk_buff *msg;
10761         void *hdr;
10762
10763         if (!rdev->wiphy.coalesce)
10764                 return -EOPNOTSUPP;
10765
10766         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10767         if (!msg)
10768                 return -ENOMEM;
10769
10770         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
10771                              NL80211_CMD_GET_COALESCE);
10772         if (!hdr)
10773                 goto nla_put_failure;
10774
10775         if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
10776                 goto nla_put_failure;
10777
10778         genlmsg_end(msg, hdr);
10779         return genlmsg_reply(msg, info);
10780
10781 nla_put_failure:
10782         nlmsg_free(msg);
10783         return -ENOBUFS;
10784 }
10785
10786 void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
10787 {
10788         struct cfg80211_coalesce *coalesce = rdev->coalesce;
10789         int i, j;
10790         struct cfg80211_coalesce_rules *rule;
10791
10792         if (!coalesce)
10793                 return;
10794
10795         for (i = 0; i < coalesce->n_rules; i++) {
10796                 rule = &coalesce->rules[i];
10797                 for (j = 0; j < rule->n_patterns; j++)
10798                         kfree(rule->patterns[j].mask);
10799                 kfree(rule->patterns);
10800         }
10801         kfree(coalesce->rules);
10802         kfree(coalesce);
10803         rdev->coalesce = NULL;
10804 }
10805
10806 static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
10807                                        struct nlattr *rule,
10808                                        struct cfg80211_coalesce_rules *new_rule)
10809 {
10810         int err, i;
10811         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10812         struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
10813         int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
10814         struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
10815
10816         err = nla_parse_nested(tb, NL80211_ATTR_COALESCE_RULE_MAX, rule,
10817                                nl80211_coalesce_policy, NULL);
10818         if (err)
10819                 return err;
10820
10821         if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
10822                 new_rule->delay =
10823                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
10824         if (new_rule->delay > coalesce->max_delay)
10825                 return -EINVAL;
10826
10827         if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
10828                 new_rule->condition =
10829                         nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
10830         if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
10831             new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
10832                 return -EINVAL;
10833
10834         if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
10835                 return -EINVAL;
10836
10837         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10838                             rem)
10839                 n_patterns++;
10840         if (n_patterns > coalesce->n_patterns)
10841                 return -EINVAL;
10842
10843         new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
10844                                      GFP_KERNEL);
10845         if (!new_rule->patterns)
10846                 return -ENOMEM;
10847
10848         new_rule->n_patterns = n_patterns;
10849         i = 0;
10850
10851         nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
10852                             rem) {
10853                 u8 *mask_pat;
10854
10855                 err = nla_parse_nested(pat_tb, MAX_NL80211_PKTPAT, pat,
10856                                        nl80211_packet_pattern_policy, NULL);
10857                 if (err)
10858                         return err;
10859
10860                 if (!pat_tb[NL80211_PKTPAT_MASK] ||
10861                     !pat_tb[NL80211_PKTPAT_PATTERN])
10862                         return -EINVAL;
10863                 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
10864                 mask_len = DIV_ROUND_UP(pat_len, 8);
10865                 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
10866                         return -EINVAL;
10867                 if (pat_len > coalesce->pattern_max_len ||
10868                     pat_len < coalesce->pattern_min_len)
10869                         return -EINVAL;
10870
10871                 if (!pat_tb[NL80211_PKTPAT_OFFSET])
10872                         pkt_offset = 0;
10873                 else
10874                         pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
10875                 if (pkt_offset > coalesce->max_pkt_offset)
10876                         return -EINVAL;
10877                 new_rule->patterns[i].pkt_offset = pkt_offset;
10878
10879                 mask_pat = kmalloc(mask_len + pat_len, GFP_KERNEL);
10880                 if (!mask_pat)
10881                         return -ENOMEM;
10882
10883                 new_rule->patterns[i].mask = mask_pat;
10884                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_MASK]),
10885                        mask_len);
10886
10887                 mask_pat += mask_len;
10888                 new_rule->patterns[i].pattern = mask_pat;
10889                 new_rule->patterns[i].pattern_len = pat_len;
10890                 memcpy(mask_pat, nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
10891                        pat_len);
10892                 i++;
10893         }
10894
10895         return 0;
10896 }
10897
10898 static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
10899 {
10900         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10901         const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
10902         struct cfg80211_coalesce new_coalesce = {};
10903         struct cfg80211_coalesce *n_coalesce;
10904         int err, rem_rule, n_rules = 0, i, j;
10905         struct nlattr *rule;
10906         struct cfg80211_coalesce_rules *tmp_rule;
10907
10908         if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
10909                 return -EOPNOTSUPP;
10910
10911         if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
10912                 cfg80211_rdev_free_coalesce(rdev);
10913                 rdev_set_coalesce(rdev, NULL);
10914                 return 0;
10915         }
10916
10917         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10918                             rem_rule)
10919                 n_rules++;
10920         if (n_rules > coalesce->n_rules)
10921                 return -EINVAL;
10922
10923         new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
10924                                      GFP_KERNEL);
10925         if (!new_coalesce.rules)
10926                 return -ENOMEM;
10927
10928         new_coalesce.n_rules = n_rules;
10929         i = 0;
10930
10931         nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
10932                             rem_rule) {
10933                 err = nl80211_parse_coalesce_rule(rdev, rule,
10934                                                   &new_coalesce.rules[i]);
10935                 if (err)
10936                         goto error;
10937
10938                 i++;
10939         }
10940
10941         err = rdev_set_coalesce(rdev, &new_coalesce);
10942         if (err)
10943                 goto error;
10944
10945         n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
10946         if (!n_coalesce) {
10947                 err = -ENOMEM;
10948                 goto error;
10949         }
10950         cfg80211_rdev_free_coalesce(rdev);
10951         rdev->coalesce = n_coalesce;
10952
10953         return 0;
10954 error:
10955         for (i = 0; i < new_coalesce.n_rules; i++) {
10956                 tmp_rule = &new_coalesce.rules[i];
10957                 for (j = 0; j < tmp_rule->n_patterns; j++)
10958                         kfree(tmp_rule->patterns[j].mask);
10959                 kfree(tmp_rule->patterns);
10960         }
10961         kfree(new_coalesce.rules);
10962
10963         return err;
10964 }
10965
10966 static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
10967 {
10968         struct cfg80211_registered_device *rdev = info->user_ptr[0];
10969         struct net_device *dev = info->user_ptr[1];
10970         struct wireless_dev *wdev = dev->ieee80211_ptr;
10971         struct nlattr *tb[NUM_NL80211_REKEY_DATA];
10972         struct cfg80211_gtk_rekey_data rekey_data = {};
10973         int err;
10974
10975         if (!info->attrs[NL80211_ATTR_REKEY_DATA])
10976                 return -EINVAL;
10977
10978         err = nla_parse_nested(tb, MAX_NL80211_REKEY_DATA,
10979                                info->attrs[NL80211_ATTR_REKEY_DATA],
10980                                nl80211_rekey_policy, info->extack);
10981         if (err)
10982                 return err;
10983
10984         if (!tb[NL80211_REKEY_DATA_REPLAY_CTR] || !tb[NL80211_REKEY_DATA_KEK] ||
10985             !tb[NL80211_REKEY_DATA_KCK])
10986                 return -EINVAL;
10987         if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
10988                 return -ERANGE;
10989         if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
10990                 return -ERANGE;
10991         if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
10992                 return -ERANGE;
10993
10994         rekey_data.kek = nla_data(tb[NL80211_REKEY_DATA_KEK]);
10995         rekey_data.kck = nla_data(tb[NL80211_REKEY_DATA_KCK]);
10996         rekey_data.replay_ctr = nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]);
10997
10998         wdev_lock(wdev);
10999         if (!wdev->current_bss) {
11000                 err = -ENOTCONN;
11001                 goto out;
11002         }
11003
11004         if (!rdev->ops->set_rekey_data) {
11005                 err = -EOPNOTSUPP;
11006                 goto out;
11007         }
11008
11009         err = rdev_set_rekey_data(rdev, dev, &rekey_data);
11010  out:
11011         wdev_unlock(wdev);
11012         return err;
11013 }
11014
11015 static int nl80211_register_unexpected_frame(struct sk_buff *skb,
11016                                              struct genl_info *info)
11017 {
11018         struct net_device *dev = info->user_ptr[1];
11019         struct wireless_dev *wdev = dev->ieee80211_ptr;
11020
11021         if (wdev->iftype != NL80211_IFTYPE_AP &&
11022             wdev->iftype != NL80211_IFTYPE_P2P_GO)
11023                 return -EINVAL;
11024
11025         if (wdev->ap_unexpected_nlportid)
11026                 return -EBUSY;
11027
11028         wdev->ap_unexpected_nlportid = info->snd_portid;
11029         return 0;
11030 }
11031
11032 static int nl80211_probe_client(struct sk_buff *skb,
11033                                 struct genl_info *info)
11034 {
11035         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11036         struct net_device *dev = info->user_ptr[1];
11037         struct wireless_dev *wdev = dev->ieee80211_ptr;
11038         struct sk_buff *msg;
11039         void *hdr;
11040         const u8 *addr;
11041         u64 cookie;
11042         int err;
11043
11044         if (wdev->iftype != NL80211_IFTYPE_AP &&
11045             wdev->iftype != NL80211_IFTYPE_P2P_GO)
11046                 return -EOPNOTSUPP;
11047
11048         if (!info->attrs[NL80211_ATTR_MAC])
11049                 return -EINVAL;
11050
11051         if (!rdev->ops->probe_client)
11052                 return -EOPNOTSUPP;
11053
11054         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11055         if (!msg)
11056                 return -ENOMEM;
11057
11058         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11059                              NL80211_CMD_PROBE_CLIENT);
11060         if (!hdr) {
11061                 err = -ENOBUFS;
11062                 goto free_msg;
11063         }
11064
11065         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
11066
11067         err = rdev_probe_client(rdev, dev, addr, &cookie);
11068         if (err)
11069                 goto free_msg;
11070
11071         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11072                               NL80211_ATTR_PAD))
11073                 goto nla_put_failure;
11074
11075         genlmsg_end(msg, hdr);
11076
11077         return genlmsg_reply(msg, info);
11078
11079  nla_put_failure:
11080         err = -ENOBUFS;
11081  free_msg:
11082         nlmsg_free(msg);
11083         return err;
11084 }
11085
11086 static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
11087 {
11088         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11089         struct cfg80211_beacon_registration *reg, *nreg;
11090         int rv;
11091
11092         if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
11093                 return -EOPNOTSUPP;
11094
11095         nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
11096         if (!nreg)
11097                 return -ENOMEM;
11098
11099         /* First, check if already registered. */
11100         spin_lock_bh(&rdev->beacon_registrations_lock);
11101         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11102                 if (reg->nlportid == info->snd_portid) {
11103                         rv = -EALREADY;
11104                         goto out_err;
11105                 }
11106         }
11107         /* Add it to the list */
11108         nreg->nlportid = info->snd_portid;
11109         list_add(&nreg->list, &rdev->beacon_registrations);
11110
11111         spin_unlock_bh(&rdev->beacon_registrations_lock);
11112
11113         return 0;
11114 out_err:
11115         spin_unlock_bh(&rdev->beacon_registrations_lock);
11116         kfree(nreg);
11117         return rv;
11118 }
11119
11120 static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
11121 {
11122         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11123         struct wireless_dev *wdev = info->user_ptr[1];
11124         int err;
11125
11126         if (!rdev->ops->start_p2p_device)
11127                 return -EOPNOTSUPP;
11128
11129         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11130                 return -EOPNOTSUPP;
11131
11132         if (wdev_running(wdev))
11133                 return 0;
11134
11135         if (rfkill_blocked(rdev->rfkill))
11136                 return -ERFKILL;
11137
11138         err = rdev_start_p2p_device(rdev, wdev);
11139         if (err)
11140                 return err;
11141
11142         wdev->is_running = true;
11143         rdev->opencount++;
11144
11145         return 0;
11146 }
11147
11148 static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
11149 {
11150         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11151         struct wireless_dev *wdev = info->user_ptr[1];
11152
11153         if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
11154                 return -EOPNOTSUPP;
11155
11156         if (!rdev->ops->stop_p2p_device)
11157                 return -EOPNOTSUPP;
11158
11159         cfg80211_stop_p2p_device(rdev, wdev);
11160
11161         return 0;
11162 }
11163
11164 static int nl80211_start_nan(struct sk_buff *skb, struct genl_info *info)
11165 {
11166         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11167         struct wireless_dev *wdev = info->user_ptr[1];
11168         struct cfg80211_nan_conf conf = {};
11169         int err;
11170
11171         if (wdev->iftype != NL80211_IFTYPE_NAN)
11172                 return -EOPNOTSUPP;
11173
11174         if (wdev_running(wdev))
11175                 return -EEXIST;
11176
11177         if (rfkill_blocked(rdev->rfkill))
11178                 return -ERFKILL;
11179
11180         if (!info->attrs[NL80211_ATTR_NAN_MASTER_PREF])
11181                 return -EINVAL;
11182
11183         conf.master_pref =
11184                 nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11185         if (!conf.master_pref)
11186                 return -EINVAL;
11187
11188         if (info->attrs[NL80211_ATTR_BANDS]) {
11189                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11190
11191                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11192                         return -EOPNOTSUPP;
11193
11194                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11195                         return -EINVAL;
11196
11197                 conf.bands = bands;
11198         }
11199
11200         err = rdev_start_nan(rdev, wdev, &conf);
11201         if (err)
11202                 return err;
11203
11204         wdev->is_running = true;
11205         rdev->opencount++;
11206
11207         return 0;
11208 }
11209
11210 static int nl80211_stop_nan(struct sk_buff *skb, struct genl_info *info)
11211 {
11212         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11213         struct wireless_dev *wdev = info->user_ptr[1];
11214
11215         if (wdev->iftype != NL80211_IFTYPE_NAN)
11216                 return -EOPNOTSUPP;
11217
11218         cfg80211_stop_nan(rdev, wdev);
11219
11220         return 0;
11221 }
11222
11223 static int validate_nan_filter(struct nlattr *filter_attr)
11224 {
11225         struct nlattr *attr;
11226         int len = 0, n_entries = 0, rem;
11227
11228         nla_for_each_nested(attr, filter_attr, rem) {
11229                 len += nla_len(attr);
11230                 n_entries++;
11231         }
11232
11233         if (len >= U8_MAX)
11234                 return -EINVAL;
11235
11236         return n_entries;
11237 }
11238
11239 static int handle_nan_filter(struct nlattr *attr_filter,
11240                              struct cfg80211_nan_func *func,
11241                              bool tx)
11242 {
11243         struct nlattr *attr;
11244         int n_entries, rem, i;
11245         struct cfg80211_nan_func_filter *filter;
11246
11247         n_entries = validate_nan_filter(attr_filter);
11248         if (n_entries < 0)
11249                 return n_entries;
11250
11251         BUILD_BUG_ON(sizeof(*func->rx_filters) != sizeof(*func->tx_filters));
11252
11253         filter = kcalloc(n_entries, sizeof(*func->rx_filters), GFP_KERNEL);
11254         if (!filter)
11255                 return -ENOMEM;
11256
11257         i = 0;
11258         nla_for_each_nested(attr, attr_filter, rem) {
11259                 filter[i].filter = nla_memdup(attr, GFP_KERNEL);
11260                 filter[i].len = nla_len(attr);
11261                 i++;
11262         }
11263         if (tx) {
11264                 func->num_tx_filters = n_entries;
11265                 func->tx_filters = filter;
11266         } else {
11267                 func->num_rx_filters = n_entries;
11268                 func->rx_filters = filter;
11269         }
11270
11271         return 0;
11272 }
11273
11274 static int nl80211_nan_add_func(struct sk_buff *skb,
11275                                 struct genl_info *info)
11276 {
11277         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11278         struct wireless_dev *wdev = info->user_ptr[1];
11279         struct nlattr *tb[NUM_NL80211_NAN_FUNC_ATTR], *func_attr;
11280         struct cfg80211_nan_func *func;
11281         struct sk_buff *msg = NULL;
11282         void *hdr = NULL;
11283         int err = 0;
11284
11285         if (wdev->iftype != NL80211_IFTYPE_NAN)
11286                 return -EOPNOTSUPP;
11287
11288         if (!wdev_running(wdev))
11289                 return -ENOTCONN;
11290
11291         if (!info->attrs[NL80211_ATTR_NAN_FUNC])
11292                 return -EINVAL;
11293
11294         err = nla_parse_nested(tb, NL80211_NAN_FUNC_ATTR_MAX,
11295                                info->attrs[NL80211_ATTR_NAN_FUNC],
11296                                nl80211_nan_func_policy, info->extack);
11297         if (err)
11298                 return err;
11299
11300         func = kzalloc(sizeof(*func), GFP_KERNEL);
11301         if (!func)
11302                 return -ENOMEM;
11303
11304         func->cookie = wdev->wiphy->cookie_counter++;
11305
11306         if (!tb[NL80211_NAN_FUNC_TYPE] ||
11307             nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]) > NL80211_NAN_FUNC_MAX_TYPE) {
11308                 err = -EINVAL;
11309                 goto out;
11310         }
11311
11312
11313         func->type = nla_get_u8(tb[NL80211_NAN_FUNC_TYPE]);
11314
11315         if (!tb[NL80211_NAN_FUNC_SERVICE_ID]) {
11316                 err = -EINVAL;
11317                 goto out;
11318         }
11319
11320         memcpy(func->service_id, nla_data(tb[NL80211_NAN_FUNC_SERVICE_ID]),
11321                sizeof(func->service_id));
11322
11323         func->close_range =
11324                 nla_get_flag(tb[NL80211_NAN_FUNC_CLOSE_RANGE]);
11325
11326         if (tb[NL80211_NAN_FUNC_SERVICE_INFO]) {
11327                 func->serv_spec_info_len =
11328                         nla_len(tb[NL80211_NAN_FUNC_SERVICE_INFO]);
11329                 func->serv_spec_info =
11330                         kmemdup(nla_data(tb[NL80211_NAN_FUNC_SERVICE_INFO]),
11331                                 func->serv_spec_info_len,
11332                                 GFP_KERNEL);
11333                 if (!func->serv_spec_info) {
11334                         err = -ENOMEM;
11335                         goto out;
11336                 }
11337         }
11338
11339         if (tb[NL80211_NAN_FUNC_TTL])
11340                 func->ttl = nla_get_u32(tb[NL80211_NAN_FUNC_TTL]);
11341
11342         switch (func->type) {
11343         case NL80211_NAN_FUNC_PUBLISH:
11344                 if (!tb[NL80211_NAN_FUNC_PUBLISH_TYPE]) {
11345                         err = -EINVAL;
11346                         goto out;
11347                 }
11348
11349                 func->publish_type =
11350                         nla_get_u8(tb[NL80211_NAN_FUNC_PUBLISH_TYPE]);
11351                 func->publish_bcast =
11352                         nla_get_flag(tb[NL80211_NAN_FUNC_PUBLISH_BCAST]);
11353
11354                 if ((!(func->publish_type & NL80211_NAN_SOLICITED_PUBLISH)) &&
11355                         func->publish_bcast) {
11356                         err = -EINVAL;
11357                         goto out;
11358                 }
11359                 break;
11360         case NL80211_NAN_FUNC_SUBSCRIBE:
11361                 func->subscribe_active =
11362                         nla_get_flag(tb[NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE]);
11363                 break;
11364         case NL80211_NAN_FUNC_FOLLOW_UP:
11365                 if (!tb[NL80211_NAN_FUNC_FOLLOW_UP_ID] ||
11366                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] ||
11367                     !tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]) {
11368                         err = -EINVAL;
11369                         goto out;
11370                 }
11371
11372                 func->followup_id =
11373                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_ID]);
11374                 func->followup_reqid =
11375                         nla_get_u8(tb[NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID]);
11376                 memcpy(func->followup_dest.addr,
11377                        nla_data(tb[NL80211_NAN_FUNC_FOLLOW_UP_DEST]),
11378                        sizeof(func->followup_dest.addr));
11379                 if (func->ttl) {
11380                         err = -EINVAL;
11381                         goto out;
11382                 }
11383                 break;
11384         default:
11385                 err = -EINVAL;
11386                 goto out;
11387         }
11388
11389         if (tb[NL80211_NAN_FUNC_SRF]) {
11390                 struct nlattr *srf_tb[NUM_NL80211_NAN_SRF_ATTR];
11391
11392                 err = nla_parse_nested(srf_tb, NL80211_NAN_SRF_ATTR_MAX,
11393                                        tb[NL80211_NAN_FUNC_SRF],
11394                                        nl80211_nan_srf_policy, info->extack);
11395                 if (err)
11396                         goto out;
11397
11398                 func->srf_include =
11399                         nla_get_flag(srf_tb[NL80211_NAN_SRF_INCLUDE]);
11400
11401                 if (srf_tb[NL80211_NAN_SRF_BF]) {
11402                         if (srf_tb[NL80211_NAN_SRF_MAC_ADDRS] ||
11403                             !srf_tb[NL80211_NAN_SRF_BF_IDX]) {
11404                                 err = -EINVAL;
11405                                 goto out;
11406                         }
11407
11408                         func->srf_bf_len =
11409                                 nla_len(srf_tb[NL80211_NAN_SRF_BF]);
11410                         func->srf_bf =
11411                                 kmemdup(nla_data(srf_tb[NL80211_NAN_SRF_BF]),
11412                                         func->srf_bf_len, GFP_KERNEL);
11413                         if (!func->srf_bf) {
11414                                 err = -ENOMEM;
11415                                 goto out;
11416                         }
11417
11418                         func->srf_bf_idx =
11419                                 nla_get_u8(srf_tb[NL80211_NAN_SRF_BF_IDX]);
11420                 } else {
11421                         struct nlattr *attr, *mac_attr =
11422                                 srf_tb[NL80211_NAN_SRF_MAC_ADDRS];
11423                         int n_entries, rem, i = 0;
11424
11425                         if (!mac_attr) {
11426                                 err = -EINVAL;
11427                                 goto out;
11428                         }
11429
11430                         n_entries = validate_acl_mac_addrs(mac_attr);
11431                         if (n_entries <= 0) {
11432                                 err = -EINVAL;
11433                                 goto out;
11434                         }
11435
11436                         func->srf_num_macs = n_entries;
11437                         func->srf_macs =
11438                                 kzalloc(sizeof(*func->srf_macs) * n_entries,
11439                                         GFP_KERNEL);
11440                         if (!func->srf_macs) {
11441                                 err = -ENOMEM;
11442                                 goto out;
11443                         }
11444
11445                         nla_for_each_nested(attr, mac_attr, rem)
11446                                 memcpy(func->srf_macs[i++].addr, nla_data(attr),
11447                                        sizeof(*func->srf_macs));
11448                 }
11449         }
11450
11451         if (tb[NL80211_NAN_FUNC_TX_MATCH_FILTER]) {
11452                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_TX_MATCH_FILTER],
11453                                         func, true);
11454                 if (err)
11455                         goto out;
11456         }
11457
11458         if (tb[NL80211_NAN_FUNC_RX_MATCH_FILTER]) {
11459                 err = handle_nan_filter(tb[NL80211_NAN_FUNC_RX_MATCH_FILTER],
11460                                         func, false);
11461                 if (err)
11462                         goto out;
11463         }
11464
11465         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11466         if (!msg) {
11467                 err = -ENOMEM;
11468                 goto out;
11469         }
11470
11471         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11472                              NL80211_CMD_ADD_NAN_FUNCTION);
11473         /* This can't really happen - we just allocated 4KB */
11474         if (WARN_ON(!hdr)) {
11475                 err = -ENOMEM;
11476                 goto out;
11477         }
11478
11479         err = rdev_add_nan_func(rdev, wdev, func);
11480 out:
11481         if (err < 0) {
11482                 cfg80211_free_nan_func(func);
11483                 nlmsg_free(msg);
11484                 return err;
11485         }
11486
11487         /* propagate the instance id and cookie to userspace  */
11488         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, func->cookie,
11489                               NL80211_ATTR_PAD))
11490                 goto nla_put_failure;
11491
11492         func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11493         if (!func_attr)
11494                 goto nla_put_failure;
11495
11496         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID,
11497                        func->instance_id))
11498                 goto nla_put_failure;
11499
11500         nla_nest_end(msg, func_attr);
11501
11502         genlmsg_end(msg, hdr);
11503         return genlmsg_reply(msg, info);
11504
11505 nla_put_failure:
11506         nlmsg_free(msg);
11507         return -ENOBUFS;
11508 }
11509
11510 static int nl80211_nan_del_func(struct sk_buff *skb,
11511                                struct genl_info *info)
11512 {
11513         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11514         struct wireless_dev *wdev = info->user_ptr[1];
11515         u64 cookie;
11516
11517         if (wdev->iftype != NL80211_IFTYPE_NAN)
11518                 return -EOPNOTSUPP;
11519
11520         if (!wdev_running(wdev))
11521                 return -ENOTCONN;
11522
11523         if (!info->attrs[NL80211_ATTR_COOKIE])
11524                 return -EINVAL;
11525
11526         cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
11527
11528         rdev_del_nan_func(rdev, wdev, cookie);
11529
11530         return 0;
11531 }
11532
11533 static int nl80211_nan_change_config(struct sk_buff *skb,
11534                                      struct genl_info *info)
11535 {
11536         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11537         struct wireless_dev *wdev = info->user_ptr[1];
11538         struct cfg80211_nan_conf conf = {};
11539         u32 changed = 0;
11540
11541         if (wdev->iftype != NL80211_IFTYPE_NAN)
11542                 return -EOPNOTSUPP;
11543
11544         if (!wdev_running(wdev))
11545                 return -ENOTCONN;
11546
11547         if (info->attrs[NL80211_ATTR_NAN_MASTER_PREF]) {
11548                 conf.master_pref =
11549                         nla_get_u8(info->attrs[NL80211_ATTR_NAN_MASTER_PREF]);
11550                 if (conf.master_pref <= 1 || conf.master_pref == 255)
11551                         return -EINVAL;
11552
11553                 changed |= CFG80211_NAN_CONF_CHANGED_PREF;
11554         }
11555
11556         if (info->attrs[NL80211_ATTR_BANDS]) {
11557                 u32 bands = nla_get_u32(info->attrs[NL80211_ATTR_BANDS]);
11558
11559                 if (bands & ~(u32)wdev->wiphy->nan_supported_bands)
11560                         return -EOPNOTSUPP;
11561
11562                 if (bands && !(bands & BIT(NL80211_BAND_2GHZ)))
11563                         return -EINVAL;
11564
11565                 conf.bands = bands;
11566                 changed |= CFG80211_NAN_CONF_CHANGED_BANDS;
11567         }
11568
11569         if (!changed)
11570                 return -EINVAL;
11571
11572         return rdev_nan_change_conf(rdev, wdev, &conf, changed);
11573 }
11574
11575 void cfg80211_nan_match(struct wireless_dev *wdev,
11576                         struct cfg80211_nan_match_params *match, gfp_t gfp)
11577 {
11578         struct wiphy *wiphy = wdev->wiphy;
11579         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11580         struct nlattr *match_attr, *local_func_attr, *peer_func_attr;
11581         struct sk_buff *msg;
11582         void *hdr;
11583
11584         if (WARN_ON(!match->inst_id || !match->peer_inst_id || !match->addr))
11585                 return;
11586
11587         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11588         if (!msg)
11589                 return;
11590
11591         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NAN_MATCH);
11592         if (!hdr) {
11593                 nlmsg_free(msg);
11594                 return;
11595         }
11596
11597         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11598             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11599                                          wdev->netdev->ifindex)) ||
11600             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11601                               NL80211_ATTR_PAD))
11602                 goto nla_put_failure;
11603
11604         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, match->cookie,
11605                               NL80211_ATTR_PAD) ||
11606             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, match->addr))
11607                 goto nla_put_failure;
11608
11609         match_attr = nla_nest_start(msg, NL80211_ATTR_NAN_MATCH);
11610         if (!match_attr)
11611                 goto nla_put_failure;
11612
11613         local_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_LOCAL);
11614         if (!local_func_attr)
11615                 goto nla_put_failure;
11616
11617         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->inst_id))
11618                 goto nla_put_failure;
11619
11620         nla_nest_end(msg, local_func_attr);
11621
11622         peer_func_attr = nla_nest_start(msg, NL80211_NAN_MATCH_FUNC_PEER);
11623         if (!peer_func_attr)
11624                 goto nla_put_failure;
11625
11626         if (nla_put_u8(msg, NL80211_NAN_FUNC_TYPE, match->type) ||
11627             nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, match->peer_inst_id))
11628                 goto nla_put_failure;
11629
11630         if (match->info && match->info_len &&
11631             nla_put(msg, NL80211_NAN_FUNC_SERVICE_INFO, match->info_len,
11632                     match->info))
11633                 goto nla_put_failure;
11634
11635         nla_nest_end(msg, peer_func_attr);
11636         nla_nest_end(msg, match_attr);
11637         genlmsg_end(msg, hdr);
11638
11639         if (!wdev->owner_nlportid)
11640                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11641                                         msg, 0, NL80211_MCGRP_NAN, gfp);
11642         else
11643                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11644                                 wdev->owner_nlportid);
11645
11646         return;
11647
11648 nla_put_failure:
11649         nlmsg_free(msg);
11650 }
11651 EXPORT_SYMBOL(cfg80211_nan_match);
11652
11653 void cfg80211_nan_func_terminated(struct wireless_dev *wdev,
11654                                   u8 inst_id,
11655                                   enum nl80211_nan_func_term_reason reason,
11656                                   u64 cookie, gfp_t gfp)
11657 {
11658         struct wiphy *wiphy = wdev->wiphy;
11659         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
11660         struct sk_buff *msg;
11661         struct nlattr *func_attr;
11662         void *hdr;
11663
11664         if (WARN_ON(!inst_id))
11665                 return;
11666
11667         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11668         if (!msg)
11669                 return;
11670
11671         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_NAN_FUNCTION);
11672         if (!hdr) {
11673                 nlmsg_free(msg);
11674                 return;
11675         }
11676
11677         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11678             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11679                                          wdev->netdev->ifindex)) ||
11680             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
11681                               NL80211_ATTR_PAD))
11682                 goto nla_put_failure;
11683
11684         if (nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
11685                               NL80211_ATTR_PAD))
11686                 goto nla_put_failure;
11687
11688         func_attr = nla_nest_start(msg, NL80211_ATTR_NAN_FUNC);
11689         if (!func_attr)
11690                 goto nla_put_failure;
11691
11692         if (nla_put_u8(msg, NL80211_NAN_FUNC_INSTANCE_ID, inst_id) ||
11693             nla_put_u8(msg, NL80211_NAN_FUNC_TERM_REASON, reason))
11694                 goto nla_put_failure;
11695
11696         nla_nest_end(msg, func_attr);
11697         genlmsg_end(msg, hdr);
11698
11699         if (!wdev->owner_nlportid)
11700                 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy),
11701                                         msg, 0, NL80211_MCGRP_NAN, gfp);
11702         else
11703                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg,
11704                                 wdev->owner_nlportid);
11705
11706         return;
11707
11708 nla_put_failure:
11709         nlmsg_free(msg);
11710 }
11711 EXPORT_SYMBOL(cfg80211_nan_func_terminated);
11712
11713 static int nl80211_get_protocol_features(struct sk_buff *skb,
11714                                          struct genl_info *info)
11715 {
11716         void *hdr;
11717         struct sk_buff *msg;
11718
11719         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11720         if (!msg)
11721                 return -ENOMEM;
11722
11723         hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
11724                              NL80211_CMD_GET_PROTOCOL_FEATURES);
11725         if (!hdr)
11726                 goto nla_put_failure;
11727
11728         if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
11729                         NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
11730                 goto nla_put_failure;
11731
11732         genlmsg_end(msg, hdr);
11733         return genlmsg_reply(msg, info);
11734
11735  nla_put_failure:
11736         kfree_skb(msg);
11737         return -ENOBUFS;
11738 }
11739
11740 static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
11741 {
11742         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11743         struct cfg80211_update_ft_ies_params ft_params;
11744         struct net_device *dev = info->user_ptr[1];
11745
11746         if (!rdev->ops->update_ft_ies)
11747                 return -EOPNOTSUPP;
11748
11749         if (!info->attrs[NL80211_ATTR_MDID] ||
11750             !info->attrs[NL80211_ATTR_IE] ||
11751             !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
11752                 return -EINVAL;
11753
11754         memset(&ft_params, 0, sizeof(ft_params));
11755         ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
11756         ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
11757         ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
11758
11759         return rdev_update_ft_ies(rdev, dev, &ft_params);
11760 }
11761
11762 static int nl80211_crit_protocol_start(struct sk_buff *skb,
11763                                        struct genl_info *info)
11764 {
11765         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11766         struct wireless_dev *wdev = info->user_ptr[1];
11767         enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
11768         u16 duration;
11769         int ret;
11770
11771         if (!rdev->ops->crit_proto_start)
11772                 return -EOPNOTSUPP;
11773
11774         if (WARN_ON(!rdev->ops->crit_proto_stop))
11775                 return -EINVAL;
11776
11777         if (rdev->crit_proto_nlportid)
11778                 return -EBUSY;
11779
11780         /* determine protocol if provided */
11781         if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
11782                 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
11783
11784         if (proto >= NUM_NL80211_CRIT_PROTO)
11785                 return -EINVAL;
11786
11787         /* timeout must be provided */
11788         if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
11789                 return -EINVAL;
11790
11791         duration =
11792                 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
11793
11794         if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
11795                 return -ERANGE;
11796
11797         ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
11798         if (!ret)
11799                 rdev->crit_proto_nlportid = info->snd_portid;
11800
11801         return ret;
11802 }
11803
11804 static int nl80211_crit_protocol_stop(struct sk_buff *skb,
11805                                       struct genl_info *info)
11806 {
11807         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11808         struct wireless_dev *wdev = info->user_ptr[1];
11809
11810         if (!rdev->ops->crit_proto_stop)
11811                 return -EOPNOTSUPP;
11812
11813         if (rdev->crit_proto_nlportid) {
11814                 rdev->crit_proto_nlportid = 0;
11815                 rdev_crit_proto_stop(rdev, wdev);
11816         }
11817         return 0;
11818 }
11819
11820 static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
11821 {
11822         struct cfg80211_registered_device *rdev = info->user_ptr[0];
11823         struct wireless_dev *wdev =
11824                 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
11825         int i, err;
11826         u32 vid, subcmd;
11827
11828         if (!rdev->wiphy.vendor_commands)
11829                 return -EOPNOTSUPP;
11830
11831         if (IS_ERR(wdev)) {
11832                 err = PTR_ERR(wdev);
11833                 if (err != -EINVAL)
11834                         return err;
11835                 wdev = NULL;
11836         } else if (wdev->wiphy != &rdev->wiphy) {
11837                 return -EINVAL;
11838         }
11839
11840         if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
11841             !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
11842                 return -EINVAL;
11843
11844         vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
11845         subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
11846         for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
11847                 const struct wiphy_vendor_command *vcmd;
11848                 void *data = NULL;
11849                 int len = 0;
11850
11851                 vcmd = &rdev->wiphy.vendor_commands[i];
11852
11853                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11854                         continue;
11855
11856                 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
11857                                    WIPHY_VENDOR_CMD_NEED_NETDEV)) {
11858                         if (!wdev)
11859                                 return -EINVAL;
11860                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
11861                             !wdev->netdev)
11862                                 return -EINVAL;
11863
11864                         if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
11865                                 if (!wdev_running(wdev))
11866                                         return -ENETDOWN;
11867                         }
11868                 } else {
11869                         wdev = NULL;
11870                 }
11871
11872                 if (!vcmd->doit)
11873                         return -EOPNOTSUPP;
11874
11875                 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
11876                         data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11877                         len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
11878                 }
11879
11880                 rdev->cur_cmd_info = info;
11881                 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
11882                                                           data, len);
11883                 rdev->cur_cmd_info = NULL;
11884                 return err;
11885         }
11886
11887         return -EOPNOTSUPP;
11888 }
11889
11890 static int nl80211_prepare_vendor_dump(struct sk_buff *skb,
11891                                        struct netlink_callback *cb,
11892                                        struct cfg80211_registered_device **rdev,
11893                                        struct wireless_dev **wdev)
11894 {
11895         struct nlattr **attrbuf = genl_family_attrbuf(&nl80211_fam);
11896         u32 vid, subcmd;
11897         unsigned int i;
11898         int vcmd_idx = -1;
11899         int err;
11900         void *data = NULL;
11901         unsigned int data_len = 0;
11902
11903         if (cb->args[0]) {
11904                 /* subtract the 1 again here */
11905                 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
11906                 struct wireless_dev *tmp;
11907
11908                 if (!wiphy)
11909                         return -ENODEV;
11910                 *rdev = wiphy_to_rdev(wiphy);
11911                 *wdev = NULL;
11912
11913                 if (cb->args[1]) {
11914                         list_for_each_entry(tmp, &wiphy->wdev_list, list) {
11915                                 if (tmp->identifier == cb->args[1] - 1) {
11916                                         *wdev = tmp;
11917                                         break;
11918                                 }
11919                         }
11920                 }
11921
11922                 /* keep rtnl locked in successful case */
11923                 return 0;
11924         }
11925
11926         err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize, attrbuf,
11927                           nl80211_fam.maxattr, nl80211_policy, NULL);
11928         if (err)
11929                 return err;
11930
11931         if (!attrbuf[NL80211_ATTR_VENDOR_ID] ||
11932             !attrbuf[NL80211_ATTR_VENDOR_SUBCMD])
11933                 return -EINVAL;
11934
11935         *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk), attrbuf);
11936         if (IS_ERR(*wdev))
11937                 *wdev = NULL;
11938
11939         *rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk), attrbuf);
11940         if (IS_ERR(*rdev))
11941                 return PTR_ERR(*rdev);
11942
11943         vid = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_ID]);
11944         subcmd = nla_get_u32(attrbuf[NL80211_ATTR_VENDOR_SUBCMD]);
11945
11946         for (i = 0; i < (*rdev)->wiphy.n_vendor_commands; i++) {
11947                 const struct wiphy_vendor_command *vcmd;
11948
11949                 vcmd = &(*rdev)->wiphy.vendor_commands[i];
11950
11951                 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
11952                         continue;
11953
11954                 if (!vcmd->dumpit)
11955                         return -EOPNOTSUPP;
11956
11957                 vcmd_idx = i;
11958                 break;
11959         }
11960
11961         if (vcmd_idx < 0)
11962                 return -EOPNOTSUPP;
11963
11964         if (attrbuf[NL80211_ATTR_VENDOR_DATA]) {
11965                 data = nla_data(attrbuf[NL80211_ATTR_VENDOR_DATA]);
11966                 data_len = nla_len(attrbuf[NL80211_ATTR_VENDOR_DATA]);
11967         }
11968
11969         /* 0 is the first index - add 1 to parse only once */
11970         cb->args[0] = (*rdev)->wiphy_idx + 1;
11971         /* add 1 to know if it was NULL */
11972         cb->args[1] = *wdev ? (*wdev)->identifier + 1 : 0;
11973         cb->args[2] = vcmd_idx;
11974         cb->args[3] = (unsigned long)data;
11975         cb->args[4] = data_len;
11976
11977         /* keep rtnl locked in successful case */
11978         return 0;
11979 }
11980
11981 static int nl80211_vendor_cmd_dump(struct sk_buff *skb,
11982                                    struct netlink_callback *cb)
11983 {
11984         struct cfg80211_registered_device *rdev;
11985         struct wireless_dev *wdev;
11986         unsigned int vcmd_idx;
11987         const struct wiphy_vendor_command *vcmd;
11988         void *data;
11989         int data_len;
11990         int err;
11991         struct nlattr *vendor_data;
11992
11993         rtnl_lock();
11994         err = nl80211_prepare_vendor_dump(skb, cb, &rdev, &wdev);
11995         if (err)
11996                 goto out;
11997
11998         vcmd_idx = cb->args[2];
11999         data = (void *)cb->args[3];
12000         data_len = cb->args[4];
12001         vcmd = &rdev->wiphy.vendor_commands[vcmd_idx];
12002
12003         if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
12004                            WIPHY_VENDOR_CMD_NEED_NETDEV)) {
12005                 if (!wdev) {
12006                         err = -EINVAL;
12007                         goto out;
12008                 }
12009                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
12010                     !wdev->netdev) {
12011                         err = -EINVAL;
12012                         goto out;
12013                 }
12014
12015                 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
12016                         if (!wdev_running(wdev)) {
12017                                 err = -ENETDOWN;
12018                                 goto out;
12019                         }
12020                 }
12021         }
12022
12023         while (1) {
12024                 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
12025                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
12026                                            NL80211_CMD_VENDOR);
12027                 if (!hdr)
12028                         break;
12029
12030                 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
12031                     (wdev && nla_put_u64_64bit(skb, NL80211_ATTR_WDEV,
12032                                                wdev_id(wdev),
12033                                                NL80211_ATTR_PAD))) {
12034                         genlmsg_cancel(skb, hdr);
12035                         break;
12036                 }
12037
12038                 vendor_data = nla_nest_start(skb, NL80211_ATTR_VENDOR_DATA);
12039                 if (!vendor_data) {
12040                         genlmsg_cancel(skb, hdr);
12041                         break;
12042                 }
12043
12044                 err = vcmd->dumpit(&rdev->wiphy, wdev, skb, data, data_len,
12045                                    (unsigned long *)&cb->args[5]);
12046                 nla_nest_end(skb, vendor_data);
12047
12048                 if (err == -ENOBUFS || err == -ENOENT) {
12049                         genlmsg_cancel(skb, hdr);
12050                         break;
12051                 } else if (err) {
12052                         genlmsg_cancel(skb, hdr);
12053                         goto out;
12054                 }
12055
12056                 genlmsg_end(skb, hdr);
12057         }
12058
12059         err = skb->len;
12060  out:
12061         rtnl_unlock();
12062         return err;
12063 }
12064
12065 struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
12066                                            enum nl80211_commands cmd,
12067                                            enum nl80211_attrs attr,
12068                                            int approxlen)
12069 {
12070         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
12071
12072         if (WARN_ON(!rdev->cur_cmd_info))
12073                 return NULL;
12074
12075         return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
12076                                            rdev->cur_cmd_info->snd_portid,
12077                                            rdev->cur_cmd_info->snd_seq,
12078                                            cmd, attr, NULL, GFP_KERNEL);
12079 }
12080 EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
12081
12082 int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
12083 {
12084         struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
12085         void *hdr = ((void **)skb->cb)[1];
12086         struct nlattr *data = ((void **)skb->cb)[2];
12087
12088         /* clear CB data for netlink core to own from now on */
12089         memset(skb->cb, 0, sizeof(skb->cb));
12090
12091         if (WARN_ON(!rdev->cur_cmd_info)) {
12092                 kfree_skb(skb);
12093                 return -EINVAL;
12094         }
12095
12096         nla_nest_end(skb, data);
12097         genlmsg_end(skb, hdr);
12098         return genlmsg_reply(skb, rdev->cur_cmd_info);
12099 }
12100 EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
12101
12102 static int nl80211_set_qos_map(struct sk_buff *skb,
12103                                struct genl_info *info)
12104 {
12105         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12106         struct cfg80211_qos_map *qos_map = NULL;
12107         struct net_device *dev = info->user_ptr[1];
12108         u8 *pos, len, num_des, des_len, des;
12109         int ret;
12110
12111         if (!rdev->ops->set_qos_map)
12112                 return -EOPNOTSUPP;
12113
12114         if (info->attrs[NL80211_ATTR_QOS_MAP]) {
12115                 pos = nla_data(info->attrs[NL80211_ATTR_QOS_MAP]);
12116                 len = nla_len(info->attrs[NL80211_ATTR_QOS_MAP]);
12117
12118                 if (len % 2 || len < IEEE80211_QOS_MAP_LEN_MIN ||
12119                     len > IEEE80211_QOS_MAP_LEN_MAX)
12120                         return -EINVAL;
12121
12122                 qos_map = kzalloc(sizeof(struct cfg80211_qos_map), GFP_KERNEL);
12123                 if (!qos_map)
12124                         return -ENOMEM;
12125
12126                 num_des = (len - IEEE80211_QOS_MAP_LEN_MIN) >> 1;
12127                 if (num_des) {
12128                         des_len = num_des *
12129                                 sizeof(struct cfg80211_dscp_exception);
12130                         memcpy(qos_map->dscp_exception, pos, des_len);
12131                         qos_map->num_des = num_des;
12132                         for (des = 0; des < num_des; des++) {
12133                                 if (qos_map->dscp_exception[des].up > 7) {
12134                                         kfree(qos_map);
12135                                         return -EINVAL;
12136                                 }
12137                         }
12138                         pos += des_len;
12139                 }
12140                 memcpy(qos_map->up, pos, IEEE80211_QOS_MAP_LEN_MIN);
12141         }
12142
12143         wdev_lock(dev->ieee80211_ptr);
12144         ret = nl80211_key_allowed(dev->ieee80211_ptr);
12145         if (!ret)
12146                 ret = rdev_set_qos_map(rdev, dev, qos_map);
12147         wdev_unlock(dev->ieee80211_ptr);
12148
12149         kfree(qos_map);
12150         return ret;
12151 }
12152
12153 static int nl80211_add_tx_ts(struct sk_buff *skb, struct genl_info *info)
12154 {
12155         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12156         struct net_device *dev = info->user_ptr[1];
12157         struct wireless_dev *wdev = dev->ieee80211_ptr;
12158         const u8 *peer;
12159         u8 tsid, up;
12160         u16 admitted_time = 0;
12161         int err;
12162
12163         if (!(rdev->wiphy.features & NL80211_FEATURE_SUPPORTS_WMM_ADMISSION))
12164                 return -EOPNOTSUPP;
12165
12166         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC] ||
12167             !info->attrs[NL80211_ATTR_USER_PRIO])
12168                 return -EINVAL;
12169
12170         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12171         if (tsid >= IEEE80211_NUM_TIDS)
12172                 return -EINVAL;
12173
12174         up = nla_get_u8(info->attrs[NL80211_ATTR_USER_PRIO]);
12175         if (up >= IEEE80211_NUM_UPS)
12176                 return -EINVAL;
12177
12178         /* WMM uses TIDs 0-7 even for TSPEC */
12179         if (tsid >= IEEE80211_FIRST_TSPEC_TSID) {
12180                 /* TODO: handle 802.11 TSPEC/admission control
12181                  * need more attributes for that (e.g. BA session requirement);
12182                  * change the WMM adminssion test above to allow both then
12183                  */
12184                 return -EINVAL;
12185         }
12186
12187         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12188
12189         if (info->attrs[NL80211_ATTR_ADMITTED_TIME]) {
12190                 admitted_time =
12191                         nla_get_u16(info->attrs[NL80211_ATTR_ADMITTED_TIME]);
12192                 if (!admitted_time)
12193                         return -EINVAL;
12194         }
12195
12196         wdev_lock(wdev);
12197         switch (wdev->iftype) {
12198         case NL80211_IFTYPE_STATION:
12199         case NL80211_IFTYPE_P2P_CLIENT:
12200                 if (wdev->current_bss)
12201                         break;
12202                 err = -ENOTCONN;
12203                 goto out;
12204         default:
12205                 err = -EOPNOTSUPP;
12206                 goto out;
12207         }
12208
12209         err = rdev_add_tx_ts(rdev, dev, tsid, peer, up, admitted_time);
12210
12211  out:
12212         wdev_unlock(wdev);
12213         return err;
12214 }
12215
12216 static int nl80211_del_tx_ts(struct sk_buff *skb, struct genl_info *info)
12217 {
12218         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12219         struct net_device *dev = info->user_ptr[1];
12220         struct wireless_dev *wdev = dev->ieee80211_ptr;
12221         const u8 *peer;
12222         u8 tsid;
12223         int err;
12224
12225         if (!info->attrs[NL80211_ATTR_TSID] || !info->attrs[NL80211_ATTR_MAC])
12226                 return -EINVAL;
12227
12228         tsid = nla_get_u8(info->attrs[NL80211_ATTR_TSID]);
12229         peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
12230
12231         wdev_lock(wdev);
12232         err = rdev_del_tx_ts(rdev, dev, tsid, peer);
12233         wdev_unlock(wdev);
12234
12235         return err;
12236 }
12237
12238 static int nl80211_tdls_channel_switch(struct sk_buff *skb,
12239                                        struct genl_info *info)
12240 {
12241         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12242         struct net_device *dev = info->user_ptr[1];
12243         struct wireless_dev *wdev = dev->ieee80211_ptr;
12244         struct cfg80211_chan_def chandef = {};
12245         const u8 *addr;
12246         u8 oper_class;
12247         int err;
12248
12249         if (!rdev->ops->tdls_channel_switch ||
12250             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12251                 return -EOPNOTSUPP;
12252
12253         switch (dev->ieee80211_ptr->iftype) {
12254         case NL80211_IFTYPE_STATION:
12255         case NL80211_IFTYPE_P2P_CLIENT:
12256                 break;
12257         default:
12258                 return -EOPNOTSUPP;
12259         }
12260
12261         if (!info->attrs[NL80211_ATTR_MAC] ||
12262             !info->attrs[NL80211_ATTR_OPER_CLASS])
12263                 return -EINVAL;
12264
12265         err = nl80211_parse_chandef(rdev, info, &chandef);
12266         if (err)
12267                 return err;
12268
12269         /*
12270          * Don't allow wide channels on the 2.4Ghz band, as per IEEE802.11-2012
12271          * section 10.22.6.2.1. Disallow 5/10Mhz channels as well for now, the
12272          * specification is not defined for them.
12273          */
12274         if (chandef.chan->band == NL80211_BAND_2GHZ &&
12275             chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
12276             chandef.width != NL80211_CHAN_WIDTH_20)
12277                 return -EINVAL;
12278
12279         /* we will be active on the TDLS link */
12280         if (!cfg80211_reg_can_beacon_relax(&rdev->wiphy, &chandef,
12281                                            wdev->iftype))
12282                 return -EINVAL;
12283
12284         /* don't allow switching to DFS channels */
12285         if (cfg80211_chandef_dfs_required(wdev->wiphy, &chandef, wdev->iftype))
12286                 return -EINVAL;
12287
12288         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12289         oper_class = nla_get_u8(info->attrs[NL80211_ATTR_OPER_CLASS]);
12290
12291         wdev_lock(wdev);
12292         err = rdev_tdls_channel_switch(rdev, dev, addr, oper_class, &chandef);
12293         wdev_unlock(wdev);
12294
12295         return err;
12296 }
12297
12298 static int nl80211_tdls_cancel_channel_switch(struct sk_buff *skb,
12299                                               struct genl_info *info)
12300 {
12301         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12302         struct net_device *dev = info->user_ptr[1];
12303         struct wireless_dev *wdev = dev->ieee80211_ptr;
12304         const u8 *addr;
12305
12306         if (!rdev->ops->tdls_channel_switch ||
12307             !rdev->ops->tdls_cancel_channel_switch ||
12308             !(rdev->wiphy.features & NL80211_FEATURE_TDLS_CHANNEL_SWITCH))
12309                 return -EOPNOTSUPP;
12310
12311         switch (dev->ieee80211_ptr->iftype) {
12312         case NL80211_IFTYPE_STATION:
12313         case NL80211_IFTYPE_P2P_CLIENT:
12314                 break;
12315         default:
12316                 return -EOPNOTSUPP;
12317         }
12318
12319         if (!info->attrs[NL80211_ATTR_MAC])
12320                 return -EINVAL;
12321
12322         addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
12323
12324         wdev_lock(wdev);
12325         rdev_tdls_cancel_channel_switch(rdev, dev, addr);
12326         wdev_unlock(wdev);
12327
12328         return 0;
12329 }
12330
12331 static int nl80211_set_multicast_to_unicast(struct sk_buff *skb,
12332                                             struct genl_info *info)
12333 {
12334         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12335         struct net_device *dev = info->user_ptr[1];
12336         struct wireless_dev *wdev = dev->ieee80211_ptr;
12337         const struct nlattr *nla;
12338         bool enabled;
12339
12340         if (!rdev->ops->set_multicast_to_unicast)
12341                 return -EOPNOTSUPP;
12342
12343         if (wdev->iftype != NL80211_IFTYPE_AP &&
12344             wdev->iftype != NL80211_IFTYPE_P2P_GO)
12345                 return -EOPNOTSUPP;
12346
12347         nla = info->attrs[NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED];
12348         enabled = nla_get_flag(nla);
12349
12350         return rdev_set_multicast_to_unicast(rdev, dev, enabled);
12351 }
12352
12353 static int nl80211_set_pmk(struct sk_buff *skb, struct genl_info *info)
12354 {
12355         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12356         struct net_device *dev = info->user_ptr[1];
12357         struct wireless_dev *wdev = dev->ieee80211_ptr;
12358         struct cfg80211_pmk_conf pmk_conf = {};
12359         int ret;
12360
12361         if (wdev->iftype != NL80211_IFTYPE_STATION &&
12362             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12363                 return -EOPNOTSUPP;
12364
12365         if (!wiphy_ext_feature_isset(&rdev->wiphy,
12366                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12367                 return -EOPNOTSUPP;
12368
12369         if (!info->attrs[NL80211_ATTR_MAC] || !info->attrs[NL80211_ATTR_PMK])
12370                 return -EINVAL;
12371
12372         wdev_lock(wdev);
12373         if (!wdev->current_bss) {
12374                 ret = -ENOTCONN;
12375                 goto out;
12376         }
12377
12378         pmk_conf.aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12379         if (memcmp(pmk_conf.aa, wdev->current_bss->pub.bssid, ETH_ALEN)) {
12380                 ret = -EINVAL;
12381                 goto out;
12382         }
12383
12384         pmk_conf.pmk = nla_data(info->attrs[NL80211_ATTR_PMK]);
12385         pmk_conf.pmk_len = nla_len(info->attrs[NL80211_ATTR_PMK]);
12386         if (pmk_conf.pmk_len != WLAN_PMK_LEN &&
12387             pmk_conf.pmk_len != WLAN_PMK_LEN_SUITE_B_192) {
12388                 ret = -EINVAL;
12389                 goto out;
12390         }
12391
12392         if (info->attrs[NL80211_ATTR_PMKR0_NAME]) {
12393                 int r0_name_len = nla_len(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12394
12395                 if (r0_name_len != WLAN_PMK_NAME_LEN) {
12396                         ret = -EINVAL;
12397                         goto out;
12398                 }
12399
12400                 pmk_conf.pmk_r0_name =
12401                         nla_data(info->attrs[NL80211_ATTR_PMKR0_NAME]);
12402         }
12403
12404         ret = rdev_set_pmk(rdev, dev, &pmk_conf);
12405 out:
12406         wdev_unlock(wdev);
12407         return ret;
12408 }
12409
12410 static int nl80211_del_pmk(struct sk_buff *skb, struct genl_info *info)
12411 {
12412         struct cfg80211_registered_device *rdev = info->user_ptr[0];
12413         struct net_device *dev = info->user_ptr[1];
12414         struct wireless_dev *wdev = dev->ieee80211_ptr;
12415         const u8 *aa;
12416         int ret;
12417
12418         if (wdev->iftype != NL80211_IFTYPE_STATION &&
12419             wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
12420                 return -EOPNOTSUPP;
12421
12422         if (!wiphy_ext_feature_isset(&rdev->wiphy,
12423                                      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X))
12424                 return -EOPNOTSUPP;
12425
12426         if (!info->attrs[NL80211_ATTR_MAC])
12427                 return -EINVAL;
12428
12429         wdev_lock(wdev);
12430         aa = nla_data(info->attrs[NL80211_ATTR_MAC]);
12431         ret = rdev_del_pmk(rdev, dev, aa);
12432         wdev_unlock(wdev);
12433
12434         return ret;
12435 }
12436
12437 #define NL80211_FLAG_NEED_WIPHY         0x01
12438 #define NL80211_FLAG_NEED_NETDEV        0x02
12439 #define NL80211_FLAG_NEED_RTNL          0x04
12440 #define NL80211_FLAG_CHECK_NETDEV_UP    0x08
12441 #define NL80211_FLAG_NEED_NETDEV_UP     (NL80211_FLAG_NEED_NETDEV |\
12442                                          NL80211_FLAG_CHECK_NETDEV_UP)
12443 #define NL80211_FLAG_NEED_WDEV          0x10
12444 /* If a netdev is associated, it must be UP, P2P must be started */
12445 #define NL80211_FLAG_NEED_WDEV_UP       (NL80211_FLAG_NEED_WDEV |\
12446                                          NL80211_FLAG_CHECK_NETDEV_UP)
12447 #define NL80211_FLAG_CLEAR_SKB          0x20
12448
12449 static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
12450                             struct genl_info *info)
12451 {
12452         struct cfg80211_registered_device *rdev;
12453         struct wireless_dev *wdev;
12454         struct net_device *dev;
12455         bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
12456
12457         if (rtnl)
12458                 rtnl_lock();
12459
12460         if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
12461                 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
12462                 if (IS_ERR(rdev)) {
12463                         if (rtnl)
12464                                 rtnl_unlock();
12465                         return PTR_ERR(rdev);
12466                 }
12467                 info->user_ptr[0] = rdev;
12468         } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
12469                    ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12470                 ASSERT_RTNL();
12471
12472                 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
12473                                                   info->attrs);
12474                 if (IS_ERR(wdev)) {
12475                         if (rtnl)
12476                                 rtnl_unlock();
12477                         return PTR_ERR(wdev);
12478                 }
12479
12480                 dev = wdev->netdev;
12481                 rdev = wiphy_to_rdev(wdev->wiphy);
12482
12483                 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
12484                         if (!dev) {
12485                                 if (rtnl)
12486                                         rtnl_unlock();
12487                                 return -EINVAL;
12488                         }
12489
12490                         info->user_ptr[1] = dev;
12491                 } else {
12492                         info->user_ptr[1] = wdev;
12493                 }
12494
12495                 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
12496                     !wdev_running(wdev)) {
12497                         if (rtnl)
12498                                 rtnl_unlock();
12499                         return -ENETDOWN;
12500                 }
12501
12502                 if (dev)
12503                         dev_hold(dev);
12504
12505                 info->user_ptr[0] = rdev;
12506         }
12507
12508         return 0;
12509 }
12510
12511 static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
12512                               struct genl_info *info)
12513 {
12514         if (info->user_ptr[1]) {
12515                 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
12516                         struct wireless_dev *wdev = info->user_ptr[1];
12517
12518                         if (wdev->netdev)
12519                                 dev_put(wdev->netdev);
12520                 } else {
12521                         dev_put(info->user_ptr[1]);
12522                 }
12523         }
12524
12525         if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
12526                 rtnl_unlock();
12527
12528         /* If needed, clear the netlink message payload from the SKB
12529          * as it might contain key data that shouldn't stick around on
12530          * the heap after the SKB is freed. The netlink message header
12531          * is still needed for further processing, so leave it intact.
12532          */
12533         if (ops->internal_flags & NL80211_FLAG_CLEAR_SKB) {
12534                 struct nlmsghdr *nlh = nlmsg_hdr(skb);
12535
12536                 memset(nlmsg_data(nlh), 0, nlmsg_len(nlh));
12537         }
12538 }
12539
12540 static const struct genl_ops nl80211_ops[] = {
12541         {
12542                 .cmd = NL80211_CMD_GET_WIPHY,
12543                 .doit = nl80211_get_wiphy,
12544                 .dumpit = nl80211_dump_wiphy,
12545                 .done = nl80211_dump_wiphy_done,
12546                 .policy = nl80211_policy,
12547                 /* can be retrieved by unprivileged users */
12548                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12549                                   NL80211_FLAG_NEED_RTNL,
12550         },
12551         {
12552                 .cmd = NL80211_CMD_SET_WIPHY,
12553                 .doit = nl80211_set_wiphy,
12554                 .policy = nl80211_policy,
12555                 .flags = GENL_UNS_ADMIN_PERM,
12556                 .internal_flags = NL80211_FLAG_NEED_RTNL,
12557         },
12558         {
12559                 .cmd = NL80211_CMD_GET_INTERFACE,
12560                 .doit = nl80211_get_interface,
12561                 .dumpit = nl80211_dump_interface,
12562                 .policy = nl80211_policy,
12563                 /* can be retrieved by unprivileged users */
12564                 .internal_flags = NL80211_FLAG_NEED_WDEV |
12565                                   NL80211_FLAG_NEED_RTNL,
12566         },
12567         {
12568                 .cmd = NL80211_CMD_SET_INTERFACE,
12569                 .doit = nl80211_set_interface,
12570                 .policy = nl80211_policy,
12571                 .flags = GENL_UNS_ADMIN_PERM,
12572                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12573                                   NL80211_FLAG_NEED_RTNL,
12574         },
12575         {
12576                 .cmd = NL80211_CMD_NEW_INTERFACE,
12577                 .doit = nl80211_new_interface,
12578                 .policy = nl80211_policy,
12579                 .flags = GENL_UNS_ADMIN_PERM,
12580                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12581                                   NL80211_FLAG_NEED_RTNL,
12582         },
12583         {
12584                 .cmd = NL80211_CMD_DEL_INTERFACE,
12585                 .doit = nl80211_del_interface,
12586                 .policy = nl80211_policy,
12587                 .flags = GENL_UNS_ADMIN_PERM,
12588                 .internal_flags = NL80211_FLAG_NEED_WDEV |
12589                                   NL80211_FLAG_NEED_RTNL,
12590         },
12591         {
12592                 .cmd = NL80211_CMD_GET_KEY,
12593                 .doit = nl80211_get_key,
12594                 .policy = nl80211_policy,
12595                 .flags = GENL_UNS_ADMIN_PERM,
12596                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12597                                   NL80211_FLAG_NEED_RTNL,
12598         },
12599         {
12600                 .cmd = NL80211_CMD_SET_KEY,
12601                 .doit = nl80211_set_key,
12602                 .policy = nl80211_policy,
12603                 .flags = GENL_UNS_ADMIN_PERM,
12604                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12605                                   NL80211_FLAG_NEED_RTNL |
12606                                   NL80211_FLAG_CLEAR_SKB,
12607         },
12608         {
12609                 .cmd = NL80211_CMD_NEW_KEY,
12610                 .doit = nl80211_new_key,
12611                 .policy = nl80211_policy,
12612                 .flags = GENL_UNS_ADMIN_PERM,
12613                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12614                                   NL80211_FLAG_NEED_RTNL |
12615                                   NL80211_FLAG_CLEAR_SKB,
12616         },
12617         {
12618                 .cmd = NL80211_CMD_DEL_KEY,
12619                 .doit = nl80211_del_key,
12620                 .policy = nl80211_policy,
12621                 .flags = GENL_UNS_ADMIN_PERM,
12622                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12623                                   NL80211_FLAG_NEED_RTNL,
12624         },
12625         {
12626                 .cmd = NL80211_CMD_SET_BEACON,
12627                 .policy = nl80211_policy,
12628                 .flags = GENL_UNS_ADMIN_PERM,
12629                 .doit = nl80211_set_beacon,
12630                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12631                                   NL80211_FLAG_NEED_RTNL,
12632         },
12633         {
12634                 .cmd = NL80211_CMD_START_AP,
12635                 .policy = nl80211_policy,
12636                 .flags = GENL_UNS_ADMIN_PERM,
12637                 .doit = nl80211_start_ap,
12638                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12639                                   NL80211_FLAG_NEED_RTNL,
12640         },
12641         {
12642                 .cmd = NL80211_CMD_STOP_AP,
12643                 .policy = nl80211_policy,
12644                 .flags = GENL_UNS_ADMIN_PERM,
12645                 .doit = nl80211_stop_ap,
12646                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12647                                   NL80211_FLAG_NEED_RTNL,
12648         },
12649         {
12650                 .cmd = NL80211_CMD_GET_STATION,
12651                 .doit = nl80211_get_station,
12652                 .dumpit = nl80211_dump_station,
12653                 .policy = nl80211_policy,
12654                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12655                                   NL80211_FLAG_NEED_RTNL,
12656         },
12657         {
12658                 .cmd = NL80211_CMD_SET_STATION,
12659                 .doit = nl80211_set_station,
12660                 .policy = nl80211_policy,
12661                 .flags = GENL_UNS_ADMIN_PERM,
12662                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12663                                   NL80211_FLAG_NEED_RTNL,
12664         },
12665         {
12666                 .cmd = NL80211_CMD_NEW_STATION,
12667                 .doit = nl80211_new_station,
12668                 .policy = nl80211_policy,
12669                 .flags = GENL_UNS_ADMIN_PERM,
12670                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12671                                   NL80211_FLAG_NEED_RTNL,
12672         },
12673         {
12674                 .cmd = NL80211_CMD_DEL_STATION,
12675                 .doit = nl80211_del_station,
12676                 .policy = nl80211_policy,
12677                 .flags = GENL_UNS_ADMIN_PERM,
12678                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12679                                   NL80211_FLAG_NEED_RTNL,
12680         },
12681         {
12682                 .cmd = NL80211_CMD_GET_MPATH,
12683                 .doit = nl80211_get_mpath,
12684                 .dumpit = nl80211_dump_mpath,
12685                 .policy = nl80211_policy,
12686                 .flags = GENL_UNS_ADMIN_PERM,
12687                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12688                                   NL80211_FLAG_NEED_RTNL,
12689         },
12690         {
12691                 .cmd = NL80211_CMD_GET_MPP,
12692                 .doit = nl80211_get_mpp,
12693                 .dumpit = nl80211_dump_mpp,
12694                 .policy = nl80211_policy,
12695                 .flags = GENL_UNS_ADMIN_PERM,
12696                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12697                                   NL80211_FLAG_NEED_RTNL,
12698         },
12699         {
12700                 .cmd = NL80211_CMD_SET_MPATH,
12701                 .doit = nl80211_set_mpath,
12702                 .policy = nl80211_policy,
12703                 .flags = GENL_UNS_ADMIN_PERM,
12704                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12705                                   NL80211_FLAG_NEED_RTNL,
12706         },
12707         {
12708                 .cmd = NL80211_CMD_NEW_MPATH,
12709                 .doit = nl80211_new_mpath,
12710                 .policy = nl80211_policy,
12711                 .flags = GENL_UNS_ADMIN_PERM,
12712                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12713                                   NL80211_FLAG_NEED_RTNL,
12714         },
12715         {
12716                 .cmd = NL80211_CMD_DEL_MPATH,
12717                 .doit = nl80211_del_mpath,
12718                 .policy = nl80211_policy,
12719                 .flags = GENL_UNS_ADMIN_PERM,
12720                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12721                                   NL80211_FLAG_NEED_RTNL,
12722         },
12723         {
12724                 .cmd = NL80211_CMD_SET_BSS,
12725                 .doit = nl80211_set_bss,
12726                 .policy = nl80211_policy,
12727                 .flags = GENL_UNS_ADMIN_PERM,
12728                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12729                                   NL80211_FLAG_NEED_RTNL,
12730         },
12731         {
12732                 .cmd = NL80211_CMD_GET_REG,
12733                 .doit = nl80211_get_reg_do,
12734                 .dumpit = nl80211_get_reg_dump,
12735                 .policy = nl80211_policy,
12736                 .internal_flags = NL80211_FLAG_NEED_RTNL,
12737                 /* can be retrieved by unprivileged users */
12738         },
12739 #ifdef CONFIG_CFG80211_CRDA_SUPPORT
12740         {
12741                 .cmd = NL80211_CMD_SET_REG,
12742                 .doit = nl80211_set_reg,
12743                 .policy = nl80211_policy,
12744                 .flags = GENL_ADMIN_PERM,
12745                 .internal_flags = NL80211_FLAG_NEED_RTNL,
12746         },
12747 #endif
12748         {
12749                 .cmd = NL80211_CMD_REQ_SET_REG,
12750                 .doit = nl80211_req_set_reg,
12751                 .policy = nl80211_policy,
12752                 .flags = GENL_ADMIN_PERM,
12753         },
12754         {
12755                 .cmd = NL80211_CMD_GET_MESH_CONFIG,
12756                 .doit = nl80211_get_mesh_config,
12757                 .policy = nl80211_policy,
12758                 /* can be retrieved by unprivileged users */
12759                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12760                                   NL80211_FLAG_NEED_RTNL,
12761         },
12762         {
12763                 .cmd = NL80211_CMD_SET_MESH_CONFIG,
12764                 .doit = nl80211_update_mesh_config,
12765                 .policy = nl80211_policy,
12766                 .flags = GENL_UNS_ADMIN_PERM,
12767                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12768                                   NL80211_FLAG_NEED_RTNL,
12769         },
12770         {
12771                 .cmd = NL80211_CMD_TRIGGER_SCAN,
12772                 .doit = nl80211_trigger_scan,
12773                 .policy = nl80211_policy,
12774                 .flags = GENL_UNS_ADMIN_PERM,
12775                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12776                                   NL80211_FLAG_NEED_RTNL,
12777         },
12778         {
12779                 .cmd = NL80211_CMD_ABORT_SCAN,
12780                 .doit = nl80211_abort_scan,
12781                 .policy = nl80211_policy,
12782                 .flags = GENL_UNS_ADMIN_PERM,
12783                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12784                                   NL80211_FLAG_NEED_RTNL,
12785         },
12786         {
12787                 .cmd = NL80211_CMD_GET_SCAN,
12788                 .policy = nl80211_policy,
12789                 .dumpit = nl80211_dump_scan,
12790         },
12791         {
12792                 .cmd = NL80211_CMD_START_SCHED_SCAN,
12793                 .doit = nl80211_start_sched_scan,
12794                 .policy = nl80211_policy,
12795                 .flags = GENL_UNS_ADMIN_PERM,
12796                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12797                                   NL80211_FLAG_NEED_RTNL,
12798         },
12799         {
12800                 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
12801                 .doit = nl80211_stop_sched_scan,
12802                 .policy = nl80211_policy,
12803                 .flags = GENL_UNS_ADMIN_PERM,
12804                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12805                                   NL80211_FLAG_NEED_RTNL,
12806         },
12807         {
12808                 .cmd = NL80211_CMD_AUTHENTICATE,
12809                 .doit = nl80211_authenticate,
12810                 .policy = nl80211_policy,
12811                 .flags = GENL_UNS_ADMIN_PERM,
12812                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12813                                   NL80211_FLAG_NEED_RTNL |
12814                                   NL80211_FLAG_CLEAR_SKB,
12815         },
12816         {
12817                 .cmd = NL80211_CMD_ASSOCIATE,
12818                 .doit = nl80211_associate,
12819                 .policy = nl80211_policy,
12820                 .flags = GENL_UNS_ADMIN_PERM,
12821                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12822                                   NL80211_FLAG_NEED_RTNL |
12823                                   NL80211_FLAG_CLEAR_SKB,
12824         },
12825         {
12826                 .cmd = NL80211_CMD_DEAUTHENTICATE,
12827                 .doit = nl80211_deauthenticate,
12828                 .policy = nl80211_policy,
12829                 .flags = GENL_UNS_ADMIN_PERM,
12830                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12831                                   NL80211_FLAG_NEED_RTNL,
12832         },
12833         {
12834                 .cmd = NL80211_CMD_DISASSOCIATE,
12835                 .doit = nl80211_disassociate,
12836                 .policy = nl80211_policy,
12837                 .flags = GENL_UNS_ADMIN_PERM,
12838                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12839                                   NL80211_FLAG_NEED_RTNL,
12840         },
12841         {
12842                 .cmd = NL80211_CMD_JOIN_IBSS,
12843                 .doit = nl80211_join_ibss,
12844                 .policy = nl80211_policy,
12845                 .flags = GENL_UNS_ADMIN_PERM,
12846                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12847                                   NL80211_FLAG_NEED_RTNL,
12848         },
12849         {
12850                 .cmd = NL80211_CMD_LEAVE_IBSS,
12851                 .doit = nl80211_leave_ibss,
12852                 .policy = nl80211_policy,
12853                 .flags = GENL_UNS_ADMIN_PERM,
12854                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12855                                   NL80211_FLAG_NEED_RTNL,
12856         },
12857 #ifdef CONFIG_NL80211_TESTMODE
12858         {
12859                 .cmd = NL80211_CMD_TESTMODE,
12860                 .doit = nl80211_testmode_do,
12861                 .dumpit = nl80211_testmode_dump,
12862                 .policy = nl80211_policy,
12863                 .flags = GENL_UNS_ADMIN_PERM,
12864                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12865                                   NL80211_FLAG_NEED_RTNL,
12866         },
12867 #endif
12868         {
12869                 .cmd = NL80211_CMD_CONNECT,
12870                 .doit = nl80211_connect,
12871                 .policy = nl80211_policy,
12872                 .flags = GENL_UNS_ADMIN_PERM,
12873                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12874                                   NL80211_FLAG_NEED_RTNL |
12875                                   NL80211_FLAG_CLEAR_SKB,
12876         },
12877         {
12878                 .cmd = NL80211_CMD_UPDATE_CONNECT_PARAMS,
12879                 .doit = nl80211_update_connect_params,
12880                 .policy = nl80211_policy,
12881                 .flags = GENL_ADMIN_PERM,
12882                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12883                                   NL80211_FLAG_NEED_RTNL |
12884                                   NL80211_FLAG_CLEAR_SKB,
12885         },
12886         {
12887                 .cmd = NL80211_CMD_DISCONNECT,
12888                 .doit = nl80211_disconnect,
12889                 .policy = nl80211_policy,
12890                 .flags = GENL_UNS_ADMIN_PERM,
12891                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12892                                   NL80211_FLAG_NEED_RTNL,
12893         },
12894         {
12895                 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
12896                 .doit = nl80211_wiphy_netns,
12897                 .policy = nl80211_policy,
12898                 .flags = GENL_UNS_ADMIN_PERM,
12899                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
12900                                   NL80211_FLAG_NEED_RTNL,
12901         },
12902         {
12903                 .cmd = NL80211_CMD_GET_SURVEY,
12904                 .policy = nl80211_policy,
12905                 .dumpit = nl80211_dump_survey,
12906         },
12907         {
12908                 .cmd = NL80211_CMD_SET_PMKSA,
12909                 .doit = nl80211_setdel_pmksa,
12910                 .policy = nl80211_policy,
12911                 .flags = GENL_UNS_ADMIN_PERM,
12912                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12913                                   NL80211_FLAG_NEED_RTNL |
12914                                   NL80211_FLAG_CLEAR_SKB,
12915         },
12916         {
12917                 .cmd = NL80211_CMD_DEL_PMKSA,
12918                 .doit = nl80211_setdel_pmksa,
12919                 .policy = nl80211_policy,
12920                 .flags = GENL_UNS_ADMIN_PERM,
12921                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12922                                   NL80211_FLAG_NEED_RTNL,
12923         },
12924         {
12925                 .cmd = NL80211_CMD_FLUSH_PMKSA,
12926                 .doit = nl80211_flush_pmksa,
12927                 .policy = nl80211_policy,
12928                 .flags = GENL_UNS_ADMIN_PERM,
12929                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
12930                                   NL80211_FLAG_NEED_RTNL,
12931         },
12932         {
12933                 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
12934                 .doit = nl80211_remain_on_channel,
12935                 .policy = nl80211_policy,
12936                 .flags = GENL_UNS_ADMIN_PERM,
12937                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12938                                   NL80211_FLAG_NEED_RTNL,
12939         },
12940         {
12941                 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
12942                 .doit = nl80211_cancel_remain_on_channel,
12943                 .policy = nl80211_policy,
12944                 .flags = GENL_UNS_ADMIN_PERM,
12945                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12946                                   NL80211_FLAG_NEED_RTNL,
12947         },
12948         {
12949                 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
12950                 .doit = nl80211_set_tx_bitrate_mask,
12951                 .policy = nl80211_policy,
12952                 .flags = GENL_UNS_ADMIN_PERM,
12953                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12954                                   NL80211_FLAG_NEED_RTNL,
12955         },
12956         {
12957                 .cmd = NL80211_CMD_REGISTER_FRAME,
12958                 .doit = nl80211_register_mgmt,
12959                 .policy = nl80211_policy,
12960                 .flags = GENL_UNS_ADMIN_PERM,
12961                 .internal_flags = NL80211_FLAG_NEED_WDEV |
12962                                   NL80211_FLAG_NEED_RTNL,
12963         },
12964         {
12965                 .cmd = NL80211_CMD_FRAME,
12966                 .doit = nl80211_tx_mgmt,
12967                 .policy = nl80211_policy,
12968                 .flags = GENL_UNS_ADMIN_PERM,
12969                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12970                                   NL80211_FLAG_NEED_RTNL,
12971         },
12972         {
12973                 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
12974                 .doit = nl80211_tx_mgmt_cancel_wait,
12975                 .policy = nl80211_policy,
12976                 .flags = GENL_UNS_ADMIN_PERM,
12977                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
12978                                   NL80211_FLAG_NEED_RTNL,
12979         },
12980         {
12981                 .cmd = NL80211_CMD_SET_POWER_SAVE,
12982                 .doit = nl80211_set_power_save,
12983                 .policy = nl80211_policy,
12984                 .flags = GENL_UNS_ADMIN_PERM,
12985                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12986                                   NL80211_FLAG_NEED_RTNL,
12987         },
12988         {
12989                 .cmd = NL80211_CMD_GET_POWER_SAVE,
12990                 .doit = nl80211_get_power_save,
12991                 .policy = nl80211_policy,
12992                 /* can be retrieved by unprivileged users */
12993                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
12994                                   NL80211_FLAG_NEED_RTNL,
12995         },
12996         {
12997                 .cmd = NL80211_CMD_SET_CQM,
12998                 .doit = nl80211_set_cqm,
12999                 .policy = nl80211_policy,
13000                 .flags = GENL_UNS_ADMIN_PERM,
13001                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13002                                   NL80211_FLAG_NEED_RTNL,
13003         },
13004         {
13005                 .cmd = NL80211_CMD_SET_CHANNEL,
13006                 .doit = nl80211_set_channel,
13007                 .policy = nl80211_policy,
13008                 .flags = GENL_UNS_ADMIN_PERM,
13009                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13010                                   NL80211_FLAG_NEED_RTNL,
13011         },
13012         {
13013                 .cmd = NL80211_CMD_SET_WDS_PEER,
13014                 .doit = nl80211_set_wds_peer,
13015                 .policy = nl80211_policy,
13016                 .flags = GENL_UNS_ADMIN_PERM,
13017                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13018                                   NL80211_FLAG_NEED_RTNL,
13019         },
13020         {
13021                 .cmd = NL80211_CMD_JOIN_MESH,
13022                 .doit = nl80211_join_mesh,
13023                 .policy = nl80211_policy,
13024                 .flags = GENL_UNS_ADMIN_PERM,
13025                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13026                                   NL80211_FLAG_NEED_RTNL,
13027         },
13028         {
13029                 .cmd = NL80211_CMD_LEAVE_MESH,
13030                 .doit = nl80211_leave_mesh,
13031                 .policy = nl80211_policy,
13032                 .flags = GENL_UNS_ADMIN_PERM,
13033                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13034                                   NL80211_FLAG_NEED_RTNL,
13035         },
13036         {
13037                 .cmd = NL80211_CMD_JOIN_OCB,
13038                 .doit = nl80211_join_ocb,
13039                 .policy = nl80211_policy,
13040                 .flags = GENL_UNS_ADMIN_PERM,
13041                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13042                                   NL80211_FLAG_NEED_RTNL,
13043         },
13044         {
13045                 .cmd = NL80211_CMD_LEAVE_OCB,
13046                 .doit = nl80211_leave_ocb,
13047                 .policy = nl80211_policy,
13048                 .flags = GENL_UNS_ADMIN_PERM,
13049                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13050                                   NL80211_FLAG_NEED_RTNL,
13051         },
13052 #ifdef CONFIG_PM
13053         {
13054                 .cmd = NL80211_CMD_GET_WOWLAN,
13055                 .doit = nl80211_get_wowlan,
13056                 .policy = nl80211_policy,
13057                 /* can be retrieved by unprivileged users */
13058                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13059                                   NL80211_FLAG_NEED_RTNL,
13060         },
13061         {
13062                 .cmd = NL80211_CMD_SET_WOWLAN,
13063                 .doit = nl80211_set_wowlan,
13064                 .policy = nl80211_policy,
13065                 .flags = GENL_UNS_ADMIN_PERM,
13066                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13067                                   NL80211_FLAG_NEED_RTNL,
13068         },
13069 #endif
13070         {
13071                 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
13072                 .doit = nl80211_set_rekey_data,
13073                 .policy = nl80211_policy,
13074                 .flags = GENL_UNS_ADMIN_PERM,
13075                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13076                                   NL80211_FLAG_NEED_RTNL |
13077                                   NL80211_FLAG_CLEAR_SKB,
13078         },
13079         {
13080                 .cmd = NL80211_CMD_TDLS_MGMT,
13081                 .doit = nl80211_tdls_mgmt,
13082                 .policy = nl80211_policy,
13083                 .flags = GENL_UNS_ADMIN_PERM,
13084                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13085                                   NL80211_FLAG_NEED_RTNL,
13086         },
13087         {
13088                 .cmd = NL80211_CMD_TDLS_OPER,
13089                 .doit = nl80211_tdls_oper,
13090                 .policy = nl80211_policy,
13091                 .flags = GENL_UNS_ADMIN_PERM,
13092                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13093                                   NL80211_FLAG_NEED_RTNL,
13094         },
13095         {
13096                 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
13097                 .doit = nl80211_register_unexpected_frame,
13098                 .policy = nl80211_policy,
13099                 .flags = GENL_UNS_ADMIN_PERM,
13100                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13101                                   NL80211_FLAG_NEED_RTNL,
13102         },
13103         {
13104                 .cmd = NL80211_CMD_PROBE_CLIENT,
13105                 .doit = nl80211_probe_client,
13106                 .policy = nl80211_policy,
13107                 .flags = GENL_UNS_ADMIN_PERM,
13108                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13109                                   NL80211_FLAG_NEED_RTNL,
13110         },
13111         {
13112                 .cmd = NL80211_CMD_REGISTER_BEACONS,
13113                 .doit = nl80211_register_beacons,
13114                 .policy = nl80211_policy,
13115                 .flags = GENL_UNS_ADMIN_PERM,
13116                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13117                                   NL80211_FLAG_NEED_RTNL,
13118         },
13119         {
13120                 .cmd = NL80211_CMD_SET_NOACK_MAP,
13121                 .doit = nl80211_set_noack_map,
13122                 .policy = nl80211_policy,
13123                 .flags = GENL_UNS_ADMIN_PERM,
13124                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13125                                   NL80211_FLAG_NEED_RTNL,
13126         },
13127         {
13128                 .cmd = NL80211_CMD_START_P2P_DEVICE,
13129                 .doit = nl80211_start_p2p_device,
13130                 .policy = nl80211_policy,
13131                 .flags = GENL_UNS_ADMIN_PERM,
13132                 .internal_flags = NL80211_FLAG_NEED_WDEV |
13133                                   NL80211_FLAG_NEED_RTNL,
13134         },
13135         {
13136                 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
13137                 .doit = nl80211_stop_p2p_device,
13138                 .policy = nl80211_policy,
13139                 .flags = GENL_UNS_ADMIN_PERM,
13140                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13141                                   NL80211_FLAG_NEED_RTNL,
13142         },
13143         {
13144                 .cmd = NL80211_CMD_START_NAN,
13145                 .doit = nl80211_start_nan,
13146                 .policy = nl80211_policy,
13147                 .flags = GENL_ADMIN_PERM,
13148                 .internal_flags = NL80211_FLAG_NEED_WDEV |
13149                                   NL80211_FLAG_NEED_RTNL,
13150         },
13151         {
13152                 .cmd = NL80211_CMD_STOP_NAN,
13153                 .doit = nl80211_stop_nan,
13154                 .policy = nl80211_policy,
13155                 .flags = GENL_ADMIN_PERM,
13156                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13157                                   NL80211_FLAG_NEED_RTNL,
13158         },
13159         {
13160                 .cmd = NL80211_CMD_ADD_NAN_FUNCTION,
13161                 .doit = nl80211_nan_add_func,
13162                 .policy = nl80211_policy,
13163                 .flags = GENL_ADMIN_PERM,
13164                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13165                                   NL80211_FLAG_NEED_RTNL,
13166         },
13167         {
13168                 .cmd = NL80211_CMD_DEL_NAN_FUNCTION,
13169                 .doit = nl80211_nan_del_func,
13170                 .policy = nl80211_policy,
13171                 .flags = GENL_ADMIN_PERM,
13172                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13173                                   NL80211_FLAG_NEED_RTNL,
13174         },
13175         {
13176                 .cmd = NL80211_CMD_CHANGE_NAN_CONFIG,
13177                 .doit = nl80211_nan_change_config,
13178                 .policy = nl80211_policy,
13179                 .flags = GENL_ADMIN_PERM,
13180                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13181                                   NL80211_FLAG_NEED_RTNL,
13182         },
13183         {
13184                 .cmd = NL80211_CMD_SET_MCAST_RATE,
13185                 .doit = nl80211_set_mcast_rate,
13186                 .policy = nl80211_policy,
13187                 .flags = GENL_UNS_ADMIN_PERM,
13188                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13189                                   NL80211_FLAG_NEED_RTNL,
13190         },
13191         {
13192                 .cmd = NL80211_CMD_SET_MAC_ACL,
13193                 .doit = nl80211_set_mac_acl,
13194                 .policy = nl80211_policy,
13195                 .flags = GENL_UNS_ADMIN_PERM,
13196                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13197                                   NL80211_FLAG_NEED_RTNL,
13198         },
13199         {
13200                 .cmd = NL80211_CMD_RADAR_DETECT,
13201                 .doit = nl80211_start_radar_detection,
13202                 .policy = nl80211_policy,
13203                 .flags = GENL_UNS_ADMIN_PERM,
13204                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13205                                   NL80211_FLAG_NEED_RTNL,
13206         },
13207         {
13208                 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
13209                 .doit = nl80211_get_protocol_features,
13210                 .policy = nl80211_policy,
13211         },
13212         {
13213                 .cmd = NL80211_CMD_UPDATE_FT_IES,
13214                 .doit = nl80211_update_ft_ies,
13215                 .policy = nl80211_policy,
13216                 .flags = GENL_UNS_ADMIN_PERM,
13217                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13218                                   NL80211_FLAG_NEED_RTNL,
13219         },
13220         {
13221                 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
13222                 .doit = nl80211_crit_protocol_start,
13223                 .policy = nl80211_policy,
13224                 .flags = GENL_UNS_ADMIN_PERM,
13225                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13226                                   NL80211_FLAG_NEED_RTNL,
13227         },
13228         {
13229                 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
13230                 .doit = nl80211_crit_protocol_stop,
13231                 .policy = nl80211_policy,
13232                 .flags = GENL_UNS_ADMIN_PERM,
13233                 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
13234                                   NL80211_FLAG_NEED_RTNL,
13235         },
13236         {
13237                 .cmd = NL80211_CMD_GET_COALESCE,
13238                 .doit = nl80211_get_coalesce,
13239                 .policy = nl80211_policy,
13240                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13241                                   NL80211_FLAG_NEED_RTNL,
13242         },
13243         {
13244                 .cmd = NL80211_CMD_SET_COALESCE,
13245                 .doit = nl80211_set_coalesce,
13246                 .policy = nl80211_policy,
13247                 .flags = GENL_UNS_ADMIN_PERM,
13248                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13249                                   NL80211_FLAG_NEED_RTNL,
13250         },
13251         {
13252                 .cmd = NL80211_CMD_CHANNEL_SWITCH,
13253                 .doit = nl80211_channel_switch,
13254                 .policy = nl80211_policy,
13255                 .flags = GENL_UNS_ADMIN_PERM,
13256                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13257                                   NL80211_FLAG_NEED_RTNL,
13258         },
13259         {
13260                 .cmd = NL80211_CMD_VENDOR,
13261                 .doit = nl80211_vendor_cmd,
13262                 .dumpit = nl80211_vendor_cmd_dump,
13263                 .policy = nl80211_policy,
13264                 .flags = GENL_UNS_ADMIN_PERM,
13265                 .internal_flags = NL80211_FLAG_NEED_WIPHY |
13266                                   NL80211_FLAG_NEED_RTNL |
13267                                   NL80211_FLAG_CLEAR_SKB,
13268         },
13269         {
13270                 .cmd = NL80211_CMD_SET_QOS_MAP,
13271                 .doit = nl80211_set_qos_map,
13272                 .policy = nl80211_policy,
13273                 .flags = GENL_UNS_ADMIN_PERM,
13274                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13275                                   NL80211_FLAG_NEED_RTNL,
13276         },
13277         {
13278                 .cmd = NL80211_CMD_ADD_TX_TS,
13279                 .doit = nl80211_add_tx_ts,
13280                 .policy = nl80211_policy,
13281                 .flags = GENL_UNS_ADMIN_PERM,
13282                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13283                                   NL80211_FLAG_NEED_RTNL,
13284         },
13285         {
13286                 .cmd = NL80211_CMD_DEL_TX_TS,
13287                 .doit = nl80211_del_tx_ts,
13288                 .policy = nl80211_policy,
13289                 .flags = GENL_UNS_ADMIN_PERM,
13290                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13291                                   NL80211_FLAG_NEED_RTNL,
13292         },
13293         {
13294                 .cmd = NL80211_CMD_TDLS_CHANNEL_SWITCH,
13295                 .doit = nl80211_tdls_channel_switch,
13296                 .policy = nl80211_policy,
13297                 .flags = GENL_UNS_ADMIN_PERM,
13298                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13299                                   NL80211_FLAG_NEED_RTNL,
13300         },
13301         {
13302                 .cmd = NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH,
13303                 .doit = nl80211_tdls_cancel_channel_switch,
13304                 .policy = nl80211_policy,
13305                 .flags = GENL_UNS_ADMIN_PERM,
13306                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13307                                   NL80211_FLAG_NEED_RTNL,
13308         },
13309         {
13310                 .cmd = NL80211_CMD_SET_MULTICAST_TO_UNICAST,
13311                 .doit = nl80211_set_multicast_to_unicast,
13312                 .policy = nl80211_policy,
13313                 .flags = GENL_UNS_ADMIN_PERM,
13314                 .internal_flags = NL80211_FLAG_NEED_NETDEV |
13315                                   NL80211_FLAG_NEED_RTNL,
13316         },
13317         {
13318                 .cmd = NL80211_CMD_SET_PMK,
13319                 .doit = nl80211_set_pmk,
13320                 .policy = nl80211_policy,
13321                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13322                                   NL80211_FLAG_NEED_RTNL |
13323                                   NL80211_FLAG_CLEAR_SKB,
13324         },
13325         {
13326                 .cmd = NL80211_CMD_DEL_PMK,
13327                 .doit = nl80211_del_pmk,
13328                 .policy = nl80211_policy,
13329                 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
13330                                   NL80211_FLAG_NEED_RTNL,
13331         },
13332
13333 };
13334
13335 static struct genl_family nl80211_fam __ro_after_init = {
13336         .name = NL80211_GENL_NAME,      /* have users key off the name instead */
13337         .hdrsize = 0,                   /* no private header */
13338         .version = 1,                   /* no particular meaning now */
13339         .maxattr = NL80211_ATTR_MAX,
13340         .netnsok = true,
13341         .pre_doit = nl80211_pre_doit,
13342         .post_doit = nl80211_post_doit,
13343         .module = THIS_MODULE,
13344         .ops = nl80211_ops,
13345         .n_ops = ARRAY_SIZE(nl80211_ops),
13346         .mcgrps = nl80211_mcgrps,
13347         .n_mcgrps = ARRAY_SIZE(nl80211_mcgrps),
13348 };
13349
13350 /* notification functions */
13351
13352 void nl80211_notify_wiphy(struct cfg80211_registered_device *rdev,
13353                           enum nl80211_commands cmd)
13354 {
13355         struct sk_buff *msg;
13356         struct nl80211_dump_wiphy_state state = {};
13357
13358         WARN_ON(cmd != NL80211_CMD_NEW_WIPHY &&
13359                 cmd != NL80211_CMD_DEL_WIPHY);
13360
13361         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13362         if (!msg)
13363                 return;
13364
13365         if (nl80211_send_wiphy(rdev, cmd, msg, 0, 0, 0, &state) < 0) {
13366                 nlmsg_free(msg);
13367                 return;
13368         }
13369
13370         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13371                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
13372 }
13373
13374 void nl80211_notify_iface(struct cfg80211_registered_device *rdev,
13375                                 struct wireless_dev *wdev,
13376                                 enum nl80211_commands cmd)
13377 {
13378         struct sk_buff *msg;
13379
13380         WARN_ON(cmd != NL80211_CMD_NEW_INTERFACE &&
13381                 cmd != NL80211_CMD_DEL_INTERFACE);
13382
13383         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13384         if (!msg)
13385                 return;
13386
13387         if (nl80211_send_iface(msg, 0, 0, 0, rdev, wdev,
13388                                cmd == NL80211_CMD_DEL_INTERFACE) < 0) {
13389                 nlmsg_free(msg);
13390                 return;
13391         }
13392
13393         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13394                                 NL80211_MCGRP_CONFIG, GFP_KERNEL);
13395 }
13396
13397 static int nl80211_add_scan_req(struct sk_buff *msg,
13398                                 struct cfg80211_registered_device *rdev)
13399 {
13400         struct cfg80211_scan_request *req = rdev->scan_req;
13401         struct nlattr *nest;
13402         int i;
13403
13404         if (WARN_ON(!req))
13405                 return 0;
13406
13407         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
13408         if (!nest)
13409                 goto nla_put_failure;
13410         for (i = 0; i < req->n_ssids; i++) {
13411                 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
13412                         goto nla_put_failure;
13413         }
13414         nla_nest_end(msg, nest);
13415
13416         nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
13417         if (!nest)
13418                 goto nla_put_failure;
13419         for (i = 0; i < req->n_channels; i++) {
13420                 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
13421                         goto nla_put_failure;
13422         }
13423         nla_nest_end(msg, nest);
13424
13425         if (req->ie &&
13426             nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
13427                 goto nla_put_failure;
13428
13429         if (req->flags &&
13430             nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
13431                 goto nla_put_failure;
13432
13433         if (req->info.scan_start_tsf &&
13434             (nla_put_u64_64bit(msg, NL80211_ATTR_SCAN_START_TIME_TSF,
13435                                req->info.scan_start_tsf, NL80211_BSS_PAD) ||
13436              nla_put(msg, NL80211_ATTR_SCAN_START_TIME_TSF_BSSID, ETH_ALEN,
13437                      req->info.tsf_bssid)))
13438                 goto nla_put_failure;
13439
13440         return 0;
13441  nla_put_failure:
13442         return -ENOBUFS;
13443 }
13444
13445 static int nl80211_prep_scan_msg(struct sk_buff *msg,
13446                                  struct cfg80211_registered_device *rdev,
13447                                  struct wireless_dev *wdev,
13448                                  u32 portid, u32 seq, int flags,
13449                                  u32 cmd)
13450 {
13451         void *hdr;
13452
13453         hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
13454         if (!hdr)
13455                 return -1;
13456
13457         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13458             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
13459                                          wdev->netdev->ifindex)) ||
13460             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
13461                               NL80211_ATTR_PAD))
13462                 goto nla_put_failure;
13463
13464         /* ignore errors and send incomplete event anyway */
13465         nl80211_add_scan_req(msg, rdev);
13466
13467         genlmsg_end(msg, hdr);
13468         return 0;
13469
13470  nla_put_failure:
13471         genlmsg_cancel(msg, hdr);
13472         return -EMSGSIZE;
13473 }
13474
13475 static int
13476 nl80211_prep_sched_scan_msg(struct sk_buff *msg,
13477                             struct cfg80211_sched_scan_request *req, u32 cmd)
13478 {
13479         void *hdr;
13480
13481         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13482         if (!hdr)
13483                 return -1;
13484
13485         if (nla_put_u32(msg, NL80211_ATTR_WIPHY,
13486                         wiphy_to_rdev(req->wiphy)->wiphy_idx) ||
13487             nla_put_u32(msg, NL80211_ATTR_IFINDEX, req->dev->ifindex) ||
13488             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, req->reqid,
13489                               NL80211_ATTR_PAD))
13490                 goto nla_put_failure;
13491
13492         genlmsg_end(msg, hdr);
13493         return 0;
13494
13495  nla_put_failure:
13496         genlmsg_cancel(msg, hdr);
13497         return -EMSGSIZE;
13498 }
13499
13500 void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
13501                              struct wireless_dev *wdev)
13502 {
13503         struct sk_buff *msg;
13504
13505         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13506         if (!msg)
13507                 return;
13508
13509         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13510                                   NL80211_CMD_TRIGGER_SCAN) < 0) {
13511                 nlmsg_free(msg);
13512                 return;
13513         }
13514
13515         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13516                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
13517 }
13518
13519 struct sk_buff *nl80211_build_scan_msg(struct cfg80211_registered_device *rdev,
13520                                        struct wireless_dev *wdev, bool aborted)
13521 {
13522         struct sk_buff *msg;
13523
13524         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13525         if (!msg)
13526                 return NULL;
13527
13528         if (nl80211_prep_scan_msg(msg, rdev, wdev, 0, 0, 0,
13529                                   aborted ? NL80211_CMD_SCAN_ABORTED :
13530                                             NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
13531                 nlmsg_free(msg);
13532                 return NULL;
13533         }
13534
13535         return msg;
13536 }
13537
13538 /* send message created by nl80211_build_scan_msg() */
13539 void nl80211_send_scan_msg(struct cfg80211_registered_device *rdev,
13540                            struct sk_buff *msg)
13541 {
13542         if (!msg)
13543                 return;
13544
13545         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13546                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
13547 }
13548
13549 void nl80211_send_sched_scan(struct cfg80211_sched_scan_request *req, u32 cmd)
13550 {
13551         struct sk_buff *msg;
13552
13553         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13554         if (!msg)
13555                 return;
13556
13557         if (nl80211_prep_sched_scan_msg(msg, req, cmd) < 0) {
13558                 nlmsg_free(msg);
13559                 return;
13560         }
13561
13562         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(req->wiphy), msg, 0,
13563                                 NL80211_MCGRP_SCAN, GFP_KERNEL);
13564 }
13565
13566 static bool nl80211_reg_change_event_fill(struct sk_buff *msg,
13567                                           struct regulatory_request *request)
13568 {
13569         /* Userspace can always count this one always being set */
13570         if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
13571                 goto nla_put_failure;
13572
13573         if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
13574                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13575                                NL80211_REGDOM_TYPE_WORLD))
13576                         goto nla_put_failure;
13577         } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
13578                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13579                                NL80211_REGDOM_TYPE_CUSTOM_WORLD))
13580                         goto nla_put_failure;
13581         } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
13582                    request->intersect) {
13583                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13584                                NL80211_REGDOM_TYPE_INTERSECTION))
13585                         goto nla_put_failure;
13586         } else {
13587                 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
13588                                NL80211_REGDOM_TYPE_COUNTRY) ||
13589                     nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
13590                                    request->alpha2))
13591                         goto nla_put_failure;
13592         }
13593
13594         if (request->wiphy_idx != WIPHY_IDX_INVALID) {
13595                 struct wiphy *wiphy = wiphy_idx_to_wiphy(request->wiphy_idx);
13596
13597                 if (wiphy &&
13598                     nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
13599                         goto nla_put_failure;
13600
13601                 if (wiphy &&
13602                     wiphy->regulatory_flags & REGULATORY_WIPHY_SELF_MANAGED &&
13603                     nla_put_flag(msg, NL80211_ATTR_WIPHY_SELF_MANAGED_REG))
13604                         goto nla_put_failure;
13605         }
13606
13607         return true;
13608
13609 nla_put_failure:
13610         return false;
13611 }
13612
13613 /*
13614  * This can happen on global regulatory changes or device specific settings
13615  * based on custom regulatory domains.
13616  */
13617 void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
13618                                      struct regulatory_request *request)
13619 {
13620         struct sk_buff *msg;
13621         void *hdr;
13622
13623         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
13624         if (!msg)
13625                 return;
13626
13627         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd_id);
13628         if (!hdr) {
13629                 nlmsg_free(msg);
13630                 return;
13631         }
13632
13633         if (nl80211_reg_change_event_fill(msg, request) == false)
13634                 goto nla_put_failure;
13635
13636         genlmsg_end(msg, hdr);
13637
13638         rcu_read_lock();
13639         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
13640                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
13641         rcu_read_unlock();
13642
13643         return;
13644
13645 nla_put_failure:
13646         genlmsg_cancel(msg, hdr);
13647         nlmsg_free(msg);
13648 }
13649
13650 static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
13651                                     struct net_device *netdev,
13652                                     const u8 *buf, size_t len,
13653                                     enum nl80211_commands cmd, gfp_t gfp,
13654                                     int uapsd_queues)
13655 {
13656         struct sk_buff *msg;
13657         void *hdr;
13658
13659         msg = nlmsg_new(100 + len, gfp);
13660         if (!msg)
13661                 return;
13662
13663         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13664         if (!hdr) {
13665                 nlmsg_free(msg);
13666                 return;
13667         }
13668
13669         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13670             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13671             nla_put(msg, NL80211_ATTR_FRAME, len, buf))
13672                 goto nla_put_failure;
13673
13674         if (uapsd_queues >= 0) {
13675                 struct nlattr *nla_wmm =
13676                         nla_nest_start(msg, NL80211_ATTR_STA_WME);
13677                 if (!nla_wmm)
13678                         goto nla_put_failure;
13679
13680                 if (nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
13681                                uapsd_queues))
13682                         goto nla_put_failure;
13683
13684                 nla_nest_end(msg, nla_wmm);
13685         }
13686
13687         genlmsg_end(msg, hdr);
13688
13689         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13690                                 NL80211_MCGRP_MLME, gfp);
13691         return;
13692
13693  nla_put_failure:
13694         genlmsg_cancel(msg, hdr);
13695         nlmsg_free(msg);
13696 }
13697
13698 void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
13699                           struct net_device *netdev, const u8 *buf,
13700                           size_t len, gfp_t gfp)
13701 {
13702         nl80211_send_mlme_event(rdev, netdev, buf, len,
13703                                 NL80211_CMD_AUTHENTICATE, gfp, -1);
13704 }
13705
13706 void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
13707                            struct net_device *netdev, const u8 *buf,
13708                            size_t len, gfp_t gfp, int uapsd_queues)
13709 {
13710         nl80211_send_mlme_event(rdev, netdev, buf, len,
13711                                 NL80211_CMD_ASSOCIATE, gfp, uapsd_queues);
13712 }
13713
13714 void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
13715                          struct net_device *netdev, const u8 *buf,
13716                          size_t len, gfp_t gfp)
13717 {
13718         nl80211_send_mlme_event(rdev, netdev, buf, len,
13719                                 NL80211_CMD_DEAUTHENTICATE, gfp, -1);
13720 }
13721
13722 void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
13723                            struct net_device *netdev, const u8 *buf,
13724                            size_t len, gfp_t gfp)
13725 {
13726         nl80211_send_mlme_event(rdev, netdev, buf, len,
13727                                 NL80211_CMD_DISASSOCIATE, gfp, -1);
13728 }
13729
13730 void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
13731                                   size_t len)
13732 {
13733         struct wireless_dev *wdev = dev->ieee80211_ptr;
13734         struct wiphy *wiphy = wdev->wiphy;
13735         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
13736         const struct ieee80211_mgmt *mgmt = (void *)buf;
13737         u32 cmd;
13738
13739         if (WARN_ON(len < 2))
13740                 return;
13741
13742         if (ieee80211_is_deauth(mgmt->frame_control))
13743                 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
13744         else
13745                 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
13746
13747         trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
13748         nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC, -1);
13749 }
13750 EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
13751
13752 static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
13753                                       struct net_device *netdev, int cmd,
13754                                       const u8 *addr, gfp_t gfp)
13755 {
13756         struct sk_buff *msg;
13757         void *hdr;
13758
13759         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13760         if (!msg)
13761                 return;
13762
13763         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
13764         if (!hdr) {
13765                 nlmsg_free(msg);
13766                 return;
13767         }
13768
13769         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13770             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13771             nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13772             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
13773                 goto nla_put_failure;
13774
13775         genlmsg_end(msg, hdr);
13776
13777         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13778                                 NL80211_MCGRP_MLME, gfp);
13779         return;
13780
13781  nla_put_failure:
13782         genlmsg_cancel(msg, hdr);
13783         nlmsg_free(msg);
13784 }
13785
13786 void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
13787                                struct net_device *netdev, const u8 *addr,
13788                                gfp_t gfp)
13789 {
13790         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
13791                                   addr, gfp);
13792 }
13793
13794 void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
13795                                 struct net_device *netdev, const u8 *addr,
13796                                 gfp_t gfp)
13797 {
13798         nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
13799                                   addr, gfp);
13800 }
13801
13802 void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
13803                                  struct net_device *netdev,
13804                                  struct cfg80211_connect_resp_params *cr,
13805                                  gfp_t gfp)
13806 {
13807         struct sk_buff *msg;
13808         void *hdr;
13809
13810         msg = nlmsg_new(100 + cr->req_ie_len + cr->resp_ie_len +
13811                         cr->fils_kek_len + cr->pmk_len +
13812                         (cr->pmkid ? WLAN_PMKID_LEN : 0), gfp);
13813         if (!msg)
13814                 return;
13815
13816         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
13817         if (!hdr) {
13818                 nlmsg_free(msg);
13819                 return;
13820         }
13821
13822         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13823             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13824             (cr->bssid &&
13825              nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, cr->bssid)) ||
13826             nla_put_u16(msg, NL80211_ATTR_STATUS_CODE,
13827                         cr->status < 0 ? WLAN_STATUS_UNSPECIFIED_FAILURE :
13828                         cr->status) ||
13829             (cr->status < 0 &&
13830              (nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
13831               nla_put_u32(msg, NL80211_ATTR_TIMEOUT_REASON,
13832                           cr->timeout_reason))) ||
13833             (cr->req_ie &&
13834              nla_put(msg, NL80211_ATTR_REQ_IE, cr->req_ie_len, cr->req_ie)) ||
13835             (cr->resp_ie &&
13836              nla_put(msg, NL80211_ATTR_RESP_IE, cr->resp_ie_len,
13837                      cr->resp_ie)) ||
13838             (cr->update_erp_next_seq_num &&
13839              nla_put_u16(msg, NL80211_ATTR_FILS_ERP_NEXT_SEQ_NUM,
13840                          cr->fils_erp_next_seq_num)) ||
13841             (cr->status == WLAN_STATUS_SUCCESS &&
13842              ((cr->fils_kek &&
13843                nla_put(msg, NL80211_ATTR_FILS_KEK, cr->fils_kek_len,
13844                        cr->fils_kek)) ||
13845               (cr->pmk &&
13846                nla_put(msg, NL80211_ATTR_PMK, cr->pmk_len, cr->pmk)) ||
13847               (cr->pmkid &&
13848                nla_put(msg, NL80211_ATTR_PMKID, WLAN_PMKID_LEN, cr->pmkid)))))
13849                 goto nla_put_failure;
13850
13851         genlmsg_end(msg, hdr);
13852
13853         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13854                                 NL80211_MCGRP_MLME, gfp);
13855         return;
13856
13857  nla_put_failure:
13858         genlmsg_cancel(msg, hdr);
13859         nlmsg_free(msg);
13860 }
13861
13862 void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
13863                          struct net_device *netdev,
13864                          struct cfg80211_roam_info *info, gfp_t gfp)
13865 {
13866         struct sk_buff *msg;
13867         void *hdr;
13868         const u8 *bssid = info->bss ? info->bss->bssid : info->bssid;
13869
13870         msg = nlmsg_new(100 + info->req_ie_len + info->resp_ie_len, gfp);
13871         if (!msg)
13872                 return;
13873
13874         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
13875         if (!hdr) {
13876                 nlmsg_free(msg);
13877                 return;
13878         }
13879
13880         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13881             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13882             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
13883             (info->req_ie &&
13884              nla_put(msg, NL80211_ATTR_REQ_IE, info->req_ie_len,
13885                      info->req_ie)) ||
13886             (info->resp_ie &&
13887              nla_put(msg, NL80211_ATTR_RESP_IE, info->resp_ie_len,
13888                      info->resp_ie)) ||
13889             (info->authorized &&
13890              nla_put_flag(msg, NL80211_ATTR_PORT_AUTHORIZED)))
13891                 goto nla_put_failure;
13892
13893         genlmsg_end(msg, hdr);
13894
13895         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13896                                 NL80211_MCGRP_MLME, gfp);
13897         return;
13898
13899  nla_put_failure:
13900         genlmsg_cancel(msg, hdr);
13901         nlmsg_free(msg);
13902 }
13903
13904 void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
13905                                struct net_device *netdev, u16 reason,
13906                                const u8 *ie, size_t ie_len, bool from_ap)
13907 {
13908         struct sk_buff *msg;
13909         void *hdr;
13910
13911         msg = nlmsg_new(100 + ie_len, GFP_KERNEL);
13912         if (!msg)
13913                 return;
13914
13915         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
13916         if (!hdr) {
13917                 nlmsg_free(msg);
13918                 return;
13919         }
13920
13921         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13922             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13923             (from_ap && reason &&
13924              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
13925             (from_ap &&
13926              nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
13927             (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
13928                 goto nla_put_failure;
13929
13930         genlmsg_end(msg, hdr);
13931
13932         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13933                                 NL80211_MCGRP_MLME, GFP_KERNEL);
13934         return;
13935
13936  nla_put_failure:
13937         genlmsg_cancel(msg, hdr);
13938         nlmsg_free(msg);
13939 }
13940
13941 void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
13942                              struct net_device *netdev, const u8 *bssid,
13943                              gfp_t gfp)
13944 {
13945         struct sk_buff *msg;
13946         void *hdr;
13947
13948         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
13949         if (!msg)
13950                 return;
13951
13952         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
13953         if (!hdr) {
13954                 nlmsg_free(msg);
13955                 return;
13956         }
13957
13958         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13959             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
13960             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
13961                 goto nla_put_failure;
13962
13963         genlmsg_end(msg, hdr);
13964
13965         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
13966                                 NL80211_MCGRP_MLME, gfp);
13967         return;
13968
13969  nla_put_failure:
13970         genlmsg_cancel(msg, hdr);
13971         nlmsg_free(msg);
13972 }
13973
13974 void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
13975                                         const u8* ie, u8 ie_len, gfp_t gfp)
13976 {
13977         struct wireless_dev *wdev = dev->ieee80211_ptr;
13978         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
13979         struct sk_buff *msg;
13980         void *hdr;
13981
13982         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
13983                 return;
13984
13985         trace_cfg80211_notify_new_peer_candidate(dev, addr);
13986
13987         msg = nlmsg_new(100 + ie_len, gfp);
13988         if (!msg)
13989                 return;
13990
13991         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
13992         if (!hdr) {
13993                 nlmsg_free(msg);
13994                 return;
13995         }
13996
13997         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
13998             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
13999             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14000             (ie_len && ie &&
14001              nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
14002                 goto nla_put_failure;
14003
14004         genlmsg_end(msg, hdr);
14005
14006         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14007                                 NL80211_MCGRP_MLME, gfp);
14008         return;
14009
14010  nla_put_failure:
14011         genlmsg_cancel(msg, hdr);
14012         nlmsg_free(msg);
14013 }
14014 EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
14015
14016 void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
14017                                  struct net_device *netdev, const u8 *addr,
14018                                  enum nl80211_key_type key_type, int key_id,
14019                                  const u8 *tsc, gfp_t gfp)
14020 {
14021         struct sk_buff *msg;
14022         void *hdr;
14023
14024         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14025         if (!msg)
14026                 return;
14027
14028         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
14029         if (!hdr) {
14030                 nlmsg_free(msg);
14031                 return;
14032         }
14033
14034         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14035             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14036             (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
14037             nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
14038             (key_id != -1 &&
14039              nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
14040             (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
14041                 goto nla_put_failure;
14042
14043         genlmsg_end(msg, hdr);
14044
14045         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14046                                 NL80211_MCGRP_MLME, gfp);
14047         return;
14048
14049  nla_put_failure:
14050         genlmsg_cancel(msg, hdr);
14051         nlmsg_free(msg);
14052 }
14053
14054 void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
14055                                     struct ieee80211_channel *channel_before,
14056                                     struct ieee80211_channel *channel_after)
14057 {
14058         struct sk_buff *msg;
14059         void *hdr;
14060         struct nlattr *nl_freq;
14061
14062         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
14063         if (!msg)
14064                 return;
14065
14066         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
14067         if (!hdr) {
14068                 nlmsg_free(msg);
14069                 return;
14070         }
14071
14072         /*
14073          * Since we are applying the beacon hint to a wiphy we know its
14074          * wiphy_idx is valid
14075          */
14076         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
14077                 goto nla_put_failure;
14078
14079         /* Before */
14080         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
14081         if (!nl_freq)
14082                 goto nla_put_failure;
14083         if (nl80211_msg_put_channel(msg, channel_before, false))
14084                 goto nla_put_failure;
14085         nla_nest_end(msg, nl_freq);
14086
14087         /* After */
14088         nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
14089         if (!nl_freq)
14090                 goto nla_put_failure;
14091         if (nl80211_msg_put_channel(msg, channel_after, false))
14092                 goto nla_put_failure;
14093         nla_nest_end(msg, nl_freq);
14094
14095         genlmsg_end(msg, hdr);
14096
14097         rcu_read_lock();
14098         genlmsg_multicast_allns(&nl80211_fam, msg, 0,
14099                                 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
14100         rcu_read_unlock();
14101
14102         return;
14103
14104 nla_put_failure:
14105         genlmsg_cancel(msg, hdr);
14106         nlmsg_free(msg);
14107 }
14108
14109 static void nl80211_send_remain_on_chan_event(
14110         int cmd, struct cfg80211_registered_device *rdev,
14111         struct wireless_dev *wdev, u64 cookie,
14112         struct ieee80211_channel *chan,
14113         unsigned int duration, gfp_t gfp)
14114 {
14115         struct sk_buff *msg;
14116         void *hdr;
14117
14118         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14119         if (!msg)
14120                 return;
14121
14122         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14123         if (!hdr) {
14124                 nlmsg_free(msg);
14125                 return;
14126         }
14127
14128         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14129             (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14130                                          wdev->netdev->ifindex)) ||
14131             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14132                               NL80211_ATTR_PAD) ||
14133             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
14134             nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
14135                         NL80211_CHAN_NO_HT) ||
14136             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14137                               NL80211_ATTR_PAD))
14138                 goto nla_put_failure;
14139
14140         if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
14141             nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
14142                 goto nla_put_failure;
14143
14144         genlmsg_end(msg, hdr);
14145
14146         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14147                                 NL80211_MCGRP_MLME, gfp);
14148         return;
14149
14150  nla_put_failure:
14151         genlmsg_cancel(msg, hdr);
14152         nlmsg_free(msg);
14153 }
14154
14155 void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
14156                                struct ieee80211_channel *chan,
14157                                unsigned int duration, gfp_t gfp)
14158 {
14159         struct wiphy *wiphy = wdev->wiphy;
14160         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14161
14162         trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
14163         nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
14164                                           rdev, wdev, cookie, chan,
14165                                           duration, gfp);
14166 }
14167 EXPORT_SYMBOL(cfg80211_ready_on_channel);
14168
14169 void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
14170                                         struct ieee80211_channel *chan,
14171                                         gfp_t gfp)
14172 {
14173         struct wiphy *wiphy = wdev->wiphy;
14174         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14175
14176         trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
14177         nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
14178                                           rdev, wdev, cookie, chan, 0, gfp);
14179 }
14180 EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
14181
14182 void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
14183                       struct station_info *sinfo, gfp_t gfp)
14184 {
14185         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14186         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14187         struct sk_buff *msg;
14188
14189         trace_cfg80211_new_sta(dev, mac_addr, sinfo);
14190
14191         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14192         if (!msg)
14193                 return;
14194
14195         if (nl80211_send_station(msg, NL80211_CMD_NEW_STATION, 0, 0, 0,
14196                                  rdev, dev, mac_addr, sinfo) < 0) {
14197                 nlmsg_free(msg);
14198                 return;
14199         }
14200
14201         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14202                                 NL80211_MCGRP_MLME, gfp);
14203 }
14204 EXPORT_SYMBOL(cfg80211_new_sta);
14205
14206 void cfg80211_del_sta_sinfo(struct net_device *dev, const u8 *mac_addr,
14207                             struct station_info *sinfo, gfp_t gfp)
14208 {
14209         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14210         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14211         struct sk_buff *msg;
14212         struct station_info empty_sinfo = {};
14213
14214         if (!sinfo)
14215                 sinfo = &empty_sinfo;
14216
14217         trace_cfg80211_del_sta(dev, mac_addr);
14218
14219         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14220         if (!msg)
14221                 return;
14222
14223         if (nl80211_send_station(msg, NL80211_CMD_DEL_STATION, 0, 0, 0,
14224                                  rdev, dev, mac_addr, sinfo) < 0) {
14225                 nlmsg_free(msg);
14226                 return;
14227         }
14228
14229         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14230                                 NL80211_MCGRP_MLME, gfp);
14231 }
14232 EXPORT_SYMBOL(cfg80211_del_sta_sinfo);
14233
14234 void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
14235                           enum nl80211_connect_failed_reason reason,
14236                           gfp_t gfp)
14237 {
14238         struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
14239         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14240         struct sk_buff *msg;
14241         void *hdr;
14242
14243         msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
14244         if (!msg)
14245                 return;
14246
14247         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
14248         if (!hdr) {
14249                 nlmsg_free(msg);
14250                 return;
14251         }
14252
14253         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14254             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
14255             nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
14256                 goto nla_put_failure;
14257
14258         genlmsg_end(msg, hdr);
14259
14260         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14261                                 NL80211_MCGRP_MLME, gfp);
14262         return;
14263
14264  nla_put_failure:
14265         genlmsg_cancel(msg, hdr);
14266         nlmsg_free(msg);
14267 }
14268 EXPORT_SYMBOL(cfg80211_conn_failed);
14269
14270 static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
14271                                        const u8 *addr, gfp_t gfp)
14272 {
14273         struct wireless_dev *wdev = dev->ieee80211_ptr;
14274         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14275         struct sk_buff *msg;
14276         void *hdr;
14277         u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
14278
14279         if (!nlportid)
14280                 return false;
14281
14282         msg = nlmsg_new(100, gfp);
14283         if (!msg)
14284                 return true;
14285
14286         hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
14287         if (!hdr) {
14288                 nlmsg_free(msg);
14289                 return true;
14290         }
14291
14292         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14293             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14294             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
14295                 goto nla_put_failure;
14296
14297         genlmsg_end(msg, hdr);
14298         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14299         return true;
14300
14301  nla_put_failure:
14302         genlmsg_cancel(msg, hdr);
14303         nlmsg_free(msg);
14304         return true;
14305 }
14306
14307 bool cfg80211_rx_spurious_frame(struct net_device *dev,
14308                                 const u8 *addr, gfp_t gfp)
14309 {
14310         struct wireless_dev *wdev = dev->ieee80211_ptr;
14311         bool ret;
14312
14313         trace_cfg80211_rx_spurious_frame(dev, addr);
14314
14315         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14316                     wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
14317                 trace_cfg80211_return_bool(false);
14318                 return false;
14319         }
14320         ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
14321                                          addr, gfp);
14322         trace_cfg80211_return_bool(ret);
14323         return ret;
14324 }
14325 EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
14326
14327 bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
14328                                         const u8 *addr, gfp_t gfp)
14329 {
14330         struct wireless_dev *wdev = dev->ieee80211_ptr;
14331         bool ret;
14332
14333         trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
14334
14335         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
14336                     wdev->iftype != NL80211_IFTYPE_P2P_GO &&
14337                     wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
14338                 trace_cfg80211_return_bool(false);
14339                 return false;
14340         }
14341         ret = __nl80211_unexpected_frame(dev,
14342                                          NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
14343                                          addr, gfp);
14344         trace_cfg80211_return_bool(ret);
14345         return ret;
14346 }
14347 EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
14348
14349 int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
14350                       struct wireless_dev *wdev, u32 nlportid,
14351                       int freq, int sig_dbm,
14352                       const u8 *buf, size_t len, u32 flags, gfp_t gfp)
14353 {
14354         struct net_device *netdev = wdev->netdev;
14355         struct sk_buff *msg;
14356         void *hdr;
14357
14358         msg = nlmsg_new(100 + len, gfp);
14359         if (!msg)
14360                 return -ENOMEM;
14361
14362         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14363         if (!hdr) {
14364                 nlmsg_free(msg);
14365                 return -ENOMEM;
14366         }
14367
14368         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14369             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14370                                         netdev->ifindex)) ||
14371             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14372                               NL80211_ATTR_PAD) ||
14373             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
14374             (sig_dbm &&
14375              nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14376             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14377             (flags &&
14378              nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
14379                 goto nla_put_failure;
14380
14381         genlmsg_end(msg, hdr);
14382
14383         return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
14384
14385  nla_put_failure:
14386         genlmsg_cancel(msg, hdr);
14387         nlmsg_free(msg);
14388         return -ENOBUFS;
14389 }
14390
14391 void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
14392                              const u8 *buf, size_t len, bool ack, gfp_t gfp)
14393 {
14394         struct wiphy *wiphy = wdev->wiphy;
14395         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14396         struct net_device *netdev = wdev->netdev;
14397         struct sk_buff *msg;
14398         void *hdr;
14399
14400         trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
14401
14402         msg = nlmsg_new(100 + len, gfp);
14403         if (!msg)
14404                 return;
14405
14406         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
14407         if (!hdr) {
14408                 nlmsg_free(msg);
14409                 return;
14410         }
14411
14412         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14413             (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
14414                                    netdev->ifindex)) ||
14415             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14416                               NL80211_ATTR_PAD) ||
14417             nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
14418             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14419                               NL80211_ATTR_PAD) ||
14420             (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
14421                 goto nla_put_failure;
14422
14423         genlmsg_end(msg, hdr);
14424
14425         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14426                                 NL80211_MCGRP_MLME, gfp);
14427         return;
14428
14429  nla_put_failure:
14430         genlmsg_cancel(msg, hdr);
14431         nlmsg_free(msg);
14432 }
14433 EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
14434
14435 static struct sk_buff *cfg80211_prepare_cqm(struct net_device *dev,
14436                                             const char *mac, gfp_t gfp)
14437 {
14438         struct wireless_dev *wdev = dev->ieee80211_ptr;
14439         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14440         struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14441         void **cb;
14442
14443         if (!msg)
14444                 return NULL;
14445
14446         cb = (void **)msg->cb;
14447
14448         cb[0] = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
14449         if (!cb[0]) {
14450                 nlmsg_free(msg);
14451                 return NULL;
14452         }
14453
14454         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14455             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
14456                 goto nla_put_failure;
14457
14458         if (mac && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac))
14459                 goto nla_put_failure;
14460
14461         cb[1] = nla_nest_start(msg, NL80211_ATTR_CQM);
14462         if (!cb[1])
14463                 goto nla_put_failure;
14464
14465         cb[2] = rdev;
14466
14467         return msg;
14468  nla_put_failure:
14469         nlmsg_free(msg);
14470         return NULL;
14471 }
14472
14473 static void cfg80211_send_cqm(struct sk_buff *msg, gfp_t gfp)
14474 {
14475         void **cb = (void **)msg->cb;
14476         struct cfg80211_registered_device *rdev = cb[2];
14477
14478         nla_nest_end(msg, cb[1]);
14479         genlmsg_end(msg, cb[0]);
14480
14481         memset(msg->cb, 0, sizeof(msg->cb));
14482
14483         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14484                                 NL80211_MCGRP_MLME, gfp);
14485 }
14486
14487 void cfg80211_cqm_rssi_notify(struct net_device *dev,
14488                               enum nl80211_cqm_rssi_threshold_event rssi_event,
14489                               s32 rssi_level, gfp_t gfp)
14490 {
14491         struct sk_buff *msg;
14492         struct wireless_dev *wdev = dev->ieee80211_ptr;
14493         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14494
14495         trace_cfg80211_cqm_rssi_notify(dev, rssi_event, rssi_level);
14496
14497         if (WARN_ON(rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW &&
14498                     rssi_event != NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH))
14499                 return;
14500
14501         if (wdev->cqm_config) {
14502                 wdev->cqm_config->last_rssi_event_value = rssi_level;
14503
14504                 cfg80211_cqm_rssi_update(rdev, dev);
14505
14506                 if (rssi_level == 0)
14507                         rssi_level = wdev->cqm_config->last_rssi_event_value;
14508         }
14509
14510         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14511         if (!msg)
14512                 return;
14513
14514         if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
14515                         rssi_event))
14516                 goto nla_put_failure;
14517
14518         if (rssi_level && nla_put_s32(msg, NL80211_ATTR_CQM_RSSI_LEVEL,
14519                                       rssi_level))
14520                 goto nla_put_failure;
14521
14522         cfg80211_send_cqm(msg, gfp);
14523
14524         return;
14525
14526  nla_put_failure:
14527         nlmsg_free(msg);
14528 }
14529 EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
14530
14531 void cfg80211_cqm_txe_notify(struct net_device *dev,
14532                              const u8 *peer, u32 num_packets,
14533                              u32 rate, u32 intvl, gfp_t gfp)
14534 {
14535         struct sk_buff *msg;
14536
14537         msg = cfg80211_prepare_cqm(dev, peer, gfp);
14538         if (!msg)
14539                 return;
14540
14541         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
14542                 goto nla_put_failure;
14543
14544         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
14545                 goto nla_put_failure;
14546
14547         if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
14548                 goto nla_put_failure;
14549
14550         cfg80211_send_cqm(msg, gfp);
14551         return;
14552
14553  nla_put_failure:
14554         nlmsg_free(msg);
14555 }
14556 EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
14557
14558 void cfg80211_cqm_pktloss_notify(struct net_device *dev,
14559                                  const u8 *peer, u32 num_packets, gfp_t gfp)
14560 {
14561         struct sk_buff *msg;
14562
14563         trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
14564
14565         msg = cfg80211_prepare_cqm(dev, peer, gfp);
14566         if (!msg)
14567                 return;
14568
14569         if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
14570                 goto nla_put_failure;
14571
14572         cfg80211_send_cqm(msg, gfp);
14573         return;
14574
14575  nla_put_failure:
14576         nlmsg_free(msg);
14577 }
14578 EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
14579
14580 void cfg80211_cqm_beacon_loss_notify(struct net_device *dev, gfp_t gfp)
14581 {
14582         struct sk_buff *msg;
14583
14584         msg = cfg80211_prepare_cqm(dev, NULL, gfp);
14585         if (!msg)
14586                 return;
14587
14588         if (nla_put_flag(msg, NL80211_ATTR_CQM_BEACON_LOSS_EVENT))
14589                 goto nla_put_failure;
14590
14591         cfg80211_send_cqm(msg, gfp);
14592         return;
14593
14594  nla_put_failure:
14595         nlmsg_free(msg);
14596 }
14597 EXPORT_SYMBOL(cfg80211_cqm_beacon_loss_notify);
14598
14599 static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
14600                                      struct net_device *netdev, const u8 *bssid,
14601                                      const u8 *replay_ctr, gfp_t gfp)
14602 {
14603         struct sk_buff *msg;
14604         struct nlattr *rekey_attr;
14605         void *hdr;
14606
14607         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14608         if (!msg)
14609                 return;
14610
14611         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
14612         if (!hdr) {
14613                 nlmsg_free(msg);
14614                 return;
14615         }
14616
14617         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14618             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14619             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
14620                 goto nla_put_failure;
14621
14622         rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
14623         if (!rekey_attr)
14624                 goto nla_put_failure;
14625
14626         if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
14627                     NL80211_REPLAY_CTR_LEN, replay_ctr))
14628                 goto nla_put_failure;
14629
14630         nla_nest_end(msg, rekey_attr);
14631
14632         genlmsg_end(msg, hdr);
14633
14634         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14635                                 NL80211_MCGRP_MLME, gfp);
14636         return;
14637
14638  nla_put_failure:
14639         genlmsg_cancel(msg, hdr);
14640         nlmsg_free(msg);
14641 }
14642
14643 void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
14644                                const u8 *replay_ctr, gfp_t gfp)
14645 {
14646         struct wireless_dev *wdev = dev->ieee80211_ptr;
14647         struct wiphy *wiphy = wdev->wiphy;
14648         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14649
14650         trace_cfg80211_gtk_rekey_notify(dev, bssid);
14651         nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
14652 }
14653 EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
14654
14655 static void
14656 nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
14657                                struct net_device *netdev, int index,
14658                                const u8 *bssid, bool preauth, gfp_t gfp)
14659 {
14660         struct sk_buff *msg;
14661         struct nlattr *attr;
14662         void *hdr;
14663
14664         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14665         if (!msg)
14666                 return;
14667
14668         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
14669         if (!hdr) {
14670                 nlmsg_free(msg);
14671                 return;
14672         }
14673
14674         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14675             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14676                 goto nla_put_failure;
14677
14678         attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
14679         if (!attr)
14680                 goto nla_put_failure;
14681
14682         if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
14683             nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
14684             (preauth &&
14685              nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
14686                 goto nla_put_failure;
14687
14688         nla_nest_end(msg, attr);
14689
14690         genlmsg_end(msg, hdr);
14691
14692         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14693                                 NL80211_MCGRP_MLME, gfp);
14694         return;
14695
14696  nla_put_failure:
14697         genlmsg_cancel(msg, hdr);
14698         nlmsg_free(msg);
14699 }
14700
14701 void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
14702                                      const u8 *bssid, bool preauth, gfp_t gfp)
14703 {
14704         struct wireless_dev *wdev = dev->ieee80211_ptr;
14705         struct wiphy *wiphy = wdev->wiphy;
14706         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14707
14708         trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
14709         nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
14710 }
14711 EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
14712
14713 static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
14714                                      struct net_device *netdev,
14715                                      struct cfg80211_chan_def *chandef,
14716                                      gfp_t gfp,
14717                                      enum nl80211_commands notif,
14718                                      u8 count)
14719 {
14720         struct sk_buff *msg;
14721         void *hdr;
14722
14723         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14724         if (!msg)
14725                 return;
14726
14727         hdr = nl80211hdr_put(msg, 0, 0, 0, notif);
14728         if (!hdr) {
14729                 nlmsg_free(msg);
14730                 return;
14731         }
14732
14733         if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
14734                 goto nla_put_failure;
14735
14736         if (nl80211_send_chandef(msg, chandef))
14737                 goto nla_put_failure;
14738
14739         if ((notif == NL80211_CMD_CH_SWITCH_STARTED_NOTIFY) &&
14740             (nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT, count)))
14741                         goto nla_put_failure;
14742
14743         genlmsg_end(msg, hdr);
14744
14745         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14746                                 NL80211_MCGRP_MLME, gfp);
14747         return;
14748
14749  nla_put_failure:
14750         genlmsg_cancel(msg, hdr);
14751         nlmsg_free(msg);
14752 }
14753
14754 void cfg80211_ch_switch_notify(struct net_device *dev,
14755                                struct cfg80211_chan_def *chandef)
14756 {
14757         struct wireless_dev *wdev = dev->ieee80211_ptr;
14758         struct wiphy *wiphy = wdev->wiphy;
14759         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14760
14761         ASSERT_WDEV_LOCK(wdev);
14762
14763         trace_cfg80211_ch_switch_notify(dev, chandef);
14764
14765         wdev->chandef = *chandef;
14766         wdev->preset_chandef = *chandef;
14767
14768         if (wdev->iftype == NL80211_IFTYPE_STATION &&
14769             !WARN_ON(!wdev->current_bss))
14770                 wdev->current_bss->pub.channel = chandef->chan;
14771
14772         nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14773                                  NL80211_CMD_CH_SWITCH_NOTIFY, 0);
14774 }
14775 EXPORT_SYMBOL(cfg80211_ch_switch_notify);
14776
14777 void cfg80211_ch_switch_started_notify(struct net_device *dev,
14778                                        struct cfg80211_chan_def *chandef,
14779                                        u8 count)
14780 {
14781         struct wireless_dev *wdev = dev->ieee80211_ptr;
14782         struct wiphy *wiphy = wdev->wiphy;
14783         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14784
14785         trace_cfg80211_ch_switch_started_notify(dev, chandef);
14786
14787         nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL,
14788                                  NL80211_CMD_CH_SWITCH_STARTED_NOTIFY, count);
14789 }
14790 EXPORT_SYMBOL(cfg80211_ch_switch_started_notify);
14791
14792 void
14793 nl80211_radar_notify(struct cfg80211_registered_device *rdev,
14794                      const struct cfg80211_chan_def *chandef,
14795                      enum nl80211_radar_event event,
14796                      struct net_device *netdev, gfp_t gfp)
14797 {
14798         struct sk_buff *msg;
14799         void *hdr;
14800
14801         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14802         if (!msg)
14803                 return;
14804
14805         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
14806         if (!hdr) {
14807                 nlmsg_free(msg);
14808                 return;
14809         }
14810
14811         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
14812                 goto nla_put_failure;
14813
14814         /* NOP and radar events don't need a netdev parameter */
14815         if (netdev) {
14816                 struct wireless_dev *wdev = netdev->ieee80211_ptr;
14817
14818                 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
14819                     nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
14820                                       NL80211_ATTR_PAD))
14821                         goto nla_put_failure;
14822         }
14823
14824         if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
14825                 goto nla_put_failure;
14826
14827         if (nl80211_send_chandef(msg, chandef))
14828                 goto nla_put_failure;
14829
14830         genlmsg_end(msg, hdr);
14831
14832         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14833                                 NL80211_MCGRP_MLME, gfp);
14834         return;
14835
14836  nla_put_failure:
14837         genlmsg_cancel(msg, hdr);
14838         nlmsg_free(msg);
14839 }
14840
14841 void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
14842                            u64 cookie, bool acked, gfp_t gfp)
14843 {
14844         struct wireless_dev *wdev = dev->ieee80211_ptr;
14845         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14846         struct sk_buff *msg;
14847         void *hdr;
14848
14849         trace_cfg80211_probe_status(dev, addr, cookie, acked);
14850
14851         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
14852
14853         if (!msg)
14854                 return;
14855
14856         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
14857         if (!hdr) {
14858                 nlmsg_free(msg);
14859                 return;
14860         }
14861
14862         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14863             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
14864             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
14865             nla_put_u64_64bit(msg, NL80211_ATTR_COOKIE, cookie,
14866                               NL80211_ATTR_PAD) ||
14867             (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
14868                 goto nla_put_failure;
14869
14870         genlmsg_end(msg, hdr);
14871
14872         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
14873                                 NL80211_MCGRP_MLME, gfp);
14874         return;
14875
14876  nla_put_failure:
14877         genlmsg_cancel(msg, hdr);
14878         nlmsg_free(msg);
14879 }
14880 EXPORT_SYMBOL(cfg80211_probe_status);
14881
14882 void cfg80211_report_obss_beacon(struct wiphy *wiphy,
14883                                  const u8 *frame, size_t len,
14884                                  int freq, int sig_dbm)
14885 {
14886         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
14887         struct sk_buff *msg;
14888         void *hdr;
14889         struct cfg80211_beacon_registration *reg;
14890
14891         trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
14892
14893         spin_lock_bh(&rdev->beacon_registrations_lock);
14894         list_for_each_entry(reg, &rdev->beacon_registrations, list) {
14895                 msg = nlmsg_new(len + 100, GFP_ATOMIC);
14896                 if (!msg) {
14897                         spin_unlock_bh(&rdev->beacon_registrations_lock);
14898                         return;
14899                 }
14900
14901                 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
14902                 if (!hdr)
14903                         goto nla_put_failure;
14904
14905                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
14906                     (freq &&
14907                      nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
14908                     (sig_dbm &&
14909                      nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
14910                     nla_put(msg, NL80211_ATTR_FRAME, len, frame))
14911                         goto nla_put_failure;
14912
14913                 genlmsg_end(msg, hdr);
14914
14915                 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
14916         }
14917         spin_unlock_bh(&rdev->beacon_registrations_lock);
14918         return;
14919
14920  nla_put_failure:
14921         spin_unlock_bh(&rdev->beacon_registrations_lock);
14922         if (hdr)
14923                 genlmsg_cancel(msg, hdr);
14924         nlmsg_free(msg);
14925 }
14926 EXPORT_SYMBOL(cfg80211_report_obss_beacon);
14927
14928 #ifdef CONFIG_PM
14929 static int cfg80211_net_detect_results(struct sk_buff *msg,
14930                                        struct cfg80211_wowlan_wakeup *wakeup)
14931 {
14932         struct cfg80211_wowlan_nd_info *nd = wakeup->net_detect;
14933         struct nlattr *nl_results, *nl_match, *nl_freqs;
14934         int i, j;
14935
14936         nl_results = nla_nest_start(
14937                 msg, NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS);
14938         if (!nl_results)
14939                 return -EMSGSIZE;
14940
14941         for (i = 0; i < nd->n_matches; i++) {
14942                 struct cfg80211_wowlan_nd_match *match = nd->matches[i];
14943
14944                 nl_match = nla_nest_start(msg, i);
14945                 if (!nl_match)
14946                         break;
14947
14948                 /* The SSID attribute is optional in nl80211, but for
14949                  * simplicity reasons it's always present in the
14950                  * cfg80211 structure.  If a driver can't pass the
14951                  * SSID, that needs to be changed.  A zero length SSID
14952                  * is still a valid SSID (wildcard), so it cannot be
14953                  * used for this purpose.
14954                  */
14955                 if (nla_put(msg, NL80211_ATTR_SSID, match->ssid.ssid_len,
14956                             match->ssid.ssid)) {
14957                         nla_nest_cancel(msg, nl_match);
14958                         goto out;
14959                 }
14960
14961                 if (match->n_channels) {
14962                         nl_freqs = nla_nest_start(
14963                                 msg, NL80211_ATTR_SCAN_FREQUENCIES);
14964                         if (!nl_freqs) {
14965                                 nla_nest_cancel(msg, nl_match);
14966                                 goto out;
14967                         }
14968
14969                         for (j = 0; j < match->n_channels; j++) {
14970                                 if (nla_put_u32(msg, j, match->channels[j])) {
14971                                         nla_nest_cancel(msg, nl_freqs);
14972                                         nla_nest_cancel(msg, nl_match);
14973                                         goto out;
14974                                 }
14975                         }
14976
14977                         nla_nest_end(msg, nl_freqs);
14978                 }
14979
14980                 nla_nest_end(msg, nl_match);
14981         }
14982
14983 out:
14984         nla_nest_end(msg, nl_results);
14985         return 0;
14986 }
14987
14988 void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
14989                                    struct cfg80211_wowlan_wakeup *wakeup,
14990                                    gfp_t gfp)
14991 {
14992         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14993         struct sk_buff *msg;
14994         void *hdr;
14995         int size = 200;
14996
14997         trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
14998
14999         if (wakeup)
15000                 size += wakeup->packet_present_len;
15001
15002         msg = nlmsg_new(size, gfp);
15003         if (!msg)
15004                 return;
15005
15006         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
15007         if (!hdr)
15008                 goto free_msg;
15009
15010         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15011             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15012                               NL80211_ATTR_PAD))
15013                 goto free_msg;
15014
15015         if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
15016                                         wdev->netdev->ifindex))
15017                 goto free_msg;
15018
15019         if (wakeup) {
15020                 struct nlattr *reasons;
15021
15022                 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
15023                 if (!reasons)
15024                         goto free_msg;
15025
15026                 if (wakeup->disconnect &&
15027                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
15028                         goto free_msg;
15029                 if (wakeup->magic_pkt &&
15030                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
15031                         goto free_msg;
15032                 if (wakeup->gtk_rekey_failure &&
15033                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
15034                         goto free_msg;
15035                 if (wakeup->eap_identity_req &&
15036                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
15037                         goto free_msg;
15038                 if (wakeup->four_way_handshake &&
15039                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
15040                         goto free_msg;
15041                 if (wakeup->rfkill_release &&
15042                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
15043                         goto free_msg;
15044
15045                 if (wakeup->pattern_idx >= 0 &&
15046                     nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
15047                                 wakeup->pattern_idx))
15048                         goto free_msg;
15049
15050                 if (wakeup->tcp_match &&
15051                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
15052                         goto free_msg;
15053
15054                 if (wakeup->tcp_connlost &&
15055                     nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
15056                         goto free_msg;
15057
15058                 if (wakeup->tcp_nomoretokens &&
15059                     nla_put_flag(msg,
15060                                  NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
15061                         goto free_msg;
15062
15063                 if (wakeup->packet) {
15064                         u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
15065                         u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
15066
15067                         if (!wakeup->packet_80211) {
15068                                 pkt_attr =
15069                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
15070                                 len_attr =
15071                                         NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
15072                         }
15073
15074                         if (wakeup->packet_len &&
15075                             nla_put_u32(msg, len_attr, wakeup->packet_len))
15076                                 goto free_msg;
15077
15078                         if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
15079                                     wakeup->packet))
15080                                 goto free_msg;
15081                 }
15082
15083                 if (wakeup->net_detect &&
15084                     cfg80211_net_detect_results(msg, wakeup))
15085                                 goto free_msg;
15086
15087                 nla_nest_end(msg, reasons);
15088         }
15089
15090         genlmsg_end(msg, hdr);
15091
15092         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15093                                 NL80211_MCGRP_MLME, gfp);
15094         return;
15095
15096  free_msg:
15097         nlmsg_free(msg);
15098 }
15099 EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
15100 #endif
15101
15102 void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
15103                                 enum nl80211_tdls_operation oper,
15104                                 u16 reason_code, gfp_t gfp)
15105 {
15106         struct wireless_dev *wdev = dev->ieee80211_ptr;
15107         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15108         struct sk_buff *msg;
15109         void *hdr;
15110
15111         trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
15112                                          reason_code);
15113
15114         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15115         if (!msg)
15116                 return;
15117
15118         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
15119         if (!hdr) {
15120                 nlmsg_free(msg);
15121                 return;
15122         }
15123
15124         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15125             nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
15126             nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
15127             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
15128             (reason_code > 0 &&
15129              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
15130                 goto nla_put_failure;
15131
15132         genlmsg_end(msg, hdr);
15133
15134         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15135                                 NL80211_MCGRP_MLME, gfp);
15136         return;
15137
15138  nla_put_failure:
15139         genlmsg_cancel(msg, hdr);
15140         nlmsg_free(msg);
15141 }
15142 EXPORT_SYMBOL(cfg80211_tdls_oper_request);
15143
15144 static int nl80211_netlink_notify(struct notifier_block * nb,
15145                                   unsigned long state,
15146                                   void *_notify)
15147 {
15148         struct netlink_notify *notify = _notify;
15149         struct cfg80211_registered_device *rdev;
15150         struct wireless_dev *wdev;
15151         struct cfg80211_beacon_registration *reg, *tmp;
15152
15153         if (state != NETLINK_URELEASE || notify->protocol != NETLINK_GENERIC)
15154                 return NOTIFY_DONE;
15155
15156         rcu_read_lock();
15157
15158         list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
15159                 struct cfg80211_sched_scan_request *sched_scan_req;
15160
15161                 list_for_each_entry_rcu(sched_scan_req,
15162                                         &rdev->sched_scan_req_list,
15163                                         list) {
15164                         if (sched_scan_req->owner_nlportid == notify->portid) {
15165                                 sched_scan_req->nl_owner_dead = true;
15166                                 schedule_work(&rdev->sched_scan_stop_wk);
15167                         }
15168                 }
15169
15170                 list_for_each_entry_rcu(wdev, &rdev->wiphy.wdev_list, list) {
15171                         cfg80211_mlme_unregister_socket(wdev, notify->portid);
15172
15173                         if (wdev->owner_nlportid == notify->portid) {
15174                                 wdev->nl_owner_dead = true;
15175                                 schedule_work(&rdev->destroy_work);
15176                         } else if (wdev->conn_owner_nlportid == notify->portid) {
15177                                 schedule_work(&wdev->disconnect_wk);
15178                         }
15179                 }
15180
15181                 spin_lock_bh(&rdev->beacon_registrations_lock);
15182                 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
15183                                          list) {
15184                         if (reg->nlportid == notify->portid) {
15185                                 list_del(&reg->list);
15186                                 kfree(reg);
15187                                 break;
15188                         }
15189                 }
15190                 spin_unlock_bh(&rdev->beacon_registrations_lock);
15191         }
15192
15193         rcu_read_unlock();
15194
15195         /*
15196          * It is possible that the user space process that is controlling the
15197          * indoor setting disappeared, so notify the regulatory core.
15198          */
15199         regulatory_netlink_notify(notify->portid);
15200         return NOTIFY_OK;
15201 }
15202
15203 static struct notifier_block nl80211_netlink_notifier = {
15204         .notifier_call = nl80211_netlink_notify,
15205 };
15206
15207 void cfg80211_ft_event(struct net_device *netdev,
15208                        struct cfg80211_ft_event_params *ft_event)
15209 {
15210         struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
15211         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15212         struct sk_buff *msg;
15213         void *hdr;
15214
15215         trace_cfg80211_ft_event(wiphy, netdev, ft_event);
15216
15217         if (!ft_event->target_ap)
15218                 return;
15219
15220         msg = nlmsg_new(100 + ft_event->ric_ies_len, GFP_KERNEL);
15221         if (!msg)
15222                 return;
15223
15224         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
15225         if (!hdr)
15226                 goto out;
15227
15228         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15229             nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
15230             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
15231                 goto out;
15232
15233         if (ft_event->ies &&
15234             nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
15235                 goto out;
15236         if (ft_event->ric_ies &&
15237             nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
15238                     ft_event->ric_ies))
15239                 goto out;
15240
15241         genlmsg_end(msg, hdr);
15242
15243         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
15244                                 NL80211_MCGRP_MLME, GFP_KERNEL);
15245         return;
15246  out:
15247         nlmsg_free(msg);
15248 }
15249 EXPORT_SYMBOL(cfg80211_ft_event);
15250
15251 void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
15252 {
15253         struct cfg80211_registered_device *rdev;
15254         struct sk_buff *msg;
15255         void *hdr;
15256         u32 nlportid;
15257
15258         rdev = wiphy_to_rdev(wdev->wiphy);
15259         if (!rdev->crit_proto_nlportid)
15260                 return;
15261
15262         nlportid = rdev->crit_proto_nlportid;
15263         rdev->crit_proto_nlportid = 0;
15264
15265         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
15266         if (!msg)
15267                 return;
15268
15269         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
15270         if (!hdr)
15271                 goto nla_put_failure;
15272
15273         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15274             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15275                               NL80211_ATTR_PAD))
15276                 goto nla_put_failure;
15277
15278         genlmsg_end(msg, hdr);
15279
15280         genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
15281         return;
15282
15283  nla_put_failure:
15284         if (hdr)
15285                 genlmsg_cancel(msg, hdr);
15286         nlmsg_free(msg);
15287 }
15288 EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
15289
15290 void nl80211_send_ap_stopped(struct wireless_dev *wdev)
15291 {
15292         struct wiphy *wiphy = wdev->wiphy;
15293         struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
15294         struct sk_buff *msg;
15295         void *hdr;
15296
15297         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
15298         if (!msg)
15299                 return;
15300
15301         hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_STOP_AP);
15302         if (!hdr)
15303                 goto out;
15304
15305         if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
15306             nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex) ||
15307             nla_put_u64_64bit(msg, NL80211_ATTR_WDEV, wdev_id(wdev),
15308                               NL80211_ATTR_PAD))
15309                 goto out;
15310
15311         genlmsg_end(msg, hdr);
15312
15313         genlmsg_multicast_netns(&nl80211_fam, wiphy_net(wiphy), msg, 0,
15314                                 NL80211_MCGRP_MLME, GFP_KERNEL);
15315         return;
15316  out:
15317         nlmsg_free(msg);
15318 }
15319
15320 /* initialisation/exit functions */
15321
15322 int __init nl80211_init(void)
15323 {
15324         int err;
15325
15326         err = genl_register_family(&nl80211_fam);
15327         if (err)
15328                 return err;
15329
15330         err = netlink_register_notifier(&nl80211_netlink_notifier);
15331         if (err)
15332                 goto err_out;
15333
15334         return 0;
15335  err_out:
15336         genl_unregister_family(&nl80211_fam);
15337         return err;
15338 }
15339
15340 void nl80211_exit(void)
15341 {
15342         netlink_unregister_notifier(&nl80211_netlink_notifier);
15343         genl_unregister_family(&nl80211_fam);
15344 }