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