Intercept.c 23 KB

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