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