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