Skip to content

Commit

Permalink
Ensure that the AF_UNIX socket pair has at least 65k of buffer space
Browse files Browse the repository at this point in the history
Without this change, pinging a lwipovpn client with something like a
3000 byte payload on macOS often fails as the default buffer sizes on
macOS are 2048 for send and 4096 for receive.

Change-Id: Ice015df81543c01094479929f0cb3075ca4f3813
Signed-off-by: Arne Schwabe <arne@rfc2549.org>
Acked-by: Gert Doering <gert@greenie.muc.de>
Message-Id: <20240925063016.22532-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg29413.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
  • Loading branch information
schwabe authored and cron2 committed Sep 25, 2024
1 parent 5c4a0b7 commit bae48c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/openvpn/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,20 +890,23 @@ socket_set_rcvbuf(socket_descriptor_t sd, int size)
#endif
}

static void
socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs)
void
socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs,
bool reduce_size)
{
if (sbs)
{
const int sndbuf_old = socket_get_sndbuf(fd);
const int rcvbuf_old = socket_get_rcvbuf(fd);

if (sbs->sndbuf)
if (sbs->sndbuf
&& (reduce_size || sndbuf_old < sbs->sndbuf))
{
socket_set_sndbuf(fd, sbs->sndbuf);
}

if (sbs->rcvbuf)
if (sbs->rcvbuf
&& (reduce_size || rcvbuf_old < sbs->rcvbuf))
{
socket_set_rcvbuf(fd, sbs->rcvbuf);
}
Expand Down Expand Up @@ -986,7 +989,7 @@ link_socket_update_buffer_sizes(struct link_socket *ls, int rcvbuf, int sndbuf)
{
ls->socket_buffer_sizes.sndbuf = sndbuf;
ls->socket_buffer_sizes.rcvbuf = rcvbuf;
socket_set_buffers(ls->sd, &ls->socket_buffer_sizes);
socket_set_buffers(ls->sd, &ls->socket_buffer_sizes, true);
}
}

Expand Down Expand Up @@ -1136,7 +1139,7 @@ create_socket(struct link_socket *sock, struct addrinfo *addr)
sock->info.af = addr->ai_family;

/* set socket buffers based on --sndbuf and --rcvbuf options */
socket_set_buffers(sock->sd, &sock->socket_buffer_sizes);
socket_set_buffers(sock->sd, &sock->socket_buffer_sizes, true);

/* set socket to --mark packets with given value */
socket_set_mark(sock->sd, sock->mark);
Expand Down
12 changes: 12 additions & 0 deletions src/openvpn/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ struct socket_buffer_size
int sndbuf;
};

/**
* Sets the receive and send buffer sizes of a socket descriptor.
*
* @param fd The socket to modify
* @param sbs new sizes.
* @param reduce_size apply the new size even if smaller than current one
*/
void
socket_set_buffers(socket_descriptor_t fd,
const struct socket_buffer_size *sbs,
bool reduce_size);

/*
* This is the main socket structure used by OpenVPN. The SOCKET_
* defines try to abstract away our implementation differences between
Expand Down
8 changes: 8 additions & 0 deletions src/openvpn/tun_afunix.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "wfp_block.h"
#include "argv.h"
#include "options.h"
#include "socket.h"

#ifndef WIN32
/* Windows does implement some AF_UNIX functionality but key features
Expand Down Expand Up @@ -80,6 +81,13 @@ open_tun_afunix(struct options *o,
return;
}


/* Ensure that the buffer sizes are decently sized. Otherwise macOS will
* just have 2048 */
struct socket_buffer_size newsizes = {65536, 65536 };
socket_set_buffers(fds[0], &newsizes, false);
socket_set_buffers(fds[1], &newsizes, false);

/* Use the first file descriptor for our side and avoid passing it
* to the child */
tt->fd = fds[1];
Expand Down

0 comments on commit bae48c1

Please sign in to comment.