1f987e1bdSBrian Somers.\"- 2e83aaae3SBrian Somers.\" Copyright (c) 2001 Charles Mott <cm@linktel.net> 3f987e1bdSBrian Somers.\" All rights reserved. 4f987e1bdSBrian Somers.\" 5f987e1bdSBrian Somers.\" Redistribution and use in source and binary forms, with or without 6f987e1bdSBrian Somers.\" modification, are permitted provided that the following conditions 7f987e1bdSBrian Somers.\" are met: 8f987e1bdSBrian Somers.\" 1. Redistributions of source code must retain the above copyright 9f987e1bdSBrian Somers.\" notice, this list of conditions and the following disclaimer. 10f987e1bdSBrian Somers.\" 2. Redistributions in binary form must reproduce the above copyright 11f987e1bdSBrian Somers.\" notice, this list of conditions and the following disclaimer in the 12f987e1bdSBrian Somers.\" documentation and/or other materials provided with the distribution. 13f987e1bdSBrian Somers.\" 14f987e1bdSBrian Somers.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15f987e1bdSBrian Somers.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16f987e1bdSBrian Somers.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17f987e1bdSBrian Somers.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18f987e1bdSBrian Somers.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19f987e1bdSBrian Somers.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20f987e1bdSBrian Somers.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21f987e1bdSBrian Somers.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22f987e1bdSBrian Somers.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23f987e1bdSBrian Somers.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24f987e1bdSBrian Somers.\" SUCH DAMAGE. 25f987e1bdSBrian Somers.\" 267f3dea24SPeter Wemm.\" $FreeBSD$ 2774804d58SMike Pritchard.\" 28*62b6e03aSAndrey V. Elsukov.Dd June 22, 2011 2974804d58SMike Pritchard.Dt LIBALIAS 3 30a307d598SRuslan Ermilov.Os 313efa11bbSBrian Somers.Sh NAME 3274804d58SMike Pritchard.Nm libalias 33a2900666SRuslan Ermilov.Nd packet aliasing library for masquerading and network address translation 343efa11bbSBrian Somers.Sh SYNOPSIS 3532eef9aeSRuslan Ermilov.In sys/types.h 3632eef9aeSRuslan Ermilov.In netinet/in.h 3732eef9aeSRuslan Ermilov.In alias.h 38a2900666SRuslan Ermilov.Pp 39a2900666SRuslan ErmilovFunction prototypes are given in the main body of the text. 4074804d58SMike Pritchard.Sh DESCRIPTION 4174804d58SMike PritchardThe 4274804d58SMike Pritchard.Nm 43a2900666SRuslan Ermilovlibrary is a collection of functions for aliasing and de-aliasing of IP 44a2900666SRuslan Ermilovpackets, intended for masquerading and network address translation (NAT). 45a2900666SRuslan Ermilov.Sh INTRODUCTION 46a2900666SRuslan ErmilovThis library is a moderately portable set of functions designed to assist 47a2900666SRuslan Ermilovin the process of IP masquerading and network address translation. 48a2900666SRuslan ErmilovOutgoing packets from a local network with unregistered IP addresses can 49a2900666SRuslan Ermilovbe aliased to appear as if they came from an accessible IP address. 50a2900666SRuslan ErmilovIncoming packets are then de-aliased so that they are sent to the correct 51a2900666SRuslan Ermilovmachine on the local network. 52a2900666SRuslan Ermilov.Pp 53a2900666SRuslan ErmilovA certain amount of flexibility is built into the packet aliasing engine. 54a2900666SRuslan ErmilovIn the simplest mode of operation, a many-to-one address mapping takes 55a2900666SRuslan Ermilovplace between local network and the packet aliasing host. 56a2900666SRuslan ErmilovThis is known as IP masquerading. 57a2900666SRuslan ErmilovIn addition, one-to-one mappings between local and public addresses can 58a2900666SRuslan Ermilovalso be implemented, which is known as static NAT. 59a2900666SRuslan ErmilovIn between these extremes, different groups of private addresses can be 60a2900666SRuslan Ermilovlinked to different public addresses, comprising several distinct 61a2900666SRuslan Ermilovmany-to-one mappings. 62a2900666SRuslan ErmilovAlso, a given public address and port can be statically redirected to a 63a2900666SRuslan Ermilovprivate address/port. 64a2900666SRuslan Ermilov.Pp 65a2900666SRuslan ErmilovThe packet aliasing engine was designed to operate in user space outside 66a2900666SRuslan Ermilovof the kernel, without any access to private kernel data structure, but 67a2900666SRuslan Ermilovthe source code can also be ported to a kernel environment. 68a2900666SRuslan Ermilov.Sh INITIALIZATION AND CONTROL 69f1a529f3SRuslan ErmilovOne special function, 705e289f9eSPoul-Henning Kamp.Fn LibAliasInit , 715e289f9eSPoul-Henning Kampmust always be called before any packet handling may be performed and 725e289f9eSPoul-Henning Kampthe returned instance pointer passed to all the other functions. 73f1a529f3SRuslan ErmilovNormally, the 745e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 75f1a529f3SRuslan Ermilovfunction is called afterwards, to set the default aliasing address. 76a2900666SRuslan ErmilovIn addition, the operating mode of the packet aliasing engine can be 77a2900666SRuslan Ermilovcustomized by calling 785e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode . 79a2900666SRuslan Ermilov.Pp 805e289f9eSPoul-Henning Kamp.Ft "struct libalias *" 815e289f9eSPoul-Henning Kamp.Fn LibAliasInit "struct libalias *" 82a2900666SRuslan Ermilov.Bd -ragged -offset indent 835e289f9eSPoul-Henning KampThis function is used to initialize 84a2900666SRuslan Ermilovinternal data structures. 85c9a24641SRuslan ErmilovWhen called the first time, a 86c9a24641SRuslan Ermilov.Dv NULL 87c9a24641SRuslan Ermilovpointer should be passed as an argument. 88a2900666SRuslan ErmilovThe following mode bits are always set after calling 895e289f9eSPoul-Henning Kamp.Fn LibAliasInit . 90a2900666SRuslan ErmilovSee the description of 915e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode 92a2900666SRuslan Ermilovbelow for the meaning of these mode bits. 93a2900666SRuslan Ermilov.Pp 94a2900666SRuslan Ermilov.Bl -item -offset indent -compact 95a2900666SRuslan Ermilov.It 96a2900666SRuslan Ermilov.Dv PKT_ALIAS_SAME_PORTS 97a2900666SRuslan Ermilov.It 98a2900666SRuslan Ermilov.Dv PKT_ALIAS_USE_SOCKETS 99a2900666SRuslan Ermilov.It 100a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 101a2900666SRuslan Ermilov.El 102a2900666SRuslan Ermilov.Pp 103a2900666SRuslan ErmilovThis function will always return the packet aliasing engine to the same 104a2900666SRuslan Ermilovinitial state. 1059703a107SPhilippe CharnierThe 1065e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 1079703a107SPhilippe Charnierfunction is normally called afterwards, and any desired changes from the 1089703a107SPhilippe Charnierdefault mode bits listed above require a call to 1095e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode . 110a2900666SRuslan Ermilov.Pp 111a2900666SRuslan ErmilovIt is mandatory that this function be called at the beginning of a program 112a2900666SRuslan Ermilovprior to any packet handling. 113a2900666SRuslan Ermilov.Ed 114a2900666SRuslan Ermilov.Pp 1158ddc51bcSEivind Eklund.Ft void 1165e289f9eSPoul-Henning Kamp.Fn LibAliasUninit "struct libalias *" 117a2900666SRuslan Ermilov.Bd -ragged -offset indent 1185e289f9eSPoul-Henning KampThis function has no return value and is used to clear any 119a2900666SRuslan Ermilovresources attached to internal data structures. 120a2900666SRuslan Ermilov.Pp 121a2900666SRuslan ErmilovThis functions should be called when a program stops using the aliasing 122a2900666SRuslan Ermilovengine; it does, amongst other things, clear out any firewall holes. 123a2900666SRuslan ErmilovTo provide backwards compatibility and extra security, it is added to 124a2900666SRuslan Ermilovthe 125a2900666SRuslan Ermilov.Xr atexit 3 126a2900666SRuslan Ermilovchain by 1275e289f9eSPoul-Henning Kamp.Fn LibAliasInit . 128a2900666SRuslan Ermilov.Ed 129a2900666SRuslan Ermilov.Pp 1303efa11bbSBrian Somers.Ft void 1315e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr" 132a2900666SRuslan Ermilov.Bd -ragged -offset indent 133a2900666SRuslan ErmilovThis function sets the source address to which outgoing packets from the 134a2900666SRuslan Ermilovlocal area network are aliased. 135a2900666SRuslan ErmilovAll outgoing packets are re-mapped to this address unless overridden by a 136a2900666SRuslan Ermilovstatic address mapping established by 1375e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 138f1a529f3SRuslan ErmilovIf this function is not called, and no static rules match, an outgoing 139f1a529f3SRuslan Ermilovpacket retains its source address. 140a2900666SRuslan Ermilov.Pp 141a2900666SRuslan ErmilovIf the 142a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 143a2900666SRuslan Ermilovmode bit is set (the default mode of operation), then the internal aliasing 144a2900666SRuslan Ermilovlink tables will be reset any time the aliasing address changes. 145a2900666SRuslan ErmilovThis is useful for interfaces such as 146a2900666SRuslan Ermilov.Xr ppp 8 , 147a2900666SRuslan Ermilovwhere the IP 148a2900666SRuslan Ermilovaddress may or may not change on successive dial-up attempts. 149a2900666SRuslan Ermilov.Pp 150a2900666SRuslan ErmilovIf the 151a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 152a2900666SRuslan Ermilovmode bit is set to zero, this function can also be used to dynamically change 153a2900666SRuslan Ermilovthe aliasing address on a packet to packet basis (it is a low overhead call). 154a2900666SRuslan Ermilov.Pp 155a2900666SRuslan ErmilovIt is mandatory that this function be called prior to any packet handling. 156a2900666SRuslan Ermilov.Ed 157a2900666SRuslan Ermilov.Pp 158442a25bdSBruce Evans.Ft unsigned int 1595e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask" 160a2900666SRuslan Ermilov.Bd -ragged -offset indent 1613efa11bbSBrian SomersThis function sets or clears mode bits 1623efa11bbSBrian Somersaccording to the value of 163a2900666SRuslan Ermilov.Fa flags . 1643efa11bbSBrian SomersOnly bits marked in 165a2900666SRuslan Ermilov.Fa mask 166a2900666SRuslan Ermilovare affected. 167a2900666SRuslan ErmilovThe following mode bits are defined in 168fe08efe6SRuslan Ermilov.In alias.h : 169a2900666SRuslan Ermilov.Bl -tag -width indent 170a2900666SRuslan Ermilov.It Dv PKT_ALIAS_LOG 171a2900666SRuslan ErmilovEnables logging into 172a2900666SRuslan Ermilov.Pa /var/log/alias.log . 173a2900666SRuslan ErmilovEach time an aliasing link is created or deleted, the log file is appended 174a2900666SRuslan Ermilovwith the current number of ICMP, TCP and UDP links. 175a2900666SRuslan ErmilovMainly useful for debugging when the log file is viewed continuously with 176a2900666SRuslan Ermilov.Xr tail 1 . 177a2900666SRuslan Ermilov.It Dv PKT_ALIAS_DENY_INCOMING 178a2900666SRuslan ErmilovIf this mode bit is set, all incoming packets associated with new TCP 179a2900666SRuslan Ermilovconnections or new UDP transactions will be marked for being ignored 1805e289f9eSPoul-Henning Kamp.Fn ( LibAliasIn 181a2900666SRuslan Ermilovreturns 182a2900666SRuslan Ermilov.Dv PKT_ALIAS_IGNORED 183c4d9468eSRuslan Ermilovcode) 184a2900666SRuslan Ermilovby the calling program. 185a2900666SRuslan ErmilovResponse packets to connections or transactions initiated from the packet 186a2900666SRuslan Ermilovaliasing host or local network will be unaffected. 187a2900666SRuslan ErmilovThis mode bit is useful for implementing a one-way firewall. 188a2900666SRuslan Ermilov.It Dv PKT_ALIAS_SAME_PORTS 189a2900666SRuslan ErmilovIf this mode bit is set, the packet aliasing engine will attempt to leave 190a2900666SRuslan Ermilovthe alias port numbers unchanged from the actual local port numbers. 191a2900666SRuslan ErmilovThis can be done as long as the quintuple (proto, alias addr, alias port, 192a2900666SRuslan Ermilovremote addr, remote port) is unique. 193a2900666SRuslan ErmilovIf a conflict exists, a new aliasing port number is chosen even if this 194a2900666SRuslan Ermilovmode bit is set. 195a2900666SRuslan Ermilov.It Dv PKT_ALIAS_USE_SOCKETS 196a2900666SRuslan ErmilovThis bit should be set when the packet aliasing host originates network 197a2900666SRuslan Ermilovtraffic as well as forwards it. 198a2900666SRuslan ErmilovWhen the packet aliasing host is waiting for a connection from an unknown 1991a0a9345SRuslan Ermilovhost address or unknown port number (e.g.\& an FTP data connection), this 200a2900666SRuslan Ermilovmode bit specifies that a socket be allocated as a place holder to prevent 201a2900666SRuslan Ermilovport conflicts. 202a2900666SRuslan ErmilovOnce a connection is established, usually within a minute or so, the socket 203a2900666SRuslan Ermilovis closed. 204a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNREGISTERED_ONLY 205a2900666SRuslan ErmilovIf this mode bit is set, traffic on the local network which does not 206a2900666SRuslan Ermilovoriginate from unregistered address spaces will be ignored. 207a2900666SRuslan ErmilovStandard Class A, B and C unregistered addresses are: 2083efa11bbSBrian Somers.Bd -literal -offset indent 2093efa11bbSBrian Somers10.0.0.0 -> 10.255.255.255 (Class A subnet) 2103efa11bbSBrian Somers172.16.0.0 -> 172.31.255.255 (Class B subnets) 2113efa11bbSBrian Somers192.168.0.0 -> 192.168.255.255 (Class C subnets) 2123efa11bbSBrian Somers.Ed 213a2900666SRuslan Ermilov.Pp 214a2900666SRuslan ErmilovThis option is useful in the case that packet aliasing host has both 215a2900666SRuslan Ermilovregistered and unregistered subnets on different interfaces. 216a2900666SRuslan ErmilovThe registered subnet is fully accessible to the outside world, so traffic 217a2900666SRuslan Ermilovfrom it does not need to be passed through the packet aliasing engine. 218a2900666SRuslan Ermilov.It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 2193efa11bbSBrian SomersWhen this mode bit is set and 2205e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 221a2900666SRuslan Ermilovis called to change the aliasing address, the internal link table of the 222a2900666SRuslan Ermilovpacket aliasing engine will be cleared. 223a2900666SRuslan ErmilovThis operating mode is useful for 224a2900666SRuslan Ermilov.Xr ppp 8 225a2900666SRuslan Ermilovlinks where the interface address can sometimes change or remain the same 226a2900666SRuslan Ermilovbetween dial-up attempts. 227a2900666SRuslan ErmilovIf this mode bit is not set, the link table will never be reset in the event 228a2900666SRuslan Ermilovof an address change. 229a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PUNCH_FW 230a2900666SRuslan ErmilovThis option makes 231a2900666SRuslan Ermilov.Nm 232a2900666SRuslan Ermilov`punch holes' in an 233a2900666SRuslan Ermilov.Xr ipfirewall 4 234a2900666SRuslan Ermilovbased firewall for FTP/IRC DCC connections. 235a2900666SRuslan ErmilovThe holes punched are bound by from/to IP address and port; it will not be 236a2900666SRuslan Ermilovpossible to use a hole for another connection. 237a2900666SRuslan ErmilovA hole is removed when the connection that uses it dies. 238a2900666SRuslan ErmilovTo cater to unexpected death of a program using 239a2900666SRuslan Ermilov.Nm 2401a0a9345SRuslan Ermilov(e.g.\& kill -9), 241a2900666SRuslan Ermilovchanging the state of the flag will clear the entire firewall range 242a2900666SRuslan Ermilovallocated for holes. 2438ddc51bcSEivind EklundThis will also happen on the initial call to 2445e289f9eSPoul-Henning Kamp.Fn LibAliasSetFWBase . 245a2900666SRuslan ErmilovThis call must happen prior to setting this flag. 246a2900666SRuslan Ermilov.It Dv PKT_ALIAS_REVERSE 247a2900666SRuslan ErmilovThis option makes 248a2900666SRuslan Ermilov.Nm 249a2900666SRuslan Ermilovreverse the way it handles incoming and outgoing packets, allowing it 250a2900666SRuslan Ermilovto be fed with data that passes through the internal interface rather 251a2900666SRuslan Ermilovthan the external one. 252a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PROXY_ONLY 253a2900666SRuslan ErmilovThis option tells 254a2900666SRuslan Ermilov.Nm 255a2900666SRuslan Ermilovto obey transparent proxy rules only. 256a2900666SRuslan ErmilovNormal packet aliasing is not performed. 25721b9df57SBrian SomersSee 2585e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 25921b9df57SBrian Somersbelow for details. 260*62b6e03aSAndrey V. Elsukov.It Dv PKT_ALIAS_SKIP_GLOBAL 261*62b6e03aSAndrey V. ElsukovThis option is used by 262*62b6e03aSAndrey V. Elsukov.Pa ipfw_nat 263*62b6e03aSAndrey V. Elsukovonly. Specifying it as a flag to 264*62b6e03aSAndrey V. Elsukov.Fn LibAliasSetMode 265*62b6e03aSAndrey V. Elsukovhas no effect. See section 266*62b6e03aSAndrey V. Elsukov.Sx NETWORK ADDRESS TRANSLATION 267*62b6e03aSAndrey V. Elsukovin 268*62b6e03aSAndrey V. Elsukov.Xr ipfw 8 269*62b6e03aSAndrey V. Elsukovfor more details. 2703efa11bbSBrian Somers.El 271a2900666SRuslan Ermilov.Ed 272a2900666SRuslan Ermilov.Pp 2738ddc51bcSEivind Eklund.Ft void 2745e289f9eSPoul-Henning Kamp.Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num" 275a2900666SRuslan Ermilov.Bd -ragged -offset indent 276a2900666SRuslan ErmilovSet firewall range allocated for punching firewall holes (with the 277a2900666SRuslan Ermilov.Dv PKT_ALIAS_PUNCH_FW 278a2900666SRuslan Ermilovflag). 279a2900666SRuslan ErmilovThe range will be cleared for all rules on initialization. 280a2900666SRuslan Ermilov.Ed 281b07fbc17SJoe Marcus Clarke.Pp 282b07fbc17SJoe Marcus Clarke.Ft void 2835e289f9eSPoul-Henning Kamp.Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port" 284b07fbc17SJoe Marcus Clarke.Bd -ragged -offset indent 285b07fbc17SJoe Marcus ClarkeSet the TCP port used by the Skinny Station protocol. 286b07fbc17SJoe Marcus ClarkeSkinny is used by Cisco IP phones to communicate with 287b07fbc17SJoe Marcus ClarkeCisco Call Managers to set up voice over IP calls. 288b07fbc17SJoe Marcus ClarkeIf this is not set, Skinny aliasing will not be done. 289b07fbc17SJoe Marcus ClarkeThe typical port used by Skinny is 2000. 290b07fbc17SJoe Marcus Clarke.Ed 291a2900666SRuslan Ermilov.Sh PACKET HANDLING 292a2900666SRuslan ErmilovThe packet handling functions are used to modify incoming (remote to local) 293a2900666SRuslan Ermilovand outgoing (local to remote) packets. 294a2900666SRuslan ErmilovThe calling program is responsible for receiving and sending packets via 295a2900666SRuslan Ermilovnetwork interfaces. 296a2900666SRuslan Ermilov.Pp 297a2900666SRuslan ErmilovAlong with 2985e289f9eSPoul-Henning Kamp.Fn LibAliasInit 299a2900666SRuslan Ermilovand 3005e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress , 301a2900666SRuslan Ermilovthe two packet handling functions, 3025e289f9eSPoul-Henning Kamp.Fn LibAliasIn 303a2900666SRuslan Ermilovand 3045e289f9eSPoul-Henning Kamp.Fn LibAliasOut , 305a2900666SRuslan Ermilovcomprise minimal set of functions needed for a basic IP masquerading 306a2900666SRuslan Ermilovimplementation. 307a2900666SRuslan Ermilov.Pp 3083efa11bbSBrian Somers.Ft int 3095e289f9eSPoul-Henning Kamp.Fn LibAliasIn "struct libalias *" "char *buffer" "int maxpacketsize" 310a2900666SRuslan Ermilov.Bd -ragged -offset indent 311a2900666SRuslan ErmilovAn incoming packet coming from a remote machine to the local network is 312a2900666SRuslan Ermilovde-aliased by this function. 3133efa11bbSBrian SomersThe IP packet is pointed to by 314a2900666SRuslan Ermilov.Fa buffer , 3153efa11bbSBrian Somersand 316a2900666SRuslan Ermilov.Fa maxpacketsize 317a2900666SRuslan Ermilovindicates the size of the data structure containing the packet and should 318a2900666SRuslan Ermilovbe at least as large as the actual packet size. 319a2900666SRuslan Ermilov.Pp 3203efa11bbSBrian SomersReturn codes: 321a2900666SRuslan Ermilov.Bl -tag -width indent 322a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK 3233efa11bbSBrian SomersThe packet aliasing process was successful. 324a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED 3253efa11bbSBrian SomersThe packet was ignored and not de-aliased. 326a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, possibly an ICMP message 327a2900666SRuslan Ermilovtype is not handled or if incoming packets for new connections are being 328a2900666SRuslan Ermilovignored (if 329a2900666SRuslan Ermilov.Dv PKT_ALIAS_DENY_INCOMING 330a2900666SRuslan Ermilovmode bit was set by 3315e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode ) . 332a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT 333a2900666SRuslan ErmilovThis is returned when a fragment cannot be resolved because the header 334a2900666SRuslan Ermilovfragment has not been sent yet. 335a2900666SRuslan ErmilovIn this situation, fragments must be saved with 3365e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment 3373efa11bbSBrian Somersuntil a header fragment is found. 338a2900666SRuslan Ermilov.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT 339a2900666SRuslan ErmilovThe packet aliasing process was successful, and a header fragment was found. 340a2900666SRuslan ErmilovThis is a signal to retrieve any unresolved fragments with 3415e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment 342a2900666SRuslan Ermilovand de-alias them with 3435e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn . 344a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR 345a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred. 3463efa11bbSBrian Somers.El 347a2900666SRuslan Ermilov.Ed 348a2900666SRuslan Ermilov.Pp 3493efa11bbSBrian Somers.Ft int 3505e289f9eSPoul-Henning Kamp.Fn LibAliasOut "struct libalias *" "char *buffer" "int maxpacketsize" 351a2900666SRuslan Ermilov.Bd -ragged -offset indent 352a2900666SRuslan ErmilovAn outgoing packet coming from the local network to a remote machine is 353a2900666SRuslan Ermilovaliased by this function. 3543efa11bbSBrian SomersThe IP packet is pointed to by 355a2900666SRuslan Ermilov.Fa buffer , 3563efa11bbSBrian Somersand 357a2900666SRuslan Ermilov.Fa maxpacketsize 358a2900666SRuslan Ermilovindicates the maximum packet size permissible should the packet length be 359a2900666SRuslan Ermilovchanged. 360a2900666SRuslan ErmilovIP encoding protocols place address and port information in the encapsulated 361a2900666SRuslan Ermilovdata stream which has to be modified and can account for changes in packet 362a2900666SRuslan Ermilovlength. 363a2900666SRuslan ErmilovWell known examples of such protocols are FTP and IRC DCC. 364a2900666SRuslan Ermilov.Pp 3653efa11bbSBrian SomersReturn codes: 366a2900666SRuslan Ermilov.Bl -tag -width indent 367a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK 3683efa11bbSBrian SomersThe packet aliasing process was successful. 369a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED 370a2900666SRuslan ErmilovThe packet was ignored and not aliased. 371a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, or possibly an ICMP message 372a2900666SRuslan Ermilovtype is not handled. 373a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR 374a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred. 3753efa11bbSBrian Somers.El 376a2900666SRuslan Ermilov.Ed 377a2900666SRuslan Ermilov.Sh PORT AND ADDRESS REDIRECTION 378a2900666SRuslan ErmilovThe functions described in this section allow machines on the local network 379a2900666SRuslan Ermilovto be accessible in some degree to new incoming connections from the external 380a2900666SRuslan Ermilovnetwork. 381a2900666SRuslan ErmilovIndividual ports can be re-mapped or static network address translations can 382a2900666SRuslan Ermilovbe designated. 383a2900666SRuslan Ermilov.Pp 3843efa11bbSBrian Somers.Ft struct alias_link * 3855e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectPort 3865e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 3873efa11bbSBrian Somers.Fa "struct in_addr local_addr" 3883efa11bbSBrian Somers.Fa "u_short local_port" 3893efa11bbSBrian Somers.Fa "struct in_addr remote_addr" 3903efa11bbSBrian Somers.Fa "u_short remote_port" 3913efa11bbSBrian Somers.Fa "struct in_addr alias_addr" 3923efa11bbSBrian Somers.Fa "u_short alias_port" 3933efa11bbSBrian Somers.Fa "u_char proto" 3943efa11bbSBrian Somers.Fc 395a2900666SRuslan Ermilov.Bd -ragged -offset indent 396a2900666SRuslan ErmilovThis function specifies that traffic from a given remote address/port to 397a2900666SRuslan Ermilovan alias address/port be redirected to a specified local address/port. 3989c727d2cSJoseph KoshyThe parameter 399a2900666SRuslan Ermilov.Fa proto 400a2900666SRuslan Ermilovcan be either 401a2900666SRuslan Ermilov.Dv IPPROTO_TCP 4023efa11bbSBrian Somersor 403a2900666SRuslan Ermilov.Dv IPPROTO_UDP , 404a2900666SRuslan Ermilovas defined in 405fe08efe6SRuslan Ermilov.In netinet/in.h . 406a2900666SRuslan Ermilov.Pp 4073efa11bbSBrian SomersIf 408a2900666SRuslan Ermilov.Fa local_addr 409a2900666SRuslan Ermilovor 410a2900666SRuslan Ermilov.Fa alias_addr 411a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established 412a2900666SRuslan Ermilovby 4135e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 414a2900666SRuslan Ermilovis to be used. 415a2900666SRuslan ErmilovEven if 4165e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 417a2900666SRuslan Ermilovis called to change the address after 4185e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 419a2900666SRuslan Ermilovis called, a zero reference will track this change. 420a2900666SRuslan Ermilov.Pp 4216d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then 4226d20a774SRuslan Ermilov.Fa local_addr 4236d20a774SRuslan Ermilovand 4246d20a774SRuslan Ermilov.Fa local_port 4256d20a774SRuslan Ermilovare ignored, and are selected dynamically from the server pool, as described in 4265e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 4276d20a774SRuslan Ermilovbelow. 4286d20a774SRuslan Ermilov.Pp 429a2900666SRuslan ErmilovIf 430a2900666SRuslan Ermilov.Fa remote_addr 431a2900666SRuslan Ermilovis zero, this indicates to redirect packets from any remote address. 432a2900666SRuslan ErmilovLikewise, if 433a2900666SRuslan Ermilov.Fa remote_port 434a2900666SRuslan Ermilovis zero, this indicates to redirect packets originating from any remote 435a2900666SRuslan Ermilovport number. 436a2900666SRuslan ErmilovAlmost always, the remote port specification will be zero, but non-zero 437a2900666SRuslan Ermilovremote addresses can sometimes be useful for firewalling. 438a2900666SRuslan ErmilovIf two calls to 4395e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 440a2900666SRuslan Ermilovoverlap in their address/port specifications, then the most recent call 441a2900666SRuslan Ermilovwill have precedence. 442a2900666SRuslan Ermilov.Pp 443a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by 4445e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 445a2900666SRuslan ErmilovIf 446a2900666SRuslan Ermilov.Dv NULL 447a2900666SRuslan Ermilovis returned, then the function call did not complete successfully. 448a2900666SRuslan Ermilov.Pp 449a2900666SRuslan ErmilovAll port numbers should be in network address byte order, so it is necessary 450a2900666SRuslan Ermilovto use 451a2900666SRuslan Ermilov.Xr htons 3 452a2900666SRuslan Ermilovto convert these parameters from internally readable numbers to network byte 453a2900666SRuslan Ermilovorder. 454a2900666SRuslan ErmilovAddresses are also in network byte order, which is implicit in the use of the 455a2900666SRuslan Ermilov.Fa struct in_addr 4563efa11bbSBrian Somersdata type. 457a2900666SRuslan Ermilov.Ed 458a2900666SRuslan Ermilov.Pp 4593efa11bbSBrian Somers.Ft struct alias_link * 4605e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectAddr 4615e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 4623efa11bbSBrian Somers.Fa "struct in_addr local_addr" 4633efa11bbSBrian Somers.Fa "struct in_addr alias_addr" 4643efa11bbSBrian Somers.Fc 465a2900666SRuslan Ermilov.Bd -ragged -offset indent 466a2900666SRuslan ErmilovThis function designates that all incoming traffic to 467a2900666SRuslan Ermilov.Fa alias_addr 4683efa11bbSBrian Somersbe redirected to 469a2900666SRuslan Ermilov.Fa local_addr . 4703efa11bbSBrian SomersSimilarly, all outgoing traffic from 471a2900666SRuslan Ermilov.Fa local_addr 4723efa11bbSBrian Somersis aliased to 473a2900666SRuslan Ermilov.Fa alias_addr . 474a2900666SRuslan Ermilov.Pp 4753efa11bbSBrian SomersIf 476a2900666SRuslan Ermilov.Fa local_addr 4773efa11bbSBrian Somersor 478a2900666SRuslan Ermilov.Fa alias_addr 479a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established by 4805e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 481a2900666SRuslan Ermilovis to be used. 482a2900666SRuslan ErmilovEven if 4835e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 484a2900666SRuslan Ermilovis called to change the address after 4855e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr 4863efa11bbSBrian Somersis called, a zero reference will track this change. 487a2900666SRuslan Ermilov.Pp 4886d20a774SRuslan ErmilovIf the link is further set up to operate for a load sharing, then 4896d20a774SRuslan Ermilov.Fa local_addr 4906d20a774SRuslan Ermilovis ignored, and is selected dynamically from the server pool, as described in 4915e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 4926d20a774SRuslan Ermilovbelow. 4936d20a774SRuslan Ermilov.Pp 494a2900666SRuslan ErmilovIf subsequent calls to 4955e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr 496a2900666SRuslan Ermilovuse the same aliasing address, all new incoming traffic to this aliasing 497a2900666SRuslan Ermilovaddress will be redirected to the local address made in the last function 498a2900666SRuslan Ermilovcall. 499a2900666SRuslan ErmilovNew traffic generated by any of the local machines, designated in the 500a2900666SRuslan Ermilovseveral function calls, will be aliased to the same address. 501a2900666SRuslan ErmilovConsider the following example: 502a2900666SRuslan Ermilov.Bd -literal -offset indent 503c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.2"), 5043efa11bbSBrian Somers inet_aton("141.221.254.101")); 505c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.3"), 5063efa11bbSBrian Somers inet_aton("141.221.254.101")); 507c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.4"), 5083efa11bbSBrian Somers inet_aton("141.221.254.101")); 5093efa11bbSBrian Somers.Ed 510a2900666SRuslan Ermilov.Pp 511a2900666SRuslan ErmilovAny outgoing connections such as 512a2900666SRuslan Ermilov.Xr telnet 1 513a2900666SRuslan Ermilovor 514a2900666SRuslan Ermilov.Xr ftp 1 515a2900666SRuslan Ermilovfrom 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from 516a2900666SRuslan Ermilov141.221.254.101. 517a2900666SRuslan ErmilovAny incoming connections to 141.221.254.101 will be directed to 192.168.0.4. 518a2900666SRuslan Ermilov.Pp 519a2900666SRuslan ErmilovAny calls to 5205e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 521a2900666SRuslan Ermilovwill have precedence over address mappings designated by 5225e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 523a2900666SRuslan Ermilov.Pp 524a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by 5255e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 526a2900666SRuslan ErmilovIf 527a2900666SRuslan Ermilov.Dv NULL 528a2900666SRuslan Ermilovis returned, then the function call did not complete successfully. 529a2900666SRuslan Ermilov.Ed 530a2900666SRuslan Ermilov.Pp 5316d20a774SRuslan Ermilov.Ft int 5325e289f9eSPoul-Henning Kamp.Fo LibAliasAddServer 5335e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 5346d20a774SRuslan Ermilov.Fa "struct alias_link *link" 5356d20a774SRuslan Ermilov.Fa "struct in_addr addr" 5366d20a774SRuslan Ermilov.Fa "u_short port" 5376d20a774SRuslan Ermilov.Fc 5386d20a774SRuslan Ermilov.Bd -ragged -offset indent 5396d20a774SRuslan ErmilovThis function sets the 5406d20a774SRuslan Ermilov.Fa link 5416d20a774SRuslan Ermilovup for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT). 5426d20a774SRuslan ErmilovLSNAT operates as follows. 5436d20a774SRuslan ErmilovA client attempts to access a server by using the server virtual address. 5446d20a774SRuslan ErmilovThe LSNAT router transparently redirects the request to one of the hosts 5456d20a774SRuslan Ermilovin server pool, selected using a real-time load sharing algorithm. 5466d20a774SRuslan ErmilovMultiple sessions may be initiated from the same client, and each session 5476d20a774SRuslan Ermilovcould be directed to a different host based on load balance across server 5486d20a774SRuslan Ermilovpool hosts at the time. 5496d20a774SRuslan ErmilovIf load share is desired for just a few specific services, the configuration 5506d20a774SRuslan Ermilovon LSNAT could be defined to restrict load share for just the services 5516d20a774SRuslan Ermilovdesired. 5526d20a774SRuslan Ermilov.Pp 5536d20a774SRuslan ErmilovCurrently, only the simplest selection algorithm is implemented, where a 5546d20a774SRuslan Ermilovhost is selected on a round-robin basis only, without regard to load on 5556d20a774SRuslan Ermilovthe host. 5566d20a774SRuslan Ermilov.Pp 5576d20a774SRuslan ErmilovFirst, the 5586d20a774SRuslan Ermilov.Fa link 5596d20a774SRuslan Ermilovis created by either 5605e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 5616d20a774SRuslan Ermilovor 5625e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 5636d20a774SRuslan ErmilovThen, 5645e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 5656d20a774SRuslan Ermilovis called multiple times to add entries to the 5666d20a774SRuslan Ermilov.Fa link Ns 's 5676d20a774SRuslan Ermilovserver pool. 5686d20a774SRuslan Ermilov.Pp 5696d20a774SRuslan ErmilovFor links created with 5705e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr , 5716d20a774SRuslan Ermilovthe 5726d20a774SRuslan Ermilov.Fa port 5731a0a9345SRuslan Ermilovargument is ignored and could have any value, e.g.\& htons(~0). 5746d20a774SRuslan Ermilov.Pp 575234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise. 576234dfc90SRuslan Ermilov.Ed 577234dfc90SRuslan Ermilov.Pp 578234dfc90SRuslan Ermilov.Ft int 5795e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link" 580234dfc90SRuslan Ermilov.Bd -ragged -offset indent 581234dfc90SRuslan ErmilovThis function marks the specified static redirect rule entered by 5825e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 583234dfc90SRuslan Ermilovas dynamic. 5841a0a9345SRuslan ErmilovThis can be used to e.g.\& dynamically redirect a single TCP connection, 585234dfc90SRuslan Ermilovafter which the rule is removed. 586234dfc90SRuslan ErmilovOnly fully specified links can be made dynamic. 587234dfc90SRuslan Ermilov(See the 588234dfc90SRuslan Ermilov.Sx STATIC AND DYNAMIC LINKS 589234dfc90SRuslan Ermilovand 590234dfc90SRuslan Ermilov.Sx PARTIALLY SPECIFIED ALIASING LINKS 5911a0a9345SRuslan Ermilovsections below for a definition of static vs.\& dynamic, 5921a0a9345SRuslan Ermilovand partially vs.\& fully specified links.) 593234dfc90SRuslan Ermilov.Pp 594234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise. 5956d20a774SRuslan Ermilov.Ed 5966d20a774SRuslan Ermilov.Pp 5973efa11bbSBrian Somers.Ft void 5985e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link" 599a2900666SRuslan Ermilov.Bd -ragged -offset indent 600a2900666SRuslan ErmilovThis function will delete a specific static redirect rule entered by 6015e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 602a2900666SRuslan Ermilovor 6035e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 604a2900666SRuslan ErmilovThe parameter 605a2900666SRuslan Ermilov.Fa link 606a2900666SRuslan Ermilovis the pointer returned by either of the redirection functions. 607a2900666SRuslan ErmilovIf an invalid pointer is passed to 6085e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete , 609a2900666SRuslan Ermilovthen a program crash or unpredictable operation could result, so it is 6103efa11bbSBrian Somersnecessary to be careful using this function. 611a2900666SRuslan Ermilov.Ed 612a2900666SRuslan Ermilov.Pp 613619d1a30SBrian Somers.Ft int 6145e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule "struct libalias *" "const char *cmd" 615a2900666SRuslan Ermilov.Bd -ragged -offset indent 616619d1a30SBrian SomersThe passed 617a2900666SRuslan Ermilov.Fa cmd 618a2900666SRuslan Ermilovstring consists of one or more pairs of words. 619a2900666SRuslan ErmilovThe first word in each pair is a token and the second is the value that 620a2900666SRuslan Ermilovshould be applied for that token. 621a2900666SRuslan ErmilovTokens and their argument types are as follows: 622a2900666SRuslan Ermilov.Bl -tag -width indent 623a2900666SRuslan Ermilov.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode 624619d1a30SBrian SomersIn order to support transparent proxying, it is necessary to somehow 625619d1a30SBrian Somerspass the original address and port information into the new destination 626a2900666SRuslan Ermilovserver. 627a2900666SRuslan ErmilovIf 628a2900666SRuslan Ermilov.Cm encode_ip_hdr 62971760898SRuslan Ermilovis specified, the original destination address and port are passed 63071760898SRuslan Ermilovas an extra IP option. 631a2900666SRuslan ErmilovIf 632a2900666SRuslan Ermilov.Cm encode_tcp_stream 63371760898SRuslan Ermilovis specified, the original destination address and port are passed 63471760898SRuslan Ermilovas the first piece of data in the TCP stream in the format 63571760898SRuslan Ermilov.Dq Li DEST Ar IP port . 636a2900666SRuslan Ermilov.It Cm port Ar portnum 637619d1a30SBrian SomersOnly packets with the destination port 638619d1a30SBrian Somers.Ar portnum 639619d1a30SBrian Somersare proxied. 6409c88dc88SRuslan Ermilov.It Cm server Ar host Ns Op : Ns Ar portnum 641619d1a30SBrian SomersThis specifies the 642619d1a30SBrian Somers.Ar host 643619d1a30SBrian Somersand 644619d1a30SBrian Somers.Ar portnum 645ac8e3334SBrian Somersthat the data is to be redirected to. 646ac8e3334SBrian Somers.Ar host 647a2900666SRuslan Ermilovmust be an IP address rather than a DNS host name. 648a2900666SRuslan ErmilovIf 649619d1a30SBrian Somers.Ar portnum 650619d1a30SBrian Somersis not specified, the destination port number is not changed. 651619d1a30SBrian Somers.Pp 652619d1a30SBrian SomersThe 653619d1a30SBrian Somers.Ar server 654619d1a30SBrian Somersspecification is mandatory unless the 655a2900666SRuslan Ermilov.Cm delete 656619d1a30SBrian Somerscommand is being used. 657a2900666SRuslan Ermilov.It Cm rule Ar index 658619d1a30SBrian SomersNormally, each call to 6595e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 660a2900666SRuslan Ermilovinserts the next rule at the start of a linear list of rules. 661a2900666SRuslan ErmilovIf an 662619d1a30SBrian Somers.Ar index 663619d1a30SBrian Somersis specified, the new rule will be checked after all rules with lower 664a2900666SRuslan Ermilovindices. 665a2900666SRuslan ErmilovCalls to 6665e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 667619d1a30SBrian Somersthat do not specify a rule are assigned rule 0. 668a2900666SRuslan Ermilov.It Cm delete Ar index 669a2900666SRuslan ErmilovThis token and its argument MUST NOT be used with any other tokens. 670a2900666SRuslan ErmilovWhen used, all existing rules with the given 671619d1a30SBrian Somers.Ar index 672619d1a30SBrian Somersare deleted. 673a2900666SRuslan Ermilov.It Cm proto tcp | udp 674619d1a30SBrian SomersIf specified, only packets of the given protocol type are matched. 6759c88dc88SRuslan Ermilov.It Cm src Ar IP Ns Op / Ns Ar bits 676619d1a30SBrian SomersIf specified, only packets with a source address matching the given 677619d1a30SBrian Somers.Ar IP 678a2900666SRuslan Ermilovare matched. 679a2900666SRuslan ErmilovIf 680619d1a30SBrian Somers.Ar bits 681619d1a30SBrian Somersis also specified, then the first 682619d1a30SBrian Somers.Ar bits 683619d1a30SBrian Somersbits of 684619d1a30SBrian Somers.Ar IP 685619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that 686619d1a30SBrian Somersnetwork will be matched. 6879c88dc88SRuslan Ermilov.It Cm dst Ar IP Ns Op / Ns Ar bits 688619d1a30SBrian SomersIf specified, only packets with a destination address matching the given 689619d1a30SBrian Somers.Ar IP 690a2900666SRuslan Ermilovare matched. 691a2900666SRuslan ErmilovIf 692619d1a30SBrian Somers.Ar bits 693619d1a30SBrian Somersis also specified, then the first 694619d1a30SBrian Somers.Ar bits 695619d1a30SBrian Somersbits of 696619d1a30SBrian Somers.Ar IP 697619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that 698619d1a30SBrian Somersnetwork will be matched. 699619d1a30SBrian Somers.El 700a2900666SRuslan Ermilov.Pp 701619d1a30SBrian SomersThis function is usually used to redirect outgoing connections for 702619d1a30SBrian Somersinternal machines that are not permitted certain types of internet 703619d1a30SBrian Somersaccess, or to restrict access to certain external machines. 704a2900666SRuslan Ermilov.Ed 705a2900666SRuslan Ermilov.Pp 706483d2f22SRuslan Ermilov.Ft struct alias_link * 7075e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectProto 7085e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 709483d2f22SRuslan Ermilov.Fa "struct in_addr local_addr" 710483d2f22SRuslan Ermilov.Fa "struct in_addr remote_addr" 711483d2f22SRuslan Ermilov.Fa "struct in_addr alias_addr" 71280607605SRuslan Ermilov.Fa "u_char proto" 713483d2f22SRuslan Ermilov.Fc 714483d2f22SRuslan Ermilov.Bd -ragged -offset indent 71580607605SRuslan ErmilovThis function specifies that any IP packet with protocol number of 71680607605SRuslan Ermilov.Fa proto 71780607605SRuslan Ermilovfrom a given remote address to an alias address be 718483d2f22SRuslan Ermilovredirected to a specified local address. 719483d2f22SRuslan Ermilov.Pp 720483d2f22SRuslan ErmilovIf 721483d2f22SRuslan Ermilov.Fa local_addr 722483d2f22SRuslan Ermilovor 723483d2f22SRuslan Ermilov.Fa alias_addr 724483d2f22SRuslan Ermilovis zero, this indicates that the packet aliasing address as established 725483d2f22SRuslan Ermilovby 7265e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 727483d2f22SRuslan Ermilovis to be used. 728483d2f22SRuslan ErmilovEven if 7295e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 730483d2f22SRuslan Ermilovis called to change the address after 7315e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto 732483d2f22SRuslan Ermilovis called, a zero reference will track this change. 733483d2f22SRuslan Ermilov.Pp 734483d2f22SRuslan ErmilovIf 735483d2f22SRuslan Ermilov.Fa remote_addr 73680607605SRuslan Ermilovis zero, this indicates to redirect packets from any remote address. 737483d2f22SRuslan ErmilovNon-zero remote addresses can sometimes be useful for firewalling. 738483d2f22SRuslan Ermilov.Pp 739483d2f22SRuslan ErmilovIf two calls to 7405e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto 741483d2f22SRuslan Ermilovoverlap in their address specifications, then the most recent call 742483d2f22SRuslan Ermilovwill have precedence. 743483d2f22SRuslan Ermilov.Pp 744483d2f22SRuslan ErmilovThis function returns a pointer which can subsequently be used by 7455e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 746483d2f22SRuslan ErmilovIf 747483d2f22SRuslan Ermilov.Dv NULL 748483d2f22SRuslan Ermilovis returned, then the function call did not complete successfully. 749483d2f22SRuslan Ermilov.Ed 750a2900666SRuslan Ermilov.Sh FRAGMENT HANDLING 751a2900666SRuslan ErmilovThe functions in this section are used to deal with incoming fragments. 752a2900666SRuslan Ermilov.Pp 753a2900666SRuslan ErmilovOutgoing fragments are handled within 7545e289f9eSPoul-Henning Kamp.Fn LibAliasOut 755a2900666SRuslan Ermilovby changing the address according to any applicable mapping set by 7565e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr , 7571855100fSAlexey Zelkinor the default aliasing address set by 7585e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress . 759a2900666SRuslan Ermilov.Pp 7601855100fSAlexey ZelkinIncoming fragments are handled in one of two ways. 761a2900666SRuslan ErmilovIf the header of a fragmented IP packet has already been seen, then all 762a2900666SRuslan Ermilovsubsequent fragments will be re-mapped in the same manner the header 763a2900666SRuslan Ermilovfragment was. 764a2900666SRuslan ErmilovFragments which arrive before the header are saved and then retrieved 765a2900666SRuslan Ermilovonce the header fragment has been resolved. 766a2900666SRuslan Ermilov.Pp 7673efa11bbSBrian Somers.Ft int 7685e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment "struct libalias *" "char *ptr" 769a2900666SRuslan Ermilov.Bd -ragged -offset indent 770a2900666SRuslan ErmilovWhen 7715e289f9eSPoul-Henning Kamp.Fn LibAliasIn 772a2900666SRuslan Ermilovreturns 773a2900666SRuslan Ermilov.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT , 774a2900666SRuslan Ermilovthis function can be used to save the pointer to the unresolved fragment. 775a2900666SRuslan Ermilov.Pp 7763efa11bbSBrian SomersIt is implicitly assumed that 777a2900666SRuslan Ermilov.Fa ptr 7783efa11bbSBrian Somerspoints to a block of memory allocated by 779a2900666SRuslan Ermilov.Xr malloc 3 . 780a2900666SRuslan ErmilovIf the fragment is never resolved, the packet aliasing engine will 781a2900666SRuslan Ermilovautomatically free the memory after a timeout period. 782a2900666SRuslan Ermilov[Eventually this function should be modified so that a callback function 783a2900666SRuslan Ermilovfor freeing memory is passed as an argument.] 784a2900666SRuslan Ermilov.Pp 785a2900666SRuslan ErmilovThis function returns 786a2900666SRuslan Ermilov.Dv PKT_ALIAS_OK 787a2900666SRuslan Ermilovif it was successful and 788a2900666SRuslan Ermilov.Dv PKT_ALIAS_ERROR 789a2900666SRuslan Ermilovif there was an error. 790a2900666SRuslan Ermilov.Ed 791a2900666SRuslan Ermilov.Pp 7923efa11bbSBrian Somers.Ft char * 7935e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment "struct libalias *" "char *buffer" 794a2900666SRuslan Ermilov.Bd -ragged -offset indent 795a2900666SRuslan ErmilovThis function can be used to retrieve fragment pointers saved by 7965e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment . 7973efa11bbSBrian SomersThe IP header fragment pointed to by 798a2900666SRuslan Ermilov.Fa buffer 7993efa11bbSBrian Somersis the header fragment indicated when 8005e289f9eSPoul-Henning Kamp.Fn LibAliasIn 801a2900666SRuslan Ermilovreturns 802a2900666SRuslan Ermilov.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT . 803a2900666SRuslan ErmilovOnce a fragment pointer is retrieved, it becomes the calling program's 804a2900666SRuslan Ermilovresponsibility to free the dynamically allocated memory for the fragment. 805a2900666SRuslan Ermilov.Pp 8069703a107SPhilippe CharnierThe 8075e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment 8089703a107SPhilippe Charnierfunction can be called sequentially until there are no more fragments 8099703a107SPhilippe Charnieravailable, at which time it returns 810a2900666SRuslan Ermilov.Dv NULL . 811a2900666SRuslan Ermilov.Ed 812a2900666SRuslan Ermilov.Pp 8133efa11bbSBrian Somers.Ft void 8145e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn "struct libalias *" "char *header" "char *fragment" 815a2900666SRuslan Ermilov.Bd -ragged -offset indent 8163efa11bbSBrian SomersWhen a fragment is retrieved with 8175e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment , 818a2900666SRuslan Ermilovit can then be de-aliased with a call to 8195e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn . 820a2900666SRuslan ErmilovThe 821a2900666SRuslan Ermilov.Fa header 822a2900666SRuslan Ermilovargument is the pointer to a header fragment used as a template, and 823a2900666SRuslan Ermilov.Fa fragment 8243efa11bbSBrian Somersis the pointer to the packet to be de-aliased. 825a2900666SRuslan Ermilov.Ed 826a2900666SRuslan Ermilov.Sh MISCELLANEOUS FUNCTIONS 8273efa11bbSBrian Somers.Ft void 8285e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr" 829a2900666SRuslan Ermilov.Bd -ragged -offset indent 830a2900666SRuslan ErmilovWhen an incoming packet not associated with any pre-existing aliasing link 831a2900666SRuslan Ermilovarrives at the host machine, it will be sent to the address indicated by a 832a2900666SRuslan Ermilovcall to 8335e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget . 834a2900666SRuslan Ermilov.Pp 835151682eaSBrian SomersIf this function is called with an 836a2900666SRuslan Ermilov.Dv INADDR_NONE 837a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address set by 8385e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress . 839a2900666SRuslan Ermilov.Pp 840151682eaSBrian SomersIf this function is not called, or is called with an 841a2900666SRuslan Ermilov.Dv INADDR_ANY 842a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address specified 843a2900666SRuslan Ermilovin the packet. 844a2900666SRuslan ErmilovThis allows external machines to talk directly to internal machines if they 845a2900666SRuslan Ermilovcan route packets to the machine in question. 846a2900666SRuslan Ermilov.Ed 847a2900666SRuslan Ermilov.Pp 8483efa11bbSBrian Somers.Ft int 849fc1eaecfSRuslan Ermilov.Fn LibAliasCheckNewLink "struct libalias *" 850a2900666SRuslan Ermilov.Bd -ragged -offset indent 851a2900666SRuslan ErmilovThis function returns a non-zero value when a new aliasing link is created. 852a2900666SRuslan ErmilovIn circumstances where incoming traffic is being sequentially sent to 853a2900666SRuslan Ermilovdifferent local servers, this function can be used to trigger when 8545e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget 8553efa11bbSBrian Somersis called to change the default target address. 856a2900666SRuslan Ermilov.Ed 857a2900666SRuslan Ermilov.Pp 8583efa11bbSBrian Somers.Ft u_short 8595e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes" 860a2900666SRuslan Ermilov.Bd -ragged -offset indent 861a2900666SRuslan ErmilovThis is a utility function that does not seem to be available elsewhere and 862a2900666SRuslan Ermilovis included as a convenience. 863a2900666SRuslan ErmilovIt computes the internet checksum, which is used in both IP and 864a2900666SRuslan Ermilovprotocol-specific headers (TCP, UDP, ICMP). 865a2900666SRuslan Ermilov.Pp 866a2900666SRuslan ErmilovThe 867a2900666SRuslan Ermilov.Fa buffer 868a2900666SRuslan Ermilovargument points to the data block to be checksummed, and 869a2900666SRuslan Ermilov.Fa nbytes 870a2900666SRuslan Ermilovis the number of bytes. 871a2900666SRuslan ErmilovThe 16-bit checksum field should be zeroed before computing the checksum. 872a2900666SRuslan Ermilov.Pp 873a2900666SRuslan ErmilovChecksums can also be verified by operating on a block of data including 874a2900666SRuslan Ermilovits checksum. 875a2900666SRuslan ErmilovIf the checksum is valid, 8765e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum 877a2900666SRuslan Ermilovwill return zero. 878a2900666SRuslan Ermilov.Ed 879642e43b3SArchie Cobbs.Pp 880642e43b3SArchie Cobbs.Ft int 8815e289f9eSPoul-Henning Kamp.Fn LibAliasUnaliasOut "struct libalias *" "char *buffer" "int maxpacketsize" 882642e43b3SArchie Cobbs.Bd -ragged -offset indent 88371845bffSSheldon HearnAn outgoing packet, which has already been aliased, 88471845bffSSheldon Hearnhas its private address/port information restored by this function. 885642e43b3SArchie CobbsThe IP packet is pointed to by 886642e43b3SArchie Cobbs.Fa buffer , 887642e43b3SArchie Cobbsand 888642e43b3SArchie Cobbs.Fa maxpacketsize 889642e43b3SArchie Cobbsis provided for error checking purposes. 890642e43b3SArchie CobbsThis function can be used if an already-aliased packet needs to have its 8911a0a9345SRuslan Ermilovoriginal IP header restored for further processing (e.g.\& logging). 892642e43b3SArchie Cobbs.Ed 893a2900666SRuslan Ermilov.Sh AUTHORS 894e83aaae3SBrian Somers.An Charles Mott Aq cm@linktel.net , 895a2900666SRuslan Ermilovversions 1.0 - 1.8, 2.0 - 2.4. 896a2900666SRuslan Ermilov.An Eivind Eklund Aq eivind@FreeBSD.org , 897a2900666SRuslan Ermilovversions 1.8b, 1.9 and 2.5. 898a2900666SRuslan ErmilovAdded IRC DCC support as well as contributing a number of architectural 899a2900666SRuslan Ermilovimprovements; added the firewall bypass for FTP/IRC DCC. 90055a39fc5SRuslan Ermilov.An Erik Salander Aq erik@whistle.com 901642e43b3SArchie Cobbsadded support for PPTP and RTSP. 902642e43b3SArchie Cobbs.An Junichi Satoh Aq junichi@junichi.org 903642e43b3SArchie Cobbsadded support for RTSP/PNA. 90461de149dSRuslan Ermilov.An Ruslan Ermilov Aq ru@FreeBSD.org 90561de149dSRuslan Ermilovadded support for PPTP and LSNAT as well as general hacking. 906be4f3cd0SPaolo Pisati.An Paolo Pisati Aq piso@FreeBSD.org 907be4f3cd0SPaolo Pisatimade the library modular, moving support for all 908be4f3cd0SPaolo Pisatiprotocols (except for IP, TCP and UDP) to external modules. 909a2900666SRuslan Ermilov.Sh ACKNOWLEDGMENTS 910a2900666SRuslan ErmilovListed below, in approximate chronological order, are individuals who 911a2900666SRuslan Ermilovhave provided valuable comments and/or debugging assistance. 9121b7b85c4SRuslan Ermilov.Bd -ragged -offset indent 9131b7b85c4SRuslan Ermilov.An -split 9141b7b85c4SRuslan Ermilov.An Gary Roberts 9151b7b85c4SRuslan Ermilov.An Tom Torrance 9161b7b85c4SRuslan Ermilov.An Reto Burkhalter 9171b7b85c4SRuslan Ermilov.An Martin Renters 9181b7b85c4SRuslan Ermilov.An Brian Somers 9191b7b85c4SRuslan Ermilov.An Paul Traina 9201b7b85c4SRuslan Ermilov.An Ari Suutari 9211b7b85c4SRuslan Ermilov.An Dave Remien 9221b7b85c4SRuslan Ermilov.An J. Fortes 9231b7b85c4SRuslan Ermilov.An Andrzej Bialecki 9241b7b85c4SRuslan Ermilov.An Gordon Burditt 9251b7b85c4SRuslan Ermilov.Ed 926a2900666SRuslan Ermilov.Sh CONCEPTUAL BACKGROUND 927a2900666SRuslan ErmilovThis section is intended for those who are planning to modify the source 928a2900666SRuslan Ermilovcode or want to create somewhat esoteric applications using the packet 929a2900666SRuslan Ermilovaliasing functions. 930a2900666SRuslan Ermilov.Pp 931a2900666SRuslan ErmilovThe conceptual framework under which the packet aliasing engine operates 932a2900666SRuslan Ermilovis described here. 9333efa11bbSBrian SomersCentral to the discussion is the idea of an 934a2900666SRuslan Ermilov.Em aliasing link 935a2900666SRuslan Ermilovwhich describes the relationship for a given packet transaction between 936a2900666SRuslan Ermilovthe local machine, aliased identity and remote machine. 937a2900666SRuslan ErmilovIt is discussed how such links come into existence and are destroyed. 938a2900666SRuslan Ermilov.Ss ALIASING LINKS 939a2900666SRuslan ErmilovThere is a notion of an 940a2900666SRuslan Ermilov.Em aliasing link , 941a2900666SRuslan Ermilovwhich is a 7-tuple describing a specific translation: 9423efa11bbSBrian Somers.Bd -literal -offset indent 9433efa11bbSBrian Somers(local addr, local port, alias addr, alias port, 9443efa11bbSBrian Somers remote addr, remote port, protocol) 9453efa11bbSBrian Somers.Ed 946a2900666SRuslan Ermilov.Pp 947a2900666SRuslan ErmilovOutgoing packets have the local address and port number replaced with the 948a2900666SRuslan Ermilovalias address and port number. 949a2900666SRuslan ErmilovIncoming packets undergo the reverse process. 950a2900666SRuslan ErmilovThe packet aliasing engine attempts to match packets against an internal 951a2900666SRuslan Ermilovtable of aliasing links to determine how to modify a given IP packet. 952a2900666SRuslan ErmilovBoth the IP header and protocol dependent headers are modified as necessary. 953a2900666SRuslan ErmilovAliasing links are created and deleted as necessary according to network 954a2900666SRuslan Ermilovtraffic. 955a2900666SRuslan Ermilov.Pp 956a2900666SRuslan ErmilovProtocols can be TCP, UDP or even ICMP in certain circumstances. 957a2900666SRuslan Ermilov(Some types of ICMP packets can be aliased according to sequence or ID 958a2900666SRuslan Ermilovnumber which acts as an equivalent port number for identifying how 959a2900666SRuslan Ermilovindividual packets should be handled.) 960a2900666SRuslan Ermilov.Pp 961a2900666SRuslan ErmilovEach aliasing link must have a unique combination of the following five 962a2900666SRuslan Ermilovquantities: alias address/port, remote address/port and protocol. 963a2900666SRuslan ErmilovThis ensures that several machines on a local network can share the 964a2900666SRuslan Ermilovsame aliasing IP address. 965a2900666SRuslan ErmilovIn cases where conflicts might arise, the aliasing port is chosen so that 966a2900666SRuslan Ermilovuniqueness is maintained. 967a2900666SRuslan Ermilov.Ss STATIC AND DYNAMIC LINKS 9683efa11bbSBrian SomersAliasing links can either be static or dynamic. 969a2900666SRuslan ErmilovStatic links persist indefinitely and represent fixed rules for translating 970a2900666SRuslan ErmilovIP packets. 971a2900666SRuslan ErmilovDynamic links come into existence for a specific TCP connection or UDP 972a2900666SRuslan Ermilovtransaction or ICMP ECHO sequence. 973a2900666SRuslan ErmilovFor the case of TCP, the connection can be monitored to see when the 974a2900666SRuslan Ermilovassociated aliasing link should be deleted. 975a2900666SRuslan ErmilovAliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests) 976a2900666SRuslan Ermilovwork on a simple timeout rule. 977a2900666SRuslan ErmilovWhen no activity is observed on a dynamic link for a certain amount of time 978a2900666SRuslan Ermilovit is automatically deleted. 979a2900666SRuslan ErmilovTimeout rules also apply to TCP connections which do not open or close 9803efa11bbSBrian Somersproperly. 981a2900666SRuslan Ermilov.Ss PARTIALLY SPECIFIED ALIASING LINKS 982a2900666SRuslan ErmilovAliasing links can be partially specified, meaning that the remote address 983a2900666SRuslan Ermilovand/or remote port are unknown. 984a2900666SRuslan ErmilovIn this case, when a packet matching the incomplete specification is found, 985a2900666SRuslan Ermilova fully specified dynamic link is created. 986a2900666SRuslan ErmilovIf the original partially specified link is dynamic, it will be deleted 987a2900666SRuslan Ermilovafter the fully specified link is created, otherwise it will persist. 988a2900666SRuslan Ermilov.Pp 989a2900666SRuslan ErmilovFor instance, a partially specified link might be 9903efa11bbSBrian Somers.Bd -literal -offset indent 9913efa11bbSBrian Somers(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp) 9923efa11bbSBrian Somers.Ed 993a2900666SRuslan Ermilov.Pp 994a2900666SRuslan ErmilovThe zeros denote unspecified components for the remote address and port. 995a2900666SRuslan ErmilovIf this link were static it would have the effect of redirecting all 996a2900666SRuslan Ermilovincoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet) 997a2900666SRuslan Ermilovof machine 192.168.0.4 on the local network. 998a2900666SRuslan ErmilovEach individual telnet connection would initiate the creation of a distinct 999a2900666SRuslan Ermilovdynamic link. 1000a2900666SRuslan Ermilov.Ss DYNAMIC LINK CREATION 1001a2900666SRuslan ErmilovIn addition to aliasing links, there are also address mappings that can be 1002a2900666SRuslan Ermilovstored within the internal data table of the packet aliasing mechanism. 10033efa11bbSBrian Somers.Bd -literal -offset indent 10043efa11bbSBrian Somers(local addr, alias addr) 10053efa11bbSBrian Somers.Ed 1006a2900666SRuslan Ermilov.Pp 1007a2900666SRuslan ErmilovAddress mappings are searched when creating new dynamic links. 1008a2900666SRuslan Ermilov.Pp 1009a2900666SRuslan ErmilovAll outgoing packets from the local network automatically create a dynamic 1010a2900666SRuslan Ermilovlink if they do not match an already existing fully specified link. 1011a2900666SRuslan ErmilovIf an address mapping exists for the outgoing packet, this determines 1012a2900666SRuslan Ermilovthe alias address to be used. 1013a2900666SRuslan ErmilovIf no mapping exists, then a default address, usually the address of the 1014a2900666SRuslan Ermilovpacket aliasing host, is used. 1015a2900666SRuslan ErmilovIf necessary, this default address can be changed as often as each individual 1016a2900666SRuslan Ermilovpacket arrives. 1017a2900666SRuslan Ermilov.Pp 1018a2900666SRuslan ErmilovThe aliasing port number is determined such that the new dynamic link does 1019a2900666SRuslan Ermilovnot conflict with any existing links. 1020a2900666SRuslan ErmilovIn the default operating mode, the packet aliasing engine attempts to set 1021a2900666SRuslan Ermilovthe aliasing port equal to the local port number. 1022a2900666SRuslan ErmilovIf this results in a conflict, then port numbers are randomly chosen until 1023a2900666SRuslan Ermilova unique aliasing link can be established. 1024a2900666SRuslan ErmilovIn an alternate operating mode, the first choice of an aliasing port is also 1025a2900666SRuslan Ermilovrandom and unrelated to the local port number. 1026be4f3cd0SPaolo Pisati.Sh MODULAR ARCHITECTURE (AND Xr ipfw 4 Sh SUPPORT) 1027be4f3cd0SPaolo PisatiOne of the latest improvements to 1028f3d9aab3SPaolo Pisati.Nm 1029f3d9aab3SPaolo Pisatiwas to make its support 1030be4f3cd0SPaolo Pisatifor new protocols independent from the rest of the library, giving it 1031f3d9aab3SPaolo Pisatithe ability to load/unload support for new protocols at run-time. 1032be4f3cd0SPaolo PisatiTo achieve this feature, all the code for protocol handling was moved 1033be4f3cd0SPaolo Pisatito a series of modules outside of the main library. 1034f3d9aab3SPaolo PisatiThese modules are compiled from the same sources but work in 1035be4f3cd0SPaolo Pisatidifferent ways, depending on whether they are compiled to work inside a kernel 1036be4f3cd0SPaolo Pisatior as part of the userland library. 1037be4f3cd0SPaolo Pisati.Ss LIBALIAS MODULES IN KERNEL LAND 1038f3d9aab3SPaolo PisatiWhen compiled for the kernel, 1039f3d9aab3SPaolo Pisati.Nm 1040cc81ddd9SRuslan Ermilovmodules are plain KLDs recognizable with the 1041cc81ddd9SRuslan Ermilov.Pa alias_ 1042cc81ddd9SRuslan Ermilovprefix. 1043be4f3cd0SPaolo Pisati.Pp 1044f3d9aab3SPaolo PisatiTo add support for a new protocol, load the corresponding module. 1045f3d9aab3SPaolo PisatiFor example: 1046be4f3cd0SPaolo Pisati.Pp 1047f3d9aab3SPaolo Pisati.Dl "kldload alias_ftp" 1048be4f3cd0SPaolo Pisati.Pp 1049f3d9aab3SPaolo PisatiWhen support for a protocol is no longer needed, its module can be unloaded: 1050be4f3cd0SPaolo Pisati.Pp 1051f3d9aab3SPaolo Pisati.Dl "kldunload alias_ftp" 1052be4f3cd0SPaolo Pisati.Ss LIBALIAS MODULES IN USERLAND 1053f3d9aab3SPaolo PisatiDue to the differences between kernel and userland (no KLD mechanism, 1054f3d9aab3SPaolo Pisatimany different address spaces, etc.), we had to change a bit how to 1055f3d9aab3SPaolo Pisatihandle module loading/tracking/unloading in userland. 1056be4f3cd0SPaolo Pisati.Pp 1057be4f3cd0SPaolo PisatiWhile compiled for a userland 1058f3d9aab3SPaolo Pisati.Nm , 1059cc81ddd9SRuslan Ermilovall the modules are plain libraries, residing in 1060cc81ddd9SRuslan Ermilov.Pa /usr/lib , 1061cc81ddd9SRuslan Ermilovand recognizable with the 1062cc81ddd9SRuslan Ermilov.Pa libalias_ 1063cc81ddd9SRuslan Ermilovprefix. 1064be4f3cd0SPaolo Pisati.Pp 1065f3d9aab3SPaolo PisatiThere is a configuration file, 1066f3d9aab3SPaolo Pisati.Pa /etc/libalias.conf , 1067f3d9aab3SPaolo Pisatiwith the following contents (by default): 1068f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1069f3d9aab3SPaolo Pisati/usr/lib/libalias_cuseeme.so 1070f3d9aab3SPaolo Pisati/usr/lib/libalias_ftp.so 1071f3d9aab3SPaolo Pisati/usr/lib/libalias_irc.so 1072f3d9aab3SPaolo Pisati/usr/lib/libalias_nbt.so 1073f3d9aab3SPaolo Pisati/usr/lib/libalias_pptp.so 1074f3d9aab3SPaolo Pisati/usr/lib/libalias_skinny.so 1075f3d9aab3SPaolo Pisati/usr/lib/libalias_smedia.so 1076f3d9aab3SPaolo Pisati.Ed 1077be4f3cd0SPaolo Pisati.Pp 1078f3d9aab3SPaolo PisatiThis file contains the paths to the modules that 1079f3d9aab3SPaolo Pisati.Nm 1080be4f3cd0SPaolo Pisatiwill load. 1081f3d9aab3SPaolo PisatiTo load/unload a new module, just add its path to 1082f3d9aab3SPaolo Pisati.Pa libalias.conf 1083cc81ddd9SRuslan Ermilovand call 1084cc81ddd9SRuslan Ermilov.Fn LibAliasRefreshModules 1085cc81ddd9SRuslan Ermilovfrom the program. 1086cc81ddd9SRuslan ErmilovIn case the application provides a 1087cc81ddd9SRuslan Ermilov.Dv SIGHUP 1088cc81ddd9SRuslan Ermilovsignal handler, add a call to 1089cc81ddd9SRuslan Ermilov.Fn LibAliasRefreshModules 1090cc81ddd9SRuslan Ermilovinside the handler, and everytime you want to refresh the loaded modules, 1091cc81ddd9SRuslan Ermilovsend it the 1092f3d9aab3SPaolo Pisati.Dv SIGHUP 1093f3d9aab3SPaolo Pisatisignal: 1094be4f3cd0SPaolo Pisati.Pp 1095f3d9aab3SPaolo Pisati.Dl "kill -HUP <process_pid>" 1096be4f3cd0SPaolo Pisati.Ss MODULAR ARCHITECURE: HOW IT WORKS 1097be4f3cd0SPaolo PisatiThe modular architecture of 1098f3d9aab3SPaolo Pisati.Nm 1099f3d9aab3SPaolo Pisatiworks similar whether it is running inside the 1100f3d9aab3SPaolo Pisatikernel or in userland. 1101f3d9aab3SPaolo PisatiFrom 1102f3d9aab3SPaolo Pisati.Pa alias_mod.c : 1103be4f3cd0SPaolo Pisati.Bd -literal 1104f3d9aab3SPaolo Pisati/* Protocol and userland module handlers chains. */ 1105cc81ddd9SRuslan ErmilovLIST_HEAD(handler_chain, proto_handler) handler_chain ... 1106f3d9aab3SPaolo Pisati\&... 1107cc81ddd9SRuslan ErmilovSLIST_HEAD(dll_chain, dll) dll_chain ... 1108f3d9aab3SPaolo Pisati.Ed 1109f3d9aab3SPaolo Pisati.Pp 1110f3d9aab3SPaolo Pisati.Va handler_chain 1111f3d9aab3SPaolo Pisatikeep tracks of all the protocol handlers loaded, while 1112f3d9aab3SPaolo Pisati.Va ddl_chain 1113f3d9aab3SPaolo Pisatitakes care of userland modules loaded. 1114f3d9aab3SPaolo Pisati.Pp 1115f3d9aab3SPaolo Pisati.Va handler_chain 1116f3d9aab3SPaolo Pisatiis composed of 1117f3d9aab3SPaolo Pisati.Vt "struct proto_handler" 1118f3d9aab3SPaolo Pisatientries: 1119f3d9aab3SPaolo Pisati.Bd -literal 1120be4f3cd0SPaolo Pisatistruct proto_handler { 1121f3d9aab3SPaolo Pisati u_int pri; 1122be4f3cd0SPaolo Pisati int16_t dir; 1123f3d9aab3SPaolo Pisati uint8_t proto; 1124be4f3cd0SPaolo Pisati int (*fingerprint)(struct libalias *la, 1125be4f3cd0SPaolo Pisati struct ip *pip, struct alias_data *ah); 1126be4f3cd0SPaolo Pisati int (*protohandler)(struct libalias *la, 1127be4f3cd0SPaolo Pisati struct ip *pip, struct alias_data *ah); 1128f3d9aab3SPaolo Pisati LIST_ENTRY(proto_handler) entries; 1129be4f3cd0SPaolo Pisati}; 1130be4f3cd0SPaolo Pisati.Ed 1131be4f3cd0SPaolo Pisati.Pp 1132be4f3cd0SPaolo Pisatiwhere: 1133f3d9aab3SPaolo Pisati.Bl -inset 1134f3d9aab3SPaolo Pisati.It Va pri 1135f3d9aab3SPaolo Pisatiis the priority assigned to a protocol handler, lower 1136be4f3cd0SPaolo Pisatiis better. 1137f3d9aab3SPaolo Pisati.It Va dir 1138f3d9aab3SPaolo Pisatiis the direction of packets: ingoing or outgoing. 1139f3d9aab3SPaolo Pisati.It Va proto 1140f3d9aab3SPaolo Pisatisays at which protocol this packet belongs: IP, TCP or UDP. 1141f3d9aab3SPaolo Pisati.It Va fingerprint 1142f3d9aab3SPaolo Pisatipoints to the fingerprint function while protohandler points 1143be4f3cd0SPaolo Pisatito the protocol handler function. 1144f3d9aab3SPaolo Pisati.El 1145be4f3cd0SPaolo Pisati.Pp 1146f3d9aab3SPaolo PisatiThe 1147f3d9aab3SPaolo Pisati.Va fingerprint 1148f3d9aab3SPaolo Pisatifunction has the double of scope of checking if the 1149f3d9aab3SPaolo Pisatiincoming packet is found and if it belongs to any categories that this 1150be4f3cd0SPaolo Pisatimodule can handle. 1151be4f3cd0SPaolo Pisati.Pp 1152f3d9aab3SPaolo PisatiThe 1153f3d9aab3SPaolo Pisati.Va protohandler 1154f3d9aab3SPaolo Pisatifunction actually manipulates 1155be4f3cd0SPaolo Pisatithe packet to make 1156f3d9aab3SPaolo Pisati.Nm 1157f3d9aab3SPaolo Pisaticorrectly NAT it. 1158be4f3cd0SPaolo Pisati.Pp 1159be4f3cd0SPaolo PisatiWhen a packet enters 1160f3d9aab3SPaolo Pisati.Nm , 1161f3d9aab3SPaolo Pisatiif it meets a module hook, 1162f3d9aab3SPaolo Pisati.Va handler_chain 1163f3d9aab3SPaolo Pisatiis searched to see if there is an handler that matches 1164f3d9aab3SPaolo Pisatithis type of a packet (it checks protocol and direction of packet), then if 1165f3d9aab3SPaolo Pisatimore than one handler is found, it starts with the module with 1166f3d9aab3SPaolo Pisatithe lowest priority number: it calls the 1167f3d9aab3SPaolo Pisati.Va fingerprint 1168f3d9aab3SPaolo Pisatifunction and interprets the result. 1169be4f3cd0SPaolo Pisati.Pp 1170f3d9aab3SPaolo PisatiIf the result value is equal to 0 then it calls the protocol handler 1171f3d9aab3SPaolo Pisatiof this handler and returns. 1172f3d9aab3SPaolo PisatiOtherwise, it proceeds to the next eligible module until the 1173f3d9aab3SPaolo Pisati.Va handler_chain 1174f3d9aab3SPaolo Pisatiis exhausted. 1175be4f3cd0SPaolo Pisati.Pp 1176be4f3cd0SPaolo PisatiInside 1177f3d9aab3SPaolo Pisati.Nm , 1178be4f3cd0SPaolo Pisatithe module hook looks like this: 1179f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1180be4f3cd0SPaolo Pisatistruct alias_data ad = { 1181be4f3cd0SPaolo Pisati lnk, 1182be4f3cd0SPaolo Pisati &original_address, 1183be4f3cd0SPaolo Pisati &alias_address, 1184be4f3cd0SPaolo Pisati &alias_port, 1185be4f3cd0SPaolo Pisati &ud->uh_sport, /* original source port */ 1186be4f3cd0SPaolo Pisati &ud->uh_dport, /* original dest port */ 1187be4f3cd0SPaolo Pisati 256 /* maxpacketsize */ 1188be4f3cd0SPaolo Pisati}; 1189be4f3cd0SPaolo Pisati 1190f3d9aab3SPaolo Pisati\&... 1191be4f3cd0SPaolo Pisati 1192be4f3cd0SPaolo Pisati/* walk out chain */ 1193be4f3cd0SPaolo Pisatierr = find_handler(IN, UDP, la, pip, &ad); 1194be4f3cd0SPaolo Pisati.Ed 1195f3d9aab3SPaolo Pisati.Pp 1196f3d9aab3SPaolo PisatiAll data useful to a module are gathered together in an 1197f3d9aab3SPaolo Pisati.Vt alias_data 1198f3d9aab3SPaolo Pisatistructure, then 1199f3d9aab3SPaolo Pisati.Fn find_handler 1200f3d9aab3SPaolo Pisatiis called. 1201f3d9aab3SPaolo PisatiThe 1202f3d9aab3SPaolo Pisati.Fn find_handler 1203f3d9aab3SPaolo Pisatifunction is responsible for walking out the handler 1204be4f3cd0SPaolo Pisatichain, it receives as input parameters: 1205f3d9aab3SPaolo Pisati.Bl -tag -width indent 1206f3d9aab3SPaolo Pisati.It Fa IN 1207f3d9aab3SPaolo Pisatidirection 1208f3d9aab3SPaolo Pisati.It Fa UDP 1209f3d9aab3SPaolo Pisatiworking protocol 1210f3d9aab3SPaolo Pisati.It Fa la 1211f3d9aab3SPaolo Pisatipointer to this instance of libalias 1212f3d9aab3SPaolo Pisati.It Fa pip 1213f3d9aab3SPaolo Pisatipointer to a 1214f3d9aab3SPaolo Pisati.Vt "struct ip" 1215f3d9aab3SPaolo Pisati.It Fa ad 1216f3d9aab3SPaolo Pisatipointer to 1217f3d9aab3SPaolo Pisati.Vt "struct alias_data" 1218f3d9aab3SPaolo Pisati(see above) 1219f3d9aab3SPaolo Pisati.El 1220be4f3cd0SPaolo Pisati.Pp 1221f3d9aab3SPaolo PisatiIn this case, 1222f3d9aab3SPaolo Pisati.Fn find_handler 1223f3d9aab3SPaolo Pisatiwill search only for modules registered for 1224be4f3cd0SPaolo Pisatisupporting INcoming UDP packets. 1225be4f3cd0SPaolo Pisati.Pp 1226f3d9aab3SPaolo PisatiAs was mentioned earlier, 1227f3d9aab3SPaolo Pisati.Nm 1228f3d9aab3SPaolo Pisatiin userland is a bit different, cause 1229f3d9aab3SPaolo Pisaticare has to be taken of module handling too (avoiding duplicate load of 1230f3d9aab3SPaolo Pisatimodule, avoiding module with same name, etc.) so 1231f3d9aab3SPaolo Pisati.Va dll_chain 1232f3d9aab3SPaolo Pisatiwas introduced. 1233be4f3cd0SPaolo Pisati.Pp 1234f3d9aab3SPaolo Pisati.Va dll_chain 1235f3d9aab3SPaolo Pisaticontains a list of all userland 1236f3d9aab3SPaolo Pisati.Nm 1237be4f3cd0SPaolo Pisatimodules loaded. 1238be4f3cd0SPaolo Pisati.Pp 1239f3d9aab3SPaolo PisatiWhen an application calls 1240f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules , 1241f3d9aab3SPaolo Pisati.Nm 1242f3d9aab3SPaolo Pisatifirst unloads all the loaded modules, then reloads all the modules listed in 1243f3d9aab3SPaolo Pisati.Pa /etc/libalias.conf : 1244f3d9aab3SPaolo Pisatifor every module loaded, a new entry to 1245f3d9aab3SPaolo Pisati.Va dll_chain 1246be4f3cd0SPaolo Pisatiis added. 1247be4f3cd0SPaolo Pisati.Pp 1248f3d9aab3SPaolo Pisati.Va dll_chain 1249f3d9aab3SPaolo Pisatiis composed of 1250f3d9aab3SPaolo Pisati.Vt "struct dll" 1251f3d9aab3SPaolo Pisatientries: 1252be4f3cd0SPaolo Pisati.Bd -literal 1253be4f3cd0SPaolo Pisatistruct dll { 1254be4f3cd0SPaolo Pisati /* name of module */ 1255be4f3cd0SPaolo Pisati char name[DLL_LEN]; 1256be4f3cd0SPaolo Pisati /* 1257be4f3cd0SPaolo Pisati * ptr to shared obj obtained through 1258be4f3cd0SPaolo Pisati * dlopen() - use this ptr to get access 1259be4f3cd0SPaolo Pisati * to any symbols from a loaded module 1260be4f3cd0SPaolo Pisati * via dlsym() 1261be4f3cd0SPaolo Pisati */ 1262be4f3cd0SPaolo Pisati void *handle; 1263be4f3cd0SPaolo Pisati struct dll *next; 1264be4f3cd0SPaolo Pisati}; 1265be4f3cd0SPaolo Pisati.Ed 1266f3d9aab3SPaolo Pisati.Bl -inset 1267f3d9aab3SPaolo Pisati.It Va name 1268f3d9aab3SPaolo Pisatiis the name of the module 1269f3d9aab3SPaolo Pisati.It Va handle 1270f3d9aab3SPaolo Pisatiis a pointer to the module obtained through 1271f3d9aab3SPaolo Pisati.Xr dlopen 3 1272f3d9aab3SPaolo Pisati.El 1273be4f3cd0SPaolo PisatiWhenever a module is loaded in userland, an entry is added to 1274f3d9aab3SPaolo Pisati.Va dll_chain , 1275f3d9aab3SPaolo Pisatithen every protocol handler present in that module 1276f3d9aab3SPaolo Pisatiis resolved and registered in 1277f3d9aab3SPaolo Pisati.Va handler_chain . 1278be4f3cd0SPaolo Pisati.Ss HOW TO WRITE A MODULE FOR LIBALIAS 1279f3d9aab3SPaolo PisatiThere is a module (called 1280f3d9aab3SPaolo Pisati.Pa alias_dummy.[ch] ) 1281f3d9aab3SPaolo Pisatiin 1282f3d9aab3SPaolo Pisati.Nm 1283be4f3cd0SPaolo Pisatithat can be used as a skeleton for future work, here we analyse some parts of that 1284be4f3cd0SPaolo Pisatimodule. 1285f3d9aab3SPaolo PisatiFrom 1286f3d9aab3SPaolo Pisati.Pa alias_dummy.c : 1287be4f3cd0SPaolo Pisati.Bd -literal 1288be4f3cd0SPaolo Pisatistruct proto_handler handlers [] = {{666, IN|OUT, UDP|TCP, 1289be4f3cd0SPaolo Pisati &fingerprint, &protohandler}}; 1290be4f3cd0SPaolo Pisati.Ed 1291be4f3cd0SPaolo Pisati.Pp 1292f3d9aab3SPaolo PisatiThe variable 1293f3d9aab3SPaolo Pisati.Va handlers 1294f3d9aab3SPaolo Pisatiis the 1295f3d9aab3SPaolo Pisati.Dq "most important thing" 1296f3d9aab3SPaolo Pisatiin a module 1297f3d9aab3SPaolo Pisaticause it describes the handlers present and lets the outside world use 1298be4f3cd0SPaolo Pisatiit in an opaque way. 1299be4f3cd0SPaolo Pisati.Pp 1300be4f3cd0SPaolo PisatiIt must ALWAYS be present in every module, and it MUST retain 1301f3d9aab3SPaolo Pisatithe name 1302f3d9aab3SPaolo Pisati.Va handlers , 1303f3d9aab3SPaolo Pisatiotherwise attempting to load a module in userland will fail and 1304f3d9aab3SPaolo Pisaticomplain about missing symbols: for more information about module 1305f3d9aab3SPaolo Pisatiload/unload, please refer to 1306f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules , 1307f3d9aab3SPaolo Pisati.Fn LibAliasLoadModule 1308f3d9aab3SPaolo Pisatiand 1309f3d9aab3SPaolo Pisati.Fn LibAliasUnloadModule 1310f3d9aab3SPaolo Pisatiin 1311f3d9aab3SPaolo Pisati.Pa alias.c . 1312be4f3cd0SPaolo Pisati.Pp 1313f3d9aab3SPaolo Pisati.Va handlers 1314f3d9aab3SPaolo Pisaticontains all the 1315f3d9aab3SPaolo Pisati.Vt proto_handler 1316f3d9aab3SPaolo Pisatistructures present in a module. 1317be4f3cd0SPaolo Pisati.Bd -literal 1318be4f3cd0SPaolo Pisatistatic int 1319be4f3cd0SPaolo Pisatimod_handler(module_t mod, int type, void *data) 1320be4f3cd0SPaolo Pisati{ 1321be4f3cd0SPaolo Pisati int error; 1322be4f3cd0SPaolo Pisati 1323be4f3cd0SPaolo Pisati switch (type) { 1324be4f3cd0SPaolo Pisati case MOD_LOAD: 1325be4f3cd0SPaolo Pisati error = 0; 1326be4f3cd0SPaolo Pisati attach_handlers(handlers); 1327be4f3cd0SPaolo Pisati break; 1328be4f3cd0SPaolo Pisati case MOD_UNLOAD: 1329be4f3cd0SPaolo Pisati error = 0; 1330be4f3cd0SPaolo Pisati detach_handlers(handlers; 1331be4f3cd0SPaolo Pisati break; 1332be4f3cd0SPaolo Pisati default: 1333be4f3cd0SPaolo Pisati error = EINVAL; 1334be4f3cd0SPaolo Pisati } 1335be4f3cd0SPaolo Pisati return (error); 1336be4f3cd0SPaolo Pisati} 1337be4f3cd0SPaolo Pisati.Ed 1338f3d9aab3SPaolo PisatiWhen running as KLD, 1339f3d9aab3SPaolo Pisati.Fn mod_handler 1340f3d9aab3SPaolo Pisatiregister/deregister the module using 1341f3d9aab3SPaolo Pisati.Fn attach_handlers 1342f3d9aab3SPaolo Pisatiand 1343f3d9aab3SPaolo Pisati.Fn detach_handlers , 1344f3d9aab3SPaolo Pisatirespectively. 1345be4f3cd0SPaolo Pisati.Pp 1346be4f3cd0SPaolo PisatiEvery module must contain at least 2 functions: one fingerprint 1347be4f3cd0SPaolo Pisatifunction and a protocol handler function. 1348be4f3cd0SPaolo Pisati.Bd -literal 1349be4f3cd0SPaolo Pisati#ifdef _KERNEL 1350be4f3cd0SPaolo Pisatistatic 1351be4f3cd0SPaolo Pisati#endif 1352be4f3cd0SPaolo Pisatiint 1353be4f3cd0SPaolo Pisatifingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah) 1354be4f3cd0SPaolo Pisati{ 1355be4f3cd0SPaolo Pisati 1356f3d9aab3SPaolo Pisati\&... 1357be4f3cd0SPaolo Pisati} 1358be4f3cd0SPaolo Pisati 1359be4f3cd0SPaolo Pisati#ifdef _KERNEL 1360be4f3cd0SPaolo Pisatistatic 1361be4f3cd0SPaolo Pisati#endif 1362be4f3cd0SPaolo Pisatiint 1363be4f3cd0SPaolo Pisatiprotohandler(struct libalias *la, struct ip *pip, 1364be4f3cd0SPaolo Pisati struct alias_data *ah) 1365be4f3cd0SPaolo Pisati{ 1366be4f3cd0SPaolo Pisati 1367f3d9aab3SPaolo Pisati\&... 1368be4f3cd0SPaolo Pisati} 1369be4f3cd0SPaolo Pisati.Ed 1370be4f3cd0SPaolo Pisatiand they must accept exactly these input parameters. 1371be4f3cd0SPaolo Pisati.Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES 1372f3d9aab3SPaolo PisatiTo add module support into an application that uses 1373f3d9aab3SPaolo Pisati.Nm , 1374f3d9aab3SPaolo Pisatithe following simple steps can be followed. 1375f3d9aab3SPaolo Pisati.Bl -enum 1376f3d9aab3SPaolo Pisati.It 1377f3d9aab3SPaolo PisatiFind the main file of an application 1378f3d9aab3SPaolo Pisati(let us call it 1379f3d9aab3SPaolo Pisati.Pa main.c ) . 1380f3d9aab3SPaolo Pisati.It 1381f3d9aab3SPaolo PisatiAdd this to the header section of 1382f3d9aab3SPaolo Pisati.Pa main.c , 1383f3d9aab3SPaolo Pisatiif not already present: 1384be4f3cd0SPaolo Pisati.Pp 1385f3d9aab3SPaolo Pisati.Dl "#include <signal.h>" 1386be4f3cd0SPaolo Pisati.Pp 1387f3d9aab3SPaolo Pisatiand this just after the header section: 1388be4f3cd0SPaolo Pisati.Pp 1389f3d9aab3SPaolo Pisati.Dl "static void signal_handler(int);" 1390f3d9aab3SPaolo Pisati.It 1391f3d9aab3SPaolo PisatiAdd the following line to the init function of an application or, 1392f3d9aab3SPaolo Pisatiif it does not have any init function, put it in 1393f3d9aab3SPaolo Pisati.Fn main : 1394be4f3cd0SPaolo Pisati.Pp 1395f3d9aab3SPaolo Pisati.Dl "signal(SIGHUP, signal_handler);" 1396be4f3cd0SPaolo Pisati.Pp 1397f3d9aab3SPaolo Pisatiand place the 1398f3d9aab3SPaolo Pisati.Fn signal_handler 1399f3d9aab3SPaolo Pisatifunction somewhere in 1400f3d9aab3SPaolo Pisati.Pa main.c : 1401f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1402f3d9aab3SPaolo Pisatistatic void 1403f3d9aab3SPaolo Pisatisignal_handler(int sig) 1404f3d9aab3SPaolo Pisati{ 1405f3d9aab3SPaolo Pisati 1406f3d9aab3SPaolo Pisati LibAliasRefreshModules(); 1407f3d9aab3SPaolo Pisati} 1408be4f3cd0SPaolo Pisati.Ed 1409be4f3cd0SPaolo Pisati.Pp 1410f3d9aab3SPaolo PisatiOtherwise, if an application already traps the 1411f3d9aab3SPaolo Pisati.Dv SIGHUP 1412f3d9aab3SPaolo Pisatisignal, just add a call to 1413f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules 1414f3d9aab3SPaolo Pisatiin the signal handler function. 1415f3d9aab3SPaolo Pisati.El 1416f3d9aab3SPaolo PisatiFor example, to patch 1417f3d9aab3SPaolo Pisati.Xr natd 8 1418f3d9aab3SPaolo Pisatito use 1419f3d9aab3SPaolo Pisati.Nm 1420f3d9aab3SPaolo Pisatimodules, just add the following line to 1421f3d9aab3SPaolo Pisati.Fn RefreshAddr "int sig __unused" : 1422f3d9aab3SPaolo Pisati.Pp 1423f3d9aab3SPaolo Pisati.Dl "LibAliasRefreshModules()" 1424f3d9aab3SPaolo Pisati.Pp 1425f3d9aab3SPaolo Pisatirecompile and you are done. 1426f3d9aab3SPaolo Pisati.Ss LOGGING SUPPORT IN KERNEL LAND 1427f3d9aab3SPaolo PisatiWhen working as KLD, 1428f3d9aab3SPaolo Pisati.Nm 1429f3d9aab3SPaolo Pisatinow has log support that 1430f3d9aab3SPaolo Pisatihappens on a buffer allocated inside 1431f3d9aab3SPaolo Pisati.Vt "struct libalias" 1432f3d9aab3SPaolo Pisati(from 1433f3d9aab3SPaolo Pisati.Pa alias_local.h ) : 1434be4f3cd0SPaolo Pisati.Bd -literal 1435be4f3cd0SPaolo Pisatistruct libalias { 1436be4f3cd0SPaolo Pisati ... 1437be4f3cd0SPaolo Pisati 1438be4f3cd0SPaolo Pisati /* log descriptor */ 1439be4f3cd0SPaolo Pisati#ifdef KERNEL_LOG 1440be4f3cd0SPaolo Pisati char *logDesc; /* 1441be4f3cd0SPaolo Pisati * ptr to an auto-malloced 1442be4f3cd0SPaolo Pisati * memory buffer when libalias 1443be4f3cd0SPaolo Pisati * works as kld 1444be4f3cd0SPaolo Pisati */ 1445be4f3cd0SPaolo Pisati#else 1446be4f3cd0SPaolo Pisati FILE *logDesc; /* 1447be4f3cd0SPaolo Pisati * ptr to /var/log/alias.log 1448be4f3cd0SPaolo Pisati * when libalias runs as a 1449be4f3cd0SPaolo Pisati * userland lib 1450be4f3cd0SPaolo Pisati */ 1451be4f3cd0SPaolo Pisati#endif 1452be4f3cd0SPaolo Pisati 1453be4f3cd0SPaolo Pisati ... 1454be4f3cd0SPaolo Pisati} 1455be4f3cd0SPaolo Pisati.Ed 1456f3d9aab3SPaolo Pisatiso all applications using 1457f3d9aab3SPaolo Pisati.Nm 1458f3d9aab3SPaolo Pisatiwill be able to handle their 1459f3d9aab3SPaolo Pisatiown logs, if they want, accessing 1460f3d9aab3SPaolo Pisati.Va logDesc . 1461f3d9aab3SPaolo PisatiMoreover, every change to a log buffer is automatically added to 1462f3d9aab3SPaolo Pisati.Xr syslog 3 1463f3d9aab3SPaolo Pisatiwith the 1464f3d9aab3SPaolo Pisati.Dv LOG_SECURITY 1465f3d9aab3SPaolo Pisatifacility and the 1466f3d9aab3SPaolo Pisati.Dv LOG_INFO 1467f3d9aab3SPaolo Pisatilevel. 1468