]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man4/divert.4
Refuse to unload the ipdivert module unless the 'force' flag is given to kldunload.
[FreeBSD/FreeBSD.git] / share / man / man4 / divert.4
1 .\" $FreeBSD$
2 .\"
3 .Dd October 22, 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 and minimal error checking is done.
61 Packets are written as either incoming or outgoing:
62 if
63 .Xr write 2
64 or
65 .Xr send 2
66 is used to deliver the packet, or if
67 .Xr sendto 2
68 is used with a destination IP address of
69 .Dv INADDR_ANY ,
70 then the packet is treated as if it were outgoing, i.e., destined
71 for a non-local address.
72 Otherwise, the packet is assumed to be
73 incoming and full packet routing is done.
74 .Pp
75 In the latter case, the
76 IP address specified must match the address of some local interface,
77 or an interface name
78 must be found after the IP address.
79 If an interface name is found,
80 that interface will be used and the value of the IP address will be
81 ignored (other than the fact that it is not
82 .Dv INADDR_ANY ) .
83 This is to indicate on which interface the packet
84 .Dq arrived .
85 .Pp
86 Normally, packets read as incoming should be written as incoming;
87 similarly for outgoing packets.
88 When reading and then writing back
89 packets, passing the same socket address supplied by
90 .Xr recvfrom 2
91 unmodified to
92 .Xr sendto 2
93 simplifies things (see below).
94 .Pp
95 The port part of the socket address passed to the
96 .Xr sendto 2
97 contains a tag that should be meaningful to the diversion module.
98 In the
99 case of
100 .Xr ipfw 8
101 the tag is interpreted as the rule number
102 .Em after which
103 rule processing should restart.
104 .Sh LOOP AVOIDANCE
105 Packets written into a divert socket
106 (using
107 .Xr sendto 2 )
108 re-enter the packet filter at the rule number
109 following the tag given in the port part of the socket address, which
110 is usually already set at the rule number that caused the diversion
111 (not the next rule if there are several at the same number).
112 If the 'tag'
113 is altered to indicate an alternative re-entry point, care should be taken
114 to avoid loops, where the same packet is diverted more than once at the
115 same rule.
116 .Sh DETAILS
117 To enable divert sockets, your kernel must be compiled with the option
118 .Dv IPDIVERT
119 or you have to load the
120 .Dv IPDIVERT
121 module.
122 .Pp
123 You can load the
124 .Dv IPDIVERT
125 module at runtime by issuing the following command:
126 .Bd -literal -offset indent
127 kldload ipdivert
128 .Ed
129 .Pp
130 If a packet is diverted but no socket is bound to the
131 port, or if
132 .Dv IPDIVERT
133 is not enabled or loaded in the kernel, the packet is dropped.
134 .Pp
135 Incoming packet fragments which get diverted are fully reassembled
136 before delivery; the diversion of any one fragment causes the entire
137 packet to get diverted.
138 If different fragments divert to different ports,
139 then which port ultimately gets chosen is unpredictable.
140 .Pp
141 Note that packets arriving on the divert socket by the
142 .Xr ipfw 8
143 .Cm tee
144 action are delivered as-is and packet fragments do not get reassembled
145 in this case.
146 .Pp
147 Packets are received and sent unchanged, except that
148 packets read as outgoing have invalid IP header checksums, and
149 packets written as outgoing have their IP header checksums overwritten
150 with the correct value.
151 Packets written as incoming and having incorrect checksums will be dropped.
152 Otherwise, all header fields are unchanged (and therefore in network order).
153 .Pp
154 Binding to port numbers less than 1024 requires super-user access, as does
155 creating a socket of type SOCK_RAW.
156 .Sh ERRORS
157 Writing to a divert socket can return these errors, along with
158 the usual errors possible when writing raw packets:
159 .Bl -tag -width Er
160 .It Bq Er EINVAL
161 The packet had an invalid header, or the IP options in the packet
162 and the socket options set were incompatible.
163 .It Bq Er EADDRNOTAVAIL
164 The destination address contained an IP address not equal to
165 .Dv INADDR_ANY
166 that was not associated with any interface.
167 .El
168 .Sh SEE ALSO
169 .Xr bind 2 ,
170 .Xr recvfrom 2 ,
171 .Xr sendto 2 ,
172 .Xr socket 2 ,
173 .Xr ipfw 8
174 .Sh BUGS
175 This is an attempt to provide a clean way for user mode processes
176 to implement various IP tricks like address translation, but it
177 could be cleaner, and it's too dependent on
178 .Xr ipfw 8 .
179 .Pp
180 It's questionable whether incoming fragments should be reassembled
181 before being diverted.
182 For example, if only some fragments of a
183 packet destined for another machine don't get routed through the
184 local machine, the packet is lost.
185 This should probably be
186 a settable socket option in any case.
187 .Sh AUTHORS
188 .An Archie Cobbs Aq archie@FreeBSD.org ,
189 Whistle Communications Corp.