1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SME code for cfg80211
4 * both driver SME event handling and the SME implementation
5 * (for nl80211's connect() and wext)
6 *
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
8 * Copyright (C) 2009, 2020 Intel Corporation. All rights reserved.
9 * Copyright 2017 Intel Deutschland GmbH
10 */
11
12 #include <linux/etherdevice.h>
13 #include <linux/if_arp.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/wireless.h>
17 #include <linux/export.h>
18 #include <net/iw_handler.h>
19 #include <net/cfg80211.h>
20 #include <net/rtnetlink.h>
21 #include "nl80211.h"
22 #include "reg.h"
23 #include "rdev-ops.h"
24
25 /*
26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
27 * driver. This is for implementing nl80211's connect/disconnect
28 * and wireless extensions (if configured.)
29 */
30
31 struct cfg80211_conn {
32 struct cfg80211_connect_params params;
33 /* these are sub-states of the _CONNECTING sme_state */
34 enum {
35 CFG80211_CONN_SCANNING,
36 CFG80211_CONN_SCAN_AGAIN,
37 CFG80211_CONN_AUTHENTICATE_NEXT,
38 CFG80211_CONN_AUTHENTICATING,
39 CFG80211_CONN_AUTH_FAILED_TIMEOUT,
40 CFG80211_CONN_ASSOCIATE_NEXT,
41 CFG80211_CONN_ASSOCIATING,
42 CFG80211_CONN_ASSOC_FAILED,
43 CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
44 CFG80211_CONN_DEAUTH,
45 CFG80211_CONN_ABANDON,
46 CFG80211_CONN_CONNECTED,
47 } state;
48 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
49 const u8 *ie;
50 size_t ie_len;
51 bool auto_auth, prev_bssid_valid;
52 };
53
cfg80211_sme_free(struct wireless_dev * wdev)54 static void cfg80211_sme_free(struct wireless_dev *wdev)
55 {
56 if (!wdev->conn)
57 return;
58
59 kfree(wdev->conn->ie);
60 kfree(wdev->conn);
61 wdev->conn = NULL;
62 }
63
cfg80211_conn_scan(struct wireless_dev * wdev)64 static int cfg80211_conn_scan(struct wireless_dev *wdev)
65 {
66 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
67 struct cfg80211_scan_request *request;
68 int n_channels, err;
69
70 ASSERT_RTNL();
71 ASSERT_WDEV_LOCK(wdev);
72
73 if (rdev->scan_req || rdev->scan_msg)
74 return -EBUSY;
75
76 if (wdev->conn->params.channel)
77 n_channels = 1;
78 else
79 n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
80
81 request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
82 sizeof(request->channels[0]) * n_channels,
83 GFP_KERNEL);
84 if (!request)
85 return -ENOMEM;
86
87 if (wdev->conn->params.channel) {
88 enum nl80211_band band = wdev->conn->params.channel->band;
89 struct ieee80211_supported_band *sband =
90 wdev->wiphy->bands[band];
91
92 if (!sband) {
93 kfree(request);
94 return -EINVAL;
95 }
96 request->channels[0] = wdev->conn->params.channel;
97 request->rates[band] = (1 << sband->n_bitrates) - 1;
98 } else {
99 int i = 0, j;
100 enum nl80211_band band;
101 struct ieee80211_supported_band *bands;
102 struct ieee80211_channel *channel;
103
104 for (band = 0; band < NUM_NL80211_BANDS; band++) {
105 bands = wdev->wiphy->bands[band];
106 if (!bands)
107 continue;
108 for (j = 0; j < bands->n_channels; j++) {
109 channel = &bands->channels[j];
110 if (channel->flags & IEEE80211_CHAN_DISABLED)
111 continue;
112 request->channels[i++] = channel;
113 }
114 request->rates[band] = (1 << bands->n_bitrates) - 1;
115 }
116 n_channels = i;
117 }
118 request->n_channels = n_channels;
119 request->ssids = (void *)&request->channels[n_channels];
120 request->n_ssids = 1;
121
122 memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
123 wdev->conn->params.ssid_len);
124 request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
125
126 eth_broadcast_addr(request->bssid);
127
128 request->wdev = wdev;
129 request->wiphy = &rdev->wiphy;
130 request->scan_start = jiffies;
131
132 rdev->scan_req = request;
133
134 err = rdev_scan(rdev, request);
135 if (!err) {
136 wdev->conn->state = CFG80211_CONN_SCANNING;
137 nl80211_send_scan_start(rdev, wdev);
138 dev_hold(wdev->netdev);
139 } else {
140 rdev->scan_req = NULL;
141 kfree(request);
142 }
143 return err;
144 }
145
cfg80211_conn_do_work(struct wireless_dev * wdev,enum nl80211_timeout_reason * treason)146 static int cfg80211_conn_do_work(struct wireless_dev *wdev,
147 enum nl80211_timeout_reason *treason)
148 {
149 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
150 struct cfg80211_connect_params *params;
151 struct cfg80211_assoc_request req = {};
152 int err;
153
154 ASSERT_WDEV_LOCK(wdev);
155
156 if (!wdev->conn)
157 return 0;
158
159 params = &wdev->conn->params;
160
161 switch (wdev->conn->state) {
162 case CFG80211_CONN_SCANNING:
163 /* didn't find it during scan ... */
164 return -ENOENT;
165 case CFG80211_CONN_SCAN_AGAIN:
166 return cfg80211_conn_scan(wdev);
167 case CFG80211_CONN_AUTHENTICATE_NEXT:
168 if (WARN_ON(!rdev->ops->auth))
169 return -EOPNOTSUPP;
170 wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
171 return cfg80211_mlme_auth(rdev, wdev->netdev,
172 params->channel, params->auth_type,
173 params->bssid,
174 params->ssid, params->ssid_len,
175 NULL, 0,
176 params->key, params->key_len,
177 params->key_idx, NULL, 0);
178 case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
179 *treason = NL80211_TIMEOUT_AUTH;
180 return -ENOTCONN;
181 case CFG80211_CONN_ASSOCIATE_NEXT:
182 if (WARN_ON(!rdev->ops->assoc))
183 return -EOPNOTSUPP;
184 wdev->conn->state = CFG80211_CONN_ASSOCIATING;
185 if (wdev->conn->prev_bssid_valid)
186 req.prev_bssid = wdev->conn->prev_bssid;
187 req.ie = params->ie;
188 req.ie_len = params->ie_len;
189 req.use_mfp = params->mfp != NL80211_MFP_NO;
190 req.crypto = params->crypto;
191 req.flags = params->flags;
192 req.ht_capa = params->ht_capa;
193 req.ht_capa_mask = params->ht_capa_mask;
194 req.vht_capa = params->vht_capa;
195 req.vht_capa_mask = params->vht_capa_mask;
196
197 err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
198 params->bssid, params->ssid,
199 params->ssid_len, &req);
200 if (err)
201 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202 NULL, 0,
203 WLAN_REASON_DEAUTH_LEAVING,
204 false);
205 return err;
206 case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
207 *treason = NL80211_TIMEOUT_ASSOC;
208 fallthrough;
209 case CFG80211_CONN_ASSOC_FAILED:
210 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211 NULL, 0,
212 WLAN_REASON_DEAUTH_LEAVING, false);
213 return -ENOTCONN;
214 case CFG80211_CONN_DEAUTH:
215 cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
216 NULL, 0,
217 WLAN_REASON_DEAUTH_LEAVING, false);
218 fallthrough;
219 case CFG80211_CONN_ABANDON:
220 /* free directly, disconnected event already sent */
221 cfg80211_sme_free(wdev);
222 return 0;
223 default:
224 return 0;
225 }
226 }
227
cfg80211_conn_work(struct work_struct * work)228 void cfg80211_conn_work(struct work_struct *work)
229 {
230 struct cfg80211_registered_device *rdev =
231 container_of(work, struct cfg80211_registered_device, conn_work);
232 struct wireless_dev *wdev;
233 u8 bssid_buf[ETH_ALEN], *bssid = NULL;
234 enum nl80211_timeout_reason treason;
235
236 rtnl_lock();
237
238 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
239 if (!wdev->netdev)
240 continue;
241
242 wdev_lock(wdev);
243 if (!netif_running(wdev->netdev)) {
244 wdev_unlock(wdev);
245 continue;
246 }
247 if (!wdev->conn ||
248 wdev->conn->state == CFG80211_CONN_CONNECTED) {
249 wdev_unlock(wdev);
250 continue;
251 }
252 if (wdev->conn->params.bssid) {
253 memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
254 bssid = bssid_buf;
255 }
256 treason = NL80211_TIMEOUT_UNSPECIFIED;
257 if (cfg80211_conn_do_work(wdev, &treason)) {
258 struct cfg80211_connect_resp_params cr;
259
260 memset(&cr, 0, sizeof(cr));
261 cr.status = -1;
262 cr.bssid = bssid;
263 cr.timeout_reason = treason;
264 __cfg80211_connect_result(wdev->netdev, &cr, false);
265 }
266 wdev_unlock(wdev);
267 }
268
269 rtnl_unlock();
270 }
271
cfg80211_step_auth_next(struct cfg80211_conn * conn,struct cfg80211_bss * bss)272 static void cfg80211_step_auth_next(struct cfg80211_conn *conn,
273 struct cfg80211_bss *bss)
274 {
275 memcpy(conn->bssid, bss->bssid, ETH_ALEN);
276 conn->params.bssid = conn->bssid;
277 conn->params.channel = bss->channel;
278 conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
279 }
280
281 /* Returned bss is reference counted and must be cleaned up appropriately. */
cfg80211_get_conn_bss(struct wireless_dev * wdev)282 static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
283 {
284 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
285 struct cfg80211_bss *bss;
286
287 ASSERT_WDEV_LOCK(wdev);
288
289 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
290 wdev->conn->params.bssid,
291 wdev->conn->params.ssid,
292 wdev->conn->params.ssid_len,
293 wdev->conn_bss_type,
294 IEEE80211_PRIVACY(wdev->conn->params.privacy));
295 if (!bss)
296 return NULL;
297
298 cfg80211_step_auth_next(wdev->conn, bss);
299 schedule_work(&rdev->conn_work);
300
301 return bss;
302 }
303
__cfg80211_sme_scan_done(struct net_device * dev)304 static void __cfg80211_sme_scan_done(struct net_device *dev)
305 {
306 struct wireless_dev *wdev = dev->ieee80211_ptr;
307 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
308 struct cfg80211_bss *bss;
309
310 ASSERT_WDEV_LOCK(wdev);
311
312 if (!wdev->conn)
313 return;
314
315 if (wdev->conn->state != CFG80211_CONN_SCANNING &&
316 wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
317 return;
318
319 bss = cfg80211_get_conn_bss(wdev);
320 if (bss)
321 cfg80211_put_bss(&rdev->wiphy, bss);
322 else
323 schedule_work(&rdev->conn_work);
324 }
325
cfg80211_sme_scan_done(struct net_device * dev)326 void cfg80211_sme_scan_done(struct net_device *dev)
327 {
328 struct wireless_dev *wdev = dev->ieee80211_ptr;
329
330 wdev_lock(wdev);
331 __cfg80211_sme_scan_done(dev);
332 wdev_unlock(wdev);
333 }
334
cfg80211_sme_rx_auth(struct wireless_dev * wdev,const u8 * buf,size_t len)335 void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
336 {
337 struct wiphy *wiphy = wdev->wiphy;
338 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
339 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
340 u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
341
342 ASSERT_WDEV_LOCK(wdev);
343
344 if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
345 return;
346
347 if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
348 wdev->conn->auto_auth &&
349 wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
350 /* select automatically between only open, shared, leap */
351 switch (wdev->conn->params.auth_type) {
352 case NL80211_AUTHTYPE_OPEN_SYSTEM:
353 if (wdev->connect_keys)
354 wdev->conn->params.auth_type =
355 NL80211_AUTHTYPE_SHARED_KEY;
356 else
357 wdev->conn->params.auth_type =
358 NL80211_AUTHTYPE_NETWORK_EAP;
359 break;
360 case NL80211_AUTHTYPE_SHARED_KEY:
361 wdev->conn->params.auth_type =
362 NL80211_AUTHTYPE_NETWORK_EAP;
363 break;
364 default:
365 /* huh? */
366 wdev->conn->params.auth_type =
367 NL80211_AUTHTYPE_OPEN_SYSTEM;
368 break;
369 }
370 wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
371 schedule_work(&rdev->conn_work);
372 } else if (status_code != WLAN_STATUS_SUCCESS) {
373 struct cfg80211_connect_resp_params cr;
374
375 memset(&cr, 0, sizeof(cr));
376 cr.status = status_code;
377 cr.bssid = mgmt->bssid;
378 cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
379 __cfg80211_connect_result(wdev->netdev, &cr, false);
380 } else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
381 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
382 schedule_work(&rdev->conn_work);
383 }
384 }
385
cfg80211_sme_rx_assoc_resp(struct wireless_dev * wdev,u16 status)386 bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
387 {
388 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
389
390 if (!wdev->conn)
391 return false;
392
393 if (status == WLAN_STATUS_SUCCESS) {
394 wdev->conn->state = CFG80211_CONN_CONNECTED;
395 return false;
396 }
397
398 if (wdev->conn->prev_bssid_valid) {
399 /*
400 * Some stupid APs don't accept reassoc, so we
401 * need to fall back to trying regular assoc;
402 * return true so no event is sent to userspace.
403 */
404 wdev->conn->prev_bssid_valid = false;
405 wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
406 schedule_work(&rdev->conn_work);
407 return true;
408 }
409
410 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
411 schedule_work(&rdev->conn_work);
412 return false;
413 }
414
cfg80211_sme_deauth(struct wireless_dev * wdev)415 void cfg80211_sme_deauth(struct wireless_dev *wdev)
416 {
417 cfg80211_sme_free(wdev);
418 }
419
cfg80211_sme_auth_timeout(struct wireless_dev * wdev)420 void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
421 {
422 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
423
424 if (!wdev->conn)
425 return;
426
427 wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
428 schedule_work(&rdev->conn_work);
429 }
430
cfg80211_sme_disassoc(struct wireless_dev * wdev)431 void cfg80211_sme_disassoc(struct wireless_dev *wdev)
432 {
433 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
434
435 if (!wdev->conn)
436 return;
437
438 wdev->conn->state = CFG80211_CONN_DEAUTH;
439 schedule_work(&rdev->conn_work);
440 }
441
cfg80211_sme_assoc_timeout(struct wireless_dev * wdev)442 void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
443 {
444 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
445
446 if (!wdev->conn)
447 return;
448
449 wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
450 schedule_work(&rdev->conn_work);
451 }
452
cfg80211_sme_abandon_assoc(struct wireless_dev * wdev)453 void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
454 {
455 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
456
457 if (!wdev->conn)
458 return;
459
460 wdev->conn->state = CFG80211_CONN_ABANDON;
461 schedule_work(&rdev->conn_work);
462 }
463
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)464 static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
465 const u8 *ies, size_t ies_len,
466 const u8 **out_ies, size_t *out_ies_len)
467 {
468 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
469 u8 *buf;
470 size_t offs;
471
472 if (!rdev->wiphy.extended_capabilities_len ||
473 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
474 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
475 if (!*out_ies)
476 return -ENOMEM;
477 *out_ies_len = ies_len;
478 return 0;
479 }
480
481 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
482 GFP_KERNEL);
483 if (!buf)
484 return -ENOMEM;
485
486 if (ies_len) {
487 static const u8 before_extcapa[] = {
488 /* not listing IEs expected to be created by driver */
489 WLAN_EID_RSN,
490 WLAN_EID_QOS_CAPA,
491 WLAN_EID_RRM_ENABLED_CAPABILITIES,
492 WLAN_EID_MOBILITY_DOMAIN,
493 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
494 WLAN_EID_BSS_COEX_2040,
495 };
496
497 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
498 ARRAY_SIZE(before_extcapa), 0);
499 memcpy(buf, ies, offs);
500 /* leave a whole for extended capabilities IE */
501 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
502 ies + offs, ies_len - offs);
503 } else {
504 offs = 0;
505 }
506
507 /* place extended capabilities IE (with only driver capabilities) */
508 buf[offs] = WLAN_EID_EXT_CAPABILITY;
509 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
510 memcpy(buf + offs + 2,
511 rdev->wiphy.extended_capabilities,
512 rdev->wiphy.extended_capabilities_len);
513
514 *out_ies = buf;
515 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
516
517 return 0;
518 }
519
cfg80211_sme_connect(struct wireless_dev * wdev,struct cfg80211_connect_params * connect,const u8 * prev_bssid)520 static int cfg80211_sme_connect(struct wireless_dev *wdev,
521 struct cfg80211_connect_params *connect,
522 const u8 *prev_bssid)
523 {
524 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
525 struct cfg80211_bss *bss;
526 int err;
527
528 if (!rdev->ops->auth || !rdev->ops->assoc)
529 return -EOPNOTSUPP;
530
531 if (wdev->current_bss) {
532 cfg80211_unhold_bss(wdev->current_bss);
533 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
534 wdev->current_bss = NULL;
535
536 cfg80211_sme_free(wdev);
537 }
538
539 if (wdev->conn)
540 return -EINPROGRESS;
541
542 wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
543 if (!wdev->conn)
544 return -ENOMEM;
545
546 /*
547 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
548 */
549 memcpy(&wdev->conn->params, connect, sizeof(*connect));
550 if (connect->bssid) {
551 wdev->conn->params.bssid = wdev->conn->bssid;
552 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
553 }
554
555 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
556 &wdev->conn->ie,
557 &wdev->conn->params.ie_len)) {
558 kfree(wdev->conn);
559 wdev->conn = NULL;
560 return -ENOMEM;
561 }
562 wdev->conn->params.ie = wdev->conn->ie;
563
564 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
565 wdev->conn->auto_auth = true;
566 /* start with open system ... should mostly work */
567 wdev->conn->params.auth_type =
568 NL80211_AUTHTYPE_OPEN_SYSTEM;
569 } else {
570 wdev->conn->auto_auth = false;
571 }
572
573 wdev->conn->params.ssid = wdev->ssid;
574 wdev->conn->params.ssid_len = wdev->ssid_len;
575
576 /* see if we have the bss already */
577 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
578 wdev->conn->params.bssid,
579 wdev->conn->params.ssid,
580 wdev->conn->params.ssid_len,
581 wdev->conn_bss_type,
582 IEEE80211_PRIVACY(wdev->conn->params.privacy));
583
584 if (prev_bssid) {
585 memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
586 wdev->conn->prev_bssid_valid = true;
587 }
588
589 /* we're good if we have a matching bss struct */
590 if (bss) {
591 enum nl80211_timeout_reason treason;
592
593 cfg80211_step_auth_next(wdev->conn, bss);
594 err = cfg80211_conn_do_work(wdev, &treason);
595 cfg80211_put_bss(wdev->wiphy, bss);
596 } else {
597 /* otherwise we'll need to scan for the AP first */
598 err = cfg80211_conn_scan(wdev);
599
600 /*
601 * If we can't scan right now, then we need to scan again
602 * after the current scan finished, since the parameters
603 * changed (unless we find a good AP anyway).
604 */
605 if (err == -EBUSY) {
606 err = 0;
607 wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
608 }
609 }
610
611 if (err)
612 cfg80211_sme_free(wdev);
613
614 return err;
615 }
616
cfg80211_sme_disconnect(struct wireless_dev * wdev,u16 reason)617 static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
618 {
619 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
620 int err;
621
622 if (!wdev->conn)
623 return 0;
624
625 if (!rdev->ops->deauth)
626 return -EOPNOTSUPP;
627
628 if (wdev->conn->state == CFG80211_CONN_SCANNING ||
629 wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
630 err = 0;
631 goto out;
632 }
633
634 /* wdev->conn->params.bssid must be set if > SCANNING */
635 err = cfg80211_mlme_deauth(rdev, wdev->netdev,
636 wdev->conn->params.bssid,
637 NULL, 0, reason, false);
638 out:
639 cfg80211_sme_free(wdev);
640 return err;
641 }
642
643 /*
644 * code shared for in-device and software SME
645 */
646
cfg80211_is_all_idle(void)647 static bool cfg80211_is_all_idle(void)
648 {
649 struct cfg80211_registered_device *rdev;
650 struct wireless_dev *wdev;
651 bool is_all_idle = true;
652
653 /*
654 * All devices must be idle as otherwise if you are actively
655 * scanning some new beacon hints could be learned and would
656 * count as new regulatory hints.
657 * Also if there is any other active beaconing interface we
658 * need not issue a disconnect hint and reset any info such
659 * as chan dfs state, etc.
660 */
661 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
662 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
663 wdev_lock(wdev);
664 if (wdev->conn || wdev->current_bss ||
665 cfg80211_beaconing_iface_active(wdev))
666 is_all_idle = false;
667 wdev_unlock(wdev);
668 }
669 }
670
671 return is_all_idle;
672 }
673
disconnect_work(struct work_struct * work)674 static void disconnect_work(struct work_struct *work)
675 {
676 rtnl_lock();
677 if (cfg80211_is_all_idle())
678 regulatory_hint_disconnect();
679 rtnl_unlock();
680 }
681
682 DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
683
684
685 /*
686 * API calls for drivers implementing connect/disconnect and
687 * SME event handling
688 */
689
690 /* This method must consume bss one way or another */
__cfg80211_connect_result(struct net_device * dev,struct cfg80211_connect_resp_params * cr,bool wextev)691 void __cfg80211_connect_result(struct net_device *dev,
692 struct cfg80211_connect_resp_params *cr,
693 bool wextev)
694 {
695 struct wireless_dev *wdev = dev->ieee80211_ptr;
696 const u8 *country_ie;
697 #ifdef CONFIG_CFG80211_WEXT
698 union iwreq_data wrqu;
699 #endif
700
701 ASSERT_WDEV_LOCK(wdev);
702
703 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
704 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
705 cfg80211_put_bss(wdev->wiphy, cr->bss);
706 return;
707 }
708
709 wdev->unprot_beacon_reported = 0;
710 nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
711 GFP_KERNEL);
712
713 #ifdef CONFIG_CFG80211_WEXT
714 if (wextev) {
715 if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
716 memset(&wrqu, 0, sizeof(wrqu));
717 wrqu.data.length = cr->req_ie_len;
718 wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
719 cr->req_ie);
720 }
721
722 if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
723 memset(&wrqu, 0, sizeof(wrqu));
724 wrqu.data.length = cr->resp_ie_len;
725 wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
726 cr->resp_ie);
727 }
728
729 memset(&wrqu, 0, sizeof(wrqu));
730 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
731 if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
732 memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
733 memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
734 wdev->wext.prev_bssid_valid = true;
735 }
736 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
737 }
738 #endif
739
740 if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
741 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
742 cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
743 wdev->ssid, wdev->ssid_len,
744 wdev->conn_bss_type,
745 IEEE80211_PRIVACY_ANY);
746 if (cr->bss)
747 cfg80211_hold_bss(bss_from_pub(cr->bss));
748 }
749
750 if (wdev->current_bss) {
751 cfg80211_unhold_bss(wdev->current_bss);
752 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
753 wdev->current_bss = NULL;
754 }
755
756 if (cr->status != WLAN_STATUS_SUCCESS) {
757 kfree_sensitive(wdev->connect_keys);
758 wdev->connect_keys = NULL;
759 wdev->ssid_len = 0;
760 wdev->conn_owner_nlportid = 0;
761 if (cr->bss) {
762 cfg80211_unhold_bss(bss_from_pub(cr->bss));
763 cfg80211_put_bss(wdev->wiphy, cr->bss);
764 }
765 cfg80211_sme_free(wdev);
766 return;
767 }
768
769 if (WARN_ON(!cr->bss))
770 return;
771
772 wdev->current_bss = bss_from_pub(cr->bss);
773
774 if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
775 cfg80211_upload_connect_keys(wdev);
776
777 rcu_read_lock();
778 country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
779 if (!country_ie) {
780 rcu_read_unlock();
781 return;
782 }
783
784 country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
785 rcu_read_unlock();
786
787 if (!country_ie)
788 return;
789
790 /*
791 * ieee80211_bss_get_ie() ensures we can access:
792 * - country_ie + 2, the start of the country ie data, and
793 * - and country_ie[1] which is the IE length
794 */
795 regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
796 country_ie + 2, country_ie[1]);
797 kfree(country_ie);
798 }
799
800 /* Consumes bss object one way or another */
cfg80211_connect_done(struct net_device * dev,struct cfg80211_connect_resp_params * params,gfp_t gfp)801 void cfg80211_connect_done(struct net_device *dev,
802 struct cfg80211_connect_resp_params *params,
803 gfp_t gfp)
804 {
805 struct wireless_dev *wdev = dev->ieee80211_ptr;
806 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
807 struct cfg80211_event *ev;
808 unsigned long flags;
809 u8 *next;
810
811 if (params->bss) {
812 struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
813
814 if (list_empty(&ibss->list)) {
815 struct cfg80211_bss *found = NULL, *tmp = params->bss;
816
817 found = cfg80211_get_bss(wdev->wiphy, NULL,
818 params->bss->bssid,
819 wdev->ssid, wdev->ssid_len,
820 wdev->conn_bss_type,
821 IEEE80211_PRIVACY_ANY);
822 if (found) {
823 /* The same BSS is already updated so use it
824 * instead, as it has latest info.
825 */
826 params->bss = found;
827 } else {
828 /* Update with BSS provided by driver, it will
829 * be freshly added and ref cnted, we can free
830 * the old one.
831 *
832 * signal_valid can be false, as we are not
833 * expecting the BSS to be found.
834 *
835 * keep the old timestamp to avoid confusion
836 */
837 cfg80211_bss_update(rdev, ibss, false,
838 ibss->ts);
839 }
840
841 cfg80211_put_bss(wdev->wiphy, tmp);
842 }
843 }
844
845 ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
846 params->req_ie_len + params->resp_ie_len +
847 params->fils.kek_len + params->fils.pmk_len +
848 (params->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
849 if (!ev) {
850 cfg80211_put_bss(wdev->wiphy, params->bss);
851 return;
852 }
853
854 ev->type = EVENT_CONNECT_RESULT;
855 next = ((u8 *)ev) + sizeof(*ev);
856 if (params->bssid) {
857 ev->cr.bssid = next;
858 memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
859 next += ETH_ALEN;
860 }
861 if (params->req_ie_len) {
862 ev->cr.req_ie = next;
863 ev->cr.req_ie_len = params->req_ie_len;
864 memcpy((void *)ev->cr.req_ie, params->req_ie,
865 params->req_ie_len);
866 next += params->req_ie_len;
867 }
868 if (params->resp_ie_len) {
869 ev->cr.resp_ie = next;
870 ev->cr.resp_ie_len = params->resp_ie_len;
871 memcpy((void *)ev->cr.resp_ie, params->resp_ie,
872 params->resp_ie_len);
873 next += params->resp_ie_len;
874 }
875 if (params->fils.kek_len) {
876 ev->cr.fils.kek = next;
877 ev->cr.fils.kek_len = params->fils.kek_len;
878 memcpy((void *)ev->cr.fils.kek, params->fils.kek,
879 params->fils.kek_len);
880 next += params->fils.kek_len;
881 }
882 if (params->fils.pmk_len) {
883 ev->cr.fils.pmk = next;
884 ev->cr.fils.pmk_len = params->fils.pmk_len;
885 memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
886 params->fils.pmk_len);
887 next += params->fils.pmk_len;
888 }
889 if (params->fils.pmkid) {
890 ev->cr.fils.pmkid = next;
891 memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
892 WLAN_PMKID_LEN);
893 next += WLAN_PMKID_LEN;
894 }
895 ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
896 if (params->fils.update_erp_next_seq_num)
897 ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
898 if (params->bss)
899 cfg80211_hold_bss(bss_from_pub(params->bss));
900 ev->cr.bss = params->bss;
901 ev->cr.status = params->status;
902 ev->cr.timeout_reason = params->timeout_reason;
903
904 spin_lock_irqsave(&wdev->event_lock, flags);
905 list_add_tail(&ev->list, &wdev->event_list);
906 spin_unlock_irqrestore(&wdev->event_lock, flags);
907 queue_work(cfg80211_wq, &rdev->event_work);
908 }
909 EXPORT_SYMBOL(cfg80211_connect_done);
910
911 /* Consumes bss object one way or another */
__cfg80211_roamed(struct wireless_dev * wdev,struct cfg80211_roam_info * info)912 void __cfg80211_roamed(struct wireless_dev *wdev,
913 struct cfg80211_roam_info *info)
914 {
915 #ifdef CONFIG_CFG80211_WEXT
916 union iwreq_data wrqu;
917 #endif
918 ASSERT_WDEV_LOCK(wdev);
919
920 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
921 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
922 goto out;
923
924 if (WARN_ON(!wdev->current_bss))
925 goto out;
926
927 cfg80211_unhold_bss(wdev->current_bss);
928 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
929 wdev->current_bss = NULL;
930
931 if (WARN_ON(!info->bss))
932 return;
933
934 cfg80211_hold_bss(bss_from_pub(info->bss));
935 wdev->current_bss = bss_from_pub(info->bss);
936
937 wdev->unprot_beacon_reported = 0;
938 nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
939 wdev->netdev, info, GFP_KERNEL);
940
941 #ifdef CONFIG_CFG80211_WEXT
942 if (info->req_ie) {
943 memset(&wrqu, 0, sizeof(wrqu));
944 wrqu.data.length = info->req_ie_len;
945 wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
946 &wrqu, info->req_ie);
947 }
948
949 if (info->resp_ie) {
950 memset(&wrqu, 0, sizeof(wrqu));
951 wrqu.data.length = info->resp_ie_len;
952 wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
953 &wrqu, info->resp_ie);
954 }
955
956 memset(&wrqu, 0, sizeof(wrqu));
957 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
958 memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
959 memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
960 wdev->wext.prev_bssid_valid = true;
961 wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
962 #endif
963
964 return;
965 out:
966 cfg80211_put_bss(wdev->wiphy, info->bss);
967 }
968
969 /* Consumes info->bss object one way or another */
cfg80211_roamed(struct net_device * dev,struct cfg80211_roam_info * info,gfp_t gfp)970 void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
971 gfp_t gfp)
972 {
973 struct wireless_dev *wdev = dev->ieee80211_ptr;
974 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
975 struct cfg80211_event *ev;
976 unsigned long flags;
977 u8 *next;
978
979 if (!info->bss) {
980 info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
981 info->bssid, wdev->ssid,
982 wdev->ssid_len,
983 wdev->conn_bss_type,
984 IEEE80211_PRIVACY_ANY);
985 }
986
987 if (WARN_ON(!info->bss))
988 return;
989
990 ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
991 info->fils.kek_len + info->fils.pmk_len +
992 (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
993 if (!ev) {
994 cfg80211_put_bss(wdev->wiphy, info->bss);
995 return;
996 }
997
998 ev->type = EVENT_ROAMED;
999 next = ((u8 *)ev) + sizeof(*ev);
1000 if (info->req_ie_len) {
1001 ev->rm.req_ie = next;
1002 ev->rm.req_ie_len = info->req_ie_len;
1003 memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
1004 next += info->req_ie_len;
1005 }
1006 if (info->resp_ie_len) {
1007 ev->rm.resp_ie = next;
1008 ev->rm.resp_ie_len = info->resp_ie_len;
1009 memcpy((void *)ev->rm.resp_ie, info->resp_ie,
1010 info->resp_ie_len);
1011 next += info->resp_ie_len;
1012 }
1013 if (info->fils.kek_len) {
1014 ev->rm.fils.kek = next;
1015 ev->rm.fils.kek_len = info->fils.kek_len;
1016 memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1017 info->fils.kek_len);
1018 next += info->fils.kek_len;
1019 }
1020 if (info->fils.pmk_len) {
1021 ev->rm.fils.pmk = next;
1022 ev->rm.fils.pmk_len = info->fils.pmk_len;
1023 memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1024 info->fils.pmk_len);
1025 next += info->fils.pmk_len;
1026 }
1027 if (info->fils.pmkid) {
1028 ev->rm.fils.pmkid = next;
1029 memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1030 WLAN_PMKID_LEN);
1031 next += WLAN_PMKID_LEN;
1032 }
1033 ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1034 if (info->fils.update_erp_next_seq_num)
1035 ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
1036 ev->rm.bss = info->bss;
1037
1038 spin_lock_irqsave(&wdev->event_lock, flags);
1039 list_add_tail(&ev->list, &wdev->event_list);
1040 spin_unlock_irqrestore(&wdev->event_lock, flags);
1041 queue_work(cfg80211_wq, &rdev->event_work);
1042 }
1043 EXPORT_SYMBOL(cfg80211_roamed);
1044
__cfg80211_port_authorized(struct wireless_dev * wdev,const u8 * bssid)1045 void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1046 {
1047 ASSERT_WDEV_LOCK(wdev);
1048
1049 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1050 return;
1051
1052 if (WARN_ON(!wdev->current_bss) ||
1053 WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1054 return;
1055
1056 nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1057 bssid);
1058 }
1059
cfg80211_port_authorized(struct net_device * dev,const u8 * bssid,gfp_t gfp)1060 void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1061 gfp_t gfp)
1062 {
1063 struct wireless_dev *wdev = dev->ieee80211_ptr;
1064 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1065 struct cfg80211_event *ev;
1066 unsigned long flags;
1067
1068 if (WARN_ON(!bssid))
1069 return;
1070
1071 ev = kzalloc(sizeof(*ev), gfp);
1072 if (!ev)
1073 return;
1074
1075 ev->type = EVENT_PORT_AUTHORIZED;
1076 memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1077
1078 /*
1079 * Use the wdev event list so that if there are pending
1080 * connected/roamed events, they will be reported first.
1081 */
1082 spin_lock_irqsave(&wdev->event_lock, flags);
1083 list_add_tail(&ev->list, &wdev->event_list);
1084 spin_unlock_irqrestore(&wdev->event_lock, flags);
1085 queue_work(cfg80211_wq, &rdev->event_work);
1086 }
1087 EXPORT_SYMBOL(cfg80211_port_authorized);
1088
__cfg80211_disconnected(struct net_device * dev,const u8 * ie,size_t ie_len,u16 reason,bool from_ap)1089 void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1090 size_t ie_len, u16 reason, bool from_ap)
1091 {
1092 struct wireless_dev *wdev = dev->ieee80211_ptr;
1093 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1094 int i;
1095 #ifdef CONFIG_CFG80211_WEXT
1096 union iwreq_data wrqu;
1097 #endif
1098
1099 ASSERT_WDEV_LOCK(wdev);
1100
1101 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1102 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1103 return;
1104
1105 if (wdev->current_bss) {
1106 cfg80211_unhold_bss(wdev->current_bss);
1107 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1108 }
1109
1110 wdev->current_bss = NULL;
1111 wdev->ssid_len = 0;
1112 wdev->conn_owner_nlportid = 0;
1113 kfree_sensitive(wdev->connect_keys);
1114 wdev->connect_keys = NULL;
1115
1116 nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1117
1118 /* stop critical protocol if supported */
1119 if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1120 rdev->crit_proto_nlportid = 0;
1121 rdev_crit_proto_stop(rdev, wdev);
1122 }
1123
1124 /*
1125 * Delete all the keys ... pairwise keys can't really
1126 * exist any more anyway, but default keys might.
1127 */
1128 if (rdev->ops->del_key) {
1129 int max_key_idx = 5;
1130
1131 if (wiphy_ext_feature_isset(
1132 wdev->wiphy,
1133 NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1134 wiphy_ext_feature_isset(
1135 wdev->wiphy,
1136 NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
1137 max_key_idx = 7;
1138 for (i = 0; i <= max_key_idx; i++)
1139 rdev_del_key(rdev, dev, i, false, NULL);
1140 }
1141
1142 rdev_set_qos_map(rdev, dev, NULL);
1143
1144 #ifdef CONFIG_CFG80211_WEXT
1145 memset(&wrqu, 0, sizeof(wrqu));
1146 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1147 wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1148 wdev->wext.connect.ssid_len = 0;
1149 #endif
1150
1151 schedule_work(&cfg80211_disconnect_work);
1152 }
1153
cfg80211_disconnected(struct net_device * dev,u16 reason,const u8 * ie,size_t ie_len,bool locally_generated,gfp_t gfp)1154 void cfg80211_disconnected(struct net_device *dev, u16 reason,
1155 const u8 *ie, size_t ie_len,
1156 bool locally_generated, gfp_t gfp)
1157 {
1158 struct wireless_dev *wdev = dev->ieee80211_ptr;
1159 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1160 struct cfg80211_event *ev;
1161 unsigned long flags;
1162
1163 ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1164 if (!ev)
1165 return;
1166
1167 ev->type = EVENT_DISCONNECTED;
1168 ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1169 ev->dc.ie_len = ie_len;
1170 memcpy((void *)ev->dc.ie, ie, ie_len);
1171 ev->dc.reason = reason;
1172 ev->dc.locally_generated = locally_generated;
1173
1174 spin_lock_irqsave(&wdev->event_lock, flags);
1175 list_add_tail(&ev->list, &wdev->event_list);
1176 spin_unlock_irqrestore(&wdev->event_lock, flags);
1177 queue_work(cfg80211_wq, &rdev->event_work);
1178 }
1179 EXPORT_SYMBOL(cfg80211_disconnected);
1180
1181 /*
1182 * API calls for nl80211/wext compatibility code
1183 */
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)1184 int cfg80211_connect(struct cfg80211_registered_device *rdev,
1185 struct net_device *dev,
1186 struct cfg80211_connect_params *connect,
1187 struct cfg80211_cached_keys *connkeys,
1188 const u8 *prev_bssid)
1189 {
1190 struct wireless_dev *wdev = dev->ieee80211_ptr;
1191 int err;
1192
1193 ASSERT_WDEV_LOCK(wdev);
1194
1195 /*
1196 * If we have an ssid_len, we're trying to connect or are
1197 * already connected, so reject a new SSID unless it's the
1198 * same (which is the case for re-association.)
1199 */
1200 if (wdev->ssid_len &&
1201 (wdev->ssid_len != connect->ssid_len ||
1202 memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
1203 return -EALREADY;
1204
1205 /*
1206 * If connected, reject (re-)association unless prev_bssid
1207 * matches the current BSSID.
1208 */
1209 if (wdev->current_bss) {
1210 if (!prev_bssid)
1211 return -EALREADY;
1212 if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
1213 return -ENOTCONN;
1214 }
1215
1216 /*
1217 * Reject if we're in the process of connecting with WEP,
1218 * this case isn't very interesting and trying to handle
1219 * it would make the code much more complex.
1220 */
1221 if (wdev->connect_keys)
1222 return -EINPROGRESS;
1223
1224 cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1225 rdev->wiphy.ht_capa_mod_mask);
1226 cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1227 rdev->wiphy.vht_capa_mod_mask);
1228
1229 if (connkeys && connkeys->def >= 0) {
1230 int idx;
1231 u32 cipher;
1232
1233 idx = connkeys->def;
1234 cipher = connkeys->params[idx].cipher;
1235 /* If given a WEP key we may need it for shared key auth */
1236 if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1237 cipher == WLAN_CIPHER_SUITE_WEP104) {
1238 connect->key_idx = idx;
1239 connect->key = connkeys->params[idx].key;
1240 connect->key_len = connkeys->params[idx].key_len;
1241
1242 /*
1243 * If ciphers are not set (e.g. when going through
1244 * iwconfig), we have to set them appropriately here.
1245 */
1246 if (connect->crypto.cipher_group == 0)
1247 connect->crypto.cipher_group = cipher;
1248
1249 if (connect->crypto.n_ciphers_pairwise == 0) {
1250 connect->crypto.n_ciphers_pairwise = 1;
1251 connect->crypto.ciphers_pairwise[0] = cipher;
1252 }
1253 }
1254
1255 connect->crypto.wep_keys = connkeys->params;
1256 connect->crypto.wep_tx_key = connkeys->def;
1257 } else {
1258 if (WARN_ON(connkeys))
1259 return -EINVAL;
1260
1261 /* connect can point to wdev->wext.connect which
1262 * can hold key data from a previous connection
1263 */
1264 connect->key = NULL;
1265 connect->key_len = 0;
1266 connect->key_idx = 0;
1267 }
1268
1269 wdev->connect_keys = connkeys;
1270 memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
1271 wdev->ssid_len = connect->ssid_len;
1272
1273 wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1274 IEEE80211_BSS_TYPE_ESS;
1275
1276 if (!rdev->ops->connect)
1277 err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1278 else
1279 err = rdev_connect(rdev, dev, connect);
1280
1281 if (err) {
1282 wdev->connect_keys = NULL;
1283 /*
1284 * This could be reassoc getting refused, don't clear
1285 * ssid_len in that case.
1286 */
1287 if (!wdev->current_bss)
1288 wdev->ssid_len = 0;
1289 return err;
1290 }
1291
1292 return 0;
1293 }
1294
cfg80211_disconnect(struct cfg80211_registered_device * rdev,struct net_device * dev,u16 reason,bool wextev)1295 int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1296 struct net_device *dev, u16 reason, bool wextev)
1297 {
1298 struct wireless_dev *wdev = dev->ieee80211_ptr;
1299 int err = 0;
1300
1301 ASSERT_WDEV_LOCK(wdev);
1302
1303 kfree_sensitive(wdev->connect_keys);
1304 wdev->connect_keys = NULL;
1305
1306 wdev->conn_owner_nlportid = 0;
1307
1308 if (wdev->conn)
1309 err = cfg80211_sme_disconnect(wdev, reason);
1310 else if (!rdev->ops->disconnect)
1311 cfg80211_mlme_down(rdev, dev);
1312 else if (wdev->ssid_len)
1313 err = rdev_disconnect(rdev, dev, reason);
1314
1315 /*
1316 * Clear ssid_len unless we actually were fully connected,
1317 * in which case cfg80211_disconnected() will take care of
1318 * this later.
1319 */
1320 if (!wdev->current_bss)
1321 wdev->ssid_len = 0;
1322
1323 return err;
1324 }
1325
1326 /*
1327 * Used to clean up after the connection / connection attempt owner socket
1328 * disconnects
1329 */
cfg80211_autodisconnect_wk(struct work_struct * work)1330 void cfg80211_autodisconnect_wk(struct work_struct *work)
1331 {
1332 struct wireless_dev *wdev =
1333 container_of(work, struct wireless_dev, disconnect_wk);
1334 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1335
1336 wdev_lock(wdev);
1337
1338 if (wdev->conn_owner_nlportid) {
1339 switch (wdev->iftype) {
1340 case NL80211_IFTYPE_ADHOC:
1341 __cfg80211_leave_ibss(rdev, wdev->netdev, false);
1342 break;
1343 case NL80211_IFTYPE_AP:
1344 case NL80211_IFTYPE_P2P_GO:
1345 __cfg80211_stop_ap(rdev, wdev->netdev, false);
1346 break;
1347 case NL80211_IFTYPE_MESH_POINT:
1348 __cfg80211_leave_mesh(rdev, wdev->netdev);
1349 break;
1350 case NL80211_IFTYPE_STATION:
1351 case NL80211_IFTYPE_P2P_CLIENT:
1352 /*
1353 * Use disconnect_bssid if still connecting and
1354 * ops->disconnect not implemented. Otherwise we can
1355 * use cfg80211_disconnect.
1356 */
1357 if (rdev->ops->disconnect || wdev->current_bss)
1358 cfg80211_disconnect(rdev, wdev->netdev,
1359 WLAN_REASON_DEAUTH_LEAVING,
1360 true);
1361 else
1362 cfg80211_mlme_deauth(rdev, wdev->netdev,
1363 wdev->disconnect_bssid,
1364 NULL, 0,
1365 WLAN_REASON_DEAUTH_LEAVING,
1366 false);
1367 break;
1368 default:
1369 break;
1370 }
1371 }
1372
1373 wdev_unlock(wdev);
1374 }
1375