xref: /freebsd/share/man/man4/ng_ksocket.4 (revision 9234a50752cd47887849d4665af0f9f4abdefb5d)
1f7045ac0SArchie Cobbs.\" Copyright (c) 1999 Whistle Communications, Inc.
2f7045ac0SArchie Cobbs.\" All rights reserved.
3f7045ac0SArchie Cobbs.\"
4f7045ac0SArchie Cobbs.\" Subject to the following obligations and disclaimer of warranty, use and
5f7045ac0SArchie Cobbs.\" redistribution of this software, in source or object code forms, with or
6f7045ac0SArchie Cobbs.\" without modifications are expressly permitted by Whistle Communications;
7f7045ac0SArchie Cobbs.\" provided, however, that:
8f7045ac0SArchie Cobbs.\" 1. Any and all reproductions of the source or object code must include the
9f7045ac0SArchie Cobbs.\"    copyright notice above and the following disclaimer of warranties; and
10f7045ac0SArchie Cobbs.\" 2. No rights are granted, in any manner or form, to use Whistle
11f7045ac0SArchie Cobbs.\"    Communications, Inc. trademarks, including the mark "WHISTLE
12f7045ac0SArchie Cobbs.\"    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
13f7045ac0SArchie Cobbs.\"    such appears in the above copyright notice or in the software.
14f7045ac0SArchie Cobbs.\"
15f7045ac0SArchie Cobbs.\" THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
16f7045ac0SArchie Cobbs.\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
17f7045ac0SArchie Cobbs.\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18f7045ac0SArchie Cobbs.\" INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19f7045ac0SArchie Cobbs.\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20f7045ac0SArchie Cobbs.\" WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21f7045ac0SArchie Cobbs.\" REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22f7045ac0SArchie Cobbs.\" SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23f7045ac0SArchie Cobbs.\" IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
24f7045ac0SArchie Cobbs.\" RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25f7045ac0SArchie Cobbs.\" WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26f7045ac0SArchie Cobbs.\" PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27f7045ac0SArchie Cobbs.\" SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28f7045ac0SArchie Cobbs.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29f7045ac0SArchie Cobbs.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30f7045ac0SArchie Cobbs.\" THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
31f7045ac0SArchie Cobbs.\" OF SUCH DAMAGE.
32f7045ac0SArchie Cobbs.\"
33eddc45e7SJeroen Ruigrok van der Werven.\" Author: Archie Cobbs <archie@FreeBSD.org>
34f7045ac0SArchie Cobbs.\"
35*9234a507SSeyed Pouria Mousavizadeh Tehrani.Dd January 9, 2025
36bec35b9aSArchie Cobbs.Dt NG_KSOCKET 4
373d45e180SRuslan Ermilov.Os
38f7045ac0SArchie Cobbs.Sh NAME
39f7045ac0SArchie Cobbs.Nm ng_ksocket
40f7045ac0SArchie Cobbs.Nd kernel socket netgraph node type
41f7045ac0SArchie Cobbs.Sh SYNOPSIS
42fbad9e2dSRuslan Ermilov.In sys/types.h
4332eef9aeSRuslan Ermilov.In netgraph/ng_ksocket.h
44f7045ac0SArchie Cobbs.Sh DESCRIPTION
45f7045ac0SArchie CobbsA
46f7045ac0SArchie Cobbs.Nm ksocket
47753d686dSRuslan Ermilovnode is both a netgraph node and a
48753d686dSRuslan Ermilov.Bx
49b5e7e999SRuslan Ermilovsocket.
50b5e7e999SRuslan ErmilovThe
5151291597SJulian Elischer.Nm
52f7045ac0SArchie Cobbsnode type allows one to open a socket inside the kernel and have
53b5e7e999SRuslan Ermilovit appear as a Netgraph node.
54b5e7e999SRuslan ErmilovThe
5551291597SJulian Elischer.Nm
56f7045ac0SArchie Cobbsnode type is the reverse of the socket node type (see
57bec35b9aSArchie Cobbs.Xr ng_socket 4 ) :
58f7045ac0SArchie Cobbswhereas the socket node type enables the user-level manipulation (via
598e078873SArchie Cobbsa socket) of what is normally a kernel-level entity (the associated
608e078873SArchie CobbsNetgraph node), the
6151291597SJulian Elischer.Nm
62f7045ac0SArchie Cobbsnode type enables the kernel-level manipulation (via a Netgraph node) of
638e078873SArchie Cobbswhat is normally a user-level entity (the associated socket).
64f7045ac0SArchie Cobbs.Pp
65f7045ac0SArchie CobbsA
6651291597SJulian Elischer.Nm
67b5e7e999SRuslan Ermilovnode allows at most one hook connection.
68b5e7e999SRuslan ErmilovConnecting to the node is
69b5e7e999SRuslan Ermilovequivalent to opening the associated socket.
70b5e7e999SRuslan ErmilovThe name given to the hook
718e078873SArchie Cobbsdetermines what kind of socket the node will open (see below).
72f7045ac0SArchie CobbsWhen the hook is disconnected and/or the node is shutdown, the
73f7045ac0SArchie Cobbsassociated socket is closed.
74f7045ac0SArchie Cobbs.Sh HOOKS
75f7045ac0SArchie CobbsThis node type supports a single hook connection at a time.
76f7045ac0SArchie CobbsThe name of the hook must be of the form
77c2d03ea8SRuslan Ermilov.Em <family>/<type>/<proto> ,
78f7045ac0SArchie Cobbswhere the
79c2d03ea8SRuslan Ermilov.Em family ,
80c2d03ea8SRuslan Ermilov.Em type ,
81f7045ac0SArchie Cobbsand
82c2d03ea8SRuslan Ermilov.Em proto
83f7045ac0SArchie Cobbsare the decimal equivalent of the same arguments to
84f7045ac0SArchie Cobbs.Xr socket 2 .
85f7045ac0SArchie CobbsAlternately, aliases for the commonly used values are accepted as
865203edcdSRuslan Ermilovwell.
875203edcdSRuslan ErmilovFor example
88f7045ac0SArchie Cobbs.Dv inet/dgram/udp
89f7045ac0SArchie Cobbsis a more readable but equivalent version of
90f7045ac0SArchie Cobbs.Dv 2/2/17 .
91327b288eSJulian Elischer.Pp
92327b288eSJulian ElischerData received into socket is sent out via hook.
93327b288eSJulian ElischerData received on hook is sent out from socket, if the latter is
94327b288eSJulian Elischerconnected (an
95327b288eSJulian Elischer.Dv NGM_KSOCKET_CONNECT
96327b288eSJulian Elischerwas sent to node before).
97327b288eSJulian ElischerIf socket is not connected, destination
985203edcdSRuslan Ermilov.Vt "struct sockaddr"
99327b288eSJulian Elischermust be supplied in an mbuf tag with cookie
100327b288eSJulian Elischer.Dv NGM_KSOCKET_COOKIE
101327b288eSJulian Elischerand type
102327b288eSJulian Elischer.Dv NG_KSOCKET_TAG_SOCKADDR
103327b288eSJulian Elischerattached to data.
104327b288eSJulian ElischerOtherwise
105327b288eSJulian Elischer.Nm
106327b288eSJulian Elischerwill return
1075203edcdSRuslan Ermilov.Er ENOTCONN
108327b288eSJulian Elischerto sender.
109f7045ac0SArchie Cobbs.Sh CONTROL MESSAGES
110f7045ac0SArchie CobbsThis node type supports the generic control messages, plus the following:
111f7045ac0SArchie Cobbs.Bl -tag -width foo
112c60bda17SJoel Dahl.It Dv NGM_KSOCKET_BIND Pq Ic bind
113f7045ac0SArchie CobbsThis functions exactly like the
114f7045ac0SArchie Cobbs.Xr bind 2
115b5e7e999SRuslan Ermilovsystem call.
116b5e7e999SRuslan ErmilovThe
1175203edcdSRuslan Ermilov.Vt "struct sockaddr"
118f7045ac0SArchie Cobbssocket address parameter should be supplied as an argument.
119c60bda17SJoel Dahl.It Dv NGM_KSOCKET_LISTEN Pq Ic listen
120f7045ac0SArchie CobbsThis functions exactly like the
121f7045ac0SArchie Cobbs.Xr listen 2
122b5e7e999SRuslan Ermilovsystem call.
123b5e7e999SRuslan ErmilovThe backlog parameter (a single 32 bit
124f7045ac0SArchie Cobbs.Dv int )
125f7045ac0SArchie Cobbsshould be supplied as an argument.
126c60bda17SJoel Dahl.It Dv NGM_KSOCKET_CONNECT Pq Ic connect
127f7045ac0SArchie CobbsThis functions exactly like the
128f7045ac0SArchie Cobbs.Xr connect 2
129b5e7e999SRuslan Ermilovsystem call.
130b5e7e999SRuslan ErmilovThe
1315203edcdSRuslan Ermilov.Vt "struct sockaddr"
1328e078873SArchie Cobbsdestination address parameter should be supplied as an argument.
133c60bda17SJoel Dahl.It Dv NGM_KSOCKET_ACCEPT Pq Ic accept
134c2888a85SRuslan ErmilovEquivalent to the
135c2888a85SRuslan Ermilov.Xr accept 2
136c2888a85SRuslan Ermilovsystem call on a non-blocking socket.
137c2888a85SRuslan ErmilovIf there is a pending connection on the queue,
138c2888a85SRuslan Ermilova new socket and a corresponding cloned node are created.
139c2888a85SRuslan ErmilovReturned are the cloned node's ID and a peer name (as
140c2888a85SRuslan Ermilov.Vt "struct sockaddr" ) .
141c2888a85SRuslan ErmilovIf there are no pending connections,
142c2888a85SRuslan Ermilovthis control message returns nothing,
143c2888a85SRuslan Ermilovand a connected node will receive the above message asynchronously,
144c2888a85SRuslan Ermilovwhen a connection is established.
145c2888a85SRuslan Ermilov.Pp
146c2888a85SRuslan ErmilovA cloned node supports a single hook with an arbitrary name.
147c2888a85SRuslan ErmilovIf not connected, a node disappears when its parent node is destroyed.
148c2888a85SRuslan ErmilovOnce connected, it becomes an independent node.
149c60bda17SJoel Dahl.It Dv NGM_KSOCKET_GETNAME Pq Ic getname
1508e078873SArchie CobbsEquivalent to the
151000d5619SMaxim Sobolev.Xr getsockname 2
152b5e7e999SRuslan Ermilovsystem call.
153b5e7e999SRuslan ErmilovThe name is returned as a
1545203edcdSRuslan Ermilov.Vt "struct sockaddr"
1558e078873SArchie Cobbsin the arguments field of the reply.
156c60bda17SJoel Dahl.It Dv NGM_KSOCKET_GETPEERNAME Pq Ic getpeername
1578e078873SArchie CobbsEquivalent to the
1588e078873SArchie Cobbs.Xr getpeername 2
159b5e7e999SRuslan Ermilovsystem call.
160b5e7e999SRuslan ErmilovThe name is returned as a
1615203edcdSRuslan Ermilov.Vt "struct sockaddr"
1628e078873SArchie Cobbsin the arguments field of the reply.
163c60bda17SJoel Dahl.It Dv NGM_KSOCKET_SETOPT Pq Ic setopt
1648e078873SArchie CobbsEquivalent to the
1658e078873SArchie Cobbs.Xr setsockopt 2
1668e078873SArchie Cobbssystem call, except that the option name, level, and value are passed in a
1675203edcdSRuslan Ermilov.Vt "struct ng_ksocket_sockopt" .
168c60bda17SJoel Dahl.It Dv NGM_KSOCKET_GETOPT Pq Ic getopt
1698e078873SArchie CobbsEquivalent to the
1708e078873SArchie Cobbs.Xr getsockopt 2
1718e078873SArchie Cobbssystem call, except that the option is passed in a
1725203edcdSRuslan Ermilov.Vt "struct ng_ksocket_sockopt" .
1738e078873SArchie CobbsWhen sending this command, the
1748e078873SArchie Cobbs.Dv value
1758e078873SArchie Cobbsfield should be empty; upon return, it will contain the
1768e078873SArchie Cobbsretrieved value.
177f7045ac0SArchie Cobbs.El
1788e078873SArchie Cobbs.Sh ASCII FORM CONTROL MESSAGES
1798e078873SArchie CobbsFor control messages that pass a
1805203edcdSRuslan Ermilov.Vt "struct sockaddr"
18151291597SJulian Elischerin the argument field, the normal
18251291597SJulian Elischer.Tn ASCII
18351291597SJulian Elischerequivalent of the C structure
184b5e7e999SRuslan Ermilovis an acceptable form.
185b5e7e999SRuslan ErmilovFor the
186*9234a507SSeyed Pouria Mousavizadeh Tehrani.Dv PF_INET ,
187*9234a507SSeyed Pouria Mousavizadeh Tehrani.Dv PF_INET6
1888e078873SArchie Cobbsand
1898e078873SArchie Cobbs.Dv PF_LOCAL
1908e078873SArchie Cobbsaddress families, a more convenient form is also used, which is
1918e078873SArchie Cobbsthe protocol family name, followed by a slash, followed by the actual
192b5e7e999SRuslan Ermilovaddress.
193b5e7e999SRuslan ErmilovFor
1948e078873SArchie Cobbs.Dv PF_INET ,
195*9234a507SSeyed Pouria Mousavizadeh Tehranithe address is an IPv4 address followed by an optional colon and port number.
196*9234a507SSeyed Pouria Mousavizadeh TehraniFor
197*9234a507SSeyed Pouria Mousavizadeh Tehrani.Dv PF_INET6 ,
198*9234a507SSeyed Pouria Mousavizadeh Tehranithe address is an IPv6 address enclosed in square brackets followed
199*9234a507SSeyed Pouria Mousavizadeh Tehraniby an optional colon and port number.
2008e078873SArchie CobbsFor
2018e078873SArchie Cobbs.Dv PF_LOCAL ,
2028e078873SArchie Cobbsthe address is the pathname as a doubly quoted string.
2038e078873SArchie Cobbs.Pp
2048e078873SArchie CobbsExamples:
205756e2ef0SAlexander V. Chernikov.Bl -tag -width "PF_LOCAL"
2068e078873SArchie Cobbs.It Dv PF_LOCAL
2078e078873SArchie Cobbslocal/"/tmp/foo.socket"
208ef02c1e5SArchie Cobbs.It Dv PF_INET
209ef02c1e5SArchie Cobbsinet/192.168.1.1:1234
210*9234a507SSeyed Pouria Mousavizadeh Tehrani.It Dv PF_INET6
211*9234a507SSeyed Pouria Mousavizadeh Tehraniinet6/[2001::1]:1234
2128e078873SArchie Cobbs.It Other
213ef02c1e5SArchie Cobbs.Dv "\&{ family=16 len=16 data=[0x70 0x00 0x01 0x23] \&}"
2148e078873SArchie Cobbs.El
2158e078873SArchie Cobbs.Pp
2168e078873SArchie CobbsFor control messages that pass a
2175203edcdSRuslan Ermilov.Vt "struct ng_ksocket_sockopt" ,
21851291597SJulian Elischerthe normal
21951291597SJulian Elischer.Tn ASCII
220b5e7e999SRuslan Ermilovform for that structure is used.
221b5e7e999SRuslan ErmilovIn the future, more
2228e078873SArchie Cobbsconvenient encoding of the more common socket options may be supported.
223756e2ef0SAlexander V. Chernikov.Pp
224756e2ef0SAlexander V. ChernikovSetting socket options example:
225756e2ef0SAlexander V. Chernikov.Bl -tag -width "PF_LOCAL"
226756e2ef0SAlexander V. Chernikov.It Set FIB 2 for a socket (SOL_SOCKET, SO_SETFIB):
227756e2ef0SAlexander V. Chernikov.Dv "setopt \&{ level=0xffff name=0x1014 data=[ 2 ] \&}"
228756e2ef0SAlexander V. Chernikov.El
229f7045ac0SArchie Cobbs.Sh SHUTDOWN
230f7045ac0SArchie CobbsThis node shuts down upon receipt of a
231f7045ac0SArchie Cobbs.Dv NGM_SHUTDOWN
232f7045ac0SArchie Cobbscontrol message, or when the hook is disconnected.
233f7045ac0SArchie CobbsShutdown of the node closes the associated socket.
234f7045ac0SArchie Cobbs.Sh SEE ALSO
235f7045ac0SArchie Cobbs.Xr socket 2 ,
236f7045ac0SArchie Cobbs.Xr netgraph 4 ,
237bec35b9aSArchie Cobbs.Xr ng_socket 4 ,
238327b288eSJulian Elischer.Xr ngctl 8 ,
23966e7b3a3SRobert Watson.Xr mbuf_tags 9 ,
24066e7b3a3SRobert Watson.Xr socket 9
24151291597SJulian Elischer.Sh HISTORY
24251291597SJulian ElischerThe
24351291597SJulian Elischer.Nm
24451291597SJulian Elischernode type was implemented in
24551291597SJulian Elischer.Fx 4.0 .
24651291597SJulian Elischer.Sh AUTHORS
2476c899950SBaptiste Daroussin.An Archie Cobbs Aq Mt archie@FreeBSD.org
248