blob: 0d75d3587896074f65d2377db1ed790c1bd95cad [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * SME code for cfg80211's connect emulation.
3 *
4 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright (C) 2009 Intel Corporation. All rights reserved.
6 */
7
8#include <linux/etherdevice.h>
9#include <linux/if_arp.h>
10#include <linux/slab.h>
11#include <linux/workqueue.h>
12#include <linux/wireless.h>
13#include <linux/export.h>
14#include <net/iw_handler.h>
15#include <net/cfg80211.h>
16#include <net/rtnetlink.h>
17#include "nl80211.h"
18#include "reg.h"
19
20struct cfg80211_conn {
21 struct cfg80211_connect_params params;
22 /* these are sub-states of the _CONNECTING sme_state */
23 enum {
24 CFG80211_CONN_IDLE,
25 CFG80211_CONN_SCANNING,
26 CFG80211_CONN_SCAN_AGAIN,
27 CFG80211_CONN_AUTHENTICATE_NEXT,
28 CFG80211_CONN_AUTHENTICATING,
29 CFG80211_CONN_ASSOCIATE_NEXT,
30 CFG80211_CONN_ASSOCIATING,
31 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
32 } state;
33 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
34 u8 *ie;
35 size_t ie_len;
36 bool auto_auth, prev_bssid_valid;
37};
38
39static bool cfg80211_is_all_idle(void)
40{
41 struct cfg80211_registered_device *rdev;
42 struct wireless_dev *wdev;
43 bool is_all_idle = true;
44
45 mutex_lock(&cfg80211_mutex);
46
47 /*
48 * All devices must be idle as otherwise if you are actively
49 * scanning some new beacon hints could be learned and would
50 * count as new regulatory hints.
51 */
52 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
53 cfg80211_lock_rdev(rdev);
54 list_for_each_entry(wdev, &rdev->netdev_list, list) {
55 wdev_lock(wdev);
56 if (wdev->sme_state != CFG80211_SME_IDLE)
57 is_all_idle = false;
58 wdev_unlock(wdev);
59 }
60 cfg80211_unlock_rdev(rdev);
61 }
62
63 mutex_unlock(&cfg80211_mutex);
64
65 return is_all_idle;
66}
67
68static void disconnect_work(struct work_struct *work)
69{
70 if (!cfg80211_is_all_idle())
71 return;
72
73 regulatory_hint_disconnect();
74}
75
76static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
77
78static int cfg80211_conn_scan(struct wireless_dev *wdev)
79{
80 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
81 struct cfg80211_scan_request *request;
82 int n_channels, err;
83
84 ASSERT_RTNL();
85 ASSERT_RDEV_LOCK(rdev);
86 ASSERT_WDEV_LOCK(wdev);
87
88 if (rdev->scan_req)
89 return -EBUSY;
90
91 if (wdev->conn->params.channel) {
92 n_channels = 1;
93 } else {
94 enum ieee80211_band band;
95 n_channels = 0;
96
97 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
98 if (!wdev->wiphy->bands[band])
99 continue;
100 n_channels += wdev->wiphy->bands[band]->n_channels;
101 }
102 }
103 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
104 sizeof(request->channels[0]) * n_channels,
105 GFP_KERNEL);
106 if (!request)
107 return -ENOMEM;
108
109 if (wdev->conn->params.channel)
110 request->channels[0] = wdev->conn->params.channel;
111 else {
112 int i = 0, j;
113 enum ieee80211_band band;
114 struct ieee80211_supported_band *bands;
115 struct ieee80211_channel *channel;
116
117 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
118 bands = wdev->wiphy->bands[band];
119 if (!bands)
120 continue;
121 for (j = 0; j < bands->n_channels; j++) {
122 channel = &bands->channels[j];
123 if (channel->flags & IEEE80211_CHAN_DISABLED)
124 continue;
125 request->channels[i++] = channel;
126 }
127 request->rates[band] = (1 << bands->n_bitrates) - 1;
128 }
129 n_channels = i;
130 }
131 request->n_channels = n_channels;
132 request->ssids = (void *)&request->channels[n_channels];
133 request->n_ssids = 1;
134
135 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
136 wdev->conn->params.ssid_len);
137 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
138
139 request->dev = wdev->netdev;
140 request->wiphy = &rdev->wiphy;
141
142 rdev->scan_req = request;
143
144 err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
145 if (!err) {
146 wdev->conn->state = CFG80211_CONN_SCANNING;
147 nl80211_send_scan_start(rdev, wdev->netdev);
148 dev_hold(wdev->netdev);
149 } else {
150 rdev->scan_req = NULL;
151 kfree(request);
152 }
153 return err;
154}
155
156static int cfg80211_conn_do_work(struct wireless_dev *wdev)
157{
158 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
159 struct cfg80211_connect_params *params;
160 const u8 *prev_bssid = NULL;
161 int err;
162
163 ASSERT_WDEV_LOCK(wdev);
164
165 if (!wdev->conn)
166 return 0;
167
168 params = &wdev->conn->params;
169
170 switch (wdev->conn->state) {
171 case CFG80211_CONN_SCAN_AGAIN:
172 return cfg80211_conn_scan(wdev);
173 case CFG80211_CONN_AUTHENTICATE_NEXT:
174 BUG_ON(!rdev->ops->auth);
175 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
176 return __cfg80211_mlme_auth(rdev, wdev->netdev,
177 params->channel, params->auth_type,
178 params->bssid,
179 params->ssid, params->ssid_len,
180 NULL, 0,
181 params->key, params->key_len,
182 params->key_idx);
183 case CFG80211_CONN_ASSOCIATE_NEXT:
184 BUG_ON(!rdev->ops->assoc);
185 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
186 if (wdev->conn->prev_bssid_valid)
187 prev_bssid = wdev->conn->prev_bssid;
188 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
189 params->channel, params->bssid,
190 prev_bssid,
191 params->ssid, params->ssid_len,
192 params->ie, params->ie_len,
193 false, &params->crypto,
194 params->flags, &params->ht_capa,
195 &params->ht_capa_mask);
196 if (err)
197 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
198 NULL, 0,
199 WLAN_REASON_DEAUTH_LEAVING,
200 false);
201 return err;
202 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
203 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
204 NULL, 0,
205 WLAN_REASON_DEAUTH_LEAVING, false);
206 /* return an error so that we call __cfg80211_connect_result() */
207 return -EINVAL;
208 default:
209 return 0;
210 }
211}
212
213void cfg80211_conn_work(struct work_struct *work)
214{
215 struct cfg80211_registered_device *rdev =
216 container_of(work, struct cfg80211_registered_device, conn_work);
217 struct wireless_dev *wdev;
218 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
219
220 rtnl_lock();
221 cfg80211_lock_rdev(rdev);
222 mutex_lock(&rdev->devlist_mtx);
223
224 list_for_each_entry(wdev, &rdev->netdev_list, list) {
225 if (!wdev->netdev)
226 continue;
227
228 wdev_lock(wdev);
229 if (!netif_running(wdev->netdev)) {
230 wdev_unlock(wdev);
231 continue;
232 }
233 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
234 wdev_unlock(wdev);
235 continue;
236 }
237 if (wdev->conn->params.bssid) {
238 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
239 bssid = bssid_buf;
240 }
241 if (cfg80211_conn_do_work(wdev))
242 __cfg80211_connect_result(
243 wdev->netdev, bssid,
244 NULL, 0, NULL, 0,
245 WLAN_STATUS_UNSPECIFIED_FAILURE,
246 false, NULL);
247 wdev_unlock(wdev);
248 }
249
250 mutex_unlock(&rdev->devlist_mtx);
251 cfg80211_unlock_rdev(rdev);
252 rtnl_unlock();
253}
254
255static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
256{
257 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
258 struct cfg80211_bss *bss;
259 u16 capa = WLAN_CAPABILITY_ESS;
260
261 ASSERT_WDEV_LOCK(wdev);
262
263 if (wdev->conn->params.privacy)
264 capa |= WLAN_CAPABILITY_PRIVACY;
265
266 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
267 wdev->conn->params.bssid,
268 wdev->conn->params.ssid,
269 wdev->conn->params.ssid_len,
270 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
271 capa);
272 if (!bss)
273 return NULL;
274
275 memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
276 wdev->conn->params.bssid = wdev->conn->bssid;
277 wdev->conn->params.channel = bss->channel;
278 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
279 schedule_work(&rdev->conn_work);
280
281 return bss;
282}
283
284static void __cfg80211_sme_scan_done(struct net_device *dev)
285{
286 struct wireless_dev *wdev = dev->ieee80211_ptr;
287 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
288 struct cfg80211_bss *bss;
289
290 ASSERT_WDEV_LOCK(wdev);
291
292 if (wdev->sme_state != CFG80211_SME_CONNECTING)
293 return;
294
295 if (!wdev->conn)
296 return;
297
298 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
299 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
300 return;
301
302 bss = cfg80211_get_conn_bss(wdev);
303 if (bss) {
304 cfg80211_put_bss(bss);
305 } else {
306 /* not found */
307 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
308 schedule_work(&rdev->conn_work);
309 else
310 __cfg80211_connect_result(
311 wdev->netdev,
312 wdev->conn->params.bssid,
313 NULL, 0, NULL, 0,
314 WLAN_STATUS_UNSPECIFIED_FAILURE,
315 false, NULL);
316 }
317}
318
319void cfg80211_sme_scan_done(struct net_device *dev)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322
323 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
324 wdev_lock(wdev);
325 __cfg80211_sme_scan_done(dev);
326 wdev_unlock(wdev);
327 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
328}
329
330void cfg80211_sme_rx_auth(struct net_device *dev,
331 const u8 *buf, size_t len)
332{
333 struct wireless_dev *wdev = dev->ieee80211_ptr;
334 struct wiphy *wiphy = wdev->wiphy;
335 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
336 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
337 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
338
339 ASSERT_WDEV_LOCK(wdev);
340
341 /* should only RX auth frames when connecting */
342 if (wdev->sme_state != CFG80211_SME_CONNECTING)
343 return;
344
345 if (WARN_ON(!wdev->conn))
346 return;
347
348 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
349 wdev->conn->auto_auth &&
350 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
351 /* select automatically between only open, shared, leap */
352 switch (wdev->conn->params.auth_type) {
353 case NL80211_AUTHTYPE_OPEN_SYSTEM:
354 if (wdev->connect_keys)
355 wdev->conn->params.auth_type =
356 NL80211_AUTHTYPE_SHARED_KEY;
357 else
358 wdev->conn->params.auth_type =
359 NL80211_AUTHTYPE_NETWORK_EAP;
360 break;
361 case NL80211_AUTHTYPE_SHARED_KEY:
362 wdev->conn->params.auth_type =
363 NL80211_AUTHTYPE_NETWORK_EAP;
364 break;
365 default:
366 /* huh? */
367 wdev->conn->params.auth_type =
368 NL80211_AUTHTYPE_OPEN_SYSTEM;
369 break;
370 }
371 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
372 schedule_work(&rdev->conn_work);
373 } else if (status_code != WLAN_STATUS_SUCCESS) {
374 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
375 status_code, false, NULL);
376 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
377 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
378 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
379 schedule_work(&rdev->conn_work);
380 }
381}
382
383bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
384{
385 struct wiphy *wiphy = wdev->wiphy;
386 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
387
388 if (WARN_ON(!wdev->conn))
389 return false;
390
391 if (!wdev->conn->prev_bssid_valid)
392 return false;
393
394 /*
395 * Some stupid APs don't accept reassoc, so we
396 * need to fall back to trying regular assoc.
397 */
398 wdev->conn->prev_bssid_valid = false;
399 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
400 schedule_work(&rdev->conn_work);
401
402 return true;
403}
404
405void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
406{
407 struct wiphy *wiphy = wdev->wiphy;
408 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
409
410 wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
411 schedule_work(&rdev->conn_work);
412}
413
414void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
415 const u8 *req_ie, size_t req_ie_len,
416 const u8 *resp_ie, size_t resp_ie_len,
417 u16 status, bool wextev,
418 struct cfg80211_bss *bss)
419{
420 struct wireless_dev *wdev = dev->ieee80211_ptr;
421 u8 *country_ie;
422#ifdef CONFIG_CFG80211_WEXT
423 union iwreq_data wrqu;
424#endif
425
426 ASSERT_WDEV_LOCK(wdev);
427
428 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
429 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
430 return;
431
432 if (wdev->sme_state != CFG80211_SME_CONNECTING)
433 return;
434
435 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
436 bssid, req_ie, req_ie_len,
437 resp_ie, resp_ie_len,
438 status, GFP_KERNEL);
439
440#ifdef CONFIG_CFG80211_WEXT
441 if (wextev) {
442 if (req_ie && status == WLAN_STATUS_SUCCESS) {
443 memset(&wrqu, 0, sizeof(wrqu));
444 wrqu.data.length = req_ie_len;
445 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
446 }
447
448 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
449 memset(&wrqu, 0, sizeof(wrqu));
450 wrqu.data.length = resp_ie_len;
451 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
452 }
453
454 memset(&wrqu, 0, sizeof(wrqu));
455 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
456 if (bssid && status == WLAN_STATUS_SUCCESS) {
457 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
458 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
459 wdev->wext.prev_bssid_valid = true;
460 }
461 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
462 }
463#endif
464
465 if (wdev->current_bss) {
466 cfg80211_unhold_bss(wdev->current_bss);
467 cfg80211_put_bss(&wdev->current_bss->pub);
468 wdev->current_bss = NULL;
469 }
470
471 if (wdev->conn)
472 wdev->conn->state = CFG80211_CONN_IDLE;
473
474 if (status != WLAN_STATUS_SUCCESS) {
475 wdev->sme_state = CFG80211_SME_IDLE;
476 if (wdev->conn)
477 kfree(wdev->conn->ie);
478 kfree(wdev->conn);
479 wdev->conn = NULL;
480 kfree(wdev->connect_keys);
481 wdev->connect_keys = NULL;
482 wdev->ssid_len = 0;
483 cfg80211_put_bss(bss);
484 return;
485 }
486
487 if (!bss)
488 bss = cfg80211_get_bss(wdev->wiphy,
489 wdev->conn ? wdev->conn->params.channel :
490 NULL,
491 bssid,
492 wdev->ssid, wdev->ssid_len,
493 WLAN_CAPABILITY_ESS,
494 WLAN_CAPABILITY_ESS);
495
496 if (WARN_ON(!bss))
497 return;
498
499 cfg80211_hold_bss(bss_from_pub(bss));
500 wdev->current_bss = bss_from_pub(bss);
501
502 wdev->sme_state = CFG80211_SME_CONNECTED;
503 cfg80211_upload_connect_keys(wdev);
504
505 country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
506
507 if (!country_ie)
508 return;
509
510 /*
511 * ieee80211_bss_get_ie() ensures we can access:
512 * - country_ie + 2, the start of the country ie data, and
513 * - and country_ie[1] which is the IE length
514 */
515 regulatory_hint_11d(wdev->wiphy,
516 bss->channel->band,
517 country_ie + 2,
518 country_ie[1]);
519}
520
521void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
522 const u8 *req_ie, size_t req_ie_len,
523 const u8 *resp_ie, size_t resp_ie_len,
524 u16 status, gfp_t gfp)
525{
526 struct wireless_dev *wdev = dev->ieee80211_ptr;
527 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
528 struct cfg80211_event *ev;
529 unsigned long flags;
530
531 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
532
533 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
534 if (!ev)
535 return;
536
537 ev->type = EVENT_CONNECT_RESULT;
538 if (bssid)
539 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
540 if (req_ie_len) {
541 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
542 ev->cr.req_ie_len = req_ie_len;
543 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
544 }
545 if (resp_ie_len) {
546 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
547 ev->cr.resp_ie_len = resp_ie_len;
548 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
549 }
550 ev->cr.status = status;
551
552 spin_lock_irqsave(&wdev->event_lock, flags);
553 list_add_tail(&ev->list, &wdev->event_list);
554 spin_unlock_irqrestore(&wdev->event_lock, flags);
555 queue_work(cfg80211_wq, &rdev->event_work);
556}
557EXPORT_SYMBOL(cfg80211_connect_result);
558
559void __cfg80211_roamed(struct wireless_dev *wdev,
560 struct cfg80211_bss *bss,
561 const u8 *req_ie, size_t req_ie_len,
562 const u8 *resp_ie, size_t resp_ie_len)
563{
564#ifdef CONFIG_CFG80211_WEXT
565 union iwreq_data wrqu;
566#endif
567 ASSERT_WDEV_LOCK(wdev);
568
569 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
570 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
571 goto out;
572
573 if (wdev->sme_state != CFG80211_SME_CONNECTED)
574 goto out;
575
576 /* internal error -- how did we get to CONNECTED w/o BSS? */
577 if (WARN_ON(!wdev->current_bss)) {
578 goto out;
579 }
580
581 cfg80211_unhold_bss(wdev->current_bss);
582 cfg80211_put_bss(&wdev->current_bss->pub);
583 wdev->current_bss = NULL;
584
585 cfg80211_hold_bss(bss_from_pub(bss));
586 wdev->current_bss = bss_from_pub(bss);
587
588 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
589 req_ie, req_ie_len, resp_ie, resp_ie_len,
590 GFP_KERNEL);
591
592#ifdef CONFIG_CFG80211_WEXT
593 if (req_ie) {
594 memset(&wrqu, 0, sizeof(wrqu));
595 wrqu.data.length = req_ie_len;
596 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
597 &wrqu, req_ie);
598 }
599
600 if (resp_ie) {
601 memset(&wrqu, 0, sizeof(wrqu));
602 wrqu.data.length = resp_ie_len;
603 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
604 &wrqu, resp_ie);
605 }
606
607 memset(&wrqu, 0, sizeof(wrqu));
608 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
609 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
610 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
611 wdev->wext.prev_bssid_valid = true;
612 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
613#endif
614
615 return;
616out:
617 cfg80211_put_bss(bss);
618}
619
620void cfg80211_roamed(struct net_device *dev,
621 struct ieee80211_channel *channel,
622 const u8 *bssid,
623 const u8 *req_ie, size_t req_ie_len,
624 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
625{
626 struct wireless_dev *wdev = dev->ieee80211_ptr;
627 struct cfg80211_bss *bss;
628
629 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
630
631 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
632 wdev->ssid_len, WLAN_CAPABILITY_ESS,
633 WLAN_CAPABILITY_ESS);
634 if (WARN_ON(!bss))
635 return;
636
637 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
638 resp_ie_len, gfp);
639}
640EXPORT_SYMBOL(cfg80211_roamed);
641
642void cfg80211_roamed_bss(struct net_device *dev,
643 struct cfg80211_bss *bss, const u8 *req_ie,
644 size_t req_ie_len, const u8 *resp_ie,
645 size_t resp_ie_len, gfp_t gfp)
646{
647 struct wireless_dev *wdev = dev->ieee80211_ptr;
648 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
649 struct cfg80211_event *ev;
650 unsigned long flags;
651
652 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
653
654 if (WARN_ON(!bss))
655 return;
656
657 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
658 if (!ev) {
659 cfg80211_put_bss(bss);
660 return;
661 }
662
663 ev->type = EVENT_ROAMED;
664 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
665 ev->rm.req_ie_len = req_ie_len;
666 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
667 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
668 ev->rm.resp_ie_len = resp_ie_len;
669 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
670 ev->rm.bss = bss;
671
672 spin_lock_irqsave(&wdev->event_lock, flags);
673 list_add_tail(&ev->list, &wdev->event_list);
674 spin_unlock_irqrestore(&wdev->event_lock, flags);
675 queue_work(cfg80211_wq, &rdev->event_work);
676}
677EXPORT_SYMBOL(cfg80211_roamed_bss);
678
679void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
680 size_t ie_len, u16 reason, bool from_ap)
681{
682 struct wireless_dev *wdev = dev->ieee80211_ptr;
683 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
684 int i;
685#ifdef CONFIG_CFG80211_WEXT
686 union iwreq_data wrqu;
687#endif
688
689 ASSERT_WDEV_LOCK(wdev);
690
691 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
692 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
693 return;
694
695#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
696 if (wdev->sme_state != CFG80211_SME_CONNECTED)
697 return;
698#endif
699
700 if (wdev->current_bss) {
701 cfg80211_unhold_bss(wdev->current_bss);
702 cfg80211_put_bss(&wdev->current_bss->pub);
703 }
704
705 wdev->current_bss = NULL;
706 wdev->sme_state = CFG80211_SME_IDLE;
707 wdev->ssid_len = 0;
708
709 if (wdev->conn) {
710 kfree(wdev->conn->ie);
711 wdev->conn->ie = NULL;
712 kfree(wdev->conn);
713 wdev->conn = NULL;
714 }
715
716 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
717
718 /*
719 * Delete all the keys ... pairwise keys can't really
720 * exist any more anyway, but default keys might.
721 */
722 if (rdev->ops->del_key)
723 for (i = 0; i < 6; i++)
724 rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
725
726#ifdef CONFIG_CFG80211_WEXT
727 memset(&wrqu, 0, sizeof(wrqu));
728 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
729 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
730 wdev->wext.connect.ssid_len = 0;
731#endif
732
733 schedule_work(&cfg80211_disconnect_work);
734}
735
736void cfg80211_disconnected(struct net_device *dev, u16 reason,
737 u8 *ie, size_t ie_len, gfp_t gfp)
738{
739 struct wireless_dev *wdev = dev->ieee80211_ptr;
740 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
741 struct cfg80211_event *ev;
742 unsigned long flags;
743
744 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
745
746 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
747 if (!ev)
748 return;
749
750 ev->type = EVENT_DISCONNECTED;
751 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
752 ev->dc.ie_len = ie_len;
753 memcpy((void *)ev->dc.ie, ie, ie_len);
754 ev->dc.reason = reason;
755
756 spin_lock_irqsave(&wdev->event_lock, flags);
757 list_add_tail(&ev->list, &wdev->event_list);
758 spin_unlock_irqrestore(&wdev->event_lock, flags);
759 queue_work(cfg80211_wq, &rdev->event_work);
760}
761EXPORT_SYMBOL(cfg80211_disconnected);
762
763int __cfg80211_connect(struct cfg80211_registered_device *rdev,
764 struct net_device *dev,
765 struct cfg80211_connect_params *connect,
766 struct cfg80211_cached_keys *connkeys,
767 const u8 *prev_bssid)
768{
769 struct wireless_dev *wdev = dev->ieee80211_ptr;
770 struct cfg80211_bss *bss = NULL;
771 int err;
772
773 ASSERT_WDEV_LOCK(wdev);
774
775#ifndef CONFIG_CFG80211_ALLOW_RECONNECT
776 if (wdev->sme_state != CFG80211_SME_IDLE)
777 return -EALREADY;
778
779 if (WARN_ON(wdev->connect_keys)) {
780#else
781 if (wdev->connect_keys) {
782#endif
783 kfree(wdev->connect_keys);
784 wdev->connect_keys = NULL;
785 }
786
787 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
788 rdev->wiphy.ht_capa_mod_mask);
789
790 if (connkeys && connkeys->def >= 0) {
791 int idx;
792 u32 cipher;
793
794 idx = connkeys->def;
795 cipher = connkeys->params[idx].cipher;
796 /* If given a WEP key we may need it for shared key auth */
797 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
798 cipher == WLAN_CIPHER_SUITE_WEP104) {
799 connect->key_idx = idx;
800 connect->key = connkeys->params[idx].key;
801 connect->key_len = connkeys->params[idx].key_len;
802
803 /*
804 * If ciphers are not set (e.g. when going through
805 * iwconfig), we have to set them appropriately here.
806 */
807 if (connect->crypto.cipher_group == 0)
808 connect->crypto.cipher_group = cipher;
809
810 if (connect->crypto.n_ciphers_pairwise == 0) {
811 connect->crypto.n_ciphers_pairwise = 1;
812 connect->crypto.ciphers_pairwise[0] = cipher;
813 }
814 }
815 }
816
817 if (!rdev->ops->connect) {
818 if (!rdev->ops->auth || !rdev->ops->assoc)
819 return -EOPNOTSUPP;
820
821 if (WARN_ON(wdev->conn))
822 return -EINPROGRESS;
823
824 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
825 if (!wdev->conn)
826 return -ENOMEM;
827
828 /*
829 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
830 */
831 memcpy(&wdev->conn->params, connect, sizeof(*connect));
832 if (connect->bssid) {
833 wdev->conn->params.bssid = wdev->conn->bssid;
834 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
835 }
836
837 if (connect->ie) {
838 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
839 GFP_KERNEL);
840 wdev->conn->params.ie = wdev->conn->ie;
841 if (!wdev->conn->ie) {
842 kfree(wdev->conn);
843 wdev->conn = NULL;
844 return -ENOMEM;
845 }
846 }
847
848 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
849 wdev->conn->auto_auth = true;
850 /* start with open system ... should mostly work */
851 wdev->conn->params.auth_type =
852 NL80211_AUTHTYPE_OPEN_SYSTEM;
853 } else {
854 wdev->conn->auto_auth = false;
855 }
856
857 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
858 wdev->ssid_len = connect->ssid_len;
859 wdev->conn->params.ssid = wdev->ssid;
860 wdev->conn->params.ssid_len = connect->ssid_len;
861
862 /* see if we have the bss already */
863 bss = cfg80211_get_conn_bss(wdev);
864
865 wdev->sme_state = CFG80211_SME_CONNECTING;
866 wdev->connect_keys = connkeys;
867
868 if (prev_bssid) {
869 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
870 wdev->conn->prev_bssid_valid = true;
871 }
872
873 /* we're good if we have a matching bss struct */
874 if (bss) {
875 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
876 err = cfg80211_conn_do_work(wdev);
877 cfg80211_put_bss(bss);
878 } else {
879 /* otherwise we'll need to scan for the AP first */
880 err = cfg80211_conn_scan(wdev);
881 /*
882 * If we can't scan right now, then we need to scan again
883 * after the current scan finished, since the parameters
884 * changed (unless we find a good AP anyway).
885 */
886 if (err == -EBUSY) {
887 err = 0;
888 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
889 }
890 }
891 if (err) {
892 kfree(wdev->conn->ie);
893 kfree(wdev->conn);
894 wdev->conn = NULL;
895 wdev->sme_state = CFG80211_SME_IDLE;
896 wdev->connect_keys = NULL;
897 wdev->ssid_len = 0;
898 }
899
900 return err;
901 } else {
902 wdev->sme_state = CFG80211_SME_CONNECTING;
903 wdev->connect_keys = connkeys;
904 err = rdev->ops->connect(&rdev->wiphy, dev, connect);
905 if (err) {
906 wdev->connect_keys = NULL;
907 wdev->sme_state = CFG80211_SME_IDLE;
908 return err;
909 }
910
911 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
912 wdev->ssid_len = connect->ssid_len;
913
914 return 0;
915 }
916}
917
918int cfg80211_connect(struct cfg80211_registered_device *rdev,
919 struct net_device *dev,
920 struct cfg80211_connect_params *connect,
921 struct cfg80211_cached_keys *connkeys)
922{
923 int err;
924
925 mutex_lock(&rdev->devlist_mtx);
926 wdev_lock(dev->ieee80211_ptr);
927 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
928 wdev_unlock(dev->ieee80211_ptr);
929 mutex_unlock(&rdev->devlist_mtx);
930
931 return err;
932}
933
934int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
935 struct net_device *dev, u16 reason, bool wextev)
936{
937 struct wireless_dev *wdev = dev->ieee80211_ptr;
938 int err;
939
940 ASSERT_WDEV_LOCK(wdev);
941
942 if (wdev->sme_state == CFG80211_SME_IDLE)
943 return -EINVAL;
944
945 kfree(wdev->connect_keys);
946 wdev->connect_keys = NULL;
947
948 if (!rdev->ops->disconnect) {
949 if (!rdev->ops->deauth)
950 return -EOPNOTSUPP;
951
952 /* was it connected by userspace SME? */
953 if (!wdev->conn) {
954 cfg80211_mlme_down(rdev, dev);
955 return 0;
956 }
957
958 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
959 (wdev->conn->state == CFG80211_CONN_SCANNING ||
960 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
961 wdev->sme_state = CFG80211_SME_IDLE;
962 kfree(wdev->conn->ie);
963 kfree(wdev->conn);
964 wdev->conn = NULL;
965 wdev->ssid_len = 0;
966 return 0;
967 }
968
969 /* wdev->conn->params.bssid must be set if > SCANNING */
970 err = __cfg80211_mlme_deauth(rdev, dev,
971 wdev->conn->params.bssid,
972 NULL, 0, reason, false);
973 if (err)
974 return err;
975 } else {
976 err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
977 if (err)
978 return err;
979 }
980
981 if (wdev->sme_state == CFG80211_SME_CONNECTED)
982 __cfg80211_disconnected(dev, NULL, 0, 0, false);
983 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
984 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
985 WLAN_STATUS_UNSPECIFIED_FAILURE,
986 wextev, NULL);
987
988 return 0;
989}
990
991int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
992 struct net_device *dev,
993 u16 reason, bool wextev)
994{
995 int err;
996
997 wdev_lock(dev->ieee80211_ptr);
998 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
999 wdev_unlock(dev->ieee80211_ptr);
1000
1001 return err;
1002}
1003
1004void cfg80211_sme_disassoc(struct net_device *dev,
1005 struct cfg80211_internal_bss *bss)
1006{
1007 struct wireless_dev *wdev = dev->ieee80211_ptr;
1008 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1009 u8 bssid[ETH_ALEN];
1010
1011 ASSERT_WDEV_LOCK(wdev);
1012
1013 if (!wdev->conn)
1014 return;
1015
1016 if (wdev->conn->state == CFG80211_CONN_IDLE)
1017 return;
1018
1019 /*
1020 * Ok, so the association was made by this SME -- we don't
1021 * want it any more so deauthenticate too.
1022 */
1023
1024 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
1025
1026 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1027 WLAN_REASON_DEAUTH_LEAVING, false);
1028}