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