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