ucmd_wifi.c 13 KB

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