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