2 .\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH IPNAT 7I "May 22, 2008"
8 ipnat \- IP Filter/NAT module interface
12 The \fBipnat\fR device provides interfaction with the NAT features of the
14 .SH APPLICATION PROGRAMMING INTERFACE
17 The NAT features programming model is a component of the Solaris IP Filter and
18 is accessed via the NAT device file \fB/dev/ipnat\fR. Opening the device for
19 reading or writing determines which ioctl calls can be successfully made.
23 The caller must construct a \fBipfobj\fR structure when issuing a
24 \fBSIOCGNATL\fR or \fBSIOCSTPUT\fR. The \fBipfobj\fR structure is then passed
25 to the ioctl call and is filled out with ipfo_type set to \fBIPFOBJ_value\fR.
26 \fBIPFOBJ_ value\fR provides a matching name for the structure, while ipfo_size
27 is set to the total size of the structure being passed and ipfo_ptr is set to
28 the structure address. The ipfo_rev structure should be set to the current
29 value of IPFILTER_VERSION, while ipfo_offset and ipfo_xxxpad should be set to
35 * Structure used with SIOCGNATL/SIOCSTPUT.
38 * Object structure description. For passing through in ioctls.
40 typedef struct ipfobj {
41 u_32_t ipfo_rev; /* IPFilter version (IPFILTER_VERSION) */
42 u_32_t ipfo_size; /* size of object at ipfo_ptr */
43 void *ipfo_ptr; /* pointer to object */
44 int ipfo_type; /* type of object being pointed to */
45 int ipfo_offset; /* bytes from ipfo_ptr where to start */
46 u_char ipfo_xxxpad[32]; /* reserved for future use */
49 #define IPFILTER_VERSION 4010901 /* IPFilter version */
50 #define IPFOBJ_NATSAVE 8 /* struct nat_save */
51 #define IPFOBJ_NATLOOKUP 9 /* struct natlookup */
57 The following ioctl() calls may be used to manipulate the ipnat sub-system
58 inside of ipf. Note that the ipnat driver only accept calls from applications
59 using the same data model as the kernel. In other words, 64-bit kernels can
60 only accept calls from 64-bit applications. Calls from 32-bit applications fail
68 Set or clear the NAT lock to prevent table updates attributable to packet
78 Search the NAT table for the rdr entry that matches the fields in the natlookup
79 structure. The caller must populate the structure with the address/port
80 information of the accepted TCP connection (nl_inip, nl_inport) and the
81 address/port information of the peer (nl_outip, nl_outport). The nl_flags field
82 must have the IPN_TCP option set. All other fields must be set to 0. If the
83 call succeeds, nl_realip and nl_realport are set to the real destination
84 address and port, respectively. The nl_inport and nl_outport fields must be in
87 If \fBIPN_FINDFORWARD\fR is set in nl_flags, a check is made to see if it is
88 possible to create an outgoing NAT session by checking if a packet coming from
89 (nl_realip,nl_realport) and destined for (nl_outip,nl_outport) can be
90 translated. If translation is possible, the flag remains set, otherwise it is
91 cleared in the structure returned to the caller.
96 * Structure used with SIOCGNATL.
98 typedef struct natlookup {
100 i6addr_t nl_outipaddr;
101 i6addr_t nl_realipaddr;
109 #define nl_inip nl_inipaddr.in4
110 #define nl_outip nl_outipaddr.in4
111 #define nl_realip nl_realipaddr.in4
112 #define nl_inip6 nl_inipaddr.in6
113 #define nl_outip6 nl_outipaddr.in6
114 #define nl_realip6 nl_realipaddr.in6
117 * Accepted values for nl_flags
119 #define IPN_TCP 0x00001
120 #define IPN_FINDFORWARD 0x400000
129 \fB\fBSIOCSTPUT\fR\fR
132 Move a NAT mapping structure from user space into the kernel. This ioctl is
133 used by \fBipfs\fR(1M) to restore NAT sessions saved in
134 \fB/var/db/ipf/ipnat.ipf\fR. The nat_save structure must have its ipn_nat and
135 ipn_ipnat structures filled out correctly. Fields not assigned a value must be
136 initialised to 0. All pointer fields are adjusted, as appropriate, once the
137 structure is passed into the kernel and none are preserved.
139 To create a translation, the following fields must be set:
142 Interface name - The interface name on which the host is to be exited must be
143 set in nat_ifnames[0].
147 Local IP address and port number - The connection's local IP address and port
148 number are stored in network byte order using nat_inip/nat_inport.
152 Destination address/port - The destination address/port are stored in
157 Target address/port - The translation's target address/port is stored in
158 nat_outip/nat_outport.
160 The caller must also precalculate the checksum adjustments necessary to
161 complete the translation and store those values in nat_sumd (delta required for
162 TCP header) and nat_ipsumd (delta required for IP header).
167 * Structures used with SIOCSTPUT.
169 typedef struct nat_save {
172 struct ipnat ipn_ipnat;
173 struct frentry ipn_fr;
180 struct nat *nat_next;
181 struct nat **nat_pnext;
182 struct nat *nat_hnext[2];
183 struct nat **nat_phnext[2];
184 struct hostmap *nat_hm;
187 struct ipstate *nat_state;
188 struct ap_session *nat_aps;
190 struct ipnat *nat_ptr;
201 U_QUAD_T nat_pkts[2];
202 U_QUAD_T nat_bytes[2];
215 char nat_ifnames[2][LIFNAMSIZ];
220 #define nat_inip nat_inip6.in4
221 #define nat_outip nat_outip6.in4
222 #define nat_oip nat_oip6.in4
223 #define nat_inport nat_un.nat_unt.ts_sport
224 #define nat_outport nat_un.nat_unt.ts_dport
228 #define NAT_INBOUND 0
229 #define NAT_OUTBOUND 1
231 * Definitions for nat_flags
233 #define NAT_TCP 0x0001 /* IPN_TCP */
242 The following example shows how to prepare and use \fBSIOCSTPUT\fR to insert a
243 NAT session directly into the table. Note that the usual TCP/IP code is omitted
247 In the code segment below, incoming_fd is the TCP connection file descriptor
248 that is accepted as part of the redirect process, while remote_fd is the
249 outgoing TCP connection to the remote server being translated back to the
250 original IP address/port pair.
255 The following ipnat headers must be included before you can use the code shown
260 #include <netinet/in.h>
261 #include <arpa/inet.h>
263 #include <netinet/ipl.h>
264 #include <netinet/ip_compat.h>
265 #include <netinet/ip_fil.h>
266 #include <netinet/ip_nat.h>
277 In the example below, various code fragments have been excluded to enhance
284 translate_connection(int incoming_fd)
286 struct sockaddr_in usin;
287 struct natlookup nlp;
295 memset(&ns, 0, sizeof(ns));
298 namelen = sizeof(usin);
299 getsockname(remote_fd, (struct sockaddr *)&usin, &namelen);
301 namelen = sizeof(sin);
302 getpeername(incoming_fd, (struct sockaddr *) &sin, &namelen);
304 namelen = sizeof(sloc);
305 getsockname(incoming_fd, (struct sockaddr *) &sloc, &namelen);
307 bzero((char *) &obi, sizeof(obj));
308 obj.ipfo_rev = IPFILTER_VERSION;
309 obj.ipfo_size = sizeof(nlp);
311 obj.ipfo_type = IPFOBJ_NATLOOKUP;
314 * Build up the NAT natlookup structure.
316 bzero((char *) &nlp, sizeof(nlp));
317 nlp.nl_outip = sin.sin_addr;
318 nlp.nl_inip = sloc.sin_addr;
319 nlp.nl_flags = IPN_TCP;
320 nlp.nl_outport = ntohs(sin.sin_port);
321 nlp.nl_inport = ntohs(sloc.sin_port);
324 * Open the NAT device and lookup the mapping pair.
326 nat_fd = open(IPNAT_NAME, O_RDWR);
327 if (ioctl(nat_fd, SIOCGNATL, &obj) != 0)
330 nat->nat_inip = usin.sin_addr;
331 nat->nat_outip = nlp.nl_outip;
332 nat->nat_oip = nlp.nl_realip;
334 sum1 = LONG_SUM(ntohl(usin.sin_addr.s_addr)) +
335 ntohs(usin.sin_port);
336 sum2 = LONG_SUM(ntohl(nat->nat_outip.s_addr)) +
337 ntohs(nlp.nl_outport);
338 CALC_SUMD(sum1, sum2, sumd);
339 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16);
340 nat->nat_sumd[1] = nat->nat_sumd[0];
342 sum1 = LONG_SUM(ntohl(usin.sin_addr.s_addr));
343 sum2 = LONG_SUM(ntohl(nat->nat_outip.s_addr));
344 CALC_SUMD(sum1, sum2, sumd);
345 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16);
347 nat->nat_inport = usin.sin_port;
348 nat->nat_outport = nlp.nl_outport;
349 nat->nat_oport = nlp.nl_realport;
351 nat->nat_flags = IPN_TCPUDP;
354 * Prepare the ipfobj structure, accordingly.
356 bzero((char *)&obi, sizeof(obj));
357 obj.ipfo_rev = IPFILTER_VERSION;
358 obj.ipfo_size = sizeof(*nsp);
360 obj.ipfo_type = IPFOBJ_NATSAVE;
363 if (ioctl(nat_fd, SIOCSTPUT, &obj) != 0)
364 fprintf(stderr, "Error occurred\en");
366 return connect(rem_fd, (struct sockaddr ) &usin, sizeof(usin));
378 The device has been opened for reading only. To succeed, the ioctl call must be
379 opened for both reading and writing. The call may be returned if it is
380 privileged and the calling process did not assert {\fBPRIV_SYS_NET_CONFIG\fR}
381 in the effective set.
390 More memory was allocated than the kernel can provide. The call may also be
391 returned if the application inserts a NAT entry that exceeds the hash bucket
392 chain's maximum length.
401 The calling process specified an invalid pointer in the ipfobj structure.
410 The calling process detected a parameter or field set to an unacceptable value.
419 The calling process, via \fBSIOCSTPUT\fR, attempted to add a NAT entry that
420 already exists in the NAT table.
429 The calling process called \fBSIOCSTPUT\fR before setting the SI_NEWFR flag and
430 providing a pointer in the nat_fr field that cannot be found in the current
440 The calling process issued a \fBSIOCSTPUT\fR before issuing a SIOCSTLCK.
446 See \fBattributes\fR(5) for descriptions of the following attributes:
454 ATTRIBUTE TYPE ATTRIBUTE VALUE
456 Interface Stability Committed
462 \fBipfs\fR(1M), \fBipnat\fR(1M), \fBioctl\fR(2), \fBattributes\fR(5)