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