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