The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
fd_open.c
Go to the documentation of this file.
1/*
2 * This program is is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: 2e93b74bf1c117546da5bdf9b4c7b78e415cea8b $
19 * @file lib/bio/fd_open.c
20 * @brief BIO abstractions for opening file descriptors
21 *
22 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
23 */
24
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>
29
30#include <sys/stat.h>
31#include <net/if.h>
32#include <fcntl.h>
33#include <libgen.h>
34#include <netinet/tcp.h>
35#include <netinet/in.h>
36
37/** Initialize common datagram information
38 *
39 */
40static int fr_bio_fd_common_tcp(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
41{
42 int on = 1;
43
44#ifdef SO_KEEPALIVE
45 /*
46 * TCP keepalives are always a good idea. Too many people put firewalls between critical
47 * systems, and then the firewalls drop live TCP streams.
48 */
49 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0) {
50 fr_strerror_printf("Failed setting SO_KEEPALIVE: %s", fr_syserror(errno));
51 return -1;
52 }
53#endif
54
55#ifdef TCP_NODELAY
56 /*
57 * Add some defines for *BSD, and Solaris systems.
58 */
59# if !defined(SOL_TCP) && defined(IPPROTO_TCP)
60# define SOL_TCP IPPROTO_TCP
61# endif
62
63 /*
64 * Also set TCP_NODELAY, to force the data to be written quickly.
65 *
66 * We buffer full packets in memory before we write them, so there's no reason for the kernel to
67 * sit around waiting for more data from us.
68 */
69 if (!cfg->tcp_delay) {
70 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) {
71 fr_strerror_printf("Failed setting TCP_NODELAY: %s", fr_syserror(errno));
72 return -1;
73 }
74 }
75#endif
76
77 return 0;
78}
79
80
81/** Initialize common datagram information
82 *
83 */
84static int fr_bio_fd_common_datagram(int fd, UNUSED fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
85{
86 int on = 1;
87
88#ifdef SO_TIMESTAMPNS
89 /*
90 * Enable receive timestamps, these should reflect
91 * when the packet was received, not when it was read
92 * from the socket.
93 */
94 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPNS, &on, sizeof(int)) < 0) {
95 fr_strerror_printf("Failed setting SO_TIMESTAMPNS: %s", fr_syserror(errno));
96 return -1;
97 }
98
99#elif defined(SO_TIMESTAMP)
100 /*
101 * Enable receive timestamps, these should reflect
102 * when the packet was received, not when it was read
103 * from the socket.
104 */
105 if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(int)) < 0) {
106 fr_strerror_printf("Failed setting SO_TIMESTAMP: %s", fr_syserror(errno));
107 return -1;
108 }
109#endif
110
111
112#ifdef SO_RCVBUF
113 if (cfg->recv_buff) {
114 int opt = cfg->recv_buff;
115
116 /*
117 * Clamp value to something reasonable.
118 */
119 if (opt > (1 << 29)) opt = (1 << 29);
120
121 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) < 0) {
122 fr_strerror_printf("Failed setting SO_RCVBUF: %s", fr_syserror(errno));
123 return -1;
124 }
125 }
126#endif
127
128#ifdef SO_SNDBUF
129 if (cfg->send_buff) {
130 int opt = cfg->send_buff;
131
132 /*
133 * Clamp value to something reasonable.
134 */
135 if (opt > (1 << 29)) opt = (1 << 29);
136
137 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) < 0) {
138 fr_strerror_printf("Failed setting SO_SNDBUF: %s", fr_syserror(errno));
139 return -1;
140 }
141 }
142#endif
143
144 return 0;
145}
146
147/** Initialize a UDP socket.
148 *
149 */
150static int fr_bio_fd_common_udp(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
151{
152#ifdef SO_REUSEPORT
153 /*
154 * Servers re-use ports by default. And clients, too, if they ask nicely.
155 */
156 if (cfg->reuse_port) {
157 int on = 1;
158
159 /*
160 * Set SO_REUSEPORT before bind, so that all sockets can
161 * listen on the same destination IP address.
162 */
163 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) {
164 fr_strerror_printf("Failed setting SO_REUSEPORT: %s", fr_syserror(errno));
165 return -1;
166 }
167 }
168#endif
169
170 /*
171 * IPv4 fragments packets. IPv6 does not.
172 *
173 * Many systems set the "don't fragment" bit by default for IPv4. This setting means that "too
174 * large" packets are silently discarded in the network.
175 *
176 * Many local networks support UDP fragmentation, so we just send packets and hope for the best.
177 * In order to support local networks, we disable the "don't fragment" bit.
178 *
179 * The wider Internet does not support UDP fragmentation. This means that fragmented packets are
180 * silently discarded.
181 *
182 * @todo - add more code to properly handle EMSGSIZE for PMTUD. We can then also do
183 * getsockopt(fd,IP_MTU,&integer) to get the current path MTU. It can only be used after the
184 * socket has been connected. It should also be called after an EMSGSIZE error is returned.
185 *
186 * getsockopt(fd,IP_MTU,&integer) can also be used for unconnected sockets, if we also set
187 * IP_RECVERR. In which case the errors are put into an error queue. This is only for Linux.
188 */
189 if ((sock->af == AF_INET) && cfg->exceed_mtu) {
190#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
191 /*
192 * Disable PMTU discovery. On Linux, this also makes sure that the "don't
193 * fragment" flag is zero.
194 */
195 {
196 int flag = IP_PMTUDISC_DONT;
197
198 if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &flag, sizeof(flag)) < 0) {
199 fr_strerror_printf("Failed setting IP_MTU_DISCOVER: %s", fr_syserror(errno));
200 return -1;
201 }
202 }
203#endif
204
205#if defined(IP_DONTFRAG)
206 /*
207 * Ensure that the "don't fragment" flag is zero.
208 */
209 {
210 int off = 0;
211
212 if (setsockopt(fd, IPPROTO_IP, IP_DONTFRAG, &off, sizeof(off)) < 0) {
213 fr_strerror_printf("Failed setting IP_DONTFRAG: %s", fr_syserror(errno));
214 return -1;
215 }
216 }
217#endif
218 }
219
220 return fr_bio_fd_common_datagram(fd, sock, cfg);
221}
222
223/** Initialize a TCP server socket.
224 *
225 */
226static int fr_bio_fd_server_tcp(int fd, UNUSED fr_socket_t const *sock)
227{
228 int on = 1;
229
230 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
231 fr_strerror_printf("Failed setting SO_REUSEADDR: %s", fr_syserror(errno));
232 return -1;
233 }
234
235 return 0;
236}
237
238/** Initialize an IPv4 server socket.
239 *
240 */
241static int fr_bio_fd_server_ipv4(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
242{
243 /*
244 * And set up any UDP / TCP specific information.
245 */
246 if (sock->type == SOCK_DGRAM) return fr_bio_fd_common_udp(fd, sock, cfg);
247
248 return fr_bio_fd_server_tcp(fd, sock);
249}
250
251/** Initialize an IPv6 server socket.
252 *
253 */
254static int fr_bio_fd_server_ipv6(int fd, fr_socket_t const *sock, fr_bio_fd_config_t const *cfg)
255{
256#ifdef IPV6_V6ONLY
257 /*
258 * Don't allow v4 packets on v6 connections.
259 */
260 if (IN6_IS_ADDR_UNSPECIFIED(UNCONST(struct in6_addr *, &sock->inet.src_ipaddr.addr.v6))) {
261 int on = 1;
262
263 if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on, sizeof(on)) < 0) {
264 fr_strerror_printf("Failed setting IPV6_ONLY: %s", fr_syserror(errno));
265 return -1;
266 }
267 }
268#endif /* IPV6_V6ONLY */
269
270 /*
271 * And set up any UDP / TCP specific information.
272 */
273 if (sock->type == SOCK_DGRAM) return fr_bio_fd_common_udp(fd, sock, cfg);
274
275 return fr_bio_fd_server_tcp(fd, sock);
276}
277
278/** Verify or clean up a pre-existing domain socket.
279 *
280 */
281static int fr_bio_fd_socket_unix_verify(int dirfd, char const *filename, fr_bio_fd_config_t const *cfg)
282{
283 int fd;
284 struct stat buf;
285
286 /*
287 * See if the socket exits. If there's an error opening it, that's an issue.
288 *
289 * If it doesn't exist, that's fine.
290 */
291 if (fstatat(dirfd, filename, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
292 if (errno != ENOENT) {
293 fr_strerror_printf("Failed opening domain socket %s: %s", cfg->path, fr_syserror(errno));
294 return -1;
295 }
296
297 return 0;
298 }
299
300 /*
301 * If it exists, it must be a socket.
302 */
303 if (!S_ISSOCK(buf.st_mode)) {
304 fr_strerror_printf("Failed open domain socket %s: it is not a socket", filename);
305 return -1;
306 }
307
308 /*
309 * Refuse to open sockets not owned by us. This prevents configurations from stomping on each
310 * other.
311 */
312 if (buf.st_uid != cfg->uid) {
313 fr_strerror_printf("Failed opening domain socket %s: incorrect UID", cfg->path);
314 return -1;
315 }
316
317 /*
318 * The file exists,and someone is listening. We can't claim it for ourselves.
319 *
320 * Note that this function calls connect(), but connect() always returns immediately for domain
321 * sockets.
322 *
323 * @todo - redo that function here, with separate checks for permission errors vs anything else.
324 */
325 fd = fr_socket_client_unix(cfg->path, false);
326 if (fd >= 0) {
327 close(fd);
328 fr_strerror_printf("Failed creating domain socket %s: It is currently active", cfg->path);
329 return -1;
330 }
331
332 /*
333 * It exists, but no one is listening. Delete it so that we can re-bind to it.
334 */
335 if (unlinkat(dirfd, filename, 0) < 0) {
336 fr_strerror_printf("Failed removing pre-existing domain socket %s: %s",
337 cfg->path, fr_syserror(errno));
338 return -1;
339 }
340
341 return 0;
342}
343
344/*
345 * We normally can't call fchmod() or fchown() on sockets, as they don't really exist in the file system.
346 * Instead, we enforce those permissions on the parent directory of the socket.
347 */
348static int fr_bio_fd_socket_unix_mkdir(int *dirfd, char const **filename, fr_bio_fd_config_t const *cfg)
349{
350 mode_t perm;
351 int parent_fd, fd;
352 char const *path = cfg->path;
353 char *p, *dir = NULL;
354 char *slashes[2];
355
356 perm = S_IREAD | S_IWRITE | S_IEXEC;
357 perm |= S_IRGRP | S_IWGRP | S_IXGRP;
358
359 /*
360 * The parent directory exists. Ensure that it has the correct ownership and permissions.
361 *
362 * If the parent directory exists, then it enforces access, and we can create the domain socket
363 * within it.
364 */
365 if (fr_dirfd(dirfd, filename, path) == 0) {
366 struct stat buf;
367
368 if (fstat(*dirfd, &buf) < 0) {
369 fr_strerror_printf("Failed reading parent directory for file %s: %s", path, fr_syserror(errno));
370 fail:
371 talloc_free(dir);
372 close(*dirfd);
373 return -1;
374 }
375
376 if (buf.st_uid != cfg->uid) {
377 fr_strerror_printf("Failed reading parent directory for file %s: Incorrect UID", path);
378 goto fail;
379 }
380
381 if (buf.st_gid != cfg->gid) {
382 fr_strerror_printf("Failed reading parent directory for file %s: Incorrect GID", path);
383 goto fail;
384 }
385
386 /*
387 * We don't have the correct permissions on the directory, so we fix them.
388 *
389 * @todo - allow for "other" to read/write if we do authentication on the socket?
390 */
391 if (fchmod(*dirfd, perm) < 0) {
392 fr_strerror_printf("Failed setting parent directory permissions for file %s: %s", path, fr_syserror(errno));
393 goto fail;
394 }
395
396 return 0;
397 }
398
399 dir = talloc_strdup(NULL, path);
400 if (!dir) goto fail;
401
402 /*
403 * Find the last two directory separators.
404 */
405 slashes[0] = slashes[1] = NULL;
406 for (p = dir; *p != '\0'; p++) {
407 if (*p == '/') {
408 slashes[0] = slashes[1];
409 slashes[1] = p;
410 }
411 }
412
413 /*
414 * There's only one / in the path, we can't do anything.
415 *
416 * Opening 'foo/bar.sock' might be useful, but isn't normally a good idea.
417 */
418 if (!slashes[0]) {
419 fr_strerror_printf("Failed parsing filename %s: it is not absolute", path);
420 goto fail;
421 }
422
423 /*
424 * Ensure that the grandparent directory exists.
425 *
426 * /var/run/radiusd/foo.sock
427 *
428 * slashes[0] points to the slash after 'run'.
429 *
430 * slashes[1] points to the slash after 'radiusd', which doesn't exist.
431 */
432 *slashes[0] = '\0';
433
434 /*
435 * If the grandparent doesn't exist, then we don't create it.
436 *
437 * These checks minimize the possibility that a misconfiguration by user "radiusd" can cause a
438 * suid-root binary top create a directory in the wrong place. These checks are only necessary
439 * if the unix domain socket is opened as root.
440 */
441 parent_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
442 if (parent_fd < 0) {
443 fr_strerror_printf("Failed opening directory %s: %s", dir, fr_syserror(errno));
444 goto fail;
445 }
446
447 /*
448 * Create the parent directory.
449 */
450 *slashes[0] = '/';
451 *slashes[1] = '\0';
452 if (mkdirat(parent_fd, dir, 0700) < 0) {
453 fr_strerror_printf("Failed creating directory %s: %s", dir, fr_syserror(errno));
454 close_parent:
455 close(parent_fd);
456 goto fail;
457 }
458
459 fd = openat(parent_fd, dir, O_DIRECTORY);
460 if (fd < 0) {
461 fr_strerror_printf("Failed opening directory %s: %s", dir, fr_syserror(errno));
462 goto close_parent;
463 }
464
465 if (fchmod(fd, perm) < 0) {
466 fr_strerror_printf("Failed changing permission for directory %s: %s", dir, fr_syserror(errno));
467 close_fd:
468 close(fd);
469 goto close_parent;
470 }
471
472 /*
473 * This is a NOOP if we're chowning a file owned by ourselves to our own UID / GID.
474 *
475 * Otherwise if we're running as root, it will set ownership to the correct user.
476 */
477 if (fchown(fd, cfg->uid, cfg->gid) < 0) {
478 fr_strerror_printf("Failed changing ownershipt for directory %s: %s", dir, fr_syserror(errno));
479 goto close_fd;
480 }
481
482 talloc_free(dir);
483 close(fd);
484 close(parent_fd);
485
486 return 0;
487}
488
490{
491 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
492
493 /*
494 * This is called after fr_bio_fd_close() - which marks the bio state as closed
495 *
496 * Unix domain sockets are deleted when the bio is closed.
497 *
498 * Unix domain sockets are never in the "connecting" state, because connect() always returns
499 * immediately.
500 */
501 fr_assert(my->info.state == FR_BIO_FD_STATE_CLOSED);
502
503 /*
504 * Run the user shutdown before we run ours.
505 */
506 if (my->user_shutdown) {
507 int rcode;
508
509 rcode = my->user_shutdown(bio);
510 if (rcode < 0) return rcode;
511 }
512
513 if (unlink(my->info.socket.unix.path) < 0) return fr_bio_error(GENERIC);
514
515 return 0;
516}
517
518/** Bind to a Unix domain socket.
519 *
520 * @todo - this function only does a tiny bit of what fr_server_domain_socket_peercred() and
521 * fr_server_domain_socket_perm() do. Those functions do a lot more sanity checks.
522 *
523 * The main question is whether or not those checks are useful. In many cases, fchmod() and fchown() are not
524 * possible on Unix sockets, so we shouldn't bother doing them,
525 *
526 * Note that the listeners generally call these functions with wrappers of fr_suid_up() and fr_suid_down().
527 * So these functions are running as "root", and will create files owned as "root".
528 */
530{
531 int dirfd, rcode;
532 char const *filename, *p;
533 socklen_t sunlen;
534 struct sockaddr_un sun;
535
536 if (!cfg->path) {
537 fr_strerror_const("Failed to specify path");
538 return -1;
539 }
540
541 /*
542 * The UID and GID should be taken automatically from the "user" and "group" settings in
543 * mainconfig. There is no reason to set them to anything else.
544 */
545 if (cfg->uid == (uid_t) -1) {
546 fr_strerror_printf("Failed opening domain socket %s: no UID specified", cfg->path);
547 return -1;
548 }
549
550 if (cfg->gid == (gid_t) -1) {
551 fr_strerror_printf("Failed opening domain socket %s: no GID specified", cfg->path);
552 return -1;
553 }
554
555 /*
556 * Opening 'foo.sock' is OK.
557 */
558 p = strrchr(cfg->path, '/');
559 if (!p) {
560 dirfd = AT_FDCWD;
561 filename = cfg->path;
562
563 } else if (p == cfg->path) {
564 /*
565 * Opening '/foo.sock' is dumb.
566 */
567 fr_strerror_printf("Failed opening domain socket %s: cannot exist at file system root", p);
568 return -1;
569
570 } else if (fr_bio_fd_socket_unix_mkdir(&dirfd, &filename, cfg) < 0) {
571 return -1;
572 }
573
574 /*
575 * Verify and/or clean up the domain socket.
576 */
577 if (fr_bio_fd_socket_unix_verify(dirfd, filename, cfg) < 0) {
578 fail:
579 if (dirfd != AT_FDCWD) close(dirfd);
580 return -1;
581 }
582
583#ifdef HAVE_BINDAT
584 /*
585 * The best function to use here is bindat(), but only quite recent versions of FreeBSD actually
586 * have it, and it's definitely not POSIX.
587 *
588 * If we use bindat(), we pass a relative pathname.
589 */
590 if (fr_filename_to_sockaddr(&sun, &sunlen, filename) < 0) goto fail;
591
592 rcode = bindat(dirfd, my->info.socket.fd, (struct sockaddr *) &sun, sunlen);
593#else
594 /*
595 * For bind(), we pass the full path.
596 */
597 if (fr_filename_to_sockaddr(&sun, &sunlen, cfg->path) < 0) goto fail;
598
599 rcode = bind(my->info.socket.fd, (struct sockaddr *) &sun, sunlen);
600#endif
601 if (rcode < 0) {
602 /*
603 * @todo - if EADDRINUSE, then the socket exists. Try connect(), and if that fails,
604 * delete the socket and try again. This may be simpler than the checks above.
605 */
606 fr_strerror_printf("Failed binding to domain socket %s: %s", cfg->path, fr_syserror(errno));
607 goto fail;
608 }
609
610#ifdef __linux__
611 /*
612 * Linux supports chown && chmod for sockets.
613 */
614 if (fchmod(my->info.socket.fd, S_IREAD | S_IWRITE | S_IEXEC | S_IRGRP | S_IWGRP | S_IXGRP) < 0) {
615 fr_strerror_printf("Failed changing permission for domain socket %s: %s", cfg->path, fr_syserror(errno));
616 goto fail;
617 }
618
619 /*
620 * This is a NOOP if we're chowning a file owned by ourselves to our own UID / GID.
621 *
622 * Otherwise if we're running as root, it will set ownership to the correct user.
623 */
624 if (fchown(my->info.socket.fd, cfg->uid, cfg->gid) < 0) {
625 fr_strerror_printf("Failed changing ownershipt for domain directory %s: %s", cfg->path, fr_syserror(errno));
626 goto fail;
627 }
628
629#endif
630
631 /*
632 * Socket is open. We need to clean it up on shutdown.
633 */
634 if (my->cb.shutdown) my->user_shutdown = my->cb.shutdown;
635 my->cb.shutdown = fr_bio_fd_unix_shutdown;
636
637 return 0;
638}
639
640/*
641 * Use the OSX native versions on OSX.
642 */
643#ifdef __APPLE__
644#undef SO_BINDTODEVICE
645#endif
646
647#ifdef SO_BINDTODEVICE
648/** Linux bind to device by name.
649 *
650 */
652{
653 /*
654 * ifindex isn't set, do nothing.
655 */
656 if (!my->info.socket.inet.ifindex) return 0;
657
658 if (!cfg->interface) return 0;
659
660 /*
661 * The internet hints that CAP_NET_RAW is required to use SO_BINDTODEVICE.
662 *
663 * This function also sets fr_strerror() on failure, which will be seen if the bind fails. If
664 * the bind succeeds, then we don't really care that the capability change has failed. We must
665 * already have that capability.
666 */
667#ifdef HAVE_CAPABILITY_H
668 (void)fr_cap_enable(CAP_NET_RAW, CAP_EFFECTIVE);
669#endif
670
671 if (setsockopt(my->info.socket.fd, SOL_SOCKET, SO_BINDTODEVICE, cfg->interface, strlen(cfg->interface)) < 0) {
672 fr_strerror_printf("Failed setting SO_BINDTODEVICE for %s: %s", cfg->interface, fr_syserror(errno));
673 return -1;
674 }
675
676 return 0;
677}
678
679#elif defined(IP_BOUND_IF) || defined(IPV6_BOUND_IF)
680/** OSX bind to interface by index.
681 *
682 */
684{
685 int opt, rcode;
686
687 if (!my->info.socket.inet.ifindex) return 0;
688
689 opt = my->info.socket.inet.ifindex;
690
691 switch (my->info.socket.af) {
692 case AF_LOCAL:
693 rcode = setsockopt(my->info.socket.fd, IPPROTO_IP, IP_BOUND_IF, &opt, sizeof(opt));
694 break;
695
696 case AF_INET6:
697 rcode = setsockopt(my->info.socket.fd, IPPROTO_IPV6, IPV6_BOUND_IF, &opt, sizeof(opt));
698 break;
699
700 default:
701 rcode = -1;
702 errno = EAFNOSUPPORT;
703 break;
704 }
705
706 if (rcode < 0) fr_strerror_printf("Failed setting IP_BOUND_IF: %s", fr_syserror(errno));
707 return rcode;
708}
709#else
710
711/** This system is missing SO_BINDTODEVICE, IP_BOUND_IF, IPV6_BOUND_IF
712 *
713 * @todo - FreeBSD IP_RECVIF and IP_SENDIF
714 *
715 * Except that has to be done in recvmsg() and sendmsg(). And it only works on datagram sockets.
716 *
717 * cmsg_len = sizeof(struct sockaddr_dl)
718 * cmsg_level = IPPROTO_IP
719 * cmsg_type = IP_RECVIF
720 */
722{
723 if (!my->info.socket.inet.ifindex) return 0;
724
725 fr_strerror_const("Bind to interface is not supported on this platform");
726 return -1;
727}
728
729/* bind to device */
730#endif
731
733{
734 bool do_suid;
735 socklen_t salen;
736 struct sockaddr_storage salocal;
737
738 fr_assert((my->info.socket.af == AF_INET) || (my->info.socket.af == AF_INET6));
739
740 /*
741 * Ranges must be a high value.
742 */
743 if (my->info.cfg->src_port_start && (my->info.cfg->src_port_start < 1024)) {
744 fr_strerror_const("Cannot set src_port_start in the range 1..1023");
745 return -1;
746 }
747
748 /*
749 * Source port is in the restricted range, and we're not root, update permissions / capabilities
750 * so that the bind is permitted.
751 */
752 do_suid = (my->info.socket.inet.src_port > 0) && (my->info.socket.inet.src_port < 1024) && (geteuid() != 0);
753
754#ifdef HAVE_CAPABILITY_H
755 /*
756 * If we can set the capabilities, then we don't need to do SUID.
757 */
758 if (do_suid) do_suid = (fr_cap_enable(CAP_NET_BIND_SERVICE, CAP_EFFECTIVE) < 0);
759#endif
760
761 /*
762 * SUID up before we bind to the interface. If we can set the capabilities above, then should
763 * also be able to set the capabilities to bind to an interface.
764 */
765 if (do_suid) fr_suid_up();
766
767 if (fr_bio_fd_socket_bind_to_device(my, cfg) < 0) {
768 down:
769 if (do_suid) fr_suid_down();
770 return -1;
771 }
772
773 /*
774 * Get the sockaddr for bind()
775 */
776 if (fr_ipaddr_to_sockaddr(&salocal, &salen, &my->info.socket.inet.src_ipaddr, my->info.socket.inet.src_port) < 0) goto down;
777
778 /*
779 * We don't have a source port range, just bind to whatever source port that we're given.
780 */
781 if (!my->info.cfg->src_port_start) {
782 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) < 0) {
783 fr_strerror_printf("Failed binding to socket: %s", fr_syserror(errno));
784 goto down;
785 }
786 if (do_suid) fr_suid_down();
787
788 } else {
789 uint16_t src_port, current, range;
790 struct sockaddr_in *sin = (struct sockaddr_in *) &salocal;
791
792 fr_assert(my->info.cfg->src_port_start);
793 fr_assert(my->info.cfg->src_port_end);
794 fr_assert(my->info.cfg->src_port_end >= my->info.cfg->src_port_start);
795
796 range = my->info.cfg->src_port_end - my->info.cfg->src_port_start;
797 src_port = fr_rand() % range;
798
799 /*
800 * We only care about sin_port, which is in the same location for sockaddr_in and sockaddr_in6.
801 */
802 fr_assert(sin->sin_port == 0);
803
804 sin->sin_port = htons(my->info.cfg->src_port_start + src_port);
805
806 /*
807 * We've picked a random port in what is hopefully a large range. If that works, we're
808 * done.
809 */
810 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
811
812 /*
813 * Hunt & peck. Which is horrible.
814 */
815 current = src_port;
816 while (true) {
817 if (current == range) {
818 current = 0;
819 } else {
820 current++;
821 }
822
823 /*
824 * We've already checked this, so stop.
825 */
826 if (current == src_port) break;
827
828 sin->sin_port = htons(my->info.cfg->src_port_start + current);
829
830 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
831 }
832
833 /*
834 * The error is a good hint at _why_ we failed to bind.
835 * We expect errno to be EADDRINUSE, anything else is a surprise.
836 */
837 fr_strerror_printf("Failed binding port between 'src_port_start' and 'src_port_end': %s", fr_syserror(errno));
838 return -1;
839 }
840
841 /*
842 * The IP and/or port may have changed, so get the new one.
843 */
844done:
846}
847
848/** Set the name of an FD BIO
849 *
850 */
852{
853 fr_bio_fd_config_t const *cfg = my->info.cfg;
854
855 switch (my->info.type) {
857 return;
858
860 fr_assert(cfg->socket_type == SOCK_DGRAM);
861
862 switch (my->info.socket.af) {
863 case AF_INET:
864 case AF_INET6:
865 my->info.name = fr_asprintf(my, "proto udp local %pV port %u",
866 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
867 my->info.socket.inet.src_port);
868 break;
869
870 case AF_LOCAL:
871 my->info.name = fr_asprintf(my, "proto unix (datagram) filename %s",
872 cfg->path);
873 break;
874
875 default:
876 fr_assert(0);
877 my->info.name = "??? invalid BIO ???";
878 break;
879 }
880 break;
881
883 switch (my->info.socket.af) {
884 case AF_INET:
885 case AF_INET6:
886 my->info.name = fr_asprintf(my, "proto %s local %pV port %u remote %pV port %u",
887 (cfg->socket_type == SOCK_DGRAM) ? "udp" : "tcp",
888 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
889 my->info.socket.inet.src_port,
890 fr_box_ipaddr(my->info.socket.inet.dst_ipaddr),
891 my->info.socket.inet.dst_port);
892 break;
893
894 case AF_LOCAL:
895 my->info.name = fr_asprintf(my, "proto unix %sfilename %s",
896 (cfg->socket_type == SOCK_DGRAM) ? "(datagram) " : "",
897 cfg->path);
898 break;
899
900 case AF_FILE_BIO:
901 fr_assert(cfg->socket_type == SOCK_STREAM);
902
903 if (cfg->flags == O_RDONLY) {
904 my->info.name = fr_asprintf(my, "proto file (read-only) filename %s ",
905 cfg->filename);
906
907 } else if (cfg->flags == O_WRONLY) {
908 my->info.name = fr_asprintf(my, "proto file (write-only) filename %s",
909 cfg->filename);
910 } else {
911 my->info.name = fr_asprintf(my, "proto file (read-write) filename %s",
912 cfg->filename);
913 }
914 break;
915
916 default:
917 fr_assert(0);
918 my->info.name = "??? invalid BIO ???";
919 break;
920 }
921 break;
922
923 case FR_BIO_FD_LISTEN:
924 switch (my->info.socket.af) {
925 case AF_INET:
926 case AF_INET6:
927 my->info.name = fr_asprintf(my, "proto %s local %pV port %u",
928 (cfg->socket_type == SOCK_DGRAM) ? "udp" : "tcp",
929 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
930 my->info.socket.inet.src_port);
931 break;
932
933 case AF_LOCAL:
934 my->info.name = fr_asprintf(my, "proto unix filename %s",
935 cfg->path);
936 break;
937
938 default:
939 fr_assert(0);
940 my->info.name = "??? invalid BIO ???";
941 break;
942 }
943 break;
944 }
945}
946
947/** Checks the configuration without modifying anything.
948 *
949 */
951{
952 /*
953 * Unix sockets and files are OK.
954 */
955 if (cfg->path || cfg->filename) return 0;
956
957 /*
958 * Sanitize the IP addresses.
959 *
960 */
961 switch (cfg->type) {
963 fr_strerror_const("No connection type was specified");
964 return -1;
965
967 /*
968 * Ensure that we have a destination address.
969 */
970 if (cfg->dst_ipaddr.af == AF_UNSPEC) {
971 fr_strerror_const("No destination IP address was specified");
972 return -1;
973 }
974
975 if (!cfg->dst_port) {
976 fr_strerror_const("No destination port was specified");
977 return -1;
978 }
979
980 /*
981 * The source IP has to be the same address family as the destination IP.
982 */
983 if ((cfg->src_ipaddr.af != AF_UNSPEC) && (cfg->src_ipaddr.af != cfg->dst_ipaddr.af)) {
984 fr_strerror_printf("Source and destination IP addresses are not from the same IP address family");
985 return -1;
986 }
987 break;
988
989 case FR_BIO_FD_LISTEN:
990 if (!cfg->src_port) {
991 fr_strerror_const("No source port was specified");
992 return -1;
993 }
995
996 /*
997 * Unconnected sockets can use a source port, but don't need one.
998 */
1000 if (cfg->path && cfg->filename) {
1001 fr_strerror_const("Unconnected sockets cannot be used with Unix sockets or files");
1002 return -1;
1003 }
1004
1005 if (cfg->src_ipaddr.af == AF_UNSPEC) {
1006 fr_strerror_const("No source IP address was specified");
1007 return -1;
1008 }
1009 break;
1010 }
1011
1012 return 0;
1013}
1014
1015/** Opens a socket and updates sock->fd
1016 *
1017 * If the socket is asynchronous, it also calls connect()
1018 */
1020{
1021 int fd;
1022 int rcode;
1023 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1024
1026
1027 my->info = (fr_bio_fd_info_t) {
1028 .socket = {
1029 .type = cfg->socket_type,
1030 },
1031 .cfg = cfg,
1032 };
1033
1034 if (!cfg->path && !cfg->filename) {
1035 int protocol;
1036
1037 my->info.socket.af = cfg->src_ipaddr.af;
1038 my->info.socket.inet.src_ipaddr = cfg->src_ipaddr;
1039 my->info.socket.inet.dst_ipaddr = cfg->dst_ipaddr;
1040 my->info.socket.inet.src_port = cfg->src_port;
1041 my->info.socket.inet.dst_port = cfg->dst_port;
1042
1043 if (fr_bio_fd_check_config(cfg) < 0) return -1;
1044
1045 /*
1046 * Sanitize the IP addresses.
1047 *
1048 */
1049 switch (cfg->type) {
1050 case FR_BIO_FD_INVALID:
1051 return -1;
1052
1054 /*
1055 * No source specified, just bootstrap it from the destination.
1056 */
1057 if (my->info.socket.inet.src_ipaddr.af == AF_UNSPEC) {
1058 my->info.socket.inet.src_ipaddr = (fr_ipaddr_t) {
1059 .af = my->info.socket.inet.dst_ipaddr.af,
1060 .prefix = (my->info.socket.inet.dst_ipaddr.af == AF_INET) ? 32 : 128,
1061 };
1062
1063 /*
1064 * Set the main socket AF too.
1065 */
1066 my->info.socket.af = my->info.socket.inet.dst_ipaddr.af;
1067 }
1068
1069 /*
1070 * The source IP has to be the same address family as the destination IP.
1071 */
1072 if (my->info.socket.inet.src_ipaddr.af != my->info.socket.inet.dst_ipaddr.af) {
1073 fr_strerror_const("Source and destination IP addresses are not from the same IP address family");
1074 return -1;
1075 }
1076
1077#ifdef SO_REUSEPORT
1078 /*
1079 * Connected UDP sockets really only matter when writing packets. They allow the
1080 * system to use write() instead of sendto().
1081 *
1082 * However for reading packets, the kernel does NOT check the source IP.
1083 * Instead, it just delivers any packet with the correct dst IP/port. Even
1084 * worse, if multiple sockets re-use the same dst IP/port, packets are delivered
1085 * to a _random_ socket.
1086 *
1087 * As a result, we cannot use wildcard sockets with SO_REUSEPORT, and UDP.
1088 */
1089 if (cfg->reuse_port && (cfg->socket_type == SOCK_DGRAM) &&
1090 fr_ipaddr_is_inaddr_any(&my->info.socket.inet.dst_ipaddr)) { /* checks AF, so we're OK */
1091 fr_strerror_const("Cannot set 'reuse_port' for connected UDP sockets with wildcard IP");
1092 return -1;
1093 }
1094#endif
1095
1096 break;
1097
1099 case FR_BIO_FD_LISTEN:
1100 fr_assert(my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
1101 break;
1102 }
1103
1104 if (cfg->socket_type == SOCK_STREAM) {
1105 protocol = IPPROTO_TCP;
1106 } else {
1107 protocol = IPPROTO_UDP;
1108 }
1109
1110 if (cfg->interface) {
1111 my->info.socket.inet.ifindex = if_nametoindex(cfg->interface);
1112
1113 if (!my->info.socket.inet.ifindex) {
1114 fr_strerror_printf_push("Failed finding interface %s: %s", cfg->interface, fr_syserror(errno));
1115 return -1;
1116 }
1117 }
1118
1119 fd = socket(my->info.socket.af, my->info.socket.type, protocol);
1120 if (fd < 0) {
1121 fr_strerror_printf("Failed opening socket: %s", fr_syserror(errno));
1122 return fr_bio_error(GENERIC);
1123 }
1124
1125 } else if (cfg->path) {
1126 my->info.socket.af = AF_LOCAL;
1127 my->info.socket.type = SOCK_STREAM;
1128 my->info.socket.unix.path = cfg->path;
1129
1130 fd = socket(my->info.socket.af, my->info.socket.type, 0);
1131 if (fd < 0) {
1132 fr_strerror_printf("Failed opening domain socket %s: %s", cfg->path, fr_syserror(errno));
1133 return fr_bio_error(GENERIC);
1134 }
1135
1136 } else {
1137 if (cfg->type != FR_BIO_FD_CONNECTED) {
1138 fr_strerror_printf("Can only use connected sockets for file IO");
1139 return -1;
1140 }
1141
1142 /*
1143 * Filenames overload the #fr_socket_t for now.
1144 */
1145 my->info.socket.af = AF_FILE_BIO;
1146 my->info.socket.type = SOCK_STREAM;
1147 my->info.socket.unix.path = cfg->filename;
1148
1149 /*
1150 * Allow hacks for stdout and stderr
1151 */
1152 if (strcmp(cfg->filename, "/dev/stdout") == 0) {
1153 if (cfg->flags != O_WRONLY) {
1154 fail_dev:
1155 fr_strerror_printf("Cannot read from %s", cfg->filename);
1156 return -1;
1157 }
1158
1159 fd = dup(STDOUT_FILENO);
1160
1161 } else if (strcmp(cfg->filename, "/dev/stderr") == 0) {
1162 if (cfg->flags != O_WRONLY) goto fail_dev;
1163
1164 fd = dup(STDERR_FILENO);
1165
1166 } else if (strcmp(cfg->filename, "/dev/stdin") == 0) {
1167 if (cfg->flags != O_RDONLY) {
1168 fr_strerror_printf("Cannot write to %s", cfg->filename);
1169 return -1;
1170 }
1171
1172 fd = dup(STDIN_FILENO);
1173
1174 } else {
1175 /*
1176 * Minor hacks so that we have only _one_ source of open / mkdir
1177 */
1178 my->info.socket.fd = -1;
1179
1180 fd = fr_bio_fd_reopen(bio);
1181 }
1182 if (fd < 0) {
1183 fr_strerror_printf("Failed opening file %s: %s", cfg->filename, fr_syserror(errno));
1184 return fr_bio_error(GENERIC);
1185 }
1186 }
1187
1188 /*
1189 * Set it to be non-blocking if required.
1190 */
1191 if (cfg->async && (fr_nonblock(fd) < 0)) {
1192 fr_strerror_printf("Failed setting O_NONBLOCK: %s", fr_syserror(errno));
1193 rcode = fr_bio_error(GENERIC);
1194
1195 fail:
1196 my->info.socket = (fr_socket_t) {
1197 .fd = -1,
1198 };
1199 my->info.state = FR_BIO_FD_STATE_CLOSED;
1200 my->info.cfg = NULL;
1201 close(fd);
1202 return rcode;
1203 }
1204
1205#ifdef FD_CLOEXEC
1206 /*
1207 * We don't want child processes inheriting these file descriptors.
1208 */
1209 rcode = fcntl(fd, F_GETFD);
1210 if (rcode >= 0) {
1211 if (fcntl(fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
1212 fr_strerror_printf("Failed setting FD_CLOEXEC: %s", fr_syserror(errno));
1213 goto fail;
1214 }
1215 }
1216#endif
1217
1218 /*
1219 * Initialize the bio information before calling the various setup functions.
1220 */
1221 my->info.state = FR_BIO_FD_STATE_CONNECTING;
1222
1223 /*
1224 * Set the FD so that the subsequent calls can use it.
1225 */
1226 my->info.socket.fd = fd;
1227
1228 /*
1229 * Set the type, too.
1230 */
1231 my->info.type = cfg->type;
1232
1233 /*
1234 * Do sanity checks, bootstrap common socket options, bind to the socket, and initialize the read
1235 * / write functions.
1236 */
1237 switch (cfg->type) {
1238 case FR_BIO_FD_INVALID:
1239 return -1;
1240
1241 /*
1242 * Unconnected UDP or datagram AF_LOCAL server sockets.
1243 */
1245 if (my->info.socket.type != SOCK_DGRAM) {
1246 fr_strerror_const("Failed configuring socket: unconnected sockets must be UDP");
1247 return -1;
1248 }
1249
1250 switch (my->info.socket.af) {
1251 case AF_INET:
1252 case AF_INET6:
1253 if ((rcode = fr_bio_fd_common_udp(fd, &my->info.socket, cfg)) < 0) goto fail;
1254 break;
1255
1256 case AF_LOCAL:
1257 if ((rcode = fr_bio_fd_common_datagram(fd, &my->info.socket, cfg)) < 0) goto fail;
1258 break;
1259
1260 case AF_FILE_BIO:
1261 fr_strerror_const("Filenames must use the connected API");
1262 goto fail;
1263
1264 default:
1265 fr_strerror_const("Unsupported address family for unconnected sockets");
1266 goto fail;
1267 }
1268
1269 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1270
1271 if ((rcode = fr_bio_fd_init_common(my)) < 0) goto fail;
1272 break;
1273
1274 /*
1275 * A connected client: UDP, TCP, AF_LOCAL, or AF_FILE_BIO
1276 */
1278 if (my->info.socket.type == SOCK_DGRAM) {
1279 switch (my->info.socket.af) {
1280 case AF_INET:
1281 case AF_INET6:
1282 if ((rcode = fr_bio_fd_common_udp(fd, &my->info.socket, cfg)) < 0) goto fail;
1283 break;
1284 default:
1285 if ((rcode = fr_bio_fd_common_datagram(fd, &my->info.socket, cfg)) < 0) goto fail;
1286 break;
1287 }
1288
1289 } else if ((my->info.socket.af == AF_INET) || (my->info.socket.af == AF_INET6)) {
1290 rcode = fr_bio_fd_common_tcp(fd, &my->info.socket, cfg);
1291 if (rcode < 0) goto fail;
1292 }
1293
1294 switch (my->info.socket.af) {
1295 case AF_LOCAL:
1296 if ((rcode = fr_bio_fd_socket_bind_unix(my, cfg)) < 0) goto fail;
1297 break;
1298
1299 case AF_FILE_BIO:
1300 break;
1301
1302 case AF_INET:
1303 case AF_INET6:
1304 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1305 break;
1306
1307 default:
1308 return -1;
1309 }
1310
1311 if ((rcode = fr_bio_fd_init_connected(my)) < 0) goto fail;
1312 break;
1313
1314 /*
1315 * Server socket which listens for new stream connections
1316 */
1317 case FR_BIO_FD_LISTEN:
1318 if ((my->info.socket.type == SOCK_DGRAM) && !cfg->reuse_port) {
1319 fr_strerror_const("reuseport must be set for datagram sockets");
1320 rcode = -1;
1321 goto fail;
1322 }
1323
1324 switch (my->info.socket.af) {
1325 case AF_INET:
1326 if ((rcode = fr_bio_fd_server_ipv4(fd, &my->info.socket, cfg)) < 0) goto fail;
1327
1328 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1329 break;
1330
1331 case AF_INET6:
1332 if ((rcode = fr_bio_fd_server_ipv6(fd, &my->info.socket, cfg)) < 0) goto fail;
1333
1334 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1335 break;
1336
1337 case AF_LOCAL:
1338 if ((rcode = fr_bio_fd_socket_bind_unix(my, cfg)) < 0) goto fail;
1339 break;
1340
1341 default:
1342 fr_strerror_const("Unsupported address family for accept() socket");
1343 rcode = -1;
1344 goto fail;
1345 }
1346
1347 if ((rcode = fr_bio_fd_init_listen(my)) < 0) goto fail;
1348 break;
1349 }
1350
1351 /*
1352 * Set the name of the BIO.
1353 */
1355
1356 return 0;
1357}
1358
1359/** Reopen a file BIO
1360 *
1361 * e.g. for log files.
1362 */
1364{
1365 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1366 fr_bio_fd_config_t const *cfg = my->info.cfg;
1367 int fd, flags;
1368
1369 if (my->info.socket.af != AF_FILE_BIO) {
1370 fr_strerror_const("Cannot reopen a non-file BIO");
1371 return -1;
1372 }
1373
1374 /*
1375 * Create it if necessary.
1376 */
1377 flags = cfg->flags;
1378 if (flags != O_RDONLY) flags |= O_CREAT;
1379
1380 if (!cfg->mkdir) {
1381 /*
1382 * Client BIOs writing to a file, and therefore need to create it.
1383 */
1384 do_open:
1385 fd = open(cfg->filename, flags, cfg->perm);
1386 if (fd < 0) {
1387 failed_open:
1388 fr_strerror_printf("Failed opening file %s: %s", cfg->filename, fr_syserror(errno));
1389 return -1;
1390 }
1391
1392 } else {
1393 /*
1394 * We make the parent directory if told to, AND if there's a '/' in the path.
1395 */
1396 char *p = strrchr(cfg->filename, '/');
1397 int dir_fd;
1398
1399 if (!p) goto do_open;
1400
1401 if (fr_mkdir(&dir_fd, cfg->filename, (size_t) (p - cfg->filename), cfg->perm, fr_mkdir_chown,
1402 &(fr_mkdir_chown_t) {
1403 .uid = cfg->uid,
1404 .gid = cfg->gid,
1405 }) < 0) {
1406 return -1;
1407 }
1408
1409 fd = openat(dir_fd, p + 1, flags, cfg->perm);
1410 if (fd < 0) {
1411 close(dir_fd);
1412 goto failed_open;
1413 }
1414 }
1415
1416 /*
1417 * We're boot-strapping, just set the new FD and return.
1418 */
1419 if (my->info.socket.fd < 0) {
1420 return fd;
1421 }
1422
1423 /*
1424 * Replace the FD rather than swapping it out with a new one. This is potentially more
1425 * thread-safe.
1426 */
1427 if (dup2(fd, my->info.socket.fd) < 0) {
1428 close(fd);
1429 fr_strerror_printf("Failed reopening file - %s", fr_syserror(errno));
1430 return -1;
1431 }
1432
1433 close(fd);
1434 return my->info.socket.fd;
1435}
#define fr_bio_error(_x)
Definition base.h:200
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:167
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:324
#define UNUSED
Definition build.h:317
#define CAP_EFFECTIVE
Definition cap.h:49
int fr_bio_fd_socket_name(fr_bio_fd_t *my)
Definition fd.c:653
int fr_bio_fd_init_connected(fr_bio_fd_t *my)
Definition fd.c:811
int fr_filename_to_sockaddr(struct sockaddr_un *sun, socklen_t *sunlen, char const *filename)
Definition fd.c:635
int fr_bio_fd_init_listen(fr_bio_fd_t *my)
Definition fd.c:915
int fr_bio_fd_init_common(fr_bio_fd_t *my)
Definition fd.c:874
uint16_t src_port
our port
Definition fd.h:91
@ FR_BIO_FD_CONNECTED
connected client sockets (UDP or TCP)
Definition fd.h:68
@ FR_BIO_FD_INVALID
not set
Definition fd.h:64
@ FR_BIO_FD_UNCONNECTED
unconnected UDP / datagram only
Definition fd.h:65
@ FR_BIO_FD_LISTEN
returns new fd in buffer on fr_bio_read() or fr_bio_fd_accept()
Definition fd.h:69
uint32_t recv_buff
How big the kernel's receive buffer should be.
Definition fd.h:101
bool mkdir
make intermediate directories
Definition fd.h:108
@ FR_BIO_FD_STATE_CLOSED
Definition fd.h:58
@ FR_BIO_FD_STATE_CONNECTING
Definition fd.h:60
fr_ipaddr_t dst_ipaddr
their IP address
Definition fd.h:89
fr_bio_fd_type_t type
accept, connected, unconnected, etc.
Definition fd.h:81
char const * path
for Unix domain sockets
Definition fd.h:104
uint32_t send_buff
How big the kernel's send buffer should be.
Definition fd.h:102
char const * filename
for files
Definition fd.h:110
uid_t uid
who owns the socket
Definition fd.h:106
gid_t gid
who owns the socket
Definition fd.h:107
char const * interface
for binding to an interface
Definition fd.h:97
mode_t perm
permissions for domain sockets
Definition fd.h:105
#define AF_FILE_BIO
Definition fd.h:40
int socket_type
SOCK_STREAM or SOCK_DGRAM.
Definition fd.h:83
uint16_t dst_port
their port
Definition fd.h:92
bool tcp_delay
We do tcp_nodelay by default.
Definition fd.h:114
bool reuse_port
whether or not we re-use the same destination port for datagram sockets
Definition fd.h:86
bool exceed_mtu
Whether we allow packets which exceed the local MTU.
Definition fd.h:115
bool async
is it async
Definition fd.h:113
int flags
O_RDONLY, etc.
Definition fd.h:111
fr_ipaddr_t src_ipaddr
our IP address
Definition fd.h:88
Configuration for sockets.
Definition fd.h:80
Run-time status of the socket.
Definition fd.h:131
void fr_bio_shutdown & my
Definition fd_errno.h:70
static int fr_bio_fd_socket_unix_mkdir(int *dirfd, char const **filename, fr_bio_fd_config_t const *cfg)
Definition fd_open.c:348
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.
Definition fd_open.c:721
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.
Definition fd_open.c:281
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.
Definition fd_open.c:254
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.
Definition fd_open.c:150
int fr_bio_fd_reopen(fr_bio_t *bio)
Reopen a file BIO.
Definition fd_open.c:1363
static int fr_bio_fd_server_tcp(int fd, UNUSED fr_socket_t const *sock)
Initialize a TCP server socket.
Definition fd_open.c:226
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.
Definition fd_open.c:529
void fr_bio_fd_name(fr_bio_fd_t *my)
Set the name of an FD BIO.
Definition fd_open.c:851
static int fr_bio_fd_socket_bind(fr_bio_fd_t *my, fr_bio_fd_config_t const *cfg)
Definition fd_open.c:732
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.
Definition fd_open.c:241
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.
Definition fd_open.c:40
static int fr_bio_fd_unix_shutdown(fr_bio_t *bio)
Definition fd_open.c:489
int fr_bio_fd_open(fr_bio_t *bio, fr_bio_fd_config_t const *cfg)
Opens a socket and updates sock->fd.
Definition fd_open.c:1019
int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg)
Checks the configuration without modifying anything.
Definition fd_open.c:950
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.
Definition fd_open.c:84
Our FD bio structure.
Definition fd_priv.h:35
talloc_free(hp)
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
Definition inet.c:63
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.
Definition inet.c:1393
int af
Address family.
Definition inet.h:64
IPv4/6 prefix.
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.
Definition file.c:219
int fr_mkdir_chown(int fd, char const *path, void *uctx)
Callback for the common case of chown() of the directory.
Definition file.c:39
int fr_dirfd(int *dirfd, char const **filename, char const *pathname)
From a pathname, return fd and filename needed for *at() functions.
Definition file.c:412
unsigned short uint16_t
unsigned int mode_t
int fr_nonblock(UNUSED int fd)
Definition misc.c:294
fr_suid_t fr_suid_up
Definition misc.c:569
fr_suid_t fr_suid_down
Definition misc.c:570
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
Definition print.c:883
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
static bool done
Definition radclient.c:83
uint32_t fr_rand(void)
Return a 32-bit random number.
Definition rand.c:105
#define FD_CLOEXEC
int fr_socket_client_unix(UNUSED char const *path, UNUSED bool async)
Definition socket.c:570
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
int af
AF_INET, AF_INET6, or AF_UNIX.
Definition socket.h:78
int type
SOCK_STREAM, SOCK_DGRAM, etc.
Definition socket.h:79
Holds information necessary for binding or connecting to a socket.
Definition socket.h:63
void fr_strerror_clear(void)
Clears all pending messages from the talloc pools.
Definition strerror.c:576
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition strerror.h:64
#define fr_strerror_printf_push(_fmt,...)
Add a message to an existing stack of messages at the tail.
Definition strerror.h:84
#define fr_strerror_const(_msg)
Definition strerror.h:223
#define fr_box_ipaddr(_val)
Definition value.h:317