sys_net.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. /* print_info reads the PHY via MDIC and can wait on the PHY/ME
  89. * semaphore; the syscall stub entered with interrupts off. */
  90. net_enter();
  91. nd->print_info();
  92. net_exit();
  93. return 0;
  94. }
  95. /* The owner must be the caller: the syscall runs in its process context. */
  96. static int owns_nic(void)
  97. {
  98. return netdev_owner() == caller_pid();
  99. }
  100. int sys_net_acquire(void)
  101. {
  102. if (owns_nic())
  103. return 0;
  104. if (netdev_in_use())
  105. return -1;
  106. netdev_acquire();
  107. return 0;
  108. }
  109. int sys_net_release(void)
  110. {
  111. if (owns_nic())
  112. netdev_release();
  113. return 0;
  114. }
  115. int sys_net_mac(unsigned char mac[6])
  116. {
  117. const struct netdev *nd = netdev_active();
  118. if (!nd || !nd->get_mac)
  119. return -1;
  120. nd->get_mac(mac);
  121. return 0;
  122. }
  123. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  124. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  125. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  126. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  127. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  128. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  129. int sys_net_tx(const unsigned char *buf, unsigned int len)
  130. {
  131. const struct netdev *nd = netdev_active();
  132. if (!nd || !owns_nic())
  133. return -1;
  134. return nd->tx(buf, len);
  135. }
  136. int sys_net_rx(unsigned char *buf, unsigned int max)
  137. {
  138. const struct netdev *nd = netdev_active();
  139. if (!nd || !owns_nic())
  140. return -1;
  141. return nd->rx(buf, max);
  142. }
  143. void sys_net_decode(const unsigned char *buf, unsigned int len)
  144. {
  145. net_decode_frame(buf, len);
  146. }
  147. /* The reply buffer is always a full frame (the callers carry one). */
  148. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  149. {
  150. if (!owns_nic())
  151. return -1;
  152. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  153. }
  154. int sys_net_loopback(int on)
  155. {
  156. const struct netdev *nd = netdev_active();
  157. if (!nd || !nd->set_loopback)
  158. return -1;
  159. return nd->set_loopback(on);
  160. }
  161. int sys_net_regs(void)
  162. {
  163. const struct netdev *nd = netdev_active();
  164. if (!nd || !nd->dump_regs)
  165. return -1;
  166. net_enter();
  167. nd->dump_regs();
  168. net_exit();
  169. return 0;
  170. }
  171. int sys_net_reset(void)
  172. {
  173. const struct netdev *nd = netdev_active();
  174. if (!nd || !nd->reset)
  175. return -1;
  176. /* Own the receiver for the whole reset, so the netlog kworker cannot
  177. * submit frames while the MAC and rings are being reinitialised. */
  178. netdev_acquire();
  179. /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
  180. * entered with interrupts off. */
  181. net_enter();
  182. nd->reset();
  183. net_exit();
  184. netdev_release();
  185. return 0;
  186. }
  187. /* net100 fallback: force 100M or restore auto. The driver drops and
  188. * renegotiates the link, which can take a few seconds; own the receiver so
  189. * the background watcher cannot poll frames while the link is down. */
  190. int sys_net_speed(unsigned int mbps)
  191. {
  192. const struct netdev *nd = netdev_active();
  193. int r;
  194. if (!nd || !nd->set_speed)
  195. return -1;
  196. if (sys_net_acquire() != 0)
  197. return -2;
  198. net_enter();
  199. r = nd->set_speed(mbps);
  200. net_exit();
  201. sys_net_release();
  202. return r;
  203. }
  204. int sys_net_mac_set(const unsigned char mac[6])
  205. {
  206. const struct netdev *nd = netdev_active();
  207. if (!nd || !nd->set_mac)
  208. return -1;
  209. if (sys_net_acquire() != 0)
  210. return -2;
  211. nd->set_mac(mac);
  212. sys_net_release();
  213. return 0;
  214. }
  215. int sys_dhcp_run(unsigned int timeout_ms)
  216. {
  217. int r;
  218. net_enter();
  219. r = dhcp_run(timeout_ms);
  220. net_exit();
  221. return r;
  222. }
  223. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  224. {
  225. return dhcp_last_info(server, lease);
  226. }
  227. int sys_dns_resolve(const char *name, unsigned char ip[4])
  228. {
  229. int r;
  230. if (!owns_nic())
  231. return -1;
  232. net_enter();
  233. r = dns_resolve(name, ip);
  234. net_exit();
  235. return r;
  236. }
  237. /* ---- TCP client ---- */
  238. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  239. unsigned int port)
  240. {
  241. int r;
  242. if (!owns_nic())
  243. return -3;
  244. net_enter();
  245. r = tcp_connect(mac, ip, port, &tcp);
  246. net_exit();
  247. return r;
  248. }
  249. int sys_tcp_peer_mss(void)
  250. {
  251. return (int)tcp.peer_mss;
  252. }
  253. int sys_tcp_send(const void *buf, unsigned int len)
  254. {
  255. int r;
  256. if (!owns_nic())
  257. return -3;
  258. net_enter();
  259. r = tcp_send(&tcp, buf, len);
  260. net_exit();
  261. return r;
  262. }
  263. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  264. {
  265. int r;
  266. if (!owns_nic())
  267. return -2;
  268. net_enter();
  269. r = tcp_recv(&tcp, buf, max, get_ticks_ms() + timeout_ms);
  270. net_exit();
  271. return r;
  272. }
  273. void sys_tcp_close(void)
  274. {
  275. if (!owns_nic())
  276. return;
  277. net_enter();
  278. tcp_close(&tcp);
  279. net_exit();
  280. }
  281. /* ---- TCP server ---- */
  282. int sys_tcp_listen(unsigned int port)
  283. {
  284. if (!owns_nic() || port == 0 || port > 65535)
  285. return -1;
  286. net_enter();
  287. tcp_srv_init(port);
  288. net_exit();
  289. srv_conn = 0;
  290. return 0;
  291. }
  292. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  293. * netkexec header back to the kworker (the server exits, the watcher picks
  294. * the transfer up without waiting for the host's retry). */
  295. int sys_tcp_accept(void)
  296. {
  297. const struct netdev *nd = netdev_active();
  298. unsigned char *rx;
  299. struct tcp_conn *c;
  300. int i, len;
  301. if (!nd || !owns_nic())
  302. return -2;
  303. rx = netdev_rx_scratch();
  304. net_enter();
  305. for (i = 0; i < 16; i++) {
  306. len = nd->rx(rx, NETDEV_FRAME_MAX);
  307. if (len < 0)
  308. break;
  309. if (tcp_srv_input(rx, (unsigned int)len))
  310. continue;
  311. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  312. net_exit();
  313. return -3;
  314. }
  315. {
  316. int rlen = net_build_reply(rx, (unsigned int)len,
  317. srv_reply,
  318. sizeof(srv_reply));
  319. if (rlen > 0)
  320. nd->tx(srv_reply, (unsigned int)rlen);
  321. }
  322. }
  323. c = tcp_srv_accept();
  324. net_exit();
  325. if (!c)
  326. return 0;
  327. srv_conn = c;
  328. return 1;
  329. }
  330. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  331. {
  332. int r;
  333. if (!owns_nic() || !srv_conn)
  334. return -2;
  335. net_enter();
  336. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  337. net_exit();
  338. return r;
  339. }
  340. int sys_tcp_srv_send(const void *buf, unsigned int len)
  341. {
  342. int r;
  343. if (!owns_nic() || !srv_conn)
  344. return -3;
  345. net_enter();
  346. r = tcp_srv_send(srv_conn, buf, len);
  347. net_exit();
  348. return r;
  349. }
  350. void sys_tcp_srv_close(void)
  351. {
  352. if (owns_nic() && srv_conn) {
  353. net_enter();
  354. tcp_srv_close(srv_conn);
  355. net_exit();
  356. }
  357. srv_conn = 0;
  358. }
  359. /* Peer of the accepted connection (for the server's connection log). */
  360. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  361. {
  362. unsigned int i;
  363. if (!srv_conn)
  364. return -1;
  365. if (ip)
  366. for (i = 0; i < 4; i++)
  367. ip[i] = srv_conn->dst_ip[i];
  368. if (port)
  369. *port = srv_conn->dport;
  370. return 0;
  371. }
  372. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  373. {
  374. net_next_hop(dst, hop);
  375. return 0;
  376. }
  377. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  378. {
  379. int r;
  380. if (!owns_nic())
  381. return -2;
  382. net_enter();
  383. r = net_arp_resolve(ip, mac);
  384. net_exit();
  385. return r;
  386. }
  387. int sys_net_configured(void)
  388. {
  389. return net_is_configured();
  390. }
  391. int sys_netlog(const char *text)
  392. {
  393. static const unsigned char bcast_mac[6] = {
  394. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  395. };
  396. unsigned char dst[4];
  397. unsigned int len = 0;
  398. while (text[len] && len < NET_UDP_MAX)
  399. len++;
  400. netlog_get_host(dst);
  401. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  402. }
  403. int sys_netlog_conf(int enabled, const unsigned char *ip)
  404. {
  405. if (enabled >= 0)
  406. netlog_set_enabled(enabled);
  407. if (ip)
  408. netlog_set_host(ip);
  409. return 0;
  410. }