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