1*d32639f6Sjmc.\" $OpenBSD: socreate.9,v 1.11 2022/09/11 06:38:11 jmc Exp $ 277400e24Sclaudio.\" 377400e24Sclaudio.\" Copyright (c) 2006 Robert N. M. Watson 477400e24Sclaudio.\" All rights reserved. 577400e24Sclaudio.\" 677400e24Sclaudio.\" Redistribution and use in source and binary forms, with or without 777400e24Sclaudio.\" modification, are permitted provided that the following conditions 877400e24Sclaudio.\" are met: 977400e24Sclaudio.\" 1. Redistributions of source code must retain the above copyright 1077400e24Sclaudio.\" notice, this list of conditions and the following disclaimer. 1177400e24Sclaudio.\" 2. Redistributions in binary form must reproduce the above copyright 1277400e24Sclaudio.\" notice, this list of conditions and the following disclaimer in the 1377400e24Sclaudio.\" documentation and/or other materials provided with the distribution. 1477400e24Sclaudio.\" 1577400e24Sclaudio.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1677400e24Sclaudio.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1777400e24Sclaudio.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1877400e24Sclaudio.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1977400e24Sclaudio.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2077400e24Sclaudio.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2177400e24Sclaudio.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2277400e24Sclaudio.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2377400e24Sclaudio.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2477400e24Sclaudio.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2577400e24Sclaudio.\" SUCH DAMAGE. 2677400e24Sclaudio.\" 2777400e24Sclaudio.\" $FreeBSD: src/share/man/man9/socket.9,v 1.2 2006/12/16 10:32:10 rwatson Exp $ 2877400e24Sclaudio.\" 29*d32639f6Sjmc.Dd $Mdocdate: September 11 2022 $ 30fa1f3413Sjmc.Dt SOCREATE 9 3177400e24Sclaudio.Os 3277400e24Sclaudio.Sh NAME 33fa1f3413Sjmc.Nm sobind , 34fa1f3413Sjmc.Nm soclose , 35fa1f3413Sjmc.Nm soconnect , 36fa1f3413Sjmc.Nm socreate , 37fa1f3413Sjmc.Nm soreceive , 3887473315Sphessler.Nm so_upcall , 39fa1f3413Sjmc.Nm sosetopt , 40fa1f3413Sjmc.Nm sogetopt , 41fa1f3413Sjmc.Nm sosend , 42fa1f3413Sjmc.Nm soshutdown 4377400e24Sclaudio.Nd kernel socket interface 4477400e24Sclaudio.Sh SYNOPSIS 4577400e24Sclaudio.In sys/socket.h 4677400e24Sclaudio.In sys/socketvar.h 4777400e24Sclaudio.Ft int 4877400e24Sclaudio.Fn sobind "struct socket *so" "struct mbuf *nam" "struct proc *p" 4977400e24Sclaudio.Ft void 504a4dc3eaSmpi.Fn soclose "struct socket *so" "int flags" 5177400e24Sclaudio.Ft int 5277400e24Sclaudio.Fn soconnect "struct socket *so" "struct mbuf *nam" 5377400e24Sclaudio.Ft int 5477400e24Sclaudio.Fo socreate 5577400e24Sclaudio.Fa "int dom" "struct socket **aso" "int type" "int proto" 5677400e24Sclaudio.Fc 5777400e24Sclaudio.Ft int 5877400e24Sclaudio.Fo soreceive 5977400e24Sclaudio.Fa "struct socket *so" "struct mbuf **paddr" "struct uio *uio" 6077400e24Sclaudio.Fa "struct mbuf **mp0" "struct mbuf **controlp" "int *flagsp" 6108177ce7Sdlg.Fa "socklen_t controllen" 6277400e24Sclaudio.Fc 6387473315Sphessler.Ft void 6487473315Sphessler.Fn (*so_upcall) "struct socket *so" "caddr_t arg" "int waitflag" 6577400e24Sclaudio.Ft int 66ce59c7a9Smpi.Fn sosetopt "struct socket *so" "int level" "int optname" "struct mbuf *m" 6777400e24Sclaudio.Ft int 68ce59c7a9Smpi.Fn sogetopt "struct socket *so" "int level" "int optname" "struct mbuf *m" 6977400e24Sclaudio.Ft int 7077400e24Sclaudio.Fo sosend 7177400e24Sclaudio.Fa "struct socket *so" "struct mbuf *addr" "struct uio *uio" 7277400e24Sclaudio.Fa "struct mbuf *top" "struct mbuf *control" "int flags" 7377400e24Sclaudio.Fc 7477400e24Sclaudio.Ft int 7577400e24Sclaudio.Fn soshutdown "struct socket *so" "int how" 7677400e24Sclaudio.Sh DESCRIPTION 7777400e24SclaudioThe kernel socket 7877400e24Sclaudioprogramming interface permits in-kernel consumers to interact with 7977400e24Sclaudiolocal and network socket objects in a manner similar to that permitted using 8077400e24Sclaudiothe 8177400e24Sclaudio.Xr socket 2 8277400e24Sclaudiouser API. 8377400e24SclaudioThese interfaces are appropriate for use by distributed file systems and 8477400e24Sclaudioother network-aware kernel services. 8577400e24SclaudioWhile the user API operates on file descriptors, the kernel interfaces 8677400e24Sclaudiooperate directly on 87*d32639f6Sjmc.Vt struct socket 8877400e24Sclaudiopointers. 8977400e24Sclaudio.Pp 9077400e24SclaudioExcept where otherwise indicated, 9177400e24Sclaudio.Nm 9277400e24Sclaudiofunctions may sleep. 9377400e24Sclaudio.Ss Creating and Destroying Sockets 9477400e24SclaudioA new socket may be created using 9577400e24Sclaudio.Fn socreate . 9677400e24SclaudioAs with 9777400e24Sclaudio.Xr socket 2 , 9877400e24Sclaudioarguments specify the requested domain, type, and protocol via 9977400e24Sclaudio.Fa dom , type , 10077400e24Sclaudioand 10177400e24Sclaudio.Fa proto . 10277400e24SclaudioThe socket is returned via 10377400e24Sclaudio.Fa aso 10477400e24Sclaudioon success. 10577400e24Sclaudio.Em Warning : 10677400e24Sclaudioauthorization of the socket creation operation will be performed 10777400e24Sclaudiousing 10877400e24Sclaudio.Dv curproc 10977400e24Sclaudiofor some protocols (such as raw sockets). 11077400e24Sclaudio.Pp 11177400e24SclaudioSockets may be closed and freed using 11277400e24Sclaudio.Fn soclose , 11377400e24Sclaudiowhich has similar semantics to 11477400e24Sclaudio.Xr close 2 . 11577400e24Sclaudio.Ss Connections and Addresses 11677400e24SclaudioThe 11777400e24Sclaudio.Fn sobind 11877400e24Sclaudiofunction is equivalent to the 11977400e24Sclaudio.Xr bind 2 12077400e24Sclaudiosystem call, and binds the socket 12177400e24Sclaudio.Fa so 12277400e24Sclaudioto the address 12377400e24Sclaudio.Fa nam . 12477400e24SclaudioThe operation would be authorized using the credential on process 12577400e24Sclaudio.Fa p . 12677400e24Sclaudio.Pp 12777400e24SclaudioThe 12877400e24Sclaudio.Fn soconnect 12977400e24Sclaudiofunction is equivalent to the 13077400e24Sclaudio.Xr connect 2 13177400e24Sclaudiosystem call, and initiates a connection on the socket 13277400e24Sclaudio.Fa so 13377400e24Sclaudioto the address 13477400e24Sclaudio.Fa nam . 13577400e24SclaudioThe operation will be authorized using the credential on 13677400e24Sclaudio.Dv curproc . 13777400e24SclaudioUnlike the user system call, 13877400e24Sclaudio.Fn soconnect 13977400e24Sclaudioreturns immediately; the caller may 14077400e24Sclaudio.Xr tsleep 9 14177400e24Sclaudioon 14290211f92Snaddy.Fa so->so_timeo 14377400e24Sclaudioand wait for the 14477400e24Sclaudio.Dv SS_ISCONNECTING 14577400e24Sclaudioflag to clear or 14690211f92Snaddy.Fa so->so_error 14777400e24Sclaudioto become non-zero. 14877400e24SclaudioIf 14977400e24Sclaudio.Fn soconnect 15077400e24Sclaudiofails, the caller must manually clear the 15177400e24Sclaudio.Dv SS_ISCONNECTING 15277400e24Sclaudioflag. 15377400e24Sclaudio.Pp 15477400e24SclaudioThe 15577400e24Sclaudio.Fn soshutdown 15677400e24Sclaudiofunction is equivalent to the 15777400e24Sclaudio.Xr shutdown 2 15877400e24Sclaudiosystem call, and causes part or all of a connection on a socket to be closed 15977400e24Sclaudiodown. 16077400e24Sclaudio.Ss Socket Options 16177400e24SclaudioThe 16277400e24Sclaudio.Fn sogetopt 16377400e24Sclaudiofunction is equivalent to the 16477400e24Sclaudio.Xr getsockopt 2 16577400e24Sclaudiosystem call, and retrieves a socket option on socket 16677400e24Sclaudio.Fa so . 16777400e24SclaudioThe 16877400e24Sclaudio.Fn sosetopt 16977400e24Sclaudiofunction is equivalent to the 17077400e24Sclaudio.Xr setsockopt 2 17177400e24Sclaudiosystem call, and sets a socket option on socket 17277400e24Sclaudio.Fa so . 17377400e24Sclaudio.Pp 17477400e24SclaudioThe next two arguments in both 17577400e24Sclaudio.Fn sogetopt 17677400e24Sclaudioand 17777400e24Sclaudio.Fn sosetopt 17877400e24Sclaudioare 17977400e24Sclaudio.Fa level 18077400e24Sclaudioand 18177400e24Sclaudio.Fa optname 18277400e24Sclaudiodescribing the protocol level and socket option. 183ce59c7a9SmpiThe last argument 184ce59c7a9Smpi.Fa m 1850d81705fSjmcis either a pointer to a prefilled mbuf or a pointer to an mbuf to retrieve 186ce59c7a9Smpidata. 18777400e24Sclaudio.Ss Socket I/O 18877400e24SclaudioThe 18977400e24Sclaudio.Fn soreceive 19077400e24Sclaudiofunction is equivalent to the 19177400e24Sclaudio.Xr recvmsg 2 19277400e24Sclaudiosystem call, and attempts to receive bytes of data from the socket 19377400e24Sclaudio.Fa so , 19477400e24Sclaudiooptionally blocking and awaiting data if none is ready to read. 19577400e24SclaudioData may be retrieved directly to kernel or user memory via the 19677400e24Sclaudio.Fa uio 19777400e24Sclaudioargument, or as an mbuf chain returned to the caller via 19877400e24Sclaudio.Fa mp0 , 19977400e24Sclaudioavoiding a data copy. 200338b571eSdlgIf 20177400e24Sclaudio.Fa mp0 202338b571eSdlgis not 203338b571eSdlg.Dv NULL , 204338b571eSdlg.Fa uio 205338b571eSdlgmust still be passed with uio_resid set to specify the maximum 206338b571eSdlgamount of data to be returned to the caller via an mbuf chain. 20777400e24SclaudioThe caller may optionally retrieve a socket address on a protocol with the 20877400e24Sclaudio.Dv PR_ADDR 20977400e24Sclaudiocapability by providing storage via a 21077400e24Sclaudio.Pf non- Dv NULL 21177400e24Sclaudio.Fa paddr 21277400e24Sclaudioargument. 21308177ce7SdlgThe caller may optionally retrieve up to 21408177ce7Sdlg.Fa controllen 21508177ce7Sdlgbytes of control data in mbufs via a 21677400e24Sclaudio.Pf non- Dv NULL 21777400e24Sclaudio.Fa controlp 21877400e24Sclaudioargument. 21977400e24SclaudioOptional flags may be passed to 22077400e24Sclaudio.Fn soreceive 22177400e24Sclaudiovia a 22277400e24Sclaudio.Pf non- Dv NULL 22377400e24Sclaudio.Fa flagsp 22477400e24Sclaudioargument, and use the same flag name space as the 22577400e24Sclaudio.Xr recvmsg 2 22677400e24Sclaudiosystem call. 22777400e24Sclaudio.Pp 22887473315SphesslerWhen the 22987473315Sphessler.Fn so_upcall 2301e7790adSschwarzefunction pointer is not 2311e7790adSschwarze.Dv NULL , 2321e7790adSschwarzeit is called when 2331e7790adSschwarze.Fn soreceive 23487473315Sphesslermatches an incoming connection. 23587473315Sphessler.Pp 23677400e24SclaudioThe 23777400e24Sclaudio.Fn sosend 23877400e24Sclaudiofunction is equivalent to the 23977400e24Sclaudio.Xr sendmsg 2 24077400e24Sclaudiosystem call, and attempts to send bytes of data via the socket 24177400e24Sclaudio.Fa so , 24277400e24Sclaudiooptionally blocking if data cannot be immediately sent. 24377400e24SclaudioData may be sent directly from kernel or user memory via the 24477400e24Sclaudio.Fa uio 24577400e24Sclaudioargument, or as an mbuf chain via 24677400e24Sclaudio.Fa top , 24777400e24Sclaudioavoiding a data copy. 24877400e24SclaudioOnly one of the 24977400e24Sclaudio.Fa uio 25077400e24Sclaudioor 25177400e24Sclaudio.Fa top 25277400e24Sclaudiopointers may be 25377400e24Sclaudio.Pf non- Dv NULL . 25477400e24SclaudioAn optional destination address may be specified via a 25577400e24Sclaudio.Pf non- Dv NULL 25677400e24Sclaudio.Fa addr 25777400e24Sclaudioargument, which may result in an implicit connect if supported by the 25877400e24Sclaudioprotocol. 25977400e24SclaudioThe caller may optionally send control data mbufs via a 26077400e24Sclaudio.Pf non- Dv NULL 26177400e24Sclaudio.Fa control 26277400e24Sclaudioargument. 26377400e24SclaudioFlags may be passed to 26477400e24Sclaudio.Fn sosend 26577400e24Sclaudiousing the 26677400e24Sclaudio.Fa flags 26777400e24Sclaudioargument, and use the same flag name space as the 26877400e24Sclaudio.Xr sendmsg 2 26977400e24Sclaudiosystem call. 27077400e24Sclaudio.Pp 27177400e24SclaudioKernel callers running in interrupt context, or with a mutex held, will wish to 27277400e24Sclaudiouse non-blocking sockets and pass the 27377400e24Sclaudio.Dv MSG_DONTWAIT 27477400e24Sclaudioflag in order to prevent these functions from sleeping. 27577400e24Sclaudio.Sh SEE ALSO 27677400e24Sclaudio.Xr bind 2 , 27777400e24Sclaudio.Xr close 2 , 27877400e24Sclaudio.Xr connect 2 , 27977400e24Sclaudio.Xr getsockopt 2 , 28077400e24Sclaudio.Xr recv 2 , 28177400e24Sclaudio.Xr send 2 , 28277400e24Sclaudio.Xr setsockopt 2 , 28377400e24Sclaudio.Xr shutdown 2 , 28477400e24Sclaudio.Xr socket 2 , 28577400e24Sclaudio.Xr tsleep 9 28677400e24Sclaudio.Sh HISTORY 28777400e24SclaudioThe 28877400e24Sclaudio.Xr socket 2 28977400e24Sclaudiosystem call appeared in 29077400e24Sclaudio.Bx 4.2 . 29177400e24SclaudioThis manual page was introduced in 29277400e24Sclaudio.Fx 7.0 29377400e24Sclaudioand ported to 29477400e24Sclaudio.Ox 4.5 . 29577400e24Sclaudio.Sh AUTHORS 29677400e24SclaudioThis manual page was written by 29777400e24Sclaudio.An Robert Watson . 29877400e24Sclaudio.Sh BUGS 29977400e24SclaudioThe use of credentials hung from explicitly passed processes, 30077400e24Sclaudioand the credential on 30177400e24Sclaudio.Dv curproc , 30277400e24Sclaudioand the cached credential from socket creation time is inconsistent, and may 30377400e24Sclaudiolead to unexpected behaviour. 30477400e24Sclaudio.Pp 30577400e24SclaudioThe caller may need to manually clear 30677400e24Sclaudio.Dv SS_ISCONNECTING 30777400e24Sclaudioif 30877400e24Sclaudio.Fn soconnect 30977400e24Sclaudioreturns an error. 31077400e24Sclaudio.Pp 31177400e24SclaudioThis manual page does not describe how to register socket upcalls or monitor 31277400e24Sclaudioa socket for readability/writability without using blocking I/O. 313