wifi.c 25 KB

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