|
@@ -10,17 +10,21 @@
|
|
|
* wifi connectfile <path> join using an SSID/passphrase/BSSID record
|
|
* wifi connectfile <path> join using an SSID/passphrase/BSSID record
|
|
|
* staged by the network menu (SYS_SPAWN's
|
|
* staged by the network menu (SYS_SPAWN's
|
|
|
* argument buffer is only 64 bytes)
|
|
* argument buffer is only 64 bytes)
|
|
|
- * wifi autoconnect reconnect from /data/WIFI.CFG (boot helper)
|
|
|
|
|
- * wifi forget remove the saved credentials
|
|
|
|
|
|
|
+ * wifi saved list the networks saved in /data/WIFI.CFG
|
|
|
|
|
+ * wifi autoconnect join any saved network found (boot helper)
|
|
|
|
|
+ * wifi forget [ssid] remove one saved network, or all without an
|
|
|
|
|
+ * argument
|
|
|
* wifi disconnect
|
|
* wifi disconnect
|
|
|
*
|
|
*
|
|
|
- * The WPA2 handshake (wpa.c) runs here; a small background copy of this
|
|
|
|
|
- * command (`wifi __keep <state>`) stays alive after `connect` to answer
|
|
|
|
|
- * group-key rekeys, and exits when the connection changes. */
|
|
|
|
|
|
|
+ * A successful connect adds the SSID/passphrase to a most-recently-used list
|
|
|
|
|
+ * (up to WIFI_CFG_MAX networks) in /data/WIFI.CFG. The WPA2 handshake (wpa.c)
|
|
|
|
|
+ * runs here; a small background copy of this command (`wifi __keep <state>`)
|
|
|
|
|
+ * stays alive after `connect` to answer group-key rekeys, and exits when the
|
|
|
|
|
+ * connection changes. */
|
|
|
|
|
|
|
|
#include "ucmd_lib.h"
|
|
#include "ucmd_lib.h"
|
|
|
#include "tls_rng.h"
|
|
#include "tls_rng.h"
|
|
|
-#include "wifi_cfg.h"
|
|
|
|
|
|
|
+#include "wifi_store.h"
|
|
|
#include "wpa.h"
|
|
#include "wpa.h"
|
|
|
|
|
|
|
|
#define MAX_BSS 32
|
|
#define MAX_BSS 32
|
|
@@ -43,34 +47,15 @@ static int name_eq(const char *a, const char *b)
|
|
|
return *a == *b;
|
|
return *a == *b;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int bssid_zero(const unsigned char b[6])
|
|
|
|
|
|
|
+/* Add the network to /data/WIFI.CFG, replacing an older record for the same
|
|
|
|
|
+ * SSID and moving it to the front (most recently used). The BSSID is
|
|
|
|
|
+ * deliberately left zero so a mesh/repeater AP with the same SSID still
|
|
|
|
|
+ * works; /data is a plain FAT partition, so the file is 8.3 and the
|
|
|
|
|
+ * passphrase is stored in clear. */
|
|
|
|
|
+static int remember_wifi(const unsigned char *ssid, unsigned int ssid_len,
|
|
|
|
|
+ const char *pass, unsigned int pass_len)
|
|
|
{
|
|
{
|
|
|
- unsigned int i;
|
|
|
|
|
-
|
|
|
|
|
- for (i = 0; i < 6; i++)
|
|
|
|
|
- if (b[i])
|
|
|
|
|
- return 0;
|
|
|
|
|
- return 1;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-static void wipe_cfg(struct wifi_cfg *cfg)
|
|
|
|
|
-{
|
|
|
|
|
- unsigned int i;
|
|
|
|
|
-
|
|
|
|
|
- for (i = 0; i < sizeof(*cfg); i++)
|
|
|
|
|
- ((volatile unsigned char *)cfg)[i] = 0;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Save the network for the next boot. The BSSID is deliberately left zero
|
|
|
|
|
- * so a mesh/repeater AP with the same SSID still works; /data is a plain FAT
|
|
|
|
|
- * partition, so WIFI.CFG is 8.3 and the password is stored in clear. */
|
|
|
|
|
-static int save_wifi_cred(const unsigned char *ssid, unsigned int ssid_len,
|
|
|
|
|
- const char *pass, unsigned int pass_len)
|
|
|
|
|
-{
|
|
|
|
|
- /* FAT writes may read a final partial sector past the source length;
|
|
|
|
|
- * keep slack after the record as well. */
|
|
|
|
|
- unsigned char buf[sizeof(struct wifi_cfg) + 512];
|
|
|
|
|
- struct wifi_cfg *cfg = (struct wifi_cfg *)buf;
|
|
|
|
|
|
|
+ struct wifi_cfg rec;
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
|
int r;
|
|
int r;
|
|
|
|
|
|
|
@@ -78,51 +63,21 @@ static int save_wifi_cred(const unsigned char *ssid, unsigned int ssid_len,
|
|
|
ssid_len = 32;
|
|
ssid_len = 32;
|
|
|
if (pass_len > 64)
|
|
if (pass_len > 64)
|
|
|
pass_len = 64;
|
|
pass_len = 64;
|
|
|
- for (i = 0; i < sizeof(buf); i++)
|
|
|
|
|
- buf[i] = 0;
|
|
|
|
|
- cfg->magic = WIFI_CFG_MAGIC;
|
|
|
|
|
- cfg->ssid_len = (unsigned char)ssid_len;
|
|
|
|
|
- cfg->pass_len = (unsigned char)pass_len;
|
|
|
|
|
|
|
+ for (i = 0; i < sizeof(rec); i++)
|
|
|
|
|
+ ((unsigned char *)&rec)[i] = 0;
|
|
|
|
|
+ rec.magic = WIFI_CFG_MAGIC;
|
|
|
|
|
+ rec.ssid_len = (unsigned char)ssid_len;
|
|
|
|
|
+ rec.pass_len = (unsigned char)pass_len;
|
|
|
for (i = 0; i < ssid_len; i++)
|
|
for (i = 0; i < ssid_len; i++)
|
|
|
- cfg->ssid[i] = ssid[i];
|
|
|
|
|
|
|
+ rec.ssid[i] = ssid[i];
|
|
|
for (i = 0; i < pass_len; i++)
|
|
for (i = 0; i < pass_len; i++)
|
|
|
- cfg->pass[i] = pass[i];
|
|
|
|
|
- r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CRED_PATH,
|
|
|
|
|
- (int)cfg, (int)sizeof(*cfg));
|
|
|
|
|
- for (i = 0; i < sizeof(buf); i++)
|
|
|
|
|
- buf[i] = 0;
|
|
|
|
|
|
|
+ rec.pass[i] = pass[i];
|
|
|
|
|
+ r = wifi_store_remember(&rec);
|
|
|
|
|
+ for (i = 0; i < sizeof(rec); i++)
|
|
|
|
|
+ ((volatile unsigned char *)&rec)[i] = 0;
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int load_wifi_cred(struct wifi_cfg *cfg)
|
|
|
|
|
-{
|
|
|
|
|
- /* FAT reads can write a final partial sector past the requested
|
|
|
|
|
- * length; keep 512 bytes of slack so the stack top is never touched. */
|
|
|
|
|
- unsigned char buf[sizeof(struct wifi_cfg) + 512];
|
|
|
|
|
- unsigned int i;
|
|
|
|
|
- int r;
|
|
|
|
|
-
|
|
|
|
|
- wipe_cfg(cfg);
|
|
|
|
|
- r = u_syscall3(SYS_READ_FILE, (int)WIFI_CRED_PATH,
|
|
|
|
|
- (int)buf, (int)sizeof(*cfg));
|
|
|
|
|
- if (r < (int)sizeof(*cfg) || ((struct wifi_cfg *)buf)->magic !=
|
|
|
|
|
- WIFI_CFG_MAGIC) {
|
|
|
|
|
- for (i = 0; i < sizeof(buf); i++)
|
|
|
|
|
- buf[i] = 0;
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
- for (i = 0; i < sizeof(*cfg); i++)
|
|
|
|
|
- ((unsigned char *)cfg)[i] = buf[i];
|
|
|
|
|
- for (i = 0; i < sizeof(buf); i++)
|
|
|
|
|
- buf[i] = 0;
|
|
|
|
|
- if (cfg->ssid_len == 0 || cfg->ssid_len > 32 ||
|
|
|
|
|
- cfg->pass_len > 64) {
|
|
|
|
|
- wipe_cfg(cfg);
|
|
|
|
|
- return -1;
|
|
|
|
|
- }
|
|
|
|
|
- return 0;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
static void put_ssid(const unsigned char *s, unsigned int n)
|
|
static void put_ssid(const unsigned char *s, unsigned int n)
|
|
|
{
|
|
{
|
|
|
unsigned int i;
|
|
unsigned int i;
|
|
@@ -441,50 +396,19 @@ static void spawn_keeper(struct wpa_sm *sm, unsigned int conn_id)
|
|
|
"drop at the next group rekey\n");
|
|
"drop at the next group rekey\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int connect_bss(const char *ssid, const char *pass,
|
|
|
|
|
- const unsigned char *want_bssid)
|
|
|
|
|
|
|
+/* Join one BSS from a completed scan. `pass` is empty for an open network.
|
|
|
|
|
+ * Returns 0 when the data port is open, 1 on failure. */
|
|
|
|
|
+static int join_bss(const struct sys_wifi_bss *b, const char *pass)
|
|
|
{
|
|
{
|
|
|
struct sys_wifi_connect c;
|
|
struct sys_wifi_connect c;
|
|
|
struct sys_wifi_status st;
|
|
struct sys_wifi_status st;
|
|
|
static struct wpa_sm sm;
|
|
static struct wpa_sm sm;
|
|
|
unsigned char pmk[32];
|
|
unsigned char pmk[32];
|
|
|
- struct sys_wifi_bss *b = 0;
|
|
|
|
|
- int n, i, r;
|
|
|
|
|
- unsigned int ssid_len;
|
|
|
|
|
- int have_bssid = want_bssid != 0;
|
|
|
|
|
-
|
|
|
|
|
- ssid_len = (unsigned int)slen(ssid);
|
|
|
|
|
- if (!ssid_len) {
|
|
|
|
|
- u_puts("usage: wifi connect <ssid> [passphrase]\n");
|
|
|
|
|
- return 1;
|
|
|
|
|
- }
|
|
|
|
|
- if (bring_up() < 0)
|
|
|
|
|
- return 1;
|
|
|
|
|
- wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
|
|
|
- u_puts("wifi: scanning for \""); u_puts(ssid); u_puts("\"...\n");
|
|
|
|
|
- n = do_scan();
|
|
|
|
|
- if (n < 0)
|
|
|
|
|
- return 1;
|
|
|
|
|
- for (i = 0; i < n; i++) {
|
|
|
|
|
- unsigned int j;
|
|
|
|
|
|
|
+ unsigned int ssid_len = b->ssid_len;
|
|
|
|
|
+ int i, r;
|
|
|
|
|
|
|
|
- if (bss[i].ssid_len != ssid_len)
|
|
|
|
|
- continue;
|
|
|
|
|
- if (have_bssid) {
|
|
|
|
|
- for (j = 0; j < 6 && bss[i].bssid[j] == want_bssid[j]; j++)
|
|
|
|
|
- ;
|
|
|
|
|
- if (j != 6)
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- for (j = 0; j < ssid_len && bss[i].ssid[j] == (unsigned char)ssid[j]; j++)
|
|
|
|
|
- ;
|
|
|
|
|
- if (j == ssid_len) {
|
|
|
|
|
- b = &bss[i]; /* sorted: first match is strongest */
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (!b) {
|
|
|
|
|
- u_puts("wifi: network not found\n");
|
|
|
|
|
|
|
+ if (ssid_len == 0 || ssid_len > 32) {
|
|
|
|
|
+ u_puts("wifi: malformed SSID in scan result\n");
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
if (b->security == SYS_WIFI_SEC_OTHER) {
|
|
if (b->security == SYS_WIFI_SEC_OTHER) {
|
|
@@ -500,9 +424,9 @@ static int connect_bss(const char *ssid, const char *pass,
|
|
|
for (i = 0; i < 6; i++)
|
|
for (i = 0; i < 6; i++)
|
|
|
c.bssid[i] = b->bssid[i];
|
|
c.bssid[i] = b->bssid[i];
|
|
|
c.channel = b->channel;
|
|
c.channel = b->channel;
|
|
|
- c.ssid_len = ssid_len;
|
|
|
|
|
|
|
+ c.ssid_len = (unsigned char)ssid_len;
|
|
|
for (i = 0; i < (int)ssid_len; i++)
|
|
for (i = 0; i < (int)ssid_len; i++)
|
|
|
- c.ssid[i] = (unsigned char)ssid[i];
|
|
|
|
|
|
|
+ c.ssid[i] = b->ssid[i];
|
|
|
c.beacon_int = b->beacon_int;
|
|
c.beacon_int = b->beacon_int;
|
|
|
c.dtim = b->dtim;
|
|
c.dtim = b->dtim;
|
|
|
if (b->security == SYS_WIFI_SEC_PSK) {
|
|
if (b->security == SYS_WIFI_SEC_PSK) {
|
|
@@ -549,6 +473,51 @@ static int connect_bss(const char *ssid, const char *pass,
|
|
|
return r == 0 ? 0 : 1;
|
|
return r == 0 ? 0 : 1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static int connect_bss(const char *ssid, const char *pass,
|
|
|
|
|
+ const unsigned char *want_bssid)
|
|
|
|
|
+{
|
|
|
|
|
+ struct sys_wifi_bss *b = 0;
|
|
|
|
|
+ int n, i;
|
|
|
|
|
+ unsigned int ssid_len;
|
|
|
|
|
+ int have_bssid = want_bssid != 0;
|
|
|
|
|
+
|
|
|
|
|
+ ssid_len = (unsigned int)slen(ssid);
|
|
|
|
|
+ if (!ssid_len) {
|
|
|
|
|
+ u_puts("usage: wifi connect <ssid> [passphrase]\n");
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (bring_up() < 0)
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
|
|
|
+ u_puts("wifi: scanning for \""); u_puts(ssid); u_puts("\"...\n");
|
|
|
|
|
+ n = do_scan();
|
|
|
|
|
+ if (n < 0)
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ for (i = 0; i < n; i++) {
|
|
|
|
|
+ unsigned int j;
|
|
|
|
|
+
|
|
|
|
|
+ if (bss[i].ssid_len != ssid_len)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (have_bssid) {
|
|
|
|
|
+ for (j = 0; j < 6 && bss[i].bssid[j] == want_bssid[j]; j++)
|
|
|
|
|
+ ;
|
|
|
|
|
+ if (j != 6)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (j = 0; j < ssid_len && bss[i].ssid[j] == (unsigned char)ssid[j]; j++)
|
|
|
|
|
+ ;
|
|
|
|
|
+ if (j == ssid_len) {
|
|
|
|
|
+ b = &bss[i]; /* sorted: first match is strongest */
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!b) {
|
|
|
|
|
+ u_puts("wifi: network not found\n");
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return join_bss(b, pass);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
static int cmd_connect(const char *args)
|
|
static int cmd_connect(const char *args)
|
|
|
{
|
|
{
|
|
|
char ssid[33], pass[80], bssid_s[13];
|
|
char ssid[33], pass[80], bssid_s[13];
|
|
@@ -569,9 +538,9 @@ static int cmd_connect(const char *args)
|
|
|
}
|
|
}
|
|
|
r = connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
|
|
r = connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
|
|
|
if (r == 0) {
|
|
if (r == 0) {
|
|
|
- if (save_wifi_cred((const unsigned char *)ssid,
|
|
|
|
|
- (unsigned int)slen(ssid), pass,
|
|
|
|
|
- (unsigned int)slen(pass)) == 0)
|
|
|
|
|
|
|
+ if (remember_wifi((const unsigned char *)ssid,
|
|
|
|
|
+ (unsigned int)slen(ssid), pass,
|
|
|
|
|
+ (unsigned int)slen(pass)) == 0)
|
|
|
u_puts("wifi: saved for auto-connect\n");
|
|
u_puts("wifi: saved for auto-connect\n");
|
|
|
else
|
|
else
|
|
|
u_puts("wifi: note: could not save credentials to /data\n");
|
|
u_puts("wifi: note: could not save credentials to /data\n");
|
|
@@ -611,8 +580,8 @@ static int cmd_connect_file(const char *args)
|
|
|
pass[cfg.pass_len] = '\0';
|
|
pass[cfg.pass_len] = '\0';
|
|
|
r = connect_bss(ssid, pass, cfg.bssid);
|
|
r = connect_bss(ssid, pass, cfg.bssid);
|
|
|
if (r == 0) {
|
|
if (r == 0) {
|
|
|
- if (save_wifi_cred(cfg.ssid, cfg.ssid_len, cfg.pass,
|
|
|
|
|
- cfg.pass_len) == 0)
|
|
|
|
|
|
|
+ if (remember_wifi(cfg.ssid, cfg.ssid_len, cfg.pass,
|
|
|
|
|
+ cfg.pass_len) == 0)
|
|
|
u_puts("wifi: saved for auto-connect\n");
|
|
u_puts("wifi: saved for auto-connect\n");
|
|
|
else
|
|
else
|
|
|
u_puts("wifi: note: credentials not saved (no writable /data)\n");
|
|
u_puts("wifi: note: credentials not saved (no writable /data)\n");
|
|
@@ -624,66 +593,130 @@ static int cmd_connect_file(const char *args)
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG. A
|
|
|
|
|
- * wired link that is physically up wins; an existing WiFi link is left
|
|
|
|
|
- * alone. */
|
|
|
|
|
|
|
+/* Boot-time helper: scan once, then join the strongest network present whose
|
|
|
|
|
+ * SSID is in /data/WIFI.CFG. If none succeeds, scan again after three
|
|
|
|
|
+ * seconds. A wired link that is physically up wins; an existing WiFi link
|
|
|
|
|
+ * is left alone. */
|
|
|
static int cmd_autoconnect(void)
|
|
static int cmd_autoconnect(void)
|
|
|
{
|
|
{
|
|
|
- struct wifi_cfg cfg;
|
|
|
|
|
|
|
+ struct wifi_cfg nets[WIFI_CFG_MAX];
|
|
|
struct sys_wifi_status ws;
|
|
struct sys_wifi_status ws;
|
|
|
struct sys_netinfo ni;
|
|
struct sys_netinfo ni;
|
|
|
- char ssid[33], pass[65];
|
|
|
|
|
- unsigned int i;
|
|
|
|
|
- int attempt, r = 1;
|
|
|
|
|
|
|
+ char pass[65];
|
|
|
|
|
+ int count, n, i, round, r = 1;
|
|
|
|
|
|
|
|
- if (load_wifi_cred(&cfg) != 0)
|
|
|
|
|
|
|
+ count = wifi_store_load(nets, WIFI_CFG_MAX);
|
|
|
|
|
+ if (count <= 0)
|
|
|
return 0;
|
|
return 0;
|
|
|
/* A wired link that is physically up wins: do not even probe the WiFi
|
|
/* A wired link that is physically up wins: do not even probe the WiFi
|
|
|
* card in that case. Only fall back to WiFi when no cable is
|
|
* card in that case. Only fall back to WiFi when no cable is
|
|
|
* connected. */
|
|
* connected. */
|
|
|
if (u_net_info(&ni) == 1 && ni.present && ni.link &&
|
|
if (u_net_info(&ni) == 1 && ni.present && ni.link &&
|
|
|
- !name_eq(ni.name, "wifi")) {
|
|
|
|
|
- wipe_cfg(&cfg);
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
|
|
|
|
|
- wipe_cfg(&cfg);
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- if (ws.state >= SYS_WIFI_ST_LINK) {
|
|
|
|
|
- wipe_cfg(&cfg);
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- for (i = 0; i < cfg.ssid_len; i++)
|
|
|
|
|
- ssid[i] = (char)cfg.ssid[i];
|
|
|
|
|
- ssid[cfg.ssid_len] = '\0';
|
|
|
|
|
- for (i = 0; i < cfg.pass_len; i++)
|
|
|
|
|
- pass[i] = cfg.pass[i];
|
|
|
|
|
- pass[cfg.pass_len] = '\0';
|
|
|
|
|
- for (attempt = 0; attempt < 2; attempt++) {
|
|
|
|
|
- r = connect_bss(ssid, pass,
|
|
|
|
|
- bssid_zero(cfg.bssid) ? 0 : cfg.bssid);
|
|
|
|
|
- if (r == 0)
|
|
|
|
|
- break;
|
|
|
|
|
- if (attempt == 0)
|
|
|
|
|
|
|
+ !name_eq(ni.name, "wifi"))
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present)
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ if (ws.state >= SYS_WIFI_ST_LINK)
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ if (bring_up() < 0)
|
|
|
|
|
+ goto out;
|
|
|
|
|
+ for (round = 0; round < 2 && r; round++) {
|
|
|
|
|
+ unsigned char tried[WIFI_CFG_MAX];
|
|
|
|
|
+ unsigned int j;
|
|
|
|
|
+
|
|
|
|
|
+ if (round)
|
|
|
u_sleep_ms(3000);
|
|
u_sleep_ms(3000);
|
|
|
|
|
+ for (j = 0; j < WIFI_CFG_MAX; j++)
|
|
|
|
|
+ tried[j] = 0;
|
|
|
|
|
+ wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
|
|
|
+ n = do_scan();
|
|
|
|
|
+ if (n < 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ for (i = 0; i < n && r; i++) {
|
|
|
|
|
+ struct sys_wifi_bss *b = &bss[i];
|
|
|
|
|
+ int ci;
|
|
|
|
|
+
|
|
|
|
|
+ if (b->security == SYS_WIFI_SEC_OTHER)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ ci = wifi_store_find(nets, count, b->ssid,
|
|
|
|
|
+ b->ssid_len);
|
|
|
|
|
+ if (ci < 0 || tried[ci])
|
|
|
|
|
+ continue;
|
|
|
|
|
+ if (b->security == SYS_WIFI_SEC_PSK &&
|
|
|
|
|
+ nets[ci].pass_len == 0)
|
|
|
|
|
+ continue;
|
|
|
|
|
+ tried[ci] = 1;
|
|
|
|
|
+ for (j = 0; j < nets[ci].pass_len; j++)
|
|
|
|
|
+ pass[j] = nets[ci].pass[j];
|
|
|
|
|
+ pass[nets[ci].pass_len] = '\0';
|
|
|
|
|
+ u_puts("wifi: auto-connect \"");
|
|
|
|
|
+ put_ssid(b->ssid, b->ssid_len);
|
|
|
|
|
+ u_puts("\"\n");
|
|
|
|
|
+ r = join_bss(b, pass);
|
|
|
|
|
+ for (j = 0; j < sizeof(pass); j++)
|
|
|
|
|
+ ((volatile char *)pass)[j] = 0;
|
|
|
|
|
+ if (r != 0)
|
|
|
|
|
+ wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- for (i = 0; i < sizeof(cfg); i++)
|
|
|
|
|
- ((unsigned char *)&cfg)[i] = 0;
|
|
|
|
|
- for (i = 0; i < sizeof(pass); i++)
|
|
|
|
|
|
|
+out:
|
|
|
|
|
+ for (i = 0; i < (int)sizeof(nets); i++)
|
|
|
|
|
+ ((volatile unsigned char *)nets)[i] = 0;
|
|
|
|
|
+ for (i = 0; i < (int)sizeof(pass); i++)
|
|
|
((volatile char *)pass)[i] = 0;
|
|
((volatile char *)pass)[i] = 0;
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-static int cmd_forget(void)
|
|
|
|
|
|
|
+static int cmd_saved(void)
|
|
|
{
|
|
{
|
|
|
- if (u_syscall1(SYS_UNLINK, (int)WIFI_CRED_PATH) == 0)
|
|
|
|
|
- u_puts("wifi: saved credentials removed\n");
|
|
|
|
|
- else
|
|
|
|
|
- u_puts("wifi: no saved credentials\n");
|
|
|
|
|
|
|
+ struct wifi_cfg list[WIFI_CFG_MAX];
|
|
|
|
|
+ int n, i;
|
|
|
|
|
+ unsigned int j;
|
|
|
|
|
+
|
|
|
|
|
+ n = wifi_store_load(list, WIFI_CFG_MAX);
|
|
|
|
|
+ if (n <= 0) {
|
|
|
|
|
+ u_puts("wifi: no saved networks\n");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ u_puts("saved networks (");
|
|
|
|
|
+ u_putdec((unsigned int)n);
|
|
|
|
|
+ u_puts("):\n");
|
|
|
|
|
+ for (i = 0; i < n; i++) {
|
|
|
|
|
+ u_puts(" \"");
|
|
|
|
|
+ put_ssid(list[i].ssid, list[i].ssid_len);
|
|
|
|
|
+ u_puts("\" ");
|
|
|
|
|
+ u_puts(list[i].pass_len ? "WPA2-PSK" : "open");
|
|
|
|
|
+ u_puts("\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ for (j = 0; j < sizeof(list); j++)
|
|
|
|
|
+ ((volatile unsigned char *)list)[j] = 0;
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+static int cmd_forget(const char *args)
|
|
|
|
|
+{
|
|
|
|
|
+ char ssid[33];
|
|
|
|
|
+ int r;
|
|
|
|
|
+
|
|
|
|
|
+ args = word(args, ssid, sizeof(ssid));
|
|
|
|
|
+ if (!ssid[0]) {
|
|
|
|
|
+ if (u_syscall1(SYS_UNLINK, (int)WIFI_CRED_PATH) == 0)
|
|
|
|
|
+ u_puts("wifi: saved networks removed\n");
|
|
|
|
|
+ else
|
|
|
|
|
+ u_puts("wifi: no saved networks\n");
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ r = wifi_store_forget((const unsigned char *)ssid,
|
|
|
|
|
+ (unsigned int)slen(ssid));
|
|
|
|
|
+ if (r > 0)
|
|
|
|
|
+ u_puts("wifi: saved network removed\n");
|
|
|
|
|
+ else if (r == 0)
|
|
|
|
|
+ u_puts("wifi: network is not saved\n");
|
|
|
|
|
+ else
|
|
|
|
|
+ u_puts("wifi: could not update /data/WIFI.CFG\n");
|
|
|
|
|
+ return r < 0 ? 1 : 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
/* Background: answer group-key rekeys until the connection changes. */
|
|
/* Background: answer group-key rekeys until the connection changes. */
|
|
|
static int keeper(const char *args)
|
|
static int keeper(const char *args)
|
|
|
{
|
|
{
|
|
@@ -763,15 +796,17 @@ void _start(const char *arg)
|
|
|
r = cmd_connect(a + 8);
|
|
r = cmd_connect(a + 8);
|
|
|
else if (starts(a, "connectfile "))
|
|
else if (starts(a, "connectfile "))
|
|
|
r = cmd_connect_file(a + 12);
|
|
r = cmd_connect_file(a + 12);
|
|
|
|
|
+ else if (starts(a, "saved"))
|
|
|
|
|
+ r = cmd_saved();
|
|
|
else if (starts(a, "autoconnect"))
|
|
else if (starts(a, "autoconnect"))
|
|
|
r = cmd_autoconnect();
|
|
r = cmd_autoconnect();
|
|
|
else if (starts(a, "forget"))
|
|
else if (starts(a, "forget"))
|
|
|
- r = cmd_forget();
|
|
|
|
|
|
|
+ r = cmd_forget(a + 6);
|
|
|
else if (starts(a, "disconnect")) {
|
|
else if (starts(a, "disconnect")) {
|
|
|
wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
wifi(SYS_WIFI_DISCONNECT, 0, 0);
|
|
|
u_puts("wifi: disconnected\n");
|
|
u_puts("wifi: disconnected\n");
|
|
|
} else {
|
|
} else {
|
|
|
- u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|autoconnect|forget|disconnect]\n");
|
|
|
|
|
|
|
+ u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|saved|autoconnect|forget [ssid]|disconnect]\n");
|
|
|
r = 1;
|
|
r = 1;
|
|
|
}
|
|
}
|
|
|
u_exit(r);
|
|
u_exit(r);
|