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.Pp 684.Ft int 685.Fn PacketAliasPptp "struct in_addr addr" 686.Bd -ragged -offset indent 687This function causes any PPTP packets to be aliased using 688.Fa addr 689rather than the address set via 690.Fn PacketAliasSetAddress . 691This allows the uses of the PPTP on a single machine on the internal network. 692.Pp 693If the passed address is 694.Dv INADDR_NONE , 695then PPTP aliasing is disabled. 696.Pp 697.Bf -symbolic 698This function is made obsolete by 699.Fn PacketAliasRedirectProto , 700and is provided only for backward compatibility. 701.Ef 702.Ed 703.Sh FRAGMENT HANDLING 704The functions in this section are used to deal with incoming fragments. 705.Pp 706Outgoing fragments are handled within 707.Fn PacketAliasOut 708by changing the address according to any applicable mapping set by 709.Fn PacketAliasRedirectAddr , 710or the default aliasing address set by 711.Fn PacketAliasSetAddress . 712.Pp 713Incoming fragments are handled in one of two ways. 714If the header of a fragmented IP packet has already been seen, then all 715subsequent fragments will be re-mapped in the same manner the header 716fragment was. 717Fragments which arrive before the header are saved and then retrieved 718once the header fragment has been resolved. 719.Pp 720.Ft int 721.Fn PacketAliasSaveFragment "char *ptr" 722.Bd -ragged -offset indent 723When 724.Fn PacketAliasIn 725returns 726.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT , 727this function can be used to save the pointer to the unresolved fragment. 728.Pp 729It is implicitly assumed that 730.Fa ptr 731points to a block of memory allocated by 732.Xr malloc 3 . 733If the fragment is never resolved, the packet aliasing engine will 734automatically free the memory after a timeout period. 735[Eventually this function should be modified so that a callback function 736for freeing memory is passed as an argument.] 737.Pp 738This function returns 739.Dv PKT_ALIAS_OK 740if it was successful and 741.Dv PKT_ALIAS_ERROR 742if there was an error. 743.Ed 744.Pp 745.Ft char * 746.Fn PacketAliasGetFragment "char *buffer" 747.Bd -ragged -offset indent 748This function can be used to retrieve fragment pointers saved by 749.Fn PacketAliasSaveFragment . 750The IP header fragment pointed to by 751.Fa buffer 752is the header fragment indicated when 753.Fn PacketAliasIn 754returns 755.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT . 756Once a fragment pointer is retrieved, it becomes the calling program's 757responsibility to free the dynamically allocated memory for the fragment. 758.Pp 759.Fn PacketAliasGetFragment 760can be called sequentially until there are no more fragments available, 761at which time it returns 762.Dv NULL . 763.Ed 764.Pp 765.Ft void 766.Fn PacketAliasFragmentIn "char *header" "char *fragment" 767.Bd -ragged -offset indent 768When a fragment is retrieved with 769.Fn PacketAliasGetFragment , 770it can then be de-aliased with a call to 771.Fn PacketAliasFragmentIn . 772The 773.Fa header 774argument is the pointer to a header fragment used as a template, and 775.Fa fragment 776is the pointer to the packet to be de-aliased. 777.Ed 778.Sh MISCELLANEOUS FUNCTIONS 779.Ft void 780.Fn PacketAliasSetTarget "struct in_addr addr" 781.Bd -ragged -offset indent 782When an incoming packet not associated with any pre-existing aliasing link 783arrives at the host machine, it will be sent to the address indicated by a 784call to 785.Fn PacketAliasSetTarget . 786.Pp 787If this function is not called, or is called with an 788.Dv INADDR_NONE 789address argument, then all new incoming packets go to the address set by 790.Fn PacketAliasSetAddress . 791.Pp 792If this function is called with an 793.Dv INADDR_ANY 794address argument, then all new incoming packets go to the address specified 795in the packet. 796This allows external machines to talk directly to internal machines if they 797can route packets to the machine in question. 798.Ed 799.Pp 800.Ft int 801.Fn PacketAliasCheckNewLink void 802.Bd -ragged -offset indent 803This function returns a non-zero value when a new aliasing link is created. 804In circumstances where incoming traffic is being sequentially sent to 805different local servers, this function can be used to trigger when 806.Fn PacketAliasSetTarget 807is called to change the default target address. 808.Ed 809.Pp 810.Ft u_short 811.Fn PacketAliasInternetChecksum "u_short *buffer" "int nbytes" 812.Bd -ragged -offset indent 813This is a utility function that does not seem to be available elsewhere and 814is included as a convenience. 815It computes the internet checksum, which is used in both IP and 816protocol-specific headers (TCP, UDP, ICMP). 817.Pp 818The 819.Fa buffer 820argument points to the data block to be checksummed, and 821.Fa nbytes 822is the number of bytes. 823The 16-bit checksum field should be zeroed before computing the checksum. 824.Pp 825Checksums can also be verified by operating on a block of data including 826its checksum. 827If the checksum is valid, 828.Fn PacketAliasInternetChecksum 829will return zero. 830.Ed 831.Sh AUTHORS 832.An Charles Mott Aq cmott@scientech.com , 833versions 1.0 - 1.8, 2.0 - 2.4. 834.An Eivind Eklund Aq eivind@FreeBSD.org , 835versions 1.8b, 1.9 and 2.5. 836Added IRC DCC support as well as contributing a number of architectural 837improvements; added the firewall bypass for FTP/IRC DCC. 838.Sh ACKNOWLEDGMENTS 839Listed below, in approximate chronological order, are individuals who 840have provided valuable comments and/or debugging assistance. 841.Pp 842.Bl -item -offset indent -compact 843.It 844Gary Roberts 845.It 846Tom Torrance 847.It 848Reto Burkhalter 849.It 850Martin Renters 851.It 852Brian Somers 853.It 854Paul Traina 855.It 856Ari Suutari 857.It 858Dave Remien 859.It 860J. Fortes 861.It 862Andrzej Bialecki 863.It 864Gordon Burditt 865.El 866.Sh CONCEPTUAL BACKGROUND 867This section is intended for those who are planning to modify the source 868code or want to create somewhat esoteric applications using the packet 869aliasing functions. 870.Pp 871The conceptual framework under which the packet aliasing engine operates 872is described here. 873Central to the discussion is the idea of an 874.Em aliasing link 875which describes the relationship for a given packet transaction between 876the local machine, aliased identity and remote machine. 877It is discussed how such links come into existence and are destroyed. 878.Ss ALIASING LINKS 879There is a notion of an 880.Em aliasing link , 881which is a 7-tuple describing a specific translation: 882.Bd -literal -offset indent 883(local addr, local port, alias addr, alias port, 884 remote addr, remote port, protocol) 885.Ed 886.Pp 887Outgoing packets have the local address and port number replaced with the 888alias address and port number. 889Incoming packets undergo the reverse process. 890The packet aliasing engine attempts to match packets against an internal 891table of aliasing links to determine how to modify a given IP packet. 892Both the IP header and protocol dependent headers are modified as necessary. 893Aliasing links are created and deleted as necessary according to network 894traffic. 895.Pp 896Protocols can be TCP, UDP or even ICMP in certain circumstances. 897(Some types of ICMP packets can be aliased according to sequence or ID 898number which acts as an equivalent port number for identifying how 899individual packets should be handled.) 900.Pp 901Each aliasing link must have a unique combination of the following five 902quantities: alias address/port, remote address/port and protocol. 903This ensures that several machines on a local network can share the 904same aliasing IP address. 905In cases where conflicts might arise, the aliasing port is chosen so that 906uniqueness is maintained. 907.Ss STATIC AND DYNAMIC LINKS 908Aliasing links can either be static or dynamic. 909Static links persist indefinitely and represent fixed rules for translating 910IP packets. 911Dynamic links come into existence for a specific TCP connection or UDP 912transaction or ICMP ECHO sequence. 913For the case of TCP, the connection can be monitored to see when the 914associated aliasing link should be deleted. 915Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests) 916work on a simple timeout rule. 917When no activity is observed on a dynamic link for a certain amount of time 918it is automatically deleted. 919Timeout rules also apply to TCP connections which do not open or close 920properly. 921.Ss PARTIALLY SPECIFIED ALIASING LINKS 922Aliasing links can be partially specified, meaning that the remote address 923and/or remote port are unknown. 924In this case, when a packet matching the incomplete specification is found, 925a fully specified dynamic link is created. 926If the original partially specified link is dynamic, it will be deleted 927after the fully specified link is created, otherwise it will persist. 928.Pp 929For instance, a partially specified link might be 930.Bd -literal -offset indent 931(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp) 932.Ed 933.Pp 934The zeros denote unspecified components for the remote address and port. 935If this link were static it would have the effect of redirecting all 936incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet) 937of machine 192.168.0.4 on the local network. 938Each individual telnet connection would initiate the creation of a distinct 939dynamic link. 940.Ss DYNAMIC LINK CREATION 941In addition to aliasing links, there are also address mappings that can be 942stored within the internal data table of the packet aliasing mechanism. 943.Bd -literal -offset indent 944(local addr, alias addr) 945.Ed 946.Pp 947Address mappings are searched when creating new dynamic links. 948.Pp 949All outgoing packets from the local network automatically create a dynamic 950link if they do not match an already existing fully specified link. 951If an address mapping exists for the outgoing packet, this determines 952the alias address to be used. 953If no mapping exists, then a default address, usually the address of the 954packet aliasing host, is used. 955If necessary, this default address can be changed as often as each individual 956packet arrives. 957.Pp 958The aliasing port number is determined such that the new dynamic link does 959not conflict with any existing links. 960In the default operating mode, the packet aliasing engine attempts to set 961the aliasing port equal to the local port number. 962If this results in a conflict, then port numbers are randomly chosen until 963a unique aliasing link can be established. 964In an alternate operating mode, the first choice of an aliasing port is also 965random and unrelated to the local port number. 966