xref: /freebsd/share/man/man4/divert.4 (revision 85432d406559e255179b504707f73328f6447a6d)
17f3dea24SPeter Wemm.\" $FreeBSD$
20b992c1dSWolfram Schneider.\"
324fc79b0SAndre Oppermann.Dd October 22, 2004
493e0e116SJulian Elischer.Dt DIVERT 4
53d45e180SRuslan Ermilov.Os
693e0e116SJulian Elischer.Sh NAME
793e0e116SJulian Elischer.Nm divert
893e0e116SJulian Elischer.Nd kernel packet diversion mechanism
993e0e116SJulian Elischer.Sh SYNOPSIS
1032eef9aeSRuslan Ermilov.In sys/types.h
1132eef9aeSRuslan Ermilov.In sys/socket.h
1232eef9aeSRuslan Ermilov.In netinet/in.h
1393e0e116SJulian Elischer.Ft int
1493e0e116SJulian Elischer.Fn socket PF_INET SOCK_RAW IPPROTO_DIVERT
1593e0e116SJulian Elischer.Sh DESCRIPTION
1693e0e116SJulian ElischerDivert sockets are similar to raw IP sockets, except that they
1793e0e116SJulian Elischercan be bound to a specific
1893e0e116SJulian Elischer.Nm
1993e0e116SJulian Elischerport via the
2093e0e116SJulian Elischer.Xr bind 2
216d249eeeSSheldon Hearnsystem call.
226d249eeeSSheldon HearnThe IP address in the bind is ignored; only the port
2393e0e116SJulian Elischernumber is significant.
2493e0e116SJulian ElischerA divert socket bound to a divert port will receive all packets diverted
2593e0e116SJulian Elischerto that port by some (here unspecified) kernel mechanism(s).
2693e0e116SJulian ElischerPackets may also be written to a divert port, in which case they
2793e0e116SJulian Elischerre-enter kernel IP packet processing.
2893e0e116SJulian Elischer.Pp
2993e0e116SJulian ElischerDivert sockets are normally used in conjunction with
30b5c508fbSRuslan Ermilov.Fx Ns 's
31b5c508fbSRuslan Ermilovpacket filtering implementation and the
3293e0e116SJulian Elischer.Xr ipfw 8
336d249eeeSSheldon Hearnprogram.
346d249eeeSSheldon HearnBy reading from and writing to a divert socket, matching packets
3593e0e116SJulian Elischercan be passed through an arbitrary ``filter'' as they travel through
3693e0e116SJulian Elischerthe host machine, special routing tricks can be done, etc.
3793e0e116SJulian Elischer.Sh READING PACKETS
3893e0e116SJulian ElischerPackets are diverted either as they are ``incoming'' or ``outgoing.''
3993e0e116SJulian ElischerIncoming packets are diverted after reception on an IP interface,
4093e0e116SJulian Elischerwhereas outgoing packets are diverted before next hop forwarding.
4193e0e116SJulian Elischer.Pp
4293e0e116SJulian ElischerDiverted packets may be read unaltered via
4393e0e116SJulian Elischer.Xr read 2 ,
4493e0e116SJulian Elischer.Xr recv 2 ,
4593e0e116SJulian Elischeror
4693e0e116SJulian Elischer.Xr recvfrom 2 .
4793e0e116SJulian ElischerIn the latter case, the address returned will have its port set to
4871678683SGiorgos Keramidassome tag supplied by the packet diverter, (usually the ipfw rule number)
499355ecfcSJulian Elischerand the IP address set to the (first) address of
5009b4b086SMike Pritchardthe interface on which the packet was received (if the packet
5193e0e116SJulian Elischerwas incoming) or
5293e0e116SJulian Elischer.Dv INADDR_ANY
53a10c9747SDaniel Harris(if the packet was outgoing).
54a10c9747SDaniel HarrisThe interface name (if defined
55a10c9747SDaniel Harrisfor the packet) will be placed in the 8 bytes following the address,
56a10c9747SDaniel Harrisif it fits.
5793e0e116SJulian Elischer.Sh WRITING PACKETS
5893e0e116SJulian ElischerWriting to a divert socket is similar to writing to a raw IP socket;
5993e0e116SJulian Elischerthe packet is injected ``as is'' into the normal kernel IP packet
6085432d40SGleb Smirnoffprocessing using
6185432d40SGleb Smirnoff.Xr sendto 2
6285432d40SGleb Smirnoffand minimal error checking is done.
6385432d40SGleb SmirnoffPackets are distinguished as either incoming or outgoing.
6485432d40SGleb SmirnoffIf
6593e0e116SJulian Elischer.Xr sendto 2
6693e0e116SJulian Elischeris used with a destination IP address of
6793e0e116SJulian Elischer.Dv INADDR_ANY ,
6893e0e116SJulian Elischerthen the packet is treated as if it were outgoing, i.e., destined
69b5e7e999SRuslan Ermilovfor a non-local address.
70b5e7e999SRuslan ErmilovOtherwise, the packet is assumed to be
7193e0e116SJulian Elischerincoming and full packet routing is done.
7293e0e116SJulian Elischer.Pp
7393e0e116SJulian ElischerIn the latter case, the
74436c7212SJulian ElischerIP address specified must match the address of some local interface,
75436c7212SJulian Elischeror an interface name
766d249eeeSSheldon Hearnmust be found after the IP address.
776d249eeeSSheldon HearnIf an interface name is found,
78436c7212SJulian Elischerthat interface will be used and the value of the IP address will be
79436c7212SJulian Elischerignored (other than the fact that it is not
8094ba280cSRuslan Ermilov.Dv INADDR_ANY ) .
81b5e7e999SRuslan ErmilovThis is to indicate on which interface the packet
82b5e7e999SRuslan Ermilov.Dq arrived .
8393e0e116SJulian Elischer.Pp
8493e0e116SJulian ElischerNormally, packets read as incoming should be written as incoming;
85b5e7e999SRuslan Ermilovsimilarly for outgoing packets.
86b5e7e999SRuslan ErmilovWhen reading and then writing back
8793e0e116SJulian Elischerpackets, passing the same socket address supplied by
8893e0e116SJulian Elischer.Xr recvfrom 2
8993e0e116SJulian Elischerunmodified to
9093e0e116SJulian Elischer.Xr sendto 2
919355ecfcSJulian Elischersimplifies things (see below).
929355ecfcSJulian Elischer.Pp
939355ecfcSJulian ElischerThe port part of the socket address passed to the
949355ecfcSJulian Elischer.Xr sendto 2
956d249eeeSSheldon Hearncontains a tag that should be meaningful to the diversion module.
966d249eeeSSheldon HearnIn the
979355ecfcSJulian Elischercase of
98f38ca148SRuslan Ermilov.Xr ipfw 8
99f38ca148SRuslan Ermilovthe tag is interpreted as the rule number
1009355ecfcSJulian Elischer.Em after which
1019355ecfcSJulian Elischerrule processing should restart.
10293e0e116SJulian Elischer.Sh LOOP AVOIDANCE
103d7ec3e91SRuslan ErmilovPackets written into a divert socket
104c4d9468eSRuslan Ermilov(using
105c4d9468eSRuslan Ermilov.Xr sendto 2 )
106d7ec3e91SRuslan Ermilovre-enter the packet filter at the rule number
1079355ecfcSJulian Elischerfollowing the tag given in the port part of the socket address, which
1089355ecfcSJulian Elischeris usually already set at the rule number that caused the diversion
1095203edcdSRuslan Ermilov(not the next rule if there are several at the same number).
1105203edcdSRuslan ErmilovIf the 'tag'
1119355ecfcSJulian Elischeris altered to indicate an alternative re-entry point, care should be taken
1129355ecfcSJulian Elischerto avoid loops, where the same packet is diverted more than once at the
1139355ecfcSJulian Elischersame rule.
11493e0e116SJulian Elischer.Sh DETAILS
11593e0e116SJulian ElischerTo enable divert sockets, your kernel must be compiled with the option
11624fc79b0SAndre Oppermann.Dv IPDIVERT
11724fc79b0SAndre Oppermannor you have to load the
11824fc79b0SAndre Oppermann.Dv IPDIVERT
11924fc79b0SAndre Oppermannmodule.
12024fc79b0SAndre Oppermann.Pp
12124fc79b0SAndre OppermannYou can load the
12224fc79b0SAndre Oppermann.Dv IPDIVERT
12324fc79b0SAndre Oppermannmodule at runtime by issuing the following command:
12424fc79b0SAndre Oppermann.Bd -literal -offset indent
12524fc79b0SAndre Oppermannkldload ipdivert
12624fc79b0SAndre Oppermann.Ed
12793e0e116SJulian Elischer.Pp
12893e0e116SJulian ElischerIf a packet is diverted but no socket is bound to the
12993e0e116SJulian Elischerport, or if
13093e0e116SJulian Elischer.Dv IPDIVERT
13124fc79b0SAndre Oppermannis not enabled or loaded in the kernel, the packet is dropped.
13293e0e116SJulian Elischer.Pp
13393e0e116SJulian ElischerIncoming packet fragments which get diverted are fully reassembled
13493e0e116SJulian Elischerbefore delivery; the diversion of any one fragment causes the entire
13593e0e116SJulian Elischerpacket to get diverted.
13693e0e116SJulian ElischerIf different fragments divert to different ports,
13793e0e116SJulian Elischerthen which port ultimately gets chosen is unpredictable.
13893e0e116SJulian Elischer.Pp
13913177659SAndre OppermannNote that packets arriving on the divert socket by the
14013177659SAndre Oppermann.Xr ipfw 8
14113177659SAndre Oppermann.Cm tee
14213177659SAndre Oppermannaction are delivered as-is and packet fragments do not get reassembled
14313177659SAndre Oppermannin this case.
14413177659SAndre Oppermann.Pp
14504f36f75SBrian SomersPackets are received and sent unchanged, except that
146dd121c1eSArchie Cobbspackets read as outgoing have invalid IP header checksums, and
14704f36f75SBrian Somerspackets written as outgoing have their IP header checksums overwritten
14893e0e116SJulian Elischerwith the correct value.
14993e0e116SJulian ElischerPackets written as incoming and having incorrect checksums will be dropped.
15093e0e116SJulian ElischerOtherwise, all header fields are unchanged (and therefore in network order).
15193e0e116SJulian Elischer.Pp
15204f36f75SBrian SomersBinding to port numbers less than 1024 requires super-user access, as does
15304f36f75SBrian Somerscreating a socket of type SOCK_RAW.
15493e0e116SJulian Elischer.Sh ERRORS
15593e0e116SJulian ElischerWriting to a divert socket can return these errors, along with
15693e0e116SJulian Elischerthe usual errors possible when writing raw packets:
15793e0e116SJulian Elischer.Bl -tag -width Er
15893e0e116SJulian Elischer.It Bq Er EINVAL
15993e0e116SJulian ElischerThe packet had an invalid header, or the IP options in the packet
16093e0e116SJulian Elischerand the socket options set were incompatible.
16193e0e116SJulian Elischer.It Bq Er EADDRNOTAVAIL
16293e0e116SJulian ElischerThe destination address contained an IP address not equal to
16393e0e116SJulian Elischer.Dv INADDR_ANY
16493e0e116SJulian Elischerthat was not associated with any interface.
16593e0e116SJulian Elischer.El
16693e0e116SJulian Elischer.Sh SEE ALSO
16793e0e116SJulian Elischer.Xr bind 2 ,
1680b992c1dSWolfram Schneider.Xr recvfrom 2 ,
169aab5e1b6SMike Pritchard.Xr sendto 2 ,
1700b992c1dSWolfram Schneider.Xr socket 2 ,
1710b992c1dSWolfram Schneider.Xr ipfw 8
17293e0e116SJulian Elischer.Sh BUGS
17393e0e116SJulian ElischerThis is an attempt to provide a clean way for user mode processes
17493e0e116SJulian Elischerto implement various IP tricks like address translation, but it
17593e0e116SJulian Elischercould be cleaner, and it's too dependent on
17693e0e116SJulian Elischer.Xr ipfw 8 .
17793e0e116SJulian Elischer.Pp
17893e0e116SJulian ElischerIt's questionable whether incoming fragments should be reassembled
1796d249eeeSSheldon Hearnbefore being diverted.
1806d249eeeSSheldon HearnFor example, if only some fragments of a
18193e0e116SJulian Elischerpacket destined for another machine don't get routed through the
1826d249eeeSSheldon Hearnlocal machine, the packet is lost.
1836d249eeeSSheldon HearnThis should probably be
18493e0e116SJulian Elischera settable socket option in any case.
185aaf1f16eSPhilippe Charnier.Sh AUTHORS
186eddc45e7SJeroen Ruigrok van der Werven.An Archie Cobbs Aq archie@FreeBSD.org ,
187aaf1f16eSPhilippe CharnierWhistle Communications Corp.
188