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