sys_net.c 11 KB

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