sys_net.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. static unsigned int caller_pid(void)
  19. {
  20. return procs[current_pid].pid;
  21. }
  22. /* Called from the scheduler's reap loop for every dead process. */
  23. void sys_net_reap(unsigned int pid)
  24. {
  25. if (netdev_owner() == pid)
  26. netdev_release_all();
  27. }
  28. int sys_net_info(struct sys_netinfo *out)
  29. {
  30. const struct netdev *nd = netdev_active();
  31. unsigned int i;
  32. for (i = 0; i < sizeof(*out); i++)
  33. ((unsigned char *)out)[i] = 0;
  34. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  35. netlog_get_host(out->mirror_host);
  36. if (!nd)
  37. return 0;
  38. out->present = 1;
  39. if (nd->get_mac)
  40. nd->get_mac(out->mac);
  41. net_get_ip(out->ip);
  42. net_get_mask(out->mask);
  43. net_get_gateway(out->gw);
  44. net_get_dns(out->dns);
  45. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  46. out->name[i] = nd->name[i];
  47. for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
  48. out->chip[i] = nd->chip[i];
  49. return 1;
  50. }
  51. int sys_net_print_info(void)
  52. {
  53. const struct netdev *nd = netdev_active();
  54. if (!nd || !nd->print_info)
  55. return -1;
  56. nd->print_info();
  57. return 0;
  58. }
  59. /* The owner must be the caller: the syscall runs in its process context. */
  60. static int owns_nic(void)
  61. {
  62. return netdev_owner() == caller_pid();
  63. }
  64. int sys_net_acquire(void)
  65. {
  66. if (owns_nic())
  67. return 0;
  68. if (netdev_in_use())
  69. return -1;
  70. netdev_acquire();
  71. return 0;
  72. }
  73. int sys_net_release(void)
  74. {
  75. if (owns_nic())
  76. netdev_release();
  77. return 0;
  78. }
  79. int sys_net_mac(unsigned char mac[6])
  80. {
  81. const struct netdev *nd = netdev_active();
  82. if (!nd || !nd->get_mac)
  83. return -1;
  84. nd->get_mac(mac);
  85. return 0;
  86. }
  87. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  88. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  89. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  90. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  91. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  92. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  93. int sys_net_tx(const unsigned char *buf, unsigned int len)
  94. {
  95. const struct netdev *nd = netdev_active();
  96. if (!nd || !owns_nic())
  97. return -1;
  98. return nd->tx(buf, len);
  99. }
  100. int sys_net_rx(unsigned char *buf, unsigned int max)
  101. {
  102. const struct netdev *nd = netdev_active();
  103. if (!nd || !owns_nic())
  104. return -1;
  105. return nd->rx(buf, max);
  106. }
  107. void sys_net_decode(const unsigned char *buf, unsigned int len)
  108. {
  109. net_decode_frame(buf, len);
  110. }
  111. /* The reply buffer is always a full frame (the callers carry one). */
  112. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  113. {
  114. if (!owns_nic())
  115. return -1;
  116. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  117. }
  118. int sys_net_loopback(int on)
  119. {
  120. const struct netdev *nd = netdev_active();
  121. if (!nd || !nd->set_loopback)
  122. return -1;
  123. return nd->set_loopback(on);
  124. }
  125. int sys_net_regs(void)
  126. {
  127. const struct netdev *nd = netdev_active();
  128. if (!nd || !nd->dump_regs)
  129. return -1;
  130. nd->dump_regs();
  131. return 0;
  132. }
  133. int sys_net_reset(void)
  134. {
  135. const struct netdev *nd = netdev_active();
  136. if (!nd || !nd->reset)
  137. return -1;
  138. nd->reset();
  139. return 0;
  140. }
  141. int sys_dhcp_run(unsigned int timeout_ms)
  142. {
  143. return dhcp_run(timeout_ms);
  144. }
  145. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  146. {
  147. return dhcp_last_info(server, lease);
  148. }
  149. int sys_dns_resolve(const char *name, unsigned char ip[4])
  150. {
  151. if (!owns_nic())
  152. return -1;
  153. return dns_resolve(name, ip);
  154. }
  155. int sys_netlog(const char *text)
  156. {
  157. static const unsigned char bcast_mac[6] = {
  158. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  159. };
  160. unsigned char dst[4];
  161. unsigned int len = 0;
  162. while (text[len] && len < NET_UDP_MAX)
  163. len++;
  164. netlog_get_host(dst);
  165. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  166. }
  167. int sys_netlog_conf(int enabled, const unsigned char *ip)
  168. {
  169. if (enabled >= 0)
  170. netlog_set_enabled(enabled);
  171. if (ip)
  172. netlog_set_host(ip);
  173. return 0;
  174. }