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