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