wifi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. static void rx_data(const unsigned char *f, unsigned int len,
  241. unsigned int status)
  242. {
  243. static const unsigned char snap[6] = { 0xaa, 0xaa, 0x03, 0, 0, 0 };
  244. unsigned int hdr = 24, stype = (f[0] >> 4) & 0xf, type, plen;
  245. const unsigned char *p;
  246. struct rxq_slot *s;
  247. int prot = (f[1] & 0x40) != 0;
  248. if (W.state < SYS_WIFI_ST_ASSOC || (f[1] & 0x03) != 0x02)
  249. return; /* FromDS only */
  250. if (!meq(f + 10, W.cur.bssid, 6))
  251. return;
  252. if (stype & 4)
  253. return; /* null / no data */
  254. if (stype & 8)
  255. hdr += 2; /* QoS control */
  256. if (prot) {
  257. if ((status & IWL_RX_SEC_MSK) != IWL_RX_SEC_CCM ||
  258. !(status & IWL_RX_MIC_OK)) {
  259. W.rx_dropped++;
  260. return;
  261. }
  262. hdr += 8; /* CCMP header */
  263. if (len < hdr + 8)
  264. return;
  265. len -= 8; /* MIC */
  266. }
  267. if (len < hdr + 8 || !meq(f + hdr, snap, 6))
  268. return;
  269. type = (f[hdr + 6] << 8) | f[hdr + 7];
  270. p = f + hdr + 8;
  271. plen = len - hdr - 8;
  272. if (type == 0x888e) {
  273. queue_eapol(p, plen);
  274. return;
  275. }
  276. /* Before the keys are in, or unprotected after: drop. */
  277. if (W.state != SYS_WIFI_ST_LINK || (W.protect && !prot)) {
  278. W.rx_dropped++;
  279. return;
  280. }
  281. if (plen + 14 > RXQ_FRAME ||
  282. ((W.rxq_head + 1) % RXQ_SLOTS) == W.rxq_tail) {
  283. W.rx_dropped++;
  284. return;
  285. }
  286. s = &W.rxq[W.rxq_head];
  287. wmemcpy(s->data, f + 4, 6); /* DA = A1 */
  288. wmemcpy(s->data + 6, f + 16, 6); /* SA = A3 */
  289. s->data[12] = type >> 8;
  290. s->data[13] = type;
  291. wmemcpy(s->data + 14, p, plen);
  292. s->len = plen + 14;
  293. W.rxq_head = (W.rxq_head + 1) % RXQ_SLOTS;
  294. W.rx_frames++;
  295. }
  296. void wifi_rx_frame(const unsigned char *frame, unsigned int len, int rssi,
  297. unsigned int status, unsigned int channel)
  298. {
  299. unsigned int type;
  300. if (len < 24)
  301. return;
  302. type = (frame[0] >> 2) & 3;
  303. if (type == 0)
  304. rx_mgmt(frame, len, rssi, channel);
  305. else if (type == 2)
  306. rx_data(frame, len, status);
  307. }
  308. void wifi_scan_done(int ok)
  309. {
  310. if (!ok)
  311. serial_puts(SERIAL_COM1, "wifi: scan aborted\n");
  312. }
  313. /* ---- TX ---------------------------------------------------------------- */
  314. /* Build and send an 802.11 data frame to the AP: header, [CCMP header],
  315. * LLC/SNAP, ethertype, payload. */
  316. static int tx_data(const unsigned char *dst, unsigned int ethertype,
  317. const unsigned char *payload, unsigned int plen)
  318. {
  319. unsigned char *f = W.txbuf;
  320. unsigned int o = 24;
  321. int prot = W.protect && W.state == SYS_WIFI_ST_LINK;
  322. if (W.state < SYS_WIFI_ST_ASSOC || plen + 24 + 8 + 8 > 2048)
  323. return -1;
  324. iwl_memzero(f, 24);
  325. f[0] = 0x08; /* data */
  326. f[1] = 0x01 | (prot ? 0x40 : 0); /* ToDS [| protected] */
  327. wmemcpy(f + 4, W.cur.bssid, 6);
  328. wmemcpy(f + 10, W.mac, 6);
  329. wmemcpy(f + 16, dst, 6);
  330. if (prot) {
  331. unsigned long long pn = ++W.pn;
  332. f[o + 0] = pn;
  333. f[o + 1] = pn >> 8;
  334. f[o + 2] = 0;
  335. f[o + 3] = 0x20; /* ExtIV, key id 0 */
  336. f[o + 4] = pn >> 16;
  337. f[o + 5] = pn >> 24;
  338. f[o + 6] = pn >> 32;
  339. f[o + 7] = pn >> 40;
  340. o += 8;
  341. }
  342. f[o++] = 0xaa; f[o++] = 0xaa; f[o++] = 0x03;
  343. f[o++] = 0; f[o++] = 0; f[o++] = 0;
  344. f[o++] = ethertype >> 8;
  345. f[o++] = ethertype;
  346. wmemcpy(f + o, payload, plen);
  347. if (iwl_mvm_tx_data(f, o + plen, prot) < 0)
  348. return -1;
  349. W.tx_frames++;
  350. return 0;
  351. }
  352. static int tx_mgmt_frame(unsigned int stype, const unsigned char *body,
  353. unsigned int blen)
  354. {
  355. unsigned char *f = W.txbuf;
  356. if (24 + blen > 2048)
  357. return -1;
  358. iwl_memzero(f, 24);
  359. f[0] = stype << 4;
  360. wmemcpy(f + 4, W.cur.bssid, 6);
  361. wmemcpy(f + 10, W.mac, 6);
  362. wmemcpy(f + 16, W.cur.bssid, 6);
  363. wmemcpy(f + 24, body, blen);
  364. return iwl_mvm_tx_mgmt(f, 24 + blen);
  365. }
  366. static int wait_flag(int *flag, unsigned int ms)
  367. {
  368. unsigned int t;
  369. for (t = 0; t < ms && !*flag; t++) {
  370. if (iwl_mvm_poll() < 0)
  371. return -1;
  372. if (!*flag)
  373. iwl_udelay(1000);
  374. }
  375. return *flag ? 0 : -1;
  376. }
  377. static int do_connect(const struct sys_wifi_connect *c)
  378. {
  379. unsigned char body[160];
  380. unsigned int o, i, tries;
  381. static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
  382. 0x0c, 0x12, 0x18, 0x24,
  383. 50, 4, 0x30, 0x48, 0x60, 0x6c };
  384. static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
  385. 0xb0, 0x48, 0x60, 0x6c };
  386. if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0)
  387. return -1;
  388. if (W.state >= SYS_WIFI_ST_ASSOC) {
  389. iwl_mvm_disconnect();
  390. W.state = SYS_WIFI_ST_IDLE;
  391. }
  392. if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel)
  393. return -1;
  394. W.conn_id++;
  395. iwl_memzero(&W.cur, sizeof(W.cur));
  396. wmemcpy(W.cur.bssid, c->bssid, 6);
  397. W.cur.channel = c->channel;
  398. W.cur.ssid_len = c->ssid_len;
  399. wmemcpy(W.cur.ssid, c->ssid, c->ssid_len);
  400. W.cur.beacon_int = c->beacon_int ? c->beacon_int : 100;
  401. W.cur.dtim = c->dtim ? c->dtim : 1;
  402. W.protect = c->ie_len != 0;
  403. W.pn = 0;
  404. W.eapq_head = W.eapq_tail = 0;
  405. W.rxq_head = W.rxq_tail = 0;
  406. if (iwl_mvm_prepare_connect(c->bssid, c->channel, W.cur.beacon_int) < 0) {
  407. W.detail = "firmware connect setup failed";
  408. return -1;
  409. }
  410. /* Open-system authentication: alg 0, seq 1, status 0. */
  411. for (tries = 0; tries < 3; tries++) {
  412. W.auth_done = 0;
  413. put16(body, 0);
  414. put16(body + 2, 1);
  415. put16(body + 4, 0);
  416. if (tx_mgmt_frame(11, body, 6) < 0)
  417. break;
  418. if (wait_flag(&W.auth_done, 300) == 0)
  419. break;
  420. }
  421. if (!W.auth_done || W.auth_status) {
  422. W.detail = W.auth_done ? "authentication refused"
  423. : "no authentication reply";
  424. iwl_mvm_disconnect();
  425. return -1;
  426. }
  427. /* Association request: capability, listen interval, SSID, rates,
  428. * [RSN IE]. */
  429. o = 0;
  430. put16(body, 0x0001 | (W.protect ? 0x0010 : 0) | 0x0400);
  431. put16(body + 2, 10);
  432. o = 4;
  433. body[o++] = 0;
  434. body[o++] = c->ssid_len;
  435. for (i = 0; i < c->ssid_len; i++)
  436. body[o++] = c->ssid[i];
  437. if (c->channel <= 14) {
  438. wmemcpy(body + o, r24, sizeof(r24));
  439. o += sizeof(r24);
  440. } else {
  441. wmemcpy(body + o, r5, sizeof(r5));
  442. o += sizeof(r5);
  443. }
  444. if (c->ie_len) {
  445. wmemcpy(body + o, c->ie, c->ie_len);
  446. o += c->ie_len;
  447. }
  448. for (tries = 0; tries < 3; tries++) {
  449. W.assoc_done = 0;
  450. if (tx_mgmt_frame(0, body, o) < 0)
  451. break;
  452. if (wait_flag(&W.assoc_done, 500) == 0)
  453. break;
  454. }
  455. if (!W.assoc_done || W.assoc_status) {
  456. W.detail = W.assoc_done ? "association refused"
  457. : "no association reply";
  458. if (W.assoc_done) {
  459. serial_puts(SERIAL_COM1, "wifi: assoc status ");
  460. serial_putdec(SERIAL_COM1, W.assoc_status);
  461. serial_puts(SERIAL_COM1, "\n");
  462. }
  463. iwl_mvm_disconnect();
  464. return -1;
  465. }
  466. if (iwl_mvm_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) {
  467. W.detail = "firmware association update failed";
  468. iwl_mvm_disconnect();
  469. return -1;
  470. }
  471. W.state = SYS_WIFI_ST_ASSOC;
  472. W.detail = W.protect ? "associated, waiting for keys" : "associated";
  473. serial_puts(SERIAL_COM1, "wifi: associated, aid ");
  474. serial_putdec(SERIAL_COM1, W.aid);
  475. serial_puts(SERIAL_COM1, "\n");
  476. return 0;
  477. }
  478. static void do_disconnect(void)
  479. {
  480. static const unsigned char reason[2] = { 3, 0 }; /* leaving */
  481. if (W.state >= SYS_WIFI_ST_ASSOC) {
  482. tx_mgmt_frame(12, reason, 2);
  483. iwl_mvm_poll();
  484. iwl_mvm_disconnect();
  485. }
  486. if (W.state != SYS_WIFI_ST_DOWN)
  487. W.state = SYS_WIFI_ST_IDLE;
  488. W.detail = "disconnected";
  489. }
  490. /* ---- netdev ------------------------------------------------------------- */
  491. static int nd_init(void)
  492. {
  493. if (W.state != SYS_WIFI_ST_DOWN)
  494. return 0;
  495. /* Boot probe: find the card only. The firmware is loaded on first
  496. * use (`wifi`), so boot time and RAM are unchanged without WiFi. */
  497. if (iwl_probe() < 0)
  498. return -1;
  499. W.present = 1;
  500. W.detail = "firmware not loaded";
  501. return 0;
  502. }
  503. static void nd_get_mac(unsigned char mac[6])
  504. {
  505. wmemcpy(mac, W.mac, 6);
  506. }
  507. static int nd_rx(unsigned char *buf, unsigned int maxlen)
  508. {
  509. struct rxq_slot *s;
  510. unsigned int n;
  511. if (W.state == SYS_WIFI_ST_DOWN)
  512. return -1;
  513. if (W.rxq_head == W.rxq_tail)
  514. iwl_mvm_poll();
  515. if (W.rxq_head == W.rxq_tail)
  516. return -1;
  517. s = &W.rxq[W.rxq_tail];
  518. n = s->len < maxlen ? s->len : maxlen;
  519. wmemcpy(buf, s->data, n);
  520. W.rxq_tail = (W.rxq_tail + 1) % RXQ_SLOTS;
  521. return (int)n;
  522. }
  523. static int nd_tx(const unsigned char *buf, unsigned int len)
  524. {
  525. if (len < 14 || W.state != SYS_WIFI_ST_LINK)
  526. return -1;
  527. return tx_data(buf, (buf[12] << 8) | buf[13], buf + 14, len - 14);
  528. }
  529. static void print_hex6(const unsigned char *m)
  530. {
  531. unsigned int i;
  532. for (i = 0; i < 6; i++) {
  533. static const char hx[] = "0123456789abcdef";
  534. char s[4];
  535. s[0] = hx[m[i] >> 4];
  536. s[1] = hx[m[i] & 15];
  537. s[2] = i < 5 ? ':' : 0;
  538. s[3] = 0;
  539. io_putstr(s);
  540. }
  541. }
  542. static void print_dec(unsigned int v)
  543. {
  544. char b[12];
  545. int i = 11;
  546. b[i] = 0;
  547. do {
  548. b[--i] = (char)('0' + v % 10);
  549. v /= 10;
  550. } while (v && i > 0);
  551. io_putstr(b + i);
  552. }
  553. static void nd_print_info(void)
  554. {
  555. static const char *const st[] = { "down", "idle", "associated", "link up" };
  556. io_putstr("chip: ");
  557. io_putstr(iwl_chip_name());
  558. io_putstr("\nstate: ");
  559. io_putstr(st[W.state & 3]);
  560. io_putstr(" (");
  561. io_putstr(W.detail ? W.detail : "-");
  562. io_putstr(")\nmac: ");
  563. print_hex6(W.mac);
  564. if (W.state >= SYS_WIFI_ST_ASSOC) {
  565. char ssid[33];
  566. unsigned int i;
  567. for (i = 0; i < W.cur.ssid_len && i < 32; i++)
  568. ssid[i] = W.cur.ssid[i] >= 32 && W.cur.ssid[i] < 127
  569. ? (char)W.cur.ssid[i] : '?';
  570. ssid[i] = 0;
  571. io_putstr("\nbss: ");
  572. print_hex6(W.cur.bssid);
  573. io_putstr(" \"");
  574. io_putstr(ssid);
  575. io_putstr("\" ch ");
  576. print_dec(W.cur.channel);
  577. }
  578. io_putstr("\n");
  579. }
  580. static void nd_stop(void)
  581. {
  582. wifi_shutdown();
  583. }
  584. static int nd_link_up(void)
  585. {
  586. return W.state == SYS_WIFI_ST_LINK;
  587. }
  588. const struct netdev wifi_netdev = {
  589. "wifi",
  590. "Intel 7265D/3165 WiFi",
  591. nd_init,
  592. nd_get_mac,
  593. nd_rx,
  594. nd_tx,
  595. nd_print_info,
  596. 0,
  597. 0,
  598. 0,
  599. 0,
  600. 0,
  601. 0,
  602. 0,
  603. 0,
  604. nd_stop,
  605. nd_link_up,
  606. };
  607. void wifi_shutdown(void)
  608. {
  609. if (W.state != SYS_WIFI_ST_DOWN || iwl_present())
  610. iwl_mvm_down();
  611. if (W.state != SYS_WIFI_ST_DOWN)
  612. W.state = SYS_WIFI_ST_DOWN;
  613. }
  614. /* ---- SYS_WIFI ------------------------------------------------------------ */
  615. static int user_ok(int ptr, unsigned int len, int write)
  616. {
  617. return ptr && paging_user_range_ok(current_proc()->cr3,
  618. (unsigned int)ptr, len, write);
  619. }
  620. static void copy_str(char *dst, unsigned int max, const char *src)
  621. {
  622. unsigned int i;
  623. for (i = 0; src && src[i] && i + 1 < max; i++)
  624. dst[i] = src[i];
  625. dst[i] = 0;
  626. }
  627. int sys_wifi(int op, int a2, int a3)
  628. {
  629. switch (op) {
  630. case SYS_WIFI_UP:
  631. if (!W.present && nd_init() < 0)
  632. return -1;
  633. return wifi_up();
  634. case SYS_WIFI_STATUS: {
  635. struct sys_wifi_status *s = (struct sys_wifi_status *)a2;
  636. const struct iwl_fw_info *fw;
  637. if (!user_ok(a2, sizeof(*s), 1))
  638. return -1;
  639. if (!W.present && iwl_probe() == 0)
  640. W.present = 1;
  641. iwl_memzero(s, sizeof(*s));
  642. s->state = W.state;
  643. s->present = (unsigned char)W.present;
  644. wmemcpy(s->mac, W.mac, 6);
  645. if (W.state >= SYS_WIFI_ST_ASSOC) {
  646. wmemcpy(s->bssid, W.cur.bssid, 6);
  647. s->channel = W.cur.channel;
  648. s->rssi = W.cur.rssi;
  649. s->ssid_len = W.cur.ssid_len;
  650. wmemcpy(s->ssid, W.cur.ssid, W.cur.ssid_len);
  651. }
  652. copy_str(s->chip, sizeof(s->chip), W.present ? iwl_chip_name() : "none");
  653. fw = iwl_fw();
  654. copy_str(s->fw, sizeof(s->fw), fw && fw->present ? fw->human : "");
  655. copy_str(s->detail, sizeof(s->detail),
  656. W.detail ? W.detail : (W.present ? "firmware not loaded"
  657. : "no card"));
  658. s->rx_frames = W.rx_frames;
  659. s->tx_frames = W.tx_frames;
  660. s->rx_dropped = W.rx_dropped;
  661. s->conn_id = W.conn_id;
  662. return 0;
  663. }
  664. case SYS_WIFI_SCAN:
  665. if (W.state == SYS_WIFI_ST_DOWN && sys_wifi(SYS_WIFI_UP, 0, 0) < 0)
  666. return -1;
  667. if (W.state >= SYS_WIFI_ST_ASSOC)
  668. return -1; /* scanning while associated: not yet */
  669. W.nbss = 0;
  670. return iwl_mvm_scan_start();
  671. case SYS_WIFI_RESULTS: {
  672. unsigned int max = (unsigned int)a3, n;
  673. if (W.state == SYS_WIFI_ST_DOWN)
  674. return -1;
  675. iwl_mvm_poll();
  676. if (iwl_mvm_scan_running())
  677. return -2;
  678. if (max > W.nbss)
  679. max = W.nbss;
  680. if (max && !user_ok(a2, max * sizeof(struct sys_wifi_bss), 1))
  681. return -1;
  682. for (n = 0; n < max; n++)
  683. ((struct sys_wifi_bss *)a2)[n] = W.bss[n];
  684. return (int)max;
  685. }
  686. case SYS_WIFI_CONNECT: {
  687. struct sys_wifi_connect c;
  688. if (!user_ok(a2, sizeof(c), 0))
  689. return -1;
  690. wmemcpy(&c, (const void *)a2, sizeof(c));
  691. return do_connect(&c);
  692. }
  693. case SYS_WIFI_DISCONNECT:
  694. do_disconnect();
  695. return 0;
  696. case SYS_WIFI_EAPOL_RX: {
  697. struct eapol_slot *s;
  698. unsigned int n;
  699. if (W.state < SYS_WIFI_ST_ASSOC)
  700. return -1;
  701. iwl_mvm_poll();
  702. if (W.eapq_head == W.eapq_tail)
  703. return 0;
  704. s = &W.eapq[W.eapq_tail];
  705. n = s->len < (unsigned int)a3 ? s->len : (unsigned int)a3;
  706. if (!user_ok(a2, n, 1))
  707. return -1;
  708. wmemcpy((void *)a2, s->data, n);
  709. W.eapq_tail = (W.eapq_tail + 1) % EAPOLQ_SLOTS;
  710. return (int)n;
  711. }
  712. case SYS_WIFI_EAPOL_TX: {
  713. unsigned int len = (unsigned int)a3;
  714. if (W.state < SYS_WIFI_ST_ASSOC || len > EAPOL_MAX ||
  715. !user_ok(a2, len, 0))
  716. return -1;
  717. /* Stage behind the 2 KB frame build area of the TX page. */
  718. wmemcpy(W.txbuf + 2048, (const void *)a2, len);
  719. return tx_data(W.cur.bssid, 0x888e, W.txbuf + 2048, len);
  720. }
  721. case SYS_WIFI_SET_KEY: {
  722. struct sys_wifi_key k;
  723. if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(k), 0))
  724. return -1;
  725. wmemcpy(&k, (const void *)a2, sizeof(k));
  726. return iwl_mvm_set_key(k.pairwise, k.keyidx, k.key,
  727. k.pairwise ? 0 : k.rsc);
  728. }
  729. case SYS_WIFI_AUTHORIZE:
  730. if (W.state < SYS_WIFI_ST_ASSOC)
  731. return -1;
  732. W.state = SYS_WIFI_ST_LINK;
  733. W.detail = "connected";
  734. if (netdev_active() != &wifi_netdev && netdev_select("wifi") != 0) {
  735. W.detail = "connected (netdev busy)";
  736. return -1;
  737. }
  738. return 0;
  739. }
  740. return -1;
  741. }