Intercept.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. /*------------------------------------------------------------------------------
  57. ------------------- Intercept<--->Service Comm mechanisms ----------------------
  58. ------------------------------------------------------------------------------*/
  59. static char *netpath = (char *)0;
  60. /* Check whether the socket is mapped to the service or not. We
  61. need to know if this is a regular AF_LOCAL socket or an end of a socketpair
  62. that the service uses. We don't want to keep state in the intercept, so
  63. we simply ask the service via an RPC */
  64. static int connected_to_service(int sockfd)
  65. {
  66. dwr(MSG_DEBUG_EXTRA,"connected_to_service():\n");
  67. socklen_t len;
  68. struct sockaddr_storage addr;
  69. len = sizeof addr;
  70. struct sockaddr_un * addr_un;
  71. getpeername(sockfd, (struct sockaddr*)&addr, &len);
  72. if (addr.ss_family == AF_LOCAL || addr.ss_family == AF_LOCAL) {
  73. addr_un = (struct sockaddr_un*)&addr;
  74. if(strcmp(addr_un->sun_path, netpath) == 0) {
  75. dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Yes, %s\n", addr_un->sun_path);
  76. return 1;
  77. }
  78. }
  79. dwr(MSG_DEBUG_EXTRA,"connected_to_service(): Not connected to service\n");
  80. return 0;
  81. }
  82. /*static void my_dest(void) __attribute__ ((destructor));
  83. static void my_dest(void) {
  84. dwr(MSG_DEBUG,"closing connections to service...\n");
  85. rpc_mutex_destroy();
  86. }*/
  87. /* Private Function Prototypes */
  88. /*static void _init(void) __attribute__ ((constructor));
  89. static void _init(void) { set_up_intercept(); } */
  90. /* get symbols and initialize mutexes */
  91. static int set_up_intercept()
  92. {
  93. if (!realconnect) {
  94. realconnect = dlsym(RTLD_NEXT, "connect");
  95. realbind = dlsym(RTLD_NEXT, "bind");
  96. realaccept = dlsym(RTLD_NEXT, "accept");
  97. reallisten = dlsym(RTLD_NEXT, "listen");
  98. realsocket = dlsym(RTLD_NEXT, "socket");
  99. realbind = dlsym(RTLD_NEXT, "bind");
  100. realsetsockopt = dlsym(RTLD_NEXT, "setsockopt");
  101. realgetsockopt = dlsym(RTLD_NEXT, "getsockopt");
  102. realaccept4 = dlsym(RTLD_NEXT, "accept4");
  103. realclose = dlsym(RTLD_NEXT, "close");
  104. realsyscall = dlsym(RTLD_NEXT, "syscall");
  105. realgetsockname = dlsym(RTLD_NEXT, "getsockname");
  106. }
  107. if (!netpath) {
  108. netpath = getenv("ZT_NC_NETWORK");
  109. if (!netpath)
  110. return 0;
  111. dwr(MSG_DEBUG,"Connecting to service at: %s\n", netpath);
  112. /* Hook/intercept Posix net API symbols */
  113. rpc_mutex_init();
  114. }
  115. return 1;
  116. }
  117. /*------------------------------------------------------------------------------
  118. --------------------------------- setsockopt() ---------------------------------
  119. ------------------------------------------------------------------------------*/
  120. /* int socket, int level, int option_name, const void *option_value, socklen_t option_len */
  121. int setsockopt(SETSOCKOPT_SIG)
  122. {
  123. if (!set_up_intercept())
  124. return realsetsockopt(socket, level, option_name, option_value, option_len);
  125. dwr(MSG_DEBUG,"setsockopt(%d)\n", socket);
  126. /* return(realsetsockopt(socket, level, option_name, option_value, option_len)); */
  127. if(level == SOL_IPV6 && option_name == IPV6_V6ONLY)
  128. return 0;
  129. if(level == SOL_IP && option_name == IP_TTL)
  130. return 0;
  131. if(level == IPPROTO_TCP || (level == SOL_SOCKET && option_name == SO_KEEPALIVE))
  132. return 0;
  133. /* make sure we don't touch any standard outputs */
  134. if(socket == STDIN_FILENO || socket == STDOUT_FILENO || socket == STDERR_FILENO)
  135. return(realsetsockopt(socket, level, option_name, option_value, option_len));
  136. int err = realsetsockopt(socket, level, option_name, option_value, option_len);
  137. if(err < 0)
  138. perror("setsockopt():\n");
  139. return 0;
  140. }
  141. /*------------------------------------------------------------------------------
  142. --------------------------------- getsockopt() ---------------------------------
  143. ------------------------------------------------------------------------------*/
  144. /* int sockfd, int level, int optname, void *optval, socklen_t *optlen */
  145. int getsockopt(GETSOCKOPT_SIG)
  146. {
  147. if (!set_up_intercept())
  148. return realgetsockopt(sockfd, level, optname, optval, optlen);
  149. dwr(MSG_DEBUG,"getsockopt(%d)\n", sockfd);
  150. if(!connected_to_service(sockfd)) {
  151. return realgetsockopt(sockfd, level, optname, optval, optlen);
  152. }
  153. if(optname == SO_TYPE) {
  154. int* val = (int*)optval;
  155. *val = 2;
  156. optval = (void*)val;
  157. }
  158. return 0;
  159. }
  160. /*------------------------------------------------------------------------------
  161. ----------------------------------- socket() -----------------------------------
  162. ------------------------------------------------------------------------------*/
  163. /* int socket_family, int socket_type, int protocol
  164. socket() intercept function */
  165. int socket(SOCKET_SIG)
  166. {
  167. if (!set_up_intercept())
  168. return realsocket(socket_family, socket_type, protocol);
  169. dwr(MSG_DEBUG,"socket():\n");
  170. /* Check that type makes sense */
  171. int flags = socket_type & ~SOCK_TYPE_MASK;
  172. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK)) {
  173. errno = EINVAL;
  174. return -1;
  175. }
  176. /*
  177. if(flags & SOCK_DGRAM) {
  178. fprintf(stderr, "socket(): DGRAM, passing through\n");
  179. return realsocket(socket_family, socket_type, protocol);
  180. }
  181. */
  182. socket_type &= SOCK_TYPE_MASK;
  183. /* Check protocol is in range */
  184. if (socket_family < 0 || socket_family >= NPROTO){
  185. errno = EAFNOSUPPORT;
  186. return -1;
  187. }
  188. if (socket_type < 0 || socket_type >= SOCK_MAX) {
  189. errno = EINVAL;
  190. return -1;
  191. }
  192. /* TODO: detect ENFILE condition */
  193. if(socket_family == AF_LOCAL
  194. || socket_family == AF_NETLINK
  195. || socket_family == AF_UNIX) {
  196. int err = realsocket(socket_family, socket_type, protocol);
  197. dwr(MSG_DEBUG,"realsocket() = %d\n", err);
  198. return err;
  199. }
  200. /* Assemble and send RPC */
  201. struct socket_st rpc_st;
  202. rpc_st.socket_family = socket_family;
  203. rpc_st.socket_type = socket_type;
  204. rpc_st.protocol = protocol;
  205. rpc_st.__tid = syscall(SYS_gettid);
  206. /* -1 is passed since we we're generating the new socket in this call */
  207. return rpc_send_command(netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
  208. }
  209. /*------------------------------------------------------------------------------
  210. ---------------------------------- connect() -----------------------------------
  211. ------------------------------------------------------------------------------*/
  212. /* int __fd, const struct sockaddr * __addr, socklen_t __len
  213. connect() intercept function */
  214. int connect(CONNECT_SIG)
  215. {
  216. if (!set_up_intercept())
  217. return realconnect(__fd, __addr, __len);
  218. /*
  219. int opt;
  220. socklen_t opt_len;
  221. realgetsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len);
  222. if(opt & SOCK_DGRAM)
  223. {
  224. fprintf(stderr, "connect(): DGRAM, passing through.\n");
  225. return realconnect(__fd, __addr, __len);
  226. }
  227. */
  228. struct sockaddr_in *connaddr;
  229. connaddr = (struct sockaddr_in *)__addr;
  230. if(__addr->sa_family == AF_LOCAL || __addr->sa_family == AF_UNIX) {
  231. struct sockaddr_storage storage;
  232. memcpy(&storage, __addr, __len);
  233. struct sockaddr_un *s_un = (struct sockaddr_un*)&storage;
  234. fprintf(stderr, "connect(): address = %s\n", s_un->sun_path);
  235. }
  236. int port = connaddr->sin_port;
  237. int ip = connaddr->sin_addr.s_addr;
  238. unsigned char d[4];
  239. d[0] = ip & 0xFF;
  240. d[1] = (ip >> 8) & 0xFF;
  241. d[2] = (ip >> 16) & 0xFF;
  242. d[3] = (ip >> 24) & 0xFF;
  243. dwr(MSG_DEBUG,"connect(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
  244. if (!set_up_intercept())
  245. return realconnect(__fd, __addr, __len);
  246. dwr(MSG_DEBUG,"connect(%d):\n", __fd);
  247. /* Check that this is a valid fd */
  248. if(fcntl(__fd, F_GETFD) < 0) {
  249. errno = EBADF;
  250. return -1;
  251. }
  252. /* Check that it is a socket */
  253. int sock_type;
  254. socklen_t sock_type_len = sizeof(sock_type);
  255. if(getsockopt(__fd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  256. errno = ENOTSOCK;
  257. return -1;
  258. }
  259. /* Check family */
  260. if (connaddr->sin_family < 0 || connaddr->sin_family >= NPROTO){
  261. errno = EAFNOSUPPORT;
  262. return -1;
  263. }
  264. /* make sure we don't touch any standard outputs */
  265. if(__fd == STDIN_FILENO || __fd == STDOUT_FILENO || __fd == STDERR_FILENO)
  266. return(realconnect(__fd, __addr, __len));
  267. if(__addr != NULL && (connaddr->sin_family == AF_LOCAL
  268. || connaddr->sin_family == PF_NETLINK
  269. || connaddr->sin_family == AF_NETLINK
  270. || connaddr->sin_family == AF_UNIX)) {
  271. return realconnect(__fd, __addr, __len);
  272. }
  273. /* Assemble and send RPC */
  274. struct connect_st rpc_st;
  275. rpc_st.__tid = syscall(SYS_gettid);
  276. rpc_st.__fd = __fd;
  277. memcpy(&rpc_st.__addr, __addr, sizeof(struct sockaddr_storage));
  278. memcpy(&rpc_st.__len, &__len, sizeof(socklen_t));
  279. return rpc_send_command(netpath, RPC_CONNECT, __fd, &rpc_st, sizeof(struct connect_st));
  280. }
  281. /*------------------------------------------------------------------------------
  282. ------------------------------------ bind() ------------------------------------
  283. ------------------------------------------------------------------------------*/
  284. /* int sockfd, const struct sockaddr *addr, socklen_t addrlen
  285. bind() intercept function */
  286. int bind(BIND_SIG)
  287. {
  288. if (!set_up_intercept())
  289. return realbind(sockfd, addr, addrlen);
  290. dwr(MSG_DEBUG,"bind(%d):\n", sockfd);
  291. /* Check that this is a valid fd */
  292. if(fcntl(sockfd, F_GETFD) < 0) {
  293. errno = EBADF;
  294. return -1;
  295. }
  296. /* Check that it is a socket */
  297. int opt = -1;
  298. socklen_t opt_len;
  299. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  300. errno = ENOTSOCK;
  301. return -1;
  302. }
  303. /* make sure we don't touch any standard outputs */
  304. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  305. return(realbind(sockfd, addr, addrlen));
  306. /* If local, just use normal syscall */
  307. struct sockaddr_in *connaddr;
  308. connaddr = (struct sockaddr_in *)addr;
  309. if(connaddr->sin_family == AF_LOCAL
  310. || connaddr->sin_family == AF_NETLINK
  311. || connaddr->sin_family == AF_UNIX) {
  312. int err = realbind(sockfd, addr, addrlen);
  313. dwr(MSG_DEBUG,"realbind, err = %d\n", err);
  314. return err;
  315. }
  316. int port = connaddr->sin_port;
  317. int ip = connaddr->sin_addr.s_addr;
  318. unsigned char d[4];
  319. d[0] = ip & 0xFF;
  320. d[1] = (ip >> 8) & 0xFF;
  321. d[2] = (ip >> 16) & 0xFF;
  322. d[3] = (ip >> 24) & 0xFF;
  323. dwr(MSG_DEBUG,"bind(): %d.%d.%d.%d: %d\n", d[0],d[1],d[2],d[3], ntohs(port));
  324. /* Assemble and send RPC */
  325. struct bind_st rpc_st;
  326. rpc_st.sockfd = sockfd;
  327. rpc_st.__tid = syscall(SYS_gettid);
  328. memcpy(&rpc_st.addr, addr, sizeof(struct sockaddr_storage));
  329. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  330. return rpc_send_command(netpath, RPC_BIND, sockfd, &rpc_st, sizeof(struct bind_st));
  331. }
  332. /*------------------------------------------------------------------------------
  333. ----------------------------------- accept4() ----------------------------------
  334. ------------------------------------------------------------------------------*/
  335. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags */
  336. int accept4(ACCEPT4_SIG)
  337. {
  338. dwr(MSG_DEBUG,"accept4(%d):\n", sockfd);
  339. if ((flags & SOCK_CLOEXEC))
  340. fcntl(sockfd, F_SETFL, FD_CLOEXEC);
  341. if ((flags & SOCK_NONBLOCK))
  342. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  343. return accept(sockfd, addr, addrlen);
  344. }
  345. /*------------------------------------------------------------------------------
  346. ----------------------------------- accept() -----------------------------------
  347. ------------------------------------------------------------------------------*/
  348. /* int sockfd struct sockaddr *addr, socklen_t *addrlen
  349. accept() intercept function */
  350. int accept(ACCEPT_SIG)
  351. {
  352. if (!set_up_intercept())
  353. return realaccept(sockfd, addr, addrlen);
  354. dwr(MSG_DEBUG,"accept(%d):\n", sockfd);
  355. /* Check that this is a valid fd */
  356. if(fcntl(sockfd, F_GETFD) < 0) {
  357. return -1;
  358. errno = EBADF;
  359. dwr(MSG_DEBUG,"EBADF\n");
  360. return -1;
  361. }
  362. /* Check that it is a socket */
  363. int opt;
  364. socklen_t opt_len;
  365. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &opt, &opt_len) < 0) {
  366. errno = ENOTSOCK;
  367. dwr(MSG_DEBUG,"ENOTSOCK\n");
  368. return -1;
  369. }
  370. /* Check that this socket supports accept() */
  371. if(!(opt && (SOCK_STREAM | SOCK_SEQPACKET))) {
  372. errno = EOPNOTSUPP;
  373. dwr(MSG_DEBUG,"EOPNOTSUPP\n");
  374. return -1;
  375. }
  376. /* Check that we haven't hit the soft-limit file descriptors allowed */
  377. struct rlimit rl;
  378. getrlimit(RLIMIT_NOFILE, &rl);
  379. if(sockfd >= rl.rlim_cur){
  380. errno = EMFILE;
  381. dwr(MSG_DEBUG,"EMFILE\n");
  382. return -1;
  383. }
  384. /* Check address length */
  385. if(addrlen < 0) {
  386. errno = EINVAL;
  387. dwr(MSG_DEBUG,"EINVAL\n");
  388. return -1;
  389. }
  390. /* redirect calls for standard I/O descriptors to kernel */
  391. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO){
  392. dwr(MSG_DEBUG,"realaccept():\n");
  393. return(realaccept(sockfd, addr, addrlen));
  394. }
  395. if(addr)
  396. addr->sa_family = AF_INET;
  397. int new_fd = get_new_fd(sockfd);
  398. if(new_fd > 0) {
  399. errno = ERR_OK;
  400. return new_fd;
  401. }
  402. errno = EAGAIN;
  403. return -EAGAIN;
  404. }
  405. /*------------------------------------------------------------------------------
  406. ------------------------------------- listen()----------------------------------
  407. ------------------------------------------------------------------------------*/
  408. /* int sockfd, int backlog */
  409. int listen(LISTEN_SIG)
  410. {
  411. if (!set_up_intercept())
  412. return(reallisten(sockfd, backlog));
  413. dwr(MSG_DEBUG,"listen(%d):\n", sockfd);
  414. int sock_type;
  415. socklen_t sock_type_len = sizeof(sock_type);
  416. /* Check that this is a valid fd */
  417. if(fcntl(sockfd, F_GETFD) < 0) {
  418. errno = EBADF;
  419. return -1;
  420. }
  421. /* Check that it is a socket */
  422. if(getsockopt(sockfd, SOL_SOCKET, SO_TYPE, (void *) &sock_type, &sock_type_len) < 0) {
  423. errno = ENOTSOCK;
  424. return -1;
  425. }
  426. /* Check that this socket supports accept() */
  427. if(!(sock_type && (SOCK_STREAM | SOCK_SEQPACKET))) {
  428. errno = EOPNOTSUPP;
  429. return -1;
  430. }
  431. /* make sure we don't touch any standard outputs */
  432. if(sockfd == STDIN_FILENO || sockfd == STDOUT_FILENO || sockfd == STDERR_FILENO)
  433. return(reallisten(sockfd, backlog));
  434. if(!connected_to_service(sockfd)) {
  435. reallisten(sockfd, backlog);
  436. }
  437. /* Assemble and send RPC */
  438. struct listen_st rpc_st;
  439. rpc_st.sockfd = sockfd;
  440. rpc_st.backlog = backlog;
  441. rpc_st.__tid = syscall(SYS_gettid);
  442. return rpc_send_command(netpath, RPC_LISTEN, sockfd, &rpc_st, sizeof(struct listen_st));
  443. }
  444. /*------------------------------------------------------------------------------
  445. ------------------------------------- close() ----------------------------------
  446. ------------------------------------------------------------------------------*/
  447. /* int fd */
  448. int close(CLOSE_SIG)
  449. {
  450. dwr(MSG_DEBUG, "close(%d)\n", fd);
  451. set_up_intercept();
  452. return realclose(fd);
  453. }
  454. /*------------------------------------------------------------------------------
  455. -------------------------------- getsockname() ---------------------------------
  456. ------------------------------------------------------------------------------*/
  457. /* int sockfd, struct sockaddr *addr, socklen_t *addrlen */
  458. int getsockname(GETSOCKNAME_SIG)
  459. {
  460. if (!set_up_intercept())
  461. return realgetsockname(sockfd, addr, addrlen);
  462. dwr(MSG_DEBUG,"getsockname(%d)\n", sockfd);
  463. if(connected_to_service(sockfd) == 0) {
  464. dwr(MSG_DEBUG,"getsockname(): not used by service\n");
  465. return realgetsockname(sockfd, addr, addrlen);
  466. }
  467. /* This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in
  468. * and is an IPv4 address. */
  469. /* assemble and send command */
  470. struct getsockname_st rpc_st;
  471. rpc_st.sockfd = sockfd;
  472. memcpy(&rpc_st.addr, addr, *addrlen);
  473. memcpy(&rpc_st.addrlen, &addrlen, sizeof(socklen_t));
  474. int rpcfd = rpc_send_command(netpath, RPC_GETSOCKNAME, sockfd, &rpc_st, sizeof(struct getsockname_st));
  475. /* read address info from service */
  476. char addrbuf[sizeof(struct sockaddr_storage)];
  477. memset(&addrbuf, 0, sizeof(struct sockaddr_storage));
  478. if(rpcfd > -1)
  479. if(read(rpcfd, &addrbuf, sizeof(struct sockaddr_storage)) > 0)
  480. close(rpcfd);
  481. struct sockaddr_storage sock_storage;
  482. memcpy(&sock_storage, addrbuf, sizeof(struct sockaddr_storage));
  483. *addrlen = sizeof(struct sockaddr_in);
  484. memcpy(addr, &sock_storage, (*addrlen > sizeof(sock_storage)) ? sizeof(sock_storage) : *addrlen);
  485. addr->sa_family = AF_INET;
  486. return 0;
  487. }
  488. /*------------------------------------------------------------------------------
  489. ------------------------------------ syscall() ---------------------------------
  490. ------------------------------------------------------------------------------*/
  491. long syscall(SYSCALL_SIG)
  492. {
  493. va_list ap;
  494. uintptr_t a,b,c,d,e,f;
  495. va_start(ap, number);
  496. a=va_arg(ap, uintptr_t);
  497. b=va_arg(ap, uintptr_t);
  498. c=va_arg(ap, uintptr_t);
  499. d=va_arg(ap, uintptr_t);
  500. e=va_arg(ap, uintptr_t);
  501. f=va_arg(ap, uintptr_t);
  502. va_end(ap);
  503. if (!set_up_intercept())
  504. return realsyscall(number,a,b,c,d,e,f);
  505. dwr(MSG_DEBUG_EXTRA,"syscall(%u, ...):\n", number);
  506. #if defined(__i386__)
  507. /* TODO: Implement for 32-bit systems: syscall(__NR_socketcall, 18, args);
  508. args[0] = (unsigned long) fd;
  509. args[1] = (unsigned long) addr;
  510. args[2] = (unsigned long) addrlen;
  511. args[3] = (unsigned long) flags;
  512. */
  513. #else
  514. if(number == __NR_accept4) {
  515. int sockfd = a;
  516. struct sockaddr * addr = (struct sockaddr*)b;
  517. socklen_t * addrlen = (socklen_t*)c;
  518. int flags = d;
  519. int old_errno = errno;
  520. int err = accept4(sockfd, addr, addrlen, flags);
  521. errno = old_errno;
  522. err = err == -EBADF ? -EAGAIN : err;
  523. return err;
  524. }
  525. #endif
  526. return realsyscall(number,a,b,c,d,e,f);
  527. }