| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- /* 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. The receiver is owned by one process at a time
- * (netdev_acquire/release): the background kexec watcher pauses while a
- * command owns it, and the reaper force-releases it if that process dies
- * (sys_net_reap). */
- #include "syscall.h"
- #include "dhcp.h"
- #include "dns.h"
- #include "io.h"
- #include "net.h"
- #include "netdev.h"
- #include "netlog.h"
- #include "process.h"
- #include "tcp.h"
- #include "timer.h"
- /* One TCP connection at a time (the stack is single-connection, and the
- * receiver is owned by the process using it). */
- static struct tcp_conn tcp;
- /* 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)
- {
- __asm__ volatile ("sti");
- }
- static void net_exit(void)
- {
- __asm__ volatile ("cli");
- }
- static unsigned int caller_pid(void)
- {
- return current_proc()->pid;
- }
- /* 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();
- }
- int sys_net_info(struct sys_netinfo *out)
- {
- const struct netdev *nd = netdev_active();
- 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);
- 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];
- return 1;
- }
- int sys_net_print_info(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->print_info)
- return -1;
- nd->print_info();
- 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;
- if (netdev_in_use())
- 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)
- return -1;
- nd->dump_regs();
- return 0;
- }
- int sys_net_reset(void)
- {
- const struct netdev *nd = netdev_active();
- if (!nd || !nd->reset)
- return -1;
- nd->reset();
- return 0;
- }
- int sys_dhcp_run(unsigned int timeout_ms)
- {
- int r;
- 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;
- if (!owns_nic())
- return -1;
- net_enter();
- r = dns_resolve(name, ip);
- net_exit();
- return r;
- }
- /* ---- TCP client ---- */
- int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
- unsigned int port)
- {
- int r;
- if (!owns_nic())
- return -3;
- net_enter();
- r = tcp_connect(mac, ip, port, &tcp);
- net_exit();
- return r;
- }
- int sys_tcp_peer_mss(void)
- {
- return (int)tcp.peer_mss;
- }
- int sys_tcp_send(const void *buf, unsigned int len)
- {
- int r;
- if (!owns_nic())
- return -3;
- net_enter();
- r = tcp_send(&tcp, buf, len);
- net_exit();
- return r;
- }
- int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
- {
- int r;
- if (!owns_nic())
- return -2;
- net_enter();
- r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
- net_exit();
- return r;
- }
- void sys_tcp_close(void)
- {
- if (!owns_nic())
- return;
- net_enter();
- tcp_close(&tcp);
- net_exit();
- }
- 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;
- if (!owns_nic())
- return -2;
- net_enter();
- r = net_arp_resolve(ip, mac);
- net_exit();
- return r;
- }
- 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;
- }
|