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