35f42fcdaff154cbf29dcbb247d1b0dfa7abf7f2
[dragonfly.git] / lib / 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: src/lib/libalias/libalias.3,v 1.23.2.11 2001/12/17 10:08:22 ru Exp $
27 .\" $DragonFly: src/lib/libalias/libalias.3,v 1.6 2008/05/02 02:05:03 swildner Exp $
28 .\"
29 .Dd April 13, 2000
30 .Dt LIBALIAS 3
31 .Os
32 .Sh NAME
33 .Nm libalias
34 .Nd packet aliasing library for masquerading and network address translation
35 .Sh LIBRARY
36 .Lb libalias
37 .Sh SYNOPSIS
38 .In sys/types.h
39 .In netinet/in.h
40 .In alias.h
41 .Pp
42 Function prototypes are given in the main body of the text.
43 .Sh DESCRIPTION
44 The
45 .Nm
46 library is a collection of functions for aliasing and de-aliasing of IP
47 packets, intended for masquerading and network address translation (NAT).
48 .Sh INTRODUCTION
49 This library is a moderately portable set of functions designed to assist
50 in the process of IP masquerading and network address translation.
51 Outgoing packets from a local network with unregistered IP addresses can
52 be aliased to appear as if they came from an accessible IP address.
53 Incoming packets are then de-aliased so that they are sent to the correct
54 machine on the local network.
55 .Pp
56 A certain amount of flexibility is built into the packet aliasing engine.
57 In the simplest mode of operation, a many-to-one address mapping takes
58 place between local network and the packet aliasing host.
59 This is known as IP masquerading.
60 In addition, one-to-one mappings between local and public addresses can
61 also be implemented, which is known as static NAT.
62 In between these extremes, different groups of private addresses can be
63 linked to different public addresses, comprising several distinct
64 many-to-one mappings.
65 Also, a given public address and port can be statically redirected to a
66 private address/port.
67 .Pp
68 The packet aliasing engine was designed to operate in user space outside
69 of the kernel, without any access to private kernel data structure, but
70 the source code can also be ported to a kernel environment.
71 .Sh INITIALIZATION AND CONTROL
72 Two special functions,
73 .Fn PacketAliasInit
74 and
75 .Fn PacketAliasSetAddress ,
76 must always be called before any packet handling may be performed.
77 In addition, the operating mode of the packet aliasing engine can be
78 customized by calling
79 .Fn PacketAliasSetMode .
80 .Pp
81 .Ft void
82 .Fn PacketAliasInit void
83 .Bd -ragged -offset indent
84 This function has no arguments or return value and is used to initialize
85 internal data structures.
86 The following mode bits are always set after calling
87 .Fn PacketAliasInit .
88 See the description of
89 .Fn PacketAliasSetMode
90 below for the meaning of these mode bits.
91 .Pp
92 .Bl -item -offset indent -compact
93 .It
94 .Dv PKT_ALIAS_SAME_PORTS
95 .It
96 .Dv PKT_ALIAS_USE_SOCKETS
97 .It
98 .Dv PKT_ALIAS_RESET_ON_ADDR_CHANGE
99 .El
100 .Pp
101 This function will always return the packet aliasing engine to the same
102 initial state.
103 .Fn PacketAliasSetAddress
104 must be called afterwards, and any desired changes from the default mode
105 bits listed above require a call to
106 .Fn PacketAliasSetMode .
107 .Pp
108 It is mandatory that this function be called at the beginning of a program
109 prior to any packet handling.
110 .Ed
111 .Pp
112 .Ft void
113 .Fn PacketAliasUninit void
114 .Bd -ragged -offset indent
115 This function has no arguments or return value and is used to clear any
116 resources attached to internal data structures.
117 .Pp
118 This functions should be called when a program stops using the aliasing
119 engine; it does, amongst other things, clear out any firewall holes.
120 To provide backwards compatibility and extra security, it is added to
121 the
122 .Xr atexit 3
123 chain by
124 .Fn PacketAliasInit .
125 Calling it multiple times is harmless.
126 .Ed
127 .Pp
128 .Ft void
129 .Fn PacketAliasSetAddress "struct in_addr addr"
130 .Bd -ragged -offset indent
131 This function sets the source address to which outgoing packets from the
132 local area network are aliased.
133 All outgoing packets are re-mapped to this address unless overridden by a
134 static address mapping established by
135 .Fn PacketAliasRedirectAddr .
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 PacketAliasSetMode "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
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 ( PacketAliasIn
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 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 PacketAliasSetAddress
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 `punch holes' in an
229 .Xr ipfirewall 4
230 based firewall for FTP/IRC DCC connections.
231 The holes punched are bound by from/to IP address and port; it will not be
232 possible to use a hole for another connection.
233 A hole is removed when the connection that uses it dies.
234 To cater to unexpected death of a program using
235 .Nm
236 (e.g. kill -9),
237 changing the state of the flag will clear the entire firewall range
238 allocated for holes.
239 This will also happen on the initial call to
240 .Fn PacketAliasSetFWBase .
241 This call must happen prior to setting this flag.
242 .It Dv PKT_ALIAS_REVERSE
243 This option makes
244 .Nm
245 reverse the way it handles incoming and outgoing packets, allowing it
246 to be fed with data that passes through the internal interface rather
247 than the external one.
248 .It Dv PKT_ALIAS_PROXY_ONLY
249 This option tells
250 .Nm
251 to obey transparent proxy rules only.
252 Normal packet aliasing is not performed.
253 See
254 .Fn PacketAliasProxyRule
255 below for details.
256 .El
257 .Ed
258 .Pp
259 .Ft void
260 .Fn PacketAliasSetFWBase "unsigned int base" "unsigned int num"
261 .Bd -ragged -offset indent
262 Set firewall range allocated for punching firewall holes (with the
263 .Dv PKT_ALIAS_PUNCH_FW
264 flag).
265 The range will be cleared for all rules on initialization.
266 .Ed
267 .Sh PACKET HANDLING
268 The packet handling functions are used to modify incoming (remote to local)
269 and outgoing (local to remote) packets.
270 The calling program is responsible for receiving and sending packets via
271 network interfaces.
272 .Pp
273 Along with
274 .Fn PacketAliasInit
275 and
276 .Fn PacketAliasSetAddress ,
277 the two packet handling functions,
278 .Fn PacketAliasIn
279 and
280 .Fn PacketAliasOut ,
281 comprise minimal set of functions needed for a basic IP masquerading
282 implementation.
283 .Pp
284 .Ft int
285 .Fn PacketAliasIn "char *buffer" "int maxpacketsize"
286 .Bd -ragged -offset indent
287 An incoming packet coming from a remote machine to the local network is
288 de-aliased by this function.
289 The IP packet is pointed to by
290 .Fa buffer ,
291 and
292 .Fa maxpacketsize
293 indicates the size of the data structure containing the packet and should
294 be at least as large as the actual packet size.
295 .Pp
296 Return codes:
297 .Bl -tag -width indent
298 .It Dv PKT_ALIAS_OK
299 The packet aliasing process was successful.
300 .It Dv PKT_ALIAS_IGNORED
301 The packet was ignored and not de-aliased.
302 This can happen if the protocol is unrecognized, possibly an ICMP message
303 type is not handled or if incoming packets for new connections are being
304 ignored (if
305 .Dv PKT_ALIAS_DENY_INCOMING
306 mode bit was set by
307 .Fn PacketAliasSetMode ) .
308 .It Dv PKT_ALIAS_UNRESOLVED_FRAGMENT
309 This is returned when a fragment cannot be resolved because the header
310 fragment has not been sent yet.
311 In this situation, fragments must be saved with
312 .Fn PacketAliasSaveFragment
313 until a header fragment is found.
314 .It Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT
315 The packet aliasing process was successful, and a header fragment was found.
316 This is a signal to retrieve any unresolved fragments with
317 .Fn PacketAliasGetFragment
318 and de-alias them with
319 .Fn PacketAliasFragmentIn .
320 .It Dv PKT_ALIAS_ERROR
321 An internal error within the packet aliasing engine occurred.
322 .El
323 .Ed
324 .Pp
325 .Ft int
326 .Fn PacketAliasOut "char *buffer" "int maxpacketsize"
327 .Bd -ragged -offset indent
328 An outgoing packet coming from the local network to a remote machine is
329 aliased by this function.
330 The IP packet is pointed to by
331 .Fa buffer ,
332 and
333 .Fa maxpacketsize
334 indicates the maximum packet size permissible should the packet length be
335 changed.
336 IP encoding protocols place address and port information in the encapsulated
337 data stream which has to be modified and can account for changes in packet
338 length.
339 Well known examples of such protocols are FTP and IRC DCC.
340 .Pp
341 Return codes:
342 .Bl -tag -width indent
343 .It Dv PKT_ALIAS_OK
344 The packet aliasing process was successful.
345 .It Dv PKT_ALIAS_IGNORED
346 The packet was ignored and not aliased.
347 This can happen if the protocol is unrecognized, or possibly an ICMP message
348 type is not handled.
349 .It Dv PKT_ALIAS_ERROR
350 An internal error within the packet aliasing engine occurred.
351 .El
352 .Ed
353 .Sh PORT AND ADDRESS REDIRECTION
354 The functions described in this section allow machines on the local network
355 to be accessible in some degree to new incoming connections from the external
356 network.
357 Individual ports can be re-mapped or static network address translations can
358 be designated.
359 .Pp
360 .Ft struct alias_link *
361 .Fo PacketAliasRedirectPort
362 .Fa "struct in_addr local_addr"
363 .Fa "u_short local_port"
364 .Fa "struct in_addr remote_addr"
365 .Fa "u_short remote_port"
366 .Fa "struct in_addr alias_addr"
367 .Fa "u_short alias_port"
368 .Fa "u_char proto"
369 .Fc
370 .Bd -ragged -offset indent
371 This function specifies that traffic from a given remote address/port to
372 an alias address/port be redirected to a specified local address/port.
373 The parameter
374 .Fa proto
375 can be either
376 .Dv IPPROTO_TCP
377 or
378 .Dv IPPROTO_UDP ,
379 as defined in
380 .In netinet/in.h .
381 .Pp
382 If
383 .Fa local_addr
384 or
385 .Fa alias_addr
386 is zero, this indicates that the packet aliasing address as established
387 by
388 .Fn PacketAliasSetAddress
389 is to be used.
390 Even if
391 .Fn PacketAliasSetAddress
392 is called to change the address after
393 .Fn PacketAliasRedirectPort
394 is called, a zero reference will track this change.
395 .Pp
396 If the link is further set up to operate for a load sharing, then
397 .Fa local_addr
398 and
399 .Fa local_port
400 are ignored, and are selected dynamically from the server pool, as described in
401 .Fn PacketAliasAddServer
402 below.
403 .Pp
404 If
405 .Fa remote_addr
406 is zero, this indicates to redirect packets from any remote address.
407 Likewise, if
408 .Fa remote_port
409 is zero, this indicates to redirect packets originating from any remote
410 port number.
411 Almost always, the remote port specification will be zero, but non-zero
412 remote addresses can sometimes be useful for firewalling.
413 If two calls to
414 .Fn PacketAliasRedirectPort
415 overlap in their address/port specifications, then the most recent call
416 will have precedence.
417 .Pp
418 This function returns a pointer which can subsequently be used by
419 .Fn PacketAliasRedirectDelete .
420 If
421 .Dv NULL
422 is returned, then the function call did not complete successfully.
423 .Pp
424 All port numbers should be in network address byte order, so it is necessary
425 to use
426 .Xr htons 3
427 to convert these parameters from internally readable numbers to network byte
428 order.
429 Addresses are also in network byte order, which is implicit in the use of the
430 .Fa struct in_addr
431 data type.
432 .Ed
433 .Pp
434 .Ft struct alias_link *
435 .Fo PacketAliasRedirectAddr
436 .Fa "struct in_addr local_addr"
437 .Fa "struct in_addr alias_addr"
438 .Fc
439 .Bd -ragged -offset indent
440 This function designates that all incoming traffic to
441 .Fa alias_addr
442 be redirected to
443 .Fa local_addr .
444 Similarly, all outgoing traffic from
445 .Fa local_addr
446 is aliased to
447 .Fa alias_addr .
448 .Pp
449 If
450 .Fa local_addr
451 or
452 .Fa alias_addr
453 is zero, this indicates that the packet aliasing address as established by
454 .Fn PacketAliasSetAddress
455 is to be used.
456 Even if
457 .Fn PacketAliasSetAddress
458 is called to change the address after
459 .Fn PacketAliasRedirectAddr
460 is called, a zero reference will track this change.
461 .Pp
462 If the link is further set up to operate for a load sharing, then
463 .Fa local_addr
464 is ignored, and is selected dynamically from the server pool, as described in
465 .Fn PacketAliasAddServer
466 below.
467 .Pp
468 If subsequent calls to
469 .Fn PacketAliasRedirectAddr
470 use the same aliasing address, all new incoming traffic to this aliasing
471 address will be redirected to the local address made in the last function
472 call.
473 New traffic generated by any of the local machines, designated in the
474 several function calls, will be aliased to the same address.
475 Consider the following example:
476 .Bd -literal -offset indent
477 PacketAliasRedirectAddr(inet_aton("192.168.0.2"),
478                         inet_aton("141.221.254.101"));
479 PacketAliasRedirectAddr(inet_aton("192.168.0.3"),
480                         inet_aton("141.221.254.101"));
481 PacketAliasRedirectAddr(inet_aton("192.168.0.4"),
482                         inet_aton("141.221.254.101"));
483 .Ed
484 .Pp
485 Any outgoing connections such as
486 .Xr telnet 1
487 or
488 .Xr ftp 1
489 from 192.168.0.2, 192.168.0.3 and 192.168.0.4 will appear to come from
490 141.221.254.101.
491 Any incoming connections to 141.221.254.101 will be directed to 192.168.0.4.
492 .Pp
493 Any calls to
494 .Fn PacketAliasRedirectPort
495 will have precedence over address mappings designated by
496 .Fn PacketAliasRedirectAddr .
497 .Pp
498 This function returns a pointer which can subsequently be used by
499 .Fn PacketAliasRedirectDelete .
500 If
501 .Dv NULL
502 is returned, then the function call did not complete successfully.
503 .Ed
504 .Pp
505 .Ft int
506 .Fo PacketAliasAddServer
507 .Fa "struct alias_link *link"
508 .Fa "struct in_addr addr"
509 .Fa "u_short port"
510 .Fc
511 .Bd -ragged -offset indent
512 This function sets the
513 .Fa link
514 up for Load Sharing using IP Network Address Translation (RFC 2391, LSNAT).
515 LSNAT operates as follows.
516 A client attempts to access a server by using the server virtual address.
517 The LSNAT router transparently redirects the request to one of the hosts
518 in server pool, selected using a real-time load sharing algorithm.
519 Multiple sessions may be initiated from the same client, and each session
520 could be directed to a different host based on load balance across server
521 pool hosts at the time.
522 If load share is desired for just a few specific services, the configuration
523 on LSNAT could be defined to restrict load share for just the services
524 desired.
525 .Pp
526 Currently, only the simplest selection algorithm is implemented, where a
527 host is selected on a round-robin basis only, without regard to load on
528 the host.
529 .Pp
530 First, the
531 .Fa link
532 is created by either
533 .Fn PacketAliasRedirectPort
534 or
535 .Fn PacketAliasRedirectAddr .
536 Then,
537 .Fn PacketAliasAddServer
538 is called multiple times to add entries to the
539 .Fa link Ns 's
540 server pool.
541 .Pp
542 For links created with
543 .Fn PacketAliasRedirectAddr ,
544 the
545 .Fa port
546 argument is ignored and could have any value, e.g. htons(~0).
547 .Pp
548 This function returns 0 on success, -1 otherwise.
549 .Ed
550 .Pp
551 .Ft void
552 .Fn PacketAliasRedirectDelete "struct alias_link *link"
553 .Bd -ragged -offset indent
554 This function will delete a specific static redirect rule entered by
555 .Fn PacketAliasRedirectPort
556 or
557 .Fn PacketAliasRedirectAddr .
558 The parameter
559 .Fa link
560 is the pointer returned by either of the redirection functions.
561 If an invalid pointer is passed to
562 .Fn PacketAliasRedirectDelete ,
563 then a program crash or unpredictable operation could result, so it is
564 necessary to be careful using this function.
565 .Ed
566 .Pp
567 .Ft int
568 .Fn PacketAliasProxyRule "const char *cmd"
569 .Bd -ragged -offset indent
570 The passed
571 .Fa cmd
572 string consists of one or more pairs of words.
573 The first word in each pair is a token and the second is the value that
574 should be applied for that token.
575 Tokens and their argument types are as follows:
576 .Bl -tag -width indent
577 .It Cm type encode_ip_hdr | encode_tcp_stream | no_encode
578 In order to support transparent proxying, it is necessary to somehow
579 pass the original address and port information into the new destination
580 server.
581 If
582 .Cm encode_ip_hdr
583 is specified, the original address and port is passed as an extra IP
584 option.
585 If
586 .Cm encode_tcp_stream
587 is specified, the original address and port is passed as the first
588 piece of data in the TCP stream in the format
589 .Dq DEST Ar IP port .
590 .It Cm port Ar portnum
591 Only packets with the destination port
592 .Ar portnum
593 are proxied.
594 .It Cm server Ar host Ns Xo
595 .Op : Ns Ar portnum
596 .Xc
597 This specifies the
598 .Ar host
599 and
600 .Ar portnum
601 that the data is to be redirected to.
602 .Ar host
603 must be an IP address rather than a DNS host name.
604 If
605 .Ar portnum
606 is not specified, the destination port number is not changed.
607 .Pp
608 The
609 .Ar server
610 specification is mandatory unless the
611 .Cm delete
612 command is being used.
613 .It Cm rule Ar index
614 Normally, each call to
615 .Fn PacketAliasProxyRule
616 inserts the next rule at the start of a linear list of rules.
617 If an
618 .Ar index
619 is specified, the new rule will be checked after all rules with lower
620 indices.
621 Calls to
622 .Fn PacketAliasProxyRule
623 that do not specify a rule are assigned rule 0.
624 .It Cm delete Ar index
625 This token and its argument MUST NOT be used with any other tokens.
626 When used, all existing rules with the given
627 .Ar index
628 are deleted.
629 .It Cm proto tcp | udp
630 If specified, only packets of the given protocol type are matched.
631 .It Cm src Ar IP Ns Xo
632 .Op / Ns Ar bits
633 .Xc
634 If specified, only packets with a source address matching the given
635 .Ar IP
636 are matched.
637 If
638 .Ar bits
639 is also specified, then the first
640 .Ar bits
641 bits of
642 .Ar IP
643 are taken as a network specification, and all IP addresses from that
644 network will be matched.
645 .It Cm dst Ar IP Ns Xo
646 .Op / Ns Ar bits
647 .Xc
648 If specified, only packets with a destination address matching the given
649 .Ar IP
650 are matched.
651 If
652 .Ar bits
653 is also specified, then the first
654 .Ar bits
655 bits of
656 .Ar IP
657 are taken as a network specification, and all IP addresses from that
658 network will be matched.
659 .El
660 .Pp
661 This function is usually used to redirect outgoing connections for
662 internal machines that are not permitted certain types of internet
663 access, or to restrict access to certain external machines.
664 .Ed
665 .Pp
666 .Ft struct alias_link *
667 .Fo PacketAliasRedirectProto
668 .Fa "struct in_addr local_addr"
669 .Fa "struct in_addr remote_addr"
670 .Fa "struct in_addr alias_addr"
671 .Fa "u_char proto"
672 .Fc
673 .Bd -ragged -offset indent
674 This function specifies that any IP packet with protocol number of
675 .Fa proto
676 from a given remote address to an alias address be
677 redirected to a specified local address.
678 .Pp
679 If
680 .Fa local_addr
681 or
682 .Fa alias_addr
683 is zero, this indicates that the packet aliasing address as established
684 by
685 .Fn PacketAliasSetAddress
686 is to be used.
687 Even if
688 .Fn PacketAliasSetAddress
689 is called to change the address after
690 .Fn PacketAliasRedirectProto
691 is called, a zero reference will track this change.
692 .Pp
693 If
694 .Fa remote_addr
695 is zero, this indicates to redirect packets from any remote address.
696 Non-zero remote addresses can sometimes be useful for firewalling.
697 .Pp
698 If two calls to
699 .Fn PacketAliasRedirectProto
700 overlap in their address specifications, then the most recent call
701 will have precedence.
702 .Pp
703 This function returns a pointer which can subsequently be used by
704 .Fn PacketAliasRedirectDelete .
705 If
706 .Dv NULL
707 is returned, then the function call did not complete successfully.
708 .Ed
709 .Sh FRAGMENT HANDLING
710 The functions in this section are used to deal with incoming fragments.
711 .Pp
712 Outgoing fragments are handled within
713 .Fn PacketAliasOut
714 by changing the address according to any applicable mapping set by
715 .Fn PacketAliasRedirectAddr ,
716 or the default aliasing address set by
717 .Fn PacketAliasSetAddress .
718 .Pp
719 Incoming fragments are handled in one of two ways.
720 If the header of a fragmented IP packet has already been seen, then all
721 subsequent fragments will be re-mapped in the same manner the header
722 fragment was.
723 Fragments which arrive before the header are saved and then retrieved
724 once the header fragment has been resolved.
725 .Pp
726 .Ft int
727 .Fn PacketAliasSaveFragment "char *ptr"
728 .Bd -ragged -offset indent
729 When
730 .Fn PacketAliasIn
731 returns
732 .Dv PKT_ALIAS_UNRESOLVED_FRAGMENT ,
733 this function can be used to save the pointer to the unresolved fragment.
734 .Pp
735 It is implicitly assumed that
736 .Fa ptr
737 points to a block of memory allocated by
738 .Xr malloc 3 .
739 If the fragment is never resolved, the packet aliasing engine will
740 automatically free the memory after a timeout period.
741 [Eventually this function should be modified so that a callback function
742 for freeing memory is passed as an argument.]
743 .Pp
744 This function returns
745 .Dv PKT_ALIAS_OK
746 if it was successful and
747 .Dv PKT_ALIAS_ERROR
748 if there was an error.
749 .Ed
750 .Pp
751 .Ft char *
752 .Fn PacketAliasGetFragment "char *buffer"
753 .Bd -ragged -offset indent
754 This function can be used to retrieve fragment pointers saved by
755 .Fn PacketAliasSaveFragment .
756 The IP header fragment pointed to by
757 .Fa buffer
758 is the header fragment indicated when
759 .Fn PacketAliasIn
760 returns
761 .Dv PKT_ALIAS_FOUND_HEADER_FRAGMENT .
762 Once a fragment pointer is retrieved, it becomes the calling program's
763 responsibility to free the dynamically allocated memory for the fragment.
764 .Pp
765 .Fn PacketAliasGetFragment
766 can be called sequentially until there are no more fragments available,
767 at which time it returns
768 .Dv NULL .
769 .Ed
770 .Pp
771 .Ft void
772 .Fn PacketAliasFragmentIn "char *header" "char *fragment"
773 .Bd -ragged -offset indent
774 When a fragment is retrieved with
775 .Fn PacketAliasGetFragment ,
776 it can then be de-aliased with a call to
777 .Fn PacketAliasFragmentIn .
778 The
779 .Fa header
780 argument is the pointer to a header fragment used as a template, and
781 .Fa fragment
782 is the pointer to the packet to be de-aliased.
783 .Ed
784 .Sh MISCELLANEOUS FUNCTIONS
785 .Ft void
786 .Fn PacketAliasSetTarget "struct in_addr addr"
787 .Bd -ragged -offset indent
788 When an incoming packet not associated with any pre-existing aliasing link
789 arrives at the host machine, it will be sent to the address indicated by a
790 call to
791 .Fn PacketAliasSetTarget .
792 .Pp
793 If this function is called with an
794 .Dv INADDR_NONE
795 address argument, then all new incoming packets go to the address set by
796 .Fn PacketAliasSetAddress .
797 .Pp
798 If this function is not called, or is called with an
799 .Dv INADDR_ANY
800 address argument, then all new incoming packets go to the address specified
801 in the packet.
802 This allows external machines to talk directly to internal machines if they
803 can route packets to the machine in question.
804 .Ed
805 .Pp
806 .Ft int
807 .Fn PacketAliasCheckNewLink void
808 .Bd -ragged -offset indent
809 This function returns a non-zero value when a new aliasing link is created.
810 In circumstances where incoming traffic is being sequentially sent to
811 different local servers, this function can be used to trigger when
812 .Fn PacketAliasSetTarget
813 is called to change the default target address.
814 .Ed
815 .Pp
816 .Ft u_short
817 .Fn PacketAliasInternetChecksum "u_short *buffer" "int nbytes"
818 .Bd -ragged -offset indent
819 This is a utility function that does not seem to be available elsewhere and
820 is included as a convenience.
821 It computes the internet checksum, which is used in both IP and
822 protocol-specific headers (TCP, UDP, ICMP).
823 .Pp
824 The
825 .Fa buffer
826 argument points to the data block to be checksummed, and
827 .Fa nbytes
828 is the number of bytes.
829 The 16-bit checksum field should be zeroed before computing the checksum.
830 .Pp
831 Checksums can also be verified by operating on a block of data including
832 its checksum.
833 If the checksum is valid,
834 .Fn PacketAliasInternetChecksum
835 will return zero.
836 .Ed
837 .Pp
838 .Ft int
839 .Fn PacketUnaliasOut "char *buffer" "int maxpacketsize"
840 .Bd -ragged -offset indent
841 An outgoing packet, which has already been aliased,
842 has its private address/port information restored by this function.
843 The IP packet is pointed to by
844 .Fa buffer ,
845 and
846 .Fa maxpacketsize
847 is provided for error checking purposes.
848 This function can be used if an already-aliased packet needs to have its
849 original IP header restored for further processing (eg. logging).
850 .Ed
851 .Sh AUTHORS
852 .An Charles Mott Aq cm@linktel.net ,
853 versions 1.0 - 1.8, 2.0 - 2.4.
854 .An Eivind Eklund Aq eivind@FreeBSD.org ,
855 versions 1.8b, 1.9 and 2.5.
856 Added IRC DCC support as well as contributing a number of architectural
857 improvements; added the firewall bypass for FTP/IRC DCC.
858 .An Erik Salander Aq erik@whistle.com
859 added support for PPTP and RTSP.
860 .An Junichi Satoh Aq junichi@junichi.org
861 added support for RTSP/PNA.
862 .Sh ACKNOWLEDGMENTS
863 Listed below, in approximate chronological order, are individuals who
864 have provided valuable comments and/or debugging assistance.
865 .Bd -ragged -offset indent
866 .An -split
867 .An Gary Roberts
868 .An Tom Torrance
869 .An Reto Burkhalter
870 .An Martin Renters
871 .An Brian Somers
872 .An Paul Traina
873 .An Ari Suutari
874 .An Dave Remien
875 .An J. Fortes
876 .An Andrzej Bialecki
877 .An Gordon Burditt
878 .Ed
879 .Sh CONCEPTUAL BACKGROUND
880 This section is intended for those who are planning to modify the source
881 code or want to create somewhat esoteric applications using the packet
882 aliasing functions.
883 .Pp
884 The conceptual framework under which the packet aliasing engine operates
885 is described here.
886 Central to the discussion is the idea of an
887 .Em aliasing link
888 which describes the relationship for a given packet transaction between
889 the local machine, aliased identity and remote machine.
890 It is discussed how such links come into existence and are destroyed.
891 .Ss ALIASING LINKS
892 There is a notion of an
893 .Em aliasing link ,
894 which is a 7-tuple describing a specific translation:
895 .Bd -literal -offset indent
896 (local addr, local port, alias addr, alias port,
897  remote addr, remote port, protocol)
898 .Ed
899 .Pp
900 Outgoing packets have the local address and port number replaced with the
901 alias address and port number.
902 Incoming packets undergo the reverse process.
903 The packet aliasing engine attempts to match packets against an internal
904 table of aliasing links to determine how to modify a given IP packet.
905 Both the IP header and protocol dependent headers are modified as necessary.
906 Aliasing links are created and deleted as necessary according to network
907 traffic.
908 .Pp
909 Protocols can be TCP, UDP or even ICMP in certain circumstances.
910 (Some types of ICMP packets can be aliased according to sequence or ID
911 number which acts as an equivalent port number for identifying how
912 individual packets should be handled.)
913 .Pp
914 Each aliasing link must have a unique combination of the following five
915 quantities: alias address/port, remote address/port and protocol.
916 This ensures that several machines on a local network can share the
917 same aliasing IP address.
918 In cases where conflicts might arise, the aliasing port is chosen so that
919 uniqueness is maintained.
920 .Ss STATIC AND DYNAMIC LINKS
921 Aliasing links can either be static or dynamic.
922 Static links persist indefinitely and represent fixed rules for translating
923 IP packets.
924 Dynamic links come into existence for a specific TCP connection or UDP
925 transaction or ICMP ECHO sequence.
926 For the case of TCP, the connection can be monitored to see when the
927 associated aliasing link should be deleted.
928 Aliasing links for UDP transactions (and ICMP ECHO and TIMESTAMP requests)
929 work on a simple timeout rule.
930 When no activity is observed on a dynamic link for a certain amount of time
931 it is automatically deleted.
932 Timeout rules also apply to TCP connections which do not open or close
933 properly.
934 .Ss PARTIALLY SPECIFIED ALIASING LINKS
935 Aliasing links can be partially specified, meaning that the remote address
936 and/or remote port are unknown.
937 In this case, when a packet matching the incomplete specification is found,
938 a fully specified dynamic link is created.
939 If the original partially specified link is dynamic, it will be deleted
940 after the fully specified link is created, otherwise it will persist.
941 .Pp
942 For instance, a partially specified link might be
943 .Bd -literal -offset indent
944 (192.168.0.4, 23, 204.228.203.215, 8066, 0, 0, tcp)
945 .Ed
946 .Pp
947 The zeros denote unspecified components for the remote address and port.
948 If this link were static it would have the effect of redirecting all
949 incoming traffic from port 8066 of 204.228.203.215 to port 23 (telnet)
950 of machine 192.168.0.4 on the local network.
951 Each individual telnet connection would initiate the creation of a distinct
952 dynamic link.
953 .Ss DYNAMIC LINK CREATION
954 In addition to aliasing links, there are also address mappings that can be
955 stored within the internal data table of the packet aliasing mechanism.
956 .Bd -literal -offset indent
957 (local addr, alias addr)
958 .Ed
959 .Pp
960 Address mappings are searched when creating new dynamic links.
961 .Pp
962 All outgoing packets from the local network automatically create a dynamic
963 link if they do not match an already existing fully specified link.
964 If an address mapping exists for the outgoing packet, this determines
965 the alias address to be used.
966 If no mapping exists, then a default address, usually the address of the
967 packet aliasing host, is used.
968 If necessary, this default address can be changed as often as each individual
969 packet arrives.
970 .Pp
971 The aliasing port number is determined such that the new dynamic link does
972 not conflict with any existing links.
973 In the default operating mode, the packet aliasing engine attempts to set
974 the aliasing port equal to the local port number.
975 If this results in a conflict, then port numbers are randomly chosen until
976 a unique aliasing link can be established.
977 In an alternate operating mode, the first choice of an aliasing port is also
978 random and unrelated to the local port number.
979 .Sh BUGS
980 PPTP aliasing does not work when more than one internal client
981 connects to the same external server at the same time, because
982 PPTP requires a single TCP control connection to be established
983 between any two IP addresses.