xref: /freebsd/sys/netinet/libalias/libalias.3 (revision 1a0a934547909744a6a2fa4cfd5b795ec6394f05)
1f987e1bdSBrian Somers.\"-
2e83aaae3SBrian Somers.\" Copyright (c) 2001 Charles Mott <cm@linktel.net>
3f987e1bdSBrian Somers.\" All rights reserved.
4f987e1bdSBrian Somers.\"
5f987e1bdSBrian Somers.\" Redistribution and use in source and binary forms, with or without
6f987e1bdSBrian Somers.\" modification, are permitted provided that the following conditions
7f987e1bdSBrian Somers.\" are met:
8f987e1bdSBrian Somers.\" 1. Redistributions of source code must retain the above copyright
9f987e1bdSBrian Somers.\"    notice, this list of conditions and the following disclaimer.
10f987e1bdSBrian Somers.\" 2. Redistributions in binary form must reproduce the above copyright
11f987e1bdSBrian Somers.\"    notice, this list of conditions and the following disclaimer in the
12f987e1bdSBrian Somers.\"    documentation and/or other materials provided with the distribution.
13f987e1bdSBrian Somers.\"
14f987e1bdSBrian Somers.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f987e1bdSBrian Somers.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f987e1bdSBrian Somers.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f987e1bdSBrian Somers.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f987e1bdSBrian Somers.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f987e1bdSBrian Somers.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f987e1bdSBrian Somers.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f987e1bdSBrian Somers.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f987e1bdSBrian Somers.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f987e1bdSBrian Somers.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f987e1bdSBrian Somers.\" SUCH DAMAGE.
25f987e1bdSBrian Somers.\"
267f3dea24SPeter Wemm.\" $FreeBSD$
2774804d58SMike Pritchard.\"
28c9a24641SRuslan Ermilov.Dd January 17, 2004
2974804d58SMike Pritchard.Dt LIBALIAS 3
30a307d598SRuslan Ermilov.Os
313efa11bbSBrian Somers.Sh NAME
3274804d58SMike Pritchard.Nm libalias
33a2900666SRuslan Ermilov.Nd packet aliasing library for masquerading and network address translation
343efa11bbSBrian Somers.Sh SYNOPSIS
3532eef9aeSRuslan Ermilov.In sys/types.h
3632eef9aeSRuslan Ermilov.In netinet/in.h
3732eef9aeSRuslan Ermilov.In alias.h
38a2900666SRuslan Ermilov.Pp
39a2900666SRuslan ErmilovFunction prototypes are given in the main body of the text.
4074804d58SMike Pritchard.Sh DESCRIPTION
4174804d58SMike PritchardThe
4274804d58SMike Pritchard.Nm
43a2900666SRuslan Ermilovlibrary is a collection of functions for aliasing and de-aliasing of IP
44a2900666SRuslan Ermilovpackets, intended for masquerading and network address translation (NAT).
45a2900666SRuslan Ermilov.Sh INTRODUCTION
46a2900666SRuslan ErmilovThis library is a moderately portable set of functions designed to assist
47a2900666SRuslan Ermilovin the process of IP masquerading and network address translation.
48a2900666SRuslan ErmilovOutgoing packets from a local network with unregistered IP addresses can
49a2900666SRuslan Ermilovbe aliased to appear as if they came from an accessible IP address.
50a2900666SRuslan ErmilovIncoming packets are then de-aliased so that they are sent to the correct
51a2900666SRuslan Ermilovmachine on the local network.
52a2900666SRuslan Ermilov.Pp
53a2900666SRuslan ErmilovA certain amount of flexibility is built into the packet aliasing engine.
54a2900666SRuslan ErmilovIn the simplest mode of operation, a many-to-one address mapping takes
55a2900666SRuslan Ermilovplace between local network and the packet aliasing host.
56a2900666SRuslan ErmilovThis is known as IP masquerading.
57a2900666SRuslan ErmilovIn addition, one-to-one mappings between local and public addresses can
58a2900666SRuslan Ermilovalso be implemented, which is known as static NAT.
59a2900666SRuslan ErmilovIn between these extremes, different groups of private addresses can be
60a2900666SRuslan Ermilovlinked to different public addresses, comprising several distinct
61a2900666SRuslan Ermilovmany-to-one mappings.
62a2900666SRuslan ErmilovAlso, a given public address and port can be statically redirected to a
63a2900666SRuslan Ermilovprivate address/port.
64a2900666SRuslan Ermilov.Pp
65a2900666SRuslan ErmilovThe packet aliasing engine was designed to operate in user space outside
66a2900666SRuslan Ermilovof the kernel, without any access to private kernel data structure, but
67a2900666SRuslan Ermilovthe source code can also be ported to a kernel environment.
68a2900666SRuslan Ermilov.Sh INITIALIZATION AND CONTROL
69f1a529f3SRuslan ErmilovOne special function,
705e289f9eSPoul-Henning Kamp.Fn LibAliasInit ,
715e289f9eSPoul-Henning Kampmust always be called before any packet handling may be performed and
725e289f9eSPoul-Henning Kampthe returned instance pointer passed to all the other functions.
73f1a529f3SRuslan ErmilovNormally, the
745e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
75f1a529f3SRuslan Ermilovfunction is called afterwards, to set the default aliasing address.
76a2900666SRuslan ErmilovIn addition, the operating mode of the packet aliasing engine can be
77a2900666SRuslan Ermilovcustomized by calling
785e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode .
79a2900666SRuslan Ermilov.Pp
805e289f9eSPoul-Henning Kamp.Ft "struct libalias *"
815e289f9eSPoul-Henning Kamp.Fn LibAliasInit "struct libalias *"
82a2900666SRuslan Ermilov.Bd -ragged -offset indent
835e289f9eSPoul-Henning KampThis function is used to initialize
84a2900666SRuslan Ermilovinternal data structures.
85c9a24641SRuslan ErmilovWhen called the first time, a
86c9a24641SRuslan Ermilov.Dv NULL
87c9a24641SRuslan Ermilovpointer should be passed as an argument.
88a2900666SRuslan ErmilovThe following mode bits are always set after calling
895e289f9eSPoul-Henning Kamp.Fn LibAliasInit .
90a2900666SRuslan ErmilovSee the description of
915e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode
92a2900666SRuslan Ermilovbelow for the meaning of these mode bits.
93a2900666SRuslan Ermilov.Pp
94a2900666SRuslan Ermilov.Bl -item -offset indent -compact
95a2900666SRuslan Ermilov.It
96a2900666SRuslan Ermilov.Dv PKT_ALIAS_SAME_PORTS
97a2900666SRuslan Ermilov.It
98a2900666SRuslan Ermilov.Dv PKT_ALIAS_USE_SOCKETS
99a2900666SRuslan Ermilov.It
100a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
101a2900666SRuslan Ermilov.El
102a2900666SRuslan Ermilov.Pp
103a2900666SRuslan ErmilovThis function will always return the packet aliasing engine to the same
104a2900666SRuslan Ermilovinitial state.
1059703a107SPhilippe CharnierThe
1065e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
1079703a107SPhilippe Charnierfunction is normally called afterwards, and any desired changes from the
1089703a107SPhilippe Charnierdefault mode bits listed above require a call to
1095e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode .
110a2900666SRuslan Ermilov.Pp
111a2900666SRuslan ErmilovIt is mandatory that this function be called at the beginning of a program
112a2900666SRuslan Ermilovprior to any packet handling.
113a2900666SRuslan Ermilov.Ed
114a2900666SRuslan Ermilov.Pp
1158ddc51bcSEivind Eklund.Ft void
1165e289f9eSPoul-Henning Kamp.Fn LibAliasUninit "struct libalias *"
117a2900666SRuslan Ermilov.Bd -ragged -offset indent
1185e289f9eSPoul-Henning KampThis function has no return value and is used to clear any
119a2900666SRuslan Ermilovresources attached to internal data structures.
120a2900666SRuslan Ermilov.Pp
121a2900666SRuslan ErmilovThis functions should be called when a program stops using the aliasing
122a2900666SRuslan Ermilovengine; it does, amongst other things, clear out any firewall holes.
123a2900666SRuslan ErmilovTo provide backwards compatibility and extra security, it is added to
124a2900666SRuslan Ermilovthe
125a2900666SRuslan Ermilov.Xr atexit 3
126a2900666SRuslan Ermilovchain by
1275e289f9eSPoul-Henning Kamp.Fn LibAliasInit .
128a2900666SRuslan Ermilov.Ed
129a2900666SRuslan Ermilov.Pp
1303efa11bbSBrian Somers.Ft void
1315e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr"
132a2900666SRuslan Ermilov.Bd -ragged -offset indent
133a2900666SRuslan ErmilovThis function sets the source address to which outgoing packets from the
134a2900666SRuslan Ermilovlocal area network are aliased.
135a2900666SRuslan ErmilovAll outgoing packets are re-mapped to this address unless overridden by a
136a2900666SRuslan Ermilovstatic address mapping established by
1375e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr .
138f1a529f3SRuslan ErmilovIf this function is not called, and no static rules match, an outgoing
139f1a529f3SRuslan Ermilovpacket retains its source address.
140a2900666SRuslan Ermilov.Pp
141a2900666SRuslan ErmilovIf the
142a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
143a2900666SRuslan Ermilovmode bit is set (the default mode of operation), then the internal aliasing
144a2900666SRuslan Ermilovlink tables will be reset any time the aliasing address changes.
145a2900666SRuslan ErmilovThis is useful for interfaces such as
146a2900666SRuslan Ermilov.Xr ppp 8 ,
147a2900666SRuslan Ermilovwhere the IP
148a2900666SRuslan Ermilovaddress may or may not change on successive dial-up attempts.
149a2900666SRuslan Ermilov.Pp
150a2900666SRuslan ErmilovIf the
151a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
152a2900666SRuslan Ermilovmode bit is set to zero, this function can also be used to dynamically change
153a2900666SRuslan Ermilovthe aliasing address on a packet to packet basis (it is a low overhead call).
154a2900666SRuslan Ermilov.Pp
155a2900666SRuslan ErmilovIt is mandatory that this function be called prior to any packet handling.
156a2900666SRuslan Ermilov.Ed
157a2900666SRuslan Ermilov.Pp
158442a25bdSBruce Evans.Ft unsigned int
1595e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask"
160a2900666SRuslan Ermilov.Bd -ragged -offset indent
1613efa11bbSBrian SomersThis function sets or clears mode bits
1623efa11bbSBrian Somersaccording to the value of
163a2900666SRuslan Ermilov.Fa flags .
1643efa11bbSBrian SomersOnly bits marked in
165a2900666SRuslan Ermilov.Fa mask
166a2900666SRuslan Ermilovare affected.
167a2900666SRuslan ErmilovThe following mode bits are defined in
168fe08efe6SRuslan Ermilov.In alias.h :
169a2900666SRuslan Ermilov.Bl -tag -width indent
170a2900666SRuslan Ermilov.It Dv PKT_ALIAS_LOG
171a2900666SRuslan ErmilovEnables logging into
172a2900666SRuslan Ermilov.Pa /var/log/alias.log .
173a2900666SRuslan ErmilovEach time an aliasing link is created or deleted, the log file is appended
174a2900666SRuslan Ermilovwith the current number of ICMP, TCP and UDP links.
175a2900666SRuslan ErmilovMainly useful for debugging when the log file is viewed continuously with
176a2900666SRuslan Ermilov.Xr tail 1 .
177a2900666SRuslan Ermilov.It Dv PKT_ALIAS_DENY_INCOMING
178a2900666SRuslan ErmilovIf this mode bit is set, all incoming packets associated with new TCP
179a2900666SRuslan Ermilovconnections or new UDP transactions will be marked for being ignored
1805e289f9eSPoul-Henning Kamp.Fn ( LibAliasIn
181a2900666SRuslan Ermilovreturns
182a2900666SRuslan Ermilov.Dv PKT_ALIAS_IGNORED
183c4d9468eSRuslan Ermilovcode)
184a2900666SRuslan Ermilovby the calling program.
185a2900666SRuslan ErmilovResponse packets to connections or transactions initiated from the packet
186a2900666SRuslan Ermilovaliasing host or local network will be unaffected.
187a2900666SRuslan ErmilovThis mode bit is useful for implementing a one-way firewall.
188a2900666SRuslan Ermilov.It Dv PKT_ALIAS_SAME_PORTS
189a2900666SRuslan ErmilovIf this mode bit is set, the packet aliasing engine will attempt to leave
190a2900666SRuslan Ermilovthe alias port numbers unchanged from the actual local port numbers.
191a2900666SRuslan ErmilovThis can be done as long as the quintuple (proto, alias addr, alias port,
192a2900666SRuslan Ermilovremote addr, remote port) is unique.
193a2900666SRuslan ErmilovIf a conflict exists, a new aliasing port number is chosen even if this
194a2900666SRuslan Ermilovmode bit is set.
195a2900666SRuslan Ermilov.It Dv PKT_ALIAS_USE_SOCKETS
196a2900666SRuslan ErmilovThis bit should be set when the packet aliasing host originates network
197a2900666SRuslan Ermilovtraffic as well as forwards it.
198a2900666SRuslan ErmilovWhen the packet aliasing host is waiting for a connection from an unknown
1991a0a9345SRuslan Ermilovhost address or unknown port number (e.g.\& an FTP data connection), this
200a2900666SRuslan Ermilovmode bit specifies that a socket be allocated as a place holder to prevent
201a2900666SRuslan Ermilovport conflicts.
202a2900666SRuslan ErmilovOnce a connection is established, usually within a minute or so, the socket
203a2900666SRuslan Ermilovis closed.
204a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNREGISTERED_ONLY
205a2900666SRuslan ErmilovIf this mode bit is set, traffic on the local network which does not
206a2900666SRuslan Ermilovoriginate from unregistered address spaces will be ignored.
207a2900666SRuslan ErmilovStandard Class A, B and C unregistered addresses are:
2083efa11bbSBrian Somers.Bd -literal -offset indent
2093efa11bbSBrian Somers10.0.0.0     ->  10.255.255.255   (Class A subnet)
2103efa11bbSBrian Somers172.16.0.0   ->  172.31.255.255   (Class B subnets)
2113efa11bbSBrian Somers192.168.0.0  ->  192.168.255.255  (Class C subnets)
2123efa11bbSBrian Somers.Ed
213a2900666SRuslan Ermilov.Pp
214a2900666SRuslan ErmilovThis option is useful in the case that packet aliasing host has both
215a2900666SRuslan Ermilovregistered and unregistered subnets on different interfaces.
216a2900666SRuslan ErmilovThe registered subnet is fully accessible to the outside world, so traffic
217a2900666SRuslan Ermilovfrom it does not need to be passed through the packet aliasing engine.
218a2900666SRuslan Ermilov.It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
2193efa11bbSBrian SomersWhen this mode bit is set and
2205e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
221a2900666SRuslan Ermilovis called to change the aliasing address, the internal link table of the
222a2900666SRuslan Ermilovpacket aliasing engine will be cleared.
223a2900666SRuslan ErmilovThis operating mode is useful for
224a2900666SRuslan Ermilov.Xr ppp 8
225a2900666SRuslan Ermilovlinks where the interface address can sometimes change or remain the same
226a2900666SRuslan Ermilovbetween dial-up attempts.
227a2900666SRuslan ErmilovIf this mode bit is not set, the link table will never be reset in the event
228a2900666SRuslan Ermilovof an address change.
229a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PUNCH_FW
230a2900666SRuslan ErmilovThis option makes
231a2900666SRuslan Ermilov.Nm
232a2900666SRuslan Ermilov`punch holes' in an
233a2900666SRuslan Ermilov.Xr ipfirewall 4
234a2900666SRuslan Ermilovbased firewall for FTP/IRC DCC connections.
235a2900666SRuslan ErmilovThe holes punched are bound by from/to IP address and port; it will not be
236a2900666SRuslan Ermilovpossible to use a hole for another connection.
237a2900666SRuslan ErmilovA hole is removed when the connection that uses it dies.
238a2900666SRuslan ErmilovTo cater to unexpected death of a program using
239a2900666SRuslan Ermilov.Nm
2401a0a9345SRuslan Ermilov(e.g.\& kill -9),
241a2900666SRuslan Ermilovchanging the state of the flag will clear the entire firewall range
242a2900666SRuslan Ermilovallocated for holes.
2438ddc51bcSEivind EklundThis will also happen on the initial call to
2445e289f9eSPoul-Henning Kamp.Fn LibAliasSetFWBase .
245a2900666SRuslan ErmilovThis call must happen prior to setting this flag.
246a2900666SRuslan Ermilov.It Dv PKT_ALIAS_REVERSE
247a2900666SRuslan ErmilovThis option makes
248a2900666SRuslan Ermilov.Nm
249a2900666SRuslan Ermilovreverse the way it handles incoming and outgoing packets, allowing it
250a2900666SRuslan Ermilovto be fed with data that passes through the internal interface rather
251a2900666SRuslan Ermilovthan the external one.
252a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PROXY_ONLY
253a2900666SRuslan ErmilovThis option tells
254a2900666SRuslan Ermilov.Nm
255a2900666SRuslan Ermilovto obey transparent proxy rules only.
256a2900666SRuslan ErmilovNormal packet aliasing is not performed.
25721b9df57SBrian SomersSee
2585e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule
25921b9df57SBrian Somersbelow for details.
2603efa11bbSBrian Somers.El
261a2900666SRuslan Ermilov.Ed
262a2900666SRuslan Ermilov.Pp
2638ddc51bcSEivind Eklund.Ft void
2645e289f9eSPoul-Henning Kamp.Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num"
265a2900666SRuslan Ermilov.Bd -ragged -offset indent
266a2900666SRuslan ErmilovSet firewall range allocated for punching firewall holes (with the
267a2900666SRuslan Ermilov.Dv PKT_ALIAS_PUNCH_FW
268a2900666SRuslan Ermilovflag).
269a2900666SRuslan ErmilovThe range will be cleared for all rules on initialization.
270a2900666SRuslan Ermilov.Ed
271b07fbc17SJoe Marcus Clarke.Pp
272b07fbc17SJoe Marcus Clarke.Ft void
2735e289f9eSPoul-Henning Kamp.Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port"
274b07fbc17SJoe Marcus Clarke.Bd -ragged -offset indent
275b07fbc17SJoe Marcus ClarkeSet the TCP port used by the Skinny Station protocol.
276b07fbc17SJoe Marcus ClarkeSkinny is used by Cisco IP phones to communicate with
277b07fbc17SJoe Marcus ClarkeCisco Call Managers to set up voice over IP calls.
278b07fbc17SJoe Marcus ClarkeIf this is not set, Skinny aliasing will not be done.
279b07fbc17SJoe Marcus ClarkeThe typical port used by Skinny is 2000.
280b07fbc17SJoe Marcus Clarke.Ed
281a2900666SRuslan Ermilov.Sh PACKET HANDLING
282a2900666SRuslan ErmilovThe packet handling functions are used to modify incoming (remote to local)
283a2900666SRuslan Ermilovand outgoing (local to remote) packets.
284a2900666SRuslan ErmilovThe calling program is responsible for receiving and sending packets via
285a2900666SRuslan Ermilovnetwork interfaces.
286a2900666SRuslan Ermilov.Pp
287a2900666SRuslan ErmilovAlong with
2885e289f9eSPoul-Henning Kamp.Fn LibAliasInit
289a2900666SRuslan Ermilovand
2905e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress ,
291a2900666SRuslan Ermilovthe two packet handling functions,
2925e289f9eSPoul-Henning Kamp.Fn LibAliasIn
293a2900666SRuslan Ermilovand
2945e289f9eSPoul-Henning Kamp.Fn LibAliasOut ,
295a2900666SRuslan Ermilovcomprise minimal set of functions needed for a basic IP masquerading
296a2900666SRuslan Ermilovimplementation.
297a2900666SRuslan Ermilov.Pp
2983efa11bbSBrian Somers.Ft int
2995e289f9eSPoul-Henning Kamp.Fn LibAliasIn "struct libalias *" "char *buffer" "int maxpacketsize"
300a2900666SRuslan Ermilov.Bd -ragged -offset indent
301a2900666SRuslan ErmilovAn incoming packet coming from a remote machine to the local network is
302a2900666SRuslan Ermilovde-aliased by this function.
3033efa11bbSBrian SomersThe IP packet is pointed to by
304a2900666SRuslan Ermilov.Fa buffer ,
3053efa11bbSBrian Somersand
306a2900666SRuslan Ermilov.Fa maxpacketsize
307a2900666SRuslan Ermilovindicates the size of the data structure containing the packet and should
308a2900666SRuslan Ermilovbe at least as large as the actual packet size.
309a2900666SRuslan Ermilov.Pp
3103efa11bbSBrian SomersReturn codes:
311a2900666SRuslan Ermilov.Bl -tag -width indent
312a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK
3133efa11bbSBrian SomersThe packet aliasing process was successful.
314a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED
3153efa11bbSBrian SomersThe packet was ignored and not de-aliased.
316a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, possibly an ICMP message
317a2900666SRuslan Ermilovtype is not handled or if incoming packets for new connections are being
318a2900666SRuslan Ermilovignored (if
319a2900666SRuslan Ermilov.Dv PKT_ALIAS_DENY_INCOMING
320a2900666SRuslan Ermilovmode bit was set by
3215e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode ) .
322a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
323a2900666SRuslan ErmilovThis is returned when a fragment cannot be resolved because the header
324a2900666SRuslan Ermilovfragment has not been sent yet.
325a2900666SRuslan ErmilovIn this situation, fragments must be saved with
3265e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment
3273efa11bbSBrian Somersuntil a header fragment is found.
328a2900666SRuslan Ermilov.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
329a2900666SRuslan ErmilovThe packet aliasing process was successful, and a header fragment was found.
330a2900666SRuslan ErmilovThis is a signal to retrieve any unresolved fragments with
3315e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment
332a2900666SRuslan Ermilovand de-alias them with
3335e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn .
334a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR
335a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred.
3363efa11bbSBrian Somers.El
337a2900666SRuslan Ermilov.Ed
338a2900666SRuslan Ermilov.Pp
3393efa11bbSBrian Somers.Ft int
3405e289f9eSPoul-Henning Kamp.Fn LibAliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
341a2900666SRuslan Ermilov.Bd -ragged -offset indent
342a2900666SRuslan ErmilovAn outgoing packet coming from the local network to a remote machine is
343a2900666SRuslan Ermilovaliased by this function.
3443efa11bbSBrian SomersThe IP packet is pointed to by
345a2900666SRuslan Ermilov.Fa buffer ,
3463efa11bbSBrian Somersand
347a2900666SRuslan Ermilov.Fa maxpacketsize
348a2900666SRuslan Ermilovindicates the maximum packet size permissible should the packet length be
349a2900666SRuslan Ermilovchanged.
350a2900666SRuslan ErmilovIP encoding protocols place address and port information in the encapsulated
351a2900666SRuslan Ermilovdata stream which has to be modified and can account for changes in packet
352a2900666SRuslan Ermilovlength.
353a2900666SRuslan ErmilovWell known examples of such protocols are FTP and IRC DCC.
354a2900666SRuslan Ermilov.Pp
3553efa11bbSBrian SomersReturn codes:
356a2900666SRuslan Ermilov.Bl -tag -width indent
357a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK
3583efa11bbSBrian SomersThe packet aliasing process was successful.
359a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED
360a2900666SRuslan ErmilovThe packet was ignored and not aliased.
361a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, or possibly an ICMP message
362a2900666SRuslan Ermilovtype is not handled.
363a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR
364a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred.
3653efa11bbSBrian Somers.El
366a2900666SRuslan Ermilov.Ed
367a2900666SRuslan Ermilov.Sh PORT AND ADDRESS REDIRECTION
368a2900666SRuslan ErmilovThe functions described in this section allow machines on the local network
369a2900666SRuslan Ermilovto be accessible in some degree to new incoming connections from the external
370a2900666SRuslan Ermilovnetwork.
371a2900666SRuslan ErmilovIndividual ports can be re-mapped or static network address translations can
372a2900666SRuslan Ermilovbe designated.
373a2900666SRuslan Ermilov.Pp
3743efa11bbSBrian Somers.Ft struct alias_link *
3755e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectPort
3765e289f9eSPoul-Henning Kamp.Fa "struct libalias *"
3773efa11bbSBrian Somers.Fa "struct in_addr local_addr"
3783efa11bbSBrian Somers.Fa "u_short local_port"
3793efa11bbSBrian Somers.Fa "struct in_addr remote_addr"
3803efa11bbSBrian Somers.Fa "u_short remote_port"
3813efa11bbSBrian Somers.Fa "struct in_addr alias_addr"
3823efa11bbSBrian Somers.Fa "u_short alias_port"
3833efa11bbSBrian Somers.Fa "u_char proto"
3843efa11bbSBrian Somers.Fc
385a2900666SRuslan Ermilov.Bd -ragged -offset indent
386a2900666SRuslan ErmilovThis function specifies that traffic from a given remote address/port to
387a2900666SRuslan Ermilovan alias address/port be redirected to a specified local address/port.
3889c727d2cSJoseph KoshyThe parameter
389a2900666SRuslan Ermilov.Fa proto
390a2900666SRuslan Ermilovcan be either
391a2900666SRuslan Ermilov.Dv IPPROTO_TCP
3923efa11bbSBrian Somersor
393a2900666SRuslan Ermilov.Dv IPPROTO_UDP ,
394a2900666SRuslan Ermilovas defined in
395fe08efe6SRuslan Ermilov.In netinet/in.h .
396a2900666SRuslan Ermilov.Pp
3973efa11bbSBrian SomersIf
398a2900666SRuslan Ermilov.Fa local_addr
399a2900666SRuslan Ermilovor
400a2900666SRuslan Ermilov.Fa alias_addr
401a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established
402a2900666SRuslan Ermilovby
4035e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
404a2900666SRuslan Ermilovis to be used.
405a2900666SRuslan ErmilovEven if
4065e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
407a2900666SRuslan Ermilovis called to change the address after
4085e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
409a2900666SRuslan Ermilovis called, a zero reference will track this change.
410a2900666SRuslan Ermilov.Pp
4116d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then
4126d20a774SRuslan Ermilov.Fa local_addr
4136d20a774SRuslan Ermilovand
4146d20a774SRuslan Ermilov.Fa local_port
4156d20a774SRuslan Ermilovare ignored, and are selected dynamically from the server pool, as described in
4165e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer
4176d20a774SRuslan Ermilovbelow.
4186d20a774SRuslan Ermilov.Pp
419a2900666SRuslan ErmilovIf
420a2900666SRuslan Ermilov.Fa remote_addr
421a2900666SRuslan Ermilovis zero, this indicates to redirect packets from any remote address.
422a2900666SRuslan ErmilovLikewise, if
423a2900666SRuslan Ermilov.Fa remote_port
424a2900666SRuslan Ermilovis zero, this indicates to redirect packets originating from any remote
425a2900666SRuslan Ermilovport number.
426a2900666SRuslan ErmilovAlmost always, the remote port specification will be zero, but non-zero
427a2900666SRuslan Ermilovremote addresses can sometimes be useful for firewalling.
428a2900666SRuslan ErmilovIf two calls to
4295e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
430a2900666SRuslan Ermilovoverlap in their address/port specifications, then the most recent call
431a2900666SRuslan Ermilovwill have precedence.
432a2900666SRuslan Ermilov.Pp
433a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by
4345e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete .
435a2900666SRuslan ErmilovIf
436a2900666SRuslan Ermilov.Dv NULL
437a2900666SRuslan Ermilovis returned, then the function call did not complete successfully.
438a2900666SRuslan Ermilov.Pp
439a2900666SRuslan ErmilovAll port numbers should be in network address byte order, so it is necessary
440a2900666SRuslan Ermilovto use
441a2900666SRuslan Ermilov.Xr htons 3
442a2900666SRuslan Ermilovto convert these parameters from internally readable numbers to network byte
443a2900666SRuslan Ermilovorder.
444a2900666SRuslan ErmilovAddresses are also in network byte order, which is implicit in the use of the
445a2900666SRuslan Ermilov.Fa struct in_addr
4463efa11bbSBrian Somersdata type.
447a2900666SRuslan Ermilov.Ed
448a2900666SRuslan Ermilov.Pp
4493efa11bbSBrian Somers.Ft struct alias_link *
4505e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectAddr
4515e289f9eSPoul-Henning Kamp.Fa "struct libalias *"
4523efa11bbSBrian Somers.Fa "struct in_addr local_addr"
4533efa11bbSBrian Somers.Fa "struct in_addr alias_addr"
4543efa11bbSBrian Somers.Fc
455a2900666SRuslan Ermilov.Bd -ragged -offset indent
456a2900666SRuslan ErmilovThis function designates that all incoming traffic to
457a2900666SRuslan Ermilov.Fa alias_addr
4583efa11bbSBrian Somersbe redirected to
459a2900666SRuslan Ermilov.Fa local_addr .
4603efa11bbSBrian SomersSimilarly, all outgoing traffic from
461a2900666SRuslan Ermilov.Fa local_addr
4623efa11bbSBrian Somersis aliased to
463a2900666SRuslan Ermilov.Fa alias_addr .
464a2900666SRuslan Ermilov.Pp
4653efa11bbSBrian SomersIf
466a2900666SRuslan Ermilov.Fa local_addr
4673efa11bbSBrian Somersor
468a2900666SRuslan Ermilov.Fa alias_addr
469a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established by
4705e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
471a2900666SRuslan Ermilovis to be used.
472a2900666SRuslan ErmilovEven if
4735e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
474a2900666SRuslan Ermilovis called to change the address after
4755e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr
4763efa11bbSBrian Somersis called, a zero reference will track this change.
477a2900666SRuslan Ermilov.Pp
4786d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then
4796d20a774SRuslan Ermilov.Fa local_addr
4806d20a774SRuslan Ermilovis ignored, and is selected dynamically from the server pool, as described in
4815e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer
4826d20a774SRuslan Ermilovbelow.
4836d20a774SRuslan Ermilov.Pp
484a2900666SRuslan ErmilovIf subsequent calls to
4855e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr
486a2900666SRuslan Ermilovuse the same aliasing address, all new incoming traffic to this aliasing
487a2900666SRuslan Ermilovaddress will be redirected to the local address made in the last function
488a2900666SRuslan Ermilovcall.
489a2900666SRuslan ErmilovNew traffic generated by any of the local machines, designated in the
490a2900666SRuslan Ermilovseveral function calls, will be aliased to the same address.
491a2900666SRuslan ErmilovConsider the following example:
492a2900666SRuslan Ermilov.Bd -literal -offset indent
493c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.2"),
4943efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
495c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.3"),
4963efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
497c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.4"),
4983efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
4993efa11bbSBrian Somers.Ed
500a2900666SRuslan Ermilov.Pp
501a2900666SRuslan ErmilovAny outgoing connections such as
502a2900666SRuslan Ermilov.Xr telnet 1
503a2900666SRuslan Ermilovor
504a2900666SRuslan Ermilov.Xr ftp 1
505a2900666SRuslan Ermilovfrom 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
506a2900666SRuslan Ermilov141.221.254.101.
507a2900666SRuslan ErmilovAny incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
508a2900666SRuslan Ermilov.Pp
509a2900666SRuslan ErmilovAny calls to
5105e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
511a2900666SRuslan Ermilovwill have precedence over address mappings designated by
5125e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr .
513a2900666SRuslan Ermilov.Pp
514a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by
5155e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete .
516a2900666SRuslan ErmilovIf
517a2900666SRuslan Ermilov.Dv NULL
518a2900666SRuslan Ermilovis returned, then the function call did not complete successfully.
519a2900666SRuslan Ermilov.Ed
520a2900666SRuslan Ermilov.Pp
5216d20a774SRuslan Ermilov.Ft int
5225e289f9eSPoul-Henning Kamp.Fo LibAliasAddServer
5235e289f9eSPoul-Henning Kamp.Fa "struct libalias *"
5246d20a774SRuslan Ermilov.Fa "struct alias_link *link"
5256d20a774SRuslan Ermilov.Fa "struct in_addr addr"
5266d20a774SRuslan Ermilov.Fa "u_short port"
5276d20a774SRuslan Ermilov.Fc
5286d20a774SRuslan Ermilov.Bd -ragged -offset indent
5296d20a774SRuslan ErmilovThis function sets the
5306d20a774SRuslan Ermilov.Fa link
5316d20a774SRuslan Ermilovup for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
5326d20a774SRuslan ErmilovLSNAT operates as follows.
5336d20a774SRuslan ErmilovA client attempts to access a server by using the server virtual address.
5346d20a774SRuslan ErmilovThe LSNAT router transparently redirects the request to one of the hosts
5356d20a774SRuslan Ermilovin server pool, selected using a real-time load sharing algorithm.
5366d20a774SRuslan ErmilovMultiple sessions may be initiated from the same client, and each session
5376d20a774SRuslan Ermilovcould be directed to a different host based on load balance across server
5386d20a774SRuslan Ermilovpool hosts at the time.
5396d20a774SRuslan ErmilovIf load share is desired for just a few specific services, the configuration
5406d20a774SRuslan Ermilovon LSNAT could be defined to restrict load share for just the services
5416d20a774SRuslan Ermilovdesired.
5426d20a774SRuslan Ermilov.Pp
5436d20a774SRuslan ErmilovCurrently, only the simplest selection algorithm is implemented, where a
5446d20a774SRuslan Ermilovhost is selected on a round-robin basis only, without regard to load on
5456d20a774SRuslan Ermilovthe host.
5466d20a774SRuslan Ermilov.Pp
5476d20a774SRuslan ErmilovFirst, the
5486d20a774SRuslan Ermilov.Fa link
5496d20a774SRuslan Ermilovis created by either
5505e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
5516d20a774SRuslan Ermilovor
5525e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr .
5536d20a774SRuslan ErmilovThen,
5545e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer
5556d20a774SRuslan Ermilovis called multiple times to add entries to the
5566d20a774SRuslan Ermilov.Fa link Ns 's
5576d20a774SRuslan Ermilovserver pool.
5586d20a774SRuslan Ermilov.Pp
5596d20a774SRuslan ErmilovFor links created with
5605e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr ,
5616d20a774SRuslan Ermilovthe
5626d20a774SRuslan Ermilov.Fa port
5631a0a9345SRuslan Ermilovargument is ignored and could have any value, e.g.\& htons(~0).
5646d20a774SRuslan Ermilov.Pp
565234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise.
566234dfc90SRuslan Ermilov.Ed
567234dfc90SRuslan Ermilov.Pp
568234dfc90SRuslan Ermilov.Ft int
5695e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link"
570234dfc90SRuslan Ermilov.Bd -ragged -offset indent
571234dfc90SRuslan ErmilovThis function marks the specified static redirect rule entered by
5725e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
573234dfc90SRuslan Ermilovas dynamic.
5741a0a9345SRuslan ErmilovThis can be used to e.g.\& dynamically redirect a single TCP connection,
575234dfc90SRuslan Ermilovafter which the rule is removed.
576234dfc90SRuslan ErmilovOnly fully specified links can be made dynamic.
577234dfc90SRuslan Ermilov(See the
578234dfc90SRuslan Ermilov.Sx STATIC AND DYNAMIC LINKS
579234dfc90SRuslan Ermilovand
580234dfc90SRuslan Ermilov.Sx PARTIALLY SPECIFIED ALIASING LINKS
5811a0a9345SRuslan Ermilovsections below for a definition of static vs.\& dynamic,
5821a0a9345SRuslan Ermilovand partially vs.\& fully specified links.)
583234dfc90SRuslan Ermilov.Pp
584234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise.
5856d20a774SRuslan Ermilov.Ed
5866d20a774SRuslan Ermilov.Pp
5873efa11bbSBrian Somers.Ft void
5885e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link"
589a2900666SRuslan Ermilov.Bd -ragged -offset indent
590a2900666SRuslan ErmilovThis function will delete a specific static redirect rule entered by
5915e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort
592a2900666SRuslan Ermilovor
5935e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr .
594a2900666SRuslan ErmilovThe parameter
595a2900666SRuslan Ermilov.Fa link
596a2900666SRuslan Ermilovis the pointer returned by either of the redirection functions.
597a2900666SRuslan ErmilovIf an invalid pointer is passed to
5985e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete ,
599a2900666SRuslan Ermilovthen a program crash or unpredictable operation could result, so it is
6003efa11bbSBrian Somersnecessary to be careful using this function.
601a2900666SRuslan Ermilov.Ed
602a2900666SRuslan Ermilov.Pp
603619d1a30SBrian Somers.Ft int
6045e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule "struct libalias *" "const char *cmd"
605a2900666SRuslan Ermilov.Bd -ragged -offset indent
606619d1a30SBrian SomersThe passed
607a2900666SRuslan Ermilov.Fa cmd
608a2900666SRuslan Ermilovstring consists of one or more pairs of words.
609a2900666SRuslan ErmilovThe first word in each pair is a token and the second is the value that
610a2900666SRuslan Ermilovshould be applied for that token.
611a2900666SRuslan ErmilovTokens and their argument types are as follows:
612a2900666SRuslan Ermilov.Bl -tag -width indent
613a2900666SRuslan Ermilov.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
614619d1a30SBrian SomersIn order to support transparent proxying, it is necessary to somehow
615619d1a30SBrian Somerspass the original address and port information into the new destination
616a2900666SRuslan Ermilovserver.
617a2900666SRuslan ErmilovIf
618a2900666SRuslan Ermilov.Cm encode_ip_hdr
61971760898SRuslan Ermilovis specified, the original destination address and port are passed
62071760898SRuslan Ermilovas an extra IP option.
621a2900666SRuslan ErmilovIf
622a2900666SRuslan Ermilov.Cm encode_tcp_stream
62371760898SRuslan Ermilovis specified, the original destination address and port are passed
62471760898SRuslan Ermilovas the first piece of data in the TCP stream in the format
62571760898SRuslan Ermilov.Dq Li DEST Ar IP port .
626a2900666SRuslan Ermilov.It Cm port Ar portnum
627619d1a30SBrian SomersOnly packets with the destination port
628619d1a30SBrian Somers.Ar portnum
629619d1a30SBrian Somersare proxied.
6309c88dc88SRuslan Ermilov.It Cm server Ar host Ns Op : Ns Ar portnum
631619d1a30SBrian SomersThis specifies the
632619d1a30SBrian Somers.Ar host
633619d1a30SBrian Somersand
634619d1a30SBrian Somers.Ar portnum
635ac8e3334SBrian Somersthat the data is to be redirected to.
636ac8e3334SBrian Somers.Ar host
637a2900666SRuslan Ermilovmust be an IP address rather than a DNS host name.
638a2900666SRuslan ErmilovIf
639619d1a30SBrian Somers.Ar portnum
640619d1a30SBrian Somersis not specified, the destination port number is not changed.
641619d1a30SBrian Somers.Pp
642619d1a30SBrian SomersThe
643619d1a30SBrian Somers.Ar server
644619d1a30SBrian Somersspecification is mandatory unless the
645a2900666SRuslan Ermilov.Cm delete
646619d1a30SBrian Somerscommand is being used.
647a2900666SRuslan Ermilov.It Cm rule Ar index
648619d1a30SBrian SomersNormally, each call to
6495e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule
650a2900666SRuslan Ermilovinserts the next rule at the start of a linear list of rules.
651a2900666SRuslan ErmilovIf an
652619d1a30SBrian Somers.Ar index
653619d1a30SBrian Somersis specified, the new rule will be checked after all rules with lower
654a2900666SRuslan Ermilovindices.
655a2900666SRuslan ErmilovCalls to
6565e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule
657619d1a30SBrian Somersthat do not specify a rule are assigned rule 0.
658a2900666SRuslan Ermilov.It Cm delete Ar index
659a2900666SRuslan ErmilovThis token and its argument MUST NOT be used with any other tokens.
660a2900666SRuslan ErmilovWhen used, all existing rules with the given
661619d1a30SBrian Somers.Ar index
662619d1a30SBrian Somersare deleted.
663a2900666SRuslan Ermilov.It Cm proto tcp | udp
664619d1a30SBrian SomersIf specified, only packets of the given protocol type are matched.
6659c88dc88SRuslan Ermilov.It Cm src Ar IP Ns Op / Ns Ar bits
666619d1a30SBrian SomersIf specified, only packets with a source address matching the given
667619d1a30SBrian Somers.Ar IP
668a2900666SRuslan Ermilovare matched.
669a2900666SRuslan ErmilovIf
670619d1a30SBrian Somers.Ar bits
671619d1a30SBrian Somersis also specified, then the first
672619d1a30SBrian Somers.Ar bits
673619d1a30SBrian Somersbits of
674619d1a30SBrian Somers.Ar IP
675619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that
676619d1a30SBrian Somersnetwork will be matched.
6779c88dc88SRuslan Ermilov.It Cm dst Ar IP Ns Op / Ns Ar bits
678619d1a30SBrian SomersIf specified, only packets with a destination address matching the given
679619d1a30SBrian Somers.Ar IP
680a2900666SRuslan Ermilovare matched.
681a2900666SRuslan ErmilovIf
682619d1a30SBrian Somers.Ar bits
683619d1a30SBrian Somersis also specified, then the first
684619d1a30SBrian Somers.Ar bits
685619d1a30SBrian Somersbits of
686619d1a30SBrian Somers.Ar IP
687619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that
688619d1a30SBrian Somersnetwork will be matched.
689619d1a30SBrian Somers.El
690a2900666SRuslan Ermilov.Pp
691619d1a30SBrian SomersThis function is usually used to redirect outgoing connections for
692619d1a30SBrian Somersinternal machines that are not permitted certain types of internet
693619d1a30SBrian Somersaccess, or to restrict access to certain external machines.
694a2900666SRuslan Ermilov.Ed
695a2900666SRuslan Ermilov.Pp
696483d2f22SRuslan Ermilov.Ft struct alias_link *
6975e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectProto
6985e289f9eSPoul-Henning Kamp.Fa "struct libalias *"
699483d2f22SRuslan Ermilov.Fa "struct in_addr local_addr"
700483d2f22SRuslan Ermilov.Fa "struct in_addr remote_addr"
701483d2f22SRuslan Ermilov.Fa "struct in_addr alias_addr"
70280607605SRuslan Ermilov.Fa "u_char proto"
703483d2f22SRuslan Ermilov.Fc
704483d2f22SRuslan Ermilov.Bd -ragged -offset indent
70580607605SRuslan ErmilovThis function specifies that any IP packet with protocol number of
70680607605SRuslan Ermilov.Fa proto
70780607605SRuslan Ermilovfrom a given remote address to an alias address be
708483d2f22SRuslan Ermilovredirected to a specified local address.
709483d2f22SRuslan Ermilov.Pp
710483d2f22SRuslan ErmilovIf
711483d2f22SRuslan Ermilov.Fa local_addr
712483d2f22SRuslan Ermilovor
713483d2f22SRuslan Ermilov.Fa alias_addr
714483d2f22SRuslan Ermilovis zero, this indicates that the packet aliasing address as established
715483d2f22SRuslan Ermilovby
7165e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
717483d2f22SRuslan Ermilovis to be used.
718483d2f22SRuslan ErmilovEven if
7195e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress
720483d2f22SRuslan Ermilovis called to change the address after
7215e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto
722483d2f22SRuslan Ermilovis called, a zero reference will track this change.
723483d2f22SRuslan Ermilov.Pp
724483d2f22SRuslan ErmilovIf
725483d2f22SRuslan Ermilov.Fa remote_addr
72680607605SRuslan Ermilovis zero, this indicates to redirect packets from any remote address.
727483d2f22SRuslan ErmilovNon-zero remote addresses can sometimes be useful for firewalling.
728483d2f22SRuslan Ermilov.Pp
729483d2f22SRuslan ErmilovIf two calls to
7305e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto
731483d2f22SRuslan Ermilovoverlap in their address specifications, then the most recent call
732483d2f22SRuslan Ermilovwill have precedence.
733483d2f22SRuslan Ermilov.Pp
734483d2f22SRuslan ErmilovThis function returns a pointer which can subsequently be used by
7355e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete .
736483d2f22SRuslan ErmilovIf
737483d2f22SRuslan Ermilov.Dv NULL
738483d2f22SRuslan Ermilovis returned, then the function call did not complete successfully.
739483d2f22SRuslan Ermilov.Ed
740a2900666SRuslan Ermilov.Sh FRAGMENT HANDLING
741a2900666SRuslan ErmilovThe functions in this section are used to deal with incoming fragments.
742a2900666SRuslan Ermilov.Pp
743a2900666SRuslan ErmilovOutgoing fragments are handled within
7445e289f9eSPoul-Henning Kamp.Fn LibAliasOut
745a2900666SRuslan Ermilovby changing the address according to any applicable mapping set by
7465e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr ,
7471855100fSAlexey Zelkinor the default aliasing address set by
7485e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress .
749a2900666SRuslan Ermilov.Pp
7501855100fSAlexey ZelkinIncoming fragments are handled in one of two ways.
751a2900666SRuslan ErmilovIf the header of a fragmented IP packet has already been seen, then all
752a2900666SRuslan Ermilovsubsequent fragments will be re-mapped in the same manner the header
753a2900666SRuslan Ermilovfragment was.
754a2900666SRuslan ErmilovFragments which arrive before the header are saved and then retrieved
755a2900666SRuslan Ermilovonce the header fragment has been resolved.
756a2900666SRuslan Ermilov.Pp
7573efa11bbSBrian Somers.Ft int
7585e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment "struct libalias *" "char *ptr"
759a2900666SRuslan Ermilov.Bd -ragged -offset indent
760a2900666SRuslan ErmilovWhen
7615e289f9eSPoul-Henning Kamp.Fn LibAliasIn
762a2900666SRuslan Ermilovreturns
763a2900666SRuslan Ermilov.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
764a2900666SRuslan Ermilovthis function can be used to save the pointer to the unresolved fragment.
765a2900666SRuslan Ermilov.Pp
7663efa11bbSBrian SomersIt is implicitly assumed that
767a2900666SRuslan Ermilov.Fa ptr
7683efa11bbSBrian Somerspoints to a block of memory allocated by
769a2900666SRuslan Ermilov.Xr malloc 3 .
770a2900666SRuslan ErmilovIf the fragment is never resolved, the packet aliasing engine will
771a2900666SRuslan Ermilovautomatically free the memory after a timeout period.
772a2900666SRuslan Ermilov[Eventually this function should be modified so that a callback function
773a2900666SRuslan Ermilovfor freeing memory is passed as an argument.]
774a2900666SRuslan Ermilov.Pp
775a2900666SRuslan ErmilovThis function returns
776a2900666SRuslan Ermilov.Dv PKT_ALIAS_OK
777a2900666SRuslan Ermilovif it was successful and
778a2900666SRuslan Ermilov.Dv PKT_ALIAS_ERROR
779a2900666SRuslan Ermilovif there was an error.
780a2900666SRuslan Ermilov.Ed
781a2900666SRuslan Ermilov.Pp
7823efa11bbSBrian Somers.Ft char *
7835e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment "struct libalias *" "char *buffer"
784a2900666SRuslan Ermilov.Bd -ragged -offset indent
785a2900666SRuslan ErmilovThis function can be used to retrieve fragment pointers saved by
7865e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment .
7873efa11bbSBrian SomersThe IP header fragment pointed to by
788a2900666SRuslan Ermilov.Fa buffer
7893efa11bbSBrian Somersis the header fragment indicated when
7905e289f9eSPoul-Henning Kamp.Fn LibAliasIn
791a2900666SRuslan Ermilovreturns
792a2900666SRuslan Ermilov.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
793a2900666SRuslan ErmilovOnce a fragment pointer is retrieved, it becomes the calling program's
794a2900666SRuslan Ermilovresponsibility to free the dynamically allocated memory for the fragment.
795a2900666SRuslan Ermilov.Pp
7969703a107SPhilippe CharnierThe
7975e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment
7989703a107SPhilippe Charnierfunction can be called sequentially until there are no more fragments
7999703a107SPhilippe Charnieravailable, at which time it returns
800a2900666SRuslan Ermilov.Dv NULL .
801a2900666SRuslan Ermilov.Ed
802a2900666SRuslan Ermilov.Pp
8033efa11bbSBrian Somers.Ft void
8045e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn "struct libalias *" "char *header" "char *fragment"
805a2900666SRuslan Ermilov.Bd -ragged -offset indent
8063efa11bbSBrian SomersWhen a fragment is retrieved with
8075e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment ,
808a2900666SRuslan Ermilovit can then be de-aliased with a call to
8095e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn .
810a2900666SRuslan ErmilovThe
811a2900666SRuslan Ermilov.Fa header
812a2900666SRuslan Ermilovargument is the pointer to a header fragment used as a template, and
813a2900666SRuslan Ermilov.Fa fragment
8143efa11bbSBrian Somersis the pointer to the packet to be de-aliased.
815a2900666SRuslan Ermilov.Ed
816a2900666SRuslan Ermilov.Sh MISCELLANEOUS FUNCTIONS
8173efa11bbSBrian Somers.Ft void
8185e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr"
819a2900666SRuslan Ermilov.Bd -ragged -offset indent
820a2900666SRuslan ErmilovWhen an incoming packet not associated with any pre-existing aliasing link
821a2900666SRuslan Ermilovarrives at the host machine, it will be sent to the address indicated by a
822a2900666SRuslan Ermilovcall to
8235e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget .
824a2900666SRuslan Ermilov.Pp
825151682eaSBrian SomersIf this function is called with an
826a2900666SRuslan Ermilov.Dv INADDR_NONE
827a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address set by
8285e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress .
829a2900666SRuslan Ermilov.Pp
830151682eaSBrian SomersIf this function is not called, or is called with an
831a2900666SRuslan Ermilov.Dv INADDR_ANY
832a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address specified
833a2900666SRuslan Ermilovin the packet.
834a2900666SRuslan ErmilovThis allows external machines to talk directly to internal machines if they
835a2900666SRuslan Ermilovcan route packets to the machine in question.
836a2900666SRuslan Ermilov.Ed
837a2900666SRuslan Ermilov.Pp
8383efa11bbSBrian Somers.Ft int
8395e289f9eSPoul-Henning Kamp.Fn LibAliasCheckNewLink void
840a2900666SRuslan Ermilov.Bd -ragged -offset indent
841a2900666SRuslan ErmilovThis function returns a non-zero value when a new aliasing link is created.
842a2900666SRuslan ErmilovIn circumstances where incoming traffic is being sequentially sent to
843a2900666SRuslan Ermilovdifferent local servers, this function can be used to trigger when
8445e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget
8453efa11bbSBrian Somersis called to change the default target address.
846a2900666SRuslan Ermilov.Ed
847a2900666SRuslan Ermilov.Pp
8483efa11bbSBrian Somers.Ft u_short
8495e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes"
850a2900666SRuslan Ermilov.Bd -ragged -offset indent
851a2900666SRuslan ErmilovThis is a utility function that does not seem to be available elsewhere and
852a2900666SRuslan Ermilovis included as a convenience.
853a2900666SRuslan ErmilovIt computes the internet checksum, which is used in both IP and
854a2900666SRuslan Ermilovprotocol-specific headers (TCP, UDP, ICMP).
855a2900666SRuslan Ermilov.Pp
856a2900666SRuslan ErmilovThe
857a2900666SRuslan Ermilov.Fa buffer
858a2900666SRuslan Ermilovargument points to the data block to be checksummed, and
859a2900666SRuslan Ermilov.Fa nbytes
860a2900666SRuslan Ermilovis the number of bytes.
861a2900666SRuslan ErmilovThe 16-bit checksum field should be zeroed before computing the checksum.
862a2900666SRuslan Ermilov.Pp
863a2900666SRuslan ErmilovChecksums can also be verified by operating on a block of data including
864a2900666SRuslan Ermilovits checksum.
865a2900666SRuslan ErmilovIf the checksum is valid,
8665e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum
867a2900666SRuslan Ermilovwill return zero.
868a2900666SRuslan Ermilov.Ed
869642e43b3SArchie Cobbs.Pp
870642e43b3SArchie Cobbs.Ft int
8715e289f9eSPoul-Henning Kamp.Fn LibAliasUnaliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
872642e43b3SArchie Cobbs.Bd -ragged -offset indent
87371845bffSSheldon HearnAn outgoing packet, which has already been aliased,
87471845bffSSheldon Hearnhas its private address/port information restored by this function.
875642e43b3SArchie CobbsThe IP packet is pointed to by
876642e43b3SArchie Cobbs.Fa buffer ,
877642e43b3SArchie Cobbsand
878642e43b3SArchie Cobbs.Fa maxpacketsize
879642e43b3SArchie Cobbsis provided for error checking purposes.
880642e43b3SArchie CobbsThis function can be used if an already-aliased packet needs to have its
8811a0a9345SRuslan Ermilovoriginal IP header restored for further processing (e.g.\& logging).
882642e43b3SArchie Cobbs.Ed
883642e43b3SArchie Cobbs.Sh BUGS
884642e43b3SArchie CobbsPPTP aliasing does not work when more than one internal client
885642e43b3SArchie Cobbsconnects to the same external server at the same time, because
886642e43b3SArchie CobbsPPTP requires a single TCP control connection to be established
887642e43b3SArchie Cobbsbetween any two IP addresses.
888a2900666SRuslan Ermilov.Sh AUTHORS
889e83aaae3SBrian Somers.An Charles Mott Aq cm@linktel.net ,
890a2900666SRuslan Ermilovversions 1.0 - 1.8, 2.0 - 2.4.
891a2900666SRuslan Ermilov.An Eivind Eklund Aq eivind@FreeBSD.org ,
892a2900666SRuslan Ermilovversions 1.8b, 1.9 and 2.5.
893a2900666SRuslan ErmilovAdded IRC DCC support as well as contributing a number of architectural
894a2900666SRuslan Ermilovimprovements; added the firewall bypass for FTP/IRC DCC.
89555a39fc5SRuslan Ermilov.An Erik Salander Aq erik@whistle.com
896642e43b3SArchie Cobbsadded support for PPTP and RTSP.
897642e43b3SArchie Cobbs.An Junichi Satoh Aq junichi@junichi.org
898642e43b3SArchie Cobbsadded support for RTSP/PNA.
89961de149dSRuslan Ermilov.An Ruslan Ermilov Aq ru@FreeBSD.org
90061de149dSRuslan Ermilovadded support for PPTP and LSNAT as well as general hacking.
901a2900666SRuslan Ermilov.Sh ACKNOWLEDGMENTS
902a2900666SRuslan ErmilovListed below, in approximate chronological order, are individuals who
903a2900666SRuslan Ermilovhave provided valuable comments and/or debugging assistance.
904a2900666SRuslan Ermilov.Pp
9051b7b85c4SRuslan Ermilov.Bd -ragged -offset indent
9061b7b85c4SRuslan Ermilov.An -split
9071b7b85c4SRuslan Ermilov.An Gary Roberts
9081b7b85c4SRuslan Ermilov.An Tom Torrance
9091b7b85c4SRuslan Ermilov.An Reto Burkhalter
9101b7b85c4SRuslan Ermilov.An Martin Renters
9111b7b85c4SRuslan Ermilov.An Brian Somers
9121b7b85c4SRuslan Ermilov.An Paul Traina
9131b7b85c4SRuslan Ermilov.An Ari Suutari
9141b7b85c4SRuslan Ermilov.An Dave Remien
9151b7b85c4SRuslan Ermilov.An J. Fortes
9161b7b85c4SRuslan Ermilov.An Andrzej Bialecki
9171b7b85c4SRuslan Ermilov.An Gordon Burditt
9181b7b85c4SRuslan Ermilov.Ed
919a2900666SRuslan Ermilov.Sh CONCEPTUAL BACKGROUND
920a2900666SRuslan ErmilovThis section is intended for those who are planning to modify the source
921a2900666SRuslan Ermilovcode or want to create somewhat esoteric applications using the packet
922a2900666SRuslan Ermilovaliasing functions.
923a2900666SRuslan Ermilov.Pp
924a2900666SRuslan ErmilovThe conceptual framework under which the packet aliasing engine operates
925a2900666SRuslan Ermilovis described here.
9263efa11bbSBrian SomersCentral to the discussion is the idea of an
927a2900666SRuslan Ermilov.Em aliasing link
928a2900666SRuslan Ermilovwhich describes the relationship for a given packet transaction between
929a2900666SRuslan Ermilovthe local machine, aliased identity and remote machine.
930a2900666SRuslan ErmilovIt is discussed how such links come into existence and are destroyed.
931a2900666SRuslan Ermilov.Ss ALIASING LINKS
932a2900666SRuslan ErmilovThere is a notion of an
933a2900666SRuslan Ermilov.Em aliasing link ,
934a2900666SRuslan Ermilovwhich is a 7-tuple describing a specific translation:
9353efa11bbSBrian Somers.Bd -literal -offset indent
9363efa11bbSBrian Somers(local addr, local port, alias addr, alias port,
9373efa11bbSBrian Somers remote addr, remote port, protocol)
9383efa11bbSBrian Somers.Ed
939a2900666SRuslan Ermilov.Pp
940a2900666SRuslan ErmilovOutgoing packets have the local address and port number replaced with the
941a2900666SRuslan Ermilovalias address and port number.
942a2900666SRuslan ErmilovIncoming packets undergo the reverse process.
943a2900666SRuslan ErmilovThe packet aliasing engine attempts to match packets against an internal
944a2900666SRuslan Ermilovtable of aliasing links to determine how to modify a given IP packet.
945a2900666SRuslan ErmilovBoth the IP header and protocol dependent headers are modified as necessary.
946a2900666SRuslan ErmilovAliasing links are created and deleted as necessary according to network
947a2900666SRuslan Ermilovtraffic.
948a2900666SRuslan Ermilov.Pp
949a2900666SRuslan ErmilovProtocols can be TCP, UDP or even ICMP in certain circumstances.
950a2900666SRuslan Ermilov(Some types of ICMP packets can be aliased according to sequence or ID
951a2900666SRuslan Ermilovnumber which acts as an equivalent port number for identifying how
952a2900666SRuslan Ermilovindividual packets should be handled.)
953a2900666SRuslan Ermilov.Pp
954a2900666SRuslan ErmilovEach aliasing link must have a unique combination of the following five
955a2900666SRuslan Ermilovquantities: alias address/port, remote address/port and protocol.
956a2900666SRuslan ErmilovThis ensures that several machines on a local network can share the
957a2900666SRuslan Ermilovsame aliasing IP address.
958a2900666SRuslan ErmilovIn cases where conflicts might arise, the aliasing port is chosen so that
959a2900666SRuslan Ermilovuniqueness is maintained.
960a2900666SRuslan Ermilov.Ss STATIC AND DYNAMIC LINKS
9613efa11bbSBrian SomersAliasing links can either be static or dynamic.
962a2900666SRuslan ErmilovStatic links persist indefinitely and represent fixed rules for translating
963a2900666SRuslan ErmilovIP packets.
964a2900666SRuslan ErmilovDynamic links come into existence for a specific TCP connection or UDP
965a2900666SRuslan Ermilovtransaction or ICMP ECHO sequence.
966a2900666SRuslan ErmilovFor the case of TCP, the connection can be monitored to see when the
967a2900666SRuslan Ermilovassociated aliasing link should be deleted.
968a2900666SRuslan ErmilovAliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
969a2900666SRuslan Ermilovwork on a simple timeout rule.
970a2900666SRuslan ErmilovWhen no activity is observed on a dynamic link for a certain amount of time
971a2900666SRuslan Ermilovit is automatically deleted.
972a2900666SRuslan ErmilovTimeout rules also apply to TCP connections which do not open or close
9733efa11bbSBrian Somersproperly.
974a2900666SRuslan Ermilov.Ss PARTIALLY SPECIFIED ALIASING LINKS
975a2900666SRuslan ErmilovAliasing links can be partially specified, meaning that the remote address
976a2900666SRuslan Ermilovand/or remote port are unknown.
977a2900666SRuslan ErmilovIn this case, when a packet matching the incomplete specification is found,
978a2900666SRuslan Ermilova fully specified dynamic link is created.
979a2900666SRuslan ErmilovIf the original partially specified link is dynamic, it will be deleted
980a2900666SRuslan Ermilovafter the fully specified link is created, otherwise it will persist.
981a2900666SRuslan Ermilov.Pp
982a2900666SRuslan ErmilovFor instance, a partially specified link might be
9833efa11bbSBrian Somers.Bd -literal -offset indent
9843efa11bbSBrian Somers(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
9853efa11bbSBrian Somers.Ed
986a2900666SRuslan Ermilov.Pp
987a2900666SRuslan ErmilovThe zeros denote unspecified components for the remote address and port.
988a2900666SRuslan ErmilovIf this link were static it would have the effect of redirecting all
989a2900666SRuslan Ermilovincoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
990a2900666SRuslan Ermilovof machine 192.168.0.4 on the local network.
991a2900666SRuslan ErmilovEach individual telnet connection would initiate the creation of a distinct
992a2900666SRuslan Ermilovdynamic link.
993a2900666SRuslan Ermilov.Ss DYNAMIC LINK CREATION
994a2900666SRuslan ErmilovIn addition to aliasing links, there are also address mappings that can be
995a2900666SRuslan Ermilovstored within the internal data table of the packet aliasing mechanism.
9963efa11bbSBrian Somers.Bd -literal -offset indent
9973efa11bbSBrian Somers(local addr, alias addr)
9983efa11bbSBrian Somers.Ed
999a2900666SRuslan Ermilov.Pp
1000a2900666SRuslan ErmilovAddress mappings are searched when creating new dynamic links.
1001a2900666SRuslan Ermilov.Pp
1002a2900666SRuslan ErmilovAll outgoing packets from the local network automatically create a dynamic
1003a2900666SRuslan Ermilovlink if they do not match an already existing fully specified link.
1004a2900666SRuslan ErmilovIf an address mapping exists for the outgoing packet, this determines
1005a2900666SRuslan Ermilovthe alias address to be used.
1006a2900666SRuslan ErmilovIf no mapping exists, then a default address, usually the address of the
1007a2900666SRuslan Ermilovpacket aliasing host, is used.
1008a2900666SRuslan ErmilovIf necessary, this default address can be changed as often as each individual
1009a2900666SRuslan Ermilovpacket arrives.
1010a2900666SRuslan Ermilov.Pp
1011a2900666SRuslan ErmilovThe aliasing port number is determined such that the new dynamic link does
1012a2900666SRuslan Ermilovnot conflict with any existing links.
1013a2900666SRuslan ErmilovIn the default operating mode, the packet aliasing engine attempts to set
1014a2900666SRuslan Ermilovthe aliasing port equal to the local port number.
1015a2900666SRuslan ErmilovIf this results in a conflict, then port numbers are randomly chosen until
1016a2900666SRuslan Ermilova unique aliasing link can be established.
1017a2900666SRuslan ErmilovIn an alternate operating mode, the first choice of an aliasing port is also
1018a2900666SRuslan Ermilovrandom and unrelated to the local port number.
1019