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>
62 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on,
sizeof(on)) < 0) {
72# if !defined(SOL_TCP) && defined(IPPROTO_TCP)
73# define SOL_TCP IPPROTO_TCP
83 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on,
sizeof(on)) < 0) {
107 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on,
sizeof(
int)) < 0) {
112#elif defined(SO_TIMESTAMP)
118 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on,
sizeof(
int)) < 0) {
132 if (opt > (1 << 29)) opt = (1 << 29);
134 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt,
sizeof(opt)) < 0) {
148 if (opt > (1 << 29)) opt = (1 << 29);
150 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt,
sizeof(opt)) < 0) {
176 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on,
sizeof(on)) < 0) {
203#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
209 int flag = IP_PMTUDISC_DONT;
211 if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag,
sizeof(flag)) < 0) {
218#if defined(IP_DONTFRAG)
225 if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &off,
sizeof(off)) < 0) {
243 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) < 0) {
273 if (IN6_IS_ADDR_UNSPECIFIED(
UNCONST(
struct in6_addr *, &sock->inet.src_ipaddr.addr.v6))) {
276 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (
char *)&on,
sizeof(on)) < 0) {
304 if (fstatat(dirfd, filename, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
305 if (errno != ENOENT) {
316 if (!S_ISSOCK(buf.st_mode)) {
325 if (buf.st_uid != cfg->
uid) {
348 if (unlinkat(dirfd, filename, 0) < 0) {
365 char const *path = cfg->
path;
366 char *p, *dir = NULL;
369 perm = S_IREAD | S_IWRITE | S_IEXEC;
370 perm |= S_IRGRP | S_IWGRP | S_IXGRP;
378 if (
fr_dirfd(dirfd, filename, path) == 0) {
384 if (fstat(*dirfd, &buf) < 0) {
392 if (buf.st_uid != cfg->
uid) {
393 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect UID", path);
397 if (buf.st_gid != cfg->
gid) {
398 fr_strerror_printf(
"Failed reading parent directory for file %s: Incorrect GID", path);
407 if (fchmod(*dirfd, perm) < 0) {
424 slashes[0] = slashes[1] = NULL;
425 for (p = dir; *p !=
'\0'; p++) {
427 slashes[0] = slashes[1];
460 parent_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
471 if (mkdirat(parent_fd, slashes[0] + 1, 0700) < 0) {
478 fd = openat(parent_fd, slashes[0] + 1, O_DIRECTORY);
484 if (fchmod(fd, perm) < 0) {
496 if (fchown(fd, cfg->
uid, cfg->
gid) < 0) {
502 path = strrchr(cfg->
path,
'/');
530 if (
my->user_shutdown) {
533 rcode =
my->user_shutdown(bio);
534 if (rcode < 0)
return rcode;
537 if (unlink(
my->info.socket.unix.path) < 0)
return fr_bio_error(GENERIC);
556 char const *filename, *p;
558 struct sockaddr_un sun;
569 if (cfg->
uid == (uid_t) -1) {
574 if (cfg->
gid == (gid_t) -1) {
582 p = strrchr(cfg->
path,
'/');
585 filename = cfg->
path;
587 }
else if (p == cfg->
path) {
591 fr_strerror_printf(
"Failed opening domain socket %s: cannot exist at file system root", p);
603 if (dirfd != AT_FDCWD) close(dirfd);
616 rcode = bindat(dirfd,
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
623 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &sun, sunlen);
638 if (fchmod(
my->info.socket.fd, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
648 if (fchown(
my->info.socket.fd, cfg->
uid, cfg->
gid) < 0) {
658 if (
my->cb.shutdown)
my->user_shutdown =
my->cb.shutdown;
660 if (dirfd != AT_FDCWD) close(dirfd);
669#undef SO_BINDTODEVICE
672#ifdef SO_BINDTODEVICE
681 if (!
my->info.socket.inet.ifindex)
return 0;
692#ifdef HAVE_CAPABILITY_H
696 if (setsockopt(
my->info.socket.fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->
interface, strlen(cfg->
interface)) < 0) {
704#elif defined(IP_BOUND_IF) || defined(IPV6_BOUND_IF)
712 if (!
my->info.socket.inet.ifindex)
return 0;
714 opt =
my->info.socket.inet.ifindex;
716 switch (
my->info.socket.af) {
718 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IP, IP_BOUND_IF, &opt,
sizeof(opt));
722 rcode = setsockopt(
my->info.socket.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &opt,
sizeof(opt));
727 errno = EAFNOSUPPORT;
748 if (!
my->info.socket.inet.ifindex)
return 0;
762 struct sockaddr_storage salocal;
764 fr_assert((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6));
769 if (
my->info.cfg->src_port_start) {
770 if (
my->info.cfg->src_port_start < 1024) {
775 if (
my->info.cfg->src_port_end <=
my->info.cfg->src_port_start) {
785 do_suid = (
my->info.socket.inet.src_port > 0) && (
my->info.socket.inet.src_port < 1024) && (geteuid() != 0);
787#ifdef HAVE_CAPABILITY_H
791 if (do_suid) do_suid = (fr_cap_enable(CAP_NET_BIND_SERVICE,
CAP_EFFECTIVE) < 0);
809 if (
fr_ipaddr_to_sockaddr(&salocal, &salen, &
my->info.socket.inet.src_ipaddr,
my->info.socket.inet.src_port) < 0)
goto down;
814 if (!
my->info.cfg->src_port_start) {
815 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
825 struct sockaddr_in *sin = (
struct sockaddr_in *) &salocal;
829 fr_assert(
my->info.cfg->src_port_end >=
my->info.cfg->src_port_start);
831 range =
my->info.cfg->src_port_end -
my->info.cfg->src_port_start;
839 sin->sin_port = htons(
my->info.cfg->src_port_start + src_port);
845 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
856 if (current == range) {
865 if (current == src_port)
break;
867 sin->sin_port = htons(
my->info.cfg->src_port_start + current);
869 rcode = bind(
my->info.socket.fd, (
struct sockaddr *) &salocal, salen);
899 switch (
my->info.type) {
906 switch (
my->info.socket.af) {
911 my->info.socket.inet.src_port);
921 my->info.name =
"??? invalid BIO ???";
927 switch (
my->info.socket.af) {
930 my->info.name =
fr_asprintf(
my,
"proto %s local %pV port %u remote %pV port %u",
933 my->info.socket.inet.src_port,
935 my->info.socket.inet.dst_port);
940 (cfg->
socket_type == SOCK_DGRAM) ?
"(datagram) " :
"",
947 if (cfg->
flags == O_RDONLY) {
951 }
else if (cfg->
flags == O_WRONLY) {
962 my->info.name =
"??? invalid BIO ???";
968 switch (
my->info.socket.af) {
974 my->info.socket.inet.src_port);
984 my->info.name =
"??? invalid BIO ???";
1000 switch (cfg->
type) {
1023 fr_strerror_printf(
"Source and destination IP addresses are not from the same IP address family");
1040 fr_strerror_const(
"Unconnected sockets cannot be used with Unix sockets or files");
1080 my->info.socket.inet.src_port = cfg->
src_port;
1081 my->info.socket.inet.dst_port = cfg->
dst_port;
1089 switch (cfg->
type) {
1097 if (
my->info.socket.inet.src_ipaddr.af == AF_UNSPEC) {
1099 .af =
my->info.socket.inet.dst_ipaddr.af,
1100 .prefix = (
my->info.socket.inet.dst_ipaddr.af == AF_INET) ? 32 : 128,
1106 my->info.socket.af =
my->info.socket.inet.dst_ipaddr.af;
1112 if (
my->info.socket.inet.src_ipaddr.af !=
my->info.socket.inet.dst_ipaddr.af) {
1113 fr_strerror_const(
"Source and destination IP addresses are not from the same IP address family");
1131 fr_strerror_const(
"Cannot set 'reuse_port' for connected UDP sockets with wildcard IP");
1140 fr_assert(
my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
1145 protocol = IPPROTO_TCP;
1147 protocol = IPPROTO_UDP;
1151 my->info.socket.inet.ifindex = if_nametoindex(cfg->
interface);
1153 if (!
my->info.socket.inet.ifindex) {
1159 fd = socket(
my->info.socket.af,
my->info.socket.type, protocol);
1165 }
else if (cfg->
path) {
1166 my->info.socket.af = AF_LOCAL;
1167 my->info.socket.type = SOCK_STREAM;
1168 my->info.socket.unix.path = cfg->
path;
1170 fd = socket(
my->info.socket.af,
my->info.socket.type, 0);
1186 my->info.socket.type = SOCK_STREAM;
1192 if (strcmp(cfg->
filename,
"/dev/stdout") == 0) {
1193 if (cfg->
flags != O_WRONLY) {
1199 fd = dup(STDOUT_FILENO);
1201 }
else if (strcmp(cfg->
filename,
"/dev/stderr") == 0) {
1202 if (cfg->
flags != O_WRONLY)
goto fail_dev;
1204 fd = dup(STDERR_FILENO);
1206 }
else if (strcmp(cfg->
filename,
"/dev/stdin") == 0) {
1207 if (cfg->
flags != O_RDONLY) {
1212 fd = dup(STDIN_FILENO);
1218 my->info.socket.fd = -1;
1240 my->info.cfg = NULL;
1255 my->info.socket.fd = fd;
1260 my->info.type = cfg->
type;
1266 switch (cfg->
type) {
1274 if (
my->info.socket.type != SOCK_DGRAM) {
1275 fr_strerror_const(
"Failed configuring socket: unconnected sockets must be UDP");
1279 switch (
my->info.socket.af) {
1307 if (
my->info.socket.type == SOCK_DGRAM) {
1308 switch (
my->info.socket.af) {
1318 }
else if ((
my->info.socket.af == AF_INET) || (
my->info.socket.af == AF_INET6)) {
1320 if (rcode < 0)
goto fail;
1323 switch (
my->info.socket.af) {
1347 if ((
my->info.socket.type == SOCK_DGRAM) && !cfg->
reuse_port) {
1353 switch (
my->info.socket.af) {
1407 if (flags != O_RDONLY) flags |= O_CREAT;
1425 char *p = strrchr(cfg->
filename,
'/');
1428 if (!p)
goto do_open;
1438 fd = openat(dir_fd, p + 1, flags, cfg->
perm);
1450 if (
my->info.socket.fd < 0) {
1458 if (dup2(fd,
my->info.socket.fd) < 0) {
1465 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)