25#include <freeradius-devel/bio/fd_priv.h>
26#include <freeradius-devel/util/file.h>
27#include <freeradius-devel/util/cap.h>
28#include <freeradius-devel/util/rand.h>
33#include <netinet/tcp.h>
34#include <netinet/in.h>
48 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on,
sizeof(on)) < 0) {
58# if !defined(SOL_TCP) && defined(IPPROTO_TCP)
59# define SOL_TCP IPPROTO_TCP
69 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on,
sizeof(on)) < 0) {
93 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on,
sizeof(
int)) < 0) {
98#elif defined(SO_TIMESTAMP)
104 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on,
sizeof(
int)) < 0) {
118 if (opt > (1 << 29)) opt = (1 << 29);
120 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
134 if (opt > (1 << 29)) opt = (1 << 29);
136 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt,
sizeof(opt)) < 0) {
162 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
189#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
195 int flag = IP_PMTUDISC_DONT;
197 if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag,
sizeof(flag)) < 0) {
204#if defined(IP_DONTFRAG)
211 if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &off,
sizeof(off)) < 0) {
229 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
259 if (IN6_IS_ADDR_UNSPECIFIED(
UNCONST(
struct in6_addr *, &sock->inet.src_ipaddr.addr.v6))) {
262 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (
char *)&on,
sizeof(on)) < 0) {
290 if (fstatat(dirfd, filename, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
291 if (errno != ENOENT) {
302 if (!S_ISSOCK(buf.st_mode)) {
311 if (buf.st_uid != cfg->
uid) {
334 if (unlinkat(dirfd, filename, 0) < 0) {
351 char const *path = cfg->
path;
352 char *p, *dir = NULL;
355 perm = S_IREAD | S_IWRITE | S_IEXEC;
356 perm |= S_IRGRP | S_IWGRP | S_IXGRP;
364 if (
fr_dirfd(dirfd, filename, path) == 0) {
370 if (fstat(*dirfd, &buf) < 0) {
378 if (buf.st_uid != cfg->
uid) {
379 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect UID", path);
383 if (buf.st_gid != cfg->
gid) {
384 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect GID", path);
393 if (fchmod(*dirfd, perm) < 0) {
410 slashes[0] = slashes[1] = NULL;
411 for (p = dir; *p !=
'\0'; p++) {
413 slashes[0] = slashes[1];
446 parent_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
457 if (mkdirat(parent_fd, slashes[0] + 1, 0700) < 0) {
464 fd = openat(parent_fd, slashes[0] + 1, O_DIRECTORY);
470 if (fchmod(fd, perm) < 0) {
482 if (fchown(fd, cfg->
uid, cfg->
gid) < 0) {
488 path = strrchr(cfg->
path,
'/');
516 if (
my->user_shutdown) {
519 rcode =
my->user_shutdown(bio);
520 if (rcode < 0)
return rcode;
523 if (unlink(
my->info.socket.unix.path) < 0)
return fr_bio_error(GENERIC);
542 char const *filename, *p;
544 struct sockaddr_un sun;
555 if (cfg->
uid == (uid_t) -1) {
560 if (cfg->
gid == (gid_t) -1) {
568 p = strrchr(cfg->
path,
'/');
571 filename = cfg->
path;
573 }
else if (p == cfg->
path) {
577 fr_strerror_printf(
"Failed opening domain socket %s: cannot exist at file system root", p);
589 if (dirfd != AT_FDCWD) close(dirfd);
602 rcode = bindat(dirfd,
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
609 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
624 if (fchmod(
my->info.socket.fd, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
634 if (fchown(
my->info.socket.fd, cfg->
uid, cfg->
gid) < 0) {
644 if (
my->cb.shutdown)
my->user_shutdown =
my->cb.shutdown;
646 if (dirfd != AT_FDCWD) close(dirfd);
655#undef SO_BINDTODEVICE
658#ifdef SO_BINDTODEVICE
667 if (!
my->info.socket.inet.ifindex)
return 0;
678#ifdef HAVE_CAPABILITY_H
682 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->
interface, strlen(cfg->
interface)) < 0) {
690#elif defined(IP_BOUND_IF) || defined(IPV6_BOUND_IF)
698 if (!
my->info.socket.inet.ifindex)
return 0;
700 opt =
my->info.socket.inet.ifindex;
702 switch (
my->info.socket.af) {
704 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IP, IP_BOUND_IF, &opt,
sizeof(opt));
708 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &opt,
sizeof(opt));
713 errno = EAFNOSUPPORT;
734 if (!
my->info.socket.inet.ifindex)
return 0;
748 struct sockaddr_storage salocal;
750 fr_assert((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6));
755 if (
my->info.cfg->src_port_start) {
756 if (
my->info.cfg->src_port_start < 1024) {
761 if (
my->info.cfg->src_port_end <=
my->info.cfg->src_port_start) {
771 do_suid = (
my->info.socket.inet.src_port > 0) && (
my->info.socket.inet.src_port < 1024) && (geteuid() != 0);
773#ifdef HAVE_CAPABILITY_H
777 if (do_suid) do_suid = (fr_cap_enable(CAP_NET_BIND_SERVICE,
CAP_EFFECTIVE) < 0);
795 if (
fr_ipaddr_to_sockaddr(&salocal, &salen, &
my->info.socket.inet.src_ipaddr,
my->info.socket.inet.src_port) < 0)
goto down;
800 if (!
my->info.cfg->src_port_start) {
801 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
811 struct sockaddr_in *sin = (
struct sockaddr_in *) &salocal;
815 fr_assert(
my->info.cfg->src_port_end >=
my->info.cfg->src_port_start);
817 range =
my->info.cfg->src_port_end -
my->info.cfg->src_port_start;
825 sin->sin_port = htons(
my->info.cfg->src_port_start + src_port);
831 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
842 if (current == range) {
851 if (current == src_port)
break;
853 sin->sin_port = htons(
my->info.cfg->src_port_start + current);
855 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
885 switch (
my->info.type) {
892 switch (
my->info.socket.af) {
897 my->info.socket.inet.src_port);
907 my->info.name =
"??? invalid BIO ???";
913 switch (
my->info.socket.af) {
916 my->info.name =
fr_asprintf(
my,
"proto %s local %pV port %u remote %pV port %u",
919 my->info.socket.inet.src_port,
921 my->info.socket.inet.dst_port);
926 (cfg->
socket_type == SOCK_DGRAM) ?
"(datagram) " :
"",
933 if (cfg->
flags == O_RDONLY) {
937 }
else if (cfg->
flags == O_WRONLY) {
948 my->info.name =
"??? invalid BIO ???";
954 switch (
my->info.socket.af) {
960 my->info.socket.inet.src_port);
970 my->info.name =
"??? invalid BIO ???";
1009 fr_strerror_printf(
"Source and destination IP addresses are not from the same IP address family");
1026 fr_strerror_const(
"Unconnected sockets cannot be used with Unix sockets or files");
1066 my->info.socket.inet.src_port = cfg->
src_port;
1067 my->info.socket.inet.dst_port = cfg->
dst_port;
1075 switch (cfg->
type) {
1083 if (
my->info.socket.inet.src_ipaddr.af == AF_UNSPEC) {
1085 .af =
my->info.socket.inet.dst_ipaddr.af,
1086 .prefix = (
my->info.socket.inet.dst_ipaddr.af == AF_INET) ? 32 : 128,
1092 my->info.socket.af =
my->info.socket.inet.dst_ipaddr.af;
1098 if (
my->info.socket.inet.src_ipaddr.af !=
my->info.socket.inet.dst_ipaddr.af) {
1099 fr_strerror_const(
"Source and destination IP addresses are not from the same IP address family");
1117 fr_strerror_const(
"Cannot set 'reuse_port' for connected UDP sockets with wildcard IP");
1126 fr_assert(
my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
1131 protocol = IPPROTO_TCP;
1133 protocol = IPPROTO_UDP;
1137 my->info.socket.inet.ifindex = if_nametoindex(cfg->
interface);
1139 if (!
my->info.socket.inet.ifindex) {
1145 fd = socket(
my->info.socket.af,
my->info.socket.type, protocol);
1151 }
else if (cfg->
path) {
1152 my->info.socket.af = AF_LOCAL;
1153 my->info.socket.type = SOCK_STREAM;
1154 my->info.socket.unix.path = cfg->
path;
1156 fd = socket(
my->info.socket.af,
my->info.socket.type, 0);
1172 my->info.socket.type = SOCK_STREAM;
1178 if (strcmp(cfg->
filename,
"/dev/stdout") == 0) {
1179 if (cfg->
flags != O_WRONLY) {
1185 fd = dup(STDOUT_FILENO);
1187 }
else if (strcmp(cfg->
filename,
"/dev/stderr") == 0) {
1188 if (cfg->
flags != O_WRONLY)
goto fail_dev;
1190 fd = dup(STDERR_FILENO);
1192 }
else if (strcmp(cfg->
filename,
"/dev/stdin") == 0) {
1193 if (cfg->
flags != O_RDONLY) {
1198 fd = dup(STDIN_FILENO);
1204 my->info.socket.fd = -1;
1226 my->info.cfg = NULL;
1241 my->info.socket.fd = fd;
1246 my->info.type = cfg->
type;
1252 switch (cfg->
type) {
1260 if (
my->info.socket.type != SOCK_DGRAM) {
1261 fr_strerror_const(
"Failed configuring socket: unconnected sockets must be UDP");
1265 switch (
my->info.socket.af) {
1293 if (
my->info.socket.type == SOCK_DGRAM) {
1294 switch (
my->info.socket.af) {
1304 }
else if ((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6)) {
1306 if (rcode < 0)
goto fail;
1309 switch (
my->info.socket.af) {
1333 if ((
my->info.socket.type == SOCK_DGRAM) && !cfg->
reuse_port) {
1339 switch (
my->info.socket.af) {
1393 if (flags != O_RDONLY) flags |= O_CREAT;
1411 char *p = strrchr(cfg->
filename,
'/');
1414 if (!p)
goto do_open;
1424 fd = openat(dir_fd, p + 1, flags, cfg->
perm);
1436 if (
my->info.socket.fd < 0) {
1444 if (dup2(fd,
my->info.socket.fd) < 0) {
1451 return my->info.socket.fd;
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
int fr_bio_fd_socket_name(fr_bio_fd_t *my)
int fr_bio_fd_init_connected(fr_bio_fd_t *my)
int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
int fr_bio_fd_init_listen(fr_bio_fd_t *my)
int fr_bio_fd_init_common(fr_bio_fd_t *my)
uint16_t src_port
our port
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
@ FR_BIO_FD_INVALID
not set
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
uint32_t recv_buff
How big the kernel's receive buffer should be.
bool mkdir
make intermediate directories
@ FR_BIO_FD_STATE_CONNECTING
fr_ipaddr_t dst_ipaddr
their IP address
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
char const * path
for Unix domain sockets
uint32_t send_buff
How big the kernel's send buffer should be.
char const * filename
for files
uid_t uid
who owns the socket
gid_t gid
who owns the socket
char const * interface
for binding to an interface
mode_t perm
permissions for domain sockets
int socket_type
SOCK_STREAM or SOCK_DGRAM.
uint16_t dst_port
their port
bool tcp_delay
We do tcp_nodelay by default.
bool reuse_port
whether or not we re-use the same destination port for datagram sockets
bool exceed_mtu
Whether we allow packets which exceed the local MTU.
fr_ipaddr_t src_ipaddr
our IP address
Configuration for sockets.
Run-time status of the socket.
void fr_bio_shutdown & my
static int fr_bio_fd_socket_unix_mkdir(int *dirfd, char const **filename, fr_bio_fd_config_t const *cfg)
static int fr_bio_fd_socket_bind_to_device(fr_bio_fd_t *my, UNUSED fr_bio_fd_config_t const *cfg)
This system is missing SO_BINDTODEVICE, IP_BOUND_IF, IPV6_BOUND_IF.
static int fr_bio_fd_socket_unix_verify(int dirfd, char const *filename, fr_bio_fd_config_t const *cfg)
Verify or clean up a pre-existing domain socket.
static int fr_bio_fd_server_ipv6(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize an IPv6 server socket.
static int fr_bio_fd_common_udp(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize a UDP socket.
int fr_bio_fd_reopen(fr_bio_t *bio)
Reopen a file BIO.
static int fr_bio_fd_server_tcp(int fd, UNUSED fr_socket_t const *sock)
Initialize a TCP server socket.
static int fr_bio_fd_socket_bind_unix(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
Bind to a Unix domain socket.
void fr_bio_fd_name(fr_bio_fd_t *my)
Set the name of an FD BIO.
static int fr_bio_fd_socket_bind(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
static int fr_bio_fd_server_ipv4(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize an IPv4 server socket.
static int fr_bio_fd_common_tcp(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize common datagram information.
static int fr_bio_fd_unix_shutdown(fr_bio_t *bio)
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg)
Checks the configuration without modifying anything.
static int fr_bio_fd_common_datagram(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize common datagram information.
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
Convert our internal ip address representation to a sockaddr.
ssize_t fr_mkdir(int *fd_out, char const *path, ssize_t len, mode_t mode, fr_mkdir_func_t func, void *uctx)
Create directories that are missing in the specified path.
int fr_mkdir_chown(int fd, char const *path, void *uctx)
Callback for the common case of chown() of the directory.
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
int fr_nonblock(UNUSED int fd)
int fr_cloexec(UNUSED int fd)
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
uint32_t fr_rand(void)
Return a 32-bit random number.
int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define talloc_strdup(_ctx, _str)
int af
AF_INET, AF_INET6, or AF_UNIX.
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Holds information necessary for binding or connecting to a socket.
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
#define fr_strerror_const(_msg)
#define fr_box_ipaddr(_val)