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.

EXAMPLES
The following PF rule queues outbound IPv4 packets to TCP port 80, as well as the return traffic, on the em0 interface to divert port 700:

    pass out on em0 inet proto tcp to port 80 divert-packet port 700

The following program reads packets on divert port 700 and reinjects them back into the kernel. This program does not perform any processing of the packets, apart from discarding invalid IP packets.

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/in_systm.h>
    #include <netinet/ip.h>
    #include <netinet/ip_var.h>
    #include <netinet/tcp.h>
    #include <netinet/tcpip.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <err.h>
    #define DIVERT_PORT 700
    int
    main(int argc, char *argv[])
    {
    	int fd, s;
    	struct sockaddr_in sin;
    	socklen_t sin_len;
    	fd = socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
    	if (fd == -1)
    		err(1, "socket");
    	bzero(&sin, sizeof(sin));
    	sin.sin_family = AF_INET;
    	sin.sin_port = htons(DIVERT_PORT);
    	sin.sin_addr.s_addr = 0;
    	sin_len = sizeof(struct sockaddr_in);
    	s = bind(fd, (struct sockaddr *) &sin, sin_len);
    	if (s == -1)
    		err(1, "bind");
    	for (;;) {
    		ssize_t n;
    		char packet[10000];
    		struct ip *ip_hdr;
    		struct tcpiphdr *tcpip_hdr;
    		char srcip[40], dstip[40];
    		bzero(packet, sizeof(packet));
    		n = recvfrom(fd, packet, sizeof(packet), 0,
    		    (struct sockaddr *) &sin, &sin_len);
    		tcpip_hdr = (struct tcpiphdr *) packet;
    		ip_hdr = (struct ip *) packet;
    		bzero(srcip, sizeof(srcip));
    		bzero(dstip, sizeof(dstip));
    		if (inet_ntop(AF_INET, &ip_hdr->ip_src, srcip,
    		    sizeof(srcip)) == NULL) {
    			fprintf(stderr, "Invalid IPv4 source packet\n");
    			continue;
    		}
    		if (inet_ntop(AF_INET, &ip_hdr->ip_dst, dstip,
    		    sizeof(dstip)) == NULL) {
    			fprintf(stderr, "Invalid IPv4 destination "
    			    "packet\n");
    			continue;
    		}
    		printf("%s:%u -> %s:%u\n",
    			srcip,
    			ntohs(tcpip_hdr->ti_sport),
    			dstip,
    			ntohs(tcpip_hdr->ti_dport)
    		);
    		n = sendto(fd, packet, n, 0, (struct sockaddr *) &sin,
    		    sin_len);
    	}
    	return 0;
    }

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

HISTORY
The divert protocol first appeared in OpenBSD 4.7.