src/socket_wrapper.c: split out _swrap_bind_symbol_generic()
[socket_wrapper.git] / src / socket_wrapper.c
1 /*
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2005-2008, Jelmer Vernooij <jelmer@samba.org>
5  * Copyright (c) 2006-2018, Stefan Metzmacher <metze@samba.org>
6  * Copyright (c) 2013-2018, Andreas Schneider <asn@samba.org>
7  * Copyright (c) 2014-2017, Michael Adam <obnox@samba.org>
8  * Copyright (c) 2016-2018, Anoop C S <anoopcs@redhat.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * 3. Neither the name of the author nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 /*
40    Socket wrapper library. Passes all socket communication over
41    unix domain sockets if the environment variable SOCKET_WRAPPER_DIR
42    is set.
43 */
44
45 #include "config.h"
46
47 #include <sys/types.h>
48 #include <sys/time.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <sys/ioctl.h>
52 #ifdef HAVE_SYS_FILIO_H
53 #include <sys/filio.h>
54 #endif
55 #ifdef HAVE_SYS_SIGNALFD_H
56 #include <sys/signalfd.h>
57 #endif
58 #ifdef HAVE_SYS_EVENTFD_H
59 #include <sys/eventfd.h>
60 #endif
61 #ifdef HAVE_SYS_TIMERFD_H
62 #include <sys/timerfd.h>
63 #endif
64 #include <sys/uio.h>
65 #include <errno.h>
66 #include <sys/un.h>
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 #ifdef HAVE_NETINET_TCP_FSM_H
70 #include <netinet/tcp_fsm.h>
71 #endif
72 #include <arpa/inet.h>
73 #include <fcntl.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <stdio.h>
77 #include <stdint.h>
78 #include <stdarg.h>
79 #include <stdbool.h>
80 #include <unistd.h>
81 #ifdef HAVE_GNU_LIB_NAMES_H
82 #include <gnu/lib-names.h>
83 #endif
84 #ifdef HAVE_RPC_RPC_H
85 #include <rpc/rpc.h>
86 #endif
87 #include <pthread.h>
88
89 enum swrap_dbglvl_e {
90         SWRAP_LOG_ERROR = 0,
91         SWRAP_LOG_WARN,
92         SWRAP_LOG_DEBUG,
93         SWRAP_LOG_TRACE
94 };
95
96 /* GCC have printf type attribute check. */
97 #ifdef HAVE_FUNCTION_ATTRIBUTE_FORMAT
98 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
99 #else
100 #define PRINTF_ATTRIBUTE(a,b)
101 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
102
103 #ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
104 #define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
105 #else
106 #define CONSTRUCTOR_ATTRIBUTE
107 #endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
108
109 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
110 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
111 #else
112 #define DESTRUCTOR_ATTRIBUTE
113 #endif
114
115 #ifndef FALL_THROUGH
116 # ifdef HAVE_FALLTHROUGH_ATTRIBUTE
117 #  define FALL_THROUGH __attribute__ ((fallthrough))
118 # else /* HAVE_FALLTHROUGH_ATTRIBUTE */
119 #  define FALL_THROUGH ((void)0)
120 # endif /* HAVE_FALLTHROUGH_ATTRIBUTE */
121 #endif /* FALL_THROUGH */
122
123 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
124 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
125 #else
126 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
127 #endif
128
129 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
130 # define SWRAP_THREAD __thread
131 #else
132 # define SWRAP_THREAD
133 #endif
134
135 #ifndef MIN
136 #define MIN(a,b) ((a)<(b)?(a):(b))
137 #endif
138
139 #ifndef ZERO_STRUCT
140 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
141 #endif
142
143 #ifndef ZERO_STRUCTP
144 #define ZERO_STRUCTP(x) do { \
145                 if ((x) != NULL) \
146                         memset((char *)(x), 0, sizeof(*(x))); \
147         } while(0)
148 #endif
149
150 #ifndef SAFE_FREE
151 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
152 #endif
153
154 #ifndef discard_const
155 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
156 #endif
157
158 #ifndef discard_const_p
159 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
160 #endif
161
162 #define UNUSED(x) (void)(x)
163
164 #ifdef IPV6_PKTINFO
165 # ifndef IPV6_RECVPKTINFO
166 #  define IPV6_RECVPKTINFO IPV6_PKTINFO
167 # endif /* IPV6_RECVPKTINFO */
168 #endif /* IPV6_PKTINFO */
169
170 /*
171  * On BSD IP_PKTINFO has a different name because during
172  * the time when they implemented it, there was no RFC.
173  * The name for IPv6 is the same as on Linux.
174  */
175 #ifndef IP_PKTINFO
176 # ifdef IP_RECVDSTADDR
177 #  define IP_PKTINFO IP_RECVDSTADDR
178 # endif
179 #endif
180
181 /* Add new global locks here please */
182 # define SWRAP_LOCK_ALL \
183         swrap_mutex_lock(&libc_symbol_binding_mutex); \
184
185 # define SWRAP_UNLOCK_ALL \
186         swrap_mutex_unlock(&libc_symbol_binding_mutex); \
187
188 #define SOCKET_INFO_CONTAINER(si) \
189         (struct socket_info_container *)(si)
190
191 #define SWRAP_LOCK_SI(si) do { \
192         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); \
193         swrap_mutex_lock(&sic->meta.mutex); \
194 } while(0)
195
196 #define SWRAP_UNLOCK_SI(si) do { \
197         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); \
198         swrap_mutex_unlock(&sic->meta.mutex); \
199 } while(0)
200
201 #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID)
202 #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL)
203 #else
204 #define swrapGetTimeOfDay(tval) gettimeofday(tval)
205 #endif
206
207 /* we need to use a very terse format here as IRIX 6.4 silently
208    truncates names to 16 chars, so if we use a longer name then we
209    can't tell which port a packet came from with recvfrom()
210
211    with this format we have 8 chars left for the directory name
212 */
213 #define SOCKET_FORMAT "%c%02X%04X"
214 #define SOCKET_TYPE_CHAR_TCP            'T'
215 #define SOCKET_TYPE_CHAR_UDP            'U'
216 #define SOCKET_TYPE_CHAR_TCP_V6         'X'
217 #define SOCKET_TYPE_CHAR_UDP_V6         'Y'
218
219 /*
220  * Set the packet MTU to 1500 bytes for stream sockets to make it it easier to
221  * format PCAP capture files (as the caller will simply continue from here).
222  */
223 #define SOCKET_WRAPPER_MTU_DEFAULT 1500
224 #define SOCKET_WRAPPER_MTU_MIN     512
225 #define SOCKET_WRAPPER_MTU_MAX     32768
226
227 #define SOCKET_MAX_SOCKETS 1024
228
229 /*
230  * Maximum number of socket_info structures that can
231  * be used. Can be overriden by the environment variable
232  * SOCKET_WRAPPER_MAX_SOCKETS.
233  */
234 #define SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT 65535
235
236 #define SOCKET_WRAPPER_MAX_SOCKETS_LIMIT 262140
237
238 /* This limit is to avoid broadcast sendto() needing to stat too many
239  * files.  It may be raised (with a performance cost) to up to 254
240  * without changing the format above */
241 #define MAX_WRAPPED_INTERFACES 64
242
243 struct swrap_address {
244         socklen_t sa_socklen;
245         union {
246                 struct sockaddr s;
247                 struct sockaddr_in in;
248 #ifdef HAVE_IPV6
249                 struct sockaddr_in6 in6;
250 #endif
251                 struct sockaddr_un un;
252                 struct sockaddr_storage ss;
253         } sa;
254 };
255
256 int first_free;
257
258 struct socket_info
259 {
260         int family;
261         int type;
262         int protocol;
263         int bound;
264         int bcast;
265         int is_server;
266         int connected;
267         int defer_connect;
268         int pktinfo;
269         int tcp_nodelay;
270         int listening;
271
272         /* The unix path so we can unlink it on close() */
273         struct sockaddr_un un_addr;
274
275         struct swrap_address bindname;
276         struct swrap_address myname;
277         struct swrap_address peername;
278
279         struct {
280                 unsigned long pck_snd;
281                 unsigned long pck_rcv;
282         } io;
283 };
284
285 struct socket_info_meta
286 {
287         unsigned int refcount;
288         int next_free;
289         pthread_mutex_t mutex;
290 };
291
292 struct socket_info_container
293 {
294         struct socket_info info;
295         struct socket_info_meta meta;
296 };
297
298 static struct socket_info_container *sockets;
299
300 static size_t socket_info_max = 0;
301
302 /*
303  * Allocate the socket array always on the limit value. We want it to be
304  * at least bigger than the default so if we reach the limit we can
305  * still deal with duplicate fds pointing to the same socket_info.
306  */
307 static size_t socket_fds_max = SOCKET_WRAPPER_MAX_SOCKETS_LIMIT;
308
309 /* Hash table to map fds to corresponding socket_info index */
310 static int *socket_fds_idx;
311
312 /* Mutex to synchronize access to global libc.symbols */
313 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
314
315 /* Mutex for syncronizing port selection during swrap_auto_bind() */
316 static pthread_mutex_t autobind_start_mutex;
317
318 /* Mutex to guard the initialization of array of socket_info structures */
319 static pthread_mutex_t sockets_mutex;
320
321 /* Mutex to guard the socket reset in swrap_close() and swrap_remove_stale() */
322 static pthread_mutex_t socket_reset_mutex;
323
324 /* Mutex to synchronize access to first free index in socket_info array */
325 static pthread_mutex_t first_free_mutex;
326
327 /* Mutex to synchronize access to packet capture dump file */
328 static pthread_mutex_t pcap_dump_mutex;
329
330 /* Mutex for synchronizing mtu value fetch*/
331 static pthread_mutex_t mtu_update_mutex;
332
333 /* Function prototypes */
334
335 bool socket_wrapper_enabled(void);
336
337 #if ! defined(HAVE_CONSTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_INIT)
338 /* xlC and other oldschool compilers support (only) this */
339 #pragma init (swrap_constructor)
340 #endif
341 void swrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
342 #if ! defined(HAVE_DESTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_FINI)
343 #pragma fini (swrap_destructor)
344 #endif
345 void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
346
347 #ifndef HAVE_GETPROGNAME
348 static const char *getprogname(void)
349 {
350 #if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
351         return program_invocation_short_name;
352 #elif defined(HAVE_GETEXECNAME)
353         return getexecname();
354 #else
355         return NULL;
356 #endif /* HAVE_PROGRAM_INVOCATION_SHORT_NAME */
357 }
358 #endif /* HAVE_GETPROGNAME */
359
360 static void swrap_log(enum swrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
361 # define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)
362
363 static void swrap_log(enum swrap_dbglvl_e dbglvl,
364                       const char *func,
365                       const char *format, ...)
366 {
367         char buffer[1024];
368         va_list va;
369         const char *d;
370         unsigned int lvl = 0;
371         const char *prefix = "SWRAP";
372         const char *progname = getprogname();
373
374         d = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
375         if (d != NULL) {
376                 lvl = atoi(d);
377         }
378
379         if (lvl < dbglvl) {
380                 return;
381         }
382
383         va_start(va, format);
384         vsnprintf(buffer, sizeof(buffer), format, va);
385         va_end(va);
386
387         switch (dbglvl) {
388                 case SWRAP_LOG_ERROR:
389                         prefix = "SWRAP_ERROR";
390                         break;
391                 case SWRAP_LOG_WARN:
392                         prefix = "SWRAP_WARN";
393                         break;
394                 case SWRAP_LOG_DEBUG:
395                         prefix = "SWRAP_DEBUG";
396                         break;
397                 case SWRAP_LOG_TRACE:
398                         prefix = "SWRAP_TRACE";
399                         break;
400         }
401
402         if (progname == NULL) {
403                 progname = "<unknown>";
404         }
405
406         fprintf(stderr,
407                 "%s[%s (%u)] - %s: %s\n",
408                 prefix,
409                 progname,
410                 (unsigned int)getpid(),
411                 func,
412                 buffer);
413 }
414
415 /*********************************************************
416  * SWRAP LOADING LIBC FUNCTIONS
417  *********************************************************/
418
419 #include <dlfcn.h>
420
421 #ifdef HAVE_ACCEPT4
422 typedef int (*__libc_accept4)(int sockfd,
423                               struct sockaddr *addr,
424                               socklen_t *addrlen,
425                               int flags);
426 #else
427 typedef int (*__libc_accept)(int sockfd,
428                              struct sockaddr *addr,
429                              socklen_t *addrlen);
430 #endif
431 typedef int (*__libc_bind)(int sockfd,
432                            const struct sockaddr *addr,
433                            socklen_t addrlen);
434 typedef int (*__libc_close)(int fd);
435 typedef int (*__libc_connect)(int sockfd,
436                               const struct sockaddr *addr,
437                               socklen_t addrlen);
438 typedef int (*__libc_dup)(int fd);
439 typedef int (*__libc_dup2)(int oldfd, int newfd);
440 typedef int (*__libc_fcntl)(int fd, int cmd, ...);
441 typedef FILE *(*__libc_fopen)(const char *name, const char *mode);
442 #ifdef HAVE_FOPEN64
443 typedef FILE *(*__libc_fopen64)(const char *name, const char *mode);
444 #endif
445 #ifdef HAVE_EVENTFD
446 typedef int (*__libc_eventfd)(int count, int flags);
447 #endif
448 typedef int (*__libc_getpeername)(int sockfd,
449                                   struct sockaddr *addr,
450                                   socklen_t *addrlen);
451 typedef int (*__libc_getsockname)(int sockfd,
452                                   struct sockaddr *addr,
453                                   socklen_t *addrlen);
454 typedef int (*__libc_getsockopt)(int sockfd,
455                                int level,
456                                int optname,
457                                void *optval,
458                                socklen_t *optlen);
459 typedef int (*__libc_ioctl)(int d, unsigned long int request, ...);
460 typedef int (*__libc_listen)(int sockfd, int backlog);
461 typedef int (*__libc_open)(const char *pathname, int flags, ...);
462 #ifdef HAVE_OPEN64
463 typedef int (*__libc_open64)(const char *pathname, int flags, ...);
464 #endif /* HAVE_OPEN64 */
465 typedef int (*__libc_openat)(int dirfd, const char *path, int flags, ...);
466 typedef int (*__libc_pipe)(int pipefd[2]);
467 typedef int (*__libc_read)(int fd, void *buf, size_t count);
468 typedef ssize_t (*__libc_readv)(int fd, const struct iovec *iov, int iovcnt);
469 typedef int (*__libc_recv)(int sockfd, void *buf, size_t len, int flags);
470 typedef int (*__libc_recvfrom)(int sockfd,
471                              void *buf,
472                              size_t len,
473                              int flags,
474                              struct sockaddr *src_addr,
475                              socklen_t *addrlen);
476 typedef int (*__libc_recvmsg)(int sockfd, const struct msghdr *msg, int flags);
477 typedef int (*__libc_send)(int sockfd, const void *buf, size_t len, int flags);
478 typedef int (*__libc_sendmsg)(int sockfd, const struct msghdr *msg, int flags);
479 typedef int (*__libc_sendto)(int sockfd,
480                            const void *buf,
481                            size_t len,
482                            int flags,
483                            const  struct sockaddr *dst_addr,
484                            socklen_t addrlen);
485 typedef int (*__libc_setsockopt)(int sockfd,
486                                int level,
487                                int optname,
488                                const void *optval,
489                                socklen_t optlen);
490 #ifdef HAVE_SIGNALFD
491 typedef int (*__libc_signalfd)(int fd, const sigset_t *mask, int flags);
492 #endif
493 typedef int (*__libc_socket)(int domain, int type, int protocol);
494 typedef int (*__libc_socketpair)(int domain, int type, int protocol, int sv[2]);
495 #ifdef HAVE_TIMERFD_CREATE
496 typedef int (*__libc_timerfd_create)(int clockid, int flags);
497 #endif
498 typedef ssize_t (*__libc_write)(int fd, const void *buf, size_t count);
499 typedef ssize_t (*__libc_writev)(int fd, const struct iovec *iov, int iovcnt);
500
501 #define SWRAP_SYMBOL_ENTRY(i) \
502         union { \
503                 __libc_##i f; \
504                 void *obj; \
505         } _libc_##i
506
507 struct swrap_libc_symbols {
508 #ifdef HAVE_ACCEPT4
509         SWRAP_SYMBOL_ENTRY(accept4);
510 #else
511         SWRAP_SYMBOL_ENTRY(accept);
512 #endif
513         SWRAP_SYMBOL_ENTRY(bind);
514         SWRAP_SYMBOL_ENTRY(close);
515         SWRAP_SYMBOL_ENTRY(connect);
516         SWRAP_SYMBOL_ENTRY(dup);
517         SWRAP_SYMBOL_ENTRY(dup2);
518         SWRAP_SYMBOL_ENTRY(fcntl);
519         SWRAP_SYMBOL_ENTRY(fopen);
520 #ifdef HAVE_FOPEN64
521         SWRAP_SYMBOL_ENTRY(fopen64);
522 #endif
523 #ifdef HAVE_EVENTFD
524         SWRAP_SYMBOL_ENTRY(eventfd);
525 #endif
526         SWRAP_SYMBOL_ENTRY(getpeername);
527         SWRAP_SYMBOL_ENTRY(getsockname);
528         SWRAP_SYMBOL_ENTRY(getsockopt);
529         SWRAP_SYMBOL_ENTRY(ioctl);
530         SWRAP_SYMBOL_ENTRY(listen);
531         SWRAP_SYMBOL_ENTRY(open);
532 #ifdef HAVE_OPEN64
533         SWRAP_SYMBOL_ENTRY(open64);
534 #endif
535         SWRAP_SYMBOL_ENTRY(openat);
536         SWRAP_SYMBOL_ENTRY(pipe);
537         SWRAP_SYMBOL_ENTRY(read);
538         SWRAP_SYMBOL_ENTRY(readv);
539         SWRAP_SYMBOL_ENTRY(recv);
540         SWRAP_SYMBOL_ENTRY(recvfrom);
541         SWRAP_SYMBOL_ENTRY(recvmsg);
542         SWRAP_SYMBOL_ENTRY(send);
543         SWRAP_SYMBOL_ENTRY(sendmsg);
544         SWRAP_SYMBOL_ENTRY(sendto);
545         SWRAP_SYMBOL_ENTRY(setsockopt);
546 #ifdef HAVE_SIGNALFD
547         SWRAP_SYMBOL_ENTRY(signalfd);
548 #endif
549         SWRAP_SYMBOL_ENTRY(socket);
550         SWRAP_SYMBOL_ENTRY(socketpair);
551 #ifdef HAVE_TIMERFD_CREATE
552         SWRAP_SYMBOL_ENTRY(timerfd_create);
553 #endif
554         SWRAP_SYMBOL_ENTRY(write);
555         SWRAP_SYMBOL_ENTRY(writev);
556 };
557
558 struct swrap {
559         struct {
560                 void *handle;
561                 void *socket_handle;
562                 struct swrap_libc_symbols symbols;
563         } libc;
564 };
565
566 static struct swrap swrap;
567
568 /* prototypes */
569 static char *socket_wrapper_dir(void);
570
571 #define LIBC_NAME "libc.so"
572
573 enum swrap_lib {
574     SWRAP_LIBC,
575     SWRAP_LIBSOCKET,
576 };
577
578 static const char *swrap_str_lib(enum swrap_lib lib)
579 {
580         switch (lib) {
581         case SWRAP_LIBC:
582                 return "libc";
583         case SWRAP_LIBSOCKET:
584                 return "libsocket";
585         }
586
587         /* Compiler would warn us about unhandled enum value if we get here */
588         return "unknown";
589 }
590
591 static void *swrap_load_lib_handle(enum swrap_lib lib)
592 {
593         int flags = RTLD_LAZY;
594         void *handle = NULL;
595         int i;
596
597 #ifdef RTLD_DEEPBIND
598         const char *env_preload = getenv("LD_PRELOAD");
599         const char *env_deepbind = getenv("SOCKET_WRAPPER_DISABLE_DEEPBIND");
600         bool enable_deepbind = true;
601
602         /* Don't do a deepbind if we run with libasan */
603         if (env_preload != NULL && strlen(env_preload) < 1024) {
604                 const char *p = strstr(env_preload, "libasan.so");
605                 if (p != NULL) {
606                         enable_deepbind = false;
607                 }
608         }
609
610         if (env_deepbind != NULL && strlen(env_deepbind) >= 1) {
611                 enable_deepbind = false;
612         }
613
614         if (enable_deepbind) {
615                 flags |= RTLD_DEEPBIND;
616         }
617 #endif
618
619         switch (lib) {
620         case SWRAP_LIBSOCKET:
621 #ifdef HAVE_LIBSOCKET
622                 handle = swrap.libc.socket_handle;
623                 if (handle == NULL) {
624                         for (i = 10; i >= 0; i--) {
625                                 char soname[256] = {0};
626
627                                 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
628                                 handle = dlopen(soname, flags);
629                                 if (handle != NULL) {
630                                         break;
631                                 }
632                         }
633
634                         swrap.libc.socket_handle = handle;
635                 }
636                 break;
637 #endif
638         case SWRAP_LIBC:
639                 handle = swrap.libc.handle;
640 #ifdef LIBC_SO
641                 if (handle == NULL) {
642                         handle = dlopen(LIBC_SO, flags);
643
644                         swrap.libc.handle = handle;
645                 }
646 #endif
647                 if (handle == NULL) {
648                         for (i = 10; i >= 0; i--) {
649                                 char soname[256] = {0};
650
651                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
652                                 handle = dlopen(soname, flags);
653                                 if (handle != NULL) {
654                                         break;
655                                 }
656                         }
657
658                         swrap.libc.handle = handle;
659                 }
660                 break;
661         }
662
663         if (handle == NULL) {
664 #ifdef RTLD_NEXT
665                 handle = swrap.libc.handle = swrap.libc.socket_handle = RTLD_NEXT;
666 #else
667                 SWRAP_LOG(SWRAP_LOG_ERROR,
668                           "Failed to dlopen library: %s",
669                           dlerror());
670                 exit(-1);
671 #endif
672         }
673
674         return handle;
675 }
676
677 static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
678 {
679         void *handle;
680         void *func;
681
682         handle = swrap_load_lib_handle(lib);
683
684         func = dlsym(handle, fn_name);
685         if (func == NULL) {
686                 SWRAP_LOG(SWRAP_LOG_ERROR,
687                           "Failed to find %s: %s",
688                           fn_name,
689                           dlerror());
690                 exit(-1);
691         }
692
693         SWRAP_LOG(SWRAP_LOG_TRACE,
694                   "Loaded %s from %s",
695                   fn_name,
696                   swrap_str_lib(lib));
697
698         return func;
699 }
700
701 static void swrap_mutex_lock(pthread_mutex_t *mutex)
702 {
703         int ret;
704
705         ret = pthread_mutex_lock(mutex);
706         if (ret != 0) {
707                 SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't lock pthread mutex - %s",
708                           strerror(ret));
709         }
710 }
711
712 static void swrap_mutex_unlock(pthread_mutex_t *mutex)
713 {
714         int ret;
715
716         ret = pthread_mutex_unlock(mutex);
717         if (ret != 0) {
718                 SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't unlock pthread mutex - %s",
719                           strerror(ret));
720         }
721 }
722
723 /*
724  * These macros have a thread race condition on purpose!
725  *
726  * This is an optimization to avoid locking each time we check if the symbol is
727  * bound.
728  */
729 #define _swrap_bind_symbol_generic(lib, sym_name) \
730         if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
731                 swrap_mutex_lock(&libc_symbol_binding_mutex); \
732                 if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
733                         swrap.libc.symbols._libc_##sym_name.obj = \
734                                 _swrap_bind_symbol(lib, #sym_name); \
735                 } \
736                 swrap_mutex_unlock(&libc_symbol_binding_mutex); \
737         }
738
739 #define swrap_bind_symbol_libc(sym_name) \
740         _swrap_bind_symbol_generic(SWRAP_LIBC, sym_name)
741
742 #define swrap_bind_symbol_libsocket(sym_name) \
743         _swrap_bind_symbol_generic(SWRAP_LIBSOCKET, sym_name)
744
745 /****************************************************************************
746  *                               IMPORTANT
747  ****************************************************************************
748  *
749  * Functions especially from libc need to be loaded individually, you can't
750  * load all at once or gdb will segfault at startup. The same applies to
751  * valgrind and has probably something todo with with the linker.  So we need
752  * load each function at the point it is called the first time.
753  *
754  ****************************************************************************/
755
756 #ifdef HAVE_ACCEPT4
757 static int libc_accept4(int sockfd,
758                         struct sockaddr *addr,
759                         socklen_t *addrlen,
760                         int flags)
761 {
762         swrap_bind_symbol_libsocket(accept4);
763
764         return swrap.libc.symbols._libc_accept4.f(sockfd, addr, addrlen, flags);
765 }
766
767 #else /* HAVE_ACCEPT4 */
768
769 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
770 {
771         swrap_bind_symbol_libsocket(accept);
772
773         return swrap.libc.symbols._libc_accept.f(sockfd, addr, addrlen);
774 }
775 #endif /* HAVE_ACCEPT4 */
776
777 static int libc_bind(int sockfd,
778                      const struct sockaddr *addr,
779                      socklen_t addrlen)
780 {
781         swrap_bind_symbol_libsocket(bind);
782
783         return swrap.libc.symbols._libc_bind.f(sockfd, addr, addrlen);
784 }
785
786 static int libc_close(int fd)
787 {
788         swrap_bind_symbol_libc(close);
789
790         return swrap.libc.symbols._libc_close.f(fd);
791 }
792
793 static int libc_connect(int sockfd,
794                         const struct sockaddr *addr,
795                         socklen_t addrlen)
796 {
797         swrap_bind_symbol_libsocket(connect);
798
799         return swrap.libc.symbols._libc_connect.f(sockfd, addr, addrlen);
800 }
801
802 static int libc_dup(int fd)
803 {
804         swrap_bind_symbol_libc(dup);
805
806         return swrap.libc.symbols._libc_dup.f(fd);
807 }
808
809 static int libc_dup2(int oldfd, int newfd)
810 {
811         swrap_bind_symbol_libc(dup2);
812
813         return swrap.libc.symbols._libc_dup2.f(oldfd, newfd);
814 }
815
816 #ifdef HAVE_EVENTFD
817 static int libc_eventfd(int count, int flags)
818 {
819         swrap_bind_symbol_libc(eventfd);
820
821         return swrap.libc.symbols._libc_eventfd.f(count, flags);
822 }
823 #endif
824
825 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
826 static int libc_vfcntl(int fd, int cmd, va_list ap)
827 {
828         void *arg;
829         int rc;
830
831         swrap_bind_symbol_libc(fcntl);
832
833         arg = va_arg(ap, void *);
834
835         rc = swrap.libc.symbols._libc_fcntl.f(fd, cmd, arg);
836
837         return rc;
838 }
839
840 static int libc_getpeername(int sockfd,
841                             struct sockaddr *addr,
842                             socklen_t *addrlen)
843 {
844         swrap_bind_symbol_libsocket(getpeername);
845
846         return swrap.libc.symbols._libc_getpeername.f(sockfd, addr, addrlen);
847 }
848
849 static int libc_getsockname(int sockfd,
850                             struct sockaddr *addr,
851                             socklen_t *addrlen)
852 {
853         swrap_bind_symbol_libsocket(getsockname);
854
855         return swrap.libc.symbols._libc_getsockname.f(sockfd, addr, addrlen);
856 }
857
858 static int libc_getsockopt(int sockfd,
859                            int level,
860                            int optname,
861                            void *optval,
862                            socklen_t *optlen)
863 {
864         swrap_bind_symbol_libsocket(getsockopt);
865
866         return swrap.libc.symbols._libc_getsockopt.f(sockfd,
867                                                      level,
868                                                      optname,
869                                                      optval,
870                                                      optlen);
871 }
872
873 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
874 static int libc_vioctl(int d, unsigned long int request, va_list ap)
875 {
876         void *arg;
877         int rc;
878
879         swrap_bind_symbol_libc(ioctl);
880
881         arg = va_arg(ap, void *);
882
883         rc = swrap.libc.symbols._libc_ioctl.f(d, request, arg);
884
885         return rc;
886 }
887
888 static int libc_listen(int sockfd, int backlog)
889 {
890         swrap_bind_symbol_libsocket(listen);
891
892         return swrap.libc.symbols._libc_listen.f(sockfd, backlog);
893 }
894
895 static FILE *libc_fopen(const char *name, const char *mode)
896 {
897         swrap_bind_symbol_libc(fopen);
898
899         return swrap.libc.symbols._libc_fopen.f(name, mode);
900 }
901
902 #ifdef HAVE_FOPEN64
903 static FILE *libc_fopen64(const char *name, const char *mode)
904 {
905         swrap_bind_symbol_libc(fopen64);
906
907         return swrap.libc.symbols._libc_fopen64.f(name, mode);
908 }
909 #endif /* HAVE_FOPEN64 */
910
911 static int libc_vopen(const char *pathname, int flags, va_list ap)
912 {
913         int mode = 0;
914         int fd;
915
916         swrap_bind_symbol_libc(open);
917
918         if (flags & O_CREAT) {
919                 mode = va_arg(ap, int);
920         }
921         fd = swrap.libc.symbols._libc_open.f(pathname, flags, (mode_t)mode);
922
923         return fd;
924 }
925
926 static int libc_open(const char *pathname, int flags, ...)
927 {
928         va_list ap;
929         int fd;
930
931         va_start(ap, flags);
932         fd = libc_vopen(pathname, flags, ap);
933         va_end(ap);
934
935         return fd;
936 }
937
938 #ifdef HAVE_OPEN64
939 static int libc_vopen64(const char *pathname, int flags, va_list ap)
940 {
941         int mode = 0;
942         int fd;
943
944         swrap_bind_symbol_libc(open64);
945
946         if (flags & O_CREAT) {
947                 mode = va_arg(ap, int);
948         }
949         fd = swrap.libc.symbols._libc_open64.f(pathname, flags, (mode_t)mode);
950
951         return fd;
952 }
953 #endif /* HAVE_OPEN64 */
954
955 static int libc_vopenat(int dirfd, const char *path, int flags, va_list ap)
956 {
957         int mode = 0;
958         int fd;
959
960         swrap_bind_symbol_libc(openat);
961
962         if (flags & O_CREAT) {
963                 mode = va_arg(ap, int);
964         }
965         fd = swrap.libc.symbols._libc_openat.f(dirfd,
966                                                path,
967                                                flags,
968                                                (mode_t)mode);
969
970         return fd;
971 }
972
973 #if 0
974 static int libc_openat(int dirfd, const char *path, int flags, ...)
975 {
976         va_list ap;
977         int fd;
978
979         va_start(ap, flags);
980         fd = libc_vopenat(dirfd, path, flags, ap);
981         va_end(ap);
982
983         return fd;
984 }
985 #endif
986
987 static int libc_pipe(int pipefd[2])
988 {
989         swrap_bind_symbol_libsocket(pipe);
990
991         return swrap.libc.symbols._libc_pipe.f(pipefd);
992 }
993
994 static int libc_read(int fd, void *buf, size_t count)
995 {
996         swrap_bind_symbol_libc(read);
997
998         return swrap.libc.symbols._libc_read.f(fd, buf, count);
999 }
1000
1001 static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
1002 {
1003         swrap_bind_symbol_libsocket(readv);
1004
1005         return swrap.libc.symbols._libc_readv.f(fd, iov, iovcnt);
1006 }
1007
1008 static int libc_recv(int sockfd, void *buf, size_t len, int flags)
1009 {
1010         swrap_bind_symbol_libsocket(recv);
1011
1012         return swrap.libc.symbols._libc_recv.f(sockfd, buf, len, flags);
1013 }
1014
1015 static int libc_recvfrom(int sockfd,
1016                          void *buf,
1017                          size_t len,
1018                          int flags,
1019                          struct sockaddr *src_addr,
1020                          socklen_t *addrlen)
1021 {
1022         swrap_bind_symbol_libsocket(recvfrom);
1023
1024         return swrap.libc.symbols._libc_recvfrom.f(sockfd,
1025                                                    buf,
1026                                                    len,
1027                                                    flags,
1028                                                    src_addr,
1029                                                    addrlen);
1030 }
1031
1032 static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
1033 {
1034         swrap_bind_symbol_libsocket(recvmsg);
1035
1036         return swrap.libc.symbols._libc_recvmsg.f(sockfd, msg, flags);
1037 }
1038
1039 static int libc_send(int sockfd, const void *buf, size_t len, int flags)
1040 {
1041         swrap_bind_symbol_libsocket(send);
1042
1043         return swrap.libc.symbols._libc_send.f(sockfd, buf, len, flags);
1044 }
1045
1046 static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
1047 {
1048         swrap_bind_symbol_libsocket(sendmsg);
1049
1050         return swrap.libc.symbols._libc_sendmsg.f(sockfd, msg, flags);
1051 }
1052
1053 static int libc_sendto(int sockfd,
1054                        const void *buf,
1055                        size_t len,
1056                        int flags,
1057                        const  struct sockaddr *dst_addr,
1058                        socklen_t addrlen)
1059 {
1060         swrap_bind_symbol_libsocket(sendto);
1061
1062         return swrap.libc.symbols._libc_sendto.f(sockfd,
1063                                                  buf,
1064                                                  len,
1065                                                  flags,
1066                                                  dst_addr,
1067                                                  addrlen);
1068 }
1069
1070 static int libc_setsockopt(int sockfd,
1071                            int level,
1072                            int optname,
1073                            const void *optval,
1074                            socklen_t optlen)
1075 {
1076         swrap_bind_symbol_libsocket(setsockopt);
1077
1078         return swrap.libc.symbols._libc_setsockopt.f(sockfd,
1079                                                      level,
1080                                                      optname,
1081                                                      optval,
1082                                                      optlen);
1083 }
1084
1085 #ifdef HAVE_SIGNALFD
1086 static int libc_signalfd(int fd, const sigset_t *mask, int flags)
1087 {
1088         swrap_bind_symbol_libsocket(signalfd);
1089
1090         return swrap.libc.symbols._libc_signalfd.f(fd, mask, flags);
1091 }
1092 #endif
1093
1094 static int libc_socket(int domain, int type, int protocol)
1095 {
1096         swrap_bind_symbol_libsocket(socket);
1097
1098         return swrap.libc.symbols._libc_socket.f(domain, type, protocol);
1099 }
1100
1101 static int libc_socketpair(int domain, int type, int protocol, int sv[2])
1102 {
1103         swrap_bind_symbol_libsocket(socketpair);
1104
1105         return swrap.libc.symbols._libc_socketpair.f(domain, type, protocol, sv);
1106 }
1107
1108 #ifdef HAVE_TIMERFD_CREATE
1109 static int libc_timerfd_create(int clockid, int flags)
1110 {
1111         swrap_bind_symbol_libc(timerfd_create);
1112
1113         return swrap.libc.symbols._libc_timerfd_create.f(clockid, flags);
1114 }
1115 #endif
1116
1117 static ssize_t libc_write(int fd, const void *buf, size_t count)
1118 {
1119         swrap_bind_symbol_libc(write);
1120
1121         return swrap.libc.symbols._libc_write.f(fd, buf, count);
1122 }
1123
1124 static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
1125 {
1126         swrap_bind_symbol_libsocket(writev);
1127
1128         return swrap.libc.symbols._libc_writev.f(fd, iov, iovcnt);
1129 }
1130
1131 /* DO NOT call this function during library initialization! */
1132 static void swrap_bind_symbol_all(void)
1133 {
1134 #ifdef HAVE_ACCEPT4
1135         swrap_bind_symbol_libsocket(accept4);
1136 #else
1137         swrap_bind_symbol_libsocket(accept);
1138 #endif
1139         swrap_bind_symbol_libsocket(bind);
1140         swrap_bind_symbol_libc(close);
1141         swrap_bind_symbol_libsocket(connect);
1142         swrap_bind_symbol_libc(dup);
1143         swrap_bind_symbol_libc(dup2);
1144         swrap_bind_symbol_libc(fcntl);
1145         swrap_bind_symbol_libc(fopen);
1146 #ifdef HAVE_FOPEN64
1147         swrap_bind_symbol_libc(fopen64);
1148 #endif
1149 #ifdef HAVE_EVENTFD
1150         swrap_bind_symbol_libc(eventfd);
1151 #endif
1152         swrap_bind_symbol_libsocket(getpeername);
1153         swrap_bind_symbol_libsocket(getsockname);
1154         swrap_bind_symbol_libsocket(getsockopt);
1155         swrap_bind_symbol_libc(ioctl);
1156         swrap_bind_symbol_libsocket(listen);
1157         swrap_bind_symbol_libc(open);
1158 #ifdef HAVE_OPEN64
1159         swrap_bind_symbol_libc(open64);
1160 #endif
1161         swrap_bind_symbol_libc(openat);
1162         swrap_bind_symbol_libsocket(pipe);
1163         swrap_bind_symbol_libc(read);
1164         swrap_bind_symbol_libsocket(readv);
1165         swrap_bind_symbol_libsocket(recv);
1166         swrap_bind_symbol_libsocket(recvfrom);
1167         swrap_bind_symbol_libsocket(recvmsg);
1168         swrap_bind_symbol_libsocket(send);
1169         swrap_bind_symbol_libsocket(sendmsg);
1170         swrap_bind_symbol_libsocket(sendto);
1171         swrap_bind_symbol_libsocket(setsockopt);
1172 #ifdef HAVE_SIGNALFD
1173         swrap_bind_symbol_libsocket(signalfd);
1174 #endif
1175         swrap_bind_symbol_libsocket(socket);
1176         swrap_bind_symbol_libsocket(socketpair);
1177 #ifdef HAVE_TIMERFD_CREATE
1178         swrap_bind_symbol_libc(timerfd_create);
1179 #endif
1180         swrap_bind_symbol_libc(write);
1181         swrap_bind_symbol_libsocket(writev);
1182 }
1183
1184 /*********************************************************
1185  * SWRAP HELPER FUNCTIONS
1186  *********************************************************/
1187
1188 /*
1189  * We return 127.0.0.0 (default) or 10.53.57.0.
1190  *
1191  * This can be controlled by:
1192  * SOCKET_WRAPPER_IPV4_NETWORK=127.0.0.0 (default)
1193  * or
1194  * SOCKET_WRAPPER_IPV4_NETWORK=10.53.57.0
1195  */
1196 static in_addr_t swrap_ipv4_net(void)
1197 {
1198         static int initialized;
1199         static in_addr_t hv;
1200         const char *net_str = NULL;
1201         struct in_addr nv;
1202         int ret;
1203
1204         if (initialized) {
1205                 return hv;
1206         }
1207         initialized = 1;
1208
1209         net_str = getenv("SOCKET_WRAPPER_IPV4_NETWORK");
1210         if (net_str == NULL) {
1211                 net_str = "127.0.0.0";
1212         }
1213
1214         ret = inet_pton(AF_INET, net_str, &nv);
1215         if (ret <= 0) {
1216                 SWRAP_LOG(SWRAP_LOG_ERROR,
1217                           "INVALID IPv4 Network [%s]",
1218                           net_str);
1219                 abort();
1220         }
1221
1222         hv = ntohl(nv.s_addr);
1223
1224         switch (hv) {
1225         case 0x7f000000:
1226                 /* 127.0.0.0 */
1227                 break;
1228         case 0x0a353900:
1229                 /* 10.53.57.0 */
1230                 break;
1231         default:
1232                 SWRAP_LOG(SWRAP_LOG_ERROR,
1233                           "INVALID IPv4 Network [%s][0x%x] should be "
1234                           "127.0.0.0 or 10.53.57.0",
1235                           net_str, (unsigned)hv);
1236                 abort();
1237         }
1238
1239         return hv;
1240 }
1241
1242 /*
1243  * This returns 127.255.255.255 or 10.255.255.255
1244  */
1245 static in_addr_t swrap_ipv4_bcast(void)
1246 {
1247         in_addr_t hv;
1248
1249         hv = swrap_ipv4_net();
1250         hv |= IN_CLASSA_HOST;
1251
1252         return hv;
1253 }
1254
1255 /*
1256  * This returns 127.0.0.${iface} or 10.53.57.${iface}
1257  */
1258 static in_addr_t swrap_ipv4_iface(unsigned int iface)
1259 {
1260         in_addr_t hv;
1261
1262         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1263                 SWRAP_LOG(SWRAP_LOG_ERROR,
1264                           "swrap_ipv4_iface(%u) invalid!",
1265                           iface);
1266                 abort();
1267                 return -1;
1268         }
1269
1270         hv = swrap_ipv4_net();
1271         hv |= iface;
1272
1273         return hv;
1274 }
1275
1276 #ifdef HAVE_IPV6
1277 /*
1278  * FD00::5357:5FXX
1279  */
1280 static const struct in6_addr *swrap_ipv6(void)
1281 {
1282         static struct in6_addr v;
1283         static int initialized;
1284         int ret;
1285
1286         if (initialized) {
1287                 return &v;
1288         }
1289         initialized = 1;
1290
1291         ret = inet_pton(AF_INET6, "FD00::5357:5F00", &v);
1292         if (ret <= 0) {
1293                 abort();
1294         }
1295
1296         return &v;
1297 }
1298 #endif
1299
1300 static void set_port(int family, int prt, struct swrap_address *addr)
1301 {
1302         switch (family) {
1303         case AF_INET:
1304                 addr->sa.in.sin_port = htons(prt);
1305                 break;
1306 #ifdef HAVE_IPV6
1307         case AF_INET6:
1308                 addr->sa.in6.sin6_port = htons(prt);
1309                 break;
1310 #endif
1311         }
1312 }
1313
1314 static size_t socket_length(int family)
1315 {
1316         switch (family) {
1317         case AF_INET:
1318                 return sizeof(struct sockaddr_in);
1319 #ifdef HAVE_IPV6
1320         case AF_INET6:
1321                 return sizeof(struct sockaddr_in6);
1322 #endif
1323         }
1324         return 0;
1325 }
1326
1327 static struct socket_info *swrap_get_socket_info(int si_index)
1328 {
1329         return (struct socket_info *)(&(sockets[si_index].info));
1330 }
1331
1332 static int swrap_get_refcount(struct socket_info *si)
1333 {
1334         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si);
1335         return sic->meta.refcount;
1336 }
1337
1338 static void swrap_inc_refcount(struct socket_info *si)
1339 {
1340         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si);
1341
1342         sic->meta.refcount += 1;
1343 }
1344
1345 static void swrap_dec_refcount(struct socket_info *si)
1346 {
1347         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si);
1348
1349         sic->meta.refcount -= 1;
1350 }
1351
1352 static int swrap_get_next_free(struct socket_info *si)
1353 {
1354         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si);
1355
1356         return sic->meta.next_free;
1357 }
1358
1359 static void swrap_set_next_free(struct socket_info *si, int next_free)
1360 {
1361         struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si);
1362
1363         sic->meta.next_free = next_free;
1364 }
1365
1366 static int swrap_un_path(struct sockaddr_un *un,
1367                          const char *swrap_dir,
1368                          char type,
1369                          unsigned int iface,
1370                          unsigned int prt)
1371 {
1372         int ret;
1373
1374         ret = snprintf(un->sun_path,
1375                        sizeof(un->sun_path),
1376                        "%s/"SOCKET_FORMAT,
1377                        swrap_dir,
1378                        type,
1379                        iface,
1380                        prt);
1381         if ((size_t)ret >= sizeof(un->sun_path)) {
1382                 return ENAMETOOLONG;
1383         }
1384
1385         return 0;
1386 }
1387
1388 static int swrap_un_path_EINVAL(struct sockaddr_un *un,
1389                                 const char *swrap_dir)
1390 {
1391         int ret;
1392
1393         ret = snprintf(un->sun_path,
1394                        sizeof(un->sun_path),
1395                        "%s/EINVAL",
1396                        swrap_dir);
1397
1398         if ((size_t)ret >= sizeof(un->sun_path)) {
1399                 return ENAMETOOLONG;
1400         }
1401
1402         return 0;
1403 }
1404
1405 static bool swrap_dir_usable(const char *swrap_dir)
1406 {
1407         struct sockaddr_un un;
1408         int ret;
1409
1410         ret = swrap_un_path(&un, swrap_dir, SOCKET_TYPE_CHAR_TCP, 0, 0);
1411         if (ret == 0) {
1412                 return true;
1413         }
1414
1415         ret = swrap_un_path_EINVAL(&un, swrap_dir);
1416         if (ret == 0) {
1417                 return true;
1418         }
1419
1420         return false;
1421 }
1422
1423 static char *socket_wrapper_dir(void)
1424 {
1425         char *swrap_dir = NULL;
1426         char *s = getenv("SOCKET_WRAPPER_DIR");
1427         char *t;
1428         bool ok;
1429
1430         if (s == NULL) {
1431                 SWRAP_LOG(SWRAP_LOG_WARN, "SOCKET_WRAPPER_DIR not set");
1432                 return NULL;
1433         }
1434
1435         swrap_dir = realpath(s, NULL);
1436         if (swrap_dir == NULL) {
1437                 SWRAP_LOG(SWRAP_LOG_ERROR,
1438                           "Unable to resolve socket_wrapper dir path: %s",
1439                           strerror(errno));
1440                 abort();
1441         }
1442
1443         ok = swrap_dir_usable(swrap_dir);
1444         if (ok) {
1445                 goto done;
1446         }
1447
1448         free(swrap_dir);
1449
1450         ok = swrap_dir_usable(s);
1451         if (!ok) {
1452                 SWRAP_LOG(SWRAP_LOG_ERROR, "SOCKET_WRAPPER_DIR is too long");
1453                 abort();
1454         }
1455
1456         t = getenv("SOCKET_WRAPPER_DIR_ALLOW_ORIG");
1457         if (t == NULL) {
1458                 SWRAP_LOG(SWRAP_LOG_ERROR,
1459                           "realpath(SOCKET_WRAPPER_DIR) too long and "
1460                           "SOCKET_WRAPPER_DIR_ALLOW_ORIG not set");
1461                 abort();
1462
1463         }
1464
1465         swrap_dir = strdup(s);
1466         if (swrap_dir == NULL) {
1467                 SWRAP_LOG(SWRAP_LOG_ERROR,
1468                           "Unable to duplicate socket_wrapper dir path");
1469                 abort();
1470         }
1471
1472         SWRAP_LOG(SWRAP_LOG_WARN,
1473                   "realpath(SOCKET_WRAPPER_DIR) too long, "
1474                   "using original SOCKET_WRAPPER_DIR\n");
1475
1476 done:
1477         SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", swrap_dir);
1478         return swrap_dir;
1479 }
1480
1481 static unsigned int socket_wrapper_mtu(void)
1482 {
1483         static unsigned int max_mtu = 0;
1484         unsigned int tmp;
1485         const char *s;
1486         char *endp;
1487
1488         swrap_mutex_lock(&mtu_update_mutex);
1489
1490         if (max_mtu != 0) {
1491                 goto done;
1492         }
1493
1494         max_mtu = SOCKET_WRAPPER_MTU_DEFAULT;
1495
1496         s = getenv("SOCKET_WRAPPER_MTU");
1497         if (s == NULL) {
1498                 goto done;
1499         }
1500
1501         tmp = strtol(s, &endp, 10);
1502         if (s == endp) {
1503                 goto done;
1504         }
1505
1506         if (tmp < SOCKET_WRAPPER_MTU_MIN || tmp > SOCKET_WRAPPER_MTU_MAX) {
1507                 goto done;
1508         }
1509         max_mtu = tmp;
1510
1511 done:
1512         swrap_mutex_unlock(&mtu_update_mutex);
1513         return max_mtu;
1514 }
1515
1516 static int socket_wrapper_init_mutex(pthread_mutex_t *m)
1517 {
1518         pthread_mutexattr_t ma;
1519         int ret;
1520
1521         ret = pthread_mutexattr_init(&ma);
1522         if (ret != 0) {
1523                 return ret;
1524         }
1525
1526         ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK);
1527         if (ret != 0) {
1528                 goto done;
1529         }
1530
1531         ret = pthread_mutex_init(m, &ma);
1532
1533 done:
1534         pthread_mutexattr_destroy(&ma);
1535
1536         return ret;
1537 }
1538
1539 static size_t socket_wrapper_max_sockets(void)
1540 {
1541         const char *s;
1542         size_t tmp;
1543         char *endp;
1544
1545         if (socket_info_max != 0) {
1546                 return socket_info_max;
1547         }
1548
1549         socket_info_max = SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT;
1550
1551         s = getenv("SOCKET_WRAPPER_MAX_SOCKETS");
1552         if (s == NULL || s[0] == '\0') {
1553                 goto done;
1554         }
1555
1556         tmp = strtoul(s, &endp, 10);
1557         if (s == endp) {
1558                 goto done;
1559         }
1560         if (tmp == 0) {
1561                 tmp = SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT;
1562                 SWRAP_LOG(SWRAP_LOG_ERROR,
1563                           "Invalid number of sockets specified, "
1564                           "using default (%zu)",
1565                           tmp);
1566         }
1567
1568         if (tmp > SOCKET_WRAPPER_MAX_SOCKETS_LIMIT) {
1569                 tmp = SOCKET_WRAPPER_MAX_SOCKETS_LIMIT;
1570                 SWRAP_LOG(SWRAP_LOG_ERROR,
1571                           "Invalid number of sockets specified, "
1572                           "using maximum (%zu).",
1573                           tmp);
1574         }
1575
1576         socket_info_max = tmp;
1577
1578 done:
1579         return socket_info_max;
1580 }
1581
1582 static void socket_wrapper_init_fds_idx(void)
1583 {
1584         int *tmp = NULL;
1585         size_t i;
1586
1587         if (socket_fds_idx != NULL) {
1588                 return;
1589         }
1590
1591         tmp = (int *)calloc(socket_fds_max, sizeof(int));
1592         if (tmp == NULL) {
1593                 SWRAP_LOG(SWRAP_LOG_ERROR,
1594                           "Failed to allocate socket fds index array: %s",
1595                           strerror(errno));
1596                 exit(-1);
1597         }
1598
1599         for (i = 0; i < socket_fds_max; i++) {
1600                 tmp[i] = -1;
1601         }
1602
1603         socket_fds_idx = tmp;
1604 }
1605
1606 static void socket_wrapper_init_sockets(void)
1607 {
1608         size_t max_sockets;
1609         size_t i;
1610         int ret;
1611
1612         swrap_mutex_lock(&sockets_mutex);
1613
1614         if (sockets != NULL) {
1615                 swrap_mutex_unlock(&sockets_mutex);
1616                 return;
1617         }
1618
1619         /*
1620          * Intialize the static cache early before
1621          * any thread is able to start.
1622          */
1623         (void)swrap_ipv4_net();
1624
1625         socket_wrapper_init_fds_idx();
1626
1627         /* Needs to be called inside the sockets_mutex lock here. */
1628         max_sockets = socket_wrapper_max_sockets();
1629
1630         sockets = (struct socket_info_container *)calloc(max_sockets,
1631                                         sizeof(struct socket_info_container));
1632
1633         if (sockets == NULL) {
1634                 SWRAP_LOG(SWRAP_LOG_ERROR,
1635                           "Failed to allocate sockets array: %s",
1636                           strerror(errno));
1637                 swrap_mutex_unlock(&sockets_mutex);
1638                 exit(-1);
1639         }
1640
1641         swrap_mutex_lock(&first_free_mutex);
1642
1643         first_free = 0;
1644
1645         for (i = 0; i < max_sockets; i++) {
1646                 swrap_set_next_free(&sockets[i].info, i+1);
1647                 ret = socket_wrapper_init_mutex(&sockets[i].meta.mutex);
1648                 if (ret != 0) {
1649                         SWRAP_LOG(SWRAP_LOG_ERROR,
1650                                   "Failed to initialize pthread mutex");
1651                         goto done;
1652                 }
1653         }
1654
1655         /* mark the end of the free list */
1656         swrap_set_next_free(&sockets[max_sockets-1].info, -1);
1657
1658         ret = socket_wrapper_init_mutex(&autobind_start_mutex);
1659         if (ret != 0) {
1660                 SWRAP_LOG(SWRAP_LOG_ERROR,
1661                           "Failed to initialize pthread mutex");
1662                 goto done;
1663         }
1664
1665         ret = socket_wrapper_init_mutex(&pcap_dump_mutex);
1666         if (ret != 0) {
1667                 SWRAP_LOG(SWRAP_LOG_ERROR,
1668                           "Failed to initialize pthread mutex");
1669                 goto done;
1670         }
1671
1672         ret = socket_wrapper_init_mutex(&mtu_update_mutex);
1673         if (ret != 0) {
1674                 SWRAP_LOG(SWRAP_LOG_ERROR,
1675                           "Failed to initialize pthread mutex");
1676                 goto done;
1677         }
1678
1679 done:
1680         swrap_mutex_unlock(&first_free_mutex);
1681         swrap_mutex_unlock(&sockets_mutex);
1682         if (ret != 0) {
1683                 exit(-1);
1684         }
1685 }
1686
1687 bool socket_wrapper_enabled(void)
1688 {
1689         char *s = socket_wrapper_dir();
1690
1691         if (s == NULL) {
1692                 return false;
1693         }
1694
1695         SAFE_FREE(s);
1696
1697         socket_wrapper_init_sockets();
1698
1699         return true;
1700 }
1701
1702 static unsigned int socket_wrapper_default_iface(void)
1703 {
1704         const char *s = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
1705         if (s) {
1706                 unsigned int iface;
1707                 if (sscanf(s, "%u", &iface) == 1) {
1708                         if (iface >= 1 && iface <= MAX_WRAPPED_INTERFACES) {
1709                                 return iface;
1710                         }
1711                 }
1712         }
1713
1714         return 1;/* 127.0.0.1 */
1715 }
1716
1717 static void set_socket_info_index(int fd, int idx)
1718 {
1719         SWRAP_LOG(SWRAP_LOG_TRACE,
1720                   "fd=%d idx=%d",
1721                   fd, idx);
1722         socket_fds_idx[fd] = idx;
1723         /* This builtin issues a full memory barrier. */
1724         __sync_synchronize();
1725 }
1726
1727 static void reset_socket_info_index(int fd)
1728 {
1729         SWRAP_LOG(SWRAP_LOG_TRACE,
1730                   "fd=%d idx=%d",
1731                   fd, -1);
1732         set_socket_info_index(fd, -1);
1733 }
1734
1735 static int find_socket_info_index(int fd)
1736 {
1737         if (fd < 0) {
1738                 return -1;
1739         }
1740
1741         if (socket_fds_idx == NULL) {
1742                 return -1;
1743         }
1744
1745         if ((size_t)fd >= socket_fds_max) {
1746                 /*
1747                  * Do not add a log here as some applications do stupid things
1748                  * like:
1749                  *
1750                  *     for (fd = 0; fd <= getdtablesize(); fd++) {
1751                  *         close(fd)
1752                  *     };
1753                  *
1754                  * This would produce millions of lines of debug messages.
1755                  */
1756 #if 0
1757                 SWRAP_LOG(SWRAP_LOG_ERROR,
1758                           "Looking for a socket info for the fd %d is over the "
1759                           "max socket index limit of %zu.",
1760                           fd,
1761                           socket_fds_max);
1762 #endif
1763                 return -1;
1764         }
1765
1766         /* This builtin issues a full memory barrier. */
1767         __sync_synchronize();
1768         return socket_fds_idx[fd];
1769 }
1770
1771 static int swrap_add_socket_info(struct socket_info *si_input)
1772 {
1773         struct socket_info *si = NULL;
1774         int si_index = -1;
1775
1776         if (si_input == NULL) {
1777                 errno = EINVAL;
1778                 return -1;
1779         }
1780
1781         swrap_mutex_lock(&first_free_mutex);
1782         if (first_free == -1) {
1783                 errno = ENFILE;
1784                 goto out;
1785         }
1786
1787         si_index = first_free;
1788         si = swrap_get_socket_info(si_index);
1789
1790         SWRAP_LOCK_SI(si);
1791
1792         first_free = swrap_get_next_free(si);
1793         *si = *si_input;
1794         swrap_inc_refcount(si);
1795
1796         SWRAP_UNLOCK_SI(si);
1797
1798 out:
1799         swrap_mutex_unlock(&first_free_mutex);
1800
1801         return si_index;
1802 }
1803
1804 static int swrap_create_socket(struct socket_info *si, int fd)
1805 {
1806         int idx;
1807
1808         if ((size_t)fd >= socket_fds_max) {
1809                 SWRAP_LOG(SWRAP_LOG_ERROR,
1810                           "The max socket index limit of %zu has been reached, "
1811                           "trying to add %d",
1812                           socket_fds_max,
1813                           fd);
1814                 return -1;
1815         }
1816
1817         idx = swrap_add_socket_info(si);
1818         if (idx == -1) {
1819                 return -1;
1820         }
1821
1822         set_socket_info_index(fd, idx);
1823
1824         return idx;
1825 }
1826
1827 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len)
1828 {
1829         unsigned int iface;
1830         unsigned int prt;
1831         const char *p;
1832         char type;
1833
1834         p = strrchr(un->sun_path, '/');
1835         if (p) p++; else p = un->sun_path;
1836
1837         if (sscanf(p, SOCKET_FORMAT, &type, &iface, &prt) != 3) {
1838                 errno = EINVAL;
1839                 return -1;
1840         }
1841
1842         SWRAP_LOG(SWRAP_LOG_TRACE, "type %c iface %u port %u",
1843                         type, iface, prt);
1844
1845         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1846                 errno = EINVAL;
1847                 return -1;
1848         }
1849
1850         if (prt > 0xFFFF) {
1851                 errno = EINVAL;
1852                 return -1;
1853         }
1854
1855         switch(type) {
1856         case SOCKET_TYPE_CHAR_TCP:
1857         case SOCKET_TYPE_CHAR_UDP: {
1858                 struct sockaddr_in *in2 = (struct sockaddr_in *)(void *)in;
1859
1860                 if ((*len) < sizeof(*in2)) {
1861                     errno = EINVAL;
1862                     return -1;
1863                 }
1864
1865                 memset(in2, 0, sizeof(*in2));
1866                 in2->sin_family = AF_INET;
1867                 in2->sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
1868                 in2->sin_port = htons(prt);
1869
1870                 *len = sizeof(*in2);
1871                 break;
1872         }
1873 #ifdef HAVE_IPV6
1874         case SOCKET_TYPE_CHAR_TCP_V6:
1875         case SOCKET_TYPE_CHAR_UDP_V6: {
1876                 struct sockaddr_in6 *in2 = (struct sockaddr_in6 *)(void *)in;
1877
1878                 if ((*len) < sizeof(*in2)) {
1879                         errno = EINVAL;
1880                         return -1;
1881                 }
1882
1883                 memset(in2, 0, sizeof(*in2));
1884                 in2->sin6_family = AF_INET6;
1885                 in2->sin6_addr = *swrap_ipv6();
1886                 in2->sin6_addr.s6_addr[15] = iface;
1887                 in2->sin6_port = htons(prt);
1888
1889                 *len = sizeof(*in2);
1890                 break;
1891         }
1892 #endif
1893         default:
1894                 errno = EINVAL;
1895                 return -1;
1896         }
1897
1898         return 0;
1899 }
1900
1901 static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
1902                                 int *bcast)
1903 {
1904         char type = '\0';
1905         unsigned int prt;
1906         unsigned int iface;
1907         int is_bcast = 0;
1908         char *swrap_dir = NULL;
1909
1910         if (bcast) *bcast = 0;
1911
1912         switch (inaddr->sa_family) {
1913         case AF_INET: {
1914                 const struct sockaddr_in *in =
1915                     (const struct sockaddr_in *)(const void *)inaddr;
1916                 unsigned int addr = ntohl(in->sin_addr.s_addr);
1917                 char u_type = '\0';
1918                 char b_type = '\0';
1919                 char a_type = '\0';
1920                 const unsigned int sw_net_addr = swrap_ipv4_net();
1921                 const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
1922
1923                 switch (si->type) {
1924                 case SOCK_STREAM:
1925                         u_type = SOCKET_TYPE_CHAR_TCP;
1926                         break;
1927                 case SOCK_DGRAM:
1928                         u_type = SOCKET_TYPE_CHAR_UDP;
1929                         a_type = SOCKET_TYPE_CHAR_UDP;
1930                         b_type = SOCKET_TYPE_CHAR_UDP;
1931                         break;
1932                 default:
1933                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
1934                         errno = ESOCKTNOSUPPORT;
1935                         return -1;
1936                 }
1937
1938                 prt = ntohs(in->sin_port);
1939                 if (a_type && addr == 0xFFFFFFFF) {
1940                         /* 255.255.255.255 only udp */
1941                         is_bcast = 2;
1942                         type = a_type;
1943                         iface = socket_wrapper_default_iface();
1944                 } else if (b_type && addr == sw_bcast_addr) {
1945                         /*
1946                          * 127.255.255.255
1947                          * or
1948                          * 10.255.255.255
1949                          * only udp
1950                          */
1951                         is_bcast = 1;
1952                         type = b_type;
1953                         iface = socket_wrapper_default_iface();
1954                 } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
1955                         /* 127.0.0.X or 10.53.57.X */
1956                         is_bcast = 0;
1957                         type = u_type;
1958                         iface = (addr & 0x000000FF);
1959                 } else {
1960                         errno = ENETUNREACH;
1961                         return -1;
1962                 }
1963                 if (bcast) *bcast = is_bcast;
1964                 break;
1965         }
1966 #ifdef HAVE_IPV6
1967         case AF_INET6: {
1968                 const struct sockaddr_in6 *in =
1969                     (const struct sockaddr_in6 *)(const void *)inaddr;
1970                 struct in6_addr cmp1, cmp2;
1971
1972                 switch (si->type) {
1973                 case SOCK_STREAM:
1974                         type = SOCKET_TYPE_CHAR_TCP_V6;
1975                         break;
1976                 case SOCK_DGRAM:
1977                         type = SOCKET_TYPE_CHAR_UDP_V6;
1978                         break;
1979                 default:
1980                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
1981                         errno = ESOCKTNOSUPPORT;
1982                         return -1;
1983                 }
1984
1985                 /* XXX no multicast/broadcast */
1986
1987                 prt = ntohs(in->sin6_port);
1988
1989                 cmp1 = *swrap_ipv6();
1990                 cmp2 = in->sin6_addr;
1991                 cmp2.s6_addr[15] = 0;
1992                 if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
1993                         iface = in->sin6_addr.s6_addr[15];
1994                 } else {
1995                         errno = ENETUNREACH;
1996                         return -1;
1997                 }
1998
1999                 break;
2000         }
2001 #endif
2002         default:
2003                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!");
2004                 errno = ENETUNREACH;
2005                 return -1;
2006         }
2007
2008         if (prt == 0) {
2009                 SWRAP_LOG(SWRAP_LOG_WARN, "Port not set");
2010                 errno = EINVAL;
2011                 return -1;
2012         }
2013
2014         swrap_dir = socket_wrapper_dir();
2015         if (swrap_dir == NULL) {
2016                 errno = EINVAL;
2017                 return -1;
2018         }
2019
2020         if (is_bcast) {
2021                 swrap_un_path_EINVAL(un, swrap_dir);
2022                 SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
2023                 SAFE_FREE(swrap_dir);
2024                 /* the caller need to do more processing */
2025                 return 0;
2026         }
2027
2028         swrap_un_path(un, swrap_dir, type, iface, prt);
2029         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
2030
2031         SAFE_FREE(swrap_dir);
2032
2033         return 0;
2034 }
2035
2036 static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
2037                                int *bcast)
2038 {
2039         char type = '\0';
2040         unsigned int prt;
2041         unsigned int iface;
2042         struct stat st;
2043         int is_bcast = 0;
2044         char *swrap_dir = NULL;
2045
2046         if (bcast) *bcast = 0;
2047
2048         switch (si->family) {
2049         case AF_INET: {
2050                 const struct sockaddr_in *in =
2051                     (const struct sockaddr_in *)(const void *)inaddr;
2052                 unsigned int addr = ntohl(in->sin_addr.s_addr);
2053                 char u_type = '\0';
2054                 char d_type = '\0';
2055                 char b_type = '\0';
2056                 char a_type = '\0';
2057                 const unsigned int sw_net_addr = swrap_ipv4_net();
2058                 const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
2059
2060                 prt = ntohs(in->sin_port);
2061
2062                 switch (si->type) {
2063                 case SOCK_STREAM:
2064                         u_type = SOCKET_TYPE_CHAR_TCP;
2065                         d_type = SOCKET_TYPE_CHAR_TCP;
2066                         break;
2067                 case SOCK_DGRAM:
2068                         u_type = SOCKET_TYPE_CHAR_UDP;
2069                         d_type = SOCKET_TYPE_CHAR_UDP;
2070                         a_type = SOCKET_TYPE_CHAR_UDP;
2071                         b_type = SOCKET_TYPE_CHAR_UDP;
2072                         break;
2073                 default:
2074                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
2075                         errno = ESOCKTNOSUPPORT;
2076                         return -1;
2077                 }
2078
2079                 if (addr == 0) {
2080                         /* 0.0.0.0 */
2081                         is_bcast = 0;
2082                         type = d_type;
2083                         iface = socket_wrapper_default_iface();
2084                 } else if (a_type && addr == 0xFFFFFFFF) {
2085                         /* 255.255.255.255 only udp */
2086                         is_bcast = 2;
2087                         type = a_type;
2088                         iface = socket_wrapper_default_iface();
2089                 } else if (b_type && addr == sw_bcast_addr) {
2090                         /* 127.255.255.255 only udp */
2091                         is_bcast = 1;
2092                         type = b_type;
2093                         iface = socket_wrapper_default_iface();
2094                 } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
2095                         /* 127.0.0.X */
2096                         is_bcast = 0;
2097                         type = u_type;
2098                         iface = (addr & 0x000000FF);
2099                 } else {
2100                         errno = EADDRNOTAVAIL;
2101                         return -1;
2102                 }
2103
2104                 /* Store the bind address for connect() */
2105                 if (si->bindname.sa_socklen == 0) {
2106                         struct sockaddr_in bind_in;
2107                         socklen_t blen = sizeof(struct sockaddr_in);
2108
2109                         ZERO_STRUCT(bind_in);
2110                         bind_in.sin_family = in->sin_family;
2111                         bind_in.sin_port = in->sin_port;
2112                         bind_in.sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
2113                         si->bindname.sa_socklen = blen;
2114                         memcpy(&si->bindname.sa.in, &bind_in, blen);
2115                 }
2116
2117                 break;
2118         }
2119 #ifdef HAVE_IPV6
2120         case AF_INET6: {
2121                 const struct sockaddr_in6 *in =
2122                     (const struct sockaddr_in6 *)(const void *)inaddr;
2123                 struct in6_addr cmp1, cmp2;
2124
2125                 switch (si->type) {
2126                 case SOCK_STREAM:
2127                         type = SOCKET_TYPE_CHAR_TCP_V6;
2128                         break;
2129                 case SOCK_DGRAM:
2130                         type = SOCKET_TYPE_CHAR_UDP_V6;
2131                         break;
2132                 default:
2133                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
2134                         errno = ESOCKTNOSUPPORT;
2135                         return -1;
2136                 }
2137
2138                 /* XXX no multicast/broadcast */
2139
2140                 prt = ntohs(in->sin6_port);
2141
2142                 cmp1 = *swrap_ipv6();
2143                 cmp2 = in->sin6_addr;
2144                 cmp2.s6_addr[15] = 0;
2145                 if (IN6_IS_ADDR_UNSPECIFIED(&in->sin6_addr)) {
2146                         iface = socket_wrapper_default_iface();
2147                 } else if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
2148                         iface = in->sin6_addr.s6_addr[15];
2149                 } else {
2150                         errno = EADDRNOTAVAIL;
2151                         return -1;
2152                 }
2153
2154                 /* Store the bind address for connect() */
2155                 if (si->bindname.sa_socklen == 0) {
2156                         struct sockaddr_in6 bind_in;
2157                         socklen_t blen = sizeof(struct sockaddr_in6);
2158
2159                         ZERO_STRUCT(bind_in);
2160                         bind_in.sin6_family = in->sin6_family;
2161                         bind_in.sin6_port = in->sin6_port;
2162
2163                         bind_in.sin6_addr = *swrap_ipv6();
2164                         bind_in.sin6_addr.s6_addr[15] = iface;
2165
2166                         memcpy(&si->bindname.sa.in6, &bind_in, blen);
2167                         si->bindname.sa_socklen = blen;
2168                 }
2169
2170                 break;
2171         }
2172 #endif
2173         default:
2174                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
2175                 errno = EADDRNOTAVAIL;
2176                 return -1;
2177         }
2178
2179
2180         if (bcast) *bcast = is_bcast;
2181
2182         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
2183                 errno = EINVAL;
2184                 return -1;
2185         }
2186
2187         swrap_dir = socket_wrapper_dir();
2188         if (swrap_dir == NULL) {
2189                 errno = EINVAL;
2190                 return -1;
2191         }
2192
2193         if (prt == 0) {
2194                 /* handle auto-allocation of ephemeral ports */
2195                 for (prt = 5001; prt < 10000; prt++) {
2196                         swrap_un_path(un, swrap_dir, type, iface, prt);
2197                         if (stat(un->sun_path, &st) == 0) continue;
2198
2199                         set_port(si->family, prt, &si->myname);
2200                         set_port(si->family, prt, &si->bindname);
2201
2202                         break;
2203                 }
2204
2205                 if (prt == 10000) {
2206                         errno = ENFILE;
2207                         SAFE_FREE(swrap_dir);
2208                         return -1;
2209                 }
2210         }
2211
2212         swrap_un_path(un, swrap_dir, type, iface, prt);
2213         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
2214
2215         SAFE_FREE(swrap_dir);
2216
2217         return 0;
2218 }
2219
2220 static struct socket_info *find_socket_info(int fd)
2221 {
2222         int idx = find_socket_info_index(fd);
2223
2224         if (idx == -1) {
2225                 return NULL;
2226         }
2227
2228         return swrap_get_socket_info(idx);
2229 }
2230
2231 #if 0 /* FIXME */
2232 static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
2233 {
2234         struct socket_info_fd *f;
2235         const struct socket_info *last_s = NULL;
2236
2237         /* first catch invalid input */
2238         switch (sa->sa_family) {
2239         case AF_INET:
2240                 if (len < sizeof(struct sockaddr_in)) {
2241                         return false;
2242                 }
2243                 break;
2244 #ifdef HAVE_IPV6
2245         case AF_INET6:
2246                 if (len < sizeof(struct sockaddr_in6)) {
2247                         return false;
2248                 }
2249                 break;
2250 #endif
2251         default:
2252                 return false;
2253                 break;
2254         }
2255
2256         for (f = socket_fds; f; f = f->next) {
2257                 struct socket_info *s = swrap_get_socket_info(f->si_index);
2258
2259                 if (s == last_s) {
2260                         continue;
2261                 }
2262                 last_s = s;
2263
2264                 if (s->myname == NULL) {
2265                         continue;
2266                 }
2267                 if (s->myname->sa_family != sa->sa_family) {
2268                         continue;
2269                 }
2270                 switch (s->myname->sa_family) {
2271                 case AF_INET: {
2272                         struct sockaddr_in *sin1, *sin2;
2273
2274                         sin1 = (struct sockaddr_in *)s->myname;
2275                         sin2 = (struct sockaddr_in *)sa;
2276
2277                         if (sin1->sin_addr.s_addr == htonl(INADDR_ANY)) {
2278                                 continue;
2279                         }
2280                         if (sin1->sin_port != sin2->sin_port) {
2281                                 continue;
2282                         }
2283                         if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
2284                                 continue;
2285                         }
2286
2287                         /* found */
2288                         return true;
2289                         break;
2290                 }
2291 #ifdef HAVE_IPV6
2292                 case AF_INET6: {
2293                         struct sockaddr_in6 *sin1, *sin2;
2294
2295                         sin1 = (struct sockaddr_in6 *)s->myname;
2296                         sin2 = (struct sockaddr_in6 *)sa;
2297
2298                         if (sin1->sin6_port != sin2->sin6_port) {
2299                                 continue;
2300                         }
2301                         if (!IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
2302                                                 &sin2->sin6_addr))
2303                         {
2304                                 continue;
2305                         }
2306
2307                         /* found */
2308                         return true;
2309                         break;
2310                 }
2311 #endif
2312                 default:
2313                         continue;
2314                         break;
2315
2316                 }
2317         }
2318
2319         return false;
2320 }
2321 #endif
2322
2323 static void swrap_remove_stale(int fd)
2324 {
2325         struct socket_info *si;
2326         int si_index;
2327
2328         SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd);
2329
2330         swrap_mutex_lock(&socket_reset_mutex);
2331
2332         si_index = find_socket_info_index(fd);
2333         if (si_index == -1) {
2334                 swrap_mutex_unlock(&socket_reset_mutex);
2335                 return;
2336         }
2337
2338         reset_socket_info_index(fd);
2339
2340         si = swrap_get_socket_info(si_index);
2341
2342         swrap_mutex_lock(&first_free_mutex);
2343         SWRAP_LOCK_SI(si);
2344
2345         swrap_dec_refcount(si);
2346
2347         if (swrap_get_refcount(si) > 0) {
2348                 goto out;
2349         }
2350
2351         if (si->un_addr.sun_path[0] != '\0') {
2352                 unlink(si->un_addr.sun_path);
2353         }
2354
2355         swrap_set_next_free(si, first_free);
2356         first_free = si_index;
2357
2358 out:
2359         SWRAP_UNLOCK_SI(si);
2360         swrap_mutex_unlock(&first_free_mutex);
2361         swrap_mutex_unlock(&socket_reset_mutex);
2362 }
2363
2364 static int sockaddr_convert_to_un(struct socket_info *si,
2365                                   const struct sockaddr *in_addr,
2366                                   socklen_t in_len,
2367                                   struct sockaddr_un *out_addr,
2368                                   int alloc_sock,
2369                                   int *bcast)
2370 {
2371         struct sockaddr *out = (struct sockaddr *)(void *)out_addr;
2372
2373         (void) in_len; /* unused */
2374
2375         if (out_addr == NULL) {
2376                 return 0;
2377         }
2378
2379         out->sa_family = AF_UNIX;
2380 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2381         out->sa_len = sizeof(*out_addr);
2382 #endif
2383
2384         switch (in_addr->sa_family) {
2385         case AF_UNSPEC: {
2386                 const struct sockaddr_in *sin;
2387                 if (si->family != AF_INET) {
2388                         break;
2389                 }
2390                 if (in_len < sizeof(struct sockaddr_in)) {
2391                         break;
2392                 }
2393                 sin = (const struct sockaddr_in *)(const void *)in_addr;
2394                 if(sin->sin_addr.s_addr != htonl(INADDR_ANY)) {
2395                         break;
2396                 }
2397
2398                 /*
2399                  * Note: in the special case of AF_UNSPEC and INADDR_ANY,
2400                  * AF_UNSPEC is mapped to AF_INET and must be treated here.
2401                  */
2402
2403                 FALL_THROUGH;
2404         }
2405         case AF_INET:
2406 #ifdef HAVE_IPV6
2407         case AF_INET6:
2408 #endif
2409                 switch (si->type) {
2410                 case SOCK_STREAM:
2411                 case SOCK_DGRAM:
2412                         break;
2413                 default:
2414                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
2415                         errno = ESOCKTNOSUPPORT;
2416                         return -1;
2417                 }
2418                 if (alloc_sock) {
2419                         return convert_in_un_alloc(si, in_addr, out_addr, bcast);
2420                 } else {
2421                         return convert_in_un_remote(si, in_addr, out_addr, bcast);
2422                 }
2423         default:
2424                 break;
2425         }
2426
2427         errno = EAFNOSUPPORT;
2428         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
2429         return -1;
2430 }
2431
2432 static int sockaddr_convert_from_un(const struct socket_info *si,
2433                                     const struct sockaddr_un *in_addr,
2434                                     socklen_t un_addrlen,
2435                                     int family,
2436                                     struct sockaddr *out_addr,
2437                                     socklen_t *out_addrlen)
2438 {
2439         int ret;
2440
2441         if (out_addr == NULL || out_addrlen == NULL)
2442                 return 0;
2443
2444         if (un_addrlen == 0) {
2445                 *out_addrlen = 0;
2446                 return 0;
2447         }
2448
2449         switch (family) {
2450         case AF_INET:
2451 #ifdef HAVE_IPV6
2452         case AF_INET6:
2453 #endif
2454                 switch (si->type) {
2455                 case SOCK_STREAM:
2456                 case SOCK_DGRAM:
2457                         break;
2458                 default:
2459                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
2460                         errno = ESOCKTNOSUPPORT;
2461                         return -1;
2462                 }
2463                 ret = convert_un_in(in_addr, out_addr, out_addrlen);
2464 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
2465                 out_addr->sa_len = *out_addrlen;
2466 #endif
2467                 return ret;
2468         default:
2469                 break;
2470         }
2471
2472         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
2473         errno = EAFNOSUPPORT;
2474         return -1;
2475 }
2476
2477 enum swrap_packet_type {
2478         SWRAP_CONNECT_SEND,
2479         SWRAP_CONNECT_UNREACH,
2480         SWRAP_CONNECT_RECV,
2481         SWRAP_CONNECT_ACK,
2482         SWRAP_ACCEPT_SEND,
2483         SWRAP_ACCEPT_RECV,
2484         SWRAP_ACCEPT_ACK,
2485         SWRAP_RECVFROM,
2486         SWRAP_SENDTO,
2487         SWRAP_SENDTO_UNREACH,
2488         SWRAP_PENDING_RST,
2489         SWRAP_RECV,
2490         SWRAP_RECV_RST,
2491         SWRAP_SEND,
2492         SWRAP_SEND_RST,
2493         SWRAP_CLOSE_SEND,
2494         SWRAP_CLOSE_RECV,
2495         SWRAP_CLOSE_ACK,
2496 };
2497
2498 struct swrap_file_hdr {
2499         uint32_t        magic;
2500         uint16_t        version_major;
2501         uint16_t        version_minor;
2502         int32_t         timezone;
2503         uint32_t        sigfigs;
2504         uint32_t        frame_max_len;
2505 #define SWRAP_FRAME_LENGTH_MAX 0xFFFF
2506         uint32_t        link_type;
2507 };
2508 #define SWRAP_FILE_HDR_SIZE 24
2509
2510 struct swrap_packet_frame {
2511         uint32_t seconds;
2512         uint32_t micro_seconds;
2513         uint32_t recorded_length;
2514         uint32_t full_length;
2515 };
2516 #define SWRAP_PACKET_FRAME_SIZE 16
2517
2518 union swrap_packet_ip {
2519         struct {
2520                 uint8_t         ver_hdrlen;
2521                 uint8_t         tos;
2522                 uint16_t        packet_length;
2523                 uint16_t        identification;
2524                 uint8_t         flags;
2525                 uint8_t         fragment;
2526                 uint8_t         ttl;
2527                 uint8_t         protocol;
2528                 uint16_t        hdr_checksum;
2529                 uint32_t        src_addr;
2530                 uint32_t        dest_addr;
2531         } v4;
2532 #define SWRAP_PACKET_IP_V4_SIZE 20
2533         struct {
2534                 uint8_t         ver_prio;
2535                 uint8_t         flow_label_high;
2536                 uint16_t        flow_label_low;
2537                 uint16_t        payload_length;
2538                 uint8_t         next_header;
2539                 uint8_t         hop_limit;
2540                 uint8_t         src_addr[16];
2541                 uint8_t         dest_addr[16];
2542         } v6;
2543 #define SWRAP_PACKET_IP_V6_SIZE 40
2544 };
2545 #define SWRAP_PACKET_IP_SIZE 40
2546
2547 union swrap_packet_payload {
2548         struct {
2549                 uint16_t        source_port;
2550                 uint16_t        dest_port;
2551                 uint32_t        seq_num;
2552                 uint32_t        ack_num;
2553                 uint8_t         hdr_length;
2554                 uint8_t         control;
2555                 uint16_t        window;
2556                 uint16_t        checksum;
2557                 uint16_t        urg;
2558         } tcp;
2559 #define SWRAP_PACKET_PAYLOAD_TCP_SIZE 20
2560         struct {
2561                 uint16_t        source_port;
2562                 uint16_t        dest_port;
2563                 uint16_t        length;
2564                 uint16_t        checksum;
2565         } udp;
2566 #define SWRAP_PACKET_PAYLOAD_UDP_SIZE 8
2567         struct {
2568                 uint8_t         type;
2569                 uint8_t         code;
2570                 uint16_t        checksum;
2571                 uint32_t        unused;
2572         } icmp4;
2573 #define SWRAP_PACKET_PAYLOAD_ICMP4_SIZE 8
2574         struct {
2575                 uint8_t         type;
2576                 uint8_t         code;
2577                 uint16_t        checksum;
2578                 uint32_t        unused;
2579         } icmp6;
2580 #define SWRAP_PACKET_PAYLOAD_ICMP6_SIZE 8
2581 };
2582 #define SWRAP_PACKET_PAYLOAD_SIZE 20
2583
2584 #define SWRAP_PACKET_MIN_ALLOC \
2585         (SWRAP_PACKET_FRAME_SIZE + \
2586          SWRAP_PACKET_IP_SIZE + \
2587          SWRAP_PACKET_PAYLOAD_SIZE)
2588
2589 static const char *swrap_pcap_init_file(void)
2590 {
2591         static int initialized = 0;
2592         static const char *s = NULL;
2593         static const struct swrap_file_hdr h;
2594         static const struct swrap_packet_frame f;
2595         static const union swrap_packet_ip i;
2596         static const union swrap_packet_payload p;
2597
2598         if (initialized == 1) {
2599                 return s;
2600         }
2601         initialized = 1;
2602
2603         /*
2604          * TODO: don't use the structs use plain buffer offsets
2605          *       and PUSH_U8(), PUSH_U16() and PUSH_U32()
2606          *
2607          * for now make sure we disable PCAP support
2608          * if the struct has alignment!
2609          */
2610         if (sizeof(h) != SWRAP_FILE_HDR_SIZE) {
2611                 return NULL;
2612         }
2613         if (sizeof(f) != SWRAP_PACKET_FRAME_SIZE) {
2614                 return NULL;
2615         }
2616         if (sizeof(i) != SWRAP_PACKET_IP_SIZE) {
2617                 return NULL;
2618         }
2619         if (sizeof(i.v4) != SWRAP_PACKET_IP_V4_SIZE) {
2620                 return NULL;
2621         }
2622         if (sizeof(i.v6) != SWRAP_PACKET_IP_V6_SIZE) {
2623                 return NULL;
2624         }
2625         if (sizeof(p) != SWRAP_PACKET_PAYLOAD_SIZE) {
2626                 return NULL;
2627         }
2628         if (sizeof(p.tcp) != SWRAP_PACKET_PAYLOAD_TCP_SIZE) {
2629                 return NULL;
2630         }
2631         if (sizeof(p.udp) != SWRAP_PACKET_PAYLOAD_UDP_SIZE) {
2632                 return NULL;
2633         }
2634         if (sizeof(p.icmp4) != SWRAP_PACKET_PAYLOAD_ICMP4_SIZE) {
2635                 return NULL;
2636         }
2637         if (sizeof(p.icmp6) != SWRAP_PACKET_PAYLOAD_ICMP6_SIZE) {
2638                 return NULL;
2639         }
2640
2641         s = getenv("SOCKET_WRAPPER_PCAP_FILE");
2642         if (s == NULL) {
2643                 return NULL;
2644         }
2645         if (strncmp(s, "./", 2) == 0) {
2646                 s += 2;
2647         }
2648         SWRAP_LOG(SWRAP_LOG_TRACE, "SOCKET_WRAPPER_PCAP_FILE: %s", s);
2649         return s;
2650 }
2651
2652 static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
2653                                        const struct sockaddr *src,
2654                                        const struct sockaddr *dest,
2655                                        int socket_type,
2656                                        const uint8_t *payload,
2657                                        size_t payload_len,
2658                                        unsigned long tcp_seqno,
2659                                        unsigned long tcp_ack,
2660                                        unsigned char tcp_ctl,
2661                                        int unreachable,
2662                                        size_t *_packet_len)
2663 {
2664         uint8_t *base = NULL;
2665         uint8_t *buf = NULL;
2666         union {
2667                 uint8_t *ptr;
2668                 struct swrap_packet_frame *frame;
2669         } f;
2670         union {
2671                 uint8_t *ptr;
2672                 union swrap_packet_ip *ip;
2673         } i;
2674         union swrap_packet_payload *pay;
2675         size_t packet_len;
2676         size_t alloc_len;
2677         size_t nonwire_len = sizeof(struct swrap_packet_frame);
2678         size_t wire_hdr_len = 0;
2679         size_t wire_len = 0;
2680         size_t ip_hdr_len = 0;
2681         size_t icmp_hdr_len = 0;
2682         size_t icmp_truncate_len = 0;
2683         uint8_t protocol = 0, icmp_protocol = 0;
2684         const struct sockaddr_in *src_in = NULL;
2685         const struct sockaddr_in *dest_in = NULL;
2686 #ifdef HAVE_IPV6
2687         const struct sockaddr_in6 *src_in6 = NULL;
2688         const struct sockaddr_in6 *dest_in6 = NULL;
2689 #endif
2690         uint16_t src_port;
2691         uint16_t dest_port;
2692
2693         switch (src->sa_family) {
2694         case AF_INET:
2695                 src_in = (const struct sockaddr_in *)(const void *)src;
2696                 dest_in = (const struct sockaddr_in *)(const void *)dest;
2697                 src_port = src_in->sin_port;
2698                 dest_port = dest_in->sin_port;
2699                 ip_hdr_len = sizeof(i.ip->v4);
2700                 break;
2701 #ifdef HAVE_IPV6
2702         case AF_INET6:
2703                 src_in6 = (const struct sockaddr_in6 *)(const void *)src;
2704                 dest_in6 = (const struct sockaddr_in6 *)(const void *)dest;
2705                 src_port = src_in6->sin6_port;
2706                 dest_port = dest_in6->sin6_port;
2707                 ip_hdr_len = sizeof(i.ip->v6);
2708                 break;
2709 #endif
2710         default:
2711                 return NULL;
2712         }
2713
2714         switch (socket_type) {
2715         case SOCK_STREAM:
2716                 protocol = 0x06; /* TCP */
2717                 wire_hdr_len = ip_hdr_len + sizeof(pay->tcp);
2718                 wire_len = wire_hdr_len + payload_len;
2719                 break;
2720
2721         case SOCK_DGRAM:
2722                 protocol = 0x11; /* UDP */
2723                 wire_hdr_len = ip_hdr_len + sizeof(pay->udp);
2724                 wire_len = wire_hdr_len + payload_len;
2725                 break;
2726
2727         default:
2728                 return NULL;
2729         }
2730
2731         if (unreachable) {
2732                 icmp_protocol = protocol;
2733                 switch (src->sa_family) {
2734                 case AF_INET:
2735                         protocol = 0x01; /* ICMPv4 */
2736                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp4);
2737                         break;
2738 #ifdef HAVE_IPV6
2739                 case AF_INET6:
2740                         protocol = 0x3A; /* ICMPv6 */
2741                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp6);
2742                         break;
2743 #endif
2744                 }
2745                 if (wire_len > 64 ) {
2746                         icmp_truncate_len = wire_len - 64;
2747                 }
2748                 wire_len += icmp_hdr_len;
2749         }
2750
2751         packet_len = nonwire_len + wire_len;
2752         alloc_len = packet_len;
2753         if (alloc_len < SWRAP_PACKET_MIN_ALLOC) {
2754                 alloc_len = SWRAP_PACKET_MIN_ALLOC;
2755         }
2756
2757         base = (uint8_t *)calloc(1, alloc_len);
2758         if (base == NULL) {
2759                 return NULL;
2760         }
2761
2762         buf = base;
2763         f.ptr = buf;
2764
2765         f.frame->seconds                = tval->tv_sec;
2766         f.frame->micro_seconds  = tval->tv_usec;
2767         f.frame->recorded_length        = wire_len - icmp_truncate_len;
2768         f.frame->full_length    = wire_len - icmp_truncate_len;
2769
2770         buf += SWRAP_PACKET_FRAME_SIZE;
2771
2772         i.ptr = buf;
2773         switch (src->sa_family) {
2774         case AF_INET:
2775                 if (src_in == NULL || dest_in == NULL) {
2776                         SAFE_FREE(base);
2777                         return NULL;
2778                 }
2779
2780                 i.ip->v4.ver_hdrlen     = 0x45; /* version 4 and 5 * 32 bit words */
2781                 i.ip->v4.tos            = 0x00;
2782                 i.ip->v4.packet_length  = htons(wire_len - icmp_truncate_len);
2783                 i.ip->v4.identification = htons(0xFFFF);
2784                 i.ip->v4.flags          = 0x40; /* BIT 1 set - means don't fragment */
2785                 i.ip->v4.fragment       = htons(0x0000);
2786                 i.ip->v4.ttl            = 0xFF;
2787                 i.ip->v4.protocol       = protocol;
2788                 i.ip->v4.hdr_checksum   = htons(0x0000);
2789                 i.ip->v4.src_addr       = src_in->sin_addr.s_addr;
2790                 i.ip->v4.dest_addr      = dest_in->sin_addr.s_addr;
2791                 buf += SWRAP_PACKET_IP_V4_SIZE;
2792                 break;
2793 #ifdef HAVE_IPV6
2794         case AF_INET6:
2795                 if (src_in6 == NULL || dest_in6 == NULL) {
2796                         SAFE_FREE(base);
2797                         return NULL;
2798                 }
2799
2800                 i.ip->v6.ver_prio               = 0x60; /* version 4 and 5 * 32 bit words */
2801                 i.ip->v6.flow_label_high        = 0x00;
2802                 i.ip->v6.flow_label_low = 0x0000;
2803                 i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
2804                 i.ip->v6.next_header    = protocol;
2805                 memcpy(i.ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
2806                 memcpy(i.ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
2807                 buf += SWRAP_PACKET_IP_V6_SIZE;
2808                 break;
2809 #endif
2810         }
2811
2812         if (unreachable) {
2813                 pay = (union swrap_packet_payload *)(void *)buf;
2814                 switch (src->sa_family) {
2815                 case AF_INET:
2816                         pay->icmp4.type         = 0x03; /* destination unreachable */
2817                         pay->icmp4.code         = 0x01; /* host unreachable */
2818                         pay->icmp4.checksum     = htons(0x0000);
2819                         pay->icmp4.unused       = htonl(0x00000000);
2820
2821                         buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
2822
2823                         /* set the ip header in the ICMP payload */
2824                         i.ptr = buf;
2825                         i.ip->v4.ver_hdrlen     = 0x45; /* version 4 and 5 * 32 bit words */
2826                         i.ip->v4.tos            = 0x00;
2827                         i.ip->v4.packet_length  = htons(wire_len - icmp_hdr_len);
2828                         i.ip->v4.identification = htons(0xFFFF);
2829                         i.ip->v4.flags          = 0x40; /* BIT 1 set - means don't fragment */
2830                         i.ip->v4.fragment       = htons(0x0000);
2831                         i.ip->v4.ttl            = 0xFF;
2832                         i.ip->v4.protocol       = icmp_protocol;
2833                         i.ip->v4.hdr_checksum   = htons(0x0000);
2834                         i.ip->v4.src_addr       = dest_in->sin_addr.s_addr;
2835                         i.ip->v4.dest_addr      = src_in->sin_addr.s_addr;
2836
2837                         buf += SWRAP_PACKET_IP_V4_SIZE;
2838
2839                         src_port = dest_in->sin_port;
2840                         dest_port = src_in->sin_port;
2841                         break;
2842 #ifdef HAVE_IPV6
2843                 case AF_INET6:
2844                         pay->icmp6.type         = 0x01; /* destination unreachable */
2845                         pay->icmp6.code         = 0x03; /* address unreachable */
2846                         pay->icmp6.checksum     = htons(0x0000);
2847                         pay->icmp6.unused       = htonl(0x00000000);
2848                         buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
2849
2850                         /* set the ip header in the ICMP payload */
2851                         i.ptr = buf;
2852                         i.ip->v6.ver_prio               = 0x60; /* version 4 and 5 * 32 bit words */
2853                         i.ip->v6.flow_label_high        = 0x00;
2854                         i.ip->v6.flow_label_low = 0x0000;
2855                         i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
2856                         i.ip->v6.next_header    = protocol;
2857                         memcpy(i.ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
2858                         memcpy(i.ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
2859
2860                         buf += SWRAP_PACKET_IP_V6_SIZE;
2861
2862                         src_port = dest_in6->sin6_port;
2863                         dest_port = src_in6->sin6_port;
2864                         break;
2865 #endif
2866                 }
2867         }
2868
2869         pay = (union swrap_packet_payload *)(void *)buf;
2870
2871         switch (socket_type) {
2872         case SOCK_STREAM:
2873                 pay->tcp.source_port    = src_port;
2874                 pay->tcp.dest_port      = dest_port;
2875                 pay->tcp.seq_num        = htonl(tcp_seqno);
2876                 pay->tcp.ack_num        = htonl(tcp_ack);
2877                 pay->tcp.hdr_length     = 0x50; /* 5 * 32 bit words */
2878                 pay->tcp.control        = tcp_ctl;
2879                 pay->tcp.window         = htons(0x7FFF);
2880                 pay->tcp.checksum       = htons(0x0000);
2881                 pay->tcp.urg            = htons(0x0000);
2882                 buf += SWRAP_PACKET_PAYLOAD_TCP_SIZE;
2883
2884                 break;
2885
2886         case SOCK_DGRAM:
2887                 pay->udp.source_port    = src_port;
2888                 pay->udp.dest_port      = dest_port;
2889                 pay->udp.length         = htons(8 + payload_len);
2890                 pay->udp.checksum       = htons(0x0000);
2891                 buf += SWRAP_PACKET_PAYLOAD_UDP_SIZE;
2892
2893                 break;
2894         }
2895
2896         if (payload && payload_len > 0) {
2897                 memcpy(buf, payload, payload_len);
2898         }
2899
2900         *_packet_len = packet_len - icmp_truncate_len;
2901         return base;
2902 }
2903
2904 static int swrap_pcap_get_fd(const char *fname)
2905 {
2906         static int fd = -1;
2907
2908         if (fd != -1) {
2909                 return fd;
2910         }
2911
2912         fd = libc_open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644);
2913         if (fd != -1) {
2914                 struct swrap_file_hdr file_hdr;
2915                 file_hdr.magic          = 0xA1B2C3D4;
2916                 file_hdr.version_major  = 0x0002;
2917                 file_hdr.version_minor  = 0x0004;
2918                 file_hdr.timezone       = 0x00000000;
2919                 file_hdr.sigfigs        = 0x00000000;
2920                 file_hdr.frame_max_len  = SWRAP_FRAME_LENGTH_MAX;
2921                 file_hdr.link_type      = 0x0065; /* 101 RAW IP */
2922
2923                 if (write(fd, &file_hdr, sizeof(file_hdr)) != sizeof(file_hdr)) {
2924                         close(fd);
2925                         fd = -1;
2926                 }
2927                 return fd;
2928         }
2929
2930         fd = libc_open(fname, O_WRONLY|O_APPEND, 0644);
2931
2932         return fd;
2933 }
2934
2935 static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
2936                                            const struct sockaddr *addr,
2937                                            enum swrap_packet_type type,
2938                                            const void *buf, size_t len,
2939                                            size_t *packet_len)
2940 {
2941         const struct sockaddr *src_addr;
2942         const struct sockaddr *dest_addr;
2943         unsigned long tcp_seqno = 0;
2944         unsigned long tcp_ack = 0;
2945         unsigned char tcp_ctl = 0;
2946         int unreachable = 0;
2947
2948         struct timeval tv;
2949
2950         switch (si->family) {
2951         case AF_INET:
2952                 break;
2953 #ifdef HAVE_IPV6
2954         case AF_INET6:
2955                 break;
2956 #endif
2957         default:
2958                 return NULL;
2959         }
2960
2961         switch (type) {
2962         case SWRAP_CONNECT_SEND:
2963                 if (si->type != SOCK_STREAM) {
2964                         return NULL;
2965                 }
2966
2967                 src_addr  = &si->myname.sa.s;
2968                 dest_addr = addr;
2969
2970                 tcp_seqno = si->io.pck_snd;
2971                 tcp_ack = si->io.pck_rcv;
2972                 tcp_ctl = 0x02; /* SYN */
2973
2974                 si->io.pck_snd += 1;
2975
2976                 break;
2977
2978         case SWRAP_CONNECT_RECV:
2979                 if (si->type != SOCK_STREAM) {
2980                         return NULL;
2981                 }
2982
2983                 dest_addr = &si->myname.sa.s;
2984                 src_addr = addr;
2985
2986                 tcp_seqno = si->io.pck_rcv;
2987                 tcp_ack = si->io.pck_snd;
2988                 tcp_ctl = 0x12; /** SYN,ACK */
2989
2990                 si->io.pck_rcv += 1;
2991
2992                 break;
2993
2994         case SWRAP_CONNECT_UNREACH:
2995                 if (si->type != SOCK_STREAM) {
2996                         return NULL;
2997                 }
2998
2999                 dest_addr = &si->myname.sa.s;
3000                 src_addr  = addr;
3001
3002                 /* Unreachable: resend the data of SWRAP_CONNECT_SEND */
3003                 tcp_seqno = si->io.pck_snd - 1;
3004                 tcp_ack = si->io.pck_rcv;
3005                 tcp_ctl = 0x02; /* SYN */
3006                 unreachable = 1;
3007
3008                 break;
3009
3010         case SWRAP_CONNECT_ACK:
3011                 if (si->type != SOCK_STREAM) {
3012                         return NULL;
3013                 }
3014
3015                 src_addr  = &si->myname.sa.s;
3016                 dest_addr = addr;
3017
3018                 tcp_seqno = si->io.pck_snd;
3019                 tcp_ack = si->io.pck_rcv;
3020                 tcp_ctl = 0x10; /* ACK */
3021
3022                 break;
3023
3024         case SWRAP_ACCEPT_SEND:
3025                 if (si->type != SOCK_STREAM) {
3026                         return NULL;
3027                 }
3028
3029                 dest_addr = &si->myname.sa.s;
3030                 src_addr = addr;
3031
3032                 tcp_seqno = si->io.pck_rcv;
3033                 tcp_ack = si->io.pck_snd;
3034                 tcp_ctl = 0x02; /* SYN */
3035
3036                 si->io.pck_rcv += 1;
3037
3038                 break;
3039
3040         case SWRAP_ACCEPT_RECV:
3041                 if (si->type != SOCK_STREAM) {
3042                         return NULL;
3043                 }
3044
3045                 src_addr = &si->myname.sa.s;
3046                 dest_addr = addr;
3047
3048                 tcp_seqno = si->io.pck_snd;
3049                 tcp_ack = si->io.pck_rcv;
3050                 tcp_ctl = 0x12; /* SYN,ACK */
3051
3052                 si->io.pck_snd += 1;
3053
3054                 break;
3055
3056         case SWRAP_ACCEPT_ACK:
3057                 if (si->type != SOCK_STREAM) {
3058                         return NULL;
3059                 }
3060
3061                 dest_addr = &si->myname.sa.s;
3062                 src_addr = addr;
3063
3064                 tcp_seqno = si->io.pck_rcv;
3065                 tcp_ack = si->io.pck_snd;
3066                 tcp_ctl = 0x10; /* ACK */
3067
3068                 break;
3069
3070         case SWRAP_SEND:
3071                 src_addr  = &si->myname.sa.s;
3072                 dest_addr = &si->peername.sa.s;
3073
3074                 tcp_seqno = si->io.pck_snd;
3075                 tcp_ack = si->io.pck_rcv;
3076                 tcp_ctl = 0x18; /* PSH,ACK */
3077
3078                 si->io.pck_snd += len;
3079
3080                 break;
3081
3082         case SWRAP_SEND_RST:
3083                 dest_addr = &si->myname.sa.s;
3084                 src_addr  = &si->peername.sa.s;
3085
3086                 if (si->type == SOCK_DGRAM) {
3087                         return swrap_pcap_marshall_packet(si,
3088                                                           &si->peername.sa.s,
3089                                                           SWRAP_SENDTO_UNREACH,
3090                                                           buf,
3091                                                           len,
3092                                                           packet_len);
3093                 }
3094
3095                 tcp_seqno = si->io.pck_rcv;
3096                 tcp_ack = si->io.pck_snd;
3097                 tcp_ctl = 0x14; /** RST,ACK */
3098
3099                 break;
3100
3101         case SWRAP_PENDING_RST:
3102                 dest_addr = &si->myname.sa.s;
3103                 src_addr  = &si->peername.sa.s;
3104
3105                 if (si->type == SOCK_DGRAM) {
3106                         return NULL;
3107                 }
3108
3109                 tcp_seqno = si->io.pck_rcv;
3110                 tcp_ack = si->io.pck_snd;
3111                 tcp_ctl = 0x14; /* RST,ACK */
3112
3113                 break;
3114
3115         case SWRAP_RECV:
3116                 dest_addr = &si->myname.sa.s;
3117                 src_addr  = &si->peername.sa.s;
3118
3119                 tcp_seqno = si->io.pck_rcv;
3120                 tcp_ack = si->io.pck_snd;
3121                 tcp_ctl = 0x18; /* PSH,ACK */
3122
3123                 si->io.pck_rcv += len;
3124
3125                 break;
3126
3127         case SWRAP_RECV_RST:
3128                 dest_addr = &si->myname.sa.s;
3129                 src_addr  = &si->peername.sa.s;
3130
3131                 if (si->type == SOCK_DGRAM) {
3132                         return NULL;
3133                 }
3134
3135                 tcp_seqno = si->io.pck_rcv;
3136                 tcp_ack = si->io.pck_snd;
3137                 tcp_ctl = 0x14; /* RST,ACK */
3138
3139                 break;
3140
3141         case SWRAP_SENDTO:
3142                 src_addr = &si->myname.sa.s;
3143                 dest_addr = addr;
3144
3145                 si->io.pck_snd += len;
3146
3147                 break;
3148
3149         case SWRAP_SENDTO_UNREACH:
3150                 dest_addr = &si->myname.sa.s;
3151                 src_addr = addr;
3152
3153                 unreachable = 1;
3154
3155                 break;
3156
3157         case SWRAP_RECVFROM:
3158                 dest_addr = &si->myname.sa.s;
3159                 src_addr = addr;
3160
3161                 si->io.pck_rcv += len;
3162
3163                 break;
3164
3165         case SWRAP_CLOSE_SEND:
3166                 if (si->type != SOCK_STREAM) {
3167                         return NULL;
3168                 }
3169
3170                 src_addr  = &si->myname.sa.s;
3171                 dest_addr = &si->peername.sa.s;
3172
3173                 tcp_seqno = si->io.pck_snd;
3174                 tcp_ack = si->io.pck_rcv;
3175                 tcp_ctl = 0x11; /* FIN, ACK */
3176
3177                 si->io.pck_snd += 1;
3178
3179                 break;
3180
3181         case SWRAP_CLOSE_RECV:
3182                 if (si->type != SOCK_STREAM) {
3183                         return NULL;
3184                 }
3185
3186                 dest_addr = &si->myname.sa.s;
3187                 src_addr  = &si->peername.sa.s;
3188
3189                 tcp_seqno = si->io.pck_rcv;
3190                 tcp_ack = si->io.pck_snd;
3191                 tcp_ctl = 0x11; /* FIN,ACK */
3192
3193                 si->io.pck_rcv += 1;
3194
3195                 break;
3196
3197         case SWRAP_CLOSE_ACK:
3198                 if (si->type != SOCK_STREAM) {
3199                         return NULL;
3200                 }
3201
3202                 src_addr  = &si->myname.sa.s;
3203                 dest_addr = &si->peername.sa.s;
3204
3205                 tcp_seqno = si->io.pck_snd;
3206                 tcp_ack = si->io.pck_rcv;
3207                 tcp_ctl = 0x10; /* ACK */
3208
3209                 break;
3210         default:
3211                 return NULL;
3212         }
3213
3214         swrapGetTimeOfDay(&tv);
3215
3216         return swrap_pcap_packet_init(&tv,
3217                                       src_addr,
3218                                       dest_addr,
3219                                       si->type,
3220                                       (const uint8_t *)buf,
3221                                       len,
3222                                       tcp_seqno,
3223                                       tcp_ack,
3224                                       tcp_ctl,
3225                                       unreachable,
3226                                       packet_len);
3227 }
3228
3229 static void swrap_pcap_dump_packet(struct socket_info *si,
3230                                    const struct sockaddr *addr,
3231                                    enum swrap_packet_type type,
3232                                    const void *buf, size_t len)
3233 {
3234         const char *file_name;
3235         uint8_t *packet;
3236         size_t packet_len = 0;
3237         int fd;
3238
3239         swrap_mutex_lock(&pcap_dump_mutex);
3240
3241         file_name = swrap_pcap_init_file();
3242         if (!file_name) {
3243                 goto done;
3244         }
3245
3246         packet = swrap_pcap_marshall_packet(si,
3247                                             addr,
3248                                             type,
3249                                             buf,
3250                                             len,
3251                                             &packet_len);
3252         if (packet == NULL) {
3253                 goto done;
3254         }
3255
3256         fd = swrap_pcap_get_fd(file_name);
3257         if (fd != -1) {
3258                 if (write(fd, packet, packet_len) != (ssize_t)packet_len) {
3259                         free(packet);
3260                         goto done;
3261                 }
3262         }
3263
3264         free(packet);
3265
3266 done:
3267         swrap_mutex_unlock(&pcap_dump_mutex);
3268 }
3269
3270 /****************************************************************************
3271  *   SIGNALFD
3272  ***************************************************************************/
3273
3274 #ifdef HAVE_SIGNALFD
3275 static int swrap_signalfd(int fd, const sigset_t *mask, int flags)
3276 {
3277         int rc;
3278
3279         rc = libc_signalfd(fd, mask, flags);
3280         if (rc != -1) {
3281                 swrap_remove_stale(fd);
3282         }
3283
3284         return rc;
3285 }
3286
3287 int signalfd(int fd, const sigset_t *mask, int flags)
3288 {
3289         return swrap_signalfd(fd, mask, flags);
3290 }
3291 #endif
3292
3293 /****************************************************************************
3294  *   SOCKET
3295  ***************************************************************************/
3296
3297 static int swrap_socket(int family, int type, int protocol)
3298 {
3299         struct socket_info *si = NULL;
3300         struct socket_info _si = { 0 };
3301         int fd;
3302         int ret;
3303         int real_type = type;
3304
3305         /*
3306          * Remove possible addition flags passed to socket() so
3307          * do not fail checking the type.
3308          * See https://lwn.net/Articles/281965/
3309          */
3310 #ifdef SOCK_CLOEXEC
3311         real_type &= ~SOCK_CLOEXEC;
3312 #endif
3313 #ifdef SOCK_NONBLOCK
3314         real_type &= ~SOCK_NONBLOCK;
3315 #endif
3316
3317         if (!socket_wrapper_enabled()) {
3318                 return libc_socket(family, type, protocol);
3319         }
3320
3321         switch (family) {
3322         case AF_INET:
3323 #ifdef HAVE_IPV6
3324         case AF_INET6:
3325 #endif
3326                 break;
3327 #ifdef AF_NETLINK
3328         case AF_NETLINK:
3329 #endif /* AF_NETLINK */
3330 #ifdef AF_PACKET
3331         case AF_PACKET:
3332 #endif /* AF_PACKET */
3333         case AF_UNIX:
3334                 fd = libc_socket(family, type, protocol);
3335                 if (fd != -1) {
3336                         /* Check if we have a stale fd and remove it */
3337                         swrap_remove_stale(fd);
3338                         SWRAP_LOG(SWRAP_LOG_TRACE,
3339                                   "Unix socket fd=%d",
3340                                   fd);
3341                 }
3342                 return fd;
3343         default:
3344                 errno = EAFNOSUPPORT;
3345                 return -1;
3346         }
3347
3348         switch (real_type) {
3349         case SOCK_STREAM:
3350                 break;
3351         case SOCK_DGRAM:
3352                 break;
3353         default:
3354                 errno = EPROTONOSUPPORT;
3355                 return -1;
3356         }
3357
3358         switch (protocol) {
3359         case 0:
3360                 break;
3361         case 6:
3362                 if (real_type == SOCK_STREAM) {
3363                         break;
3364                 }
3365                 FALL_THROUGH;
3366         case 17:
3367                 if (real_type == SOCK_DGRAM) {
3368                         break;
3369                 }
3370                 FALL_THROUGH;
3371         default:
3372                 errno = EPROTONOSUPPORT;
3373                 return -1;
3374         }
3375
3376         /*
3377          * We must call libc_socket with type, from the caller, not the version
3378          * we removed SOCK_CLOEXEC and SOCK_NONBLOCK from
3379          */
3380         fd = libc_socket(AF_UNIX, type, 0);
3381
3382         if (fd == -1) {
3383                 return -1;
3384         }
3385
3386         /* Check if we have a stale fd and remove it */
3387         swrap_remove_stale(fd);
3388
3389         si = &_si;
3390         si->family = family;
3391
3392         /* however, the rest of the socket_wrapper code expects just
3393          * the type, not the flags */
3394         si->type = real_type;
3395         si->protocol = protocol;
3396
3397         /*
3398          * Setup myname so getsockname() can succeed to find out the socket
3399          * type.
3400          */
3401         switch(si->family) {
3402         case AF_INET: {
3403                 struct sockaddr_in sin = {
3404                         .sin_family = AF_INET,
3405                 };
3406
3407                 si->myname.sa_socklen = sizeof(struct sockaddr_in);
3408                 memcpy(&si->myname.sa.in, &sin, si->myname.sa_socklen);
3409                 break;
3410         }
3411 #ifdef HAVE_IPV6
3412         case AF_INET6: {
3413                 struct sockaddr_in6 sin6 = {
3414                         .sin6_family = AF_INET6,
3415                 };
3416
3417                 si->myname.sa_socklen = sizeof(struct sockaddr_in6);
3418                 memcpy(&si->myname.sa.in6, &sin6, si->myname.sa_socklen);
3419                 break;
3420         }
3421 #endif
3422         default:
3423                 errno = EINVAL;
3424                 return -1;
3425         }
3426
3427         ret = swrap_create_socket(si, fd);
3428         if (ret == -1) {
3429                 return -1;
3430         }
3431
3432         SWRAP_LOG(SWRAP_LOG_TRACE,
3433                   "Created %s socket for protocol %s, fd=%d",
3434                   family == AF_INET ? "IPv4" : "IPv6",
3435                   real_type == SOCK_DGRAM ? "UDP" : "TCP",
3436                   fd);
3437
3438         return fd;
3439 }
3440
3441 int socket(int family, int type, int protocol)
3442 {
3443         return swrap_socket(family, type, protocol);
3444 }
3445
3446 /****************************************************************************
3447  *   SOCKETPAIR
3448  ***************************************************************************/
3449
3450 static int swrap_socketpair(int family, int type, int protocol, int sv[2])
3451 {
3452         int rc;
3453
3454         rc = libc_socketpair(family, type, protocol, sv);
3455         if (rc != -1) {
3456                 swrap_remove_stale(sv[0]);
3457                 swrap_remove_stale(sv[1]);
3458         }
3459
3460         return rc;
3461 }
3462
3463 int socketpair(int family, int type, int protocol, int sv[2])
3464 {
3465         return swrap_socketpair(family, type, protocol, sv);
3466 }
3467
3468 /****************************************************************************
3469  *   SOCKETPAIR
3470  ***************************************************************************/
3471
3472 #ifdef HAVE_TIMERFD_CREATE
3473 static int swrap_timerfd_create(int clockid, int flags)
3474 {
3475         int fd;
3476
3477         fd = libc_timerfd_create(clockid, flags);
3478         if (fd != -1) {
3479                 swrap_remove_stale(fd);
3480         }
3481
3482         return fd;
3483 }
3484
3485 int timerfd_create(int clockid, int flags)
3486 {
3487         return swrap_timerfd_create(clockid, flags);
3488 }
3489 #endif
3490
3491 /****************************************************************************
3492  *   PIPE
3493  ***************************************************************************/
3494
3495 static int swrap_pipe(int pipefd[2])
3496 {
3497         int rc;
3498
3499         rc = libc_pipe(pipefd);
3500         if (rc != -1) {
3501                 swrap_remove_stale(pipefd[0]);
3502                 swrap_remove_stale(pipefd[1]);
3503         }
3504
3505         return rc;
3506 }
3507
3508 int pipe(int pipefd[2])
3509 {
3510         return swrap_pipe(pipefd);
3511 }
3512
3513 /****************************************************************************
3514  *   ACCEPT
3515  ***************************************************************************/
3516
3517 static int swrap_accept(int s,
3518                         struct sockaddr *addr,
3519                         socklen_t *addrlen,
3520                         int flags)
3521 {
3522         struct socket_info *parent_si, *child_si;
3523         struct socket_info new_si = { 0 };
3524         int fd;
3525         int idx;
3526         struct swrap_address un_addr = {
3527                 .sa_socklen = sizeof(struct sockaddr_un),
3528         };
3529         struct swrap_address un_my_addr = {
3530                 .sa_socklen = sizeof(struct sockaddr_un),
3531         };
3532         struct swrap_address in_addr = {
3533                 .sa_socklen = sizeof(struct sockaddr_storage),
3534         };
3535         struct swrap_address in_my_addr = {
3536                 .sa_socklen = sizeof(struct sockaddr_storage),
3537         };
3538         int ret;
3539
3540         parent_si = find_socket_info(s);
3541         if (!parent_si) {
3542 #ifdef HAVE_ACCEPT4
3543                 return libc_accept4(s, addr, addrlen, flags);
3544 #else
3545                 UNUSED(flags);
3546                 return libc_accept(s, addr, addrlen);
3547 #endif
3548         }
3549
3550
3551         /*
3552          * prevent parent_si from being altered / closed
3553          * while we read it
3554          */
3555         SWRAP_LOCK_SI(parent_si);
3556
3557         /*
3558          * assume out sockaddr have the same size as the in parent
3559          * socket family
3560          */
3561         in_addr.sa_socklen = socket_length(parent_si->family);
3562         if (in_addr.sa_socklen <= 0) {
3563                 SWRAP_UNLOCK_SI(parent_si);
3564                 errno = EINVAL;
3565                 return -1;
3566         }
3567
3568         SWRAP_UNLOCK_SI(parent_si);
3569
3570 #ifdef HAVE_ACCEPT4
3571         ret = libc_accept4(s, &un_addr.sa.s, &un_addr.sa_socklen, flags);
3572 #else
3573         UNUSED(flags);
3574         ret = libc_accept(s, &un_addr.sa.s, &un_addr.sa_socklen);
3575 #endif
3576         if (ret == -1) {
3577                 if (errno == ENOTSOCK) {
3578                         /* Remove stale fds */
3579                         swrap_remove_stale(s);
3580                 }
3581                 return ret;
3582         }
3583
3584         fd = ret;
3585
3586         /* Check if we have a stale fd and remove it */
3587         swrap_remove_stale(fd);
3588
3589         SWRAP_LOCK_SI(parent_si);
3590
3591         ret = sockaddr_convert_from_un(parent_si,
3592                                        &un_addr.sa.un,
3593                                        un_addr.sa_socklen,
3594                                        parent_si->family,
3595                                        &in_addr.sa.s,
3596                                        &in_addr.sa_socklen);
3597         if (ret == -1) {
3598                 SWRAP_UNLOCK_SI(parent_si);
3599                 close(fd);
3600                 return ret;
3601         }
3602
3603         child_si = &new_si;
3604
3605         child_si->family = parent_si->family;
3606         child_si->type = parent_si->type;
3607         child_si->protocol = parent_si->protocol;
3608         child_si->bound = 1;
3609         child_si->is_server = 1;
3610         child_si->connected = 1;
3611
3612         SWRAP_UNLOCK_SI(parent_si);
3613
3614         child_si->peername = (struct swrap_address) {
3615                 .sa_socklen = in_addr.sa_socklen,
3616         };
3617         memcpy(&child_si->peername.sa.ss, &in_addr.sa.ss, in_addr.sa_socklen);
3618
3619         if (addr != NULL && addrlen != NULL) {
3620                 size_t copy_len = MIN(*addrlen, in_addr.sa_socklen);
3621                 if (copy_len > 0) {
3622                         memcpy(addr, &in_addr.sa.ss, copy_len);
3623                 }
3624                 *addrlen = in_addr.sa_socklen;
3625         }
3626
3627         ret = libc_getsockname(fd,
3628                                &un_my_addr.sa.s,
3629                                &un_my_addr.sa_socklen);
3630         if (ret == -1) {
3631                 close(fd);
3632                 return ret;
3633         }
3634
3635         ret = sockaddr_convert_from_un(child_si,
3636                                        &un_my_addr.sa.un,
3637                                        un_my_addr.sa_socklen,
3638                                        child_si->family,
3639                                        &in_my_addr.sa.s,
3640                                        &in_my_addr.sa_socklen);
3641         if (ret == -1) {
3642                 close(fd);
3643                 return ret;
3644         }
3645
3646         SWRAP_LOG(SWRAP_LOG_TRACE,
3647                   "accept() path=%s, fd=%d",
3648                   un_my_addr.sa.un.sun_path, s);
3649
3650         child_si->myname = (struct swrap_address) {
3651                 .sa_socklen = in_my_addr.sa_socklen,
3652         };
3653         memcpy(&child_si->myname.sa.ss, &in_my_addr.sa.ss, in_my_addr.sa_socklen);
3654
3655         idx = swrap_create_socket(&new_si, fd);
3656         if (idx == -1) {
3657                 close (fd);
3658                 return -1;
3659         }
3660
3661         if (addr != NULL) {
3662                 struct socket_info *si = swrap_get_socket_info(idx);
3663
3664                 SWRAP_LOCK_SI(si);
3665                 swrap_pcap_dump_packet(si, addr, SWRAP_ACCEPT_SEND, NULL, 0);
3666                 swrap_pcap_dump_packet(si, addr, SWRAP_ACCEPT_RECV, NULL, 0);
3667                 swrap_pcap_dump_packet(si, addr, SWRAP_ACCEPT_ACK, NULL, 0);
3668                 SWRAP_UNLOCK_SI(si);
3669         }
3670
3671         return fd;
3672 }
3673
3674 #ifdef HAVE_ACCEPT4
3675 int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
3676 {
3677         return swrap_accept(s, addr, (socklen_t *)addrlen, flags);
3678 }
3679 #endif
3680
3681 #ifdef HAVE_ACCEPT_PSOCKLEN_T
3682 int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)
3683 #else
3684 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
3685 #endif
3686 {
3687         return swrap_accept(s, addr, (socklen_t *)addrlen, 0);
3688 }
3689
3690 static int autobind_start_init;
3691 static int autobind_start;
3692
3693 /* using sendto() or connect() on an unbound socket would give the
3694    recipient no way to reply, as unlike UDP and TCP, a unix domain
3695    socket can't auto-assign ephemeral port numbers, so we need to
3696    assign it here.
3697    Note: this might change the family from ipv6 to ipv4
3698 */
3699 static int swrap_auto_bind(int fd, struct socket_info *si, int family)
3700 {
3701         struct swrap_address un_addr = {
3702                 .sa_socklen = sizeof(struct sockaddr_un),
3703         };
3704         int i;
3705         char type;
3706         int ret;
3707         int port;
3708         struct stat st;
3709         char *swrap_dir = NULL;
3710
3711         swrap_mutex_lock(&autobind_start_mutex);
3712
3713         if (autobind_start_init != 1) {
3714                 autobind_start_init = 1;
3715                 autobind_start = getpid();
3716                 autobind_start %= 50000;
3717                 autobind_start += 10000;
3718         }
3719
3720         un_addr.sa.un.sun_family = AF_UNIX;
3721
3722         switch (family) {
3723         case AF_INET: {
3724                 struct sockaddr_in in;
3725
3726                 switch (si->type) {
3727                 case SOCK_STREAM:
3728                         type = SOCKET_TYPE_CHAR_TCP;
3729                         break;
3730                 case SOCK_DGRAM:
3731                         type = SOCKET_TYPE_CHAR_UDP;
3732                         break;
3733                 default:
3734                         errno = ESOCKTNOSUPPORT;
3735                         ret = -1;
3736                         goto done;
3737                 }
3738
3739                 memset(&in, 0, sizeof(in));
3740                 in.sin_family = AF_INET;
3741                 in.sin_addr.s_addr = htonl(swrap_ipv4_iface(
3742                                            socket_wrapper_default_iface()));
3743
3744                 si->myname = (struct swrap_address) {
3745                         .sa_socklen = sizeof(in),
3746                 };
3747                 memcpy(&si->myname.sa.in, &in, si->myname.sa_socklen);
3748                 break;
3749         }
3750 #ifdef HAVE_IPV6
3751         case AF_INET6: {
3752                 struct sockaddr_in6 in6;
3753
3754                 if (si->family != family) {
3755                         errno = ENETUNREACH;
3756                         ret = -1;
3757                         goto done;
3758                 }
3759
3760                 switch (si->type) {
3761                 case SOCK_STREAM:
3762                         type = SOCKET_TYPE_CHAR_TCP_V6;
3763                         break;
3764                 case SOCK_DGRAM:
3765                         type = SOCKET_TYPE_CHAR_UDP_V6;
3766                         break;
3767                 default:
3768                         errno = ESOCKTNOSUPPORT;
3769                         ret = -1;
3770                         goto done;
3771                 }
3772
3773                 memset(&in6, 0, sizeof(in6));
3774                 in6.sin6_family = AF_INET6;
3775                 in6.sin6_addr = *swrap_ipv6();
3776                 in6.sin6_addr.s6_addr[15] = socket_wrapper_default_iface();
3777
3778                 si->myname = (struct swrap_address) {
3779                         .sa_socklen = sizeof(in6),
3780                 };
3781                 memcpy(&si->myname.sa.in6, &in6, si->myname.sa_socklen);
3782                 break;
3783         }
3784 #endif
3785         default:
3786                 errno = ESOCKTNOSUPPORT;
3787                 ret = -1;
3788                 goto done;
3789         }
3790
3791         if (autobind_start > 60000) {
3792                 autobind_start = 10000;
3793         }
3794
3795         swrap_dir = socket_wrapper_dir();
3796         if (swrap_dir == NULL) {
3797                 errno = EINVAL;
3798                 ret = -1;
3799                 goto done;
3800         }
3801
3802         for (i = 0; i < SOCKET_MAX_SOCKETS; i++) {
3803                 port = autobind_start + i;
3804                 swrap_un_path(&un_addr.sa.un,
3805                               swrap_dir,
3806                               type,
3807                               socket_wrapper_default_iface(),
3808                               port);
3809                 if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
3810
3811                 ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
3812                 if (ret == -1) {
3813                         goto done;
3814                 }
3815
3816                 si->un_addr = un_addr.sa.un;
3817
3818                 si->bound = 1;
3819                 autobind_start = port + 1;
3820                 break;
3821         }
3822         if (i == SOCKET_MAX_SOCKETS) {
3823                 SWRAP_LOG(SWRAP_LOG_ERROR, "Too many open unix sockets (%u) for "
3824                                            "interface "SOCKET_FORMAT,
3825                                            SOCKET_MAX_SOCKETS,
3826                                            type,
3827                                            socket_wrapper_default_iface(),
3828                                            0);
3829                 errno = ENFILE;
3830                 ret = -1;
3831                 goto done;
3832         }
3833
3834         si->family = family;
3835         set_port(si->family, port, &si->myname);
3836
3837         ret = 0;
3838
3839 done:
3840         SAFE_FREE(swrap_dir);
3841         swrap_mutex_unlock(&autobind_start_mutex);
3842         return ret;
3843 }
3844
3845 /****************************************************************************
3846  *   CONNECT
3847  ***************************************************************************/
3848
3849 static int swrap_connect(int s, const struct sockaddr *serv_addr,
3850                          socklen_t addrlen)
3851 {
3852         int ret;
3853         struct swrap_address un_addr = {
3854                 .sa_socklen = sizeof(struct sockaddr_un),
3855         };
3856         struct socket_info *si = find_socket_info(s);
3857         int bcast = 0;
3858
3859         if (!si) {
3860                 return libc_connect(s, serv_addr, addrlen);
3861         }
3862
3863         SWRAP_LOCK_SI(si);
3864
3865         if (si->bound == 0) {
3866                 ret = swrap_auto_bind(s, si, serv_addr->sa_family);
3867                 if (ret == -1) {
3868                         goto done;
3869                 }
3870         }
3871
3872         if (si->family != serv_addr->sa_family) {
3873                 SWRAP_LOG(SWRAP_LOG_ERROR,
3874                           "called for fd=%d (family=%d) called with invalid family=%d",
3875                           s, si->family, serv_addr->sa_family);
3876                 errno = EINVAL;
3877                 ret = -1;
3878                 goto done;
3879         }
3880
3881         ret = sockaddr_convert_to_un(si, serv_addr,
3882                                      addrlen, &un_addr.sa.un, 0, &bcast);
3883         if (ret == -1) {
3884                 goto done;
3885         }
3886
3887         if (bcast) {
3888                 errno = ENETUNREACH;
3889                 ret = -1;
3890                 goto done;
3891         }
3892
3893         if (si->type == SOCK_DGRAM) {
3894                 si->defer_connect = 1;
3895                 ret = 0;
3896         } else {
3897                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_SEND, NULL, 0);
3898
3899                 ret = libc_connect(s,
3900                                    &un_addr.sa.s,
3901                                    un_addr.sa_socklen);
3902         }
3903
3904         SWRAP_LOG(SWRAP_LOG_TRACE,
3905                   "connect() path=%s, fd=%d",
3906                   un_addr.sa.un.sun_path, s);
3907
3908
3909         /* to give better errors */
3910         if (ret == -1 && errno == ENOENT) {
3911                 errno = EHOSTUNREACH;
3912         }
3913
3914         if (ret == 0) {
3915                 si->peername = (struct swrap_address) {
3916                         .sa_socklen = addrlen,
3917                 };
3918
3919                 memcpy(&si->peername.sa.ss, serv_addr, addrlen);
3920                 si->connected = 1;
3921
3922                 /*
3923                  * When we connect() on a socket than we have to bind the
3924                  * outgoing connection on the interface we use for the
3925                  * transport. We already bound it on the right interface
3926                  * but here we have to update the name so getsockname()
3927                  * returns correct information.
3928                  */
3929                 if (si->bindname.sa_socklen > 0) {
3930                         si->myname = (struct swrap_address) {
3931                                 .sa_socklen = si->bindname.sa_socklen,
3932                         };
3933
3934                         memcpy(&si->myname.sa.ss,
3935                                &si->bindname.sa.ss,
3936                                si->bindname.sa_socklen);
3937
3938                         /* Cleanup bindname */
3939                         si->bindname = (struct swrap_address) {
3940                                 .sa_socklen = 0,
3941                         };
3942                 }
3943
3944                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_RECV, NULL, 0);
3945                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_ACK, NULL, 0);
3946         } else {
3947                 swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_UNREACH, NULL, 0);
3948         }
3949
3950 done:
3951         SWRAP_UNLOCK_SI(si);
3952         return ret;
3953 }
3954
3955 int connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
3956 {
3957         return swrap_connect(s, serv_addr, addrlen);
3958 }
3959
3960 /****************************************************************************
3961  *   BIND
3962  ***************************************************************************/
3963
3964 static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
3965 {
3966         int ret;
3967         struct swrap_address un_addr = {
3968                 .sa_socklen = sizeof(struct sockaddr_un),
3969         };
3970         struct socket_info *si = find_socket_info(s);
3971         int bind_error = 0;
3972 #if 0 /* FIXME */
3973         bool in_use;
3974 #endif
3975
3976         if (!si) {
3977                 return libc_bind(s, myaddr, addrlen);
3978         }
3979
3980         SWRAP_LOCK_SI(si);
3981
3982         switch (si->family) {
3983         case AF_INET: {
3984                 const struct sockaddr_in *sin;
3985                 if (addrlen < sizeof(struct sockaddr_in)) {
3986                         bind_error = EINVAL;
3987                         break;
3988                 }
3989
3990                 sin = (const struct sockaddr_in *)(const void *)myaddr;
3991
3992                 if (sin->sin_family != AF_INET) {
3993                         bind_error = EAFNOSUPPORT;
3994                 }
3995
3996                 /* special case for AF_UNSPEC */
3997                 if (sin->sin_family == AF_UNSPEC &&
3998                     (sin->sin_addr.s_addr == htonl(INADDR_ANY)))
3999                 {
4000                         bind_error = 0;
4001                 }
4002
4003                 break;
4004         }
4005 #ifdef HAVE_IPV6
4006         case AF_INET6: {
4007                 const struct sockaddr_in6 *sin6;
4008                 if (addrlen < sizeof(struct sockaddr_in6)) {
4009                         bind_error = EINVAL;
4010                         break;
4011                 }
4012
4013                 sin6 = (const struct sockaddr_in6 *)(const void *)myaddr;
4014
4015                 if (sin6->sin6_family != AF_INET6) {
4016                         bind_error = EAFNOSUPPORT;
4017                 }
4018
4019                 break;
4020         }
4021 #endif
4022         default:
4023                 bind_error = EINVAL;
4024                 break;
4025         }
4026
4027         if (bind_error != 0) {
4028                 errno = bind_error;
4029                 ret = -1;
4030                 goto out;
4031         }
4032
4033 #if 0 /* FIXME */
4034         in_use = check_addr_port_in_use(myaddr, addrlen);
4035         if (in_use) {
4036                 errno = EADDRINUSE;
4037                 ret = -1;
4038                 goto out;
4039         }
4040 #endif
4041
4042         si->myname.sa_socklen = addrlen;
4043         memcpy(&si->myname.sa.ss, myaddr, addrlen);
4044
4045         ret = sockaddr_convert_to_un(si,
4046                                      myaddr,
4047                                      addrlen,
4048                                      &un_addr.sa.un,
4049                                      1,
4050                                      &si->bcast);
4051         if (ret == -1) {
4052                 goto out;
4053         }
4054
4055         unlink(un_addr.sa.un.sun_path);
4056
4057         ret = libc_bind(s, &un_addr.sa.s, un_addr.sa_socklen);
4058
4059         SWRAP_LOG(SWRAP_LOG_TRACE,
4060                   "bind() path=%s, fd=%d",
4061                   un_addr.sa.un.sun_path, s);
4062
4063         if (ret == 0) {
4064                 si->bound = 1;
4065         }
4066
4067 out:
4068         SWRAP_UNLOCK_SI(si);
4069
4070         return ret;
4071 }
4072
4073 int bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
4074 {
4075         return swrap_bind(s, myaddr, addrlen);
4076 }
4077
4078 /****************************************************************************
4079  *   BINDRESVPORT
4080  ***************************************************************************/
4081
4082 #ifdef HAVE_BINDRESVPORT
4083 static int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen);
4084
4085 static int swrap_bindresvport_sa(int sd, struct sockaddr *sa)
4086 {
4087         struct swrap_address myaddr = {
4088                 .sa_socklen = sizeof(struct sockaddr_storage),
4089         };
4090         socklen_t salen;
4091         static uint16_t port;
4092         uint16_t i;
4093         int rc = -1;
4094         int af;
4095
4096 #define SWRAP_STARTPORT 600
4097 #define SWRAP_ENDPORT (IPPORT_RESERVED - 1)
4098 #define SWRAP_NPORTS (SWRAP_ENDPORT - SWRAP_STARTPORT + 1)
4099
4100         if (port == 0) {
4101                 port = (getpid() % SWRAP_NPORTS) + SWRAP_STARTPORT;
4102         }
4103
4104         if (sa == NULL) {
4105                 salen = myaddr.sa_socklen;
4106                 sa = &myaddr.sa.s;
4107
4108                 rc = swrap_getsockname(sd, &myaddr.sa.s, &salen);
4109                 if (rc < 0) {
4110                         return -1;
4111                 }
4112
4113                 af = sa->sa_family;
4114                 memset(&myaddr.sa.ss, 0, salen);
4115         } else {
4116                 af = sa->sa_family;
4117         }
4118
4119         for (i = 0; i < SWRAP_NPORTS; i++, port++) {
4120                 switch(af) {
4121                 case AF_INET: {
4122                         struct sockaddr_in *sinp = (struct sockaddr_in *)(void *)sa;
4123
4124                         salen = sizeof(struct sockaddr_in);
4125                         sinp->sin_port = htons(port);
4126                         break;
4127                 }
4128                 case AF_INET6: {
4129                         struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *)(void *)sa;
4130
4131                         salen = sizeof(struct sockaddr_in6);
4132                         sin6p->sin6_port = htons(port);
4133                         break;
4134                 }
4135                 default:
4136                         errno = EAFNOSUPPORT;
4137                         return -1;
4138                 }
4139                 sa->sa_family = af;
4140
4141                 if (port > SWRAP_ENDPORT) {
4142                         port = SWRAP_STARTPORT;
4143                 }
4144
4145                 rc = swrap_bind(sd, (struct sockaddr *)sa, salen);
4146                 if (rc == 0 || errno != EADDRINUSE) {
4147                         break;
4148                 }
4149         }
4150
4151         return rc;
4152 }
4153
4154 int bindresvport(int sockfd, struct sockaddr_in *sinp)
4155 {
4156         return swrap_bindresvport_sa(sockfd, (struct sockaddr *)sinp);
4157 }
4158 #endif
4159
4160 /****************************************************************************
4161  *   LISTEN
4162  ***************************************************************************/
4163
4164 static int swrap_listen(int s, int backlog)
4165 {
4166         int ret;
4167         struct socket_info *si = find_socket_info(s);
4168
4169         if (!si) {
4170                 return libc_listen(s, backlog);
4171         }
4172
4173         SWRAP_LOCK_SI(si);
4174
4175         if (si->bound == 0) {
4176                 ret = swrap_auto_bind(s, si, si->family);
4177                 if (ret == -1) {
4178                         errno = EADDRINUSE;
4179                         goto out;
4180                 }
4181         }
4182
4183         ret = libc_listen(s, backlog);
4184         if (ret == 0) {
4185                 si->listening = 1;
4186         }
4187
4188 out:
4189         SWRAP_UNLOCK_SI(si);
4190
4191         return ret;
4192 }
4193
4194 int listen(int s, int backlog)
4195 {
4196         return swrap_listen(s, backlog);
4197 }
4198
4199 /****************************************************************************
4200  *   FOPEN
4201  ***************************************************************************/
4202
4203 static FILE *swrap_fopen(const char *name, const char *mode)
4204 {
4205         FILE *fp;
4206
4207         fp = libc_fopen(name, mode);
4208         if (fp != NULL) {
4209                 int fd = fileno(fp);
4210
4211                 swrap_remove_stale(fd);
4212         }
4213
4214         return fp;
4215 }
4216
4217 FILE *fopen(const char *name, const char *mode)
4218 {
4219         return swrap_fopen(name, mode);
4220 }
4221
4222 /****************************************************************************
4223  *   FOPEN64
4224  ***************************************************************************/
4225
4226 #ifdef HAVE_FOPEN64
4227 static FILE *swrap_fopen64(const char *name, const char *mode)
4228 {
4229         FILE *fp;
4230
4231         fp = libc_fopen64(name, mode);
4232         if (fp != NULL) {
4233                 int fd = fileno(fp);
4234
4235                 swrap_remove_stale(fd);
4236         }
4237
4238         return fp;
4239 }
4240
4241 FILE *fopen64(const char *name, const char *mode)
4242 {
4243         return swrap_fopen64(name, mode);
4244 }
4245 #endif /* HAVE_FOPEN64 */
4246
4247 /****************************************************************************
4248  *   OPEN
4249  ***************************************************************************/
4250
4251 static int swrap_vopen(const char *pathname, int flags, va_list ap)
4252 {
4253         int ret;
4254
4255         ret = libc_vopen(pathname, flags, ap);
4256         if (ret != -1) {
4257                 /*
4258                  * There are methods for closing descriptors (libc-internal code
4259                  * paths, direct syscalls) which close descriptors in ways that
4260                  * we can't intercept, so try to recover when we notice that
4261                  * that's happened
4262                  */
4263                 swrap_remove_stale(ret);
4264         }
4265         return ret;
4266 }
4267
4268 int open(const char *pathname, int flags, ...)
4269 {
4270         va_list ap;
4271         int fd;
4272
4273         va_start(ap, flags);
4274         fd = swrap_vopen(pathname, flags, ap);
4275         va_end(ap);
4276
4277         return fd;
4278 }
4279
4280 /****************************************************************************
4281  *   OPEN64
4282  ***************************************************************************/
4283
4284 #ifdef HAVE_OPEN64
4285 static int swrap_vopen64(const char *pathname, int flags, va_list ap)
4286 {
4287         int ret;
4288
4289         ret = libc_vopen64(pathname, flags, ap);
4290         if (ret != -1) {
4291                 /*
4292                  * There are methods for closing descriptors (libc-internal code
4293                  * paths, direct syscalls) which close descriptors in ways that
4294                  * we can't intercept, so try to recover when we notice that
4295                  * that's happened
4296                  */
4297                 swrap_remove_stale(ret);
4298         }
4299         return ret;
4300 }
4301
4302 int open64(const char *pathname, int flags, ...)
4303 {
4304         va_list ap;
4305         int fd;
4306
4307         va_start(ap, flags);
4308         fd = swrap_vopen64(pathname, flags, ap);
4309         va_end(ap);
4310
4311         return fd;
4312 }
4313 #endif /* HAVE_OPEN64 */
4314
4315 /****************************************************************************
4316  *   OPENAT
4317  ***************************************************************************/
4318
4319 static int swrap_vopenat(int dirfd, const char *path, int flags, va_list ap)
4320 {
4321         int ret;
4322
4323         ret = libc_vopenat(dirfd, path, flags, ap);
4324         if (ret != -1) {
4325                 /*
4326                  * There are methods for closing descriptors (libc-internal code
4327                  * paths, direct syscalls) which close descriptors in ways that
4328                  * we can't intercept, so try to recover when we notice that
4329                  * that's happened
4330                  */
4331                 swrap_remove_stale(ret);
4332         }
4333
4334         return ret;
4335 }
4336
4337 int openat(int dirfd, const char *path, int flags, ...)
4338 {
4339         va_list ap;
4340         int fd;
4341
4342         va_start(ap, flags);
4343         fd = swrap_vopenat(dirfd, path, flags, ap);
4344         va_end(ap);
4345
4346         return fd;
4347 }
4348
4349 /****************************************************************************
4350  *   GETPEERNAME
4351  ***************************************************************************/
4352
4353 static int swrap_getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
4354 {
4355         struct socket_info *si = find_socket_info(s);
4356         socklen_t len;
4357         int ret = -1;
4358
4359         if (!si) {
4360                 return libc_getpeername(s, name, addrlen);
4361         }
4362
4363         SWRAP_LOCK_SI(si);
4364
4365         if (si->peername.sa_socklen == 0)
4366         {
4367                 errno = ENOTCONN;
4368                 goto out;
4369         }
4370
4371         len = MIN(*addrlen, si->peername.sa_socklen);
4372         if (len == 0) {
4373                 ret = 0;
4374                 goto out;
4375         }
4376
4377         memcpy(name, &si->peername.sa.ss, len);
4378         *addrlen = si->peername.sa_socklen;
4379
4380         ret = 0;
4381 out:
4382         SWRAP_UNLOCK_SI(si);
4383
4384         return ret;
4385 }
4386
4387 #ifdef HAVE_ACCEPT_PSOCKLEN_T
4388 int getpeername(int s, struct sockaddr *name, Psocklen_t addrlen)
4389 #else
4390 int getpeername(int s, struct sockaddr *name, socklen_t *addrlen)
4391 #endif
4392 {
4393         return swrap_getpeername(s, name, (socklen_t *)addrlen);
4394 }
4395
4396 /****************************************************************************
4397  *   GETSOCKNAME
4398  ***************************************************************************/
4399
4400 static int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
4401 {
4402         struct socket_info *si = find_socket_info(s);
4403         socklen_t len;
4404         int ret = -1;
4405
4406         if (!si) {
4407                 return libc_getsockname(s, name, addrlen);
4408         }
4409
4410         SWRAP_LOCK_SI(si);
4411
4412         len = MIN(*addrlen, si->myname.sa_socklen);
4413         if (len == 0) {
4414                 ret = 0;
4415                 goto out;
4416         }
4417
4418         memcpy(name, &si->myname.sa.ss, len);
4419         *addrlen = si->myname.sa_socklen;
4420
4421         ret = 0;
4422 out:
4423         SWRAP_UNLOCK_SI(si);
4424
4425         return ret;
4426 }
4427
4428 #ifdef HAVE_ACCEPT_PSOCKLEN_T
4429 int getsockname(int s, struct sockaddr *name, Psocklen_t addrlen)
4430 #else
4431 int getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
4432 #endif
4433 {
4434         return swrap_getsockname(s, name, (socklen_t *)addrlen);
4435 }
4436
4437 /****************************************************************************
4438  *   GETSOCKOPT
4439  ***************************************************************************/
4440
4441 #ifndef SO_PROTOCOL
4442 # ifdef SO_PROTOTYPE /* The Solaris name */
4443 #  define SO_PROTOCOL SO_PROTOTYPE
4444 # endif /* SO_PROTOTYPE */
4445 #endif /* SO_PROTOCOL */
4446
4447 static int swrap_getsockopt(int s, int level, int optname,
4448                             void *optval, socklen_t *optlen)
4449 {
4450         struct socket_info *si = find_socket_info(s);
4451         int ret;
4452
4453         if (!si) {
4454                 return libc_getsockopt(s,
4455                                        level,
4456                                        optname,
4457                                        optval,
4458                                        optlen);
4459         }
4460
4461         SWRAP_LOCK_SI(si);
4462
4463         if (level == SOL_SOCKET) {
4464                 switch (optname) {
4465 #ifdef SO_DOMAIN
4466                 case SO_DOMAIN:
4467                         if (optval == NULL || optlen == NULL ||
4468                             *optlen < (socklen_t)sizeof(int)) {
4469                                 errno = EINVAL;
4470                                 ret = -1;
4471                                 goto done;
4472                         }
4473
4474                         *optlen = sizeof(int);
4475                         *(int *)optval = si->family;
4476                         ret = 0;
4477                         goto done;
4478 #endif /* SO_DOMAIN */
4479
4480 #ifdef SO_PROTOCOL
4481                 case SO_PROTOCOL:
4482                         if (optval == NULL || optlen == NULL ||
4483                             *optlen < (socklen_t)sizeof(int)) {
4484                                 errno = EINVAL;
4485                                 ret = -1;
4486                                 goto done;
4487                         }
4488
4489                         *optlen = sizeof(int);
4490                         *(int *)optval = si->protocol;
4491                         ret = 0;
4492                         goto done;
4493 #endif /* SO_PROTOCOL */
4494                 case SO_TYPE:
4495                         if (optval == NULL || optlen == NULL ||
4496                             *optlen < (socklen_t)sizeof(int)) {
4497                                 errno = EINVAL;
4498                                 ret = -1;
4499                                 goto done;
4500                         }
4501
4502                         *optlen = sizeof(int);
4503                         *(int *)optval = si->type;
4504                         ret = 0;
4505                         goto done;
4506                 default:
4507                         ret = libc_getsockopt(s,
4508                                               level,
4509                                               optname,
4510                                               optval,
4511                                               optlen);
4512                         goto done;
4513                 }
4514         } else if (level == IPPROTO_TCP) {
4515                 switch (optname) {
4516 #ifdef TCP_NODELAY
4517                 case TCP_NODELAY:
4518                         /*
4519                          * This enables sending packets directly out over TCP.
4520                          * As a unix socket is doing that any way, report it as
4521                          * enabled.
4522                          */
4523                         if (optval == NULL || optlen == NULL ||
4524                             *optlen < (socklen_t)sizeof(int)) {
4525                                 errno = EINVAL;
4526                                 ret = -1;
4527                                 goto done;
4528                         }
4529
4530                         *optlen = sizeof(int);
4531                         *(int *)optval = si->tcp_nodelay;
4532
4533                         ret = 0;
4534                         goto done;
4535 #endif /* TCP_NODELAY */
4536 #ifdef TCP_INFO
4537                 case TCP_INFO: {
4538                         struct tcp_info info;
4539                         socklen_t ilen = sizeof(info);
4540
4541 #ifdef HAVE_NETINET_TCP_FSM_H
4542 /* This is FreeBSD */
4543 # define __TCP_LISTEN TCPS_LISTEN
4544 # define __TCP_ESTABLISHED TCPS_ESTABLISHED
4545 # define __TCP_CLOSE TCPS_CLOSED
4546 #else
4547 /* This is Linux */
4548 # define __TCP_LISTEN TCP_LISTEN
4549 # define __TCP_ESTABLISHED TCP_ESTABLISHED
4550 # define __TCP_CLOSE TCP_CLOSE
4551 #endif
4552
4553                         ZERO_STRUCT(info);
4554                         if (si->listening) {
4555                                 info.tcpi_state = __TCP_LISTEN;
4556                         } else if (si->connected) {
4557                                 /*
4558                                  * For now we just fake a few values
4559                                  * supported both by FreeBSD and Linux
4560                                  */
4561                                 info.tcpi_state = __TCP_ESTABLISHED;
4562                                 info.tcpi_rto = 200000;  /* 200 msec */
4563                                 info.tcpi_rtt = 5000;    /* 5 msec */
4564                                 info.tcpi_rttvar = 5000; /* 5 msec */
4565                         } else {
4566                                 info.tcpi_state = __TCP_CLOSE;
4567                                 info.tcpi_rto = 1000000;  /* 1 sec */
4568                                 info.tcpi_rtt = 0;
4569                                 info.tcpi_rttvar = 250000; /* 250 msec */
4570                         }
4571
4572                         if (optval == NULL || optlen == NULL ||
4573                             *optlen < (socklen_t)ilen) {
4574                                 errno = EINVAL;
4575                                 ret = -1;
4576                                 goto done;
4577                         }
4578
4579                         *optlen = ilen;
4580                         memcpy(optval, &info, ilen);
4581
4582                         ret = 0;
4583                         goto done;
4584                 }
4585 #endif /* TCP_INFO */
4586                 default:
4587                         break;
4588                 }
4589         }
4590
4591         errno = ENOPROTOOPT;
4592         ret = -1;
4593
4594 done:
4595         SWRAP_UNLOCK_SI(si);
4596         return ret;
4597 }
4598
4599 #ifdef HAVE_ACCEPT_PSOCKLEN_T
4600 int getsockopt(int s, int level, int optname, void *optval, Psocklen_t optlen)
4601 #else
4602 int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
4603 #endif
4604 {
4605         return swrap_getsockopt(s, level, optname, optval, (socklen_t *)optlen);
4606 }
4607
4608 /****************************************************************************
4609  *   SETSOCKOPT
4610  ***************************************************************************/
4611
4612 static int swrap_setsockopt(int s, int level, int optname,
4613                             const void *optval, socklen_t optlen)
4614 {
4615         struct socket_info *si = find_socket_info(s);
4616         int ret;
4617
4618         if (!si) {
4619                 return libc_setsockopt(s,
4620                                        level,
4621                                        optname,
4622                                        optval,
4623                                        optlen);
4624         }
4625
4626         if (level == SOL_SOCKET) {
4627                 return libc_setsockopt(s,
4628                                        level,
4629                                        optname,
4630                                        optval,
4631                                        optlen);
4632         }
4633
4634         SWRAP_LOCK_SI(si);
4635
4636         if (level == IPPROTO_TCP) {
4637                 switch (optname) {
4638 #ifdef TCP_NODELAY
4639                 case TCP_NODELAY: {
4640                         int i;
4641
4642                         /*
4643                          * This enables sending packets directly out over TCP.
4644                          * A unix socket is doing that any way.
4645                          */
4646                         if (optval == NULL || optlen == 0 ||
4647                             optlen < (socklen_t)sizeof(int)) {
4648                                 errno = EINVAL;
4649                                 ret = -1;
4650                                 goto done;
4651                         }
4652
4653                         i = *discard_const_p(int, optval);
4654                         if (i != 0 && i != 1) {
4655                                 errno = EINVAL;
4656                                 ret = -1;
4657                                 goto done;
4658                         }
4659                         si->tcp_nodelay = i;
4660
4661                         ret = 0;
4662                         goto done;
4663                 }
4664 #endif /* TCP_NODELAY */
4665                 default:
4666                         break;
4667                 }
4668         }
4669
4670         switch (si->family) {
4671         case AF_INET:
4672                 if (level == IPPROTO_IP) {
4673 #ifdef IP_PKTINFO
4674                         if (optname == IP_PKTINFO) {
4675                                 si->pktinfo = AF_INET;
4676                         }
4677 #endif /* IP_PKTINFO */
4678                 }
4679                 ret = 0;
4680                 goto done;
4681 #ifdef HAVE_IPV6
4682         case AF_INET6:
4683                 if (level == IPPROTO_IPV6) {
4684 #ifdef IPV6_RECVPKTINFO
4685                         if (optname == IPV6_RECVPKTINFO) {
4686                                 si->pktinfo = AF_INET6;
4687                         }
4688 #endif /* IPV6_PKTINFO */
4689                 }
4690                 ret = 0;
4691                 goto done;
4692 #endif
4693         default:
4694                 errno = ENOPROTOOPT;
4695                 ret = -1;
4696                 goto done;
4697         }
4698
4699 done:
4700         SWRAP_UNLOCK_SI(si);
4701         return ret;
4702 }
4703
4704 int setsockopt(int s, int level, int optname,
4705                const void *optval, socklen_t optlen)
4706 {
4707         return swrap_setsockopt(s, level, optname, optval, optlen);
4708 }
4709
4710 /****************************************************************************
4711  *   IOCTL
4712  ***************************************************************************/
4713
4714 static int swrap_vioctl(int s, unsigned long int r, va_list va)
4715 {
4716         struct socket_info *si = find_socket_info(s);
4717         va_list ap;
4718         int *value_ptr = NULL;
4719         int rc;
4720
4721         if (!si) {
4722                 return libc_vioctl(s, r, va);
4723         }
4724
4725         SWRAP_LOCK_SI(si);
4726
4727         va_copy(ap, va);
4728
4729         rc = libc_vioctl(s, r, va);
4730
4731         switch (r) {
4732         case FIONREAD:
4733                 if (rc == 0) {
4734                         value_ptr = ((int *)va_arg(ap, int *));
4735                 }
4736
4737                 if (rc == -1 && errno != EAGAIN && errno != ENOBUFS) {
4738                         swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
4739                 } else if (value_ptr != NULL && *value_ptr == 0) { /* END OF FILE */
4740                         swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
4741                 }
4742                 break;
4743 #ifdef FIONWRITE
4744         case FIONWRITE:
4745                 /* this is FreeBSD */
4746                 FALL_THROUGH; /* to TIOCOUTQ */
4747 #endif /* FIONWRITE */
4748         case TIOCOUTQ: /* same as SIOCOUTQ on Linux */
4749                 /*
4750                  * This may return more bytes then the application
4751                  * sent into the socket, for tcp it should
4752                  * return the number of unacked bytes.
4753                  *
4754                  * On AF_UNIX, all bytes are immediately acked!
4755                  */
4756                 if (rc == 0) {
4757                         value_ptr = ((int *)va_arg(ap, int *));
4758                         *value_ptr = 0;
4759                 }
4760                 break;
4761         }
4762
4763         va_end(ap);
4764
4765         SWRAP_UNLOCK_SI(si);
4766         return rc;
4767 }
4768
4769 #ifdef HAVE_IOCTL_INT
4770 int ioctl(int s, int r, ...)
4771 #else
4772 int ioctl(int s, unsigned long int r, ...)
4773 #endif
4774 {
4775         va_list va;
4776         int rc;
4777
4778         va_start(va, r);
4779
4780         rc = swrap_vioctl(s, (unsigned long int) r, va);
4781
4782         va_end(va);
4783
4784         return rc;
4785 }
4786
4787 /*****************
4788  * CMSG
4789  *****************/
4790
4791 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
4792
4793 #ifndef CMSG_ALIGN
4794 # ifdef _ALIGN /* BSD */
4795 #define CMSG_ALIGN _ALIGN
4796 # else
4797 #define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
4798 # endif /* _ALIGN */
4799 #endif /* CMSG_ALIGN */
4800
4801 /**
4802  * @brief Add a cmsghdr to a msghdr.
4803  *
4804  * This is an function to add any type of cmsghdr. It will operate on the
4805  * msg->msg_control and msg->msg_controllen you pass in by adapting them to
4806  * the buffer position after the added cmsg element. Hence, this function is
4807  * intended to be used with an intermediate msghdr and not on the original
4808  * one handed in by the client.
4809  *
4810  * @param[in]  msg      The msghdr to which to add the cmsg.
4811  *
4812  * @param[in]  level    The cmsg level to set.
4813  *
4814  * @param[in]  type     The cmsg type to set.
4815  *
4816  * @param[in]  data     The cmsg data to set.
4817  *
4818  * @param[in]  len      the length of the data to set.
4819  */
4820 static void swrap_msghdr_add_cmsghdr(struct msghdr *msg,
4821                                      int level,
4822                                      int type,
4823                                      const void *data,
4824                                      size_t len)
4825 {
4826         size_t cmlen = CMSG_LEN(len);
4827         size_t cmspace = CMSG_SPACE(len);
4828         uint8_t cmbuf[cmspace];
4829         void *cast_ptr = (void *)cmbuf;
4830         struct cmsghdr *cm = (struct cmsghdr *)cast_ptr;
4831         uint8_t *p;
4832
4833         memset(cmbuf, 0, cmspace);
4834
4835         if (msg->msg_controllen < cmlen) {
4836                 cmlen = msg->msg_controllen;
4837                 msg->msg_flags |= MSG_CTRUNC;
4838         }
4839
4840         if (msg->msg_controllen < cmspace) {
4841                 cmspace = msg->msg_controllen;
4842         }
4843
4844         /*
4845          * We copy the full input data into an intermediate cmsghdr first
4846          * in order to more easily cope with truncation.
4847          */
4848         cm->cmsg_len = cmlen;
4849         cm->cmsg_level = level;
4850         cm->cmsg_type = type;
4851         memcpy(CMSG_DATA(cm), data, len);
4852
4853         /*
4854          * We now copy the possibly truncated buffer.
4855          * We copy cmlen bytes, but consume cmspace bytes,
4856          * leaving the possible padding uninitialiazed.
4857          */
4858         p = (uint8_t *)msg->msg_control;
4859         memcpy(p, cm, cmlen);
4860         p += cmspace;
4861         msg->msg_control = p;
4862         msg->msg_controllen -= cmspace;
4863
4864         return;
4865 }
4866
4867 static int swrap_msghdr_add_pktinfo(struct socket_info *si,
4868                                     struct msghdr *msg)
4869 {
4870         /* Add packet info */
4871         switch (si->pktinfo) {
4872 #if defined(IP_PKTINFO) && (defined(HAVE_STRUCT_IN_PKTINFO) || defined(IP_RECVDSTADDR))
4873         case AF_INET: {
4874                 struct sockaddr_in *sin;
4875 #if defined(HAVE_STRUCT_IN_PKTINFO)
4876                 struct in_pktinfo pkt;
4877 #elif defined(IP_RECVDSTADDR)
4878                 struct in_addr pkt;
4879 #endif
4880
4881                 if (si->bindname.sa_socklen == sizeof(struct sockaddr_in)) {
4882                         sin = &si->bindname.sa.in;
4883                 } else {
4884                         if (si->myname.sa_socklen != sizeof(struct sockaddr_in)) {
4885                                 return 0;
4886                         }
4887                         sin = &si->myname.sa.in;
4888                 }
4889
4890                 ZERO_STRUCT(pkt);
4891
4892 #if defined(HAVE_STRUCT_IN_PKTINFO)
4893                 pkt.ipi_ifindex = socket_wrapper_default_iface();
4894                 pkt.ipi_addr.s_addr = sin->sin_addr.s_addr;
4895 #elif defined(IP_RECVDSTADDR)
4896                 pkt = sin->sin_addr;
4897 #endif
4898
4899                 swrap_msghdr_add_cmsghdr(msg, IPPROTO_IP, IP_PKTINFO,
4900                                          &pkt, sizeof(pkt));
4901
4902                 break;
4903         }
4904 #endif /* IP_PKTINFO */
4905 #if defined(HAVE_IPV6)
4906         case AF_INET6: {
4907 #if defined(IPV6_PKTINFO) && defined(HAVE_STRUCT_IN6_PKTINFO)
4908                 struct sockaddr_in6 *sin6;
4909                 struct in6_pktinfo pkt6;
4910
4911                 if (si->bindname.sa_socklen == sizeof(struct sockaddr_in6)) {
4912                         sin6 = &si->bindname.sa.in6;
4913                 } else {
4914                         if (si->myname.sa_socklen != sizeof(struct sockaddr_in6)) {
4915                                 return 0;
4916                         }
4917                         sin6 = &si->myname.sa.in6;
4918                 }
4919
4920                 ZERO_STRUCT(pkt6);
4921
4922                 pkt6.ipi6_ifindex = socket_wrapper_default_iface();
4923                 pkt6.ipi6_addr = sin6->sin6_addr;
4924
4925                 swrap_msghdr_add_cmsghdr(msg, IPPROTO_IPV6, IPV6_PKTINFO,
4926                                         &pkt6, sizeof(pkt6));
4927 #endif /* HAVE_STRUCT_IN6_PKTINFO */
4928
4929                 break;
4930         }
4931 #endif /* IPV6_PKTINFO */
4932         default:
4933                 return -1;
4934         }
4935
4936         return 0;
4937 }
4938
4939 static int swrap_msghdr_add_socket_info(struct socket_info *si,
4940                                         struct msghdr *omsg)
4941 {
4942         int rc = 0;
4943
4944         if (si->pktinfo > 0) {
4945                 rc = swrap_msghdr_add_pktinfo(si, omsg);
4946         }
4947
4948         return rc;
4949 }
4950
4951 static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
4952                                    uint8_t **cm_data,
4953                                    size_t *cm_data_space);
4954 static int swrap_sendmsg_filter_cmsg_socket(struct cmsghdr *cmsg,
4955                                             uint8_t **cm_data,
4956                                             size_t *cm_data_space);
4957
4958 static int swrap_sendmsg_filter_cmsghdr(struct msghdr *msg,
4959                                         uint8_t **cm_data,
4960                                         size_t *cm_data_space) {
4961         struct cmsghdr *cmsg;
4962         int rc = -1;
4963
4964         /* Nothing to do */
4965         if (msg->msg_controllen == 0 || msg->msg_control == NULL) {
4966                 return 0;
4967         }
4968
4969         for (cmsg = CMSG_FIRSTHDR(msg);
4970              cmsg != NULL;
4971              cmsg = CMSG_NXTHDR(msg, cmsg)) {
4972                 switch (cmsg->cmsg_level) {
4973                 case IPPROTO_IP:
4974                         rc = swrap_sendmsg_filter_cmsg_socket(cmsg,
4975                                                               cm_data,
4976                                                               cm_data_space);
4977                         break;
4978                 default:
4979                         rc = swrap_sendmsg_copy_cmsg(cmsg,
4980                                                      cm_data,
4981                                                      cm_data_space);
4982                         break;
4983                 }
4984         }
4985
4986         return rc;
4987 }
4988
4989 static int swrap_sendmsg_copy_cmsg(struct cmsghdr *cmsg,
4990                                    uint8_t **cm_data,
4991                                    size_t *cm_data_space)
4992 {
4993         size_t cmspace;
4994         uint8_t *p;
4995
4996         cmspace = *cm_data_space + CMSG_ALIGN(cmsg->cmsg_len);
4997
4998         p = realloc((*cm_data), cmspace);
4999         if (p == NULL) {
5000                 return -1;
5001         }
5002         (*cm_data) = p;
5003
5004         p = (*cm_data) + (*cm_data_space);
5005         *cm_data_space = cmspace;
5006
5007         memcpy(p, cmsg, cmsg->cmsg_len);
5008
5009         return 0;
5010 }
5011
5012 static int swrap_sendmsg_filter_cmsg_pktinfo(struct cmsghdr *cmsg,
5013                                             uint8_t **cm_data,
5014                                             size_t *cm_data_space);
5015
5016
5017 static int swrap_sendmsg_filter_cmsg_socket(struct cmsghdr *cmsg,
5018                                             uint8_t **cm_data,
5019                                             size_t *cm_data_space)
5020 {
5021         int rc = -1;
5022
5023         switch(cmsg->cmsg_type) {
5024 #ifdef IP_PKTINFO
5025         case IP_PKTINFO:
5026                 rc = swrap_sendmsg_filter_cmsg_pktinfo(cmsg,
5027                                                        cm_data,
5028                                                        cm_data_space);
5029                 break;
5030 #endif
5031 #ifdef IPV6_PKTINFO
5032         case IPV6_PKTINFO:
5033                 rc = swrap_sendmsg_filter_cmsg_pktinfo(cmsg,
5034                                                        cm_data,
5035                                                        cm_data_space);
5036                 break;
5037 #endif
5038         default:
5039                 break;
5040         }
5041
5042         return rc;
5043 }
5044
5045 static int swrap_sendmsg_filter_cmsg_pktinfo(struct cmsghdr *cmsg,
5046                                              uint8_t **cm_data,
5047                                              size_t *cm_data_space)
5048 {
5049         (void)cmsg; /* unused */
5050         (void)cm_data; /* unused */
5051         (void)cm_data_space; /* unused */
5052
5053         /*
5054          * Passing a IP pktinfo to a unix socket might be rejected by the
5055          * Kernel, at least on FreeBSD. So skip this cmsg.
5056          */
5057         return 0;
5058 }
5059 #endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
5060
5061 static ssize_t swrap_sendmsg_before(int fd,
5062                                     struct socket_info *si,
5063                                     struct msghdr *msg,
5064                                     struct iovec *tmp_iov,
5065                                     struct sockaddr_un *tmp_un,
5066                                     const struct sockaddr_un **to_un,
5067                                     const struct sockaddr **to,
5068                                     int *bcast)
5069 {
5070         size_t i, len = 0;
5071         ssize_t ret = -1;
5072
5073         if (to_un) {
5074                 *to_un = NULL;
5075         }
5076         if (to) {
5077                 *to = NULL;
5078         }
5079         if (bcast) {
5080                 *bcast = 0;
5081         }
5082
5083         SWRAP_LOCK_SI(si);
5084
5085         switch (si->type) {
5086         case SOCK_STREAM: {
5087                 unsigned long mtu;
5088
5089                 if (!si->connected) {
5090                         errno = ENOTCONN;
5091                         goto out;
5092                 }
5093
5094                 if (msg->msg_iovlen == 0) {
5095                         break;
5096                 }
5097
5098                 mtu = socket_wrapper_mtu();
5099                 for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5100                         size_t nlen;
5101                         nlen = len + msg->msg_iov[i].iov_len;
5102                         if (nlen < len) {
5103                                 /* overflow */
5104                                 errno = EMSGSIZE;
5105                                 goto out;
5106                         }
5107                         if (nlen > mtu) {
5108                                 break;
5109                         }
5110                 }
5111                 msg->msg_iovlen = i;
5112                 if (msg->msg_iovlen == 0) {
5113                         *tmp_iov = msg->msg_iov[0];
5114                         tmp_iov->iov_len = MIN((size_t)tmp_iov->iov_len,
5115                                                (size_t)mtu);
5116                         msg->msg_iov = tmp_iov;
5117                         msg->msg_iovlen = 1;
5118                 }
5119                 break;
5120         }
5121         case SOCK_DGRAM:
5122                 if (si->connected) {
5123                         if (msg->msg_name != NULL) {
5124                                 /*
5125                                  * We are dealing with unix sockets and if we
5126                                  * are connected, we should only talk to the
5127                                  * connected unix path. Using the fd to send
5128                                  * to another server would be hard to achieve.
5129                                  */
5130                                 msg->msg_name = NULL;
5131                                 msg->msg_namelen = 0;
5132                         }
5133                 } else {
5134                         const struct sockaddr *msg_name;
5135                         msg_name = (const struct sockaddr *)msg->msg_name;
5136
5137                         if (msg_name == NULL) {
5138                                 errno = ENOTCONN;
5139                                 goto out;
5140                         }
5141
5142
5143                         ret = sockaddr_convert_to_un(si, msg_name, msg->msg_namelen,
5144                                                      tmp_un, 0, bcast);
5145                         if (ret == -1) {
5146                                 goto out;
5147                         }
5148
5149                         if (to_un) {
5150                                 *to_un = tmp_un;
5151                         }
5152                         if (to) {
5153                                 *to = msg_name;
5154                         }
5155                         msg->msg_name = tmp_un;
5156                         msg->msg_namelen = sizeof(*tmp_un);
5157                 }
5158
5159                 if (si->bound == 0) {
5160                         ret = swrap_auto_bind(fd, si, si->family);
5161                         if (ret == -1) {
5162                                 SWRAP_UNLOCK_SI(si);
5163                                 if (errno == ENOTSOCK) {
5164                                         swrap_remove_stale(fd);
5165                                         ret = -ENOTSOCK;
5166                                 } else {
5167                                         SWRAP_LOG(SWRAP_LOG_ERROR, "swrap_sendmsg_before failed");
5168                                 }
5169                                 return ret;
5170                         }
5171                 }
5172
5173                 if (!si->defer_connect) {
5174                         break;
5175                 }
5176
5177                 ret = sockaddr_convert_to_un(si,
5178                                              &si->peername.sa.s,
5179                                              si->peername.sa_socklen,
5180                                              tmp_un,
5181                                              0,
5182                                              NULL);
5183                 if (ret == -1) {
5184                         goto out;
5185                 }
5186
5187                 ret = libc_connect(fd,
5188                                    (struct sockaddr *)(void *)tmp_un,
5189                                    sizeof(*tmp_un));
5190
5191                 /* to give better errors */
5192                 if (ret == -1 && errno == ENOENT) {
5193                         errno = EHOSTUNREACH;
5194                 }
5195
5196                 if (ret == -1) {
5197                         goto out;
5198                 }
5199
5200                 si->defer_connect = 0;
5201                 break;
5202         default:
5203                 errno = EHOSTUNREACH;
5204                 goto out;
5205         }
5206
5207 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5208         if (msg->msg_controllen > 0 && msg->msg_control != NULL) {
5209                 uint8_t *cmbuf = NULL;
5210                 size_t cmlen = 0;
5211
5212                 ret = swrap_sendmsg_filter_cmsghdr(msg, &cmbuf, &cmlen);
5213                 if (ret < 0) {
5214                         free(cmbuf);
5215                         goto out;
5216                 }
5217
5218                 if (cmlen == 0) {
5219                         msg->msg_controllen = 0;
5220                         msg->msg_control = NULL;
5221                 } else if (cmlen < msg->msg_controllen && cmbuf != NULL) {
5222                         memcpy(msg->msg_control, cmbuf, cmlen);
5223                         msg->msg_controllen = cmlen;
5224                 }
5225                 free(cmbuf);
5226         }
5227 #endif
5228
5229         ret = 0;
5230 out:
5231         SWRAP_UNLOCK_SI(si);
5232
5233         return ret;
5234 }
5235
5236 static void swrap_sendmsg_after(int fd,
5237                                 struct socket_info *si,
5238                                 struct msghdr *msg,
5239                                 const struct sockaddr *to,
5240                                 ssize_t ret)
5241 {
5242         int saved_errno = errno;
5243         size_t i, len = 0;
5244         uint8_t *buf;
5245         off_t ofs = 0;
5246         size_t avail = 0;
5247         size_t remain;
5248
5249         /* to give better errors */
5250         if (ret == -1) {
5251                 if (saved_errno == ENOENT) {
5252                         saved_errno = EHOSTUNREACH;
5253                 } else if (saved_errno == ENOTSOCK) {
5254                         /* If the fd is not a socket, remove it */
5255                         swrap_remove_stale(fd);
5256                 }
5257         }
5258
5259         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5260                 avail += msg->msg_iov[i].iov_len;
5261         }
5262
5263         if (ret == -1) {
5264                 remain = MIN(80, avail);
5265         } else {
5266                 remain = ret;
5267         }
5268
5269         /* we capture it as one single packet */
5270         buf = (uint8_t *)malloc(remain);
5271         if (!buf) {
5272                 /* we just not capture the packet */
5273                 errno = saved_errno;
5274                 return;
5275         }
5276
5277         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5278                 size_t this_time = MIN(remain, (size_t)msg->msg_iov[i].iov_len);
5279                 memcpy(buf + ofs,
5280                        msg->msg_iov[i].iov_base,
5281                        this_time);
5282                 ofs += this_time;
5283                 remain -= this_time;
5284         }
5285         len = ofs;
5286
5287         SWRAP_LOCK_SI(si);
5288
5289         switch (si->type) {
5290         case SOCK_STREAM:
5291                 if (ret == -1) {
5292                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
5293                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND_RST, NULL, 0);
5294                 } else {
5295                         swrap_pcap_dump_packet(si, NULL, SWRAP_SEND, buf, len);
5296                 }
5297                 break;
5298
5299         case SOCK_DGRAM:
5300                 if (si->connected) {
5301                         to = &si->peername.sa.s;
5302                 }
5303                 if (ret == -1) {
5304                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
5305                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO_UNREACH, buf, len);
5306                 } else {
5307                         swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
5308                 }
5309                 break;
5310         }
5311
5312         SWRAP_UNLOCK_SI(si);
5313
5314         free(buf);
5315         errno = saved_errno;
5316 }
5317
5318 static int swrap_recvmsg_before(int fd,
5319                                 struct socket_info *si,
5320                                 struct msghdr *msg,
5321                                 struct iovec *tmp_iov)
5322 {
5323         size_t i, len = 0;
5324         int ret = -1;
5325
5326         SWRAP_LOCK_SI(si);
5327
5328         (void)fd; /* unused */
5329
5330         switch (si->type) {
5331         case SOCK_STREAM: {
5332                 unsigned int mtu;
5333                 if (!si->connected) {
5334                         errno = ENOTCONN;
5335                         goto out;
5336                 }
5337
5338                 if (msg->msg_iovlen == 0) {
5339                         break;
5340                 }
5341
5342                 mtu = socket_wrapper_mtu();
5343                 for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5344                         size_t nlen;
5345                         nlen = len + msg->msg_iov[i].iov_len;
5346                         if (nlen > mtu) {
5347                                 break;
5348                         }
5349                 }
5350                 msg->msg_iovlen = i;
5351                 if (msg->msg_iovlen == 0) {
5352                         *tmp_iov = msg->msg_iov[0];
5353                         tmp_iov->iov_len = MIN((size_t)tmp_iov->iov_len,
5354                                                (size_t)mtu);
5355                         msg->msg_iov = tmp_iov;
5356                         msg->msg_iovlen = 1;
5357                 }
5358                 break;
5359         }
5360         case SOCK_DGRAM:
5361                 if (msg->msg_name == NULL) {
5362                         errno = EINVAL;
5363                         goto out;
5364                 }
5365
5366                 if (msg->msg_iovlen == 0) {
5367                         break;
5368                 }
5369
5370                 if (si->bound == 0) {
5371                         ret = swrap_auto_bind(fd, si, si->family);
5372                         if (ret == -1) {
5373                                 SWRAP_UNLOCK_SI(si);
5374                                 /*
5375                                  * When attempting to read or write to a
5376                                  * descriptor, if an underlying autobind fails
5377                                  * because it's not a socket, stop intercepting
5378                                  * uses of that descriptor.
5379                                  */
5380                                 if (errno == ENOTSOCK) {
5381                                         swrap_remove_stale(fd);
5382                                         ret = -ENOTSOCK;
5383                                 } else {
5384                                         SWRAP_LOG(SWRAP_LOG_ERROR,
5385                                                   "swrap_recvmsg_before failed");
5386                                 }
5387                                 return ret;
5388                         }
5389                 }
5390                 break;
5391         default:
5392                 errno = EHOSTUNREACH;
5393                 goto out;
5394         }
5395
5396         ret = 0;
5397 out:
5398         SWRAP_UNLOCK_SI(si);
5399
5400         return ret;
5401 }
5402
5403 static int swrap_recvmsg_after(int fd,
5404                                struct socket_info *si,
5405                                struct msghdr *msg,
5406                                const struct sockaddr_un *un_addr,
5407                                socklen_t un_addrlen,
5408                                ssize_t ret)
5409 {
5410         int saved_errno = errno;
5411         size_t i;
5412         uint8_t *buf = NULL;
5413         off_t ofs = 0;
5414         size_t avail = 0;
5415         size_t remain;
5416         int rc;
5417
5418         /* to give better errors */
5419         if (ret == -1) {
5420                 if (saved_errno == ENOENT) {
5421                         saved_errno = EHOSTUNREACH;
5422                 } else if (saved_errno == ENOTSOCK) {
5423                         /* If the fd is not a socket, remove it */
5424                         swrap_remove_stale(fd);
5425                 }
5426         }
5427
5428         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5429                 avail += msg->msg_iov[i].iov_len;
5430         }
5431
5432         SWRAP_LOCK_SI(si);
5433
5434         /* Convert the socket address before we leave */
5435         if (si->type == SOCK_DGRAM && un_addr != NULL) {
5436                 rc = sockaddr_convert_from_un(si,
5437                                               un_addr,
5438                                               un_addrlen,
5439                                               si->family,
5440                                               msg->msg_name,
5441                                               &msg->msg_namelen);
5442                 if (rc == -1) {
5443                         goto done;
5444                 }
5445         }
5446
5447         if (avail == 0) {
5448                 rc = 0;
5449                 goto done;
5450         }
5451
5452         if (ret == -1) {
5453                 remain = MIN(80, avail);
5454         } else {
5455                 remain = ret;
5456         }
5457
5458         /* we capture it as one single packet */
5459         buf = (uint8_t *)malloc(remain);
5460         if (buf == NULL) {
5461                 /* we just not capture the packet */
5462                 SWRAP_UNLOCK_SI(si);
5463                 errno = saved_errno;
5464                 return -1;
5465         }
5466
5467         for (i = 0; i < (size_t)msg->msg_iovlen; i++) {
5468                 size_t this_time = MIN(remain, (size_t)msg->msg_iov[i].iov_len);
5469                 memcpy(buf + ofs,
5470                        msg->msg_iov[i].iov_base,
5471                        this_time);
5472                 ofs += this_time;
5473                 remain -= this_time;
5474         }
5475
5476         switch (si->type) {
5477         case SOCK_STREAM:
5478                 if (ret == -1 && saved_errno != EAGAIN && saved_errno != ENOBUFS) {
5479                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
5480                 } else if (ret == 0) { /* END OF FILE */
5481                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV_RST, NULL, 0);
5482                 } else if (ret > 0) {
5483                         swrap_pcap_dump_packet(si, NULL, SWRAP_RECV, buf, ret);
5484                 }
5485                 break;
5486
5487         case SOCK_DGRAM:
5488                 if (ret == -1) {
5489                         break;
5490                 }
5491
5492                 if (un_addr != NULL) {
5493                         swrap_pcap_dump_packet(si,
5494                                           msg->msg_name,
5495                                           SWRAP_RECVFROM,
5496                                           buf,
5497                                           ret);
5498                 } else {
5499                         swrap_pcap_dump_packet(si,
5500                                           msg->msg_name,
5501                                           SWRAP_RECV,
5502                                           buf,
5503                                           ret);
5504                 }
5505
5506                 break;
5507         }
5508
5509         rc = 0;
5510 done:
5511         free(buf);
5512         errno = saved_errno;
5513
5514 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5515         if (rc == 0 &&
5516             msg->msg_controllen > 0 &&
5517             msg->msg_control != NULL) {
5518                 rc = swrap_msghdr_add_socket_info(si, msg);
5519                 if (rc < 0) {
5520                         SWRAP_UNLOCK_SI(si);
5521                         return -1;
5522                 }
5523         }
5524 #endif
5525
5526         SWRAP_UNLOCK_SI(si);
5527         return rc;
5528 }
5529
5530 /****************************************************************************
5531  *   RECVFROM
5532  ***************************************************************************/
5533
5534 static ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags,
5535                               struct sockaddr *from, socklen_t *fromlen)
5536 {
5537         struct swrap_address from_addr = {
5538                 .sa_socklen = sizeof(struct sockaddr_un),
5539         };
5540         ssize_t ret;
5541         struct socket_info *si = find_socket_info(s);
5542         struct swrap_address saddr = {
5543                 .sa_socklen = sizeof(struct sockaddr_storage),
5544         };
5545         struct msghdr msg;
5546         struct iovec tmp;
5547         int tret;
5548
5549         if (!si) {
5550                 return libc_recvfrom(s,
5551                                      buf,
5552                                      len,
5553                                      flags,
5554                                      from,
5555                                      fromlen);
5556         }
5557
5558         tmp.iov_base = buf;
5559         tmp.iov_len = len;
5560
5561         ZERO_STRUCT(msg);
5562         if (from != NULL && fromlen != NULL) {
5563                 msg.msg_name = from;   /* optional address */
5564                 msg.msg_namelen = *fromlen; /* size of address */
5565         } else {
5566                 msg.msg_name = &saddr.sa.s; /* optional address */
5567                 msg.msg_namelen = saddr.sa_socklen; /* size of address */
5568         }
5569         msg.msg_iov = &tmp;            /* scatter/gather array */
5570         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5571 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5572         msg.msg_control = NULL;        /* ancillary data, see below */
5573         msg.msg_controllen = 0;        /* ancillary data buffer len */
5574         msg.msg_flags = 0;             /* flags on received message */
5575 #endif
5576
5577         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
5578         if (tret < 0) {
5579                 return -1;
5580         }
5581
5582         buf = msg.msg_iov[0].iov_base;
5583         len = msg.msg_iov[0].iov_len;
5584
5585         ret = libc_recvfrom(s,
5586                             buf,
5587                             len,
5588                             flags,
5589                             &from_addr.sa.s,
5590                             &from_addr.sa_socklen);
5591         if (ret == -1) {
5592                 return ret;
5593         }
5594
5595         tret = swrap_recvmsg_after(s,
5596                                    si,
5597                                    &msg,
5598                                    &from_addr.sa.un,
5599                                    from_addr.sa_socklen,
5600                                    ret);
5601         if (tret != 0) {
5602                 return tret;
5603         }
5604
5605         if (from != NULL && fromlen != NULL) {
5606                 *fromlen = msg.msg_namelen;
5607         }
5608
5609         return ret;
5610 }
5611
5612 #ifdef HAVE_ACCEPT_PSOCKLEN_T
5613 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
5614                  struct sockaddr *from, Psocklen_t fromlen)
5615 #else
5616 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
5617                  struct sockaddr *from, socklen_t *fromlen)
5618 #endif
5619 {
5620         return swrap_recvfrom(s, buf, len, flags, from, (socklen_t *)fromlen);
5621 }
5622
5623 /****************************************************************************
5624  *   SENDTO
5625  ***************************************************************************/
5626
5627 static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
5628                             const struct sockaddr *to, socklen_t tolen)
5629 {
5630         struct msghdr msg;
5631         struct iovec tmp;
5632         struct swrap_address un_addr = {
5633                 .sa_socklen = sizeof(struct sockaddr_un),
5634         };
5635         const struct sockaddr_un *to_un = NULL;
5636         ssize_t ret;
5637         int rc;
5638         struct socket_info *si = find_socket_info(s);
5639         int bcast = 0;
5640
5641         if (!si) {
5642                 return libc_sendto(s, buf, len, flags, to, tolen);
5643         }
5644
5645         tmp.iov_base = discard_const_p(char, buf);
5646         tmp.iov_len = len;
5647
5648         ZERO_STRUCT(msg);
5649         msg.msg_name = discard_const_p(struct sockaddr, to); /* optional address */
5650         msg.msg_namelen = tolen;       /* size of address */
5651         msg.msg_iov = &tmp;            /* scatter/gather array */
5652         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5653 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5654         msg.msg_control = NULL;        /* ancillary data, see below */
5655         msg.msg_controllen = 0;        /* ancillary data buffer len */
5656         msg.msg_flags = 0;             /* flags on received message */
5657 #endif
5658
5659         rc = swrap_sendmsg_before(s,
5660                                   si,
5661                                   &msg,
5662                                   &tmp,
5663                                   &un_addr.sa.un,
5664                                   &to_un,
5665                                   &to,
5666                                   &bcast);
5667         if (rc < 0) {
5668                 return -1;
5669         }
5670
5671         buf = msg.msg_iov[0].iov_base;
5672         len = msg.msg_iov[0].iov_len;
5673
5674         if (bcast) {
5675                 struct stat st;
5676                 unsigned int iface;
5677                 unsigned int prt = ntohs(((const struct sockaddr_in *)(const void *)to)->sin_port);
5678                 char type;
5679                 char *swrap_dir = NULL;
5680
5681                 type = SOCKET_TYPE_CHAR_UDP;
5682
5683                 swrap_dir = socket_wrapper_dir();
5684                 if (swrap_dir == NULL) {
5685                         return -1;
5686                 }
5687
5688                 for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
5689                         swrap_un_path(&un_addr.sa.un,
5690                                       swrap_dir,
5691                                       type,
5692                                       iface,
5693                                       prt);
5694                         if (stat(un_addr.sa.un.sun_path, &st) != 0) continue;
5695
5696                         /* ignore the any errors in broadcast sends */
5697                         libc_sendto(s,
5698                                     buf,
5699                                     len,
5700                                     flags,
5701                                     &un_addr.sa.s,
5702                                     un_addr.sa_socklen);
5703                 }
5704
5705                 SAFE_FREE(swrap_dir);
5706
5707                 SWRAP_LOCK_SI(si);
5708
5709                 swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
5710
5711                 SWRAP_UNLOCK_SI(si);
5712
5713                 return len;
5714         }
5715
5716         SWRAP_LOCK_SI(si);
5717         /*
5718          * If it is a dgram socket and we are connected, don't include the
5719          * 'to' address.
5720          */
5721         if (si->type == SOCK_DGRAM && si->connected) {
5722                 ret = libc_sendto(s,
5723                                   buf,
5724                                   len,
5725                                   flags,
5726                                   NULL,
5727                                   0);
5728         } else {
5729                 ret = libc_sendto(s,
5730                                   buf,
5731                                   len,
5732                                   flags,
5733                                   (struct sockaddr *)msg.msg_name,
5734                                   msg.msg_namelen);
5735         }
5736
5737         SWRAP_UNLOCK_SI(si);
5738
5739         swrap_sendmsg_after(s, si, &msg, to, ret);
5740
5741         return ret;
5742 }
5743
5744 ssize_t sendto(int s, const void *buf, size_t len, int flags,
5745                const struct sockaddr *to, socklen_t tolen)
5746 {
5747         return swrap_sendto(s, buf, len, flags, to, tolen);
5748 }
5749
5750 /****************************************************************************
5751  *   READV
5752  ***************************************************************************/
5753
5754 static ssize_t swrap_recv(int s, void *buf, size_t len, int flags)
5755 {
5756         struct socket_info *si;
5757         struct msghdr msg;
5758         struct swrap_address saddr = {
5759                 .sa_socklen = sizeof(struct sockaddr_storage),
5760         };
5761         struct iovec tmp;
5762         ssize_t ret;
5763         int tret;
5764
5765         si = find_socket_info(s);
5766         if (si == NULL) {
5767                 return libc_recv(s, buf, len, flags);
5768         }
5769
5770         tmp.iov_base = buf;
5771         tmp.iov_len = len;
5772
5773         ZERO_STRUCT(msg);
5774         msg.msg_name = &saddr.sa.s;    /* optional address */
5775         msg.msg_namelen = saddr.sa_socklen; /* size of address */
5776         msg.msg_iov = &tmp;            /* scatter/gather array */
5777         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5778 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5779         msg.msg_control = NULL;        /* ancillary data, see below */
5780         msg.msg_controllen = 0;        /* ancillary data buffer len */
5781         msg.msg_flags = 0;             /* flags on received message */
5782 #endif
5783
5784         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
5785         if (tret < 0) {
5786                 return -1;
5787         }
5788
5789         buf = msg.msg_iov[0].iov_base;
5790         len = msg.msg_iov[0].iov_len;
5791
5792         ret = libc_recv(s, buf, len, flags);
5793
5794         tret = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
5795         if (tret != 0) {
5796                 return tret;
5797         }
5798
5799         return ret;
5800 }
5801
5802 ssize_t recv(int s, void *buf, size_t len, int flags)
5803 {
5804         return swrap_recv(s, buf, len, flags);
5805 }
5806
5807 /****************************************************************************
5808  *   READ
5809  ***************************************************************************/
5810
5811 static ssize_t swrap_read(int s, void *buf, size_t len)
5812 {
5813         struct socket_info *si;
5814         struct msghdr msg;
5815         struct iovec tmp;
5816         struct swrap_address saddr = {
5817                 .sa_socklen = sizeof(struct sockaddr_storage),
5818         };
5819         ssize_t ret;
5820         int tret;
5821
5822         si = find_socket_info(s);
5823         if (si == NULL) {
5824                 return libc_read(s, buf, len);
5825         }
5826
5827         tmp.iov_base = buf;
5828         tmp.iov_len = len;
5829
5830         ZERO_STRUCT(msg);
5831         msg.msg_name = &saddr.sa.ss;   /* optional address */
5832         msg.msg_namelen = saddr.sa_socklen; /* size of address */
5833         msg.msg_iov = &tmp;            /* scatter/gather array */
5834         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5835 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5836         msg.msg_control = NULL;        /* ancillary data, see below */
5837         msg.msg_controllen = 0;        /* ancillary data buffer len */
5838         msg.msg_flags = 0;             /* flags on received message */
5839 #endif
5840
5841         tret = swrap_recvmsg_before(s, si, &msg, &tmp);
5842         if (tret < 0) {
5843                 if (tret == -ENOTSOCK) {
5844                         return libc_read(s, buf, len);
5845                 }
5846                 return -1;
5847         }
5848
5849         buf = msg.msg_iov[0].iov_base;
5850         len = msg.msg_iov[0].iov_len;
5851
5852         ret = libc_read(s, buf, len);
5853
5854         tret = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
5855         if (tret != 0) {
5856                 return tret;
5857         }
5858
5859         return ret;
5860 }
5861
5862 ssize_t read(int s, void *buf, size_t len)
5863 {
5864         return swrap_read(s, buf, len);
5865 }
5866
5867 /****************************************************************************
5868  *   WRITE
5869  ***************************************************************************/
5870
5871 static ssize_t swrap_write(int s, const void *buf, size_t len)
5872 {
5873         struct msghdr msg;
5874         struct iovec tmp;
5875         struct sockaddr_un un_addr;
5876         ssize_t ret;
5877         int rc;
5878         struct socket_info *si;
5879
5880         si = find_socket_info(s);
5881         if (si == NULL) {
5882                 return libc_write(s, buf, len);
5883         }
5884
5885         tmp.iov_base = discard_const_p(char, buf);
5886         tmp.iov_len = len;
5887
5888         ZERO_STRUCT(msg);
5889         msg.msg_name = NULL;           /* optional address */
5890         msg.msg_namelen = 0;           /* size of address */
5891         msg.msg_iov = &tmp;            /* scatter/gather array */
5892         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5893 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5894         msg.msg_control = NULL;        /* ancillary data, see below */
5895         msg.msg_controllen = 0;        /* ancillary data buffer len */
5896         msg.msg_flags = 0;             /* flags on received message */
5897 #endif
5898
5899         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
5900         if (rc < 0) {
5901                 return -1;
5902         }
5903
5904         buf = msg.msg_iov[0].iov_base;
5905         len = msg.msg_iov[0].iov_len;
5906
5907         ret = libc_write(s, buf, len);
5908
5909         swrap_sendmsg_after(s, si, &msg, NULL, ret);
5910
5911         return ret;
5912 }
5913
5914 ssize_t write(int s, const void *buf, size_t len)
5915 {
5916         return swrap_write(s, buf, len);
5917 }
5918
5919 /****************************************************************************
5920  *   SEND
5921  ***************************************************************************/
5922
5923 static ssize_t swrap_send(int s, const void *buf, size_t len, int flags)
5924 {
5925         struct msghdr msg;
5926         struct iovec tmp;
5927         struct sockaddr_un un_addr;
5928         ssize_t ret;
5929         int rc;
5930         struct socket_info *si = find_socket_info(s);
5931
5932         if (!si) {
5933                 return libc_send(s, buf, len, flags);
5934         }
5935
5936         tmp.iov_base = discard_const_p(char, buf);
5937         tmp.iov_len = len;
5938
5939         ZERO_STRUCT(msg);
5940         msg.msg_name = NULL;           /* optional address */
5941         msg.msg_namelen = 0;           /* size of address */
5942         msg.msg_iov = &tmp;            /* scatter/gather array */
5943         msg.msg_iovlen = 1;            /* # elements in msg_iov */
5944 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5945         msg.msg_control = NULL;        /* ancillary data, see below */
5946         msg.msg_controllen = 0;        /* ancillary data buffer len */
5947         msg.msg_flags = 0;             /* flags on received message */
5948 #endif
5949
5950         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
5951         if (rc < 0) {
5952                 return -1;
5953         }
5954
5955         buf = msg.msg_iov[0].iov_base;
5956         len = msg.msg_iov[0].iov_len;
5957
5958         ret = libc_send(s, buf, len, flags);
5959
5960         swrap_sendmsg_after(s, si, &msg, NULL, ret);
5961
5962         return ret;
5963 }
5964
5965 ssize_t send(int s, const void *buf, size_t len, int flags)
5966 {
5967         return swrap_send(s, buf, len, flags);
5968 }
5969
5970 /****************************************************************************
5971  *   RECVMSG
5972  ***************************************************************************/
5973
5974 static ssize_t swrap_recvmsg(int s, struct msghdr *omsg, int flags)
5975 {
5976         struct swrap_address from_addr = {
5977                 .sa_socklen = sizeof(struct sockaddr_un),
5978         };
5979         struct swrap_address convert_addr = {
5980                 .sa_socklen = sizeof(struct sockaddr_storage),
5981         };
5982         struct socket_info *si;
5983         struct msghdr msg;
5984         struct iovec tmp;
5985 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
5986         size_t msg_ctrllen_filled;
5987         size_t msg_ctrllen_left;
5988 #endif
5989
5990         ssize_t ret;
5991         int rc;
5992
5993         si = find_socket_info(s);
5994         if (si == NULL) {
5995                 return libc_recvmsg(s, omsg, flags);
5996         }
5997
5998         tmp.iov_base = NULL;
5999         tmp.iov_len = 0;
6000
6001         ZERO_STRUCT(msg);
6002         msg.msg_name = &from_addr.sa;              /* optional address */
6003         msg.msg_namelen = from_addr.sa_socklen;    /* size of address */
6004         msg.msg_iov = omsg->msg_iov;               /* scatter/gather array */
6005         msg.msg_iovlen = omsg->msg_iovlen;         /* # elements in msg_iov */
6006 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6007         msg_ctrllen_filled = 0;
6008         msg_ctrllen_left = omsg->msg_controllen;
6009
6010         msg.msg_control = omsg->msg_control;       /* ancillary data, see below */
6011         msg.msg_controllen = omsg->msg_controllen; /* ancillary data buffer len */
6012         msg.msg_flags = omsg->msg_flags;           /* flags on received message */
6013 #endif
6014
6015         rc = swrap_recvmsg_before(s, si, &msg, &tmp);
6016         if (rc < 0) {
6017                 return -1;
6018         }
6019
6020         ret = libc_recvmsg(s, &msg, flags);
6021
6022 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6023         msg_ctrllen_filled += msg.msg_controllen;
6024         msg_ctrllen_left -= msg.msg_controllen;
6025
6026         if (omsg->msg_control != NULL) {
6027                 uint8_t *p;
6028
6029                 p = omsg->msg_control;
6030                 p += msg_ctrllen_filled;
6031
6032                 msg.msg_control = p;
6033                 msg.msg_controllen = msg_ctrllen_left;
6034         } else {
6035                 msg.msg_control = NULL;
6036                 msg.msg_controllen = 0;
6037         }
6038 #endif
6039
6040         /*
6041          * We convert the unix address to a IP address so we need a buffer
6042          * which can store the address in case of SOCK_DGRAM, see below.
6043          */
6044         msg.msg_name = &convert_addr.sa;
6045         msg.msg_namelen = convert_addr.sa_socklen;
6046
6047         rc = swrap_recvmsg_after(s,
6048                                  si,
6049                                  &msg,
6050                                  &from_addr.sa.un,
6051                                  from_addr.sa_socklen,
6052                                  ret);
6053         if (rc != 0) {
6054                 return rc;
6055         }
6056
6057 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6058         if (omsg->msg_control != NULL) {
6059                 /* msg.msg_controllen = space left */
6060                 msg_ctrllen_left = msg.msg_controllen;
6061                 msg_ctrllen_filled = omsg->msg_controllen - msg_ctrllen_left;
6062         }
6063
6064         /* Update the original message length */
6065         omsg->msg_controllen = msg_ctrllen_filled;
6066         omsg->msg_flags = msg.msg_flags;
6067 #endif
6068         omsg->msg_iovlen = msg.msg_iovlen;
6069
6070         SWRAP_LOCK_SI(si);
6071
6072         /*
6073          * From the manpage:
6074          *
6075          * The  msg_name  field  points  to a caller-allocated buffer that is
6076          * used to return the source address if the socket is unconnected.  The
6077          * caller should set msg_namelen to the size of this buffer before this
6078          * call; upon return from a successful call, msg_name will contain the
6079          * length of the returned address.  If the application  does  not  need
6080          * to know the source address, msg_name can be specified as NULL.
6081          */
6082         if (si->type == SOCK_STREAM) {
6083                 omsg->msg_namelen = 0;
6084         } else if (omsg->msg_name != NULL &&
6085                    omsg->msg_namelen != 0 &&
6086                    omsg->msg_namelen >= msg.msg_namelen) {
6087                 memcpy(omsg->msg_name, msg.msg_name, msg.msg_namelen);
6088                 omsg->msg_namelen = msg.msg_namelen;
6089         }
6090
6091         SWRAP_UNLOCK_SI(si);
6092
6093         return ret;
6094 }
6095
6096 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
6097 {
6098         return swrap_recvmsg(sockfd, msg, flags);
6099 }
6100
6101 /****************************************************************************
6102  *   SENDMSG
6103  ***************************************************************************/
6104
6105 static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
6106 {
6107         struct msghdr msg;
6108         struct iovec tmp;
6109         struct sockaddr_un un_addr;
6110         const struct sockaddr_un *to_un = NULL;
6111         const struct sockaddr *to = NULL;
6112         ssize_t ret;
6113         int rc;
6114         struct socket_info *si = find_socket_info(s);
6115         int bcast = 0;
6116
6117         if (!si) {
6118                 return libc_sendmsg(s, omsg, flags);
6119         }
6120
6121         ZERO_STRUCT(un_addr);
6122
6123         tmp.iov_base = NULL;
6124         tmp.iov_len = 0;
6125
6126         ZERO_STRUCT(msg);
6127
6128         SWRAP_LOCK_SI(si);
6129
6130         if (si->connected == 0) {
6131                 msg.msg_name = omsg->msg_name;             /* optional address */
6132                 msg.msg_namelen = omsg->msg_namelen;       /* size of address */
6133         }
6134         msg.msg_iov = omsg->msg_iov;               /* scatter/gather array */
6135         msg.msg_iovlen = omsg->msg_iovlen;         /* # elements in msg_iov */
6136
6137         SWRAP_UNLOCK_SI(si);
6138
6139 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6140         if (msg.msg_controllen > 0 && msg.msg_control != NULL) {
6141                 /* omsg is a const so use a local buffer for modifications */
6142                 uint8_t cmbuf[omsg->msg_controllen];
6143
6144                 memcpy(cmbuf, omsg->msg_control, omsg->msg_controllen);
6145
6146                 msg.msg_control = cmbuf;       /* ancillary data, see below */
6147                 msg.msg_controllen = omsg->msg_controllen; /* ancillary data buffer len */
6148         }
6149         msg.msg_flags = omsg->msg_flags;           /* flags on received message */
6150 #endif
6151
6152         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, &to_un, &to, &bcast);
6153         if (rc < 0) {
6154                 return -1;
6155         }
6156
6157         if (bcast) {
6158                 struct stat st;
6159                 unsigned int iface;
6160                 unsigned int prt = ntohs(((const struct sockaddr_in *)(const void *)to)->sin_port);
6161                 char type;
6162                 size_t i, len = 0;
6163                 uint8_t *buf;
6164                 off_t ofs = 0;
6165                 size_t avail = 0;
6166                 size_t remain;
6167                 char *swrap_dir = NULL;
6168
6169                 for (i = 0; i < (size_t)msg.msg_iovlen; i++) {
6170                         avail += msg.msg_iov[i].iov_len;
6171                 }
6172
6173                 len = avail;
6174                 remain = avail;
6175
6176                 /* we capture it as one single packet */
6177                 buf = (uint8_t *)malloc(remain);
6178                 if (!buf) {
6179                         return -1;
6180                 }
6181
6182                 for (i = 0; i < (size_t)msg.msg_iovlen; i++) {
6183                         size_t this_time = MIN(remain, (size_t)msg.msg_iov[i].iov_len);
6184                         memcpy(buf + ofs,
6185                                msg.msg_iov[i].iov_base,
6186                                this_time);
6187                         ofs += this_time;
6188                         remain -= this_time;
6189                 }
6190
6191                 type = SOCKET_TYPE_CHAR_UDP;
6192
6193                 swrap_dir = socket_wrapper_dir();
6194                 if (swrap_dir == NULL) {
6195                         free(buf);
6196                         return -1;
6197                 }
6198
6199                 for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
6200                         swrap_un_path(&un_addr, swrap_dir, type, iface, prt);
6201                         if (stat(un_addr.sun_path, &st) != 0) continue;
6202
6203                         msg.msg_name = &un_addr;           /* optional address */
6204                         msg.msg_namelen = sizeof(un_addr); /* size of address */
6205
6206                         /* ignore the any errors in broadcast sends */
6207                         libc_sendmsg(s, &msg, flags);
6208                 }
6209
6210                 SAFE_FREE(swrap_dir);
6211
6212                 SWRAP_LOCK_SI(si);
6213
6214                 swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
6215                 free(buf);
6216
6217                 SWRAP_UNLOCK_SI(si);
6218
6219                 return len;
6220         }
6221
6222         ret = libc_sendmsg(s, &msg, flags);
6223
6224         swrap_sendmsg_after(s, si, &msg, to, ret);
6225
6226         return ret;
6227 }
6228
6229 ssize_t sendmsg(int s, const struct msghdr *omsg, int flags)
6230 {
6231         return swrap_sendmsg(s, omsg, flags);
6232 }
6233
6234 /****************************************************************************
6235  *   READV
6236  ***************************************************************************/
6237
6238 static ssize_t swrap_readv(int s, const struct iovec *vector, int count)
6239 {
6240         struct socket_info *si;
6241         struct msghdr msg;
6242         struct iovec tmp;
6243         struct swrap_address saddr = {
6244                 .sa_socklen = sizeof(struct sockaddr_storage)
6245         };
6246         ssize_t ret;
6247         int rc;
6248
6249         si = find_socket_info(s);
6250         if (si == NULL) {
6251                 return libc_readv(s, vector, count);
6252         }
6253
6254         tmp.iov_base = NULL;
6255         tmp.iov_len = 0;
6256
6257         ZERO_STRUCT(msg);
6258         msg.msg_name = &saddr.sa.s; /* optional address */
6259         msg.msg_namelen = saddr.sa_socklen;      /* size of address */
6260         msg.msg_iov = discard_const_p(struct iovec, vector); /* scatter/gather array */
6261         msg.msg_iovlen = count;        /* # elements in msg_iov */
6262 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6263         msg.msg_control = NULL;        /* ancillary data, see below */
6264         msg.msg_controllen = 0;        /* ancillary data buffer len */
6265         msg.msg_flags = 0;             /* flags on received message */
6266 #endif
6267
6268         rc = swrap_recvmsg_before(s, si, &msg, &tmp);
6269         if (rc < 0) {
6270                 if (rc == -ENOTSOCK) {
6271                         return libc_readv(s, vector, count);
6272                 }
6273                 return -1;
6274         }
6275
6276         ret = libc_readv(s, msg.msg_iov, msg.msg_iovlen);
6277
6278         rc = swrap_recvmsg_after(s, si, &msg, NULL, 0, ret);
6279         if (rc != 0) {
6280                 return rc;
6281         }
6282
6283         return ret;
6284 }
6285
6286 ssize_t readv(int s, const struct iovec *vector, int count)
6287 {
6288         return swrap_readv(s, vector, count);
6289 }
6290
6291 /****************************************************************************
6292  *   WRITEV
6293  ***************************************************************************/
6294
6295 static ssize_t swrap_writev(int s, const struct iovec *vector, int count)
6296 {
6297         struct msghdr msg;
6298         struct iovec tmp;
6299         struct sockaddr_un un_addr;
6300         ssize_t ret;
6301         int rc;
6302         struct socket_info *si = find_socket_info(s);
6303
6304         if (!si) {
6305                 return libc_writev(s, vector, count);
6306         }
6307
6308         tmp.iov_base = NULL;
6309         tmp.iov_len = 0;
6310
6311         ZERO_STRUCT(msg);
6312         msg.msg_name = NULL;           /* optional address */
6313         msg.msg_namelen = 0;           /* size of address */
6314         msg.msg_iov = discard_const_p(struct iovec, vector); /* scatter/gather array */
6315         msg.msg_iovlen = count;        /* # elements in msg_iov */
6316 #ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
6317         msg.msg_control = NULL;        /* ancillary data, see below */
6318         msg.msg_controllen = 0;        /* ancillary data buffer len */
6319         msg.msg_flags = 0;             /* flags on received message */
6320 #endif
6321
6322         rc = swrap_sendmsg_before(s, si, &msg, &tmp, &un_addr, NULL, NULL, NULL);
6323         if (rc < 0) {
6324                 if (rc == -ENOTSOCK) {
6325                         return libc_readv(s, vector, count);
6326                 }
6327                 return -1;
6328         }
6329
6330         ret = libc_writev(s, msg.msg_iov, msg.msg_iovlen);
6331
6332         swrap_sendmsg_after(s, si, &msg, NULL, ret);
6333
6334         return ret;
6335 }
6336
6337 ssize_t writev(int s, const struct iovec *vector, int count)
6338 {
6339         return swrap_writev(s, vector, count);
6340 }
6341
6342 /****************************
6343  * CLOSE
6344  ***************************/
6345
6346 static int swrap_close(int fd)
6347 {
6348         struct socket_info *si = NULL;
6349         int si_index;
6350         int ret;
6351
6352         swrap_mutex_lock(&socket_reset_mutex);
6353
6354         si_index = find_socket_info_index(fd);
6355         if (si_index == -1) {
6356                 swrap_mutex_unlock(&socket_reset_mutex);
6357                 return libc_close(fd);
6358         }
6359
6360         SWRAP_LOG(SWRAP_LOG_TRACE, "Close wrapper for fd=%d", fd);
6361         reset_socket_info_index(fd);
6362
6363         si = swrap_get_socket_info(si_index);
6364
6365         swrap_mutex_lock(&first_free_mutex);
6366         SWRAP_LOCK_SI(si);
6367
6368         ret = libc_close(fd);
6369
6370         swrap_dec_refcount(si);
6371
6372         if (swrap_get_refcount(si) > 0) {
6373                 /* there are still references left */
6374                 goto out;
6375         }
6376
6377         if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
6378                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_SEND, NULL, 0);
6379         }
6380
6381         if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
6382                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_RECV, NULL, 0);
6383                 swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_ACK, NULL, 0);
6384         }
6385
6386         if (si->un_addr.sun_path[0] != '\0') {
6387                 unlink(si->un_addr.sun_path);
6388         }
6389
6390         swrap_set_next_free(si, first_free);
6391         first_free = si_index;
6392
6393 out:
6394         SWRAP_UNLOCK_SI(si);
6395         swrap_mutex_unlock(&first_free_mutex);
6396         swrap_mutex_unlock(&socket_reset_mutex);
6397
6398         return ret;
6399 }
6400
6401 int close(int fd)
6402 {
6403         return swrap_close(fd);
6404 }
6405
6406 /****************************
6407  * DUP
6408  ***************************/
6409
6410 static int swrap_dup(int fd)
6411 {
6412         struct socket_info *si;
6413         int dup_fd, idx;
6414
6415         idx = find_socket_info_index(fd);
6416         if (idx == -1) {
6417                 return libc_dup(fd);
6418         }
6419
6420         si = swrap_get_socket_info(idx);
6421
6422         dup_fd = libc_dup(fd);
6423         if (dup_fd == -1) {
6424                 int saved_errno = errno;
6425                 errno = saved_errno;
6426                 return -1;
6427         }
6428
6429         SWRAP_LOCK_SI(si);
6430
6431         swrap_inc_refcount(si);
6432
6433         SWRAP_UNLOCK_SI(si);
6434
6435         /* Make sure we don't have an entry for the fd */
6436         swrap_remove_stale(dup_fd);
6437
6438         set_socket_info_index(dup_fd, idx);
6439
6440         return dup_fd;
6441 }
6442
6443 int dup(int fd)
6444 {
6445         return swrap_dup(fd);
6446 }
6447
6448 /****************************
6449  * DUP2
6450  ***************************/
6451
6452 static int swrap_dup2(int fd, int newfd)
6453 {
6454         struct socket_info *si;
6455         int dup_fd, idx;
6456
6457         idx = find_socket_info_index(fd);
6458         if (idx == -1) {
6459                 return libc_dup2(fd, newfd);
6460         }
6461
6462         si = swrap_get_socket_info(idx);
6463
6464         if (fd == newfd) {
6465                 /*
6466                  * According to the manpage:
6467                  *
6468                  * "If oldfd is a valid file descriptor, and newfd has the same
6469                  * value as oldfd, then dup2() does nothing, and returns newfd."
6470                  */
6471                 return newfd;
6472         }
6473
6474         if (find_socket_info(newfd)) {
6475                 /* dup2() does an implicit close of newfd, which we
6476                  * need to emulate */
6477                 swrap_close(newfd);
6478         }
6479
6480         dup_fd = libc_dup2(fd, newfd);
6481         if (dup_fd == -1) {
6482                 int saved_errno = errno;
6483                 errno = saved_errno;
6484                 return -1;
6485         }
6486
6487         SWRAP_LOCK_SI(si);
6488
6489         swrap_inc_refcount(si);
6490
6491         SWRAP_UNLOCK_SI(si);
6492
6493         /* Make sure we don't have an entry for the fd */
6494         swrap_remove_stale(dup_fd);
6495
6496         set_socket_info_index(dup_fd, idx);
6497
6498         return dup_fd;
6499 }
6500
6501 int dup2(int fd, int newfd)
6502 {
6503         return swrap_dup2(fd, newfd);
6504 }
6505
6506 /****************************
6507  * FCNTL
6508  ***************************/
6509
6510 static int swrap_vfcntl(int fd, int cmd, va_list va)
6511 {
6512         struct socket_info *si;
6513         int rc, dup_fd, idx;
6514
6515         idx = find_socket_info_index(fd);
6516         if (idx == -1) {
6517                 return libc_vfcntl(fd, cmd, va);
6518         }
6519
6520         si = swrap_get_socket_info(idx);
6521
6522         switch (cmd) {
6523         case F_DUPFD:
6524                 dup_fd = libc_vfcntl(fd, cmd, va);
6525                 if (dup_fd == -1) {
6526                         int saved_errno = errno;
6527                         errno = saved_errno;
6528                         return -1;
6529                 }
6530
6531                 SWRAP_LOCK_SI(si);
6532
6533                 swrap_inc_refcount(si);
6534
6535                 SWRAP_UNLOCK_SI(si);
6536
6537                 /* Make sure we don't have an entry for the fd */
6538                 swrap_remove_stale(dup_fd);
6539
6540                 set_socket_info_index(dup_fd, idx);
6541
6542                 rc = dup_fd;
6543                 break;
6544         default:
6545                 rc = libc_vfcntl(fd, cmd, va);
6546                 break;
6547         }
6548
6549         return rc;
6550 }
6551
6552 int fcntl(int fd, int cmd, ...)
6553 {
6554         va_list va;
6555         int rc;
6556
6557         va_start(va, cmd);
6558
6559         rc = swrap_vfcntl(fd, cmd, va);
6560
6561         va_end(va);
6562
6563         return rc;
6564 }
6565
6566 /****************************
6567  * EVENTFD
6568  ***************************/
6569
6570 #ifdef HAVE_EVENTFD
6571 static int swrap_eventfd(int count, int flags)
6572 {
6573         int fd;
6574
6575         fd = libc_eventfd(count, flags);
6576         if (fd != -1) {
6577                 swrap_remove_stale(fd);
6578         }
6579
6580         return fd;
6581 }
6582
6583 #ifdef HAVE_EVENTFD_UNSIGNED_INT
6584 int eventfd(unsigned int count, int flags)
6585 #else
6586 int eventfd(int count, int flags)
6587 #endif
6588 {
6589         return swrap_eventfd(count, flags);
6590 }
6591 #endif
6592
6593 #ifdef HAVE_PLEDGE
6594 int pledge(const char *promises, const char *paths[])
6595 {
6596         (void)promises; /* unused */
6597         (void)paths; /* unused */
6598
6599         return 0;
6600 }
6601 #endif /* HAVE_PLEDGE */
6602
6603 static void swrap_thread_prepare(void)
6604 {
6605         /*
6606          * This function should only be called here!!
6607          *
6608          * We bind all symobls to avoid deadlocks of the fork is
6609          * interrupted by a signal handler using a symbol of this
6610          * library.
6611          */
6612         swrap_bind_symbol_all();
6613
6614         SWRAP_LOCK_ALL;
6615 }
6616
6617 static void swrap_thread_parent(void)
6618 {
6619         SWRAP_UNLOCK_ALL;
6620 }
6621
6622 static void swrap_thread_child(void)
6623 {
6624         SWRAP_UNLOCK_ALL;
6625 }
6626
6627 /****************************
6628  * CONSTRUCTOR
6629  ***************************/
6630 void swrap_constructor(void)
6631 {
6632         int ret;
6633
6634         /*
6635         * If we hold a lock and the application forks, then the child
6636         * is not able to unlock the mutex and we are in a deadlock.
6637         * This should prevent such deadlocks.
6638         */
6639         pthread_atfork(&swrap_thread_prepare,
6640                        &swrap_thread_parent,
6641                        &swrap_thread_child);
6642
6643         ret = socket_wrapper_init_mutex(&sockets_mutex);
6644         if (ret != 0) {
6645                 SWRAP_LOG(SWRAP_LOG_ERROR,
6646                           "Failed to initialize pthread mutex");
6647                 exit(-1);
6648         }
6649
6650         ret = socket_wrapper_init_mutex(&socket_reset_mutex);
6651         if (ret != 0) {
6652                 SWRAP_LOG(SWRAP_LOG_ERROR,
6653                           "Failed to initialize pthread mutex");
6654                 exit(-1);
6655         }
6656
6657         ret = socket_wrapper_init_mutex(&first_free_mutex);
6658         if (ret != 0) {
6659                 SWRAP_LOG(SWRAP_LOG_ERROR,
6660                           "Failed to initialize pthread mutex");
6661                 exit(-1);
6662         }
6663 }
6664
6665 /****************************
6666  * DESTRUCTOR
6667  ***************************/
6668
6669 /*
6670  * This function is called when the library is unloaded and makes sure that
6671  * sockets get closed and the unix file for the socket are unlinked.
6672  */
6673 void swrap_destructor(void)
6674 {
6675         size_t i;
6676
6677         if (socket_fds_idx != NULL) {
6678                 for (i = 0; i < socket_fds_max; ++i) {
6679                         if (socket_fds_idx[i] != -1) {
6680                                 swrap_close(i);
6681                         }
6682                 }
6683                 SAFE_FREE(socket_fds_idx);
6684         }
6685
6686         SAFE_FREE(sockets);
6687
6688         if (swrap.libc.handle != NULL) {
6689                 dlclose(swrap.libc.handle);
6690         }
6691         if (swrap.libc.socket_handle) {
6692                 dlclose(swrap.libc.socket_handle);
6693         }
6694 }
6695
6696 #if defined(HAVE__SOCKET) && defined(HAVE__CLOSE)
6697 /*
6698  * On FreeBSD 12 (and maybe other platforms)
6699  * system libraries like libresolv prefix there
6700  * syscalls with '_' in order to always use
6701  * the symbols from libc.
6702  *
6703  * In the interaction with resolv_wrapper,
6704  * we need to inject socket wrapper into libresolv,
6705  * which means we need to private all socket
6706  * related syscalls also with the '_' prefix.
6707  *
6708  * This is tested in Samba's 'make test',
6709  * there we noticed that providing '_read'
6710  * and '_open' would cause errors, which
6711  * means we skip '_read', '_write' and
6712  * all non socket related calls without
6713  * further analyzing the problem.
6714  */
6715 #define SWRAP_SYMBOL_ALIAS(__sym, __aliassym) \
6716         extern typeof(__sym) __aliassym __attribute__ ((alias(#__sym)))
6717
6718 #ifdef HAVE_ACCEPT4
6719 SWRAP_SYMBOL_ALIAS(accept4, _accept4);
6720 #endif
6721 SWRAP_SYMBOL_ALIAS(accept, _accept);
6722 SWRAP_SYMBOL_ALIAS(bind, _bind);
6723 SWRAP_SYMBOL_ALIAS(close, _close);
6724 SWRAP_SYMBOL_ALIAS(connect, _connect);
6725 SWRAP_SYMBOL_ALIAS(dup, _dup);
6726 SWRAP_SYMBOL_ALIAS(dup2, _dup2);
6727 SWRAP_SYMBOL_ALIAS(fcntl, _fcntl);
6728 SWRAP_SYMBOL_ALIAS(getpeername, _getpeername);
6729 SWRAP_SYMBOL_ALIAS(getsockname, _getsockname);
6730 SWRAP_SYMBOL_ALIAS(getsockopt, _getsockopt);
6731 SWRAP_SYMBOL_ALIAS(ioctl, _ioctl);
6732 SWRAP_SYMBOL_ALIAS(listen, _listen);
6733 SWRAP_SYMBOL_ALIAS(readv, _readv);
6734 SWRAP_SYMBOL_ALIAS(recv, _recv);
6735 SWRAP_SYMBOL_ALIAS(recvfrom, _recvfrom);
6736 SWRAP_SYMBOL_ALIAS(recvmsg, _recvmsg);
6737 SWRAP_SYMBOL_ALIAS(send, _send);
6738 SWRAP_SYMBOL_ALIAS(sendmsg, _sendmsg);
6739 SWRAP_SYMBOL_ALIAS(sendto, _sendto);
6740 SWRAP_SYMBOL_ALIAS(setsockopt, _setsockopt);
6741 SWRAP_SYMBOL_ALIAS(socket, _socket);
6742 SWRAP_SYMBOL_ALIAS(socketpair, _socketpair);
6743 SWRAP_SYMBOL_ALIAS(writev, _writev);
6744
6745 #endif /* SOCKET_WRAPPER_EXPORT_UNDERSCORE_SYMBOLS */