xref: /f-stack/freebsd/netinet/libalias/libalias.3 (revision 22ce4aff)
1a9643ea8Slogwang.\"-
2a9643ea8Slogwang.\" Copyright (c) 2001 Charles Mott <[email protected]>
3a9643ea8Slogwang.\" All rights reserved.
4a9643ea8Slogwang.\"
5a9643ea8Slogwang.\" Redistribution and use in source and binary forms, with or without
6a9643ea8Slogwang.\" modification, are permitted provided that the following conditions
7a9643ea8Slogwang.\" are met:
8a9643ea8Slogwang.\" 1. Redistributions of source code must retain the above copyright
9a9643ea8Slogwang.\"    notice, this list of conditions and the following disclaimer.
10a9643ea8Slogwang.\" 2. Redistributions in binary form must reproduce the above copyright
11a9643ea8Slogwang.\"    notice, this list of conditions and the following disclaimer in the
12a9643ea8Slogwang.\"    documentation and/or other materials provided with the distribution.
13a9643ea8Slogwang.\"
14a9643ea8Slogwang.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a9643ea8Slogwang.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a9643ea8Slogwang.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a9643ea8Slogwang.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a9643ea8Slogwang.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a9643ea8Slogwang.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a9643ea8Slogwang.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a9643ea8Slogwang.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a9643ea8Slogwang.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a9643ea8Slogwang.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a9643ea8Slogwang.\" SUCH DAMAGE.
25a9643ea8Slogwang.\"
26a9643ea8Slogwang.\" $FreeBSD$
27a9643ea8Slogwang.\"
28*22ce4affSfengbojiang.Dd January 1, 2020
29a9643ea8Slogwang.Dt LIBALIAS 3
30a9643ea8Slogwang.Os
31a9643ea8Slogwang.Sh NAME
32a9643ea8Slogwang.Nm libalias
33a9643ea8Slogwang.Nd packet aliasing library for masquerading and network address translation
34a9643ea8Slogwang.Sh SYNOPSIS
35a9643ea8Slogwang.In sys/types.h
36a9643ea8Slogwang.In netinet/in.h
37a9643ea8Slogwang.In alias.h
38a9643ea8Slogwang.Pp
39a9643ea8SlogwangFunction prototypes are given in the main body of the text.
40a9643ea8Slogwang.Sh DESCRIPTION
41a9643ea8SlogwangThe
42a9643ea8Slogwang.Nm
43a9643ea8Slogwanglibrary is a collection of functions for aliasing and de-aliasing of IP
44a9643ea8Slogwangpackets, intended for masquerading and network address translation (NAT).
45a9643ea8Slogwang.Sh INTRODUCTION
46a9643ea8SlogwangThis library is a moderately portable set of functions designed to assist
47a9643ea8Slogwangin the process of IP masquerading and network address translation.
48a9643ea8SlogwangOutgoing packets from a local network with unregistered IP addresses can
49a9643ea8Slogwangbe aliased to appear as if they came from an accessible IP address.
50a9643ea8SlogwangIncoming packets are then de-aliased so that they are sent to the correct
51a9643ea8Slogwangmachine on the local network.
52a9643ea8Slogwang.Pp
53a9643ea8SlogwangA certain amount of flexibility is built into the packet aliasing engine.
54a9643ea8SlogwangIn the simplest mode of operation, a many-to-one address mapping takes
55a9643ea8Slogwangplace between the local network and the packet aliasing host.
56a9643ea8SlogwangThis is known as IP masquerading.
57a9643ea8SlogwangIn addition, one-to-one mappings between local and public addresses can
58a9643ea8Slogwangalso be implemented, which is known as static NAT.
59a9643ea8SlogwangIn between these extremes, different groups of private addresses can be
60a9643ea8Slogwanglinked to different public addresses, comprising several distinct
61a9643ea8Slogwangmany-to-one mappings.
62a9643ea8SlogwangAlso, a given public address and port can be statically redirected to a
63a9643ea8Slogwangprivate address/port.
64a9643ea8Slogwang.Sh INITIALIZATION AND CONTROL
65a9643ea8SlogwangOne special function,
66a9643ea8Slogwang.Fn LibAliasInit ,
67a9643ea8Slogwangmust always be called before any packet handling may be performed, and
68a9643ea8Slogwangthe returned instance pointer must be passed to all the other functions.
69a9643ea8SlogwangNormally, the
70a9643ea8Slogwang.Fn LibAliasSetAddress
71a9643ea8Slogwangfunction is called afterwards, to set the default aliasing address.
72a9643ea8SlogwangIn addition, the operating mode of the packet aliasing engine can be
73a9643ea8Slogwangcustomized by calling
74a9643ea8Slogwang.Fn LibAliasSetMode .
75a9643ea8Slogwang.Pp
76a9643ea8Slogwang.Ft "struct libalias *"
77a9643ea8Slogwang.Fn LibAliasInit "struct libalias *"
78a9643ea8Slogwang.Bd -ragged -offset indent
79a9643ea8SlogwangThis function is used to initialize
80a9643ea8Slogwanginternal data structures.
81a9643ea8SlogwangWhen called the first time, a
82a9643ea8Slogwang.Dv NULL
83a9643ea8Slogwangpointer should be passed as an argument.
84a9643ea8SlogwangThe following mode bits are always set after calling
85a9643ea8Slogwang.Fn LibAliasInit .
86a9643ea8SlogwangSee the description of
87a9643ea8Slogwang.Fn LibAliasSetMode
88a9643ea8Slogwangbelow for the meaning of these mode bits.
89a9643ea8Slogwang.Pp
90a9643ea8Slogwang.Bl -item -offset indent -compact
91a9643ea8Slogwang.It
92a9643ea8Slogwang.Dv PKT_ALIAS_SAME_PORTS
93a9643ea8Slogwang.It
94a9643ea8Slogwang.Dv PKT_ALIAS_USE_SOCKETS
95a9643ea8Slogwang.It
96a9643ea8Slogwang.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
97a9643ea8Slogwang.El
98a9643ea8Slogwang.Pp
99a9643ea8SlogwangThis function will always return the packet aliasing engine to the same
100a9643ea8Slogwanginitial state.
101a9643ea8SlogwangThe
102a9643ea8Slogwang.Fn LibAliasSetAddress
103a9643ea8Slogwangfunction is normally called afterwards, and any desired changes from the
104a9643ea8Slogwangdefault mode bits listed above require a call to
105a9643ea8Slogwang.Fn LibAliasSetMode .
106a9643ea8Slogwang.Pp
107a9643ea8SlogwangIt is mandatory that this function be called at the beginning of a program
108a9643ea8Slogwangprior to any packet handling.
109a9643ea8Slogwang.Ed
110a9643ea8Slogwang.Pp
111a9643ea8Slogwang.Ft void
112a9643ea8Slogwang.Fn LibAliasUninit "struct libalias *"
113a9643ea8Slogwang.Bd -ragged -offset indent
114a9643ea8SlogwangThis function has no return value and is used to clear any
115a9643ea8Slogwangresources attached to internal data structures.
116a9643ea8Slogwang.Pp
117a9643ea8SlogwangThis function should be called when a program stops using the aliasing
118a9643ea8Slogwangengine; amongst other things, it clears out any firewall holes.
119a9643ea8SlogwangTo provide backwards compatibility and extra security, it is added to
120a9643ea8Slogwangthe
121a9643ea8Slogwang.Xr atexit 3
122a9643ea8Slogwangchain by
123a9643ea8Slogwang.Fn LibAliasInit .
124a9643ea8Slogwang.Ed
125a9643ea8Slogwang.Pp
126a9643ea8Slogwang.Ft void
127a9643ea8Slogwang.Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr"
128a9643ea8Slogwang.Bd -ragged -offset indent
129a9643ea8SlogwangThis function sets the source address to which outgoing packets from the
130a9643ea8Slogwanglocal area network are aliased.
131a9643ea8SlogwangAll outgoing packets are re-mapped to this address unless overridden by a
132a9643ea8Slogwangstatic address mapping established by
133a9643ea8Slogwang.Fn LibAliasRedirectAddr .
134a9643ea8SlogwangIf this function has not been called, and no static rules match, an outgoing
135a9643ea8Slogwangpacket retains its source address.
136a9643ea8Slogwang.Pp
137a9643ea8SlogwangIf the
138a9643ea8Slogwang.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
139a9643ea8Slogwangmode bit is set (the default mode of operation), then the internal aliasing
140a9643ea8Slogwanglink tables will be reset any time the aliasing address changes.
141a9643ea8SlogwangThis is useful for interfaces such as
142a9643ea8Slogwang.Xr ppp 8 ,
143a9643ea8Slogwangwhere the IP
144a9643ea8Slogwangaddress may or may not change on successive dial-up attempts.
145a9643ea8Slogwang.Pp
146a9643ea8SlogwangIf the
147a9643ea8Slogwang.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
148a9643ea8Slogwangmode bit is set to zero, this function can also be used to dynamically change
149a9643ea8Slogwangthe aliasing address on a packet-to-packet basis (it is a low overhead call).
150a9643ea8Slogwang.Pp
151a9643ea8SlogwangIt is mandatory that this function be called prior to any packet handling.
152a9643ea8Slogwang.Ed
153a9643ea8Slogwang.Pp
154a9643ea8Slogwang.Ft unsigned int
155a9643ea8Slogwang.Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask"
156a9643ea8Slogwang.Bd -ragged -offset indent
157a9643ea8SlogwangThis function sets or clears mode bits
158a9643ea8Slogwangaccording to the value of
159a9643ea8Slogwang.Fa flags .
160a9643ea8SlogwangOnly bits marked in
161a9643ea8Slogwang.Fa mask
162a9643ea8Slogwangare affected.
163a9643ea8SlogwangThe following mode bits are defined in
164a9643ea8Slogwang.In alias.h :
165a9643ea8Slogwang.Bl -tag -width indent
166a9643ea8Slogwang.It Dv PKT_ALIAS_LOG
167a9643ea8SlogwangEnables logging into
168a9643ea8Slogwang.Pa /var/log/alias.log .
169a9643ea8SlogwangEach time an aliasing link is created or deleted, the log file is appended to
170a9643ea8Slogwangwith the current number of ICMP, TCP and UDP links.
171a9643ea8SlogwangMainly useful for debugging when the log file is viewed continuously with
172a9643ea8Slogwang.Xr tail 1 .
173a9643ea8Slogwang.It Dv PKT_ALIAS_DENY_INCOMING
174a9643ea8SlogwangIf this mode bit is set, all incoming packets associated with new TCP
175a9643ea8Slogwangconnections or new UDP transactions will be marked for being ignored
176a9643ea8Slogwang.Po
177a9643ea8Slogwang.Fn LibAliasIn
178a9643ea8Slogwangreturns
179a9643ea8Slogwang.Dv PKT_ALIAS_IGNORED
180a9643ea8Slogwangcode
181a9643ea8Slogwang.Pc
182a9643ea8Slogwangby the calling program.
183a9643ea8SlogwangResponse packets to connections or transactions initiated from the packet
184a9643ea8Slogwangaliasing host or local network will be unaffected.
185a9643ea8SlogwangThis mode bit is useful for implementing a one-way firewall.
186a9643ea8Slogwang.It Dv PKT_ALIAS_SAME_PORTS
187a9643ea8SlogwangIf this mode bit is set, the packet-aliasing engine will attempt to leave
188a9643ea8Slogwangthe alias port numbers unchanged from the actual local port numbers.
189a9643ea8SlogwangThis can be done as long as the quintuple (proto, alias addr, alias port,
190a9643ea8Slogwangremote addr, remote port) is unique.
191a9643ea8SlogwangIf a conflict exists, a new aliasing port number is chosen even if this
192a9643ea8Slogwangmode bit is set.
193a9643ea8Slogwang.It Dv PKT_ALIAS_USE_SOCKETS
194a9643ea8SlogwangThis bit should be set when the packet aliasing host originates network
195a9643ea8Slogwangtraffic as well as forwards it.
196a9643ea8SlogwangWhen the packet aliasing host is waiting for a connection from an unknown
197a9643ea8Slogwanghost address or unknown port number (e.g.\& an FTP data connection), this
198a9643ea8Slogwangmode bit specifies that a socket be allocated as a place holder to prevent
199a9643ea8Slogwangport conflicts.
200a9643ea8SlogwangOnce a connection is established, usually within a minute or so, the socket
201a9643ea8Slogwangis closed.
202a9643ea8Slogwang.It Dv PKT_ALIAS_UNREGISTERED_ONLY
203a9643ea8SlogwangIf this mode bit is set, traffic on the local network which does not
204a9643ea8Slogwangoriginate from unregistered address spaces will be ignored.
205a9643ea8SlogwangStandard Class A, B and C unregistered addresses are:
206a9643ea8Slogwang.Pp
207a9643ea8Slogwang10.0.0.0     ->  10.255.255.255   (Class A subnet)
208a9643ea8Slogwang172.16.0.0   ->  172.31.255.255   (Class B subnets)
209a9643ea8Slogwang192.168.0.0  ->  192.168.255.255  (Class C subnets)
210a9643ea8Slogwang.Pp
211a9643ea8SlogwangThis option is useful in the case that the packet aliasing host has both
212a9643ea8Slogwangregistered and unregistered subnets on different interfaces.
213a9643ea8SlogwangThe registered subnet is fully accessible to the outside world, so traffic
214a9643ea8Slogwangfrom it does not need to be passed through the packet aliasing engine.
215*22ce4affSfengbojiang.It Dv PKT_ALIAS_UNREGISTERED_CGN
216*22ce4affSfengbojiangLike PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598 (Carrier Grade
217*22ce4affSfengbojiangNAT) subnet as follows:
218*22ce4affSfengbojiang.Pp
219*22ce4affSfengbojiang100.64.0.0   ->  100.127.255.255  (RFC 6598 subnet)
220a9643ea8Slogwang.It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
221a9643ea8SlogwangWhen this mode bit is set and
222a9643ea8Slogwang.Fn LibAliasSetAddress
223a9643ea8Slogwangis called to change the aliasing address, the internal link table of the
224a9643ea8Slogwangpacket aliasing engine will be cleared.
225a9643ea8SlogwangThis operating mode is useful for
226a9643ea8Slogwang.Xr ppp 8
227a9643ea8Slogwanglinks where the interface address can sometimes change or remain the same
228a9643ea8Slogwangbetween dial-up attempts.
229a9643ea8SlogwangIf this mode bit is not set, the link table will never be reset in the event
230a9643ea8Slogwangof an address change.
231a9643ea8Slogwang.It Dv PKT_ALIAS_PUNCH_FW
232a9643ea8SlogwangThis option makes
233a9643ea8Slogwang.Nm
234a9643ea8Slogwang.Dq punch holes
235a9643ea8Slogwangin an
236a9643ea8Slogwang.Xr ipfirewall 4 -
237a9643ea8Slogwangbased firewall for FTP/IRC DCC connections.
238a9643ea8SlogwangThe holes punched are bound by from/to IP address and port; it will not be
239a9643ea8Slogwangpossible to use a hole for another connection.
240a9643ea8SlogwangA hole is removed when the connection that uses it dies.
241a9643ea8SlogwangTo cater to unexpected death of a program using
242a9643ea8Slogwang.Nm
243a9643ea8Slogwang(e.g.\& kill -9),
244a9643ea8Slogwangchanging the state of the flag will clear the entire firewall range
245a9643ea8Slogwangallocated for holes.
246a9643ea8SlogwangThis clearing will also happen on the initial call to
247a9643ea8Slogwang.Fn LibAliasSetFWBase ,
248a9643ea8Slogwangwhich must happen prior to setting this flag.
249a9643ea8Slogwang.It Dv PKT_ALIAS_REVERSE
250a9643ea8SlogwangThis option makes
251a9643ea8Slogwang.Nm
252a9643ea8Slogwangreverse the way it handles incoming and outgoing packets, allowing it
253a9643ea8Slogwangto be fed with data that passes through the internal interface rather
254a9643ea8Slogwangthan the external one.
255a9643ea8Slogwang.It Dv PKT_ALIAS_PROXY_ONLY
256a9643ea8SlogwangThis option tells
257a9643ea8Slogwang.Nm
258a9643ea8Slogwangto obey transparent proxy rules only.
259a9643ea8SlogwangNormal packet aliasing is not performed.
260a9643ea8SlogwangSee
261a9643ea8Slogwang.Fn LibAliasProxyRule
262a9643ea8Slogwangbelow for details.
263a9643ea8Slogwang.It Dv PKT_ALIAS_SKIP_GLOBAL
264a9643ea8SlogwangThis option is used by
265a9643ea8Slogwang.Pa ipfw_nat
266a9643ea8Slogwangonly.
267a9643ea8SlogwangSpecifying it as a flag to
268a9643ea8Slogwang.Fn LibAliasSetMode
269a9643ea8Slogwanghas no effect.
270a9643ea8SlogwangSee section
271a9643ea8Slogwang.Sx NETWORK ADDRESS TRANSLATION
272a9643ea8Slogwangin
273a9643ea8Slogwang.Xr ipfw 8
274a9643ea8Slogwangfor more details.
275a9643ea8Slogwang.El
276a9643ea8Slogwang.Ed
277a9643ea8Slogwang.Pp
278a9643ea8Slogwang.Ft void
279a9643ea8Slogwang.Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num"
280a9643ea8Slogwang.Bd -ragged -offset indent
281a9643ea8SlogwangSet the firewall range allocated for punching firewall holes (with the
282a9643ea8Slogwang.Dv PKT_ALIAS_PUNCH_FW
283a9643ea8Slogwangflag).
284a9643ea8SlogwangThe range is cleared for all rules on initialization.
285a9643ea8Slogwang.Ed
286a9643ea8Slogwang.Pp
287a9643ea8Slogwang.Ft void
288a9643ea8Slogwang.Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port"
289a9643ea8Slogwang.Bd -ragged -offset indent
290a9643ea8SlogwangSet the TCP port used by the Skinny Station protocol.
291a9643ea8SlogwangSkinny is used by Cisco IP phones to communicate with
292a9643ea8SlogwangCisco Call Managers to set up voice over IP calls.
293a9643ea8SlogwangIf this is not set, Skinny aliasing will not be done.
294a9643ea8SlogwangThe typical port used by Skinny is 2000.
295a9643ea8Slogwang.Ed
296a9643ea8Slogwang.Sh PACKET HANDLING
297a9643ea8SlogwangThe packet handling functions are used to modify incoming (remote to local)
298a9643ea8Slogwangand outgoing (local to remote) packets.
299a9643ea8SlogwangThe calling program is responsible for receiving and sending packets via
300a9643ea8Slogwangnetwork interfaces.
301a9643ea8Slogwang.Pp
302a9643ea8SlogwangAlong with
303a9643ea8Slogwang.Fn LibAliasInit
304a9643ea8Slogwangand
305a9643ea8Slogwang.Fn LibAliasSetAddress ,
306a9643ea8Slogwangthe two packet handling functions,
307a9643ea8Slogwang.Fn LibAliasIn
308a9643ea8Slogwangand
309a9643ea8Slogwang.Fn LibAliasOut ,
310a9643ea8Slogwangcomprise the minimal set of functions needed for a basic IP masquerading
311a9643ea8Slogwangimplementation.
312a9643ea8Slogwang.Pp
313a9643ea8Slogwang.Ft int
314*22ce4affSfengbojiang.Fn LibAliasIn "struct libalias *" "void *buffer" "int maxpacketsize"
315a9643ea8Slogwang.Bd -ragged -offset indent
316a9643ea8SlogwangAn incoming packet coming from a remote machine to the local network is
317a9643ea8Slogwangde-aliased by this function.
318a9643ea8SlogwangThe IP packet is pointed to by
319a9643ea8Slogwang.Fa buffer ,
320a9643ea8Slogwangand
321a9643ea8Slogwang.Fa maxpacketsize
322a9643ea8Slogwangindicates the size of the data structure containing the packet and should
323a9643ea8Slogwangbe at least as large as the actual packet size.
324a9643ea8Slogwang.Pp
325a9643ea8SlogwangReturn codes:
326a9643ea8Slogwang.Bl -tag -width indent
327a9643ea8Slogwang.It Dv PKT_ALIAS_OK
328a9643ea8SlogwangThe packet aliasing process was successful.
329a9643ea8Slogwang.It Dv PKT_ALIAS_IGNORED
330a9643ea8SlogwangThe packet was ignored and not de-aliased.
331a9643ea8SlogwangThis can happen if the protocol is unrecognized, as for an ICMP message
332a9643ea8Slogwangtype that is not handled, or if incoming packets for new connections are being
333a9643ea8Slogwangignored (if the
334a9643ea8Slogwang.Dv PKT_ALIAS_DENY_INCOMING
335a9643ea8Slogwangmode bit was set using
336a9643ea8Slogwang.Fn LibAliasSetMode ) .
337a9643ea8Slogwang.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
338a9643ea8SlogwangThis is returned when a fragment cannot be resolved because the header
339a9643ea8Slogwangfragment has not been sent yet.
340a9643ea8SlogwangIn this situation, fragments must be saved with
341a9643ea8Slogwang.Fn LibAliasSaveFragment
342a9643ea8Slogwanguntil a header fragment is found.
343a9643ea8Slogwang.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
344a9643ea8SlogwangThe packet aliasing process was successful, and a header fragment was found.
345a9643ea8SlogwangThis is a signal to retrieve any unresolved fragments with
346a9643ea8Slogwang.Fn LibAliasGetFragment
347a9643ea8Slogwangand de-alias them with
348a9643ea8Slogwang.Fn LibAliasFragmentIn .
349a9643ea8Slogwang.It Dv PKT_ALIAS_ERROR
350a9643ea8SlogwangAn internal error within the packet aliasing engine occurred.
351a9643ea8Slogwang.El
352a9643ea8Slogwang.Ed
353a9643ea8Slogwang.Pp
354a9643ea8Slogwang.Ft int
355*22ce4affSfengbojiang.Fn LibAliasOut "struct libalias *" "void *buffer" "int maxpacketsize"
356a9643ea8Slogwang.Bd -ragged -offset indent
357a9643ea8SlogwangAn outgoing packet coming from the local network to a remote machine is
358a9643ea8Slogwangaliased by this function.
359a9643ea8SlogwangThe IP packet is pointed to by
360a9643ea8Slogwang.Fa buffer ,
361a9643ea8Slogwangand
362a9643ea8Slogwang.Fa maxpacketsize
363a9643ea8Slogwangindicates the maximum packet size permissible should the packet length be
364a9643ea8Slogwangchanged.
365a9643ea8SlogwangIP encoding protocols place address and port information in the encapsulated
366a9643ea8Slogwangdata stream which has to be modified and can account for changes in packet
367a9643ea8Slogwanglength.
368a9643ea8SlogwangWell known examples of such protocols are FTP and IRC DCC.
369a9643ea8Slogwang.Pp
370a9643ea8SlogwangReturn codes:
371a9643ea8Slogwang.Bl -tag -width indent
372a9643ea8Slogwang.It Dv PKT_ALIAS_OK
373a9643ea8SlogwangThe packet aliasing process was successful.
374a9643ea8Slogwang.It Dv PKT_ALIAS_IGNORED
375a9643ea8SlogwangThe packet was ignored and not aliased.
376a9643ea8SlogwangThis can happen if the protocol is unrecognized, or possibly an ICMP message
377a9643ea8Slogwangtype is not handled.
378a9643ea8Slogwang.It Dv PKT_ALIAS_ERROR
379a9643ea8SlogwangAn internal error within the packet aliasing engine occurred.
380a9643ea8Slogwang.El
381a9643ea8Slogwang.Ed
382a9643ea8Slogwang.Sh PORT AND ADDRESS REDIRECTION
383a9643ea8SlogwangThe functions described in this section allow machines on the local network
384a9643ea8Slogwangto be accessible in some degree to new incoming connections from the external
385a9643ea8Slogwangnetwork.
386a9643ea8SlogwangIndividual ports can be re-mapped or static network address translations can
387a9643ea8Slogwangbe designated.
388a9643ea8Slogwang.Pp
389a9643ea8Slogwang.Ft struct alias_link *
390a9643ea8Slogwang.Fo LibAliasRedirectPort
391a9643ea8Slogwang.Fa "struct libalias *"
392a9643ea8Slogwang.Fa "struct in_addr local_addr"
393a9643ea8Slogwang.Fa "u_short local_port"
394a9643ea8Slogwang.Fa "struct in_addr remote_addr"
395a9643ea8Slogwang.Fa "u_short remote_port"
396a9643ea8Slogwang.Fa "struct in_addr alias_addr"
397a9643ea8Slogwang.Fa "u_short alias_port"
398a9643ea8Slogwang.Fa "u_char proto"
399a9643ea8Slogwang.Fc
400a9643ea8Slogwang.Bd -ragged -offset indent
401a9643ea8SlogwangThis function specifies that traffic from a given remote address/port to
402a9643ea8Slogwangan alias address/port be redirected to a specified local address/port.
403a9643ea8SlogwangThe parameter
404a9643ea8Slogwang.Fa proto
405a9643ea8Slogwangcan be either
406a9643ea8Slogwang.Dv IPPROTO_TCP
407a9643ea8Slogwangor
408a9643ea8Slogwang.Dv IPPROTO_UDP ,
409a9643ea8Slogwangas defined in
410a9643ea8Slogwang.In netinet/in.h .
411a9643ea8Slogwang.Pp
412a9643ea8SlogwangIf
413a9643ea8Slogwang.Fa local_addr
414a9643ea8Slogwangor
415a9643ea8Slogwang.Fa alias_addr
416a9643ea8Slogwangis zero, this indicates that the packet aliasing address as established
417a9643ea8Slogwangby
418a9643ea8Slogwang.Fn LibAliasSetAddress
419a9643ea8Slogwangis to be used.
420a9643ea8SlogwangEven if
421a9643ea8Slogwang.Fn LibAliasSetAddress
422a9643ea8Slogwangis called to change the address after
423a9643ea8Slogwang.Fn LibAliasRedirectPort
424a9643ea8Slogwangis called, a zero reference will track this change.
425a9643ea8Slogwang.Pp
426a9643ea8SlogwangIf the link is further set up to operate with load sharing, then
427a9643ea8Slogwang.Fa local_addr
428a9643ea8Slogwangand
429a9643ea8Slogwang.Fa local_port
430a9643ea8Slogwangare ignored, and are selected dynamically from the server pool, as described in
431a9643ea8Slogwang.Fn LibAliasAddServer
432a9643ea8Slogwangbelow.
433a9643ea8Slogwang.Pp
434a9643ea8SlogwangIf
435a9643ea8Slogwang.Fa remote_addr
436a9643ea8Slogwangis zero, this indicates to redirect packets from any remote address.
437a9643ea8SlogwangLikewise, if
438a9643ea8Slogwang.Fa remote_port
439a9643ea8Slogwangis zero, this indicates to redirect packets originating from any remote
440a9643ea8Slogwangport number.
441a9643ea8SlogwangThe remote port specification will almost always be zero, but non-zero
442a9643ea8Slogwangremote addresses can sometimes be useful for firewalling.
443a9643ea8SlogwangIf two calls to
444a9643ea8Slogwang.Fn LibAliasRedirectPort
445a9643ea8Slogwangoverlap in their address/port specifications, then the most recent call
446a9643ea8Slogwangwill have precedence.
447a9643ea8Slogwang.Pp
448a9643ea8SlogwangThis function returns a pointer which can subsequently be used by
449a9643ea8Slogwang.Fn LibAliasRedirectDelete .
450a9643ea8SlogwangIf
451a9643ea8Slogwang.Dv NULL
452a9643ea8Slogwangis returned, then the function call did not complete successfully.
453a9643ea8Slogwang.Pp
454a9643ea8SlogwangAll port numbers should be in network address byte order, so it is necessary
455a9643ea8Slogwangto use
456a9643ea8Slogwang.Xr htons 3
457a9643ea8Slogwangto convert these parameters from internally readable numbers to network byte
458a9643ea8Slogwangorder.
459a9643ea8SlogwangAddresses are also in network byte order, which is implicit in the use of the
460a9643ea8Slogwang.Fa struct in_addr
461a9643ea8Slogwangdata type.
462a9643ea8Slogwang.Ed
463a9643ea8Slogwang.Pp
464a9643ea8Slogwang.Ft struct alias_link *
465a9643ea8Slogwang.Fo LibAliasRedirectAddr
466a9643ea8Slogwang.Fa "struct libalias *"
467a9643ea8Slogwang.Fa "struct in_addr local_addr"
468a9643ea8Slogwang.Fa "struct in_addr alias_addr"
469a9643ea8Slogwang.Fc
470a9643ea8Slogwang.Bd -ragged -offset indent
471a9643ea8SlogwangThis function designates that all incoming traffic to
472a9643ea8Slogwang.Fa alias_addr
473a9643ea8Slogwangbe redirected to
474a9643ea8Slogwang.Fa local_addr .
475a9643ea8SlogwangSimilarly, all outgoing traffic from
476a9643ea8Slogwang.Fa local_addr
477a9643ea8Slogwangis aliased to
478a9643ea8Slogwang.Fa alias_addr .
479a9643ea8Slogwang.Pp
480a9643ea8SlogwangIf
481a9643ea8Slogwang.Fa local_addr
482a9643ea8Slogwangor
483a9643ea8Slogwang.Fa alias_addr
484a9643ea8Slogwangis zero, this indicates that the packet aliasing address as established by
485a9643ea8Slogwang.Fn LibAliasSetAddress
486a9643ea8Slogwangis to be used.
487a9643ea8SlogwangEven if
488a9643ea8Slogwang.Fn LibAliasSetAddress
489a9643ea8Slogwangis called to change the address after
490a9643ea8Slogwang.Fn LibAliasRedirectAddr
491a9643ea8Slogwangis called, a zero reference will track this change.
492a9643ea8Slogwang.Pp
493a9643ea8SlogwangIf the link is further set up to operate with load sharing, then the
494a9643ea8Slogwang.Fa local_addr
495a9643ea8Slogwangargument is ignored, and is selected dynamically from the server pool,
496a9643ea8Slogwangas described in
497a9643ea8Slogwang.Fn LibAliasAddServer
498a9643ea8Slogwangbelow.
499a9643ea8Slogwang.Pp
500a9643ea8SlogwangIf subsequent calls to
501a9643ea8Slogwang.Fn LibAliasRedirectAddr
502a9643ea8Slogwanguse the same aliasing address, all new incoming traffic to this aliasing
503a9643ea8Slogwangaddress will be redirected to the local address made in the last function
504a9643ea8Slogwangcall.
505a9643ea8SlogwangNew traffic generated by any of the local machines, designated in the
506a9643ea8Slogwangseveral function calls, will be aliased to the same address.
507a9643ea8SlogwangConsider the following example:
508a9643ea8Slogwang.Pp
509a9643ea8SlogwangLibAliasRedirectAddr(la, inet_aton("192.168.0.2"),
510a9643ea8Slogwang                        inet_aton("141.221.254.101"));
511a9643ea8SlogwangLibAliasRedirectAddr(la, inet_aton("192.168.0.3"),
512a9643ea8Slogwang                        inet_aton("141.221.254.101"));
513a9643ea8SlogwangLibAliasRedirectAddr(la, inet_aton("192.168.0.4"),
514a9643ea8Slogwang                        inet_aton("141.221.254.101"));
515a9643ea8Slogwang.Pp
516a9643ea8SlogwangAny outgoing connections such as
517a9643ea8Slogwang.Xr telnet 1
518a9643ea8Slogwangor
519a9643ea8Slogwang.Xr ftp 1
520a9643ea8Slogwangfrom 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
521a9643ea8Slogwang141.221.254.101.
522a9643ea8SlogwangAny incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
523a9643ea8Slogwang.Pp
524a9643ea8SlogwangAny calls to
525a9643ea8Slogwang.Fn LibAliasRedirectPort
526a9643ea8Slogwangwill have precedence over address mappings designated by
527a9643ea8Slogwang.Fn LibAliasRedirectAddr .
528a9643ea8Slogwang.Pp
529a9643ea8SlogwangThis function returns a pointer which can subsequently be used by
530a9643ea8Slogwang.Fn LibAliasRedirectDelete .
531a9643ea8SlogwangIf
532a9643ea8Slogwang.Dv NULL
533a9643ea8Slogwangis returned, then the function call did not complete successfully.
534a9643ea8Slogwang.Ed
535a9643ea8Slogwang.Pp
536a9643ea8Slogwang.Ft int
537a9643ea8Slogwang.Fo LibAliasAddServer
538a9643ea8Slogwang.Fa "struct libalias *"
539a9643ea8Slogwang.Fa "struct alias_link *link"
540a9643ea8Slogwang.Fa "struct in_addr addr"
541a9643ea8Slogwang.Fa "u_short port"
542a9643ea8Slogwang.Fc
543a9643ea8Slogwang.Bd -ragged -offset indent
544a9643ea8SlogwangThis function sets the
545a9643ea8Slogwang.Fa link
546a9643ea8Slogwangup for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
547a9643ea8SlogwangLSNAT operates as follows.
548a9643ea8SlogwangA client attempts to access a server by using the server virtual address.
549a9643ea8SlogwangThe LSNAT router transparently redirects the request to one of the hosts
550a9643ea8Slogwangin the server pool, using a real-time load sharing algorithm.
551a9643ea8SlogwangMultiple sessions may be initiated from the same client, and each session
552a9643ea8Slogwangcould be directed to a different host based on the load balance across server
553a9643ea8Slogwangpool hosts when the sessions are initiated.
554a9643ea8SlogwangIf load sharing is desired for just a few specific services, the configuration
555a9643ea8Slogwangon LSNAT could be defined to restrict load sharing to just the services
556a9643ea8Slogwangdesired.
557a9643ea8Slogwang.Pp
558a9643ea8SlogwangCurrently, only the simplest selection algorithm is implemented, where a
559a9643ea8Slogwanghost is selected on a round-robin basis only, without regard to load on
560a9643ea8Slogwangthe host.
561a9643ea8Slogwang.Pp
562a9643ea8SlogwangFirst, the
563a9643ea8Slogwang.Fa link
564a9643ea8Slogwangis created by either
565a9643ea8Slogwang.Fn LibAliasRedirectPort
566a9643ea8Slogwangor
567a9643ea8Slogwang.Fn LibAliasRedirectAddr .
568a9643ea8SlogwangThen,
569a9643ea8Slogwang.Fn LibAliasAddServer
570a9643ea8Slogwangis called multiple times to add entries to the
571a9643ea8Slogwang.Fa link Ns 's
572a9643ea8Slogwangserver pool.
573a9643ea8Slogwang.Pp
574a9643ea8SlogwangFor links created with
575a9643ea8Slogwang.Fn LibAliasRedirectAddr ,
576a9643ea8Slogwangthe
577a9643ea8Slogwang.Fa port
578a9643ea8Slogwangargument is ignored and could have any value, e.g.\& htons(~0).
579a9643ea8Slogwang.Pp
580a9643ea8SlogwangThis function returns 0 on success, \-1 otherwise.
581a9643ea8Slogwang.Ed
582a9643ea8Slogwang.Pp
583a9643ea8Slogwang.Ft int
584a9643ea8Slogwang.Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link"
585a9643ea8Slogwang.Bd -ragged -offset indent
586a9643ea8SlogwangThis function marks the specified static redirect rule entered by
587a9643ea8Slogwang.Fn LibAliasRedirectPort
588a9643ea8Slogwangas dynamic.
589a9643ea8SlogwangThis can be used to e.g.\& dynamically redirect a single TCP connection,
590a9643ea8Slogwangafter which the rule is removed.
591a9643ea8SlogwangOnly fully specified links can be made dynamic.
592a9643ea8Slogwang(See the
593a9643ea8Slogwang.Sx STATIC AND DYNAMIC LINKS
594a9643ea8Slogwangand
595a9643ea8Slogwang.Sx PARTIALLY SPECIFIED ALIASING LINKS
596a9643ea8Slogwangsections below for a definition of static vs.\& dynamic,
597a9643ea8Slogwangand partially vs.\& fully specified links.)
598a9643ea8Slogwang.Pp
599a9643ea8SlogwangThis function returns 0 on success, \-1 otherwise.
600a9643ea8Slogwang.Ed
601a9643ea8Slogwang.Pp
602a9643ea8Slogwang.Ft void
603a9643ea8Slogwang.Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link"
604a9643ea8Slogwang.Bd -ragged -offset indent
605a9643ea8SlogwangThis function will delete a specific static redirect rule entered by
606a9643ea8Slogwang.Fn LibAliasRedirectPort
607a9643ea8Slogwangor
608a9643ea8Slogwang.Fn LibAliasRedirectAddr .
609a9643ea8SlogwangThe parameter
610a9643ea8Slogwang.Fa link
611a9643ea8Slogwangis the pointer returned by either of the redirection functions.
612a9643ea8SlogwangIf an invalid pointer is passed to
613a9643ea8Slogwang.Fn LibAliasRedirectDelete ,
614a9643ea8Slogwangthen a program crash or unpredictable operation could result, so
615a9643ea8Slogwangcare is needed when using this function.
616a9643ea8Slogwang.Ed
617a9643ea8Slogwang.Pp
618a9643ea8Slogwang.Ft int
619a9643ea8Slogwang.Fn LibAliasProxyRule "struct libalias *" "const char *cmd"
620a9643ea8Slogwang.Bd -ragged -offset indent
621a9643ea8SlogwangThe passed
622a9643ea8Slogwang.Fa cmd
623a9643ea8Slogwangstring consists of one or more pairs of words.
624a9643ea8SlogwangThe first word in each pair is a token and the second is the value that
625a9643ea8Slogwangshould be applied for that token.
626a9643ea8SlogwangTokens and their argument types are as follows:
627a9643ea8Slogwang.Bl -tag -width indent
628a9643ea8Slogwang.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
629a9643ea8SlogwangIn order to support transparent proxying, it is necessary to somehow
630a9643ea8Slogwangpass the original address and port information into the new destination
631a9643ea8Slogwangserver.
632a9643ea8SlogwangIf
633a9643ea8Slogwang.Cm encode_ip_hdr
634a9643ea8Slogwangis specified, the original destination address and port are passed
635a9643ea8Slogwangas an extra IP option.
636a9643ea8SlogwangIf
637a9643ea8Slogwang.Cm encode_tcp_stream
638a9643ea8Slogwangis specified, the original destination address and port are passed
639a9643ea8Slogwangas the first piece of data in the TCP stream in the format
640a9643ea8Slogwang.Dq Li DEST Ar IP port .
641a9643ea8Slogwang.It Cm port Ar portnum
642a9643ea8SlogwangOnly packets with the destination port
643a9643ea8Slogwang.Ar portnum
644a9643ea8Slogwangare proxied.
645a9643ea8Slogwang.It Cm server Ar host Ns Op : Ns Ar portnum
646a9643ea8SlogwangThis specifies the
647a9643ea8Slogwang.Ar host
648a9643ea8Slogwangand
649a9643ea8Slogwang.Ar portnum
650a9643ea8Slogwangthat the data is to be redirected to.
651a9643ea8Slogwang.Ar host
652a9643ea8Slogwangmust be an IP address rather than a DNS host name.
653a9643ea8SlogwangIf
654a9643ea8Slogwang.Ar portnum
655a9643ea8Slogwangis not specified, the destination port number is not changed.
656a9643ea8Slogwang.Pp
657a9643ea8SlogwangThe
658a9643ea8Slogwang.Ar server
659a9643ea8Slogwangspecification is mandatory unless the
660a9643ea8Slogwang.Cm delete
661a9643ea8Slogwangcommand is being used.
662a9643ea8Slogwang.It Cm rule Ar index
663a9643ea8SlogwangNormally, each call to
664a9643ea8Slogwang.Fn LibAliasProxyRule
665a9643ea8Slogwanginserts the next rule at the start of a linear list of rules.
666a9643ea8SlogwangIf an
667a9643ea8Slogwang.Ar index
668a9643ea8Slogwangis specified, the new rule will be checked after all rules with lower
669a9643ea8Slogwangindices.
670a9643ea8SlogwangCalls to
671a9643ea8Slogwang.Fn LibAliasProxyRule
672a9643ea8Slogwangthat do not specify a rule are assigned rule 0.
673a9643ea8Slogwang.It Cm delete Ar index
674a9643ea8SlogwangThis token and its argument MUST NOT be used with any other tokens.
675a9643ea8SlogwangWhen used, all existing rules with the given
676a9643ea8Slogwang.Ar index
677a9643ea8Slogwangare deleted.
678a9643ea8Slogwang.It Cm proto tcp | udp
679a9643ea8SlogwangIf specified, only packets of the given protocol type are matched.
680a9643ea8Slogwang.It Cm src Ar IP Ns Op / Ns Ar bits
681a9643ea8SlogwangIf specified, only packets with a source address matching the given
682a9643ea8Slogwang.Ar IP
683a9643ea8Slogwangare matched.
684a9643ea8SlogwangIf
685a9643ea8Slogwang.Ar bits
686a9643ea8Slogwangis also specified, then the first
687a9643ea8Slogwang.Ar bits
688a9643ea8Slogwangbits of
689a9643ea8Slogwang.Ar IP
690a9643ea8Slogwangare taken as a network specification, and all IP addresses from that
691a9643ea8Slogwangnetwork will be matched.
692a9643ea8Slogwang.It Cm dst Ar IP Ns Op / Ns Ar bits
693a9643ea8SlogwangIf specified, only packets with a destination address matching the given
694a9643ea8Slogwang.Ar IP
695a9643ea8Slogwangare matched.
696a9643ea8SlogwangIf
697a9643ea8Slogwang.Ar bits
698a9643ea8Slogwangis also specified, then the first
699a9643ea8Slogwang.Ar bits
700a9643ea8Slogwangbits of
701a9643ea8Slogwang.Ar IP
702a9643ea8Slogwangare taken as a network specification, and all IP addresses from that
703a9643ea8Slogwangnetwork will be matched.
704a9643ea8Slogwang.El
705a9643ea8Slogwang.Pp
706a9643ea8SlogwangThis function is usually used to redirect outgoing connections for
707a9643ea8Slogwanginternal machines that are not permitted certain types of internet
708a9643ea8Slogwangaccess, or to restrict access to certain external machines.
709a9643ea8Slogwang.Ed
710a9643ea8Slogwang.Pp
711a9643ea8Slogwang.Ft struct alias_link *
712a9643ea8Slogwang.Fo LibAliasRedirectProto
713a9643ea8Slogwang.Fa "struct libalias *"
714a9643ea8Slogwang.Fa "struct in_addr local_addr"
715a9643ea8Slogwang.Fa "struct in_addr remote_addr"
716a9643ea8Slogwang.Fa "struct in_addr alias_addr"
717a9643ea8Slogwang.Fa "u_char proto"
718a9643ea8Slogwang.Fc
719a9643ea8Slogwang.Bd -ragged -offset indent
720a9643ea8SlogwangThis function specifies that any IP packet with protocol number of
721a9643ea8Slogwang.Fa proto
722a9643ea8Slogwangfrom a given remote address to an alias address will be
723a9643ea8Slogwangredirected to a specified local address.
724a9643ea8Slogwang.Pp
725a9643ea8SlogwangIf
726a9643ea8Slogwang.Fa local_addr
727a9643ea8Slogwangor
728a9643ea8Slogwang.Fa alias_addr
729a9643ea8Slogwangis zero, this indicates that the packet aliasing address as established
730a9643ea8Slogwangby
731a9643ea8Slogwang.Fn LibAliasSetAddress
732a9643ea8Slogwangis to be used.
733a9643ea8SlogwangEven if
734a9643ea8Slogwang.Fn LibAliasSetAddress
735a9643ea8Slogwangis called to change the address after
736a9643ea8Slogwang.Fn LibAliasRedirectProto
737a9643ea8Slogwangis called, a zero reference will track this change.
738a9643ea8Slogwang.Pp
739a9643ea8SlogwangIf
740a9643ea8Slogwang.Fa remote_addr
741a9643ea8Slogwangis zero, this indicates to redirect packets from any remote address.
742a9643ea8SlogwangNon-zero remote addresses can sometimes be useful for firewalling.
743a9643ea8Slogwang.Pp
744a9643ea8SlogwangIf two calls to
745a9643ea8Slogwang.Fn LibAliasRedirectProto
746a9643ea8Slogwangoverlap in their address specifications, then the most recent call
747a9643ea8Slogwangwill have precedence.
748a9643ea8Slogwang.Pp
749a9643ea8SlogwangThis function returns a pointer which can subsequently be used by
750a9643ea8Slogwang.Fn LibAliasRedirectDelete .
751a9643ea8SlogwangIf
752a9643ea8Slogwang.Dv NULL
753a9643ea8Slogwangis returned, then the function call did not complete successfully.
754a9643ea8Slogwang.Ed
755a9643ea8Slogwang.Sh FRAGMENT HANDLING
756a9643ea8SlogwangThe functions in this section are used to deal with incoming fragments.
757a9643ea8Slogwang.Pp
758a9643ea8SlogwangOutgoing fragments are handled within
759a9643ea8Slogwang.Fn LibAliasOut
760a9643ea8Slogwangby changing the address according to any applicable mapping set by
761a9643ea8Slogwang.Fn LibAliasRedirectAddr ,
762a9643ea8Slogwangor the default aliasing address set by
763a9643ea8Slogwang.Fn LibAliasSetAddress .
764a9643ea8Slogwang.Pp
765a9643ea8SlogwangIncoming fragments are handled in one of two ways.
766a9643ea8SlogwangIf the header of a fragmented IP packet has already been seen, then all
767a9643ea8Slogwangsubsequent fragments will be re-mapped in the same manner the header
768a9643ea8Slogwangfragment was.
769a9643ea8SlogwangFragments which arrive before the header are saved and then retrieved
770a9643ea8Slogwangonce the header fragment has been resolved.
771a9643ea8Slogwang.Pp
772a9643ea8Slogwang.Ft int
773*22ce4affSfengbojiang.Fn LibAliasSaveFragment "struct libalias *" "void *ptr"
774a9643ea8Slogwang.Bd -ragged -offset indent
775a9643ea8SlogwangWhen
776a9643ea8Slogwang.Fn LibAliasIn
777a9643ea8Slogwangreturns
778a9643ea8Slogwang.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
779a9643ea8Slogwangthis function can be used to save the pointer to the unresolved fragment.
780a9643ea8Slogwang.Pp
781a9643ea8SlogwangIt is implicitly assumed that
782a9643ea8Slogwang.Fa ptr
783a9643ea8Slogwangpoints to a block of memory allocated by
784a9643ea8Slogwang.Xr malloc 3 .
785a9643ea8SlogwangIf the fragment is never resolved, the packet aliasing engine will
786a9643ea8Slogwangautomatically free the memory after a timeout period.
787a9643ea8Slogwang[Eventually this function should be modified so that a callback function
788a9643ea8Slogwangfor freeing memory is passed as an argument.]
789a9643ea8Slogwang.Pp
790a9643ea8SlogwangThis function returns
791a9643ea8Slogwang.Dv PKT_ALIAS_OK
792a9643ea8Slogwangif it was successful and
793a9643ea8Slogwang.Dv PKT_ALIAS_ERROR
794a9643ea8Slogwangif there was an error.
795a9643ea8Slogwang.Ed
796a9643ea8Slogwang.Pp
797*22ce4affSfengbojiang.Ft void *
798*22ce4affSfengbojiang.Fn LibAliasGetFragment "struct libalias *" "void *buffer"
799a9643ea8Slogwang.Bd -ragged -offset indent
800a9643ea8SlogwangThis function can be used to retrieve fragment pointers saved by
801a9643ea8Slogwang.Fn LibAliasSaveFragment .
802a9643ea8SlogwangThe IP header fragment pointed to by
803a9643ea8Slogwang.Fa buffer
804a9643ea8Slogwangis the header fragment indicated when
805a9643ea8Slogwang.Fn LibAliasIn
806a9643ea8Slogwangreturns
807a9643ea8Slogwang.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
808a9643ea8SlogwangOnce a fragment pointer is retrieved, it becomes the calling program's
809a9643ea8Slogwangresponsibility to free the dynamically allocated memory for the fragment.
810a9643ea8Slogwang.Pp
811a9643ea8SlogwangThe
812a9643ea8Slogwang.Fn LibAliasGetFragment
813a9643ea8Slogwangfunction can be called sequentially until there are no more fragments
814a9643ea8Slogwangavailable, at which time it returns
815a9643ea8Slogwang.Dv NULL .
816a9643ea8Slogwang.Ed
817a9643ea8Slogwang.Pp
818a9643ea8Slogwang.Ft void
819*22ce4affSfengbojiang.Fn LibAliasFragmentIn "struct libalias *" "void *header" "void *fragment"
820a9643ea8Slogwang.Bd -ragged -offset indent
821a9643ea8SlogwangWhen a fragment is retrieved with
822a9643ea8Slogwang.Fn LibAliasGetFragment ,
823a9643ea8Slogwangit can then be de-aliased with a call to
824a9643ea8Slogwang.Fn LibAliasFragmentIn .
825a9643ea8SlogwangThe
826a9643ea8Slogwang.Fa header
827a9643ea8Slogwangargument is the pointer to a header fragment used as a template, and
828a9643ea8Slogwang.Fa fragment
829a9643ea8Slogwangis the pointer to the packet to be de-aliased.
830a9643ea8Slogwang.Ed
831a9643ea8Slogwang.Sh MISCELLANEOUS FUNCTIONS
832a9643ea8Slogwang.Ft struct alias_link *
833a9643ea8Slogwang.Fn AddLink "struct libalias *" "struct in_addr src_addr" "struct in_addr dst_addr" \
834a9643ea8Slogwang"struct in_addr alias_addr" "u_short src_port" "u_short dst_port" \
835a9643ea8Slogwang"int alias_param" "int link_type"
836a9643ea8Slogwang.Bd -ragged -offset indent
837a9643ea8SlogwangThis function adds new state to the instance hash table.
838a9643ea8SlogwangThe dst_address and/or dst_port may be given as zero, which
839a9643ea8Slogwangintroduces some dynamic character into the link, since
840a9643ea8SlogwangLibAliasSetAddress can change the address that is used.
841a9643ea8SlogwangHowever, in the current implementation, such links can only be used
842a9643ea8Slogwangfor inbound (ext -> int) traffic.
843a9643ea8Slogwang.Ed
844a9643ea8Slogwang.Pp
845a9643ea8Slogwang.Ft void
846a9643ea8Slogwang.Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr"
847a9643ea8Slogwang.Bd -ragged -offset indent
848a9643ea8SlogwangWhen an incoming packet not associated with any pre-existing aliasing link
849a9643ea8Slogwangarrives at the host machine, it will be sent to the address indicated by a
850a9643ea8Slogwangcall to
851a9643ea8Slogwang.Fn LibAliasSetTarget .
852a9643ea8Slogwang.Pp
853a9643ea8SlogwangIf this function is called with an
854a9643ea8Slogwang.Dv INADDR_NONE
855a9643ea8Slogwangaddress argument, then all new incoming packets go to the address set by
856a9643ea8Slogwang.Fn LibAliasSetAddress .
857a9643ea8Slogwang.Pp
858a9643ea8SlogwangIf this function is not called, or is called with an
859a9643ea8Slogwang.Dv INADDR_ANY
860a9643ea8Slogwangaddress argument, then all new incoming packets go to the address specified
861a9643ea8Slogwangin the packet.
862a9643ea8SlogwangThis allows external machines to talk directly to internal machines if they
863a9643ea8Slogwangcan route packets to the machine in question.
864a9643ea8Slogwang.Ed
865a9643ea8Slogwang.Pp
866a9643ea8Slogwang.Ft int
867a9643ea8Slogwang.Fn LibAliasCheckNewLink "struct libalias *"
868a9643ea8Slogwang.Bd -ragged -offset indent
869a9643ea8SlogwangThis function returns a non-zero value when a new aliasing link is created.
870a9643ea8SlogwangIn circumstances where incoming traffic is being sequentially sent to
871a9643ea8Slogwangdifferent local servers, this function can be used to trigger when
872a9643ea8Slogwang.Fn LibAliasSetTarget
873a9643ea8Slogwangis called to change the default target address.
874a9643ea8Slogwang.Ed
875a9643ea8Slogwang.Pp
876a9643ea8Slogwang.Ft u_short
877a9643ea8Slogwang.Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes"
878a9643ea8Slogwang.Bd -ragged -offset indent
879a9643ea8SlogwangThis is a utility function that does not seem to be available elsewhere and
880a9643ea8Slogwangis included as a convenience.
881a9643ea8SlogwangIt computes the internet checksum, which is used in both IP and
882a9643ea8Slogwangprotocol-specific headers (TCP, UDP, ICMP).
883a9643ea8Slogwang.Pp
884a9643ea8SlogwangThe
885a9643ea8Slogwang.Fa buffer
886a9643ea8Slogwangargument points to the data block to be checksummed, and
887a9643ea8Slogwang.Fa nbytes
888a9643ea8Slogwangis the number of bytes.
889a9643ea8SlogwangThe 16-bit checksum field should be zeroed before computing the checksum.
890a9643ea8Slogwang.Pp
891a9643ea8SlogwangChecksums can also be verified by operating on a block of data including
892a9643ea8Slogwangits checksum.
893a9643ea8SlogwangIf the checksum is valid,
894a9643ea8Slogwang.Fn LibAliasInternetChecksum
895a9643ea8Slogwangwill return zero.
896a9643ea8Slogwang.Ed
897a9643ea8Slogwang.Pp
898a9643ea8Slogwang.Ft int
899*22ce4affSfengbojiang.Fn LibAliasUnaliasOut "struct libalias *" "void *buffer" "int maxpacketsize"
900a9643ea8Slogwang.Bd -ragged -offset indent
901a9643ea8SlogwangAn outgoing packet, which has already been aliased,
902a9643ea8Slogwanghas its private address/port information restored by this function.
903a9643ea8SlogwangThe IP packet is pointed to by
904a9643ea8Slogwang.Fa buffer ,
905a9643ea8Slogwangand
906a9643ea8Slogwang.Fa maxpacketsize
907a9643ea8Slogwangis provided for error checking purposes.
908a9643ea8SlogwangThis function can be used if an already-aliased packet needs to have its
909a9643ea8Slogwangoriginal IP header restored for further processing (e.g.\& logging).
910a9643ea8Slogwang.Ed
911a9643ea8Slogwang.Sh CONCEPTUAL BACKGROUND
912a9643ea8SlogwangThis section is intended for those who are planning to modify the source
913a9643ea8Slogwangcode or want to create somewhat esoteric applications using the packet
914a9643ea8Slogwangaliasing functions.
915a9643ea8Slogwang.Pp
916a9643ea8SlogwangThe conceptual framework under which the packet aliasing engine operates
917a9643ea8Slogwangis described here.
918a9643ea8SlogwangCentral to the discussion is the idea of an
919a9643ea8Slogwang.Em aliasing link
920a9643ea8Slogwangwhich describes the relationship for a given packet transaction between
921a9643ea8Slogwangthe local machine, aliased identity and remote machine.
922a9643ea8SlogwangIt is discussed how such links come into existence and are destroyed.
923a9643ea8Slogwang.Ss ALIASING LINKS
924a9643ea8SlogwangThere is a notion of an
925a9643ea8Slogwang.Em aliasing link ,
926a9643ea8Slogwangwhich is a 7-tuple describing a specific translation:
927a9643ea8Slogwang.Bd -literal -offset indent
928a9643ea8Slogwang(local addr, local port, alias addr, alias port,
929a9643ea8Slogwang remote addr, remote port, protocol)
930a9643ea8Slogwang.Ed
931a9643ea8Slogwang.Pp
932a9643ea8SlogwangOutgoing packets have the local address and port number replaced with the
933a9643ea8Slogwangalias address and port number.
934a9643ea8SlogwangIncoming packets undergo the reverse process.
935a9643ea8SlogwangThe packet aliasing engine attempts to match packets against an internal
936a9643ea8Slogwangtable of aliasing links to determine how to modify a given IP packet.
937a9643ea8SlogwangBoth the IP header and protocol dependent headers are modified as necessary.
938a9643ea8SlogwangAliasing links are created and deleted as necessary according to network
939a9643ea8Slogwangtraffic.
940a9643ea8Slogwang.Pp
941a9643ea8SlogwangProtocols can be TCP, UDP or even ICMP in certain circumstances.
942a9643ea8Slogwang(Some types of ICMP packets can be aliased according to sequence or ID
943a9643ea8Slogwangnumber which acts as an equivalent port number for identifying how
944a9643ea8Slogwangindividual packets should be handled.)
945a9643ea8Slogwang.Pp
946a9643ea8SlogwangEach aliasing link must have a unique combination of the following five
947a9643ea8Slogwangquantities: alias address/port, remote address/port and protocol.
948a9643ea8SlogwangThis ensures that several machines on a local network can share the
949a9643ea8Slogwangsame aliasing IP address.
950a9643ea8SlogwangIn cases where conflicts might arise, the aliasing port is chosen so that
951a9643ea8Slogwanguniqueness is maintained.
952a9643ea8Slogwang.Ss STATIC AND DYNAMIC LINKS
953a9643ea8SlogwangAliasing links can either be static or dynamic.
954a9643ea8SlogwangStatic links persist indefinitely and represent fixed rules for translating
955a9643ea8SlogwangIP packets.
956a9643ea8SlogwangDynamic links come into existence for a specific TCP connection or UDP
957a9643ea8Slogwangtransaction or ICMP ECHO sequence.
958a9643ea8SlogwangFor the case of TCP, the connection can be monitored to see when the
959a9643ea8Slogwangassociated aliasing link should be deleted.
960a9643ea8SlogwangAliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
961a9643ea8Slogwangwork on a simple timeout rule.
962a9643ea8SlogwangWhen no activity is observed on a dynamic link for a certain amount of time
963a9643ea8Slogwangit is automatically deleted.
964a9643ea8SlogwangTimeout rules also apply to TCP connections which do not open or close
965a9643ea8Slogwangproperly.
966a9643ea8Slogwang.Ss PARTIALLY SPECIFIED ALIASING LINKS
967a9643ea8SlogwangAliasing links can be partially specified, meaning that the remote address
968a9643ea8Slogwangand/or remote port are unknown.
969a9643ea8SlogwangIn this case, when a packet matching the incomplete specification is found,
970a9643ea8Slogwanga fully specified dynamic link is created.
971a9643ea8SlogwangIf the original partially specified link is dynamic, it will be deleted
972a9643ea8Slogwangafter the fully specified link is created, otherwise it will persist.
973a9643ea8Slogwang.Pp
974a9643ea8SlogwangFor instance, a partially specified link might be
975a9643ea8Slogwang.Bd -literal -offset indent
976a9643ea8Slogwang(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
977a9643ea8Slogwang.Ed
978a9643ea8Slogwang.Pp
979a9643ea8SlogwangThe zeros denote unspecified components for the remote address and port.
980a9643ea8SlogwangIf this link were static it would have the effect of redirecting all
981a9643ea8Slogwangincoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
982a9643ea8Slogwangof machine 192.168.0.4 on the local network.
983a9643ea8SlogwangEach individual telnet connection would initiate the creation of a distinct
984a9643ea8Slogwangdynamic link.
985a9643ea8Slogwang.Ss DYNAMIC LINK CREATION
986a9643ea8SlogwangIn addition to aliasing links, there are also address mappings that can be
987a9643ea8Slogwangstored within the internal data table of the packet aliasing mechanism.
988a9643ea8Slogwang.Bd -literal -offset indent
989a9643ea8Slogwang(local addr, alias addr)
990a9643ea8Slogwang.Ed
991a9643ea8Slogwang.Pp
992a9643ea8SlogwangAddress mappings are searched when creating new dynamic links.
993a9643ea8Slogwang.Pp
994a9643ea8SlogwangAll outgoing packets from the local network automatically create a dynamic
995a9643ea8Slogwanglink if they do not match an already existing fully specified link.
996a9643ea8SlogwangIf an address mapping exists for the outgoing packet, this determines
997a9643ea8Slogwangthe alias address to be used.
998a9643ea8SlogwangIf no mapping exists, then a default address, usually the address of the
999a9643ea8Slogwangpacket aliasing host, is used.
1000a9643ea8SlogwangIf necessary, this default address can be changed as often as each individual
1001a9643ea8Slogwangpacket arrives.
1002a9643ea8Slogwang.Pp
1003a9643ea8SlogwangThe aliasing port number is determined such that the new dynamic link does
1004a9643ea8Slogwangnot conflict with any existing links.
1005a9643ea8SlogwangIn the default operating mode, the packet aliasing engine attempts to set
1006a9643ea8Slogwangthe aliasing port equal to the local port number.
1007a9643ea8SlogwangIf this results in a conflict, then port numbers are randomly chosen until
1008a9643ea8Slogwanga unique aliasing link can be established.
1009a9643ea8SlogwangIn an alternate operating mode, the first choice of an aliasing port is also
1010a9643ea8Slogwangrandom and unrelated to the local port number.
1011a9643ea8Slogwang.Sh MODULAR ARCHITECTURE Po AND Xr ipfw 4 SUPPORT Pc
1012a9643ea8SlogwangOne of the latest improvements to
1013a9643ea8Slogwang.Nm
1014a9643ea8Slogwangwas to make its support
1015a9643ea8Slogwangfor new protocols independent from the rest of the library, giving it
1016a9643ea8Slogwangthe ability to load/unload support for new protocols at run-time.
1017a9643ea8SlogwangTo achieve this feature, all the code for protocol handling was moved
1018a9643ea8Slogwangto a series of modules outside of the main library.
1019a9643ea8SlogwangThese modules are compiled from the same sources but work in
1020a9643ea8Slogwangdifferent ways, depending on whether they are compiled to work inside a kernel
1021a9643ea8Slogwangor as part of the userland library.
1022a9643ea8Slogwang.Ss LIBALIAS MODULES IN KERNEL LAND
1023a9643ea8SlogwangWhen compiled for the kernel,
1024a9643ea8Slogwang.Nm
1025a9643ea8Slogwangmodules are plain KLDs recognizable with the
1026a9643ea8Slogwang.Pa alias_
1027a9643ea8Slogwangprefix.
1028a9643ea8Slogwang.Pp
1029a9643ea8SlogwangTo add support for a new protocol, load the corresponding module.
1030a9643ea8SlogwangFor example:
1031a9643ea8Slogwang.Pp
1032a9643ea8Slogwang.Dl "kldload alias_ftp"
1033a9643ea8Slogwang.Pp
1034a9643ea8SlogwangWhen support for a protocol is no longer needed, its module can be unloaded:
1035a9643ea8Slogwang.Pp
1036a9643ea8Slogwang.Dl "kldunload alias_ftp"
1037a9643ea8Slogwang.Ss LIBALIAS MODULES IN USERLAND
1038a9643ea8SlogwangDue to the differences between kernel and userland (no KLD mechanism,
1039a9643ea8Slogwangmany different address spaces, etc.), we had to change a bit how to
1040a9643ea8Slogwanghandle module loading/tracking/unloading in userland.
1041a9643ea8Slogwang.Pp
1042a9643ea8SlogwangWhile compiled for a userland
1043a9643ea8Slogwang.Nm ,
1044a9643ea8Slogwangall the modules are plain libraries, residing in
1045a9643ea8Slogwang.Pa /usr/lib ,
1046a9643ea8Slogwangand recognizable with the
1047a9643ea8Slogwang.Pa libalias_
1048a9643ea8Slogwangprefix.
1049a9643ea8Slogwang.Pp
1050a9643ea8SlogwangThere is a configuration file,
1051a9643ea8Slogwang.Pa /etc/libalias.conf ,
1052a9643ea8Slogwangwith the following contents (by default):
1053a9643ea8Slogwang.Bd -literal -offset indent
1054a9643ea8Slogwang/usr/lib/libalias_ftp.so
1055a9643ea8Slogwang/usr/lib/libalias_irc.so
1056a9643ea8Slogwang/usr/lib/libalias_nbt.so
1057a9643ea8Slogwang/usr/lib/libalias_pptp.so
1058a9643ea8Slogwang/usr/lib/libalias_skinny.so
1059a9643ea8Slogwang/usr/lib/libalias_smedia.so
1060a9643ea8Slogwang.Ed
1061a9643ea8Slogwang.Pp
1062a9643ea8SlogwangThis file contains the paths to the modules that
1063a9643ea8Slogwang.Nm
1064a9643ea8Slogwangwill load.
1065a9643ea8SlogwangTo load/unload a new module, just add its path to
1066a9643ea8Slogwang.Pa libalias.conf
1067a9643ea8Slogwangand call
1068a9643ea8Slogwang.Fn LibAliasRefreshModules
1069a9643ea8Slogwangfrom the program.
1070a9643ea8SlogwangIn case the application provides a
1071a9643ea8Slogwang.Dv SIGHUP
1072a9643ea8Slogwangsignal handler, add a call to
1073a9643ea8Slogwang.Fn LibAliasRefreshModules
1074a9643ea8Slogwanginside the handler, and every time you want to refresh the loaded modules,
1075a9643ea8Slogwangsend it the
1076a9643ea8Slogwang.Dv SIGHUP
1077a9643ea8Slogwangsignal:
1078a9643ea8Slogwang.Pp
1079a9643ea8Slogwang.Dl "kill -HUP <process_pid>"
1080a9643ea8Slogwang.Ss MODULAR ARCHITECURE: HOW IT WORKS
1081a9643ea8SlogwangThe modular architecture of
1082a9643ea8Slogwang.Nm
1083a9643ea8Slogwangworks similar whether it is running inside the
1084a9643ea8Slogwangkernel or in userland.
1085a9643ea8SlogwangFrom
1086a9643ea8Slogwang.Pa alias_mod.c :
1087a9643ea8Slogwang.Bd -literal
1088a9643ea8Slogwang/* Protocol and userland module handlers chains. */
1089a9643ea8SlogwangLIST_HEAD(handler_chain, proto_handler) handler_chain ...
1090a9643ea8Slogwang\&...
1091a9643ea8SlogwangSLIST_HEAD(dll_chain, dll) dll_chain ...
1092a9643ea8Slogwang.Ed
1093a9643ea8Slogwang.Pp
1094a9643ea8Slogwang.Va handler_chain
1095a9643ea8Slogwangkeeps track of all the protocol handlers loaded, while
1096a9643ea8Slogwang.Va ddl_chain
1097a9643ea8Slogwangtracks which userland modules are loaded.
1098a9643ea8Slogwang.Pp
1099a9643ea8Slogwang.Va handler_chain
1100a9643ea8Slogwangis composed of
1101a9643ea8Slogwang.Vt "struct proto_handler"
1102a9643ea8Slogwangentries:
1103a9643ea8Slogwang.Bd -literal
1104a9643ea8Slogwangstruct proto_handler {
1105a9643ea8Slogwang	u_int pri;
1106a9643ea8Slogwang	int16_t dir;
1107a9643ea8Slogwang	uint8_t proto;
1108a9643ea8Slogwang	int (*fingerprint)(struct libalias *la,
1109a9643ea8Slogwang		 struct ip *pip, struct alias_data *ah);
1110a9643ea8Slogwang	int (*protohandler)(struct libalias *la,
1111a9643ea8Slogwang		 struct ip *pip, struct alias_data *ah);
1112a9643ea8Slogwang	TAILQ_ENTRY(proto_handler) link;
1113a9643ea8Slogwang};
1114a9643ea8Slogwang.Ed
1115a9643ea8Slogwang.Pp
1116a9643ea8Slogwangwhere:
1117a9643ea8Slogwang.Bl -inset
1118a9643ea8Slogwang.It Va pri
1119a9643ea8Slogwangis the priority assigned to a protocol handler; lower priority
1120a9643ea8Slogwangis better.
1121a9643ea8Slogwang.It Va dir
1122a9643ea8Slogwangis the direction of packets: ingoing or outgoing.
1123a9643ea8Slogwang.It Va proto
1124a9643ea8Slogwangindicates to which protocol this packet belongs: IP, TCP or UDP.
1125a9643ea8Slogwang.It Va fingerprint
1126a9643ea8Slogwangpoints to the fingerprint function while protohandler points
1127a9643ea8Slogwangto the protocol handler function.
1128a9643ea8Slogwang.El
1129a9643ea8Slogwang.Pp
1130a9643ea8SlogwangThe
1131a9643ea8Slogwang.Va fingerprint
1132a9643ea8Slogwangfunction has the dual role of checking if the
1133a9643ea8Slogwangincoming packet is found, and if it belongs to any categories that this
1134a9643ea8Slogwangmodule can handle.
1135a9643ea8Slogwang.Pp
1136a9643ea8SlogwangThe
1137a9643ea8Slogwang.Va protohandler
1138a9643ea8Slogwangfunction actually manipulates
1139a9643ea8Slogwangthe packet to make
1140a9643ea8Slogwang.Nm
1141a9643ea8Slogwangcorrectly NAT it.
1142a9643ea8Slogwang.Pp
1143a9643ea8SlogwangWhen a packet enters
1144a9643ea8Slogwang.Nm ,
1145a9643ea8Slogwangif it meets a module hook,
1146a9643ea8Slogwang.Va handler_chain
1147a9643ea8Slogwangis searched to see if there is an handler that matches
1148a9643ea8Slogwangthis type of a packet (it checks protocol and direction of packet).
1149a9643ea8SlogwangThen, if more than one handler is found, it starts with the module with
1150a9643ea8Slogwangthe lowest priority number: it calls the
1151a9643ea8Slogwang.Va fingerprint
1152a9643ea8Slogwangfunction and interprets the result.
1153a9643ea8Slogwang.Pp
1154a9643ea8SlogwangIf the result value is equal to 0 then it calls the protocol handler
1155a9643ea8Slogwangof this handler and returns.
1156a9643ea8SlogwangOtherwise, it proceeds to the next eligible module until the
1157a9643ea8Slogwang.Va handler_chain
1158a9643ea8Slogwangis exhausted.
1159a9643ea8Slogwang.Pp
1160a9643ea8SlogwangInside
1161a9643ea8Slogwang.Nm ,
1162a9643ea8Slogwangthe module hook looks like this:
1163a9643ea8Slogwang.Bd -literal -offset indent
1164a9643ea8Slogwangstruct alias_data ad = {
1165a9643ea8Slogwang	lnk,
1166a9643ea8Slogwang	&original_address,
1167a9643ea8Slogwang	&alias_address,
1168a9643ea8Slogwang	&alias_port,
1169a9643ea8Slogwang	&ud->uh_sport,          /* original source port */
1170a9643ea8Slogwang	&ud->uh_dport,		/* original dest port */
1171a9643ea8Slogwang	256                     /* maxpacketsize */
1172a9643ea8Slogwang};
1173a9643ea8Slogwang
1174a9643ea8Slogwang\&...
1175a9643ea8Slogwang
1176a9643ea8Slogwang/* walk out chain */
1177a9643ea8Slogwangerr = find_handler(IN, UDP, la, pip, &ad);
1178a9643ea8Slogwang.Ed
1179a9643ea8Slogwang.Pp
1180a9643ea8SlogwangAll data useful to a module are gathered together in an
1181a9643ea8Slogwang.Vt alias_data
1182a9643ea8Slogwangstructure, then
1183a9643ea8Slogwang.Fn find_handler
1184a9643ea8Slogwangis called.
1185a9643ea8SlogwangThe
1186a9643ea8Slogwang.Fn find_handler
1187a9643ea8Slogwangfunction is responsible for walking the handler
1188a9643ea8Slogwangchain; it receives as input parameters:
1189a9643ea8Slogwang.Bl -tag -width indent
1190a9643ea8Slogwang.It Fa IN
1191a9643ea8Slogwangdirection
1192a9643ea8Slogwang.It Fa UDP
1193a9643ea8Slogwangworking protocol
1194a9643ea8Slogwang.It Fa la
1195a9643ea8Slogwangpointer to this instance of libalias
1196a9643ea8Slogwang.It Fa pip
1197a9643ea8Slogwangpointer to a
1198a9643ea8Slogwang.Vt "struct ip"
1199a9643ea8Slogwang.It Fa ad
1200a9643ea8Slogwangpointer to
1201a9643ea8Slogwang.Vt "struct alias_data"
1202a9643ea8Slogwang(see above)
1203a9643ea8Slogwang.El
1204a9643ea8Slogwang.Pp
1205a9643ea8SlogwangIn this case,
1206a9643ea8Slogwang.Fn find_handler
1207a9643ea8Slogwangwill search only for modules registered for
1208a9643ea8Slogwangsupporting INcoming UDP packets.
1209a9643ea8Slogwang.Pp
1210a9643ea8SlogwangAs was mentioned earlier,
1211a9643ea8Slogwang.Nm
1212a9643ea8Slogwangin userland is a bit different, as
1213a9643ea8Slogwangcare must be taken in module handling as well (avoiding duplicate load of
1214a9643ea8Slogwangmodules, avoiding modules with same name, etc.) so
1215a9643ea8Slogwang.Va dll_chain
1216a9643ea8Slogwangwas introduced.
1217a9643ea8Slogwang.Pp
1218a9643ea8Slogwang.Va dll_chain
1219a9643ea8Slogwangcontains a list of all userland
1220a9643ea8Slogwang.Nm
1221a9643ea8Slogwangmodules loaded.
1222a9643ea8Slogwang.Pp
1223a9643ea8SlogwangWhen an application calls
1224a9643ea8Slogwang.Fn LibAliasRefreshModules ,
1225a9643ea8Slogwang.Nm
1226a9643ea8Slogwangfirst unloads all the loaded modules, then reloads all the modules listed in
1227a9643ea8Slogwang.Pa /etc/libalias.conf :
1228a9643ea8Slogwangfor every module loaded, a new entry is added to
1229a9643ea8Slogwang.Va dll_chain .
1230a9643ea8Slogwang.Pp
1231a9643ea8Slogwang.Va dll_chain
1232a9643ea8Slogwangis composed of
1233a9643ea8Slogwang.Vt "struct dll"
1234a9643ea8Slogwangentries:
1235a9643ea8Slogwang.Bd -literal
1236a9643ea8Slogwangstruct dll {
1237a9643ea8Slogwang	/* name of module */
1238a9643ea8Slogwang	char            name[DLL_LEN];
1239a9643ea8Slogwang	/*
1240a9643ea8Slogwang	 * ptr to shared obj obtained through
1241a9643ea8Slogwang	 * dlopen() - use this ptr to get access
1242a9643ea8Slogwang	 * to any symbols from a loaded module
1243a9643ea8Slogwang	 * via dlsym()
1244a9643ea8Slogwang	 */
1245a9643ea8Slogwang	void            *handle;
1246a9643ea8Slogwang	struct dll      *next;
1247a9643ea8Slogwang};
1248a9643ea8Slogwang.Ed
1249a9643ea8Slogwang.Bl -inset
1250a9643ea8Slogwang.It Va name
1251a9643ea8Slogwangis the name of the module.
1252a9643ea8Slogwang.It Va handle
1253a9643ea8Slogwangis a pointer to the module obtained through
1254a9643ea8Slogwang.Xr dlopen 3 .
1255a9643ea8Slogwang.El
1256a9643ea8SlogwangWhenever a module is loaded in userland, an entry is added to
1257a9643ea8Slogwang.Va dll_chain ,
1258a9643ea8Slogwangthen every protocol handler present in that module
1259a9643ea8Slogwangis resolved and registered in
1260a9643ea8Slogwang.Va handler_chain .
1261a9643ea8Slogwang.Ss HOW TO WRITE A MODULE FOR LIBALIAS
1262a9643ea8SlogwangThere is a module (called
1263a9643ea8Slogwang.Pa alias_dummy.[ch] )
1264a9643ea8Slogwangin
1265a9643ea8Slogwang.Nm
1266a9643ea8Slogwangthat can be used as a skeleton for future work.
1267a9643ea8SlogwangHere we analyse some parts of that module.
1268a9643ea8SlogwangFrom
1269a9643ea8Slogwang.Pa alias_dummy.c :
1270a9643ea8Slogwang.Bd -literal
1271a9643ea8Slogwangstruct proto_handler handlers[] = {
1272a9643ea8Slogwang    {
1273a9643ea8Slogwang	.pri = 666,
1274a9643ea8Slogwang	.dir = IN|OUT,
1275a9643ea8Slogwang	.proto = UDP|TCP,
1276a9643ea8Slogwang	.fingerprint = fingerprint,
1277a9643ea8Slogwang	.protohandler= protohandler,
1278a9643ea8Slogwang    },
1279a9643ea8Slogwang    { EOH }
1280a9643ea8Slogwang};
1281a9643ea8Slogwang.Ed
1282a9643ea8Slogwang.Pp
1283a9643ea8SlogwangThe variable
1284a9643ea8Slogwang.Va handlers
1285a9643ea8Slogwangis the
1286a9643ea8Slogwang.Dq "most important thing"
1287a9643ea8Slogwangin a module
1288a9643ea8Slogwangsince it describes the handlers present and lets the outside world use
1289a9643ea8Slogwangit in an opaque way.
1290a9643ea8Slogwang.Pp
1291a9643ea8SlogwangIt must ALWAYS be present in every module, and it MUST retain
1292a9643ea8Slogwangthe name
1293a9643ea8Slogwang.Va handlers ,
1294a9643ea8Slogwangotherwise attempting to load a module in userland will fail and
1295a9643ea8Slogwangcomplain about missing symbols: for more information about module
1296a9643ea8Slogwangload/unload, please refer to
1297a9643ea8Slogwang.Fn LibAliasRefreshModules ,
1298a9643ea8Slogwang.Fn LibAliasLoadModule
1299a9643ea8Slogwangand
1300a9643ea8Slogwang.Fn LibAliasUnloadModule
1301a9643ea8Slogwangin
1302a9643ea8Slogwang.Pa alias.c .
1303a9643ea8Slogwang.Pp
1304a9643ea8Slogwang.Va handlers
1305a9643ea8Slogwangcontains all the
1306a9643ea8Slogwang.Vt proto_handler
1307a9643ea8Slogwangstructures present in a module.
1308a9643ea8Slogwang.Bd -literal
1309a9643ea8Slogwangstatic int
1310a9643ea8Slogwangmod_handler(module_t mod, int type, void *data)
1311a9643ea8Slogwang{
1312a9643ea8Slogwang	int error;
1313a9643ea8Slogwang
1314a9643ea8Slogwang	switch (type) {
1315a9643ea8Slogwang	case MOD_LOAD:
1316a9643ea8Slogwang		error = LibAliasAttachHandlers(handlers);
1317a9643ea8Slogwang		break;
1318a9643ea8Slogwang	case MOD_UNLOAD:
1319a9643ea8Slogwang		error = LibAliasDetachHandlers(handlers);
1320a9643ea8Slogwang		break;
1321a9643ea8Slogwang	default:
1322a9643ea8Slogwang		error = EINVAL;
1323a9643ea8Slogwang	}
1324a9643ea8Slogwang	return (error);
1325a9643ea8Slogwang}
1326a9643ea8Slogwang.Ed
1327a9643ea8SlogwangWhen running as KLD,
1328a9643ea8Slogwang.Fn mod_handler
1329a9643ea8Slogwangregisters/deregisters the module using
1330a9643ea8Slogwang.Fn LibAliasAttachHandlers
1331a9643ea8Slogwangand
1332a9643ea8Slogwang.Fn LibAliasDetachHandlers ,
1333a9643ea8Slogwangrespectively.
1334a9643ea8Slogwang.Pp
1335a9643ea8SlogwangEvery module must contain at least 2 functions: one fingerprint
1336a9643ea8Slogwangfunction and a protocol handler function.
1337a9643ea8Slogwang.Bd -literal
1338a9643ea8Slogwang#ifdef _KERNEL
1339a9643ea8Slogwangstatic
1340a9643ea8Slogwang#endif
1341a9643ea8Slogwangint
1342a9643ea8Slogwangfingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
1343a9643ea8Slogwang{
1344a9643ea8Slogwang
1345a9643ea8Slogwang\&...
1346a9643ea8Slogwang}
1347a9643ea8Slogwang
1348a9643ea8Slogwang#ifdef _KERNEL
1349a9643ea8Slogwangstatic
1350a9643ea8Slogwang#endif
1351a9643ea8Slogwangint
1352a9643ea8Slogwangprotohandler(struct libalias *la, struct ip *pip,
1353a9643ea8Slogwang             struct alias_data *ah)
1354a9643ea8Slogwang{
1355a9643ea8Slogwang
1356a9643ea8Slogwang\&...
1357a9643ea8Slogwang}
1358a9643ea8Slogwang.Ed
1359a9643ea8Slogwangand they must accept exactly these input parameters.
1360a9643ea8Slogwang.Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES
1361a9643ea8SlogwangTo add module support into an application that uses
1362a9643ea8Slogwang.Nm ,
1363a9643ea8Slogwangthe following simple steps can be followed.
1364a9643ea8Slogwang.Bl -enum
1365a9643ea8Slogwang.It
1366a9643ea8SlogwangFind the main file of an application
1367a9643ea8Slogwang(let us call it
1368a9643ea8Slogwang.Pa main.c ) .
1369a9643ea8Slogwang.It
1370a9643ea8SlogwangAdd this to the header section of
1371a9643ea8Slogwang.Pa main.c ,
1372a9643ea8Slogwangif not already present:
1373a9643ea8Slogwang.Pp
1374a9643ea8Slogwang.Dl "#include <signal.h>"
1375a9643ea8Slogwang.Pp
1376a9643ea8Slogwangand this just after the header section:
1377a9643ea8Slogwang.Pp
1378a9643ea8Slogwang.Dl "static void signal_handler(int);"
1379a9643ea8Slogwang.It
1380a9643ea8SlogwangAdd the following line to the init function of an application or,
1381a9643ea8Slogwangif it does not have any init function, put it in
1382a9643ea8Slogwang.Fn main :
1383a9643ea8Slogwang.Pp
1384a9643ea8Slogwang.Dl "signal(SIGHUP, signal_handler);"
1385a9643ea8Slogwang.Pp
1386a9643ea8Slogwangand place the
1387a9643ea8Slogwang.Fn signal_handler
1388a9643ea8Slogwangfunction somewhere in
1389a9643ea8Slogwang.Pa main.c :
1390a9643ea8Slogwang.Bd -literal -offset indent
1391a9643ea8Slogwangstatic void
1392a9643ea8Slogwangsignal_handler(int sig)
1393a9643ea8Slogwang{
1394a9643ea8Slogwang
1395a9643ea8Slogwang	LibAliasRefreshModules();
1396a9643ea8Slogwang}
1397a9643ea8Slogwang.Ed
1398a9643ea8Slogwang.Pp
1399a9643ea8SlogwangOtherwise, if an application already traps the
1400a9643ea8Slogwang.Dv SIGHUP
1401a9643ea8Slogwangsignal, just add a call to
1402a9643ea8Slogwang.Fn LibAliasRefreshModules
1403a9643ea8Slogwangin the signal handler function.
1404a9643ea8Slogwang.El
1405a9643ea8SlogwangFor example, to patch
1406a9643ea8Slogwang.Xr natd 8
1407a9643ea8Slogwangto use
1408a9643ea8Slogwang.Nm
1409a9643ea8Slogwangmodules, just add the following line to
1410a9643ea8Slogwang.Fn RefreshAddr "int sig __unused" :
1411a9643ea8Slogwang.Pp
1412a9643ea8Slogwang.Dl "LibAliasRefreshModules()"
1413a9643ea8Slogwang.Pp
1414a9643ea8Slogwangrecompile and you are done.
1415a9643ea8Slogwang.Ss LOGGING SUPPORT IN KERNEL LAND
1416a9643ea8SlogwangWhen working as KLD,
1417a9643ea8Slogwang.Nm
1418a9643ea8Slogwangnow has log support that
1419a9643ea8Slogwanghappens on a buffer allocated inside
1420a9643ea8Slogwang.Vt "struct libalias"
1421a9643ea8Slogwang(from
1422a9643ea8Slogwang.Pa alias_local.h ) :
1423a9643ea8Slogwang.Bd -literal
1424a9643ea8Slogwangstruct libalias {
1425a9643ea8Slogwang       ...
1426a9643ea8Slogwang
1427a9643ea8Slogwang	/* log descriptor        */
1428a9643ea8Slogwang#ifdef	KERNEL_LOG
1429a9643ea8Slogwang	char           *logDesc;        /*
1430a9643ea8Slogwang					 * ptr to an auto-malloced
1431a9643ea8Slogwang					 * memory buffer when libalias
1432a9643ea8Slogwang					 * works as kld
1433a9643ea8Slogwang					 */
1434a9643ea8Slogwang#else
1435a9643ea8Slogwang	FILE           *logDesc;	/*
1436a9643ea8Slogwang					 * ptr to /var/log/alias.log
1437a9643ea8Slogwang					 * when libalias runs as a
1438a9643ea8Slogwang					 * userland lib
1439a9643ea8Slogwang					 */
1440a9643ea8Slogwang#endif
1441a9643ea8Slogwang
1442a9643ea8Slogwang	...
1443a9643ea8Slogwang}
1444a9643ea8Slogwang.Ed
1445a9643ea8Slogwangso all applications using
1446a9643ea8Slogwang.Nm
1447a9643ea8Slogwangwill be able to handle their
1448a9643ea8Slogwangown logs, if they want, accessing
1449a9643ea8Slogwang.Va logDesc .
1450a9643ea8SlogwangMoreover, every change to a log buffer is automatically added to
1451a9643ea8Slogwang.Xr syslog 3
1452a9643ea8Slogwangwith the
1453a9643ea8Slogwang.Dv LOG_SECURITY
1454a9643ea8Slogwangfacility and the
1455a9643ea8Slogwang.Dv LOG_INFO
1456a9643ea8Slogwanglevel.
1457a9643ea8Slogwang.Sh AUTHORS
1458a9643ea8Slogwang.An Charles Mott Aq [email protected] ,
1459a9643ea8Slogwangversions 1.0 - 1.8, 2.0 - 2.4.
1460a9643ea8Slogwang.An Eivind Eklund Aq [email protected] ,
1461a9643ea8Slogwangversions 1.8b, 1.9 and 2.5.
1462a9643ea8SlogwangAdded IRC DCC support as well as contributing a number of architectural
1463a9643ea8Slogwangimprovements; added the firewall bypass for FTP/IRC DCC.
1464a9643ea8Slogwang.An Erik Salander Aq [email protected]
1465a9643ea8Slogwangadded support for PPTP and RTSP.
1466a9643ea8Slogwang.An Junichi Satoh Aq [email protected]
1467a9643ea8Slogwangadded support for RTSP/PNA.
1468a9643ea8Slogwang.An Ruslan Ermilov Aq [email protected]
1469a9643ea8Slogwangadded support for PPTP and LSNAT as well as general hacking.
1470a9643ea8Slogwang.An Gleb Smirnoff Aq [email protected]
1471a9643ea8Slogwangported the library to kernel space.
1472a9643ea8Slogwang.An Paolo Pisati Aq [email protected]
1473a9643ea8Slogwangmade the library modular, moving support for all
1474a9643ea8Slogwangprotocols (except for IP, TCP and UDP) to external modules.
1475a9643ea8Slogwang.Sh ACKNOWLEDGEMENTS
1476a9643ea8SlogwangListed below, in approximate chronological order, are individuals who
1477a9643ea8Slogwanghave provided valuable comments and/or debugging assistance.
1478a9643ea8Slogwang.Bd -ragged -offset indent
1479a9643ea8Slogwang.An -split
1480a9643ea8Slogwang.An Gary Roberts
1481a9643ea8Slogwang.An Tom Torrance
1482a9643ea8Slogwang.An Reto Burkhalter
1483a9643ea8Slogwang.An Martin Renters
1484a9643ea8Slogwang.An Brian Somers
1485a9643ea8Slogwang.An Paul Traina
1486a9643ea8Slogwang.An Ari Suutari
1487a9643ea8Slogwang.An Dave Remien
1488a9643ea8Slogwang.An J. Fortes
1489a9643ea8Slogwang.An Andrzej Bialecki
1490a9643ea8Slogwang.An Gordon Burditt
1491a9643ea8Slogwang.Ed
1492