3 ipnat, ipnat.conf \- IP NAT file format
5 The format for files accepted by ipnat is described by the following grammar:
8 ipmap :: = mapblock | redir | map .
10 map ::= mapit ifname ipmask "->" ipmask [ mapport ] .
11 map ::= mapit ifname fromto "->" ipmask [ mapport ] .
12 mapblock ::= "map-block" ifname ipmask "->" ipmask [ ports ] .
13 redir ::= "rdr" ifname ipmask dport "->" ip [ "," ip ] rdrport options .
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 mapport ::= "portmap" tcpudp portnumber ":" portnumber .
22 options ::= [ tcpudp ] [ rr ] .
24 object = addr [ port-comp | port-range ] .
25 addr = "any" | nummask | host-name [ "mask" ipaddr | "mask" hexnumber ] .
26 port-comp = "port" compare port-num .
27 port-range = "port" port-num range port-num .
29 rr ::= "round-robin" .
30 tcpudp ::= "tcp" | "udp" | "tcp/udp" .
31 portnumber ::= number { numbers } | "auto" .
32 ifname ::= 'A' - 'Z' { 'A' - 'Z' } numbers .
34 numbers ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' .
37 For standard NAT functionality, a rule should start with \fBmap\fP and then
38 proceeds to specify the interface for which outgoing packets will have their
39 source address rewritten.
41 Packets which will be rewritten can only be selected by matching the original
42 source address. A netmask must be specified with the IP address.
44 The address selected for replacing the original is chosen from an IP#/netmask
45 pair. A netmask of all 1's indicating a hostname is valid. A netmask of
46 31 1's (255.255.255.254) is considered invalid as there is no space for
47 allocating host IP#'s after consideration for broadcast and network
50 When remapping TCP and UDP packets, it is also possible to change the source
51 port number. Either TCP or UDP or both can be selected by each rule, with a
52 range of port numbers to remap into given as \fBport-number:port-number\fP.
54 There are four commands recognised by IP Filter's NAT code:
57 that is used for mapping one address or network to another in an unregulated
61 that is used for redirecting packets to one IP address and port pair to
65 for setting up bidirectional NAT between an external IP address and an internal
69 which sets up static IP address based translation, based on a algorithm to
70 squeeze the addresses to be translated into the destination range.
73 For basic NAT and redirection of packets, the address subject to change is used
74 along with its protocol to check if a packet should be altered. The packet
75 \fImatching\fP part of the rule is to the left of the "->" in each rule.
77 Matching of packets has now been extended to allow more complex compares.
78 In place of the address which is to be translated, an IP address and port
79 number comparison can be made using the same expressions available with
80 \fBipf\fP. A simple NAT rule could be written as:
83 map de0 10.1.0.0/16 -> 201.2.3.4/32
89 map de0 from 10.1.0.0/16 to any -> 201.2.3.4/32
92 Only IP address and port numbers can be compared against. This is available
96 To the right of the "->" is the address and port specificaton which will be
97 written into the packet providing it has already successful matched the
98 prior constraints. The case of redirections (\fBrdr\fP) is the simpliest:
99 the new destination address is that specified in the rule. For \fBmap\fP
100 rules, the destination address will be one for which the tuple combining
101 the new source and destination is known to be unique. If the packet is
102 either a TCP or UDP packet, the destination and source ports come into the
103 equation too. If the tuple already exists, IP Filter will increment the
104 port number first, within the available range specified with \fBportmap\fP
105 and if there exists no unique tuple, the source address will be incremented
106 within the specified netmask. If a unique tuple cannot be determined, then
107 the packet will not be translated. The \fBmap-block\fP is more limited in
108 how it searches for a new, free and unique tuple, in that it will used an
109 algorithm to determine what the new source address should be, along with the
110 range of available ports - the IP address is never changed and nor does the
111 port number ever exceed its alloted range.
114 IP Filter comes with a few, simple, proxies built into the code that is loaded
115 into the kernel to allow secondary channels to be opened without forcing the
116 packets through a user program.
117 .SH TRNSPARENT PROXIES
119 True transparent proxying should be performed using the redirect (\fBrdr\fP)
120 rules directing ports to localhost (127.0.0.1) with the proxy program doing
121 a lookup through \fB/dev/ipnat\fP to determine the real source and address
125 Two options for use with \fBrdr\fP are available to support primitive,
126 \fIround-robin\fP based load balancing. The first option allows for a
127 \fBrdr\fP to specify a second destination, as follows:
130 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.3,203.1.2.4 port 80 tcp
133 This would send alternate connections to either 203.1.2.3 or 203.1.2.4.
134 In scenarios where the load is being spread amongst a larger set of
135 servers, you can use:
138 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.3,203.1.2.4 port 80 tcp round-robin
139 rdr le0 203.1.2.3/32 port 80 -> 203.1.2.5 port 80 tcp round-robin
142 In this case, a connection will be redirected to 203.1.2.3, then 203.1.2.4
143 and then 203.1.2.5 before going back to 203.1.2.3. In accomplishing this,
144 the rule is removed from the top of the list and added to the end,
145 automatically, as required. This will not effect the display of rules
146 using "ipnat -l", only the internal application order.
149 This section deals with the \fBmap\fP command and it's variations.
151 To change IP#'s used internally from network 10 into an ISP provided 8 bit
152 subnet at 209.1.2.0 through the ppp0 interface, the following would be used:
155 map ppp0 10.0.0.0/8 -> 209.1.2.0/24
158 The obvious problem here is we're trying to squeeze over 16,000,000 IP
159 addresses into a 254 address space. To increase the scope, remapping for TCP
160 and/or UDP, port remapping can be used;
163 map ppp0 10.0.0.0/8 -> 209.1.2.0/24 portmap tcp/udp 1025:65000
166 which falls only 527,566 `addresses' short of the space available in network
167 10. If we were to combine these rules, they would need to be specified as
171 map ppp0 10.0.0.0/8 -> 209.1.2.0/24 portmap tcp/udp 1025:65000
172 map ppp0 10.0.0.0/8 -> 209.1.2.0/24
175 so that all TCP/UDP packets were port mapped and only other protocols, such as
176 ICMP, only have their IP# changed. In some instaces, it is more appropriate
177 to use the keyword \fBauto\fP in place of an actual range of port numbers if
178 you want to guarantee simultaneous access to all within the given range.
179 However, in the above case, it would default to 1 port per IP address, since
180 we need to squeeze 24 bits of address space into 8. A good example of how
181 this is used might be:
184 map ppp0 172.192.0.0/16 -> 209.1.2.0/24 portmap tcp/udp auto
187 which would result in each IP address being given a small range of ports to
188 use (252). The problem here is that the \fBmap\fP directive tells the NAT
189 code to use the next address/port pair available for an outgoing connection,
190 resulting in no easily discernable relation between external addresses/ports
191 and internal ones. This is overcome by using \fBmap-block\fP as follows:
194 map-block ppp0 172.192.0.0/16 -> 209.1.2.0/24 ports auto
197 For example, this would result in 172.192.0.0/24 being mapped to 209.1.2.0/32
198 with each address, from 172.192.0.0 to 172.192.0.255 having 252 ports of its
199 own. As opposed to the above use of \fBmap\fP, if for some reason the user
200 of (say) 172.192.0.2 wanted 260 simultaneous connections going out, they would
201 be limited to 252 with \fBmap-block\fP but would just \fImove on\fP to the next
202 IP address with the \fBmap\fP command.
210 ipnat(4), hosts(5), ipf(5), services(5), ipf(8), ipnat(8)