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