sys_net.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. __asm__ volatile ("sti");
  31. }
  32. static void net_exit(void)
  33. {
  34. __asm__ volatile ("cli");
  35. }
  36. static unsigned int caller_pid(void)
  37. {
  38. return current_proc()->pid;
  39. }
  40. /* Called from the scheduler's reap loop for every dead process. */
  41. void sys_net_reap(unsigned int pid)
  42. {
  43. if (netdev_owner() == pid)
  44. netdev_release_all();
  45. }
  46. int sys_net_info(struct sys_netinfo *out)
  47. {
  48. const struct netdev *nd = netdev_active();
  49. unsigned int i;
  50. for (i = 0; i < sizeof(*out); i++)
  51. ((unsigned char *)out)[i] = 0;
  52. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  53. netlog_get_host(out->mirror_host);
  54. if (!nd)
  55. return 0;
  56. out->present = 1;
  57. if (nd->get_mac)
  58. nd->get_mac(out->mac);
  59. net_get_ip(out->ip);
  60. net_get_mask(out->mask);
  61. net_get_gateway(out->gw);
  62. net_get_dns(out->dns);
  63. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  64. out->name[i] = nd->name[i];
  65. for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
  66. out->chip[i] = nd->chip[i];
  67. return 1;
  68. }
  69. int sys_net_print_info(void)
  70. {
  71. const struct netdev *nd = netdev_active();
  72. if (!nd || !nd->print_info)
  73. return -1;
  74. nd->print_info();
  75. return 0;
  76. }
  77. /* The owner must be the caller: the syscall runs in its process context. */
  78. static int owns_nic(void)
  79. {
  80. return netdev_owner() == caller_pid();
  81. }
  82. int sys_net_acquire(void)
  83. {
  84. if (owns_nic())
  85. return 0;
  86. if (netdev_in_use())
  87. return -1;
  88. netdev_acquire();
  89. return 0;
  90. }
  91. int sys_net_release(void)
  92. {
  93. if (owns_nic())
  94. netdev_release();
  95. return 0;
  96. }
  97. int sys_net_mac(unsigned char mac[6])
  98. {
  99. const struct netdev *nd = netdev_active();
  100. if (!nd || !nd->get_mac)
  101. return -1;
  102. nd->get_mac(mac);
  103. return 0;
  104. }
  105. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  106. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  107. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  108. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  109. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  110. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  111. int sys_net_tx(const unsigned char *buf, unsigned int len)
  112. {
  113. const struct netdev *nd = netdev_active();
  114. if (!nd || !owns_nic())
  115. return -1;
  116. return nd->tx(buf, len);
  117. }
  118. int sys_net_rx(unsigned char *buf, unsigned int max)
  119. {
  120. const struct netdev *nd = netdev_active();
  121. if (!nd || !owns_nic())
  122. return -1;
  123. return nd->rx(buf, max);
  124. }
  125. void sys_net_decode(const unsigned char *buf, unsigned int len)
  126. {
  127. net_decode_frame(buf, len);
  128. }
  129. /* The reply buffer is always a full frame (the callers carry one). */
  130. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  131. {
  132. if (!owns_nic())
  133. return -1;
  134. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  135. }
  136. int sys_net_loopback(int on)
  137. {
  138. const struct netdev *nd = netdev_active();
  139. if (!nd || !nd->set_loopback)
  140. return -1;
  141. return nd->set_loopback(on);
  142. }
  143. int sys_net_regs(void)
  144. {
  145. const struct netdev *nd = netdev_active();
  146. if (!nd || !nd->dump_regs)
  147. return -1;
  148. nd->dump_regs();
  149. return 0;
  150. }
  151. int sys_net_reset(void)
  152. {
  153. const struct netdev *nd = netdev_active();
  154. if (!nd || !nd->reset)
  155. return -1;
  156. nd->reset();
  157. return 0;
  158. }
  159. int sys_dhcp_run(unsigned int timeout_ms)
  160. {
  161. int r;
  162. net_enter();
  163. r = dhcp_run(timeout_ms);
  164. net_exit();
  165. return r;
  166. }
  167. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  168. {
  169. return dhcp_last_info(server, lease);
  170. }
  171. int sys_dns_resolve(const char *name, unsigned char ip[4])
  172. {
  173. int r;
  174. if (!owns_nic())
  175. return -1;
  176. net_enter();
  177. r = dns_resolve(name, ip);
  178. net_exit();
  179. return r;
  180. }
  181. /* ---- TCP client ---- */
  182. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  183. unsigned int port)
  184. {
  185. int r;
  186. if (!owns_nic())
  187. return -3;
  188. net_enter();
  189. r = tcp_connect(mac, ip, port, &tcp);
  190. net_exit();
  191. return r;
  192. }
  193. int sys_tcp_peer_mss(void)
  194. {
  195. return (int)tcp.peer_mss;
  196. }
  197. int sys_tcp_send(const void *buf, unsigned int len)
  198. {
  199. int r;
  200. if (!owns_nic())
  201. return -3;
  202. net_enter();
  203. r = tcp_send(&tcp, buf, len);
  204. net_exit();
  205. return r;
  206. }
  207. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  208. {
  209. int r;
  210. if (!owns_nic())
  211. return -2;
  212. net_enter();
  213. r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
  214. net_exit();
  215. return r;
  216. }
  217. void sys_tcp_close(void)
  218. {
  219. if (!owns_nic())
  220. return;
  221. net_enter();
  222. tcp_close(&tcp);
  223. net_exit();
  224. }
  225. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  226. {
  227. net_next_hop(dst, hop);
  228. return 0;
  229. }
  230. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  231. {
  232. int r;
  233. if (!owns_nic())
  234. return -2;
  235. net_enter();
  236. r = net_arp_resolve(ip, mac);
  237. net_exit();
  238. return r;
  239. }
  240. int sys_net_configured(void)
  241. {
  242. return net_is_configured();
  243. }
  244. int sys_netlog(const char *text)
  245. {
  246. static const unsigned char bcast_mac[6] = {
  247. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  248. };
  249. unsigned char dst[4];
  250. unsigned int len = 0;
  251. while (text[len] && len < NET_UDP_MAX)
  252. len++;
  253. netlog_get_host(dst);
  254. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  255. }
  256. int sys_netlog_conf(int enabled, const unsigned char *ip)
  257. {
  258. if (enabled >= 0)
  259. netlog_set_enabled(enabled);
  260. if (ip)
  261. netlog_set_host(ip);
  262. return 0;
  263. }