sys_net.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "netkexec.h"
  17. #include "netlog.h"
  18. #include "process.h"
  19. #include "tcp.h"
  20. #include "timer.h"
  21. /* One TCP connection at a time (the stack is single-connection, and the
  22. * receiver is owned by the process using it). */
  23. static struct tcp_conn tcp;
  24. /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
  25. static struct tcp_conn *srv_conn;
  26. /* Reply buffer for ARP/ICMP answered while listening (the rx scratch is
  27. * occupied by the received frame). */
  28. static unsigned char srv_reply[NETDEV_FRAME_MAX];
  29. /* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
  30. * they need interrupts: the syscall stub entered with them off, and a frozen
  31. * tick counter would never satisfy a deadline. The netdev ownership already
  32. * serializes net users, so running with IRQs on (and being preempted) is
  33. * safe. */
  34. static void net_enter(void)
  35. {
  36. /* ARP/DNS/DHCP/TCP waits spin on the PIT and can take seconds. Give
  37. * the other CPU's syscalls (and the WM) the kernel while we wait; the
  38. * NIC ownership still serializes every stack user. */
  39. bkl_pause();
  40. __asm__ volatile ("sti");
  41. }
  42. static void net_exit(void)
  43. {
  44. __asm__ volatile ("cli");
  45. bkl_resume();
  46. }
  47. static unsigned int caller_pid(void)
  48. {
  49. return current_proc()->pid;
  50. }
  51. /* Called from the scheduler's reap loop for every dead process. */
  52. void sys_net_reap(unsigned int pid)
  53. {
  54. if (netdev_owner() == pid) {
  55. netdev_release_all();
  56. tcp_srv_reset();
  57. srv_conn = 0;
  58. }
  59. }
  60. int sys_net_info(struct sys_netinfo *out)
  61. {
  62. const struct netdev *nd = netdev_active();
  63. unsigned int i;
  64. for (i = 0; i < sizeof(*out); i++)
  65. ((unsigned char *)out)[i] = 0;
  66. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  67. netlog_get_host(out->mirror_host);
  68. if (!nd)
  69. return 0;
  70. out->present = 1;
  71. if (nd->get_mac)
  72. nd->get_mac(out->mac);
  73. net_get_ip(out->ip);
  74. net_get_mask(out->mask);
  75. net_get_gateway(out->gw);
  76. net_get_dns(out->dns);
  77. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  78. out->name[i] = nd->name[i];
  79. for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
  80. out->chip[i] = nd->chip[i];
  81. return 1;
  82. }
  83. int sys_net_print_info(void)
  84. {
  85. const struct netdev *nd = netdev_active();
  86. if (!nd || !nd->print_info)
  87. return -1;
  88. nd->print_info();
  89. return 0;
  90. }
  91. /* The owner must be the caller: the syscall runs in its process context. */
  92. static int owns_nic(void)
  93. {
  94. return netdev_owner() == caller_pid();
  95. }
  96. int sys_net_acquire(void)
  97. {
  98. if (owns_nic())
  99. return 0;
  100. if (netdev_in_use())
  101. return -1;
  102. netdev_acquire();
  103. return 0;
  104. }
  105. int sys_net_release(void)
  106. {
  107. if (owns_nic())
  108. netdev_release();
  109. return 0;
  110. }
  111. int sys_net_mac(unsigned char mac[6])
  112. {
  113. const struct netdev *nd = netdev_active();
  114. if (!nd || !nd->get_mac)
  115. return -1;
  116. nd->get_mac(mac);
  117. return 0;
  118. }
  119. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  120. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  121. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  122. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  123. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  124. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  125. int sys_net_tx(const unsigned char *buf, unsigned int len)
  126. {
  127. const struct netdev *nd = netdev_active();
  128. if (!nd || !owns_nic())
  129. return -1;
  130. return nd->tx(buf, len);
  131. }
  132. int sys_net_rx(unsigned char *buf, unsigned int max)
  133. {
  134. const struct netdev *nd = netdev_active();
  135. if (!nd || !owns_nic())
  136. return -1;
  137. return nd->rx(buf, max);
  138. }
  139. void sys_net_decode(const unsigned char *buf, unsigned int len)
  140. {
  141. net_decode_frame(buf, len);
  142. }
  143. /* The reply buffer is always a full frame (the callers carry one). */
  144. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  145. {
  146. if (!owns_nic())
  147. return -1;
  148. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  149. }
  150. int sys_net_loopback(int on)
  151. {
  152. const struct netdev *nd = netdev_active();
  153. if (!nd || !nd->set_loopback)
  154. return -1;
  155. return nd->set_loopback(on);
  156. }
  157. int sys_net_regs(void)
  158. {
  159. const struct netdev *nd = netdev_active();
  160. if (!nd || !nd->dump_regs)
  161. return -1;
  162. nd->dump_regs();
  163. return 0;
  164. }
  165. int sys_net_reset(void)
  166. {
  167. const struct netdev *nd = netdev_active();
  168. if (!nd || !nd->reset)
  169. return -1;
  170. nd->reset();
  171. return 0;
  172. }
  173. int sys_dhcp_run(unsigned int timeout_ms)
  174. {
  175. int r;
  176. net_enter();
  177. r = dhcp_run(timeout_ms);
  178. net_exit();
  179. return r;
  180. }
  181. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  182. {
  183. return dhcp_last_info(server, lease);
  184. }
  185. int sys_dns_resolve(const char *name, unsigned char ip[4])
  186. {
  187. int r;
  188. if (!owns_nic())
  189. return -1;
  190. net_enter();
  191. r = dns_resolve(name, ip);
  192. net_exit();
  193. return r;
  194. }
  195. /* ---- TCP client ---- */
  196. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  197. unsigned int port)
  198. {
  199. int r;
  200. if (!owns_nic())
  201. return -3;
  202. net_enter();
  203. r = tcp_connect(mac, ip, port, &tcp);
  204. net_exit();
  205. return r;
  206. }
  207. int sys_tcp_peer_mss(void)
  208. {
  209. return (int)tcp.peer_mss;
  210. }
  211. int sys_tcp_send(const void *buf, unsigned int len)
  212. {
  213. int r;
  214. if (!owns_nic())
  215. return -3;
  216. net_enter();
  217. r = tcp_send(&tcp, buf, len);
  218. net_exit();
  219. return r;
  220. }
  221. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  222. {
  223. int r;
  224. if (!owns_nic())
  225. return -2;
  226. net_enter();
  227. r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
  228. net_exit();
  229. return r;
  230. }
  231. void sys_tcp_close(void)
  232. {
  233. if (!owns_nic())
  234. return;
  235. net_enter();
  236. tcp_close(&tcp);
  237. net_exit();
  238. }
  239. /* ---- TCP server ---- */
  240. int sys_tcp_listen(unsigned int port)
  241. {
  242. if (!owns_nic() || port == 0 || port > 65535)
  243. return -1;
  244. net_enter();
  245. tcp_srv_init(port);
  246. net_exit();
  247. srv_conn = 0;
  248. return 0;
  249. }
  250. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  251. * netkexec header back to the kworker (the server exits, the watcher picks
  252. * the transfer up without waiting for the host's retry). */
  253. int sys_tcp_accept(void)
  254. {
  255. const struct netdev *nd = netdev_active();
  256. unsigned char *rx;
  257. struct tcp_conn *c;
  258. int i, len;
  259. if (!nd || !owns_nic())
  260. return -2;
  261. rx = netdev_rx_scratch();
  262. net_enter();
  263. for (i = 0; i < 16; i++) {
  264. len = nd->rx(rx, NETDEV_FRAME_MAX);
  265. if (len < 0)
  266. break;
  267. if (tcp_srv_input(rx, (unsigned int)len))
  268. continue;
  269. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  270. net_exit();
  271. return -3;
  272. }
  273. {
  274. int rlen = net_build_reply(rx, (unsigned int)len,
  275. srv_reply,
  276. sizeof(srv_reply));
  277. if (rlen > 0)
  278. nd->tx(srv_reply, (unsigned int)rlen);
  279. }
  280. }
  281. c = tcp_srv_accept();
  282. net_exit();
  283. if (!c)
  284. return 0;
  285. srv_conn = c;
  286. return 1;
  287. }
  288. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  289. {
  290. int r;
  291. if (!owns_nic() || !srv_conn)
  292. return -2;
  293. net_enter();
  294. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  295. net_exit();
  296. return r;
  297. }
  298. int sys_tcp_srv_send(const void *buf, unsigned int len)
  299. {
  300. int r;
  301. if (!owns_nic() || !srv_conn)
  302. return -3;
  303. net_enter();
  304. r = tcp_srv_send(srv_conn, buf, len);
  305. net_exit();
  306. return r;
  307. }
  308. void sys_tcp_srv_close(void)
  309. {
  310. if (owns_nic() && srv_conn) {
  311. net_enter();
  312. tcp_srv_close(srv_conn);
  313. net_exit();
  314. }
  315. srv_conn = 0;
  316. }
  317. /* Peer of the accepted connection (for the server's connection log). */
  318. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  319. {
  320. unsigned int i;
  321. if (!srv_conn)
  322. return -1;
  323. if (ip)
  324. for (i = 0; i < 4; i++)
  325. ip[i] = srv_conn->dst_ip[i];
  326. if (port)
  327. *port = srv_conn->dport;
  328. return 0;
  329. }
  330. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  331. {
  332. net_next_hop(dst, hop);
  333. return 0;
  334. }
  335. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  336. {
  337. int r;
  338. if (!owns_nic())
  339. return -2;
  340. net_enter();
  341. r = net_arp_resolve(ip, mac);
  342. net_exit();
  343. return r;
  344. }
  345. int sys_net_configured(void)
  346. {
  347. return net_is_configured();
  348. }
  349. int sys_netlog(const char *text)
  350. {
  351. static const unsigned char bcast_mac[6] = {
  352. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  353. };
  354. unsigned char dst[4];
  355. unsigned int len = 0;
  356. while (text[len] && len < NET_UDP_MAX)
  357. len++;
  358. netlog_get_host(dst);
  359. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  360. }
  361. int sys_netlog_conf(int enabled, const unsigned char *ip)
  362. {
  363. if (enabled >= 0)
  364. netlog_set_enabled(enabled);
  365. if (ip)
  366. netlog_set_host(ip);
  367. return 0;
  368. }