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