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
26 .\" $FreeBSD: src/sys/netinet/libalias/libalias.3,v 1.58.6.1 2008/11/25 02:59:29 kensmith Exp $
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 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
65 The packet aliasing engine was designed to operate in user space outside
66 of the kernel, without any access to private kernel data structure, but
67 the source code can also be ported to a kernel environment.
68 .Sh INITIALIZATION AND CONTROL
71 must always be called before any packet handling may be performed and
72 the returned instance pointer passed to all the other functions.
74 .Fn LibAliasSetAddress
75 function is called afterwards, to set the default aliasing address.
76 In addition, the operating mode of the packet aliasing engine can be
80 .Ft "struct libalias *"
81 .Fn LibAliasInit "struct libalias *"
82 .Bd -ragged -offset indent
83 This function is used to initialize
84 internal data structures.
85 When called the first time, a
87 pointer should be passed as an argument.
88 The following mode bits are always set after calling
90 See the description of
92 below for the meaning of these mode bits.
94 .Bl -item -offset indent -compact
96 .Dv PKT_ALIAS_SAME_PORTS
98 .Dv PKT_ALIAS_USE_SOCKETS
100 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
103 This function will always return the packet aliasing engine to the same
106 .Fn LibAliasSetAddress
107 function is normally called afterwards, and any desired changes from the
108 default mode bits listed above require a call to
109 .Fn LibAliasSetMode .
111 It is mandatory that this function be called at the beginning of a program
112 prior to any packet handling.
116 .Fn LibAliasUninit "struct libalias *"
117 .Bd -ragged -offset indent
118 This function has no return value and is used to clear any
119 resources attached to internal data structures.
121 This functions should be called when a program stops using the aliasing
122 engine; it does, amongst other things, clear out any firewall holes.
123 To provide backwards compatibility and extra security, it is added to
131 .Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr"
132 .Bd -ragged -offset indent
133 This function sets the source address to which outgoing packets from the
134 local area network are aliased.
135 All outgoing packets are re-mapped to this address unless overridden by a
136 static address mapping established by
137 .Fn LibAliasRedirectAddr .
138 If this function is not called, and no static rules match, an outgoing
139 packet retains its source address.
142 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
143 mode bit is set (the default mode of operation), then the internal aliasing
144 link tables will be reset any time the aliasing address changes.
145 This is useful for interfaces such as
148 address may or may not change on successive dial-up attempts.
151 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
152 mode bit is set to zero, this function can also be used to dynamically change
153 the aliasing address on a packet to packet basis (it is a low overhead call).
155 It is mandatory that this function be called prior to any packet handling.
159 .Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask"
160 .Bd -ragged -offset indent
161 This function sets or clears mode bits
162 according to the value of
167 The following mode bits are defined in
169 .Bl -tag -width indent
172 .Pa /var/log/alias.log .
173 Each time an aliasing link is created or deleted, the log file is appended
174 with the current number of ICMP, TCP and UDP links.
175 Mainly useful for debugging when the log file is viewed continuously with
177 .It Dv PKT_ALIAS_DENY_INCOMING
178 If this mode bit is set, all incoming packets associated with new TCP
179 connections or new UDP transactions will be marked for being ignored
182 .Dv PKT_ALIAS_IGNORED
184 by the calling program.
185 Response packets to connections or transactions initiated from the packet
186 aliasing host or local network will be unaffected.
187 This mode bit is useful for implementing a one-way firewall.
188 .It Dv PKT_ALIAS_SAME_PORTS
189 If this mode bit is set, the packet aliasing engine will attempt to leave
190 the alias port numbers unchanged from the actual local port numbers.
191 This can be done as long as the quintuple (proto, alias addr, alias port,
192 remote addr, remote port) is unique.
193 If a conflict exists, a new aliasing port number is chosen even if this
195 .It Dv PKT_ALIAS_USE_SOCKETS
196 This bit should be set when the packet aliasing host originates network
197 traffic as well as forwards it.
198 When the packet aliasing host is waiting for a connection from an unknown
199 host address or unknown port number (e.g.\& an FTP data connection), this
200 mode bit specifies that a socket be allocated as a place holder to prevent
202 Once a connection is established, usually within a minute or so, the socket
204 .It Dv PKT_ALIAS_UNREGISTERED_ONLY
205 If this mode bit is set, traffic on the local network which does not
206 originate from unregistered address spaces will be ignored.
207 Standard Class A, B and C unregistered addresses are:
208 .Bd -literal -offset indent
209 10.0.0.0 -> 10.255.255.255 (Class A subnet)
210 172.16.0.0 -> 172.31.255.255 (Class B subnets)
211 192.168.0.0 -> 192.168.255.255 (Class C subnets)
214 This option is useful in the case that packet aliasing host has both
215 registered and unregistered subnets on different interfaces.
216 The registered subnet is fully accessible to the outside world, so traffic
217 from it does not need to be passed through the packet aliasing engine.
218 .It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
219 When this mode bit is set and
220 .Fn LibAliasSetAddress
221 is called to change the aliasing address, the internal link table of the
222 packet aliasing engine will be cleared.
223 This operating mode is useful for
225 links where the interface address can sometimes change or remain the same
226 between dial-up attempts.
227 If this mode bit is not set, the link table will never be reset in the event
228 of an address change.
229 .It Dv PKT_ALIAS_PUNCH_FW
234 based firewall for FTP/IRC DCC connections.
235 The holes punched are bound by from/to IP address and port; it will not be
236 possible to use a hole for another connection.
237 A hole is removed when the connection that uses it dies.
238 To cater to unexpected death of a program using
241 changing the state of the flag will clear the entire firewall range
243 This will also happen on the initial call to
244 .Fn LibAliasSetFWBase .
245 This call must happen prior to setting this flag.
246 .It Dv PKT_ALIAS_REVERSE
249 reverse the way it handles incoming and outgoing packets, allowing it
250 to be fed with data that passes through the internal interface rather
251 than the external one.
252 .It Dv PKT_ALIAS_PROXY_ONLY
255 to obey transparent proxy rules only.
256 Normal packet aliasing is not performed.
258 .Fn LibAliasProxyRule
264 .Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num"
265 .Bd -ragged -offset indent
266 Set firewall range allocated for punching firewall holes (with the
267 .Dv PKT_ALIAS_PUNCH_FW
269 The range will be cleared for all rules on initialization.
273 .Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port"
274 .Bd -ragged -offset indent
275 Set the TCP port used by the Skinny Station protocol.
276 Skinny is used by Cisco IP phones to communicate with
277 Cisco Call Managers to set up voice over IP calls.
278 If this is not set, Skinny aliasing will not be done.
279 The typical port used by Skinny is 2000.
282 The packet handling functions are used to modify incoming (remote to local)
283 and outgoing (local to remote) packets.
284 The calling program is responsible for receiving and sending packets via
290 .Fn LibAliasSetAddress ,
291 the two packet handling functions,
295 comprise minimal set of functions needed for a basic IP masquerading
299 .Fn LibAliasIn "struct libalias *" "char *buffer" "int maxpacketsize"
300 .Bd -ragged -offset indent
301 An incoming packet coming from a remote machine to the local network is
302 de-aliased by this function.
303 The IP packet is pointed to by
307 indicates the size of the data structure containing the packet and should
308 be at least as large as the actual packet size.
311 .Bl -tag -width indent
313 The packet aliasing process was successful.
314 .It Dv PKT_ALIAS_IGNORED
315 The packet was ignored and not de-aliased.
316 This can happen if the protocol is unrecognized, possibly an ICMP message
317 type is not handled or if incoming packets for new connections are being
319 .Dv PKT_ALIAS_DENY_INCOMING
321 .Fn LibAliasSetMode ) .
322 .It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
323 This is returned when a fragment cannot be resolved because the header
324 fragment has not been sent yet.
325 In this situation, fragments must be saved with
326 .Fn LibAliasSaveFragment
327 until a header fragment is found.
328 .It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
329 The packet aliasing process was successful, and a header fragment was found.
330 This is a signal to retrieve any unresolved fragments with
331 .Fn LibAliasGetFragment
332 and de-alias them with
333 .Fn LibAliasFragmentIn .
334 .It Dv PKT_ALIAS_ERROR
335 An internal error within the packet aliasing engine occurred.
340 .Fn LibAliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
341 .Bd -ragged -offset indent
342 An outgoing packet coming from the local network to a remote machine is
343 aliased by this function.
344 The IP packet is pointed to by
348 indicates the maximum packet size permissible should the packet length be
350 IP encoding protocols place address and port information in the encapsulated
351 data stream which has to be modified and can account for changes in packet
353 Well known examples of such protocols are FTP and IRC DCC.
356 .Bl -tag -width indent
358 The packet aliasing process was successful.
359 .It Dv PKT_ALIAS_IGNORED
360 The packet was ignored and not aliased.
361 This can happen if the protocol is unrecognized, or possibly an ICMP message
363 .It Dv PKT_ALIAS_ERROR
364 An internal error within the packet aliasing engine occurred.
367 .Sh PORT AND ADDRESS REDIRECTION
368 The functions described in this section allow machines on the local network
369 to be accessible in some degree to new incoming connections from the external
371 Individual ports can be re-mapped or static network address translations can
374 .Ft struct alias_link *
375 .Fo LibAliasRedirectPort
376 .Fa "struct libalias *"
377 .Fa "struct in_addr local_addr"
378 .Fa "u_short local_port"
379 .Fa "struct in_addr remote_addr"
380 .Fa "u_short remote_port"
381 .Fa "struct in_addr alias_addr"
382 .Fa "u_short alias_port"
385 .Bd -ragged -offset indent
386 This function specifies that traffic from a given remote address/port to
387 an alias address/port be redirected to a specified local address/port.
401 is zero, this indicates that the packet aliasing address as established
403 .Fn LibAliasSetAddress
406 .Fn LibAliasSetAddress
407 is called to change the address after
408 .Fn LibAliasRedirectPort
409 is called, a zero reference will track this change.
411 If the link is further set up to operate for a load sharing, then
415 are ignored, and are selected dynamically from the server pool, as described in
416 .Fn LibAliasAddServer
421 is zero, this indicates to redirect packets from any remote address.
424 is zero, this indicates to redirect packets originating from any remote
426 Almost always, the remote port specification will be zero, but non-zero
427 remote addresses can sometimes be useful for firewalling.
429 .Fn LibAliasRedirectPort
430 overlap in their address/port specifications, then the most recent call
431 will have precedence.
433 This function returns a pointer which can subsequently be used by
434 .Fn LibAliasRedirectDelete .
437 is returned, then the function call did not complete successfully.
439 All port numbers should be in network address byte order, so it is necessary
442 to convert these parameters from internally readable numbers to network byte
444 Addresses are also in network byte order, which is implicit in the use of the
449 .Ft struct alias_link *
450 .Fo LibAliasRedirectAddr
451 .Fa "struct libalias *"
452 .Fa "struct in_addr local_addr"
453 .Fa "struct in_addr alias_addr"
455 .Bd -ragged -offset indent
456 This function designates that all incoming traffic to
460 Similarly, all outgoing traffic from
469 is zero, this indicates that the packet aliasing address as established by
470 .Fn LibAliasSetAddress
473 .Fn LibAliasSetAddress
474 is called to change the address after
475 .Fn LibAliasRedirectAddr
476 is called, a zero reference will track this change.
478 If the link is further set up to operate for a load sharing, then
480 is ignored, and is selected dynamically from the server pool, as described in
481 .Fn LibAliasAddServer
484 If subsequent calls to
485 .Fn LibAliasRedirectAddr
486 use the same aliasing address, all new incoming traffic to this aliasing
487 address will be redirected to the local address made in the last function
489 New traffic generated by any of the local machines, designated in the
490 several function calls, will be aliased to the same address.
491 Consider the following example:
492 .Bd -literal -offset indent
493 LibAliasRedirectAddr(la, inet_aton("192.168.0.2"),
494 inet_aton("141.221.254.101"));
495 LibAliasRedirectAddr(la, inet_aton("192.168.0.3"),
496 inet_aton("141.221.254.101"));
497 LibAliasRedirectAddr(la, inet_aton("192.168.0.4"),
498 inet_aton("141.221.254.101"));
501 Any outgoing connections such as
505 from 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
507 Any incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
510 .Fn LibAliasRedirectPort
511 will have precedence over address mappings designated by
512 .Fn LibAliasRedirectAddr .
514 This function returns a pointer which can subsequently be used by
515 .Fn LibAliasRedirectDelete .
518 is returned, then the function call did not complete successfully.
522 .Fo LibAliasAddServer
523 .Fa "struct libalias *"
524 .Fa "struct alias_link *link"
525 .Fa "struct in_addr addr"
528 .Bd -ragged -offset indent
529 This function sets the
531 up for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
532 LSNAT operates as follows.
533 A client attempts to access a server by using the server virtual address.
534 The LSNAT router transparently redirects the request to one of the hosts
535 in server pool, selected using a real-time load sharing algorithm.
536 Multiple sessions may be initiated from the same client, and each session
537 could be directed to a different host based on load balance across server
538 pool hosts at the time.
539 If load share is desired for just a few specific services, the configuration
540 on LSNAT could be defined to restrict load share for just the services
543 Currently, only the simplest selection algorithm is implemented, where a
544 host is selected on a round-robin basis only, without regard to load on
550 .Fn LibAliasRedirectPort
552 .Fn LibAliasRedirectAddr .
554 .Fn LibAliasAddServer
555 is called multiple times to add entries to the
559 For links created with
560 .Fn LibAliasRedirectAddr ,
563 argument is ignored and could have any value, e.g.\& htons(~0).
565 This function returns 0 on success, \-1 otherwise.
569 .Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link"
570 .Bd -ragged -offset indent
571 This function marks the specified static redirect rule entered by
572 .Fn LibAliasRedirectPort
574 This can be used to e.g.\& dynamically redirect a single TCP connection,
575 after which the rule is removed.
576 Only fully specified links can be made dynamic.
578 .Sx STATIC AND DYNAMIC LINKS
580 .Sx PARTIALLY SPECIFIED ALIASING LINKS
581 sections below for a definition of static vs.\& dynamic,
582 and partially vs.\& fully specified links.)
584 This function returns 0 on success, \-1 otherwise.
588 .Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link"
589 .Bd -ragged -offset indent
590 This function will delete a specific static redirect rule entered by
591 .Fn LibAliasRedirectPort
593 .Fn LibAliasRedirectAddr .
596 is the pointer returned by either of the redirection functions.
597 If an invalid pointer is passed to
598 .Fn LibAliasRedirectDelete ,
599 then a program crash or unpredictable operation could result, so it is
600 necessary to be careful using this function.
604 .Fn LibAliasProxyRule "struct libalias *" "const char *cmd"
605 .Bd -ragged -offset indent
608 string consists of one or more pairs of words.
609 The first word in each pair is a token and the second is the value that
610 should be applied for that token.
611 Tokens and their argument types are as follows:
612 .Bl -tag -width indent
613 .It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
614 In order to support transparent proxying, it is necessary to somehow
615 pass the original address and port information into the new destination
619 is specified, the original destination address and port are passed
620 as an extra IP option.
622 .Cm encode_tcp_stream
623 is specified, the original destination address and port are passed
624 as the first piece of data in the TCP stream in the format
625 .Dq Li DEST Ar IP port .
626 .It Cm port Ar portnum
627 Only packets with the destination port
630 .It Cm server Ar host Ns Op : Ns Ar portnum
635 that the data is to be redirected to.
637 must be an IP address rather than a DNS host name.
640 is not specified, the destination port number is not changed.
644 specification is mandatory unless the
646 command is being used.
648 Normally, each call to
649 .Fn LibAliasProxyRule
650 inserts the next rule at the start of a linear list of rules.
653 is specified, the new rule will be checked after all rules with lower
656 .Fn LibAliasProxyRule
657 that do not specify a rule are assigned rule 0.
658 .It Cm delete Ar index
659 This token and its argument MUST NOT be used with any other tokens.
660 When used, all existing rules with the given
663 .It Cm proto tcp | udp
664 If specified, only packets of the given protocol type are matched.
665 .It Cm src Ar IP Ns Op / Ns Ar bits
666 If specified, only packets with a source address matching the given
671 is also specified, then the first
675 are taken as a network specification, and all IP addresses from that
676 network will be matched.
677 .It Cm dst Ar IP Ns Op / Ns Ar bits
678 If specified, only packets with a destination address matching the given
683 is also specified, then the first
687 are taken as a network specification, and all IP addresses from that
688 network will be matched.
691 This function is usually used to redirect outgoing connections for
692 internal machines that are not permitted certain types of internet
693 access, or to restrict access to certain external machines.
696 .Ft struct alias_link *
697 .Fo LibAliasRedirectProto
698 .Fa "struct libalias *"
699 .Fa "struct in_addr local_addr"
700 .Fa "struct in_addr remote_addr"
701 .Fa "struct in_addr alias_addr"
704 .Bd -ragged -offset indent
705 This function specifies that any IP packet with protocol number of
707 from a given remote address to an alias address be
708 redirected to a specified local address.
714 is zero, this indicates that the packet aliasing address as established
716 .Fn LibAliasSetAddress
719 .Fn LibAliasSetAddress
720 is called to change the address after
721 .Fn LibAliasRedirectProto
722 is called, a zero reference will track this change.
726 is zero, this indicates to redirect packets from any remote address.
727 Non-zero remote addresses can sometimes be useful for firewalling.
730 .Fn LibAliasRedirectProto
731 overlap in their address specifications, then the most recent call
732 will have precedence.
734 This function returns a pointer which can subsequently be used by
735 .Fn LibAliasRedirectDelete .
738 is returned, then the function call did not complete successfully.
740 .Sh FRAGMENT HANDLING
741 The functions in this section are used to deal with incoming fragments.
743 Outgoing fragments are handled within
745 by changing the address according to any applicable mapping set by
746 .Fn LibAliasRedirectAddr ,
747 or the default aliasing address set by
748 .Fn LibAliasSetAddress .
750 Incoming fragments are handled in one of two ways.
751 If the header of a fragmented IP packet has already been seen, then all
752 subsequent fragments will be re-mapped in the same manner the header
754 Fragments which arrive before the header are saved and then retrieved
755 once the header fragment has been resolved.
758 .Fn LibAliasSaveFragment "struct libalias *" "char *ptr"
759 .Bd -ragged -offset indent
763 .Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
764 this function can be used to save the pointer to the unresolved fragment.
766 It is implicitly assumed that
768 points to a block of memory allocated by
770 If the fragment is never resolved, the packet aliasing engine will
771 automatically free the memory after a timeout period.
772 [Eventually this function should be modified so that a callback function
773 for freeing memory is passed as an argument.]
775 This function returns
777 if it was successful and
779 if there was an error.
783 .Fn LibAliasGetFragment "struct libalias *" "char *buffer"
784 .Bd -ragged -offset indent
785 This function can be used to retrieve fragment pointers saved by
786 .Fn LibAliasSaveFragment .
787 The IP header fragment pointed to by
789 is the header fragment indicated when
792 .Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
793 Once a fragment pointer is retrieved, it becomes the calling program's
794 responsibility to free the dynamically allocated memory for the fragment.
797 .Fn LibAliasGetFragment
798 function can be called sequentially until there are no more fragments
799 available, at which time it returns
804 .Fn LibAliasFragmentIn "struct libalias *" "char *header" "char *fragment"
805 .Bd -ragged -offset indent
806 When a fragment is retrieved with
807 .Fn LibAliasGetFragment ,
808 it can then be de-aliased with a call to
809 .Fn LibAliasFragmentIn .
812 argument is the pointer to a header fragment used as a template, and
814 is the pointer to the packet to be de-aliased.
816 .Sh MISCELLANEOUS FUNCTIONS
818 .Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr"
819 .Bd -ragged -offset indent
820 When an incoming packet not associated with any pre-existing aliasing link
821 arrives at the host machine, it will be sent to the address indicated by a
823 .Fn LibAliasSetTarget .
825 If this function is called with an
827 address argument, then all new incoming packets go to the address set by
828 .Fn LibAliasSetAddress .
830 If this function is not called, or is called with an
832 address argument, then all new incoming packets go to the address specified
834 This allows external machines to talk directly to internal machines if they
835 can route packets to the machine in question.
839 .Fn LibAliasCheckNewLink "struct libalias *"
840 .Bd -ragged -offset indent
841 This function returns a non-zero value when a new aliasing link is created.
842 In circumstances where incoming traffic is being sequentially sent to
843 different local servers, this function can be used to trigger when
844 .Fn LibAliasSetTarget
845 is called to change the default target address.
849 .Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes"
850 .Bd -ragged -offset indent
851 This is a utility function that does not seem to be available elsewhere and
852 is included as a convenience.
853 It computes the internet checksum, which is used in both IP and
854 protocol-specific headers (TCP, UDP, ICMP).
858 argument points to the data block to be checksummed, and
860 is the number of bytes.
861 The 16-bit checksum field should be zeroed before computing the checksum.
863 Checksums can also be verified by operating on a block of data including
865 If the checksum is valid,
866 .Fn LibAliasInternetChecksum
871 .Fn LibAliasUnaliasOut "struct libalias *" "char *buffer" "int maxpacketsize"
872 .Bd -ragged -offset indent
873 An outgoing packet, which has already been aliased,
874 has its private address/port information restored by this function.
875 The IP packet is pointed to by
879 is provided for error checking purposes.
880 This function can be used if an already-aliased packet needs to have its
881 original IP header restored for further processing (e.g.\& logging).
884 .An Charles Mott Aq cm@linktel.net ,
885 versions 1.0 - 1.8, 2.0 - 2.4.
886 .An Eivind Eklund Aq eivind@FreeBSD.org ,
887 versions 1.8b, 1.9 and 2.5.
888 Added IRC DCC support as well as contributing a number of architectural
889 improvements; added the firewall bypass for FTP/IRC DCC.
890 .An Erik Salander Aq erik@whistle.com
891 added support for PPTP and RTSP.
892 .An Junichi Satoh Aq junichi@junichi.org
893 added support for RTSP/PNA.
894 .An Ruslan Ermilov Aq ru@FreeBSD.org
895 added support for PPTP and LSNAT as well as general hacking.
896 .An Paolo Pisati Aq piso@FreeBSD.org
897 made the library modular, moving support for all
898 protocols (except for IP, TCP and UDP) to external modules.
900 Listed below, in approximate chronological order, are individuals who
901 have provided valuable comments and/or debugging assistance.
903 .Bd -ragged -offset indent
917 .Sh CONCEPTUAL BACKGROUND
918 This section is intended for those who are planning to modify the source
919 code or want to create somewhat esoteric applications using the packet
922 The conceptual framework under which the packet aliasing engine operates
924 Central to the discussion is the idea of an
926 which describes the relationship for a given packet transaction between
927 the local machine, aliased identity and remote machine.
928 It is discussed how such links come into existence and are destroyed.
930 There is a notion of an
932 which is a 7-tuple describing a specific translation:
933 .Bd -literal -offset indent
934 (local addr, local port, alias addr, alias port,
935 remote addr, remote port, protocol)
938 Outgoing packets have the local address and port number replaced with the
939 alias address and port number.
940 Incoming packets undergo the reverse process.
941 The packet aliasing engine attempts to match packets against an internal
942 table of aliasing links to determine how to modify a given IP packet.
943 Both the IP header and protocol dependent headers are modified as necessary.
944 Aliasing links are created and deleted as necessary according to network
947 Protocols can be TCP, UDP or even ICMP in certain circumstances.
948 (Some types of ICMP packets can be aliased according to sequence or ID
949 number which acts as an equivalent port number for identifying how
950 individual packets should be handled.)
952 Each aliasing link must have a unique combination of the following five
953 quantities: alias address/port, remote address/port and protocol.
954 This ensures that several machines on a local network can share the
955 same aliasing IP address.
956 In cases where conflicts might arise, the aliasing port is chosen so that
957 uniqueness is maintained.
958 .Ss STATIC AND DYNAMIC LINKS
959 Aliasing links can either be static or dynamic.
960 Static links persist indefinitely and represent fixed rules for translating
962 Dynamic links come into existence for a specific TCP connection or UDP
963 transaction or ICMP ECHO sequence.
964 For the case of TCP, the connection can be monitored to see when the
965 associated aliasing link should be deleted.
966 Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
967 work on a simple timeout rule.
968 When no activity is observed on a dynamic link for a certain amount of time
969 it is automatically deleted.
970 Timeout rules also apply to TCP connections which do not open or close
972 .Ss PARTIALLY SPECIFIED ALIASING LINKS
973 Aliasing links can be partially specified, meaning that the remote address
974 and/or remote port are unknown.
975 In this case, when a packet matching the incomplete specification is found,
976 a fully specified dynamic link is created.
977 If the original partially specified link is dynamic, it will be deleted
978 after the fully specified link is created, otherwise it will persist.
980 For instance, a partially specified link might be
981 .Bd -literal -offset indent
982 (192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
985 The zeros denote unspecified components for the remote address and port.
986 If this link were static it would have the effect of redirecting all
987 incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
988 of machine 192.168.0.4 on the local network.
989 Each individual telnet connection would initiate the creation of a distinct
991 .Ss DYNAMIC LINK CREATION
992 In addition to aliasing links, there are also address mappings that can be
993 stored within the internal data table of the packet aliasing mechanism.
994 .Bd -literal -offset indent
995 (local addr, alias addr)
998 Address mappings are searched when creating new dynamic links.
1000 All outgoing packets from the local network automatically create a dynamic
1001 link if they do not match an already existing fully specified link.
1002 If an address mapping exists for the outgoing packet, this determines
1003 the alias address to be used.
1004 If no mapping exists, then a default address, usually the address of the
1005 packet aliasing host, is used.
1006 If necessary, this default address can be changed as often as each individual
1009 The aliasing port number is determined such that the new dynamic link does
1010 not conflict with any existing links.
1011 In the default operating mode, the packet aliasing engine attempts to set
1012 the aliasing port equal to the local port number.
1013 If this results in a conflict, then port numbers are randomly chosen until
1014 a unique aliasing link can be established.
1015 In an alternate operating mode, the first choice of an aliasing port is also
1016 random and unrelated to the local port number.
1017 .Sh MODULAR ARCHITECTURE (AND Xr ipfw 4 Sh SUPPORT)
1018 One of the latest improvements to
1020 was to make its support
1021 for new protocols independent from the rest of the library, giving it
1022 the ability to load/unload support for new protocols at run-time.
1023 To achieve this feature, all the code for protocol handling was moved
1024 to a series of modules outside of the main library.
1025 These modules are compiled from the same sources but work in
1026 different ways, depending on whether they are compiled to work inside a kernel
1027 or as part of the userland library.
1028 .Ss LIBALIAS MODULES IN KERNEL LAND
1029 When compiled for the kernel,
1031 modules are plain KLDs recognizable with the
1035 To add support for a new protocol, load the corresponding module.
1038 .Dl "kldload alias_ftp"
1040 When support for a protocol is no longer needed, its module can be unloaded:
1042 .Dl "kldunload alias_ftp"
1043 .Ss LIBALIAS MODULES IN USERLAND
1044 Due to the differences between kernel and userland (no KLD mechanism,
1045 many different address spaces, etc.), we had to change a bit how to
1046 handle module loading/tracking/unloading in userland.
1048 While compiled for a userland
1050 all the modules are plain libraries, residing in
1052 and recognizable with the
1056 There is a configuration file,
1057 .Pa /etc/libalias.conf ,
1058 with the following contents (by default):
1059 .Bd -literal -offset indent
1060 /usr/lib/libalias_cuseeme.so
1061 /usr/lib/libalias_ftp.so
1062 /usr/lib/libalias_irc.so
1063 /usr/lib/libalias_nbt.so
1064 /usr/lib/libalias_pptp.so
1065 /usr/lib/libalias_skinny.so
1066 /usr/lib/libalias_smedia.so
1069 This file contains the paths to the modules that
1072 To load/unload a new module, just add its path to
1075 .Fn LibAliasRefreshModules
1077 In case the application provides a
1079 signal handler, add a call to
1080 .Fn LibAliasRefreshModules
1081 inside the handler, and everytime you want to refresh the loaded modules,
1086 .Dl "kill -HUP <process_pid>"
1087 .Ss MODULAR ARCHITECURE: HOW IT WORKS
1088 The modular architecture of
1090 works similar whether it is running inside the
1091 kernel or in userland.
1095 /* Protocol and userland module handlers chains. */
1096 LIST_HEAD(handler_chain, proto_handler) handler_chain ...
1098 SLIST_HEAD(dll_chain, dll) dll_chain ...
1102 keep tracks of all the protocol handlers loaded, while
1104 takes care of userland modules loaded.
1108 .Vt "struct proto_handler"
1111 struct proto_handler {
1115 int (*fingerprint)(struct libalias *la,
1116 struct ip *pip, struct alias_data *ah);
1117 int (*protohandler)(struct libalias *la,
1118 struct ip *pip, struct alias_data *ah);
1119 LIST_ENTRY(proto_handler) entries;
1126 is the priority assigned to a protocol handler, lower
1129 is the direction of packets: ingoing or outgoing.
1131 says at which protocol this packet belongs: IP, TCP or UDP.
1133 points to the fingerprint function while protohandler points
1134 to the protocol handler function.
1139 function has the double of scope of checking if the
1140 incoming packet is found and if it belongs to any categories that this
1145 function actually manipulates
1150 When a packet enters
1152 if it meets a module hook,
1154 is searched to see if there is an handler that matches
1155 this type of a packet (it checks protocol and direction of packet), then if
1156 more than one handler is found, it starts with the module with
1157 the lowest priority number: it calls the
1159 function and interprets the result.
1161 If the result value is equal to 0 then it calls the protocol handler
1162 of this handler and returns.
1163 Otherwise, it proceeds to the next eligible module until the
1169 the module hook looks like this:
1170 .Bd -literal -offset indent
1171 struct alias_data ad = {
1176 &ud->uh_sport, /* original source port */
1177 &ud->uh_dport, /* original dest port */
1178 256 /* maxpacketsize */
1183 /* walk out chain */
1184 err = find_handler(IN, UDP, la, pip, &ad);
1187 All data useful to a module are gathered together in an
1194 function is responsible for walking out the handler
1195 chain, it receives as input parameters:
1196 .Bl -tag -width indent
1202 pointer to this instance of libalias
1208 .Vt "struct alias_data"
1214 will search only for modules registered for
1215 supporting INcoming UDP packets.
1217 As was mentioned earlier,
1219 in userland is a bit different, cause
1220 care has to be taken of module handling too (avoiding duplicate load of
1221 module, avoiding module with same name, etc.) so
1226 contains a list of all userland
1230 When an application calls
1231 .Fn LibAliasRefreshModules ,
1233 first unloads all the loaded modules, then reloads all the modules listed in
1234 .Pa /etc/libalias.conf :
1235 for every module loaded, a new entry to
1245 /* name of module */
1248 * ptr to shared obj obtained through
1249 * dlopen() - use this ptr to get access
1250 * to any symbols from a loaded module
1259 is the name of the module
1261 is a pointer to the module obtained through
1264 Whenever a module is loaded in userland, an entry is added to
1266 then every protocol handler present in that module
1267 is resolved and registered in
1269 .Ss HOW TO WRITE A MODULE FOR LIBALIAS
1270 There is a module (called
1271 .Pa alias_dummy.[ch] )
1274 that can be used as a skeleton for future work, here we analyse some parts of that
1279 struct proto_handler handlers [] = {{666, IN|OUT, UDP|TCP,
1280 &fingerprint, &protohandler}};
1286 .Dq "most important thing"
1288 cause it describes the handlers present and lets the outside world use
1289 it in an opaque way.
1291 It must ALWAYS be present in every module, and it MUST retain
1294 otherwise attempting to load a module in userland will fail and
1295 complain about missing symbols: for more information about module
1296 load/unload, please refer to
1297 .Fn LibAliasRefreshModules ,
1298 .Fn LibAliasLoadModule
1300 .Fn LibAliasUnloadModule
1307 structures present in a module.
1310 mod_handler(module_t mod, int type, void *data)
1317 attach_handlers(handlers);
1321 detach_handlers(handlers;
1329 When running as KLD,
1331 register/deregister the module using
1334 .Fn detach_handlers ,
1337 Every module must contain at least 2 functions: one fingerprint
1338 function and a protocol handler function.
1344 fingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
1354 protohandler(struct libalias *la, struct ip *pip,
1355 struct alias_data *ah)
1361 and they must accept exactly these input parameters.
1362 .Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES
1363 To add module support into an application that uses
1365 the following simple steps can be followed.
1368 Find the main file of an application
1372 Add this to the header section of
1374 if not already present:
1376 .Dl "#include <signal.h>"
1378 and this just after the header section:
1380 .Dl "static void signal_handler(int);"
1382 Add the following line to the init function of an application or,
1383 if it does not have any init function, put it in
1386 .Dl "signal(SIGHUP, signal_handler);"
1390 function somewhere in
1392 .Bd -literal -offset indent
1394 signal_handler(int sig)
1397 LibAliasRefreshModules();
1401 Otherwise, if an application already traps the
1403 signal, just add a call to
1404 .Fn LibAliasRefreshModules
1405 in the signal handler function.
1407 For example, to patch
1411 modules, just add the following line to
1412 .Fn RefreshAddr "int sig __unused" :
1414 .Dl "LibAliasRefreshModules()"
1416 recompile and you are done.
1417 .Ss LOGGING SUPPORT IN KERNEL LAND
1418 When working as KLD,
1420 now has log support that
1421 happens on a buffer allocated inside
1422 .Vt "struct libalias"
1424 .Pa alias_local.h ) :
1429 /* log descriptor */
1432 * ptr to an auto-malloced
1433 * memory buffer when libalias
1438 * ptr to /var/log/alias.log
1439 * when libalias runs as a
1447 so all applications using
1449 will be able to handle their
1450 own logs, if they want, accessing
1452 Moreover, every change to a log buffer is automatically added to