2 .\" Copyright (c) 2001 Charles Mott <cm@linktel.net>
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
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.
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
33 .Nd packet aliasing library for masquerading and network address translation
39 Function prototypes are given in the main body of the text.
43 library is a collection of functions for aliasing and de-aliasing of IP
44 packets, intended for masquerading and network address translation (NAT).
46 This library is a moderately portable set of functions designed to assist
47 in the process of IP masquerading and network address translation.
48 Outgoing packets from a local network with unregistered IP addresses can
49 be aliased to appear as if they came from an accessible IP address.
50 Incoming packets are then de-aliased so that they are sent to the correct
51 machine on the local network.
53 A certain amount of flexibility is built into the packet aliasing engine.
54 In the simplest mode of operation, a many-to-one address mapping takes
55 place between the local network and the packet aliasing host.
56 This is known as IP masquerading.
57 In addition, one-to-one mappings between local and public addresses can
58 also be implemented, which is known as static NAT.
59 In between these extremes, different groups of private addresses can be
60 linked to different public addresses, comprising several distinct
62 Also, a given public address and port can be statically redirected to a
64 .Sh INITIALIZATION AND CONTROL
67 must always be called before any packet handling may be performed, and
68 the returned instance pointer must be passed to all the other functions.
70 .Fn LibAliasSetAddress
71 function is called afterwards, to set the default aliasing address.
72 In addition, the operating mode of the packet aliasing engine can be
76 .Ft "struct libalias *"
77 .Fn LibAliasInit "struct libalias *"
78 .Bd -ragged -offset indent
79 This function is used to initialize
80 internal data structures.
81 When called the first time, a
83 pointer should be passed as an argument.
84 The following mode bits are always set after calling
86 See the description of
88 below for the meaning of these mode bits.
90 .Bl -item -offset indent -compact
92 .Dv PKT_ALIAS_SAME_PORTS
94 .Dv PKT_ALIAS_USE_SOCKETS
96 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
99 This function will always return the packet aliasing engine to the same
102 .Fn LibAliasSetAddress
103 function is normally called afterwards, and any desired changes from the
104 default mode bits listed above require a call to
105 .Fn LibAliasSetMode .
107 It is mandatory that this function be called at the beginning of a program
108 prior to any packet handling.
112 .Fn LibAliasUninit "struct libalias *"
113 .Bd -ragged -offset indent
114 This function has no return value and is used to clear any
115 resources attached to internal data structures.
117 This function should be called when a program stops using the aliasing
118 engine; amongst other things, it clears out any firewall holes.
119 To provide backwards compatibility and extra security, it is added to
127 .Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr"
128 .Bd -ragged -offset indent
129 This function sets the source address to which outgoing packets from the
130 local area network are aliased.
131 All outgoing packets are re-mapped to this address unless overridden by a
132 static address mapping established by
133 .Fn LibAliasRedirectAddr .
134 If this function has not been called, and no static rules match, an outgoing
135 packet retains its source address.
138 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
139 mode bit is set (the default mode of operation), then the internal aliasing
140 link tables will be reset any time the aliasing address changes.
141 This is useful for interfaces such as
144 address may or may not change on successive dial-up attempts.
147 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
148 mode bit is set to zero, this function can also be used to dynamically change
149 the aliasing address on a packet-to-packet basis (it is a low overhead call).
151 It is mandatory that this function be called prior to any packet handling.
155 .Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask"
156 .Bd -ragged -offset indent
157 This function sets or clears mode bits
158 according to the value of
163 The following mode bits are defined in
165 .Bl -tag -width indent
168 .Pa /var/log/alias.log .
169 Each time an aliasing link is created or deleted, the log file is appended to
170 with the current number of ICMP, TCP and UDP links.
171 Mainly useful for debugging when the log file is viewed continuously with
173 .It Dv PKT_ALIAS_DENY_INCOMING
174 If this mode bit is set, all incoming packets associated with new TCP
175 connections or new UDP transactions will be marked for being ignored
178 .Dv PKT_ALIAS_IGNORED
180 by the calling program.
181 Response packets to connections or transactions initiated from the packet
182 aliasing host or local network will be unaffected.
183 This mode bit is useful for implementing a one-way firewall.
184 .It Dv PKT_ALIAS_SAME_PORTS
185 If this mode bit is set, the packet-aliasing engine will attempt to leave
186 the alias port numbers unchanged from the actual local port numbers.
187 This can be done as long as the quintuple (proto, alias addr, alias port,
188 remote addr, remote port) is unique.
189 If a conflict exists, a new aliasing port number is chosen even if this
191 .It Dv PKT_ALIAS_USE_SOCKETS
192 This bit should be set when the packet aliasing host originates network
193 traffic as well as forwards it.
194 When the packet aliasing host is waiting for a connection from an unknown
195 host address or unknown port number (e.g.\& an FTP data connection), this
196 mode bit specifies that a socket be allocated as a place holder to prevent
198 Once a connection is established, usually within a minute or so, the socket
200 .It Dv PKT_ALIAS_UNREGISTERED_ONLY
201 If this mode bit is set, traffic on the local network which does not
202 originate from unregistered address spaces will be ignored.
203 Standard Class A, B and C unregistered addresses are:
205 10.0.0.0 -> 10.255.255.255 (Class A subnet)
206 172.16.0.0 -> 172.31.255.255 (Class B subnets)
207 192.168.0.0 -> 192.168.255.255 (Class C subnets)
209 This option is useful in the case that the packet aliasing host has both
210 registered and unregistered subnets on different interfaces.
211 The registered subnet is fully accessible to the outside world, so traffic
212 from it does not need to be passed through the packet aliasing engine.
213 .It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
214 When this mode bit is set and
215 .Fn LibAliasSetAddress
216 is called to change the aliasing address, the internal link table of the
217 packet aliasing engine will be cleared.
218 This operating mode is useful for
220 links where the interface address can sometimes change or remain the same
221 between dial-up attempts.
222 If this mode bit is not set, the link table will never be reset in the event
223 of an address change.
224 .It Dv PKT_ALIAS_PUNCH_FW
230 based firewall for FTP/IRC DCC connections.
231 The holes punched are bound by from/to IP address and port; it will not be
232 possible to use a hole for another connection.
233 A hole is removed when the connection that uses it dies.
234 To cater to unexpected death of a program using
237 changing the state of the flag will clear the entire firewall range
239 This clearing will also happen on the initial call to
240 .Fn LibAliasSetFWBase ,
241 which must happen prior to setting this flag.
242 .It Dv PKT_ALIAS_REVERSE
245 reverse the way it handles incoming and outgoing packets, allowing it
246 to be fed with data that passes through the internal interface rather
247 than the external one.
248 .It Dv PKT_ALIAS_PROXY_ONLY
251 to obey transparent proxy rules only.
252 Normal packet aliasing is not performed.
254 .Fn LibAliasProxyRule
256 .It Dv PKT_ALIAS_SKIP_GLOBAL
257 This option is used by
260 Specifying it as a flag to
264 .Sx NETWORK ADDRESS TRANSLATION
272 .Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num"
273 .Bd -ragged -offset indent
274 Set the firewall range allocated for punching firewall holes (with the
275 .Dv PKT_ALIAS_PUNCH_FW
277 The range is cleared for all rules on initialization.
281 .Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port"
282 .Bd -ragged -offset indent
283 Set the TCP port used by the Skinny Station protocol.
284 Skinny is used by Cisco IP phones to communicate with
285 Cisco Call Managers to set up voice over IP calls.
286 If this is not set, Skinny aliasing will not be done.
287 The typical port used by Skinny is 2000.
290 The packet handling functions are used to modify incoming (remote to local)
291 and outgoing (local to remote) packets.
292 The calling program is responsible for receiving and sending packets via
298 .Fn LibAliasSetAddress ,
299 the two packet handling functions,
303 comprise the minimal set of functions needed for a basic IP masquerading
307 .Fn LibAliasIn "struct libalias *" "char *buffer" "int maxpacketsize"
308 .Bd -ragged -offset indent
309 An incoming packet coming from a remote machine to the local network is
310 de-aliased by this function.
311 The IP packet is pointed to by
315 indicates the size of the data structure containing the packet and should
316 be at least as large as the actual packet size.
319 .Bl -tag -width indent
321 The packet aliasing process was successful.
322 .It Dv PKT_ALIAS_IGNORED
323 The packet was ignored and not de-aliased.
324 This can happen if the protocol is unrecognized, as for an ICMP message
325 type that is not handled, or if incoming packets for new connections are being
327 .Dv PKT_ALIAS_DENY_INCOMING
328 mode bit was set using
329 .Fn LibAliasSetMode ) .
330 .It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
331 This is returned when a fragment cannot be resolved because the header
332 fragment has not been sent yet.
333 In this situation, fragments must be saved with
334 .Fn LibAliasSaveFragment
335 until a header fragment is found.
336 .It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
337 The packet aliasing process was successful, and a header fragment was found.
338 This is a signal to retrieve any unresolved fragments with
339 .Fn LibAliasGetFragment
340 and de-alias them with
341 .Fn LibAliasFragmentIn .
342 .It Dv PKT_ALIAS_ERROR
343 An internal error within the packet aliasing engine occurred.
348 .Fn LibAliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
349 .Bd -ragged -offset indent
350 An outgoing packet coming from the local network to a remote machine is
351 aliased by this function.
352 The IP packet is pointed to by
356 indicates the maximum packet size permissible should the packet length be
358 IP encoding protocols place address and port information in the encapsulated
359 data stream which has to be modified and can account for changes in packet
361 Well known examples of such protocols are FTP and IRC DCC.
364 .Bl -tag -width indent
366 The packet aliasing process was successful.
367 .It Dv PKT_ALIAS_IGNORED
368 The packet was ignored and not aliased.
369 This can happen if the protocol is unrecognized, or possibly an ICMP message
371 .It Dv PKT_ALIAS_ERROR
372 An internal error within the packet aliasing engine occurred.
375 .Sh PORT AND ADDRESS REDIRECTION
376 The functions described in this section allow machines on the local network
377 to be accessible in some degree to new incoming connections from the external
379 Individual ports can be re-mapped or static network address translations can
382 .Ft struct alias_link *
383 .Fo LibAliasRedirectPort
384 .Fa "struct libalias *"
385 .Fa "struct in_addr local_addr"
386 .Fa "u_short local_port"
387 .Fa "struct in_addr remote_addr"
388 .Fa "u_short remote_port"
389 .Fa "struct in_addr alias_addr"
390 .Fa "u_short alias_port"
393 .Bd -ragged -offset indent
394 This function specifies that traffic from a given remote address/port to
395 an alias address/port be redirected to a specified local address/port.
409 is zero, this indicates that the packet aliasing address as established
411 .Fn LibAliasSetAddress
414 .Fn LibAliasSetAddress
415 is called to change the address after
416 .Fn LibAliasRedirectPort
417 is called, a zero reference will track this change.
419 If the link is further set up to operate with load sharing, then
423 are ignored, and are selected dynamically from the server pool, as described in
424 .Fn LibAliasAddServer
429 is zero, this indicates to redirect packets from any remote address.
432 is zero, this indicates to redirect packets originating from any remote
434 The remote port specification will almost always be zero, but non-zero
435 remote addresses can sometimes be useful for firewalling.
437 .Fn LibAliasRedirectPort
438 overlap in their address/port specifications, then the most recent call
439 will have precedence.
441 This function returns a pointer which can subsequently be used by
442 .Fn LibAliasRedirectDelete .
445 is returned, then the function call did not complete successfully.
447 All port numbers should be in network address byte order, so it is necessary
450 to convert these parameters from internally readable numbers to network byte
452 Addresses are also in network byte order, which is implicit in the use of the
457 .Ft struct alias_link *
458 .Fo LibAliasRedirectAddr
459 .Fa "struct libalias *"
460 .Fa "struct in_addr local_addr"
461 .Fa "struct in_addr alias_addr"
463 .Bd -ragged -offset indent
464 This function designates that all incoming traffic to
468 Similarly, all outgoing traffic from
477 is zero, this indicates that the packet aliasing address as established by
478 .Fn LibAliasSetAddress
481 .Fn LibAliasSetAddress
482 is called to change the address after
483 .Fn LibAliasRedirectAddr
484 is called, a zero reference will track this change.
486 If the link is further set up to operate with load sharing, then the
488 argument is ignored, and is selected dynamically from the server pool,
490 .Fn LibAliasAddServer
493 If subsequent calls to
494 .Fn LibAliasRedirectAddr
495 use the same aliasing address, all new incoming traffic to this aliasing
496 address will be redirected to the local address made in the last function
498 New traffic generated by any of the local machines, designated in the
499 several function calls, will be aliased to the same address.
500 Consider the following example:
502 LibAliasRedirectAddr(la, inet_aton("192.168.0.2"),
503 inet_aton("141.221.254.101"));
504 LibAliasRedirectAddr(la, inet_aton("192.168.0.3"),
505 inet_aton("141.221.254.101"));
506 LibAliasRedirectAddr(la, inet_aton("192.168.0.4"),
507 inet_aton("141.221.254.101"));
509 Any outgoing connections such as
513 from 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
515 Any incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
518 .Fn LibAliasRedirectPort
519 will have precedence over address mappings designated by
520 .Fn LibAliasRedirectAddr .
522 This function returns a pointer which can subsequently be used by
523 .Fn LibAliasRedirectDelete .
526 is returned, then the function call did not complete successfully.
530 .Fo LibAliasAddServer
531 .Fa "struct libalias *"
532 .Fa "struct alias_link *link"
533 .Fa "struct in_addr addr"
536 .Bd -ragged -offset indent
537 This function sets the
539 up for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
540 LSNAT operates as follows.
541 A client attempts to access a server by using the server virtual address.
542 The LSNAT router transparently redirects the request to one of the hosts
543 in the server pool, using a real-time load sharing algorithm.
544 Multiple sessions may be initiated from the same client, and each session
545 could be directed to a different host based on the load balance across server
546 pool hosts when the sessions are initiated.
547 If load sharing is desired for just a few specific services, the configuration
548 on LSNAT could be defined to restrict load sharing to just the services
551 Currently, only the simplest selection algorithm is implemented, where a
552 host is selected on a round-robin basis only, without regard to load on
558 .Fn LibAliasRedirectPort
560 .Fn LibAliasRedirectAddr .
562 .Fn LibAliasAddServer
563 is called multiple times to add entries to the
567 For links created with
568 .Fn LibAliasRedirectAddr ,
571 argument is ignored and could have any value, e.g.\& htons(~0).
573 This function returns 0 on success, \-1 otherwise.
577 .Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link"
578 .Bd -ragged -offset indent
579 This function marks the specified static redirect rule entered by
580 .Fn LibAliasRedirectPort
582 This can be used to e.g.\& dynamically redirect a single TCP connection,
583 after which the rule is removed.
584 Only fully specified links can be made dynamic.
586 .Sx STATIC AND DYNAMIC LINKS
588 .Sx PARTIALLY SPECIFIED ALIASING LINKS
589 sections below for a definition of static vs.\& dynamic,
590 and partially vs.\& fully specified links.)
592 This function returns 0 on success, \-1 otherwise.
596 .Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link"
597 .Bd -ragged -offset indent
598 This function will delete a specific static redirect rule entered by
599 .Fn LibAliasRedirectPort
601 .Fn LibAliasRedirectAddr .
604 is the pointer returned by either of the redirection functions.
605 If an invalid pointer is passed to
606 .Fn LibAliasRedirectDelete ,
607 then a program crash or unpredictable operation could result, so
608 care is needed when using this function.
612 .Fn LibAliasProxyRule "struct libalias *" "const char *cmd"
613 .Bd -ragged -offset indent
616 string consists of one or more pairs of words.
617 The first word in each pair is a token and the second is the value that
618 should be applied for that token.
619 Tokens and their argument types are as follows:
620 .Bl -tag -width indent
621 .It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
622 In order to support transparent proxying, it is necessary to somehow
623 pass the original address and port information into the new destination
627 is specified, the original destination address and port are passed
628 as an extra IP option.
630 .Cm encode_tcp_stream
631 is specified, the original destination address and port are passed
632 as the first piece of data in the TCP stream in the format
633 .Dq Li DEST Ar IP port .
634 .It Cm port Ar portnum
635 Only packets with the destination port
638 .It Cm server Ar host Ns Op : Ns Ar portnum
643 that the data is to be redirected to.
645 must be an IP address rather than a DNS host name.
648 is not specified, the destination port number is not changed.
652 specification is mandatory unless the
654 command is being used.
656 Normally, each call to
657 .Fn LibAliasProxyRule
658 inserts the next rule at the start of a linear list of rules.
661 is specified, the new rule will be checked after all rules with lower
664 .Fn LibAliasProxyRule
665 that do not specify a rule are assigned rule 0.
666 .It Cm delete Ar index
667 This token and its argument MUST NOT be used with any other tokens.
668 When used, all existing rules with the given
671 .It Cm proto tcp | udp
672 If specified, only packets of the given protocol type are matched.
673 .It Cm src Ar IP Ns Op / Ns Ar bits
674 If specified, only packets with a source address matching the given
679 is also specified, then the first
683 are taken as a network specification, and all IP addresses from that
684 network will be matched.
685 .It Cm dst Ar IP Ns Op / Ns Ar bits
686 If specified, only packets with a destination address matching the given
691 is also specified, then the first
695 are taken as a network specification, and all IP addresses from that
696 network will be matched.
699 This function is usually used to redirect outgoing connections for
700 internal machines that are not permitted certain types of internet
701 access, or to restrict access to certain external machines.
704 .Ft struct alias_link *
705 .Fo LibAliasRedirectProto
706 .Fa "struct libalias *"
707 .Fa "struct in_addr local_addr"
708 .Fa "struct in_addr remote_addr"
709 .Fa "struct in_addr alias_addr"
712 .Bd -ragged -offset indent
713 This function specifies that any IP packet with protocol number of
715 from a given remote address to an alias address will be
716 redirected to a specified local address.
722 is zero, this indicates that the packet aliasing address as established
724 .Fn LibAliasSetAddress
727 .Fn LibAliasSetAddress
728 is called to change the address after
729 .Fn LibAliasRedirectProto
730 is called, a zero reference will track this change.
734 is zero, this indicates to redirect packets from any remote address.
735 Non-zero remote addresses can sometimes be useful for firewalling.
738 .Fn LibAliasRedirectProto
739 overlap in their address specifications, then the most recent call
740 will have precedence.
742 This function returns a pointer which can subsequently be used by
743 .Fn LibAliasRedirectDelete .
746 is returned, then the function call did not complete successfully.
748 .Sh FRAGMENT HANDLING
749 The functions in this section are used to deal with incoming fragments.
751 Outgoing fragments are handled within
753 by changing the address according to any applicable mapping set by
754 .Fn LibAliasRedirectAddr ,
755 or the default aliasing address set by
756 .Fn LibAliasSetAddress .
758 Incoming fragments are handled in one of two ways.
759 If the header of a fragmented IP packet has already been seen, then all
760 subsequent fragments will be re-mapped in the same manner the header
762 Fragments which arrive before the header are saved and then retrieved
763 once the header fragment has been resolved.
766 .Fn LibAliasSaveFragment "struct libalias *" "char *ptr"
767 .Bd -ragged -offset indent
771 .Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
772 this function can be used to save the pointer to the unresolved fragment.
774 It is implicitly assumed that
776 points to a block of memory allocated by
778 If the fragment is never resolved, the packet aliasing engine will
779 automatically free the memory after a timeout period.
780 [Eventually this function should be modified so that a callback function
781 for freeing memory is passed as an argument.]
783 This function returns
785 if it was successful and
787 if there was an error.
791 .Fn LibAliasGetFragment "struct libalias *" "char *buffer"
792 .Bd -ragged -offset indent
793 This function can be used to retrieve fragment pointers saved by
794 .Fn LibAliasSaveFragment .
795 The IP header fragment pointed to by
797 is the header fragment indicated when
800 .Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
801 Once a fragment pointer is retrieved, it becomes the calling program's
802 responsibility to free the dynamically allocated memory for the fragment.
805 .Fn LibAliasGetFragment
806 function can be called sequentially until there are no more fragments
807 available, at which time it returns
812 .Fn LibAliasFragmentIn "struct libalias *" "char *header" "char *fragment"
813 .Bd -ragged -offset indent
814 When a fragment is retrieved with
815 .Fn LibAliasGetFragment ,
816 it can then be de-aliased with a call to
817 .Fn LibAliasFragmentIn .
820 argument is the pointer to a header fragment used as a template, and
822 is the pointer to the packet to be de-aliased.
824 .Sh MISCELLANEOUS FUNCTIONS
825 .Ft struct alias_link *
826 .Fn AddLink "struct libalias *" "struct in_addr src_addr" "struct in_addr dst_addr" \
827 "struct in_addr alias_addr" "u_short src_port" "u_short dst_port" \
828 "int alias_param" "int link_type"
829 .Bd -ragged -offset indent
830 This function adds new state to the instance hash table.
831 The dst_address and/or dst_port may be given as zero, which
832 introduces some dynamic character into the link, since
833 LibAliasSetAddress can change the address that is used.
834 However, in the current implementation, such links can only be used
835 for inbound (ext -> int) traffic.
839 .Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr"
840 .Bd -ragged -offset indent
841 When an incoming packet not associated with any pre-existing aliasing link
842 arrives at the host machine, it will be sent to the address indicated by a
844 .Fn LibAliasSetTarget .
846 If this function is called with an
848 address argument, then all new incoming packets go to the address set by
849 .Fn LibAliasSetAddress .
851 If this function is not called, or is called with an
853 address argument, then all new incoming packets go to the address specified
855 This allows external machines to talk directly to internal machines if they
856 can route packets to the machine in question.
860 .Fn LibAliasCheckNewLink "struct libalias *"
861 .Bd -ragged -offset indent
862 This function returns a non-zero value when a new aliasing link is created.
863 In circumstances where incoming traffic is being sequentially sent to
864 different local servers, this function can be used to trigger when
865 .Fn LibAliasSetTarget
866 is called to change the default target address.
870 .Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes"
871 .Bd -ragged -offset indent
872 This is a utility function that does not seem to be available elsewhere and
873 is included as a convenience.
874 It computes the internet checksum, which is used in both IP and
875 protocol-specific headers (TCP, UDP, ICMP).
879 argument points to the data block to be checksummed, and
881 is the number of bytes.
882 The 16-bit checksum field should be zeroed before computing the checksum.
884 Checksums can also be verified by operating on a block of data including
886 If the checksum is valid,
887 .Fn LibAliasInternetChecksum
892 .Fn LibAliasUnaliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
893 .Bd -ragged -offset indent
894 An outgoing packet, which has already been aliased,
895 has its private address/port information restored by this function.
896 The IP packet is pointed to by
900 is provided for error checking purposes.
901 This function can be used if an already-aliased packet needs to have its
902 original IP header restored for further processing (e.g.\& logging).
904 .Sh CONCEPTUAL BACKGROUND
905 This section is intended for those who are planning to modify the source
906 code or want to create somewhat esoteric applications using the packet
909 The conceptual framework under which the packet aliasing engine operates
911 Central to the discussion is the idea of an
913 which describes the relationship for a given packet transaction between
914 the local machine, aliased identity and remote machine.
915 It is discussed how such links come into existence and are destroyed.
917 There is a notion of an
919 which is a 7-tuple describing a specific translation:
920 .Bd -literal -offset indent
921 (local addr, local port, alias addr, alias port,
922 remote addr, remote port, protocol)
925 Outgoing packets have the local address and port number replaced with the
926 alias address and port number.
927 Incoming packets undergo the reverse process.
928 The packet aliasing engine attempts to match packets against an internal
929 table of aliasing links to determine how to modify a given IP packet.
930 Both the IP header and protocol dependent headers are modified as necessary.
931 Aliasing links are created and deleted as necessary according to network
934 Protocols can be TCP, UDP or even ICMP in certain circumstances.
935 (Some types of ICMP packets can be aliased according to sequence or ID
936 number which acts as an equivalent port number for identifying how
937 individual packets should be handled.)
939 Each aliasing link must have a unique combination of the following five
940 quantities: alias address/port, remote address/port and protocol.
941 This ensures that several machines on a local network can share the
942 same aliasing IP address.
943 In cases where conflicts might arise, the aliasing port is chosen so that
944 uniqueness is maintained.
945 .Ss STATIC AND DYNAMIC LINKS
946 Aliasing links can either be static or dynamic.
947 Static links persist indefinitely and represent fixed rules for translating
949 Dynamic links come into existence for a specific TCP connection or UDP
950 transaction or ICMP ECHO sequence.
951 For the case of TCP, the connection can be monitored to see when the
952 associated aliasing link should be deleted.
953 Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
954 work on a simple timeout rule.
955 When no activity is observed on a dynamic link for a certain amount of time
956 it is automatically deleted.
957 Timeout rules also apply to TCP connections which do not open or close
959 .Ss PARTIALLY SPECIFIED ALIASING LINKS
960 Aliasing links can be partially specified, meaning that the remote address
961 and/or remote port are unknown.
962 In this case, when a packet matching the incomplete specification is found,
963 a fully specified dynamic link is created.
964 If the original partially specified link is dynamic, it will be deleted
965 after the fully specified link is created, otherwise it will persist.
967 For instance, a partially specified link might be
968 .Bd -literal -offset indent
969 (192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
972 The zeros denote unspecified components for the remote address and port.
973 If this link were static it would have the effect of redirecting all
974 incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
975 of machine 192.168.0.4 on the local network.
976 Each individual telnet connection would initiate the creation of a distinct
978 .Ss DYNAMIC LINK CREATION
979 In addition to aliasing links, there are also address mappings that can be
980 stored within the internal data table of the packet aliasing mechanism.
981 .Bd -literal -offset indent
982 (local addr, alias addr)
985 Address mappings are searched when creating new dynamic links.
987 All outgoing packets from the local network automatically create a dynamic
988 link if they do not match an already existing fully specified link.
989 If an address mapping exists for the outgoing packet, this determines
990 the alias address to be used.
991 If no mapping exists, then a default address, usually the address of the
992 packet aliasing host, is used.
993 If necessary, this default address can be changed as often as each individual
996 The aliasing port number is determined such that the new dynamic link does
997 not conflict with any existing links.
998 In the default operating mode, the packet aliasing engine attempts to set
999 the aliasing port equal to the local port number.
1000 If this results in a conflict, then port numbers are randomly chosen until
1001 a unique aliasing link can be established.
1002 In an alternate operating mode, the first choice of an aliasing port is also
1003 random and unrelated to the local port number.
1004 .Sh MODULAR ARCHITECTURE (AND Xr ipfw 4 Sh SUPPORT)
1005 One of the latest improvements to
1007 was to make its support
1008 for new protocols independent from the rest of the library, giving it
1009 the ability to load/unload support for new protocols at run-time.
1010 To achieve this feature, all the code for protocol handling was moved
1011 to a series of modules outside of the main library.
1012 These modules are compiled from the same sources but work in
1013 different ways, depending on whether they are compiled to work inside a kernel
1014 or as part of the userland library.
1015 .Ss LIBALIAS MODULES IN KERNEL LAND
1016 When compiled for the kernel,
1018 modules are plain KLDs recognizable with the
1022 To add support for a new protocol, load the corresponding module.
1025 .Dl "kldload alias_ftp"
1027 When support for a protocol is no longer needed, its module can be unloaded:
1029 .Dl "kldunload alias_ftp"
1030 .Ss LIBALIAS MODULES IN USERLAND
1031 Due to the differences between kernel and userland (no KLD mechanism,
1032 many different address spaces, etc.), we had to change a bit how to
1033 handle module loading/tracking/unloading in userland.
1035 While compiled for a userland
1037 all the modules are plain libraries, residing in
1039 and recognizable with the
1043 There is a configuration file,
1044 .Pa /etc/libalias.conf ,
1045 with the following contents (by default):
1046 .Bd -literal -offset indent
1047 /usr/lib/libalias_cuseeme.so
1048 /usr/lib/libalias_ftp.so
1049 /usr/lib/libalias_irc.so
1050 /usr/lib/libalias_nbt.so
1051 /usr/lib/libalias_pptp.so
1052 /usr/lib/libalias_skinny.so
1053 /usr/lib/libalias_smedia.so
1056 This file contains the paths to the modules that
1059 To load/unload a new module, just add its path to
1062 .Fn LibAliasRefreshModules
1064 In case the application provides a
1066 signal handler, add a call to
1067 .Fn LibAliasRefreshModules
1068 inside the handler, and everytime you want to refresh the loaded modules,
1073 .Dl "kill -HUP <process_pid>"
1074 .Ss MODULAR ARCHITECURE: HOW IT WORKS
1075 The modular architecture of
1077 works similar whether it is running inside the
1078 kernel or in userland.
1082 /* Protocol and userland module handlers chains. */
1083 LIST_HEAD(handler_chain, proto_handler) handler_chain ...
1085 SLIST_HEAD(dll_chain, dll) dll_chain ...
1089 keeps track of all the protocol handlers loaded, while
1091 tracks which userland modules are loaded.
1095 .Vt "struct proto_handler"
1098 struct proto_handler {
1102 int (*fingerprint)(struct libalias *la,
1103 struct ip *pip, struct alias_data *ah);
1104 int (*protohandler)(struct libalias *la,
1105 struct ip *pip, struct alias_data *ah);
1106 TAILQ_ENTRY(proto_handler) link;
1113 is the priority assigned to a protocol handler; lower priority
1116 is the direction of packets: ingoing or outgoing.
1118 indicates to which protocol this packet belongs: IP, TCP or UDP.
1120 points to the fingerprint function while protohandler points
1121 to the protocol handler function.
1126 function has the dual role of checking if the
1127 incoming packet is found, and if it belongs to any categories that this
1132 function actually manipulates
1137 When a packet enters
1139 if it meets a module hook,
1141 is searched to see if there is an handler that matches
1142 this type of a packet (it checks protocol and direction of packet).
1143 Then, if more than one handler is found, it starts with the module with
1144 the lowest priority number: it calls the
1146 function and interprets the result.
1148 If the result value is equal to 0 then it calls the protocol handler
1149 of this handler and returns.
1150 Otherwise, it proceeds to the next eligible module until the
1156 the module hook looks like this:
1157 .Bd -literal -offset indent
1158 struct alias_data ad = {
1163 &ud->uh_sport, /* original source port */
1164 &ud->uh_dport, /* original dest port */
1165 256 /* maxpacketsize */
1170 /* walk out chain */
1171 err = find_handler(IN, UDP, la, pip, &ad);
1174 All data useful to a module are gathered together in an
1181 function is responsible for walking the handler
1182 chain; it receives as input parameters:
1183 .Bl -tag -width indent
1189 pointer to this instance of libalias
1195 .Vt "struct alias_data"
1201 will search only for modules registered for
1202 supporting INcoming UDP packets.
1204 As was mentioned earlier,
1206 in userland is a bit different, as
1207 care must be taken in module handling as well (avoiding duplicate load of
1208 modules, avoiding modules with same name, etc.) so
1213 contains a list of all userland
1217 When an application calls
1218 .Fn LibAliasRefreshModules ,
1220 first unloads all the loaded modules, then reloads all the modules listed in
1221 .Pa /etc/libalias.conf :
1222 for every module loaded, a new entry is added to
1231 /* name of module */
1234 * ptr to shared obj obtained through
1235 * dlopen() - use this ptr to get access
1236 * to any symbols from a loaded module
1245 is the name of the module.
1247 is a pointer to the module obtained through
1250 Whenever a module is loaded in userland, an entry is added to
1252 then every protocol handler present in that module
1253 is resolved and registered in
1255 .Ss HOW TO WRITE A MODULE FOR LIBALIAS
1256 There is a module (called
1257 .Pa alias_dummy.[ch] )
1260 that can be used as a skeleton for future work.
1261 Here we analyse some parts of that module.
1265 struct proto_handler handlers[] = {
1270 .fingerprint = fingerprint,
1271 .protohandler= protohandler,
1280 .Dq "most important thing"
1282 since it describes the handlers present and lets the outside world use
1283 it in an opaque way.
1285 It must ALWAYS be present in every module, and it MUST retain
1288 otherwise attempting to load a module in userland will fail and
1289 complain about missing symbols: for more information about module
1290 load/unload, please refer to
1291 .Fn LibAliasRefreshModules ,
1292 .Fn LibAliasLoadModule
1294 .Fn LibAliasUnloadModule
1301 structures present in a module.
1304 mod_handler(module_t mod, int type, void *data)
1310 error = LibAliasAttachHandlers(handlers);
1313 error = LibAliasDetachHandlers(handlers);
1321 When running as KLD,
1323 registers/deregisters the module using
1324 .Fn LibAliasAttachHandlers
1326 .Fn LibAliasDetachHandlers ,
1329 Every module must contain at least 2 functions: one fingerprint
1330 function and a protocol handler function.
1336 fingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
1346 protohandler(struct libalias *la, struct ip *pip,
1347 struct alias_data *ah)
1353 and they must accept exactly these input parameters.
1354 .Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES
1355 To add module support into an application that uses
1357 the following simple steps can be followed.
1360 Find the main file of an application
1364 Add this to the header section of
1366 if not already present:
1368 .Dl "#include <signal.h>"
1370 and this just after the header section:
1372 .Dl "static void signal_handler(int);"
1374 Add the following line to the init function of an application or,
1375 if it does not have any init function, put it in
1378 .Dl "signal(SIGHUP, signal_handler);"
1382 function somewhere in
1384 .Bd -literal -offset indent
1386 signal_handler(int sig)
1389 LibAliasRefreshModules();
1393 Otherwise, if an application already traps the
1395 signal, just add a call to
1396 .Fn LibAliasRefreshModules
1397 in the signal handler function.
1399 For example, to patch
1403 modules, just add the following line to
1404 .Fn RefreshAddr "int sig __unused" :
1406 .Dl "LibAliasRefreshModules()"
1408 recompile and you are done.
1409 .Ss LOGGING SUPPORT IN KERNEL LAND
1410 When working as KLD,
1412 now has log support that
1413 happens on a buffer allocated inside
1414 .Vt "struct libalias"
1416 .Pa alias_local.h ) :
1421 /* log descriptor */
1424 * ptr to an auto-malloced
1425 * memory buffer when libalias
1430 * ptr to /var/log/alias.log
1431 * when libalias runs as a
1439 so all applications using
1441 will be able to handle their
1442 own logs, if they want, accessing
1444 Moreover, every change to a log buffer is automatically added to
1452 .An Charles Mott Aq cm@linktel.net ,
1453 versions 1.0 - 1.8, 2.0 - 2.4.
1454 .An Eivind Eklund Aq eivind@FreeBSD.org ,
1455 versions 1.8b, 1.9 and 2.5.
1456 Added IRC DCC support as well as contributing a number of architectural
1457 improvements; added the firewall bypass for FTP/IRC DCC.
1458 .An Erik Salander Aq erik@whistle.com
1459 added support for PPTP and RTSP.
1460 .An Junichi Satoh Aq junichi@junichi.org
1461 added support for RTSP/PNA.
1462 .An Ruslan Ermilov Aq ru@FreeBSD.org
1463 added support for PPTP and LSNAT as well as general hacking.
1464 .An Gleb Smirnoff Aq glebius@FreeBSD.org
1465 ported the library to kernel space.
1466 .An Paolo Pisati Aq piso@FreeBSD.org
1467 made the library modular, moving support for all
1468 protocols (except for IP, TCP and UDP) to external modules.
1469 .Sh ACKNOWLEDGEMENTS
1470 Listed below, in approximate chronological order, are individuals who
1471 have provided valuable comments and/or debugging assistance.
1472 .Bd -ragged -offset indent
1483 .An Andrzej Bialecki