xref: /freebsd/share/man/man4/divert.4 (revision a79b71281cd63ad7a6cc43a6d5673a2510b51630)
1.\" $FreeBSD$
2.\"
3.Dd June 18, 1996
4.Dt DIVERT 4
5.Os FreeBSD
6.Sh NAME
7.Nm divert
8.Nd kernel packet diversion mechanism
9.Sh SYNOPSIS
10.Fd #include <sys/types.h>
11.Fd #include <sys/socket.h>
12.Fd #include <netinet/in.h>
13.Ft int
14.Fn socket PF_INET SOCK_RAW IPPROTO_DIVERT
15.Sh DESCRIPTION
16.Pp
17Divert sockets are similar to raw IP sockets, except that they
18can be bound to a specific
19.Nm
20port via the
21.Xr bind 2
22system call.
23The IP address in the bind is ignored; only the port
24number is significant.
25A divert socket bound to a divert port will receive all packets diverted
26to that port by some (here unspecified) kernel mechanism(s).
27Packets may also be written to a divert port, in which case they
28re-enter kernel IP packet processing.
29.Pp
30Divert sockets are normally used in conjunction with
31FreeBSD's packet filtering implementation and the
32.Xr ipfw 8
33program.
34By reading from and writing to a divert socket, matching packets
35can be passed through an arbitrary ``filter'' as they travel through
36the host machine, special routing tricks can be done, etc.
37.Sh READING PACKETS
38Packets are diverted either as they are ``incoming'' or ``outgoing.''
39Incoming packets are diverted after reception on an IP interface,
40whereas outgoing packets are diverted before next hop forwarding.
41.Pp
42Diverted packets may be read unaltered via
43.Xr read 2 ,
44.Xr recv 2 ,
45or
46.Xr recvfrom 2 .
47In the latter case, the address returned will have its port set to
48the some tag supplied by the packet diverter, (usually the ipfw rule number)
49and the IP address set to the (first) address of
50the interface on which the packet was received (if the packet
51was incoming) or
52.Dv INADDR_ANY
53(if the packet was outgoing). In the case of an incoming packet the interface
54name will also be placed in the 8 bytes following the address,
55(assuming it fits).
56.Sh WRITING PACKETS
57Writing to a divert socket is similar to writing to a raw IP socket;
58the packet is injected ``as is'' into the normal kernel IP packet
59processing and minimal error checking is done.
60Packets are written as either incoming or outgoing:
61if
62.Xr write 2
63or
64.Xr send 2
65is used to deliver the packet, or if
66.Xr sendto 2
67is used with a destination IP address of
68.Dv INADDR_ANY ,
69then the packet is treated as if it were outgoing, i.e., destined
70for a non-local address.  Otherwise, the packet is assumed to be
71incoming and full packet routing is done.
72.Pp
73In the latter case, the
74IP address specified must match the address of some local interface,
75or an interface name
76must be found after the IP address.
77If an interface name is found,
78that interface will be used and the value of the IP address will be
79ignored (other than the fact that it is not
80.Dv INADDR_ANY
81).
82This is to indicate on which interface the packet ``arrived.''
83.Pp
84Normally, packets read as incoming should be written as incoming;
85similarly for outgoing packets.  When reading and then writing back
86packets, passing the same socket address supplied by
87.Xr recvfrom 2
88unmodified to
89.Xr sendto 2
90simplifies things (see below).
91.Pp
92The port part of the socket address passed to the
93.Xr sendto 2
94contains a tag that should be meaningful to the diversion module.
95In the
96case of
97.Xr Ipfw 8
98the tag is interpretted as the rule number
99.Em after which
100rule processing should restart.
101.Sh LOOP AVOIDANCE
102Packets written into a divert socket
103.Po
104using
105.Xr sendto 2
106.Pc
107re-enter the packet filter at the rule number
108following the tag given in the port part of the socket address, which
109is usually already set at the rule number that caused the diversion
110(not the next rule if there are several at the same number). If the 'tag'
111is altered to indicate an alternative re-entry point, care should be taken
112to avoid loops, where the same packet is diverted more than once at the
113same rule.
114.Sh DETAILS
115To enable divert sockets, your kernel must be compiled with the option
116.Dv IPDIVERT .
117.Pp
118If a packet is diverted but no socket is bound to the
119port, or if
120.Dv IPDIVERT
121is not enabled in the kernel, the packet is dropped.
122.Pp
123Incoming packet fragments which get diverted are fully reassembled
124before delivery; the diversion of any one fragment causes the entire
125packet to get diverted.
126If different fragments divert to different ports,
127then which port ultimately gets chosen is unpredictable.
128.Pp
129Packets are received and sent unchanged, except that
130packets written as outgoing have their IP header checksums overwritten
131with the correct value.
132Packets written as incoming and having incorrect checksums will be dropped.
133Otherwise, all header fields are unchanged (and therefore in network order).
134.Pp
135Binding to port numbers less than 1024 requires super-user access, as does
136creating a socket of type SOCK_RAW.
137.Sh ERRORS
138Writing to a divert socket can return these errors, along with
139the usual errors possible when writing raw packets:
140.Bl -tag -width Er
141.It Bq Er EINVAL
142The packet had an invalid header, or the IP options in the packet
143and the socket options set were incompatible.
144.It Bq Er EADDRNOTAVAIL
145The destination address contained an IP address not equal to
146.Dv INADDR_ANY
147that was not associated with any interface.
148.El
149.Sh SEE ALSO
150.Xr bind 2 ,
151.Xr recvfrom 2 ,
152.Xr sendto 2 ,
153.Xr socket 2 ,
154.Xr ipfw 8
155.Sh BUGS
156This is an attempt to provide a clean way for user mode processes
157to implement various IP tricks like address translation, but it
158could be cleaner, and it's too dependent on
159.Xr ipfw 8 .
160.Pp
161It's questionable whether incoming fragments should be reassembled
162before being diverted.
163For example, if only some fragments of a
164packet destined for another machine don't get routed through the
165local machine, the packet is lost.
166This should probably be
167a settable socket option in any case.
168.Sh AUTHORS
169.An Archie Cobbs Aq archie@whistle.com ,
170Whistle Communications Corp.
171