sys_net.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. The receiver is owned by one process at a time
  7. * (netdev_acquire/release): the background kexec watcher pauses while a
  8. * command owns it, and the reaper force-releases it if that process dies
  9. * (sys_net_reap). */
  10. #include "syscall.h"
  11. #include "dhcp.h"
  12. #include "dns.h"
  13. #include "io.h"
  14. #include "net.h"
  15. #include "netdev.h"
  16. #include "netlog.h"
  17. #include "process.h"
  18. #include "tcp.h"
  19. #include "timer.h"
  20. /* One TCP connection at a time (the stack is single-connection, and the
  21. * receiver is owned by the process using it). */
  22. static struct tcp_conn tcp;
  23. /* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
  24. * they need interrupts: the syscall stub entered with them off, and a frozen
  25. * tick counter would never satisfy a deadline. The netdev ownership already
  26. * serializes net users, so running with IRQs on (and being preempted) is
  27. * safe. */
  28. static void net_enter(void)
  29. {
  30. /* ARP/DNS/DHCP/TCP waits spin on the PIT and can take seconds. Give
  31. * the other CPU's syscalls (and the WM) the kernel while we wait; the
  32. * NIC ownership still serializes every stack user. */
  33. bkl_pause();
  34. __asm__ volatile ("sti");
  35. }
  36. static void net_exit(void)
  37. {
  38. __asm__ volatile ("cli");
  39. bkl_resume();
  40. }
  41. static unsigned int caller_pid(void)
  42. {
  43. return current_proc()->pid;
  44. }
  45. /* Called from the scheduler's reap loop for every dead process. */
  46. void sys_net_reap(unsigned int pid)
  47. {
  48. if (netdev_owner() == pid)
  49. netdev_release_all();
  50. }
  51. int sys_net_info(struct sys_netinfo *out)
  52. {
  53. const struct netdev *nd = netdev_active();
  54. unsigned int i;
  55. for (i = 0; i < sizeof(*out); i++)
  56. ((unsigned char *)out)[i] = 0;
  57. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  58. netlog_get_host(out->mirror_host);
  59. if (!nd)
  60. return 0;
  61. out->present = 1;
  62. if (nd->get_mac)
  63. nd->get_mac(out->mac);
  64. net_get_ip(out->ip);
  65. net_get_mask(out->mask);
  66. net_get_gateway(out->gw);
  67. net_get_dns(out->dns);
  68. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  69. out->name[i] = nd->name[i];
  70. for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
  71. out->chip[i] = nd->chip[i];
  72. return 1;
  73. }
  74. int sys_net_print_info(void)
  75. {
  76. const struct netdev *nd = netdev_active();
  77. if (!nd || !nd->print_info)
  78. return -1;
  79. nd->print_info();
  80. return 0;
  81. }
  82. /* The owner must be the caller: the syscall runs in its process context. */
  83. static int owns_nic(void)
  84. {
  85. return netdev_owner() == caller_pid();
  86. }
  87. int sys_net_acquire(void)
  88. {
  89. if (owns_nic())
  90. return 0;
  91. if (netdev_in_use())
  92. return -1;
  93. netdev_acquire();
  94. return 0;
  95. }
  96. int sys_net_release(void)
  97. {
  98. if (owns_nic())
  99. netdev_release();
  100. return 0;
  101. }
  102. int sys_net_mac(unsigned char mac[6])
  103. {
  104. const struct netdev *nd = netdev_active();
  105. if (!nd || !nd->get_mac)
  106. return -1;
  107. nd->get_mac(mac);
  108. return 0;
  109. }
  110. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  111. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  112. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  113. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  114. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  115. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  116. int sys_net_tx(const unsigned char *buf, unsigned int len)
  117. {
  118. const struct netdev *nd = netdev_active();
  119. if (!nd || !owns_nic())
  120. return -1;
  121. return nd->tx(buf, len);
  122. }
  123. int sys_net_rx(unsigned char *buf, unsigned int max)
  124. {
  125. const struct netdev *nd = netdev_active();
  126. if (!nd || !owns_nic())
  127. return -1;
  128. return nd->rx(buf, max);
  129. }
  130. void sys_net_decode(const unsigned char *buf, unsigned int len)
  131. {
  132. net_decode_frame(buf, len);
  133. }
  134. /* The reply buffer is always a full frame (the callers carry one). */
  135. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  136. {
  137. if (!owns_nic())
  138. return -1;
  139. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  140. }
  141. int sys_net_loopback(int on)
  142. {
  143. const struct netdev *nd = netdev_active();
  144. if (!nd || !nd->set_loopback)
  145. return -1;
  146. return nd->set_loopback(on);
  147. }
  148. int sys_net_regs(void)
  149. {
  150. const struct netdev *nd = netdev_active();
  151. if (!nd || !nd->dump_regs)
  152. return -1;
  153. nd->dump_regs();
  154. return 0;
  155. }
  156. int sys_net_reset(void)
  157. {
  158. const struct netdev *nd = netdev_active();
  159. if (!nd || !nd->reset)
  160. return -1;
  161. nd->reset();
  162. return 0;
  163. }
  164. int sys_dhcp_run(unsigned int timeout_ms)
  165. {
  166. int r;
  167. net_enter();
  168. r = dhcp_run(timeout_ms);
  169. net_exit();
  170. return r;
  171. }
  172. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  173. {
  174. return dhcp_last_info(server, lease);
  175. }
  176. int sys_dns_resolve(const char *name, unsigned char ip[4])
  177. {
  178. int r;
  179. if (!owns_nic())
  180. return -1;
  181. net_enter();
  182. r = dns_resolve(name, ip);
  183. net_exit();
  184. return r;
  185. }
  186. /* ---- TCP client ---- */
  187. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  188. unsigned int port)
  189. {
  190. int r;
  191. if (!owns_nic())
  192. return -3;
  193. net_enter();
  194. r = tcp_connect(mac, ip, port, &tcp);
  195. net_exit();
  196. return r;
  197. }
  198. int sys_tcp_peer_mss(void)
  199. {
  200. return (int)tcp.peer_mss;
  201. }
  202. int sys_tcp_send(const void *buf, unsigned int len)
  203. {
  204. int r;
  205. if (!owns_nic())
  206. return -3;
  207. net_enter();
  208. r = tcp_send(&tcp, buf, len);
  209. net_exit();
  210. return r;
  211. }
  212. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  213. {
  214. int r;
  215. if (!owns_nic())
  216. return -2;
  217. net_enter();
  218. r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
  219. net_exit();
  220. return r;
  221. }
  222. void sys_tcp_close(void)
  223. {
  224. if (!owns_nic())
  225. return;
  226. net_enter();
  227. tcp_close(&tcp);
  228. net_exit();
  229. }
  230. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  231. {
  232. net_next_hop(dst, hop);
  233. return 0;
  234. }
  235. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  236. {
  237. int r;
  238. if (!owns_nic())
  239. return -2;
  240. net_enter();
  241. r = net_arp_resolve(ip, mac);
  242. net_exit();
  243. return r;
  244. }
  245. int sys_net_configured(void)
  246. {
  247. return net_is_configured();
  248. }
  249. int sys_netlog(const char *text)
  250. {
  251. static const unsigned char bcast_mac[6] = {
  252. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  253. };
  254. unsigned char dst[4];
  255. unsigned int len = 0;
  256. while (text[len] && len < NET_UDP_MAX)
  257. len++;
  258. netlog_get_host(dst);
  259. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  260. }
  261. int sys_netlog_conf(int enabled, const unsigned char *ip)
  262. {
  263. if (enabled >= 0)
  264. netlog_set_enabled(enabled);
  265. if (ip)
  266. netlog_set_host(ip);
  267. return 0;
  268. }