1.\" $OpenBSD: divert.4,v 1.8 2012/03/29 17:09:41 jmc Exp $ 2.\" 3.\" Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> 4.\" 5.\" Permission to use, copy, modify, and distribute this software for any 6.\" purpose with or without fee is hereby granted, provided that the above 7.\" copyright notice and this permission notice appear in all copies. 8.\" 9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16.\" 17.Dd $Mdocdate: March 29 2012 $ 18.Dt DIVERT 4 19.Os 20.Sh NAME 21.Nm divert 22.Nd kernel packet diversion mechanism 23.Sh SYNOPSIS 24.Fd #include <sys/types.h> 25.Fd #include <sys/socket.h> 26.Fd #include <netinet/in.h> 27.Ft int 28.Fn socket AF_INET SOCK_RAW IPPROTO_DIVERT 29.Ft int 30.Fn socket AF_INET6 SOCK_RAW IPPROTO_DIVERT 31.Sh DESCRIPTION 32Divert sockets are part of a mechanism completely integrated with 33.Xr pf 4 34that queues raw packets from the kernel stack to userspace applications, 35and vice versa. 36.Pp 37A divert socket must be bound to a divert port through 38.Xr bind 2 , 39which only the superuser can do. 40Divert ports have their own number space, completely separated from 41.Xr tcp 4 42and 43.Xr udp 4 . 44When 45.Xr pf 4 46processes a packet that matches a divert rule (see 47.Xr pf.conf 5 48for details) it is immediately sent to the divert socket listening on the 49port specified in the rule. 50.Xr pf 4 51reassembles TCP streams by default (if IP reassembly is not disabled) 52before sending them to the divert sockets. 53If there are no divert sockets listening, the packets are dropped. 54.Pp 55Packets can be read via 56.Xr read 2 , 57.Xr recv 2 , 58or 59.Xr recvfrom 2 60from the divert socket. 61The application that is processing the packets can then reinject them into the 62kernel. 63After being reinjected, inbound and outbound packets are treated differently. 64Inbound packets are added to the relevant input queue and a soft interrupt is 65scheduled to signal that a new packet is ready to be processed; outbound ones 66are processed directly by the relevant IP/IPv6 output function. 67The packets' checksums are recalculated upon reinjection. 68.Pp 69Writing to a divert socket can be achieved using 70.Xr sendto 2 71and it will skip 72.Xr pf 4 73filters to avoid loops. 74A diverted packet that is not reinjected into the kernel stack is lost. 75.Pp 76Receive and send divert socket buffer space can be tuned through 77.Xr sysctl 8 . 78.Xr netstat 1 79shows information relevant to divert sockets. 80.Sh EXAMPLES 81The following PF rule queues outbound IPv4 packets to TCP port 80, 82as well as the return traffic, on the em0 interface to divert port 700: 83.Bd -literal -offset indent 84pass out on em0 inet proto tcp to port 80 divert-packet port 700 85.Ed 86.Pp 87The following program reads packets on divert port 700 and reinjects them 88back into the kernel. 89This program does not perform any processing of the packets, 90apart from discarding invalid IP packets. 91.Bd -literal 92#include <sys/types.h> 93#include <sys/socket.h> 94#include <netinet/in.h> 95#include <netinet/in_systm.h> 96#include <netinet/ip.h> 97#include <netinet/ip_var.h> 98#include <netinet/tcp.h> 99#include <netinet/tcpip.h> 100#include <arpa/inet.h> 101#include <stdio.h> 102#include <stdlib.h> 103#include <string.h> 104#include <err.h> 105 106#define DIVERT_PORT 700 107 108int 109main(int argc, char *argv[]) 110{ 111 int fd, s; 112 struct sockaddr_in sin; 113 socklen_t sin_len; 114 115 fd = socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT); 116 if (fd == -1) 117 err(1, "socket"); 118 119 bzero(&sin, sizeof(sin)); 120 sin.sin_family = AF_INET; 121 sin.sin_port = htons(DIVERT_PORT); 122 sin.sin_addr.s_addr = 0; 123 124 sin_len = sizeof(struct sockaddr_in); 125 126 s = bind(fd, (struct sockaddr *) &sin, sin_len); 127 if (s == -1) 128 err(1, "bind"); 129 130 for (;;) { 131 ssize_t n; 132 char packet[10000]; 133 struct ip *ip_hdr; 134 struct tcpiphdr *tcpip_hdr; 135 char srcip[40], dstip[40]; 136 137 bzero(packet, sizeof(packet)); 138 n = recvfrom(fd, packet, sizeof(packet), 0, 139 (struct sockaddr *) &sin, &sin_len); 140 141 tcpip_hdr = (struct tcpiphdr *) packet; 142 ip_hdr = (struct ip *) packet; 143 144 bzero(srcip, sizeof(srcip)); 145 bzero(dstip, sizeof(dstip)); 146 147 if (inet_ntop(AF_INET, &ip_hdr->ip_src, srcip, 148 sizeof(srcip)) == NULL) { 149 fprintf(stderr, "Invalid IPv4 source packet\en"); 150 continue; 151 } 152 if (inet_ntop(AF_INET, &ip_hdr->ip_dst, dstip, 153 sizeof(dstip)) == NULL) { 154 fprintf(stderr, "Invalid IPv4 destination " 155 "packet\en"); 156 continue; 157 } 158 159 printf("%s:%u -> %s:%u\en", 160 srcip, 161 ntohs(tcpip_hdr->ti_sport), 162 dstip, 163 ntohs(tcpip_hdr->ti_dport) 164 ); 165 166 n = sendto(fd, packet, n, 0, (struct sockaddr *) &sin, 167 sin_len); 168 } 169 170 return 0; 171} 172.Ed 173.Sh SEE ALSO 174.Xr socket 2 , 175.Xr ip 4 , 176.Xr pf.conf 5 177.Sh HISTORY 178The 179.Nm 180protocol first appeared in 181.Ox 4.7 . 182