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