25#include <freeradius-devel/bio/fd_priv.h>
26#include <freeradius-devel/util/file.h>
27#include <freeradius-devel/util/cap.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) {
158 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
174 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
189#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
194 flag = IP_PMTUDISC_DONT;
196 if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag,
sizeof(flag)) < 0) {
202#if defined(IP_DONTFRAG)
208 if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &flag,
sizeof(flag)) < 0) {
231 if (IN6_IS_ADDR_UNSPECIFIED(
UNCONST(
struct in6_addr *, &sock->inet.src_ipaddr.addr.v6))) {
234 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (
char *)&on,
sizeof(on)) < 0) {
262 if (fstatat(dirfd, filename, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
263 if (errno != ENOENT) {
274 if (!S_ISSOCK(buf.st_mode)) {
283 if (buf.st_uid != cfg->
uid) {
306 if (unlinkat(dirfd, filename, 0) < 0) {
323 char const *path = cfg->
path;
324 char *p, *dir = NULL;
327 perm = S_IREAD | S_IWRITE | S_IEXEC;
328 perm |= S_IRGRP | S_IWGRP | S_IXGRP;
336 if (
fr_dirfd(dirfd, filename, path) == 0) {
339 if (fstat(*dirfd, &buf) < 0) {
347 if (buf.st_uid != cfg->
uid) {
348 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect UID", path);
352 if (buf.st_gid != cfg->
gid) {
353 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect GID", path);
362 if (fchmod(*dirfd, perm) < 0) {
370 dir = talloc_strdup(NULL, path);
376 slashes[0] = slashes[1] = NULL;
377 for (p = dir; *p !=
'\0'; p++) {
379 slashes[0] = slashes[1];
412 parent_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
423 if (mkdirat(parent_fd, dir, 0700) < 0) {
430 fd = openat(parent_fd, dir, O_DIRECTORY);
436 if (fchmod(fd, perm) < 0) {
448 if (fchown(fd, cfg->
uid, cfg->
gid) < 0) {
477 if (
my->user_shutdown)
my->user_shutdown(bio);
479 (void) unlink(
my->info.socket.unix.path);
496 char const *filename, *p;
498 struct sockaddr_un sun;
509 if (cfg->
uid == (uid_t) -1) {
514 if (cfg->
gid == (gid_t) -1) {
522 p = strrchr(cfg->
path,
'/');
525 filename = cfg->
path;
527 }
else if (p == cfg->
path) {
531 fr_strerror_printf(
"Failed opening domain socket %s: cannot exist at file system root", p);
543 if (dirfd != AT_FDCWD)
close(dirfd);
556 rcode = bindat(dirfd,
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
563 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
578 if (fchmod(
my->info.socket.fd, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
588 if (fchown(
my->info.socket.fd, cfg->
uid, cfg->
gid) < 0) {
598 if (
my->cb.shutdown)
my->user_shutdown =
my->cb.shutdown;
608#undef SO_BINDTODEVICE
611#ifdef SO_BINDTODEVICE
620 if (!
my->info.socket.inet.ifindex)
return 0;
631#ifdef HAVE_CAPABILITY_H
632 (void)fr_cap_enable(CAP_NET_RAW, CAP_EFFECTIVE);
635 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->
interface, strlen(cfg->
interface)) < 0) {
643#elif defined(IP_BOUND_IF) || defined(IPV6_BOUND_IF)
651 if (!
my->info.socket.inet.ifindex)
return 0;
653 opt =
my->info.socket.inet.ifindex;
655 switch (
my->info.socket.af) {
657 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IP, IP_BOUND_IF, &opt,
sizeof(opt));
661 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &opt,
sizeof(opt));
666 errno = EAFNOSUPPORT;
687 if (!
my->info.socket.inet.ifindex)
return 0;
699 struct sockaddr_storage salocal;
701 fr_assert((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6));
703#ifdef HAVE_CAPABILITY_H
709 if ((
my->info.socket.inet.src_port < 1024) && (geteuid() != 0)) {
710 (void)fr_cap_enable(CAP_NET_BIND_SERVICE, CAP_EFFECTIVE);
719 if (
fr_ipaddr_to_sockaddr(&salocal, &salen, &
my->info.socket.inet.src_ipaddr,
my->info.socket.inet.src_port) < 0)
return -1;
721 if (bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen) < 0) {
736 switch (
my->info.type) {
743 switch (
my->info.socket.af) {
748 my->info.socket.inet.src_port);
758 my->info.name =
"??? invalid BIO ???";
765 switch (
my->info.socket.af) {
768 my->info.name =
fr_asprintf(
my,
"proto %s local %pV port %u remote %pV port %u",
771 my->info.socket.inet.src_port,
773 my->info.socket.inet.dst_port);
778 (cfg->
socket_type == SOCK_DGRAM) ?
"(datagram) " :
"",
785 if (cfg->
flags == O_RDONLY) {
789 }
else if (cfg->
flags == O_WRONLY) {
800 my->info.name =
"??? invalid BIO ???";
808 switch (
my->info.socket.af) {
814 my->info.socket.inet.src_port);
824 my->info.name =
"??? invalid BIO ???";
868 fr_strerror_printf(
"Source and destination IP addresses are not from the same IP address family");
885 fr_strerror_const(
"Unconnected sockets cannot be used with Unix sockets or files");
929 my->info.socket.inet.src_port = cfg->
src_port;
930 my->info.socket.inet.dst_port = cfg->
dst_port;
946 if (
my->info.socket.inet.src_ipaddr.af == AF_UNSPEC) {
948 .af =
my->info.socket.inet.dst_ipaddr.af,
949 .prefix = (
my->info.socket.inet.dst_ipaddr.af == AF_INET) ? 32 : 128,
955 my->info.socket.af =
my->info.socket.inet.dst_ipaddr.af;
961 if (
my->info.socket.inet.src_ipaddr.af !=
my->info.socket.inet.dst_ipaddr.af) {
962 fr_strerror_const(
"Source and destination IP addresses are not from the same IP address family");
969 fr_assert(
my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
973 fr_assert(
my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
974 fr_assert(
my->info.socket.inet.dst_ipaddr.af != AF_UNSPEC);
979 protocol = IPPROTO_TCP;
981 protocol = IPPROTO_UDP;
985 my->info.socket.inet.ifindex = if_nametoindex(cfg->
interface);
987 if (!
my->info.socket.inet.ifindex) {
997 fd =
my->info.socket.fd;
1001 fd = socket(
my->info.socket.af,
my->info.socket.type, protocol);
1008 }
else if (cfg->
path) {
1009 my->info.socket.af = AF_LOCAL;
1010 my->info.socket.type = SOCK_STREAM;
1011 my->info.socket.unix.path = cfg->
path;
1013 fd = socket(
my->info.socket.af,
my->info.socket.type, 0);
1029 my->info.socket.type = SOCK_STREAM;
1035 if (strcmp(cfg->
filename,
"/dev/stdout") == 0) {
1036 if (cfg->
flags != O_WRONLY) {
1042 fd = dup(STDOUT_FILENO);
1044 }
else if (strcmp(cfg->
filename,
"/dev/stderr") == 0) {
1045 if (cfg->
flags != O_WRONLY)
goto fail_dev;
1047 fd = dup(STDERR_FILENO);
1049 }
else if (strcmp(cfg->
filename,
"/dev/stdin") == 0) {
1050 if (cfg->
flags != O_RDONLY) {
1055 fd = dup(STDIN_FILENO);
1061 my->info.socket.fd = -1;
1082 my->info.cfg = NULL;
1091 rcode = fcntl(fd, F_GETFD);
1093 if (fcntl(fd, F_SETFD, rcode |
FD_CLOEXEC) < 0) {
1108 my->info.socket.fd = fd;
1113 my->info.type = cfg->
type;
1119 switch (cfg->
type) {
1127 if (
my->info.socket.type != SOCK_DGRAM) {
1128 fr_strerror_const(
"Failed configuring socket: unconnected sockets must be UDP");
1132 switch (
my->info.socket.af) {
1161 if (
my->info.socket.type == SOCK_DGRAM) {
1163 if (rcode < 0)
goto fail;
1165 }
else if ((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6)) {
1167 if (rcode < 0)
goto fail;
1170 switch (
my->info.socket.af) {
1202 setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_NOSIGPIPE, &on,
sizeof(on));
1217 switch (
my->info.socket.af) {
1270 if (flags != O_RDONLY) flags |= O_CREAT;
1288 char *p = strrchr(cfg->
filename,
'/');
1291 if (!p)
goto do_open;
1301 fd = openat(dir_fd, p + 1, flags, cfg->
perm);
1311 if (
my->info.socket.fd < 0) {
1319 if (dup2(fd,
my->info.socket.fd) < 0) {
1326 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_ACCEPTED
temporarily until it's connected.
@ 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_BIO_FD_STATE_OPEN
error states must be before this
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.
fr_ipaddr_t src_ipaddr
our IP address
Configuration for sockets.
Run-time status of the socket.
static int fr_bio_fd_socket_unix_mkdir(int *dirfd, char const **filename, fr_bio_fd_config_t const *cfg)
static void fr_bio_fd_name(fr_bio_fd_t *my)
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 void fr_bio_fd_unix_shutdown(fr_bio_t *bio)
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.
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.
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_socket_bind_to_device(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
This system is missing SO_BINDTODEVICE, IP_BOUND_IF, IPV6_BOUND_IF.
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_server_udp(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
Initialize a UDP server socket.
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.
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_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.
int fr_nonblock(UNUSED int fd)
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
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.
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)