wifi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /* WiFi station: see wifi.h.
  2. *
  3. * MLME-lite: open-system authentication and (re)association are built and
  4. * parsed here; the RSN IE for the association request and the whole WPA2
  5. * 4-way/group handshake come from the ring-3 `wifi` command (wpa.c), which
  6. * exchanges EAPOL frames through SYS_WIFI_EAPOL_RX/TX and installs the
  7. * TK/GTK with SYS_WIFI_SET_KEY. CCMP runs in the firmware.
  8. *
  9. * Data frames are non-QoS (the association request has no WMM/HT IE), so
  10. * every frame is a plain 24-byte-header 802.11 data frame + LLC/SNAP.
  11. * Page-allocated buffers only: the kernel image must stay below 0x90000. */
  12. #include "wifi.h"
  13. #include "ccmp.h"
  14. #include "iwl_fw.h"
  15. #include "iwl_mvm.h" /* IWL_RX_* status bits, wifi_rx_frame() upcalls */
  16. #include "iwl_trans.h"
  17. #include "io.h"
  18. #include "mm.h"
  19. #include "net.h"
  20. #include "paging.h"
  21. #include "process.h"
  22. #include "serial.h"
  23. #include "syscall.h"
  24. #define MAX_BSS 32
  25. #define RXQ_SLOTS 16
  26. #define RXQ_FRAME 1536
  27. #define EAPOLQ_SLOTS 4
  28. #define EAPOL_MAX 1024
  29. struct rxq_slot {
  30. unsigned short len;
  31. unsigned char data[RXQ_FRAME];
  32. };
  33. struct eapol_slot {
  34. unsigned short len;
  35. unsigned char data[EAPOL_MAX];
  36. };
  37. static struct {
  38. int present; /* probed on PCI */
  39. int state; /* SYS_WIFI_ST_* */
  40. unsigned char mac[6];
  41. struct sys_wifi_bss *bss; /* page: MAX_BSS entries */
  42. unsigned int nbss;
  43. struct sys_wifi_bss cur; /* the BSS we are joining / joined */
  44. int protect; /* RSN association: CCMP data */
  45. unsigned long long pn; /* CCMP TX packet number */
  46. /* MLME waits */
  47. int auth_done, auth_status;
  48. int assoc_done, assoc_status;
  49. unsigned short aid;
  50. /* queues (page-allocated) */
  51. struct rxq_slot *rxq;
  52. unsigned int rxq_head, rxq_tail;
  53. struct eapol_slot *eapq;
  54. unsigned int eapq_head, eapq_tail;
  55. unsigned char *txbuf; /* one 2 KB frame build buffer */
  56. unsigned int rx_frames, tx_frames, rx_dropped;
  57. unsigned int rx_logged_grp, rx_logged_uni;
  58. int beacon_log;
  59. unsigned char tk[16], gtk[16]; /* software CCMP RX keys */
  60. int have_tk, have_gtk;
  61. unsigned int gtk_idx;
  62. int sw_rx_log, sw_fail_log;
  63. unsigned int conn_id;
  64. unsigned char ptk[48];
  65. unsigned char ptk_replay[8];
  66. unsigned int ptk_conn;
  67. const char *detail;
  68. } W;
  69. static void wmemcpy(void *d, const void *s, unsigned int n)
  70. {
  71. iwl_memcpy(d, s, n);
  72. }
  73. static int meq(const unsigned char *a, const unsigned char *b, unsigned int n)
  74. {
  75. while (n--)
  76. if (*a++ != *b++)
  77. return 0;
  78. return 1;
  79. }
  80. static void put16(unsigned char *p, unsigned int v)
  81. {
  82. p[0] = v; p[1] = v >> 8;
  83. }
  84. static unsigned int get16(const unsigned char *p)
  85. {
  86. return p[0] | (p[1] << 8);
  87. }
  88. static int alloc_buffers(void)
  89. {
  90. if (W.bss)
  91. return 0;
  92. W.bss = (struct sys_wifi_bss *)mm_alloc_pages(
  93. (MAX_BSS * sizeof(struct sys_wifi_bss) + 4095) / 4096);
  94. W.rxq = (struct rxq_slot *)mm_alloc_pages(
  95. (RXQ_SLOTS * sizeof(struct rxq_slot) + 4095) / 4096);
  96. W.eapq = (struct eapol_slot *)mm_alloc_pages(
  97. (EAPOLQ_SLOTS * sizeof(struct eapol_slot) + 4095) / 4096);
  98. W.txbuf = (unsigned char *)mm_alloc_page();
  99. if (!W.bss || !W.rxq || !W.eapq || !W.txbuf) {
  100. W.bss = 0;
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. /* A firmware assert (CSR_INT SW_ERR) leaves the card dead: every command
  106. * times out until the image is reloaded. Drop to DOWN so the next scan or
  107. * connect runs the whole bring-up again (it also clears the fatal flag). */
  108. static void wifi_check_crash(void)
  109. {
  110. if (W.state == SYS_WIFI_ST_DOWN || !iwl_fatal())
  111. return;
  112. serial_puts(SERIAL_COM1, "wifi: firmware crashed, reloading on next use\n");
  113. iwlfw_down();
  114. W.state = SYS_WIFI_ST_DOWN;
  115. W.detail = "firmware crashed; reload on next use";
  116. }
  117. static int wifi_up(void)
  118. {
  119. if (W.state != SYS_WIFI_ST_DOWN)
  120. return 0;
  121. if (alloc_buffers() < 0) {
  122. W.detail = "out of memory";
  123. return -1;
  124. }
  125. if (iwlfw_up(W.mac) < 0) {
  126. W.detail = iwlfw_state();
  127. return -1;
  128. }
  129. W.present = 1;
  130. W.state = SYS_WIFI_ST_IDLE;
  131. W.detail = "ready";
  132. return 0;
  133. }
  134. /* ---- management frame parsing ------------------------------------------ */
  135. static void parse_rsn(struct sys_wifi_bss *b, const unsigned char *ie,
  136. unsigned int len)
  137. {
  138. static const unsigned char oui[3] = { 0x00, 0x0f, 0xac };
  139. unsigned int n, i, p = 2;
  140. int ccmp = 0, psk = 0, group_ok = 0;
  141. if (len + 2 > sizeof(b->rsn))
  142. len = sizeof(b->rsn) - 2;
  143. b->rsn[0] = 48;
  144. b->rsn[1] = len;
  145. wmemcpy(b->rsn + 2, ie, len);
  146. b->rsn_len = len + 2;
  147. /* version(2) group(4) npair(2) pair[] nakm(2) akm[] caps */
  148. if (len < 8)
  149. goto out;
  150. group_ok = meq(ie + p, oui, 3) && ie[p + 3] == 4;
  151. p += 4;
  152. n = get16(ie + p);
  153. p += 2;
  154. for (i = 0; i < n && p + 4 <= len; i++, p += 4)
  155. if (meq(ie + p, oui, 3) && ie[p + 3] == 4)
  156. ccmp = 1;
  157. if (p + 2 > len)
  158. goto out;
  159. n = get16(ie + p);
  160. p += 2;
  161. for (i = 0; i < n && p + 4 <= len; i++, p += 4)
  162. if (meq(ie + p, oui, 3) && ie[p + 3] == 2)
  163. psk = 1;
  164. out:
  165. b->security = (ccmp && psk && group_ok) ? SYS_WIFI_SEC_PSK
  166. : SYS_WIFI_SEC_OTHER;
  167. }
  168. static void rx_beacon(const unsigned char *f, unsigned int len, int rssi,
  169. unsigned int phy_chan)
  170. {
  171. struct sys_wifi_bss tmp, *b = 0;
  172. unsigned int off = 36, i;
  173. if (len < 36 || !W.bss)
  174. return;
  175. iwl_memzero(&tmp, sizeof(tmp));
  176. wmemcpy(tmp.bssid, f + 16, 6);
  177. tmp.beacon_int = get16(f + 32);
  178. tmp.capab = get16(f + 34);
  179. tmp.rssi = (signed char)rssi;
  180. tmp.channel = phy_chan;
  181. tmp.dtim = 1;
  182. tmp.security = (tmp.capab & 0x10) ? SYS_WIFI_SEC_OTHER : SYS_WIFI_SEC_OPEN;
  183. while (off + 2 <= len) {
  184. unsigned int id = f[off], l = f[off + 1];
  185. const unsigned char *ie = f + off + 2;
  186. if (off + 2 + l > len)
  187. break;
  188. if (id == 0 && l <= 32) {
  189. tmp.ssid_len = l;
  190. wmemcpy(tmp.ssid, ie, l);
  191. } else if (id == 3 && l >= 1) {
  192. tmp.channel = ie[0];
  193. } else if (id == 5 && l >= 2) {
  194. tmp.dtim = ie[1] ? ie[1] : 1;
  195. } else if (id == 48) {
  196. parse_rsn(&tmp, ie, l);
  197. }
  198. off += 2 + l;
  199. }
  200. for (i = 0; i < W.nbss; i++)
  201. if (meq(W.bss[i].bssid, tmp.bssid, 6)) {
  202. b = &W.bss[i];
  203. break;
  204. }
  205. if (!b) {
  206. if (W.nbss < MAX_BSS) {
  207. b = &W.bss[W.nbss++];
  208. } else {
  209. /* replace the weakest entry if this one is stronger */
  210. b = &W.bss[0];
  211. for (i = 1; i < MAX_BSS; i++)
  212. if (W.bss[i].rssi < b->rssi)
  213. b = &W.bss[i];
  214. if (b->rssi >= tmp.rssi)
  215. return;
  216. }
  217. }
  218. *b = tmp;
  219. }
  220. static void rx_mgmt(const unsigned char *f, unsigned int len, int rssi,
  221. unsigned int chan)
  222. {
  223. unsigned int stype = (f[0] >> 4) & 0xf;
  224. if (stype == 8 || stype == 5) { /* beacon / probe resp */
  225. rx_beacon(f, len, rssi, chan);
  226. if (W.state >= SYS_WIFI_ST_ASSOC && meq(f + 16, W.cur.bssid, 6))
  227. W.cur.rssi = (signed char)rssi;
  228. return;
  229. }
  230. if (len < 24 || !meq(f + 4, W.mac, 6) || !meq(f + 16, W.cur.bssid, 6))
  231. return;
  232. if (stype == 11 && len >= 30) { /* authentication */
  233. if (get16(f + 26) == 2) {
  234. W.auth_status = get16(f + 28);
  235. W.auth_done = 1;
  236. }
  237. } else if ((stype == 1 || stype == 3) && len >= 30) { /* assoc resp */
  238. W.assoc_status = get16(f + 26);
  239. W.aid = get16(f + 28) & 0x3fff;
  240. W.assoc_done = 1;
  241. } else if (stype == 12 || stype == 10) { /* deauth / disassoc */
  242. if (W.state >= SYS_WIFI_ST_ASSOC) {
  243. serial_puts(SERIAL_COM1, "wifi: deauthenticated, reason ");
  244. serial_putdec(SERIAL_COM1, len >= 26 ? get16(f + 24) : 0);
  245. serial_puts(SERIAL_COM1, "\n");
  246. W.state = SYS_WIFI_ST_IDLE;
  247. W.detail = "deauthenticated by AP";
  248. iwlfw_disconnect();
  249. }
  250. }
  251. }
  252. static void queue_eapol(const unsigned char *p, unsigned int len)
  253. {
  254. struct eapol_slot *s;
  255. if (len > EAPOL_MAX ||
  256. ((W.eapq_head + 1) % EAPOLQ_SLOTS) == W.eapq_tail)
  257. return;
  258. s = &W.eapq[W.eapq_head];
  259. s->len = len;
  260. wmemcpy(s->data, p, len);
  261. W.eapq_head = (W.eapq_head + 1) % EAPOLQ_SLOTS;
  262. }
  263. static void puthex2(unsigned int v)
  264. {
  265. static const char hx[] = "0123456789abcdef";
  266. serial_putc(SERIAL_COM1, hx[(v >> 4) & 15]);
  267. serial_putc(SERIAL_COM1, hx[v & 15]);
  268. }
  269. static void putmac(const unsigned char *m)
  270. {
  271. unsigned int i;
  272. for (i = 0; i < 6; i++) {
  273. if (i)
  274. serial_putc(SERIAL_COM1, ':');
  275. puthex2(m[i]);
  276. }
  277. }
  278. /* First few data-path drops of each kind (group and unicast) go to
  279. * serial/netmon with the reason, address, CCMP key ID and the firmware RX
  280. * status (decrypt result in bits 8..10, MIC_OK bit 6, replay bit 7,
  281. * DEC_DONE bit 11). */
  282. static void rx_drop(const char *why, const unsigned char *f, unsigned int len,
  283. unsigned int status)
  284. {
  285. unsigned int hdr = 24 + (((f[0] >> 4) & 0xf) & 8 ? 2 : 0);
  286. int grp = f[4] & 1;
  287. W.rx_dropped++;
  288. if (grp) {
  289. if (W.rx_logged_grp++ >= 8)
  290. return;
  291. } else {
  292. if (W.rx_logged_uni++ >= 8)
  293. return;
  294. }
  295. serial_puts(SERIAL_COM1, "wifi: rx drop ");
  296. serial_puts(SERIAL_COM1, why);
  297. serial_puts(SERIAL_COM1, " fc ");
  298. serial_puthex(SERIAL_COM1, f[0] | (f[1] << 8));
  299. serial_puts(SERIAL_COM1, " len ");
  300. serial_putdec(SERIAL_COM1, len);
  301. serial_puts(SERIAL_COM1, " a1 ");
  302. putmac(f + 4);
  303. serial_puts(SERIAL_COM1, " a2 ");
  304. putmac(f + 10);
  305. serial_puts(SERIAL_COM1, " ccmp ");
  306. serial_putdec(SERIAL_COM1, len >= hdr + 8 ? (f[hdr + 3] >> 6) & 3
  307. : 0xff);
  308. serial_puts(SERIAL_COM1, " sec ");
  309. serial_putdec(SERIAL_COM1, (status >> 8) & 7);
  310. serial_puts(SERIAL_COM1, " kv ");
  311. serial_putdec(SERIAL_COM1, (status >> 3) & 1);
  312. serial_puts(SERIAL_COM1, " mic ");
  313. serial_putdec(SERIAL_COM1, (status >> 6) & 1);
  314. serial_puts(SERIAL_COM1, " dec ");
  315. serial_putdec(SERIAL_COM1, (status >> 11) & 1);
  316. serial_puts(SERIAL_COM1, " rep ");
  317. serial_putdec(SERIAL_COM1, (status >> 7) & 1);
  318. serial_puts(SERIAL_COM1, " status ");
  319. serial_puthex(SERIAL_COM1, status);
  320. serial_puts(SERIAL_COM1, "\n");
  321. }
  322. static void rx_data(const unsigned char *f, unsigned int len,
  323. unsigned int status)
  324. {
  325. static const unsigned char snap[6] = { 0xaa, 0xaa, 0x03, 0, 0, 0 };
  326. unsigned int hdr = 24, stype = (f[0] >> 4) & 0xf, type, plen;
  327. const unsigned char *p;
  328. struct rxq_slot *s;
  329. int prot = (f[1] & 0x40) != 0;
  330. if (W.state < SYS_WIFI_ST_ASSOC || (f[1] & 0x03) != 0x02)
  331. return; /* FromDS only */
  332. if (!meq(f + 10, W.cur.bssid, 6))
  333. return;
  334. if (stype & 4)
  335. return; /* null / no data */
  336. if (stype & 8)
  337. hdr += 2; /* QoS control */
  338. if (prot) {
  339. unsigned int kid = (f[hdr + 3] >> 6) & 3;
  340. const unsigned char *key = 0;
  341. int fw_ok = (status & IWL_RX_SEC_MSK) == IWL_RX_SEC_CCM &&
  342. (status & IWL_RX_MIC_OK);
  343. /* The firmware sometimes does the AES-CTR step in place but
  344. * still reports a failed MIC/decrypt status. The SNAP header
  345. * in the clear is a reliable sign that the payload is already
  346. * plaintext. */
  347. if (!fw_ok && len >= hdr + 16 &&
  348. meq(f + hdr + 8, snap, 6)) {
  349. if (W.sw_rx_log < 4) {
  350. W.sw_rx_log++;
  351. serial_puts(SERIAL_COM1,
  352. "wifi: fw plaintext id ");
  353. serial_putdec(SERIAL_COM1, kid);
  354. serial_puts(SERIAL_COM1, "\n");
  355. }
  356. fw_ok = 1;
  357. }
  358. if (!fw_ok) {
  359. /* The firmware's RX crypto path sometimes reports
  360. * SEC_ENC_ERR for keys it has installed. Decrypt in
  361. * software with the same TK/GTK; TX proves they are
  362. * correct. */
  363. if (f[4] & 1) {
  364. if (W.have_gtk && kid == W.gtk_idx)
  365. key = W.gtk;
  366. } else if (kid == 0 && W.have_tk) {
  367. key = W.tk;
  368. }
  369. if (!key) {
  370. rx_drop("no key", f, len, status);
  371. return;
  372. }
  373. if (ccmp_rx_decrypt((unsigned char *)f, len, hdr,
  374. key) < 0) {
  375. if (W.sw_fail_log < 3 && len >= hdr + 16) {
  376. unsigned int z;
  377. W.sw_fail_log++;
  378. serial_puts(SERIAL_COM1,
  379. "wifi: sw ccmp failed payload ");
  380. for (z = 0; z < 8; z++)
  381. puthex2(f[hdr + 8 + z]);
  382. serial_puts(SERIAL_COM1, "\n");
  383. }
  384. rx_drop("sw ccmp failed", f, len, status);
  385. return;
  386. }
  387. if (W.sw_rx_log < 4) {
  388. W.sw_rx_log++;
  389. serial_puts(SERIAL_COM1,
  390. "wifi: sw ccmp ok id ");
  391. serial_putdec(SERIAL_COM1, kid);
  392. serial_puts(SERIAL_COM1, "\n");
  393. }
  394. }
  395. hdr += 8; /* CCMP header */
  396. if (len < hdr + 8)
  397. return;
  398. len -= 8; /* MIC */
  399. }
  400. if (len < hdr + 8 || !meq(f + hdr, snap, 6)) {
  401. rx_drop("no LLC/SNAP", f, len, status);
  402. return;
  403. }
  404. type = (f[hdr + 6] << 8) | f[hdr + 7];
  405. p = f + hdr + 8;
  406. plen = len - hdr - 8;
  407. if (type == 0x888e) {
  408. queue_eapol(p, plen);
  409. return;
  410. }
  411. /* Before the keys are in, or unprotected after: drop. */
  412. if (W.state != SYS_WIFI_ST_LINK || (W.protect && !prot)) {
  413. rx_drop(W.state != SYS_WIFI_ST_LINK ? "port closed"
  414. : "unprotected", f, len,
  415. status);
  416. return;
  417. }
  418. if (plen + 14 > RXQ_FRAME ||
  419. ((W.rxq_head + 1) % RXQ_SLOTS) == W.rxq_tail) {
  420. rx_drop("queue full", f, len, status);
  421. return;
  422. }
  423. s = &W.rxq[W.rxq_head];
  424. wmemcpy(s->data, f + 4, 6); /* DA = A1 */
  425. wmemcpy(s->data + 6, f + 16, 6); /* SA = A3 */
  426. s->data[12] = type >> 8;
  427. s->data[13] = type;
  428. wmemcpy(s->data + 14, p, plen);
  429. s->len = plen + 14;
  430. W.rxq_head = (W.rxq_head + 1) % RXQ_SLOTS;
  431. W.rx_frames++;
  432. }
  433. void wifi_rx_frame(const unsigned char *frame, unsigned int len, int rssi,
  434. unsigned int status, unsigned int channel)
  435. {
  436. unsigned int type, stype;
  437. if (len < 24)
  438. return;
  439. type = (frame[0] >> 2) & 3;
  440. stype = (frame[0] >> 4) & 0xf;
  441. if (W.state >= SYS_WIFI_ST_ASSOC && type == 0 && stype == 8 &&
  442. W.beacon_log < 3) {
  443. W.beacon_log++;
  444. serial_puts(SERIAL_COM1, "wifi: beacon status ");
  445. serial_puthex(SERIAL_COM1, status);
  446. serial_puts(SERIAL_COM1, " srcfound ");
  447. serial_putdec(SERIAL_COM1, (status >> 2) & 1);
  448. serial_puts(SERIAL_COM1, " staid ");
  449. serial_putdec(SERIAL_COM1, (status >> 24) & 0x1f);
  450. serial_puts(SERIAL_COM1, "\n");
  451. }
  452. if (type == 0)
  453. rx_mgmt(frame, len, rssi, channel);
  454. else if (type == 2)
  455. rx_data(frame, len, status);
  456. }
  457. void wifi_scan_done(int ok)
  458. {
  459. if (!ok)
  460. serial_puts(SERIAL_COM1, "wifi: scan aborted\n");
  461. }
  462. /* ---- TX ---------------------------------------------------------------- */
  463. /* Build and send an 802.11 data frame to the AP: header, [CCMP header],
  464. * LLC/SNAP, ethertype, payload. */
  465. static int tx_data(const unsigned char *dst, unsigned int ethertype,
  466. const unsigned char *payload, unsigned int plen)
  467. {
  468. unsigned char *f = W.txbuf;
  469. unsigned int o = 24;
  470. int prot = W.protect && W.state == SYS_WIFI_ST_LINK;
  471. if (W.state < SYS_WIFI_ST_ASSOC || plen + 24 + 8 + 8 > 2048)
  472. return -1;
  473. iwl_memzero(f, 24);
  474. f[0] = 0x08; /* data */
  475. f[1] = 0x01 | (prot ? 0x40 : 0); /* ToDS [| protected] */
  476. wmemcpy(f + 4, W.cur.bssid, 6);
  477. wmemcpy(f + 10, W.mac, 6);
  478. wmemcpy(f + 16, dst, 6);
  479. if (prot) {
  480. unsigned long long pn = ++W.pn;
  481. f[o + 0] = pn;
  482. f[o + 1] = pn >> 8;
  483. f[o + 2] = 0;
  484. f[o + 3] = 0x20; /* ExtIV, key id 0 */
  485. f[o + 4] = pn >> 16;
  486. f[o + 5] = pn >> 24;
  487. f[o + 6] = pn >> 32;
  488. f[o + 7] = pn >> 40;
  489. o += 8;
  490. }
  491. f[o++] = 0xaa; f[o++] = 0xaa; f[o++] = 0x03;
  492. f[o++] = 0; f[o++] = 0; f[o++] = 0;
  493. f[o++] = ethertype >> 8;
  494. f[o++] = ethertype;
  495. wmemcpy(f + o, payload, plen);
  496. if (iwlfw_tx_data(f, o + plen, prot) < 0)
  497. return -1;
  498. W.tx_frames++;
  499. return 0;
  500. }
  501. static int tx_mgmt_frame(unsigned int stype, const unsigned char *body,
  502. unsigned int blen)
  503. {
  504. unsigned char *f = W.txbuf;
  505. if (24 + blen > 2048)
  506. return -1;
  507. iwl_memzero(f, 24);
  508. f[0] = stype << 4;
  509. wmemcpy(f + 4, W.cur.bssid, 6);
  510. wmemcpy(f + 10, W.mac, 6);
  511. wmemcpy(f + 16, W.cur.bssid, 6);
  512. wmemcpy(f + 24, body, blen);
  513. return iwlfw_tx_mgmt(f, 24 + blen);
  514. }
  515. static int wait_flag(int *flag, unsigned int ms)
  516. {
  517. unsigned int t;
  518. for (t = 0; t < ms && !*flag; t++) {
  519. if (iwlfw_poll() < 0)
  520. return -1;
  521. if (!*flag)
  522. iwl_udelay(1000);
  523. }
  524. return *flag ? 0 : -1;
  525. }
  526. static int do_connect(const struct sys_wifi_connect *c)
  527. {
  528. unsigned char body[160];
  529. unsigned int o, i, tries;
  530. static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
  531. 0x0c, 0x12, 0x18, 0x24,
  532. 50, 4, 0x30, 0x48, 0x60, 0x6c };
  533. static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
  534. 0xb0, 0x48, 0x60, 0x6c };
  535. if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0)
  536. return -1;
  537. if (W.state >= SYS_WIFI_ST_ASSOC) {
  538. iwlfw_disconnect();
  539. W.state = SYS_WIFI_ST_IDLE;
  540. }
  541. if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel)
  542. return -1;
  543. W.conn_id++;
  544. iwl_memzero(&W.cur, sizeof(W.cur));
  545. wmemcpy(W.cur.bssid, c->bssid, 6);
  546. W.cur.channel = c->channel;
  547. W.cur.ssid_len = c->ssid_len;
  548. wmemcpy(W.cur.ssid, c->ssid, c->ssid_len);
  549. W.cur.beacon_int = c->beacon_int ? c->beacon_int : 100;
  550. W.cur.dtim = c->dtim ? c->dtim : 1;
  551. W.protect = c->ie_len != 0;
  552. W.pn = 0;
  553. W.eapq_head = W.eapq_tail = 0;
  554. W.rxq_head = W.rxq_tail = 0;
  555. W.rx_dropped = 0;
  556. W.rx_logged_grp = W.rx_logged_uni = 0;
  557. W.beacon_log = 0;
  558. W.have_tk = W.have_gtk = 0;
  559. W.sw_rx_log = W.sw_fail_log = 0;
  560. W.ptk_conn = 0;
  561. if (iwlfw_prepare_connect(c->bssid, c->channel, W.cur.beacon_int) < 0) {
  562. W.detail = "firmware connect setup failed";
  563. return -1;
  564. }
  565. /* Open-system authentication: alg 0, seq 1, status 0. */
  566. for (tries = 0; tries < 3; tries++) {
  567. W.auth_done = 0;
  568. put16(body, 0);
  569. put16(body + 2, 1);
  570. put16(body + 4, 0);
  571. if (tx_mgmt_frame(11, body, 6) < 0)
  572. break;
  573. if (wait_flag(&W.auth_done, 300) == 0)
  574. break;
  575. }
  576. if (!W.auth_done || W.auth_status) {
  577. W.detail = W.auth_done ? "authentication refused"
  578. : "no authentication reply";
  579. iwlfw_disconnect();
  580. return -1;
  581. }
  582. /* Association request: capability, listen interval, SSID, rates,
  583. * [RSN IE]. */
  584. o = 0;
  585. put16(body, 0x0001 | (W.protect ? 0x0010 : 0) | 0x0400);
  586. put16(body + 2, 10);
  587. o = 4;
  588. body[o++] = 0;
  589. body[o++] = c->ssid_len;
  590. for (i = 0; i < c->ssid_len; i++)
  591. body[o++] = c->ssid[i];
  592. if (c->channel <= 14) {
  593. wmemcpy(body + o, r24, sizeof(r24));
  594. o += sizeof(r24);
  595. } else {
  596. wmemcpy(body + o, r5, sizeof(r5));
  597. o += sizeof(r5);
  598. }
  599. if (c->ie_len) {
  600. wmemcpy(body + o, c->ie, c->ie_len);
  601. o += c->ie_len;
  602. }
  603. for (tries = 0; tries < 3; tries++) {
  604. W.assoc_done = 0;
  605. if (tx_mgmt_frame(0, body, o) < 0)
  606. break;
  607. if (wait_flag(&W.assoc_done, 500) == 0)
  608. break;
  609. }
  610. if (!W.assoc_done || W.assoc_status) {
  611. W.detail = W.assoc_done ? "association refused"
  612. : "no association reply";
  613. if (W.assoc_done) {
  614. serial_puts(SERIAL_COM1, "wifi: assoc status ");
  615. serial_putdec(SERIAL_COM1, W.assoc_status);
  616. serial_puts(SERIAL_COM1, "\n");
  617. }
  618. iwlfw_disconnect();
  619. return -1;
  620. }
  621. if (iwlfw_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) {
  622. W.detail = "firmware association update failed";
  623. iwlfw_disconnect();
  624. return -1;
  625. }
  626. W.state = SYS_WIFI_ST_ASSOC;
  627. W.detail = W.protect ? "associated, waiting for keys" : "associated";
  628. serial_puts(SERIAL_COM1, "wifi: associated, aid ");
  629. serial_putdec(SERIAL_COM1, W.aid);
  630. serial_puts(SERIAL_COM1, "\n");
  631. return 0;
  632. }
  633. static void do_disconnect(void)
  634. {
  635. static const unsigned char reason[2] = { 3, 0 }; /* leaving */
  636. W.have_tk = W.have_gtk = 0;
  637. W.ptk_conn = 0;
  638. if (W.state >= SYS_WIFI_ST_ASSOC) {
  639. tx_mgmt_frame(12, reason, 2);
  640. iwlfw_poll();
  641. iwlfw_disconnect();
  642. }
  643. if (W.state != SYS_WIFI_ST_DOWN)
  644. W.state = SYS_WIFI_ST_IDLE;
  645. W.detail = "disconnected";
  646. }
  647. /* ---- netdev ------------------------------------------------------------- */
  648. static int nd_init(void)
  649. {
  650. if (W.state != SYS_WIFI_ST_DOWN)
  651. return 0;
  652. /* Boot probe: find the card only. The firmware is loaded on first
  653. * use (`wifi`), so boot time and RAM are unchanged without WiFi. */
  654. if (iwl_probe() < 0)
  655. return -1;
  656. W.present = 1;
  657. W.detail = "firmware not loaded";
  658. return 0;
  659. }
  660. static void nd_get_mac(unsigned char mac[6])
  661. {
  662. wmemcpy(mac, W.mac, 6);
  663. }
  664. static int nd_rx(unsigned char *buf, unsigned int maxlen)
  665. {
  666. struct rxq_slot *s;
  667. unsigned int n;
  668. wifi_check_crash();
  669. if (W.state == SYS_WIFI_ST_DOWN)
  670. return -1;
  671. if (W.rxq_head == W.rxq_tail)
  672. iwlfw_poll();
  673. if (W.rxq_head == W.rxq_tail)
  674. return -1;
  675. s = &W.rxq[W.rxq_tail];
  676. n = s->len < maxlen ? s->len : maxlen;
  677. wmemcpy(buf, s->data, n);
  678. W.rxq_tail = (W.rxq_tail + 1) % RXQ_SLOTS;
  679. return (int)n;
  680. }
  681. static int nd_tx(const unsigned char *buf, unsigned int len)
  682. {
  683. if (len < 14 || W.state != SYS_WIFI_ST_LINK)
  684. return -1;
  685. return tx_data(buf, (buf[12] << 8) | buf[13], buf + 14, len - 14);
  686. }
  687. static void print_hex6(const unsigned char *m)
  688. {
  689. unsigned int i;
  690. for (i = 0; i < 6; i++) {
  691. static const char hx[] = "0123456789abcdef";
  692. char s[4];
  693. s[0] = hx[m[i] >> 4];
  694. s[1] = hx[m[i] & 15];
  695. s[2] = i < 5 ? ':' : 0;
  696. s[3] = 0;
  697. io_putstr(s);
  698. }
  699. }
  700. static void print_dec(unsigned int v)
  701. {
  702. char b[12];
  703. int i = 11;
  704. b[i] = 0;
  705. do {
  706. b[--i] = (char)('0' + v % 10);
  707. v /= 10;
  708. } while (v && i > 0);
  709. io_putstr(b + i);
  710. }
  711. static void nd_print_info(void)
  712. {
  713. static const char *const st[] = { "down", "idle", "associated", "link up" };
  714. io_putstr("chip: ");
  715. io_putstr(iwl_chip_name());
  716. io_putstr("\nstate: ");
  717. io_putstr(st[W.state & 3]);
  718. io_putstr(" (");
  719. io_putstr(W.detail ? W.detail : "-");
  720. io_putstr(")\nmac: ");
  721. print_hex6(W.mac);
  722. if (W.state >= SYS_WIFI_ST_ASSOC) {
  723. char ssid[33];
  724. unsigned int i;
  725. for (i = 0; i < W.cur.ssid_len && i < 32; i++)
  726. ssid[i] = W.cur.ssid[i] >= 32 && W.cur.ssid[i] < 127
  727. ? (char)W.cur.ssid[i] : '?';
  728. ssid[i] = 0;
  729. io_putstr("\nbss: ");
  730. print_hex6(W.cur.bssid);
  731. io_putstr(" \"");
  732. io_putstr(ssid);
  733. io_putstr("\" ch ");
  734. print_dec(W.cur.channel);
  735. }
  736. io_putstr("\n");
  737. }
  738. static void nd_stop(void)
  739. {
  740. wifi_shutdown();
  741. }
  742. static int nd_link_up(void)
  743. {
  744. return W.state == SYS_WIFI_ST_LINK;
  745. }
  746. const struct netdev wifi_netdev = {
  747. "wifi",
  748. "Intel 7265D/3165 or 6200/6300 WiFi",
  749. nd_init,
  750. nd_get_mac,
  751. nd_rx,
  752. nd_tx,
  753. nd_print_info,
  754. 0,
  755. 0,
  756. 0,
  757. 0,
  758. 0,
  759. 0,
  760. 0,
  761. 0,
  762. nd_stop,
  763. nd_link_up,
  764. };
  765. void wifi_shutdown(void)
  766. {
  767. if (W.state != SYS_WIFI_ST_DOWN || iwl_present())
  768. iwlfw_down();
  769. if (W.state != SYS_WIFI_ST_DOWN)
  770. W.state = SYS_WIFI_ST_DOWN;
  771. }
  772. /* ---- SYS_WIFI ------------------------------------------------------------ */
  773. static int user_ok(int ptr, unsigned int len, int write)
  774. {
  775. return ptr && paging_user_range_ok(current_proc()->cr3,
  776. (unsigned int)ptr, len, write);
  777. }
  778. static void copy_str(char *dst, unsigned int max, const char *src)
  779. {
  780. unsigned int i;
  781. for (i = 0; src && src[i] && i + 1 < max; i++)
  782. dst[i] = src[i];
  783. dst[i] = 0;
  784. }
  785. int sys_wifi(int op, int a2, int a3)
  786. {
  787. wifi_check_crash();
  788. switch (op) {
  789. case SYS_WIFI_UP:
  790. if (!W.present && nd_init() < 0)
  791. return -1;
  792. return wifi_up();
  793. case SYS_WIFI_STATUS: {
  794. struct sys_wifi_status *s = (struct sys_wifi_status *)a2;
  795. const struct iwl_fw_info *fw;
  796. if (!user_ok(a2, sizeof(*s), 1))
  797. return -1;
  798. if (!W.present && iwl_probe() == 0)
  799. W.present = 1;
  800. iwl_memzero(s, sizeof(*s));
  801. s->state = W.state;
  802. s->present = (unsigned char)W.present;
  803. wmemcpy(s->mac, W.mac, 6);
  804. if (W.state >= SYS_WIFI_ST_ASSOC) {
  805. wmemcpy(s->bssid, W.cur.bssid, 6);
  806. s->channel = W.cur.channel;
  807. s->rssi = W.cur.rssi;
  808. s->ssid_len = W.cur.ssid_len;
  809. wmemcpy(s->ssid, W.cur.ssid, W.cur.ssid_len);
  810. }
  811. copy_str(s->chip, sizeof(s->chip), W.present ? iwl_chip_name() : "none");
  812. fw = iwl_fw();
  813. copy_str(s->fw, sizeof(s->fw), fw && fw->present ? fw->human : "");
  814. copy_str(s->detail, sizeof(s->detail),
  815. W.detail ? W.detail : (W.present ? "firmware not loaded"
  816. : "no card"));
  817. s->rx_frames = W.rx_frames;
  818. s->tx_frames = W.tx_frames;
  819. s->rx_dropped = W.rx_dropped;
  820. s->conn_id = W.conn_id;
  821. return 0;
  822. }
  823. case SYS_WIFI_SCAN:
  824. if (W.state == SYS_WIFI_ST_DOWN && sys_wifi(SYS_WIFI_UP, 0, 0) < 0)
  825. return -1;
  826. if (W.state >= SYS_WIFI_ST_ASSOC)
  827. return -1; /* scanning while associated: not yet */
  828. W.nbss = 0;
  829. return iwlfw_scan_start();
  830. case SYS_WIFI_RESULTS: {
  831. unsigned int max = (unsigned int)a3, n;
  832. if (W.state == SYS_WIFI_ST_DOWN)
  833. return -1;
  834. iwlfw_poll();
  835. if (iwlfw_scan_running())
  836. return -2;
  837. if (max > W.nbss)
  838. max = W.nbss;
  839. if (max && !user_ok(a2, max * sizeof(struct sys_wifi_bss), 1))
  840. return -1;
  841. for (n = 0; n < max; n++)
  842. ((struct sys_wifi_bss *)a2)[n] = W.bss[n];
  843. return (int)max;
  844. }
  845. case SYS_WIFI_CONNECT: {
  846. struct sys_wifi_connect c;
  847. if (!user_ok(a2, sizeof(c), 0))
  848. return -1;
  849. wmemcpy(&c, (const void *)a2, sizeof(c));
  850. return do_connect(&c);
  851. }
  852. case SYS_WIFI_DISCONNECT:
  853. do_disconnect();
  854. return 0;
  855. case SYS_WIFI_EAPOL_RX: {
  856. struct eapol_slot *s;
  857. unsigned int n;
  858. if (W.state < SYS_WIFI_ST_ASSOC)
  859. return -1;
  860. iwlfw_poll();
  861. if (W.eapq_head == W.eapq_tail)
  862. return 0;
  863. s = &W.eapq[W.eapq_tail];
  864. n = s->len < (unsigned int)a3 ? s->len : (unsigned int)a3;
  865. if (!user_ok(a2, n, 1))
  866. return -1;
  867. wmemcpy((void *)a2, s->data, n);
  868. W.eapq_tail = (W.eapq_tail + 1) % EAPOLQ_SLOTS;
  869. return (int)n;
  870. }
  871. case SYS_WIFI_EAPOL_TX: {
  872. unsigned int len = (unsigned int)a3;
  873. if (W.state < SYS_WIFI_ST_ASSOC || len > EAPOL_MAX ||
  874. !user_ok(a2, len, 0))
  875. return -1;
  876. /* Stage behind the 2 KB frame build area of the TX page. */
  877. wmemcpy(W.txbuf + 2048, (const void *)a2, len);
  878. return tx_data(W.cur.bssid, 0x888e, W.txbuf + 2048, len);
  879. }
  880. case SYS_WIFI_SET_KEY: {
  881. struct sys_wifi_key k;
  882. int r;
  883. if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(k), 0))
  884. return -1;
  885. wmemcpy(&k, (const void *)a2, sizeof(k));
  886. r = iwlfw_set_key(k.pairwise, k.keyidx, k.key,
  887. k.pairwise ? 0 : k.rsc);
  888. if (r == 0) {
  889. if (k.pairwise) {
  890. wmemcpy(W.tk, k.key, 16);
  891. W.have_tk = 1;
  892. } else {
  893. wmemcpy(W.gtk, k.key, 16);
  894. W.gtk_idx = k.keyidx & 3;
  895. W.have_gtk = 1;
  896. }
  897. }
  898. return r;
  899. }
  900. case SYS_WIFI_SET_PTK: {
  901. struct sys_wifi_ptk p;
  902. if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(p), 0))
  903. return -1;
  904. wmemcpy(&p, (const void *)a2, sizeof(p));
  905. wmemcpy(W.ptk, p.ptk, 48);
  906. wmemcpy(W.ptk_replay, p.replay, 8);
  907. W.ptk_conn = p.conn_id;
  908. return 0;
  909. }
  910. case SYS_WIFI_GET_PTK: {
  911. struct sys_wifi_ptk p;
  912. if (!W.ptk_conn || (unsigned int)a3 != W.ptk_conn ||
  913. !user_ok(a2, sizeof(p), 1))
  914. return -1;
  915. p.conn_id = W.ptk_conn;
  916. wmemcpy(p.ptk, W.ptk, 48);
  917. wmemcpy(p.replay, W.ptk_replay, 8);
  918. wmemcpy((void *)a2, &p, sizeof(p));
  919. return 0;
  920. }
  921. case SYS_WIFI_AUTHORIZE:
  922. if (W.state < SYS_WIFI_ST_ASSOC)
  923. return -1;
  924. W.state = SYS_WIFI_ST_LINK;
  925. W.detail = "connected";
  926. if (netdev_active() != &wifi_netdev && netdev_select("wifi") != 0) {
  927. W.detail = "connected (netdev busy)";
  928. return -1;
  929. }
  930. return 0;
  931. }
  932. return -1;
  933. }