xref: /freebsd/share/man/man4/ng_ksocket.4 (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
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.\" $FreeBSD$
36.\"
37.Dd January 9, 2012
38.Dt NG_KSOCKET 4
39.Os
40.Sh NAME
41.Nm ng_ksocket
42.Nd kernel socket netgraph node type
43.Sh SYNOPSIS
44.In sys/types.h
45.In netgraph/ng_ksocket.h
46.Sh DESCRIPTION
47A
48.Nm ksocket
49node is both a netgraph node and a
50.Bx
51socket.
52The
53.Nm
54node type allows one to open a socket inside the kernel and have
55it appear as a Netgraph node.
56The
57.Nm
58node type is the reverse of the socket node type (see
59.Xr ng_socket 4 ) :
60whereas the socket node type enables the user-level manipulation (via
61a socket) of what is normally a kernel-level entity (the associated
62Netgraph node), the
63.Nm
64node type enables the kernel-level manipulation (via a Netgraph node) of
65what is normally a user-level entity (the associated socket).
66.Pp
67A
68.Nm
69node allows at most one hook connection.
70Connecting to the node is
71equivalent to opening the associated socket.
72The name given to the hook
73determines what kind of socket the node will open (see below).
74When the hook is disconnected and/or the node is shutdown, the
75associated socket is closed.
76.Sh HOOKS
77This node type supports a single hook connection at a time.
78The name of the hook must be of the form
79.Em <family>/<type>/<proto> ,
80where the
81.Em family ,
82.Em type ,
83and
84.Em proto
85are the decimal equivalent of the same arguments to
86.Xr socket 2 .
87Alternately, aliases for the commonly used values are accepted as
88well.
89For example
90.Dv inet/dgram/udp
91is a more readable but equivalent version of
92.Dv 2/2/17 .
93.Pp
94Data received into socket is sent out via hook.
95Data received on hook is sent out from socket, if the latter is
96connected (an
97.Dv NGM_KSOCKET_CONNECT
98was sent to node before).
99If socket is not connected, destination
100.Vt "struct sockaddr"
101must be supplied in an mbuf tag with cookie
102.Dv NGM_KSOCKET_COOKIE
103and type
104.Dv NG_KSOCKET_TAG_SOCKADDR
105attached to data.
106Otherwise
107.Nm
108will return
109.Er ENOTCONN
110to sender.
111.Sh CONTROL MESSAGES
112This node type supports the generic control messages, plus the following:
113.Bl -tag -width foo
114.It Dv NGM_KSOCKET_BIND Pq Ic bind
115This functions exactly like the
116.Xr bind 2
117system call.
118The
119.Vt "struct sockaddr"
120socket address parameter should be supplied as an argument.
121.It Dv NGM_KSOCKET_LISTEN Pq Ic listen
122This functions exactly like the
123.Xr listen 2
124system call.
125The backlog parameter (a single 32 bit
126.Dv int )
127should be supplied as an argument.
128.It Dv NGM_KSOCKET_CONNECT Pq Ic connect
129This functions exactly like the
130.Xr connect 2
131system call.
132The
133.Vt "struct sockaddr"
134destination address parameter should be supplied as an argument.
135.It Dv NGM_KSOCKET_ACCEPT Pq Ic accept
136Equivalent to the
137.Xr accept 2
138system call on a non-blocking socket.
139If there is a pending connection on the queue,
140a new socket and a corresponding cloned node are created.
141Returned are the cloned node's ID and a peer name (as
142.Vt "struct sockaddr" ) .
143If there are no pending connections,
144this control message returns nothing,
145and a connected node will receive the above message asynchronously,
146when a connection is established.
147.Pp
148A cloned node supports a single hook with an arbitrary name.
149If not connected, a node disappears when its parent node is destroyed.
150Once connected, it becomes an independent node.
151.It Dv NGM_KSOCKET_GETNAME Pq Ic getname
152Equivalent to the
153.Xr getsockname 2
154system call.
155The name is returned as a
156.Vt "struct sockaddr"
157in the arguments field of the reply.
158.It Dv NGM_KSOCKET_GETPEERNAME Pq Ic getpeername
159Equivalent to the
160.Xr getpeername 2
161system call.
162The name is returned as a
163.Vt "struct sockaddr"
164in the arguments field of the reply.
165.It Dv NGM_KSOCKET_SETOPT Pq Ic setopt
166Equivalent to the
167.Xr setsockopt 2
168system call, except that the option name, level, and value are passed in a
169.Vt "struct ng_ksocket_sockopt" .
170.It Dv NGM_KSOCKET_GETOPT Pq Ic getopt
171Equivalent to the
172.Xr getsockopt 2
173system call, except that the option is passed in a
174.Vt "struct ng_ksocket_sockopt" .
175When sending this command, the
176.Dv value
177field should be empty; upon return, it will contain the
178retrieved value.
179.El
180.Sh ASCII FORM CONTROL MESSAGES
181For control messages that pass a
182.Vt "struct sockaddr"
183in the argument field, the normal
184.Tn ASCII
185equivalent of the C structure
186is an acceptable form.
187For the
188.Dv PF_INET
189and
190.Dv PF_LOCAL
191address families, a more convenient form is also used, which is
192the protocol family name, followed by a slash, followed by the actual
193address.
194For
195.Dv PF_INET ,
196the address is an IP address followed by an optional colon and port number.
197For
198.Dv PF_LOCAL ,
199the address is the pathname as a doubly quoted string.
200.Pp
201Examples:
202.Bl -tag -width "PF_LOCAL"
203.It Dv PF_LOCAL
204local/"/tmp/foo.socket"
205.It Dv PF_INET
206inet/192.168.1.1:1234
207.It Other
208.Dv "\&{ family=16 len=16 data=[0x70 0x00 0x01 0x23] \&}"
209.El
210.Pp
211For control messages that pass a
212.Vt "struct ng_ksocket_sockopt" ,
213the normal
214.Tn ASCII
215form for that structure is used.
216In the future, more
217convenient encoding of the more common socket options may be supported.
218.Pp
219Setting socket options example:
220.Bl -tag -width "PF_LOCAL"
221.It Set FIB 2 for a socket (SOL_SOCKET, SO_SETFIB):
222.Dv "setopt \&{ level=0xffff name=0x1014 data=[ 2 ] \&}"
223.El
224.Sh SHUTDOWN
225This node shuts down upon receipt of a
226.Dv NGM_SHUTDOWN
227control message, or when the hook is disconnected.
228Shutdown of the node closes the associated socket.
229.Sh SEE ALSO
230.Xr socket 2 ,
231.Xr netgraph 4 ,
232.Xr ng_socket 4 ,
233.Xr ngctl 8 ,
234.Xr mbuf_tags 9 ,
235.Xr socket 9
236.Sh HISTORY
237The
238.Nm
239node type was implemented in
240.Fx 4.0 .
241.Sh AUTHORS
242.An Archie Cobbs Aq Mt archie@FreeBSD.org
243