DIVERT(4)

NAME
divert - kernel packet diversion mechanism

SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int
socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
int
socket(AF_INET6, SOCK_RAW, IPPROTO_DIVERT);

DESCRIPTION
Divert sockets are part of a mechanism completely integrated with pf(4) that queues raw packets from the kernel stack to userspace applications, and vice versa.

A divert socket must be bound to a divert port through bind(2), which only the superuser can do. Divert ports have their own number space, completely separated from tcp(4) and udp(4). When pf(4) processes a packet that matches a divert rule (see pf.conf(5) for details) it is immediately sent to the divert socket listening on the port specified in the rule. pf(4) reassembles TCP streams by default (if IP reassembly is not disabled) before sending them to the divert sockets. If there are no divert sockets listening, the packets are dropped.

Packets can be read via read(2), recv(2), or recvfrom(2) from the divert socket. The application that is processing the packets can then reinject them into the kernel. After being reinjected, inbound and outbound packets are treated differently. Inbound packets are added to the relevant input queue and a soft interrupt is scheduled to signal that a new packet is ready to be processed; outbound ones are processed directly by the relevant IP/IPv6 output function. The packets' checksums are recalculated upon reinjection.

Writing to a divert socket can be achieved using sendto(2) and it will skip pf(4) filters to avoid loops. A diverted packet that is not reinjected into the kernel stack is lost.

Receive and send divert socket buffer space can be tuned through sysctl(8). netstat(1) shows information relevant to divert sockets.

SEE ALSO
socket(2), ip(4), pf.conf(5)

HISTORY
The divert protocol first appeared in OpenBSD 4.7.