Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / man / ipnat.5
1 .TH IPNAT 5
2 .SH NAME
3 ipnat, ipnat.conf \- IP NAT file format
4 .SH DESCRIPTION
5 The format for files accepted by ipnat is described by the following grammar:
6 .LP
7 .nf
8 ipmap :: = mapblock | redir | map .
9
10 map ::= mapit ifname ipmask "->" dstipmask [ mapport ] mapoptions.
11 map ::= mapit ifname fromto "->" dstipmask [ mapport ] mapoptions.
12 mapblock ::= "map-block" ifname ipmask "->" ipmask [ ports ] mapoptions.
13 redir ::= "rdr" ifname ipmask dport "->" ip [ "," ip ] rdrport rdroptions .
14
15 dport ::= "port" portnum [ "-" portnum ] .
16 ports ::= "ports" numports | "auto" .
17 rdrport ::= "port" portnum .
18 mapit ::= "map" | "bimap" .
19 fromto ::= "from" object "to" object .
20 ipmask ::= ip "/" bits | ip "/" mask | ip "netmask" mask .
21 dstipmask ::= ipmask | "range" ip "-" ip .
22 mapport ::= "portmap" tcpudp portspec .
23 mapoptions ::= [ tcpudp ] [ "frag" ] [ age ] [ clamp ] .
24 rdroptions ::= [ tcpudp ] [ rr ] [ "frag" ] [ age ] [ clamp ] .
25
26 object  :: = addr [ port-comp | port-range ] .
27 addr    :: = "any" | nummask | host-name [ "mask" ipaddr | "mask" hexnumber ] .
28 port-comp :: = "port" compare port-num .
29 port-range :: = "port" port-num range port-num .
30
31 rr ::= "round-robin" .
32 age ::= "age" decnumber [ "/" decnumber ] .
33 clamp ::= "mssclamp" decnumber .
34 tcpudp ::= "tcp/udp" | protocol .
35
36 protocol ::= protocol-name | decnumber .
37 nummask ::= host-name [ "/" decnumber ] .
38 portspec ::= "auto" | portnumber ":" portnumber .
39 portnumber ::= number { numbers } .
40 ifname ::= 'A' - 'Z' { 'A' - 'Z' } numbers .
41
42 numbers ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
43 .fi
44 .PP
45 In addition to this, # is used to mark the start of a comment and may
46 appear at the end of a line with a NAT rule (as described above) or on its
47 own lines.  Blank lines are ignored.
48 .PP
49 For standard NAT functionality, a rule should start with \fBmap\fP and then
50 proceeds to specify the interface for which outgoing packets will have their
51 source address rewritten.
52 .PP
53 Packets which will be rewritten can only be selected by matching the original
54 source address.  A netmask must be specified with the IP address.
55 .PP
56 The address selected for replacing the original is chosen from an IP#/netmask
57 pair.  A netmask of all 1's indicating a hostname is valid.  A netmask of
58 31 1's (255.255.255.254) is considered invalid as there is no space for
59 allocating host IP#'s after consideration for broadcast and network
60 addresses.
61 .PP
62 When remapping TCP and UDP packets, it is also possible to change the source
63 port number.  Either TCP or UDP or both can be selected by each rule, with a
64 range of port numbers to remap into given as \fBport-number:port-number\fP.
65 .SH COMMANDS
66 There are four commands recognised by IP Filter's NAT code:
67 .TP
68 .B map
69 that is used for mapping one address or network to another in an unregulated
70 round robin fashion;
71 .TP
72 .B rdr
73 that is used for redirecting packets to one IP address and port pair to
74 another;
75 .TP
76 .B bimap
77 for setting up bidirectional NAT between an external IP address and an internal
78 IP address and
79 .TP
80 .B map-block
81 which sets up static IP address based translation, based on a algorithm to
82 squeeze the addresses to be translated into the destination range.
83 .SH MATCHING
84 .PP
85 For basic NAT and redirection of packets, the address subject to change is used
86 along with its protocol to check if a packet should be altered.  The packet
87 \fImatching\fP part of the rule is to the left of the "->" in each rule.
88 .PP
89 Matching of packets has now been extended to allow more complex compares.
90 In place of the address which is to be translated, an IP address and port
91 number comparison can be made using the same expressions available with
92 \fBipf\fP.  A simple NAT rule could be written as:
93 .LP
94 .nf
95 map de0 10.1.0.0/16 -> 201.2.3.4/32
96 .fi
97 .LP
98 or as
99 .LP
100 .nf
101 map de0 from 10.1.0.0/16 to any -> 201.2.3.4/32
102 .fi
103 .LP
104 For even greater control, one may negate either of the "from" or "to" clauses
105 with a preceding exclamation mark ("!").  Please note that one may not use a
106 negated "from" within a \fBmap\fP rule or a negated "to" within a \fBrdr\fP
107 rule.  Such a rule might look like the following:
108 .LP
109 .nf
110 +map de0 from 10.1.0.0/16 ! to 10.1.0.0/16 -> 201.2.3.4/32
111 .fi
112 .PP
113 Only IP address and port numbers can be compared against.  This is available
114 with all NAT rules.
115 .SH COMMAND QUALIFIERS
116 At the end of each rule, a number of qualifiers can be used to change how
117 the rule works.  They are as follows:
118 .TP
119 protocol
120 A specific protocol may be given either by its name (as found in
121 /etc/protocols) or its number.  A special case for supporting both
122 TCP and UDP is allowed with the name \fBtcp/udp\fP.
123 .TP
124 .B round-robin
125 Once a rule with this term has been successfully used, it is put at the
126 bottom of the list of those available so that each one will get used, in
127 turn, in a list of matching left hand sides.
128 .TP
129 .B frag
130 This qualifier is currently has no impact on NAT operation.
131 .TP
132 .B age
133 If more refined timeouts are required than those available globally for
134 NAT settings, this allows you to set them for \fBnon-TCP\fP use.
135 .SH TRANSLATION
136 .PP
137 To the right of the "->" is the address and port specificaton which will be
138 written into the packet providing it has already successful matched the
139 prior constraints.  The case of redirections (\fBrdr\fP) is the simpliest:
140 the new destination address is that specified in the rule.  For \fBmap\fP
141 rules, the destination address will be one for which the tuple combining
142 the new source and destination is known to be unique.  If the packet is
143 either a TCP or UDP packet, the destination and source ports come into the
144 equation too.  If the tuple already exists, IP Filter will increment the
145 port number first, within the available range specified with \fBportmap\fP
146 and if there exists no unique tuple, the source address will be incremented
147 within the specified netmask.  If a unique tuple cannot be determined, then
148 the packet will not be translated.  The \fBmap-block\fP is more limited in
149 how it searches for a new, free and unique tuple, in that it will used an
150 algorithm to determine what the new source address should be, along with the
151 range of available ports - the IP address is never changed and nor does the
152 port number ever exceed its alloted range.
153 .SH KERNEL PROXIES
154 .PP
155 IP Filter comes with a few, simple, proxies built into the code that is loaded
156 into the kernel to allow secondary channels to be opened without forcing the
157 packets through a user program.
158 .SH TRNSPARENT PROXIES
159 .PP
160 True transparent proxying should be performed using the redirect (\fBrdr\fP)
161 rules directing ports to localhost (127.0.0.1) with the proxy program doing
162 a lookup through \fB/dev/ipnat\fP to determine the real source and address
163 of the connection.
164 .SH LOAD-BALANCING
165 .PP
166 Two options for use with \fBrdr\fP are available to support primitive,
167 \fIround-robin\fP based load balancing.  The first option allows for a
168 \fBrdr\fP to specify a second destination, as follows:
169 .LP
170 .nf
171 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.3,203.1.2.4 port 80 tcp
172 .fi
173 .LP
174 This would send alternate connections to either 203.1.2.3 or 203.1.2.4.
175 In scenarios where the load is being spread amongst a larger set of
176 servers, you can use:
177 .LP
178 .nf
179 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.3,203.1.2.4 port 80 tcp round-robin
180 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.5 port 80 tcp round-robin
181 .fi
182 .LP
183 In this case, a connection will be redirected to 203.1.2.3, then 203.1.2.4
184 and then 203.1.2.5 before going back to 203.1.2.3.  In accomplishing this,
185 the rule is removed from the top of the list and added to the end,
186 automatically, as required.  This will not effect the display of rules
187 using "ipnat -l", only the internal application order.
188 .SH EXAMPLES
189 .PP
190 This section deals with the \fBmap\fP command and it's variations.
191 .PP
192 To change IP#'s used internally from network 10 into an ISP provided 8 bit
193 subnet at 209.1.2.0 through the ppp0 interface, the following would be used:
194 .LP
195 .nf
196 map ppp0 10.0.0.0/8 -> 209.1.2.0/24
197 .fi
198 .PP
199 The obvious problem here is we're trying to squeeze over 16,000,000 IP
200 addresses into a 254 address space.  To increase the scope, remapping for TCP
201 and/or UDP, port remapping can be used;
202 .LP
203 .nf
204 map ppp0 10.0.0.0/8 -> 209.1.2.0/24 portmap tcp/udp 1025:65000
205 .fi
206 .PP
207 which falls only 527,566 `addresses' short of the space available in network
208 10.  If we were to combine these rules, they would need to be specified as
209 follows:
210 .LP
211 .nf
212 map ppp0 10.0.0.0/8 -> 209.1.2.0/24 portmap tcp/udp 1025:65000
213 map ppp0 10.0.0.0/8 -> 209.1.2.0/24
214 .fi
215 .PP
216 so that all TCP/UDP packets were port mapped and only other protocols, such as
217 ICMP, only have their IP# changed.  In some instaces, it is more appropriate
218 to use the keyword \fBauto\fP in place of an actual range of port numbers if
219 you want to guarantee simultaneous access to all within the given range.
220 However, in the above case, it would default to 1 port per IP address, since
221 we need to squeeze 24 bits of address space into 8.  A good example of how
222 this is used might be:
223 .LP
224 .nf
225 map ppp0 172.192.0.0/16 -> 209.1.2.0/24 portmap tcp/udp auto
226 .fi
227 .PP
228 which would result in each IP address being given a small range of ports to
229 use (252).  The problem here is that the \fBmap\fP directive tells the NAT
230 code to use the next address/port pair available for an outgoing connection,
231 resulting in no easily discernable relation between external addresses/ports
232 and internal ones.  This is overcome by using \fBmap-block\fP as follows:
233 .LP
234 .nf
235 map-block ppp0 172.192.0.0/16 -> 209.1.2.0/24 ports auto
236 .fi
237 .PP
238 For example, this would result in 172.192.0.0/24 being mapped to 209.1.2.0/32
239 with each address, from 172.192.0.0 to 172.192.0.255 having 252 ports of its
240 own.  As opposed to the above use of \fBmap\fP, if for some reason the user
241 of (say) 172.192.0.2 wanted 260 simultaneous connections going out, they would
242 be limited to 252 with \fBmap-block\fP but would just \fImove on\fP to the next
243 IP address with the \fBmap\fP command.
244 .SH FILES
245 /dev/ipnat
246 .br
247 /etc/services
248 .br
249 /etc/hosts
250 .SH SEE ALSO
251 ipnat(4), hosts(5), ipf(5), services(5), ipf(8), ipnat(8)