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: c62780bd899859500431d58b6408ca9e7f39976e $
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 * The bio must be open in order to shut it down.
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_OPEN);
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 socklen_t salen;
735 struct sockaddr_storage salocal;
736
737 fr_assert((my->info.socket.af == AF_INET) || (my->info.socket.af == AF_INET6));
738
739#ifdef HAVE_CAPABILITY_H
740 /*
741 * If we're binding to a special port as non-root, then
742 * check capabilities. If we're root, we already have
743 * equivalent capabilities so we don't need to check.
744 */
745 if ((my->info.socket.inet.src_port < 1024) && (geteuid() != 0)) {
746 (void)fr_cap_enable(CAP_NET_BIND_SERVICE, CAP_EFFECTIVE);
747 }
748#endif
749
750 if (fr_bio_fd_socket_bind_to_device(my, cfg) < 0) return -1;
751
752 /*
753 * Bind to the IP + interface.
754 */
755 if (fr_ipaddr_to_sockaddr(&salocal, &salen, &my->info.socket.inet.src_ipaddr, my->info.socket.inet.src_port) < 0) return -1;
756
757 /*
758 * If we have a fixed source port, just use that.
759 */
760 if (my->info.cfg->src_port || !my->info.cfg->src_port_start) {
761 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) < 0) {
762 fr_strerror_printf("Failed binding to socket: %s", fr_syserror(errno));
763 return -1;
764 }
765 } else {
766 uint16_t src_port, current, range;
767 struct sockaddr_in *sin = (struct sockaddr_in *) &salocal;
768
769 fr_assert(my->info.cfg->src_port_start);
770 fr_assert(my->info.cfg->src_port_end);
771 fr_assert(my->info.cfg->src_port_end >= my->info.cfg->src_port_start);
772
773 range = my->info.cfg->src_port_end - my->info.cfg->src_port_start;
774 src_port = fr_rand() % range;
775
776 /*
777 * We only care about sin_port, which is in the same location for sockaddr_in and sockaddr_in6.
778 */
779 fr_assert(sin->sin_port == 0);
780
781 sin->sin_port = htons(my->info.cfg->src_port_start + src_port);
782
783 /*
784 * We've picked a random port in what is hopefully a large range. If that works, we're
785 * done.
786 */
787 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
788
789 /*
790 * Hunt & peck. Which is horrible.
791 */
792 current = src_port;
793 while (true) {
794 if (current == range) {
795 current = 0;
796 } else {
797 current++;
798 }
799
800 /*
801 * We've already checked this, so stop.
802 */
803 if (current == src_port) break;
804
805 sin->sin_port = htons(my->info.cfg->src_port_start + current);
806
807 if (bind(my->info.socket.fd, (struct sockaddr *) &salocal, salen) == 0) goto done;
808 }
809
810 /*
811 * The error is a good hint at _why_ we failed to bind.
812 * We expect errno to be EADDRINUSE, anything else is a surprise.
813 */
814 fr_strerror_printf("Failed binding port between 'src_port_start' and 'src_port_end': %s", fr_syserror(errno));
815 return -1;
816 }
817
818 /*
819 * The source IP may have changed, so get the new one.
820 */
821done:
823}
824
825/** Set the name of an FD BIO
826 *
827 */
829{
830 fr_bio_fd_config_t const *cfg = my->info.cfg;
831
832 switch (my->info.type) {
834 return;
835
837 fr_assert(cfg->socket_type == SOCK_DGRAM);
838
839 switch (my->info.socket.af) {
840 case AF_INET:
841 case AF_INET6:
842 my->info.name = fr_asprintf(my, "proto udp local %pV port %u",
843 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
844 my->info.socket.inet.src_port);
845 break;
846
847 case AF_LOCAL:
848 my->info.name = fr_asprintf(my, "proto unix (datagram) filename %s",
849 cfg->path);
850 break;
851
852 default:
853 fr_assert(0);
854 my->info.name = "??? invalid BIO ???";
855 break;
856 }
857 break;
858
860 switch (my->info.socket.af) {
861 case AF_INET:
862 case AF_INET6:
863 my->info.name = fr_asprintf(my, "proto %s local %pV port %u remote %pV port %u",
864 (cfg->socket_type == SOCK_DGRAM) ? "udp" : "tcp",
865 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
866 my->info.socket.inet.src_port,
867 fr_box_ipaddr(my->info.socket.inet.dst_ipaddr),
868 my->info.socket.inet.dst_port);
869 break;
870
871 case AF_LOCAL:
872 my->info.name = fr_asprintf(my, "proto unix %sfilename %s",
873 (cfg->socket_type == SOCK_DGRAM) ? "(datagram) " : "",
874 cfg->path);
875 break;
876
877 case AF_FILE_BIO:
878 fr_assert(cfg->socket_type == SOCK_STREAM);
879
880 if (cfg->flags == O_RDONLY) {
881 my->info.name = fr_asprintf(my, "proto file (read-only) filename %s ",
882 cfg->filename);
883
884 } else if (cfg->flags == O_WRONLY) {
885 my->info.name = fr_asprintf(my, "proto file (write-only) filename %s",
886 cfg->filename);
887 } else {
888 my->info.name = fr_asprintf(my, "proto file (read-write) filename %s",
889 cfg->filename);
890 }
891 break;
892
893 default:
894 fr_assert(0);
895 my->info.name = "??? invalid BIO ???";
896 break;
897 }
898 break;
899
900 case FR_BIO_FD_LISTEN:
901 switch (my->info.socket.af) {
902 case AF_INET:
903 case AF_INET6:
904 my->info.name = fr_asprintf(my, "proto %s local %pV port %u",
905 (cfg->socket_type == SOCK_DGRAM) ? "udp" : "tcp",
906 fr_box_ipaddr(my->info.socket.inet.src_ipaddr),
907 my->info.socket.inet.src_port);
908 break;
909
910 case AF_LOCAL:
911 my->info.name = fr_asprintf(my, "proto unix filename %s",
912 cfg->path);
913 break;
914
915 default:
916 fr_assert(0);
917 my->info.name = "??? invalid BIO ???";
918 break;
919 }
920 break;
921 }
922}
923
924/** Checks the configuration without modifying anything.
925 *
926 */
928{
929 /*
930 * Unix sockets and files are OK.
931 */
932 if (cfg->path || cfg->filename) return 0;
933
934 /*
935 * Sanitize the IP addresses.
936 *
937 */
938 switch (cfg->type) {
940 fr_strerror_const("No connection type was specified");
941 return -1;
942
944 /*
945 * Ensure that we have a destination address.
946 */
947 if (cfg->dst_ipaddr.af == AF_UNSPEC) {
948 fr_strerror_const("No destination IP address was specified");
949 return -1;
950 }
951
952 if (!cfg->dst_port) {
953 fr_strerror_const("No destination port was specified");
954 return -1;
955 }
956
957 /*
958 * The source IP has to be the same address family as the destination IP.
959 */
960 if ((cfg->src_ipaddr.af != AF_UNSPEC) && (cfg->src_ipaddr.af != cfg->dst_ipaddr.af)) {
961 fr_strerror_printf("Source and destination IP addresses are not from the same IP address family");
962 return -1;
963 }
964 break;
965
966 case FR_BIO_FD_LISTEN:
967 if (!cfg->src_port) {
968 fr_strerror_const("No source port was specified");
969 return -1;
970 }
972
973 /*
974 * Unconnected sockets can use a source port, but don't need one.
975 */
977 if (cfg->path && cfg->filename) {
978 fr_strerror_const("Unconnected sockets cannot be used with Unix sockets or files");
979 return -1;
980 }
981
982 if (cfg->src_ipaddr.af == AF_UNSPEC) {
983 fr_strerror_const("No source IP address was specified");
984 return -1;
985 }
986 break;
987 }
988
989 return 0;
990}
991
992/** Opens a socket and updates sock->fd
993 *
994 * If the socket is asynchronous, it also calls connect()
995 */
997{
998 int fd;
999 int rcode;
1000 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1001
1003
1004 my->info = (fr_bio_fd_info_t) {
1005 .socket = {
1006 .type = cfg->socket_type,
1007 },
1008 .cfg = cfg,
1009 };
1010
1011 if (!cfg->path && !cfg->filename) {
1012 int protocol;
1013
1014 my->info.socket.af = cfg->src_ipaddr.af;
1015 my->info.socket.inet.src_ipaddr = cfg->src_ipaddr;
1016 my->info.socket.inet.dst_ipaddr = cfg->dst_ipaddr;
1017 my->info.socket.inet.src_port = cfg->src_port;
1018 my->info.socket.inet.dst_port = cfg->dst_port;
1019
1020 if (fr_bio_fd_check_config(cfg) < 0) return -1;
1021
1022 /*
1023 * Sanitize the IP addresses.
1024 *
1025 */
1026 switch (cfg->type) {
1027 case FR_BIO_FD_INVALID:
1028 return -1;
1029
1031 /*
1032 * No source specified, just bootstrap it from the destination.
1033 */
1034 if (my->info.socket.inet.src_ipaddr.af == AF_UNSPEC) {
1035 my->info.socket.inet.src_ipaddr = (fr_ipaddr_t) {
1036 .af = my->info.socket.inet.dst_ipaddr.af,
1037 .prefix = (my->info.socket.inet.dst_ipaddr.af == AF_INET) ? 32 : 128,
1038 };
1039
1040 /*
1041 * Set the main socket AF too.
1042 */
1043 my->info.socket.af = my->info.socket.inet.dst_ipaddr.af;
1044 }
1045
1046 /*
1047 * The source IP has to be the same address family as the destination IP.
1048 */
1049 if (my->info.socket.inet.src_ipaddr.af != my->info.socket.inet.dst_ipaddr.af) {
1050 fr_strerror_const("Source and destination IP addresses are not from the same IP address family");
1051 return -1;
1052 }
1053
1054#ifdef SO_REUSEPORT
1055 /*
1056 * Connected UDP sockets really only matter when writing packets. They allow the
1057 * system to use write() instead of sendto().
1058 *
1059 * However for reading packets, the kernel does NOT check the source IP.
1060 * Instead, it just delivers any packet with the correct dst IP/port. Even
1061 * worse, if multiple sockets re-use the same dst IP/port, packets are delivered
1062 * to a _random_ socket.
1063 *
1064 * As a result, we cannot use wildcard sockets with SO_REUSEPORT, and UDP.
1065 */
1066 if (cfg->reuse_port && (cfg->socket_type == SOCK_DGRAM) &&
1067 fr_ipaddr_is_inaddr_any(&my->info.socket.inet.dst_ipaddr)) { /* checks AF, so we're OK */
1068 fr_strerror_const("Cannot set 'reuse_port' for connected UDP sockets with wildcard IP");
1069 return -1;
1070 }
1071#endif
1072
1073 break;
1074
1076 case FR_BIO_FD_LISTEN:
1077 fr_assert(my->info.socket.inet.src_ipaddr.af != AF_UNSPEC);
1078 break;
1079 }
1080
1081 if (cfg->socket_type == SOCK_STREAM) {
1082 protocol = IPPROTO_TCP;
1083 } else {
1084 protocol = IPPROTO_UDP;
1085 }
1086
1087 if (cfg->interface) {
1088 my->info.socket.inet.ifindex = if_nametoindex(cfg->interface);
1089
1090 if (!my->info.socket.inet.ifindex) {
1091 fr_strerror_printf_push("Failed finding interface %s: %s", cfg->interface, fr_syserror(errno));
1092 return -1;
1093 }
1094 }
1095
1096 fd = socket(my->info.socket.af, my->info.socket.type, protocol);
1097 if (fd < 0) {
1098 fr_strerror_printf("Failed opening socket: %s", fr_syserror(errno));
1099 return fr_bio_error(GENERIC);
1100 }
1101
1102 } else if (cfg->path) {
1103 my->info.socket.af = AF_LOCAL;
1104 my->info.socket.type = SOCK_STREAM;
1105 my->info.socket.unix.path = cfg->path;
1106
1107 fd = socket(my->info.socket.af, my->info.socket.type, 0);
1108 if (fd < 0) {
1109 fr_strerror_printf("Failed opening domain socket %s: %s", cfg->path, fr_syserror(errno));
1110 return fr_bio_error(GENERIC);
1111 }
1112
1113 } else {
1114 if (cfg->type != FR_BIO_FD_CONNECTED) {
1115 fr_strerror_printf("Can only use connected sockets for file IO");
1116 return -1;
1117 }
1118
1119 /*
1120 * Filenames overload the #fr_socket_t for now.
1121 */
1122 my->info.socket.af = AF_FILE_BIO;
1123 my->info.socket.type = SOCK_STREAM;
1124 my->info.socket.unix.path = cfg->filename;
1125
1126 /*
1127 * Allow hacks for stdout and stderr
1128 */
1129 if (strcmp(cfg->filename, "/dev/stdout") == 0) {
1130 if (cfg->flags != O_WRONLY) {
1131 fail_dev:
1132 fr_strerror_printf("Cannot read from %s", cfg->filename);
1133 return -1;
1134 }
1135
1136 fd = dup(STDOUT_FILENO);
1137
1138 } else if (strcmp(cfg->filename, "/dev/stderr") == 0) {
1139 if (cfg->flags != O_WRONLY) goto fail_dev;
1140
1141 fd = dup(STDERR_FILENO);
1142
1143 } else if (strcmp(cfg->filename, "/dev/stdin") == 0) {
1144 if (cfg->flags != O_RDONLY) {
1145 fr_strerror_printf("Cannot write to %s", cfg->filename);
1146 return -1;
1147 }
1148
1149 fd = dup(STDIN_FILENO);
1150
1151 } else {
1152 /*
1153 * Minor hacks so that we have only _one_ source of open / mkdir
1154 */
1155 my->info.socket.fd = -1;
1156
1157 fd = fr_bio_fd_reopen(bio);
1158 }
1159 if (fd < 0) {
1160 fr_strerror_printf("Failed opening file %s: %s", cfg->filename, fr_syserror(errno));
1161 return fr_bio_error(GENERIC);
1162 }
1163 }
1164
1165 /*
1166 * Set it to be non-blocking if required.
1167 */
1168 if (cfg->async && (fr_nonblock(fd) < 0)) {
1169 fr_strerror_printf("Failed setting O_NONBLOCK: %s", fr_syserror(errno));
1170 rcode = fr_bio_error(GENERIC);
1171
1172 fail:
1173 my->info.socket = (fr_socket_t) {
1174 .fd = -1,
1175 };
1176 my->info.state = FR_BIO_FD_STATE_CLOSED;
1177 my->info.cfg = NULL;
1178 close(fd);
1179 return rcode;
1180 }
1181
1182#ifdef FD_CLOEXEC
1183 /*
1184 * We don't want child processes inheriting these file descriptors.
1185 */
1186 rcode = fcntl(fd, F_GETFD);
1187 if (rcode >= 0) {
1188 if (fcntl(fd, F_SETFD, rcode | FD_CLOEXEC) < 0) {
1189 fr_strerror_printf("Failed setting FD_CLOEXEC: %s", fr_syserror(errno));
1190 goto fail;
1191 }
1192 }
1193#endif
1194
1195 /*
1196 * Initialize the bio information before calling the various setup functions.
1197 */
1198 my->info.state = FR_BIO_FD_STATE_CONNECTING;
1199
1200 /*
1201 * Set the FD so that the subsequent calls can use it.
1202 */
1203 my->info.socket.fd = fd;
1204
1205 /*
1206 * Set the type, too.
1207 */
1208 my->info.type = cfg->type;
1209
1210 /*
1211 * Do sanity checks, bootstrap common socket options, bind to the socket, and initialize the read
1212 * / write functions.
1213 */
1214 switch (cfg->type) {
1215 case FR_BIO_FD_INVALID:
1216 return -1;
1217
1218 /*
1219 * Unconnected UDP or datagram AF_LOCAL server sockets.
1220 */
1222 if (my->info.socket.type != SOCK_DGRAM) {
1223 fr_strerror_const("Failed configuring socket: unconnected sockets must be UDP");
1224 return -1;
1225 }
1226
1227 switch (my->info.socket.af) {
1228 case AF_INET:
1229 case AF_INET6:
1230 if ((rcode = fr_bio_fd_common_udp(fd, &my->info.socket, cfg)) < 0) goto fail;
1231 break;
1232
1233 case AF_LOCAL:
1234 if ((rcode = fr_bio_fd_common_datagram(fd, &my->info.socket, cfg)) < 0) goto fail;
1235 break;
1236
1237 case AF_FILE_BIO:
1238 fr_strerror_const("Filenames must use the connected API");
1239 goto fail;
1240
1241 default:
1242 fr_strerror_const("Unsupported address family for unconnected sockets");
1243 goto fail;
1244 }
1245
1246 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1247
1248 if ((rcode = fr_bio_fd_init_common(my)) < 0) goto fail;
1249 break;
1250
1251 /*
1252 * A connected client: UDP, TCP, AF_LOCAL, or AF_FILE_BIO
1253 */
1255 if (my->info.socket.type == SOCK_DGRAM) {
1256 switch (my->info.socket.af) {
1257 case AF_INET:
1258 case AF_INET6:
1259 if ((rcode = fr_bio_fd_common_udp(fd, &my->info.socket, cfg)) < 0) goto fail;
1260 break;
1261 default:
1262 if ((rcode = fr_bio_fd_common_datagram(fd, &my->info.socket, cfg)) < 0) goto fail;
1263 break;
1264 }
1265
1266 } else if ((my->info.socket.af == AF_INET) || (my->info.socket.af == AF_INET6)) {
1267 rcode = fr_bio_fd_common_tcp(fd, &my->info.socket, cfg);
1268 if (rcode < 0) goto fail;
1269 }
1270
1271 switch (my->info.socket.af) {
1272 case AF_LOCAL:
1273 if ((rcode = fr_bio_fd_socket_bind_unix(my, cfg)) < 0) goto fail;
1274 break;
1275
1276 case AF_FILE_BIO:
1277 break;
1278
1279 case AF_INET:
1280 case AF_INET6:
1281 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1282 break;
1283
1284 default:
1285 return -1;
1286 }
1287
1288 if ((rcode = fr_bio_fd_init_connected(my)) < 0) goto fail;
1289 break;
1290
1291 /*
1292 * Server socket which listens for new stream connections
1293 */
1294 case FR_BIO_FD_LISTEN:
1295 if ((my->info.socket.type == SOCK_DGRAM) && !cfg->reuse_port) {
1296 fr_strerror_const("reuseport must be set for datagram sockets");
1297 rcode = -1;
1298 goto fail;
1299 }
1300
1301 switch (my->info.socket.af) {
1302 case AF_INET:
1303 if ((rcode = fr_bio_fd_server_ipv4(fd, &my->info.socket, cfg)) < 0) goto fail;
1304
1305 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1306 break;
1307
1308 case AF_INET6:
1309 if ((rcode = fr_bio_fd_server_ipv6(fd, &my->info.socket, cfg)) < 0) goto fail;
1310
1311 if ((rcode = fr_bio_fd_socket_bind(my, cfg)) < 0) goto fail;
1312 break;
1313
1314 case AF_LOCAL:
1315 if ((rcode = fr_bio_fd_socket_bind_unix(my, cfg)) < 0) goto fail;
1316 break;
1317
1318 default:
1319 fr_strerror_const("Unsupported address family for accept() socket");
1320 rcode = -1;
1321 goto fail;
1322 }
1323
1324 if ((rcode = fr_bio_fd_init_listen(my)) < 0) goto fail;
1325 break;
1326 }
1327
1328 /*
1329 * Set the name of the BIO.
1330 */
1332
1333 return 0;
1334}
1335
1336/** Reopen a file BIO
1337 *
1338 * e.g. for log files.
1339 */
1341{
1342 fr_bio_fd_t *my = talloc_get_type_abort(bio, fr_bio_fd_t);
1343 fr_bio_fd_config_t const *cfg = my->info.cfg;
1344 int fd, flags;
1345
1346 if (my->info.socket.af != AF_FILE_BIO) {
1347 fr_strerror_const("Cannot reopen a non-file BIO");
1348 return -1;
1349 }
1350
1351 /*
1352 * Create it if necessary.
1353 */
1354 flags = cfg->flags;
1355 if (flags != O_RDONLY) flags |= O_CREAT;
1356
1357 if (!cfg->mkdir) {
1358 /*
1359 * Client BIOs writing to a file, and therefore need to create it.
1360 */
1361 do_open:
1362 fd = open(cfg->filename, flags, cfg->perm);
1363 if (fd < 0) {
1364 failed_open:
1365 fr_strerror_printf("Failed opening file %s: %s", cfg->filename, fr_syserror(errno));
1366 return -1;
1367 }
1368
1369 } else {
1370 /*
1371 * We make the parent directory if told to, AND if there's a '/' in the path.
1372 */
1373 char *p = strrchr(cfg->filename, '/');
1374 int dir_fd;
1375
1376 if (!p) goto do_open;
1377
1378 if (fr_mkdir(&dir_fd, cfg->filename, (size_t) (p - cfg->filename), cfg->perm, fr_mkdir_chown,
1379 &(fr_mkdir_chown_t) {
1380 .uid = cfg->uid,
1381 .gid = cfg->gid,
1382 }) < 0) {
1383 return -1;
1384 }
1385
1386 fd = openat(dir_fd, p + 1, flags, cfg->perm);
1387 if (fd < 0) {
1388 close(dir_fd);
1389 goto failed_open;
1390 }
1391 }
1392
1393 /*
1394 * We're boot-strapping, just set the new FD and return.
1395 */
1396 if (my->info.socket.fd < 0) {
1397 return fd;
1398 }
1399
1400 /*
1401 * Replace the FD rather than swapping it out with a new one. This is potentially more
1402 * thread-safe.
1403 */
1404 if (dup2(fd, my->info.socket.fd) < 0) {
1405 close(fd);
1406 fr_strerror_printf("Failed reopening file - %s", fr_syserror(errno));
1407 return -1;
1408 }
1409
1410 close(fd);
1411 return my->info.socket.fd;
1412}
#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
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:99
bool mkdir
make intermediate directories
Definition fd.h:106
@ FR_BIO_FD_STATE_CLOSED
Definition fd.h:58
@ FR_BIO_FD_STATE_CONNECTING
Definition fd.h:60
@ FR_BIO_FD_STATE_OPEN
error states must be before this
Definition fd.h:59
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:102
uint32_t send_buff
How big the kernel's send buffer should be.
Definition fd.h:100
char const * filename
for files
Definition fd.h:108
uid_t uid
who owns the socket
Definition fd.h:104
gid_t gid
who owns the socket
Definition fd.h:105
char const * interface
for binding to an interface
Definition fd.h:97
mode_t perm
permissions for domain sockets
Definition fd.h:103
#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:112
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:113
bool async
is it async
Definition fd.h:111
int flags
O_RDONLY, etc.
Definition fd.h:109
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:128
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:1340
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:828
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:996
int fr_bio_fd_check_config(fr_bio_fd_config_t const *cfg)
Checks the configuration without modifying anything.
Definition fd_open.c:927
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
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
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.
talloc_free(reap)
unsigned short uint16_t
unsigned int mode_t
int fr_nonblock(UNUSED int fd)
Definition misc.c:294
char * fr_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Special version of asprintf which implements custom format specifiers.
Definition print.c:874
#define fr_assert(_expr)
Definition rad_assert.h:38
static rc_request_t * current
static bool done
Definition radclient.c:81
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
close(uq->fd)
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:316