sys_net.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. /* Own the receiver for the whole reset, so the netlog kworker cannot
  171. * submit frames while the MAC and rings are being reinitialised. */
  172. netdev_acquire();
  173. nd->reset();
  174. netdev_release();
  175. return 0;
  176. }
  177. int sys_dhcp_run(unsigned int timeout_ms)
  178. {
  179. int r;
  180. net_enter();
  181. r = dhcp_run(timeout_ms);
  182. net_exit();
  183. return r;
  184. }
  185. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  186. {
  187. return dhcp_last_info(server, lease);
  188. }
  189. int sys_dns_resolve(const char *name, unsigned char ip[4])
  190. {
  191. int r;
  192. if (!owns_nic())
  193. return -1;
  194. net_enter();
  195. r = dns_resolve(name, ip);
  196. net_exit();
  197. return r;
  198. }
  199. /* ---- TCP client ---- */
  200. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  201. unsigned int port)
  202. {
  203. int r;
  204. if (!owns_nic())
  205. return -3;
  206. net_enter();
  207. r = tcp_connect(mac, ip, port, &tcp);
  208. net_exit();
  209. return r;
  210. }
  211. int sys_tcp_peer_mss(void)
  212. {
  213. return (int)tcp.peer_mss;
  214. }
  215. int sys_tcp_send(const void *buf, unsigned int len)
  216. {
  217. int r;
  218. if (!owns_nic())
  219. return -3;
  220. net_enter();
  221. r = tcp_send(&tcp, buf, len);
  222. net_exit();
  223. return r;
  224. }
  225. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  226. {
  227. int r;
  228. if (!owns_nic())
  229. return -2;
  230. net_enter();
  231. r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
  232. net_exit();
  233. return r;
  234. }
  235. void sys_tcp_close(void)
  236. {
  237. if (!owns_nic())
  238. return;
  239. net_enter();
  240. tcp_close(&tcp);
  241. net_exit();
  242. }
  243. /* ---- TCP server ---- */
  244. int sys_tcp_listen(unsigned int port)
  245. {
  246. if (!owns_nic() || port == 0 || port > 65535)
  247. return -1;
  248. net_enter();
  249. tcp_srv_init(port);
  250. net_exit();
  251. srv_conn = 0;
  252. return 0;
  253. }
  254. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  255. * netkexec header back to the kworker (the server exits, the watcher picks
  256. * the transfer up without waiting for the host's retry). */
  257. int sys_tcp_accept(void)
  258. {
  259. const struct netdev *nd = netdev_active();
  260. unsigned char *rx;
  261. struct tcp_conn *c;
  262. int i, len;
  263. if (!nd || !owns_nic())
  264. return -2;
  265. rx = netdev_rx_scratch();
  266. net_enter();
  267. for (i = 0; i < 16; i++) {
  268. len = nd->rx(rx, NETDEV_FRAME_MAX);
  269. if (len < 0)
  270. break;
  271. if (tcp_srv_input(rx, (unsigned int)len))
  272. continue;
  273. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  274. net_exit();
  275. return -3;
  276. }
  277. {
  278. int rlen = net_build_reply(rx, (unsigned int)len,
  279. srv_reply,
  280. sizeof(srv_reply));
  281. if (rlen > 0)
  282. nd->tx(srv_reply, (unsigned int)rlen);
  283. }
  284. }
  285. c = tcp_srv_accept();
  286. net_exit();
  287. if (!c)
  288. return 0;
  289. srv_conn = c;
  290. return 1;
  291. }
  292. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  293. {
  294. int r;
  295. if (!owns_nic() || !srv_conn)
  296. return -2;
  297. net_enter();
  298. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  299. net_exit();
  300. return r;
  301. }
  302. int sys_tcp_srv_send(const void *buf, unsigned int len)
  303. {
  304. int r;
  305. if (!owns_nic() || !srv_conn)
  306. return -3;
  307. net_enter();
  308. r = tcp_srv_send(srv_conn, buf, len);
  309. net_exit();
  310. return r;
  311. }
  312. void sys_tcp_srv_close(void)
  313. {
  314. if (owns_nic() && srv_conn) {
  315. net_enter();
  316. tcp_srv_close(srv_conn);
  317. net_exit();
  318. }
  319. srv_conn = 0;
  320. }
  321. /* Peer of the accepted connection (for the server's connection log). */
  322. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  323. {
  324. unsigned int i;
  325. if (!srv_conn)
  326. return -1;
  327. if (ip)
  328. for (i = 0; i < 4; i++)
  329. ip[i] = srv_conn->dst_ip[i];
  330. if (port)
  331. *port = srv_conn->dport;
  332. return 0;
  333. }
  334. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  335. {
  336. net_next_hop(dst, hop);
  337. return 0;
  338. }
  339. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  340. {
  341. int r;
  342. if (!owns_nic())
  343. return -2;
  344. net_enter();
  345. r = net_arp_resolve(ip, mac);
  346. net_exit();
  347. return r;
  348. }
  349. int sys_net_configured(void)
  350. {
  351. return net_is_configured();
  352. }
  353. int sys_netlog(const char *text)
  354. {
  355. static const unsigned char bcast_mac[6] = {
  356. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  357. };
  358. unsigned char dst[4];
  359. unsigned int len = 0;
  360. while (text[len] && len < NET_UDP_MAX)
  361. len++;
  362. netlog_get_host(dst);
  363. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  364. }
  365. int sys_netlog_conf(int enabled, const unsigned char *ip)
  366. {
  367. if (enabled >= 0)
  368. netlog_set_enabled(enabled);
  369. if (ip)
  370. netlog_set_host(ip);
  371. return 0;
  372. }