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