ucmd_wifi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 autoconnect reconnect from /data/WIFI.CFG (boot helper)
  14. * wifi forget remove the saved credentials
  15. * wifi disconnect
  16. *
  17. * The WPA2 handshake (wpa.c) runs here; a small background copy of this
  18. * command (`wifi __keep <state>`) stays alive after `connect` to answer
  19. * group-key rekeys, and exits when the connection changes. */
  20. #include "ucmd_lib.h"
  21. #include "tls_rng.h"
  22. #include "wifi_cfg.h"
  23. #include "wpa.h"
  24. #define MAX_BSS 32
  25. static struct sys_wifi_bss bss[MAX_BSS];
  26. static int wifi(int op, int a2, int a3)
  27. {
  28. return u_syscall3(SYS_WIFI, op, a2, a3);
  29. }
  30. static int name_eq(const char *a, const char *b)
  31. {
  32. while (*a && *b) {
  33. if (*a != *b)
  34. return 0;
  35. a++;
  36. b++;
  37. }
  38. return *a == *b;
  39. }
  40. static int bssid_zero(const unsigned char b[6])
  41. {
  42. unsigned int i;
  43. for (i = 0; i < 6; i++)
  44. if (b[i])
  45. return 0;
  46. return 1;
  47. }
  48. static void wipe_cfg(struct wifi_cfg *cfg)
  49. {
  50. unsigned int i;
  51. for (i = 0; i < sizeof(*cfg); i++)
  52. ((volatile unsigned char *)cfg)[i] = 0;
  53. }
  54. /* Save the network for the next boot. The BSSID is deliberately left zero
  55. * so a mesh/repeater AP with the same SSID still works; /data is a plain FAT
  56. * partition, so WIFI.CFG is 8.3 and the password is stored in clear. */
  57. static int save_wifi_cred(const unsigned char *ssid, unsigned int ssid_len,
  58. const char *pass, unsigned int pass_len)
  59. {
  60. /* FAT writes may read a final partial sector past the source length;
  61. * keep slack after the record as well. */
  62. unsigned char buf[sizeof(struct wifi_cfg) + 512];
  63. struct wifi_cfg *cfg = (struct wifi_cfg *)buf;
  64. unsigned int i;
  65. int r;
  66. if (ssid_len > 32)
  67. ssid_len = 32;
  68. if (pass_len > 64)
  69. pass_len = 64;
  70. for (i = 0; i < sizeof(buf); i++)
  71. buf[i] = 0;
  72. cfg->magic = WIFI_CFG_MAGIC;
  73. cfg->ssid_len = (unsigned char)ssid_len;
  74. cfg->pass_len = (unsigned char)pass_len;
  75. for (i = 0; i < ssid_len; i++)
  76. cfg->ssid[i] = ssid[i];
  77. for (i = 0; i < pass_len; i++)
  78. cfg->pass[i] = pass[i];
  79. r = u_syscall3(SYS_WRITE_FILE, (int)WIFI_CRED_PATH,
  80. (int)cfg, (int)sizeof(*cfg));
  81. for (i = 0; i < sizeof(buf); i++)
  82. buf[i] = 0;
  83. return r;
  84. }
  85. static int load_wifi_cred(struct wifi_cfg *cfg)
  86. {
  87. /* FAT reads can write a final partial sector past the requested
  88. * length; keep 512 bytes of slack so the stack top is never touched. */
  89. unsigned char buf[sizeof(struct wifi_cfg) + 512];
  90. unsigned int i;
  91. int r;
  92. wipe_cfg(cfg);
  93. r = u_syscall3(SYS_READ_FILE, (int)WIFI_CRED_PATH,
  94. (int)buf, (int)sizeof(*cfg));
  95. if (r < (int)sizeof(*cfg) || ((struct wifi_cfg *)buf)->magic !=
  96. WIFI_CFG_MAGIC) {
  97. for (i = 0; i < sizeof(buf); i++)
  98. buf[i] = 0;
  99. return -1;
  100. }
  101. for (i = 0; i < sizeof(*cfg); i++)
  102. ((unsigned char *)cfg)[i] = buf[i];
  103. for (i = 0; i < sizeof(buf); i++)
  104. buf[i] = 0;
  105. if (cfg->ssid_len == 0 || cfg->ssid_len > 32 ||
  106. cfg->pass_len > 64) {
  107. wipe_cfg(cfg);
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. static void put_ssid(const unsigned char *s, unsigned int n)
  113. {
  114. unsigned int i;
  115. for (i = 0; i < n; i++)
  116. u_putc(s[i] >= 32 && s[i] < 127 ? (char)s[i] : '?');
  117. }
  118. static int slen(const char *s)
  119. {
  120. int n = 0;
  121. while (s[n])
  122. n++;
  123. return n;
  124. }
  125. static int starts(const char *s, const char *p)
  126. {
  127. while (*p)
  128. if (*s++ != *p++)
  129. return 0;
  130. return 1;
  131. }
  132. static const char *skip_ws(const char *s)
  133. {
  134. while (*s == ' ')
  135. s++;
  136. return s;
  137. }
  138. /* Next word or "quoted string" into out; returns the rest. */
  139. static const char *word(const char *s, char *out, unsigned int max)
  140. {
  141. unsigned int n = 0;
  142. char end = ' ';
  143. s = skip_ws(s);
  144. if (*s == '"') {
  145. end = '"';
  146. s++;
  147. }
  148. while (*s && *s != end) {
  149. if (end == '"' && *s == '\\' && s[1])
  150. s++;
  151. if (n + 1 < max)
  152. out[n++] = *s;
  153. s++;
  154. }
  155. if (*s == end)
  156. s++;
  157. out[n] = 0;
  158. return s;
  159. }
  160. static void put_dbm(int v)
  161. {
  162. if (v < 0) {
  163. u_putc('-');
  164. v = -v;
  165. }
  166. u_putdec((unsigned int)v);
  167. u_puts(" dBm");
  168. }
  169. static const char *sec_name(unsigned int s)
  170. {
  171. return s == SYS_WIFI_SEC_OPEN ? "open" :
  172. s == SYS_WIFI_SEC_PSK ? "WPA2" : "other";
  173. }
  174. static int cmd_status(void)
  175. {
  176. struct sys_wifi_status st;
  177. static const char *const names[] = { "down", "idle", "associated",
  178. "connected" };
  179. unsigned char ip[4];
  180. if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0) {
  181. u_puts("wifi: status failed\n");
  182. return 1;
  183. }
  184. if (!st.present) {
  185. u_puts("wifi: no Intel 7265/3165 card found\n");
  186. return 1;
  187. }
  188. u_puts("chip: "); u_puts(st.chip);
  189. u_puts("\nstate: "); u_puts(names[st.state & 3]);
  190. u_puts(" ("); u_puts(st.detail); u_puts(")\n");
  191. if (st.fw[0]) {
  192. u_puts("firmware: "); u_puts(st.fw); u_puts("\n");
  193. }
  194. if (st.state != SYS_WIFI_ST_DOWN) {
  195. u_puts("mac: "); u_put_mac(st.mac); u_puts("\n");
  196. }
  197. if (st.state >= SYS_WIFI_ST_ASSOC) {
  198. u_puts("network: \""); put_ssid(st.ssid, st.ssid_len);
  199. u_puts("\" "); u_put_mac(st.bssid);
  200. u_puts(" ch "); u_putdec(st.channel);
  201. u_puts(" "); put_dbm(st.rssi); u_puts("\n");
  202. u_syscall1(SYS_NET_GET_IP, (int)ip);
  203. u_puts("ip: "); u_put_ip(ip); u_puts("\n");
  204. u_puts("frames: rx "); u_putdec(st.rx_frames);
  205. u_puts(" tx "); u_putdec(st.tx_frames);
  206. u_puts(" dropped "); u_putdec(st.rx_dropped); u_puts("\n");
  207. }
  208. return 0;
  209. }
  210. static int bring_up(void)
  211. {
  212. struct sys_wifi_status st;
  213. u_puts("wifi: loading firmware...\n");
  214. if (wifi(SYS_WIFI_UP, 0, 0) < 0) {
  215. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  216. u_puts("wifi: bring-up failed: ");
  217. u_puts(st.detail);
  218. u_puts(" (details on the serial/netmon log)\n");
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. /* Scan and return the number of entries, sorted by RSSI. */
  224. static int do_scan(void)
  225. {
  226. int n, i, j;
  227. unsigned int t0;
  228. if (wifi(SYS_WIFI_SCAN, 0, 0) < 0) {
  229. u_puts("wifi: scan failed (disconnect first?)\n");
  230. return -1;
  231. }
  232. t0 = u_ticks_ms();
  233. while ((n = wifi(SYS_WIFI_RESULTS, (int)bss, MAX_BSS)) == -2) {
  234. if (u_ticks_ms() - t0 > 15000) {
  235. u_puts("wifi: scan timed out\n");
  236. return -1;
  237. }
  238. u_sleep_ms(100);
  239. }
  240. if (n < 0)
  241. return -1;
  242. for (i = 1; i < n; i++)
  243. for (j = i; j > 0 && bss[j].rssi > bss[j - 1].rssi; j--) {
  244. struct sys_wifi_bss t = bss[j];
  245. bss[j] = bss[j - 1];
  246. bss[j - 1] = t;
  247. }
  248. return n;
  249. }
  250. static int cmd_scan(void)
  251. {
  252. int n, i;
  253. if (bring_up() < 0)
  254. return 1;
  255. u_puts("wifi: scanning...\n");
  256. n = do_scan();
  257. if (n < 0)
  258. return 1;
  259. if (n == 0)
  260. u_puts("no networks found\n");
  261. for (i = 0; i < n; i++) {
  262. u_put_mac(bss[i].bssid);
  263. u_puts(" ");
  264. put_dbm(bss[i].rssi);
  265. u_puts(bss[i].channel < 10 ? " ch " :
  266. bss[i].channel < 100 ? " ch " : " ch ");
  267. u_putdec(bss[i].channel);
  268. u_puts(" ");
  269. u_puts(sec_name(bss[i].security));
  270. u_puts(bss[i].security == SYS_WIFI_SEC_PSK ? " " : " ");
  271. if (bss[i].ssid_len)
  272. put_ssid(bss[i].ssid, bss[i].ssid_len);
  273. else
  274. u_puts("(hidden)");
  275. u_puts("\n");
  276. }
  277. return 0;
  278. }
  279. static int hexval(char c)
  280. {
  281. if (c >= '0' && c <= '9')
  282. return c - '0';
  283. if (c >= 'a' && c <= 'f')
  284. return c - 'a' + 10;
  285. if (c >= 'A' && c <= 'F')
  286. return c - 'A' + 10;
  287. return -1;
  288. }
  289. static int unhex(const char *s, unsigned char *out, unsigned int n)
  290. {
  291. unsigned int i;
  292. for (i = 0; i < n; i++) {
  293. int a = hexval(s[2 * i]), b = hexval(s[2 * i + 1]);
  294. if (a < 0 || b < 0)
  295. return -1;
  296. out[i] = (unsigned char)(a * 16 + b);
  297. }
  298. return 0;
  299. }
  300. static void tohex(const unsigned char *in, unsigned int n, char *out)
  301. {
  302. static const char hx[] = "0123456789abcdef";
  303. unsigned int i;
  304. for (i = 0; i < n; i++) {
  305. out[2 * i] = hx[in[i] >> 4];
  306. out[2 * i + 1] = hx[in[i] & 15];
  307. }
  308. out[2 * n] = 0;
  309. }
  310. static int set_gtk(struct wpa_sm *sm)
  311. {
  312. struct sys_wifi_key k;
  313. unsigned int i;
  314. k.pairwise = 0;
  315. k.keyidx = sm->gtk_idx;
  316. for (i = 0; i < 16; i++)
  317. k.key[i] = sm->gtk[i];
  318. for (i = 0; i < 8; i++)
  319. k.rsc[i] = sm->gtk_rsc[i];
  320. return wifi(SYS_WIFI_SET_KEY, (int)&k, 0);
  321. }
  322. /* Run the 4-way handshake: msg1 -> msg2, msg3 -> msg4 + keys. */
  323. static int handshake(struct wpa_sm *sm)
  324. {
  325. static unsigned char frame[1024], reply[256];
  326. unsigned int t0 = u_ticks_ms(), rlen;
  327. int n, r;
  328. while (u_ticks_ms() - t0 < 8000) {
  329. n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
  330. if (n < 0)
  331. return -1;
  332. if (n == 0) {
  333. u_sleep_ms(20);
  334. continue;
  335. }
  336. r = wpa_rx(sm, frame, (unsigned int)n, reply, &rlen, tls_random);
  337. if (r == WPA_ERR) {
  338. u_puts("wifi: handshake: ");
  339. u_puts(sm->error ? sm->error : "error");
  340. u_puts("\n");
  341. return -1;
  342. }
  343. if (r == WPA_NONE)
  344. continue;
  345. if (wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen) < 0)
  346. return -1;
  347. if (r == WPA_PTK) {
  348. struct sys_wifi_key k;
  349. unsigned int i;
  350. k.pairwise = 1;
  351. k.keyidx = 0;
  352. for (i = 0; i < 16; i++)
  353. k.key[i] = WPA_TK(sm)[i];
  354. for (i = 0; i < 8; i++)
  355. k.rsc[i] = 0;
  356. u_sleep_ms(20); /* let message 4 leave in the clear */
  357. if (wifi(SYS_WIFI_SET_KEY, (int)&k, 0) < 0 ||
  358. set_gtk(sm) < 0) {
  359. u_puts("wifi: key install failed\n");
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. }
  365. u_puts("wifi: handshake timed out (no EAPOL from the AP)\n");
  366. return -1;
  367. }
  368. static void spawn_keeper(struct wpa_sm *sm, unsigned int conn_id)
  369. {
  370. static char arg[16];
  371. struct sys_wifi_ptk p;
  372. unsigned char id[4];
  373. char *q = arg;
  374. unsigned int i;
  375. p.conn_id = conn_id;
  376. for (i = 0; i < 48; i++)
  377. p.ptk[i] = sm->ptk[i];
  378. for (i = 0; i < 8; i++)
  379. p.replay[i] = sm->replay[i];
  380. if (wifi(SYS_WIFI_SET_PTK, (int)&p, 0) < 0) {
  381. u_puts("wifi: note: could not store rekey state; the link will "
  382. "drop at the next group rekey\n");
  383. return;
  384. }
  385. for (i = 0; "__keep "[i]; i++)
  386. *q++ = "__keep "[i];
  387. id[0] = conn_id >> 24;
  388. id[1] = conn_id >> 16;
  389. id[2] = conn_id >> 8;
  390. id[3] = conn_id;
  391. tohex(id, 4, q);
  392. q += 8;
  393. *q = 0;
  394. if (u_spawn("/ramfs/bin/wifi", arg, 0, "/") < 0)
  395. u_puts("wifi: note: rekey helper did not start; the link will "
  396. "drop at the next group rekey\n");
  397. }
  398. static int connect_bss(const char *ssid, const char *pass,
  399. const unsigned char *want_bssid)
  400. {
  401. struct sys_wifi_connect c;
  402. struct sys_wifi_status st;
  403. static struct wpa_sm sm;
  404. unsigned char pmk[32];
  405. struct sys_wifi_bss *b = 0;
  406. int n, i, r;
  407. unsigned int ssid_len;
  408. int have_bssid = want_bssid != 0;
  409. ssid_len = (unsigned int)slen(ssid);
  410. if (!ssid_len) {
  411. u_puts("usage: wifi connect <ssid> [passphrase]\n");
  412. return 1;
  413. }
  414. if (bring_up() < 0)
  415. return 1;
  416. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  417. u_puts("wifi: scanning for \""); u_puts(ssid); u_puts("\"...\n");
  418. n = do_scan();
  419. if (n < 0)
  420. return 1;
  421. for (i = 0; i < n; i++) {
  422. unsigned int j;
  423. if (bss[i].ssid_len != ssid_len)
  424. continue;
  425. if (have_bssid) {
  426. for (j = 0; j < 6 && bss[i].bssid[j] == want_bssid[j]; j++)
  427. ;
  428. if (j != 6)
  429. continue;
  430. }
  431. for (j = 0; j < ssid_len && bss[i].ssid[j] == (unsigned char)ssid[j]; j++)
  432. ;
  433. if (j == ssid_len) {
  434. b = &bss[i]; /* sorted: first match is strongest */
  435. break;
  436. }
  437. }
  438. if (!b) {
  439. u_puts("wifi: network not found\n");
  440. return 1;
  441. }
  442. if (b->security == SYS_WIFI_SEC_OTHER) {
  443. u_puts("wifi: unsupported security (only open and WPA2-PSK/CCMP)\n");
  444. return 1;
  445. }
  446. if (b->security == SYS_WIFI_SEC_PSK && !pass[0]) {
  447. u_puts("wifi: this network needs a passphrase\n");
  448. return 1;
  449. }
  450. for (i = 0; i < (int)sizeof(c); i++)
  451. ((unsigned char *)&c)[i] = 0;
  452. for (i = 0; i < 6; i++)
  453. c.bssid[i] = b->bssid[i];
  454. c.channel = b->channel;
  455. c.ssid_len = ssid_len;
  456. for (i = 0; i < (int)ssid_len; i++)
  457. c.ssid[i] = (unsigned char)ssid[i];
  458. c.beacon_int = b->beacon_int;
  459. c.dtim = b->dtim;
  460. if (b->security == SYS_WIFI_SEC_PSK) {
  461. int plen = slen(pass);
  462. if (plen == 64 && unhex(pass, pmk, 32) == 0) {
  463. /* raw 256-bit PSK */
  464. } else if (plen >= 8 && plen <= 63) {
  465. u_puts("wifi: deriving key...\n");
  466. wpa_pmk(pass, c.ssid, ssid_len, pmk);
  467. } else {
  468. u_puts("wifi: a WPA2 passphrase has 8..63 characters\n");
  469. return 1;
  470. }
  471. c.ie_len = (unsigned char)wpa_rsn_ie(c.ie);
  472. }
  473. u_puts("wifi: joining "); u_put_mac(c.bssid);
  474. u_puts(" on channel "); u_putdec(c.channel); u_puts("\n");
  475. if (wifi(SYS_WIFI_CONNECT, (int)&c, 0) < 0) {
  476. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  477. u_puts("wifi: "); u_puts(st.detail); u_puts("\n");
  478. return 1;
  479. }
  480. wifi(SYS_WIFI_STATUS, (int)&st, 0);
  481. if (b->security == SYS_WIFI_SEC_PSK) {
  482. wpa_init(&sm, pmk, c.bssid, st.mac, c.ie, c.ie_len,
  483. b->rsn, b->rsn_len);
  484. u_puts("wifi: 4-way handshake...\n");
  485. if (handshake(&sm) < 0) {
  486. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  487. return 1;
  488. }
  489. }
  490. r = wifi(SYS_WIFI_AUTHORIZE, 0, 0);
  491. if (r < 0)
  492. u_puts("wifi: connected, but the WiFi card could not become the "
  493. "active netdev (close TCP users, then `netdev wifi`)\n");
  494. else
  495. u_puts("wifi: connected\n");
  496. if (b->security == SYS_WIFI_SEC_PSK)
  497. spawn_keeper(&sm, st.conn_id);
  498. if (r == 0 && u_syscall1(SYS_DHCP_RUN, 6000) != 0)
  499. u_puts("wifi: DHCP failed; set an address with netip\n");
  500. return r == 0 ? 0 : 1;
  501. }
  502. static int cmd_connect(const char *args)
  503. {
  504. char ssid[33], pass[80], bssid_s[13];
  505. unsigned char want_bssid[6];
  506. int have_bssid = 0;
  507. int r;
  508. args = word(args, ssid, sizeof(ssid));
  509. args = word(args, pass, sizeof(pass));
  510. if (*skip_ws(args)) {
  511. word(args, bssid_s, sizeof(bssid_s));
  512. if (slen(bssid_s) != 12 ||
  513. unhex(bssid_s, want_bssid, 6) < 0) {
  514. u_puts("wifi: BSSID must be 12 hex digits\n");
  515. return 1;
  516. }
  517. have_bssid = 1;
  518. }
  519. r = connect_bss(ssid, pass, have_bssid ? want_bssid : 0);
  520. if (r == 0) {
  521. if (save_wifi_cred((const unsigned char *)ssid,
  522. (unsigned int)slen(ssid), pass,
  523. (unsigned int)slen(pass)) == 0)
  524. u_puts("wifi: saved for auto-connect\n");
  525. else
  526. u_puts("wifi: note: could not save credentials to /data\n");
  527. }
  528. return r;
  529. }
  530. /* `wifi connectfile <path>`: the network menu stages an SSID + passphrase +
  531. * BSSID record in /share because SYS_SPAWN's argument buffer is only
  532. * VFS_PATH_MAX (64) bytes. The file is removed after it has been read. */
  533. static int cmd_connect_file(const char *args)
  534. {
  535. struct wifi_cfg cfg;
  536. char path[80], ssid[33], pass[65];
  537. unsigned int i;
  538. int r;
  539. args = word(args, path, sizeof(path));
  540. for (i = 0; i < sizeof(cfg); i++)
  541. ((unsigned char *)&cfg)[i] = 0;
  542. r = u_syscall3(SYS_READ_FILE, (int)path, (int)&cfg, (int)sizeof(cfg));
  543. u_syscall1(SYS_UNLINK, (int)path);
  544. if (r < 0 || (unsigned int)r < sizeof(cfg) ||
  545. cfg.magic != WIFI_CFG_MAGIC) {
  546. u_puts("wifi: no valid staged connect request\n");
  547. return 1;
  548. }
  549. if (cfg.ssid_len == 0 || cfg.ssid_len > 32 || cfg.pass_len > 64) {
  550. u_puts("wifi: staged connect request is malformed\n");
  551. return 1;
  552. }
  553. for (i = 0; i < cfg.ssid_len; i++)
  554. ssid[i] = (char)cfg.ssid[i];
  555. ssid[cfg.ssid_len] = '\0';
  556. for (i = 0; i < cfg.pass_len; i++)
  557. pass[i] = cfg.pass[i];
  558. pass[cfg.pass_len] = '\0';
  559. r = connect_bss(ssid, pass, cfg.bssid);
  560. if (r == 0) {
  561. if (save_wifi_cred(cfg.ssid, cfg.ssid_len, cfg.pass,
  562. cfg.pass_len) == 0)
  563. u_puts("wifi: saved for auto-connect\n");
  564. else
  565. u_puts("wifi: note: credentials not saved (no writable /data)\n");
  566. }
  567. for (i = 0; i < sizeof(cfg); i++)
  568. ((unsigned char *)&cfg)[i] = 0;
  569. for (i = 0; i < sizeof(pass); i++)
  570. ((volatile char *)pass)[i] = 0;
  571. return r;
  572. }
  573. /* Boot-time helper: reconnect to the network saved in /data/WIFI.CFG. A
  574. * wired link that already has an address wins; an existing WiFi link is left
  575. * alone. */
  576. static int cmd_autoconnect(void)
  577. {
  578. struct wifi_cfg cfg;
  579. struct sys_wifi_status ws;
  580. struct sys_netinfo ni;
  581. char ssid[33], pass[65];
  582. unsigned int i;
  583. int attempt, r = 1;
  584. if (load_wifi_cred(&cfg) != 0)
  585. return 0;
  586. if (u_wifi(SYS_WIFI_STATUS, (int)&ws, 0) != 0 || !ws.present) {
  587. wipe_cfg(&cfg);
  588. return 0;
  589. }
  590. if (ws.state >= SYS_WIFI_ST_LINK) {
  591. wipe_cfg(&cfg);
  592. return 0;
  593. }
  594. if (u_net_info(&ni) == 1 && ni.present && !name_eq(ni.name, "wifi")) {
  595. unsigned char srv[4];
  596. unsigned int lease = 0;
  597. /* Only a real wired DHCP lease keeps the boot-time wired link;
  598. * the static fallback address does not mean the cable is up. */
  599. if (u_syscall2(SYS_DHCP_INFO, (int)srv, (int)&lease) == 1) {
  600. wipe_cfg(&cfg);
  601. return 0;
  602. }
  603. }
  604. for (i = 0; i < cfg.ssid_len; i++)
  605. ssid[i] = (char)cfg.ssid[i];
  606. ssid[cfg.ssid_len] = '\0';
  607. for (i = 0; i < cfg.pass_len; i++)
  608. pass[i] = cfg.pass[i];
  609. pass[cfg.pass_len] = '\0';
  610. for (attempt = 0; attempt < 2; attempt++) {
  611. r = connect_bss(ssid, pass,
  612. bssid_zero(cfg.bssid) ? 0 : cfg.bssid);
  613. if (r == 0)
  614. break;
  615. if (attempt == 0)
  616. u_sleep_ms(3000);
  617. }
  618. for (i = 0; i < sizeof(cfg); i++)
  619. ((unsigned char *)&cfg)[i] = 0;
  620. for (i = 0; i < sizeof(pass); i++)
  621. ((volatile char *)pass)[i] = 0;
  622. return r;
  623. }
  624. static int cmd_forget(void)
  625. {
  626. if (u_syscall1(SYS_UNLINK, (int)WIFI_CRED_PATH) == 0)
  627. u_puts("wifi: saved credentials removed\n");
  628. else
  629. u_puts("wifi: no saved credentials\n");
  630. return 0;
  631. }
  632. /* Background: answer group-key rekeys until the connection changes. */
  633. static int keeper(const char *args)
  634. {
  635. static struct wpa_sm sm;
  636. static unsigned char frame[1024], reply[256];
  637. struct sys_wifi_status st;
  638. struct sys_wifi_ptk p;
  639. unsigned char id[4];
  640. unsigned int conn_id, rlen, i;
  641. args = skip_ws(args);
  642. if (slen(args) < 8 || unhex(args, id, 4) < 0) {
  643. u_puts("wifi: keeper: bad state argument\n");
  644. return 1;
  645. }
  646. conn_id = ((unsigned int)id[0] << 24) | (id[1] << 16) |
  647. (id[2] << 8) | id[3];
  648. if (wifi(SYS_WIFI_GET_PTK, (int)&p, (int)conn_id) < 0 ||
  649. p.conn_id != conn_id) {
  650. u_puts("wifi: keeper: no key state for this connection\n");
  651. return 1;
  652. }
  653. for (i = 0; i < 48; i++)
  654. sm.ptk[i] = p.ptk[i];
  655. for (i = 0; i < 8; i++)
  656. sm.replay[i] = p.replay[i];
  657. sm.have_replay = 1;
  658. sm.ptk_ok = 1;
  659. u_puts("wifi: keeper running\n");
  660. for (;;) {
  661. int n, r;
  662. if (wifi(SYS_WIFI_STATUS, (int)&st, 0) < 0 ||
  663. st.conn_id != conn_id || st.state < SYS_WIFI_ST_ASSOC)
  664. return 0;
  665. n = wifi(SYS_WIFI_EAPOL_RX, (int)frame, sizeof(frame));
  666. if (n > 0) {
  667. r = wpa_rx(&sm, frame, (unsigned int)n, reply, &rlen,
  668. tls_random);
  669. u_puts("wifi: keeper eapol ");
  670. u_putdec((unsigned int)n);
  671. u_puts(" -> ");
  672. u_putdec((unsigned int)r);
  673. u_puts("\n");
  674. if (r == WPA_GTK) {
  675. wifi(SYS_WIFI_EAPOL_TX, (int)reply, (int)rlen);
  676. u_puts("wifi: keeper gtk ");
  677. u_putdec((unsigned int)sm.gtk_idx);
  678. u_puts(set_gtk(&sm) == 0 ? " installed\n"
  679. : " install failed\n");
  680. } else if (r == WPA_ERR) {
  681. u_puts("wifi: keeper error: ");
  682. u_puts(sm.error ? sm.error : "?");
  683. u_puts("\n");
  684. }
  685. }
  686. u_sleep_ms(200);
  687. }
  688. }
  689. void _start(const char *arg)
  690. {
  691. const char *a = skip_ws(arg ? arg : "");
  692. int r = 0;
  693. if (!*a)
  694. r = cmd_status();
  695. else if (starts(a, "__keep "))
  696. r = keeper(a + 7);
  697. else if (starts(a, "up")) {
  698. r = bring_up() < 0;
  699. if (!r)
  700. r = cmd_status();
  701. } else if (starts(a, "scan"))
  702. r = cmd_scan();
  703. else if (starts(a, "connect "))
  704. r = cmd_connect(a + 8);
  705. else if (starts(a, "connectfile "))
  706. r = cmd_connect_file(a + 12);
  707. else if (starts(a, "autoconnect"))
  708. r = cmd_autoconnect();
  709. else if (starts(a, "forget"))
  710. r = cmd_forget();
  711. else if (starts(a, "disconnect")) {
  712. wifi(SYS_WIFI_DISCONNECT, 0, 0);
  713. u_puts("wifi: disconnected\n");
  714. } else {
  715. u_puts("usage: wifi [up|scan|connect <ssid> [pass] [bssid]|connectfile <path>|autoconnect|forget|disconnect]\n");
  716. r = 1;
  717. }
  718. u_exit(r);
  719. }