NG_KSOCKET(4)
๐ NAME ๐
okay so this is NG_KSOCKET(4) from the Kernel Interfaces Manual, and the name line is: ng_ksocket โ kernel socket netgraph node type. That's it, that's the whole vibe. ๐ซถ
๐ SYNOPSIS ๐
#include <sys/types.h>
#include <netgraph/ng_ksocket.h>
๐ซถ DESCRIPTION ๐ซถ
A ksocket node is literally both a netgraph node and a BSD socket. The ng_ksocket node type lets you open a socket inside the kernel and have it show up as a Netgraph node. It's the reverse of the socket node type โ go see ng_socket(4) โ because over there, the socket node type enables user-level manipulation (via a socket) of what's normally a kernel-level entity (the associated Netgraph node). Over here, ng_ksocket enables kernel-level manipulation (via a Netgraph node) of what's normally a user-level entity (the associated socket). The way that mirror-flip ate, honestly. ๐ช
A ng_ksocket node allows at most one hook connection. Connecting to the node is equivalent to opening the associated socket. The name you give the hook determines what kind of socket the node opens (peep below). When the hook is disconnected and/or the node is shutdown, the associated socket is closed. No lingering, no ghosting. ๐
๐ช HOOKS ๐ช
This node type supports a single hook connection at a time. The name of the hook must be of the form <family>/<type>/<proto>, where family, type, and proto are the decimal equivalent of the same arguments to socket(2). Alternately, aliases for the commonly used values are accepted as well. For example inet/dgram/udp is a more readable but equivalent version of 2/2/17. iykyk โจ
Data received into socket is sent out via hook. Data received on hook is sent out from socket, if the latter is connected (an NGM_KSOCKET_CONNECT was sent to node before). If socket is not connected, destination struct sockaddr must be supplied in an mbuf tag with cookie NGM_KSOCKET_COOKIE and type NG_KSOCKET_TAG_SOCKADDR attached to data. Otherwise ng_ksocket will return ENOTCONN to sender. โ ๏ธ no cap, that's the rule.
๐๏ธ CONTROL MESSAGES ๐๏ธ
This node type supports the generic control messages, plus the following:
NGM_KSOCKET_BIND(bind) โ This functions exactly like thebind(2)system call. Thestruct sockaddrsocket address parameter should be supplied as an argument. ๐NGM_KSOCKET_LISTEN(listen) โ This functions exactly like thelisten(2)system call. Thebacklogparameter (a single 32 bitint) should be supplied as an argument. ๐NGM_KSOCKET_CONNECT(connect) โ This functions exactly like theconnect(2)system call. Thestruct sockaddrdestination address parameter should be supplied as an argument. ๐NGM_KSOCKET_ACCEPT(accept) โ Equivalent to theaccept(2)system call on a non-blocking socket. If there is a pending connection on the queue, a new socket and a corresponding cloned node are created. Returned are the cloned node's ID and a peer name (asstruct sockaddr). If there are no pending connections, this control message returns nothing, and a connected node will receive the above message asynchronously, when a connection is established. ๐ซถ
A cloned node supports a single hook with an arbitrary name. If not connected, a node disappears when its parent node is destroyed. Once connected, it becomes an independent node. main character energy fr fr ๐
NGM_KSOCKET_GETNAME(getname) โ Equivalent to thegetsockname(2)system call. The name is returned as astruct sockaddrin the arguments field of the reply. ๐ท๏ธNGM_KSOCKET_GETPEERNAME(getpeername) โ Equivalent to thegetpeername(2)system call. The name is returned as astruct sockaddrin the arguments field of the reply. ๐ชNGM_KSOCKET_SETOPT(setopt) โ Equivalent to thesetsockopt(2)system call, except that the option name, level, and value are passed in astruct ng_ksocket_sockopt. ๐๏ธNGM_KSOCKET_GETOPT(getopt) โ Equivalent to thegetsockopt(2)system call, except that the option is passed in astruct ng_ksocket_sockopt. When sending this command, the value field should be empty; upon return, it will contain the retrieved value. ๐ฅ
๐ค ASCII FORM CONTROL MESSAGES ๐ค
For control messages that pass a struct sockaddr in the argument field, the normal ASCII equivalent of the C structure is an acceptable form. For the PF_INET, PF_INET6 and PF_LOCAL address families, a more convenient form is also used, which is the protocol family name, followed by a slash, followed by the actual address. For PF_INET, the address is an IPv4 address followed by an optional colon and port number. For PF_INET6, the address is an IPv6 address enclosed in square brackets followed by an optional colon and port number. For PF_LOCAL, the address is the pathname as a doubly quoted string. ๐
Examples:
PF_LOCAL local/"/tmp/foo.socket"
PF_INET inet/192.168.1.1:1234
PF_INET6 inet6/[2001::1]:1234
Other { family=16 len=16 data=[0x70 0x00 0x01 0x23] }
For control messages that pass a struct ng_ksocket_sockopt, the normal ASCII form for that structure is used. In the future, more convenient encoding of the more common socket options may be supported. ๐ฎ
Setting socket options example:
Set FIB 2 for a socket (SOL_SOCKET, SO_SETFIB):
setopt { level=0xffff name=0x1014 data=[ 2 ] }
๐ SHUTDOWN ๐
This node shuts down upon receipt of a NGM_SHUTDOWN control message, or when the hook is disconnected. Shutdown of the node closes the associated socket. bye bye socket ๐๐
๐ SEE ALSO ๐
socket(2), netgraph(4), ng_socket(4), ngctl(8), mbuf_tags(9), socket(9)
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
The ng_ksocket node type was implemented in FreeBSD 4.0. vintage but still iconic ๐
๐ AUTHORS ๐
Archie Cobbs <archie@FreeBSD.org>
xoxo, your kernel bestie ๐ โ this page is from Debian, dated January 9, 2025. stay socket-y ๐ซถ
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1999 Whistle Communications, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms