1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/socket.h>
14 #include "sd-daemon.h"
16 #include "sd-resolve.h"
18 #include "alloc-util.h"
21 #include "main-func.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "pretty-print.h"
25 #include "resolve-private.h"
27 #include "socket-util.h"
28 #include "string-util.h"
31 #define BUFFER_SIZE (256 * 1024)
32 static unsigned arg_connections_max
= 256;
34 static const char *arg_remote_host
= NULL
;
36 typedef struct Context
{
44 typedef struct Connection
{
47 int server_fd
, client_fd
;
48 int server_to_client_buffer
[2]; /* a pipe */
49 int client_to_server_buffer
[2]; /* a pipe */
51 size_t server_to_client_buffer_full
, client_to_server_buffer_full
;
52 size_t server_to_client_buffer_size
, client_to_server_buffer_size
;
54 sd_event_source
*server_event_source
, *client_event_source
;
56 sd_resolve_query
*resolve_query
;
59 static void connection_free(Connection
*c
) {
63 set_remove(c
->context
->connections
, c
);
65 sd_event_source_unref(c
->server_event_source
);
66 sd_event_source_unref(c
->client_event_source
);
68 safe_close(c
->server_fd
);
69 safe_close(c
->client_fd
);
71 safe_close_pair(c
->server_to_client_buffer
);
72 safe_close_pair(c
->client_to_server_buffer
);
74 sd_resolve_query_unref(c
->resolve_query
);
79 static void context_clear(Context
*context
) {
82 set_free_with_destructor(context
->listen
, sd_event_source_unref
);
83 set_free_with_destructor(context
->connections
, connection_free
);
85 sd_event_unref(context
->event
);
86 sd_resolve_unref(context
->resolve
);
89 static int connection_create_pipes(Connection
*c
, int buffer
[static 2], size_t *sz
) {
99 r
= pipe2(buffer
, O_CLOEXEC
|O_NONBLOCK
);
101 return log_error_errno(errno
, "Failed to allocate pipe buffer: %m");
103 (void) fcntl(buffer
[0], F_SETPIPE_SZ
, BUFFER_SIZE
);
105 r
= fcntl(buffer
[0], F_GETPIPE_SZ
);
107 return log_error_errno(errno
, "Failed to get pipe buffer size: %m");
115 static int connection_shovel(
117 int *from
, int buffer
[2], int *to
,
118 size_t *full
, size_t *sz
,
119 sd_event_source
**from_source
, sd_event_source
**to_source
) {
126 assert(buffer
[0] >= 0);
127 assert(buffer
[1] >= 0);
139 if (*full
< *sz
&& *from
>= 0 && *to
>= 0) {
140 z
= splice(*from
, NULL
, buffer
[1], NULL
, *sz
- *full
, SPLICE_F_MOVE
|SPLICE_F_NONBLOCK
);
144 } else if (z
== 0 || IN_SET(errno
, EPIPE
, ECONNRESET
)) {
145 *from_source
= sd_event_source_unref(*from_source
);
146 *from
= safe_close(*from
);
147 } else if (!IN_SET(errno
, EAGAIN
, EINTR
))
148 return log_error_errno(errno
, "Failed to splice: %m");
151 if (*full
> 0 && *to
>= 0) {
152 z
= splice(buffer
[0], NULL
, *to
, NULL
, *full
, SPLICE_F_MOVE
|SPLICE_F_NONBLOCK
);
156 } else if (z
== 0 || IN_SET(errno
, EPIPE
, ECONNRESET
)) {
157 *to_source
= sd_event_source_unref(*to_source
);
158 *to
= safe_close(*to
);
159 } else if (!IN_SET(errno
, EAGAIN
, EINTR
))
160 return log_error_errno(errno
, "Failed to splice: %m");
167 static int connection_enable_event_sources(Connection
*c
);
169 static int traffic_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
170 Connection
*c
= userdata
;
177 r
= connection_shovel(c
,
178 &c
->server_fd
, c
->server_to_client_buffer
, &c
->client_fd
,
179 &c
->server_to_client_buffer_full
, &c
->server_to_client_buffer_size
,
180 &c
->server_event_source
, &c
->client_event_source
);
184 r
= connection_shovel(c
,
185 &c
->client_fd
, c
->client_to_server_buffer
, &c
->server_fd
,
186 &c
->client_to_server_buffer_full
, &c
->client_to_server_buffer_size
,
187 &c
->client_event_source
, &c
->server_event_source
);
191 /* EOF on both sides? */
192 if (c
->server_fd
== -1 && c
->client_fd
== -1)
195 /* Server closed, and all data written to client? */
196 if (c
->server_fd
== -1 && c
->server_to_client_buffer_full
<= 0)
199 /* Client closed, and all data written to server? */
200 if (c
->client_fd
== -1 && c
->client_to_server_buffer_full
<= 0)
203 r
= connection_enable_event_sources(c
);
211 return 0; /* ignore errors, continue serving */
214 static int connection_enable_event_sources(Connection
*c
) {
215 uint32_t a
= 0, b
= 0;
220 if (c
->server_to_client_buffer_full
> 0)
222 if (c
->server_to_client_buffer_full
< c
->server_to_client_buffer_size
)
225 if (c
->client_to_server_buffer_full
> 0)
227 if (c
->client_to_server_buffer_full
< c
->client_to_server_buffer_size
)
230 if (c
->server_event_source
)
231 r
= sd_event_source_set_io_events(c
->server_event_source
, a
);
232 else if (c
->server_fd
>= 0)
233 r
= sd_event_add_io(c
->context
->event
, &c
->server_event_source
, c
->server_fd
, a
, traffic_cb
, c
);
238 return log_error_errno(r
, "Failed to set up server event source: %m");
240 if (c
->client_event_source
)
241 r
= sd_event_source_set_io_events(c
->client_event_source
, b
);
242 else if (c
->client_fd
>= 0)
243 r
= sd_event_add_io(c
->context
->event
, &c
->client_event_source
, c
->client_fd
, b
, traffic_cb
, c
);
248 return log_error_errno(r
, "Failed to set up client event source: %m");
253 static int connection_complete(Connection
*c
) {
258 r
= connection_create_pipes(c
, c
->server_to_client_buffer
, &c
->server_to_client_buffer_size
);
262 r
= connection_create_pipes(c
, c
->client_to_server_buffer
, &c
->client_to_server_buffer_size
);
266 r
= connection_enable_event_sources(c
);
274 return 0; /* ignore errors, continue serving */
277 static int connect_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
278 Connection
*c
= userdata
;
286 solen
= sizeof(error
);
287 r
= getsockopt(fd
, SOL_SOCKET
, SO_ERROR
, &error
, &solen
);
289 log_error_errno(errno
, "Failed to issue SO_ERROR: %m");
294 log_error_errno(error
, "Failed to connect to remote host: %m");
298 c
->client_event_source
= sd_event_source_unref(c
->client_event_source
);
300 return connection_complete(c
);
304 return 0; /* ignore errors, continue serving */
307 static int connection_start(Connection
*c
, struct sockaddr
*sa
, socklen_t salen
) {
314 c
->client_fd
= socket(sa
->sa_family
, SOCK_STREAM
|SOCK_NONBLOCK
|SOCK_CLOEXEC
, 0);
315 if (c
->client_fd
< 0) {
316 log_error_errno(errno
, "Failed to get remote socket: %m");
320 r
= connect(c
->client_fd
, sa
, salen
);
322 if (errno
== EINPROGRESS
) {
323 r
= sd_event_add_io(c
->context
->event
, &c
->client_event_source
, c
->client_fd
, EPOLLOUT
, connect_cb
, c
);
325 log_error_errno(r
, "Failed to add connection socket: %m");
329 r
= sd_event_source_set_enabled(c
->client_event_source
, SD_EVENT_ONESHOT
);
331 log_error_errno(r
, "Failed to enable oneshot event source: %m");
335 log_error_errno(errno
, "Failed to connect to remote host: %m");
339 r
= connection_complete(c
);
348 return 0; /* ignore errors, continue serving */
351 static int resolve_handler(sd_resolve_query
*q
, int ret
, const struct addrinfo
*ai
, Connection
*c
) {
356 log_error("Failed to resolve host: %s", gai_strerror(ret
));
360 c
->resolve_query
= sd_resolve_query_unref(c
->resolve_query
);
362 return connection_start(c
, ai
->ai_addr
, ai
->ai_addrlen
);
366 return 0; /* ignore errors, continue serving */
369 static int resolve_remote(Connection
*c
) {
371 static const struct addrinfo hints
= {
372 .ai_family
= AF_UNSPEC
,
373 .ai_socktype
= SOCK_STREAM
,
374 .ai_flags
= AI_ADDRCONFIG
377 union sockaddr_union sa
= {};
378 const char *node
, *service
;
381 if (IN_SET(arg_remote_host
[0], '/', '@')) {
384 salen
= sockaddr_un_set_path(&sa
.un
, arg_remote_host
);
386 log_error_errno(salen
, "Specified address doesn't fit in an AF_UNIX address, refusing: %m");
390 return connection_start(c
, &sa
.sa
, salen
);
393 service
= strrchr(arg_remote_host
, ':');
395 node
= strndupa(arg_remote_host
, service
- arg_remote_host
);
398 node
= arg_remote_host
;
402 log_debug("Looking up address info for %s:%s", node
, service
);
403 r
= resolve_getaddrinfo(c
->context
->resolve
, &c
->resolve_query
, node
, service
, &hints
, resolve_handler
, NULL
, c
);
405 log_error_errno(r
, "Failed to resolve remote host: %m");
413 return 0; /* ignore errors, continue serving */
416 static int add_connection_socket(Context
*context
, int fd
) {
423 if (set_size(context
->connections
) > arg_connections_max
) {
424 log_warning("Hit connection limit, refusing connection.");
429 r
= set_ensure_allocated(&context
->connections
, NULL
);
435 c
= new0(Connection
, 1);
441 c
->context
= context
;
444 c
->server_to_client_buffer
[0] = c
->server_to_client_buffer
[1] = -1;
445 c
->client_to_server_buffer
[0] = c
->client_to_server_buffer
[1] = -1;
447 r
= set_put(context
->connections
, c
);
454 return resolve_remote(c
);
457 static int accept_cb(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
458 _cleanup_free_
char *peer
= NULL
;
459 Context
*context
= userdata
;
464 assert(revents
& EPOLLIN
);
467 nfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
469 if (errno
!= -EAGAIN
)
470 log_warning_errno(errno
, "Failed to accept() socket: %m");
472 getpeername_pretty(nfd
, true, &peer
);
473 log_debug("New connection from %s", strna(peer
));
475 r
= add_connection_socket(context
, nfd
);
477 log_error_errno(r
, "Failed to accept connection, ignoring: %m");
482 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
484 log_error_errno(r
, "Error while re-enabling listener with ONESHOT: %m");
485 sd_event_exit(context
->event
, r
);
492 static int add_listen_socket(Context
*context
, int fd
) {
493 sd_event_source
*source
;
499 r
= set_ensure_allocated(&context
->listen
, NULL
);
505 r
= sd_is_socket(fd
, 0, SOCK_STREAM
, 1);
507 return log_error_errno(r
, "Failed to determine socket type: %m");
509 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
510 "Passed in socket is not a stream socket.");
512 r
= fd_nonblock(fd
, true);
514 return log_error_errno(r
, "Failed to mark file descriptor non-blocking: %m");
516 r
= sd_event_add_io(context
->event
, &source
, fd
, EPOLLIN
, accept_cb
, context
);
518 return log_error_errno(r
, "Failed to add event source: %m");
520 r
= set_put(context
->listen
, source
);
522 log_error_errno(r
, "Failed to add source to set: %m");
523 sd_event_source_unref(source
);
527 /* Set the watcher to oneshot in case other processes are also
528 * watching to accept(). */
529 r
= sd_event_source_set_enabled(source
, SD_EVENT_ONESHOT
);
531 return log_error_errno(r
, "Failed to enable oneshot mode: %m");
536 static int help(void) {
537 _cleanup_free_
char *link
= NULL
;
540 r
= terminal_urlify_man("systemd-socket-proxyd", "8", &link
);
544 printf("%1$s [HOST:PORT]\n"
546 "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n"
547 " -c --connections-max= Set the maximum number of connections to be accepted\n"
548 " -h --help Show this help\n"
549 " --version Show package version\n"
550 "\nSee the %2$s for details.\n"
551 , program_invocation_short_name
558 static int parse_argv(int argc
, char *argv
[]) {
565 static const struct option options
[] = {
566 { "connections-max", required_argument
, NULL
, 'c' },
567 { "help", no_argument
, NULL
, 'h' },
568 { "version", no_argument
, NULL
, ARG_VERSION
},
577 while ((c
= getopt_long(argc
, argv
, "c:h", options
, NULL
)) >= 0)
588 r
= safe_atou(optarg
, &arg_connections_max
);
590 log_error("Failed to parse --connections-max= argument: %s", optarg
);
594 if (arg_connections_max
< 1)
595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
596 "Connection limit is too low.");
604 assert_not_reached("Unhandled option");
608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
609 "Not enough parameters.");
611 if (argc
!= optind
+1)
612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
613 "Too many parameters.");
615 arg_remote_host
= argv
[optind
];
619 static int run(int argc
, char *argv
[]) {
620 _cleanup_(context_clear
) Context context
= {};
623 log_parse_environment();
626 r
= parse_argv(argc
, argv
);
630 r
= sd_event_default(&context
.event
);
632 return log_error_errno(r
, "Failed to allocate event loop: %m");
634 r
= sd_resolve_default(&context
.resolve
);
636 return log_error_errno(r
, "Failed to allocate resolver: %m");
638 r
= sd_resolve_attach_event(context
.resolve
, context
.event
, 0);
640 return log_error_errno(r
, "Failed to attach resolver: %m");
642 sd_event_set_watchdog(context
.event
, true);
644 r
= sd_listen_fds(1);
646 return log_error_errno(r
, "Failed to receive sockets from parent.");
648 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Didn't get any sockets passed in.");
652 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
653 r
= add_listen_socket(&context
, fd
);
658 r
= sd_event_loop(context
.event
);
660 return log_error_errno(r
, "Failed to run event loop: %m");
665 DEFINE_MAIN_FUNCTION(run
);