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