sys_net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. static int nd_name_eq(const char *a, const char *b)
  54. {
  55. while (*a && *b) {
  56. if (*a != *b)
  57. return 0;
  58. a++;
  59. b++;
  60. }
  61. return *a == *b;
  62. }
  63. static void nd_copy_name(char *dst, unsigned int max, const char *src)
  64. {
  65. unsigned int i;
  66. for (i = 0; src && src[i] && i + 1 < max; i++)
  67. dst[i] = src[i];
  68. dst[i] = '\0';
  69. }
  70. static unsigned int nd_kind(const struct netdev *nd)
  71. {
  72. if (nd_name_eq(nd->name, "wifi"))
  73. return SYS_NETDEV_WIFI;
  74. if (nd_name_eq(nd->name, "rndis"))
  75. return SYS_NETDEV_USB;
  76. return SYS_NETDEV_WIRED;
  77. }
  78. /* Called from the scheduler's reap loop for every dead process. */
  79. void sys_net_reap(unsigned int pid)
  80. {
  81. if (netdev_owner() == pid) {
  82. netdev_release_all();
  83. tcp_srv_reset();
  84. srv_conn = 0;
  85. }
  86. tcp_client_reap(pid);
  87. dns_reap(pid);
  88. net_stack_reap(pid);
  89. }
  90. int sys_net_info(struct sys_netinfo *out)
  91. {
  92. const struct netdev *nd;
  93. unsigned int i;
  94. for (i = 0; i < sizeof(*out); i++)
  95. ((unsigned char *)out)[i] = 0;
  96. out->mirror_on = netlog_get_enabled() ? 1 : 0;
  97. netlog_get_host(out->mirror_host);
  98. /* The first SYS_NET_INFO may be the first NIC probe (the WM's top-bar
  99. * poll runs before the boot DHCP on some boots). Driver init waits on
  100. * the PHY/USB with tick-based delays, so run it with interrupts on and
  101. * the BKL released. Once probed, this is just a cached lookup. */
  102. if (!netdev_probed()) {
  103. net_enter();
  104. nd = netdev_active();
  105. net_exit();
  106. } else {
  107. nd = netdev_active();
  108. }
  109. if (!nd)
  110. return 0;
  111. out->present = 1;
  112. if (nd->get_mac)
  113. nd->get_mac(out->mac);
  114. net_get_ip(out->ip);
  115. net_get_mask(out->mask);
  116. net_get_gateway(out->gw);
  117. net_get_dns(out->dns);
  118. for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
  119. out->name[i] = nd->name[i];
  120. {
  121. const char *chip = netdev_chip(nd);
  122. for (i = 0; chip[i] && i + 1 < sizeof(out->chip); i++)
  123. out->chip[i] = chip[i];
  124. }
  125. out->link = (unsigned int)netdev_link(nd);
  126. return 1;
  127. }
  128. /* Probe every registered driver once and return the ones that are present.
  129. * The menu calls this when it opens, so a plain USB dongle or phone that was
  130. * not the boot-time active NIC still shows up. Driver init() can wait on the
  131. * PHY/USB, so it runs with interrupts on and the BKL released. */
  132. int sys_net_devlist(struct sys_netdev *out, unsigned int max)
  133. {
  134. const struct netdev *act;
  135. unsigned int i, n = 0;
  136. if (!out || !max)
  137. return 0;
  138. net_enter();
  139. act = netdev_active();
  140. for (i = 0; i < netdev_count() && n < max; i++) {
  141. const struct netdev *nd = netdev_at(i);
  142. unsigned int j;
  143. int present;
  144. if (!nd)
  145. continue;
  146. present = (nd == act);
  147. if (!present && nd->init)
  148. present = nd->init() == 0;
  149. if (!present)
  150. continue;
  151. for (j = 0; j < sizeof(out[n]); j++)
  152. ((unsigned char *)&out[n])[j] = 0;
  153. nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
  154. nd_copy_name(out[n].chip, sizeof(out[n].chip), netdev_chip(nd));
  155. out[n].kind = nd_kind(nd);
  156. out[n].active = (nd == act);
  157. out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
  158. (nd->link_up ? (nd->link_up() ? 1 : 0)
  159. : 1));
  160. n++;
  161. }
  162. net_exit();
  163. return (int)n;
  164. }
  165. int sys_net_select(const char *name)
  166. {
  167. int r;
  168. if (!name || !*name)
  169. return -1;
  170. net_enter();
  171. r = netdev_select(name);
  172. net_exit();
  173. return r;
  174. }
  175. int sys_net_print_info(void)
  176. {
  177. const struct netdev *nd = netdev_active();
  178. if (!nd || !nd->print_info || tcp_client_count() != 0)
  179. return -1;
  180. /* print_info reads the PHY via MDIC and can wait on the PHY/ME
  181. * semaphore; the syscall stub entered with interrupts off. */
  182. net_enter();
  183. nd->print_info();
  184. net_exit();
  185. return 0;
  186. }
  187. /* The owner must be the caller: the syscall runs in its process context. */
  188. static int owns_nic(void)
  189. {
  190. return netdev_owner() == caller_pid();
  191. }
  192. int sys_net_acquire(void)
  193. {
  194. if (owns_nic())
  195. return 0;
  196. /* Raw commands consume every frame, so they cannot coexist with a
  197. * shared TCP client (whose data the pump must dispatch). */
  198. if (netdev_in_use() || tcp_client_count() != 0)
  199. return -1;
  200. netdev_acquire();
  201. return 0;
  202. }
  203. int sys_net_release(void)
  204. {
  205. if (owns_nic())
  206. netdev_release();
  207. return 0;
  208. }
  209. int sys_net_mac(unsigned char mac[6])
  210. {
  211. const struct netdev *nd = netdev_active();
  212. if (!nd || !nd->get_mac)
  213. return -1;
  214. nd->get_mac(mac);
  215. return 0;
  216. }
  217. void sys_net_get_ip(unsigned char ip[4]) { net_get_ip(ip); }
  218. void sys_net_set_ip(const unsigned char ip[4]) { net_set_ip(ip); }
  219. void sys_net_get_mask(unsigned char m[4]) { net_get_mask(m); }
  220. void sys_net_get_gw(unsigned char gw[4]) { net_get_gateway(gw); }
  221. void sys_net_get_dns(unsigned char d[4]) { net_get_dns(d); }
  222. void sys_net_set_dns(const unsigned char d[4]) { net_set_dns(d); }
  223. int sys_net_tx(const unsigned char *buf, unsigned int len)
  224. {
  225. const struct netdev *nd = netdev_active();
  226. if (!nd || !owns_nic())
  227. return -1;
  228. return nd->tx(buf, len);
  229. }
  230. int sys_net_rx(unsigned char *buf, unsigned int max)
  231. {
  232. const struct netdev *nd = netdev_active();
  233. if (!nd || !owns_nic())
  234. return -1;
  235. return nd->rx(buf, max);
  236. }
  237. void sys_net_decode(const unsigned char *buf, unsigned int len)
  238. {
  239. net_decode_frame(buf, len);
  240. }
  241. /* The reply buffer is always a full frame (the callers carry one). */
  242. int sys_net_reply(const unsigned char *in, unsigned int len, unsigned char *out)
  243. {
  244. if (!owns_nic())
  245. return -1;
  246. return net_build_reply(in, len, out, NETDEV_FRAME_MAX);
  247. }
  248. int sys_net_loopback(int on)
  249. {
  250. const struct netdev *nd = netdev_active();
  251. if (!nd || !nd->set_loopback)
  252. return -1;
  253. return nd->set_loopback(on);
  254. }
  255. int sys_net_regs(void)
  256. {
  257. const struct netdev *nd = netdev_active();
  258. if (!nd || !nd->dump_regs || tcp_client_count() != 0)
  259. return -1;
  260. net_enter();
  261. nd->dump_regs();
  262. net_exit();
  263. return 0;
  264. }
  265. int sys_net_reset(void)
  266. {
  267. const struct netdev *nd = netdev_active();
  268. if (!nd || !nd->reset)
  269. return -1;
  270. if (tcp_client_count() != 0 ||
  271. (netdev_in_use() && !owns_nic()))
  272. return -1;
  273. /* Own the receiver for the whole reset, so the netlog kworker cannot
  274. * submit frames while the MAC and rings are being reinitialised. */
  275. netdev_acquire();
  276. /* The reset waits on ticks (reset timeouts, PHY autoneg); the syscall
  277. * entered with interrupts off. */
  278. net_enter();
  279. nd->reset();
  280. net_exit();
  281. netdev_release();
  282. return 0;
  283. }
  284. /* net100 fallback: force 100M or restore auto. The driver drops and
  285. * renegotiates the link, which can take a few seconds; own the receiver so
  286. * the background watcher cannot poll frames while the link is down. */
  287. int sys_net_speed(unsigned int mbps)
  288. {
  289. const struct netdev *nd = netdev_active();
  290. int r;
  291. if (!nd || !nd->set_speed)
  292. return -1;
  293. if (sys_net_acquire() != 0)
  294. return -2;
  295. net_enter();
  296. r = nd->set_speed(mbps);
  297. net_exit();
  298. sys_net_release();
  299. return r;
  300. }
  301. int sys_net_mac_set(const unsigned char mac[6])
  302. {
  303. const struct netdev *nd = netdev_active();
  304. if (!nd || !nd->set_mac)
  305. return -1;
  306. if (sys_net_acquire() != 0)
  307. return -2;
  308. nd->set_mac(mac);
  309. sys_net_release();
  310. return 0;
  311. }
  312. int sys_dhcp_run(unsigned int timeout_ms)
  313. {
  314. int r;
  315. if (netdev_in_use() || tcp_client_count() != 0)
  316. return -1;
  317. net_enter();
  318. r = dhcp_run(timeout_ms);
  319. net_exit();
  320. return r;
  321. }
  322. int sys_dhcp_info(unsigned char server[4], unsigned int *lease)
  323. {
  324. return dhcp_last_info(server, lease);
  325. }
  326. int sys_dns_resolve(const char *name, unsigned char ip[4])
  327. {
  328. int r;
  329. /* A raw owner keeps its direct poll path; stack users share the pump.
  330. * Refuse only when another process owns the raw lease. */
  331. if (netdev_in_use() && !owns_nic())
  332. return -1;
  333. if (owns_nic()) {
  334. net_enter();
  335. r = dns_resolve(name, ip);
  336. net_exit();
  337. return r;
  338. }
  339. return dns_resolve(name, ip);
  340. }
  341. /* SNTP sync for ring-3 callers. The service owns the receiver for the
  342. * duration (unless the caller already does), so settime does not have to
  343. * take SYS_NET_ACQUIRE itself. A second network user gets SYS_NTP_EBUSY. */
  344. int sys_ntp_sync(const char *server, unsigned int timeout_ms,
  345. struct sys_datetime *out)
  346. {
  347. unsigned int pid = caller_pid();
  348. int acquired = 0;
  349. int r;
  350. if (!netdev_active())
  351. return SYS_NTP_ENONIC;
  352. if (netdev_in_use()) {
  353. if (netdev_owner() != pid)
  354. return SYS_NTP_EBUSY;
  355. } else if (tcp_client_count() != 0) {
  356. return SYS_NTP_EBUSY;
  357. } else {
  358. netdev_acquire();
  359. acquired = 1;
  360. }
  361. net_enter();
  362. r = ntp_sync(server, timeout_ms, out);
  363. net_exit();
  364. if (acquired)
  365. netdev_release();
  366. return r;
  367. }
  368. /* ---- TCP client ---- */
  369. int sys_tcp_connect(const unsigned char mac[6], const unsigned char ip[4],
  370. unsigned int port)
  371. {
  372. return tcp_client_connect(caller_pid(), mac, ip, port);
  373. }
  374. int sys_tcp_peer_mss(void)
  375. {
  376. return tcp_client_peer_mss(caller_pid());
  377. }
  378. int sys_tcp_send(const void *buf, unsigned int len)
  379. {
  380. return tcp_client_send(caller_pid(), buf, len);
  381. }
  382. int sys_tcp_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  383. {
  384. return tcp_client_recv(caller_pid(), buf, max,
  385. get_ticks_ms() + timeout_ms);
  386. }
  387. void sys_tcp_close(void)
  388. {
  389. tcp_client_close(caller_pid());
  390. }
  391. /* ---- TCP server ---- */
  392. int sys_tcp_listen(unsigned int port)
  393. {
  394. if (!owns_nic() || port == 0 || port > 65535)
  395. return -1;
  396. net_enter();
  397. tcp_srv_init(port);
  398. net_exit();
  399. srv_conn = 0;
  400. return 0;
  401. }
  402. /* Poll the receiver for new connections. Also answers ARP/ICMP and hands a
  403. * netkexec header back to the kworker (the server exits, the watcher picks
  404. * the transfer up without waiting for the host's retry). */
  405. int sys_tcp_accept(void)
  406. {
  407. const struct netdev *nd = netdev_active();
  408. unsigned char *rx;
  409. struct tcp_conn *c;
  410. int i, len;
  411. if (!nd || !owns_nic())
  412. return -2;
  413. rx = netdev_rx_scratch();
  414. net_enter();
  415. for (i = 0; i < 16; i++) {
  416. len = nd->rx(rx, NETDEV_FRAME_MAX);
  417. if (len < 0)
  418. break;
  419. if (tcp_srv_input(rx, (unsigned int)len))
  420. continue;
  421. if (netkexec_capture_frame(rx, (unsigned int)len)) {
  422. net_exit();
  423. return -3;
  424. }
  425. {
  426. int rlen = net_build_reply(rx, (unsigned int)len,
  427. srv_reply,
  428. sizeof(srv_reply));
  429. if (rlen > 0)
  430. nd->tx(srv_reply, (unsigned int)rlen);
  431. }
  432. }
  433. c = tcp_srv_accept();
  434. net_exit();
  435. if (!c)
  436. return 0;
  437. srv_conn = c;
  438. return 1;
  439. }
  440. int sys_tcp_srv_recv(void *buf, unsigned int max, unsigned int timeout_ms)
  441. {
  442. int r;
  443. if (!owns_nic() || !srv_conn)
  444. return -2;
  445. net_enter();
  446. r = tcp_recv(srv_conn, buf, max, get_ticks_ms() + timeout_ms);
  447. net_exit();
  448. return r;
  449. }
  450. int sys_tcp_srv_send(const void *buf, unsigned int len)
  451. {
  452. int r;
  453. if (!owns_nic() || !srv_conn)
  454. return -3;
  455. net_enter();
  456. r = tcp_srv_send(srv_conn, buf, len);
  457. net_exit();
  458. return r;
  459. }
  460. void sys_tcp_srv_close(void)
  461. {
  462. if (owns_nic() && srv_conn) {
  463. net_enter();
  464. tcp_srv_close(srv_conn);
  465. net_exit();
  466. }
  467. srv_conn = 0;
  468. }
  469. /* Peer of the accepted connection (for the server's connection log). */
  470. int sys_tcp_peer(unsigned char ip[4], unsigned int *port)
  471. {
  472. unsigned int i;
  473. if (!srv_conn)
  474. return -1;
  475. if (ip)
  476. for (i = 0; i < 4; i++)
  477. ip[i] = srv_conn->dst_ip[i];
  478. if (port)
  479. *port = srv_conn->dport;
  480. return 0;
  481. }
  482. int sys_net_next_hop(const unsigned char dst[4], unsigned char hop[4])
  483. {
  484. net_next_hop(dst, hop);
  485. return 0;
  486. }
  487. int sys_net_arp(const unsigned char ip[4], unsigned char mac[6])
  488. {
  489. int r;
  490. /* Raw owners keep the direct poll path; stack users share the pump. */
  491. if (netdev_in_use() && !owns_nic())
  492. return -2;
  493. if (owns_nic()) {
  494. net_enter();
  495. r = net_arp_resolve(ip, mac);
  496. net_exit();
  497. return r;
  498. }
  499. return net_arp_resolve(ip, mac);
  500. }
  501. int sys_net_configured(void)
  502. {
  503. return net_is_configured();
  504. }
  505. int sys_netlog(const char *text)
  506. {
  507. static const unsigned char bcast_mac[6] = {
  508. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  509. };
  510. unsigned char dst[4];
  511. unsigned int len = 0;
  512. while (text[len] && len < NET_UDP_MAX)
  513. len++;
  514. netlog_get_host(dst);
  515. return net_udp_send(bcast_mac, dst, NETLOG_PORT, NETLOG_PORT, text, len);
  516. }
  517. int sys_netlog_conf(int enabled, const unsigned char *ip)
  518. {
  519. if (enabled >= 0)
  520. netlog_set_enabled(enabled);
  521. if (ip)
  522. netlog_set_host(ip);
  523. return 0;
  524. }