| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781 |
- /* wifi: ring-3 WiFi command for the Intel 7265D/3165 (see wifi.c).
- *
- * wifi status
- * wifi up load the firmware, show the MAC address
- * wifi scan list networks (strongest first)
- * wifi connect <ssid> [pass] [bssid]
- * join an open or WPA2-PSK (CCMP) network,
- * run DHCP; quote an SSID with spaces; a
- * BSSID picks one of several APs
- * wifi connectfile <path> join using an SSID/passphrase/BSSID record
- * staged by the network menu (SYS_SPAWN's
- * argument buffer is only 64 bytes)
- * wifi autoconnect reconnect from /data/WIFI.CFG (boot helper)
- * wifi forget remove the saved credentials
- * 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. */
- #include "ucmd_lib.h"
- #include "tls_rng.h"
- #include "wifi_cfg.h"
- #include "wpa.h"
- #define MAX_BSS 32
- static struct sys_wifi_bss bss[MAX_BSS];
- static int wifi(int op, int a2, int a3)
- {
- return u_syscall3(SYS_WIFI, op, a2, a3);
- }
- static int name_eq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static int bssid_zero(const unsigned char b[6])
- {
- 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;
- unsigned int i;
- int r;
- if (ssid_len > 32)
- ssid_len = 32;
- if (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 < ssid_len; i++)
- cfg->ssid[i] = ssid[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;
- 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)
- {
- unsigned int i;
- for (i = 0; i < n; i++)
- u_putc(s[i] >= 32 && s[i] < 127 ? (char)s[i] : '?');
- }
- static int slen(const char *s)
- {
- int n = 0;
- while (s[n])
- n++;
- return n;
- }
- static int starts(const char *s, const char *p)
- {
- while (*p)
- if (*s++ != *p++)
- return 0;
- return 1;
- }
- static const char *skip_ws(const char *s)
- {
- while (*s == ' ')
- s++;
- return s;
- }
- /* Next word or "quoted string" into out; returns the rest. */
- static const char *word(const char *s, char *out, unsigned int max)
- {
- unsigned int n = 0;
- char end = ' ';
- s = skip_ws(s);
- if (*s == '"') {
- end = '"';
- s++;
- }
- while (*s && *s != end) {
- if (end == '"' && *s == '\\' && s[1])
- s++;
- if (n + 1 < max)
- out[n++] = *s;
- s++;
- }
- if (*s == end)
- s++;
- out[n] = 0;
- return s;
- }
- static void put_dbm(int v)
- {
- if (v < 0) {
- u_putc('-');
- v = -v;
- }
- u_putdec((unsigned int)v);
- u_puts(" dBm");
- }
- static const char *sec_name(unsigned int s)
- {
- return s == SYS_WIFI_SEC_OPEN ? "open" :
- s == SYS_WIFI_SEC_PSK ? "WPA2" : "other";
- }
- static int cmd_status(void)
- {
- struct sys_wifi_status st;
- static const char *const names[] = { "down", "idle", "associated",
- "connected" };
- unsigned char ip[4];
- if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0) {
- u_puts("wifi: status failed\n");
- return 1;
- }
- if (!st.present) {
- u_puts("wifi: no Intel 7265/3165 card found\n");
- return 1;
- }
- u_puts("chip: "); u_puts(st.chip);
- u_puts("\nstate: "); u_puts(names[st.state & 3]);
- u_puts(" ("); u_puts(st.detail); u_puts(")\n");
- if (st.fw[0]) {
- u_puts("firmware: "); u_puts(st.fw); u_puts("\n");
- }
- if (st.state != SYS_WIFI_ST_DOWN) {
- u_puts("mac: "); u_put_mac(st.mac); u_puts("\n");
- }
- if (st.state >= SYS_WIFI_ST_ASSOC) {
- u_puts("network: \""); put_ssid(st.ssid, st.ssid_len);
- u_puts("\" "); u_put_mac(st.bssid);
- u_puts(" ch "); u_putdec(st.channel);
- u_puts(" "); put_dbm(st.rssi); u_puts("\n");
- u_syscall1(SYS_NET_GET_IP, (int)ip);
- u_puts("ip: "); u_put_ip(ip); u_puts("\n");
- u_puts("frames: rx "); u_putdec(st.rx_frames);
- u_puts(" tx "); u_putdec(st.tx_frames);
- u_puts(" dropped "); u_putdec(st.rx_dropped); u_puts("\n");
- }
- return 0;
- }
- static int bring_up(void)
- {
- struct sys_wifi_status st;
- u_puts("wifi: loading firmware...\n");
- if (wifi(SYS_WIFI_UP, 0, 0) < 0) {
- wifi(SYS_WIFI_STATUS, (int)&st, 0);
- u_puts("wifi: bring-up failed: ");
- u_puts(st.detail);
- u_puts(" (details on the serial/netmon log)\n");
- return -1;
- }
- return 0;
- }
- /* Scan and return the number of entries, sorted by RSSI. */
- static int do_scan(void)
- {
- int n, i, j;
- unsigned int t0;
- if (wifi(SYS_WIFI_SCAN, 0, 0) < 0) {
- u_puts("wifi: scan failed (disconnect first?)\n");
- return -1;
- }
- t0 = u_ticks_ms();
- while ((n = wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS)) == -2) {
- if (u_ticks_ms() - t0 > 15000) {
- u_puts("wifi: scan timed out\n");
- return -1;
- }
- u_sleep_ms(100);
- }
- if (n < 0)
- return -1;
- for (i = 1; i < n; i++)
- for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
- struct sys_wifi_bss t = bss[j];
- bss[j] = bss[j - 1];
- bss[j - 1] = t;
- }
- return n;
- }
- static int cmd_scan(void)
- {
- int n, i;
- if (bring_up() < 0)
- return 1;
- u_puts("wifi: scanning...\n");
- n = do_scan();
- if (n < 0)
- return 1;
- if (n == 0)
- u_puts("no networks found\n");
- for (i = 0; i < n; i++) {
- u_put_mac(bss[i].bssid);
- u_puts(" ");
- put_dbm(bss[i].rssi);
- u_puts(bss[i].channel < 10 ? " ch " :
- bss[i].channel < 100 ? " ch " : " ch ");
- u_putdec(bss[i].channel);
- u_puts(" ");
- u_puts(sec_name(bss[i].security));
- u_puts(bss[i].security == SYS_WIFI_SEC_PSK ? " " : " ");
- if (bss[i].ssid_len)
- put_ssid(bss[i].ssid, bss[i].ssid_len);
- else
- u_puts("(hidden)");
- u_puts("\n");
- }
- return 0;
- }
- static int hexval(char c)
- {
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- return -1;
- }
- static int unhex(const char *s, unsigned char *out, unsigned int n)
- {
- unsigned int i;
- for (i = 0; i < n; i++) {
- int a = hexval(s[2 * i]), b = hexval(s[2 * i + 1]);
- if (a < 0 || b < 0)
- return -1;
- out[i] = (unsigned char)(a * 16 + b);
- }
- return 0;
- }
- static void tohex(const unsigned char *in, unsigned int n, char *out)
- {
- static const char hx[] = "0123456789abcdef";
- unsigned int i;
- for (i = 0; i < n; i++) {
- out[2 * i] = hx[in[i] >> 4];
- out[2 * i + 1] = hx[in[i] & 15];
- }
- out[2 * n] = 0;
- }
- static int set_gtk(struct wpa_sm *sm)
- {
- struct sys_wifi_key k;
- unsigned int i;
- k.pairwise = 0;
- k.keyidx = sm->gtk_idx;
- for (i = 0; i < 16; i++)
- k.key[i] = sm->gtk[i];
- for (i = 0; i < 8; i++)
- k.rsc[i] = sm->gtk_rsc[i];
- return wifi(SYS_WIFI_SET_KEY, (int)&k, 0);
- }
- /* Run the 4-way handshake: msg1 -> msg2, msg3 -> msg4 + keys. */
- static int handshake(struct wpa_sm *sm)
- {
- static unsigned char frame[1024], reply[256];
- unsigned int t0 = u_ticks_ms(), rlen;
- int n, r;
- while (u_ticks_ms() - t0 < 8000) {
- n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
- if (n < 0)
- return -1;
- if (n == 0) {
- u_sleep_ms(20);
- continue;
- }
- r = wpa_rx(sm, frame, (unsigned int)n, reply, &rlen, tls_random);
- if (r == WPA_ERR) {
- u_puts("wifi: handshake: ");
- u_puts(sm->error ? sm->error : "error");
- u_puts("\n");
- return -1;
- }
- if (r == WPA_NONE)
- continue;
- if (wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen) < 0)
- return -1;
- if (r == WPA_PTK) {
- struct sys_wifi_key k;
- unsigned int i;
- k.pairwise = 1;
- k.keyidx = 0;
- for (i = 0; i < 16; i++)
- k.key[i] = WPA_TK(sm)[i];
- for (i = 0; i < 8; i++)
- k.rsc[i] = 0;
- u_sleep_ms(20); /* let message 4 leave in the clear */
- if (wifi(SYS_WIFI_SET_KEY, (int)&k, 0) < 0 ||
- set_gtk(sm) < 0) {
- u_puts("wifi: key install failed\n");
- return -1;
- }
- return 0;
- }
- }
- u_puts("wifi: handshake timed out (no EAPOL from the AP)\n");
- return -1;
- }
- static void spawn_keeper(struct wpa_sm *sm, unsigned int conn_id)
- {
- static char arg[16];
- struct sys_wifi_ptk p;
- unsigned char id[4];
- char *q = arg;
- unsigned int i;
- p.conn_id = conn_id;
- for (i = 0; i < 48; i++)
- p.ptk[i] = sm->ptk[i];
- for (i = 0; i < 8; i++)
- p.replay[i] = sm->replay[i];
- if (wifi(SYS_WIFI_SET_PTK, (int)&p, 0) < 0) {
- u_puts("wifi: note: could not store rekey state; the link will "
- "drop at the next group rekey\n");
- return;
- }
- for (i = 0; "__keep "[i]; i++)
- *q++ = "__keep "[i];
- id[0] = conn_id >> 24;
- id[1] = conn_id >> 16;
- id[2] = conn_id >> 8;
- id[3] = conn_id;
- tohex(id, 4, q);
- q += 8;
- *q = 0;
- if (u_spawn("/ramfs/bin/wifi", arg, 0, "/") < 0)
- u_puts("wifi: note: rekey helper did not start; the link will "
- "drop at the next group rekey\n");
- }
- static int connect_bss(const char *ssid, const char *pass,
- const unsigned char *want_bssid)
- {
- struct sys_wifi_connect c;
- struct sys_wifi_status st;
- static struct wpa_sm sm;
- 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;
- 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;
- }
- if (b->security == SYS_WIFI_SEC_OTHER) {
- u_puts("wifi: unsupported security (only open and WPA2-PSK/CCMP)\n");
- return 1;
- }
- if (b->security == SYS_WIFI_SEC_PSK && !pass[0]) {
- u_puts("wifi: this network needs a passphrase\n");
- return 1;
- }
- for (i = 0; i < (int)sizeof(c); i++)
- ((unsigned char *)&c)[i] = 0;
- for (i = 0; i < 6; i++)
- c.bssid[i] = b->bssid[i];
- c.channel = b->channel;
- c.ssid_len = ssid_len;
- for (i = 0; i < (int)ssid_len; i++)
- c.ssid[i] = (unsigned char)ssid[i];
- c.beacon_int = b->beacon_int;
- c.dtim = b->dtim;
- if (b->security == SYS_WIFI_SEC_PSK) {
- int plen = slen(pass);
- if (plen == 64 && unhex(pass, pmk, 32) == 0) {
- /* raw 256-bit PSK */
- } else if (plen >= 8 && plen <= 63) {
- u_puts("wifi: deriving key...\n");
- wpa_pmk(pass, c.ssid, ssid_len, pmk);
- } else {
- u_puts("wifi: a WPA2 passphrase has 8..63 characters\n");
- return 1;
- }
- c.ie_len = (unsigned char)wpa_rsn_ie(c.ie);
- }
- u_puts("wifi: joining "); u_put_mac(c.bssid);
- u_puts(" on channel "); u_putdec(c.channel); u_puts("\n");
- if (wifi(SYS_WIFI_CONNECT, (int)&c, 0) < 0) {
- wifi(SYS_WIFI_STATUS, (int)&st, 0);
- u_puts("wifi: "); u_puts(st.detail); u_puts("\n");
- return 1;
- }
- wifi(SYS_WIFI_STATUS, (int)&st, 0);
- if (b->security == SYS_WIFI_SEC_PSK) {
- wpa_init(&sm, pmk, c.bssid, st.mac, c.ie, c.ie_len,
- b->rsn, b->rsn_len);
- u_puts("wifi: 4-way handshake...\n");
- if (handshake(&sm) < 0) {
- wifi(SYS_WIFI_DISCONNECT, 0, 0);
- return 1;
- }
- }
- r = wifi(SYS_WIFI_AUTHORIZE, 0, 0);
- if (r < 0)
- u_puts("wifi: connected, but the WiFi card could not become the "
- "active netdev (close TCP users, then `netdev wifi`)\n");
- else
- u_puts("wifi: connected\n");
- if (b->security == SYS_WIFI_SEC_PSK)
- spawn_keeper(&sm, st.conn_id);
- if (r == 0 && u_syscall1(SYS_DHCP_RUN, 6000) != 0)
- u_puts("wifi: DHCP failed; set an address with netip\n");
- return r == 0 ? 0 : 1;
- }
- static int cmd_connect(const char *args)
- {
- char ssid[33], pass[80], bssid_s[13];
- unsigned char want_bssid[6];
- int have_bssid = 0;
- int r;
- args = word(args, ssid, sizeof(ssid));
- args = word(args, pass, sizeof(pass));
- if (*skip_ws(args)) {
- word(args, bssid_s, sizeof(bssid_s));
- if (slen(bssid_s) != 12 ||
- unhex(bssid_s, want_bssid, 6) < 0) {
- u_puts("wifi: BSSID must be 12 hex digits\n");
- return 1;
- }
- have_bssid = 1;
- }
- r = connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
- if (r == 0) {
- if (save_wifi_cred((const unsigned char *)ssid,
- (unsigned int)slen(ssid), pass,
- (unsigned int)slen(pass)) == 0)
- u_puts("wifi: saved for auto-connect\n");
- else
- u_puts("wifi: note: could not save credentials to /data\n");
- }
- return r;
- }
- /* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
- * BSSID record in /share because SYS_SPAWN's argument buffer is only
- * VFS_PATH_MAX (64) bytes. The file is removed after it has been read. */
- static int cmd_connect_file(const char *args)
- {
- struct wifi_cfg cfg;
- char path[80], ssid[33], pass[65];
- unsigned int i;
- int r;
- args = word(args, path, sizeof(path));
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- r = u_syscall3(SYS_READ_FILE, (int)path, (int)&cfg, (int)sizeof(cfg));
- u_syscall1(SYS_UNLINK, (int)path);
- if (r < 0 || (unsigned int)r < sizeof(cfg) ||
- cfg.magic != WIFI_CFG_MAGIC) {
- u_puts("wifi: no valid staged connect request\n");
- return 1;
- }
- if (cfg.ssid_len == 0 || cfg.ssid_len > 32 || cfg.pass_len > 64) {
- u_puts("wifi: staged connect request is malformed\n");
- return 1;
- }
- 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';
- r = connect_bss(ssid, pass, cfg.bssid);
- if (r == 0) {
- if (save_wifi_cred(cfg.ssid, cfg.ssid_len, cfg.pass,
- cfg.pass_len) == 0)
- u_puts("wifi: saved for auto-connect\n");
- else
- u_puts("wifi: note: credentials not saved (no writable /data)\n");
- }
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- for (i = 0; i < sizeof(pass); i++)
- ((volatile char *)pass)[i] = 0;
- return r;
- }
- /* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG. A
- * wired link that already has an address wins; an existing WiFi link is left
- * alone. */
- static int cmd_autoconnect(void)
- {
- struct wifi_cfg cfg;
- struct sys_wifi_status ws;
- struct sys_netinfo ni;
- char ssid[33], pass[65];
- unsigned int i;
- int attempt, r = 1;
- if (load_wifi_cred(&cfg) != 0)
- 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;
- }
- if (u_net_info(&ni) == 1 && ni.present && !name_eq(ni.name, "wifi")) {
- unsigned char srv[4];
- unsigned int lease = 0;
- /* Only a real wired DHCP lease keeps the boot-time wired link;
- * the static fallback address does not mean the cable is up. */
- if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
- 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)
- u_sleep_ms(3000);
- }
- for (i = 0; i < sizeof(cfg); i++)
- ((unsigned char *)&cfg)[i] = 0;
- for (i = 0; i < sizeof(pass); i++)
- ((volatile char *)pass)[i] = 0;
- return r;
- }
- static int cmd_forget(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");
- return 0;
- }
- /* Background: answer group-key rekeys until the connection changes. */
- static int keeper(const char *args)
- {
- static struct wpa_sm sm;
- static unsigned char frame[1024], reply[256];
- struct sys_wifi_status st;
- struct sys_wifi_ptk p;
- unsigned char id[4];
- unsigned int conn_id, rlen, i;
- args = skip_ws(args);
- if (slen(args) < 8 || unhex(args, id, 4) < 0) {
- u_puts("wifi: keeper: bad state argument\n");
- return 1;
- }
- conn_id = ((unsigned int)id[0] << 24) | (id[1] << 16) |
- (id[2] << 8) | id[3];
- if (wifi(SYS_WIFI_GET_PTK, (int)&p, (int)conn_id) < 0 ||
- p.conn_id != conn_id) {
- u_puts("wifi: keeper: no key state for this connection\n");
- return 1;
- }
- for (i = 0; i < 48; i++)
- sm.ptk[i] = p.ptk[i];
- for (i = 0; i < 8; i++)
- sm.replay[i] = p.replay[i];
- sm.have_replay = 1;
- sm.ptk_ok = 1;
- u_puts("wifi: keeper running\n");
- for (;;) {
- int n, r;
- if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0 ||
- st.conn_id != conn_id || st.state < SYS_WIFI_ST_ASSOC)
- return 0;
- n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
- if (n > 0) {
- r = wpa_rx(&sm, frame, (unsigned int)n, reply, &rlen,
- tls_random);
- u_puts("wifi: keeper eapol ");
- u_putdec((unsigned int)n);
- u_puts(" -> ");
- u_putdec((unsigned int)r);
- u_puts("\n");
- if (r == WPA_GTK) {
- wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen);
- u_puts("wifi: keeper gtk ");
- u_putdec((unsigned int)sm.gtk_idx);
- u_puts(set_gtk(&sm) == 0 ? " installed\n"
- : " install failed\n");
- } else if (r == WPA_ERR) {
- u_puts("wifi: keeper error: ");
- u_puts(sm.error ? sm.error : "?");
- u_puts("\n");
- }
- }
- u_sleep_ms(200);
- }
- }
- void _start(const char *arg)
- {
- const char *a = skip_ws(arg ? arg : "");
- int r = 0;
- if (!*a)
- r = cmd_status();
- else if (starts(a, "__keep "))
- r = keeper(a + 7);
- else if (starts(a, "up")) {
- r = bring_up() < 0;
- if (!r)
- r = cmd_status();
- } else if (starts(a, "scan"))
- r = cmd_scan();
- else if (starts(a, "connect "))
- r = cmd_connect(a + 8);
- else if (starts(a, "connectfile "))
- r = cmd_connect_file(a + 12);
- else if (starts(a, "autoconnect"))
- r = cmd_autoconnect();
- else if (starts(a, "forget"))
- r = cmd_forget();
- else if (starts(a, "disconnect")) {
- wifi(SYS_WIFI_DISCONNECT, 0, 0);
- u_puts("wifi: disconnected\n");
- } else {
- u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|autoconnect|forget|disconnect]\n");
- r = 1;
- }
- u_exit(r);
- }
|