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