Intercept.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifdef USE_GNU_SOURCE
  28. #define _GNU_SOURCE
  29. #endif
  30. /* Name used in err msgs */
  31. char *progname = "";
  32. #include <unistd.h>
  33. #include <stdint.h>
  34. #include <pthread.h>
  35. #include <stdio.h>
  36. #include <dlfcn.h>
  37. #include <strings.h>
  38. #include <netinet/in.h>
  39. #include <sys/time.h>
  40. #include <pwd.h>
  41. #include <errno.h>
  42. #include <linux/errno.h>
  43. #include <stdarg.h>
  44. #include <netdb.h>
  45. #include <string.h>
  46. #include <stdlib.h>
  47. #include <sys/syscall.h>
  48. #include <sys/types.h>
  49. #include <sys/socket.h>
  50. #include <sys/poll.h>
  51. #include <sys/un.h>
  52. #include <arpa/inet.h>
  53. #include "Intercept.h"
  54. #include "Common.h"
  55. #ifdef CHECKS
  56. #include <sys/resource.h>
  57. #include <linux/net.h> /* for NPROTO */
  58. #define SOCK_MAX (SOCK_PACKET + 1)
  59. #define SOCK_TYPE_MASK 0xf
  60. #endif
  61. /* Global Declarations */
  62. #ifdef USE_SOCKS_DNS
  63. static int (*realresinit)(void);
  64. #endif
  65. static int (*realconnect)(CONNECT_SIG);
  66. static int (*realselect)(SELECT_SIG);
  67. static int (*realbind)(BIND_SIG);
  68. static int (*realaccept)(ACCEPT_SIG);
  69. static int (*reallisten)(LISTEN_SIG);
  70. static int (*realsocket)(SOCKET_SIG);
  71. static int (*realsetsockopt)(SETSOCKOPT_SIG);
  72. static int (*realgetsockopt)(GETSOCKOPT_SIG);
  73. static int (*realaccept4)(ACCEPT4_SIG);
  74. static long (*realsyscall)(SYSCALL_SIG);
  75. //static int (*realclone)(CLONE_SIG);
  76. //static int (*realpoll)(POLL_SIG);
  77. /* Exported Function Prototypes */
  78. void my_init(void);
  79. int connect(CONNECT_SIG);
  80. int select(SELECT_SIG);
  81. int close(CLOSE_SIG);
  82. int bind(BIND_SIG);
  83. int accept(ACCEPT_SIG);
  84. int listen(LISTEN_SIG);
  85. int socket(SOCKET_SIG);
  86. int setsockopt(SETSOCKOPT_SIG);
  87. int getsockopt(GETSOCKOPT_SIG);
  88. int accept4(ACCEPT4_SIG);
  89. long syscall(SYSCALL_SIG);
  90. //int clone(CLONE_SIG);
  91. //int poll(POLL_SIG);
  92. #ifdef USE_SOCKS_DNS
  93. int res_init(void);
  94. #endif
  95. int connect_to_service(void);
  96. int init_service_connection();
  97. void dwr(const char *fmt, ...);
  98. void load_symbols(void);
  99. void set_up_intercept();
  100. int checkpid();
  101. #define SERVICE_CONNECT_ATTEMPTS 30
  102. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  103. /* threading */
  104. pthread_mutex_t lock;
  105. pthread_mutex_t loglock;
  106. void handle_error(char *name, char *info, int err)
  107. {
  108. #ifdef ERRORS_ARE_FATAL
  109. if(err < 0) {
  110. dwr("handle_error(%s)=%d: FATAL: %s\n", name, err, info);
  111. //exit(-1);
  112. }
  113. #endif
  114. #ifdef VERBOSE
  115. //dwr("%s()=%d\n", name, err);
  116. #endif
  117. }
  118. /*------------------------------------------------------------------------------
  119. ------------------- Intercept<--->Service Comm mechanisms-----------------------
  120. ------------------------------------------------------------------------------*/
  121. static int is_initialized = 0;
  122. static int fdret_sock; // used for fd-transfers
  123. static int newfd; // used for "this_end" socket
  124. static int thispid;
  125. static char* af_sock_name = "/tmp/.ztnc_e5cd7a9e1c5311ab";
  126. /*
  127. * Check for forking
  128. */
  129. int checkpid() {
  130. if(thispid != getpid()) {
  131. printf("clone/fork detected. re-initializing this instance.\n");
  132. set_up_intercept();
  133. fdret_sock = init_service_connection();
  134. thispid = getpid();
  135. }
  136. return 0;
  137. }
  138. /*
  139. * Sends an RPC command to the service
  140. */
  141. int send_command(int rpc_fd, char *cmd)
  142. {
  143. int n_write = write(rpc_fd, cmd, BUF_SZ);
  144. if(n_write < 0){
  145. dwr("Error writing command to service (CMD = %d)\n", cmd[0]);
  146. errno = 0;
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Reads a return value from the service and sets errno (if applicable)
  153. */
  154. int get_retval()
  155. {
  156. dwr("get_retval()\n");
  157. if(fdret_sock >= 0) {
  158. int retval;
  159. int sz = sizeof(char) + sizeof(retval) + sizeof(errno);
  160. char retbuf[BUF_SZ];
  161. memset(&retbuf, '\0', sz);
  162. int n_read = read(fdret_sock, &retbuf, sz);
  163. if(n_read > 0) {
  164. memcpy(&retval, &retbuf[1], sizeof(retval));
  165. memcpy(&errno, &retbuf[1+sizeof(retval)], sizeof(errno));
  166. return retval;
  167. }
  168. }
  169. dwr("unable to read return value\n");
  170. return -1;
  171. }
  172. /* Check whether the socket is mapped to the service or not. We
  173. need to know if this is a regular AF_LOCAL socket or an end of a socketpair
  174. that the service uses. We don't want to keep state in the intercept, so
  175. we simply ask the service via an RPC */
  176. int is_mapped_to_service(int sockfd)
  177. {
  178. dwr("is_mapped_to_service()\n");
  179. char cmd[BUF_SZ];
  180. memset(cmd, '\0', BUF_SZ);
  181. cmd[0] = RPC_MAP_REQ;
  182. memcpy(&cmd[1], &sockfd, sizeof(sockfd));
  183. if(send_command(fdret_sock, cmd) < 0)
  184. return -1;
  185. return get_retval();
  186. }
  187. /*------------------------------------------------------------------------------
  188. ---------- Unix-domain socket lazy initializer (for fd-transfers)--------------
  189. ------------------------------------------------------------------------------*/
  190. /* Sets up the connection pipes and sockets to the service */
  191. int init_service_connection()
  192. {
  193. dwr("init_service_connection()\n");
  194. if(!is_initialized) {
  195. struct sockaddr_un addr;
  196. int tfd = -1, attempts = 0, conn_err = -1;
  197. memset(&addr, 0, sizeof(addr));
  198. addr.sun_family = AF_UNIX;
  199. strncpy(addr.sun_path, af_sock_name, sizeof(addr.sun_path)-1);
  200. if ( (tfd = realsocket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  201. perror("socket error");
  202. exit(-1);
  203. }
  204. while(conn_err < 0 && attempts < SERVICE_CONNECT_ATTEMPTS) {
  205. conn_err = realconnect(tfd, (struct sockaddr*)&addr, sizeof(addr));
  206. if(conn_err < 0) {
  207. dwr("re-attempting connection in %ds\n", 1+attempts);
  208. sleep(1);
  209. }
  210. else {
  211. dwr("AF_UNIX connection established: %d\n", tfd);
  212. is_initialized = 1;
  213. return tfd;
  214. }
  215. attempts++;
  216. }
  217. }
  218. return -1;
  219. }
  220. /*------------------------------------------------------------------------------
  221. ------------------------ ctors and dtors (and friends)-------------------------
  222. ------------------------------------------------------------------------------*/
  223. void my_dest(void) __attribute__ ((destructor));
  224. void my_dest(void) {
  225. //dwr("closing connections to service...\n");
  226. close(fdret_sock);
  227. pthread_mutex_destroy(&lock);
  228. }
  229. void load_symbols(void)
  230. {
  231. #ifdef USE_OLD_DLSYM
  232. void *lib;
  233. #endif
  234. /* possibly add check to beginning of each method to avoid needing to cll the constructor */
  235. if(thispid == getpid()) {
  236. dwr("detected duplicate call to global ctor (pid=%d).\n", thispid);
  237. }
  238. //dwr(" -- pid = %d\n", getpid());
  239. //dwr(" -- uid = %d\n", getuid());
  240. thispid = getpid();
  241. #ifndef USE_OLD_DLSYM
  242. realconnect = dlsym(RTLD_NEXT, "connect");
  243. realbind = dlsym(RTLD_NEXT, "bind");
  244. realaccept = dlsym(RTLD_NEXT, "accept");
  245. reallisten = dlsym(RTLD_NEXT, "listen");
  246. realsocket = dlsym(RTLD_NEXT, "socket");
  247. realbind = dlsym(RTLD_NEXT, "bind");
  248. realselect = dlsym(RTLD_NEXT, "select");
  249. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  250. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  251. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  252. //realclone = dlsym(RTLD_NEXT, "clone");
  253. realsyscall = dlsym(RTLD_NEXT, "syscall");
  254. //realsyscall = dlsym(RTLD_NEXT, "poll");
  255. #ifdef USE_SOCKS_DNS
  256. realresinit = dlsym(RTLD_NEXT, "res_init");
  257. #endif
  258. #else
  259. lib = dlopen(LIBCONNECT, RTLD_LAZY);
  260. realconnect = dlsym(lib, "connect");
  261. realbind = dlsym(lib, "bind");
  262. realaccept = dlsym(lib, "accept");
  263. reallisten = dlsym(lib, "listen");
  264. realsocket = dlsym(lib, "socket");
  265. realselect = dlsym(lib, "select");
  266. realsetsockopt = dlsym(lib, "setsockopt");
  267. realgetsockopt = dlsym(lib, "getsockopt");
  268. realaccept4 = dlsym(lib), "accept4");
  269. //realclone = dlsym(lib, "clone");
  270. realsyscall = dlsym(lib, "syscall");
  271. //realsyscall = dlsym(lib, "poll");
  272. #ifdef USE_SOCKS_DNS
  273. realresinit = dlsym(lib, "res_init");
  274. #endif
  275. dlclose(lib);
  276. lib = dlopen(LIBC, RTLD_LAZY);
  277. dlclose(lib);
  278. #endif
  279. }
  280. /* Private Function Prototypes */
  281. void _init(void) __attribute__ ((constructor));
  282. void _init(void) {
  283. set_up_intercept();
  284. }
  285. /* get symbols and initialize mutexes */
  286. void set_up_intercept()
  287. {
  288. load_symbols();
  289. if(pthread_mutex_init(&lock, NULL) != 0) {
  290. printf("error while initializing service call mutex\n");
  291. }
  292. if(pthread_mutex_init(&loglock, NULL) != 0) {
  293. printf("error while initializing log mutex mutex\n");
  294. }
  295. }
  296. /*------------------------------------------------------------------------------
  297. --------------------------------- setsockopt() ---------------------------------
  298. ------------------------------------------------------------------------------*/
  299. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  300. int setsockopt(SETSOCKOPT_SIG)
  301. {
  302. dwr("setsockopt(%d)\n", socket);
  303. if(is_mapped_to_service(socket) < 0) { // First, check if the service manages this
  304. return realsetsockopt(socket, level, option_name, option_value, option_len);
  305. }
  306. //return(realsetsockopt(socket, level, option_name, option_value, option_len));
  307. if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
  308. return 0;
  309. if(level == SOL_IP && option_name == IP_TTL)
  310. return 0;
  311. if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
  312. return 0;
  313. /* make sure we don't touch any standard outputs */
  314. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  315. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  316. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  317. if(err < 0){
  318. //perror("setsockopt():\n");
  319. }
  320. return 0;
  321. }
  322. /*------------------------------------------------------------------------------
  323. --------------------------------- getsockopt() ---------------------------------
  324. ------------------------------------------------------------------------------*/
  325. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  326. int getsockopt(GETSOCKOPT_SIG)
  327. {
  328. dwr("getsockopt(%d)\n", sockfd);
  329. if(is_mapped_to_service(sockfd) < 0) { // First, check if the service manages this
  330. return realgetsockopt(sockfd, level, optname, optval, optlen);
  331. }
  332. int err = realgetsockopt(sockfd, level, optname, optval, optlen);
  333. // FIXME: this condition will need a little more intelligence later on
  334. // -- we will need to know if this fd is a local we are spoofing, or a true local
  335. if(optname == SO_TYPE)
  336. {
  337. int* val = (int*)optval;
  338. *val = 2;
  339. optval = (void*)val;
  340. }
  341. if(err < 0){
  342. //perror("setsockopt():\n");
  343. }
  344. return 0;
  345. }
  346. /*------------------------------------------------------------------------------
  347. ----------------------------------- socket() -----------------------------------
  348. ------------------------------------------------------------------------------*/
  349. /* int socket_family, int socket_type, int protocol
  350. socket() intercept function */
  351. int socket(SOCKET_SIG)
  352. {
  353. dwr("socket()*:\n");
  354. int err;
  355. #ifdef CHECKS
  356. /* Check that type makes sense */
  357. int flags = socket_type & ~SOCK_TYPE_MASK;
  358. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  359. errno = EINVAL;
  360. handle_error("socket1", "", -1);
  361. return -1;
  362. }
  363. socket_type &= SOCK_TYPE_MASK;
  364. /* Check protocol is in range */
  365. if (socket_family < 0 || socket_family >= NPROTO){
  366. errno = EAFNOSUPPORT;
  367. handle_error("socket2", "", -1);
  368. return -1;
  369. }
  370. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  371. errno = EINVAL;
  372. handle_error("socket3", "", -1);
  373. return -1;
  374. }
  375. /* Check that we haven't hit the soft-limit file descriptors allowed */
  376. /* FIXME: Find number of open fds
  377. struct rlimit rl;
  378. getrlimit(RLIMIT_NOFILE, &rl);
  379. if(sockfd >= rl.rlim_cur){
  380. errno = EMFILE;
  381. return -1;
  382. }
  383. */
  384. /* FIXME: detect ENFILE condition */
  385. #endif
  386. char cmd[BUF_SZ];
  387. fdret_sock = !is_initialized ? init_service_connection() : fdret_sock;
  388. if(fdret_sock < 0) {
  389. dwr("BAD service connection. exiting.\n");
  390. handle_error("socket4", "", -1);
  391. exit(-1);
  392. }
  393. if(socket_family == AF_LOCAL
  394. || socket_family == AF_NETLINK
  395. || socket_family == AF_UNIX) {
  396. int err = realsocket(socket_family, socket_type, protocol);
  397. dwr("realsocket, err = %d\n", err);
  398. handle_error("socket5", "", err);
  399. return err;
  400. }
  401. /* Assemble and send RPC */
  402. struct socket_st rpc_st;
  403. rpc_st.socket_family = socket_family;
  404. rpc_st.socket_type = socket_type;
  405. rpc_st.protocol = protocol;
  406. rpc_st.__tid = syscall(SYS_gettid);
  407. memset(cmd, '\0', BUF_SZ);
  408. cmd[0] = RPC_SOCKET;
  409. dwr("pid = %d\n", thispid);
  410. memcpy(&cmd[1], &thispid, sizeof(pid_t));
  411. memcpy(&cmd[1]+sizeof(pid_t), &rpc_st, sizeof(struct socket_st));
  412. pthread_mutex_lock(&lock);
  413. send_command(fdret_sock, cmd);
  414. /* get new fd */
  415. char rbuf[16];
  416. ssize_t sz = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &newfd);
  417. dwr("read %d bytes (%s)\n", sz, &rbuf);
  418. if(sz > 0)
  419. {
  420. /* send our local-fd number back to service so
  421. it can complete its mapping table entry */
  422. memset(cmd, '\0', BUF_SZ);
  423. cmd[0] = RPC_MAP;
  424. memcpy(&cmd[1], &newfd, sizeof(newfd));
  425. if(newfd > -1) { // FIXME: check logic
  426. send_command(fdret_sock, cmd);
  427. pthread_mutex_unlock(&lock);
  428. errno = ERR_OK; // OK
  429. handle_error("socket6", "", newfd);
  430. return newfd;
  431. }
  432. else { // Try to read retval+errno since we RXed a bad fd
  433. dwr("Error, service sent bad fd.\n");
  434. err = get_retval();
  435. pthread_mutex_unlock(&lock);
  436. handle_error("socket7", "", -1);
  437. return err;
  438. }
  439. }
  440. else {
  441. dwr("Error while receiving new FD.\n");
  442. err = get_retval();
  443. pthread_mutex_unlock(&lock);
  444. handle_error("socket8", "", -1);
  445. return err;
  446. }
  447. }
  448. /*------------------------------------------------------------------------------
  449. ---------------------------------- connect() -----------------------------------
  450. ------------------------------------------------------------------------------*/
  451. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  452. connect() intercept function */
  453. int connect(CONNECT_SIG)
  454. {
  455. dwr("connect(%d):\n", __fd);
  456. print_addr(__addr);
  457. struct sockaddr_in *connaddr;
  458. connaddr = (struct sockaddr_in *) __addr;
  459. #ifdef CHECKS
  460. /* Check that this is a valid fd */
  461. if(fcntl(__fd, F_GETFD) < 0) {
  462. errno = EBADF;
  463. handle_error("connect", "EBADF", -1);
  464. return -1;
  465. }
  466. /* Check that it is a socket */
  467. int sock_type;
  468. socklen_t sock_type_len = sizeof(sock_type);
  469. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  470. errno = ENOTSOCK;
  471. handle_error("connect", "ENOTSOCK", -1);
  472. return -1;
  473. }
  474. /* Check family */
  475. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  476. errno = EAFNOSUPPORT;
  477. handle_error("connect", "EAFNOSUPPORT", -1);
  478. return -1;
  479. }
  480. /* FIXME: Check that address is in user space, return EFAULT ? */
  481. #endif
  482. /* make sure we don't touch any standard outputs */
  483. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO){
  484. if (realconnect == NULL) {
  485. handle_error("connect", "Unresolved symbol [connect]", -1);
  486. exit(-1);
  487. }
  488. return(realconnect(__fd, __addr, __len));
  489. }
  490. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  491. || connaddr->sin_family == PF_NETLINK
  492. || connaddr->sin_family == AF_NETLINK
  493. || connaddr->sin_family == AF_UNIX)) {
  494. int err = realconnect(__fd, __addr, __len);
  495. perror("connect():");
  496. //handle_error("connect", "Cannot connect to local socket", err);
  497. return err;
  498. }
  499. /* Assemble and send RPC */
  500. int err;
  501. char cmd[BUF_SZ];
  502. memset(cmd, '\0', BUF_SZ);
  503. struct connect_st rpc_st;
  504. rpc_st.__tid = syscall(SYS_gettid);
  505. rpc_st.__fd = __fd;
  506. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr));
  507. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  508. cmd[0] = RPC_CONNECT;
  509. memcpy(&cmd[1], &rpc_st, sizeof(struct connect_st));
  510. pthread_mutex_lock(&lock);
  511. send_command(fdret_sock, cmd);
  512. /*
  513. if(sock_type && O_NONBLOCK) {
  514. //pthread_mutex_unlock(&lock);
  515. //return EINPROGRESS;
  516. }
  517. */
  518. err = get_retval();
  519. pthread_mutex_unlock(&lock);
  520. //handle_error("connect", "", err);
  521. return err;
  522. }
  523. /*------------------------------------------------------------------------------
  524. ---------------------------------- select() ------------------------------------
  525. ------------------------------------------------------------------------------*/
  526. /* int n, fd_set *readfds, fd_set *writefds,
  527. fd_set *exceptfds, struct timeval *timeout */
  528. int select(SELECT_SIG)
  529. {
  530. //dwr("select():\n");
  531. return realselect(n, readfds, writefds, exceptfds, timeout);
  532. }
  533. /*------------------------------------------------------------------------------
  534. ------------------------------------ bind() ------------------------------------
  535. ------------------------------------------------------------------------------*/
  536. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  537. bind() intercept function */
  538. int bind(BIND_SIG)
  539. {
  540. dwr("bind(%d):\n", sockfd);
  541. print_addr(addr);
  542. #ifdef CHECKS
  543. /* Check that this is a valid fd */
  544. if(fcntl(sockfd, F_GETFD) < 0) {
  545. errno = EBADF;
  546. handle_error("bind", "EBADF", -1);
  547. return -1;
  548. }
  549. /* Check that it is a socket */
  550. int opt = -1;
  551. socklen_t opt_len;
  552. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  553. errno = ENOTSOCK;
  554. handle_error("bind", "ENOTSOCK", -1);
  555. return -1;
  556. }
  557. #endif
  558. int err;
  559. /* make sure we don't touch any standard outputs */
  560. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  561. return(realbind(sockfd, addr, addrlen));
  562. /* If local, just use normal syscall */
  563. struct sockaddr_in *connaddr;
  564. connaddr = (struct sockaddr_in *)addr;
  565. if(connaddr->sin_family == AF_LOCAL
  566. || connaddr->sin_family == AF_NETLINK
  567. || connaddr->sin_family == AF_UNIX) {
  568. int err = realbind(sockfd, addr, addrlen);
  569. dwr("realbind, err = %d\n", err);
  570. return err;
  571. }
  572. /* Assemble and send RPC */
  573. char cmd[BUF_SZ];
  574. struct bind_st rpc_st;
  575. rpc_st.sockfd = sockfd;
  576. rpc_st.__tid = syscall(SYS_gettid);
  577. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr));
  578. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  579. cmd[0]=RPC_BIND;
  580. memcpy(&cmd[1], &rpc_st, sizeof(struct bind_st));
  581. pthread_mutex_lock(&lock);
  582. send_command(fdret_sock, cmd);
  583. err = get_retval();
  584. pthread_mutex_unlock(&lock);
  585. errno = ERR_OK;
  586. handle_error("bind", "", err);
  587. return err;
  588. }
  589. /*------------------------------------------------------------------------------
  590. ----------------------------------- accept4() ----------------------------------
  591. ------------------------------------------------------------------------------*/
  592. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  593. int accept4(ACCEPT4_SIG)
  594. {
  595. dwr("accept4(%d):\n", sockfd);
  596. #ifdef CHECKS
  597. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  598. errno = EINVAL;
  599. return -1;
  600. }
  601. #endif
  602. int newfd = accept(sockfd, addr, addrlen);
  603. if(newfd > 0) {
  604. if(flags & SOCK_CLOEXEC)
  605. fcntl(newfd, F_SETFL, FD_CLOEXEC);
  606. if(flags & SOCK_NONBLOCK)
  607. fcntl(newfd, F_SETFL, O_NONBLOCK);
  608. }
  609. handle_error("accept4", "", newfd);
  610. return newfd;
  611. }
  612. /*------------------------------------------------------------------------------
  613. ----------------------------------- accept() -----------------------------------
  614. ------------------------------------------------------------------------------*/
  615. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  616. accept() intercept function */
  617. int accept(ACCEPT_SIG)
  618. {
  619. dwr("accept(%d):\n", sockfd);
  620. #ifdef CHECKS
  621. /* Check that this is a valid fd */
  622. if(fcntl(sockfd, F_GETFD) < 0) {
  623. return -1;
  624. errno = EBADF;
  625. dwr("EBADF\n");
  626. handle_error("accept", "EBADF", -1);
  627. return -1;
  628. }
  629. /* Check that it is a socket */
  630. int opt;
  631. socklen_t opt_len;
  632. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  633. errno = ENOTSOCK;
  634. dwr("ENOTSOCK\n");
  635. handle_error("accept", "ENOTSOCK", -1);
  636. return -1;
  637. }
  638. /* Check that this socket supports accept() */
  639. if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
  640. errno = EOPNOTSUPP;
  641. dwr("EOPNOTSUPP\n");
  642. handle_error("accept", "EOPNOTSUPP", -1);
  643. return -1;
  644. }
  645. /* Check that we haven't hit the soft-limit file descriptors allowed */
  646. struct rlimit rl;
  647. getrlimit(RLIMIT_NOFILE, &rl);
  648. if(sockfd >= rl.rlim_cur){
  649. errno = EMFILE;
  650. dwr("EMFILE\n");
  651. handle_error("accept", "EMFILE", -1);
  652. return -1;
  653. }
  654. /* Check address length */
  655. if(addrlen < 0) {
  656. errno = EINVAL;
  657. dwr("EINVAL\n");
  658. handle_error("accept", "EINVAL", -1);
  659. return -1;
  660. }
  661. #endif
  662. /* redirect calls for standard I/O descriptors to kernel */
  663. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
  664. dwr("realaccept():\n");
  665. return(realaccept(sockfd, addr, addrlen));
  666. }
  667. if(addr)
  668. addr->sa_family = AF_INET;
  669. /* TODO: also get address info */
  670. char cmd[BUF_SZ];
  671. if(realaccept == NULL) {
  672. handle_error("accept", "Unresolved symbol [accept]", -1);
  673. return -1;
  674. }
  675. //if(opt & O_NONBLOCK)
  676. //fcntl(sockfd, F_SETFL, O_NONBLOCK);
  677. char rbuf[16], c[1];
  678. int new_conn_socket;
  679. int n = read(sockfd, c, sizeof(c)); // Read signal byte
  680. if(n > 0)
  681. {
  682. ssize_t size = sock_fd_read(fdret_sock, rbuf, sizeof(rbuf), &new_conn_socket);
  683. if(size > 0) {
  684. /* Send our local-fd number back to service so it can complete its mapping table */
  685. memset(cmd, '\0', BUF_SZ);
  686. cmd[0] = RPC_MAP;
  687. memcpy(&cmd[1], &new_conn_socket, sizeof(new_conn_socket));
  688. pthread_mutex_lock(&lock);
  689. int n_write = write(fdret_sock, cmd, BUF_SZ);
  690. if(n_write < 0) {
  691. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  692. handle_error("accept", "ECONNABORTED - Error sending perceived FD to service", -1);
  693. return -1;
  694. }
  695. pthread_mutex_unlock(&lock);
  696. errno = ERR_OK;
  697. dwr("*accept()=%d\n", new_conn_socket);
  698. handle_error("accept", "", new_conn_socket);
  699. return new_conn_socket; // OK
  700. }
  701. else {
  702. errno = ECONNABORTED; // FIXME: Closest match, service unreachable
  703. handle_error("accept", "ECONNABORTED - Error receiving new FD from service", -1);
  704. return -1;
  705. }
  706. }
  707. errno = EBADF;
  708. handle_error("accept", "EBADF - Error reading signal byte from service", -1);
  709. return -1;
  710. }
  711. /*------------------------------------------------------------------------------
  712. ------------------------------------- listen()----------------------------------
  713. ------------------------------------------------------------------------------*/
  714. /* int sockfd, int backlog
  715. listen() intercept function */
  716. int listen(LISTEN_SIG)
  717. {
  718. dwr("listen(%d):\n", sockfd);
  719. int sock_type;
  720. socklen_t sock_type_len = sizeof(sock_type);
  721. #ifdef CHECKS
  722. /* Check that this is a valid fd */
  723. if(fcntl(sockfd, F_GETFD) < 0) {
  724. errno = EBADF;
  725. handle_error("listen", "EBADF", -1);
  726. return -1;
  727. }
  728. /* Check that it is a socket */
  729. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  730. errno = ENOTSOCK;
  731. handle_error("listen", "ENOTSOCK", -1);
  732. return -1;
  733. }
  734. /* Check that this socket supports accept() */
  735. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  736. errno = EOPNOTSUPP;
  737. handle_error("listen", "EOPNOTSUPP", -1);
  738. return -1;
  739. }
  740. #endif
  741. /* make sure we don't touch any standard outputs */
  742. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  743. return(reallisten(sockfd, backlog));
  744. if(is_mapped_to_service(sockfd) < 0) {
  745. // We now know this socket is not one of our socketpairs
  746. int err = reallisten(sockfd, backlog);
  747. dwr("reallisten()=%d\n", err);
  748. return err;
  749. }
  750. /* Assemble and send RPC */
  751. char cmd[BUF_SZ];
  752. memset(cmd, '\0', BUF_SZ);
  753. struct listen_st rpc_st;
  754. rpc_st.sockfd = sockfd;
  755. rpc_st.backlog = backlog;
  756. rpc_st.__tid = syscall(SYS_gettid);
  757. cmd[0] = RPC_LISTEN;
  758. memcpy(&cmd[1], &rpc_st, sizeof(struct listen_st));
  759. pthread_mutex_lock(&lock);
  760. send_command(fdret_sock, cmd);
  761. int err = get_retval();
  762. pthread_mutex_unlock(&lock);
  763. handle_error("listen", "", ERR_OK);
  764. return ERR_OK;
  765. }
  766. /*------------------------------------------------------------------------------
  767. -------------------------------------- clone()----------------------------------
  768. ------------------------------------------------------------------------------*/
  769. // int (*fn)(void *), void *child_stack, int flags, void *arg, ...
  770. /*
  771. int clone(CLONE_SIG)
  772. {
  773. dwr("clone()\n");
  774. return realclone(fn, child_stack, flags, arg);
  775. }
  776. */
  777. /*------------------------------------------------------------------------------
  778. -------------------------------------- poll()-----------------------------------
  779. ------------------------------------------------------------------------------*/
  780. // struct pollfd *fds, nfds_t nfds, int timeout
  781. /*
  782. int poll(POLL_SIG)
  783. {
  784. dwr("poll()\n");
  785. return realpoll(fds, nfds, timeout);
  786. //return ERESTART_RESTARTBLOCK;
  787. }
  788. */
  789. /*------------------------------------------------------------------------------
  790. ------------------------------------ syscall()----------------------------------
  791. ------------------------------------------------------------------------------*/
  792. long syscall(SYSCALL_SIG)
  793. {
  794. dwr("syscall(%u, ...):\n", number);
  795. va_list ap;
  796. uintptr_t a,b,c,d,e,f;
  797. va_start(ap, number);
  798. a=va_arg(ap, uintptr_t);
  799. b=va_arg(ap, uintptr_t);
  800. c=va_arg(ap, uintptr_t);
  801. d=va_arg(ap, uintptr_t);
  802. e=va_arg(ap, uintptr_t);
  803. f=va_arg(ap, uintptr_t);
  804. va_end(ap);
  805. #if defined(__i386__)
  806. /* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
  807. args[0] = (unsigned long) fd;
  808. args[1] = (unsigned long) addr;
  809. args[2] = (unsigned long) addrlen;
  810. args[3] = (unsigned long) flags;
  811. */
  812. #else
  813. if(number == __NR_accept4) {
  814. int sockfd = a;
  815. struct sockaddr * addr = (struct sockaddr*)b;
  816. socklen_t * addrlen = (socklen_t*)c;
  817. int flags = d;
  818. int old_errno = errno;
  819. int err = accept4(sockfd, addr, addrlen, flags);
  820. errno = old_errno;
  821. if(err == -EBADF) {
  822. //errno = EAGAIN;
  823. err = -EAGAIN;
  824. //exit(0);
  825. }
  826. return err;
  827. }
  828. #endif
  829. return realsyscall(number,a,b,c,d,e,f);
  830. }