~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/wireless/sme.c

Version: ~ [ linux-5.10-rc6 ] ~ [ linux-5.9.12 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.81 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.161 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.210 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.247 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.247 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp