/* WiFi station: see wifi.h. * * MLME-lite: open-system authentication and (re)association are built and * parsed here; the RSN IE for the association request and the whole WPA2 * 4-way/group handshake come from the ring-3 `wifi` command (wpa.c), which * exchanges EAPOL frames through SYS_WIFI_EAPOL_RX/TX and installs the * TK/GTK with SYS_WIFI_SET_KEY. CCMP TX runs in the Intel firmware or in * rtl8821au.c; CCMP RX is checked here and decrypted in software when the * device did not (ccmp.c). * * Data frames are non-QoS (the association request has no WMM/HT IE), so * every frame is a plain 24-byte-header 802.11 data frame + LLC/SNAP. * Page-allocated buffers only: the kernel image must stay below 0x90000. */ #include "wifi.h" #include "ccmp.h" #include "iwl_mvm.h" /* IWL_RX_* status bits */ #include "iwl_trans.h" /* iwl_memcpy/memzero/udelay */ #include "wifi_fw.h" #include "io.h" #include "mm.h" #include "net.h" #include "paging.h" #include "process.h" #include "serial.h" #include "syscall.h" #define MAX_BSS 32 #define RXQ_SLOTS 16 #define RXQ_FRAME 1536 #define EAPOLQ_SLOTS 4 #define EAPOL_MAX 1024 struct rxq_slot { unsigned short len; unsigned char data[RXQ_FRAME]; }; struct eapol_slot { unsigned short len; unsigned char data[EAPOL_MAX]; }; static struct { int present; /* probed on PCI */ int state; /* SYS_WIFI_ST_* */ unsigned char mac[6]; struct sys_wifi_bss *bss; /* page: MAX_BSS entries */ unsigned int nbss; struct sys_wifi_bss cur; /* the BSS we are joining / joined */ int protect; /* RSN association: CCMP data */ unsigned long long pn; /* CCMP TX packet number */ /* MLME waits */ int auth_done, auth_status; int assoc_done, assoc_status; unsigned short aid; /* queues (page-allocated) */ struct rxq_slot *rxq; unsigned int rxq_head, rxq_tail; struct eapol_slot *eapq; unsigned int eapq_head, eapq_tail; unsigned char *txbuf; /* one 2 KB frame build buffer */ unsigned int rx_frames, tx_frames, rx_dropped; unsigned int rx_logged_grp, rx_logged_uni; int beacon_log; unsigned char tk[16], gtk[16]; /* software CCMP RX keys */ int have_tk, have_gtk; unsigned int gtk_idx; int sw_rx_log, sw_fail_log; unsigned int conn_id; unsigned char ptk[48]; unsigned char ptk_replay[8]; unsigned int ptk_conn; const char *detail; } W; static void wmemcpy(void *d, const void *s, unsigned int n) { iwl_memcpy(d, s, n); } static int meq(const unsigned char *a, const unsigned char *b, unsigned int n) { while (n--) if (*a++ != *b++) return 0; return 1; } static void put16(unsigned char *p, unsigned int v) { p[0] = v; p[1] = v >> 8; } static unsigned int get16(const unsigned char *p) { return p[0] | (p[1] << 8); } static int alloc_buffers(void) { if (W.bss) return 0; W.bss = (struct sys_wifi_bss *)mm_alloc_pages( (MAX_BSS * sizeof(struct sys_wifi_bss) + 4095) / 4096); W.rxq = (struct rxq_slot *)mm_alloc_pages( (RXQ_SLOTS * sizeof(struct rxq_slot) + 4095) / 4096); W.eapq = (struct eapol_slot *)mm_alloc_pages( (EAPOLQ_SLOTS * sizeof(struct eapol_slot) + 4095) / 4096); W.txbuf = (unsigned char *)mm_alloc_page(); if (!W.bss || !W.rxq || !W.eapq || !W.txbuf) { W.bss = 0; return -1; } return 0; } /* A firmware assert (CSR_INT SW_ERR) leaves the card dead: every command * times out until the image is reloaded. Drop to DOWN so the next scan or * connect runs the whole bring-up again (it also clears the fatal flag). */ static void wifi_check_crash(void) { if (W.state == SYS_WIFI_ST_DOWN || !wififw_fatal()) return; serial_puts(SERIAL_COM1, "wifi: device died (firmware crash or unplug), " "reloading on next use\n"); wififw_down(); W.state = SYS_WIFI_ST_DOWN; W.detail = "device died; reload on next use"; } static int wifi_up(void) { if (W.state != SYS_WIFI_ST_DOWN) return 0; if (alloc_buffers() < 0) { W.detail = "out of memory"; return -1; } if (wififw_up(W.mac) < 0) { W.detail = wififw_state(); return -1; } W.present = 1; W.state = SYS_WIFI_ST_IDLE; W.detail = "ready"; return 0; } /* ---- management frame parsing ------------------------------------------ */ static void parse_rsn(struct sys_wifi_bss *b, const unsigned char *ie, unsigned int len) { static const unsigned char oui[3] = { 0x00, 0x0f, 0xac }; unsigned int n, i, p = 2; int ccmp = 0, psk = 0, group_ok = 0; if (len + 2 > sizeof(b->rsn)) len = sizeof(b->rsn) - 2; b->rsn[0] = 48; b->rsn[1] = len; wmemcpy(b->rsn + 2, ie, len); b->rsn_len = len + 2; /* version(2) group(4) npair(2) pair[] nakm(2) akm[] caps */ if (len < 8) goto out; group_ok = meq(ie + p, oui, 3) && ie[p + 3] == 4; p += 4; n = get16(ie + p); p += 2; for (i = 0; i < n && p + 4 <= len; i++, p += 4) if (meq(ie + p, oui, 3) && ie[p + 3] == 4) ccmp = 1; if (p + 2 > len) goto out; n = get16(ie + p); p += 2; for (i = 0; i < n && p + 4 <= len; i++, p += 4) if (meq(ie + p, oui, 3) && ie[p + 3] == 2) psk = 1; out: b->security = (ccmp && psk && group_ok) ? SYS_WIFI_SEC_PSK : SYS_WIFI_SEC_OTHER; } static void rx_beacon(const unsigned char *f, unsigned int len, int rssi, unsigned int phy_chan) { struct sys_wifi_bss tmp, *b = 0; unsigned int off = 36, i; if (len < 36 || !W.bss) return; iwl_memzero(&tmp, sizeof(tmp)); wmemcpy(tmp.bssid, f + 16, 6); tmp.beacon_int = get16(f + 32); tmp.capab = get16(f + 34); tmp.rssi = (signed char)rssi; tmp.channel = phy_chan; tmp.dtim = 1; tmp.security = (tmp.capab & 0x10) ? SYS_WIFI_SEC_OTHER : SYS_WIFI_SEC_OPEN; while (off + 2 <= len) { unsigned int id = f[off], l = f[off + 1]; const unsigned char *ie = f + off + 2; if (off + 2 + l > len) break; if (id == 0 && l <= 32) { tmp.ssid_len = l; wmemcpy(tmp.ssid, ie, l); } else if (id == 3 && l >= 1) { tmp.channel = ie[0]; } else if (id == 5 && l >= 2) { tmp.dtim = ie[1] ? ie[1] : 1; } else if (id == 48) { parse_rsn(&tmp, ie, l); } off += 2 + l; } for (i = 0; i < W.nbss; i++) if (meq(W.bss[i].bssid, tmp.bssid, 6)) { b = &W.bss[i]; break; } if (!b) { if (W.nbss < MAX_BSS) { b = &W.bss[W.nbss++]; } else { /* replace the weakest entry if this one is stronger */ b = &W.bss[0]; for (i = 1; i < MAX_BSS; i++) if (W.bss[i].rssi < b->rssi) b = &W.bss[i]; if (b->rssi >= tmp.rssi) return; } } *b = tmp; } static void rx_mgmt(const unsigned char *f, unsigned int len, int rssi, unsigned int chan) { unsigned int stype = (f[0] >> 4) & 0xf; if (stype == 8 || stype == 5) { /* beacon / probe resp */ rx_beacon(f, len, rssi, chan); if (W.state >= SYS_WIFI_ST_ASSOC && meq(f + 16, W.cur.bssid, 6)) W.cur.rssi = (signed char)rssi; return; } if (len < 24 || !meq(f + 4, W.mac, 6) || !meq(f + 16, W.cur.bssid, 6)) return; if (stype == 11 && len >= 30) { /* authentication */ if (get16(f + 26) == 2) { W.auth_status = get16(f + 28); W.auth_done = 1; } } else if ((stype == 1 || stype == 3) && len >= 30) { /* assoc resp */ W.assoc_status = get16(f + 26); W.aid = get16(f + 28) & 0x3fff; W.assoc_done = 1; } else if (stype == 12 || stype == 10) { /* deauth / disassoc */ if (W.state >= SYS_WIFI_ST_ASSOC) { serial_puts(SERIAL_COM1, "wifi: deauthenticated, reason "); serial_putdec(SERIAL_COM1, len >= 26 ? get16(f + 24) : 0); serial_puts(SERIAL_COM1, "\n"); W.state = SYS_WIFI_ST_IDLE; W.detail = "deauthenticated by AP"; wififw_disconnect(); } } } static void queue_eapol(const unsigned char *p, unsigned int len) { struct eapol_slot *s; if (len > EAPOL_MAX || ((W.eapq_head + 1) % EAPOLQ_SLOTS) == W.eapq_tail) return; s = &W.eapq[W.eapq_head]; s->len = len; wmemcpy(s->data, p, len); W.eapq_head = (W.eapq_head + 1) % EAPOLQ_SLOTS; } static void puthex2(unsigned int v) { static const char hx[] = "0123456789abcdef"; serial_putc(SERIAL_COM1, hx[(v >> 4) & 15]); serial_putc(SERIAL_COM1, hx[v & 15]); } static void putmac(const unsigned char *m) { unsigned int i; for (i = 0; i < 6; i++) { if (i) serial_putc(SERIAL_COM1, ':'); puthex2(m[i]); } } /* First few data-path drops of each kind (group and unicast) go to * serial/netmon with the reason, address, CCMP key ID and the firmware RX * status (decrypt result in bits 8..10, MIC_OK bit 6, replay bit 7, * DEC_DONE bit 11). */ static void rx_drop(const char *why, const unsigned char *f, unsigned int len, unsigned int status) { unsigned int hdr = 24 + (((f[0] >> 4) & 0xf) & 8 ? 2 : 0); int grp = f[4] & 1; W.rx_dropped++; if (grp) { if (W.rx_logged_grp++ >= 8) return; } else { if (W.rx_logged_uni++ >= 8) return; } serial_puts(SERIAL_COM1, "wifi: rx drop "); serial_puts(SERIAL_COM1, why); serial_puts(SERIAL_COM1, " fc "); serial_puthex(SERIAL_COM1, f[0] | (f[1] << 8)); serial_puts(SERIAL_COM1, " len "); serial_putdec(SERIAL_COM1, len); serial_puts(SERIAL_COM1, " a1 "); putmac(f + 4); serial_puts(SERIAL_COM1, " a2 "); putmac(f + 10); serial_puts(SERIAL_COM1, " ccmp "); serial_putdec(SERIAL_COM1, len >= hdr + 8 ? (f[hdr + 3] >> 6) & 3 : 0xff); serial_puts(SERIAL_COM1, " sec "); serial_putdec(SERIAL_COM1, (status >> 8) & 7); serial_puts(SERIAL_COM1, " kv "); serial_putdec(SERIAL_COM1, (status >> 3) & 1); serial_puts(SERIAL_COM1, " mic "); serial_putdec(SERIAL_COM1, (status >> 6) & 1); serial_puts(SERIAL_COM1, " dec "); serial_putdec(SERIAL_COM1, (status >> 11) & 1); serial_puts(SERIAL_COM1, " rep "); serial_putdec(SERIAL_COM1, (status >> 7) & 1); serial_puts(SERIAL_COM1, " status "); serial_puthex(SERIAL_COM1, status); serial_puts(SERIAL_COM1, "\n"); } static void rx_data(const unsigned char *f, unsigned int len, unsigned int status) { static const unsigned char snap[6] = { 0xaa, 0xaa, 0x03, 0, 0, 0 }; unsigned int hdr = 24, stype = (f[0] >> 4) & 0xf, type, plen; const unsigned char *p; struct rxq_slot *s; int prot = (f[1] & 0x40) != 0; if (W.state < SYS_WIFI_ST_ASSOC || (f[1] & 0x03) != 0x02) return; /* FromDS only */ if (!meq(f + 10, W.cur.bssid, 6)) return; if (stype & 4) return; /* null / no data */ if (stype & 8) hdr += 2; /* QoS control */ if (prot) { unsigned int kid = (f[hdr + 3] >> 6) & 3; const unsigned char *key = 0; int fw_ok = (status & IWL_RX_SEC_MSK) == IWL_RX_SEC_CCM && (status & IWL_RX_MIC_OK); /* The firmware sometimes does the AES-CTR step in place but * still reports a failed MIC/decrypt status. The SNAP header * in the clear is a reliable sign that the payload is already * plaintext. */ if (!fw_ok && len >= hdr + 16 && meq(f + hdr + 8, snap, 6)) { if (W.sw_rx_log < 4) { W.sw_rx_log++; serial_puts(SERIAL_COM1, "wifi: fw plaintext id "); serial_putdec(SERIAL_COM1, kid); serial_puts(SERIAL_COM1, "\n"); } fw_ok = 1; } if (!fw_ok) { /* The firmware's RX crypto path sometimes reports * SEC_ENC_ERR for keys it has installed. Decrypt in * software with the same TK/GTK; TX proves they are * correct. */ if (f[4] & 1) { if (W.have_gtk && kid == W.gtk_idx) key = W.gtk; } else if (kid == 0 && W.have_tk) { key = W.tk; } if (!key) { rx_drop("no key", f, len, status); return; } if (ccmp_rx_decrypt((unsigned char *)f, len, hdr, key) < 0) { if (W.sw_fail_log < 3 && len >= hdr + 16) { unsigned int z; W.sw_fail_log++; serial_puts(SERIAL_COM1, "wifi: sw ccmp failed payload "); for (z = 0; z < 8; z++) puthex2(f[hdr + 8 + z]); serial_puts(SERIAL_COM1, "\n"); } rx_drop("sw ccmp failed", f, len, status); return; } if (W.sw_rx_log < 4) { W.sw_rx_log++; serial_puts(SERIAL_COM1, "wifi: sw ccmp ok id "); serial_putdec(SERIAL_COM1, kid); serial_puts(SERIAL_COM1, "\n"); } } hdr += 8; /* CCMP header */ if (len < hdr + 8) return; len -= 8; /* MIC */ } if (len < hdr + 8 || !meq(f + hdr, snap, 6)) { rx_drop("no LLC/SNAP", f, len, status); return; } type = (f[hdr + 6] << 8) | f[hdr + 7]; p = f + hdr + 8; plen = len - hdr - 8; if (type == 0x888e) { queue_eapol(p, plen); return; } /* Before the keys are in, or unprotected after: drop. */ if (W.state != SYS_WIFI_ST_LINK || (W.protect && !prot)) { rx_drop(W.state != SYS_WIFI_ST_LINK ? "port closed" : "unprotected", f, len, status); return; } if (plen + 14 > RXQ_FRAME || ((W.rxq_head + 1) % RXQ_SLOTS) == W.rxq_tail) { rx_drop("queue full", f, len, status); return; } s = &W.rxq[W.rxq_head]; wmemcpy(s->data, f + 4, 6); /* DA = A1 */ wmemcpy(s->data + 6, f + 16, 6); /* SA = A3 */ s->data[12] = type >> 8; s->data[13] = type; wmemcpy(s->data + 14, p, plen); s->len = plen + 14; W.rxq_head = (W.rxq_head + 1) % RXQ_SLOTS; W.rx_frames++; } void wifi_rx_frame(const unsigned char *frame, unsigned int len, int rssi, unsigned int status, unsigned int channel) { unsigned int type, stype; if (len < 24) return; type = (frame[0] >> 2) & 3; stype = (frame[0] >> 4) & 0xf; if (W.state >= SYS_WIFI_ST_ASSOC && type == 0 && stype == 8 && W.beacon_log < 3) { W.beacon_log++; serial_puts(SERIAL_COM1, "wifi: beacon status "); serial_puthex(SERIAL_COM1, status); serial_puts(SERIAL_COM1, " srcfound "); serial_putdec(SERIAL_COM1, (status >> 2) & 1); serial_puts(SERIAL_COM1, " staid "); serial_putdec(SERIAL_COM1, (status >> 24) & 0x1f); serial_puts(SERIAL_COM1, "\n"); } if (type == 0) rx_mgmt(frame, len, rssi, channel); else if (type == 2) rx_data(frame, len, status); } void wifi_scan_done(int ok) { if (!ok) serial_puts(SERIAL_COM1, "wifi: scan aborted\n"); } /* ---- TX ---------------------------------------------------------------- */ /* Build and send an 802.11 data frame to the AP: header, [CCMP header], * LLC/SNAP, ethertype, payload. */ static int tx_data(const unsigned char *dst, unsigned int ethertype, const unsigned char *payload, unsigned int plen) { unsigned char *f = W.txbuf; unsigned int o = 24; int prot = W.protect && W.state == SYS_WIFI_ST_LINK; if (W.state < SYS_WIFI_ST_ASSOC || plen + 24 + 8 + 8 > 2048) return -1; iwl_memzero(f, 24); f[0] = 0x08; /* data */ f[1] = 0x01 | (prot ? 0x40 : 0); /* ToDS [| protected] */ wmemcpy(f + 4, W.cur.bssid, 6); wmemcpy(f + 10, W.mac, 6); wmemcpy(f + 16, dst, 6); if (prot) { unsigned long long pn = ++W.pn; f[o + 0] = pn; f[o + 1] = pn >> 8; f[o + 2] = 0; f[o + 3] = 0x20; /* ExtIV, key id 0 */ f[o + 4] = pn >> 16; f[o + 5] = pn >> 24; f[o + 6] = pn >> 32; f[o + 7] = pn >> 40; o += 8; } f[o++] = 0xaa; f[o++] = 0xaa; f[o++] = 0x03; f[o++] = 0; f[o++] = 0; f[o++] = 0; f[o++] = ethertype >> 8; f[o++] = ethertype; wmemcpy(f + o, payload, plen); if (wififw_tx_data(f, o + plen, prot) < 0) return -1; W.tx_frames++; return 0; } static int tx_mgmt_frame(unsigned int stype, const unsigned char *body, unsigned int blen) { unsigned char *f = W.txbuf; if (24 + blen > 2048) return -1; iwl_memzero(f, 24); f[0] = stype << 4; wmemcpy(f + 4, W.cur.bssid, 6); wmemcpy(f + 10, W.mac, 6); wmemcpy(f + 16, W.cur.bssid, 6); wmemcpy(f + 24, body, blen); return wififw_tx_mgmt(f, 24 + blen); } static int wait_flag(int *flag, unsigned int ms) { unsigned int t; for (t = 0; t < ms && !*flag; t++) { if (wififw_poll() < 0) return -1; if (!*flag) iwl_udelay(1000); } return *flag ? 0 : -1; } static int do_connect(const struct sys_wifi_connect *c) { unsigned char body[160]; unsigned int o, i, tries; static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96, 0x0c, 0x12, 0x18, 0x24, 50, 4, 0x30, 0x48, 0x60, 0x6c }; static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24, 0xb0, 0x48, 0x60, 0x6c }; if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0) return -1; if (W.state >= SYS_WIFI_ST_ASSOC) { wififw_disconnect(); W.state = SYS_WIFI_ST_IDLE; } if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel) return -1; W.conn_id++; iwl_memzero(&W.cur, sizeof(W.cur)); wmemcpy(W.cur.bssid, c->bssid, 6); W.cur.channel = c->channel; W.cur.ssid_len = c->ssid_len; wmemcpy(W.cur.ssid, c->ssid, c->ssid_len); W.cur.beacon_int = c->beacon_int ? c->beacon_int : 100; W.cur.dtim = c->dtim ? c->dtim : 1; W.protect = c->ie_len != 0; W.pn = 0; W.eapq_head = W.eapq_tail = 0; W.rxq_head = W.rxq_tail = 0; W.rx_dropped = 0; W.rx_logged_grp = W.rx_logged_uni = 0; W.beacon_log = 0; W.have_tk = W.have_gtk = 0; W.sw_rx_log = W.sw_fail_log = 0; W.ptk_conn = 0; if (wififw_prepare_connect(c->bssid, c->channel, W.cur.beacon_int) < 0) { W.detail = "firmware connect setup failed"; return -1; } /* Open-system authentication: alg 0, seq 1, status 0. */ for (tries = 0; tries < 3; tries++) { W.auth_done = 0; put16(body, 0); put16(body + 2, 1); put16(body + 4, 0); if (tx_mgmt_frame(11, body, 6) < 0) break; if (wait_flag(&W.auth_done, 300) == 0) break; } if (!W.auth_done || W.auth_status) { W.detail = W.auth_done ? "authentication refused" : "no authentication reply"; wififw_disconnect(); return -1; } /* Association request: capability, listen interval, SSID, rates, * [RSN IE]. */ o = 0; put16(body, 0x0001 | (W.protect ? 0x0010 : 0) | 0x0400); put16(body + 2, 10); o = 4; body[o++] = 0; body[o++] = c->ssid_len; for (i = 0; i < c->ssid_len; i++) body[o++] = c->ssid[i]; if (c->channel <= 14) { wmemcpy(body + o, r24, sizeof(r24)); o += sizeof(r24); } else { wmemcpy(body + o, r5, sizeof(r5)); o += sizeof(r5); } if (c->ie_len) { wmemcpy(body + o, c->ie, c->ie_len); o += c->ie_len; } for (tries = 0; tries < 3; tries++) { W.assoc_done = 0; if (tx_mgmt_frame(0, body, o) < 0) break; if (wait_flag(&W.assoc_done, 500) == 0) break; } if (!W.assoc_done || W.assoc_status) { W.detail = W.assoc_done ? "association refused" : "no association reply"; if (W.assoc_done) { serial_puts(SERIAL_COM1, "wifi: assoc status "); serial_putdec(SERIAL_COM1, W.assoc_status); serial_puts(SERIAL_COM1, "\n"); } wififw_disconnect(); return -1; } if (wififw_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) { W.detail = "firmware association update failed"; wififw_disconnect(); return -1; } W.state = SYS_WIFI_ST_ASSOC; W.detail = W.protect ? "associated, waiting for keys" : "associated"; serial_puts(SERIAL_COM1, "wifi: associated, aid "); serial_putdec(SERIAL_COM1, W.aid); serial_puts(SERIAL_COM1, "\n"); return 0; } static void do_disconnect(void) { static const unsigned char reason[2] = { 3, 0 }; /* leaving */ W.have_tk = W.have_gtk = 0; W.ptk_conn = 0; if (W.state >= SYS_WIFI_ST_ASSOC) { tx_mgmt_frame(12, reason, 2); wififw_poll(); wififw_disconnect(); } if (W.state != SYS_WIFI_ST_DOWN) W.state = SYS_WIFI_ST_IDLE; W.detail = "disconnected"; } /* ---- netdev ------------------------------------------------------------- */ static int nd_init(void) { if (W.state != SYS_WIFI_ST_DOWN) return 0; /* Boot probe: find the card only. The firmware is loaded on first * use (`wifi`), so boot time and RAM are unchanged without WiFi. */ if (wififw_probe() < 0) return -1; W.present = 1; W.detail = "firmware not loaded"; return 0; } static void nd_get_mac(unsigned char mac[6]) { wmemcpy(mac, W.mac, 6); } static int nd_rx(unsigned char *buf, unsigned int maxlen) { struct rxq_slot *s; unsigned int n; wifi_check_crash(); if (W.state == SYS_WIFI_ST_DOWN) return -1; if (W.rxq_head == W.rxq_tail) wififw_poll(); if (W.rxq_head == W.rxq_tail) return -1; s = &W.rxq[W.rxq_tail]; n = s->len < maxlen ? s->len : maxlen; wmemcpy(buf, s->data, n); W.rxq_tail = (W.rxq_tail + 1) % RXQ_SLOTS; return (int)n; } static int nd_tx(const unsigned char *buf, unsigned int len) { if (len < 14 || W.state != SYS_WIFI_ST_LINK) return -1; return tx_data(buf, (buf[12] << 8) | buf[13], buf + 14, len - 14); } static void print_hex6(const unsigned char *m) { unsigned int i; for (i = 0; i < 6; i++) { static const char hx[] = "0123456789abcdef"; char s[4]; s[0] = hx[m[i] >> 4]; s[1] = hx[m[i] & 15]; s[2] = i < 5 ? ':' : 0; s[3] = 0; io_putstr(s); } } static void print_dec(unsigned int v) { char b[12]; int i = 11; b[i] = 0; do { b[--i] = (char)('0' + v % 10); v /= 10; } while (v && i > 0); io_putstr(b + i); } static void nd_print_info(void) { static const char *const st[] = { "down", "idle", "associated", "link up" }; io_putstr("chip: "); io_putstr(wififw_chip_name()); io_putstr("\nstate: "); io_putstr(st[W.state & 3]); io_putstr(" ("); io_putstr(W.detail ? W.detail : "-"); io_putstr(")\nmac: "); print_hex6(W.mac); if (W.state >= SYS_WIFI_ST_ASSOC) { char ssid[33]; unsigned int i; for (i = 0; i < W.cur.ssid_len && i < 32; i++) ssid[i] = W.cur.ssid[i] >= 32 && W.cur.ssid[i] < 127 ? (char)W.cur.ssid[i] : '?'; ssid[i] = 0; io_putstr("\nbss: "); print_hex6(W.cur.bssid); io_putstr(" \""); io_putstr(ssid); io_putstr("\" ch "); print_dec(W.cur.channel); } io_putstr("\n"); } static void nd_stop(void) { wifi_shutdown(); } static int nd_link_up(void) { return W.state == SYS_WIFI_ST_LINK; } const struct netdev wifi_netdev = { "wifi", "WiFi", nd_init, nd_get_mac, nd_rx, nd_tx, nd_print_info, 0, 0, 0, 0, 0, 0, 0, 0, nd_stop, nd_link_up, }; void wifi_shutdown(void) { wififw_shutdown(); if (W.state != SYS_WIFI_ST_DOWN) W.state = SYS_WIFI_ST_DOWN; } /* ---- SYS_WIFI ------------------------------------------------------------ */ static int user_ok(int ptr, unsigned int len, int write) { return ptr && paging_user_range_ok(current_proc()->cr3, (unsigned int)ptr, len, write); } static void copy_str(char *dst, unsigned int max, const char *src) { unsigned int i; for (i = 0; src && src[i] && i + 1 < max; i++) dst[i] = src[i]; dst[i] = 0; } int sys_wifi(int op, int a2, int a3) { wifi_check_crash(); switch (op) { case SYS_WIFI_UP: if (!W.present && nd_init() < 0) return -1; return wifi_up(); case SYS_WIFI_STATUS: { struct sys_wifi_status *s = (struct sys_wifi_status *)a2; if (!user_ok(a2, sizeof(*s), 1)) return -1; if (!W.present && wififw_probe() == 0) W.present = 1; iwl_memzero(s, sizeof(*s)); s->state = W.state; s->present = (unsigned char)W.present; wmemcpy(s->mac, W.mac, 6); if (W.state >= SYS_WIFI_ST_ASSOC) { wmemcpy(s->bssid, W.cur.bssid, 6); s->channel = W.cur.channel; s->rssi = W.cur.rssi; s->ssid_len = W.cur.ssid_len; wmemcpy(s->ssid, W.cur.ssid, W.cur.ssid_len); } copy_str(s->chip, sizeof(s->chip), W.present ? wififw_chip_name() : "none"); copy_str(s->fw, sizeof(s->fw), wififw_fw_name()); copy_str(s->detail, sizeof(s->detail), W.detail ? W.detail : (W.present ? "firmware not loaded" : "no card")); s->rx_frames = W.rx_frames; s->tx_frames = W.tx_frames; s->rx_dropped = W.rx_dropped; s->conn_id = W.conn_id; return 0; } case SYS_WIFI_SCAN: if (W.state == SYS_WIFI_ST_DOWN && sys_wifi(SYS_WIFI_UP, 0, 0) < 0) return -1; if (W.state >= SYS_WIFI_ST_ASSOC) return -1; /* scanning while associated: not yet */ W.nbss = 0; return wififw_scan_start(); case SYS_WIFI_RESULTS: { unsigned int max = (unsigned int)a3, n; if (W.state == SYS_WIFI_ST_DOWN) return -1; wififw_poll(); if (wififw_scan_running()) return -2; if (max > W.nbss) max = W.nbss; if (max && !user_ok(a2, max * sizeof(struct sys_wifi_bss), 1)) return -1; for (n = 0; n < max; n++) ((struct sys_wifi_bss *)a2)[n] = W.bss[n]; return (int)max; } case SYS_WIFI_CONNECT: { struct sys_wifi_connect c; if (!user_ok(a2, sizeof(c), 0)) return -1; wmemcpy(&c, (const void *)a2, sizeof(c)); return do_connect(&c); } case SYS_WIFI_DISCONNECT: do_disconnect(); return 0; case SYS_WIFI_EAPOL_RX: { struct eapol_slot *s; unsigned int n; if (W.state < SYS_WIFI_ST_ASSOC) return -1; wififw_poll(); if (W.eapq_head == W.eapq_tail) return 0; s = &W.eapq[W.eapq_tail]; n = s->len < (unsigned int)a3 ? s->len : (unsigned int)a3; if (!user_ok(a2, n, 1)) return -1; wmemcpy((void *)a2, s->data, n); W.eapq_tail = (W.eapq_tail + 1) % EAPOLQ_SLOTS; return (int)n; } case SYS_WIFI_EAPOL_TX: { unsigned int len = (unsigned int)a3; if (W.state < SYS_WIFI_ST_ASSOC || len > EAPOL_MAX || !user_ok(a2, len, 0)) return -1; /* Stage behind the 2 KB frame build area of the TX page. */ wmemcpy(W.txbuf + 2048, (const void *)a2, len); return tx_data(W.cur.bssid, 0x888e, W.txbuf + 2048, len); } case SYS_WIFI_SET_KEY: { struct sys_wifi_key k; int r; if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(k), 0)) return -1; wmemcpy(&k, (const void *)a2, sizeof(k)); r = wififw_set_key(k.pairwise, k.keyidx, k.key, k.pairwise ? 0 : k.rsc); if (r == 0) { if (k.pairwise) { wmemcpy(W.tk, k.key, 16); W.have_tk = 1; } else { wmemcpy(W.gtk, k.key, 16); W.gtk_idx = k.keyidx & 3; W.have_gtk = 1; } } return r; } case SYS_WIFI_SET_PTK: { struct sys_wifi_ptk p; if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(p), 0)) return -1; wmemcpy(&p, (const void *)a2, sizeof(p)); wmemcpy(W.ptk, p.ptk, 48); wmemcpy(W.ptk_replay, p.replay, 8); W.ptk_conn = p.conn_id; return 0; } case SYS_WIFI_GET_PTK: { struct sys_wifi_ptk p; if (!W.ptk_conn || (unsigned int)a3 != W.ptk_conn || !user_ok(a2, sizeof(p), 1)) return -1; p.conn_id = W.ptk_conn; wmemcpy(p.ptk, W.ptk, 48); wmemcpy(p.replay, W.ptk_replay, 8); wmemcpy((void *)a2, &p, sizeof(p)); return 0; } case SYS_WIFI_AUTHORIZE: if (W.state < SYS_WIFI_ST_ASSOC) return -1; W.state = SYS_WIFI_ST_LINK; W.detail = "connected"; if (netdev_active() != &wifi_netdev && netdev_select("wifi") != 0) { W.detail = "connected (netdev busy)"; return -1; } return 0; } return -1; }