ucmd_wifi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /* wifi: ring-3 WiFi command for the Intel 7265D/3165 (see wifi.c).
  2. *
  3. * wifi status
  4. * wifi up load the firmware, show the MAC address
  5. * wifi scan list networks (strongest first)
  6. * wifi connect <ssid> [pass] [bssid]
  7. * join an open or WPA2-PSK (CCMP) network,
  8. * run DHCP; quote an SSID with spaces; a
  9. * BSSID picks one of several APs
  10. * wifi connectfile <path> join using an SSID/passphrase/BSSID record
  11. * staged by the network menu (SYS_SPAWN's
  12. * argument buffer is only 64 bytes)
  13. * wifi disconnect
  14. *
  15. * The WPA2 handshake (wpa.c) runs here; a small background copy of this
  16. * command (`wifi __keep <state>`) stays alive after `connect` to answer
  17. * group-key rekeys, and exits when the connection changes. */
  18. #include "ucmd_lib.h"
  19. #include "tls_rng.h"
  20. #include "wifi_cfg.h"
  21. #include "wpa.h"
  22. #define MAX_BSS 32
  23. static struct sys_wifi_bss bss[MAX_BSS];
  24. static int wifi(int op, int a2, int a3)
  25. {
  26. return u_syscall3(SYS_WIFI, op, a2, a3);
  27. }
  28. static void put_ssid(const unsigned char *s, unsigned int n)
  29. {
  30. unsigned int i;
  31. for (i = 0; i < n; i++)
  32. u_putc(s[i] >= 32 && s[i] < 127 ? (char)s[i] : '?');
  33. }
  34. static int slen(const char *s)
  35. {
  36. int n = 0;
  37. while (s[n])
  38. n++;
  39. return n;
  40. }
  41. static int starts(const char *s, const char *p)
  42. {
  43. while (*p)
  44. if (*s++ != *p++)
  45. return 0;
  46. return 1;
  47. }
  48. static const char *skip_ws(const char *s)
  49. {
  50. while (*s == ' ')
  51. s++;
  52. return s;
  53. }
  54. /* Next word or "quoted string" into out; returns the rest. */
  55. static const char *word(const char *s, char *out, unsigned int max)
  56. {
  57. unsigned int n = 0;
  58. char end = ' ';
  59. s = skip_ws(s);
  60. if (*s == '"') {
  61. end = '"';
  62. s++;
  63. }
  64. while (*s && *s != end) {
  65. if (end == '"' && *s == '\\' && s[1])
  66. s++;
  67. if (n + 1 < max)
  68. out[n++] = *s;
  69. s++;
  70. }
  71. if (*s == end)
  72. s++;
  73. out[n] = 0;
  74. return s;
  75. }
  76. static void put_dbm(int v)
  77. {
  78. if (v < 0) {
  79. u_putc('-');
  80. v = -v;
  81. }
  82. u_putdec((unsigned int)v);
  83. u_puts(" dBm");
  84. }
  85. static const char *sec_name(unsigned int s)
  86. {
  87. return s == SYS_WIFI_SEC_OPEN ? "open" :
  88. s == SYS_WIFI_SEC_PSK ? "WPA2" : "other";
  89. }
  90. static int cmd_status(void)
  91. {
  92. struct sys_wifi_status st;
  93. static const char *const names[] = { "down", "idle", "associated",
  94. "connected" };
  95. unsigned char ip[4];
  96. if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0) {
  97. u_puts("wifi: status failed\n");
  98. return 1;
  99. }
  100. if (!st.present) {
  101. u_puts("wifi: no Intel 7265/3165 card found\n");
  102. return 1;
  103. }
  104. u_puts("chip: "); u_puts(st.chip);
  105. u_puts("\nstate: "); u_puts(names[st.state & 3]);
  106. u_puts(" ("); u_puts(st.detail); u_puts(")\n");
  107. if (st.fw[0]) {
  108. u_puts("firmware: "); u_puts(st.fw); u_puts("\n");
  109. }
  110. if (st.state != SYS_WIFI_ST_DOWN) {
  111. u_puts("mac: "); u_put_mac(st.mac); u_puts("\n");
  112. }
  113. if (st.state >= SYS_WIFI_ST_ASSOC) {
  114. u_puts("network: \""); put_ssid(st.ssid, st.ssid_len);
  115. u_puts("\" "); u_put_mac(st.bssid);
  116. u_puts(" ch "); u_putdec(st.channel);
  117. u_puts(" "); put_dbm(st.rssi); u_puts("\n");
  118. u_syscall1(SYS_NET_GET_IP, (int)ip);
  119. u_puts("ip: "); u_put_ip(ip); u_puts("\n");
  120. u_puts("frames: rx "); u_putdec(st.rx_frames);
  121. u_puts(" tx "); u_putdec(st.tx_frames);
  122. u_puts(" dropped "); u_putdec(st.rx_dropped); u_puts("\n");
  123. }
  124. return 0;
  125. }
  126. static int bring_up(void)
  127. {
  128. struct sys_wifi_status st;
  129. u_puts("wifi: loading firmware...\n");
  130. if (wifi(SYS_WIFI_UP, 0, 0) < 0) {
  131. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  132. u_puts("wifi: bring-up failed: ");
  133. u_puts(st.detail);
  134. u_puts(" (details on the serial/netmon log)\n");
  135. return -1;
  136. }
  137. return 0;
  138. }
  139. /* Scan and return the number of entries, sorted by RSSI. */
  140. static int do_scan(void)
  141. {
  142. int n, i, j;
  143. unsigned int t0;
  144. if (wifi(SYS_WIFI_SCAN, 0, 0) < 0) {
  145. u_puts("wifi: scan failed (disconnect first?)\n");
  146. return -1;
  147. }
  148. t0 = u_ticks_ms();
  149. while ((n = wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS)) == -2) {
  150. if (u_ticks_ms() - t0 > 15000) {
  151. u_puts("wifi: scan timed out\n");
  152. return -1;
  153. }
  154. u_sleep_ms(100);
  155. }
  156. if (n < 0)
  157. return -1;
  158. for (i = 1; i < n; i++)
  159. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  160. struct sys_wifi_bss t = bss[j];
  161. bss[j] = bss[j - 1];
  162. bss[j - 1] = t;
  163. }
  164. return n;
  165. }
  166. static int cmd_scan(void)
  167. {
  168. int n, i;
  169. if (bring_up() < 0)
  170. return 1;
  171. u_puts("wifi: scanning...\n");
  172. n = do_scan();
  173. if (n < 0)
  174. return 1;
  175. if (n == 0)
  176. u_puts("no networks found\n");
  177. for (i = 0; i < n; i++) {
  178. u_put_mac(bss[i].bssid);
  179. u_puts(" ");
  180. put_dbm(bss[i].rssi);
  181. u_puts(bss[i].channel < 10 ? " ch " :
  182. bss[i].channel < 100 ? " ch " : " ch ");
  183. u_putdec(bss[i].channel);
  184. u_puts(" ");
  185. u_puts(sec_name(bss[i].security));
  186. u_puts(bss[i].security == SYS_WIFI_SEC_PSK ? " " : " ");
  187. if (bss[i].ssid_len)
  188. put_ssid(bss[i].ssid, bss[i].ssid_len);
  189. else
  190. u_puts("(hidden)");
  191. u_puts("\n");
  192. }
  193. return 0;
  194. }
  195. static int hexval(char c)
  196. {
  197. if (c >= '0' && c <= '9')
  198. return c - '0';
  199. if (c >= 'a' && c <= 'f')
  200. return c - 'a' + 10;
  201. if (c >= 'A' && c <= 'F')
  202. return c - 'A' + 10;
  203. return -1;
  204. }
  205. static int unhex(const char *s, unsigned char *out, unsigned int n)
  206. {
  207. unsigned int i;
  208. for (i = 0; i < n; i++) {
  209. int a = hexval(s[2 * i]), b = hexval(s[2 * i + 1]);
  210. if (a < 0 || b < 0)
  211. return -1;
  212. out[i] = (unsigned char)(a * 16 + b);
  213. }
  214. return 0;
  215. }
  216. static void tohex(const unsigned char *in, unsigned int n, char *out)
  217. {
  218. static const char hx[] = "0123456789abcdef";
  219. unsigned int i;
  220. for (i = 0; i < n; i++) {
  221. out[2 * i] = hx[in[i] >> 4];
  222. out[2 * i + 1] = hx[in[i] & 15];
  223. }
  224. out[2 * n] = 0;
  225. }
  226. static int set_gtk(struct wpa_sm *sm)
  227. {
  228. struct sys_wifi_key k;
  229. unsigned int i;
  230. k.pairwise = 0;
  231. k.keyidx = sm->gtk_idx;
  232. for (i = 0; i < 16; i++)
  233. k.key[i] = sm->gtk[i];
  234. for (i = 0; i < 8; i++)
  235. k.rsc[i] = sm->gtk_rsc[i];
  236. return wifi(SYS_WIFI_SET_KEY, (int)&k, 0);
  237. }
  238. /* Run the 4-way handshake: msg1 -> msg2, msg3 -> msg4 + keys. */
  239. static int handshake(struct wpa_sm *sm)
  240. {
  241. static unsigned char frame[1024], reply[256];
  242. unsigned int t0 = u_ticks_ms(), rlen;
  243. int n, r;
  244. while (u_ticks_ms() - t0 < 8000) {
  245. n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
  246. if (n < 0)
  247. return -1;
  248. if (n == 0) {
  249. u_sleep_ms(20);
  250. continue;
  251. }
  252. r = wpa_rx(sm, frame, (unsigned int)n, reply, &rlen, tls_random);
  253. if (r == WPA_ERR) {
  254. u_puts("wifi: handshake: ");
  255. u_puts(sm->error ? sm->error : "error");
  256. u_puts("\n");
  257. return -1;
  258. }
  259. if (r == WPA_NONE)
  260. continue;
  261. if (wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen) < 0)
  262. return -1;
  263. if (r == WPA_PTK) {
  264. struct sys_wifi_key k;
  265. unsigned int i;
  266. k.pairwise = 1;
  267. k.keyidx = 0;
  268. for (i = 0; i < 16; i++)
  269. k.key[i] = WPA_TK(sm)[i];
  270. for (i = 0; i < 8; i++)
  271. k.rsc[i] = 0;
  272. u_sleep_ms(20); /* let message 4 leave in the clear */
  273. if (wifi(SYS_WIFI_SET_KEY, (int)&k, 0) < 0 ||
  274. set_gtk(sm) < 0) {
  275. u_puts("wifi: key install failed\n");
  276. return -1;
  277. }
  278. return 0;
  279. }
  280. }
  281. u_puts("wifi: handshake timed out (no EAPOL from the AP)\n");
  282. return -1;
  283. }
  284. static void spawn_keeper(struct wpa_sm *sm, unsigned int conn_id)
  285. {
  286. static char arg[16];
  287. struct sys_wifi_ptk p;
  288. unsigned char id[4];
  289. char *q = arg;
  290. unsigned int i;
  291. p.conn_id = conn_id;
  292. for (i = 0; i < 48; i++)
  293. p.ptk[i] = sm->ptk[i];
  294. for (i = 0; i < 8; i++)
  295. p.replay[i] = sm->replay[i];
  296. if (wifi(SYS_WIFI_SET_PTK, (int)&p, 0) < 0) {
  297. u_puts("wifi: note: could not store rekey state; the link will "
  298. "drop at the next group rekey\n");
  299. return;
  300. }
  301. for (i = 0; "__keep "[i]; i++)
  302. *q++ = "__keep "[i];
  303. id[0] = conn_id >> 24;
  304. id[1] = conn_id >> 16;
  305. id[2] = conn_id >> 8;
  306. id[3] = conn_id;
  307. tohex(id, 4, q);
  308. q += 8;
  309. *q = 0;
  310. if (u_spawn("/ramfs/bin/wifi", arg, 0, "/") < 0)
  311. u_puts("wifi: note: rekey helper did not start; the link will "
  312. "drop at the next group rekey\n");
  313. }
  314. static int connect_bss(const char *ssid, const char *pass,
  315. const unsigned char *want_bssid)
  316. {
  317. struct sys_wifi_connect c;
  318. struct sys_wifi_status st;
  319. static struct wpa_sm sm;
  320. unsigned char pmk[32];
  321. struct sys_wifi_bss *b = 0;
  322. int n, i, r;
  323. unsigned int ssid_len;
  324. int have_bssid = want_bssid != 0;
  325. ssid_len = (unsigned int)slen(ssid);
  326. if (!ssid_len) {
  327. u_puts("usage: wifi connect <ssid> [passphrase]\n");
  328. return 1;
  329. }
  330. if (bring_up() < 0)
  331. return 1;
  332. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  333. u_puts("wifi: scanning for \""); u_puts(ssid); u_puts("\"...\n");
  334. n = do_scan();
  335. if (n < 0)
  336. return 1;
  337. for (i = 0; i < n; i++) {
  338. unsigned int j;
  339. if (bss[i].ssid_len != ssid_len)
  340. continue;
  341. if (have_bssid) {
  342. for (j = 0; j < 6 && bss[i].bssid[j] == want_bssid[j]; j++)
  343. ;
  344. if (j != 6)
  345. continue;
  346. }
  347. for (j = 0; j < ssid_len && bss[i].ssid[j] == (unsigned char)ssid[j]; j++)
  348. ;
  349. if (j == ssid_len) {
  350. b = &bss[i]; /* sorted: first match is strongest */
  351. break;
  352. }
  353. }
  354. if (!b) {
  355. u_puts("wifi: network not found\n");
  356. return 1;
  357. }
  358. if (b->security == SYS_WIFI_SEC_OTHER) {
  359. u_puts("wifi: unsupported security (only open and WPA2-PSK/CCMP)\n");
  360. return 1;
  361. }
  362. if (b->security == SYS_WIFI_SEC_PSK && !pass[0]) {
  363. u_puts("wifi: this network needs a passphrase\n");
  364. return 1;
  365. }
  366. for (i = 0; i < (int)sizeof(c); i++)
  367. ((unsigned char *)&c)[i] = 0;
  368. for (i = 0; i < 6; i++)
  369. c.bssid[i] = b->bssid[i];
  370. c.channel = b->channel;
  371. c.ssid_len = ssid_len;
  372. for (i = 0; i < (int)ssid_len; i++)
  373. c.ssid[i] = (unsigned char)ssid[i];
  374. c.beacon_int = b->beacon_int;
  375. c.dtim = b->dtim;
  376. if (b->security == SYS_WIFI_SEC_PSK) {
  377. int plen = slen(pass);
  378. if (plen == 64 && unhex(pass, pmk, 32) == 0) {
  379. /* raw 256-bit PSK */
  380. } else if (plen >= 8 && plen <= 63) {
  381. u_puts("wifi: deriving key...\n");
  382. wpa_pmk(pass, c.ssid, ssid_len, pmk);
  383. } else {
  384. u_puts("wifi: a WPA2 passphrase has 8..63 characters\n");
  385. return 1;
  386. }
  387. c.ie_len = (unsigned char)wpa_rsn_ie(c.ie);
  388. }
  389. u_puts("wifi: joining "); u_put_mac(c.bssid);
  390. u_puts(" on channel "); u_putdec(c.channel); u_puts("\n");
  391. if (wifi(SYS_WIFI_CONNECT, (int)&c, 0) < 0) {
  392. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  393. u_puts("wifi: "); u_puts(st.detail); u_puts("\n");
  394. return 1;
  395. }
  396. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  397. if (b->security == SYS_WIFI_SEC_PSK) {
  398. wpa_init(&sm, pmk, c.bssid, st.mac, c.ie, c.ie_len,
  399. b->rsn, b->rsn_len);
  400. u_puts("wifi: 4-way handshake...\n");
  401. if (handshake(&sm) < 0) {
  402. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  403. return 1;
  404. }
  405. }
  406. r = wifi(SYS_WIFI_AUTHORIZE, 0, 0);
  407. if (r < 0)
  408. u_puts("wifi: connected, but the WiFi card could not become the "
  409. "active netdev (close TCP users, then `netdev wifi`)\n");
  410. else
  411. u_puts("wifi: connected\n");
  412. if (b->security == SYS_WIFI_SEC_PSK)
  413. spawn_keeper(&sm, st.conn_id);
  414. if (r == 0 && u_syscall1(SYS_DHCP_RUN, 6000) != 0)
  415. u_puts("wifi: DHCP failed; set an address with netip\n");
  416. return 0;
  417. }
  418. static int cmd_connect(const char *args)
  419. {
  420. char ssid[33], pass[80], bssid_s[13];
  421. unsigned char want_bssid[6];
  422. int have_bssid = 0;
  423. args = word(args, ssid, sizeof(ssid));
  424. args = word(args, pass, sizeof(pass));
  425. if (*skip_ws(args)) {
  426. word(args, bssid_s, sizeof(bssid_s));
  427. if (slen(bssid_s) != 12 ||
  428. unhex(bssid_s, want_bssid, 6) < 0) {
  429. u_puts("wifi: BSSID must be 12 hex digits\n");
  430. return 1;
  431. }
  432. have_bssid = 1;
  433. }
  434. return connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
  435. }
  436. /* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
  437. * BSSID record in /share because SYS_SPAWN's argument buffer is only
  438. * VFS_PATH_MAX (64) bytes. The file is removed after it has been read. */
  439. static int cmd_connect_file(const char *args)
  440. {
  441. struct wifi_cfg cfg;
  442. char path[80], ssid[33], pass[65];
  443. unsigned int i;
  444. int r;
  445. args = word(args, path, sizeof(path));
  446. for (i = 0; i < sizeof(cfg); i++)
  447. ((unsigned char *)&cfg)[i] = 0;
  448. r = u_syscall3(SYS_READ_FILE, (int)path, (int)&cfg, (int)sizeof(cfg));
  449. u_syscall1(SYS_UNLINK, (int)path);
  450. if (r < 0 || (unsigned int)r < sizeof(cfg) ||
  451. cfg.magic != WIFI_CFG_MAGIC) {
  452. u_puts("wifi: no valid staged connect request\n");
  453. return 1;
  454. }
  455. if (cfg.ssid_len == 0 || cfg.ssid_len > 32 || cfg.pass_len > 64) {
  456. u_puts("wifi: staged connect request is malformed\n");
  457. return 1;
  458. }
  459. for (i = 0; i < cfg.ssid_len; i++)
  460. ssid[i] = (char)cfg.ssid[i];
  461. ssid[cfg.ssid_len] = '\0';
  462. for (i = 0; i < cfg.pass_len; i++)
  463. pass[i] = cfg.pass[i];
  464. pass[cfg.pass_len] = '\0';
  465. r = connect_bss(ssid, pass, cfg.bssid);
  466. for (i = 0; i < sizeof(cfg); i++)
  467. ((unsigned char *)&cfg)[i] = 0;
  468. for (i = 0; i < sizeof(pass); i++)
  469. ((volatile char *)pass)[i] = 0;
  470. return r;
  471. }
  472. /* Background: answer group-key rekeys until the connection changes. */
  473. static int keeper(const char *args)
  474. {
  475. static struct wpa_sm sm;
  476. static unsigned char frame[1024], reply[256];
  477. struct sys_wifi_status st;
  478. struct sys_wifi_ptk p;
  479. unsigned char id[4];
  480. unsigned int conn_id, rlen, i;
  481. args = skip_ws(args);
  482. if (slen(args) < 8 || unhex(args, id, 4) < 0) {
  483. u_puts("wifi: keeper: bad state argument\n");
  484. return 1;
  485. }
  486. conn_id = ((unsigned int)id[0] << 24) | (id[1] << 16) |
  487. (id[2] << 8) | id[3];
  488. if (wifi(SYS_WIFI_GET_PTK, (int)&p, (int)conn_id) < 0 ||
  489. p.conn_id != conn_id) {
  490. u_puts("wifi: keeper: no key state for this connection\n");
  491. return 1;
  492. }
  493. for (i = 0; i < 48; i++)
  494. sm.ptk[i] = p.ptk[i];
  495. for (i = 0; i < 8; i++)
  496. sm.replay[i] = p.replay[i];
  497. sm.have_replay = 1;
  498. sm.ptk_ok = 1;
  499. u_puts("wifi: keeper running\n");
  500. for (;;) {
  501. int n, r;
  502. if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0 ||
  503. st.conn_id != conn_id || st.state < SYS_WIFI_ST_ASSOC)
  504. return 0;
  505. n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
  506. if (n > 0) {
  507. r = wpa_rx(&sm, frame, (unsigned int)n, reply, &rlen,
  508. tls_random);
  509. u_puts("wifi: keeper eapol ");
  510. u_putdec((unsigned int)n);
  511. u_puts(" -> ");
  512. u_putdec((unsigned int)r);
  513. u_puts("\n");
  514. if (r == WPA_GTK) {
  515. wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen);
  516. u_puts("wifi: keeper gtk ");
  517. u_putdec((unsigned int)sm.gtk_idx);
  518. u_puts(set_gtk(&sm) == 0 ? " installed\n"
  519. : " install failed\n");
  520. } else if (r == WPA_ERR) {
  521. u_puts("wifi: keeper error: ");
  522. u_puts(sm.error ? sm.error : "?");
  523. u_puts("\n");
  524. }
  525. }
  526. u_sleep_ms(200);
  527. }
  528. }
  529. void _start(const char *arg)
  530. {
  531. const char *a = skip_ws(arg ? arg : "");
  532. int r = 0;
  533. if (!*a)
  534. r = cmd_status();
  535. else if (starts(a, "__keep "))
  536. r = keeper(a + 7);
  537. else if (starts(a, "up")) {
  538. r = bring_up() < 0;
  539. if (!r)
  540. r = cmd_status();
  541. } else if (starts(a, "scan"))
  542. r = cmd_scan();
  543. else if (starts(a, "connect "))
  544. r = cmd_connect(a + 8);
  545. else if (starts(a, "connectfile "))
  546. r = cmd_connect_file(a + 12);
  547. else if (starts(a, "disconnect")) {
  548. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  549. u_puts("wifi: disconnected\n");
  550. } else {
  551. u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|disconnect]\n");
  552. r = 1;
  553. }
  554. u_exit(r);
  555. }