Intercept.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. #include <unistd.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <dlfcn.h>
  34. #include <strings.h>
  35. #include <netinet/in.h>
  36. #include <sys/time.h>
  37. #include <pwd.h>
  38. #include <errno.h>
  39. #include <linux/errno.h>
  40. #include <stdarg.h>
  41. #include <netdb.h>
  42. #include <string.h>
  43. #include <sys/syscall.h>
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <sys/poll.h>
  47. #include <sys/un.h>
  48. #include <arpa/inet.h>
  49. #include <sys/resource.h>
  50. #include <linux/net.h> /* for NPROTO */
  51. #define SOCK_MAX (SOCK_PACKET + 1)
  52. #define SOCK_TYPE_MASK 0xf
  53. #include "Intercept.h"
  54. #include "rpc.h"
  55. #include "common.inc.c"
  56. /* Global Declarations */
  57. static int (*realconnect)(CONNECT_SIG);
  58. static int (*realbind)(BIND_SIG);
  59. static int (*realaccept)(ACCEPT_SIG);
  60. static int (*reallisten)(LISTEN_SIG);
  61. static int (*realsocket)(SOCKET_SIG);
  62. static int (*realsetsockopt)(SETSOCKOPT_SIG);
  63. static int (*realgetsockopt)(GETSOCKOPT_SIG);
  64. static int (*realaccept4)(ACCEPT4_SIG);
  65. static long (*realsyscall)(SYSCALL_SIG);
  66. static int (*realclose)(CLOSE_SIG);
  67. static int (*realclone)(CLONE_SIG);
  68. static int (*realdup2)(DUP2_SIG);
  69. static int (*realdup3)(DUP3_SIG);
  70. static int (*realgetsockname)(GETSOCKNAME_SIG);
  71. /* Exported Function Prototypes */
  72. void my_init(void);
  73. int connect(CONNECT_SIG);
  74. int bind(BIND_SIG);
  75. int accept(ACCEPT_SIG);
  76. int listen(LISTEN_SIG);
  77. int socket(SOCKET_SIG);
  78. int setsockopt(SETSOCKOPT_SIG);
  79. int getsockopt(GETSOCKOPT_SIG);
  80. int accept4(ACCEPT4_SIG);
  81. long syscall(SYSCALL_SIG);
  82. int close(CLOSE_SIG);
  83. int clone(CLONE_SIG);
  84. int dup2(DUP2_SIG);
  85. int dup3(DUP3_SIG);
  86. int getsockname(GETSOCKNAME_SIG);
  87. static int init_service_connection();
  88. static void load_symbols(void);
  89. static void set_up_intercept();
  90. /*------------------------------------------------------------------------------
  91. ------------------- Intercept<--->Service Comm mechanisms ----------------------
  92. ------------------------------------------------------------------------------*/
  93. static int rpcfd = -1; /* used for fd-transfers */
  94. static int thispid = -1;
  95. static int instance_count = 0;
  96. static int connected_to_service() {
  97. return rpcfd == -1 ? 0 : 1;
  98. }
  99. /* Check whether the socket is mapped to the service or not. We
  100. need to know if this is a regular AF_LOCAL socket or an end of a socketpair
  101. that the service uses. We don't want to keep state in the intercept, so
  102. we simply ask the service via an RPC */
  103. static int is_mapped_to_service(int sockfd)
  104. {
  105. if(rpcfd < 0)
  106. return 0; /* no connection obviously implies no mapping */
  107. dwr(MSG_DEBUG,"is_mapped_to_service()\n");
  108. return rpc_send_command(RPC_MAP_REQ, rpcfd, &sockfd, sizeof(sockfd));
  109. }
  110. /* Sets up the connection pipes and sockets to the service */
  111. static int init_service_connection()
  112. {
  113. const char *network_id;
  114. char rpcname[1024];
  115. network_id = getenv("ZT_NC_NETWORK");
  116. /* Do noting if not configured (sanity check -- should never get here in this case) */
  117. if (!network_id){
  118. fprintf(stderr, "init_service_connection(): ZT_NC_NETWORK not set.\n");
  119. exit(0);
  120. }
  121. if((rpcfd < 0 && instance_count==0) || thispid != getpid())
  122. rpc_mutex_init();
  123. strncpy(rpcname,network_id,sizeof(rpcname));
  124. instance_count++;
  125. rpcfd = rpc_join(rpcname);
  126. fprintf(stderr, "rpc_join = %d\n", rpcfd);
  127. return rpcfd;
  128. }
  129. /*------------------------------------------------------------------------------
  130. ------------------------ ctors and dtors (and friends) ------------------------
  131. ------------------------------------------------------------------------------*/
  132. static void my_dest(void) __attribute__ ((destructor));
  133. static void my_dest(void) {
  134. dwr(MSG_DEBUG,"closing connections to service...\n");
  135. rpc_mutex_destroy();
  136. }
  137. static void load_symbols(void)
  138. {
  139. if(thispid == getpid()) {
  140. dwr(MSG_DEBUG,"detected duplicate call to global constructor (pid=%d).\n", thispid);
  141. }
  142. thispid = getpid();
  143. realconnect = dlsym(RTLD_NEXT, "connect");
  144. realbind = dlsym(RTLD_NEXT, "bind");
  145. realaccept = dlsym(RTLD_NEXT, "accept");
  146. reallisten = dlsym(RTLD_NEXT, "listen");
  147. realsocket = dlsym(RTLD_NEXT, "socket");
  148. realbind = dlsym(RTLD_NEXT, "bind");
  149. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  150. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  151. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  152. realclone = dlsym(RTLD_NEXT, "clone");
  153. realclose = dlsym(RTLD_NEXT, "close");
  154. realsyscall = dlsym(RTLD_NEXT, "syscall");
  155. realdup2 = dlsym(RTLD_NEXT, "dup2");
  156. realdup3 = dlsym(RTLD_NEXT, "dup3");
  157. realgetsockname = dlsym(RTLD_NEXT, "getsockname");
  158. }
  159. /* Private Function Prototypes */
  160. static void _init(void) __attribute__ ((constructor));
  161. static void _init(void) { set_up_intercept(); }
  162. /* get symbols and initialize mutexes */
  163. static void set_up_intercept()
  164. {
  165. if (!getenv("ZT_NC_NETWORK"))
  166. return;
  167. /* Hook/intercept Posix net API symbols */
  168. load_symbols();
  169. }
  170. /*------------------------------------------------------------------------------
  171. --------------------------------- setsockopt() ---------------------------------
  172. ------------------------------------------------------------------------------*/
  173. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  174. int setsockopt(SETSOCKOPT_SIG)
  175. {
  176. if(realsetsockopt == NULL){
  177. dwr(MSG_ERROR, "setsockopt(): SYMBOL NOT FOUND.\n");
  178. return -1;
  179. }
  180. dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
  181. /* return(realsetsockopt(socket, level, option_name, option_value, option_len)); */
  182. if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
  183. return 0;
  184. if(level == SOL_IP && option_name == IP_TTL)
  185. return 0;
  186. if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
  187. return 0;
  188. /* make sure we don't touch any standard outputs */
  189. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  190. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  191. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  192. if(err < 0){
  193. perror("setsockopt():\n");
  194. }
  195. return 0;
  196. }
  197. /*------------------------------------------------------------------------------
  198. --------------------------------- getsockopt() ---------------------------------
  199. ------------------------------------------------------------------------------*/
  200. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  201. int getsockopt(GETSOCKOPT_SIG)
  202. {
  203. if(realgetsockopt == NULL){
  204. dwr(MSG_ERROR, "getsockopt(): SYMBOL NOT FOUND.\n");
  205. return -1;
  206. }
  207. dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
  208. if(is_mapped_to_service(sockfd) <= 0) { // First, check if the service manages this
  209. return realgetsockopt(sockfd, level, optname, optval, optlen);
  210. }
  211. //int err = realgetsockopt(sockfd, level, optname, optval, optlen);
  212. /* TODO: this condition will need a little more intelligence later on
  213. -- we will need to know if this fd is a local we are spoofing, or a true local */
  214. if(optname == SO_TYPE) {
  215. int* val = (int*)optval;
  216. *val = 2;
  217. optval = (void*)val;
  218. }
  219. return 0;
  220. }
  221. /*------------------------------------------------------------------------------
  222. ----------------------------------- socket() -----------------------------------
  223. ------------------------------------------------------------------------------*/
  224. /* int socket_family, int socket_type, int protocol
  225. socket() intercept function */
  226. int socket(SOCKET_SIG)
  227. {
  228. if(realsocket == NULL)
  229. set_up_intercept();
  230. dwr(MSG_DEBUG,"socket():\n");
  231. int newfd = -1;
  232. /* Check that type makes sense */
  233. int flags = socket_type & ~SOCK_TYPE_MASK;
  234. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  235. errno = EINVAL;
  236. return -1;
  237. }
  238. socket_type &= SOCK_TYPE_MASK;
  239. /* Check protocol is in range */
  240. if (socket_family < 0 || socket_family >= NPROTO){
  241. errno = EAFNOSUPPORT;
  242. return -1;
  243. }
  244. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  245. errno = EINVAL;
  246. return -1;
  247. }
  248. /* Check that we haven't hit the soft-limit file descriptors allowed */
  249. /* FIXME: Find number of open fds
  250. struct rlimit rl;
  251. getrlimit(RLIMIT_NOFILE, &rl);
  252. if(sockfd >= rl.rlim_cur){
  253. errno = EMFILE;
  254. return -1;
  255. }
  256. */
  257. /* TODO: detect ENFILE condition */
  258. if(socket_family == AF_LOCAL
  259. || socket_family == AF_NETLINK
  260. || socket_family == AF_UNIX) {
  261. int err = realsocket(socket_family, socket_type, protocol);
  262. dwr(MSG_DEBUG,"realsocket() = %d\n", err);
  263. return err;
  264. }
  265. rpcfd = !connected_to_service() ? init_service_connection() : rpcfd;
  266. if(rpcfd < 0) {
  267. dwr(MSG_DEBUG,"BAD service connection. exiting.\n");
  268. exit(-1);
  269. }
  270. /* Assemble and send RPC */
  271. struct socket_st rpc_st;
  272. rpc_st.socket_family = socket_family;
  273. rpc_st.socket_type = socket_type;
  274. rpc_st.protocol = protocol;
  275. rpc_st.__tid = syscall(SYS_gettid);
  276. newfd = rpc_send_command(RPC_SOCKET, rpcfd, &rpc_st, sizeof(struct socket_st));
  277. if(newfd > 0)
  278. {
  279. dwr(MSG_DEBUG,"sending fd = %d to Service over (%d)\n", newfd, rpcfd);
  280. /* send our local-fd number back to service so
  281. it can complete its mapping table entry */
  282. /* send fd mapping and get confirmation */
  283. if(rpc_send_command(RPC_MAP, rpcfd, &newfd, sizeof(newfd)) > -1) {
  284. errno = ERR_OK;
  285. dwr(MSG_DEBUG, "RXd fd confirmation. Mapped!\n");
  286. return newfd; /* Mapping complete, everything is OK */
  287. }
  288. }
  289. dwr(MSG_DEBUG,"Error while receiving new fd.\n");
  290. return -1;
  291. }
  292. /*------------------------------------------------------------------------------
  293. ---------------------------------- connect() -----------------------------------
  294. ------------------------------------------------------------------------------*/
  295. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  296. connect() intercept function */
  297. int connect(CONNECT_SIG)
  298. {
  299. if(realconnect == NULL){
  300. dwr(MSG_ERROR, "connect(): SYMBOL NOT FOUND.\n");
  301. return -1;
  302. }
  303. dwr(MSG_DEBUG,"connect(%d):\n", __fd);
  304. /* print_addr(__addr); */
  305. struct sockaddr_in *connaddr;
  306. connaddr = (struct sockaddr_in *) __addr;
  307. /* Check that this is a valid fd */
  308. if(fcntl(__fd, F_GETFD) < 0) {
  309. errno = EBADF;
  310. return -1;
  311. }
  312. /* Check that it is a socket */
  313. int sock_type;
  314. socklen_t sock_type_len = sizeof(sock_type);
  315. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  316. errno = ENOTSOCK;
  317. return -1;
  318. }
  319. /* Check family */
  320. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  321. errno = EAFNOSUPPORT;
  322. return -1;
  323. }
  324. /* FIXME: Check that address is in user space, return EFAULT ? */
  325. /* make sure we don't touch any standard outputs */
  326. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO)
  327. return(realconnect(__fd, __addr, __len));
  328. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  329. || connaddr->sin_family == PF_NETLINK
  330. || connaddr->sin_family == AF_NETLINK
  331. || connaddr->sin_family == AF_UNIX)) {
  332. int err = realconnect(__fd, __addr, __len);
  333. perror("connect():");
  334. return err;
  335. }
  336. /* Assemble and send RPC */
  337. struct connect_st rpc_st;
  338. rpc_st.__tid = syscall(SYS_gettid);
  339. rpc_st.__fd = __fd;
  340. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr_storage));
  341. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  342. return rpc_send_command(RPC_CONNECT, rpcfd, &rpc_st, sizeof(struct connect_st));
  343. }
  344. /*------------------------------------------------------------------------------
  345. ------------------------------------ bind() ------------------------------------
  346. ------------------------------------------------------------------------------*/
  347. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  348. bind() intercept function */
  349. int bind(BIND_SIG)
  350. {
  351. if(realbind == NULL){
  352. dwr(MSG_ERROR, "bind(): SYMBOL NOT FOUND.\n");
  353. return -1;
  354. }
  355. dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
  356. /* Check that this is a valid fd */
  357. if(fcntl(sockfd, F_GETFD) < 0) {
  358. errno = EBADF;
  359. return -1;
  360. }
  361. /* Check that it is a socket */
  362. int opt = -1;
  363. socklen_t opt_len;
  364. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  365. errno = ENOTSOCK;
  366. return -1;
  367. }
  368. /* make sure we don't touch any standard outputs */
  369. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  370. return(realbind(sockfd, addr, addrlen));
  371. /* If local, just use normal syscall */
  372. struct sockaddr_in *connaddr;
  373. connaddr = (struct sockaddr_in *)addr;
  374. if(connaddr->sin_family == AF_LOCAL
  375. || connaddr->sin_family == AF_NETLINK
  376. || connaddr->sin_family == AF_UNIX) {
  377. int err = realbind(sockfd, addr, addrlen);
  378. dwr(MSG_DEBUG,"realbind, err = %d\n", err);
  379. return err;
  380. }
  381. int port = connaddr->sin_port;
  382. int ip = connaddr->sin_addr.s_addr;
  383. unsigned char d[4];
  384. d[0] = ip & 0xFF;
  385. d[1] = (ip >> 8) & 0xFF;
  386. d[2] = (ip >> 16) & 0xFF;
  387. d[3] = (ip >> 24) & 0xFF;
  388. dwr(MSG_DEBUG, "bind(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
  389. /* Assemble and send RPC */
  390. struct bind_st rpc_st;
  391. rpc_st.sockfd = sockfd;
  392. rpc_st.__tid = syscall(SYS_gettid);
  393. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
  394. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  395. return rpc_send_command(RPC_BIND, rpcfd, &rpc_st, sizeof(struct bind_st));
  396. }
  397. /*------------------------------------------------------------------------------
  398. ----------------------------------- accept4() ----------------------------------
  399. ------------------------------------------------------------------------------*/
  400. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  401. int accept4(ACCEPT4_SIG)
  402. {
  403. if(realaccept4 == NULL){
  404. dwr(MSG_ERROR, "accept4(): SYMBOL NOT FOUND.\n");
  405. return -1;
  406. }
  407. dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
  408. if ((flags & SOCK_CLOEXEC))
  409. fcntl(sockfd, F_SETFL, FD_CLOEXEC);
  410. if ((flags & SOCK_NONBLOCK))
  411. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  412. int newfd = accept(sockfd, addr, addrlen);
  413. return newfd;
  414. }
  415. /*------------------------------------------------------------------------------
  416. ----------------------------------- accept() -----------------------------------
  417. ------------------------------------------------------------------------------*/
  418. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  419. accept() intercept function */
  420. int accept(ACCEPT_SIG)
  421. {
  422. if(realaccept == NULL){
  423. dwr(MSG_ERROR, "accept(): SYMBOL NOT FOUND.\n");
  424. return -1;
  425. }
  426. dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
  427. /* Check that this is a valid fd */
  428. if(fcntl(sockfd, F_GETFD) < 0) {
  429. return -1;
  430. errno = EBADF;
  431. dwr(MSG_DEBUG,"EBADF\n");
  432. return -1;
  433. }
  434. /* Check that it is a socket */
  435. int opt;
  436. socklen_t opt_len;
  437. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  438. errno = ENOTSOCK;
  439. dwr(MSG_DEBUG,"ENOTSOCK\n");
  440. return -1;
  441. }
  442. /* Check that this socket supports accept() */
  443. if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
  444. errno = EOPNOTSUPP;
  445. dwr(MSG_DEBUG,"EOPNOTSUPP\n");
  446. return -1;
  447. }
  448. /* Check that we haven't hit the soft-limit file descriptors allowed */
  449. struct rlimit rl;
  450. getrlimit(RLIMIT_NOFILE, &rl);
  451. if(sockfd >= rl.rlim_cur){
  452. errno = EMFILE;
  453. dwr(MSG_DEBUG,"EMFILE\n");
  454. return -1;
  455. }
  456. /* Check address length */
  457. if(addrlen < 0) {
  458. errno = EINVAL;
  459. dwr(MSG_DEBUG,"EINVAL\n");
  460. return -1;
  461. }
  462. /* redirect calls for standard I/O descriptors to kernel */
  463. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
  464. dwr(MSG_DEBUG,"realaccept():\n");
  465. return(realaccept(sockfd, addr, addrlen));
  466. }
  467. if(addr)
  468. addr->sa_family = AF_INET;
  469. /* TODO: also get address info */
  470. /* The following line is required for libuv/nodejs to accept connections properly,
  471. however, this has the side effect of causing certain webservers to max out the CPU
  472. in an accept loop */
  473. //fcntl(sockfd, F_SETFL, SOCK_NONBLOCK);
  474. int new_conn_socket = get_new_fd(sockfd);
  475. if(new_conn_socket > 0)
  476. {
  477. dwr(MSG_DEBUG, "accept(): RX: fd = (%d) over (%d)\n", new_conn_socket, rpcfd);
  478. /* Send our local-fd number back to service so it can complete its mapping table */
  479. dwr(MSG_DEBUG, "accept(): sending perceived fd (%d) to service.\n", new_conn_socket);
  480. rpc_send_command(RPC_MAP, rpcfd, &new_conn_socket, sizeof(new_conn_socket));
  481. dwr(MSG_DEBUG,"accept()=%d\n", new_conn_socket);
  482. errno = ERR_OK;
  483. return new_conn_socket; /* OK */
  484. }
  485. dwr(MSG_DEBUG, "accept(): EAGAIN - Error reading signal byte from service");
  486. errno = EAGAIN;
  487. return -EAGAIN;
  488. }
  489. /*------------------------------------------------------------------------------
  490. ------------------------------------- listen()----------------------------------
  491. ------------------------------------------------------------------------------*/
  492. /* int sockfd, int backlog */
  493. int listen(LISTEN_SIG)
  494. {
  495. if(reallisten == NULL){
  496. dwr(MSG_ERROR, "listen(): SYMBOL NOT FOUND.\n");
  497. return -1;
  498. }
  499. dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
  500. int sock_type;
  501. socklen_t sock_type_len = sizeof(sock_type);
  502. /* Check that this is a valid fd */
  503. if(fcntl(sockfd, F_GETFD) < 0) {
  504. errno = EBADF;
  505. return -1;
  506. }
  507. /* Check that it is a socket */
  508. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  509. errno = ENOTSOCK;
  510. return -1;
  511. }
  512. /* Check that this socket supports accept() */
  513. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  514. errno = EOPNOTSUPP;
  515. return -1;
  516. }
  517. /* make sure we don't touch any standard outputs */
  518. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  519. return(reallisten(sockfd, backlog));
  520. if(is_mapped_to_service(sockfd) < 0) {
  521. /* We now know this socket is not one of our socketpairs */
  522. int err = reallisten(sockfd, backlog);
  523. dwr(MSG_DEBUG,"reallisten()=%d\n", err);
  524. return err;
  525. }
  526. /* Assemble and send RPC */
  527. struct listen_st rpc_st;
  528. rpc_st.sockfd = sockfd;
  529. rpc_st.backlog = backlog;
  530. rpc_st.__tid = syscall(SYS_gettid);
  531. return rpc_send_command(RPC_LISTEN, rpcfd, &rpc_st, sizeof(struct listen_st));
  532. }
  533. /*------------------------------------------------------------------------------
  534. -------------------------------------- clone() ---------------------------------
  535. ------------------------------------------------------------------------------*/
  536. /* int (*fn)(void *), void *child_stack, int flags, void *arg, ... */
  537. int clone(CLONE_SIG)
  538. {
  539. if(realclone == NULL){
  540. dwr(MSG_ERROR, "clone(): SYMBOL NOT FOUND.\n");
  541. return -1;
  542. }
  543. dwr(MSG_DEBUG,"clone()\n");
  544. int err = realclone(fn, child_stack, flags, arg);
  545. init_service_connection();
  546. return err;
  547. }
  548. /*------------------------------------------------------------------------------
  549. ------------------------------------- close() ----------------------------------
  550. ------------------------------------------------------------------------------*/
  551. /* int fd */
  552. int close(CLOSE_SIG)
  553. {
  554. dwr(MSG_DEBUG, "close(%d)\n", fd);
  555. if(realclose == NULL)
  556. init_service_connection();
  557. if(fd == rpcfd)
  558. return -1; /* TODO: Ignore request to shut down our rpc fd, this is *almost always* safe */
  559. if(fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
  560. return realclose(fd);
  561. return -1;
  562. }
  563. /*------------------------------------------------------------------------------
  564. -------------------------------------- dup2() ----------------------------------
  565. ------------------------------------------------------------------------------*/
  566. /* int oldfd, int newfd */
  567. int dup2(DUP2_SIG)
  568. {
  569. if(realdup2 == NULL){
  570. dwr(MSG_ERROR, "dup2(): SYMBOL NOT FOUND.\n");
  571. return -1;
  572. }
  573. dwr(MSG_DEBUG,"dup2(%d, %d)\n", oldfd, newfd);
  574. if(oldfd == rpcfd) {
  575. dwr(MSG_DEBUG,"client application attempted to dup2 RPC socket (%d). This is not allowed.\n", oldfd);
  576. errno = EBADF;
  577. return -1;
  578. }
  579. return realdup2(oldfd, newfd);
  580. }
  581. /*------------------------------------------------------------------------------
  582. -------------------------------------- dup3() ----------------------------------
  583. ------------------------------------------------------------------------------*/
  584. /* int oldfd, int newfd, int flags */
  585. int dup3(DUP3_SIG)
  586. {
  587. if(realdup3 == NULL){
  588. dwr(MSG_ERROR, "dup3(): SYMBOL NOT FOUND.\n");
  589. return -1;
  590. }
  591. dwr(MSG_DEBUG,"dup3(%d, %d, %d)\n", oldfd, newfd, flags);
  592. return realdup3(oldfd, newfd, flags);
  593. }
  594. /*------------------------------------------------------------------------------
  595. -------------------------------- getsockname() ---------------------------------
  596. ------------------------------------------------------------------------------*/
  597. /* define GETSOCKNAME_SIG int sockfd, struct sockaddr *addr, socklen_t *addrlen */
  598. int getsockname(GETSOCKNAME_SIG)
  599. {
  600. if (realgetsockname == NULL) {
  601. dwr(MSG_ERROR, "getsockname(): SYMBOL NOT FOUND. \n");
  602. return -1;
  603. }
  604. dwr(MSG_DEBUG, "getsockname(%d)\n", sockfd);
  605. if(!is_mapped_to_service(sockfd))
  606. return realgetsockname(sockfd, addr, addrlen);
  607. /* This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in
  608. * and is an IPv4 address. */
  609. /* assemble and send command */
  610. struct getsockname_st rpc_st;
  611. rpc_st.sockfd = sockfd;
  612. memcpy(&rpc_st.addr, addr, *addrlen);
  613. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  614. rpc_send_command(RPC_GETSOCKNAME, rpcfd, &rpc_st, sizeof(struct getsockname_st));
  615. /* read address info from service */
  616. char addrbuf[sizeof(struct sockaddr_storage)];
  617. memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
  618. read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage));
  619. struct sockaddr_storage sock_storage;
  620. memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
  621. *addrlen = sizeof(struct sockaddr_in);
  622. memcpy(addr, &sock_storage, (*addrlen > sizeof(sock_storage)) ? sizeof(sock_storage) : *addrlen);
  623. addr->sa_family = AF_INET;
  624. return 0;
  625. }
  626. /*------------------------------------------------------------------------------
  627. ------------------------------------ syscall() ---------------------------------
  628. ------------------------------------------------------------------------------*/
  629. long syscall(SYSCALL_SIG){
  630. //dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
  631. va_list ap;
  632. uintptr_t a,b,c,d,e,f;
  633. va_start(ap, number);
  634. a=va_arg(ap, uintptr_t);
  635. b=va_arg(ap, uintptr_t);
  636. c=va_arg(ap, uintptr_t);
  637. d=va_arg(ap, uintptr_t);
  638. e=va_arg(ap, uintptr_t);
  639. f=va_arg(ap, uintptr_t);
  640. va_end(ap);
  641. if(realsyscall == NULL)
  642. return -1;
  643. #if defined(__i386__)
  644. /* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
  645. args[0] = (unsigned long) fd;
  646. args[1] = (unsigned long) addr;
  647. args[2] = (unsigned long) addrlen;
  648. args[3] = (unsigned long) flags;
  649. */
  650. #else
  651. if(number == __NR_accept4) {
  652. int sockfd = a;
  653. struct sockaddr * addr = (struct sockaddr*)b;
  654. socklen_t * addrlen = (socklen_t*)c;
  655. int flags = d;
  656. int old_errno = errno;
  657. int err = accept4(sockfd, addr, addrlen, flags);
  658. errno = old_errno;
  659. err = err == -EBADF ? -EAGAIN : err;
  660. return err;
  661. }
  662. #endif
  663. return realsyscall(number,a,b,c,d,e,f);
  664. }