1.\" Copyright (c) 1999 Whistle Communications, Inc. 2.\" All rights reserved. 3.\" 4.\" Subject to the following obligations and disclaimer of warranty, use and 5.\" redistribution of this software, in source or object code forms, with or 6.\" without modifications are expressly permitted by Whistle Communications; 7.\" provided, however, that: 8.\" 1. Any and all reproductions of the source or object code must include the 9.\" copyright notice above and the following disclaimer of warranties; and 10.\" 2. No rights are granted, in any manner or form, to use Whistle 11.\" Communications, Inc. trademarks, including the mark "WHISTLE 12.\" COMMUNICATIONS" on advertising, endorsements, or otherwise except as 13.\" such appears in the above copyright notice or in the software. 14.\" 15.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 16.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 17.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 18.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 19.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 20.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 21.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 22.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 23.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 24.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 25.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 26.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 27.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 28.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 31.\" OF SUCH DAMAGE. 32.\" 33.\" Author: Archie Cobbs <archie@FreeBSD.org> 34.\" 35.Dd January 9, 2012 36.Dt NG_KSOCKET 4 37.Os 38.Sh NAME 39.Nm ng_ksocket 40.Nd kernel socket netgraph node type 41.Sh SYNOPSIS 42.In sys/types.h 43.In netgraph/ng_ksocket.h 44.Sh DESCRIPTION 45A 46.Nm ksocket 47node is both a netgraph node and a 48.Bx 49socket. 50The 51.Nm 52node type allows one to open a socket inside the kernel and have 53it appear as a Netgraph node. 54The 55.Nm 56node type is the reverse of the socket node type (see 57.Xr ng_socket 4 ) : 58whereas the socket node type enables the user-level manipulation (via 59a socket) of what is normally a kernel-level entity (the associated 60Netgraph node), the 61.Nm 62node type enables the kernel-level manipulation (via a Netgraph node) of 63what is normally a user-level entity (the associated socket). 64.Pp 65A 66.Nm 67node allows at most one hook connection. 68Connecting to the node is 69equivalent to opening the associated socket. 70The name given to the hook 71determines what kind of socket the node will open (see below). 72When the hook is disconnected and/or the node is shutdown, the 73associated socket is closed. 74.Sh HOOKS 75This node type supports a single hook connection at a time. 76The name of the hook must be of the form 77.Em <family>/<type>/<proto> , 78where the 79.Em family , 80.Em type , 81and 82.Em proto 83are the decimal equivalent of the same arguments to 84.Xr socket 2 . 85Alternately, aliases for the commonly used values are accepted as 86well. 87For example 88.Dv inet/dgram/udp 89is a more readable but equivalent version of 90.Dv 2/2/17 . 91.Pp 92Data received into socket is sent out via hook. 93Data received on hook is sent out from socket, if the latter is 94connected (an 95.Dv NGM_KSOCKET_CONNECT 96was sent to node before). 97If socket is not connected, destination 98.Vt "struct sockaddr" 99must be supplied in an mbuf tag with cookie 100.Dv NGM_KSOCKET_COOKIE 101and type 102.Dv NG_KSOCKET_TAG_SOCKADDR 103attached to data. 104Otherwise 105.Nm 106will return 107.Er ENOTCONN 108to sender. 109.Sh CONTROL MESSAGES 110This node type supports the generic control messages, plus the following: 111.Bl -tag -width foo 112.It Dv NGM_KSOCKET_BIND Pq Ic bind 113This functions exactly like the 114.Xr bind 2 115system call. 116The 117.Vt "struct sockaddr" 118socket address parameter should be supplied as an argument. 119.It Dv NGM_KSOCKET_LISTEN Pq Ic listen 120This functions exactly like the 121.Xr listen 2 122system call. 123The backlog parameter (a single 32 bit 124.Dv int ) 125should be supplied as an argument. 126.It Dv NGM_KSOCKET_CONNECT Pq Ic connect 127This functions exactly like the 128.Xr connect 2 129system call. 130The 131.Vt "struct sockaddr" 132destination address parameter should be supplied as an argument. 133.It Dv NGM_KSOCKET_ACCEPT Pq Ic accept 134Equivalent to the 135.Xr accept 2 136system call on a non-blocking socket. 137If there is a pending connection on the queue, 138a new socket and a corresponding cloned node are created. 139Returned are the cloned node's ID and a peer name (as 140.Vt "struct sockaddr" ) . 141If there are no pending connections, 142this control message returns nothing, 143and a connected node will receive the above message asynchronously, 144when a connection is established. 145.Pp 146A cloned node supports a single hook with an arbitrary name. 147If not connected, a node disappears when its parent node is destroyed. 148Once connected, it becomes an independent node. 149.It Dv NGM_KSOCKET_GETNAME Pq Ic getname 150Equivalent to the 151.Xr getsockname 2 152system call. 153The name is returned as a 154.Vt "struct sockaddr" 155in the arguments field of the reply. 156.It Dv NGM_KSOCKET_GETPEERNAME Pq Ic getpeername 157Equivalent to the 158.Xr getpeername 2 159system call. 160The name is returned as a 161.Vt "struct sockaddr" 162in the arguments field of the reply. 163.It Dv NGM_KSOCKET_SETOPT Pq Ic setopt 164Equivalent to the 165.Xr setsockopt 2 166system call, except that the option name, level, and value are passed in a 167.Vt "struct ng_ksocket_sockopt" . 168.It Dv NGM_KSOCKET_GETOPT Pq Ic getopt 169Equivalent to the 170.Xr getsockopt 2 171system call, except that the option is passed in a 172.Vt "struct ng_ksocket_sockopt" . 173When sending this command, the 174.Dv value 175field should be empty; upon return, it will contain the 176retrieved value. 177.El 178.Sh ASCII FORM CONTROL MESSAGES 179For control messages that pass a 180.Vt "struct sockaddr" 181in the argument field, the normal 182.Tn ASCII 183equivalent of the C structure 184is an acceptable form. 185For the 186.Dv PF_INET 187and 188.Dv PF_LOCAL 189address families, a more convenient form is also used, which is 190the protocol family name, followed by a slash, followed by the actual 191address. 192For 193.Dv PF_INET , 194the address is an IP address followed by an optional colon and port number. 195For 196.Dv PF_LOCAL , 197the address is the pathname as a doubly quoted string. 198.Pp 199Examples: 200.Bl -tag -width "PF_LOCAL" 201.It Dv PF_LOCAL 202local/"/tmp/foo.socket" 203.It Dv PF_INET 204inet/192.168.1.1:1234 205.It Other 206.Dv "\&{ family=16 len=16 data=[0x70 0x00 0x01 0x23] \&}" 207.El 208.Pp 209For control messages that pass a 210.Vt "struct ng_ksocket_sockopt" , 211the normal 212.Tn ASCII 213form for that structure is used. 214In the future, more 215convenient encoding of the more common socket options may be supported. 216.Pp 217Setting socket options example: 218.Bl -tag -width "PF_LOCAL" 219.It Set FIB 2 for a socket (SOL_SOCKET, SO_SETFIB): 220.Dv "setopt \&{ level=0xffff name=0x1014 data=[ 2 ] \&}" 221.El 222.Sh SHUTDOWN 223This node shuts down upon receipt of a 224.Dv NGM_SHUTDOWN 225control message, or when the hook is disconnected. 226Shutdown of the node closes the associated socket. 227.Sh SEE ALSO 228.Xr socket 2 , 229.Xr netgraph 4 , 230.Xr ng_socket 4 , 231.Xr ngctl 8 , 232.Xr mbuf_tags 9 , 233.Xr socket 9 234.Sh HISTORY 235The 236.Nm 237node type was implemented in 238.Fx 4.0 . 239.Sh AUTHORS 240.An Archie Cobbs Aq Mt archie@FreeBSD.org 241