Files
John Ericson 6563dcb6b1 unix: allow connectat(2) to name the peer socket by descriptor
Accept an empty `sun_path` when `fd` is not `AT_FDCWD`: the descriptor
then names the peer unix socket directly, instead of being the starting
directory for a pathname lookup.  The held file reference keeps the peer
PCB stable, playing the role `unp_vp_mtxpool` plays in the pathname
path.

The descriptor must carry `CAP_CONNECTAT` and refer to an `AF_UNIX`
socket (`EPROTOTYPE` otherwise, `ENOTSOCK` for non-sockets).  As with a
pathname, a stream/seqpacket peer must be listening.  No filesystem
permission or MAC vnode check applies on this path: possession of the
descriptor is the authorization, as with descriptor passing.

Note this makes it possible to connect a datagram socket to an unbound
peer, which no pathname could previously name.

`connect(2)` and the implicit-connect send path pass `AT_FDCWD` and
still reject an empty path with `EINVAL`.

The `unp_sun_path()` call is hoisted out of `unp_connectat()` because the
early exit conditions for the two system calls (`connect(2)` and
`connectat(2)`) are slightly different.

Additionally, support `/dev/fd/<N>`. In a world with `connectat(2)`,
this is largely overkill, but this also allows me to add support for
direct peer connections with plain `connect(2)`. I think that is a wise
choice because this will allow me to propose this functionality for
Linux too without a new system call (saving that conversation for
later). Ultimately, I want to see multiple operating systems support
this to foster broader userland adoption, which should benefit everyone
including FreeBSD --- it's nicer if more 3rd party in addition to 1st
party software uses the new kernel functionality. Therefore, I hope this
additional feature is also acceptable.

Signed-off-by: John Ericson <[email protected]>
Assisted-by: Claude Code (Claude Opus 4.8 and Fable 5)

Reviewed by:	markj
MFC after:	2 months
Differential Revision:	https://reviews.freebsd.org/D58405
2026-08-10 17:31:22 +00:00

157 lines
4.0 KiB
Plaintext

.\" Copyright (c) 2013 The FreeBSD Foundation
.\"
.\" This documentation was written by Pawel Jakub Dawidek under sponsorship from
.\" the FreeBSD Foundation.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd July 25, 2026
.Dt CONNECTAT 2
.Os
.Sh NAME
.Nm connectat
.Nd initiate a connection on a socket
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/types.h
.In sys/socket.h
.Pp
.In fcntl.h
.Ft int
.Fn connectat "int fd" "int s" "const struct sockaddr *name" "socklen_t namelen"
.Sh DESCRIPTION
The
.Fn connectat
system call initiates a connection on the socket
.Fa s .
When passed the special value
.Dv AT_FDCWD
in the
.Fa fd
parameter, the behavior is identical to a call to
.Xr connect 2 .
Otherwise,
.Fn connectat
works like the
.Xr connect 2
system call with two exceptions:
.Pp
.Bl -enum -offset indent -compact
.It
It is limited to sockets in the PF_LOCAL domain.
.Pp
.It
If the file path stored in the
.Fa sun_path
field of the sockaddr_un structure is a relative path, it is located relative
to the directory associated with the file descriptor
.Fa fd .
.Pp
.It
If the
.Fa sun_path
field is empty, that is,
.Fa namelen
equals
.Li offsetof(struct sockaddr_un, sun_path) ,
then
.Fa fd
does not resolve a path but instead names the peer socket directly.
.Pp
The descriptor may be the peer socket itself, or a descriptor for a file, such
as an
.Dv O_PATH
handle opened with
.Xr open 2 .
In the latter case, the
.Dv O_PATH
file description may point either to a traditional socket file bound to the
peer, or to a
.Pa /dev/fd/N
node.
The
.Pa /dev/fd/N
node must name the socket descriptor itself; the named descriptor is resolved a
single level and not chased further.
With a standard
.Xr fdescfs 5
mount this means a node naming a traditional socket file, or another
.Pa /dev/fd/N
node, does not resolve to a peer.
.Pp
In all cases,
.Fa fd
must carry the
.Dv CAP_CONNECTAT
capability right, and the target of a
.Pa /dev/fd/N
node must also carry that right; see
.Xr unix 4
for the details of this form.
.El
.Sh RETURN VALUES
.Rv -std connectat
.Sh ERRORS
The
.Fn connectat
system call may fail with the same errors as the
.Xr connect 2
system call or with the following errors:
.Bl -tag -width Er
.It Bq Er EBADF
The
.Fa sun_path
field does not specify an absolute path and the
.Fa fd
argument is neither
.Dv AT_FDCWD
nor a valid file descriptor.
.It Bq Er ENOTDIR
The
.Fa sun_path
field is not an absolute path and
.Fa fd
is neither
.Dv AT_FDCWD
nor a file descriptor associated with a directory.
.It Bq Er EINVAL
The
.Fa sun_path
field is empty but
.Fa fd
is
.Dv AT_FDCWD ,
so no descriptor names the peer.
.El
.Sh SEE ALSO
.Xr bindat 2 ,
.Xr connect 2 ,
.Xr socket 2 ,
.Xr unix 4
.Sh AUTHORS
The
.Nm
was developed by
.An Pawel Jakub Dawidek Aq Mt [email protected]
under sponsorship from the FreeBSD Foundation.