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.\" 2661bf830cSDamjan Jovanovic.Dd November 29, 2024 2774804d58SMike Pritchard.Dt LIBALIAS 3 28a307d598SRuslan Ermilov.Os 293efa11bbSBrian Somers.Sh NAME 3074804d58SMike Pritchard.Nm libalias 31a2900666SRuslan Ermilov.Nd packet aliasing library for masquerading and network address translation 323efa11bbSBrian Somers.Sh SYNOPSIS 3332eef9aeSRuslan Ermilov.In sys/types.h 3432eef9aeSRuslan Ermilov.In netinet/in.h 3532eef9aeSRuslan Ermilov.In alias.h 36a2900666SRuslan Ermilov.Pp 37a2900666SRuslan ErmilovFunction prototypes are given in the main body of the text. 3874804d58SMike Pritchard.Sh DESCRIPTION 3974804d58SMike PritchardThe 4074804d58SMike Pritchard.Nm 41a2900666SRuslan Ermilovlibrary is a collection of functions for aliasing and de-aliasing of IP 42a2900666SRuslan Ermilovpackets, intended for masquerading and network address translation (NAT). 43a2900666SRuslan Ermilov.Sh INTRODUCTION 44a2900666SRuslan ErmilovThis library is a moderately portable set of functions designed to assist 45a2900666SRuslan Ermilovin the process of IP masquerading and network address translation. 46a2900666SRuslan ErmilovOutgoing packets from a local network with unregistered IP addresses can 47a2900666SRuslan Ermilovbe aliased to appear as if they came from an accessible IP address. 48a2900666SRuslan ErmilovIncoming packets are then de-aliased so that they are sent to the correct 49a2900666SRuslan Ermilovmachine on the local network. 50a2900666SRuslan Ermilov.Pp 51a2900666SRuslan ErmilovA certain amount of flexibility is built into the packet aliasing engine. 52a2900666SRuslan ErmilovIn the simplest mode of operation, a many-to-one address mapping takes 53ff19f85dSGlen Barberplace between the local network and the packet aliasing host. 54a2900666SRuslan ErmilovThis is known as IP masquerading. 55a2900666SRuslan ErmilovIn addition, one-to-one mappings between local and public addresses can 56a2900666SRuslan Ermilovalso be implemented, which is known as static NAT. 57a2900666SRuslan ErmilovIn between these extremes, different groups of private addresses can be 58a2900666SRuslan Ermilovlinked to different public addresses, comprising several distinct 59a2900666SRuslan Ermilovmany-to-one mappings. 60a2900666SRuslan ErmilovAlso, a given public address and port can be statically redirected to a 61a2900666SRuslan Ermilovprivate address/port. 62a2900666SRuslan Ermilov.Sh INITIALIZATION AND CONTROL 63f1a529f3SRuslan ErmilovOne special function, 645e289f9eSPoul-Henning Kamp.Fn LibAliasInit , 65ff19f85dSGlen Barbermust always be called before any packet handling may be performed, and 66ff19f85dSGlen Barberthe returned instance pointer must be passed to all the other functions. 67f1a529f3SRuslan ErmilovNormally, the 685e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 69f1a529f3SRuslan Ermilovfunction is called afterwards, to set the default aliasing address. 70a2900666SRuslan ErmilovIn addition, the operating mode of the packet aliasing engine can be 71a2900666SRuslan Ermilovcustomized by calling 725e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode . 73a2900666SRuslan Ermilov.Pp 745e289f9eSPoul-Henning Kamp.Ft "struct libalias *" 755e289f9eSPoul-Henning Kamp.Fn LibAliasInit "struct libalias *" 76a2900666SRuslan Ermilov.Bd -ragged -offset indent 775e289f9eSPoul-Henning KampThis function is used to initialize 78a2900666SRuslan Ermilovinternal data structures. 79c9a24641SRuslan ErmilovWhen called the first time, a 80c9a24641SRuslan Ermilov.Dv NULL 81c9a24641SRuslan Ermilovpointer should be passed as an argument. 82a2900666SRuslan ErmilovThe following mode bits are always set after calling 835e289f9eSPoul-Henning Kamp.Fn LibAliasInit . 84a2900666SRuslan ErmilovSee the description of 855e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode 86a2900666SRuslan Ermilovbelow for the meaning of these mode bits. 87a2900666SRuslan Ermilov.Pp 88a2900666SRuslan Ermilov.Bl -item -offset indent -compact 89a2900666SRuslan Ermilov.It 90a2900666SRuslan Ermilov.Dv PKT_ALIAS_SAME_PORTS 91a2900666SRuslan Ermilov.It 92a2900666SRuslan Ermilov.Dv PKT_ALIAS_USE_SOCKETS 93a2900666SRuslan Ermilov.It 94a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 95a2900666SRuslan Ermilov.El 96a2900666SRuslan Ermilov.Pp 97a2900666SRuslan ErmilovThis function will always return the packet aliasing engine to the same 98a2900666SRuslan Ermilovinitial state. 999703a107SPhilippe CharnierThe 1005e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 1019703a107SPhilippe Charnierfunction is normally called afterwards, and any desired changes from the 1029703a107SPhilippe Charnierdefault mode bits listed above require a call to 1035e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode . 104a2900666SRuslan Ermilov.Pp 105a2900666SRuslan ErmilovIt is mandatory that this function be called at the beginning of a program 106a2900666SRuslan Ermilovprior to any packet handling. 107a2900666SRuslan Ermilov.Ed 108a2900666SRuslan Ermilov.Pp 1098ddc51bcSEivind Eklund.Ft void 1105e289f9eSPoul-Henning Kamp.Fn LibAliasUninit "struct libalias *" 111a2900666SRuslan Ermilov.Bd -ragged -offset indent 1125e289f9eSPoul-Henning KampThis function has no return value and is used to clear any 113a2900666SRuslan Ermilovresources attached to internal data structures. 114a2900666SRuslan Ermilov.Pp 115ff19f85dSGlen BarberThis function should be called when a program stops using the aliasing 116ff19f85dSGlen Barberengine; amongst other things, it clears out any firewall holes. 117a2900666SRuslan ErmilovTo provide backwards compatibility and extra security, it is added to 118a2900666SRuslan Ermilovthe 119a2900666SRuslan Ermilov.Xr atexit 3 120a2900666SRuslan Ermilovchain by 1215e289f9eSPoul-Henning Kamp.Fn LibAliasInit . 122a2900666SRuslan Ermilov.Ed 123a2900666SRuslan Ermilov.Pp 1243efa11bbSBrian Somers.Ft void 1255e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr" 126a2900666SRuslan Ermilov.Bd -ragged -offset indent 127a2900666SRuslan ErmilovThis function sets the source address to which outgoing packets from the 128a2900666SRuslan Ermilovlocal area network are aliased. 129a2900666SRuslan ErmilovAll outgoing packets are re-mapped to this address unless overridden by a 130a2900666SRuslan Ermilovstatic address mapping established by 1315e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 132ff19f85dSGlen BarberIf this function has not been called, and no static rules match, an outgoing 133f1a529f3SRuslan Ermilovpacket retains its source address. 134a2900666SRuslan Ermilov.Pp 135a2900666SRuslan ErmilovIf the 136a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 137a2900666SRuslan Ermilovmode bit is set (the default mode of operation), then the internal aliasing 138a2900666SRuslan Ermilovlink tables will be reset any time the aliasing address changes. 139a2900666SRuslan ErmilovThis is useful for interfaces such as 140a2900666SRuslan Ermilov.Xr ppp 8 , 141a2900666SRuslan Ermilovwhere the IP 142a2900666SRuslan Ermilovaddress may or may not change on successive dial-up attempts. 143a2900666SRuslan Ermilov.Pp 144a2900666SRuslan ErmilovIf the 145a2900666SRuslan Ermilov.Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE 146a2900666SRuslan Ermilovmode bit is set to zero, this function can also be used to dynamically change 147ff19f85dSGlen Barberthe aliasing address on a packet-to-packet basis (it is a low overhead call). 148a2900666SRuslan Ermilov.Pp 149a2900666SRuslan ErmilovIt is mandatory that this function be called prior to any packet handling. 150a2900666SRuslan Ermilov.Ed 151a2900666SRuslan Ermilov.Pp 152442a25bdSBruce Evans.Ft unsigned int 1535e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask" 154a2900666SRuslan Ermilov.Bd -ragged -offset indent 1553efa11bbSBrian SomersThis function sets or clears mode bits 1563efa11bbSBrian Somersaccording to the value of 157a2900666SRuslan Ermilov.Fa flags . 1583efa11bbSBrian SomersOnly bits marked in 159a2900666SRuslan Ermilov.Fa mask 160a2900666SRuslan Ermilovare affected. 161a2900666SRuslan ErmilovThe following mode bits are defined in 162fe08efe6SRuslan Ermilov.In alias.h : 163a2900666SRuslan Ermilov.Bl -tag -width indent 164a2900666SRuslan Ermilov.It Dv PKT_ALIAS_LOG 165a2900666SRuslan ErmilovEnables logging into 166a2900666SRuslan Ermilov.Pa /var/log/alias.log . 167ff19f85dSGlen BarberEach time an aliasing link is created or deleted, the log file is appended to 168a2900666SRuslan Ermilovwith the current number of ICMP, TCP and UDP links. 169a2900666SRuslan ErmilovMainly useful for debugging when the log file is viewed continuously with 170a2900666SRuslan Ermilov.Xr tail 1 . 171a2900666SRuslan Ermilov.It Dv PKT_ALIAS_DENY_INCOMING 172a2900666SRuslan ErmilovIf this mode bit is set, all incoming packets associated with new TCP 173a2900666SRuslan Ermilovconnections or new UDP transactions will be marked for being ignored 174c37a0a82SBaptiste Daroussin.Po 175c37a0a82SBaptiste Daroussin.Fn LibAliasIn 176a2900666SRuslan Ermilovreturns 177a2900666SRuslan Ermilov.Dv PKT_ALIAS_IGNORED 178c37a0a82SBaptiste Daroussincode 179c37a0a82SBaptiste Daroussin.Pc 180a2900666SRuslan Ermilovby the calling program. 181a2900666SRuslan ErmilovResponse packets to connections or transactions initiated from the packet 182a2900666SRuslan Ermilovaliasing host or local network will be unaffected. 183a2900666SRuslan ErmilovThis mode bit is useful for implementing a one-way firewall. 184a2900666SRuslan Ermilov.It Dv PKT_ALIAS_SAME_PORTS 185ff19f85dSGlen BarberIf this mode bit is set, the packet-aliasing engine will attempt to leave 186a2900666SRuslan Ermilovthe alias port numbers unchanged from the actual local port numbers. 187a2900666SRuslan ErmilovThis can be done as long as the quintuple (proto, alias addr, alias port, 188a2900666SRuslan Ermilovremote addr, remote port) is unique. 189a2900666SRuslan ErmilovIf a conflict exists, a new aliasing port number is chosen even if this 190a2900666SRuslan Ermilovmode bit is set. 191a2900666SRuslan Ermilov.It Dv PKT_ALIAS_USE_SOCKETS 192a2900666SRuslan ErmilovThis bit should be set when the packet aliasing host originates network 193a2900666SRuslan Ermilovtraffic as well as forwards it. 194a2900666SRuslan ErmilovWhen the packet aliasing host is waiting for a connection from an unknown 1951a0a9345SRuslan Ermilovhost address or unknown port number (e.g.\& an FTP data connection), this 196a2900666SRuslan Ermilovmode bit specifies that a socket be allocated as a place holder to prevent 197a2900666SRuslan Ermilovport conflicts. 198a2900666SRuslan ErmilovOnce a connection is established, usually within a minute or so, the socket 199a2900666SRuslan Ermilovis closed. 200a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNREGISTERED_ONLY 201a2900666SRuslan ErmilovIf this mode bit is set, traffic on the local network which does not 202a2900666SRuslan Ermilovoriginate from unregistered address spaces will be ignored. 203*f16ca171SOlivier CochardThe standard private IP address ranges are: 2046ae742a5SJoel Dahl.Pp 205*f16ca171SOlivier Cochard10.0.0.0 -> 10.255.255.255 (/8) 206*f16ca171SOlivier Cochard172.16.0.0 -> 172.31.255.255 (/16) 207*f16ca171SOlivier Cochard192.168.0.0 -> 192.168.255.255 (/24) 208a2900666SRuslan Ermilov.Pp 209ff19f85dSGlen BarberThis option is useful in the case that the packet aliasing host has both 210a2900666SRuslan Ermilovregistered and unregistered subnets on different interfaces. 211a2900666SRuslan ErmilovThe registered subnet is fully accessible to the outside world, so traffic 212a2900666SRuslan Ermilovfrom it does not need to be passed through the packet aliasing engine. 21375b89337SAlexander V. Chernikov.It Dv PKT_ALIAS_UNREGISTERED_CGN 21475b89337SAlexander V. ChernikovLike PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598 (Carrier Grade 21575b89337SAlexander V. ChernikovNAT) subnet as follows: 21675b89337SAlexander V. Chernikov.Pp 21775b89337SAlexander V. Chernikov100.64.0.0 -> 100.127.255.255 (RFC 6598 subnet) 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 232ff19f85dSGlen Barber.Dq punch holes 233ff19f85dSGlen Barberin an 234ff19f85dSGlen Barber.Xr ipfirewall 4 - 235a2900666SRuslan Ermilovbased firewall for FTP/IRC DCC connections. 236a2900666SRuslan ErmilovThe holes punched are bound by from/to IP address and port; it will not be 237a2900666SRuslan Ermilovpossible to use a hole for another connection. 238a2900666SRuslan ErmilovA hole is removed when the connection that uses it dies. 239a2900666SRuslan ErmilovTo cater to unexpected death of a program using 240a2900666SRuslan Ermilov.Nm 2411a0a9345SRuslan Ermilov(e.g.\& kill -9), 242a2900666SRuslan Ermilovchanging the state of the flag will clear the entire firewall range 243a2900666SRuslan Ermilovallocated for holes. 244ff19f85dSGlen BarberThis clearing will also happen on the initial call to 245ff19f85dSGlen Barber.Fn LibAliasSetFWBase , 246ff19f85dSGlen Barberwhich must happen prior to setting this flag. 247a2900666SRuslan Ermilov.It Dv PKT_ALIAS_REVERSE 248a2900666SRuslan ErmilovThis option makes 249a2900666SRuslan Ermilov.Nm 250a2900666SRuslan Ermilovreverse the way it handles incoming and outgoing packets, allowing it 251a2900666SRuslan Ermilovto be fed with data that passes through the internal interface rather 252a2900666SRuslan Ermilovthan the external one. 253a2900666SRuslan Ermilov.It Dv PKT_ALIAS_PROXY_ONLY 254a2900666SRuslan ErmilovThis option tells 255a2900666SRuslan Ermilov.Nm 256a2900666SRuslan Ermilovto obey transparent proxy rules only. 257a2900666SRuslan ErmilovNormal packet aliasing is not performed. 25821b9df57SBrian SomersSee 2595e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 26021b9df57SBrian Somersbelow for details. 26162b6e03aSAndrey V. Elsukov.It Dv PKT_ALIAS_SKIP_GLOBAL 26262b6e03aSAndrey V. ElsukovThis option is used by 26362b6e03aSAndrey V. Elsukov.Pa ipfw_nat 264ff19f85dSGlen Barberonly. 265ff19f85dSGlen BarberSpecifying it as a flag to 26662b6e03aSAndrey V. Elsukov.Fn LibAliasSetMode 267ff19f85dSGlen Barberhas no effect. 268ff19f85dSGlen BarberSee section 26962b6e03aSAndrey V. Elsukov.Sx NETWORK ADDRESS TRANSLATION 27062b6e03aSAndrey V. Elsukovin 27162b6e03aSAndrey V. Elsukov.Xr ipfw 8 27262b6e03aSAndrey V. Elsukovfor more details. 27361bf830cSDamjan Jovanovic.It Dv PKT_ALIAS_UDP_EIM 27461bf830cSDamjan JovanovicWhen this bit is set, UDP uses endpoint-independent mapping (EIM), as per 27561bf830cSDamjan JovanovicRFC 4787 ("full cone" NAT of RFC 3489). 27661bf830cSDamjan JovanovicAll packets from the same internal address:port are mapped to the same NAT 27761bf830cSDamjan Jovanovicaddress:port, regardless of their destination address:port. 27861bf830cSDamjan JovanovicIf filtering rules allow, and if 27961bf830cSDamjan Jovanovic.Em PKT_ALIAS_DENY_INCOMING 28061bf830cSDamjan Jovanovicis unset, any other external address:port can 28161bf830cSDamjan Jovanovicalso send to the internal address:port through its mapped NAT address:port. 28261bf830cSDamjan JovanovicThis is more compatible with applications, and can reduce the need for port 28361bf830cSDamjan Jovanovicforwarding, but less scalable as each NAT address:port can only be 28461bf830cSDamjan Jovanovicconcurrently used by at most one internal address:port. 28561bf830cSDamjan Jovanovic.Pp 28661bf830cSDamjan JovanovicWhen this bit is unset, UDP packets use endpoint-dependent mapping (EDM) 28761bf830cSDamjan Jovanovic("symmetric" NAT). 28861bf830cSDamjan JovanovicEach connection from a particular internal address:port to different 28961bf830cSDamjan Jovanovicexternal addresses:ports is mapped to a random and unpredictable NAT 29061bf830cSDamjan Jovanovicaddress:port. 29161bf830cSDamjan JovanovicTwo appplications behind EDM NATs can only connect to each other 29261bf830cSDamjan Jovanovicby port forwarding on the NAT, or tunnelling through an in-between server. 2933efa11bbSBrian Somers.El 294a2900666SRuslan Ermilov.Ed 295a2900666SRuslan Ermilov.Pp 2968ddc51bcSEivind Eklund.Ft void 2975e289f9eSPoul-Henning Kamp.Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num" 298a2900666SRuslan Ermilov.Bd -ragged -offset indent 299ff19f85dSGlen BarberSet the firewall range allocated for punching firewall holes (with the 300a2900666SRuslan Ermilov.Dv PKT_ALIAS_PUNCH_FW 301a2900666SRuslan Ermilovflag). 302ff19f85dSGlen BarberThe range is cleared for all rules on initialization. 303a2900666SRuslan Ermilov.Ed 304b07fbc17SJoe Marcus Clarke.Pp 305b07fbc17SJoe Marcus Clarke.Ft void 3065e289f9eSPoul-Henning Kamp.Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port" 307b07fbc17SJoe Marcus Clarke.Bd -ragged -offset indent 308b07fbc17SJoe Marcus ClarkeSet the TCP port used by the Skinny Station protocol. 309b07fbc17SJoe Marcus ClarkeSkinny is used by Cisco IP phones to communicate with 310b07fbc17SJoe Marcus ClarkeCisco Call Managers to set up voice over IP calls. 311b07fbc17SJoe Marcus ClarkeIf this is not set, Skinny aliasing will not be done. 312b07fbc17SJoe Marcus ClarkeThe typical port used by Skinny is 2000. 313b07fbc17SJoe Marcus Clarke.Ed 314a2900666SRuslan Ermilov.Sh PACKET HANDLING 315a2900666SRuslan ErmilovThe packet handling functions are used to modify incoming (remote to local) 316a2900666SRuslan Ermilovand outgoing (local to remote) packets. 317a2900666SRuslan ErmilovThe calling program is responsible for receiving and sending packets via 318a2900666SRuslan Ermilovnetwork interfaces. 319a2900666SRuslan Ermilov.Pp 320a2900666SRuslan ErmilovAlong with 3215e289f9eSPoul-Henning Kamp.Fn LibAliasInit 322a2900666SRuslan Ermilovand 3235e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress , 324a2900666SRuslan Ermilovthe two packet handling functions, 3255e289f9eSPoul-Henning Kamp.Fn LibAliasIn 326a2900666SRuslan Ermilovand 3275e289f9eSPoul-Henning Kamp.Fn LibAliasOut , 328ff19f85dSGlen Barbercomprise the minimal set of functions needed for a basic IP masquerading 329a2900666SRuslan Ermilovimplementation. 330a2900666SRuslan Ermilov.Pp 3313efa11bbSBrian Somers.Ft int 332bc596e56SAlex Richardson.Fn LibAliasIn "struct libalias *" "void *buffer" "int maxpacketsize" 333a2900666SRuslan Ermilov.Bd -ragged -offset indent 334a2900666SRuslan ErmilovAn incoming packet coming from a remote machine to the local network is 335a2900666SRuslan Ermilovde-aliased by this function. 3363efa11bbSBrian SomersThe IP packet is pointed to by 337a2900666SRuslan Ermilov.Fa buffer , 3383efa11bbSBrian Somersand 339a2900666SRuslan Ermilov.Fa maxpacketsize 340a2900666SRuslan Ermilovindicates the size of the data structure containing the packet and should 341a2900666SRuslan Ermilovbe at least as large as the actual packet size. 342a2900666SRuslan Ermilov.Pp 3433efa11bbSBrian SomersReturn codes: 344a2900666SRuslan Ermilov.Bl -tag -width indent 345a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK 3463efa11bbSBrian SomersThe packet aliasing process was successful. 347a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED 3483efa11bbSBrian SomersThe packet was ignored and not de-aliased. 349ff19f85dSGlen BarberThis can happen if the protocol is unrecognized, as for an ICMP message 350ff19f85dSGlen Barbertype that is not handled, or if incoming packets for new connections are being 351ff19f85dSGlen Barberignored (if the 352a2900666SRuslan Ermilov.Dv PKT_ALIAS_DENY_INCOMING 353ff19f85dSGlen Barbermode bit was set using 3545e289f9eSPoul-Henning Kamp.Fn LibAliasSetMode ) . 355a2900666SRuslan Ermilov.It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT 356a2900666SRuslan ErmilovThis is returned when a fragment cannot be resolved because the header 357a2900666SRuslan Ermilovfragment has not been sent yet. 358a2900666SRuslan ErmilovIn this situation, fragments must be saved with 3595e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment 3603efa11bbSBrian Somersuntil a header fragment is found. 361a2900666SRuslan Ermilov.It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT 362a2900666SRuslan ErmilovThe packet aliasing process was successful, and a header fragment was found. 363a2900666SRuslan ErmilovThis is a signal to retrieve any unresolved fragments with 3645e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment 365a2900666SRuslan Ermilovand de-alias them with 3665e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn . 367a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR 368a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred. 3693efa11bbSBrian Somers.El 370a2900666SRuslan Ermilov.Ed 371a2900666SRuslan Ermilov.Pp 3723efa11bbSBrian Somers.Ft int 373bc596e56SAlex Richardson.Fn LibAliasOut "struct libalias *" "void *buffer" "int maxpacketsize" 374a2900666SRuslan Ermilov.Bd -ragged -offset indent 375a2900666SRuslan ErmilovAn outgoing packet coming from the local network to a remote machine is 376a2900666SRuslan Ermilovaliased by this function. 3773efa11bbSBrian SomersThe IP packet is pointed to by 378a2900666SRuslan Ermilov.Fa buffer , 3793efa11bbSBrian Somersand 380a2900666SRuslan Ermilov.Fa maxpacketsize 381a2900666SRuslan Ermilovindicates the maximum packet size permissible should the packet length be 382a2900666SRuslan Ermilovchanged. 383a2900666SRuslan ErmilovIP encoding protocols place address and port information in the encapsulated 384a2900666SRuslan Ermilovdata stream which has to be modified and can account for changes in packet 385a2900666SRuslan Ermilovlength. 386a2900666SRuslan ErmilovWell known examples of such protocols are FTP and IRC DCC. 387a2900666SRuslan Ermilov.Pp 3883efa11bbSBrian SomersReturn codes: 389a2900666SRuslan Ermilov.Bl -tag -width indent 390a2900666SRuslan Ermilov.It Dv PKT_ALIAS_OK 3913efa11bbSBrian SomersThe packet aliasing process was successful. 392a2900666SRuslan Ermilov.It Dv PKT_ALIAS_IGNORED 393a2900666SRuslan ErmilovThe packet was ignored and not aliased. 394a2900666SRuslan ErmilovThis can happen if the protocol is unrecognized, or possibly an ICMP message 395a2900666SRuslan Ermilovtype is not handled. 396a2900666SRuslan Ermilov.It Dv PKT_ALIAS_ERROR 397a2900666SRuslan ErmilovAn internal error within the packet aliasing engine occurred. 3983efa11bbSBrian Somers.El 399a2900666SRuslan Ermilov.Ed 400a2900666SRuslan Ermilov.Sh PORT AND ADDRESS REDIRECTION 401a2900666SRuslan ErmilovThe functions described in this section allow machines on the local network 402a2900666SRuslan Ermilovto be accessible in some degree to new incoming connections from the external 403a2900666SRuslan Ermilovnetwork. 404a2900666SRuslan ErmilovIndividual ports can be re-mapped or static network address translations can 405a2900666SRuslan Ermilovbe designated. 406a2900666SRuslan Ermilov.Pp 4073efa11bbSBrian Somers.Ft struct alias_link * 4085e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectPort 4095e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 4103efa11bbSBrian Somers.Fa "struct in_addr local_addr" 4113efa11bbSBrian Somers.Fa "u_short local_port" 4123efa11bbSBrian Somers.Fa "struct in_addr remote_addr" 4133efa11bbSBrian Somers.Fa "u_short remote_port" 4143efa11bbSBrian Somers.Fa "struct in_addr alias_addr" 4153efa11bbSBrian Somers.Fa "u_short alias_port" 4163efa11bbSBrian Somers.Fa "u_char proto" 4173efa11bbSBrian Somers.Fc 418a2900666SRuslan Ermilov.Bd -ragged -offset indent 419a2900666SRuslan ErmilovThis function specifies that traffic from a given remote address/port to 420a2900666SRuslan Ermilovan alias address/port be redirected to a specified local address/port. 4219c727d2cSJoseph KoshyThe parameter 422a2900666SRuslan Ermilov.Fa proto 423a2900666SRuslan Ermilovcan be either 424a2900666SRuslan Ermilov.Dv IPPROTO_TCP 4253efa11bbSBrian Somersor 426a2900666SRuslan Ermilov.Dv IPPROTO_UDP , 427a2900666SRuslan Ermilovas defined in 428fe08efe6SRuslan Ermilov.In netinet/in.h . 429a2900666SRuslan Ermilov.Pp 4303efa11bbSBrian SomersIf 431a2900666SRuslan Ermilov.Fa local_addr 432a2900666SRuslan Ermilovor 433a2900666SRuslan Ermilov.Fa alias_addr 434a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established 435a2900666SRuslan Ermilovby 4365e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 437a2900666SRuslan Ermilovis to be used. 438a2900666SRuslan ErmilovEven if 4395e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 440a2900666SRuslan Ermilovis called to change the address after 4415e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 442a2900666SRuslan Ermilovis called, a zero reference will track this change. 443a2900666SRuslan Ermilov.Pp 444ff19f85dSGlen BarberIf the link is further set up to operate with load sharing, then 4456d20a774SRuslan Ermilov.Fa local_addr 4466d20a774SRuslan Ermilovand 4476d20a774SRuslan Ermilov.Fa local_port 4486d20a774SRuslan Ermilovare ignored, and are selected dynamically from the server pool, as described in 4495e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 4506d20a774SRuslan Ermilovbelow. 4516d20a774SRuslan Ermilov.Pp 452a2900666SRuslan ErmilovIf 453a2900666SRuslan Ermilov.Fa remote_addr 454a2900666SRuslan Ermilovis zero, this indicates to redirect packets from any remote address. 455a2900666SRuslan ErmilovLikewise, if 456a2900666SRuslan Ermilov.Fa remote_port 457a2900666SRuslan Ermilovis zero, this indicates to redirect packets originating from any remote 458a2900666SRuslan Ermilovport number. 459ff19f85dSGlen BarberThe remote port specification will almost always be zero, but non-zero 460a2900666SRuslan Ermilovremote addresses can sometimes be useful for firewalling. 461a2900666SRuslan ErmilovIf two calls to 4625e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 463a2900666SRuslan Ermilovoverlap in their address/port specifications, then the most recent call 464a2900666SRuslan Ermilovwill have precedence. 465a2900666SRuslan Ermilov.Pp 466a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by 4675e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 468a2900666SRuslan ErmilovIf 469a2900666SRuslan Ermilov.Dv NULL 470a2900666SRuslan Ermilovis returned, then the function call did not complete successfully. 471a2900666SRuslan Ermilov.Pp 472a2900666SRuslan ErmilovAll port numbers should be in network address byte order, so it is necessary 473a2900666SRuslan Ermilovto use 474a2900666SRuslan Ermilov.Xr htons 3 475a2900666SRuslan Ermilovto convert these parameters from internally readable numbers to network byte 476a2900666SRuslan Ermilovorder. 477a2900666SRuslan ErmilovAddresses are also in network byte order, which is implicit in the use of the 478a2900666SRuslan Ermilov.Fa struct in_addr 4793efa11bbSBrian Somersdata type. 480a2900666SRuslan Ermilov.Ed 481a2900666SRuslan Ermilov.Pp 4823efa11bbSBrian Somers.Ft struct alias_link * 4835e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectAddr 4845e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 4853efa11bbSBrian Somers.Fa "struct in_addr local_addr" 4863efa11bbSBrian Somers.Fa "struct in_addr alias_addr" 4873efa11bbSBrian Somers.Fc 488a2900666SRuslan Ermilov.Bd -ragged -offset indent 489a2900666SRuslan ErmilovThis function designates that all incoming traffic to 490a2900666SRuslan Ermilov.Fa alias_addr 4913efa11bbSBrian Somersbe redirected to 492a2900666SRuslan Ermilov.Fa local_addr . 4933efa11bbSBrian SomersSimilarly, all outgoing traffic from 494a2900666SRuslan Ermilov.Fa local_addr 4953efa11bbSBrian Somersis aliased to 496a2900666SRuslan Ermilov.Fa alias_addr . 497a2900666SRuslan Ermilov.Pp 4983efa11bbSBrian SomersIf 499a2900666SRuslan Ermilov.Fa local_addr 5003efa11bbSBrian Somersor 501a2900666SRuslan Ermilov.Fa alias_addr 502a2900666SRuslan Ermilovis zero, this indicates that the packet aliasing address as established by 5035e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 504a2900666SRuslan Ermilovis to be used. 505a2900666SRuslan ErmilovEven if 5065e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 507a2900666SRuslan Ermilovis called to change the address after 5085e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr 5093efa11bbSBrian Somersis called, a zero reference will track this change. 510a2900666SRuslan Ermilov.Pp 511ff19f85dSGlen BarberIf the link is further set up to operate with load sharing, then the 5126d20a774SRuslan Ermilov.Fa local_addr 513ff19f85dSGlen Barberargument is ignored, and is selected dynamically from the server pool, 514ff19f85dSGlen Barberas described in 5155e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 5166d20a774SRuslan Ermilovbelow. 5176d20a774SRuslan Ermilov.Pp 518a2900666SRuslan ErmilovIf subsequent calls to 5195e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr 520a2900666SRuslan Ermilovuse the same aliasing address, all new incoming traffic to this aliasing 521a2900666SRuslan Ermilovaddress will be redirected to the local address made in the last function 522a2900666SRuslan Ermilovcall. 523a2900666SRuslan ErmilovNew traffic generated by any of the local machines, designated in the 524a2900666SRuslan Ermilovseveral function calls, will be aliased to the same address. 525a2900666SRuslan ErmilovConsider the following example: 5266ae742a5SJoel Dahl.Pp 527c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.2"), 5283efa11bbSBrian Somers inet_aton("141.221.254.101")); 529c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.3"), 5303efa11bbSBrian Somers inet_aton("141.221.254.101")); 531c9a24641SRuslan ErmilovLibAliasRedirectAddr(la, inet_aton("192.168.0.4"), 5323efa11bbSBrian Somers inet_aton("141.221.254.101")); 533a2900666SRuslan Ermilov.Pp 534a2900666SRuslan ErmilovAny outgoing connections such as 535a2900666SRuslan Ermilov.Xr telnet 1 536a2900666SRuslan Ermilovor 537a2900666SRuslan Ermilov.Xr ftp 1 538a2900666SRuslan Ermilovfrom 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from 539a2900666SRuslan Ermilov141.221.254.101. 540a2900666SRuslan ErmilovAny incoming connections to 141.221.254.101 will be directed to 192.168.0.4. 541a2900666SRuslan Ermilov.Pp 542a2900666SRuslan ErmilovAny calls to 5435e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 544a2900666SRuslan Ermilovwill have precedence over address mappings designated by 5455e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 546a2900666SRuslan Ermilov.Pp 547a2900666SRuslan ErmilovThis function returns a pointer which can subsequently be used by 5485e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 549a2900666SRuslan ErmilovIf 550a2900666SRuslan Ermilov.Dv NULL 551a2900666SRuslan Ermilovis returned, then the function call did not complete successfully. 552a2900666SRuslan Ermilov.Ed 553a2900666SRuslan Ermilov.Pp 5546d20a774SRuslan Ermilov.Ft int 5555e289f9eSPoul-Henning Kamp.Fo LibAliasAddServer 5565e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 5576d20a774SRuslan Ermilov.Fa "struct alias_link *link" 5586d20a774SRuslan Ermilov.Fa "struct in_addr addr" 5596d20a774SRuslan Ermilov.Fa "u_short port" 5606d20a774SRuslan Ermilov.Fc 5616d20a774SRuslan Ermilov.Bd -ragged -offset indent 5626d20a774SRuslan ErmilovThis function sets the 5636d20a774SRuslan Ermilov.Fa link 5646d20a774SRuslan Ermilovup for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT). 5656d20a774SRuslan ErmilovLSNAT operates as follows. 5666d20a774SRuslan ErmilovA client attempts to access a server by using the server virtual address. 5676d20a774SRuslan ErmilovThe LSNAT router transparently redirects the request to one of the hosts 568ff19f85dSGlen Barberin the server pool, using a real-time load sharing algorithm. 5696d20a774SRuslan ErmilovMultiple sessions may be initiated from the same client, and each session 570ff19f85dSGlen Barbercould be directed to a different host based on the load balance across server 571ff19f85dSGlen Barberpool hosts when the sessions are initiated. 572ff19f85dSGlen BarberIf load sharing is desired for just a few specific services, the configuration 573ff19f85dSGlen Barberon LSNAT could be defined to restrict load sharing to just the services 5746d20a774SRuslan Ermilovdesired. 5756d20a774SRuslan Ermilov.Pp 5766d20a774SRuslan ErmilovCurrently, only the simplest selection algorithm is implemented, where a 5776d20a774SRuslan Ermilovhost is selected on a round-robin basis only, without regard to load on 5786d20a774SRuslan Ermilovthe host. 5796d20a774SRuslan Ermilov.Pp 5806d20a774SRuslan ErmilovFirst, the 5816d20a774SRuslan Ermilov.Fa link 5826d20a774SRuslan Ermilovis created by either 5835e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 5846d20a774SRuslan Ermilovor 5855e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 5866d20a774SRuslan ErmilovThen, 5875e289f9eSPoul-Henning Kamp.Fn LibAliasAddServer 5886d20a774SRuslan Ermilovis called multiple times to add entries to the 5896d20a774SRuslan Ermilov.Fa link Ns 's 5906d20a774SRuslan Ermilovserver pool. 5916d20a774SRuslan Ermilov.Pp 5926d20a774SRuslan ErmilovFor links created with 5935e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr , 5946d20a774SRuslan Ermilovthe 5956d20a774SRuslan Ermilov.Fa port 5961a0a9345SRuslan Ermilovargument is ignored and could have any value, e.g.\& htons(~0). 5976d20a774SRuslan Ermilov.Pp 598234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise. 599234dfc90SRuslan Ermilov.Ed 600234dfc90SRuslan Ermilov.Pp 601234dfc90SRuslan Ermilov.Ft int 6025e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link" 603234dfc90SRuslan Ermilov.Bd -ragged -offset indent 604234dfc90SRuslan ErmilovThis function marks the specified static redirect rule entered by 6055e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 606234dfc90SRuslan Ermilovas dynamic. 6071a0a9345SRuslan ErmilovThis can be used to e.g.\& dynamically redirect a single TCP connection, 608234dfc90SRuslan Ermilovafter which the rule is removed. 609234dfc90SRuslan ErmilovOnly fully specified links can be made dynamic. 610234dfc90SRuslan Ermilov(See the 611234dfc90SRuslan Ermilov.Sx STATIC AND DYNAMIC LINKS 612234dfc90SRuslan Ermilovand 613234dfc90SRuslan Ermilov.Sx PARTIALLY SPECIFIED ALIASING LINKS 6141a0a9345SRuslan Ermilovsections below for a definition of static vs.\& dynamic, 6151a0a9345SRuslan Ermilovand partially vs.\& fully specified links.) 616234dfc90SRuslan Ermilov.Pp 617234dfc90SRuslan ErmilovThis function returns 0 on success, \-1 otherwise. 6186d20a774SRuslan Ermilov.Ed 6196d20a774SRuslan Ermilov.Pp 6203efa11bbSBrian Somers.Ft void 6215e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link" 622a2900666SRuslan Ermilov.Bd -ragged -offset indent 623a2900666SRuslan ErmilovThis function will delete a specific static redirect rule entered by 6245e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectPort 625a2900666SRuslan Ermilovor 6265e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr . 627a2900666SRuslan ErmilovThe parameter 628a2900666SRuslan Ermilov.Fa link 629a2900666SRuslan Ermilovis the pointer returned by either of the redirection functions. 630a2900666SRuslan ErmilovIf an invalid pointer is passed to 6315e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete , 632ff19f85dSGlen Barberthen a program crash or unpredictable operation could result, so 633ff19f85dSGlen Barbercare is needed when using this function. 634a2900666SRuslan Ermilov.Ed 635a2900666SRuslan Ermilov.Pp 636619d1a30SBrian Somers.Ft int 6375e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule "struct libalias *" "const char *cmd" 638a2900666SRuslan Ermilov.Bd -ragged -offset indent 639619d1a30SBrian SomersThe passed 640a2900666SRuslan Ermilov.Fa cmd 641a2900666SRuslan Ermilovstring consists of one or more pairs of words. 642a2900666SRuslan ErmilovThe first word in each pair is a token and the second is the value that 643a2900666SRuslan Ermilovshould be applied for that token. 644a2900666SRuslan ErmilovTokens and their argument types are as follows: 645a2900666SRuslan Ermilov.Bl -tag -width indent 646a2900666SRuslan Ermilov.It Cm type encode_ip_hdr | encode_tcp_stream | no_encode 647619d1a30SBrian SomersIn order to support transparent proxying, it is necessary to somehow 648619d1a30SBrian Somerspass the original address and port information into the new destination 649a2900666SRuslan Ermilovserver. 650a2900666SRuslan ErmilovIf 651a2900666SRuslan Ermilov.Cm encode_ip_hdr 65271760898SRuslan Ermilovis specified, the original destination address and port are passed 65371760898SRuslan Ermilovas an extra IP option. 654a2900666SRuslan ErmilovIf 655a2900666SRuslan Ermilov.Cm encode_tcp_stream 65671760898SRuslan Ermilovis specified, the original destination address and port are passed 65771760898SRuslan Ermilovas the first piece of data in the TCP stream in the format 65871760898SRuslan Ermilov.Dq Li DEST Ar IP port . 659a2900666SRuslan Ermilov.It Cm port Ar portnum 660619d1a30SBrian SomersOnly packets with the destination port 661619d1a30SBrian Somers.Ar portnum 662619d1a30SBrian Somersare proxied. 6639c88dc88SRuslan Ermilov.It Cm server Ar host Ns Op : Ns Ar portnum 664619d1a30SBrian SomersThis specifies the 665619d1a30SBrian Somers.Ar host 666619d1a30SBrian Somersand 667619d1a30SBrian Somers.Ar portnum 668ac8e3334SBrian Somersthat the data is to be redirected to. 669ac8e3334SBrian Somers.Ar host 670a2900666SRuslan Ermilovmust be an IP address rather than a DNS host name. 671a2900666SRuslan ErmilovIf 672619d1a30SBrian Somers.Ar portnum 673619d1a30SBrian Somersis not specified, the destination port number is not changed. 674619d1a30SBrian Somers.Pp 675619d1a30SBrian SomersThe 676619d1a30SBrian Somers.Ar server 677619d1a30SBrian Somersspecification is mandatory unless the 678a2900666SRuslan Ermilov.Cm delete 679619d1a30SBrian Somerscommand is being used. 680a2900666SRuslan Ermilov.It Cm rule Ar index 681619d1a30SBrian SomersNormally, each call to 6825e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 683a2900666SRuslan Ermilovinserts the next rule at the start of a linear list of rules. 684a2900666SRuslan ErmilovIf an 685619d1a30SBrian Somers.Ar index 686619d1a30SBrian Somersis specified, the new rule will be checked after all rules with lower 687a2900666SRuslan Ermilovindices. 688a2900666SRuslan ErmilovCalls to 6895e289f9eSPoul-Henning Kamp.Fn LibAliasProxyRule 690619d1a30SBrian Somersthat do not specify a rule are assigned rule 0. 691a2900666SRuslan Ermilov.It Cm delete Ar index 692a2900666SRuslan ErmilovThis token and its argument MUST NOT be used with any other tokens. 693a2900666SRuslan ErmilovWhen used, all existing rules with the given 694619d1a30SBrian Somers.Ar index 695619d1a30SBrian Somersare deleted. 696a2900666SRuslan Ermilov.It Cm proto tcp | udp 697619d1a30SBrian SomersIf specified, only packets of the given protocol type are matched. 6989c88dc88SRuslan Ermilov.It Cm src Ar IP Ns Op / Ns Ar bits 699619d1a30SBrian SomersIf specified, only packets with a source address matching the given 700619d1a30SBrian Somers.Ar IP 701a2900666SRuslan Ermilovare matched. 702a2900666SRuslan ErmilovIf 703619d1a30SBrian Somers.Ar bits 704619d1a30SBrian Somersis also specified, then the first 705619d1a30SBrian Somers.Ar bits 706619d1a30SBrian Somersbits of 707619d1a30SBrian Somers.Ar IP 708619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that 709619d1a30SBrian Somersnetwork will be matched. 7109c88dc88SRuslan Ermilov.It Cm dst Ar IP Ns Op / Ns Ar bits 711619d1a30SBrian SomersIf specified, only packets with a destination address matching the given 712619d1a30SBrian Somers.Ar IP 713a2900666SRuslan Ermilovare matched. 714a2900666SRuslan ErmilovIf 715619d1a30SBrian Somers.Ar bits 716619d1a30SBrian Somersis also specified, then the first 717619d1a30SBrian Somers.Ar bits 718619d1a30SBrian Somersbits of 719619d1a30SBrian Somers.Ar IP 720619d1a30SBrian Somersare taken as a network specification, and all IP addresses from that 721619d1a30SBrian Somersnetwork will be matched. 722619d1a30SBrian Somers.El 723a2900666SRuslan Ermilov.Pp 724619d1a30SBrian SomersThis function is usually used to redirect outgoing connections for 725619d1a30SBrian Somersinternal machines that are not permitted certain types of internet 726619d1a30SBrian Somersaccess, or to restrict access to certain external machines. 727a2900666SRuslan Ermilov.Ed 728a2900666SRuslan Ermilov.Pp 729483d2f22SRuslan Ermilov.Ft struct alias_link * 7305e289f9eSPoul-Henning Kamp.Fo LibAliasRedirectProto 7315e289f9eSPoul-Henning Kamp.Fa "struct libalias *" 732483d2f22SRuslan Ermilov.Fa "struct in_addr local_addr" 733483d2f22SRuslan Ermilov.Fa "struct in_addr remote_addr" 734483d2f22SRuslan Ermilov.Fa "struct in_addr alias_addr" 73580607605SRuslan Ermilov.Fa "u_char proto" 736483d2f22SRuslan Ermilov.Fc 737483d2f22SRuslan Ermilov.Bd -ragged -offset indent 73880607605SRuslan ErmilovThis function specifies that any IP packet with protocol number of 73980607605SRuslan Ermilov.Fa proto 740ff19f85dSGlen Barberfrom a given remote address to an alias address will be 741483d2f22SRuslan Ermilovredirected to a specified local address. 742483d2f22SRuslan Ermilov.Pp 743483d2f22SRuslan ErmilovIf 744483d2f22SRuslan Ermilov.Fa local_addr 745483d2f22SRuslan Ermilovor 746483d2f22SRuslan Ermilov.Fa alias_addr 747483d2f22SRuslan Ermilovis zero, this indicates that the packet aliasing address as established 748483d2f22SRuslan Ermilovby 7495e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 750483d2f22SRuslan Ermilovis to be used. 751483d2f22SRuslan ErmilovEven if 7525e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress 753483d2f22SRuslan Ermilovis called to change the address after 7545e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto 755483d2f22SRuslan Ermilovis called, a zero reference will track this change. 756483d2f22SRuslan Ermilov.Pp 757483d2f22SRuslan ErmilovIf 758483d2f22SRuslan Ermilov.Fa remote_addr 75980607605SRuslan Ermilovis zero, this indicates to redirect packets from any remote address. 760483d2f22SRuslan ErmilovNon-zero remote addresses can sometimes be useful for firewalling. 761483d2f22SRuslan Ermilov.Pp 762483d2f22SRuslan ErmilovIf two calls to 7635e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectProto 764483d2f22SRuslan Ermilovoverlap in their address specifications, then the most recent call 765483d2f22SRuslan Ermilovwill have precedence. 766483d2f22SRuslan Ermilov.Pp 767483d2f22SRuslan ErmilovThis function returns a pointer which can subsequently be used by 7685e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectDelete . 769483d2f22SRuslan ErmilovIf 770483d2f22SRuslan Ermilov.Dv NULL 771483d2f22SRuslan Ermilovis returned, then the function call did not complete successfully. 772483d2f22SRuslan Ermilov.Ed 773a2900666SRuslan Ermilov.Sh FRAGMENT HANDLING 774a2900666SRuslan ErmilovThe functions in this section are used to deal with incoming fragments. 775a2900666SRuslan Ermilov.Pp 776a2900666SRuslan ErmilovOutgoing fragments are handled within 7775e289f9eSPoul-Henning Kamp.Fn LibAliasOut 778a2900666SRuslan Ermilovby changing the address according to any applicable mapping set by 7795e289f9eSPoul-Henning Kamp.Fn LibAliasRedirectAddr , 7801855100fSAlexey Zelkinor the default aliasing address set by 7815e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress . 782a2900666SRuslan Ermilov.Pp 7831855100fSAlexey ZelkinIncoming fragments are handled in one of two ways. 784a2900666SRuslan ErmilovIf the header of a fragmented IP packet has already been seen, then all 785a2900666SRuslan Ermilovsubsequent fragments will be re-mapped in the same manner the header 786a2900666SRuslan Ermilovfragment was. 787a2900666SRuslan ErmilovFragments which arrive before the header are saved and then retrieved 788a2900666SRuslan Ermilovonce the header fragment has been resolved. 789a2900666SRuslan Ermilov.Pp 7903efa11bbSBrian Somers.Ft int 791bc596e56SAlex Richardson.Fn LibAliasSaveFragment "struct libalias *" "void *ptr" 792a2900666SRuslan Ermilov.Bd -ragged -offset indent 793a2900666SRuslan ErmilovWhen 7945e289f9eSPoul-Henning Kamp.Fn LibAliasIn 795a2900666SRuslan Ermilovreturns 796a2900666SRuslan Ermilov.Dv PKT_ALIAS_UNRESOLVED_FRAGMENT , 797a2900666SRuslan Ermilovthis function can be used to save the pointer to the unresolved fragment. 798a2900666SRuslan Ermilov.Pp 7993efa11bbSBrian SomersIt is implicitly assumed that 800a2900666SRuslan Ermilov.Fa ptr 8013efa11bbSBrian Somerspoints to a block of memory allocated by 802a2900666SRuslan Ermilov.Xr malloc 3 . 803a2900666SRuslan ErmilovIf the fragment is never resolved, the packet aliasing engine will 804a2900666SRuslan Ermilovautomatically free the memory after a timeout period. 805a2900666SRuslan Ermilov[Eventually this function should be modified so that a callback function 806a2900666SRuslan Ermilovfor freeing memory is passed as an argument.] 807a2900666SRuslan Ermilov.Pp 808a2900666SRuslan ErmilovThis function returns 809a2900666SRuslan Ermilov.Dv PKT_ALIAS_OK 810a2900666SRuslan Ermilovif it was successful and 811a2900666SRuslan Ermilov.Dv PKT_ALIAS_ERROR 812a2900666SRuslan Ermilovif there was an error. 813a2900666SRuslan Ermilov.Ed 814a2900666SRuslan Ermilov.Pp 815bc596e56SAlex Richardson.Ft void * 816bc596e56SAlex Richardson.Fn LibAliasGetFragment "struct libalias *" "void *buffer" 817a2900666SRuslan Ermilov.Bd -ragged -offset indent 818a2900666SRuslan ErmilovThis function can be used to retrieve fragment pointers saved by 8195e289f9eSPoul-Henning Kamp.Fn LibAliasSaveFragment . 8203efa11bbSBrian SomersThe IP header fragment pointed to by 821a2900666SRuslan Ermilov.Fa buffer 8223efa11bbSBrian Somersis the header fragment indicated when 8235e289f9eSPoul-Henning Kamp.Fn LibAliasIn 824a2900666SRuslan Ermilovreturns 825a2900666SRuslan Ermilov.Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT . 826a2900666SRuslan ErmilovOnce a fragment pointer is retrieved, it becomes the calling program's 827a2900666SRuslan Ermilovresponsibility to free the dynamically allocated memory for the fragment. 828a2900666SRuslan Ermilov.Pp 8299703a107SPhilippe CharnierThe 8305e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment 8319703a107SPhilippe Charnierfunction can be called sequentially until there are no more fragments 8329703a107SPhilippe Charnieravailable, at which time it returns 833a2900666SRuslan Ermilov.Dv NULL . 834a2900666SRuslan Ermilov.Ed 835a2900666SRuslan Ermilov.Pp 8363efa11bbSBrian Somers.Ft void 837bc596e56SAlex Richardson.Fn LibAliasFragmentIn "struct libalias *" "void *header" "void *fragment" 838a2900666SRuslan Ermilov.Bd -ragged -offset indent 8393efa11bbSBrian SomersWhen a fragment is retrieved with 8405e289f9eSPoul-Henning Kamp.Fn LibAliasGetFragment , 841a2900666SRuslan Ermilovit can then be de-aliased with a call to 8425e289f9eSPoul-Henning Kamp.Fn LibAliasFragmentIn . 843a2900666SRuslan ErmilovThe 844a2900666SRuslan Ermilov.Fa header 845a2900666SRuslan Ermilovargument is the pointer to a header fragment used as a template, and 846a2900666SRuslan Ermilov.Fa fragment 8473efa11bbSBrian Somersis the pointer to the packet to be de-aliased. 848a2900666SRuslan Ermilov.Ed 849a2900666SRuslan Ermilov.Sh MISCELLANEOUS FUNCTIONS 85005116753SAndrey V. Elsukov.Ft struct alias_link * 85105116753SAndrey V. Elsukov.Fn AddLink "struct libalias *" "struct in_addr src_addr" "struct in_addr dst_addr" \ 85205116753SAndrey V. Elsukov"struct in_addr alias_addr" "u_short src_port" "u_short dst_port" \ 85305116753SAndrey V. Elsukov"int alias_param" "int link_type" 85405116753SAndrey V. Elsukov.Bd -ragged -offset indent 855ff19f85dSGlen BarberThis function adds new state to the instance hash table. 856ff19f85dSGlen BarberThe dst_address and/or dst_port may be given as zero, which 857ff19f85dSGlen Barberintroduces some dynamic character into the link, since 858ff19f85dSGlen BarberLibAliasSetAddress can change the address that is used. 859ff19f85dSGlen BarberHowever, in the current implementation, such links can only be used 860ff19f85dSGlen Barberfor inbound (ext -> int) traffic. 86105116753SAndrey V. Elsukov.Ed 86205116753SAndrey V. Elsukov.Pp 8633efa11bbSBrian Somers.Ft void 8645e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr" 865a2900666SRuslan Ermilov.Bd -ragged -offset indent 866a2900666SRuslan ErmilovWhen an incoming packet not associated with any pre-existing aliasing link 867a2900666SRuslan Ermilovarrives at the host machine, it will be sent to the address indicated by a 868a2900666SRuslan Ermilovcall to 8695e289f9eSPoul-Henning Kamp.Fn LibAliasSetTarget . 870a2900666SRuslan Ermilov.Pp 871151682eaSBrian SomersIf this function is called with an 872a2900666SRuslan Ermilov.Dv INADDR_NONE 873a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address set by 8745e289f9eSPoul-Henning Kamp.Fn LibAliasSetAddress . 875a2900666SRuslan Ermilov.Pp 876151682eaSBrian SomersIf this function is not called, or is called with an 877a2900666SRuslan Ermilov.Dv INADDR_ANY 878a2900666SRuslan Ermilovaddress argument, then all new incoming packets go to the address specified 879a2900666SRuslan Ermilovin the packet. 880a2900666SRuslan ErmilovThis allows external machines to talk directly to internal machines if they 881a2900666SRuslan Ermilovcan route packets to the machine in question. 882a2900666SRuslan Ermilov.Ed 883a2900666SRuslan Ermilov.Pp 8843efa11bbSBrian Somers.Ft u_short 8855e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes" 886a2900666SRuslan Ermilov.Bd -ragged -offset indent 887a2900666SRuslan ErmilovThis is a utility function that does not seem to be available elsewhere and 888a2900666SRuslan Ermilovis included as a convenience. 889a2900666SRuslan ErmilovIt computes the internet checksum, which is used in both IP and 890a2900666SRuslan Ermilovprotocol-specific headers (TCP, UDP, ICMP). 891a2900666SRuslan Ermilov.Pp 892a2900666SRuslan ErmilovThe 893a2900666SRuslan Ermilov.Fa buffer 894a2900666SRuslan Ermilovargument points to the data block to be checksummed, and 895a2900666SRuslan Ermilov.Fa nbytes 896a2900666SRuslan Ermilovis the number of bytes. 897a2900666SRuslan ErmilovThe 16-bit checksum field should be zeroed before computing the checksum. 898a2900666SRuslan Ermilov.Pp 899a2900666SRuslan ErmilovChecksums can also be verified by operating on a block of data including 900a2900666SRuslan Ermilovits checksum. 901a2900666SRuslan ErmilovIf the checksum is valid, 9025e289f9eSPoul-Henning Kamp.Fn LibAliasInternetChecksum 903a2900666SRuslan Ermilovwill return zero. 904a2900666SRuslan Ermilov.Ed 905642e43b3SArchie Cobbs.Pp 906642e43b3SArchie Cobbs.Ft int 907bc596e56SAlex Richardson.Fn LibAliasUnaliasOut "struct libalias *" "void *buffer" "int maxpacketsize" 908642e43b3SArchie Cobbs.Bd -ragged -offset indent 90971845bffSSheldon HearnAn outgoing packet, which has already been aliased, 91071845bffSSheldon Hearnhas its private address/port information restored by this function. 911642e43b3SArchie CobbsThe IP packet is pointed to by 912642e43b3SArchie Cobbs.Fa buffer , 913642e43b3SArchie Cobbsand 914642e43b3SArchie Cobbs.Fa maxpacketsize 915642e43b3SArchie Cobbsis provided for error checking purposes. 916642e43b3SArchie CobbsThis function can be used if an already-aliased packet needs to have its 9171a0a9345SRuslan Ermilovoriginal IP header restored for further processing (e.g.\& logging). 918642e43b3SArchie Cobbs.Ed 919a2900666SRuslan Ermilov.Sh CONCEPTUAL BACKGROUND 920a2900666SRuslan ErmilovThis section is intended for those who are planning to modify the source 921a2900666SRuslan Ermilovcode or want to create somewhat esoteric applications using the packet 922a2900666SRuslan Ermilovaliasing functions. 923a2900666SRuslan Ermilov.Pp 924a2900666SRuslan ErmilovThe conceptual framework under which the packet aliasing engine operates 925a2900666SRuslan Ermilovis described here. 9263efa11bbSBrian SomersCentral to the discussion is the idea of an 927a2900666SRuslan Ermilov.Em aliasing link 928a2900666SRuslan Ermilovwhich describes the relationship for a given packet transaction between 929a2900666SRuslan Ermilovthe local machine, aliased identity and remote machine. 930a2900666SRuslan ErmilovIt is discussed how such links come into existence and are destroyed. 931a2900666SRuslan Ermilov.Ss ALIASING LINKS 932a2900666SRuslan ErmilovThere is a notion of an 933a2900666SRuslan Ermilov.Em aliasing link , 934a2900666SRuslan Ermilovwhich is a 7-tuple describing a specific translation: 9353efa11bbSBrian Somers.Bd -literal -offset indent 9363efa11bbSBrian Somers(local addr, local port, alias addr, alias port, 9373efa11bbSBrian Somers remote addr, remote port, protocol) 9383efa11bbSBrian Somers.Ed 939a2900666SRuslan Ermilov.Pp 940a2900666SRuslan ErmilovOutgoing packets have the local address and port number replaced with the 941a2900666SRuslan Ermilovalias address and port number. 942a2900666SRuslan ErmilovIncoming packets undergo the reverse process. 943a2900666SRuslan ErmilovThe packet aliasing engine attempts to match packets against an internal 944a2900666SRuslan Ermilovtable of aliasing links to determine how to modify a given IP packet. 945a2900666SRuslan ErmilovBoth the IP header and protocol dependent headers are modified as necessary. 946a2900666SRuslan ErmilovAliasing links are created and deleted as necessary according to network 947a2900666SRuslan Ermilovtraffic. 948a2900666SRuslan Ermilov.Pp 949a2900666SRuslan ErmilovProtocols can be TCP, UDP or even ICMP in certain circumstances. 950a2900666SRuslan Ermilov(Some types of ICMP packets can be aliased according to sequence or ID 951a2900666SRuslan Ermilovnumber which acts as an equivalent port number for identifying how 952a2900666SRuslan Ermilovindividual packets should be handled.) 953a2900666SRuslan Ermilov.Pp 954a2900666SRuslan ErmilovEach aliasing link must have a unique combination of the following five 955a2900666SRuslan Ermilovquantities: alias address/port, remote address/port and protocol. 956a2900666SRuslan ErmilovThis ensures that several machines on a local network can share the 957a2900666SRuslan Ermilovsame aliasing IP address. 958a2900666SRuslan ErmilovIn cases where conflicts might arise, the aliasing port is chosen so that 959a2900666SRuslan Ermilovuniqueness is maintained. 960a2900666SRuslan Ermilov.Ss STATIC AND DYNAMIC LINKS 9613efa11bbSBrian SomersAliasing links can either be static or dynamic. 962a2900666SRuslan ErmilovStatic links persist indefinitely and represent fixed rules for translating 963a2900666SRuslan ErmilovIP packets. 964a2900666SRuslan ErmilovDynamic links come into existence for a specific TCP connection or UDP 965a2900666SRuslan Ermilovtransaction or ICMP ECHO sequence. 966a2900666SRuslan ErmilovFor the case of TCP, the connection can be monitored to see when the 967a2900666SRuslan Ermilovassociated aliasing link should be deleted. 968a2900666SRuslan ErmilovAliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests) 969a2900666SRuslan Ermilovwork on a simple timeout rule. 970a2900666SRuslan ErmilovWhen no activity is observed on a dynamic link for a certain amount of time 971a2900666SRuslan Ermilovit is automatically deleted. 972a2900666SRuslan ErmilovTimeout rules also apply to TCP connections which do not open or close 9733efa11bbSBrian Somersproperly. 974a2900666SRuslan Ermilov.Ss PARTIALLY SPECIFIED ALIASING LINKS 975a2900666SRuslan ErmilovAliasing links can be partially specified, meaning that the remote address 976a2900666SRuslan Ermilovand/or remote port are unknown. 977a2900666SRuslan ErmilovIn this case, when a packet matching the incomplete specification is found, 978a2900666SRuslan Ermilova fully specified dynamic link is created. 979a2900666SRuslan ErmilovIf the original partially specified link is dynamic, it will be deleted 980a2900666SRuslan Ermilovafter the fully specified link is created, otherwise it will persist. 981a2900666SRuslan Ermilov.Pp 982a2900666SRuslan ErmilovFor instance, a partially specified link might be 9833efa11bbSBrian Somers.Bd -literal -offset indent 9843efa11bbSBrian Somers(192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp) 9853efa11bbSBrian Somers.Ed 986a2900666SRuslan Ermilov.Pp 987a2900666SRuslan ErmilovThe zeros denote unspecified components for the remote address and port. 988a2900666SRuslan ErmilovIf this link were static it would have the effect of redirecting all 989a2900666SRuslan Ermilovincoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet) 990a2900666SRuslan Ermilovof machine 192.168.0.4 on the local network. 991a2900666SRuslan ErmilovEach individual telnet connection would initiate the creation of a distinct 992a2900666SRuslan Ermilovdynamic link. 993a2900666SRuslan Ermilov.Ss DYNAMIC LINK CREATION 994a2900666SRuslan ErmilovIn addition to aliasing links, there are also address mappings that can be 995a2900666SRuslan Ermilovstored within the internal data table of the packet aliasing mechanism. 9963efa11bbSBrian Somers.Bd -literal -offset indent 9973efa11bbSBrian Somers(local addr, alias addr) 9983efa11bbSBrian Somers.Ed 999a2900666SRuslan Ermilov.Pp 1000a2900666SRuslan ErmilovAddress mappings are searched when creating new dynamic links. 1001a2900666SRuslan Ermilov.Pp 1002a2900666SRuslan ErmilovAll outgoing packets from the local network automatically create a dynamic 1003a2900666SRuslan Ermilovlink if they do not match an already existing fully specified link. 1004a2900666SRuslan ErmilovIf an address mapping exists for the outgoing packet, this determines 1005a2900666SRuslan Ermilovthe alias address to be used. 1006a2900666SRuslan ErmilovIf no mapping exists, then a default address, usually the address of the 1007a2900666SRuslan Ermilovpacket aliasing host, is used. 1008a2900666SRuslan ErmilovIf necessary, this default address can be changed as often as each individual 1009a2900666SRuslan Ermilovpacket arrives. 1010a2900666SRuslan Ermilov.Pp 1011a2900666SRuslan ErmilovThe aliasing port number is determined such that the new dynamic link does 1012a2900666SRuslan Ermilovnot conflict with any existing links. 1013a2900666SRuslan ErmilovIn the default operating mode, the packet aliasing engine attempts to set 1014a2900666SRuslan Ermilovthe aliasing port equal to the local port number. 1015a2900666SRuslan ErmilovIf this results in a conflict, then port numbers are randomly chosen until 1016a2900666SRuslan Ermilova unique aliasing link can be established. 1017a2900666SRuslan ErmilovIn an alternate operating mode, the first choice of an aliasing port is also 1018a2900666SRuslan Ermilovrandom and unrelated to the local port number. 1019c37a0a82SBaptiste Daroussin.Sh MODULAR ARCHITECTURE Po AND Xr ipfw 4 SUPPORT Pc 1020be4f3cd0SPaolo PisatiOne of the latest improvements to 1021f3d9aab3SPaolo Pisati.Nm 1022f3d9aab3SPaolo Pisatiwas to make its support 1023be4f3cd0SPaolo Pisatifor new protocols independent from the rest of the library, giving it 1024f3d9aab3SPaolo Pisatithe ability to load/unload support for new protocols at run-time. 1025be4f3cd0SPaolo PisatiTo achieve this feature, all the code for protocol handling was moved 1026be4f3cd0SPaolo Pisatito a series of modules outside of the main library. 1027f3d9aab3SPaolo PisatiThese modules are compiled from the same sources but work in 1028be4f3cd0SPaolo Pisatidifferent ways, depending on whether they are compiled to work inside a kernel 1029be4f3cd0SPaolo Pisatior as part of the userland library. 1030be4f3cd0SPaolo Pisati.Ss LIBALIAS MODULES IN KERNEL LAND 1031f3d9aab3SPaolo PisatiWhen compiled for the kernel, 1032f3d9aab3SPaolo Pisati.Nm 1033cc81ddd9SRuslan Ermilovmodules are plain KLDs recognizable with the 1034cc81ddd9SRuslan Ermilov.Pa alias_ 1035cc81ddd9SRuslan Ermilovprefix. 1036be4f3cd0SPaolo Pisati.Pp 1037f3d9aab3SPaolo PisatiTo add support for a new protocol, load the corresponding module. 1038f3d9aab3SPaolo PisatiFor example: 1039be4f3cd0SPaolo Pisati.Pp 1040f3d9aab3SPaolo Pisati.Dl "kldload alias_ftp" 1041be4f3cd0SPaolo Pisati.Pp 1042f3d9aab3SPaolo PisatiWhen support for a protocol is no longer needed, its module can be unloaded: 1043be4f3cd0SPaolo Pisati.Pp 1044f3d9aab3SPaolo Pisati.Dl "kldunload alias_ftp" 1045be4f3cd0SPaolo Pisati.Ss LIBALIAS MODULES IN USERLAND 1046f3d9aab3SPaolo PisatiDue to the differences between kernel and userland (no KLD mechanism, 1047f3d9aab3SPaolo Pisatimany different address spaces, etc.), we had to change a bit how to 1048f3d9aab3SPaolo Pisatihandle module loading/tracking/unloading in userland. 1049be4f3cd0SPaolo Pisati.Pp 1050be4f3cd0SPaolo PisatiWhile compiled for a userland 1051f3d9aab3SPaolo Pisati.Nm , 1052cc81ddd9SRuslan Ermilovall the modules are plain libraries, residing in 1053cc81ddd9SRuslan Ermilov.Pa /usr/lib , 1054cc81ddd9SRuslan Ermilovand recognizable with the 1055cc81ddd9SRuslan Ermilov.Pa libalias_ 1056cc81ddd9SRuslan Ermilovprefix. 1057be4f3cd0SPaolo Pisati.Pp 1058f3d9aab3SPaolo PisatiThere is a configuration file, 1059f3d9aab3SPaolo Pisati.Pa /etc/libalias.conf , 1060f3d9aab3SPaolo Pisatiwith the following contents (by default): 1061f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1062f3d9aab3SPaolo Pisati/usr/lib/libalias_ftp.so 1063f3d9aab3SPaolo Pisati/usr/lib/libalias_irc.so 1064f3d9aab3SPaolo Pisati/usr/lib/libalias_nbt.so 1065f3d9aab3SPaolo Pisati/usr/lib/libalias_pptp.so 1066f3d9aab3SPaolo Pisati/usr/lib/libalias_skinny.so 1067f3d9aab3SPaolo Pisati/usr/lib/libalias_smedia.so 1068f3d9aab3SPaolo Pisati.Ed 1069be4f3cd0SPaolo Pisati.Pp 1070f3d9aab3SPaolo PisatiThis file contains the paths to the modules that 1071f3d9aab3SPaolo Pisati.Nm 1072be4f3cd0SPaolo Pisatiwill load. 1073f3d9aab3SPaolo PisatiTo load/unload a new module, just add its path to 1074f3d9aab3SPaolo Pisati.Pa libalias.conf 1075cc81ddd9SRuslan Ermilovand call 1076cc81ddd9SRuslan Ermilov.Fn LibAliasRefreshModules 1077cc81ddd9SRuslan Ermilovfrom the program. 1078cc81ddd9SRuslan ErmilovIn case the application provides a 1079cc81ddd9SRuslan Ermilov.Dv SIGHUP 1080cc81ddd9SRuslan Ermilovsignal handler, add a call to 1081cc81ddd9SRuslan Ermilov.Fn LibAliasRefreshModules 1082cc81ddd9SRuslan Ermilovinside the handler, and every time you want to refresh the loaded modules, 1083cc81ddd9SRuslan Ermilovsend it the 1084f3d9aab3SPaolo Pisati.Dv SIGHUP 1085f3d9aab3SPaolo Pisatisignal: 1086be4f3cd0SPaolo Pisati.Pp 1087f3d9aab3SPaolo Pisati.Dl "kill -HUP <process_pid>" 1088be4f3cd0SPaolo Pisati.Ss MODULAR ARCHITECURE: HOW IT WORKS 1089be4f3cd0SPaolo PisatiThe modular architecture of 1090f3d9aab3SPaolo Pisati.Nm 1091f3d9aab3SPaolo Pisatiworks similar whether it is running inside the 1092f3d9aab3SPaolo Pisatikernel or in userland. 1093f3d9aab3SPaolo PisatiFrom 1094f3d9aab3SPaolo Pisati.Pa alias_mod.c : 1095be4f3cd0SPaolo Pisati.Bd -literal 1096f3d9aab3SPaolo Pisati/* Protocol and userland module handlers chains. */ 1097cc81ddd9SRuslan ErmilovLIST_HEAD(handler_chain, proto_handler) handler_chain ... 1098f3d9aab3SPaolo Pisati\&... 1099cc81ddd9SRuslan ErmilovSLIST_HEAD(dll_chain, dll) dll_chain ... 1100f3d9aab3SPaolo Pisati.Ed 1101f3d9aab3SPaolo Pisati.Pp 1102f3d9aab3SPaolo Pisati.Va handler_chain 1103ff19f85dSGlen Barberkeeps track of all the protocol handlers loaded, while 1104f3d9aab3SPaolo Pisati.Va ddl_chain 1105ff19f85dSGlen Barbertracks which userland modules are loaded. 1106f3d9aab3SPaolo Pisati.Pp 1107f3d9aab3SPaolo Pisati.Va handler_chain 1108f3d9aab3SPaolo Pisatiis composed of 1109f3d9aab3SPaolo Pisati.Vt "struct proto_handler" 1110f3d9aab3SPaolo Pisatientries: 1111f3d9aab3SPaolo Pisati.Bd -literal 1112be4f3cd0SPaolo Pisatistruct proto_handler { 1113f3d9aab3SPaolo Pisati u_int pri; 1114be4f3cd0SPaolo Pisati int16_t dir; 1115f3d9aab3SPaolo Pisati uint8_t proto; 1116be4f3cd0SPaolo Pisati int (*fingerprint)(struct libalias *la, 1117be4f3cd0SPaolo Pisati struct ip *pip, struct alias_data *ah); 1118be4f3cd0SPaolo Pisati int (*protohandler)(struct libalias *la, 1119be4f3cd0SPaolo Pisati struct ip *pip, struct alias_data *ah); 1120535e0a09SGleb Smirnoff TAILQ_ENTRY(proto_handler) link; 1121be4f3cd0SPaolo Pisati}; 1122be4f3cd0SPaolo Pisati.Ed 1123be4f3cd0SPaolo Pisati.Pp 1124be4f3cd0SPaolo Pisatiwhere: 1125f3d9aab3SPaolo Pisati.Bl -inset 1126f3d9aab3SPaolo Pisati.It Va pri 1127ff19f85dSGlen Barberis the priority assigned to a protocol handler; lower priority 1128be4f3cd0SPaolo Pisatiis better. 1129f3d9aab3SPaolo Pisati.It Va dir 1130f3d9aab3SPaolo Pisatiis the direction of packets: ingoing or outgoing. 1131f3d9aab3SPaolo Pisati.It Va proto 1132ff19f85dSGlen Barberindicates to which protocol this packet belongs: IP, TCP or UDP. 1133f3d9aab3SPaolo Pisati.It Va fingerprint 1134f3d9aab3SPaolo Pisatipoints to the fingerprint function while protohandler points 1135be4f3cd0SPaolo Pisatito the protocol handler function. 1136f3d9aab3SPaolo Pisati.El 1137be4f3cd0SPaolo Pisati.Pp 1138f3d9aab3SPaolo PisatiThe 1139f3d9aab3SPaolo Pisati.Va fingerprint 1140ff19f85dSGlen Barberfunction has the dual role of checking if the 1141ff19f85dSGlen Barberincoming packet is found, and if it belongs to any categories that this 1142be4f3cd0SPaolo Pisatimodule can handle. 1143be4f3cd0SPaolo Pisati.Pp 1144f3d9aab3SPaolo PisatiThe 1145f3d9aab3SPaolo Pisati.Va protohandler 1146f3d9aab3SPaolo Pisatifunction actually manipulates 1147be4f3cd0SPaolo Pisatithe packet to make 1148f3d9aab3SPaolo Pisati.Nm 1149f3d9aab3SPaolo Pisaticorrectly NAT it. 1150be4f3cd0SPaolo Pisati.Pp 1151be4f3cd0SPaolo PisatiWhen a packet enters 1152f3d9aab3SPaolo Pisati.Nm , 1153f3d9aab3SPaolo Pisatiif it meets a module hook, 1154f3d9aab3SPaolo Pisati.Va handler_chain 1155f3d9aab3SPaolo Pisatiis searched to see if there is an handler that matches 1156ff19f85dSGlen Barberthis type of a packet (it checks protocol and direction of packet). 1157ff19f85dSGlen BarberThen, if more than one handler is found, it starts with the module with 1158f3d9aab3SPaolo Pisatithe lowest priority number: it calls the 1159f3d9aab3SPaolo Pisati.Va fingerprint 1160f3d9aab3SPaolo Pisatifunction and interprets the result. 1161be4f3cd0SPaolo Pisati.Pp 1162f3d9aab3SPaolo PisatiIf the result value is equal to 0 then it calls the protocol handler 1163f3d9aab3SPaolo Pisatiof this handler and returns. 1164f3d9aab3SPaolo PisatiOtherwise, it proceeds to the next eligible module until the 1165f3d9aab3SPaolo Pisati.Va handler_chain 1166f3d9aab3SPaolo Pisatiis exhausted. 1167be4f3cd0SPaolo Pisati.Pp 1168be4f3cd0SPaolo PisatiInside 1169f3d9aab3SPaolo Pisati.Nm , 1170be4f3cd0SPaolo Pisatithe module hook looks like this: 1171f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1172be4f3cd0SPaolo Pisatistruct alias_data ad = { 1173be4f3cd0SPaolo Pisati lnk, 1174be4f3cd0SPaolo Pisati &original_address, 1175be4f3cd0SPaolo Pisati &alias_address, 1176be4f3cd0SPaolo Pisati &alias_port, 1177be4f3cd0SPaolo Pisati &ud->uh_sport, /* original source port */ 1178be4f3cd0SPaolo Pisati &ud->uh_dport, /* original dest port */ 1179be4f3cd0SPaolo Pisati 256 /* maxpacketsize */ 1180be4f3cd0SPaolo Pisati}; 1181be4f3cd0SPaolo Pisati 1182f3d9aab3SPaolo Pisati\&... 1183be4f3cd0SPaolo Pisati 1184be4f3cd0SPaolo Pisati/* walk out chain */ 1185be4f3cd0SPaolo Pisatierr = find_handler(IN, UDP, la, pip, &ad); 1186be4f3cd0SPaolo Pisati.Ed 1187f3d9aab3SPaolo Pisati.Pp 1188f3d9aab3SPaolo PisatiAll data useful to a module are gathered together in an 1189f3d9aab3SPaolo Pisati.Vt alias_data 1190f3d9aab3SPaolo Pisatistructure, then 1191f3d9aab3SPaolo Pisati.Fn find_handler 1192f3d9aab3SPaolo Pisatiis called. 1193f3d9aab3SPaolo PisatiThe 1194f3d9aab3SPaolo Pisati.Fn find_handler 1195ff19f85dSGlen Barberfunction is responsible for walking the handler 1196ff19f85dSGlen Barberchain; it receives as input parameters: 1197f3d9aab3SPaolo Pisati.Bl -tag -width indent 1198f3d9aab3SPaolo Pisati.It Fa IN 1199f3d9aab3SPaolo Pisatidirection 1200f3d9aab3SPaolo Pisati.It Fa UDP 1201f3d9aab3SPaolo Pisatiworking protocol 1202f3d9aab3SPaolo Pisati.It Fa la 1203f3d9aab3SPaolo Pisatipointer to this instance of libalias 1204f3d9aab3SPaolo Pisati.It Fa pip 1205f3d9aab3SPaolo Pisatipointer to a 1206f3d9aab3SPaolo Pisati.Vt "struct ip" 1207f3d9aab3SPaolo Pisati.It Fa ad 1208f3d9aab3SPaolo Pisatipointer to 1209f3d9aab3SPaolo Pisati.Vt "struct alias_data" 1210f3d9aab3SPaolo Pisati(see above) 1211f3d9aab3SPaolo Pisati.El 1212be4f3cd0SPaolo Pisati.Pp 1213f3d9aab3SPaolo PisatiIn this case, 1214f3d9aab3SPaolo Pisati.Fn find_handler 1215f3d9aab3SPaolo Pisatiwill search only for modules registered for 1216be4f3cd0SPaolo Pisatisupporting INcoming UDP packets. 1217be4f3cd0SPaolo Pisati.Pp 1218f3d9aab3SPaolo PisatiAs was mentioned earlier, 1219f3d9aab3SPaolo Pisati.Nm 1220ff19f85dSGlen Barberin userland is a bit different, as 1221ff19f85dSGlen Barbercare must be taken in module handling as well (avoiding duplicate load of 1222ff19f85dSGlen Barbermodules, avoiding modules with same name, etc.) so 1223f3d9aab3SPaolo Pisati.Va dll_chain 1224f3d9aab3SPaolo Pisatiwas introduced. 1225be4f3cd0SPaolo Pisati.Pp 1226f3d9aab3SPaolo Pisati.Va dll_chain 1227f3d9aab3SPaolo Pisaticontains a list of all userland 1228f3d9aab3SPaolo Pisati.Nm 1229be4f3cd0SPaolo Pisatimodules loaded. 1230be4f3cd0SPaolo Pisati.Pp 1231f3d9aab3SPaolo PisatiWhen an application calls 1232f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules , 1233f3d9aab3SPaolo Pisati.Nm 1234f3d9aab3SPaolo Pisatifirst unloads all the loaded modules, then reloads all the modules listed in 1235f3d9aab3SPaolo Pisati.Pa /etc/libalias.conf : 1236ff19f85dSGlen Barberfor every module loaded, a new entry is added to 1237ff19f85dSGlen Barber.Va dll_chain . 1238be4f3cd0SPaolo Pisati.Pp 1239f3d9aab3SPaolo Pisati.Va dll_chain 1240f3d9aab3SPaolo Pisatiis composed of 1241f3d9aab3SPaolo Pisati.Vt "struct dll" 1242f3d9aab3SPaolo Pisatientries: 1243be4f3cd0SPaolo Pisati.Bd -literal 1244be4f3cd0SPaolo Pisatistruct dll { 1245be4f3cd0SPaolo Pisati /* name of module */ 1246be4f3cd0SPaolo Pisati char name[DLL_LEN]; 1247be4f3cd0SPaolo Pisati /* 1248be4f3cd0SPaolo Pisati * ptr to shared obj obtained through 1249be4f3cd0SPaolo Pisati * dlopen() - use this ptr to get access 1250be4f3cd0SPaolo Pisati * to any symbols from a loaded module 1251be4f3cd0SPaolo Pisati * via dlsym() 1252be4f3cd0SPaolo Pisati */ 1253be4f3cd0SPaolo Pisati void *handle; 1254be4f3cd0SPaolo Pisati struct dll *next; 1255be4f3cd0SPaolo Pisati}; 1256be4f3cd0SPaolo Pisati.Ed 1257f3d9aab3SPaolo Pisati.Bl -inset 1258f3d9aab3SPaolo Pisati.It Va name 1259a3fb6da9SGlen Barberis the name of the module. 1260f3d9aab3SPaolo Pisati.It Va handle 1261f3d9aab3SPaolo Pisatiis a pointer to the module obtained through 1262a3fb6da9SGlen Barber.Xr dlopen 3 . 1263f3d9aab3SPaolo Pisati.El 1264be4f3cd0SPaolo PisatiWhenever a module is loaded in userland, an entry is added to 1265f3d9aab3SPaolo Pisati.Va dll_chain , 1266f3d9aab3SPaolo Pisatithen every protocol handler present in that module 1267f3d9aab3SPaolo Pisatiis resolved and registered in 1268f3d9aab3SPaolo Pisati.Va handler_chain . 1269be4f3cd0SPaolo Pisati.Ss HOW TO WRITE A MODULE FOR LIBALIAS 1270f3d9aab3SPaolo PisatiThere is a module (called 1271f3d9aab3SPaolo Pisati.Pa alias_dummy.[ch] ) 1272f3d9aab3SPaolo Pisatiin 1273f3d9aab3SPaolo Pisati.Nm 1274ff19f85dSGlen Barberthat can be used as a skeleton for future work. 1275ff19f85dSGlen BarberHere we analyse some parts of that module. 1276f3d9aab3SPaolo PisatiFrom 1277f3d9aab3SPaolo Pisati.Pa alias_dummy.c : 1278be4f3cd0SPaolo Pisati.Bd -literal 1279535e0a09SGleb Smirnoffstruct proto_handler handlers[] = { 1280535e0a09SGleb Smirnoff { 1281535e0a09SGleb Smirnoff .pri = 666, 1282535e0a09SGleb Smirnoff .dir = IN|OUT, 1283535e0a09SGleb Smirnoff .proto = UDP|TCP, 1284535e0a09SGleb Smirnoff .fingerprint = fingerprint, 1285535e0a09SGleb Smirnoff .protohandler= protohandler, 1286535e0a09SGleb Smirnoff }, 1287535e0a09SGleb Smirnoff { EOH } 1288535e0a09SGleb Smirnoff}; 1289be4f3cd0SPaolo Pisati.Ed 1290be4f3cd0SPaolo Pisati.Pp 1291f3d9aab3SPaolo PisatiThe variable 1292f3d9aab3SPaolo Pisati.Va handlers 1293f3d9aab3SPaolo Pisatiis the 1294f3d9aab3SPaolo Pisati.Dq "most important thing" 1295f3d9aab3SPaolo Pisatiin a module 1296ff19f85dSGlen Barbersince it describes the handlers present and lets the outside world use 1297be4f3cd0SPaolo Pisatiit in an opaque way. 1298be4f3cd0SPaolo Pisati.Pp 1299be4f3cd0SPaolo PisatiIt must ALWAYS be present in every module, and it MUST retain 1300f3d9aab3SPaolo Pisatithe name 1301f3d9aab3SPaolo Pisati.Va handlers , 1302f3d9aab3SPaolo Pisatiotherwise attempting to load a module in userland will fail and 1303f3d9aab3SPaolo Pisaticomplain about missing symbols: for more information about module 1304f3d9aab3SPaolo Pisatiload/unload, please refer to 1305f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules , 1306f3d9aab3SPaolo Pisati.Fn LibAliasLoadModule 1307f3d9aab3SPaolo Pisatiand 1308f3d9aab3SPaolo Pisati.Fn LibAliasUnloadModule 1309f3d9aab3SPaolo Pisatiin 1310f3d9aab3SPaolo Pisati.Pa alias.c . 1311be4f3cd0SPaolo Pisati.Pp 1312f3d9aab3SPaolo Pisati.Va handlers 1313f3d9aab3SPaolo Pisaticontains all the 1314f3d9aab3SPaolo Pisati.Vt proto_handler 1315f3d9aab3SPaolo Pisatistructures present in a module. 1316be4f3cd0SPaolo Pisati.Bd -literal 1317be4f3cd0SPaolo Pisatistatic int 1318be4f3cd0SPaolo Pisatimod_handler(module_t mod, int type, void *data) 1319be4f3cd0SPaolo Pisati{ 1320be4f3cd0SPaolo Pisati int error; 1321be4f3cd0SPaolo Pisati 1322be4f3cd0SPaolo Pisati switch (type) { 1323be4f3cd0SPaolo Pisati case MOD_LOAD: 1324535e0a09SGleb Smirnoff error = LibAliasAttachHandlers(handlers); 1325be4f3cd0SPaolo Pisati break; 1326be4f3cd0SPaolo Pisati case MOD_UNLOAD: 1327535e0a09SGleb Smirnoff error = LibAliasDetachHandlers(handlers); 1328be4f3cd0SPaolo Pisati break; 1329be4f3cd0SPaolo Pisati default: 1330be4f3cd0SPaolo Pisati error = EINVAL; 1331be4f3cd0SPaolo Pisati } 1332be4f3cd0SPaolo Pisati return (error); 1333be4f3cd0SPaolo Pisati} 1334be4f3cd0SPaolo Pisati.Ed 1335f3d9aab3SPaolo PisatiWhen running as KLD, 1336f3d9aab3SPaolo Pisati.Fn mod_handler 1337ff19f85dSGlen Barberregisters/deregisters the module using 1338535e0a09SGleb Smirnoff.Fn LibAliasAttachHandlers 1339f3d9aab3SPaolo Pisatiand 1340535e0a09SGleb Smirnoff.Fn LibAliasDetachHandlers , 1341f3d9aab3SPaolo Pisatirespectively. 1342be4f3cd0SPaolo Pisati.Pp 1343be4f3cd0SPaolo PisatiEvery module must contain at least 2 functions: one fingerprint 1344be4f3cd0SPaolo Pisatifunction and a protocol handler function. 1345be4f3cd0SPaolo Pisati.Bd -literal 1346be4f3cd0SPaolo Pisati#ifdef _KERNEL 1347be4f3cd0SPaolo Pisatistatic 1348be4f3cd0SPaolo Pisati#endif 1349be4f3cd0SPaolo Pisatiint 1350be4f3cd0SPaolo Pisatifingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah) 1351be4f3cd0SPaolo Pisati{ 1352be4f3cd0SPaolo Pisati 1353f3d9aab3SPaolo Pisati\&... 1354be4f3cd0SPaolo Pisati} 1355be4f3cd0SPaolo Pisati 1356be4f3cd0SPaolo Pisati#ifdef _KERNEL 1357be4f3cd0SPaolo Pisatistatic 1358be4f3cd0SPaolo Pisati#endif 1359be4f3cd0SPaolo Pisatiint 1360be4f3cd0SPaolo Pisatiprotohandler(struct libalias *la, struct ip *pip, 1361be4f3cd0SPaolo Pisati struct alias_data *ah) 1362be4f3cd0SPaolo Pisati{ 1363be4f3cd0SPaolo Pisati 1364f3d9aab3SPaolo Pisati\&... 1365be4f3cd0SPaolo Pisati} 1366be4f3cd0SPaolo Pisati.Ed 1367be4f3cd0SPaolo Pisatiand they must accept exactly these input parameters. 1368be4f3cd0SPaolo Pisati.Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES 1369f3d9aab3SPaolo PisatiTo add module support into an application that uses 1370f3d9aab3SPaolo Pisati.Nm , 1371f3d9aab3SPaolo Pisatithe following simple steps can be followed. 1372f3d9aab3SPaolo Pisati.Bl -enum 1373f3d9aab3SPaolo Pisati.It 1374f3d9aab3SPaolo PisatiFind the main file of an application 1375f3d9aab3SPaolo Pisati(let us call it 1376f3d9aab3SPaolo Pisati.Pa main.c ) . 1377f3d9aab3SPaolo Pisati.It 1378f3d9aab3SPaolo PisatiAdd this to the header section of 1379f3d9aab3SPaolo Pisati.Pa main.c , 1380f3d9aab3SPaolo Pisatiif not already present: 1381be4f3cd0SPaolo Pisati.Pp 1382f3d9aab3SPaolo Pisati.Dl "#include <signal.h>" 1383be4f3cd0SPaolo Pisati.Pp 1384f3d9aab3SPaolo Pisatiand this just after the header section: 1385be4f3cd0SPaolo Pisati.Pp 1386f3d9aab3SPaolo Pisati.Dl "static void signal_handler(int);" 1387f3d9aab3SPaolo Pisati.It 1388f3d9aab3SPaolo PisatiAdd the following line to the init function of an application or, 1389f3d9aab3SPaolo Pisatiif it does not have any init function, put it in 1390f3d9aab3SPaolo Pisati.Fn main : 1391be4f3cd0SPaolo Pisati.Pp 1392f3d9aab3SPaolo Pisati.Dl "signal(SIGHUP, signal_handler);" 1393be4f3cd0SPaolo Pisati.Pp 1394f3d9aab3SPaolo Pisatiand place the 1395f3d9aab3SPaolo Pisati.Fn signal_handler 1396f3d9aab3SPaolo Pisatifunction somewhere in 1397f3d9aab3SPaolo Pisati.Pa main.c : 1398f3d9aab3SPaolo Pisati.Bd -literal -offset indent 1399f3d9aab3SPaolo Pisatistatic void 1400f3d9aab3SPaolo Pisatisignal_handler(int sig) 1401f3d9aab3SPaolo Pisati{ 1402f3d9aab3SPaolo Pisati 1403f3d9aab3SPaolo Pisati LibAliasRefreshModules(); 1404f3d9aab3SPaolo Pisati} 1405be4f3cd0SPaolo Pisati.Ed 1406be4f3cd0SPaolo Pisati.Pp 1407f3d9aab3SPaolo PisatiOtherwise, if an application already traps the 1408f3d9aab3SPaolo Pisati.Dv SIGHUP 1409f3d9aab3SPaolo Pisatisignal, just add a call to 1410f3d9aab3SPaolo Pisati.Fn LibAliasRefreshModules 1411f3d9aab3SPaolo Pisatiin the signal handler function. 1412f3d9aab3SPaolo Pisati.El 1413f3d9aab3SPaolo PisatiFor example, to patch 1414f3d9aab3SPaolo Pisati.Xr natd 8 1415f3d9aab3SPaolo Pisatito use 1416f3d9aab3SPaolo Pisati.Nm 1417f3d9aab3SPaolo Pisatimodules, just add the following line to 1418f3d9aab3SPaolo Pisati.Fn RefreshAddr "int sig __unused" : 1419f3d9aab3SPaolo Pisati.Pp 1420f3d9aab3SPaolo Pisati.Dl "LibAliasRefreshModules()" 1421f3d9aab3SPaolo Pisati.Pp 1422f3d9aab3SPaolo Pisatirecompile and you are done. 1423f3d9aab3SPaolo Pisati.Ss LOGGING SUPPORT IN KERNEL LAND 1424f3d9aab3SPaolo PisatiWhen working as KLD, 1425f3d9aab3SPaolo Pisati.Nm 1426f3d9aab3SPaolo Pisatinow has log support that 1427f3d9aab3SPaolo Pisatihappens on a buffer allocated inside 1428f3d9aab3SPaolo Pisati.Vt "struct libalias" 1429f3d9aab3SPaolo Pisati(from 1430f3d9aab3SPaolo Pisati.Pa alias_local.h ) : 1431be4f3cd0SPaolo Pisati.Bd -literal 1432be4f3cd0SPaolo Pisatistruct libalias { 1433be4f3cd0SPaolo Pisati ... 1434be4f3cd0SPaolo Pisati 1435be4f3cd0SPaolo Pisati /* log descriptor */ 1436be4f3cd0SPaolo Pisati#ifdef KERNEL_LOG 1437be4f3cd0SPaolo Pisati char *logDesc; /* 1438be4f3cd0SPaolo Pisati * ptr to an auto-malloced 1439be4f3cd0SPaolo Pisati * memory buffer when libalias 1440be4f3cd0SPaolo Pisati * works as kld 1441be4f3cd0SPaolo Pisati */ 1442be4f3cd0SPaolo Pisati#else 1443be4f3cd0SPaolo Pisati FILE *logDesc; /* 1444be4f3cd0SPaolo Pisati * ptr to /var/log/alias.log 1445be4f3cd0SPaolo Pisati * when libalias runs as a 1446be4f3cd0SPaolo Pisati * userland lib 1447be4f3cd0SPaolo Pisati */ 1448be4f3cd0SPaolo Pisati#endif 1449be4f3cd0SPaolo Pisati 1450be4f3cd0SPaolo Pisati ... 1451be4f3cd0SPaolo Pisati} 1452be4f3cd0SPaolo Pisati.Ed 1453f3d9aab3SPaolo Pisatiso all applications using 1454f3d9aab3SPaolo Pisati.Nm 1455f3d9aab3SPaolo Pisatiwill be able to handle their 1456f3d9aab3SPaolo Pisatiown logs, if they want, accessing 1457f3d9aab3SPaolo Pisati.Va logDesc . 1458f3d9aab3SPaolo PisatiMoreover, every change to a log buffer is automatically added to 1459f3d9aab3SPaolo Pisati.Xr syslog 3 1460f3d9aab3SPaolo Pisatiwith the 1461f3d9aab3SPaolo Pisati.Dv LOG_SECURITY 1462f3d9aab3SPaolo Pisatifacility and the 1463f3d9aab3SPaolo Pisati.Dv LOG_INFO 1464f3d9aab3SPaolo Pisatilevel. 146549894437SGleb Smirnoff.Sh AUTHORS 146649894437SGleb Smirnoff.An Charles Mott Aq cm@linktel.net , 146749894437SGleb Smirnoffversions 1.0 - 1.8, 2.0 - 2.4. 146849894437SGleb Smirnoff.An Eivind Eklund Aq eivind@FreeBSD.org , 146949894437SGleb Smirnoffversions 1.8b, 1.9 and 2.5. 147049894437SGleb SmirnoffAdded IRC DCC support as well as contributing a number of architectural 147149894437SGleb Smirnoffimprovements; added the firewall bypass for FTP/IRC DCC. 147249894437SGleb Smirnoff.An Erik Salander Aq erik@whistle.com 147349894437SGleb Smirnoffadded support for PPTP and RTSP. 147449894437SGleb Smirnoff.An Junichi Satoh Aq junichi@junichi.org 147549894437SGleb Smirnoffadded support for RTSP/PNA. 147649894437SGleb Smirnoff.An Ruslan Ermilov Aq ru@FreeBSD.org 147749894437SGleb Smirnoffadded support for PPTP and LSNAT as well as general hacking. 147849894437SGleb Smirnoff.An Gleb Smirnoff Aq glebius@FreeBSD.org 147949894437SGleb Smirnoffported the library to kernel space. 148049894437SGleb Smirnoff.An Paolo Pisati Aq piso@FreeBSD.org 148149894437SGleb Smirnoffmade the library modular, moving support for all 148249894437SGleb Smirnoffprotocols (except for IP, TCP and UDP) to external modules. 148349894437SGleb Smirnoff.Sh ACKNOWLEDGEMENTS 148449894437SGleb SmirnoffListed below, in approximate chronological order, are individuals who 148549894437SGleb Smirnoffhave provided valuable comments and/or debugging assistance. 148649894437SGleb Smirnoff.Bd -ragged -offset indent 148749894437SGleb Smirnoff.An -split 148849894437SGleb Smirnoff.An Gary Roberts 148949894437SGleb Smirnoff.An Tom Torrance 149049894437SGleb Smirnoff.An Reto Burkhalter 149149894437SGleb Smirnoff.An Martin Renters 149249894437SGleb Smirnoff.An Brian Somers 149349894437SGleb Smirnoff.An Paul Traina 149449894437SGleb Smirnoff.An Ari Suutari 149549894437SGleb Smirnoff.An Dave Remien 149649894437SGleb Smirnoff.An J. Fortes 149749894437SGleb Smirnoff.An Andrzej Bialecki 149849894437SGleb Smirnoff.An Gordon Burditt 149949894437SGleb Smirnoff.Ed 1500