third_party: Update socket_wrapper to version 1.1.9
[samba.git] / third_party / socket_wrapper / 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 #ifndef FALL_THROUGH
111 # ifdef HAVE_FALLTHROUGH_ATTRIBUTE
112 #  define FALL_THROUGH __attribute__ ((fallthrough))
113 # else /* HAVE_FALLTHROUGH_ATTRIBUTE */
114 #  define FALL_THROUGH
115 # endif /* HAVE_FALLTHROUGH_ATTRIBUTE */
116 #endif /* FALL_THROUGH */
117
118 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
119 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
120 #else
121 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
122 #endif
123
124 #ifdef HAVE_GCC_THREAD_LOCAL_STORAGE
125 # define SWRAP_THREAD __thread
126 #else
127 # define SWRAP_THREAD
128 #endif
129
130 #ifndef MIN
131 #define MIN(a,b) ((a)<(b)?(a):(b))
132 #endif
133
134 #ifndef ZERO_STRUCT
135 #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
136 #endif
137
138 #ifndef ZERO_STRUCTP
139 #define ZERO_STRUCTP(x) do { \
140                 if ((x) != NULL) \
141                         memset((char *)(x), 0, sizeof(*(x))); \
142         } while(0)
143 #endif
144
145 #ifndef discard_const
146 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
147 #endif
148
149 #ifndef discard_const_p
150 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
151 #endif
152
153 #define UNUSED(x) (void)(x)
154
155 #ifdef IPV6_PKTINFO
156 # ifndef IPV6_RECVPKTINFO
157 #  define IPV6_RECVPKTINFO IPV6_PKTINFO
158 # endif /* IPV6_RECVPKTINFO */
159 #endif /* IPV6_PKTINFO */
160
161 /*
162  * On BSD IP_PKTINFO has a different name because during
163  * the time when they implemented it, there was no RFC.
164  * The name for IPv6 is the same as on Linux.
165  */
166 #ifndef IP_PKTINFO
167 # ifdef IP_RECVDSTADDR
168 #  define IP_PKTINFO IP_RECVDSTADDR
169 # endif
170 #endif
171
172 /* Macros for accessing mutexes */
173 # define SWRAP_LOCK(m) do { \
174         pthread_mutex_lock(&(m ## _mutex)); \
175 } while(0)
176
177 # define SWRAP_UNLOCK(m) do { \
178         pthread_mutex_unlock(&(m ## _mutex)); \
179 } while(0)
180
181 /* Add new global locks here please */
182 # define SWRAP_LOCK_ALL \
183         SWRAP_LOCK(libc_symbol_binding); \
184
185 # define SWRAP_UNLOCK_ALL \
186         SWRAP_UNLOCK(libc_symbol_binding); \
187
188
189 #define SWRAP_DLIST_ADD(list,item) do { \
190         if (!(list)) { \
191                 (item)->prev    = NULL; \
192                 (item)->next    = NULL; \
193                 (list)          = (item); \
194         } else { \
195                 (item)->prev    = NULL; \
196                 (item)->next    = (list); \
197                 (list)->prev    = (item); \
198                 (list)          = (item); \
199         } \
200 } while (0)
201
202 #define SWRAP_DLIST_REMOVE(list,item) do { \
203         if ((list) == (item)) { \
204                 (list)          = (item)->next; \
205                 if (list) { \
206                         (list)->prev    = NULL; \
207                 } \
208         } else { \
209                 if ((item)->prev) { \
210                         (item)->prev->next      = (item)->next; \
211                 } \
212                 if ((item)->next) { \
213                         (item)->next->prev      = (item)->prev; \
214                 } \
215         } \
216         (item)->prev    = NULL; \
217         (item)->next    = NULL; \
218 } while (0)
219
220 #define SWRAP_DLIST_ADD_AFTER(list, item, el) \
221 do { \
222         if ((list) == NULL || (el) == NULL) { \
223                 SWRAP_DLIST_ADD(list, item); \
224         } else { \
225                 (item)->prev = (el); \
226                 (item)->next = (el)->next; \
227                 (el)->next = (item); \
228                 if ((item)->next != NULL) { \
229                         (item)->next->prev = (item); \
230                 } \
231         } \
232 } while (0)
233
234 #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID)
235 #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL)
236 #else
237 #define swrapGetTimeOfDay(tval) gettimeofday(tval)
238 #endif
239
240 /* we need to use a very terse format here as IRIX 6.4 silently
241    truncates names to 16 chars, so if we use a longer name then we
242    can't tell which port a packet came from with recvfrom()
243
244    with this format we have 8 chars left for the directory name
245 */
246 #define SOCKET_FORMAT "%c%02X%04X"
247 #define SOCKET_TYPE_CHAR_TCP            'T'
248 #define SOCKET_TYPE_CHAR_UDP            'U'
249 #define SOCKET_TYPE_CHAR_TCP_V6         'X'
250 #define SOCKET_TYPE_CHAR_UDP_V6         'Y'
251
252 /*
253  * Set the packet MTU to 1500 bytes for stream sockets to make it it easier to
254  * format PCAP capture files (as the caller will simply continue from here).
255  */
256 #define SOCKET_WRAPPER_MTU_DEFAULT 1500
257 #define SOCKET_WRAPPER_MTU_MIN     512
258 #define SOCKET_WRAPPER_MTU_MAX     32768
259
260 #define SOCKET_MAX_SOCKETS 1024
261
262
263 /*
264  * Maximum number of socket_info structures that can
265  * be used. Can be overriden by the environment variable
266  * SOCKET_WRAPPER_MAX_SOCKETS.
267  */
268 #define SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT 65535
269
270 #define SOCKET_WRAPPER_MAX_SOCKETS_LIMIT 256000
271
272 /* This limit is to avoid broadcast sendto() needing to stat too many
273  * files.  It may be raised (with a performance cost) to up to 254
274  * without changing the format above */
275 #define MAX_WRAPPED_INTERFACES 64
276
277 struct swrap_address {
278         socklen_t sa_socklen;
279         union {
280                 struct sockaddr s;
281                 struct sockaddr_in in;
282 #ifdef HAVE_IPV6
283                 struct sockaddr_in6 in6;
284 #endif
285                 struct sockaddr_un un;
286                 struct sockaddr_storage ss;
287         } sa;
288 };
289
290 struct socket_info_fd {
291         struct socket_info_fd *prev, *next;
292         int fd;
293
294         /*
295          * Points to corresponding index in array of
296          * socket_info structures
297          */
298         int si_index;
299 };
300
301 int first_free;
302
303 struct socket_info
304 {
305         unsigned int refcount;
306
307         int next_free;
308
309         int family;
310         int type;
311         int protocol;
312         int bound;
313         int bcast;
314         int is_server;
315         int connected;
316         int defer_connect;
317         int pktinfo;
318         int tcp_nodelay;
319
320         /* The unix path so we can unlink it on close() */
321         struct sockaddr_un un_addr;
322
323         struct swrap_address bindname;
324         struct swrap_address myname;
325         struct swrap_address peername;
326
327         struct {
328                 unsigned long pck_snd;
329                 unsigned long pck_rcv;
330         } io;
331 };
332
333 static struct socket_info *sockets;
334 static size_t max_sockets = 0;
335
336 /*
337  * While socket file descriptors are passed among different processes, the
338  * numerical value gets changed. So its better to store it locally to each
339  * process rather than including it within socket_info which will be shared.
340  */
341 static struct socket_info_fd *socket_fds;
342
343 /* The mutex for accessing the global libc.symbols */
344 static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
345
346 /* Function prototypes */
347
348 bool socket_wrapper_enabled(void);
349
350 void swrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
351 void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
352
353 static void swrap_log(enum swrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
354 # define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)
355
356 static void swrap_log(enum swrap_dbglvl_e dbglvl,
357                       const char *func,
358                       const char *format, ...)
359 {
360         char buffer[1024];
361         va_list va;
362         const char *d;
363         unsigned int lvl = 0;
364         const char *prefix = "SWRAP";
365
366         d = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
367         if (d != NULL) {
368                 lvl = atoi(d);
369         }
370
371         if (lvl < dbglvl) {
372                 return;
373         }
374
375         va_start(va, format);
376         vsnprintf(buffer, sizeof(buffer), format, va);
377         va_end(va);
378
379         switch (dbglvl) {
380                 case SWRAP_LOG_ERROR:
381                         prefix = "SWRAP_ERROR";
382                         break;
383                 case SWRAP_LOG_WARN:
384                         prefix = "SWRAP_WARN";
385                         break;
386                 case SWRAP_LOG_DEBUG:
387                         prefix = "SWRAP_DEBUG";
388                         break;
389                 case SWRAP_LOG_TRACE:
390                         prefix = "SWRAP_TRACE";
391                         break;
392         }
393
394         fprintf(stderr,
395                 "%s(%d) - %s: %s\n",
396                 prefix, (int)getpid(), func, buffer);
397 }
398
399 /*********************************************************
400  * SWRAP LOADING LIBC FUNCTIONS
401  *********************************************************/
402
403 #include <dlfcn.h>
404
405 #ifdef HAVE_ACCEPT4
406 typedef int (*__libc_accept4)(int sockfd,
407                               struct sockaddr *addr,
408                               socklen_t *addrlen,
409                               int flags);
410 #else
411 typedef int (*__libc_accept)(int sockfd,
412                              struct sockaddr *addr,
413                              socklen_t *addrlen);
414 #endif
415 typedef int (*__libc_bind)(int sockfd,
416                            const struct sockaddr *addr,
417                            socklen_t addrlen);
418 typedef int (*__libc_close)(int fd);
419 typedef int (*__libc_connect)(int sockfd,
420                               const struct sockaddr *addr,
421                               socklen_t addrlen);
422 typedef int (*__libc_dup)(int fd);
423 typedef int (*__libc_dup2)(int oldfd, int newfd);
424 typedef int (*__libc_fcntl)(int fd, int cmd, ...);
425 typedef FILE *(*__libc_fopen)(const char *name, const char *mode);
426 #ifdef HAVE_FOPEN64
427 typedef FILE *(*__libc_fopen64)(const char *name, const char *mode);
428 #endif
429 #ifdef HAVE_EVENTFD
430 typedef int (*__libc_eventfd)(int count, int flags);
431 #endif
432 typedef int (*__libc_getpeername)(int sockfd,
433                                   struct sockaddr *addr,
434                                   socklen_t *addrlen);
435 typedef int (*__libc_getsockname)(int sockfd,
436                                   struct sockaddr *addr,
437                                   socklen_t *addrlen);
438 typedef int (*__libc_getsockopt)(int sockfd,
439                                int level,
440                                int optname,
441                                void *optval,
442                                socklen_t *optlen);
443 typedef int (*__libc_ioctl)(int d, unsigned long int request, ...);
444 typedef int (*__libc_listen)(int sockfd, int backlog);
445 typedef int (*__libc_open)(const char *pathname, int flags, ...);
446 #ifdef HAVE_OPEN64
447 typedef int (*__libc_open64)(const char *pathname, int flags, ...);
448 #endif /* HAVE_OPEN64 */
449 typedef int (*__libc_openat)(int dirfd, const char *path, int flags, ...);
450 typedef int (*__libc_pipe)(int pipefd[2]);
451 typedef int (*__libc_read)(int fd, void *buf, size_t count);
452 typedef ssize_t (*__libc_readv)(int fd, const struct iovec *iov, int iovcnt);
453 typedef int (*__libc_recv)(int sockfd, void *buf, size_t len, int flags);
454 typedef int (*__libc_recvfrom)(int sockfd,
455                              void *buf,
456                              size_t len,
457                              int flags,
458                              struct sockaddr *src_addr,
459                              socklen_t *addrlen);
460 typedef int (*__libc_recvmsg)(int sockfd, const struct msghdr *msg, int flags);
461 typedef int (*__libc_send)(int sockfd, const void *buf, size_t len, int flags);
462 typedef int (*__libc_sendmsg)(int sockfd, const struct msghdr *msg, int flags);
463 typedef int (*__libc_sendto)(int sockfd,
464                            const void *buf,
465                            size_t len,
466                            int flags,
467                            const  struct sockaddr *dst_addr,
468                            socklen_t addrlen);
469 typedef int (*__libc_setsockopt)(int sockfd,
470                                int level,
471                                int optname,
472                                const void *optval,
473                                socklen_t optlen);
474 #ifdef HAVE_SIGNALFD
475 typedef int (*__libc_signalfd)(int fd, const sigset_t *mask, int flags);
476 #endif
477 typedef int (*__libc_socket)(int domain, int type, int protocol);
478 typedef int (*__libc_socketpair)(int domain, int type, int protocol, int sv[2]);
479 #ifdef HAVE_TIMERFD_CREATE
480 typedef int (*__libc_timerfd_create)(int clockid, int flags);
481 #endif
482 typedef ssize_t (*__libc_write)(int fd, const void *buf, size_t count);
483 typedef ssize_t (*__libc_writev)(int fd, const struct iovec *iov, int iovcnt);
484
485 #define SWRAP_SYMBOL_ENTRY(i) \
486         union { \
487                 __libc_##i f; \
488                 void *obj; \
489         } _libc_##i
490
491 struct swrap_libc_symbols {
492 #ifdef HAVE_ACCEPT4
493         SWRAP_SYMBOL_ENTRY(accept4);
494 #else
495         SWRAP_SYMBOL_ENTRY(accept);
496 #endif
497         SWRAP_SYMBOL_ENTRY(bind);
498         SWRAP_SYMBOL_ENTRY(close);
499         SWRAP_SYMBOL_ENTRY(connect);
500         SWRAP_SYMBOL_ENTRY(dup);
501         SWRAP_SYMBOL_ENTRY(dup2);
502         SWRAP_SYMBOL_ENTRY(fcntl);
503         SWRAP_SYMBOL_ENTRY(fopen);
504 #ifdef HAVE_FOPEN64
505         SWRAP_SYMBOL_ENTRY(fopen64);
506 #endif
507 #ifdef HAVE_EVENTFD
508         SWRAP_SYMBOL_ENTRY(eventfd);
509 #endif
510         SWRAP_SYMBOL_ENTRY(getpeername);
511         SWRAP_SYMBOL_ENTRY(getsockname);
512         SWRAP_SYMBOL_ENTRY(getsockopt);
513         SWRAP_SYMBOL_ENTRY(ioctl);
514         SWRAP_SYMBOL_ENTRY(listen);
515         SWRAP_SYMBOL_ENTRY(open);
516 #ifdef HAVE_OPEN64
517         SWRAP_SYMBOL_ENTRY(open64);
518 #endif
519         SWRAP_SYMBOL_ENTRY(openat);
520         SWRAP_SYMBOL_ENTRY(pipe);
521         SWRAP_SYMBOL_ENTRY(read);
522         SWRAP_SYMBOL_ENTRY(readv);
523         SWRAP_SYMBOL_ENTRY(recv);
524         SWRAP_SYMBOL_ENTRY(recvfrom);
525         SWRAP_SYMBOL_ENTRY(recvmsg);
526         SWRAP_SYMBOL_ENTRY(send);
527         SWRAP_SYMBOL_ENTRY(sendmsg);
528         SWRAP_SYMBOL_ENTRY(sendto);
529         SWRAP_SYMBOL_ENTRY(setsockopt);
530 #ifdef HAVE_SIGNALFD
531         SWRAP_SYMBOL_ENTRY(signalfd);
532 #endif
533         SWRAP_SYMBOL_ENTRY(socket);
534         SWRAP_SYMBOL_ENTRY(socketpair);
535 #ifdef HAVE_TIMERFD_CREATE
536         SWRAP_SYMBOL_ENTRY(timerfd_create);
537 #endif
538         SWRAP_SYMBOL_ENTRY(write);
539         SWRAP_SYMBOL_ENTRY(writev);
540 };
541
542 struct swrap {
543         struct {
544                 void *handle;
545                 void *socket_handle;
546                 struct swrap_libc_symbols symbols;
547         } libc;
548 };
549
550 static struct swrap swrap;
551
552 /* prototypes */
553 static const char *socket_wrapper_dir(void);
554
555 #define LIBC_NAME "libc.so"
556
557 enum swrap_lib {
558     SWRAP_LIBC,
559     SWRAP_LIBNSL,
560     SWRAP_LIBSOCKET,
561 };
562
563 static const char *swrap_str_lib(enum swrap_lib lib)
564 {
565         switch (lib) {
566         case SWRAP_LIBC:
567                 return "libc";
568         case SWRAP_LIBNSL:
569                 return "libnsl";
570         case SWRAP_LIBSOCKET:
571                 return "libsocket";
572         }
573
574         /* Compiler would warn us about unhandled enum value if we get here */
575         return "unknown";
576 }
577
578 static void *swrap_load_lib_handle(enum swrap_lib lib)
579 {
580         int flags = RTLD_LAZY;
581         void *handle = NULL;
582         int i;
583
584 #ifdef RTLD_DEEPBIND
585         flags |= RTLD_DEEPBIND;
586 #endif
587
588         switch (lib) {
589         case SWRAP_LIBNSL:
590                 FALL_THROUGH;
591         case SWRAP_LIBSOCKET:
592 #ifdef HAVE_LIBSOCKET
593                 handle = swrap.libc.socket_handle;
594                 if (handle == NULL) {
595                         for (i = 10; i >= 0; i--) {
596                                 char soname[256] = {0};
597
598                                 snprintf(soname, sizeof(soname), "libsocket.so.%d", i);
599                                 handle = dlopen(soname, flags);
600                                 if (handle != NULL) {
601                                         break;
602                                 }
603                         }
604
605                         swrap.libc.socket_handle = handle;
606                 }
607                 break;
608 #endif
609                 FALL_THROUGH;
610         case SWRAP_LIBC:
611                 handle = swrap.libc.handle;
612 #ifdef LIBC_SO
613                 if (handle == NULL) {
614                         handle = dlopen(LIBC_SO, flags);
615
616                         swrap.libc.handle = handle;
617                 }
618 #endif
619                 if (handle == NULL) {
620                         for (i = 10; i >= 0; i--) {
621                                 char soname[256] = {0};
622
623                                 snprintf(soname, sizeof(soname), "libc.so.%d", i);
624                                 handle = dlopen(soname, flags);
625                                 if (handle != NULL) {
626                                         break;
627                                 }
628                         }
629
630                         swrap.libc.handle = handle;
631                 }
632                 break;
633         }
634
635         if (handle == NULL) {
636 #ifdef RTLD_NEXT
637                 handle = swrap.libc.handle = swrap.libc.socket_handle = RTLD_NEXT;
638 #else
639                 SWRAP_LOG(SWRAP_LOG_ERROR,
640                           "Failed to dlopen library: %s\n",
641                           dlerror());
642                 exit(-1);
643 #endif
644         }
645
646         return handle;
647 }
648
649 static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
650 {
651         void *handle;
652         void *func;
653
654         handle = swrap_load_lib_handle(lib);
655
656         func = dlsym(handle, fn_name);
657         if (func == NULL) {
658                 SWRAP_LOG(SWRAP_LOG_ERROR,
659                           "Failed to find %s: %s\n",
660                           fn_name,
661                           dlerror());
662                 exit(-1);
663         }
664
665         SWRAP_LOG(SWRAP_LOG_TRACE,
666                   "Loaded %s from %s",
667                   fn_name,
668                   swrap_str_lib(lib));
669
670         return func;
671 }
672
673 #define swrap_bind_symbol_libc(sym_name) \
674         if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
675                 SWRAP_LOCK(libc_symbol_binding); \
676                 if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
677                         swrap.libc.symbols._libc_##sym_name.obj = \
678                                 _swrap_bind_symbol(SWRAP_LIBC, #sym_name); \
679                 } \
680                 SWRAP_UNLOCK(libc_symbol_binding); \
681         }
682
683 #define swrap_bind_symbol_libsocket(sym_name) \
684         if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
685                 SWRAP_LOCK(libc_symbol_binding); \
686                 if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
687                         swrap.libc.symbols._libc_##sym_name.obj = \
688                                 _swrap_bind_symbol(SWRAP_LIBSOCKET, #sym_name); \
689                 } \
690                 SWRAP_UNLOCK(libc_symbol_binding); \
691         }
692
693 #define swrap_bind_symbol_libnsl(sym_name) \
694         if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
695                 SWRAP_LOCK(libc_symbol_binding); \
696                 if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
697                         swrap.libc.symbols._libc_##sym_name.obj = \
698                                 _swrap_bind_symbol(SWRAP_LIBNSL, #sym_name); \
699                 } \
700                 SWRAP_UNLOCK(libc_symbol_binding); \
701         }
702
703 /****************************************************************************
704  *                               IMPORTANT
705  ****************************************************************************
706  *
707  * Functions especially from libc need to be loaded individually, you can't
708  * load all at once or gdb will segfault at startup. The same applies to
709  * valgrind and has probably something todo with with the linker.  So we need
710  * load each function at the point it is called the first time.
711  *
712  ****************************************************************************/
713
714 #ifdef HAVE_ACCEPT4
715 static int libc_accept4(int sockfd,
716                         struct sockaddr *addr,
717                         socklen_t *addrlen,
718                         int flags)
719 {
720         swrap_bind_symbol_libsocket(accept4);
721
722         return swrap.libc.symbols._libc_accept4.f(sockfd, addr, addrlen, flags);
723 }
724
725 #else /* HAVE_ACCEPT4 */
726
727 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
728 {
729         swrap_bind_symbol_libsocket(accept);
730
731         return swrap.libc.symbols._libc_accept.f(sockfd, addr, addrlen);
732 }
733 #endif /* HAVE_ACCEPT4 */
734
735 static int libc_bind(int sockfd,
736                      const struct sockaddr *addr,
737                      socklen_t addrlen)
738 {
739         swrap_bind_symbol_libsocket(bind);
740
741         return swrap.libc.symbols._libc_bind.f(sockfd, addr, addrlen);
742 }
743
744 static int libc_close(int fd)
745 {
746         swrap_bind_symbol_libc(close);
747
748         return swrap.libc.symbols._libc_close.f(fd);
749 }
750
751 static int libc_connect(int sockfd,
752                         const struct sockaddr *addr,
753                         socklen_t addrlen)
754 {
755         swrap_bind_symbol_libsocket(connect);
756
757         return swrap.libc.symbols._libc_connect.f(sockfd, addr, addrlen);
758 }
759
760 static int libc_dup(int fd)
761 {
762         swrap_bind_symbol_libc(dup);
763
764         return swrap.libc.symbols._libc_dup.f(fd);
765 }
766
767 static int libc_dup2(int oldfd, int newfd)
768 {
769         swrap_bind_symbol_libc(dup2);
770
771         return swrap.libc.symbols._libc_dup2.f(oldfd, newfd);
772 }
773
774 #ifdef HAVE_EVENTFD
775 static int libc_eventfd(int count, int flags)
776 {
777         swrap_bind_symbol_libc(eventfd);
778
779         return swrap.libc.symbols._libc_eventfd.f(count, flags);
780 }
781 #endif
782
783 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
784 static int libc_vfcntl(int fd, int cmd, va_list ap)
785 {
786         void *arg;
787         int rc;
788
789         swrap_bind_symbol_libc(fcntl);
790
791         arg = va_arg(ap, void *);
792
793         rc = swrap.libc.symbols._libc_fcntl.f(fd, cmd, arg);
794
795         return rc;
796 }
797
798 static int libc_getpeername(int sockfd,
799                             struct sockaddr *addr,
800                             socklen_t *addrlen)
801 {
802         swrap_bind_symbol_libsocket(getpeername);
803
804         return swrap.libc.symbols._libc_getpeername.f(sockfd, addr, addrlen);
805 }
806
807 static int libc_getsockname(int sockfd,
808                             struct sockaddr *addr,
809                             socklen_t *addrlen)
810 {
811         swrap_bind_symbol_libsocket(getsockname);
812
813         return swrap.libc.symbols._libc_getsockname.f(sockfd, addr, addrlen);
814 }
815
816 static int libc_getsockopt(int sockfd,
817                            int level,
818                            int optname,
819                            void *optval,
820                            socklen_t *optlen)
821 {
822         swrap_bind_symbol_libsocket(getsockopt);
823
824         return swrap.libc.symbols._libc_getsockopt.f(sockfd,
825                                                      level,
826                                                      optname,
827                                                      optval,
828                                                      optlen);
829 }
830
831 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
832 static int libc_vioctl(int d, unsigned long int request, va_list ap)
833 {
834         void *arg;
835         int rc;
836
837         swrap_bind_symbol_libc(ioctl);
838
839         arg = va_arg(ap, void *);
840
841         rc = swrap.libc.symbols._libc_ioctl.f(d, request, arg);
842
843         return rc;
844 }
845
846 static int libc_listen(int sockfd, int backlog)
847 {
848         swrap_bind_symbol_libsocket(listen);
849
850         return swrap.libc.symbols._libc_listen.f(sockfd, backlog);
851 }
852
853 static FILE *libc_fopen(const char *name, const char *mode)
854 {
855         swrap_bind_symbol_libc(fopen);
856
857         return swrap.libc.symbols._libc_fopen.f(name, mode);
858 }
859
860 #ifdef HAVE_FOPEN64
861 static FILE *libc_fopen64(const char *name, const char *mode)
862 {
863         swrap_bind_symbol_libc(fopen64);
864
865         return swrap.libc.symbols._libc_fopen64.f(name, mode);
866 }
867 #endif /* HAVE_FOPEN64 */
868
869 static int libc_vopen(const char *pathname, int flags, va_list ap)
870 {
871         int mode = 0;
872         int fd;
873
874         swrap_bind_symbol_libc(open);
875
876         if (flags & O_CREAT) {
877                 mode = va_arg(ap, int);
878         }
879         fd = swrap.libc.symbols._libc_open.f(pathname, flags, (mode_t)mode);
880
881         return fd;
882 }
883
884 static int libc_open(const char *pathname, int flags, ...)
885 {
886         va_list ap;
887         int fd;
888
889         va_start(ap, flags);
890         fd = libc_vopen(pathname, flags, ap);
891         va_end(ap);
892
893         return fd;
894 }
895
896 #ifdef HAVE_OPEN64
897 static int libc_vopen64(const char *pathname, int flags, va_list ap)
898 {
899         int mode = 0;
900         int fd;
901
902         swrap_bind_symbol_libc(open64);
903
904         if (flags & O_CREAT) {
905                 mode = va_arg(ap, int);
906         }
907         fd = swrap.libc.symbols._libc_open64.f(pathname, flags, (mode_t)mode);
908
909         return fd;
910 }
911 #endif /* HAVE_OPEN64 */
912
913 static int libc_vopenat(int dirfd, const char *path, int flags, va_list ap)
914 {
915         int mode = 0;
916         int fd;
917
918         swrap_bind_symbol_libc(openat);
919
920         if (flags & O_CREAT) {
921                 mode = va_arg(ap, int);
922         }
923         fd = swrap.libc.symbols._libc_openat.f(dirfd,
924                                                path,
925                                                flags,
926                                                (mode_t)mode);
927
928         return fd;
929 }
930
931 #if 0
932 static int libc_openat(int dirfd, const char *path, int flags, ...)
933 {
934         va_list ap;
935         int fd;
936
937         va_start(ap, flags);
938         fd = libc_vopenat(dirfd, path, flags, ap);
939         va_end(ap);
940
941         return fd;
942 }
943 #endif
944
945 static int libc_pipe(int pipefd[2])
946 {
947         swrap_bind_symbol_libsocket(pipe);
948
949         return swrap.libc.symbols._libc_pipe.f(pipefd);
950 }
951
952 static int libc_read(int fd, void *buf, size_t count)
953 {
954         swrap_bind_symbol_libc(read);
955
956         return swrap.libc.symbols._libc_read.f(fd, buf, count);
957 }
958
959 static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
960 {
961         swrap_bind_symbol_libsocket(readv);
962
963         return swrap.libc.symbols._libc_readv.f(fd, iov, iovcnt);
964 }
965
966 static int libc_recv(int sockfd, void *buf, size_t len, int flags)
967 {
968         swrap_bind_symbol_libsocket(recv);
969
970         return swrap.libc.symbols._libc_recv.f(sockfd, buf, len, flags);
971 }
972
973 static int libc_recvfrom(int sockfd,
974                          void *buf,
975                          size_t len,
976                          int flags,
977                          struct sockaddr *src_addr,
978                          socklen_t *addrlen)
979 {
980         swrap_bind_symbol_libsocket(recvfrom);
981
982         return swrap.libc.symbols._libc_recvfrom.f(sockfd,
983                                                    buf,
984                                                    len,
985                                                    flags,
986                                                    src_addr,
987                                                    addrlen);
988 }
989
990 static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
991 {
992         swrap_bind_symbol_libsocket(recvmsg);
993
994         return swrap.libc.symbols._libc_recvmsg.f(sockfd, msg, flags);
995 }
996
997 static int libc_send(int sockfd, const void *buf, size_t len, int flags)
998 {
999         swrap_bind_symbol_libsocket(send);
1000
1001         return swrap.libc.symbols._libc_send.f(sockfd, buf, len, flags);
1002 }
1003
1004 static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
1005 {
1006         swrap_bind_symbol_libsocket(sendmsg);
1007
1008         return swrap.libc.symbols._libc_sendmsg.f(sockfd, msg, flags);
1009 }
1010
1011 static int libc_sendto(int sockfd,
1012                        const void *buf,
1013                        size_t len,
1014                        int flags,
1015                        const  struct sockaddr *dst_addr,
1016                        socklen_t addrlen)
1017 {
1018         swrap_bind_symbol_libsocket(sendto);
1019
1020         return swrap.libc.symbols._libc_sendto.f(sockfd,
1021                                                  buf,
1022                                                  len,
1023                                                  flags,
1024                                                  dst_addr,
1025                                                  addrlen);
1026 }
1027
1028 static int libc_setsockopt(int sockfd,
1029                            int level,
1030                            int optname,
1031                            const void *optval,
1032                            socklen_t optlen)
1033 {
1034         swrap_bind_symbol_libsocket(setsockopt);
1035
1036         return swrap.libc.symbols._libc_setsockopt.f(sockfd,
1037                                                      level,
1038                                                      optname,
1039                                                      optval,
1040                                                      optlen);
1041 }
1042
1043 #ifdef HAVE_SIGNALFD
1044 static int libc_signalfd(int fd, const sigset_t *mask, int flags)
1045 {
1046         swrap_bind_symbol_libsocket(signalfd);
1047
1048         return swrap.libc.symbols._libc_signalfd.f(fd, mask, flags);
1049 }
1050 #endif
1051
1052 static int libc_socket(int domain, int type, int protocol)
1053 {
1054         swrap_bind_symbol_libsocket(socket);
1055
1056         return swrap.libc.symbols._libc_socket.f(domain, type, protocol);
1057 }
1058
1059 static int libc_socketpair(int domain, int type, int protocol, int sv[2])
1060 {
1061         swrap_bind_symbol_libsocket(socketpair);
1062
1063         return swrap.libc.symbols._libc_socketpair.f(domain, type, protocol, sv);
1064 }
1065
1066 #ifdef HAVE_TIMERFD_CREATE
1067 static int libc_timerfd_create(int clockid, int flags)
1068 {
1069         swrap_bind_symbol_libc(timerfd_create);
1070
1071         return swrap.libc.symbols._libc_timerfd_create.f(clockid, flags);
1072 }
1073 #endif
1074
1075 static ssize_t libc_write(int fd, const void *buf, size_t count)
1076 {
1077         swrap_bind_symbol_libc(write);
1078
1079         return swrap.libc.symbols._libc_write.f(fd, buf, count);
1080 }
1081
1082 static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
1083 {
1084         swrap_bind_symbol_libsocket(writev);
1085
1086         return swrap.libc.symbols._libc_writev.f(fd, iov, iovcnt);
1087 }
1088
1089 /* DO NOT call this function during library initialization! */
1090 static void swrap_bind_symbol_all(void)
1091 {
1092 #ifdef HAVE_ACCEPT4
1093         swrap_bind_symbol_libsocket(accept4);
1094 #else
1095         swrap_bind_symbol_libsocket(accept);
1096 #endif
1097         swrap_bind_symbol_libsocket(bind);
1098         swrap_bind_symbol_libc(close);
1099         swrap_bind_symbol_libsocket(connect);
1100         swrap_bind_symbol_libc(dup);
1101         swrap_bind_symbol_libc(dup2);
1102         swrap_bind_symbol_libc(fcntl);
1103         swrap_bind_symbol_libc(fopen);
1104 #ifdef HAVE_FOPEN64
1105         swrap_bind_symbol_libc(fopen64);
1106 #endif
1107 #ifdef HAVE_EVENTFD
1108         swrap_bind_symbol_libc(eventfd);
1109 #endif
1110         swrap_bind_symbol_libsocket(getpeername);
1111         swrap_bind_symbol_libsocket(getsockname);
1112         swrap_bind_symbol_libsocket(getsockopt);
1113         swrap_bind_symbol_libc(ioctl);
1114         swrap_bind_symbol_libsocket(listen);
1115         swrap_bind_symbol_libc(open);
1116 #ifdef HAVE_OPEN64
1117         swrap_bind_symbol_libc(open64);
1118 #endif
1119         swrap_bind_symbol_libc(openat);
1120         swrap_bind_symbol_libsocket(pipe);
1121         swrap_bind_symbol_libc(read);
1122         swrap_bind_symbol_libsocket(readv);
1123         swrap_bind_symbol_libsocket(recv);
1124         swrap_bind_symbol_libsocket(recvfrom);
1125         swrap_bind_symbol_libsocket(recvmsg);
1126         swrap_bind_symbol_libsocket(send);
1127         swrap_bind_symbol_libsocket(sendmsg);
1128         swrap_bind_symbol_libsocket(sendto);
1129         swrap_bind_symbol_libsocket(setsockopt);
1130 #ifdef HAVE_SIGNALFD
1131         swrap_bind_symbol_libsocket(signalfd);
1132 #endif
1133         swrap_bind_symbol_libsocket(socket);
1134         swrap_bind_symbol_libsocket(socketpair);
1135 #ifdef HAVE_TIMERFD_CREATE
1136         swrap_bind_symbol_libc(timerfd_create);
1137 #endif
1138         swrap_bind_symbol_libc(write);
1139         swrap_bind_symbol_libsocket(writev);
1140 }
1141
1142 /*********************************************************
1143  * SWRAP HELPER FUNCTIONS
1144  *********************************************************/
1145
1146 #ifdef HAVE_IPV6
1147 /*
1148  * FD00::5357:5FXX
1149  */
1150 static const struct in6_addr *swrap_ipv6(void)
1151 {
1152         static struct in6_addr v;
1153         static int initialized;
1154         int ret;
1155
1156         if (initialized) {
1157                 return &v;
1158         }
1159         initialized = 1;
1160
1161         ret = inet_pton(AF_INET6, "FD00::5357:5F00", &v);
1162         if (ret <= 0) {
1163                 abort();
1164         }
1165
1166         return &v;
1167 }
1168 #endif
1169
1170 static void set_port(int family, int prt, struct swrap_address *addr)
1171 {
1172         switch (family) {
1173         case AF_INET:
1174                 addr->sa.in.sin_port = htons(prt);
1175                 break;
1176 #ifdef HAVE_IPV6
1177         case AF_INET6:
1178                 addr->sa.in6.sin6_port = htons(prt);
1179                 break;
1180 #endif
1181         }
1182 }
1183
1184 static size_t socket_length(int family)
1185 {
1186         switch (family) {
1187         case AF_INET:
1188                 return sizeof(struct sockaddr_in);
1189 #ifdef HAVE_IPV6
1190         case AF_INET6:
1191                 return sizeof(struct sockaddr_in6);
1192 #endif
1193         }
1194         return 0;
1195 }
1196
1197 static const char *socket_wrapper_dir(void)
1198 {
1199         const char *s = getenv("SOCKET_WRAPPER_DIR");
1200         if (s == NULL) {
1201                 return NULL;
1202         }
1203         /* TODO use realpath(3) here, when we add support for threads */
1204         if (strncmp(s, "./", 2) == 0) {
1205                 s += 2;
1206         }
1207
1208         SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", s);
1209         return s;
1210 }
1211
1212 static unsigned int socket_wrapper_mtu(void)
1213 {
1214         static unsigned int max_mtu = 0;
1215         unsigned int tmp;
1216         const char *s;
1217         char *endp;
1218
1219         if (max_mtu != 0) {
1220                 return max_mtu;
1221         }
1222
1223         max_mtu = SOCKET_WRAPPER_MTU_DEFAULT;
1224
1225         s = getenv("SOCKET_WRAPPER_MTU");
1226         if (s == NULL) {
1227                 goto done;
1228         }
1229
1230         tmp = strtol(s, &endp, 10);
1231         if (s == endp) {
1232                 goto done;
1233         }
1234
1235         if (tmp < SOCKET_WRAPPER_MTU_MIN || tmp > SOCKET_WRAPPER_MTU_MAX) {
1236                 goto done;
1237         }
1238         max_mtu = tmp;
1239
1240 done:
1241         return max_mtu;
1242 }
1243
1244 static size_t socket_wrapper_max_sockets(void)
1245 {
1246         const char *s;
1247         unsigned long tmp;
1248         char *endp;
1249
1250         if (max_sockets != 0) {
1251                 return max_sockets;
1252         }
1253
1254         max_sockets = SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT;
1255
1256         s = getenv("SOCKET_WRAPPER_MAX_SOCKETS");
1257         if (s == NULL || s[0] == '\0') {
1258                 goto done;
1259         }
1260
1261         tmp = strtoul(s, &endp, 10);
1262         if (s == endp) {
1263                 goto done;
1264         }
1265         if (tmp == 0 || tmp > SOCKET_WRAPPER_MAX_SOCKETS_LIMIT) {
1266                 SWRAP_LOG(SWRAP_LOG_ERROR,
1267                           "Invalid number of sockets specified, using default.");
1268                 goto done;
1269         }
1270
1271         max_sockets = tmp;
1272
1273 done:
1274         return max_sockets;
1275 }
1276
1277 static void socket_wrapper_init_sockets(void)
1278 {
1279         size_t i;
1280
1281         if (sockets != NULL) {
1282                 return;
1283         }
1284
1285         max_sockets = socket_wrapper_max_sockets();
1286
1287         sockets = (struct socket_info *)calloc(max_sockets,
1288                                                sizeof(struct socket_info));
1289
1290         if (sockets == NULL) {
1291                 SWRAP_LOG(SWRAP_LOG_ERROR,
1292                           "Failed to allocate sockets array.\n");
1293                 exit(-1);
1294         }
1295
1296         first_free = 0;
1297
1298         for (i = 0; i < max_sockets; i++) {
1299                 sockets[i].next_free = i+1;
1300         }
1301
1302         sockets[max_sockets-1].next_free = -1;
1303 }
1304
1305 bool socket_wrapper_enabled(void)
1306 {
1307         const char *s = socket_wrapper_dir();
1308
1309         if (s == NULL) {
1310                 return false;
1311         }
1312
1313         socket_wrapper_init_sockets();
1314
1315         return true;
1316 }
1317
1318 static unsigned int socket_wrapper_default_iface(void)
1319 {
1320         const char *s = getenv("SOCKET_WRAPPER_DEFAULT_IFACE");
1321         if (s) {
1322                 unsigned int iface;
1323                 if (sscanf(s, "%u", &iface) == 1) {
1324                         if (iface >= 1 && iface <= MAX_WRAPPED_INTERFACES) {
1325                                 return iface;
1326                         }
1327                 }
1328         }
1329
1330         return 1;/* 127.0.0.1 */
1331 }
1332
1333 /*
1334  * Return the first free entry (if any) and make
1335  * it re-usable again (by nulling it out)
1336  */
1337 static int socket_wrapper_first_free_index(void)
1338 {
1339         int next_free;
1340
1341         if (first_free == -1) {
1342                 return -1;
1343         }
1344
1345         next_free = sockets[first_free].next_free;
1346         ZERO_STRUCT(sockets[first_free]);
1347         sockets[first_free].next_free = next_free;
1348
1349         return first_free;
1350 }
1351
1352 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len)
1353 {
1354         unsigned int iface;
1355         unsigned int prt;
1356         const char *p;
1357         char type;
1358
1359         p = strrchr(un->sun_path, '/');
1360         if (p) p++; else p = un->sun_path;
1361
1362         if (sscanf(p, SOCKET_FORMAT, &type, &iface, &prt) != 3) {
1363                 errno = EINVAL;
1364                 return -1;
1365         }
1366
1367         SWRAP_LOG(SWRAP_LOG_TRACE, "type %c iface %u port %u",
1368                         type, iface, prt);
1369
1370         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1371                 errno = EINVAL;
1372                 return -1;
1373         }
1374
1375         if (prt > 0xFFFF) {
1376                 errno = EINVAL;
1377                 return -1;
1378         }
1379
1380         switch(type) {
1381         case SOCKET_TYPE_CHAR_TCP:
1382         case SOCKET_TYPE_CHAR_UDP: {
1383                 struct sockaddr_in *in2 = (struct sockaddr_in *)(void *)in;
1384
1385                 if ((*len) < sizeof(*in2)) {
1386                     errno = EINVAL;
1387                     return -1;
1388                 }
1389
1390                 memset(in2, 0, sizeof(*in2));
1391                 in2->sin_family = AF_INET;
1392                 in2->sin_addr.s_addr = htonl((127<<24) | iface);
1393                 in2->sin_port = htons(prt);
1394
1395                 *len = sizeof(*in2);
1396                 break;
1397         }
1398 #ifdef HAVE_IPV6
1399         case SOCKET_TYPE_CHAR_TCP_V6:
1400         case SOCKET_TYPE_CHAR_UDP_V6: {
1401                 struct sockaddr_in6 *in2 = (struct sockaddr_in6 *)(void *)in;
1402
1403                 if ((*len) < sizeof(*in2)) {
1404                         errno = EINVAL;
1405                         return -1;
1406                 }
1407
1408                 memset(in2, 0, sizeof(*in2));
1409                 in2->sin6_family = AF_INET6;
1410                 in2->sin6_addr = *swrap_ipv6();
1411                 in2->sin6_addr.s6_addr[15] = iface;
1412                 in2->sin6_port = htons(prt);
1413
1414                 *len = sizeof(*in2);
1415                 break;
1416         }
1417 #endif
1418         default:
1419                 errno = EINVAL;
1420                 return -1;
1421         }
1422
1423         return 0;
1424 }
1425
1426 static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
1427                                 int *bcast)
1428 {
1429         char type = '\0';
1430         unsigned int prt;
1431         unsigned int iface;
1432         int is_bcast = 0;
1433
1434         if (bcast) *bcast = 0;
1435
1436         switch (inaddr->sa_family) {
1437         case AF_INET: {
1438                 const struct sockaddr_in *in =
1439                     (const struct sockaddr_in *)(const void *)inaddr;
1440                 unsigned int addr = ntohl(in->sin_addr.s_addr);
1441                 char u_type = '\0';
1442                 char b_type = '\0';
1443                 char a_type = '\0';
1444
1445                 switch (si->type) {
1446                 case SOCK_STREAM:
1447                         u_type = SOCKET_TYPE_CHAR_TCP;
1448                         break;
1449                 case SOCK_DGRAM:
1450                         u_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                 prt = ntohs(in->sin_port);
1461                 if (a_type && addr == 0xFFFFFFFF) {
1462                         /* 255.255.255.255 only udp */
1463                         is_bcast = 2;
1464                         type = a_type;
1465                         iface = socket_wrapper_default_iface();
1466                 } else if (b_type && addr == 0x7FFFFFFF) {
1467                         /* 127.255.255.255 only udp */
1468                         is_bcast = 1;
1469                         type = b_type;
1470                         iface = socket_wrapper_default_iface();
1471                 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
1472                         /* 127.0.0.X */
1473                         is_bcast = 0;
1474                         type = u_type;
1475                         iface = (addr & 0x000000FF);
1476                 } else {
1477                         errno = ENETUNREACH;
1478                         return -1;
1479                 }
1480                 if (bcast) *bcast = is_bcast;
1481                 break;
1482         }
1483 #ifdef HAVE_IPV6
1484         case AF_INET6: {
1485                 const struct sockaddr_in6 *in =
1486                     (const struct sockaddr_in6 *)(const void *)inaddr;
1487                 struct in6_addr cmp1, cmp2;
1488
1489                 switch (si->type) {
1490                 case SOCK_STREAM:
1491                         type = SOCKET_TYPE_CHAR_TCP_V6;
1492                         break;
1493                 case SOCK_DGRAM:
1494                         type = SOCKET_TYPE_CHAR_UDP_V6;
1495                         break;
1496                 default:
1497                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1498                         errno = ESOCKTNOSUPPORT;
1499                         return -1;
1500                 }
1501
1502                 /* XXX no multicast/broadcast */
1503
1504                 prt = ntohs(in->sin6_port);
1505
1506                 cmp1 = *swrap_ipv6();
1507                 cmp2 = in->sin6_addr;
1508                 cmp2.s6_addr[15] = 0;
1509                 if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
1510                         iface = in->sin6_addr.s6_addr[15];
1511                 } else {
1512                         errno = ENETUNREACH;
1513                         return -1;
1514                 }
1515
1516                 break;
1517         }
1518 #endif
1519         default:
1520                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!\n");
1521                 errno = ENETUNREACH;
1522                 return -1;
1523         }
1524
1525         if (prt == 0) {
1526                 SWRAP_LOG(SWRAP_LOG_WARN, "Port not set\n");
1527                 errno = EINVAL;
1528                 return -1;
1529         }
1530
1531         if (is_bcast) {
1532                 snprintf(un->sun_path, sizeof(un->sun_path), "%s/EINVAL",
1533                          socket_wrapper_dir());
1534                 SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1535                 /* the caller need to do more processing */
1536                 return 0;
1537         }
1538
1539         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1540                  socket_wrapper_dir(), type, iface, prt);
1541         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1542
1543         return 0;
1544 }
1545
1546 static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *inaddr, struct sockaddr_un *un,
1547                                int *bcast)
1548 {
1549         char type = '\0';
1550         unsigned int prt;
1551         unsigned int iface;
1552         struct stat st;
1553         int is_bcast = 0;
1554
1555         if (bcast) *bcast = 0;
1556
1557         switch (si->family) {
1558         case AF_INET: {
1559                 const struct sockaddr_in *in =
1560                     (const struct sockaddr_in *)(const void *)inaddr;
1561                 unsigned int addr = ntohl(in->sin_addr.s_addr);
1562                 char u_type = '\0';
1563                 char d_type = '\0';
1564                 char b_type = '\0';
1565                 char a_type = '\0';
1566
1567                 prt = ntohs(in->sin_port);
1568
1569                 switch (si->type) {
1570                 case SOCK_STREAM:
1571                         u_type = SOCKET_TYPE_CHAR_TCP;
1572                         d_type = SOCKET_TYPE_CHAR_TCP;
1573                         break;
1574                 case SOCK_DGRAM:
1575                         u_type = SOCKET_TYPE_CHAR_UDP;
1576                         d_type = SOCKET_TYPE_CHAR_UDP;
1577                         a_type = SOCKET_TYPE_CHAR_UDP;
1578                         b_type = SOCKET_TYPE_CHAR_UDP;
1579                         break;
1580                 default:
1581                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1582                         errno = ESOCKTNOSUPPORT;
1583                         return -1;
1584                 }
1585
1586                 if (addr == 0) {
1587                         /* 0.0.0.0 */
1588                         is_bcast = 0;
1589                         type = d_type;
1590                         iface = socket_wrapper_default_iface();
1591                 } else if (a_type && addr == 0xFFFFFFFF) {
1592                         /* 255.255.255.255 only udp */
1593                         is_bcast = 2;
1594                         type = a_type;
1595                         iface = socket_wrapper_default_iface();
1596                 } else if (b_type && addr == 0x7FFFFFFF) {
1597                         /* 127.255.255.255 only udp */
1598                         is_bcast = 1;
1599                         type = b_type;
1600                         iface = socket_wrapper_default_iface();
1601                 } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
1602                         /* 127.0.0.X */
1603                         is_bcast = 0;
1604                         type = u_type;
1605                         iface = (addr & 0x000000FF);
1606                 } else {
1607                         errno = EADDRNOTAVAIL;
1608                         return -1;
1609                 }
1610
1611                 /* Store the bind address for connect() */
1612                 if (si->bindname.sa_socklen == 0) {
1613                         struct sockaddr_in bind_in;
1614                         socklen_t blen = sizeof(struct sockaddr_in);
1615
1616                         ZERO_STRUCT(bind_in);
1617                         bind_in.sin_family = in->sin_family;
1618                         bind_in.sin_port = in->sin_port;
1619                         bind_in.sin_addr.s_addr = htonl(0x7F000000 | iface);
1620
1621                         si->bindname.sa_socklen = blen;
1622                         memcpy(&si->bindname.sa.in, &bind_in, blen);
1623                 }
1624
1625                 break;
1626         }
1627 #ifdef HAVE_IPV6
1628         case AF_INET6: {
1629                 const struct sockaddr_in6 *in =
1630                     (const struct sockaddr_in6 *)(const void *)inaddr;
1631                 struct in6_addr cmp1, cmp2;
1632
1633                 switch (si->type) {
1634                 case SOCK_STREAM:
1635                         type = SOCKET_TYPE_CHAR_TCP_V6;
1636                         break;
1637                 case SOCK_DGRAM:
1638                         type = SOCKET_TYPE_CHAR_UDP_V6;
1639                         break;
1640                 default:
1641                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1642                         errno = ESOCKTNOSUPPORT;
1643                         return -1;
1644                 }
1645
1646                 /* XXX no multicast/broadcast */
1647
1648                 prt = ntohs(in->sin6_port);
1649
1650                 cmp1 = *swrap_ipv6();
1651                 cmp2 = in->sin6_addr;
1652                 cmp2.s6_addr[15] = 0;
1653                 if (IN6_IS_ADDR_UNSPECIFIED(&in->sin6_addr)) {
1654                         iface = socket_wrapper_default_iface();
1655                 } else if (IN6_ARE_ADDR_EQUAL(&cmp1, &cmp2)) {
1656                         iface = in->sin6_addr.s6_addr[15];
1657                 } else {
1658                         errno = EADDRNOTAVAIL;
1659                         return -1;
1660                 }
1661
1662                 /* Store the bind address for connect() */
1663                 if (si->bindname.sa_socklen == 0) {
1664                         struct sockaddr_in6 bind_in;
1665                         socklen_t blen = sizeof(struct sockaddr_in6);
1666
1667                         ZERO_STRUCT(bind_in);
1668                         bind_in.sin6_family = in->sin6_family;
1669                         bind_in.sin6_port = in->sin6_port;
1670
1671                         bind_in.sin6_addr = *swrap_ipv6();
1672                         bind_in.sin6_addr.s6_addr[15] = iface;
1673
1674                         memcpy(&si->bindname.sa.in6, &bind_in, blen);
1675                         si->bindname.sa_socklen = blen;
1676                 }
1677
1678                 break;
1679         }
1680 #endif
1681         default:
1682                 SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1683                 errno = EADDRNOTAVAIL;
1684                 return -1;
1685         }
1686
1687
1688         if (bcast) *bcast = is_bcast;
1689
1690         if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
1691                 errno = EINVAL;
1692                 return -1;
1693         }
1694
1695         if (prt == 0) {
1696                 /* handle auto-allocation of ephemeral ports */
1697                 for (prt = 5001; prt < 10000; prt++) {
1698                         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1699                                  socket_wrapper_dir(), type, iface, prt);
1700                         if (stat(un->sun_path, &st) == 0) continue;
1701
1702                         set_port(si->family, prt, &si->myname);
1703                         set_port(si->family, prt, &si->bindname);
1704
1705                         break;
1706                 }
1707                 if (prt == 10000) {
1708                         errno = ENFILE;
1709                         return -1;
1710                 }
1711         }
1712
1713         snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
1714                  socket_wrapper_dir(), type, iface, prt);
1715         SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
1716         return 0;
1717 }
1718
1719 static struct socket_info_fd *find_socket_info_fd(int fd)
1720 {
1721         struct socket_info_fd *f;
1722
1723         for (f = socket_fds; f; f = f->next) {
1724                 if (f->fd == fd) {
1725                         return f;
1726                 }
1727         }
1728
1729         return NULL;
1730 }
1731
1732 static int find_socket_info_index(int fd)
1733 {
1734         struct socket_info_fd *fi = find_socket_info_fd(fd);
1735
1736         if (fi == NULL) {
1737                 return -1;
1738         }
1739
1740         return fi->si_index;
1741 }
1742
1743 static struct socket_info *find_socket_info(int fd)
1744 {
1745         int idx = find_socket_info_index(fd);
1746
1747         if (idx == -1) {
1748                 return NULL;
1749         }
1750
1751         return &sockets[idx];
1752 }
1753
1754 #if 0 /* FIXME */
1755 static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
1756 {
1757         struct socket_info_fd *f;
1758         const struct socket_info *last_s = NULL;
1759
1760         /* first catch invalid input */
1761         switch (sa->sa_family) {
1762         case AF_INET:
1763                 if (len < sizeof(struct sockaddr_in)) {
1764                         return false;
1765                 }
1766                 break;
1767 #if HAVE_IPV6
1768         case AF_INET6:
1769                 if (len < sizeof(struct sockaddr_in6)) {
1770                         return false;
1771                 }
1772                 break;
1773 #endif
1774         default:
1775                 return false;
1776                 break;
1777         }
1778
1779         for (f = socket_fds; f; f = f->next) {
1780                 struct socket_info *s = &sockets[f->si_index];
1781
1782                 if (s == last_s) {
1783                         continue;
1784                 }
1785                 last_s = s;
1786
1787                 if (s->myname == NULL) {
1788                         continue;
1789                 }
1790                 if (s->myname->sa_family != sa->sa_family) {
1791                         continue;
1792                 }
1793                 switch (s->myname->sa_family) {
1794                 case AF_INET: {
1795                         struct sockaddr_in *sin1, *sin2;
1796
1797                         sin1 = (struct sockaddr_in *)s->myname;
1798                         sin2 = (struct sockaddr_in *)sa;
1799
1800                         if (sin1->sin_addr.s_addr == htonl(INADDR_ANY)) {
1801                                 continue;
1802                         }
1803                         if (sin1->sin_port != sin2->sin_port) {
1804                                 continue;
1805                         }
1806                         if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
1807                                 continue;
1808                         }
1809
1810                         /* found */
1811                         return true;
1812                         break;
1813                 }
1814 #if HAVE_IPV6
1815                 case AF_INET6: {
1816                         struct sockaddr_in6 *sin1, *sin2;
1817
1818                         sin1 = (struct sockaddr_in6 *)s->myname;
1819                         sin2 = (struct sockaddr_in6 *)sa;
1820
1821                         if (sin1->sin6_port != sin2->sin6_port) {
1822                                 continue;
1823                         }
1824                         if (!IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
1825                                                 &sin2->sin6_addr))
1826                         {
1827                                 continue;
1828                         }
1829
1830                         /* found */
1831                         return true;
1832                         break;
1833                 }
1834 #endif
1835                 default:
1836                         continue;
1837                         break;
1838
1839                 }
1840         }
1841
1842         return false;
1843 }
1844 #endif
1845
1846 static void swrap_remove_stale(int fd)
1847 {
1848         struct socket_info_fd *fi = find_socket_info_fd(fd);
1849         struct socket_info *si;
1850         int si_index;
1851
1852         if (fi == NULL) {
1853                 return;
1854         }
1855
1856         si_index = fi->si_index;
1857
1858         SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd);
1859         SWRAP_DLIST_REMOVE(socket_fds, fi);
1860         free(fi);
1861
1862         si = &sockets[si_index];
1863         si->refcount--;
1864
1865         if (si->refcount > 0) {
1866                 return;
1867         }
1868
1869         if (si->un_addr.sun_path[0] != '\0') {
1870                 unlink(si->un_addr.sun_path);
1871         }
1872
1873         si->next_free = first_free;
1874         first_free = si_index;
1875 }
1876
1877 static int sockaddr_convert_to_un(struct socket_info *si,
1878                                   const struct sockaddr *in_addr,
1879                                   socklen_t in_len,
1880                                   struct sockaddr_un *out_addr,
1881                                   int alloc_sock,
1882                                   int *bcast)
1883 {
1884         struct sockaddr *out = (struct sockaddr *)(void *)out_addr;
1885
1886         (void) in_len; /* unused */
1887
1888         if (out_addr == NULL) {
1889                 return 0;
1890         }
1891
1892         out->sa_family = AF_UNIX;
1893 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1894         out->sa_len = sizeof(*out_addr);
1895 #endif
1896
1897         switch (in_addr->sa_family) {
1898         case AF_UNSPEC: {
1899                 const struct sockaddr_in *sin;
1900                 if (si->family != AF_INET) {
1901                         break;
1902                 }
1903                 if (in_len < sizeof(struct sockaddr_in)) {
1904                         break;
1905                 }
1906                 sin = (const struct sockaddr_in *)(const void *)in_addr;
1907                 if(sin->sin_addr.s_addr != htonl(INADDR_ANY)) {
1908                         break;
1909                 }
1910
1911                 /*
1912                  * Note: in the special case of AF_UNSPEC and INADDR_ANY,
1913                  * AF_UNSPEC is mapped to AF_INET and must be treated here.
1914                  */
1915
1916                 FALL_THROUGH;
1917         }
1918         case AF_INET:
1919 #ifdef HAVE_IPV6
1920         case AF_INET6:
1921 #endif
1922                 switch (si->type) {
1923                 case SOCK_STREAM:
1924                 case SOCK_DGRAM:
1925                         break;
1926                 default:
1927                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1928                         errno = ESOCKTNOSUPPORT;
1929                         return -1;
1930                 }
1931                 if (alloc_sock) {
1932                         return convert_in_un_alloc(si, in_addr, out_addr, bcast);
1933                 } else {
1934                         return convert_in_un_remote(si, in_addr, out_addr, bcast);
1935                 }
1936         default:
1937                 break;
1938         }
1939
1940         errno = EAFNOSUPPORT;
1941         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1942         return -1;
1943 }
1944
1945 static int sockaddr_convert_from_un(const struct socket_info *si,
1946                                     const struct sockaddr_un *in_addr,
1947                                     socklen_t un_addrlen,
1948                                     int family,
1949                                     struct sockaddr *out_addr,
1950                                     socklen_t *out_addrlen)
1951 {
1952         int ret;
1953
1954         if (out_addr == NULL || out_addrlen == NULL)
1955                 return 0;
1956
1957         if (un_addrlen == 0) {
1958                 *out_addrlen = 0;
1959                 return 0;
1960         }
1961
1962         switch (family) {
1963         case AF_INET:
1964 #ifdef HAVE_IPV6
1965         case AF_INET6:
1966 #endif
1967                 switch (si->type) {
1968                 case SOCK_STREAM:
1969                 case SOCK_DGRAM:
1970                         break;
1971                 default:
1972                         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
1973                         errno = ESOCKTNOSUPPORT;
1974                         return -1;
1975                 }
1976                 ret = convert_un_in(in_addr, out_addr, out_addrlen);
1977 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
1978                 out_addr->sa_len = *out_addrlen;
1979 #endif
1980                 return ret;
1981         default:
1982                 break;
1983         }
1984
1985         SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
1986         errno = EAFNOSUPPORT;
1987         return -1;
1988 }
1989
1990 enum swrap_packet_type {
1991         SWRAP_CONNECT_SEND,
1992         SWRAP_CONNECT_UNREACH,
1993         SWRAP_CONNECT_RECV,
1994         SWRAP_CONNECT_ACK,
1995         SWRAP_ACCEPT_SEND,
1996         SWRAP_ACCEPT_RECV,
1997         SWRAP_ACCEPT_ACK,
1998         SWRAP_RECVFROM,
1999         SWRAP_SENDTO,
2000         SWRAP_SENDTO_UNREACH,
2001         SWRAP_PENDING_RST,
2002         SWRAP_RECV,
2003         SWRAP_RECV_RST,
2004         SWRAP_SEND,
2005         SWRAP_SEND_RST,
2006         SWRAP_CLOSE_SEND,
2007         SWRAP_CLOSE_RECV,
2008         SWRAP_CLOSE_ACK,
2009 };
2010
2011 struct swrap_file_hdr {
2012         uint32_t        magic;
2013         uint16_t        version_major;
2014         uint16_t        version_minor;
2015         int32_t         timezone;
2016         uint32_t        sigfigs;
2017         uint32_t        frame_max_len;
2018 #define SWRAP_FRAME_LENGTH_MAX 0xFFFF
2019         uint32_t        link_type;
2020 };
2021 #define SWRAP_FILE_HDR_SIZE 24
2022
2023 struct swrap_packet_frame {
2024         uint32_t seconds;
2025         uint32_t micro_seconds;
2026         uint32_t recorded_length;
2027         uint32_t full_length;
2028 };
2029 #define SWRAP_PACKET_FRAME_SIZE 16
2030
2031 union swrap_packet_ip {
2032         struct {
2033                 uint8_t         ver_hdrlen;
2034                 uint8_t         tos;
2035                 uint16_t        packet_length;
2036                 uint16_t        identification;
2037                 uint8_t         flags;
2038                 uint8_t         fragment;
2039                 uint8_t         ttl;
2040                 uint8_t         protocol;
2041                 uint16_t        hdr_checksum;
2042                 uint32_t        src_addr;
2043                 uint32_t        dest_addr;
2044         } v4;
2045 #define SWRAP_PACKET_IP_V4_SIZE 20
2046         struct {
2047                 uint8_t         ver_prio;
2048                 uint8_t         flow_label_high;
2049                 uint16_t        flow_label_low;
2050                 uint16_t        payload_length;
2051                 uint8_t         next_header;
2052                 uint8_t         hop_limit;
2053                 uint8_t         src_addr[16];
2054                 uint8_t         dest_addr[16];
2055         } v6;
2056 #define SWRAP_PACKET_IP_V6_SIZE 40
2057 };
2058 #define SWRAP_PACKET_IP_SIZE 40
2059
2060 union swrap_packet_payload {
2061         struct {
2062                 uint16_t        source_port;
2063                 uint16_t        dest_port;
2064                 uint32_t        seq_num;
2065                 uint32_t        ack_num;
2066                 uint8_t         hdr_length;
2067                 uint8_t         control;
2068                 uint16_t        window;
2069                 uint16_t        checksum;
2070                 uint16_t        urg;
2071         } tcp;
2072 #define SWRAP_PACKET_PAYLOAD_TCP_SIZE 20
2073         struct {
2074                 uint16_t        source_port;
2075                 uint16_t        dest_port;
2076                 uint16_t        length;
2077                 uint16_t        checksum;
2078         } udp;
2079 #define SWRAP_PACKET_PAYLOAD_UDP_SIZE 8
2080         struct {
2081                 uint8_t         type;
2082                 uint8_t         code;
2083                 uint16_t        checksum;
2084                 uint32_t        unused;
2085         } icmp4;
2086 #define SWRAP_PACKET_PAYLOAD_ICMP4_SIZE 8
2087         struct {
2088                 uint8_t         type;
2089                 uint8_t         code;
2090                 uint16_t        checksum;
2091                 uint32_t        unused;
2092         } icmp6;
2093 #define SWRAP_PACKET_PAYLOAD_ICMP6_SIZE 8
2094 };
2095 #define SWRAP_PACKET_PAYLOAD_SIZE 20
2096
2097 #define SWRAP_PACKET_MIN_ALLOC \
2098         (SWRAP_PACKET_FRAME_SIZE + \
2099          SWRAP_PACKET_IP_SIZE + \
2100          SWRAP_PACKET_PAYLOAD_SIZE)
2101
2102 static const char *swrap_pcap_init_file(void)
2103 {
2104         static int initialized = 0;
2105         static const char *s = NULL;
2106         static const struct swrap_file_hdr h;
2107         static const struct swrap_packet_frame f;
2108         static const union swrap_packet_ip i;
2109         static const union swrap_packet_payload p;
2110
2111         if (initialized == 1) {
2112                 return s;
2113         }
2114         initialized = 1;
2115
2116         /*
2117          * TODO: don't use the structs use plain buffer offsets
2118          *       and PUSH_U8(), PUSH_U16() and PUSH_U32()
2119          *
2120          * for now make sure we disable PCAP support
2121          * if the struct has alignment!
2122          */
2123         if (sizeof(h) != SWRAP_FILE_HDR_SIZE) {
2124                 return NULL;
2125         }
2126         if (sizeof(f) != SWRAP_PACKET_FRAME_SIZE) {
2127                 return NULL;
2128         }
2129         if (sizeof(i) != SWRAP_PACKET_IP_SIZE) {
2130                 return NULL;
2131         }
2132         if (sizeof(i.v4) != SWRAP_PACKET_IP_V4_SIZE) {
2133                 return NULL;
2134         }
2135         if (sizeof(i.v6) != SWRAP_PACKET_IP_V6_SIZE) {
2136                 return NULL;
2137         }
2138         if (sizeof(p) != SWRAP_PACKET_PAYLOAD_SIZE) {
2139                 return NULL;
2140         }
2141         if (sizeof(p.tcp) != SWRAP_PACKET_PAYLOAD_TCP_SIZE) {
2142                 return NULL;
2143         }
2144         if (sizeof(p.udp) != SWRAP_PACKET_PAYLOAD_UDP_SIZE) {
2145                 return NULL;
2146         }
2147         if (sizeof(p.icmp4) != SWRAP_PACKET_PAYLOAD_ICMP4_SIZE) {
2148                 return NULL;
2149         }
2150         if (sizeof(p.icmp6) != SWRAP_PACKET_PAYLOAD_ICMP6_SIZE) {
2151                 return NULL;
2152         }
2153
2154         s = getenv("SOCKET_WRAPPER_PCAP_FILE");
2155         if (s == NULL) {
2156                 return NULL;
2157         }
2158         if (strncmp(s, "./", 2) == 0) {
2159                 s += 2;
2160         }
2161         return s;
2162 }
2163
2164 static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
2165                                        const struct sockaddr *src,
2166                                        const struct sockaddr *dest,
2167                                        int socket_type,
2168                                        const uint8_t *payload,
2169                                        size_t payload_len,
2170                                        unsigned long tcp_seqno,
2171                                        unsigned long tcp_ack,
2172                                        unsigned char tcp_ctl,
2173                                        int unreachable,
2174                                        size_t *_packet_len)
2175 {
2176         uint8_t *base;
2177         uint8_t *buf;
2178         struct swrap_packet_frame *frame;
2179         union swrap_packet_ip *ip;
2180         union swrap_packet_payload *pay;
2181         size_t packet_len;
2182         size_t alloc_len;
2183         size_t nonwire_len = sizeof(*frame);
2184         size_t wire_hdr_len = 0;
2185         size_t wire_len = 0;
2186         size_t ip_hdr_len = 0;
2187         size_t icmp_hdr_len = 0;
2188         size_t icmp_truncate_len = 0;
2189         uint8_t protocol = 0, icmp_protocol = 0;
2190         const struct sockaddr_in *src_in = NULL;
2191         const struct sockaddr_in *dest_in = NULL;
2192 #ifdef HAVE_IPV6
2193         const struct sockaddr_in6 *src_in6 = NULL;
2194         const struct sockaddr_in6 *dest_in6 = NULL;
2195 #endif
2196         uint16_t src_port;
2197         uint16_t dest_port;
2198
2199         switch (src->sa_family) {
2200         case AF_INET:
2201                 src_in = (const struct sockaddr_in *)(const void *)src;
2202                 dest_in = (const struct sockaddr_in *)(const void *)dest;
2203                 src_port = src_in->sin_port;
2204                 dest_port = dest_in->sin_port;
2205                 ip_hdr_len = sizeof(ip->v4);
2206                 break;
2207 #ifdef HAVE_IPV6
2208         case AF_INET6:
2209                 src_in6 = (const struct sockaddr_in6 *)(const void *)src;
2210                 dest_in6 = (const struct sockaddr_in6 *)(const void *)dest;
2211                 src_port = src_in6->sin6_port;
2212                 dest_port = dest_in6->sin6_port;
2213                 ip_hdr_len = sizeof(ip->v6);
2214                 break;
2215 #endif
2216         default:
2217                 return NULL;
2218         }
2219
2220         switch (socket_type) {
2221         case SOCK_STREAM:
2222                 protocol = 0x06; /* TCP */
2223                 wire_hdr_len = ip_hdr_len + sizeof(pay->tcp);
2224                 wire_len = wire_hdr_len + payload_len;
2225                 break;
2226
2227         case SOCK_DGRAM:
2228                 protocol = 0x11; /* UDP */
2229                 wire_hdr_len = ip_hdr_len + sizeof(pay->udp);
2230                 wire_len = wire_hdr_len + payload_len;
2231                 break;
2232
2233         default:
2234                 return NULL;
2235         }
2236
2237         if (unreachable) {
2238                 icmp_protocol = protocol;
2239                 switch (src->sa_family) {
2240                 case AF_INET:
2241                         protocol = 0x01; /* ICMPv4 */
2242                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp4);
2243                         break;
2244 #ifdef HAVE_IPV6
2245                 case AF_INET6:
2246                         protocol = 0x3A; /* ICMPv6 */
2247                         icmp_hdr_len = ip_hdr_len + sizeof(pay->icmp6);
2248                         break;
2249 #endif
2250                 }
2251                 if (wire_len > 64 ) {
2252                         icmp_truncate_len = wire_len - 64;
2253                 }
2254                 wire_hdr_len += icmp_hdr_len;
2255                 wire_len += icmp_hdr_len;
2256         }
2257
2258         packet_len = nonwire_len + wire_len;
2259         alloc_len = packet_len;
2260         if (alloc_len < SWRAP_PACKET_MIN_ALLOC) {
2261                 alloc_len = SWRAP_PACKET_MIN_ALLOC;
2262         }
2263
2264         base = (uint8_t *)calloc(1, alloc_len);
2265         if (base == NULL) {
2266                 return NULL;
2267         }
2268
2269         buf = base;
2270
2271         frame = (struct swrap_packet_frame *)(void *)buf;
2272         frame->seconds          = tval->tv_sec;
2273         frame->micro_seconds    = tval->tv_usec;
2274         frame->recorded_length  = wire_len - icmp_truncate_len;
2275         frame->full_length      = wire_len - icmp_truncate_len;
2276         buf += SWRAP_PACKET_FRAME_SIZE;
2277
2278         ip = (union swrap_packet_ip *)(void *)buf;
2279         switch (src->sa_family) {
2280         case AF_INET:
2281                 ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
2282                 ip->v4.tos              = 0x00;
2283                 ip->v4.packet_length    = htons(wire_len - icmp_truncate_len);
2284                 ip->v4.identification   = htons(0xFFFF);
2285                 ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
2286                 ip->v4.fragment         = htons(0x0000);
2287                 ip->v4.ttl              = 0xFF;
2288                 ip->v4.protocol         = protocol;
2289                 ip->v4.hdr_checksum     = htons(0x0000);
2290                 ip->v4.src_addr         = src_in->sin_addr.s_addr;
2291                 ip->v4.dest_addr        = dest_in->sin_addr.s_addr;
2292                 buf += SWRAP_PACKET_IP_V4_SIZE;
2293                 break;
2294 #ifdef HAVE_IPV6
2295         case AF_INET6:
2296                 ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
2297                 ip->v6.flow_label_high  = 0x00;
2298                 ip->v6.flow_label_low   = 0x0000;
2299                 ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
2300                 ip->v6.next_header      = protocol;
2301                 memcpy(ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
2302                 memcpy(ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
2303                 buf += SWRAP_PACKET_IP_V6_SIZE;
2304                 break;
2305 #endif
2306         }
2307
2308         if (unreachable) {
2309                 pay = (union swrap_packet_payload *)(void *)buf;
2310                 switch (src->sa_family) {
2311                 case AF_INET:
2312                         pay->icmp4.type         = 0x03; /* destination unreachable */
2313                         pay->icmp4.code         = 0x01; /* host unreachable */
2314                         pay->icmp4.checksum     = htons(0x0000);
2315                         pay->icmp4.unused       = htonl(0x00000000);
2316                         buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
2317
2318                         /* set the ip header in the ICMP payload */
2319                         ip = (union swrap_packet_ip *)(void *)buf;
2320                         ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
2321                         ip->v4.tos              = 0x00;
2322                         ip->v4.packet_length    = htons(wire_len - icmp_hdr_len);
2323                         ip->v4.identification   = htons(0xFFFF);
2324                         ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
2325                         ip->v4.fragment         = htons(0x0000);
2326                         ip->v4.ttl              = 0xFF;
2327                         ip->v4.protocol         = icmp_protocol;
2328                         ip->v4.hdr_checksum     = htons(0x0000);
2329                         ip->v4.src_addr         = dest_in->sin_addr.s_addr;
2330                         ip->v4.dest_addr        = src_in->sin_addr.s_addr;
2331                         buf += SWRAP_PACKET_IP_V4_SIZE;
2332
2333                         src_port = dest_in->sin_port;
2334                         dest_port = src_in->sin_port;
2335                         break;
2336 #ifdef HAVE_IPV6
2337                 case AF_INET6:
2338                         pay->icmp6.type         = 0x01; /* destination unreachable */
2339                         pay->icmp6.code         = 0x03; /* address unreachable */
2340                         pay->icmp6.checksum     = htons(0x0000);
2341                         pay->icmp6.unused       = htonl(0x00000000);
2342                         buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
2343
2344                         /* set the ip header in the ICMP payload */
2345                         ip = (union swrap_packet_ip *)(void *)buf;
2346                         ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
2347                         ip->v6.flow_label_high  = 0x00;
2348                         ip->v6.flow_label_low   = 0x0000;
2349                         ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
2350                         ip->v6.next_header      = protocol;
2351                         memcpy(ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
2352                         memcpy(ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
2353                         buf += SWRAP_PACKET_IP_V6_SIZE;
2354
2355                         src_port = dest_in6->sin6_port;
2356                         dest_port = src_in6->sin6_port;
2357                         break;
2358 #endif
2359                 }
2360         }
2361
2362         pay = (union swrap_packet_payload *)(void *)buf;
2363
2364         switch (socket_type) {
2365         case SOCK_STREAM:
2366                 pay->tcp.source_port    = src_port;
2367                 pay->tcp.dest_port      = dest_port;
2368                 pay->tcp.seq_num        = htonl(tcp_seqno);
2369                 pay->tcp.ack_num        = htonl(tcp_ack);
2370                 pay->tcp.hdr_length     = 0x50; /* 5 * 32 bit words */
2371                 pay->tcp.control        = tcp_ctl;
2372                 pay->tcp.window         = htons(0x7FFF);
2373                 pay->tcp.checksum       = htons(0x0000);
2374                 pay->tcp.urg            = htons(0x0000);
2375                 buf += SWRAP_PACKET_PAYLOAD_TCP_SIZE;
2376
2377                 break;
2378
2379         case SOCK_DGRAM:
2380                 pay->udp.source_port    = src_port;
2381                 pay->udp.dest_port      = dest_port;
2382                 pay->udp.length         = htons(8 + payload_len);
2383                 pay->udp.checksum       = htons(0x0000);
2384                 buf += SWRAP_PACKET_PAYLOAD_UDP_SIZE;
2385
2386                 break;
2387         }
2388
2389         if (payload && payload_len > 0) {
2390                 memcpy(buf, payload, payload_len);
2391         }
2392
2393         *_packet_len = packet_len - icmp_truncate_len;
2394         return base;
2395 }
2396
2397 static int swrap_pcap_get_fd(const char *fname)
2398 {
2399         static int fd = -1;
2400
2401         if (fd != -1) {
2402                 return fd;
2403         }
2404
2405         fd = libc_open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644);
2406         if (fd != -1) {
2407                 struct swrap_file_hdr file_hdr;
2408                 file_hdr.magic          = 0xA1B2C3D4;
2409                 file_hdr.version_major  = 0x0002;
2410                 file_hdr.version_minor  = 0x0004;
2411                 file_hdr.timezone       = 0x00000000;
2412                 file_hdr.sigfigs        = 0x00000000;
2413                 file_hdr.frame_max_len  = SWRAP_FRAME_LENGTH_MAX;
2414                 file_hdr.link_type      = 0x0065; /* 101 RAW IP */
2415
2416                 if (write(fd, &file_hdr, sizeof(file_hdr)) != sizeof(file_hdr)) {
2417                         close(fd);
2418                         fd = -1;
2419                 }
2420                 return fd;
2421         }
2422
2423         fd = libc_open(fname, O_WRONLY|O_APPEND, 0644);
2424
2425         return fd;
2426 }
2427
2428 static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
2429                                            const struct sockaddr *addr,
2430                                            enum swrap_packet_type type,
2431                                            const void *buf, size_t len,
2432                                            size_t *packet_len)
2433 {
2434         const struct sockaddr *src_addr;
2435         const struct sockaddr *dest_addr;
2436         unsigned long tcp_seqno = 0;
2437         unsigned long tcp_ack = 0;
2438         unsigned char tcp_ctl = 0;
2439         int unreachable = 0;
2440
2441         struct timeval tv;
2442
2443         switch (si->family) {
2444         case AF_INET:
2445                 break;
2446 #ifdef HAVE_IPV6
2447         case AF_INET6:
2448                 break;
2449 #endif
2450         default:
2451                 return NULL;
2452         }
2453
2454         switch (type) {
2455         case SWRAP_CONNECT_SEND:
2456                 if (si->type != SOCK_STREAM) {
2457                         return NULL;
2458                 }
2459
2460                 src_addr  = &si->myname.sa.s;
2461                 dest_addr = addr;
2462
2463                 tcp_seqno = si->io.pck_snd;
2464                 tcp_ack = si->io.pck_rcv;
2465                 tcp_ctl = 0x02; /* SYN */
2466
2467                 si->io.pck_snd += 1;
2468
2469                 break;
2470
2471         case SWRAP_CONNECT_RECV:
2472                 if (si->type != SOCK_STREAM) {
2473                         return NULL;
2474                 }
2475
2476                 dest_addr = &si->myname.sa.s;
2477                 src_addr = addr;
2478
2479                 tcp_seqno = si->io.pck_rcv;
2480                 tcp_ack = si->io.pck_snd;
2481                 tcp_ctl = 0x12; /** SYN,ACK */
2482
2483                 si->io.pck_rcv += 1;
2484
2485                 break;
2486
2487         case SWRAP_CONNECT_UNREACH:
2488                 if (si->type != SOCK_STREAM) {
2489                         return NULL;
2490                 }
2491
2492                 dest_addr = &si->myname.sa.s;
2493                 src_addr  = addr;
2494
2495                 /* Unreachable: resend the data of SWRAP_CONNECT_SEND */
2496                 tcp_seqno = si->io.pck_snd - 1;
2497                 tcp_ack = si->io.pck_rcv;
2498                 tcp_ctl = 0x02; /* SYN */
2499                 unreachable = 1;
2500
2501                 break;
2502
2503         case SWRAP_CONNECT_ACK:
2504                 if (si->type != SOCK_STREAM) {
2505                         return NULL;
2506                 }
2507
2508                 src_addr  = &si->myname.sa.s;
2509                 dest_addr = addr;
2510
2511                 tcp_seqno = si->io.pck_snd;
2512                 tcp_ack = si->io.pck_rcv;
2513                 tcp_ctl = 0x10; /* ACK */
2514
2515                 break;
2516
2517         case SWRAP_ACCEPT_SEND:
2518                 if (si->type != SOCK_STREAM) {
2519                         return NULL;
2520                 }
2521
2522                 dest_addr = &si->myname.sa.s;
2523                 src_addr = addr;
2524
2525                 tcp_seqno = si->io.pck_rcv;
2526                 tcp_ack = si->io.pck_snd;
2527                 tcp_ctl = 0x02; /* SYN */
2528
2529                 si->io.pck_rcv += 1;
2530
2531                 break;
2532
2533         case SWRAP_ACCEPT_RECV:
2534                 if (si->type != SOCK_STREAM) {
2535                         return NULL;
2536                 }
2537
2538                 src_addr = &si->myname.sa.s;
2539                 dest_addr = addr;
2540
2541                 tcp_seqno = si->io.pck_snd;
2542                 tcp_ack = si->io.pck_rcv;
2543                 tcp_ctl = 0x12; /* SYN,ACK */
2544
2545                 si->io.pck_snd += 1;
2546
2547                 break;
2548
2549         case SWRAP_ACCEPT_ACK:
2550                 if (si->type != SOCK_STREAM) {
2551                         return NULL;
2552                 }
2553
2554                 dest_addr = &si->myname.sa.s;
2555                 src_addr = addr;
2556
2557                 tcp_seqno = si->io.pck_rcv;
2558                 tcp_ack = si->io.pck_snd;
2559                 tcp_ctl = 0x10; /* ACK */
2560
2561                 break;
2562
2563         case SWRAP_SEND:
2564                 src_addr  = &si->myname.sa.s;
2565                 dest_addr = &si->peername.sa.s;
2566
2567                 tcp_seqno = si->io.pck_snd;
2568                 tcp_ack = si->io.pck_rcv;
2569                 tcp_ctl = 0x18; /* PSH,ACK */
2570
2571                 si->io.pck_snd += len;
2572
2573                 break;
2574
2575         case SWRAP_SEND_RST:
2576                 dest_addr = &si->myname.sa.s;
2577                 src_addr  = &si->peername.sa.s;
2578
2579                 if (si->type == SOCK_DGRAM) {
2580                         return swrap_pcap_marshall_packet(si,
2581                                                           &si->peername.sa.s,
2582                                                           SWRAP_SENDTO_UNREACH,
2583                                                           buf,
2584                                                           len,
2585                                                           packet_len);
2586                 }
2587
2588                 tcp_seqno = si->io.pck_rcv;
2589                 tcp_ack = si->io.pck_snd;
2590                 tcp_ctl = 0x14; /** RST,ACK */
2591
2592                 break;
2593
2594         case SWRAP_PENDING_RST:
2595                 dest_addr = &si->myname.sa.s;
2596                 src_addr  = &si->peername.sa.s;
2597
2598                 if (si->type == SOCK_DGRAM) {
2599                         return NULL;
2600                 }
2601
2602                 tcp_seqno = si->io.pck_rcv;
2603                 tcp_ack = si->io.pck_snd;
2604                 tcp_ctl = 0x14; /* RST,ACK */
2605
2606                 break;
2607
2608         case SWRAP_RECV:
2609                 dest_addr = &si->myname.sa.s;
2610                 src_addr  = &si->peername.sa.s;
2611
2612                 tcp_seqno = si->io.pck_rcv;
2613                 tcp_ack = si->io.pck_snd;
2614                 tcp_ctl = 0x18; /* PSH,ACK */
2615
2616                 si->io.pck_rcv += len;
2617
2618                 break;
2619
2620         case SWRAP_RECV_RST:
2621                 dest_addr = &si->myname.sa.s;
2622                 src_addr  = &si->peername.sa.s;
2623
2624                 if (si->type == SOCK_DGRAM) {
2625                         return NULL;
2626                 }
2627
2628                 tcp_seqno = si->io.pck_rcv;
2629                 tcp_ack = si->io.pck_snd;
2630                 tcp_ctl = 0x14; /* RST,ACK */
2631
2632                 break;
2633
2634         case SWRAP_SENDTO:
2635                 src_addr = &si->myname.sa.s;
2636                 dest_addr = addr;
2637
2638                 si->io.pck_snd += len;
2639
2640                 break;
2641
2642         case SWRAP_SENDTO_UNREACH:
2643                 dest_addr = &si->myname.sa.s;
2644                 src_addr = addr;
2645
2646                 unreachable = 1;
2647
2648                 break;
2649
2650         case SWRAP_RECVFROM:
2651                 dest_addr = &si->myname.sa.s;
2652                 src_addr = addr;
2653
2654                 si->io.pck_rcv += len;
2655
2656                 break;
2657
2658         case SWRAP_CLOSE_SEND:
2659                 if (si->type != SOCK_STREAM) {
2660                         return NULL;
2661                 }
2662
2663                 src_addr  = &si->myname.sa.s;
2664                 dest_addr = &si->peername.sa.s;
2665
2666                 tcp_seqno = si->io.pck_snd;
2667                 tcp_ack = si->io.pck_rcv;
2668                 tcp_ctl = 0x11; /* FIN, ACK */
2669
2670                 si->io.pck_snd += 1;
2671
2672                 break;
2673
2674         case SWRAP_CLOSE_RECV:
2675                 if (si->type != SOCK_STREAM) {
2676                         return NULL;
2677                 }
2678
2679                 dest_addr = &si->myname.sa.s;
2680                 src_addr  = &si->peername.sa.s;
2681
2682                 tcp_seqno = si->io.pck_rcv;
2683                 tcp_ack = si->io.pck_snd;
2684                 tcp_ctl = 0x11; /* FIN,ACK */
2685
2686                 si->io.pck_rcv += 1;
2687
2688                 break;
2689
2690         case SWRAP_CLOSE_ACK:
2691                 if (si->type != SOCK_STREAM) {
2692                         return NULL;
2693                 }
2694
2695                 src_addr  = &si->myname.sa.s;
2696                 dest_addr = &si->peername.sa.s;
2697
2698                 tcp_seqno = si->io.pck_snd;
2699                 tcp_ack = si->io.pck_rcv;
2700                 tcp_ctl = 0x10; /* ACK */
2701
2702                 break;
2703         default:
2704                 return NULL;
2705         }
2706
2707         swrapGetTimeOfDay(&tv);
2708
2709         return swrap_pcap_packet_init(&tv,
2710                                       src_addr,
2711                                       dest_addr,
2712                                       si->type,
2713                                       (const uint8_t *)buf,
2714                                       len,
2715                                       tcp_seqno,
2716                                       tcp_ack,
2717                                       tcp_ctl,
2718                                       unreachable,
2719                                       packet_len);
2720 }
2721
2722 static void swrap_pcap_dump_packet(struct socket_info *si,
2723                                    const struct sockaddr *addr,
2724                                    enum swrap_packet_type type,
2725                                    const void *buf, size_t len)
2726 {
2727         const char *file_name;
2728         uint8_t *packet;
2729         size_t packet_len = 0;
2730         int fd;
2731
2732         file_name = swrap_pcap_init_file();
2733         if (!file_name) {
2734                 return;
2735         }
2736
2737         packet = swrap_pcap_marshall_packet(si,
2738                                             addr,
2739                                             type,
2740                                             buf,
2741                                             len,
2742                                             &packet_len);
2743         if (packet == NULL) {
2744                 return;
2745         }
2746
2747         fd = swrap_pcap_get_fd(file_name);
2748         if (fd != -1) {
2749                 if (write(fd, packet, packet_len) != (ssize_t)packet_len) {
2750                         free(packet);
2751                         return;
2752                 }
2753         }
2754
2755         free(packet);
2756 }
2757
2758 /****************************************************************************
2759  *   SIGNALFD
2760  ***************************************************************************/
2761
2762 #ifdef HAVE_SIGNALFD
2763 static int swrap_signalfd(int fd, const sigset_t *mask, int flags)
2764 {
2765         int rc;
2766
2767         rc = libc_signalfd(fd, mask, flags);
2768         if (rc != -1) {
2769                 swrap_remove_stale(fd);
2770         }
2771
2772         return rc;
2773 }
2774
2775 int signalfd(int fd, const sigset_t *mask, int flags)
2776 {
2777         return swrap_signalfd(fd, mask, flags);
2778 }
2779 #endif
2780
2781 /****************************************************************************
2782  *   SOCKET
2783  ***************************************************************************/
2784
2785 static int swrap_socket(int family, int type, int protocol)
2786 {
2787         struct socket_info *si;
2788         struct socket_info_fd *fi;
2789         int fd;
2790         int idx;
2791         int real_type = type;
2792
2793         /*
2794          * Remove possible addition flags passed to socket() so
2795          * do not fail checking the type.
2796          * See https://lwn.net/Articles/281965/
2797          */
2798 #ifdef SOCK_CLOEXEC
2799         real_type &= ~SOCK_CLOEXEC;
2800 #endif
2801 #ifdef SOCK_NONBLOCK
2802         real_type &= ~SOCK_NONBLOCK;
2803 #endif
2804
2805         if (!socket_wrapper_enabled()) {
2806                 return libc_socket(family, type, protocol);
2807         }
2808
2809         switch (family) {
2810         case AF_INET:
2811 #ifdef HAVE_IPV6
2812         case AF_INET6:
2813 #endif
2814                 break;
2815 #ifdef AF_NETLINK
2816         case AF_NETLINK:
2817 #endif /* AF_NETLINK */
2818 #ifdef AF_PACKET
2819         case AF_PACKET:
2820 #endif /* AF_PACKET */
2821         case AF_UNIX:
2822                 return libc_socket(family, type, protocol);
2823         default:
2824                 errno = EAFNOSUPPORT;
2825                 return -1;
2826         }
2827
2828         switch (real_type) {
2829         case SOCK_STREAM:
2830                 break;
2831         case SOCK_DGRAM:
2832                 break;
2833         default:
2834                 errno = EPROTONOSUPPORT;
2835                 return -1;
2836         }
2837
2838         switch (protocol) {
2839         case 0:
2840                 break;
2841         case 6:
2842                 if (real_type == SOCK_STREAM) {
2843                         break;
2844                 }
2845                 FALL_THROUGH;
2846         case 17:
2847                 if (real_type == SOCK_DGRAM) {
2848                         break;
2849                 }
2850                 FALL_THROUGH;
2851         default:
2852                 errno = EPROTONOSUPPORT;
2853                 return -1;
2854         }
2855
2856         /*
2857          * We must call libc_socket with type, from the caller, not the version
2858          * we removed SOCK_CLOEXEC and SOCK_NONBLOCK from
2859          */
2860         fd = libc_socket(AF_UNIX, type, 0);
2861
2862         if (fd == -1) {
2863                 return -1;
2864         }
2865
2866         /* Check if we have a stale fd and remove it */
2867         swrap_remove_stale(fd);
2868
2869         idx = socket_wrapper_first_free_index();
2870         if (idx == -1) {
2871                 errno = ENOMEM;
2872                 return -1;
2873         }
2874
2875         si = &sockets[idx];
2876
2877         si->family = family;
2878
2879         /* however, the rest of the socket_wrapper code expects just
2880          * the type, not the flags */
2881         si->type = real_type;
2882         si->protocol = protocol;
2883
2884         /*
2885          * Setup myname so getsockname() can succeed to find out the socket
2886          * type.
2887          */
2888         switch(si->family) {
2889         case AF_INET: {
2890                 struct sockaddr_in sin = {
2891                         .sin_family = AF_INET,
2892                 };
2893
2894                 si->myname.sa_socklen = sizeof(struct sockaddr_in);
2895                 memcpy(&si->myname.sa.in, &sin, si->myname.sa_socklen);
2896                 break;
2897         }
2898         case AF_INET6: {
2899                 struct sockaddr_in6 sin6 = {
2900                         .sin6_family = AF_INET6,
2901                 };
2902
2903                 si->myname.sa_socklen = sizeof(struct sockaddr_in6);
2904                 memcpy(&si->myname.sa.in6, &sin6, si->myname.sa_socklen);
2905                 break;
2906         }
2907         default:
2908                 errno = EINVAL;
2909                 return -1;
2910         }
2911
2912         fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
2913         if (fi == NULL) {
2914                 errno = ENOMEM;
2915                 return -1;
2916         }
2917
2918         si->refcount = 1;
2919         first_free = si->next_free;
2920         si->next_free = 0;
2921
2922         fi->fd = fd;
2923         fi->si_index = idx;
2924
2925         SWRAP_DLIST_ADD(socket_fds, fi);
2926
2927         SWRAP_LOG(SWRAP_LOG_TRACE,
2928                   "Created %s socket for protocol %s",
2929                   si->family == AF_INET ? "IPv4" : "IPv6",
2930                   si->type == SOCK_DGRAM ? "UDP" : "TCP");
2931
2932         return fd;
2933 }
2934
2935 int socket(int family, int type, int protocol)
2936 {
2937         return swrap_socket(family, type, protocol);
2938 }
2939
2940 /****************************************************************************
2941  *   SOCKETPAIR
2942  ***************************************************************************/
2943
2944 static int swrap_socketpair(int family, int type, int protocol, int sv[2])
2945 {
2946         int rc;
2947
2948         rc = libc_socketpair(family, type, protocol, sv);
2949         if (rc != -1) {
2950                 swrap_remove_stale(sv[0]);
2951                 swrap_remove_stale(sv[1]);
2952         }
2953
2954         return rc;
2955 }
2956
2957 int socketpair(int family, int type, int protocol, int sv[2])
2958 {
2959         return swrap_socketpair(family, type, protocol, sv);
2960 }
2961
2962 /****************************************************************************
2963  *   SOCKETPAIR
2964  ***************************************************************************/
2965
2966 #ifdef HAVE_TIMERFD_CREATE
2967 static int swrap_timerfd_create(int clockid, int flags)
2968 {
2969         int fd;
2970
2971         fd = libc_timerfd_create(clockid, flags);
2972         if (fd != -1) {
2973                 swrap_remove_stale(fd);
2974         }
2975
2976         return fd;
2977 }
2978
2979 int timerfd_create(int clockid, int flags)
2980 {
2981         return swrap_timerfd_create(clockid, flags);
2982 }
2983 #endif
2984
2985 /****************************************************************************
2986  *   PIPE
2987  ***************************************************************************/
2988
2989 static int swrap_pipe(int pipefd[2])
2990 {
2991         int rc;
2992
2993         rc = libc_pipe(pipefd);
2994         if (rc != -1) {
2995                 swrap_remove_stale(pipefd[0]);
2996                 swrap_remove_stale(pipefd[1]);
2997         }
2998
2999         return rc;
3000 }
3001
3002 int pipe(int pipefd[2])
3003 {
3004         return swrap_pipe(pipefd);
3005 }
3006
3007 /****************************************************************************
3008  *   ACCEPT
3009  ***************************************************************************/
3010
3011 static int swrap_accept(int s,
3012                         struct sockaddr *addr,
3013                         socklen_t *addrlen,
3014                         int flags)
3015 {
3016         struct socket_info *parent_si, *child_si;
3017         struct socket_info_fd *child_fi;
3018         int fd;
3019         int idx;
3020         struct swrap_address un_addr = {
3021                 .sa_socklen = sizeof(struct sockaddr_un),
3022         };
3023         struct swrap_address un_my_addr = {
3024                 .sa_socklen = sizeof(struct sockaddr_un),
3025         };
3026         struct swrap_address in_addr = {
3027                 .sa_socklen = sizeof(struct sockaddr_storage),
3028         };
3029         struct swrap_address in_my_addr = {
3030                 .sa_socklen = sizeof(struct sockaddr_storage),
3031         };
3032         int ret;
3033
3034         parent_si = find_socket_info(s);
3035         if (!parent_si) {
3036 #ifdef HAVE_ACCEPT4
3037                 return libc_accept4(s, addr, addrlen, flags);
3038 #else
3039                 UNUSED(flags);
3040                 return libc_accept(s, addr, addrlen);
3041 #endif
3042         }
3043
3044         /*
3045          * assume out sockaddr have the same size as the in parent
3046          * socket family
3047          */
3048         in_addr.sa_socklen = socket_length(parent_si->family);
3049         if (in_addr.sa_socklen <= 0) {
3050                 errno = EINVAL;
3051                 return -1;
3052         }
3053
3054 #ifdef HAVE_ACCEPT4
3055         ret = libc_accept4(s, &un_addr.sa.s, &un_addr.sa_socklen, flags);
3056 #else
3057         UNUSED(flags);
3058         ret = libc_accept(s, &un_addr.sa.s, &un_addr.sa_socklen);
3059 #endif
3060         if (ret == -1) {
3061                 if (errno == ENOTSOCK) {
3062                         /* Remove stale fds */
3063                         swrap_remove_stale(s);
3064                 }
3065                 return ret;
3066         }
3067
3068         fd = ret;
3069
3070         ret = sockaddr_convert_from_un(parent_si,
3071                                        &un_addr.sa.un,
3072                                        un_addr.sa_socklen,
3073                                        parent_si->family,
3074                                        &in_addr.sa.s,
3075                                        &in_addr.sa_socklen);
3076         if (ret == -1) {
3077                 close(fd);
3078                 return ret;
3079         }
3080
3081         idx = socket_wrapper_first_free_index();
3082         if (idx == -1) {
3083                 errno = ENOMEM;
3084                 return -1;
3085         }
3086
3087         child_si = &sockets[idx];
3088
3089         child_fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
3090         if (child_fi == NULL) {
3091                 close(fd);
3092                 errno = ENOMEM;
3093                 return -1;
3094         }
3095
3096         child_fi->fd = fd;
3097
3098         child_si->family = parent_si->family;
3099         child_si->type = parent_si->type;
3100         child_si->protocol = parent_si->protocol;
3101         child_si->bound = 1;
3102         child_si->is_server = 1;
3103         child_si->connected = 1;
3104
3105         child_si->peername = (struct swrap_address) {
3106                 .sa_socklen = in_addr.sa_socklen,
3107         };
3108         memcpy(&child_si->peername.sa.ss, &in_addr.sa.ss, in_addr.sa_socklen);
3109
3110         if (addr != NULL && addrlen != NULL) {
3111                 size_t copy_len = MIN(*addrlen, in_addr.sa_socklen);
3112                 if (copy_len > 0) {
3113                         memcpy(addr, &in_addr.sa.ss, copy_len);
3114                 }
3115                 *addrlen = in_addr.sa_socklen;
3116         }
3117
3118         ret = libc_getsockname(fd,
3119                                &un_my_addr.sa.s,
3120                                &un_my_addr.sa_socklen);
3121         if (ret == -1) {
3122                 free(child_fi);
3123                 close(fd);
3124                 return ret;
3125         }
3126
3127         ret = sockaddr_convert_from_un(child_si,
3128                                        &un_my_addr.sa.un,
3129                                        un_my_addr.sa_socklen,
3130                                        child_si->family,
3131                                        &in_my_addr.sa.s,
3132                                        &in_my_addr.sa_socklen);
3133         if (ret == -1) {
3134                 free(child_fi);
3135                 close(fd);
3136                 return ret;
3137         }
3138
3139         SWRAP_LOG(SWRAP_LOG_TRACE,
3140                   "accept() path=%s, fd=%d",
3141                   un_my_addr.sa.un.sun_path, s);
3142
3143         child_si->myname = (struct swrap_address) {
3144                 .sa_socklen = in_my_addr.sa_socklen,
3145         };
3146         memcpy(&child_si->myname.sa.ss, &in_my_addr.sa.ss, in_my_addr.sa_socklen);
3147
3148         child_si->refcount = 1;
3149         first_free = child_si->next_free;
3150         child_si->next_free = 0;
3151
3152         child_fi->si_index = idx;
3153
3154         SWRAP_DLIST_ADD(socket_fds, child_fi);
3155
3156         if (addr != NULL) {
3157                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_SEND, NULL, 0);
3158                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_RECV, NULL, 0);
3159                 swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_ACK, NULL, 0);
3160         }
3161
3162         return fd;
3163 }
3164
3165 #ifdef HAVE_ACCEPT4
3166 int accept4(int s, struct sockaddr *addr, socklen_t *addrlen, int flags)
3167 {
3168         return swrap_accept(s, addr, (socklen_t *)addrlen, flags);
3169 }
3170 #endif
3171
3172 #ifdef HAVE_ACCEPT_PSOCKLEN_T
3173 int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)
3174 #else
3175 int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
3176 #endif
3177 {
3178         return swrap_accept(s, addr, (socklen_t *)addrlen, 0);
3179 }
3180
3181 static int autobind_start_init;
3182 static int autobind_start;
3183
3184 /* using sendto() or connect() on an unbound socket would give the
3185    recipient no way to reply, as unlike UDP and TCP, a unix domain
3186    socket can't auto-assign ephemeral port numbers, so we need to
3187    assign it here.
3188    Note: this might change the family from ipv6 to ipv4
3189 */
3190 static int swrap_auto_bind(int fd, struct socket_info *si, int family)
3191 {
3192         struct swrap_address un_addr = {
3193                 .sa_socklen = sizeof(struct sockaddr_un),
3194         };
3195         int i;
3196         char type;
3197         int ret;
3198         int port;
3199         struct stat st;
3200
3201         if (autobind_start_init != 1) {
3202                 autobind_start_init = 1;
3203                 autobind_start = getpid();
3204                 autobind_start %= 50000;
3205                 autobind_start += 10000;
3206         }
3207
3208         un_addr.sa.un.sun_family = AF_UNIX;
3209
3210         switch (family) {
3211         case AF_INET: {
3212                 struct sockaddr_in in;
3213
3214                 switch (si->type) {
3215                 case SOCK_STREAM:
3216                         type = SOCKET_TYPE_CHAR_TCP;
3217                         break;
3218                 case SOCK_DGRAM:
3219                         type = SOCKET_TYPE_CHAR_UDP;
3220                         break;
3221                 default:
3222                         errno = ESOCKTNOSUPPORT;
3223                         ret = -1;
3224                         goto done;
3225                 }
3226
3227                 memset(&in, 0, sizeof(in));
3228                 in.sin_family = AF_INET;
3229                 in.sin_addr.s_addr = htonl(127<<24 |
3230                                            socket_wrapper_default_iface());
3231
3232                 si->myname = (struct swrap_address) {
3233                         .sa_socklen = sizeof(in),
3234                 };
3235                 memcpy(&si->myname.sa.in, &in, si->myname.sa_socklen);
3236                 break;
3237         }
3238 #ifdef HAVE_IPV6
3239         case AF_INET6: {
3240                 struct sockaddr_in6 in6;
3241
3242                 if (si->family != family) {
3243                         errno = ENETUNREACH;
3244                         ret = -1;
3245                         goto done;
3246                 }
3247
3248                 switch (si->type) {
3249                 case SOCK_STREAM:
3250                         type = SOCKET_TYPE_CHAR_TCP_V6;
3251                         break;
3252                 case SOCK_DGRAM:
3253                         type = SOCKET_TYPE_CHAR_UDP_V6;
3254                         break;
3255                 default:
3256                         errno = ESOCKTNOSUPPORT;
3257                         ret = -1;
3258                         goto done;
3259                 }
3260
3261                 memset(&in6, 0, sizeof(in6));
3262                 in6.sin6_family = AF_INET6;
3263                 in6.sin6_addr = *swrap_ipv6();
3264                 in6.sin6_addr.s6_addr[15] = socket_wrapper_default_iface();
3265
3266                 si->myname = (struct swrap_address) {
3267                         .sa_socklen = sizeof(in6),
3268                 };
3269                 memcpy(&si->myname.sa.in6, &in6, si->myname.sa_socklen);
3270                 break;
3271         }
3272 #endif
3273         default:
3274                 errno = ESOCKTNOSUPPORT;
3275                 ret = -1;
3276                 goto done;
3277         }
3278
3279         if (autobind_start > 60000) {
3280                 autobind_start = 10000;
3281         }
3282
3283         for (i = 0; i < SOCKET_MAX_SOCKETS; i++) {
3284                 port = autobind_start + i;
3285                 snprintf(un_addr.sa.un.sun_path, sizeof(un_addr.sa.un.sun_path),
3286                          "%s/"SOCKET_FORMAT, socket_wrapper_dir(),
3287                          type, socket_wrapper_default_iface(), port);
3288                 if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
3289
3290                 ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
3291                 if (ret == -1) {
3292                         goto done;
3293                 }
3294
3295                 si->un_addr = un_addr.sa.un;
3296
3297                 si->bound = 1;
3298                 autobind_start = port + 1;
3299                 break;
3300         }
3301         if (i == SOCKET_MAX_SOCKETS) {
3302                 SWRAP_LOG(SWRAP_LOG_ERROR, "Too many open unix sockets (%u) for "
3303                                            "interface "SOCKET_FORMAT,
3304                                            SOCKET_MAX_SOCKETS,
3305                                            type,