sys_net.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.
  7. *
  8. * Packet-level commands take an exclusive raw lease (netdev_acquire); the
  9. * background kexec watcher pauses while one owns the receiver, and the reaper
  10. * releases it if that process dies. TCP clients do NOT take that lease: each
  11. * process may hold one connection in tcp.c, and every stack syscall drives
  12. * the shared net_pump() so several apps can transfer concurrently. */
  13. #include "syscall.h"
  14. #include "dhcp.h"
  15. #include "dns.h"
  16. #include "io.h"
  17. #include "net.h"
  18. #include "netcore.h"
  19. #include "netdev.h"
  20. #include "netkexec.h"
  21. #include "netlog.h"
  22. #include "ntp.h"
  23. #include "process.h"
  24. #include "tcp.h"
  25. #include "timer.h"
  26. /* Accepted server connection (NULL until SYS_TCP_ACCEPT returns one). */
  27. static struct tcp_conn *srv_conn;
  28. /* Reply buffer for ARP/ICMP answered while listening (the rx scratch is
  29. * occupied by the received frame). */
  30. static unsigned char srv_reply[NETDEV_FRAME_MAX];
  31. /* Long waits inside the stack (ARP/DNS/DHCP/TCP) spin on get_ticks_ms(), so
  32. * they need interrupts: the syscall stub entered with them off, and a frozen
  33. * tick counter would never satisfy a deadline. The netdev ownership already
  34. * serializes net users, so running with IRQs on (and being preempted) is
  35. * safe. */
  36. static void net_enter(void)
  37. {
  38. /* ARP/DNS/DHCP/TCP waits spin on the PIT and can take seconds. Give
  39. * the other CPU's syscalls (and the WM) the kernel while we wait; the
  40. * NIC ownership still serializes every stack user. */
  41. bkl_pause();
  42. __asm__ volatile ("sti");
  43. }
  44. static void net_exit(void)
  45. {
  46. __asm__ volatile ("cli");
  47. bkl_resume();
  48. }
  49. static unsigned int caller_pid(void)
  50. {
  51. return current_proc()->pid;
  52. }
  53. /* Called from the scheduler's reap loop for every dead process. */
  54. void sys_net_reap(unsigned int pid)
  55. {
  56. if (netdev_owner() == pid) {
  57. netdev_release_all();
  58. tcp_srv_reset();
  59. srv_conn = 0;
  60. }
  61. tcp_client_reap(pid);
  62. dns_reap(pid);
  63. net_stack_reap(pid);
  64. }
  65. int sys_net_info(struct sys_netinfo *out)
  66. {
  67. const struct netdev *nd = netdev_active();
  68. unsigned int i;
  69. for (i = 0; i < sizeof(*out); i++)
  70. ((unsigned char *)out)[i] = 0;
  71. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  72. netlog_get_host(out->mirror_host);
  73. if (!nd)
  74. return 0;
  75. out->present = 1;
  76. if (nd->get_mac)
  77. nd->get_mac(out->mac);
  78. net_get_ip(out->ip);
  79. net_get_mask(out->mask);
  80. net_get_gateway(out->gw);
  81. net_get_dns(out->dns);
  82. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  83. out->name[i] = nd->name[i];
  84. for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
  85. out->chip[i] = nd->chip[i];
  86. return 1;
  87. }
  88. int sys_net_print_info(void)
  89. {
  90. const struct netdev *nd = netdev_active();
  91. if (!nd || !nd->print_info || tcp_client_count() != 0)
  92. return -1;
  93. /* print_info reads the PHY via MDIC and can wait on the PHY/ME
  94. * semaphore; the syscall stub entered with interrupts off. */
  95. net_enter();
  96. nd->print_info();
  97. net_exit();
  98. return 0;
  99. }
  100. /* The owner must be the caller: the syscall runs in its process context. */
  101. static int owns_nic(void)
  102. {
  103. return netdev_owner() == caller_pid();
  104. }
  105. int sys_net_acquire(void)
  106. {
  107. if (owns_nic())
  108. return 0;
  109. /* Raw commands consume every frame, so they cannot coexist with a
  110. * shared TCP client (whose data the pump must dispatch). */
  111. if (netdev_in_use() || tcp_client_count() != 0)
  112. return -1;
  113. netdev_acquire();
  114. return 0;
  115. }
  116. int sys_net_release(void)
  117. {
  118. if (owns_nic())
  119. netdev_release();
  120. return 0;
  121. }
  122. int sys_net_mac(unsigned char mac[6])
  123. {
  124. const struct netdev *nd = netdev_active();
  125. if (!nd || !nd->get_mac)
  126. return -1;
  127. nd->get_mac(mac);
  128. return 0;
  129. }
  130. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  131. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  132. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  133. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  134. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  135. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  136. int sys_net_tx(const unsigned char *buf, unsigned int len)
  137. {
  138. const struct netdev *nd = netdev_active();
  139. if (!nd || !owns_nic())
  140. return -1;
  141. return nd->tx(buf, len);
  142. }
  143. int sys_net_rx(unsigned char *buf, unsigned int max)
  144. {
  145. const struct netdev *nd = netdev_active();
  146. if (!nd || !owns_nic())
  147. return -1;
  148. return nd->rx(buf, max);
  149. }
  150. void sys_net_decode(const unsigned char *buf, unsigned int len)
  151. {
  152. net_decode_frame(buf, len);
  153. }
  154. /* The reply buffer is always a full frame (the callers carry one). */
  155. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  156. {
  157. if (!owns_nic())
  158. return -1;
  159. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  160. }
  161. int sys_net_loopback(int on)
  162. {
  163. const struct netdev *nd = netdev_active();
  164. if (!nd || !nd->set_loopback)
  165. return -1;
  166. return nd->set_loopback(on);
  167. }
  168. int sys_net_regs(void)
  169. {
  170. const struct netdev *nd = netdev_active();
  171. if (!nd || !nd->dump_regs || tcp_client_count() != 0)
  172. return -1;
  173. net_enter();
  174. nd->dump_regs();
  175. net_exit();
  176. return 0;
  177. }
  178. int sys_net_reset(void)
  179. {
  180. const struct netdev *nd = netdev_active();
  181. if (!nd || !nd->reset)
  182. return -1;
  183. if (tcp_client_count() != 0 ||
  184. (netdev_in_use() && !owns_nic()))
  185. return -1;
  186. /* Own the receiver for the whole reset, so the netlog kworker cannot
  187. * submit frames while the MAC and rings are being reinitialised. */
  188. netdev_acquire();
  189. /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
  190. * entered with interrupts off. */
  191. net_enter();
  192. nd->reset();
  193. net_exit();
  194. netdev_release();
  195. return 0;
  196. }
  197. /* net100 fallback: force 100M or restore auto. The driver drops and
  198. * renegotiates the link, which can take a few seconds; own the receiver so
  199. * the background watcher cannot poll frames while the link is down. */
  200. int sys_net_speed(unsigned int mbps)
  201. {
  202. const struct netdev *nd = netdev_active();
  203. int r;
  204. if (!nd || !nd->set_speed)
  205. return -1;
  206. if (sys_net_acquire() != 0)
  207. return -2;
  208. net_enter();
  209. r = nd->set_speed(mbps);
  210. net_exit();
  211. sys_net_release();
  212. return r;
  213. }
  214. int sys_net_mac_set(const unsigned char mac[6])
  215. {
  216. const struct netdev *nd = netdev_active();
  217. if (!nd || !nd->set_mac)
  218. return -1;
  219. if (sys_net_acquire() != 0)
  220. return -2;
  221. nd->set_mac(mac);
  222. sys_net_release();
  223. return 0;
  224. }
  225. int sys_dhcp_run(unsigned int timeout_ms)
  226. {
  227. int r;
  228. if (netdev_in_use() || tcp_client_count() != 0)
  229. return -1;
  230. net_enter();
  231. r = dhcp_run(timeout_ms);
  232. net_exit();
  233. return r;
  234. }
  235. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  236. {
  237. return dhcp_last_info(server, lease);
  238. }
  239. int sys_dns_resolve(const char *name, unsigned char ip[4])
  240. {
  241. int r;
  242. /* A raw owner keeps its direct poll path; stack users share the pump.
  243. * Refuse only when another process owns the raw lease. */
  244. if (netdev_in_use() && !owns_nic())
  245. return -1;
  246. if (owns_nic()) {
  247. net_enter();
  248. r = dns_resolve(name, ip);
  249. net_exit();
  250. return r;
  251. }
  252. return dns_resolve(name, ip);
  253. }
  254. /* SNTP sync for ring-3 callers. The service owns the receiver for the
  255. * duration (unless the caller already does), so settime does not have to
  256. * take SYS_NET_ACQUIRE itself. A second network user gets SYS_NTP_EBUSY. */
  257. int sys_ntp_sync(const char *server, unsigned int timeout_ms,
  258. struct sys_datetime *out)
  259. {
  260. unsigned int pid = caller_pid();
  261. int acquired = 0;
  262. int r;
  263. if (!netdev_active())
  264. return SYS_NTP_ENONIC;
  265. if (netdev_in_use()) {
  266. if (netdev_owner() != pid)
  267. return SYS_NTP_EBUSY;
  268. } else if (tcp_client_count() != 0) {
  269. return SYS_NTP_EBUSY;
  270. } else {
  271. netdev_acquire();
  272. acquired = 1;
  273. }
  274. net_enter();
  275. r = ntp_sync(server, timeout_ms, out);
  276. net_exit();
  277. if (acquired)
  278. netdev_release();
  279. return r;
  280. }
  281. /* ---- TCP client ---- */
  282. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  283. unsigned int port)
  284. {
  285. return tcp_client_connect(caller_pid(), mac, ip, port);
  286. }
  287. int sys_tcp_peer_mss(void)
  288. {
  289. return tcp_client_peer_mss(caller_pid());
  290. }
  291. int sys_tcp_send(const void *buf, unsigned int len)
  292. {
  293. return tcp_client_send(caller_pid(), buf, len);
  294. }
  295. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  296. {
  297. return tcp_client_recv(caller_pid(), buf, max,
  298. get_ticks_ms() + timeout_ms);
  299. }
  300. void sys_tcp_close(void)
  301. {
  302. tcp_client_close(caller_pid());
  303. }
  304. /* ---- TCP server ---- */
  305. int sys_tcp_listen(unsigned int port)
  306. {
  307. if (!owns_nic() || port == 0 || port > 65535)
  308. return -1;
  309. net_enter();
  310. tcp_srv_init(port);
  311. net_exit();
  312. srv_conn = 0;
  313. return 0;
  314. }
  315. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  316. * netkexec header back to the kworker (the server exits, the watcher picks
  317. * the transfer up without waiting for the host's retry). */
  318. int sys_tcp_accept(void)
  319. {
  320. const struct netdev *nd = netdev_active();
  321. unsigned char *rx;
  322. struct tcp_conn *c;
  323. int i, len;
  324. if (!nd || !owns_nic())
  325. return -2;
  326. rx = netdev_rx_scratch();
  327. net_enter();
  328. for (i = 0; i < 16; i++) {
  329. len = nd->rx(rx, NETDEV_FRAME_MAX);
  330. if (len < 0)
  331. break;
  332. if (tcp_srv_input(rx, (unsigned int)len))
  333. continue;
  334. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  335. net_exit();
  336. return -3;
  337. }
  338. {
  339. int rlen = net_build_reply(rx, (unsigned int)len,
  340. srv_reply,
  341. sizeof(srv_reply));
  342. if (rlen > 0)
  343. nd->tx(srv_reply, (unsigned int)rlen);
  344. }
  345. }
  346. c = tcp_srv_accept();
  347. net_exit();
  348. if (!c)
  349. return 0;
  350. srv_conn = c;
  351. return 1;
  352. }
  353. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  354. {
  355. int r;
  356. if (!owns_nic() || !srv_conn)
  357. return -2;
  358. net_enter();
  359. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  360. net_exit();
  361. return r;
  362. }
  363. int sys_tcp_srv_send(const void *buf, unsigned int len)
  364. {
  365. int r;
  366. if (!owns_nic() || !srv_conn)
  367. return -3;
  368. net_enter();
  369. r = tcp_srv_send(srv_conn, buf, len);
  370. net_exit();
  371. return r;
  372. }
  373. void sys_tcp_srv_close(void)
  374. {
  375. if (owns_nic() && srv_conn) {
  376. net_enter();
  377. tcp_srv_close(srv_conn);
  378. net_exit();
  379. }
  380. srv_conn = 0;
  381. }
  382. /* Peer of the accepted connection (for the server's connection log). */
  383. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  384. {
  385. unsigned int i;
  386. if (!srv_conn)
  387. return -1;
  388. if (ip)
  389. for (i = 0; i < 4; i++)
  390. ip[i] = srv_conn->dst_ip[i];
  391. if (port)
  392. *port = srv_conn->dport;
  393. return 0;
  394. }
  395. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  396. {
  397. net_next_hop(dst, hop);
  398. return 0;
  399. }
  400. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  401. {
  402. int r;
  403. /* Raw owners keep the direct poll path; stack users share the pump. */
  404. if (netdev_in_use() && !owns_nic())
  405. return -2;
  406. if (owns_nic()) {
  407. net_enter();
  408. r = net_arp_resolve(ip, mac);
  409. net_exit();
  410. return r;
  411. }
  412. return net_arp_resolve(ip, mac);
  413. }
  414. int sys_net_configured(void)
  415. {
  416. return net_is_configured();
  417. }
  418. int sys_netlog(const char *text)
  419. {
  420. static const unsigned char bcast_mac[6] = {
  421. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  422. };
  423. unsigned char dst[4];
  424. unsigned int len = 0;
  425. while (text[len] && len < NET_UDP_MAX)
  426. len++;
  427. netlog_get_host(dst);
  428. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  429. }
  430. int sys_netlog_conf(int enabled, const unsigned char *ip)
  431. {
  432. if (enabled >= 0)
  433. netlog_set_enabled(enabled);
  434. if (ip)
  435. netlog_set_host(ip);
  436. return 0;
  437. }