ucmd_wifi.c 11 KB

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