| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992 |
- /* 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 runs in the firmware.
- *
- * 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"
- #include "iwl_trans.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;
- }
- 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 (iwl_mvm_up(W.mac) < 0) {
- W.detail = iwl_mvm_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";
- iwl_mvm_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 (iwl_mvm_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 iwl_mvm_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 (iwl_mvm_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) {
- iwl_mvm_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 (iwl_mvm_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";
- iwl_mvm_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");
- }
- iwl_mvm_disconnect();
- return -1;
- }
- if (iwl_mvm_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) {
- W.detail = "firmware association update failed";
- iwl_mvm_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);
- iwl_mvm_poll();
- iwl_mvm_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 (iwl_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;
- if (W.state == SYS_WIFI_ST_DOWN)
- return -1;
- if (W.rxq_head == W.rxq_tail)
- iwl_mvm_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(iwl_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",
- "Intel 7265D/3165 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)
- {
- if (W.state != SYS_WIFI_ST_DOWN || iwl_present())
- iwl_mvm_down();
- 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)
- {
- 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;
- const struct iwl_fw_info *fw;
- if (!user_ok(a2, sizeof(*s), 1))
- return -1;
- if (!W.present && iwl_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 ? iwl_chip_name() : "none");
- fw = iwl_fw();
- copy_str(s->fw, sizeof(s->fw), fw && fw->present ? fw->human : "");
- 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 iwl_mvm_scan_start();
- case SYS_WIFI_RESULTS: {
- unsigned int max = (unsigned int)a3, n;
- if (W.state == SYS_WIFI_ST_DOWN)
- return -1;
- iwl_mvm_poll();
- if (iwl_mvm_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;
- iwl_mvm_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 = iwl_mvm_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;
- }
|