blob: f432bd3755b19f0d865b1b2e9f2a132890f3b6a3 [file] [log] [blame]
Samuel Ortizb23aa672009-07-01 21:26:54 +02001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020011#include <linux/workqueue.h>
Johannes Berga9a11622009-07-27 12:01:53 +020012#include <linux/wireless.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040013#include <linux/export.h>
Johannes Berga9a11622009-07-27 12:01:53 +020014#include <net/iw_handler.h>
Samuel Ortizb23aa672009-07-01 21:26:54 +020015#include <net/cfg80211.h>
16#include <net/rtnetlink.h>
17#include "nl80211.h"
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -070018#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030019#include "rdev-ops.h"
Samuel Ortizb23aa672009-07-01 21:26:54 +020020
Johannes Berg6829c872009-07-02 09:13:27 +020021struct cfg80211_conn {
22 struct cfg80211_connect_params params;
23 /* these are sub-states of the _CONNECTING sme_state */
24 enum {
25 CFG80211_CONN_IDLE,
26 CFG80211_CONN_SCANNING,
27 CFG80211_CONN_SCAN_AGAIN,
28 CFG80211_CONN_AUTHENTICATE_NEXT,
29 CFG80211_CONN_AUTHENTICATING,
30 CFG80211_CONN_ASSOCIATE_NEXT,
31 CFG80211_CONN_ASSOCIATING,
Johannes Berg7d930bc2009-10-20 15:08:53 +090032 CFG80211_CONN_DEAUTH_ASSOC_FAIL,
Johannes Berg6829c872009-07-02 09:13:27 +020033 } state;
Johannes Bergf401a6f2009-08-07 14:51:05 +020034 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
Johannes Berg6829c872009-07-02 09:13:27 +020035 u8 *ie;
36 size_t ie_len;
Johannes Bergf401a6f2009-08-07 14:51:05 +020037 bool auto_auth, prev_bssid_valid;
Johannes Berg6829c872009-07-02 09:13:27 +020038};
39
John W. Linville20925fe2010-07-20 12:32:52 -040040static bool cfg80211_is_all_idle(void)
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050041{
42 struct cfg80211_registered_device *rdev;
43 struct wireless_dev *wdev;
44 bool is_all_idle = true;
45
46 mutex_lock(&cfg80211_mutex);
47
48 /*
49 * All devices must be idle as otherwise if you are actively
50 * scanning some new beacon hints could be learned and would
51 * count as new regulatory hints.
52 */
53 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
54 cfg80211_lock_rdev(rdev);
Johannes Berg89a54e42012-06-15 14:33:17 +020055 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -050056 wdev_lock(wdev);
57 if (wdev->sme_state != CFG80211_SME_IDLE)
58 is_all_idle = false;
59 wdev_unlock(wdev);
60 }
61 cfg80211_unlock_rdev(rdev);
62 }
63
64 mutex_unlock(&cfg80211_mutex);
65
66 return is_all_idle;
67}
68
69static void disconnect_work(struct work_struct *work)
70{
71 if (!cfg80211_is_all_idle())
72 return;
73
74 regulatory_hint_disconnect();
75}
76
77static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
Johannes Berg6829c872009-07-02 09:13:27 +020078
79static int cfg80211_conn_scan(struct wireless_dev *wdev)
80{
Johannes Berg79c97e92009-07-07 03:56:12 +020081 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +020082 struct cfg80211_scan_request *request;
83 int n_channels, err;
84
85 ASSERT_RTNL();
Johannes Berg79c97e92009-07-07 03:56:12 +020086 ASSERT_RDEV_LOCK(rdev);
Johannes Berg667503dd2009-07-07 03:56:11 +020087 ASSERT_WDEV_LOCK(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +020088
Johannes Berg79c97e92009-07-07 03:56:12 +020089 if (rdev->scan_req)
Johannes Berg6829c872009-07-02 09:13:27 +020090 return -EBUSY;
91
92 if (wdev->conn->params.channel) {
93 n_channels = 1;
94 } else {
95 enum ieee80211_band band;
96 n_channels = 0;
97
98 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
99 if (!wdev->wiphy->bands[band])
100 continue;
101 n_channels += wdev->wiphy->bands[band]->n_channels;
102 }
103 }
104 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
105 sizeof(request->channels[0]) * n_channels,
106 GFP_KERNEL);
107 if (!request)
108 return -ENOMEM;
109
Johannes Berg6829c872009-07-02 09:13:27 +0200110 if (wdev->conn->params.channel)
111 request->channels[0] = wdev->conn->params.channel;
112 else {
113 int i = 0, j;
114 enum ieee80211_band band;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530115 struct ieee80211_supported_band *bands;
116 struct ieee80211_channel *channel;
Johannes Berg6829c872009-07-02 09:13:27 +0200117
118 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530119 bands = wdev->wiphy->bands[band];
120 if (!bands)
Johannes Berg6829c872009-07-02 09:13:27 +0200121 continue;
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530122 for (j = 0; j < bands->n_channels; j++) {
123 channel = &bands->channels[j];
124 if (channel->flags & IEEE80211_CHAN_DISABLED)
125 continue;
126 request->channels[i++] = channel;
127 }
128 request->rates[band] = (1 << bands->n_bitrates) - 1;
Johannes Berg6829c872009-07-02 09:13:27 +0200129 }
Rajkumar Manoharane3081502011-09-15 17:40:50 +0530130 n_channels = i;
Johannes Berg6829c872009-07-02 09:13:27 +0200131 }
132 request->n_channels = n_channels;
Johannes Berg5ba63532009-08-07 17:54:07 +0200133 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg6829c872009-07-02 09:13:27 +0200134 request->n_ssids = 1;
135
136 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
137 wdev->conn->params.ssid_len);
138 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
139
Johannes Bergfd014282012-06-18 19:17:03 +0200140 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +0200141 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -0700142 request->scan_start = jiffies;
Johannes Berg6829c872009-07-02 09:13:27 +0200143
Johannes Berg79c97e92009-07-07 03:56:12 +0200144 rdev->scan_req = request;
Johannes Berg6829c872009-07-02 09:13:27 +0200145
Hila Gonene35e4d22012-06-27 17:19:42 +0300146 err = rdev_scan(rdev, request);
Johannes Berg6829c872009-07-02 09:13:27 +0200147 if (!err) {
148 wdev->conn->state = CFG80211_CONN_SCANNING;
Johannes Bergfd014282012-06-18 19:17:03 +0200149 nl80211_send_scan_start(rdev, wdev);
Johannes Berg463d0182009-07-14 00:33:35 +0200150 dev_hold(wdev->netdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200151 } else {
Johannes Berg79c97e92009-07-07 03:56:12 +0200152 rdev->scan_req = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200153 kfree(request);
154 }
155 return err;
156}
157
158static int cfg80211_conn_do_work(struct wireless_dev *wdev)
159{
Johannes Berg79c97e92009-07-07 03:56:12 +0200160 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg19957bb2009-07-02 17:20:43 +0200161 struct cfg80211_connect_params *params;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200162 const u8 *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200163 int err;
Johannes Berg6829c872009-07-02 09:13:27 +0200164
Johannes Berg667503dd2009-07-07 03:56:11 +0200165 ASSERT_WDEV_LOCK(wdev);
166
Johannes Berg6829c872009-07-02 09:13:27 +0200167 if (!wdev->conn)
168 return 0;
169
Johannes Berg19957bb2009-07-02 17:20:43 +0200170 params = &wdev->conn->params;
171
Johannes Berg6829c872009-07-02 09:13:27 +0200172 switch (wdev->conn->state) {
173 case CFG80211_CONN_SCAN_AGAIN:
174 return cfg80211_conn_scan(wdev);
175 case CFG80211_CONN_AUTHENTICATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200176 BUG_ON(!rdev->ops->auth);
Johannes Berg19957bb2009-07-02 17:20:43 +0200177 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
Johannes Berg79c97e92009-07-07 03:56:12 +0200178 return __cfg80211_mlme_auth(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200179 params->channel, params->auth_type,
180 params->bssid,
181 params->ssid, params->ssid_len,
Johannes Bergfffd0932009-07-08 14:22:54 +0200182 NULL, 0,
183 params->key, params->key_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +0300184 params->key_idx, NULL, 0);
Johannes Berg6829c872009-07-02 09:13:27 +0200185 case CFG80211_CONN_ASSOCIATE_NEXT:
Johannes Berg79c97e92009-07-07 03:56:12 +0200186 BUG_ON(!rdev->ops->assoc);
Johannes Berg19957bb2009-07-02 17:20:43 +0200187 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200188 if (wdev->conn->prev_bssid_valid)
189 prev_bssid = wdev->conn->prev_bssid;
Johannes Berg79c97e92009-07-07 03:56:12 +0200190 err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
Johannes Berg667503dd2009-07-07 03:56:11 +0200191 params->channel, params->bssid,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200192 prev_bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200193 params->ssid, params->ssid_len,
194 params->ie, params->ie_len,
Jouni Malinencee00a92013-01-15 17:15:57 +0200195 params->mfp != NL80211_MFP_NO,
196 &params->crypto,
Ben Greear7e7c8922011-11-18 11:31:59 -0800197 params->flags, &params->ht_capa,
198 &params->ht_capa_mask);
Johannes Berg19957bb2009-07-02 17:20:43 +0200199 if (err)
Johannes Berg79c97e92009-07-07 03:56:12 +0200200 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200201 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300202 WLAN_REASON_DEAUTH_LEAVING,
203 false);
Johannes Berg19957bb2009-07-02 17:20:43 +0200204 return err;
Johannes Berg7d930bc2009-10-20 15:08:53 +0900205 case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
206 __cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
207 NULL, 0,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300208 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg7d930bc2009-10-20 15:08:53 +0900209 /* return an error so that we call __cfg80211_connect_result() */
210 return -EINVAL;
Johannes Berg6829c872009-07-02 09:13:27 +0200211 default:
212 return 0;
213 }
214}
215
216void cfg80211_conn_work(struct work_struct *work)
217{
Johannes Berg79c97e92009-07-07 03:56:12 +0200218 struct cfg80211_registered_device *rdev =
Johannes Berg6829c872009-07-02 09:13:27 +0200219 container_of(work, struct cfg80211_registered_device, conn_work);
220 struct wireless_dev *wdev;
Johannes Berg7400f422009-10-31 07:40:37 +0100221 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200222
223 rtnl_lock();
Johannes Berg79c97e92009-07-07 03:56:12 +0200224 cfg80211_lock_rdev(rdev);
225 mutex_lock(&rdev->devlist_mtx);
Johannes Berg6829c872009-07-02 09:13:27 +0200226
Johannes Berg89a54e42012-06-15 14:33:17 +0200227 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Berg667503dd2009-07-07 03:56:11 +0200228 wdev_lock(wdev);
229 if (!netif_running(wdev->netdev)) {
230 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200231 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200232 }
233 if (wdev->sme_state != CFG80211_SME_CONNECTING) {
234 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200235 continue;
Johannes Berg667503dd2009-07-07 03:56:11 +0200236 }
Johannes Berg7400f422009-10-31 07:40:37 +0100237 if (wdev->conn->params.bssid) {
238 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
239 bssid = bssid_buf;
240 }
Johannes Berg6829c872009-07-02 09:13:27 +0200241 if (cfg80211_conn_do_work(wdev))
Johannes Berg667503dd2009-07-07 03:56:11 +0200242 __cfg80211_connect_result(
Johannes Berg7d930bc2009-10-20 15:08:53 +0900243 wdev->netdev, bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200244 NULL, 0, NULL, 0,
245 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200246 false, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200247 wdev_unlock(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200248 }
249
Johannes Berg79c97e92009-07-07 03:56:12 +0200250 mutex_unlock(&rdev->devlist_mtx);
251 cfg80211_unlock_rdev(rdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200252 rtnl_unlock();
253}
254
Johannes Bergbbac31f2009-09-16 09:04:26 -0700255static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
Johannes Berg6829c872009-07-02 09:13:27 +0200256{
Johannes Berg79c97e92009-07-07 03:56:12 +0200257 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Berg6829c872009-07-02 09:13:27 +0200258 struct cfg80211_bss *bss;
259 u16 capa = WLAN_CAPABILITY_ESS;
260
Johannes Berg667503dd2009-07-07 03:56:11 +0200261 ASSERT_WDEV_LOCK(wdev);
262
Johannes Berg6829c872009-07-02 09:13:27 +0200263 if (wdev->conn->params.privacy)
264 capa |= WLAN_CAPABILITY_PRIVACY;
265
Jouni Malinened9d0102011-05-16 19:40:15 +0300266 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
267 wdev->conn->params.bssid,
Johannes Berg6829c872009-07-02 09:13:27 +0200268 wdev->conn->params.ssid,
269 wdev->conn->params.ssid_len,
270 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
271 capa);
Johannes Berg6829c872009-07-02 09:13:27 +0200272 if (!bss)
Johannes Bergbbac31f2009-09-16 09:04:26 -0700273 return NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200274
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;
Johannes Berg79c97e92009-07-07 03:56:12 +0200279 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200280
Johannes Bergbbac31f2009-09-16 09:04:26 -0700281 return bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200282}
283
Johannes Berg667503dd2009-07-07 03:56:11 +0200284static void __cfg80211_sme_scan_done(struct net_device *dev)
Johannes Berg6829c872009-07-02 09:13:27 +0200285{
286 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg79c97e92009-07-07 03:56:12 +0200287 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700288 struct cfg80211_bss *bss;
Johannes Berg6829c872009-07-02 09:13:27 +0200289
Johannes Berg667503dd2009-07-07 03:56:11 +0200290 ASSERT_WDEV_LOCK(wdev);
291
Johannes Berg6829c872009-07-02 09:13:27 +0200292 if (wdev->sme_state != CFG80211_SME_CONNECTING)
293 return;
294
Zhu Yid4b1a682009-07-16 17:34:14 +0800295 if (!wdev->conn)
Johannes Berg6829c872009-07-02 09:13:27 +0200296 return;
297
298 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
299 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
300 return;
301
Johannes Bergbbac31f2009-09-16 09:04:26 -0700302 bss = cfg80211_get_conn_bss(wdev);
303 if (bss) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100304 cfg80211_put_bss(&rdev->wiphy, bss);
Johannes Bergbbac31f2009-09-16 09:04:26 -0700305 } else {
Johannes Berg6829c872009-07-02 09:13:27 +0200306 /* not found */
307 if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
Johannes Berg79c97e92009-07-07 03:56:12 +0200308 schedule_work(&rdev->conn_work);
Johannes Berg6829c872009-07-02 09:13:27 +0200309 else
Johannes Berg667503dd2009-07-07 03:56:11 +0200310 __cfg80211_connect_result(
311 wdev->netdev,
312 wdev->conn->params.bssid,
313 NULL, 0, NULL, 0,
314 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200315 false, NULL);
Johannes Berg6829c872009-07-02 09:13:27 +0200316 }
317}
318
Johannes Berg667503dd2009-07-07 03:56:11 +0200319void cfg80211_sme_scan_done(struct net_device *dev)
320{
321 struct wireless_dev *wdev = dev->ieee80211_ptr;
322
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200323 mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200324 wdev_lock(wdev);
325 __cfg80211_sme_scan_done(dev);
326 wdev_unlock(wdev);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200327 mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200328}
329
330void cfg80211_sme_rx_auth(struct net_device *dev,
331 const u8 *buf, size_t len)
Johannes Berg6829c872009-07-02 09:13:27 +0200332{
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
Johannes Berg667503dd2009-07-07 03:56:11 +0200339 ASSERT_WDEV_LOCK(wdev);
340
Johannes Berg6829c872009-07-02 09:13:27 +0200341 /* 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:
Johannes Bergfffd0932009-07-08 14:22:54 +0200354 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;
Johannes Berg6829c872009-07-02 09:13:27 +0200360 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);
Johannes Berg19957bb2009-07-02 17:20:43 +0200373 } else if (status_code != WLAN_STATUS_SUCCESS) {
Johannes Berg4bde0f72009-07-07 23:46:51 +0200374 __cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200375 status_code, false, NULL);
Johannes Berg19957bb2009-07-02 17:20:43 +0200376 } else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
Johannes Berg6829c872009-07-02 09:13:27 +0200377 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
378 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
379 schedule_work(&rdev->conn_work);
380 }
381}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200382
Johannes Bergf401a6f2009-08-07 14:51:05 +0200383bool 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
Johannes Berg7d930bc2009-10-20 15:08:53 +0900405void 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
Johannes Berg667503dd2009-07-07 03:56:11 +0200414void __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,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200417 u16 status, bool wextev,
418 struct cfg80211_bss *bss)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200419{
420 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg9caf0362012-11-29 01:25:20 +0100421 const u8 *country_ie;
Johannes Berg3d23e342009-09-29 23:27:28 +0200422#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200423 union iwreq_data wrqu;
424#endif
425
Johannes Berg667503dd2009-07-07 03:56:11 +0200426 ASSERT_WDEV_LOCK(wdev);
427
Johannes Berg074ac8d2010-09-16 14:58:22 +0200428 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
429 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200430 return;
431
Johannes Bergf7969962009-08-21 12:23:49 +0200432 if (wdev->sme_state != CFG80211_SME_CONNECTING)
Johannes Bergea416a72009-08-17 12:22:14 +0200433 return;
434
435 nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
Johannes Berge45cd822009-07-02 09:58:04 +0200436 bssid, req_ie, req_ie_len,
Johannes Bergea416a72009-08-17 12:22:14 +0200437 resp_ie, resp_ie_len,
438 status, GFP_KERNEL);
Johannes Berge45cd822009-07-02 09:58:04 +0200439
Johannes Berg3d23e342009-09-29 23:27:28 +0200440#ifdef CONFIG_CFG80211_WEXT
Johannes Berge45cd822009-07-02 09:58:04 +0200441 if (wextev) {
442 if (req_ie && status == WLAN_STATUS_SUCCESS) {
443 memset(&wrqu, 0, sizeof(wrqu));
444 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800445 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
Johannes Berge45cd822009-07-02 09:58:04 +0200446 }
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;
Johannes Bergf401a6f2009-08-07 14:51:05 +0200456 if (bssid && status == WLAN_STATUS_SUCCESS) {
Johannes Berge45cd822009-07-02 09:58:04 +0200457 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200458 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
459 wdev->wext.prev_bssid_valid = true;
460 }
Johannes Berge45cd822009-07-02 09:58:04 +0200461 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
462 }
463#endif
464
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200465 if (wdev->current_bss) {
466 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100467 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200468 wdev->current_bss = NULL;
469 }
470
Johannes Berg19957bb2009-07-02 17:20:43 +0200471 if (wdev->conn)
472 wdev->conn->state = CFG80211_CONN_IDLE;
473
Johannes Bergfffd0932009-07-08 14:22:54 +0200474 if (status != WLAN_STATUS_SUCCESS) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200475 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100476 if (wdev->conn)
477 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200478 kfree(wdev->conn);
479 wdev->conn = NULL;
Johannes Bergfffd0932009-07-08 14:22:54 +0200480 kfree(wdev->connect_keys);
481 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200482 wdev->ssid_len = 0;
Johannes Berg5b112d32013-02-01 01:49:58 +0100483 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Bergfffd0932009-07-08 14:22:54 +0200484 return;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200485 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200486
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200487 if (!bss)
Jouni Malinened9d0102011-05-16 19:40:15 +0300488 bss = cfg80211_get_bss(wdev->wiphy,
489 wdev->conn ? wdev->conn->params.channel :
490 NULL,
491 bssid,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200492 wdev->ssid, wdev->ssid_len,
493 WLAN_CAPABILITY_ESS,
494 WLAN_CAPABILITY_ESS);
Johannes Bergfffd0932009-07-08 14:22:54 +0200495
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
Johannes Bergfffd0932009-07-08 14:22:54 +0200502 wdev->sme_state = CFG80211_SME_CONNECTED;
503 cfg80211_upload_connect_keys(wdev);
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700504
Johannes Berg9caf0362012-11-29 01:25:20 +0100505 rcu_read_lock();
506 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
507 if (!country_ie) {
508 rcu_read_unlock();
509 return;
510 }
511
512 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
513 rcu_read_unlock();
Luis R. Rodriguez8b19e6c2009-07-30 17:38:09 -0700514
515 if (!country_ie)
516 return;
517
518 /*
519 * ieee80211_bss_get_ie() ensures we can access:
520 * - country_ie + 2, the start of the country ie data, and
521 * - and country_ie[1] which is the IE length
522 */
Johannes Berg1a919312012-12-03 17:21:11 +0100523 regulatory_hint_11d(wdev->wiphy, bss->channel->band,
524 country_ie + 2, country_ie[1]);
Johannes Berg9caf0362012-11-29 01:25:20 +0100525 kfree(country_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200526}
Johannes Bergf2129352009-07-01 21:26:56 +0200527
528void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
529 const u8 *req_ie, size_t req_ie_len,
530 const u8 *resp_ie, size_t resp_ie_len,
531 u16 status, gfp_t gfp)
532{
Johannes Berg667503dd2009-07-07 03:56:11 +0200533 struct wireless_dev *wdev = dev->ieee80211_ptr;
534 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
535 struct cfg80211_event *ev;
536 unsigned long flags;
537
Johannes Bergf7969962009-08-21 12:23:49 +0200538 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
539
Johannes Berg667503dd2009-07-07 03:56:11 +0200540 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
541 if (!ev)
542 return;
543
544 ev->type = EVENT_CONNECT_RESULT;
Zhu Yi16a832e2009-08-19 16:08:22 +0800545 if (bssid)
546 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
Nishant Sarmukadam7834704b2010-04-14 22:03:02 -0700547 if (req_ie_len) {
548 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
549 ev->cr.req_ie_len = req_ie_len;
550 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
551 }
552 if (resp_ie_len) {
553 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
554 ev->cr.resp_ie_len = resp_ie_len;
555 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
556 }
Johannes Berg667503dd2009-07-07 03:56:11 +0200557 ev->cr.status = status;
558
559 spin_lock_irqsave(&wdev->event_lock, flags);
560 list_add_tail(&ev->list, &wdev->event_list);
561 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100562 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Bergf2129352009-07-01 21:26:56 +0200563}
Samuel Ortizb23aa672009-07-01 21:26:54 +0200564EXPORT_SYMBOL(cfg80211_connect_result);
565
Jouni Malinened9d0102011-05-16 19:40:15 +0300566void __cfg80211_roamed(struct wireless_dev *wdev,
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530567 struct cfg80211_bss *bss,
Johannes Berg667503dd2009-07-07 03:56:11 +0200568 const u8 *req_ie, size_t req_ie_len,
569 const u8 *resp_ie, size_t resp_ie_len)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200570{
Johannes Berg3d23e342009-09-29 23:27:28 +0200571#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200572 union iwreq_data wrqu;
573#endif
Johannes Berg667503dd2009-07-07 03:56:11 +0200574 ASSERT_WDEV_LOCK(wdev);
575
Johannes Berg074ac8d2010-09-16 14:58:22 +0200576 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
577 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530578 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200579
Johannes Bergf7969962009-08-21 12:23:49 +0200580 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530581 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200582
583 /* internal error -- how did we get to CONNECTED w/o BSS? */
584 if (WARN_ON(!wdev->current_bss)) {
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530585 goto out;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200586 }
587
588 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100589 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200590 wdev->current_bss = NULL;
591
Johannes Berg19957bb2009-07-02 17:20:43 +0200592 cfg80211_hold_bss(bss_from_pub(bss));
593 wdev->current_bss = bss_from_pub(bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200594
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530595 nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200596 req_ie, req_ie_len, resp_ie, resp_ie_len,
597 GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200598
Johannes Berg3d23e342009-09-29 23:27:28 +0200599#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200600 if (req_ie) {
601 memset(&wrqu, 0, sizeof(wrqu));
602 wrqu.data.length = req_ie_len;
Zhu Yi3409ff72009-07-20 11:47:44 +0800603 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
Johannes Berg667503dd2009-07-07 03:56:11 +0200604 &wrqu, req_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200605 }
606
607 if (resp_ie) {
608 memset(&wrqu, 0, sizeof(wrqu));
609 wrqu.data.length = resp_ie_len;
Johannes Berg667503dd2009-07-07 03:56:11 +0200610 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
611 &wrqu, resp_ie);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200612 }
613
614 memset(&wrqu, 0, sizeof(wrqu));
615 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530616 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
617 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200618 wdev->wext.prev_bssid_valid = true;
Johannes Berg667503dd2009-07-07 03:56:11 +0200619 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200620#endif
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530621
622 return;
623out:
Johannes Berg5b112d32013-02-01 01:49:58 +0100624 cfg80211_put_bss(wdev->wiphy, bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200625}
Johannes Berg667503dd2009-07-07 03:56:11 +0200626
Jouni Malinened9d0102011-05-16 19:40:15 +0300627void cfg80211_roamed(struct net_device *dev,
628 struct ieee80211_channel *channel,
629 const u8 *bssid,
Johannes Berg667503dd2009-07-07 03:56:11 +0200630 const u8 *req_ie, size_t req_ie_len,
631 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
632{
633 struct wireless_dev *wdev = dev->ieee80211_ptr;
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530634 struct cfg80211_bss *bss;
635
636 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
637
638 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
639 wdev->ssid_len, WLAN_CAPABILITY_ESS,
640 WLAN_CAPABILITY_ESS);
641 if (WARN_ON(!bss))
642 return;
643
644 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
645 resp_ie_len, gfp);
646}
647EXPORT_SYMBOL(cfg80211_roamed);
648
649void cfg80211_roamed_bss(struct net_device *dev,
650 struct cfg80211_bss *bss, const u8 *req_ie,
651 size_t req_ie_len, const u8 *resp_ie,
652 size_t resp_ie_len, gfp_t gfp)
653{
654 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg667503dd2009-07-07 03:56:11 +0200655 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
656 struct cfg80211_event *ev;
657 unsigned long flags;
658
Johannes Bergf7969962009-08-21 12:23:49 +0200659 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
660
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530661 if (WARN_ON(!bss))
Johannes Berg667503dd2009-07-07 03:56:11 +0200662 return;
663
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530664 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
665 if (!ev) {
Johannes Berg5b112d32013-02-01 01:49:58 +0100666 cfg80211_put_bss(wdev->wiphy, bss);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530667 return;
668 }
669
Johannes Berg667503dd2009-07-07 03:56:11 +0200670 ev->type = EVENT_ROAMED;
Johannes Berg667503dd2009-07-07 03:56:11 +0200671 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
672 ev->rm.req_ie_len = req_ie_len;
673 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
674 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
675 ev->rm.resp_ie_len = resp_ie_len;
676 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530677 ev->rm.bss = bss;
Johannes Berg667503dd2009-07-07 03:56:11 +0200678
679 spin_lock_irqsave(&wdev->event_lock, flags);
680 list_add_tail(&ev->list, &wdev->event_list);
681 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100682 queue_work(cfg80211_wq, &rdev->event_work);
Johannes Berg667503dd2009-07-07 03:56:11 +0200683}
Vasanthakumar Thiagarajanadbde342011-12-08 14:28:47 +0530684EXPORT_SYMBOL(cfg80211_roamed_bss);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200685
Johannes Berg667503dd2009-07-07 03:56:11 +0200686void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
Johannes Berg6829c872009-07-02 09:13:27 +0200687 size_t ie_len, u16 reason, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688{
689 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergfffd0932009-07-08 14:22:54 +0200690 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
691 int i;
Johannes Berg3d23e342009-09-29 23:27:28 +0200692#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200693 union iwreq_data wrqu;
694#endif
695
Johannes Berg667503dd2009-07-07 03:56:11 +0200696 ASSERT_WDEV_LOCK(wdev);
697
Johannes Berg074ac8d2010-09-16 14:58:22 +0200698 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
699 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
Samuel Ortizb23aa672009-07-01 21:26:54 +0200700 return;
701
Johannes Bergf7969962009-08-21 12:23:49 +0200702 if (wdev->sme_state != CFG80211_SME_CONNECTED)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200703 return;
704
705 if (wdev->current_bss) {
706 cfg80211_unhold_bss(wdev->current_bss);
Johannes Berg5b112d32013-02-01 01:49:58 +0100707 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200708 }
709
710 wdev->current_bss = NULL;
711 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200712 wdev->ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200713
Johannes Berg6829c872009-07-02 09:13:27 +0200714 if (wdev->conn) {
715 kfree(wdev->conn->ie);
716 wdev->conn->ie = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +0200717 kfree(wdev->conn);
718 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200719 }
720
Johannes Bergfffd0932009-07-08 14:22:54 +0200721 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
722
723 /*
724 * Delete all the keys ... pairwise keys can't really
725 * exist any more anyway, but default keys might.
726 */
727 if (rdev->ops->del_key)
728 for (i = 0; i < 6; i++)
Hila Gonene35e4d22012-06-27 17:19:42 +0300729 rdev_del_key(rdev, dev, i, false, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200730
Johannes Berg3d23e342009-09-29 23:27:28 +0200731#ifdef CONFIG_CFG80211_WEXT
Samuel Ortizb23aa672009-07-01 21:26:54 +0200732 memset(&wrqu, 0, sizeof(wrqu));
733 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
734 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
Abhijeet Kolekar5f612032010-01-13 13:23:14 -0800735 wdev->wext.connect.ssid_len = 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200736#endif
Luis R. Rodriguez09d989d2010-01-29 19:58:57 -0500737
738 schedule_work(&cfg80211_disconnect_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200739}
740
741void cfg80211_disconnected(struct net_device *dev, u16 reason,
742 u8 *ie, size_t ie_len, gfp_t gfp)
743{
Johannes Berg667503dd2009-07-07 03:56:11 +0200744 struct wireless_dev *wdev = dev->ieee80211_ptr;
745 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
746 struct cfg80211_event *ev;
747 unsigned long flags;
748
Johannes Bergf7969962009-08-21 12:23:49 +0200749 CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
750
Johannes Berg667503dd2009-07-07 03:56:11 +0200751 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
752 if (!ev)
753 return;
754
755 ev->type = EVENT_DISCONNECTED;
756 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
757 ev->dc.ie_len = ie_len;
758 memcpy((void *)ev->dc.ie, ie, ie_len);
759 ev->dc.reason = reason;
760
761 spin_lock_irqsave(&wdev->event_lock, flags);
762 list_add_tail(&ev->list, &wdev->event_list);
763 spin_unlock_irqrestore(&wdev->event_lock, flags);
Alban Browaeyse60d7442009-11-25 15:13:00 +0100764 queue_work(cfg80211_wq, &rdev->event_work);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200765}
766EXPORT_SYMBOL(cfg80211_disconnected);
767
Johannes Berg667503dd2009-07-07 03:56:11 +0200768int __cfg80211_connect(struct cfg80211_registered_device *rdev,
769 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200770 struct cfg80211_connect_params *connect,
Johannes Bergf401a6f2009-08-07 14:51:05 +0200771 struct cfg80211_cached_keys *connkeys,
772 const u8 *prev_bssid)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773{
Samuel Ortizb23aa672009-07-01 21:26:54 +0200774 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbbac31f2009-09-16 09:04:26 -0700775 struct cfg80211_bss *bss = NULL;
Johannes Berg667503dd2009-07-07 03:56:11 +0200776 int err;
777
778 ASSERT_WDEV_LOCK(wdev);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200779
780 if (wdev->sme_state != CFG80211_SME_IDLE)
781 return -EALREADY;
782
Johannes Bergfffd0932009-07-08 14:22:54 +0200783 if (WARN_ON(wdev->connect_keys)) {
784 kfree(wdev->connect_keys);
785 wdev->connect_keys = NULL;
786 }
787
Ben Greear7e7c8922011-11-18 11:31:59 -0800788 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
789 rdev->wiphy.ht_capa_mod_mask);
790
Johannes Bergfffd0932009-07-08 14:22:54 +0200791 if (connkeys && connkeys->def >= 0) {
792 int idx;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200793 u32 cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200794
795 idx = connkeys->def;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200796 cipher = connkeys->params[idx].cipher;
Johannes Bergfffd0932009-07-08 14:22:54 +0200797 /* If given a WEP key we may need it for shared key auth */
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200798 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
799 cipher == WLAN_CIPHER_SUITE_WEP104) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200800 connect->key_idx = idx;
801 connect->key = connkeys->params[idx].key;
802 connect->key_len = connkeys->params[idx].key_len;
Samuel Ortizbcba8ea2009-08-06 21:04:41 +0200803
804 /*
805 * If ciphers are not set (e.g. when going through
806 * iwconfig), we have to set them appropriately here.
807 */
808 if (connect->crypto.cipher_group == 0)
809 connect->crypto.cipher_group = cipher;
810
811 if (connect->crypto.n_ciphers_pairwise == 0) {
812 connect->crypto.n_ciphers_pairwise = 1;
813 connect->crypto.ciphers_pairwise[0] = cipher;
814 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200815 }
816 }
817
Samuel Ortizb23aa672009-07-01 21:26:54 +0200818 if (!rdev->ops->connect) {
Johannes Berg6829c872009-07-02 09:13:27 +0200819 if (!rdev->ops->auth || !rdev->ops->assoc)
820 return -EOPNOTSUPP;
821
Johannes Berg19957bb2009-07-02 17:20:43 +0200822 if (WARN_ON(wdev->conn))
823 return -EINPROGRESS;
824
825 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
826 if (!wdev->conn)
827 return -ENOMEM;
Johannes Berg6829c872009-07-02 09:13:27 +0200828
829 /*
830 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
831 */
832 memcpy(&wdev->conn->params, connect, sizeof(*connect));
833 if (connect->bssid) {
834 wdev->conn->params.bssid = wdev->conn->bssid;
835 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
836 }
837
838 if (connect->ie) {
839 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
840 GFP_KERNEL);
841 wdev->conn->params.ie = wdev->conn->ie;
Johannes Berg19957bb2009-07-02 17:20:43 +0200842 if (!wdev->conn->ie) {
843 kfree(wdev->conn);
844 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200845 return -ENOMEM;
Johannes Berg19957bb2009-07-02 17:20:43 +0200846 }
Johannes Berg6829c872009-07-02 09:13:27 +0200847 }
848
849 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
850 wdev->conn->auto_auth = true;
851 /* start with open system ... should mostly work */
852 wdev->conn->params.auth_type =
853 NL80211_AUTHTYPE_OPEN_SYSTEM;
854 } else {
855 wdev->conn->auto_auth = false;
856 }
857
858 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
859 wdev->ssid_len = connect->ssid_len;
860 wdev->conn->params.ssid = wdev->ssid;
861 wdev->conn->params.ssid_len = connect->ssid_len;
862
Johannes Berg8bb89482009-09-26 14:42:53 +0200863 /* see if we have the bss already */
864 bss = cfg80211_get_conn_bss(wdev);
Johannes Berg6829c872009-07-02 09:13:27 +0200865
866 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200867 wdev->connect_keys = connkeys;
Johannes Berg6829c872009-07-02 09:13:27 +0200868
Johannes Bergf401a6f2009-08-07 14:51:05 +0200869 if (prev_bssid) {
870 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
871 wdev->conn->prev_bssid_valid = true;
872 }
873
Johannes Bergbbac31f2009-09-16 09:04:26 -0700874 /* we're good if we have a matching bss struct */
875 if (bss) {
Johannes Berg6829c872009-07-02 09:13:27 +0200876 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
877 err = cfg80211_conn_do_work(wdev);
Johannes Berg5b112d32013-02-01 01:49:58 +0100878 cfg80211_put_bss(wdev->wiphy, bss);
Johannes Berg6829c872009-07-02 09:13:27 +0200879 } else {
880 /* otherwise we'll need to scan for the AP first */
881 err = cfg80211_conn_scan(wdev);
882 /*
883 * If we can't scan right now, then we need to scan again
884 * after the current scan finished, since the parameters
885 * changed (unless we find a good AP anyway).
886 */
887 if (err == -EBUSY) {
888 err = 0;
889 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
890 }
891 }
Johannes Berg19957bb2009-07-02 17:20:43 +0200892 if (err) {
David Kilroy415ad1ef2009-08-19 00:43:31 +0100893 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200894 kfree(wdev->conn);
895 wdev->conn = NULL;
Johannes Berg6829c872009-07-02 09:13:27 +0200896 wdev->sme_state = CFG80211_SME_IDLE;
Johannes Bergfffd0932009-07-08 14:22:54 +0200897 wdev->connect_keys = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200898 wdev->ssid_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +0200899 }
Johannes Berg6829c872009-07-02 09:13:27 +0200900
901 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200902 } else {
903 wdev->sme_state = CFG80211_SME_CONNECTING;
Johannes Bergfffd0932009-07-08 14:22:54 +0200904 wdev->connect_keys = connkeys;
Hila Gonene35e4d22012-06-27 17:19:42 +0300905 err = rdev_connect(rdev, dev, connect);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200906 if (err) {
Johannes Bergfffd0932009-07-08 14:22:54 +0200907 wdev->connect_keys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200908 wdev->sme_state = CFG80211_SME_IDLE;
909 return err;
910 }
Johannes Berg6829c872009-07-02 09:13:27 +0200911
912 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
913 wdev->ssid_len = connect->ssid_len;
914
915 return 0;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200916 }
Samuel Ortizb23aa672009-07-01 21:26:54 +0200917}
918
Johannes Berg667503dd2009-07-07 03:56:11 +0200919int cfg80211_connect(struct cfg80211_registered_device *rdev,
920 struct net_device *dev,
Johannes Bergfffd0932009-07-08 14:22:54 +0200921 struct cfg80211_connect_params *connect,
922 struct cfg80211_cached_keys *connkeys)
Johannes Berg667503dd2009-07-07 03:56:11 +0200923{
924 int err;
925
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200926 mutex_lock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200927 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf401a6f2009-08-07 14:51:05 +0200928 err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
Johannes Berg667503dd2009-07-07 03:56:11 +0200929 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg59bbb6f2009-08-07 17:22:35 +0200930 mutex_unlock(&rdev->devlist_mtx);
Johannes Berg667503dd2009-07-07 03:56:11 +0200931
932 return err;
933}
934
935int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
936 struct net_device *dev, u16 reason, bool wextev)
Samuel Ortizb23aa672009-07-01 21:26:54 +0200937{
Johannes Berg6829c872009-07-02 09:13:27 +0200938 struct wireless_dev *wdev = dev->ieee80211_ptr;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200939 int err;
940
Johannes Berg667503dd2009-07-07 03:56:11 +0200941 ASSERT_WDEV_LOCK(wdev);
942
Johannes Berg6829c872009-07-02 09:13:27 +0200943 if (wdev->sme_state == CFG80211_SME_IDLE)
944 return -EINVAL;
945
Johannes Bergfffd0932009-07-08 14:22:54 +0200946 kfree(wdev->connect_keys);
947 wdev->connect_keys = NULL;
948
Samuel Ortizb23aa672009-07-01 21:26:54 +0200949 if (!rdev->ops->disconnect) {
Johannes Berg19957bb2009-07-02 17:20:43 +0200950 if (!rdev->ops->deauth)
951 return -EOPNOTSUPP;
Johannes Berg6829c872009-07-02 09:13:27 +0200952
Johannes Berg19957bb2009-07-02 17:20:43 +0200953 /* was it connected by userspace SME? */
954 if (!wdev->conn) {
955 cfg80211_mlme_down(rdev, dev);
956 return 0;
957 }
Johannes Berg6829c872009-07-02 09:13:27 +0200958
959 if (wdev->sme_state == CFG80211_SME_CONNECTING &&
960 (wdev->conn->state == CFG80211_CONN_SCANNING ||
961 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
962 wdev->sme_state = CFG80211_SME_IDLE;
David Kilroy415ad1ef2009-08-19 00:43:31 +0100963 kfree(wdev->conn->ie);
Johannes Berg19957bb2009-07-02 17:20:43 +0200964 kfree(wdev->conn);
965 wdev->conn = NULL;
Johannes Berg8dadadb2009-08-04 09:32:23 +0200966 wdev->ssid_len = 0;
Johannes Berg6829c872009-07-02 09:13:27 +0200967 return 0;
968 }
969
Johannes Berg6829c872009-07-02 09:13:27 +0200970 /* wdev->conn->params.bssid must be set if > SCANNING */
Johannes Berg667503dd2009-07-07 03:56:11 +0200971 err = __cfg80211_mlme_deauth(rdev, dev,
972 wdev->conn->params.bssid,
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300973 NULL, 0, reason, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200974 if (err)
975 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +0200976 } else {
Hila Gonene35e4d22012-06-27 17:19:42 +0300977 err = rdev_disconnect(rdev, dev, reason);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200978 if (err)
979 return err;
980 }
981
Johannes Berg6829c872009-07-02 09:13:27 +0200982 if (wdev->sme_state == CFG80211_SME_CONNECTED)
Johannes Berg667503dd2009-07-07 03:56:11 +0200983 __cfg80211_disconnected(dev, NULL, 0, 0, false);
Johannes Berg6829c872009-07-02 09:13:27 +0200984 else if (wdev->sme_state == CFG80211_SME_CONNECTING)
Johannes Bergf2129352009-07-01 21:26:56 +0200985 __cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
986 WLAN_STATUS_UNSPECIFIED_FAILURE,
Johannes Bergdf7fc0f2009-07-29 11:23:49 +0200987 wextev, NULL);
Samuel Ortizb23aa672009-07-01 21:26:54 +0200988
989 return 0;
990}
Johannes Berg19957bb2009-07-02 17:20:43 +0200991
Johannes Berg667503dd2009-07-07 03:56:11 +0200992int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
993 struct net_device *dev,
994 u16 reason, bool wextev)
995{
996 int err;
997
998 wdev_lock(dev->ieee80211_ptr);
999 err = __cfg80211_disconnect(rdev, dev, reason, wextev);
1000 wdev_unlock(dev->ieee80211_ptr);
1001
1002 return err;
1003}
1004
Johannes Berg95de8172012-01-20 13:55:25 +01001005void cfg80211_sme_disassoc(struct net_device *dev,
1006 struct cfg80211_internal_bss *bss)
Johannes Berg19957bb2009-07-02 17:20:43 +02001007{
1008 struct wireless_dev *wdev = dev->ieee80211_ptr;
1009 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1010 u8 bssid[ETH_ALEN];
1011
Johannes Berg667503dd2009-07-07 03:56:11 +02001012 ASSERT_WDEV_LOCK(wdev);
1013
Johannes Berg19957bb2009-07-02 17:20:43 +02001014 if (!wdev->conn)
1015 return;
1016
1017 if (wdev->conn->state == CFG80211_CONN_IDLE)
1018 return;
1019
1020 /*
1021 * Ok, so the association was made by this SME -- we don't
1022 * want it any more so deauthenticate too.
1023 */
1024
Johannes Berg95de8172012-01-20 13:55:25 +01001025 memcpy(bssid, bss->pub.bssid, ETH_ALEN);
Johannes Berg19957bb2009-07-02 17:20:43 +02001026
Johannes Berg95de8172012-01-20 13:55:25 +01001027 __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
1028 WLAN_REASON_DEAUTH_LEAVING, false);
Johannes Berg19957bb2009-07-02 17:20:43 +02001029}