wifi.c 21 KB

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