sys_net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. netdev_byte_counters(&out->rx_bytes, &out->tx_bytes);
  134. return 1;
  135. }
  136. /* Probe every registered driver once and return the ones that are present.
  137. * The menu calls this when it opens, so a plain USB dongle or phone that was
  138. * not the boot-time active NIC still shows up. Driver init() can wait on the
  139. * PHY/USB, so it runs with interrupts on and the BKL released. */
  140. int sys_net_devlist(struct sys_netdev *out, unsigned int max)
  141. {
  142. const struct netdev *act;
  143. unsigned int i, n = 0;
  144. if (!out || !max)
  145. return 0;
  146. net_enter();
  147. act = netdev_active();
  148. for (i = 0; i < netdev_count() && n < max; i++) {
  149. const struct netdev *nd = netdev_at(i);
  150. unsigned int j;
  151. int present;
  152. if (!nd)
  153. continue;
  154. present = (nd == act);
  155. if (!present && nd->init)
  156. present = nd->init() == 0;
  157. if (!present)
  158. continue;
  159. for (j = 0; j < sizeof(out[n]); j++)
  160. ((unsigned char *)&out[n])[j] = 0;
  161. nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
  162. nd_copy_name(out[n].chip, sizeof(out[n].chip), netdev_chip(nd));
  163. out[n].kind = nd_kind(nd);
  164. out[n].active = (nd == act);
  165. out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
  166. (nd->link_up ? (nd->link_up() ? 1 : 0)
  167. : 1));
  168. n++;
  169. }
  170. net_exit();
  171. return (int)n;
  172. }
  173. int sys_net_select(const char *name)
  174. {
  175. int r;
  176. if (!name || !*name)
  177. return -1;
  178. net_enter();
  179. r = netdev_select(name);
  180. net_exit();
  181. return r;
  182. }
  183. int sys_net_print_info(void)
  184. {
  185. const struct netdev *nd = netdev_active();
  186. if (!nd || !nd->print_info || tcp_client_count() != 0)
  187. return -1;
  188. /* print_info reads the PHY via MDIC and can wait on the PHY/ME
  189. * semaphore; the syscall stub entered with interrupts off. */
  190. net_enter();
  191. nd->print_info();
  192. net_exit();
  193. return 0;
  194. }
  195. /* The owner must be the caller: the syscall runs in its process context. */
  196. static int owns_nic(void)
  197. {
  198. return netdev_owner() == caller_pid();
  199. }
  200. int sys_net_acquire(void)
  201. {
  202. if (owns_nic())
  203. return 0;
  204. /* Raw commands consume every frame, so they cannot coexist with a
  205. * shared TCP client (whose data the pump must dispatch). */
  206. if (netdev_in_use() || tcp_client_count() != 0)
  207. return -1;
  208. netdev_acquire();
  209. return 0;
  210. }
  211. int sys_net_release(void)
  212. {
  213. if (owns_nic())
  214. netdev_release();
  215. return 0;
  216. }
  217. int sys_net_mac(unsigned char mac[6])
  218. {
  219. const struct netdev *nd = netdev_active();
  220. if (!nd || !nd->get_mac)
  221. return -1;
  222. nd->get_mac(mac);
  223. return 0;
  224. }
  225. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  226. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  227. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  228. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  229. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  230. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  231. int sys_net_tx(const unsigned char *buf, unsigned int len)
  232. {
  233. const struct netdev *nd = netdev_active();
  234. if (!nd || !owns_nic())
  235. return -1;
  236. return netdev_tx_frame(nd, buf, len);
  237. }
  238. int sys_net_rx(unsigned char *buf, unsigned int max)
  239. {
  240. const struct netdev *nd = netdev_active();
  241. if (!nd || !owns_nic())
  242. return -1;
  243. return netdev_rx_frame(nd, buf, max);
  244. }
  245. void sys_net_decode(const unsigned char *buf, unsigned int len)
  246. {
  247. net_decode_frame(buf, len);
  248. }
  249. /* The reply buffer is always a full frame (the callers carry one). */
  250. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  251. {
  252. if (!owns_nic())
  253. return -1;
  254. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  255. }
  256. int sys_net_loopback(int on)
  257. {
  258. const struct netdev *nd = netdev_active();
  259. if (!nd || !nd->set_loopback)
  260. return -1;
  261. return nd->set_loopback(on);
  262. }
  263. int sys_net_regs(void)
  264. {
  265. const struct netdev *nd = netdev_active();
  266. if (!nd || !nd->dump_regs || tcp_client_count() != 0)
  267. return -1;
  268. net_enter();
  269. nd->dump_regs();
  270. net_exit();
  271. return 0;
  272. }
  273. int sys_net_reset(void)
  274. {
  275. const struct netdev *nd = netdev_active();
  276. if (!nd || !nd->reset)
  277. return -1;
  278. if (tcp_client_count() != 0 ||
  279. (netdev_in_use() && !owns_nic()))
  280. return -1;
  281. /* Own the receiver for the whole reset, so the netlog kworker cannot
  282. * submit frames while the MAC and rings are being reinitialised. */
  283. netdev_acquire();
  284. /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
  285. * entered with interrupts off. */
  286. net_enter();
  287. nd->reset();
  288. net_exit();
  289. netdev_release();
  290. return 0;
  291. }
  292. /* net100 fallback: force 100M or restore auto. The driver drops and
  293. * renegotiates the link, which can take a few seconds; own the receiver so
  294. * the background watcher cannot poll frames while the link is down. */
  295. int sys_net_speed(unsigned int mbps)
  296. {
  297. const struct netdev *nd = netdev_active();
  298. int r;
  299. if (!nd || !nd->set_speed)
  300. return -1;
  301. if (sys_net_acquire() != 0)
  302. return -2;
  303. net_enter();
  304. r = nd->set_speed(mbps);
  305. net_exit();
  306. sys_net_release();
  307. return r;
  308. }
  309. int sys_net_mac_set(const unsigned char mac[6])
  310. {
  311. const struct netdev *nd = netdev_active();
  312. if (!nd || !nd->set_mac)
  313. return -1;
  314. if (sys_net_acquire() != 0)
  315. return -2;
  316. nd->set_mac(mac);
  317. sys_net_release();
  318. return 0;
  319. }
  320. int sys_dhcp_run(unsigned int timeout_ms)
  321. {
  322. int r;
  323. if (netdev_in_use() || tcp_client_count() != 0)
  324. return -1;
  325. net_enter();
  326. r = dhcp_run(timeout_ms);
  327. net_exit();
  328. return r;
  329. }
  330. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  331. {
  332. return dhcp_last_info(server, lease);
  333. }
  334. int sys_dns_resolve(const char *name, unsigned char ip[4])
  335. {
  336. int r;
  337. /* A raw owner keeps its direct poll path; stack users share the pump.
  338. * Refuse only when another process owns the raw lease. */
  339. if (netdev_in_use() && !owns_nic())
  340. return -1;
  341. if (owns_nic()) {
  342. net_enter();
  343. r = dns_resolve(name, ip);
  344. net_exit();
  345. return r;
  346. }
  347. return dns_resolve(name, ip);
  348. }
  349. /* SNTP sync for ring-3 callers. The service owns the receiver for the
  350. * duration (unless the caller already does), so settime does not have to
  351. * take SYS_NET_ACQUIRE itself. A second network user gets SYS_NTP_EBUSY. */
  352. int sys_ntp_sync(const char *server, unsigned int timeout_ms,
  353. struct sys_datetime *out)
  354. {
  355. unsigned int pid = caller_pid();
  356. int acquired = 0;
  357. int r;
  358. if (!netdev_active())
  359. return SYS_NTP_ENONIC;
  360. if (netdev_in_use()) {
  361. if (netdev_owner() != pid)
  362. return SYS_NTP_EBUSY;
  363. } else if (tcp_client_count() != 0) {
  364. return SYS_NTP_EBUSY;
  365. } else {
  366. netdev_acquire();
  367. acquired = 1;
  368. }
  369. net_enter();
  370. r = ntp_sync(server, timeout_ms, out);
  371. net_exit();
  372. if (acquired)
  373. netdev_release();
  374. return r;
  375. }
  376. /* ---- TCP client ---- */
  377. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  378. unsigned int port)
  379. {
  380. return tcp_client_connect(caller_pid(), mac, ip, port);
  381. }
  382. int sys_tcp_peer_mss(void)
  383. {
  384. return tcp_client_peer_mss(caller_pid());
  385. }
  386. int sys_tcp_send(const void *buf, unsigned int len)
  387. {
  388. return tcp_client_send(caller_pid(), buf, len);
  389. }
  390. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  391. {
  392. return tcp_client_recv(caller_pid(), buf, max,
  393. get_ticks_ms() + timeout_ms);
  394. }
  395. void sys_tcp_close(void)
  396. {
  397. tcp_client_close(caller_pid());
  398. }
  399. /* ---- TCP server ---- */
  400. int sys_tcp_listen(unsigned int port)
  401. {
  402. if (!owns_nic() || port == 0 || port > 65535)
  403. return -1;
  404. net_enter();
  405. tcp_srv_init(port);
  406. net_exit();
  407. srv_conn = 0;
  408. return 0;
  409. }
  410. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  411. * netkexec header back to the kworker (the server exits, the watcher picks
  412. * the transfer up without waiting for the host's retry). */
  413. int sys_tcp_accept(void)
  414. {
  415. const struct netdev *nd = netdev_active();
  416. unsigned char *rx;
  417. struct tcp_conn *c;
  418. int i, len;
  419. if (!nd || !owns_nic())
  420. return -2;
  421. rx = netdev_rx_scratch();
  422. net_enter();
  423. for (i = 0; i < 16; i++) {
  424. len = netdev_rx_frame(nd, rx, NETDEV_FRAME_MAX);
  425. if (len < 0)
  426. break;
  427. if (tcp_srv_input(rx, (unsigned int)len))
  428. continue;
  429. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  430. net_exit();
  431. return -3;
  432. }
  433. {
  434. int rlen = net_build_reply(rx, (unsigned int)len,
  435. srv_reply,
  436. sizeof(srv_reply));
  437. if (rlen > 0)
  438. netdev_tx_frame(nd, srv_reply, (unsigned int)rlen);
  439. }
  440. }
  441. c = tcp_srv_accept();
  442. net_exit();
  443. if (!c)
  444. return 0;
  445. srv_conn = c;
  446. return 1;
  447. }
  448. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  449. {
  450. int r;
  451. if (!owns_nic() || !srv_conn)
  452. return -2;
  453. net_enter();
  454. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  455. net_exit();
  456. return r;
  457. }
  458. int sys_tcp_srv_send(const void *buf, unsigned int len)
  459. {
  460. int r;
  461. if (!owns_nic() || !srv_conn)
  462. return -3;
  463. net_enter();
  464. r = tcp_srv_send(srv_conn, buf, len);
  465. net_exit();
  466. return r;
  467. }
  468. void sys_tcp_srv_close(void)
  469. {
  470. if (owns_nic() && srv_conn) {
  471. net_enter();
  472. tcp_srv_close(srv_conn);
  473. net_exit();
  474. }
  475. srv_conn = 0;
  476. }
  477. /* Peer of the accepted connection (for the server's connection log). */
  478. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  479. {
  480. unsigned int i;
  481. if (!srv_conn)
  482. return -1;
  483. if (ip)
  484. for (i = 0; i < 4; i++)
  485. ip[i] = srv_conn->dst_ip[i];
  486. if (port)
  487. *port = srv_conn->dport;
  488. return 0;
  489. }
  490. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  491. {
  492. net_next_hop(dst, hop);
  493. return 0;
  494. }
  495. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  496. {
  497. int r;
  498. /* Raw owners keep the direct poll path; stack users share the pump. */
  499. if (netdev_in_use() && !owns_nic())
  500. return -2;
  501. if (owns_nic()) {
  502. net_enter();
  503. r = net_arp_resolve(ip, mac);
  504. net_exit();
  505. return r;
  506. }
  507. return net_arp_resolve(ip, mac);
  508. }
  509. int sys_net_configured(void)
  510. {
  511. return net_is_configured();
  512. }
  513. int sys_netlog(const char *text)
  514. {
  515. static const unsigned char bcast_mac[6] = {
  516. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  517. };
  518. unsigned char dst[4];
  519. unsigned int len = 0;
  520. while (text[len] && len < NET_UDP_MAX)
  521. len++;
  522. netlog_get_host(dst);
  523. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  524. }
  525. int sys_netlog_conf(int enabled, const unsigned char *ip)
  526. {
  527. if (enabled >= 0)
  528. netlog_set_enabled(enabled);
  529. if (ip)
  530. netlog_set_host(ip);
  531. return 0;
  532. }