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