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