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