sys_net.c 13 KB

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