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