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
29 struct 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
cfg80211_sme_free(struct wireless_dev * wdev)51 static 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
cfg80211_conn_scan(struct wireless_dev * wdev)61 static 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
cfg80211_conn_do_work(struct wireless_dev * wdev)143 static 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
cfg80211_conn_work(struct work_struct * work)220 void 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. */
cfg80211_get_conn_bss(struct wireless_dev * wdev)259 static 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
__cfg80211_sme_scan_done(struct net_device * dev)284 static 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
cfg80211_sme_scan_done(struct net_device * dev)306 void 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
cfg80211_sme_rx_auth(struct wireless_dev * wdev,const u8 * buf,size_t len)315 void 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
cfg80211_sme_rx_assoc_resp(struct wireless_dev * wdev,u16 status)362 bool 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
cfg80211_sme_deauth(struct wireless_dev * wdev)391 void cfg80211_sme_deauth(struct wireless_dev *wdev)
392 {
393 cfg80211_sme_free(wdev);
394 }
395
cfg80211_sme_auth_timeout(struct wireless_dev * wdev)396 void 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
cfg80211_sme_disassoc(struct wireless_dev * wdev)407 void 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
cfg80211_sme_assoc_timeout(struct wireless_dev * wdev)418 void 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
cfg80211_sme_abandon_assoc(struct wireless_dev * wdev)429 void 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
cfg80211_sme_get_conn_ies(struct wireless_dev * wdev,const u8 * ies,size_t ies_len,const u8 ** out_ies,size_t * out_ies_len)440 static 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
cfg80211_sme_connect(struct wireless_dev * wdev,struct cfg80211_connect_params * connect,const u8 * prev_bssid)496 static 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 cfg80211_unhold_bss(wdev->current_bss);
509 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
510 wdev->current_bss = NULL;
511
512 cfg80211_sme_free(wdev);
513 }
514
515 if (WARN_ON(wdev->conn))
516 return -EINPROGRESS;
517
518 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
519 if (!wdev->conn)
520 return -ENOMEM;
521
522 /*
523 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
524 */
525 memcpy(&wdev->conn->params, connect, sizeof(*connect));
526 if (connect->bssid) {
527 wdev->conn->params.bssid = wdev->conn->bssid;
528 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
529 }
530
531 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
532 &wdev->conn->ie,
533 &wdev->conn->params.ie_len)) {
534 kfree(wdev->conn);
535 wdev->conn = NULL;
536 return -ENOMEM;
537 }
538 wdev->conn->params.ie = wdev->conn->ie;
539
540 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
541 wdev->conn->auto_auth = true;
542 /* start with open system ... should mostly work */
543 wdev->conn->params.auth_type =
544 NL80211_AUTHTYPE_OPEN_SYSTEM;
545 } else {
546 wdev->conn->auto_auth = false;
547 }
548
549 wdev->conn->params.ssid = wdev->ssid;
550 wdev->conn->params.ssid_len = wdev->ssid_len;
551
552 /* see if we have the bss already */
553 bss = cfg80211_get_conn_bss(wdev);
554
555 if (prev_bssid) {
556 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
557 wdev->conn->prev_bssid_valid = true;
558 }
559
560 /* we're good if we have a matching bss struct */
561 if (bss) {
562 err = cfg80211_conn_do_work(wdev);
563 cfg80211_put_bss(wdev->wiphy, bss);
564 } else {
565 /* otherwise we'll need to scan for the AP first */
566 err = cfg80211_conn_scan(wdev);
567
568 /*
569 * If we can't scan right now, then we need to scan again
570 * after the current scan finished, since the parameters
571 * changed (unless we find a good AP anyway).
572 */
573 if (err == -EBUSY) {
574 err = 0;
575 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
576 }
577 }
578
579 if (err)
580 cfg80211_sme_free(wdev);
581
582 return err;
583 }
584
cfg80211_sme_disconnect(struct wireless_dev * wdev,u16 reason)585 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
586 {
587 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
588 int err;
589
590 if (!wdev->conn)
591 return 0;
592
593 if (!rdev->ops->deauth)
594 return -EOPNOTSUPP;
595
596 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
597 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
598 err = 0;
599 goto out;
600 }
601
602 /* wdev->conn->params.bssid must be set if > SCANNING */
603 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
604 wdev->conn->params.bssid,
605 NULL, 0, reason, false);
606 out:
607 cfg80211_sme_free(wdev);
608 return err;
609 }
610
611 /*
612 * code shared for in-device and software SME
613 */
614
cfg80211_is_all_idle(void)615 static bool cfg80211_is_all_idle(void)
616 {
617 struct cfg80211_registered_device *rdev;
618 struct wireless_dev *wdev;
619 bool is_all_idle = true;
620
621 /*
622 * All devices must be idle as otherwise if you are actively
623 * scanning some new beacon hints could be learned and would
624 * count as new regulatory hints.
625 */
626 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
627 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
628 wdev_lock(wdev);
629 if (wdev->conn || wdev->current_bss)
630 is_all_idle = false;
631 wdev_unlock(wdev);
632 }
633 }
634
635 return is_all_idle;
636 }
637
disconnect_work(struct work_struct * work)638 static void disconnect_work(struct work_struct *work)
639 {
640 rtnl_lock();
641 if (cfg80211_is_all_idle())
642 regulatory_hint_disconnect();
643 rtnl_unlock();
644 }
645
646 static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
647
648
649 /*
650 * API calls for drivers implementing connect/disconnect and
651 * SME event handling
652 */
653
654 /* This method must consume bss one way or another */
__cfg80211_connect_result(struct net_device * dev,const u8 * bssid,const u8 * req_ie,size_t req_ie_len,const u8 * resp_ie,size_t resp_ie_len,int status,bool wextev,struct cfg80211_bss * bss)655 void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
656 const u8 *req_ie, size_t req_ie_len,
657 const u8 *resp_ie, size_t resp_ie_len,
658 int status, bool wextev,
659 struct cfg80211_bss *bss)
660 {
661 struct wireless_dev *wdev = dev->ieee80211_ptr;
662 const u8 *country_ie;
663 #ifdef CONFIG_CFG80211_WEXT
664 union iwreq_data wrqu;
665 #endif
666
667 ASSERT_WDEV_LOCK(wdev);
668
669 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
670 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
671 cfg80211_put_bss(wdev->wiphy, bss);
672 return;
673 }
674
675 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
676 bssid, req_ie, req_ie_len,
677 resp_ie, resp_ie_len,
678 status, GFP_KERNEL);
679
680 #ifdef CONFIG_CFG80211_WEXT
681 if (wextev) {
682 if (req_ie && status == WLAN_STATUS_SUCCESS) {
683 memset(&wrqu, 0, sizeof(wrqu));
684 wrqu.data.length = req_ie_len;
685 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
686 }
687
688 if (resp_ie && status == WLAN_STATUS_SUCCESS) {
689 memset(&wrqu, 0, sizeof(wrqu));
690 wrqu.data.length = resp_ie_len;
691 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
692 }
693
694 memset(&wrqu, 0, sizeof(wrqu));
695 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
696 if (bssid && status == WLAN_STATUS_SUCCESS) {
697 memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
698 memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
699 wdev->wext.prev_bssid_valid = true;
700 }
701 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
702 }
703 #endif
704
705 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
706 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
707 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
708 wdev->ssid, wdev->ssid_len,
709 wdev->conn_bss_type,
710 IEEE80211_PRIVACY_ANY);
711 if (bss)
712 cfg80211_hold_bss(bss_from_pub(bss));
713 }
714
715 if (wdev->current_bss) {
716 cfg80211_unhold_bss(wdev->current_bss);
717 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
718 wdev->current_bss = NULL;
719 }
720
721 if (status != WLAN_STATUS_SUCCESS) {
722 kzfree(wdev->connect_keys);
723 wdev->connect_keys = NULL;
724 wdev->ssid_len = 0;
725 if (bss) {
726 cfg80211_unhold_bss(bss_from_pub(bss));
727 cfg80211_put_bss(wdev->wiphy, bss);
728 }
729 cfg80211_sme_free(wdev);
730 return;
731 }
732
733 if (WARN_ON(!bss))
734 return;
735
736 wdev->current_bss = bss_from_pub(bss);
737
738 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
739 cfg80211_upload_connect_keys(wdev);
740
741 rcu_read_lock();
742 country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
743 if (!country_ie) {
744 rcu_read_unlock();
745 return;
746 }
747
748 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
749 rcu_read_unlock();
750
751 if (!country_ie)
752 return;
753
754 /*
755 * ieee80211_bss_get_ie() ensures we can access:
756 * - country_ie + 2, the start of the country ie data, and
757 * - and country_ie[1] which is the IE length
758 */
759 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
760 country_ie + 2, country_ie[1]);
761 kfree(country_ie);
762 }
763
764 /* Consumes bss object one way or another */
cfg80211_connect_bss(struct net_device * dev,const u8 * bssid,struct cfg80211_bss * bss,const u8 * req_ie,size_t req_ie_len,const u8 * resp_ie,size_t resp_ie_len,int status,gfp_t gfp)765 void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
766 struct cfg80211_bss *bss, const u8 *req_ie,
767 size_t req_ie_len, const u8 *resp_ie,
768 size_t resp_ie_len, int status, gfp_t gfp)
769 {
770 struct wireless_dev *wdev = dev->ieee80211_ptr;
771 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
772 struct cfg80211_event *ev;
773 unsigned long flags;
774
775 if (bss) {
776 /* Make sure the bss entry provided by the driver is valid. */
777 struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
778
779 if (WARN_ON(list_empty(&ibss->list))) {
780 cfg80211_put_bss(wdev->wiphy, bss);
781 return;
782 }
783 }
784
785 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
786 if (!ev) {
787 cfg80211_put_bss(wdev->wiphy, bss);
788 return;
789 }
790
791 ev->type = EVENT_CONNECT_RESULT;
792 if (bssid)
793 memcpy(ev->cr.bssid, bssid, ETH_ALEN);
794 if (req_ie_len) {
795 ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
796 ev->cr.req_ie_len = req_ie_len;
797 memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
798 }
799 if (resp_ie_len) {
800 ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
801 ev->cr.resp_ie_len = resp_ie_len;
802 memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
803 }
804 if (bss)
805 cfg80211_hold_bss(bss_from_pub(bss));
806 ev->cr.bss = bss;
807 ev->cr.status = status;
808
809 spin_lock_irqsave(&wdev->event_lock, flags);
810 list_add_tail(&ev->list, &wdev->event_list);
811 spin_unlock_irqrestore(&wdev->event_lock, flags);
812 queue_work(cfg80211_wq, &rdev->event_work);
813 }
814 EXPORT_SYMBOL(cfg80211_connect_bss);
815
816 /* Consumes bss object one way or another */
__cfg80211_roamed(struct wireless_dev * wdev,struct cfg80211_bss * bss,const u8 * req_ie,size_t req_ie_len,const u8 * resp_ie,size_t resp_ie_len)817 void __cfg80211_roamed(struct wireless_dev *wdev,
818 struct cfg80211_bss *bss,
819 const u8 *req_ie, size_t req_ie_len,
820 const u8 *resp_ie, size_t resp_ie_len)
821 {
822 #ifdef CONFIG_CFG80211_WEXT
823 union iwreq_data wrqu;
824 #endif
825 ASSERT_WDEV_LOCK(wdev);
826
827 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
828 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
829 goto out;
830
831 if (WARN_ON(!wdev->current_bss))
832 goto out;
833
834 cfg80211_unhold_bss(wdev->current_bss);
835 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
836 wdev->current_bss = NULL;
837
838 cfg80211_hold_bss(bss_from_pub(bss));
839 wdev->current_bss = bss_from_pub(bss);
840
841 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
842 wdev->netdev, bss->bssid,
843 req_ie, req_ie_len, resp_ie, resp_ie_len,
844 GFP_KERNEL);
845
846 #ifdef CONFIG_CFG80211_WEXT
847 if (req_ie) {
848 memset(&wrqu, 0, sizeof(wrqu));
849 wrqu.data.length = req_ie_len;
850 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
851 &wrqu, req_ie);
852 }
853
854 if (resp_ie) {
855 memset(&wrqu, 0, sizeof(wrqu));
856 wrqu.data.length = resp_ie_len;
857 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
858 &wrqu, resp_ie);
859 }
860
861 memset(&wrqu, 0, sizeof(wrqu));
862 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
863 memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
864 memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
865 wdev->wext.prev_bssid_valid = true;
866 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
867 #endif
868
869 return;
870 out:
871 cfg80211_put_bss(wdev->wiphy, bss);
872 }
873
cfg80211_roamed(struct net_device * dev,struct ieee80211_channel * channel,const u8 * bssid,const u8 * req_ie,size_t req_ie_len,const u8 * resp_ie,size_t resp_ie_len,gfp_t gfp)874 void cfg80211_roamed(struct net_device *dev,
875 struct ieee80211_channel *channel,
876 const u8 *bssid,
877 const u8 *req_ie, size_t req_ie_len,
878 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
879 {
880 struct wireless_dev *wdev = dev->ieee80211_ptr;
881 struct cfg80211_bss *bss;
882
883 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
884 wdev->ssid_len,
885 wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
886 if (WARN_ON(!bss))
887 return;
888
889 cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
890 resp_ie_len, gfp);
891 }
892 EXPORT_SYMBOL(cfg80211_roamed);
893
894 /* Consumes bss object one way or another */
cfg80211_roamed_bss(struct net_device * dev,struct cfg80211_bss * bss,const u8 * req_ie,size_t req_ie_len,const u8 * resp_ie,size_t resp_ie_len,gfp_t gfp)895 void cfg80211_roamed_bss(struct net_device *dev,
896 struct cfg80211_bss *bss, const u8 *req_ie,
897 size_t req_ie_len, const u8 *resp_ie,
898 size_t resp_ie_len, gfp_t gfp)
899 {
900 struct wireless_dev *wdev = dev->ieee80211_ptr;
901 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
902 struct cfg80211_event *ev;
903 unsigned long flags;
904
905 if (WARN_ON(!bss))
906 return;
907
908 ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
909 if (!ev) {
910 cfg80211_put_bss(wdev->wiphy, bss);
911 return;
912 }
913
914 ev->type = EVENT_ROAMED;
915 ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
916 ev->rm.req_ie_len = req_ie_len;
917 memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
918 ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
919 ev->rm.resp_ie_len = resp_ie_len;
920 memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
921 ev->rm.bss = bss;
922
923 spin_lock_irqsave(&wdev->event_lock, flags);
924 list_add_tail(&ev->list, &wdev->event_list);
925 spin_unlock_irqrestore(&wdev->event_lock, flags);
926 queue_work(cfg80211_wq, &rdev->event_work);
927 }
928 EXPORT_SYMBOL(cfg80211_roamed_bss);
929
__cfg80211_disconnected(struct net_device * dev,const u8 * ie,size_t ie_len,u16 reason,bool from_ap)930 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
931 size_t ie_len, u16 reason, bool from_ap)
932 {
933 struct wireless_dev *wdev = dev->ieee80211_ptr;
934 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
935 int i;
936 #ifdef CONFIG_CFG80211_WEXT
937 union iwreq_data wrqu;
938 #endif
939
940 ASSERT_WDEV_LOCK(wdev);
941
942 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
943 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
944 return;
945
946 if (wdev->current_bss) {
947 cfg80211_unhold_bss(wdev->current_bss);
948 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
949 }
950
951 wdev->current_bss = NULL;
952 wdev->ssid_len = 0;
953
954 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
955
956 /* stop critical protocol if supported */
957 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
958 rdev->crit_proto_nlportid = 0;
959 rdev_crit_proto_stop(rdev, wdev);
960 }
961
962 /*
963 * Delete all the keys ... pairwise keys can't really
964 * exist any more anyway, but default keys might.
965 */
966 if (rdev->ops->del_key)
967 for (i = 0; i < 6; i++)
968 rdev_del_key(rdev, dev, i, false, NULL);
969
970 rdev_set_qos_map(rdev, dev, NULL);
971
972 #ifdef CONFIG_CFG80211_WEXT
973 memset(&wrqu, 0, sizeof(wrqu));
974 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
975 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
976 wdev->wext.connect.ssid_len = 0;
977 #endif
978
979 schedule_work(&cfg80211_disconnect_work);
980 }
981
cfg80211_disconnected(struct net_device * dev,u16 reason,const u8 * ie,size_t ie_len,bool locally_generated,gfp_t gfp)982 void cfg80211_disconnected(struct net_device *dev, u16 reason,
983 const u8 *ie, size_t ie_len,
984 bool locally_generated, gfp_t gfp)
985 {
986 struct wireless_dev *wdev = dev->ieee80211_ptr;
987 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
988 struct cfg80211_event *ev;
989 unsigned long flags;
990
991 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
992 if (!ev)
993 return;
994
995 ev->type = EVENT_DISCONNECTED;
996 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
997 ev->dc.ie_len = ie_len;
998 memcpy((void *)ev->dc.ie, ie, ie_len);
999 ev->dc.reason = reason;
1000 ev->dc.locally_generated = locally_generated;
1001
1002 spin_lock_irqsave(&wdev->event_lock, flags);
1003 list_add_tail(&ev->list, &wdev->event_list);
1004 spin_unlock_irqrestore(&wdev->event_lock, flags);
1005 queue_work(cfg80211_wq, &rdev->event_work);
1006 }
1007 EXPORT_SYMBOL(cfg80211_disconnected);
1008
1009 /*
1010 * API calls for nl80211/wext compatibility code
1011 */
cfg80211_connect(struct cfg80211_registered_device * rdev,struct net_device * dev,struct cfg80211_connect_params * connect,struct cfg80211_cached_keys * connkeys,const u8 * prev_bssid)1012 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1013 struct net_device *dev,
1014 struct cfg80211_connect_params *connect,
1015 struct cfg80211_cached_keys *connkeys,
1016 const u8 *prev_bssid)
1017 {
1018 struct wireless_dev *wdev = dev->ieee80211_ptr;
1019 int err;
1020
1021 ASSERT_WDEV_LOCK(wdev);
1022
1023 /*
1024 * If we have an ssid_len, we're trying to connect or are
1025 * already connected, so reject a new SSID unless it's the
1026 * same (which is the case for re-association.)
1027 */
1028 if (wdev->ssid_len &&
1029 (wdev->ssid_len != connect->ssid_len ||
1030 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1031 return -EALREADY;
1032
1033 /*
1034 * If connected, reject (re-)association unless prev_bssid
1035 * matches the current BSSID.
1036 */
1037 if (wdev->current_bss) {
1038 if (!prev_bssid)
1039 return -EALREADY;
1040 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1041 return -ENOTCONN;
1042 }
1043
1044 /*
1045 * Reject if we're in the process of connecting with WEP,
1046 * this case isn't very interesting and trying to handle
1047 * it would make the code much more complex.
1048 */
1049 if (wdev->connect_keys)
1050 return -EINPROGRESS;
1051
1052 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1053 rdev->wiphy.ht_capa_mod_mask);
1054
1055 if (connkeys && connkeys->def >= 0) {
1056 int idx;
1057 u32 cipher;
1058
1059 idx = connkeys->def;
1060 cipher = connkeys->params[idx].cipher;
1061 /* If given a WEP key we may need it for shared key auth */
1062 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1063 cipher == WLAN_CIPHER_SUITE_WEP104) {
1064 connect->key_idx = idx;
1065 connect->key = connkeys->params[idx].key;
1066 connect->key_len = connkeys->params[idx].key_len;
1067
1068 /*
1069 * If ciphers are not set (e.g. when going through
1070 * iwconfig), we have to set them appropriately here.
1071 */
1072 if (connect->crypto.cipher_group == 0)
1073 connect->crypto.cipher_group = cipher;
1074
1075 if (connect->crypto.n_ciphers_pairwise == 0) {
1076 connect->crypto.n_ciphers_pairwise = 1;
1077 connect->crypto.ciphers_pairwise[0] = cipher;
1078 }
1079 }
1080
1081 connect->crypto.wep_keys = connkeys->params;
1082 connect->crypto.wep_tx_key = connkeys->def;
1083 } else {
1084 if (WARN_ON(connkeys))
1085 return -EINVAL;
1086 }
1087
1088 wdev->connect_keys = connkeys;
1089 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1090 wdev->ssid_len = connect->ssid_len;
1091
1092 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1093 IEEE80211_BSS_TYPE_ESS;
1094
1095 if (!rdev->ops->connect)
1096 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1097 else
1098 err = rdev_connect(rdev, dev, connect);
1099
1100 if (err) {
1101 wdev->connect_keys = NULL;
1102 /*
1103 * This could be reassoc getting refused, don't clear
1104 * ssid_len in that case.
1105 */
1106 if (!wdev->current_bss)
1107 wdev->ssid_len = 0;
1108 return err;
1109 }
1110
1111 return 0;
1112 }
1113
cfg80211_disconnect(struct cfg80211_registered_device * rdev,struct net_device * dev,u16 reason,bool wextev)1114 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1115 struct net_device *dev, u16 reason, bool wextev)
1116 {
1117 struct wireless_dev *wdev = dev->ieee80211_ptr;
1118 int err = 0;
1119
1120 ASSERT_WDEV_LOCK(wdev);
1121
1122 kzfree(wdev->connect_keys);
1123 wdev->connect_keys = NULL;
1124
1125 if (wdev->conn)
1126 err = cfg80211_sme_disconnect(wdev, reason);
1127 else if (!rdev->ops->disconnect)
1128 cfg80211_mlme_down(rdev, dev);
1129 else if (wdev->current_bss)
1130 err = rdev_disconnect(rdev, dev, reason);
1131
1132 /*
1133 * Clear ssid_len unless we actually were fully connected,
1134 * in which case cfg80211_disconnected() will take care of
1135 * this later.
1136 */
1137 if (!wdev->current_bss)
1138 wdev->ssid_len = 0;
1139
1140 return err;
1141 }
1142