summaryrefslogtreecommitdiff
path: root/share/man/man4/divert.4
blob: a52d6d416cb94f5e318ded60fdc919ee9bf34575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
.\"     $OpenBSD: divert.4,v 1.10 2012/10/21 15:25:42 jmc Exp $
.\"
.\" Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: October 21 2012 $
.Dt DIVERT 4
.Os
.Sh NAME
.Nm divert
.Nd kernel packet diversion mechanism
.Sh SYNOPSIS
.Fd #include <sys/types.h>
.Fd #include <sys/socket.h>
.Fd #include <netinet/in.h>
.Ft int
.Fn socket AF_INET SOCK_RAW IPPROTO_DIVERT
.Ft int
.Fn socket AF_INET6 SOCK_RAW IPPROTO_DIVERT
.Sh DESCRIPTION
Divert sockets are part of a mechanism completely integrated with
.Xr pf 4
that queues raw packets from the kernel stack to userspace applications,
and vice versa.
.Pp
A divert socket must be bound to a divert port through
.Xr bind 2 ,
which only the superuser can do.
Divert ports have their own number space, completely separated from
.Xr tcp 4
and
.Xr udp 4 .
When
.Xr pf 4
processes a packet that matches a divert rule (see
.Xr pf.conf 5
for details) it is immediately sent to the divert socket listening on the
port specified in the rule.
.Xr 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.
.Pp
Packets can be read via
.Xr read 2 ,
.Xr recv 2 ,
or
.Xr 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.
.Pp
Writing to a divert socket can be achieved using
.Xr sendto 2
and it will skip
.Xr pf 4
filters to avoid loops.
A diverted packet that is not reinjected into the kernel stack is lost.
.Pp
Receive and send divert socket buffer space can be tuned through
.Xr sysctl 8 .
.Xr netstat 1
shows information relevant to divert sockets.
.Pp
The IP_DIVERTFL socket option on the IPPROTO_IP level controls if inbound and
outbound packets are diverted (the default) or only one direction.
Only the superuser can change this setting, and it cannot be reset once set.
Valid values are
.Dv IPPROTO_DIVERT_INIT
for the direction of the initial packet of a flow, and
.Dv IPPROTO_DIVERT_RESP
for the direction of the response packets.
.Sh 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:
.Bd -literal -offset indent
pass out on em0 inet proto tcp to port 80 divert-packet port 700
.Ed
.Pp
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.
.Bd -literal
#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\en");
			continue;
		}
		if (inet_ntop(AF_INET, &ip_hdr->ip_dst, dstip,
		    sizeof(dstip)) == NULL) {
			fprintf(stderr, "Invalid IPv4 destination "
			    "packet\en");
			continue;
		}

		printf("%s:%u -> %s:%u\en",
			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;
}
.Ed
.Sh SEE ALSO
.Xr setsockopt 2 ,
.Xr socket 2 ,
.Xr ip 4 ,
.Xr pf.conf 5
.Sh HISTORY
The
.Nm
protocol first appeared in
.Ox 4.7 .