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