NG_KSOCKET(4)

✨ NG_KSOCKET β€” kernel socket netgraph node type ✨

okay bestie, settle in, because this one is from the Kernel Interfaces Manual and it's genuinely such a fun little creature πŸ’–


πŸŽ€ NAME πŸŽ€

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 πŸ¦‹

okay so picture this: a ksocket node is both a netgraph node and a BSD socket at the same time. like. it's giving duality. it's giving main character with two jobs πŸ’…

The ng_ksocket node type lets you open a socket inside the kernel and have it show up as a Netgraph node. And the way that it's literally the reverse of the socket node type (peep ng_socket(4))?? iconic.

  • the socket node type = user-level manipulation (via a socket) of what is normally a kernel-level entity (the associated Netgraph node)
  • the ng_ksocket node type = kernel-level manipulation (via a Netgraph node) of what is normally a user-level entity (the associated socket)

fr fr, mirror images. obsessed.

A ng_ksocket node allows at most one hook connection. Connecting to the node is the same thing as opening the associated socket. The name you give to the hook decides what kind of socket the node opens (more on that below, keep reading 🫢). And when the hook gets disconnected and/or the node is shutdown, the associated socket is closed. clean. no loose ends. periodt.


🌈 HOOKS 🌈

This node type supports a single hook connection at a time. The name of the hook has to be of the form <family>/<type>/<proto>, where the 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. same girl, cuter outfit πŸ’–

Data received into the socket is sent out via the hook. Data received on the hook is sent out from the socket, if the socket is connected (an NGM_KSOCKET_CONNECT was sent to the node before). If the socket is not connected, a destination struct sockaddr must be supplied in an mbuf tag with cookie NGM_KSOCKET_COOKIE and type NG_KSOCKET_TAG_SOCKADDR attached to the data. Otherwise ng_ksocket will return ENOTCONN to the sender. 😭 rude but fair


πŸ‘‘ CONTROL MESSAGES πŸ‘‘

This node type supports the generic control messages, plus the following:

  • NGM_KSOCKET_BIND (bind) β€” this functions exactly like the bind(2) system call. The struct sockaddr socket address parameter should be supplied as an argument.
  • NGM_KSOCKET_LISTEN (listen) β€” this functions exactly like the listen(2) system call. The backlog parameter (a single 32 bit int) should be supplied as an argument.
  • NGM_KSOCKET_CONNECT (connect) β€” this functions exactly like the connect(2) system call. The struct sockaddr destination address parameter should be supplied as an argument.
  • NGM_KSOCKET_ACCEPT (accept) β€” equivalent to the accept(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 (as struct 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. glow up era ✨

  • NGM_KSOCKET_GETNAME (getname) β€” equivalent to the getsockname(2) system call. The name is returned as a struct sockaddr in the arguments field of the reply.
  • NGM_KSOCKET_GETPEERNAME (getpeername) β€” equivalent to the getpeername(2) system call. The name is returned as a struct sockaddr in the arguments field of the reply.
  • NGM_KSOCKET_SETOPT (setopt) β€” equivalent to the setsockopt(2) system call, except that the option name, level, and value are passed in a struct ng_ksocket_sockopt.
  • NGM_KSOCKET_GETOPT (getopt) β€” equivalent to the getsockopt(2) system call, except that the option is passed in a struct ng_ksocket_sockopt. When sending this command, the value field should be empty; upon return, it will contain the retrieved value. surprise!! it filled itself in 🎁

🧚 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 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 IP address 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

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. πŸ‘€ noted

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. she's leaving, and she's taking the socket with her πŸ’…


πŸ’œ 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. old money, honestly πŸ‘‘


🫢 AUTHORS 🫢

Archie Cobbs <archie@FreeBSD.org>


and that's the whole page bestie!! this one came from Debian, dated January 9, 2012, and it's the NG_KSOCKET(4) girlie. no cap. go open a socket inside your kernel and have a great day πŸŒΈβœ¨πŸ’–

πŸ“¦ where this page comes from