xref: /dragonfly/lib/libalias/libalias.3 (revision 532828a0)
1.\"-
2.\" Copyright (c) 2001 Charles Mott <cm@linktel.net>
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD: src/lib/libalias/libalias.3,v 1.23.2.11 2001/12/17 10:08:22 ru Exp $
27.\"
28.Dd April 13, 2000
29.Dt LIBALIAS 3
30.Os
31.Sh NAME
32.Nm libalias
33.Nd packet aliasing library for masquerading and network address translation
34.Sh LIBRARY
35.Lb libalias
36.Sh SYNOPSIS
37.In sys/types.h
38.In netinet/in.h
39.In alias.h
40.Pp
41Function prototypes are given in the main body of the text.
42.Sh DESCRIPTION
43The
44.Nm
45library is a collection of functions for aliasing and de-aliasing of IP
46packets, intended for masquerading and network address translation (NAT).
47.Sh INTRODUCTION
48This library is a moderately portable set of functions designed to assist
49in the process of IP masquerading and network address translation.
50Outgoing packets from a local network with unregistered IP addresses can
51be aliased to appear as if they came from an accessible IP address.
52Incoming packets are then de-aliased so that they are sent to the correct
53machine on the local network.
54.Pp
55A certain amount of flexibility is built into the packet aliasing engine.
56In the simplest mode of operation, a many-to-one address mapping takes
57place between local network and the packet aliasing host.
58This is known as IP masquerading.
59In addition, one-to-one mappings between local and public addresses can
60also be implemented, which is known as static NAT.
61In between these extremes, different groups of private addresses can be
62linked to different public addresses, comprising several distinct
63many-to-one mappings.
64Also, a given public address and port can be statically redirected to a
65private address/port.
66.Pp
67The packet aliasing engine was designed to operate in user space outside
68of the kernel, without any access to private kernel data structure, but
69the source code can also be ported to a kernel environment.
70.Sh INITIALIZATION AND CONTROL
71Two special functions,
72.Fn PacketAliasInit
73and
74.Fn PacketAliasSetAddress ,
75must always be called before any packet handling may be performed.
76In addition, the operating mode of the packet aliasing engine can be
77customized by calling
78.Fn PacketAliasSetMode .
79.Pp
80.Ft void
81.Fn PacketAliasInit void
82.Bd -ragged -offset indent
83This function has no arguments or return value and is used to initialize
84internal data structures.
85The following mode bits are always set after calling
86.Fn PacketAliasInit .
87See the description of
88.Fn PacketAliasSetMode
89below for the meaning of these mode bits.
90.Pp
91.Bl -item -offset indent -compact
92.It
93.Dv PKT_ALIAS_SAME_PORTS
94.It
95.Dv PKT_ALIAS_USE_SOCKETS
96.It
97.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
98.El
99.Pp
100This function will always return the packet aliasing engine to the same
101initial state.
102.Fn PacketAliasSetAddress
103must be called afterwards, and any desired changes from the default mode
104bits listed above require a call to
105.Fn PacketAliasSetMode .
106.Pp
107It is mandatory that this function be called at the beginning of a program
108prior to any packet handling.
109.Ed
110.Pp
111.Ft void
112.Fn PacketAliasUninit void
113.Bd -ragged -offset indent
114This function has no arguments or return value and is used to clear any
115resources attached to internal data structures.
116.Pp
117This functions should be called when a program stops using the aliasing
118engine; it does, amongst other things, clear out any firewall holes.
119To provide backwards compatibility and extra security, it is added to
120the
121.Xr atexit 3
122chain by
123.Fn PacketAliasInit .
124Calling it multiple times is harmless.
125.Ed
126.Pp
127.Ft void
128.Fn PacketAliasSetAddress "struct in_addr addr"
129.Bd -ragged -offset indent
130This function sets the source address to which outgoing packets from the
131local area network are aliased.
132All outgoing packets are re-mapped to this address unless overridden by a
133static address mapping established by
134.Fn PacketAliasRedirectAddr .
135.Pp
136If the
137.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
138mode bit is set (the default mode of operation), then the internal aliasing
139link tables will be reset any time the aliasing address changes.
140This is useful for interfaces such as
141.Xr ppp 8 ,
142where the IP
143address may or may not change on successive dial-up attempts.
144.Pp
145If the
146.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
147mode bit is set to zero, this function can also be used to dynamically change
148the aliasing address on a packet to packet basis (it is a low overhead call).
149.Pp
150It is mandatory that this function be called prior to any packet handling.
151.Ed
152.Pp
153.Ft unsigned int
154.Fn PacketAliasSetMode "unsigned int flags" "unsigned int mask"
155.Bd -ragged -offset indent
156This function sets or clears mode bits
157according to the value of
158.Fa flags .
159Only bits marked in
160.Fa mask
161are affected.
162The following mode bits are defined in
163.In alias.h :
164.Bl -tag -width indent
165.It Dv PKT_ALIAS_LOG
166Enables logging into
167.Pa /var/log/alias.log .
168Each time an aliasing link is created or deleted, the log file is appended
169with the current number of ICMP, TCP and UDP links.
170Mainly useful for debugging when the log file is viewed continuously with
171.Xr tail 1 .
172.It Dv PKT_ALIAS_DENY_INCOMING
173If this mode bit is set, all incoming packets associated with new TCP
174connections or new UDP transactions will be marked for being ignored
175.Fn ( PacketAliasIn
176returns
177.Dv PKT_ALIAS_IGNORED
178code)
179by the calling program.
180Response packets to connections or transactions initiated from the packet
181aliasing host or local network will be unaffected.
182This mode bit is useful for implementing a one-way firewall.
183.It Dv PKT_ALIAS_SAME_PORTS
184If this mode bit is set, the packet aliasing engine will attempt to leave
185the alias port numbers unchanged from the actual local port numbers.
186This can be done as long as the quintuple (proto, alias addr, alias port,
187remote addr, remote port) is unique.
188If a conflict exists, a new aliasing port number is chosen even if this
189mode bit is set.
190.It Dv PKT_ALIAS_USE_SOCKETS
191This bit should be set when the packet aliasing host originates network
192traffic as well as forwards it.
193When the packet aliasing host is waiting for a connection from an unknown
194host address or unknown port number (e.g. an FTP data connection), this
195mode bit specifies that a socket be allocated as a place holder to prevent
196port conflicts.
197Once a connection is established, usually within a minute or so, the socket
198is closed.
199.It Dv PKT_ALIAS_UNREGISTERED_ONLY
200If this mode bit is set, traffic on the local network which does not
201originate from unregistered address spaces will be ignored.
202Standard Class A, B and C unregistered addresses are:
203.Bd -literal -offset indent
20410.0.0.0     ->  10.255.255.255   (Class A subnet)
205172.16.0.0   ->  172.31.255.255   (Class B subnets)
206192.168.0.0  ->  192.168.255.255  (Class C subnets)
207.Ed
208.Pp
209This option is useful in the case that packet aliasing host has both
210registered and unregistered subnets on different interfaces.
211The registered subnet is fully accessible to the outside world, so traffic
212from it does not need to be passed through the packet aliasing engine.
213.It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
214When this mode bit is set and
215.Fn PacketAliasSetAddress
216is called to change the aliasing address, the internal link table of the
217packet aliasing engine will be cleared.
218This operating mode is useful for
219.Xr ppp 8
220links where the interface address can sometimes change or remain the same
221between dial-up attempts.
222If this mode bit is not set, the link table will never be reset in the event
223of an address change.
224.It Dv PKT_ALIAS_PUNCH_FW
225This option makes
226.Nm
227`punch holes' in an
228.Xr ipfirewall 4
229based firewall for FTP/IRC DCC connections.
230The holes punched are bound by from/to IP address and port; it will not be
231possible to use a hole for another connection.
232A hole is removed when the connection that uses it dies.
233To cater to unexpected death of a program using
234.Nm
235(e.g. kill -9),
236changing the state of the flag will clear the entire firewall range
237allocated for holes.
238This will also happen on the initial call to
239.Fn PacketAliasSetFWBase .
240This call must happen prior to setting this flag.
241.It Dv PKT_ALIAS_REVERSE
242This option makes
243.Nm
244reverse the way it handles incoming and outgoing packets, allowing it
245to be fed with data that passes through the internal interface rather
246than the external one.
247.It Dv PKT_ALIAS_PROXY_ONLY
248This option tells
249.Nm
250to obey transparent proxy rules only.
251Normal packet aliasing is not performed.
252See
253.Fn PacketAliasProxyRule
254below for details.
255.El
256.Ed
257.Pp
258.Ft void
259.Fn PacketAliasSetFWBase "unsigned int base" "unsigned int num"
260.Bd -ragged -offset indent
261Set firewall range allocated for punching firewall holes (with the
262.Dv PKT_ALIAS_PUNCH_FW
263flag).
264The range will be cleared for all rules on initialization.
265.Ed
266.Sh PACKET HANDLING
267The packet handling functions are used to modify incoming (remote to local)
268and outgoing (local to remote) packets.
269The calling program is responsible for receiving and sending packets via
270network interfaces.
271.Pp
272Along with
273.Fn PacketAliasInit
274and
275.Fn PacketAliasSetAddress ,
276the two packet handling functions,
277.Fn PacketAliasIn
278and
279.Fn PacketAliasOut ,
280comprise minimal set of functions needed for a basic IP masquerading
281implementation.
282.Pp
283.Ft int
284.Fn PacketAliasIn "char *buffer" "int maxpacketsize"
285.Bd -ragged -offset indent
286An incoming packet coming from a remote machine to the local network is
287de-aliased by this function.
288The IP packet is pointed to by
289.Fa buffer ,
290and
291.Fa maxpacketsize
292indicates the size of the data structure containing the packet and should
293be at least as large as the actual packet size.
294.Pp
295Return codes:
296.Bl -tag -width indent
297.It Dv PKT_ALIAS_OK
298The packet aliasing process was successful.
299.It Dv PKT_ALIAS_IGNORED
300The packet was ignored and not de-aliased.
301This can happen if the protocol is unrecognized, possibly an ICMP message
302type is not handled or if incoming packets for new connections are being
303ignored (if
304.Dv PKT_ALIAS_DENY_INCOMING
305mode bit was set by
306.Fn PacketAliasSetMode ) .
307.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
308This is returned when a fragment cannot be resolved because the header
309fragment has not been sent yet.
310In this situation, fragments must be saved with
311.Fn PacketAliasSaveFragment
312until a header fragment is found.
313.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
314The packet aliasing process was successful, and a header fragment was found.
315This is a signal to retrieve any unresolved fragments with
316.Fn PacketAliasGetFragment
317and de-alias them with
318.Fn PacketAliasFragmentIn .
319.It Dv PKT_ALIAS_ERROR
320An internal error within the packet aliasing engine occurred.
321.El
322.Ed
323.Pp
324.Ft int
325.Fn PacketAliasOut "char *buffer" "int maxpacketsize"
326.Bd -ragged -offset indent
327An outgoing packet coming from the local network to a remote machine is
328aliased by this function.
329The IP packet is pointed to by
330.Fa buffer ,
331and
332.Fa maxpacketsize
333indicates the maximum packet size permissible should the packet length be
334changed.
335IP encoding protocols place address and port information in the encapsulated
336data stream which has to be modified and can account for changes in packet
337length.
338Well known examples of such protocols are FTP and IRC DCC.
339.Pp
340Return codes:
341.Bl -tag -width indent
342.It Dv PKT_ALIAS_OK
343The packet aliasing process was successful.
344.It Dv PKT_ALIAS_IGNORED
345The packet was ignored and not aliased.
346This can happen if the protocol is unrecognized, or possibly an ICMP message
347type is not handled.
348.It Dv PKT_ALIAS_ERROR
349An internal error within the packet aliasing engine occurred.
350.El
351.Ed
352.Sh PORT AND ADDRESS REDIRECTION
353The functions described in this section allow machines on the local network
354to be accessible in some degree to new incoming connections from the external
355network.
356Individual ports can be re-mapped or static network address translations can
357be designated.
358.Pp
359.Ft struct alias_link *
360.Fo PacketAliasRedirectPort
361.Fa "struct in_addr local_addr"
362.Fa "u_short local_port"
363.Fa "struct in_addr remote_addr"
364.Fa "u_short remote_port"
365.Fa "struct in_addr alias_addr"
366.Fa "u_short alias_port"
367.Fa "u_char proto"
368.Fc
369.Bd -ragged -offset indent
370This function specifies that traffic from a given remote address/port to
371an alias address/port be redirected to a specified local address/port.
372The parameter
373.Fa proto
374can be either
375.Dv IPPROTO_TCP
376or
377.Dv IPPROTO_UDP ,
378as defined in
379.In netinet/in.h .
380.Pp
381If
382.Fa local_addr
383or
384.Fa alias_addr
385is zero, this indicates that the packet aliasing address as established
386by
387.Fn PacketAliasSetAddress
388is to be used.
389Even if
390.Fn PacketAliasSetAddress
391is called to change the address after
392.Fn PacketAliasRedirectPort
393is called, a zero reference will track this change.
394.Pp
395If the link is further set up to operate for a load sharing, then
396.Fa local_addr
397and
398.Fa local_port
399are ignored, and are selected dynamically from the server pool, as described in
400.Fn PacketAliasAddServer
401below.
402.Pp
403If
404.Fa remote_addr
405is zero, this indicates to redirect packets from any remote address.
406Likewise, if
407.Fa remote_port
408is zero, this indicates to redirect packets originating from any remote
409port number.
410Almost always, the remote port specification will be zero, but non-zero
411remote addresses can sometimes be useful for firewalling.
412If two calls to
413.Fn PacketAliasRedirectPort
414overlap in their address/port specifications, then the most recent call
415will have precedence.
416.Pp
417This function returns a pointer which can subsequently be used by
418.Fn PacketAliasRedirectDelete .
419If
420.Dv NULL
421is returned, then the function call did not complete successfully.
422.Pp
423All port numbers should be in network address byte order, so it is necessary
424to use
425.Xr htons 3
426to convert these parameters from internally readable numbers to network byte
427order.
428Addresses are also in network byte order, which is implicit in the use of the
429.Fa struct in_addr
430data type.
431.Ed
432.Pp
433.Ft struct alias_link *
434.Fo PacketAliasRedirectAddr
435.Fa "struct in_addr local_addr"
436.Fa "struct in_addr alias_addr"
437.Fc
438.Bd -ragged -offset indent
439This function designates that all incoming traffic to
440.Fa alias_addr
441be redirected to
442.Fa local_addr .
443Similarly, all outgoing traffic from
444.Fa local_addr
445is aliased to
446.Fa alias_addr .
447.Pp
448If
449.Fa local_addr
450or
451.Fa alias_addr
452is zero, this indicates that the packet aliasing address as established by
453.Fn PacketAliasSetAddress
454is to be used.
455Even if
456.Fn PacketAliasSetAddress
457is called to change the address after
458.Fn PacketAliasRedirectAddr
459is called, a zero reference will track this change.
460.Pp
461If the link is further set up to operate for a load sharing, then
462.Fa local_addr
463is ignored, and is selected dynamically from the server pool, as described in
464.Fn PacketAliasAddServer
465below.
466.Pp
467If subsequent calls to
468.Fn PacketAliasRedirectAddr
469use the same aliasing address, all new incoming traffic to this aliasing
470address will be redirected to the local address made in the last function
471call.
472New traffic generated by any of the local machines, designated in the
473several function calls, will be aliased to the same address.
474Consider the following example:
475.Bd -literal -offset indent
476PacketAliasRedirectAddr(inet_aton("192.168.0.2"),
477                        inet_aton("141.221.254.101"));
478PacketAliasRedirectAddr(inet_aton("192.168.0.3"),
479                        inet_aton("141.221.254.101"));
480PacketAliasRedirectAddr(inet_aton("192.168.0.4"),
481                        inet_aton("141.221.254.101"));
482.Ed
483.Pp
484Any outgoing connections such as
485.Xr telnet 1
486or
487.Xr ftp 1
488from 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
489141.221.254.101.
490Any incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
491.Pp
492Any calls to
493.Fn PacketAliasRedirectPort
494will have precedence over address mappings designated by
495.Fn PacketAliasRedirectAddr .
496.Pp
497This function returns a pointer which can subsequently be used by
498.Fn PacketAliasRedirectDelete .
499If
500.Dv NULL
501is returned, then the function call did not complete successfully.
502.Ed
503.Pp
504.Ft int
505.Fo PacketAliasAddServer
506.Fa "struct alias_link *link"
507.Fa "struct in_addr addr"
508.Fa "u_short port"
509.Fc
510.Bd -ragged -offset indent
511This function sets the
512.Fa link
513up for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
514LSNAT operates as follows.
515A client attempts to access a server by using the server virtual address.
516The LSNAT router transparently redirects the request to one of the hosts
517in server pool, selected using a real-time load sharing algorithm.
518Multiple sessions may be initiated from the same client, and each session
519could be directed to a different host based on load balance across server
520pool hosts at the time.
521If load share is desired for just a few specific services, the configuration
522on LSNAT could be defined to restrict load share for just the services
523desired.
524.Pp
525Currently, only the simplest selection algorithm is implemented, where a
526host is selected on a round-robin basis only, without regard to load on
527the host.
528.Pp
529First, the
530.Fa link
531is created by either
532.Fn PacketAliasRedirectPort
533or
534.Fn PacketAliasRedirectAddr .
535Then,
536.Fn PacketAliasAddServer
537is called multiple times to add entries to the
538.Fa link Ns 's
539server pool.
540.Pp
541For links created with
542.Fn PacketAliasRedirectAddr ,
543the
544.Fa port
545argument is ignored and could have any value, e.g. htons(~0).
546.Pp
547This function returns 0 on success, -1 otherwise.
548.Ed
549.Pp
550.Ft void
551.Fn PacketAliasRedirectDelete "struct alias_link *link"
552.Bd -ragged -offset indent
553This function will delete a specific static redirect rule entered by
554.Fn PacketAliasRedirectPort
555or
556.Fn PacketAliasRedirectAddr .
557The parameter
558.Fa link
559is the pointer returned by either of the redirection functions.
560If an invalid pointer is passed to
561.Fn PacketAliasRedirectDelete ,
562then a program crash or unpredictable operation could result, so it is
563necessary to be careful using this function.
564.Ed
565.Pp
566.Ft int
567.Fn PacketAliasProxyRule "const char *cmd"
568.Bd -ragged -offset indent
569The passed
570.Fa cmd
571string consists of one or more pairs of words.
572The first word in each pair is a token and the second is the value that
573should be applied for that token.
574Tokens and their argument types are as follows:
575.Bl -tag -width indent
576.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
577In order to support transparent proxying, it is necessary to somehow
578pass the original address and port information into the new destination
579server.
580If
581.Cm encode_ip_hdr
582is specified, the original address and port is passed as an extra IP
583option.
584If
585.Cm encode_tcp_stream
586is specified, the original address and port is passed as the first
587piece of data in the TCP stream in the format
588.Dq DEST Ar IP port .
589.It Cm port Ar portnum
590Only packets with the destination port
591.Ar portnum
592are proxied.
593.It Cm server Ar host Ns Xo
594.Op : Ns Ar portnum
595.Xc
596This specifies the
597.Ar host
598and
599.Ar portnum
600that the data is to be redirected to.
601.Ar host
602must be an IP address rather than a DNS host name.
603If
604.Ar portnum
605is not specified, the destination port number is not changed.
606.Pp
607The
608.Ar server
609specification is mandatory unless the
610.Cm delete
611command is being used.
612.It Cm rule Ar index
613Normally, each call to
614.Fn PacketAliasProxyRule
615inserts the next rule at the start of a linear list of rules.
616If an
617.Ar index
618is specified, the new rule will be checked after all rules with lower
619indices.
620Calls to
621.Fn PacketAliasProxyRule
622that do not specify a rule are assigned rule 0.
623.It Cm delete Ar index
624This token and its argument MUST NOT be used with any other tokens.
625When used, all existing rules with the given
626.Ar index
627are deleted.
628.It Cm proto tcp | udp
629If specified, only packets of the given protocol type are matched.
630.It Cm src Ar IP Ns Xo
631.Op / Ns Ar bits
632.Xc
633If specified, only packets with a source address matching the given
634.Ar IP
635are matched.
636If
637.Ar bits
638is also specified, then the first
639.Ar bits
640bits of
641.Ar IP
642are taken as a network specification, and all IP addresses from that
643network will be matched.
644.It Cm dst Ar IP Ns Xo
645.Op / Ns Ar bits
646.Xc
647If specified, only packets with a destination address matching the given
648.Ar IP
649are matched.
650If
651.Ar bits
652is also specified, then the first
653.Ar bits
654bits of
655.Ar IP
656are taken as a network specification, and all IP addresses from that
657network will be matched.
658.El
659.Pp
660This function is usually used to redirect outgoing connections for
661internal machines that are not permitted certain types of internet
662access, or to restrict access to certain external machines.
663.Ed
664.Pp
665.Ft struct alias_link *
666.Fo PacketAliasRedirectProto
667.Fa "struct in_addr local_addr"
668.Fa "struct in_addr remote_addr"
669.Fa "struct in_addr alias_addr"
670.Fa "u_char proto"
671.Fc
672.Bd -ragged -offset indent
673This function specifies that any IP packet with protocol number of
674.Fa proto
675from a given remote address to an alias address be
676redirected to a specified local address.
677.Pp
678If
679.Fa local_addr
680or
681.Fa alias_addr
682is zero, this indicates that the packet aliasing address as established
683by
684.Fn PacketAliasSetAddress
685is to be used.
686Even if
687.Fn PacketAliasSetAddress
688is called to change the address after
689.Fn PacketAliasRedirectProto
690is called, a zero reference will track this change.
691.Pp
692If
693.Fa remote_addr
694is zero, this indicates to redirect packets from any remote address.
695Non-zero remote addresses can sometimes be useful for firewalling.
696.Pp
697If two calls to
698.Fn PacketAliasRedirectProto
699overlap in their address specifications, then the most recent call
700will have precedence.
701.Pp
702This function returns a pointer which can subsequently be used by
703.Fn PacketAliasRedirectDelete .
704If
705.Dv NULL
706is returned, then the function call did not complete successfully.
707.Ed
708.Sh FRAGMENT HANDLING
709The functions in this section are used to deal with incoming fragments.
710.Pp
711Outgoing fragments are handled within
712.Fn PacketAliasOut
713by changing the address according to any applicable mapping set by
714.Fn PacketAliasRedirectAddr ,
715or the default aliasing address set by
716.Fn PacketAliasSetAddress .
717.Pp
718Incoming fragments are handled in one of two ways.
719If the header of a fragmented IP packet has already been seen, then all
720subsequent fragments will be re-mapped in the same manner the header
721fragment was.
722Fragments which arrive before the header are saved and then retrieved
723once the header fragment has been resolved.
724.Pp
725.Ft int
726.Fn PacketAliasSaveFragment "char *ptr"
727.Bd -ragged -offset indent
728When
729.Fn PacketAliasIn
730returns
731.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
732this function can be used to save the pointer to the unresolved fragment.
733.Pp
734It is implicitly assumed that
735.Fa ptr
736points to a block of memory allocated by
737.Xr malloc 3 .
738If the fragment is never resolved, the packet aliasing engine will
739automatically free the memory after a timeout period.
740[Eventually this function should be modified so that a callback function
741for freeing memory is passed as an argument.]
742.Pp
743This function returns
744.Dv PKT_ALIAS_OK
745if it was successful and
746.Dv PKT_ALIAS_ERROR
747if there was an error.
748.Ed
749.Pp
750.Ft char *
751.Fn PacketAliasGetFragment "char *buffer"
752.Bd -ragged -offset indent
753This function can be used to retrieve fragment pointers saved by
754.Fn PacketAliasSaveFragment .
755The IP header fragment pointed to by
756.Fa buffer
757is the header fragment indicated when
758.Fn PacketAliasIn
759returns
760.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
761Once a fragment pointer is retrieved, it becomes the calling program's
762responsibility to free the dynamically allocated memory for the fragment.
763.Pp
764.Fn PacketAliasGetFragment
765can be called sequentially until there are no more fragments available,
766at which time it returns
767.Dv NULL .
768.Ed
769.Pp
770.Ft void
771.Fn PacketAliasFragmentIn "char *header" "char *fragment"
772.Bd -ragged -offset indent
773When a fragment is retrieved with
774.Fn PacketAliasGetFragment ,
775it can then be de-aliased with a call to
776.Fn PacketAliasFragmentIn .
777The
778.Fa header
779argument is the pointer to a header fragment used as a template, and
780.Fa fragment
781is the pointer to the packet to be de-aliased.
782.Ed
783.Sh MISCELLANEOUS FUNCTIONS
784.Ft void
785.Fn PacketAliasSetTarget "struct in_addr addr"
786.Bd -ragged -offset indent
787When an incoming packet not associated with any pre-existing aliasing link
788arrives at the host machine, it will be sent to the address indicated by a
789call to
790.Fn PacketAliasSetTarget .
791.Pp
792If this function is called with an
793.Dv INADDR_NONE
794address argument, then all new incoming packets go to the address set by
795.Fn PacketAliasSetAddress .
796.Pp
797If this function is not called, or is called with an
798.Dv INADDR_ANY
799address argument, then all new incoming packets go to the address specified
800in the packet.
801This allows external machines to talk directly to internal machines if they
802can route packets to the machine in question.
803.Ed
804.Pp
805.Ft int
806.Fn PacketAliasCheckNewLink void
807.Bd -ragged -offset indent
808This function returns a non-zero value when a new aliasing link is created.
809In circumstances where incoming traffic is being sequentially sent to
810different local servers, this function can be used to trigger when
811.Fn PacketAliasSetTarget
812is called to change the default target address.
813.Ed
814.Pp
815.Ft u_short
816.Fn PacketAliasInternetChecksum "u_short *buffer" "int nbytes"
817.Bd -ragged -offset indent
818This is a utility function that does not seem to be available elsewhere and
819is included as a convenience.
820It computes the internet checksum, which is used in both IP and
821protocol-specific headers (TCP, UDP, ICMP).
822.Pp
823The
824.Fa buffer
825argument points to the data block to be checksummed, and
826.Fa nbytes
827is the number of bytes.
828The 16-bit checksum field should be zeroed before computing the checksum.
829.Pp
830Checksums can also be verified by operating on a block of data including
831its checksum.
832If the checksum is valid,
833.Fn PacketAliasInternetChecksum
834will return zero.
835.Ed
836.Pp
837.Ft int
838.Fn PacketUnaliasOut "char *buffer" "int maxpacketsize"
839.Bd -ragged -offset indent
840An outgoing packet, which has already been aliased,
841has its private address/port information restored by this function.
842The IP packet is pointed to by
843.Fa buffer ,
844and
845.Fa maxpacketsize
846is provided for error checking purposes.
847This function can be used if an already-aliased packet needs to have its
848original IP header restored for further processing (eg. logging).
849.Ed
850.Sh AUTHORS
851.An Charles Mott Aq Mt cm@linktel.net ,
852versions 1.0 - 1.8, 2.0 - 2.4.
853.An Eivind Eklund Aq Mt eivind@FreeBSD.org ,
854versions 1.8b, 1.9 and 2.5.
855Added IRC DCC support as well as contributing a number of architectural
856improvements; added the firewall bypass for FTP/IRC DCC.
857.An Erik Salander Aq Mt erik@whistle.com
858added support for PPTP and RTSP.
859.An Junichi Satoh Aq Mt junichi@junichi.org
860added support for RTSP/PNA.
861.Sh ACKNOWLEDGMENTS
862Listed below, in approximate chronological order, are individuals who
863have provided valuable comments and/or debugging assistance.
864.Bd -ragged -offset indent
865.An -split
866.An Gary Roberts
867.An Tom Torrance
868.An Reto Burkhalter
869.An Martin Renters
870.An Brian Somers
871.An Paul Traina
872.An Ari Suutari
873.An Dave Remien
874.An J. Fortes
875.An Andrzej Bialecki
876.An Gordon Burditt
877.Ed
878.Sh CONCEPTUAL BACKGROUND
879This section is intended for those who are planning to modify the source
880code or want to create somewhat esoteric applications using the packet
881aliasing functions.
882.Pp
883The conceptual framework under which the packet aliasing engine operates
884is described here.
885Central to the discussion is the idea of an
886.Em aliasing link
887which describes the relationship for a given packet transaction between
888the local machine, aliased identity and remote machine.
889It is discussed how such links come into existence and are destroyed.
890.Ss ALIASING LINKS
891There is a notion of an
892.Em aliasing link ,
893which is a 7-tuple describing a specific translation:
894.Bd -literal -offset indent
895(local addr, local port, alias addr, alias port,
896 remote addr, remote port, protocol)
897.Ed
898.Pp
899Outgoing packets have the local address and port number replaced with the
900alias address and port number.
901Incoming packets undergo the reverse process.
902The packet aliasing engine attempts to match packets against an internal
903table of aliasing links to determine how to modify a given IP packet.
904Both the IP header and protocol dependent headers are modified as necessary.
905Aliasing links are created and deleted as necessary according to network
906traffic.
907.Pp
908Protocols can be TCP, UDP or even ICMP in certain circumstances.
909(Some types of ICMP packets can be aliased according to sequence or ID
910number which acts as an equivalent port number for identifying how
911individual packets should be handled.)
912.Pp
913Each aliasing link must have a unique combination of the following five
914quantities: alias address/port, remote address/port and protocol.
915This ensures that several machines on a local network can share the
916same aliasing IP address.
917In cases where conflicts might arise, the aliasing port is chosen so that
918uniqueness is maintained.
919.Ss STATIC AND DYNAMIC LINKS
920Aliasing links can either be static or dynamic.
921Static links persist indefinitely and represent fixed rules for translating
922IP packets.
923Dynamic links come into existence for a specific TCP connection or UDP
924transaction or ICMP ECHO sequence.
925For the case of TCP, the connection can be monitored to see when the
926associated aliasing link should be deleted.
927Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
928work on a simple timeout rule.
929When no activity is observed on a dynamic link for a certain amount of time
930it is automatically deleted.
931Timeout rules also apply to TCP connections which do not open or close
932properly.
933.Ss PARTIALLY SPECIFIED ALIASING LINKS
934Aliasing links can be partially specified, meaning that the remote address
935and/or remote port are unknown.
936In this case, when a packet matching the incomplete specification is found,
937a fully specified dynamic link is created.
938If the original partially specified link is dynamic, it will be deleted
939after the fully specified link is created, otherwise it will persist.
940.Pp
941For instance, a partially specified link might be
942.Bd -literal -offset indent
943(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
944.Ed
945.Pp
946The zeros denote unspecified components for the remote address and port.
947If this link were static it would have the effect of redirecting all
948incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
949of machine 192.168.0.4 on the local network.
950Each individual telnet connection would initiate the creation of a distinct
951dynamic link.
952.Ss DYNAMIC LINK CREATION
953In addition to aliasing links, there are also address mappings that can be
954stored within the internal data table of the packet aliasing mechanism.
955.Bd -literal -offset indent
956(local addr, alias addr)
957.Ed
958.Pp
959Address mappings are searched when creating new dynamic links.
960.Pp
961All outgoing packets from the local network automatically create a dynamic
962link if they do not match an already existing fully specified link.
963If an address mapping exists for the outgoing packet, this determines
964the alias address to be used.
965If no mapping exists, then a default address, usually the address of the
966packet aliasing host, is used.
967If necessary, this default address can be changed as often as each individual
968packet arrives.
969.Pp
970The aliasing port number is determined such that the new dynamic link does
971not conflict with any existing links.
972In the default operating mode, the packet aliasing engine attempts to set
973the aliasing port equal to the local port number.
974If this results in a conflict, then port numbers are randomly chosen until
975a unique aliasing link can be established.
976In an alternate operating mode, the first choice of an aliasing port is also
977random and unrelated to the local port number.
978.Sh BUGS
979PPTP aliasing does not work when more than one internal client
980connects to the same external server at the same time, because
981PPTP requires a single TCP control connection to be established
982between any two IP addresses.
983