xref: /freebsd/sys/netinet/libalias/libalias.3 (revision 642e43b39bb26897301c1dbaa3037cdc6cbf2391)
17f3dea24SPeter Wemm.\" $FreeBSD$
274804d58SMike Pritchard.\"
3a2900666SRuslan Ermilov.Dd April 13, 2000
474804d58SMike Pritchard.Dt LIBALIAS 3
5a2900666SRuslan Ermilov.Os FreeBSD
63efa11bbSBrian Somers.Sh NAME
774804d58SMike Pritchard.Nm libalias
8a2900666SRuslan Ermilov.Nd packet aliasing library for masquerading and network address translation
93efa11bbSBrian Somers.Sh SYNOPSIS
10442a25bdSBruce Evans.Fd #include <sys/types.h>
113efa11bbSBrian Somers.Fd #include <netinet/in.h>
123efa11bbSBrian Somers.Fd #include <alias.h>
13a2900666SRuslan Ermilov.Pp
14a2900666SRuslan ErmilovFunction prototypes are given in the main body of the text.
1574804d58SMike Pritchard.Sh DESCRIPTION
1674804d58SMike PritchardThe
1774804d58SMike Pritchard.Nm
18a2900666SRuslan Ermilovlibrary is a collection of functions for aliasing and de-aliasing of IP
19a2900666SRuslan Ermilovpackets, intended for masquerading and network address translation (NAT).
20a2900666SRuslan Ermilov.Sh INTRODUCTION
21a2900666SRuslan ErmilovThis library is a moderately portable set of functions designed to assist
22a2900666SRuslan Ermilovin the process of IP masquerading and network address translation.
23a2900666SRuslan ErmilovOutgoing packets from a local network with unregistered IP addresses can
24a2900666SRuslan Ermilovbe aliased to appear as if they came from an accessible IP address.
25a2900666SRuslan ErmilovIncoming packets are then de-aliased so that they are sent to the correct
26a2900666SRuslan Ermilovmachine on the local network.
27a2900666SRuslan Ermilov.Pp
28a2900666SRuslan ErmilovA certain amount of flexibility is built into the packet aliasing engine.
29a2900666SRuslan ErmilovIn the simplest mode of operation, a many-to-one address mapping takes
30a2900666SRuslan Ermilovplace between local network and the packet aliasing host.
31a2900666SRuslan ErmilovThis is known as IP masquerading.
32a2900666SRuslan ErmilovIn addition, one-to-one mappings between local and public addresses can
33a2900666SRuslan Ermilovalso be implemented, which is known as static NAT.
34a2900666SRuslan ErmilovIn between these extremes, different groups of private addresses can be
35a2900666SRuslan Ermilovlinked to different public addresses, comprising several distinct
36a2900666SRuslan Ermilovmany-to-one mappings.
37a2900666SRuslan ErmilovAlso, a given public address and port can be statically redirected to a
38a2900666SRuslan Ermilovprivate address/port.
39a2900666SRuslan Ermilov.Pp
40a2900666SRuslan ErmilovThe packet aliasing engine was designed to operate in user space outside
41a2900666SRuslan Ermilovof the kernel, without any access to private kernel data structure, but
42a2900666SRuslan Ermilovthe source code can also be ported to a kernel environment.
43a2900666SRuslan Ermilov.Sh INITIALIZATION AND CONTROL
44a2900666SRuslan ErmilovTwo special functions,
45a2900666SRuslan Ermilov.Fn PacketAliasInit
46a2900666SRuslan Ermilovand
47a2900666SRuslan Ermilov.Fn PacketAliasSetAddress ,
48a2900666SRuslan Ermilovmust always be called before any packet handling may be performed.
49a2900666SRuslan ErmilovIn addition, the operating mode of the packet aliasing engine can be
50a2900666SRuslan Ermilovcustomized by calling
51a2900666SRuslan Ermilov.Fn PacketAliasSetMode .
52a2900666SRuslan Ermilov.Pp
533efa11bbSBrian Somers.Ft void
54a2900666SRuslan Ermilov.Fn PacketAliasInit void
55a2900666SRuslan Ermilov.Bd -ragged -offset indent
56a2900666SRuslan ErmilovThis function has no arguments or return value and is used to initialize
57a2900666SRuslan Ermilovinternal data structures.
58a2900666SRuslan ErmilovThe following mode bits are always set after calling
59a2900666SRuslan Ermilov.Fn PacketAliasInit .
60a2900666SRuslan ErmilovSee the description of
61a2900666SRuslan Ermilov.Fn PacketAliasSetMode
62a2900666SRuslan Ermilovbelow for the meaning of these mode bits.
63a2900666SRuslan Ermilov.Pp
64a2900666SRuslan Ermilov.Bl -item -offset indent -compact
65a2900666SRuslan Ermilov.It
66a2900666SRuslan Ermilov.Dv PKT_ALIAS_SAME_PORTS
67a2900666SRuslan Ermilov.It
68a2900666SRuslan Ermilov.Dv PKT_ALIAS_USE_SOCKETS
69a2900666SRuslan Ermilov.It
70a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
71a2900666SRuslan Ermilov.El
72a2900666SRuslan Ermilov.Pp
73a2900666SRuslan ErmilovThis function will always return the packet aliasing engine to the same
74a2900666SRuslan Ermilovinitial state.
75a2900666SRuslan Ermilov.Fn PacketAliasSetAddress
76a2900666SRuslan Ermilovmust be called afterwards, and any desired changes from the default mode
773efa11bbSBrian Somersbits listed above require a call to
78a2900666SRuslan Ermilov.Fn PacketAliasSetMode .
79a2900666SRuslan Ermilov.Pp
80a2900666SRuslan ErmilovIt is mandatory that this function be called at the beginning of a program
81a2900666SRuslan Ermilovprior to any packet handling.
82a2900666SRuslan Ermilov.Ed
83a2900666SRuslan Ermilov.Pp
848ddc51bcSEivind Eklund.Ft void
85a2900666SRuslan Ermilov.Fn PacketAliasUninit void
86a2900666SRuslan Ermilov.Bd -ragged -offset indent
87a2900666SRuslan ErmilovThis function has no arguments or return value and is used to clear any
88a2900666SRuslan Ermilovresources attached to internal data structures.
89a2900666SRuslan Ermilov.Pp
90a2900666SRuslan ErmilovThis functions should be called when a program stops using the aliasing
91a2900666SRuslan Ermilovengine; it does, amongst other things, clear out any firewall holes.
92a2900666SRuslan ErmilovTo provide backwards compatibility and extra security, it is added to
93a2900666SRuslan Ermilovthe
94a2900666SRuslan Ermilov.Xr atexit 3
95a2900666SRuslan Ermilovchain by
96a2900666SRuslan Ermilov.Fn PacketAliasInit .
97a2900666SRuslan ErmilovCalling it multiple times is harmless.
98a2900666SRuslan Ermilov.Ed
99a2900666SRuslan Ermilov.Pp
1003efa11bbSBrian Somers.Ft void
1013efa11bbSBrian Somers.Fn PacketAliasSetAddress "struct in_addr addr"
102a2900666SRuslan Ermilov.Bd -ragged -offset indent
103a2900666SRuslan ErmilovThis function sets the source address to which outgoing packets from the
104a2900666SRuslan Ermilovlocal area network are aliased.
105a2900666SRuslan ErmilovAll outgoing packets are re-mapped to this address unless overridden by a
106a2900666SRuslan Ermilovstatic address mapping established by
107a2900666SRuslan Ermilov.Fn PacketAliasRedirectAddr .
108a2900666SRuslan Ermilov.Pp
109a2900666SRuslan ErmilovIf the
110a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
111a2900666SRuslan Ermilovmode bit is set (the default mode of operation), then the internal aliasing
112a2900666SRuslan Ermilovlink tables will be reset any time the aliasing address changes.
113a2900666SRuslan ErmilovThis is useful for interfaces such as
114a2900666SRuslan Ermilov.Xr ppp 8 ,
115a2900666SRuslan Ermilovwhere the IP
116a2900666SRuslan Ermilovaddress may or may not change on successive dial-up attempts.
117a2900666SRuslan Ermilov.Pp
118a2900666SRuslan ErmilovIf the
119a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
120a2900666SRuslan Ermilovmode bit is set to zero, this function can also be used to dynamically change
121a2900666SRuslan Ermilovthe aliasing address on a packet to packet basis (it is a low overhead call).
122a2900666SRuslan Ermilov.Pp
123a2900666SRuslan ErmilovIt is mandatory that this function be called prior to any packet handling.
124a2900666SRuslan Ermilov.Ed
125a2900666SRuslan Ermilov.Pp
126442a25bdSBruce Evans.Ft unsigned int
127a2900666SRuslan Ermilov.Fn PacketAliasSetMode "unsigned int flags" "unsigned int mask"
128a2900666SRuslan Ermilov.Bd -ragged -offset indent
1293efa11bbSBrian SomersThis function sets or clears mode bits
1303efa11bbSBrian Somersaccording to the value of
131a2900666SRuslan Ermilov.Fa flags .
1323efa11bbSBrian SomersOnly bits marked in
133a2900666SRuslan Ermilov.Fa mask
134a2900666SRuslan Ermilovare affected.
135a2900666SRuslan ErmilovThe following mode bits are defined in
136a2900666SRuslan Ermilov.Aq Pa alias.h :
137a2900666SRuslan Ermilov.Bl -tag -width indent
138a2900666SRuslan Ermilov.It Dv PKT_ALIAS_LOG
139a2900666SRuslan ErmilovEnables logging into
140a2900666SRuslan Ermilov.Pa /var/log/alias.log .
141a2900666SRuslan ErmilovEach time an aliasing link is created or deleted, the log file is appended
142a2900666SRuslan Ermilovwith the current number of ICMP, TCP and UDP links.
143a2900666SRuslan ErmilovMainly useful for debugging when the log file is viewed continuously with
144a2900666SRuslan Ermilov.Xr tail 1 .
145a2900666SRuslan Ermilov.It Dv PKT_ALIAS_DENY_INCOMING
146a2900666SRuslan ErmilovIf this mode bit is set, all incoming packets associated with new TCP
147a2900666SRuslan Ermilovconnections or new UDP transactions will be marked for being ignored
148a2900666SRuslan Ermilov.Po
149a2900666SRuslan Ermilov.Fn PacketAliasIn
150a2900666SRuslan Ermilovreturns
151a2900666SRuslan Ermilov.Dv PKT_ALIAS_IGNORED
152a2900666SRuslan Ermilovcode
153a2900666SRuslan Ermilov.Pc
154a2900666SRuslan Ermilovby the calling program.
155a2900666SRuslan ErmilovResponse packets to connections or transactions initiated from the packet
156a2900666SRuslan Ermilovaliasing host or local network will be unaffected.
157a2900666SRuslan ErmilovThis mode bit is useful for implementing a one-way firewall.
158a2900666SRuslan Ermilov.It Dv PKT_ALIAS_SAME_PORTS
159a2900666SRuslan ErmilovIf this mode bit is set, the packet aliasing engine will attempt to leave
160a2900666SRuslan Ermilovthe alias port numbers unchanged from the actual local port numbers.
161a2900666SRuslan ErmilovThis can be done as long as the quintuple (proto, alias addr, alias port,
162a2900666SRuslan Ermilovremote addr, remote port) is unique.
163a2900666SRuslan ErmilovIf a conflict exists, a new aliasing port number is chosen even if this
164a2900666SRuslan Ermilovmode bit is set.
165a2900666SRuslan Ermilov.It Dv PKT_ALIAS_USE_SOCKETS
166a2900666SRuslan ErmilovThis bit should be set when the packet aliasing host originates network
167a2900666SRuslan Ermilovtraffic as well as forwards it.
168a2900666SRuslan ErmilovWhen the packet aliasing host is waiting for a connection from an unknown
169a2900666SRuslan Ermilovhost address or unknown port number (e.g. an FTP data connection), this
170a2900666SRuslan Ermilovmode bit specifies that a socket be allocated as a place holder to prevent
171a2900666SRuslan Ermilovport conflicts.
172a2900666SRuslan ErmilovOnce a connection is established, usually within a minute or so, the socket
173a2900666SRuslan Ermilovis closed.
174a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNREGISTERED_ONLY
175a2900666SRuslan ErmilovIf this mode bit is set, traffic on the local network which does not
176a2900666SRuslan Ermilovoriginate from unregistered address spaces will be ignored.
177a2900666SRuslan ErmilovStandard Class A, B and C unregistered addresses are:
1783efa11bbSBrian Somers.Bd -literal -offset indent
1793efa11bbSBrian Somers10.0.0.0     ->  10.255.255.255   (Class A subnet)
1803efa11bbSBrian Somers172.16.0.0   ->  172.31.255.255   (Class B subnets)
1813efa11bbSBrian Somers192.168.0.0  ->  192.168.255.255  (Class C subnets)
1823efa11bbSBrian Somers.Ed
183a2900666SRuslan Ermilov.Pp
184a2900666SRuslan ErmilovThis option is useful in the case that packet aliasing host has both
185a2900666SRuslan Ermilovregistered and unregistered subnets on different interfaces.
186a2900666SRuslan ErmilovThe registered subnet is fully accessible to the outside world, so traffic
187a2900666SRuslan Ermilovfrom it does not need to be passed through the packet aliasing engine.
188a2900666SRuslan Ermilov.It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
1893efa11bbSBrian SomersWhen this mode bit is set and
190a2900666SRuslan Ermilov.Fn PacketAliasSetAddress
191a2900666SRuslan Ermilovis called to change the aliasing address, the internal link table of the
192a2900666SRuslan Ermilovpacket aliasing engine will be cleared.
193a2900666SRuslan ErmilovThis operating mode is useful for
194a2900666SRuslan Ermilov.Xr ppp 8
195a2900666SRuslan Ermilovlinks where the interface address can sometimes change or remain the same
196a2900666SRuslan Ermilovbetween dial-up attempts.
197a2900666SRuslan ErmilovIf this mode bit is not set, the link table will never be reset in the event
198a2900666SRuslan Ermilovof an address change.
199a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PUNCH_FW
200a2900666SRuslan ErmilovThis option makes
201a2900666SRuslan Ermilov.Nm
202a2900666SRuslan Ermilov`punch holes' in an
203a2900666SRuslan Ermilov.Xr ipfirewall 4
204a2900666SRuslan Ermilovbased firewall for FTP/IRC DCC connections.
205a2900666SRuslan ErmilovThe holes punched are bound by from/to IP address and port; it will not be
206a2900666SRuslan Ermilovpossible to use a hole for another connection.
207a2900666SRuslan ErmilovA hole is removed when the connection that uses it dies.
208a2900666SRuslan ErmilovTo cater to unexpected death of a program using
209a2900666SRuslan Ermilov.Nm
210a2900666SRuslan Ermilov(e.g. kill -9),
211a2900666SRuslan Ermilovchanging the state of the flag will clear the entire firewall range
212a2900666SRuslan Ermilovallocated for holes.
2138ddc51bcSEivind EklundThis will also happen on the initial call to
214a2900666SRuslan Ermilov.Fn PacketAliasSetFWBase .
215a2900666SRuslan ErmilovThis call must happen prior to setting this flag.
216a2900666SRuslan Ermilov.It Dv PKT_ALIAS_REVERSE
217a2900666SRuslan ErmilovThis option makes
218a2900666SRuslan Ermilov.Nm
219a2900666SRuslan Ermilovreverse the way it handles incoming and outgoing packets, allowing it
220a2900666SRuslan Ermilovto be fed with data that passes through the internal interface rather
221a2900666SRuslan Ermilovthan the external one.
222a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PROXY_ONLY
223a2900666SRuslan ErmilovThis option tells
224a2900666SRuslan Ermilov.Nm
225a2900666SRuslan Ermilovto obey transparent proxy rules only.
226a2900666SRuslan ErmilovNormal packet aliasing is not performed.
22721b9df57SBrian SomersSee
22821b9df57SBrian Somers.Fn PacketAliasProxyRule
22921b9df57SBrian Somersbelow for details.
2303efa11bbSBrian Somers.El
231a2900666SRuslan Ermilov.Ed
232a2900666SRuslan Ermilov.Pp
2338ddc51bcSEivind Eklund.Ft void
2348ddc51bcSEivind Eklund.Fn PacketAliasSetFWBase "unsigned int base" "unsigned int num"
235a2900666SRuslan Ermilov.Bd -ragged -offset indent
236a2900666SRuslan ErmilovSet firewall range allocated for punching firewall holes (with the
237a2900666SRuslan Ermilov.Dv PKT_ALIAS_PUNCH_FW
238a2900666SRuslan Ermilovflag).
239a2900666SRuslan ErmilovThe range will be cleared for all rules on initialization.
240a2900666SRuslan Ermilov.Ed
241a2900666SRuslan Ermilov.Sh PACKET HANDLING
242a2900666SRuslan ErmilovThe packet handling functions are used to modify incoming (remote to local)
243a2900666SRuslan Ermilovand outgoing (local to remote) packets.
244a2900666SRuslan ErmilovThe calling program is responsible for receiving and sending packets via
245a2900666SRuslan Ermilovnetwork interfaces.
246a2900666SRuslan Ermilov.Pp
247a2900666SRuslan ErmilovAlong with
248a2900666SRuslan Ermilov.Fn PacketAliasInit
249a2900666SRuslan Ermilovand
250a2900666SRuslan Ermilov.Fn PacketAliasSetAddress ,
251a2900666SRuslan Ermilovthe two packet handling functions,
252a2900666SRuslan Ermilov.Fn PacketAliasIn
253a2900666SRuslan Ermilovand
254a2900666SRuslan Ermilov.Fn PacketAliasOut ,
255a2900666SRuslan Ermilovcomprise minimal set of functions needed for a basic IP masquerading
256a2900666SRuslan Ermilovimplementation.
257a2900666SRuslan Ermilov.Pp
2583efa11bbSBrian Somers.Ft int
2593efa11bbSBrian Somers.Fn PacketAliasIn "char *buffer" "int maxpacketsize"
260a2900666SRuslan Ermilov.Bd -ragged -offset indent
261a2900666SRuslan ErmilovAn incoming packet coming from a remote machine to the local network is
262a2900666SRuslan Ermilovde-aliased by this function.
2633efa11bbSBrian SomersThe IP packet is pointed to by
264a2900666SRuslan Ermilov.Fa buffer ,
2653efa11bbSBrian Somersand
266a2900666SRuslan Ermilov.Fa maxpacketsize
267a2900666SRuslan Ermilovindicates the size of the data structure containing the packet and should
268a2900666SRuslan Ermilovbe at least as large as the actual packet size.
269a2900666SRuslan Ermilov.Pp
2703efa11bbSBrian SomersReturn codes:
271a2900666SRuslan Ermilov.Bl -tag -width indent
272a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK
2733efa11bbSBrian SomersThe packet aliasing process was successful.
274a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED
2753efa11bbSBrian SomersThe packet was ignored and not de-aliased.
276a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, possibly an ICMP message
277a2900666SRuslan Ermilovtype is not handled or if incoming packets for new connections are being
278a2900666SRuslan Ermilovignored (if
279a2900666SRuslan Ermilov.Dv PKT_ALIAS_DENY_INCOMING
280a2900666SRuslan Ermilovmode bit was set by
281a2900666SRuslan Ermilov.Fn PacketAliasSetMode ) .
282a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
283a2900666SRuslan ErmilovThis is returned when a fragment cannot be resolved because the header
284a2900666SRuslan Ermilovfragment has not been sent yet.
285a2900666SRuslan ErmilovIn this situation, fragments must be saved with
286a2900666SRuslan Ermilov.Fn PacketAliasSaveFragment
2873efa11bbSBrian Somersuntil a header fragment is found.
288a2900666SRuslan Ermilov.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
289a2900666SRuslan ErmilovThe packet aliasing process was successful, and a header fragment was found.
290a2900666SRuslan ErmilovThis is a signal to retrieve any unresolved fragments with
291a2900666SRuslan Ermilov.Fn PacketAliasGetFragment
292a2900666SRuslan Ermilovand de-alias them with
293a2900666SRuslan Ermilov.Fn PacketAliasFragmentIn .
294a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR
295a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred.
2963efa11bbSBrian Somers.El
297a2900666SRuslan Ermilov.Ed
298a2900666SRuslan Ermilov.Pp
2993efa11bbSBrian Somers.Ft int
300b2052ac8SJohn Polstra.Fn PacketAliasOut "char *buffer" "int maxpacketsize"
301a2900666SRuslan Ermilov.Bd -ragged -offset indent
302a2900666SRuslan ErmilovAn outgoing packet coming from the local network to a remote machine is
303a2900666SRuslan Ermilovaliased by this function.
3043efa11bbSBrian SomersThe IP packet is pointed to by
305a2900666SRuslan Ermilov.Fa buffer ,
3063efa11bbSBrian Somersand
307a2900666SRuslan Ermilov.Fa maxpacketsize
308a2900666SRuslan Ermilovindicates the maximum packet size permissible should the packet length be
309a2900666SRuslan Ermilovchanged.
310a2900666SRuslan ErmilovIP encoding protocols place address and port information in the encapsulated
311a2900666SRuslan Ermilovdata stream which has to be modified and can account for changes in packet
312a2900666SRuslan Ermilovlength.
313a2900666SRuslan ErmilovWell known examples of such protocols are FTP and IRC DCC.
314a2900666SRuslan Ermilov.Pp
3153efa11bbSBrian SomersReturn codes:
316a2900666SRuslan Ermilov.Bl -tag -width indent
317a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK
3183efa11bbSBrian SomersThe packet aliasing process was successful.
319a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED
320a2900666SRuslan ErmilovThe packet was ignored and not aliased.
321a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, or possibly an ICMP message
322a2900666SRuslan Ermilovtype is not handled.
323a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR
324a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred.
3253efa11bbSBrian Somers.El
326a2900666SRuslan Ermilov.Ed
327a2900666SRuslan Ermilov.Sh PORT AND ADDRESS REDIRECTION
328a2900666SRuslan ErmilovThe functions described in this section allow machines on the local network
329a2900666SRuslan Ermilovto be accessible in some degree to new incoming connections from the external
330a2900666SRuslan Ermilovnetwork.
331a2900666SRuslan ErmilovIndividual ports can be re-mapped or static network address translations can
332a2900666SRuslan Ermilovbe designated.
333a2900666SRuslan Ermilov.Pp
3343efa11bbSBrian Somers.Ft struct alias_link *
3353efa11bbSBrian Somers.Fo PacketAliasRedirectPort
3363efa11bbSBrian Somers.Fa "struct in_addr local_addr"
3373efa11bbSBrian Somers.Fa "u_short local_port"
3383efa11bbSBrian Somers.Fa "struct in_addr remote_addr"
3393efa11bbSBrian Somers.Fa "u_short remote_port"
3403efa11bbSBrian Somers.Fa "struct in_addr alias_addr"
3413efa11bbSBrian Somers.Fa "u_short alias_port"
3423efa11bbSBrian Somers.Fa "u_char proto"
3433efa11bbSBrian Somers.Fc
344a2900666SRuslan Ermilov.Bd -ragged -offset indent
345a2900666SRuslan ErmilovThis function specifies that traffic from a given remote address/port to
346a2900666SRuslan Ermilovan alias address/port be redirected to a specified local address/port.
3479c727d2cSJoseph KoshyThe parameter
348a2900666SRuslan Ermilov.Fa proto
349a2900666SRuslan Ermilovcan be either
350a2900666SRuslan Ermilov.Dv IPPROTO_TCP
3513efa11bbSBrian Somersor
352a2900666SRuslan Ermilov.Dv IPPROTO_UDP ,
353a2900666SRuslan Ermilovas defined in
354a2900666SRuslan Ermilov.Aq Pa netinet/in.h .
355a2900666SRuslan Ermilov.Pp
3563efa11bbSBrian SomersIf
357a2900666SRuslan Ermilov.Fa local_addr
358a2900666SRuslan Ermilovor
359a2900666SRuslan Ermilov.Fa alias_addr
360a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established
361a2900666SRuslan Ermilovby
362a2900666SRuslan Ermilov.Fn PacketAliasSetAddress
363a2900666SRuslan Ermilovis to be used.
364a2900666SRuslan ErmilovEven if
365483d2f22SRuslan Ermilov.Fn PacketAliasSetAddress
366a2900666SRuslan Ermilovis called to change the address after
367483d2f22SRuslan Ermilov.Fn PacketAliasRedirectPort
368a2900666SRuslan Ermilovis called, a zero reference will track this change.
369a2900666SRuslan Ermilov.Pp
3706d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then
3716d20a774SRuslan Ermilov.Fa local_addr
3726d20a774SRuslan Ermilovand
3736d20a774SRuslan Ermilov.Fa local_port
3746d20a774SRuslan Ermilovare ignored, and are selected dynamically from the server pool, as described in
3756d20a774SRuslan Ermilov.Fn PacketAliasAddServer
3766d20a774SRuslan Ermilovbelow.
3776d20a774SRuslan Ermilov.Pp
378a2900666SRuslan ErmilovIf
379a2900666SRuslan Ermilov.Fa remote_addr
380a2900666SRuslan Ermilovis zero, this indicates to redirect packets from any remote address.
381a2900666SRuslan ErmilovLikewise, if
382a2900666SRuslan Ermilov.Fa remote_port
383a2900666SRuslan Ermilovis zero, this indicates to redirect packets originating from any remote
384a2900666SRuslan Ermilovport number.
385a2900666SRuslan ErmilovAlmost always, the remote port specification will be zero, but non-zero
386a2900666SRuslan Ermilovremote addresses can sometimes be useful for firewalling.
387a2900666SRuslan ErmilovIf two calls to
388a2900666SRuslan Ermilov.Fn PacketAliasRedirectPort
389a2900666SRuslan Ermilovoverlap in their address/port specifications, then the most recent call
390a2900666SRuslan Ermilovwill have precedence.
391a2900666SRuslan Ermilov.Pp
392a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by
393a2900666SRuslan Ermilov.Fn PacketAliasRedirectDelete .
394a2900666SRuslan ErmilovIf
395a2900666SRuslan Ermilov.Dv NULL
396a2900666SRuslan Ermilovis returned, then the function call did not complete successfully.
397a2900666SRuslan Ermilov.Pp
398a2900666SRuslan ErmilovAll port numbers should be in network address byte order, so it is necessary
399a2900666SRuslan Ermilovto use
400a2900666SRuslan Ermilov.Xr htons 3
401a2900666SRuslan Ermilovto convert these parameters from internally readable numbers to network byte
402a2900666SRuslan Ermilovorder.
403a2900666SRuslan ErmilovAddresses are also in network byte order, which is implicit in the use of the
404a2900666SRuslan Ermilov.Fa struct in_addr
4053efa11bbSBrian Somersdata type.
406a2900666SRuslan Ermilov.Ed
407a2900666SRuslan Ermilov.Pp
4083efa11bbSBrian Somers.Ft struct alias_link *
409442a25bdSBruce Evans.Fo PacketAliasRedirectAddr
4103efa11bbSBrian Somers.Fa "struct in_addr local_addr"
4113efa11bbSBrian Somers.Fa "struct in_addr alias_addr"
4123efa11bbSBrian Somers.Fc
413a2900666SRuslan Ermilov.Bd -ragged -offset indent
414a2900666SRuslan ErmilovThis function designates that all incoming traffic to
415a2900666SRuslan Ermilov.Fa alias_addr
4163efa11bbSBrian Somersbe redirected to
417a2900666SRuslan Ermilov.Fa local_addr .
4183efa11bbSBrian SomersSimilarly, all outgoing traffic from
419a2900666SRuslan Ermilov.Fa local_addr
4203efa11bbSBrian Somersis aliased to
421a2900666SRuslan Ermilov.Fa alias_addr .
422a2900666SRuslan Ermilov.Pp
4233efa11bbSBrian SomersIf
424a2900666SRuslan Ermilov.Fa local_addr
4253efa11bbSBrian Somersor
426a2900666SRuslan Ermilov.Fa alias_addr
427a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established by
428a2900666SRuslan Ermilov.Fn PacketAliasSetAddress
429a2900666SRuslan Ermilovis to be used.
430a2900666SRuslan ErmilovEven if
431a2900666SRuslan Ermilov.Fn PacketAliasSetAddress
432a2900666SRuslan Ermilovis called to change the address after
433a2900666SRuslan Ermilov.Fn PacketAliasRedirectAddr
4343efa11bbSBrian Somersis called, a zero reference will track this change.
435a2900666SRuslan Ermilov.Pp
4366d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then
4376d20a774SRuslan Ermilov.Fa local_addr
4386d20a774SRuslan Ermilovis ignored, and is selected dynamically from the server pool, as described in
4396d20a774SRuslan Ermilov.Fn PacketAliasAddServer
4406d20a774SRuslan Ermilovbelow.
4416d20a774SRuslan Ermilov.Pp
442a2900666SRuslan ErmilovIf subsequent calls to
443a2900666SRuslan Ermilov.Fn PacketAliasRedirectAddr
444a2900666SRuslan Ermilovuse the same aliasing address, all new incoming traffic to this aliasing
445a2900666SRuslan Ermilovaddress will be redirected to the local address made in the last function
446a2900666SRuslan Ermilovcall.
447a2900666SRuslan ErmilovNew traffic generated by any of the local machines, designated in the
448a2900666SRuslan Ermilovseveral function calls, will be aliased to the same address.
449a2900666SRuslan ErmilovConsider the following example:
450a2900666SRuslan Ermilov.Bd -literal -offset indent
4513efa11bbSBrian SomersPacketAliasRedirectAddr(inet_aton("192.168.0.2"),
4523efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
4533efa11bbSBrian SomersPacketAliasRedirectAddr(inet_aton("192.168.0.3"),
4543efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
4553efa11bbSBrian SomersPacketAliasRedirectAddr(inet_aton("192.168.0.4"),
4563efa11bbSBrian Somers                        inet_aton("141.221.254.101"));
4573efa11bbSBrian Somers.Ed
458a2900666SRuslan Ermilov.Pp
459a2900666SRuslan ErmilovAny outgoing connections such as
460a2900666SRuslan Ermilov.Xr telnet 1
461a2900666SRuslan Ermilovor
462a2900666SRuslan Ermilov.Xr ftp 1
463a2900666SRuslan Ermilovfrom 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
464a2900666SRuslan Ermilov141.221.254.101.
465a2900666SRuslan ErmilovAny incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
466a2900666SRuslan Ermilov.Pp
467a2900666SRuslan ErmilovAny calls to
468a2900666SRuslan Ermilov.Fn PacketAliasRedirectPort
469a2900666SRuslan Ermilovwill have precedence over address mappings designated by
470a2900666SRuslan Ermilov.Fn PacketAliasRedirectAddr .
471a2900666SRuslan Ermilov.Pp
472a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by
473a2900666SRuslan Ermilov.Fn PacketAliasRedirectDelete .
474a2900666SRuslan ErmilovIf
475a2900666SRuslan Ermilov.Dv NULL
476a2900666SRuslan Ermilovis returned, then the function call did not complete successfully.
477a2900666SRuslan Ermilov.Ed
478a2900666SRuslan Ermilov.Pp
4796d20a774SRuslan Ermilov.Ft int
4806d20a774SRuslan Ermilov.Fo PacketAliasAddServer
4816d20a774SRuslan Ermilov.Fa "struct alias_link *link"
4826d20a774SRuslan Ermilov.Fa "struct in_addr addr"
4836d20a774SRuslan Ermilov.Fa "u_short port"
4846d20a774SRuslan Ermilov.Fc
4856d20a774SRuslan Ermilov.Bd -ragged -offset indent
4866d20a774SRuslan ErmilovThis function sets the
4876d20a774SRuslan Ermilov.Fa link
4886d20a774SRuslan Ermilovup for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
4896d20a774SRuslan ErmilovLSNAT operates as follows.
4906d20a774SRuslan ErmilovA client attempts to access a server by using the server virtual address.
4916d20a774SRuslan ErmilovThe LSNAT router transparently redirects the request to one of the hosts
4926d20a774SRuslan Ermilovin server pool, selected using a real-time load sharing algorithm.
4936d20a774SRuslan ErmilovMultiple sessions may be initiated from the same client, and each session
4946d20a774SRuslan Ermilovcould be directed to a different host based on load balance across server
4956d20a774SRuslan Ermilovpool hosts at the time.
4966d20a774SRuslan ErmilovIf load share is desired for just a few specific services, the configuration
4976d20a774SRuslan Ermilovon LSNAT could be defined to restrict load share for just the services
4986d20a774SRuslan Ermilovdesired.
4996d20a774SRuslan Ermilov.Pp
5006d20a774SRuslan ErmilovCurrently, only the simplest selection algorithm is implemented, where a
5016d20a774SRuslan Ermilovhost is selected on a round-robin basis only, without regard to load on
5026d20a774SRuslan Ermilovthe host.
5036d20a774SRuslan Ermilov.Pp
5046d20a774SRuslan ErmilovFirst, the
5056d20a774SRuslan Ermilov.Fa link
5066d20a774SRuslan Ermilovis created by either
5076d20a774SRuslan Ermilov.Fn PacketAliasRedirectPort
5086d20a774SRuslan Ermilovor
5096d20a774SRuslan Ermilov.Fn PacketAliasRedirectAddr .
5106d20a774SRuslan ErmilovThen,
5116d20a774SRuslan Ermilov.Fn PacketAliasAddServer
5126d20a774SRuslan Ermilovis called multiple times to add entries to the
5136d20a774SRuslan Ermilov.Fa link Ns 's
5146d20a774SRuslan Ermilovserver pool.
5156d20a774SRuslan Ermilov.Pp
5166d20a774SRuslan ErmilovFor links created with
5176d20a774SRuslan Ermilov.Fn PacketAliasRedirectAddr ,
5186d20a774SRuslan Ermilovthe
5196d20a774SRuslan Ermilov.Fa port
5206d20a774SRuslan Ermilovargument is ignored and could have any value, e.g. htons(~0).
5216d20a774SRuslan Ermilov.Pp
5226d20a774SRuslan ErmilovThis function returns 0 on success, -1 otherwise.
5236d20a774SRuslan Ermilov.Ed
5246d20a774SRuslan Ermilov.Pp
5253efa11bbSBrian Somers.Ft void
526a2900666SRuslan Ermilov.Fn PacketAliasRedirectDelete "struct alias_link *link"
527a2900666SRuslan Ermilov.Bd -ragged -offset indent
528a2900666SRuslan ErmilovThis function will delete a specific static redirect rule entered by
529a2900666SRuslan Ermilov.Fn PacketAliasRedirectPort
530a2900666SRuslan Ermilovor
531a2900666SRuslan Ermilov.Fn PacketAliasRedirectAddr .
532a2900666SRuslan ErmilovThe parameter
533a2900666SRuslan Ermilov.Fa link
534a2900666SRuslan Ermilovis the pointer returned by either of the redirection functions.
535a2900666SRuslan ErmilovIf an invalid pointer is passed to
536a2900666SRuslan Ermilov.Fn PacketAliasRedirectDelete ,
537a2900666SRuslan Ermilovthen a program crash or unpredictable operation could result, so it is
5383efa11bbSBrian Somersnecessary to be careful using this function.
539a2900666SRuslan Ermilov.Ed
540a2900666SRuslan Ermilov.Pp
541619d1a30SBrian Somers.Ft int
54242889ed1SBrian Somers.Fn PacketAliasProxyRule "const char *cmd"
543a2900666SRuslan Ermilov.Bd -ragged -offset indent
544619d1a30SBrian SomersThe passed
545a2900666SRuslan Ermilov.Fa cmd
546a2900666SRuslan Ermilovstring consists of one or more pairs of words.
547a2900666SRuslan ErmilovThe first word in each pair is a token and the second is the value that
548a2900666SRuslan Ermilovshould be applied for that token.
549a2900666SRuslan ErmilovTokens and their argument types are as follows:
550a2900666SRuslan Ermilov.Bl -tag -width indent
551a2900666SRuslan Ermilov.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
552619d1a30SBrian SomersIn order to support transparent proxying, it is necessary to somehow
553619d1a30SBrian Somerspass the original address and port information into the new destination
554a2900666SRuslan Ermilovserver.
555a2900666SRuslan ErmilovIf
556a2900666SRuslan Ermilov.Cm encode_ip_hdr
557619d1a30SBrian Somersis specified, the original address and port is passed as an extra IP
558a2900666SRuslan Ermilovoption.
559a2900666SRuslan ErmilovIf
560a2900666SRuslan Ermilov.Cm encode_tcp_stream
561619d1a30SBrian Somersis specified, the original address and port is passed as the first
562a2900666SRuslan Ermilovpiece of data in the TCP stream in the format
563619d1a30SBrian Somers.Dq DEST Ar IP port .
564a2900666SRuslan Ermilov.It Cm port Ar portnum
565619d1a30SBrian SomersOnly packets with the destination port
566619d1a30SBrian Somers.Ar portnum
567619d1a30SBrian Somersare proxied.
568a2900666SRuslan Ermilov.It Cm server Ar host Ns Xo
569a2900666SRuslan Ermilov.Op : Ns Ar portnum
570a2900666SRuslan Ermilov.Xc
571619d1a30SBrian SomersThis specifies the
572619d1a30SBrian Somers.Ar host
573619d1a30SBrian Somersand
574619d1a30SBrian Somers.Ar portnum
575ac8e3334SBrian Somersthat the data is to be redirected to.
576ac8e3334SBrian Somers.Ar host
577a2900666SRuslan Ermilovmust be an IP address rather than a DNS host name.
578a2900666SRuslan ErmilovIf
579619d1a30SBrian Somers.Ar portnum
580619d1a30SBrian Somersis not specified, the destination port number is not changed.
581619d1a30SBrian Somers.Pp
582619d1a30SBrian SomersThe
583619d1a30SBrian Somers.Ar server
584619d1a30SBrian Somersspecification is mandatory unless the
585a2900666SRuslan Ermilov.Cm delete
586619d1a30SBrian Somerscommand is being used.
587a2900666SRuslan Ermilov.It Cm rule Ar index
588619d1a30SBrian SomersNormally, each call to
589619d1a30SBrian Somers.Fn PacketAliasProxyRule
590a2900666SRuslan Ermilovinserts the next rule at the start of a linear list of rules.
591a2900666SRuslan ErmilovIf an
592619d1a30SBrian Somers.Ar index
593619d1a30SBrian Somersis specified, the new rule will be checked after all rules with lower
594a2900666SRuslan Ermilovindices.
595a2900666SRuslan ErmilovCalls to
596619d1a30SBrian Somers.Fn PacketAliasProxyRule
597619d1a30SBrian Somersthat do not specify a rule are assigned rule 0.
598a2900666SRuslan Ermilov.It Cm delete Ar index
599a2900666SRuslan ErmilovThis token and its argument MUST NOT be used with any other tokens.
600a2900666SRuslan ErmilovWhen used, all existing rules with the given
601619d1a30SBrian Somers.Ar index
602619d1a30SBrian Somersare deleted.
603a2900666SRuslan Ermilov.It Cm proto tcp | udp
604619d1a30SBrian SomersIf specified, only packets of the given protocol type are matched.
605a2900666SRuslan Ermilov.It Cm src Ar IP Ns Xo
606a2900666SRuslan Ermilov.Op / Ns Ar bits
607a2900666SRuslan Ermilov.Xc
608619d1a30SBrian SomersIf specified, only packets with a source address matching the given
609619d1a30SBrian Somers.Ar IP
610a2900666SRuslan Ermilovare matched.
611a2900666SRuslan ErmilovIf
612619d1a30SBrian Somers.Ar bits
613619d1a30SBrian Somersis also specified, then the first
614619d1a30SBrian Somers.Ar bits
615619d1a30SBrian Somersbits of
616619d1a30SBrian Somers.Ar IP
617619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that
618619d1a30SBrian Somersnetwork will be matched.
619a2900666SRuslan Ermilov.It Cm dst Ar IP Ns Xo
620a2900666SRuslan Ermilov.Op / Ns Ar bits
621a2900666SRuslan Ermilov.Xc
622619d1a30SBrian SomersIf specified, only packets with a destination address matching the given
623619d1a30SBrian Somers.Ar IP
624a2900666SRuslan Ermilovare matched.
625a2900666SRuslan ErmilovIf
626619d1a30SBrian Somers.Ar bits
627619d1a30SBrian Somersis also specified, then the first
628619d1a30SBrian Somers.Ar bits
629619d1a30SBrian Somersbits of
630619d1a30SBrian Somers.Ar IP
631619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that
632619d1a30SBrian Somersnetwork will be matched.
633619d1a30SBrian Somers.El
634a2900666SRuslan Ermilov.Pp
635619d1a30SBrian SomersThis function is usually used to redirect outgoing connections for
636619d1a30SBrian Somersinternal machines that are not permitted certain types of internet
637619d1a30SBrian Somersaccess, or to restrict access to certain external machines.
638a2900666SRuslan Ermilov.Ed
639a2900666SRuslan Ermilov.Pp
640483d2f22SRuslan Ermilov.Ft struct alias_link *
64180607605SRuslan Ermilov.Fo PacketAliasRedirectProto
642483d2f22SRuslan Ermilov.Fa "struct in_addr local_addr"
643483d2f22SRuslan Ermilov.Fa "struct in_addr remote_addr"
644483d2f22SRuslan Ermilov.Fa "struct in_addr alias_addr"
64580607605SRuslan Ermilov.Fa "u_char proto"
646483d2f22SRuslan Ermilov.Fc
647483d2f22SRuslan Ermilov.Bd -ragged -offset indent
64880607605SRuslan ErmilovThis function specifies that any IP packet with protocol number of
64980607605SRuslan Ermilov.Fa proto
65080607605SRuslan Ermilovfrom a given remote address to an alias address be
651483d2f22SRuslan Ermilovredirected to a specified local address.
652483d2f22SRuslan Ermilov.Pp
653483d2f22SRuslan ErmilovIf
654483d2f22SRuslan Ermilov.Fa local_addr
655483d2f22SRuslan Ermilovor
656483d2f22SRuslan Ermilov.Fa alias_addr
657483d2f22SRuslan Ermilovis zero, this indicates that the packet aliasing address as established
658483d2f22SRuslan Ermilovby
659483d2f22SRuslan Ermilov.Fn PacketAliasSetAddress
660483d2f22SRuslan Ermilovis to be used.
661483d2f22SRuslan ErmilovEven if
662483d2f22SRuslan Ermilov.Fn PacketAliasSetAddress
663483d2f22SRuslan Ermilovis called to change the address after
66480607605SRuslan Ermilov.Fn PacketAliasRedirectProto
665483d2f22SRuslan Ermilovis called, a zero reference will track this change.
666483d2f22SRuslan Ermilov.Pp
667483d2f22SRuslan ErmilovIf
668483d2f22SRuslan Ermilov.Fa remote_addr
66980607605SRuslan Ermilovis zero, this indicates to redirect packets from any remote address.
670483d2f22SRuslan ErmilovNon-zero remote addresses can sometimes be useful for firewalling.
671483d2f22SRuslan Ermilov.Pp
672483d2f22SRuslan ErmilovIf two calls to
67380607605SRuslan Ermilov.Fn PacketAliasRedirectProto
674483d2f22SRuslan Ermilovoverlap in their address specifications, then the most recent call
675483d2f22SRuslan Ermilovwill have precedence.
676483d2f22SRuslan Ermilov.Pp
677483d2f22SRuslan ErmilovThis function returns a pointer which can subsequently be used by
678483d2f22SRuslan Ermilov.Fn PacketAliasRedirectDelete .
679483d2f22SRuslan ErmilovIf
680483d2f22SRuslan Ermilov.Dv NULL
681483d2f22SRuslan Ermilovis returned, then the function call did not complete successfully.
682483d2f22SRuslan Ermilov.Ed
683a2900666SRuslan Ermilov.Sh FRAGMENT HANDLING
684a2900666SRuslan ErmilovThe functions in this section are used to deal with incoming fragments.
685a2900666SRuslan Ermilov.Pp
686a2900666SRuslan ErmilovOutgoing fragments are handled within
687a2900666SRuslan Ermilov.Fn PacketAliasOut
688a2900666SRuslan Ermilovby changing the address according to any applicable mapping set by
6895e8fc2d2SRuslan Ermilov.Fn PacketAliasRedirectAddr ,
6901855100fSAlexey Zelkinor the default aliasing address set by
691a2900666SRuslan Ermilov.Fn PacketAliasSetAddress .
692a2900666SRuslan Ermilov.Pp
6931855100fSAlexey ZelkinIncoming fragments are handled in one of two ways.
694a2900666SRuslan ErmilovIf the header of a fragmented IP packet has already been seen, then all
695a2900666SRuslan Ermilovsubsequent fragments will be re-mapped in the same manner the header
696a2900666SRuslan Ermilovfragment was.
697a2900666SRuslan ErmilovFragments which arrive before the header are saved and then retrieved
698a2900666SRuslan Ermilovonce the header fragment has been resolved.
699a2900666SRuslan Ermilov.Pp
7003efa11bbSBrian Somers.Ft int
7013efa11bbSBrian Somers.Fn PacketAliasSaveFragment "char *ptr"
702a2900666SRuslan Ermilov.Bd -ragged -offset indent
703a2900666SRuslan ErmilovWhen
704a2900666SRuslan Ermilov.Fn PacketAliasIn
705a2900666SRuslan Ermilovreturns
706a2900666SRuslan Ermilov.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
707a2900666SRuslan Ermilovthis function can be used to save the pointer to the unresolved fragment.
708a2900666SRuslan Ermilov.Pp
7093efa11bbSBrian SomersIt is implicitly assumed that
710a2900666SRuslan Ermilov.Fa ptr
7113efa11bbSBrian Somerspoints to a block of memory allocated by
712a2900666SRuslan Ermilov.Xr malloc 3 .
713a2900666SRuslan ErmilovIf the fragment is never resolved, the packet aliasing engine will
714a2900666SRuslan Ermilovautomatically free the memory after a timeout period.
715a2900666SRuslan Ermilov[Eventually this function should be modified so that a callback function
716a2900666SRuslan Ermilovfor freeing memory is passed as an argument.]
717a2900666SRuslan Ermilov.Pp
718a2900666SRuslan ErmilovThis function returns
719a2900666SRuslan Ermilov.Dv PKT_ALIAS_OK
720a2900666SRuslan Ermilovif it was successful and
721a2900666SRuslan Ermilov.Dv PKT_ALIAS_ERROR
722a2900666SRuslan Ermilovif there was an error.
723a2900666SRuslan Ermilov.Ed
724a2900666SRuslan Ermilov.Pp
7253efa11bbSBrian Somers.Ft char *
7263efa11bbSBrian Somers.Fn PacketAliasGetFragment "char *buffer"
727a2900666SRuslan Ermilov.Bd -ragged -offset indent
728a2900666SRuslan ErmilovThis function can be used to retrieve fragment pointers saved by
729a2900666SRuslan Ermilov.Fn PacketAliasSaveFragment .
7303efa11bbSBrian SomersThe IP header fragment pointed to by
731a2900666SRuslan Ermilov.Fa buffer
7323efa11bbSBrian Somersis the header fragment indicated when
733a2900666SRuslan Ermilov.Fn PacketAliasIn
734a2900666SRuslan Ermilovreturns
735a2900666SRuslan Ermilov.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
736a2900666SRuslan ErmilovOnce a fragment pointer is retrieved, it becomes the calling program's
737a2900666SRuslan Ermilovresponsibility to free the dynamically allocated memory for the fragment.
738a2900666SRuslan Ermilov.Pp
739a2900666SRuslan Ermilov.Fn PacketAliasGetFragment
740a2900666SRuslan Ermilovcan be called sequentially until there are no more fragments available,
741a2900666SRuslan Ermilovat which time it returns
742a2900666SRuslan Ermilov.Dv NULL .
743a2900666SRuslan Ermilov.Ed
744a2900666SRuslan Ermilov.Pp
7453efa11bbSBrian Somers.Ft void
7463efa11bbSBrian Somers.Fn PacketAliasFragmentIn "char *header" "char *fragment"
747a2900666SRuslan Ermilov.Bd -ragged -offset indent
7483efa11bbSBrian SomersWhen a fragment is retrieved with
749a2900666SRuslan Ermilov.Fn PacketAliasGetFragment ,
750a2900666SRuslan Ermilovit can then be de-aliased with a call to
751a2900666SRuslan Ermilov.Fn PacketAliasFragmentIn .
752a2900666SRuslan ErmilovThe
753a2900666SRuslan Ermilov.Fa header
754a2900666SRuslan Ermilovargument is the pointer to a header fragment used as a template, and
755a2900666SRuslan Ermilov.Fa fragment
7563efa11bbSBrian Somersis the pointer to the packet to be de-aliased.
757a2900666SRuslan Ermilov.Ed
758a2900666SRuslan Ermilov.Sh MISCELLANEOUS FUNCTIONS
7593efa11bbSBrian Somers.Ft void
7603efa11bbSBrian Somers.Fn PacketAliasSetTarget "struct in_addr addr"
761a2900666SRuslan Ermilov.Bd -ragged -offset indent
762a2900666SRuslan ErmilovWhen an incoming packet not associated with any pre-existing aliasing link
763a2900666SRuslan Ermilovarrives at the host machine, it will be sent to the address indicated by a
764a2900666SRuslan Ermilovcall to
765a2900666SRuslan Ermilov.Fn PacketAliasSetTarget .
766a2900666SRuslan Ermilov.Pp
767151682eaSBrian SomersIf this function is called with an
768a2900666SRuslan Ermilov.Dv INADDR_NONE
769a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address set by
770a2900666SRuslan Ermilov.Fn PacketAliasSetAddress .
771a2900666SRuslan Ermilov.Pp
772151682eaSBrian SomersIf this function is not called, or is called with an
773a2900666SRuslan Ermilov.Dv INADDR_ANY
774a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address specified
775a2900666SRuslan Ermilovin the packet.
776a2900666SRuslan ErmilovThis allows external machines to talk directly to internal machines if they
777a2900666SRuslan Ermilovcan route packets to the machine in question.
778a2900666SRuslan Ermilov.Ed
779a2900666SRuslan Ermilov.Pp
7803efa11bbSBrian Somers.Ft int
781a2900666SRuslan Ermilov.Fn PacketAliasCheckNewLink void
782a2900666SRuslan Ermilov.Bd -ragged -offset indent
783a2900666SRuslan ErmilovThis function returns a non-zero value when a new aliasing link is created.
784a2900666SRuslan ErmilovIn circumstances where incoming traffic is being sequentially sent to
785a2900666SRuslan Ermilovdifferent local servers, this function can be used to trigger when
786a2900666SRuslan Ermilov.Fn PacketAliasSetTarget
7873efa11bbSBrian Somersis called to change the default target address.
788a2900666SRuslan Ermilov.Ed
789a2900666SRuslan Ermilov.Pp
7903efa11bbSBrian Somers.Ft u_short
791442a25bdSBruce Evans.Fn PacketAliasInternetChecksum "u_short *buffer" "int nbytes"
792a2900666SRuslan Ermilov.Bd -ragged -offset indent
793a2900666SRuslan ErmilovThis is a utility function that does not seem to be available elsewhere and
794a2900666SRuslan Ermilovis included as a convenience.
795a2900666SRuslan ErmilovIt computes the internet checksum, which is used in both IP and
796a2900666SRuslan Ermilovprotocol-specific headers (TCP, UDP, ICMP).
797a2900666SRuslan Ermilov.Pp
798a2900666SRuslan ErmilovThe
799a2900666SRuslan Ermilov.Fa buffer
800a2900666SRuslan Ermilovargument points to the data block to be checksummed, and
801a2900666SRuslan Ermilov.Fa nbytes
802a2900666SRuslan Ermilovis the number of bytes.
803a2900666SRuslan ErmilovThe 16-bit checksum field should be zeroed before computing the checksum.
804a2900666SRuslan Ermilov.Pp
805a2900666SRuslan ErmilovChecksums can also be verified by operating on a block of data including
806a2900666SRuslan Ermilovits checksum.
807a2900666SRuslan ErmilovIf the checksum is valid,
808a2900666SRuslan Ermilov.Fn PacketAliasInternetChecksum
809a2900666SRuslan Ermilovwill return zero.
810a2900666SRuslan Ermilov.Ed
811642e43b3SArchie Cobbs.Pp
812642e43b3SArchie Cobbs.Ft int
813642e43b3SArchie Cobbs.Fn PacketUnaliasOut "char *buffer" "int maxpacketsize"
814642e43b3SArchie Cobbs.Bd -ragged -offset indent
815642e43b3SArchie CobbsAn outgoing packet, which has already been aliased, has its private address/port
816642e43b3SArchie Cobbsinformation restored by this function.
817642e43b3SArchie CobbsThe IP packet is pointed to by
818642e43b3SArchie Cobbs.Fa buffer ,
819642e43b3SArchie Cobbsand
820642e43b3SArchie Cobbs.Fa maxpacketsize
821642e43b3SArchie Cobbsis provided for error checking purposes.
822642e43b3SArchie CobbsThis function can be used if an already-aliased packet needs to have its
823642e43b3SArchie Cobbsoriginal IP header restored for further processing (eg. logging).
824642e43b3SArchie Cobbs.Ed
825642e43b3SArchie Cobbs.Sh BUGS
826642e43b3SArchie CobbsPPTP aliasing does not work when more than one internal client
827642e43b3SArchie Cobbsconnects to the same external server at the same time, because
828642e43b3SArchie CobbsPPTP requires a single TCP control connection to be established
829642e43b3SArchie Cobbsbetween any two IP addresses.
830a2900666SRuslan Ermilov.Sh AUTHORS
831a2900666SRuslan Ermilov.An Charles Mott Aq cmott@scientech.com ,
832a2900666SRuslan Ermilovversions 1.0 - 1.8, 2.0 - 2.4.
833a2900666SRuslan Ermilov.An Eivind Eklund Aq eivind@FreeBSD.org ,
834a2900666SRuslan Ermilovversions 1.8b, 1.9 and 2.5.
835a2900666SRuslan ErmilovAdded IRC DCC support as well as contributing a number of architectural
836a2900666SRuslan Ermilovimprovements; added the firewall bypass for FTP/IRC DCC.
83755a39fc5SRuslan Ermilov.An Erik Salander Aq erik@whistle.com
838642e43b3SArchie Cobbsadded support for PPTP and RTSP.
839642e43b3SArchie Cobbs.An Junichi Satoh Aq junichi@junichi.org
840642e43b3SArchie Cobbsadded support for RTSP/PNA.
841a2900666SRuslan Ermilov.Sh ACKNOWLEDGMENTS
842a2900666SRuslan ErmilovListed below, in approximate chronological order, are individuals who
843a2900666SRuslan Ermilovhave provided valuable comments and/or debugging assistance.
844a2900666SRuslan Ermilov.Pp
845a2900666SRuslan Ermilov.Bl -item -offset indent -compact
846a2900666SRuslan Ermilov.It
847a2900666SRuslan ErmilovGary Roberts
848a2900666SRuslan Ermilov.It
849a2900666SRuslan ErmilovTom Torrance
850a2900666SRuslan Ermilov.It
851a2900666SRuslan ErmilovReto Burkhalter
852a2900666SRuslan Ermilov.It
853a2900666SRuslan ErmilovMartin Renters
854a2900666SRuslan Ermilov.It
855a2900666SRuslan ErmilovBrian Somers
856a2900666SRuslan Ermilov.It
857a2900666SRuslan ErmilovPaul Traina
858a2900666SRuslan Ermilov.It
859a2900666SRuslan ErmilovAri Suutari
860a2900666SRuslan Ermilov.It
861a2900666SRuslan ErmilovDave Remien
862a2900666SRuslan Ermilov.It
863a2900666SRuslan ErmilovJ. Fortes
864a2900666SRuslan Ermilov.It
865a2900666SRuslan ErmilovAndrzej Bialecki
866a2900666SRuslan Ermilov.It
867a2900666SRuslan ErmilovGordon Burditt
8683efa11bbSBrian Somers.El
869a2900666SRuslan Ermilov.Sh CONCEPTUAL BACKGROUND
870a2900666SRuslan ErmilovThis section is intended for those who are planning to modify the source
871a2900666SRuslan Ermilovcode or want to create somewhat esoteric applications using the packet
872a2900666SRuslan Ermilovaliasing functions.
873a2900666SRuslan Ermilov.Pp
874a2900666SRuslan ErmilovThe conceptual framework under which the packet aliasing engine operates
875a2900666SRuslan Ermilovis described here.
8763efa11bbSBrian SomersCentral to the discussion is the idea of an
877a2900666SRuslan Ermilov.Em aliasing link
878a2900666SRuslan Ermilovwhich describes the relationship for a given packet transaction between
879a2900666SRuslan Ermilovthe local machine, aliased identity and remote machine.
880a2900666SRuslan ErmilovIt is discussed how such links come into existence and are destroyed.
881a2900666SRuslan Ermilov.Ss ALIASING LINKS
882a2900666SRuslan ErmilovThere is a notion of an
883a2900666SRuslan Ermilov.Em aliasing link ,
884a2900666SRuslan Ermilovwhich is a 7-tuple describing a specific translation:
8853efa11bbSBrian Somers.Bd -literal -offset indent
8863efa11bbSBrian Somers(local addr, local port, alias addr, alias port,
8873efa11bbSBrian Somers remote addr, remote port, protocol)
8883efa11bbSBrian Somers.Ed
889a2900666SRuslan Ermilov.Pp
890a2900666SRuslan ErmilovOutgoing packets have the local address and port number replaced with the
891a2900666SRuslan Ermilovalias address and port number.
892a2900666SRuslan ErmilovIncoming packets undergo the reverse process.
893a2900666SRuslan ErmilovThe packet aliasing engine attempts to match packets against an internal
894a2900666SRuslan Ermilovtable of aliasing links to determine how to modify a given IP packet.
895a2900666SRuslan ErmilovBoth the IP header and protocol dependent headers are modified as necessary.
896a2900666SRuslan ErmilovAliasing links are created and deleted as necessary according to network
897a2900666SRuslan Ermilovtraffic.
898a2900666SRuslan Ermilov.Pp
899a2900666SRuslan ErmilovProtocols can be TCP, UDP or even ICMP in certain circumstances.
900a2900666SRuslan Ermilov(Some types of ICMP packets can be aliased according to sequence or ID
901a2900666SRuslan Ermilovnumber which acts as an equivalent port number for identifying how
902a2900666SRuslan Ermilovindividual packets should be handled.)
903a2900666SRuslan Ermilov.Pp
904a2900666SRuslan ErmilovEach aliasing link must have a unique combination of the following five
905a2900666SRuslan Ermilovquantities: alias address/port, remote address/port and protocol.
906a2900666SRuslan ErmilovThis ensures that several machines on a local network can share the
907a2900666SRuslan Ermilovsame aliasing IP address.
908a2900666SRuslan ErmilovIn cases where conflicts might arise, the aliasing port is chosen so that
909a2900666SRuslan Ermilovuniqueness is maintained.
910a2900666SRuslan Ermilov.Ss STATIC AND DYNAMIC LINKS
9113efa11bbSBrian SomersAliasing links can either be static or dynamic.
912a2900666SRuslan ErmilovStatic links persist indefinitely and represent fixed rules for translating
913a2900666SRuslan ErmilovIP packets.
914a2900666SRuslan ErmilovDynamic links come into existence for a specific TCP connection or UDP
915a2900666SRuslan Ermilovtransaction or ICMP ECHO sequence.
916a2900666SRuslan ErmilovFor the case of TCP, the connection can be monitored to see when the
917a2900666SRuslan Ermilovassociated aliasing link should be deleted.
918a2900666SRuslan ErmilovAliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
919a2900666SRuslan Ermilovwork on a simple timeout rule.
920a2900666SRuslan ErmilovWhen no activity is observed on a dynamic link for a certain amount of time
921a2900666SRuslan Ermilovit is automatically deleted.
922a2900666SRuslan ErmilovTimeout rules also apply to TCP connections which do not open or close
9233efa11bbSBrian Somersproperly.
924a2900666SRuslan Ermilov.Ss PARTIALLY SPECIFIED ALIASING LINKS
925a2900666SRuslan ErmilovAliasing links can be partially specified, meaning that the remote address
926a2900666SRuslan Ermilovand/or remote port are unknown.
927a2900666SRuslan ErmilovIn this case, when a packet matching the incomplete specification is found,
928a2900666SRuslan Ermilova fully specified dynamic link is created.
929a2900666SRuslan ErmilovIf the original partially specified link is dynamic, it will be deleted
930a2900666SRuslan Ermilovafter the fully specified link is created, otherwise it will persist.
931a2900666SRuslan Ermilov.Pp
932a2900666SRuslan ErmilovFor instance, a partially specified link might be
9333efa11bbSBrian Somers.Bd -literal -offset indent
9343efa11bbSBrian Somers(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
9353efa11bbSBrian Somers.Ed
936a2900666SRuslan Ermilov.Pp
937a2900666SRuslan ErmilovThe zeros denote unspecified components for the remote address and port.
938a2900666SRuslan ErmilovIf this link were static it would have the effect of redirecting all
939a2900666SRuslan Ermilovincoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
940a2900666SRuslan Ermilovof machine 192.168.0.4 on the local network.
941a2900666SRuslan ErmilovEach individual telnet connection would initiate the creation of a distinct
942a2900666SRuslan Ermilovdynamic link.
943a2900666SRuslan Ermilov.Ss DYNAMIC LINK CREATION
944a2900666SRuslan ErmilovIn addition to aliasing links, there are also address mappings that can be
945a2900666SRuslan Ermilovstored within the internal data table of the packet aliasing mechanism.
9463efa11bbSBrian Somers.Bd -literal -offset indent
9473efa11bbSBrian Somers(local addr, alias addr)
9483efa11bbSBrian Somers.Ed
949a2900666SRuslan Ermilov.Pp
950a2900666SRuslan ErmilovAddress mappings are searched when creating new dynamic links.
951a2900666SRuslan Ermilov.Pp
952a2900666SRuslan ErmilovAll outgoing packets from the local network automatically create a dynamic
953a2900666SRuslan Ermilovlink if they do not match an already existing fully specified link.
954a2900666SRuslan ErmilovIf an address mapping exists for the outgoing packet, this determines
955a2900666SRuslan Ermilovthe alias address to be used.
956a2900666SRuslan ErmilovIf no mapping exists, then a default address, usually the address of the
957a2900666SRuslan Ermilovpacket aliasing host, is used.
958a2900666SRuslan ErmilovIf necessary, this default address can be changed as often as each individual
959a2900666SRuslan Ermilovpacket arrives.
960a2900666SRuslan Ermilov.Pp
961a2900666SRuslan ErmilovThe aliasing port number is determined such that the new dynamic link does
962a2900666SRuslan Ermilovnot conflict with any existing links.
963a2900666SRuslan ErmilovIn the default operating mode, the packet aliasing engine attempts to set
964a2900666SRuslan Ermilovthe aliasing port equal to the local port number.
965a2900666SRuslan ErmilovIf this results in a conflict, then port numbers are randomly chosen until
966a2900666SRuslan Ermilova unique aliasing link can be established.
967a2900666SRuslan ErmilovIn an alternate operating mode, the first choice of an aliasing port is also
968a2900666SRuslan Ermilovrandom and unrelated to the local port number.
969