1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SME code for cfg80211
4 * both driver SME event handling and the SME implementation
5 * (for nl80211's connect() and wext)
6 *
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
8 * Copyright (C) 2009, 2020, 2022-2023 Intel Corporation. All rights reserved.
9 * Copyright 2017 Intel Deutschland GmbH
10 */
11
12 #include <linux/etherdevice.h>
13 #include <linux/if_arp.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/wireless.h>
17 #include <linux/export.h>
18 #include <net/iw_handler.h>
19 #include <net/cfg80211.h>
20 #include <net/rtnetlink.h>
21 #include "nl80211.h"
22 #include "reg.h"
23 #include "rdev-ops.h"
24
25 /*
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
27 * driver. This is for implementing nl80211's connect/disconnect
28 * and wireless extensions (if configured.)
29 */
30
31 struct cfg80211_conn {
32 struct cfg80211_connect_params params;
33 /* these are sub-states of the _CONNECTING sme_state */
34 enum {
35 CFG80211_CONN_SCANNING,
36 CFG80211_CONN_SCAN_AGAIN,
37 CFG80211_CONN_AUTHENTICATE_NEXT,
38 CFG80211_CONN_AUTHENTICATING,
39 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
40 CFG80211_CONN_ASSOCIATE_NEXT,
41 CFG80211_CONN_ASSOCIATING,
42 CFG80211_CONN_ASSOC_FAILED,
43 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
44 CFG80211_CONN_DEAUTH,
45 CFG80211_CONN_ABANDON,
46 CFG80211_CONN_CONNECTED,
47 } state;
48 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
49 const u8 *ie;
50 size_t ie_len;
51 bool auto_auth, prev_bssid_valid;
52 };
53
cfg80211_sme_free(struct wireless_dev * wdev)54 static void cfg80211_sme_free(struct wireless_dev *wdev)
55 {
56 if (!wdev->conn)
57 return;
58
59 kfree(wdev->conn->ie);
60 kfree(wdev->conn);
61 wdev->conn = NULL;
62 }
63
cfg80211_conn_scan(struct wireless_dev * wdev)64 static int cfg80211_conn_scan(struct wireless_dev *wdev)
65 {
66 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
67 struct cfg80211_scan_request *request;
68 int n_channels, err;
69
70 ASSERT_WDEV_LOCK(wdev);
71
72 if (rdev->scan_req || rdev->scan_msg)
73 return -EBUSY;
74
75 if (wdev->conn->params.channel)
76 n_channels = 1;
77 else
78 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
79
80 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
81 sizeof(request->channels[0]) * n_channels,
82 GFP_KERNEL);
83 if (!request)
84 return -ENOMEM;
85
86 if (wdev->conn->params.channel) {
87 enum nl80211_band band = wdev->conn->params.channel->band;
88 struct ieee80211_supported_band *sband =
89 wdev->wiphy->bands[band];
90
91 if (!sband) {
92 kfree(request);
93 return -EINVAL;
94 }
95 request->channels[0] = wdev->conn->params.channel;
96 request->rates[band] = (1 << sband->n_bitrates) - 1;
97 } else {
98 int i = 0, j;
99 enum nl80211_band band;
100 struct ieee80211_supported_band *bands;
101 struct ieee80211_channel *channel;
102
103 for (band = 0; band < NUM_NL80211_BANDS; band++) {
104 bands = wdev->wiphy->bands[band];
105 if (!bands)
106 continue;
107 for (j = 0; j < bands->n_channels; j++) {
108 channel = &bands->channels[j];
109 if (channel->flags & IEEE80211_CHAN_DISABLED)
110 continue;
111 request->channels[i++] = channel;
112 }
113 request->rates[band] = (1 << bands->n_bitrates) - 1;
114 }
115 n_channels = i;
116 }
117 request->n_channels = n_channels;
118 request->ssids = (void *)&request->channels[n_channels];
119 request->n_ssids = 1;
120
121 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
122 wdev->conn->params.ssid_len);
123 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
124
125 eth_broadcast_addr(request->bssid);
126
127 request->wdev = wdev;
128 request->wiphy = &rdev->wiphy;
129 request->scan_start = jiffies;
130
131 rdev->scan_req = request;
132
133 err = rdev_scan(rdev, request);
134 if (!err) {
135 wdev->conn->state = CFG80211_CONN_SCANNING;
136 nl80211_send_scan_start(rdev, wdev);
137 dev_hold(wdev->netdev);
138 } else {
139 rdev->scan_req = NULL;
140 kfree(request);
141 }
142 return err;
143 }
144
cfg80211_conn_do_work(struct wireless_dev * wdev,enum nl80211_timeout_reason * treason)145 static int cfg80211_conn_do_work(struct wireless_dev *wdev,
146 enum nl80211_timeout_reason *treason)
147 {
148 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
149 struct cfg80211_connect_params *params;
150 struct cfg80211_auth_request auth_req = {};
151 struct cfg80211_assoc_request req = {};
152 int err;
153
154 ASSERT_WDEV_LOCK(wdev);
155
156 if (!wdev->conn)
157 return 0;
158
159 params = &wdev->conn->params;
160
161 switch (wdev->conn->state) {
162 case CFG80211_CONN_SCANNING:
163 /* didn't find it during scan ... */
164 return -ENOENT;
165 case CFG80211_CONN_SCAN_AGAIN:
166 return cfg80211_conn_scan(wdev);
167 case CFG80211_CONN_AUTHENTICATE_NEXT:
168 if (WARN_ON(!rdev->ops->auth))
169 return -EOPNOTSUPP;
170 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
171 auth_req.key = params->key;
172 auth_req.key_len = params->key_len;
173 auth_req.key_idx = params->key_idx;
174 auth_req.auth_type = params->auth_type;
175 auth_req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
176 params->bssid,
177 params->ssid, params->ssid_len,
178 IEEE80211_BSS_TYPE_ESS,
179 IEEE80211_PRIVACY_ANY);
180 auth_req.link_id = -1;
181 err = cfg80211_mlme_auth(rdev, wdev->netdev, &auth_req);
182 cfg80211_put_bss(&rdev->wiphy, auth_req.bss);
183 return err;
184 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
185 *treason = NL80211_TIMEOUT_AUTH;
186 return -ENOTCONN;
187 case CFG80211_CONN_ASSOCIATE_NEXT:
188 if (WARN_ON(!rdev->ops->assoc))
189 return -EOPNOTSUPP;
190 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
191 if (wdev->conn->prev_bssid_valid)
192 req.prev_bssid = wdev->conn->prev_bssid;
193 req.ie = params->ie;
194 req.ie_len = params->ie_len;
195 req.use_mfp = params->mfp != NL80211_MFP_NO;
196 req.crypto = params->crypto;
197 req.flags = params->flags;
198 req.ht_capa = params->ht_capa;
199 req.ht_capa_mask = params->ht_capa_mask;
200 req.vht_capa = params->vht_capa;
201 req.vht_capa_mask = params->vht_capa_mask;
202 req.link_id = -1;
203
204 req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
205 params->bssid,
206 params->ssid, params->ssid_len,
207 IEEE80211_BSS_TYPE_ESS,
208 IEEE80211_PRIVACY_ANY);
209 if (!req.bss) {
210 err = -ENOENT;
211 } else {
212 err = cfg80211_mlme_assoc(rdev, wdev->netdev, &req);
213 cfg80211_put_bss(&rdev->wiphy, req.bss);
214 }
215
216 if (err)
217 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
218 NULL, 0,
219 WLAN_REASON_DEAUTH_LEAVING,
220 false);
221 return err;
222 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
223 *treason = NL80211_TIMEOUT_ASSOC;
224 fallthrough;
225 case CFG80211_CONN_ASSOC_FAILED:
226 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
227 NULL, 0,
228 WLAN_REASON_DEAUTH_LEAVING, false);
229 return -ENOTCONN;
230 case CFG80211_CONN_DEAUTH:
231 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
232 NULL, 0,
233 WLAN_REASON_DEAUTH_LEAVING, false);
234 fallthrough;
235 case CFG80211_CONN_ABANDON:
236 /* free directly, disconnected event already sent */
237 cfg80211_sme_free(wdev);
238 return 0;
239 default:
240 return 0;
241 }
242 }
243
cfg80211_conn_work(struct work_struct * work)244 void cfg80211_conn_work(struct work_struct *work)
245 {
246 struct cfg80211_registered_device *rdev =
247 container_of(work, struct cfg80211_registered_device, conn_work);
248 struct wireless_dev *wdev;
249 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
250 enum nl80211_timeout_reason treason;
251
252 wiphy_lock(&rdev->wiphy);
253
254 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
255 if (!wdev->netdev)
256 continue;
257
258 wdev_lock(wdev);
259 if (!netif_running(wdev->netdev)) {
260 wdev_unlock(wdev);
261 continue;
262 }
263 if (!wdev->conn ||
264 wdev->conn->state == CFG80211_CONN_CONNECTED) {
265 wdev_unlock(wdev);
266 continue;
267 }
268 if (wdev->conn->params.bssid) {
269 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
270 bssid = bssid_buf;
271 }
272 treason = NL80211_TIMEOUT_UNSPECIFIED;
273 if (cfg80211_conn_do_work(wdev, &treason)) {
274 struct cfg80211_connect_resp_params cr;
275
276 memset(&cr, 0, sizeof(cr));
277 cr.status = -1;
278 cr.links[0].bssid = bssid;
279 cr.timeout_reason = treason;
280 __cfg80211_connect_result(wdev->netdev, &cr, false);
281 }
282 wdev_unlock(wdev);
283 }
284
285 wiphy_unlock(&rdev->wiphy);
286 }
287
cfg80211_step_auth_next(struct cfg80211_conn * conn,struct cfg80211_bss * bss)288 static void cfg80211_step_auth_next(struct cfg80211_conn *conn,
289 struct cfg80211_bss *bss)
290 {
291 memcpy(conn->bssid, bss->bssid, ETH_ALEN);
292 conn->params.bssid = conn->bssid;
293 conn->params.channel = bss->channel;
294 conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
295 }
296
297 /* Returned bss is reference counted and must be cleaned up appropriately. */
cfg80211_get_conn_bss(struct wireless_dev * wdev)298 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
299 {
300 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
301 struct cfg80211_bss *bss;
302
303 ASSERT_WDEV_LOCK(wdev);
304
305 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
306 wdev->conn->params.bssid,
307 wdev->conn->params.ssid,
308 wdev->conn->params.ssid_len,
309 wdev->conn_bss_type,
310 IEEE80211_PRIVACY(wdev->conn->params.privacy));
311 if (!bss)
312 return NULL;
313
314 cfg80211_step_auth_next(wdev->conn, bss);
315 schedule_work(&rdev->conn_work);
316
317 return bss;
318 }
319
__cfg80211_sme_scan_done(struct net_device * dev)320 static void __cfg80211_sme_scan_done(struct net_device *dev)
321 {
322 struct wireless_dev *wdev = dev->ieee80211_ptr;
323 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
324 struct cfg80211_bss *bss;
325
326 ASSERT_WDEV_LOCK(wdev);
327
328 if (!wdev->conn)
329 return;
330
331 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
332 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
333 return;
334
335 bss = cfg80211_get_conn_bss(wdev);
336 if (bss)
337 cfg80211_put_bss(&rdev->wiphy, bss);
338 else
339 schedule_work(&rdev->conn_work);
340 }
341
cfg80211_sme_scan_done(struct net_device * dev)342 void cfg80211_sme_scan_done(struct net_device *dev)
343 {
344 struct wireless_dev *wdev = dev->ieee80211_ptr;
345
346 wdev_lock(wdev);
347 __cfg80211_sme_scan_done(dev);
348 wdev_unlock(wdev);
349 }
350
cfg80211_sme_rx_auth(struct wireless_dev * wdev,const u8 * buf,size_t len)351 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
352 {
353 struct wiphy *wiphy = wdev->wiphy;
354 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
355 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
356 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
357
358 ASSERT_WDEV_LOCK(wdev);
359
360 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
361 return;
362
363 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
364 wdev->conn->auto_auth &&
365 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
366 /* select automatically between only open, shared, leap */
367 switch (wdev->conn->params.auth_type) {
368 case NL80211_AUTHTYPE_OPEN_SYSTEM:
369 if (wdev->connect_keys)
370 wdev->conn->params.auth_type =
371 NL80211_AUTHTYPE_SHARED_KEY;
372 else
373 wdev->conn->params.auth_type =
374 NL80211_AUTHTYPE_NETWORK_EAP;
375 break;
376 case NL80211_AUTHTYPE_SHARED_KEY:
377 wdev->conn->params.auth_type =
378 NL80211_AUTHTYPE_NETWORK_EAP;
379 break;
380 default:
381 /* huh? */
382 wdev->conn->params.auth_type =
383 NL80211_AUTHTYPE_OPEN_SYSTEM;
384 break;
385 }
386 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
387 schedule_work(&rdev->conn_work);
388 } else if (status_code != WLAN_STATUS_SUCCESS) {
389 struct cfg80211_connect_resp_params cr;
390
391 memset(&cr, 0, sizeof(cr));
392 cr.status = status_code;
393 cr.links[0].bssid = mgmt->bssid;
394 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
395 __cfg80211_connect_result(wdev->netdev, &cr, false);
396 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
397 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
398 schedule_work(&rdev->conn_work);
399 }
400 }
401
cfg80211_sme_rx_assoc_resp(struct wireless_dev * wdev,u16 status)402 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
403 {
404 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
405
406 if (!wdev->conn)
407 return false;
408
409 if (status == WLAN_STATUS_SUCCESS) {
410 wdev->conn->state = CFG80211_CONN_CONNECTED;
411 return false;
412 }
413
414 if (wdev->conn->prev_bssid_valid) {
415 /*
416 * Some stupid APs don't accept reassoc, so we
417 * need to fall back to trying regular assoc;
418 * return true so no event is sent to userspace.
419 */
420 wdev->conn->prev_bssid_valid = false;
421 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
422 schedule_work(&rdev->conn_work);
423 return true;
424 }
425
426 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
427 schedule_work(&rdev->conn_work);
428 return false;
429 }
430
cfg80211_sme_deauth(struct wireless_dev * wdev)431 void cfg80211_sme_deauth(struct wireless_dev *wdev)
432 {
433 cfg80211_sme_free(wdev);
434 }
435
cfg80211_sme_auth_timeout(struct wireless_dev * wdev)436 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
437 {
438 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
439
440 if (!wdev->conn)
441 return;
442
443 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
444 schedule_work(&rdev->conn_work);
445 }
446
cfg80211_sme_disassoc(struct wireless_dev * wdev)447 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
448 {
449 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
450
451 if (!wdev->conn)
452 return;
453
454 wdev->conn->state = CFG80211_CONN_DEAUTH;
455 schedule_work(&rdev->conn_work);
456 }
457
cfg80211_sme_assoc_timeout(struct wireless_dev * wdev)458 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
459 {
460 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
461
462 if (!wdev->conn)
463 return;
464
465 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
466 schedule_work(&rdev->conn_work);
467 }
468
cfg80211_sme_abandon_assoc(struct wireless_dev * wdev)469 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
470 {
471 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
472
473 if (!wdev->conn)
474 return;
475
476 wdev->conn->state = CFG80211_CONN_ABANDON;
477 schedule_work(&rdev->conn_work);
478 }
479
cfg80211_wdev_release_bsses(struct wireless_dev * wdev)480 static void cfg80211_wdev_release_bsses(struct wireless_dev *wdev)
481 {
482 unsigned int link;
483
484 for_each_valid_link(wdev, link) {
485 if (!wdev->links[link].client.current_bss)
486 continue;
487 cfg80211_unhold_bss(wdev->links[link].client.current_bss);
488 cfg80211_put_bss(wdev->wiphy,
489 &wdev->links[link].client.current_bss->pub);
490 wdev->links[link].client.current_bss = NULL;
491 }
492 }
493
cfg80211_wdev_release_link_bsses(struct wireless_dev * wdev,u16 link_mask)494 void cfg80211_wdev_release_link_bsses(struct wireless_dev *wdev, u16 link_mask)
495 {
496 unsigned int link;
497
498 for_each_valid_link(wdev, link) {
499 if (!wdev->links[link].client.current_bss ||
500 !(link_mask & BIT(link)))
501 continue;
502 cfg80211_unhold_bss(wdev->links[link].client.current_bss);
503 cfg80211_put_bss(wdev->wiphy,
504 &wdev->links[link].client.current_bss->pub);
505 wdev->links[link].client.current_bss = NULL;
506 }
507 }
508
cfg80211_sme_get_conn_ies(struct wireless_dev * wdev,const u8 * ies,size_t ies_len,const u8 ** out_ies,size_t * out_ies_len)509 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
510 const u8 *ies, size_t ies_len,
511 const u8 **out_ies, size_t *out_ies_len)
512 {
513 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
514 u8 *buf;
515 size_t offs;
516
517 if (!rdev->wiphy.extended_capabilities_len ||
518 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
519 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
520 if (!*out_ies)
521 return -ENOMEM;
522 *out_ies_len = ies_len;
523 return 0;
524 }
525
526 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
527 GFP_KERNEL);
528 if (!buf)
529 return -ENOMEM;
530
531 if (ies_len) {
532 static const u8 before_extcapa[] = {
533 /* not listing IEs expected to be created by driver */
534 WLAN_EID_RSN,
535 WLAN_EID_QOS_CAPA,
536 WLAN_EID_RRM_ENABLED_CAPABILITIES,
537 WLAN_EID_MOBILITY_DOMAIN,
538 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
539 WLAN_EID_BSS_COEX_2040,
540 };
541
542 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
543 ARRAY_SIZE(before_extcapa), 0);
544 memcpy(buf, ies, offs);
545 /* leave a whole for extended capabilities IE */
546 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
547 ies + offs, ies_len - offs);
548 } else {
549 offs = 0;
550 }
551
552 /* place extended capabilities IE (with only driver capabilities) */
553 buf[offs] = WLAN_EID_EXT_CAPABILITY;
554 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
555 memcpy(buf + offs + 2,
556 rdev->wiphy.extended_capabilities,
557 rdev->wiphy.extended_capabilities_len);
558
559 *out_ies = buf;
560 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
561
562 return 0;
563 }
564
cfg80211_sme_connect(struct wireless_dev * wdev,struct cfg80211_connect_params * connect,const u8 * prev_bssid)565 static int cfg80211_sme_connect(struct wireless_dev *wdev,
566 struct cfg80211_connect_params *connect,
567 const u8 *prev_bssid)
568 {
569 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
570 struct cfg80211_bss *bss;
571 int err;
572
573 if (!rdev->ops->auth || !rdev->ops->assoc)
574 return -EOPNOTSUPP;
575
576 cfg80211_wdev_release_bsses(wdev);
577
578 if (wdev->connected) {
579 cfg80211_sme_free(wdev);
580 wdev->connected = false;
581 }
582
583 if (wdev->conn)
584 return -EINPROGRESS;
585
586 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
587 if (!wdev->conn)
588 return -ENOMEM;
589
590 /*
591 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
592 */
593 memcpy(&wdev->conn->params, connect, sizeof(*connect));
594 if (connect->bssid) {
595 wdev->conn->params.bssid = wdev->conn->bssid;
596 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
597 }
598
599 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
600 &wdev->conn->ie,
601 &wdev->conn->params.ie_len)) {
602 kfree(wdev->conn);
603 wdev->conn = NULL;
604 return -ENOMEM;
605 }
606 wdev->conn->params.ie = wdev->conn->ie;
607
608 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
609 wdev->conn->auto_auth = true;
610 /* start with open system ... should mostly work */
611 wdev->conn->params.auth_type =
612 NL80211_AUTHTYPE_OPEN_SYSTEM;
613 } else {
614 wdev->conn->auto_auth = false;
615 }
616
617 wdev->conn->params.ssid = wdev->u.client.ssid;
618 wdev->conn->params.ssid_len = wdev->u.client.ssid_len;
619
620 /* see if we have the bss already */
621 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
622 wdev->conn->params.bssid,
623 wdev->conn->params.ssid,
624 wdev->conn->params.ssid_len,
625 wdev->conn_bss_type,
626 IEEE80211_PRIVACY(wdev->conn->params.privacy));
627
628 if (prev_bssid) {
629 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
630 wdev->conn->prev_bssid_valid = true;
631 }
632
633 /* we're good if we have a matching bss struct */
634 if (bss) {
635 enum nl80211_timeout_reason treason;
636
637 cfg80211_step_auth_next(wdev->conn, bss);
638 err = cfg80211_conn_do_work(wdev, &treason);
639 cfg80211_put_bss(wdev->wiphy, bss);
640 } else {
641 /* otherwise we'll need to scan for the AP first */
642 err = cfg80211_conn_scan(wdev);
643
644 /*
645 * If we can't scan right now, then we need to scan again
646 * after the current scan finished, since the parameters
647 * changed (unless we find a good AP anyway).
648 */
649 if (err == -EBUSY) {
650 err = 0;
651 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
652 }
653 }
654
655 if (err)
656 cfg80211_sme_free(wdev);
657
658 return err;
659 }
660
cfg80211_sme_disconnect(struct wireless_dev * wdev,u16 reason)661 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
662 {
663 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
664 int err;
665
666 if (!wdev->conn)
667 return 0;
668
669 if (!rdev->ops->deauth)
670 return -EOPNOTSUPP;
671
672 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
673 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
674 err = 0;
675 goto out;
676 }
677
678 /* wdev->conn->params.bssid must be set if > SCANNING */
679 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
680 wdev->conn->params.bssid,
681 NULL, 0, reason, false);
682 out:
683 cfg80211_sme_free(wdev);
684 return err;
685 }
686
687 /*
688 * code shared for in-device and software SME
689 */
690
cfg80211_is_all_idle(void)691 static bool cfg80211_is_all_idle(void)
692 {
693 struct cfg80211_registered_device *rdev;
694 struct wireless_dev *wdev;
695 bool is_all_idle = true;
696
697 /*
698 * All devices must be idle as otherwise if you are actively
699 * scanning some new beacon hints could be learned and would
700 * count as new regulatory hints.
701 * Also if there is any other active beaconing interface we
702 * need not issue a disconnect hint and reset any info such
703 * as chan dfs state, etc.
704 */
705 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
706 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
707 wdev_lock(wdev);
708 if (wdev->conn || wdev->connected ||
709 cfg80211_beaconing_iface_active(wdev))
710 is_all_idle = false;
711 wdev_unlock(wdev);
712 }
713 }
714
715 return is_all_idle;
716 }
717
disconnect_work(struct work_struct * work)718 static void disconnect_work(struct work_struct *work)
719 {
720 rtnl_lock();
721 if (cfg80211_is_all_idle())
722 regulatory_hint_disconnect();
723 rtnl_unlock();
724 }
725
726 DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
727
728 static void
cfg80211_connect_result_release_bsses(struct wireless_dev * wdev,struct cfg80211_connect_resp_params * cr)729 cfg80211_connect_result_release_bsses(struct wireless_dev *wdev,
730 struct cfg80211_connect_resp_params *cr)
731 {
732 unsigned int link;
733
734 for_each_valid_link(cr, link) {
735 if (!cr->links[link].bss)
736 continue;
737 cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
738 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
739 }
740 }
741
742 /*
743 * API calls for drivers implementing connect/disconnect and
744 * SME event handling
745 */
746
747 /* This method must consume bss one way or another */
__cfg80211_connect_result(struct net_device * dev,struct cfg80211_connect_resp_params * cr,bool wextev)748 void __cfg80211_connect_result(struct net_device *dev,
749 struct cfg80211_connect_resp_params *cr,
750 bool wextev)
751 {
752 struct wireless_dev *wdev = dev->ieee80211_ptr;
753 const struct element *country_elem = NULL;
754 const struct element *ssid;
755 const u8 *country_data;
756 u8 country_datalen;
757 #ifdef CONFIG_CFG80211_WEXT
758 union iwreq_data wrqu;
759 #endif
760 unsigned int link;
761 const u8 *connected_addr;
762 bool bss_not_found = false;
763
764 ASSERT_WDEV_LOCK(wdev);
765
766 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
767 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
768 goto out;
769
770 if (cr->valid_links) {
771 if (WARN_ON(!cr->ap_mld_addr))
772 goto out;
773
774 for_each_valid_link(cr, link) {
775 if (WARN_ON(!cr->links[link].addr))
776 goto out;
777 }
778
779 if (WARN_ON(wdev->connect_keys))
780 goto out;
781 }
782
783 wdev->unprot_beacon_reported = 0;
784 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
785 GFP_KERNEL);
786 connected_addr = cr->valid_links ? cr->ap_mld_addr : cr->links[0].bssid;
787
788 #ifdef CONFIG_CFG80211_WEXT
789 if (wextev && !cr->valid_links) {
790 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
791 memset(&wrqu, 0, sizeof(wrqu));
792 wrqu.data.length = cr->req_ie_len;
793 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
794 cr->req_ie);
795 }
796
797 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
798 memset(&wrqu, 0, sizeof(wrqu));
799 wrqu.data.length = cr->resp_ie_len;
800 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
801 cr->resp_ie);
802 }
803
804 memset(&wrqu, 0, sizeof(wrqu));
805 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
806 if (connected_addr && cr->status == WLAN_STATUS_SUCCESS) {
807 memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
808 memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
809 wdev->wext.prev_bssid_valid = true;
810 }
811 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
812 }
813 #endif
814
815 if (cr->status == WLAN_STATUS_SUCCESS) {
816 if (!wiphy_to_rdev(wdev->wiphy)->ops->connect) {
817 for_each_valid_link(cr, link) {
818 if (WARN_ON_ONCE(!cr->links[link].bss))
819 break;
820 }
821 }
822
823 for_each_valid_link(cr, link) {
824 /* don't do extra lookups for failures */
825 if (cr->links[link].status != WLAN_STATUS_SUCCESS)
826 continue;
827
828 if (cr->links[link].bss)
829 continue;
830
831 cr->links[link].bss =
832 cfg80211_get_bss(wdev->wiphy, NULL,
833 cr->links[link].bssid,
834 wdev->u.client.ssid,
835 wdev->u.client.ssid_len,
836 wdev->conn_bss_type,
837 IEEE80211_PRIVACY_ANY);
838 if (!cr->links[link].bss) {
839 bss_not_found = true;
840 break;
841 }
842 cfg80211_hold_bss(bss_from_pub(cr->links[link].bss));
843 }
844 }
845
846 cfg80211_wdev_release_bsses(wdev);
847
848 if (cr->status != WLAN_STATUS_SUCCESS) {
849 kfree_sensitive(wdev->connect_keys);
850 wdev->connect_keys = NULL;
851 wdev->u.client.ssid_len = 0;
852 wdev->conn_owner_nlportid = 0;
853 cfg80211_connect_result_release_bsses(wdev, cr);
854 cfg80211_sme_free(wdev);
855 return;
856 }
857
858 if (WARN_ON(bss_not_found)) {
859 cfg80211_connect_result_release_bsses(wdev, cr);
860 return;
861 }
862
863 memset(wdev->links, 0, sizeof(wdev->links));
864 for_each_valid_link(cr, link) {
865 if (cr->links[link].status == WLAN_STATUS_SUCCESS)
866 continue;
867 cr->valid_links &= ~BIT(link);
868 /* don't require bss pointer for failed links */
869 if (!cr->links[link].bss)
870 continue;
871 cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
872 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
873 }
874 wdev->valid_links = cr->valid_links;
875 for_each_valid_link(cr, link)
876 wdev->links[link].client.current_bss =
877 bss_from_pub(cr->links[link].bss);
878 wdev->connected = true;
879 ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
880 if (cr->valid_links) {
881 for_each_valid_link(cr, link)
882 memcpy(wdev->links[link].addr, cr->links[link].addr,
883 ETH_ALEN);
884 }
885
886 cfg80211_upload_connect_keys(wdev);
887
888 rcu_read_lock();
889 for_each_valid_link(cr, link) {
890 country_elem =
891 ieee80211_bss_get_elem(cr->links[link].bss,
892 WLAN_EID_COUNTRY);
893 if (country_elem)
894 break;
895 }
896 if (!country_elem) {
897 rcu_read_unlock();
898 return;
899 }
900
901 country_datalen = country_elem->datalen;
902 country_data = kmemdup(country_elem->data, country_datalen, GFP_ATOMIC);
903 rcu_read_unlock();
904
905 if (!country_data)
906 return;
907
908 regulatory_hint_country_ie(wdev->wiphy,
909 cr->links[link].bss->channel->band,
910 country_data, country_datalen);
911 kfree(country_data);
912
913 if (!wdev->u.client.ssid_len) {
914 rcu_read_lock();
915 for_each_valid_link(cr, link) {
916 ssid = ieee80211_bss_get_elem(cr->links[link].bss,
917 WLAN_EID_SSID);
918
919 if (!ssid || !ssid->datalen)
920 continue;
921
922 memcpy(wdev->u.client.ssid, ssid->data, ssid->datalen);
923 wdev->u.client.ssid_len = ssid->datalen;
924 break;
925 }
926 rcu_read_unlock();
927 }
928
929 return;
930 out:
931 for_each_valid_link(cr, link)
932 cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
933 }
934
cfg80211_update_link_bss(struct wireless_dev * wdev,struct cfg80211_bss ** bss)935 static void cfg80211_update_link_bss(struct wireless_dev *wdev,
936 struct cfg80211_bss **bss)
937 {
938 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
939 struct cfg80211_internal_bss *ibss;
940
941 if (!*bss)
942 return;
943
944 ibss = bss_from_pub(*bss);
945 if (list_empty(&ibss->list)) {
946 struct cfg80211_bss *found = NULL, *tmp = *bss;
947
948 found = cfg80211_get_bss(wdev->wiphy, NULL,
949 (*bss)->bssid,
950 wdev->u.client.ssid,
951 wdev->u.client.ssid_len,
952 wdev->conn_bss_type,
953 IEEE80211_PRIVACY_ANY);
954 if (found) {
955 /* The same BSS is already updated so use it
956 * instead, as it has latest info.
957 */
958 *bss = found;
959 } else {
960 /* Update with BSS provided by driver, it will
961 * be freshly added and ref cnted, we can free
962 * the old one.
963 *
964 * signal_valid can be false, as we are not
965 * expecting the BSS to be found.
966 *
967 * keep the old timestamp to avoid confusion
968 */
969 cfg80211_bss_update(rdev, ibss, false,
970 ibss->ts);
971 }
972
973 cfg80211_put_bss(wdev->wiphy, tmp);
974 }
975 }
976
977 /* Consumes bss object(s) one way or another */
cfg80211_connect_done(struct net_device * dev,struct cfg80211_connect_resp_params * params,gfp_t gfp)978 void cfg80211_connect_done(struct net_device *dev,
979 struct cfg80211_connect_resp_params *params,
980 gfp_t gfp)
981 {
982 struct wireless_dev *wdev = dev->ieee80211_ptr;
983 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
984 struct cfg80211_event *ev;
985 unsigned long flags;
986 u8 *next;
987 size_t link_info_size = 0;
988 unsigned int link;
989
990 for_each_valid_link(params, link) {
991 cfg80211_update_link_bss(wdev, ¶ms->links[link].bss);
992 link_info_size += params->links[link].bssid ? ETH_ALEN : 0;
993 link_info_size += params->links[link].addr ? ETH_ALEN : 0;
994 }
995
996 ev = kzalloc(sizeof(*ev) + (params->ap_mld_addr ? ETH_ALEN : 0) +
997 params->req_ie_len + params->resp_ie_len +
998 params->fils.kek_len + params->fils.pmk_len +
999 (params->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
1000 gfp);
1001
1002 if (!ev) {
1003 for_each_valid_link(params, link)
1004 cfg80211_put_bss(wdev->wiphy,
1005 params->links[link].bss);
1006 return;
1007 }
1008
1009 ev->type = EVENT_CONNECT_RESULT;
1010 next = ((u8 *)ev) + sizeof(*ev);
1011 if (params->ap_mld_addr) {
1012 ev->cr.ap_mld_addr = next;
1013 memcpy((void *)ev->cr.ap_mld_addr, params->ap_mld_addr,
1014 ETH_ALEN);
1015 next += ETH_ALEN;
1016 }
1017 if (params->req_ie_len) {
1018 ev->cr.req_ie = next;
1019 ev->cr.req_ie_len = params->req_ie_len;
1020 memcpy((void *)ev->cr.req_ie, params->req_ie,
1021 params->req_ie_len);
1022 next += params->req_ie_len;
1023 }
1024 if (params->resp_ie_len) {
1025 ev->cr.resp_ie = next;
1026 ev->cr.resp_ie_len = params->resp_ie_len;
1027 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
1028 params->resp_ie_len);
1029 next += params->resp_ie_len;
1030 }
1031 if (params->fils.kek_len) {
1032 ev->cr.fils.kek = next;
1033 ev->cr.fils.kek_len = params->fils.kek_len;
1034 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
1035 params->fils.kek_len);
1036 next += params->fils.kek_len;
1037 }
1038 if (params->fils.pmk_len) {
1039 ev->cr.fils.pmk = next;
1040 ev->cr.fils.pmk_len = params->fils.pmk_len;
1041 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
1042 params->fils.pmk_len);
1043 next += params->fils.pmk_len;
1044 }
1045 if (params->fils.pmkid) {
1046 ev->cr.fils.pmkid = next;
1047 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
1048 WLAN_PMKID_LEN);
1049 next += WLAN_PMKID_LEN;
1050 }
1051 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
1052 if (params->fils.update_erp_next_seq_num)
1053 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
1054 ev->cr.valid_links = params->valid_links;
1055 for_each_valid_link(params, link) {
1056 if (params->links[link].bss)
1057 cfg80211_hold_bss(
1058 bss_from_pub(params->links[link].bss));
1059 ev->cr.links[link].bss = params->links[link].bss;
1060 ev->cr.links[link].status = params->links[link].status;
1061
1062 if (params->links[link].addr) {
1063 ev->cr.links[link].addr = next;
1064 memcpy((void *)ev->cr.links[link].addr,
1065 params->links[link].addr,
1066 ETH_ALEN);
1067 next += ETH_ALEN;
1068 }
1069 if (params->links[link].bssid) {
1070 ev->cr.links[link].bssid = next;
1071 memcpy((void *)ev->cr.links[link].bssid,
1072 params->links[link].bssid,
1073 ETH_ALEN);
1074 next += ETH_ALEN;
1075 }
1076 }
1077 ev->cr.status = params->status;
1078 ev->cr.timeout_reason = params->timeout_reason;
1079
1080 spin_lock_irqsave(&wdev->event_lock, flags);
1081 list_add_tail(&ev->list, &wdev->event_list);
1082 spin_unlock_irqrestore(&wdev->event_lock, flags);
1083 queue_work(cfg80211_wq, &rdev->event_work);
1084 }
1085 EXPORT_SYMBOL(cfg80211_connect_done);
1086
1087 /* Consumes bss object one way or another */
__cfg80211_roamed(struct wireless_dev * wdev,struct cfg80211_roam_info * info)1088 void __cfg80211_roamed(struct wireless_dev *wdev,
1089 struct cfg80211_roam_info *info)
1090 {
1091 #ifdef CONFIG_CFG80211_WEXT
1092 union iwreq_data wrqu;
1093 #endif
1094 unsigned int link;
1095 const u8 *connected_addr;
1096
1097 ASSERT_WDEV_LOCK(wdev);
1098
1099 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1100 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1101 goto out;
1102
1103 if (WARN_ON(!wdev->connected))
1104 goto out;
1105
1106 if (info->valid_links) {
1107 if (WARN_ON(!info->ap_mld_addr))
1108 goto out;
1109
1110 for_each_valid_link(info, link) {
1111 if (WARN_ON(!info->links[link].addr))
1112 goto out;
1113 }
1114 }
1115
1116 cfg80211_wdev_release_bsses(wdev);
1117
1118 for_each_valid_link(info, link) {
1119 if (WARN_ON(!info->links[link].bss))
1120 goto out;
1121 }
1122
1123 memset(wdev->links, 0, sizeof(wdev->links));
1124 wdev->valid_links = info->valid_links;
1125 for_each_valid_link(info, link) {
1126 cfg80211_hold_bss(bss_from_pub(info->links[link].bss));
1127 wdev->links[link].client.current_bss =
1128 bss_from_pub(info->links[link].bss);
1129 }
1130
1131 connected_addr = info->valid_links ?
1132 info->ap_mld_addr :
1133 info->links[0].bss->bssid;
1134 ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
1135 if (info->valid_links) {
1136 for_each_valid_link(info, link)
1137 memcpy(wdev->links[link].addr, info->links[link].addr,
1138 ETH_ALEN);
1139 }
1140 wdev->unprot_beacon_reported = 0;
1141 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
1142 wdev->netdev, info, GFP_KERNEL);
1143
1144 #ifdef CONFIG_CFG80211_WEXT
1145 if (!info->valid_links) {
1146 if (info->req_ie) {
1147 memset(&wrqu, 0, sizeof(wrqu));
1148 wrqu.data.length = info->req_ie_len;
1149 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
1150 &wrqu, info->req_ie);
1151 }
1152
1153 if (info->resp_ie) {
1154 memset(&wrqu, 0, sizeof(wrqu));
1155 wrqu.data.length = info->resp_ie_len;
1156 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
1157 &wrqu, info->resp_ie);
1158 }
1159
1160 memset(&wrqu, 0, sizeof(wrqu));
1161 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1162 memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
1163 memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
1164 wdev->wext.prev_bssid_valid = true;
1165 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
1166 }
1167 #endif
1168
1169 return;
1170 out:
1171 for_each_valid_link(info, link)
1172 cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1173 }
1174
1175 /* Consumes info->links.bss object(s) one way or another */
cfg80211_roamed(struct net_device * dev,struct cfg80211_roam_info * info,gfp_t gfp)1176 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
1177 gfp_t gfp)
1178 {
1179 struct wireless_dev *wdev = dev->ieee80211_ptr;
1180 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1181 struct cfg80211_event *ev;
1182 unsigned long flags;
1183 u8 *next;
1184 unsigned int link;
1185 size_t link_info_size = 0;
1186 bool bss_not_found = false;
1187
1188 for_each_valid_link(info, link) {
1189 link_info_size += info->links[link].addr ? ETH_ALEN : 0;
1190 link_info_size += info->links[link].bssid ? ETH_ALEN : 0;
1191
1192 if (info->links[link].bss)
1193 continue;
1194
1195 info->links[link].bss =
1196 cfg80211_get_bss(wdev->wiphy,
1197 info->links[link].channel,
1198 info->links[link].bssid,
1199 wdev->u.client.ssid,
1200 wdev->u.client.ssid_len,
1201 wdev->conn_bss_type,
1202 IEEE80211_PRIVACY_ANY);
1203
1204 if (!info->links[link].bss) {
1205 bss_not_found = true;
1206 break;
1207 }
1208 }
1209
1210 if (WARN_ON(bss_not_found))
1211 goto out;
1212
1213 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
1214 info->fils.kek_len + info->fils.pmk_len +
1215 (info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
1216 (info->ap_mld_addr ? ETH_ALEN : 0) + link_info_size, gfp);
1217 if (!ev)
1218 goto out;
1219
1220 ev->type = EVENT_ROAMED;
1221 next = ((u8 *)ev) + sizeof(*ev);
1222 if (info->req_ie_len) {
1223 ev->rm.req_ie = next;
1224 ev->rm.req_ie_len = info->req_ie_len;
1225 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
1226 next += info->req_ie_len;
1227 }
1228 if (info->resp_ie_len) {
1229 ev->rm.resp_ie = next;
1230 ev->rm.resp_ie_len = info->resp_ie_len;
1231 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
1232 info->resp_ie_len);
1233 next += info->resp_ie_len;
1234 }
1235 if (info->fils.kek_len) {
1236 ev->rm.fils.kek = next;
1237 ev->rm.fils.kek_len = info->fils.kek_len;
1238 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1239 info->fils.kek_len);
1240 next += info->fils.kek_len;
1241 }
1242 if (info->fils.pmk_len) {
1243 ev->rm.fils.pmk = next;
1244 ev->rm.fils.pmk_len = info->fils.pmk_len;
1245 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1246 info->fils.pmk_len);
1247 next += info->fils.pmk_len;
1248 }
1249 if (info->fils.pmkid) {
1250 ev->rm.fils.pmkid = next;
1251 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1252 WLAN_PMKID_LEN);
1253 next += WLAN_PMKID_LEN;
1254 }
1255 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1256 if (info->fils.update_erp_next_seq_num)
1257 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
1258 if (info->ap_mld_addr) {
1259 ev->rm.ap_mld_addr = next;
1260 memcpy((void *)ev->rm.ap_mld_addr, info->ap_mld_addr,
1261 ETH_ALEN);
1262 next += ETH_ALEN;
1263 }
1264 ev->rm.valid_links = info->valid_links;
1265 for_each_valid_link(info, link) {
1266 ev->rm.links[link].bss = info->links[link].bss;
1267
1268 if (info->links[link].addr) {
1269 ev->rm.links[link].addr = next;
1270 memcpy((void *)ev->rm.links[link].addr,
1271 info->links[link].addr,
1272 ETH_ALEN);
1273 next += ETH_ALEN;
1274 }
1275
1276 if (info->links[link].bssid) {
1277 ev->rm.links[link].bssid = next;
1278 memcpy((void *)ev->rm.links[link].bssid,
1279 info->links[link].bssid,
1280 ETH_ALEN);
1281 next += ETH_ALEN;
1282 }
1283 }
1284
1285 spin_lock_irqsave(&wdev->event_lock, flags);
1286 list_add_tail(&ev->list, &wdev->event_list);
1287 spin_unlock_irqrestore(&wdev->event_lock, flags);
1288 queue_work(cfg80211_wq, &rdev->event_work);
1289
1290 return;
1291 out:
1292 for_each_valid_link(info, link)
1293 cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1294
1295 }
1296 EXPORT_SYMBOL(cfg80211_roamed);
1297
__cfg80211_port_authorized(struct wireless_dev * wdev,const u8 * bssid,const u8 * td_bitmap,u8 td_bitmap_len)1298 void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid,
1299 const u8 *td_bitmap, u8 td_bitmap_len)
1300 {
1301 ASSERT_WDEV_LOCK(wdev);
1302
1303 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1304 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1305 return;
1306
1307 if (WARN_ON(!wdev->connected) ||
1308 WARN_ON(!ether_addr_equal(wdev->u.client.connected_addr, bssid)))
1309 return;
1310
1311 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1312 bssid, td_bitmap, td_bitmap_len);
1313 }
1314
cfg80211_port_authorized(struct net_device * dev,const u8 * bssid,const u8 * td_bitmap,u8 td_bitmap_len,gfp_t gfp)1315 void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1316 const u8 *td_bitmap, u8 td_bitmap_len, gfp_t gfp)
1317 {
1318 struct wireless_dev *wdev = dev->ieee80211_ptr;
1319 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1320 struct cfg80211_event *ev;
1321 unsigned long flags;
1322
1323 if (WARN_ON(!bssid))
1324 return;
1325
1326 ev = kzalloc(sizeof(*ev) + td_bitmap_len, gfp);
1327 if (!ev)
1328 return;
1329
1330 ev->type = EVENT_PORT_AUTHORIZED;
1331 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1332 ev->pa.td_bitmap = ((u8 *)ev) + sizeof(*ev);
1333 ev->pa.td_bitmap_len = td_bitmap_len;
1334 memcpy((void *)ev->pa.td_bitmap, td_bitmap, td_bitmap_len);
1335
1336 /*
1337 * Use the wdev event list so that if there are pending
1338 * connected/roamed events, they will be reported first.
1339 */
1340 spin_lock_irqsave(&wdev->event_lock, flags);
1341 list_add_tail(&ev->list, &wdev->event_list);
1342 spin_unlock_irqrestore(&wdev->event_lock, flags);
1343 queue_work(cfg80211_wq, &rdev->event_work);
1344 }
1345 EXPORT_SYMBOL(cfg80211_port_authorized);
1346
__cfg80211_disconnected(struct net_device * dev,const u8 * ie,size_t ie_len,u16 reason,bool from_ap)1347 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1348 size_t ie_len, u16 reason, bool from_ap)
1349 {
1350 struct wireless_dev *wdev = dev->ieee80211_ptr;
1351 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1352 int i;
1353 #ifdef CONFIG_CFG80211_WEXT
1354 union iwreq_data wrqu;
1355 #endif
1356
1357 ASSERT_WDEV_LOCK(wdev);
1358
1359 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1360 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1361 return;
1362
1363 cfg80211_wdev_release_bsses(wdev);
1364 wdev->valid_links = 0;
1365 wdev->connected = false;
1366 wdev->u.client.ssid_len = 0;
1367 wdev->conn_owner_nlportid = 0;
1368 kfree_sensitive(wdev->connect_keys);
1369 wdev->connect_keys = NULL;
1370
1371 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1372
1373 /* stop critical protocol if supported */
1374 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1375 rdev->crit_proto_nlportid = 0;
1376 rdev_crit_proto_stop(rdev, wdev);
1377 }
1378
1379 /*
1380 * Delete all the keys ... pairwise keys can't really
1381 * exist any more anyway, but default keys might.
1382 */
1383 if (rdev->ops->del_key) {
1384 int max_key_idx = 5;
1385
1386 if (wiphy_ext_feature_isset(
1387 wdev->wiphy,
1388 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1389 wiphy_ext_feature_isset(
1390 wdev->wiphy,
1391 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
1392 max_key_idx = 7;
1393 for (i = 0; i <= max_key_idx; i++)
1394 rdev_del_key(rdev, dev, -1, i, false, NULL);
1395 }
1396
1397 rdev_set_qos_map(rdev, dev, NULL);
1398
1399 #ifdef CONFIG_CFG80211_WEXT
1400 memset(&wrqu, 0, sizeof(wrqu));
1401 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1402 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1403 wdev->wext.connect.ssid_len = 0;
1404 #endif
1405
1406 schedule_work(&cfg80211_disconnect_work);
1407 }
1408
cfg80211_disconnected(struct net_device * dev,u16 reason,const u8 * ie,size_t ie_len,bool locally_generated,gfp_t gfp)1409 void cfg80211_disconnected(struct net_device *dev, u16 reason,
1410 const u8 *ie, size_t ie_len,
1411 bool locally_generated, gfp_t gfp)
1412 {
1413 struct wireless_dev *wdev = dev->ieee80211_ptr;
1414 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1415 struct cfg80211_event *ev;
1416 unsigned long flags;
1417
1418 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1419 if (!ev)
1420 return;
1421
1422 ev->type = EVENT_DISCONNECTED;
1423 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1424 ev->dc.ie_len = ie_len;
1425 memcpy((void *)ev->dc.ie, ie, ie_len);
1426 ev->dc.reason = reason;
1427 ev->dc.locally_generated = locally_generated;
1428
1429 spin_lock_irqsave(&wdev->event_lock, flags);
1430 list_add_tail(&ev->list, &wdev->event_list);
1431 spin_unlock_irqrestore(&wdev->event_lock, flags);
1432 queue_work(cfg80211_wq, &rdev->event_work);
1433 }
1434 EXPORT_SYMBOL(cfg80211_disconnected);
1435
1436 /*
1437 * API calls for nl80211/wext compatibility code
1438 */
cfg80211_connect(struct cfg80211_registered_device * rdev,struct net_device * dev,struct cfg80211_connect_params * connect,struct cfg80211_cached_keys * connkeys,const u8 * prev_bssid)1439 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1440 struct net_device *dev,
1441 struct cfg80211_connect_params *connect,
1442 struct cfg80211_cached_keys *connkeys,
1443 const u8 *prev_bssid)
1444 {
1445 struct wireless_dev *wdev = dev->ieee80211_ptr;
1446 int err;
1447
1448 ASSERT_WDEV_LOCK(wdev);
1449
1450 /*
1451 * If we have an ssid_len, we're trying to connect or are
1452 * already connected, so reject a new SSID unless it's the
1453 * same (which is the case for re-association.)
1454 */
1455 if (wdev->u.client.ssid_len &&
1456 (wdev->u.client.ssid_len != connect->ssid_len ||
1457 memcmp(wdev->u.client.ssid, connect->ssid, wdev->u.client.ssid_len)))
1458 return -EALREADY;
1459
1460 /*
1461 * If connected, reject (re-)association unless prev_bssid
1462 * matches the current BSSID.
1463 */
1464 if (wdev->connected) {
1465 if (!prev_bssid)
1466 return -EALREADY;
1467 if (!ether_addr_equal(prev_bssid,
1468 wdev->u.client.connected_addr))
1469 return -ENOTCONN;
1470 }
1471
1472 /*
1473 * Reject if we're in the process of connecting with WEP,
1474 * this case isn't very interesting and trying to handle
1475 * it would make the code much more complex.
1476 */
1477 if (wdev->connect_keys)
1478 return -EINPROGRESS;
1479
1480 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1481 rdev->wiphy.ht_capa_mod_mask);
1482 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1483 rdev->wiphy.vht_capa_mod_mask);
1484
1485 if (connkeys && connkeys->def >= 0) {
1486 int idx;
1487 u32 cipher;
1488
1489 idx = connkeys->def;
1490 cipher = connkeys->params[idx].cipher;
1491 /* If given a WEP key we may need it for shared key auth */
1492 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1493 cipher == WLAN_CIPHER_SUITE_WEP104) {
1494 connect->key_idx = idx;
1495 connect->key = connkeys->params[idx].key;
1496 connect->key_len = connkeys->params[idx].key_len;
1497
1498 /*
1499 * If ciphers are not set (e.g. when going through
1500 * iwconfig), we have to set them appropriately here.
1501 */
1502 if (connect->crypto.cipher_group == 0)
1503 connect->crypto.cipher_group = cipher;
1504
1505 if (connect->crypto.n_ciphers_pairwise == 0) {
1506 connect->crypto.n_ciphers_pairwise = 1;
1507 connect->crypto.ciphers_pairwise[0] = cipher;
1508 }
1509 }
1510 } else {
1511 if (WARN_ON(connkeys))
1512 return -EINVAL;
1513
1514 /* connect can point to wdev->wext.connect which
1515 * can hold key data from a previous connection
1516 */
1517 connect->key = NULL;
1518 connect->key_len = 0;
1519 connect->key_idx = 0;
1520 }
1521
1522 wdev->connect_keys = connkeys;
1523 memcpy(wdev->u.client.ssid, connect->ssid, connect->ssid_len);
1524 wdev->u.client.ssid_len = connect->ssid_len;
1525
1526 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1527 IEEE80211_BSS_TYPE_ESS;
1528
1529 if (!rdev->ops->connect)
1530 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1531 else
1532 err = rdev_connect(rdev, dev, connect);
1533
1534 if (err) {
1535 wdev->connect_keys = NULL;
1536 /*
1537 * This could be reassoc getting refused, don't clear
1538 * ssid_len in that case.
1539 */
1540 if (!wdev->connected)
1541 wdev->u.client.ssid_len = 0;
1542 return err;
1543 }
1544
1545 return 0;
1546 }
1547
cfg80211_disconnect(struct cfg80211_registered_device * rdev,struct net_device * dev,u16 reason,bool wextev)1548 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1549 struct net_device *dev, u16 reason, bool wextev)
1550 {
1551 struct wireless_dev *wdev = dev->ieee80211_ptr;
1552 int err = 0;
1553
1554 ASSERT_WDEV_LOCK(wdev);
1555
1556 kfree_sensitive(wdev->connect_keys);
1557 wdev->connect_keys = NULL;
1558
1559 wdev->conn_owner_nlportid = 0;
1560
1561 if (wdev->conn)
1562 err = cfg80211_sme_disconnect(wdev, reason);
1563 else if (!rdev->ops->disconnect)
1564 cfg80211_mlme_down(rdev, dev);
1565 else if (wdev->u.client.ssid_len)
1566 err = rdev_disconnect(rdev, dev, reason);
1567
1568 /*
1569 * Clear ssid_len unless we actually were fully connected,
1570 * in which case cfg80211_disconnected() will take care of
1571 * this later.
1572 */
1573 if (!wdev->connected)
1574 wdev->u.client.ssid_len = 0;
1575
1576 return err;
1577 }
1578
1579 /*
1580 * Used to clean up after the connection / connection attempt owner socket
1581 * disconnects
1582 */
cfg80211_autodisconnect_wk(struct work_struct * work)1583 void cfg80211_autodisconnect_wk(struct work_struct *work)
1584 {
1585 struct wireless_dev *wdev =
1586 container_of(work, struct wireless_dev, disconnect_wk);
1587 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1588
1589 wiphy_lock(wdev->wiphy);
1590 wdev_lock(wdev);
1591
1592 if (wdev->conn_owner_nlportid) {
1593 switch (wdev->iftype) {
1594 case NL80211_IFTYPE_ADHOC:
1595 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
1596 break;
1597 case NL80211_IFTYPE_AP:
1598 case NL80211_IFTYPE_P2P_GO:
1599 __cfg80211_stop_ap(rdev, wdev->netdev, -1, false);
1600 break;
1601 case NL80211_IFTYPE_MESH_POINT:
1602 __cfg80211_leave_mesh(rdev, wdev->netdev);
1603 break;
1604 case NL80211_IFTYPE_STATION:
1605 case NL80211_IFTYPE_P2P_CLIENT:
1606 /*
1607 * Use disconnect_bssid if still connecting and
1608 * ops->disconnect not implemented. Otherwise we can
1609 * use cfg80211_disconnect.
1610 */
1611 if (rdev->ops->disconnect || wdev->connected)
1612 cfg80211_disconnect(rdev, wdev->netdev,
1613 WLAN_REASON_DEAUTH_LEAVING,
1614 true);
1615 else
1616 cfg80211_mlme_deauth(rdev, wdev->netdev,
1617 wdev->disconnect_bssid,
1618 NULL, 0,
1619 WLAN_REASON_DEAUTH_LEAVING,
1620 false);
1621 break;
1622 default:
1623 break;
1624 }
1625 }
1626
1627 wdev_unlock(wdev);
1628 wiphy_unlock(wdev->wiphy);
1629 }
1630