The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/libalias/libalias.3

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 .\"-
    2 .\" Copyright (c) 2001 Charles Mott <cm@linktel.net>
    3 .\" All rights reserved.
    4 .\"
    5 .\" Redistribution and use in source and binary forms, with or without
    6 .\" modification, are permitted provided that the following conditions
    7 .\" are met:
    8 .\" 1. Redistributions of source code must retain the above copyright
    9 .\"    notice, this list of conditions and the following disclaimer.
   10 .\" 2. Redistributions in binary form must reproduce the above copyright
   11 .\"    notice, this list of conditions and the following disclaimer in the
   12 .\"    documentation and/or other materials provided with the distribution.
   13 .\"
   14 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24 .\" SUCH DAMAGE.
   25 .\"
   26 .\" $FreeBSD$
   27 .\"
   28 .Dd May 31, 2021
   29 .Dt LIBALIAS 3
   30 .Os
   31 .Sh NAME
   32 .Nm libalias
   33 .Nd packet aliasing library for masquerading and network address translation
   34 .Sh SYNOPSIS
   35 .In sys/types.h
   36 .In netinet/in.h
   37 .In alias.h
   38 .Pp
   39 Function prototypes are given in the main body of the text.
   40 .Sh DESCRIPTION
   41 The
   42 .Nm
   43 library is a collection of functions for aliasing and de-aliasing of IP
   44 packets, intended for masquerading and network address translation (NAT).
   45 .Sh INTRODUCTION
   46 This library is a moderately portable set of functions designed to assist
   47 in the process of IP masquerading and network address translation.
   48 Outgoing packets from a local network with unregistered IP addresses can
   49 be aliased to appear as if they came from an accessible IP address.
   50 Incoming packets are then de-aliased so that they are sent to the correct
   51 machine on the local network.
   52 .Pp
   53 A certain amount of flexibility is built into the packet aliasing engine.
   54 In the simplest mode of operation, a many-to-one address mapping takes
   55 place between the local network and the packet aliasing host.
   56 This is known as IP masquerading.
   57 In addition, one-to-one mappings between local and public addresses can
   58 also be implemented, which is known as static NAT.
   59 In between these extremes, different groups of private addresses can be
   60 linked to different public addresses, comprising several distinct
   61 many-to-one mappings.
   62 Also, a given public address and port can be statically redirected to a
   63 private address/port.
   64 .Sh INITIALIZATION AND CONTROL
   65 One special function,
   66 .Fn LibAliasInit ,
   67 must always be called before any packet handling may be performed, and
   68 the returned instance pointer must be passed to all the other functions.
   69 Normally, the
   70 .Fn LibAliasSetAddress
   71 function is called afterwards, to set the default aliasing address.
   72 In addition, the operating mode of the packet aliasing engine can be
   73 customized by calling
   74 .Fn LibAliasSetMode .
   75 .Pp
   76 .Ft "struct libalias *"
   77 .Fn LibAliasInit "struct libalias *"
   78 .Bd -ragged -offset indent
   79 This function is used to initialize
   80 internal data structures.
   81 When called the first time, a
   82 .Dv NULL
   83 pointer should be passed as an argument.
   84 The following mode bits are always set after calling
   85 .Fn LibAliasInit .
   86 See the description of
   87 .Fn LibAliasSetMode
   88 below for the meaning of these mode bits.
   89 .Pp
   90 .Bl -item -offset indent -compact
   91 .It
   92 .Dv PKT_ALIAS_SAME_PORTS
   93 .It
   94 .Dv PKT_ALIAS_USE_SOCKETS
   95 .It
   96 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
   97 .El
   98 .Pp
   99 This function will always return the packet aliasing engine to the same
  100 initial state.
  101 The
  102 .Fn LibAliasSetAddress
  103 function is normally called afterwards, and any desired changes from the
  104 default mode bits listed above require a call to
  105 .Fn LibAliasSetMode .
  106 .Pp
  107 It is mandatory that this function be called at the beginning of a program
  108 prior to any packet handling.
  109 .Ed
  110 .Pp
  111 .Ft void
  112 .Fn LibAliasUninit "struct libalias *"
  113 .Bd -ragged -offset indent
  114 This function has no return value and is used to clear any
  115 resources attached to internal data structures.
  116 .Pp
  117 This function should be called when a program stops using the aliasing
  118 engine; amongst other things, it clears out any firewall holes.
  119 To provide backwards compatibility and extra security, it is added to
  120 the
  121 .Xr atexit 3
  122 chain by
  123 .Fn LibAliasInit .
  124 .Ed
  125 .Pp
  126 .Ft void
  127 .Fn LibAliasSetAddress "struct libalias *" "struct in_addr addr"
  128 .Bd -ragged -offset indent
  129 This function sets the source address to which outgoing packets from the
  130 local area network are aliased.
  131 All outgoing packets are re-mapped to this address unless overridden by a
  132 static address mapping established by
  133 .Fn LibAliasRedirectAddr .
  134 If this function has not been called, and no static rules match, an outgoing
  135 packet retains its source address.
  136 .Pp
  137 If the
  138 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
  139 mode bit is set (the default mode of operation), then the internal aliasing
  140 link tables will be reset any time the aliasing address changes.
  141 This is useful for interfaces such as
  142 .Xr ppp 8 ,
  143 where the IP
  144 address may or may not change on successive dial-up attempts.
  145 .Pp
  146 If the
  147 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
  148 mode bit is set to zero, this function can also be used to dynamically change
  149 the aliasing address on a packet-to-packet basis (it is a low overhead call).
  150 .Pp
  151 It is mandatory that this function be called prior to any packet handling.
  152 .Ed
  153 .Pp
  154 .Ft unsigned int
  155 .Fn LibAliasSetMode "struct libalias *" "unsigned int flags" "unsigned int mask"
  156 .Bd -ragged -offset indent
  157 This function sets or clears mode bits
  158 according to the value of
  159 .Fa flags .
  160 Only bits marked in
  161 .Fa mask
  162 are affected.
  163 The following mode bits are defined in
  164 .In alias.h :
  165 .Bl -tag -width indent
  166 .It Dv PKT_ALIAS_LOG
  167 Enables logging into
  168 .Pa /var/log/alias.log .
  169 Each time an aliasing link is created or deleted, the log file is appended to
  170 with the current number of ICMP, TCP and UDP links.
  171 Mainly useful for debugging when the log file is viewed continuously with
  172 .Xr tail 1 .
  173 .It Dv PKT_ALIAS_DENY_INCOMING
  174 If this mode bit is set, all incoming packets associated with new TCP
  175 connections or new UDP transactions will be marked for being ignored
  176 .Po
  177 .Fn LibAliasIn
  178 returns
  179 .Dv PKT_ALIAS_IGNORED
  180 code
  181 .Pc
  182 by the calling program.
  183 Response packets to connections or transactions initiated from the packet
  184 aliasing host or local network will be unaffected.
  185 This mode bit is useful for implementing a one-way firewall.
  186 .It Dv PKT_ALIAS_SAME_PORTS
  187 If this mode bit is set, the packet-aliasing engine will attempt to leave
  188 the alias port numbers unchanged from the actual local port numbers.
  189 This can be done as long as the quintuple (proto, alias addr, alias port,
  190 remote addr, remote port) is unique.
  191 If a conflict exists, a new aliasing port number is chosen even if this
  192 mode bit is set.
  193 .It Dv PKT_ALIAS_USE_SOCKETS
  194 This bit should be set when the packet aliasing host originates network
  195 traffic as well as forwards it.
  196 When the packet aliasing host is waiting for a connection from an unknown
  197 host address or unknown port number (e.g.\& an FTP data connection), this
  198 mode bit specifies that a socket be allocated as a place holder to prevent
  199 port conflicts.
  200 Once a connection is established, usually within a minute or so, the socket
  201 is closed.
  202 .It Dv PKT_ALIAS_UNREGISTERED_ONLY
  203 If this mode bit is set, traffic on the local network which does not
  204 originate from unregistered address spaces will be ignored.
  205 Standard Class A, B and C unregistered addresses are:
  206 .Pp
  207 10.0.0.0     ->  10.255.255.255   (Class A subnet)
  208 172.16.0.0   ->  172.31.255.255   (Class B subnets)
  209 192.168.0.0  ->  192.168.255.255  (Class C subnets)
  210 .Pp
  211 This option is useful in the case that the packet aliasing host has both
  212 registered and unregistered subnets on different interfaces.
  213 The registered subnet is fully accessible to the outside world, so traffic
  214 from it does not need to be passed through the packet aliasing engine.
  215 .It Dv PKT_ALIAS_UNREGISTERED_CGN
  216 Like PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598 (Carrier Grade
  217 NAT) subnet as follows:
  218 .Pp
  219 100.64.0.0   ->  100.127.255.255  (RFC 6598 subnet)
  220 .It Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
  221 When this mode bit is set and
  222 .Fn LibAliasSetAddress
  223 is called to change the aliasing address, the internal link table of the
  224 packet aliasing engine will be cleared.
  225 This operating mode is useful for
  226 .Xr ppp 8
  227 links where the interface address can sometimes change or remain the same
  228 between dial-up attempts.
  229 If this mode bit is not set, the link table will never be reset in the event
  230 of an address change.
  231 .It Dv PKT_ALIAS_PUNCH_FW
  232 This option makes
  233 .Nm
  234 .Dq punch holes
  235 in an
  236 .Xr ipfirewall 4 -
  237 based firewall for FTP/IRC DCC connections.
  238 The holes punched are bound by from/to IP address and port; it will not be
  239 possible to use a hole for another connection.
  240 A hole is removed when the connection that uses it dies.
  241 To cater to unexpected death of a program using
  242 .Nm
  243 (e.g.\& kill -9),
  244 changing the state of the flag will clear the entire firewall range
  245 allocated for holes.
  246 This clearing will also happen on the initial call to
  247 .Fn LibAliasSetFWBase ,
  248 which must happen prior to setting this flag.
  249 .It Dv PKT_ALIAS_REVERSE
  250 This option makes
  251 .Nm
  252 reverse the way it handles incoming and outgoing packets, allowing it
  253 to be fed with data that passes through the internal interface rather
  254 than the external one.
  255 .It Dv PKT_ALIAS_PROXY_ONLY
  256 This option tells
  257 .Nm
  258 to obey transparent proxy rules only.
  259 Normal packet aliasing is not performed.
  260 See
  261 .Fn LibAliasProxyRule
  262 below for details.
  263 .It Dv PKT_ALIAS_SKIP_GLOBAL
  264 This option is used by
  265 .Pa ipfw_nat
  266 only.
  267 Specifying it as a flag to
  268 .Fn LibAliasSetMode
  269 has no effect.
  270 See section
  271 .Sx NETWORK ADDRESS TRANSLATION
  272 in
  273 .Xr ipfw 8
  274 for more details.
  275 .El
  276 .Ed
  277 .Pp
  278 .Ft void
  279 .Fn LibAliasSetFWBase "struct libalias *" "unsigned int base" "unsigned int num"
  280 .Bd -ragged -offset indent
  281 Set the firewall range allocated for punching firewall holes (with the
  282 .Dv PKT_ALIAS_PUNCH_FW
  283 flag).
  284 The range is cleared for all rules on initialization.
  285 .Ed
  286 .Pp
  287 .Ft void
  288 .Fn LibAliasSkinnyPort "struct libalias *" "unsigned int port"
  289 .Bd -ragged -offset indent
  290 Set the TCP port used by the Skinny Station protocol.
  291 Skinny is used by Cisco IP phones to communicate with
  292 Cisco Call Managers to set up voice over IP calls.
  293 If this is not set, Skinny aliasing will not be done.
  294 The typical port used by Skinny is 2000.
  295 .Ed
  296 .Sh PACKET HANDLING
  297 The packet handling functions are used to modify incoming (remote to local)
  298 and outgoing (local to remote) packets.
  299 The calling program is responsible for receiving and sending packets via
  300 network interfaces.
  301 .Pp
  302 Along with
  303 .Fn LibAliasInit
  304 and
  305 .Fn LibAliasSetAddress ,
  306 the two packet handling functions,
  307 .Fn LibAliasIn
  308 and
  309 .Fn LibAliasOut ,
  310 comprise the minimal set of functions needed for a basic IP masquerading
  311 implementation.
  312 .Pp
  313 .Ft int
  314 .Fn LibAliasIn "struct libalias *" "void *buffer" "int maxpacketsize"
  315 .Bd -ragged -offset indent
  316 An incoming packet coming from a remote machine to the local network is
  317 de-aliased by this function.
  318 The IP packet is pointed to by
  319 .Fa buffer ,
  320 and
  321 .Fa maxpacketsize
  322 indicates the size of the data structure containing the packet and should
  323 be at least as large as the actual packet size.
  324 .Pp
  325 Return codes:
  326 .Bl -tag -width indent
  327 .It Dv PKT_ALIAS_OK
  328 The packet aliasing process was successful.
  329 .It Dv PKT_ALIAS_IGNORED
  330 The packet was ignored and not de-aliased.
  331 This can happen if the protocol is unrecognized, as for an ICMP message
  332 type that is not handled, or if incoming packets for new connections are being
  333 ignored (if the
  334 .Dv PKT_ALIAS_DENY_INCOMING
  335 mode bit was set using
  336 .Fn LibAliasSetMode ) .
  337 .It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
  338 This is returned when a fragment cannot be resolved because the header
  339 fragment has not been sent yet.
  340 In this situation, fragments must be saved with
  341 .Fn LibAliasSaveFragment
  342 until a header fragment is found.
  343 .It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
  344 The packet aliasing process was successful, and a header fragment was found.
  345 This is a signal to retrieve any unresolved fragments with
  346 .Fn LibAliasGetFragment
  347 and de-alias them with
  348 .Fn LibAliasFragmentIn .
  349 .It Dv PKT_ALIAS_ERROR
  350 An internal error within the packet aliasing engine occurred.
  351 .El
  352 .Ed
  353 .Pp
  354 .Ft int
  355 .Fn LibAliasOut "struct libalias *" "void *buffer" "int maxpacketsize"
  356 .Bd -ragged -offset indent
  357 An outgoing packet coming from the local network to a remote machine is
  358 aliased by this function.
  359 The IP packet is pointed to by
  360 .Fa buffer ,
  361 and
  362 .Fa maxpacketsize
  363 indicates the maximum packet size permissible should the packet length be
  364 changed.
  365 IP encoding protocols place address and port information in the encapsulated
  366 data stream which has to be modified and can account for changes in packet
  367 length.
  368 Well known examples of such protocols are FTP and IRC DCC.
  369 .Pp
  370 Return codes:
  371 .Bl -tag -width indent
  372 .It Dv PKT_ALIAS_OK
  373 The packet aliasing process was successful.
  374 .It Dv PKT_ALIAS_IGNORED
  375 The packet was ignored and not aliased.
  376 This can happen if the protocol is unrecognized, or possibly an ICMP message
  377 type is not handled.
  378 .It Dv PKT_ALIAS_ERROR
  379 An internal error within the packet aliasing engine occurred.
  380 .El
  381 .Ed
  382 .Sh PORT AND ADDRESS REDIRECTION
  383 The functions described in this section allow machines on the local network
  384 to be accessible in some degree to new incoming connections from the external
  385 network.
  386 Individual ports can be re-mapped or static network address translations can
  387 be designated.
  388 .Pp
  389 .Ft struct alias_link *
  390 .Fo LibAliasRedirectPort
  391 .Fa "struct libalias *"
  392 .Fa "struct in_addr local_addr"
  393 .Fa "u_short local_port"
  394 .Fa "struct in_addr remote_addr"
  395 .Fa "u_short remote_port"
  396 .Fa "struct in_addr alias_addr"
  397 .Fa "u_short alias_port"
  398 .Fa "u_char proto"
  399 .Fc
  400 .Bd -ragged -offset indent
  401 This function specifies that traffic from a given remote address/port to
  402 an alias address/port be redirected to a specified local address/port.
  403 The parameter
  404 .Fa proto
  405 can be either
  406 .Dv IPPROTO_TCP
  407 or
  408 .Dv IPPROTO_UDP ,
  409 as defined in
  410 .In netinet/in.h .
  411 .Pp
  412 If
  413 .Fa local_addr
  414 or
  415 .Fa alias_addr
  416 is zero, this indicates that the packet aliasing address as established
  417 by
  418 .Fn LibAliasSetAddress
  419 is to be used.
  420 Even if
  421 .Fn LibAliasSetAddress
  422 is called to change the address after
  423 .Fn LibAliasRedirectPort
  424 is called, a zero reference will track this change.
  425 .Pp
  426 If the link is further set up to operate with load sharing, then
  427 .Fa local_addr
  428 and
  429 .Fa local_port
  430 are ignored, and are selected dynamically from the server pool, as described in
  431 .Fn LibAliasAddServer
  432 below.
  433 .Pp
  434 If
  435 .Fa remote_addr
  436 is zero, this indicates to redirect packets from any remote address.
  437 Likewise, if
  438 .Fa remote_port
  439 is zero, this indicates to redirect packets originating from any remote
  440 port number.
  441 The remote port specification will almost always be zero, but non-zero
  442 remote addresses can sometimes be useful for firewalling.
  443 If two calls to
  444 .Fn LibAliasRedirectPort
  445 overlap in their address/port specifications, then the most recent call
  446 will have precedence.
  447 .Pp
  448 This function returns a pointer which can subsequently be used by
  449 .Fn LibAliasRedirectDelete .
  450 If
  451 .Dv NULL
  452 is returned, then the function call did not complete successfully.
  453 .Pp
  454 All port numbers should be in network address byte order, so it is necessary
  455 to use
  456 .Xr htons 3
  457 to convert these parameters from internally readable numbers to network byte
  458 order.
  459 Addresses are also in network byte order, which is implicit in the use of the
  460 .Fa struct in_addr
  461 data type.
  462 .Ed
  463 .Pp
  464 .Ft struct alias_link *
  465 .Fo LibAliasRedirectAddr
  466 .Fa "struct libalias *"
  467 .Fa "struct in_addr local_addr"
  468 .Fa "struct in_addr alias_addr"
  469 .Fc
  470 .Bd -ragged -offset indent
  471 This function designates that all incoming traffic to
  472 .Fa alias_addr
  473 be redirected to
  474 .Fa local_addr .
  475 Similarly, all outgoing traffic from
  476 .Fa local_addr
  477 is aliased to
  478 .Fa alias_addr .
  479 .Pp
  480 If
  481 .Fa local_addr
  482 or
  483 .Fa alias_addr
  484 is zero, this indicates that the packet aliasing address as established by
  485 .Fn LibAliasSetAddress
  486 is to be used.
  487 Even if
  488 .Fn LibAliasSetAddress
  489 is called to change the address after
  490 .Fn LibAliasRedirectAddr
  491 is called, a zero reference will track this change.
  492 .Pp
  493 If the link is further set up to operate with load sharing, then the
  494 .Fa local_addr
  495 argument is ignored, and is selected dynamically from the server pool,
  496 as described in
  497 .Fn LibAliasAddServer
  498 below.
  499 .Pp
  500 If subsequent calls to
  501 .Fn LibAliasRedirectAddr
  502 use the same aliasing address, all new incoming traffic to this aliasing
  503 address will be redirected to the local address made in the last function
  504 call.
  505 New traffic generated by any of the local machines, designated in the
  506 several function calls, will be aliased to the same address.
  507 Consider the following example:
  508 .Pp
  509 LibAliasRedirectAddr(la, inet_aton("192.168.0.2"),
  510                         inet_aton("141.221.254.101"));
  511 LibAliasRedirectAddr(la, inet_aton("192.168.0.3"),
  512                         inet_aton("141.221.254.101"));
  513 LibAliasRedirectAddr(la, inet_aton("192.168.0.4"),
  514                         inet_aton("141.221.254.101"));
  515 .Pp
  516 Any outgoing connections such as
  517 .Xr telnet 1
  518 or
  519 .Xr ftp 1
  520 from 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
  521 141.221.254.101.
  522 Any incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
  523 .Pp
  524 Any calls to
  525 .Fn LibAliasRedirectPort
  526 will have precedence over address mappings designated by
  527 .Fn LibAliasRedirectAddr .
  528 .Pp
  529 This function returns a pointer which can subsequently be used by
  530 .Fn LibAliasRedirectDelete .
  531 If
  532 .Dv NULL
  533 is returned, then the function call did not complete successfully.
  534 .Ed
  535 .Pp
  536 .Ft int
  537 .Fo LibAliasAddServer
  538 .Fa "struct libalias *"
  539 .Fa "struct alias_link *link"
  540 .Fa "struct in_addr addr"
  541 .Fa "u_short port"
  542 .Fc
  543 .Bd -ragged -offset indent
  544 This function sets the
  545 .Fa link
  546 up for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
  547 LSNAT operates as follows.
  548 A client attempts to access a server by using the server virtual address.
  549 The LSNAT router transparently redirects the request to one of the hosts
  550 in the server pool, using a real-time load sharing algorithm.
  551 Multiple sessions may be initiated from the same client, and each session
  552 could be directed to a different host based on the load balance across server
  553 pool hosts when the sessions are initiated.
  554 If load sharing is desired for just a few specific services, the configuration
  555 on LSNAT could be defined to restrict load sharing to just the services
  556 desired.
  557 .Pp
  558 Currently, only the simplest selection algorithm is implemented, where a
  559 host is selected on a round-robin basis only, without regard to load on
  560 the host.
  561 .Pp
  562 First, the
  563 .Fa link
  564 is created by either
  565 .Fn LibAliasRedirectPort
  566 or
  567 .Fn LibAliasRedirectAddr .
  568 Then,
  569 .Fn LibAliasAddServer
  570 is called multiple times to add entries to the
  571 .Fa link Ns 's
  572 server pool.
  573 .Pp
  574 For links created with
  575 .Fn LibAliasRedirectAddr ,
  576 the
  577 .Fa port
  578 argument is ignored and could have any value, e.g.\& htons(~0).
  579 .Pp
  580 This function returns 0 on success, \-1 otherwise.
  581 .Ed
  582 .Pp
  583 .Ft int
  584 .Fn LibAliasRedirectDynamic "struct libalias *" "struct alias_link *link"
  585 .Bd -ragged -offset indent
  586 This function marks the specified static redirect rule entered by
  587 .Fn LibAliasRedirectPort
  588 as dynamic.
  589 This can be used to e.g.\& dynamically redirect a single TCP connection,
  590 after which the rule is removed.
  591 Only fully specified links can be made dynamic.
  592 (See the
  593 .Sx STATIC AND DYNAMIC LINKS
  594 and
  595 .Sx PARTIALLY SPECIFIED ALIASING LINKS
  596 sections below for a definition of static vs.\& dynamic,
  597 and partially vs.\& fully specified links.)
  598 .Pp
  599 This function returns 0 on success, \-1 otherwise.
  600 .Ed
  601 .Pp
  602 .Ft void
  603 .Fn LibAliasRedirectDelete "struct libalias *" "struct alias_link *link"
  604 .Bd -ragged -offset indent
  605 This function will delete a specific static redirect rule entered by
  606 .Fn LibAliasRedirectPort
  607 or
  608 .Fn LibAliasRedirectAddr .
  609 The parameter
  610 .Fa link
  611 is the pointer returned by either of the redirection functions.
  612 If an invalid pointer is passed to
  613 .Fn LibAliasRedirectDelete ,
  614 then a program crash or unpredictable operation could result, so
  615 care is needed when using this function.
  616 .Ed
  617 .Pp
  618 .Ft int
  619 .Fn LibAliasProxyRule "struct libalias *" "const char *cmd"
  620 .Bd -ragged -offset indent
  621 The passed
  622 .Fa cmd
  623 string consists of one or more pairs of words.
  624 The first word in each pair is a token and the second is the value that
  625 should be applied for that token.
  626 Tokens and their argument types are as follows:
  627 .Bl -tag -width indent
  628 .It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
  629 In order to support transparent proxying, it is necessary to somehow
  630 pass the original address and port information into the new destination
  631 server.
  632 If
  633 .Cm encode_ip_hdr
  634 is specified, the original destination address and port are passed
  635 as an extra IP option.
  636 If
  637 .Cm encode_tcp_stream
  638 is specified, the original destination address and port are passed
  639 as the first piece of data in the TCP stream in the format
  640 .Dq Li DEST Ar IP port .
  641 .It Cm port Ar portnum
  642 Only packets with the destination port
  643 .Ar portnum
  644 are proxied.
  645 .It Cm server Ar host Ns Op : Ns Ar portnum
  646 This specifies the
  647 .Ar host
  648 and
  649 .Ar portnum
  650 that the data is to be redirected to.
  651 .Ar host
  652 must be an IP address rather than a DNS host name.
  653 If
  654 .Ar portnum
  655 is not specified, the destination port number is not changed.
  656 .Pp
  657 The
  658 .Ar server
  659 specification is mandatory unless the
  660 .Cm delete
  661 command is being used.
  662 .It Cm rule Ar index
  663 Normally, each call to
  664 .Fn LibAliasProxyRule
  665 inserts the next rule at the start of a linear list of rules.
  666 If an
  667 .Ar index
  668 is specified, the new rule will be checked after all rules with lower
  669 indices.
  670 Calls to
  671 .Fn LibAliasProxyRule
  672 that do not specify a rule are assigned rule 0.
  673 .It Cm delete Ar index
  674 This token and its argument MUST NOT be used with any other tokens.
  675 When used, all existing rules with the given
  676 .Ar index
  677 are deleted.
  678 .It Cm proto tcp | udp
  679 If specified, only packets of the given protocol type are matched.
  680 .It Cm src Ar IP Ns Op / Ns Ar bits
  681 If specified, only packets with a source address matching the given
  682 .Ar IP
  683 are matched.
  684 If
  685 .Ar bits
  686 is also specified, then the first
  687 .Ar bits
  688 bits of
  689 .Ar IP
  690 are taken as a network specification, and all IP addresses from that
  691 network will be matched.
  692 .It Cm dst Ar IP Ns Op / Ns Ar bits
  693 If specified, only packets with a destination address matching the given
  694 .Ar IP
  695 are matched.
  696 If
  697 .Ar bits
  698 is also specified, then the first
  699 .Ar bits
  700 bits of
  701 .Ar IP
  702 are taken as a network specification, and all IP addresses from that
  703 network will be matched.
  704 .El
  705 .Pp
  706 This function is usually used to redirect outgoing connections for
  707 internal machines that are not permitted certain types of internet
  708 access, or to restrict access to certain external machines.
  709 .Ed
  710 .Pp
  711 .Ft struct alias_link *
  712 .Fo LibAliasRedirectProto
  713 .Fa "struct libalias *"
  714 .Fa "struct in_addr local_addr"
  715 .Fa "struct in_addr remote_addr"
  716 .Fa "struct in_addr alias_addr"
  717 .Fa "u_char proto"
  718 .Fc
  719 .Bd -ragged -offset indent
  720 This function specifies that any IP packet with protocol number of
  721 .Fa proto
  722 from a given remote address to an alias address will be
  723 redirected to a specified local address.
  724 .Pp
  725 If
  726 .Fa local_addr
  727 or
  728 .Fa alias_addr
  729 is zero, this indicates that the packet aliasing address as established
  730 by
  731 .Fn LibAliasSetAddress
  732 is to be used.
  733 Even if
  734 .Fn LibAliasSetAddress
  735 is called to change the address after
  736 .Fn LibAliasRedirectProto
  737 is called, a zero reference will track this change.
  738 .Pp
  739 If
  740 .Fa remote_addr
  741 is zero, this indicates to redirect packets from any remote address.
  742 Non-zero remote addresses can sometimes be useful for firewalling.
  743 .Pp
  744 If two calls to
  745 .Fn LibAliasRedirectProto
  746 overlap in their address specifications, then the most recent call
  747 will have precedence.
  748 .Pp
  749 This function returns a pointer which can subsequently be used by
  750 .Fn LibAliasRedirectDelete .
  751 If
  752 .Dv NULL
  753 is returned, then the function call did not complete successfully.
  754 .Ed
  755 .Sh FRAGMENT HANDLING
  756 The functions in this section are used to deal with incoming fragments.
  757 .Pp
  758 Outgoing fragments are handled within
  759 .Fn LibAliasOut
  760 by changing the address according to any applicable mapping set by
  761 .Fn LibAliasRedirectAddr ,
  762 or the default aliasing address set by
  763 .Fn LibAliasSetAddress .
  764 .Pp
  765 Incoming fragments are handled in one of two ways.
  766 If the header of a fragmented IP packet has already been seen, then all
  767 subsequent fragments will be re-mapped in the same manner the header
  768 fragment was.
  769 Fragments which arrive before the header are saved and then retrieved
  770 once the header fragment has been resolved.
  771 .Pp
  772 .Ft int
  773 .Fn LibAliasSaveFragment "struct libalias *" "void *ptr"
  774 .Bd -ragged -offset indent
  775 When
  776 .Fn LibAliasIn
  777 returns
  778 .Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
  779 this function can be used to save the pointer to the unresolved fragment.
  780 .Pp
  781 It is implicitly assumed that
  782 .Fa ptr
  783 points to a block of memory allocated by
  784 .Xr malloc 3 .
  785 If the fragment is never resolved, the packet aliasing engine will
  786 automatically free the memory after a timeout period.
  787 [Eventually this function should be modified so that a callback function
  788 for freeing memory is passed as an argument.]
  789 .Pp
  790 This function returns
  791 .Dv PKT_ALIAS_OK
  792 if it was successful and
  793 .Dv PKT_ALIAS_ERROR
  794 if there was an error.
  795 .Ed
  796 .Pp
  797 .Ft void *
  798 .Fn LibAliasGetFragment "struct libalias *" "void *buffer"
  799 .Bd -ragged -offset indent
  800 This function can be used to retrieve fragment pointers saved by
  801 .Fn LibAliasSaveFragment .
  802 The IP header fragment pointed to by
  803 .Fa buffer
  804 is the header fragment indicated when
  805 .Fn LibAliasIn
  806 returns
  807 .Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
  808 Once a fragment pointer is retrieved, it becomes the calling program's
  809 responsibility to free the dynamically allocated memory for the fragment.
  810 .Pp
  811 The
  812 .Fn LibAliasGetFragment
  813 function can be called sequentially until there are no more fragments
  814 available, at which time it returns
  815 .Dv NULL .
  816 .Ed
  817 .Pp
  818 .Ft void
  819 .Fn LibAliasFragmentIn "struct libalias *" "void *header" "void *fragment"
  820 .Bd -ragged -offset indent
  821 When a fragment is retrieved with
  822 .Fn LibAliasGetFragment ,
  823 it can then be de-aliased with a call to
  824 .Fn LibAliasFragmentIn .
  825 The
  826 .Fa header
  827 argument is the pointer to a header fragment used as a template, and
  828 .Fa fragment
  829 is the pointer to the packet to be de-aliased.
  830 .Ed
  831 .Sh MISCELLANEOUS FUNCTIONS
  832 .Ft struct alias_link *
  833 .Fn AddLink "struct libalias *" "struct in_addr src_addr" "struct in_addr dst_addr" \
  834 "struct in_addr alias_addr" "u_short src_port" "u_short dst_port" \
  835 "int alias_param" "int link_type"
  836 .Bd -ragged -offset indent
  837 This function adds new state to the instance hash table.
  838 The dst_address and/or dst_port may be given as zero, which
  839 introduces some dynamic character into the link, since
  840 LibAliasSetAddress can change the address that is used.
  841 However, in the current implementation, such links can only be used
  842 for inbound (ext -> int) traffic.
  843 .Ed
  844 .Pp
  845 .Ft void
  846 .Fn LibAliasSetTarget "struct libalias *" "struct in_addr addr"
  847 .Bd -ragged -offset indent
  848 When an incoming packet not associated with any pre-existing aliasing link
  849 arrives at the host machine, it will be sent to the address indicated by a
  850 call to
  851 .Fn LibAliasSetTarget .
  852 .Pp
  853 If this function is called with an
  854 .Dv INADDR_NONE
  855 address argument, then all new incoming packets go to the address set by
  856 .Fn LibAliasSetAddress .
  857 .Pp
  858 If this function is not called, or is called with an
  859 .Dv INADDR_ANY
  860 address argument, then all new incoming packets go to the address specified
  861 in the packet.
  862 This allows external machines to talk directly to internal machines if they
  863 can route packets to the machine in question.
  864 .Ed
  865 .Pp
  866 .Ft u_short
  867 .Fn LibAliasInternetChecksum "struct libalias *" "u_short *buffer" "int nbytes"
  868 .Bd -ragged -offset indent
  869 This is a utility function that does not seem to be available elsewhere and
  870 is included as a convenience.
  871 It computes the internet checksum, which is used in both IP and
  872 protocol-specific headers (TCP, UDP, ICMP).
  873 .Pp
  874 The
  875 .Fa buffer
  876 argument points to the data block to be checksummed, and
  877 .Fa nbytes
  878 is the number of bytes.
  879 The 16-bit checksum field should be zeroed before computing the checksum.
  880 .Pp
  881 Checksums can also be verified by operating on a block of data including
  882 its checksum.
  883 If the checksum is valid,
  884 .Fn LibAliasInternetChecksum
  885 will return zero.
  886 .Ed
  887 .Pp
  888 .Ft int
  889 .Fn LibAliasUnaliasOut "struct libalias *" "void *buffer" "int maxpacketsize"
  890 .Bd -ragged -offset indent
  891 An outgoing packet, which has already been aliased,
  892 has its private address/port information restored by this function.
  893 The IP packet is pointed to by
  894 .Fa buffer ,
  895 and
  896 .Fa maxpacketsize
  897 is provided for error checking purposes.
  898 This function can be used if an already-aliased packet needs to have its
  899 original IP header restored for further processing (e.g.\& logging).
  900 .Ed
  901 .Sh CONCEPTUAL BACKGROUND
  902 This section is intended for those who are planning to modify the source
  903 code or want to create somewhat esoteric applications using the packet
  904 aliasing functions.
  905 .Pp
  906 The conceptual framework under which the packet aliasing engine operates
  907 is described here.
  908 Central to the discussion is the idea of an
  909 .Em aliasing link
  910 which describes the relationship for a given packet transaction between
  911 the local machine, aliased identity and remote machine.
  912 It is discussed how such links come into existence and are destroyed.
  913 .Ss ALIASING LINKS
  914 There is a notion of an
  915 .Em aliasing link ,
  916 which is a 7-tuple describing a specific translation:
  917 .Bd -literal -offset indent
  918 (local addr, local port, alias addr, alias port,
  919  remote addr, remote port, protocol)
  920 .Ed
  921 .Pp
  922 Outgoing packets have the local address and port number replaced with the
  923 alias address and port number.
  924 Incoming packets undergo the reverse process.
  925 The packet aliasing engine attempts to match packets against an internal
  926 table of aliasing links to determine how to modify a given IP packet.
  927 Both the IP header and protocol dependent headers are modified as necessary.
  928 Aliasing links are created and deleted as necessary according to network
  929 traffic.
  930 .Pp
  931 Protocols can be TCP, UDP or even ICMP in certain circumstances.
  932 (Some types of ICMP packets can be aliased according to sequence or ID
  933 number which acts as an equivalent port number for identifying how
  934 individual packets should be handled.)
  935 .Pp
  936 Each aliasing link must have a unique combination of the following five
  937 quantities: alias address/port, remote address/port and protocol.
  938 This ensures that several machines on a local network can share the
  939 same aliasing IP address.
  940 In cases where conflicts might arise, the aliasing port is chosen so that
  941 uniqueness is maintained.
  942 .Ss STATIC AND DYNAMIC LINKS
  943 Aliasing links can either be static or dynamic.
  944 Static links persist indefinitely and represent fixed rules for translating
  945 IP packets.
  946 Dynamic links come into existence for a specific TCP connection or UDP
  947 transaction or ICMP ECHO sequence.
  948 For the case of TCP, the connection can be monitored to see when the
  949 associated aliasing link should be deleted.
  950 Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
  951 work on a simple timeout rule.
  952 When no activity is observed on a dynamic link for a certain amount of time
  953 it is automatically deleted.
  954 Timeout rules also apply to TCP connections which do not open or close
  955 properly.
  956 .Ss PARTIALLY SPECIFIED ALIASING LINKS
  957 Aliasing links can be partially specified, meaning that the remote address
  958 and/or remote port are unknown.
  959 In this case, when a packet matching the incomplete specification is found,
  960 a fully specified dynamic link is created.
  961 If the original partially specified link is dynamic, it will be deleted
  962 after the fully specified link is created, otherwise it will persist.
  963 .Pp
  964 For instance, a partially specified link might be
  965 .Bd -literal -offset indent
  966 (192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
  967 .Ed
  968 .Pp
  969 The zeros denote unspecified components for the remote address and port.
  970 If this link were static it would have the effect of redirecting all
  971 incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
  972 of machine 192.168.0.4 on the local network.
  973 Each individual telnet connection would initiate the creation of a distinct
  974 dynamic link.
  975 .Ss DYNAMIC LINK CREATION
  976 In addition to aliasing links, there are also address mappings that can be
  977 stored within the internal data table of the packet aliasing mechanism.
  978 .Bd -literal -offset indent
  979 (local addr, alias addr)
  980 .Ed
  981 .Pp
  982 Address mappings are searched when creating new dynamic links.
  983 .Pp
  984 All outgoing packets from the local network automatically create a dynamic
  985 link if they do not match an already existing fully specified link.
  986 If an address mapping exists for the outgoing packet, this determines
  987 the alias address to be used.
  988 If no mapping exists, then a default address, usually the address of the
  989 packet aliasing host, is used.
  990 If necessary, this default address can be changed as often as each individual
  991 packet arrives.
  992 .Pp
  993 The aliasing port number is determined such that the new dynamic link does
  994 not conflict with any existing links.
  995 In the default operating mode, the packet aliasing engine attempts to set
  996 the aliasing port equal to the local port number.
  997 If this results in a conflict, then port numbers are randomly chosen until
  998 a unique aliasing link can be established.
  999 In an alternate operating mode, the first choice of an aliasing port is also
 1000 random and unrelated to the local port number.
 1001 .Sh MODULAR ARCHITECTURE Po AND Xr ipfw 4 SUPPORT Pc
 1002 One of the latest improvements to
 1003 .Nm
 1004 was to make its support
 1005 for new protocols independent from the rest of the library, giving it
 1006 the ability to load/unload support for new protocols at run-time.
 1007 To achieve this feature, all the code for protocol handling was moved
 1008 to a series of modules outside of the main library.
 1009 These modules are compiled from the same sources but work in
 1010 different ways, depending on whether they are compiled to work inside a kernel
 1011 or as part of the userland library.
 1012 .Ss LIBALIAS MODULES IN KERNEL LAND
 1013 When compiled for the kernel,
 1014 .Nm
 1015 modules are plain KLDs recognizable with the
 1016 .Pa alias_
 1017 prefix.
 1018 .Pp
 1019 To add support for a new protocol, load the corresponding module.
 1020 For example:
 1021 .Pp
 1022 .Dl "kldload alias_ftp"
 1023 .Pp
 1024 When support for a protocol is no longer needed, its module can be unloaded:
 1025 .Pp
 1026 .Dl "kldunload alias_ftp"
 1027 .Ss LIBALIAS MODULES IN USERLAND
 1028 Due to the differences between kernel and userland (no KLD mechanism,
 1029 many different address spaces, etc.), we had to change a bit how to
 1030 handle module loading/tracking/unloading in userland.
 1031 .Pp
 1032 While compiled for a userland
 1033 .Nm ,
 1034 all the modules are plain libraries, residing in
 1035 .Pa /usr/lib ,
 1036 and recognizable with the
 1037 .Pa libalias_
 1038 prefix.
 1039 .Pp
 1040 There is a configuration file,
 1041 .Pa /etc/libalias.conf ,
 1042 with the following contents (by default):
 1043 .Bd -literal -offset indent
 1044 /usr/lib/libalias_ftp.so
 1045 /usr/lib/libalias_irc.so
 1046 /usr/lib/libalias_nbt.so
 1047 /usr/lib/libalias_pptp.so
 1048 /usr/lib/libalias_skinny.so
 1049 /usr/lib/libalias_smedia.so
 1050 .Ed
 1051 .Pp
 1052 This file contains the paths to the modules that
 1053 .Nm
 1054 will load.
 1055 To load/unload a new module, just add its path to
 1056 .Pa libalias.conf
 1057 and call
 1058 .Fn LibAliasRefreshModules
 1059 from the program.
 1060 In case the application provides a
 1061 .Dv SIGHUP
 1062 signal handler, add a call to
 1063 .Fn LibAliasRefreshModules
 1064 inside the handler, and every time you want to refresh the loaded modules,
 1065 send it the
 1066 .Dv SIGHUP
 1067 signal:
 1068 .Pp
 1069 .Dl "kill -HUP <process_pid>"
 1070 .Ss MODULAR ARCHITECURE: HOW IT WORKS
 1071 The modular architecture of
 1072 .Nm
 1073 works similar whether it is running inside the
 1074 kernel or in userland.
 1075 From
 1076 .Pa alias_mod.c :
 1077 .Bd -literal
 1078 /* Protocol and userland module handlers chains. */
 1079 LIST_HEAD(handler_chain, proto_handler) handler_chain ...
 1080 \&...
 1081 SLIST_HEAD(dll_chain, dll) dll_chain ...
 1082 .Ed
 1083 .Pp
 1084 .Va handler_chain
 1085 keeps track of all the protocol handlers loaded, while
 1086 .Va ddl_chain
 1087 tracks which userland modules are loaded.
 1088 .Pp
 1089 .Va handler_chain
 1090 is composed of
 1091 .Vt "struct proto_handler"
 1092 entries:
 1093 .Bd -literal
 1094 struct proto_handler {
 1095         u_int pri;
 1096         int16_t dir;
 1097         uint8_t proto;
 1098         int (*fingerprint)(struct libalias *la,
 1099                  struct ip *pip, struct alias_data *ah);
 1100         int (*protohandler)(struct libalias *la,
 1101                  struct ip *pip, struct alias_data *ah);
 1102         TAILQ_ENTRY(proto_handler) link;
 1103 };
 1104 .Ed
 1105 .Pp
 1106 where:
 1107 .Bl -inset
 1108 .It Va pri
 1109 is the priority assigned to a protocol handler; lower priority
 1110 is better.
 1111 .It Va dir
 1112 is the direction of packets: ingoing or outgoing.
 1113 .It Va proto
 1114 indicates to which protocol this packet belongs: IP, TCP or UDP.
 1115 .It Va fingerprint
 1116 points to the fingerprint function while protohandler points
 1117 to the protocol handler function.
 1118 .El
 1119 .Pp
 1120 The
 1121 .Va fingerprint
 1122 function has the dual role of checking if the
 1123 incoming packet is found, and if it belongs to any categories that this
 1124 module can handle.
 1125 .Pp
 1126 The
 1127 .Va protohandler
 1128 function actually manipulates
 1129 the packet to make
 1130 .Nm
 1131 correctly NAT it.
 1132 .Pp
 1133 When a packet enters
 1134 .Nm ,
 1135 if it meets a module hook,
 1136 .Va handler_chain
 1137 is searched to see if there is an handler that matches
 1138 this type of a packet (it checks protocol and direction of packet).
 1139 Then, if more than one handler is found, it starts with the module with
 1140 the lowest priority number: it calls the
 1141 .Va fingerprint
 1142 function and interprets the result.
 1143 .Pp
 1144 If the result value is equal to 0 then it calls the protocol handler
 1145 of this handler and returns.
 1146 Otherwise, it proceeds to the next eligible module until the
 1147 .Va handler_chain
 1148 is exhausted.
 1149 .Pp
 1150 Inside
 1151 .Nm ,
 1152 the module hook looks like this:
 1153 .Bd -literal -offset indent
 1154 struct alias_data ad = {
 1155         lnk,
 1156         &original_address,
 1157         &alias_address,
 1158         &alias_port,
 1159         &ud->uh_sport,          /* original source port */
 1160         &ud->uh_dport,          /* original dest port */
 1161         256                     /* maxpacketsize */
 1162 };
 1163 
 1164 \&...
 1165 
 1166 /* walk out chain */
 1167 err = find_handler(IN, UDP, la, pip, &ad);
 1168 .Ed
 1169 .Pp
 1170 All data useful to a module are gathered together in an
 1171 .Vt alias_data
 1172 structure, then
 1173 .Fn find_handler
 1174 is called.
 1175 The
 1176 .Fn find_handler
 1177 function is responsible for walking the handler
 1178 chain; it receives as input parameters:
 1179 .Bl -tag -width indent
 1180 .It Fa IN
 1181 direction
 1182 .It Fa UDP
 1183 working protocol
 1184 .It Fa la
 1185 pointer to this instance of libalias
 1186 .It Fa pip
 1187 pointer to a
 1188 .Vt "struct ip"
 1189 .It Fa ad
 1190 pointer to
 1191 .Vt "struct alias_data"
 1192 (see above)
 1193 .El
 1194 .Pp
 1195 In this case,
 1196 .Fn find_handler
 1197 will search only for modules registered for
 1198 supporting INcoming UDP packets.
 1199 .Pp
 1200 As was mentioned earlier,
 1201 .Nm
 1202 in userland is a bit different, as
 1203 care must be taken in module handling as well (avoiding duplicate load of
 1204 modules, avoiding modules with same name, etc.) so
 1205 .Va dll_chain
 1206 was introduced.
 1207 .Pp
 1208 .Va dll_chain
 1209 contains a list of all userland
 1210 .Nm
 1211 modules loaded.
 1212 .Pp
 1213 When an application calls
 1214 .Fn LibAliasRefreshModules ,
 1215 .Nm
 1216 first unloads all the loaded modules, then reloads all the modules listed in
 1217 .Pa /etc/libalias.conf :
 1218 for every module loaded, a new entry is added to
 1219 .Va dll_chain .
 1220 .Pp
 1221 .Va dll_chain
 1222 is composed of
 1223 .Vt "struct dll"
 1224 entries:
 1225 .Bd -literal
 1226 struct dll {
 1227         /* name of module */
 1228         char            name[DLL_LEN];
 1229         /*
 1230          * ptr to shared obj obtained through
 1231          * dlopen() - use this ptr to get access
 1232          * to any symbols from a loaded module
 1233          * via dlsym()
 1234          */
 1235         void            *handle;
 1236         struct dll      *next;
 1237 };
 1238 .Ed
 1239 .Bl -inset
 1240 .It Va name
 1241 is the name of the module.
 1242 .It Va handle
 1243 is a pointer to the module obtained through
 1244 .Xr dlopen 3 .
 1245 .El
 1246 Whenever a module is loaded in userland, an entry is added to
 1247 .Va dll_chain ,
 1248 then every protocol handler present in that module
 1249 is resolved and registered in
 1250 .Va handler_chain .
 1251 .Ss HOW TO WRITE A MODULE FOR LIBALIAS
 1252 There is a module (called
 1253 .Pa alias_dummy.[ch] )
 1254 in
 1255 .Nm
 1256 that can be used as a skeleton for future work.
 1257 Here we analyse some parts of that module.
 1258 From
 1259 .Pa alias_dummy.c :
 1260 .Bd -literal
 1261 struct proto_handler handlers[] = {
 1262     {
 1263         .pri = 666,
 1264         .dir = IN|OUT,
 1265         .proto = UDP|TCP,
 1266         .fingerprint = fingerprint,
 1267         .protohandler= protohandler,
 1268     },
 1269     { EOH }
 1270 };
 1271 .Ed
 1272 .Pp
 1273 The variable
 1274 .Va handlers
 1275 is the
 1276 .Dq "most important thing"
 1277 in a module
 1278 since it describes the handlers present and lets the outside world use
 1279 it in an opaque way.
 1280 .Pp
 1281 It must ALWAYS be present in every module, and it MUST retain
 1282 the name
 1283 .Va handlers ,
 1284 otherwise attempting to load a module in userland will fail and
 1285 complain about missing symbols: for more information about module
 1286 load/unload, please refer to
 1287 .Fn LibAliasRefreshModules ,
 1288 .Fn LibAliasLoadModule
 1289 and
 1290 .Fn LibAliasUnloadModule
 1291 in
 1292 .Pa alias.c .
 1293 .Pp
 1294 .Va handlers
 1295 contains all the
 1296 .Vt proto_handler
 1297 structures present in a module.
 1298 .Bd -literal
 1299 static int
 1300 mod_handler(module_t mod, int type, void *data)
 1301 {
 1302         int error;
 1303 
 1304         switch (type) {
 1305         case MOD_LOAD:
 1306                 error = LibAliasAttachHandlers(handlers);
 1307                 break;
 1308         case MOD_UNLOAD:
 1309                 error = LibAliasDetachHandlers(handlers);
 1310                 break;
 1311         default:
 1312                 error = EINVAL;
 1313         }
 1314         return (error);
 1315 }
 1316 .Ed
 1317 When running as KLD,
 1318 .Fn mod_handler
 1319 registers/deregisters the module using
 1320 .Fn LibAliasAttachHandlers
 1321 and
 1322 .Fn LibAliasDetachHandlers ,
 1323 respectively.
 1324 .Pp
 1325 Every module must contain at least 2 functions: one fingerprint
 1326 function and a protocol handler function.
 1327 .Bd -literal
 1328 #ifdef _KERNEL
 1329 static
 1330 #endif
 1331 int
 1332 fingerprint(struct libalias *la, struct ip *pip, struct alias_data *ah)
 1333 {
 1334 
 1335 \&...
 1336 }
 1337 
 1338 #ifdef _KERNEL
 1339 static
 1340 #endif
 1341 int
 1342 protohandler(struct libalias *la, struct ip *pip,
 1343              struct alias_data *ah)
 1344 {
 1345 
 1346 \&...
 1347 }
 1348 .Ed
 1349 and they must accept exactly these input parameters.
 1350 .Ss PATCHING AN APPLICATION FOR USERLAND LIBALIAS MODULES
 1351 To add module support into an application that uses
 1352 .Nm ,
 1353 the following simple steps can be followed.
 1354 .Bl -enum
 1355 .It
 1356 Find the main file of an application
 1357 (let us call it
 1358 .Pa main.c ) .
 1359 .It
 1360 Add this to the header section of
 1361 .Pa main.c ,
 1362 if not already present:
 1363 .Pp
 1364 .Dl "#include <signal.h>"
 1365 .Pp
 1366 and this just after the header section:
 1367 .Pp
 1368 .Dl "static void signal_handler(int);"
 1369 .It
 1370 Add the following line to the init function of an application or,
 1371 if it does not have any init function, put it in
 1372 .Fn main :
 1373 .Pp
 1374 .Dl "signal(SIGHUP, signal_handler);"
 1375 .Pp
 1376 and place the
 1377 .Fn signal_handler
 1378 function somewhere in
 1379 .Pa main.c :
 1380 .Bd -literal -offset indent
 1381 static void
 1382 signal_handler(int sig)
 1383 {
 1384 
 1385         LibAliasRefreshModules();
 1386 }
 1387 .Ed
 1388 .Pp
 1389 Otherwise, if an application already traps the
 1390 .Dv SIGHUP
 1391 signal, just add a call to
 1392 .Fn LibAliasRefreshModules
 1393 in the signal handler function.
 1394 .El
 1395 For example, to patch
 1396 .Xr natd 8
 1397 to use
 1398 .Nm
 1399 modules, just add the following line to
 1400 .Fn RefreshAddr "int sig __unused" :
 1401 .Pp
 1402 .Dl "LibAliasRefreshModules()"
 1403 .Pp
 1404 recompile and you are done.
 1405 .Ss LOGGING SUPPORT IN KERNEL LAND
 1406 When working as KLD,
 1407 .Nm
 1408 now has log support that
 1409 happens on a buffer allocated inside
 1410 .Vt "struct libalias"
 1411 (from
 1412 .Pa alias_local.h ) :
 1413 .Bd -literal
 1414 struct libalias {
 1415        ...
 1416 
 1417         /* log descriptor        */
 1418 #ifdef  KERNEL_LOG
 1419         char           *logDesc;        /*
 1420                                          * ptr to an auto-malloced
 1421                                          * memory buffer when libalias
 1422                                          * works as kld
 1423                                          */
 1424 #else
 1425         FILE           *logDesc;        /*
 1426                                          * ptr to /var/log/alias.log
 1427                                          * when libalias runs as a
 1428                                          * userland lib
 1429                                          */
 1430 #endif
 1431 
 1432         ...
 1433 }
 1434 .Ed
 1435 so all applications using
 1436 .Nm
 1437 will be able to handle their
 1438 own logs, if they want, accessing
 1439 .Va logDesc .
 1440 Moreover, every change to a log buffer is automatically added to
 1441 .Xr syslog 3
 1442 with the
 1443 .Dv LOG_SECURITY
 1444 facility and the
 1445 .Dv LOG_INFO
 1446 level.
 1447 .Sh AUTHORS
 1448 .An Charles Mott Aq cm@linktel.net ,
 1449 versions 1.0 - 1.8, 2.0 - 2.4.
 1450 .An Eivind Eklund Aq eivind@FreeBSD.org ,
 1451 versions 1.8b, 1.9 and 2.5.
 1452 Added IRC DCC support as well as contributing a number of architectural
 1453 improvements; added the firewall bypass for FTP/IRC DCC.
 1454 .An Erik Salander Aq erik@whistle.com
 1455 added support for PPTP and RTSP.
 1456 .An Junichi Satoh Aq junichi@junichi.org
 1457 added support for RTSP/PNA.
 1458 .An Ruslan Ermilov Aq ru@FreeBSD.org
 1459 added support for PPTP and LSNAT as well as general hacking.
 1460 .An Gleb Smirnoff Aq glebius@FreeBSD.org
 1461 ported the library to kernel space.
 1462 .An Paolo Pisati Aq piso@FreeBSD.org
 1463 made the library modular, moving support for all
 1464 protocols (except for IP, TCP and UDP) to external modules.
 1465 .Sh ACKNOWLEDGEMENTS
 1466 Listed below, in approximate chronological order, are individuals who
 1467 have provided valuable comments and/or debugging assistance.
 1468 .Bd -ragged -offset indent
 1469 .An -split
 1470 .An Gary Roberts
 1471 .An Tom Torrance
 1472 .An Reto Burkhalter
 1473 .An Martin Renters
 1474 .An Brian Somers
 1475 .An Paul Traina
 1476 .An Ari Suutari
 1477 .An Dave Remien
 1478 .An J. Fortes
 1479 .An Andrzej Bialecki
 1480 .An Gordon Burditt
 1481 .Ed

Cache object: 3ccaf39915d01a8669caf839a2359337


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.