1.\" $OpenBSD: divert.4,v 1.6 2009/12/06 18:32:43 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: December 6 2009 $ 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 SEE ALSO 81.Xr socket 2 , 82.Xr ip 4 , 83.Xr pf.conf 5 84.Sh HISTORY 85The 86.Nm 87protocol first appeared in 88.Ox 4.7 . 89