/* 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 [pass] join an open or WPA2-PSK (CCMP) network, * run DHCP; quote an SSID with spaces * wifi disconnect * * The WPA2 handshake (wpa.c) runs here; a small background copy of this * command (`wifi __keep `) stays alive after `connect` to answer * group-key rekeys, and exits when the connection changes. */ #include "ucmd_lib.h" #include "tls_rng.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 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 (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++) { 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[8 + 96 + 16 + 12 + 4]; char *p = arg; unsigned int i; for (i = 0; "__keep "[i]; i++) *p++ = "__keep "[i]; tohex(sm->ptk, 48, p); p += 96; *p++ = ' '; tohex(sm->replay, 8, p); p += 16; *p++ = ' '; { unsigned char id[4] = { conn_id >> 24, conn_id >> 16, conn_id >> 8, conn_id }; tohex(id, 4, p); p += 8; } *p = 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 cmd_connect(const char *args) { char ssid[33], pass[80]; 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; args = word(args, ssid, sizeof(ssid)); word(args, pass, sizeof(pass)); ssid_len = (unsigned int)slen(ssid); if (!ssid_len) { u_puts("usage: wifi connect [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; 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 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; unsigned char id[4]; unsigned int conn_id, rlen; args = skip_ws(args); if (slen(args) < 96 + 1 + 16 + 1 + 8 || unhex(args, sm.ptk, 48) < 0 || unhex(args + 97, sm.replay, 8) < 0 || unhex(args + 114, id, 4) < 0) return 1; conn_id = ((unsigned int)id[0] << 24) | (id[1] << 16) | (id[2] << 8) | id[3]; sm.have_replay = 1; sm.ptk_ok = 1; for (;;) { int n; 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 && wpa_rx(&sm, frame, (unsigned int)n, reply, &rlen, tls_random) == WPA_GTK) { wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen); set_gtk(&sm); } 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, "disconnect")) { wifi(SYS_WIFI_DISCONNECT, 0, 0); u_puts("wifi: disconnected\n"); } else { u_puts("usage: wifi [up|scan|connect [pass]|disconnect]\n"); r = 1; } u_exit(r); }