1.\" $OpenBSD: socreate.9,v 1.10 2018/07/30 12:22:14 mpi Exp $ 2.\" 3.\" Copyright (c) 2006 Robert N. M. Watson 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" $FreeBSD: src/share/man/man9/socket.9,v 1.2 2006/12/16 10:32:10 rwatson Exp $ 28.\" 29.Dd $Mdocdate: July 30 2018 $ 30.Dt SOCREATE 9 31.Os 32.Sh NAME 33.Nm sobind , 34.Nm soclose , 35.Nm soconnect , 36.Nm socreate , 37.Nm soreceive , 38.Nm so_upcall , 39.Nm sosetopt , 40.Nm sogetopt , 41.Nm sosend , 42.Nm soshutdown 43.Nd kernel socket interface 44.Sh SYNOPSIS 45.In sys/socket.h 46.In sys/socketvar.h 47.Ft int 48.Fn sobind "struct socket *so" "struct mbuf *nam" "struct proc *p" 49.Ft void 50.Fn soclose "struct socket *so" "int flags" 51.Ft int 52.Fn soconnect "struct socket *so" "struct mbuf *nam" 53.Ft int 54.Fo socreate 55.Fa "int dom" "struct socket **aso" "int type" "int proto" 56.Fc 57.Ft int 58.Fo soreceive 59.Fa "struct socket *so" "struct mbuf **paddr" "struct uio *uio" 60.Fa "struct mbuf **mp0" "struct mbuf **controlp" "int *flagsp" 61.Fa "socklen_t controllen" 62.Fc 63.Ft void 64.Fn (*so_upcall) "struct socket *so" "caddr_t arg" "int waitflag" 65.Ft int 66.Fn sosetopt "struct socket *so" "int level" "int optname" "struct mbuf *m" 67.Ft int 68.Fn sogetopt "struct socket *so" "int level" "int optname" "struct mbuf *m" 69.Ft int 70.Fo sosend 71.Fa "struct socket *so" "struct mbuf *addr" "struct uio *uio" 72.Fa "struct mbuf *top" "struct mbuf *control" "int flags" 73.Fc 74.Ft int 75.Fn soshutdown "struct socket *so" "int how" 76.Sh DESCRIPTION 77The kernel socket 78programming interface permits in-kernel consumers to interact with 79local and network socket objects in a manner similar to that permitted using 80the 81.Xr socket 2 82user API. 83These interfaces are appropriate for use by distributed file systems and 84other network-aware kernel services. 85While the user API operates on file descriptors, the kernel interfaces 86operate directly on 87.Vt "struct socket" 88pointers. 89.Pp 90Except where otherwise indicated, 91.Nm 92functions may sleep. 93.Ss Creating and Destroying Sockets 94A new socket may be created using 95.Fn socreate . 96As with 97.Xr socket 2 , 98arguments specify the requested domain, type, and protocol via 99.Fa dom , type , 100and 101.Fa proto . 102The socket is returned via 103.Fa aso 104on success. 105.Em Warning : 106authorization of the socket creation operation will be performed 107using 108.Dv curproc 109for some protocols (such as raw sockets). 110.Pp 111Sockets may be closed and freed using 112.Fn soclose , 113which has similar semantics to 114.Xr close 2 . 115.Ss Connections and Addresses 116The 117.Fn sobind 118function is equivalent to the 119.Xr bind 2 120system call, and binds the socket 121.Fa so 122to the address 123.Fa nam . 124The operation would be authorized using the credential on process 125.Fa p . 126.Pp 127The 128.Fn soconnect 129function is equivalent to the 130.Xr connect 2 131system call, and initiates a connection on the socket 132.Fa so 133to the address 134.Fa nam . 135The operation will be authorized using the credential on 136.Dv curproc . 137Unlike the user system call, 138.Fn soconnect 139returns immediately; the caller may 140.Xr tsleep 9 141on 142.Fa so->so_timeo 143and wait for the 144.Dv SS_ISCONNECTING 145flag to clear or 146.Fa so->so_error 147to become non-zero. 148If 149.Fn soconnect 150fails, the caller must manually clear the 151.Dv SS_ISCONNECTING 152flag. 153.Pp 154The 155.Fn soshutdown 156function is equivalent to the 157.Xr shutdown 2 158system call, and causes part or all of a connection on a socket to be closed 159down. 160.Ss Socket Options 161The 162.Fn sogetopt 163function is equivalent to the 164.Xr getsockopt 2 165system call, and retrieves a socket option on socket 166.Fa so . 167The 168.Fn sosetopt 169function is equivalent to the 170.Xr setsockopt 2 171system call, and sets a socket option on socket 172.Fa so . 173.Pp 174The next two arguments in both 175.Fn sogetopt 176and 177.Fn sosetopt 178are 179.Fa level 180and 181.Fa optname 182describing the protocol level and socket option. 183The last argument 184.Fa m 185is either a pointer to a prefilled mbuf or a pointer to an mbuf to retrieve 186data. 187.Ss Socket I/O 188The 189.Fn soreceive 190function is equivalent to the 191.Xr recvmsg 2 192system call, and attempts to receive bytes of data from the socket 193.Fa so , 194optionally blocking and awaiting data if none is ready to read. 195Data may be retrieved directly to kernel or user memory via the 196.Fa uio 197argument, or as an mbuf chain returned to the caller via 198.Fa mp0 , 199avoiding a data copy. 200If 201.Fa mp0 202is not 203.Dv NULL , 204.Fa uio 205must still be passed with uio_resid set to specify the maximum 206amount of data to be returned to the caller via an mbuf chain. 207The caller may optionally retrieve a socket address on a protocol with the 208.Dv PR_ADDR 209capability by providing storage via a 210.Pf non- Dv NULL 211.Fa paddr 212argument. 213The caller may optionally retrieve up to 214.Fa controllen 215bytes of control data in mbufs via a 216.Pf non- Dv NULL 217.Fa controlp 218argument. 219Optional flags may be passed to 220.Fn soreceive 221via a 222.Pf non- Dv NULL 223.Fa flagsp 224argument, and use the same flag name space as the 225.Xr recvmsg 2 226system call. 227.Pp 228When the 229.Fn so_upcall 230function pointer is not 231.Dv NULL , 232it is called when 233.Fn soreceive 234matches an incoming connection. 235.Pp 236The 237.Fn sosend 238function is equivalent to the 239.Xr sendmsg 2 240system call, and attempts to send bytes of data via the socket 241.Fa so , 242optionally blocking if data cannot be immediately sent. 243Data may be sent directly from kernel or user memory via the 244.Fa uio 245argument, or as an mbuf chain via 246.Fa top , 247avoiding a data copy. 248Only one of the 249.Fa uio 250or 251.Fa top 252pointers may be 253.Pf non- Dv NULL . 254An optional destination address may be specified via a 255.Pf non- Dv NULL 256.Fa addr 257argument, which may result in an implicit connect if supported by the 258protocol. 259The caller may optionally send control data mbufs via a 260.Pf non- Dv NULL 261.Fa control 262argument. 263Flags may be passed to 264.Fn sosend 265using the 266.Fa flags 267argument, and use the same flag name space as the 268.Xr sendmsg 2 269system call. 270.Pp 271Kernel callers running in interrupt context, or with a mutex held, will wish to 272use non-blocking sockets and pass the 273.Dv MSG_DONTWAIT 274flag in order to prevent these functions from sleeping. 275.Sh SEE ALSO 276.Xr bind 2 , 277.Xr close 2 , 278.Xr connect 2 , 279.Xr getsockopt 2 , 280.Xr recv 2 , 281.Xr send 2 , 282.Xr setsockopt 2 , 283.Xr shutdown 2 , 284.Xr socket 2 , 285.Xr tsleep 9 286.Sh HISTORY 287The 288.Xr socket 2 289system call appeared in 290.Bx 4.2 . 291This manual page was introduced in 292.Fx 7.0 293and ported to 294.Ox 4.5 . 295.Sh AUTHORS 296This manual page was written by 297.An Robert Watson . 298.Sh BUGS 299The use of credentials hung from explicitly passed processes, 300and the credential on 301.Dv curproc , 302and the cached credential from socket creation time is inconsistent, and may 303lead to unexpected behaviour. 304.Pp 305The caller may need to manually clear 306.Dv SS_ISCONNECTING 307if 308.Fn soconnect 309returns an error. 310.Pp 311This manual page does not describe how to register socket upcalls or monitor 312a socket for readability/writability without using blocking I/O. 313