summaryrefslogtreecommitdiffstats
path: root/nexus/Supplicant.cpp
blob: 6aa36e87590c98f846482d2522c218a3603d340c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>

#define LOG_TAG "Supplicant"
#include <cutils/log.h>
#include <cutils/properties.h>

#include "private/android_filesystem_config.h"

#include <sysutils/ServiceManager.h>

#include "Supplicant.h"
#include "SupplicantListener.h"
#include "NetworkManager.h"
#include "WifiController.h"
#include "SupplicantStatus.h"

#include "libwpa_client/wpa_ctrl.h"

#define IFACE_DIR        "/data/system/wpa_supplicant"
#define DRIVER_PROP_NAME "wlan.driver.status"
#define SUPPLICANT_SERVICE_NAME  "wpa_supplicant"
#define SUPP_CONFIG_TEMPLATE "/system/etc/wifi/wpa_supplicant.conf"
#define SUPP_CONFIG_FILE "/data/misc/wifi/wpa_supplicant.conf"

Supplicant::Supplicant(WifiController *wc, ISupplicantEventHandler *handlers) {
    mHandlers = handlers;
    mController = wc;
    mInterfaceName = NULL;
    mCtrl = NULL;
    mMonitor = NULL;
    mListener = NULL;
   
    mServiceManager = new ServiceManager();

    mNetworks = new WifiNetworkCollection();
    pthread_mutex_init(&mNetworksLock, NULL);
}

Supplicant::~Supplicant() {
    delete mServiceManager;
    if (mInterfaceName)
        free(mInterfaceName);
}

int Supplicant::start() {

    if (setupConfig()) {
        LOGW("Unable to setup supplicant.conf");
    }

    if (mServiceManager->start(SUPPLICANT_SERVICE_NAME)) {
        LOGE("Error starting supplicant (%s)", strerror(errno));
        return -1;
    }

    wpa_ctrl_cleanup();
    if (connectToSupplicant()) {
        LOGE("Error connecting to supplicant (%s)\n", strerror(errno));
        return -1;
    }
    
    if (retrieveInterfaceName()) {
        LOGE("Error retrieving interface name (%s)\n", strerror(errno));
        return -1;
    }

    return 0;
}

int Supplicant::stop() {

    if (mListener->stopListener()) {
        LOGW("Unable to stop supplicant listener (%s)", strerror(errno));
        return -1;
    }

    if (mServiceManager->stop(SUPPLICANT_SERVICE_NAME)) {
        LOGW("Error stopping supplicant (%s)", strerror(errno));
    }

    if (mCtrl) {
        wpa_ctrl_close(mCtrl);
        mCtrl = NULL;
    }
    if (mMonitor) {
        wpa_ctrl_close(mMonitor);
        mMonitor = NULL;
    }

    return 0;
}

bool Supplicant::isStarted() {
    return mServiceManager->isRunning(SUPPLICANT_SERVICE_NAME);
}

int Supplicant::sendCommand(const char *cmd, char *reply, size_t *reply_len) {

    if (!mCtrl) {
        errno = ENOTCONN;
        return -1;
    }

//    LOGD("sendCommand(): -> '%s'", cmd);

    int rc;
    memset(reply, 0, *reply_len);
    if ((rc = wpa_ctrl_request(mCtrl, cmd, strlen(cmd), reply, reply_len, NULL)) == -2)  {
        errno = ETIMEDOUT;
        return -1;
    } else if (rc < 0 || !strncmp(reply, "FAIL", 4)) {
        strcpy(reply, "FAIL");
        errno = EIO;
        return -1;
    }

 //   LOGD("sendCommand(): <- '%s'", reply);
    return 0;
}
SupplicantStatus *Supplicant::getStatus() {
    char *reply;
    size_t len = 4096;

    if (!(reply = (char *) malloc(len))) {
        errno = ENOMEM;
        return NULL;
    }

    if (sendCommand("STATUS", reply, &len)) {
        free(reply);
        return NULL;
    }

    SupplicantStatus *ss = SupplicantStatus::createStatus(reply, len);
  
    free (reply);
    return ss;
}

/*
 * Retrieves the list of networks from Supplicant
 * and merge them into our current list
 */
int Supplicant::refreshNetworkList() {
    char *reply;
    size_t len = 4096;

    if (!(reply = (char *) malloc(len))) {
        errno = ENOMEM;
        return -1;
    }

    if (sendCommand("LIST_NETWORKS", reply, &len)) {
        free(reply);
        return -1;
    }

    char *linep;
    char *linep_next = NULL;

    if (!strtok_r(reply, "\n", &linep_next)) {
        LOGW("Malformatted network list\n");
        free(reply);
        errno = EIO;
        return -1;
    }

    PropertyManager *pm = NetworkManager::Instance()->getPropMngr();
    pthread_mutex_lock(&mNetworksLock);

    int num_added = 0;
    int num_refreshed = 0;
    int num_removed = 0;
    while((linep = strtok_r(NULL, "\n", &linep_next))) {
        // TODO: Move the decode into a static method so we
        // don't create new_wn when we don't have to.
        WifiNetwork *new_wn = new WifiNetwork(mController, this, linep);
        WifiNetwork *merge_wn;

        if ((merge_wn = this->lookupNetwork_UNLOCKED(new_wn->getNetworkId()))) {
            num_refreshed++;
            if (merge_wn->refresh()) {
                LOGW("Error refreshing network %d (%s)",
                     merge_wn->getNetworkId(), strerror(errno));
                }
            delete new_wn;
        } else {
            num_added++;
            char new_ns[20];
            snprintf(new_ns, sizeof(new_ns), "wifi.net.%d", new_wn->getNetworkId());
            new_wn->attachProperties(pm, new_ns);
            mNetworks->push_back(new_wn);
            if (new_wn->refresh()) {
                LOGW("Unable to refresh network id %d (%s)",
                    new_wn->getNetworkId(), strerror(errno));
            }
        }
    }

    if (!mNetworks->empty()) {
        // TODO: Add support for detecting removed networks
        WifiNetworkCollection::iterator i;

        for (i = mNetworks->begin(); i != mNetworks->end(); ++i) {
            if (0) {
                num_removed++;
                char del_ns[20];
                snprintf(del_ns, sizeof(del_ns), "wifi.net.%d", (*i)->getNetworkId());
                (*i)->detachProperties(pm, del_ns);
                delete (*i);
                i = mNetworks->erase(i);
            }
        }
    }


    LOGD("Networks added %d, refreshed %d, removed %d\n", 
         num_added, num_refreshed, num_removed);
    pthread_mutex_unlock(&mNetworksLock);

    free(reply);
    return 0;
}

int Supplicant::connectToSupplicant() {
    if (!isStarted())
        LOGW("Supplicant service not running");

    mCtrl = wpa_ctrl_open("tiwlan0"); // XXX:
    if (mCtrl == NULL) {
        LOGE("Unable to open connection to supplicant on \"%s\": %s",
             "tiwlan0", strerror(errno));
        return -1;
    }
    mMonitor = wpa_ctrl_open("tiwlan0");
    if (mMonitor == NULL) {
        wpa_ctrl_close(mCtrl);
        mCtrl = NULL;
        return -1;
    }
    if (wpa_ctrl_attach(mMonitor) != 0) {
        wpa_ctrl_close(mMonitor);
        wpa_ctrl_close(mCtrl);
        mCtrl = mMonitor = NULL;
        return -1;
    }

    mListener = new SupplicantListener(mHandlers, mMonitor);

    if (mListener->startListener()) {
        LOGE("Error - unable to start supplicant listener");
        stop();
        return -1;
    }
    return 0;
}

int Supplicant::setScanMode(bool active) {
    char reply[255];
    size_t len = sizeof(reply);

    if (sendCommand((active ? "DRIVER SCAN-ACTIVE" : "DRIVER SCAN-PASSIVE"),
                     reply, &len)) {
        LOGW("triggerScan(%d): Error setting scan mode (%s)", active,
             strerror(errno));
        return -1;
    }
    return 0;
}

int Supplicant::triggerScan() {
    char reply[255];
    size_t len = sizeof(reply);

    if (sendCommand("SCAN", reply, &len)) {
        LOGW("triggerScan(): Error initiating scan");
        return -1;
    }
    return 0;
}

int Supplicant::getRssi(int *buffer) {
    char reply[64];
    size_t len = sizeof(reply);

    if (sendCommand("DRIVER RSSI", reply, &len)) {
        LOGW("Failed to get RSSI (%s)", strerror(errno));
        return -1;
    }

    char *next = reply;
    char *s;
    for (int i = 0; i < 3; i++) {
        if (!(s = strsep(&next, " "))) {
            LOGE("Error parsing RSSI");
            errno = EIO;
            return -1;
        }
    }
    *buffer = atoi(s);
    return 0;
}

int Supplicant::getLinkSpeed() {
    char reply[64];
    size_t len = sizeof(reply);

    if (sendCommand("DRIVER LINKSPEED", reply, &len)) {
        LOGW("Failed to get LINKSPEED (%s)", strerror(errno));
        return -1;
    }

    char *next = reply;
    char *s;

    if (!(s = strsep(&next, " "))) {
        LOGE("Error parsing LINKSPEED");
        errno = EIO;
        return -1;
    }

    if (!(s = strsep(&next, " "))) {
        LOGE("Error parsing LINKSPEED");
        errno = EIO;
        return -1;
    }
    return atoi(s);
}

int Supplicant::stopDriver() {
    char reply[64];
    size_t len = sizeof(reply);

    LOGD("stopDriver()");

    if (sendCommand("DRIVER STOP", reply, &len)) {
        LOGW("Failed to stop driver (%s)", strerror(errno));
        return -1;
    }
    return 0;
}

int Supplicant::startDriver() {
    char reply[64];
    size_t len = sizeof(reply);

    LOGD("startDriver()");
    if (sendCommand("DRIVER START", reply, &len)) {
        LOGW("Failed to start driver (%s)", strerror(errno));
        return -1;
    }
    return 0;
}

WifiNetwork *Supplicant::createNetwork() {
    char reply[255];
    size_t len = sizeof(reply) -1;

    if (sendCommand("ADD_NETWORK", reply, &len))
        return NULL;

    if (reply[strlen(reply) -1] == '\n')
        reply[strlen(reply) -1] = '\0';

    WifiNetwork *wn = new WifiNetwork(mController, this, atoi(reply));
    PropertyManager *pm = NetworkManager::Instance()->getPropMngr();
    pthread_mutex_lock(&mNetworksLock);
    char new_ns[20];
    snprintf(new_ns, sizeof(new_ns), "wifi.net.%d", wn->getNetworkId());
    wn->attachProperties(pm, new_ns);
    mNetworks->push_back(wn);
    pthread_mutex_unlock(&mNetworksLock);
    return wn;
}

int Supplicant::removeNetwork(WifiNetwork *wn) {
    char req[64];

    sprintf(req, "REMOVE_NETWORK %d", wn->getNetworkId());
    char reply[32];
    size_t len = sizeof(reply) -1;

    if (sendCommand(req, reply, &len))
        return -1;

    pthread_mutex_lock(&mNetworksLock);
    WifiNetworkCollection::iterator it;
    for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
        if ((*it) == wn) {
            mNetworks->erase(it);
            break;
        }
    }
    pthread_mutex_unlock(&mNetworksLock);
    return 0;
}

WifiNetwork *Supplicant::lookupNetwork(int networkId) {
    pthread_mutex_lock(&mNetworksLock);
    WifiNetwork *wn = lookupNetwork_UNLOCKED(networkId);
    pthread_mutex_unlock(&mNetworksLock);
    return wn;
}

WifiNetwork *Supplicant::lookupNetwork_UNLOCKED(int networkId) {
    WifiNetworkCollection::iterator it;
    for (it = mNetworks->begin(); it != mNetworks->end(); ++it) {
        if ((*it)->getNetworkId() == networkId) {
            return *it;
        }
    }
    errno = ENOENT;
    return NULL;
}

WifiNetworkCollection *Supplicant::createNetworkList() {
    WifiNetworkCollection *d = new WifiNetworkCollection();
    WifiNetworkCollection::iterator i;

    pthread_mutex_lock(&mNetworksLock);
    for (i = mNetworks->begin(); i != mNetworks->end(); ++i)
        d->push_back((*i)->clone());

    pthread_mutex_unlock(&mNetworksLock);
    return d;
}

int Supplicant::setupConfig() {
    char buf[2048];
    int srcfd, destfd;
    int nread;

    if (access(SUPP_CONFIG_FILE, R_OK|W_OK) == 0) {
        return 0;
    } else if (errno != ENOENT) {
        LOGE("Cannot access \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
        return -1;
    }

    srcfd = open(SUPP_CONFIG_TEMPLATE, O_RDONLY);
    if (srcfd < 0) {
        LOGE("Cannot open \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
        return -1;
    }

    destfd = open(SUPP_CONFIG_FILE, O_CREAT|O_WRONLY, 0660);
    if (destfd < 0) {
        close(srcfd);
        LOGE("Cannot create \"%s\": %s", SUPP_CONFIG_FILE, strerror(errno));
        return -1;
    }

    while ((nread = read(srcfd, buf, sizeof(buf))) != 0) {
        if (nread < 0) {
            LOGE("Error reading \"%s\": %s", SUPP_CONFIG_TEMPLATE, strerror(errno));
            close(srcfd);
            close(destfd);
            unlink(SUPP_CONFIG_FILE);
            return -1;
        }
        write(destfd, buf, nread);
    }

    close(destfd);
    close(srcfd);

    if (chown(SUPP_CONFIG_FILE, AID_SYSTEM, AID_WIFI) < 0) {
        LOGE("Error changing group ownership of %s to %d: %s",
             SUPP_CONFIG_FILE, AID_WIFI, strerror(errno));
        unlink(SUPP_CONFIG_FILE);
        return -1;
    }
    return 0;
}

int Supplicant::setNetworkVar(int networkId, const char *var, const char *val) {
    char reply[255];
    size_t len = sizeof(reply) -1;

    LOGD("netid %d, var '%s' = '%s'", networkId, var, val);
    char *tmp;
    asprintf(&tmp, "SET_NETWORK %d %s %s", networkId, var, val);
    if (sendCommand(tmp, reply, &len)) {
        free(tmp);
        return -1;
    }
    free(tmp);

    len = sizeof(reply) -1;
    if (sendCommand("SAVE_CONFIG", reply, &len)) {
        LOGE("Error saving config after %s = %s", var, val);
        return -1;
    }
    return 0;
}

const char *Supplicant::getNetworkVar(int networkId, const char *var,
                                      char *buffer, size_t max) {
    size_t len = max - 1;
    char *tmp;

    asprintf(&tmp, "GET_NETWORK %d %s", networkId, var);
    if (sendCommand(tmp, buffer, &len)) {
        free(tmp);
        return NULL;
    }
    free(tmp);
    return buffer;
}

int Supplicant::enableNetwork(int networkId, bool enabled) {
    char req[64];

    if (enabled)
        sprintf(req, "ENABLE_NETWORK %d", networkId);
    else
        sprintf(req, "DISABLE_NETWORK %d", networkId);

    char reply[16];
    size_t len = sizeof(reply) -1;

    if (sendCommand(req, reply, &len))
        return -1;
    return 0;
}

int Supplicant::enablePacketFilter() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    for (i = 0; i <=3; i++) {
        snprintf(req, sizeof(req), "DRIVER RXFILTER-ADD %d", i);
        len = sizeof(reply);
        if (sendCommand(req, reply, &len))
            return -1;
    }

    len = sizeof(reply);
    if (sendCommand("DRIVER RXFILTER-START", reply, &len))
        return -1;
    return 0;
}
  
int Supplicant::disablePacketFilter() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    len = sizeof(reply);
    if (sendCommand("DRIVER RXFILTER-STOP", reply, &len))
        return -1;

    for (i = 3; i >=0; i--) {
        snprintf(req, sizeof(req), "DRIVER RXFILTER-REMOVE %d", i);
        len = sizeof(reply);
        if (sendCommand(req, reply, &len))
            return -1;
    }
    return 0;
}

int Supplicant::enableBluetoothCoexistenceScan() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    len = sizeof(reply);
    if (sendCommand("DRIVER BTCOEXSCAN-START", reply, &len))
        return -1;
    return 0;
}

int Supplicant::disableBluetoothCoexistenceScan() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    len = sizeof(reply);
    if (sendCommand("DRIVER BTCOEXSCAN-STOP", reply, &len))
        return -1;
    return 0;
}

int Supplicant::setBluetoothCoexistenceMode(int mode) {
    char req[64];

    sprintf(req, "DRIVER BTCOEXMODE %d", mode);

    char reply[16];
    size_t len = sizeof(reply) -1;

    if (sendCommand(req, reply, &len))
        return -1;
    return 0;
}

int Supplicant::setApScanMode(int mode) {
    char req[64];

//    LOGD("setApScanMode(%d)", mode);
    sprintf(req, "AP_SCAN %d", mode);

    char reply[16];
    size_t len = sizeof(reply) -1;

    if (sendCommand(req, reply, &len))
        return -1;
    return 0;
}


int Supplicant::retrieveInterfaceName() {
    char reply[255];
    size_t len = sizeof(reply) -1;

    if (sendCommand("INTERFACES", reply, &len))
        return -1;

    reply[strlen(reply)-1] = '\0';
    mInterfaceName = strdup(reply);
    return 0;
}

int Supplicant::reconnect() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    len = sizeof(reply);
    if (sendCommand("RECONNECT", reply, &len))
        return -1;
    return 0;
}

int Supplicant::disconnect() {
    char req[128];
    char reply[16];
    size_t len;
    int i;
    
    len = sizeof(reply);
    if (sendCommand("DISCONNECT", reply, &len))
        return -1;
    return 0;
}

int Supplicant::getNetworkCount() {
    pthread_mutex_lock(&mNetworksLock);
    int cnt = mNetworks->size();
    pthread_mutex_unlock(&mNetworksLock);
    return cnt;
}