sys_net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /* Network service for ring-3 commands (see syscall.h for the ABI).
  2. *
  3. * The stack (net.c/dns.c/tcp.c) and the NIC drivers stay in the kernel; user
  4. * commands get the configuration, the DNS/DHCP clients and the raw frame
  5. * paths, so the packet-level commands (ping, pong, netrecv, ...) port over
  6. * their existing frame code.
  7. *
  8. * Packet-level commands take an exclusive raw lease (netdev_acquire); the
  9. * background kexec watcher pauses while one owns the receiver, and the reaper
  10. * releases it if that process dies. TCP clients do NOT take that lease: each
  11. * process may hold one connection in tcp.c, and every stack syscall drives
  12. * the shared net_pump() so several apps can transfer concurrently. */
  13. #include "syscall.h"
  14. #include "dhcp.h"
  15. #include "dns.h"
  16. #include "io.h"
  17. #include "net.h"
  18. #include "netcore.h"
  19. #include "netdev.h"
  20. #include "netkexec.h"
  21. #include "netlog.h"
  22. #include "ntp.h"
  23. #include "process.h"
  24. #include "tcp.h"
  25. #include "timer.h"
  26. #include "wm.h"
  27. /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
  28. static struct tcp_conn *srv_conn;
  29. /* Reply buffer for ARP/ICMP answered while listening (the rx scratch is
  30. * occupied by the received frame). */
  31. static unsigned char srv_reply[NETDEV_FRAME_MAX];
  32. /* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
  33. * they need interrupts: the syscall stub entered with them off, and a frozen
  34. * tick counter would never satisfy a deadline. The netdev ownership already
  35. * serializes net users, so running with IRQs on (and being preempted) is
  36. * safe. */
  37. static void net_enter(void)
  38. {
  39. /* ARP/DNS/DHCP/TCP waits spin on the PIT and can take seconds. Give
  40. * the other CPU's syscalls (and the WM) the kernel while we wait; the
  41. * NIC ownership still serializes every stack user. */
  42. bkl_pause();
  43. __asm__ volatile ("sti");
  44. }
  45. static void net_exit(void)
  46. {
  47. __asm__ volatile ("cli");
  48. bkl_resume();
  49. }
  50. static unsigned int caller_pid(void)
  51. {
  52. return current_proc()->pid;
  53. }
  54. static int nd_name_eq(const char *a, const char *b)
  55. {
  56. while (*a && *b) {
  57. if (*a != *b)
  58. return 0;
  59. a++;
  60. b++;
  61. }
  62. return *a == *b;
  63. }
  64. static void nd_copy_name(char *dst, unsigned int max, const char *src)
  65. {
  66. unsigned int i;
  67. for (i = 0; src && src[i] && i + 1 < max; i++)
  68. dst[i] = src[i];
  69. dst[i] = '\0';
  70. }
  71. static unsigned int nd_kind(const struct netdev *nd)
  72. {
  73. if (nd_name_eq(nd->name, "wifi"))
  74. return SYS_NETDEV_WIFI;
  75. if (nd_name_eq(nd->name, "rndis"))
  76. return SYS_NETDEV_USB;
  77. return SYS_NETDEV_WIRED;
  78. }
  79. /* Called from the scheduler's reap loop for every dead process. */
  80. void sys_net_reap(unsigned int pid)
  81. {
  82. if (netdev_owner() == pid) {
  83. netdev_release_all();
  84. tcp_srv_reset();
  85. srv_conn = 0;
  86. }
  87. tcp_client_reap(pid);
  88. dns_reap(pid);
  89. net_stack_reap(pid);
  90. }
  91. int sys_net_info(struct sys_netinfo *out)
  92. {
  93. const struct netdev *nd;
  94. unsigned int i;
  95. for (i = 0; i < sizeof(*out); i++)
  96. ((unsigned char *)out)[i] = 0;
  97. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  98. netlog_get_host(out->mirror_host);
  99. /* The first SYS_NET_INFO may be the first NIC probe (the WM's top-bar
  100. * poll runs before the boot DHCP on some boots). Driver init waits on
  101. * the PHY/USB with tick-based delays, so run it with interrupts on and
  102. * the BKL released. Once probed, this is just a cached lookup. */
  103. if (!netdev_probed()) {
  104. /* Not for the WM: its first top-bar poll comes before its first
  105. * frame, and a NIC probe (PHY reset, USB control transfers) would
  106. * hold that frame back. kernel_main probes right after the WM
  107. * starts and the WM polls again every second. */
  108. if (wm_server_is_wm())
  109. return 0;
  110. net_enter();
  111. nd = netdev_active();
  112. net_exit();
  113. } else {
  114. nd = netdev_active();
  115. }
  116. if (!nd)
  117. return 0;
  118. out->present = 1;
  119. if (nd->get_mac)
  120. nd->get_mac(out->mac);
  121. net_get_ip(out->ip);
  122. net_get_mask(out->mask);
  123. net_get_gateway(out->gw);
  124. net_get_dns(out->dns);
  125. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  126. out->name[i] = nd->name[i];
  127. {
  128. const char *chip = netdev_chip(nd);
  129. for (i = 0; chip[i] && i + 1 < sizeof(out->chip); i++)
  130. out->chip[i] = chip[i];
  131. }
  132. out->link = (unsigned int)netdev_link(nd);
  133. return 1;
  134. }
  135. /* Probe every registered driver once and return the ones that are present.
  136. * The menu calls this when it opens, so a plain USB dongle or phone that was
  137. * not the boot-time active NIC still shows up. Driver init() can wait on the
  138. * PHY/USB, so it runs with interrupts on and the BKL released. */
  139. int sys_net_devlist(struct sys_netdev *out, unsigned int max)
  140. {
  141. const struct netdev *act;
  142. unsigned int i, n = 0;
  143. if (!out || !max)
  144. return 0;
  145. net_enter();
  146. act = netdev_active();
  147. for (i = 0; i < netdev_count() && n < max; i++) {
  148. const struct netdev *nd = netdev_at(i);
  149. unsigned int j;
  150. int present;
  151. if (!nd)
  152. continue;
  153. present = (nd == act);
  154. if (!present && nd->init)
  155. present = nd->init() == 0;
  156. if (!present)
  157. continue;
  158. for (j = 0; j < sizeof(out[n]); j++)
  159. ((unsigned char *)&out[n])[j] = 0;
  160. nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
  161. nd_copy_name(out[n].chip, sizeof(out[n].chip), netdev_chip(nd));
  162. out[n].kind = nd_kind(nd);
  163. out[n].active = (nd == act);
  164. out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
  165. (nd->link_up ? (nd->link_up() ? 1 : 0)
  166. : 1));
  167. n++;
  168. }
  169. net_exit();
  170. return (int)n;
  171. }
  172. int sys_net_select(const char *name)
  173. {
  174. int r;
  175. if (!name || !*name)
  176. return -1;
  177. net_enter();
  178. r = netdev_select(name);
  179. net_exit();
  180. return r;
  181. }
  182. int sys_net_print_info(void)
  183. {
  184. const struct netdev *nd = netdev_active();
  185. if (!nd || !nd->print_info || tcp_client_count() != 0)
  186. return -1;
  187. /* print_info reads the PHY via MDIC and can wait on the PHY/ME
  188. * semaphore; the syscall stub entered with interrupts off. */
  189. net_enter();
  190. nd->print_info();
  191. net_exit();
  192. return 0;
  193. }
  194. /* The owner must be the caller: the syscall runs in its process context. */
  195. static int owns_nic(void)
  196. {
  197. return netdev_owner() == caller_pid();
  198. }
  199. int sys_net_acquire(void)
  200. {
  201. if (owns_nic())
  202. return 0;
  203. /* Raw commands consume every frame, so they cannot coexist with a
  204. * shared TCP client (whose data the pump must dispatch). */
  205. if (netdev_in_use() || tcp_client_count() != 0)
  206. return -1;
  207. netdev_acquire();
  208. return 0;
  209. }
  210. int sys_net_release(void)
  211. {
  212. if (owns_nic())
  213. netdev_release();
  214. return 0;
  215. }
  216. int sys_net_mac(unsigned char mac[6])
  217. {
  218. const struct netdev *nd = netdev_active();
  219. if (!nd || !nd->get_mac)
  220. return -1;
  221. nd->get_mac(mac);
  222. return 0;
  223. }
  224. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  225. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  226. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  227. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  228. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  229. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  230. int sys_net_tx(const unsigned char *buf, unsigned int len)
  231. {
  232. const struct netdev *nd = netdev_active();
  233. if (!nd || !owns_nic())
  234. return -1;
  235. return nd->tx(buf, len);
  236. }
  237. int sys_net_rx(unsigned char *buf, unsigned int max)
  238. {
  239. const struct netdev *nd = netdev_active();
  240. if (!nd || !owns_nic())
  241. return -1;
  242. return nd->rx(buf, max);
  243. }
  244. void sys_net_decode(const unsigned char *buf, unsigned int len)
  245. {
  246. net_decode_frame(buf, len);
  247. }
  248. /* The reply buffer is always a full frame (the callers carry one). */
  249. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  250. {
  251. if (!owns_nic())
  252. return -1;
  253. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  254. }
  255. int sys_net_loopback(int on)
  256. {
  257. const struct netdev *nd = netdev_active();
  258. if (!nd || !nd->set_loopback)
  259. return -1;
  260. return nd->set_loopback(on);
  261. }
  262. int sys_net_regs(void)
  263. {
  264. const struct netdev *nd = netdev_active();
  265. if (!nd || !nd->dump_regs || tcp_client_count() != 0)
  266. return -1;
  267. net_enter();
  268. nd->dump_regs();
  269. net_exit();
  270. return 0;
  271. }
  272. int sys_net_reset(void)
  273. {
  274. const struct netdev *nd = netdev_active();
  275. if (!nd || !nd->reset)
  276. return -1;
  277. if (tcp_client_count() != 0 ||
  278. (netdev_in_use() && !owns_nic()))
  279. return -1;
  280. /* Own the receiver for the whole reset, so the netlog kworker cannot
  281. * submit frames while the MAC and rings are being reinitialised. */
  282. netdev_acquire();
  283. /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
  284. * entered with interrupts off. */
  285. net_enter();
  286. nd->reset();
  287. net_exit();
  288. netdev_release();
  289. return 0;
  290. }
  291. /* net100 fallback: force 100M or restore auto. The driver drops and
  292. * renegotiates the link, which can take a few seconds; own the receiver so
  293. * the background watcher cannot poll frames while the link is down. */
  294. int sys_net_speed(unsigned int mbps)
  295. {
  296. const struct netdev *nd = netdev_active();
  297. int r;
  298. if (!nd || !nd->set_speed)
  299. return -1;
  300. if (sys_net_acquire() != 0)
  301. return -2;
  302. net_enter();
  303. r = nd->set_speed(mbps);
  304. net_exit();
  305. sys_net_release();
  306. return r;
  307. }
  308. int sys_net_mac_set(const unsigned char mac[6])
  309. {
  310. const struct netdev *nd = netdev_active();
  311. if (!nd || !nd->set_mac)
  312. return -1;
  313. if (sys_net_acquire() != 0)
  314. return -2;
  315. nd->set_mac(mac);
  316. sys_net_release();
  317. return 0;
  318. }
  319. int sys_dhcp_run(unsigned int timeout_ms)
  320. {
  321. int r;
  322. if (netdev_in_use() || tcp_client_count() != 0)
  323. return -1;
  324. net_enter();
  325. r = dhcp_run(timeout_ms);
  326. net_exit();
  327. return r;
  328. }
  329. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  330. {
  331. return dhcp_last_info(server, lease);
  332. }
  333. int sys_dns_resolve(const char *name, unsigned char ip[4])
  334. {
  335. int r;
  336. /* A raw owner keeps its direct poll path; stack users share the pump.
  337. * Refuse only when another process owns the raw lease. */
  338. if (netdev_in_use() && !owns_nic())
  339. return -1;
  340. if (owns_nic()) {
  341. net_enter();
  342. r = dns_resolve(name, ip);
  343. net_exit();
  344. return r;
  345. }
  346. return dns_resolve(name, ip);
  347. }
  348. /* SNTP sync for ring-3 callers. The service owns the receiver for the
  349. * duration (unless the caller already does), so settime does not have to
  350. * take SYS_NET_ACQUIRE itself. A second network user gets SYS_NTP_EBUSY. */
  351. int sys_ntp_sync(const char *server, unsigned int timeout_ms,
  352. struct sys_datetime *out)
  353. {
  354. unsigned int pid = caller_pid();
  355. int acquired = 0;
  356. int r;
  357. if (!netdev_active())
  358. return SYS_NTP_ENONIC;
  359. if (netdev_in_use()) {
  360. if (netdev_owner() != pid)
  361. return SYS_NTP_EBUSY;
  362. } else if (tcp_client_count() != 0) {
  363. return SYS_NTP_EBUSY;
  364. } else {
  365. netdev_acquire();
  366. acquired = 1;
  367. }
  368. net_enter();
  369. r = ntp_sync(server, timeout_ms, out);
  370. net_exit();
  371. if (acquired)
  372. netdev_release();
  373. return r;
  374. }
  375. /* ---- TCP client ---- */
  376. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  377. unsigned int port)
  378. {
  379. return tcp_client_connect(caller_pid(), mac, ip, port);
  380. }
  381. int sys_tcp_peer_mss(void)
  382. {
  383. return tcp_client_peer_mss(caller_pid());
  384. }
  385. int sys_tcp_send(const void *buf, unsigned int len)
  386. {
  387. return tcp_client_send(caller_pid(), buf, len);
  388. }
  389. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  390. {
  391. return tcp_client_recv(caller_pid(), buf, max,
  392. get_ticks_ms() + timeout_ms);
  393. }
  394. void sys_tcp_close(void)
  395. {
  396. tcp_client_close(caller_pid());
  397. }
  398. /* ---- TCP server ---- */
  399. int sys_tcp_listen(unsigned int port)
  400. {
  401. if (!owns_nic() || port == 0 || port > 65535)
  402. return -1;
  403. net_enter();
  404. tcp_srv_init(port);
  405. net_exit();
  406. srv_conn = 0;
  407. return 0;
  408. }
  409. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  410. * netkexec header back to the kworker (the server exits, the watcher picks
  411. * the transfer up without waiting for the host's retry). */
  412. int sys_tcp_accept(void)
  413. {
  414. const struct netdev *nd = netdev_active();
  415. unsigned char *rx;
  416. struct tcp_conn *c;
  417. int i, len;
  418. if (!nd || !owns_nic())
  419. return -2;
  420. rx = netdev_rx_scratch();
  421. net_enter();
  422. for (i = 0; i < 16; i++) {
  423. len = nd->rx(rx, NETDEV_FRAME_MAX);
  424. if (len < 0)
  425. break;
  426. if (tcp_srv_input(rx, (unsigned int)len))
  427. continue;
  428. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  429. net_exit();
  430. return -3;
  431. }
  432. {
  433. int rlen = net_build_reply(rx, (unsigned int)len,
  434. srv_reply,
  435. sizeof(srv_reply));
  436. if (rlen > 0)
  437. nd->tx(srv_reply, (unsigned int)rlen);
  438. }
  439. }
  440. c = tcp_srv_accept();
  441. net_exit();
  442. if (!c)
  443. return 0;
  444. srv_conn = c;
  445. return 1;
  446. }
  447. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  448. {
  449. int r;
  450. if (!owns_nic() || !srv_conn)
  451. return -2;
  452. net_enter();
  453. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  454. net_exit();
  455. return r;
  456. }
  457. int sys_tcp_srv_send(const void *buf, unsigned int len)
  458. {
  459. int r;
  460. if (!owns_nic() || !srv_conn)
  461. return -3;
  462. net_enter();
  463. r = tcp_srv_send(srv_conn, buf, len);
  464. net_exit();
  465. return r;
  466. }
  467. void sys_tcp_srv_close(void)
  468. {
  469. if (owns_nic() && srv_conn) {
  470. net_enter();
  471. tcp_srv_close(srv_conn);
  472. net_exit();
  473. }
  474. srv_conn = 0;
  475. }
  476. /* Peer of the accepted connection (for the server's connection log). */
  477. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  478. {
  479. unsigned int i;
  480. if (!srv_conn)
  481. return -1;
  482. if (ip)
  483. for (i = 0; i < 4; i++)
  484. ip[i] = srv_conn->dst_ip[i];
  485. if (port)
  486. *port = srv_conn->dport;
  487. return 0;
  488. }
  489. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  490. {
  491. net_next_hop(dst, hop);
  492. return 0;
  493. }
  494. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  495. {
  496. int r;
  497. /* Raw owners keep the direct poll path; stack users share the pump. */
  498. if (netdev_in_use() && !owns_nic())
  499. return -2;
  500. if (owns_nic()) {
  501. net_enter();
  502. r = net_arp_resolve(ip, mac);
  503. net_exit();
  504. return r;
  505. }
  506. return net_arp_resolve(ip, mac);
  507. }
  508. int sys_net_configured(void)
  509. {
  510. return net_is_configured();
  511. }
  512. int sys_netlog(const char *text)
  513. {
  514. static const unsigned char bcast_mac[6] = {
  515. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  516. };
  517. unsigned char dst[4];
  518. unsigned int len = 0;
  519. while (text[len] && len < NET_UDP_MAX)
  520. len++;
  521. netlog_get_host(dst);
  522. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  523. }
  524. int sys_netlog_conf(int enabled, const unsigned char *ip)
  525. {
  526. if (enabled >= 0)
  527. netlog_set_enabled(enabled);
  528. if (ip)
  529. netlog_set_host(ip);
  530. return 0;
  531. }