| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- /* Network service for ring-3 commands (see syscall.h for the ABI).
- *
- * The stack (net.c/dns.c/tcp.c) and the NIC drivers stay in the kernel; user
- * commands get the configuration, the DNS/DHCP clients and the raw frame
- * paths, so the packet-level commands (ping, pong, netrecv, ...) port over
- * their existing frame code.
- *
- * Packet-level commands take an exclusive raw lease (netdev_acquire); the
- * background kexec watcher pauses while one owns the receiver, and the reaper
- * releases it if that process dies. TCP clients do NOT take that lease: each
- * process may hold one connection in tcp.c, and every stack syscall drives
- * the shared net_pump() so several apps can transfer concurrently. */
- #include "syscall.h"
- #include "dhcp.h"
- #include "dns.h"
- #include "io.h"
- #include "net.h"
- #include "netcore.h"
- #include "netdev.h"
- #include "netkexec.h"
- #include "netlog.h"
- #include "ntp.h"
- #include "process.h"
- #include "tcp.h"
- #include "timer.h"
- /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
- static struct tcp_conn *srv_conn;
- /* Reply buffer for ARP/ICMP answered while listening (the rx scratch is
- * occupied by the received frame). */
- static unsigned char srv_reply[NETDEV_FRAME_MAX];
- /* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
- * they need interrupts: the syscall stub entered with them off, and a frozen
- * tick counter would never satisfy a deadline. The netdev ownership already
- * serializes net users, so running with IRQs on (and being preempted) is
- * safe. */
- static void net_enter(void)
- {
- /* ARP/DNS/DHCP/TCP waits spin on the PIT and can take seconds. Give
- * the other CPU's syscalls (and the WM) the kernel while we wait; the
- * NIC ownership still serializes every stack user. */
- bkl_pause();
- __asm__ volatile ("sti");
- }
- static void net_exit(void)
- {
- __asm__ volatile ("cli");
- bkl_resume();
- }
- static unsigned int caller_pid(void)
- {
- return current_proc()->pid;
- }
- static int nd_name_eq(const char *a, const char *b)
- {
- while (*a && *b) {
- if (*a != *b)
- return 0;
- a++;
- b++;
- }
- return *a == *b;
- }
- static void nd_copy_name(char *dst, unsigned int max, const char *src)
- {
- unsigned int i;
- for (i = 0; src && src[i] && i + 1 < max; i++)
- dst[i] = src[i];
- dst[i] = '\0';
- }
- static unsigned int nd_kind(const struct netdev *nd)
- {
- if (nd_name_eq(nd->name, "wifi"))
- return SYS_NETDEV_WIFI;
- if (nd_name_eq(nd->name, "rndis"))
- return SYS_NETDEV_USB;
- return SYS_NETDEV_WIRED;
- }
- /* Called from the scheduler's reap loop for every dead process. */
- void sys_net_reap(unsigned int pid)
- {
- if (netdev_owner() == pid) {
- netdev_release_all();
- tcp_srv_reset();
- srv_conn = 0;
- }
- tcp_client_reap(pid);
- dns_reap(pid);
- net_stack_reap(pid);
- }
- int sys_net_info(struct sys_netinfo *out)
- {
- const struct netdev *nd;
- unsigned int i;
- for (i = 0; i < sizeof(*out); i++)
- ((unsigned char *)out)[i] = 0;
- out->mirror_on = netlog_get_enabled() ? 1 : 0;
- netlog_get_host(out->mirror_host);
- /* The first SYS_NET_INFO may be the first NIC probe (the WM's top-bar
- * poll runs before the boot DHCP on some boots). Driver init waits on
- * the PHY/USB with tick-based delays, so run it with interrupts on and
- * the BKL released. Once probed, this is just a cached lookup. */
- if (!netdev_probed()) {
- net_enter();
- nd = netdev_active();
- net_exit();
- } else {
- nd = netdev_active();
- }
- if (!nd)
- return 0;
- out->present = 1;
- if (nd->get_mac)
- nd->get_mac(out->mac);
- net_get_ip(out->ip);
- net_get_mask(out->mask);
- net_get_gateway(out->gw);
- net_get_dns(out->dns);
- for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
- out->name[i] = nd->name[i];
- for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
- out->chip[i] = nd->chip[i];
- out->link = (unsigned int)netdev_link(nd);
- return 1;
- }
- /* Probe every registered driver once and return the ones that are present.
- * The menu calls this when it opens, so a plain USB dongle or phone that was
- * not the boot-time active NIC still shows up. Driver init() can wait on the
- * PHY/USB, so it runs with interrupts on and the BKL released. */
- int sys_net_devlist(struct sys_netdev *out, unsigned int max)
- {
- const struct netdev *act;
- unsigned int i, n = 0;
- if (!out || !max)
- return 0;
- net_enter();
- act = netdev_active();
- for (i = 0; i < netdev_count() && n < max; i++) {
- const struct netdev *nd = netdev_at(i);
- unsigned int j;
- int present;
- if (!nd)
- continue;
- present = (nd == act);
- if (!present && nd->init)
- present = nd->init() == 0;
- if (!present)
- continue;
- for (j = 0; j < sizeof(out[n]); j++)
- ((unsigned char *)&out[n])[j] = 0;
- nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
- nd_copy_name(out[n].chip, sizeof(out[n].chip), nd->chip);
- out[n].kind = nd_kind(nd);
- out[n].active = (nd == act);
- out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
- (nd->link_up ? (nd->link_up() ? 1 : 0)
- : 1));
- n++;
- }
- net_exit();
- return (int)n;
- }
- int sys_net_select(const char *name)
- {
- int r;
- if (!name || !*name)
- return -1;
- net_enter();
- r = netdev_select(name);
- net_exit();
- return r;
- }
- int sys_net_print_info(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->print_info || tcp_client_count() != 0)
- return -1;
- /* print_info reads the PHY via MDIC and can wait on the PHY/ME
- * semaphore; the syscall stub entered with interrupts off. */
- net_enter();
- nd->print_info();
- net_exit();
- return 0;
- }
- /* The owner must be the caller: the syscall runs in its process context. */
- static int owns_nic(void)
- {
- return netdev_owner() == caller_pid();
- }
- int sys_net_acquire(void)
- {
- if (owns_nic())
- return 0;
- /* Raw commands consume every frame, so they cannot coexist with a
- * shared TCP client (whose data the pump must dispatch). */
- if (netdev_in_use() || tcp_client_count() != 0)
- return -1;
- netdev_acquire();
- return 0;
- }
- int sys_net_release(void)
- {
- if (owns_nic())
- netdev_release();
- return 0;
- }
- int sys_net_mac(unsigned char mac[6])
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->get_mac)
- return -1;
- nd->get_mac(mac);
- return 0;
- }
- void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
- void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
- void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
- void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
- void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
- void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
- int sys_net_tx(const unsigned char *buf, unsigned int len)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !owns_nic())
- return -1;
- return nd->tx(buf, len);
- }
- int sys_net_rx(unsigned char *buf, unsigned int max)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !owns_nic())
- return -1;
- return nd->rx(buf, max);
- }
- void sys_net_decode(const unsigned char *buf, unsigned int len)
- {
- net_decode_frame(buf, len);
- }
- /* The reply buffer is always a full frame (the callers carry one). */
- int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
- {
- if (!owns_nic())
- return -1;
- return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
- }
- int sys_net_loopback(int on)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->set_loopback)
- return -1;
- return nd->set_loopback(on);
- }
- int sys_net_regs(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->dump_regs || tcp_client_count() != 0)
- return -1;
- net_enter();
- nd->dump_regs();
- net_exit();
- return 0;
- }
- int sys_net_reset(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->reset)
- return -1;
- if (tcp_client_count() != 0 ||
- (netdev_in_use() && !owns_nic()))
- return -1;
- /* Own the receiver for the whole reset, so the netlog kworker cannot
- * submit frames while the MAC and rings are being reinitialised. */
- netdev_acquire();
- /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
- * entered with interrupts off. */
- net_enter();
- nd->reset();
- net_exit();
- netdev_release();
- return 0;
- }
- /* net100 fallback: force 100M or restore auto. The driver drops and
- * renegotiates the link, which can take a few seconds; own the receiver so
- * the background watcher cannot poll frames while the link is down. */
- int sys_net_speed(unsigned int mbps)
- {
- const struct netdev *nd = netdev_active();
- int r;
- if (!nd || !nd->set_speed)
- return -1;
- if (sys_net_acquire() != 0)
- return -2;
- net_enter();
- r = nd->set_speed(mbps);
- net_exit();
- sys_net_release();
- return r;
- }
- int sys_net_mac_set(const unsigned char mac[6])
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->set_mac)
- return -1;
- if (sys_net_acquire() != 0)
- return -2;
- nd->set_mac(mac);
- sys_net_release();
- return 0;
- }
- int sys_dhcp_run(unsigned int timeout_ms)
- {
- int r;
- if (netdev_in_use() || tcp_client_count() != 0)
- return -1;
- net_enter();
- r = dhcp_run(timeout_ms);
- net_exit();
- return r;
- }
- int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
- {
- return dhcp_last_info(server, lease);
- }
- int sys_dns_resolve(const char *name, unsigned char ip[4])
- {
- int r;
- /* A raw owner keeps its direct poll path; stack users share the pump.
- * Refuse only when another process owns the raw lease. */
- if (netdev_in_use() && !owns_nic())
- return -1;
- if (owns_nic()) {
- net_enter();
- r = dns_resolve(name, ip);
- net_exit();
- return r;
- }
- return dns_resolve(name, ip);
- }
- /* SNTP sync for ring-3 callers. The service owns the receiver for the
- * duration (unless the caller already does), so settime does not have to
- * take SYS_NET_ACQUIRE itself. A second network user gets SYS_NTP_EBUSY. */
- int sys_ntp_sync(const char *server, unsigned int timeout_ms,
- struct sys_datetime *out)
- {
- unsigned int pid = caller_pid();
- int acquired = 0;
- int r;
- if (!netdev_active())
- return SYS_NTP_ENONIC;
- if (netdev_in_use()) {
- if (netdev_owner() != pid)
- return SYS_NTP_EBUSY;
- } else if (tcp_client_count() != 0) {
- return SYS_NTP_EBUSY;
- } else {
- netdev_acquire();
- acquired = 1;
- }
- net_enter();
- r = ntp_sync(server, timeout_ms, out);
- net_exit();
- if (acquired)
- netdev_release();
- return r;
- }
- /* ---- TCP client ---- */
- int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
- unsigned int port)
- {
- return tcp_client_connect(caller_pid(), mac, ip, port);
- }
- int sys_tcp_peer_mss(void)
- {
- return tcp_client_peer_mss(caller_pid());
- }
- int sys_tcp_send(const void *buf, unsigned int len)
- {
- return tcp_client_send(caller_pid(), buf, len);
- }
- int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
- {
- return tcp_client_recv(caller_pid(), buf, max,
- get_ticks_ms() + timeout_ms);
- }
- void sys_tcp_close(void)
- {
- tcp_client_close(caller_pid());
- }
- /* ---- TCP server ---- */
- int sys_tcp_listen(unsigned int port)
- {
- if (!owns_nic() || port == 0 || port > 65535)
- return -1;
- net_enter();
- tcp_srv_init(port);
- net_exit();
- srv_conn = 0;
- return 0;
- }
- /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
- * netkexec header back to the kworker (the server exits, the watcher picks
- * the transfer up without waiting for the host's retry). */
- int sys_tcp_accept(void)
- {
- const struct netdev *nd = netdev_active();
- unsigned char *rx;
- struct tcp_conn *c;
- int i, len;
- if (!nd || !owns_nic())
- return -2;
- rx = netdev_rx_scratch();
- net_enter();
- for (i = 0; i < 16; i++) {
- len = nd->rx(rx, NETDEV_FRAME_MAX);
- if (len < 0)
- break;
- if (tcp_srv_input(rx, (unsigned int)len))
- continue;
- if (netkexec_capture_frame(rx, (unsigned int)len)) {
- net_exit();
- return -3;
- }
- {
- int rlen = net_build_reply(rx, (unsigned int)len,
- srv_reply,
- sizeof(srv_reply));
- if (rlen > 0)
- nd->tx(srv_reply, (unsigned int)rlen);
- }
- }
- c = tcp_srv_accept();
- net_exit();
- if (!c)
- return 0;
- srv_conn = c;
- return 1;
- }
- int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
- {
- int r;
- if (!owns_nic() || !srv_conn)
- return -2;
- net_enter();
- r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
- net_exit();
- return r;
- }
- int sys_tcp_srv_send(const void *buf, unsigned int len)
- {
- int r;
- if (!owns_nic() || !srv_conn)
- return -3;
- net_enter();
- r = tcp_srv_send(srv_conn, buf, len);
- net_exit();
- return r;
- }
- void sys_tcp_srv_close(void)
- {
- if (owns_nic() && srv_conn) {
- net_enter();
- tcp_srv_close(srv_conn);
- net_exit();
- }
- srv_conn = 0;
- }
- /* Peer of the accepted connection (for the server's connection log). */
- int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
- {
- unsigned int i;
- if (!srv_conn)
- return -1;
- if (ip)
- for (i = 0; i < 4; i++)
- ip[i] = srv_conn->dst_ip[i];
- if (port)
- *port = srv_conn->dport;
- return 0;
- }
- int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
- {
- net_next_hop(dst, hop);
- return 0;
- }
- int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
- {
- int r;
- /* Raw owners keep the direct poll path; stack users share the pump. */
- if (netdev_in_use() && !owns_nic())
- return -2;
- if (owns_nic()) {
- net_enter();
- r = net_arp_resolve(ip, mac);
- net_exit();
- return r;
- }
- return net_arp_resolve(ip, mac);
- }
- int sys_net_configured(void)
- {
- return net_is_configured();
- }
- int sys_netlog(const char *text)
- {
- static const unsigned char bcast_mac[6] = {
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
- };
- unsigned char dst[4];
- unsigned int len = 0;
- while (text[len] && len < NET_UDP_MAX)
- len++;
- netlog_get_host(dst);
- return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
- }
- int sys_netlog_conf(int enabled, const unsigned char *ip)
- {
- if (enabled >= 0)
- netlog_set_enabled(enabled);
- if (ip)
- netlog_set_host(ip);
- return 0;
- }
|