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