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: src/lib/libalias/libalias.3,v 1.23.2.11 2001/12/17 10:08:22 ru Exp $ 27.\" $DragonFly: src/lib/libalias/libalias.3,v 1.6 2008/05/02 02:05:03 swildner Exp $ 28.\" 29.Dd April 13, 2000 30.Dt LIBALIAS 3 31.Os 32.Sh NAME 33.Nm libalias 34.Nd packet aliasing library for masquerading and network address translation 35.Sh LIBRARY 36.Lb libalias 37.Sh SYNOPSIS 38.In sys/types.h 39.In netinet/in.h 40.In alias.h 41.Pp 42Function prototypes are given in the main body of the text. 43.Sh DESCRIPTION 44The 45.Nm 46library is a collection of functions for aliasing and de-aliasing of IP 47packets, intended for masquerading and network address translation (NAT). 48.Sh INTRODUCTION 49This library is a moderately portable set of functions designed to assist 50in the process of IP masquerading and network address translation. 51Outgoing packets from a local network with unregistered IP addresses can 52be aliased to appear as if they came from an accessible IP address. 53Incoming packets are then de-aliased so that they are sent to the correct 54machine on the local network. 55.Pp 56A certain amount of flexibility is built into the packet aliasing engine. 57In the simplest mode of operation, a many-to-one address mapping takes 58place between local network and the packet aliasing host. 59This is known as IP masquerading. 60In addition, one-to-one mappings between local and public addresses can 61also be implemented, which is known as static NAT. 62In between these extremes, different groups of private addresses can be 63linked to different public addresses, comprising several distinct 64many-to-one mappings. 65Also, a given public address and port can be statically redirected to a 66private address/port. 67.Pp 68The packet aliasing engine was designed to operate in user space outside 69of the kernel, without any access to private kernel data structure, but 70the source code can also be ported to a kernel environment. 71.Sh INITIALIZATION AND CONTROL 72Two special functions, 73.Fn PacketAliasInit 74and 75.Fn PacketAliasSetAddress , 76must always be called before any packet handling may be performed. 77In addition, the operating mode of the packet aliasing engine can be 78customized by calling 79.Fn PacketAliasSetMode . 80.Pp 81.Ft void 82.Fn PacketAliasInit void 83.Bd -ragged -offset indent 84This function has no arguments or return value and is used to initialize 85internal data structures. 86The following mode bits are always set after calling 87.Fn PacketAliasInit . 88See the description of 89.Fn PacketAliasSetMode 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. 103.Fn PacketAliasSetAddress 104must be called afterwards, and any desired changes from the default mode 105bits listed above require a call to 106.Fn PacketAliasSetMode . 107.Pp 108It is mandatory that this function be called at the beginning of a program 109prior to any packet handling. 110.Ed 111.Pp 112.Ft void 113.Fn PacketAliasUninit void 114.Bd -ragged -offset indent 115This function has no arguments or return value and is used to clear any 116resources attached to internal data structures. 117.Pp 118This functions should be called when a program stops using the aliasing 119engine; it does, amongst other things, clear out any firewall holes. 120To provide backwards compatibility and extra security, it is added to 121the 122.Xr atexit 3 123chain by 124.Fn PacketAliasInit . 125Calling it multiple times is harmless. 126.Ed 127.Pp 128.Ft void 129.Fn PacketAliasSetAddress "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 PacketAliasRedirectAddr . 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.In 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.In 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 void 552.Fn PacketAliasRedirectDelete "struct alias_link *link" 553.Bd -ragged -offset indent 554This function will delete a specific static redirect rule entered by 555.Fn PacketAliasRedirectPort 556or 557.Fn PacketAliasRedirectAddr . 558The parameter 559.Fa link 560is the pointer returned by either of the redirection functions. 561If an invalid pointer is passed to 562.Fn PacketAliasRedirectDelete , 563then a program crash or unpredictable operation could result, so it is 564necessary to be careful using this function. 565.Ed 566.Pp 567.Ft int 568.Fn PacketAliasProxyRule "const char *cmd" 569.Bd -ragged -offset indent 570The passed 571.Fa cmd 572string consists of one or more pairs of words. 573The first word in each pair is a token and the second is the value that 574should be applied for that token. 575Tokens and their argument types are as follows: 576.Bl -tag -width indent 577.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode 578In order to support transparent proxying, it is necessary to somehow 579pass the original address and port information into the new destination 580server. 581If 582.Cm encode_ip_hdr 583is specified, the original address and port is passed as an extra IP 584option. 585If 586.Cm encode_tcp_stream 587is specified, the original address and port is passed as the first 588piece of data in the TCP stream in the format 589.Dq DEST Ar IP port . 590.It Cm port Ar portnum 591Only packets with the destination port 592.Ar portnum 593are proxied. 594.It Cm server Ar host Ns Xo 595.Op : Ns Ar portnum 596.Xc 597This specifies the 598.Ar host 599and 600.Ar portnum 601that the data is to be redirected to. 602.Ar host 603must be an IP address rather than a DNS host name. 604If 605.Ar portnum 606is not specified, the destination port number is not changed. 607.Pp 608The 609.Ar server 610specification is mandatory unless the 611.Cm delete 612command is being used. 613.It Cm rule Ar index 614Normally, each call to 615.Fn PacketAliasProxyRule 616inserts the next rule at the start of a linear list of rules. 617If an 618.Ar index 619is specified, the new rule will be checked after all rules with lower 620indices. 621Calls to 622.Fn PacketAliasProxyRule 623that do not specify a rule are assigned rule 0. 624.It Cm delete Ar index 625This token and its argument MUST NOT be used with any other tokens. 626When used, all existing rules with the given 627.Ar index 628are deleted. 629.It Cm proto tcp | udp 630If specified, only packets of the given protocol type are matched. 631.It Cm src Ar IP Ns Xo 632.Op / Ns Ar bits 633.Xc 634If specified, only packets with a source address matching the given 635.Ar IP 636are matched. 637If 638.Ar bits 639is also specified, then the first 640.Ar bits 641bits of 642.Ar IP 643are taken as a network specification, and all IP addresses from that 644network will be matched. 645.It Cm dst Ar IP Ns Xo 646.Op / Ns Ar bits 647.Xc 648If specified, only packets with a destination address matching the given 649.Ar IP 650are matched. 651If 652.Ar bits 653is also specified, then the first 654.Ar bits 655bits of 656.Ar IP 657are taken as a network specification, and all IP addresses from that 658network will be matched. 659.El 660.Pp 661This function is usually used to redirect outgoing connections for 662internal machines that are not permitted certain types of internet 663access, or to restrict access to certain external machines. 664.Ed 665.Pp 666.Ft struct alias_link * 667.Fo PacketAliasRedirectProto 668.Fa "struct in_addr local_addr" 669.Fa "struct in_addr remote_addr" 670.Fa "struct in_addr alias_addr" 671.Fa "u_char proto" 672.Fc 673.Bd -ragged -offset indent 674This function specifies that any IP packet with protocol number of 675.Fa proto 676from a given remote address to an alias address be 677redirected to a specified local address. 678.Pp 679If 680.Fa local_addr 681or 682.Fa alias_addr 683is zero, this indicates that the packet aliasing address as established 684by 685.Fn PacketAliasSetAddress 686is to be used. 687Even if 688.Fn PacketAliasSetAddress 689is called to change the address after 690.Fn PacketAliasRedirectProto 691is called, a zero reference will track this change. 692.Pp 693If 694.Fa remote_addr 695is zero, this indicates to redirect packets from any remote address. 696Non-zero remote addresses can sometimes be useful for firewalling. 697.Pp 698If two calls to 699.Fn PacketAliasRedirectProto 700overlap in their address specifications, then the most recent call 701will have precedence. 702.Pp 703This function returns a pointer which can subsequently be used by 704.Fn PacketAliasRedirectDelete . 705If 706.Dv NULL 707is returned, then the function call did not complete successfully. 708.Ed 709.Sh FRAGMENT HANDLING 710The functions in this section are used to deal with incoming fragments. 711.Pp 712Outgoing fragments are handled within 713.Fn PacketAliasOut 714by changing the address according to any applicable mapping set by 715.Fn PacketAliasRedirectAddr , 716or the default aliasing address set by 717.Fn PacketAliasSetAddress . 718.Pp 719Incoming fragments are handled in one of two ways. 720If the header of a fragmented IP packet has already been seen, then all 721subsequent fragments will be re-mapped in the same manner the header 722fragment was. 723Fragments which arrive before the header are saved and then retrieved 724once the header fragment has been resolved. 725.Pp 726.Ft int 727.Fn PacketAliasSaveFragment "char *ptr" 728.Bd -ragged -offset indent 729When 730.Fn PacketAliasIn 731returns 732.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT , 733this function can be used to save the pointer to the unresolved fragment. 734.Pp 735It is implicitly assumed that 736.Fa ptr 737points to a block of memory allocated by 738.Xr malloc 3 . 739If the fragment is never resolved, the packet aliasing engine will 740automatically free the memory after a timeout period. 741[Eventually this function should be modified so that a callback function 742for freeing memory is passed as an argument.] 743.Pp 744This function returns 745.Dv PKT_ALIAS_OK 746if it was successful and 747.Dv PKT_ALIAS_ERROR 748if there was an error. 749.Ed 750.Pp 751.Ft char * 752.Fn PacketAliasGetFragment "char *buffer" 753.Bd -ragged -offset indent 754This function can be used to retrieve fragment pointers saved by 755.Fn PacketAliasSaveFragment . 756The IP header fragment pointed to by 757.Fa buffer 758is the header fragment indicated when 759.Fn PacketAliasIn 760returns 761.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT . 762Once a fragment pointer is retrieved, it becomes the calling program's 763responsibility to free the dynamically allocated memory for the fragment. 764.Pp 765.Fn PacketAliasGetFragment 766can be called sequentially until there are no more fragments available, 767at which time it returns 768.Dv NULL . 769.Ed 770.Pp 771.Ft void 772.Fn PacketAliasFragmentIn "char *header" "char *fragment" 773.Bd -ragged -offset indent 774When a fragment is retrieved with 775.Fn PacketAliasGetFragment , 776it can then be de-aliased with a call to 777.Fn PacketAliasFragmentIn . 778The 779.Fa header 780argument is the pointer to a header fragment used as a template, and 781.Fa fragment 782is the pointer to the packet to be de-aliased. 783.Ed 784.Sh MISCELLANEOUS FUNCTIONS 785.Ft void 786.Fn PacketAliasSetTarget "struct in_addr addr" 787.Bd -ragged -offset indent 788When an incoming packet not associated with any pre-existing aliasing link 789arrives at the host machine, it will be sent to the address indicated by a 790call to 791.Fn PacketAliasSetTarget . 792.Pp 793If this function is called with an 794.Dv INADDR_NONE 795address argument, then all new incoming packets go to the address set by 796.Fn PacketAliasSetAddress . 797.Pp 798If this function is not called, or is called with an 799.Dv INADDR_ANY 800address argument, then all new incoming packets go to the address specified 801in the packet. 802This allows external machines to talk directly to internal machines if they 803can route packets to the machine in question. 804.Ed 805.Pp 806.Ft int 807.Fn PacketAliasCheckNewLink void 808.Bd -ragged -offset indent 809This function returns a non-zero value when a new aliasing link is created. 810In circumstances where incoming traffic is being sequentially sent to 811different local servers, this function can be used to trigger when 812.Fn PacketAliasSetTarget 813is called to change the default target address. 814.Ed 815.Pp 816.Ft u_short 817.Fn PacketAliasInternetChecksum "u_short *buffer" "int nbytes" 818.Bd -ragged -offset indent 819This is a utility function that does not seem to be available elsewhere and 820is included as a convenience. 821It computes the internet checksum, which is used in both IP and 822protocol-specific headers (TCP, UDP, ICMP). 823.Pp 824The 825.Fa buffer 826argument points to the data block to be checksummed, and 827.Fa nbytes 828is the number of bytes. 829The 16-bit checksum field should be zeroed before computing the checksum. 830.Pp 831Checksums can also be verified by operating on a block of data including 832its checksum. 833If the checksum is valid, 834.Fn PacketAliasInternetChecksum 835will return zero. 836.Ed 837.Pp 838.Ft int 839.Fn PacketUnaliasOut "char *buffer" "int maxpacketsize" 840.Bd -ragged -offset indent 841An outgoing packet, which has already been aliased, 842has its private address/port information restored by this function. 843The IP packet is pointed to by 844.Fa buffer , 845and 846.Fa maxpacketsize 847is provided for error checking purposes. 848This function can be used if an already-aliased packet needs to have its 849original IP header restored for further processing (eg. logging). 850.Ed 851.Sh AUTHORS 852.An Charles Mott Aq cm@linktel.net , 853versions 1.0 - 1.8, 2.0 - 2.4. 854.An Eivind Eklund Aq eivind@FreeBSD.org , 855versions 1.8b, 1.9 and 2.5. 856Added IRC DCC support as well as contributing a number of architectural 857improvements; added the firewall bypass for FTP/IRC DCC. 858.An Erik Salander Aq erik@whistle.com 859added support for PPTP and RTSP. 860.An Junichi Satoh Aq junichi@junichi.org 861added support for RTSP/PNA. 862.Sh ACKNOWLEDGMENTS 863Listed below, in approximate chronological order, are individuals who 864have provided valuable comments and/or debugging assistance. 865.Bd -ragged -offset indent 866.An -split 867.An Gary Roberts 868.An Tom Torrance 869.An Reto Burkhalter 870.An Martin Renters 871.An Brian Somers 872.An Paul Traina 873.An Ari Suutari 874.An Dave Remien 875.An J. Fortes 876.An Andrzej Bialecki 877.An Gordon Burditt 878.Ed 879.Sh CONCEPTUAL BACKGROUND 880This section is intended for those who are planning to modify the source 881code or want to create somewhat esoteric applications using the packet 882aliasing functions. 883.Pp 884The conceptual framework under which the packet aliasing engine operates 885is described here. 886Central to the discussion is the idea of an 887.Em aliasing link 888which describes the relationship for a given packet transaction between 889the local machine, aliased identity and remote machine. 890It is discussed how such links come into existence and are destroyed. 891.Ss ALIASING LINKS 892There is a notion of an 893.Em aliasing link , 894which is a 7-tuple describing a specific translation: 895.Bd -literal -offset indent 896(local addr, local port, alias addr, alias port, 897 remote addr, remote port, protocol) 898.Ed 899.Pp 900Outgoing packets have the local address and port number replaced with the 901alias address and port number. 902Incoming packets undergo the reverse process. 903The packet aliasing engine attempts to match packets against an internal 904table of aliasing links to determine how to modify a given IP packet. 905Both the IP header and protocol dependent headers are modified as necessary. 906Aliasing links are created and deleted as necessary according to network 907traffic. 908.Pp 909Protocols can be TCP, UDP or even ICMP in certain circumstances. 910(Some types of ICMP packets can be aliased according to sequence or ID 911number which acts as an equivalent port number for identifying how 912individual packets should be handled.) 913.Pp 914Each aliasing link must have a unique combination of the following five 915quantities: alias address/port, remote address/port and protocol. 916This ensures that several machines on a local network can share the 917same aliasing IP address. 918In cases where conflicts might arise, the aliasing port is chosen so that 919uniqueness is maintained. 920.Ss STATIC AND DYNAMIC LINKS 921Aliasing links can either be static or dynamic. 922Static links persist indefinitely and represent fixed rules for translating 923IP packets. 924Dynamic links come into existence for a specific TCP connection or UDP 925transaction or ICMP ECHO sequence. 926For the case of TCP, the connection can be monitored to see when the 927associated aliasing link should be deleted. 928Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests) 929work on a simple timeout rule. 930When no activity is observed on a dynamic link for a certain amount of time 931it is automatically deleted. 932Timeout rules also apply to TCP connections which do not open or close 933properly. 934.Ss PARTIALLY SPECIFIED ALIASING LINKS 935Aliasing links can be partially specified, meaning that the remote address 936and/or remote port are unknown. 937In this case, when a packet matching the incomplete specification is found, 938a fully specified dynamic link is created. 939If the original partially specified link is dynamic, it will be deleted 940after the fully specified link is created, otherwise it will persist. 941.Pp 942For instance, a partially specified link might be 943.Bd -literal -offset indent 944(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp) 945.Ed 946.Pp 947The zeros denote unspecified components for the remote address and port. 948If this link were static it would have the effect of redirecting all 949incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet) 950of machine 192.168.0.4 on the local network. 951Each individual telnet connection would initiate the creation of a distinct 952dynamic link. 953.Ss DYNAMIC LINK CREATION 954In addition to aliasing links, there are also address mappings that can be 955stored within the internal data table of the packet aliasing mechanism. 956.Bd -literal -offset indent 957(local addr, alias addr) 958.Ed 959.Pp 960Address mappings are searched when creating new dynamic links. 961.Pp 962All outgoing packets from the local network automatically create a dynamic 963link if they do not match an already existing fully specified link. 964If an address mapping exists for the outgoing packet, this determines 965the alias address to be used. 966If no mapping exists, then a default address, usually the address of the 967packet aliasing host, is used. 968If necessary, this default address can be changed as often as each individual 969packet arrives. 970.Pp 971The aliasing port number is determined such that the new dynamic link does 972not conflict with any existing links. 973In the default operating mode, the packet aliasing engine attempts to set 974the aliasing port equal to the local port number. 975If this results in a conflict, then port numbers are randomly chosen until 976a unique aliasing link can be established. 977In an alternate operating mode, the first choice of an aliasing port is also 978random and unrelated to the local port number. 979.Sh BUGS 980PPTP aliasing does not work when more than one internal client 981connects to the same external server at the same time, because 982PPTP requires a single TCP control connection to be established 983between any two IP addresses. 984